Merge FFmpeg from upstream for M28

Conflicts:
	libavcodec/h264_cabac.c
	libavcodec/h264_cavlc.c
	libavformat/matroskadec.c
diff --git a/.gitignore b/.gitignore
index 8cc9653..191d154 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,6 +6,8 @@
 *.dylib
 *.exe
 *.exp
+*.gcda
+*.gcno
 *.h.c
 *.ilk
 *.lib
@@ -24,11 +26,13 @@
 /ffprobe
 /ffserver
 /config.*
+/coverage.info
 /version.h
 /doc/*.1
 /doc/*.3
 /doc/*.html
 /doc/*.pod
+/doc/config.texi
 /doc/avoptions_codec.texi
 /doc/avoptions_format.texi
 /doc/examples/decoding_encoding
@@ -43,6 +47,7 @@
 /doc/fate.txt
 /doc/doxy/html/
 /doc/print_options
+/lcov/
 /libavcodec/*_tablegen
 /libavcodec/*_tables.c
 /libavcodec/*_tables.h
@@ -52,6 +57,7 @@
 /tests/data/
 /tests/rotozoom
 /tests/tiny_psnr
+/tests/tiny_ssim
 /tests/videogen
 /tests/vsynth1/
 /tools/aviocat
@@ -76,4 +82,4 @@
 *.target.mk
 /*.targets
 /*.vcxproj*
-/*.xml
\ No newline at end of file
+/*.xml
diff --git a/Changelog b/Changelog
index 6e7bac6..f71e0d8 100644
--- a/Changelog
+++ b/Changelog
@@ -2,6 +2,40 @@
 releases are sorted from youngest to oldest.
 
 version <next>:
+- curves filter
+- reference-counting for AVFrame and AVPacket data
+- ffmpeg now fails when input options are used for output file
+  or vice versa
+- support for Monkey's Audio versions from 3.93
+- perms and aperms filters
+- audio filtering support in ffplay
+- 10% faster aac encoding on x86 and MIPS
+- sine audio filter source
+- WebP demuxing and decoding support
+- new ffmpeg options -filter_script and -filter_complex_script, which allow a
+  filtergraph description to be read from a file
+- OpenCL support
+- audio phaser filter
+- separatefields filter
+- libquvi demuxer
+- uniform options syntax across all filters
+- telecine filter
+- new interlace filter
+- smptehdbars source
+- inverse telecine filters (fieldmatch and decimate)
+- colorbalance filter
+- colorchannelmixer filter
+- The matroska demuxer can now output proper verbatim ASS packets. It will
+  become the default at the next libavformat major bump.
+- decent native animated GIF encoding
+- asetrate filter
+- interleave filter
+- timeline editing with filters
+- vidstabdetect and vidstabtransform filters for video stabilization using
+  the vid.stab library
+
+
+version 1.2:
 
 - VDPAU hardware acceleration through normal hwaccel
 - SRTP support
@@ -23,6 +57,7 @@
 - noise filter ported from libmpcodecs
 - Subtitles character encoding conversion
 - blend filter
+- stereo3d filter ported from libmpcodecs
 
 
 version 1.1:
diff --git a/LICENSE b/LICENSE
index 14e3e0f..8760753 100644
--- a/LICENSE
+++ b/LICENSE
@@ -39,6 +39,7 @@
     - vf_noise.c
     - vf_pp.c
     - vf_smartblur.c
+    - vf_stereo3d.c
     - vf_super2xsai.c
     - vf_tinterlace.c
     - vf_yadif.c
@@ -68,8 +69,14 @@
 compatible libraries
 --------------------
 
-The libcdio, libx264, libxavs and libxvid libraries are under GPL. When
-combining them with FFmpeg, FFmpeg needs to be licensed as GPL as well by
+The following libraries are under GPL:
+    - libcdio
+    - libutvideo
+    - libvidstab
+    - libx264
+    - libxavs
+    - libxvid
+When combining them with FFmpeg, FFmpeg needs to be licensed as GPL as well by
 passing --enable-gpl to configure.
 
 The OpenCORE and VisualOn libraries are under the Apache License 2.0. That
diff --git a/MAINTAINERS b/MAINTAINERS
index 3319359..3b3cdc5 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -67,6 +67,7 @@
   mathematics.c, mathematics.h          Michael Niedermayer
   integer.c, integer.h                  Michael Niedermayer
   bswap.h
+  opencl.c, opencl.h                    Wei Gao
 
 
 libavcodec
@@ -163,6 +164,7 @@
   interplayvideo.c                      Mike Melanson
   ivi*                                  Kostya Shishkov
   jacosub*                              Clément Bœsch
+  jpeg2000*                             Nicolas Bertrand
   jpeg_ls.c                             Kostya Shishkov
   jvdec.c                               Peter Ross
   kmvc.c                                Kostya Shishkov
@@ -189,6 +191,7 @@
   mpc*                                  Kostya Shishkov
   mpeg12.c, mpeg12data.h                Michael Niedermayer
   mpegvideo.c, mpegvideo.h              Michael Niedermayer
+  mqc*                                  Nicolas Bertrand
   msmpeg4.c, msmpeg4data.h              Michael Niedermayer
   msrle.c                               Mike Melanson
   msvideo1.c                            Mike Melanson
@@ -314,6 +317,7 @@
   astdec.c                              Paul B Mahol
   astenc.c                              James Almer
   avi*                                  Michael Niedermayer
+  avisynth.c                            AvxSynth Team
   avr.c                                 Paul B Mahol
   bink.c                                Peter Ross
   brstm.c                               Paul B Mahol
@@ -445,10 +449,11 @@
 Releases
 ========
 
+1.2                                     Michael Niedermayer
 1.1                                     Michael Niedermayer
 1.0                                     Michael Niedermayer
-0.11                                    Michael Niedermayer
 
+If you want to maintain an older release, please contact us
 
 
 GnuPG Fingerprints of maintainers and contributors
@@ -481,3 +486,4 @@
 Stefano Sabatini              0D0B AD6B 5330 BBAD D3D6 6A0C 719C 2839 FC43 2D5F
 Stephan Hilb                  4F38 0B3A 5F39 B99B F505 E562 8D5C 5554 4E17 8863
 Tomas Härdin                  A79D 4E3D F38F 763F 91F5 8B33 A01E 8AE0 41BB 2551
+Wei Gao                       4269 7741 857A 0E60 9EC5 08D2 4744 4EFA 62C1 87B9
diff --git a/Makefile b/Makefile
index 07821d1..7addffb 100644
--- a/Makefile
+++ b/Makefile
@@ -20,7 +20,7 @@
 
 OBJS        = cmdutils.o $(EXEOBJS)
 OBJS-ffmpeg = ffmpeg_opt.o ffmpeg_filter.o
-TESTTOOLS   = audiogen videogen rotozoom tiny_psnr base64
+TESTTOOLS   = audiogen videogen rotozoom tiny_psnr tiny_ssim base64
 HOSTPROGS  := $(TESTTOOLS:%=tests/%) doc/print_options
 TOOLS       = qt-faststart trasher
 TOOLS-$(CONFIG_ZLIB) += cws2fws
@@ -154,25 +154,16 @@
 	$(RM) $(ALLPROGS) $(ALLPROGS_G)
 	$(RM) $(CLEANSUFFIXES)
 	$(RM) $(CLEANSUFFIXES:%=tools/%)
-	$(RM) coverage.info
 	$(RM) -r coverage-html
+	$(RM) -rf coverage.info lcov
 
 distclean::
 	$(RM) $(DISTCLEANSUFFIXES)
-	$(RM) config.* .version version.h libavutil/avconfig.h libavcodec/codec_names.h
+	$(RM) config.* .config libavutil/avconfig.h .version version.h libavcodec/codec_names.h
 
 config:
 	$(SRC_PATH)/configure $(value FFMPEG_CONFIGURATION)
 
-# Without the sed genthml thinks "libavutil" and "./libavutil" are two different things
-coverage.info: $(wildcard *.gcda *.gcno */*.gcda */*.gcno */*/*.gcda */*/*.gcno)
-	$(Q)lcov -c -d . -b . | sed -e 's#/./#/#g' > $@
-
-coverage-html: coverage.info
-	$(Q)mkdir -p $@
-	$(Q)genhtml -o $@ $<
-	$(Q)touch $@
-
 check: all alltools examples testprogs fate
 
 include $(SRC_PATH)/doc/Makefile
diff --git a/cmdutils.c b/cmdutils.c
index c6fde33..b9985d6 100644
--- a/cmdutils.c
+++ b/cmdutils.c
@@ -36,9 +36,7 @@
 #include "libavresample/avresample.h"
 #include "libswscale/swscale.h"
 #include "libswresample/swresample.h"
-#if CONFIG_POSTPROC
 #include "libpostproc/postprocess.h"
-#endif
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
 #include "libavutil/bprint.h"
@@ -58,6 +56,10 @@
 #include <sys/time.h>
 #include <sys/resource.h>
 #endif
+#if CONFIG_OPENCL
+#include "libavutil/opencl.h"
+#endif
+
 
 static int init_report(const char *env);
 
@@ -280,7 +282,7 @@
     if (po->flags & OPT_STRING) {
         char *str;
         str = av_strdup(arg);
-//         av_freep(dst);
+        av_freep(dst);
         *(char **)dst = str;
     } else if (po->flags & OPT_BOOL || po->flags & OPT_INT) {
         *(int *)dst = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
@@ -296,7 +298,8 @@
         int ret = po->u.func_arg(optctx, opt, arg);
         if (ret < 0) {
             av_log(NULL, AV_LOG_ERROR,
-                   "Failed to set value '%s' for option '%s'\n", arg, opt);
+                   "Failed to set value '%s' for option '%s': %s\n",
+                   arg, opt, av_err2str(ret));
             return ret;
         }
     }
@@ -380,6 +383,16 @@
     for (i = 0; i < g->nb_opts; i++) {
         Option *o = &g->opts[i];
 
+        if (g->group_def->flags &&
+            !(g->group_def->flags & o->opt->flags)) {
+            av_log(NULL, AV_LOG_ERROR, "Option %s (%s) cannot be applied to "
+                   "%s %s -- you are trying to apply an input option to an "
+                   "output file or vice versa. Move this option before the "
+                   "file it belongs to.\n", o->key, o->opt->help,
+                   g->group_def->name, g->arg);
+            return AVERROR(EINVAL);
+        }
+
         av_log(NULL, AV_LOG_DEBUG, "Applying option %s (%s) with argument %s.\n",
                o->key, o->opt->help, o->val);
 
@@ -494,10 +507,10 @@
         consumed = 1;
     }
     if ((o = av_opt_find(&fc, opt, NULL, 0,
-                              AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
+                         AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ))) {
         av_dict_set(&format_opts, opt, arg, FLAGS);
-        if(consumed)
-            av_log(NULL, AV_LOG_VERBOSE, "Routing %s to codec and muxer layer\n", opt);
+        if (consumed)
+            av_log(NULL, AV_LOG_VERBOSE, "Routing option %s to both codec and muxer layer\n", opt);
         consumed = 1;
     }
 #if CONFIG_SWSCALE
@@ -784,6 +797,13 @@
     int level;
     int i;
 
+    tail = strstr(arg, "repeat");
+    av_log_set_flags(tail ? 0 : AV_LOG_SKIP_REPEATED);
+    if (tail == arg)
+        arg += 6 + (arg[6]=='+');
+    if(tail && !*arg)
+        return 0;
+
     for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
         if (!strcmp(log_levels[i].name, arg)) {
             av_log_set_level(log_levels[i].level);
@@ -939,6 +959,26 @@
     return 0;
 }
 
+#if CONFIG_OPENCL
+int opt_opencl(void *optctx, const char *opt, const char *arg)
+{
+    char *key, *value;
+    const char *opts = arg;
+    int ret = 0;
+    while (*opts) {
+        ret = av_opt_get_key_value(&opts, "=", ":", 0, &key, &value);
+        if (ret < 0)
+            return ret;
+        ret = av_opencl_set_option(key, value);
+        if (ret < 0)
+            return ret;
+        if (*opts)
+            opts++;
+    }
+    return ret;
+}
+#endif
+
 void print_error(const char *filename, int err)
 {
     char errbuf[128];
@@ -991,12 +1031,10 @@
     PRINT_LIB_INFO(avformat, AVFORMAT, flags, level);
     PRINT_LIB_INFO(avdevice, AVDEVICE, flags, level);
     PRINT_LIB_INFO(avfilter, AVFILTER, flags, level);
-//    PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
+    PRINT_LIB_INFO(avresample, AVRESAMPLE, flags, level);
     PRINT_LIB_INFO(swscale,  SWSCALE,  flags, level);
     PRINT_LIB_INFO(swresample,SWRESAMPLE,  flags, level);
-#if CONFIG_POSTPROC
     PRINT_LIB_INFO(postproc, POSTPROC, flags, level);
-#endif
 }
 
 static void print_program_info(int flags, int level)
@@ -1410,31 +1448,36 @@
 
 int show_filters(void *optctx, const char *opt, const char *arg)
 {
-    AVFilter av_unused(**filter) = NULL;
+    const AVFilter av_unused(*filter) = NULL;
     char descr[64], *descr_cur;
     int i, j;
     const AVFilterPad *pad;
 
-    printf("Filters:\n");
+    printf("Filters:\n"
+           "  A = Audio input/output\n"
+           "  V = Video input/output\n"
+           "  N = Dynamic number and/or type of input/output\n"
+           "  | = Source or sink filter\n");
 #if CONFIG_AVFILTER
-    while ((filter = av_filter_next(filter)) && *filter) {
+    while ((filter = avfilter_next(filter))) {
         descr_cur = descr;
         for (i = 0; i < 2; i++) {
             if (i) {
                 *(descr_cur++) = '-';
                 *(descr_cur++) = '>';
             }
-            pad = i ? (*filter)->outputs : (*filter)->inputs;
+            pad = i ? filter->outputs : filter->inputs;
             for (j = 0; pad && pad[j].name; j++) {
                 if (descr_cur >= descr + sizeof(descr) - 4)
                     break;
                 *(descr_cur++) = get_media_type_char(pad[j].type);
             }
             if (!j)
-                *(descr_cur++) = '|';
+                *(descr_cur++) = ((!i && (filter->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)) ||
+                                  ( i && (filter->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS))) ? 'N' : '|';
         }
         *descr_cur = 0;
-        printf("%-16s %-10s %s\n", (*filter)->name, descr, (*filter)->description);
+        printf("%-16s %-10s %s\n", filter->name, descr, filter->description);
     }
 #endif
     return 0;
@@ -1597,6 +1640,58 @@
         show_help_children(fmt->priv_class, AV_OPT_FLAG_ENCODING_PARAM);
 }
 
+#if CONFIG_AVFILTER
+static void show_help_filter(const char *name)
+{
+#if CONFIG_AVFILTER
+    const AVFilter *f = avfilter_get_by_name(name);
+    int i, count;
+
+    if (!name) {
+        av_log(NULL, AV_LOG_ERROR, "No filter name specified.\n");
+        return;
+    } else if (!f) {
+        av_log(NULL, AV_LOG_ERROR, "Unknown filter '%s'.\n", name);
+        return;
+    }
+
+    printf("Filter %s\n", f->name);
+    if (f->description)
+        printf("  %s\n", f->description);
+    printf("    Inputs:\n");
+    count = avfilter_pad_count(f->inputs);
+    for (i = 0; i < count; i++) {
+        printf("       #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
+               media_type_string(avfilter_pad_get_type(f->inputs, i)));
+    }
+    if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
+        printf("        dynamic (depending on the options)\n");
+    else if (!count)
+        printf("        none (source filter)\n");
+
+    printf("    Outputs:\n");
+    count = avfilter_pad_count(f->outputs);
+    for (i = 0; i < count; i++) {
+        printf("       #%d: %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
+               media_type_string(avfilter_pad_get_type(f->outputs, i)));
+    }
+    if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
+        printf("        dynamic (depending on the options)\n");
+    else if (!count)
+        printf("        none (sink filter)\n");
+
+    if (f->priv_class)
+        show_help_children(f->priv_class, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM |
+                                          AV_OPT_FLAG_AUDIO_PARAM);
+    if (f->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)
+        printf("This filter has support for timeline through the 'enable' option.\n");
+#else
+    av_log(NULL, AV_LOG_ERROR, "Build without libavfilter; "
+           "can not to satisfy request\n");
+#endif
+}
+#endif
+
 int show_help(void *optctx, const char *opt, const char *arg)
 {
     char *topic, *par;
@@ -1617,6 +1712,10 @@
         show_help_demuxer(par);
     } else if (!strcmp(topic, "muxer")) {
         show_help_muxer(par);
+#if CONFIG_AVFILTER
+    } else if (!strcmp(topic, "filter")) {
+        show_help_filter(par);
+#endif
     } else {
         show_help_default(topic, par);
     }
@@ -1628,7 +1727,7 @@
 int read_yesno(void)
 {
     int c = getchar();
-    int yesno = (toupper(c) == 'Y');
+    int yesno = (av_toupper(c) == 'Y');
 
     while (c != '\n' && c != EOF)
         c = getchar();
@@ -1830,144 +1929,3 @@
     }
     return array;
 }
-
-static int alloc_buffer(FrameBuffer **pool, AVCodecContext *s, FrameBuffer **pbuf)
-{
-    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
-    FrameBuffer *buf;
-    int i, ret;
-    int pixel_size;
-    int h_chroma_shift, v_chroma_shift;
-    int edge = 32; // XXX should be avcodec_get_edge_width(), but that fails on svq1
-    int w = s->width, h = s->height;
-
-    if (!desc)
-        return AVERROR(EINVAL);
-    pixel_size = desc->comp[0].step_minus1 + 1;
-
-    buf = av_mallocz(sizeof(*buf));
-    if (!buf)
-        return AVERROR(ENOMEM);
-
-    avcodec_align_dimensions(s, &w, &h);
-
-    if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
-        w += 2*edge;
-        h += 2*edge;
-    }
-
-    if ((ret = av_image_alloc(buf->base, buf->linesize, w, h,
-                              s->pix_fmt, 32)) < 0) {
-        av_freep(&buf);
-        av_log(s, AV_LOG_ERROR, "alloc_buffer: av_image_alloc() failed\n");
-        return ret;
-    }
-
-    avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
-    for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
-        const int h_shift = i==0 ? 0 : h_chroma_shift;
-        const int v_shift = i==0 ? 0 : v_chroma_shift;
-        if ((s->flags & CODEC_FLAG_EMU_EDGE) || !buf->linesize[i] || !buf->base[i])
-            buf->data[i] = buf->base[i];
-        else
-            buf->data[i] = buf->base[i] +
-                           FFALIGN((buf->linesize[i]*edge >> v_shift) +
-                                   (pixel_size*edge >> h_shift), 32);
-    }
-    buf->w       = s->width;
-    buf->h       = s->height;
-    buf->pix_fmt = s->pix_fmt;
-    buf->pool    = pool;
-
-    *pbuf = buf;
-    return 0;
-}
-
-int codec_get_buffer(AVCodecContext *s, AVFrame *frame)
-{
-    FrameBuffer **pool = s->opaque;
-    FrameBuffer *buf;
-    int ret, i;
-
-    if(av_image_check_size(s->width, s->height, 0, s) || s->pix_fmt<0) {
-        av_log(s, AV_LOG_ERROR, "codec_get_buffer: image parameters invalid\n");
-        return -1;
-    }
-
-    if (!*pool && (ret = alloc_buffer(pool, s, pool)) < 0)
-        return ret;
-
-    buf              = *pool;
-    *pool            = buf->next;
-    buf->next        = NULL;
-    if (buf->w != s->width || buf->h != s->height || buf->pix_fmt != s->pix_fmt) {
-        av_freep(&buf->base[0]);
-        av_free(buf);
-        if ((ret = alloc_buffer(pool, s, &buf)) < 0)
-            return ret;
-    }
-    av_assert0(!buf->refcount);
-    buf->refcount++;
-
-    frame->opaque        = buf;
-    frame->type          = FF_BUFFER_TYPE_USER;
-    frame->extended_data = frame->data;
-
-    for (i = 0; i < FF_ARRAY_ELEMS(buf->data); i++) {
-        frame->base[i]     = buf->base[i];  // XXX h264.c uses base though it shouldn't
-        frame->data[i]     = buf->data[i];
-        frame->linesize[i] = buf->linesize[i];
-    }
-
-    return 0;
-}
-
-static void unref_buffer(FrameBuffer *buf)
-{
-    FrameBuffer **pool = buf->pool;
-
-    av_assert0(buf->refcount > 0);
-    buf->refcount--;
-    if (!buf->refcount) {
-        FrameBuffer *tmp;
-        for(tmp= *pool; tmp; tmp= tmp->next)
-            av_assert1(tmp != buf);
-
-        buf->next = *pool;
-        *pool = buf;
-    }
-}
-
-void codec_release_buffer(AVCodecContext *s, AVFrame *frame)
-{
-    FrameBuffer *buf = frame->opaque;
-    int i;
-
-    if(frame->type!=FF_BUFFER_TYPE_USER) {
-        avcodec_default_release_buffer(s, frame);
-        return;
-    }
-
-    for (i = 0; i < FF_ARRAY_ELEMS(frame->data); i++)
-        frame->data[i] = NULL;
-
-    unref_buffer(buf);
-}
-
-void filter_release_buffer(AVFilterBuffer *fb)
-{
-    FrameBuffer *buf = fb->priv;
-    av_free(fb);
-    unref_buffer(buf);
-}
-
-void free_buffer_pool(FrameBuffer **pool)
-{
-    FrameBuffer *buf = *pool;
-    while (buf) {
-        *pool = buf->next;
-        av_freep(&buf->base[0]);
-        av_free(buf);
-        buf = *pool;
-    }
-}
diff --git a/cmdutils.h b/cmdutils.h
index 08c92ce..8a5dada 100644
--- a/cmdutils.h
+++ b/cmdutils.h
@@ -90,6 +90,8 @@
 
 int opt_codec_debug(void *optctx, const char *opt, const char *arg);
 
+int opt_opencl(void *optctx, const char *opt, const char *arg);
+
 /**
  * Limit the execution time.
  */
@@ -162,6 +164,8 @@
                                    an int containing element count in the array. */
 #define OPT_TIME  0x10000
 #define OPT_DOUBLE 0x20000
+#define OPT_INPUT  0x40000
+#define OPT_OUTPUT 0x80000
      union {
         void *dst_ptr;
         int (*func_arg)(void *, const char *, const char *);
@@ -190,13 +194,13 @@
 void show_help_children(const AVClass *class, int flags);
 
 /**
- * Per-avtool specific help handler. Implemented in each
- * avtool, called by show_help().
+ * Per-fftool specific help handler. Implemented in each
+ * fftool, called by show_help().
  */
 void show_help_default(const char *opt, const char *arg);
 
 /**
- * Generic -h handler common to all avtools.
+ * Generic -h handler common to all fftools.
  */
 int show_help(void *optctx, const char *opt, const char *arg);
 
@@ -242,6 +246,11 @@
      * are terminated by a non-option argument (e.g. ffmpeg output files)
      */
     const char *sep;
+    /**
+     * Option flags that must be set on each option that is
+     * applied to this group
+     */
+    int flags;
 } OptionGroupDef;
 
 typedef struct OptionGroup {
@@ -517,52 +526,11 @@
  */
 void *grow_array(void *array, int elem_size, int *size, int new_size);
 
+#define media_type_string av_get_media_type_string
+
 #define GROW_ARRAY(array, nb_elems)\
     array = grow_array(array, sizeof(*array), &nb_elems, nb_elems + 1)
 
-typedef struct FrameBuffer {
-    uint8_t *base[4];
-    uint8_t *data[4];
-    int  linesize[4];
-
-    int h, w;
-    enum AVPixelFormat pix_fmt;
-
-    int refcount;
-    struct FrameBuffer **pool;  ///< head of the buffer pool
-    struct FrameBuffer *next;
-} FrameBuffer;
-
-/**
- * Get a frame from the pool. This is intended to be used as a callback for
- * AVCodecContext.get_buffer.
- *
- * @param s codec context. s->opaque must be a pointer to the head of the
- *          buffer pool.
- * @param frame frame->opaque will be set to point to the FrameBuffer
- *              containing the frame data.
- */
-int codec_get_buffer(AVCodecContext *s, AVFrame *frame);
-
-/**
- * A callback to be used for AVCodecContext.release_buffer along with
- * codec_get_buffer().
- */
-void codec_release_buffer(AVCodecContext *s, AVFrame *frame);
-
-/**
- * A callback to be used for AVFilterBuffer.free.
- * @param fb buffer to free. fb->priv must be a pointer to the FrameBuffer
- *           containing the buffer data.
- */
-void filter_release_buffer(AVFilterBuffer *fb);
-
-/**
- * Free all the buffers in the pool. This must be called after all the
- * buffers have been released.
- */
-void free_buffer_pool(FrameBuffer **pool);
-
 #define GET_PIX_FMT_NAME(pix_fmt)\
     const char *name = av_get_pix_fmt_name(pix_fmt);
 
diff --git a/cmdutils_common_opts.h b/cmdutils_common_opts.h
index 1a84564..b57abaf 100644
--- a/cmdutils_common_opts.h
+++ b/cmdutils_common_opts.h
@@ -14,8 +14,11 @@
     { "pix_fmts"   , OPT_EXIT, {.func_arg = show_pix_fmts },    "show available pixel formats" },
     { "layouts"    , OPT_EXIT, {.func_arg = show_layouts  },    "show standard channel layouts" },
     { "sample_fmts", OPT_EXIT, {.func_arg = show_sample_fmts }, "show available audio sample formats" },
-    { "loglevel"   , HAS_ARG,  {.func_arg = opt_loglevel},      "set libav* logging level", "loglevel" },
-    { "v",           HAS_ARG,  {.func_arg = opt_loglevel},      "set libav* logging level", "loglevel" },
+    { "loglevel"   , HAS_ARG,  {.func_arg = opt_loglevel},      "set logging level", "loglevel" },
+    { "v",           HAS_ARG,  {.func_arg = opt_loglevel},      "set logging level", "loglevel" },
     { "report"     , 0,        {(void*)opt_report}, "generate a report" },
     { "max_alloc"  , HAS_ARG,  {.func_arg = opt_max_alloc},     "set maximum size of a single allocated block", "bytes" },
     { "cpuflags"   , HAS_ARG | OPT_EXPERT, {.func_arg = opt_cpuflags}, "force specific cpu flags", "flags" },
+#if CONFIG_OPENCL
+    { "opencl_options", HAS_ARG, {.func_arg = opt_opencl},      "set OpenCL environment options" },
+#endif
diff --git a/common.mak b/common.mak
index d6cb5f6..dc4133a 100644
--- a/common.mak
+++ b/common.mak
@@ -101,6 +101,7 @@
 
 DEP_LIBS := $(foreach NAME,$(FFLIBS),lib$(NAME)/$($(CONFIG_SHARED:yes=S)LIBNAME))
 
+SRC_DIR    := $(SRC_PATH)/lib$(NAME)
 ALLHEADERS := $(subst $(SRC_DIR)/,$(SUBDIR),$(wildcard $(SRC_DIR)/*.h $(SRC_DIR)/$(ARCH)/*.h))
 SKIPHEADERS += $(ARCH_HEADERS:%=$(ARCH)/%) $(SKIPHEADERS-)
 SKIPHEADERS := $(SKIPHEADERS:%=$(SUBDIR)%)
diff --git a/compat/avisynth/avisynth_c.h b/compat/avisynth/avisynth_c.h
new file mode 100644
index 0000000..84b6e13
--- /dev/null
+++ b/compat/avisynth/avisynth_c.h
@@ -0,0 +1,879 @@
+// Avisynth C Interface Version 0.20
+// Copyright 2003 Kevin Atkinson
+
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program 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 General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA, or visit
+// http://www.gnu.org/copyleft/gpl.html .
+//
+// As a special exception, I give you permission to link to the
+// Avisynth C interface with independent modules that communicate with
+// the Avisynth C interface solely through the interfaces defined in
+// avisynth_c.h, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting combined work
+// under terms of your choice, provided that every copy of the
+// combined work is accompanied by a complete copy of the source code
+// of the Avisynth C interface and Avisynth itself (with the version
+// used to produce the combined work), being distributed under the
+// terms of the GNU General Public License plus this exception.  An
+// independent module is a module which is not derived from or based
+// on Avisynth C Interface, such as 3rd-party filters, import and
+// export plugins, or graphical user interfaces.
+
+// NOTE: this is a partial update of the Avisynth C interface to recognize
+// new color spaces added in Avisynth 2.60. By no means is this document
+// completely Avisynth 2.60 compliant.
+
+#ifndef __AVISYNTH_C__
+#define __AVISYNTH_C__
+
+#ifdef __cplusplus
+#  define EXTERN_C extern "C"
+#else
+#  define EXTERN_C
+#endif
+
+#define AVSC_USE_STDCALL 1
+
+#ifndef AVSC_USE_STDCALL
+#  define AVSC_CC __cdecl
+#else
+#  define AVSC_CC __stdcall
+#endif
+
+#define AVSC_INLINE static __inline
+
+#ifdef AVISYNTH_C_EXPORTS
+#  define AVSC_EXPORT EXTERN_C
+#  define AVSC_API(ret, name) EXTERN_C __declspec(dllexport) ret AVSC_CC name
+#else
+#  define AVSC_EXPORT EXTERN_C __declspec(dllexport)
+#  ifndef AVSC_NO_DECLSPEC
+#    define AVSC_API(ret, name) EXTERN_C __declspec(dllimport) ret AVSC_CC name
+#  else
+#    define AVSC_API(ret, name) typedef ret (AVSC_CC *name##_func)
+#  endif
+#endif
+
+typedef unsigned char BYTE;
+#ifdef __GNUC__
+typedef long long int INT64;
+#else
+typedef __int64 INT64;
+#endif
+
+
+/////////////////////////////////////////////////////////////////////
+//
+// Constants
+//
+
+#ifndef __AVISYNTH_H__
+enum { AVISYNTH_INTERFACE_VERSION = 4 };
+#endif
+
+enum {AVS_SAMPLE_INT8  = 1<<0,
+      AVS_SAMPLE_INT16 = 1<<1,
+      AVS_SAMPLE_INT24 = 1<<2,
+      AVS_SAMPLE_INT32 = 1<<3,
+      AVS_SAMPLE_FLOAT = 1<<4};
+
+enum {AVS_PLANAR_Y=1<<0,
+      AVS_PLANAR_U=1<<1,
+      AVS_PLANAR_V=1<<2,
+      AVS_PLANAR_ALIGNED=1<<3,
+      AVS_PLANAR_Y_ALIGNED=AVS_PLANAR_Y|AVS_PLANAR_ALIGNED,
+      AVS_PLANAR_U_ALIGNED=AVS_PLANAR_U|AVS_PLANAR_ALIGNED,
+      AVS_PLANAR_V_ALIGNED=AVS_PLANAR_V|AVS_PLANAR_ALIGNED,
+      AVS_PLANAR_A=1<<4,
+      AVS_PLANAR_R=1<<5,
+      AVS_PLANAR_G=1<<6,
+      AVS_PLANAR_B=1<<7,
+      AVS_PLANAR_A_ALIGNED=AVS_PLANAR_A|AVS_PLANAR_ALIGNED,
+      AVS_PLANAR_R_ALIGNED=AVS_PLANAR_R|AVS_PLANAR_ALIGNED,
+      AVS_PLANAR_G_ALIGNED=AVS_PLANAR_G|AVS_PLANAR_ALIGNED,
+      AVS_PLANAR_B_ALIGNED=AVS_PLANAR_B|AVS_PLANAR_ALIGNED};
+
+  // Colorspace properties.
+enum {AVS_CS_BGR = 1<<28,
+      AVS_CS_YUV = 1<<29,
+      AVS_CS_INTERLEAVED = 1<<30,
+      AVS_CS_PLANAR = 1<<31,
+
+      AVS_CS_SHIFT_SUB_WIDTH   = 0,
+      AVS_CS_SHIFT_SUB_HEIGHT  = 1 << 3,
+      AVS_CS_SHIFT_SAMPLE_BITS = 1 << 4,
+
+      AVS_CS_SUB_WIDTH_MASK    = 7 << AVS_CS_SHIFT_SUB_WIDTH,
+      AVS_CS_SUB_WIDTH_1       = 3 << AVS_CS_SHIFT_SUB_WIDTH, // YV24
+      AVS_CS_SUB_WIDTH_2       = 0 << AVS_CS_SHIFT_SUB_WIDTH, // YV12, I420, YV16
+      AVS_CS_SUB_WIDTH_4       = 1 << AVS_CS_SHIFT_SUB_WIDTH, // YUV9, YV411
+
+      AVS_CS_VPLANEFIRST       = 1 << 3, // YV12, YV16, YV24, YV411, YUV9
+      AVS_CS_UPLANEFIRST       = 1 << 4, // I420
+
+      AVS_CS_SUB_HEIGHT_MASK   = 7 << AVS_CS_SHIFT_SUB_HEIGHT,
+      AVS_CS_SUB_HEIGHT_1      = 3 << AVS_CS_SHIFT_SUB_HEIGHT, // YV16, YV24, YV411
+      AVS_CS_SUB_HEIGHT_2      = 0 << AVS_CS_SHIFT_SUB_HEIGHT, // YV12, I420
+      AVS_CS_SUB_HEIGHT_4      = 1 << AVS_CS_SHIFT_SUB_HEIGHT, // YUV9
+
+      AVS_CS_SAMPLE_BITS_MASK  = 7 << AVS_CS_SHIFT_SAMPLE_BITS,
+      AVS_CS_SAMPLE_BITS_8     = 0 << AVS_CS_SHIFT_SAMPLE_BITS,
+      AVS_CS_SAMPLE_BITS_16    = 1 << AVS_CS_SHIFT_SAMPLE_BITS,
+      AVS_CS_SAMPLE_BITS_32    = 2 << AVS_CS_SHIFT_SAMPLE_BITS,
+
+      AVS_CS_PLANAR_MASK       = AVS_CS_PLANAR | AVS_CS_INTERLEAVED | AVS_CS_YUV | AVS_CS_BGR | AVS_CS_SAMPLE_BITS_MASK | AVS_CS_SUB_HEIGHT_MASK | AVS_CS_SUB_WIDTH_MASK,
+      AVS_CS_PLANAR_FILTER     = ~( AVS_CS_VPLANEFIRST | AVS_CS_UPLANEFIRST )};
+
+  // Specific colorformats
+enum {
+  AVS_CS_UNKNOWN = 0,
+  AVS_CS_BGR24 = 1<<0 | AVS_CS_BGR | AVS_CS_INTERLEAVED,
+  AVS_CS_BGR32 = 1<<1 | AVS_CS_BGR | AVS_CS_INTERLEAVED,
+  AVS_CS_YUY2 = 1<<2 | AVS_CS_YUV | AVS_CS_INTERLEAVED,
+  //  AVS_CS_YV12  = 1<<3  Reserved
+  //  AVS_CS_I420  = 1<<4  Reserved
+  AVS_CS_RAW32 = 1<<5 | AVS_CS_INTERLEAVED,
+
+  AVS_CS_YV24  = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8 | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_1 | AVS_CS_SUB_WIDTH_1,  // YVU 4:4:4 planar
+  AVS_CS_YV16  = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8 | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_1 | AVS_CS_SUB_WIDTH_2,  // YVU 4:2:2 planar
+  AVS_CS_YV12  = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8 | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_2 | AVS_CS_SUB_WIDTH_2,  // YVU 4:2:0 planar
+  AVS_CS_I420  = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8 | AVS_CS_UPLANEFIRST | AVS_CS_SUB_HEIGHT_2 | AVS_CS_SUB_WIDTH_2,  // YUV 4:2:0 planar
+  AVS_CS_IYUV  = AVS_CS_I420,
+  AVS_CS_YV411 = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8 | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_1 | AVS_CS_SUB_WIDTH_4,  // YVU 4:1:1 planar
+  AVS_CS_YUV9  = AVS_CS_PLANAR | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8 | AVS_CS_VPLANEFIRST | AVS_CS_SUB_HEIGHT_4 | AVS_CS_SUB_WIDTH_4,  // YVU 4:1:0 planar
+  AVS_CS_Y8    = AVS_CS_PLANAR | AVS_CS_INTERLEAVED | AVS_CS_YUV | AVS_CS_SAMPLE_BITS_8                                              // Y   4:0:0 planar
+};
+
+enum {
+  AVS_IT_BFF = 1<<0,
+  AVS_IT_TFF = 1<<1,
+  AVS_IT_FIELDBASED = 1<<2};
+
+enum {
+  AVS_FILTER_TYPE=1,
+  AVS_FILTER_INPUT_COLORSPACE=2,
+  AVS_FILTER_OUTPUT_TYPE=9,
+  AVS_FILTER_NAME=4,
+  AVS_FILTER_AUTHOR=5,
+  AVS_FILTER_VERSION=6,
+  AVS_FILTER_ARGS=7,
+  AVS_FILTER_ARGS_INFO=8,
+  AVS_FILTER_ARGS_DESCRIPTION=10,
+  AVS_FILTER_DESCRIPTION=11};
+
+enum {  //SUBTYPES
+  AVS_FILTER_TYPE_AUDIO=1,
+  AVS_FILTER_TYPE_VIDEO=2,
+  AVS_FILTER_OUTPUT_TYPE_SAME=3,
+  AVS_FILTER_OUTPUT_TYPE_DIFFERENT=4};
+
+enum {
+  AVS_CACHE_NOTHING=0,
+  AVS_CACHE_RANGE=1,
+  AVS_CACHE_ALL=2,
+  AVS_CACHE_AUDIO=3,
+  AVS_CACHE_AUDIO_NONE=4,
+  AVS_CACHE_AUDIO_AUTO=5
+  };
+
+#define AVS_FRAME_ALIGN 16
+
+typedef struct AVS_Clip AVS_Clip;
+typedef struct AVS_ScriptEnvironment AVS_ScriptEnvironment;
+
+/////////////////////////////////////////////////////////////////////
+//
+// AVS_VideoInfo
+//
+
+// AVS_VideoInfo is layed out identicly to VideoInfo
+typedef struct AVS_VideoInfo {
+  int width, height;    // width=0 means no video
+  unsigned fps_numerator, fps_denominator;
+  int num_frames;
+
+  int pixel_type;
+
+  int audio_samples_per_second;   // 0 means no audio
+  int sample_type;
+  INT64 num_audio_samples;
+  int nchannels;
+
+  // Imagetype properties
+
+  int image_type;
+} AVS_VideoInfo;
+
+// useful functions of the above
+AVSC_INLINE int avs_has_video(const AVS_VideoInfo * p)
+        { return (p->width!=0); }
+
+AVSC_INLINE int avs_has_audio(const AVS_VideoInfo * p)
+        { return (p->audio_samples_per_second!=0); }
+
+AVSC_INLINE int avs_is_rgb(const AVS_VideoInfo * p)
+        { return !!(p->pixel_type&AVS_CS_BGR); }
+
+AVSC_INLINE int avs_is_rgb24(const AVS_VideoInfo * p)
+        { return (p->pixel_type&AVS_CS_BGR24)==AVS_CS_BGR24; } // Clear out additional properties
+
+AVSC_INLINE int avs_is_rgb32(const AVS_VideoInfo * p)
+        { return (p->pixel_type & AVS_CS_BGR32) == AVS_CS_BGR32 ; }
+
+AVSC_INLINE int avs_is_yuv(const AVS_VideoInfo * p)
+        { return !!(p->pixel_type&AVS_CS_YUV ); }
+
+AVSC_INLINE int avs_is_yuy2(const AVS_VideoInfo * p)
+        { return (p->pixel_type & AVS_CS_YUY2) == AVS_CS_YUY2; }
+
+AVSC_INLINE int avs_is_yv24(const AVS_VideoInfo * p)
+        { return (p->pixel_type & AVS_CS_PLANAR_MASK) == (AVS_CS_YV24  & AVS_CS_PLANAR_FILTER); }
+
+AVSC_INLINE int avs_is_yv16(const AVS_VideoInfo * p)
+        { return (p->pixel_type & AVS_CS_PLANAR_MASK) == (AVS_CS_YV16  & AVS_CS_PLANAR_FILTER); }
+
+AVSC_INLINE int avs_is_yv12(const AVS_VideoInfo * p)
+        { return (p->pixel_type & AVS_CS_PLANAR_MASK) == (AVS_CS_YV12  & AVS_CS_PLANAR_FILTER); }
+
+AVSC_INLINE int avs_is_yv411(const AVS_VideoInfo * p)
+        { return (p->pixel_type & AVS_CS_PLANAR_MASK) == (AVS_CS_YV411 & AVS_CS_PLANAR_FILTER); }
+
+AVSC_INLINE int avs_is_y8(const AVS_VideoInfo * p)
+        { return (p->pixel_type & AVS_CS_PLANAR_MASK) == (AVS_CS_Y8    & AVS_CS_PLANAR_FILTER); }
+
+AVSC_INLINE int avs_is_property(const AVS_VideoInfo * p, int property)
+        { return ((p->pixel_type & property)==property ); }
+
+AVSC_INLINE int avs_is_planar(const AVS_VideoInfo * p)
+        { return !!(p->pixel_type & AVS_CS_PLANAR); }
+
+AVSC_INLINE int avs_is_color_space(const AVS_VideoInfo * p, int c_space)
+        { return avs_is_planar(p) ? ((p->pixel_type & AVS_CS_PLANAR_MASK) == (c_space & AVS_CS_PLANAR_FILTER)) : ((p->pixel_type & c_space) == c_space); }
+
+AVSC_INLINE int avs_is_field_based(const AVS_VideoInfo * p)
+        { return !!(p->image_type & AVS_IT_FIELDBASED); }
+
+AVSC_INLINE int avs_is_parity_known(const AVS_VideoInfo * p)
+        { return ((p->image_type & AVS_IT_FIELDBASED)&&(p->image_type & (AVS_IT_BFF | AVS_IT_TFF))); }
+
+AVSC_INLINE int avs_is_bff(const AVS_VideoInfo * p)
+        { return !!(p->image_type & AVS_IT_BFF); }
+
+AVSC_INLINE int avs_is_tff(const AVS_VideoInfo * p)
+        { return !!(p->image_type & AVS_IT_TFF); }
+
+AVSC_INLINE int avs_bits_per_pixel(const AVS_VideoInfo * p)
+{
+  switch (p->pixel_type) {
+      case AVS_CS_BGR24: return 24;
+      case AVS_CS_BGR32: return 32;
+      case AVS_CS_YUY2:  return 16;
+      case AVS_CS_YV12:
+      case AVS_CS_I420:  return 12;
+      default:           return 0;
+    }
+}
+AVSC_INLINE int avs_bytes_from_pixels(const AVS_VideoInfo * p, int pixels)
+        { return pixels * (avs_bits_per_pixel(p)>>3); }   // Will work on planar images, but will return only luma planes
+
+AVSC_INLINE int avs_row_size(const AVS_VideoInfo * p)
+        { return avs_bytes_from_pixels(p,p->width); }  // Also only returns first plane on planar images
+
+AVSC_INLINE int avs_bmp_size(const AVS_VideoInfo * vi)
+        { if (avs_is_planar(vi)) {int p = vi->height * ((avs_row_size(vi)+3) & ~3); p+=p>>1; return p;  } return vi->height * ((avs_row_size(vi)+3) & ~3); }
+
+AVSC_INLINE int avs_samples_per_second(const AVS_VideoInfo * p)
+        { return p->audio_samples_per_second; }
+
+
+AVSC_INLINE int avs_bytes_per_channel_sample(const AVS_VideoInfo * p)
+{
+    switch (p->sample_type) {
+      case AVS_SAMPLE_INT8:  return sizeof(signed char);
+      case AVS_SAMPLE_INT16: return sizeof(signed short);
+      case AVS_SAMPLE_INT24: return 3;
+      case AVS_SAMPLE_INT32: return sizeof(signed int);
+      case AVS_SAMPLE_FLOAT: return sizeof(float);
+      default: return 0;
+    }
+}
+AVSC_INLINE int avs_bytes_per_audio_sample(const AVS_VideoInfo * p)
+        { return p->nchannels*avs_bytes_per_channel_sample(p);}
+
+AVSC_INLINE INT64 avs_audio_samples_from_frames(const AVS_VideoInfo * p, INT64 frames)
+        { return ((INT64)(frames) * p->audio_samples_per_second * p->fps_denominator / p->fps_numerator); }
+
+AVSC_INLINE int avs_frames_from_audio_samples(const AVS_VideoInfo * p, INT64 samples)
+        { return (int)(samples * (INT64)p->fps_numerator / (INT64)p->fps_denominator / (INT64)p->audio_samples_per_second); }
+
+AVSC_INLINE INT64 avs_audio_samples_from_bytes(const AVS_VideoInfo * p, INT64 bytes)
+        { return bytes / avs_bytes_per_audio_sample(p); }
+
+AVSC_INLINE INT64 avs_bytes_from_audio_samples(const AVS_VideoInfo * p, INT64 samples)
+        { return samples * avs_bytes_per_audio_sample(p); }
+
+AVSC_INLINE int avs_audio_channels(const AVS_VideoInfo * p)
+        { return p->nchannels; }
+
+AVSC_INLINE int avs_sample_type(const AVS_VideoInfo * p)
+        { return p->sample_type;}
+
+// useful mutator
+AVSC_INLINE void avs_set_property(AVS_VideoInfo * p, int property)
+        { p->image_type|=property; }
+
+AVSC_INLINE void avs_clear_property(AVS_VideoInfo * p, int property)
+        { p->image_type&=~property; }
+
+AVSC_INLINE void avs_set_field_based(AVS_VideoInfo * p, int isfieldbased)
+        { if (isfieldbased) p->image_type|=AVS_IT_FIELDBASED; else p->image_type&=~AVS_IT_FIELDBASED; }
+
+AVSC_INLINE void avs_set_fps(AVS_VideoInfo * p, unsigned numerator, unsigned denominator)
+{
+    unsigned x=numerator, y=denominator;
+    while (y) {   // find gcd
+      unsigned t = x%y; x = y; y = t;
+    }
+    p->fps_numerator = numerator/x;
+    p->fps_denominator = denominator/x;
+}
+
+AVSC_INLINE int avs_is_same_colorspace(AVS_VideoInfo * x, AVS_VideoInfo * y)
+{
+        return (x->pixel_type == y->pixel_type)
+                || (avs_is_yv12(x) && avs_is_yv12(y));
+}
+
+/////////////////////////////////////////////////////////////////////
+//
+// AVS_VideoFrame
+//
+
+// VideoFrameBuffer holds information about a memory block which is used
+// for video data.  For efficiency, instances of this class are not deleted
+// when the refcount reaches zero; instead they're stored in a linked list
+// to be reused.  The instances are deleted when the corresponding AVS
+// file is closed.
+
+// AVS_VideoFrameBuffer is layed out identicly to VideoFrameBuffer
+// DO NOT USE THIS STRUCTURE DIRECTLY
+typedef struct AVS_VideoFrameBuffer {
+  BYTE * data;
+  int data_size;
+  // sequence_number is incremented every time the buffer is changed, so
+  // that stale views can tell they're no longer valid.
+  volatile long sequence_number;
+
+  volatile long refcount;
+} AVS_VideoFrameBuffer;
+
+// VideoFrame holds a "window" into a VideoFrameBuffer.
+
+// AVS_VideoFrame is layed out identicly to IVideoFrame
+// DO NOT USE THIS STRUCTURE DIRECTLY
+typedef struct AVS_VideoFrame {
+  volatile long refcount;
+  AVS_VideoFrameBuffer * vfb;
+  int offset, pitch, row_size, height, offsetU, offsetV, pitchUV;  // U&V offsets are from top of picture.
+  int row_sizeUV, heightUV;
+} AVS_VideoFrame;
+
+// Access functions for AVS_VideoFrame
+AVSC_INLINE int avs_get_pitch(const AVS_VideoFrame * p) {
+        return p->pitch;}
+
+AVSC_INLINE int avs_get_pitch_p(const AVS_VideoFrame * p, int plane) {
+  switch (plane) {
+  case AVS_PLANAR_U: case AVS_PLANAR_V: return p->pitchUV;}
+  return p->pitch;}
+
+AVSC_INLINE int avs_get_row_size(const AVS_VideoFrame * p) {
+        return p->row_size; }
+
+AVSC_INLINE int avs_get_row_size_p(const AVS_VideoFrame * p, int plane) {
+        int r;
+    switch (plane) {
+    case AVS_PLANAR_U: case AVS_PLANAR_V:
+                if (p->pitchUV) return p->row_sizeUV;
+                else            return 0;
+    case AVS_PLANAR_U_ALIGNED: case AVS_PLANAR_V_ALIGNED:
+                if (p->pitchUV) {
+                        r = (p->row_sizeUV+AVS_FRAME_ALIGN-1)&(~(AVS_FRAME_ALIGN-1)); // Aligned rowsize
+                        if (r < p->pitchUV)
+                                return r;
+                        return p->row_sizeUV;
+                } else return 0;
+    case AVS_PLANAR_Y_ALIGNED:
+                r = (p->row_size+AVS_FRAME_ALIGN-1)&(~(AVS_FRAME_ALIGN-1)); // Aligned rowsize
+                if (r <= p->pitch)
+                        return r;
+                return p->row_size;
+    }
+    return p->row_size;
+}
+
+AVSC_INLINE int avs_get_height(const AVS_VideoFrame * p) {
+        return p->height;}
+
+AVSC_INLINE int avs_get_height_p(const AVS_VideoFrame * p, int plane) {
+        switch (plane) {
+                case AVS_PLANAR_U: case AVS_PLANAR_V:
+                        if (p->pitchUV) return p->heightUV;
+                        return 0;
+        }
+        return p->height;}
+
+AVSC_INLINE const BYTE* avs_get_read_ptr(const AVS_VideoFrame * p) {
+        return p->vfb->data + p->offset;}
+
+AVSC_INLINE const BYTE* avs_get_read_ptr_p(const AVS_VideoFrame * p, int plane)
+{
+        switch (plane) {
+                case AVS_PLANAR_U: return p->vfb->data + p->offsetU;
+                case AVS_PLANAR_V: return p->vfb->data + p->offsetV;
+                default:           return p->vfb->data + p->offset;}
+}
+
+AVSC_INLINE int avs_is_writable(const AVS_VideoFrame * p) {
+        return (p->refcount == 1 && p->vfb->refcount == 1);}
+
+AVSC_INLINE BYTE* avs_get_write_ptr(const AVS_VideoFrame * p)
+{
+        if (avs_is_writable(p)) {
+                ++p->vfb->sequence_number;
+                return p->vfb->data + p->offset;
+        } else
+                return 0;
+}
+
+AVSC_INLINE BYTE* avs_get_write_ptr_p(const AVS_VideoFrame * p, int plane)
+{
+        if (plane==AVS_PLANAR_Y && avs_is_writable(p)) {
+                ++p->vfb->sequence_number;
+                return p->vfb->data + p->offset;
+        } else if (plane==AVS_PLANAR_Y) {
+                return 0;
+        } else {
+                switch (plane) {
+                        case AVS_PLANAR_U: return p->vfb->data + p->offsetU;
+                        case AVS_PLANAR_V: return p->vfb->data + p->offsetV;
+                        default:       return p->vfb->data + p->offset;
+                }
+        }
+}
+
+
+AVSC_API(void, avs_release_video_frame)(AVS_VideoFrame *);
+// makes a shallow copy of a video frame
+AVSC_API(AVS_VideoFrame *, avs_copy_video_frame)(AVS_VideoFrame *);
+
+#ifndef AVSC_NO_DECLSPEC
+AVSC_INLINE void avs_release_frame(AVS_VideoFrame * f)
+  {avs_release_video_frame(f);}
+AVSC_INLINE AVS_VideoFrame * avs_copy_frame(AVS_VideoFrame * f)
+  {return avs_copy_video_frame(f);}
+#endif
+
+/////////////////////////////////////////////////////////////////////
+//
+// AVS_Value
+//
+
+// Treat AVS_Value as a fat pointer.  That is use avs_copy_value
+// and avs_release_value appropiaty as you would if AVS_Value was
+// a pointer.
+
+// To maintain source code compatibility with future versions of the
+// avisynth_c API don't use the AVS_Value directly.  Use the helper
+// functions below.
+
+// AVS_Value is layed out identicly to AVSValue
+typedef struct AVS_Value AVS_Value;
+struct AVS_Value {
+  short type;  // 'a'rray, 'c'lip, 'b'ool, 'i'nt, 'f'loat, 's'tring, 'v'oid, or 'l'ong
+               // for some function e'rror
+  short array_size;
+  union {
+    void * clip; // do not use directly, use avs_take_clip
+    char boolean;
+    int integer;
+    float floating_pt;
+    const char * string;
+    const AVS_Value * array;
+  } d;
+};
+
+// AVS_Value should be initilized with avs_void.
+// Should also set to avs_void after the value is released
+// with avs_copy_value.  Consider it the equalvent of setting
+// a pointer to NULL
+static const AVS_Value avs_void = {'v'};
+
+AVSC_API(void, avs_copy_value)(AVS_Value * dest, AVS_Value src);
+AVSC_API(void, avs_release_value)(AVS_Value);
+
+AVSC_INLINE int avs_defined(AVS_Value v) { return v.type != 'v'; }
+AVSC_INLINE int avs_is_clip(AVS_Value v) { return v.type == 'c'; }
+AVSC_INLINE int avs_is_bool(AVS_Value v) { return v.type == 'b'; }
+AVSC_INLINE int avs_is_int(AVS_Value v) { return v.type == 'i'; }
+AVSC_INLINE int avs_is_float(AVS_Value v) { return v.type == 'f' || v.type == 'i'; }
+AVSC_INLINE int avs_is_string(AVS_Value v) { return v.type == 's'; }
+AVSC_INLINE int avs_is_array(AVS_Value v) { return v.type == 'a'; }
+AVSC_INLINE int avs_is_error(AVS_Value v) { return v.type == 'e'; }
+
+AVSC_API(AVS_Clip *, avs_take_clip)(AVS_Value, AVS_ScriptEnvironment *);
+AVSC_API(void, avs_set_to_clip)(AVS_Value *, AVS_Clip *);
+
+AVSC_INLINE int avs_as_bool(AVS_Value v)
+        { return v.d.boolean; }
+AVSC_INLINE int avs_as_int(AVS_Value v)
+        { return v.d.integer; }
+AVSC_INLINE const char * avs_as_string(AVS_Value v)
+        { return avs_is_error(v) || avs_is_string(v) ? v.d.string : 0; }
+AVSC_INLINE double avs_as_float(AVS_Value v)
+        { return avs_is_int(v) ? v.d.integer : v.d.floating_pt; }
+AVSC_INLINE const char * avs_as_error(AVS_Value v)
+        { return avs_is_error(v) ? v.d.string : 0; }
+AVSC_INLINE const AVS_Value * avs_as_array(AVS_Value v)
+        { return v.d.array; }
+AVSC_INLINE int avs_array_size(AVS_Value v)
+        { return avs_is_array(v) ? v.array_size : 1; }
+AVSC_INLINE AVS_Value avs_array_elt(AVS_Value v, int index)
+        { return avs_is_array(v) ? v.d.array[index] : v; }
+
+// only use these functions on an AVS_Value that does not already have
+// an active value.  Remember, treat AVS_Value as a fat pointer.
+AVSC_INLINE AVS_Value avs_new_value_bool(int v0)
+        { AVS_Value v; v.type = 'b'; v.d.boolean = v0 == 0 ? 0 : 1; return v; }
+AVSC_INLINE AVS_Value avs_new_value_int(int v0)
+        { AVS_Value v; v.type = 'i'; v.d.integer = v0; return v; }
+AVSC_INLINE AVS_Value avs_new_value_string(const char * v0)
+        { AVS_Value v; v.type = 's'; v.d.string = v0; return v; }
+AVSC_INLINE AVS_Value avs_new_value_float(float v0)
+        { AVS_Value v; v.type = 'f'; v.d.floating_pt = v0; return v;}
+AVSC_INLINE AVS_Value avs_new_value_error(const char * v0)
+        { AVS_Value v; v.type = 'e'; v.d.string = v0; return v; }
+#ifndef AVSC_NO_DECLSPEC
+AVSC_INLINE AVS_Value avs_new_value_clip(AVS_Clip * v0)
+        { AVS_Value v; avs_set_to_clip(&v, v0); return v; }
+#endif
+AVSC_INLINE AVS_Value avs_new_value_array(AVS_Value * v0, int size)
+        { AVS_Value v; v.type = 'a'; v.d.array = v0; v.array_size = size; return v; }
+
+/////////////////////////////////////////////////////////////////////
+//
+// AVS_Clip
+//
+
+AVSC_API(void, avs_release_clip)(AVS_Clip *);
+AVSC_API(AVS_Clip *, avs_copy_clip)(AVS_Clip *);
+
+AVSC_API(const char *, avs_clip_get_error)(AVS_Clip *); // return 0 if no error
+
+AVSC_API(const AVS_VideoInfo *, avs_get_video_info)(AVS_Clip *);
+
+AVSC_API(int, avs_get_version)(AVS_Clip *);
+
+AVSC_API(AVS_VideoFrame *, avs_get_frame)(AVS_Clip *, int n);
+// The returned video frame must be released with avs_release_video_frame
+
+AVSC_API(int, avs_get_parity)(AVS_Clip *, int n);
+// return field parity if field_based, else parity of first field in frame
+
+AVSC_API(int, avs_get_audio)(AVS_Clip *, void * buf,
+                             INT64 start, INT64 count);
+// start and count are in samples
+
+AVSC_API(int, avs_set_cache_hints)(AVS_Clip *,
+                                   int cachehints, int frame_range);
+
+// This is the callback type used by avs_add_function
+typedef AVS_Value (AVSC_CC * AVS_ApplyFunc)
+                        (AVS_ScriptEnvironment *, AVS_Value args, void * user_data);
+
+typedef struct AVS_FilterInfo AVS_FilterInfo;
+struct AVS_FilterInfo
+{
+  // these members should not be modified outside of the AVS_ApplyFunc callback
+  AVS_Clip * child;
+  AVS_VideoInfo vi;
+  AVS_ScriptEnvironment * env;
+  AVS_VideoFrame * (AVSC_CC * get_frame)(AVS_FilterInfo *, int n);
+  int (AVSC_CC * get_parity)(AVS_FilterInfo *, int n);
+  int (AVSC_CC * get_audio)(AVS_FilterInfo *, void * buf,
+                                  INT64 start, INT64 count);
+  int (AVSC_CC * set_cache_hints)(AVS_FilterInfo *, int cachehints,
+                                        int frame_range);
+  void (AVSC_CC * free_filter)(AVS_FilterInfo *);
+
+  // Should be set when ever there is an error to report.
+  // It is cleared before any of the above methods are called
+  const char * error;
+  // this is to store whatever and may be modified at will
+  void * user_data;
+};
+
+// Create a new filter
+// fi is set to point to the AVS_FilterInfo so that you can
+//   modify it once it is initilized.
+// store_child should generally be set to true.  If it is not
+//    set than ALL methods (the function pointers) must be defined
+// If it is set than you do not need to worry about freeing the child
+//    clip.
+AVSC_API(AVS_Clip *, avs_new_c_filter)(AVS_ScriptEnvironment * e,
+                                       AVS_FilterInfo * * fi,
+                                       AVS_Value child, int store_child);
+
+/////////////////////////////////////////////////////////////////////
+//
+// AVS_ScriptEnvironment
+//
+
+// For GetCPUFlags.  These are backwards-compatible with those in VirtualDub.
+enum {
+                                /* slowest CPU to support extension */
+  AVS_CPU_FORCE        = 0x01,   // N/A
+  AVS_CPU_FPU          = 0x02,   // 386/486DX
+  AVS_CPU_MMX          = 0x04,   // P55C, K6, PII
+  AVS_CPU_INTEGER_SSE  = 0x08,   // PIII, Athlon
+  AVS_CPU_SSE          = 0x10,   // PIII, Athlon XP/MP
+  AVS_CPU_SSE2         = 0x20,   // PIV, Hammer
+  AVS_CPU_3DNOW        = 0x40,   // K6-2
+  AVS_CPU_3DNOW_EXT    = 0x80,   // Athlon
+  AVS_CPU_X86_64       = 0xA0,   // Hammer (note: equiv. to 3DNow + SSE2,
+                                 // which only Hammer will have anyway)
+  AVS_CPUF_SSE3       = 0x100,   //  PIV+, K8 Venice
+  AVS_CPUF_SSSE3      = 0x200,   //  Core 2
+  AVS_CPUF_SSE4       = 0x400,   //  Penryn, Wolfdale, Yorkfield
+  AVS_CPUF_SSE4_1     = 0x400,
+  AVS_CPUF_SSE4_2     = 0x800,   //  Nehalem
+};
+
+AVSC_API(const char *, avs_get_error)(AVS_ScriptEnvironment *); // return 0 if no error
+
+AVSC_API(long, avs_get_cpu_flags)(AVS_ScriptEnvironment *);
+AVSC_API(int, avs_check_version)(AVS_ScriptEnvironment *, int version);
+
+AVSC_API(char *, avs_save_string)(AVS_ScriptEnvironment *, const char* s, int length);
+AVSC_API(char *, avs_sprintf)(AVS_ScriptEnvironment *, const char * fmt, ...);
+
+AVSC_API(char *, avs_vsprintf)(AVS_ScriptEnvironment *, const char * fmt, void* val);
+ // note: val is really a va_list; I hope everyone typedefs va_list to a pointer
+
+AVSC_API(int, avs_add_function)(AVS_ScriptEnvironment *,
+                                const char * name, const char * params,
+                                AVS_ApplyFunc apply, void * user_data);
+
+AVSC_API(int, avs_function_exists)(AVS_ScriptEnvironment *, const char * name);
+
+AVSC_API(AVS_Value, avs_invoke)(AVS_ScriptEnvironment *, const char * name,
+                               AVS_Value args, const char** arg_names);
+// The returned value must be be released with avs_release_value
+
+AVSC_API(AVS_Value, avs_get_var)(AVS_ScriptEnvironment *, const char* name);
+// The returned value must be be released with avs_release_value
+
+AVSC_API(int, avs_set_var)(AVS_ScriptEnvironment *, const char* name, AVS_Value val);
+
+AVSC_API(int, avs_set_global_var)(AVS_ScriptEnvironment *, const char* name, const AVS_Value val);
+
+//void avs_push_context(AVS_ScriptEnvironment *, int level=0);
+//void avs_pop_context(AVS_ScriptEnvironment *);
+
+AVSC_API(AVS_VideoFrame *, avs_new_video_frame_a)(AVS_ScriptEnvironment *,
+                                          const AVS_VideoInfo * vi, int align);
+// align should be at least 16
+
+#ifndef AVSC_NO_DECLSPEC
+AVSC_INLINE
+AVS_VideoFrame * avs_new_video_frame(AVS_ScriptEnvironment * env,
+                                     const AVS_VideoInfo * vi)
+  {return avs_new_video_frame_a(env,vi,AVS_FRAME_ALIGN);}
+
+AVSC_INLINE
+AVS_VideoFrame * avs_new_frame(AVS_ScriptEnvironment * env,
+                               const AVS_VideoInfo * vi)
+  {return avs_new_video_frame_a(env,vi,AVS_FRAME_ALIGN);}
+#endif
+
+
+AVSC_API(int, avs_make_writable)(AVS_ScriptEnvironment *, AVS_VideoFrame * * pvf);
+
+AVSC_API(void, avs_bit_blt)(AVS_ScriptEnvironment *, BYTE* dstp, int dst_pitch, const BYTE* srcp, int src_pitch, int row_size, int height);
+
+typedef void (AVSC_CC *AVS_ShutdownFunc)(void* user_data, AVS_ScriptEnvironment * env);
+AVSC_API(void, avs_at_exit)(AVS_ScriptEnvironment *, AVS_ShutdownFunc function, void * user_data);
+
+AVSC_API(AVS_VideoFrame *, avs_subframe)(AVS_ScriptEnvironment *, AVS_VideoFrame * src, int rel_offset, int new_pitch, int new_row_size, int new_height);
+// The returned video frame must be be released
+
+AVSC_API(int, avs_set_memory_max)(AVS_ScriptEnvironment *, int mem);
+
+AVSC_API(int, avs_set_working_dir)(AVS_ScriptEnvironment *, const char * newdir);
+
+// avisynth.dll exports this; it's a way to use it as a library, without
+// writing an AVS script or without going through AVIFile.
+AVSC_API(AVS_ScriptEnvironment *, avs_create_script_environment)(int version);
+
+// this symbol is the entry point for the plugin and must
+// be defined
+AVSC_EXPORT
+const char * AVSC_CC avisynth_c_plugin_init(AVS_ScriptEnvironment* env);
+
+
+AVSC_API(void, avs_delete_script_environment)(AVS_ScriptEnvironment *);
+
+
+AVSC_API(AVS_VideoFrame *, avs_subframe_planar)(AVS_ScriptEnvironment *, AVS_VideoFrame * src, int rel_offset, int new_pitch, int new_row_size, int new_height, int rel_offsetU, int rel_offsetV, int new_pitchUV);
+// The returned video frame must be be released
+
+#ifdef AVSC_NO_DECLSPEC
+// use LoadLibrary and related functions to dynamically load Avisynth instead of declspec(dllimport)
+/*
+  The following functions needs to have been declared, probably from windows.h
+
+  void* malloc(size_t)
+  void free(void*);
+
+  HMODULE LoadLibrary(const char*);
+  void* GetProcAddress(HMODULE, const char*);
+  FreeLibrary(HMODULE);
+*/
+
+
+typedef struct AVS_Library AVS_Library;
+
+#define AVSC_DECLARE_FUNC(name) name##_func name
+
+struct AVS_Library {
+  HMODULE handle;
+
+  AVSC_DECLARE_FUNC(avs_add_function);
+  AVSC_DECLARE_FUNC(avs_at_exit);
+  AVSC_DECLARE_FUNC(avs_bit_blt);
+  AVSC_DECLARE_FUNC(avs_check_version);
+  AVSC_DECLARE_FUNC(avs_clip_get_error);
+  AVSC_DECLARE_FUNC(avs_copy_clip);
+  AVSC_DECLARE_FUNC(avs_copy_value);
+  AVSC_DECLARE_FUNC(avs_copy_video_frame);
+  AVSC_DECLARE_FUNC(avs_create_script_environment);
+  AVSC_DECLARE_FUNC(avs_delete_script_environment);
+  AVSC_DECLARE_FUNC(avs_function_exists);
+  AVSC_DECLARE_FUNC(avs_get_audio);
+  AVSC_DECLARE_FUNC(avs_get_cpu_flags);
+  AVSC_DECLARE_FUNC(avs_get_error);
+  AVSC_DECLARE_FUNC(avs_get_frame);
+  AVSC_DECLARE_FUNC(avs_get_parity);
+  AVSC_DECLARE_FUNC(avs_get_var);
+  AVSC_DECLARE_FUNC(avs_get_version);
+  AVSC_DECLARE_FUNC(avs_get_video_info);
+  AVSC_DECLARE_FUNC(avs_invoke);
+  AVSC_DECLARE_FUNC(avs_make_writable);
+  AVSC_DECLARE_FUNC(avs_new_c_filter);
+  AVSC_DECLARE_FUNC(avs_new_video_frame_a);
+  AVSC_DECLARE_FUNC(avs_release_clip);
+  AVSC_DECLARE_FUNC(avs_release_value);
+  AVSC_DECLARE_FUNC(avs_release_video_frame);
+  AVSC_DECLARE_FUNC(avs_save_string);
+  AVSC_DECLARE_FUNC(avs_set_cache_hints);
+  AVSC_DECLARE_FUNC(avs_set_global_var);
+  AVSC_DECLARE_FUNC(avs_set_memory_max);
+  AVSC_DECLARE_FUNC(avs_set_to_clip);
+  AVSC_DECLARE_FUNC(avs_set_var);
+  AVSC_DECLARE_FUNC(avs_set_working_dir);
+  AVSC_DECLARE_FUNC(avs_sprintf);
+  AVSC_DECLARE_FUNC(avs_subframe);
+  AVSC_DECLARE_FUNC(avs_subframe_planar);
+  AVSC_DECLARE_FUNC(avs_take_clip);
+  AVSC_DECLARE_FUNC(avs_vsprintf);
+};
+
+#undef AVSC_DECLARE_FUNC
+
+
+AVSC_INLINE AVS_Library * avs_load_library() {
+  AVS_Library *library = (AVS_Library *)malloc(sizeof(AVS_Library));
+  if (library == NULL)
+    return NULL;
+  library->handle = LoadLibrary("avisynth");
+  if (library->handle == NULL)
+    goto fail;
+
+#define __AVSC_STRINGIFY(x) #x
+#define AVSC_STRINGIFY(x) __AVSC_STRINGIFY(x)
+#define AVSC_LOAD_FUNC(name) {\
+  library->name = (name##_func) GetProcAddress(library->handle, AVSC_STRINGIFY(name));\
+  if (library->name == NULL)\
+    goto fail;\
+}
+
+  AVSC_LOAD_FUNC(avs_add_function);
+  AVSC_LOAD_FUNC(avs_at_exit);
+  AVSC_LOAD_FUNC(avs_bit_blt);
+  AVSC_LOAD_FUNC(avs_check_version);
+  AVSC_LOAD_FUNC(avs_clip_get_error);
+  AVSC_LOAD_FUNC(avs_copy_clip);
+  AVSC_LOAD_FUNC(avs_copy_value);
+  AVSC_LOAD_FUNC(avs_copy_video_frame);
+  AVSC_LOAD_FUNC(avs_create_script_environment);
+  AVSC_LOAD_FUNC(avs_delete_script_environment);
+  AVSC_LOAD_FUNC(avs_function_exists);
+  AVSC_LOAD_FUNC(avs_get_audio);
+  AVSC_LOAD_FUNC(avs_get_cpu_flags);
+  AVSC_LOAD_FUNC(avs_get_error);
+  AVSC_LOAD_FUNC(avs_get_frame);
+  AVSC_LOAD_FUNC(avs_get_parity);
+  AVSC_LOAD_FUNC(avs_get_var);
+  AVSC_LOAD_FUNC(avs_get_version);
+  AVSC_LOAD_FUNC(avs_get_video_info);
+  AVSC_LOAD_FUNC(avs_invoke);
+  AVSC_LOAD_FUNC(avs_make_writable);
+  AVSC_LOAD_FUNC(avs_new_c_filter);
+  AVSC_LOAD_FUNC(avs_new_video_frame_a);
+  AVSC_LOAD_FUNC(avs_release_clip);
+  AVSC_LOAD_FUNC(avs_release_value);
+  AVSC_LOAD_FUNC(avs_release_video_frame);
+  AVSC_LOAD_FUNC(avs_save_string);
+  AVSC_LOAD_FUNC(avs_set_cache_hints);
+  AVSC_LOAD_FUNC(avs_set_global_var);
+  AVSC_LOAD_FUNC(avs_set_memory_max);
+  AVSC_LOAD_FUNC(avs_set_to_clip);
+  AVSC_LOAD_FUNC(avs_set_var);
+  AVSC_LOAD_FUNC(avs_set_working_dir);
+  AVSC_LOAD_FUNC(avs_sprintf);
+  AVSC_LOAD_FUNC(avs_subframe);
+  AVSC_LOAD_FUNC(avs_subframe_planar);
+  AVSC_LOAD_FUNC(avs_take_clip);
+  AVSC_LOAD_FUNC(avs_vsprintf);
+
+#undef __AVSC_STRINGIFY
+#undef AVSC_STRINGIFY
+#undef AVSC_LOAD_FUNC
+
+  return library;
+
+fail:
+  free(library);
+  return NULL;
+}
+
+AVSC_INLINE void avs_free_library(AVS_Library *library) {
+  if (library == NULL)
+    return;
+  FreeLibrary(library->handle);
+  free(library);
+}
+#endif
+
+#endif
diff --git a/compat/avisynth/avxsynth_c.h b/compat/avisynth/avxsynth_c.h
new file mode 100644
index 0000000..7a81e21
--- /dev/null
+++ b/compat/avisynth/avxsynth_c.h
@@ -0,0 +1,727 @@
+// Avisynth C Interface Version 0.20
+// Copyright 2003 Kevin Atkinson
+
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program 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 General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA, or visit
+// http://www.gnu.org/copyleft/gpl.html .
+//
+// As a special exception, I give you permission to link to the
+// Avisynth C interface with independent modules that communicate with
+// the Avisynth C interface solely through the interfaces defined in
+// avisynth_c.h, regardless of the license terms of these independent
+// modules, and to copy and distribute the resulting combined work
+// under terms of your choice, provided that every copy of the
+// combined work is accompanied by a complete copy of the source code
+// of the Avisynth C interface and Avisynth itself (with the version
+// used to produce the combined work), being distributed under the
+// terms of the GNU General Public License plus this exception.  An
+// independent module is a module which is not derived from or based
+// on Avisynth C Interface, such as 3rd-party filters, import and
+// export plugins, or graphical user interfaces.
+
+#ifndef __AVXSYNTH_C__
+#define __AVXSYNTH_C__
+
+#include "windowsPorts/windows2linux.h"
+#include <stdarg.h>
+
+#ifdef __cplusplus
+#  define EXTERN_C extern "C"
+#else
+#  define EXTERN_C
+#endif
+
+#define AVSC_USE_STDCALL 1
+
+#ifndef AVSC_USE_STDCALL
+#  define AVSC_CC __cdecl
+#else
+#  define AVSC_CC __stdcall
+#endif
+
+#define AVSC_INLINE static __inline
+
+#ifdef AVISYNTH_C_EXPORTS
+#  define AVSC_EXPORT EXTERN_C
+#  define AVSC_API(ret, name) EXTERN_C __declspec(dllexport) ret AVSC_CC name
+#else
+#  define AVSC_EXPORT EXTERN_C __declspec(dllexport)
+#  ifndef AVSC_NO_DECLSPEC
+#    define AVSC_API(ret, name) EXTERN_C __declspec(dllimport) ret AVSC_CC name
+#  else
+#    define AVSC_API(ret, name) typedef ret (AVSC_CC *name##_func)
+#  endif
+#endif
+
+#ifdef __GNUC__
+typedef long long int INT64;
+#else
+typedef __int64 INT64;
+#endif
+
+
+/////////////////////////////////////////////////////////////////////
+//
+// Constants
+//
+
+#ifndef __AVXSYNTH_H__
+enum { AVISYNTH_INTERFACE_VERSION = 3 };
+#endif
+
+enum {AVS_SAMPLE_INT8  = 1<<0,
+      AVS_SAMPLE_INT16 = 1<<1,
+      AVS_SAMPLE_INT24 = 1<<2,
+      AVS_SAMPLE_INT32 = 1<<3,
+      AVS_SAMPLE_FLOAT = 1<<4};
+
+enum {AVS_PLANAR_Y=1<<0,
+      AVS_PLANAR_U=1<<1,
+      AVS_PLANAR_V=1<<2,
+      AVS_PLANAR_ALIGNED=1<<3,
+      AVS_PLANAR_Y_ALIGNED=AVS_PLANAR_Y|AVS_PLANAR_ALIGNED,
+      AVS_PLANAR_U_ALIGNED=AVS_PLANAR_U|AVS_PLANAR_ALIGNED,
+      AVS_PLANAR_V_ALIGNED=AVS_PLANAR_V|AVS_PLANAR_ALIGNED};
+
+  // Colorspace properties.
+enum {AVS_CS_BGR = 1<<28,
+      AVS_CS_YUV = 1<<29,
+      AVS_CS_INTERLEAVED = 1<<30,
+      AVS_CS_PLANAR = 1<<31};
+
+  // Specific colorformats
+enum {
+  AVS_CS_UNKNOWN = 0,
+  AVS_CS_BGR24 = 1<<0 | AVS_CS_BGR | AVS_CS_INTERLEAVED,
+  AVS_CS_BGR32 = 1<<1 | AVS_CS_BGR | AVS_CS_INTERLEAVED,
+  AVS_CS_YUY2 = 1<<2 | AVS_CS_YUV | AVS_CS_INTERLEAVED,
+  AVS_CS_YV12 = 1<<3 | AVS_CS_YUV | AVS_CS_PLANAR,  // y-v-u, planar
+  AVS_CS_I420 = 1<<4 | AVS_CS_YUV | AVS_CS_PLANAR,  // y-u-v, planar
+  AVS_CS_IYUV = 1<<4 | AVS_CS_YUV | AVS_CS_PLANAR  // same as above
+};
+
+enum {
+  AVS_IT_BFF = 1<<0,
+  AVS_IT_TFF = 1<<1,
+  AVS_IT_FIELDBASED = 1<<2};
+
+enum {
+  AVS_FILTER_TYPE=1,
+  AVS_FILTER_INPUT_COLORSPACE=2,
+  AVS_FILTER_OUTPUT_TYPE=9,
+  AVS_FILTER_NAME=4,
+  AVS_FILTER_AUTHOR=5,
+  AVS_FILTER_VERSION=6,
+  AVS_FILTER_ARGS=7,
+  AVS_FILTER_ARGS_INFO=8,
+  AVS_FILTER_ARGS_DESCRIPTION=10,
+  AVS_FILTER_DESCRIPTION=11};
+
+enum {  //SUBTYPES
+  AVS_FILTER_TYPE_AUDIO=1,
+  AVS_FILTER_TYPE_VIDEO=2,
+  AVS_FILTER_OUTPUT_TYPE_SAME=3,
+  AVS_FILTER_OUTPUT_TYPE_DIFFERENT=4};
+
+enum {
+  AVS_CACHE_NOTHING=0,
+  AVS_CACHE_RANGE=1,
+  AVS_CACHE_ALL=2,
+  AVS_CACHE_AUDIO=3,
+  AVS_CACHE_AUDIO_NONE=4,
+  AVS_CACHE_AUDIO_AUTO=5
+};
+
+#define AVS_FRAME_ALIGN 16
+
+typedef struct AVS_Clip AVS_Clip;
+typedef struct AVS_ScriptEnvironment AVS_ScriptEnvironment;
+
+/////////////////////////////////////////////////////////////////////
+//
+// AVS_VideoInfo
+//
+
+// AVS_VideoInfo is layed out identicly to VideoInfo
+typedef struct AVS_VideoInfo {
+  int width, height;    // width=0 means no video
+  unsigned fps_numerator, fps_denominator;
+  int num_frames;
+
+  int pixel_type;
+
+  int audio_samples_per_second;   // 0 means no audio
+  int sample_type;
+  INT64 num_audio_samples;
+  int nchannels;
+
+  // Imagetype properties
+
+  int image_type;
+} AVS_VideoInfo;
+
+// useful functions of the above
+AVSC_INLINE int avs_has_video(const AVS_VideoInfo * p)
+        { return (p->width!=0); }
+
+AVSC_INLINE int avs_has_audio(const AVS_VideoInfo * p)
+        { return (p->audio_samples_per_second!=0); }
+
+AVSC_INLINE int avs_is_rgb(const AVS_VideoInfo * p)
+        { return !!(p->pixel_type&AVS_CS_BGR); }
+
+AVSC_INLINE int avs_is_rgb24(const AVS_VideoInfo * p)
+        { return (p->pixel_type&AVS_CS_BGR24)==AVS_CS_BGR24; } // Clear out additional properties
+
+AVSC_INLINE int avs_is_rgb32(const AVS_VideoInfo * p)
+        { return (p->pixel_type & AVS_CS_BGR32) == AVS_CS_BGR32 ; }
+
+AVSC_INLINE int avs_is_yuv(const AVS_VideoInfo * p)
+        { return !!(p->pixel_type&AVS_CS_YUV ); }
+
+AVSC_INLINE int avs_is_yuy2(const AVS_VideoInfo * p)
+        { return (p->pixel_type & AVS_CS_YUY2) == AVS_CS_YUY2; }
+
+AVSC_INLINE int avs_is_yv12(const AVS_VideoInfo * p)
+        { return ((p->pixel_type & AVS_CS_YV12) == AVS_CS_YV12)||((p->pixel_type & AVS_CS_I420) == AVS_CS_I420); }
+
+AVSC_INLINE int avs_is_color_space(const AVS_VideoInfo * p, int c_space)
+        { return ((p->pixel_type & c_space) == c_space); }
+
+AVSC_INLINE int avs_is_property(const AVS_VideoInfo * p, int property)
+        { return ((p->pixel_type & property)==property ); }
+
+AVSC_INLINE int avs_is_planar(const AVS_VideoInfo * p)
+        { return !!(p->pixel_type & AVS_CS_PLANAR); }
+
+AVSC_INLINE int avs_is_field_based(const AVS_VideoInfo * p)
+        { return !!(p->image_type & AVS_IT_FIELDBASED); }
+
+AVSC_INLINE int avs_is_parity_known(const AVS_VideoInfo * p)
+        { return ((p->image_type & AVS_IT_FIELDBASED)&&(p->image_type & (AVS_IT_BFF | AVS_IT_TFF))); }
+
+AVSC_INLINE int avs_is_bff(const AVS_VideoInfo * p)
+        { return !!(p->image_type & AVS_IT_BFF); }
+
+AVSC_INLINE int avs_is_tff(const AVS_VideoInfo * p)
+        { return !!(p->image_type & AVS_IT_TFF); }
+
+AVSC_INLINE int avs_bits_per_pixel(const AVS_VideoInfo * p)
+{
+  switch (p->pixel_type) {
+      case AVS_CS_BGR24: return 24;
+      case AVS_CS_BGR32: return 32;
+      case AVS_CS_YUY2:  return 16;
+      case AVS_CS_YV12:
+      case AVS_CS_I420:  return 12;
+      default:           return 0;
+    }
+}
+AVSC_INLINE int avs_bytes_from_pixels(const AVS_VideoInfo * p, int pixels)
+        { return pixels * (avs_bits_per_pixel(p)>>3); }   // Will work on planar images, but will return only luma planes
+
+AVSC_INLINE int avs_row_size(const AVS_VideoInfo * p)
+        { return avs_bytes_from_pixels(p,p->width); }  // Also only returns first plane on planar images
+
+AVSC_INLINE int avs_bmp_size(const AVS_VideoInfo * vi)
+        { if (avs_is_planar(vi)) {int p = vi->height * ((avs_row_size(vi)+3) & ~3); p+=p>>1; return p;  } return vi->height * ((avs_row_size(vi)+3) & ~3); }
+
+AVSC_INLINE int avs_samples_per_second(const AVS_VideoInfo * p)
+        { return p->audio_samples_per_second; }
+
+
+AVSC_INLINE int avs_bytes_per_channel_sample(const AVS_VideoInfo * p)
+{
+    switch (p->sample_type) {
+      case AVS_SAMPLE_INT8:  return sizeof(signed char);
+      case AVS_SAMPLE_INT16: return sizeof(signed short);
+      case AVS_SAMPLE_INT24: return 3;
+      case AVS_SAMPLE_INT32: return sizeof(signed int);
+      case AVS_SAMPLE_FLOAT: return sizeof(float);
+      default: return 0;
+    }
+}
+AVSC_INLINE int avs_bytes_per_audio_sample(const AVS_VideoInfo * p)
+        { return p->nchannels*avs_bytes_per_channel_sample(p);}
+
+AVSC_INLINE INT64 avs_audio_samples_from_frames(const AVS_VideoInfo * p, INT64 frames)
+        { return ((INT64)(frames) * p->audio_samples_per_second * p->fps_denominator / p->fps_numerator); }
+
+AVSC_INLINE int avs_frames_from_audio_samples(const AVS_VideoInfo * p, INT64 samples)
+        { return (int)(samples * (INT64)p->fps_numerator / (INT64)p->fps_denominator / (INT64)p->audio_samples_per_second); }
+
+AVSC_INLINE INT64 avs_audio_samples_from_bytes(const AVS_VideoInfo * p, INT64 bytes)
+        { return bytes / avs_bytes_per_audio_sample(p); }
+
+AVSC_INLINE INT64 avs_bytes_from_audio_samples(const AVS_VideoInfo * p, INT64 samples)
+        { return samples * avs_bytes_per_audio_sample(p); }
+
+AVSC_INLINE int avs_audio_channels(const AVS_VideoInfo * p)
+        { return p->nchannels; }
+
+AVSC_INLINE int avs_sample_type(const AVS_VideoInfo * p)
+        { return p->sample_type;}
+
+// useful mutator
+AVSC_INLINE void avs_set_property(AVS_VideoInfo * p, int property)
+        { p->image_type|=property; }
+
+AVSC_INLINE void avs_clear_property(AVS_VideoInfo * p, int property)
+        { p->image_type&=~property; }
+
+AVSC_INLINE void avs_set_field_based(AVS_VideoInfo * p, int isfieldbased)
+        { if (isfieldbased) p->image_type|=AVS_IT_FIELDBASED; else p->image_type&=~AVS_IT_FIELDBASED; }
+
+AVSC_INLINE void avs_set_fps(AVS_VideoInfo * p, unsigned numerator, unsigned denominator)
+{
+    unsigned x=numerator, y=denominator;
+    while (y) {   // find gcd
+      unsigned t = x%y; x = y; y = t;
+    }
+    p->fps_numerator = numerator/x;
+    p->fps_denominator = denominator/x;
+}
+
+AVSC_INLINE int avs_is_same_colorspace(AVS_VideoInfo * x, AVS_VideoInfo * y)
+{
+        return (x->pixel_type == y->pixel_type)
+                || (avs_is_yv12(x) && avs_is_yv12(y));
+}
+
+/////////////////////////////////////////////////////////////////////
+//
+// AVS_VideoFrame
+//
+
+// VideoFrameBuffer holds information about a memory block which is used
+// for video data.  For efficiency, instances of this class are not deleted
+// when the refcount reaches zero; instead they're stored in a linked list
+// to be reused.  The instances are deleted when the corresponding AVS
+// file is closed.
+
+// AVS_VideoFrameBuffer is layed out identicly to VideoFrameBuffer
+// DO NOT USE THIS STRUCTURE DIRECTLY
+typedef struct AVS_VideoFrameBuffer {
+  unsigned char * data;
+  int data_size;
+  // sequence_number is incremented every time the buffer is changed, so
+  // that stale views can tell they're no longer valid.
+  long sequence_number;
+
+  long refcount;
+} AVS_VideoFrameBuffer;
+
+// VideoFrame holds a "window" into a VideoFrameBuffer.
+
+// AVS_VideoFrame is layed out identicly to IVideoFrame
+// DO NOT USE THIS STRUCTURE DIRECTLY
+typedef struct AVS_VideoFrame {
+  int refcount;
+  AVS_VideoFrameBuffer * vfb;
+  int offset, pitch, row_size, height, offsetU, offsetV, pitchUV;  // U&V offsets are from top of picture.
+} AVS_VideoFrame;
+
+// Access functions for AVS_VideoFrame
+AVSC_INLINE int avs_get_pitch(const AVS_VideoFrame * p) {
+        return p->pitch;}
+
+AVSC_INLINE int avs_get_pitch_p(const AVS_VideoFrame * p, int plane) {
+  switch (plane) {
+  case AVS_PLANAR_U: case AVS_PLANAR_V: return p->pitchUV;}
+  return p->pitch;}
+
+AVSC_INLINE int avs_get_row_size(const AVS_VideoFrame * p) {
+        return p->row_size; }
+
+AVSC_INLINE int avs_get_row_size_p(const AVS_VideoFrame * p, int plane) {
+        int r;
+    switch (plane) {
+    case AVS_PLANAR_U: case AVS_PLANAR_V:
+                if (p->pitchUV) return p->row_size>>1;
+                else            return 0;
+    case AVS_PLANAR_U_ALIGNED: case AVS_PLANAR_V_ALIGNED:
+                if (p->pitchUV) {
+                        r = ((p->row_size+AVS_FRAME_ALIGN-1)&(~(AVS_FRAME_ALIGN-1)) )>>1; // Aligned rowsize
+                        if (r < p->pitchUV)
+                                return r;
+                        return p->row_size>>1;
+                } else return 0;
+    case AVS_PLANAR_Y_ALIGNED:
+                r = (p->row_size+AVS_FRAME_ALIGN-1)&(~(AVS_FRAME_ALIGN-1)); // Aligned rowsize
+                if (r <= p->pitch)
+                        return r;
+                return p->row_size;
+    }
+    return p->row_size;
+}
+
+AVSC_INLINE int avs_get_height(const AVS_VideoFrame * p) {
+        return p->height;}
+
+AVSC_INLINE int avs_get_height_p(const AVS_VideoFrame * p, int plane) {
+        switch (plane) {
+                case AVS_PLANAR_U: case AVS_PLANAR_V:
+                        if (p->pitchUV) return p->height>>1;
+                        return 0;
+        }
+        return p->height;}
+
+AVSC_INLINE const unsigned char* avs_get_read_ptr(const AVS_VideoFrame * p) {
+        return p->vfb->data + p->offset;}
+
+AVSC_INLINE const unsigned char* avs_get_read_ptr_p(const AVS_VideoFrame * p, int plane)
+{
+        switch (plane) {
+                case AVS_PLANAR_U: return p->vfb->data + p->offsetU;
+                case AVS_PLANAR_V: return p->vfb->data + p->offsetV;
+                default:           return p->vfb->data + p->offset;}
+}
+
+AVSC_INLINE int avs_is_writable(const AVS_VideoFrame * p) {
+        return (p->refcount == 1 && p->vfb->refcount == 1);}
+
+AVSC_INLINE unsigned char* avs_get_write_ptr(const AVS_VideoFrame * p)
+{
+        if (avs_is_writable(p)) {
+                ++p->vfb->sequence_number;
+                return p->vfb->data + p->offset;
+        } else
+                return 0;
+}
+
+AVSC_INLINE unsigned char* avs_get_write_ptr_p(const AVS_VideoFrame * p, int plane)
+{
+        if (plane==AVS_PLANAR_Y && avs_is_writable(p)) {
+                ++p->vfb->sequence_number;
+                return p->vfb->data + p->offset;
+        } else if (plane==AVS_PLANAR_Y) {
+                return 0;
+        } else {
+                switch (plane) {
+                        case AVS_PLANAR_U: return p->vfb->data + p->offsetU;
+                        case AVS_PLANAR_V: return p->vfb->data + p->offsetV;
+                        default:       return p->vfb->data + p->offset;
+                }
+        }
+}
+
+#if defined __cplusplus
+extern "C"
+{
+#endif // __cplusplus
+AVSC_API(void, avs_release_video_frame)(AVS_VideoFrame *);
+// makes a shallow copy of a video frame
+AVSC_API(AVS_VideoFrame *, avs_copy_video_frame)(AVS_VideoFrame *);
+#if defined __cplusplus
+}
+#endif // __cplusplus
+
+#ifndef AVSC_NO_DECLSPEC
+AVSC_INLINE void avs_release_frame(AVS_VideoFrame * f)
+  {avs_release_video_frame(f);}
+AVSC_INLINE AVS_VideoFrame * avs_copy_frame(AVS_VideoFrame * f)
+  {return avs_copy_video_frame(f);}
+#endif
+
+/////////////////////////////////////////////////////////////////////
+//
+// AVS_Value
+//
+
+// Treat AVS_Value as a fat pointer.  That is use avs_copy_value
+// and avs_release_value appropiaty as you would if AVS_Value was
+// a pointer.
+
+// To maintain source code compatibility with future versions of the
+// avisynth_c API don't use the AVS_Value directly.  Use the helper
+// functions below.
+
+// AVS_Value is layed out identicly to AVSValue
+typedef struct AVS_Value AVS_Value;
+struct AVS_Value {
+  short type;  // 'a'rray, 'c'lip, 'b'ool, 'i'nt, 'f'loat, 's'tring, 'v'oid, or 'l'ong
+               // for some function e'rror
+  short array_size;
+  union {
+    void * clip; // do not use directly, use avs_take_clip
+    char boolean;
+    int integer;
+    INT64 integer64; // match addition of __int64 to avxplugin.h
+    float floating_pt;
+    const char * string;
+    const AVS_Value * array;
+  } d;
+};
+
+// AVS_Value should be initilized with avs_void.
+// Should also set to avs_void after the value is released
+// with avs_copy_value.  Consider it the equalvent of setting
+// a pointer to NULL
+static const AVS_Value avs_void = {'v'};
+
+AVSC_API(void, avs_copy_value)(AVS_Value * dest, AVS_Value src);
+AVSC_API(void, avs_release_value)(AVS_Value);
+
+AVSC_INLINE int avs_defined(AVS_Value v) { return v.type != 'v'; }
+AVSC_INLINE int avs_is_clip(AVS_Value v) { return v.type == 'c'; }
+AVSC_INLINE int avs_is_bool(AVS_Value v) { return v.type == 'b'; }
+AVSC_INLINE int avs_is_int(AVS_Value v) { return v.type == 'i'; }
+AVSC_INLINE int avs_is_float(AVS_Value v) { return v.type == 'f' || v.type == 'i'; }
+AVSC_INLINE int avs_is_string(AVS_Value v) { return v.type == 's'; }
+AVSC_INLINE int avs_is_array(AVS_Value v) { return v.type == 'a'; }
+AVSC_INLINE int avs_is_error(AVS_Value v) { return v.type == 'e'; }
+
+#if defined __cplusplus
+extern "C"
+{
+#endif // __cplusplus
+AVSC_API(AVS_Clip *, avs_take_clip)(AVS_Value, AVS_ScriptEnvironment *);
+AVSC_API(void, avs_set_to_clip)(AVS_Value *, AVS_Clip *);
+#if defined __cplusplus
+}
+#endif // __cplusplus
+
+AVSC_INLINE int avs_as_bool(AVS_Value v)
+        { return v.d.boolean; }
+AVSC_INLINE int avs_as_int(AVS_Value v)
+        { return v.d.integer; }
+AVSC_INLINE const char * avs_as_string(AVS_Value v)
+        { return avs_is_error(v) || avs_is_string(v) ? v.d.string : 0; }
+AVSC_INLINE double avs_as_float(AVS_Value v)
+        { return avs_is_int(v) ? v.d.integer : v.d.floating_pt; }
+AVSC_INLINE const char * avs_as_error(AVS_Value v)
+        { return avs_is_error(v) ? v.d.string : 0; }
+AVSC_INLINE const AVS_Value * avs_as_array(AVS_Value v)
+        { return v.d.array; }
+AVSC_INLINE int avs_array_size(AVS_Value v)
+        { return avs_is_array(v) ? v.array_size : 1; }
+AVSC_INLINE AVS_Value avs_array_elt(AVS_Value v, int index)
+        { return avs_is_array(v) ? v.d.array[index] : v; }
+
+// only use these functions on am AVS_Value that does not already have
+// an active value.  Remember, treat AVS_Value as a fat pointer.
+AVSC_INLINE AVS_Value avs_new_value_bool(int v0)
+        { AVS_Value v; v.type = 'b'; v.d.boolean = v0 == 0 ? 0 : 1; return v; }
+AVSC_INLINE AVS_Value avs_new_value_int(int v0)
+        { AVS_Value v; v.type = 'i'; v.d.integer = v0; return v; }
+AVSC_INLINE AVS_Value avs_new_value_string(const char * v0)
+        { AVS_Value v; v.type = 's'; v.d.string = v0; return v; }
+AVSC_INLINE AVS_Value avs_new_value_float(float v0)
+        { AVS_Value v; v.type = 'f'; v.d.floating_pt = v0; return v;}
+AVSC_INLINE AVS_Value avs_new_value_error(const char * v0)
+        { AVS_Value v; v.type = 'e'; v.d.string = v0; return v; }
+#ifndef AVSC_NO_DECLSPEC
+AVSC_INLINE AVS_Value avs_new_value_clip(AVS_Clip * v0)
+        { AVS_Value v; avs_set_to_clip(&v, v0); return v; }
+#endif
+AVSC_INLINE AVS_Value avs_new_value_array(AVS_Value * v0, int size)
+        { AVS_Value v; v.type = 'a'; v.d.array = v0; v.array_size = size; return v; }
+
+/////////////////////////////////////////////////////////////////////
+//
+// AVS_Clip
+//
+#if defined __cplusplus
+extern "C"
+{
+#endif // __cplusplus
+AVSC_API(void, avs_release_clip)(AVS_Clip *);
+AVSC_API(AVS_Clip *, avs_copy_clip)(AVS_Clip *);
+
+AVSC_API(const char *, avs_clip_get_error)(AVS_Clip *); // return 0 if no error
+
+AVSC_API(const AVS_VideoInfo *, avs_get_video_info)(AVS_Clip *);
+
+AVSC_API(int, avs_get_version)(AVS_Clip *);
+
+AVSC_API(AVS_VideoFrame *, avs_get_frame)(AVS_Clip *, int n);
+// The returned video frame must be released with avs_release_video_frame
+
+AVSC_API(int, avs_get_parity)(AVS_Clip *, int n);
+// return field parity if field_based, else parity of first field in frame
+
+AVSC_API(int, avs_get_audio)(AVS_Clip *, void * buf,
+                                  INT64 start, INT64 count);
+// start and count are in samples
+
+AVSC_API(int, avs_set_cache_hints)(AVS_Clip *,
+                                        int cachehints, size_t frame_range);
+#if defined __cplusplus
+}
+#endif // __cplusplus
+
+// This is the callback type used by avs_add_function
+typedef AVS_Value (AVSC_CC * AVS_ApplyFunc)
+                        (AVS_ScriptEnvironment *, AVS_Value args, void * user_data);
+
+typedef struct AVS_FilterInfo AVS_FilterInfo;
+struct AVS_FilterInfo
+{
+  // these members should not be modified outside of the AVS_ApplyFunc callback
+  AVS_Clip * child;
+  AVS_VideoInfo vi;
+  AVS_ScriptEnvironment * env;
+  AVS_VideoFrame * (AVSC_CC * get_frame)(AVS_FilterInfo *, int n);
+  int (AVSC_CC * get_parity)(AVS_FilterInfo *, int n);
+  int (AVSC_CC * get_audio)(AVS_FilterInfo *, void * buf,
+                                  INT64 start, INT64 count);
+  int (AVSC_CC * set_cache_hints)(AVS_FilterInfo *, int cachehints,
+                                        int frame_range);
+  void (AVSC_CC * free_filter)(AVS_FilterInfo *);
+
+  // Should be set when ever there is an error to report.
+  // It is cleared before any of the above methods are called
+  const char * error;
+  // this is to store whatever and may be modified at will
+  void * user_data;
+};
+
+// Create a new filter
+// fi is set to point to the AVS_FilterInfo so that you can
+//   modify it once it is initilized.
+// store_child should generally be set to true.  If it is not
+//    set than ALL methods (the function pointers) must be defined
+// If it is set than you do not need to worry about freeing the child
+//    clip.
+#if defined __cplusplus
+extern "C"
+{
+#endif // __cplusplus
+AVSC_API(AVS_Clip *, avs_new_c_filter)(AVS_ScriptEnvironment * e,
+                                      AVS_FilterInfo * * fi,
+                                      AVS_Value child, int store_child);
+#if defined __cplusplus
+}
+#endif // __cplusplus
+
+
+/////////////////////////////////////////////////////////////////////
+//
+// AVS_ScriptEnvironment
+//
+
+// For GetCPUFlags.  These are backwards-compatible with those in VirtualDub.
+enum {
+                                /* slowest CPU to support extension */
+  AVS_CPU_FORCE        = 0x01,   // N/A
+  AVS_CPU_FPU          = 0x02,   // 386/486DX
+  AVS_CPU_MMX          = 0x04,   // P55C, K6, PII
+  AVS_CPU_INTEGER_SSE  = 0x08,   // PIII, Athlon
+  AVS_CPU_SSE          = 0x10,   // PIII, Athlon XP/MP
+  AVS_CPU_SSE2         = 0x20,   // PIV, Hammer
+  AVS_CPU_3DNOW        = 0x40,   // K6-2
+  AVS_CPU_3DNOW_EXT    = 0x80,   // Athlon
+  AVS_CPU_X86_64       = 0xA0,   // Hammer (note: equiv. to 3DNow + SSE2,
+                                 // which only Hammer will have anyway)
+};
+
+#if defined __cplusplus
+extern "C"
+{
+#endif // __cplusplus
+AVSC_API(const char *, avs_get_error)(AVS_ScriptEnvironment *); // return 0 if no error
+
+AVSC_API(long, avs_get_cpu_flags)(AVS_ScriptEnvironment *);
+AVSC_API(int, avs_check_version)(AVS_ScriptEnvironment *, int version);
+
+AVSC_API(char *, avs_save_string)(AVS_ScriptEnvironment *, const char* s, int length);
+AVSC_API(char *, avs_sprintf)(AVS_ScriptEnvironment *, const char * fmt, ...);
+
+AVSC_API(char *, avs_vsprintf)(AVS_ScriptEnvironment *, const char * fmt, va_list val);
+ // note: val is really a va_list; I hope everyone typedefs va_list to a pointer
+
+AVSC_API(int, avs_add_function)(AVS_ScriptEnvironment *,
+                                     const char * name, const char * params,
+                                     AVS_ApplyFunc apply, void * user_data);
+
+AVSC_API(int, avs_function_exists)(AVS_ScriptEnvironment *, const char * name);
+
+AVSC_API(AVS_Value, avs_invoke)(AVS_ScriptEnvironment *, const char * name,
+                               AVS_Value args, const char** arg_names);
+// The returned value must be be released with avs_release_value
+
+AVSC_API(AVS_Value, avs_get_var)(AVS_ScriptEnvironment *, const char* name);
+// The returned value must be be released with avs_release_value
+
+AVSC_API(int, avs_set_var)(AVS_ScriptEnvironment *, const char* name, AVS_Value val);
+
+AVSC_API(int, avs_set_global_var)(AVS_ScriptEnvironment *, const char* name, const AVS_Value val);
+
+//void avs_push_context(AVS_ScriptEnvironment *, int level=0);
+//void avs_pop_context(AVS_ScriptEnvironment *);
+
+AVSC_API(AVS_VideoFrame *, avs_new_video_frame_a)(AVS_ScriptEnvironment *,
+                                          const AVS_VideoInfo * vi, int align);
+// align should be at least 16
+#if defined __cplusplus
+}
+#endif // __cplusplus
+
+#ifndef AVSC_NO_DECLSPEC
+AVSC_INLINE
+AVS_VideoFrame * avs_new_video_frame(AVS_ScriptEnvironment * env,
+                                     const AVS_VideoInfo * vi)
+  {return avs_new_video_frame_a(env,vi,AVS_FRAME_ALIGN);}
+
+AVSC_INLINE
+AVS_VideoFrame * avs_new_frame(AVS_ScriptEnvironment * env,
+                               const AVS_VideoInfo * vi)
+  {return avs_new_video_frame_a(env,vi,AVS_FRAME_ALIGN);}
+#endif
+
+#if defined __cplusplus
+extern "C"
+{
+#endif // __cplusplus
+AVSC_API(int, avs_make_writable)(AVS_ScriptEnvironment *, AVS_VideoFrame * * pvf);
+
+AVSC_API(void, avs_bit_blt)(AVS_ScriptEnvironment *, unsigned char* dstp, int dst_pitch, const unsigned char* srcp, int src_pitch, int row_size, int height);
+
+typedef void (AVSC_CC *AVS_ShutdownFunc)(void* user_data, AVS_ScriptEnvironment * env);
+AVSC_API(void, avs_at_exit)(AVS_ScriptEnvironment *, AVS_ShutdownFunc function, void * user_data);
+
+AVSC_API(AVS_VideoFrame *, avs_subframe)(AVS_ScriptEnvironment *, AVS_VideoFrame * src, int rel_offset, int new_pitch, int new_row_size, int new_height);
+// The returned video frame must be be released
+
+AVSC_API(int, avs_set_memory_max)(AVS_ScriptEnvironment *, int mem);
+
+AVSC_API(int, avs_set_working_dir)(AVS_ScriptEnvironment *, const char * newdir);
+
+// avisynth.dll exports this; it's a way to use it as a library, without
+// writing an AVS script or without going through AVIFile.
+AVSC_API(AVS_ScriptEnvironment *, avs_create_script_environment)(int version);
+#if defined __cplusplus
+}
+#endif // __cplusplus
+
+// this symbol is the entry point for the plugin and must
+// be defined
+AVSC_EXPORT
+const char * AVSC_CC avisynth_c_plugin_init(AVS_ScriptEnvironment* env);
+
+
+#if defined __cplusplus
+extern "C"
+{
+#endif // __cplusplus
+AVSC_API(void, avs_delete_script_environment)(AVS_ScriptEnvironment *);
+
+
+AVSC_API(AVS_VideoFrame *, avs_subframe_planar)(AVS_ScriptEnvironment *, AVS_VideoFrame * src, int rel_offset, int new_pitch, int new_row_size, int new_height, int rel_offsetU, int rel_offsetV, int new_pitchUV);
+// The returned video frame must be be released
+#if defined __cplusplus
+}
+#endif // __cplusplus
+
+#endif //__AVXSYNTH_C__
diff --git a/compat/avisynth/windowsPorts/basicDataTypeConversions.h b/compat/avisynth/windowsPorts/basicDataTypeConversions.h
new file mode 100644
index 0000000..ff367d5
--- /dev/null
+++ b/compat/avisynth/windowsPorts/basicDataTypeConversions.h
@@ -0,0 +1,85 @@
+#ifndef __DATA_TYPE_CONVERSIONS_H__
+#define __DATA_TYPE_CONVERSIONS_H__
+
+#include <stdint.h>
+#include <wchar.h>
+
+#ifdef __cplusplus
+namespace avxsynth {
+#endif // __cplusplus
+
+typedef int64_t __int64;
+typedef int32_t __int32;
+#ifdef __cplusplus
+typedef bool BOOL;
+#else
+typedef uint32_t BOOL;
+#endif // __cplusplus
+typedef void* HMODULE;
+typedef void* LPVOID;
+typedef void* PVOID;
+typedef PVOID HANDLE;
+typedef HANDLE HWND;
+typedef HANDLE HINSTANCE;
+typedef void* HDC;
+typedef void* HBITMAP;
+typedef void* HICON;
+typedef void* HFONT;
+typedef void* HGDIOBJ;
+typedef void* HBRUSH;
+typedef void* HMMIO;
+typedef void* HACMSTREAM;
+typedef void* HACMDRIVER;
+typedef void* HIC;
+typedef void* HACMOBJ;
+typedef HACMSTREAM* LPHACMSTREAM;
+typedef void* HACMDRIVERID;
+typedef void* LPHACMDRIVER;
+typedef unsigned char BYTE;
+typedef BYTE* LPBYTE;
+typedef char TCHAR;
+typedef TCHAR* LPTSTR;
+typedef const TCHAR* LPCTSTR;
+typedef char* LPSTR;
+typedef LPSTR LPOLESTR;
+typedef const char* LPCSTR;
+typedef LPCSTR LPCOLESTR;
+typedef wchar_t WCHAR;
+typedef unsigned short WORD;
+typedef unsigned int UINT;
+typedef UINT MMRESULT;
+typedef uint32_t DWORD;
+typedef DWORD COLORREF;
+typedef DWORD FOURCC;
+typedef DWORD HRESULT;
+typedef DWORD* LPDWORD;
+typedef DWORD* DWORD_PTR;
+typedef int32_t LONG;
+typedef int32_t* LONG_PTR;
+typedef LONG_PTR LRESULT;
+typedef uint32_t ULONG;
+typedef uint32_t* ULONG_PTR;
+//typedef __int64_t intptr_t;
+typedef uint64_t _fsize_t;
+
+
+//
+// Structures
+//
+
+typedef struct _GUID {
+  DWORD Data1;
+  WORD  Data2;
+  WORD  Data3;
+  BYTE  Data4[8];
+} GUID;
+
+typedef GUID REFIID;
+typedef GUID CLSID;
+typedef CLSID* LPCLSID;
+typedef GUID IID;
+
+#ifdef __cplusplus
+}; // namespace avxsynth
+#endif // __cplusplus
+#endif //  __DATA_TYPE_CONVERSIONS_H__
diff --git a/compat/avisynth/windowsPorts/windows2linux.h b/compat/avisynth/windowsPorts/windows2linux.h
new file mode 100644
index 0000000..5476afe
--- /dev/null
+++ b/compat/avisynth/windowsPorts/windows2linux.h
@@ -0,0 +1,77 @@
+#ifndef __WINDOWS2LINUX_H__
+#define __WINDOWS2LINUX_H__
+
+/*
+ * LINUX SPECIFIC DEFINITIONS
+*/
+//
+// Data types conversions
+//
+#include <stdlib.h>
+#include <string.h>
+#include "basicDataTypeConversions.h"
+
+#ifdef __cplusplus
+namespace avxsynth {
+#endif // __cplusplus
+//
+// purposefully define the following MSFT definitions
+// to mean nothing (as they do not mean anything on Linux)
+//
+#define __stdcall
+#define __cdecl
+#define noreturn
+#define __declspec(x)
+#define STDAPI       extern "C" HRESULT
+#define STDMETHODIMP HRESULT __stdcall
+#define STDMETHODIMP_(x) x __stdcall
+
+#define STDMETHOD(x)    virtual HRESULT x
+#define STDMETHOD_(a, x) virtual a x
+
+#ifndef TRUE
+#define TRUE  true
+#endif
+
+#ifndef FALSE
+#define FALSE false
+#endif
+
+#define S_OK                (0x00000000)
+#define S_FALSE             (0x00000001)
+#define E_NOINTERFACE       (0X80004002)
+#define E_POINTER           (0x80004003)
+#define E_FAIL              (0x80004005)
+#define E_OUTOFMEMORY       (0x8007000E)
+
+#define INVALID_HANDLE_VALUE    ((HANDLE)((LONG_PTR)-1))
+#define FAILED(hr)              ((hr) & 0x80000000)
+#define SUCCEEDED(hr)           (!FAILED(hr))
+
+
+//
+// Functions
+//
+#define MAKEDWORD(a,b,c,d) ((a << 24) | (b << 16) | (c << 8) | (d))
+#define MAKEWORD(a,b) ((a << 8) | (b))
+
+#define lstrlen                             strlen
+#define lstrcpy                             strcpy
+#define lstrcmpi                            strcasecmp
+#define _stricmp                            strcasecmp
+#define InterlockedIncrement(x)             __sync_fetch_and_add((x), 1)
+#define InterlockedDecrement(x)             __sync_fetch_and_sub((x), 1)
+// Windows uses (new, old) ordering but GCC has (old, new)
+#define InterlockedCompareExchange(x,y,z)   __sync_val_compare_and_swap(x,z,y)
+
+#define UInt32x32To64(a, b)                 ( (uint64_t) ( ((uint64_t)((uint32_t)(a))) * ((uint32_t)(b))  ) )
+#define Int64ShrlMod32(a, b)                ( (uint64_t) ( (uint64_t)(a) >> (b) ) )
+#define Int32x32To64(a, b)                  ((__int64)(((__int64)((long)(a))) * ((long)(b))))
+
+#define MulDiv(nNumber, nNumerator, nDenominator)   (int32_t) (((int64_t) (nNumber) * (int64_t) (nNumerator) + (int64_t) ((nDenominator)/2)) / (int64_t) (nDenominator))
+
+#ifdef __cplusplus
+}; // namespace avxsynth
+#endif // __cplusplus
+
+#endif //  __WINDOWS2LINUX_H__
diff --git a/compat/strtod.c b/compat/strtod.c
index d0f9b3d..3a9452e 100644
--- a/compat/strtod.c
+++ b/compat/strtod.c
@@ -19,7 +19,6 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include <ctype.h>
 #include <limits.h>
 #include <stdlib.h>
 
@@ -49,7 +48,7 @@
     double res;
 
     /* Skip leading spaces */
-    while (isspace(*nptr))
+    while (av_isspace(*nptr))
         nptr++;
 
     if (!av_strncasecmp(nptr, "infinity", 8)) {
diff --git a/configure b/configure
index a9907ac..648db2f 100755
--- a/configure
+++ b/configure
@@ -133,6 +133,7 @@
   --disable-network        disable network support [no]
   --disable-dct            disable DCT code
   --disable-dwt            disable DWT code
+  --disable-error-resilience disable error resilience code
   --disable-lsp            disable LSP code
   --disable-lzo            disable LZO decoder code
   --disable-mdct           disable MDCT code
@@ -214,8 +215,10 @@
   --enable-libopenjpeg     enable JPEG 2000 de/encoding via OpenJPEG [no]
   --enable-libopus         enable Opus decoding via libopus [no]
   --enable-libpulse        enable Pulseaudio input via libpulse [no]
+  --enable-libquvi         enable quvi input via libquvi [no]
   --enable-librtmp         enable RTMP[E] support via librtmp [no]
   --enable-libschroedinger enable Dirac de/encoding via libschroedinger [no]
+  --enable-libshine        enable fixed-point MP3 encoding via libshine [no]
   --enable-libsoxr         enable Include libsoxr resampling [no]
   --enable-libspeex        enable Speex de/encoding via libspeex [no]
   --enable-libstagefright-h264  enable H.264 decoding via libstagefright [no]
@@ -223,6 +226,7 @@
   --enable-libtwolame      enable MP2 encoding via libtwolame [no]
   --enable-libutvideo      enable Ut Video encoding and decoding via libutvideo [no]
   --enable-libv4l2         enable libv4l2/v4l-utils [no]
+  --enable-libvidstab      enable video stabilization using vid.stab [no]
   --enable-libvo-aacenc    enable AAC encoding via libvo-aacenc [no]
   --enable-libvo-amrwbenc  enable AMR-WB encoding via libvo-amrwbenc [no]
   --enable-libvorbis       enable Vorbis en/decoding via libvorbis,
@@ -233,6 +237,7 @@
   --enable-libxvid         enable Xvid encoding via xvidcore,
                            native MPEG-4/Xvid encoder exists [no]
   --enable-openal          enable OpenAL 1.1 capture support [no]
+  --enable-opencl          enable OpenCL code
   --enable-openssl         enable openssl [no]
   --enable-x11grab         enable X11 grabbing [no]
   --enable-zlib            enable zlib [autodetect]
@@ -314,7 +319,6 @@
   --disable-fast-unaligned consider unaligned accesses slow
 
 Developer options (useful when working on FFmpeg itself):
-  --enable-coverage        build with test coverage instrumentation
   --disable-debug          disable debugging symbols
   --enable-debug=LEVEL     set the debug level [$debuglevel]
   --disable-optimizations  disable compiler optimizations
@@ -456,14 +460,18 @@
     done
 }
 
+sanitize_var_name(){
+    echo $@ | sed 's/[^A-Za-z0-9_]/_/g'
+}
+
 set_safe(){
     var=$1
     shift
-    eval $(echo "$var" | sed 's/[^A-Za-z0-9_]/_/g')='$*'
+    eval $(sanitize_var_name "$var")='$*'
 }
 
 get_safe(){
-    eval echo \$$(echo "$1" | sed 's/[^A-Za-z0-9_]/_/g')
+    eval echo \$$(sanitize_var_name "$1")
 }
 
 pushvar(){
@@ -652,6 +660,12 @@
                 } else if (file ~ /\\.mak\$/) {
                     n = -v ? \"\" : \"!\";
                     printf(\"%s%s=yes\\n\", n, c) >>file;
+                } else if (file ~ /\\.texi\$/) {
+                    pre = -v ? \"\" : \"@c \";
+                    yesno = \$2;
+                    c2 = tolower(c);
+                    gsub(/_/, \"-\", c2);
+                    printf(\"%s@set %s %s\\n\", pre, c2, yesno) >>file;
                 }
             }
         }"
@@ -1020,7 +1034,7 @@
 }
 
 check_struct(){
-    log check_type "$@"
+    log check_struct "$@"
     headers=$1
     struct=$2
     member=$3
@@ -1030,6 +1044,16 @@
         enable_safe "${struct}_${member}"
 }
 
+check_builtin(){
+    log check_builtin "$@"
+    name=$1
+    headers=$2
+    builtin=$3
+    shift 3
+    disable "$name"
+    check_code ld "$headers" "$builtin" "cc" "$@" && enable "$name"
+}
+
 require(){
     name="$1"
     header="$2"
@@ -1147,8 +1171,10 @@
     libopenjpeg
     libopus
     libpulse
+    libquvi
     librtmp
     libschroedinger
+    libshine
     libsoxr
     libspeex
     libstagefright_h264
@@ -1156,6 +1182,7 @@
     libtwolame
     libutvideo
     libv4l2
+    libvidstab
     libvo_aacenc
     libvo_amrwbenc
     libvorbis
@@ -1164,6 +1191,7 @@
     libxavs
     libxvid
     openal
+    opencl
     openssl
     x11grab
     zlib
@@ -1212,6 +1240,7 @@
     $PROGRAM_LIST
     dct
     dwt
+    error_resilience
     fast_unaligned
     fft
     ftrapv
@@ -1246,6 +1275,12 @@
     os2threads
 '
 
+ATOMICS_LIST='
+    atomics_gcc
+    atomics_suncc
+    atomics_win32
+'
+
 ARCH_LIST='
     aarch64
     alpha
@@ -1356,6 +1391,7 @@
     $HAVE_LIST_CMDLINE
     $HAVE_LIST_PUB
     $THREADS_LIST
+    $ATOMICS_LIST
     $MATH_FUNCS
     access
     aligned_malloc
@@ -1366,6 +1402,8 @@
     asm_mod_q
     asm_mod_y
     asm_types_h
+    atomic_cas_ptr
+    atomics_native
     attribute_may_alias
     attribute_packed
     cdio_paranoia_h
@@ -1423,10 +1461,12 @@
     lzo1x_999_compress
     machine_ioctl_bt848_h
     machine_ioctl_meteor_h
+    machine_rw_barrier
     makeinfo
     malloc_h
     MapViewOfFile
     memalign
+    MemoryBarrier
     mkstemp
     mm_empty
     mmap
@@ -1444,7 +1484,6 @@
     rsync_contimeout
     sched_getaffinity
     sdl
-    sdl_video_size
     SetConsoleTextAttribute
     setmode
     setrlimit
@@ -1466,6 +1505,7 @@
     struct_v4l2_frmivalenum_discrete
     symver_asm_label
     symver_gnu_asm
+    sync_val_compare_and_swap
     sysconf
     sysctl
     sys_mman_h
@@ -1493,7 +1533,8 @@
     aandcttables
     ac3dsp
     audio_frame_queue
-    error_resilience
+    dsputil
+    frame_thread_encoder
     gcrypt
     golomb
     gplv3
@@ -1501,6 +1542,7 @@
     h264dsp
     h264pred
     h264qpel
+    hpeldsp
     huffman
     lgplv3
     lpc
@@ -1523,7 +1565,6 @@
     $HAVE_LIST_CMDLINE
     $THREADS_LIST
     asm
-    coverage
     cross_compile
     debug
     extra_warnings
@@ -1652,72 +1693,100 @@
 
 # subsystems
 dct_select="rdft"
+error_resilience_select="dsputil"
+frame_thread_encoder_deps="encoders threads"
+lpc_select="dsputil"
 mdct_select="fft"
 rdft_select="fft"
 mpegaudio_select="mpegaudiodsp"
 mpegaudiodsp_select="dct"
-mpegvideo_select="videodsp"
+mpegvideo_select="dsputil h264chroma hpeldsp videodsp"
 mpegvideoenc_select="mpegvideo"
 
 # decoders / encoders
 aac_decoder_select="mdct sinewin"
 aac_encoder_select="audio_frame_queue mdct sinewin"
 aac_latm_decoder_select="aac_decoder aac_latm_parser"
-ac3_decoder_select="mdct ac3dsp ac3_parser"
-ac3_encoder_select="mdct ac3dsp"
-ac3_fixed_encoder_select="mdct ac3dsp"
+ac3_decoder_select="mdct ac3dsp ac3_parser dsputil"
+ac3_encoder_select="mdct ac3dsp dsputil"
+ac3_fixed_encoder_select="mdct ac3dsp dsputil"
 alac_encoder_select="lpc"
+als_decoder_select="dsputil"
 amrnb_decoder_select="lsp"
 amrwb_decoder_select="lsp"
+amv_decoder_select="dsputil hpeldsp"
 amv_encoder_select="aandcttables"
+ape_decoder_select="dsputil"
+asv1_decoder_select="dsputil"
+asv1_encoder_select="dsputil"
+asv2_decoder_select="dsputil"
+asv2_encoder_select="dsputil"
 atrac1_decoder_select="mdct sinewin"
 atrac3_decoder_select="mdct"
+bink_decoder_select="dsputil hpeldsp"
 binkaudio_dct_decoder_select="mdct rdft dct sinewin"
 binkaudio_rdft_decoder_select="mdct rdft sinewin"
-cavs_decoder_select="golomb mpegvideo"
+cavs_decoder_select="dsputil golomb h264chroma videodsp"
+cllc_decoder_select="dsputil"
 comfortnoise_encoder_select="lpc"
-cook_decoder_select="mdct sinewin"
+cook_decoder_select="dsputil mdct sinewin"
 cscd_decoder_select="lzo"
 cscd_decoder_suggest="zlib"
 dca_decoder_select="mdct"
-dirac_decoder_select="dwt golomb videodsp"
-dnxhd_encoder_select="aandcttables mpegvideoenc"
+dirac_decoder_select="dsputil dwt golomb videodsp"
+dnxhd_decoder_select="dsputil"
+dnxhd_encoder_select="aandcttables dsputil mpegvideoenc"
+dvvideo_decoder_select="dsputil"
+dvvideo_encoder_select="dsputil"
 dxa_decoder_select="zlib"
 eac3_decoder_select="ac3_decoder"
 eac3_encoder_select="ac3_encoder"
-eamad_decoder_select="aandcttables error_resilience mpegvideo"
+eamad_decoder_select="aandcttables dsputil mpegvideo"
 eatgq_decoder_select="aandcttables"
 eatqi_decoder_select="aandcttables error_resilience mpegvideo"
 exr_decoder_select="zlib"
-ffv1_decoder_select="golomb rangecoder"
-ffv1_encoder_select="rangecoder"
-ffvhuff_encoder_select="huffman"
+ffv1_decoder_select="dsputil golomb rangecoder"
+ffv1_encoder_select="dsputil rangecoder"
+ffvhuff_decoder_select="dsputil"
+ffvhuff_encoder_select="dsputil huffman"
 flac_decoder_select="golomb"
-flac_encoder_select="golomb lpc"
+flac_encoder_select="dsputil golomb lpc"
 flashsv_decoder_select="zlib"
 flashsv_encoder_select="zlib"
 flashsv2_encoder_select="zlib"
 flashsv2_decoder_select="zlib"
 flv_decoder_select="h263_decoder"
 flv_encoder_select="h263_encoder"
-fraps_decoder_select="huffman"
+fourxm_decoder_select="dsputil"
+fraps_decoder_select="dsputil huffman"
+g729_decoder_select="dsputil"
 h261_decoder_select="error_resilience mpegvideo"
 h261_encoder_select="aandcttables mpegvideoenc"
 h263_decoder_select="error_resilience h263_parser mpegvideo"
-h263_encoder_select="aandcttables error_resilience mpegvideoenc"
+h263_encoder_select="aandcttables mpegvideoenc"
 h263i_decoder_select="h263_decoder"
 h263p_encoder_select="h263_encoder"
-h264_decoder_select="error_resilience golomb h264chroma h264dsp h264pred h264qpel mpegvideo videodsp"
-huffyuv_encoder_select="huffman"
-iac_decoder_select="fft mdct sinewin"
-imc_decoder_select="fft mdct sinewin"
-jpegls_decoder_select="golomb"
+h264_decoder_select="golomb h264chroma h264dsp h264pred h264qpel videodsp"
+h264_decoder_suggest="error_resilience"
+huffyuv_decoder_select="dsputil"
+huffyuv_encoder_select="dsputil huffman"
+iac_decoder_select="dsputil fft mdct sinewin"
+imc_decoder_select="dsputil fft mdct sinewin"
+indeo3_decoder_select="hpeldsp"
+interplay_video_decoder_select="hpeldsp"
+jpegls_decoder_select="dsputil golomb hpeldsp"
 jpegls_encoder_select="golomb"
+jv_decoder_select="dsputil"
+lagarith_decoder_select="dsputil"
 ljpeg_encoder_select="aandcttables mpegvideoenc"
 loco_decoder_select="golomb"
-mdec_decoder_select="error_resilience mpegvideo"
-mjpeg_encoder_select="aandcttables mpegvideoenc"
-mlp_decoder_select="mlp_parser"
+mdec_decoder_select="dsputil error_resilience mpegvideo"
+mimic_decoder_select="dsputil hpeldsp"
+mjpeg_decoder_select="dsputil hpeldsp"
+mjpegb_decoder_select="dsputil hpeldsp"
+mjpeg_encoder_select="aandcttables dsputil mpegvideoenc"
+mlp_decoder_select="dsputil mlp_parser"
+motionpixels_decoder_select="dsputil"
 mp1_decoder_select="mpegaudio"
 mp1float_decoder_select="mpegaudio"
 mp2_decoder_select="mpegaudio"
@@ -1728,63 +1797,73 @@
 mp3float_decoder_select="mpegaudio"
 mp3on4_decoder_select="mpegaudio"
 mp3on4float_decoder_select="mpegaudio"
-mpc7_decoder_select="mpegaudiodsp"
-mpc8_decoder_select="mpegaudiodsp"
+mpc7_decoder_select="dsputil mpegaudiodsp"
+mpc8_decoder_select="dsputil mpegaudiodsp"
 mpeg_xvmc_decoder_deps="X11_extensions_XvMClib_h"
-mpeg_xvmc_decoder_select="mpegvideo_decoder"
+mpeg_xvmc_decoder_select="mpeg2video_decoder"
 mpeg1video_decoder_select="error_resilience mpegvideo"
-mpeg1video_encoder_select="aandcttables error_resilience mpegvideoenc"
+mpeg1video_encoder_select="aandcttables mpegvideoenc"
 mpeg2video_decoder_select="error_resilience mpegvideo"
-mpeg2video_encoder_select="aandcttables error_resilience mpegvideoenc"
+mpeg2video_encoder_select="aandcttables mpegvideoenc"
 mpeg4_decoder_select="h263_decoder mpeg4video_parser"
 mpeg4_encoder_select="h263_encoder"
 msmpeg4v1_decoder_select="h263_decoder"
-msmpeg4v1_encoder_select="h263_encoder"
 msmpeg4v2_decoder_select="h263_decoder"
 msmpeg4v2_encoder_select="h263_encoder"
 msmpeg4v3_decoder_select="h263_decoder"
 msmpeg4v3_encoder_select="h263_encoder"
-mss2_decoder_select="vc1_decoder"
+mss2_decoder_select="error_resilience vc1_decoder"
+mxpeg_decoder_select="dsputil hpeldsp"
 nellymoser_decoder_select="mdct sinewin"
 nellymoser_encoder_select="audio_frame_queue mdct sinewin"
-nuv_decoder_select="lzo"
+nuv_decoder_select="dsputil lzo"
 png_decoder_select="zlib"
-png_encoder_select="zlib"
+png_encoder_select="dsputil zlib"
+prores_decoder_select="dsputil"
+prores_encoder_select="dsputil"
 qcelp_decoder_select="lsp"
 qdm2_decoder_select="mdct rdft mpegaudiodsp"
 ra_144_encoder_select="audio_frame_queue lpc"
 ralf_decoder_select="golomb"
-rv10_decoder_select="h263_decoder"
+rtjpeg_decoder_select="dsputil"
+rv10_decoder_select="error_resilience h263_decoder"
 rv10_encoder_select="h263_encoder"
-rv20_decoder_select="h263_decoder"
+rv20_decoder_select="error_resilience h263_decoder"
 rv20_encoder_select="h263_encoder"
-rv30_decoder_select="error_resilience golomb h264chroma h264pred h264qpel mpegvideo"
-rv40_decoder_select="error_resilience golomb h264chroma h264pred h264qpel mpegvideo"
+rv30_decoder_select="error_resilience golomb h264chroma h264pred h264qpel mpegvideo videodsp"
+rv40_decoder_select="error_resilience golomb h264chroma h264pred h264qpel mpegvideo videodsp"
 shorten_decoder_select="golomb"
 sipr_decoder_select="lsp"
-snow_decoder_select="dwt rangecoder"
-snow_encoder_select="aandcttables dwt error_resilience mpegvideoenc rangecoder"
+snow_decoder_select="dsputil dwt h264qpel hpeldsp rangecoder"
+snow_encoder_select="aandcttables dsputil dwt h264qpel hpeldsp mpegvideoenc rangecoder"
 sonic_decoder_select="golomb"
 sonic_encoder_select="golomb"
 sonic_ls_encoder_select="golomb"
-svq1_decoder_select="error_resilience mpegvideo"
-svq1_encoder_select="aandcttables error_resilience mpegvideoenc"
-svq3_decoder_select="error_resilience golomb h264chroma h264dsp h264pred h264qpel mpegvideo"
-svq3_decoder_suggest="zlib"
+sp5x_decoder_select="dsputil hpeldsp"
+svq1_decoder_select="hpeldsp"
+svq1_encoder_select="aandcttables dsputil hpeldsp mpegvideoenc"
+svq3_decoder_select="golomb h264chroma h264dsp h264pred h264qpel hpeldsp mpegvideo videodsp"
+svq3_decoder_suggest="error_resilience zlib"
+tak_decoder_select="dsputil"
 theora_decoder_select="vp3_decoder"
 tiff_decoder_suggest="zlib"
 tiff_encoder_suggest="zlib"
+thp_decoder_select="dsputil hpeldsp"
 truehd_decoder_select="mlp_parser"
+truemotion2_decoder_select="dsputil"
+truespeech_decoder_select="dsputil"
 tscc_decoder_select="zlib"
 twinvq_decoder_select="mdct lsp sinewin"
-utvideo_encoder_select="huffman"
-vc1_decoder_select="h263_decoder h264chroma h264qpel"
+utvideo_decoder_select="dsputil"
+utvideo_encoder_select="dsputil huffman"
+vble_decoder_select="dsputil"
+vc1_decoder_select="error_resilience h263_decoder h264chroma h264qpel"
 vc1image_decoder_select="vc1_decoder"
 vorbis_decoder_select="mdct"
 vorbis_encoder_select="mdct"
-vp3_decoder_select="vp3dsp videodsp"
-vp5_decoder_select="vp3dsp videodsp"
-vp6_decoder_select="huffman vp3dsp videodsp"
+vp3_decoder_select="hpeldsp vp3dsp videodsp"
+vp5_decoder_select="h264chroma hpeldsp videodsp vp3dsp"
+vp6_decoder_select="h264chroma hpeldsp huffman videodsp vp3dsp"
 vp6a_decoder_select="vp6_decoder"
 vp6f_decoder_select="vp6_decoder"
 vp8_decoder_select="h264pred videodsp"
@@ -1796,7 +1875,7 @@
 wmavoice_decoder_select="lsp rdft dct mdct sinewin"
 wmv1_decoder_select="h263_decoder"
 wmv1_encoder_select="h263_encoder"
-wmv2_decoder_select="h263_decoder"
+wmv2_decoder_select="h263_decoder videodsp"
 wmv2_encoder_select="h263_encoder"
 wmv3_decoder_select="vc1_decoder"
 wmv3image_decoder_select="wmv3_decoder"
@@ -1832,7 +1911,7 @@
 h264_vdpau_hwaccel_deps="vdpau"
 h264_vdpau_hwaccel_select="h264_decoder"
 mpeg_vdpau_decoder_deps="vdpau"
-mpeg_vdpau_decoder_select="mpegvideo_decoder"
+mpeg_vdpau_decoder_select="mpeg2video_decoder"
 mpeg1_vdpau_decoder_deps="vdpau"
 mpeg1_vdpau_decoder_select="mpeg1video_decoder"
 mpeg1_vdpau_hwaccel_deps="vdpau"
@@ -1868,10 +1947,10 @@
 wmv3_vdpau_hwaccel_select="vc1_vdpau_hwaccel"
 
 # parsers
-h264_parser_select="error_resilience golomb h264chroma h264dsp h264pred h264qpel mpegvideo videodsp"
+h264_parser_select="golomb h264chroma h264dsp h264pred h264qpel videodsp"
 mpeg4video_parser_select="error_resilience mpegvideo"
 mpegvideo_parser_select="error_resilience mpegvideo"
-vc1_parser_select="error_resilience mpegvideo"
+vc1_parser_select="mpegvideo"
 
 # external libraries
 libaacplus_encoder_deps="libaacplus"
@@ -1898,8 +1977,11 @@
 libopus_decoder_deps="libopus"
 libopus_encoder_deps="libopus"
 libopus_encoder_select="audio_frame_queue"
+libquvi_demuxer_deps="libquvi"
 libschroedinger_decoder_deps="libschroedinger"
 libschroedinger_encoder_deps="libschroedinger"
+libshine_encoder_deps="libshine"
+libshine_encoder_select="audio_frame_queue"
 libspeex_decoder_deps="libspeex"
 libspeex_encoder_deps="libspeex"
 libspeex_encoder_select="audio_frame_queue"
@@ -1943,13 +2025,12 @@
 mov_muxer_select="rtpenc_chain"
 mp3_demuxer_select="mpegaudio_parser"
 mp4_muxer_select="mov_muxer"
-mpegts_muxer_select="adts_muxer latm_muxer mpegvideo"
+mpegts_muxer_select="adts_muxer latm_muxer"
 mpegtsraw_demuxer_select="mpegts_demuxer"
 mxf_d10_muxer_select="mxf_muxer"
 ogg_demuxer_select="golomb"
 psp_muxer_select="mov_muxer"
 rtp_demuxer_select="sdp_demuxer"
-rtp_muxer_select="mpegvideo"
 rtpdec_select="asf_demuxer rm_demuxer rtp_protocol mpegts_demuxer mov_demuxer"
 rtsp_demuxer_select="http_protocol rtpdec"
 rtsp_muxer_select="rtp_muxer http_protocol rtp_protocol rtpenc_chain"
@@ -2039,9 +2120,9 @@
 boxblur_filter_deps="gpl"
 colormatrix_filter_deps="gpl"
 cropdetect_filter_deps="gpl"
-decimate_filter_deps="gpl avcodec"
 delogo_filter_deps="gpl"
 deshake_filter_deps="avcodec"
+deshake_filter_select="dsputil"
 drawtext_filter_deps="libfreetype"
 ebur128_filter_deps="gpl"
 flite_filter_deps="libflite"
@@ -2053,9 +2134,11 @@
 histeq_filter_deps="gpl"
 hqdn3d_filter_deps="gpl"
 hue_filter_deps="gpl"
+interlace_filter_deps="gpl"
 kerndeint_filter_deps="gpl"
 movie_filter_deps="avcodec avformat"
 mp_filter_deps="gpl avcodec swscale inline_asm"
+mpdecimate_filter_deps="gpl avcodec"
 mptestsrc_filter_deps="gpl"
 negate_filter_deps="lut_filter"
 noise_filter_deps="gpl"
@@ -2067,9 +2150,12 @@
 scale_filter_deps="swscale"
 smartblur_filter_deps="gpl swscale"
 showspectrum_filter_deps="avcodec rdft"
+stereo3d_filter_deps="gpl"
 subtitles_filter_deps="avformat avcodec libass"
 super2xsai_filter_deps="gpl"
 tinterlace_filter_deps="gpl"
+vidstabdetect_filter_deps="libvidstab"
+vidstabtransform_filter_deps="libvidstab"
 yadif_filter_deps="gpl"
 pixfmts_super2xsai_test_deps="super2xsai_filter"
 tinterlace_merge_test_deps="tinterlace_filter"
@@ -2086,10 +2172,10 @@
 
 # programs
 ffmpeg_deps="avcodec avfilter avformat swscale swresample"
-ffmpeg_select="ffbuffersink_filter format_filter aformat_filter
-               setpts_filter null_filter anull_filter ffabuffersink_filter"
+ffmpeg_select="format_filter aformat_filter
+               setpts_filter null_filter anull_filter"
 ffplay_deps="avcodec avformat swscale swresample sdl"
-ffplay_select="ffbuffersink_filter rdft crop_filter"
+ffplay_select="rdft crop_filter"
 ffprobe_deps="avcodec avformat"
 ffserver_deps="avformat ffm_muxer fork rtp_protocol rtsp_demuxer"
 ffserver_extralibs='$ldl'
@@ -2154,6 +2240,9 @@
 enable static
 enable swscale_alpha
 
+# By default, enable only those hwaccels that have no external dependencies.
+enable dxva2 vdpau
+
 # build settings
 SHFLAGS='-shared -Wl,-soname,$$(@F)'
 FFSERVERLDFLAGS=-Wl,-E
@@ -2401,13 +2490,13 @@
 case "$toolchain" in
     clang-asan)
         cc_default="clang"
-        add_cflags  -faddress-sanitizer
-        add_ldflags -faddress-sanitizer
+        add_cflags  -fsanitize=address
+        add_ldflags -fsanitize=address
     ;;
     clang-tsan)
         cc_default="clang"
-        add_cflags  -fthread-sanitizer
-        add_ldflags -fthread-sanitizer
+        add_cflags  -fsanitize=thread -pie
+        add_ldflags -fsanitize=thread -pie
     ;;
     msvc)
         cc_default="c99wrap cl"
@@ -2416,6 +2505,10 @@
         ar_default="lib"
         target_os_default="win32"
     ;;
+    gcov)
+        add_cflags  -fprofile-arcs -ftest-coverage
+        add_ldflags -fprofile-arcs -ftest-coverage
+    ;;
     ?*)
         die "Unknown toolchain $toolchain"
     ;;
@@ -2717,7 +2810,7 @@
         opt_common='-alias=ansi -Mdse -Mlre -Mpre'
         _cflags_speed="-O3 -Mautoinline -Munroll=c:4 $opt_common"
         _cflags_size="-O2 -Munroll=c:1 $opt_common"
-        _cflags_noopt="-O1"
+        _cflags_noopt="-O"
         _flags_filter=pgi_flags
     elif $_cc 2>&1 | grep -q Microsoft; then
         _type=msvc
@@ -3484,6 +3577,7 @@
 
 die_license_disabled gpl libcdio
 die_license_disabled gpl libutvideo
+die_license_disabled gpl libvidstab
 die_license_disabled gpl libx264
 die_license_disabled gpl libxavs
 die_license_disabled gpl libxvid
@@ -3640,9 +3734,8 @@
 
 elif enabled x86; then
 
-    check_code ld intrin.h "__rdtsc()" "cc" && enable rdtsc
-
-    check_code ld mmintrin.h "_mm_empty()" "cc" && enable mm_empty
+    check_builtin rdtsc    intrin.h   "__rdtsc()"
+    check_builtin mm_empty mmintrin.h "_mm_empty()"
 
     enable local_aligned_8 local_aligned_16
 
@@ -3778,6 +3871,10 @@
 check_func  setrlimit
 check_func  strerror_r
 check_func  sched_getaffinity
+check_builtin sync_val_compare_and_swap "" "int *ptr; int oldval, newval; __sync_val_compare_and_swap(ptr, oldval, newval)"
+check_builtin machine_rw_barrier mbarrier.h "__machine_rw_barrier()"
+check_builtin atomic_cas_ptr atomic.h "void **ptr; void *oldval, *newval; atomic_cas_ptr(ptr, oldval, newval)"
+check_builtin MemoryBarrier windows.h "MemoryBarrier()"
 check_func  sysconf
 check_func  sysctl
 check_func  usleep
@@ -3821,7 +3918,6 @@
 
 disabled  zlib || check_lib   zlib.h      zlibVersion -lz   || disable  zlib
 disabled bzlib || check_lib2 bzlib.h BZ2_bzlibVersion -lbz2 || disable bzlib
-disabled iconv || check_func_headers iconv.h iconv || check_lib2 iconv.h iconv -liconv || disable iconv
 
 if ! disabled w32threads && ! enabled pthreads; then
     check_func_headers "windows.h process.h" _beginthreadex && enable w32threads
@@ -3858,6 +3954,10 @@
   check_func pthread_cancel
 fi
 
+enabled sync_val_compare_and_swap && enable atomics_gcc
+enabled machine_rw_barrier && enabled atomic_cas_ptr && enable atomics_suncc
+enabled MemoryBarrier && enable atomics_win32
+
 check_lib math.h sin -lm && LIBM="-lm"
 disabled crystalhd || check_lib libcrystalhd/libcrystalhd_if.h DtsCrystalHDVersion -lcrystalhd || disable crystalhd
 enabled vaapi && require vaapi va/va.h vaInitialize -lva
@@ -3871,7 +3971,9 @@
 done
 
 # these are off by default, so fail if requested and not available
-enabled avisynth   && require2 vfw32 "windows.h vfw.h" AVIFileInit -lavifil32
+enabled avisynth   && { { check_lib2 "windows.h" LoadLibrary; } ||
+                        { check_lib2 "dlfcn.h" dlopen -ldl; } ||
+                        die "ERROR: LoadLibrary/dlopen not found for avisynth"; }
 enabled fontconfig && require_pkg_config fontconfig "fontconfig/fontconfig.h" FcInit
 enabled frei0r     && { check_header frei0r.h || die "ERROR: frei0r.h header not found"; }
 enabled gnutls     && require_pkg_config gnutls gnutls/gnutls.h gnutls_global_init
@@ -3903,8 +4005,10 @@
                          die "ERROR: libopenjpeg not found"; }
 enabled libopus    && require_pkg_config opus opus_multistream.h opus_multistream_decoder_create
 enabled libpulse && require_pkg_config libpulse-simple pulse/simple.h pa_simple_new
+enabled libquvi    && require_pkg_config libquvi quvi/quvi.h quvi_init
 enabled librtmp    && require_pkg_config librtmp librtmp/rtmp.h RTMP_Socket
 enabled libschroedinger && require_pkg_config schroedinger-1.0 schroedinger/schro.h schro_init
+enabled libshine   && require_pkg_config shine shine/layer3.h shine_encode_frame
 enabled libsoxr    && require  libsoxr soxr.h soxr_create -lsoxr
 enabled libspeex   && require  libspeex speex/speex.h speex_decoder_init -lspeex
 enabled libstagefright_h264  && require_cpp libstagefright_h264 "binder/ProcessState.h media/stagefright/MetaData.h
@@ -3916,6 +4020,7 @@
                         die "ERROR: libtwolame must be installed and version must be >= 0.3.10"; }
 enabled libutvideo    && require_cpp utvideo "stdint.h stdlib.h utvideo/utvideo.h utvideo/Codec.h" 'CCodec*' -lutvideo -lstdc++
 enabled libv4l2    && require_pkg_config libv4l2 libv4l2.h v4l2_ioctl
+enabled libvidstab && require_pkg_config vidstab vid.stab/libvidstab.h vsMotionDetectInit
 enabled libvo_aacenc && require libvo_aacenc vo-aacenc/voAAC.h voGetAACEncAPI -lvo-aacenc
 enabled libvo_amrwbenc && require libvo_amrwbenc vo-amrwbenc/enc_if.h E_IF_init -lvo-amrwbenc
 enabled libvorbis  && require  libvorbis vorbis/vorbisenc.h vorbis_info_init -lvorbisenc -lvorbis -logg
@@ -3936,6 +4041,7 @@
                         die "ERROR: openal not found"; } &&
                       { check_cpp_condition "AL/al.h" "defined(AL_VERSION_1_1)" ||
                         die "ERROR: openal must be installed and version must be 1.1 or compatible"; }
+enabled opencl     && require2 opencl CL/cl.h clEnqueueNDRangeKernel -lOpenCL
 enabled openssl    && { check_lib openssl/ssl.h SSL_library_init -lssl -lcrypto ||
                         check_lib openssl/ssl.h SSL_library_init -lssl32 -leay32 ||
                         check_lib openssl/ssl.h SSL_library_init -lssl -lcrypto -lws2_32 -lgdi32 ||
@@ -3958,16 +4064,16 @@
 SDL_CONFIG="${cross_prefix}sdl-config"
 if check_pkg_config sdl SDL_events.h SDL_PollEvent; then
     check_cpp_condition SDL.h "(SDL_MAJOR_VERSION<<16 | SDL_MINOR_VERSION<<8 | SDL_PATCHLEVEL) >= 0x010201" $sdl_cflags &&
-    enable sdl &&
-    check_struct SDL.h SDL_VideoInfo current_w $sdl_cflags && enable sdl_video_size
+    check_cpp_condition SDL.h "(SDL_MAJOR_VERSION<<16 | SDL_MINOR_VERSION<<8 | SDL_PATCHLEVEL) < 0x010300" $sdl_cflags &&
+    enable sdl
 else
   if "${SDL_CONFIG}" --version > /dev/null 2>&1; then
     sdl_cflags=$("${SDL_CONFIG}" --cflags)
     sdl_libs=$("${SDL_CONFIG}" --libs)
     check_func_headers SDL_version.h SDL_Linked_Version $sdl_cflags $sdl_libs &&
     check_cpp_condition SDL.h "(SDL_MAJOR_VERSION<<16 | SDL_MINOR_VERSION<<8 | SDL_PATCHLEVEL) >= 0x010201" $sdl_cflags &&
-    enable sdl &&
-    check_struct SDL.h SDL_VideoInfo current_w $sdl_cflags && enable sdl_video_size
+    check_cpp_condition SDL.h "(SDL_MAJOR_VERSION<<16 | SDL_MINOR_VERSION<<8 | SDL_PATCHLEVEL) < 0x010300" $sdl_cflags &&
+    enable sdl
   fi
 fi
 enabled sdl && add_cflags $sdl_cflags && add_extralibs $sdl_libs
@@ -4039,8 +4145,10 @@
     check_cpp_condition vdpau/vdpau.h "defined VDP_DECODER_PROFILE_MPEG4_PART2_ASP" ||
     disable vdpau
 
+# Funny iconv installations are not unusual, so check it after all flags have been set
+disabled iconv || check_func_headers iconv.h iconv || check_lib2 iconv.h iconv -liconv || disable iconv
+
 enabled debug && add_cflags -g"$debuglevel" && add_asflags -g"$debuglevel"
-enabled coverage && add_cflags "-fprofile-arcs -ftest-coverage" && add_ldflags "-fprofile-arcs -ftest-coverage"
 test -n "$valgrind" && target_exec="$valgrind --error-exitcode=1 --malloc-fill=0x2a --track-origins=yes --leak-check=full --gen-suppressions=all --suppressions=$source_path/tests/fate-valgrind.supp"
 
 # add some useful compiler flags if supported
@@ -4191,7 +4299,9 @@
     ;;
 esac
 
+enable frame_thread_encoder
 enabled_any $THREADS_LIST      && enable threads
+enabled_any $ATOMICS_LIST      && enable atomics_native
 
 enabled asm || { arch=c; disable $ARCH_LIST $ARCH_EXT_LIST; }
 
@@ -4300,6 +4410,8 @@
 echo "threading support         ${thread_type-no}"
 echo "safe bitstream reader     ${safe_bitstream_reader-no}"
 echo "SDL support               ${sdl-no}"
+echo "opencl enabled            ${opencl-no}"
+echo "libshine enabled          ${libshine-no}"
 echo "texi2html enabled         ${texi2html-no}"
 echo "perl enabled              ${perl-no}"
 echo "pod2man enabled           ${pod2man-no}"
@@ -4332,13 +4444,13 @@
 
 echo "License: $license"
 
-echo "Creating config.mak and config.h..."
+echo "Creating config.mak, config.h, and doc/config.texi..."
 
 test -e Makefile || $ln_s "$source_path/Makefile" .
 
 enabled stripping || strip="echo skipping strip"
 
-config_files="$TMPH config.mak"
+config_files="$TMPH config.mak doc/config.texi"
 
 cat > config.mak <<EOF
 # Automatically generated by configure - do not modify!
@@ -4490,6 +4602,9 @@
     printf '' >$TMPASM
 fi
 
+mkdir -p doc
+echo "@c auto-generated by configure" > doc/config.texi
+
 print_config ARCH_   "$config_files" $ARCH_LIST
 print_config HAVE_   "$config_files" $HAVE_LIST
 print_config CONFIG_ "$config_files" $CONFIG_LIST       \
diff --git a/doc/APIchanges b/doc/APIchanges
index 8535d9f..cd7c4ca 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -2,10 +2,10 @@
 since the last major version increase or the API was added.
 
 The last version increases were:
-libavcodec:    2012-01-27
-libavdevice:   2011-04-18
+libavcodec:    2013-03-xx
+libavdevice:   2013-03-xx
 libavfilter:   2012-06-22
-libavformat:   2012-01-27
+libavformat:   2013-03-xx
 libavresample: 2012-10-05
 libpostproc:   2011-04-18
 libswresample: 2011-09-19
@@ -15,6 +15,32 @@
 
 API changes, most recent first:
 
+2013-04-19 - xxxxxxx - lavc 55.4.100
+  Add AV_CODEC_PROP_TEXT_SUB property for text based subtitles codec.
+
+2013-04-18 - xxxxxxx - lavf 55.3.100
+  The matroska demuxer can now output proper verbatim ASS packets. It will
+  become the default starting lavf 56.0.100.
+
+2013-04-10 - xxxxxxx - lavu 25.26.100 - avutil.h,opt.h
+  Add av_int_list_length()
+  and av_opt_set_int_list().
+
+2013-03-30 - xxxxxxx - lavu 52.24.100 - samplefmt.h
+  Add av_samples_alloc_array_and_samples().
+
+2013-03-29 - xxxxxxx - lavf 55.1.100 - avformat.h
+  Add av_guess_frame_rate()
+
+2013-03-20 - xxxxxxx - lavu 52.22.100 - opt.h
+  Add AV_OPT_TYPE_DURATION value to AVOptionType enum.
+
+2013-03-17 - xxxxxx - lavu 52.20.100 - opt.h
+  Add AV_OPT_TYPE_VIDEO_RATE value to AVOptionType enum.
+
+2013-03-07 - xxxxxx - lavu 52.18.100 - avstring.h,bprint.h
+  Add av_escape() and av_bprint_escape() API.
+
 2013-02-24 - xxxxxx - lavfi 3.41.100 - buffersink.h
   Add sample_rates field to AVABufferSinkParams.
 
@@ -141,6 +167,72 @@
 2012-03-26 - a67d9cf - lavfi 2.66.100
   Add avfilter_fill_frame_from_{audio_,}buffer_ref() functions.
 
+2013-03-xx - xxxxxxx - lavc 55.2.0 - avcodec.h
+  Add CODEC_FLAG_UNALIGNED to allow decoders to produce unaligned output.
+
+2013-xx-xx - lavfi 3.8.0
+  Move all content from avfiltergraph.h to avfilter.h. Deprecate
+  avfilterhraph.h, user applications should include just avfilter.h
+  Add avfilter_graph_alloc_filter(), deprecate avfilter_open() and
+  avfilter_graph_add_filter().
+  Add AVFilterContext.graph pointing to the AVFilterGraph that contains the
+  filter.
+  Add avfilter_init_str(), deprecate avfilter_init_filter().
+  Add avfilter_init_dict().
+  Add AVFilter.flags field and AVFILTER_FLAG_DYNAMIC_{INPUTS,OUTPUTS} flags.
+  Add avfilter_pad_count() for counting filter inputs/outputs.
+  Add avfilter_next(), deprecate av_filter_next().
+  Deprecate avfilter_uninit().
+
+2013-xx-xx - lavfi 3.7.0 - avfilter.h
+  Add AVFilter.priv_class for exporting filter options through the AVOptions API
+  in the similar way private options work in lavc and lavf.
+  Add avfilter_get_class().
+  Switch all filters to use AVOptions.
+
+2013-xx-xx - lavu 52.9.0 - pixdesc.h
+  Add av_pix_fmt_count_planes() function for counting planes in a pixel format.
+
+2013-xx-xx - lavfi 3.6.0
+  Add AVFilterGraph.nb_filters, deprecate AVFilterGraph.filter_count.
+
+2013-03-xx - Reference counted buffers - lavu 52.8.0, lavc 55.0.0, lavf 55.0.0,
+lavd 54.0.0, lavfi 3.5.0
+  xxxxxxx, xxxxxxx - add a new API for reference counted buffers and buffer
+                     pools (new header libavutil/buffer.h).
+  xxxxxxx - add AVPacket.buf to allow reference counting for the AVPacket data.
+            Add av_packet_from_data() function for constructing packets from
+            av_malloc()ed data.
+  xxxxxxx - move AVFrame from lavc to lavu (new header libavutil/frame.h), add
+            AVFrame.buf/extended_buf to allow reference counting for the AVFrame
+            data. Add new API for working with reference-counted AVFrames.
+  xxxxxxx - add the refcounted_frames field to AVCodecContext to make audio and
+            video decoders return reference-counted frames. Add get_buffer2()
+            callback to AVCodecContext which allocates reference-counted frames.
+            Add avcodec_default_get_buffer2() as the default get_buffer2()
+            implementation.
+            Deprecate AVCodecContext.get_buffer() / release_buffer() /
+            reget_buffer(), avcodec_default_get_buffer(),
+            avcodec_default_reget_buffer(), avcodec_default_release_buffer().
+            Remove avcodec_default_free_buffers(), which should not have ever
+            been called from outside of lavc.
+            Deprecate the following AVFrame fields:
+                * base -- is now stored in AVBufferRef
+                * reference, type, buffer_hints -- are unnecessary in the new API
+                * hwaccel_picture_private, owner, thread_opaque -- should not
+                  have been acessed from outside of lavc
+                * qscale_table, qstride, qscale_type, mbskip_table, motion_val,
+                  mb_type, dct_coeff, ref_index -- mpegvideo-specific tables,
+                  which are not exported anymore.
+  xxxxxxx - switch libavfilter to use AVFrame instead of AVFilterBufferRef. Add
+            av_buffersrc_add_frame(), deprecate av_buffersrc_buffer().
+            Add av_buffersink_get_frame() and av_buffersink_get_samples(),
+            deprecate av_buffersink_read() and av_buffersink_read_samples().
+            Deprecate AVFilterBufferRef and all functions for working with it.
+
+2013-xx-xx - xxxxxxx - lavu 52.8.0 - avstring.h
+  Add av_isdigit, av_isgraph, av_isspace, av_isxdigit.
+
 2013-xx-xx - xxxxxxx - lavfi 3.4.0 - avfiltergraph.h
   Add resample_lavr_opts to AVFilterGraph for setting libavresample options
   for auto-inserted resample filters.
diff --git a/doc/Doxyfile b/doc/Doxyfile
index 7e6d0f5..faaa462 100644
--- a/doc/Doxyfile
+++ b/doc/Doxyfile
@@ -709,7 +709,7 @@
 # doxygen to hide any special comment blocks from generated source code
 # fragments. Normal C and C++ comments will always remain visible.
 
-STRIP_CODE_COMMENTS    = YES
+STRIP_CODE_COMMENTS    = NO
 
 # If the REFERENCED_BY_RELATION tag is set to YES
 # then for each documented function all documented
diff --git a/doc/Makefile b/doc/Makefile
index a861655..9189c7b 100644
--- a/doc/Makefile
+++ b/doc/Makefile
@@ -6,7 +6,6 @@
 LIBRARIES-$(CONFIG_AVDEVICE)   += libavdevice
 LIBRARIES-$(CONFIG_AVFILTER)   += libavfilter
 
-COMPONENTS-yes = $(PROGS-yes)
 COMPONENTS-$(CONFIG_AVUTIL)     += ffmpeg-utils
 COMPONENTS-$(CONFIG_SWSCALE)    += ffmpeg-scaler
 COMPONENTS-$(CONFIG_SWRESAMPLE) += ffmpeg-resampler
@@ -15,9 +14,9 @@
 COMPONENTS-$(CONFIG_AVDEVICE)   += ffmpeg-devices
 COMPONENTS-$(CONFIG_AVFILTER)   += ffmpeg-filters
 
-MANPAGES    = $(COMPONENTS-yes:%=doc/%.1)    $(LIBRARIES-yes:%=doc/%.3)
-PODPAGES    = $(COMPONENTS-yes:%=doc/%.pod)  $(LIBRARIES-yes:%=doc/%.pod)
-HTMLPAGES   = $(COMPONENTS-yes:%=doc/%.html) $(LIBRARIES-yes:%=doc/%.html)     \
+MANPAGES    = $(PROGS-yes:%=doc/%.1)    $(PROGS-yes:%=doc/%-all.1)    $(COMPONENTS-yes:%=doc/%.1)    $(LIBRARIES-yes:%=doc/%.3)
+PODPAGES    = $(PROGS-yes:%=doc/%.pod)  $(PROGS-yes:%=doc/%-all.pod)  $(COMPONENTS-yes:%=doc/%.pod)  $(LIBRARIES-yes:%=doc/%.pod)
+HTMLPAGES   = $(PROGS-yes:%=doc/%.html) $(PROGS-yes:%=doc/%-all.html) $(COMPONENTS-yes:%=doc/%.html) $(LIBRARIES-yes:%=doc/%.html) \
               doc/developer.html                                        \
               doc/faq.html                                              \
               doc/fate.html                                             \
@@ -59,12 +58,22 @@
 doc/%.html: TAG = HTML
 doc/%.html: doc/%.texi $(SRC_PATH)/doc/t2h.init $(GENTEXI)
 	$(Q)$(TEXIDEP)
-	$(M)texi2html -I doc -monolithic --init-file $(SRC_PATH)/doc/t2h.init --output $@ $<
+	$(M)texi2html -I doc -monolithic --D=config-not-all --init-file $(SRC_PATH)/doc/t2h.init --output $@ $<
+
+doc/%-all.html: TAG = HTML
+doc/%-all.html: doc/%.texi $(SRC_PATH)/doc/t2h.init $(GENTEXI)
+	$(Q)$(TEXIDEP)
+	$(M)texi2html -I doc -monolithic --D=config-all --init-file $(SRC_PATH)/doc/t2h.init --output $@ $<
 
 doc/%.pod: TAG = POD
 doc/%.pod: doc/%.texi $(SRC_PATH)/doc/texi2pod.pl $(GENTEXI)
 	$(Q)$(TEXIDEP)
-	$(M)perl $(SRC_PATH)/doc/texi2pod.pl -Idoc $< $@
+	$(M)perl $(SRC_PATH)/doc/texi2pod.pl -Dconfig-not-all=yes -Idoc $< $@
+
+doc/%-all.pod: TAG = POD
+doc/%-all.pod: doc/%.texi $(SRC_PATH)/doc/texi2pod.pl $(GENTEXI)
+	$(Q)$(TEXIDEP)
+	$(M)perl $(SRC_PATH)/doc/texi2pod.pl -Dconfig-all=yes -Idoc $< $@
 
 doc/%.1 doc/%.3: TAG = MAN
 doc/%.1: doc/%.pod $(GENTEXI)
@@ -94,6 +103,9 @@
 
 clean:: docclean
 
+distclean:: docclean
+	$(RM) doc/config.texi
+
 docclean:
 	$(RM) $(TXTPAGES) doc/*.html doc/*.pod doc/*.1 doc/*.3 $(CLEANSUFFIXES:%=doc/%) doc/avoptions_*.texi
 	$(RM) -r doc/doxy/html
diff --git a/doc/RELEASE_NOTES b/doc/RELEASE_NOTES
index f71f18a3..2faf40d 100644
--- a/doc/RELEASE_NOTES
+++ b/doc/RELEASE_NOTES
@@ -1,7 +1,7 @@
 Release Notes
 =============
 
-* 0.10 "Freedom"  January, 2012
+* 1.2 "Magic"  March, 2013
 
 
 General notes
@@ -14,9 +14,3 @@
 FFmpeg, please try git master to check if the issue still exists. If it does,
 make your report against the development code following the usual bug reporting
 guidelines.
-
-Of big interest to our Windows users, FFmpeg now supports building with the MSVC
-compiler. Since MSVC does not support C99 features used extensively by FFmpeg,
-this has been accomplished using a converter that turns C99 code to C89. See the
-platform-specific documentation for more detailed documentation on building
-FFmpeg with MSVC.
diff --git a/doc/all-components.texi b/doc/all-components.texi
new file mode 100644
index 0000000..177cf10
--- /dev/null
+++ b/doc/all-components.texi
@@ -0,0 +1,31 @@
+@include config.texi
+
+@ifset config-avutil
+@include utils.texi
+@end ifset
+
+@ifset config-avcodec
+@include codecs.texi
+@include bitstream_filters.texi
+@end ifset
+
+@ifset config-avformat
+@include formats.texi
+@include protocols.texi
+@end ifset
+
+@ifset config-avdevice
+@include devices.texi
+@end ifset
+
+@ifset config-swresample
+@include resampler.texi
+@end ifset
+
+@ifset config-swscale
+@include scaler.texi
+@end ifset
+
+@ifset config-avfilter
+@include filters.texi
+@end ifset
diff --git a/doc/avtools-common-opts.texi b/doc/avtools-common-opts.texi
index d9d0bd0..3892a4a 100644
--- a/doc/avtools-common-opts.texi
+++ b/doc/avtools-common-opts.texi
@@ -1,32 +1,33 @@
-All the numerical options, if not specified otherwise, accept in input
-a string representing a number, which may contain one of the
-SI unit prefixes, for example 'K', 'M', 'G'.
-If 'i' is appended after the prefix, binary prefixes are used,
-which are based on powers of 1024 instead of powers of 1000.
-The 'B' postfix multiplies the value by 8, and can be
-appended after a unit prefix or used alone. This allows using for
-example 'KB', 'MiB', 'G' and 'B' as number postfix.
+All the numerical options, if not specified otherwise, accept a string
+representing a number as input, which may be followed by one of the SI
+unit prefixes, for example: 'K', 'M', or 'G'.
+
+If 'i' is appended to the SI unit prefix, the complete prefix will be
+interpreted as a unit prefix for binary multiplies, which are based on
+powers of 1024 instead of powers of 1000. Appending 'B' to the SI unit
+prefix multiplies the value by 8. This allows using, for example:
+'KB', 'MiB', 'G' and 'B' as number suffixes.
 
 Options which do not take arguments are boolean options, and set the
 corresponding value to true. They can be set to false by prefixing
-with "no" the option name, for example using "-nofoo" in the
-command line will set to false the boolean option with name "foo".
+the option name with "no". For example using "-nofoo"
+will set the boolean option with name "foo" to false.
 
 @anchor{Stream specifiers}
 @section Stream specifiers
 Some options are applied per-stream, e.g. bitrate or codec. Stream specifiers
-are used to precisely specify which stream(s) does a given option belong to.
+are used to precisely specify which stream(s) a given option belongs to.
 
 A stream specifier is a string generally appended to the option name and
-separated from it by a colon. E.g. @code{-codec:a:1 ac3} option contains
-@code{a:1} stream specifier, which matches the second audio stream. Therefore it
+separated from it by a colon. E.g. @code{-codec:a:1 ac3} contains the
+@code{a:1} stream specifier, which matches the second audio stream. Therefore, it
 would select the ac3 codec for the second audio stream.
 
-A stream specifier can match several streams, the option is then applied to all
+A stream specifier can match several streams, so that the option is applied to all
 of them. E.g. the stream specifier in @code{-b:a 128k} matches all audio
 streams.
 
-An empty stream specifier matches all streams, for example @code{-codec copy}
+An empty stream specifier matches all streams. For example, @code{-codec copy}
 or @code{-codec: copy} would copy all the streams without reencoding.
 
 Possible forms of stream specifiers are:
@@ -35,20 +36,21 @@
 Matches the stream with this index. E.g. @code{-threads:1 4} would set the
 thread count for the second stream to 4.
 @item @var{stream_type}[:@var{stream_index}]
-@var{stream_type} is one of: 'v' for video, 'a' for audio, 's' for subtitle,
-'d' for data and 't' for attachments. If @var{stream_index} is given, then
-matches stream number @var{stream_index} of this type. Otherwise matches all
+@var{stream_type} is one of following: 'v' for video, 'a' for audio, 's' for subtitle,
+'d' for data, and 't' for attachments. If @var{stream_index} is given, then it matches
+stream number @var{stream_index} of this type. Otherwise, it matches all
 streams of this type.
 @item p:@var{program_id}[:@var{stream_index}]
-If @var{stream_index} is given, then matches stream number @var{stream_index} in
-program with id @var{program_id}. Otherwise matches all streams in this program.
+If @var{stream_index} is given, then it matches the stream with number @var{stream_index}
+in the program with the id @var{program_id}. Otherwise, it matches all streams in the
+program.
 @item #@var{stream_id}
-Matches the stream by format-specific ID.
+Matches the stream by a format-specific ID.
 @end table
 
 @section Generic options
 
-These options are shared amongst the av* tools.
+These options are shared amongst the ff* tools.
 
 @table @option
 
@@ -77,6 +79,10 @@
 Print detailed information about the muxer named @var{muxer_name}. Use the
 @option{-formats} option to get a list of all muxers and demuxers.
 
+@item filter=@var{filter_name}
+Print detailed information about the filter name @var{filter_name}. Use the
+@option{-filters} option to get a list of all filters.
+
 @end table
 
 @item -version
@@ -85,14 +91,6 @@
 @item -formats
 Show available formats.
 
-The fields preceding the format names have the following meanings:
-@table @samp
-@item D
-Decoding available
-@item E
-Encoding available
-@end table
-
 @item -codecs
 Show all codecs known to libavcodec.
 
@@ -123,18 +121,36 @@
 @item -layouts
 Show channel names and standard channel layouts.
 
-@item -loglevel @var{loglevel} | -v @var{loglevel}
+@item -loglevel [repeat+]@var{loglevel} | -v [repeat+]@var{loglevel}
 Set the logging level used by the library.
+Adding "repeat+" indicates that repeated log output should not be compressed
+to the first line and the "Last message repeated n times" line will be
+omitted. "repeat" can also be used alone.
+If "repeat" is used alone, and with no prior loglevel set, the default
+loglevel will be used. If multiple loglevel parameters are given, using
+'repeat' will not change the loglevel.
 @var{loglevel} is a number or a string containing one of the following values:
 @table @samp
 @item quiet
+Show nothing at all; be silent.
 @item panic
+Only show fatal errors which could lead the process to crash, such as
+and assert failure. This is not currently used for anything.
 @item fatal
+Only show fatal errors. These are errors after which the process absolutely
+cannot continue after.
 @item error
+Show all errors, including ones which can be recovered from.
 @item warning
+Show all warnings and errors. Any message related to possibly
+incorrect or unexpected events will be shown.
 @item info
+Show informative messages during processing. This is in addition to
+warnings and errors. This is the default value.
 @item verbose
+Same as @code{info}, except more verbose.
 @item debug
+Show everything, including debugging information.
 @end table
 
 By default the program logs to stderr, if coloring is supported by the
@@ -176,7 +192,61 @@
 ffmpeg -cpuflags mmx ...
 ffmpeg -cpuflags 0 ...
 @end example
+Possible flags for this option are:
+@table @samp
+@item x86
+@table @samp
+@item mmx
+@item mmxext
+@item sse
+@item sse2
+@item sse2slow
+@item sse3
+@item sse3slow
+@item ssse3
+@item atom
+@item sse4.1
+@item sse4.2
+@item avx
+@item xop
+@item fma4
+@item 3dnow
+@item 3dnowext
+@item cmov
+@end table
+@item ARM
+@table @samp
+@item armv5te
+@item armv6
+@item armv6t2
+@item vfp
+@item vfpv3
+@item neon
+@end table
+@item PowerPC
+@table @samp
+@item altivec
+@end table
+@item Specific Processors
+@table @samp
+@item pentium2
+@item pentium3
+@item pentium4
+@item k6
+@item k62
+@item athlon
+@item athlonxp
+@item k8
+@end table
+@end table
 
+@item -opencl_options options (@emph{global})
+Set OpenCL environment options. This option is only available when
+FFmpeg has been compiled with @code{--enable-opencl}.
+
+@var{options} must be a list of @var{key}=@var{value} option pairs
+separated by ':'. See the ``OpenCL Options'' section in the
+ffmpeg-utils manual for the list of supported options.
 @end table
 
 @section AVOptions
diff --git a/doc/codecs.texi b/doc/codecs.texi
new file mode 100644
index 0000000..2adadc0
--- /dev/null
+++ b/doc/codecs.texi
@@ -0,0 +1,1069 @@
+@chapter Codec Options
+@c man begin CODEC OPTIONS
+
+libavcodec provides some generic global options, which can be set on
+all the encoders and decoders. In addition each codec may support
+so-called private options, which are specific for a given codec.
+
+Sometimes, a global option may only affect a specific kind of codec,
+and may be unsensical or ignored by another, so you need to be aware
+of the meaning of the specified options. Also some options are
+meant only for decoding or encoding.
+
+Options may be set by specifying -@var{option} @var{value} in the
+FFmpeg tools, or by setting the value explicitly in the
+@code{AVCodecContext} options or using the @file{libavutil/opt.h} API
+for programmatic use.
+
+The list of supported options follow:
+
+@table @option
+@item b @var{integer} (@emph{encoding,audio,video})
+Set bitrate in bits/s. Default value is 200K.
+
+@item ab @var{integer} (@emph{encoding,audio})
+Set audio bitrate (in bits/s). Default value is 128K.
+
+@item bt @var{integer} (@emph{encoding,video})
+Set video bitrate tolerance (in bits/s). In 1-pass mode, bitrate
+tolerance specifies how far ratecontrol is willing to deviate from the
+target average bitrate value. This is not related to min/max
+bitrate. Lowering tolerance too much has an adverse effect on quality.
+
+@item flags @var{flags} (@emph{decoding/encoding,audio,video,subtitles})
+Set generic flags.
+
+Possible values:
+@table @samp
+@item mv4
+Use four motion vector by macroblock (mpeg4).
+@item qpel
+Use 1/4 pel motion compensation.
+@item loop
+Use loop filter.
+@item qscale
+Use fixed qscale.
+@item gmc
+Use gmc.
+@item mv0
+Always try a mb with mv=<0,0>.
+@item input_preserved
+
+@item pass1
+Use internal 2pass ratecontrol in first pass mode.
+@item pass2
+Use internal 2pass ratecontrol in second pass mode.
+@item gray
+Only decode/encode grayscale.
+@item emu_edge
+Do not draw edges.
+@item psnr
+Set error[?] variables during encoding.
+@item truncated
+
+@item naq
+Normalize adaptive quantization.
+@item ildct
+Use interlaced DCT.
+@item low_delay
+Force low delay.
+@item global_header
+Place global headers in extradata instead of every keyframe.
+@item bitexact
+Use only bitexact stuff (except (I)DCT).
+@item aic
+Apply H263 advanced intra coding / mpeg4 ac prediction.
+@item cbp
+Deprecated, use mpegvideo private options instead.
+@item qprd
+Deprecated, use mpegvideo private options instead.
+@item ilme
+Apply interlaced motion estimation.
+@item cgop
+Use closed gop.
+@end table
+
+@item sub_id @var{integer}
+Deprecated, currently unused.
+
+@item me_method @var{integer} (@emph{encoding,video})
+Set motion estimation method.
+
+Possible values:
+@table @samp
+@item zero
+zero motion estimation (fastest)
+@item full
+full motion estimation (slowest)
+@item epzs
+EPZS motion estimation (default)
+@item esa
+esa motion estimation (alias for full)
+@item tesa
+tesa motion estimation
+@item dia
+dia motion estimation (alias for epzs)
+@item log
+log motion estimation
+@item phods
+phods motion estimation
+@item x1
+X1 motion estimation
+@item hex
+hex motion estimation
+@item umh
+umh motion estimation
+@item iter
+iter motion estimation
+@end table
+
+@item extradata_size @var{integer}
+Set extradata size.
+
+@item time_base @var{rational number}
+Set codec time base.
+
+It is the fundamental unit of time (in seconds) in terms of which
+frame timestamps are represented. For fixed-fps content, timebase
+should be @code{1 / frame_rate} and timestamp increments should be
+identically 1.
+
+@item g @var{integer} (@emph{encoding,video})
+Set the group of picture size. Default value is 12.
+
+@item ar @var{integer} (@emph{decoding/encoding,audio})
+Set audio sampling rate (in Hz).
+
+@item ac @var{integer} (@emph{decoding/encoding,audio})
+Set number of audio channels.
+
+@item cutoff @var{integer} (@emph{encoding,audio})
+Set cutoff bandwidth.
+
+@item frame_size @var{integer} (@emph{encoding,audio})
+Set audio frame size.
+
+Each submitted frame except the last must contain exactly frame_size
+samples per channel. May be 0 when the codec has
+CODEC_CAP_VARIABLE_FRAME_SIZE set, in that case the frame size is not
+restricted. It is set by some decoders to indicate constant frame
+size.
+
+@item frame_number @var{integer}
+Set the frame number.
+
+@item delay @var{integer}
+
+@item qcomp @var{float} (@emph{encoding,video})
+Set video quantizer scale compression (VBR). It is used as a constant
+in the ratecontrol equation. Recommended range for default rc_eq:
+0.0-1.0.
+
+@item qblur @var{float} (@emph{encoding,video})
+Set video quantizer scale blur (VBR).
+
+@item qmin @var{integer} (@emph{encoding,video})
+Set min video quantizer scale (VBR). Must be included between -1 and
+69, default value is 2.
+
+@item qmax @var{integer} (@emph{encoding,video})
+Set max video quantizer scale (VBR). Must be included between -1 and
+1024, default value is 31.
+
+@item qdiff @var{integer} (@emph{encoding,video})
+Set max difference between the quantizer scale (VBR).
+
+@item bf @var{integer} (@emph{encoding,video})
+Set max number of B frames.
+
+@item b_qfactor @var{float} (@emph{encoding,video})
+Set qp factor between P and B frames.
+
+@item rc_strategy @var{integer} (@emph{encoding,video})
+Set ratecontrol method.
+
+@item b_strategy @var{integer} (@emph{encoding,video})
+Set strategy to choose between I/P/B-frames.
+
+@item ps @var{integer} (@emph{encoding,video})
+Set RTP payload size in bytes.
+
+@item mv_bits @var{integer}
+@item header_bits @var{integer}
+@item i_tex_bits @var{integer}
+@item p_tex_bits @var{integer}
+@item i_count @var{integer}
+@item p_count @var{integer}
+@item skip_count @var{integer}
+@item misc_bits @var{integer}
+@item frame_bits @var{integer}
+@item codec_tag @var{integer}
+@item bug @var{flags} (@emph{decoding,video})
+Workaround not auto detected encoder bugs.
+
+Possible values:
+@table @samp
+@item autodetect
+
+@item old_msmpeg4
+some old lavc generated msmpeg4v3 files (no autodetection)
+@item xvid_ilace
+Xvid interlacing bug (autodetected if fourcc==XVIX)
+@item ump4
+(autodetected if fourcc==UMP4)
+@item no_padding
+padding bug (autodetected)
+@item amv
+
+@item ac_vlc
+illegal vlc bug (autodetected per fourcc)
+@item qpel_chroma
+
+@item std_qpel
+old standard qpel (autodetected per fourcc/version)
+@item qpel_chroma2
+
+@item direct_blocksize
+direct-qpel-blocksize bug (autodetected per fourcc/version)
+@item edge
+edge padding bug (autodetected per fourcc/version)
+@item hpel_chroma
+
+@item dc_clip
+
+@item ms
+Workaround various bugs in microsoft broken decoders.
+@item trunc
+trancated frames
+@end table
+
+@item lelim @var{integer} (@emph{encoding,video})
+Set single coefficient elimination threshold for luminance (negative
+values also consider DC coefficient).
+
+@item celim @var{integer} (@emph{encoding,video})
+Set single coefficient elimination threshold for chrominance (negative
+values also consider dc coefficient)
+
+@item strict @var{integer} (@emph{decoding/encoding,audio,video})
+Specify how strictly to follow the standards.
+
+Possible values:
+@table @samp
+@item very
+strictly conform to a older more strict version of the spec or reference software
+@item strict
+strictly conform to all the things in the spec no matter what consequences
+@item normal
+
+@item unofficial
+allow unofficial extensions
+@item experimental
+allow non standardized experimental things
+@end table
+
+@item b_qoffset @var{float} (@emph{encoding,video})
+Set QP offset between P and B frames.
+
+@item err_detect @var{flags} (@emph{decoding,audio,video})
+Set error detection flags.
+
+Possible values:
+@table @samp
+@item crccheck
+verify embedded CRCs
+@item bitstream
+detect bitstream specification deviations
+@item buffer
+detect improper bitstream length
+@item explode
+abort decoding on minor error detection
+@item careful
+consider things that violate the spec and have not been seen in the wild as errors
+@item compliant
+consider all spec non compliancies as errors
+@item aggressive
+consider things that a sane encoder should not do as an error
+@end table
+
+@item has_b_frames @var{integer}
+
+@item block_align @var{integer}
+
+@item mpeg_quant @var{integer} (@emph{encoding,video})
+Use MPEG quantizers instead of H.263.
+
+@item qsquish @var{float} (@emph{encoding,video})
+How to keep quantizer between qmin and qmax (0 = clip, 1 = use
+differentiable function).
+
+@item rc_qmod_amp @var{float} (@emph{encoding,video})
+Set experimental quantizer modulation.
+
+@item rc_qmod_freq @var{integer} (@emph{encoding,video})
+Set experimental quantizer modulation.
+
+@item rc_override_count @var{integer}
+
+@item rc_eq @var{string} (@emph{encoding,video})
+Set rate control equation. When computing the expression, besides the
+standard functions defined in the section 'Expression Evaluation', the
+following functions are available: bits2qp(bits), qp2bits(qp). Also
+the following constants are available: iTex pTex tex mv fCode iCount
+mcVar var isI isP isB avgQP qComp avgIITex avgPITex avgPPTex avgBPTex
+avgTex.
+
+@item maxrate @var{integer} (@emph{encoding,audio,video})
+Set max bitrate tolerance (in bits/s). Requires bufsize to be set.
+
+@item minrate @var{integer} (@emph{encoding,audio,video})
+Set min bitrate tolerance (in bits/s). Most useful in setting up a CBR
+encode. It is of little use elsewise.
+
+@item bufsize @var{integer} (@emph{encoding,audio,video})
+Set ratecontrol buffer size (in bits).
+
+@item rc_buf_aggressivity @var{float} (@emph{encoding,video})
+Currently useless.
+
+@item i_qfactor @var{float} (@emph{encoding,video})
+Set QP factor between P and I frames.
+
+@item i_qoffset @var{float} (@emph{encoding,video})
+Set QP offset between P and I frames.
+
+@item rc_init_cplx @var{float} (@emph{encoding,video})
+Set initial complexity for 1-pass encoding.
+
+@item dct @var{integer} (@emph{encoding,video})
+Set DCT algorithm.
+
+Possible values:
+@table @samp
+@item auto
+autoselect a good one (default)
+@item fastint
+fast integer
+@item int
+accurate integer
+@item mmx
+
+@item altivec
+
+@item faan
+floating point AAN DCT
+@end table
+
+@item lumi_mask @var{float} (@emph{encoding,video})
+Compress bright areas stronger than medium ones.
+
+@item tcplx_mask @var{float} (@emph{encoding,video})
+Set temporal complexity masking.
+
+@item scplx_mask @var{float} (@emph{encoding,video})
+Set spatial complexity masking.
+
+@item p_mask @var{float} (@emph{encoding,video})
+Set inter masking.
+
+@item dark_mask @var{float} (@emph{encoding,video})
+Compress dark areas stronger than medium ones.
+
+@item idct @var{integer} (@emph{decoding/encoding,video})
+Select IDCT implementation.
+
+Possible values:
+@table @samp
+@item auto
+
+@item int
+
+@item simple
+
+@item simplemmx
+
+@item libmpeg2mmx
+
+@item mmi
+
+@item arm
+
+@item altivec
+
+@item sh4
+
+@item simplearm
+
+@item simplearmv5te
+
+@item simplearmv6
+
+@item simpleneon
+
+@item simplealpha
+
+@item h264
+
+@item vp3
+
+@item ipp
+
+@item xvidmmx
+
+@item faani
+floating point AAN IDCT
+@end table
+
+@item slice_count @var{integer}
+
+@item ec @var{flags} (@emph{decoding,video})
+Set error concealment strategy.
+
+Possible values:
+@table @samp
+@item guess_mvs
+iterative motion vector (MV) search (slow)
+@item deblock
+use strong deblock filter for damaged MBs
+@end table
+
+@item bits_per_coded_sample @var{integer}
+
+@item pred @var{integer} (@emph{encoding,video})
+Set prediction method.
+
+Possible values:
+@table @samp
+@item left
+
+@item plane
+
+@item median
+
+@end table
+
+@item aspect @var{rational number} (@emph{encoding,video})
+Set sample aspect ratio.
+
+@item debug @var{flags} (@emph{decoding/encoding,audio,video,subtitles})
+Print specific debug info.
+
+Possible values:
+@table @samp
+@item pict
+picture info
+@item rc
+rate control
+@item bitstream
+
+@item mb_type
+macroblock (MB) type
+@item qp
+per-block quantization parameter (QP)
+@item mv
+motion vector
+@item dct_coeff
+
+@item skip
+
+@item startcode
+
+@item pts
+
+@item er
+error recognition
+@item mmco
+memory management control operations (H.264)
+@item bugs
+
+@item vis_qp
+visualize quantization parameter (QP), lower QP are tinted greener
+@item vis_mb_type
+visualize block types
+@item buffers
+picture buffer allocations
+@item thread_ops
+threading operations
+@end table
+
+@item vismv @var{integer} (@emph{decoding,video})
+Visualize motion vectors (MVs).
+
+Possible values:
+@table @samp
+@item pf
+forward predicted MVs of P-frames
+@item bf
+forward predicted MVs of B-frames
+@item bb
+backward predicted MVs of B-frames
+@end table
+
+@item cmp @var{integer} (@emph{encoding,video})
+Set full pel me compare function.
+
+Possible values:
+@table @samp
+@item sad
+sum of absolute differences, fast (default)
+@item sse
+sum of squared errors
+@item satd
+sum of absolute Hadamard transformed differences
+@item dct
+sum of absolute DCT transformed differences
+@item psnr
+sum of squared quantization errors (avoid, low quality)
+@item bit
+number of bits needed for the block
+@item rd
+rate distortion optimal, slow
+@item zero
+0
+@item vsad
+sum of absolute vertical differences
+@item vsse
+sum of squared vertical differences
+@item nsse
+noise preserving sum of squared differences
+@item w53
+5/3 wavelet, only used in snow
+@item w97
+9/7 wavelet, only used in snow
+@item dctmax
+
+@item chroma
+
+@end table
+
+@item subcmp @var{integer} (@emph{encoding,video})
+Set sub pel me compare function.
+
+Possible values:
+@table @samp
+@item sad
+sum of absolute differences, fast (default)
+@item sse
+sum of squared errors
+@item satd
+sum of absolute Hadamard transformed differences
+@item dct
+sum of absolute DCT transformed differences
+@item psnr
+sum of squared quantization errors (avoid, low quality)
+@item bit
+number of bits needed for the block
+@item rd
+rate distortion optimal, slow
+@item zero
+0
+@item vsad
+sum of absolute vertical differences
+@item vsse
+sum of squared vertical differences
+@item nsse
+noise preserving sum of squared differences
+@item w53
+5/3 wavelet, only used in snow
+@item w97
+9/7 wavelet, only used in snow
+@item dctmax
+
+@item chroma
+
+@end table
+
+@item mbcmp @var{integer} (@emph{encoding,video})
+Set macroblock compare function.
+
+Possible values:
+@table @samp
+@item sad
+sum of absolute differences, fast (default)
+@item sse
+sum of squared errors
+@item satd
+sum of absolute Hadamard transformed differences
+@item dct
+sum of absolute DCT transformed differences
+@item psnr
+sum of squared quantization errors (avoid, low quality)
+@item bit
+number of bits needed for the block
+@item rd
+rate distortion optimal, slow
+@item zero
+0
+@item vsad
+sum of absolute vertical differences
+@item vsse
+sum of squared vertical differences
+@item nsse
+noise preserving sum of squared differences
+@item w53
+5/3 wavelet, only used in snow
+@item w97
+9/7 wavelet, only used in snow
+@item dctmax
+
+@item chroma
+
+@end table
+
+@item ildctcmp @var{integer} (@emph{encoding,video})
+Set interlaced dct compare function.
+
+Possible values:
+@table @samp
+@item sad
+sum of absolute differences, fast (default)
+@item sse
+sum of squared errors
+@item satd
+sum of absolute Hadamard transformed differences
+@item dct
+sum of absolute DCT transformed differences
+@item psnr
+sum of squared quantization errors (avoid, low quality)
+@item bit
+number of bits needed for the block
+@item rd
+rate distortion optimal, slow
+@item zero
+0
+@item vsad
+sum of absolute vertical differences
+@item vsse
+sum of squared vertical differences
+@item nsse
+noise preserving sum of squared differences
+@item w53
+5/3 wavelet, only used in snow
+@item w97
+9/7 wavelet, only used in snow
+@item dctmax
+
+@item chroma
+
+@end table
+
+@item dia_size @var{integer} (@emph{encoding,video})
+Set diamond type & size for motion estimation.
+
+@item last_pred @var{integer} (@emph{encoding,video})
+Set amount of motion predictors from the previous frame.
+
+@item preme @var{integer} (@emph{encoding,video})
+Set pre motion estimation.
+
+@item precmp @var{integer} (@emph{encoding,video})
+Set pre motion estimation compare function.
+
+Possible values:
+@table @samp
+@item sad
+sum of absolute differences, fast (default)
+@item sse
+sum of squared errors
+@item satd
+sum of absolute Hadamard transformed differences
+@item dct
+sum of absolute DCT transformed differences
+@item psnr
+sum of squared quantization errors (avoid, low quality)
+@item bit
+number of bits needed for the block
+@item rd
+rate distortion optimal, slow
+@item zero
+0
+@item vsad
+sum of absolute vertical differences
+@item vsse
+sum of squared vertical differences
+@item nsse
+noise preserving sum of squared differences
+@item w53
+5/3 wavelet, only used in snow
+@item w97
+9/7 wavelet, only used in snow
+@item dctmax
+
+@item chroma
+
+@end table
+
+@item pre_dia_size @var{integer} (@emph{encoding,video})
+Set diamond type & size for motion estimation pre-pass.
+
+@item subq @var{integer} (@emph{encoding,video})
+Set sub pel motion estimation quality.
+
+@item dtg_active_format @var{integer}
+
+@item me_range @var{integer} (@emph{encoding,video})
+Set limit motion vectors range (1023 for DivX player).
+
+@item ibias @var{integer} (@emph{encoding,video})
+Set intra quant bias.
+
+@item pbias @var{integer} (@emph{encoding,video})
+Set inter quant bias.
+
+@item color_table_id @var{integer}
+
+@item global_quality @var{integer} (@emph{encoding,audio,video})
+
+@item coder @var{integer} (@emph{encoding,video})
+
+Possible values:
+@table @samp
+@item vlc
+variable length coder / huffman coder
+@item ac
+arithmetic coder
+@item raw
+raw (no encoding)
+@item rle
+run-length coder
+@item deflate
+deflate-based coder
+@end table
+
+@item context @var{integer} (@emph{encoding,video})
+Set context model.
+
+@item slice_flags @var{integer}
+
+@item xvmc_acceleration @var{integer}
+
+@item mbd @var{integer} (@emph{encoding,video})
+Set macroblock decision algorithm (high quality mode).
+
+Possible values:
+@table @samp
+@item simple
+use mbcmp (default)
+@item bits
+use fewest bits
+@item rd
+use best rate distortion
+@end table
+
+@item stream_codec_tag @var{integer}
+
+@item sc_threshold @var{integer} (@emph{encoding,video})
+Set scene change threshold.
+
+@item lmin @var{integer} (@emph{encoding,video})
+Set min lagrange factor (VBR).
+
+@item lmax @var{integer} (@emph{encoding,video})
+Set max lagrange factor (VBR).
+
+@item nr @var{integer} (@emph{encoding,video})
+Set noise reduction.
+
+@item rc_init_occupancy @var{integer} (@emph{encoding,video})
+Set number of bits which should be loaded into the rc buffer before
+decoding starts.
+
+@item inter_threshold @var{integer} (@emph{encoding,video})
+
+@item flags2 @var{flags} (@emph{decoding/encoding,audio,video})
+
+Possible values:
+@table @samp
+@item fast
+allow non spec compliant speedup tricks
+@item sgop
+Deprecated, use mpegvideo private options instead
+@item noout
+skip bitstream encoding
+@item local_header
+place global headers at every keyframe instead of in extradata
+@item chunks
+Frame data might be split into multiple chunks
+@item showall
+Show all frames before the first keyframe
+@item skiprd
+Deprecated, use mpegvideo private options instead
+@end table
+
+@item error @var{integer} (@emph{encoding,video})
+
+@item qns @var{integer} (@emph{encoding,video})
+Deprecated, use mpegvideo private options instead.
+
+@item threads @var{integer} (@emph{decoding/encoding,video})
+
+Possible values:
+@table @samp
+@item auto
+detect a good number of threads
+@end table
+
+@item me_threshold @var{integer} (@emph{encoding,video})
+Set motion estimation threshold.
+
+@item mb_threshold @var{integer} (@emph{encoding,video})
+Set macroblock threshold.
+
+@item dc @var{integer} (@emph{encoding,video})
+Set intra_dc_precision.
+
+@item nssew @var{integer} (@emph{encoding,video})
+Set nsse weight.
+
+@item skip_top @var{integer} (@emph{decoding,video})
+Set number of macroblock rows at the top which are skipped.
+
+@item skip_bottom @var{integer} (@emph{decoding,video})
+Set number of macroblock rows at the bottom which are skipped.
+
+@item profile @var{integer} (@emph{encoding,audio,video})
+
+Possible values:
+@table @samp
+@item unknown
+
+@item aac_main
+
+@item aac_low
+
+@item aac_ssr
+
+@item aac_ltp
+
+@item aac_he
+
+@item aac_he_v2
+
+@item aac_ld
+
+@item aac_eld
+
+@item dts
+
+@item dts_es
+
+@item dts_96_24
+
+@item dts_hd_hra
+
+@item dts_hd_ma
+
+@end table
+
+@item level @var{integer} (@emph{encoding,audio,video})
+
+Possible values:
+@table @samp
+@item unknown
+
+@end table
+
+@item lowres @var{integer} (@emph{decoding,audio,video})
+Decode at 1= 1/2, 2=1/4, 3=1/8 resolutions.
+
+@item skip_threshold @var{integer} (@emph{encoding,video})
+Set frame skip threshold.
+
+@item skip_factor @var{integer} (@emph{encoding,video})
+Set frame skip factor.
+
+@item skip_exp @var{integer} (@emph{encoding,video})
+Set frame skip exponent.
+
+@item skipcmp @var{integer} (@emph{encoding,video})
+Set frame skip compare function.
+
+Possible values:
+@table @samp
+@item sad
+sum of absolute differences, fast (default)
+@item sse
+sum of squared errors
+@item satd
+sum of absolute Hadamard transformed differences
+@item dct
+sum of absolute DCT transformed differences
+@item psnr
+sum of squared quantization errors (avoid, low quality)
+@item bit
+number of bits needed for the block
+@item rd
+rate distortion optimal, slow
+@item zero
+0
+@item vsad
+sum of absolute vertical differences
+@item vsse
+sum of squared vertical differences
+@item nsse
+noise preserving sum of squared differences
+@item w53
+5/3 wavelet, only used in snow
+@item w97
+9/7 wavelet, only used in snow
+@item dctmax
+
+@item chroma
+
+@end table
+
+@item border_mask @var{float} (@emph{encoding,video})
+Increase the quantizer for macroblocks close to borders.
+
+@item mblmin @var{integer} (@emph{encoding,video})
+Set min macroblock lagrange factor (VBR).
+
+@item mblmax @var{integer} (@emph{encoding,video})
+Set max macroblock lagrange factor (VBR).
+
+@item mepc @var{integer} (@emph{encoding,video})
+Set motion estimation bitrate penalty compensation (1.0 = 256).
+
+@item skip_loop_filter @var{integer} (@emph{decoding,video})
+@item skip_idct        @var{integer} (@emph{decoding,video})
+@item skip_frame       @var{integer} (@emph{decoding,video})
+
+Make decoder discard processing depending on the frame type selected
+by the option value.
+
+@option{skip_loop_filter} skips frame loop filtering, @option{skip_idct}
+skips frame IDCT/dequantization, @option{skip_frame} skips decoding.
+
+Possible values:
+@table @samp
+@item none
+Discard no frame.
+
+@item default
+Discard useless frames like 0-sized frames.
+
+@item noref
+Discard all non-reference frames.
+
+@item bidir
+Discard all bidirectional frames.
+
+@item nokey
+Discard all frames excepts keyframes.
+
+@item all
+Discard all frames.
+@end table
+
+Default value is @samp{default}.
+
+@item bidir_refine @var{integer} (@emph{encoding,video})
+Refine the two motion vectors used in bidirectional macroblocks.
+
+@item brd_scale @var{integer} (@emph{encoding,video})
+Downscale frames for dynamic B-frame decision.
+
+@item keyint_min @var{integer} (@emph{encoding,video})
+Set minimum interval between IDR-frames.
+
+@item refs @var{integer} (@emph{encoding,video})
+Set reference frames to consider for motion compensation.
+
+@item chromaoffset @var{integer} (@emph{encoding,video})
+Set chroma qp offset from luma.
+
+@item trellis @var{integer} (@emph{encoding,audio,video})
+Set rate-distortion optimal quantization.
+
+@item sc_factor @var{integer} (@emph{encoding,video})
+Set value multiplied by qscale for each frame and added to
+scene_change_score.
+
+@item mv0_threshold @var{integer} (@emph{encoding,video})
+@item b_sensitivity @var{integer} (@emph{encoding,video})
+Adjust sensitivity of b_frame_strategy 1.
+
+@item compression_level @var{integer} (@emph{encoding,audio,video})
+@item min_prediction_order @var{integer} (@emph{encoding,audio})
+@item max_prediction_order @var{integer} (@emph{encoding,audio})
+@item timecode_frame_start @var{integer} (@emph{encoding,video})
+Set GOP timecode frame start number, in non drop frame format.
+
+@item request_channels @var{integer} (@emph{decoding,audio})
+Set desired number of audio channels.
+
+@item bits_per_raw_sample @var{integer}
+@item channel_layout @var{integer} (@emph{decoding/encoding,audio})
+
+Possible values:
+@table @samp
+@end table
+@item request_channel_layout @var{integer} (@emph{decoding,audio})
+
+Possible values:
+@table @samp
+@end table
+@item rc_max_vbv_use @var{float} (@emph{encoding,video})
+@item rc_min_vbv_use @var{float} (@emph{encoding,video})
+@item ticks_per_frame @var{integer} (@emph{decoding/encoding,audio,video})
+@item color_primaries @var{integer} (@emph{decoding/encoding,video})
+@item color_trc @var{integer} (@emph{decoding/encoding,video})
+@item colorspace @var{integer} (@emph{decoding/encoding,video})
+@item color_range @var{integer} (@emph{decoding/encoding,video})
+@item chroma_sample_location @var{integer} (@emph{decoding/encoding,video})
+
+@item log_level_offset @var{integer}
+Set the log level offset.
+
+@item slices @var{integer} (@emph{encoding,video})
+Number of slices, used in parallelized encoding.
+
+@item thread_type @var{flags} (@emph{decoding/encoding,video})
+Select multithreading type.
+
+Possible values:
+@table @samp
+@item slice
+
+@item frame
+
+@end table
+@item audio_service_type @var{integer} (@emph{encoding,audio})
+Set audio service type.
+
+Possible values:
+@table @samp
+@item ma
+Main Audio Service
+@item ef
+Effects
+@item vi
+Visually Impaired
+@item hi
+Hearing Impaired
+@item di
+Dialogue
+@item co
+Commentary
+@item em
+Emergency
+@item vo
+Voice Over
+@item ka
+Karaoke
+@end table
+
+@item request_sample_fmt @var{sample_fmt} (@emph{decoding,audio})
+Set sample format audio decoders should prefer. Default value is
+@code{none}.
+
+@item pkt_timebase @var{rational number}
+
+@item sub_charenc @var{encoding} (@emph{decoding,subtitles})
+Set the input subtitles character encoding.
+@end table
+
+@c man end CODEC OPTIONS
+
+@include decoders.texi
+@include encoders.texi
diff --git a/doc/demuxers.texi b/doc/demuxers.texi
index c8eec21..c75e1ab 100644
--- a/doc/demuxers.texi
+++ b/doc/demuxers.texi
@@ -29,6 +29,7 @@
 The total bitrate of the variant that the stream belongs to is
 available in a metadata key named "variant_bitrate".
 
+@anchor{concat}
 @section concat
 
 Virtual concatenation script demuxer.
@@ -44,6 +45,12 @@
 
 All files must have the same streams (same codecs, same time base, etc.).
 
+The duration of each file is used to adjust the timestamps of the next file:
+if the duration is incorrect (because it was computed using the bit-rate or
+because the file is truncated, for example), it can cause artifacts. The
+@code{duration} directive can be used to override the duration stored in
+each file.
+
 @subsection Syntax
 
 The script is a text file in extended-ASCII, with one directive per line.
@@ -71,6 +78,9 @@
 specifying it here may be more efficient or help if the information from the
 file is not available or accurate.
 
+If the duration is set for all files, then it is possible to seek in the
+whole concatenated video.
+
 @end table
 
 @subsection Options
@@ -93,6 +103,18 @@
 
 @end table
 
+@section libquvi
+
+Play media from Internet services using the quvi project.
+
+The demuxer accepts a @option{format} option to request a specific quality. It
+is by default set to @var{best}.
+
+See @url{http://quvi.sourceforge.net/} for more information.
+
+FFmpeg needs to be built with @code{--enable-libquvi} for this demuxer to be
+enabled.
+
 @section image2
 
 Image file demuxer.
@@ -110,7 +132,7 @@
 This demuxer accepts the following options:
 @table @option
 @item framerate
-Set the framerate for the video stream. It defaults to 25.
+Set the frame rate for the video stream. It defaults to 25.
 @item loop
 If set to 1, loop over the input. Default value is 0.
 @item pattern_type
diff --git a/doc/developer.texi b/doc/developer.texi
index fe4b40a..d5580d1 100644
--- a/doc/developer.texi
+++ b/doc/developer.texi
@@ -157,13 +157,16 @@
 @item
 For local variables no prefix is required.
 @item
-For variables and functions declared as @code{static} no prefix is required.
+For file-scope variables and functions declared as @code{static}, no prefix
+is required.
 @item
-For variables and functions used internally by a library an @code{ff_}
-prefix should be used, e.g. @samp{ff_w64_demuxer}.
+For variables and functions visible outside of file scope, but only used
+internally by a library, an @code{ff_} prefix should be used,
+e.g. @samp{ff_w64_demuxer}.
 @item
-For variables and functions used internally across multiple libraries, use
-@code{avpriv_}. For example, @samp{avpriv_aac_parse_header}.
+For variables and functions visible outside of file scope, used internally
+across multiple libraries, use @code{avpriv_} as prefix, for example,
+@samp{avpriv_aac_parse_header}.
 @item
 Each library has its own prefix for public symbols, in addition to the
 commonly used @code{av_} (@code{avformat_} for libavformat,
@@ -577,4 +580,116 @@
 the ffmpeg-devel mailing list, a direct link to download the sample media.
 
 
+@subsection Visualizing Test Coverage
+
+The FFmpeg build system allows visualizing the test coverage in an easy
+manner with the coverage tools @code{gcov}/@code{lcov}.  This involves
+the following steps:
+
+@enumerate
+@item
+    Configure to compile with instrumentation enabled:
+    @code{configure --toolchain=gcov}.
+@item
+    Run your test case, either manually or via FATE. This can be either
+    the full FATE regression suite, or any arbitrary invocation of any
+    front-end tool provided by FFmpeg, in any combination.
+@item
+    Run @code{make lcov} to generate coverage data in HTML format.
+@item
+    View @code{lcov/index.html} in your preferred HTML viewer.
+@end enumerate
+
+You can use the command @code{make lcov-reset} to reset the coverage
+measurements. You will need to rerun @code{make lcov} after running a
+new test.
+
+@anchor{Release process}
+@section Release process
+
+FFmpeg maintains a set of @strong{release branches}, which are the
+recommended deliverable for system integrators and distributors (such as
+Linux distributions, etc.). At regular times, a @strong{release
+manager} prepares, tests and publishes tarballs on the
+@url{http://ffmpeg.org} website.
+
+There are two kinds of releases:
+
+@enumerate
+@item
+    @strong{Major releases} always include the latest and greatest
+    features and functionality.
+@item
+    @strong{Point releases} are cut from @strong{release} branches,
+    which are named @code{release/X}, with @code{X} being the release
+    version number.
+@end enumerate
+
+Note that we promise to our users that shared libraries from any FFmpeg
+release never break programs that have been @strong{compiled} against
+previous versions of @strong{the same release series} in any case!
+
+However, from time to time, we do make API changes that require adaptations
+in applications. Such changes are only allowed in (new) major releases and
+require further steps such as bumping library version numbers and/or
+adjustments to the symbol versioning file. Please discuss such changes
+on the @strong{ffmpeg-devel} mailing list in time to allow forward planning.
+
+@anchor{Criteria for Point Releases}
+@subsection Criteria for Point Releases
+
+Changes that match the following criteria are valid candidates for
+inclusion into a point release:
+
+@enumerate
+@item
+    Fixes a security issue, preferably identified by a @strong{CVE
+    number} issued by @url{http://cve.mitre.org/}.
+@item
+    Fixes a documented bug in @url{https://ffmpeg.org/trac/ffmpeg}.
+@item
+    Improves the included documentation.
+@item
+    Retains both source code and binary compatibility with previous
+    point releases of the same release branch.
+@end enumerate
+
+The order for checking the rules is (1 OR 2 OR 3) AND 4.
+
+
+@subsection Release Checklist
+
+The release process involves the following steps:
+
+@enumerate
+@item
+    Ensure that the @file{RELEASE} file contains the version number for
+    the upcoming release.
+@item
+    Add the release at @url{https://ffmpeg.org/trac/ffmpeg/admin/ticket/versions}.
+@item
+    Announce the intent to do a release to the mailing list.
+@item
+    Make sure all relevant security fixes have been backported. See
+    @url{https://ffmpeg.org/security.html}.
+@item
+    Ensure that the FATE regression suite still passes in the release
+    branch on at least @strong{i386} and @strong{amd64}
+    (cf. @ref{Regression tests}).
+@item
+    Prepare the release tarballs in @code{bz2} and @code{gz} formats, and
+    supplementing files that contain @code{gpg} signatures
+@item
+    Publish the tarballs at @url{http://ffmpeg.org/releases}. Create and
+    push an annotated tag in the form @code{nX}, with @code{X}
+    containing the version number.
+@item
+    Propose and send a patch to the @strong{ffmpeg-devel} mailing list
+    with a news entry for the website.
+@item
+    Publish the news entry.
+@item
+    Send announcement to the mailing list.
+@end enumerate
+
 @bye
diff --git a/doc/devices.texi b/doc/devices.texi
new file mode 100644
index 0000000..8f8b22a
--- /dev/null
+++ b/doc/devices.texi
@@ -0,0 +1,21 @@
+@chapter Device Options
+@c man begin DEVICE OPTIONS
+
+The libavdevice library provides the same interface as
+libavformat. Namely, an input device is considered like a demuxer, and
+an output device like a muxer, and the interface and generic device
+options are the same provided by libavformat (see the ffmpeg-formats
+manual).
+
+In addition each input or output device may support so-called private
+options, which are specific for that component.
+
+Options may be set by specifying -@var{option} @var{value} in the
+FFmpeg tools, or by setting the value explicitly in the device
+@code{AVFormatContext} options or using the @file{libavutil/opt.h} API
+for programmatic use.
+
+@c man end DEVICE OPTIONS
+
+@include indevs.texi
+@include outdevs.texi
diff --git a/doc/encoders.texi b/doc/encoders.texi
index 07343eb..ab88b6c 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -777,4 +777,65 @@
 Encoding avpresets for common usages are provided so they can be used with the
 general presets system (e.g. passing the @code{-pre} option).
 
+@section ProRes
+
+Apple ProRes encoder.
+
+FFmpeg contains 2 ProRes encoders, the prores-aw and prores-ks encoder.
+The used encoder can be choosen with the @code{-vcodec} option.
+
+@subsection Private Options for prores-ks
+
+@table @option
+@item profile @var{integer}
+Select the ProRes profile to encode
+@table @samp
+@item proxy
+@item lt
+@item standard
+@item hq
+@end table
+
+@item quant_mat @var{integer}
+Select quantization matrix.
+@table @samp
+@item auto
+@item default
+@item proxy
+@item lt
+@item standard
+@item hq
+@end table
+If set to @var{auto}, the matrix matching the profile will be picked.
+If not set, the matrix providing the highest quality, @var{default}, will be
+picked.
+
+@item bits_per_mb @var{integer}
+How many bits to allot for coding one macroblock. Different profiles use
+between 200 and 2400 bits per macroblock, the maximum is 8000.
+
+@item mbs_per_slice @var{integer}
+Number of macroblocks in each slice (1-8); the default value (8)
+should be good in almost all situations.
+
+@item vendor @var{string}
+Override the 4-byte vendor ID.
+A custom vendor ID like @var{apl0} would claim the stream was produced by
+the Apple encoder.
+
+@end table
+
+@subsection Speed considerations
+
+In the default mode of operation the encoder has to honor frame constraints
+(i.e. not produc frames with size bigger than requested) while still making
+output picture as good as possible.
+A frame containing a lot of small details is harder to compress and the encoder
+would spend more time searching for appropriate quantizers for each slice.
+
+Setting a higher @option{bits_per_mb} limit will improve the speed.
+
+For the fastest encoding speed set the @option{qscale} parameter (4 is the
+recommended value) and do not set a size constraint.
+
 @c man end VIDEO ENCODERS
diff --git a/doc/eval.texi b/doc/eval.texi
deleted file mode 100644
index 3b7964c..0000000
--- a/doc/eval.texi
+++ /dev/null
@@ -1,288 +0,0 @@
-@chapter Expression Evaluation
-@c man begin EXPRESSION EVALUATION
-
-When evaluating an arithmetic expression, FFmpeg uses an internal
-formula evaluator, implemented through the @file{libavutil/eval.h}
-interface.
-
-An expression may contain unary, binary operators, constants, and
-functions.
-
-Two expressions @var{expr1} and @var{expr2} can be combined to form
-another expression "@var{expr1};@var{expr2}".
-@var{expr1} and @var{expr2} are evaluated in turn, and the new
-expression evaluates to the value of @var{expr2}.
-
-The following binary operators are available: @code{+}, @code{-},
-@code{*}, @code{/}, @code{^}.
-
-The following unary operators are available: @code{+}, @code{-}.
-
-The following functions are available:
-@table @option
-@item abs(x)
-Compute absolute value of @var{x}.
-
-@item acos(x)
-Compute arccosine of @var{x}.
-
-@item asin(x)
-Compute arcsine of @var{x}.
-
-@item atan(x)
-Compute arctangent of @var{x}.
-
-@item ceil(expr)
-Round the value of expression @var{expr} upwards to the nearest
-integer. For example, "ceil(1.5)" is "2.0".
-
-@item cos(x)
-Compute cosine of @var{x}.
-
-@item cosh(x)
-Compute hyperbolic cosine of @var{x}.
-
-@item eq(x, y)
-Return 1 if @var{x} and @var{y} are equivalent, 0 otherwise.
-
-@item exp(x)
-Compute exponential of @var{x} (with base @code{e}, the Euler's number).
-
-@item floor(expr)
-Round the value of expression @var{expr} downwards to the nearest
-integer. For example, "floor(-1.5)" is "-2.0".
-
-@item gauss(x)
-Compute Gauss function of @var{x}, corresponding to
-@code{exp(-x*x/2) / sqrt(2*PI)}.
-
-@item gcd(x, y)
-Return the greatest common divisor of @var{x} and @var{y}. If both @var{x} and
-@var{y} are 0 or either or both are less than zero then behavior is undefined.
-
-@item gt(x, y)
-Return 1 if @var{x} is greater than @var{y}, 0 otherwise.
-
-@item gte(x, y)
-Return 1 if @var{x} is greater than or equal to @var{y}, 0 otherwise.
-
-@item hypot(x, y)
-This function is similar to the C function with the same name; it returns
-"sqrt(@var{x}*@var{x} + @var{y}*@var{y})", the length of the hypotenuse of a
-right triangle with sides of length @var{x} and @var{y}, or the distance of the
-point (@var{x}, @var{y}) from the origin.
-
-@item if(x, y)
-Evaluate @var{x}, and if the result is non-zero return the result of
-the evaluation of @var{y}, return 0 otherwise.
-
-@item if(x, y, z)
-Evaluate @var{x}, and if the result is non-zero return the evaluation
-result of @var{y}, otherwise the evaluation result of @var{z}.
-
-@item ifnot(x, y)
-Evaluate @var{x}, and if the result is zero return the result of the
-evaluation of @var{y}, return 0 otherwise.
-
-@item ifnot(x, y, z)
-Evaluate @var{x}, and if the result is zero return the evaluation
-result of @var{y}, otherwise the evaluation result of @var{z}.
-
-@item isinf(x)
-Return 1.0 if @var{x} is +/-INFINITY, 0.0 otherwise.
-
-@item isnan(x)
-Return 1.0 if @var{x} is NAN, 0.0 otherwise.
-
-@item ld(var)
-Allow to load the value of the internal variable with number
-@var{var}, which was previously stored with st(@var{var}, @var{expr}).
-The function returns the loaded value.
-
-@item log(x)
-Compute natural logarithm of @var{x}.
-
-@item lt(x, y)
-Return 1 if @var{x} is lesser than @var{y}, 0 otherwise.
-
-@item lte(x, y)
-Return 1 if @var{x} is lesser than or equal to @var{y}, 0 otherwise.
-
-@item max(x, y)
-Return the maximum between @var{x} and @var{y}.
-
-@item min(x, y)
-Return the maximum between @var{x} and @var{y}.
-
-@item mod(x, y)
-Compute the remainder of division of @var{x} by @var{y}.
-
-@item not(expr)
-Return 1.0 if @var{expr} is zero, 0.0 otherwise.
-
-@item pow(x, y)
-Compute the power of @var{x} elevated @var{y}, it is equivalent to
-"(@var{x})^(@var{y})".
-
-@item print(t)
-@item print(t, l)
-Print the value of expression @var{t} with loglevel @var{l}. If
-@var{l} is not specified then a default log level is used.
-Returns the value of the expression printed.
-
-Prints t with loglevel l
-
-@item random(x)
-Return a pseudo random value between 0.0 and 1.0. @var{x} is the index of the
-internal variable which will be used to save the seed/state.
-
-@item root(expr, max)
-Find an input value for which the function represented by @var{expr}
-with argument @var{ld(0)} is 0 in the interval 0..@var{max}.
-
-The expression in @var{expr} must denote a continuous function or the
-result is undefined.
-
-@var{ld(0)} is used to represent the function input value, which means
-that the given expression will be evaluated multiple times with
-various input values that the expression can access through
-@code{ld(0)}. When the expression evaluates to 0 then the
-corresponding input value will be returned.
-
-@item sin(x)
-Compute sine of @var{x}.
-
-@item sinh(x)
-Compute hyperbolic sine of @var{x}.
-
-@item sqrt(expr)
-Compute the square root of @var{expr}. This is equivalent to
-"(@var{expr})^.5".
-
-@item squish(x)
-Compute expression @code{1/(1 + exp(4*x))}.
-
-@item st(var, expr)
-Allow to store the value of the expression @var{expr} in an internal
-variable. @var{var} specifies the number of the variable where to
-store the value, and it is a value ranging from 0 to 9. The function
-returns the value stored in the internal variable.
-Note, Variables are currently not shared between expressions.
-
-@item tan(x)
-Compute tangent of @var{x}.
-
-@item tanh(x)
-Compute hyperbolic tangent of @var{x}.
-
-@item taylor(expr, x)
-@item taylor(expr, x, id)
-Evaluate a Taylor series at @var{x}, given an expression representing
-the @code{ld(id)}-th derivative of a function at 0.
-
-When the series does not converge the result is undefined.
-
-@var{ld(id)} is used to represent the derivative order in @var{expr},
-which means that the given expression will be evaluated multiple times
-with various input values that the expression can access through
-@code{ld(id)}. If @var{id} is not specified then 0 is assumed.
-
-Note, when you have the derivatives at y instead of 0,
-@code{taylor(expr, x-y)} can be used.
-
-@item time(0)
-Return the current (wallclock) time in seconds.
-
-@item trunc(expr)
-Round the value of expression @var{expr} towards zero to the nearest
-integer. For example, "trunc(-1.5)" is "-1.0".
-
-@item while(cond, expr)
-Evaluate expression @var{expr} while the expression @var{cond} is
-non-zero, and returns the value of the last @var{expr} evaluation, or
-NAN if @var{cond} was always false.
-@end table
-
-The following constants are available:
-@table @option
-@item PI
-area of the unit disc, approximately 3.14
-@item E
-exp(1) (Euler's number), approximately 2.718
-@item PHI
-golden ratio (1+sqrt(5))/2, approximately 1.618
-@end table
-
-Assuming that an expression is considered "true" if it has a non-zero
-value, note that:
-
-@code{*} works like AND
-
-@code{+} works like OR
-
-For example the construct:
-@example
-if (A AND B) then C
-@end example
-is equivalent to:
-@example
-if(A*B, C)
-@end example
-
-In your C code, you can extend the list of unary and binary functions,
-and define recognized constants, so that they are available for your
-expressions.
-
-The evaluator also recognizes the International System unit prefixes.
-If 'i' is appended after the prefix, binary prefixes are used, which
-are based on powers of 1024 instead of powers of 1000.
-The 'B' postfix multiplies the value by 8, and can be appended after a
-unit prefix or used alone. This allows using for example 'KB', 'MiB',
-'G' and 'B' as number postfix.
-
-The list of available International System prefixes follows, with
-indication of the corresponding powers of 10 and of 2.
-@table @option
-@item y
-10^-24 / 2^-80
-@item z
-10^-21 / 2^-70
-@item a
-10^-18 / 2^-60
-@item f
-10^-15 / 2^-50
-@item p
-10^-12 / 2^-40
-@item n
-10^-9 / 2^-30
-@item u
-10^-6 / 2^-20
-@item m
-10^-3 / 2^-10
-@item c
-10^-2
-@item d
-10^-1
-@item h
-10^2
-@item k
-10^3 / 2^10
-@item K
-10^3 / 2^10
-@item M
-10^6 / 2^20
-@item G
-10^9 / 2^30
-@item T
-10^12 / 2^40
-@item P
-10^15 / 2^40
-@item E
-10^18 / 2^50
-@item Z
-10^21 / 2^60
-@item Y
-10^24 / 2^70
-@end table
-
-@c man end
diff --git a/doc/examples/decoding_encoding.c b/doc/examples/decoding_encoding.c
index ae1057c..976b611 100644
--- a/doc/examples/decoding_encoding.c
+++ b/doc/examples/decoding_encoding.c
@@ -79,7 +79,7 @@
 {
     const uint64_t *p;
     uint64_t best_ch_layout = 0;
-    int best_nb_channells   = 0;
+    int best_nb_channels   = 0;
 
     if (!codec->channel_layouts)
         return AV_CH_LAYOUT_STEREO;
@@ -88,9 +88,9 @@
     while (*p) {
         int nb_channels = av_get_channel_layout_nb_channels(*p);
 
-        if (nb_channels > best_nb_channells) {
+        if (nb_channels > best_nb_channels) {
             best_ch_layout    = *p;
-            best_nb_channells = nb_channels;
+            best_nb_channels = nb_channels;
         }
         p++;
     }
diff --git a/doc/examples/demuxing.c b/doc/examples/demuxing.c
index 6780e07..8a1b69b 100644
--- a/doc/examples/demuxing.c
+++ b/doc/examples/demuxing.c
@@ -98,7 +98,7 @@
                    audio_frame_count++, frame->nb_samples,
                    av_ts2timestr(frame->pts, &audio_dec_ctx->time_base));
 
-            ret = av_samples_alloc(audio_dst_data, &audio_dst_linesize, frame->channels,
+            ret = av_samples_alloc(audio_dst_data, &audio_dst_linesize, av_frame_get_channels(frame),
                                    frame->nb_samples, frame->format, 1);
             if (ret < 0) {
                 fprintf(stderr, "Could not allocate audio buffer\n");
@@ -107,13 +107,13 @@
 
             /* TODO: extend return code of the av_samples_* functions so that this call is not needed */
             audio_dst_bufsize =
-                av_samples_get_buffer_size(NULL, frame->channels,
+                av_samples_get_buffer_size(NULL, av_frame_get_channels(frame),
                                            frame->nb_samples, frame->format, 1);
 
             /* copy audio data to destination buffer:
              * this is required since rawaudio expects non aligned data */
             av_samples_copy(audio_dst_data, frame->data, 0, 0,
-                            frame->nb_samples, frame->channels, frame->format);
+                            frame->nb_samples, av_frame_get_channels(frame), frame->format);
 
             /* write to rawaudio file */
             fwrite(audio_dst_data[0], 1, audio_dst_bufsize, audio_dst_file);
diff --git a/doc/examples/filtering_audio.c b/doc/examples/filtering_audio.c
index 6f70a8d..b6b05a2 100644
--- a/doc/examples/filtering_audio.c
+++ b/doc/examples/filtering_audio.c
@@ -36,8 +36,9 @@
 #include <libavfilter/avcodec.h>
 #include <libavfilter/buffersink.h>
 #include <libavfilter/buffersrc.h>
+#include <libavutil/opt.h>
 
-const char *filter_descr = "aresample=8000,aconvert=s16:mono";
+const char *filter_descr = "aresample=8000,aformat=sample_fmts=s16:channel_layouts=mono";
 const char *player       = "ffplay -f s16le -ar 8000 -ac 1 -";
 
 static AVFormatContext *fmt_ctx;
@@ -70,6 +71,7 @@
     }
     audio_stream_index = ret;
     dec_ctx = fmt_ctx->streams[audio_stream_index]->codec;
+    av_opt_set_int(dec_ctx, "refcounted_frames", 1, 0);
 
     /* init the audio decoder */
     if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
@@ -85,11 +87,12 @@
     char args[512];
     int ret;
     AVFilter *abuffersrc  = avfilter_get_by_name("abuffer");
-    AVFilter *abuffersink = avfilter_get_by_name("ffabuffersink");
+    AVFilter *abuffersink = avfilter_get_by_name("abuffersink");
     AVFilterInOut *outputs = avfilter_inout_alloc();
     AVFilterInOut *inputs  = avfilter_inout_alloc();
-    const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, -1 };
-    AVABufferSinkParams *abuffersink_params;
+    const enum AVSampleFormat out_sample_fmts[] = { AV_SAMPLE_FMT_S16, -1 };
+    const int64_t out_channel_layouts[] = { AV_CH_LAYOUT_MONO, -1 };
+    const int out_sample_rates[] = { 8000, -1 };
     const AVFilterLink *outlink;
     AVRational time_base = fmt_ctx->streams[audio_stream_index]->time_base;
 
@@ -110,16 +113,34 @@
     }
 
     /* buffer audio sink: to terminate the filter chain. */
-    abuffersink_params = av_abuffersink_params_alloc();
-    abuffersink_params->sample_fmts     = sample_fmts;
     ret = avfilter_graph_create_filter(&buffersink_ctx, abuffersink, "out",
-                                       NULL, abuffersink_params, filter_graph);
-    av_free(abuffersink_params);
+                                       NULL, NULL, filter_graph);
     if (ret < 0) {
         av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n");
         return ret;
     }
 
+    ret = av_opt_set_int_list(buffersink_ctx, "sample_fmts", out_sample_fmts, -1,
+                              AV_OPT_SEARCH_CHILDREN);
+    if (ret < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot set output sample format\n");
+        return ret;
+    }
+
+    ret = av_opt_set_int_list(buffersink_ctx, "channel_layouts", out_channel_layouts, -1,
+                              AV_OPT_SEARCH_CHILDREN);
+    if (ret < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot set output channel layout\n");
+        return ret;
+    }
+
+    ret = av_opt_set_int_list(buffersink_ctx, "sample_rates", out_sample_rates, -1,
+                              AV_OPT_SEARCH_CHILDREN);
+    if (ret < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot set output sample rate\n");
+        return ret;
+    }
+
     /* Endpoints for the filter graph. */
     outputs->name       = av_strdup("in");
     outputs->filter_ctx = buffersrc_ctx;
@@ -150,11 +171,10 @@
     return 0;
 }
 
-static void print_samplesref(AVFilterBufferRef *samplesref)
+static void print_frame(const AVFrame *frame)
 {
-    const AVFilterBufferRefAudioProps *props = samplesref->audio;
-    const int n = props->nb_samples * av_get_channel_layout_nb_channels(props->channel_layout);
-    const uint16_t *p     = (uint16_t*)samplesref->data[0];
+    const int n = frame->nb_samples * av_get_channel_layout_nb_channels(av_frame_get_channel_layout(frame));
+    const uint16_t *p     = (uint16_t*)frame->data[0];
     const uint16_t *p_end = p + n;
 
     while (p < p_end) {
@@ -169,10 +189,11 @@
 {
     int ret;
     AVPacket packet;
-    AVFrame *frame = avcodec_alloc_frame();
+    AVFrame *frame = av_frame_alloc();
+    AVFrame *filt_frame = av_frame_alloc();
     int got_frame;
 
-    if (!frame) {
+    if (!frame || !filt_frame) {
         perror("Could not allocate frame");
         exit(1);
     }
@@ -192,7 +213,6 @@
 
     /* read all packets */
     while (1) {
-        AVFilterBufferRef *samplesref;
         if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
             break;
 
@@ -207,22 +227,20 @@
 
             if (got_frame) {
                 /* push the audio data from decoded frame into the filtergraph */
-                if (av_buffersrc_add_frame(buffersrc_ctx, frame, 0) < 0) {
+                if (av_buffersrc_add_frame_flags(buffersrc_ctx, frame, 0) < 0) {
                     av_log(NULL, AV_LOG_ERROR, "Error while feeding the audio filtergraph\n");
                     break;
                 }
 
                 /* pull filtered audio from the filtergraph */
                 while (1) {
-                    ret = av_buffersink_get_buffer_ref(buffersink_ctx, &samplesref, 0);
+                    ret = av_buffersink_get_frame(buffersink_ctx, filt_frame);
                     if(ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
                         break;
                     if(ret < 0)
                         goto end;
-                    if (samplesref) {
-                        print_samplesref(samplesref);
-                        avfilter_unref_bufferp(&samplesref);
-                    }
+                    print_frame(filt_frame);
+                    av_frame_unref(filt_frame);
                 }
             }
         }
@@ -233,7 +251,8 @@
     if (dec_ctx)
         avcodec_close(dec_ctx);
     avformat_close_input(&fmt_ctx);
-    av_freep(&frame);
+    av_frame_free(&frame);
+    av_frame_free(&filt_frame);
 
     if (ret < 0 && ret != AVERROR_EOF) {
         char buf[1024];
diff --git a/doc/examples/filtering_video.c b/doc/examples/filtering_video.c
index 660e526..daa3966 100644
--- a/doc/examples/filtering_video.c
+++ b/doc/examples/filtering_video.c
@@ -85,7 +85,7 @@
     char args[512];
     int ret;
     AVFilter *buffersrc  = avfilter_get_by_name("buffer");
-    AVFilter *buffersink = avfilter_get_by_name("ffbuffersink");
+    AVFilter *buffersink = avfilter_get_by_name("buffersink");
     AVFilterInOut *outputs = avfilter_inout_alloc();
     AVFilterInOut *inputs  = avfilter_inout_alloc();
     enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE };
@@ -138,33 +138,33 @@
     return 0;
 }
 
-static void display_picref(AVFilterBufferRef *picref, AVRational time_base)
+static void display_frame(const AVFrame *frame, AVRational time_base)
 {
     int x, y;
     uint8_t *p0, *p;
     int64_t delay;
 
-    if (picref->pts != AV_NOPTS_VALUE) {
+    if (frame->pts != AV_NOPTS_VALUE) {
         if (last_pts != AV_NOPTS_VALUE) {
             /* sleep roughly the right amount of time;
              * usleep is in microseconds, just like AV_TIME_BASE. */
-            delay = av_rescale_q(picref->pts - last_pts,
+            delay = av_rescale_q(frame->pts - last_pts,
                                  time_base, AV_TIME_BASE_Q);
             if (delay > 0 && delay < 1000000)
                 usleep(delay);
         }
-        last_pts = picref->pts;
+        last_pts = frame->pts;
     }
 
     /* Trivial ASCII grayscale display. */
-    p0 = picref->data[0];
+    p0 = frame->data[0];
     puts("\033c");
-    for (y = 0; y < picref->video->h; y++) {
+    for (y = 0; y < frame->height; y++) {
         p = p0;
-        for (x = 0; x < picref->video->w; x++)
+        for (x = 0; x < frame->width; x++)
             putchar(" .-+#"[*(p++) / 52]);
         putchar('\n');
-        p0 += picref->linesize[0];
+        p0 += frame->linesize[0];
     }
     fflush(stdout);
 }
@@ -173,10 +173,11 @@
 {
     int ret;
     AVPacket packet;
-    AVFrame *frame = avcodec_alloc_frame();
+    AVFrame *frame = av_frame_alloc();
+    AVFrame *filt_frame = av_frame_alloc();
     int got_frame;
 
-    if (!frame) {
+    if (!frame || !filt_frame) {
         perror("Could not allocate frame");
         exit(1);
     }
@@ -196,7 +197,6 @@
 
     /* read all packets */
     while (1) {
-        AVFilterBufferRef *picref;
         if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
             break;
 
@@ -213,23 +213,20 @@
                 frame->pts = av_frame_get_best_effort_timestamp(frame);
 
                 /* push the decoded frame into the filtergraph */
-                if (av_buffersrc_add_frame(buffersrc_ctx, frame, 0) < 0) {
+                if (av_buffersrc_add_frame_flags(buffersrc_ctx, frame, AV_BUFFERSRC_FLAG_KEEP_REF) < 0) {
                     av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
                     break;
                 }
 
-                /* pull filtered pictures from the filtergraph */
+                /* pull filtered frames from the filtergraph */
                 while (1) {
-                    ret = av_buffersink_get_buffer_ref(buffersink_ctx, &picref, 0);
+                    ret = av_buffersink_get_frame(buffersink_ctx, filt_frame);
                     if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
                         break;
                     if (ret < 0)
                         goto end;
-
-                    if (picref) {
-                        display_picref(picref, buffersink_ctx->inputs[0]->time_base);
-                        avfilter_unref_bufferp(&picref);
-                    }
+                    display_frame(filt_frame, buffersink_ctx->inputs[0]->time_base);
+                    av_frame_unref(filt_frame);
                 }
             }
         }
@@ -240,7 +237,8 @@
     if (dec_ctx)
         avcodec_close(dec_ctx);
     avformat_close_input(&fmt_ctx);
-    av_freep(&frame);
+    av_frame_free(&frame);
+    av_frame_free(&filt_frame);
 
     if (ret < 0 && ret != AVERROR_EOF) {
         char buf[1024];
diff --git a/doc/examples/muxing.c b/doc/examples/muxing.c
index 8469b27..7305cc6 100644
--- a/doc/examples/muxing.c
+++ b/doc/examples/muxing.c
@@ -86,7 +86,6 @@
         break;
 
     case AVMEDIA_TYPE_VIDEO:
-        avcodec_get_context_defaults3(c, *codec);
         c->codec_id = codec_id;
 
         c->bit_rate = 400000;
@@ -343,25 +342,19 @@
 
         ret = av_interleaved_write_frame(oc, &pkt);
     } else {
-        /* encode the image */
-        AVPacket pkt;
-        int got_output;
-
+        AVPacket pkt = { 0 };
+        int got_packet;
         av_init_packet(&pkt);
-        pkt.data = NULL;    // packet data will be allocated by the encoder
-        pkt.size = 0;
 
-        ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
+        /* encode the image */
+        ret = avcodec_encode_video2(c, &pkt, frame, &got_packet);
         if (ret < 0) {
             fprintf(stderr, "Error encoding video frame: %s\n", av_err2str(ret));
             exit(1);
         }
-
         /* If size is zero, it means the image was buffered. */
-        if (got_output) {
-            if (c->coded_frame->key_frame)
-                pkt.flags |= AV_PKT_FLAG_KEY;
 
+        if (!ret && got_packet && pkt.size) {
             pkt.stream_index = st->index;
 
             /* Write the compressed frame to the media file. */
@@ -396,7 +389,7 @@
     AVStream *audio_st, *video_st;
     AVCodec *audio_codec, *video_codec;
     double audio_pts, video_pts;
-    int ret, i;
+    int ret;
 
     /* Initialize libavcodec, and register all codecs and formats. */
     av_register_all();
@@ -504,18 +497,12 @@
     if (audio_st)
         close_audio(oc, audio_st);
 
-    /* Free the streams. */
-    for (i = 0; i < oc->nb_streams; i++) {
-        av_freep(&oc->streams[i]->codec);
-        av_freep(&oc->streams[i]);
-    }
-
     if (!(fmt->flags & AVFMT_NOFILE))
         /* Close the output file. */
         avio_close(oc->pb);
 
     /* free the stream */
-    av_free(oc);
+    avformat_free_context(oc);
 
     return 0;
 }
diff --git a/doc/examples/resampling_audio.c b/doc/examples/resampling_audio.c
index dd128e8..70db9ef 100644
--- a/doc/examples/resampling_audio.c
+++ b/doc/examples/resampling_audio.c
@@ -78,18 +78,6 @@
     }
 }
 
-int alloc_samples_array_and_data(uint8_t ***data, int *linesize, int nb_channels,
-                                    int nb_samples, enum AVSampleFormat sample_fmt, int align)
-{
-    int nb_planes = av_sample_fmt_is_planar(sample_fmt) ? nb_channels : 1;
-
-    *data = av_malloc(sizeof(*data) * nb_planes);
-    if (!*data)
-        return AVERROR(ENOMEM);
-    return av_samples_alloc(*data, linesize, nb_channels,
-                            nb_samples, sample_fmt, align);
-}
-
 int main(int argc, char **argv)
 {
     int64_t src_ch_layout = AV_CH_LAYOUT_STEREO, dst_ch_layout = AV_CH_LAYOUT_SURROUND;
@@ -149,8 +137,8 @@
     /* allocate source and destination samples buffers */
 
     src_nb_channels = av_get_channel_layout_nb_channels(src_ch_layout);
-    ret = alloc_samples_array_and_data(&src_data, &src_linesize, src_nb_channels,
-                                       src_nb_samples, src_sample_fmt, 0);
+    ret = av_samples_alloc_array_and_samples(&src_data, &src_linesize, src_nb_channels,
+                                             src_nb_samples, src_sample_fmt, 0);
     if (ret < 0) {
         fprintf(stderr, "Could not allocate source samples\n");
         goto end;
@@ -164,8 +152,8 @@
 
     /* buffer is going to be directly written to a rawaudio file, no alignment */
     dst_nb_channels = av_get_channel_layout_nb_channels(dst_ch_layout);
-    ret = alloc_samples_array_and_data(&dst_data, &dst_linesize, dst_nb_channels,
-                                       dst_nb_samples, dst_sample_fmt, 0);
+    ret = av_samples_alloc_array_and_samples(&dst_data, &dst_linesize, dst_nb_channels,
+                                             dst_nb_samples, dst_sample_fmt, 0);
     if (ret < 0) {
         fprintf(stderr, "Could not allocate destination samples\n");
         goto end;
diff --git a/doc/faq.texi b/doc/faq.texi
index ebf21f5..4b0b09c 100644
--- a/doc/faq.texi
+++ b/doc/faq.texi
@@ -393,17 +393,17 @@
 Use @option{-dumpgraph -} to find out exactly where the channel layout is
 lost.
 
-Most likely, it is through @code{auto-inserted aconvert}. Try to understand
+Most likely, it is through @code{auto-inserted aresample}. Try to understand
 why the converting filter was needed at that place.
 
 Just before the output is a likely place, as @option{-f lavfi} currently
 only support packed S16.
 
-Then insert the correct @code{aconvert} explicitly in the filter graph,
+Then insert the correct @code{aformat} explicitly in the filtergraph,
 specifying the exact format.
 
 @example
-aconvert=s16:stereo:packed
+aformat=sample_fmts=s16:channel_layouts=stereo
 @end example
 
 @section Why does FFmpeg not see the subtitles in my VOB file?
diff --git a/doc/fate.texi b/doc/fate.texi
index 4c2ba4d..28eadfd 100644
--- a/doc/fate.texi
+++ b/doc/fate.texi
@@ -27,7 +27,7 @@
 
   This is especially recommended for all people contributing source
 code to FFmpeg, as it can be seen if some test on some platform broke
-with there recent contribution. This usually happens on the platforms
+with their recent contribution. This usually happens on the platforms
 the developers could not test on.
 
   The second part of this document describes how you can run FATE to
@@ -185,6 +185,8 @@
     The @var{TARGET_EXEC} option provides a way to run FATE wrapped in
     @command{valgrind}, @command{qemu-user} or @command{wine} or on remote targets
     through @command{ssh}.
+@item GEN
+Set to @var{1} to generate the missing or mismatched references.
 @end table
 
 @section Examples
diff --git a/doc/ffmpeg-codecs.texi b/doc/ffmpeg-codecs.texi
index db20aec..6f8f5a3 100644
--- a/doc/ffmpeg-codecs.texi
+++ b/doc/ffmpeg-codecs.texi
@@ -17,1096 +17,7 @@
 
 @c man end DESCRIPTION
 
-@chapter Codec Options
-@c man begin CODEC OPTIONS
-
-libavcodec provides some generic global options, which can be set on
-all the encoders and decoders. In addition each codec may support
-so-called private options, which are specific for a given codec.
-
-Sometimes, a global option may only affect a specific kind of codec,
-and may be unsensical or ignored by another, so you need to be aware
-of the meaning of the specified options. Also some options are
-meant only for decoding or encoding.
-
-Options may be set by specifying -@var{option} @var{value} in the
-FFmpeg tools, or by setting the value explicitly in the
-@code{AVCodecContext} options or using the @file{libavutil/opt.h} API
-for programmatic use.
-
-The list of supported options follow:
-
-@table @option
-@item b @var{integer} (@emph{encoding,audio,video})
-Set bitrate in bits/s. Default value is 200K.
-
-@item ab @var{integer} (@emph{encoding,audio})
-Set audio bitrate (in bits/s). Default value is 128K.
-
-@item bt @var{integer} (@emph{encoding,video})
-Set video bitrate tolerance (in bits/s). In 1-pass mode, bitrate
-tolerance specifies how far ratecontrol is willing to deviate from the
-target average bitrate value. This is not related to min/max
-bitrate. Lowering tolerance too much has an adverse effect on quality.
-
-@item flags @var{flags} (@emph{decoding/encoding,audio,video,subtitles})
-Set generic flags.
-
-Possible values:
-@table @samp
-@item mv4
-Use four motion vector by macroblock (mpeg4).
-@item qpel
-Use 1/4 pel motion compensation.
-@item loop
-Use loop filter.
-@item qscale
-Use fixed qscale.
-@item gmc
-Use gmc.
-@item mv0
-Always try a mb with mv=<0,0>.
-@item input_preserved
-
-@item pass1
-Use internal 2pass ratecontrol in first pass mode.
-@item pass2
-Use internal 2pass ratecontrol in second pass mode.
-@item gray
-Only decode/encode grayscale.
-@item emu_edge
-Do not draw edges.
-@item psnr
-Set error[?] variables during encoding.
-@item truncated
-
-@item naq
-Normalize adaptive quantization.
-@item ildct
-Use interlaced DCT.
-@item low_delay
-Force low delay.
-@item global_header
-Place global headers in extradata instead of every keyframe.
-@item bitexact
-Use only bitexact stuff (except (I)DCT).
-@item aic
-Apply H263 advanced intra coding / mpeg4 ac prediction.
-@item cbp
-Deprecated, use mpegvideo private options instead.
-@item qprd
-Deprecated, use mpegvideo private options instead.
-@item ilme
-Apply interlaced motion estimation.
-@item cgop
-Use closed gop.
-@end table
-
-@item sub_id @var{integer}
-Deprecated, currently unused.
-
-@item me_method @var{integer} (@emph{encoding,video})
-Set motion estimation method.
-
-Possible values:
-@table @samp
-@item zero
-zero motion estimation (fastest)
-@item full
-full motion estimation (slowest)
-@item epzs
-EPZS motion estimation (default)
-@item esa
-esa motion estimation (alias for full)
-@item tesa
-tesa motion estimation
-@item dia
-dia motion estimation (alias for epzs)
-@item log
-log motion estimation
-@item phods
-phods motion estimation
-@item x1
-X1 motion estimation
-@item hex
-hex motion estimation
-@item umh
-umh motion estimation
-@item iter
-iter motion estimation
-@end table
-
-@item extradata_size @var{integer}
-Set extradata size.
-
-@item time_base @var{rational number}
-Set codec time base.
-
-It is the fundamental unit of time (in seconds) in terms of which
-frame timestamps are represented. For fixed-fps content, timebase
-should be 1/framerate and timestamp increments should be identically
-1.
-
-@item g @var{integer} (@emph{encoding,video})
-Set the group of picture size. Default value is 12.
-
-@item ar @var{integer} (@emph{decoding/encoding,audio})
-Set audio sampling rate (in Hz).
-
-@item ac @var{integer} (@emph{decoding/encoding,audio})
-Set number of audio channels.
-
-@item cutoff @var{integer} (@emph{encoding,audio})
-Set cutoff bandwidth.
-
-@item frame_size @var{integer} (@emph{encoding,audio})
-Set audio frame size.
-
-Each submitted frame except the last must contain exactly frame_size
-samples per channel. May be 0 when the codec has
-CODEC_CAP_VARIABLE_FRAME_SIZE set, in that case the frame size is not
-restricted. It is set by some decoders to indicate constant frame
-size.
-
-@item frame_number @var{integer}
-Set the frame number.
-
-@item delay @var{integer}
-
-@item qcomp @var{float} (@emph{encoding,video})
-Set video quantizer scale compression (VBR). It is used as a constant
-in the ratecontrol equation. Recommended range for default rc_eq:
-0.0-1.0.
-
-@item qblur @var{float} (@emph{encoding,video})
-Set video quantizer scale blur (VBR).
-
-@item qmin @var{integer} (@emph{encoding,video})
-Set min video quantizer scale (VBR). Must be included between -1 and
-69, default value is 2.
-
-@item qmax @var{integer} (@emph{encoding,video})
-Set max video quantizer scale (VBR). Must be included between -1 and
-1024, default value is 31.
-
-@item qdiff @var{integer} (@emph{encoding,video})
-Set max difference between the quantizer scale (VBR).
-
-@item bf @var{integer} (@emph{encoding,video})
-Set max number of B frames.
-
-@item b_qfactor @var{float} (@emph{encoding,video})
-Set qp factor between P and B frames.
-
-@item rc_strategy @var{integer} (@emph{encoding,video})
-Set ratecontrol method.
-
-@item b_strategy @var{integer} (@emph{encoding,video})
-Set strategy to choose between I/P/B-frames.
-
-@item ps @var{integer} (@emph{encoding,video})
-Set RTP payload size in bytes.
-
-@item mv_bits @var{integer}
-@item header_bits @var{integer}
-@item i_tex_bits @var{integer}
-@item p_tex_bits @var{integer}
-@item i_count @var{integer}
-@item p_count @var{integer}
-@item skip_count @var{integer}
-@item misc_bits @var{integer}
-@item frame_bits @var{integer}
-@item codec_tag @var{integer}
-@item bug @var{flags} (@emph{decoding,video})
-Workaround not auto detected encoder bugs.
-
-Possible values:
-@table @samp
-@item autodetect
-
-@item old_msmpeg4
-some old lavc generated msmpeg4v3 files (no autodetection)
-@item xvid_ilace
-Xvid interlacing bug (autodetected if fourcc==XVIX)
-@item ump4
-(autodetected if fourcc==UMP4)
-@item no_padding
-padding bug (autodetected)
-@item amv
-
-@item ac_vlc
-illegal vlc bug (autodetected per fourcc)
-@item qpel_chroma
-
-@item std_qpel
-old standard qpel (autodetected per fourcc/version)
-@item qpel_chroma2
-
-@item direct_blocksize
-direct-qpel-blocksize bug (autodetected per fourcc/version)
-@item edge
-edge padding bug (autodetected per fourcc/version)
-@item hpel_chroma
-
-@item dc_clip
-
-@item ms
-Workaround various bugs in microsoft broken decoders.
-@item trunc
-trancated frames
-@end table
-
-@item lelim @var{integer} (@emph{encoding,video})
-Set single coefficient elimination threshold for luminance (negative
-values also consider DC coefficient).
-
-@item celim @var{integer} (@emph{encoding,video})
-Set single coefficient elimination threshold for chrominance (negative
-values also consider dc coefficient)
-
-@item strict @var{integer} (@emph{decoding/encoding,audio,video})
-Specify how strictly to follow the standards.
-
-Possible values:
-@table @samp
-@item very
-strictly conform to a older more strict version of the spec or reference software
-@item strict
-strictly conform to all the things in the spec no matter what consequences
-@item normal
-
-@item unofficial
-allow unofficial extensions
-@item experimental
-allow non standardized experimental things
-@end table
-
-@item b_qoffset @var{float} (@emph{encoding,video})
-Set QP offset between P and B frames.
-
-@item err_detect @var{flags} (@emph{decoding,audio,video})
-Set error detection flags.
-
-Possible values:
-@table @samp
-@item crccheck
-verify embedded CRCs
-@item bitstream
-detect bitstream specification deviations
-@item buffer
-detect improper bitstream length
-@item explode
-abort decoding on minor error detection
-@item careful
-consider things that violate the spec and have not been seen in the wild as errors
-@item compliant
-consider all spec non compliancies as errors
-@item aggressive
-consider things that a sane encoder should not do as an error
-@end table
-
-@item has_b_frames @var{integer}
-
-@item block_align @var{integer}
-
-@item mpeg_quant @var{integer} (@emph{encoding,video})
-Use MPEG quantizers instead of H.263.
-
-@item qsquish @var{float} (@emph{encoding,video})
-How to keep quantizer between qmin and qmax (0 = clip, 1 = use
-differentiable function).
-
-@item rc_qmod_amp @var{float} (@emph{encoding,video})
-Set experimental quantizer modulation.
-
-@item rc_qmod_freq @var{integer} (@emph{encoding,video})
-Set experimental quantizer modulation.
-
-@item rc_override_count @var{integer}
-
-@item rc_eq @var{string} (@emph{encoding,video})
-Set rate control equation. When computing the expression, besides the
-standard functions defined in the section 'Expression Evaluation', the
-following functions are available: bits2qp(bits), qp2bits(qp). Also
-the following constants are available: iTex pTex tex mv fCode iCount
-mcVar var isI isP isB avgQP qComp avgIITex avgPITex avgPPTex avgBPTex
-avgTex.
-
-@item maxrate @var{integer} (@emph{encoding,audio,video})
-Set max bitrate tolerance (in bits/s). Requires bufsize to be set.
-
-@item minrate @var{integer} (@emph{encoding,audio,video})
-Set min bitrate tolerance (in bits/s). Most useful in setting up a CBR
-encode. It is of little use elsewise.
-
-@item bufsize @var{integer} (@emph{encoding,audio,video})
-Set ratecontrol buffer size (in bits).
-
-@item rc_buf_aggressivity @var{float} (@emph{encoding,video})
-Currently useless.
-
-@item i_qfactor @var{float} (@emph{encoding,video})
-Set QP factor between P and I frames.
-
-@item i_qoffset @var{float} (@emph{encoding,video})
-Set QP offset between P and I frames.
-
-@item rc_init_cplx @var{float} (@emph{encoding,video})
-Set initial complexity for 1-pass encoding.
-
-@item dct @var{integer} (@emph{encoding,video})
-Set DCT algorithm.
-
-Possible values:
-@table @samp
-@item auto
-autoselect a good one (default)
-@item fastint
-fast integer
-@item int
-accurate integer
-@item mmx
-
-@item altivec
-
-@item faan
-floating point AAN DCT
-@end table
-
-@item lumi_mask @var{float} (@emph{encoding,video})
-Compress bright areas stronger than medium ones.
-
-@item tcplx_mask @var{float} (@emph{encoding,video})
-Set temporal complexity masking.
-
-@item scplx_mask @var{float} (@emph{encoding,video})
-Set spatial complexity masking.
-
-@item p_mask @var{float} (@emph{encoding,video})
-Set inter masking.
-
-@item dark_mask @var{float} (@emph{encoding,video})
-Compress dark areas stronger than medium ones.
-
-@item idct @var{integer} (@emph{decoding/encoding,video})
-Select IDCT implementation.
-
-Possible values:
-@table @samp
-@item auto
-
-@item int
-
-@item simple
-
-@item simplemmx
-
-@item libmpeg2mmx
-
-@item mmi
-
-@item arm
-
-@item altivec
-
-@item sh4
-
-@item simplearm
-
-@item simplearmv5te
-
-@item simplearmv6
-
-@item simpleneon
-
-@item simplealpha
-
-@item h264
-
-@item vp3
-
-@item ipp
-
-@item xvidmmx
-
-@item faani
-floating point AAN IDCT
-@end table
-
-@item slice_count @var{integer}
-
-@item ec @var{flags} (@emph{decoding,video})
-Set error concealment strategy.
-
-Possible values:
-@table @samp
-@item guess_mvs
-iterative motion vector (MV) search (slow)
-@item deblock
-use strong deblock filter for damaged MBs
-@end table
-
-@item bits_per_coded_sample @var{integer}
-
-@item pred @var{integer} (@emph{encoding,video})
-Set prediction method.
-
-Possible values:
-@table @samp
-@item left
-
-@item plane
-
-@item median
-
-@end table
-
-@item aspect @var{rational number} (@emph{encoding,video})
-Set sample aspect ratio.
-
-@item debug @var{flags} (@emph{decoding/encoding,audio,video,subtitles})
-Print specific debug info.
-
-Possible values:
-@table @samp
-@item pict
-picture info
-@item rc
-rate control
-@item bitstream
-
-@item mb_type
-macroblock (MB) type
-@item qp
-per-block quantization parameter (QP)
-@item mv
-motion vector
-@item dct_coeff
-
-@item skip
-
-@item startcode
-
-@item pts
-
-@item er
-error recognition
-@item mmco
-memory management control operations (H.264)
-@item bugs
-
-@item vis_qp
-visualize quantization parameter (QP), lower QP are tinted greener
-@item vis_mb_type
-visualize block types
-@item buffers
-picture buffer allocations
-@item thread_ops
-threading operations
-@end table
-
-@item vismv @var{integer} (@emph{decoding,video})
-Visualize motion vectors (MVs).
-
-Possible values:
-@table @samp
-@item pf
-forward predicted MVs of P-frames
-@item bf
-forward predicted MVs of B-frames
-@item bb
-backward predicted MVs of B-frames
-@end table
-
-@item cmp @var{integer} (@emph{encoding,video})
-Set full pel me compare function.
-
-Possible values:
-@table @samp
-@item sad
-sum of absolute differences, fast (default)
-@item sse
-sum of squared errors
-@item satd
-sum of absolute Hadamard transformed differences
-@item dct
-sum of absolute DCT transformed differences
-@item psnr
-sum of squared quantization errors (avoid, low quality)
-@item bit
-number of bits needed for the block
-@item rd
-rate distortion optimal, slow
-@item zero
-0
-@item vsad
-sum of absolute vertical differences
-@item vsse
-sum of squared vertical differences
-@item nsse
-noise preserving sum of squared differences
-@item w53
-5/3 wavelet, only used in snow
-@item w97
-9/7 wavelet, only used in snow
-@item dctmax
-
-@item chroma
-
-@end table
-
-@item subcmp @var{integer} (@emph{encoding,video})
-Set sub pel me compare function.
-
-Possible values:
-@table @samp
-@item sad
-sum of absolute differences, fast (default)
-@item sse
-sum of squared errors
-@item satd
-sum of absolute Hadamard transformed differences
-@item dct
-sum of absolute DCT transformed differences
-@item psnr
-sum of squared quantization errors (avoid, low quality)
-@item bit
-number of bits needed for the block
-@item rd
-rate distortion optimal, slow
-@item zero
-0
-@item vsad
-sum of absolute vertical differences
-@item vsse
-sum of squared vertical differences
-@item nsse
-noise preserving sum of squared differences
-@item w53
-5/3 wavelet, only used in snow
-@item w97
-9/7 wavelet, only used in snow
-@item dctmax
-
-@item chroma
-
-@end table
-
-@item mbcmp @var{integer} (@emph{encoding,video})
-Set macroblock compare function.
-
-Possible values:
-@table @samp
-@item sad
-sum of absolute differences, fast (default)
-@item sse
-sum of squared errors
-@item satd
-sum of absolute Hadamard transformed differences
-@item dct
-sum of absolute DCT transformed differences
-@item psnr
-sum of squared quantization errors (avoid, low quality)
-@item bit
-number of bits needed for the block
-@item rd
-rate distortion optimal, slow
-@item zero
-0
-@item vsad
-sum of absolute vertical differences
-@item vsse
-sum of squared vertical differences
-@item nsse
-noise preserving sum of squared differences
-@item w53
-5/3 wavelet, only used in snow
-@item w97
-9/7 wavelet, only used in snow
-@item dctmax
-
-@item chroma
-
-@end table
-
-@item ildctcmp @var{integer} (@emph{encoding,video})
-Set interlaced dct compare function.
-
-Possible values:
-@table @samp
-@item sad
-sum of absolute differences, fast (default)
-@item sse
-sum of squared errors
-@item satd
-sum of absolute Hadamard transformed differences
-@item dct
-sum of absolute DCT transformed differences
-@item psnr
-sum of squared quantization errors (avoid, low quality)
-@item bit
-number of bits needed for the block
-@item rd
-rate distortion optimal, slow
-@item zero
-0
-@item vsad
-sum of absolute vertical differences
-@item vsse
-sum of squared vertical differences
-@item nsse
-noise preserving sum of squared differences
-@item w53
-5/3 wavelet, only used in snow
-@item w97
-9/7 wavelet, only used in snow
-@item dctmax
-
-@item chroma
-
-@end table
-
-@item dia_size @var{integer} (@emph{encoding,video})
-Set diamond type & size for motion estimation.
-
-@item last_pred @var{integer} (@emph{encoding,video})
-Set amount of motion predictors from the previous frame.
-
-@item preme @var{integer} (@emph{encoding,video})
-Set pre motion estimation.
-
-@item precmp @var{integer} (@emph{encoding,video})
-Set pre motion estimation compare function.
-
-Possible values:
-@table @samp
-@item sad
-sum of absolute differences, fast (default)
-@item sse
-sum of squared errors
-@item satd
-sum of absolute Hadamard transformed differences
-@item dct
-sum of absolute DCT transformed differences
-@item psnr
-sum of squared quantization errors (avoid, low quality)
-@item bit
-number of bits needed for the block
-@item rd
-rate distortion optimal, slow
-@item zero
-0
-@item vsad
-sum of absolute vertical differences
-@item vsse
-sum of squared vertical differences
-@item nsse
-noise preserving sum of squared differences
-@item w53
-5/3 wavelet, only used in snow
-@item w97
-9/7 wavelet, only used in snow
-@item dctmax
-
-@item chroma
-
-@end table
-
-@item pre_dia_size @var{integer} (@emph{encoding,video})
-Set diamond type & size for motion estimation pre-pass.
-
-@item subq @var{integer} (@emph{encoding,video})
-Set sub pel motion estimation quality.
-
-@item dtg_active_format @var{integer}
-
-@item me_range @var{integer} (@emph{encoding,video})
-Set limit motion vectors range (1023 for DivX player).
-
-@item ibias @var{integer} (@emph{encoding,video})
-Set intra quant bias.
-
-@item pbias @var{integer} (@emph{encoding,video})
-Set inter quant bias.
-
-@item color_table_id @var{integer}
-
-@item global_quality @var{integer} (@emph{encoding,audio,video})
-
-@item coder @var{integer} (@emph{encoding,video})
-
-Possible values:
-@table @samp
-@item vlc
-variable length coder / huffman coder
-@item ac
-arithmetic coder
-@item raw
-raw (no encoding)
-@item rle
-run-length coder
-@item deflate
-deflate-based coder
-@end table
-
-@item context @var{integer} (@emph{encoding,video})
-Set context model.
-
-@item slice_flags @var{integer}
-
-@item xvmc_acceleration @var{integer}
-
-@item mbd @var{integer} (@emph{encoding,video})
-Set macroblock decision algorithm (high quality mode).
-
-Possible values:
-@table @samp
-@item simple
-use mbcmp (default)
-@item bits
-use fewest bits
-@item rd
-use best rate distortion
-@end table
-
-@item stream_codec_tag @var{integer}
-
-@item sc_threshold @var{integer} (@emph{encoding,video})
-Set scene change threshold.
-
-@item lmin @var{integer} (@emph{encoding,video})
-Set min lagrange factor (VBR).
-
-@item lmax @var{integer} (@emph{encoding,video})
-Set max lagrange factor (VBR).
-
-@item nr @var{integer} (@emph{encoding,video})
-Set noise reduction.
-
-@item rc_init_occupancy @var{integer} (@emph{encoding,video})
-Set number of bits which should be loaded into the rc buffer before
-decoding starts.
-
-@item inter_threshold @var{integer} (@emph{encoding,video})
-
-@item flags2 @var{flags} (@emph{decoding/encoding,audio,video})
-
-Possible values:
-@table @samp
-@item fast
-allow non spec compliant speedup tricks
-@item sgop
-Deprecated, use mpegvideo private options instead
-@item noout
-skip bitstream encoding
-@item local_header
-place global headers at every keyframe instead of in extradata
-@item chunks
-Frame data might be split into multiple chunks
-@item showall
-Show all frames before the first keyframe
-@item skiprd
-Deprecated, use mpegvideo private options instead
-@end table
-
-@item error @var{integer} (@emph{encoding,video})
-
-@item qns @var{integer} (@emph{encoding,video})
-Deprecated, use mpegvideo private options instead.
-
-@item threads @var{integer} (@emph{decoding/encoding,video})
-
-Possible values:
-@table @samp
-@item auto
-detect a good number of threads
-@end table
-
-@item me_threshold @var{integer} (@emph{encoding,video})
-Set motion estimation threshold.
-
-@item mb_threshold @var{integer} (@emph{encoding,video})
-Set macroblock threshold.
-
-@item dc @var{integer} (@emph{encoding,video})
-Set intra_dc_precision.
-
-@item nssew @var{integer} (@emph{encoding,video})
-Set nsse weight.
-
-@item skip_top @var{integer} (@emph{decoding,video})
-Set number of macroblock rows at the top which are skipped.
-
-@item skip_bottom @var{integer} (@emph{decoding,video})
-Set number of macroblock rows at the bottom which are skipped.
-
-@item profile @var{integer} (@emph{encoding,audio,video})
-
-Possible values:
-@table @samp
-@item unknown
-
-@item aac_main
-
-@item aac_low
-
-@item aac_ssr
-
-@item aac_ltp
-
-@item aac_he
-
-@item aac_he_v2
-
-@item aac_ld
-
-@item aac_eld
-
-@item dts
-
-@item dts_es
-
-@item dts_96_24
-
-@item dts_hd_hra
-
-@item dts_hd_ma
-
-@end table
-
-@item level @var{integer} (@emph{encoding,audio,video})
-
-Possible values:
-@table @samp
-@item unknown
-
-@end table
-
-@item lowres @var{integer} (@emph{decoding,audio,video})
-Decode at 1= 1/2, 2=1/4, 3=1/8 resolutions.
-
-@item skip_threshold @var{integer} (@emph{encoding,video})
-Set frame skip threshold.
-
-@item skip_factor @var{integer} (@emph{encoding,video})
-Set frame skip factor.
-
-@item skip_exp @var{integer} (@emph{encoding,video})
-Set frame skip exponent.
-
-@item skipcmp @var{integer} (@emph{encoding,video})
-Set frame skip compare function.
-
-Possible values:
-@table @samp
-@item sad
-sum of absolute differences, fast (default)
-@item sse
-sum of squared errors
-@item satd
-sum of absolute Hadamard transformed differences
-@item dct
-sum of absolute DCT transformed differences
-@item psnr
-sum of squared quantization errors (avoid, low quality)
-@item bit
-number of bits needed for the block
-@item rd
-rate distortion optimal, slow
-@item zero
-0
-@item vsad
-sum of absolute vertical differences
-@item vsse
-sum of squared vertical differences
-@item nsse
-noise preserving sum of squared differences
-@item w53
-5/3 wavelet, only used in snow
-@item w97
-9/7 wavelet, only used in snow
-@item dctmax
-
-@item chroma
-
-@end table
-
-@item border_mask @var{float} (@emph{encoding,video})
-Increase the quantizer for macroblocks close to borders.
-
-@item mblmin @var{integer} (@emph{encoding,video})
-Set min macroblock lagrange factor (VBR).
-
-@item mblmax @var{integer} (@emph{encoding,video})
-Set max macroblock lagrange factor (VBR).
-
-@item mepc @var{integer} (@emph{encoding,video})
-Set motion estimation bitrate penalty compensation (1.0 = 256).
-
-@item skip_loop_filter @var{integer} (@emph{decoding,video})
-
-Possible values:
-@table @samp
-@item none
-
-@item default
-
-@item noref
-
-@item bidir
-
-@item nokey
-
-@item all
-
-@end table
-
-@item skip_idct @var{integer} (@emph{decoding,video})
-
-Possible values:
-@table @samp
-@item none
-
-@item default
-
-@item noref
-
-@item bidir
-
-@item nokey
-
-@item all
-
-@end table
-
-@item skip_frame @var{integer} (@emph{decoding,video})
-
-Possible values:
-@table @samp
-@item none
-
-@item default
-
-@item noref
-
-@item bidir
-
-@item nokey
-
-@item all
-
-@end table
-
-@item bidir_refine @var{integer} (@emph{encoding,video})
-Refine the two motion vectors used in bidirectional macroblocks.
-
-@item brd_scale @var{integer} (@emph{encoding,video})
-Downscale frames for dynamic B-frame decision.
-
-@item keyint_min @var{integer} (@emph{encoding,video})
-Set minimum interval between IDR-frames.
-
-@item refs @var{integer} (@emph{encoding,video})
-Set reference frames to consider for motion compensation.
-
-@item chromaoffset @var{integer} (@emph{encoding,video})
-Set chroma qp offset from luma.
-
-@item trellis @var{integer} (@emph{encoding,audio,video})
-Set rate-distortion optimal quantization.
-
-@item sc_factor @var{integer} (@emph{encoding,video})
-Set value multiplied by qscale for each frame and added to
-scene_change_score.
-
-@item mv0_threshold @var{integer} (@emph{encoding,video})
-@item b_sensitivity @var{integer} (@emph{encoding,video})
-Adjust sensitivity of b_frame_strategy 1.
-
-@item compression_level @var{integer} (@emph{encoding,audio,video})
-@item min_prediction_order @var{integer} (@emph{encoding,audio})
-@item max_prediction_order @var{integer} (@emph{encoding,audio})
-@item timecode_frame_start @var{integer} (@emph{encoding,video})
-Set GOP timecode frame start number, in non drop frame format.
-
-@item request_channels @var{integer} (@emph{decoding,audio})
-Set desired number of audio channels.
-
-@item bits_per_raw_sample @var{integer}
-@item channel_layout @var{integer} (@emph{decoding/encoding,audio})
-
-Possible values:
-@table @samp
-@end table
-@item request_channel_layout @var{integer} (@emph{decoding,audio})
-
-Possible values:
-@table @samp
-@end table
-@item rc_max_vbv_use @var{float} (@emph{encoding,video})
-@item rc_min_vbv_use @var{float} (@emph{encoding,video})
-@item ticks_per_frame @var{integer} (@emph{decoding/encoding,audio,video})
-@item color_primaries @var{integer} (@emph{decoding/encoding,video})
-@item color_trc @var{integer} (@emph{decoding/encoding,video})
-@item colorspace @var{integer} (@emph{decoding/encoding,video})
-@item color_range @var{integer} (@emph{decoding/encoding,video})
-@item chroma_sample_location @var{integer} (@emph{decoding/encoding,video})
-
-@item log_level_offset @var{integer}
-Set the log level offset.
-
-@item slices @var{integer} (@emph{encoding,video})
-Number of slices, used in parallelized encoding.
-
-@item thread_type @var{flags} (@emph{decoding/encoding,video})
-Select multithreading type.
-
-Possible values:
-@table @samp
-@item slice
-
-@item frame
-
-@end table
-@item audio_service_type @var{integer} (@emph{encoding,audio})
-Set audio service type.
-
-Possible values:
-@table @samp
-@item ma
-Main Audio Service
-@item ef
-Effects
-@item vi
-Visually Impaired
-@item hi
-Hearing Impaired
-@item di
-Dialogue
-@item co
-Commentary
-@item em
-Emergency
-@item vo
-Voice Over
-@item ka
-Karaoke
-@end table
-
-@item request_sample_fmt @var{sample_fmt} (@emph{decoding,audio})
-Set sample format audio decoders should prefer. Default value is
-@code{none}.
-
-@item pkt_timebase @var{rational number}
-
-@item sub_charenc @var{encoding} (@emph{decoding,subtitles})
-Set the input subtitles character encoding.
-@end table
-
-@c man end CODEC OPTIONS
-
-@include decoders.texi
-@include encoders.texi
+@include codecs.texi
 
 @chapter See Also
 
diff --git a/doc/ffmpeg-devices.texi b/doc/ffmpeg-devices.texi
index 9e004d5..b44bd72 100644
--- a/doc/ffmpeg-devices.texi
+++ b/doc/ffmpeg-devices.texi
@@ -17,27 +17,7 @@
 
 @c man end DESCRIPTION
 
-@chapter Device Options
-@c man begin DEVICE OPTIONS
-
-The libavdevice library provides the same interface as
-libavformat. Namely, an input device is considered like a demuxer, and
-an output device like a muxer, and the interface and generic device
-options are the same provided by libavformat (see the ffmpeg-formats
-manual).
-
-In addition each input or output device may support so-called private
-options, which are specific for that component.
-
-Options may be set by specifying -@var{option} @var{value} in the
-FFmpeg tools, or by setting the value explicitly in the device
-@code{AVFormatContext} options or using the @file{libavutil/opt.h} API
-for programmatic use.
-
-@c man end DEVICE OPTIONS
-
-@include indevs.texi
-@include outdevs.texi
+@include devices.texi
 
 @chapter See Also
 
diff --git a/doc/ffmpeg-formats.texi b/doc/ffmpeg-formats.texi
index 30cf415..e205caa 100644
--- a/doc/ffmpeg-formats.texi
+++ b/doc/ffmpeg-formats.texi
@@ -17,138 +17,7 @@
 
 @c man end DESCRIPTION
 
-@chapter Format Options
-@c man begin FORMAT OPTIONS
-
-The libavformat library provides some generic global options, which
-can be set on all the muxers and demuxers. In addition each muxer or
-demuxer may support so-called private options, which are specific for
-that component.
-
-Options may be set by specifying -@var{option} @var{value} in the
-FFmpeg tools, or by setting the value explicitly in the
-@code{AVFormatContext} options or using the @file{libavutil/opt.h} API
-for programmatic use.
-
-The list of supported options follows:
-
-@table @option
-@item avioflags @var{flags} (@emph{input/output})
-Possible values:
-@table @samp
-@item direct
-Reduce buffering.
-@end table
-
-@item probesize @var{integer} (@emph{input})
-Set probing size in bytes, i.e. the size of the data to analyze to get
-stream information. A higher value will allow to detect more
-information in case it is dispersed into the stream, but will increase
-latency. Must be an integer not lesser than 32. It is 5000000 by default.
-
-@item packetsize @var{integer} (@emph{output})
-Set packet size.
-
-@item fflags @var{flags} (@emph{input/output})
-Set format flags.
-
-Possible values:
-@table @samp
-@item ignidx
-Ignore index.
-@item genpts
-Generate PTS.
-@item nofillin
-Do not fill in missing values that can be exactly calculated.
-@item noparse
-Disable AVParsers, this needs @code{+nofillin} too.
-@item igndts
-Ignore DTS.
-@item discardcorrupt
-Discard corrupted frames.
-@item sortdts
-Try to interleave output packets by DTS.
-@item keepside
-Do not merge side data.
-@item latm
-Enable RTP MP4A-LATM payload.
-@item nobuffer
-Reduce the latency introduced by optional buffering
-@end table
-
-@item analyzeduration @var{integer} (@emph{input})
-Specify how many microseconds are analyzed to probe the input. A
-higher value will allow to detect more accurate information, but will
-increase latency. It defaults to 5,000,000 microseconds = 5 seconds.
-
-@item cryptokey @var{hexadecimal string} (@emph{input})
-Set decryption key.
-
-@item indexmem @var{integer} (@emph{input})
-Set max memory used for timestamp index (per stream).
-
-@item rtbufsize @var{integer} (@emph{input})
-Set max memory used for buffering real-time frames.
-
-@item fdebug @var{flags} (@emph{input/output})
-Print specific debug info.
-
-Possible values:
-@table @samp
-@item ts
-@end table
-
-@item max_delay @var{integer} (@emph{input/output})
-Set maximum muxing or demuxing delay in microseconds.
-
-@item fpsprobesize @var{integer} (@emph{input})
-Set number of frames used to probe fps.
-
-@item audio_preload @var{integer} (@emph{output})
-Set microseconds by which audio packets should be interleaved earlier.
-
-@item chunk_duration @var{integer} (@emph{output})
-Set microseconds for each chunk.
-
-@item chunk_size @var{integer} (@emph{output})
-Set size in bytes for each chunk.
-
-@item err_detect, f_err_detect @var{flags} (@emph{input})
-Set error detection flags. @code{f_err_detect} is deprecated and
-should be used only via the @command{ffmpeg} tool.
-
-Possible values:
-@table @samp
-@item crccheck
-Verify embedded CRCs.
-@item bitstream
-Detect bitstream specification deviations.
-@item buffer
-Detect improper bitstream length.
-@item explode
-Abort decoding on minor error detection.
-@item careful
-Consider things that violate the spec and have not been seen in the
-wild as errors.
-@item compliant
-Consider all spec non compliancies as errors.
-@item aggressive
-Consider things that a sane encoder should not do as an error.
-@end table
-
-@item use_wallclock_as_timestamps @var{integer} (@emph{input})
-Use wallclock as timestamps.
-
-@item avoid_negative_ts @var{integer} (@emph{output})
-Shift timestamps to make them positive. 1 enables, 0 disables, default
-of -1 enables when required by target format.
-@end table
-
-@c man end FORMAT OPTIONS
-
-@include demuxers.texi
-@include muxers.texi
-@include metadata.texi
+@include formats.texi
 
 @chapter See Also
 
diff --git a/doc/ffmpeg-resampler.texi b/doc/ffmpeg-resampler.texi
index 525907a..da3d033 100644
--- a/doc/ffmpeg-resampler.texi
+++ b/doc/ffmpeg-resampler.texi
@@ -19,228 +19,7 @@
 
 @c man end DESCRIPTION
 
-@chapter Resampler Options
-@c man begin RESAMPLER OPTIONS
-
-The audio resampler supports the following named options.
-
-Options may be set by specifying -@var{option} @var{value} in the
-FFmpeg tools, @var{option}=@var{value} for the aresample filter,
-by setting the value explicitly in the
-@code{SwrContext} options or using the @file{libavutil/opt.h} API for
-programmatic use.
-
-@table @option
-
-@item ich, in_channel_count
-Set the number of input channels. Default value is 0. Setting this
-value is not mandatory if the corresponding channel layout
-@option{in_channel_layout} is set.
-
-@item och, out_channel_count
-Set the number of output channels. Default value is 0. Setting this
-value is not mandatory if the corresponding channel layout
-@option{out_channel_layout} is set.
-
-@item uch, used_channel_count
-Set the number of used input channels. Default value is 0. This option is
-only used for special remapping.
-
-@item isr, in_sample_rate
-Set the input sample rate. Default value is 0.
-
-@item osr, out_sample_rate
-Set the output sample rate. Default value is 0.
-
-@item isf, in_sample_fmt
-Specify the input sample format. It is set by default to @code{none}.
-
-@item osf, out_sample_fmt
-Specify the output sample format. It is set by default to @code{none}.
-
-@item tsf, internal_sample_fmt
-Set the internal sample format. Default value is @code{none}.
-This will automatically be chosen when it is not explicitly set.
-
-@item icl, in_channel_layout
-Set the input channel layout.
-
-@item ocl, out_channel_layout
-Set the output channel layout.
-
-@item clev, center_mix_level
-Set the center mix level. It is a value expressed in deciBel, and must be
-in the interval [-32,32].
-
-@item slev, surround_mix_level
-Set the surround mix level. It is a value expressed in deciBel, and must
-be in the interval [-32,32].
-
-@item lfe_mix_level
-Set LFE mix into non LFE level. It is used when there is a LFE input but no
-LFE output. It is a value expressed in deciBel, and must
-be in the interval [-32,32].
-
-@item rmvol, rematrix_volume
-Set rematrix volume. Default value is 1.0.
-
-@item flags, swr_flags
-Set flags used by the converter. Default value is 0.
-
-It supports the following individual flags:
-@table @option
-@item res
-force resampling, this flag forces resampling to be used even when the
-input and output sample rates match.
-@end table
-
-@item dither_scale
-Set the dither scale. Default value is 1.
-
-@item dither_method
-Set dither method. Default value is 0.
-
-Supported values:
-@table @samp
-@item rectangular
-select rectangular dither
-@item triangular
-select triangular dither
-@item triangular_hp
-select triangular dither with high pass
-@item lipshitz
-select lipshitz noise shaping dither
-@item shibata
-select shibata noise shaping dither
-@item low_shibata
-select low shibata noise shaping dither
-@item high_shibata
-select high shibata noise shaping dither
-@item f_weighted
-select f-weighted noise shaping dither
-@item modified_e_weighted
-select modified-e-weighted noise shaping dither
-@item improved_e_weighted
-select improved-e-weighted noise shaping dither
-
-@end table
-
-@item resampler
-Set resampling engine. Default value is swr.
-
-Supported values:
-@table @samp
-@item swr
-select the native SW Resampler; filter options precision and cheby are not
-applicable in this case.
-@item soxr
-select the SoX Resampler (where available); compensation, and filter options
-filter_size, phase_shift, filter_type & kaiser_beta, are not applicable in this
-case.
-@end table
-
-@item filter_size
-For swr only, set resampling filter size, default value is 32.
-
-@item phase_shift
-For swr only, set resampling phase shift, default value is 10, and must be in
-the interval [0,30].
-
-@item linear_interp
-Use Linear Interpolation if set to 1, default value is 0.
-
-@item cutoff
-Set cutoff frequency (swr: 6dB point; soxr: 0dB point) ratio; must be a float
-value between 0 and 1.  Default value is 0.97 with swr, and 0.91 with soxr
-(which, with a sample-rate of 44100, preserves the entire audio band to 20kHz).
-
-@item precision
-For soxr only, the precision in bits to which the resampled signal will be
-calculated.  The default value of 20 (which, with suitable dithering, is
-appropriate for a destination bit-depth of 16) gives SoX's 'High Quality'; a
-value of 28 gives SoX's 'Very High Quality'.
-
-@item cheby
-For soxr only, selects passband rolloff none (Chebyshev) & higher-precision
-approximation for 'irrational' ratios. Default value is 0.
-
-@item async
-For swr only, simple 1 parameter audio sync to timestamps using stretching,
-squeezing, filling and trimming. Setting this to 1 will enable filling and
-trimming, larger values represent the maximum amount in samples that the data
-may be stretched or squeezed for each second.
-Default value is 0, thus no compensation is applied to make the samples match
-the audio timestamps.
-
-@item first_pts
-For swr only, assume the first pts should be this value. The time unit is 1 / sample rate.
-This allows for padding/trimming at the start of stream. By default, no
-assumption is made about the first frame's expected pts, so no padding or
-trimming is done. For example, this could be set to 0 to pad the beginning with
-silence if an audio stream starts after the video stream or to trim any samples
-with a negative pts due to encoder delay.
-
-@item min_comp
-For swr only, set the minimum difference between timestamps and audio data (in
-seconds) to trigger stretching/squeezing/filling or trimming of the
-data to make it match the timestamps. The default is that
-stretching/squeezing/filling and trimming is disabled
-(@option{min_comp} = @code{FLT_MAX}).
-
-@item min_hard_comp
-For swr only, set the minimum difference between timestamps and audio data (in
-seconds) to trigger adding/dropping samples to make it match the
-timestamps.  This option effectively is a threshold to select between
-hard (trim/fill) and soft (squeeze/stretch) compensation. Note that
-all compensation is by default disabled through @option{min_comp}.
-The default is 0.1.
-
-@item comp_duration
-For swr only, set duration (in seconds) over which data is stretched/squeezed
-to make it match the timestamps. Must be a non-negative double float value,
-default value is 1.0.
-
-@item max_soft_comp
-For swr only, set maximum factor by which data is stretched/squeezed to make it
-match the timestamps. Must be a non-negative double float value, default value
-is 0.
-
-@item matrix_encoding
-Select matrixed stereo encoding.
-
-It accepts the following values:
-@table @samp
-@item none
-select none
-@item dolby
-select Dolby
-@item dplii
-select Dolby Pro Logic II
-@end table
-
-Default value is @code{none}.
-
-@item filter_type
-For swr only, select resampling filter type. This only affects resampling
-operations.
-
-It accepts the following values:
-@table @samp
-@item cubic
-select cubic
-@item blackman_nuttall
-select Blackman Nuttall Windowed Sinc
-@item kaiser
-select Kaiser Windowed Sinc
-@end table
-
-@item kaiser_beta
-For swr only, set Kaiser Window Beta value. Must be an integer in the
-interval [2,16], default value is 9.
-
-@end table
-
-@c man end RESAMPLER OPTIONS
+@include resampler.texi
 
 @chapter See Also
 
diff --git a/doc/ffmpeg-scaler.texi b/doc/ffmpeg-scaler.texi
index 1110c69..47b81a4 100644
--- a/doc/ffmpeg-scaler.texi
+++ b/doc/ffmpeg-scaler.texi
@@ -18,105 +18,7 @@
 
 @c man end DESCRIPTION
 
-@chapter Scaler Options
-@c man begin SCALER OPTIONS
-
-The video scaler supports the following named options.
-
-Options may be set by specifying -@var{option} @var{value} in the
-FFmpeg tools. For programmatic use, they can be set explicitly in the
-@code{SwsContext} options or through the @file{libavutil/opt.h} API.
-
-@table @option
-
-@item sws_flags
-Set the scaler flags. This is also used to set the scaling
-algorithm. Only a single algorithm should be selected.
-
-It accepts the following values:
-@table @samp
-@item fast_bilinear
-Select fast bilinear scaling algorithm.
-
-@item bilinear
-Select bilinear scaling algorithm.
-
-@item bicubic
-Select bicubic scaling algorithm.
-
-@item experimental
-Select experimental scaling algorithm.
-
-@item neighbor
-Select nearest neighbor rescaling algorithm.
-
-@item area
-Select averaging area rescaling algorithm.
-
-@item bicubiclin
-Select bicubic scaling algorithm for the luma component, bilinear for
-chroma components.
-
-@item gauss
-Select Gaussian rescaling algorithm.
-
-@item sinc
-Select sinc rescaling algorithm.
-
-@item lanczos
-Select lanczos rescaling algorithm.
-
-@item spline
-Select natural bicubic spline rescaling algorithm.
-
-@item print_info
-Enable printing/debug logging.
-
-@item accurate_rnd
-Enable accurate rounding.
-
-@item full_chroma_int
-Enable full chroma interpolation.
-
-@item full_chroma_inp
-Select full chroma input.
-
-@item bitexact
-Enable bitexact output.
-@end table
-
-@item srcw
-Set source width.
-
-@item srch
-Set source height.
-
-@item dstw
-Set destination width.
-
-@item dsth
-Set destination height.
-
-@item src_format
-Set source pixel format (must be expressed as an integer).
-
-@item dst_format
-Set destination pixel format (must be expressed as an integer).
-
-@item src_range
-Select source range.
-
-@item dst_range
-Select destination range.
-
-@item param0, param1
-Set scaling algorithm parameters. The specified values are specific of
-some scaling algorithms and ignored by others. The specified values
-are floating point number values.
-
-@end table
-
-@c man end SCALER OPTIONS
+@include scaler.texi
 
 @chapter See Also
 
diff --git a/doc/ffmpeg-utils.texi b/doc/ffmpeg-utils.texi
index c5822a8..581e2ea 100644
--- a/doc/ffmpeg-utils.texi
+++ b/doc/ffmpeg-utils.texi
@@ -17,8 +17,7 @@
 
 @c man end DESCRIPTION
 
-@include syntax.texi
-@include eval.texi
+@include utils.texi
 
 @chapter See Also
 
diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi
index 7563107..94f48fd 100644
--- a/doc/ffmpeg.texi
+++ b/doc/ffmpeg.texi
@@ -16,26 +16,26 @@
 @chapter Description
 @c man begin DESCRIPTION
 
-ffmpeg is a very fast video and audio converter that can also grab from
+@command{ffmpeg} is a very fast video and audio converter that can also grab from
 a live audio/video source. It can also convert between arbitrary sample
 rates and resize video on the fly with a high quality polyphase filter.
 
-ffmpeg reads from an arbitrary number of input "files" (which can be regular
+@command{ffmpeg} reads from an arbitrary number of input "files" (which can be regular
 files, pipes, network streams, grabbing devices, etc.), specified by the
 @code{-i} option, and writes to an arbitrary number of output "files", which are
 specified by a plain output filename. Anything found on the command line which
 cannot be interpreted as an option is considered to be an output filename.
 
-Each input or output file can in principle contain any number of streams of
-different types (video/audio/subtitle/attachment/data). Allowed number and/or
-types of streams can be limited by the container format. Selecting, which
-streams from which inputs go into output, is done either automatically or with
-the @code{-map} option (see the Stream selection chapter).
+Each input or output file can, in principle, contain any number of streams of
+different types (video/audio/subtitle/attachment/data). The allowed number and/or
+types of streams may be limited by the container format. Selecting which
+streams from which inputs will go into which output is either done automatically
+or with the @code{-map} option (see the Stream selection chapter).
 
 To refer to input files in options, you must use their indices (0-based). E.g.
-the first input file is @code{0}, the second is @code{1} etc. Similarly, streams
+the first input file is @code{0}, the second is @code{1}, etc. Similarly, streams
 within a file are referred to by their indices. E.g. @code{2:3} refers to the
-fourth stream in the third input file. See also the Stream specifiers chapter.
+fourth stream in the third input file. Also see the Stream specifiers chapter.
 
 As a general rule, options are applied to the next specified
 file. Therefore, order is important, and you can have the same
@@ -50,7 +50,7 @@
 
 @itemize
 @item
-To set the video bitrate of the output file to 64kbit/s:
+To set the video bitrate of the output file to 64 kbit/s:
 @example
 ffmpeg -i input.avi -b:v 64k -bufsize 64k output.avi
 @end example
@@ -96,14 +96,14 @@
 Encoded packets are then passed to the decoder (unless streamcopy is selected
 for the stream, see further for a description). The decoder produces
 uncompressed frames (raw video/PCM audio/...) which can be processed further by
-filtering (see next section). After filtering the frames are passed to the
-encoder, which encodes them and outputs encoded packets again. Finally those are
+filtering (see next section). After filtering, the frames are passed to the
+encoder, which encodes them and outputs encoded packets. Finally those are
 passed to the muxer, which writes the encoded packets to the output file.
 
 @section Filtering
 Before encoding, @command{ffmpeg} can process raw audio and video frames using
 filters from the libavfilter library. Several chained filters form a filter
-graph.  @command{ffmpeg} distinguishes between two types of filtergraphs -
+graph. @command{ffmpeg} distinguishes between two types of filtergraphs:
 simple and complex.
 
 @subsection Simple filtergraphs
@@ -139,7 +139,7 @@
 
 @subsection Complex filtergraphs
 Complex filtergraphs are those which cannot be described as simply a linear
-processing chain applied to one stream. This is the case e.g. when the graph has
+processing chain applied to one stream. This is the case, for example, when the graph has
 more than one input and/or output, or when output stream type is different from
 input. They can be represented with the following diagram:
 
@@ -164,9 +164,11 @@
 @end example
 
 Complex filtergraphs are configured with the @option{-filter_complex} option.
-Note that this option is global, since a complex filtergraph by its nature
+Note that this option is global, since a complex filtergraph, by its nature,
 cannot be unambiguously associated with a single stream or file.
 
+The @option{-lavfi} option is equivalent to @option{-filter_complex}.
+
 A trivial example of a complex filtergraph is the @code{overlay} filter, which
 has two video inputs and one video output, containing one video overlaid on top
 of the other. Its audio counterpart is the @code{amix} filter.
@@ -176,7 +178,7 @@
 @option{-codec} option. It makes @command{ffmpeg} omit the decoding and encoding
 step for the specified stream, so it does only demuxing and muxing. It is useful
 for changing the container format or modifying container-level metadata. The
-diagram above will in this case simplify to this:
+diagram above will, in this case, simplify to this:
 
 @example
  _______              ______________            ________
@@ -188,7 +190,7 @@
 @end example
 
 Since there is no decoding or encoding, it is very fast and there is no quality
-loss. However it might not work in some cases because of many factors. Applying
+loss. However, it might not work in some cases because of many factors. Applying
 filters is obviously also impossible, since filters work on uncompressed data.
 
 @c man end DETAILED DESCRIPTION
@@ -196,14 +198,14 @@
 @chapter Stream selection
 @c man begin STREAM SELECTION
 
-By default ffmpeg includes only one stream of each type (video, audio, subtitle)
+By default, @command{ffmpeg} includes only one stream of each type (video, audio, subtitle)
 present in the input files and adds them to each output file.  It picks the
-"best" of each based upon the following criteria; for video it is the stream
-with the highest resolution, for audio the stream with the most channels, for
-subtitle it's the first subtitle stream. In the case where several streams of
-the same type rate equally, the lowest numbered stream is chosen.
+"best" of each based upon the following criteria: for video, it is the stream
+with the highest resolution, for audio, it is the stream with the most channels, for
+subtitles, it is the first subtitle stream. In the case where several streams of
+the same type rate equally, the stream with the lowest index is chosen.
 
-You can disable some of those defaults by using @code{-vn/-an/-sn} options. For
+You can disable some of those defaults by using the @code{-vn/-an/-sn} options. For
 full manual control, use the @code{-map} option, which disables the defaults just
 described.
 
@@ -220,7 +222,7 @@
 
 @item -f @var{fmt} (@emph{input/output})
 Force input or output file format. The format is normally auto detected for input
-files and guessed from file extension for output files, so this option is not
+files and guessed from the file extension for output files, so this option is not
 needed in most cases.
 
 @item -i @var{filename} (@emph{input})
@@ -230,7 +232,8 @@
 Overwrite output files without asking.
 
 @item -n (@emph{global})
-Do not overwrite output files but exit if file exists.
+Do not overwrite output files, and exit immediately if a specified
+output file already exists.
 
 @item -c[:@var{stream_specifier}] @var{codec} (@emph{input/output,per-stream})
 @itemx -codec[:@var{stream_specifier}] @var{codec} (@emph{input/output,per-stream})
@@ -343,25 +346,31 @@
 codec-dependent.
 
 @anchor{filter_option}
-@item -filter[:@var{stream_specifier}] @var{filter_graph} (@emph{output,per-stream})
-Create the filter graph specified by @var{filter_graph} and use it to
+@item -filter[:@var{stream_specifier}] @var{filtergraph} (@emph{output,per-stream})
+Create the filtergraph specified by @var{filtergraph} and use it to
 filter the stream.
 
-@var{filter_graph} is a description of the filter graph to apply to
+@var{filtergraph} is a description of the filtergraph to apply to
 the stream, and must have a single input and a single output of the
-same type of the stream. In the filter graph, the input is associated
+same type of the stream. In the filtergraph, the input is associated
 to the label @code{in}, and the output to the label @code{out}. See
 the ffmpeg-filters manual for more information about the filtergraph
 syntax.
 
 See the @ref{filter_complex_option,,-filter_complex option} if you
-want to create filter graphs with multiple inputs and/or outputs.
+want to create filtergraphs with multiple inputs and/or outputs.
+
+@item -filter_script[:@var{stream_specifier}] @var{filename} (@emph{output,per-stream})
+This option is similar to @option{-filter}, the only difference is that its
+argument is the name of the file from which a filtergraph description is to be
+read.
 
 @item -pre[:@var{stream_specifier}] @var{preset_name} (@emph{output,per-stream})
 Specify the preset for matching stream(s).
 
 @item -stats (@emph{global})
-Print encoding progress/statistics. On by default.
+Print encoding progress/statistics. It is on by default, to explicitly
+disable it you need to specify @code{-nostats}.
 
 @item -progress @var{url} (@emph{global})
 Send program-friendly progress information to @var{url}.
@@ -459,6 +468,10 @@
 numerator and denominator of the aspect ratio. For example "4:3",
 "16:9", "1.3333", and "1.7777" are valid argument values.
 
+If used together with @option{-vcodec copy}, it will affect the aspect ratio
+stored at container level, but not the aspect ratio stored in encoded
+frames, if it exists.
+
 @item -vn (@emph{output})
 Disable video recording.
 
@@ -487,8 +500,8 @@
 @item -vlang @var{code}
 Set the ISO 639 language code (3 letters) of the current video stream.
 
-@item -vf @var{filter_graph} (@emph{output})
-Create the filter graph specified by @var{filter_graph} and use it to
+@item -vf @var{filtergraph} (@emph{output})
+Create the filtergraph specified by @var{filtergraph} and use it to
 filter the stream.
 
 This is an alias for @code{-filter:v}, see the @ref{filter_option,,-filter option}.
@@ -504,7 +517,7 @@
 warning and select the best pixel format supported by the encoder.
 If @var{pix_fmt} is prefixed by a @code{+}, ffmpeg will exit with an error
 if the requested pixel format can not be selected, and automatic conversions
-inside filter graphs are disabled.
+inside filtergraphs are disabled.
 If @var{pix_fmt} is a single @code{+}, ffmpeg selects the same pixel format
 as the input (or graph output) and automatic conversions are disabled.
 
@@ -628,8 +641,8 @@
 Set the audio sample format. Use @code{-sample_fmts} to get a list
 of supported sample formats.
 
-@item -af @var{filter_graph} (@emph{output})
-Create the filter graph specified by @var{filter_graph} and use it to
+@item -af @var{filtergraph} (@emph{output})
+Create the filtergraph specified by @var{filtergraph} and use it to
 filter the stream.
 
 This is an alias for @code{-filter:a}, see the @ref{filter_option,,-filter option}.
@@ -680,6 +693,9 @@
 subtitle packet is decoded: it may increase memory consumption and latency a
 lot.
 
+@item -canvas_size @var{size}
+Set the size of the canvas used to render subtitles.
+
 @end table
 
 @section Advanced options
@@ -884,7 +900,7 @@
 Each frame is passed with its timestamp from the demuxer to the muxer.
 @item 1, cfr
 Frames will be duplicated and dropped to achieve exactly the requested
-constant framerate.
+constant frame rate.
 @item 2, vfr
 Frames are passed through with their timestamp or dropped so as to
 prevent 2 frames from having the same timestamp.
@@ -896,6 +912,10 @@
 default method.
 @end table
 
+Note that the timestamps may be further modified by the muxer, after this.
+For example, in the case that the format option @option{avoid_negative_ts}
+is enabled.
+
 With -map you can select from which stream the timestamps should be
 taken. You can leave either video or audio unchanged and sync the
 remaining stream(s) to the unchanged one.
@@ -905,6 +925,11 @@
 the parameter is the maximum samples per second by which the audio is changed.
 -async 1 is a special case where only the start of the audio stream is corrected
 without any later correction.
+
+Note that the timestamps may be further modified by the muxer, after this.
+For example, in the case that the format option @option{avoid_negative_ts}
+is enabled.
+
 This option has been deprecated. Use the @code{aresample} audio filter instead.
 
 @item -copyts
@@ -913,7 +938,8 @@
 offset value.
 
 Note that, depending on the @option{vsync} option or on specific muxer
-processing, the output timestamps may mismatch with the input
+processing (e.g. in case the format option @option{avoid_negative_ts}
+is enabled) the output timestamps may mismatch with the input
 timestamps even when this option is selected.
 
 @item -copytb @var{mode}
@@ -983,10 +1009,10 @@
 
 @anchor{filter_complex_option}
 @item -filter_complex @var{filtergraph} (@emph{global})
-Define a complex filter graph, i.e. one with arbitrary number of inputs and/or
+Define a complex filtergraph, i.e. one with arbitrary number of inputs and/or
 outputs. For simple graphs -- those with one input and one output of the same
 type -- see the @option{-filter} options. @var{filtergraph} is a description of
-the filter graph, as described in the ``Filtergraph syntax'' section of the
+the filtergraph, as described in the ``Filtergraph syntax'' section of the
 ffmpeg-filters manual.
 
 Input link labels must refer to input streams using the
@@ -1028,6 +1054,16 @@
 @example
 ffmpeg -filter_complex 'color=c=red' -t 5 out.mkv
 @end example
+
+@item -lavfi @var{filtergraph} (@emph{global})
+Define a complex filtergraph, i.e. one with arbitrary number of inputs and/or
+outputs. Equivalent to @option{-filter_complex}.
+
+@item -filter_complex_script @var{filename} (@emph{global})
+This option is similar to @option{-filter_complex}, the only difference is that
+its argument is the name of the file from which a complex filtergraph
+description is to be read.
+
 @end table
 
 As a special exception, you can use a bitmap subtitle stream as input: it
@@ -1330,9 +1366,19 @@
 @end itemize
 @c man end EXAMPLES
 
+@ifset config-all
+@include all-components.texi
+@end ifset
+
 @chapter See Also
 
 @ifhtml
+@ifset config-all
+@url{ffmpeg.html,ffmpeg}
+@end ifset
+@ifset config-not-all
+@url{ffmpeg-all.html,ffmpeg-all},
+@end ifset
 @url{ffplay.html,ffplay}, @url{ffprobe.html,ffprobe}, @url{ffserver.html,ffserver},
 @url{ffmpeg-utils.html,ffmpeg-utils},
 @url{ffmpeg-scaler.html,ffmpeg-scaler},
@@ -1346,6 +1392,12 @@
 @end ifhtml
 
 @ifnothtml
+@ifset config-all
+ffmpeg(1),
+@end ifset
+@ifset config-not-all
+ffmpeg-all(1),
+@end ifset
 ffplay(1), ffprobe(1), ffserver(1),
 ffmpeg-utils(1), ffmpeg-scaler(1), ffmpeg-resampler(1),
 ffmpeg-codecs(1), ffmpeg-bitstream-filters(1), ffmpeg-formats(1),
diff --git a/doc/ffplay.texi b/doc/ffplay.texi
index 8d6abee..21a6e6c 100644
--- a/doc/ffplay.texi
+++ b/doc/ffplay.texi
@@ -73,17 +73,23 @@
 You can interactively cycle through the available show modes by
 pressing the key @key{w}.
 
-@item -vf @var{filter_graph}
-Create the filter graph specified by @var{filter_graph} and use it to
+@item -vf @var{filtergraph}
+Create the filtergraph specified by @var{filtergraph} and use it to
 filter the video stream.
 
-@var{filter_graph} is a description of the filter graph to apply to
+@var{filtergraph} is a description of the filtergraph to apply to
 the stream, and must have a single video input and a single video
-output. In the filter graph, the input is associated to the label
+output. In the filtergraph, the input is associated to the label
 @code{in}, and the output to the label @code{out}. See the
 ffmpeg-filters manual for more information about the filtergraph
 syntax.
 
+@item -af @var{filtergraph}
+@var{filtergraph} is a description of the filtergraph to apply to
+the input audio.
+Use the option "-filters" to show all the available filters (including
+sources and sinks).
+
 @item -i @var{input_file}
 Read @var{input_file}.
 @end table
@@ -93,9 +99,13 @@
 @item -pix_fmt @var{format}
 Set pixel format.
 This option has been deprecated in favor of private options, try -pixel_format.
+
 @item -stats
-Show the stream duration, the codec parameters, the current position in
-the stream and the audio/video synchronisation drift.
+Print several playback statistics, in particular show the stream
+duration, the codec parameters, the current position in the stream and
+the audio/video synchronisation drift. It is on by default, to
+explicitly disable it you need to specify @code{-nostats}.
+
 @item -bug
 Work around bugs.
 @item -fast
@@ -191,9 +201,19 @@
 
 @c man end
 
+@ifset config-all
+@include all-components.texi
+@end ifset
+
 @chapter See Also
 
 @ifhtml
+@ifset config-all
+@url{ffplay.html,ffplay},
+@end ifset
+@ifset config-not-all
+@url{ffplay-all.html,ffmpeg-all},
+@end ifset
 @url{ffmpeg.html,ffmpeg}, @url{ffprobe.html,ffprobe}, @url{ffserver.html,ffserver},
 @url{ffmpeg-utils.html,ffmpeg-utils},
 @url{ffmpeg-scaler.html,ffmpeg-scaler},
@@ -207,6 +227,12 @@
 @end ifhtml
 
 @ifnothtml
+@ifset config-all
+ffplay(1),
+@end ifset
+@ifset config-not-all
+ffplay-all(1),
+@end ifset
 ffmpeg(1), ffprobe(1), ffserver(1),
 ffmpeg-utils(1), ffmpeg-scaler(1), ffmpeg-resampler(1),
 ffmpeg-codecs(1), ffmpeg-bitstream-filters(1), ffmpeg-formats(1),
diff --git a/doc/ffprobe.texi b/doc/ffprobe.texi
index 6e30b2f..aacee3a 100644
--- a/doc/ffprobe.texi
+++ b/doc/ffprobe.texi
@@ -487,10 +487,20 @@
 @end itemize
 @c man end TIMECODE
 
+@ifset config-all
+@include all-components.texi
+@end ifset
+
 @chapter See Also
 
 @ifhtml
-@url{ffplay.html,ffmpeg}, @url{ffprobe.html,ffprobe}, @url{ffserver.html,ffserver},
+@ifset config-all
+@url{ffprobe.html,ffprobe},
+@end ifset
+@ifset config-not-all
+@url{ffprobe-all.html,ffprobe-all},
+@end ifset
+@url{ffmpeg.html,ffmpeg}, @url{ffplay.html,ffplay}, @url{ffserver.html,ffserver},
 @url{ffmpeg-utils.html,ffmpeg-utils},
 @url{ffmpeg-scaler.html,ffmpeg-scaler},
 @url{ffmpeg-resampler.html,ffmpeg-resampler},
@@ -503,6 +513,12 @@
 @end ifhtml
 
 @ifnothtml
+@ifset config-all
+ffprobe(1),
+@end ifset
+@ifset config-not-all
+ffprobe-all(1),
+@end ifset
 ffmpeg(1), ffplay(1), ffserver(1),
 ffmpeg-utils(1), ffmpeg-scaler(1), ffmpeg-resampler(1),
 ffmpeg-codecs(1), ffmpeg-bitstream-filters(1), ffmpeg-formats(1),
diff --git a/doc/ffprobe.xsd b/doc/ffprobe.xsd
index d9c6655..eab97fb 100644
--- a/doc/ffprobe.xsd
+++ b/doc/ffprobe.xsd
@@ -78,7 +78,6 @@
       <xsd:attribute name="interlaced_frame"       type="xsd:int"   />
       <xsd:attribute name="top_field_first"        type="xsd:int"   />
       <xsd:attribute name="repeat_pict"            type="xsd:int"   />
-      <xsd:attribute name="reference"              type="xsd:int"   />
     </xsd:complexType>
 
     <xsd:complexType name="streamsType">
diff --git a/doc/ffserver.texi b/doc/ffserver.texi
index f1b7599..8844941 100644
--- a/doc/ffserver.texi
+++ b/doc/ffserver.texi
@@ -246,10 +246,20 @@
 @end table
 @c man end
 
+@ifset config-all
+@include all-components.texi
+@end ifset
+
 @chapter See Also
 
 @ifhtml
-The @file{doc/ffserver.conf} example,
+@ifset config-all
+@url{ffserver.html,ffserver},
+@end ifset
+@ifset config-not-all
+@url{ffserver-all.html,ffserver-all},
+@end ifset
+the @file{doc/ffserver.conf} example,
 @url{ffmpeg.html,ffmpeg}, @url{ffplay.html,ffplay}, @url{ffprobe.html,ffprobe},
 @url{ffmpeg-utils.html,ffmpeg-utils},
 @url{ffmpeg-scaler.html,ffmpeg-scaler},
@@ -263,7 +273,13 @@
 @end ifhtml
 
 @ifnothtml
-The @file{doc/ffserver.conf} example, ffmpeg(1), ffplay(1), ffprobe(1),
+@ifset config-all
+ffserver(1),
+@end ifset
+@ifset config-not-all
+ffserver-all(1),
+@end ifset
+the @file{doc/ffserver.conf} example, ffmpeg(1), ffplay(1), ffprobe(1),
 ffmpeg-utils(1), ffmpeg-scaler(1), ffmpeg-resampler(1),
 ffmpeg-codecs(1), ffmpeg-bitstream-filters(1), ffmpeg-formats(1),
 ffmpeg-devices(1), ffmpeg-protocols(1), ffmpeg-filters(1)
diff --git a/doc/filter_design.txt b/doc/filter_design.txt
index 772ca9d..c303c3c 100644
--- a/doc/filter_design.txt
+++ b/doc/filter_design.txt
@@ -29,6 +29,11 @@
   same format amongst a supported list, all it has to do is use a reference
   to the same list of formats.
 
+  query_formats can leave some formats unset and return AVERROR(EAGAIN) to
+  cause the negotiation mechanism to try again later. That can be used by
+  filters with complex requirements to use the format negotiated on one link
+  to set the formats supported on another.
+
 
 Buffer references ownership and permissions
 ===========================================
@@ -199,7 +204,7 @@
     filter; these buffered frames must be flushed immediately if a new input
     produces new output.
 
-    (Example: framerate-doubling filter: filter_frame must (1) flush the
+    (Example: frame rate-doubling filter: filter_frame must (1) flush the
     second copy of the previous frame, if it is still there, (2) push the
     first copy of the incoming frame, (3) keep the second copy for later.)
 
diff --git a/doc/filters.texi b/doc/filters.texi
index bcbe21a..8971276 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -3,10 +3,10 @@
 
 Filtering in FFmpeg is enabled through the libavfilter library.
 
-In libavfilter, it is possible for filters to have multiple inputs and
-multiple outputs.
-To illustrate the sorts of things that are possible, we can
-use a complex filter graph. For example, the following one:
+In libavfilter, a filter can have multiple inputs and multiple
+outputs.
+To illustrate the sorts of things that are possible, we consider the
+following filtergraph.
 
 @example
 input --> split ---------------------> overlay --> output
@@ -15,25 +15,32 @@
             +-----> crop --> vflip -------+
 @end example
 
-splits the stream in two streams, sends one stream through the crop filter
-and the vflip filter before merging it back with the other stream by
-overlaying it on top. You can use the following command to achieve this:
+This filtergraph splits the input stream in two streams, sends one
+stream through the crop filter and the vflip filter before merging it
+back with the other stream by overlaying it on top. You can use the
+following command to achieve this:
 
 @example
-ffmpeg -i input -vf "[in] split [T1], [T2] overlay=0:H/2 [out]; [T1] crop=iw:ih/2:0:ih/2, vflip [T2]" output
+ffmpeg -i INPUT -vf "split [main][tmp]; [tmp] crop=iw:ih/2:0:0, vflip [flip]; [main][flip] overlay=0:H/2" OUTPUT
 @end example
 
 The result will be that in output the top half of the video is mirrored
 onto the bottom half.
 
-Filters are loaded using the @var{-vf} or @var{-af} option passed to
-@command{ffmpeg} or to @command{ffplay}. Filters in the same linear
-chain are separated by commas. In our example, @var{split,
-overlay} are in one linear chain, and @var{crop, vflip} are in
-another. The points where the linear chains join are labeled by names
-enclosed in square brackets. In our example, that is @var{[T1]} and
-@var{[T2]}. The special labels @var{[in]} and @var{[out]} are the points
-where video is input and output.
+Filters in the same linear chain are separated by commas, and distinct
+linear chains of filters are separated by semicolons. In our example,
+@var{crop,vflip} are in one linear chain, @var{split} and
+@var{overlay} are separately in another. The points where the linear
+chains join are labelled by names enclosed in square brackets. In the
+example, the split filter generates two outputs that are associated to
+the labels @var{[main]} and @var{[tmp]}.
+
+The stream sent to the second output of @var{split}, labelled as
+@var{[tmp]}, is processed through the @var{crop} filter, which crops
+away the lower half part of the video, and then vertically flipped. The
+@var{overlay} filter takes in input the first unchanged output of the
+split filter (which was labelled as @var{[main]}), and overlay on its
+lower half the output generated by the @var{crop,vflip} filterchain.
 
 Some filters take in input a list of parameters: they are specified
 after the filter name and an equal sign, and are separated from each other
@@ -49,7 +56,7 @@
 @c man begin GRAPH2DOT
 
 The @file{graph2dot} program included in the FFmpeg @file{tools}
-directory can be used to parse a filter graph description and issue a
+directory can be used to parse a filtergraph description and issue a
 corresponding textual representation in the dot language.
 
 Invoke the command:
@@ -61,7 +68,7 @@
 
 You can then pass the dot description to the @file{dot} program (from
 the graphviz suite of programs) and obtain a graphical representation
-of the filter graph.
+of the filtergraph.
 
 For example the sequence of commands:
 @example
@@ -110,7 +117,7 @@
 recognized by the @option{-filter}/@option{-vf} and @option{-filter_complex}
 options in @command{ffmpeg} and @option{-vf} in @command{ffplay}, and by the
 @code{avfilter_graph_parse()}/@code{avfilter_graph_parse2()} function defined in
-@file{libavfilter/avfiltergraph.h}.
+@file{libavfilter/avfilter.h}.
 
 A filterchain consists of a sequence of connected filters, each one
 connected to the previous one in the sequence. A filterchain is
@@ -130,8 +137,31 @@
 "=@var{arguments}".
 
 @var{arguments} is a string which contains the parameters used to
-initialize the filter instance, and are described in the filter
-descriptions below.
+initialize the filter instance. It may have one of the following forms:
+@itemize
+
+@item
+A ':'-separated list of @var{key=value} pairs.
+
+@item
+A ':'-separated list of @var{value}. In this case, the keys are assumed to be
+the option names in the order they are declared. E.g. the @code{fade} filter
+declares three options in this order -- @option{type}, @option{start_frame} and
+@option{nb_frames}. Then the parameter list @var{in:0:30} means that the value
+@var{in} is assigned to the option @option{type}, @var{0} to
+@option{start_frame} and @var{30} to @option{nb_frames}.
+
+@item
+A ':'-separated list of mixed direct @var{value} and long @var{key=value}
+pairs. The direct @var{value} must precede the @var{key=value} pairs, and
+follow the same constraints order of the previous point. The following
+@var{key=value} pairs can be set in any preferred order.
+
+@end itemize
+
+If the option value itself is a list of items (e.g. the @code{format} filter
+takes a list of pixel formats), the items in the list are usually separated by
+'|'.
 
 The list of arguments can be quoted using the character "'" as initial
 and ending mark, and the character '\' for escaping the characters
@@ -237,6 +267,39 @@
 for more information about the escaping and quoting rules adopted by
 FFmpeg.
 
+@chapter Timeline editing
+
+Some filters support a generic @option{enable} option. For the filters
+supporting timeline editing, this option can be set to an expression which is
+evaluated before sending a frame to the filter. If the evaluation is non-zero,
+the filter will be enabled, otherwise the frame will be sent unchanged to the
+next filter in the filtergraph.
+
+The expression accepts the following values:
+@table @samp
+@item t
+timestamp expressed in seconds, NAN if the input timestamp is unknown
+
+@item n
+sequential number of the input frame, starting from 0
+
+@item pos
+the position in the file of the input frame, NAN if unknown
+@end table
+
+Additionally, these filters support an @option{enable} command that can be used
+to re-define the expression.
+
+Like any other filtering option, the @option{enable} option follows the same
+rules.
+
+For example, to enable a denoiser filter (@ref{hqdn3d}) from 10 seconds to 3
+minutes, and a @ref{curves} filter starting at 3 seconds:
+@example
+hqdn3d = enable='between(t,10,3*60)',
+curves = enable='gte(t,3)' : preset=cross_process
+@end example
+
 @c man end FILTERGRAPH DESCRIPTION
 
 @chapter Audio Filters
@@ -253,6 +316,8 @@
 
 Convert the input audio format to the specified formats.
 
+@emph{This filter is deprecated. Use @ref{aformat} instead.}
+
 The filter accepts a string of the form:
 "@var{sample_format}:@var{channel_layout}".
 
@@ -266,7 +331,7 @@
 The special parameter "auto", signifies that the filter will
 automatically select the output format depending on the output filter.
 
-Some examples follow.
+@subsection Examples
 
 @itemize
 @item
@@ -289,10 +354,7 @@
 An all-pass filter changes the audio's frequency to phase relationship
 without changing its frequency to amplitude relationship.
 
-The filter accepts parameters as a list of @var{key}=@var{value}
-pairs, separated by ":".
-
-A description of the accepted parameters follows.
+The filter accepts the following options:
 
 @table @option
 @item frequency, f
@@ -321,10 +383,7 @@
 The filter can be either single-pole, or double-pole (the default).
 The filter roll off at 6dB per pole per octave (20dB per pole per decade).
 
-The filter accepts parameters as a list of @var{key}=@var{value}
-pairs, separated by ":".
-
-A description of the accepted parameters follows.
+The filter accepts the following options:
 
 @table @option
 @item frequency, f
@@ -358,10 +417,7 @@
 The filter can be either single-pole or double-pole (the default).
 The filter roll off at 6dB per pole per octave (20dB per pole per decade).
 
-The filter accepts parameters as a list of @var{key}=@var{value}
-pairs, separated by ":".
-
-A description of the accepted parameters follows.
+The filter accepts the following options:
 
 @table @option
 @item frequency, f
@@ -395,10 +451,7 @@
 shelving filter with a response similar to that of a standard
 hi-fi's tone-controls. This is also known as shelving equalisation (EQ).
 
-The filter accepts parameters as a list of @var{key}=@var{value}
-pairs, separated by ":".
-
-A description of the accepted parameters follows.
+The filter accepts the following options:
 
 @table @option
 @item gain, g
@@ -434,10 +487,7 @@
 shelving filter with a response similar to that of a standard
 hi-fi's tone-controls. This is also known as shelving equalisation (EQ).
 
-The filter accepts parameters as a list of @var{key}=@var{value}
-pairs, separated by ":".
-
-A description of the accepted parameters follows.
+The filter accepts the following options:
 
 @table @option
 @item gain, g
@@ -475,10 +525,7 @@
 instead of the default: constant 0dB peak gain.
 The filter roll off at 6dB per octave (20dB per decade).
 
-The filter accepts parameters as a list of @var{key}=@var{value}
-pairs, separated by ":".
-
-A description of the accepted parameters follows.
+The filter accepts the following options:
 
 @table @option
 @item frequency, f
@@ -510,10 +557,7 @@
 frequency @var{frequency}, and (3dB-point) band-width @var{width}.
 The filter roll off at 6dB per octave (20dB per decade).
 
-The filter accepts parameters as a list of @var{key}=@var{value}
-pairs, separated by ":".
-
-A description of the accepted parameters follows.
+The filter accepts the following options:
 
 @table @option
 @item frequency, f
@@ -552,10 +596,7 @@
 In order to produce complex equalisation curves, this filter can
 be given several times, each with a different central frequency.
 
-The filter accepts parameters as a list of @var{key}=@var{value}
-pairs, separated by ":".
-
-A description of the accepted parameters follows.
+The filter accepts the following options:
 
 @table @option
 @item frequency, f
@@ -586,9 +627,6 @@
 
 Apply fade-in/out effect to input audio.
 
-The filter accepts parameters as a list of @var{key}=@var{value}
-pairs, separated by ":".
-
 A description of the accepted parameters follows.
 
 @table @option
@@ -607,15 +645,26 @@
 the output audio will be silence. Default is 44100.
 
 @item start_time, st
-Specify time in seconds for starting to apply the fade
-effect. Default is 0.
+Specify time for starting to apply the fade effect. Default is 0.
+The accepted syntax is:
+@example
+[-]HH[:MM[:SS[.m...]]]
+[-]S+[.m...]
+@end example
+See also the function @code{av_parse_time()}.
 If set this option is used instead of @var{start_sample} one.
 
 @item duration, d
-Specify the number of seconds for which the fade effect has to last. At
-the end of the fade-in effect the output audio will have the same
+Specify the duration for which the fade effect has to last. Default is 0.
+The accepted syntax is:
+@example
+[-]HH[:MM[:SS[.m...]]]
+[-]S+[.m...]
+@end example
+See also the function @code{av_parse_time()}.
+At the end of the fade-in effect the output audio will have the same
 volume as the input audio, at the end of the fade-out transition
-the output audio will be silence. Default is 0.
+the output audio will be silence.
 If set this option is used instead of @var{nb_samples} one.
 
 @item curve
@@ -647,6 +696,7 @@
 @end table
 
 @subsection Examples
+
 @itemize
 @item
 Fade in first 15 seconds of audio:
@@ -661,6 +711,7 @@
 @end example
 @end itemize
 
+@anchor{aformat}
 @section aformat
 
 Set output format constraints for the input audio. The framework will
@@ -670,13 +721,13 @@
 @table @option
 
 @item sample_fmts
-A comma-separated list of requested sample formats.
+A '|'-separated list of requested sample formats.
 
 @item sample_rates
-A comma-separated list of requested sample rates.
+A '|'-separated list of requested sample rates.
 
 @item channel_layouts
-A comma-separated list of requested channel layouts.
+A '|'-separated list of requested channel layouts.
 
 @end table
 
@@ -684,14 +735,14 @@
 
 For example to force the output to either unsigned 8-bit or signed 16-bit stereo:
 @example
-aformat='sample_fmts=u8,s16:channel_layouts=stereo'
+aformat=sample_fmts=u8|s16:channel_layouts=stereo
 @end example
 
 @section amerge
 
 Merge two or more audio streams into a single multi-channel stream.
 
-The filter accepts the following named options:
+The filter accepts the following options:
 
 @table @option
 
@@ -722,22 +773,21 @@
 If inputs do not have the same duration, the output will stop with the
 shortest.
 
-Example: merge two mono files into a stereo stream:
+@subsection Examples
+
+@itemize
+@item
+Merge two mono files into a stereo stream:
 @example
 amovie=left.wav [l] ; amovie=right.mp3 [r] ; [l] [r] amerge
 @end example
 
-Example: multiple merges:
+@item
+Multiple merges assuming 1 video stream and 6 audio streams in @file{input.mkv}:
 @example
-ffmpeg -f lavfi -i "
-amovie=input.mkv:si=0 [a0];
-amovie=input.mkv:si=1 [a1];
-amovie=input.mkv:si=2 [a2];
-amovie=input.mkv:si=3 [a3];
-amovie=input.mkv:si=4 [a4];
-amovie=input.mkv:si=5 [a5];
-[a0][a1][a2][a3][a4][a5] amerge=inputs=6" -c:a pcm_s16le output.mkv
+ffmpeg -i input.mkv -filter_complex "[0:1][0:2][0:3][0:4][0:5][0:6] amerge=inputs=6" -c:a pcm_s16le output.mkv
 @end example
+@end itemize
 
 @section amix
 
@@ -786,6 +836,40 @@
 Pad the end of a audio stream with silence, this can be used together with
 -shortest to extend audio streams to the same length as the video stream.
 
+@section aphaser
+Add a phasing effect to the input audio.
+
+A phaser filter creates series of peaks and troughs in the frequency spectrum.
+The position of the peaks and troughs are modulated so that they vary over time, creating a sweeping effect.
+
+A description of the accepted parameters follows.
+
+@table @option
+@item in_gain
+Set input gain. Default is 0.4.
+
+@item out_gain
+Set output gain. Default is 0.74
+
+@item delay
+Set delay in milliseconds. Default is 3.0.
+
+@item decay
+Set decay. Default is 0.4.
+
+@item speed
+Set modulation speed in Hz. Default is 0.5.
+
+@item type
+Set modulation type. Default is triangular.
+
+It accepts the following values:
+@table @samp
+@item triangular, t
+@item sinusoidal, s
+@end table
+@end table
+
 @anchor{aresample}
 @section aresample
 
@@ -803,16 +887,22 @@
 @var{key}=@var{value} pairs, separated by ":". See the
 ffmpeg-resampler manual for the complete list of supported options.
 
-For example, to resample the input audio to 44100Hz:
+@subsection Examples
+
+@itemize
+@item
+Resample the input audio to 44100Hz:
 @example
 aresample=44100
 @end example
 
-To stretch/squeeze samples to the given timestamps, with a maximum of 1000
+@item
+Stretch/squeeze samples to the given timestamps, with a maximum of 1000
 samples per second compensation:
 @example
 aresample=async=1000
 @end example
+@end itemize
 
 @section asetnsamples
 
@@ -822,8 +912,7 @@
 the filter will flush all the remaining samples when the input audio
 signal its end.
 
-The filter accepts parameters as a list of @var{key}=@var{value} pairs,
-separated by ":".
+The filter accepts the following options:
 
 @table @option
 
@@ -844,6 +933,18 @@
 asetnsamples=n=1234:p=0
 @end example
 
+@section asetrate
+
+Set the sample rate without altering the PCM data.
+This will result in a change of speed and pitch.
+
+The filter accepts the following options:
+
+@table @option
+@item sample_rate, r
+Set the output sample rate. Default is 44100 Hz.
+@end table
+
 @section ashowinfo
 
 Show a line containing various information for each input audio frame.
@@ -889,37 +990,15 @@
 A list of Adler-32 checksums for each data plane.
 @end table
 
-@section asplit
-
-Split input audio into several identical outputs.
-
-The filter accepts a single parameter which specifies the number of outputs. If
-unspecified, it defaults to 2.
-
-For example:
-@example
-[in] asplit [out0][out1]
-@end example
-
-will create two separate outputs from the same input.
-
-To create 3 or more outputs, you need to specify the number of
-outputs, like in:
-@example
-[in] asplit=3 [out0][out1][out2]
-@end example
-
-@example
-ffmpeg -i INPUT -filter_complex asplit=5 OUTPUT
-@end example
-will create 5 copies of the input audio.
-
-
 @section astreamsync
 
 Forward two audio streams and control the order the buffers are forwarded.
 
-The argument to the filter is an expression deciding which stream should be
+The filter accepts the following options:
+
+@table @option
+@item expr, e
+Set the expression deciding which stream should be
 forwarded next: if the result is negative, the first stream is forwarded; if
 the result is positive or zero, the second stream is forwarded. It can use
 the following variables:
@@ -935,8 +1014,11 @@
 
 The default value is @code{t1-t2}, which means to always forward the stream
 that has a smaller timestamp.
+@end table
 
-Example: stress-test @code{amerge} by randomly sending buffers on the wrong
+@subsection Examples
+
+Stress-test @code{amerge} by randomly sending buffers on the wrong
 input, while avoiding too much of a desynchronization:
 @example
 amovie=file.ogg [a] ; amovie=file.mp3 [b] ;
@@ -952,15 +1034,21 @@
 specified then the filter will assume nominal 1.0 tempo. Tempo must
 be in the [0.5, 2.0] range.
 
-For example, to slow down audio to 80% tempo:
+@subsection Examples
+
+@itemize
+@item
+Slow down audio to 80% tempo:
 @example
 atempo=0.8
 @end example
 
-For example, to speed up audio to 125% tempo:
+@item
+To speed up audio to 125% tempo:
 @example
 atempo=1.25
 @end example
+@end itemize
 
 @section earwax
 
@@ -1073,6 +1161,8 @@
 
 The printed times and duration are expressed in seconds.
 
+The filter accepts the following options:
+
 @table @option
 @item duration, d
 Set silence duration until notification (default is 2 seconds).
@@ -1082,16 +1172,22 @@
 specified value) or amplitude ratio. Default is -60dB, or 0.001.
 @end table
 
+@subsection Examples
+
+@itemize
+@item
 Detect 5 seconds of silence with -50dB noise tolerance:
 @example
 silencedetect=n=-50dB:d=5
 @end example
 
+@item
 Complete example with @command{ffmpeg} to detect silence with 0.0001 noise
 tolerance in @file{silence.mp3}:
 @example
-ffmpeg -f lavfi -i amovie=silence.mp3,silencedetect=noise=0.0001 -f null -
+ffmpeg -i silence.mp3 -af silencedetect=noise=0.0001 -f null -
 @end example
+@end itemize
 
 @section asyncts
 Synchronize audio data with timestamps by squeezing/stretching it and/or
@@ -1159,7 +1255,7 @@
 Channel layout of the output stream.
 
 @item map
-Map channels from input to output. The argument is a comma-separated list of
+Map channels from input to output. The argument is a '|'-separated list of
 mappings, each in the @code{@var{in_channel}-@var{out_channel}} or
 @var{in_channel} form. @var{in_channel} can be either the name of the input
 channel (e.g. FL for front left) or its index in the input channel layout.
@@ -1173,14 +1269,14 @@
 
 For example, assuming a 5.1+downmix input MOV file
 @example
-ffmpeg -i in.mov -filter 'channelmap=map=DL-FL\,DR-FR' out.wav
+ffmpeg -i in.mov -filter 'channelmap=map=DL-FL|DR-FR' out.wav
 @end example
 will create an output WAV file tagged as stereo from the downmix channels of
 the input.
 
 To fix a 5.1 WAV improperly encoded in AAC's native channel order
 @example
-ffmpeg -i in.wav -filter 'channelmap=1\,2\,0\,5\,3\,4:channel_layout=5.1' out.wav
+ffmpeg -i in.wav -filter 'channelmap=1|2|0|5|3|4:channel_layout=5.1' out.wav
 @end example
 
 @section join
@@ -1196,7 +1292,7 @@
 Desired output channel layout. Defaults to stereo.
 
 @item map
-Map channels from inputs to output. The argument is a comma-separated list of
+Map channels from inputs to output. The argument is a '|'-separated list of
 mappings, each in the @code{@var{input_idx}.@var{in_channel}-@var{out_channel}}
 form. @var{input_idx} is the 0-based index of the input stream. @var{in_channel}
 can be either the name of the input channel (e.g. FL for front left) or its
@@ -1216,7 +1312,7 @@
 To build a 5.1 output from 6 single-channel streams:
 @example
 ffmpeg -i fl -i fr -i fc -i sl -i sr -i lfe -filter_complex
-'join=inputs=6:channel_layout=5.1:map=0.0-FL\,1.0-FR\,2.0-FC\,3.0-SL\,4.0-SR\,5.0-LFE'
+'join=inputs=6:channel_layout=5.1:map=0.0-FL|1.0-FR|2.0-FC|3.0-SL|4.0-SR|5.0-LFE'
 out
 @end example
 
@@ -1228,12 +1324,7 @@
 
 Adjust the input audio volume.
 
-The filter accepts the following named parameters. If the key of the
-first options is omitted, the arguments are interpreted according to
-the following syntax:
-@example
-volume=@var{volume}:@var{precision}
-@end example
+The filter accepts the following options:
 
 @table @option
 
@@ -1303,6 +1394,8 @@
 
 All volumes are in decibels relative to the maximum PCM value.
 
+@subsection Examples
+
 Here is an excerpt of the output:
 @example
 [Parsed_volumedetect_0 @ 0xa23120] mean_volume: -27 dB
@@ -1343,11 +1436,14 @@
 This source is mainly intended for a programmatic use, in particular
 through the interface defined in @file{libavfilter/asrc_abuffer.h}.
 
-It accepts the following mandatory parameters:
-@var{sample_rate}:@var{sample_fmt}:@var{channel_layout}
+It accepts the following named parameters:
 
 @table @option
 
+@item time_base
+Timebase which will be used for timestamps of submitted frames. It must be
+either a floating-point number or in @var{numerator}/@var{denominator} form.
+
 @item sample_rate
 The sample rate of the incoming audio buffers.
 
@@ -1369,9 +1465,10 @@
 
 @end table
 
-For example:
+@subsection Examples
+
 @example
-abuffer=44100:s16p:stereo
+abuffer=sample_rate=44100:sample_fmt=s16p:channel_layout=stereo
 @end example
 
 will instruct the source to accept planar 16bit signed stereo at 44100Hz.
@@ -1379,7 +1476,7 @@
 6 and the "stereo" channel layout corresponds to the value 0x3, this is
 equivalent to:
 @example
-abuffer=44100:6:0x3
+abuffer=sample_rate=44100:sample_fmt=6:channel_layout=0x3
 @end example
 
 @section aevalsrc
@@ -1390,18 +1487,13 @@
 channel), which are evaluated and used to generate a corresponding
 audio signal.
 
-It accepts the syntax: @var{exprs}[::@var{options}].
-@var{exprs} is a list of expressions separated by ":", one for each
-separate channel. In case the @var{channel_layout} is not
-specified, the selected channel layout depends on the number of
-provided expressions.
-
-@var{options} is an optional sequence of @var{key}=@var{value} pairs,
-separated by ":".
-
-The description of the accepted options follows.
+This source accepts the following options:
 
 @table @option
+@item exprs
+Set the '|'-separated expressions list for each separate channel. In case the
+@option{channel_layout} option is not specified, the selected channel layout
+depends on the number of provided expressions.
 
 @item channel_layout, c
 Set the channel layout. The number of channels in the specified layout
@@ -1442,7 +1534,6 @@
 @subsection Examples
 
 @itemize
-
 @item
 Generate silence:
 @example
@@ -1450,18 +1541,17 @@
 @end example
 
 @item
-
 Generate a sin signal with frequency of 440 Hz, set sample rate to
 8000 Hz:
 @example
-aevalsrc="sin(440*2*PI*t)::s=8000"
+aevalsrc="sin(440*2*PI*t):s=8000"
 @end example
 
 @item
 Generate a two channels signal, specify the channel layout (Front
 Center + Back Center) explicitly:
 @example
-aevalsrc="sin(420*2*PI*t):cos(430*2*PI*t)::c=FC|BC"
+aevalsrc="sin(420*2*PI*t)|cos(430*2*PI*t):c=FC|BC"
 @end example
 
 @item
@@ -1479,7 +1569,7 @@
 @item
 Generate 2.5 Hz binaural beats on a 360 Hz carrier:
 @example
-aevalsrc="0.1*sin(2*PI*(360-2.5/2)*t) : 0.1*sin(2*PI*(360+2.5/2)*t)"
+aevalsrc="0.1*sin(2*PI*(360-2.5/2)*t) | 0.1*sin(2*PI*(360+2.5/2)*t)"
 @end example
 
 @end itemize
@@ -1491,16 +1581,10 @@
 the source for filters which ignore the input data (for example the sox
 synth filter).
 
-It accepts an optional sequence of @var{key}=@var{value} pairs,
-separated by ":".
-
-The description of the accepted options follows.
+This source accepts the following options:
 
 @table @option
 
-@item sample_rate, s
-Specify the sample rate, and defaults to 44100.
-
 @item channel_layout, cl
 
 Specify the channel layout, and can be either an integer or a string
@@ -1511,19 +1595,29 @@
 @file{libavutil/channel_layout.c} for the mapping between strings and
 channel layout values.
 
+@item sample_rate, r
+Specify the sample rate, and defaults to 44100.
+
 @item nb_samples, n
 Set the number of samples per requested frames.
 
 @end table
 
-Follow some examples:
-@example
-#  set the sample rate to 48000 Hz and the channel layout to AV_CH_LAYOUT_MONO.
-anullsrc=r=48000:cl=4
+@subsection Examples
 
-# same as
+@itemize
+@item
+Set the sample rate to 48000 Hz and the channel layout to AV_CH_LAYOUT_MONO.
+@example
+anullsrc=r=48000:cl=4
+@end example
+
+@item
+Do the same operation with a more obvious syntax:
+@example
 anullsrc=r=48000:cl=mono
 @end example
+@end itemize
 
 @section abuffer
 Buffer audio frames, and make them available to the filter chain.
@@ -1561,10 +1655,7 @@
 
 Note that the flite library is not thread-safe.
 
-The source accepts parameters as a list of @var{key}=@var{value} pairs,
-separated by ":".
-
-The description of the accepted parameters follows.
+The filter accepts the following options:
 
 @table @option
 
@@ -1619,6 +1710,53 @@
 For more information about libflite, check:
 @url{http://www.speech.cs.cmu.edu/flite/}
 
+@section sine
+
+Generate an audio signal made of a sine wave with amplitude 1/8.
+
+The audio signal is bit-exact.
+
+The filter accepts the following options:
+
+@table @option
+
+@item frequency, f
+Set the carrier frequency. Default is 440 Hz.
+
+@item beep_factor, b
+Enable a periodic beep every second with frequency @var{beep_factor} times
+the carrier frequency. Default is 0, meaning the beep is disabled.
+
+@item sample_rate, s
+Specify the sample rate, default is 44100.
+
+@item duration, d
+Specify the duration of the generated audio stream.
+
+@item samples_per_frame
+Set the number of samples per output frame, default is 1024.
+@end table
+
+@subsection Examples
+
+@itemize
+
+@item
+Generate a simple 440 Hz sine wave:
+@example
+sine
+@end example
+
+@item
+Generate a 220 Hz sine wave with a 880 Hz beep each second, for 5 seconds:
+@example
+sine=220:4:d=5
+sine=f=220:b=4:d=5
+sine=frequency=220:beep_factor=4:duration=5
+@end example
+
+@end itemize
+
 @c man end AUDIO SOURCES
 
 @chapter Audio Sinks
@@ -1631,9 +1769,10 @@
 Buffer audio frames, and make them available to the end of filter chain.
 
 This sink is mainly intended for programmatic use, in particular
-through the interface defined in @file{libavfilter/buffersink.h}.
+through the interface defined in @file{libavfilter/buffersink.h}
+or the options system.
 
-It requires a pointer to an AVABufferSinkContext structure, which
+It accepts a pointer to an AVABufferSinkContext structure, which
 defines the incoming buffers' formats, to be passed as the opaque
 parameter to @code{avfilter_init_filter} for initialization.
 
@@ -1643,13 +1782,6 @@
 mainly useful as a template and to be employed in analysis / debugging
 tools.
 
-@section abuffersink
-This sink is intended for programmatic use. Frames that arrive on this sink can
-be retrieved by the calling program using the interface defined in
-@file{libavfilter/buffersink.h}.
-
-This filter accepts no parameters.
-
 @c man end AUDIO SINKS
 
 @chapter Video Filters
@@ -1713,9 +1845,7 @@
 In order to display the output lines, you need to set the loglevel at
 least to the AV_LOG_INFO value.
 
-This filter accepts a list of options in the form of
-@var{key}=@var{value} pairs separated by ":". A description of the
-accepted options follows.
+The filter accepts the following options:
 
 @table @option
 @item black_min_duration, d
@@ -1767,16 +1897,19 @@
 In order to display the output lines, you need to set the loglevel at
 least to the AV_LOG_INFO value.
 
-The filter accepts the syntax:
-@example
-blackframe[=@var{amount}:[@var{threshold}]]
-@end example
+The filter accepts the following options:
 
-@var{amount} is the percentage of the pixels that have to be below the
-threshold, and defaults to 98.
+@table @option
 
-@var{threshold} is the threshold below which a pixel value is
-considered black, and defaults to 32.
+@item amount
+Set the percentage of the pixels that have to be below the threshold, defaults
+to @code{98}.
+
+@item threshold, thresh
+Set the threshold below which a pixel value is considered black, defaults to
+@code{32}.
+
+@end table
 
 @section blend
 
@@ -1786,8 +1919,7 @@
 "top" layer and second input is "bottom" layer.
 Output terminates when shortest input terminates.
 
-This filter accepts a list of options in the form of @var{key}=@var{value}
-pairs separated by ":". A description of the accepted options follows.
+A description of the accepted options follows.
 
 @table @option
 @item c0_mode
@@ -1832,7 +1964,7 @@
 @item c3_opacity
 @item all_opacity
 Set blend opacity for specific pixel component or all pixel components in case
-of @var{all_expr}. Only used in combination with pixel component blend modes.
+of @var{all_opacity}. Only used in combination with pixel component blend modes.
 
 @item c0_expr
 @item c1_expr
@@ -1845,6 +1977,9 @@
 The expressions can use the following variables:
 
 @table @option
+@item N
+The sequential number of the filtered frame, starting from @code{0}.
+
 @item X
 @item Y
 the coordinates of the current sample
@@ -1871,10 +2006,9 @@
 @end table
 @end table
 
-Some examples follow.
+@subsection Examples
 
 @itemize
-
 @item
 Apply transition from bottom layer to top layer in first 10 seconds:
 @example
@@ -1892,10 +2026,18 @@
 
 Apply boxblur algorithm to the input video.
 
-The filter accepts parameters as a list of @var{key}=@var{value}
-pairs, separated by ":". If the key of the first options is omitted,
-the arguments are interpreted according to the syntax
-@option{luma_radius}:@option{luma_power}:@option{chroma_radius}:@option{chroma_power}:@option{alpha_radius}:@option{alpha_power}.
+The filter accepts the following options:
+
+@table @option
+
+@item luma_radius, lr
+@item luma_power, lp
+@item chroma_radius, cr
+@item chroma_power, cp
+@item alpha_radius, ar
+@item alpha_power, ap
+
+@end table
 
 A description of the accepted options follows.
 
@@ -1941,13 +2083,14 @@
 A value of 0 will disable the effect.
 @end table
 
-Some examples follow.
+@subsection Examples
 
 @itemize
 @item
 Apply a boxblur filter with luma, chroma, and alpha radius
 set to 2:
 @example
+boxblur=luma_radius=2:luma_power=1
 boxblur=2:1
 @end example
 
@@ -1960,18 +2103,134 @@
 @item
 Set luma and chroma radius to a fraction of the video dimension:
 @example
-boxblur=min(h\,w)/10:1:min(cw\,ch)/10:1
+boxblur=luma_radius=min(h\,w)/10:luma_power=1:chroma_radius=min(cw\,ch)/10:chroma_power=1
+@end example
+@end itemize
+
+@section colorbalance
+Modify intensity of primary colors (red, green and blue) of input frames.
+
+The filter allows an input frame to be adjusted in the shadows, midtones or highlights
+regions for the red-cyan, green-magenta or blue-yellow balance.
+
+A positive adjustment value shifts the balance towards the primary color, a negative
+value towards the complementary color.
+
+The filter accepts the following options:
+
+@table @option
+@item rs
+@item gs
+@item bs
+Adjust red, green and blue shadows (darkest pixels).
+
+@item rm
+@item gm
+@item bm
+Adjust red, green and blue midtones (medium pixels).
+
+@item rh
+@item gh
+@item bh
+Adjust red, green and blue highlights (brightest pixels).
+
+Allowed ranges for options are @code{[-1.0, 1.0]}. Defaults are @code{0}.
+@end table
+
+@subsection Examples
+
+@itemize
+@item
+Add red color cast to shadows:
+@example
+colorbalance=rs=.3
+@end example
+@end itemize
+
+@section colorchannelmixer
+
+Adjust video input frames by re-mixing color channels.
+
+This filter modifies a color channel by adding the values associated to
+the other channels of the same pixels. For example if the value to
+modify is red, the output value will be:
+@example
+@var{red}=@var{red}*@var{rr} + @var{blue}*@var{rb} + @var{green}*@var{rg} + @var{alpha}*@var{ra}
+@end example
+
+The filter accepts the following options:
+
+@table @option
+@item rr
+@item rg
+@item rb
+@item ra
+Adjust contribution of input red, green, blue and alpha channels for output red channel.
+Default is @code{1} for @var{rr}, and @code{0} for @var{rg}, @var{rb} and @var{ra}.
+
+@item gr
+@item gg
+@item gb
+@item ga
+Adjust contribution of input red, green, blue and alpha channels for output green channel.
+Default is @code{1} for @var{gg}, and @code{0} for @var{gr}, @var{gb} and @var{ga}.
+
+@item br
+@item bg
+@item bb
+@item ba
+Adjust contribution of input red, green, blue and alpha channels for output blue channel.
+Default is @code{1} for @var{bb}, and @code{0} for @var{br}, @var{bg} and @var{ba}.
+
+@item ar
+@item ag
+@item ab
+@item aa
+Adjust contribution of input red, green, blue and alpha channels for output alpha channel.
+Default is @code{1} for @var{aa}, and @code{0} for @var{ar}, @var{ag} and @var{ab}.
+
+Allowed ranges for options are @code{[-2.0, 2.0]}.
+@end table
+
+@subsection Examples
+
+@itemize
+@item
+Convert source to grayscale:
+@example
+colorchannelmixer=.3:.4:.3:0:.3:.4:.3:0:.3:.4:.3
 @end example
 @end itemize
 
 @section colormatrix
 
-The colormatrix filter allows conversion between any of the following color
-space: BT.709 (@var{bt709}), BT.601 (@var{bt601}), SMPTE-240M (@var{smpte240m})
-and FCC (@var{fcc}).
+Convert color matrix.
 
-The syntax of the parameters is @var{source}:@var{destination}:
+The filter accepts the following options:
 
+@table @option
+@item src
+@item dst
+Specify the source and destination color matrix. Both values must be
+specified.
+
+The accepted values are:
+@table @samp
+@item bt709
+BT.709
+
+@item bt601
+BT.601
+
+@item smpte240m
+SMPTE-240M
+
+@item fcc
+FCC
+@end table
+@end table
+
+For example to convert from BT.601 to SMPTE-240M, use the command:
 @example
 colormatrix=bt601:smpte240m
 @end example
@@ -1983,32 +2242,28 @@
 
 @section crop
 
-Crop the input video.
+Crop the input video to given dimensions.
 
-This filter accepts a list of @var{key}=@var{value} pairs as argument,
-separated by ':'. If the key of the first options is omitted, the
-arguments are interpreted according to the syntax
-@var{out_w}:@var{out_h}:@var{x}:@var{y}:@var{keep_aspect}.
+The filter accepts the following options:
 
-A description of the accepted options follows:
 @table @option
 @item w, out_w
-Set the crop area width. It defaults to @code{iw}.
+Width of the output video. It defaults to @code{iw}.
 This expression is evaluated only once during the filter
 configuration.
 
 @item h, out_h
-Set the crop area width. It defaults to @code{ih}.
+Height of the output video. It defaults to @code{ih}.
 This expression is evaluated only once during the filter
 configuration.
 
 @item x
-Set the expression for the x top-left coordinate of the cropped area.
+Horizontal position, in the input video, of the left edge of the output video.
 It defaults to @code{(in_w-out_w)/2}.
 This expression is evaluated per-frame.
 
 @item y
-Set the expression for the y top-left coordinate of the cropped area.
+Vertical position, in the input video, of the top edge of the output video.
 It defaults to @code{(in_h-out_h)/2}.
 This expression is evaluated per-frame.
 
@@ -2076,6 +2331,7 @@
 for @var{y} may depend on @var{x}.
 
 @subsection Examples
+
 @itemize
 @item
 Crop area with size 100x100 at position (12,34).
@@ -2103,6 +2359,7 @@
 @item
 Crop the input video central square:
 @example
+crop=out_w=in_h
 crop=in_h
 @end example
 
@@ -2160,74 +2417,183 @@
 parameters through the logging system. The detected dimensions
 correspond to the non-black area of the input video.
 
-It accepts the syntax:
-@example
-cropdetect[=@var{limit}[:@var{round}[:@var{reset}]]]
-@end example
+The filter accepts the following options:
 
 @table @option
 
 @item limit
-Threshold, which can be optionally specified from nothing (0) to
-everything (255), defaults to 24.
+Set higher black value threshold, which can be optionally specified
+from nothing (0) to everything (255). An intensity value greater
+to the set value is considered non-black. Default value is 24.
 
 @item round
-Value which the width/height should be divisible by, defaults to
-16. The offset is automatically adjusted to center the video. Use 2 to
-get only even dimensions (needed for 4:2:2 video). 16 is best when
-encoding to most video codecs.
+Set the value for which the width/height should be divisible by. The
+offset is automatically adjusted to center the video. Use 2 to get
+only even dimensions (needed for 4:2:2 video). 16 is best when
+encoding to most video codecs. Default value is 16.
 
-@item reset
-Counter that determines after how many frames cropdetect will reset
-the previously detected largest video area and start over to detect
-the current optimal crop area. Defaults to 0.
+@item reset_count, reset
+Set the counter that determines after how many frames cropdetect will
+reset the previously detected largest video area and start over to
+detect the current optimal crop area. Default value is 0.
 
 This can be useful when channel logos distort the video area. 0
 indicates never reset and return the largest area encountered during
 playback.
 @end table
 
-@section decimate
+@anchor{curves}
+@section curves
 
-Drop frames that do not differ greatly from the previous frame in
-order to reduce framerate.
+Apply color adjustments using curves.
 
-The main use of this filter is for very-low-bitrate encoding
-(e.g. streaming over dialup modem), but it could in theory be used for
-fixing movies that were inverse-telecined incorrectly.
+This filter is similar to the Adobe Photoshop and GIMP curves tools. Each
+component (red, green and blue) has its values defined by @var{N} key points
+tied from each other using a smooth curve. The x-axis represents the pixel
+values from the input frame, and the y-axis the new pixel values to be set for
+the output frame.
 
-The filter accepts parameters as a list of @var{key}=@var{value}
-pairs, separated by ":". If the key of the first options is omitted,
-the arguments are interpreted according to the syntax:
-@option{max}:@option{hi}:@option{lo}:@option{frac}.
+By default, a component curve is defined by the two points @var{(0;0)} and
+@var{(1;1)}. This creates a straight line where each original pixel value is
+"adjusted" to its own value, which means no change to the image.
 
-A description of the accepted options follows.
+The filter allows you to redefine these two points and add some more. A new
+curve (using a natural cubic spline interpolation) will be define to pass
+smoothly through all these new coordinates. The new defined points needs to be
+strictly increasing over the x-axis, and their @var{x} and @var{y} values must
+be in the @var{[0;1]} interval.  If the computed curves happened to go outside
+the vector spaces, the values will be clipped accordingly.
+
+If there is no key point defined in @code{x=0}, the filter will automatically
+insert a @var{(0;0)} point. In the same way, if there is no key point defined
+in @code{x=1}, the filter will automatically insert a @var{(1;1)} point.
+
+The filter accepts the following options:
 
 @table @option
-@item max
-Set the maximum number of consecutive frames which can be dropped (if
-positive), or the minimum interval between dropped frames (if
-negative). If the value is 0, the frame is dropped unregarding the
-number of previous sequentially dropped frames.
+@item preset
+Select one of the available color presets. This option can be used in addition
+to the @option{r}, @option{g}, @option{b} parameters; in this case, the later
+options takes priority on the preset values.
+Available presets are:
+@table @samp
+@item none
+@item color_negative
+@item cross_process
+@item darker
+@item increase_contrast
+@item lighter
+@item linear_contrast
+@item medium_contrast
+@item negative
+@item strong_contrast
+@item vintage
+@end table
+Default is @code{none}.
+@item master, m
+Set the master key points. These points will define a second pass mapping. It
+is sometimes called a "luminance" or "value" mapping. It can be used with
+@option{r}, @option{g}, @option{b} or @option{all} since it acts like a
+post-processing LUT.
+@item red, r
+Set the key points for the red component.
+@item green, g
+Set the key points for the green component.
+@item blue, b
+Set the key points for the blue component.
+@item all
+Set the key points for all components (not including master).
+Can be used in addition to the other key points component
+options. In this case, the unset component(s) will fallback on this
+@option{all} setting.
+@item psfile
+Specify a Photoshop curves file (@code{.asv}) to import the settings from.
+@end table
 
-Default value is 0.
+To avoid some filtergraph syntax conflicts, each key points list need to be
+defined using the following syntax: @code{x0/y0 x1/y1 x2/y2 ...}.
 
-@item hi
-@item lo
-@item frac
-Set the dropping threshold values.
+@subsection Examples
 
-Values for @option{hi} and @option{lo} are for 8x8 pixel blocks and
-represent actual pixel value differences, so a threshold of 64
-corresponds to 1 unit of difference for each pixel, or the same spread
-out differently over the block.
+@itemize
+@item
+Increase slightly the middle level of blue:
+@example
+curves=blue='0.5/0.58'
+@end example
 
-A frame is a candidate for dropping if no 8x8 blocks differ by more
-than a threshold of @option{hi}, and if no more than @option{frac} blocks (1
-meaning the whole image) differ by more than a threshold of @option{lo}.
+@item
+Vintage effect:
+@example
+curves=r='0/0.11 .42/.51 1/0.95':g='0.50/0.48':b='0/0.22 .49/.44 1/0.8'
+@end example
+Here we obtain the following coordinates for each components:
+@table @var
+@item red
+@code{(0;0.11) (0.42;0.51) (1;0.95)}
+@item green
+@code{(0;0) (0.50;0.48) (1;1)}
+@item blue
+@code{(0;0.22) (0.49;0.44) (1;0.80)}
+@end table
 
-Default value for @option{hi} is 64*12, default value for @option{lo} is
-64*5, and default value for @option{frac} is 0.33.
+@item
+The previous example can also be achieved with the associated built-in preset:
+@example
+curves=preset=vintage
+@end example
+
+@item
+Or simply:
+@example
+curves=vintage
+@end example
+
+@item
+Use a Photoshop preset and redefine the points of the green component:
+@example
+curves=psfile='MyCurvesPresets/purple.asv':green='0.45/0.53'
+@end example
+@end itemize
+
+@anchor{decimate}
+@section decimate
+
+Drop duplicated frames at regular intervals.
+
+The filter accepts the following options:
+
+@table @option
+@item cycle
+Set the number of frames from which one will be dropped. Setting this to
+@var{N} means one frame in every batch of @var{N} frames will be dropped.
+Default is @code{5}.
+
+@item dupthresh
+Set the threshold for duplicate detection. If the difference metric for a frame
+is less than or equal to this value, then it is declared as duplicate. Default
+is @code{1.1}
+
+@item scthresh
+Set scene change threshold. Default is @code{15}.
+
+@item blockx
+@item blocky
+Set the size of the x and y-axis blocks used during metric calculations.
+Larger blocks give better noise suppression, but also give worse detection of
+small movements. Must be a power of two. Default is @code{32}.
+
+@item ppsrc
+Mark main input as a pre-processed input and activate clean source input
+stream. This allows the input to be pre-processed with various filters to help
+the metrics calculation while keeping the frame selection lossless. When set to
+@code{1}, the first stream is for the pre-processed input, and the second
+stream is the clean source from where the kept frames are chosen. Default is
+@code{0}.
+
+@item chroma
+Set whether or not chroma is considered in the metric calculations. Default is
+@code{1}.
 @end table
 
 @section delogo
@@ -2236,12 +2602,7 @@
 pixels. Just set a rectangle covering the logo and watch it disappear
 (and sometimes something even uglier appear - your mileage may vary).
 
-The filter accepts parameters as a string of the form
-"@var{x}:@var{y}:@var{w}:@var{h}:@var{band}", or as a list of
-@var{key}=@var{value} pairs, separated by ":".
-
-The description of the accepted parameters follows.
-
+This filter accepts the following options:
 @table @option
 
 @item x, y
@@ -2263,20 +2624,13 @@
 
 @end table
 
-Some examples follow.
+@subsection Examples
 
 @itemize
-
 @item
 Set a rectangle covering the area with top left corner coordinates 0,0
 and size 100x77, setting a band of size 10:
 @example
-delogo=0:0:100:77:10
-@end example
-
-@item
-As the previous example, but use named options:
-@example
 delogo=x=0:y=0:w=100:h=77:band=10
 @end example
 
@@ -2288,14 +2642,14 @@
 filter helps remove camera shake from hand-holding a camera, bumping a
 tripod, moving on a vehicle, etc.
 
-The filter accepts parameters as a string of the form
-"@var{x}:@var{y}:@var{w}:@var{h}:@var{rx}:@var{ry}:@var{edge}:@var{blocksize}:@var{contrast}:@var{search}:@var{filename}"
-
-A description of the accepted parameters follows.
+The filter accepts the following options:
 
 @table @option
 
-@item x, y, w, h
+@item x
+@item y
+@item w
+@item h
 Specify a rectangular area where to limit the search for motion
 vectors.
 If desired the search for motion vectors can be limited to a
@@ -2313,25 +2667,25 @@
 
 Default - search the whole frame.
 
-@item rx, ry
+@item rx
+@item ry
 Specify the maximum extent of movement in x and y directions in the
 range 0-64 pixels. Default 16.
 
 @item edge
 Specify how to generate pixels to fill blanks at the edge of the
-frame. An integer from 0 to 3 as follows:
-@table @option
-@item 0
+frame. Available values are:
+@table @samp
+@item blank, 0
 Fill zeroes at blank locations
-@item 1
+@item original, 1
 Original image at blank locations
-@item 2
+@item clamp, 2
 Extruded edge value at blank locations
-@item 3
+@item mirror, 3
 Mirrored edge at blank locations
 @end table
-
-The default setting is mirror edge at blank locations.
+Default value is @samp{mirror}.
 
 @item blocksize
 Specify the blocksize to use for motion search. Range 4-128 pixels,
@@ -2343,25 +2697,30 @@
 pixels) will be considered. Range 1-255, default 125.
 
 @item search
-Specify the search strategy 0 = exhaustive search, 1 = less exhaustive
-search. Default - exhaustive search.
+Specify the search strategy. Available values are:
+@table @samp
+@item exhaustive, 0
+Set exhaustive search
+@item less, 1
+Set less exhaustive search.
+@end table
+Default value is @samp{exhaustive}.
 
 @item filename
 If set then a detailed log of the motion search is written to the
 specified file.
 
+@item opencl
+If set to 1, specify using OpenCL capabilities, only available if
+FFmpeg was configured with @code{--enable-opencl}. Default value is 0.
+
 @end table
 
 @section drawbox
 
 Draw a colored box on the input image.
 
-The filter accepts parameters as a list of @var{key}=@var{value}
-pairs, separated by ":". If the key of the first options is omitted,
-the arguments are interpreted according to the syntax
-@option{x}:@option{y}:@option{width}:@option{height}:@option{color}:@option{thickness}.
-
-A description of the accepted options follows.
+This filter accepts the following options:
 
 @table @option
 @item x, y
@@ -2382,7 +2741,8 @@
 Set the thickness of the box edge. Default value is @code{4}.
 @end table
 
-Some examples follow:
+@subsection Examples
+
 @itemize
 @item
 Draw a black box around the edge of the input image:
@@ -2419,9 +2779,6 @@
 
 @subsection Syntax
 
-The filter accepts parameters as a list of @var{key}=@var{value} pairs,
-separated by ":".
-
 The description of the accepted parameters follows.
 
 @table @option
@@ -2644,7 +3001,7 @@
 
 Note that they probably must also be escaped as the value for the
 @option{text} option in the filter argument string and as the filter
-argument in the filter graph description, and possibly also for the shell,
+argument in the filtergraph description, and possibly also for the shell,
 that makes up to four levels of escaping; using a text file avoids these
 problems.
 
@@ -2673,6 +3030,9 @@
 @item n, frame_num
 The frame number, starting from 0.
 
+@item pict_type
+A 1 character description of the current picture type.
+
 @item pts
 The timestamp of the current frame, in seconds, with microsecond accuracy.
 
@@ -2680,10 +3040,7 @@
 
 @subsection Examples
 
-Some examples follow.
-
 @itemize
-
 @item
 Draw "Test Text" with font FreeSerif, using the default values for the
 optional parameters.
@@ -2763,7 +3120,7 @@
 
 Detect and draw edges. The filter uses the Canny Edge Detection algorithm.
 
-This filter accepts the following optional named parameters:
+The filter accepts the following options:
 
 @table @option
 @item low, high
@@ -2790,34 +3147,42 @@
 
 Apply fade-in/out effect to input video.
 
-The filter accepts parameters as a list of @var{key}=@var{value}
-pairs, separated by ":". If the key of the first options is omitted,
-the arguments are interpreted according to the syntax
-@var{type}:@var{start_frame}:@var{nb_frames}.
-
-A description of the accepted parameters follows.
+This filter accepts the following options:
 
 @table @option
 @item type, t
-Specify if the effect type, can be either @code{in} for fade-in, or
-@code{out} for a fade-out effect. Default is @code{in}.
+The effect type -- can be either "in" for fade-in, or "out" for a fade-out
+effect.
+Default is @code{in}.
 
 @item start_frame, s
 Specify the number of the start frame for starting to apply the fade
 effect. Default is 0.
 
 @item nb_frames, n
-Specify the number of frames for which the fade effect has to last. At
-the end of the fade-in effect the output video will have the same
-intensity as the input video, at the end of the fade-out transition
-the output video will be completely black. Default is 25.
+The number of frames for which the fade effect has to last. At the end of the
+fade-in effect the output video will have the same intensity as the input video,
+at the end of the fade-out transition the output video will be completely black.
+Default is 25.
 
 @item alpha
 If set to 1, fade only alpha channel, if one exists on the input.
 Default value is 0.
+
+@item start_time, st
+Specify the timestamp (in seconds) of the frame to start to apply the fade
+effect. If both start_frame and start_time are specified, the fade will start at
+whichever comes last.  Default is 0.
+
+@item duration, d
+The number of seconds for which the fade effect has to last. At the end of the
+fade-in effect the output video will have the same intensity as the input video,
+at the end of the fade-out transition the output video will be completely black.
+If both duration and nb_frames are specified, duration is used. Default is 0.
 @end table
 
 @subsection Examples
+
 @itemize
 @item
 Fade in first 30 frames of video:
@@ -2834,6 +3199,7 @@
 Fade out last 45 frames of a 200-frame video:
 @example
 fade=out:155:45
+fade=type=out:start_frame=155:nb_frames=45
 @end example
 
 @item
@@ -2853,6 +3219,13 @@
 @example
 fade=in:0:25:alpha=1
 @end example
+
+@item
+Make first 5.5 seconds black, then fade in for 0.5 seconds:
+@example
+fade=t=in:st=5.5:d=0.5
+@end example
+
 @end itemize
 
 @section field
@@ -2861,7 +3234,8 @@
 arithmetic to avoid wasting CPU time. The output frames are marked as
 non-interlaced.
 
-This filter accepts the following named options:
+The filter accepts the following options:
+
 @table @option
 @item type
 Specify whether to extract the top (if the value is @code{0} or
@@ -2869,33 +3243,342 @@
 @code{bottom}).
 @end table
 
-If the option key is not specified, the first value sets the @var{type}
-option. For example:
+@section fieldmatch
+
+Field matching filter for inverse telecine. It is meant to reconstruct the
+progressive frames from a telecined stream. The filter does not drop duplicated
+frames, so to achieve a complete inverse telecine @code{fieldmatch} needs to be
+followed by a decimation filter such as @ref{decimate} in the filtergraph.
+
+The separation of the field matching and the decimation is notably motivated by
+the possibility of inserting a de-interlacing filter fallback between the two.
+If the source has mixed telecined and real interlaced content,
+@code{fieldmatch} will not be able to match fields for the interlaced parts.
+But these remaining combed frames will be marked as interlaced, and thus can be
+de-interlaced by a later filter such as @ref{yadif} before decimation.
+
+In addition to the various configuration options, @code{fieldmatch} can take an
+optional second stream, activated through the @option{ppsrc} option. If
+enabled, the frames reconstruction will be based on the fields and frames from
+this second stream. This allows the first input to be pre-processed in order to
+help the various algorithms of the filter, while keeping the output lossless
+(assuming the fields are matched properly). Typically, a field-aware denoiser,
+or brightness/contrast adjustments can help.
+
+Note that this filter uses the same algorithms as TIVTC/TFM (AviSynth project)
+and VIVTC/VFM (VapourSynth project). The later is a light clone of TFM from
+which @code{fieldmatch} is based on. While the semantic and usage are very
+close, some behaviour and options names can differ.
+
+The filter accepts the following options:
+
+@table @option
+@item order
+Specify the assumed field order of the input stream. Available values are:
+
+@table @samp
+@item auto
+Auto detect parity (use FFmpeg's internal parity value).
+@item bff
+Assume bottom field first.
+@item tff
+Assume top field first.
+@end table
+
+Note that it is sometimes recommended not to trust the parity announced by the
+stream.
+
+Default value is @var{auto}.
+
+@item mode
+Set the matching mode or strategy to use. @option{pc} mode is the safest in the
+sense that it wont risk creating jerkiness due to duplicate frames when
+possible, but if there are bad edits or blended fields it will end up
+outputting combed frames when a good match might actually exist. On the other
+hand, @option{pcn_ub} mode is the most risky in terms of creating jerkiness,
+but will almost always find a good frame if there is one. The other values are
+all somewhere in between @option{pc} and @option{pcn_ub} in terms of risking
+jerkiness and creating duplicate frames versus finding good matches in sections
+with bad edits, orphaned fields, blended fields, etc.
+
+More details about p/c/n/u/b are available in @ref{p/c/n/u/b meaning} section.
+
+Available values are:
+
+@table @samp
+@item pc
+2-way matching (p/c)
+@item pc_n
+2-way matching, and trying 3rd match if still combed (p/c + n)
+@item pc_u
+2-way matching, and trying 3rd match (same order) if still combed (p/c + u)
+@item pc_n_ub
+2-way matching, trying 3rd match if still combed, and trying 4th/5th matches if
+still combed (p/c + n + u/b)
+@item pcn
+3-way matching (p/c/n)
+@item pcn_ub
+3-way matching, and trying 4th/5th matches if all 3 of the original matches are
+detected as combed (p/c/n + u/b)
+@end table
+
+The parenthesis at the end indicate the matches that would be used for that
+mode assuming @option{order}=@var{tff} (and @option{field} on @var{auto} or
+@var{top}).
+
+In terms of speed @option{pc} mode is by far the fastest and @option{pcn_ub} is
+the slowest.
+
+Default value is @var{pc_n}.
+
+@item ppsrc
+Mark the main input stream as a pre-processed input, and enable the secondary
+input stream as the clean source to pick the fields from. See the filter
+introduction for more details. It is similar to the @option{clip2} feature from
+VFM/TFM.
+
+Default value is @code{0} (disabled).
+
+@item field
+Set the field to match from. It is recommended to set this to the same value as
+@option{order} unless you experience matching failures with that setting. In
+certain circumstances changing the field that is used to match from can have a
+large impact on matching performance. Available values are:
+
+@table @samp
+@item auto
+Automatic (same value as @option{order}).
+@item bottom
+Match from the bottom field.
+@item top
+Match from the top field.
+@end table
+
+Default value is @var{auto}.
+
+@item mchroma
+Set whether or not chroma is included during the match comparisons. In most
+cases it is recommended to leave this enabled. You should set this to @code{0}
+only if your clip has bad chroma problems such as heavy rainbowing or other
+artifacts. Setting this to @code{0} could also be used to speed things up at
+the cost of some accuracy.
+
+Default value is @code{1}.
+
+@item y0
+@item y1
+These define an exclusion band which excludes the lines between @option{y0} and
+@option{y1} from being included in the field matching decision. An exclusion
+band can be used to ignore subtitles, a logo, or other things that may
+interfere with the matching. @option{y0} sets the starting scan line and
+@option{y1} sets the ending line; all lines in between @option{y0} and
+@option{y1} (including @option{y0} and @option{y1}) will be ignored. Setting
+@option{y0} and @option{y1} to the same value will disable the feature.
+@option{y0} and @option{y1} defaults to @code{0}.
+
+@item scthresh
+Set the scene change detection threshold as a percentage of maximum change on
+the luma plane. Good values are in the @code{[8.0, 14.0]} range. Scene change
+detection is only relevant in case @option{combmatch}=@var{sc}.  The range for
+@option{scthresh} is @code{[0.0, 100.0]}.
+
+Default value is @code{12.0}.
+
+@item combmatch
+When @option{combatch} is not @var{none}, @code{fieldmatch} will take into
+account the combed scores of matches when deciding what match to use as the
+final match. Available values are:
+
+@table @samp
+@item none
+No final matching based on combed scores.
+@item sc
+Combed scores are only used when a scene change is detected.
+@item full
+Use combed scores all the time.
+@end table
+
+Default is @var{sc}.
+
+@item combdbg
+Force @code{fieldmatch} to calculate the combed metrics for certain matches and
+print them. This setting is known as @option{micout} in TFM/VFM vocabulary.
+Available values are:
+
+@table @samp
+@item none
+No forced calculation.
+@item pcn
+Force p/c/n calculations.
+@item pcnub
+Force p/c/n/u/b calculations.
+@end table
+
+Default value is @var{none}.
+
+@item cthresh
+This is the area combing threshold used for combed frame detection. This
+essentially controls how "strong" or "visible" combing must be to be detected.
+Larger values mean combing must be more visible and smaller values mean combing
+can be less visible or strong and still be detected. Valid settings are from
+@code{-1} (every pixel will be detected as combed) to @code{255} (no pixel will
+be detected as combed). This is basically a pixel difference value. A good
+range is @code{[8, 12]}.
+
+Default value is @code{9}.
+
+@item chroma
+Sets whether or not chroma is considered in the combed frame decision.  Only
+disable this if your source has chroma problems (rainbowing, etc.) that are
+causing problems for the combed frame detection with chroma enabled. Actually,
+using @option{chroma}=@var{0} is usually more reliable, except for the case
+where there is chroma only combing in the source.
+
+Default value is @code{0}.
+
+@item blockx
+@item blocky
+Respectively set the x-axis and y-axis size of the window used during combed
+frame detection. This has to do with the size of the area in which
+@option{combpel} pixels are required to be detected as combed for a frame to be
+declared combed. See the @option{combpel} parameter description for more info.
+Possible values are any number that is a power of 2 starting at 4 and going up
+to 512.
+
+Default value is @code{16}.
+
+@item combpel
+The number of combed pixels inside any of the @option{blocky} by
+@option{blockx} size blocks on the frame for the frame to be detected as
+combed. While @option{cthresh} controls how "visible" the combing must be, this
+setting controls "how much" combing there must be in any localized area (a
+window defined by the @option{blockx} and @option{blocky} settings) on the
+frame. Minimum value is @code{0} and maximum is @code{blocky x blockx} (at
+which point no frames will ever be detected as combed). This setting is known
+as @option{MI} in TFM/VFM vocabulary.
+
+Default value is @code{80}.
+@end table
+
+@anchor{p/c/n/u/b meaning}
+@subsection p/c/n/u/b meaning
+
+@subsubsection p/c/n
+
+We assume the following telecined stream:
+
 @example
-field=bottom
+Top fields:     1 2 2 3 4
+Bottom fields:  1 2 3 4 4
 @end example
 
-is equivalent to:
+The numbers correspond to the progressive frame the fields relate to. Here, the
+first two frames are progressive, the 3rd and 4th are combed, and so on.
+
+When @code{fieldmatch} is configured to run a matching from bottom
+(@option{field}=@var{bottom}) this is how this input stream get transformed:
+
 @example
-field=type=bottom
+Input stream:
+                T     1 2 2 3 4
+                B     1 2 3 4 4   <-- matching reference
+
+Matches:              c c n n c
+
+Output stream:
+                T     1 2 3 4 4
+                B     1 2 3 4 4
+@end example
+
+As a result of the field matching, we can see that some frames get duplicated.
+To perform a complete inverse telecine, you need to rely on a decimation filter
+after this operation. See for instance the @ref{decimate} filter.
+
+The same operation now matching from top fields (@option{field}=@var{top})
+looks like this:
+
+@example
+Input stream:
+                T     1 2 2 3 4   <-- matching reference
+                B     1 2 3 4 4
+
+Matches:              c c p p c
+
+Output stream:
+                T     1 2 2 3 4
+                B     1 2 2 3 4
+@end example
+
+In these examples, we can see what @var{p}, @var{c} and @var{n} mean;
+basically, they refer to the frame and field of the opposite parity:
+
+@itemize
+@item @var{p} matches the field of the opposite parity in the previous frame
+@item @var{c} matches the field of the opposite parity in the current frame
+@item @var{n} matches the field of the opposite parity in the next frame
+@end itemize
+
+@subsubsection u/b
+
+The @var{u} and @var{b} matching are a bit special in the sense that they match
+from the opposite parity flag. In the following examples, we assume that we are
+currently matching the 2nd frame (Top:2, bottom:2). According to the match, a
+'x' is placed above and below each matched fields.
+
+With bottom matching (@option{field}=@var{bottom}):
+@example
+Match:           c         p           n          b          u
+
+                 x       x               x        x          x
+  Top          1 2 2     1 2 2       1 2 2      1 2 2      1 2 2
+  Bottom       1 2 3     1 2 3       1 2 3      1 2 3      1 2 3
+                 x         x           x        x              x
+
+Output frames:
+                 2          1          2          2          2
+                 2          2          2          1          3
+@end example
+
+With top matching (@option{field}=@var{top}):
+@example
+Match:           c         p           n          b          u
+
+                 x         x           x        x              x
+  Top          1 2 2     1 2 2       1 2 2      1 2 2      1 2 2
+  Bottom       1 2 3     1 2 3       1 2 3      1 2 3      1 2 3
+                 x       x               x        x          x
+
+Output frames:
+                 2          2          2          1          2
+                 2          1          3          2          2
+@end example
+
+@subsection Examples
+
+Simple IVTC of a top field first telecined stream:
+@example
+fieldmatch=order=tff:combmatch=none, decimate
+@end example
+
+Advanced IVTC, with fallback on @ref{yadif} for still combed frames:
+@example
+fieldmatch=order=tff:combmatch=full, yadif=deint=interlaced, decimate
 @end example
 
 @section fieldorder
 
 Transform the field order of the input video.
 
-It accepts one parameter which specifies the required field order that
-the input interlaced video will be transformed to. The parameter can
-assume one of the following values:
+This filter accepts the following options:
 
 @table @option
-@item 0 or bff
-output bottom field first
-@item 1 or tff
-output top field first
+
+@item order
+Output field order. Valid values are @var{tff} for top field first or @var{bff}
+for bottom field first.
 @end table
 
-Default value is "tff".
+Default value is @samp{tff}.
 
 Transformation is achieved by shifting the picture content up or down
 by one line, and filling the remaining line with appropriate picture content.
@@ -2922,34 +3605,47 @@
 
 The filter does not take parameters.
 
+@anchor{format}
 @section format
 
 Convert the input video to one of the specified pixel formats.
 Libavfilter will try to pick one that is supported for the input to
 the next filter.
 
-The filter accepts a list of pixel format names, separated by ":",
-for example "yuv420p:monow:rgb24".
+This filter accepts the following parameters:
+@table @option
 
-Some examples follow:
+@item pix_fmts
+A '|'-separated list of pixel format names, for example
+"pix_fmts=yuv420p|monow|rgb24".
+
+@end table
+
+@subsection Examples
+
+@itemize
+@item
+Convert the input video to the format @var{yuv420p}
 @example
-# convert the input video to the format "yuv420p"
-format=yuv420p
-
-# convert the input video to any of the formats in the list
-format=yuv420p:yuv444p:yuv410p
+format=pix_fmts=yuv420p
 @end example
 
+Convert the input video to any of the formats in the list
+@example
+format=pix_fmts=yuv420p|yuv444p|yuv410p
+@end example
+@end itemize
+
 @section fps
 
-Convert the video to specified constant framerate by duplicating or dropping
+Convert the video to specified constant frame rate by duplicating or dropping
 frames as necessary.
 
 This filter accepts the following named parameters:
 @table @option
 
 @item fps
-Desired output framerate. The default is @code{25}.
+Desired output frame rate. The default is @code{25}.
 
 @item round
 Rounding method.
@@ -2978,10 +3674,14 @@
 
 @section framestep
 
-Select one frame every N.
+Select one frame every N-th frame.
 
-This filter accepts in input a string representing a positive
-integer. Default argument is @code{1}.
+This filter accepts the following option:
+@table @option
+@item step
+Select frame after every @code{step} frames.
+Allowed values are positive integers higher than 0. Default value is @code{1}.
+@end table
 
 @anchor{frei0r}
 @section frei0r
@@ -2991,21 +3691,22 @@
 To enable compilation of this filter you need to install the frei0r
 header and configure FFmpeg with @code{--enable-frei0r}.
 
-The filter supports the syntax:
-@example
-@var{filter_name}[@{:|=@}@var{param1}:@var{param2}:...:@var{paramN}]
-@end example
+This filter accepts the following options:
 
-@var{filter_name} is the name of the frei0r effect to load. If the
-environment variable @env{FREI0R_PATH} is defined, the frei0r effect
-is searched in each one of the directories specified by the colon (or
-semicolon on Windows platforms) separated list in @env{FREIOR_PATH},
+@table @option
+
+@item filter_name
+The name to the frei0r effect to load. If the environment variable
+@env{FREI0R_PATH} is defined, the frei0r effect is searched in each one of the
+directories specified by the colon separated list in @env{FREIOR_PATH},
 otherwise in the standard frei0r paths, which are in this order:
 @file{HOME/.frei0r-1/lib/}, @file{/usr/local/lib/frei0r-1/},
 @file{/usr/lib/frei0r-1/}.
 
-@var{param1}, @var{param2}, ... , @var{paramN} specify the parameters
-for the frei0r effect.
+@item filter_params
+A '|'-separated list of parameters to pass to the frei0r effect.
+
+@end table
 
 A frei0r effect parameter can be a boolean (whose values are specified
 with "y" and "n"), a double, a color (specified by the syntax
@@ -3017,13 +3718,13 @@
 The number and kind of parameters depend on the loaded effect. If an
 effect parameter is not specified the default value is set.
 
-Some examples follow:
+@subsection Examples
 
 @itemize
 @item
 Apply the distort0r effect, set the first two double parameters:
 @example
-frei0r=distort0r:0.5:0.01
+frei0r=filter_name=distort0r:filter_params=0.5|0.01
 @end example
 
 @item
@@ -3038,7 +3739,7 @@
 Apply the perspective effect, specify the top left and top right image
 positions:
 @example
-frei0r=perspective:0.2/0.2:0.8/0.2
+frei0r=perspective:0.2/0.2|0.8/0.2
 @end example
 @end itemize
 
@@ -3047,11 +3748,7 @@
 
 @section geq
 
-The filter takes one, two, three or four equations as parameter, separated by ':'.
-The first equation is mandatory and applies to the luma plane. The two
-following are respectively for chroma blue and chroma red planes.
-
-The filter syntax allows named parameters:
+The filter accepts the following options:
 
 @table @option
 @item lum_expr
@@ -3062,6 +3759,12 @@
 the chrominance red expression
 @item alpha_expr
 the alpha expression
+@item r
+the red expression
+@item g
+the green expression
+@item b
+the blue expression
 @end table
 
 If one of the chrominance expression is not defined, it falls back on the other
@@ -3075,13 +3778,16 @@
 @item N
 The sequential number of the filtered frame, starting from @code{0}.
 
-@item X, Y
+@item X
+@item Y
 The coordinates of the current sample.
 
-@item W, H
+@item W
+@item H
 The width and height of the image.
 
-@item SW, SH
+@item SW
+@item SH
 Width and height scale depending on the currently filtered plane. It is the
 ratio between the corresponding luma plane number of pixels and the current
 plane ones. E.g. for YUV4:2:0 the values are @code{1,1} for the luma plane, and
@@ -3114,7 +3820,7 @@
 For functions, if @var{x} and @var{y} are outside the area, the value will be
 automatically clipped to the closer edge.
 
-Some examples follow:
+@subsection Examples
 
 @itemize
 @item
@@ -3135,6 +3841,12 @@
 @example
 nullsrc=s=256x256,geq=random(1)/hypot(X-cos(N*0.07)*W/2-W/2\,Y-sin(N*0.09)*H/2-H/2)^2*1000000*sin(N*0.02):128:128
 @end example
+
+@item
+Generate a quick emboss effect:
+@example
+format=gray,geq=lum_expr='(p(X,Y)+(256-p(X-4,Y-4)))/2'
+@end example
 @end itemize
 
 @section gradfun
@@ -3148,22 +3860,21 @@
 lossy compression, because compression tends to lose the dither and
 bring back the bands.
 
-The filter accepts a list of options in the form of @var{key}=@var{value} pairs
-separated by ":". A description of the accepted options follows.
+This filter accepts the following options:
 
 @table @option
 
 @item strength
-The maximum amount by which the filter will change
-any one pixel. Also the threshold for detecting nearly flat
-regions. Acceptable values range from @code{0.51} to @code{64}, default value
-is @code{1.2}.
+The maximum amount by which the filter will change any one pixel. Also the
+threshold for detecting nearly flat regions. Acceptable values range from .51 to
+64, default value is 1.2, out-of-range values will be clipped to the valid
+range.
 
 @item radius
-The neighborhood to fit the gradient to. A larger
-radius makes for smoother gradients, but also prevents the filter from
-modifying the pixels near detailed regions. Acceptable values are
-@code{8-32}, default value is @code{16}.
+The neighborhood to fit the gradient to. A larger radius makes for smoother
+gradients, but also prevents the filter from modifying the pixels near detailed
+regions. Acceptable values are 8-32, default value is 16, out-of-range values
+will be clipped to the valid range.
 
 @end table
 
@@ -3208,12 +3919,7 @@
 useful only for correcting degraded or poorly captured source
 video.
 
-The filter accepts parameters as a list of @var{key}=@var{value}
-pairs, separated by ":". If the key of the first options is omitted,
-the arguments are interpreted according to syntax
-@var{strength}:@var{intensity}:@var{antibanding}.
-
-This filter accepts the following named options:
+The filter accepts the following options:
 
 @table @option
 @item strength
@@ -3242,7 +3948,7 @@
 The computed histogram is a representation of distribution of color components
 in an image.
 
-The filter accepts the following named parameters:
+The filter accepts the following options:
 
 @table @option
 @item mode
@@ -3350,6 +4056,7 @@
 
 @end itemize
 
+@anchor{hqdn3d}
 @section hqdn3d
 
 High precision/quality 3d denoise filter. This filter aims to reduce
@@ -3357,7 +4064,6 @@
 still. It should enhance compressibility.
 
 It accepts the following optional parameters:
-@var{luma_spatial}:@var{chroma_spatial}:@var{luma_tmp}:@var{chroma_tmp}
 
 @table @option
 @item luma_spatial
@@ -3381,24 +4087,27 @@
 
 Modify the hue and/or the saturation of the input.
 
-This filter accepts the following optional named options:
+This filter accepts the following options:
 
 @table @option
 @item h
-Specify the hue angle as a number of degrees. It accepts a float
-number or an expression, and defaults to 0.0.
-
-@item H
-Specify the hue angle as a number of degrees. It accepts a float
-number or an expression, and defaults to 0.0.
+Specify the hue angle as a number of degrees. It accepts an expression,
+and defaults to "0".
 
 @item s
-Specify the saturation in the [-10,10] range. It accepts a float number and
-defaults to 1.0.
+Specify the saturation in the [-10,10] range. It accepts an expression and
+defaults to "1".
+
+@item H
+Specify the hue angle as a number of radians. It accepts an
+expression, and defaults to "0".
 @end table
 
-The @var{h}, @var{H} and @var{s} parameters are expressions containing the
-following constants:
+@option{h} and @option{H} are mutually exclusive, and can't be
+specified at the same time.
+
+The @option{h}, @option{H} and @option{s} option values are
+expressions containing the following constants:
 
 @table @option
 @item n
@@ -3417,11 +4126,8 @@
 time base of the input video
 @end table
 
-The options can also be set using the syntax: @var{hue}:@var{saturation}
+@subsection Examples
 
-In this case @var{hue} is expressed in degrees.
-
-Some examples follow:
 @itemize
 @item
 Set the hue to 90 degrees and the saturation to 1.0:
@@ -3436,19 +4142,6 @@
 @end example
 
 @item
-Same command without named options, hue must be expressed in degrees:
-@example
-hue=90:1
-@end example
-
-@item
-Note that "h:s" syntax does not support expressions for the values of
-h and s, so the following example will issue an error:
-@example
-hue=PI/2:1
-@end example
-
-@item
 Rotate hue and make the saturation swing between 0
 and 2 over a period of 1 second:
 @example
@@ -3481,14 +4174,16 @@
 
 @subsection Commands
 
-This filter supports the following command:
+This filter supports the following commands:
 @table @option
-@item reinit
+@item s
+@item h
+@item H
 Modify the hue and/or the saturation of the input video.
-The command accepts the same named options and syntax than when calling the
-filter from the command-line.
+The command accepts the same syntax of the corresponding option.
 
-If a parameter is omitted, it is kept at its current value.
+If the specified expression is not valid, it is kept at its current
+value.
 @end table
 
 @section idet
@@ -3498,6 +4193,15 @@
 This filter tries to detect if the input is interlaced or progressive,
 top or bottom field first.
 
+The filter accepts the following options:
+
+@table @option
+@item intl_thres
+Set interlacing threshold.
+@item prog_thres
+Set progressive threshold.
+@end table
+
 @section il
 
 Deinterleave or interleave fields.
@@ -3508,8 +4212,7 @@
 half of the output image, even lines to the bottom half.
 You can process (filter) them independently and then re-interleave them.
 
-It accepts a list of options in the form of @var{key}=@var{value} pairs
-separated by ":". A description of the accepted options follows.
+The filter accepts the following options:
 
 @table @option
 @item luma_mode, l
@@ -3536,17 +4239,42 @@
 Swap luma/chroma/alpha fields. Exchange even & odd lines. Default value is @code{0}.
 @end table
 
+@section interlace
+
+Simple interlacing filter from progressive contents. This interleaves upper (or
+lower) lines from odd frames with lower (or upper) lines from even frames,
+halving the frame rate and preserving image height.
+
+@example
+   Original        Original             New Frame
+   Frame 'j'      Frame 'j+1'             (tff)
+  ==========      ===========       ==================
+    Line 0  -------------------->    Frame 'j' Line 0
+    Line 1          Line 1  ---->   Frame 'j+1' Line 1
+    Line 2 --------------------->    Frame 'j' Line 2
+    Line 3          Line 3  ---->   Frame 'j+1' Line 3
+     ...             ...                   ...
+New Frame + 1 will be generated by Frame 'j+2' and Frame 'j+3' and so on
+@end example
+
+It accepts the following optional parameters:
+
+@table @option
+@item scan
+determines whether the interlaced frame is taken from the even (tff - default)
+or odd (bff) lines of the progressive frame.
+
+@item lowpass
+Enable (default) or disable the vertical lowpass filter to avoid twitter
+interlacing and reduce moire patterns.
+@end table
+
 @section kerndeint
 
 Deinterlace input video by applying Donald Graft's adaptive kernel
 deinterling. Work on interlaced parts of a video to produce
 progressive frames.
 
-This filter accepts parameters as a list of @var{key}=@var{value}
-pairs, separated by ":". If the key of the first options is omitted,
-the arguments are interpreted according to the following syntax:
-@var{thresh}:@var{map}:@var{order}:@var{sharp}:@var{twoway}.
-
 The description of the accepted parameters follows.
 
 @table @option
@@ -3601,12 +4329,7 @@
 @var{lutyuv} applies a lookup table to a YUV input video, @var{lutrgb}
 to an RGB input video.
 
-These filters accept in input a ":"-separated list of options, which
-specify the expressions used for computing the lookup table for the
-corresponding pixel component values.
-
-The @var{lut} filter requires either YUV or RGB pixel formats in
-input, and accepts the options:
+These filters accept the following options:
 @table @option
 @item c0
 set first pixel component expression
@@ -3616,14 +4339,7 @@
 set third pixel component expression
 @item c3
 set fourth pixel component expression, corresponds to the alpha component
-@end table
 
-The exact component associated to each option depends on the format in
-input.
-
-The @var{lutrgb} filter requires RGB pixel formats in input, and
-accepts the options:
-@table @option
 @item r
 set red component expression
 @item g
@@ -3632,21 +4348,24 @@
 set blue component expression
 @item a
 alpha component expression
-@end table
 
-The @var{lutyuv} filter requires YUV pixel formats in input, and
-accepts the options:
-@table @option
 @item y
 set Y/luminance component expression
 @item u
 set U/Cb component expression
 @item v
 set V/Cr component expression
-@item a
-set alpha component expression
 @end table
 
+Each of them specifies the expression to use for computing the lookup table for
+the corresponding pixel component values.
+
+The exact component associated to each of the @var{c*} options depends on the
+format in input.
+
+The @var{lut} filter requires either YUV or RGB pixel formats in input,
+@var{lutrgb} requires RGB pixel formats in input, and @var{lutyuv} requires YUV.
+
 The expressions can contain the following constants and functions:
 
 @table @option
@@ -3735,6 +4454,12 @@
 @example
 lutyuv=y=gammaval(0.5)
 @end example
+
+@item
+Discard least significant bits of luma:
+@example
+lutyuv=y='bitand(val, 128+64+32)'
+@end example
 @end itemize
 
 @section mp
@@ -3758,17 +4483,12 @@
 
 The list of the currently supported filters follows:
 @table @var
-@item detc
 @item dint
-@item divtc
-@item down3dright
 @item eq2
 @item eq
 @item fil
 @item fspp
-@item harddup
 @item ilpack
-@item ivtc
 @item mcdeint
 @item ow
 @item perspective
@@ -3779,7 +4499,6 @@
 @item sab
 @item softpulldown
 @item spp
-@item telecine
 @item tinterlace
 @item uspp
 @end table
@@ -3788,7 +4507,8 @@
 of the corresponding MPlayer filters. For detailed instructions check
 the "VIDEO FILTERS" section in the MPlayer manual.
 
-Some examples follow:
+@subsection Examples
+
 @itemize
 @item
 Adjust gamma, brightness, contrast:
@@ -3799,6 +4519,45 @@
 
 See also mplayer(1), @url{http://www.mplayerhq.hu/}.
 
+@section mpdecimate
+
+Drop frames that do not differ greatly from the previous frame in
+order to reduce frame rate.
+
+The main use of this filter is for very-low-bitrate encoding
+(e.g. streaming over dialup modem), but it could in theory be used for
+fixing movies that were inverse-telecined incorrectly.
+
+A description of the accepted options follows.
+
+@table @option
+@item max
+Set the maximum number of consecutive frames which can be dropped (if
+positive), or the minimum interval between dropped frames (if
+negative). If the value is 0, the frame is dropped unregarding the
+number of previous sequentially dropped frames.
+
+Default value is 0.
+
+@item hi
+@item lo
+@item frac
+Set the dropping threshold values.
+
+Values for @option{hi} and @option{lo} are for 8x8 pixel blocks and
+represent actual pixel value differences, so a threshold of 64
+corresponds to 1 unit of difference for each pixel, or the same spread
+out differently over the block.
+
+A frame is a candidate for dropping if no 8x8 blocks differ by more
+than a threshold of @option{hi}, and if no more than @option{frac} blocks (1
+meaning the whole image) differ by more than a threshold of @option{lo}.
+
+Default value for @option{hi} is 64*12, default value for @option{lo} is
+64*5, and default value for @option{frac} is 0.33.
+@end table
+
+
 @section negate
 
 Negate input video.
@@ -3811,25 +4570,37 @@
 Force libavfilter not to use any of the specified pixel formats for the
 input to the next filter.
 
-The filter accepts a list of pixel format names, separated by ":",
-for example "yuv420p:monow:rgb24".
+This filter accepts the following parameters:
+@table @option
 
-Some examples follow:
+@item pix_fmts
+A '|'-separated list of pixel format names, for example
+"pix_fmts=yuv420p|monow|rgb24".
+
+@end table
+
+@subsection Examples
+
+@itemize
+@item
+Force libavfilter to use a format different from @var{yuv420p} for the
+input to the vflip filter:
 @example
-# force libavfilter to use a format different from "yuv420p" for the
-# input to the vflip filter
-noformat=yuv420p,vflip
-
-# convert the input video to any of the formats not contained in the list
-noformat=yuv420p:yuv444p:yuv410p
+noformat=pix_fmts=yuv420p,vflip
 @end example
 
+@item
+Convert the input video to any of the formats not contained in the list:
+@example
+noformat=yuv420p|yuv444p|yuv410p
+@end example
+@end itemize
+
 @section noise
 
 Add noise on video input frame.
 
-This filter accepts a list of options in the form of @var{key}=@var{value}
-pairs separated by ":". A description of the accepted options follows.
+The filter accepts the following options:
 
 @table @option
 @item all_seed
@@ -3840,7 +4611,7 @@
 Set noise seed for specific pixel component or all pixel components in case
 of @var{all_seed}. Default value is @code{123457}.
 
-@item all_strength, as
+@item all_strength, alls
 @item c0_strength, c0s
 @item c1_strength, c1s
 @item c2_strength, c2s
@@ -3848,7 +4619,7 @@
 Set noise strength for specific pixel component or all pixel components in case
 @var{all_strength}. Default value is @code{0}. Allowed range is [0, 100].
 
-@item all_flags, af
+@item all_flags, allf
 @item c0_flags, c0f
 @item c1_flags, c1f
 @item c2_flags, c2f
@@ -3869,9 +4640,10 @@
 @end table
 @end table
 
-Some examples follow:
-@example
+@subsection Examples
+
 Add temporal and uniform noise to input video:
+@example
 noise=alls=20:allf=t+u
 @end example
 
@@ -3886,12 +4658,18 @@
 To enable this filter install libopencv library and headers and
 configure FFmpeg with @code{--enable-libopencv}.
 
-The filter takes the parameters: @var{filter_name}@{:=@}@var{filter_params}.
+This filter accepts the following parameters:
 
-@var{filter_name} is the name of the libopencv filter to apply.
+@table @option
 
-@var{filter_params} specifies the parameters to pass to the libopencv
-filter. If not specified the default values are assumed.
+@item filter_name
+The name of the libopencv filter to apply.
+
+@item filter_params
+The parameters to pass to the libopencv filter. If not specified the default
+values are assumed.
+
+@end table
 
 Refer to the official libopencv documentation for more precise
 information:
@@ -3905,7 +4683,7 @@
 Dilate an image by using a specific structuring element.
 This filter corresponds to the libopencv function @code{cvDilate}.
 
-It accepts the parameters: @var{struct_el}:@var{nb_iterations}.
+It accepts the parameters: @var{struct_el}|@var{nb_iterations}.
 
 @var{struct_el} represents a structuring element, and has the syntax:
 @var{cols}x@var{rows}+@var{anchor_x}x@var{anchor_y}/@var{shape}
@@ -3933,7 +4711,7 @@
 ocv=dilate
 
 # dilate using a structuring element with a 5x5 cross, iterate two times
-ocv=dilate=5x5+2x2/cross:2
+ocv=filter_name=dilate:filter_params=5x5+2x2/cross|2
 
 # read the shape from the file diamond.shape, iterate two times
 # the file diamond.shape may contain a pattern of characters like this:
@@ -3943,7 +4721,7 @@
 #  ***
 #   *
 # the specified cols and rows are ignored (but not the anchor point coordinates)
-ocv=0x0+2x2/custom=diamond.shape:2
+ocv=dilate:0x0+2x2/custom=diamond.shape|2
 @end example
 
 @subsection erode
@@ -3959,7 +4737,7 @@
 Smooth the input video.
 
 The filter takes the following parameters:
-@var{type}:@var{param1}:@var{param2}:@var{param3}:@var{param4}.
+@var{type}|@var{param1}|@var{param2}|@var{param3}|@var{param4}.
 
 @var{type} is the type of smooth filter to apply, and can be one of
 the following values: "blur", "blur_no_scale", "median", "gaussian",
@@ -3984,33 +4762,37 @@
 It takes two inputs and one output, the first input is the "main"
 video on which the second input is overlayed.
 
-This filter accepts a list of @var{key}=@var{value} pairs as argument,
-separated by ":". If the key of the first options is omitted, the
-arguments are interpreted according to the syntax @var{x}:@var{y}.
+This filter accepts the following parameters:
 
 A description of the accepted options follows.
 
 @table @option
-@item x, y
+@item x
+@item y
 Set the expression for the x and y coordinates of the overlayed video
-on the main video. Default value is 0.
+on the main video. Default value is "0" for both expressions. In case
+the expression is invalid, it is set to a huge value (meaning that the
+overlay will not be displayed within the output visible area).
 
-The @var{x} and @var{y} expressions can contain the following
-parameters:
-@table @option
-@item main_w, main_h
-main input width and height
+@item eval
+Set when the expressions for @option{x}, and @option{y} are evaluated.
 
-@item W, H
-same as @var{main_w} and @var{main_h}
+It accepts the following values:
+@table @samp
+@item init
+only evaluate expressions once during the filter initialization or
+when a command is processed
 
-@item overlay_w, overlay_h
-overlay input width and height
-
-@item w, h
-same as @var{overlay_w} and @var{overlay_h}
+@item frame
+evaluate expressions for each incoming frame
 @end table
 
+Default value is @samp{frame}.
+
+@item shortest
+If set to 1, force the output to terminate when the shortest input
+terminates. Default value is 0.
+
 @item format
 Set the format for the output video.
 
@@ -4033,11 +4815,49 @@
 color space. Default value is 0. This option is deprecated, use
 @option{format} instead.
 
-@item shortest
-If set to 1, force the output to terminate when the shortest input
-terminates. Default value is 0.
+@item repeatlast
+If set to 1, force the filter to draw the last overlay frame over the
+main input until the end of the stream. A value of 0 disables this
+behavior, which is enabled by default.
 @end table
 
+The @option{x}, and @option{y} expressions can contain the following
+parameters.
+
+@table @option
+@item main_w, W
+@item main_h, H
+main input width and height
+
+@item overlay_w, w
+@item overlay_h, h
+overlay input width and height
+
+@item x
+@item y
+the computed values for @var{x} and @var{y}. They are evaluated for
+each new frame.
+
+@item hsub
+@item vsub
+horizontal and vertical chroma subsample values of the output
+format. For example for the pixel format "yuv422p" @var{hsub} is 2 and
+@var{vsub} is 1.
+
+@item n
+the number of input frame, starting from 0
+
+@item pos
+the position in the file of the input frame, NAN if unknown
+
+@item t
+timestamp expressed in seconds, NAN if the input timestamp is unknown
+@end table
+
+Note that the @var{n}, @var{pos}, @var{t} variables are available only
+when evaluation is done @emph{per frame}, and will evaluate to NAN
+when @option{eval} is set to @samp{init}.
+
 Be aware that frames are taken from each input video in timestamp
 order, hence, if their initial timestamps differ, it is a a good idea
 to pass the two inputs through a @var{setpts=PTS-STARTPTS} filter to
@@ -4047,6 +4867,19 @@
 You can chain together more overlays but you should test the
 efficiency of such approach.
 
+@subsection Commands
+
+This filter supports the following commands:
+@table @option
+@item x
+@item y
+Modify the x and y of the overlay input.
+The command accepts the same syntax of the corresponding option.
+
+If the specified expression is not valid, it is kept at its current
+value.
+@end table
+
 @subsection Examples
 
 @itemize
@@ -4073,14 +4906,14 @@
 Insert 2 different transparent PNG logos (second logo on bottom
 right corner) using the @command{ffmpeg} tool:
 @example
-ffmpeg -i input -i logo1 -i logo2 -filter_complex 'overlay=10:H-h-10,overlay=W-w-10:H-h-10' output
+ffmpeg -i input -i logo1 -i logo2 -filter_complex 'overlay=x=10:y=H-h-10,overlay=x=W-w-10:y=H-h-10' output
 @end example
 
 @item
-Add a transparent color layer on top of the main video, WxH specifies
-the size of the main input to the overlay filter:
+Add a transparent color layer on top of the main video, @code{WxH}
+must specify the size of the main input to the overlay filter:
 @example
-color=red@@.3:WxH [over]; [in][over] overlay [out]
+color=color=red@@.3:size=WxH [over]; [in][over] overlay [out]
 @end example
 
 @item
@@ -4096,6 +4929,13 @@
 @end example
 
 @item
+Make a sliding overlay appearing from the left to the right top part of the
+screen starting since time 2:
+@example
+overlay=x='if(gte(t,2), -w+(t-2)*20, NAN)':y=0
+@end example
+
+@item
 Compose output by putting two input videos side to side:
 @example
 ffmpeg -i left.avi -i right.avi -filter_complex "
@@ -4125,14 +4965,7 @@
 Add paddings to the input image, and place the original input at the
 given coordinates @var{x}, @var{y}.
 
-The filter accepts parameters as a list of @var{key}=@var{value} pairs,
-separated by ":".
-
-If the key of the first options is omitted, the arguments are
-interpreted according to the syntax
-@var{width}:@var{height}:@var{x}:@var{y}:@var{color}.
-
-A description of the accepted options follows.
+This filter accepts the following parameters:
 
 @table @option
 @item width, w
@@ -4278,6 +5111,13 @@
 Each subfilter and some options have a short and a long name that can be used
 interchangeably, i.e. dr/dering are the same.
 
+The filters accept the following options:
+
+@table @option
+@item subfilters
+Set postprocessing subfilters string.
+@end table
+
 All subfilters share common options to determine their scope:
 
 @table @option
@@ -4294,12 +5134,12 @@
 Do chrominance filtering only (no luminance).
 @end table
 
-These options can be appended after the subfilter name, separated by a ':'.
+These options can be appended after the subfilter name, separated by a '|'.
 
 Available subfilters are:
 
 @table @option
-@item hb/hdeblock[:difference[:flatness]]
+@item hb/hdeblock[|difference[|flatness]]
 Horizontal deblocking filter
 @table @option
 @item difference
@@ -4308,7 +5148,7 @@
 Flatness threshold where lower values mean more deblocking (default: @code{39}).
 @end table
 
-@item vb/vdeblock[:difference[:flatness]]
+@item vb/vdeblock[|difference[|flatness]]
 Vertical deblocking filter
 @table @option
 @item difference
@@ -4317,7 +5157,7 @@
 Flatness threshold where lower values mean more deblocking (default: @code{39}).
 @end table
 
-@item ha/hadeblock[:difference[:flatness]]
+@item ha/hadeblock[|difference[|flatness]]
 Accurate horizontal deblocking filter
 @table @option
 @item difference
@@ -4326,7 +5166,7 @@
 Flatness threshold where lower values mean more deblocking (default: @code{39}).
 @end table
 
-@item va/vadeblock[:difference[:flatness]]
+@item va/vadeblock[|difference[|flatness]]
 Accurate vertical deblocking filter
 @table @option
 @item difference
@@ -4350,7 +5190,7 @@
 @item dr/dering
 Deringing filter
 
-@item tn/tmpnoise[:threshold1[:threshold2[:threshold3]]], temporal noise reducer
+@item tn/tmpnoise[|threshold1[|threshold2[|threshold3]]], temporal noise reducer
 @table @option
 @item threshold1
 larger -> stronger filtering
@@ -4390,7 +5230,7 @@
 Vertically applied FIR lowpass deinterlacing filter that deinterlaces the given
 block by filtering all lines with a @code{(-1 2 6 2 -1)} filter.
 
-@item fq/forceQuant[:quantizer]
+@item fq/forceQuant[|quantizer]
 Overrides the quantizer table from the input with the constant quantizer you
 specify.
 @table @option
@@ -4399,13 +5239,13 @@
 @end table
 
 @item de/default
-Default pp filter combination (@code{hb:a,vb:a,dr:a})
+Default pp filter combination (@code{hb|a,vb|a,dr|a})
 
 @item fa/fast
-Fast pp filter combination (@code{h1:a,v1:a,dr:a})
+Fast pp filter combination (@code{h1|a,v1|a,dr|a})
 
 @item ac
-High quality pp filter combination (@code{ha:a:128:7,va:a,dr:a})
+High quality pp filter combination (@code{ha|a|128|7,va|a,dr|a})
 @end table
 
 @subsection Examples
@@ -4427,14 +5267,14 @@
 @item
 Apply default filters and temporal denoiser:
 @example
-pp=default/tmpnoise:1:2:3
+pp=default/tmpnoise|1|2|3
 @end example
 
 @item
 Apply deblocking on luminance only, and switch vertical deblocking on or off
 automatically depending on available CPU time:
 @example
-pp=hb:y/vb:a
+pp=hb|y/vb|a
 @end example
 @end itemize
 
@@ -4444,10 +5284,14 @@
 pixels comprise the logo. It works by filling in the pixels that
 comprise the logo with neighboring pixels.
 
-This filter requires one argument which specifies the filter bitmap
-file, which can be any image format supported by libavformat. The
-width and height of the image file must match those of the video
-stream being processed.
+The filters accept the following options:
+
+@table @option
+@item filename, f
+Set the filter bitmap file, which can be any image format supported by
+libavformat. The width and height of the image file must match those of the
+video stream being processed.
+@end table
 
 Pixels in the provided bitmap image with a value of zero are not
 considered part of the logo, non-zero pixels are considered part of
@@ -4464,6 +5308,140 @@
 the image and will destroy more information than necessary, and extra
 pixels will slow things down on a large logo.
 
+@anchor{vidstabtransform}
+@section vidstabtransform
+
+Video stabilization/deshaking: pass 2 of 2,
+see @ref{vidstabdetect} for pass 1.
+
+Read a file with transform information for each frame and
+apply/compensate them. Together with the @ref{vidstabdetect}
+filter this can be used to deshake videos. See also
+@url{http://public.hronopik.de/vid.stab}. It is important to also use
+the unsharp filter, see below.
+
+To enable compilation of this filter you need to configure FFmpeg with
+@code{--enable-libvidstab}.
+
+This filter accepts the following options:
+
+@table @option
+
+@item input
+path to the file used to read the transforms (default: @file{transforms.trf})
+
+@item smoothing
+number of frames (value*2 + 1) used for lowpass filtering the camera movements
+(default: 10). For example a number of 10 means that 21 frames are used
+(10 in the past and 10 in the future) to smoothen the motion in the
+video. A larger values leads to a smoother video, but limits the
+acceleration of the camera (pan/tilt movements).
+
+@item maxshift
+maximal number of pixels to translate frames (default: -1 no limit)
+
+@item maxangle
+maximal angle in radians (degree*PI/180) to rotate frames (default: -1
+no limit)
+
+@item crop
+How to deal with borders that may be visible due to movement
+compensation. Available values are:
+
+@table @samp
+@item keep
+keep image information from previous frame (default)
+@item black
+fill the border black
+@end table
+
+@item invert
+@table @samp
+@item 0
+ keep transforms normal (default)
+@item 1
+ invert transforms
+@end table
+
+
+@item relative
+consider transforms as
+@table @samp
+@item 0
+ absolute
+@item 1
+ relative to previous frame (default)
+@end table
+
+
+@item zoom
+percentage to zoom (default: 0)
+@table @samp
+@item >0
+  zoom in
+@item <0
+  zoom out
+@end table
+
+@item optzoom
+if 1 then optimal zoom value is determined (default).
+Optimal zoom means no (or only little) border should be visible.
+Note that the value given at zoom is added to the one calculated
+here.
+
+@item interpol
+type of interpolation
+
+Available values are:
+@table @samp
+@item no
+no interpolation
+@item linear
+linear only horizontal
+@item bilinear
+linear in both directions (default)
+@item bicubic
+cubic in both directions (slow)
+@end table
+
+@item tripod
+virtual tripod mode means that the video is stabilized such that the
+camera stays stationary. Use also @code{tripod} option of
+@ref{vidstabdetect}.
+@table @samp
+@item 0
+off (default)
+@item 1
+virtual tripod mode: equivalent to @code{relative=0:smoothing=0}
+@end table
+
+@end table
+
+@subsection Examples
+
+@itemize
+@item
+typical call with default default values:
+ (note the unsharp filter which is always recommended)
+@example
+ffmpeg -i inp.mpeg -vf vidstabtransform,unsharp=5:5:0.8:3:3:0.4 inp_stabilized.mpeg
+@end example
+
+@item
+zoom in a bit more and load transform data from a given file
+@example
+vidstabtransform=zoom=5:input="mytransforms.trf"
+@end example
+
+@item
+smoothen the video even more
+@example
+vidstabtransform=smoothing=30
+@end example
+
+@end itemize
+
+
 @section scale
 
 Scale (resize) the input video, using the libswscale library.
@@ -4471,22 +5449,17 @@
 The scale filter forces the output display aspect ratio to be the same
 of the input, by changing the output sample aspect ratio.
 
-This filter accepts a list of named options in the form of
-@var{key}=@var{value} pairs separated by ":". If the key for the first
-two options is not specified, the assumed keys for the first two
-values are @code{w} and @code{h}. If the first option has no key and
-can be interpreted like a video size specification, it will be used
-to set the video size.
-
-A description of the accepted options follows.
+The filter accepts the following options:
 
 @table @option
 @item width, w
-Set the video width expression, default value is @code{iw}. See below
+Output video width.
+default value is @code{iw}. See below
 for the list of accepted constants.
 
 @item height, h
-Set the video heiht expression, default value is @code{ih}.
+Output video height.
+default value is @code{ih}.
 See below for the list of accepted constants.
 
 @item interl
@@ -4549,12 +5522,12 @@
 the next filter, the scale filter will convert the input to the
 requested format.
 
-If the value for @var{width} or @var{height} is 0, the respective input
+If the value for @var{w} or @var{h} is 0, the respective input
 size is used for the output.
 
-If the value for @var{width} or @var{height} is -1, the scale filter will
-use, for the respective output size, a value that maintains the aspect
-ratio of the input image.
+If the value for @var{w} or @var{h} is -1, the scale filter will use, for the
+respective output size, a value that maintains the aspect ratio of the input
+image.
 
 @subsection Examples
 
@@ -4562,7 +5535,7 @@
 @item
 Scale the input video to a size of 200x100:
 @example
-scale=200:100
+scale=w=200:h=100
 @end example
 
 This is equivalent to:
@@ -4589,7 +5562,7 @@
 @item
 Scale the input to 2x:
 @example
-scale=2*iw:2*ih
+scale=w=2*iw:h=2*ih
 @end example
 
 @item
@@ -4607,7 +5580,7 @@
 @item
 Scale the input to half size:
 @example
-scale=iw/2:ih/2
+scale=w=iw/2:h=ih/2
 @end example
 
 @item
@@ -4626,11 +5599,12 @@
 @item
 Increase the height, and set the width to 3/2 of the height:
 @example
-scale=3/2*oh:3/5*ih
+scale=w=3/2*oh:h=3/5*ih
 @end example
 
 @item
-Increase the size, but make the size a multiple of the chroma:
+Increase the size, but make the size a multiple of the chroma
+subsample values:
 @example
 scale="trunc(3/2*iw/hsub)*hsub:trunc(3/2*ih/vsub)*vsub"
 @end example
@@ -4639,10 +5613,20 @@
 Increase the width to a maximum of 500 pixels, keep the same input
 aspect ratio:
 @example
-scale='min(500\, iw*3/2):-1'
+scale=w='min(500\, iw*3/2):h=-1'
 @end example
 @end itemize
 
+@section separatefields
+
+The @code{separatefields} takes a frame-based video input and splits
+each frame into its components fields, producing a new half height clip
+with twice the frame rate and twice the frame count.
+
+This filter use field-dominance information in frame to decide which
+of each pair of fields to place first in the output.
+If it gets it wrong use @ref{setfield} filter before @code{separatefields} filter.
+
 @section setdar, setsar
 
 The @code{setdar} filter sets the Display Aspect Ratio for the filter
@@ -4671,52 +5655,54 @@
 filter may be changed by later filters in the filterchain, e.g. if
 another "setsar" or a "setdar" filter is applied.
 
-The @code{setdar} and @code{setsar} filters accept a string in the
-form @var{num}:@var{den} expressing an aspect ratio, or the following
-named options, expressed as a sequence of @var{key}=@var{value} pairs,
-separated by ":".
+The filters accept the following options:
 
 @table @option
-@item max
-Set the maximum integer value to use for expressing numerator and
-denominator when reducing the expressed aspect ratio to a rational.
-Default value is @code{100}.
-
-@item r, ratio:
+@item r, ratio, dar (@code{setdar} only), sar (@code{setsar} only)
 Set the aspect ratio used by the filter.
 
 The parameter can be a floating point number string, an expression, or
 a string of the form @var{num}:@var{den}, where @var{num} and
 @var{den} are the numerator and denominator of the aspect ratio. If
 the parameter is not specified, it is assumed the value "0".
-In case the form "@var{num}:@var{den}" the @code{:} character should
-be escaped.
+In case the form "@var{num}:@var{den}" is used, the @code{:} character
+should be escaped.
+
+@item max
+Set the maximum integer value to use for expressing numerator and
+denominator when reducing the expressed aspect ratio to a rational.
+Default value is @code{100}.
+
 @end table
 
-If the keys are omitted in the named options list, the specifed values
-are assumed to be @var{ratio} and @var{max} in that order.
+@subsection Examples
 
-For example to change the display aspect ratio to 16:9, specify:
+@itemize
+
+@item
+To change the display aspect ratio to 16:9, specify one of the following:
 @example
-setdar='16:9'
+setdar=dar=1.77777
+setdar=dar=16/9
+setdar=dar=1.77777
 @end example
 
-The example above is equivalent to:
-@example
-setdar=1.77777
-@end example
-
+@item
 To change the sample aspect ratio to 10:11, specify:
 @example
-setsar='10:11'
+setsar=sar=10/11
 @end example
 
+@item
 To set a display aspect ratio of 16:9, and specify a maximum integer value of
 1000 in the aspect ratio reduction, use the command:
 @example
-setdar=ratio='16:9':max=1000
+setdar=ratio=16/9:max=1000
 @end example
 
+@end itemize
+
+@anchor{setfield}
 @section setfield
 
 Force field for the output video frame.
@@ -4726,9 +5712,12 @@
 corresponding property, which affects how the frame is treated by
 following filters (e.g. @code{fieldorder} or @code{yadif}).
 
-This filter accepts a single option @option{mode}, which can be
-specified either by setting @code{mode=VALUE} or setting the value
-alone. Available values are:
+The filter accepts the following options:
+
+@table @option
+
+@item mode
+Available values are:
 
 @table @samp
 @item auto
@@ -4743,6 +5732,7 @@
 @item prog
 Mark the frame as progressive.
 @end table
+@end table
 
 @section showinfo
 
@@ -4807,30 +5797,270 @@
 
 Blur the input video without impacting the outlines.
 
-The filter accepts the following parameters:
-@var{luma_radius}:@var{luma_strength}:@var{luma_threshold}[:@var{chroma_radius}:@var{chroma_strength}:@var{chroma_threshold}]
+The filter accepts the following options:
 
-Parameters prefixed by @var{luma} indicate that they work on the
-luminance of the pixels whereas parameters prefixed by @var{chroma}
-refer to the chrominance of the pixels.
+@table @option
+@item luma_radius, lr
+Set the luma radius. The option value must be a float number in
+the range [0.1,5.0] that specifies the variance of the gaussian filter
+used to blur the image (slower if larger). Default value is 1.0.
 
-If the chroma parameters are not set, the luma parameters are used for
-either the luminance and the chrominance of the pixels.
+@item luma_strength, ls
+Set the luma strength. The option value must be a float number
+in the range [-1.0,1.0] that configures the blurring. A value included
+in [0.0,1.0] will blur the image whereas a value included in
+[-1.0,0.0] will sharpen the image. Default value is 1.0.
 
-@var{luma_radius} or @var{chroma_radius} must be a float number in the
-range [0.1,5.0] that specifies the variance of the gaussian filter
-used to blur the image (slower if larger).
+@item luma_threshold, lt
+Set the luma threshold used as a coefficient to determine
+whether a pixel should be blurred or not. The option value must be an
+integer in the range [-30,30]. A value of 0 will filter all the image,
+a value included in [0,30] will filter flat areas and a value included
+in [-30,0] will filter edges. Default value is 0.
 
-@var{luma_strength} or @var{chroma_strength} must be a float number in
-the range [-1.0,1.0] that configures the blurring. A value included in
-[0.0,1.0] will blur the image whereas a value included in [-1.0,0.0]
-will sharpen the image.
+@item chroma_radius, cr
+Set the chroma radius. The option value must be a float number in
+the range [0.1,5.0] that specifies the variance of the gaussian filter
+used to blur the image (slower if larger). Default value is 1.0.
 
-@var{luma_threshold} or @var{chroma_threshold} must be an integer in
-the range [-30,30] that is used as a coefficient to determine whether
-a pixel should be blurred or not. A value of 0 will filter all the
-image, a value included in [0,30] will filter flat areas and a value
-included in [-30,0] will filter edges.
+@item chroma_strength, cs
+Set the chroma strength. The option value must be a float number
+in the range [-1.0,1.0] that configures the blurring. A value included
+in [0.0,1.0] will blur the image whereas a value included in
+[-1.0,0.0] will sharpen the image. Default value is 1.0.
+
+@item chroma_threshold, ct
+Set the chroma threshold used as a coefficient to determine
+whether a pixel should be blurred or not. The option value must be an
+integer in the range [-30,30]. A value of 0 will filter all the image,
+a value included in [0,30] will filter flat areas and a value included
+in [-30,0] will filter edges. Default value is 0.
+@end table
+
+If a chroma option is not explicitly set, the corresponding luma value
+is set.
+
+@anchor{vidstabdetect}
+@section vidstabdetect
+
+Video stabilization/deshaking: pass 1 of 2, see @ref{vidstabtransform}
+for pass 2.
+
+Generates a file with relative transform information translation,
+rotation about subsequent frames.
+
+To enable compilation of this filter you need to configure FFmpeg with
+@code{--enable-libvidstab}.
+
+This filter accepts the following options:
+
+@table @option
+@item result
+path to the file used to write the transforms (default:@file{transforms.trf})
+
+@item shakiness
+how shaky is the video and how quick is the camera? (default: 5)
+@table @samp
+@item 1
+ little (fast)
+@item ...
+@item 10
+ very strong/quick (slow)
+@end table
+
+@item accuracy
+accuracy of detection process (>=shakiness) (default: 9)
+@table @samp
+@item 1
+ low (fast)
+@item 15
+ high (slow)
+@end table
+
+@item stepsize
+stepsize of search process, region around minimum is scanned with 1 pixel
+resolution (default: 6)
+
+@item mincontrast
+below this contrast a local measurement field is discarded (0-1) (default: 0.3)
+
+@item tripod
+virtual tripod mode: @code{tripod=framenum} if framenum>0 otherwise disabled.
+The motion of the frames is compared to a reference frame (framenum).
+The idea is to compensate all movements in a more-or-less static scene
+ and keep the camera view absolutely still.
+(default: 0 (disabled))
+
+@item show
+draw nothing (default); 1,2: show fields and transforms in the resulting frames
+
+@end table
+
+@subsection Examples
+
+@itemize
+@item
+use default values:
+@example
+vidstabdetect
+@end example
+
+@item
+strongly shaky movie and put the results in @code{mytransforms.trf}
+@example
+vidstabdetect=shakiness=10:accuracy=15:result="mytransforms.trf"
+@end example
+
+@item
+visualize some internals in the resulting video
+@example
+vidstabdetect=show=1
+@end example
+
+
+@item
+Typical call with visualization
+@example
+ffmpeg -i input -vf vidstabdetect=shakiness=5:show=1 dummy.avi
+@end example
+@end itemize
+
+
+@section stereo3d
+
+Convert between different stereoscopic image formats.
+
+The filters accept the following options:
+
+@table @option
+@item in
+Set stereoscopic image format of input.
+
+Available values for input image formats are:
+@table @samp
+@item sbsl
+side by side parallel (left eye left, right eye right)
+
+@item sbsr
+side by side crosseye (right eye left, left eye right)
+
+@item sbs2l
+side by side parallel with half width resolution
+(left eye left, right eye right)
+
+@item sbs2r
+side by side crosseye with half width resolution
+(right eye left, left eye right)
+
+@item abl
+above-below (left eye above, right eye below)
+
+@item abr
+above-below (right eye above, left eye below)
+
+@item ab2l
+above-below with half height resolution
+(left eye above, right eye below)
+
+@item ab2r
+above-below with half height resolution
+(right eye above, left eye below)
+
+Default value is @samp{sbsl}.
+@end table
+
+@item out
+Set stereoscopic image format of output.
+
+Available values for output image formats are all the input formats as well as:
+@table @samp
+@item arbg
+anaglyph red/blue gray
+(red filter on left eye, blue filter on right eye)
+
+@item argg
+anaglyph red/green gray
+(red filter on left eye, green filter on right eye)
+
+@item arcg
+anaglyph red/cyan gray
+(red filter on left eye, cyan filter on right eye)
+
+@item arch
+anaglyph red/cyan half colored
+(red filter on left eye, cyan filter on right eye)
+
+@item arcc
+anaglyph red/cyan color
+(red filter on left eye, cyan filter on right eye)
+
+@item arcd
+anaglyph red/cyan color optimized with the least squares projection of dubois
+(red filter on left eye, cyan filter on right eye)
+
+@item agmg
+anaglyph green/magenta gray
+(green filter on left eye, magenta filter on right eye)
+
+@item agmh
+anaglyph green/magenta half colored
+(green filter on left eye, magenta filter on right eye)
+
+@item agmc
+anaglyph green/magenta colored
+(green filter on left eye, magenta filter on right eye)
+
+@item agmd
+anaglyph green/magenta color optimized with the least squares projection of dubois
+(green filter on left eye, magenta filter on right eye)
+
+@item aybg
+anaglyph yellow/blue gray
+(yellow filter on left eye, blue filter on right eye)
+
+@item aybh
+anaglyph yellow/blue half colored
+(yellow filter on left eye, blue filter on right eye)
+
+@item aybc
+anaglyph yellow/blue colored
+(yellow filter on left eye, blue filter on right eye)
+
+@item aybd
+anaglyph yellow/blue color optimized with the least squares projection of dubois
+(yellow filter on left eye, blue filter on right eye)
+
+@item irl
+interleaved rows (left eye has top row, right eye starts on next row)
+
+@item irr
+interleaved rows (right eye has top row, left eye starts on next row)
+
+@item ml
+mono output (left eye only)
+
+@item mr
+mono output (right eye only)
+@end table
+
+Default value is @samp{arcd}.
+@end table
+
+@subsection Examples
+
+@itemize
+@item
+Convert input video from side by side parallel to anaglyph yellow/blue dubois:
+@example
+stereo3d=sbsl:aybd
+@end example
+
+@item
+Convert input video from above bellow (left eye above, right eye below) to side by side crosseye.
+@example
+stereo3d=abl:sbsr
+@end example
+@end itemize
 
 @anchor{subtitles}
 @section subtitles
@@ -4842,8 +6072,7 @@
 libavformat to convert the passed subtitles file to ASS (Advanced Substation
 Alpha) subtitles format.
 
-This filter accepts the following named options, expressed as a
-sequence of @var{key}=@var{value} pairs, separated by ":".
+The filter accepts the following options:
 
 @table @option
 @item filename, f
@@ -4873,29 +6102,6 @@
 subtitles=filename=sub.srt
 @end example
 
-@section split
-
-Split input video into several identical outputs.
-
-The filter accepts a single parameter which specifies the number of outputs. If
-unspecified, it defaults to 2.
-
-For example
-@example
-ffmpeg -i INPUT -filter_complex split=5 OUTPUT
-@end example
-will create 5 copies of the input video.
-
-For example:
-@example
-[in] split [splitout1][splitout2];
-[splitout1] crop=100:100:0:0    [cropout];
-[splitout2] pad=200:200:100:100 [padout];
-@end example
-
-will create two separate outputs from the same input, one cropped and
-one padded.
-
 @section super2xsai
 
 Scale the input by 2x and smooth using the Super2xSaI (Scale and
@@ -4906,32 +6112,81 @@
 @section swapuv
 Swap U & V plane.
 
+@section telecine
+
+Apply telecine process to the video.
+
+This filter accepts the following options:
+
+@table @option
+@item first_field
+@table @samp
+@item top, t
+top field first
+@item bottom, b
+bottom field first
+The default value is @code{top}.
+@end table
+
+@item pattern
+A string of numbers representing the pulldown pattern you wish to apply.
+The default value is @code{23}.
+@end table
+
+@example
+Some typical patterns:
+
+NTSC output (30i):
+27.5p: 32222
+24p: 23 (classic)
+24p: 2332 (preferred)
+20p: 33
+18p: 334
+16p: 3444
+
+PAL output (25i):
+27.5p: 12222
+24p: 222222222223 ("Euro pulldown")
+16.67p: 33
+16p: 33333334
+@end example
+
 @section thumbnail
 Select the most representative frame in a given sequence of consecutive frames.
 
-It accepts as argument the frames batch size to analyze (default @var{N}=100);
-in a set of @var{N} frames, the filter will pick one of them, and then handle
-the next batch of @var{N} frames until the end.
+The filter accepts the following options:
 
-Since the filter keeps track of the whole frames sequence, a bigger @var{N}
+@table @option
+@item n
+Set the frames batch size to analyze; in a set of @var{n} frames, the filter
+will pick one of them, and then handle the next batch of @var{n} frames until
+the end. Default is @code{100}.
+@end table
+
+Since the filter keeps track of the whole frames sequence, a bigger @var{n}
 value will result in a higher memory usage, so a high value is not recommended.
 
-The following example extract one picture each 50 frames:
+@subsection Examples
+
+@itemize
+@item
+Extract one picture each 50 frames:
 @example
 thumbnail=50
 @end example
 
+@item
 Complete example of a thumbnail creation with @command{ffmpeg}:
 @example
 ffmpeg -i in.avi -vf thumbnail,scale=300:200 -frames:v 1 out.png
 @end example
+@end itemize
 
 @section tile
 
 Tile several successive frames together.
 
-It accepts a list of options in the form of @var{key}=@var{value} pairs
-separated by ":". A description of the accepted options follows.
+The filter accepts the following options:
 
 @table @option
 
@@ -4939,6 +6194,11 @@
 Set the grid size (i.e. the number of lines and columns) in the form
 "@var{w}x@var{h}".
 
+@item nb_frames
+Set the maximum number of frames to render in the given area. It must be less
+than or equal to @var{w}x@var{h}. The default value is @code{0}, meaning all
+the area will be used.
+
 @item margin
 Set the outer border margin in pixels.
 
@@ -4947,19 +6207,13 @@
 more advanced padding options (such as having different values for the edges),
 refer to the pad video filter.
 
-@item nb_frames
-Set the maximum number of frames to render in the given area. It must be less
-than or equal to @var{w}x@var{h}. The default value is @code{0}, meaning all
-the area will be used.
-
 @end table
 
-Alternatively, the options can be specified as a flat string:
+@subsection Examples
 
-@var{layout}[:@var{nb_frames}[:@var{margin}[:@var{padding}]]]
-
-For example, produce 8x8 PNG tiles of all keyframes (@option{-skip_frame
-nokey}) in a movie:
+@itemize
+@item
+Produce 8x8 PNG tiles of all keyframes (@option{-skip_frame nokey}) in a movie:
 @example
 ffmpeg -skip_frame nokey -i file.avi -vf 'scale=128:72,tile=8x8' -an -vsync 0 keyframes%03d.png
 @end example
@@ -4967,12 +6221,14 @@
 duplicating each output frame to accomodate the originally detected frame
 rate.
 
-Another example to display @code{5} pictures in an area of @code{3x2} frames,
+@item
+Display @code{5} pictures in an area of @code{3x2} frames,
 with @code{7} pixels between them, and @code{2} pixels of initial margin, using
 mixed flat and named options:
 @example
 tile=3x2:nb_frames=5:padding=7:margin=2
 @end example
+@end itemize
 
 @section tinterlace
 
@@ -4981,13 +6237,7 @@
 Frames are counted starting from 1, so the first input frame is
 considered odd.
 
-This filter accepts options in the form of @var{key}=@var{value} pairs
-separated by ":".
-Alternatively, the @var{mode} option can be specified as a value alone,
-optionally followed by a ":" and further ":" separated @var{key}=@var{value}
-pairs.
-
-A description of the accepted options follows.
+The filter accepts the following options:
 
 @table @option
 
@@ -5000,27 +6250,27 @@
 @table @samp
 @item merge, 0
 Move odd frames into the upper field, even into the lower field,
-generating a double height frame at half framerate.
+generating a double height frame at half frame rate.
 
 @item drop_odd, 1
 Only output even frames, odd frames are dropped, generating a frame with
-unchanged height at half framerate.
+unchanged height at half frame rate.
 
 @item drop_even, 2
 Only output odd frames, even frames are dropped, generating a frame with
-unchanged height at half framerate.
+unchanged height at half frame rate.
 
 @item pad, 3
 Expand each frame to full height, but pad alternate lines with black,
-generating a frame with double height at the same input framerate.
+generating a frame with double height at the same input frame rate.
 
 @item interleave_top, 4
 Interleave the upper field from odd frames with the lower field from
-even frames, generating a frame with unchanged height at half framerate.
+even frames, generating a frame with unchanged height at half frame rate.
 
 @item interleave_bottom, 5
 Interleave the lower field from odd frames with the upper field from
-even frames, generating a frame with unchanged height at half framerate.
+even frames, generating a frame with unchanged height at half frame rate.
 
 @item interlacex2, 6
 Double frame rate with unchanged height. Frames are inserted each
@@ -5058,17 +6308,15 @@
 
 Transpose rows with columns in the input video and optionally flip it.
 
-The filter accepts parameters as a list of @var{key}=@var{value}
-pairs, separated by ':'. If the key of the first options is omitted,
-the arguments are interpreted according to the syntax
-@var{dir}:@var{passthrough}.
+This filter accepts the following options:
 
 @table @option
+
 @item dir
-Specify the transposition direction. Can assume the following values:
+The direction of the transpose.
 
 @table @samp
-@item 0, 4
+@item 0, 4, cclock_flip
 Rotate by 90 degrees counterclockwise and vertically flip (default), that is:
 @example
 L.R     L.l
@@ -5076,7 +6324,7 @@
 l.r     R.r
 @end example
 
-@item 1, 5
+@item 1, 5, clock
 Rotate by 90 degrees clockwise, that is:
 @example
 L.R     l.L
@@ -5084,7 +6332,7 @@
 l.r     r.R
 @end example
 
-@item 2, 6
+@item 2, 6, cclock
 Rotate by 90 degrees counterclockwise, that is:
 @example
 L.R     R.r
@@ -5092,7 +6340,7 @@
 l.r     L.l
 @end example
 
-@item 3, 7
+@item 3, 7, clock_flip
 Rotate by 90 degrees clockwise and vertically flip, that is:
 @example
 L.R     r.R
@@ -5135,44 +6383,54 @@
 
 Sharpen or blur the input video.
 
-This filter accepts parameters as a list of @var{key}=@var{value} pairs,
-separated by ":".
-
-If the key of the first options is omitted, the arguments are
-interpreted according to the syntax:
-@var{luma_msize_x}:@var{luma_msize_y}:@var{luma_amount}:@var{chroma_msize_x}:@var{chroma_msize_y}:@var{chroma_amount}
-
-A description of the accepted options follows.
+It accepts the following parameters:
 
 @table @option
 @item luma_msize_x, lx
-@item chroma_msize_x, cx
-Set the luma/chroma matrix horizontal size. It must be an odd integer
-between 3 and 63, default value is 5.
+Set the luma matrix horizontal size. It must be an odd integer between
+3 and 63, default value is 5.
 
 @item luma_msize_y, ly
-@item chroma_msize_y, cy
-Set the luma/chroma matrix vertical size. It must be an odd integer
-between 3 and 63, default value is 5.
+Set the luma matrix vertical size. It must be an odd integer between 3
+and 63, default value is 5.
 
 @item luma_amount, la
-@item chroma_amount, ca
-Set the luma/chroma effect strength. It can be a float number,
-reasonable values lay between -1.5 and 1.5.
+Set the luma effect strength. It can be a float number, reasonable
+values lay between -1.5 and 1.5.
 
 Negative values will blur the input video, while positive values will
 sharpen it, a value of zero will disable the effect.
 
-Default value is 1.0 for @option{luma_amount}, 0.0 for
-@option{chroma_amount}.
+Default value is 1.0.
+
+@item chroma_msize_x, cx
+Set the chroma matrix horizontal size. It must be an odd integer
+between 3 and 63, default value is 5.
+
+@item chroma_msize_y, cy
+Set the chroma matrix vertical size. It must be an odd integer
+between 3 and 63, default value is 5.
+
+@item chroma_amount, ca
+Set the chroma effect strength. It can be a float number, reasonable
+values lay between -1.5 and 1.5.
+
+Negative values will blur the input video, while positive values will
+sharpen it, a value of zero will disable the effect.
+
+Default value is 0.0.
 @end table
 
-Some examples follow:
+All parameters are optional and default to the equivalent of the
+string '5:5:1.0:5:5:0.0'.
+
+@subsection Examples
+
 @itemize
 @item
 Apply strong luma sharpen effect:
 @example
-unsharp=7:7:2.5
+unsharp=luma_msize_x=7:luma_msize_y=7:luma_amount=2.5
 @end example
 
 @item
@@ -5186,26 +6444,24 @@
 
 Flip the input video vertically.
 
+For example, to vertically flip a video with @command{ffmpeg}:
 @example
 ffmpeg -i in.avi -vf "vflip" out.avi
 @end example
 
+@anchor{yadif}
 @section yadif
 
 Deinterlace the input video ("yadif" means "yet another deinterlacing
 filter").
 
-The filter accepts parameters as a list of @var{key}=@var{value}
-pairs, separated by ":". If the key of the first options is omitted,
-the arguments are interpreted according to syntax
-@var{mode}:@var{parity}:@var{deint}.
+This filter accepts the following options:
 
-The description of the accepted parameters follows.
 
 @table @option
+
 @item mode
-Specify the interlacing mode to adopt. Accept one of the following
-values:
+The interlacing mode to adopt, accepts one of the following values:
 
 @table @option
 @item 0, send_frame
@@ -5221,8 +6477,8 @@
 Default value is @code{send_frame}.
 
 @item parity
-Specify the picture field parity assumed for the input interlaced
-video. Accept one of the following values:
+The picture field parity assumed for the input interlaced video, accepts one of
+the following values:
 
 @table @option
 @item 0, tff
@@ -5265,14 +6521,19 @@
 This source is mainly intended for a programmatic use, in particular
 through the interface defined in @file{libavfilter/vsrc_buffer.h}.
 
-It accepts a list of options in the form of @var{key}=@var{value} pairs
-separated by ":". A description of the accepted options follows.
+This source accepts the following options:
 
 @table @option
 
 @item video_size
 Specify the size (width and height) of the buffered video frames.
 
+@item width
+Input video width.
+
+@item height
+Input video height.
+
 @item pix_fmt
 A string representing the pixel format of the buffered video frames.
 It may be a number corresponding to a pixel format, or a pixel format
@@ -5281,10 +6542,10 @@
 @item time_base
 Specify the timebase assumed by the timestamps of the buffered frames.
 
-@item time_base
+@item frame_rate
 Specify the frame rate expected for the video stream.
 
-@item pixel_aspect
+@item pixel_aspect, sar
 Specify the sample aspect ratio assumed by the video frames.
 
 @item sws_param
@@ -5295,7 +6556,7 @@
 
 For example:
 @example
-buffer=size=320x240:pix_fmt=yuv410p:time_base=1/24:pixel_aspect=1/1
+buffer=width=320:height=240:pix_fmt=yuv410p:time_base=1/24:sar=1
 @end example
 
 will instruct the source to accept video frames with size 320x240 and
@@ -5325,9 +6586,7 @@
 the cellular automaton next generation. The behavior when the whole
 frame is filled is defined by the @option{scroll} option.
 
-This source accepts a list of options in the form of
-@var{key}=@var{value} pairs separated by ":". A description of the
-accepted options follows.
+This source accepts the following options:
 
 @table @option
 @item filename, f
@@ -5433,9 +6692,7 @@
 Generate a Mandelbrot set fractal, and progressively zoom towards the
 point specified with @var{start_x} and @var{start_y}.
 
-This source accepts a list of options in the form of
-@var{key}=@var{value} pairs separated by ":". A description of the
-accepted options follows.
+This source accepts the following options:
 
 @table @option
 
@@ -5508,8 +6765,7 @@
 The size of the generated video is fixed, and is 256x256.
 This source is useful in particular for testing encoding features.
 
-This source accepts an optional sequence of @var{key}=@var{value} pairs,
-separated by ":". The description of the accepted options follows.
+This source accepts the following options:
 
 @table @option
 
@@ -5565,23 +6821,32 @@
 To enable compilation of this filter you need to install the frei0r
 header and configure FFmpeg with @code{--enable-frei0r}.
 
-The source supports the syntax:
-@example
-@var{size}:@var{rate}:@var{src_name}[@{=|:@}@var{param1}:@var{param2}:...:@var{paramN}]
-@end example
+This source accepts the following options:
 
-@var{size} is the size of the video to generate, may be a string of the
-form @var{width}x@var{height} or a frame size abbreviation.
-@var{rate} is the rate of the video to generate, may be a string of
-the form @var{num}/@var{den} or a frame rate abbreviation.
-@var{src_name} is the name to the frei0r source to load. For more
-information regarding frei0r and how to set the parameters read the
-section @ref{frei0r} in the description of the video filters.
+@table @option
+
+@item size
+The size of the video to generate, may be a string of the form
+@var{width}x@var{height} or a frame size abbreviation.
+
+@item framerate
+Framerate of the generated video, may be a string of the form
+@var{num}/@var{den} or a frame rate abbreviation.
+
+@item filter_name
+The name to the frei0r source to load. For more information regarding frei0r and
+how to set the parameters read the section @ref{frei0r} in the description of
+the video filters.
+
+@item filter_params
+A '|'-separated list of parameters to pass to the frei0r source.
+
+@end table
 
 For example, to generate a frei0r partik0l source with size 200x200
 and frame rate 10 which is overlayed on the overlay filter main input:
 @example
-frei0r_src=200x200:10:partik0l=1234 [overlay]; [in][overlay] overlay
+frei0r_src=size=200x200:framerate=10:filter_name=partik0l:filter_params=1234 [overlay]; [in][overlay] overlay
 @end example
 
 @section life
@@ -5600,9 +6865,7 @@
 cell stay alive or born. The @option{rule} option allows to specify
 the rule to adopt.
 
-This source accepts a list of options in the form of
-@var{key}=@var{value} pairs separated by ":". A description of the
-accepted options follows.
+This source accepts the following options:
 
 @table @option
 @item filename, f
@@ -5711,7 +6974,7 @@
 @end example
 @end itemize
 
-@section color, nullsrc, rgbtestsrc, smptebars, testsrc
+@section color, nullsrc, rgbtestsrc, smptebars, smptehdbars, testsrc
 
 The @code{color} source provides an uniformly colored input.
 
@@ -5726,12 +6989,14 @@
 The @code{smptebars} source generates a color bars pattern, based on
 the SMPTE Engineering Guideline EG 1-1990.
 
+The @code{smptehdbars} source generates a color bars pattern, based on
+the SMPTE RP 219-2002.
+
 The @code{testsrc} source generates a test video pattern, showing a
 color pattern, a scrolling gradient and a timestamp. This is mainly
 intended for testing purposes.
 
-These sources accept an optional sequence of @var{key}=@var{value} pairs,
-separated by ":". The description of the accepted options follows.
+The sources accept the following options:
 
 @table @option
 
@@ -5811,12 +7076,12 @@
 graph.
 
 This sink is mainly intended for a programmatic use, in particular
-through the interface defined in @file{libavfilter/buffersink.h}.
+through the interface defined in @file{libavfilter/buffersink.h}
+or the options system.
 
-It does not require a string parameter in input, but you need to
-specify a pointer to a list of supported pixel formats terminated by
--1 in the opaque parameter provided to @code{avfilter_init_filter}
-when initializing this sink.
+It accepts a pointer to an AVBufferSinkContext structure, which
+defines the incoming buffers' formats, to be passed as the opaque
+parameter to @code{avfilter_init_filter} for initialization.
 
 @section nullsink
 
@@ -5831,17 +7096,270 @@
 
 Below is a description of the currently available multimedia filters.
 
-@section aselect, select
+@section concat
+
+Concatenate audio and video streams, joining them together one after the
+other.
+
+The filter works on segments of synchronized video and audio streams. All
+segments must have the same number of streams of each type, and that will
+also be the number of streams at output.
+
+The filter accepts the following options:
+
+@table @option
+
+@item n
+Set the number of segments. Default is 2.
+
+@item v
+Set the number of output video streams, that is also the number of video
+streams in each segment. Default is 1.
+
+@item a
+Set the number of output audio streams, that is also the number of video
+streams in each segment. Default is 0.
+
+@item unsafe
+Activate unsafe mode: do not fail if segments have a different format.
+
+@end table
+
+The filter has @var{v}+@var{a} outputs: first @var{v} video outputs, then
+@var{a} audio outputs.
+
+There are @var{n}x(@var{v}+@var{a}) inputs: first the inputs for the first
+segment, in the same order as the outputs, then the inputs for the second
+segment, etc.
+
+Related streams do not always have exactly the same duration, for various
+reasons including codec frame size or sloppy authoring. For that reason,
+related synchronized streams (e.g. a video and its audio track) should be
+concatenated at once. The concat filter will use the duration of the longest
+stream in each segment (except the last one), and if necessary pad shorter
+audio streams with silence.
+
+For this filter to work correctly, all segments must start at timestamp 0.
+
+All corresponding streams must have the same parameters in all segments; the
+filtering system will automatically select a common pixel format for video
+streams, and a common sample format, sample rate and channel layout for
+audio streams, but other settings, such as resolution, must be converted
+explicitly by the user.
+
+Different frame rates are acceptable but will result in variable frame rate
+at output; be sure to configure the output file to handle it.
+
+@subsection Examples
+
+@itemize
+@item
+Concatenate an opening, an episode and an ending, all in bilingual version
+(video in stream 0, audio in streams 1 and 2):
+@example
+ffmpeg -i opening.mkv -i episode.mkv -i ending.mkv -filter_complex \
+  '[0:0] [0:1] [0:2] [1:0] [1:1] [1:2] [2:0] [2:1] [2:2]
+   concat=n=3:v=1:a=2 [v] [a1] [a2]' \
+  -map '[v]' -map '[a1]' -map '[a2]' output.mkv
+@end example
+
+@item
+Concatenate two parts, handling audio and video separately, using the
+(a)movie sources, and adjusting the resolution:
+@example
+movie=part1.mp4, scale=512:288 [v1] ; amovie=part1.mp4 [a1] ;
+movie=part2.mp4, scale=512:288 [v2] ; amovie=part2.mp4 [a2] ;
+[v1] [v2] concat [outv] ; [a1] [a2] concat=v=0:a=1 [outa]
+@end example
+Note that a desync will happen at the stitch if the audio and video streams
+do not have exactly the same duration in the first file.
+
+@end itemize
+
+@section ebur128
+
+EBU R128 scanner filter. This filter takes an audio stream as input and outputs
+it unchanged. By default, it logs a message at a frequency of 10Hz with the
+Momentary loudness (identified by @code{M}), Short-term loudness (@code{S}),
+Integrated loudness (@code{I}) and Loudness Range (@code{LRA}).
+
+The filter also has a video output (see the @var{video} option) with a real
+time graph to observe the loudness evolution. The graphic contains the logged
+message mentioned above, so it is not printed anymore when this option is set,
+unless the verbose logging is set. The main graphing area contains the
+short-term loudness (3 seconds of analysis), and the gauge on the right is for
+the momentary loudness (400 milliseconds).
+
+More information about the Loudness Recommendation EBU R128 on
+@url{http://tech.ebu.ch/loudness}.
+
+The filter accepts the following options:
+
+@table @option
+
+@item video
+Activate the video output. The audio stream is passed unchanged whether this
+option is set or no. The video stream will be the first output stream if
+activated. Default is @code{0}.
+
+@item size
+Set the video size. This option is for video only. Default and minimum
+resolution is @code{640x480}.
+
+@item meter
+Set the EBU scale meter. Default is @code{9}. Common values are @code{9} and
+@code{18}, respectively for EBU scale meter +9 and EBU scale meter +18. Any
+other integer value between this range is allowed.
+
+@item metadata
+Set metadata injection. If set to @code{1}, the audio input will be segmented
+into 100ms output frames, each of them containing various loudness information
+in metadata.  All the metadata keys are prefixed with @code{lavfi.r128.}.
+
+Default is @code{0}.
+
+@item framelog
+Force the frame logging level.
+
+Available values are:
+@table @samp
+@item info
+information logging level
+@item verbose
+verbose logging level
+@end table
+
+By default, the logging level is set to @var{info}. If the @option{video} or
+the @option{metadata} options are set, it switches to @var{verbose}.
+@end table
+
+@subsection Examples
+
+@itemize
+@item
+Real-time graph using @command{ffplay}, with a EBU scale meter +18:
+@example
+ffplay -f lavfi -i "amovie=input.mp3,ebur128=video=1:meter=18 [out0][out1]"
+@end example
+
+@item
+Run an analysis with @command{ffmpeg}:
+@example
+ffmpeg -nostats -i input.mp3 -filter_complex ebur128 -f null -
+@end example
+@end itemize
+
+@section interleave, ainterleave
+
+Temporally interleave frames from several inputs.
+
+@code{interleave} works with video inputs, @code{ainterleave} with audio.
+
+These filters read frames from several inputs and send the oldest
+queued frame to the output.
+
+Input streams must have a well defined, monotonically increasing frame
+timestamp values.
+
+In order to submit one frame to output, these filters need to enqueue
+at least one frame for each input, so they cannot work in case one
+input is not yet terminated and will not receive incoming frames.
+
+For example consider the case when one input is a @code{select} filter
+which always drop input frames. The @code{interleave} filter will keep
+reading from that input, but it will never be able to send new frames
+to output until the input will send an end-of-stream signal.
+
+Also, depending on inputs synchronization, the filters will drop
+frames in case one input receives more frames than the other ones, and
+the queue is already filled.
+
+These filters accept the following options:
+
+@table @option
+@item nb_inputs, n
+Set the number of different inputs, it is 2 by default.
+@end table
+
+@subsection Examples
+
+@itemize
+@item
+Interleave frames belonging to different streams using @command{ffmpeg}:
+@example
+ffmpeg -i bambi.avi -i pr0n.mkv -filter_complex "[0:v][1:v] interleave" out.avi
+@end example
+
+@item
+Add flickering blur effect:
+@example
+select='if(gt(random(0), 0.2), 1, 2)':n=2 [tmp], boxblur=2:2, [tmp] interleave
+@end example
+@end itemize
+
+@section perms, aperms
+
+Set read/write permissions for the output frames.
+
+These filters are mainly aimed at developers to test direct path in the
+following filter in the filtergraph.
+
+The filters accept the following options:
+
+@table @option
+@item mode
+Select the permissions mode.
+
+It accepts the following values:
+@table @samp
+@item none
+Do nothing. This is the default.
+@item ro
+Set all the output frames read-only.
+@item rw
+Set all the output frames directly writable.
+@item toggle
+Make the frame read-only if writable, and writable if read-only.
+@item random
+Set each output frame read-only or writable randomly.
+@end table
+
+@item seed
+Set the seed for the @var{random} mode, must be an integer included between
+@code{0} and @code{UINT32_MAX}. If not specified, or if explicitly set to
+@code{-1}, the filter will try to use a good random seed on a best effort
+basis.
+@end table
+
+Note: in case of auto-inserted filter between the permission filter and the
+following one, the permission might not be received as expected in that
+following filter. Inserting a @ref{format} or @ref{aformat} filter before the
+perms/aperms filter can avoid this problem.
+
+@section select, aselect
+
 Select frames to pass in output.
 
-These filters accept a single option @option{expr} or @option{e}
-specifying the select expression, which can be specified either by
-specyfing @code{expr=VALUE} or specifying the expression
-alone.
+This filter accepts the following options:
 
-The select expression is evaluated for each input frame. If the
-evaluation result is a non-zero value, the frame is selected and
-passed to the output, otherwise it is discarded.
+@table @option
+
+@item expr, e
+Set expression, which is evaluated for each input frame.
+
+If the expression is evaluated to zero, the frame is discarded.
+
+If the evaluation result is negative or NaN, the frame is sent to the
+first output; otherwise it is sent to the output with index
+@code{ceil(val)-1}, assuming that the input index starts from 0.
+
+For example a value of @code{1.2} corresponds to the output with index
+@code{ceil(1.2)-1 = 2-1 = 1}, that is the second output.
+
+@item outputs, n
+Set the number of outputs. The output to which to send the selected
+frame is based on the result of the evaluation. Default value is 1.
+@end table
 
 The expression can contain the following constants:
 
@@ -5994,17 +7512,23 @@
 
 Comparing @var{scene} against a value between 0.3 and 0.5 is generally a sane
 choice.
+
+@item
+Send even and odd frames to separate outputs, and compose them:
+@example
+select=n=2:e='mod(n, 2)+1' [odd][even]; [odd] pad=h=2*ih [tmp]; [tmp][even] overlay=y=h
+@end example
 @end itemize
 
-@section asendcmd, sendcmd
+@section sendcmd, asendcmd
 
 Send commands to filters in the filtergraph.
 
 These filters read commands to be sent to other filters in the
 filtergraph.
 
-@code{asendcmd} must be inserted between two audio filters,
-@code{sendcmd} must be inserted between two video filters, but apart
+@code{sendcmd} must be inserted between two video filters,
+@code{asendcmd} must be inserted between two audio filters, but apart
 from that they act the same way.
 
 The specification of commands can be provided in the filter arguments
@@ -6111,13 +7635,13 @@
          [leave] drawtext reinit 'fontfile=FreeSerif.ttf:text=';
 
 # desaturate the image in the interval 15-20
-15.0-20.0 [enter] hue reinit s=0,
+15.0-20.0 [enter] hue s 0,
           [enter] drawtext reinit 'fontfile=FreeSerif.ttf:text=nocolor',
-          [leave] hue reinit s=1,
+          [leave] hue s 1,
           [leave] drawtext reinit 'fontfile=FreeSerif.ttf:text=color';
 
 # apply an exponential saturation fade-out effect, starting from time 25
-25 [enter] hue s=exp(t-25)
+25 [enter] hue s exp(25-t)
 @end example
 
 A filtergraph allowing to read and process the above command list
@@ -6128,14 +7652,23 @@
 @end itemize
 
 @anchor{setpts}
-@section asetpts, setpts
+@section setpts, asetpts
 
 Change the PTS (presentation timestamp) of the input frames.
 
-@code{asetpts} works on audio frames, @code{setpts} on video frames.
+@code{setpts} works on video frames, @code{asetpts} on audio frames.
 
-Accept in input an expression evaluated through the eval API, which
-can contain the following constants:
+This filter accepts the following options:
+
+@table @option
+
+@item expr
+The expression which is evaluated for each frame to construct its timestamp.
+
+@end table
+
+The expression is evaluated through the eval API and can contain the following
+constants:
 
 @table @option
 @item FRAME_RATE
@@ -6242,64 +7775,24 @@
 @end example
 @end itemize
 
-@section ebur128
-
-EBU R128 scanner filter. This filter takes an audio stream as input and outputs
-it unchanged. By default, it logs a message at a frequency of 10Hz with the
-Momentary loudness (identified by @code{M}), Short-term loudness (@code{S}),
-Integrated loudness (@code{I}) and Loudness Range (@code{LRA}).
-
-The filter also has a video output (see the @var{video} option) with a real
-time graph to observe the loudness evolution. The graphic contains the logged
-message mentioned above, so it is not printed anymore when this option is set,
-unless the verbose logging is set. The main graphing area contains the
-short-term loudness (3 seconds of analysis), and the gauge on the right is for
-the momentary loudness (400 milliseconds).
-
-More information about the Loudness Recommendation EBU R128 on
-@url{http://tech.ebu.ch/loudness}.
-
-The filter accepts the following named parameters:
-
-@table @option
-
-@item video
-Activate the video output. The audio stream is passed unchanged whether this
-option is set or no. The video stream will be the first output stream if
-activated. Default is @code{0}.
-
-@item size
-Set the video size. This option is for video only. Default and minimum
-resolution is @code{640x480}.
-
-@item meter
-Set the EBU scale meter. Default is @code{9}. Common values are @code{9} and
-@code{18}, respectively for EBU scale meter +9 and EBU scale meter +18. Any
-other integer value between this range is allowed.
-
-@end table
-
-Example of real-time graph using @command{ffplay}, with a EBU scale meter +18:
-@example
-ffplay -f lavfi -i "amovie=input.mp3,ebur128=video=1:meter=18 [out0][out1]"
-@end example
-
-Run an analysis with @command{ffmpeg}:
-@example
-ffmpeg -nostats -i input.mp3 -filter_complex ebur128 -f null -
-@end example
-
 @section settb, asettb
 
 Set the timebase to use for the output frames timestamps.
 It is mainly useful for testing timebase configuration.
 
-It accepts in input an arithmetic expression representing a rational.
-The expression can contain the constants "AVTB" (the
-default timebase), "intb" (the input timebase) and "sr" (the sample rate,
-audio only).
+This filter accepts the following options:
 
-The default value for the input is "intb".
+@table @option
+
+@item expr, tb
+The expression which is evaluated into the output timebase.
+
+@end table
+
+The value for @option{tb} is an arithmetic expression representing a
+rational. The expression can contain the constants "AVTB" (the default
+timebase), "intb" (the input timebase) and "sr" (the sample rate,
+audio only). Default value is "intb".
 
 @subsection Examples
 
@@ -6307,13 +7800,13 @@
 @item
 Set the timebase to 1/25:
 @example
-settb=1/25
+settb=expr=1/25
 @end example
 
 @item
 Set the timebase to 1/10:
 @example
-settb=0.1
+settb=expr=0.1
 @end example
 
 @item
@@ -6335,90 +7828,13 @@
 @end example
 @end itemize
 
-@section concat
-
-Concatenate audio and video streams, joining them together one after the
-other.
-
-The filter works on segments of synchronized video and audio streams. All
-segments must have the same number of streams of each type, and that will
-also be the number of streams at output.
-
-The filter accepts the following named parameters:
-@table @option
-
-@item n
-Set the number of segments. Default is 2.
-
-@item v
-Set the number of output video streams, that is also the number of video
-streams in each segment. Default is 1.
-
-@item a
-Set the number of output audio streams, that is also the number of video
-streams in each segment. Default is 0.
-
-@item unsafe
-Activate unsafe mode: do not fail if segments have a different format.
-
-@end table
-
-The filter has @var{v}+@var{a} outputs: first @var{v} video outputs, then
-@var{a} audio outputs.
-
-There are @var{n}x(@var{v}+@var{a}) inputs: first the inputs for the first
-segment, in the same order as the outputs, then the inputs for the second
-segment, etc.
-
-Related streams do not always have exactly the same duration, for various
-reasons including codec frame size or sloppy authoring. For that reason,
-related synchronized streams (e.g. a video and its audio track) should be
-concatenated at once. The concat filter will use the duration of the longest
-stream in each segment (except the last one), and if necessary pad shorter
-audio streams with silence.
-
-For this filter to work correctly, all segments must start at timestamp 0.
-
-All corresponding streams must have the same parameters in all segments; the
-filtering system will automatically select a common pixel format for video
-streams, and a common sample format, sample rate and channel layout for
-audio streams, but other settings, such as resolution, must be converted
-explicitly by the user.
-
-Different frame rates are acceptable but will result in variable frame rate
-at output; be sure to configure the output file to handle it.
-
-Examples:
-@itemize
-@item
-Concatenate an opening, an episode and an ending, all in bilingual version
-(video in stream 0, audio in streams 1 and 2):
-@example
-ffmpeg -i opening.mkv -i episode.mkv -i ending.mkv -filter_complex \
-  '[0:0] [0:1] [0:2] [1:0] [1:1] [1:2] [2:0] [2:1] [2:2]
-   concat=n=3:v=1:a=2 [v] [a1] [a2]' \
-  -map '[v]' -map '[a1]' -map '[a2]' output.mkv
-@end example
-
-@item
-Concatenate two parts, handling audio and video separately, using the
-(a)movie sources, and adjusting the resolution:
-@example
-movie=part1.mp4, scale=512:288 [v1] ; amovie=part1.mp4 [a1] ;
-movie=part2.mp4, scale=512:288 [v2] ; amovie=part2.mp4 [a2] ;
-[v1] [v2] concat [outv] ; [a1] [a2] concat=v=0:a=1 [outa]
-@end example
-Note that a desync will happen at the stitch if the audio and video streams
-do not have exactly the same duration in the first file.
-
-@end itemize
-
 @section showspectrum
 
 Convert input audio to a video output, representing the audio frequency
 spectrum.
 
-The filter accepts the following named parameters:
+The filter accepts the following options:
+
 @table @option
 @item size, s
 Specify the video size for the output. Default value is @code{640x512}.
@@ -6480,12 +7896,33 @@
 The usage is very similar to the showwaves filter; see the examples in that
 section.
 
+@subsection Examples
+
+@itemize
+@item
+Large window with logarithmic color scaling:
+@example
+showspectrum=s=1280x480:scale=log
+@end example
+
+@item
+Complete example for a colored and sliding spectrum per channel using @command{ffplay}:
+@example
+ffplay -f lavfi 'amovie=input.mp3, asplit [a][out1];
+             [a] showspectrum=mode=separate:color=intensity:slide=1:scale=cbrt [out0]'
+@end example
+@end itemize
+
 @section showwaves
 
 Convert input audio to a video output, representing the samples waves.
 
-The filter accepts the following named parameters:
+The filter accepts the following options:
+
 @table @option
+@item size, s
+Specify the video size for the output. Default value is "600x240".
+
 @item mode
 Set display mode.
 
@@ -6510,11 +7947,10 @@
 Set the (approximate) output frame rate. This is done by setting the
 option @var{n}. Default value is "25".
 
-@item size, s
-Specify the video size for the output. Default value is "600x240".
 @end table
 
-Some examples follow.
+@subsection Examples
+
 @itemize
 @item
 Output the input file audio and the corresponding video representation
@@ -6525,12 +7961,53 @@
 
 @item
 Create a synthetic signal and show it with showwaves, forcing a
-framerate of 30 frames per second:
+frame rate of 30 frames per second:
 @example
 aevalsrc=sin(1*2*PI*t)*sin(880*2*PI*t):cos(2*PI*200*t),asplit[out0],showwaves=r=30[out1]
 @end example
 @end itemize
 
+@section split, asplit
+
+Split input into several identical outputs.
+
+@code{asplit} works with audio input, @code{split} with video.
+
+The filter accepts a single parameter which specifies the number of outputs. If
+unspecified, it defaults to 2.
+
+@subsection Examples
+
+@itemize
+@item
+Create two separate outputs from the same input:
+@example
+[in] split [out0][out1]
+@end example
+
+@item
+To create 3 or more outputs, you need to specify the number of
+outputs, like in:
+@example
+[in] asplit=3 [out0][out1][out2]
+@end example
+
+@item
+Create two separate outputs from the same input, one cropped and
+one padded:
+@example
+[in] split [splitout1][splitout2];
+[splitout1] crop=100:100:0:0    [cropout];
+[splitout2] pad=200:200:100:100 [padout];
+@end example
+
+@item
+Create 5 copies of the input audio with @command{ffmpeg}:
+@example
+ffmpeg -i INPUT -filter_complex asplit=5 OUTPUT
+@end example
+@end itemize
+
 @c man end MULTIMEDIA FILTERS
 
 @chapter Multimedia Sources
@@ -6548,15 +8025,12 @@
 
 Read audio and/or video stream(s) from a movie container.
 
-It accepts the syntax: @var{movie_name}[:@var{options}] where
-@var{movie_name} is the name of the resource to read (not necessarily
-a file but also a device or a stream accessed through some protocol),
-and @var{options} is an optional sequence of @var{key}=@var{value}
-pairs, separated by ":".
-
-The description of the accepted options follows.
+This filter accepts the following options:
 
 @table @option
+@item filename
+The name of the resource to read (not necessarily a file but also a device or a
+stream accessed through some protocol).
 
 @item format_name, f
 Specifies the format assumed for the movie to read, and can be either
@@ -6601,23 +8075,25 @@
 movie --> scale--> deltapts1 -------+
 @end example
 
-Some examples follow.
+@subsection Examples
 
 @itemize
 @item
 Skip 3.2 seconds from the start of the avi file in.avi, and overlay it
 on top of the input labelled as "in":
 @example
-movie=in.avi:seek_point=3.2, scale=180:-1, setpts=PTS-STARTPTS [movie];
-[in] setpts=PTS-STARTPTS, [movie] overlay=16:16 [out]
+movie=in.avi:seek_point=3.2, scale=180:-1, setpts=PTS-STARTPTS [over];
+[in] setpts=PTS-STARTPTS [main];
+[main][over] overlay=16:16 [out]
 @end example
 
 @item
 Read from a video4linux2 device, and overlay it on top of the input
 labelled as "in":
 @example
-movie=/dev/video0:f=video4linux2, scale=180:-1, setpts=PTS-STARTPTS [movie];
-[in] setpts=PTS-STARTPTS, [movie] overlay=16:16 [out]
+movie=/dev/video0:f=video4linux2, scale=180:-1, setpts=PTS-STARTPTS [over];
+[in] setpts=PTS-STARTPTS [main];
+[main][over] overlay=16:16 [out]
 @end example
 
 @item
diff --git a/doc/formats.texi b/doc/formats.texi
new file mode 100644
index 0000000..44e4532
--- /dev/null
+++ b/doc/formats.texi
@@ -0,0 +1,146 @@
+@chapter Format Options
+@c man begin FORMAT OPTIONS
+
+The libavformat library provides some generic global options, which
+can be set on all the muxers and demuxers. In addition each muxer or
+demuxer may support so-called private options, which are specific for
+that component.
+
+Options may be set by specifying -@var{option} @var{value} in the
+FFmpeg tools, or by setting the value explicitly in the
+@code{AVFormatContext} options or using the @file{libavutil/opt.h} API
+for programmatic use.
+
+The list of supported options follows:
+
+@table @option
+@item avioflags @var{flags} (@emph{input/output})
+Possible values:
+@table @samp
+@item direct
+Reduce buffering.
+@end table
+
+@item probesize @var{integer} (@emph{input})
+Set probing size in bytes, i.e. the size of the data to analyze to get
+stream information. A higher value will allow to detect more
+information in case it is dispersed into the stream, but will increase
+latency. Must be an integer not lesser than 32. It is 5000000 by default.
+
+@item packetsize @var{integer} (@emph{output})
+Set packet size.
+
+@item fflags @var{flags} (@emph{input/output})
+Set format flags.
+
+Possible values:
+@table @samp
+@item ignidx
+Ignore index.
+@item genpts
+Generate PTS.
+@item nofillin
+Do not fill in missing values that can be exactly calculated.
+@item noparse
+Disable AVParsers, this needs @code{+nofillin} too.
+@item igndts
+Ignore DTS.
+@item discardcorrupt
+Discard corrupted frames.
+@item sortdts
+Try to interleave output packets by DTS.
+@item keepside
+Do not merge side data.
+@item latm
+Enable RTP MP4A-LATM payload.
+@item nobuffer
+Reduce the latency introduced by optional buffering
+@end table
+
+@item analyzeduration @var{integer} (@emph{input})
+Specify how many microseconds are analyzed to probe the input. A
+higher value will allow to detect more accurate information, but will
+increase latency. It defaults to 5,000,000 microseconds = 5 seconds.
+
+@item cryptokey @var{hexadecimal string} (@emph{input})
+Set decryption key.
+
+@item indexmem @var{integer} (@emph{input})
+Set max memory used for timestamp index (per stream).
+
+@item rtbufsize @var{integer} (@emph{input})
+Set max memory used for buffering real-time frames.
+
+@item fdebug @var{flags} (@emph{input/output})
+Print specific debug info.
+
+Possible values:
+@table @samp
+@item ts
+@end table
+
+@item max_delay @var{integer} (@emph{input/output})
+Set maximum muxing or demuxing delay in microseconds.
+
+@item fpsprobesize @var{integer} (@emph{input})
+Set number of frames used to probe fps.
+
+@item audio_preload @var{integer} (@emph{output})
+Set microseconds by which audio packets should be interleaved earlier.
+
+@item chunk_duration @var{integer} (@emph{output})
+Set microseconds for each chunk.
+
+@item chunk_size @var{integer} (@emph{output})
+Set size in bytes for each chunk.
+
+@item err_detect, f_err_detect @var{flags} (@emph{input})
+Set error detection flags. @code{f_err_detect} is deprecated and
+should be used only via the @command{ffmpeg} tool.
+
+Possible values:
+@table @samp
+@item crccheck
+Verify embedded CRCs.
+@item bitstream
+Detect bitstream specification deviations.
+@item buffer
+Detect improper bitstream length.
+@item explode
+Abort decoding on minor error detection.
+@item careful
+Consider things that violate the spec and have not been seen in the
+wild as errors.
+@item compliant
+Consider all spec non compliancies as errors.
+@item aggressive
+Consider things that a sane encoder should not do as an error.
+@end table
+
+@item use_wallclock_as_timestamps @var{integer} (@emph{input})
+Use wallclock as timestamps.
+
+@item avoid_negative_ts @var{integer} (@emph{output})
+Shift timestamps to make them positive. A value of 1 enables shifting,
+a value of 0 disables it, the default value of -1 enables shifting
+when required by the target format.
+
+When shifting is enabled, all output timestamps are shifted by the
+same amount. Audio, video, and subtitles desynching and relative
+timestamp differences are preserved compared to how they would have
+been without shifting.
+
+Also note that this affects only leading negative timestamps, and not
+non-monotonic negative timestamps.
+
+@item flush_packets @var{integer} (@emph{output})
+Flush the underlying I/O stream after each packet. Default 1 enables it, and
+has the effect of reducing the latency; 0 disables it and may slightly
+increase performance in some cases.
+@end table
+
+@c man end FORMAT OPTIONS
+
+@include demuxers.texi
+@include muxers.texi
+@include metadata.texi
diff --git a/doc/general.texi b/doc/general.texi
index 39b9360..b6e731a 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -418,7 +418,6 @@
 @item .Y.U.V       @tab X @tab X
     @tab one raw file per component
 @item animated GIF @tab X @tab X
-    @tab Only uncompressed GIFs are generated.
 @item BMP          @tab X @tab X
     @tab Microsoft BMP image
 @item PIX          @tab   @tab X
diff --git a/doc/indevs.texi b/doc/indevs.texi
index cc5d666..5e8d215 100644
--- a/doc/indevs.texi
+++ b/doc/indevs.texi
@@ -86,7 +86,7 @@
 Set the video size in the captured video.
 
 @item framerate
-Set the framerate in the captured video.
+Set the frame rate in the captured video.
 
 @item sample_rate
 Set the sample rate (in Hz) of the captured audio.
@@ -600,7 +600,7 @@
 the device.
 
 Video4Linux2 devices usually support a limited set of
-@var{width}x@var{height} sizes and framerates. You can check which are
+@var{width}x@var{height} sizes and frame rates. You can check which are
 supported using @command{-list_formats all} for Video4Linux2 devices.
 Some devices, like TV cards, support one or more standards. It is possible
 to list all the supported standards using @command{-list_standards all}.
@@ -623,7 +623,7 @@
 
 @item
 Grab and record the input of a video4linux2 device, leave the
-framerate and size as previously set:
+frame rate and size as previously set:
 @example
 ffmpeg -f video4linux2 -input_format mjpeg -i /dev/video0 out.mpeg
 @end example
@@ -640,7 +640,8 @@
 option.
 
 @item channel
-Set the input channel number. Default to 0.
+Set the input channel number. Default to -1, which means using the
+previously selected channel.
 
 @item video_size
 Set the video frame size. The argument must be a string in the form
@@ -655,7 +656,7 @@
 available.
 
 @item framerate
-Set the preferred video framerate.
+Set the preferred video frame rate.
 
 @item list_formats
 List available formats (supported pixel formats, codecs, and frame
@@ -771,7 +772,7 @@
 
 @item framerate
 Set the grabbing frame rate. Default value is @code{ntsc},
-corresponding to a framerate of @code{30000/1001}.
+corresponding to a frame rate of @code{30000/1001}.
 
 @item show_region
 Show grabbed region on screen.
diff --git a/doc/mips.txt b/doc/mips.txt
index aabdef0..051b819 100644
--- a/doc/mips.txt
+++ b/doc/mips.txt
@@ -48,6 +48,8 @@
       float_dsp_mips.c
       libm_mips.h
 * libavcodec/mips/
+      aaccoder_mips.c
+      aacpsy_mips.h
       ac3dsp_mips.c
       acelp_filters_mips.c
       acelp_vectors_mips.c
@@ -63,5 +65,6 @@
       fft_table.h
       fft_init_table.c
       fmtconvert_mips.c
+      iirfilter_mips.c
       mpegaudiodsp_mips_fixed.c
       mpegaudiodsp_mips_float.c
diff --git a/doc/multithreading.txt b/doc/multithreading.txt
index a106842..2b992fc 100644
--- a/doc/multithreading.txt
+++ b/doc/multithreading.txt
@@ -57,6 +57,11 @@
 Add CODEC_CAP_FRAME_THREADS to the codec capabilities. There will be very little
 speed gain at this point but it should work.
 
+If there are inter-frame dependencies, so the codec calls
+ff_thread_report/await_progress(), set AVCodecInternal.allocate_progress. The
+frames must then be freed with ff_thread_release_buffer().
+Otherwise leave it at zero and decode directly into the user-supplied frames.
+
 Call ff_thread_report_progress() after some part of the current picture has decoded.
 A good place to put this is where draw_horiz_band() is called - add this if it isn't
 called anywhere, as it's useful too and the implementation is trivial when you're
diff --git a/doc/muxers.texi b/doc/muxers.texi
index 6aae871..f127494 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -245,9 +245,11 @@
 Start the sequence from @var{number}. Default value is 1. Must be a
 positive number.
 
-@item updatefirst 1|0
-If set to 1, update the first written image file again and
-again. Default value is 0.
+@item -update @var{number}
+If @var{number} is nonzero, the filename will always be interpreted as just a
+filename, not a pattern, and this file will be continuously overwritten with new
+images.
+
 @end table
 
 The image muxer supports the .Y.U.V image file format. This format is
@@ -579,6 +581,13 @@
 
 @code{ext} is deprecated in favor or @code{csv}.
 
+@item ffconcat
+Generate an ffconcat file for the created segments. The resulting file
+can be read using the FFmpeg @ref{concat} demuxer.
+
+A list file with the suffix @code{".ffcat"} or @code{".ffconcat"} will
+auto-select this format.
+
 @item m3u8
 Generate an extended M3U8 file, version 3, compliant with
 @url{http://tools.ietf.org/id/draft-pantos-http-live-streaming}.
@@ -773,10 +782,10 @@
 must be escaped; note that this is a second level escaping.
 
 Example: encode something and both archive it in a WebM file and stream it
-as MPEG-TS over UDP:
+as MPEG-TS over UDP (the streams need to be explicitly mapped):
 
 @example
-ffmpeg -i ... -c:v libx264 -c:a mp2 -f tee
+ffmpeg -i ... -c:v libx264 -c:a mp2 -f tee -map 0:v -map 0:a
   "archive-20121107.mkv|[f=mpegts]udp://10.0.1.255:1234/"
 @end example
 
diff --git a/doc/platform.texi b/doc/platform.texi
index bb8e6ca..0703000 100644
--- a/doc/platform.texi
+++ b/doc/platform.texi
@@ -184,16 +184,7 @@
 to make sure you have @code{Runtime Library} set to
 @code{Multi-threaded (/MT)} in your project's settings.
 
-FFmpeg headers do not declare global data for Windows DLLs through the usual
-dllexport/dllimport interface. Such data will be exported properly while
-building, but to use them in your MSVC code you will have to edit the
-appropriate headers and mark the data as dllimport. For example, in
-libavutil/pixdesc.h you should have:
-@example
-extern __declspec(dllimport) const AVPixFmtDescriptor av_pix_fmt_descriptors[];
-@end example
-
-You will also need to define @code{inline} to something MSVC understands:
+You will need to define @code{inline} to something MSVC understands:
 @example
 #define inline __inline
 @end example
diff --git a/doc/print_options.c b/doc/print_options.c
index 339b942..c369cfd 100644
--- a/doc/print_options.c
+++ b/doc/print_options.c
@@ -39,6 +39,9 @@
 
 static void print_option(const AVOption *opts, const AVOption *o, int per_stream)
 {
+    if (!(o->flags & (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM)))
+        return;
+
     printf("@item -%s%s @var{", o->name, per_stream ? "[:stream_specifier]" : "");
     switch (o->type) {
     case AV_OPT_TYPE_BINARY:   printf("hexadecimal string"); break;
diff --git a/doc/protocols.texi b/doc/protocols.texi
index 9940b67..0c56b8b 100644
--- a/doc/protocols.texi
+++ b/doc/protocols.texi
@@ -538,6 +538,11 @@
 ffmpeg -rtsp_flags listen -i rtsp://ownaddress/live.sdp @var{output}
 @end example
 
+@table @option
+@item stimeout
+Socket IO timeout in micro seconds.
+@end table
+
 @section sap
 
 Session Announcement Protocol (RFC 2974). This is not technically a
diff --git a/doc/resampler.texi b/doc/resampler.texi
new file mode 100644
index 0000000..d37d53d
--- /dev/null
+++ b/doc/resampler.texi
@@ -0,0 +1,222 @@
+@chapter Resampler Options
+@c man begin RESAMPLER OPTIONS
+
+The audio resampler supports the following named options.
+
+Options may be set by specifying -@var{option} @var{value} in the
+FFmpeg tools, @var{option}=@var{value} for the aresample filter,
+by setting the value explicitly in the
+@code{SwrContext} options or using the @file{libavutil/opt.h} API for
+programmatic use.
+
+@table @option
+
+@item ich, in_channel_count
+Set the number of input channels. Default value is 0. Setting this
+value is not mandatory if the corresponding channel layout
+@option{in_channel_layout} is set.
+
+@item och, out_channel_count
+Set the number of output channels. Default value is 0. Setting this
+value is not mandatory if the corresponding channel layout
+@option{out_channel_layout} is set.
+
+@item uch, used_channel_count
+Set the number of used input channels. Default value is 0. This option is
+only used for special remapping.
+
+@item isr, in_sample_rate
+Set the input sample rate. Default value is 0.
+
+@item osr, out_sample_rate
+Set the output sample rate. Default value is 0.
+
+@item isf, in_sample_fmt
+Specify the input sample format. It is set by default to @code{none}.
+
+@item osf, out_sample_fmt
+Specify the output sample format. It is set by default to @code{none}.
+
+@item tsf, internal_sample_fmt
+Set the internal sample format. Default value is @code{none}.
+This will automatically be chosen when it is not explicitly set.
+
+@item icl, in_channel_layout
+Set the input channel layout.
+
+@item ocl, out_channel_layout
+Set the output channel layout.
+
+@item clev, center_mix_level
+Set the center mix level. It is a value expressed in deciBel, and must be
+in the interval [-32,32].
+
+@item slev, surround_mix_level
+Set the surround mix level. It is a value expressed in deciBel, and must
+be in the interval [-32,32].
+
+@item lfe_mix_level
+Set LFE mix into non LFE level. It is used when there is a LFE input but no
+LFE output. It is a value expressed in deciBel, and must
+be in the interval [-32,32].
+
+@item rmvol, rematrix_volume
+Set rematrix volume. Default value is 1.0.
+
+@item flags, swr_flags
+Set flags used by the converter. Default value is 0.
+
+It supports the following individual flags:
+@table @option
+@item res
+force resampling, this flag forces resampling to be used even when the
+input and output sample rates match.
+@end table
+
+@item dither_scale
+Set the dither scale. Default value is 1.
+
+@item dither_method
+Set dither method. Default value is 0.
+
+Supported values:
+@table @samp
+@item rectangular
+select rectangular dither
+@item triangular
+select triangular dither
+@item triangular_hp
+select triangular dither with high pass
+@item lipshitz
+select lipshitz noise shaping dither
+@item shibata
+select shibata noise shaping dither
+@item low_shibata
+select low shibata noise shaping dither
+@item high_shibata
+select high shibata noise shaping dither
+@item f_weighted
+select f-weighted noise shaping dither
+@item modified_e_weighted
+select modified-e-weighted noise shaping dither
+@item improved_e_weighted
+select improved-e-weighted noise shaping dither
+
+@end table
+
+@item resampler
+Set resampling engine. Default value is swr.
+
+Supported values:
+@table @samp
+@item swr
+select the native SW Resampler; filter options precision and cheby are not
+applicable in this case.
+@item soxr
+select the SoX Resampler (where available); compensation, and filter options
+filter_size, phase_shift, filter_type & kaiser_beta, are not applicable in this
+case.
+@end table
+
+@item filter_size
+For swr only, set resampling filter size, default value is 32.
+
+@item phase_shift
+For swr only, set resampling phase shift, default value is 10, and must be in
+the interval [0,30].
+
+@item linear_interp
+Use Linear Interpolation if set to 1, default value is 0.
+
+@item cutoff
+Set cutoff frequency (swr: 6dB point; soxr: 0dB point) ratio; must be a float
+value between 0 and 1.  Default value is 0.97 with swr, and 0.91 with soxr
+(which, with a sample-rate of 44100, preserves the entire audio band to 20kHz).
+
+@item precision
+For soxr only, the precision in bits to which the resampled signal will be
+calculated.  The default value of 20 (which, with suitable dithering, is
+appropriate for a destination bit-depth of 16) gives SoX's 'High Quality'; a
+value of 28 gives SoX's 'Very High Quality'.
+
+@item cheby
+For soxr only, selects passband rolloff none (Chebyshev) & higher-precision
+approximation for 'irrational' ratios. Default value is 0.
+
+@item async
+For swr only, simple 1 parameter audio sync to timestamps using stretching,
+squeezing, filling and trimming. Setting this to 1 will enable filling and
+trimming, larger values represent the maximum amount in samples that the data
+may be stretched or squeezed for each second.
+Default value is 0, thus no compensation is applied to make the samples match
+the audio timestamps.
+
+@item first_pts
+For swr only, assume the first pts should be this value. The time unit is 1 / sample rate.
+This allows for padding/trimming at the start of stream. By default, no
+assumption is made about the first frame's expected pts, so no padding or
+trimming is done. For example, this could be set to 0 to pad the beginning with
+silence if an audio stream starts after the video stream or to trim any samples
+with a negative pts due to encoder delay.
+
+@item min_comp
+For swr only, set the minimum difference between timestamps and audio data (in
+seconds) to trigger stretching/squeezing/filling or trimming of the
+data to make it match the timestamps. The default is that
+stretching/squeezing/filling and trimming is disabled
+(@option{min_comp} = @code{FLT_MAX}).
+
+@item min_hard_comp
+For swr only, set the minimum difference between timestamps and audio data (in
+seconds) to trigger adding/dropping samples to make it match the
+timestamps.  This option effectively is a threshold to select between
+hard (trim/fill) and soft (squeeze/stretch) compensation. Note that
+all compensation is by default disabled through @option{min_comp}.
+The default is 0.1.
+
+@item comp_duration
+For swr only, set duration (in seconds) over which data is stretched/squeezed
+to make it match the timestamps. Must be a non-negative double float value,
+default value is 1.0.
+
+@item max_soft_comp
+For swr only, set maximum factor by which data is stretched/squeezed to make it
+match the timestamps. Must be a non-negative double float value, default value
+is 0.
+
+@item matrix_encoding
+Select matrixed stereo encoding.
+
+It accepts the following values:
+@table @samp
+@item none
+select none
+@item dolby
+select Dolby
+@item dplii
+select Dolby Pro Logic II
+@end table
+
+Default value is @code{none}.
+
+@item filter_type
+For swr only, select resampling filter type. This only affects resampling
+operations.
+
+It accepts the following values:
+@table @samp
+@item cubic
+select cubic
+@item blackman_nuttall
+select Blackman Nuttall Windowed Sinc
+@item kaiser
+select Kaiser Windowed Sinc
+@end table
+
+@item kaiser_beta
+For swr only, set Kaiser Window Beta value. Must be an integer in the
+interval [2,16], default value is 9.
+
+@end table
+
+@c man end RESAMPLER OPTIONS
diff --git a/doc/scaler.texi b/doc/scaler.texi
new file mode 100644
index 0000000..c33b6d9
--- /dev/null
+++ b/doc/scaler.texi
@@ -0,0 +1,99 @@
+@chapter Scaler Options
+@c man begin SCALER OPTIONS
+
+The video scaler supports the following named options.
+
+Options may be set by specifying -@var{option} @var{value} in the
+FFmpeg tools. For programmatic use, they can be set explicitly in the
+@code{SwsContext} options or through the @file{libavutil/opt.h} API.
+
+@table @option
+
+@item sws_flags
+Set the scaler flags. This is also used to set the scaling
+algorithm. Only a single algorithm should be selected.
+
+It accepts the following values:
+@table @samp
+@item fast_bilinear
+Select fast bilinear scaling algorithm.
+
+@item bilinear
+Select bilinear scaling algorithm.
+
+@item bicubic
+Select bicubic scaling algorithm.
+
+@item experimental
+Select experimental scaling algorithm.
+
+@item neighbor
+Select nearest neighbor rescaling algorithm.
+
+@item area
+Select averaging area rescaling algorithm.
+
+@item bicubiclin
+Select bicubic scaling algorithm for the luma component, bilinear for
+chroma components.
+
+@item gauss
+Select Gaussian rescaling algorithm.
+
+@item sinc
+Select sinc rescaling algorithm.
+
+@item lanczos
+Select lanczos rescaling algorithm.
+
+@item spline
+Select natural bicubic spline rescaling algorithm.
+
+@item print_info
+Enable printing/debug logging.
+
+@item accurate_rnd
+Enable accurate rounding.
+
+@item full_chroma_int
+Enable full chroma interpolation.
+
+@item full_chroma_inp
+Select full chroma input.
+
+@item bitexact
+Enable bitexact output.
+@end table
+
+@item srcw
+Set source width.
+
+@item srch
+Set source height.
+
+@item dstw
+Set destination width.
+
+@item dsth
+Set destination height.
+
+@item src_format
+Set source pixel format (must be expressed as an integer).
+
+@item dst_format
+Set destination pixel format (must be expressed as an integer).
+
+@item src_range
+Select source range.
+
+@item dst_range
+Select destination range.
+
+@item param0, param1
+Set scaling algorithm parameters. The specified values are specific of
+some scaling algorithms and ignored by others. The specified values
+are floating point number values.
+
+@end table
+
+@c man end SCALER OPTIONS
diff --git a/doc/syntax.texi b/doc/syntax.texi
deleted file mode 100644
index af22d6c..0000000
--- a/doc/syntax.texi
+++ /dev/null
@@ -1,258 +0,0 @@
-@chapter Syntax
-@c man begin SYNTAX
-
-This section documents the syntax and formats employed by the FFmpeg
-libraries and tools.
-
-@anchor{quoting_and_escaping}
-@section Quoting and escaping
-
-FFmpeg adopts the following quoting and escaping mechanism, unless
-explicitly specified. The following rules are applied:
-
-@itemize
-@item
-@code{'} and @code{\} are special characters (respectively used for
-quoting and escaping). In addition to them, there might be other
-special characters depending on the specific syntax where the escaping
-and quoting are employed.
-
-@item
-A special character is escaped by prefixing it with a '\'.
-
-@item
-All characters enclosed between '' are included literally in the
-parsed string. The quote character @code{'} itself cannot be quoted,
-so you may need to close the quote and escape it.
-
-@item
-Leading and trailing whitespaces, unless escaped or quoted, are
-removed from the parsed string.
-@end itemize
-
-Note that you may need to add a second level of escaping when using
-the command line or a script, which depends on the syntax of the
-adopted shell language.
-
-The function @code{av_get_token} defined in
-@file{libavutil/avstring.h} can be used to parse a token quoted or
-escaped according to the rules defined above.
-
-The tool @file{tools/ffescape} in the FFmpeg source tree can be used
-to automatically quote or escape a string in a script.
-
-@subsection Examples
-
-@itemize
-@item
-Escape the string @code{Crime d'Amour} containing the @code{'} special
-character:
-@example
-Crime d\'Amour
-@end example
-
-@item
-The string above contains a quote, so the @code{'} needs to be escaped
-when quoting it:
-@example
-'Crime d'\''Amour'
-@end example
-
-@item
-Include leading or trailing whitespaces using quoting:
-@example
-'  this string starts and ends with whitespaces  '
-@end example
-
-@item
-Escaping and quoting can be mixed together:
-@example
-' The string '\'string\'' is a string '
-@end example
-
-@item
-To include a literal @code{\} you can use either escaping or quoting:
-@example
-'c:\foo' can be written as c:\\foo
-@end example
-@end itemize
-
-@anchor{date syntax}
-@section Date
-
-The accepted syntax is:
-@example
-[(YYYY-MM-DD|YYYYMMDD)[T|t| ]]((HH:MM:SS[.m...]]])|(HHMMSS[.m...]]]))[Z]
-now
-@end example
-
-If the value is "now" it takes the current time.
-
-Time is local time unless Z is appended, in which case it is
-interpreted as UTC.
-If the year-month-day part is not specified it takes the current
-year-month-day.
-
-@anchor{time duration syntax}
-@section Time duration
-
-The accepted syntax is:
-@example
-[-][HH:]MM:SS[.m...]
-[-]S+[.m...]
-@end example
-
-@var{HH} expresses the number of hours, @var{MM} the number a of minutes
-and @var{SS} the number of seconds.
-
-@anchor{video size syntax}
-@section Video size
-Specify the size of the sourced video, it may be a string of the form
-@var{width}x@var{height}, or the name of a size abbreviation.
-
-The following abbreviations are recognized:
-@table @samp
-@item ntsc
-720x480
-@item pal
-720x576
-@item qntsc
-352x240
-@item qpal
-352x288
-@item sntsc
-640x480
-@item spal
-768x576
-@item film
-352x240
-@item ntsc-film
-352x240
-@item sqcif
-128x96
-@item qcif
-176x144
-@item cif
-352x288
-@item 4cif
-704x576
-@item 16cif
-1408x1152
-@item qqvga
-160x120
-@item qvga
-320x240
-@item vga
-640x480
-@item svga
-800x600
-@item xga
-1024x768
-@item uxga
-1600x1200
-@item qxga
-2048x1536
-@item sxga
-1280x1024
-@item qsxga
-2560x2048
-@item hsxga
-5120x4096
-@item wvga
-852x480
-@item wxga
-1366x768
-@item wsxga
-1600x1024
-@item wuxga
-1920x1200
-@item woxga
-2560x1600
-@item wqsxga
-3200x2048
-@item wquxga
-3840x2400
-@item whsxga
-6400x4096
-@item whuxga
-7680x4800
-@item cga
-320x200
-@item ega
-640x350
-@item hd480
-852x480
-@item hd720
-1280x720
-@item hd1080
-1920x1080
-@item 2k
-2048x1080
-@item 2kflat
-1998x1080
-@item 2kscope
-2048x858
-@item 4k
-4096x2160
-@item 4kflat
-3996x2160
-@item 4kscope
-4096x1716
-@end table
-
-@anchor{video rate syntax}
-@section Video rate
-
-Specify the frame rate of a video, expressed as the number of frames
-generated per second. It has to be a string in the format
-@var{frame_rate_num}/@var{frame_rate_den}, an integer number, a float
-number or a valid video frame rate abbreviation.
-
-The following abbreviations are recognized:
-@table @samp
-@item ntsc
-30000/1001
-@item pal
-25/1
-@item qntsc
-30000/1001
-@item qpal
-25/1
-@item sntsc
-30000/1001
-@item spal
-25/1
-@item film
-24/1
-@item ntsc-film
-24000/1001
-@end table
-
-@anchor{ratio syntax}
-@section Ratio
-
-A ratio can be expressed as an expression, or in the form
-@var{numerator}:@var{denominator}.
-
-Note that a ratio with infinite (1/0) or negative value is
-considered valid, so you should check on the returned value if you
-want to exclude those values.
-
-The undefined value can be expressed using the "0:0" string.
-
-@anchor{color syntax}
-@section Color
-
-It can be the name of a color (case insensitive match) or a
-[0x|#]RRGGBB[AA] sequence, possibly followed by "@@" and a string
-representing the alpha component.
-
-The alpha component may be a string composed by "0x" followed by an
-hexadecimal number or a decimal number between 0.0 and 1.0, which
-represents the opacity value (0x00/0.0 means completely transparent,
-0xff/1.0 completely opaque).
-If the alpha component is not specified then 0xff is assumed.
-
-The string "random" will result in a random color.
-
-@c man end SYNTAX
diff --git a/doc/texi2pod.pl b/doc/texi2pod.pl
index 5c3aba6..610f349 100755
--- a/doc/texi2pod.pl
+++ b/doc/texi2pod.pl
@@ -121,7 +121,7 @@
         $chapters{$chapter_name} .= postprocess($chapter) if ($chapter_name);
 
         # start new chapter
-        $chapter_name = $1, push (@chapters_sequence, $chapter_name);
+        $chapter_name = $1, push (@chapters_sequence, $chapter_name) unless $skipping;
         $chapters{$chapter_name} = "" unless exists $chapters{$chapter_name};
         $chapter = "";
         $output = 1;
@@ -377,7 +377,7 @@
     s/\(?\@xref\{(?:[^\}]*)\}(?:[^.<]|(?:<[^<>]*>))*\.\)?//g;
     s/\s+\(\@pxref\{(?:[^\}]*)\}\)//g;
     s/;\s+\@pxref\{(?:[^\}]*)\}//g;
-    s/\@ref\{(?:[^,]*,)(?:[^,]*,)([^,\}]*).*\}/$1/g;
+    s/\@ref\{(?:[^,\}]*,)(?:[^,\}]*,)([^,\}]*).*\}/$1/g;
     s/\@ref\{([^\}]*)\}/$1/g;
     s/\@noindent\s*//g;
     s/\@refill//g;
diff --git a/doc/utils.texi b/doc/utils.texi
new file mode 100644
index 0000000..75f7fdc
--- /dev/null
+++ b/doc/utils.texi
@@ -0,0 +1,592 @@
+@chapter Syntax
+@c man begin SYNTAX
+
+This section documents the syntax and formats employed by the FFmpeg
+libraries and tools.
+
+@anchor{quoting_and_escaping}
+@section Quoting and escaping
+
+FFmpeg adopts the following quoting and escaping mechanism, unless
+explicitly specified. The following rules are applied:
+
+@itemize
+@item
+@code{'} and @code{\} are special characters (respectively used for
+quoting and escaping). In addition to them, there might be other
+special characters depending on the specific syntax where the escaping
+and quoting are employed.
+
+@item
+A special character is escaped by prefixing it with a '\'.
+
+@item
+All characters enclosed between '' are included literally in the
+parsed string. The quote character @code{'} itself cannot be quoted,
+so you may need to close the quote and escape it.
+
+@item
+Leading and trailing whitespaces, unless escaped or quoted, are
+removed from the parsed string.
+@end itemize
+
+Note that you may need to add a second level of escaping when using
+the command line or a script, which depends on the syntax of the
+adopted shell language.
+
+The function @code{av_get_token} defined in
+@file{libavutil/avstring.h} can be used to parse a token quoted or
+escaped according to the rules defined above.
+
+The tool @file{tools/ffescape} in the FFmpeg source tree can be used
+to automatically quote or escape a string in a script.
+
+@subsection Examples
+
+@itemize
+@item
+Escape the string @code{Crime d'Amour} containing the @code{'} special
+character:
+@example
+Crime d\'Amour
+@end example
+
+@item
+The string above contains a quote, so the @code{'} needs to be escaped
+when quoting it:
+@example
+'Crime d'\''Amour'
+@end example
+
+@item
+Include leading or trailing whitespaces using quoting:
+@example
+'  this string starts and ends with whitespaces  '
+@end example
+
+@item
+Escaping and quoting can be mixed together:
+@example
+' The string '\'string\'' is a string '
+@end example
+
+@item
+To include a literal @code{\} you can use either escaping or quoting:
+@example
+'c:\foo' can be written as c:\\foo
+@end example
+@end itemize
+
+@anchor{date syntax}
+@section Date
+
+The accepted syntax is:
+@example
+[(YYYY-MM-DD|YYYYMMDD)[T|t| ]]((HH:MM:SS[.m...]]])|(HHMMSS[.m...]]]))[Z]
+now
+@end example
+
+If the value is "now" it takes the current time.
+
+Time is local time unless Z is appended, in which case it is
+interpreted as UTC.
+If the year-month-day part is not specified it takes the current
+year-month-day.
+
+@anchor{time duration syntax}
+@section Time duration
+
+The accepted syntax is:
+@example
+[-][HH:]MM:SS[.m...]
+[-]S+[.m...]
+@end example
+
+@var{HH} expresses the number of hours, @var{MM} the number a of minutes
+and @var{SS} the number of seconds.
+
+@anchor{video size syntax}
+@section Video size
+Specify the size of the sourced video, it may be a string of the form
+@var{width}x@var{height}, or the name of a size abbreviation.
+
+The following abbreviations are recognized:
+@table @samp
+@item ntsc
+720x480
+@item pal
+720x576
+@item qntsc
+352x240
+@item qpal
+352x288
+@item sntsc
+640x480
+@item spal
+768x576
+@item film
+352x240
+@item ntsc-film
+352x240
+@item sqcif
+128x96
+@item qcif
+176x144
+@item cif
+352x288
+@item 4cif
+704x576
+@item 16cif
+1408x1152
+@item qqvga
+160x120
+@item qvga
+320x240
+@item vga
+640x480
+@item svga
+800x600
+@item xga
+1024x768
+@item uxga
+1600x1200
+@item qxga
+2048x1536
+@item sxga
+1280x1024
+@item qsxga
+2560x2048
+@item hsxga
+5120x4096
+@item wvga
+852x480
+@item wxga
+1366x768
+@item wsxga
+1600x1024
+@item wuxga
+1920x1200
+@item woxga
+2560x1600
+@item wqsxga
+3200x2048
+@item wquxga
+3840x2400
+@item whsxga
+6400x4096
+@item whuxga
+7680x4800
+@item cga
+320x200
+@item ega
+640x350
+@item hd480
+852x480
+@item hd720
+1280x720
+@item hd1080
+1920x1080
+@item 2k
+2048x1080
+@item 2kflat
+1998x1080
+@item 2kscope
+2048x858
+@item 4k
+4096x2160
+@item 4kflat
+3996x2160
+@item 4kscope
+4096x1716
+@end table
+
+@anchor{video rate syntax}
+@section Video rate
+
+Specify the frame rate of a video, expressed as the number of frames
+generated per second. It has to be a string in the format
+@var{frame_rate_num}/@var{frame_rate_den}, an integer number, a float
+number or a valid video frame rate abbreviation.
+
+The following abbreviations are recognized:
+@table @samp
+@item ntsc
+30000/1001
+@item pal
+25/1
+@item qntsc
+30000/1001
+@item qpal
+25/1
+@item sntsc
+30000/1001
+@item spal
+25/1
+@item film
+24/1
+@item ntsc-film
+24000/1001
+@end table
+
+@anchor{ratio syntax}
+@section Ratio
+
+A ratio can be expressed as an expression, or in the form
+@var{numerator}:@var{denominator}.
+
+Note that a ratio with infinite (1/0) or negative value is
+considered valid, so you should check on the returned value if you
+want to exclude those values.
+
+The undefined value can be expressed using the "0:0" string.
+
+@anchor{color syntax}
+@section Color
+
+It can be the name of a color (case insensitive match) or a
+[0x|#]RRGGBB[AA] sequence, possibly followed by "@@" and a string
+representing the alpha component.
+
+The alpha component may be a string composed by "0x" followed by an
+hexadecimal number or a decimal number between 0.0 and 1.0, which
+represents the opacity value (0x00/0.0 means completely transparent,
+0xff/1.0 completely opaque).
+If the alpha component is not specified then 0xff is assumed.
+
+The string "random" will result in a random color.
+
+@c man end SYNTAX
+
+@chapter Expression Evaluation
+@c man begin EXPRESSION EVALUATION
+
+When evaluating an arithmetic expression, FFmpeg uses an internal
+formula evaluator, implemented through the @file{libavutil/eval.h}
+interface.
+
+An expression may contain unary, binary operators, constants, and
+functions.
+
+Two expressions @var{expr1} and @var{expr2} can be combined to form
+another expression "@var{expr1};@var{expr2}".
+@var{expr1} and @var{expr2} are evaluated in turn, and the new
+expression evaluates to the value of @var{expr2}.
+
+The following binary operators are available: @code{+}, @code{-},
+@code{*}, @code{/}, @code{^}.
+
+The following unary operators are available: @code{+}, @code{-}.
+
+The following functions are available:
+@table @option
+@item abs(x)
+Compute absolute value of @var{x}.
+
+@item acos(x)
+Compute arccosine of @var{x}.
+
+@item asin(x)
+Compute arcsine of @var{x}.
+
+@item atan(x)
+Compute arctangent of @var{x}.
+
+@item between(x, min, max)
+Return 1 if @var{x} is greater than or equal to @var{min} and lesser than or
+equal to @var{max}, 0 otherwise.
+
+@item bitand(x, y)
+@item bitor(x, y)
+Compute bitwise and/or operation on @var{x} and @var{y}.
+
+The results of the evaluation of @var{x} and @var{y} are converted to
+integers before executing the bitwise operation.
+
+Note that both the conversion to integer and the conversion back to
+floating point can lose precision. Beware of unexpected results for
+large numbers (usually 2^53 and larger).
+
+@item ceil(expr)
+Round the value of expression @var{expr} upwards to the nearest
+integer. For example, "ceil(1.5)" is "2.0".
+
+@item cos(x)
+Compute cosine of @var{x}.
+
+@item cosh(x)
+Compute hyperbolic cosine of @var{x}.
+
+@item eq(x, y)
+Return 1 if @var{x} and @var{y} are equivalent, 0 otherwise.
+
+@item exp(x)
+Compute exponential of @var{x} (with base @code{e}, the Euler's number).
+
+@item floor(expr)
+Round the value of expression @var{expr} downwards to the nearest
+integer. For example, "floor(-1.5)" is "-2.0".
+
+@item gauss(x)
+Compute Gauss function of @var{x}, corresponding to
+@code{exp(-x*x/2) / sqrt(2*PI)}.
+
+@item gcd(x, y)
+Return the greatest common divisor of @var{x} and @var{y}. If both @var{x} and
+@var{y} are 0 or either or both are less than zero then behavior is undefined.
+
+@item gt(x, y)
+Return 1 if @var{x} is greater than @var{y}, 0 otherwise.
+
+@item gte(x, y)
+Return 1 if @var{x} is greater than or equal to @var{y}, 0 otherwise.
+
+@item hypot(x, y)
+This function is similar to the C function with the same name; it returns
+"sqrt(@var{x}*@var{x} + @var{y}*@var{y})", the length of the hypotenuse of a
+right triangle with sides of length @var{x} and @var{y}, or the distance of the
+point (@var{x}, @var{y}) from the origin.
+
+@item if(x, y)
+Evaluate @var{x}, and if the result is non-zero return the result of
+the evaluation of @var{y}, return 0 otherwise.
+
+@item if(x, y, z)
+Evaluate @var{x}, and if the result is non-zero return the evaluation
+result of @var{y}, otherwise the evaluation result of @var{z}.
+
+@item ifnot(x, y)
+Evaluate @var{x}, and if the result is zero return the result of the
+evaluation of @var{y}, return 0 otherwise.
+
+@item ifnot(x, y, z)
+Evaluate @var{x}, and if the result is zero return the evaluation
+result of @var{y}, otherwise the evaluation result of @var{z}.
+
+@item isinf(x)
+Return 1.0 if @var{x} is +/-INFINITY, 0.0 otherwise.
+
+@item isnan(x)
+Return 1.0 if @var{x} is NAN, 0.0 otherwise.
+
+@item ld(var)
+Allow to load the value of the internal variable with number
+@var{var}, which was previously stored with st(@var{var}, @var{expr}).
+The function returns the loaded value.
+
+@item log(x)
+Compute natural logarithm of @var{x}.
+
+@item lt(x, y)
+Return 1 if @var{x} is lesser than @var{y}, 0 otherwise.
+
+@item lte(x, y)
+Return 1 if @var{x} is lesser than or equal to @var{y}, 0 otherwise.
+
+@item max(x, y)
+Return the maximum between @var{x} and @var{y}.
+
+@item min(x, y)
+Return the maximum between @var{x} and @var{y}.
+
+@item mod(x, y)
+Compute the remainder of division of @var{x} by @var{y}.
+
+@item not(expr)
+Return 1.0 if @var{expr} is zero, 0.0 otherwise.
+
+@item pow(x, y)
+Compute the power of @var{x} elevated @var{y}, it is equivalent to
+"(@var{x})^(@var{y})".
+
+@item print(t)
+@item print(t, l)
+Print the value of expression @var{t} with loglevel @var{l}. If
+@var{l} is not specified then a default log level is used.
+Returns the value of the expression printed.
+
+Prints t with loglevel l
+
+@item random(x)
+Return a pseudo random value between 0.0 and 1.0. @var{x} is the index of the
+internal variable which will be used to save the seed/state.
+
+@item root(expr, max)
+Find an input value for which the function represented by @var{expr}
+with argument @var{ld(0)} is 0 in the interval 0..@var{max}.
+
+The expression in @var{expr} must denote a continuous function or the
+result is undefined.
+
+@var{ld(0)} is used to represent the function input value, which means
+that the given expression will be evaluated multiple times with
+various input values that the expression can access through
+@code{ld(0)}. When the expression evaluates to 0 then the
+corresponding input value will be returned.
+
+@item sin(x)
+Compute sine of @var{x}.
+
+@item sinh(x)
+Compute hyperbolic sine of @var{x}.
+
+@item sqrt(expr)
+Compute the square root of @var{expr}. This is equivalent to
+"(@var{expr})^.5".
+
+@item squish(x)
+Compute expression @code{1/(1 + exp(4*x))}.
+
+@item st(var, expr)
+Allow to store the value of the expression @var{expr} in an internal
+variable. @var{var} specifies the number of the variable where to
+store the value, and it is a value ranging from 0 to 9. The function
+returns the value stored in the internal variable.
+Note, Variables are currently not shared between expressions.
+
+@item tan(x)
+Compute tangent of @var{x}.
+
+@item tanh(x)
+Compute hyperbolic tangent of @var{x}.
+
+@item taylor(expr, x)
+@item taylor(expr, x, id)
+Evaluate a Taylor series at @var{x}, given an expression representing
+the @code{ld(id)}-th derivative of a function at 0.
+
+When the series does not converge the result is undefined.
+
+@var{ld(id)} is used to represent the derivative order in @var{expr},
+which means that the given expression will be evaluated multiple times
+with various input values that the expression can access through
+@code{ld(id)}. If @var{id} is not specified then 0 is assumed.
+
+Note, when you have the derivatives at y instead of 0,
+@code{taylor(expr, x-y)} can be used.
+
+@item time(0)
+Return the current (wallclock) time in seconds.
+
+@item trunc(expr)
+Round the value of expression @var{expr} towards zero to the nearest
+integer. For example, "trunc(-1.5)" is "-1.0".
+
+@item while(cond, expr)
+Evaluate expression @var{expr} while the expression @var{cond} is
+non-zero, and returns the value of the last @var{expr} evaluation, or
+NAN if @var{cond} was always false.
+@end table
+
+The following constants are available:
+@table @option
+@item PI
+area of the unit disc, approximately 3.14
+@item E
+exp(1) (Euler's number), approximately 2.718
+@item PHI
+golden ratio (1+sqrt(5))/2, approximately 1.618
+@end table
+
+Assuming that an expression is considered "true" if it has a non-zero
+value, note that:
+
+@code{*} works like AND
+
+@code{+} works like OR
+
+For example the construct:
+@example
+if (A AND B) then C
+@end example
+is equivalent to:
+@example
+if(A*B, C)
+@end example
+
+In your C code, you can extend the list of unary and binary functions,
+and define recognized constants, so that they are available for your
+expressions.
+
+The evaluator also recognizes the International System unit prefixes.
+If 'i' is appended after the prefix, binary prefixes are used, which
+are based on powers of 1024 instead of powers of 1000.
+The 'B' postfix multiplies the value by 8, and can be appended after a
+unit prefix or used alone. This allows using for example 'KB', 'MiB',
+'G' and 'B' as number postfix.
+
+The list of available International System prefixes follows, with
+indication of the corresponding powers of 10 and of 2.
+@table @option
+@item y
+10^-24 / 2^-80
+@item z
+10^-21 / 2^-70
+@item a
+10^-18 / 2^-60
+@item f
+10^-15 / 2^-50
+@item p
+10^-12 / 2^-40
+@item n
+10^-9 / 2^-30
+@item u
+10^-6 / 2^-20
+@item m
+10^-3 / 2^-10
+@item c
+10^-2
+@item d
+10^-1
+@item h
+10^2
+@item k
+10^3 / 2^10
+@item K
+10^3 / 2^10
+@item M
+10^6 / 2^20
+@item G
+10^9 / 2^30
+@item T
+10^12 / 2^40
+@item P
+10^15 / 2^40
+@item E
+10^18 / 2^50
+@item Z
+10^21 / 2^60
+@item Y
+10^24 / 2^70
+@end table
+
+@c man end
+
+@chapter OpenCL Options
+@c man begin OPENCL OPTIONS
+
+When FFmpeg is configured with @code{--enable-opencl}, it is possible
+to set the options for the global OpenCL context.
+
+The list of supported options follows:
+
+@table @option
+@item build_options
+Set build options used to compile the registered kernels.
+
+See reference "OpenCL Specification Version: 1.2 chapter 5.6.4".
+
+@item platform_idx
+Select the index of the platform to run OpenCL code.
+
+The specified index must be one of the indexes in the device list
+which can be obtained with @code{av_opencl_get_device_list()}.
+
+@item device_idx
+Select the index of the device used to run OpenCL code.
+
+The specifed index must be one of the indexes in the device list which
+can be obtained with @code{av_opencl_get_device_list()}.
+
+@end table
+
+@c man end OPENCL OPTIONS
diff --git a/ffmpeg.c b/ffmpeg.c
index 448a9e2..30b69d3 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -64,7 +64,6 @@
 
 # include "libavfilter/avcodec.h"
 # include "libavfilter/avfilter.h"
-# include "libavfilter/avfiltergraph.h"
 # include "libavfilter/buffersrc.h"
 # include "libavfilter/buffersink.h"
 
@@ -120,6 +119,7 @@
 
 static void do_video_stats(OutputStream *ost, int frame_size);
 static int64_t getutime(void);
+static int64_t getmaxrss(void);
 
 static int run_as_daemon  = 0;
 static int64_t video_size = 0;
@@ -128,6 +128,7 @@
 static int64_t extra_size = 0;
 static int nb_frames_dup = 0;
 static int nb_frames_drop = 0;
+static int64_t decode_error_stat[2];
 
 static int current_time;
 AVIOContext *progress_avio = NULL;
@@ -167,7 +168,20 @@
    This is a temporary solution until libavfilter gets real subtitles support.
  */
 
+static int sub2video_get_blank_frame(InputStream *ist)
+{
+    int ret;
+    AVFrame *frame = ist->sub2video.frame;
 
+    av_frame_unref(frame);
+    ist->sub2video.frame->width  = ist->sub2video.w;
+    ist->sub2video.frame->height = ist->sub2video.h;
+    ist->sub2video.frame->format = AV_PIX_FMT_RGB32;
+    if ((ret = av_frame_get_buffer(frame, 32)) < 0)
+        return ret;
+    memset(frame->data[0], 0, frame->height * frame->linesize[0]);
+    return 0;
+}
 
 static void sub2video_copy_rect(uint8_t *dst, int dst_linesize, int w, int h,
                                 AVSubtitleRect *r)
@@ -200,28 +214,27 @@
 
 static void sub2video_push_ref(InputStream *ist, int64_t pts)
 {
-    AVFilterBufferRef *ref = ist->sub2video.ref;
+    AVFrame *frame = ist->sub2video.frame;
     int i;
 
-    ist->sub2video.last_pts = ref->pts = pts;
+    av_assert1(frame->data[0]);
+    ist->sub2video.last_pts = frame->pts = pts;
     for (i = 0; i < ist->nb_filters; i++)
-        av_buffersrc_add_ref(ist->filters[i]->filter,
-                             avfilter_ref_buffer(ref, ~0),
-                             AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT |
-                             AV_BUFFERSRC_FLAG_NO_COPY |
-                             AV_BUFFERSRC_FLAG_PUSH);
+        av_buffersrc_add_frame_flags(ist->filters[i]->filter, frame,
+                                     AV_BUFFERSRC_FLAG_KEEP_REF |
+                                     AV_BUFFERSRC_FLAG_PUSH);
 }
 
 static void sub2video_update(InputStream *ist, AVSubtitle *sub)
 {
     int w = ist->sub2video.w, h = ist->sub2video.h;
-    AVFilterBufferRef *ref = ist->sub2video.ref;
+    AVFrame *frame = ist->sub2video.frame;
     int8_t *dst;
     int     dst_linesize;
     int num_rects, i;
     int64_t pts, end_pts;
 
-    if (!ref)
+    if (!frame)
         return;
     if (sub) {
         pts       = av_rescale_q(sub->pts + sub->start_display_time * 1000,
@@ -234,9 +247,13 @@
         end_pts   = INT64_MAX;
         num_rects = 0;
     }
-    dst          = ref->data    [0];
-    dst_linesize = ref->linesize[0];
-    memset(dst, 0, h * dst_linesize);
+    if (sub2video_get_blank_frame(ist) < 0) {
+        av_log(ist->st->codec, AV_LOG_ERROR,
+               "Impossible to get a blank canvas.\n");
+        return;
+    }
+    dst          = frame->data    [0];
+    dst_linesize = frame->linesize[0];
     for (i = 0; i < num_rects; i++)
         sub2video_copy_rect(dst, dst_linesize, w, h, sub->rects[i]);
     sub2video_push_ref(ist, pts);
@@ -255,7 +272,7 @@
        (possibly overlay) is desperately waiting for a subtitle frame. */
     for (i = 0; i < infile->nb_streams; i++) {
         InputStream *ist2 = input_streams[infile->ist_index + i];
-        if (!ist2->sub2video.ref)
+        if (!ist2->sub2video.frame)
             continue;
         /* subtitles seem to be usually muxed ahead of other streams;
            if not, substracting a larger time here is necessary */
@@ -263,7 +280,7 @@
         /* do not send the heartbeat frame if the subtitle is already ahead */
         if (pts2 <= ist2->sub2video.last_pts)
             continue;
-        if (pts2 >= ist2->sub2video.end_pts)
+        if (pts2 >= ist2->sub2video.end_pts || !ist2->sub2video.frame->data[0])
             sub2video_update(ist2, NULL);
         for (j = 0, nb_reqs = 0; j < ist2->nb_filters; j++)
             nb_reqs += av_buffersrc_get_nb_failed_requests(ist2->filters[j]->filter);
@@ -408,6 +425,11 @@
 {
     int i, j;
 
+    if (do_benchmark) {
+        int maxrss = getmaxrss() / 1024;
+        printf("bench: maxrss=%ikB\n", maxrss);
+    }
+
     for (i = 0; i < nb_filtergraphs; i++) {
         avfilter_graph_free(&filtergraphs[i]->graph);
         for (j = 0; j < filtergraphs[i]->nb_inputs; j++) {
@@ -420,6 +442,7 @@
             av_freep(&filtergraphs[i]->outputs[j]);
         }
         av_freep(&filtergraphs[i]->outputs);
+        av_freep(&filtergraphs[i]->graph_desc);
         av_freep(&filtergraphs[i]);
     }
     av_freep(&filtergraphs);
@@ -456,11 +479,11 @@
         av_freep(&input_files[i]);
     }
     for (i = 0; i < nb_input_streams; i++) {
-        avcodec_free_frame(&input_streams[i]->decoded_frame);
+        av_frame_free(&input_streams[i]->decoded_frame);
+        av_frame_free(&input_streams[i]->filter_frame);
         av_dict_free(&input_streams[i]->opts);
-        free_buffer_pool(&input_streams[i]->buffer_pool);
         avsubtitle_free(&input_streams[i]->prev_sub.subtitle);
-        avfilter_unref_bufferp(&input_streams[i]->sub2video.ref);
+        av_frame_free(&input_streams[i]->sub2video.frame);
         av_freep(&input_streams[i]->filters);
         av_freep(&input_streams[i]);
     }
@@ -476,7 +499,6 @@
 
     uninit_opts();
 
-    avfilter_uninit();
     avformat_network_deinit();
 
     if (received_sigterm) {
@@ -564,13 +586,17 @@
                 memcpy(t, new_pkt.data, new_pkt.size);
                 memset(t + new_pkt.size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
                 new_pkt.data = t;
+                new_pkt.buf = NULL;
                 a = 1;
             } else
                 a = AVERROR(ENOMEM);
         }
         if (a > 0) {
             av_free_packet(pkt);
-            new_pkt.destruct = av_destruct_packet;
+            new_pkt.buf = av_buffer_create(new_pkt.data, new_pkt.size,
+                                           av_buffer_default_free, NULL, 0);
+            if (!new_pkt.buf)
+                exit(1);
         } else if (a < 0) {
             av_log(NULL, AV_LOG_ERROR, "Failed to open bitstream filter %s for stream %d with codec %s",
                    bsfc->filter->name, pkt->stream_index,
@@ -675,47 +701,6 @@
     }
 }
 
-static void pre_process_video_frame(InputStream *ist, AVPicture *picture, void **bufp)
-{
-    AVCodecContext *dec;
-    AVPicture *picture2;
-    AVPicture picture_tmp;
-    uint8_t *buf = 0;
-
-    dec = ist->st->codec;
-
-    /* deinterlace : must be done before any resize */
-    if (do_deinterlace) {
-        int size;
-
-        /* create temporary picture */
-        size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
-        if (size < 0)
-            return;
-        buf  = av_malloc(size);
-        if (!buf)
-            return;
-
-        picture2 = &picture_tmp;
-        avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height);
-
-        if (avpicture_deinterlace(picture2, picture,
-                                 dec->pix_fmt, dec->width, dec->height) < 0) {
-            /* if error, do not deinterlace */
-            av_log(NULL, AV_LOG_WARNING, "Deinterlacing failed\n");
-            av_free(buf);
-            buf = NULL;
-            picture2 = picture;
-        }
-    } else {
-        picture2 = picture;
-    }
-
-    if (picture != picture2)
-        *picture = *picture2;
-    *bufp = buf;
-}
-
 static void do_subtitle_out(AVFormatContext *s,
                             OutputStream *ost,
                             InputStream *ist,
@@ -884,36 +869,28 @@
         write_frame(s, &pkt, ost);
     } else {
         int got_packet, forced_keyframe = 0;
-        AVFrame big_picture;
         double pts_time;
 
-        big_picture = *in_picture;
-        /* better than nothing: use input picture interlaced
-           settings */
-        big_picture.interlaced_frame = in_picture->interlaced_frame;
-        if (ost->st->codec->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)) {
-            if (ost->top_field_first == -1)
-                big_picture.top_field_first = in_picture->top_field_first;
-            else
-                big_picture.top_field_first = !!ost->top_field_first;
-        }
+        if (ost->st->codec->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME) &&
+            ost->top_field_first >= 0)
+            in_picture->top_field_first = !!ost->top_field_first;
 
-        if (big_picture.interlaced_frame) {
+        if (in_picture->interlaced_frame) {
             if (enc->codec->id == AV_CODEC_ID_MJPEG)
-                enc->field_order = big_picture.top_field_first ? AV_FIELD_TT:AV_FIELD_BB;
+                enc->field_order = in_picture->top_field_first ? AV_FIELD_TT:AV_FIELD_BB;
             else
-                enc->field_order = big_picture.top_field_first ? AV_FIELD_TB:AV_FIELD_BT;
+                enc->field_order = in_picture->top_field_first ? AV_FIELD_TB:AV_FIELD_BT;
         } else
             enc->field_order = AV_FIELD_PROGRESSIVE;
 
-        big_picture.quality = ost->st->codec->global_quality;
+        in_picture->quality = ost->st->codec->global_quality;
         if (!enc->me_threshold)
-            big_picture.pict_type = 0;
+            in_picture->pict_type = 0;
 
-        pts_time = big_picture.pts != AV_NOPTS_VALUE ?
-            big_picture.pts * av_q2d(enc->time_base) : NAN;
+        pts_time = in_picture->pts != AV_NOPTS_VALUE ?
+            in_picture->pts * av_q2d(enc->time_base) : NAN;
         if (ost->forced_kf_index < ost->forced_kf_count &&
-            big_picture.pts >= ost->forced_kf_pts[ost->forced_kf_index]) {
+            in_picture->pts >= ost->forced_kf_pts[ost->forced_kf_index]) {
             ost->forced_kf_index++;
             forced_keyframe = 1;
         } else if (ost->forced_keyframes_pexpr) {
@@ -940,12 +917,12 @@
             ost->forced_keyframes_expr_const_values[FKF_N] += 1;
         }
         if (forced_keyframe) {
-            big_picture.pict_type = AV_PICTURE_TYPE_I;
+            in_picture->pict_type = AV_PICTURE_TYPE_I;
             av_log(NULL, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time);
         }
 
         update_benchmark(NULL);
-        ret = avcodec_encode_video2(enc, &pkt, &big_picture, &got_packet);
+        ret = avcodec_encode_video2(enc, &pkt, in_picture, &got_packet);
         update_benchmark("encode_video %d.%d", ost->file_index, ost->index);
         if (ret < 0) {
             av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
@@ -986,10 +963,10 @@
      * flush, we need to limit them here, before they go into encoder.
      */
     ost->frame_number++;
-  }
 
     if (vstats_filename && frame_size)
         do_video_stats(ost, frame_size);
+  }
 }
 
 static double psnr(double d)
@@ -1041,7 +1018,6 @@
  */
 static int reap_filters(void)
 {
-    AVFilterBufferRef *picref;
     AVFrame *filtered_frame = NULL;
     int i;
     int64_t frame_pts;
@@ -1062,20 +1038,18 @@
         filtered_frame = ost->filtered_frame;
 
         while (1) {
-            ret = av_buffersink_get_buffer_ref(ost->filter->filter, &picref,
+            ret = av_buffersink_get_frame_flags(ost->filter->filter, filtered_frame,
                                                AV_BUFFERSINK_FLAG_NO_REQUEST);
             if (ret < 0) {
                 if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
-                    char buf[256];
-                    av_strerror(ret, buf, sizeof(buf));
                     av_log(NULL, AV_LOG_WARNING,
-                           "Error in av_buffersink_get_buffer_ref(): %s\n", buf);
+                           "Error in av_buffersink_get_frame_flags(): %s\n", av_err2str(ret));
                 }
                 break;
             }
             frame_pts = AV_NOPTS_VALUE;
-            if (picref->pts != AV_NOPTS_VALUE) {
-                filtered_frame->pts = frame_pts = av_rescale_q(picref->pts,
+            if (filtered_frame->pts != AV_NOPTS_VALUE) {
+                filtered_frame->pts = frame_pts = av_rescale_q(filtered_frame->pts,
                                                 ost->filter->filter->inputs[0]->time_base,
                                                 ost->st->codec->time_base) -
                                     av_rescale_q(of->start_time,
@@ -1083,7 +1057,7 @@
                                                 ost->st->codec->time_base);
 
                 if (of->start_time && filtered_frame->pts < 0) {
-                    avfilter_unref_buffer(picref);
+                    av_frame_unref(filtered_frame);
                     continue;
                 }
             }
@@ -1093,15 +1067,13 @@
 
             switch (ost->filter->filter->inputs[0]->type) {
             case AVMEDIA_TYPE_VIDEO:
-                avfilter_copy_buf_props(filtered_frame, picref);
                 filtered_frame->pts = frame_pts;
-                if (!ost->frame_aspect_ratio)
-                    ost->st->codec->sample_aspect_ratio = picref->video->sample_aspect_ratio;
+                if (!ost->frame_aspect_ratio.num)
+                    ost->st->codec->sample_aspect_ratio = filtered_frame->sample_aspect_ratio;
 
                 do_video_out(of->ctx, ost, filtered_frame);
                 break;
             case AVMEDIA_TYPE_AUDIO:
-                avfilter_copy_buf_props(filtered_frame, picref);
                 filtered_frame->pts = frame_pts;
                 if (!(ost->st->codec->codec->capabilities & CODEC_CAP_PARAM_CHANGE) &&
                     ost->st->codec->channels != av_frame_get_channels(filtered_frame)) {
@@ -1116,7 +1088,7 @@
                 av_assert0(0);
             }
 
-            avfilter_unref_buffer(picref);
+            av_frame_unref(filtered_frame);
         }
     }
 
@@ -1263,7 +1235,10 @@
     av_bprintf(&buf_script, "drop_frames=%d\n", nb_frames_drop);
 
     if (print_stats || is_last_report) {
-    av_log(NULL, AV_LOG_INFO, "%s    \r", buf);
+        if (print_stats==1 && AV_LOG_INFO > av_log_get_level()) {
+            fprintf(stderr, "%s    \r", buf);
+        } else
+            av_log(NULL, AV_LOG_INFO, "%s    \r", buf);
 
     fflush(stderr);
     }
@@ -1454,8 +1429,11 @@
        && ost->st->codec->codec_id != AV_CODEC_ID_MPEG2VIDEO
        && ost->st->codec->codec_id != AV_CODEC_ID_VC1
        ) {
-        if (av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, pkt->data, pkt->size, pkt->flags & AV_PKT_FLAG_KEY))
-            opkt.destruct = av_destruct_packet;
+        if (av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, pkt->data, pkt->size, pkt->flags & AV_PKT_FLAG_KEY)) {
+            opkt.buf = av_buffer_create(opkt.data, opkt.size, av_buffer_default_free, NULL, 0);
+            if (!opkt.buf)
+                exit(1);
+        }
     } else {
         opkt.data = pkt->data;
         opkt.size = pkt->size;
@@ -1505,13 +1483,15 @@
 
 static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output)
 {
-    AVFrame *decoded_frame;
+    AVFrame *decoded_frame, *f;
     AVCodecContext *avctx = ist->st->codec;
-    int i, ret, resample_changed;
+    int i, ret, err = 0, resample_changed;
     AVRational decoded_frame_tb;
 
     if (!ist->decoded_frame && !(ist->decoded_frame = avcodec_alloc_frame()))
         return AVERROR(ENOMEM);
+    if (!ist->filter_frame && !(ist->filter_frame = av_frame_alloc()))
+        return AVERROR(ENOMEM);
     decoded_frame = ist->decoded_frame;
 
     update_benchmark(NULL);
@@ -1523,10 +1503,17 @@
         ret = AVERROR_INVALIDDATA;
     }
 
+    if (*got_output || ret<0 || pkt->size)
+        decode_error_stat[ret<0] ++;
+
     if (!*got_output || ret < 0) {
         if (!pkt->size) {
             for (i = 0; i < ist->nb_filters; i++)
+#if 1
                 av_buffersrc_add_ref(ist->filters[i]->filter, NULL, 0);
+#else
+                av_buffersrc_add_frame(ist->filters[i]->filter, NULL);
+#endif
         }
         return ret;
     }
@@ -1614,24 +1601,37 @@
         decoded_frame->pts = av_rescale_delta(decoded_frame_tb, decoded_frame->pts,
                                               (AVRational){1, ist->st->codec->sample_rate}, decoded_frame->nb_samples, &ist->filter_in_rescale_delta_last,
                                               (AVRational){1, ist->st->codec->sample_rate});
-    for (i = 0; i < ist->nb_filters; i++)
-        av_buffersrc_add_frame(ist->filters[i]->filter, decoded_frame,
-                               AV_BUFFERSRC_FLAG_PUSH);
-
+    for (i = 0; i < ist->nb_filters; i++) {
+        if (i < ist->nb_filters - 1) {
+            f = ist->filter_frame;
+            err = av_frame_ref(f, decoded_frame);
+            if (err < 0)
+                break;
+        } else
+            f = decoded_frame;
+        err = av_buffersrc_add_frame_flags(ist->filters[i]->filter, f,
+                                     AV_BUFFERSRC_FLAG_PUSH);
+        if (err < 0)
+            break;
+    }
     decoded_frame->pts = AV_NOPTS_VALUE;
 
-    return ret;
+    av_frame_unref(ist->filter_frame);
+    av_frame_unref(decoded_frame);
+    return err < 0 ? err : ret;
 }
 
 static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output)
 {
-    AVFrame *decoded_frame;
+    AVFrame *decoded_frame, *f;
     void *buffer_to_free = NULL;
-    int i, ret = 0, resample_changed;
+    int i, ret = 0, err = 0, resample_changed;
     int64_t best_effort_timestamp;
     AVRational *frame_sample_aspect;
 
-    if (!ist->decoded_frame && !(ist->decoded_frame = avcodec_alloc_frame()))
+    if (!ist->decoded_frame && !(ist->decoded_frame = av_frame_alloc()))
+        return AVERROR(ENOMEM);
+    if (!ist->filter_frame && !(ist->filter_frame = av_frame_alloc()))
         return AVERROR(ENOMEM);
     decoded_frame = ist->decoded_frame;
     pkt->dts  = av_rescale_q(ist->dts, AV_TIME_BASE_Q, ist->st->time_base);
@@ -1640,10 +1640,18 @@
     ret = avcodec_decode_video2(ist->st->codec,
                                 decoded_frame, got_output, pkt);
     update_benchmark("decode_video %d.%d", ist->file_index, ist->st->index);
+
+    if (*got_output || ret<0 || pkt->size)
+        decode_error_stat[ret<0] ++;
+
     if (!*got_output || ret < 0) {
         if (!pkt->size) {
             for (i = 0; i < ist->nb_filters; i++)
+#if 1
                 av_buffersrc_add_ref(ist->filters[i]->filter, NULL, 0);
+#else
+                av_buffersrc_add_frame(ist->filters[i]->filter, NULL);
+#endif
         }
         return ret;
     }
@@ -1666,7 +1674,6 @@
     }
 
     pkt->size = 0;
-    pre_process_video_frame(ist, (AVPicture *)decoded_frame, &buffer_to_free);
 
     rate_emu_sleep(ist);
 
@@ -1698,40 +1705,28 @@
 
     frame_sample_aspect= av_opt_ptr(avcodec_get_frame_class(), decoded_frame, "sample_aspect_ratio");
     for (i = 0; i < ist->nb_filters; i++) {
-        int changed =      ist->st->codec->width   != ist->filters[i]->filter->outputs[0]->w
-                        || ist->st->codec->height  != ist->filters[i]->filter->outputs[0]->h
-                        || ist->st->codec->pix_fmt != ist->filters[i]->filter->outputs[0]->format;
-
         if (!frame_sample_aspect->num)
             *frame_sample_aspect = ist->st->sample_aspect_ratio;
-        if (ist->dr1 && decoded_frame->type==FF_BUFFER_TYPE_USER && !changed) {
-            FrameBuffer      *buf = decoded_frame->opaque;
-            AVFilterBufferRef *fb = avfilter_get_video_buffer_ref_from_arrays(
-                                        decoded_frame->data, decoded_frame->linesize,
-                                        AV_PERM_READ | AV_PERM_PRESERVE,
-                                        ist->st->codec->width, ist->st->codec->height,
-                                        ist->st->codec->pix_fmt);
 
-            avfilter_copy_frame_props(fb, decoded_frame);
-            fb->buf->priv           = buf;
-            fb->buf->free           = filter_release_buffer;
-
-            av_assert0(buf->refcount>0);
-            buf->refcount++;
-            av_buffersrc_add_ref(ist->filters[i]->filter, fb,
-                                 AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT |
-                                 AV_BUFFERSRC_FLAG_NO_COPY |
-                                 AV_BUFFERSRC_FLAG_PUSH);
+        if (i < ist->nb_filters - 1) {
+            f = ist->filter_frame;
+            err = av_frame_ref(f, decoded_frame);
+            if (err < 0)
+                break;
         } else
-        if(av_buffersrc_add_frame(ist->filters[i]->filter, decoded_frame, AV_BUFFERSRC_FLAG_PUSH)<0) {
-            av_log(NULL, AV_LOG_FATAL, "Failed to inject frame into filter network\n");
+            f = decoded_frame;
+        ret = av_buffersrc_add_frame_flags(ist->filters[i]->filter, f, AV_BUFFERSRC_FLAG_PUSH);
+        if (ret < 0) {
+            av_log(NULL, AV_LOG_FATAL,
+                   "Failed to inject frame into filter network: %s\n", av_err2str(ret));
             exit(1);
         }
-
     }
 
+    av_frame_unref(ist->filter_frame);
+    av_frame_unref(decoded_frame);
     av_free(buffer_to_free);
-    return ret;
+    return err < 0 ? err : ret;
 }
 
 static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
@@ -1739,6 +1734,10 @@
     AVSubtitle subtitle;
     int i, ret = avcodec_decode_subtitle2(ist->st->codec,
                                           &subtitle, got_output, pkt);
+
+    if (*got_output || ret<0 || pkt->size)
+        decode_error_stat[ret<0] ++;
+
     if (ret < 0 || !*got_output) {
         if (!pkt->size)
             sub2video_flush(ist);
@@ -1785,7 +1784,7 @@
 static int output_packet(InputStream *ist, const AVPacket *pkt)
 {
     int ret = 0, i;
-    int got_output;
+    int got_output = 0;
 
     AVPacket avpkt;
     if (!ist->saw_first_ts) {
@@ -1892,7 +1891,12 @@
                              ist->st->codec->sample_rate;
             break;
         case AVMEDIA_TYPE_VIDEO:
-            if (pkt->duration) {
+            if (ist->framerate.num) {
+                // TODO: Remove work-around for c99-to-c89 issue 7
+                AVRational time_base_q = AV_TIME_BASE_Q;
+                int64_t next_dts = av_rescale_q(ist->next_dts, time_base_q, av_inv_q(ist->framerate));
+                ist->next_dts = av_rescale_q(next_dts + 1, av_inv_q(ist->framerate), time_base_q);
+            } else if (pkt->duration) {
                 ist->next_dts += av_rescale_q(pkt->duration, ist->st->time_base, AV_TIME_BASE_Q);
             } else if(ist->st->codec->time_base.num != 0) {
                 int ticks= ist->st->parser ? ist->st->parser->repeat_pict + 1 : ist->st->codec->ticks_per_frame;
@@ -1947,12 +1951,7 @@
             return AVERROR(EINVAL);
         }
 
-        ist->dr1 = (codec->capabilities & CODEC_CAP_DR1) && !do_deinterlace;
-        if (codec->type == AVMEDIA_TYPE_VIDEO && ist->dr1) {
-            ist->st->codec->get_buffer     = codec_get_buffer;
-            ist->st->codec->release_buffer = codec_release_buffer;
-            ist->st->codec->opaque         = &ist->buffer_pool;
-        }
+        av_opt_set_int(ist->st->codec, "refcounted_frames", 1, 0);
 
         if (!av_dict_get(ist->opts, "threads", NULL, 0))
             av_dict_set(&ist->opts, "threads", "auto", 0);
@@ -2195,6 +2194,8 @@
                 codec->time_base = icodec->time_base;
             }
 
+            if (ist && !ost->frame_rate.num)
+                ost->frame_rate = ist->framerate;
             if(ost->frame_rate.num)
                 codec->time_base = av_inv_q(ost->frame_rate);
 
@@ -2223,7 +2224,14 @@
                 codec->width              = icodec->width;
                 codec->height             = icodec->height;
                 codec->has_b_frames       = icodec->has_b_frames;
-                if (!codec->sample_aspect_ratio.num) {
+                if (ost->frame_aspect_ratio.num) { // overridden by the -aspect cli option
+                    codec->sample_aspect_ratio   =
+                    ost->st->sample_aspect_ratio =
+                        av_mul_q(ost->frame_aspect_ratio,
+                                 (AVRational){ codec->height, codec->width });
+                    av_log(NULL, AV_LOG_WARNING, "Overriding aspect ratio "
+                           "with stream copy may produce invalid files\n");
+                } else if (!codec->sample_aspect_ratio.num) {
                     codec->sample_aspect_ratio   =
                     ost->st->sample_aspect_ratio =
                         ist->st->sample_aspect_ratio.num ? ist->st->sample_aspect_ratio :
@@ -2307,9 +2315,16 @@
                 codec->width  = ost->filter->filter->inputs[0]->w;
                 codec->height = ost->filter->filter->inputs[0]->h;
                 codec->sample_aspect_ratio = ost->st->sample_aspect_ratio =
-                    ost->frame_aspect_ratio ? // overridden by the -aspect cli option
-                    av_d2q(ost->frame_aspect_ratio * codec->height/codec->width, 255) :
+                    ost->frame_aspect_ratio.num ? // overridden by the -aspect cli option
+                    av_mul_q(ost->frame_aspect_ratio, (AVRational){ codec->height, codec->width }) :
                     ost->filter->filter->inputs[0]->sample_aspect_ratio;
+                if (!strncmp(ost->enc->name, "libx264", 7) &&
+                    codec->pix_fmt == AV_PIX_FMT_NONE &&
+                    ost->filter->filter->inputs[0]->format != AV_PIX_FMT_YUV420P)
+                    av_log(NULL, AV_LOG_INFO,
+                           "No pixel format specified, %s for H.264 encoding chosen.\n"
+                           "Use -pix_fmt yuv420p for compatibility with outdated media players.\n",
+                           av_get_pix_fmt_name(ost->filter->filter->inputs[0]->format));
                 codec->pix_fmt = ost->filter->filter->inputs[0]->format;
 
                 if (!icodec ||
@@ -2431,8 +2446,13 @@
 
     /* init input streams */
     for (i = 0; i < nb_input_streams; i++)
-        if ((ret = init_input_stream(i, error, sizeof(error))) < 0)
+        if ((ret = init_input_stream(i, error, sizeof(error))) < 0) {
+            for (i = 0; i < nb_output_streams; i++) {
+                ost = output_streams[i];
+                avcodec_close(ost->st->codec);
+            }
             goto dump_format;
+        }
 
     /* discard unused programs */
     for (i = 0; i < nb_input_files; i++) {
@@ -2945,6 +2965,23 @@
     if (pkt.dts != AV_NOPTS_VALUE)
         pkt.dts *= ist->ts_scale;
 
+    if (pkt.dts != AV_NOPTS_VALUE && ist->next_dts == AV_NOPTS_VALUE && !copy_ts
+        && (is->iformat->flags & AVFMT_TS_DISCONT) && ifile->last_ts != AV_NOPTS_VALUE) {
+        int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
+        int64_t delta   = pkt_dts - ifile->last_ts;
+        if(delta < -1LL*dts_delta_threshold*AV_TIME_BASE ||
+            (delta > 1LL*dts_delta_threshold*AV_TIME_BASE &&
+                ist->st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE)){
+            ifile->ts_offset -= delta;
+            av_log(NULL, AV_LOG_DEBUG,
+                   "Inter stream timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
+                   delta, ifile->ts_offset);
+            pkt.dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
+            if (pkt.pts != AV_NOPTS_VALUE)
+                pkt.pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
+        }
+    }
+
     if (pkt.dts != AV_NOPTS_VALUE && ist->next_dts != AV_NOPTS_VALUE &&
         !copy_ts) {
         int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
@@ -2982,6 +3019,9 @@
         }
     }
 
+    if (pkt.dts != AV_NOPTS_VALUE)
+        ifile->last_ts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
+
     if (debug_ts) {
         av_log(NULL, AV_LOG_INFO, "demuxer+ffmpeg -> ist_index:%d type:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s off:%s off_time:%s\n",
                ifile->ist_index + pkt.stream_index, av_get_media_type_string(ist->st->codec->codec_type),
@@ -3322,9 +3362,12 @@
         exit(1);
     ti = getutime() - ti;
     if (do_benchmark) {
-        int maxrss = getmaxrss() / 1024;
-        printf("bench: utime=%0.3fs maxrss=%ikB\n", ti / 1000000.0, maxrss);
+        printf("bench: utime=%0.3fs\n", ti / 1000000.0);
     }
+    av_log(NULL, AV_LOG_DEBUG, "%"PRIu64" frames successfully decoded, %"PRIu64" decoding errors\n",
+           decode_error_stat[0], decode_error_stat[1]);
+    if (2*decode_error_stat[0] < decode_error_stat[1])
+        exit(254);
 
     exit(received_nb_signals ? 255 : 0);
     return 0;
diff --git a/ffmpeg.h b/ffmpeg.h
index d7046fd..3641a62 100644
--- a/ffmpeg.h
+++ b/ffmpeg.h
@@ -37,7 +37,6 @@
 #include "libavcodec/avcodec.h"
 
 #include "libavfilter/avfilter.h"
-#include "libavfilter/avfiltergraph.h"
 
 #include "libavutil/avutil.h"
 #include "libavutil/dict.h"
@@ -165,10 +164,14 @@
     int        nb_copy_prior_start;
     SpecifierOpt *filters;
     int        nb_filters;
+    SpecifierOpt *filter_scripts;
+    int        nb_filter_scripts;
     SpecifierOpt *reinit_filters;
     int        nb_reinit_filters;
     SpecifierOpt *fix_sub_duration;
     int        nb_fix_sub_duration;
+    SpecifierOpt *canvas_sizes;
+    int        nb_canvas_sizes;
     SpecifierOpt *pass;
     int        nb_pass;
     SpecifierOpt *passlogfiles;
@@ -214,6 +217,7 @@
     int decoding_needed;     /* true if the packets must be decoded in 'raw_fifo' */
     AVCodec *dec;
     AVFrame *decoded_frame;
+    AVFrame *filter_frame; /* a ref of decoded_frame, to be sent to filters */
 
     int64_t       start;     /* time when read started */
     /* predicted dts of the next packet read for this stream or (when there are
@@ -255,12 +259,10 @@
     struct sub2video {
         int64_t last_pts;
         int64_t end_pts;
-        AVFilterBufferRef *ref;
+        AVFrame *frame;
         int w, h;
     } sub2video;
 
-    /* a pool of free buffers for decoded data */
-    FrameBuffer *buffer_pool;
     int dr1;
 
     /* decoded data from this stream goes into all those filters
@@ -277,6 +279,7 @@
     int eagain;           /* true if last read attempt returned EAGAIN */
     int ist_index;        /* index of first stream in input_streams */
     int64_t ts_offset;
+    int64_t last_ts;
     int nb_streams;       /* number of stream that ffmpeg is aware of; may be different
                              from ctx.nb_streams if new streams appear during av_read_frame() */
     int nb_streams_warn;  /* number of streams that the user was warned of */
@@ -327,7 +330,7 @@
     int force_fps;
     int top_field_first;
 
-    float frame_aspect_ratio;
+    AVRational frame_aspect_ratio;
 
     /* forced key frames */
     int64_t *forced_kf_pts;
diff --git a/ffmpeg_filter.c b/ffmpeg_filter.c
index 1919f78..e7e7242 100644
--- a/ffmpeg_filter.c
+++ b/ffmpeg_filter.c
@@ -21,7 +21,6 @@
 #include "ffmpeg.h"
 
 #include "libavfilter/avfilter.h"
-#include "libavfilter/avfiltergraph.h"
 #include "libavfilter/buffersink.h"
 
 #include "libavresample/avresample.h"
@@ -124,7 +123,7 @@
 
         for (; *p != AV_PIX_FMT_NONE; p++) {
             const char *name = av_get_pix_fmt_name(*p);
-            avio_printf(s, "%s:", name);
+            avio_printf(s, "%s|", name);
         }
         len = avio_close_dyn_buf(s, &ret);
         ret[len - 1] = 0;
@@ -135,13 +134,13 @@
 
 /* Define a function for building a string containing a list of
  * allowed formats. */
-#define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name, separator)\
+#define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name)           \
 static char *choose_ ## var ## s(OutputStream *ost)                            \
 {                                                                              \
     if (ost->st->codec->var != none) {                                         \
         get_name(ost->st->codec->var);                                         \
         return av_strdup(name);                                                \
-    } else if (ost->enc->supported_list) {                                     \
+    } else if (ost->enc && ost->enc->supported_list) {                         \
         const type *p;                                                         \
         AVIOContext *s = NULL;                                                 \
         uint8_t *ret;                                                          \
@@ -152,7 +151,7 @@
                                                                                \
         for (p = ost->enc->supported_list; *p != none; p++) {                  \
             get_name(*p);                                                      \
-            avio_printf(s, "%s" separator, name);                              \
+            avio_printf(s, "%s|", name);                                       \
         }                                                                      \
         len = avio_close_dyn_buf(s, &ret);                                     \
         ret[len - 1] = 0;                                                      \
@@ -162,16 +161,16 @@
 }
 
 // DEF_CHOOSE_FORMAT(enum AVPixelFormat, pix_fmt, pix_fmts, AV_PIX_FMT_NONE,
-//                   GET_PIX_FMT_NAME, ":")
+//                   GET_PIX_FMT_NAME)
 
 DEF_CHOOSE_FORMAT(enum AVSampleFormat, sample_fmt, sample_fmts,
-                  AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME, ",")
+                  AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME)
 
 DEF_CHOOSE_FORMAT(int, sample_rate, supported_samplerates, 0,
-                  GET_SAMPLE_RATE_NAME, ",")
+                  GET_SAMPLE_RATE_NAME)
 
 DEF_CHOOSE_FORMAT(uint64_t, channel_layout, channel_layouts, 0,
-                  GET_CH_LAYOUT_NAME, ",")
+                  GET_CH_LAYOUT_NAME)
 
 FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost)
 {
@@ -286,13 +285,11 @@
     int pad_idx = out->pad_idx;
     int ret;
     char name[255];
-    AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
 
     snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
     ret = avfilter_graph_create_filter(&ofilter->filter,
-                                       avfilter_get_by_name("ffbuffersink"),
+                                       avfilter_get_by_name("buffersink"),
                                        name, NULL, NULL, fg->graph);
-    av_freep(&buffersink_params);
 
     if (ret < 0)
         return ret;
@@ -321,17 +318,18 @@
         AVFilterContext *filter;
         snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
                  ost->file_index, ost->index);
-        if ((ret = avfilter_graph_create_filter(&filter,
+        ret = avfilter_graph_create_filter(&filter,
                                                 avfilter_get_by_name("format"),
                                                 "format", pix_fmts, NULL,
-                                                fg->graph)) < 0)
+                                                fg->graph);
+        av_freep(&pix_fmts);
+        if (ret < 0)
             return ret;
         if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
             return ret;
 
         last_filter = filter;
         pad_idx     = 0;
-        av_freep(&pix_fmts);
     }
 
     if (ost->frame_rate.num && 0) {
@@ -369,18 +367,15 @@
     char *sample_fmts, *sample_rates, *channel_layouts;
     char name[255];
     int ret;
-    AVABufferSinkParams *params = av_abuffersink_params_alloc();
 
-    if (!params)
-        return AVERROR(ENOMEM);
-    params->all_channel_counts = 1;
     snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
     ret = avfilter_graph_create_filter(&ofilter->filter,
-                                       avfilter_get_by_name("ffabuffersink"),
-                                       name, NULL, params, fg->graph);
-    av_freep(&params);
+                                       avfilter_get_by_name("abuffersink"),
+                                       name, NULL, NULL, fg->graph);
     if (ret < 0)
         return ret;
+    if ((ret = av_opt_set_int(ofilter->filter, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
+        return ret;
 
 #define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do {                 \
     AVFilterContext *filt_ctx;                                              \
@@ -501,9 +496,7 @@
 static int sub2video_prepare(InputStream *ist)
 {
     AVFormatContext *avf = input_files[ist->file_index]->ctx;
-    int i, ret, w, h;
-    uint8_t *image[4];
-    int linesize[4];
+    int i, w, h;
 
     /* Compute the size of the canvas for the subtitles stream.
        If the subtitles codec has set a size, use it. Otherwise use the
@@ -530,17 +523,9 @@
        palettes for all rectangles are identical or compatible */
     ist->resample_pix_fmt = ist->st->codec->pix_fmt = AV_PIX_FMT_RGB32;
 
-    ret = av_image_alloc(image, linesize, w, h, AV_PIX_FMT_RGB32, 32);
-    if (ret < 0)
-        return ret;
-    memset(image[0], 0, h * linesize[0]);
-    ist->sub2video.ref = avfilter_get_video_buffer_ref_from_arrays(
-            image, linesize, AV_PERM_READ | AV_PERM_PRESERVE,
-            w, h, AV_PIX_FMT_RGB32);
-    if (!ist->sub2video.ref) {
-        av_free(image[0]);
+    ist->sub2video.frame = av_frame_alloc();
+    if (!ist->sub2video.frame)
         return AVERROR(ENOMEM);
-    }
     return 0;
 }
 
@@ -552,22 +537,15 @@
     InputStream *ist = ifilter->ist;
     AVRational tb = ist->framerate.num ? av_inv_q(ist->framerate) :
                                          ist->st->time_base;
-    AVRational fr = ist->framerate.num ? ist->framerate :
-                                         ist->st->r_frame_rate;
+    AVRational fr = ist->framerate;
     AVRational sar;
     AVBPrint args;
     char name[255];
     int pad_idx = in->pad_idx;
     int ret;
 
-    if (!ist->framerate.num && ist->st->codec->ticks_per_frame>1) {
-        AVRational codec_fr = av_inv_q(ist->st->codec->time_base);
-        AVRational   avg_fr = ist->st->avg_frame_rate;
-        codec_fr.den *= ist->st->codec->ticks_per_frame;
-        if (   codec_fr.num>0 && codec_fr.den>0 && av_q2d(codec_fr) < av_q2d(fr)*0.7
-            && fabs(1.0 - av_q2d(av_div_q(avg_fr, fr)))>0.1)
-            fr = codec_fr;
-    }
+    if (!fr.num)
+        fr = av_guess_frame_rate(input_files[ist->file_index]->ctx, ist->st, NULL);
 
     if (ist->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
         ret = sub2video_prepare(ist);
@@ -614,6 +592,24 @@
         pad_idx = 0;
     }
 
+    if (do_deinterlace) {
+        AVFilterContext *yadif;
+
+        snprintf(name, sizeof(name), "deinterlace input from stream %d:%d",
+                 ist->file_index, ist->st->index);
+        if ((ret = avfilter_graph_create_filter(&yadif,
+                                                avfilter_get_by_name("yadif"),
+                                                name, "", NULL,
+                                                fg->graph)) < 0)
+            return ret;
+
+        if ((ret = avfilter_link(yadif, 0, first_filter, pad_idx)) < 0)
+            return ret;
+
+        first_filter = yadif;
+        pad_idx = 0;
+    }
+
     if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
         return ret;
     return 0;
diff --git a/ffmpeg_opt.c b/ffmpeg_opt.c
index 22d6e41..f03f29b 100644
--- a/ffmpeg_opt.c
+++ b/ffmpeg_opt.c
@@ -28,7 +28,6 @@
 #include "libavcodec/avcodec.h"
 
 #include "libavfilter/avfilter.h"
-#include "libavfilter/avfiltergraph.h"
 
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
@@ -81,7 +80,7 @@
 int copy_tb           = -1;
 int debug_ts          = 0;
 int exit_on_error     = 0;
-int print_stats       = 1;
+int print_stats       = -1;
 int qp_hist           = 0;
 int stdin_interaction = 1;
 int frame_bits_per_raw_sample = 0;
@@ -151,6 +150,24 @@
     o->chapters_input_file = INT_MAX;
 }
 
+/* return a copy of the input with the stream specifiers removed from the keys */
+static AVDictionary *strip_specifiers(AVDictionary *dict)
+{
+    AVDictionaryEntry *e = NULL;
+    AVDictionary    *ret = NULL;
+
+    while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX))) {
+        char *p = strchr(e->key, ':');
+
+        if (p)
+            *p = 0;
+        av_dict_set(&ret, e->key, e->value, 0);
+        if (p)
+            *p = ':';
+    }
+    return ret;
+}
+
 static int opt_sameq(void *optctx, const char *opt, const char *arg)
 {
     av_log(NULL, AV_LOG_ERROR, "Option '%s' was removed. "
@@ -617,11 +634,19 @@
 
             break;
         case AVMEDIA_TYPE_DATA:
-        case AVMEDIA_TYPE_SUBTITLE:
+        case AVMEDIA_TYPE_SUBTITLE: {
+            char *canvas_size = NULL;
             if(!ist->dec)
                 ist->dec = avcodec_find_decoder(dec->codec_id);
             MATCH_PER_STREAM_OPT(fix_sub_duration, i, ist->fix_sub_duration, ic, st);
+            MATCH_PER_STREAM_OPT(canvas_sizes, str, canvas_size, ic, st);
+            if (canvas_size &&
+                av_parse_video_size(&dec->width, &dec->height, canvas_size) < 0) {
+                av_log(NULL, AV_LOG_FATAL, "Invalid canvas size: %s.\n", canvas_size);
+                exit(1);
+            }
             break;
+        }
         case AVMEDIA_TYPE_ATTACHMENT:
         case AVMEDIA_TYPE_UNKNOWN:
             break;
@@ -690,12 +715,15 @@
 
 static int open_input_file(OptionsContext *o, const char *filename)
 {
+    InputFile *f;
     AVFormatContext *ic;
     AVInputFormat *file_iformat = NULL;
     int err, i, ret;
     int64_t timestamp;
     uint8_t buf[128];
     AVDictionary **opts;
+    AVDictionary *unused_opts = NULL;
+    AVDictionaryEntry *e = NULL;
     int orig_nb_streams;                     // number of streams before avformat_find_stream_info
     char *   video_codec_name = NULL;
     char *   audio_codec_name = NULL;
@@ -811,14 +839,49 @@
     av_dump_format(ic, nb_input_files, filename, 0);
 
     GROW_ARRAY(input_files, nb_input_files);
-    if (!(input_files[nb_input_files - 1] = av_mallocz(sizeof(*input_files[0]))))
+    f = av_mallocz(sizeof(*f));
+    if (!f)
         exit(1);
+    input_files[nb_input_files - 1] = f;
 
-    input_files[nb_input_files - 1]->ctx        = ic;
-    input_files[nb_input_files - 1]->ist_index  = nb_input_streams - ic->nb_streams;
-    input_files[nb_input_files - 1]->ts_offset  = o->input_ts_offset - (copy_ts ? 0 : timestamp);
-    input_files[nb_input_files - 1]->nb_streams = ic->nb_streams;
-    input_files[nb_input_files - 1]->rate_emu   = o->rate_emu;
+    f->ctx        = ic;
+    f->ist_index  = nb_input_streams - ic->nb_streams;
+    f->ts_offset  = o->input_ts_offset - (copy_ts ? 0 : timestamp);
+    f->nb_streams = ic->nb_streams;
+    f->rate_emu   = o->rate_emu;
+
+    /* check if all codec options have been used */
+    unused_opts = strip_specifiers(o->g->codec_opts);
+    for (i = f->ist_index; i < nb_input_streams; i++) {
+        e = NULL;
+        while ((e = av_dict_get(input_streams[i]->opts, "", e,
+                                AV_DICT_IGNORE_SUFFIX)))
+            av_dict_set(&unused_opts, e->key, NULL, 0);
+    }
+
+    e = NULL;
+    while ((e = av_dict_get(unused_opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
+        const AVClass *class = avcodec_get_class();
+        const AVOption *option = av_opt_find(&class, e->key, NULL, 0,
+                                             AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
+        if (!option)
+            continue;
+        if (!(option->flags & AV_OPT_FLAG_DECODING_PARAM)) {
+            av_log(NULL, AV_LOG_ERROR, "Codec AVOption %s (%s) specified for "
+                   "input file #%d (%s) is not a decoding option.\n", e->key,
+                   option->help ? option->help : "", nb_input_files - 1,
+                   filename);
+            exit(1);
+        }
+
+        av_log(NULL, AV_LOG_WARNING, "Codec AVOption %s (%s) specified for "
+               "input file #%d (%s) has not been used for any stream. The most "
+               "likely reason is either wrong type (e.g. a video option with "
+               "no video streams) or that it is a private option of some decoder "
+               "which was not actually used for any stream.\n", e->key,
+               option->help ? option->help : "", nb_input_files - 1, filename);
+    }
+    av_dict_free(&unused_opts);
 
     for (i = 0; i < o->nb_dump_attachment; i++) {
         int j;
@@ -908,6 +971,7 @@
     char *bsf = NULL, *next, *codec_tag = NULL;
     AVBitStreamFilterContext *bsfc, *bsfc_prev = NULL;
     double qscale = -1;
+    int i;
 
     if (!st) {
         av_log(NULL, AV_LOG_FATAL, "Could not alloc stream.\n");
@@ -966,6 +1030,13 @@
 
     ost->max_frames = INT64_MAX;
     MATCH_PER_STREAM_OPT(max_frames, i64, ost->max_frames, oc, st);
+    for (i = 0; i<o->nb_max_frames; i++) {
+        char *p = o->max_frames[i].specifier;
+        if (!*p && type != AVMEDIA_TYPE_VIDEO) {
+            av_log(NULL, AV_LOG_WARNING, "Applying unspecific -frames to non video streams, maybe you meant -vframes ?\n");
+            break;
+        }
+    }
 
     ost->copy_prior_start = -1;
     MATCH_PER_STREAM_OPT(copy_prior_start, i, ost->copy_prior_start, oc ,st);
@@ -1039,12 +1110,65 @@
     }
 }
 
+/* read file contents into a string */
+static uint8_t *read_file(const char *filename)
+{
+    AVIOContext *pb      = NULL;
+    AVIOContext *dyn_buf = NULL;
+    int ret = avio_open(&pb, filename, AVIO_FLAG_READ);
+    uint8_t buf[1024], *str;
+
+    if (ret < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Error opening file %s.\n", filename);
+        return NULL;
+    }
+
+    ret = avio_open_dyn_buf(&dyn_buf);
+    if (ret < 0) {
+        avio_closep(&pb);
+        return NULL;
+    }
+    while ((ret = avio_read(pb, buf, sizeof(buf))) > 0)
+        avio_write(dyn_buf, buf, ret);
+    avio_w8(dyn_buf, 0);
+    avio_closep(&pb);
+
+    ret = avio_close_dyn_buf(dyn_buf, &str);
+    if (ret < 0)
+        return NULL;
+    return str;
+}
+
+static char *get_ost_filters(OptionsContext *o, AVFormatContext *oc,
+                             OutputStream *ost)
+{
+    AVStream *st = ost->st;
+    char *filter = NULL, *filter_script = NULL;
+
+    MATCH_PER_STREAM_OPT(filter_scripts, str, filter_script, oc, st);
+    MATCH_PER_STREAM_OPT(filters, str, filter, oc, st);
+
+    if (filter_script && filter) {
+        av_log(NULL, AV_LOG_ERROR, "Both -filter and -filter_script set for "
+               "output stream #%d:%d.\n", nb_output_files, st->index);
+        exit(1);
+    }
+
+    if (filter_script)
+        return read_file(filter_script);
+    else if (filter)
+        return av_strdup(filter);
+
+    return av_strdup(st->codec->codec_type == AVMEDIA_TYPE_VIDEO ?
+                     "null" : "anull");
+}
+
 static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc, int source_index)
 {
     AVStream *st;
     OutputStream *ost;
     AVCodecContext *video_enc;
-    char *frame_rate = NULL;
+    char *frame_rate = NULL, *frame_aspect_ratio = NULL;
 
     ost = new_output_stream(o, oc, AVMEDIA_TYPE_VIDEO, source_index);
     st  = ost->st;
@@ -1056,12 +1180,22 @@
         exit(1);
     }
 
+    MATCH_PER_STREAM_OPT(frame_aspect_ratios, str, frame_aspect_ratio, oc, st);
+    if (frame_aspect_ratio) {
+        AVRational q;
+        if (av_parse_ratio(&q, frame_aspect_ratio, 255, 0, NULL) < 0 ||
+            q.num <= 0 || q.den <= 0) {
+            av_log(NULL, AV_LOG_FATAL, "Invalid aspect ratio: %s\n", frame_aspect_ratio);
+            exit(1);
+        }
+        ost->frame_aspect_ratio = q;
+    }
+
     if (!ost->stream_copy) {
         const char *p = NULL;
         char *frame_size = NULL;
-        char *frame_aspect_ratio = NULL, *frame_pix_fmt = NULL;
+        char *frame_pix_fmt = NULL;
         char *intra_matrix = NULL, *inter_matrix = NULL;
-        const char *filters = "null";
         int do_pass = 0;
         int i;
 
@@ -1071,17 +1205,6 @@
             exit(1);
         }
 
-        MATCH_PER_STREAM_OPT(frame_aspect_ratios, str, frame_aspect_ratio, oc, st);
-        if (frame_aspect_ratio) {
-            AVRational q;
-            if (av_parse_ratio(&q, frame_aspect_ratio, 255, 0, NULL) < 0 ||
-                q.num <= 0 || q.den <= 0) {
-                av_log(NULL, AV_LOG_FATAL, "Invalid aspect ratio: %s\n", frame_aspect_ratio);
-                exit(1);
-            }
-            ost->frame_aspect_ratio = av_q2d(q);
-        }
-
         video_enc->bits_per_raw_sample = frame_bits_per_raw_sample;
         MATCH_PER_STREAM_OPT(frame_pix_fmts, str, frame_pix_fmt, oc, st);
         if (frame_pix_fmt && *frame_pix_fmt == '+') {
@@ -1172,8 +1295,10 @@
         ost->top_field_first = -1;
         MATCH_PER_STREAM_OPT(top_field_first, i, ost->top_field_first, oc, st);
 
-        MATCH_PER_STREAM_OPT(filters, str, filters, oc, st);
-        ost->avfilter = av_strdup(filters);
+
+        ost->avfilter = get_ost_filters(o, oc, ost);
+        if (!ost->avfilter)
+            exit(1);
     } else {
         MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i, ost->copy_initial_nonkeyframes, oc ,st);
     }
@@ -1196,7 +1321,6 @@
 
     if (!ost->stream_copy) {
         char *sample_fmt = NULL;
-        const char *filters = "anull";
 
         MATCH_PER_STREAM_OPT(audio_channels, i, audio_enc->channels, oc, st);
 
@@ -1209,10 +1333,9 @@
 
         MATCH_PER_STREAM_OPT(audio_sample_rate, i, audio_enc->sample_rate, oc, st);
 
-        MATCH_PER_STREAM_OPT(filters, str, filters, oc, st);
-
-        av_assert1(filters);
-        ost->avfilter = av_strdup(filters);
+        ost->avfilter = get_ost_filters(o, oc, ost);
+        if (!ost->avfilter)
+            exit(1);
 
         /* check for channel mapping for this audio stream */
         for (n = 0; n < o->nb_audio_channel_maps; n++) {
@@ -1250,6 +1373,7 @@
 {
     OutputStream *ost = new_output_stream(o, oc, AVMEDIA_TYPE_ATTACHMENT, source_index);
     ost->stream_copy = 1;
+    ost->finished    = 1;
     return ost;
 }
 
@@ -1438,8 +1562,11 @@
     AVFormatContext *oc;
     int i, j, err;
     AVOutputFormat *file_oformat;
+    OutputFile *of;
     OutputStream *ost;
     InputStream  *ist;
+    AVDictionary *unused_opts = NULL;
+    AVDictionaryEntry *e = NULL;
 
     if (configure_complex_filters() < 0) {
         av_log(NULL, AV_LOG_FATAL, "Error configuring filters.\n");
@@ -1674,18 +1801,54 @@
     }
 
     GROW_ARRAY(output_files, nb_output_files);
-    if (!(output_files[nb_output_files - 1] = av_mallocz(sizeof(*output_files[0]))))
+    of = av_mallocz(sizeof(*of));
+    if (!of)
         exit(1);
+    output_files[nb_output_files - 1] = of;
 
-    output_files[nb_output_files - 1]->ctx            = oc;
-    output_files[nb_output_files - 1]->ost_index      = nb_output_streams - oc->nb_streams;
-    output_files[nb_output_files - 1]->recording_time = o->recording_time;
+    of->ctx            = oc;
+    of->ost_index      = nb_output_streams - oc->nb_streams;
+    of->recording_time = o->recording_time;
     if (o->recording_time != INT64_MAX)
         oc->duration = o->recording_time;
-    output_files[nb_output_files - 1]->start_time     = o->start_time;
-    output_files[nb_output_files - 1]->limit_filesize = o->limit_filesize;
-    output_files[nb_output_files - 1]->shortest       = o->shortest;
-    av_dict_copy(&output_files[nb_output_files - 1]->opts, o->g->format_opts, 0);
+    of->start_time     = o->start_time;
+    of->limit_filesize = o->limit_filesize;
+    of->shortest       = o->shortest;
+    av_dict_copy(&of->opts, o->g->format_opts, 0);
+
+
+    /* check if all codec options have been used */
+    unused_opts = strip_specifiers(o->g->codec_opts);
+    for (i = of->ost_index; i < nb_output_streams; i++) {
+        e = NULL;
+        while ((e = av_dict_get(output_streams[i]->opts, "", e,
+                                AV_DICT_IGNORE_SUFFIX)))
+            av_dict_set(&unused_opts, e->key, NULL, 0);
+    }
+
+    e = NULL;
+    while ((e = av_dict_get(unused_opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
+        const AVClass *class = avcodec_get_class();
+        const AVOption *option = av_opt_find(&class, e->key, NULL, 0,
+                                             AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
+        if (!option)
+            continue;
+        if (!(option->flags & AV_OPT_FLAG_ENCODING_PARAM)) {
+            av_log(NULL, AV_LOG_ERROR, "Codec AVOption %s (%s) specified for "
+                   "output file #%d (%s) is not an encoding option.\n", e->key,
+                   option->help ? option->help : "", nb_output_files - 1,
+                   filename);
+            exit(1);
+        }
+
+        av_log(NULL, AV_LOG_WARNING, "Codec AVOption %s (%s) specified for "
+               "output file #%d (%s) has not been used for any stream. The most "
+               "likely reason is either wrong type (e.g. a video option with "
+               "no video streams) or that it is a private option of some encoder "
+               "which was not actually used for any stream.\n", e->key,
+               option->help ? option->help : "", nb_output_files - 1, filename);
+    }
+    av_dict_free(&unused_opts);
 
     /* check filename in case of an image number is expected */
     if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
@@ -1702,16 +1865,17 @@
         /* open the file */
         if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE,
                               &oc->interrupt_callback,
-                              &output_files[nb_output_files - 1]->opts)) < 0) {
+                              &of->opts)) < 0) {
             print_error(filename, err);
             exit(1);
         }
-    }
+    } else if (strcmp(oc->oformat->name, "image2")==0 && !av_filename_number_test(filename))
+        assert_file_overwrite(filename);
 
     if (o->mux_preload) {
         uint8_t buf[64];
         snprintf(buf, sizeof(buf), "%d", (int)(o->mux_preload*AV_TIME_BASE));
-        av_dict_set(&output_files[nb_output_files - 1]->opts, "preload", buf, 0);
+        av_dict_set(&of->opts, "preload", buf, 0);
     }
     oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE);
 
@@ -1746,7 +1910,7 @@
         }
     }
     if (o->chapters_input_file >= 0)
-        copy_chapters(input_files[o->chapters_input_file], output_files[nb_output_files - 1],
+        copy_chapters(input_files[o->chapters_input_file], of,
                       !o->metadata_chapters_manual);
 
     /* copy global metadata by default */
@@ -1758,7 +1922,7 @@
         av_dict_set(&oc->metadata, "creation_time", NULL, 0);
     }
     if (!o->metadata_streams_manual)
-        for (i = output_files[nb_output_files - 1]->ost_index; i < nb_output_streams; i++) {
+        for (i = of->ost_index; i < nb_output_streams; i++) {
             InputStream *ist;
             if (output_streams[i]->source_index < 0)         /* this is true e.g. for attached files */
                 continue;
@@ -2032,7 +2196,7 @@
 
         if (strcspn(line, "#\n\r") == 0)
             continue;
-        strcpy(tmp_line, line);
+        av_strlcpy(tmp_line, line, sizeof(tmp_line));
         if (!av_strtok(key,   "=",    &value) ||
             !av_strtok(value, "\r\n", &endptr)) {
             av_log(NULL, AV_LOG_FATAL, "%s: Invalid syntax: '%s'\n", filename, line);
@@ -2128,13 +2292,6 @@
     return 0;
 }
 
-static int opt_deinterlace(void *optctx, const char *opt, const char *arg)
-{
-    av_log(NULL, AV_LOG_WARNING, "-%s is deprecated, use -filter:v yadif instead\n", opt);
-    do_deinterlace = 1;
-    return 0;
-}
-
 static int opt_timecode(void *optctx, const char *opt, const char *arg)
 {
     OptionsContext *o = optctx;
@@ -2193,8 +2350,24 @@
     GROW_ARRAY(filtergraphs, nb_filtergraphs);
     if (!(filtergraphs[nb_filtergraphs - 1] = av_mallocz(sizeof(*filtergraphs[0]))))
         return AVERROR(ENOMEM);
-    filtergraphs[nb_filtergraphs - 1]->index       = nb_filtergraphs - 1;
-    filtergraphs[nb_filtergraphs - 1]->graph_desc = arg;
+    filtergraphs[nb_filtergraphs - 1]->index      = nb_filtergraphs - 1;
+    filtergraphs[nb_filtergraphs - 1]->graph_desc = av_strdup(arg);
+    if (!filtergraphs[nb_filtergraphs - 1]->graph_desc)
+        return AVERROR(ENOMEM);
+    return 0;
+}
+
+static int opt_filter_complex_script(void *optctx, const char *opt, const char *arg)
+{
+    uint8_t *graph_desc = read_file(arg);
+    if (!graph_desc)
+        return AVERROR(EINVAL);
+
+    GROW_ARRAY(filtergraphs, nb_filtergraphs);
+    if (!(filtergraphs[nb_filtergraphs - 1] = av_mallocz(sizeof(*filtergraphs[0]))))
+        return AVERROR(ENOMEM);
+    filtergraphs[nb_filtergraphs - 1]->index      = nb_filtergraphs - 1;
+    filtergraphs[nb_filtergraphs - 1]->graph_desc = graph_desc;
     return 0;
 }
 
@@ -2260,7 +2433,7 @@
         show_help_children(avformat_get_class(), flags);
         show_help_children(sws_get_class(), flags);
         show_help_children(swr_get_class(), AV_OPT_FLAG_AUDIO_PARAM);
-        show_help_children(avfilter_get_class(), AV_OPT_FLAG_FILTERING_PARAM);
+        show_help_children(avfilter_get_class(), AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM);
     }
 }
 
@@ -2277,8 +2450,8 @@
 };
 
 static const OptionGroupDef groups[] = {
-    [GROUP_OUTFILE] = { "output file",  NULL },
-    [GROUP_INFILE]  = { "input file",   "i"  },
+    [GROUP_OUTFILE] = { "output file",  NULL, OPT_OUTPUT },
+    [GROUP_INFILE]  = { "input file",   "i",  OPT_INPUT },
 };
 
 static int open_files(OptionGroupList *l, const char *inout,
@@ -2381,46 +2554,57 @@
 const OptionDef options[] = {
     /* main options */
 #include "cmdutils_common_opts.h"
-    { "f",              HAS_ARG | OPT_STRING | OPT_OFFSET,           { .off       = OFFSET(format) },
+    { "f",              HAS_ARG | OPT_STRING | OPT_OFFSET |
+                        OPT_INPUT | OPT_OUTPUT,                      { .off       = OFFSET(format) },
         "force format", "fmt" },
     { "y",              OPT_BOOL,                                    {              &file_overwrite },
         "overwrite output files" },
     { "n",              OPT_BOOL,                                    {              &no_file_overwrite },
         "do not overwrite output files" },
-    { "c",              HAS_ARG | OPT_STRING | OPT_SPEC,             { .off       = OFFSET(codec_names) },
+    { "c",              HAS_ARG | OPT_STRING | OPT_SPEC |
+                        OPT_INPUT | OPT_OUTPUT,                      { .off       = OFFSET(codec_names) },
         "codec name", "codec" },
-    { "codec",          HAS_ARG | OPT_STRING | OPT_SPEC,             { .off       = OFFSET(codec_names) },
+    { "codec",          HAS_ARG | OPT_STRING | OPT_SPEC |
+                        OPT_INPUT | OPT_OUTPUT,                      { .off       = OFFSET(codec_names) },
         "codec name", "codec" },
-    { "pre",            HAS_ARG | OPT_STRING | OPT_SPEC,             { .off       = OFFSET(presets) },
+    { "pre",            HAS_ARG | OPT_STRING | OPT_SPEC |
+                        OPT_OUTPUT,                                  { .off       = OFFSET(presets) },
         "preset name", "preset" },
-    { "map",            HAS_ARG | OPT_EXPERT | OPT_PERFILE,          { .func_arg = opt_map },
+    { "map",            HAS_ARG | OPT_EXPERT | OPT_PERFILE |
+                        OPT_OUTPUT,                                  { .func_arg = opt_map },
         "set input stream mapping",
         "[-]input_file_id[:stream_specifier][,sync_file_id[:stream_specifier]]" },
-    { "map_channel",    HAS_ARG | OPT_EXPERT | OPT_PERFILE,          { .func_arg = opt_map_channel },
+    { "map_channel",    HAS_ARG | OPT_EXPERT | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_map_channel },
         "map an audio channel from one stream to another", "file.stream.channel[:syncfile.syncstream]" },
-    { "map_metadata",   HAS_ARG | OPT_STRING | OPT_SPEC,             { .off       = OFFSET(metadata_map) },
+    { "map_metadata",   HAS_ARG | OPT_STRING | OPT_SPEC |
+                        OPT_OUTPUT,                                  { .off       = OFFSET(metadata_map) },
         "set metadata information of outfile from infile",
         "outfile[,metadata]:infile[,metadata]" },
-    { "map_chapters",   HAS_ARG | OPT_INT | OPT_EXPERT | OPT_OFFSET, { .off = OFFSET(chapters_input_file) },
+    { "map_chapters",   HAS_ARG | OPT_INT | OPT_EXPERT | OPT_OFFSET |
+                        OPT_OUTPUT,                                  { .off = OFFSET(chapters_input_file) },
         "set chapters mapping", "input_file_index" },
-    { "t",              HAS_ARG | OPT_TIME | OPT_OFFSET,             { .off = OFFSET(recording_time) },
+    { "t",              HAS_ARG | OPT_TIME | OPT_OFFSET | OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(recording_time) },
         "record or transcode \"duration\" seconds of audio/video",
         "duration" },
-    { "to",             HAS_ARG | OPT_TIME | OPT_OFFSET,             { .off = OFFSET(stop_time) },
+    { "to",             HAS_ARG | OPT_TIME | OPT_OFFSET | OPT_OUTPUT,  { .off = OFFSET(stop_time) },
         "record or transcode stop time", "time_stop" },
-    { "fs",             HAS_ARG | OPT_INT64 | OPT_OFFSET,            { .off = OFFSET(limit_filesize) },
+    { "fs",             HAS_ARG | OPT_INT64 | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(limit_filesize) },
         "set the limit file size in bytes", "limit_size" },
-    { "ss",             HAS_ARG | OPT_TIME | OPT_OFFSET,             { .off = OFFSET(start_time) },
+    { "ss",             HAS_ARG | OPT_TIME | OPT_OFFSET |
+                        OPT_INPUT | OPT_OUTPUT,                      { .off = OFFSET(start_time) },
         "set the start time offset", "time_off" },
-    { "itsoffset",      HAS_ARG | OPT_TIME | OPT_OFFSET | OPT_EXPERT,{ .off = OFFSET(input_ts_offset) },
+    { "itsoffset",      HAS_ARG | OPT_TIME | OPT_OFFSET |
+                        OPT_EXPERT | OPT_INPUT,                      { .off = OFFSET(input_ts_offset) },
         "set the input ts offset", "time_off" },
-    { "itsscale",       HAS_ARG | OPT_DOUBLE | OPT_SPEC | OPT_EXPERT,{ .off = OFFSET(ts_scale) },
+    { "itsscale",       HAS_ARG | OPT_DOUBLE | OPT_SPEC |
+                        OPT_EXPERT | OPT_INPUT,                      { .off = OFFSET(ts_scale) },
         "set the input ts scale", "scale" },
     { "timestamp",      HAS_ARG | OPT_PERFILE,                       { .func_arg = opt_recording_timestamp },
         "set the recording timestamp ('now' to set the current time)", "time" },
-    { "metadata",       HAS_ARG | OPT_STRING | OPT_SPEC,             { .off = OFFSET(metadata) },
+    { "metadata",       HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(metadata) },
         "add metadata", "string=string" },
-    { "dframes",        HAS_ARG | OPT_PERFILE | OPT_EXPERT,          { .func_arg = opt_data_frames },
+    { "dframes",        HAS_ARG | OPT_PERFILE | OPT_EXPERT |
+                        OPT_OUTPUT,                                  { .func_arg = opt_data_frames },
         "set the number of data frames to record", "number" },
     { "benchmark",      OPT_BOOL | OPT_EXPERT,                       { &do_benchmark },
         "add timings for benchmarking" },
@@ -2436,9 +2620,10 @@
         "dump each input packet" },
     { "hex",            OPT_BOOL | OPT_EXPERT,                       { &do_hex_dump },
         "when dumping packets, also dump the payload" },
-    { "re",             OPT_BOOL | OPT_EXPERT | OPT_OFFSET,          { .off = OFFSET(rate_emu) },
+    { "re",             OPT_BOOL | OPT_EXPERT | OPT_OFFSET |
+                        OPT_INPUT,                                   { .off = OFFSET(rate_emu) },
         "read input at native frame rate", "" },
-    { "target",         HAS_ARG | OPT_PERFILE,                       { .func_arg = opt_target },
+    { "target",         HAS_ARG | OPT_PERFILE | OPT_OUTPUT,          { .func_arg = opt_target },
         "specify target file type (\"vcd\", \"svcd\", \"dvd\","
         " \"dv\", \"dv50\", \"pal-vcd\", \"ntsc-svcd\", ...)", "type" },
     { "vsync",          HAS_ARG | OPT_EXPERT,                        { opt_vsync },
@@ -2451,7 +2636,8 @@
         "copy timestamps" },
     { "copytb",         HAS_ARG | OPT_INT | OPT_EXPERT,              { &copy_tb },
         "copy input stream time base when stream copying", "mode" },
-    { "shortest",       OPT_BOOL | OPT_EXPERT | OPT_OFFSET,          { .off = OFFSET(shortest) },
+    { "shortest",       OPT_BOOL | OPT_EXPERT | OPT_OFFSET |
+                        OPT_OUTPUT,                                  { .off = OFFSET(shortest) },
         "finish encoding within shortest input" },
     { "dts_delta_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT,       { &dts_delta_threshold },
         "timestamp discontinuity delta threshold", "threshold" },
@@ -2459,69 +2645,88 @@
         "timestamp error delta threshold", "threshold" },
     { "xerror",         OPT_BOOL | OPT_EXPERT,                       { &exit_on_error },
         "exit on error", "error" },
-    { "copyinkf",       OPT_BOOL | OPT_EXPERT | OPT_SPEC,            { .off = OFFSET(copy_initial_nonkeyframes) },
+    { "copyinkf",       OPT_BOOL | OPT_EXPERT | OPT_SPEC |
+                        OPT_OUTPUT,                                  { .off = OFFSET(copy_initial_nonkeyframes) },
         "copy initial non-keyframes" },
-    { "copypriorss",    OPT_INT | HAS_ARG | OPT_EXPERT | OPT_SPEC,   { .off = OFFSET(copy_prior_start) },
+    { "copypriorss",    OPT_INT | HAS_ARG | OPT_EXPERT | OPT_SPEC | OPT_OUTPUT,   { .off = OFFSET(copy_prior_start) },
         "copy or discard frames before start time" },
-    { "frames",         OPT_INT64 | HAS_ARG | OPT_SPEC,              { .off = OFFSET(max_frames) },
+    { "frames",         OPT_INT64 | HAS_ARG | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(max_frames) },
         "set the number of frames to record", "number" },
-    { "tag",            OPT_STRING | HAS_ARG | OPT_SPEC | OPT_EXPERT,{ .off = OFFSET(codec_tags) },
+    { "tag",            OPT_STRING | HAS_ARG | OPT_SPEC |
+                        OPT_EXPERT | OPT_OUTPUT,                     { .off = OFFSET(codec_tags) },
         "force codec tag/fourcc", "fourcc/tag" },
-    { "q",              HAS_ARG | OPT_EXPERT | OPT_DOUBLE | OPT_SPEC,{ .off = OFFSET(qscale) },
+    { "q",              HAS_ARG | OPT_EXPERT | OPT_DOUBLE |
+                        OPT_SPEC | OPT_OUTPUT,                       { .off = OFFSET(qscale) },
         "use fixed quality scale (VBR)", "q" },
-    { "qscale",         HAS_ARG | OPT_EXPERT | OPT_PERFILE,          { .func_arg = opt_qscale },
+    { "qscale",         HAS_ARG | OPT_EXPERT | OPT_PERFILE |
+                        OPT_OUTPUT,                                  { .func_arg = opt_qscale },
         "use fixed quality scale (VBR)", "q" },
-    { "profile",        HAS_ARG | OPT_EXPERT | OPT_PERFILE,          { .func_arg = opt_profile },
+    { "profile",        HAS_ARG | OPT_EXPERT | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_profile },
         "set profile", "profile" },
-    { "filter",         HAS_ARG | OPT_STRING | OPT_SPEC,             { .off = OFFSET(filters) },
+    { "filter",         HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(filters) },
         "set stream filtergraph", "filter_graph" },
-    { "reinit_filter",  HAS_ARG | OPT_INT | OPT_SPEC,                { .off = OFFSET(reinit_filters) },
+    { "filter_script",  HAS_ARG | OPT_STRING | OPT_SPEC | OPT_OUTPUT, { .off = OFFSET(filter_scripts) },
+        "read stream filtergraph description from a file", "filename" },
+    { "reinit_filter",  HAS_ARG | OPT_INT | OPT_SPEC | OPT_INPUT,    { .off = OFFSET(reinit_filters) },
         "reinit filtergraph on input parameter changes", "" },
     { "filter_complex", HAS_ARG | OPT_EXPERT,                        { .func_arg = opt_filter_complex },
         "create a complex filtergraph", "graph_description" },
+    { "lavfi",          HAS_ARG | OPT_EXPERT,                        { .func_arg = opt_filter_complex },
+        "create a complex filtergraph", "graph_description" },
+    { "filter_complex_script", HAS_ARG | OPT_EXPERT,                 { .func_arg = opt_filter_complex_script },
+        "read complex filtergraph description from a file", "filename" },
     { "stats",          OPT_BOOL,                                    { &print_stats },
         "print progress report during encoding", },
-    { "attach",         HAS_ARG | OPT_PERFILE | OPT_EXPERT,          { .func_arg = opt_attach },
+    { "attach",         HAS_ARG | OPT_PERFILE | OPT_EXPERT |
+                        OPT_OUTPUT,                                  { .func_arg = opt_attach },
         "add an attachment to the output file", "filename" },
-    { "dump_attachment", HAS_ARG | OPT_STRING | OPT_SPEC |OPT_EXPERT,{ .off = OFFSET(dump_attachment) },
+    { "dump_attachment", HAS_ARG | OPT_STRING | OPT_SPEC |
+                         OPT_EXPERT | OPT_INPUT,                     { .off = OFFSET(dump_attachment) },
         "extract an attachment into a file", "filename" },
     { "debug_ts",       OPT_BOOL | OPT_EXPERT,                       { &debug_ts },
         "print timestamp debugging info" },
 
     /* video options */
-    { "vframes",      OPT_VIDEO | HAS_ARG  | OPT_PERFILE,                        { .func_arg = opt_video_frames },
+    { "vframes",      OPT_VIDEO | HAS_ARG  | OPT_PERFILE | OPT_OUTPUT,           { .func_arg = opt_video_frames },
         "set the number of video frames to record", "number" },
-    { "r",            OPT_VIDEO | HAS_ARG  | OPT_STRING | OPT_SPEC,              { .off = OFFSET(frame_rates) },
+    { "r",            OPT_VIDEO | HAS_ARG  | OPT_STRING | OPT_SPEC |
+                      OPT_INPUT | OPT_OUTPUT,                                    { .off = OFFSET(frame_rates) },
         "set frame rate (Hz value, fraction or abbreviation)", "rate" },
-    { "s",            OPT_VIDEO | HAS_ARG | OPT_SUBTITLE | OPT_STRING | OPT_SPEC,{ .off = OFFSET(frame_sizes) },
+    { "s",            OPT_VIDEO | HAS_ARG | OPT_SUBTITLE | OPT_STRING | OPT_SPEC |
+                      OPT_INPUT | OPT_OUTPUT,                                    { .off = OFFSET(frame_sizes) },
         "set frame size (WxH or abbreviation)", "size" },
-    { "aspect",       OPT_VIDEO | HAS_ARG  | OPT_STRING | OPT_SPEC,              { .off = OFFSET(frame_aspect_ratios) },
+    { "aspect",       OPT_VIDEO | HAS_ARG  | OPT_STRING | OPT_SPEC |
+                      OPT_OUTPUT,                                                { .off = OFFSET(frame_aspect_ratios) },
         "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)", "aspect" },
-    { "pix_fmt",      OPT_VIDEO | HAS_ARG | OPT_EXPERT  | OPT_STRING | OPT_SPEC, { .off = OFFSET(frame_pix_fmts) },
+    { "pix_fmt",      OPT_VIDEO | HAS_ARG | OPT_EXPERT  | OPT_STRING | OPT_SPEC |
+                      OPT_INPUT | OPT_OUTPUT,                                    { .off = OFFSET(frame_pix_fmts) },
         "set pixel format", "format" },
     { "bits_per_raw_sample", OPT_VIDEO | OPT_INT | HAS_ARG,                      { &frame_bits_per_raw_sample },
         "set the number of bits per raw sample", "number" },
     { "intra",        OPT_VIDEO | OPT_BOOL | OPT_EXPERT,                         { &intra_only },
         "deprecated use -g 1" },
-    { "vn",           OPT_VIDEO | OPT_BOOL  | OPT_OFFSET,                        { .off = OFFSET(video_disable) },
+    { "vn",           OPT_VIDEO | OPT_BOOL  | OPT_OFFSET | OPT_INPUT | OPT_OUTPUT,{ .off = OFFSET(video_disable) },
         "disable video" },
     { "vdt",          OPT_VIDEO | OPT_INT | HAS_ARG | OPT_EXPERT ,               { &video_discard },
         "discard threshold", "n" },
-    { "rc_override",  OPT_VIDEO | HAS_ARG | OPT_EXPERT  | OPT_STRING | OPT_SPEC, { .off = OFFSET(rc_overrides) },
+    { "rc_override",  OPT_VIDEO | HAS_ARG | OPT_EXPERT  | OPT_STRING | OPT_SPEC |
+                      OPT_OUTPUT,                                                { .off = OFFSET(rc_overrides) },
         "rate control override for specific intervals", "override" },
-    { "vcodec",       OPT_VIDEO | HAS_ARG  | OPT_PERFILE,                        { .func_arg = opt_video_codec },
+    { "vcodec",       OPT_VIDEO | HAS_ARG  | OPT_PERFILE | OPT_INPUT |
+                      OPT_OUTPUT,                                                { .func_arg = opt_video_codec },
         "force video codec ('copy' to copy stream)", "codec" },
     { "sameq",        OPT_VIDEO | OPT_EXPERT ,                                   { .func_arg = opt_sameq },
         "Removed" },
     { "same_quant",   OPT_VIDEO | OPT_EXPERT ,                                   { .func_arg = opt_sameq },
         "Removed" },
-    { "timecode",     OPT_VIDEO | HAS_ARG | OPT_PERFILE,                         { .func_arg = opt_timecode },
+    { "timecode",     OPT_VIDEO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT,            { .func_arg = opt_timecode },
         "set initial TimeCode value.", "hh:mm:ss[:;.]ff" },
-    { "pass",         OPT_VIDEO | HAS_ARG | OPT_SPEC | OPT_INT,                  { .off = OFFSET(pass) },
+    { "pass",         OPT_VIDEO | HAS_ARG | OPT_SPEC | OPT_INT | OPT_OUTPUT,     { .off = OFFSET(pass) },
         "select the pass number (1 to 3)", "n" },
-    { "passlogfile",  OPT_VIDEO | HAS_ARG | OPT_STRING | OPT_EXPERT | OPT_SPEC,  { .off = OFFSET(passlogfiles) },
+    { "passlogfile",  OPT_VIDEO | HAS_ARG | OPT_STRING | OPT_EXPERT | OPT_SPEC |
+                      OPT_OUTPUT,                                                { .off = OFFSET(passlogfiles) },
         "select two pass log file name prefix", "prefix" },
-    { "deinterlace",  OPT_VIDEO | OPT_EXPERT ,                                   { .func_arg = opt_deinterlace },
+    { "deinterlace",  OPT_VIDEO | OPT_BOOL | OPT_EXPERT,                         { &do_deinterlace },
         "this option is deprecated, use the yadif filter instead" },
     { "psnr",         OPT_VIDEO | OPT_BOOL | OPT_EXPERT,                         { &do_psnr },
         "calculate PSNR of compressed frames" },
@@ -2529,65 +2734,79 @@
         "dump video coding statistics to file" },
     { "vstats_file",  OPT_VIDEO | HAS_ARG | OPT_EXPERT ,                         { opt_vstats_file },
         "dump video coding statistics to file", "file" },
-    { "vf",           OPT_VIDEO | HAS_ARG  | OPT_PERFILE,                        { .func_arg = opt_video_filters },
+    { "vf",           OPT_VIDEO | HAS_ARG  | OPT_PERFILE | OPT_OUTPUT,           { .func_arg = opt_video_filters },
         "set video filters", "filter_graph" },
-    { "intra_matrix", OPT_VIDEO | HAS_ARG | OPT_EXPERT  | OPT_STRING | OPT_SPEC, { .off = OFFSET(intra_matrices) },
+    { "intra_matrix", OPT_VIDEO | HAS_ARG | OPT_EXPERT  | OPT_STRING | OPT_SPEC |
+                      OPT_OUTPUT,                                                { .off = OFFSET(intra_matrices) },
         "specify intra matrix coeffs", "matrix" },
-    { "inter_matrix", OPT_VIDEO | HAS_ARG | OPT_EXPERT  | OPT_STRING | OPT_SPEC, { .off = OFFSET(inter_matrices) },
+    { "inter_matrix", OPT_VIDEO | HAS_ARG | OPT_EXPERT  | OPT_STRING | OPT_SPEC |
+                      OPT_OUTPUT,                                                { .off = OFFSET(inter_matrices) },
         "specify inter matrix coeffs", "matrix" },
-    { "top",          OPT_VIDEO | HAS_ARG | OPT_EXPERT  | OPT_INT| OPT_SPEC,     { .off = OFFSET(top_field_first) },
+    { "top",          OPT_VIDEO | HAS_ARG | OPT_EXPERT  | OPT_INT| OPT_SPEC |
+                      OPT_INPUT | OPT_OUTPUT,                                    { .off = OFFSET(top_field_first) },
         "top=1/bottom=0/auto=-1 field first", "" },
     { "dc",           OPT_VIDEO | OPT_INT | HAS_ARG | OPT_EXPERT ,               { &intra_dc_precision },
         "intra_dc_precision", "precision" },
-    { "vtag",         OPT_VIDEO | HAS_ARG | OPT_EXPERT  | OPT_PERFILE,           { .func_arg = opt_old2new },
+    { "vtag",         OPT_VIDEO | HAS_ARG | OPT_EXPERT  | OPT_PERFILE |
+                      OPT_OUTPUT,                                                { .func_arg = opt_old2new },
         "force video tag/fourcc", "fourcc/tag" },
     { "qphist",       OPT_VIDEO | OPT_BOOL | OPT_EXPERT ,                        { &qp_hist },
         "show QP histogram" },
-    { "force_fps",    OPT_VIDEO | OPT_BOOL | OPT_EXPERT  | OPT_SPEC,             { .off = OFFSET(force_fps) },
+    { "force_fps",    OPT_VIDEO | OPT_BOOL | OPT_EXPERT  | OPT_SPEC |
+                      OPT_OUTPUT,                                                { .off = OFFSET(force_fps) },
         "force the selected framerate, disable the best supported framerate selection" },
-    { "streamid",     OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_PERFILE,            { .func_arg = opt_streamid },
+    { "streamid",     OPT_VIDEO | HAS_ARG | OPT_EXPERT | OPT_PERFILE |
+                      OPT_OUTPUT,                                                { .func_arg = opt_streamid },
         "set the value of an outfile streamid", "streamIndex:value" },
-    { "force_key_frames", OPT_VIDEO | OPT_STRING | HAS_ARG | OPT_EXPERT  | OPT_SPEC,
-        { .off = OFFSET(forced_key_frames) },
+    { "force_key_frames", OPT_VIDEO | OPT_STRING | HAS_ARG | OPT_EXPERT |
+                          OPT_SPEC | OPT_OUTPUT,                                 { .off = OFFSET(forced_key_frames) },
         "force key frames at specified timestamps", "timestamps" },
-    { "b",            OPT_VIDEO | HAS_ARG | OPT_PERFILE,                         { .func_arg = opt_bitrate },
+    { "b",            OPT_VIDEO | HAS_ARG | OPT_PERFILE | OPT_OUTPUT,            { .func_arg = opt_bitrate },
         "video bitrate (please use -b:v)", "bitrate" },
 
     /* audio options */
-    { "aframes",        OPT_AUDIO | HAS_ARG  | OPT_PERFILE,                        { .func_arg = opt_audio_frames },
+    { "aframes",        OPT_AUDIO | HAS_ARG  | OPT_PERFILE | OPT_OUTPUT,           { .func_arg = opt_audio_frames },
         "set the number of audio frames to record", "number" },
-    { "aq",             OPT_AUDIO | HAS_ARG  | OPT_PERFILE,                        { .func_arg = opt_audio_qscale },
+    { "aq",             OPT_AUDIO | HAS_ARG  | OPT_PERFILE | OPT_OUTPUT,           { .func_arg = opt_audio_qscale },
         "set audio quality (codec-specific)", "quality", },
-    { "ar",             OPT_AUDIO | HAS_ARG  | OPT_INT | OPT_SPEC,                 { .off = OFFSET(audio_sample_rate) },
+    { "ar",             OPT_AUDIO | HAS_ARG  | OPT_INT | OPT_SPEC |
+                        OPT_INPUT | OPT_OUTPUT,                                    { .off = OFFSET(audio_sample_rate) },
         "set audio sampling rate (in Hz)", "rate" },
-    { "ac",             OPT_AUDIO | HAS_ARG  | OPT_INT | OPT_SPEC,                 { .off = OFFSET(audio_channels) },
+    { "ac",             OPT_AUDIO | HAS_ARG  | OPT_INT | OPT_SPEC |
+                        OPT_INPUT | OPT_OUTPUT,                                    { .off = OFFSET(audio_channels) },
         "set number of audio channels", "channels" },
-    { "an",             OPT_AUDIO | OPT_BOOL | OPT_OFFSET,                         { .off = OFFSET(audio_disable) },
+    { "an",             OPT_AUDIO | OPT_BOOL | OPT_OFFSET | OPT_INPUT | OPT_OUTPUT,{ .off = OFFSET(audio_disable) },
         "disable audio" },
-    { "acodec",         OPT_AUDIO | HAS_ARG  | OPT_PERFILE,                        { .func_arg = opt_audio_codec },
+    { "acodec",         OPT_AUDIO | HAS_ARG  | OPT_PERFILE |
+                        OPT_INPUT | OPT_OUTPUT,                                    { .func_arg = opt_audio_codec },
         "force audio codec ('copy' to copy stream)", "codec" },
-    { "atag",           OPT_AUDIO | HAS_ARG  | OPT_EXPERT | OPT_PERFILE,           { .func_arg = opt_old2new },
+    { "atag",           OPT_AUDIO | HAS_ARG  | OPT_EXPERT | OPT_PERFILE |
+                        OPT_OUTPUT,                                                { .func_arg = opt_old2new },
         "force audio tag/fourcc", "fourcc/tag" },
     { "vol",            OPT_AUDIO | HAS_ARG  | OPT_INT,                            { &audio_volume },
         "change audio volume (256=normal)" , "volume" },
-    { "sample_fmt",     OPT_AUDIO | HAS_ARG  | OPT_EXPERT | OPT_SPEC | OPT_STRING, { .off = OFFSET(sample_fmts) },
+    { "sample_fmt",     OPT_AUDIO | HAS_ARG  | OPT_EXPERT | OPT_SPEC |
+                        OPT_STRING | OPT_INPUT | OPT_OUTPUT,                       { .off = OFFSET(sample_fmts) },
         "set sample format", "format" },
-    { "channel_layout", OPT_AUDIO | HAS_ARG  | OPT_EXPERT | OPT_PERFILE,           { .func_arg = opt_channel_layout },
+    { "channel_layout", OPT_AUDIO | HAS_ARG  | OPT_EXPERT | OPT_PERFILE |
+                        OPT_INPUT | OPT_OUTPUT,                                    { .func_arg = opt_channel_layout },
         "set channel layout", "layout" },
-    { "af",             OPT_AUDIO | HAS_ARG  | OPT_PERFILE,                        { .func_arg = opt_audio_filters },
+    { "af",             OPT_AUDIO | HAS_ARG  | OPT_PERFILE | OPT_OUTPUT,           { .func_arg = opt_audio_filters },
         "set audio filters", "filter_graph" },
-    { "guess_layout_max", OPT_AUDIO | HAS_ARG | OPT_INT | OPT_SPEC | OPT_EXPERT,   { .off = OFFSET(guess_layout_max) },
+    { "guess_layout_max", OPT_AUDIO | HAS_ARG | OPT_INT | OPT_SPEC | OPT_EXPERT | OPT_INPUT, { .off = OFFSET(guess_layout_max) },
       "set the maximum number of channels to try to guess the channel layout" },
 
     /* subtitle options */
-    { "sn",     OPT_SUBTITLE | OPT_BOOL | OPT_OFFSET, { .off = OFFSET(subtitle_disable) },
+    { "sn",     OPT_SUBTITLE | OPT_BOOL | OPT_OFFSET | OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(subtitle_disable) },
         "disable subtitle" },
-    { "scodec", OPT_SUBTITLE | HAS_ARG  | OPT_PERFILE, { .func_arg = opt_subtitle_codec },
+    { "scodec", OPT_SUBTITLE | HAS_ARG  | OPT_PERFILE | OPT_INPUT | OPT_OUTPUT, { .func_arg = opt_subtitle_codec },
         "force subtitle codec ('copy' to copy stream)", "codec" },
-    { "stag",   OPT_SUBTITLE | HAS_ARG  | OPT_EXPERT  | OPT_PERFILE, { .func_arg = opt_old2new }
+    { "stag",   OPT_SUBTITLE | HAS_ARG  | OPT_EXPERT  | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_old2new }
         , "force subtitle tag/fourcc", "fourcc/tag" },
-    { "fix_sub_duration", OPT_BOOL | OPT_EXPERT | OPT_SUBTITLE | OPT_SPEC, { .off = OFFSET(fix_sub_duration) },
+    { "fix_sub_duration", OPT_BOOL | OPT_EXPERT | OPT_SUBTITLE | OPT_SPEC | OPT_INPUT, { .off = OFFSET(fix_sub_duration) },
         "fix subtitles duration" },
+    { "canvas_size", OPT_SUBTITLE | HAS_ARG | OPT_STRING | OPT_SPEC | OPT_INPUT, { .off = OFFSET(canvas_sizes) },
+        "set canvas size (WxH or abbreviation)", "size" },
 
     /* grab options */
     { "vc", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { .func_arg = opt_video_channel },
@@ -2597,30 +2816,30 @@
     { "isync", OPT_BOOL | OPT_EXPERT, { &input_sync }, "this option is deprecated and does nothing", "" },
 
     /* muxer options */
-    { "muxdelay",   OPT_FLOAT | HAS_ARG | OPT_EXPERT | OPT_OFFSET, { .off = OFFSET(mux_max_delay) },
+    { "muxdelay",   OPT_FLOAT | HAS_ARG | OPT_EXPERT | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(mux_max_delay) },
         "set the maximum demux-decode delay", "seconds" },
-    { "muxpreload", OPT_FLOAT | HAS_ARG | OPT_EXPERT | OPT_OFFSET, { .off = OFFSET(mux_preload) },
+    { "muxpreload", OPT_FLOAT | HAS_ARG | OPT_EXPERT | OPT_OFFSET | OPT_OUTPUT, { .off = OFFSET(mux_preload) },
         "set the initial demux-decode delay", "seconds" },
 
-    { "bsf", HAS_ARG | OPT_STRING | OPT_SPEC | OPT_EXPERT, { .off = OFFSET(bitstream_filters) },
+    { "bsf", HAS_ARG | OPT_STRING | OPT_SPEC | OPT_EXPERT | OPT_OUTPUT, { .off = OFFSET(bitstream_filters) },
         "A comma-separated list of bitstream filters", "bitstream_filters" },
-    { "absf", HAS_ARG | OPT_AUDIO | OPT_EXPERT| OPT_PERFILE, { .func_arg = opt_old2new },
+    { "absf", HAS_ARG | OPT_AUDIO | OPT_EXPERT| OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_old2new },
         "deprecated", "audio bitstream_filters" },
-    { "vbsf", OPT_VIDEO | HAS_ARG | OPT_EXPERT| OPT_PERFILE, { .func_arg = opt_old2new },
+    { "vbsf", OPT_VIDEO | HAS_ARG | OPT_EXPERT| OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_old2new },
         "deprecated", "video bitstream_filters" },
 
-    { "apre", HAS_ARG | OPT_AUDIO | OPT_EXPERT| OPT_PERFILE,    { .func_arg = opt_preset },
+    { "apre", HAS_ARG | OPT_AUDIO | OPT_EXPERT| OPT_PERFILE | OPT_OUTPUT,    { .func_arg = opt_preset },
         "set the audio options to the indicated preset", "preset" },
-    { "vpre", OPT_VIDEO | HAS_ARG | OPT_EXPERT| OPT_PERFILE,    { .func_arg = opt_preset },
+    { "vpre", OPT_VIDEO | HAS_ARG | OPT_EXPERT| OPT_PERFILE | OPT_OUTPUT,    { .func_arg = opt_preset },
         "set the video options to the indicated preset", "preset" },
-    { "spre", HAS_ARG | OPT_SUBTITLE | OPT_EXPERT| OPT_PERFILE, { .func_arg = opt_preset },
+    { "spre", HAS_ARG | OPT_SUBTITLE | OPT_EXPERT| OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_preset },
         "set the subtitle options to the indicated preset", "preset" },
-    { "fpre", HAS_ARG | OPT_EXPERT| OPT_PERFILE,                { .func_arg = opt_preset },
+    { "fpre", HAS_ARG | OPT_EXPERT| OPT_PERFILE | OPT_OUTPUT,                { .func_arg = opt_preset },
         "set options from indicated preset file", "filename" },
     /* data codec support */
-    { "dcodec", HAS_ARG | OPT_DATA | OPT_PERFILE | OPT_EXPERT, { .func_arg = opt_data_codec },
+    { "dcodec", HAS_ARG | OPT_DATA | OPT_PERFILE | OPT_EXPERT | OPT_INPUT | OPT_OUTPUT, { .func_arg = opt_data_codec },
         "force data codec ('copy' to copy stream)", "codec" },
-    { "dn", OPT_BOOL | OPT_VIDEO | OPT_OFFSET, { .off = OFFSET(data_disable) },
+    { "dn", OPT_BOOL | OPT_VIDEO | OPT_OFFSET | OPT_INPUT | OPT_OUTPUT, { .off = OFFSET(data_disable) },
         "disable data" },
 
     { NULL, },
diff --git a/ffplay.c b/ffplay.c
index 01ee15e..ca0a828 100644
--- a/ffplay.c
+++ b/ffplay.c
@@ -48,7 +48,6 @@
 #if CONFIG_AVFILTER
 # include "libavfilter/avcodec.h"
 # include "libavfilter/avfilter.h"
-# include "libavfilter/avfiltergraph.h"
 # include "libavfilter/buffersink.h"
 # include "libavfilter/buffersrc.h"
 #endif
@@ -121,14 +120,11 @@
     int64_t pos;            // byte position in file
     SDL_Overlay *bmp;
     int width, height; /* source height & width */
-    AVRational sample_aspect_ratio;
     int allocated;
     int reallocate;
     int serial;
 
-#if CONFIG_AVFILTER
-    AVFilterBufferRef *picref;
-#endif
+    AVRational sar;
 } VideoPicture;
 
 typedef struct SubPicture {
@@ -191,10 +187,15 @@
     unsigned int audio_buf1_size;
     int audio_buf_index; /* in bytes */
     int audio_write_buf_size;
+    int audio_buf_frames_pending;
     AVPacket audio_pkt_temp;
     AVPacket audio_pkt;
     int audio_pkt_temp_serial;
+    int audio_last_serial;
     struct AudioParams audio_src;
+#if CONFIG_AVFILTER
+    struct AudioParams audio_filter_src;
+#endif
     struct AudioParams audio_tgt;
     struct SwrContext *swr_ctx;
     double audio_current_pts;
@@ -232,6 +233,7 @@
     double frame_last_returned_time;
     double frame_last_filter_delay;
     int64_t frame_last_dropped_pos;
+    int frame_last_dropped_serial;
     int video_stream;
     AVStream *video_st;
     PacketQueue videoq;
@@ -256,8 +258,9 @@
 #if CONFIG_AVFILTER
     AVFilterContext *in_video_filter;   // the first filter in the video chain
     AVFilterContext *out_video_filter;  // the last filter in the video chain
-    int use_dr1;
-    FrameBuffer *buffer_pool;
+    AVFilterContext *in_audio_filter;   // the first filter in the audio chain
+    AVFilterContext *out_audio_filter;  // the last filter in the audio chain
+    AVFilterGraph *agraph;              // audio filter graph
 #endif
 
     int last_video_stream, last_audio_stream, last_subtitle_stream;
@@ -294,9 +297,6 @@
 static int genpts = 0;
 static int lowres = 0;
 static int idct = FF_IDCT_AUTO;
-static enum AVDiscard skip_frame       = AVDISCARD_DEFAULT;
-static enum AVDiscard skip_idct        = AVDISCARD_DEFAULT;
-static enum AVDiscard skip_loop_filter = AVDISCARD_DEFAULT;
 static int error_concealment = 3;
 static int decoder_reorder_pts = -1;
 static int autoexit;
@@ -314,6 +314,7 @@
 static int cursor_hidden = 0;
 #if CONFIG_AVFILTER
 static char *vfilters = NULL;
+static char *afilters = NULL;
 #endif
 
 /* current context */
@@ -327,6 +328,26 @@
 
 static SDL_Surface *screen;
 
+static inline
+int cmp_audio_fmts(enum AVSampleFormat fmt1, int64_t channel_count1,
+                   enum AVSampleFormat fmt2, int64_t channel_count2)
+{
+    /* If channel count == 1, planar and non-planar formats are the same */
+    if (channel_count1 == 1 && channel_count2 == 1)
+        return av_get_packed_sample_fmt(fmt1) != av_get_packed_sample_fmt(fmt2);
+    else
+        return channel_count1 != channel_count2 || fmt1 != fmt2;
+}
+
+static inline
+int64_t get_valid_channel_layout(int64_t channel_layout, int channels)
+{
+    if (channel_layout && av_get_channel_layout_nb_channels(channel_layout) == channels)
+        return channel_layout;
+    else
+        return 0;
+}
+
 static int packet_queue_put(PacketQueue *q, AVPacket *pkt);
 
 static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
@@ -753,10 +774,10 @@
     float aspect_ratio;
     int width, height, x, y;
 
-    if (vp->sample_aspect_ratio.num == 0)
+    if (vp->sar.num == 0)
         aspect_ratio = 0;
     else
-        aspect_ratio = av_q2d(vp->sample_aspect_ratio);
+        aspect_ratio = av_q2d(vp->sar);
 
     if (aspect_ratio <= 0.0)
         aspect_ratio = 1.0;
@@ -982,9 +1003,6 @@
     /* free all pictures */
     for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
         vp = &is->pictq[i];
-#if CONFIG_AVFILTER
-        avfilter_unref_bufferp(&vp->picref);
-#endif
         if (vp->bmp) {
             SDL_FreeYUVOverlay(vp->bmp);
             vp->bmp = NULL;
@@ -1009,7 +1027,6 @@
     av_lockmgr_register(NULL);
     uninit_opts();
 #if CONFIG_AVFILTER
-    avfilter_uninit();
     av_freep(&vfilters);
 #endif
     avformat_network_deinit();
@@ -1323,7 +1340,7 @@
         if (is->pictq_size == 0) {
             SDL_LockMutex(is->pictq_mutex);
             if (is->frame_last_dropped_pts != AV_NOPTS_VALUE && is->frame_last_dropped_pts > is->frame_last_pts) {
-                update_video_pts(is, is->frame_last_dropped_pts, is->frame_last_dropped_pos, 0);
+                update_video_pts(is, is->frame_last_dropped_pts, is->frame_last_dropped_pos, is->frame_last_dropped_serial);
                 is->frame_last_dropped_pts = AV_NOPTS_VALUE;
             }
             SDL_UnlockMutex(is->pictq_mutex);
@@ -1344,7 +1361,7 @@
 
             /* compute nominal last_duration */
             last_duration = vp->pts - is->frame_last_pts;
-            if (last_duration > 0 && last_duration < is->max_frame_duration) {
+            if (!isnan(last_duration) && last_duration > 0 && last_duration < is->max_frame_duration) {
                 /* if duration of the last frame was sane, update last_duration in video state */
                 is->frame_last_duration = last_duration;
             }
@@ -1360,7 +1377,8 @@
                 is->frame_timer += delay * FFMAX(1, floor((time-is->frame_timer) / delay));
 
             SDL_LockMutex(is->pictq_mutex);
-            update_video_pts(is, vp->pts, vp->pos, vp->serial);
+            if (!isnan(vp->pts))
+                update_video_pts(is, vp->pts, vp->pos, vp->serial);
             SDL_UnlockMutex(is->pictq_mutex);
 
             if (is->pictq_size > 1) {
@@ -1477,10 +1495,6 @@
     if (vp->bmp)
         SDL_FreeYUVOverlay(vp->bmp);
 
-#if CONFIG_AVFILTER
-    avfilter_unref_bufferp(&vp->picref);
-#endif
-
     video_open(is, 0, vp);
 
     vp->bmp = SDL_CreateYUVOverlay(vp->width, vp->height,
@@ -1543,11 +1557,7 @@
 
     vp = &is->pictq[is->pictq_windex];
 
-#if CONFIG_AVFILTER
-    vp->sample_aspect_ratio = ((AVFilterBufferRef *)src_frame->opaque)->video->sample_aspect_ratio;
-#else
-    vp->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, src_frame);
-#endif
+    vp->sar = src_frame->sample_aspect_ratio;
 
     /* alloc or resize hardware picture buffer */
     if (!vp->bmp || vp->reallocate || !vp->allocated ||
@@ -1586,10 +1596,6 @@
     /* if the frame is not skipped, then display it */
     if (vp->bmp) {
         AVPicture pict = { { 0 } };
-#if CONFIG_AVFILTER
-        avfilter_unref_bufferp(&vp->picref);
-        vp->picref = src_frame->opaque;
-#endif
 
         /* get a pointer on the bitmap */
         SDL_LockYUVOverlay (vp->bmp);
@@ -1637,7 +1643,7 @@
     return 0;
 }
 
-static int get_video_frame(VideoState *is, AVFrame *frame, int64_t *pts, AVPacket *pkt, int *serial)
+static int get_video_frame(VideoState *is, AVFrame *frame, AVPacket *pkt, int *serial)
 {
     int got_picture;
 
@@ -1658,7 +1664,6 @@
         is->frame_timer = (double)av_gettime() / 1000000.0;
         is->frame_last_dropped_pts = AV_NOPTS_VALUE;
         SDL_UnlockMutex(is->pictq_mutex);
-
         return 0;
     }
 
@@ -1667,31 +1672,35 @@
 
     if (got_picture) {
         int ret = 1;
+        double dpts = NAN;
 
         if (decoder_reorder_pts == -1) {
-            *pts = av_frame_get_best_effort_timestamp(frame);
+            frame->pts = av_frame_get_best_effort_timestamp(frame);
         } else if (decoder_reorder_pts) {
-            *pts = frame->pkt_pts;
+            frame->pts = frame->pkt_pts;
         } else {
-            *pts = frame->pkt_dts;
+            frame->pts = frame->pkt_dts;
         }
 
-        if (*pts == AV_NOPTS_VALUE) {
-            *pts = 0;
-        }
+        if (frame->pts != AV_NOPTS_VALUE)
+            dpts = av_q2d(is->video_st->time_base) * frame->pts;
+
+        frame->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, frame);
 
         if (framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) {
             SDL_LockMutex(is->pictq_mutex);
-            if (is->frame_last_pts != AV_NOPTS_VALUE && *pts) {
+            if (is->frame_last_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE) {
                 double clockdiff = get_video_clock(is) - get_master_clock(is);
-                double dpts = av_q2d(is->video_st->time_base) * *pts;
                 double ptsdiff = dpts - is->frame_last_pts;
                 if (!isnan(clockdiff) && fabs(clockdiff) < AV_NOSYNC_THRESHOLD &&
-                     ptsdiff > 0 && ptsdiff < AV_NOSYNC_THRESHOLD &&
-                     clockdiff + ptsdiff - is->frame_last_filter_delay < 0) {
+                    !isnan(ptsdiff) && ptsdiff > 0 && ptsdiff < AV_NOSYNC_THRESHOLD &&
+                    clockdiff + ptsdiff - is->frame_last_filter_delay < 0 &&
+                    is->videoq.nb_packets) {
                     is->frame_last_dropped_pos = pkt->pos;
                     is->frame_last_dropped_pts = dpts;
+                    is->frame_last_dropped_serial = *serial;
                     is->frame_drops_early++;
+                    av_frame_unref(frame);
                     ret = 0;
                 }
             }
@@ -1748,12 +1757,9 @@
     char sws_flags_str[128];
     char buffersrc_args[256];
     int ret;
-    AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
     AVFilterContext *filt_src = NULL, *filt_out = NULL, *filt_crop;
     AVCodecContext *codec = is->video_st->codec;
-
-    if (!buffersink_params)
-        return AVERROR(ENOMEM);
+    AVRational fr = av_guess_frame_rate(is->ic, is->video_st, NULL);
 
     av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
     snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%"PRId64, sws_flags);
@@ -1764,6 +1770,8 @@
              frame->width, frame->height, frame->format,
              is->video_st->time_base.num, is->video_st->time_base.den,
              codec->sample_aspect_ratio.num, FFMAX(codec->sample_aspect_ratio.den, 1));
+    if (fr.num && fr.den)
+        av_strlcatf(buffersrc_args, sizeof(buffersrc_args), ":frame_rate=%d/%d", fr.num, fr.den);
 
     if ((ret = avfilter_graph_create_filter(&filt_src,
                                             avfilter_get_by_name("buffer"),
@@ -1771,13 +1779,15 @@
                                             graph)) < 0)
         goto fail;
 
-    buffersink_params->pixel_fmts = pix_fmts;
     ret = avfilter_graph_create_filter(&filt_out,
-                                       avfilter_get_by_name("ffbuffersink"),
-                                       "ffplay_buffersink", NULL, buffersink_params, graph);
+                                       avfilter_get_by_name("buffersink"),
+                                       "ffplay_buffersink", NULL, NULL, graph);
     if (ret < 0)
         goto fail;
 
+    if ((ret = av_opt_set_int_list(filt_out, "pix_fmts", pix_fmts,  AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN)) < 0)
+        goto fail;
+
     /* SDL YUV code is not handling odd width/height for some driver
      * combinations, therefore we crop the picture to an even width/height. */
     if ((ret = avfilter_graph_create_filter(&filt_crop,
@@ -1794,54 +1804,106 @@
     is->out_video_filter = filt_out;
 
 fail:
-    av_freep(&buffersink_params);
     return ret;
 }
 
+static int configure_audio_filters(VideoState *is, const char *afilters, int force_output_format)
+{
+    static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE };
+    int sample_rates[2] = { 0, -1 };
+    int64_t channel_layouts[2] = { 0, -1 };
+    int channels[2] = { 0, -1 };
+    AVFilterContext *filt_asrc = NULL, *filt_asink = NULL;
+    char asrc_args[256];
+    int ret;
+
+    avfilter_graph_free(&is->agraph);
+    if (!(is->agraph = avfilter_graph_alloc()))
+        return AVERROR(ENOMEM);
+
+    ret = snprintf(asrc_args, sizeof(asrc_args),
+                   "sample_rate=%d:sample_fmt=%s:channels=%d:time_base=%d/%d",
+                   is->audio_filter_src.freq, av_get_sample_fmt_name(is->audio_filter_src.fmt),
+                   is->audio_filter_src.channels,
+                   1, is->audio_filter_src.freq);
+    if (is->audio_filter_src.channel_layout)
+        snprintf(asrc_args + ret, sizeof(asrc_args) - ret,
+                 ":channel_layout=0x%"PRIx64,  is->audio_filter_src.channel_layout);
+
+    ret = avfilter_graph_create_filter(&filt_asrc,
+                                       avfilter_get_by_name("abuffer"), "ffplay_abuffer",
+                                       asrc_args, NULL, is->agraph);
+    if (ret < 0)
+        goto end;
+
+
+    ret = avfilter_graph_create_filter(&filt_asink,
+                                       avfilter_get_by_name("abuffersink"), "ffplay_abuffersink",
+                                       NULL, NULL, is->agraph);
+    if (ret < 0)
+        goto end;
+
+    if ((ret = av_opt_set_int_list(filt_asink, "sample_fmts", sample_fmts,  AV_SAMPLE_FMT_NONE, AV_OPT_SEARCH_CHILDREN)) < 0)
+        goto end;
+    if ((ret = av_opt_set_int(filt_asink, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
+        goto end;
+
+    if (force_output_format) {
+        channel_layouts[0] = is->audio_tgt.channel_layout;
+        channels       [0] = is->audio_tgt.channels;
+        sample_rates   [0] = is->audio_tgt.freq;
+        if ((ret = av_opt_set_int(filt_asink, "all_channel_counts", 0, AV_OPT_SEARCH_CHILDREN)) < 0)
+            goto end;
+        if ((ret = av_opt_set_int_list(filt_asink, "channel_layouts", channel_layouts,  -1, AV_OPT_SEARCH_CHILDREN)) < 0)
+            goto end;
+        if ((ret = av_opt_set_int_list(filt_asink, "channel_counts" , channels       ,  -1, AV_OPT_SEARCH_CHILDREN)) < 0)
+            goto end;
+        if ((ret = av_opt_set_int_list(filt_asink, "sample_rates"   , sample_rates   ,  -1, AV_OPT_SEARCH_CHILDREN)) < 0)
+            goto end;
+    }
+
+
+    if ((ret = configure_filtergraph(is->agraph, afilters, filt_asrc, filt_asink)) < 0)
+        goto end;
+
+    is->in_audio_filter  = filt_asrc;
+    is->out_audio_filter = filt_asink;
+
+end:
+    if (ret < 0)
+        avfilter_graph_free(&is->agraph);
+    return ret;
+}
 #endif  /* CONFIG_AVFILTER */
 
 static int video_thread(void *arg)
 {
     AVPacket pkt = { 0 };
     VideoState *is = arg;
-    AVFrame *frame = avcodec_alloc_frame();
-    int64_t pts_int = AV_NOPTS_VALUE, pos = -1;
+    AVFrame *frame = av_frame_alloc();
     double pts;
     int ret;
     int serial = 0;
 
 #if CONFIG_AVFILTER
-    AVCodecContext *codec = is->video_st->codec;
     AVFilterGraph *graph = avfilter_graph_alloc();
     AVFilterContext *filt_out = NULL, *filt_in = NULL;
     int last_w = 0;
     int last_h = 0;
     enum AVPixelFormat last_format = -2;
     int last_serial = -1;
-
-    if (codec->codec->capabilities & CODEC_CAP_DR1) {
-        is->use_dr1 = 1;
-        codec->get_buffer     = codec_get_buffer;
-        codec->release_buffer = codec_release_buffer;
-        codec->opaque         = &is->buffer_pool;
-    }
 #endif
 
     for (;;) {
-#if CONFIG_AVFILTER
-        AVFilterBufferRef *picref;
-        AVRational tb;
-#endif
         while (is->paused && !is->videoq.abort_request)
             SDL_Delay(10);
 
         avcodec_get_frame_defaults(frame);
         av_free_packet(&pkt);
 
-        ret = get_video_frame(is, frame, &pts_int, &pkt, &serial);
+        ret = get_video_frame(is, frame, &pkt, &serial);
         if (ret < 0)
             goto the_end;
-
         if (!ret)
             continue;
 
@@ -1874,32 +1936,17 @@
             last_serial = serial;
         }
 
-        frame->pts = pts_int;
-        frame->sample_aspect_ratio = av_guess_sample_aspect_ratio(is->ic, is->video_st, frame);
-        if (is->use_dr1 && frame->opaque) {
-            FrameBuffer      *buf = frame->opaque;
-            AVFilterBufferRef *fb = avfilter_get_video_buffer_ref_from_arrays(
-                                        frame->data, frame->linesize,
-                                        AV_PERM_READ | AV_PERM_PRESERVE,
-                                        frame->width, frame->height,
-                                        frame->format);
-
-            avfilter_copy_frame_props(fb, frame);
-            fb->buf->priv           = buf;
-            fb->buf->free           = filter_release_buffer;
-
-            buf->refcount++;
-            av_buffersrc_add_ref(filt_in, fb, AV_BUFFERSRC_FLAG_NO_COPY);
-
-        } else
-            av_buffersrc_write_frame(filt_in, frame);
-
+        ret = av_buffersrc_add_frame(filt_in, frame);
+        if (ret < 0)
+            goto the_end;
+        av_frame_unref(frame);
+        avcodec_get_frame_defaults(frame);
         av_free_packet(&pkt);
 
         while (ret >= 0) {
             is->frame_last_returned_time = av_gettime() / 1000000.0;
 
-            ret = av_buffersink_get_buffer_ref(filt_out, &picref, 0);
+            ret = av_buffersink_get_frame_flags(filt_out, frame, 0);
             if (ret < 0) {
                 ret = 0;
                 break;
@@ -1909,27 +1956,14 @@
             if (fabs(is->frame_last_filter_delay) > AV_NOSYNC_THRESHOLD / 10.0)
                 is->frame_last_filter_delay = 0;
 
-            avfilter_copy_buf_props(frame, picref);
-
-            pts_int = picref->pts;
-            tb      = filt_out->inputs[0]->time_base;
-            pos     = picref->pos;
-            frame->opaque = picref;
-
-            if (av_cmp_q(tb, is->video_st->time_base)) {
-                av_unused int64_t pts1 = pts_int;
-                pts_int = av_rescale_q(pts_int, tb, is->video_st->time_base);
-                av_dlog(NULL, "video_thread(): "
-                        "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
-                        tb.num, tb.den, pts1,
-                        is->video_st->time_base.num, is->video_st->time_base.den, pts_int);
-            }
-            pts = pts_int * av_q2d(is->video_st->time_base);
-            ret = queue_picture(is, frame, pts, pos, serial);
+            pts = (frame->pts == AV_NOPTS_VALUE) ? NAN : frame->pts * av_q2d(filt_out->inputs[0]->time_base);
+            ret = queue_picture(is, frame, pts, av_frame_get_pkt_pos(frame), serial);
+            av_frame_unref(frame);
         }
 #else
-        pts = pts_int * av_q2d(is->video_st->time_base);
+        pts = (frame->pts == AV_NOPTS_VALUE) ? NAN : frame->pts * av_q2d(is->video_st->time_base);
         ret = queue_picture(is, frame, pts, pkt.pos, serial);
+        av_frame_unref(frame);
 #endif
 
         if (ret < 0)
@@ -1941,7 +1975,7 @@
     avfilter_graph_free(&graph);
 #endif
     av_free_packet(&pkt);
-    avcodec_free_frame(&frame);
+    av_frame_free(&frame);
     return 0;
 }
 
@@ -2089,22 +2123,27 @@
     AVPacket *pkt_temp = &is->audio_pkt_temp;
     AVPacket *pkt = &is->audio_pkt;
     AVCodecContext *dec = is->audio_st->codec;
-    int len1, len2, data_size, resampled_data_size;
+    int len1, data_size, resampled_data_size;
     int64_t dec_channel_layout;
     int got_frame;
     av_unused double audio_clock0;
     int new_packet = 0;
     int flush_complete = 0;
     int wanted_nb_samples;
+    AVRational tb;
+    int ret;
+    int reconfigure;
 
     for (;;) {
         /* NOTE: the audio packet can contain several frames */
-        while (pkt_temp->size > 0 || (!pkt_temp->data && new_packet)) {
+        while (pkt_temp->size > 0 || (!pkt_temp->data && new_packet) || is->audio_buf_frames_pending) {
             if (!is->frame) {
                 if (!(is->frame = avcodec_alloc_frame()))
                     return AVERROR(ENOMEM);
-            } else
+            } else {
+                av_frame_unref(is->frame);
                 avcodec_get_frame_defaults(is->frame);
+            }
 
             if (is->audioq.serial != is->audio_pkt_temp_serial)
                 break;
@@ -2112,32 +2151,88 @@
             if (is->paused)
                 return -1;
 
-            if (flush_complete)
-                break;
-            new_packet = 0;
-            len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp);
-            if (len1 < 0) {
-                /* if error, we skip the frame */
-                pkt_temp->size = 0;
-                break;
-            }
+            if (!is->audio_buf_frames_pending) {
+                if (flush_complete)
+                    break;
+                new_packet = 0;
+                len1 = avcodec_decode_audio4(dec, is->frame, &got_frame, pkt_temp);
+                if (len1 < 0) {
+                    /* if error, we skip the frame */
+                    pkt_temp->size = 0;
+                    break;
+                }
 
-            pkt_temp->data += len1;
-            pkt_temp->size -= len1;
+                pkt_temp->data += len1;
+                pkt_temp->size -= len1;
 
-            if (!got_frame) {
-                /* stop sending empty packets if the decoder is finished */
-                if (!pkt_temp->data && dec->codec->capabilities & CODEC_CAP_DELAY)
-                    flush_complete = 1;
-                continue;
+                if (!got_frame) {
+                    /* stop sending empty packets if the decoder is finished */
+                    if (!pkt_temp->data && dec->codec->capabilities & CODEC_CAP_DELAY)
+                        flush_complete = 1;
+                    continue;
+                }
+
+                tb = (AVRational){1, is->frame->sample_rate};
+                if (is->frame->pts != AV_NOPTS_VALUE)
+                    is->frame->pts = av_rescale_q(is->frame->pts, dec->time_base, tb);
+                if (is->frame->pts == AV_NOPTS_VALUE && pkt_temp->pts != AV_NOPTS_VALUE)
+                    is->frame->pts = av_rescale_q(pkt_temp->pts, is->audio_st->time_base, tb);
+                if (pkt_temp->pts != AV_NOPTS_VALUE)
+                    pkt_temp->pts += (double) is->frame->nb_samples / is->frame->sample_rate / av_q2d(is->audio_st->time_base);
+
+#if CONFIG_AVFILTER
+                dec_channel_layout = get_valid_channel_layout(is->frame->channel_layout, av_frame_get_channels(is->frame));
+
+                reconfigure =
+                    cmp_audio_fmts(is->audio_filter_src.fmt, is->audio_filter_src.channels,
+                                   is->frame->format, av_frame_get_channels(is->frame))    ||
+                    is->audio_filter_src.channel_layout != dec_channel_layout ||
+                    is->audio_filter_src.freq           != is->frame->sample_rate ||
+                    is->audio_pkt_temp_serial           != is->audio_last_serial;
+
+                if (reconfigure) {
+                    char buf1[1024], buf2[1024];
+                    av_get_channel_layout_string(buf1, sizeof(buf1), -1, is->audio_filter_src.channel_layout);
+                    av_get_channel_layout_string(buf2, sizeof(buf2), -1, dec_channel_layout);
+                    av_log(NULL, AV_LOG_DEBUG,
+                           "Audio frame changed from rate:%d ch:%d fmt:%s layout:%s serial:%d to rate:%d ch:%d fmt:%s layout:%s serial:%d\n",
+                           is->audio_filter_src.freq, is->audio_filter_src.channels, av_get_sample_fmt_name(is->audio_filter_src.fmt), buf1, is->audio_last_serial,
+                           is->frame->sample_rate, av_frame_get_channels(is->frame), av_get_sample_fmt_name(is->frame->format), buf2, is->audio_pkt_temp_serial);
+
+                    is->audio_filter_src.fmt            = is->frame->format;
+                    is->audio_filter_src.channels       = av_frame_get_channels(is->frame);
+                    is->audio_filter_src.channel_layout = dec_channel_layout;
+                    is->audio_filter_src.freq           = is->frame->sample_rate;
+                    is->audio_last_serial               = is->audio_pkt_temp_serial;
+
+                    if ((ret = configure_audio_filters(is, afilters, 1)) < 0)
+                        return ret;
+                }
+
+                if ((ret = av_buffersrc_add_frame(is->in_audio_filter, is->frame)) < 0)
+                    return ret;
+                av_frame_unref(is->frame);
+#endif
             }
-            data_size = av_samples_get_buffer_size(NULL, is->frame->channels,
+#if CONFIG_AVFILTER
+            if ((ret = av_buffersink_get_frame_flags(is->out_audio_filter, is->frame, 0)) < 0) {
+                if (ret == AVERROR(EAGAIN)) {
+                    is->audio_buf_frames_pending = 0;
+                    continue;
+                }
+                return ret;
+            }
+            is->audio_buf_frames_pending = 1;
+            tb = is->out_audio_filter->inputs[0]->time_base;
+#endif
+
+            data_size = av_samples_get_buffer_size(NULL, av_frame_get_channels(is->frame),
                                                    is->frame->nb_samples,
                                                    is->frame->format, 1);
 
             dec_channel_layout =
-                (is->frame->channel_layout && is->frame->channels == av_get_channel_layout_nb_channels(is->frame->channel_layout)) ?
-                is->frame->channel_layout : av_get_default_channel_layout(is->frame->channels);
+                (is->frame->channel_layout && av_frame_get_channels(is->frame) == av_get_channel_layout_nb_channels(is->frame->channel_layout)) ?
+                is->frame->channel_layout : av_get_default_channel_layout(av_frame_get_channels(is->frame));
             wanted_nb_samples = synchronize_audio(is, is->frame->nb_samples);
 
             if (is->frame->format        != is->audio_src.fmt            ||
@@ -2151,12 +2246,12 @@
                                                  0, NULL);
                 if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
                     fprintf(stderr, "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
-                            is->frame->sample_rate, av_get_sample_fmt_name(is->frame->format), is->frame->channels,
+                            is->frame->sample_rate, av_get_sample_fmt_name(is->frame->format), av_frame_get_channels(is->frame),
                             is->audio_tgt.freq, av_get_sample_fmt_name(is->audio_tgt.fmt), is->audio_tgt.channels);
                     break;
                 }
                 is->audio_src.channel_layout = dec_channel_layout;
-                is->audio_src.channels = is->frame->channels;
+                is->audio_src.channels       = av_frame_get_channels(is->frame);
                 is->audio_src.freq = is->frame->sample_rate;
                 is->audio_src.fmt = is->frame->format;
             }
@@ -2166,6 +2261,11 @@
                 uint8_t **out = &is->audio_buf1;
                 int out_count = (int64_t)wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate + 256;
                 int out_size  = av_samples_get_buffer_size(NULL, is->audio_tgt.channels, out_count, is->audio_tgt.fmt, 0);
+                int len2;
+                if (out_size < 0) {
+                    fprintf(stderr, "av_samples_get_buffer_size() failed\n");
+                    break;
+                }
                 if (wanted_nb_samples != is->frame->nb_samples) {
                     if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - is->frame->nb_samples) * is->audio_tgt.freq / is->frame->sample_rate,
                                                 wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate) < 0) {
@@ -2193,8 +2293,11 @@
             }
 
             audio_clock0 = is->audio_clock;
-            is->audio_clock += (double)data_size /
-                (is->frame->channels * is->frame->sample_rate * av_get_bytes_per_sample(is->frame->format));
+            /* update the audio clock with the pts */
+            if (is->frame->pts != AV_NOPTS_VALUE) {
+                is->audio_clock = is->frame->pts * av_q2d(tb) + (double) is->frame->nb_samples / is->frame->sample_rate;
+                is->audio_clock_serial = is->audio_pkt_temp_serial;
+            }
 #ifdef DEBUG
             {
                 static double last_clock;
@@ -2226,15 +2329,10 @@
         if (pkt->data == flush_pkt.data) {
             avcodec_flush_buffers(dec);
             flush_complete = 0;
+            is->audio_buf_frames_pending = 0;
         }
 
         *pkt_temp = *pkt;
-
-        /* if update the audio clock with the pts */
-        if (pkt->pts != AV_NOPTS_VALUE) {
-            is->audio_clock = av_q2d(is->audio_st->time_base)*pkt->pts;
-            is->audio_clock_serial = is->audio_pkt_temp_serial;
-        }
     }
 }
 
@@ -2342,6 +2440,9 @@
     const char *forced_codec_name = NULL;
     AVDictionary *opts;
     AVDictionaryEntry *t = NULL;
+    int sample_rate, nb_channels;
+    int64_t channel_layout;
+    int ret;
 
     if (stream_index < 0 || stream_index >= ic->nb_streams)
         return -1;
@@ -2371,9 +2472,6 @@
         avctx->lowres= codec->max_lowres;
     }
     avctx->idct_algo         = idct;
-    avctx->skip_frame        = skip_frame;
-    avctx->skip_idct         = skip_idct;
-    avctx->skip_loop_filter  = skip_loop_filter;
     avctx->error_concealment = error_concealment;
 
     if(avctx->lowres) avctx->flags |= CODEC_FLAG_EMU_EDGE;
@@ -2384,6 +2482,8 @@
     opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec);
     if (!av_dict_get(opts, "threads", NULL, 0))
         av_dict_set(&opts, "threads", "auto", 0);
+    if (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
+        av_dict_set(&opts, "refcounted_frames", "1", 0);
     if (avcodec_open2(avctx, codec, &opts) < 0)
         return -1;
     if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
@@ -2391,20 +2491,35 @@
         return AVERROR_OPTION_NOT_FOUND;
     }
 
-    /* prepare audio output */
-    if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
-        int audio_hw_buf_size = audio_open(is, avctx->channel_layout, avctx->channels, avctx->sample_rate, &is->audio_src);
-        if (audio_hw_buf_size < 0)
-            return -1;
-        is->audio_hw_buf_size = audio_hw_buf_size;
-        is->audio_tgt = is->audio_src;
-    }
-
     ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
     switch (avctx->codec_type) {
     case AVMEDIA_TYPE_AUDIO:
-        is->audio_stream = stream_index;
-        is->audio_st = ic->streams[stream_index];
+#if CONFIG_AVFILTER
+        {
+            AVFilterLink *link;
+
+            is->audio_filter_src.freq           = avctx->sample_rate;
+            is->audio_filter_src.channels       = avctx->channels;
+            is->audio_filter_src.channel_layout = get_valid_channel_layout(avctx->channel_layout, avctx->channels);
+            is->audio_filter_src.fmt            = avctx->sample_fmt;
+            if ((ret = configure_audio_filters(is, afilters, 0)) < 0)
+                return ret;
+            link = is->out_audio_filter->inputs[0];
+            sample_rate    = link->sample_rate;
+            nb_channels    = link->channels;
+            channel_layout = link->channel_layout;
+        }
+#else
+        sample_rate    = avctx->sample_rate;
+        nb_channels    = avctx->channels;
+        channel_layout = avctx->channel_layout;
+#endif
+
+        /* prepare audio output */
+        if ((ret = audio_open(is, channel_layout, nb_channels, sample_rate, &is->audio_tgt)) < 0)
+            return ret;
+        is->audio_hw_buf_size = ret;
+        is->audio_src = is->audio_tgt;
         is->audio_buf_size  = 0;
         is->audio_buf_index = 0;
 
@@ -2417,6 +2532,10 @@
 
         memset(&is->audio_pkt, 0, sizeof(is->audio_pkt));
         memset(&is->audio_pkt_temp, 0, sizeof(is->audio_pkt_temp));
+
+        is->audio_stream = stream_index;
+        is->audio_st = ic->streams[stream_index];
+
         packet_queue_start(&is->audioq);
         SDL_PauseAudio(0);
         break;
@@ -2426,6 +2545,7 @@
 
         packet_queue_start(&is->videoq);
         is->video_tid = SDL_CreateThread(video_thread, is);
+        is->queue_attachments_req = 1;
         break;
     case AVMEDIA_TYPE_SUBTITLE:
         is->subtitle_stream = stream_index;
@@ -2461,7 +2581,7 @@
         av_freep(&is->audio_buf1);
         is->audio_buf1_size = 0;
         is->audio_buf = NULL;
-        avcodec_free_frame(&is->frame);
+        av_frame_free(&is->frame);
 
         if (is->rdft) {
             av_rdft_end(is->rdft);
@@ -2469,6 +2589,9 @@
             is->rdft = NULL;
             is->rdft_bits = 0;
         }
+#if CONFIG_AVFILTER
+        avfilter_graph_free(&is->agraph);
+#endif
         break;
     case AVMEDIA_TYPE_VIDEO:
         packet_queue_abort(&is->videoq);
@@ -2504,9 +2627,6 @@
 
     ic->streams[stream_index]->discard = AVDISCARD_ALL;
     avcodec_close(avctx);
-#if CONFIG_AVFILTER
-    free_buffer_pool(&is->buffer_pool);
-#endif
     switch (avctx->codec_type) {
     case AVMEDIA_TYPE_AUDIO:
         is->audio_st = NULL;
@@ -2603,10 +2723,13 @@
         ic->pb->eof_reached = 0; // FIXME hack, ffplay maybe should not use url_feof() to test for the end
 
     if (seek_by_bytes < 0)
-        seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT);
+        seek_by_bytes = !!(ic->iformat->flags & AVFMT_TS_DISCONT) && strcmp("ogg", ic->iformat->name);
 
     is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;
 
+    if (!window_title && (t = av_dict_get(ic->metadata, "title", NULL, 0)))
+        window_title = av_asprintf("%s - %s", t->value, input_filename);
+
     /* if seeking requested, we execute it */
     if (start_time != AV_NOPTS_VALUE) {
         int64_t timestamp;
@@ -2725,12 +2848,18 @@
                 }
             }
             is->seek_req = 0;
+            is->queue_attachments_req = 1;
             eof = 0;
             if (is->paused)
                 step_to_next_frame(is);
         }
         if (is->queue_attachments_req) {
-            avformat_queue_attached_pictures(ic);
+            if (is->video_st && is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC) {
+                AVPacket copy;
+                if ((ret = av_copy_packet(&copy, &is->video_st->attached_pic)) < 0)
+                    goto fail;
+                packet_queue_put(&is->videoq, &copy);
+            }
             is->queue_attachments_req = 0;
         }
 
@@ -2738,7 +2867,8 @@
         if (infinite_buffer<1 &&
               (is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
             || (   (is->audioq   .nb_packets > MIN_FRAMES || is->audio_stream < 0 || is->audioq.abort_request)
-                && (is->videoq   .nb_packets > MIN_FRAMES || is->video_stream < 0 || is->videoq.abort_request)
+                && (is->videoq   .nb_packets > MIN_FRAMES || is->video_stream < 0 || is->videoq.abort_request
+                    || (is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC))
                 && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0 || is->subtitleq.abort_request)))) {
             /* wait 10 ms */
             SDL_LockMutex(wait_mutex);
@@ -2793,7 +2923,8 @@
                 <= ((double)duration / 1000000);
         if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
             packet_queue_put(&is->audioq, pkt);
-        } else if (pkt->stream_index == is->video_stream && pkt_in_play_range) {
+        } else if (pkt->stream_index == is->video_stream && pkt_in_play_range
+                   && !(is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC)) {
             packet_queue_put(&is->videoq, pkt);
         } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
             packet_queue_put(&is->subtitleq, pkt);
@@ -2861,6 +2992,7 @@
     is->video_current_pts_drift = is->audio_current_pts_drift;
     is->audio_clock_serial = -1;
     is->video_clock_serial = -1;
+    is->audio_last_serial = -1;
     is->av_sync_type = av_sync_type;
     is->read_tid     = SDL_CreateThread(read_thread, is);
     if (!is->read_tid) {
@@ -2923,8 +3055,6 @@
  the_end:
     stream_component_close(is, old_index);
     stream_component_open(is, stream_index);
-    if (codec_type == AVMEDIA_TYPE_VIDEO)
-        is->queue_attachments_req = 1;
 }
 
 
@@ -3254,9 +3384,6 @@
     { "genpts", OPT_BOOL | OPT_EXPERT, { &genpts }, "generate pts", "" },
     { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { &decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
     { "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, { &lowres }, "", "" },
-    { "skiploop", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_loop_filter }, "", "" },
-    { "skipframe", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_frame }, "", "" },
-    { "skipidct", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_idct }, "", "" },
     { "idct", OPT_INT | HAS_ARG | OPT_EXPERT, { &idct }, "set idct algo",  "algo" },
     { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, { &error_concealment }, "set error concealment options",  "bit_mask" },
     { "sync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
@@ -3269,6 +3396,7 @@
     { "window_title", OPT_STRING | HAS_ARG, { &window_title }, "set window title", "window title" },
 #if CONFIG_AVFILTER
     { "vf", OPT_STRING | HAS_ARG, { &vfilters }, "set video filters", "filter_graph" },
+    { "af", OPT_STRING | HAS_ARG, { &afilters }, "set audio filters", "filter_graph" },
 #endif
     { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, { &rdftspeed }, "rdft speed", "msecs" },
     { "showmode", HAS_ARG, { .func_arg = opt_show_mode}, "select show mode (0 = video, 1 = waves, 2 = RDFT)", "mode" },
@@ -3392,11 +3520,9 @@
     }
 
     if (!display_disable) {
-#if HAVE_SDL_VIDEO_SIZE
         const SDL_VideoInfo *vi = SDL_GetVideoInfo();
         fs_screen_width = vi->current_w;
         fs_screen_height = vi->current_h;
-#endif
     }
 
     SDL_EventState(SDL_ACTIVEEVENT, SDL_IGNORE);
diff --git a/ffprobe.c b/ffprobe.c
index f70c24c..4d2d3e1 100644
--- a/ffprobe.c
+++ b/ffprobe.c
@@ -300,7 +300,7 @@
 {
     int i, ret = 0;
 
-    if (!(*wctx = av_malloc(sizeof(WriterContext)))) {
+    if (!(*wctx = av_mallocz(sizeof(WriterContext)))) {
         ret = AVERROR(ENOMEM);
         goto fail;
     }
@@ -1472,11 +1472,11 @@
     print_time("pkt_pts_time",          frame->pkt_pts, &stream->time_base);
     print_ts  ("pkt_dts",               frame->pkt_dts);
     print_time("pkt_dts_time",          frame->pkt_dts, &stream->time_base);
-    print_duration_ts  ("pkt_duration",      frame->pkt_duration);
-    print_duration_time("pkt_duration_time", frame->pkt_duration, &stream->time_base);
-    if (frame->pkt_pos != -1) print_fmt    ("pkt_pos", "%"PRId64, frame->pkt_pos);
+    print_duration_ts  ("pkt_duration",      av_frame_get_pkt_duration(frame));
+    print_duration_time("pkt_duration_time", av_frame_get_pkt_duration(frame), &stream->time_base);
+    if (av_frame_get_pkt_pos (frame) != -1) print_fmt    ("pkt_pos", "%"PRId64, av_frame_get_pkt_pos(frame));
     else                      print_str_opt("pkt_pos", "N/A");
-    if (frame->pkt_size != -1) print_fmt    ("pkt_size", "%d", av_frame_get_pkt_size(frame));
+    if (av_frame_get_pkt_size(frame) != -1) print_fmt    ("pkt_size", "%d", av_frame_get_pkt_size(frame));
     else                       print_str_opt("pkt_size", "N/A");
 
     switch (stream->codec->codec_type) {
@@ -1500,7 +1500,6 @@
         print_int("interlaced_frame",       frame->interlaced_frame);
         print_int("top_field_first",        frame->top_field_first);
         print_int("repeat_pict",            frame->repeat_pict);
-        print_int("reference",              frame->reference);
         break;
 
     case AVMEDIA_TYPE_AUDIO:
@@ -1793,9 +1792,10 @@
 
 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
 {
-    int err, i;
+    int err, i, orig_nb_streams;
     AVFormatContext *fmt_ctx = NULL;
     AVDictionaryEntry *t;
+    AVDictionary **opts;
 
     if ((err = avformat_open_input(&fmt_ctx, filename,
                                    iformat, &format_opts)) < 0) {
@@ -1807,12 +1807,17 @@
         return AVERROR_OPTION_NOT_FOUND;
     }
 
-
     /* fill the streams in the format context */
-    if ((err = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
+    opts = setup_find_stream_info_opts(fmt_ctx, codec_opts);
+    orig_nb_streams = fmt_ctx->nb_streams;
+
+    if ((err = avformat_find_stream_info(fmt_ctx, opts)) < 0) {
         print_error(filename, err);
         return err;
     }
+    for (i = 0; i < orig_nb_streams; i++)
+        av_dict_free(&opts[i]);
+    av_freep(&opts);
 
     av_dump_format(fmt_ctx, 0, filename, 0);
 
@@ -1829,9 +1834,18 @@
             av_log(NULL, AV_LOG_ERROR,
                     "Unsupported codec with id %d for input stream %d\n",
                     stream->codec->codec_id, stream->index);
-        } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
-            av_log(NULL, AV_LOG_ERROR, "Error while opening codec for input stream %d\n",
-                   stream->index);
+        } else {
+            AVDictionary *opts = filter_codec_opts(codec_opts, stream->codec->codec_id,
+                                                   fmt_ctx, stream, codec);
+            if (avcodec_open2(stream->codec, codec, &opts) < 0) {
+                av_log(NULL, AV_LOG_ERROR, "Error while opening codec for input stream %d\n",
+                       stream->index);
+            }
+            if ((t = av_dict_get(opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
+                av_log(NULL, AV_LOG_ERROR, "Option %s for input stream %d not found\n",
+                       t->key, stream->index);
+                return AVERROR_OPTION_NOT_FOUND;
+            }
         }
     }
 
diff --git a/ffserver.c b/ffserver.c
index f2cf67f..ac9627f 100644
--- a/ffserver.c
+++ b/ffserver.c
@@ -308,7 +308,7 @@
 
 static const char *my_program_name;
 
-static const char *config_filename = "/etc/ffserver.conf";
+static const char *config_filename;
 
 static int ffserver_debug;
 static int no_launch;
@@ -396,14 +396,14 @@
     return 0;
 }
 
-static char *ctime1(char *buf2)
+static char *ctime1(char *buf2, int buf_size)
 {
     time_t ti;
     char *p;
 
     ti = time(NULL);
     p = ctime(&ti);
-    strcpy(buf2, p);
+    av_strlcpy(buf2, p, buf_size);
     p = buf2 + strlen(p) - 1;
     if (*p == '\n')
         *p = '\0';
@@ -416,7 +416,7 @@
     if (logfile) {
         if (print_prefix) {
             char buf[32];
-            ctime1(buf);
+            ctime1(buf, sizeof(buf));
             fprintf(logfile, "%s ", buf);
         }
         print_prefix = strstr(fmt, "\n") != NULL;
@@ -1125,7 +1125,7 @@
         if (av_strncasecmp(p, "Pragma:", 7) == 0) {
             const char *q = p + 7;
 
-            while (*q && *q != '\n' && isspace(*q))
+            while (*q && *q != '\n' && av_isspace(*q))
                 q++;
 
             if (av_strncasecmp(q, "stream-switch-entry=", 20) == 0) {
@@ -1147,7 +1147,7 @@
                     if (stream_no < ratelen && stream_no >= 0)
                         rates[stream_no] = rate_no;
 
-                    while (*q && *q != '\n' && !isspace(*q))
+                    while (*q && *q != '\n' && !av_isspace(*q))
                         q++;
                 }
 
@@ -1258,7 +1258,7 @@
     p = *pp;
     skip_spaces(&p);
     q = buf;
-    while (!isspace(*p) && *p != '\0') {
+    while (!av_isspace(*p) && *p != '\0') {
         if ((q - buf) < buf_size - 1)
             *q++ = *p;
         p++;
@@ -1275,7 +1275,7 @@
     int quote;
 
     p = *pp;
-    while (isspace(*p)) p++;
+    while (av_isspace(*p)) p++;
     q = buf;
     quote = 0;
     if (*p == '\"' || *p == '\'')
@@ -1285,7 +1285,7 @@
             if (*p == quote)
                 break;
         } else {
-            if (isspace(*p))
+            if (av_isspace(*p))
                 break;
         }
         if (*p == '\0')
@@ -1389,7 +1389,7 @@
             break;
         line_num++;
         p = line;
-        while (isspace(*p))
+        while (av_isspace(*p))
             p++;
         if (*p == '\0' || *p == '#')
             continue;
@@ -1540,7 +1540,7 @@
     for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
         if (av_strncasecmp(p, "User-Agent:", 11) == 0) {
             useragent = p + 11;
-            if (*useragent && *useragent != '\n' && isspace(*useragent))
+            if (*useragent && *useragent != '\n' && av_isspace(*useragent))
                 useragent++;
             break;
         }
@@ -1668,7 +1668,7 @@
             char *eoh;
             char hostbuf[260];
 
-            while (isspace(*hostinfo))
+            while (av_isspace(*hostinfo))
                 hostinfo++;
 
             eoh = strchr(hostinfo, '\n');
@@ -4100,7 +4100,7 @@
             break;
         line_num++;
         p = line;
-        while (isspace(*p))
+        while (av_isspace(*p))
             p++;
         if (*p == '\0' || *p == '#')
             continue;
@@ -4237,7 +4237,7 @@
                 get_arg(arg, sizeof(arg), &p);
                 p1 = arg;
                 fsize = strtod(p1, &p1);
-                switch(toupper(*p1)) {
+                switch(av_toupper(*p1)) {
                 case 'K':
                     fsize *= 1024;
                     break;
@@ -4545,14 +4545,6 @@
                     ERROR("VideoQMin out of range\n");
                 }
             }
-        } else if (!av_strcasecmp(cmd, "LumaElim")) {
-            get_arg(arg, sizeof(arg), &p);
-            if (stream)
-                video_enc.luma_elim_threshold = atoi(arg);
-        } else if (!av_strcasecmp(cmd, "ChromaElim")) {
-            get_arg(arg, sizeof(arg), &p);
-            if (stream)
-                video_enc.chroma_elim_threshold = atoi(arg);
         } else if (!av_strcasecmp(cmd, "LumiMask")) {
             get_arg(arg, sizeof(arg), &p);
             if (stream)
@@ -4724,6 +4716,9 @@
 
     parse_options(NULL, argc, argv, options, NULL);
 
+    if (!config_filename)
+        config_filename = av_strdup("/etc/ffserver.conf");
+
     unsetenv("http_proxy");             /* Kill the http_proxy */
 
     av_lfg_init(&random_state, av_get_random_seed());
@@ -4736,6 +4731,7 @@
         fprintf(stderr, "Incorrect config file - exiting.\n");
         exit(1);
     }
+    av_freep(&config_filename);
 
     /* open log file if needed */
     if (logfilename[0] != '\0') {
diff --git a/libavcodec/012v.c b/libavcodec/012v.c
index 6f4533b..58e3cd6 100644
--- a/libavcodec/012v.c
+++ b/libavcodec/012v.c
@@ -26,18 +26,12 @@
 
 static av_cold int zero12v_decode_init(AVCodecContext *avctx)
 {
-    avctx->pix_fmt             = PIX_FMT_YUV422P16;
+    avctx->pix_fmt             = AV_PIX_FMT_YUV422P16;
     avctx->bits_per_raw_sample = 10;
 
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame)
-        return AVERROR(ENOMEM);
-
     if (avctx->codec_tag == MKTAG('a', '1', '2', 'v'))
-        av_log_ask_for_sample(avctx, "Samples with actual transparency needed\n");
+        avpriv_request_sample(avctx, "transparency");
 
-    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
-    avctx->coded_frame->key_frame = 1;
     return 0;
 }
 
@@ -46,14 +40,11 @@
 {
     int line = 0, ret;
     const int width = avctx->width;
-    AVFrame *pic = avctx->coded_frame;
+    AVFrame *pic = data;
     uint16_t *y, *u, *v;
     const uint8_t *line_end, *src = avpkt->data;
     int stride = avctx->width * 8 / 3;
 
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
-
     if (width == 1) {
         av_log(avctx, AV_LOG_ERROR, "Width 1 not supported.\n");
         return AVERROR_INVALIDDATA;
@@ -64,10 +55,12 @@
         return AVERROR_INVALIDDATA;
     }
 
-    pic->reference = 0;
-    if ((ret = ff_get_buffer(avctx, pic)) < 0)
+    if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
         return ret;
 
+    pic->pict_type = AV_PICTURE_TYPE_I;
+    pic->key_frame = 1;
+
     y = (uint16_t *)pic->data[0];
     u = (uint16_t *)pic->data[1];
     v = (uint16_t *)pic->data[2];
@@ -145,27 +138,15 @@
     }
 
     *got_frame = 1;
-    *(AVFrame*)data= *avctx->coded_frame;
 
     return avpkt->size;
 }
 
-static av_cold int zero12v_decode_close(AVCodecContext *avctx)
-{
-    AVFrame *pic = avctx->coded_frame;
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
-    av_freep(&avctx->coded_frame);
-
-    return 0;
-}
-
 AVCodec ff_zero12v_decoder = {
     .name           = "012v",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_012V,
     .init           = zero12v_decode_init,
-    .close          = zero12v_decode_close,
     .decode         = zero12v_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
diff --git a/libavcodec/4xm.c b/libavcodec/4xm.c
index e3d8486..cd22aa4 100644
--- a/libavcodec/4xm.c
+++ b/libavcodec/4xm.c
@@ -24,6 +24,7 @@
  * 4XM codec.
  */
 
+#include "libavutil/frame.h"
 #include "libavutil/intreadwrite.h"
 #include "avcodec.h"
 #include "bytestream.h"
@@ -255,15 +256,15 @@
     }
 }
 
-static void init_mv(FourXContext *f)
+static void init_mv(FourXContext *f, int linesize)
 {
     int i;
 
     for (i = 0; i < 256; i++) {
         if (f->version > 1)
-            f->mv[i] = mv[i][0] + mv[i][1] * f->current_picture->linesize[0] / 2;
+            f->mv[i] = mv[i][0] + mv[i][1] * linesize / 2;
         else
-            f->mv[i] = (i & 15) - 8 + ((i >> 4) - 8) * f->current_picture->linesize[0] / 2;
+            f->mv[i] = (i & 15) - 8 + ((i >> 4) - 8) * linesize / 2;
     }
 }
 
@@ -404,14 +405,15 @@
     }
 }
 
-static int decode_p_frame(FourXContext *f, const uint8_t *buf, int length)
+static int decode_p_frame(FourXContext *f, AVFrame *frame,
+                          const uint8_t *buf, int length)
 {
     int x, y;
     const int width  = f->avctx->width;
     const int height = f->avctx->height;
     uint16_t *src    = (uint16_t *)f->last_picture->data[0];
-    uint16_t *dst    = (uint16_t *)f->current_picture->data[0];
-    const int stride =             f->current_picture->linesize[0] >> 1;
+    uint16_t *dst    = (uint16_t *)frame->data[0];
+    const int stride =             frame->linesize[0] >> 1;
     unsigned int bitstream_size, bytestream_size, wordstream_size, extra,
                  bytestream_offset, wordstream_offset;
 
@@ -455,7 +457,7 @@
     bytestream2_init(&f->g, buf + bytestream_offset,
                      length - bytestream_offset);
 
-    init_mv(f);
+    init_mv(f, frame->linesize[0]);
 
     for (y = 0; y < height; y += 8) {
         for (x = 0; x < width; x += 8)
@@ -519,12 +521,12 @@
     return 0;
 }
 
-static inline void idct_put(FourXContext *f, int x, int y)
+static inline void idct_put(FourXContext *f, AVFrame *frame, int x, int y)
 {
     int16_t (*block)[64] = f->block;
-    int stride           = f->current_picture->linesize[0] >> 1;
+    int stride           = frame->linesize[0] >> 1;
     int i;
-    uint16_t *dst = ((uint16_t*)f->current_picture->data[0]) + y * stride + x;
+    uint16_t *dst = ((uint16_t*)frame->data[0]) + y * stride + x;
 
     for (i = 0; i < 4; i++) {
         block[i][0] += 0x80 * 8 * 8;
@@ -682,14 +684,14 @@
     return red / 3 * 1024 + green / 3 * 32 + blue / 3;
 }
 
-static int decode_i2_frame(FourXContext *f, const uint8_t *buf, int length)
+static int decode_i2_frame(FourXContext *f, AVFrame *frame, const uint8_t *buf, int length)
 {
     int x, y, x2, y2;
     const int width  = f->avctx->width;
     const int height = f->avctx->height;
     const int mbs    = (FFALIGN(width, 16) >> 4) * (FFALIGN(height, 16) >> 4);
-    uint16_t *dst    = (uint16_t*)f->current_picture->data[0];
-    const int stride =            f->current_picture->linesize[0]>>1;
+    uint16_t *dst    = (uint16_t*)frame->data[0];
+    const int stride =            frame->linesize[0]>>1;
     const uint8_t *buf_end = buf + length;
     GetByteContext g3;
 
@@ -731,7 +733,7 @@
     return 0;
 }
 
-static int decode_i_frame(FourXContext *f, const uint8_t *buf, int length)
+static int decode_i_frame(FourXContext *f, AVFrame *frame, const uint8_t *buf, int length)
 {
     int x, y, ret;
     const int width  = f->avctx->width;
@@ -785,7 +787,7 @@
             if ((ret = decode_i_mb(f)) < 0)
                 return ret;
 
-            idct_put(f, x, y);
+            idct_put(f, frame, x, y);
         }
     }
 
@@ -802,7 +804,6 @@
     int buf_size          = avpkt->size;
     FourXContext *const f = avctx->priv_data;
     AVFrame *picture      = data;
-    AVFrame *p;
     int i, frame_4cc, frame_size, ret;
 
     if (buf_size < 12)
@@ -880,43 +881,35 @@
 
     FFSWAP(AVFrame*, f->current_picture, f->last_picture);
 
-    p                  = f->current_picture;
-    avctx->coded_frame = p;
-
     // alternatively we would have to use our own buffer management
     avctx->flags |= CODEC_FLAG_EMU_EDGE;
 
-    p->reference= 3;
-    if ((ret = avctx->reget_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, f->current_picture)) < 0)
         return ret;
-    }
 
     if (frame_4cc == AV_RL32("ifr2")) {
-        p->pict_type= AV_PICTURE_TYPE_I;
-        if ((ret = decode_i2_frame(f, buf - 4, frame_size + 4)) < 0) {
+        f->current_picture->pict_type = AV_PICTURE_TYPE_I;
+        if ((ret = decode_i2_frame(f, f->current_picture, buf - 4, frame_size + 4)) < 0) {
             av_log(f->avctx, AV_LOG_ERROR, "decode i2 frame failed\n");
             return ret;
         }
     } else if (frame_4cc == AV_RL32("ifrm")) {
-        p->pict_type= AV_PICTURE_TYPE_I;
-        if ((ret = decode_i_frame(f, buf, frame_size)) < 0) {
+        f->current_picture->pict_type = AV_PICTURE_TYPE_I;
+        if ((ret = decode_i_frame(f, f->current_picture, buf, frame_size)) < 0) {
             av_log(f->avctx, AV_LOG_ERROR, "decode i frame failed\n");
             return ret;
         }
     } else if (frame_4cc == AV_RL32("pfrm") || frame_4cc == AV_RL32("pfr2")) {
         if (!f->last_picture->data[0]) {
-            f->last_picture->reference = 3;
-            if ((ret = ff_get_buffer(avctx, f->last_picture)) < 0) {
-                av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+            if ((ret = ff_get_buffer(avctx, f->last_picture,
+                                     AV_GET_BUFFER_FLAG_REF)) < 0)
                 return ret;
-            }
             for (i=0; i<avctx->height; i++)
                 memset(f->last_picture->data[0] + i*f->last_picture->linesize[0], 0, 2*avctx->width);
         }
 
-        p->pict_type = AV_PICTURE_TYPE_P;
-        if ((ret = decode_p_frame(f, buf, frame_size)) < 0) {
+        f->current_picture->pict_type = AV_PICTURE_TYPE_P;
+        if ((ret = decode_p_frame(f, f->current_picture, buf, frame_size)) < 0) {
             av_log(f->avctx, AV_LOG_ERROR, "decode p frame failed\n");
             return ret;
         }
@@ -928,9 +921,10 @@
                buf_size);
     }
 
-    p->key_frame = p->pict_type == AV_PICTURE_TYPE_I;
+    f->current_picture->key_frame = f->current_picture->pict_type == AV_PICTURE_TYPE_I;
 
-    *picture   = *p;
+    if ((ret = av_frame_ref(picture, f->current_picture)) < 0)
+        return ret;
     *got_frame = 1;
 
     emms_c();
@@ -961,13 +955,10 @@
     else
         avctx->pix_fmt = AV_PIX_FMT_BGR555;
 
-    f->current_picture = avcodec_alloc_frame();
-    f->last_picture    = avcodec_alloc_frame();
-    if (!f->current_picture || !f->last_picture) {
-        avcodec_free_frame(&f->current_picture);
-        avcodec_free_frame(&f->last_picture);
+    f->current_picture = av_frame_alloc();
+    f->last_picture    = av_frame_alloc();
+    if (!f->current_picture  || !f->last_picture)
         return AVERROR(ENOMEM);
-    }
 
     return 0;
 }
@@ -985,12 +976,8 @@
         f->cfrm[i].allocated_size = 0;
     }
     ff_free_vlc(&f->pre_vlc);
-    if (f->current_picture->data[0])
-        avctx->release_buffer(avctx, f->current_picture);
-    if (f->last_picture->data[0])
-        avctx->release_buffer(avctx, f->last_picture);
-    avcodec_free_frame(&f->current_picture);
-    avcodec_free_frame(&f->last_picture);
+    av_frame_free(&f->current_picture);
+    av_frame_free(&f->last_picture);
 
     return 0;
 }
diff --git a/libavcodec/8bps.c b/libavcodec/8bps.c
index 158e29f..a910551 100644
--- a/libavcodec/8bps.c
+++ b/libavcodec/8bps.c
@@ -46,7 +46,6 @@
 
 typedef struct EightBpsContext {
     AVCodecContext *avctx;
-    AVFrame pic;
 
     unsigned char planes;
     unsigned char planemap[4];
@@ -57,6 +56,7 @@
 static int decode_frame(AVCodecContext *avctx, void *data,
                         int *got_frame, AVPacket *avpkt)
 {
+    AVFrame *frame = data;
     const uint8_t *buf = avpkt->data;
     int buf_size       = avpkt->size;
     EightBpsContext * const c = avctx->priv_data;
@@ -70,15 +70,8 @@
     unsigned char *planemap = c->planemap;
     int ret;
 
-    if (c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
-
-    c->pic.reference    = 0;
-    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
-    if ((ret = ff_get_buffer(avctx, &c->pic)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     /* Set data pointer after line lengths */
     dp = encoded + planes * (height << 1);
@@ -89,8 +82,8 @@
 
         /* Decode a plane */
         for (row = 0; row < height; row++) {
-            pixptr = c->pic.data[0] + row * c->pic.linesize[0] + planemap[p];
-            pixptr_end = pixptr + c->pic.linesize[0];
+            pixptr = frame->data[0] + row * frame->linesize[0] + planemap[p];
+            pixptr_end = pixptr + frame->linesize[0];
             if(lp - encoded + row*2 + 1 >= buf_size)
                 return -1;
             dlen = av_be2ne16(*(const unsigned short *)(lp + row * 2));
@@ -129,15 +122,14 @@
                                                      AV_PKT_DATA_PALETTE,
                                                      NULL);
         if (pal) {
-            c->pic.palette_has_changed = 1;
+            frame->palette_has_changed = 1;
             memcpy(c->pal, pal, AVPALETTE_SIZE);
         }
 
-        memcpy (c->pic.data[1], c->pal, AVPALETTE_SIZE);
+        memcpy (frame->data[1], c->pal, AVPALETTE_SIZE);
     }
 
     *got_frame = 1;
-    *(AVFrame*)data = c->pic;
 
     /* always report that the buffer was completely consumed */
     return buf_size;
@@ -148,9 +140,7 @@
     EightBpsContext * const c = avctx->priv_data;
 
     c->avctx       = avctx;
-    c->pic.data[0] = NULL;
 
-    avcodec_get_frame_defaults(&c->pic);
     switch (avctx->bits_per_coded_sample) {
     case 8:
         avctx->pix_fmt = AV_PIX_FMT_PAL8;
@@ -188,23 +178,12 @@
     return 0;
 }
 
-static av_cold int decode_end(AVCodecContext *avctx)
-{
-    EightBpsContext * const c = avctx->priv_data;
-
-    if (c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
-
-    return 0;
-}
-
 AVCodec ff_eightbps_decoder = {
     .name           = "8bps",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_8BPS,
     .priv_data_size = sizeof(EightBpsContext),
     .init           = decode_init,
-    .close          = decode_end,
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("QuickTime 8BPS video"),
diff --git a/libavcodec/8svx.c b/libavcodec/8svx.c
index d33f73e..82fda6f 100644
--- a/libavcodec/8svx.c
+++ b/libavcodec/8svx.c
@@ -136,10 +136,8 @@
 
     /* get output buffer */
     frame->nb_samples = buf_size * 2;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     for (ch = 0; ch < avctx->channels; ch++) {
         delta_decode(frame->data[ch], &esc->data[ch][esc->data_idx],
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 52282e3..6f98649 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -20,12 +20,8 @@
        bitstream.o                                                      \
        bitstream_filter.o                                               \
        codec_desc.o                                                     \
-       dsputil.o                                                        \
-       faanidct.o                                                       \
        fmtconvert.o                                                     \
        imgconvert.o                                                     \
-       jrevdct.o                                                        \
-       log2_tab.o                                                       \
        mathtables.o                                                     \
        options.o                                                        \
        parser.o                                                         \
@@ -33,7 +29,6 @@
        rawdec.o                                                         \
        resample.o                                                       \
        resample2.o                                                      \
-       simple_idct.o                                                    \
        utils.o                                                          \
 
 # parts needed for many different codecs
@@ -43,6 +38,8 @@
 OBJS-$(CONFIG_CRYSTALHD)               += crystalhd.o
 OBJS-$(CONFIG_DCT)                     += dct.o dct32_fixed.o dct32_float.o
 OBJS-$(CONFIG_DXVA2)                   += dxva2.o
+OBJS-$(CONFIG_DSPUTIL)                 += dsputil.o faanidct.o          \
+                                          simple_idct.o jrevdct.o
 OBJS-$(CONFIG_ENCODERS)                += faandct.o jfdctfst.o jfdctint.o
 OBJS-$(CONFIG_ERROR_RESILIENCE)        += error_resilience.o
 FFT-OBJS-$(CONFIG_HARDCODED_TABLES)    += cos_tables.o cos_fixed_tables.o
@@ -53,6 +50,7 @@
 OBJS-$(CONFIG_H264DSP)                 += h264dsp.o h264idct.o
 OBJS-$(CONFIG_H264PRED)                += h264pred.o
 OBJS-$(CONFIG_H264QPEL)                += h264qpel.o
+OBJS-$(CONFIG_HPELDSP)                 += hpeldsp.o
 OBJS-$(CONFIG_HUFFMAN)                 += huffman.o
 OBJS-$(CONFIG_LIBXVID)                 += libxvid_rc.o
 OBJS-$(CONFIG_LPC)                     += lpc.o
@@ -70,13 +68,14 @@
 OBJS-$(CONFIG_RANGECODER)              += rangecoder.o
 RDFT-OBJS-$(CONFIG_HARDCODED_TABLES)   += sin_tables.o
 OBJS-$(CONFIG_RDFT)                    += rdft.o $(RDFT-OBJS-yes)
+OBJS-$(CONFIG_SHARED)                  += log2_tab.o
 OBJS-$(CONFIG_SINEWIN)                 += sinewin.o
 OBJS-$(CONFIG_VAAPI)                   += vaapi.o
 OBJS-$(CONFIG_VDPAU)                   += vdpau.o
 OBJS-$(CONFIG_VIDEODSP)                += videodsp.o
 OBJS-$(CONFIG_VP3DSP)                  += vp3dsp.o
 
-# decoders/encoders/hardware accelerators
+# decoders/encoders
 OBJS-$(CONFIG_ZERO12V_DECODER)         += 012v.o
 OBJS-$(CONFIG_A64MULTI_ENCODER)        += a64multienc.o elbg.o
 OBJS-$(CONFIG_A64MULTI5_ENCODER)       += a64multienc.o elbg.o
@@ -111,6 +110,8 @@
 OBJS-$(CONFIG_ANM_DECODER)             += anm.o
 OBJS-$(CONFIG_ANSI_DECODER)            += ansi.o cga_data.o
 OBJS-$(CONFIG_APE_DECODER)             += apedec.o
+OBJS-$(CONFIG_SSA_DECODER)             += assdec.o ass.o ass_split.o
+OBJS-$(CONFIG_SSA_ENCODER)             += assenc.o ass.o
 OBJS-$(CONFIG_ASS_DECODER)             += assdec.o ass.o ass_split.o
 OBJS-$(CONFIG_ASS_ENCODER)             += assenc.o ass.o
 OBJS-$(CONFIG_ASV1_DECODER)            += asvdec.o asv.o mpeg12data.o
@@ -156,7 +157,7 @@
 OBJS-$(CONFIG_CSCD_DECODER)            += cscd.o
 OBJS-$(CONFIG_CYUV_DECODER)            += cyuv.o
 OBJS-$(CONFIG_DCA_DECODER)             += dcadec.o dca.o dcadsp.o      \
-                                          dca_parser.o synth_filter.o
+                                          synth_filter.o
 OBJS-$(CONFIG_DCA_ENCODER)             += dcaenc.o dca.o
 OBJS-$(CONFIG_DIRAC_DECODER)           += diracdec.o dirac.o diracdsp.o \
                                           dirac_arith.o mpeg12data.o dirac_dwt.o
@@ -182,8 +183,8 @@
                                           mpeg12data.o
 OBJS-$(CONFIG_EATGQ_DECODER)           += eatgq.o eaidct.o
 OBJS-$(CONFIG_EATGV_DECODER)           += eatgv.o
-OBJS-$(CONFIG_EATQI_DECODER)           += eatqi.o eaidct.o mpeg12.o \
-                                          mpeg12data.o
+OBJS-$(CONFIG_EATQI_DECODER)           += eatqi.o eaidct.o mpeg12dec.o  \
+                                          mpeg12.o mpeg12data.o
 OBJS-$(CONFIG_EIGHTBPS_DECODER)        += 8bps.o
 OBJS-$(CONFIG_EIGHTSVX_EXP_DECODER)    += 8svx.o
 OBJS-$(CONFIG_EIGHTSVX_FIB_DECODER)    += 8svx.o
@@ -214,24 +215,18 @@
 OBJS-$(CONFIG_GIF_ENCODER)             += gif.o lzwenc.o
 OBJS-$(CONFIG_GSM_DECODER)             += gsmdec.o gsmdec_data.o msgsmdec.o
 OBJS-$(CONFIG_GSM_MS_DECODER)          += gsmdec.o gsmdec_data.o msgsmdec.o
-OBJS-$(CONFIG_H261_DECODER)            += h261dec.o h261.o h261data.o
-OBJS-$(CONFIG_H261_ENCODER)            += h261enc.o h261.o h261data.o
+OBJS-$(CONFIG_H261_DECODER)            += h261dec.o h261data.o h261.o
+OBJS-$(CONFIG_H261_ENCODER)            += h261enc.o h261data.o h261.o
 OBJS-$(CONFIG_H263_DECODER)            += h263dec.o h263.o ituh263dec.o        \
                                           mpeg4video.o mpeg4videodec.o flvdec.o\
                                           intelh263dec.o
-OBJS-$(CONFIG_H263_VAAPI_HWACCEL)      += vaapi_mpeg4.o
-OBJS-$(CONFIG_H263_VDPAU_HWACCEL)      += vdpau_mpeg4.o
 OBJS-$(CONFIG_H263_ENCODER)            += mpeg4videoenc.o mpeg4video.o  \
                                           h263.o ituh263enc.o flvenc.o
 OBJS-$(CONFIG_H264_DECODER)            += h264.o                               \
                                           h264_loopfilter.o h264_direct.o      \
                                           cabac.o h264_sei.o h264_ps.o         \
                                           h264_refs.o h264_cavlc.o h264_cabac.o
-OBJS-$(CONFIG_H264_DXVA2_HWACCEL)      += dxva2_h264.o
-OBJS-$(CONFIG_H264_VAAPI_HWACCEL)      += vaapi_h264.o
-OBJS-$(CONFIG_H264_VDA_HWACCEL)        += vda_h264.o
 OBJS-$(CONFIG_H264_VDA_DECODER)        += vda_h264_dec.o
-OBJS-$(CONFIG_H264_VDPAU_HWACCEL)      += vdpau_h264.o
 OBJS-$(CONFIG_HUFFYUV_DECODER)         += huffyuv.o huffyuvdec.o
 OBJS-$(CONFIG_HUFFYUV_ENCODER)         += huffyuv.o huffyuvenc.o
 OBJS-$(CONFIG_IAC_DECODER)             += imc.o
@@ -247,8 +242,10 @@
 OBJS-$(CONFIG_INTERPLAY_DPCM_DECODER)  += dpcm.o
 OBJS-$(CONFIG_INTERPLAY_VIDEO_DECODER) += interplayvideo.o
 OBJS-$(CONFIG_JACOSUB_DECODER)         += jacosubdec.o ass.o
-OBJS-$(CONFIG_JPEG2000_DECODER)        += j2kdec.o mqcdec.o mqc.o j2k.o j2k_dwt.o
-OBJS-$(CONFIG_JPEG2000_ENCODER)        += j2kenc.o mqcenc.o mqc.o j2k.o j2k_dwt.o
+OBJS-$(CONFIG_J2K_DECODER)             += j2kdec.o mqcdec.o mqc.o j2k.o j2k_dwt.o
+OBJS-$(CONFIG_J2K_ENCODER)             += j2kenc.o mqcenc.o mqc.o j2k.o j2k_dwt.o
+OBJS-$(CONFIG_JPEG2000_DECODER)        += jpeg2000dec.o jpeg2000.o      \
+                                          jpeg2000dwt.o mqcdec.o mqc.o
 OBJS-$(CONFIG_JPEGLS_DECODER)          += jpeglsdec.o jpegls.o \
                                           mjpegdec.o mjpeg.o
 OBJS-$(CONFIG_JPEGLS_ENCODER)          += jpeglsenc.o jpegls.o
@@ -288,30 +285,23 @@
 OBJS-$(CONFIG_MPEGVIDEO_DECODER)       += mpeg12.o mpeg12data.o \
                                           mpegvideo.o error_resilience.o
 OBJS-$(CONFIG_MPEG_XVMC_DECODER)       += mpegvideo_xvmc.o
-OBJS-$(CONFIG_MPEG1_VDPAU_HWACCEL)     += vdpau_mpeg12.o
-OBJS-$(CONFIG_MPEG1VIDEO_DECODER)      += mpeg12.o mpeg12data.o
-OBJS-$(CONFIG_MPEG1VIDEO_ENCODER)      += mpeg12enc.o mpeg12.o          \
-                                          timecode.o
-OBJS-$(CONFIG_MPEG2_DXVA2_HWACCEL)     += dxva2_mpeg2.o
-OBJS-$(CONFIG_MPEG2_VAAPI_HWACCEL)     += vaapi_mpeg2.o
-OBJS-$(CONFIG_MPEG2_VDPAU_HWACCEL)     += vdpau_mpeg12.o
-OBJS-$(CONFIG_MPEG2VIDEO_DECODER)      += mpeg12.o mpeg12data.o
+OBJS-$(CONFIG_MPEG1VIDEO_DECODER)      += mpeg12dec.o mpeg12.o mpeg12data.o
+OBJS-$(CONFIG_MPEG1VIDEO_ENCODER)      += mpeg12enc.o mpeg12.o
+OBJS-$(CONFIG_MPEG2VIDEO_DECODER)      += mpeg12dec.o mpeg12.o mpeg12data.o
 OBJS-$(CONFIG_MPEG2VIDEO_ENCODER)      += mpeg12enc.o mpeg12.o          \
                                           timecode.o
-OBJS-$(CONFIG_MPEG4_VAAPI_HWACCEL)     += vaapi_mpeg4.o
-OBJS-$(CONFIG_MPEG4_VDPAU_HWACCEL)     += vdpau_mpeg4.o
 OBJS-$(CONFIG_MPL2_DECODER)            += mpl2dec.o ass.o
-OBJS-$(CONFIG_MSMPEG4V1_DECODER)       += msmpeg4.o msmpeg4data.o
-OBJS-$(CONFIG_MSMPEG4V2_DECODER)       += msmpeg4.o msmpeg4data.o h263dec.o \
-                                          h263.o ituh263dec.o mpeg4videodec.o
+OBJS-$(CONFIG_MSMPEG4V1_DECODER)       += msmpeg4dec.o msmpeg4.o msmpeg4data.o
+OBJS-$(CONFIG_MSMPEG4V2_DECODER)       += msmpeg4dec.o msmpeg4.o msmpeg4data.o \
+                                          h263dec.o h263.o ituh263dec.o \
+                                          mpeg4videodec.o
 OBJS-$(CONFIG_MSMPEG4V2_ENCODER)       += msmpeg4.o msmpeg4enc.o msmpeg4data.o \
-                                          h263dec.o h263.o ituh263dec.o        \
+                                          h263.o
+OBJS-$(CONFIG_MSMPEG4V3_DECODER)       += msmpeg4dec.o msmpeg4.o msmpeg4data.o \
+                                          h263dec.o h263.o ituh263dec.o \
                                           mpeg4videodec.o
-OBJS-$(CONFIG_MSMPEG4V3_DECODER)       += msmpeg4.o msmpeg4data.o h263dec.o \
-                                          h263.o ituh263dec.o mpeg4videodec.o
 OBJS-$(CONFIG_MSMPEG4V3_ENCODER)       += msmpeg4.o msmpeg4enc.o msmpeg4data.o \
-                                          h263dec.o h263.o ituh263dec.o        \
-                                          mpeg4videodec.o
+                                          h263.o
 OBJS-$(CONFIG_MSRLE_DECODER)           += msrle.o msrledec.o
 OBJS-$(CONFIG_MSA1_DECODER)            += mss3.o mss34dsp.o
 OBJS-$(CONFIG_MSS1_DECODER)            += mss1.o mss12.o
@@ -348,8 +338,8 @@
 OBJS-$(CONFIG_PRORES_DECODER)          += proresdec2.o proresdsp.o
 OBJS-$(CONFIG_PRORES_LGPL_DECODER)     += proresdec_lgpl.o proresdsp.o proresdata.o
 OBJS-$(CONFIG_PRORES_ENCODER)          += proresenc_anatoliy.o
-OBJS-$(CONFIG_PRORES_ANATOLIY_ENCODER) += proresenc_anatoliy.o
-OBJS-$(CONFIG_PRORES_KOSTYA_ENCODER)   += proresenc_kostya.o proresdata.o proresdsp.o
+OBJS-$(CONFIG_PRORES_AW_ENCODER)       += proresenc_anatoliy.o
+OBJS-$(CONFIG_PRORES_KS_ENCODER)       += proresenc_kostya.o proresdata.o proresdsp.o
 OBJS-$(CONFIG_PTX_DECODER)             += ptx.o
 OBJS-$(CONFIG_QCELP_DECODER)           += qcelpdec.o                     \
                                           celp_filters.o acelp_vectors.o \
@@ -454,11 +444,8 @@
 OBJS-$(CONFIG_VB_DECODER)              += vb.o
 OBJS-$(CONFIG_VBLE_DECODER)            += vble.o
 OBJS-$(CONFIG_VC1_DECODER)             += vc1dec.o vc1.o vc1data.o vc1dsp.o \
-                                          msmpeg4.o msmpeg4data.o           \
+                                          msmpeg4dec.o msmpeg4.o msmpeg4data.o \
                                           intrax8.o intrax8dsp.o
-OBJS-$(CONFIG_VC1_DXVA2_HWACCEL)       += dxva2_vc1.o
-OBJS-$(CONFIG_VC1_VAAPI_HWACCEL)       += vaapi_vc1.o
-OBJS-$(CONFIG_VC1_VDPAU_HWACCEL)       += vdpau_vc1.o
 OBJS-$(CONFIG_VCR1_DECODER)            += vcr1.o
 OBJS-$(CONFIG_VMDAUDIO_DECODER)        += vmdav.o
 OBJS-$(CONFIG_VMDVIDEO_DECODER)        += vmdav.o
@@ -476,6 +463,7 @@
 OBJS-$(CONFIG_VPLAYER_DECODER)         += textdec.o ass.o
 OBJS-$(CONFIG_VQA_DECODER)             += vqavideo.o
 OBJS-$(CONFIG_WAVPACK_DECODER)         += wavpack.o
+OBJS-$(CONFIG_WEBP_DECODER)            += vp8.o vp8dsp.o vp56rac.o
 OBJS-$(CONFIG_WEBVTT_DECODER)          += webvttdec.o
 OBJS-$(CONFIG_WMALOSSLESS_DECODER)     += wmalosslessdec.o wma_common.o
 OBJS-$(CONFIG_WMAPRO_DECODER)          += wmaprodec.o wma.o wma_common.o
@@ -486,13 +474,12 @@
 OBJS-$(CONFIG_WMAVOICE_DECODER)        += wmavoice.o \
                                           celp_filters.o \
                                           acelp_vectors.o acelp_filters.o
-OBJS-$(CONFIG_WMV1_DECODER)            += msmpeg4.o msmpeg4data.o
+OBJS-$(CONFIG_WMV1_DECODER)            += msmpeg4dec.o msmpeg4.o msmpeg4data.o
 OBJS-$(CONFIG_WMV2_DECODER)            += wmv2dec.o wmv2.o wmv2dsp.o \
-                                          msmpeg4.o msmpeg4data.o \
+                                          msmpeg4dec.o msmpeg4.o msmpeg4data.o \
                                           intrax8.o intrax8dsp.o
 OBJS-$(CONFIG_WMV2_ENCODER)            += wmv2enc.o wmv2.o wmv2dsp.o \
-                                          msmpeg4.o msmpeg4enc.o msmpeg4data.o \
-                                          mpeg4videodec.o ituh263dec.o h263dec.o
+                                          msmpeg4.o msmpeg4enc.o msmpeg4data.o
 OBJS-$(CONFIG_WNV1_DECODER)            += wnv1.o
 OBJS-$(CONFIG_WS_SND1_DECODER)         += ws-snd1.o
 OBJS-$(CONFIG_XAN_DPCM_DECODER)        += dpcm.o
@@ -619,6 +606,23 @@
 OBJS-$(CONFIG_ADPCM_YAMAHA_ENCODER)       += adpcmenc.o adpcm_data.o
 OBJS-$(CONFIG_VIMA_DECODER)               += vima.o adpcm_data.o
 
+# hardware accelerators
+OBJS-$(CONFIG_H263_VAAPI_HWACCEL)         += vaapi_mpeg4.o
+OBJS-$(CONFIG_H263_VDPAU_HWACCEL)         += vdpau_mpeg4.o
+OBJS-$(CONFIG_H264_DXVA2_HWACCEL)         += dxva2_h264.o
+OBJS-$(CONFIG_H264_VAAPI_HWACCEL)         += vaapi_h264.o
+OBJS-$(CONFIG_H264_VDA_HWACCEL)           += vda_h264.o
+OBJS-$(CONFIG_H264_VDPAU_HWACCEL)         += vdpau_h264.o
+OBJS-$(CONFIG_MPEG1_VDPAU_HWACCEL)        += vdpau_mpeg12.o
+OBJS-$(CONFIG_MPEG2_DXVA2_HWACCEL)        += dxva2_mpeg2.o
+OBJS-$(CONFIG_MPEG2_VAAPI_HWACCEL)        += vaapi_mpeg2.o
+OBJS-$(CONFIG_MPEG2_VDPAU_HWACCEL)        += vdpau_mpeg12.o
+OBJS-$(CONFIG_MPEG4_VAAPI_HWACCEL)        += vaapi_mpeg4.o
+OBJS-$(CONFIG_MPEG4_VDPAU_HWACCEL)        += vdpau_mpeg4.o
+OBJS-$(CONFIG_VC1_DXVA2_HWACCEL)          += dxva2_vc1.o
+OBJS-$(CONFIG_VC1_VAAPI_HWACCEL)          += vaapi_vc1.o
+OBJS-$(CONFIG_VC1_VDPAU_HWACCEL)          += vdpau_vc1.o
+
 # libavformat dependencies
 OBJS-$(CONFIG_ADTS_MUXER)              += mpeg4audio.o
 OBJS-$(CONFIG_ADX_DEMUXER)             += adx.o
@@ -687,6 +691,7 @@
                                              libschroedinger.o
 OBJS-$(CONFIG_LIBSCHROEDINGER_ENCODER)    += libschroedingerenc.o \
                                              libschroedinger.o
+OBJS-$(CONFIG_LIBSHINE_ENCODER)           += libshine.o
 OBJS-$(CONFIG_LIBSPEEX_DECODER)           += libspeexdec.o
 OBJS-$(CONFIG_LIBSPEEX_ENCODER)           += libspeexenc.o
 OBJS-$(CONFIG_LIBSTAGEFRIGHT_H264_DECODER)+= libstagefright.o
@@ -721,6 +726,7 @@
 OBJS-$(CONFIG_DIRAC_PARSER)            += dirac_parser.o
 OBJS-$(CONFIG_DNXHD_PARSER)            += dnxhd_parser.o
 OBJS-$(CONFIG_DVBSUB_PARSER)           += dvbsub_parser.o
+OBJS-$(CONFIG_DVD_NAV_PARSER)          += dvd_nav_parser.o
 OBJS-$(CONFIG_DVDSUB_PARSER)           += dvdsub_parser.o
 OBJS-$(CONFIG_FLAC_PARSER)             += flac_parser.o flacdata.o flac.o \
                                           vorbis_data.o
@@ -771,9 +777,11 @@
 OBJS-$(CONFIG_TEXT2MOVSUB_BSF)            += movsub_bsf.o
 
 # thread libraries
-OBJS-$(HAVE_PTHREADS)                  += pthread.o frame_thread_encoder.o
-OBJS-$(HAVE_W32THREADS)                += pthread.o frame_thread_encoder.o
-OBJS-$(HAVE_OS2THREADS)                += pthread.o frame_thread_encoder.o
+OBJS-$(HAVE_PTHREADS)                  += pthread.o
+OBJS-$(HAVE_W32THREADS)                += pthread.o
+OBJS-$(HAVE_OS2THREADS)                += pthread.o
+
+OBJS-$(CONFIG_FRAME_THREAD_ENCODER)    += frame_thread_encoder.o
 
 SKIPHEADERS                            += %_tablegen.h                  \
                                           %_tables.h                    \
diff --git a/libavcodec/a64enc.h b/libavcodec/a64enc.h
deleted file mode 100644
index 2895995..0000000
--- a/libavcodec/a64enc.h
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * a64 video encoder - basic headers
- * Copyright (c) 2009 Tobias Bindhammer
- *
- * 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
- * a64 video encoder - basic headers
- */
-
-#ifndef AVCODEC_A64ENC_H
-#define AVCODEC_A64ENC_H
-
-#include "libavutil/lfg.h"
-#include "avcodec.h"
-
-#define C64XRES 320
-#define C64YRES 200
-
-typedef struct A64Context {
-    /* general variables */
-    AVFrame picture;
-
-    /* variables for multicolor modes */
-    AVLFG randctx;
-    int mc_lifetime;
-    int mc_use_5col;
-    unsigned mc_frame_counter;
-    int *mc_meta_charset;
-    int *mc_charmap;
-    int *mc_best_cb;
-    int mc_luma_vals[5];
-    uint8_t *mc_charset;
-    uint8_t *mc_colram;
-    uint8_t *mc_palette;
-    int mc_pal_size;
-
-    /* pts of the next packet that will be output */
-    int64_t next_pts;
-} A64Context;
-
-#endif /* AVCODEC_A64ENC_H */
diff --git a/libavcodec/a64multienc.c b/libavcodec/a64multienc.c
index 0f6cc76..eaf7b46 100644
--- a/libavcodec/a64multienc.c
+++ b/libavcodec/a64multienc.c
@@ -24,7 +24,6 @@
  * a64 video encoder - multicolor modes
  */
 
-#include "a64enc.h"
 #include "a64colors.h"
 #include "a64tables.h"
 #include "elbg.h"
@@ -37,6 +36,31 @@
 #define INTERLACED    1
 #define CROP_SCREENS  1
 
+#define C64XRES 320
+#define C64YRES 200
+
+typedef struct A64Context {
+    /* general variables */
+    AVFrame picture;
+
+    /* variables for multicolor modes */
+    AVLFG randctx;
+    int mc_lifetime;
+    int mc_use_5col;
+    unsigned mc_frame_counter;
+    int *mc_meta_charset;
+    int *mc_charmap;
+    int *mc_best_cb;
+    int mc_luma_vals[5];
+    uint8_t *mc_charset;
+    uint8_t *mc_colram;
+    uint8_t *mc_palette;
+    int mc_pal_size;
+
+    /* pts of the next packet that will be output */
+    int64_t next_pts;
+} A64Context;
+
 /* gray gradient */
 static const int mc_colors[5]={0x0,0xb,0xc,0xf,0x1};
 
diff --git a/libavcodec/aac_adtstoasc_bsf.c b/libavcodec/aac_adtstoasc_bsf.c
index e6b32ce..c7d7b3a 100644
--- a/libavcodec/aac_adtstoasc_bsf.c
+++ b/libavcodec/aac_adtstoasc_bsf.c
@@ -61,7 +61,8 @@
     }
 
     if (!hdr.crc_absent && hdr.num_aac_frames > 1) {
-        av_log_missing_feature(avctx, "Multiple RDBs per frame with CRC", 0);
+        avpriv_report_missing_feature(avctx,
+                                      "Multiple RDBs per frame with CRC");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -74,7 +75,10 @@
         if (!hdr.chan_config) {
             init_get_bits(&gb, buf, buf_size * 8);
             if (get_bits(&gb, 3) != 5) {
-                av_log_missing_feature(avctx, "PCE based channel configuration, where the PCE is not the first syntax element", 0);
+                avpriv_report_missing_feature(avctx,
+                                              "PCE-based channel configuration "
+                                              "without PCE as first syntax "
+                                              "element");
                 return AVERROR_PATCHWELCOME;
             }
             init_put_bits(&pb, pce_data, MAX_PCE_SIZE);
diff --git a/libavcodec/aacdec.c b/libavcodec/aacdec.c
index 7a871c4..37c7de5 100644
--- a/libavcodec/aacdec.c
+++ b/libavcodec/aacdec.c
@@ -190,11 +190,10 @@
     }
 
     /* get output buffer */
+    av_frame_unref(ac->frame);
     ac->frame->nb_samples = 2048;
-    if ((ret = ff_get_buffer(avctx, ac->frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, ac->frame, 0)) < 0)
         return ret;
-    }
 
     /* map output channel pointers to AVFrame data */
     for (ch = 0; ch < avctx->channels; ch++) {
@@ -710,7 +709,7 @@
     int tags = 0;
 
     if (get_bits1(gb)) { // frameLengthFlag
-        av_log_missing_feature(avctx, "960/120 MDCT window", 1);
+        avpriv_request_sample(avctx, "960/120 MDCT window");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -1169,10 +1168,10 @@
                     offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
                     clipped_offset = av_clip(offset[2], -155, 100);
                     if (offset[2] != clipped_offset) {
-                        av_log_ask_for_sample(ac->avctx, "Intensity stereo "
-                                "position clipped (%d -> %d).\nIf you heard an "
-                                "audible artifact, there may be a bug in the "
-                                "decoder. ", offset[2], clipped_offset);
+                        avpriv_request_sample(ac->avctx,
+                                              "If you heard an audible artifact, there may be a bug in the decoder. "
+                                              "Clipped intensity stereo position (%d -> %d)",
+                                              offset[2], clipped_offset);
                     }
                     sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
                 }
@@ -1184,10 +1183,10 @@
                         offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
                     clipped_offset = av_clip(offset[1], -100, 155);
                     if (offset[1] != clipped_offset) {
-                        av_log_ask_for_sample(ac->avctx, "Noise gain clipped "
-                                "(%d -> %d).\nIf you heard an audible "
-                                "artifact, there may be a bug in the decoder. ",
-                                offset[1], clipped_offset);
+                        avpriv_request_sample(ac->avctx,
+                                              "If you heard an audible artifact, there may be a bug in the decoder. "
+                                              "Clipped noise gain (%d -> %d)",
+                                              offset[1], clipped_offset);
                     }
                     sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
                 }
@@ -1714,7 +1713,7 @@
         if ((tns->present = get_bits1(gb)) && decode_tns(ac, tns, gb, ics))
             return -1;
         if (get_bits1(gb)) {
-            av_log_missing_feature(ac->avctx, "SSR", 1);
+            avpriv_request_sample(ac->avctx, "SSR");
             return AVERROR_PATCHWELCOME;
         }
     }
@@ -2431,7 +2430,8 @@
         if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
             // This is 2 for "VLB " audio in NSV files.
             // See samples/nsv/vlb_audio.
-            av_log_missing_feature(ac->avctx, "More than one AAC RDB per ADTS frame", 0);
+            avpriv_report_missing_feature(ac->avctx,
+                                          "More than one AAC RDB per ADTS frame");
             ac->warned_num_aac_frames = 1;
         }
         push_output_configuration(ac);
@@ -2750,8 +2750,8 @@
         asclen         = get_bits_left(gb);
 
     if (config_start_bit % 8) {
-        av_log_missing_feature(latmctx->aac_ctx.avctx,
-                               "Non-byte-aligned audio-specific config", 1);
+        avpriv_request_sample(latmctx->aac_ctx.avctx,
+                              "Non-byte-aligned audio-specific config");
         return AVERROR_PATCHWELCOME;
     }
     if (asclen <= 0)
@@ -2810,8 +2810,7 @@
         skip_bits(gb, 6);                       // numSubFrames
         // numPrograms
         if (get_bits(gb, 4)) {                  // numPrograms
-            av_log_missing_feature(latmctx->aac_ctx.avctx,
-                                   "Multiple programs", 1);
+            avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple programs");
             return AVERROR_PATCHWELCOME;
         }
 
@@ -2819,8 +2818,7 @@
 
         // for each layer (which there is only one in DVB)
         if (get_bits(gb, 3)) {                   // numLayer
-            av_log_missing_feature(latmctx->aac_ctx.avctx,
-                                   "Multiple layers", 1);
+            avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple layers");
             return AVERROR_PATCHWELCOME;
         }
 
diff --git a/libavcodec/aacenc.c b/libavcodec/aacenc.c
index 5711d01..80dd3d8 100644
--- a/libavcodec/aacenc.c
+++ b/libavcodec/aacenc.c
@@ -570,10 +570,8 @@
         }
         start_ch += chans;
     }
-    if ((ret = ff_alloc_packet2(avctx, avpkt, 8192 * s->channels))) {
-        av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
+    if ((ret = ff_alloc_packet2(avctx, avpkt, 8192 * s->channels)) < 0)
         return ret;
-    }
     do {
         int frame_bits;
 
@@ -681,9 +679,6 @@
     av_freep(&s->buffer.samples);
     av_freep(&s->cpe);
     ff_af_queue_close(&s->afq);
-#if FF_API_OLD_ENCODE_AUDIO
-    av_freep(&avctx->coded_frame);
-#endif
     return 0;
 }
 
@@ -717,11 +712,6 @@
     for(ch = 0; ch < s->channels; ch++)
         s->planar_samples[ch] = s->buffer.samples + 3 * 1024 * ch;
 
-#if FF_API_OLD_ENCODE_AUDIO
-    if (!(avctx->coded_frame = avcodec_alloc_frame()))
-        goto alloc_fail;
-#endif
-
     return 0;
 alloc_fail:
     return AVERROR(ENOMEM);
@@ -776,6 +766,9 @@
     s->psypp = ff_psy_preprocess_init(avctx);
     s->coder = &ff_aac_coders[s->options.aac_coder];
 
+    if (HAVE_MIPSDSPR1)
+        ff_aac_coder_init_mips(s);
+
     s->lambda = avctx->global_quality ? avctx->global_quality : 120;
 
     ff_aac_tableinit();
diff --git a/libavcodec/aacenc.h b/libavcodec/aacenc.h
index 386c72f..cebdd18 100644
--- a/libavcodec/aacenc.h
+++ b/libavcodec/aacenc.h
@@ -85,4 +85,6 @@
 
 extern float ff_aac_pow34sf_tab[428];
 
+void ff_aac_coder_init_mips(AACEncContext *c);
+
 #endif /* AVCODEC_AACENC_H */
diff --git a/libavcodec/aacps.c b/libavcodec/aacps.c
index b82001e..9abc296 100644
--- a/libavcodec/aacps.c
+++ b/libavcodec/aacps.c
@@ -823,7 +823,8 @@
             h12 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][1];
             h21 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][2];
             h22 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][3];
-            if (!PS_BASELINE && ps->enable_ipdopd && b < ps->nr_ipdopd_par) {
+
+            if (!PS_BASELINE && ps->enable_ipdopd && 2*b <= NR_PAR_BANDS[is34]) {
                 //The spec say says to only run this smoother when enable_ipdopd
                 //is set but the reference decoder appears to run it constantly
                 float h11i, h12i, h21i, h22i;
diff --git a/libavcodec/aacpsy.c b/libavcodec/aacpsy.c
index d77b3de..b3513c7 100644
--- a/libavcodec/aacpsy.c
+++ b/libavcodec/aacpsy.c
@@ -217,6 +217,10 @@
     -5.52212e-17 * 2, -0.313819 * 2
 };
 
+#if ARCH_MIPS
+#   include "mips/aacpsy_mips.h"
+#endif /* ARCH_MIPS */
+
 /**
  * Calculate the ABR attack threshold from the above LAME psymodel table.
  */
@@ -541,8 +545,10 @@
     float thr = band->thr;
 
     if (band->energy > thr) {
-        thr = powf(thr, 0.25f) + reduction;
-        thr = powf(thr, 4.0f);
+        thr = sqrtf(thr);
+        thr = sqrtf(thr) + reduction;
+        thr *= thr;
+        thr *= thr;
 
         /* This deviates from the 3GPP spec to match the reference encoder.
          * It performs min(thr_reduced, max(thr, energy/min_snr)) only for bands
@@ -558,6 +564,51 @@
     return thr;
 }
 
+#ifndef calc_thr_3gpp
+static void calc_thr_3gpp(const FFPsyWindowInfo *wi, const int num_bands, AacPsyChannel *pch,
+                          const uint8_t *band_sizes, const float *coefs)
+{
+    int i, w, g;
+    int start = 0;
+    for (w = 0; w < wi->num_windows*16; w += 16) {
+        for (g = 0; g < num_bands; g++) {
+            AacPsyBand *band = &pch->band[w+g];
+
+            float form_factor = 0.0f;
+            float Temp;
+            band->energy = 0.0f;
+            for (i = 0; i < band_sizes[g]; i++) {
+                band->energy += coefs[start+i] * coefs[start+i];
+                form_factor  += sqrtf(fabs(coefs[start+i]));
+            }
+            Temp = band->energy > 0 ? sqrtf((float)band_sizes[g] / band->energy) : 0;
+            band->thr      = band->energy * 0.001258925f;
+            band->nz_lines = form_factor * sqrtf(Temp);
+
+            start += band_sizes[g];
+        }
+    }
+}
+#endif /* calc_thr_3gpp */
+
+#ifndef psy_hp_filter
+static void psy_hp_filter(const float *firbuf, float *hpfsmpl, const float *psy_fir_coeffs)
+{
+    int i, j;
+    for (i = 0; i < AAC_BLOCK_SIZE_LONG; i++) {
+        float sum1, sum2;
+        sum1 = firbuf[i + (PSY_LAME_FIR_LEN - 1) / 2];
+        sum2 = 0.0;
+        for (j = 0; j < ((PSY_LAME_FIR_LEN - 1) / 2) - 1; j += 2) {
+            sum1 += psy_fir_coeffs[j] * (firbuf[i + j] + firbuf[i + PSY_LAME_FIR_LEN - j]);
+            sum2 += psy_fir_coeffs[j + 1] * (firbuf[i + j + 1] + firbuf[i + PSY_LAME_FIR_LEN - j - 1]);
+        }
+        /* NOTE: The LAME psymodel expects it's input in the range -32768 to 32768. Tuning this for normalized floats would be difficult. */
+        hpfsmpl[i] = (sum1 + sum2) * 32768.0f;
+    }
+}
+#endif /* psy_hp_filter */
+
 /**
  * Calculate band thresholds as suggested in 3GPP TS26.403
  */
@@ -566,7 +617,6 @@
 {
     AacPsyContext *pctx = (AacPsyContext*) ctx->model_priv_data;
     AacPsyChannel *pch  = &pctx->ch[channel];
-    int start = 0;
     int i, w, g;
     float desired_bits, desired_pe, delta_pe, reduction= NAN, spread_en[128] = {0};
     float a = 0.0f, active_lines = 0.0f, norm_fac = 0.0f;
@@ -577,22 +627,8 @@
     const float avoid_hole_thr = wi->num_windows == 8 ? PSY_3GPP_AH_THR_SHORT : PSY_3GPP_AH_THR_LONG;
 
     //calculate energies, initial thresholds and related values - 5.4.2 "Threshold Calculation"
-    for (w = 0; w < wi->num_windows*16; w += 16) {
-        for (g = 0; g < num_bands; g++) {
-            AacPsyBand *band = &pch->band[w+g];
+    calc_thr_3gpp(wi, num_bands, pch, band_sizes, coefs);
 
-            float form_factor = 0.0f;
-            band->energy = 0.0f;
-            for (i = 0; i < band_sizes[g]; i++) {
-                band->energy += coefs[start+i] * coefs[start+i];
-                form_factor  += sqrtf(fabs(coefs[start+i]));
-            }
-            band->thr      = band->energy * 0.001258925f;
-            band->nz_lines = band->energy>0 ? form_factor / powf(band->energy / band_sizes[g], 0.25f) : 0;
-
-            start += band_sizes[g];
-        }
-    }
     //modify thresholds and energies - spread, threshold in quiet, pre-echo control
     for (w = 0; w < wi->num_windows*16; w += 16) {
         AacPsyBand *bands = &pch->band[w];
@@ -798,20 +834,10 @@
         float energy_subshort[(AAC_NUM_BLOCKS_SHORT + 1) * PSY_LAME_NUM_SUBBLOCKS];
         float energy_short[AAC_NUM_BLOCKS_SHORT + 1] = { 0 };
         const float *firbuf = la + (AAC_BLOCK_SIZE_SHORT/4 - PSY_LAME_FIR_LEN);
-        int j, att_sum = 0;
+        int att_sum = 0;
 
         /* LAME comment: apply high pass filter of fs/4 */
-        for (i = 0; i < AAC_BLOCK_SIZE_LONG; i++) {
-            float sum1, sum2;
-            sum1 = firbuf[i + (PSY_LAME_FIR_LEN - 1) / 2];
-            sum2 = 0.0;
-            for (j = 0; j < ((PSY_LAME_FIR_LEN - 1) / 2) - 1; j += 2) {
-                sum1 += psy_fir_coeffs[j] * (firbuf[i + j] + firbuf[i + PSY_LAME_FIR_LEN - j]);
-                sum2 += psy_fir_coeffs[j + 1] * (firbuf[i + j + 1] + firbuf[i + PSY_LAME_FIR_LEN - j - 1]);
-            }
-            /* NOTE: The LAME psymodel expects it's input in the range -32768 to 32768. Tuning this for normalized floats would be difficult. */
-            hpfsmpl[i] = (sum1 + sum2) * 32768.0f;
-        }
+        psy_hp_filter(firbuf, hpfsmpl, psy_fir_coeffs);
 
         /* Calculate the energies of each sub-shortblock */
         for (i = 0; i < PSY_LAME_NUM_SUBBLOCKS; i++) {
diff --git a/libavcodec/aacsbr.c b/libavcodec/aacsbr.c
index 1820f80..0b6779c 100644
--- a/libavcodec/aacsbr.c
+++ b/libavcodec/aacsbr.c
@@ -933,7 +933,7 @@
 #if 1
             *num_bits_left -= ff_ps_read_data(ac->avctx, gb, &sbr->ps, *num_bits_left);
 #else
-            av_log_missing_feature(ac->avctx, "Parametric Stereo", 0);
+            avpriv_report_missing_feature(ac->avctx, "Parametric Stereo");
             skip_bits_long(gb, *num_bits_left); // bs_fill_bits
             *num_bits_left = 0;
 #endif
@@ -942,7 +942,7 @@
     default:
         // some files contain 0-padding
         if (bs_extension_id || *num_bits_left > 16 || show_bits(gb, *num_bits_left))
-            av_log_missing_feature(ac->avctx, "Reserved SBR extensions", 1);
+            avpriv_request_sample(ac->avctx, "Reserved SBR extensions");
         skip_bits_long(gb, *num_bits_left); // bs_fill_bits
         *num_bits_left = 0;
         break;
@@ -1131,7 +1131,12 @@
             for (k = 0; k < sbr->n[sbr->data[0].bs_freq_res[e]]; k++) {
                 float temp1 = exp2f(sbr->data[0].env_facs[e][k] * alpha + 7.0f);
                 float temp2 = exp2f((pan_offset - sbr->data[1].env_facs[e][k]) * alpha);
-                float fac   = temp1 / (1.0f + temp2);
+                float fac;
+                if (temp1 > 1E20) {
+                    av_log(NULL, AV_LOG_ERROR, "envelope scalefactor overflow in dequant\n");
+                    temp1 = 1;
+                }
+                fac   = temp1 / (1.0f + temp2);
                 sbr->data[0].env_facs[e][k] = fac;
                 sbr->data[1].env_facs[e][k] = fac * temp2;
             }
@@ -1140,7 +1145,12 @@
             for (k = 0; k < sbr->n_q; k++) {
                 float temp1 = exp2f(NOISE_FLOOR_OFFSET - sbr->data[0].noise_facs[e][k] + 1);
                 float temp2 = exp2f(12 - sbr->data[1].noise_facs[e][k]);
-                float fac   = temp1 / (1.0f + temp2);
+                float fac;
+                if (temp1 > 1E20) {
+                    av_log(NULL, AV_LOG_ERROR, "envelope scalefactor overflow in dequant\n");
+                    temp1 = 1;
+                }
+                fac = temp1 / (1.0f + temp2);
                 sbr->data[0].noise_facs[e][k] = fac;
                 sbr->data[1].noise_facs[e][k] = fac * temp2;
             }
@@ -1149,9 +1159,15 @@
         for (ch = 0; ch < (id_aac == TYPE_CPE) + 1; ch++) {
             float alpha = sbr->data[ch].bs_amp_res ? 1.0f : 0.5f;
             for (e = 1; e <= sbr->data[ch].bs_num_env; e++)
-                for (k = 0; k < sbr->n[sbr->data[ch].bs_freq_res[e]]; k++)
+                for (k = 0; k < sbr->n[sbr->data[ch].bs_freq_res[e]]; k++){
                     sbr->data[ch].env_facs[e][k] =
                         exp2f(alpha * sbr->data[ch].env_facs[e][k] + 6.0f);
+                    if (sbr->data[ch].env_facs[e][k] > 1E20) {
+                        av_log(NULL, AV_LOG_ERROR, "envelope scalefactor overflow in dequant\n");
+                        sbr->data[ch].env_facs[e][k] = 1;
+                    }
+                }
+
             for (e = 1; e <= sbr->data[ch].bs_num_noise; e++)
                 for (k = 0; k < sbr->n_q; k++)
                     sbr->data[ch].noise_facs[e][k] =
diff --git a/libavcodec/aacsbrdata.h b/libavcodec/aacsbrdata.h
index dd7a827..12575ee 100644
--- a/libavcodec/aacsbrdata.h
+++ b/libavcodec/aacsbrdata.h
@@ -352,7 +352,7 @@
      0.8537385600,
 };
 
-/* First two entries repeated at end to simplify SIMD implementations. */
+/* First eight entries repeated at end to simplify SIMD implementations. */
 const DECLARE_ALIGNED(16, float, ff_sbr_noise_table)[][2] = {
 {-0.99948153278296, -0.59483417516607}, { 0.97113454393991, -0.67528515225647},
 { 0.14130051758487, -0.95090983575689}, {-0.47005496701697, -0.37340549728647},
@@ -610,7 +610,11 @@
 {-0.93412041758744,  0.41374052024363}, { 0.96063943315511,  0.93116709541280},
 { 0.97534253457837,  0.86150930812689}, { 0.99642466504163,  0.70190043427512},
 {-0.94705089665984, -0.29580042814306}, { 0.91599807087376, -0.98147830385781},
+// Start of duplicated table
 {-0.99948153278296, -0.59483417516607}, { 0.97113454393991, -0.67528515225647},
+{ 0.14130051758487, -0.95090983575689}, {-0.47005496701697, -0.37340549728647},
+{ 0.80705063769351,  0.29653668284408}, {-0.38981478896926,  0.89572605717087},
+{-0.01053049862020, -0.66959058036166}, {-0.91266367957293, -0.11522938140034},
 };
 
 #endif /* AVCODEC_AACSBRDATA_H */
diff --git a/libavcodec/aasc.c b/libavcodec/aasc.c
index 308edd0..26ba30d 100644
--- a/libavcodec/aasc.c
+++ b/libavcodec/aasc.c
@@ -29,12 +29,13 @@
 #include <string.h>
 
 #include "avcodec.h"
+#include "internal.h"
 #include "msrledec.h"
 
 typedef struct AascContext {
     AVCodecContext *avctx;
     GetByteContext gb;
-    AVFrame frame;
+    AVFrame *frame;
 
     uint32_t palette[AVPALETTE_COUNT];
     int palette_size;
@@ -68,7 +69,10 @@
         av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", avctx->bits_per_coded_sample);
         return -1;
     }
-    avcodec_get_frame_defaults(&s->frame);
+
+    s->frame = av_frame_alloc();
+    if (!s->frame)
+        return AVERROR(ENOMEM);
 
     return 0;
 }
@@ -87,12 +91,8 @@
         return AVERROR_INVALIDDATA;
     }
 
-    s->frame.reference = 3;
-    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
-    if ((ret = avctx->reget_buffer(avctx, &s->frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
         return ret;
-    }
 
     compr     = AV_RL32(buf);
     buf      += 4;
@@ -101,7 +101,7 @@
     switch (avctx->codec_tag) {
     case MKTAG('A', 'A', 'S', '4'):
         bytestream2_init(&s->gb, buf - 4, buf_size + 4);
-        ff_msrle_decode(avctx, (AVPicture*)&s->frame, 8, &s->gb);
+        ff_msrle_decode(avctx, (AVPicture*)s->frame, 8, &s->gb);
         break;
     case MKTAG('A', 'A', 'S', 'C'):
     switch (compr) {
@@ -112,14 +112,14 @@
                 av_log(avctx, AV_LOG_ERROR, "Next line is beyond buffer bounds\n");
                 break;
             }
-            memcpy(s->frame.data[0] + i*s->frame.linesize[0], buf, avctx->width * psize);
+            memcpy(s->frame->data[0] + i * s->frame->linesize[0], buf, avctx->width * psize);
             buf += stride;
             buf_size -= stride;
         }
         break;
     case 1:
         bytestream2_init(&s->gb, buf, buf_size);
-        ff_msrle_decode(avctx, (AVPicture*)&s->frame, 8, &s->gb);
+        ff_msrle_decode(avctx, (AVPicture*)s->frame, 8, &s->gb);
         break;
     default:
         av_log(avctx, AV_LOG_ERROR, "Unknown compression type %d\n", compr);
@@ -132,10 +132,11 @@
     }
 
     if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
-        memcpy(s->frame.data[1], s->palette, s->palette_size);
+        memcpy(s->frame->data[1], s->palette, s->palette_size);
 
     *got_frame = 1;
-    *(AVFrame*)data = s->frame;
+    if ((ret = av_frame_ref(data, s->frame)) < 0)
+        return ret;
 
     /* report that the buffer was completely consumed */
     return buf_size;
@@ -145,9 +146,7 @@
 {
     AascContext *s = avctx->priv_data;
 
-    /* release the last frame */
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+    av_frame_free(&s->frame);
 
     return 0;
 }
diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c
index 5dde004..a70d92a 100644
--- a/libavcodec/ac3dec.c
+++ b/libavcodec/ac3dec.c
@@ -874,7 +874,7 @@
             /* check for enhanced coupling */
             if (s->eac3 && get_bits1(gbc)) {
                 /* TODO: parse enhanced coupling strategy info */
-                av_log_missing_feature(s->avctx, "Enhanced coupling", 1);
+                avpriv_request_sample(s->avctx, "Enhanced coupling");
                 return AVERROR_PATCHWELCOME;
             }
 
@@ -1375,10 +1375,8 @@
 
     /* get output buffer */
     frame->nb_samples = s->num_blocks * 256;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     /* decode the audio blocks */
     channel_map = ff_ac3_dec_channel_map[s->output_mode & ~AC3_OUTPUT_LFEON][s->lfe_on];
@@ -1406,7 +1404,7 @@
         }
     }
 
-    frame->decode_error_flags = err ? FF_DECODE_ERROR_INVALID_BITSTREAM : 0;
+    av_frame_set_decode_error_flags(frame, err ? FF_DECODE_ERROR_INVALID_BITSTREAM : 0);
 
     /* keep last block for error concealment in next frame */
     for (ch = 0; ch < s->out_channels; ch++)
diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c
index 16f0cdc..15ff343 100644
--- a/libavcodec/ac3enc.c
+++ b/libavcodec/ac3enc.c
@@ -2049,9 +2049,6 @@
 
     s->mdct_end(s);
 
-#if FF_API_OLD_ENCODE_AUDIO
-    av_freep(&avctx->coded_frame);
-#endif
     return 0;
 }
 
@@ -2481,14 +2478,6 @@
     if (ret)
         goto init_fail;
 
-#if FF_API_OLD_ENCODE_AUDIO
-    avctx->coded_frame= avcodec_alloc_frame();
-    if (!avctx->coded_frame) {
-        ret = AVERROR(ENOMEM);
-        goto init_fail;
-    }
-#endif
-
     ff_dsputil_init(&s->dsp, avctx);
     avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
     ff_ac3dsp_init(&s->ac3dsp, avctx->flags & CODEC_FLAG_BITEXACT);
diff --git a/libavcodec/ac3enc_template.c b/libavcodec/ac3enc_template.c
index fb6aad9..0389c2e 100644
--- a/libavcodec/ac3enc_template.c
+++ b/libavcodec/ac3enc_template.c
@@ -435,7 +435,7 @@
 
     ff_ac3_quantize_mantissas(s);
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, s->frame_size)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, s->frame_size)) < 0)
         return ret;
     ff_ac3_output_frame(s, avpkt->data);
 
diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c
index 02de22c..f5af5d4 100644
--- a/libavcodec/adpcm.c
+++ b/libavcodec/adpcm.c
@@ -1,6 +1,18 @@
 /*
  * Copyright (c) 2001-2003 The ffmpeg Project
  *
+ * first version by Francois Revol (revol@free.fr)
+ * fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
+ *   by Mike Melanson (melanson@pcisys.net)
+ * CD-ROM XA ADPCM codec by BERO
+ * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
+ * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
+ * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
+ * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
+ * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
+ * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
+ * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
+ *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
@@ -28,18 +40,6 @@
 /**
  * @file
  * ADPCM decoders
- * First version by Francois Revol (revol@free.fr)
- * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
- *   by Mike Melanson (melanson@pcisys.net)
- * CD-ROM XA ADPCM codec by BERO
- * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
- * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
- * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
- * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
- * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
- * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
- * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
- *
  * Features and limitations:
  *
  * Reference documents:
@@ -323,7 +323,7 @@
         shift  = 12 - (in[4+i*2] & 15);
         filter = in[4+i*2] >> 4;
         if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
-            av_log_ask_for_sample(avctx, "unknown XA-ADPCM filter %d\n", filter);
+            avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
             filter=0;
         }
         f0 = xa_adpcm_table[filter][0];
@@ -352,7 +352,7 @@
         shift  = 12 - (in[5+i*2] & 15);
         filter = in[5+i*2] >> 4;
         if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
-            av_log_ask_for_sample(avctx, "unknown XA-ADPCM filter %d\n", filter);
+            avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
             filter=0;
         }
 
@@ -637,10 +637,8 @@
 
     /* get output buffer */
     frame->nb_samples = nb_samples;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     samples = (short *)frame->data[0];
     samples_p = (int16_t **)frame->extended_data;
 
diff --git a/libavcodec/adpcmenc.c b/libavcodec/adpcmenc.c
index 8caedf7..cef8b6f 100644
--- a/libavcodec/adpcmenc.c
+++ b/libavcodec/adpcmenc.c
@@ -1,6 +1,10 @@
 /*
  * Copyright (c) 2001-2003 The ffmpeg Project
  *
+ * first version by Francois Revol (revol@free.fr)
+ * fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
+ *   by Mike Melanson (melanson@pcisys.net)
+ *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
@@ -28,10 +32,6 @@
 /**
  * @file
  * ADPCM encoders
- * First version by Francois Revol (revol@free.fr)
- * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
- *   by Mike Melanson (melanson@pcisys.net)
- *
  * See ADPCM decoder reference documents for codec information.
  */
 
@@ -144,11 +144,6 @@
         goto error;
     }
 
-#if FF_API_OLD_ENCODE_AUDIO
-    if (!(avctx->coded_frame = avcodec_alloc_frame()))
-        goto error;
-#endif
-
     return 0;
 error:
     adpcm_encode_close(avctx);
@@ -158,9 +153,6 @@
 static av_cold int adpcm_encode_close(AVCodecContext *avctx)
 {
     ADPCMEncodeContext *s = avctx->priv_data;
-#if FF_API_OLD_ENCODE_AUDIO
-    av_freep(&avctx->coded_frame);
-#endif
     av_freep(&s->paths);
     av_freep(&s->node_buf);
     av_freep(&s->nodep_buf);
@@ -494,7 +486,7 @@
         pkt_size = (2 + avctx->channels * (22 + 4 * (frame->nb_samples - 1)) + 7) / 8;
     else
         pkt_size = avctx->block_align;
-    if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, pkt_size)) < 0)
         return ret;
     dst = avpkt->data;
 
diff --git a/libavcodec/adx.c b/libavcodec/adx.c
index 7da696d..4c88f81 100644
--- a/libavcodec/adx.c
+++ b/libavcodec/adx.c
@@ -58,7 +58,7 @@
 
     /* check for encoding=3 block_size=18, sample_size=4 */
     if (buf[4] != 3 || buf[5] != 18 || buf[6] != 4) {
-        av_log_ask_for_sample(avctx, "unsupported ADX format\n");
+        avpriv_request_sample(avctx, "Support for this ADX format");
         return AVERROR_PATCHWELCOME;
     }
 
diff --git a/libavcodec/adxdec.c b/libavcodec/adxdec.c
index aebf691..7d7fe3a 100644
--- a/libavcodec/adxdec.c
+++ b/libavcodec/adxdec.c
@@ -142,10 +142,8 @@
 
     /* get output buffer */
     frame->nb_samples = num_blocks * BLOCK_SAMPLES;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     samples = (int16_t **)frame->extended_data;
     samples_offset = 0;
 
diff --git a/libavcodec/adxenc.c b/libavcodec/adxenc.c
index fb8e06d..df07c81 100644
--- a/libavcodec/adxenc.c
+++ b/libavcodec/adxenc.c
@@ -107,14 +107,6 @@
     return HEADER_SIZE;
 }
 
-#if FF_API_OLD_ENCODE_AUDIO
-static av_cold int adx_encode_close(AVCodecContext *avctx)
-{
-    av_freep(&avctx->coded_frame);
-    return 0;
-}
-#endif
-
 static av_cold int adx_encode_init(AVCodecContext *avctx)
 {
     ADXContext *c = avctx->priv_data;
@@ -125,11 +117,6 @@
     }
     avctx->frame_size = BLOCK_SAMPLES;
 
-#if FF_API_OLD_ENCODE_AUDIO
-    if (!(avctx->coded_frame = avcodec_alloc_frame()))
-        return AVERROR(ENOMEM);
-#endif
-
     /* the cutoff can be adjusted, but this seems to work pretty well */
     c->cutoff = 500;
     ff_adx_calculate_coeffs(c->cutoff, avctx->sample_rate, COEFF_BITS, c->coeff);
@@ -175,9 +162,6 @@
     .id             = AV_CODEC_ID_ADPCM_ADX,
     .priv_data_size = sizeof(ADXContext),
     .init           = adx_encode_init,
-#if FF_API_OLD_ENCODE_AUDIO
-    .close          = adx_encode_close,
-#endif
     .encode2        = adx_encode_frame,
     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
                                                       AV_SAMPLE_FMT_NONE },
diff --git a/libavcodec/alac.c b/libavcodec/alac.c
index f8ac560..0018b9a 100644
--- a/libavcodec/alac.c
+++ b/libavcodec/alac.c
@@ -36,8 +36,8 @@
  *  8bit  compatible version   (0)
  *  8bit  sample size
  *  8bit  history mult         (40)
- *  8bit  initial history      (14)
- *  8bit  rice param limit     (10)
+ *  8bit  initial history      (10)
+ *  8bit  rice param limit     (14)
  *  8bit  channels
  * 16bit  maxRun               (255)
  * 32bit  max coded frame size (0 means unknown)
@@ -289,10 +289,8 @@
     if (!alac->nb_samples) {
         /* get output buffer */
         frame->nb_samples = output_samples;
-        if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
             return ret;
-        }
     } else if (output_samples != alac->nb_samples) {
         av_log(avctx, AV_LOG_ERROR, "sample count mismatch: %u != %d\n",
                output_samples, alac->nb_samples);
@@ -588,8 +586,7 @@
     case 24:
     case 32: avctx->sample_fmt = req_packed ? AV_SAMPLE_FMT_S32 : AV_SAMPLE_FMT_S32P;
              break;
-    default: av_log_ask_for_sample(avctx, "Sample depth %d is not supported.\n",
-                                   alac->sample_size);
+    default: avpriv_request_sample(avctx, "Sample depth %d", alac->sample_size);
              return AVERROR_PATCHWELCOME;
     }
     avctx->bits_per_raw_sample = alac->sample_size;
diff --git a/libavcodec/alacenc.c b/libavcodec/alacenc.c
index 61bb65a..4ee558c 100644
--- a/libavcodec/alacenc.c
+++ b/libavcodec/alacenc.c
@@ -613,7 +613,7 @@
     else
         max_frame_size = s->max_coded_frame_size;
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, 2 * max_frame_size)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, 2 * max_frame_size)) < 0)
         return ret;
 
     /* use verbatim mode for compression_level 0 */
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 584446f..d5b6916 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -171,7 +171,8 @@
     REGISTER_DECODER(INDEO4,            indeo4);
     REGISTER_DECODER(INDEO5,            indeo5);
     REGISTER_DECODER(INTERPLAY_VIDEO,   interplay_video);
-    REGISTER_ENCDEC (JPEG2000,          jpeg2000);
+    REGISTER_ENCDEC (J2K,               j2k);
+    REGISTER_DECODER(JPEG2000,          jpeg2000);
     REGISTER_ENCDEC (JPEGLS,            jpegls);
     REGISTER_DECODER(JV,                jv);
     REGISTER_DECODER(KGV1,              kgv1);
@@ -220,8 +221,8 @@
     REGISTER_ENCDEC (PNG,               png);
     REGISTER_ENCDEC (PPM,               ppm);
     REGISTER_ENCDEC (PRORES,            prores);
-    REGISTER_ENCODER(PRORES_ANATOLIY,   prores_anatoliy);
-    REGISTER_ENCODER(PRORES_KOSTYA,     prores_kostya);
+    REGISTER_ENCODER(PRORES_AW,         prores_aw);
+    REGISTER_ENCODER(PRORES_KS,         prores_ks);
     REGISTER_DECODER(PRORES_LGPL,       prores_lgpl);
     REGISTER_DECODER(PTX,               ptx);
     REGISTER_DECODER(QDRAW,             qdraw);
@@ -283,6 +284,7 @@
     REGISTER_DECODER(VP6F,              vp6f);
     REGISTER_DECODER(VP8,               vp8);
     REGISTER_DECODER(VQA,               vqa);
+    REGISTER_DECODER(WEBP,              webp);
     REGISTER_ENCDEC (WMV1,              wmv1);
     REGISTER_ENCDEC (WMV2,              wmv2);
     REGISTER_DECODER(WMV3,              wmv3);
@@ -448,6 +450,7 @@
     REGISTER_DECODER(VIMA,              vima);
 
     /* subtitles */
+    REGISTER_ENCDEC (SSA,               ssa);
     REGISTER_ENCDEC (ASS,               ass);
     REGISTER_ENCDEC (DVBSUB,            dvbsub);
     REGISTER_ENCDEC (DVDSUB,            dvdsub);
@@ -481,6 +484,7 @@
     REGISTER_ENCDEC (LIBOPENJPEG,       libopenjpeg);
     REGISTER_ENCDEC (LIBOPUS,           libopus);
     REGISTER_ENCDEC (LIBSCHROEDINGER,   libschroedinger);
+    REGISTER_ENCODER(LIBSHINE,          libshine);
     REGISTER_ENCDEC (LIBSPEEX,          libspeex);
     REGISTER_DECODER(LIBSTAGEFRIGHT_H264, libstagefright_h264);
     REGISTER_ENCODER(LIBTHEORA,         libtheora);
@@ -515,6 +519,7 @@
     REGISTER_PARSER(DNXHD,              dnxhd);
     REGISTER_PARSER(DVBSUB,             dvbsub);
     REGISTER_PARSER(DVDSUB,             dvdsub);
+    REGISTER_PARSER(DVD_NAV,            dvd_nav);
     REGISTER_PARSER(FLAC,               flac);
     REGISTER_PARSER(GSM,                gsm);
     REGISTER_PARSER(H261,               h261);
diff --git a/libavcodec/alpha/Makefile b/libavcodec/alpha/Makefile
index e28200d..6f22137 100644
--- a/libavcodec/alpha/Makefile
+++ b/libavcodec/alpha/Makefile
@@ -4,4 +4,6 @@
         alpha/motion_est_mvi_asm.o                                      \
         alpha/simple_idct_alpha.o                                       \
 
+OBJS-$(CONFIG_HPELDSP)                  += alpha/hpeldsp_alpha.o        \
+                                           alpha/hpeldsp_alpha_asm.o
 OBJS-$(CONFIG_MPEGVIDEO)                += alpha/mpegvideo_alpha.o
diff --git a/libavcodec/alpha/dsputil_alpha.c b/libavcodec/alpha/dsputil_alpha.c
index cb62665..03ba0a8 100644
--- a/libavcodec/alpha/dsputil_alpha.c
+++ b/libavcodec/alpha/dsputil_alpha.c
@@ -119,197 +119,12 @@
     } while (n);
 }
 
-static inline uint64_t avg2_no_rnd(uint64_t a, uint64_t b)
-{
-    return (a & b) + (((a ^ b) & BYTE_VEC(0xfe)) >> 1);
-}
-
-static inline uint64_t avg2(uint64_t a, uint64_t b)
-{
-    return (a | b) - (((a ^ b) & BYTE_VEC(0xfe)) >> 1);
-}
-
-#if 0
-/* The XY2 routines basically utilize this scheme, but reuse parts in
-   each iteration.  */
-static inline uint64_t avg4(uint64_t l1, uint64_t l2, uint64_t l3, uint64_t l4)
-{
-    uint64_t r1 = ((l1 & ~BYTE_VEC(0x03)) >> 2)
-                + ((l2 & ~BYTE_VEC(0x03)) >> 2)
-                + ((l3 & ~BYTE_VEC(0x03)) >> 2)
-                + ((l4 & ~BYTE_VEC(0x03)) >> 2);
-    uint64_t r2 = ((  (l1 & BYTE_VEC(0x03))
-                    + (l2 & BYTE_VEC(0x03))
-                    + (l3 & BYTE_VEC(0x03))
-                    + (l4 & BYTE_VEC(0x03))
-                    + BYTE_VEC(0x02)) >> 2) & BYTE_VEC(0x03);
-    return r1 + r2;
-}
-#endif
-
-#define OP(LOAD, STORE)                         \
-    do {                                        \
-        STORE(LOAD(pixels), block);             \
-        pixels += line_size;                    \
-        block += line_size;                     \
-    } while (--h)
-
-#define OP_X2(LOAD, STORE)                                      \
-    do {                                                        \
-        uint64_t pix1, pix2;                                    \
-                                                                \
-        pix1 = LOAD(pixels);                                    \
-        pix2 = pix1 >> 8 | ((uint64_t) pixels[8] << 56);        \
-        STORE(AVG2(pix1, pix2), block);                         \
-        pixels += line_size;                                    \
-        block += line_size;                                     \
-    } while (--h)
-
-#define OP_Y2(LOAD, STORE)                      \
-    do {                                        \
-        uint64_t pix = LOAD(pixels);            \
-        do {                                    \
-            uint64_t next_pix;                  \
-                                                \
-            pixels += line_size;                \
-            next_pix = LOAD(pixels);            \
-            STORE(AVG2(pix, next_pix), block);  \
-            block += line_size;                 \
-            pix = next_pix;                     \
-        } while (--h);                          \
-    } while (0)
-
-#define OP_XY2(LOAD, STORE)                                                 \
-    do {                                                                    \
-        uint64_t pix1 = LOAD(pixels);                                       \
-        uint64_t pix2 = pix1 >> 8 | ((uint64_t) pixels[8] << 56);           \
-        uint64_t pix_l = (pix1 & BYTE_VEC(0x03))                            \
-                       + (pix2 & BYTE_VEC(0x03));                           \
-        uint64_t pix_h = ((pix1 & ~BYTE_VEC(0x03)) >> 2)                    \
-                       + ((pix2 & ~BYTE_VEC(0x03)) >> 2);                   \
-                                                                            \
-        do {                                                                \
-            uint64_t npix1, npix2;                                          \
-            uint64_t npix_l, npix_h;                                        \
-            uint64_t avg;                                                   \
-                                                                            \
-            pixels += line_size;                                            \
-            npix1 = LOAD(pixels);                                           \
-            npix2 = npix1 >> 8 | ((uint64_t) pixels[8] << 56);              \
-            npix_l = (npix1 & BYTE_VEC(0x03))                               \
-                   + (npix2 & BYTE_VEC(0x03));                              \
-            npix_h = ((npix1 & ~BYTE_VEC(0x03)) >> 2)                       \
-                   + ((npix2 & ~BYTE_VEC(0x03)) >> 2);                      \
-            avg = (((pix_l + npix_l + AVG4_ROUNDER) >> 2) & BYTE_VEC(0x03)) \
-                + pix_h + npix_h;                                           \
-            STORE(avg, block);                                              \
-                                                                            \
-            block += line_size;                                             \
-            pix_l = npix_l;                                                 \
-            pix_h = npix_h;                                                 \
-        } while (--h);                                                      \
-    } while (0)
-
-#define MAKE_OP(OPNAME, SUFF, OPKIND, STORE)                                \
-static void OPNAME ## _pixels ## SUFF ## _axp                               \
-        (uint8_t *restrict block, const uint8_t *restrict pixels,           \
-         ptrdiff_t line_size, int h)                                        \
-{                                                                           \
-    if ((size_t) pixels & 0x7) {                                            \
-        OPKIND(uldq, STORE);                                                \
-    } else {                                                                \
-        OPKIND(ldq, STORE);                                                 \
-    }                                                                       \
-}                                                                           \
-                                                                            \
-static void OPNAME ## _pixels16 ## SUFF ## _axp                             \
-        (uint8_t *restrict block, const uint8_t *restrict pixels,           \
-         ptrdiff_t line_size, int h)                                        \
-{                                                                           \
-    OPNAME ## _pixels ## SUFF ## _axp(block,     pixels,     line_size, h); \
-    OPNAME ## _pixels ## SUFF ## _axp(block + 8, pixels + 8, line_size, h); \
-}
-
-#define PIXOP(OPNAME, STORE)                    \
-    MAKE_OP(OPNAME, ,     OP,     STORE)        \
-    MAKE_OP(OPNAME, _x2,  OP_X2,  STORE)        \
-    MAKE_OP(OPNAME, _y2,  OP_Y2,  STORE)        \
-    MAKE_OP(OPNAME, _xy2, OP_XY2, STORE)
-
-/* Rounding primitives.  */
-#define AVG2 avg2
-#define AVG4 avg4
-#define AVG4_ROUNDER BYTE_VEC(0x02)
-#define STORE(l, b) stq(l, b)
-PIXOP(put, STORE);
-
-#undef STORE
-#define STORE(l, b) stq(AVG2(l, ldq(b)), b);
-PIXOP(avg, STORE);
-
-/* Not rounding primitives.  */
-#undef AVG2
-#undef AVG4
-#undef AVG4_ROUNDER
-#undef STORE
-#define AVG2 avg2_no_rnd
-#define AVG4 avg4_no_rnd
-#define AVG4_ROUNDER BYTE_VEC(0x01)
-#define STORE(l, b) stq(l, b)
-PIXOP(put_no_rnd, STORE);
-
-#undef STORE
-#define STORE(l, b) stq(AVG2(l, ldq(b)), b);
-PIXOP(avg_no_rnd, STORE);
-
-static void put_pixels16_axp_asm(uint8_t *block, const uint8_t *pixels,
-                                 ptrdiff_t line_size, int h)
-{
-    put_pixels_axp_asm(block,     pixels,     line_size, h);
-    put_pixels_axp_asm(block + 8, pixels + 8, line_size, h);
-}
-
 av_cold void ff_dsputil_init_alpha(DSPContext *c, AVCodecContext *avctx)
 {
     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
 
     if (!high_bit_depth) {
-    c->put_pixels_tab[0][0] = put_pixels16_axp_asm;
-    c->put_pixels_tab[0][1] = put_pixels16_x2_axp;
-    c->put_pixels_tab[0][2] = put_pixels16_y2_axp;
-    c->put_pixels_tab[0][3] = put_pixels16_xy2_axp;
-
-    c->put_no_rnd_pixels_tab[0][0] = put_pixels16_axp_asm;
-    c->put_no_rnd_pixels_tab[0][1] = put_no_rnd_pixels16_x2_axp;
-    c->put_no_rnd_pixels_tab[0][2] = put_no_rnd_pixels16_y2_axp;
-    c->put_no_rnd_pixels_tab[0][3] = put_no_rnd_pixels16_xy2_axp;
-
-    c->avg_pixels_tab[0][0] = avg_pixels16_axp;
-    c->avg_pixels_tab[0][1] = avg_pixels16_x2_axp;
-    c->avg_pixels_tab[0][2] = avg_pixels16_y2_axp;
-    c->avg_pixels_tab[0][3] = avg_pixels16_xy2_axp;
-
-    c->avg_no_rnd_pixels_tab[0] = avg_no_rnd_pixels16_axp;
-    c->avg_no_rnd_pixels_tab[1] = avg_no_rnd_pixels16_x2_axp;
-    c->avg_no_rnd_pixels_tab[2] = avg_no_rnd_pixels16_y2_axp;
-    c->avg_no_rnd_pixels_tab[3] = avg_no_rnd_pixels16_xy2_axp;
-
-    c->put_pixels_tab[1][0] = put_pixels_axp_asm;
-    c->put_pixels_tab[1][1] = put_pixels_x2_axp;
-    c->put_pixels_tab[1][2] = put_pixels_y2_axp;
-    c->put_pixels_tab[1][3] = put_pixels_xy2_axp;
-
-    c->put_no_rnd_pixels_tab[1][0] = put_pixels_axp_asm;
-    c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels_x2_axp;
-    c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels_y2_axp;
-    c->put_no_rnd_pixels_tab[1][3] = put_no_rnd_pixels_xy2_axp;
-
-    c->avg_pixels_tab[1][0] = avg_pixels_axp;
-    c->avg_pixels_tab[1][1] = avg_pixels_x2_axp;
-    c->avg_pixels_tab[1][2] = avg_pixels_y2_axp;
-    c->avg_pixels_tab[1][3] = avg_pixels_xy2_axp;
-
-    c->clear_blocks = clear_blocks_axp;
+        c->clear_blocks = clear_blocks_axp;
     }
 
     /* amask clears all bits that correspond to present features.  */
diff --git a/libavcodec/alpha/dsputil_alpha.h b/libavcodec/alpha/dsputil_alpha.h
index cf5ca3b..828cd16 100644
--- a/libavcodec/alpha/dsputil_alpha.h
+++ b/libavcodec/alpha/dsputil_alpha.h
@@ -26,8 +26,6 @@
 void ff_simple_idct_put_axp(uint8_t *dest, int line_size, int16_t *block);
 void ff_simple_idct_add_axp(uint8_t *dest, int line_size, int16_t *block);
 
-void put_pixels_axp_asm(uint8_t *block, const uint8_t *pixels,
-                        ptrdiff_t line_size, int h);
 void put_pixels_clamped_mvi_asm(const int16_t *block, uint8_t *pixels,
                                 int line_size);
 void add_pixels_clamped_mvi_asm(const int16_t *block, uint8_t *pixels,
diff --git a/libavcodec/alpha/dsputil_alpha_asm.S b/libavcodec/alpha/dsputil_alpha_asm.S
index 557ba57..d801bcf 100644
--- a/libavcodec/alpha/dsputil_alpha_asm.S
+++ b/libavcodec/alpha/dsputil_alpha_asm.S
@@ -26,115 +26,12 @@
 
 #include "regdef.h"
 
-/* Some nicer register names.  */
-#define ta t10
-#define tb t11
-#define tc t12
-#define td AT
-/* Danger: these overlap with the argument list and the return value */
-#define te a5
-#define tf a4
-#define tg a3
-#define th v0
-
         .set noat
         .set noreorder
         .arch pca56
         .text
 
 /************************************************************************
- * void put_pixels_axp_asm(uint8_t *block, const uint8_t *pixels,
- *                         int line_size, int h)
- */
-        .align 6
-        .globl put_pixels_axp_asm
-        .ent put_pixels_axp_asm
-put_pixels_axp_asm:
-        .frame sp, 0, ra
-        .prologue 0
-
-        and     a1, 7, t0
-        beq     t0, $aligned
-
-        .align 4
-$unaligned:
-        ldq_u   t0, 0(a1)
-        ldq_u   t1, 8(a1)
-        addq    a1, a2, a1
-        nop
-
-        ldq_u   t2, 0(a1)
-        ldq_u   t3, 8(a1)
-        addq    a1, a2, a1
-        nop
-
-        ldq_u   t4, 0(a1)
-        ldq_u   t5, 8(a1)
-        addq    a1, a2, a1
-        nop
-
-        ldq_u   t6, 0(a1)
-        ldq_u   t7, 8(a1)
-        extql   t0, a1, t0
-        addq    a1, a2, a1
-
-        extqh   t1, a1, t1
-        addq    a0, a2, t8
-        extql   t2, a1, t2
-        addq    t8, a2, t9
-
-        extqh   t3, a1, t3
-        addq    t9, a2, ta
-        extql   t4, a1, t4
-        or      t0, t1, t0
-
-        extqh   t5, a1, t5
-        or      t2, t3, t2
-        extql   t6, a1, t6
-        or      t4, t5, t4
-
-        extqh   t7, a1, t7
-        or      t6, t7, t6
-        stq     t0, 0(a0)
-        stq     t2, 0(t8)
-
-        stq     t4, 0(t9)
-        subq    a3, 4, a3
-        stq     t6, 0(ta)
-        addq    ta, a2, a0
-
-        bne     a3, $unaligned
-        ret
-
-        .align 4
-$aligned:
-        ldq     t0, 0(a1)
-        addq    a1, a2, a1
-        ldq     t1, 0(a1)
-        addq    a1, a2, a1
-
-        ldq     t2, 0(a1)
-        addq    a1, a2, a1
-        ldq     t3, 0(a1)
-
-        addq    a0, a2, t4
-        addq    a1, a2, a1
-        addq    t4, a2, t5
-        subq    a3, 4, a3
-
-        stq     t0, 0(a0)
-        addq    t5, a2, t6
-        stq     t1, 0(t4)
-        addq    t6, a2, a0
-
-        stq     t2, 0(t5)
-        stq     t3, 0(t6)
-
-        bne     a3, $aligned
-        ret
-        .end put_pixels_axp_asm
-
-/************************************************************************
  * void put_pixels_clamped_mvi_asm(const int16_t *block, uint8_t *pixels,
  *                                 int line_size)
  */
diff --git a/libavcodec/alpha/hpeldsp_alpha.c b/libavcodec/alpha/hpeldsp_alpha.c
new file mode 100644
index 0000000..8d54807
--- /dev/null
+++ b/libavcodec/alpha/hpeldsp_alpha.c
@@ -0,0 +1,213 @@
+/*
+ * Alpha optimized DSP utils
+ * Copyright (c) 2002 Falk Hueffner <falk@debian.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
+ */
+
+#include "libavutil/attributes.h"
+#include "libavcodec/hpeldsp.h"
+#include "hpeldsp_alpha.h"
+#include "asm.h"
+
+static inline uint64_t avg2_no_rnd(uint64_t a, uint64_t b)
+{
+    return (a & b) + (((a ^ b) & BYTE_VEC(0xfe)) >> 1);
+}
+
+static inline uint64_t avg2(uint64_t a, uint64_t b)
+{
+    return (a | b) - (((a ^ b) & BYTE_VEC(0xfe)) >> 1);
+}
+
+#if 0
+/* The XY2 routines basically utilize this scheme, but reuse parts in
+   each iteration.  */
+static inline uint64_t avg4(uint64_t l1, uint64_t l2, uint64_t l3, uint64_t l4)
+{
+    uint64_t r1 = ((l1 & ~BYTE_VEC(0x03)) >> 2)
+                + ((l2 & ~BYTE_VEC(0x03)) >> 2)
+                + ((l3 & ~BYTE_VEC(0x03)) >> 2)
+                + ((l4 & ~BYTE_VEC(0x03)) >> 2);
+    uint64_t r2 = ((  (l1 & BYTE_VEC(0x03))
+                    + (l2 & BYTE_VEC(0x03))
+                    + (l3 & BYTE_VEC(0x03))
+                    + (l4 & BYTE_VEC(0x03))
+                    + BYTE_VEC(0x02)) >> 2) & BYTE_VEC(0x03);
+    return r1 + r2;
+}
+#endif
+
+#define OP(LOAD, STORE)                         \
+    do {                                        \
+        STORE(LOAD(pixels), block);             \
+        pixels += line_size;                    \
+        block += line_size;                     \
+    } while (--h)
+
+#define OP_X2(LOAD, STORE)                                      \
+    do {                                                        \
+        uint64_t pix1, pix2;                                    \
+                                                                \
+        pix1 = LOAD(pixels);                                    \
+        pix2 = pix1 >> 8 | ((uint64_t) pixels[8] << 56);        \
+        STORE(AVG2(pix1, pix2), block);                         \
+        pixels += line_size;                                    \
+        block += line_size;                                     \
+    } while (--h)
+
+#define OP_Y2(LOAD, STORE)                      \
+    do {                                        \
+        uint64_t pix = LOAD(pixels);            \
+        do {                                    \
+            uint64_t next_pix;                  \
+                                                \
+            pixels += line_size;                \
+            next_pix = LOAD(pixels);            \
+            STORE(AVG2(pix, next_pix), block);  \
+            block += line_size;                 \
+            pix = next_pix;                     \
+        } while (--h);                          \
+    } while (0)
+
+#define OP_XY2(LOAD, STORE)                                                 \
+    do {                                                                    \
+        uint64_t pix1 = LOAD(pixels);                                       \
+        uint64_t pix2 = pix1 >> 8 | ((uint64_t) pixels[8] << 56);           \
+        uint64_t pix_l = (pix1 & BYTE_VEC(0x03))                            \
+                       + (pix2 & BYTE_VEC(0x03));                           \
+        uint64_t pix_h = ((pix1 & ~BYTE_VEC(0x03)) >> 2)                    \
+                       + ((pix2 & ~BYTE_VEC(0x03)) >> 2);                   \
+                                                                            \
+        do {                                                                \
+            uint64_t npix1, npix2;                                          \
+            uint64_t npix_l, npix_h;                                        \
+            uint64_t avg;                                                   \
+                                                                            \
+            pixels += line_size;                                            \
+            npix1 = LOAD(pixels);                                           \
+            npix2 = npix1 >> 8 | ((uint64_t) pixels[8] << 56);              \
+            npix_l = (npix1 & BYTE_VEC(0x03))                               \
+                   + (npix2 & BYTE_VEC(0x03));                              \
+            npix_h = ((npix1 & ~BYTE_VEC(0x03)) >> 2)                       \
+                   + ((npix2 & ~BYTE_VEC(0x03)) >> 2);                      \
+            avg = (((pix_l + npix_l + AVG4_ROUNDER) >> 2) & BYTE_VEC(0x03)) \
+                + pix_h + npix_h;                                           \
+            STORE(avg, block);                                              \
+                                                                            \
+            block += line_size;                                             \
+            pix_l = npix_l;                                                 \
+            pix_h = npix_h;                                                 \
+        } while (--h);                                                      \
+    } while (0)
+
+#define MAKE_OP(OPNAME, SUFF, OPKIND, STORE)                                \
+static void OPNAME ## _pixels ## SUFF ## _axp                               \
+        (uint8_t *restrict block, const uint8_t *restrict pixels,           \
+         ptrdiff_t line_size, int h)                                        \
+{                                                                           \
+    if ((size_t) pixels & 0x7) {                                            \
+        OPKIND(uldq, STORE);                                                \
+    } else {                                                                \
+        OPKIND(ldq, STORE);                                                 \
+    }                                                                       \
+}                                                                           \
+                                                                            \
+static void OPNAME ## _pixels16 ## SUFF ## _axp                             \
+        (uint8_t *restrict block, const uint8_t *restrict pixels,           \
+         ptrdiff_t line_size, int h)                                        \
+{                                                                           \
+    OPNAME ## _pixels ## SUFF ## _axp(block,     pixels,     line_size, h); \
+    OPNAME ## _pixels ## SUFF ## _axp(block + 8, pixels + 8, line_size, h); \
+}
+
+#define PIXOP(OPNAME, STORE)                    \
+    MAKE_OP(OPNAME, ,     OP,     STORE)        \
+    MAKE_OP(OPNAME, _x2,  OP_X2,  STORE)        \
+    MAKE_OP(OPNAME, _y2,  OP_Y2,  STORE)        \
+    MAKE_OP(OPNAME, _xy2, OP_XY2, STORE)
+
+/* Rounding primitives.  */
+#define AVG2 avg2
+#define AVG4 avg4
+#define AVG4_ROUNDER BYTE_VEC(0x02)
+#define STORE(l, b) stq(l, b)
+PIXOP(put, STORE);
+
+#undef STORE
+#define STORE(l, b) stq(AVG2(l, ldq(b)), b);
+PIXOP(avg, STORE);
+
+/* Not rounding primitives.  */
+#undef AVG2
+#undef AVG4
+#undef AVG4_ROUNDER
+#undef STORE
+#define AVG2 avg2_no_rnd
+#define AVG4 avg4_no_rnd
+#define AVG4_ROUNDER BYTE_VEC(0x01)
+#define STORE(l, b) stq(l, b)
+PIXOP(put_no_rnd, STORE);
+
+#undef STORE
+#define STORE(l, b) stq(AVG2(l, ldq(b)), b);
+PIXOP(avg_no_rnd, STORE);
+
+static void put_pixels16_axp_asm(uint8_t *block, const uint8_t *pixels,
+                                 ptrdiff_t line_size, int h)
+{
+    put_pixels_axp_asm(block,     pixels,     line_size, h);
+    put_pixels_axp_asm(block + 8, pixels + 8, line_size, h);
+}
+
+av_cold void ff_hpeldsp_init_alpha(HpelDSPContext *c, int flags)
+{
+    c->put_pixels_tab[0][0] = put_pixels16_axp_asm;
+    c->put_pixels_tab[0][1] = put_pixels16_x2_axp;
+    c->put_pixels_tab[0][2] = put_pixels16_y2_axp;
+    c->put_pixels_tab[0][3] = put_pixels16_xy2_axp;
+
+    c->put_no_rnd_pixels_tab[0][0] = put_pixels16_axp_asm;
+    c->put_no_rnd_pixels_tab[0][1] = put_no_rnd_pixels16_x2_axp;
+    c->put_no_rnd_pixels_tab[0][2] = put_no_rnd_pixels16_y2_axp;
+    c->put_no_rnd_pixels_tab[0][3] = put_no_rnd_pixels16_xy2_axp;
+
+    c->avg_pixels_tab[0][0] = avg_pixels16_axp;
+    c->avg_pixels_tab[0][1] = avg_pixels16_x2_axp;
+    c->avg_pixels_tab[0][2] = avg_pixels16_y2_axp;
+    c->avg_pixels_tab[0][3] = avg_pixels16_xy2_axp;
+
+    c->avg_no_rnd_pixels_tab[0] = avg_no_rnd_pixels16_axp;
+    c->avg_no_rnd_pixels_tab[1] = avg_no_rnd_pixels16_x2_axp;
+    c->avg_no_rnd_pixels_tab[2] = avg_no_rnd_pixels16_y2_axp;
+    c->avg_no_rnd_pixels_tab[3] = avg_no_rnd_pixels16_xy2_axp;
+
+    c->put_pixels_tab[1][0] = put_pixels_axp_asm;
+    c->put_pixels_tab[1][1] = put_pixels_x2_axp;
+    c->put_pixels_tab[1][2] = put_pixels_y2_axp;
+    c->put_pixels_tab[1][3] = put_pixels_xy2_axp;
+
+    c->put_no_rnd_pixels_tab[1][0] = put_pixels_axp_asm;
+    c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels_x2_axp;
+    c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels_y2_axp;
+    c->put_no_rnd_pixels_tab[1][3] = put_no_rnd_pixels_xy2_axp;
+
+    c->avg_pixels_tab[1][0] = avg_pixels_axp;
+    c->avg_pixels_tab[1][1] = avg_pixels_x2_axp;
+    c->avg_pixels_tab[1][2] = avg_pixels_y2_axp;
+    c->avg_pixels_tab[1][3] = avg_pixels_xy2_axp;
+}
diff --git a/libavcodec/alpha/hpeldsp_alpha.h b/libavcodec/alpha/hpeldsp_alpha.h
new file mode 100644
index 0000000..985182c
--- /dev/null
+++ b/libavcodec/alpha/hpeldsp_alpha.h
@@ -0,0 +1,28 @@
+/*
+ * 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 AVCODEC_ALPHA_HPELDSP_ALPHA_H
+#define AVCODEC_ALPHA_HPELDSP_ALPHA_H
+
+#include <stdint.h>
+#include <stddef.h>
+
+void put_pixels_axp_asm(uint8_t *block, const uint8_t *pixels,
+                        ptrdiff_t line_size, int h);
+
+#endif /* AVCODEC_ALPHA_HPELDSP_ALPHA_H */
diff --git a/libavcodec/alpha/hpeldsp_alpha_asm.S b/libavcodec/alpha/hpeldsp_alpha_asm.S
new file mode 100644
index 0000000..df386c4
--- /dev/null
+++ b/libavcodec/alpha/hpeldsp_alpha_asm.S
@@ -0,0 +1,125 @@
+/*
+ * Alpha optimized DSP utils
+ * Copyright (c) 2002 Falk Hueffner <falk@debian.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
+ */
+
+/*
+ * These functions are scheduled for pca56. They should work
+ * reasonably on ev6, though.
+ */
+
+#include "regdef.h"
+
+
+        .set noat
+        .set noreorder
+        .arch pca56
+        .text
+
+/************************************************************************
+ * void put_pixels_axp_asm(uint8_t *block, const uint8_t *pixels,
+ *                         int line_size, int h)
+ */
+        .align 6
+        .globl put_pixels_axp_asm
+        .ent put_pixels_axp_asm
+put_pixels_axp_asm:
+        .frame sp, 0, ra
+        .prologue 0
+
+        and     a1, 7, t0
+        beq     t0, $aligned
+
+        .align 4
+$unaligned:
+        ldq_u   t0, 0(a1)
+        ldq_u   t1, 8(a1)
+        addq    a1, a2, a1
+        nop
+
+        ldq_u   t2, 0(a1)
+        ldq_u   t3, 8(a1)
+        addq    a1, a2, a1
+        nop
+
+        ldq_u   t4, 0(a1)
+        ldq_u   t5, 8(a1)
+        addq    a1, a2, a1
+        nop
+
+        ldq_u   t6, 0(a1)
+        ldq_u   t7, 8(a1)
+        extql   t0, a1, t0
+        addq    a1, a2, a1
+
+        extqh   t1, a1, t1
+        addq    a0, a2, t8
+        extql   t2, a1, t2
+        addq    t8, a2, t9
+
+        extqh   t3, a1, t3
+        addq    t9, a2, ta
+        extql   t4, a1, t4
+        or      t0, t1, t0
+
+        extqh   t5, a1, t5
+        or      t2, t3, t2
+        extql   t6, a1, t6
+        or      t4, t5, t4
+
+        extqh   t7, a1, t7
+        or      t6, t7, t6
+        stq     t0, 0(a0)
+        stq     t2, 0(t8)
+
+        stq     t4, 0(t9)
+        subq    a3, 4, a3
+        stq     t6, 0(ta)
+        addq    ta, a2, a0
+
+        bne     a3, $unaligned
+        ret
+
+        .align 4
+$aligned:
+        ldq     t0, 0(a1)
+        addq    a1, a2, a1
+        ldq     t1, 0(a1)
+        addq    a1, a2, a1
+
+        ldq     t2, 0(a1)
+        addq    a1, a2, a1
+        ldq     t3, 0(a1)
+
+        addq    a0, a2, t4
+        addq    a1, a2, a1
+        addq    t4, a2, t5
+        subq    a3, 4, a3
+
+        stq     t0, 0(a0)
+        addq    t5, a2, t6
+        stq     t1, 0(t4)
+        addq    t6, a2, a0
+
+        stq     t2, 0(t5)
+        stq     t3, 0(t6)
+
+        bne     a3, $aligned
+        ret
+        .end put_pixels_axp_asm
diff --git a/libavcodec/alpha/regdef.h b/libavcodec/alpha/regdef.h
index aa1959f..f05577a 100644
--- a/libavcodec/alpha/regdef.h
+++ b/libavcodec/alpha/regdef.h
@@ -63,4 +63,15 @@
 #define sp      $30     /* stack pointer */
 #define zero    $31     /* reads as zero, writes are noops */
 
+/* Some nicer register names.  */
+#define ta t10
+#define tb t11
+#define tc t12
+#define td AT
+/* Danger: these overlap with the argument list and the return value */
+#define te a5
+#define tf a4
+#define tg a3
+#define th v0
+
 #endif /* AVCODEC_ALPHA_REGDEF_H */
diff --git a/libavcodec/alsdec.c b/libavcodec/alsdec.c
index a6d70d4..96d467c 100644
--- a/libavcodec/alsdec.c
+++ b/libavcodec/alsdec.c
@@ -433,7 +433,8 @@
     #define MISSING_ERR(cond, str, errval)              \
     {                                                   \
         if (cond) {                                     \
-            av_log_missing_feature(ctx->avctx, str, 0); \
+            avpriv_report_missing_feature(ctx->avctx,   \
+                                          str);         \
             error = errval;                             \
         }                                               \
     }
@@ -1480,10 +1481,8 @@
 
     /* get output buffer */
     frame->nb_samples = ctx->cur_frame_length;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed.\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     // transform decoded frame into output format
     #define INTERLEAVE_OUTPUT(bps)                                                   \
diff --git a/libavcodec/amrnbdec.c b/libavcodec/amrnbdec.c
index 6e14a60..6376db1 100644
--- a/libavcodec/amrnbdec.c
+++ b/libavcodec/amrnbdec.c
@@ -163,7 +163,7 @@
     int i;
 
     if (avctx->channels > 1) {
-        av_log_missing_feature(avctx, "multi-channel AMR", 0);
+        avpriv_report_missing_feature(avctx, "multi-channel AMR");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -963,10 +963,8 @@
 
     /* get output buffer */
     frame->nb_samples = AMR_BLOCK_SIZE;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     buf_out = (float *)frame->data[0];
 
     p->cur_frame_mode = unpack_bitstream(p, buf, buf_size);
@@ -975,7 +973,7 @@
         return AVERROR_INVALIDDATA;
     }
     if (p->cur_frame_mode == MODE_DTX) {
-        av_log_missing_feature(avctx, "dtx mode", 0);
+        avpriv_report_missing_feature(avctx, "dtx mode");
         av_log(avctx, AV_LOG_INFO, "Note: libopencore_amrnb supports dtx\n");
         return AVERROR_PATCHWELCOME;
     }
diff --git a/libavcodec/amrwbdec.c b/libavcodec/amrwbdec.c
index c0481325e..8c3eb56 100644
--- a/libavcodec/amrwbdec.c
+++ b/libavcodec/amrwbdec.c
@@ -99,7 +99,7 @@
     int i;
 
     if (avctx->channels > 1) {
-        av_log_missing_feature(avctx, "multi-channel AMR", 0);
+        avpriv_report_missing_feature(avctx, "multi-channel AMR");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -1112,10 +1112,8 @@
 
     /* get output buffer */
     frame->nb_samples = 4 * AMRWB_SFR_SIZE_16k;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     buf_out = (float *)frame->data[0];
 
     header_size      = decode_mime_header(ctx, buf);
@@ -1137,7 +1135,7 @@
         av_log(avctx, AV_LOG_ERROR, "Encountered a bad or corrupted frame\n");
 
     if (ctx->fr_cur_mode == MODE_SID) { /* Comfort noise frame */
-        av_log_missing_feature(avctx, "SID mode", 1);
+        avpriv_request_sample(avctx, "SID mode");
         return AVERROR_PATCHWELCOME;
     }
 
diff --git a/libavcodec/anm.c b/libavcodec/anm.c
index 08f5170..9aef6d3 100644
--- a/libavcodec/anm.c
+++ b/libavcodec/anm.c
@@ -26,9 +26,10 @@
 
 #include "avcodec.h"
 #include "bytestream.h"
+#include "internal.h"
 
 typedef struct AnmContext {
-    AVFrame frame;
+    AVFrame *frame;
     int palette[AVPALETTE_COUNT];
     GetByteContext gb;
     int x;  ///< x coordinate position
@@ -41,8 +42,10 @@
 
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
 
-    avcodec_get_frame_defaults(&s->frame);
-    s->frame.reference = 3;
+    s->frame = av_frame_alloc();
+    if (!s->frame)
+        return AVERROR(ENOMEM);
+
     bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size);
     if (bytestream2_get_bytes_left(&s->gb) < 16 * 8 + 4 * 256)
         return AVERROR_INVALIDDATA;
@@ -114,21 +117,19 @@
     uint8_t *dst, *dst_end;
     int count, ret;
 
-    if ((ret = avctx->reget_buffer(avctx, &s->frame)) < 0){
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
         return ret;
-    }
-    dst     = s->frame.data[0];
-    dst_end = s->frame.data[0] + s->frame.linesize[0]*avctx->height;
+    dst     = s->frame->data[0];
+    dst_end = s->frame->data[0] + s->frame->linesize[0]*avctx->height;
 
     bytestream2_init(&s->gb, avpkt->data, buf_size);
 
     if (bytestream2_get_byte(&s->gb) != 0x42) {
-        av_log_ask_for_sample(avctx, "unknown record type\n");
+        avpriv_request_sample(avctx, "Unknown record type");
         return AVERROR_INVALIDDATA;
     }
     if (bytestream2_get_byte(&s->gb)) {
-        av_log_ask_for_sample(avctx, "padding bytes not supported\n");
+        avpriv_request_sample(avctx, "Padding bytes");
         return AVERROR_PATCHWELCOME;
     }
     bytestream2_skip(&s->gb, 2);
@@ -137,7 +138,7 @@
     do {
         /* if statements are ordered by probability */
 #define OP(gb, pixel, count) \
-    op(&dst, dst_end, (gb), (pixel), (count), &s->x, avctx->width, s->frame.linesize[0])
+    op(&dst, dst_end, (gb), (pixel), (count), &s->x, avctx->width, s->frame->linesize[0])
 
         int type = bytestream2_get_byte(&s->gb);
         count = type & 0x7F;
@@ -158,7 +159,7 @@
                 if (type == 0)
                     break; // stop
                 if (type == 2) {
-                    av_log_ask_for_sample(avctx, "unknown opcode");
+                    avpriv_request_sample(avctx, "Unknown opcode");
                     return AVERROR_PATCHWELCOME;
                 }
                 continue;
@@ -169,18 +170,20 @@
         }
     } while (bytestream2_get_bytes_left(&s->gb) > 0);
 
-    memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
+    memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
 
     *got_frame = 1;
-    *(AVFrame*)data = s->frame;
+    if ((ret = av_frame_ref(data, s->frame)) < 0)
+        return ret;
+
     return buf_size;
 }
 
 static av_cold int decode_end(AVCodecContext *avctx)
 {
     AnmContext *s = avctx->priv_data;
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+
+    av_frame_free(&s->frame);
     return 0;
 }
 
diff --git a/libavcodec/ansi.c b/libavcodec/ansi.c
index 51339d2..93915f8 100644
--- a/libavcodec/ansi.c
+++ b/libavcodec/ansi.c
@@ -25,6 +25,7 @@
  */
 
 #include "libavutil/common.h"
+#include "libavutil/frame.h"
 #include "libavutil/lfg.h"
 #include "libavutil/xga_font_data.h"
 #include "avcodec.h"
@@ -50,7 +51,7 @@
 };
 
 typedef struct {
-    AVFrame frame;
+    AVFrame *frame;
     int x;                /**< x cursor position (pixels) */
     int y;                /**< y cursor position (pixels) */
     int sx;               /**< saved x cursor position (pixels) */
@@ -79,13 +80,16 @@
     AnsiContext *s = avctx->priv_data;
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
 
+    s->frame = av_frame_alloc();
+    if (!s->frame)
+        return AVERROR(ENOMEM);
+
     /* defaults */
     s->font        = avpriv_vga16_font;
     s->font_height = 16;
     s->fg          = DEFAULT_FG_COLOR;
     s->bg          = DEFAULT_BG_COLOR;
 
-    avcodec_get_frame_defaults(&s->frame);
     if (!avctx->width || !avctx->height)
         avcodec_set_dimensions(avctx, 80<<3, 25<<4);
 
@@ -119,11 +123,11 @@
 
     i = 0;
     for (; i < avctx->height - s->font_height; i++)
-        memcpy(s->frame.data[0] + i * s->frame.linesize[0],
-               s->frame.data[0] + (i + s->font_height) * s->frame.linesize[0],
+        memcpy(s->frame->data[0] + i * s->frame->linesize[0],
+               s->frame->data[0] + (i + s->font_height) * s->frame->linesize[0],
                avctx->width);
     for (; i < avctx->height; i++)
-        memset(s->frame.data[0] + i * s->frame.linesize[0],
+        memset(s->frame->data[0] + i * s->frame->linesize[0],
             DEFAULT_BG_COLOR, avctx->width);
 }
 
@@ -132,7 +136,7 @@
     AnsiContext *s = avctx->priv_data;
     int i;
     for (i = 0; i < s->font_height; i++)
-        memset(s->frame.data[0] + (s->y + i)*s->frame.linesize[0] + xoffset,
+        memset(s->frame->data[0] + (s->y + i)*s->frame->linesize[0] + xoffset,
             DEFAULT_BG_COLOR, xlength);
 }
 
@@ -141,7 +145,7 @@
     AnsiContext *s = avctx->priv_data;
     int i;
     for (i = 0; i < avctx->height; i++)
-        memset(s->frame.data[0] + i * s->frame.linesize[0], DEFAULT_BG_COLOR, avctx->width);
+        memset(s->frame->data[0] + i * s->frame->linesize[0], DEFAULT_BG_COLOR, avctx->width);
     s->x = s->y = 0;
 }
 
@@ -162,8 +166,8 @@
         FFSWAP(int, fg, bg);
     if ((s->attributes & ATTR_CONCEALED))
         fg = bg;
-    ff_draw_pc_font(s->frame.data[0] + s->y * s->frame.linesize[0] + s->x,
-                    s->frame.linesize[0], s->font, s->font_height, c, fg, bg);
+    ff_draw_pc_font(s->frame->data[0] + s->y * s->frame->linesize[0] + s->x,
+                    s->frame->linesize[0], s->font, s->font_height, c, fg, bg);
     s->x += FONT_WIDTH;
     if (s->x >= avctx->width) {
         s->x = 0;
@@ -237,20 +241,17 @@
             height = 60<<4;
             break;
         default:
-            av_log_ask_for_sample(avctx, "unsupported screen mode\n");
+            avpriv_request_sample(avctx, "Unsupported screen mode");
         }
         if (width != avctx->width || height != avctx->height) {
-            if (s->frame.data[0])
-                avctx->release_buffer(avctx, &s->frame);
+            av_frame_unref(s->frame);
             avcodec_set_dimensions(avctx, width, height);
-            ret = ff_get_buffer(avctx, &s->frame);
-            if (ret < 0) {
-                av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+            if ((ret = ff_get_buffer(avctx, s->frame,
+                                     AV_GET_BUFFER_FLAG_REF)) < 0)
                 return ret;
-            }
-            s->frame.pict_type           = AV_PICTURE_TYPE_I;
-            s->frame.palette_has_changed = 1;
-            set_palette((uint32_t *)s->frame.data[1]);
+            s->frame->pict_type           = AV_PICTURE_TYPE_I;
+            s->frame->palette_has_changed = 1;
+            set_palette((uint32_t *)s->frame->data[1]);
             erase_screen(avctx);
         } else if (c == 'l') {
             erase_screen(avctx);
@@ -261,13 +262,13 @@
         case 0:
             erase_line(avctx, s->x, avctx->width - s->x);
             if (s->y < avctx->height - s->font_height)
-                memset(s->frame.data[0] + (s->y + s->font_height)*s->frame.linesize[0],
-                    DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame.linesize[0]);
+                memset(s->frame->data[0] + (s->y + s->font_height)*s->frame->linesize[0],
+                    DEFAULT_BG_COLOR, (avctx->height - s->y - s->font_height)*s->frame->linesize[0]);
             break;
         case 1:
             erase_line(avctx, 0, s->x);
             if (s->y > 0)
-                memset(s->frame.data[0], DEFAULT_BG_COLOR, s->y * s->frame.linesize[0]);
+                memset(s->frame->data[0], DEFAULT_BG_COLOR, s->y * s->frame->linesize[0]);
             break;
         case 2:
             erase_screen(avctx);
@@ -315,7 +316,7 @@
             } else if (m == 49) {
                 s->fg = ansi_to_cga[DEFAULT_BG_COLOR];
             } else {
-                av_log_ask_for_sample(avctx, "unsupported rendition parameter\n");
+                avpriv_request_sample(avctx, "Unsupported rendition parameter");
             }
         }
         break;
@@ -332,7 +333,7 @@
         s->y = av_clip(s->sy, 0, avctx->height - s->font_height);
         break;
     default:
-        av_log_ask_for_sample(avctx, "unsupported escape code\n");
+        avpriv_request_sample(avctx, "Unknown escape code");
         break;
     }
     return 0;
@@ -348,20 +349,17 @@
     const uint8_t *buf_end   = buf+buf_size;
     int ret, i, count;
 
-    ret = avctx->reget_buffer(avctx, &s->frame);
-    if (ret < 0){
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
         return ret;
-    }
     if (!avctx->frame_number) {
         for (i=0; i<avctx->height; i++)
-            memset(s->frame.data[0]+ i*s->frame.linesize[0], 0, avctx->width);
-        memset(s->frame.data[1], 0, AVPALETTE_SIZE);
+            memset(s->frame->data[0]+ i*s->frame->linesize[0], 0, avctx->width);
+        memset(s->frame->data[1], 0, AVPALETTE_SIZE);
     }
 
-    s->frame.pict_type           = AV_PICTURE_TYPE_I;
-    s->frame.palette_has_changed = 1;
-    set_palette((uint32_t *)s->frame.data[1]);
+    s->frame->pict_type           = AV_PICTURE_TYPE_I;
+    s->frame->palette_has_changed = 1;
+    set_palette((uint32_t *)s->frame->data[1]);
     if (!s->first_frame) {
         erase_screen(avctx);
         s->first_frame = 1;
@@ -449,15 +447,16 @@
     }
 
     *got_frame = 1;
-    *(AVFrame*)data = s->frame;
+    if ((ret = av_frame_ref(data, s->frame)) < 0)
+        return ret;
     return buf_size;
 }
 
 static av_cold int decode_close(AVCodecContext *avctx)
 {
     AnsiContext *s = avctx->priv_data;
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+
+    av_frame_free(&s->frame);
     return 0;
 }
 
diff --git a/libavcodec/apedec.c b/libavcodec/apedec.c
index e0af6aa..af5ac6f 100644
--- a/libavcodec/apedec.c
+++ b/libavcodec/apedec.c
@@ -27,6 +27,8 @@
 #include "dsputil.h"
 #include "bytestream.h"
 #include "internal.h"
+#include "get_bits.h"
+#include "unary.h"
 
 /**
  * @file
@@ -123,6 +125,8 @@
     int32_t coeffsA[2][4];  ///< adaption coefficients
     int32_t coeffsB[2][5];  ///< adaption coefficients
     int32_t historybuffer[HISTORY_SIZE + PREDICTOR_SIZE];
+
+    unsigned int sample_pos;
 } APEPredictor;
 
 /** Decoder context */
@@ -154,6 +158,7 @@
     APERice riceX;                           ///< rice code parameters for the second channel
     APERice riceY;                           ///< rice code parameters for the first channel
     APEFilter filters[APE_FILTER_LEVELS][2]; ///< filters used for reconstruction
+    GetBitContext gb;
 
     uint8_t *data;                           ///< current frame data
     uint8_t *data_end;                       ///< frame data end
@@ -161,8 +166,33 @@
     const uint8_t *ptr;                      ///< current position in frame data
 
     int error;
+
+    void (*entropy_decode_mono)(struct APEContext *ctx, int blockstodecode);
+    void (*entropy_decode_stereo)(struct APEContext *ctx, int blockstodecode);
+    void (*predictor_decode_mono)(struct APEContext *ctx, int count);
+    void (*predictor_decode_stereo)(struct APEContext *ctx, int count);
 } APEContext;
 
+static void ape_apply_filters(APEContext *ctx, int32_t *decoded0,
+                              int32_t *decoded1, int count);
+
+static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode);
+static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode);
+static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode);
+static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode);
+static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode);
+static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode);
+static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode);
+static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode);
+static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode);
+
+static void predictor_decode_mono_3800(APEContext *ctx, int count);
+static void predictor_decode_stereo_3800(APEContext *ctx, int count);
+static void predictor_decode_mono_3930(APEContext *ctx, int count);
+static void predictor_decode_stereo_3930(APEContext *ctx, int count);
+static void predictor_decode_mono_3950(APEContext *ctx, int count);
+static void predictor_decode_stereo_3950(APEContext *ctx, int count);
+
 // TODO: dsputilize
 
 static av_cold int ape_decode_close(AVCodecContext *avctx)
@@ -205,8 +235,8 @@
         avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
         break;
     default:
-        av_log_ask_for_sample(avctx, "Unsupported bits per coded sample %d\n",
-                              s->bps);
+        avpriv_request_sample(avctx,
+                              "%d bits per coded sample", s->bps);
         return AVERROR_PATCHWELCOME;
     }
     s->avctx             = avctx;
@@ -217,7 +247,9 @@
 
     av_log(avctx, AV_LOG_DEBUG, "Compression Level: %d - Flags: %d\n",
            s->compression_level, s->flags);
-    if (s->compression_level % 1000 || s->compression_level > COMPRESSION_LEVEL_INSANE || !s->compression_level) {
+    if (s->compression_level % 1000 || s->compression_level > COMPRESSION_LEVEL_INSANE ||
+        !s->compression_level ||
+        (s->fileversion < 3930 && s->compression_level == COMPRESSION_LEVEL_INSANE)) {
         av_log(avctx, AV_LOG_ERROR, "Incorrect compression level %d\n",
                s->compression_level);
         return AVERROR_INVALIDDATA;
@@ -231,6 +263,34 @@
                          filter_alloc_fail);
     }
 
+    if (s->fileversion < 3860) {
+        s->entropy_decode_mono   = entropy_decode_mono_0000;
+        s->entropy_decode_stereo = entropy_decode_stereo_0000;
+    } else if (s->fileversion < 3900) {
+        s->entropy_decode_mono   = entropy_decode_mono_3860;
+        s->entropy_decode_stereo = entropy_decode_stereo_3860;
+    } else if (s->fileversion < 3930) {
+        s->entropy_decode_mono   = entropy_decode_mono_3900;
+        s->entropy_decode_stereo = entropy_decode_stereo_3900;
+    } else if (s->fileversion < 3990) {
+        s->entropy_decode_mono   = entropy_decode_mono_3900;
+        s->entropy_decode_stereo = entropy_decode_stereo_3930;
+    } else {
+        s->entropy_decode_mono   = entropy_decode_mono_3990;
+        s->entropy_decode_stereo = entropy_decode_stereo_3990;
+    }
+
+    if (s->fileversion < 3930) {
+        s->predictor_decode_mono   = predictor_decode_mono_3800;
+        s->predictor_decode_stereo = predictor_decode_stereo_3800;
+    } else if (s->fileversion < 3950) {
+        s->predictor_decode_mono   = predictor_decode_mono_3930;
+        s->predictor_decode_stereo = predictor_decode_stereo_3930;
+    } else {
+        s->predictor_decode_mono   = predictor_decode_mono_3950;
+        s->predictor_decode_stereo = predictor_decode_stereo_3950;
+    }
+
     ff_dsputil_init(&s->dsp, avctx);
     avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
 
@@ -401,67 +461,74 @@
         rice->k++;
 }
 
-static inline int ape_decode_value(APEContext *ctx, APERice *rice)
+static inline int get_rice_ook(GetBitContext *gb, int k)
+{
+    unsigned int x;
+
+    x = get_unary(gb, 1, get_bits_left(gb));
+
+    if (k)
+        x = (x << k) | get_bits(gb, k);
+
+    return x;
+}
+
+static inline int ape_decode_value_3860(APEContext *ctx, GetBitContext *gb,
+                                        APERice *rice)
 {
     unsigned int x, overflow;
 
-    if (ctx->fileversion < 3990) {
-        int tmpk;
+    overflow = get_unary(gb, 1, get_bits_left(gb));
 
-        overflow = range_get_symbol(ctx, counts_3970, counts_diff_3970);
-
-        if (overflow == (MODEL_ELEMENTS - 1)) {
-            tmpk = range_decode_bits(ctx, 5);
-            overflow = 0;
-        } else
-            tmpk = (rice->k < 1) ? 0 : rice->k - 1;
-
-        if (tmpk <= 16)
-            x = range_decode_bits(ctx, tmpk);
-        else if (tmpk <= 32) {
-            x = range_decode_bits(ctx, 16);
-            x |= (range_decode_bits(ctx, tmpk - 16) << 16);
-        } else {
-            av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", tmpk);
-            return AVERROR_INVALIDDATA;
+    if (ctx->fileversion > 3880) {
+        while (overflow >= 16) {
+            overflow -= 16;
+            rice->k  += 4;
         }
-        x += overflow << tmpk;
-    } else {
-        int base, pivot;
-
-        pivot = rice->ksum >> 5;
-        if (pivot == 0)
-            pivot = 1;
-
-        overflow = range_get_symbol(ctx, counts_3980, counts_diff_3980);
-
-        if (overflow == (MODEL_ELEMENTS - 1)) {
-            overflow  = range_decode_bits(ctx, 16) << 16;
-            overflow |= range_decode_bits(ctx, 16);
-        }
-
-        if (pivot < 0x10000) {
-            base = range_decode_culfreq(ctx, pivot);
-            range_decode_update(ctx, 1, base);
-        } else {
-            int base_hi = pivot, base_lo;
-            int bbits = 0;
-
-            while (base_hi & ~0xFFFF) {
-                base_hi >>= 1;
-                bbits++;
-            }
-            base_hi = range_decode_culfreq(ctx, base_hi + 1);
-            range_decode_update(ctx, 1, base_hi);
-            base_lo = range_decode_culfreq(ctx, 1 << bbits);
-            range_decode_update(ctx, 1, base_lo);
-
-            base = (base_hi << bbits) + base_lo;
-        }
-
-        x = base + overflow * pivot;
     }
 
+    if (!rice->k)
+        x = overflow;
+    else
+        x = (overflow << rice->k) + get_bits(gb, rice->k);
+
+    rice->ksum += x - (rice->ksum + 8 >> 4);
+    if (rice->ksum < (rice->k ? 1 << (rice->k + 4) : 0))
+        rice->k--;
+    else if (rice->ksum >= (1 << (rice->k + 5)) && rice->k < 24)
+        rice->k++;
+
+    /* Convert to signed */
+    if (x & 1)
+        return (x >> 1) + 1;
+    else
+        return -(x >> 1);
+}
+
+static inline int ape_decode_value_3900(APEContext *ctx, APERice *rice)
+{
+    unsigned int x, overflow;
+    int tmpk;
+
+    overflow = range_get_symbol(ctx, counts_3970, counts_diff_3970);
+
+    if (overflow == (MODEL_ELEMENTS - 1)) {
+        tmpk = range_decode_bits(ctx, 5);
+        overflow = 0;
+    } else
+        tmpk = (rice->k < 1) ? 0 : rice->k - 1;
+
+    if (tmpk <= 16 || ctx->fileversion < 3910)
+        x = range_decode_bits(ctx, tmpk);
+    else if (tmpk <= 32) {
+        x = range_decode_bits(ctx, 16);
+        x |= (range_decode_bits(ctx, tmpk - 16) << 16);
+    } else {
+        av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", tmpk);
+        return AVERROR_INVALIDDATA;
+    }
+    x += overflow << tmpk;
+
     update_rice(rice, x);
 
     /* Convert to signed */
@@ -471,24 +538,194 @@
         return -(x >> 1);
 }
 
-static void entropy_decode(APEContext *ctx, int blockstodecode, int stereo)
+static inline int ape_decode_value_3990(APEContext *ctx, APERice *rice)
+{
+    unsigned int x, overflow;
+    int base, pivot;
+
+    pivot = rice->ksum >> 5;
+    if (pivot == 0)
+        pivot = 1;
+
+    overflow = range_get_symbol(ctx, counts_3980, counts_diff_3980);
+
+    if (overflow == (MODEL_ELEMENTS - 1)) {
+        overflow  = range_decode_bits(ctx, 16) << 16;
+        overflow |= range_decode_bits(ctx, 16);
+    }
+
+    if (pivot < 0x10000) {
+        base = range_decode_culfreq(ctx, pivot);
+        range_decode_update(ctx, 1, base);
+    } else {
+        int base_hi = pivot, base_lo;
+        int bbits = 0;
+
+        while (base_hi & ~0xFFFF) {
+            base_hi >>= 1;
+            bbits++;
+        }
+        base_hi = range_decode_culfreq(ctx, base_hi + 1);
+        range_decode_update(ctx, 1, base_hi);
+        base_lo = range_decode_culfreq(ctx, 1 << bbits);
+        range_decode_update(ctx, 1, base_lo);
+
+        base = (base_hi << bbits) + base_lo;
+    }
+
+    x = base + overflow * pivot;
+
+    update_rice(rice, x);
+
+    /* Convert to signed */
+    if (x & 1)
+        return (x >> 1) + 1;
+    else
+        return -(x >> 1);
+}
+
+static void decode_array_0000(APEContext *ctx, GetBitContext *gb,
+                              int32_t *out, APERice *rice, int blockstodecode)
+{
+    int i;
+    int ksummax, ksummin;
+
+    rice->ksum = 0;
+    for (i = 0; i < 5; i++) {
+        out[i] = get_rice_ook(&ctx->gb, 10);
+        rice->ksum += out[i];
+    }
+    rice->k = av_log2(rice->ksum / 10) + 1;
+    for (; i < 64; i++) {
+        out[i] = get_rice_ook(&ctx->gb, rice->k);
+        rice->ksum += out[i];
+        rice->k = av_log2(rice->ksum / ((i + 1) * 2)) + 1;
+    }
+    ksummax = 1 << rice->k + 7;
+    ksummin = rice->k ? (1 << rice->k + 6) : 0;
+    for (; i < blockstodecode; i++) {
+        out[i] = get_rice_ook(&ctx->gb, rice->k);
+        rice->ksum += out[i] - out[i - 64];
+        while (rice->ksum < ksummin) {
+            rice->k--;
+            ksummin = rice->k ? ksummin >> 1 : 0;
+            ksummax >>= 1;
+        }
+        while (rice->ksum >= ksummax) {
+            rice->k++;
+            if (rice->k > 24)
+                return;
+            ksummax <<= 1;
+            ksummin = ksummin ? ksummin << 1 : 128;
+        }
+    }
+
+    for (i = 0; i < blockstodecode; i++) {
+        if (out[i] & 1)
+            out[i] = (out[i] >> 1) + 1;
+        else
+            out[i] = -(out[i] >> 1);
+    }
+}
+
+static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode)
+{
+    decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY,
+                      blockstodecode);
+}
+
+static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode)
+{
+    decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY,
+                      blockstodecode);
+    decode_array_0000(ctx, &ctx->gb, ctx->decoded[1], &ctx->riceX,
+                      blockstodecode);
+}
+
+static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode)
+{
+    int32_t *decoded0 = ctx->decoded[0];
+
+    while (blockstodecode--)
+        *decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY);
+}
+
+static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode)
+{
+    int32_t *decoded0 = ctx->decoded[0];
+    int32_t *decoded1 = ctx->decoded[1];
+    int blocks = blockstodecode;
+
+    while (blockstodecode--)
+        *decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY);
+    while (blocks--)
+        *decoded1++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceX);
+}
+
+static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode)
+{
+    int32_t *decoded0 = ctx->decoded[0];
+
+    while (blockstodecode--)
+        *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
+}
+
+static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode)
+{
+    int32_t *decoded0 = ctx->decoded[0];
+    int32_t *decoded1 = ctx->decoded[1];
+    int blocks = blockstodecode;
+
+    while (blockstodecode--)
+        *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
+    range_dec_normalize(ctx);
+    // because of some implementation peculiarities we need to backpedal here
+    ctx->ptr -= 1;
+    range_start_decoding(ctx);
+    while (blocks--)
+        *decoded1++ = ape_decode_value_3900(ctx, &ctx->riceX);
+}
+
+static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode)
 {
     int32_t *decoded0 = ctx->decoded[0];
     int32_t *decoded1 = ctx->decoded[1];
 
     while (blockstodecode--) {
-        *decoded0++ = ape_decode_value(ctx, &ctx->riceY);
-        if (stereo)
-            *decoded1++ = ape_decode_value(ctx, &ctx->riceX);
+        *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY);
+        *decoded1++ = ape_decode_value_3900(ctx, &ctx->riceX);
+    }
+}
+
+static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode)
+{
+    int32_t *decoded0 = ctx->decoded[0];
+
+    while (blockstodecode--)
+        *decoded0++ = ape_decode_value_3990(ctx, &ctx->riceY);
+}
+
+static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode)
+{
+    int32_t *decoded0 = ctx->decoded[0];
+    int32_t *decoded1 = ctx->decoded[1];
+
+    while (blockstodecode--) {
+        *decoded0++ = ape_decode_value_3990(ctx, &ctx->riceY);
+        *decoded1++ = ape_decode_value_3990(ctx, &ctx->riceX);
     }
 }
 
 static int init_entropy_decoder(APEContext *ctx)
 {
     /* Read the CRC */
-    if (ctx->data_end - ctx->ptr < 6)
-        return AVERROR_INVALIDDATA;
-    ctx->CRC = bytestream_get_be32(&ctx->ptr);
+    if (ctx->fileversion >= 3900) {
+        if (ctx->data_end - ctx->ptr < 6)
+            return AVERROR_INVALIDDATA;
+        ctx->CRC = bytestream_get_be32(&ctx->ptr);
+    } else {
+        ctx->CRC = get_bits_long(&ctx->gb, 32);
+    }
 
     /* Read the frame flags if they exist */
     ctx->frameflags = 0;
@@ -506,15 +743,29 @@
     ctx->riceY.k = 10;
     ctx->riceY.ksum = (1 << ctx->riceY.k) * 16;
 
-    /* The first 8 bits of input are ignored. */
-    ctx->ptr++;
+    if (ctx->fileversion >= 3900) {
+        /* The first 8 bits of input are ignored. */
+        ctx->ptr++;
 
-    range_start_decoding(ctx);
+        range_start_decoding(ctx);
+    }
 
     return 0;
 }
 
-static const int32_t initial_coeffs[4] = {
+static const int32_t initial_coeffs_fast_3320[1] = {
+    375,
+};
+
+static const int32_t initial_coeffs_a_3800[3] = {
+    64, 115, 64,
+};
+
+static const int32_t initial_coeffs_b_3800[2] = {
+    740, 0
+};
+
+static const int32_t initial_coeffs_3930[4] = {
     360, 317, -109, 98
 };
 
@@ -527,13 +778,35 @@
     p->buf = p->historybuffer;
 
     /* Initialize and zero the coefficients */
-    memcpy(p->coeffsA[0], initial_coeffs, sizeof(initial_coeffs));
-    memcpy(p->coeffsA[1], initial_coeffs, sizeof(initial_coeffs));
+    if (ctx->fileversion < 3930) {
+        if (ctx->compression_level == COMPRESSION_LEVEL_FAST) {
+            memcpy(p->coeffsA[0], initial_coeffs_fast_3320,
+                   sizeof(initial_coeffs_fast_3320));
+            memcpy(p->coeffsA[1], initial_coeffs_fast_3320,
+                   sizeof(initial_coeffs_fast_3320));
+        } else {
+            memcpy(p->coeffsA[0], initial_coeffs_a_3800,
+                   sizeof(initial_coeffs_a_3800));
+            memcpy(p->coeffsA[1], initial_coeffs_a_3800,
+                   sizeof(initial_coeffs_a_3800));
+        }
+    } else {
+        memcpy(p->coeffsA[0], initial_coeffs_3930, sizeof(initial_coeffs_3930));
+        memcpy(p->coeffsA[1], initial_coeffs_3930, sizeof(initial_coeffs_3930));
+    }
     memset(p->coeffsB, 0, sizeof(p->coeffsB));
+    if (ctx->fileversion < 3930) {
+        memcpy(p->coeffsB[0], initial_coeffs_b_3800,
+               sizeof(initial_coeffs_b_3800));
+        memcpy(p->coeffsB[1], initial_coeffs_b_3800,
+               sizeof(initial_coeffs_b_3800));
+    }
 
     p->filterA[0] = p->filterA[1] = 0;
     p->filterB[0] = p->filterB[1] = 0;
     p->lastA[0]   = p->lastA[1]   = 0;
+
+    p->sample_pos = 0;
 }
 
 /** Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero) */
@@ -541,6 +814,304 @@
     return (x < 0) - (x > 0);
 }
 
+static av_always_inline int filter_fast_3320(APEPredictor *p,
+                                             const int decoded, const int filter,
+                                             const int delayA)
+{
+    int32_t predictionA;
+
+    p->buf[delayA] = p->lastA[filter];
+    if (p->sample_pos < 3) {
+        p->lastA[filter]   = decoded;
+        p->filterA[filter] = decoded;
+        return decoded;
+    }
+
+    predictionA = p->buf[delayA] * 2 - p->buf[delayA - 1];
+    p->lastA[filter] = decoded + (predictionA  * p->coeffsA[filter][0] >> 9);
+
+    if ((decoded ^ predictionA) > 0)
+        p->coeffsA[filter][0]++;
+    else
+        p->coeffsA[filter][0]--;
+
+    p->filterA[filter] += p->lastA[filter];
+
+    return p->filterA[filter];
+}
+
+static av_always_inline int filter_3800(APEPredictor *p,
+                                        const int decoded, const int filter,
+                                        const int delayA,  const int delayB,
+                                        const int start,   const int shift)
+{
+    int32_t predictionA, predictionB, sign;
+    int32_t d0, d1, d2, d3, d4;
+
+    p->buf[delayA] = p->lastA[filter];
+    p->buf[delayB] = p->filterB[filter];
+    if (p->sample_pos < start) {
+        predictionA = decoded + p->filterA[filter];
+        p->lastA[filter]   = decoded;
+        p->filterB[filter] = decoded;
+        p->filterA[filter] = predictionA;
+        return predictionA;
+    }
+    d2 =  p->buf[delayA];
+    d1 = (p->buf[delayA] - p->buf[delayA - 1]) << 1;
+    d0 =  p->buf[delayA] + ((p->buf[delayA - 2] - p->buf[delayA - 1]) << 3);
+    d3 =  p->buf[delayB] * 2 - p->buf[delayB - 1];
+    d4 =  p->buf[delayB];
+
+    predictionA = d0 * p->coeffsA[filter][0] +
+                  d1 * p->coeffsA[filter][1] +
+                  d2 * p->coeffsA[filter][2];
+
+    sign = APESIGN(decoded);
+    p->coeffsA[filter][0] += (((d0 >> 30) & 2) - 1) * sign;
+    p->coeffsA[filter][1] += (((d1 >> 28) & 8) - 4) * sign;
+    p->coeffsA[filter][2] += (((d2 >> 28) & 8) - 4) * sign;
+
+    predictionB = d3 * p->coeffsB[filter][0] -
+                  d4 * p->coeffsB[filter][1];
+    p->lastA[filter] = decoded + (predictionA >> 11);
+    sign = APESIGN(p->lastA[filter]);
+    p->coeffsB[filter][0] += (((d3 >> 29) & 4) - 2) * sign;
+    p->coeffsB[filter][1] -= (((d4 >> 30) & 2) - 1) * sign;
+
+    p->filterB[filter] = p->lastA[filter] + (predictionB >> shift);
+    p->filterA[filter] = p->filterB[filter] + ((p->filterA[filter] * 31) >> 5);
+
+    return p->filterA[filter];
+}
+
+static void long_filter_high_3800(int32_t *buffer, int order, int shift,
+                                  int32_t *coeffs, int32_t *delay, int length)
+{
+    int i, j;
+    int32_t dotprod, sign;
+
+    memset(coeffs, 0, order * sizeof(*coeffs));
+    for (i = 0; i < order; i++)
+        delay[i] = buffer[i];
+    for (i = order; i < length; i++) {
+        dotprod = 0;
+        sign = APESIGN(buffer[i]);
+        for (j = 0; j < order; j++) {
+            dotprod += delay[j] * coeffs[j];
+            coeffs[j] -= (((delay[j] >> 30) & 2) - 1) * sign;
+        }
+        buffer[i] -= dotprod >> shift;
+        for (j = 0; j < order - 1; j++)
+            delay[j] = delay[j + 1];
+        delay[order - 1] = buffer[i];
+    }
+}
+
+static void long_filter_ehigh_3830(int32_t *buffer, int length)
+{
+    int i, j;
+    int32_t dotprod, sign;
+    int32_t coeffs[8], delay[8];
+
+    memset(coeffs, 0, sizeof(coeffs));
+    memset(delay,  0, sizeof(delay));
+    for (i = 0; i < length; i++) {
+        dotprod = 0;
+        sign = APESIGN(buffer[i]);
+        for (j = 7; j >= 0; j--) {
+            dotprod += delay[j] * coeffs[j];
+            coeffs[j] -= (((delay[j] >> 30) & 2) - 1) * sign;
+        }
+        for (j = 7; j > 0; j--)
+            delay[j] = delay[j - 1];
+        delay[0] = buffer[i];
+        buffer[i] -= dotprod >> 9;
+    }
+}
+
+static void predictor_decode_stereo_3800(APEContext *ctx, int count)
+{
+    APEPredictor *p = &ctx->predictor;
+    int32_t *decoded0 = ctx->decoded[0];
+    int32_t *decoded1 = ctx->decoded[1];
+    int32_t coeffs[256], delay[256];
+    int start = 4, shift = 10;
+
+    if (ctx->compression_level == COMPRESSION_LEVEL_HIGH) {
+        start = 16;
+        long_filter_high_3800(decoded0, 16, 9, coeffs, delay, count);
+        long_filter_high_3800(decoded1, 16, 9, coeffs, delay, count);
+    } else if (ctx->compression_level == COMPRESSION_LEVEL_EXTRA_HIGH) {
+        int order = 128, shift2 = 11;
+
+        if (ctx->fileversion >= 3830) {
+            order <<= 1;
+            shift++;
+            shift2++;
+            long_filter_ehigh_3830(decoded0 + order, count - order);
+            long_filter_ehigh_3830(decoded1 + order, count - order);
+        }
+        start = order;
+        long_filter_high_3800(decoded0, order, shift2, coeffs, delay, count);
+        long_filter_high_3800(decoded1, order, shift2, coeffs, delay, count);
+    }
+
+    while (count--) {
+        int X = *decoded0, Y = *decoded1;
+        if (ctx->compression_level == COMPRESSION_LEVEL_FAST) {
+            *decoded0 = filter_fast_3320(p, Y, 0, YDELAYA);
+            decoded0++;
+            *decoded1 = filter_fast_3320(p, X, 1, XDELAYA);
+            decoded1++;
+        } else {
+            *decoded0 = filter_3800(p, Y, 0, YDELAYA, YDELAYB,
+                                    start, shift);
+            decoded0++;
+            *decoded1 = filter_3800(p, X, 1, XDELAYA, XDELAYB,
+                                    start, shift);
+            decoded1++;
+        }
+
+        /* Combined */
+        p->buf++;
+        p->sample_pos++;
+
+        /* Have we filled the history buffer? */
+        if (p->buf == p->historybuffer + HISTORY_SIZE) {
+            memmove(p->historybuffer, p->buf,
+                    PREDICTOR_SIZE * sizeof(*p->historybuffer));
+            p->buf = p->historybuffer;
+        }
+    }
+}
+
+static void predictor_decode_mono_3800(APEContext *ctx, int count)
+{
+    APEPredictor *p = &ctx->predictor;
+    int32_t *decoded0 = ctx->decoded[0];
+    int32_t coeffs[256], delay[256];
+    int start = 4, shift = 10;
+
+    if (ctx->compression_level == COMPRESSION_LEVEL_HIGH) {
+        start = 16;
+        long_filter_high_3800(decoded0, 16, 9, coeffs, delay, count);
+    } else if (ctx->compression_level == COMPRESSION_LEVEL_EXTRA_HIGH) {
+        int order = 128, shift2 = 11;
+
+        if (ctx->fileversion >= 3830) {
+            order <<= 1;
+            shift++;
+            shift2++;
+            long_filter_ehigh_3830(decoded0 + order, count - order);
+        }
+        start = order;
+        long_filter_high_3800(decoded0, order, shift2, coeffs, delay, count);
+    }
+
+    while (count--) {
+        if (ctx->compression_level == COMPRESSION_LEVEL_FAST) {
+            *decoded0 = filter_fast_3320(p, *decoded0, 0, YDELAYA);
+            decoded0++;
+        } else {
+            *decoded0 = filter_3800(p, *decoded0, 0, YDELAYA, YDELAYB,
+                                    start, shift);
+            decoded0++;
+        }
+
+        /* Combined */
+        p->buf++;
+        p->sample_pos++;
+
+        /* Have we filled the history buffer? */
+        if (p->buf == p->historybuffer + HISTORY_SIZE) {
+            memmove(p->historybuffer, p->buf,
+                    PREDICTOR_SIZE * sizeof(*p->historybuffer));
+            p->buf = p->historybuffer;
+        }
+    }
+}
+
+static av_always_inline int predictor_update_3930(APEPredictor *p,
+                                                  const int decoded, const int filter,
+                                                  const int delayA)
+{
+    int32_t predictionA, sign;
+    int32_t d0, d1, d2, d3;
+
+    p->buf[delayA]     = p->lastA[filter];
+    d0 = p->buf[delayA    ];
+    d1 = p->buf[delayA    ] - p->buf[delayA - 1];
+    d2 = p->buf[delayA - 1] - p->buf[delayA - 2];
+    d3 = p->buf[delayA - 2] - p->buf[delayA - 3];
+
+    predictionA = d0 * p->coeffsA[filter][0] +
+                  d1 * p->coeffsA[filter][1] +
+                  d2 * p->coeffsA[filter][2] +
+                  d3 * p->coeffsA[filter][3];
+
+    p->lastA[filter] = decoded + (predictionA >> 9);
+    p->filterA[filter] = p->lastA[filter] + ((p->filterA[filter] * 31) >> 5);
+
+    sign = APESIGN(decoded);
+    p->coeffsA[filter][0] += ((d0 < 0) * 2 - 1) * sign;
+    p->coeffsA[filter][1] += ((d1 < 0) * 2 - 1) * sign;
+    p->coeffsA[filter][2] += ((d2 < 0) * 2 - 1) * sign;
+    p->coeffsA[filter][3] += ((d3 < 0) * 2 - 1) * sign;
+
+    return p->filterA[filter];
+}
+
+static void predictor_decode_stereo_3930(APEContext *ctx, int count)
+{
+    APEPredictor *p = &ctx->predictor;
+    int32_t *decoded0 = ctx->decoded[0];
+    int32_t *decoded1 = ctx->decoded[1];
+
+    ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
+
+    while (count--) {
+        /* Predictor Y */
+        int Y = *decoded1, X = *decoded0;
+        *decoded0 = predictor_update_3930(p, Y, 0, YDELAYA);
+        decoded0++;
+        *decoded1 = predictor_update_3930(p, X, 1, XDELAYA);
+        decoded1++;
+
+        /* Combined */
+        p->buf++;
+
+        /* Have we filled the history buffer? */
+        if (p->buf == p->historybuffer + HISTORY_SIZE) {
+            memmove(p->historybuffer, p->buf,
+                    PREDICTOR_SIZE * sizeof(*p->historybuffer));
+            p->buf = p->historybuffer;
+        }
+    }
+}
+
+static void predictor_decode_mono_3930(APEContext *ctx, int count)
+{
+    APEPredictor *p = &ctx->predictor;
+    int32_t *decoded0 = ctx->decoded[0];
+
+    ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
+
+    while (count--) {
+        *decoded0 = predictor_update_3930(p, *decoded0, 0, YDELAYA);
+        decoded0++;
+
+        p->buf++;
+
+        /* Have we filled the history buffer? */
+        if (p->buf == p->historybuffer + HISTORY_SIZE) {
+            memmove(p->historybuffer, p->buf,
+                    PREDICTOR_SIZE * sizeof(*p->historybuffer));
+            p->buf = p->historybuffer;
+        }
+    }
+}
+
 static av_always_inline int predictor_update_filter(APEPredictor *p,
                                                     const int decoded, const int filter,
                                                     const int delayA,  const int delayB,
@@ -588,12 +1159,14 @@
     return p->filterA[filter];
 }
 
-static void predictor_decode_stereo(APEContext *ctx, int count)
+static void predictor_decode_stereo_3950(APEContext *ctx, int count)
 {
     APEPredictor *p = &ctx->predictor;
     int32_t *decoded0 = ctx->decoded[0];
     int32_t *decoded1 = ctx->decoded[1];
 
+    ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
+
     while (count--) {
         /* Predictor Y */
         *decoded0 = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB,
@@ -615,12 +1188,14 @@
     }
 }
 
-static void predictor_decode_mono(APEContext *ctx, int count)
+static void predictor_decode_mono_3950(APEContext *ctx, int count)
 {
     APEPredictor *p = &ctx->predictor;
     int32_t *decoded0 = ctx->decoded[0];
     int32_t predictionA, currentA, A, sign;
 
+    ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
+
     currentA = p->lastA[0];
 
     while (count--) {
@@ -779,11 +1354,10 @@
         return;
     }
 
-    entropy_decode(ctx, count, 0);
-    ape_apply_filters(ctx, ctx->decoded[0], NULL, count);
+    ctx->entropy_decode_mono(ctx, count);
 
     /* Now apply the predictor decoding */
-    predictor_decode_mono(ctx, count);
+    ctx->predictor_decode_mono(ctx, count);
 
     /* Pseudo-stereo - just copy left channel to right channel */
     if (ctx->channels == 2) {
@@ -803,11 +1377,10 @@
         return;
     }
 
-    entropy_decode(ctx, count, 1);
-    ape_apply_filters(ctx, decoded0, decoded1, count);
+    ctx->entropy_decode_stereo(ctx, count);
 
     /* Now apply the predictor decoding */
-    predictor_decode_stereo(ctx, count);
+    ctx->predictor_decode_stereo(ctx, count);
 
     /* Decorrelate and scale to output depth */
     while (count--) {
@@ -852,26 +1425,36 @@
             av_log(avctx, AV_LOG_WARNING, "packet size is not a multiple of 4. "
                    "extra bytes at the end will be skipped.\n");
         }
-
+        if (s->fileversion < 3950) // previous versions overread two bytes
+            buf_size += 2;
         av_fast_malloc(&s->data, &s->data_size, buf_size);
         if (!s->data)
             return AVERROR(ENOMEM);
         s->dsp.bswap_buf((uint32_t*)s->data, (const uint32_t*)buf, buf_size >> 2);
+        memset(s->data + (buf_size & ~3), 0, buf_size & 3);
         s->ptr = s->data;
         s->data_end = s->data + buf_size;
 
         nblocks = bytestream_get_be32(&s->ptr);
         offset  = bytestream_get_be32(&s->ptr);
-        if (offset > 3) {
-            av_log(avctx, AV_LOG_ERROR, "Incorrect offset passed\n");
-            s->data = NULL;
-            return AVERROR_INVALIDDATA;
+        if (s->fileversion >= 3900) {
+            if (offset > 3) {
+                av_log(avctx, AV_LOG_ERROR, "Incorrect offset passed\n");
+                s->data = NULL;
+                return AVERROR_INVALIDDATA;
+            }
+            if (s->data_end - s->ptr < offset) {
+                av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
+                return AVERROR_INVALIDDATA;
+            }
+            s->ptr += offset;
+        } else {
+            init_get_bits(&s->gb, s->ptr, (s->data_end - s->ptr) * 8);
+            if (s->fileversion > 3800)
+                skip_bits_long(&s->gb, offset * 8);
+            else
+                skip_bits_long(&s->gb, offset);
         }
-        if (s->data_end - s->ptr < offset) {
-            av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
-            return AVERROR_INVALIDDATA;
-        }
-        s->ptr += offset;
 
         if (!nblocks || nblocks > INT_MAX) {
             av_log(avctx, AV_LOG_ERROR, "Invalid sample count: %u.\n", nblocks);
@@ -892,6 +1475,10 @@
     }
 
     blockstodecode = FFMIN(s->blocks_per_loop, s->samples);
+    // for old files coefficients were not interleaved,
+    // so we need to decode all of them at once
+    if (s->fileversion < 3930)
+        blockstodecode = s->samples;
 
     /* reallocate decoded sample buffer if needed */
     av_fast_malloc(&s->decoded_buffer, &s->decoded_size,
@@ -904,10 +1491,8 @@
 
     /* get output buffer */
     frame->nb_samples = blockstodecode;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     s->error=0;
 
diff --git a/libavcodec/arm/Makefile b/libavcodec/arm/Makefile
index 1c91d62..5b717b7 100644
--- a/libavcodec/arm/Makefile
+++ b/libavcodec/arm/Makefile
@@ -31,6 +31,11 @@
 OBJS-$(CONFIG_H264PRED)                += arm/h264pred_init_arm.o
 OBJS-$(CONFIG_H264QPEL)                += arm/h264qpel_init_arm.o
 
+OBJS-$(CONFIG_HPELDSP)                 += arm/hpeldsp_init_arm.o        \
+                                          arm/hpeldsp_arm.o
+ARMV6-OBJS-$(CONFIG_HPELDSP)           += arm/hpeldsp_init_armv6.o      \
+                                          arm/hpeldsp_armv6.o
+
 OBJS-$(CONFIG_RV30_DECODER)            += arm/rv34dsp_init_arm.o
 OBJS-$(CONFIG_RV40_DECODER)            += arm/rv34dsp_init_arm.o        \
                                           arm/rv40dsp_init_arm.o        \
@@ -84,6 +89,9 @@
 NEON-OBJS-$(CONFIG_DCA_DECODER)        += arm/dcadsp_neon.o             \
                                           arm/synth_filter_neon.o       \
 
+NEON-OBJS-$(CONFIG_HPELDSP)            += arm/hpeldsp_init_neon.o       \
+                                          arm/hpeldsp_neon.o
+
 NEON-OBJS-$(CONFIG_MPEGVIDEO)          += arm/mpegvideo_neon.o
 NEON-OBJS-$(CONFIG_RV30_DECODER)       += arm/rv34dsp_neon.o
 NEON-OBJS-$(CONFIG_RV40_DECODER)       += arm/rv34dsp_neon.o            \
diff --git a/libavcodec/arm/dca.h b/libavcodec/arm/dca.h
index 2cfd18a..431b62e 100644
--- a/libavcodec/arm/dca.h
+++ b/libavcodec/arm/dca.h
@@ -34,46 +34,44 @@
 {
     int v0, v1, v2, v3, v4, v5;
 
-    __asm__ ("smmul   %8,  %14, %18           \n"
-             "smmul   %11, %15, %18           \n"
-             "smlabb  %14, %8,  %17, %14      \n"
-             "smlabb  %15, %11, %17, %15      \n"
-             "smmul   %9,  %8,  %18           \n"
-             "smmul   %12, %11, %18           \n"
-             "sub     %14, %14, %16, lsr #1   \n"
-             "sub     %15, %15, %16, lsr #1   \n"
-             "smlabb  %8,  %9,  %17, %8       \n"
-             "smlabb  %11, %12, %17, %11      \n"
-             "smmul   %10, %9,  %18           \n"
-             "smmul   %13, %12, %18           \n"
-             "str     %14, %0                 \n"
-             "str     %15, %4                 \n"
-             "sub     %8,  %8,  %16, lsr #1   \n"
-             "sub     %11, %11, %16, lsr #1   \n"
-             "smlabb  %9,  %10, %17, %9       \n"
-             "smlabb  %12, %13, %17, %12      \n"
-             "smmul   %14, %10, %18           \n"
-             "smmul   %15, %13, %18           \n"
-             "str     %8,  %1                 \n"
-             "str     %11, %5                 \n"
-             "sub     %9,  %9,  %16, lsr #1   \n"
-             "sub     %12, %12, %16, lsr #1   \n"
-             "smlabb  %10, %14, %17, %10      \n"
-             "smlabb  %13, %15, %17, %13      \n"
-             "str     %9,  %2                 \n"
-             "str     %12, %6                 \n"
-             "sub     %10, %10, %16, lsr #1   \n"
-             "sub     %13, %13, %16, lsr #1   \n"
-             "str     %10, %3                 \n"
-             "str     %13, %7                 \n"
-             : "=m"(values[0]), "=m"(values[1]),
-               "=m"(values[2]), "=m"(values[3]),
-               "=m"(values[4]), "=m"(values[5]),
-               "=m"(values[6]), "=m"(values[7]),
-               "=&r"(v0), "=&r"(v1), "=&r"(v2),
+    __asm__ ("smmul   %0,  %6,  %10           \n"
+             "smmul   %3,  %7,  %10           \n"
+             "smlabb  %6,  %0,  %9,  %6       \n"
+             "smlabb  %7,  %3,  %9,  %7       \n"
+             "smmul   %1,  %0,  %10           \n"
+             "smmul   %4,  %3,  %10           \n"
+             "sub     %6,  %6,  %8,  lsr #1   \n"
+             "sub     %7,  %7,  %8,  lsr #1   \n"
+             "smlabb  %0,  %1,  %9,  %0       \n"
+             "smlabb  %3,  %4,  %9,  %3       \n"
+             "smmul   %2,  %1,  %10           \n"
+             "smmul   %5,  %4,  %10           \n"
+             "str     %6,  [%11, #0]          \n"
+             "str     %7,  [%11, #16]         \n"
+             "sub     %0,  %0,  %8,  lsr #1   \n"
+             "sub     %3,  %3,  %8,  lsr #1   \n"
+             "smlabb  %1,  %2,  %9,  %1       \n"
+             "smlabb  %4,  %5,  %9,  %4       \n"
+             "smmul   %6,  %2,  %10           \n"
+             "smmul   %7,  %5,  %10           \n"
+             "str     %0,  [%11, #4]          \n"
+             "str     %3,  [%11, #20]         \n"
+             "sub     %1,  %1,  %8,  lsr #1   \n"
+             "sub     %4,  %4,  %8,  lsr #1   \n"
+             "smlabb  %2,  %6,  %9,  %2       \n"
+             "smlabb  %5,  %7,  %9,  %5       \n"
+             "str     %1,  [%11, #8]          \n"
+             "str     %4,  [%11, #24]         \n"
+             "sub     %2,  %2,  %8,  lsr #1   \n"
+             "sub     %5,  %5,  %8,  lsr #1   \n"
+             "str     %2,  [%11, #12]         \n"
+             "str     %5,  [%11, #28]         \n"
+             : "=&r"(v0), "=&r"(v1), "=&r"(v2),
                "=&r"(v3), "=&r"(v4), "=&r"(v5),
                "+&r"(code1), "+&r"(code2)
-             : "r"(levels - 1), "r"(-levels), "r"(ff_inverse[levels]));
+             : "r"(levels - 1), "r"(-levels),
+               "r"(ff_inverse[levels]), "r"(values)
+             : "memory");
 
     return code1 | code2;
 }
diff --git a/libavcodec/arm/dsputil_arm.S b/libavcodec/arm/dsputil_arm.S
index 994b440..586a833 100644
--- a/libavcodec/arm/dsputil_arm.S
+++ b/libavcodec/arm/dsputil_arm.S
@@ -26,590 +26,6 @@
 #define pld @
 #endif
 
-.macro  ALIGN_QWORD_D shift, Rd0, Rd1, Rd2, Rd3, Rn0, Rn1, Rn2, Rn3, Rn4
-        mov             \Rd0, \Rn0, lsr #(\shift * 8)
-        mov             \Rd1, \Rn1, lsr #(\shift * 8)
-        mov             \Rd2, \Rn2, lsr #(\shift * 8)
-        mov             \Rd3, \Rn3, lsr #(\shift * 8)
-        orr             \Rd0, \Rd0, \Rn1, lsl #(32 - \shift * 8)
-        orr             \Rd1, \Rd1, \Rn2, lsl #(32 - \shift * 8)
-        orr             \Rd2, \Rd2, \Rn3, lsl #(32 - \shift * 8)
-        orr             \Rd3, \Rd3, \Rn4, lsl #(32 - \shift * 8)
-.endm
-.macro  ALIGN_DWORD shift, R0, R1, R2
-        mov             \R0, \R0, lsr #(\shift * 8)
-        orr             \R0, \R0, \R1, lsl #(32 - \shift * 8)
-        mov             \R1, \R1, lsr #(\shift * 8)
-        orr             \R1, \R1, \R2, lsl #(32 - \shift * 8)
-.endm
-.macro  ALIGN_DWORD_D shift, Rdst0, Rdst1, Rsrc0, Rsrc1, Rsrc2
-        mov             \Rdst0, \Rsrc0, lsr #(\shift * 8)
-        mov             \Rdst1, \Rsrc1, lsr #(\shift * 8)
-        orr             \Rdst0, \Rdst0, \Rsrc1, lsl #(32 - (\shift * 8))
-        orr             \Rdst1, \Rdst1, \Rsrc2, lsl #(32 - (\shift * 8))
-.endm
-
-.macro  RND_AVG32 Rd0, Rd1, Rn0, Rn1, Rm0, Rm1, Rmask
-        @ Rd = (Rn | Rm) - (((Rn ^ Rm) & ~0x01010101) >> 1)
-        @ Rmask = 0xFEFEFEFE
-        @ Rn = destroy
-        eor             \Rd0, \Rn0, \Rm0
-        eor             \Rd1, \Rn1, \Rm1
-        orr             \Rn0, \Rn0, \Rm0
-        orr             \Rn1, \Rn1, \Rm1
-        and             \Rd0, \Rd0, \Rmask
-        and             \Rd1, \Rd1, \Rmask
-        sub             \Rd0, \Rn0, \Rd0, lsr #1
-        sub             \Rd1, \Rn1, \Rd1, lsr #1
-.endm
-
-.macro  NO_RND_AVG32 Rd0, Rd1, Rn0, Rn1, Rm0, Rm1, Rmask
-        @ Rd = (Rn & Rm) - (((Rn ^ Rm) & ~0x01010101) >> 1)
-        @ Rmask = 0xFEFEFEFE
-        @ Rn = destroy
-        eor             \Rd0, \Rn0, \Rm0
-        eor             \Rd1, \Rn1, \Rm1
-        and             \Rn0, \Rn0, \Rm0
-        and             \Rn1, \Rn1, \Rm1
-        and             \Rd0, \Rd0, \Rmask
-        and             \Rd1, \Rd1, \Rmask
-        add             \Rd0, \Rn0, \Rd0, lsr #1
-        add             \Rd1, \Rn1, \Rd1, lsr #1
-.endm
-
-.macro  JMP_ALIGN tmp, reg
-        ands            \tmp, \reg, #3
-        bic             \reg, \reg, #3
-        beq             1f
-        subs            \tmp, \tmp, #1
-        beq             2f
-        subs            \tmp, \tmp, #1
-        beq             3f
-        b    4f
-.endm
-
-@ ----------------------------------------------------------------
-        .align 5
-function ff_put_pixels16_arm, export=1
-        @ void func(uint8_t *block, const uint8_t *pixels, int line_size, int h)
-        @ block = word aligned, pixles = unaligned
-        pld             [r1]
-        push            {r4-r11, lr}
-        JMP_ALIGN       r5,  r1
-1:
-        ldm             r1,  {r4-r7}
-        add             r1,  r1,  r2
-        stm             r0,  {r4-r7}
-        pld             [r1]
-        subs            r3,  r3,  #1
-        add             r0,  r0,  r2
-        bne             1b
-        pop             {r4-r11, pc}
-        .align 5
-2:
-        ldm             r1,  {r4-r8}
-        add             r1,  r1,  r2
-        ALIGN_QWORD_D   1,   r9,  r10, r11, r12, r4,  r5,  r6,  r7,  r8
-        pld             [r1]
-        subs            r3,  r3,  #1
-        stm             r0,  {r9-r12}
-        add             r0,  r0,  r2
-        bne             2b
-        pop             {r4-r11, pc}
-        .align 5
-3:
-        ldm             r1,  {r4-r8}
-        add             r1,  r1,  r2
-        ALIGN_QWORD_D   2,   r9,  r10, r11, r12, r4,  r5,  r6,  r7,  r8
-        pld             [r1]
-        subs            r3,  r3,  #1
-        stm             r0,  {r9-r12}
-        add             r0,  r0,  r2
-        bne             3b
-        pop             {r4-r11, pc}
-        .align 5
-4:
-        ldm             r1,  {r4-r8}
-        add             r1,  r1,  r2
-        ALIGN_QWORD_D   3,   r9,  r10, r11, r12, r4,  r5,  r6,  r7,  r8
-        pld             [r1]
-        subs            r3,  r3,  #1
-        stm             r0,  {r9-r12}
-        add             r0,  r0,  r2
-        bne             4b
-        pop             {r4-r11,pc}
-endfunc
-
-@ ----------------------------------------------------------------
-        .align 5
-function ff_put_pixels8_arm, export=1
-        @ void func(uint8_t *block, const uint8_t *pixels, int line_size, int h)
-        @ block = word aligned, pixles = unaligned
-        pld             [r1]
-        push            {r4-r5,lr}
-        JMP_ALIGN       r5,  r1
-1:
-        ldm             r1,  {r4-r5}
-        add             r1,  r1,  r2
-        subs            r3,  r3,  #1
-        pld             [r1]
-        stm             r0,  {r4-r5}
-        add             r0,  r0,  r2
-        bne             1b
-        pop             {r4-r5,pc}
-        .align 5
-2:
-        ldm             r1,  {r4-r5, r12}
-        add             r1,  r1,  r2
-        ALIGN_DWORD     1,   r4,  r5,  r12
-        pld             [r1]
-        subs            r3,  r3,  #1
-        stm             r0,  {r4-r5}
-        add             r0,  r0,  r2
-        bne             2b
-        pop             {r4-r5,pc}
-        .align 5
-3:
-        ldm             r1,  {r4-r5, r12}
-        add             r1,  r1,  r2
-        ALIGN_DWORD     2,   r4,  r5,  r12
-        pld             [r1]
-        subs            r3,  r3,  #1
-        stm             r0,  {r4-r5}
-        add             r0,  r0,  r2
-        bne             3b
-        pop             {r4-r5,pc}
-        .align 5
-4:
-        ldm             r1,  {r4-r5, r12}
-        add             r1,  r1,  r2
-        ALIGN_DWORD     3,   r4,  r5,  r12
-        pld             [r1]
-        subs            r3,  r3,  #1
-        stm             r0,  {r4-r5}
-        add             r0,  r0,  r2
-        bne             4b
-        pop             {r4-r5,pc}
-endfunc
-
-@ ----------------------------------------------------------------
-        .align 5
-function ff_put_pixels8_x2_arm, export=1
-        @ void func(uint8_t *block, const uint8_t *pixels, int line_size, int h)
-        @ block = word aligned, pixles = unaligned
-        pld             [r1]
-        push            {r4-r10,lr}
-        ldr             r12, =0xfefefefe
-        JMP_ALIGN       r5,  r1
-1:
-        ldm             r1,  {r4-r5, r10}
-        add             r1,  r1,  r2
-        ALIGN_DWORD_D   1,   r6,  r7,  r4,  r5,  r10
-        pld             [r1]
-        RND_AVG32       r8,  r9,  r4,  r5,  r6,  r7,  r12
-        subs            r3,  r3,  #1
-        stm             r0,  {r8-r9}
-        add             r0,  r0,  r2
-        bne             1b
-        pop             {r4-r10,pc}
-        .align 5
-2:
-        ldm             r1,  {r4-r5, r10}
-        add             r1,  r1,  r2
-        ALIGN_DWORD_D   1,   r6,  r7,  r4,  r5,  r10
-        ALIGN_DWORD_D   2,   r8,  r9,  r4,  r5,  r10
-        pld             [r1]
-        RND_AVG32       r4,  r5,  r6,  r7,  r8,  r9,  r12
-        subs            r3,  r3,  #1
-        stm             r0,  {r4-r5}
-        add             r0,  r0,  r2
-        bne             2b
-        pop             {r4-r10,pc}
-        .align 5
-3:
-        ldm             r1,  {r4-r5, r10}
-        add             r1,  r1,  r2
-        ALIGN_DWORD_D   2,   r6,  r7,  r4,  r5,  r10
-        ALIGN_DWORD_D   3,   r8,  r9,  r4,  r5,  r10
-        pld             [r1]
-        RND_AVG32       r4,  r5,  r6,  r7,  r8,  r9,  r12
-        subs            r3,  r3,  #1
-        stm             r0,  {r4-r5}
-        add             r0,  r0,  r2
-        bne             3b
-        pop             {r4-r10,pc}
-        .align 5
-4:
-        ldm             r1,  {r4-r5, r10}
-        add             r1,  r1,  r2
-        ALIGN_DWORD_D   3,   r6,  r7,  r4,  r5,  r10
-        pld             [r1]
-        RND_AVG32       r8,  r9,  r6,  r7,  r5,  r10, r12
-        subs            r3,  r3,  #1
-        stm             r0,  {r8-r9}
-        add             r0,  r0,  r2
-        bne             4b
-        pop             {r4-r10,pc}
-endfunc
-
-        .align 5
-function ff_put_no_rnd_pixels8_x2_arm, export=1
-        @ void func(uint8_t *block, const uint8_t *pixels, int line_size, int h)
-        @ block = word aligned, pixles = unaligned
-        pld             [r1]
-        push            {r4-r10,lr}
-        ldr             r12, =0xfefefefe
-        JMP_ALIGN       r5,  r1
-1:
-        ldm             r1,  {r4-r5, r10}
-        add             r1,  r1,  r2
-        ALIGN_DWORD_D   1,   r6,  r7,  r4,  r5,  r10
-        pld             [r1]
-        NO_RND_AVG32    r8,  r9,  r4,  r5,  r6,  r7,  r12
-        subs            r3,  r3,  #1
-        stm             r0,  {r8-r9}
-        add             r0,  r0,  r2
-        bne             1b
-        pop             {r4-r10,pc}
-        .align 5
-2:
-        ldm             r1,  {r4-r5, r10}
-        add             r1,  r1,  r2
-        ALIGN_DWORD_D   1,   r6,  r7,  r4,  r5,  r10
-        ALIGN_DWORD_D   2,   r8,  r9,  r4,  r5,  r10
-        pld             [r1]
-        NO_RND_AVG32    r4,  r5,  r6,  r7,  r8,  r9,  r12
-        subs            r3,  r3,  #1
-        stm             r0,  {r4-r5}
-        add             r0,  r0,  r2
-        bne             2b
-        pop             {r4-r10,pc}
-        .align 5
-3:
-        ldm             r1,  {r4-r5, r10}
-        add             r1,  r1,  r2
-        ALIGN_DWORD_D   2,   r6,  r7,  r4,  r5,  r10
-        ALIGN_DWORD_D   3,   r8,  r9,  r4,  r5,  r10
-        pld             [r1]
-        NO_RND_AVG32    r4,  r5,  r6,  r7,  r8,  r9,  r12
-        subs            r3,  r3,  #1
-        stm             r0,  {r4-r5}
-        add             r0,  r0,  r2
-        bne             3b
-        pop             {r4-r10,pc}
-        .align 5
-4:
-        ldm             r1,  {r4-r5, r10}
-        add             r1,  r1,  r2
-        ALIGN_DWORD_D   3,   r6,  r7,  r4,  r5,  r10
-        pld             [r1]
-        NO_RND_AVG32    r8,  r9,  r6,  r7,  r5,  r10, r12
-        subs            r3,  r3,  #1
-        stm             r0,  {r8-r9}
-        add             r0,  r0,  r2
-        bne             4b
-        pop             {r4-r10,pc}
-endfunc
-
-
-@ ----------------------------------------------------------------
-        .align 5
-function ff_put_pixels8_y2_arm, export=1
-        @ void func(uint8_t *block, const uint8_t *pixels, int line_size, int h)
-        @ block = word aligned, pixles = unaligned
-        pld             [r1]
-        push            {r4-r11,lr}
-        mov             r3,  r3,  lsr #1
-        ldr             r12, =0xfefefefe
-        JMP_ALIGN       r5,  r1
-1:
-        ldm             r1,  {r4-r5}
-        add             r1,  r1,  r2
-6:      ldm             r1,  {r6-r7}
-        add             r1,  r1,  r2
-        pld             [r1]
-        RND_AVG32       r8,  r9,  r4,  r5,  r6,  r7,  r12
-        ldm             r1,  {r4-r5}
-        add             r1,  r1,  r2
-        stm             r0,  {r8-r9}
-        add             r0,  r0,  r2
-        pld             [r1]
-        RND_AVG32       r8,  r9,  r6,  r7,  r4,  r5,  r12
-        subs            r3,  r3,  #1
-        stm             r0,  {r8-r9}
-        add             r0,  r0,  r2
-        bne             6b
-        pop             {r4-r11,pc}
-        .align 5
-2:
-        ldm             r1,  {r4-r6}
-        add             r1,  r1,  r2
-        pld             [r1]
-        ALIGN_DWORD     1,   r4,  r5,  r6
-6:      ldm             r1,  {r7-r9}
-        add             r1,  r1,  r2
-        pld             [r1]
-        ALIGN_DWORD     1,   r7,  r8,  r9
-        RND_AVG32       r10, r11, r4,  r5,  r7,  r8,  r12
-        stm             r0,  {r10-r11}
-        add             r0,  r0,  r2
-        ldm             r1,  {r4-r6}
-        add             r1,  r1,  r2
-        pld             [r1]
-        ALIGN_DWORD     1,   r4,  r5,  r6
-        subs            r3,  r3,  #1
-        RND_AVG32       r10, r11, r7,  r8,  r4,  r5,  r12
-        stm             r0,  {r10-r11}
-        add             r0,  r0,  r2
-        bne             6b
-        pop             {r4-r11,pc}
-        .align 5
-3:
-        ldm             r1,  {r4-r6}
-        add             r1,  r1,  r2
-        pld             [r1]
-        ALIGN_DWORD     2,   r4,  r5,  r6
-6:      ldm             r1,  {r7-r9}
-        add             r1,  r1,  r2
-        pld             [r1]
-        ALIGN_DWORD     2,   r7,  r8,  r9
-        RND_AVG32       r10, r11, r4,  r5,  r7,  r8,  r12
-        stm             r0,  {r10-r11}
-        add             r0,  r0,  r2
-        ldm             r1,  {r4-r6}
-        add             r1,  r1,  r2
-        pld             [r1]
-        ALIGN_DWORD     2,   r4,  r5,  r6
-        subs            r3,  r3,  #1
-        RND_AVG32       r10, r11, r7,  r8,  r4,  r5,  r12
-        stm             r0,  {r10-r11}
-        add             r0,  r0,  r2
-        bne             6b
-        pop             {r4-r11,pc}
-        .align 5
-4:
-        ldm             r1,  {r4-r6}
-        add             r1,  r1,  r2
-        pld             [r1]
-        ALIGN_DWORD     3,   r4,  r5,  r6
-6:      ldm             r1,  {r7-r9}
-        add             r1,  r1,  r2
-        pld             [r1]
-        ALIGN_DWORD     3,   r7,  r8,  r9
-        RND_AVG32       r10, r11, r4,  r5,  r7,  r8,  r12
-        stm             r0,  {r10-r11}
-        add             r0,  r0,  r2
-        ldm             r1,  {r4-r6}
-        add             r1,  r1,  r2
-        pld             [r1]
-        ALIGN_DWORD     3,   r4,  r5,  r6
-        subs            r3,  r3,  #1
-        RND_AVG32       r10, r11, r7,  r8,  r4,  r5,  r12
-        stm             r0,  {r10-r11}
-        add             r0,  r0,  r2
-        bne             6b
-        pop             {r4-r11,pc}
-endfunc
-
-        .align 5
-function ff_put_no_rnd_pixels8_y2_arm, export=1
-        @ void func(uint8_t *block, const uint8_t *pixels, int line_size, int h)
-        @ block = word aligned, pixles = unaligned
-        pld             [r1]
-        push            {r4-r11,lr}
-        mov             r3,  r3,  lsr #1
-        ldr             r12, =0xfefefefe
-        JMP_ALIGN       r5,  r1
-1:
-        ldm             r1,  {r4-r5}
-        add             r1,  r1,  r2
-6:      ldm             r1,  {r6-r7}
-        add             r1,  r1,  r2
-        pld             [r1]
-        NO_RND_AVG32    r8,  r9,  r4,  r5,  r6,  r7,  r12
-        ldm             r1,  {r4-r5}
-        add             r1,  r1,  r2
-        stm             r0,  {r8-r9}
-        add             r0,  r0,  r2
-        pld             [r1]
-        NO_RND_AVG32    r8,  r9,  r6,  r7,  r4,  r5,  r12
-        subs            r3,  r3,  #1
-        stm             r0,  {r8-r9}
-        add             r0,  r0,  r2
-        bne             6b
-        pop             {r4-r11,pc}
-        .align 5
-2:
-        ldm             r1,  {r4-r6}
-        add             r1,  r1,  r2
-        pld             [r1]
-        ALIGN_DWORD     1,   r4,  r5,  r6
-6:      ldm             r1,  {r7-r9}
-        add             r1,  r1,  r2
-        pld             [r1]
-        ALIGN_DWORD     1,   r7,  r8,  r9
-        NO_RND_AVG32    r10, r11, r4,  r5,  r7,  r8,  r12
-        stm             r0,  {r10-r11}
-        add             r0,  r0,  r2
-        ldm             r1,  {r4-r6}
-        add             r1,  r1,  r2
-        pld             [r1]
-        ALIGN_DWORD     1,   r4,  r5,  r6
-        subs            r3,  r3,  #1
-        NO_RND_AVG32    r10, r11, r7,  r8,  r4,  r5,  r12
-        stm             r0,  {r10-r11}
-        add             r0,  r0,  r2
-        bne             6b
-        pop             {r4-r11,pc}
-        .align 5
-3:
-        ldm             r1,  {r4-r6}
-        add             r1,  r1,  r2
-        pld             [r1]
-        ALIGN_DWORD     2,   r4,  r5,  r6
-6:      ldm             r1,  {r7-r9}
-        add             r1,  r1,  r2
-        pld             [r1]
-        ALIGN_DWORD     2,   r7,  r8,  r9
-        NO_RND_AVG32    r10, r11, r4,  r5,  r7,  r8,  r12
-        stm             r0,  {r10-r11}
-        add             r0,  r0,  r2
-        ldm             r1,  {r4-r6}
-        add             r1,  r1,  r2
-        pld             [r1]
-        ALIGN_DWORD     2,   r4,  r5,  r6
-        subs            r3,  r3,  #1
-        NO_RND_AVG32    r10, r11, r7,  r8,  r4,  r5,  r12
-        stm             r0,  {r10-r11}
-        add             r0,  r0,  r2
-        bne             6b
-        pop             {r4-r11,pc}
-        .align 5
-4:
-        ldm             r1,  {r4-r6}
-        add             r1,  r1,  r2
-        pld             [r1]
-        ALIGN_DWORD     3,   r4,  r5,  r6
-6:      ldm             r1,  {r7-r9}
-        add             r1,  r1,  r2
-        pld             [r1]
-        ALIGN_DWORD     3,   r7,  r8,  r9
-        NO_RND_AVG32    r10, r11, r4,  r5,  r7,  r8,  r12
-        stm             r0,  {r10-r11}
-        add             r0,  r0,  r2
-        ldm             r1,  {r4-r6}
-        add             r1,  r1,  r2
-        pld             [r1]
-        ALIGN_DWORD     3,   r4,  r5,  r6
-        subs            r3,  r3,  #1
-        NO_RND_AVG32    r10, r11, r7,  r8,  r4,  r5,  r12
-        stm             r0,  {r10-r11}
-        add             r0,  r0,  r2
-        bne             6b
-        pop             {r4-r11,pc}
-endfunc
-
-        .ltorg
-
-@ ----------------------------------------------------------------
-.macro  RND_XY2_IT align, rnd
-        @ l1=  (a & 0x03030303) + (b & 0x03030303) ?(+ 0x02020202)
-        @ h1= ((a & 0xFCFCFCFCUL) >> 2) + ((b & 0xFCFCFCFCUL) >> 2)
-.if \align == 0
-        ldm             r1,  {r6-r8}
-.elseif \align == 3
-        ldm             r1,  {r5-r7}
-.else
-        ldm             r1,  {r8-r10}
-.endif
-        add             r1,  r1,  r2
-        pld             [r1]
-.if \align == 0
-        ALIGN_DWORD_D   1,   r4,  r5,  r6,  r7,  r8
-.elseif \align == 1
-        ALIGN_DWORD_D   1,   r4,  r5,  r8,  r9,  r10
-        ALIGN_DWORD_D   2,   r6,  r7,  r8,  r9,  r10
-.elseif \align == 2
-        ALIGN_DWORD_D   2,   r4,  r5,  r8,  r9,  r10
-        ALIGN_DWORD_D   3,   r6,  r7,  r8,  r9,  r10
-.elseif \align == 3
-        ALIGN_DWORD_D   3,   r4,  r5,  r5,  r6,  r7
-.endif
-        ldr             r14, =0x03030303
-        tst             r3,  #1
-        and             r8,  r4,  r14
-        and             r9,  r5,  r14
-        and             r10, r6,  r14
-        and             r11, r7,  r14
-        it              eq
-        andeq           r14, r14, r14, \rnd #1
-        add             r8,  r8,  r10
-        add             r9,  r9,  r11
-        ldr             r12, =0xfcfcfcfc >> 2
-        itt             eq
-        addeq           r8,  r8,  r14
-        addeq           r9,  r9,  r14
-        and             r4,  r12, r4,  lsr #2
-        and             r5,  r12, r5,  lsr #2
-        and             r6,  r12, r6,  lsr #2
-        and             r7,  r12, r7,  lsr #2
-        add             r10, r4,  r6
-        add             r11, r5,  r7
-        subs            r3,  r3,  #1
-.endm
-
-.macro RND_XY2_EXPAND align, rnd
-        RND_XY2_IT      \align, \rnd
-6:      push            {r8-r11}
-        RND_XY2_IT      \align, \rnd
-        pop             {r4-r7}
-        add             r4,  r4,  r8
-        add             r5,  r5,  r9
-        ldr             r14, =0x0f0f0f0f
-        add             r6,  r6,  r10
-        add             r7,  r7,  r11
-        and             r4,  r14, r4,  lsr #2
-        and             r5,  r14, r5,  lsr #2
-        add             r4,  r4,  r6
-        add             r5,  r5,  r7
-        stm             r0,  {r4-r5}
-        add             r0,  r0,  r2
-        bge             6b
-        pop             {r4-r11,pc}
-.endm
-
-        .align 5
-function ff_put_pixels8_xy2_arm, export=1
-        @ void func(uint8_t *block, const uint8_t *pixels, int line_size, int h)
-        @ block = word aligned, pixles = unaligned
-        pld             [r1]
-        push            {r4-r11,lr} @ R14 is also called LR
-        JMP_ALIGN       r5,  r1
-1:      RND_XY2_EXPAND  0, lsl
-        .align 5
-2:      RND_XY2_EXPAND  1, lsl
-        .align 5
-3:      RND_XY2_EXPAND  2, lsl
-        .align 5
-4:      RND_XY2_EXPAND  3, lsl
-endfunc
-
-        .align 5
-function ff_put_no_rnd_pixels8_xy2_arm, export=1
-        @ void func(uint8_t *block, const uint8_t *pixels, int line_size, int h)
-        @ block = word aligned, pixles = unaligned
-        pld             [r1]
-        push            {r4-r11,lr}
-        JMP_ALIGN       r5,  r1
-1:      RND_XY2_EXPAND  0, lsr
-        .align 5
-2:      RND_XY2_EXPAND  1, lsr
-        .align 5
-3:      RND_XY2_EXPAND  2, lsr
-        .align 5
-4:      RND_XY2_EXPAND  3, lsr
-endfunc
-
         .align 5
 @ void ff_add_pixels_clamped_arm(int16_t *block, uint8_t *dest, int stride)
 function ff_add_pixels_clamped_arm, export=1
diff --git a/libavcodec/arm/dsputil_armv6.S b/libavcodec/arm/dsputil_armv6.S
index 0c54b52..6ec238b 100644
--- a/libavcodec/arm/dsputil_armv6.S
+++ b/libavcodec/arm/dsputil_armv6.S
@@ -20,244 +20,6 @@
 
 #include "libavutil/arm/asm.S"
 
-.macro  call_2x_pixels  type, subp
-function ff_\type\()_pixels16\subp\()_armv6, export=1
-        push            {r0-r3, lr}
-        bl              ff_\type\()_pixels8\subp\()_armv6
-        pop             {r0-r3, lr}
-        add             r0,  r0,  #8
-        add             r1,  r1,  #8
-        b               ff_\type\()_pixels8\subp\()_armv6
-endfunc
-.endm
-
-call_2x_pixels          avg
-call_2x_pixels          put, _x2
-call_2x_pixels          put, _y2
-call_2x_pixels          put, _x2_no_rnd
-call_2x_pixels          put, _y2_no_rnd
-
-function ff_put_pixels16_armv6, export=1
-        push            {r4-r11}
-1:
-        ldr             r5,  [r1, #4]
-        ldr             r6,  [r1, #8]
-        ldr             r7,  [r1, #12]
-        ldr_post        r4,  r1,  r2
-        strd            r6,  r7,  [r0, #8]
-        ldr             r9,  [r1, #4]
-        strd_post       r4,  r5,  r0,  r2
-        ldr             r10, [r1, #8]
-        ldr             r11, [r1, #12]
-        ldr_post        r8,  r1,  r2
-        strd            r10, r11, [r0, #8]
-        subs            r3,  r3,  #2
-        strd_post       r8,  r9,  r0,  r2
-        bne             1b
-
-        pop             {r4-r11}
-        bx              lr
-endfunc
-
-function ff_put_pixels8_armv6, export=1
-        push            {r4-r7}
-1:
-        ldr             r5,  [r1, #4]
-        ldr_post        r4,  r1,  r2
-        ldr             r7,  [r1, #4]
-        strd_post       r4,  r5,  r0,  r2
-        ldr_post        r6,  r1,  r2
-        subs            r3,  r3,  #2
-        strd_post       r6,  r7,  r0,  r2
-        bne             1b
-
-        pop             {r4-r7}
-        bx              lr
-endfunc
-
-function ff_put_pixels8_x2_armv6, export=1
-        push            {r4-r11, lr}
-        mov             r12, #1
-        orr             r12, r12, r12, lsl #8
-        orr             r12, r12, r12, lsl #16
-1:
-        ldr             r4,  [r1]
-        subs            r3,  r3,  #2
-        ldr             r5,  [r1, #4]
-        ldr             r7,  [r1, #5]
-        lsr             r6,  r4,  #8
-        ldr_pre         r8,  r1,  r2
-        orr             r6,  r6,  r5,  lsl #24
-        ldr             r9,  [r1, #4]
-        ldr             r11, [r1, #5]
-        lsr             r10, r8,  #8
-        add             r1,  r1,  r2
-        orr             r10, r10, r9,  lsl #24
-        eor             r14, r4,  r6
-        uhadd8          r4,  r4,  r6
-        eor             r6,  r5,  r7
-        uhadd8          r5,  r5,  r7
-        and             r14, r14, r12
-        and             r6,  r6,  r12
-        uadd8           r4,  r4,  r14
-        eor             r14, r8,  r10
-        uadd8           r5,  r5,  r6
-        eor             r6,  r9,  r11
-        uhadd8          r8,  r8,  r10
-        and             r14, r14, r12
-        uhadd8          r9,  r9,  r11
-        and             r6,  r6,  r12
-        uadd8           r8,  r8,  r14
-        strd_post       r4,  r5,  r0,  r2
-        uadd8           r9,  r9,  r6
-        strd_post       r8,  r9,  r0,  r2
-        bne             1b
-
-        pop             {r4-r11, pc}
-endfunc
-
-function ff_put_pixels8_y2_armv6, export=1
-        push            {r4-r11}
-        mov             r12, #1
-        orr             r12, r12, r12, lsl #8
-        orr             r12, r12, r12, lsl #16
-        ldr             r4,  [r1]
-        ldr             r5,  [r1, #4]
-        ldr_pre         r6,  r1,  r2
-        ldr             r7,  [r1, #4]
-1:
-        subs            r3,  r3,  #2
-        uhadd8          r8,  r4,  r6
-        eor             r10, r4,  r6
-        uhadd8          r9,  r5,  r7
-        eor             r11, r5,  r7
-        and             r10, r10, r12
-        ldr_pre         r4,  r1,  r2
-        uadd8           r8,  r8,  r10
-        and             r11, r11, r12
-        uadd8           r9,  r9,  r11
-        ldr             r5,  [r1, #4]
-        uhadd8          r10, r4,  r6
-        eor             r6,  r4,  r6
-        uhadd8          r11, r5,  r7
-        and             r6,  r6,  r12
-        eor             r7,  r5,  r7
-        uadd8           r10, r10, r6
-        and             r7,  r7,  r12
-        ldr_pre         r6,  r1,  r2
-        uadd8           r11, r11, r7
-        strd_post       r8,  r9,  r0,  r2
-        ldr             r7,  [r1, #4]
-        strd_post       r10, r11, r0,  r2
-        bne             1b
-
-        pop             {r4-r11}
-        bx              lr
-endfunc
-
-function ff_put_pixels8_x2_no_rnd_armv6, export=1
-        push            {r4-r9, lr}
-1:
-        subs            r3,  r3,  #2
-        ldr             r4,  [r1]
-        ldr             r5,  [r1, #4]
-        ldr             r7,  [r1, #5]
-        ldr_pre         r8,  r1,  r2
-        ldr             r9,  [r1, #4]
-        ldr             r14, [r1, #5]
-        add             r1,  r1,  r2
-        lsr             r6,  r4,  #8
-        orr             r6,  r6,  r5,  lsl #24
-        lsr             r12, r8,  #8
-        orr             r12, r12, r9,  lsl #24
-        uhadd8          r4,  r4,  r6
-        uhadd8          r5,  r5,  r7
-        uhadd8          r8,  r8,  r12
-        uhadd8          r9,  r9,  r14
-        stm             r0,  {r4,r5}
-        add             r0,  r0,  r2
-        stm             r0,  {r8,r9}
-        add             r0,  r0,  r2
-        bne             1b
-
-        pop             {r4-r9, pc}
-endfunc
-
-function ff_put_pixels8_y2_no_rnd_armv6, export=1
-        push            {r4-r9, lr}
-        ldr             r4,  [r1]
-        ldr             r5,  [r1, #4]
-        ldr_pre         r6,  r1,  r2
-        ldr             r7,  [r1, #4]
-1:
-        subs            r3,  r3,  #2
-        uhadd8          r8,  r4,  r6
-        ldr_pre         r4,  r1,  r2
-        uhadd8          r9,  r5,  r7
-        ldr             r5,  [r1, #4]
-        uhadd8          r12, r4,  r6
-        ldr_pre         r6,  r1,  r2
-        uhadd8          r14, r5,  r7
-        ldr             r7,  [r1, #4]
-        stm             r0,  {r8,r9}
-        add             r0,  r0,  r2
-        stm             r0,  {r12,r14}
-        add             r0,  r0,  r2
-        bne             1b
-
-        pop             {r4-r9, pc}
-endfunc
-
-function ff_avg_pixels8_armv6, export=1
-        pld             [r1, r2]
-        push            {r4-r10, lr}
-        mov             lr,  #1
-        orr             lr,  lr,  lr,  lsl #8
-        orr             lr,  lr,  lr,  lsl #16
-        ldrd            r4,  r5,  [r0]
-        ldr             r10, [r1, #4]
-        ldr_post        r9,  r1,  r2
-        subs            r3,  r3,  #2
-1:
-        pld             [r1, r2]
-        eor             r8,  r4,  r9
-        uhadd8          r4,  r4,  r9
-        eor             r12, r5,  r10
-        ldrd_reg        r6,  r7,  r0,  r2
-        uhadd8          r5,  r5,  r10
-        and             r8,  r8,  lr
-        ldr             r10, [r1, #4]
-        and             r12, r12, lr
-        uadd8           r4,  r4,  r8
-        ldr_post        r9,  r1,  r2
-        eor             r8,  r6,  r9
-        uadd8           r5,  r5,  r12
-        pld             [r1, r2,  lsl #1]
-        eor             r12, r7,  r10
-        uhadd8          r6,  r6,  r9
-        strd_post       r4,  r5,  r0,  r2
-        uhadd8          r7,  r7,  r10
-        beq             2f
-        and             r8,  r8,  lr
-        ldrd_reg        r4,  r5,  r0,  r2
-        uadd8           r6,  r6,  r8
-        ldr             r10, [r1, #4]
-        and             r12, r12, lr
-        subs            r3,  r3,  #2
-        uadd8           r7,  r7,  r12
-        ldr_post        r9,  r1,  r2
-        strd_post       r6,  r7,  r0,  r2
-        b               1b
-2:
-        and             r8,  r8,  lr
-        and             r12, r12, lr
-        uadd8           r6,  r6,  r8
-        uadd8           r7,  r7,  r12
-        strd_post       r6,  r7,  r0,  r2
-
-        pop             {r4-r10, pc}
-endfunc
-
 function ff_add_pixels_clamped_armv6, export=1
         push            {r4-r8,lr}
         mov             r3,  #8
diff --git a/libavcodec/arm/dsputil_init_arm.c b/libavcodec/arm/dsputil_init_arm.c
index a6725f3..68991fa 100644
--- a/libavcodec/arm/dsputil_init_arm.c
+++ b/libavcodec/arm/dsputil_init_arm.c
@@ -30,24 +30,6 @@
 static void (*ff_put_pixels_clamped)(const int16_t *block, uint8_t *pixels, int line_size);
 static void (*ff_add_pixels_clamped)(const int16_t *block, uint8_t *pixels, int line_size);
 
-void ff_put_pixels8_arm(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h);
-void ff_put_pixels8_x2_arm(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h);
-void ff_put_pixels8_y2_arm(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h);
-void ff_put_pixels8_xy2_arm(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h);
-
-void ff_put_no_rnd_pixels8_x2_arm(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h);
-void ff_put_no_rnd_pixels8_y2_arm(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h);
-void ff_put_no_rnd_pixels8_xy2_arm(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h);
-
-void ff_put_pixels16_arm(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h);
-
-CALL_2X_PIXELS(ff_put_pixels16_x2_arm,         ff_put_pixels8_x2_arm,        8)
-CALL_2X_PIXELS(ff_put_pixels16_y2_arm,         ff_put_pixels8_y2_arm,        8)
-CALL_2X_PIXELS(ff_put_pixels16_xy2_arm,        ff_put_pixels8_xy2_arm,       8)
-CALL_2X_PIXELS(ff_put_no_rnd_pixels16_x2_arm,  ff_put_no_rnd_pixels8_x2_arm, 8)
-CALL_2X_PIXELS(ff_put_no_rnd_pixels16_y2_arm,  ff_put_no_rnd_pixels8_y2_arm, 8)
-CALL_2X_PIXELS(ff_put_no_rnd_pixels16_xy2_arm, ff_put_no_rnd_pixels8_xy2_arm,8)
-
 void ff_add_pixels_clamped_arm(const int16_t *block, uint8_t *dest,
                                int line_size);
 
@@ -76,7 +58,6 @@
 
 av_cold void ff_dsputil_init_arm(DSPContext *c, AVCodecContext *avctx)
 {
-    const int high_bit_depth = avctx->bits_per_raw_sample > 8;
     int cpu_flags = av_get_cpu_flags();
 
     ff_put_pixels_clamped = c->put_pixels_clamped;
@@ -99,26 +80,6 @@
 
     c->add_pixels_clamped = ff_add_pixels_clamped_arm;
 
-    if (!high_bit_depth) {
-    c->put_pixels_tab[0][0] = ff_put_pixels16_arm;
-    c->put_pixels_tab[0][1] = ff_put_pixels16_x2_arm;
-    c->put_pixels_tab[0][2] = ff_put_pixels16_y2_arm;
-    c->put_pixels_tab[0][3] = ff_put_pixels16_xy2_arm;
-    c->put_pixels_tab[1][0] = ff_put_pixels8_arm;
-    c->put_pixels_tab[1][1] = ff_put_pixels8_x2_arm;
-    c->put_pixels_tab[1][2] = ff_put_pixels8_y2_arm;
-    c->put_pixels_tab[1][3] = ff_put_pixels8_xy2_arm;
-
-    c->put_no_rnd_pixels_tab[0][0] = ff_put_pixels16_arm;
-    c->put_no_rnd_pixels_tab[0][1] = ff_put_no_rnd_pixels16_x2_arm;
-    c->put_no_rnd_pixels_tab[0][2] = ff_put_no_rnd_pixels16_y2_arm;
-    c->put_no_rnd_pixels_tab[0][3] = ff_put_no_rnd_pixels16_xy2_arm;
-    c->put_no_rnd_pixels_tab[1][0] = ff_put_pixels8_arm;
-    c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_arm;
-    c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_arm;
-    c->put_no_rnd_pixels_tab[1][3] = ff_put_no_rnd_pixels8_xy2_arm;
-    }
-
     if (have_armv5te(cpu_flags)) ff_dsputil_init_armv5te(c, avctx);
     if (have_armv6(cpu_flags))   ff_dsputil_init_armv6(c, avctx);
     if (have_neon(cpu_flags))    ff_dsputil_init_neon(c, avctx);
diff --git a/libavcodec/arm/dsputil_init_armv6.c b/libavcodec/arm/dsputil_init_armv6.c
index 3e7aa37..8f38302 100644
--- a/libavcodec/arm/dsputil_init_armv6.c
+++ b/libavcodec/arm/dsputil_init_armv6.c
@@ -27,24 +27,6 @@
 void ff_simple_idct_put_armv6(uint8_t *dest, int line_size, int16_t *data);
 void ff_simple_idct_add_armv6(uint8_t *dest, int line_size, int16_t *data);
 
-void ff_put_pixels16_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_put_pixels16_x2_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_put_pixels16_y2_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
-
-void ff_put_pixels16_x2_no_rnd_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_put_pixels16_y2_no_rnd_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
-
-void ff_avg_pixels16_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
-
-void ff_put_pixels8_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_put_pixels8_x2_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_put_pixels8_y2_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
-
-void ff_put_pixels8_x2_no_rnd_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_put_pixels8_y2_no_rnd_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
-
-void ff_avg_pixels8_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
-
 void ff_add_pixels_clamped_armv6(const int16_t *block,
                                  uint8_t *restrict pixels,
                                  int line_size);
@@ -82,29 +64,6 @@
         c->idct_permutation_type = FF_LIBMPEG2_IDCT_PERM;
     }
 
-    if (!high_bit_depth) {
-    c->put_pixels_tab[0][0] = ff_put_pixels16_armv6;
-    c->put_pixels_tab[0][1] = ff_put_pixels16_x2_armv6;
-    c->put_pixels_tab[0][2] = ff_put_pixels16_y2_armv6;
-/*     c->put_pixels_tab[0][3] = ff_put_pixels16_xy2_armv6; */
-    c->put_pixels_tab[1][0] = ff_put_pixels8_armv6;
-    c->put_pixels_tab[1][1] = ff_put_pixels8_x2_armv6;
-    c->put_pixels_tab[1][2] = ff_put_pixels8_y2_armv6;
-/*     c->put_pixels_tab[1][3] = ff_put_pixels8_xy2_armv6; */
-
-    c->put_no_rnd_pixels_tab[0][0] = ff_put_pixels16_armv6;
-    c->put_no_rnd_pixels_tab[0][1] = ff_put_pixels16_x2_no_rnd_armv6;
-    c->put_no_rnd_pixels_tab[0][2] = ff_put_pixels16_y2_no_rnd_armv6;
-/*     c->put_no_rnd_pixels_tab[0][3] = ff_put_pixels16_xy2_no_rnd_armv6; */
-    c->put_no_rnd_pixels_tab[1][0] = ff_put_pixels8_armv6;
-    c->put_no_rnd_pixels_tab[1][1] = ff_put_pixels8_x2_no_rnd_armv6;
-    c->put_no_rnd_pixels_tab[1][2] = ff_put_pixels8_y2_no_rnd_armv6;
-/*     c->put_no_rnd_pixels_tab[1][3] = ff_put_pixels8_xy2_no_rnd_armv6; */
-
-    c->avg_pixels_tab[0][0] = ff_avg_pixels16_armv6;
-    c->avg_pixels_tab[1][0] = ff_avg_pixels8_armv6;
-    }
-
     if (!high_bit_depth)
         c->get_pixels = ff_get_pixels_armv6;
     c->add_pixels_clamped = ff_add_pixels_clamped_armv6;
diff --git a/libavcodec/arm/dsputil_init_neon.c b/libavcodec/arm/dsputil_init_neon.c
index 5555bc8..6d19af7 100644
--- a/libavcodec/arm/dsputil_init_neon.c
+++ b/libavcodec/arm/dsputil_init_neon.c
@@ -32,33 +32,6 @@
 void ff_clear_block_neon(int16_t *block);
 void ff_clear_blocks_neon(int16_t *blocks);
 
-void ff_put_pixels16_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_put_pixels16_x2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_put_pixels16_y2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_put_pixels16_xy2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_put_pixels8_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_put_pixels8_x2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_put_pixels8_y2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_put_pixels8_xy2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_put_pixels16_x2_no_rnd_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_put_pixels16_y2_no_rnd_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_put_pixels16_xy2_no_rnd_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_put_pixels8_x2_no_rnd_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_put_pixels8_y2_no_rnd_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_put_pixels8_xy2_no_rnd_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-
-void ff_avg_pixels16_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_avg_pixels16_x2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_avg_pixels16_y2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_avg_pixels16_xy2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_avg_pixels8_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_avg_pixels8_x2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_avg_pixels8_y2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_avg_pixels8_xy2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_avg_pixels16_x2_no_rnd_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_avg_pixels16_y2_no_rnd_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-void ff_avg_pixels16_xy2_no_rnd_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
-
 void ff_add_pixels_clamped_neon(const int16_t *, uint8_t *, int);
 void ff_put_pixels_clamped_neon(const int16_t *, uint8_t *, int);
 void ff_put_signed_pixels_clamped_neon(const int16_t *, uint8_t *, int);
@@ -92,38 +65,6 @@
     if (!high_bit_depth) {
         c->clear_block  = ff_clear_block_neon;
         c->clear_blocks = ff_clear_blocks_neon;
-
-        c->put_pixels_tab[0][0] = ff_put_pixels16_neon;
-        c->put_pixels_tab[0][1] = ff_put_pixels16_x2_neon;
-        c->put_pixels_tab[0][2] = ff_put_pixels16_y2_neon;
-        c->put_pixels_tab[0][3] = ff_put_pixels16_xy2_neon;
-        c->put_pixels_tab[1][0] = ff_put_pixels8_neon;
-        c->put_pixels_tab[1][1] = ff_put_pixels8_x2_neon;
-        c->put_pixels_tab[1][2] = ff_put_pixels8_y2_neon;
-        c->put_pixels_tab[1][3] = ff_put_pixels8_xy2_neon;
-
-        c->put_no_rnd_pixels_tab[0][0] = ff_put_pixels16_neon;
-        c->put_no_rnd_pixels_tab[0][1] = ff_put_pixels16_x2_no_rnd_neon;
-        c->put_no_rnd_pixels_tab[0][2] = ff_put_pixels16_y2_no_rnd_neon;
-        c->put_no_rnd_pixels_tab[0][3] = ff_put_pixels16_xy2_no_rnd_neon;
-        c->put_no_rnd_pixels_tab[1][0] = ff_put_pixels8_neon;
-        c->put_no_rnd_pixels_tab[1][1] = ff_put_pixels8_x2_no_rnd_neon;
-        c->put_no_rnd_pixels_tab[1][2] = ff_put_pixels8_y2_no_rnd_neon;
-        c->put_no_rnd_pixels_tab[1][3] = ff_put_pixels8_xy2_no_rnd_neon;
-
-        c->avg_pixels_tab[0][0] = ff_avg_pixels16_neon;
-        c->avg_pixels_tab[0][1] = ff_avg_pixels16_x2_neon;
-        c->avg_pixels_tab[0][2] = ff_avg_pixels16_y2_neon;
-        c->avg_pixels_tab[0][3] = ff_avg_pixels16_xy2_neon;
-        c->avg_pixels_tab[1][0] = ff_avg_pixels8_neon;
-        c->avg_pixels_tab[1][1] = ff_avg_pixels8_x2_neon;
-        c->avg_pixels_tab[1][2] = ff_avg_pixels8_y2_neon;
-        c->avg_pixels_tab[1][3] = ff_avg_pixels8_xy2_neon;
-
-        c->avg_no_rnd_pixels_tab[0] = ff_avg_pixels16_neon;
-        c->avg_no_rnd_pixels_tab[1] = ff_avg_pixels16_x2_no_rnd_neon;
-        c->avg_no_rnd_pixels_tab[2] = ff_avg_pixels16_y2_no_rnd_neon;
-        c->avg_no_rnd_pixels_tab[3] = ff_avg_pixels16_xy2_no_rnd_neon;
     }
 
     c->add_pixels_clamped = ff_add_pixels_clamped_neon;
diff --git a/libavcodec/arm/dsputil_neon.S b/libavcodec/arm/dsputil_neon.S
index 89d3643..307e122 100644
--- a/libavcodec/arm/dsputil_neon.S
+++ b/libavcodec/arm/dsputil_neon.S
@@ -37,394 +37,6 @@
         bx              lr
 endfunc
 
-.macro  pixels16        rnd=1, avg=0
-  .if \avg
-        mov             r12, r0
-  .endif
-1:      vld1.8          {q0},     [r1], r2
-        vld1.8          {q1},     [r1], r2
-        vld1.8          {q2},     [r1], r2
-        pld             [r1, r2, lsl #2]
-        vld1.8          {q3},     [r1], r2
-        pld             [r1]
-        pld             [r1, r2]
-        pld             [r1, r2, lsl #1]
-  .if \avg
-        vld1.8          {q8},     [r12,:128], r2
-        vrhadd.u8       q0,  q0,  q8
-        vld1.8          {q9},     [r12,:128], r2
-        vrhadd.u8       q1,  q1,  q9
-        vld1.8          {q10},    [r12,:128], r2
-        vrhadd.u8       q2,  q2,  q10
-        vld1.8          {q11},    [r12,:128], r2
-        vrhadd.u8       q3,  q3,  q11
-  .endif
-        subs            r3,  r3,  #4
-        vst1.64         {q0},     [r0,:128], r2
-        vst1.64         {q1},     [r0,:128], r2
-        vst1.64         {q2},     [r0,:128], r2
-        vst1.64         {q3},     [r0,:128], r2
-        bne             1b
-        bx              lr
-.endm
-
-.macro  pixels16_x2     rnd=1, avg=0
-1:      vld1.8          {d0-d2},  [r1], r2
-        vld1.8          {d4-d6},  [r1], r2
-        pld             [r1]
-        pld             [r1, r2]
-        subs            r3,  r3,  #2
-        vext.8          q1,  q0,  q1,  #1
-        avg             q0,  q0,  q1
-        vext.8          q3,  q2,  q3,  #1
-        avg             q2,  q2,  q3
-  .if \avg
-        vld1.8          {q1},     [r0,:128], r2
-        vld1.8          {q3},     [r0,:128]
-        vrhadd.u8       q0,  q0,  q1
-        vrhadd.u8       q2,  q2,  q3
-        sub             r0,  r0,  r2
-  .endif
-        vst1.8          {q0},     [r0,:128], r2
-        vst1.8          {q2},     [r0,:128], r2
-        bne             1b
-        bx              lr
-.endm
-
-.macro  pixels16_y2     rnd=1, avg=0
-        sub             r3,  r3,  #2
-        vld1.8          {q0},     [r1], r2
-        vld1.8          {q1},     [r1], r2
-1:      subs            r3,  r3,  #2
-        avg             q2,  q0,  q1
-        vld1.8          {q0},     [r1], r2
-        avg             q3,  q0,  q1
-        vld1.8          {q1},     [r1], r2
-        pld             [r1]
-        pld             [r1, r2]
-  .if \avg
-        vld1.8          {q8},     [r0,:128], r2
-        vld1.8          {q9},     [r0,:128]
-        vrhadd.u8       q2,  q2,  q8
-        vrhadd.u8       q3,  q3,  q9
-        sub             r0,  r0,  r2
-  .endif
-        vst1.8          {q2},     [r0,:128], r2
-        vst1.8          {q3},     [r0,:128], r2
-        bne             1b
-
-        avg             q2,  q0,  q1
-        vld1.8          {q0},     [r1], r2
-        avg             q3,  q0,  q1
-  .if \avg
-        vld1.8          {q8},     [r0,:128], r2
-        vld1.8          {q9},     [r0,:128]
-        vrhadd.u8       q2,  q2,  q8
-        vrhadd.u8       q3,  q3,  q9
-        sub             r0,  r0,  r2
-  .endif
-        vst1.8          {q2},     [r0,:128], r2
-        vst1.8          {q3},     [r0,:128], r2
-
-        bx              lr
-.endm
-
-.macro  pixels16_xy2    rnd=1, avg=0
-        sub             r3,  r3,  #2
-        vld1.8          {d0-d2},  [r1], r2
-        vld1.8          {d4-d6},  [r1], r2
-NRND    vmov.i16        q13, #1
-        pld             [r1]
-        pld             [r1, r2]
-        vext.8          q1,  q0,  q1,  #1
-        vext.8          q3,  q2,  q3,  #1
-        vaddl.u8        q8,  d0,  d2
-        vaddl.u8        q10, d1,  d3
-        vaddl.u8        q9,  d4,  d6
-        vaddl.u8        q11, d5,  d7
-1:      subs            r3,  r3,  #2
-        vld1.8          {d0-d2},  [r1], r2
-        vadd.u16        q12, q8,  q9
-        pld             [r1]
-NRND    vadd.u16        q12, q12, q13
-        vext.8          q15, q0,  q1,  #1
-        vadd.u16        q1 , q10, q11
-        shrn            d28, q12, #2
-NRND    vadd.u16        q1,  q1,  q13
-        shrn            d29, q1,  #2
-  .if \avg
-        vld1.8          {q8},     [r0,:128]
-        vrhadd.u8       q14, q14, q8
-  .endif
-        vaddl.u8        q8,  d0,  d30
-        vld1.8          {d2-d4},  [r1], r2
-        vaddl.u8        q10, d1,  d31
-        vst1.8          {q14},    [r0,:128], r2
-        vadd.u16        q12, q8,  q9
-        pld             [r1, r2]
-NRND    vadd.u16        q12, q12, q13
-        vext.8          q2,  q1,  q2,  #1
-        vadd.u16        q0,  q10, q11
-        shrn            d30, q12, #2
-NRND    vadd.u16        q0,  q0,  q13
-        shrn            d31, q0,  #2
-  .if \avg
-        vld1.8          {q9},     [r0,:128]
-        vrhadd.u8       q15, q15, q9
-  .endif
-        vaddl.u8        q9,  d2,  d4
-        vaddl.u8        q11, d3,  d5
-        vst1.8          {q15},    [r0,:128], r2
-        bgt             1b
-
-        vld1.8          {d0-d2},  [r1], r2
-        vadd.u16        q12, q8,  q9
-NRND    vadd.u16        q12, q12, q13
-        vext.8          q15, q0,  q1,  #1
-        vadd.u16        q1 , q10, q11
-        shrn            d28, q12, #2
-NRND    vadd.u16        q1,  q1,  q13
-        shrn            d29, q1,  #2
-  .if \avg
-        vld1.8          {q8},     [r0,:128]
-        vrhadd.u8       q14, q14, q8
-  .endif
-        vaddl.u8        q8,  d0,  d30
-        vaddl.u8        q10, d1,  d31
-        vst1.8          {q14},    [r0,:128], r2
-        vadd.u16        q12, q8,  q9
-NRND    vadd.u16        q12, q12, q13
-        vadd.u16        q0,  q10, q11
-        shrn            d30, q12, #2
-NRND    vadd.u16        q0,  q0,  q13
-        shrn            d31, q0,  #2
-  .if \avg
-        vld1.8          {q9},     [r0,:128]
-        vrhadd.u8       q15, q15, q9
-  .endif
-        vst1.8          {q15},    [r0,:128], r2
-
-        bx              lr
-.endm
-
-.macro  pixels8         rnd=1, avg=0
-1:      vld1.8          {d0},     [r1], r2
-        vld1.8          {d1},     [r1], r2
-        vld1.8          {d2},     [r1], r2
-        pld             [r1, r2, lsl #2]
-        vld1.8          {d3},     [r1], r2
-        pld             [r1]
-        pld             [r1, r2]
-        pld             [r1, r2, lsl #1]
-  .if \avg
-        vld1.8          {d4},     [r0,:64], r2
-        vrhadd.u8       d0,  d0,  d4
-        vld1.8          {d5},     [r0,:64], r2
-        vrhadd.u8       d1,  d1,  d5
-        vld1.8          {d6},     [r0,:64], r2
-        vrhadd.u8       d2,  d2,  d6
-        vld1.8          {d7},     [r0,:64], r2
-        vrhadd.u8       d3,  d3,  d7
-        sub             r0,  r0,  r2,  lsl #2
-  .endif
-        subs            r3,  r3,  #4
-        vst1.8          {d0},     [r0,:64], r2
-        vst1.8          {d1},     [r0,:64], r2
-        vst1.8          {d2},     [r0,:64], r2
-        vst1.8          {d3},     [r0,:64], r2
-        bne             1b
-        bx              lr
-.endm
-
-.macro  pixels8_x2      rnd=1, avg=0
-1:      vld1.8          {q0},     [r1], r2
-        vext.8          d1,  d0,  d1,  #1
-        vld1.8          {q1},     [r1], r2
-        vext.8          d3,  d2,  d3,  #1
-        pld             [r1]
-        pld             [r1, r2]
-        subs            r3,  r3,  #2
-        vswp            d1,  d2
-        avg             q0,  q0,  q1
-  .if \avg
-        vld1.8          {d4},     [r0,:64], r2
-        vld1.8          {d5},     [r0,:64]
-        vrhadd.u8       q0,  q0,  q2
-        sub             r0,  r0,  r2
-  .endif
-        vst1.8          {d0},     [r0,:64], r2
-        vst1.8          {d1},     [r0,:64], r2
-        bne             1b
-        bx              lr
-.endm
-
-.macro  pixels8_y2      rnd=1, avg=0
-        sub             r3,  r3,  #2
-        vld1.8          {d0},     [r1], r2
-        vld1.8          {d1},     [r1], r2
-1:      subs            r3,  r3,  #2
-        avg             d4,  d0,  d1
-        vld1.8          {d0},     [r1], r2
-        avg             d5,  d0,  d1
-        vld1.8          {d1},     [r1], r2
-        pld             [r1]
-        pld             [r1, r2]
-  .if \avg
-        vld1.8          {d2},     [r0,:64], r2
-        vld1.8          {d3},     [r0,:64]
-        vrhadd.u8       q2,  q2,  q1
-        sub             r0,  r0,  r2
-  .endif
-        vst1.8          {d4},     [r0,:64], r2
-        vst1.8          {d5},     [r0,:64], r2
-        bne             1b
-
-        avg             d4,  d0,  d1
-        vld1.8          {d0},     [r1], r2
-        avg             d5,  d0,  d1
-  .if \avg
-        vld1.8          {d2},     [r0,:64], r2
-        vld1.8          {d3},     [r0,:64]
-        vrhadd.u8       q2,  q2,  q1
-        sub             r0,  r0,  r2
-  .endif
-        vst1.8          {d4},     [r0,:64], r2
-        vst1.8          {d5},     [r0,:64], r2
-
-        bx              lr
-.endm
-
-.macro  pixels8_xy2     rnd=1, avg=0
-        sub             r3,  r3,  #2
-        vld1.8          {q0},     [r1], r2
-        vld1.8          {q1},     [r1], r2
-NRND    vmov.i16        q11, #1
-        pld             [r1]
-        pld             [r1, r2]
-        vext.8          d4,  d0,  d1,  #1
-        vext.8          d6,  d2,  d3,  #1
-        vaddl.u8        q8,  d0,  d4
-        vaddl.u8        q9,  d2,  d6
-1:      subs            r3,  r3,  #2
-        vld1.8          {q0},     [r1], r2
-        pld             [r1]
-        vadd.u16        q10, q8,  q9
-        vext.8          d4,  d0,  d1,  #1
-NRND    vadd.u16        q10, q10, q11
-        vaddl.u8        q8,  d0,  d4
-        shrn            d5,  q10, #2
-        vld1.8          {q1},     [r1], r2
-        vadd.u16        q10, q8,  q9
-        pld             [r1, r2]
-  .if \avg
-        vld1.8          {d7},     [r0,:64]
-        vrhadd.u8       d5,  d5,  d7
-  .endif
-NRND    vadd.u16        q10, q10, q11
-        vst1.8          {d5},     [r0,:64], r2
-        shrn            d7,  q10, #2
-  .if \avg
-        vld1.8          {d5},     [r0,:64]
-        vrhadd.u8       d7,  d7,  d5
-  .endif
-        vext.8          d6,  d2,  d3,  #1
-        vaddl.u8        q9,  d2,  d6
-        vst1.8          {d7},     [r0,:64], r2
-        bgt             1b
-
-        vld1.8          {q0},     [r1], r2
-        vadd.u16        q10, q8,  q9
-        vext.8          d4,  d0,  d1,  #1
-NRND    vadd.u16        q10, q10, q11
-        vaddl.u8        q8,  d0,  d4
-        shrn            d5,  q10, #2
-        vadd.u16        q10, q8,  q9
-  .if \avg
-        vld1.8          {d7},     [r0,:64]
-        vrhadd.u8       d5,  d5,  d7
-  .endif
-NRND    vadd.u16        q10, q10, q11
-        vst1.8          {d5},     [r0,:64], r2
-        shrn            d7,  q10, #2
-  .if \avg
-        vld1.8          {d5},     [r0,:64]
-        vrhadd.u8       d7,  d7,  d5
-  .endif
-        vst1.8          {d7},     [r0,:64], r2
-
-        bx              lr
-.endm
-
-.macro  pixfunc         pfx, name, suf, rnd=1, avg=0
-  .if \rnd
-    .macro avg  rd, rn, rm
-        vrhadd.u8       \rd, \rn, \rm
-    .endm
-    .macro shrn rd, rn, rm
-        vrshrn.u16      \rd, \rn, \rm
-    .endm
-    .macro NRND insn:vararg
-    .endm
-  .else
-    .macro avg  rd, rn, rm
-        vhadd.u8        \rd, \rn, \rm
-    .endm
-    .macro shrn rd, rn, rm
-        vshrn.u16       \rd, \rn, \rm
-    .endm
-    .macro NRND insn:vararg
-        \insn
-    .endm
-  .endif
-function ff_\pfx\name\suf\()_neon, export=1
-        \name           \rnd, \avg
-endfunc
-        .purgem         avg
-        .purgem         shrn
-        .purgem         NRND
-.endm
-
-.macro  pixfunc2        pfx, name, avg=0
-        pixfunc         \pfx, \name,          rnd=1, avg=\avg
-        pixfunc         \pfx, \name, _no_rnd, rnd=0, avg=\avg
-.endm
-
-function ff_put_h264_qpel16_mc00_neon, export=1
-        mov             r3,  #16
-endfunc
-
-        pixfunc         put_, pixels16,     avg=0
-        pixfunc2        put_, pixels16_x2,  avg=0
-        pixfunc2        put_, pixels16_y2,  avg=0
-        pixfunc2        put_, pixels16_xy2, avg=0
-
-function ff_avg_h264_qpel16_mc00_neon, export=1
-        mov             r3,  #16
-endfunc
-
-        pixfunc         avg_, pixels16,     avg=1
-        pixfunc2        avg_, pixels16_x2,  avg=1
-        pixfunc2        avg_, pixels16_y2,  avg=1
-        pixfunc2        avg_, pixels16_xy2, avg=1
-
-function ff_put_h264_qpel8_mc00_neon, export=1
-        mov             r3,  #8
-endfunc
-
-        pixfunc         put_, pixels8,     avg=0
-        pixfunc2        put_, pixels8_x2,  avg=0
-        pixfunc2        put_, pixels8_y2,  avg=0
-        pixfunc2        put_, pixels8_xy2, avg=0
-
-function ff_avg_h264_qpel8_mc00_neon, export=1
-        mov             r3,  #8
-endfunc
-
-        pixfunc         avg_, pixels8,     avg=1
-        pixfunc         avg_, pixels8_x2,  avg=1
-        pixfunc         avg_, pixels8_y2,  avg=1
-        pixfunc         avg_, pixels8_xy2, avg=1
-
 function ff_put_pixels_clamped_neon, export=1
         vld1.16         {d16-d19}, [r0,:128]!
         vqmovun.s16     d0, q8
diff --git a/libavcodec/arm/h264idct_neon.S b/libavcodec/arm/h264idct_neon.S
index fa5b90c..2edeca2 100644
--- a/libavcodec/arm/h264idct_neon.S
+++ b/libavcodec/arm/h264idct_neon.S
@@ -187,8 +187,8 @@
         vshr.s16        q2,  q10, #1
         vadd.i16        q0,  q8,  q12
         vld1.16         {q14-q15},[r1,:128]
-        vst1.16         {q7},     [r1,:128]!
-        vst1.16         {q7},     [r1,:128]!
+        vst1.16         {q3},     [r1,:128]!
+        vst1.16         {q3},     [r1,:128]!
         vsub.i16        q1,  q8,  q12
         vshr.s16        q3,  q14, #1
         vsub.i16        q2,  q2,  q14
@@ -267,16 +267,16 @@
 .endm
 
 function ff_h264_idct8_add_neon, export=1
-        vmov.i16        q7,       #0
+        vmov.i16        q3,       #0
         vld1.16         {q8-q9},  [r1,:128]
-        vst1.16         {q7},     [r1,:128]!
-        vst1.16         {q7},     [r1,:128]!
+        vst1.16         {q3},     [r1,:128]!
+        vst1.16         {q3},     [r1,:128]!
         vld1.16         {q10-q11},[r1,:128]
-        vst1.16         {q7},     [r1,:128]!
-        vst1.16         {q7},     [r1,:128]!
+        vst1.16         {q3},     [r1,:128]!
+        vst1.16         {q3},     [r1,:128]!
         vld1.16         {q12-q13},[r1,:128]
-        vst1.16         {q7},     [r1,:128]!
-        vst1.16         {q7},     [r1,:128]!
+        vst1.16         {q3},     [r1,:128]!
+        vst1.16         {q3},     [r1,:128]!
 
         idct8x8_cols    0
         idct8x8_cols    1
diff --git a/libavcodec/arm/h264qpel_init_arm.c b/libavcodec/arm/h264qpel_init_arm.c
index fd014fd..eaa1324 100644
--- a/libavcodec/arm/h264qpel_init_arm.c
+++ b/libavcodec/arm/h264qpel_init_arm.c
@@ -26,73 +26,73 @@
 #include "libavutil/arm/cpu.h"
 #include "libavcodec/h264qpel.h"
 
-void ff_put_h264_qpel16_mc00_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel16_mc10_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel16_mc20_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel16_mc30_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel16_mc01_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel16_mc11_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel16_mc21_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel16_mc31_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel16_mc02_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel16_mc12_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel16_mc22_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel16_mc32_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel16_mc03_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel16_mc13_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel16_mc23_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel16_mc33_neon(uint8_t *, uint8_t *, int);
+void ff_put_h264_qpel16_mc00_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel16_mc10_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel16_mc20_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel16_mc30_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel16_mc01_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel16_mc11_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel16_mc21_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel16_mc31_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel16_mc02_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel16_mc12_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel16_mc22_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel16_mc32_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel16_mc03_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel16_mc13_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel16_mc23_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel16_mc33_neon(uint8_t *, uint8_t *, ptrdiff_t);
 
-void ff_put_h264_qpel8_mc00_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel8_mc10_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel8_mc20_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel8_mc30_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel8_mc01_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel8_mc11_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel8_mc21_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel8_mc31_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel8_mc02_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel8_mc12_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel8_mc22_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel8_mc32_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel8_mc03_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel8_mc13_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel8_mc23_neon(uint8_t *, uint8_t *, int);
-void ff_put_h264_qpel8_mc33_neon(uint8_t *, uint8_t *, int);
+void ff_put_h264_qpel8_mc00_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel8_mc10_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel8_mc20_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel8_mc30_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel8_mc01_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel8_mc11_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel8_mc21_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel8_mc31_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel8_mc02_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel8_mc12_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel8_mc22_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel8_mc32_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel8_mc03_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel8_mc13_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel8_mc23_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_put_h264_qpel8_mc33_neon(uint8_t *, uint8_t *, ptrdiff_t);
 
-void ff_avg_h264_qpel16_mc00_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel16_mc10_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel16_mc20_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel16_mc30_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel16_mc01_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel16_mc11_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel16_mc21_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel16_mc31_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel16_mc02_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel16_mc12_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel16_mc22_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel16_mc32_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel16_mc03_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel16_mc13_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel16_mc23_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel16_mc33_neon(uint8_t *, uint8_t *, int);
+void ff_avg_h264_qpel16_mc00_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel16_mc10_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel16_mc20_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel16_mc30_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel16_mc01_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel16_mc11_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel16_mc21_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel16_mc31_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel16_mc02_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel16_mc12_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel16_mc22_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel16_mc32_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel16_mc03_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel16_mc13_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel16_mc23_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel16_mc33_neon(uint8_t *, uint8_t *, ptrdiff_t);
 
-void ff_avg_h264_qpel8_mc00_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel8_mc10_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel8_mc20_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel8_mc30_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel8_mc01_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel8_mc11_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel8_mc21_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel8_mc31_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel8_mc02_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel8_mc12_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel8_mc22_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel8_mc32_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel8_mc03_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel8_mc13_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel8_mc23_neon(uint8_t *, uint8_t *, int);
-void ff_avg_h264_qpel8_mc33_neon(uint8_t *, uint8_t *, int);
+void ff_avg_h264_qpel8_mc00_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel8_mc10_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel8_mc20_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel8_mc30_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel8_mc01_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel8_mc11_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel8_mc21_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel8_mc31_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel8_mc02_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel8_mc12_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel8_mc22_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel8_mc32_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel8_mc03_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel8_mc13_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel8_mc23_neon(uint8_t *, uint8_t *, ptrdiff_t);
+void ff_avg_h264_qpel8_mc33_neon(uint8_t *, uint8_t *, ptrdiff_t);
 
 av_cold void ff_h264qpel_init_arm(H264QpelContext *c, int bit_depth)
 {
diff --git a/libavcodec/arm/hpeldsp_arm.S b/libavcodec/arm/hpeldsp_arm.S
new file mode 100644
index 0000000..2f3d311
--- /dev/null
+++ b/libavcodec/arm/hpeldsp_arm.S
@@ -0,0 +1,611 @@
+@
+@ ARMv4 optimized DSP utils
+@ Copyright (c) 2004 AGAWA Koji <i (AT) atty (DOT) jp>
+@
+@ 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 "config.h"
+#include "libavutil/arm/asm.S"
+
+#if !HAVE_ARMV5TE_EXTERNAL
+#define pld @
+#endif
+
+.macro  ALIGN_QWORD_D shift, Rd0, Rd1, Rd2, Rd3, Rn0, Rn1, Rn2, Rn3, Rn4
+        mov             \Rd0, \Rn0, lsr #(\shift * 8)
+        mov             \Rd1, \Rn1, lsr #(\shift * 8)
+        mov             \Rd2, \Rn2, lsr #(\shift * 8)
+        mov             \Rd3, \Rn3, lsr #(\shift * 8)
+        orr             \Rd0, \Rd0, \Rn1, lsl #(32 - \shift * 8)
+        orr             \Rd1, \Rd1, \Rn2, lsl #(32 - \shift * 8)
+        orr             \Rd2, \Rd2, \Rn3, lsl #(32 - \shift * 8)
+        orr             \Rd3, \Rd3, \Rn4, lsl #(32 - \shift * 8)
+.endm
+.macro  ALIGN_DWORD shift, R0, R1, R2
+        mov             \R0, \R0, lsr #(\shift * 8)
+        orr             \R0, \R0, \R1, lsl #(32 - \shift * 8)
+        mov             \R1, \R1, lsr #(\shift * 8)
+        orr             \R1, \R1, \R2, lsl #(32 - \shift * 8)
+.endm
+.macro  ALIGN_DWORD_D shift, Rdst0, Rdst1, Rsrc0, Rsrc1, Rsrc2
+        mov             \Rdst0, \Rsrc0, lsr #(\shift * 8)
+        mov             \Rdst1, \Rsrc1, lsr #(\shift * 8)
+        orr             \Rdst0, \Rdst0, \Rsrc1, lsl #(32 - (\shift * 8))
+        orr             \Rdst1, \Rdst1, \Rsrc2, lsl #(32 - (\shift * 8))
+.endm
+
+.macro  RND_AVG32 Rd0, Rd1, Rn0, Rn1, Rm0, Rm1, Rmask
+        @ Rd = (Rn | Rm) - (((Rn ^ Rm) & ~0x01010101) >> 1)
+        @ Rmask = 0xFEFEFEFE
+        @ Rn = destroy
+        eor             \Rd0, \Rn0, \Rm0
+        eor             \Rd1, \Rn1, \Rm1
+        orr             \Rn0, \Rn0, \Rm0
+        orr             \Rn1, \Rn1, \Rm1
+        and             \Rd0, \Rd0, \Rmask
+        and             \Rd1, \Rd1, \Rmask
+        sub             \Rd0, \Rn0, \Rd0, lsr #1
+        sub             \Rd1, \Rn1, \Rd1, lsr #1
+.endm
+
+.macro  NO_RND_AVG32 Rd0, Rd1, Rn0, Rn1, Rm0, Rm1, Rmask
+        @ Rd = (Rn & Rm) - (((Rn ^ Rm) & ~0x01010101) >> 1)
+        @ Rmask = 0xFEFEFEFE
+        @ Rn = destroy
+        eor             \Rd0, \Rn0, \Rm0
+        eor             \Rd1, \Rn1, \Rm1
+        and             \Rn0, \Rn0, \Rm0
+        and             \Rn1, \Rn1, \Rm1
+        and             \Rd0, \Rd0, \Rmask
+        and             \Rd1, \Rd1, \Rmask
+        add             \Rd0, \Rn0, \Rd0, lsr #1
+        add             \Rd1, \Rn1, \Rd1, lsr #1
+.endm
+
+.macro  JMP_ALIGN tmp, reg
+        ands            \tmp, \reg, #3
+        bic             \reg, \reg, #3
+        beq             1f
+        subs            \tmp, \tmp, #1
+        beq             2f
+        subs            \tmp, \tmp, #1
+        beq             3f
+        b    4f
+.endm
+
+@ ----------------------------------------------------------------
+        .align 5
+function ff_put_pixels16_arm, export=1
+        @ void func(uint8_t *block, const uint8_t *pixels, int line_size, int h)
+        @ block = word aligned, pixles = unaligned
+        pld             [r1]
+        push            {r4-r11, lr}
+        JMP_ALIGN       r5,  r1
+1:
+        ldm             r1,  {r4-r7}
+        add             r1,  r1,  r2
+        stm             r0,  {r4-r7}
+        pld             [r1]
+        subs            r3,  r3,  #1
+        add             r0,  r0,  r2
+        bne             1b
+        pop             {r4-r11, pc}
+        .align 5
+2:
+        ldm             r1,  {r4-r8}
+        add             r1,  r1,  r2
+        ALIGN_QWORD_D   1,   r9,  r10, r11, r12, r4,  r5,  r6,  r7,  r8
+        pld             [r1]
+        subs            r3,  r3,  #1
+        stm             r0,  {r9-r12}
+        add             r0,  r0,  r2
+        bne             2b
+        pop             {r4-r11, pc}
+        .align 5
+3:
+        ldm             r1,  {r4-r8}
+        add             r1,  r1,  r2
+        ALIGN_QWORD_D   2,   r9,  r10, r11, r12, r4,  r5,  r6,  r7,  r8
+        pld             [r1]
+        subs            r3,  r3,  #1
+        stm             r0,  {r9-r12}
+        add             r0,  r0,  r2
+        bne             3b
+        pop             {r4-r11, pc}
+        .align 5
+4:
+        ldm             r1,  {r4-r8}
+        add             r1,  r1,  r2
+        ALIGN_QWORD_D   3,   r9,  r10, r11, r12, r4,  r5,  r6,  r7,  r8
+        pld             [r1]
+        subs            r3,  r3,  #1
+        stm             r0,  {r9-r12}
+        add             r0,  r0,  r2
+        bne             4b
+        pop             {r4-r11,pc}
+endfunc
+
+@ ----------------------------------------------------------------
+        .align 5
+function ff_put_pixels8_arm, export=1
+        @ void func(uint8_t *block, const uint8_t *pixels, int line_size, int h)
+        @ block = word aligned, pixles = unaligned
+        pld             [r1]
+        push            {r4-r5,lr}
+        JMP_ALIGN       r5,  r1
+1:
+        ldm             r1,  {r4-r5}
+        add             r1,  r1,  r2
+        subs            r3,  r3,  #1
+        pld             [r1]
+        stm             r0,  {r4-r5}
+        add             r0,  r0,  r2
+        bne             1b
+        pop             {r4-r5,pc}
+        .align 5
+2:
+        ldm             r1,  {r4-r5, r12}
+        add             r1,  r1,  r2
+        ALIGN_DWORD     1,   r4,  r5,  r12
+        pld             [r1]
+        subs            r3,  r3,  #1
+        stm             r0,  {r4-r5}
+        add             r0,  r0,  r2
+        bne             2b
+        pop             {r4-r5,pc}
+        .align 5
+3:
+        ldm             r1,  {r4-r5, r12}
+        add             r1,  r1,  r2
+        ALIGN_DWORD     2,   r4,  r5,  r12
+        pld             [r1]
+        subs            r3,  r3,  #1
+        stm             r0,  {r4-r5}
+        add             r0,  r0,  r2
+        bne             3b
+        pop             {r4-r5,pc}
+        .align 5
+4:
+        ldm             r1,  {r4-r5, r12}
+        add             r1,  r1,  r2
+        ALIGN_DWORD     3,   r4,  r5,  r12
+        pld             [r1]
+        subs            r3,  r3,  #1
+        stm             r0,  {r4-r5}
+        add             r0,  r0,  r2
+        bne             4b
+        pop             {r4-r5,pc}
+endfunc
+
+@ ----------------------------------------------------------------
+        .align 5
+function ff_put_pixels8_x2_arm, export=1
+        @ void func(uint8_t *block, const uint8_t *pixels, int line_size, int h)
+        @ block = word aligned, pixles = unaligned
+        pld             [r1]
+        push            {r4-r10,lr}
+        ldr             r12, =0xfefefefe
+        JMP_ALIGN       r5,  r1
+1:
+        ldm             r1,  {r4-r5, r10}
+        add             r1,  r1,  r2
+        ALIGN_DWORD_D   1,   r6,  r7,  r4,  r5,  r10
+        pld             [r1]
+        RND_AVG32       r8,  r9,  r4,  r5,  r6,  r7,  r12
+        subs            r3,  r3,  #1
+        stm             r0,  {r8-r9}
+        add             r0,  r0,  r2
+        bne             1b
+        pop             {r4-r10,pc}
+        .align 5
+2:
+        ldm             r1,  {r4-r5, r10}
+        add             r1,  r1,  r2
+        ALIGN_DWORD_D   1,   r6,  r7,  r4,  r5,  r10
+        ALIGN_DWORD_D   2,   r8,  r9,  r4,  r5,  r10
+        pld             [r1]
+        RND_AVG32       r4,  r5,  r6,  r7,  r8,  r9,  r12
+        subs            r3,  r3,  #1
+        stm             r0,  {r4-r5}
+        add             r0,  r0,  r2
+        bne             2b
+        pop             {r4-r10,pc}
+        .align 5
+3:
+        ldm             r1,  {r4-r5, r10}
+        add             r1,  r1,  r2
+        ALIGN_DWORD_D   2,   r6,  r7,  r4,  r5,  r10
+        ALIGN_DWORD_D   3,   r8,  r9,  r4,  r5,  r10
+        pld             [r1]
+        RND_AVG32       r4,  r5,  r6,  r7,  r8,  r9,  r12
+        subs            r3,  r3,  #1
+        stm             r0,  {r4-r5}
+        add             r0,  r0,  r2
+        bne             3b
+        pop             {r4-r10,pc}
+        .align 5
+4:
+        ldm             r1,  {r4-r5, r10}
+        add             r1,  r1,  r2
+        ALIGN_DWORD_D   3,   r6,  r7,  r4,  r5,  r10
+        pld             [r1]
+        RND_AVG32       r8,  r9,  r6,  r7,  r5,  r10, r12
+        subs            r3,  r3,  #1
+        stm             r0,  {r8-r9}
+        add             r0,  r0,  r2
+        bne             4b
+        pop             {r4-r10,pc}
+endfunc
+
+        .align 5
+function ff_put_no_rnd_pixels8_x2_arm, export=1
+        @ void func(uint8_t *block, const uint8_t *pixels, int line_size, int h)
+        @ block = word aligned, pixles = unaligned
+        pld             [r1]
+        push            {r4-r10,lr}
+        ldr             r12, =0xfefefefe
+        JMP_ALIGN       r5,  r1
+1:
+        ldm             r1,  {r4-r5, r10}
+        add             r1,  r1,  r2
+        ALIGN_DWORD_D   1,   r6,  r7,  r4,  r5,  r10
+        pld             [r1]
+        NO_RND_AVG32    r8,  r9,  r4,  r5,  r6,  r7,  r12
+        subs            r3,  r3,  #1
+        stm             r0,  {r8-r9}
+        add             r0,  r0,  r2
+        bne             1b
+        pop             {r4-r10,pc}
+        .align 5
+2:
+        ldm             r1,  {r4-r5, r10}
+        add             r1,  r1,  r2
+        ALIGN_DWORD_D   1,   r6,  r7,  r4,  r5,  r10
+        ALIGN_DWORD_D   2,   r8,  r9,  r4,  r5,  r10
+        pld             [r1]
+        NO_RND_AVG32    r4,  r5,  r6,  r7,  r8,  r9,  r12
+        subs            r3,  r3,  #1
+        stm             r0,  {r4-r5}
+        add             r0,  r0,  r2
+        bne             2b
+        pop             {r4-r10,pc}
+        .align 5
+3:
+        ldm             r1,  {r4-r5, r10}
+        add             r1,  r1,  r2
+        ALIGN_DWORD_D   2,   r6,  r7,  r4,  r5,  r10
+        ALIGN_DWORD_D   3,   r8,  r9,  r4,  r5,  r10
+        pld             [r1]
+        NO_RND_AVG32    r4,  r5,  r6,  r7,  r8,  r9,  r12
+        subs            r3,  r3,  #1
+        stm             r0,  {r4-r5}
+        add             r0,  r0,  r2
+        bne             3b
+        pop             {r4-r10,pc}
+        .align 5
+4:
+        ldm             r1,  {r4-r5, r10}
+        add             r1,  r1,  r2
+        ALIGN_DWORD_D   3,   r6,  r7,  r4,  r5,  r10
+        pld             [r1]
+        NO_RND_AVG32    r8,  r9,  r6,  r7,  r5,  r10, r12
+        subs            r3,  r3,  #1
+        stm             r0,  {r8-r9}
+        add             r0,  r0,  r2
+        bne             4b
+        pop             {r4-r10,pc}
+endfunc
+
+
+@ ----------------------------------------------------------------
+        .align 5
+function ff_put_pixels8_y2_arm, export=1
+        @ void func(uint8_t *block, const uint8_t *pixels, int line_size, int h)
+        @ block = word aligned, pixles = unaligned
+        pld             [r1]
+        push            {r4-r11,lr}
+        mov             r3,  r3,  lsr #1
+        ldr             r12, =0xfefefefe
+        JMP_ALIGN       r5,  r1
+1:
+        ldm             r1,  {r4-r5}
+        add             r1,  r1,  r2
+6:      ldm             r1,  {r6-r7}
+        add             r1,  r1,  r2
+        pld             [r1]
+        RND_AVG32       r8,  r9,  r4,  r5,  r6,  r7,  r12
+        ldm             r1,  {r4-r5}
+        add             r1,  r1,  r2
+        stm             r0,  {r8-r9}
+        add             r0,  r0,  r2
+        pld             [r1]
+        RND_AVG32       r8,  r9,  r6,  r7,  r4,  r5,  r12
+        subs            r3,  r3,  #1
+        stm             r0,  {r8-r9}
+        add             r0,  r0,  r2
+        bne             6b
+        pop             {r4-r11,pc}
+        .align 5
+2:
+        ldm             r1,  {r4-r6}
+        add             r1,  r1,  r2
+        pld             [r1]
+        ALIGN_DWORD     1,   r4,  r5,  r6
+6:      ldm             r1,  {r7-r9}
+        add             r1,  r1,  r2
+        pld             [r1]
+        ALIGN_DWORD     1,   r7,  r8,  r9
+        RND_AVG32       r10, r11, r4,  r5,  r7,  r8,  r12
+        stm             r0,  {r10-r11}
+        add             r0,  r0,  r2
+        ldm             r1,  {r4-r6}
+        add             r1,  r1,  r2
+        pld             [r1]
+        ALIGN_DWORD     1,   r4,  r5,  r6
+        subs            r3,  r3,  #1
+        RND_AVG32       r10, r11, r7,  r8,  r4,  r5,  r12
+        stm             r0,  {r10-r11}
+        add             r0,  r0,  r2
+        bne             6b
+        pop             {r4-r11,pc}
+        .align 5
+3:
+        ldm             r1,  {r4-r6}
+        add             r1,  r1,  r2
+        pld             [r1]
+        ALIGN_DWORD     2,   r4,  r5,  r6
+6:      ldm             r1,  {r7-r9}
+        add             r1,  r1,  r2
+        pld             [r1]
+        ALIGN_DWORD     2,   r7,  r8,  r9
+        RND_AVG32       r10, r11, r4,  r5,  r7,  r8,  r12
+        stm             r0,  {r10-r11}
+        add             r0,  r0,  r2
+        ldm             r1,  {r4-r6}
+        add             r1,  r1,  r2
+        pld             [r1]
+        ALIGN_DWORD     2,   r4,  r5,  r6
+        subs            r3,  r3,  #1
+        RND_AVG32       r10, r11, r7,  r8,  r4,  r5,  r12
+        stm             r0,  {r10-r11}
+        add             r0,  r0,  r2
+        bne             6b
+        pop             {r4-r11,pc}
+        .align 5
+4:
+        ldm             r1,  {r4-r6}
+        add             r1,  r1,  r2
+        pld             [r1]
+        ALIGN_DWORD     3,   r4,  r5,  r6
+6:      ldm             r1,  {r7-r9}
+        add             r1,  r1,  r2
+        pld             [r1]
+        ALIGN_DWORD     3,   r7,  r8,  r9
+        RND_AVG32       r10, r11, r4,  r5,  r7,  r8,  r12
+        stm             r0,  {r10-r11}
+        add             r0,  r0,  r2
+        ldm             r1,  {r4-r6}
+        add             r1,  r1,  r2
+        pld             [r1]
+        ALIGN_DWORD     3,   r4,  r5,  r6
+        subs            r3,  r3,  #1
+        RND_AVG32       r10, r11, r7,  r8,  r4,  r5,  r12
+        stm             r0,  {r10-r11}
+        add             r0,  r0,  r2
+        bne             6b
+        pop             {r4-r11,pc}
+endfunc
+
+        .align 5
+function ff_put_no_rnd_pixels8_y2_arm, export=1
+        @ void func(uint8_t *block, const uint8_t *pixels, int line_size, int h)
+        @ block = word aligned, pixles = unaligned
+        pld             [r1]
+        push            {r4-r11,lr}
+        mov             r3,  r3,  lsr #1
+        ldr             r12, =0xfefefefe
+        JMP_ALIGN       r5,  r1
+1:
+        ldm             r1,  {r4-r5}
+        add             r1,  r1,  r2
+6:      ldm             r1,  {r6-r7}
+        add             r1,  r1,  r2
+        pld             [r1]
+        NO_RND_AVG32    r8,  r9,  r4,  r5,  r6,  r7,  r12
+        ldm             r1,  {r4-r5}
+        add             r1,  r1,  r2
+        stm             r0,  {r8-r9}
+        add             r0,  r0,  r2
+        pld             [r1]
+        NO_RND_AVG32    r8,  r9,  r6,  r7,  r4,  r5,  r12
+        subs            r3,  r3,  #1
+        stm             r0,  {r8-r9}
+        add             r0,  r0,  r2
+        bne             6b
+        pop             {r4-r11,pc}
+        .align 5
+2:
+        ldm             r1,  {r4-r6}
+        add             r1,  r1,  r2
+        pld             [r1]
+        ALIGN_DWORD     1,   r4,  r5,  r6
+6:      ldm             r1,  {r7-r9}
+        add             r1,  r1,  r2
+        pld             [r1]
+        ALIGN_DWORD     1,   r7,  r8,  r9
+        NO_RND_AVG32    r10, r11, r4,  r5,  r7,  r8,  r12
+        stm             r0,  {r10-r11}
+        add             r0,  r0,  r2
+        ldm             r1,  {r4-r6}
+        add             r1,  r1,  r2
+        pld             [r1]
+        ALIGN_DWORD     1,   r4,  r5,  r6
+        subs            r3,  r3,  #1
+        NO_RND_AVG32    r10, r11, r7,  r8,  r4,  r5,  r12
+        stm             r0,  {r10-r11}
+        add             r0,  r0,  r2
+        bne             6b
+        pop             {r4-r11,pc}
+        .align 5
+3:
+        ldm             r1,  {r4-r6}
+        add             r1,  r1,  r2
+        pld             [r1]
+        ALIGN_DWORD     2,   r4,  r5,  r6
+6:      ldm             r1,  {r7-r9}
+        add             r1,  r1,  r2
+        pld             [r1]
+        ALIGN_DWORD     2,   r7,  r8,  r9
+        NO_RND_AVG32    r10, r11, r4,  r5,  r7,  r8,  r12
+        stm             r0,  {r10-r11}
+        add             r0,  r0,  r2
+        ldm             r1,  {r4-r6}
+        add             r1,  r1,  r2
+        pld             [r1]
+        ALIGN_DWORD     2,   r4,  r5,  r6
+        subs            r3,  r3,  #1
+        NO_RND_AVG32    r10, r11, r7,  r8,  r4,  r5,  r12
+        stm             r0,  {r10-r11}
+        add             r0,  r0,  r2
+        bne             6b
+        pop             {r4-r11,pc}
+        .align 5
+4:
+        ldm             r1,  {r4-r6}
+        add             r1,  r1,  r2
+        pld             [r1]
+        ALIGN_DWORD     3,   r4,  r5,  r6
+6:      ldm             r1,  {r7-r9}
+        add             r1,  r1,  r2
+        pld             [r1]
+        ALIGN_DWORD     3,   r7,  r8,  r9
+        NO_RND_AVG32    r10, r11, r4,  r5,  r7,  r8,  r12
+        stm             r0,  {r10-r11}
+        add             r0,  r0,  r2
+        ldm             r1,  {r4-r6}
+        add             r1,  r1,  r2
+        pld             [r1]
+        ALIGN_DWORD     3,   r4,  r5,  r6
+        subs            r3,  r3,  #1
+        NO_RND_AVG32    r10, r11, r7,  r8,  r4,  r5,  r12
+        stm             r0,  {r10-r11}
+        add             r0,  r0,  r2
+        bne             6b
+        pop             {r4-r11,pc}
+endfunc
+
+        .ltorg
+
+@ ----------------------------------------------------------------
+.macro  RND_XY2_IT align, rnd
+        @ l1=  (a & 0x03030303) + (b & 0x03030303) ?(+ 0x02020202)
+        @ h1= ((a & 0xFCFCFCFCUL) >> 2) + ((b & 0xFCFCFCFCUL) >> 2)
+.if \align == 0
+        ldm             r1,  {r6-r8}
+.elseif \align == 3
+        ldm             r1,  {r5-r7}
+.else
+        ldm             r1,  {r8-r10}
+.endif
+        add             r1,  r1,  r2
+        pld             [r1]
+.if \align == 0
+        ALIGN_DWORD_D   1,   r4,  r5,  r6,  r7,  r8
+.elseif \align == 1
+        ALIGN_DWORD_D   1,   r4,  r5,  r8,  r9,  r10
+        ALIGN_DWORD_D   2,   r6,  r7,  r8,  r9,  r10
+.elseif \align == 2
+        ALIGN_DWORD_D   2,   r4,  r5,  r8,  r9,  r10
+        ALIGN_DWORD_D   3,   r6,  r7,  r8,  r9,  r10
+.elseif \align == 3
+        ALIGN_DWORD_D   3,   r4,  r5,  r5,  r6,  r7
+.endif
+        ldr             r14, =0x03030303
+        tst             r3,  #1
+        and             r8,  r4,  r14
+        and             r9,  r5,  r14
+        and             r10, r6,  r14
+        and             r11, r7,  r14
+        it              eq
+        andeq           r14, r14, r14, \rnd #1
+        add             r8,  r8,  r10
+        add             r9,  r9,  r11
+        ldr             r12, =0xfcfcfcfc >> 2
+        itt             eq
+        addeq           r8,  r8,  r14
+        addeq           r9,  r9,  r14
+        and             r4,  r12, r4,  lsr #2
+        and             r5,  r12, r5,  lsr #2
+        and             r6,  r12, r6,  lsr #2
+        and             r7,  r12, r7,  lsr #2
+        add             r10, r4,  r6
+        add             r11, r5,  r7
+        subs            r3,  r3,  #1
+.endm
+
+.macro RND_XY2_EXPAND align, rnd
+        RND_XY2_IT      \align, \rnd
+6:      push            {r8-r11}
+        RND_XY2_IT      \align, \rnd
+        pop             {r4-r7}
+        add             r4,  r4,  r8
+        add             r5,  r5,  r9
+        ldr             r14, =0x0f0f0f0f
+        add             r6,  r6,  r10
+        add             r7,  r7,  r11
+        and             r4,  r14, r4,  lsr #2
+        and             r5,  r14, r5,  lsr #2
+        add             r4,  r4,  r6
+        add             r5,  r5,  r7
+        stm             r0,  {r4-r5}
+        add             r0,  r0,  r2
+        bge             6b
+        pop             {r4-r11,pc}
+.endm
+
+        .align 5
+function ff_put_pixels8_xy2_arm, export=1
+        @ void func(uint8_t *block, const uint8_t *pixels, int line_size, int h)
+        @ block = word aligned, pixles = unaligned
+        pld             [r1]
+        push            {r4-r11,lr} @ R14 is also called LR
+        JMP_ALIGN       r5,  r1
+1:      RND_XY2_EXPAND  0, lsl
+        .align 5
+2:      RND_XY2_EXPAND  1, lsl
+        .align 5
+3:      RND_XY2_EXPAND  2, lsl
+        .align 5
+4:      RND_XY2_EXPAND  3, lsl
+endfunc
+
+        .align 5
+function ff_put_no_rnd_pixels8_xy2_arm, export=1
+        @ void func(uint8_t *block, const uint8_t *pixels, int line_size, int h)
+        @ block = word aligned, pixles = unaligned
+        pld             [r1]
+        push            {r4-r11,lr}
+        JMP_ALIGN       r5,  r1
+1:      RND_XY2_EXPAND  0, lsr
+        .align 5
+2:      RND_XY2_EXPAND  1, lsr
+        .align 5
+3:      RND_XY2_EXPAND  2, lsr
+        .align 5
+4:      RND_XY2_EXPAND  3, lsr
+endfunc
diff --git a/libavcodec/arm/hpeldsp_arm.h b/libavcodec/arm/hpeldsp_arm.h
new file mode 100644
index 0000000..3f18c62
--- /dev/null
+++ b/libavcodec/arm/hpeldsp_arm.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2009 Mans Rullgard <mans@mansr.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
+ */
+
+#ifndef AVCODEC_ARM_HPELDSP_H
+#define AVCODEC_ARM_HPELDSP_H
+
+#include "libavcodec/hpeldsp.h"
+
+void ff_hpeldsp_init_armv6(HpelDSPContext *c, int flags);
+void ff_hpeldsp_init_neon(HpelDSPContext *c, int flags);
+
+#endif /* AVCODEC_ARM_HPELDSP_H */
diff --git a/libavcodec/arm/hpeldsp_armv6.S b/libavcodec/arm/hpeldsp_armv6.S
new file mode 100644
index 0000000..cd50150
--- /dev/null
+++ b/libavcodec/arm/hpeldsp_armv6.S
@@ -0,0 +1,259 @@
+/*
+ * Copyright (c) 2009 Mans Rullgard <mans@mansr.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 "libavutil/arm/asm.S"
+
+.macro  call_2x_pixels  type, subp
+function ff_\type\()_pixels16\subp\()_armv6, export=1
+        push            {r0-r3, lr}
+        bl              ff_\type\()_pixels8\subp\()_armv6
+        pop             {r0-r3, lr}
+        add             r0,  r0,  #8
+        add             r1,  r1,  #8
+        b               ff_\type\()_pixels8\subp\()_armv6
+endfunc
+.endm
+
+call_2x_pixels          avg
+call_2x_pixels          put, _x2
+call_2x_pixels          put, _y2
+call_2x_pixels          put, _x2_no_rnd
+call_2x_pixels          put, _y2_no_rnd
+
+function ff_put_pixels16_armv6, export=1
+        push            {r4-r11}
+1:
+        ldr             r5,  [r1, #4]
+        ldr             r6,  [r1, #8]
+        ldr             r7,  [r1, #12]
+        ldr_post        r4,  r1,  r2
+        strd            r6,  r7,  [r0, #8]
+        ldr             r9,  [r1, #4]
+        strd_post       r4,  r5,  r0,  r2
+        ldr             r10, [r1, #8]
+        ldr             r11, [r1, #12]
+        ldr_post        r8,  r1,  r2
+        strd            r10, r11, [r0, #8]
+        subs            r3,  r3,  #2
+        strd_post       r8,  r9,  r0,  r2
+        bne             1b
+
+        pop             {r4-r11}
+        bx              lr
+endfunc
+
+function ff_put_pixels8_armv6, export=1
+        push            {r4-r7}
+1:
+        ldr             r5,  [r1, #4]
+        ldr_post        r4,  r1,  r2
+        ldr             r7,  [r1, #4]
+        strd_post       r4,  r5,  r0,  r2
+        ldr_post        r6,  r1,  r2
+        subs            r3,  r3,  #2
+        strd_post       r6,  r7,  r0,  r2
+        bne             1b
+
+        pop             {r4-r7}
+        bx              lr
+endfunc
+
+function ff_put_pixels8_x2_armv6, export=1
+        push            {r4-r11, lr}
+        mov             r12, #1
+        orr             r12, r12, r12, lsl #8
+        orr             r12, r12, r12, lsl #16
+1:
+        ldr             r4,  [r1]
+        subs            r3,  r3,  #2
+        ldr             r5,  [r1, #4]
+        ldr             r7,  [r1, #5]
+        lsr             r6,  r4,  #8
+        ldr_pre         r8,  r1,  r2
+        orr             r6,  r6,  r5,  lsl #24
+        ldr             r9,  [r1, #4]
+        ldr             r11, [r1, #5]
+        lsr             r10, r8,  #8
+        add             r1,  r1,  r2
+        orr             r10, r10, r9,  lsl #24
+        eor             r14, r4,  r6
+        uhadd8          r4,  r4,  r6
+        eor             r6,  r5,  r7
+        uhadd8          r5,  r5,  r7
+        and             r14, r14, r12
+        and             r6,  r6,  r12
+        uadd8           r4,  r4,  r14
+        eor             r14, r8,  r10
+        uadd8           r5,  r5,  r6
+        eor             r6,  r9,  r11
+        uhadd8          r8,  r8,  r10
+        and             r14, r14, r12
+        uhadd8          r9,  r9,  r11
+        and             r6,  r6,  r12
+        uadd8           r8,  r8,  r14
+        strd_post       r4,  r5,  r0,  r2
+        uadd8           r9,  r9,  r6
+        strd_post       r8,  r9,  r0,  r2
+        bne             1b
+
+        pop             {r4-r11, pc}
+endfunc
+
+function ff_put_pixels8_y2_armv6, export=1
+        push            {r4-r11}
+        mov             r12, #1
+        orr             r12, r12, r12, lsl #8
+        orr             r12, r12, r12, lsl #16
+        ldr             r4,  [r1]
+        ldr             r5,  [r1, #4]
+        ldr_pre         r6,  r1,  r2
+        ldr             r7,  [r1, #4]
+1:
+        subs            r3,  r3,  #2
+        uhadd8          r8,  r4,  r6
+        eor             r10, r4,  r6
+        uhadd8          r9,  r5,  r7
+        eor             r11, r5,  r7
+        and             r10, r10, r12
+        ldr_pre         r4,  r1,  r2
+        uadd8           r8,  r8,  r10
+        and             r11, r11, r12
+        uadd8           r9,  r9,  r11
+        ldr             r5,  [r1, #4]
+        uhadd8          r10, r4,  r6
+        eor             r6,  r4,  r6
+        uhadd8          r11, r5,  r7
+        and             r6,  r6,  r12
+        eor             r7,  r5,  r7
+        uadd8           r10, r10, r6
+        and             r7,  r7,  r12
+        ldr_pre         r6,  r1,  r2
+        uadd8           r11, r11, r7
+        strd_post       r8,  r9,  r0,  r2
+        ldr             r7,  [r1, #4]
+        strd_post       r10, r11, r0,  r2
+        bne             1b
+
+        pop             {r4-r11}
+        bx              lr
+endfunc
+
+function ff_put_pixels8_x2_no_rnd_armv6, export=1
+        push            {r4-r9, lr}
+1:
+        subs            r3,  r3,  #2
+        ldr             r4,  [r1]
+        ldr             r5,  [r1, #4]
+        ldr             r7,  [r1, #5]
+        ldr_pre         r8,  r1,  r2
+        ldr             r9,  [r1, #4]
+        ldr             r14, [r1, #5]
+        add             r1,  r1,  r2
+        lsr             r6,  r4,  #8
+        orr             r6,  r6,  r5,  lsl #24
+        lsr             r12, r8,  #8
+        orr             r12, r12, r9,  lsl #24
+        uhadd8          r4,  r4,  r6
+        uhadd8          r5,  r5,  r7
+        uhadd8          r8,  r8,  r12
+        uhadd8          r9,  r9,  r14
+        stm             r0,  {r4,r5}
+        add             r0,  r0,  r2
+        stm             r0,  {r8,r9}
+        add             r0,  r0,  r2
+        bne             1b
+
+        pop             {r4-r9, pc}
+endfunc
+
+function ff_put_pixels8_y2_no_rnd_armv6, export=1
+        push            {r4-r9, lr}
+        ldr             r4,  [r1]
+        ldr             r5,  [r1, #4]
+        ldr_pre         r6,  r1,  r2
+        ldr             r7,  [r1, #4]
+1:
+        subs            r3,  r3,  #2
+        uhadd8          r8,  r4,  r6
+        ldr_pre         r4,  r1,  r2
+        uhadd8          r9,  r5,  r7
+        ldr             r5,  [r1, #4]
+        uhadd8          r12, r4,  r6
+        ldr_pre         r6,  r1,  r2
+        uhadd8          r14, r5,  r7
+        ldr             r7,  [r1, #4]
+        stm             r0,  {r8,r9}
+        add             r0,  r0,  r2
+        stm             r0,  {r12,r14}
+        add             r0,  r0,  r2
+        bne             1b
+
+        pop             {r4-r9, pc}
+endfunc
+
+function ff_avg_pixels8_armv6, export=1
+        pld             [r1, r2]
+        push            {r4-r10, lr}
+        mov             lr,  #1
+        orr             lr,  lr,  lr,  lsl #8
+        orr             lr,  lr,  lr,  lsl #16
+        ldrd            r4,  r5,  [r0]
+        ldr             r10, [r1, #4]
+        ldr_post        r9,  r1,  r2
+        subs            r3,  r3,  #2
+1:
+        pld             [r1, r2]
+        eor             r8,  r4,  r9
+        uhadd8          r4,  r4,  r9
+        eor             r12, r5,  r10
+        ldrd_reg        r6,  r7,  r0,  r2
+        uhadd8          r5,  r5,  r10
+        and             r8,  r8,  lr
+        ldr             r10, [r1, #4]
+        and             r12, r12, lr
+        uadd8           r4,  r4,  r8
+        ldr_post        r9,  r1,  r2
+        eor             r8,  r6,  r9
+        uadd8           r5,  r5,  r12
+        pld             [r1, r2,  lsl #1]
+        eor             r12, r7,  r10
+        uhadd8          r6,  r6,  r9
+        strd_post       r4,  r5,  r0,  r2
+        uhadd8          r7,  r7,  r10
+        beq             2f
+        and             r8,  r8,  lr
+        ldrd_reg        r4,  r5,  r0,  r2
+        uadd8           r6,  r6,  r8
+        ldr             r10, [r1, #4]
+        and             r12, r12, lr
+        subs            r3,  r3,  #2
+        uadd8           r7,  r7,  r12
+        ldr_post        r9,  r1,  r2
+        strd_post       r6,  r7,  r0,  r2
+        b               1b
+2:
+        and             r8,  r8,  lr
+        and             r12, r12, lr
+        uadd8           r6,  r6,  r8
+        uadd8           r7,  r7,  r12
+        strd_post       r6,  r7,  r0,  r2
+
+        pop             {r4-r10, pc}
+endfunc
diff --git a/libavcodec/arm/hpeldsp_init_arm.c b/libavcodec/arm/hpeldsp_init_arm.c
new file mode 100644
index 0000000..2cc2b78
--- /dev/null
+++ b/libavcodec/arm/hpeldsp_init_arm.c
@@ -0,0 +1,72 @@
+/*
+ * ARM optimized DSP utils
+ * Copyright (c) 2001 Lionel Ulmer
+ *
+ * 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 "libavutil/arm/cpu.h"
+#include "libavutil/attributes.h"
+#include "libavcodec/bit_depth_template.c" // for CALL_2X_PIXELS
+#include "libavcodec/rnd_avg.h"
+#include "hpeldsp_arm.h"
+
+void ff_put_pixels8_arm(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h);
+void ff_put_pixels8_x2_arm(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h);
+void ff_put_pixels8_y2_arm(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h);
+void ff_put_pixels8_xy2_arm(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h);
+
+void ff_put_no_rnd_pixels8_x2_arm(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h);
+void ff_put_no_rnd_pixels8_y2_arm(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h);
+void ff_put_no_rnd_pixels8_xy2_arm(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h);
+
+void ff_put_pixels16_arm(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h);
+
+CALL_2X_PIXELS(ff_put_pixels16_x2_arm,         ff_put_pixels8_x2_arm,        8)
+CALL_2X_PIXELS(ff_put_pixels16_y2_arm,         ff_put_pixels8_y2_arm,        8)
+CALL_2X_PIXELS(ff_put_pixels16_xy2_arm,        ff_put_pixels8_xy2_arm,       8)
+CALL_2X_PIXELS(ff_put_no_rnd_pixels16_x2_arm,  ff_put_no_rnd_pixels8_x2_arm, 8)
+CALL_2X_PIXELS(ff_put_no_rnd_pixels16_y2_arm,  ff_put_no_rnd_pixels8_y2_arm, 8)
+CALL_2X_PIXELS(ff_put_no_rnd_pixels16_xy2_arm, ff_put_no_rnd_pixels8_xy2_arm,8)
+
+av_cold void ff_hpeldsp_init_arm(HpelDSPContext *c, int flags)
+{
+    int cpu_flags = av_get_cpu_flags();
+
+    c->put_pixels_tab[0][0] = ff_put_pixels16_arm;
+    c->put_pixels_tab[0][1] = ff_put_pixels16_x2_arm;
+    c->put_pixels_tab[0][2] = ff_put_pixels16_y2_arm;
+    c->put_pixels_tab[0][3] = ff_put_pixels16_xy2_arm;
+    c->put_pixels_tab[1][0] = ff_put_pixels8_arm;
+    c->put_pixels_tab[1][1] = ff_put_pixels8_x2_arm;
+    c->put_pixels_tab[1][2] = ff_put_pixels8_y2_arm;
+    c->put_pixels_tab[1][3] = ff_put_pixels8_xy2_arm;
+
+    c->put_no_rnd_pixels_tab[0][0] = ff_put_pixels16_arm;
+    c->put_no_rnd_pixels_tab[0][1] = ff_put_no_rnd_pixels16_x2_arm;
+    c->put_no_rnd_pixels_tab[0][2] = ff_put_no_rnd_pixels16_y2_arm;
+    c->put_no_rnd_pixels_tab[0][3] = ff_put_no_rnd_pixels16_xy2_arm;
+    c->put_no_rnd_pixels_tab[1][0] = ff_put_pixels8_arm;
+    c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_arm;
+    c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_arm;
+    c->put_no_rnd_pixels_tab[1][3] = ff_put_no_rnd_pixels8_xy2_arm;
+
+    if (have_armv6(cpu_flags))
+        ff_hpeldsp_init_armv6(c, flags);
+    if (have_neon(cpu_flags))
+        ff_hpeldsp_init_neon(c, flags);
+}
diff --git a/libavcodec/arm/hpeldsp_init_armv6.c b/libavcodec/arm/hpeldsp_init_armv6.c
new file mode 100644
index 0000000..967a8e0
--- /dev/null
+++ b/libavcodec/arm/hpeldsp_init_armv6.c
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2009 Mans Rullgard <mans@mansr.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 <stddef.h>
+#include <stdint.h>
+
+#include "libavutil/attributes.h"
+#include "hpeldsp_arm.h"
+
+void ff_put_pixels16_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_put_pixels16_x2_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_put_pixels16_y2_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
+
+void ff_put_pixels16_x2_no_rnd_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_put_pixels16_y2_no_rnd_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
+
+void ff_avg_pixels16_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
+
+void ff_put_pixels8_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_put_pixels8_x2_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_put_pixels8_y2_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
+
+void ff_put_pixels8_x2_no_rnd_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_put_pixels8_y2_no_rnd_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
+
+void ff_avg_pixels8_armv6(uint8_t *, const uint8_t *, ptrdiff_t, int);
+
+av_cold void ff_hpeldsp_init_armv6(HpelDSPContext *c, int flags)
+{
+    c->put_pixels_tab[0][0] = ff_put_pixels16_armv6;
+    c->put_pixels_tab[0][1] = ff_put_pixels16_x2_armv6;
+    c->put_pixels_tab[0][2] = ff_put_pixels16_y2_armv6;
+/*     c->put_pixels_tab[0][3] = ff_put_pixels16_xy2_armv6; */
+    c->put_pixels_tab[1][0] = ff_put_pixels8_armv6;
+    c->put_pixels_tab[1][1] = ff_put_pixels8_x2_armv6;
+    c->put_pixels_tab[1][2] = ff_put_pixels8_y2_armv6;
+/*     c->put_pixels_tab[1][3] = ff_put_pixels8_xy2_armv6; */
+
+    c->put_no_rnd_pixels_tab[0][0] = ff_put_pixels16_armv6;
+    c->put_no_rnd_pixels_tab[0][1] = ff_put_pixels16_x2_no_rnd_armv6;
+    c->put_no_rnd_pixels_tab[0][2] = ff_put_pixels16_y2_no_rnd_armv6;
+/*     c->put_no_rnd_pixels_tab[0][3] = ff_put_pixels16_xy2_no_rnd_armv6; */
+    c->put_no_rnd_pixels_tab[1][0] = ff_put_pixels8_armv6;
+    c->put_no_rnd_pixels_tab[1][1] = ff_put_pixels8_x2_no_rnd_armv6;
+    c->put_no_rnd_pixels_tab[1][2] = ff_put_pixels8_y2_no_rnd_armv6;
+/*     c->put_no_rnd_pixels_tab[1][3] = ff_put_pixels8_xy2_no_rnd_armv6; */
+
+    c->avg_pixels_tab[0][0] = ff_avg_pixels16_armv6;
+    c->avg_pixels_tab[1][0] = ff_avg_pixels8_armv6;
+}
diff --git a/libavcodec/arm/hpeldsp_init_neon.c b/libavcodec/arm/hpeldsp_init_neon.c
new file mode 100644
index 0000000..d9feadd
--- /dev/null
+++ b/libavcodec/arm/hpeldsp_init_neon.c
@@ -0,0 +1,88 @@
+/*
+ * ARM NEON optimised DSP functions
+ * Copyright (c) 2008 Mans Rullgard <mans@mansr.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 <stddef.h>
+#include <stdint.h>
+
+#include "libavutil/attributes.h"
+#include "hpeldsp_arm.h"
+
+void ff_put_pixels16_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_put_pixels16_x2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_put_pixels16_y2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_put_pixels16_xy2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_put_pixels8_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_put_pixels8_x2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_put_pixels8_y2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_put_pixels8_xy2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_put_pixels16_x2_no_rnd_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_put_pixels16_y2_no_rnd_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_put_pixels16_xy2_no_rnd_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_put_pixels8_x2_no_rnd_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_put_pixels8_y2_no_rnd_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_put_pixels8_xy2_no_rnd_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+
+void ff_avg_pixels16_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_avg_pixels16_x2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_avg_pixels16_y2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_avg_pixels16_xy2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_avg_pixels8_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_avg_pixels8_x2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_avg_pixels8_y2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_avg_pixels8_xy2_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_avg_pixels16_x2_no_rnd_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_avg_pixels16_y2_no_rnd_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+void ff_avg_pixels16_xy2_no_rnd_neon(uint8_t *, const uint8_t *, ptrdiff_t, int);
+
+av_cold void ff_hpeldsp_init_neon(HpelDSPContext *c, int flags)
+{
+    c->put_pixels_tab[0][0] = ff_put_pixels16_neon;
+    c->put_pixels_tab[0][1] = ff_put_pixels16_x2_neon;
+    c->put_pixels_tab[0][2] = ff_put_pixels16_y2_neon;
+    c->put_pixels_tab[0][3] = ff_put_pixels16_xy2_neon;
+    c->put_pixels_tab[1][0] = ff_put_pixels8_neon;
+    c->put_pixels_tab[1][1] = ff_put_pixels8_x2_neon;
+    c->put_pixels_tab[1][2] = ff_put_pixels8_y2_neon;
+    c->put_pixels_tab[1][3] = ff_put_pixels8_xy2_neon;
+
+    c->put_no_rnd_pixels_tab[0][0] = ff_put_pixels16_neon;
+    c->put_no_rnd_pixels_tab[0][1] = ff_put_pixels16_x2_no_rnd_neon;
+    c->put_no_rnd_pixels_tab[0][2] = ff_put_pixels16_y2_no_rnd_neon;
+    c->put_no_rnd_pixels_tab[0][3] = ff_put_pixels16_xy2_no_rnd_neon;
+    c->put_no_rnd_pixels_tab[1][0] = ff_put_pixels8_neon;
+    c->put_no_rnd_pixels_tab[1][1] = ff_put_pixels8_x2_no_rnd_neon;
+    c->put_no_rnd_pixels_tab[1][2] = ff_put_pixels8_y2_no_rnd_neon;
+    c->put_no_rnd_pixels_tab[1][3] = ff_put_pixels8_xy2_no_rnd_neon;
+
+    c->avg_pixels_tab[0][0] = ff_avg_pixels16_neon;
+    c->avg_pixels_tab[0][1] = ff_avg_pixels16_x2_neon;
+    c->avg_pixels_tab[0][2] = ff_avg_pixels16_y2_neon;
+    c->avg_pixels_tab[0][3] = ff_avg_pixels16_xy2_neon;
+    c->avg_pixels_tab[1][0] = ff_avg_pixels8_neon;
+    c->avg_pixels_tab[1][1] = ff_avg_pixels8_x2_neon;
+    c->avg_pixels_tab[1][2] = ff_avg_pixels8_y2_neon;
+    c->avg_pixels_tab[1][3] = ff_avg_pixels8_xy2_neon;
+
+    c->avg_no_rnd_pixels_tab[0] = ff_avg_pixels16_neon;
+    c->avg_no_rnd_pixels_tab[1] = ff_avg_pixels16_x2_no_rnd_neon;
+    c->avg_no_rnd_pixels_tab[2] = ff_avg_pixels16_y2_no_rnd_neon;
+    c->avg_no_rnd_pixels_tab[3] = ff_avg_pixels16_xy2_no_rnd_neon;
+}
diff --git a/libavcodec/arm/hpeldsp_neon.S b/libavcodec/arm/hpeldsp_neon.S
new file mode 100644
index 0000000..cf4a6cf
--- /dev/null
+++ b/libavcodec/arm/hpeldsp_neon.S
@@ -0,0 +1,410 @@
+/*
+ * ARM NEON optimised DSP functions
+ * Copyright (c) 2008 Mans Rullgard <mans@mansr.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 "libavutil/arm/asm.S"
+
+.macro  pixels16        rnd=1, avg=0
+  .if \avg
+        mov             r12, r0
+  .endif
+1:      vld1.8          {q0},     [r1], r2
+        vld1.8          {q1},     [r1], r2
+        vld1.8          {q2},     [r1], r2
+        pld             [r1, r2, lsl #2]
+        vld1.8          {q3},     [r1], r2
+        pld             [r1]
+        pld             [r1, r2]
+        pld             [r1, r2, lsl #1]
+  .if \avg
+        vld1.8          {q8},     [r12,:128], r2
+        vrhadd.u8       q0,  q0,  q8
+        vld1.8          {q9},     [r12,:128], r2
+        vrhadd.u8       q1,  q1,  q9
+        vld1.8          {q10},    [r12,:128], r2
+        vrhadd.u8       q2,  q2,  q10
+        vld1.8          {q11},    [r12,:128], r2
+        vrhadd.u8       q3,  q3,  q11
+  .endif
+        subs            r3,  r3,  #4
+        vst1.64         {q0},     [r0,:128], r2
+        vst1.64         {q1},     [r0,:128], r2
+        vst1.64         {q2},     [r0,:128], r2
+        vst1.64         {q3},     [r0,:128], r2
+        bne             1b
+        bx              lr
+.endm
+
+.macro  pixels16_x2     rnd=1, avg=0
+1:      vld1.8          {d0-d2},  [r1], r2
+        vld1.8          {d4-d6},  [r1], r2
+        pld             [r1]
+        pld             [r1, r2]
+        subs            r3,  r3,  #2
+        vext.8          q1,  q0,  q1,  #1
+        avg             q0,  q0,  q1
+        vext.8          q3,  q2,  q3,  #1
+        avg             q2,  q2,  q3
+  .if \avg
+        vld1.8          {q1},     [r0,:128], r2
+        vld1.8          {q3},     [r0,:128]
+        vrhadd.u8       q0,  q0,  q1
+        vrhadd.u8       q2,  q2,  q3
+        sub             r0,  r0,  r2
+  .endif
+        vst1.8          {q0},     [r0,:128], r2
+        vst1.8          {q2},     [r0,:128], r2
+        bne             1b
+        bx              lr
+.endm
+
+.macro  pixels16_y2     rnd=1, avg=0
+        sub             r3,  r3,  #2
+        vld1.8          {q0},     [r1], r2
+        vld1.8          {q1},     [r1], r2
+1:      subs            r3,  r3,  #2
+        avg             q2,  q0,  q1
+        vld1.8          {q0},     [r1], r2
+        avg             q3,  q0,  q1
+        vld1.8          {q1},     [r1], r2
+        pld             [r1]
+        pld             [r1, r2]
+  .if \avg
+        vld1.8          {q8},     [r0,:128], r2
+        vld1.8          {q9},     [r0,:128]
+        vrhadd.u8       q2,  q2,  q8
+        vrhadd.u8       q3,  q3,  q9
+        sub             r0,  r0,  r2
+  .endif
+        vst1.8          {q2},     [r0,:128], r2
+        vst1.8          {q3},     [r0,:128], r2
+        bne             1b
+
+        avg             q2,  q0,  q1
+        vld1.8          {q0},     [r1], r2
+        avg             q3,  q0,  q1
+  .if \avg
+        vld1.8          {q8},     [r0,:128], r2
+        vld1.8          {q9},     [r0,:128]
+        vrhadd.u8       q2,  q2,  q8
+        vrhadd.u8       q3,  q3,  q9
+        sub             r0,  r0,  r2
+  .endif
+        vst1.8          {q2},     [r0,:128], r2
+        vst1.8          {q3},     [r0,:128], r2
+
+        bx              lr
+.endm
+
+.macro  pixels16_xy2    rnd=1, avg=0
+        sub             r3,  r3,  #2
+        vld1.8          {d0-d2},  [r1], r2
+        vld1.8          {d4-d6},  [r1], r2
+NRND    vmov.i16        q13, #1
+        pld             [r1]
+        pld             [r1, r2]
+        vext.8          q1,  q0,  q1,  #1
+        vext.8          q3,  q2,  q3,  #1
+        vaddl.u8        q8,  d0,  d2
+        vaddl.u8        q10, d1,  d3
+        vaddl.u8        q9,  d4,  d6
+        vaddl.u8        q11, d5,  d7
+1:      subs            r3,  r3,  #2
+        vld1.8          {d0-d2},  [r1], r2
+        vadd.u16        q12, q8,  q9
+        pld             [r1]
+NRND    vadd.u16        q12, q12, q13
+        vext.8          q15, q0,  q1,  #1
+        vadd.u16        q1 , q10, q11
+        shrn            d28, q12, #2
+NRND    vadd.u16        q1,  q1,  q13
+        shrn            d29, q1,  #2
+  .if \avg
+        vld1.8          {q8},     [r0,:128]
+        vrhadd.u8       q14, q14, q8
+  .endif
+        vaddl.u8        q8,  d0,  d30
+        vld1.8          {d2-d4},  [r1], r2
+        vaddl.u8        q10, d1,  d31
+        vst1.8          {q14},    [r0,:128], r2
+        vadd.u16        q12, q8,  q9
+        pld             [r1, r2]
+NRND    vadd.u16        q12, q12, q13
+        vext.8          q2,  q1,  q2,  #1
+        vadd.u16        q0,  q10, q11
+        shrn            d30, q12, #2
+NRND    vadd.u16        q0,  q0,  q13
+        shrn            d31, q0,  #2
+  .if \avg
+        vld1.8          {q9},     [r0,:128]
+        vrhadd.u8       q15, q15, q9
+  .endif
+        vaddl.u8        q9,  d2,  d4
+        vaddl.u8        q11, d3,  d5
+        vst1.8          {q15},    [r0,:128], r2
+        bgt             1b
+
+        vld1.8          {d0-d2},  [r1], r2
+        vadd.u16        q12, q8,  q9
+NRND    vadd.u16        q12, q12, q13
+        vext.8          q15, q0,  q1,  #1
+        vadd.u16        q1 , q10, q11
+        shrn            d28, q12, #2
+NRND    vadd.u16        q1,  q1,  q13
+        shrn            d29, q1,  #2
+  .if \avg
+        vld1.8          {q8},     [r0,:128]
+        vrhadd.u8       q14, q14, q8
+  .endif
+        vaddl.u8        q8,  d0,  d30
+        vaddl.u8        q10, d1,  d31
+        vst1.8          {q14},    [r0,:128], r2
+        vadd.u16        q12, q8,  q9
+NRND    vadd.u16        q12, q12, q13
+        vadd.u16        q0,  q10, q11
+        shrn            d30, q12, #2
+NRND    vadd.u16        q0,  q0,  q13
+        shrn            d31, q0,  #2
+  .if \avg
+        vld1.8          {q9},     [r0,:128]
+        vrhadd.u8       q15, q15, q9
+  .endif
+        vst1.8          {q15},    [r0,:128], r2
+
+        bx              lr
+.endm
+
+.macro  pixels8         rnd=1, avg=0
+1:      vld1.8          {d0},     [r1], r2
+        vld1.8          {d1},     [r1], r2
+        vld1.8          {d2},     [r1], r2
+        pld             [r1, r2, lsl #2]
+        vld1.8          {d3},     [r1], r2
+        pld             [r1]
+        pld             [r1, r2]
+        pld             [r1, r2, lsl #1]
+  .if \avg
+        vld1.8          {d4},     [r0,:64], r2
+        vrhadd.u8       d0,  d0,  d4
+        vld1.8          {d5},     [r0,:64], r2
+        vrhadd.u8       d1,  d1,  d5
+        vld1.8          {d6},     [r0,:64], r2
+        vrhadd.u8       d2,  d2,  d6
+        vld1.8          {d7},     [r0,:64], r2
+        vrhadd.u8       d3,  d3,  d7
+        sub             r0,  r0,  r2,  lsl #2
+  .endif
+        subs            r3,  r3,  #4
+        vst1.8          {d0},     [r0,:64], r2
+        vst1.8          {d1},     [r0,:64], r2
+        vst1.8          {d2},     [r0,:64], r2
+        vst1.8          {d3},     [r0,:64], r2
+        bne             1b
+        bx              lr
+.endm
+
+.macro  pixels8_x2      rnd=1, avg=0
+1:      vld1.8          {q0},     [r1], r2
+        vext.8          d1,  d0,  d1,  #1
+        vld1.8          {q1},     [r1], r2
+        vext.8          d3,  d2,  d3,  #1
+        pld             [r1]
+        pld             [r1, r2]
+        subs            r3,  r3,  #2
+        vswp            d1,  d2
+        avg             q0,  q0,  q1
+  .if \avg
+        vld1.8          {d4},     [r0,:64], r2
+        vld1.8          {d5},     [r0,:64]
+        vrhadd.u8       q0,  q0,  q2
+        sub             r0,  r0,  r2
+  .endif
+        vst1.8          {d0},     [r0,:64], r2
+        vst1.8          {d1},     [r0,:64], r2
+        bne             1b
+        bx              lr
+.endm
+
+.macro  pixels8_y2      rnd=1, avg=0
+        sub             r3,  r3,  #2
+        vld1.8          {d0},     [r1], r2
+        vld1.8          {d1},     [r1], r2
+1:      subs            r3,  r3,  #2
+        avg             d4,  d0,  d1
+        vld1.8          {d0},     [r1], r2
+        avg             d5,  d0,  d1
+        vld1.8          {d1},     [r1], r2
+        pld             [r1]
+        pld             [r1, r2]
+  .if \avg
+        vld1.8          {d2},     [r0,:64], r2
+        vld1.8          {d3},     [r0,:64]
+        vrhadd.u8       q2,  q2,  q1
+        sub             r0,  r0,  r2
+  .endif
+        vst1.8          {d4},     [r0,:64], r2
+        vst1.8          {d5},     [r0,:64], r2
+        bne             1b
+
+        avg             d4,  d0,  d1
+        vld1.8          {d0},     [r1], r2
+        avg             d5,  d0,  d1
+  .if \avg
+        vld1.8          {d2},     [r0,:64], r2
+        vld1.8          {d3},     [r0,:64]
+        vrhadd.u8       q2,  q2,  q1
+        sub             r0,  r0,  r2
+  .endif
+        vst1.8          {d4},     [r0,:64], r2
+        vst1.8          {d5},     [r0,:64], r2
+
+        bx              lr
+.endm
+
+.macro  pixels8_xy2     rnd=1, avg=0
+        sub             r3,  r3,  #2
+        vld1.8          {q0},     [r1], r2
+        vld1.8          {q1},     [r1], r2
+NRND    vmov.i16        q11, #1
+        pld             [r1]
+        pld             [r1, r2]
+        vext.8          d4,  d0,  d1,  #1
+        vext.8          d6,  d2,  d3,  #1
+        vaddl.u8        q8,  d0,  d4
+        vaddl.u8        q9,  d2,  d6
+1:      subs            r3,  r3,  #2
+        vld1.8          {q0},     [r1], r2
+        pld             [r1]
+        vadd.u16        q10, q8,  q9
+        vext.8          d4,  d0,  d1,  #1
+NRND    vadd.u16        q10, q10, q11
+        vaddl.u8        q8,  d0,  d4
+        shrn            d5,  q10, #2
+        vld1.8          {q1},     [r1], r2
+        vadd.u16        q10, q8,  q9
+        pld             [r1, r2]
+  .if \avg
+        vld1.8          {d7},     [r0,:64]
+        vrhadd.u8       d5,  d5,  d7
+  .endif
+NRND    vadd.u16        q10, q10, q11
+        vst1.8          {d5},     [r0,:64], r2
+        shrn            d7,  q10, #2
+  .if \avg
+        vld1.8          {d5},     [r0,:64]
+        vrhadd.u8       d7,  d7,  d5
+  .endif
+        vext.8          d6,  d2,  d3,  #1
+        vaddl.u8        q9,  d2,  d6
+        vst1.8          {d7},     [r0,:64], r2
+        bgt             1b
+
+        vld1.8          {q0},     [r1], r2
+        vadd.u16        q10, q8,  q9
+        vext.8          d4,  d0,  d1,  #1
+NRND    vadd.u16        q10, q10, q11
+        vaddl.u8        q8,  d0,  d4
+        shrn            d5,  q10, #2
+        vadd.u16        q10, q8,  q9
+  .if \avg
+        vld1.8          {d7},     [r0,:64]
+        vrhadd.u8       d5,  d5,  d7
+  .endif
+NRND    vadd.u16        q10, q10, q11
+        vst1.8          {d5},     [r0,:64], r2
+        shrn            d7,  q10, #2
+  .if \avg
+        vld1.8          {d5},     [r0,:64]
+        vrhadd.u8       d7,  d7,  d5
+  .endif
+        vst1.8          {d7},     [r0,:64], r2
+
+        bx              lr
+.endm
+
+.macro  pixfunc         pfx, name, suf, rnd=1, avg=0
+  .if \rnd
+    .macro avg  rd, rn, rm
+        vrhadd.u8       \rd, \rn, \rm
+    .endm
+    .macro shrn rd, rn, rm
+        vrshrn.u16      \rd, \rn, \rm
+    .endm
+    .macro NRND insn:vararg
+    .endm
+  .else
+    .macro avg  rd, rn, rm
+        vhadd.u8        \rd, \rn, \rm
+    .endm
+    .macro shrn rd, rn, rm
+        vshrn.u16       \rd, \rn, \rm
+    .endm
+    .macro NRND insn:vararg
+        \insn
+    .endm
+  .endif
+function ff_\pfx\name\suf\()_neon, export=1
+        \name           \rnd, \avg
+endfunc
+        .purgem         avg
+        .purgem         shrn
+        .purgem         NRND
+.endm
+
+.macro  pixfunc2        pfx, name, avg=0
+        pixfunc         \pfx, \name,          rnd=1, avg=\avg
+        pixfunc         \pfx, \name, _no_rnd, rnd=0, avg=\avg
+.endm
+
+function ff_put_h264_qpel16_mc00_neon, export=1
+        mov             r3,  #16
+endfunc
+
+        pixfunc         put_, pixels16,     avg=0
+        pixfunc2        put_, pixels16_x2,  avg=0
+        pixfunc2        put_, pixels16_y2,  avg=0
+        pixfunc2        put_, pixels16_xy2, avg=0
+
+function ff_avg_h264_qpel16_mc00_neon, export=1
+        mov             r3,  #16
+endfunc
+
+        pixfunc         avg_, pixels16,     avg=1
+        pixfunc2        avg_, pixels16_x2,  avg=1
+        pixfunc2        avg_, pixels16_y2,  avg=1
+        pixfunc2        avg_, pixels16_xy2, avg=1
+
+function ff_put_h264_qpel8_mc00_neon, export=1
+        mov             r3,  #8
+endfunc
+
+        pixfunc         put_, pixels8,     avg=0
+        pixfunc2        put_, pixels8_x2,  avg=0
+        pixfunc2        put_, pixels8_y2,  avg=0
+        pixfunc2        put_, pixels8_xy2, avg=0
+
+function ff_avg_h264_qpel8_mc00_neon, export=1
+        mov             r3,  #8
+endfunc
+
+        pixfunc         avg_, pixels8,     avg=1
+        pixfunc         avg_, pixels8_x2,  avg=1
+        pixfunc         avg_, pixels8_y2,  avg=1
+        pixfunc         avg_, pixels8_xy2, avg=1
diff --git a/libavcodec/arm/rv40dsp_init_arm.c b/libavcodec/arm/rv40dsp_init_arm.c
index 59ef10b..fec3702 100644
--- a/libavcodec/arm/rv40dsp_init_arm.c
+++ b/libavcodec/arm/rv40dsp_init_arm.c
@@ -27,7 +27,7 @@
 
 #define DECL_QPEL3(type, w, pos) \
     void ff_##type##_rv40_qpel##w##_mc##pos##_neon(uint8_t *dst, uint8_t *src,\
-                                                   int stride)
+                                                   ptrdiff_t stride)
 #define DECL_QPEL2(w, pos)                      \
     DECL_QPEL3(put, w, pos);                    \
     DECL_QPEL3(avg, w, pos)
diff --git a/libavcodec/arm/vp3dsp_init_arm.c b/libavcodec/arm/vp3dsp_init_arm.c
index 5ffc8fd..5af795b 100644
--- a/libavcodec/arm/vp3dsp_init_arm.c
+++ b/libavcodec/arm/vp3dsp_init_arm.c
@@ -41,6 +41,5 @@
         c->idct_dc_add   = ff_vp3_idct_dc_add_neon;
         c->v_loop_filter = ff_vp3_v_loop_filter_neon;
         c->h_loop_filter = ff_vp3_h_loop_filter_neon;
-        c->idct_perm     = FF_TRANSPOSE_IDCT_PERM;
     }
 }
diff --git a/libavcodec/ass.c b/libavcodec/ass.c
index db0fdd8..b263c63 100644
--- a/libavcodec/ass.c
+++ b/libavcodec/ass.c
@@ -23,6 +23,7 @@
 #include "ass.h"
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
+#include "libavutil/bprint.h"
 #include "libavutil/common.h"
 
 int ff_ass_subtitle_header(AVCodecContext *avctx,
@@ -62,36 +63,60 @@
                                          ASS_DEFAULT_ALIGNMENT);
 }
 
-static int ts_to_string(char *str, int strlen, int ts)
+static void insert_ts(AVBPrint *buf, int ts)
 {
-    int h, m, s;
-    h = ts/360000;  ts -= 360000*h;
-    m = ts/  6000;  ts -=   6000*m;
-    s = ts/   100;  ts -=    100*s;
-    return snprintf(str, strlen, "%d:%02d:%02d.%02d", h, m, s, ts);
+    if (ts == -1) {
+        av_bprintf(buf, "9:59:59.99,");
+    } else {
+        int h, m, s;
+
+        h = ts/360000;  ts -= 360000*h;
+        m = ts/  6000;  ts -=   6000*m;
+        s = ts/   100;  ts -=    100*s;
+        av_bprintf(buf, "%d:%02d:%02d.%02d,", h, m, s, ts);
+    }
 }
 
 int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
                     int ts_start, int duration, int raw)
 {
-    int len = 0, dlen;
-    char s_start[16], s_end[16], header[48] = {0};
+    AVBPrint buf;
+    int ret, dlen;
     AVSubtitleRect **rects;
 
-    if (!raw) {
-        ts_to_string(s_start, sizeof(s_start), ts_start);
-        if (duration == -1)
-            snprintf(s_end, sizeof(s_end), "9:59:59.99");
-        else
-            ts_to_string(s_end, sizeof(s_end), ts_start + duration);
-        len = snprintf(header, sizeof(header), "Dialogue: 0,%s,%s,Default,",
-                       s_start, s_end);
-        av_assert0(len < sizeof(header));
+    av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
+    if (!raw || raw == 2) {
+        long int layer = 0;
+
+        if (raw == 2) {
+            /* skip ReadOrder */
+            dialog = strchr(dialog, ',');
+            if (!dialog)
+                return AVERROR_INVALIDDATA;
+            dialog++;
+
+            /* extract Layer or Marked */
+            layer = strtol(dialog, (char**)&dialog, 10);
+            if (*dialog != ',')
+                return AVERROR_INVALIDDATA;
+            dialog++;
+        }
+        av_bprintf(&buf, "Dialogue: %ld,", layer);
+        insert_ts(&buf, ts_start);
+        insert_ts(&buf, duration == -1 ? -1 : ts_start + duration);
+        if (raw != 2)
+            av_bprintf(&buf, "Default,");
     }
 
     dlen = strcspn(dialog, "\n");
     dlen += dialog[dlen] == '\n';
 
+    av_bprintf(&buf, "%.*s", dlen, dialog);
+    if (raw == 2)
+        av_bprintf(&buf, "\r\n");
+    if (!av_bprint_is_complete(&buf))
+        return AVERROR(ENOMEM);
+
     rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects));
     if (!rects)
         return AVERROR(ENOMEM);
@@ -99,9 +124,9 @@
     sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration);
     rects[sub->num_rects]       = av_mallocz(sizeof(*rects[0]));
     rects[sub->num_rects]->type = SUBTITLE_ASS;
-    rects[sub->num_rects]->ass  = av_malloc(len + dlen + 1);
-    strcpy (rects[sub->num_rects]->ass      , header);
-    av_strlcpy(rects[sub->num_rects]->ass + len, dialog, dlen + 1);
+    ret = av_bprint_finalize(&buf, &rects[sub->num_rects]->ass);
+    if (ret < 0)
+        return ret;
     sub->num_rects++;
     return dlen;
 }
diff --git a/libavcodec/ass.h b/libavcodec/ass.h
index e9339e4..ef99b58 100644
--- a/libavcodec/ass.h
+++ b/libavcodec/ass.h
@@ -76,7 +76,9 @@
  * @param ts_start start timestamp for this dialog (in 1/100 second unit)
  * @param duration duration for this dialog (in 1/100 second unit), can be -1
  *                 to last until the end of the presentation
- * @param raw when set to 1, it indicates that dialog contains a whole ASS
+ * @param raw when set to 2, it indicates that dialog contains an ASS
+ *                           dialog line as muxed in Matroska
+ *            when set to 1, it indicates that dialog contains a whole SSA
  *                           dialog line which should be copied as is.
  *            when set to 0, it indicates that dialog contains only the Text
  *                           part of the ASS dialog line, the rest of the line
diff --git a/libavcodec/ass_split.h b/libavcodec/ass_split.h
index 7a6a75e..06c1ce3 100644
--- a/libavcodec/ass_split.h
+++ b/libavcodec/ass_split.h
@@ -124,7 +124,7 @@
     void (*text)(void *priv, const char *text, int len);
     void (*new_line)(void *priv, int forced);
     void (*style)(void *priv, char style, int close);
-    void (*color)(void *priv, unsigned int color, unsigned int color_id);
+    void (*color)(void *priv, unsigned int /* color */, unsigned int color_id);
     void (*alpha)(void *priv, int alpha, int alpha_id);
     void (*font_name)(void *priv, const char *name);
     void (*font_size)(void *priv, int size);
diff --git a/libavcodec/assdec.c b/libavcodec/assdec.c
index d790656..11dbde0 100644
--- a/libavcodec/assdec.c
+++ b/libavcodec/assdec.c
@@ -41,7 +41,15 @@
     return 0;
 }
 
-static int ass_decode_frame(AVCodecContext *avctx, void *data, int *got_sub_ptr,
+static int ass_decode_close(AVCodecContext *avctx)
+{
+    ff_ass_split_free(avctx->priv_data);
+    avctx->priv_data = NULL;
+    return 0;
+}
+
+#if CONFIG_SSA_DECODER
+static int ssa_decode_frame(AVCodecContext *avctx, void *data, int *got_sub_ptr,
                             AVPacket *avpkt)
 {
     const char *ptr = avpkt->data;
@@ -64,19 +72,49 @@
     return avpkt->size;
 }
 
-static int ass_decode_close(AVCodecContext *avctx)
-{
-    ff_ass_split_free(avctx->priv_data);
-    avctx->priv_data = NULL;
-    return 0;
-}
-
-AVCodec ff_ass_decoder = {
-    .name         = "ass",
+AVCodec ff_ssa_decoder = {
+    .name         = "ssa",
     .long_name    = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
     .type         = AVMEDIA_TYPE_SUBTITLE,
     .id           = AV_CODEC_ID_SSA,
     .init         = ass_decode_init,
+    .decode       = ssa_decode_frame,
+    .close        = ass_decode_close,
+};
+#endif
+
+#if CONFIG_ASS_DECODER
+static int ass_decode_frame(AVCodecContext *avctx, void *data, int *got_sub_ptr,
+                            AVPacket *avpkt)
+{
+    int ret;
+    AVSubtitle *sub = data;
+    const char *ptr = avpkt->data;
+    static const AVRational ass_tb = {1, 100};
+    const int ts_start    = av_rescale_q(avpkt->pts,      avctx->time_base, ass_tb);
+    const int ts_duration = av_rescale_q(avpkt->duration, avctx->time_base, ass_tb);
+
+    if (avpkt->size <= 0)
+        return avpkt->size;
+
+    ret = ff_ass_add_rect(sub, ptr, ts_start, ts_duration, 2);
+    if (ret < 0) {
+        if (ret == AVERROR_INVALIDDATA)
+            av_log(avctx, AV_LOG_ERROR, "Invalid ASS packet\n");
+        return ret;
+    }
+
+    *got_sub_ptr = avpkt->size > 0;
+    return avpkt->size;
+}
+
+AVCodec ff_ass_decoder = {
+    .name         = "ass",
+    .long_name    = NULL_IF_CONFIG_SMALL("ASS (Advanced SubStation Alpha) subtitle"),
+    .type         = AVMEDIA_TYPE_SUBTITLE,
+    .id           = AV_CODEC_ID_ASS,
+    .init         = ass_decode_init,
     .decode       = ass_decode_frame,
     .close        = ass_decode_close,
 };
+#endif
diff --git a/libavcodec/assenc.c b/libavcodec/assenc.c
index 50b89c0..7b8a540 100644
--- a/libavcodec/assenc.c
+++ b/libavcodec/assenc.c
@@ -22,10 +22,16 @@
 #include <string.h>
 
 #include "avcodec.h"
+#include "ass_split.h"
+#include "ass.h"
 #include "libavutil/avstring.h"
 #include "libavutil/internal.h"
 #include "libavutil/mem.h"
 
+typedef struct {
+    int id; ///< current event id, ReadOrder field
+} ASSEncodeContext;
+
 static av_cold int ass_encode_init(AVCodecContext *avctx)
 {
     avctx->extradata = av_malloc(avctx->subtitle_header_size + 1);
@@ -41,15 +47,47 @@
                             unsigned char *buf, int bufsize,
                             const AVSubtitle *sub)
 {
+    ASSEncodeContext *s = avctx->priv_data;
     int i, len, total_len = 0;
 
     for (i=0; i<sub->num_rects; i++) {
+        char ass_line[2048];
+        const char *ass = sub->rects[i]->ass;
+
         if (sub->rects[i]->type != SUBTITLE_ASS) {
             av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
             return -1;
         }
 
-        len = av_strlcpy(buf+total_len, sub->rects[i]->ass, bufsize-total_len);
+        if (strncmp(ass, "Dialogue: ", 10)) {
+            av_log(avctx, AV_LOG_ERROR, "AVSubtitle rectangle ass \"%s\""
+                   " does not look like a SSA markup\n", ass);
+            return AVERROR_INVALIDDATA;
+        }
+
+        if (avctx->codec->id == AV_CODEC_ID_ASS) {
+            long int layer;
+            char *p;
+
+            if (i > 0) {
+                av_log(avctx, AV_LOG_ERROR, "ASS encoder supports only one "
+                       "ASS rectangle field.\n");
+                return AVERROR_INVALIDDATA;
+            }
+
+            ass += 10; // skip "Dialogue: "
+            /* parse Layer field. If it's a Marked field, the content
+             * will be "Marked=N" instead of the layer num, so we will
+             * have layer=0, which is fine. */
+            layer = strtol(ass, &p, 10);
+            if (*p) p += strcspn(p, ",") + 1; // skip layer or marked
+            if (*p) p += strcspn(p, ",") + 1; // skip start timestamp
+            if (*p) p += strcspn(p, ",") + 1; // skip end timestamp
+            snprintf(ass_line, sizeof(ass_line), "%d,%ld,%s", ++s->id, layer, p);
+            ass_line[strcspn(ass_line, "\r\n")] = 0;
+            ass = ass_line;
+        }
+        len = av_strlcpy(buf+total_len, ass, bufsize-total_len);
 
         if (len > bufsize-total_len-1) {
             av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
@@ -62,11 +100,26 @@
     return total_len;
 }
 
-AVCodec ff_ass_encoder = {
-    .name         = "ass",
+#if CONFIG_SSA_ENCODER
+AVCodec ff_ssa_encoder = {
+    .name         = "ssa",
     .long_name    = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
     .type         = AVMEDIA_TYPE_SUBTITLE,
     .id           = AV_CODEC_ID_SSA,
     .init         = ass_encode_init,
     .encode_sub   = ass_encode_frame,
+    .priv_data_size = sizeof(ASSEncodeContext),
 };
+#endif
+
+#if CONFIG_ASS_ENCODER
+AVCodec ff_ass_encoder = {
+    .name         = "ass",
+    .long_name    = NULL_IF_CONFIG_SMALL("ASS (Advanced SubStation Alpha) subtitle"),
+    .type         = AVMEDIA_TYPE_SUBTITLE,
+    .id           = AV_CODEC_ID_ASS,
+    .init         = ass_encode_init,
+    .encode_sub   = ass_encode_frame,
+    .priv_data_size = sizeof(ASSEncodeContext),
+};
+#endif
diff --git a/libavcodec/asvdec.c b/libavcodec/asvdec.c
index e7a04cd..7dca22b 100644
--- a/libavcodec/asvdec.c
+++ b/libavcodec/asvdec.c
@@ -180,14 +180,14 @@
     return 0;
 }
 
-static inline void idct_put(ASV1Context *a, int mb_x, int mb_y)
+static inline void idct_put(ASV1Context *a, AVFrame *frame, int mb_x, int mb_y)
 {
     int16_t (*block)[64] = a->block;
-    int linesize         = a->picture.linesize[0];
+    int linesize         = frame->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;
+    uint8_t *dest_y  = frame->data[0] + (mb_y * 16* linesize              ) + mb_x * 16;
+    uint8_t *dest_cb = frame->data[1] + (mb_y * 8 * frame->linesize[1]) + mb_x * 8;
+    uint8_t *dest_cr = frame->data[2] + (mb_y * 8 * frame->linesize[2]) + mb_x * 8;
 
     a->dsp.idct_put(dest_y                 , linesize, block[0]);
     a->dsp.idct_put(dest_y              + 8, linesize, block[1]);
@@ -195,8 +195,8 @@
     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]);
+        a->dsp.idct_put(dest_cb, frame->linesize[1], block[4]);
+        a->dsp.idct_put(dest_cr, frame->linesize[2], block[5]);
     }
 }
 
@@ -207,18 +207,11 @@
     ASV1Context * const a = avctx->priv_data;
     const uint8_t *buf    = avpkt->data;
     int buf_size          = avpkt->size;
-    AVFrame *picture      = data;
-    AVFrame * const p     = &a->picture;
+    AVFrame * const p     = data;
     int mb_x, mb_y, ret;
 
-    if (p->data[0])
-        avctx->release_buffer(avctx, p);
-
-    p->reference = 0;
-    if ((ret = ff_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
         return ret;
-    }
     p->pict_type = AV_PICTURE_TYPE_I;
     p->key_frame = 1;
 
@@ -242,7 +235,7 @@
             if ((ret = decode_mb(a, a->block)) < 0)
                 return ret;
 
-            idct_put(a, mb_x, mb_y);
+            idct_put(a, p, mb_x, mb_y);
         }
     }
 
@@ -252,7 +245,7 @@
             if ((ret = decode_mb(a, a->block)) < 0)
                 return ret;
 
-            idct_put(a, mb_x, mb_y);
+            idct_put(a, p, mb_x, mb_y);
         }
     }
 
@@ -262,11 +255,10 @@
             if ((ret = decode_mb(a, a->block)) < 0)
                 return ret;
 
-            idct_put(a, mb_x, mb_y);
+            idct_put(a, p, mb_x, mb_y);
         }
     }
 
-    *picture   = a->picture;
     *got_frame = 1;
 
     emms_c();
@@ -277,7 +269,6 @@
 static av_cold int decode_init(AVCodecContext *avctx)
 {
     ASV1Context * const a = avctx->priv_data;
-    AVFrame *p            = &a->picture;
     const int scale       = avctx->codec_id == AV_CODEC_ID_ASV1 ? 1 : 2;
     int i;
 
@@ -300,11 +291,6 @@
         a->intra_matrix[i] = 64 * scale * ff_mpeg1_default_intra_matrix[index] / a->inv_qscale;
     }
 
-    p->qstride      = a->mb_width;
-    p->qscale_table = av_malloc(p->qstride * a->mb_height);
-    p->quality      = (32 * scale + a->inv_qscale / 2) / a->inv_qscale;
-    memset(p->qscale_table, p->quality, p->qstride * a->mb_height);
-
     return 0;
 }
 
@@ -313,12 +299,8 @@
     ASV1Context * const a = avctx->priv_data;
 
     av_freep(&a->bitstream_buffer);
-    av_freep(&a->picture.qscale_table);
     a->bitstream_buffer_size = 0;
 
-    if (a->picture.data[0])
-        avctx->release_buffer(avctx, &a->picture);
-
     return 0;
 }
 
diff --git a/libavcodec/atrac1.c b/libavcodec/atrac1.c
index 72c1b50..7c1d1eb 100644
--- a/libavcodec/atrac1.c
+++ b/libavcodec/atrac1.c
@@ -287,10 +287,8 @@
 
     /* get output buffer */
     frame->nb_samples = AT1_SU_SAMPLES;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     for (ch = 0; ch < avctx->channels; ch++) {
         AT1SUCtx* su = &q->SUs[ch];
@@ -344,7 +342,7 @@
     }
 
     if (avctx->block_align <= 0) {
-        av_log_ask_for_sample(avctx, "unsupported block align\n");
+        av_log(avctx, AV_LOG_ERROR, "Unsupported block align.");
         return AVERROR_PATCHWELCOME;
     }
 
diff --git a/libavcodec/atrac3.c b/libavcodec/atrac3.c
index 6216536..a9e98f8 100644
--- a/libavcodec/atrac3.c
+++ b/libavcodec/atrac3.c
@@ -164,13 +164,16 @@
 
     off = (intptr_t)input & 3;
     buf = (const uint32_t *)(input - off);
-    c   = av_be2ne32((0x537F6103 >> (off * 8)) | (0x537F6103 << (32 - (off * 8))));
+    if (off)
+        c = av_be2ne32((0x537F6103U >> (off * 8)) | (0x537F6103U << (32 - (off * 8))));
+    else
+        c = av_be2ne32(0x537F6103U);
     bytes += 3 + off;
     for (i = 0; i < bytes / 4; i++)
         output[i] = c ^ buf[i];
 
     if (off)
-        av_log_ask_for_sample(NULL, "Offset of %d not handled.\n", off);
+        avpriv_request_sample(NULL, "Offset of %d", off);
 
     return off;
 }
@@ -813,10 +816,8 @@
 
     /* get output buffer */
     frame->nb_samples = SAMPLES_PER_FRAME;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     /* Check if we need to descramble and what buffer to pass on. */
     if (q->scrambled_stream) {
diff --git a/libavcodec/aura.c b/libavcodec/aura.c
index c591f2f..34d46ae 100644
--- a/libavcodec/aura.c
+++ b/libavcodec/aura.c
@@ -27,21 +27,12 @@
 #include "internal.h"
 #include "libavutil/internal.h"
 
-typedef struct AuraDecodeContext {
-    AVCodecContext *avctx;
-    AVFrame frame;
-} AuraDecodeContext;
-
 static av_cold int aura_decode_init(AVCodecContext *avctx)
 {
-    AuraDecodeContext *s = avctx->priv_data;
-
-    s->avctx = avctx;
     /* width needs to be divisible by 4 for this codec to work */
     if (avctx->width & 0x3)
         return AVERROR(EINVAL);
     avctx->pix_fmt = AV_PIX_FMT_YUV422P;
-    avcodec_get_frame_defaults(&s->frame);
 
     return 0;
 }
@@ -50,7 +41,7 @@
                              void *data, int *got_frame,
                              AVPacket *pkt)
 {
-    AuraDecodeContext *s = avctx->priv_data;
+    AVFrame *frame = data;
     uint8_t *Y, *U, *V;
     uint8_t val;
     int x, y, ret;
@@ -68,19 +59,12 @@
     /* pixel data starts 48 bytes in, after 3x16-byte tables */
     buf += 48;
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
-
-    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
-    s->frame.reference = 0;
-    if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
-    Y = s->frame.data[0];
-    U = s->frame.data[1];
-    V = s->frame.data[2];
+    Y = frame->data[0];
+    U = frame->data[1];
+    V = frame->data[2];
 
     /* iterate through each line in the height */
     for (y = 0; y < avctx->height; y++) {
@@ -103,34 +87,21 @@
             Y[1] = Y[ 0] + delta_table[val & 0xF];
             Y   += 2; U++; V++;
         }
-        Y += s->frame.linesize[0] -  avctx->width;
-        U += s->frame.linesize[1] - (avctx->width >> 1);
-        V += s->frame.linesize[2] - (avctx->width >> 1);
+        Y += frame->linesize[0] -  avctx->width;
+        U += frame->linesize[1] - (avctx->width >> 1);
+        V += frame->linesize[2] - (avctx->width >> 1);
     }
 
     *got_frame = 1;
-    *(AVFrame*)data = s->frame;
 
     return pkt->size;
 }
 
-static av_cold int aura_decode_end(AVCodecContext *avctx)
-{
-    AuraDecodeContext *s = avctx->priv_data;
-
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
-
-    return 0;
-}
-
 AVCodec ff_aura2_decoder = {
     .name           = "aura2",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_AURA2,
-    .priv_data_size = sizeof(AuraDecodeContext),
     .init           = aura_decode_init,
-    .close          = aura_decode_end,
     .decode         = aura_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("Auravision Aura 2"),
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index f809e3d..e5b0c2b 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -29,9 +29,11 @@
 #include <errno.h>
 #include "libavutil/samplefmt.h"
 #include "libavutil/avutil.h"
+#include "libavutil/buffer.h"
 #include "libavutil/cpu.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/dict.h"
+#include "libavutil/frame.h"
 #include "libavutil/log.h"
 #include "libavutil/pixfmt.h"
 #include "libavutil/rational.h"
@@ -154,7 +156,6 @@
     AV_CODEC_ID_MSZH,
     AV_CODEC_ID_ZLIB,
     AV_CODEC_ID_QTRLE,
-    AV_CODEC_ID_SNOW,
     AV_CODEC_ID_TSCC,
     AV_CODEC_ID_ULTI,
     AV_CODEC_ID_QDRAW,
@@ -290,6 +291,8 @@
     AV_CODEC_ID_SGIRLE     = MKBETAG('S','G','I','R'),
     AV_CODEC_ID_MVC1       = MKBETAG('M','V','C','1'),
     AV_CODEC_ID_MVC2       = MKBETAG('M','V','C','2'),
+    AV_CODEC_ID_SNOW       = MKBETAG('S','N','O','W'),
+    AV_CODEC_ID_WEBP       = MKBETAG('W','E','B','P'),
 
     /* various PCM "codecs" */
     AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
@@ -440,9 +443,6 @@
     AV_CODEC_ID_COMFORT_NOISE,
     AV_CODEC_ID_TAK_DEPRECATED,
     AV_CODEC_ID_FFWAVESYNTH = MKBETAG('F','F','W','S'),
-#if LIBAVCODEC_VERSION_MAJOR <= 54
-    AV_CODEC_ID_8SVX_RAW    = MKBETAG('8','S','V','X'),
-#endif
     AV_CODEC_ID_SONIC       = MKBETAG('S','O','N','C'),
     AV_CODEC_ID_SONIC_LS    = MKBETAG('S','O','N','L'),
     AV_CODEC_ID_PAF_AUDIO   = MKBETAG('P','A','F','A'),
@@ -474,6 +474,7 @@
     AV_CODEC_ID_MPL2       = MKBETAG('M','P','L','2'),
     AV_CODEC_ID_VPLAYER    = MKBETAG('V','P','l','r'),
     AV_CODEC_ID_PJS        = MKBETAG('P','h','J','S'),
+    AV_CODEC_ID_ASS        = MKBETAG('A','S','S',' '),  ///< ASS as defined in Matroska
 
     /* other specific kind of codecs (generally used for attachments) */
     AV_CODEC_ID_FIRST_UNKNOWN = 0x18000,           ///< A dummy ID pointing at the start of various fake codecs.
@@ -483,6 +484,8 @@
     AV_CODEC_ID_IDF        = MKBETAG( 0 ,'I','D','F'),
     AV_CODEC_ID_OTF        = MKBETAG( 0 ,'O','T','F'),
     AV_CODEC_ID_SMPTE_KLV  = MKBETAG('K','L','V','A'),
+    AV_CODEC_ID_DVD_NAV    = MKBETAG('D','N','A','V'),
+
 
     AV_CODEC_ID_PROBE = 0x19000, ///< codec_id is not known (like AV_CODEC_ID_NONE) but lavf should attempt to identify it
 
@@ -497,10 +500,6 @@
 #endif
 };
 
-#if FF_API_CODEC_ID
-#define CodecID AVCodecID
-#endif
-
 /**
  * This struct describes the properties of a single codec described by an
  * AVCodecID.
@@ -542,13 +541,14 @@
 #define AV_CODEC_PROP_LOSSLESS      (1 << 2)
 /**
  * Subtitle codec is bitmap based
+ * Decoded AVSubtitle data can be read from the AVSubtitleRect->pict field.
  */
 #define AV_CODEC_PROP_BITMAP_SUB    (1 << 16)
-
-#if FF_API_OLD_DECODE_AUDIO
-/* in bytes */
-#define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio
-#endif
+/**
+ * Subtitle codec is text based.
+ * Decoded AVSubtitle data can be read from the AVSubtitleRect->ass field.
+ */
+#define AV_CODEC_PROP_TEXT_SUB      (1 << 17)
 
 /**
  * @ingroup lavc_decoding
@@ -581,8 +581,8 @@
     ME_X1,          ///< reserved for experiments
     ME_HEX,         ///< hexagon based search
     ME_UMH,         ///< uneven multi-hexagon search
-    ME_ITER,        ///< iterative search
     ME_TESA,        ///< transformed exhaustive search algorithm
+    ME_ITER=50,     ///< iterative search
 };
 
 /**
@@ -685,6 +685,11 @@
    Note: Not everything is supported yet.
 */
 
+/**
+ * Allow decoders to produce frames with data planes that are not aligned
+ * to CPU requirements (e.g. due to cropping).
+ */
+#define CODEC_FLAG_UNALIGNED 0x0001
 #define CODEC_FLAG_QSCALE 0x0002  ///< Use fixed qscale.
 #define CODEC_FLAG_4MV    0x0004  ///< 4 MV per MB allowed / advanced prediction for H.263.
 #define CODEC_FLAG_QPEL   0x0010  ///< Use qpel MC.
@@ -719,12 +724,6 @@
 #define CODEC_FLAG2_DROP_FRAME_TIMECODE 0x00002000 ///< timecode is in drop frame format. DEPRECATED!!!!
 #define CODEC_FLAG2_IGNORE_CROP   0x00010000 ///< Discard cropping information from SPS.
 
-#if FF_API_MPV_GLOBAL_OPTS
-#define CODEC_FLAG_CBP_RD         0x04000000 ///< Use rate distortion optimization for cbp.
-#define CODEC_FLAG_QP_RD          0x08000000 ///< Use rate distortion optimization for qp selectioon.
-#define CODEC_FLAG2_STRICT_GOP    0x00000002 ///< Strictly enforce GOP size.
-#define CODEC_FLAG2_SKIP_RD       0x00004000 ///< RD optimal MB level residual skipping
-#endif
 #define CODEC_FLAG2_CHUNKS        0x00008000 ///< Input bitstream might be truncated at a packet boundaries instead of only at frame boundaries.
 #define CODEC_FLAG2_SHOW_ALL      0x00400000 ///< Show all frames before the first keyframe
 
@@ -892,6 +891,7 @@
 #define FF_QSCALE_TYPE_H264  2
 #define FF_QSCALE_TYPE_VP56  3
 
+#if FF_API_GET_BUFFER
 #define FF_BUFFER_TYPE_INTERNAL 1
 #define FF_BUFFER_TYPE_USER     2 ///< direct rendering buffers (image is (de)allocated by user)
 #define FF_BUFFER_TYPE_SHARED   4 ///< Buffer from somewhere else; don't deallocate image (data/base), all other tables are not shared.
@@ -901,6 +901,12 @@
 #define FF_BUFFER_HINTS_READABLE 0x02 // Codec will read from buffer.
 #define FF_BUFFER_HINTS_PRESERVE 0x04 // User must not alter buffer content.
 #define FF_BUFFER_HINTS_REUSABLE 0x08 // Codec will reuse the buffer (update).
+#endif
+
+/**
+ * The decoder will keep a reference to the frame and may reuse it later.
+ */
+#define AV_GET_BUFFER_FLAG_REF (1 << 0)
 
 /**
  * @defgroup lavc_packet AVPacket
@@ -1009,18 +1015,24 @@
  * ABI. Thus it may be allocated on stack and no new fields can be added to it
  * without libavcodec and libavformat major bump.
  *
- * The semantics of data ownership depends on the destruct field.
- * If it is set, the packet data is dynamically allocated and is valid
- * indefinitely until av_free_packet() is called (which in turn calls the
- * destruct callback to free the data). If destruct is not set, the packet data
- * is typically backed by some static buffer somewhere and is only valid for a
- * limited time (e.g. until the next read call when demuxing).
+ * The semantics of data ownership depends on the buf or destruct (deprecated)
+ * fields. If either is set, the packet data is dynamically allocated and is
+ * valid indefinitely until av_free_packet() is called (which in turn calls
+ * av_buffer_unref()/the destruct callback to free the data). If neither is set,
+ * the packet data is typically backed by some static buffer somewhere and is
+ * only valid for a limited time (e.g. until the next read call when demuxing).
  *
  * The side data is always allocated with av_malloc() and is freed in
  * av_free_packet().
  */
 typedef struct AVPacket {
     /**
+     * A reference to the reference-counted buffer where the packet data is
+     * stored.
+     * May be NULL, then the packet data is not reference-counted.
+     */
+    AVBufferRef *buf;
+    /**
      * Presentation timestamp in AVStream->time_base units; the time at which
      * the decompressed packet will be presented to the user.
      * Can be AV_NOPTS_VALUE if it is not stored in the file.
@@ -1059,8 +1071,12 @@
      * Equals next_pts - this_pts in presentation order.
      */
     int   duration;
+#if FF_API_DESTRUCT_PACKET
+    attribute_deprecated
     void  (*destruct)(struct AVPacket *);
+    attribute_deprecated
     void  *priv;
+#endif
     int64_t pos;                            ///< byte position in stream, -1 if unknown
 
     /**
@@ -1095,449 +1111,6 @@
  * @}
  */
 
-/**
- * This structure describes decoded (raw) audio or video data.
- *
- * AVFrame must be allocated using avcodec_alloc_frame() and freed with
- * avcodec_free_frame(). Note that this allocates only the AVFrame itself. The
- * buffers for the data must be managed through other means.
- *
- * AVFrame is typically allocated once and then reused multiple times to hold
- * different data (e.g. a single AVFrame to hold frames received from a
- * decoder). In such a case, avcodec_get_frame_defaults() should be used to
- * reset the frame to its original clean state before it is reused again.
- *
- * sizeof(AVFrame) is not a part of the public ABI, so new fields may be added
- * to the end with a minor bump.
- * Similarly fields that are marked as to be only accessed by
- * av_opt_ptr() can be reordered. This allows 2 forks to add fields
- * without breaking compatibility with each other.
- */
-typedef struct AVFrame {
-#define AV_NUM_DATA_POINTERS 8
-    /**
-     * pointer to the picture/channel planes.
-     * This might be different from the first allocated byte
-     * - encoding: Set by user
-     * - decoding: set by AVCodecContext.get_buffer()
-     */
-    uint8_t *data[AV_NUM_DATA_POINTERS];
-
-    /**
-     * Size, in bytes, of the data for each picture/channel plane.
-     *
-     * For audio, only linesize[0] may be set. For planar audio, each channel
-     * plane must be the same size.
-     *
-     * - encoding: Set by user
-     * - decoding: set by AVCodecContext.get_buffer()
-     */
-    int linesize[AV_NUM_DATA_POINTERS];
-
-    /**
-     * pointers to the data planes/channels.
-     *
-     * For video, this should simply point to data[].
-     *
-     * For planar audio, each channel has a separate data pointer, and
-     * linesize[0] contains the size of each channel buffer.
-     * For packed audio, there is just one data pointer, and linesize[0]
-     * contains the total size of the buffer for all channels.
-     *
-     * Note: Both data and extended_data will always be set by get_buffer(),
-     * but for planar audio with more channels that can fit in data,
-     * extended_data must be used by the decoder in order to access all
-     * channels.
-     *
-     * encoding: set by user
-     * decoding: set by AVCodecContext.get_buffer()
-     */
-    uint8_t **extended_data;
-
-    /**
-     * width and height of the video frame
-     * - encoding: unused
-     * - decoding: Read by user.
-     */
-    int width, height;
-
-    /**
-     * number of audio samples (per channel) described by this frame
-     * - encoding: Set by user
-     * - decoding: Set by libavcodec
-     */
-    int nb_samples;
-
-    /**
-     * format of the frame, -1 if unknown or unset
-     * Values correspond to enum AVPixelFormat for video frames,
-     * enum AVSampleFormat for audio)
-     * - encoding: unused
-     * - decoding: Read by user.
-     */
-    int format;
-
-    /**
-     * 1 -> keyframe, 0-> not
-     * - encoding: Set by libavcodec.
-     * - decoding: Set by libavcodec.
-     */
-    int key_frame;
-
-    /**
-     * Picture type of the frame, see ?_TYPE below.
-     * - encoding: Set by libavcodec. for coded_picture (and set by user for input).
-     * - decoding: Set by libavcodec.
-     */
-    enum AVPictureType pict_type;
-
-    /**
-     * pointer to the first allocated byte of the picture. Can be used in get_buffer/release_buffer.
-     * This isn't used by libavcodec unless the default get/release_buffer() is used.
-     * - encoding:
-     * - decoding:
-     */
-    uint8_t *base[AV_NUM_DATA_POINTERS];
-
-    /**
-     * sample aspect ratio for the video frame, 0/1 if unknown/unspecified
-     * - encoding: unused
-     * - decoding: Read by user.
-     */
-    AVRational sample_aspect_ratio;
-
-    /**
-     * presentation timestamp in time_base units (time when frame should be shown to user)
-     * If AV_NOPTS_VALUE then frame_rate = 1/time_base will be assumed.
-     * - encoding: MUST be set by user.
-     * - decoding: Set by libavcodec.
-     */
-    int64_t pts;
-
-    /**
-     * pts copied from the AVPacket that was decoded to produce this frame
-     * - encoding: unused
-     * - decoding: Read by user.
-     */
-    int64_t pkt_pts;
-
-    /**
-     * dts copied from the AVPacket that triggered returning this frame
-     * - encoding: unused
-     * - decoding: Read by user.
-     */
-    int64_t pkt_dts;
-
-    /**
-     * picture number in bitstream order
-     * - encoding: set by
-     * - decoding: Set by libavcodec.
-     */
-    int coded_picture_number;
-    /**
-     * picture number in display order
-     * - encoding: set by
-     * - decoding: Set by libavcodec.
-     */
-    int display_picture_number;
-
-    /**
-     * quality (between 1 (good) and FF_LAMBDA_MAX (bad))
-     * - encoding: Set by libavcodec. for coded_picture (and set by user for input).
-     * - decoding: Set by libavcodec.
-     */
-    int quality;
-
-    /**
-     * is this picture used as reference
-     * The values for this are the same as the MpegEncContext.picture_structure
-     * variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
-     * Set to 4 for delayed, non-reference frames.
-     * - encoding: unused
-     * - decoding: Set by libavcodec. (before get_buffer() call)).
-     */
-    int reference;
-
-    /**
-     * QP table
-     * - encoding: unused
-     * - decoding: Set by libavcodec.
-     */
-    int8_t *qscale_table;
-    /**
-     * QP store stride
-     * - encoding: unused
-     * - decoding: Set by libavcodec.
-     */
-    int qstride;
-
-    /**
-     *
-     */
-    int qscale_type;
-
-    /**
-     * mbskip_table[mb]>=1 if MB didn't change
-     * stride= mb_width = (width+15)>>4
-     * - encoding: unused
-     * - decoding: Set by libavcodec.
-     */
-    uint8_t *mbskip_table;
-
-    /**
-     * motion vector table
-     * @code
-     * example:
-     * int mv_sample_log2= 4 - motion_subsample_log2;
-     * int mb_width= (width+15)>>4;
-     * int mv_stride= (mb_width << mv_sample_log2) + 1;
-     * motion_val[direction][x + y*mv_stride][0->mv_x, 1->mv_y];
-     * @endcode
-     * - encoding: Set by user.
-     * - decoding: Set by libavcodec.
-     */
-    int16_t (*motion_val[2])[2];
-
-    /**
-     * macroblock type table
-     * mb_type_base + mb_width + 2
-     * - encoding: Set by user.
-     * - decoding: Set by libavcodec.
-     */
-    uint32_t *mb_type;
-
-    /**
-     * DCT coefficients
-     * - encoding: unused
-     * - decoding: Set by libavcodec.
-     */
-    short *dct_coeff;
-
-    /**
-     * motion reference frame index
-     * the order in which these are stored can depend on the codec.
-     * - encoding: Set by user.
-     * - decoding: Set by libavcodec.
-     */
-    int8_t *ref_index[2];
-
-    /**
-     * for some private data of the user
-     * - encoding: unused
-     * - decoding: Set by user.
-     */
-    void *opaque;
-
-    /**
-     * error
-     * - encoding: Set by libavcodec. if flags&CODEC_FLAG_PSNR.
-     * - decoding: unused
-     */
-    uint64_t error[AV_NUM_DATA_POINTERS];
-
-    /**
-     * type of the buffer (to keep track of who has to deallocate data[*])
-     * - encoding: Set by the one who allocates it.
-     * - decoding: Set by the one who allocates it.
-     * Note: User allocated (direct rendering) & internal buffers cannot coexist currently.
-     */
-    int type;
-
-    /**
-     * When decoding, this signals how much the picture must be delayed.
-     * extra_delay = repeat_pict / (2*fps)
-     * - encoding: unused
-     * - decoding: Set by libavcodec.
-     */
-    int repeat_pict;
-
-    /**
-     * The content of the picture is interlaced.
-     * - encoding: Set by user.
-     * - decoding: Set by libavcodec. (default 0)
-     */
-    int interlaced_frame;
-
-    /**
-     * If the content is interlaced, is top field displayed first.
-     * - encoding: Set by user.
-     * - decoding: Set by libavcodec.
-     */
-    int top_field_first;
-
-    /**
-     * Tell user application that palette has changed from previous frame.
-     * - encoding: ??? (no palette-enabled encoder yet)
-     * - decoding: Set by libavcodec. (default 0).
-     */
-    int palette_has_changed;
-
-    /**
-     * codec suggestion on buffer type if != 0
-     * - encoding: unused
-     * - decoding: Set by libavcodec. (before get_buffer() call)).
-     */
-    int buffer_hints;
-
-    /**
-     * Pan scan.
-     * - encoding: Set by user.
-     * - decoding: Set by libavcodec.
-     */
-    AVPanScan *pan_scan;
-
-    /**
-     * reordered opaque 64bit (generally an integer or a double precision float
-     * PTS but can be anything).
-     * The user sets AVCodecContext.reordered_opaque to represent the input at
-     * that time,
-     * the decoder reorders values as needed and sets AVFrame.reordered_opaque
-     * to exactly one of the values provided by the user through AVCodecContext.reordered_opaque
-     * @deprecated in favor of pkt_pts
-     * - encoding: unused
-     * - decoding: Read by user.
-     */
-    int64_t reordered_opaque;
-
-    /**
-     * hardware accelerator private data (FFmpeg-allocated)
-     * - encoding: unused
-     * - decoding: Set by libavcodec
-     */
-    void *hwaccel_picture_private;
-
-    /**
-     * the AVCodecContext which ff_thread_get_buffer() was last called on
-     * - encoding: Set by libavcodec.
-     * - decoding: Set by libavcodec.
-     */
-    struct AVCodecContext *owner;
-
-    /**
-     * used by multithreading to store frame-specific info
-     * - encoding: Set by libavcodec.
-     * - decoding: Set by libavcodec.
-     */
-    void *thread_opaque;
-
-    /**
-     * log2 of the size of the block which a single vector in motion_val represents:
-     * (4->16x16, 3->8x8, 2-> 4x4, 1-> 2x2)
-     * - encoding: unused
-     * - decoding: Set by libavcodec.
-     */
-    uint8_t motion_subsample_log2;
-
-    /**
-     * Sample rate of the audio data.
-     *
-     * - encoding: unused
-     * - decoding: read by user
-     */
-    int sample_rate;
-
-    /**
-     * Channel layout of the audio data.
-     *
-     * - encoding: unused
-     * - decoding: read by user.
-     */
-    uint64_t channel_layout;
-
-    /**
-     * frame timestamp estimated using various heuristics, in stream time base
-     * Code outside libavcodec should access this field using:
-     * av_frame_get_best_effort_timestamp(frame)
-     * - encoding: unused
-     * - decoding: set by libavcodec, read by user.
-     */
-    int64_t best_effort_timestamp;
-
-    /**
-     * reordered pos from the last AVPacket that has been input into the decoder
-     * Code outside libavcodec should access this field using:
-     * av_frame_get_pkt_pos(frame)
-     * - encoding: unused
-     * - decoding: Read by user.
-     */
-    int64_t pkt_pos;
-
-    /**
-     * duration of the corresponding packet, expressed in
-     * AVStream->time_base units, 0 if unknown.
-     * Code outside libavcodec should access this field using:
-     * av_frame_get_pkt_duration(frame)
-     * - encoding: unused
-     * - decoding: Read by user.
-     */
-    int64_t pkt_duration;
-
-    /**
-     * metadata.
-     * Code outside libavcodec should access this field using:
-     * av_frame_get_metadata(frame)
-     * - encoding: Set by user.
-     * - decoding: Set by libavcodec.
-     */
-    AVDictionary *metadata;
-
-    /**
-     * decode error flags of the frame, set to a combination of
-     * FF_DECODE_ERROR_xxx flags if the decoder produced a frame, but there
-     * were errors during the decoding.
-     * Code outside libavcodec should access this field using:
-     * av_frame_get_decode_error_flags(frame)
-     * - encoding: unused
-     * - decoding: set by libavcodec, read by user.
-     */
-    int decode_error_flags;
-#define FF_DECODE_ERROR_INVALID_BITSTREAM   1
-#define FF_DECODE_ERROR_MISSING_REFERENCE   2
-
-    /**
-     * number of audio channels, only used for audio.
-     * Code outside libavcodec should access this field using:
-     * av_frame_get_channels(frame)
-     * - encoding: unused
-     * - decoding: Read by user.
-     */
-    int channels;
-
-    /**
-     * size of the corresponding packet containing the compressed
-     * frame. It must be accessed using av_frame_get_pkt_size() and
-     * av_frame_set_pkt_size().
-     * It is set to a negative value if unknown.
-     * - encoding: unused
-     * - decoding: set by libavcodec, read by user.
-     */
-    int pkt_size;
-} AVFrame;
-
-/**
- * Accessors for some AVFrame fields.
- * The position of these field in the structure is not part of the ABI,
- * they should not be accessed directly outside libavcodec.
- */
-int64_t av_frame_get_best_effort_timestamp(const AVFrame *frame);
-void    av_frame_set_best_effort_timestamp(AVFrame *frame, int64_t val);
-int64_t av_frame_get_pkt_duration         (const AVFrame *frame);
-void    av_frame_set_pkt_duration         (AVFrame *frame, int64_t val);
-int64_t av_frame_get_pkt_pos              (const AVFrame *frame);
-void    av_frame_set_pkt_pos              (AVFrame *frame, int64_t val);
-int64_t av_frame_get_channel_layout       (const AVFrame *frame);
-void    av_frame_set_channel_layout       (AVFrame *frame, int64_t val);
-int     av_frame_get_channels             (const AVFrame *frame);
-void    av_frame_set_channels             (AVFrame *frame, int     val);
-int     av_frame_get_sample_rate          (const AVFrame *frame);
-void    av_frame_set_sample_rate          (AVFrame *frame, int     val);
-AVDictionary *av_frame_get_metadata       (const AVFrame *frame);
-void          av_frame_set_metadata       (AVFrame *frame, AVDictionary *val);
-int     av_frame_get_decode_error_flags   (const AVFrame *frame);
-void    av_frame_set_decode_error_flags   (AVFrame *frame, int     val);
-int     av_frame_get_pkt_size(const AVFrame *frame);
-void    av_frame_set_pkt_size(AVFrame *frame, int val);
-
 struct AVCodecInternal;
 
 enum AVFieldOrder {
@@ -1594,13 +1167,6 @@
      */
     unsigned int stream_codec_tag;
 
-#if FF_API_SUB_ID
-    /**
-     * @deprecated this field is unused
-     */
-    attribute_deprecated int sub_id;
-#endif
-
     void *priv_data;
 
     /**
@@ -1728,16 +1294,20 @@
     /**
      * picture width / height.
      * - encoding: MUST be set by user.
-     * - decoding: Set by libavcodec.
-     * Note: For compatibility it is possible to set this instead of
-     * coded_width/height before decoding.
+     * - decoding: May be set by the user before opening the decoder if known e.g.
+     *             from the container. Some decoders will require the dimensions
+     *             to be set by the caller. During decoding, the decoder may
+     *             overwrite those values as required.
      */
     int width, height;
 
     /**
-     * Bitstream width / height, may be different from width/height if lowres enabled.
+     * Bitstream width / height, may be different from width/height e.g. when
+     * the decoded frame is cropped before being output or lowres is enabled.
      * - encoding: unused
-     * - decoding: Set by user before init if known. Codec should override / dynamically change if needed.
+     * - decoding: May be set by the user before opening the decoder if known
+     *             e.g. from the container. During decoding, the decoder may
+     *             overwrite those values as required.
      */
     int coded_width, coded_height;
 
@@ -1829,22 +1399,6 @@
 
     int b_frame_strategy;
 
-#if FF_API_MPV_GLOBAL_OPTS
-    /**
-     * luma single coefficient elimination threshold
-     * - encoding: Set by user.
-     * - decoding: unused
-     */
-    attribute_deprecated int luma_elim_threshold;
-
-    /**
-     * chroma single coeff elimination threshold
-     * - encoding: Set by user.
-     * - decoding: unused
-     */
-    attribute_deprecated int chroma_elim_threshold;
-#endif
-
     /**
      * qscale offset between IP and B-frames
      * - encoding: Set by user.
@@ -2074,16 +1628,6 @@
      */
     int inter_quant_bias;
 
-#if FF_API_COLOR_TABLE_ID
-    /**
-     * color table ID
-     * - encoding: unused
-     * - decoding: Which clrtable should be used for 8bit RGB images.
-     *             Tables have to be stored somewhere. FIXME
-     */
-    attribute_deprecated int color_table_id;
-#endif
-
     /**
      * slice flags
      * - encoding: unused
@@ -2140,20 +1684,6 @@
      */
     int noise_reduction;
 
-#if FF_API_INTER_THRESHOLD
-    /**
-     * @deprecated this field is unused
-     */
-    attribute_deprecated int inter_threshold;
-#endif
-
-#if FF_API_MPV_GLOBAL_OPTS
-    /**
-     * @deprecated use mpegvideo private options instead
-     */
-    attribute_deprecated int quantizer_noise_shaping;
-#endif
-
     /**
      * Motion estimation threshold below which no motion estimation is
      * performed, but instead the user specified motion vectors are used.
@@ -2413,6 +1943,7 @@
      */
     enum AVSampleFormat request_sample_fmt;
 
+#if FF_API_GET_BUFFER
     /**
      * Called at the beginning of each frame to get a buffer for it.
      *
@@ -2472,7 +2003,10 @@
      *
      * - encoding: unused
      * - decoding: Set by libavcodec, user can override.
+     *
+     * @deprecated use get_buffer2()
      */
+    attribute_deprecated
     int (*get_buffer)(struct AVCodecContext *c, AVFrame *pic);
 
     /**
@@ -2483,7 +2017,10 @@
      * but not by more than one thread at once, so does not need to be reentrant.
      * - encoding: unused
      * - decoding: Set by libavcodec, user can override.
+     *
+     * @deprecated custom freeing callbacks should be set from get_buffer2()
      */
+    attribute_deprecated
     void (*release_buffer)(struct AVCodecContext *c, AVFrame *pic);
 
     /**
@@ -2498,8 +2035,100 @@
      * - encoding: unused
      * - decoding: Set by libavcodec, user can override.
      */
+    attribute_deprecated
     int (*reget_buffer)(struct AVCodecContext *c, AVFrame *pic);
+#endif
 
+    /**
+     * This callback is called at the beginning of each frame to get data
+     * buffer(s) for it. There may be one contiguous buffer for all the data or
+     * there may be a buffer per each data plane or anything in between. Each
+     * buffer must be reference-counted using the AVBuffer API.
+     *
+     * The following fields will be set in the frame before this callback is
+     * called:
+     * - format
+     * - width, height (video only)
+     * - sample_rate, channel_layout, nb_samples (audio only)
+     * Their values may differ from the corresponding values in
+     * AVCodecContext. This callback must use the frame values, not the codec
+     * context values, to calculate the required buffer size.
+     *
+     * This callback must fill the following fields in the frame:
+     * - data[]
+     * - linesize[]
+     * - extended_data:
+     *   * if the data is planar audio with more than 8 channels, then this
+     *     callback must allocate and fill extended_data to contain all pointers
+     *     to all data planes. data[] must hold as many pointers as it can.
+     *     extended_data must be allocated with av_malloc() and will be freed in
+     *     av_frame_unref().
+     *   * otherwise exended_data must point to data
+     * - buf[] must contain references to the buffers that contain the frame
+     *   data.
+     * - extended_buf and nb_extended_buf must be allocated with av_malloc() by
+     *   this callback and filled with the extra buffers if there are more
+     *   buffers than buf[] can hold. extended_buf will be freed in
+     *   av_frame_unref().
+     *
+     * If CODEC_CAP_DR1 is not set then get_buffer2() must call
+     * avcodec_default_get_buffer2() instead of providing buffers allocated by
+     * some other means.
+     *
+     * Each data plane must be aligned to the maximum required by the target
+     * CPU.
+     *
+     * @see avcodec_default_get_buffer2()
+     *
+     * Video:
+     *
+     * If AV_GET_BUFFER_FLAG_REF is set in flags then the frame may be reused
+     * (read and/or written to if it is writable) later by libavcodec.
+     *
+     * If CODEC_FLAG_EMU_EDGE is not set in s->flags, the buffer must contain an
+     * edge of the size returned by avcodec_get_edge_width() on all sides.
+     *
+     * avcodec_align_dimensions2() should be used to find the required width and
+     * height, as they normally need to be rounded up to the next multiple of 16.
+     *
+     * If frame multithreading is used and thread_safe_callbacks is set,
+     * this callback may be called from a different thread, but not from more
+     * than one at once. Does not need to be reentrant.
+     *
+     * @see avcodec_align_dimensions2()
+     *
+     * Audio:
+     *
+     * Decoders request a buffer of a particular size by setting
+     * AVFrame.nb_samples prior to calling get_buffer2(). The decoder may,
+     * however, utilize only part of the buffer by setting AVFrame.nb_samples
+     * to a smaller value in the output frame.
+     *
+     * As a convenience, av_samples_get_buffer_size() and
+     * av_samples_fill_arrays() in libavutil may be used by custom get_buffer2()
+     * functions to find the required data size and to fill data pointers and
+     * linesize. In AVFrame.linesize, only linesize[0] may be set for audio
+     * since all planes must be the same size.
+     *
+     * @see av_samples_get_buffer_size(), av_samples_fill_arrays()
+     *
+     * - encoding: unused
+     * - decoding: Set by libavcodec, user can override.
+     */
+    int (*get_buffer2)(struct AVCodecContext *s, AVFrame *frame, int flags);
+
+    /**
+     * If non-zero, the decoded audio and video frames returned from
+     * avcodec_decode_video2() and avcodec_decode_audio4() are reference-counted
+     * and are valid indefinitely. The caller must free them with
+     * av_frame_unref() when they are not needed anymore.
+     * Otherwise, the decoded frames must not be freed by the caller and are
+     * only valid until the next decode call.
+     *
+     * - encoding: unused
+     * - decoding: set by the caller before avcodec_open2().
+     */
+    int refcounted_frames;
 
     /* - encoding parameters */
     float qcompress;  ///< amount of qscale change between easy & hard scenes (0.0-1.0)
@@ -2896,10 +2525,6 @@
 #define FF_IDCT_INT           1
 #define FF_IDCT_SIMPLE        2
 #define FF_IDCT_SIMPLEMMX     3
-#define FF_IDCT_LIBMPEG2MMX   4
-#if FF_API_MMI
-#define FF_IDCT_MMI           5
-#endif
 #define FF_IDCT_ARM           7
 #define FF_IDCT_ALTIVEC       8
 #define FF_IDCT_SH4           9
@@ -2912,22 +2537,6 @@
 #define FF_IDCT_FAAN          20
 #define FF_IDCT_SIMPLENEON    22
 #define FF_IDCT_SIMPLEALPHA   23
-#if FF_API_IDCT
-#define FF_IDCT_H264          11
-#define FF_IDCT_VP3           12
-#define FF_IDCT_CAVS          15
-#define FF_IDCT_WMV2          19
-#define FF_IDCT_EA            21
-#define FF_IDCT_BINK          24
-#endif
-
-#if FF_API_DSP_MASK
-    /**
-     * Unused.
-     * @deprecated use av_set_cpu_flags_mask() instead.
-     */
-    attribute_deprecated unsigned dsp_mask;
-#endif
 
     /**
      * bits per sample/pixel from the demuxer (needed for huffyuv).
@@ -3110,6 +2719,12 @@
 #define FF_PROFILE_MPEG4_SIMPLE_STUDIO             14
 #define FF_PROFILE_MPEG4_ADVANCED_SIMPLE           15
 
+#define FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0   0
+#define FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1   1
+#define FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION  2
+#define FF_PROFILE_JPEG2000_DCINEMA_2K              3
+#define FF_PROFILE_JPEG2000_DCINEMA_4K              4
+
     /**
      * level
      * - encoding: Set by user.
@@ -3119,21 +2734,21 @@
 #define FF_LEVEL_UNKNOWN -99
 
     /**
-     *
+     * Skip loop filtering for selected frames.
      * - encoding: unused
      * - decoding: Set by user.
      */
     enum AVDiscard skip_loop_filter;
 
     /**
-     *
+     * Skip IDCT/dequantization for selected frames.
      * - encoding: unused
      * - decoding: Set by user.
      */
     enum AVDiscard skip_idct;
 
     /**
-     *
+     * Skip decoding for selected frames.
      * - encoding: unused
      * - decoding: Set by user.
      */
@@ -3203,13 +2818,6 @@
     int64_t pts_correction_last_dts;       /// DTS of the last frame
 
     /**
-     * Current frame metadata.
-     * - decoding: maintained and used by libavcodec, not intended to be used by user apps
-     * - encoding: unused
-     */
-    AVDictionary *metadata;
-
-    /**
      * Character encoding of the input subtitles file.
      * - decoding: set by user
      * - encoding: unused
@@ -3776,10 +3384,14 @@
  * @{
  */
 
+#if FF_API_DESTRUCT_PACKET
 /**
  * Default packet destructor.
+ * @deprecated use the AVBuffer API instead
  */
+attribute_deprecated
 void av_destruct_packet(AVPacket *pkt);
+#endif
 
 /**
  * Initialize optional fields of a packet with default values.
@@ -3818,6 +3430,21 @@
 int av_grow_packet(AVPacket *pkt, int grow_by);
 
 /**
+ * Initialize a reference-counted packet from av_malloc()ed data.
+ *
+ * @param pkt packet to be initialized. This function will set the data, size,
+ *        buf and destruct fields, all others are left untouched.
+ * @param data Data allocated by av_malloc() to be used as packet data. If this
+ *        function returns successfully, the data is owned by the underlying AVBuffer.
+ *        The caller may not access the data through other means.
+ * @param size size of data in bytes, without the padding. I.e. the full buffer
+ *        size is assumed to be size + FF_INPUT_BUFFER_PADDING_SIZE.
+ *
+ * @return 0 on success, a negative AVERROR on error
+ */
+int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size);
+
+/**
  * @warning This is a hack - the packet memory allocation stuff is broken. The
  * packet is allocated if it was not really allocated.
  */
@@ -3900,9 +3527,18 @@
  */
 AVCodec *avcodec_find_decoder_by_name(const char *name);
 
-int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic);
-void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic);
-int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic);
+#if FF_API_GET_BUFFER
+attribute_deprecated int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic);
+attribute_deprecated void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic);
+attribute_deprecated int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic);
+#endif
+
+/**
+ * The default callback for AVCodecContext.get_buffer2(). It is made public so
+ * it can be called by custom get_buffer2() implementations for decoders without
+ * CODEC_CAP_DR1 set.
+ */
+int avcodec_default_get_buffer2(AVCodecContext *s, AVFrame *frame, int flags);
 
 /**
  * Return the amount of padding in pixels which the get_buffer callback must
@@ -4015,11 +3651,17 @@
  *
  * @param      avctx the codec context
  * @param[out] frame The AVFrame in which to store decoded audio samples.
- *                   Decoders request a buffer of a particular size by setting
- *                   AVFrame.nb_samples prior to calling get_buffer(). The
- *                   decoder may, however, only utilize part of the buffer by
- *                   setting AVFrame.nb_samples to a smaller value in the
- *                   output frame.
+ *                   The decoder will allocate a buffer for the decoded frame by
+ *                   calling the AVCodecContext.get_buffer2() callback.
+ *                   When AVCodecContext.refcounted_frames is set to 1, the frame is
+ *                   reference counted and the returned reference belongs to the
+ *                   caller. The caller must release the frame using av_frame_unref()
+ *                   when the frame is no longer needed. The caller may safely write
+ *                   to the frame if av_frame_is_writable() returns 1.
+ *                   When AVCodecContext.refcounted_frames is set to 0, the returned
+ *                   reference belongs to the decoder and is valid only until the
+ *                   next call to this function or until closing the decoder.
+ *                   The caller may not write to it.
  * @param[out] got_frame_ptr Zero if no frame could be decoded, otherwise it is
  *                           non-zero.
  * @param[in]  avpkt The input AVPacket containing the input buffer.
@@ -4057,12 +3699,18 @@
  *
  * @param avctx the codec context
  * @param[out] picture The AVFrame in which the decoded video frame will be stored.
- *             Use avcodec_alloc_frame to get an AVFrame, the codec will
- *             allocate memory for the actual bitmap.
- *             with default get/release_buffer(), the decoder frees/reuses the bitmap as it sees fit.
- *             with overridden get/release_buffer() (needs CODEC_CAP_DR1) the user decides into what buffer the decoder
- *                   decodes and the decoder tells the user once it does not need the data anymore,
- *                   the user app can at this point free/reuse/keep the memory as it sees fit.
+ *             Use av_frame_alloc() to get an AVFrame. The codec will
+ *             allocate memory for the actual bitmap by calling the
+ *             AVCodecContext.get_buffer2() callback.
+ *             When AVCodecContext.refcounted_frames is set to 1, the frame is
+ *             reference counted and the returned reference belongs to the
+ *             caller. The caller must release the frame using av_frame_unref()
+ *             when the frame is no longer needed. The caller may safely write
+ *             to the frame if av_frame_is_writable() returns 1.
+ *             When AVCodecContext.refcounted_frames is set to 0, the returned
+ *             reference belongs to the decoder and is valid only until the
+ *             next call to this function or until closing the decoder. The
+ *             caller may not write to it.
  *
  * @param[in] avpkt The input AVpacket containing the input buffer.
  *            You can create such packet with av_init_packet() and by then setting
@@ -4293,7 +3941,7 @@
 
 /**
  * @return 0 if the output buffer is a subset of the input, 1 if it is allocated and must be freed
- * @deprecated use AVBitstreamFilter
+ * @deprecated use AVBitStreamFilter
  */
 int av_parser_change(AVCodecParserContext *s,
                      AVCodecContext *avctx,
@@ -4616,11 +4264,16 @@
  */
 int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height);
 
+#if FF_API_DEINTERLACE
 /**
  *  deinterlace - if not supported return -1
+ *
+ * @deprecated - use yadif (in libavfilter) instead
  */
+attribute_deprecated
 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
                           enum AVPixelFormat pix_fmt, int width, int height);
+#endif
 /**
  * Copy image src to dst. Wraps av_image_copy().
  */
@@ -4668,8 +4321,8 @@
  * pix_fmts.
  *
  * @param[in]  pix_fmt the pixel format
- * @param[out] h_shift store log2_chroma_h
- * @param[out] v_shift store log2_chroma_w
+ * @param[out] h_shift store log2_chroma_w
+ * @param[out] v_shift store log2_chroma_h
  *
  * @see av_pix_fmt_get_chroma_sub_sample
  */
@@ -4711,38 +4364,6 @@
 int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt, enum AVPixelFormat src_pix_fmt,
                              int has_alpha);
 
-#if FF_API_FIND_BEST_PIX_FMT
-/**
- * @deprecated use avcodec_find_best_pix_fmt_of_2() instead.
- *
- * Find the best pixel format to convert to given a certain source pixel
- * format.  When converting from one pixel format to another, information loss
- * may occur.  For example, when converting from RGB24 to GRAY, the color
- * information will be lost. Similarly, other losses occur when converting from
- * some formats to other formats. avcodec_find_best_pix_fmt() searches which of
- * the given pixel formats should be used to suffer the least amount of loss.
- * The pixel formats from which it chooses one, are determined by the
- * pix_fmt_mask parameter.
- *
- * Note, only the first 64 pixel formats will fit in pix_fmt_mask.
- *
- * @code
- * src_pix_fmt = AV_PIX_FMT_YUV420P;
- * pix_fmt_mask = (1 << AV_PIX_FMT_YUV422P) | (1 << AV_PIX_FMT_RGB24);
- * dst_pix_fmt = avcodec_find_best_pix_fmt(pix_fmt_mask, src_pix_fmt, alpha, &loss);
- * @endcode
- *
- * @param[in] pix_fmt_mask bitmask determining which pixel format to choose from
- * @param[in] src_pix_fmt source pixel format
- * @param[in] has_alpha Whether the source pixel format alpha channel is used.
- * @param[out] loss_ptr Combination of flags informing you what kind of losses will occur.
- * @return The best pixel format to convert to or -1 if none was found.
- */
-attribute_deprecated
-enum AVPixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum AVPixelFormat src_pix_fmt,
-                              int has_alpha, int *loss_ptr);
-#endif /* FF_API_FIND_BEST_PIX_FMT */
-
 /**
  * Find the best pixel format to convert to given a certain source pixel
  * format.  When converting from one pixel format to another, information loss
@@ -4872,8 +4493,6 @@
  */
 void avcodec_flush_buffers(AVCodecContext *avctx);
 
-void avcodec_default_free_buffers(AVCodecContext *s);
-
 /**
  * Return codec bits per sample.
  *
@@ -4987,6 +4606,7 @@
  */
 unsigned int av_xiphlacing(unsigned char *s, unsigned int v);
 
+#if FF_API_MISSING_SAMPLE
 /**
  * Log a generic warning message about a missing feature. This function is
  * intended to be used internally by FFmpeg (libavcodec, libavformat, etc.)
@@ -4998,7 +4618,9 @@
  * If want_sample is non-zero, additional verbage will be added to the log
  * message which tells the user how to report samples to the development
  * mailing list.
+ * @deprecated Use avpriv_report_missing_feature() instead.
  */
+attribute_deprecated
 void av_log_missing_feature(void *avc, const char *feature, int want_sample);
 
 /**
@@ -5008,8 +4630,11 @@
  * @param[in] avc a pointer to an arbitrary struct of which the first field is
  * a pointer to an AVClass struct
  * @param[in] msg string containing an optional message, or NULL if no message
+ * @deprecated Use avpriv_request_sample() instead.
  */
+attribute_deprecated
 void av_log_ask_for_sample(void *avc, const char *msg, ...) av_printf_format(2, 3);
+#endif /* FF_API_MISSING_SAMPLE */
 
 /**
  * Register the hardware accelerator hwaccel.
diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c
index dea72e5..651036e 100644
--- a/libavcodec/avpacket.c
+++ b/libavcodec/avpacket.c
@@ -22,6 +22,7 @@
 #include <string.h>
 
 #include "libavutil/avassert.h"
+#include "libavutil/common.h"
 #include "libavutil/mem.h"
 #include "avcodec.h"
 #include "bytestream.h"
@@ -36,6 +37,7 @@
     pkt->side_data_elems = 0;
 }
 
+#if FF_API_DESTRUCT_PACKET
 void av_destruct_packet(AVPacket *pkt)
 {
     av_free(pkt->data);
@@ -43,6 +45,14 @@
     pkt->size = 0;
 }
 
+/* a dummy destruct callback for the callers that assume AVPacket.destruct ==
+ * NULL => static data */
+static void dummy_destruct_packet(AVPacket *pkt)
+{
+    av_assert0(0);
+}
+#endif
+
 void av_init_packet(AVPacket *pkt)
 {
     pkt->pts                  = AV_NOPTS_VALUE;
@@ -52,27 +62,35 @@
     pkt->convergence_duration = 0;
     pkt->flags                = 0;
     pkt->stream_index         = 0;
+#if FF_API_DESTRUCT_PACKET
     pkt->destruct             = NULL;
+#endif
+    pkt->buf                  = NULL;
     pkt->side_data            = NULL;
     pkt->side_data_elems      = 0;
 }
 
 int av_new_packet(AVPacket *pkt, int size)
 {
-    uint8_t *data = NULL;
-    if ((unsigned)size < (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
-        data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
-    if (data) {
-        memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
-    } else
-        size = 0;
+    AVBufferRef *buf = NULL;
+
+    if ((unsigned)size >= (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
+        return AVERROR(EINVAL);
+
+    av_buffer_realloc(&buf, size + FF_INPUT_BUFFER_PADDING_SIZE);
+    if (!buf)
+        return AVERROR(ENOMEM);
+
+    memset(buf->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
 
     av_init_packet(pkt);
-    pkt->data     = data;
+    pkt->buf      = buf;
+    pkt->data     = buf->data;
     pkt->size     = size;
-    pkt->destruct = av_destruct_packet;
-    if (!data)
-        return AVERROR(ENOMEM);
+#if FF_API_DESTRUCT_PACKET
+    pkt->destruct = dummy_destruct_packet;
+#endif
+
     return 0;
 }
 
@@ -86,33 +104,71 @@
 
 int av_grow_packet(AVPacket *pkt, int grow_by)
 {
-    void *new_ptr;
+    int new_size;
     av_assert0((unsigned)pkt->size <= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
     if (!pkt->size)
         return av_new_packet(pkt, grow_by);
     if ((unsigned)grow_by >
         INT_MAX - (pkt->size + FF_INPUT_BUFFER_PADDING_SIZE))
         return -1;
-    new_ptr = av_realloc(pkt->data,
-                         pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE);
-    if (!new_ptr)
-        return AVERROR(ENOMEM);
-    pkt->data  = new_ptr;
+
+    new_size = pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE;
+    if (pkt->buf) {
+        int ret = av_buffer_realloc(&pkt->buf, new_size);
+        if (ret < 0)
+            return ret;
+    } else {
+        pkt->buf = av_buffer_alloc(new_size);
+        if (!pkt->buf)
+            return AVERROR(ENOMEM);
+        memcpy(pkt->buf->data, pkt->data, FFMIN(pkt->size, pkt->size + grow_by));
+#if FF_API_DESTRUCT_PACKET
+        pkt->destruct = dummy_destruct_packet;
+#endif
+    }
+    pkt->data  = pkt->buf->data;
     pkt->size += grow_by;
     memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
+
     return 0;
 }
 
-#define DUP_DATA(dst, src, size, padding)                               \
+int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
+{
+    if (size >= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
+        return AVERROR(EINVAL);
+
+    pkt->buf = av_buffer_create(data, size + FF_INPUT_BUFFER_PADDING_SIZE,
+                                av_buffer_default_free, NULL, 0);
+    if (!pkt->buf)
+        return AVERROR(ENOMEM);
+
+    pkt->data = data;
+    pkt->size = size;
+#if FF_API_DESTRUCT_PACKET
+    pkt->destruct = dummy_destruct_packet;
+#endif
+
+    return 0;
+}
+
+#define ALLOC_MALLOC(data, size) data = av_malloc(size)
+#define ALLOC_BUF(data, size)                \
+do {                                         \
+    av_buffer_realloc(&pkt->buf, size);      \
+    data = pkt->buf ? pkt->buf->data : NULL; \
+} while (0)
+
+#define DUP_DATA(dst, src, size, padding, ALLOC)                        \
     do {                                                                \
         void *data;                                                     \
         if (padding) {                                                  \
             if ((unsigned)(size) >                                      \
                 (unsigned)(size) + FF_INPUT_BUFFER_PADDING_SIZE)        \
                 goto failed_alloc;                                      \
-            data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);      \
+            ALLOC(data, size + FF_INPUT_BUFFER_PADDING_SIZE);           \
         } else {                                                        \
-            data = av_malloc(size);                                     \
+            ALLOC(data, size);                                          \
         }                                                               \
         if (!data)                                                      \
             goto failed_alloc;                                          \
@@ -124,31 +180,41 @@
     } while (0)
 
 /* Makes duplicates of data, side_data, but does not copy any other fields */
-static int copy_packet_data(AVPacket *dst, AVPacket *src)
+static int copy_packet_data(AVPacket *pkt, AVPacket *src)
 {
-    dst->data      = NULL;
-    dst->side_data = NULL;
-    DUP_DATA(dst->data, src->data, dst->size, 1);
-    dst->destruct = av_destruct_packet;
+    pkt->data      = NULL;
+    pkt->side_data = NULL;
+    if (pkt->buf) {
+        AVBufferRef *ref = av_buffer_ref(src->buf);
+        if (!ref)
+            return AVERROR(ENOMEM);
+        pkt->buf  = ref;
+        pkt->data = ref->data;
+    } else {
+        DUP_DATA(pkt->data, src->data, pkt->size, 1, ALLOC_BUF);
+    }
+#if FF_API_DESTRUCT_PACKET
+    pkt->destruct = dummy_destruct_packet;
+#endif
 
-    if (dst->side_data_elems) {
+    if (pkt->side_data_elems) {
         int i;
 
-        DUP_DATA(dst->side_data, src->side_data,
-                dst->side_data_elems * sizeof(*dst->side_data), 0);
-        memset(dst->side_data, 0,
-                dst->side_data_elems * sizeof(*dst->side_data));
-        for (i = 0; i < dst->side_data_elems; i++) {
-            DUP_DATA(dst->side_data[i].data, src->side_data[i].data,
-                    src->side_data[i].size, 1);
-            dst->side_data[i].size = src->side_data[i].size;
-            dst->side_data[i].type = src->side_data[i].type;
+        DUP_DATA(pkt->side_data, src->side_data,
+                pkt->side_data_elems * sizeof(*pkt->side_data), 0, ALLOC_MALLOC);
+        memset(pkt->side_data, 0,
+                pkt->side_data_elems * sizeof(*pkt->side_data));
+        for (i = 0; i < pkt->side_data_elems; i++) {
+            DUP_DATA(pkt->side_data[i].data, src->side_data[i].data,
+                    src->side_data[i].size, 1, ALLOC_MALLOC);
+            pkt->side_data[i].size = src->side_data[i].size;
+            pkt->side_data[i].type = src->side_data[i].type;
         }
     }
     return 0;
 
 failed_alloc:
-    av_destruct_packet(dst);
+    av_destruct_packet(pkt);
     return AVERROR(ENOMEM);
 }
 
@@ -156,7 +222,11 @@
 {
     AVPacket tmp_pkt;
 
-    if (pkt->destruct == NULL && pkt->data) {
+    if (!pkt->buf && pkt->data
+#if FF_API_DESTRUCT_PACKET
+        && !pkt->destruct
+#endif
+        ) {
         tmp_pkt = *pkt;
         return copy_packet_data(pkt, &tmp_pkt);
     }
@@ -174,8 +244,13 @@
     if (pkt) {
         int i;
 
-        if (pkt->destruct)
+        if (pkt->buf)
+            av_buffer_unref(&pkt->buf);
+#if FF_API_DESTRUCT_PACKET
+        else if (pkt->destruct)
             pkt->destruct(pkt);
+        pkt->destruct = NULL;
+#endif
         pkt->data            = NULL;
         pkt->size            = 0;
 
@@ -230,6 +305,7 @@
 
 int av_packet_merge_side_data(AVPacket *pkt){
     if(pkt->side_data_elems){
+        AVBufferRef *buf;
         int i;
         uint8_t *p;
         uint64_t size= pkt->size + 8LL + FF_INPUT_BUFFER_PADDING_SIZE;
@@ -239,11 +315,14 @@
         }
         if (size > INT_MAX)
             return AVERROR(EINVAL);
-        p = av_malloc(size);
-        if (!p)
+        buf = av_buffer_alloc(size);
+        if (!buf)
             return AVERROR(ENOMEM);
-        pkt->data = p;
-        pkt->destruct = av_destruct_packet;
+        pkt->buf = buf;
+        pkt->data = p = buf->data;
+#if FF_API_DESTRUCT_PACKET
+        pkt->destruct = dummy_destruct_packet;
+#endif
         pkt->size = size - FF_INPUT_BUFFER_PADDING_SIZE;
         bytestream_put_buffer(&p, old.data, old.size);
         for (i=old.side_data_elems-1; i>=0; i--) {
diff --git a/libavcodec/avrndec.c b/libavcodec/avrndec.c
index 0fc431d..40aca17 100644
--- a/libavcodec/avrndec.c
+++ b/libavcodec/avrndec.c
@@ -27,7 +27,6 @@
 
 typedef struct {
     MJpegDecodeContext mjpeg_ctx;
-    AVFrame frame;
     int is_mjpeg;
     int interlace; //FIXME use frame.interlaced_frame
     int tff;
@@ -52,7 +51,6 @@
     if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
         return ret;
 
-    avcodec_get_frame_defaults(&a->frame);
     avctx->pix_fmt = AV_PIX_FMT_UYVY422;
 
     if(avctx->extradata_size >= 9 && avctx->extradata[4]+28 < avctx->extradata_size) {
@@ -69,10 +67,6 @@
 static av_cold int end(AVCodecContext *avctx)
 {
     AVRnContext *a = avctx->priv_data;
-    AVFrame *p = &a->frame;
-
-    if(p->data[0])
-        avctx->release_buffer(avctx, p);
 
     if(a->is_mjpeg)
         ff_mjpeg_decode_end(avctx);
@@ -84,7 +78,7 @@
                         int *got_frame, AVPacket *avpkt)
 {
     AVRnContext *a = avctx->priv_data;
-    AVFrame *p = &a->frame;
+    AVFrame *p = data;
     const uint8_t *buf = avpkt->data;
     int buf_size       = avpkt->size;
     int y, ret, true_height;
@@ -93,18 +87,14 @@
         return ff_mjpeg_decode_frame(avctx, data, got_frame, avpkt);
 
     true_height    = buf_size / (2*avctx->width);
-    if(p->data[0])
-        avctx->release_buffer(avctx, p);
 
     if(buf_size < 2*avctx->width * avctx->height) {
         av_log(avctx, AV_LOG_ERROR, "packet too small\n");
         return AVERROR_INVALIDDATA;
     }
 
-    if((ret = ff_get_buffer(avctx, p)) < 0){
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
         return ret;
-    }
     p->pict_type= AV_PICTURE_TYPE_I;
     p->key_frame= 1;
 
@@ -123,7 +113,6 @@
         }
     }
 
-    *(AVFrame*)data = a->frame;
     *got_frame      = 1;
     return buf_size;
 }
diff --git a/libavcodec/avs.c b/libavcodec/avs.c
index e3d2070..e3733d7 100644
--- a/libavcodec/avs.c
+++ b/libavcodec/avs.c
@@ -21,6 +21,7 @@
 
 #include "avcodec.h"
 #include "get_bits.h"
+#include "internal.h"
 
 
 typedef struct {
@@ -59,11 +60,8 @@
     AvsBlockType type;
     GetBitContext change_map = {0}; //init to silence warning
 
-    if ((ret = avctx->reget_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, p)) < 0)
         return ret;
-    }
-    p->reference = 3;
     p->pict_type = AV_PICTURE_TYPE_P;
     p->key_frame = 0;
 
@@ -151,7 +149,8 @@
             align_get_bits(&change_map);
     }
 
-    *picture   = avs->picture;
+    if ((ret = av_frame_ref(picture, &avs->picture)) < 0)
+        return ret;
     *got_frame = 1;
 
     return buf_size;
@@ -159,18 +158,17 @@
 
 static av_cold int avs_decode_init(AVCodecContext * avctx)
 {
-    AvsContext *const avs = avctx->priv_data;
+    AvsContext *s = avctx->priv_data;
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
-    avcodec_get_frame_defaults(&avs->picture);
     avcodec_set_dimensions(avctx, 318, 198);
+    avcodec_get_frame_defaults(&s->picture);
     return 0;
 }
 
 static av_cold int avs_decode_end(AVCodecContext *avctx)
 {
     AvsContext *s = avctx->priv_data;
-    if (s->picture.data[0])
-        avctx->release_buffer(avctx, &s->picture);
+    av_frame_unref(&s->picture);
     return 0;
 }
 
diff --git a/libavcodec/avuidec.c b/libavcodec/avuidec.c
index 22af719..a574f5b 100644
--- a/libavcodec/avuidec.c
+++ b/libavcodec/avuidec.c
@@ -27,30 +27,20 @@
 static av_cold int avui_decode_init(AVCodecContext *avctx)
 {
     avctx->pix_fmt = AV_PIX_FMT_YUVA422P;
-
-    avctx->coded_frame = avcodec_alloc_frame();
-
-    if (!avctx->coded_frame) {
-        av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
-        return AVERROR(ENOMEM);
-    }
-
     return 0;
 }
 
 static int avui_decode_frame(AVCodecContext *avctx, void *data,
                              int *got_frame, AVPacket *avpkt)
 {
-    AVFrame *pic = avctx->coded_frame;
+    int ret;
+    AVFrame *pic = data;
     const uint8_t *src = avpkt->data, *extradata = avctx->extradata;
     const uint8_t *srca;
     uint8_t *y, *u, *v, *a;
     int transparent, interlaced = 1, skip, opaque_length, i, j, k;
     uint32_t extradata_size = avctx->extradata_size;
 
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
-
     while (extradata_size >= 24) {
         uint32_t atom_size = AV_RB32(extradata);
         if (!memcmp(&extradata[4], "APRGAPRG0001", 12)) {
@@ -78,12 +68,8 @@
                   avpkt->size >= opaque_length * 2 + 4;
     srca = src + opaque_length + 5;
 
-    pic->reference = 0;
-
-    if (ff_get_buffer(avctx, pic) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
-        return AVERROR(ENOMEM);
-    }
+    if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
+        return ret;
 
     pic->key_frame = 1;
     pic->pict_type = AV_PICTURE_TYPE_I;
@@ -129,28 +115,16 @@
         srca += 4;
     }
     *got_frame       = 1;
-    *(AVFrame *)data = *pic;
 
     return avpkt->size;
 }
 
-static av_cold int avui_decode_close(AVCodecContext *avctx)
-{
-    if (avctx->coded_frame->data[0])
-        avctx->release_buffer(avctx, avctx->coded_frame);
-
-    av_freep(&avctx->coded_frame);
-
-    return 0;
-}
-
 AVCodec ff_avui_decoder = {
     .name         = "avui",
     .type         = AVMEDIA_TYPE_VIDEO,
     .id           = AV_CODEC_ID_AVUI,
     .init         = avui_decode_init,
     .decode       = avui_decode_frame,
-    .close        = avui_decode_close,
     .capabilities = CODEC_CAP_DR1,
     .long_name    = NULL_IF_CONFIG_SMALL("Avid Meridien Uncompressed"),
 };
diff --git a/libavcodec/avuienc.c b/libavcodec/avuienc.c
index 4428232..a4970a0 100644
--- a/libavcodec/avuienc.c
+++ b/libavcodec/avuienc.c
@@ -70,7 +70,6 @@
         dst += avctx->width * skip;
     }
 
-    avctx->coded_frame->reference = 0;
     avctx->coded_frame->key_frame = 1;
     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
 
diff --git a/libavcodec/bethsoftvideo.c b/libavcodec/bethsoftvideo.c
index 2a03f1a..474265f 100644
--- a/libavcodec/bethsoftvideo.c
+++ b/libavcodec/bethsoftvideo.c
@@ -31,6 +31,7 @@
 #include "avcodec.h"
 #include "bethsoftvideo.h"
 #include "bytestream.h"
+#include "internal.h"
 
 typedef struct BethsoftvidContext {
     AVFrame frame;
@@ -41,9 +42,6 @@
 {
     BethsoftvidContext *vid = avctx->priv_data;
     avcodec_get_frame_defaults(&vid->frame);
-    vid->frame.reference = 3;
-    vid->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
-        FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
     return 0;
 }
@@ -77,10 +75,8 @@
     int code, ret;
     int yoffset;
 
-    if ((ret = avctx->reget_buffer(avctx, &vid->frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, &vid->frame)) < 0)
         return ret;
-    }
     wrap_to_next_line = vid->frame.linesize[0] - avctx->width;
 
     if (avpkt->side_data_elems > 0 &&
@@ -138,8 +134,10 @@
     }
     end:
 
+    if ((ret = av_frame_ref(data, &vid->frame)) < 0)
+        return ret;
+
     *got_frame = 1;
-    *(AVFrame*)data = vid->frame;
 
     return avpkt->size;
 }
@@ -147,8 +145,7 @@
 static av_cold int bethsoftvid_decode_end(AVCodecContext *avctx)
 {
     BethsoftvidContext * vid = avctx->priv_data;
-    if(vid->frame.data[0])
-        avctx->release_buffer(avctx, &vid->frame);
+    av_frame_unref(&vid->frame);
     return 0;
 }
 
diff --git a/libavcodec/bfi.c b/libavcodec/bfi.c
index 231ee62..9cfcd14 100644
--- a/libavcodec/bfi.c
+++ b/libavcodec/bfi.c
@@ -33,7 +33,6 @@
 
 typedef struct BFIContext {
     AVCodecContext *avctx;
-    AVFrame frame;
     uint8_t *dst;
     uint32_t pal[256];
 } BFIContext;
@@ -42,7 +41,6 @@
 {
     BFIContext *bfi = avctx->priv_data;
     avctx->pix_fmt  = AV_PIX_FMT_PAL8;
-    avcodec_get_frame_defaults(&bfi->frame);
     bfi->dst        = av_mallocz(avctx->width * avctx->height);
     return 0;
 }
@@ -50,6 +48,7 @@
 static int bfi_decode_frame(AVCodecContext *avctx, void *data,
                             int *got_frame, AVPacket *avpkt)
 {
+    AVFrame *frame = data;
     GetByteContext g;
     int buf_size    = avpkt->size;
     BFIContext *bfi = avctx->priv_data;
@@ -59,28 +58,21 @@
     uint32_t *pal;
     int i, j, ret, height = avctx->height;
 
-    if (bfi->frame.data[0])
-        avctx->release_buffer(avctx, &bfi->frame);
-
-    bfi->frame.reference = 3;
-
-    if ((ret = ff_get_buffer(avctx, &bfi->frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     bytestream2_init(&g, avpkt->data, buf_size);
 
     /* Set frame parameters and palette, if necessary */
     if (!avctx->frame_number) {
-        bfi->frame.pict_type = AV_PICTURE_TYPE_I;
-        bfi->frame.key_frame = 1;
+        frame->pict_type = AV_PICTURE_TYPE_I;
+        frame->key_frame = 1;
         /* Setting the palette */
         if (avctx->extradata_size > 768) {
             av_log(NULL, AV_LOG_ERROR, "Palette is too large.\n");
             return AVERROR_INVALIDDATA;
         }
-        pal = (uint32_t *)bfi->frame.data[1];
+        pal = (uint32_t *)frame->data[1];
         for (i = 0; i < avctx->extradata_size / 3; i++) {
             int shift = 16;
             *pal = 0xFFU << 24;
@@ -89,13 +81,13 @@
                          (avctx->extradata[i * 3 + j] >> 4)) << shift;
             pal++;
         }
-        memcpy(bfi->pal, bfi->frame.data[1], sizeof(bfi->pal));
-        bfi->frame.palette_has_changed = 1;
+        memcpy(bfi->pal, frame->data[1], sizeof(bfi->pal));
+        frame->palette_has_changed = 1;
     } else {
-        bfi->frame.pict_type = AV_PICTURE_TYPE_P;
-        bfi->frame.key_frame = 0;
-        bfi->frame.palette_has_changed = 0;
-        memcpy(bfi->frame.data[1], bfi->pal, sizeof(bfi->pal));
+        frame->pict_type = AV_PICTURE_TYPE_P;
+        frame->key_frame = 0;
+        frame->palette_has_changed = 0;
+        memcpy(frame->data[1], bfi->pal, sizeof(bfi->pal));
     }
 
     bytestream2_skip(&g, 4); // Unpacked size, not required.
@@ -163,22 +155,20 @@
     }
 
     src = bfi->dst;
-    dst = bfi->frame.data[0];
+    dst = frame->data[0];
     while (height--) {
         memcpy(dst, src, avctx->width);
         src += avctx->width;
-        dst += bfi->frame.linesize[0];
+        dst += frame->linesize[0];
     }
     *got_frame = 1;
-    *(AVFrame *)data = bfi->frame;
+
     return buf_size;
 }
 
 static av_cold int bfi_decode_close(AVCodecContext *avctx)
 {
     BFIContext *bfi = avctx->priv_data;
-    if (bfi->frame.data[0])
-        avctx->release_buffer(avctx, &bfi->frame);
     av_free(bfi->dst);
     return 0;
 }
diff --git a/libavcodec/bfin/Makefile b/libavcodec/bfin/Makefile
index d1b41bc..c293360 100644
--- a/libavcodec/bfin/Makefile
+++ b/libavcodec/bfin/Makefile
@@ -3,6 +3,8 @@
         bfin/idct_bfin.o                                                \
         bfin/pixels_bfin.o                                              \
 
+OBJS-$(CONFIG_HPELDSP)                  += bfin/hpeldsp_bfin.o          \
+                                           bfin/hpel_pixels_bfin.o
 OBJS-$(CONFIG_MPEGVIDEOENC)             += bfin/mpegvideo_bfin.o
 OBJS-$(CONFIG_VP3DSP)                   += bfin/vp3_bfin.o              \
                                            bfin/vp3_idct_bfin.o
diff --git a/libavcodec/bfin/dsputil_bfin.c b/libavcodec/bfin/dsputil_bfin.c
index ce04753..a95fd6b 100644
--- a/libavcodec/bfin/dsputil_bfin.c
+++ b/libavcodec/bfin/dsputil_bfin.c
@@ -54,79 +54,6 @@
         ::"a" (blocks):"P0","I0","R0");
 }
 
-
-
-static void bfin_put_pixels8 (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    ff_bfin_put_pixels8uc (block, pixels, pixels, line_size, line_size, h);
-}
-
-static void bfin_put_pixels8_x2(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    ff_bfin_put_pixels8uc (block, pixels, pixels+1, line_size, line_size, h);
-}
-
-static void bfin_put_pixels8_y2 (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    ff_bfin_put_pixels8uc (block, pixels, pixels+line_size, line_size, line_size, h);
-}
-
-static void bfin_put_pixels8_xy2 (uint8_t *block, const uint8_t *s0, ptrdiff_t line_size, int h)
-{
-    ff_bfin_z_put_pixels8_xy2 (block,s0,line_size, line_size, h);
-}
-
-static void bfin_put_pixels16 (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    ff_bfin_put_pixels16uc (block, pixels, pixels, line_size, line_size, h);
-}
-
-static void bfin_put_pixels16_x2 (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    ff_bfin_put_pixels16uc (block, pixels, pixels+1, line_size, line_size, h);
-}
-
-static void bfin_put_pixels16_y2 (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    ff_bfin_put_pixels16uc (block, pixels, pixels+line_size, line_size, line_size, h);
-}
-
-static void bfin_put_pixels16_xy2 (uint8_t *block, const uint8_t *s0, ptrdiff_t line_size, int h)
-{
-    ff_bfin_z_put_pixels16_xy2 (block,s0,line_size, line_size, h);
-}
-
-static void bfin_put_pixels8_nornd (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    ff_bfin_put_pixels8uc_nornd (block, pixels, pixels, line_size, h);
-}
-
-static void bfin_put_pixels8_x2_nornd (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    ff_bfin_put_pixels8uc_nornd (block, pixels, pixels+1, line_size, h);
-}
-
-static void bfin_put_pixels8_y2_nornd (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    ff_bfin_put_pixels8uc_nornd (block, pixels, pixels+line_size, line_size, h);
-}
-
-
-static void bfin_put_pixels16_nornd (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    ff_bfin_put_pixels16uc_nornd (block, pixels, pixels, line_size, h);
-}
-
-static void bfin_put_pixels16_x2_nornd (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    ff_bfin_put_pixels16uc_nornd (block, pixels, pixels+1, line_size, h);
-}
-
-static void bfin_put_pixels16_y2_nornd (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    ff_bfin_put_pixels16uc_nornd (block, pixels, pixels+line_size, line_size, h);
-}
-
 static int bfin_pix_abs16 (void *c, uint8_t *blk1, uint8_t *blk2, int line_size, int h)
 {
     return ff_bfin_z_sad16x16 (blk1,blk2,line_size,line_size,h);
@@ -205,7 +132,7 @@
     c->add_pixels_clamped = ff_bfin_add_pixels_clamped;
 
     if (!high_bit_depth)
-    c->get_pixels         = ff_bfin_get_pixels;
+        c->get_pixels     = ff_bfin_get_pixels;
     c->clear_blocks       = bfin_clear_blocks;
     c->pix_sum            = ff_bfin_pix_sum;
     c->pix_norm1          = ff_bfin_pix_norm1;
@@ -232,33 +159,11 @@
     c->sse[1] = ff_bfin_sse8;
     c->sse[2] = ff_bfin_sse4;
 
-    if (!high_bit_depth) {
-    c->put_pixels_tab[0][0] = bfin_put_pixels16;
-    c->put_pixels_tab[0][1] = bfin_put_pixels16_x2;
-    c->put_pixels_tab[0][2] = bfin_put_pixels16_y2;
-    c->put_pixels_tab[0][3] = bfin_put_pixels16_xy2;
-
-    c->put_pixels_tab[1][0] = bfin_put_pixels8;
-    c->put_pixels_tab[1][1] = bfin_put_pixels8_x2;
-    c->put_pixels_tab[1][2] = bfin_put_pixels8_y2;
-    c->put_pixels_tab[1][3] = bfin_put_pixels8_xy2;
-
-    c->put_no_rnd_pixels_tab[1][0] = bfin_put_pixels8_nornd;
-    c->put_no_rnd_pixels_tab[1][1] = bfin_put_pixels8_x2_nornd;
-    c->put_no_rnd_pixels_tab[1][2] = bfin_put_pixels8_y2_nornd;
-/*     c->put_no_rnd_pixels_tab[1][3] = ff_bfin_put_pixels8_xy2_nornd; */
-
-    c->put_no_rnd_pixels_tab[0][0] = bfin_put_pixels16_nornd;
-    c->put_no_rnd_pixels_tab[0][1] = bfin_put_pixels16_x2_nornd;
-    c->put_no_rnd_pixels_tab[0][2] = bfin_put_pixels16_y2_nornd;
-/*     c->put_no_rnd_pixels_tab[0][3] = ff_bfin_put_pixels16_xy2_nornd; */
-    }
-
     if (avctx->bits_per_raw_sample <= 8) {
         if (avctx->dct_algo == FF_DCT_AUTO)
             c->fdct                  = ff_bfin_fdct;
 
-    if (avctx->idct_algo == FF_IDCT_AUTO) {
+        if (avctx->idct_algo == FF_IDCT_AUTO) {
             c->idct_permutation_type = FF_NO_IDCT_PERM;
             c->idct                  = ff_bfin_idct;
             c->idct_add              = bfin_idct_add;
diff --git a/libavcodec/bfin/dsputil_bfin.h b/libavcodec/bfin/dsputil_bfin.h
index 2930923..079ebdd 100644
--- a/libavcodec/bfin/dsputil_bfin.h
+++ b/libavcodec/bfin/dsputil_bfin.h
@@ -46,19 +46,8 @@
 int  ff_bfin_z_sad8x8   (uint8_t *blk1, uint8_t *blk2, int dsz, int line_size, int h) attribute_l1_text;
 int  ff_bfin_z_sad16x16 (uint8_t *blk1, uint8_t *blk2, int dsz, int line_size, int h) attribute_l1_text;
 
-void ff_bfin_z_put_pixels16_xy2     (uint8_t *block, const uint8_t *s0, int dest_size, int line_size, int h) attribute_l1_text;
-void ff_bfin_z_put_pixels8_xy2      (uint8_t *block, const uint8_t *s0, int dest_size, int line_size, int h) attribute_l1_text;
-void ff_bfin_put_pixels16_xy2_nornd (uint8_t *block, const uint8_t *s0, int line_size, int h) attribute_l1_text;
-void ff_bfin_put_pixels8_xy2_nornd  (uint8_t *block, const uint8_t *s0, int line_size, int h) attribute_l1_text;
-
-
 int  ff_bfin_pix_sum (uint8_t *p, int stride) attribute_l1_text;
 
-void ff_bfin_put_pixels8uc        (uint8_t *block, const uint8_t *s0, const uint8_t *s1, int dest_size, int line_size, int h) attribute_l1_text;
-void ff_bfin_put_pixels16uc       (uint8_t *block, const uint8_t *s0, const uint8_t *s1, int dest_size, int line_size, int h) attribute_l1_text;
-void ff_bfin_put_pixels8uc_nornd  (uint8_t *block, const uint8_t *s0, const uint8_t *s1, int line_size, int h) attribute_l1_text;
-void ff_bfin_put_pixels16uc_nornd (uint8_t *block, const uint8_t *s0, const uint8_t *s1, int line_size, int h) attribute_l1_text;
-
 int ff_bfin_sse4  (void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h) attribute_l1_text;
 int ff_bfin_sse8  (void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h) attribute_l1_text;
 int ff_bfin_sse16 (void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h) attribute_l1_text;
diff --git a/libavcodec/bfin/hpel_pixels_bfin.S b/libavcodec/bfin/hpel_pixels_bfin.S
new file mode 100644
index 0000000..c0cbf1f
--- /dev/null
+++ b/libavcodec/bfin/hpel_pixels_bfin.S
@@ -0,0 +1,379 @@
+/*
+ * Blackfin Pixel Operations
+ * Copyright (C) 2007 Marc Hoffman <marc.hoffman@analog.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 "config_bfin.h"
+
+/*
+  motion compensation
+  primitives
+
+     * Halfpel motion compensation with rounding (a+b+1)>>1.
+     * This is an array[4][4] of motion compensation funcions for 4
+     * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
+     * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
+     * @param block destination where the result is stored
+     * @param pixels source
+     * @param line_size number of bytes in a horizontal line of block
+     * @param h height
+
+*/
+
+DEFUN(put_pixels8uc,mL1,
+        (uint8_t *block, const uint8_t *s0, const uint8_t *s1,
+                 int dest_size, int line_size, int h)):
+        i3=r0;        // dest
+        i0=r1;        // src0
+        i1=r2;        // src1
+        r0=[sp+12];   // dest_size
+        r2=[sp+16];   // line_size
+        p0=[sp+20];   // h
+        [--sp] = (r7:6);
+        r0+=-4;
+        m3=r0;
+        r2+=-8;
+        m0=r2;
+        LSETUP(pp8$0,pp8$1) LC0=P0;
+        DISALGNEXCPT                || R0 = [I0++]  || R2  =[I1++];
+
+pp8$0:  DISALGNEXCPT                || R1 = [I0++]  || R3  =[I1++];
+        R6 = BYTEOP1P(R1:0,R3:2)    || R0 = [I0++M0]|| R2  =[I1++M0];
+        R7 = BYTEOP1P(R1:0,R3:2)(R) || R0 = [I0++]  || [I3++] = R6 ;
+pp8$1:  DISALGNEXCPT                || R2 = [I1++]  || [I3++M3] = R7;
+
+        (r7:6) = [sp++];
+        RTS;
+DEFUN_END(put_pixels8uc)
+
+DEFUN(put_pixels16uc,mL1,
+        (uint8_t *block, const uint8_t *s0, const uint8_t *s1,
+                 int dest_size, int line_size, int h)):
+        link 0;
+        [--sp] = (r7:6);
+        i3=r0;        // dest
+        i0=r1;        // src0
+        i1=r2;        // src1
+        r0=[fp+20];   // dest_size
+        r2=[fp+24];   // line_size
+        p0=[fp+28];   // h
+
+
+        r0+=-12;
+        m3=r0;        // line_size
+        r2+=-16;
+        m0=r2;
+
+        LSETUP(pp16$0,pp16$1) LC0=P0;
+         DISALGNEXCPT                || R0 = [I0++]   || R2  =[I1++];
+
+pp16$0:  DISALGNEXCPT                || R1 = [I0++]   || R3  =[I1++];
+         R6 = BYTEOP1P(R1:0,R3:2)    || R0 = [I0++]   || R2  =[I1++];
+         R7 = BYTEOP1P(R1:0,R3:2)(R) || R1 = [I0++]   || R3  =[I1++];
+         [I3++] = R6;
+         R6 = BYTEOP1P(R1:0,R3:2)    || R0 = [I0++M0] || R2  =[I1++M0];
+         R7 = BYTEOP1P(R1:0,R3:2)(R) || R0 = [I0++]   || [I3++] = R7 ;
+         [I3++] = R6;
+pp16$1:  DISALGNEXCPT                || R2 = [I1++]   || [I3++M3] = R7;
+
+        (r7:6) = [sp++];
+        unlink;
+        RTS;
+DEFUN_END(put_pixels16uc)
+
+
+
+
+
+
+DEFUN(put_pixels8uc_nornd,mL1,
+        (uint8_t *block, const uint8_t *s0, const uint8_t *s1,
+                 int line_size, int h)):
+        i3=r0;        // dest
+        i0=r1;        // src0
+        i1=r2;        // src1
+        r2=[sp+12];   // line_size
+        p0=[sp+16];   // h
+        [--sp] = (r7:6);
+        r2+=-4;
+        m3=r2;
+        r2+=-4;
+        m0=r2;
+        LSETUP(pp8$2,pp8$3) LC0=P0;
+        DISALGNEXCPT                || R0 = [I0++]  || R2  =[I1++];
+
+pp8$2:  DISALGNEXCPT                || R1 = [I0++]  || R3  =[I1++];
+        R6 = BYTEOP1P(R1:0,R3:2)(T)  || R0 = [I0++M0]|| R2  =[I1++M0];
+        R7 = BYTEOP1P(R1:0,R3:2)(T,R) || R0 = [I0++]  || [I3++] = R6 ;
+pp8$3:  DISALGNEXCPT                || R2 = [I1++]  || [I3++M3] = R7;
+
+        (r7:6) = [sp++];
+        RTS;
+DEFUN_END(put_pixels8uc_nornd)
+
+DEFUN(put_pixels16uc_nornd,mL1,
+        (uint8_t *block, const uint8_t *s0, const uint8_t *s1,
+                 int line_size, int h)):
+        i3=r0;        // dest
+        i0=r1;        // src0
+        i1=r2;        // src1
+        r2=[sp+12];   // line_size
+        p0=[sp+16];   // h
+
+        [--sp] = (r7:6);
+        r2+=-12;
+        m3=r2;        // line_size
+        r2+=-4;
+        m0=r2;
+
+        LSETUP(pp16$2,pp16$3) LC0=P0;
+        DISALGNEXCPT                || R0 = [I0++]   || R2  =[I1++];
+
+pp16$2:
+        DISALGNEXCPT                || R1 = [I0++]   || R3  =[I1++];
+        R6 = BYTEOP1P(R1:0,R3:2)(T)    || R0 = [I0++]   || R2  =[I1++];
+        R7 = BYTEOP1P(R1:0,R3:2)(T,R) || R1 = [I0++]   || R3  =[I1++];
+        [I3++] = R6;
+
+        R6 = BYTEOP1P(R1:0,R3:2)(T)    || R0 = [I0++M0] || R2  =[I1++M0];
+        R7 = BYTEOP1P(R1:0,R3:2)(T,R) || R0 = [I0++]   || [I3++] = R7 ;
+        [I3++] = R6;
+pp16$3: DISALGNEXCPT                || R2 = [I1++]   || [I3++M3] = R7;
+
+        (r7:6) = [sp++];
+
+        RTS;
+DEFUN_END(put_pixels16uc_nornd)
+
+DEFUN(z_put_pixels16_xy2,mL1,
+        (uint8_t *block, const uint8_t *s0,
+                 int dest_size, int line_size, int h)):
+        link 0;
+        [--sp] = (r7:4);
+        i3=r0;        // dest
+        i0=r1;        // src0--> pixels
+        i1=r1;        // src1--> pixels + line_size
+        r2+=-12;
+        m2=r2;        // m2=dest_width-4
+        r2=[fp+20];
+        m3=r2;        // line_size
+        p0=[fp+24];   // h
+        r2+=-16;
+        i1+=m3;       /* src1 + line_size */
+        m0=r2;        /* line-size - 20 */
+
+        B0 = I0;
+        B1 = I1;
+        B3 = I3;
+
+        DISALGNEXCPT                       || R0 = [I0++] || R2  =[I1++];
+
+        LSETUP(LS$16E,LE$16E) LC0=P0;
+LS$16E: DISALGNEXCPT                       || R1 = [I0++] || R3  =[I1++];
+        R4 = BYTEOP2P (R3:2,R1:0) (RNDL)   || R0 = [I0++] || R2  =[I1++];
+        R5 = BYTEOP2P (R3:2,R1:0) (RNDL,R) || R1 = [I0++] || [I3++] = R4 ;
+        DISALGNEXCPT                       || R3 = [I1++] || [I3++] = R5;
+        R4 = BYTEOP2P (R3:2,R1:0) (RNDL)   || R0 = [I0++M0]|| R2  = [I1++M0];
+        R5 = BYTEOP2P (R3:2,R1:0) (RNDL,R) || R0 = [I0++] || [I3++] = R4 ;
+LE$16E: DISALGNEXCPT                       || R2 = [I1++] || [I3++M2] = R5;
+
+        M1 = 1;
+        I3 = B3;
+        I1 = B1;
+        I0 = B0;
+
+        I0 += M1;
+        I1 += M1;
+
+        DISALGNEXCPT                       || R0 = [I0++] || R2  =[I1++];
+        LSETUP(LS$16O,LE$16O) LC0=P0;
+LS$16O: DISALGNEXCPT                       || R1 = [I0++] || R3  =[I1++];
+        R4 = BYTEOP2P (R3:2,R1:0) (RNDH)   || R0 = [I0++] || R2  =[I1++];
+        R5 = BYTEOP2P (R3:2,R1:0) (RNDH,R) || R1 = [I0++] || R6  =[I3++];
+        R4 = R4 +|+ R6                       || R7 = [I3--];
+        R5 = R5 +|+ R7                       || [I3++] = R4;
+        DISALGNEXCPT                       || R3  =[I1++] || [I3++] = R5;
+        R4 = BYTEOP2P (R3:2,R1:0) (RNDH)   || R0 = [I0++M0]|| R2  = [I1++M0];
+        R5 = BYTEOP2P (R3:2,R1:0) (RNDH,R) || R0 = [I0++] || R6 = [I3++];
+        R4 = R4 +|+ R6                       || R7 = [I3--];
+        R5 = R5 +|+ R7                       || [I3++] = R4;
+LE$16O: DISALGNEXCPT                       || R2 = [I1++] || [I3++M2] = R5;
+
+        (r7:4) = [sp++];
+        unlink;
+        rts;
+DEFUN_END(z_put_pixels16_xy2)
+
+DEFUN(put_pixels16_xy2_nornd,mL1,
+        (uint8_t *block, const uint8_t *s0,
+                 int line_size, int h)):
+        link 0;
+        [--sp] = (r7:4);
+        i3=r0;        // dest
+        i0=r1;        // src0--> pixels
+        i1=r1;        // src1--> pixels + line_size
+        m3=r2;
+        r2+=-12;
+        m2=r2;
+        r2+=-4;
+        i1+=m3;       /* src1 + line_size */
+        m0=r2;        /* line-size - 20 */
+        p0=[fp+20];   // h
+
+        B0=I0;
+        B1=I1;
+        B3=I3;
+
+        DISALGNEXCPT                       || R0 = [I0++] || R2  =[I1++];
+
+        LSETUP(LS$16ET,LE$16ET) LC0=P0;
+LS$16ET:DISALGNEXCPT                       || R1 = [I0++] || R3  =[I1++];
+        R4 = BYTEOP2P (R3:2,R1:0) (TL)     || R0 = [I0++] || R2  =[I1++];
+        R5 = BYTEOP2P (R3:2,R1:0) (TL,R)   || R1 = [I0++] || [I3++] = R4 ;
+        DISALGNEXCPT                       || R3 = [I1++] || [I3++] = R5;
+        R4 = BYTEOP2P (R3:2,R1:0) (TL)     || R0 = [I0++M0]|| R2  = [I1++M0];
+        R5 = BYTEOP2P (R3:2,R1:0) (TL,R)   || R0 = [I0++] || [I3++] = R4 ;
+LE$16ET:DISALGNEXCPT                       || R2 = [I1++] || [I3++M2] = R5;
+
+        M1 = 1;
+        I3=B3;
+        I1=B1;
+        I0=B0;
+
+        I0 += M1;
+        I1 += M1;
+
+        DISALGNEXCPT                       || R0 = [I0++] || R2  =[I1++];
+        LSETUP(LS$16OT,LE$16OT) LC0=P0;
+LS$16OT:DISALGNEXCPT                       || R1 = [I0++] || R3  =[I1++];
+        R4 = BYTEOP2P (R3:2,R1:0) (TH)     || R0 = [I0++] || R2  =[I1++];
+        R5 = BYTEOP2P (R3:2,R1:0) (TH,R)   || R1 = [I0++] || R6  =[I3++];
+        R4 = R4 +|+ R6                                    || R7 = [I3--];
+        R5 = R5 +|+ R7                                    || [I3++] = R4;
+        DISALGNEXCPT                       || R3  =[I1++] || [I3++] = R5;
+        R4 = BYTEOP2P (R3:2,R1:0) (TH)     || R0 = [I0++M0]|| R2  = [I1++M0];
+        R5 = BYTEOP2P (R3:2,R1:0) (TH,R)   || R0 = [I0++] || R6 = [I3++];
+        R4 = R4 +|+ R6                                    || R7 = [I3--];
+        R5 = R5 +|+ R7                                    || [I3++] = R4;
+LE$16OT:DISALGNEXCPT                       || R2 = [I1++] || [I3++M2] = R5;
+
+        (r7:4) = [sp++];
+        unlink;
+        rts;
+DEFUN_END(put_pixels16_xy2_nornd)
+
+DEFUN(z_put_pixels8_xy2,mL1,
+        (uint8_t *block, const uint8_t *s0,
+                 int dest_size, int line_size, int h)):
+        link 0;
+        [--sp] = (r7:4);
+        i3=r0;        // dest
+        i0=r1;        // src0--> pixels
+        i1=r1;        // src1--> pixels + line_size
+        r2+=-4;
+        m2=r2;        // m2=dest_width-4
+        r2=[fp+20];
+        m3=r2;        // line_size
+        p0=[fp+24];   // h
+        r2+=-8;
+        i1+=m3;       /* src1 + line_size */
+        m0=r2;        /* line-size - 20 */
+
+        b0 = I0;
+        b1 = I1;
+        b3 = I3;
+
+        LSETUP(LS$8E,LE$8E) LC0=P0;
+        DISALGNEXCPT                       || R0 = [I0++]   || R2  =[I1++];
+LS$8E:  DISALGNEXCPT                       || R1 = [I0++]   || R3  =[I1++];
+        R4 = BYTEOP2P (R3:2,R1:0) (RNDL)   || R0 = [I0++M0] || R2  =[I1++M0];
+        R5 = BYTEOP2P (R3:2,R1:0) (RNDL,R) || R0 = [I0++]   || [I3++] = R4 ;
+LE$8E:  DISALGNEXCPT                       || R2 = [I1++]   || [I3++M2] = R5;
+
+        M1 = 1;
+        I3 = b3;
+        I1 = b1;
+        I0 = b0;
+
+        I0 += M1;
+        I1 += M1;
+
+        LSETUP(LS$8O,LE$8O) LC0=P0;
+        DISALGNEXCPT                       || R0 = [I0++]   || R2  =[I1++];
+LS$8O:  DISALGNEXCPT                       || R1 = [I0++]   || R3  =[I1++];
+        R4 = BYTEOP2P (R3:2,R1:0) (RNDH)   || R0 = [I0++M0] || R2  =[I1++M0];
+        R5 = BYTEOP2P (R3:2,R1:0) (RNDH,R) || R0 = [I0++]   || R6  =[I3++];
+        R4 = R4 +|+ R6                                      || R7 = [I3--];
+        R5 = R5 +|+ R7                                      || [I3++] = R4;
+LE$8O:  DISALGNEXCPT                       || R2  =[I1++]   || [I3++M2] = R5;
+
+        (r7:4) = [sp++];
+        unlink;
+        rts;
+DEFUN_END(z_put_pixels8_xy2)
+
+DEFUN(put_pixels8_xy2_nornd,mL1,
+        (uint8_t *block, const uint8_t *s0, int line_size, int h)):
+        link 0;
+        [--sp] = (r7:4);
+        i3=r0;        // dest
+        i0=r1;        // src0--> pixels
+        i1=r1;        // src1--> pixels + line_size
+        m3=r2;
+        r2+=-4;
+        m2=r2;
+        r2+=-4;
+        i1+=m3;       /* src1 + line_size */
+        m0=r2;        /* line-size - 20 */
+        p0=[fp+20];   // h
+
+
+        b0 = I0;
+        b1 = I1;
+        b3 = I3;
+
+        LSETUP(LS$8ET,LE$8ET) LC0=P0;
+        DISALGNEXCPT                       || R0 = [I0++]   || R2  =[I1++];
+
+LS$8ET: DISALGNEXCPT                       || R1 = [I0++]   || R3 = [I1++];
+        R4 = BYTEOP2P (R3:2,R1:0) (TL)     || R0 = [I0++M0] || R2 = [I1++M0];
+        R5 = BYTEOP2P (R3:2,R1:0) (TL,R)   || R0 = [I0++]   || [I3++] = R4 ;
+LE$8ET: DISALGNEXCPT                       || R2 = [I1++]   || [I3++M2] = R5;
+
+        M1 = 1;
+        I3 = b3;
+        I1 = b1;
+        I0 = b0;
+
+        I0 += M1;
+        I1 += M1;
+
+        LSETUP(LS$8OT,LE$8OT) LC0=P0;
+        DISALGNEXCPT                       || R0 = [I0++]   || R2 = [I1++];
+
+LS$8OT: DISALGNEXCPT                       || R1 = [I0++]   || R3 = [I1++];
+        R4 = BYTEOP2P (R3:2,R1:0) (TH)     || R0 = [I0++M0] || R2 = [I1++M0];
+        R5 = BYTEOP2P (R3:2,R1:0) (TH,R)   || R0 = [I0++]   || R6 = [I3++];
+        R4 = R4 +|+ R6                                      || R7 = [I3--];
+        R5 = R5 +|+ R7                                      || [I3++] = R4;
+LE$8OT: DISALGNEXCPT                       || R2  =[I1++]   || [I3++M2] = R5;
+
+        (r7:4) = [sp++];
+        unlink;
+        rts;
+DEFUN_END(put_pixels8_xy2_nornd)
diff --git a/libavcodec/bfin/hpeldsp_bfin.c b/libavcodec/bfin/hpeldsp_bfin.c
new file mode 100644
index 0000000..2df551b
--- /dev/null
+++ b/libavcodec/bfin/hpeldsp_bfin.c
@@ -0,0 +1,123 @@
+/*
+ * BlackFin DSPUTILS
+ *
+ * Copyright (C) 2007 Marc Hoffman <marc.hoffman@analog.com>
+ * Copyright (c) 2006 Michael Benjamin <michael.benjamin@analog.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 <stddef.h>
+#include <stdint.h>
+
+#include "libavutil/attributes.h"
+#include "libavcodec/hpeldsp.h"
+#include "hpeldsp_bfin.h"
+
+static void bfin_put_pixels8 (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    ff_bfin_put_pixels8uc (block, pixels, pixels, line_size, line_size, h);
+}
+
+static void bfin_put_pixels8_x2(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    ff_bfin_put_pixels8uc (block, pixels, pixels+1, line_size, line_size, h);
+}
+
+static void bfin_put_pixels8_y2 (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    ff_bfin_put_pixels8uc (block, pixels, pixels+line_size, line_size, line_size, h);
+}
+
+static void bfin_put_pixels8_xy2 (uint8_t *block, const uint8_t *s0, ptrdiff_t line_size, int h)
+{
+    ff_bfin_z_put_pixels8_xy2 (block,s0,line_size, line_size, h);
+}
+
+static void bfin_put_pixels16 (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    ff_bfin_put_pixels16uc (block, pixels, pixels, line_size, line_size, h);
+}
+
+static void bfin_put_pixels16_x2 (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    ff_bfin_put_pixels16uc (block, pixels, pixels+1, line_size, line_size, h);
+}
+
+static void bfin_put_pixels16_y2 (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    ff_bfin_put_pixels16uc (block, pixels, pixels+line_size, line_size, line_size, h);
+}
+
+static void bfin_put_pixels16_xy2 (uint8_t *block, const uint8_t *s0, ptrdiff_t line_size, int h)
+{
+    ff_bfin_z_put_pixels16_xy2 (block,s0,line_size, line_size, h);
+}
+
+static void bfin_put_pixels8_nornd (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    ff_bfin_put_pixels8uc_nornd (block, pixels, pixels, line_size, h);
+}
+
+static void bfin_put_pixels8_x2_nornd (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    ff_bfin_put_pixels8uc_nornd (block, pixels, pixels+1, line_size, h);
+}
+
+static void bfin_put_pixels8_y2_nornd (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    ff_bfin_put_pixels8uc_nornd (block, pixels, pixels+line_size, line_size, h);
+}
+
+
+static void bfin_put_pixels16_nornd (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    ff_bfin_put_pixels16uc_nornd (block, pixels, pixels, line_size, h);
+}
+
+static void bfin_put_pixels16_x2_nornd (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    ff_bfin_put_pixels16uc_nornd (block, pixels, pixels+1, line_size, h);
+}
+
+static void bfin_put_pixels16_y2_nornd (uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    ff_bfin_put_pixels16uc_nornd (block, pixels, pixels+line_size, line_size, h);
+}
+
+av_cold void ff_hpeldsp_init_bfin(HpelDSPContext *c, int flags)
+{
+    c->put_pixels_tab[0][0] = bfin_put_pixels16;
+    c->put_pixels_tab[0][1] = bfin_put_pixels16_x2;
+    c->put_pixels_tab[0][2] = bfin_put_pixels16_y2;
+    c->put_pixels_tab[0][3] = bfin_put_pixels16_xy2;
+
+    c->put_pixels_tab[1][0] = bfin_put_pixels8;
+    c->put_pixels_tab[1][1] = bfin_put_pixels8_x2;
+    c->put_pixels_tab[1][2] = bfin_put_pixels8_y2;
+    c->put_pixels_tab[1][3] = bfin_put_pixels8_xy2;
+
+    c->put_no_rnd_pixels_tab[1][0] = bfin_put_pixels8_nornd;
+    c->put_no_rnd_pixels_tab[1][1] = bfin_put_pixels8_x2_nornd;
+    c->put_no_rnd_pixels_tab[1][2] = bfin_put_pixels8_y2_nornd;
+/*     c->put_no_rnd_pixels_tab[1][3] = ff_bfin_put_pixels8_xy2_nornd; */
+
+    c->put_no_rnd_pixels_tab[0][0] = bfin_put_pixels16_nornd;
+    c->put_no_rnd_pixels_tab[0][1] = bfin_put_pixels16_x2_nornd;
+    c->put_no_rnd_pixels_tab[0][2] = bfin_put_pixels16_y2_nornd;
+/*     c->put_no_rnd_pixels_tab[0][3] = ff_bfin_put_pixels16_xy2_nornd; */
+}
diff --git a/libavcodec/bfin/hpeldsp_bfin.h b/libavcodec/bfin/hpeldsp_bfin.h
new file mode 100644
index 0000000..36f3872
--- /dev/null
+++ b/libavcodec/bfin/hpeldsp_bfin.h
@@ -0,0 +1,50 @@
+/*
+ * BlackFin DSPUTILS COMMON OPTIMIZATIONS HEADER
+ *
+ * Copyright (C) 2007 Marc Hoffman <mmh@pleasantst.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
+ */
+
+
+#ifndef AVCODEC_BFIN_HPELDSP_BFIN_H
+#define AVCODEC_BFIN_HPELDSP_BFIN_H
+
+#include <stdint.h>
+
+#include "config.h"
+
+#if defined(__FDPIC__) && CONFIG_SRAM
+#define attribute_l1_text  __attribute__ ((l1_text))
+#define attribute_l1_data_b __attribute__((l1_data_B))
+#else
+#define attribute_l1_text
+#define attribute_l1_data_b
+#endif
+
+void ff_bfin_z_put_pixels16_xy2     (uint8_t *block, const uint8_t *s0, int dest_size, int line_size, int h) attribute_l1_text;
+void ff_bfin_z_put_pixels8_xy2      (uint8_t *block, const uint8_t *s0, int dest_size, int line_size, int h) attribute_l1_text;
+void ff_bfin_put_pixels16_xy2_nornd (uint8_t *block, const uint8_t *s0, int line_size, int h) attribute_l1_text;
+void ff_bfin_put_pixels8_xy2_nornd  (uint8_t *block, const uint8_t *s0, int line_size, int h) attribute_l1_text;
+
+
+void ff_bfin_put_pixels8uc        (uint8_t *block, const uint8_t *s0, const uint8_t *s1, int dest_size, int line_size, int h) attribute_l1_text;
+void ff_bfin_put_pixels16uc       (uint8_t *block, const uint8_t *s0, const uint8_t *s1, int dest_size, int line_size, int h) attribute_l1_text;
+void ff_bfin_put_pixels8uc_nornd  (uint8_t *block, const uint8_t *s0, const uint8_t *s1, int line_size, int h) attribute_l1_text;
+void ff_bfin_put_pixels16uc_nornd (uint8_t *block, const uint8_t *s0, const uint8_t *s1, int line_size, int h) attribute_l1_text;
+
+#endif /* AVCODEC_BFIN_HPELDSP_BFIN_H */
diff --git a/libavcodec/bfin/pixels_bfin.S b/libavcodec/bfin/pixels_bfin.S
index 2e3cd0f..2c84deb 100644
--- a/libavcodec/bfin/pixels_bfin.S
+++ b/libavcodec/bfin/pixels_bfin.S
@@ -83,364 +83,6 @@
     RTS;
 DEFUN_END(add_pixels_clamped)
 
-
-/**
-  motion compensation
-  primitives
-
-     * Halfpel motion compensation with rounding (a+b+1)>>1.
-     * This is an array[4][4] of motion compensation funcions for 4
-     * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
-     * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
-     * @param block destination where the result is stored
-     * @param pixels source
-     * @param line_size number of bytes in a horizontal line of block
-     * @param h height
-
-*/
-
-DEFUN(put_pixels8uc,mL1,
-        (uint8_t *block, const uint8_t *s0, const uint8_t *s1,
-                 int dest_size, int line_size, int h)):
-        i3=r0;        // dest
-        i0=r1;        // src0
-        i1=r2;        // src1
-        r0=[sp+12];   // dest_size
-        r2=[sp+16];   // line_size
-        p0=[sp+20];   // h
-        [--sp] = (r7:6);
-        r0+=-4;
-        m3=r0;
-        r2+=-8;
-        m0=r2;
-        LSETUP(pp8$0,pp8$1) LC0=P0;
-        DISALGNEXCPT                || R0 = [I0++]  || R2  =[I1++];
-
-pp8$0:  DISALGNEXCPT                || R1 = [I0++]  || R3  =[I1++];
-        R6 = BYTEOP1P(R1:0,R3:2)    || R0 = [I0++M0]|| R2  =[I1++M0];
-        R7 = BYTEOP1P(R1:0,R3:2)(R) || R0 = [I0++]  || [I3++] = R6 ;
-pp8$1:  DISALGNEXCPT                || R2 = [I1++]  || [I3++M3] = R7;
-
-        (r7:6) = [sp++];
-        RTS;
-DEFUN_END(put_pixels8uc)
-
-DEFUN(put_pixels16uc,mL1,
-        (uint8_t *block, const uint8_t *s0, const uint8_t *s1,
-                 int dest_size, int line_size, int h)):
-        link 0;
-        [--sp] = (r7:6);
-        i3=r0;        // dest
-        i0=r1;        // src0
-        i1=r2;        // src1
-        r0=[fp+20];   // dest_size
-        r2=[fp+24];   // line_size
-        p0=[fp+28];   // h
-
-
-        r0+=-12;
-        m3=r0;        // line_size
-        r2+=-16;
-        m0=r2;
-
-        LSETUP(pp16$0,pp16$1) LC0=P0;
-         DISALGNEXCPT                || R0 = [I0++]   || R2  =[I1++];
-
-pp16$0:  DISALGNEXCPT                || R1 = [I0++]   || R3  =[I1++];
-         R6 = BYTEOP1P(R1:0,R3:2)    || R0 = [I0++]   || R2  =[I1++];
-         R7 = BYTEOP1P(R1:0,R3:2)(R) || R1 = [I0++]   || R3  =[I1++];
-         [I3++] = R6;
-         R6 = BYTEOP1P(R1:0,R3:2)    || R0 = [I0++M0] || R2  =[I1++M0];
-         R7 = BYTEOP1P(R1:0,R3:2)(R) || R0 = [I0++]   || [I3++] = R7 ;
-         [I3++] = R6;
-pp16$1:  DISALGNEXCPT                || R2 = [I1++]   || [I3++M3] = R7;
-
-        (r7:6) = [sp++];
-        unlink;
-        RTS;
-DEFUN_END(put_pixels16uc)
-
-
-
-
-
-
-DEFUN(put_pixels8uc_nornd,mL1,
-        (uint8_t *block, const uint8_t *s0, const uint8_t *s1,
-                 int line_size, int h)):
-        i3=r0;        // dest
-        i0=r1;        // src0
-        i1=r2;        // src1
-        r2=[sp+12];   // line_size
-        p0=[sp+16];   // h
-        [--sp] = (r7:6);
-        r2+=-4;
-        m3=r2;
-        r2+=-4;
-        m0=r2;
-        LSETUP(pp8$2,pp8$3) LC0=P0;
-        DISALGNEXCPT                || R0 = [I0++]  || R2  =[I1++];
-
-pp8$2:  DISALGNEXCPT                || R1 = [I0++]  || R3  =[I1++];
-        R6 = BYTEOP1P(R1:0,R3:2)(T)  || R0 = [I0++M0]|| R2  =[I1++M0];
-        R7 = BYTEOP1P(R1:0,R3:2)(T,R) || R0 = [I0++]  || [I3++] = R6 ;
-pp8$3:  DISALGNEXCPT                || R2 = [I1++]  || [I3++M3] = R7;
-
-        (r7:6) = [sp++];
-        RTS;
-DEFUN_END(put_pixels8uc_nornd)
-
-DEFUN(put_pixels16uc_nornd,mL1,
-        (uint8_t *block, const uint8_t *s0, const uint8_t *s1,
-                 int line_size, int h)):
-        i3=r0;        // dest
-        i0=r1;        // src0
-        i1=r2;        // src1
-        r2=[sp+12];   // line_size
-        p0=[sp+16];   // h
-
-        [--sp] = (r7:6);
-        r2+=-12;
-        m3=r2;        // line_size
-        r2+=-4;
-        m0=r2;
-
-        LSETUP(pp16$2,pp16$3) LC0=P0;
-        DISALGNEXCPT                || R0 = [I0++]   || R2  =[I1++];
-
-pp16$2:
-        DISALGNEXCPT                || R1 = [I0++]   || R3  =[I1++];
-        R6 = BYTEOP1P(R1:0,R3:2)(T)    || R0 = [I0++]   || R2  =[I1++];
-        R7 = BYTEOP1P(R1:0,R3:2)(T,R) || R1 = [I0++]   || R3  =[I1++];
-        [I3++] = R6;
-
-        R6 = BYTEOP1P(R1:0,R3:2)(T)    || R0 = [I0++M0] || R2  =[I1++M0];
-        R7 = BYTEOP1P(R1:0,R3:2)(T,R) || R0 = [I0++]   || [I3++] = R7 ;
-        [I3++] = R6;
-pp16$3: DISALGNEXCPT                || R2 = [I1++]   || [I3++M3] = R7;
-
-        (r7:6) = [sp++];
-
-        RTS;
-DEFUN_END(put_pixels16uc_nornd)
-
-DEFUN(z_put_pixels16_xy2,mL1,
-        (uint8_t *block, const uint8_t *s0,
-                 int dest_size, int line_size, int h)):
-        link 0;
-        [--sp] = (r7:4);
-        i3=r0;        // dest
-        i0=r1;        // src0--> pixels
-        i1=r1;        // src1--> pixels + line_size
-        r2+=-12;
-        m2=r2;        // m2=dest_width-4
-        r2=[fp+20];
-        m3=r2;        // line_size
-        p0=[fp+24];   // h
-        r2+=-16;
-        i1+=m3;       /* src1 + line_size */
-        m0=r2;        /* line-size - 20 */
-
-        B0 = I0;
-        B1 = I1;
-        B3 = I3;
-
-        DISALGNEXCPT                       || R0 = [I0++] || R2  =[I1++];
-
-        LSETUP(LS$16E,LE$16E) LC0=P0;
-LS$16E: DISALGNEXCPT                       || R1 = [I0++] || R3  =[I1++];
-        R4 = BYTEOP2P (R3:2,R1:0) (RNDL)   || R0 = [I0++] || R2  =[I1++];
-        R5 = BYTEOP2P (R3:2,R1:0) (RNDL,R) || R1 = [I0++] || [I3++] = R4 ;
-        DISALGNEXCPT                       || R3 = [I1++] || [I3++] = R5;
-        R4 = BYTEOP2P (R3:2,R1:0) (RNDL)   || R0 = [I0++M0]|| R2  = [I1++M0];
-        R5 = BYTEOP2P (R3:2,R1:0) (RNDL,R) || R0 = [I0++] || [I3++] = R4 ;
-LE$16E: DISALGNEXCPT                       || R2 = [I1++] || [I3++M2] = R5;
-
-        M1 = 1;
-        I3 = B3;
-        I1 = B1;
-        I0 = B0;
-
-        I0 += M1;
-        I1 += M1;
-
-        DISALGNEXCPT                       || R0 = [I0++] || R2  =[I1++];
-        LSETUP(LS$16O,LE$16O) LC0=P0;
-LS$16O: DISALGNEXCPT                       || R1 = [I0++] || R3  =[I1++];
-        R4 = BYTEOP2P (R3:2,R1:0) (RNDH)   || R0 = [I0++] || R2  =[I1++];
-        R5 = BYTEOP2P (R3:2,R1:0) (RNDH,R) || R1 = [I0++] || R6  =[I3++];
-        R4 = R4 +|+ R6                       || R7 = [I3--];
-        R5 = R5 +|+ R7                       || [I3++] = R4;
-        DISALGNEXCPT                       || R3  =[I1++] || [I3++] = R5;
-        R4 = BYTEOP2P (R3:2,R1:0) (RNDH)   || R0 = [I0++M0]|| R2  = [I1++M0];
-        R5 = BYTEOP2P (R3:2,R1:0) (RNDH,R) || R0 = [I0++] || R6 = [I3++];
-        R4 = R4 +|+ R6                       || R7 = [I3--];
-        R5 = R5 +|+ R7                       || [I3++] = R4;
-LE$16O: DISALGNEXCPT                       || R2 = [I1++] || [I3++M2] = R5;
-
-        (r7:4) = [sp++];
-        unlink;
-        rts;
-DEFUN_END(z_put_pixels16_xy2)
-
-DEFUN(put_pixels16_xy2_nornd,mL1,
-        (uint8_t *block, const uint8_t *s0,
-                 int line_size, int h)):
-        link 0;
-        [--sp] = (r7:4);
-        i3=r0;        // dest
-        i0=r1;        // src0--> pixels
-        i1=r1;        // src1--> pixels + line_size
-        m3=r2;
-        r2+=-12;
-        m2=r2;
-        r2+=-4;
-        i1+=m3;       /* src1 + line_size */
-        m0=r2;        /* line-size - 20 */
-        p0=[fp+20];   // h
-
-        B0=I0;
-        B1=I1;
-        B3=I3;
-
-        DISALGNEXCPT                       || R0 = [I0++] || R2  =[I1++];
-
-        LSETUP(LS$16ET,LE$16ET) LC0=P0;
-LS$16ET:DISALGNEXCPT                       || R1 = [I0++] || R3  =[I1++];
-        R4 = BYTEOP2P (R3:2,R1:0) (TL)     || R0 = [I0++] || R2  =[I1++];
-        R5 = BYTEOP2P (R3:2,R1:0) (TL,R)   || R1 = [I0++] || [I3++] = R4 ;
-        DISALGNEXCPT                       || R3 = [I1++] || [I3++] = R5;
-        R4 = BYTEOP2P (R3:2,R1:0) (TL)     || R0 = [I0++M0]|| R2  = [I1++M0];
-        R5 = BYTEOP2P (R3:2,R1:0) (TL,R)   || R0 = [I0++] || [I3++] = R4 ;
-LE$16ET:DISALGNEXCPT                       || R2 = [I1++] || [I3++M2] = R5;
-
-        M1 = 1;
-        I3=B3;
-        I1=B1;
-        I0=B0;
-
-        I0 += M1;
-        I1 += M1;
-
-        DISALGNEXCPT                       || R0 = [I0++] || R2  =[I1++];
-        LSETUP(LS$16OT,LE$16OT) LC0=P0;
-LS$16OT:DISALGNEXCPT                       || R1 = [I0++] || R3  =[I1++];
-        R4 = BYTEOP2P (R3:2,R1:0) (TH)     || R0 = [I0++] || R2  =[I1++];
-        R5 = BYTEOP2P (R3:2,R1:0) (TH,R)   || R1 = [I0++] || R6  =[I3++];
-        R4 = R4 +|+ R6                                    || R7 = [I3--];
-        R5 = R5 +|+ R7                                    || [I3++] = R4;
-        DISALGNEXCPT                       || R3  =[I1++] || [I3++] = R5;
-        R4 = BYTEOP2P (R3:2,R1:0) (TH)     || R0 = [I0++M0]|| R2  = [I1++M0];
-        R5 = BYTEOP2P (R3:2,R1:0) (TH,R)   || R0 = [I0++] || R6 = [I3++];
-        R4 = R4 +|+ R6                                    || R7 = [I3--];
-        R5 = R5 +|+ R7                                    || [I3++] = R4;
-LE$16OT:DISALGNEXCPT                       || R2 = [I1++] || [I3++M2] = R5;
-
-        (r7:4) = [sp++];
-        unlink;
-        rts;
-DEFUN_END(put_pixels16_xy2_nornd)
-
-DEFUN(z_put_pixels8_xy2,mL1,
-        (uint8_t *block, const uint8_t *s0,
-                 int dest_size, int line_size, int h)):
-        link 0;
-        [--sp] = (r7:4);
-        i3=r0;        // dest
-        i0=r1;        // src0--> pixels
-        i1=r1;        // src1--> pixels + line_size
-        r2+=-4;
-        m2=r2;        // m2=dest_width-4
-        r2=[fp+20];
-        m3=r2;        // line_size
-        p0=[fp+24];   // h
-        r2+=-8;
-        i1+=m3;       /* src1 + line_size */
-        m0=r2;        /* line-size - 20 */
-
-        b0 = I0;
-        b1 = I1;
-        b3 = I3;
-
-        LSETUP(LS$8E,LE$8E) LC0=P0;
-        DISALGNEXCPT                       || R0 = [I0++]   || R2  =[I1++];
-LS$8E:  DISALGNEXCPT                       || R1 = [I0++]   || R3  =[I1++];
-        R4 = BYTEOP2P (R3:2,R1:0) (RNDL)   || R0 = [I0++M0] || R2  =[I1++M0];
-        R5 = BYTEOP2P (R3:2,R1:0) (RNDL,R) || R0 = [I0++]   || [I3++] = R4 ;
-LE$8E:  DISALGNEXCPT                       || R2 = [I1++]   || [I3++M2] = R5;
-
-        M1 = 1;
-        I3 = b3;
-        I1 = b1;
-        I0 = b0;
-
-        I0 += M1;
-        I1 += M1;
-
-        LSETUP(LS$8O,LE$8O) LC0=P0;
-        DISALGNEXCPT                       || R0 = [I0++]   || R2  =[I1++];
-LS$8O:  DISALGNEXCPT                       || R1 = [I0++]   || R3  =[I1++];
-        R4 = BYTEOP2P (R3:2,R1:0) (RNDH)   || R0 = [I0++M0] || R2  =[I1++M0];
-        R5 = BYTEOP2P (R3:2,R1:0) (RNDH,R) || R0 = [I0++]   || R6  =[I3++];
-        R4 = R4 +|+ R6                                      || R7 = [I3--];
-        R5 = R5 +|+ R7                                      || [I3++] = R4;
-LE$8O:  DISALGNEXCPT                       || R2  =[I1++]   || [I3++M2] = R5;
-
-        (r7:4) = [sp++];
-        unlink;
-        rts;
-DEFUN_END(z_put_pixels8_xy2)
-
-DEFUN(put_pixels8_xy2_nornd,mL1,
-        (uint8_t *block, const uint8_t *s0, int line_size, int h)):
-        link 0;
-        [--sp] = (r7:4);
-        i3=r0;        // dest
-        i0=r1;        // src0--> pixels
-        i1=r1;        // src1--> pixels + line_size
-        m3=r2;
-        r2+=-4;
-        m2=r2;
-        r2+=-4;
-        i1+=m3;       /* src1 + line_size */
-        m0=r2;        /* line-size - 20 */
-        p0=[fp+20];   // h
-
-
-        b0 = I0;
-        b1 = I1;
-        b3 = I3;
-
-        LSETUP(LS$8ET,LE$8ET) LC0=P0;
-        DISALGNEXCPT                       || R0 = [I0++]   || R2  =[I1++];
-
-LS$8ET: DISALGNEXCPT                       || R1 = [I0++]   || R3 = [I1++];
-        R4 = BYTEOP2P (R3:2,R1:0) (TL)     || R0 = [I0++M0] || R2 = [I1++M0];
-        R5 = BYTEOP2P (R3:2,R1:0) (TL,R)   || R0 = [I0++]   || [I3++] = R4 ;
-LE$8ET: DISALGNEXCPT                       || R2 = [I1++]   || [I3++M2] = R5;
-
-        M1 = 1;
-        I3 = b3;
-        I1 = b1;
-        I0 = b0;
-
-        I0 += M1;
-        I1 += M1;
-
-        LSETUP(LS$8OT,LE$8OT) LC0=P0;
-        DISALGNEXCPT                       || R0 = [I0++]   || R2 = [I1++];
-
-LS$8OT: DISALGNEXCPT                       || R1 = [I0++]   || R3 = [I1++];
-        R4 = BYTEOP2P (R3:2,R1:0) (TH)     || R0 = [I0++M0] || R2 = [I1++M0];
-        R5 = BYTEOP2P (R3:2,R1:0) (TH,R)   || R0 = [I0++]   || R6 = [I3++];
-        R4 = R4 +|+ R6                                      || R7 = [I3--];
-        R5 = R5 +|+ R7                                      || [I3++] = R4;
-LE$8OT: DISALGNEXCPT                       || R2  =[I1++]   || [I3++M2] = R5;
-
-        (r7:4) = [sp++];
-        unlink;
-        rts;
-
 DEFUN(diff_pixels,mL1,
        (int16_t *block, uint8_t *s1, uint8_t *s2, int stride)):
         link 0;
@@ -467,7 +109,7 @@
         (r7:4) = [sp++];
         unlink;
         rts;
-DEFUN_END(put_pixels8_xy2_nornd)
+DEFUN_END(diff_pixels)
 
 /*
     for (i = 0; i < 16; i++) {
diff --git a/libavcodec/bfin/vp3_bfin.c b/libavcodec/bfin/vp3_bfin.c
index e823a04..868c431 100644
--- a/libavcodec/bfin/vp3_bfin.c
+++ b/libavcodec/bfin/vp3_bfin.c
@@ -28,31 +28,38 @@
 #include "vp3_bfin.h"
 
 /* Intra iDCT offset 128 */
-void ff_bfin_vp3_idct_put (uint8_t *dest, int line_size, int16_t *block)
+static void bfin_vp3_idct_put(uint8_t *dest, int line_size, int16_t *block)
 {
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP + 128;
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP + 128;
     int i,j;
 
     ff_bfin_vp3_idct (block);
 
     for (i=0;i<8;i++)
         for (j=0;j<8;j++)
-            dest[line_size*i+j]=cm[block[i*8+j]];
+            dest[line_size*i + j] = cm[block[j*8 + i]];
 
     memset(block, 0, 128);
 }
 
 /* Inter iDCT */
-void ff_bfin_vp3_idct_add (uint8_t *dest, int line_size, int16_t *block)
+static void bfin_vp3_idct_add(uint8_t *dest, int line_size, int16_t *block)
 {
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
+    int i, j;
+
     ff_bfin_vp3_idct (block);
-    ff_bfin_add_pixels_clamped (block, dest, line_size);
+    for (i = 0; i < 8; i++)
+        for (j = 0; j < 8; j++)
+            dest[line_size*i + j] = cm[dest[line_size*i + j] + block[j*8 + i]];
 
     memset(block, 0, 128);
 }
 
 av_cold void ff_vp3dsp_init_bfin(VP3DSPContext *c, int flags)
 {
-    c->idct_add = ff_bfin_vp3_idct_add;
-    c->idct_put = ff_bfin_vp3_idct_put;
+    if (!(flags & CODEC_FLAG_BITEXACT)) {
+        c->idct_add = bfin_vp3_idct_add;
+        c->idct_put = bfin_vp3_idct_put;
+    }
 }
diff --git a/libavcodec/bfin/vp3_bfin.h b/libavcodec/bfin/vp3_bfin.h
index f0bf824..e832de7 100644
--- a/libavcodec/bfin/vp3_bfin.h
+++ b/libavcodec/bfin/vp3_bfin.h
@@ -23,7 +23,5 @@
 #include <stdint.h>
 
 void ff_bfin_vp3_idct(int16_t *block);
-void ff_bfin_vp3_idct_put(uint8_t *dest, int line_size, int16_t *block);
-void ff_bfin_vp3_idct_add(uint8_t *dest, int line_size, int16_t *block);
 
 #endif /* AVCODEC_BFIN_VP3_BFIN_H */
diff --git a/libavcodec/bink.c b/libavcodec/bink.c
index 5d000a8..9bab4b9 100644
--- a/libavcodec/bink.c
+++ b/libavcodec/bink.c
@@ -26,6 +26,7 @@
 #include "dsputil.h"
 #include "binkdata.h"
 #include "binkdsp.h"
+#include "hpeldsp.h"
 #include "internal.h"
 #include "mathops.h"
 
@@ -112,8 +113,9 @@
 typedef struct BinkContext {
     AVCodecContext *avctx;
     DSPContext     dsp;
+    HpelDSPContext hdsp;
     BinkDSPContext bdsp;
-    AVFrame        *pic, *last;
+    AVFrame        *last;
     int            version;              ///< internal Bink file version
     int            has_alpha;
     int            swap_planes;
@@ -800,8 +802,8 @@
         memcpy(dst + i*stride, tmp + i*8, 8);
 }
 
-static int binkb_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
-                              int is_key, int is_chroma)
+static int binkb_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb,
+                              int plane_idx, int is_key, int is_chroma)
 {
     int blk, ret;
     int i, j, bx, by;
@@ -815,13 +817,13 @@
     int ybias = is_key ? -15 : 0;
     int qp;
 
-    const int stride = c->pic->linesize[plane_idx];
+    const int stride = frame->linesize[plane_idx];
     int bw = is_chroma ? (c->avctx->width  + 15) >> 4 : (c->avctx->width  + 7) >> 3;
     int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
 
     binkb_init_bundles(c);
-    ref_start = c->pic->data[plane_idx];
-    ref_end   = c->pic->data[plane_idx] + (bh * c->pic->linesize[plane_idx] + bw) * 8;
+    ref_start = frame->data[plane_idx];
+    ref_end   = frame->data[plane_idx] + (bh * frame->linesize[plane_idx] + bw) * 8;
 
     for (i = 0; i < 64; i++)
         coordmap[i] = (i & 7) + (i >> 3) * stride;
@@ -832,7 +834,7 @@
                 return ret;
         }
 
-        dst  = c->pic->data[plane_idx]  + 8*by*stride;
+        dst  = frame->data[plane_idx]  + 8*by*stride;
         for (bx = 0; bx < bw; bx++, dst += 8) {
             blk = binkb_get_value(c, BINKB_SRC_BLOCK_TYPES);
             switch (blk) {
@@ -878,7 +880,7 @@
                 if (ref < ref_start || ref + 8*stride > ref_end) {
                     av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
                 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
-                    c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
+                    c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
                 } else {
                     put_pixels8x8_overlapped(dst, ref, stride);
                 }
@@ -894,7 +896,7 @@
                 if (ref < ref_start || ref + 8 * stride > ref_end) {
                     av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
                 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
-                    c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
+                    c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
                 } else {
                     put_pixels8x8_overlapped(dst, ref, stride);
                 }
@@ -924,7 +926,7 @@
                 if (ref < ref_start || ref + 8 * stride > ref_end) {
                     av_log(c->avctx, AV_LOG_WARNING, "Reference block is out of bounds\n");
                 } else if (ref + 8*stride < dst || ref >= dst + 8*stride) {
-                    c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
+                    c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
                 } else {
                     put_pixels8x8_overlapped(dst, ref, stride);
                 }
@@ -946,8 +948,8 @@
     return 0;
 }
 
-static int bink_decode_plane(BinkContext *c, GetBitContext *gb, int plane_idx,
-                             int is_chroma)
+static int bink_decode_plane(BinkContext *c, AVFrame *frame, GetBitContext *gb,
+                             int plane_idx, int is_chroma)
 {
     int blk, ret;
     int i, j, bx, by;
@@ -960,7 +962,7 @@
     LOCAL_ALIGNED_16(int32_t, dctblock, [64]);
     int coordmap[64];
 
-    const int stride = c->pic->linesize[plane_idx];
+    const int stride = frame->linesize[plane_idx];
     int bw = is_chroma ? (c->avctx->width  + 15) >> 4 : (c->avctx->width  + 7) >> 3;
     int bh = is_chroma ? (c->avctx->height + 15) >> 4 : (c->avctx->height + 7) >> 3;
     int width = c->avctx->width >> is_chroma;
@@ -970,7 +972,7 @@
         read_bundle(gb, c, i);
 
     ref_start = c->last->data[plane_idx] ? c->last->data[plane_idx]
-                                        : c->pic->data[plane_idx];
+                                         : frame->data[plane_idx];
     ref_end   = ref_start
                 + (bw - 1 + c->last->linesize[plane_idx] * (bh - 1)) * 8;
 
@@ -999,9 +1001,9 @@
 
         if (by == bh)
             break;
-        dst  = c->pic->data[plane_idx]  + 8*by*stride;
+        dst  = frame->data[plane_idx]  + 8*by*stride;
         prev = (c->last->data[plane_idx] ? c->last->data[plane_idx]
-                                         : c->pic->data[plane_idx]) + 8*by*stride;
+                                         : frame->data[plane_idx]) + 8*by*stride;
         for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
             blk = get_value(c, BINK_SRC_BLOCK_TYPES);
             // 16x16 block type on odd line means part of the already decoded block, so skip it
@@ -1013,7 +1015,7 @@
             }
             switch (blk) {
             case SKIP_BLOCK:
-                c->dsp.put_pixels_tab[1][0](dst, prev, stride, 8);
+                c->hdsp.put_pixels_tab[1][0](dst, prev, stride, 8);
                 break;
             case SCALED_BLOCK:
                 blk = get_value(c, BINK_SRC_SUB_BLOCK_TYPES);
@@ -1084,7 +1086,7 @@
                            bx*8 + xoff, by*8 + yoff);
                     return AVERROR_INVALIDDATA;
                 }
-                c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
+                c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
                 break;
             case RUN_BLOCK:
                 scan = bink_patterns[get_bits(gb, 4)];
@@ -1118,7 +1120,7 @@
                            bx*8 + xoff, by*8 + yoff);
                     return AVERROR_INVALIDDATA;
                 }
-                c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
+                c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
                 c->dsp.clear_block(block);
                 v = get_bits(gb, 7);
                 read_residue(gb, block, v);
@@ -1143,7 +1145,7 @@
                            bx*8 + xoff, by*8 + yoff);
                     return -1;
                 }
-                c->dsp.put_pixels_tab[1][0](dst, ref, stride, 8);
+                c->hdsp.put_pixels_tab[1][0](dst, ref, stride, 8);
                 memset(dctblock, 0, sizeof(*dctblock) * 64);
                 dctblock[0] = get_value(c, BINK_SRC_INTER_DC);
                 read_dct_coeffs(gb, dctblock, bink_scan, bink_inter_quant, -1);
@@ -1178,30 +1180,26 @@
 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)
 {
     BinkContext * const c = avctx->priv_data;
+    AVFrame *frame = data;
     GetBitContext gb;
     int plane, plane_idx, ret;
     int bits_count = pkt->size << 3;
 
     if (c->version > 'b') {
-        if(c->pic->data[0])
-            avctx->release_buffer(avctx, c->pic);
-
-        if ((ret = ff_get_buffer(avctx, c->pic)) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
             return ret;
-        }
     } else {
-        if ((ret = avctx->reget_buffer(avctx, c->pic)) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+        if ((ret = ff_reget_buffer(avctx, c->last)) < 0)
             return ret;
-        }
+        if ((ret = av_frame_ref(frame, c->last)) < 0)
+            return ret;
     }
 
     init_get_bits(&gb, pkt->data, bits_count);
     if (c->has_alpha) {
         if (c->version >= 'i')
             skip_bits_long(&gb, 32);
-        if ((ret = bink_decode_plane(c, &gb, 3, 0)) < 0)
+        if ((ret = bink_decode_plane(c, frame, &gb, 3, 0)) < 0)
             return ret;
     }
     if (c->version >= 'i')
@@ -1211,10 +1209,10 @@
         plane_idx = (!plane || !c->swap_planes) ? plane : (plane ^ 3);
 
         if (c->version > 'b') {
-            if ((ret = bink_decode_plane(c, &gb, plane_idx, !!plane)) < 0)
+            if ((ret = bink_decode_plane(c, frame, &gb, plane_idx, !!plane)) < 0)
                 return ret;
         } else {
-            if ((ret = binkb_decode_plane(c, &gb, plane_idx,
+            if ((ret = binkb_decode_plane(c, frame, &gb, plane_idx,
                                           !avctx->frame_number, !!plane)) < 0)
                 return ret;
         }
@@ -1223,11 +1221,13 @@
     }
     emms_c();
 
-    *got_frame = 1;
-    *(AVFrame*)data = *c->pic;
+    if (c->version > 'b') {
+        av_frame_unref(c->last);
+        if ((ret = av_frame_ref(c->last, frame)) < 0)
+            return ret;
+    }
 
-    if (c->version > 'b')
-        FFSWAP(AVFrame*, c->pic, c->last);
+    *got_frame = 1;
 
     /* always report that the buffer was completely consumed */
     return pkt->size;
@@ -1293,13 +1293,9 @@
     }
     c->avctx = avctx;
 
-    c->pic  = avcodec_alloc_frame();
-    c->last = avcodec_alloc_frame();
-    if (!c->pic || !c->last) {
-        avcodec_free_frame(&c->pic);
-        avcodec_free_frame(&c->last);
+    c->last = av_frame_alloc();
+    if (!c->last)
         return AVERROR(ENOMEM);
-    }
 
     if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
         return ret;
@@ -1307,6 +1303,7 @@
     avctx->pix_fmt = c->has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
 
     ff_dsputil_init(&c->dsp, avctx);
+    ff_hpeldsp_init(&c->hdsp, avctx->flags);
     ff_binkdsp_init(&c->bdsp);
 
     if ((ret = init_bundles(c)) < 0) {
@@ -1328,12 +1325,7 @@
 {
     BinkContext * const c = avctx->priv_data;
 
-    if (c->pic->data[0])
-        avctx->release_buffer(avctx, c->pic);
-    if (c->last->data[0])
-        avctx->release_buffer(avctx, c->last);
-    avcodec_free_frame(&c->pic);
-    avcodec_free_frame(&c->last);
+    av_frame_free(&c->last);
 
     free_bundles(c);
     return 0;
diff --git a/libavcodec/binkaudio.c b/libavcodec/binkaudio.c
index 5ba77a6..ef5569a 100644
--- a/libavcodec/binkaudio.c
+++ b/libavcodec/binkaudio.c
@@ -318,10 +318,8 @@
 
     /* get output buffer */
     frame->nb_samples = s->frame_len;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     if (decode_block(s, (float **)frame->extended_data,
                      avctx->codec->id == AV_CODEC_ID_BINKAUDIO_DCT)) {
diff --git a/libavcodec/bintext.c b/libavcodec/bintext.c
index 1c9bc3e..f8059aa 100644
--- a/libavcodec/bintext.c
+++ b/libavcodec/bintext.c
@@ -33,9 +33,10 @@
 #include "avcodec.h"
 #include "cga_data.h"
 #include "bintext.h"
+#include "internal.h"
 
 typedef struct XbinContext {
-    AVFrame frame;
+    AVFrame *frame;
     int palette[16];
     int flags;
     int font_height;
@@ -91,6 +92,10 @@
         }
     }
 
+    s->frame = av_frame_alloc();
+    if (!s->frame)
+        return AVERROR(ENOMEM);
+
     return 0;
 }
 
@@ -101,10 +106,10 @@
     if (s->y < avctx->height - s->font_height) {
         s->y += s->font_height;
     } else {
-        memmove(s->frame.data[0], s->frame.data[0] + s->font_height*s->frame.linesize[0],
-            (avctx->height - s->font_height)*s->frame.linesize[0]);
-        memset(s->frame.data[0] + (avctx->height - s->font_height)*s->frame.linesize[0],
-            DEFAULT_BG_COLOR, s->font_height * s->frame.linesize[0]);
+        memmove(s->frame->data[0], s->frame->data[0] + s->font_height*s->frame->linesize[0],
+            (avctx->height - s->font_height)*s->frame->linesize[0]);
+        memset(s->frame->data[0] + (avctx->height - s->font_height)*s->frame->linesize[0],
+            DEFAULT_BG_COLOR, s->font_height * s->frame->linesize[0]);
     }
 }
 
@@ -118,8 +123,8 @@
     XbinContext *s = avctx->priv_data;
     if (s->y > avctx->height - s->font_height)
         return;
-    ff_draw_pc_font(s->frame.data[0] + s->y * s->frame.linesize[0] + s->x,
-                    s->frame.linesize[0], s->font, s->font_height, c,
+    ff_draw_pc_font(s->frame->data[0] + s->y * s->frame->linesize[0] + s->x,
+                    s->frame->linesize[0], s->font, s->font_height, c,
                     a & 0x0F, a >> 4);
     s->x += FONT_WIDTH;
     if (s->x > avctx->width - FONT_WIDTH) {
@@ -136,18 +141,14 @@
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     const uint8_t *buf_end = buf+buf_size;
+    int ret;
 
     s->x = s->y = 0;
-    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
-                            FF_BUFFER_HINTS_PRESERVE |
-                            FF_BUFFER_HINTS_REUSABLE;
-    if (avctx->reget_buffer(avctx, &s->frame)) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
-        return -1;
-    }
-    s->frame.pict_type           = AV_PICTURE_TYPE_I;
-    s->frame.palette_has_changed = 1;
-    memcpy(s->frame.data[1], s->palette, 16 * 4);
+    if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
+        return ret;
+    s->frame->pict_type           = AV_PICTURE_TYPE_I;
+    s->frame->palette_has_changed = 1;
+    memcpy(s->frame->data[1], s->palette, 16 * 4);
 
     if (avctx->codec_id == AV_CODEC_ID_XBIN) {
         while (buf + 2 < buf_end) {
@@ -201,8 +202,9 @@
         }
     }
 
+    if ((ret = av_frame_ref(data, s->frame)) < 0)
+        return ret;
     *got_frame      = 1;
-    *(AVFrame*)data = s->frame;
     return buf_size;
 }
 
@@ -210,8 +212,7 @@
 {
     XbinContext *s = avctx->priv_data;
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+    av_frame_free(&s->frame);
 
     return 0;
 }
diff --git a/libavcodec/bitstream.c b/libavcodec/bitstream.c
index 6bcdadb..7e297b9 100644
--- a/libavcodec/bitstream.c
+++ b/libavcodec/bitstream.c
@@ -305,7 +305,17 @@
         GET_DATA(buf[j].bits, bits, i, bits_wrap, bits_size);\
         if (!(condition))\
             continue;\
+        if (buf[j].bits > 3*nb_bits || buf[j].bits>32) {\
+            av_log(NULL, AV_LOG_ERROR, "Too long VLC in init_vlc\n");\
+            av_free(buf);\
+            return -1;\
+        }\
         GET_DATA(buf[j].code, codes, i, codes_wrap, codes_size);\
+        if (buf[j].code >= (1LL<<buf[j].bits)) {\
+            av_log(NULL, AV_LOG_ERROR, "Invalid code in init_vlc\n");\
+            av_free(buf);\
+            return -1;\
+        }\
         if (flags & INIT_VLC_LE)\
             buf[j].code = bitswap_32(buf[j].code);\
         else\
diff --git a/libavcodec/bmp.c b/libavcodec/bmp.c
index fb69396..a3bb1a7 100644
--- a/libavcodec/bmp.c
+++ b/libavcodec/bmp.c
@@ -25,25 +25,13 @@
 #include "internal.h"
 #include "msrledec.h"
 
-static av_cold int bmp_decode_init(AVCodecContext *avctx)
-{
-    BMPContext *s = avctx->priv_data;
-
-    avcodec_get_frame_defaults(&s->picture);
-    avctx->coded_frame = &s->picture;
-
-    return 0;
-}
-
 static int bmp_decode_frame(AVCodecContext *avctx,
                             void *data, int *got_frame,
                             AVPacket *avpkt)
 {
     const uint8_t *buf = avpkt->data;
     int buf_size       = avpkt->size;
-    BMPContext *s      = avctx->priv_data;
-    AVFrame *picture   = data;
-    AVFrame *p         = &s->picture;
+    AVFrame *p         = data;
     unsigned int fsize, hsize;
     int width, height;
     unsigned int depth;
@@ -208,14 +196,8 @@
         return AVERROR_INVALIDDATA;
     }
 
-    if (p->data[0])
-        avctx->release_buffer(avctx, p);
-
-    p->reference = 0;
-    if ((ret = ff_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
         return ret;
-    }
     p->pict_type = AV_PICTURE_TYPE_I;
     p->key_frame = 1;
 
@@ -341,29 +323,15 @@
         }
     }
 
-    *picture = s->picture;
     *got_frame = 1;
 
     return buf_size;
 }
 
-static av_cold int bmp_decode_end(AVCodecContext *avctx)
-{
-    BMPContext* c = avctx->priv_data;
-
-    if (c->picture.data[0])
-        avctx->release_buffer(avctx, &c->picture);
-
-    return 0;
-}
-
 AVCodec ff_bmp_decoder = {
     .name           = "bmp",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_BMP,
-    .priv_data_size = sizeof(BMPContext),
-    .init           = bmp_decode_init,
-    .close          = bmp_decode_end,
     .decode         = bmp_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("BMP (Windows and OS/2 bitmap)"),
diff --git a/libavcodec/bmp.h b/libavcodec/bmp.h
index b24a1fa..fb21090 100644
--- a/libavcodec/bmp.h
+++ b/libavcodec/bmp.h
@@ -24,10 +24,6 @@
 
 #include "avcodec.h"
 
-typedef struct BMPContext {
-    AVFrame picture;
-} BMPContext;
-
 typedef enum {
     BMP_RGB         =0,
     BMP_RLE8        =1,
diff --git a/libavcodec/bmpenc.c b/libavcodec/bmpenc.c
index bda6799..ee23a0c 100644
--- a/libavcodec/bmpenc.c
+++ b/libavcodec/bmpenc.c
@@ -32,11 +32,6 @@
 static const uint32_t rgb444_masks[]  = { 0x0F00, 0x00F0, 0x000F };
 
 static av_cold int bmp_encode_init(AVCodecContext *avctx){
-    BMPContext *s = avctx->priv_data;
-
-    avcodec_get_frame_defaults(&s->picture);
-    avctx->coded_frame = &s->picture;
-
     switch (avctx->pix_fmt) {
     case AV_PIX_FMT_BGRA:
         avctx->bits_per_coded_sample = 32;
@@ -71,15 +66,14 @@
 static int bmp_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
                             const AVFrame *pict, int *got_packet)
 {
-    BMPContext *s = avctx->priv_data;
-    AVFrame * const p = &s->picture;
     int n_bytes_image, n_bytes_per_row, n_bytes, i, n, hsize, ret;
     const uint32_t *pal = NULL;
     uint32_t palette256[256];
     int pad_bytes_per_row, pal_entries = 0, compression = BMP_RGB;
     int bit_count = avctx->bits_per_coded_sample;
     uint8_t *ptr, *buf;
-    *p = *pict;
+    AVFrame * const p = (AVFrame *)pict;
+
     p->pict_type= AV_PICTURE_TYPE_I;
     p->key_frame= 1;
     switch (avctx->pix_fmt) {
@@ -169,7 +163,6 @@
     .name           = "bmp",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_BMP,
-    .priv_data_size = sizeof(BMPContext),
     .init           = bmp_encode_init,
     .encode2        = bmp_encode_frame,
     .pix_fmts       = (const enum AVPixelFormat[]){
diff --git a/libavcodec/bmv.c b/libavcodec/bmv.c
index c480815..0f3f9f3 100644
--- a/libavcodec/bmv.c
+++ b/libavcodec/bmv.c
@@ -44,7 +44,6 @@
 
 typedef struct BMVDecContext {
     AVCodecContext *avctx;
-    AVFrame pic;
 
     uint8_t *frame, frame_base[SCREEN_WIDE * (SCREEN_HIGH + 1)];
     uint32_t pal[256];
@@ -140,7 +139,7 @@
         mode += 1 + advance_mode;
         if (mode >= 4)
             mode -= 3;
-        if (FFABS(dst_end - dst) < len)
+        if (len <= 0 || FFABS(dst_end - dst) < len)
             return AVERROR_INVALIDDATA;
         switch (mode) {
         case 1:
@@ -200,6 +199,7 @@
                         AVPacket *pkt)
 {
     BMVDecContext * const c = avctx->priv_data;
+    AVFrame *frame = data;
     int type, scr_off;
     int i, ret;
     uint8_t *srcptr, *outptr;
@@ -242,34 +242,27 @@
         scr_off = 0;
     }
 
-    if (c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
-
-    c->pic.reference = 3;
-    if ((ret = ff_get_buffer(avctx, &c->pic)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     if (decode_bmv_frame(c->stream, pkt->size - (c->stream - pkt->data), c->frame, scr_off)) {
         av_log(avctx, AV_LOG_ERROR, "Error decoding frame data\n");
         return AVERROR_INVALIDDATA;
     }
 
-    memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
-    c->pic.palette_has_changed = type & BMV_PALETTE;
+    memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
+    frame->palette_has_changed = type & BMV_PALETTE;
 
-    outptr = c->pic.data[0];
+    outptr = frame->data[0];
     srcptr = c->frame;
 
     for (i = 0; i < avctx->height; i++) {
         memcpy(outptr, srcptr, avctx->width);
         srcptr += avctx->width;
-        outptr += c->pic.linesize[0];
+        outptr += frame->linesize[0];
     }
 
     *got_frame = 1;
-    *(AVFrame*)data = c->pic;
 
     /* always report that the buffer was completely consumed */
     return pkt->size;
@@ -292,16 +285,6 @@
     return 0;
 }
 
-static av_cold int decode_end(AVCodecContext *avctx)
-{
-    BMVDecContext *c = avctx->priv_data;
-
-    if (c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
-
-    return 0;
-}
-
 static const int bmv_aud_mults[16] = {
     16512, 8256, 4128, 2064, 1032, 516, 258, 192, 129, 88, 64, 56, 48, 40, 36, 32
 };
@@ -335,10 +318,8 @@
 
     /* get output buffer */
     frame->nb_samples = total_blocks * 32;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     output_samples = (int16_t *)frame->data[0];
 
     for (blocks = 0; blocks < total_blocks; blocks++) {
@@ -363,7 +344,6 @@
     .id             = AV_CODEC_ID_BMV_VIDEO,
     .priv_data_size = sizeof(BMVDecContext),
     .init           = decode_init,
-    .close          = decode_end,
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("Discworld II BMV video"),
diff --git a/libavcodec/brender_pix.c b/libavcodec/brender_pix.c
index 6c63244..b30b882 100644
--- a/libavcodec/brender_pix.c
+++ b/libavcodec/brender_pix.c
@@ -30,25 +30,11 @@
 #include "bytestream.h"
 #include "internal.h"
 
-typedef struct BRPixContext {
-    AVFrame frame;
-} BRPixContext;
-
 typedef struct BRPixHeader {
     int format;
     unsigned int width, height;
 } BRPixHeader;
 
-static av_cold int brpix_init(AVCodecContext *avctx)
-{
-    BRPixContext *s = avctx->priv_data;
-
-    avcodec_get_frame_defaults(&s->frame);
-    avctx->coded_frame = &s->frame;
-
-    return 0;
-}
-
 static int brpix_decode_header(BRPixHeader *out, GetByteContext *pgb)
 {
     unsigned int header_len = bytestream2_get_be32(pgb);
@@ -73,8 +59,7 @@
                               void *data, int *got_frame,
                               AVPacket *avpkt)
 {
-    BRPixContext *s = avctx->priv_data;
-    AVFrame *frame_out = data;
+    AVFrame *frame = data;
 
     int ret;
     GetByteContext gb;
@@ -143,26 +128,21 @@
         return AVERROR_PATCHWELCOME;
     }
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
-
     if (av_image_check_size(hdr.width, hdr.height, 0, avctx) < 0)
         return AVERROR_INVALIDDATA;
 
     if (hdr.width != avctx->width || hdr.height != avctx->height)
         avcodec_set_dimensions(avctx, hdr.width, hdr.height);
 
-    if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     chunk_type = bytestream2_get_be32(&gb);
 
     if (avctx->pix_fmt == AV_PIX_FMT_PAL8 &&
         (chunk_type == 0x3 || chunk_type == 0x3d)) {
         BRPixHeader palhdr;
-        uint32_t *pal_out = (uint32_t *)s->frame.data[1];
+        uint32_t *pal_out = (uint32_t *)frame->data[1];
         int i;
 
         ret = brpix_decode_header(&palhdr, &gb);
@@ -190,17 +170,17 @@
         }
         bytestream2_skip(&gb, 8);
 
-        s->frame.palette_has_changed = 1;
+        frame->palette_has_changed = 1;
 
         chunk_type = bytestream2_get_be32(&gb);
     } else if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
-        uint32_t *pal_out = (uint32_t *)s->frame.data[1];
+        uint32_t *pal_out = (uint32_t *)frame->data[1];
         int i;
 
         for (i = 0; i < 256; ++i) {
             *pal_out++ = (0xFFU << 24) | (i * 0x010101);
         }
-        s->frame.palette_has_changed = 1;
+        frame->palette_has_changed = 1;
     }
 
     data_len = bytestream2_get_be32(&gb);
@@ -218,35 +198,21 @@
             return AVERROR_INVALIDDATA;
         }
 
-        av_image_copy_plane(s->frame.data[0], s->frame.linesize[0],
+        av_image_copy_plane(frame->data[0], frame->linesize[0],
                             avpkt->data + bytestream2_tell(&gb),
                             bytes_per_scanline,
                             bytes_per_scanline, hdr.height);
     }
 
-    *frame_out = s->frame;
     *got_frame = 1;
 
     return avpkt->size;
 }
 
-static av_cold int brpix_end(AVCodecContext *avctx)
-{
-    BRPixContext *s = avctx->priv_data;
-
-    if(s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
-
-    return 0;
-}
-
 AVCodec ff_brender_pix_decoder = {
     .name           = "brender_pix",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_BRENDER_PIX,
-    .priv_data_size = sizeof(BRPixContext),
-    .init           = brpix_init,
-    .close          = brpix_end,
     .decode         = brpix_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("BRender PIX image"),
diff --git a/libavcodec/c93.c b/libavcodec/c93.c
index e5f371b..c51f2fa 100644
--- a/libavcodec/c93.c
+++ b/libavcodec/c93.c
@@ -21,6 +21,7 @@
 
 #include "avcodec.h"
 #include "bytestream.h"
+#include "internal.h"
 
 typedef struct {
     AVFrame pictures[2];
@@ -47,11 +48,10 @@
 
 static av_cold int decode_init(AVCodecContext *avctx)
 {
-    C93DecoderContext * const c93 = avctx->priv_data;
-
-    avcodec_get_frame_defaults(&c93->pictures[0]);
-    avcodec_get_frame_defaults(&c93->pictures[1]);
+    C93DecoderContext *s = avctx->priv_data;
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
+    avcodec_get_frame_defaults(&s->pictures[0]);
+    avcodec_get_frame_defaults(&s->pictures[1]);
     return 0;
 }
 
@@ -59,10 +59,9 @@
 {
     C93DecoderContext * const c93 = avctx->priv_data;
 
-    if (c93->pictures[0].data[0])
-        avctx->release_buffer(avctx, &c93->pictures[0]);
-    if (c93->pictures[1].data[0])
-        avctx->release_buffer(avctx, &c93->pictures[1]);
+    av_frame_unref(&c93->pictures[0]);
+    av_frame_unref(&c93->pictures[1]);
+
     return 0;
 }
 
@@ -124,20 +123,14 @@
     C93DecoderContext * const c93 = avctx->priv_data;
     AVFrame * const newpic = &c93->pictures[c93->currentpic];
     AVFrame * const oldpic = &c93->pictures[c93->currentpic^1];
-    AVFrame *picture = data;
     GetByteContext gb;
     uint8_t *out;
     int stride, ret, i, x, y, b, bt = 0;
 
     c93->currentpic ^= 1;
 
-    newpic->reference = 3;
-    newpic->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
-                         FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
-    if ((ret = avctx->reget_buffer(avctx, newpic)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, newpic)) < 0)
         return ret;
-    }
 
     stride = newpic->linesize[0];
 
@@ -175,7 +168,14 @@
             case C93_4X4_FROM_PREV:
                 for (j = 0; j < 8; j += 4) {
                     for (i = 0; i < 8; i += 4) {
-                        offset = bytestream2_get_le16(&gb);
+                        int offset = bytestream2_get_le16(&gb);
+                        int from_x = offset % WIDTH;
+                        int from_y = offset / WIDTH;
+                        if (block_type == C93_4X4_FROM_CURR && from_y == y+j &&
+                            (FFABS(from_x - x-i) < 4 || FFABS(from_x - x-i) > WIDTH-4)) {
+                            avpriv_request_sample(avctx, "block overlap %d %d %d %d\n", from_x, x+i, from_y, y+j);
+                            return AVERROR_INVALIDDATA;
+                        }
                         if ((ret = copy_block(avctx, &out[j*stride+i],
                                               copy_from, offset, 4, stride)) < 0)
                             return ret;
@@ -243,7 +243,8 @@
             memcpy(newpic->data[1], oldpic->data[1], 256 * 4);
     }
 
-    *picture = *newpic;
+    if ((ret = av_frame_ref(data, newpic)) < 0)
+        return ret;
     *got_frame = 1;
 
     return buf_size;
diff --git a/libavcodec/cabac.c b/libavcodec/cabac.c
index 385721f..187b7dc 100644
--- a/libavcodec/cabac.c
+++ b/libavcodec/cabac.c
@@ -137,7 +137,8 @@
     c->range= 0x1FE;
 }
 
-void ff_init_cabac_states(){
+void ff_init_cabac_states(void)
+{
     int i, j;
 
     for(i=0; i<64; i++){
diff --git a/libavcodec/cavs.c b/libavcodec/cavs.c
index adf0eca..12af0c9 100644
--- a/libavcodec/cavs.c
+++ b/libavcodec/cavs.c
@@ -276,7 +276,7 @@
     int x, y, ia;
     int ih = 0;
     int iv = 0;
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
 
     for (x = 0; x < 4; x++) {
         ih += (x + 1) * (top [5 + x] - top [3 - x]);
@@ -738,9 +738,9 @@
     h->avctx = avctx;
     avctx->pix_fmt= AV_PIX_FMT_YUV420P;
 
-    h->cur.f    = avcodec_alloc_frame();
-    h->DPB[0].f = avcodec_alloc_frame();
-    h->DPB[1].f = avcodec_alloc_frame();
+    h->cur.f    = av_frame_alloc();
+    h->DPB[0].f = av_frame_alloc();
+    h->DPB[1].f = av_frame_alloc();
     if (!h->cur.f || !h->DPB[0].f || !h->DPB[1].f) {
         ff_cavs_end(avctx);
         return AVERROR(ENOMEM);
@@ -771,15 +771,9 @@
 av_cold int ff_cavs_end(AVCodecContext *avctx) {
     AVSContext *h = avctx->priv_data;
 
-    if (h->cur.f->data[0])
-        avctx->release_buffer(avctx, h->cur.f);
-    if (h->DPB[0].f->data[0])
-        avctx->release_buffer(avctx, h->DPB[0].f);
-    if (h->DPB[1].f->data[0])
-        avctx->release_buffer(avctx, h->DPB[1].f);
-    avcodec_free_frame(&h->cur.f);
-    avcodec_free_frame(&h->DPB[0].f);
-    avcodec_free_frame(&h->DPB[1].f);
+    av_frame_free(&h->cur.f);
+    av_frame_free(&h->DPB[0].f);
+    av_frame_free(&h->DPB[1].f);
 
     av_free(h->top_qp);
     av_free(h->top_mv[0]);
diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c
index fa60d6c..ec4a819 100644
--- a/libavcodec/cavsdec.c
+++ b/libavcodec/cavsdec.c
@@ -948,6 +948,8 @@
     int ret;
     enum cavs_mb mb_type;
 
+    av_frame_unref(h->cur.f);
+
     skip_bits(&h->gb, 16);//bbv_dwlay
     if (h->stc == PIC_PB_START_CODE) {
         h->cur.f->pict_type = get_bits(&h->gb, 2) + AV_PICTURE_TYPE_I;
@@ -973,11 +975,10 @@
         if (h->stream_revision > 0)
             skip_bits(&h->gb, 1); //marker_bit
     }
-    /* release last B frame */
-    if (h->cur.f->data[0])
-        h->avctx->release_buffer(h->avctx, h->cur.f);
 
-    if ((ret = ff_get_buffer(h->avctx, h->cur.f)) < 0)
+    if ((ret = ff_get_buffer(h->avctx, h->cur.f,
+                             h->cur.f->pict_type == AV_PICTURE_TYPE_B ?
+                             0 : AV_GET_BUFFER_FLAG_REF)) < 0)
         return ret;
 
     if (!h->edge_emu_buffer) {
@@ -1075,8 +1076,7 @@
         } while (ff_cavs_next_mb(h));
     }
     if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
-        if (h->DPB[1].f->data[0])
-            h->avctx->release_buffer(h->avctx, h->DPB[1].f);
+        av_frame_unref(h->DPB[1].f);
         FFSWAP(AVSFrame, h->cur, h->DPB[1]);
         FFSWAP(AVSFrame, h->DPB[0], h->DPB[1]);
     }
@@ -1101,7 +1101,8 @@
     width  = get_bits(&h->gb, 14);
     height = get_bits(&h->gb, 14);
     if ((h->width || h->height) && (h->width != width || h->height != height)) {
-        av_log_missing_feature(h->avctx, "Width/height changing in CAVS", 0);
+        avpriv_report_missing_feature(h->avctx,
+                                      "Width/height changing in CAVS");
         return AVERROR_PATCHWELCOME;
     }
     if (width <= 0 || height <= 0) {
@@ -1142,19 +1143,15 @@
     AVSContext *h      = avctx->priv_data;
     const uint8_t *buf = avpkt->data;
     int buf_size       = avpkt->size;
-    AVFrame *picture   = data;
     uint32_t stc       = -1;
-    int input_size;
+    int input_size, ret;
     const uint8_t *buf_end;
     const uint8_t *buf_ptr;
 
     if (buf_size == 0) {
         if (!h->low_delay && h->DPB[0].f->data[0]) {
             *got_frame = 1;
-            *picture = *h->DPB[0].f;
-            if (h->cur.f->data[0])
-                avctx->release_buffer(avctx, h->cur.f);
-            FFSWAP(AVSFrame, h->cur, h->DPB[0]);
+            av_frame_move_ref(data, h->DPB[0].f);
         }
         return 0;
     }
@@ -1162,7 +1159,7 @@
     buf_ptr = buf;
     buf_end = buf + buf_size;
     for(;;) {
-        buf_ptr = avpriv_mpv_find_start_code(buf_ptr, buf_end, &stc);
+        buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &stc);
         if ((stc & 0xFFFFFE00) || buf_ptr == buf_end)
             return FFMAX(0, buf_ptr - buf);
         input_size = (buf_end - buf_ptr) * 8;
@@ -1173,10 +1170,8 @@
             break;
         case PIC_I_START_CODE:
             if (!h->got_keyframe) {
-                if(h->DPB[0].f->data[0])
-                    avctx->release_buffer(avctx, h->DPB[0].f);
-                if(h->DPB[1].f->data[0])
-                    avctx->release_buffer(avctx, h->DPB[1].f);
+                av_frame_unref(h->DPB[0].f);
+                av_frame_unref(h->DPB[1].f);
                 h->got_keyframe = 1;
             }
         case PIC_PB_START_CODE:
@@ -1192,12 +1187,14 @@
             *got_frame = 1;
             if (h->cur.f->pict_type != AV_PICTURE_TYPE_B) {
                 if (h->DPB[1].f->data[0]) {
-                    *picture = *h->DPB[1].f;
+                    if ((ret = av_frame_ref(data, h->DPB[1].f)) < 0)
+                        return ret;
                 } else {
                     *got_frame = 0;
                 }
-            } else
-                *picture = *h->cur.f;
+            } else {
+                av_frame_move_ref(data, h->cur.f);
+            }
             break;
         case EXT_START_CODE:
             //mpeg_decode_extension(avctx, buf_ptr, input_size);
diff --git a/libavcodec/cavsdsp.c b/libavcodec/cavsdsp.c
index f66cabc..653b112 100644
--- a/libavcodec/cavsdsp.c
+++ b/libavcodec/cavsdsp.c
@@ -186,7 +186,7 @@
 static void cavs_idct8_add_c(uint8_t *dst, int16_t *block, int stride) {
     int i;
     int16_t (*src)[8] = (int16_t(*)[8])block;
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
 
     src[0][0] += 8;
 
@@ -261,7 +261,7 @@
 #define CAVS_SUBPIX(OPNAME, OP, NAME, A, B, C, D, E, F) \
 static void OPNAME ## cavs_filt8_h_ ## NAME(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
     const int h=8;\
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
     int i;\
     for(i=0; i<h; i++)\
     {\
@@ -280,7 +280,7 @@
 \
 static void OPNAME ## cavs_filt8_v_  ## NAME(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
     const int w=8;\
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
     int i;\
     for(i=0; i<w; i++)\
     {\
@@ -334,7 +334,7 @@
     int16_t *tmp = temp;\
     const int h=8;\
     const int w=8;\
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
     int i;\
     src1 -= 2*srcStride;\
     for(i=0; i<h+5; i++)\
@@ -421,63 +421,78 @@
 }\
 
 #define CAVS_MC(OPNAME, SIZE) \
-static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc10_c(uint8_t *dst, uint8_t *src, int stride){\
+static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc10_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## cavs_filt ## SIZE ## _h_qpel_l(dst, src, stride, stride);\
 }\
 \
-static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc20_c(uint8_t *dst, uint8_t *src, int stride){\
+static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc20_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## cavs_filt ## SIZE ## _h_hpel(dst, src, stride, stride);\
 }\
 \
-static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc30_c(uint8_t *dst, uint8_t *src, int stride){\
+static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc30_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## cavs_filt ## SIZE ## _h_qpel_r(dst, src, stride, stride);\
 }\
 \
-static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc01_c(uint8_t *dst, uint8_t *src, int stride){\
+static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc01_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## cavs_filt ## SIZE ## _v_qpel_l(dst, src, stride, stride);\
 }\
 \
-static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc02_c(uint8_t *dst, uint8_t *src, int stride){\
+static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc02_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## cavs_filt ## SIZE ## _v_hpel(dst, src, stride, stride);\
 }\
 \
-static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc03_c(uint8_t *dst, uint8_t *src, int stride){\
+static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc03_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## cavs_filt ## SIZE ## _v_qpel_r(dst, src, stride, stride);\
 }\
 \
-static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc22_c(uint8_t *dst, uint8_t *src, int stride){\
+static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc22_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
   OPNAME ## cavs_filt ## SIZE ## _hv_jj(dst, src, NULL, stride, stride); \
 }\
 \
-static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc11_c(uint8_t *dst, uint8_t *src, int stride){\
+static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc11_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
   OPNAME ## cavs_filt ## SIZE ## _hv_egpr(dst, src, src, stride, stride); \
 }\
 \
-static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc13_c(uint8_t *dst, uint8_t *src, int stride){\
+static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc13_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
   OPNAME ## cavs_filt ## SIZE ## _hv_egpr(dst, src, src+stride, stride, stride); \
 }\
 \
-static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc31_c(uint8_t *dst, uint8_t *src, int stride){\
+static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc31_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
   OPNAME ## cavs_filt ## SIZE ## _hv_egpr(dst, src, src+1, stride, stride); \
 }\
 \
-static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc33_c(uint8_t *dst, uint8_t *src, int stride){\
+static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc33_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
   OPNAME ## cavs_filt ## SIZE ## _hv_egpr(dst, src, src+stride+1,stride, stride); \
 }\
 \
-static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc21_c(uint8_t *dst, uint8_t *src, int stride){\
+static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc21_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
   OPNAME ## cavs_filt ## SIZE ## _hv_ff(dst, src, src+stride+1,stride, stride); \
 }\
 \
-static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc12_c(uint8_t *dst, uint8_t *src, int stride){\
+static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc12_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
   OPNAME ## cavs_filt ## SIZE ## _hv_ii(dst, src, src+stride+1,stride, stride); \
 }\
 \
-static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc32_c(uint8_t *dst, uint8_t *src, int stride){\
+static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc32_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
   OPNAME ## cavs_filt ## SIZE ## _hv_kk(dst, src, src+stride+1,stride, stride); \
 }\
 \
-static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc23_c(uint8_t *dst, uint8_t *src, int stride){\
+static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc23_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
   OPNAME ## cavs_filt ## SIZE ## _hv_qq(dst, src, src+stride+1,stride, stride); \
 }\
 
diff --git a/libavcodec/cdgraphics.c b/libavcodec/cdgraphics.c
index 202211d..d22e9f6 100644
--- a/libavcodec/cdgraphics.c
+++ b/libavcodec/cdgraphics.c
@@ -64,26 +64,18 @@
 #define CDG_PALETTE_SIZE          16
 
 typedef struct CDGraphicsContext {
-    AVFrame frame;
+    AVFrame *frame;
     int hscroll;
     int vscroll;
 } CDGraphicsContext;
 
-static void cdg_init_frame(AVFrame *frame)
-{
-    avcodec_get_frame_defaults(frame);
-    frame->reference = 3;
-    frame->buffer_hints = FF_BUFFER_HINTS_VALID    |
-                          FF_BUFFER_HINTS_READABLE |
-                          FF_BUFFER_HINTS_PRESERVE |
-                          FF_BUFFER_HINTS_REUSABLE;
-}
-
 static av_cold int cdg_decode_init(AVCodecContext *avctx)
 {
     CDGraphicsContext *cc = avctx->priv_data;
 
-    cdg_init_frame(&cc->frame);
+    cc->frame = av_frame_alloc();
+    if (!cc->frame)
+        return AVERROR(ENOMEM);
 
     avctx->width   = CDG_FULL_WIDTH;
     avctx->height  = CDG_FULL_HEIGHT;
@@ -95,8 +87,8 @@
 static void cdg_border_preset(CDGraphicsContext *cc, uint8_t *data)
 {
     int y;
-    int lsize    = cc->frame.linesize[0];
-    uint8_t *buf = cc->frame.data[0];
+    int lsize    = cc->frame->linesize[0];
+    uint8_t *buf = cc->frame->data[0];
     int color    = data[0] & 0x0F;
 
     if (!(data[1] & 0x0F)) {
@@ -120,7 +112,7 @@
     uint16_t color;
     int i;
     int array_offset  = low ? 0 : 8;
-    uint32_t *palette = (uint32_t *) cc->frame.data[1];
+    uint32_t *palette = (uint32_t *) cc->frame->data[1];
 
     for (i = 0; i < 8; i++) {
         color = (data[2 * i] << 6) + (data[2 * i + 1] & 0x3F);
@@ -129,7 +121,7 @@
         b = ((color     ) & 0x000F) * 17;
         palette[i + array_offset] = 0xFFU << 24 | r << 16 | g << 8 | b;
     }
-    cc->frame.palette_has_changed = 1;
+    cc->frame->palette_has_changed = 1;
 }
 
 static int cdg_tile_block(CDGraphicsContext *cc, uint8_t *data, int b)
@@ -138,8 +130,8 @@
     int color;
     int x, y;
     int ai;
-    int stride   = cc->frame.linesize[0];
-    uint8_t *buf = cc->frame.data[0];
+    int stride   = cc->frame->linesize[0];
+    uint8_t *buf = cc->frame->data[0];
 
     ri = (data[2] & 0x1F) * CDG_TILE_HEIGHT + cc->vscroll;
     ci = (data[3] & 0x3F) * CDG_TILE_WIDTH  + cc->hscroll;
@@ -210,8 +202,8 @@
     int color;
     int hscmd, h_off, hinc, vscmd, v_off, vinc;
     int y;
-    int stride   = cc->frame.linesize[0];
-    uint8_t *in  = cc->frame.data[0];
+    int stride   = cc->frame->linesize[0];
+    uint8_t *in  = cc->frame->data[0];
     uint8_t *out = new_frame->data[0];
 
     color =  data[0] & 0x0F;
@@ -239,7 +231,7 @@
     if (!hinc && !vinc)
         return;
 
-    memcpy(new_frame->data[1], cc->frame.data[1], CDG_PALETTE_SIZE * 4);
+    memcpy(new_frame->data[1], cc->frame->data[1], CDG_PALETTE_SIZE * 4);
 
     for (y = FFMAX(0, vinc); y < FFMIN(CDG_FULL_HEIGHT + vinc, CDG_FULL_HEIGHT); y++)
         memcpy(out + FFMAX(0, hinc) + stride * y,
@@ -274,7 +266,7 @@
     int ret;
     uint8_t command, inst;
     uint8_t cdg_data[CDG_DATA_SIZE];
-    AVFrame new_frame;
+    AVFrame *frame = data;
     CDGraphicsContext *cc = avctx->priv_data;
 
     if (buf_size < CDG_MINIMUM_PKT_SIZE) {
@@ -286,14 +278,11 @@
         return AVERROR(EINVAL);
     }
 
-    ret = avctx->reget_buffer(avctx, &cc->frame);
-    if (ret) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, cc->frame)) < 0)
         return ret;
-    }
     if (!avctx->frame_number) {
-        memset(cc->frame.data[0], 0, cc->frame.linesize[0] * avctx->height);
-        memset(cc->frame.data[1], 0, AVPALETTE_SIZE);
+        memset(cc->frame->data[0], 0, cc->frame->linesize[0] * avctx->height);
+        memset(cc->frame->data[1], 0, AVPALETTE_SIZE);
     }
 
     command = bytestream_get_byte(&buf);
@@ -306,8 +295,8 @@
         switch (inst) {
         case CDG_INST_MEMORY_PRESET:
             if (!(cdg_data[1] & 0x0F))
-                memset(cc->frame.data[0], cdg_data[0] & 0x0F,
-                       cc->frame.linesize[0] * CDG_FULL_HEIGHT);
+                memset(cc->frame->data[0], cdg_data[0] & 0x0F,
+                       cc->frame->linesize[0] * CDG_FULL_HEIGHT);
             break;
         case CDG_INST_LOAD_PAL_LO:
         case CDG_INST_LOAD_PAL_HIGH:
@@ -341,28 +330,30 @@
                 return AVERROR(EINVAL);
             }
 
-            cdg_init_frame(&new_frame);
-            ret = ff_get_buffer(avctx, &new_frame);
-            if (ret) {
-                av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+            if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
                 return ret;
-            }
 
-            cdg_scroll(cc, cdg_data, &new_frame, inst == CDG_INST_SCROLL_COPY);
-            avctx->release_buffer(avctx, &cc->frame);
-            cc->frame = new_frame;
+            cdg_scroll(cc, cdg_data, frame, inst == CDG_INST_SCROLL_COPY);
+            av_frame_unref(cc->frame);
+            ret = av_frame_ref(cc->frame, frame);
+            if (ret < 0)
+                return ret;
             break;
         default:
             break;
         }
 
+        if (!frame->data[0]) {
+            ret = av_frame_ref(frame, cc->frame);
+            if (ret < 0)
+                return ret;
+        }
         *got_frame = 1;
     } else {
         *got_frame = 0;
         buf_size   = 0;
     }
 
-    *(AVFrame *) data = cc->frame;
     return buf_size;
 }
 
@@ -370,8 +361,7 @@
 {
     CDGraphicsContext *cc = avctx->priv_data;
 
-    if (cc->frame.data[0])
-        avctx->release_buffer(avctx, &cc->frame);
+    av_frame_free(&cc->frame);
 
     return 0;
 }
diff --git a/libavcodec/cdxl.c b/libavcodec/cdxl.c
index e10ce4b..8c9e400 100644
--- a/libavcodec/cdxl.c
+++ b/libavcodec/cdxl.c
@@ -33,7 +33,6 @@
 
 typedef struct {
     AVCodecContext *avctx;
-    AVFrame        frame;
     int            bpp;
     int            format;
     int            padded_bits;
@@ -49,7 +48,6 @@
 {
     CDXLVideoContext *c = avctx->priv_data;
 
-    avcodec_get_frame_defaults(&c->frame);
     c->new_video_size = 0;
     c->avctx          = avctx;
 
@@ -113,16 +111,16 @@
     }
 }
 
-static void cdxl_decode_rgb(CDXLVideoContext *c)
+static void cdxl_decode_rgb(CDXLVideoContext *c, AVFrame *frame)
 {
-    uint32_t *new_palette = (uint32_t *)c->frame.data[1];
+    uint32_t *new_palette = (uint32_t *)frame->data[1];
 
-    memset(c->frame.data[1], 0, AVPALETTE_SIZE);
+    memset(frame->data[1], 0, AVPALETTE_SIZE);
     import_palette(c, new_palette);
-    import_format(c, c->frame.linesize[0], c->frame.data[0]);
+    import_format(c, frame->linesize[0], frame->data[0]);
 }
 
-static void cdxl_decode_ham6(CDXLVideoContext *c)
+static void cdxl_decode_ham6(CDXLVideoContext *c, AVFrame *frame)
 {
     AVCodecContext *avctx = c->avctx;
     uint32_t new_palette[16], r, g, b;
@@ -130,7 +128,7 @@
     int x, y;
 
     ptr = c->new_video;
-    out = c->frame.data[0];
+    out = frame->data[0];
 
     import_palette(c, new_palette);
     import_format(c, avctx->width, c->new_video);
@@ -161,11 +159,11 @@
             }
             AV_WL24(out + x * 3, r | g | b);
         }
-        out += c->frame.linesize[0];
+        out += frame->linesize[0];
     }
 }
 
-static void cdxl_decode_ham8(CDXLVideoContext *c)
+static void cdxl_decode_ham8(CDXLVideoContext *c, AVFrame *frame)
 {
     AVCodecContext *avctx = c->avctx;
     uint32_t new_palette[64], r, g, b;
@@ -173,7 +171,7 @@
     int x, y;
 
     ptr = c->new_video;
-    out = c->frame.data[0];
+    out = frame->data[0];
 
     import_palette(c, new_palette);
     import_format(c, avctx->width, c->new_video);
@@ -204,7 +202,7 @@
             }
             AV_WL24(out + x * 3, r | g | b);
         }
-        out += c->frame.linesize[0];
+        out += frame->linesize[0];
     }
 }
 
@@ -212,7 +210,7 @@
                              int *got_frame, AVPacket *pkt)
 {
     CDXLVideoContext *c = avctx->priv_data;
-    AVFrame * const p = &c->frame;
+    AVFrame * const p = data;
     int ret, w, h, encoding, aligned_width, buf_size = pkt->size;
     const uint8_t *buf = pkt->data;
 
@@ -235,7 +233,7 @@
     if (c->bpp < 1)
         return AVERROR_INVALIDDATA;
     if (c->format != BIT_PLANAR && c->format != BIT_LINE) {
-        av_log_ask_for_sample(avctx, "unsupported pixel format: 0x%0x\n", c->format);
+        avpriv_request_sample(avctx, "Pixel format 0x%0x", c->format);
         return AVERROR_PATCHWELCOME;
     }
 
@@ -255,19 +253,13 @@
             return AVERROR_INVALIDDATA;
         avctx->pix_fmt = AV_PIX_FMT_BGR24;
     } else {
-        av_log_ask_for_sample(avctx, "unsupported encoding %d and bpp %d\n",
+        avpriv_request_sample(avctx, "Encoding %d and bpp %d",
                               encoding, c->bpp);
         return AVERROR_PATCHWELCOME;
     }
 
-    if (p->data[0])
-        avctx->release_buffer(avctx, p);
-
-    p->reference = 0;
-    if ((ret = ff_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
         return ret;
-    }
     p->pict_type = AV_PICTURE_TYPE_I;
 
     if (encoding) {
@@ -276,14 +268,13 @@
         if (!c->new_video)
             return AVERROR(ENOMEM);
         if (c->bpp == 8)
-            cdxl_decode_ham8(c);
+            cdxl_decode_ham8(c, p);
         else
-            cdxl_decode_ham6(c);
+            cdxl_decode_ham6(c, p);
     } else {
-        cdxl_decode_rgb(c);
+        cdxl_decode_rgb(c, p);
     }
     *got_frame = 1;
-    *(AVFrame*)data = c->frame;
 
     return buf_size;
 }
@@ -293,8 +284,6 @@
     CDXLVideoContext *c = avctx->priv_data;
 
     av_free(c->new_video);
-    if (c->frame.data[0])
-        avctx->release_buffer(avctx, &c->frame);
 
     return 0;
 }
diff --git a/libavcodec/cinepak.c b/libavcodec/cinepak.c
index 5bd3f13..f5bc113 100644
--- a/libavcodec/cinepak.c
+++ b/libavcodec/cinepak.c
@@ -40,6 +40,7 @@
 #include "libavutil/common.h"
 #include "libavutil/intreadwrite.h"
 #include "avcodec.h"
+#include "internal.h"
 
 
 typedef uint8_t cvid_codebook[12];
@@ -57,7 +58,7 @@
 typedef struct CinepakContext {
 
     AVCodecContext *avctx;
-    AVFrame frame;
+    AVFrame *frame;
 
     const unsigned char *data;
     int size;
@@ -143,14 +144,14 @@
     for (y=strip->y1; y < strip->y2; y+=4) {
 
 /* take care of y dimension not being multiple of 4, such streams exist */
-        ip0 = ip1 = ip2 = ip3 = s->frame.data[0] +
-          (s->palette_video?strip->x1:strip->x1*3) + (y * s->frame.linesize[0]);
+        ip0 = ip1 = ip2 = ip3 = s->frame->data[0] +
+          (s->palette_video?strip->x1:strip->x1*3) + (y * s->frame->linesize[0]);
         if(s->avctx->height - y > 1) {
-            ip1 = ip0 + s->frame.linesize[0];
+            ip1 = ip0 + s->frame->linesize[0];
             if(s->avctx->height - y > 2) {
-                ip2 = ip1 + s->frame.linesize[0];
+                ip2 = ip1 + s->frame->linesize[0];
                 if(s->avctx->height - y > 3) {
-                    ip3 = ip2 + s->frame.linesize[0];
+                    ip3 = ip2 + s->frame->linesize[0];
                 }
             }
         }
@@ -331,7 +332,7 @@
     /* if this is the first frame, check for deviant Sega FILM data */
     if (s->sega_film_skip_bytes == -1) {
         if (!encoded_buf_size) {
-            av_log_ask_for_sample(s->avctx, "encoded_buf_size is 0");
+            avpriv_request_sample(s->avctx, "encoded_buf_size 0");
             return AVERROR_PATCHWELCOME;
         }
         if (encoded_buf_size != s->size && (s->size % encoded_buf_size) != 0) {
@@ -359,7 +360,7 @@
 
     num_strips = FFMIN(num_strips, MAX_STRIPS);
 
-    s->frame.key_frame = 0;
+    s->frame->key_frame = 0;
 
     for (i=0; i < num_strips; i++) {
         if ((s->data + 12) > eod)
@@ -375,7 +376,7 @@
         s->strips[i].x2 = AV_RB16 (&s->data[10]);
 
         if (s->strips[i].id == 0x10)
-            s->frame.key_frame = 1;
+            s->frame->key_frame = 1;
 
         strip_size = AV_RB24 (&s->data[1]) - 12;
         if (strip_size < 0)
@@ -420,8 +421,9 @@
         avctx->pix_fmt = AV_PIX_FMT_PAL8;
     }
 
-    avcodec_get_frame_defaults(&s->frame);
-    s->frame.data[0] = NULL;
+    s->frame = av_frame_alloc();
+    if (!s->frame)
+        return AVERROR(ENOMEM);
 
     return 0;
 }
@@ -437,18 +439,13 @@
     s->data = buf;
     s->size = buf_size;
 
-    s->frame.reference = 3;
-    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
-                            FF_BUFFER_HINTS_REUSABLE;
-    if ((ret = avctx->reget_buffer(avctx, &s->frame))) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
         return ret;
-    }
 
     if (s->palette_video) {
         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
         if (pal) {
-            s->frame.palette_has_changed = 1;
+            s->frame->palette_has_changed = 1;
             memcpy(s->pal, pal, AVPALETTE_SIZE);
         }
     }
@@ -458,10 +455,12 @@
     }
 
     if (s->palette_video)
-        memcpy (s->frame.data[1], s->pal, AVPALETTE_SIZE);
+        memcpy (s->frame->data[1], s->pal, AVPALETTE_SIZE);
+
+    if ((ret = av_frame_ref(data, s->frame)) < 0)
+        return ret;
 
     *got_frame = 1;
-    *(AVFrame*)data = s->frame;
 
     /* report that the buffer was completely consumed */
     return buf_size;
@@ -471,8 +470,7 @@
 {
     CinepakContext *s = avctx->priv_data;
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+    av_frame_free(&s->frame);
 
     return 0;
 }
diff --git a/libavcodec/cljr.c b/libavcodec/cljr.c
index b93997f..2120864 100644
--- a/libavcodec/cljr.c
+++ b/libavcodec/cljr.c
@@ -30,22 +30,6 @@
 #include "internal.h"
 #include "put_bits.h"
 
-typedef struct CLJRContext {
-    AVClass        *avclass;
-    AVFrame         picture;
-    int             dither_type;
-} CLJRContext;
-
-static av_cold int common_init(AVCodecContext *avctx)
-{
-    CLJRContext * const a = avctx->priv_data;
-
-    avcodec_get_frame_defaults(&a->picture);
-    avctx->coded_frame = &a->picture;
-
-    return 0;
-}
-
 #if CONFIG_CLJR_DECODER
 static int decode_frame(AVCodecContext *avctx,
                         void *data, int *got_frame,
@@ -53,15 +37,10 @@
 {
     const uint8_t *buf = avpkt->data;
     int buf_size       = avpkt->size;
-    CLJRContext * const a = avctx->priv_data;
     GetBitContext gb;
-    AVFrame *picture = data;
-    AVFrame * const p = &a->picture;
+    AVFrame * const p = data;
     int x, y, ret;
 
-    if (p->data[0])
-        avctx->release_buffer(avctx, p);
-
     if (avctx->height <= 0 || avctx->width <= 0) {
         av_log(avctx, AV_LOG_ERROR, "Invalid width or height\n");
         return AVERROR_INVALIDDATA;
@@ -73,20 +52,17 @@
         return AVERROR_INVALIDDATA;
     }
 
-    p->reference = 0;
-    if ((ret = ff_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
         return ret;
-    }
     p->pict_type = AV_PICTURE_TYPE_I;
     p->key_frame = 1;
 
     init_get_bits(&gb, buf, buf_size * 8);
 
     for (y = 0; y < avctx->height; y++) {
-        uint8_t *luma = &a->picture.data[0][y * a->picture.linesize[0]];
-        uint8_t *cb   = &a->picture.data[1][y * a->picture.linesize[1]];
-        uint8_t *cr   = &a->picture.data[2][y * a->picture.linesize[2]];
+        uint8_t *luma = &p->data[0][y * p->linesize[0]];
+        uint8_t *cb   = &p->data[1][y * p->linesize[1]];
+        uint8_t *cr   = &p->data[2][y * p->linesize[2]];
         for (x = 0; x < avctx->width; x += 4) {
             luma[3] = (get_bits(&gb, 5)*33) >> 2;
             luma[2] = (get_bits(&gb, 5)*33) >> 2;
@@ -98,7 +74,6 @@
         }
     }
 
-    *picture   = a->picture;
     *got_frame = 1;
 
     return buf_size;
@@ -107,15 +82,6 @@
 static av_cold int decode_init(AVCodecContext *avctx)
 {
     avctx->pix_fmt = AV_PIX_FMT_YUV411P;
-    return common_init(avctx);
-}
-
-static av_cold int decode_end(AVCodecContext *avctx)
-{
-    CLJRContext *a = avctx->priv_data;
-
-    if (a->picture.data[0])
-        avctx->release_buffer(avctx, &a->picture);
     return 0;
 }
 
@@ -123,9 +89,7 @@
     .name           = "cljr",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_CLJR,
-    .priv_data_size = sizeof(CLJRContext),
     .init           = decode_init,
-    .close          = decode_end,
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
@@ -133,6 +97,21 @@
 #endif
 
 #if CONFIG_CLJR_ENCODER
+typedef struct CLJRContext {
+    AVClass        *avclass;
+    AVFrame         picture;
+    int             dither_type;
+} CLJRContext;
+
+static av_cold int encode_init(AVCodecContext *avctx)
+{
+    CLJRContext * const a = avctx->priv_data;
+
+    avctx->coded_frame = &a->picture;
+
+    return 0;
+}
+
 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
                         const AVFrame *p, int *got_packet)
 {
@@ -201,7 +180,7 @@
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_CLJR,
     .priv_data_size = sizeof(CLJRContext),
-    .init           = common_init,
+    .init           = encode_init,
     .encode2        = encode_frame,
     .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV411P,
                                                    AV_PIX_FMT_NONE },
diff --git a/libavcodec/cllc.c b/libavcodec/cllc.c
index d14de60..6378c79 100644
--- a/libavcodec/cllc.c
+++ b/libavcodec/cllc.c
@@ -271,18 +271,13 @@
                              int *got_picture_ptr, AVPacket *avpkt)
 {
     CLLCContext *ctx = avctx->priv_data;
-    AVFrame *pic = avctx->coded_frame;
+    AVFrame *pic = data;
     uint8_t *src = avpkt->data;
     uint32_t info_tag, info_offset;
     int data_size;
     GetBitContext gb;
     int coding_type, ret;
 
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
-
-    pic->reference = 0;
-
     /* Skip the INFO header if present */
     info_offset = 0;
     info_tag    = AV_RL32(src);
@@ -334,11 +329,8 @@
         avctx->pix_fmt             = AV_PIX_FMT_RGB24;
         avctx->bits_per_raw_sample = 8;
 
-        ret = ff_get_buffer(avctx, pic);
-        if (ret < 0) {
-            av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
+        if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
             return ret;
-        }
 
         ret = decode_rgb24_frame(ctx, &gb, pic);
         if (ret < 0)
@@ -349,11 +341,8 @@
         avctx->pix_fmt             = AV_PIX_FMT_ARGB;
         avctx->bits_per_raw_sample = 8;
 
-        ret = ff_get_buffer(avctx, pic);
-        if (ret < 0) {
-            av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
+        if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
             return ret;
-        }
 
         ret = decode_argb_frame(ctx, &gb, pic);
         if (ret < 0)
@@ -369,7 +358,6 @@
     pic->pict_type = AV_PICTURE_TYPE_I;
 
     *got_picture_ptr = 1;
-    *(AVFrame *)data = *pic;
 
     return avpkt->size;
 }
@@ -378,10 +366,6 @@
 {
     CLLCContext *ctx = avctx->priv_data;
 
-    if (avctx->coded_frame->data[0])
-        avctx->release_buffer(avctx, avctx->coded_frame);
-
-    av_freep(&avctx->coded_frame);
     av_freep(&ctx->swapped_buf);
 
     return 0;
@@ -398,12 +382,6 @@
 
     ff_dsputil_init(&ctx->dsp, avctx);
 
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame) {
-        av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
-        return AVERROR(ENOMEM);
-    }
-
     return 0;
 }
 
diff --git a/libavcodec/cngdec.c b/libavcodec/cngdec.c
index edfdd3e..675f77d 100644
--- a/libavcodec/cngdec.c
+++ b/libavcodec/cngdec.c
@@ -142,10 +142,8 @@
                                  p->excitation, avctx->frame_size, p->order);
 
     frame->nb_samples = avctx->frame_size;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     buf_out = (int16_t *)frame->data[0];
     for (i = 0; i < avctx->frame_size; i++)
         buf_out[i] = p->filter_out[i + p->order];
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 673f971..c973025 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -1038,6 +1038,13 @@
         .props     = AV_CODEC_PROP_LOSSY,
     },
     {
+        .id        = AV_CODEC_ID_WEBP,
+        .type      = AVMEDIA_TYPE_VIDEO,
+        .name      = "webp",
+        .long_name = NULL_IF_CONFIG_SMALL("WebP"),
+        .props     = AV_CODEC_PROP_LOSSY,
+    },
+    {
         .id        = AV_CODEC_ID_PICTOR,
         .type      = AVMEDIA_TYPE_VIDEO,
         .name      = "pictor",
@@ -2387,6 +2394,7 @@
         .type      = AVMEDIA_TYPE_SUBTITLE,
         .name      = "text",
         .long_name = NULL_IF_CONFIG_SMALL("raw UTF-8 text"),
+        .props     = AV_CODEC_PROP_TEXT_SUB,
     },
     {
         .id        = AV_CODEC_ID_XSUB,
@@ -2396,16 +2404,25 @@
         .props     = AV_CODEC_PROP_BITMAP_SUB,
     },
     {
+        .id        = AV_CODEC_ID_ASS,
+        .type      = AVMEDIA_TYPE_SUBTITLE,
+        .name      = "ass",
+        .long_name = NULL_IF_CONFIG_SMALL("ASS (Advanced SSA) subtitle"),
+        .props     = AV_CODEC_PROP_TEXT_SUB,
+    },
+    {
         .id        = AV_CODEC_ID_SSA,
         .type      = AVMEDIA_TYPE_SUBTITLE,
         .name      = "ssa",
-        .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) / ASS (Advanced SSA) subtitle"),
+        .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
+        .props     = AV_CODEC_PROP_TEXT_SUB,
     },
     {
         .id        = AV_CODEC_ID_MOV_TEXT,
         .type      = AVMEDIA_TYPE_SUBTITLE,
         .name      = "mov_text",
         .long_name = NULL_IF_CONFIG_SMALL("MOV text"),
+        .props     = AV_CODEC_PROP_TEXT_SUB,
     },
     {
         .id        = AV_CODEC_ID_HDMV_PGS_SUBTITLE,
@@ -2425,24 +2442,28 @@
         .type      = AVMEDIA_TYPE_SUBTITLE,
         .name      = "srt",
         .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle with embedded timing"),
+        .props     = AV_CODEC_PROP_TEXT_SUB,
     },
     {
         .id        = AV_CODEC_ID_SUBRIP,
         .type      = AVMEDIA_TYPE_SUBTITLE,
         .name      = "subrip",
         .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
+        .props     = AV_CODEC_PROP_TEXT_SUB,
     },
     {
         .id        = AV_CODEC_ID_MICRODVD,
         .type      = AVMEDIA_TYPE_SUBTITLE,
         .name      = "microdvd",
         .long_name = NULL_IF_CONFIG_SMALL("MicroDVD subtitle"),
+        .props     = AV_CODEC_PROP_TEXT_SUB,
     },
     {
         .id        = AV_CODEC_ID_MPL2,
         .type      = AVMEDIA_TYPE_SUBTITLE,
         .name      = "mpl2",
         .long_name = NULL_IF_CONFIG_SMALL("MPL2 subtitle"),
+        .props     = AV_CODEC_PROP_TEXT_SUB,
     },
     {
         .id        = AV_CODEC_ID_EIA_608,
@@ -2455,48 +2476,56 @@
         .type      = AVMEDIA_TYPE_SUBTITLE,
         .name      = "jacosub",
         .long_name = NULL_IF_CONFIG_SMALL("JACOsub subtitle"),
+        .props     = AV_CODEC_PROP_TEXT_SUB,
     },
     {
         .id        = AV_CODEC_ID_PJS,
         .type      = AVMEDIA_TYPE_SUBTITLE,
         .name      = "pjs",
         .long_name = NULL_IF_CONFIG_SMALL("PJS (Phoenix Japanimation Society) subtitle"),
+        .props     = AV_CODEC_PROP_TEXT_SUB,
     },
     {
         .id        = AV_CODEC_ID_SAMI,
         .type      = AVMEDIA_TYPE_SUBTITLE,
         .name      = "sami",
         .long_name = NULL_IF_CONFIG_SMALL("SAMI subtitle"),
+        .props     = AV_CODEC_PROP_TEXT_SUB,
     },
     {
         .id        = AV_CODEC_ID_REALTEXT,
         .type      = AVMEDIA_TYPE_SUBTITLE,
         .name      = "realtext",
         .long_name = NULL_IF_CONFIG_SMALL("RealText subtitle"),
+        .props     = AV_CODEC_PROP_TEXT_SUB,
     },
     {
         .id        = AV_CODEC_ID_SUBVIEWER1,
         .type      = AVMEDIA_TYPE_SUBTITLE,
         .name      = "subviewer1",
         .long_name = NULL_IF_CONFIG_SMALL("SubViewer v1 subtitle"),
+        .props     = AV_CODEC_PROP_TEXT_SUB,
     },
     {
         .id        = AV_CODEC_ID_SUBVIEWER,
         .type      = AVMEDIA_TYPE_SUBTITLE,
         .name      = "subviewer",
         .long_name = NULL_IF_CONFIG_SMALL("SubViewer subtitle"),
+        .props     = AV_CODEC_PROP_TEXT_SUB,
     },
     {
         .id        = AV_CODEC_ID_VPLAYER,
         .type      = AVMEDIA_TYPE_SUBTITLE,
         .name      = "vplayer",
         .long_name = NULL_IF_CONFIG_SMALL("VPlayer subtitle"),
+        .props     = AV_CODEC_PROP_TEXT_SUB,
     },
     {
         .id        = AV_CODEC_ID_WEBVTT,
         .type      = AVMEDIA_TYPE_SUBTITLE,
         .name      = "webvtt",
         .long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
+        .props     = AV_CODEC_PROP_TEXT_SUB,
     },
     {
         .id        = AV_CODEC_ID_BINTEXT,
@@ -2525,6 +2554,12 @@
         .name      = "klv",
         .long_name = NULL_IF_CONFIG_SMALL("SMPTE 336M Key-Length-Value (KLV) metadata"),
     },
+    {
+        .id        = AV_CODEC_ID_DVD_NAV,
+        .type      = AVMEDIA_TYPE_DATA,
+        .name      = "dvd_nav_packet",
+        .long_name = NULL_IF_CONFIG_SMALL("DVD Nav packet"),
+    },
 
 };
 
diff --git a/libavcodec/cook.c b/libavcodec/cook.c
index 7dcf655..08cd401 100644
--- a/libavcodec/cook.c
+++ b/libavcodec/cook.c
@@ -970,10 +970,8 @@
     /* get output buffer */
     if (q->discarded_packets >= 2) {
         frame->nb_samples = q->samples_per_channel;
-        if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
             return ret;
-        }
         samples = (float **)frame->extended_data;
     }
 
@@ -1117,7 +1115,7 @@
         switch (q->subpacket[s].cookversion) {
         case MONO:
             if (avctx->channels != 1) {
-                av_log_ask_for_sample(avctx, "Container channels != 1.\n");
+                avpriv_request_sample(avctx, "Container channels != 1");
                 return AVERROR_PATCHWELCOME;
             }
             av_log(avctx, AV_LOG_DEBUG, "MONO\n");
@@ -1131,7 +1129,7 @@
             break;
         case JOINT_STEREO:
             if (avctx->channels != 2) {
-                av_log_ask_for_sample(avctx, "Container channels != 2.\n");
+                avpriv_request_sample(avctx, "Container channels != 2");
                 return AVERROR_PATCHWELCOME;
             }
             av_log(avctx, AV_LOG_DEBUG, "JOINT_STEREO\n");
@@ -1171,7 +1169,8 @@
 
             break;
         default:
-            av_log_ask_for_sample(avctx, "Unknown Cook version.\n");
+            avpriv_request_sample(avctx, "Cook version %d",
+                                  q->subpacket[s].cookversion);
             return AVERROR_PATCHWELCOME;
         }
 
@@ -1187,7 +1186,7 @@
 
         /* Try to catch some obviously faulty streams, othervise it might be exploitable */
         if (q->subpacket[s].total_subbands > 53) {
-            av_log_ask_for_sample(avctx, "total_subbands > 53\n");
+            avpriv_request_sample(avctx, "total_subbands > 53");
             return AVERROR_PATCHWELCOME;
         }
 
@@ -1199,11 +1198,11 @@
         }
 
         if (q->subpacket[s].subbands > 50) {
-            av_log_ask_for_sample(avctx, "subbands > 50\n");
+            avpriv_request_sample(avctx, "subbands > 50");
             return AVERROR_PATCHWELCOME;
         }
         if (q->subpacket[s].subbands == 0) {
-            av_log_ask_for_sample(avctx, "subbands is 0\n");
+            avpriv_request_sample(avctx, "subbands = 0");
             return AVERROR_PATCHWELCOME;
         }
         q->subpacket[s].gains1.now      = q->subpacket[s].gain_1;
@@ -1219,7 +1218,7 @@
         q->num_subpackets++;
         s++;
         if (s > MAX_SUBPACKETS) {
-            av_log_ask_for_sample(avctx, "Too many subpackets > 5\n");
+            avpriv_request_sample(avctx, "subpackets > %d", MAX_SUBPACKETS);
             return AVERROR_PATCHWELCOME;
         }
     }
@@ -1261,8 +1260,7 @@
     /* Try to catch some obviously faulty streams, othervise it might be exploitable */
     if (q->samples_per_channel != 256 && q->samples_per_channel != 512 &&
         q->samples_per_channel != 1024) {
-        av_log_ask_for_sample(avctx,
-                              "unknown amount of samples_per_channel = %d\n",
+        avpriv_request_sample(avctx, "samples_per_channel = %d",
                               q->samples_per_channel);
         return AVERROR_PATCHWELCOME;
     }
diff --git a/libavcodec/cpia.c b/libavcodec/cpia.c
index f741eb9..4f83503 100644
--- a/libavcodec/cpia.c
+++ b/libavcodec/cpia.c
@@ -24,6 +24,7 @@
 
 #include "avcodec.h"
 #include "get_bits.h"
+#include "internal.h"
 
 
 #define FRAME_HEADER_SIZE 64
@@ -42,7 +43,7 @@
 
 
 typedef struct {
-    AVFrame frame;
+    AVFrame *frame;
 } CpiaContext;
 
 
@@ -58,7 +59,7 @@
     uint16_t linelength;
     uint8_t skip;
 
-    AVFrame* const frame = &cpia->frame;
+    AVFrame *frame = cpia->frame;
     uint8_t *y, *u, *v, *y_end, *u_end, *v_end;
 
     // Check header
@@ -99,10 +100,8 @@
     }
 
     // Get buffer filled with previous frame
-    if ((ret = avctx->reget_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed!\n");
+    if ((ret = ff_reget_buffer(avctx, frame)) < 0)
         return ret;
-    }
 
 
     for ( i = 0;
@@ -114,12 +113,12 @@
         src += 2;
 
         if (src_size < linelength) {
-            frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
+            av_frame_set_decode_error_flags(frame, FF_DECODE_ERROR_INVALID_BITSTREAM);
             av_log(avctx, AV_LOG_WARNING, "Frame ended enexpectedly!\n");
             break;
         }
         if (src[linelength - 1] != EOL) {
-            frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
+            av_frame_set_decode_error_flags(frame, FF_DECODE_ERROR_INVALID_BITSTREAM);
             av_log(avctx, AV_LOG_WARNING, "Wrong line length %d or line not terminated properly (found 0x%02x)!\n", linelength, src[linelength - 1]);
             break;
         }
@@ -140,7 +139,7 @@
              */
             for (j = 0; j < linelength - 1; j++) {
                 if (y > y_end) {
-                    frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
+                    av_frame_set_decode_error_flags(frame, FF_DECODE_ERROR_INVALID_BITSTREAM);
                     av_log(avctx, AV_LOG_WARNING, "Decoded data exceeded linesize!\n");
                     break;
                 }
@@ -160,7 +159,7 @@
              */
             for (j = 0; j < linelength - 4; ) {
                 if (y + 1 > y_end || u > u_end || v > v_end) {
-                    frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
+                    av_frame_set_decode_error_flags(frame, FF_DECODE_ERROR_INVALID_BITSTREAM);
                     av_log(avctx, AV_LOG_WARNING, "Decoded data exceeded linesize!\n");
                     break;
                 }
@@ -184,13 +183,16 @@
     }
 
     *got_frame = 1;
-    *(AVFrame*) data = *frame;
+    if ((ret = av_frame_ref(data, cpia->frame)) < 0)
+        return ret;
 
     return avpkt->size;
 }
 
 static av_cold int cpia_decode_init(AVCodecContext *avctx)
 {
+    CpiaContext *s = avctx->priv_data;
+
     // output pixel format
     avctx->pix_fmt = AV_PIX_FMT_YUV420P;
 
@@ -202,9 +204,21 @@
         avctx->time_base.den = 60;
     }
 
+    s->frame = av_frame_alloc();
+    if (!s->frame)
+        return AVERROR(ENOMEM);
+
     return 0;
 }
 
+static av_cold int cpia_decode_end(AVCodecContext *avctx)
+{
+    CpiaContext *s = avctx->priv_data;
+
+    av_frame_free(&s->frame);
+
+    return 0;
+}
 
 AVCodec ff_cpia_decoder = {
     .name           = "cpia",
@@ -212,6 +226,7 @@
     .id             = AV_CODEC_ID_CPIA,
     .priv_data_size = sizeof(CpiaContext),
     .init           = cpia_decode_init,
+    .close          = cpia_decode_end,
     .decode         = cpia_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("CPiA video format"),
diff --git a/libavcodec/crystalhd.c b/libavcodec/crystalhd.c
index e2596a2..5dee825 100644
--- a/libavcodec/crystalhd.c
+++ b/libavcodec/crystalhd.c
@@ -122,7 +122,7 @@
 typedef struct {
     AVClass *av_class;
     AVCodecContext *avctx;
-    AVFrame pic;
+    AVFrame *pic;
     HANDLE dev;
 
     uint8_t *orig_extradata;
@@ -324,8 +324,7 @@
     priv->skip_next_output  = 0;
     priv->decode_wait       = BASE_WAIT;
 
-    if (priv->pic.data[0])
-        avctx->release_buffer(avctx, &priv->pic);
+    av_frame_unref (priv->pic);
 
     /* Flush mode 4 flushes all software and hardware buffers. */
     DtsFlushInput(priv->dev, 4);
@@ -362,8 +361,7 @@
 
     av_free(priv->sps_pps_buf);
 
-    if (priv->pic.data[0])
-        avctx->release_buffer(avctx, &priv->pic);
+    av_frame_free (&priv->pic);
 
     if (priv->head) {
        OpaqueList *node = priv->head;
@@ -411,6 +409,7 @@
     priv->is_nal       = avctx->extradata_size > 0 && *(avctx->extradata) == 1;
     priv->last_picture = -1;
     priv->decode_wait  = BASE_WAIT;
+    priv->pic          = av_frame_alloc();
 
     subtype = id2subtype(priv, avctx->codec->id);
     switch (subtype) {
@@ -635,18 +634,14 @@
     av_log(avctx, AV_LOG_VERBOSE, "Interlaced state: %d | trust_interlaced %d\n",
            interlaced, trust_interlaced);
 
-    if (priv->pic.data[0] && !priv->need_second_field)
-        avctx->release_buffer(avctx, &priv->pic);
+    if (priv->pic->data[0] && !priv->need_second_field)
+        av_frame_unref(priv->pic);
 
     priv->need_second_field = interlaced && !priv->need_second_field;
 
-    priv->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
-                             FF_BUFFER_HINTS_REUSABLE;
-    if (!priv->pic.data[0]) {
-        if (ff_get_buffer(avctx, &priv->pic) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if (!priv->pic->data[0]) {
+        if (ff_get_buffer(avctx, priv->pic, AV_GET_BUFFER_FLAG_REF) < 0)
             return RET_ERROR;
-        }
     }
 
     bwidth = av_image_get_linesize(avctx->pix_fmt, width, 0);
@@ -663,8 +658,8 @@
         sStride = bwidth;
     }
 
-    dStride = priv->pic.linesize[0];
-    dst     = priv->pic.data[0];
+    dStride = priv->pic->linesize[0];
+    dst     = priv->pic->data[0];
 
     av_log(priv->avctx, AV_LOG_VERBOSE, "CrystalHD: Copying out frame\n");
 
@@ -689,15 +684,17 @@
         av_image_copy_plane(dst, dStride, src, sStride, bwidth, height);
     }
 
-    priv->pic.interlaced_frame = interlaced;
+    priv->pic->interlaced_frame = interlaced;
     if (interlaced)
-        priv->pic.top_field_first = !bottom_first;
+        priv->pic->top_field_first = !bottom_first;
 
-    priv->pic.pkt_pts = pkt_pts;
+    priv->pic->pkt_pts = pkt_pts;
 
     if (!priv->need_second_field) {
         *got_frame       = 1;
-        *(AVFrame *)data = priv->pic;
+        if ((ret = av_frame_ref(data, priv->pic)) < 0) {
+            return ret;
+        }
     }
 
     /*
diff --git a/libavcodec/cscd.c b/libavcodec/cscd.c
index 110b06f..0a5fa69 100644
--- a/libavcodec/cscd.c
+++ b/libavcodec/cscd.c
@@ -31,7 +31,7 @@
 #include "libavutil/lzo.h"
 
 typedef struct {
-    AVFrame pic;
+    AVFrame *pic;
     int linelen, height, bpp;
     unsigned int decomp_size;
     unsigned char* decomp_buf;
@@ -67,7 +67,6 @@
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     CamStudioContext *c = avctx->priv_data;
-    AVFrame *picture = data;
     int ret;
 
     if (buf_size < 2) {
@@ -75,13 +74,8 @@
         return AVERROR_INVALIDDATA;
     }
 
-    c->pic.reference = 3;
-    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
-                          FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
-    if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
         return ret;
-    }
 
     // decompress data
     switch ((buf[0] >> 1) & 7) {
@@ -109,19 +103,21 @@
 
     // flip upside down, add difference frame
     if (buf[0] & 1) { // keyframe
-        c->pic.pict_type = AV_PICTURE_TYPE_I;
-        c->pic.key_frame = 1;
-              copy_frame_default(&c->pic, c->decomp_buf,
+        c->pic->pict_type = AV_PICTURE_TYPE_I;
+        c->pic->key_frame = 1;
+              copy_frame_default(c->pic, c->decomp_buf,
                                  c->linelen, c->height);
     } else {
-        c->pic.pict_type = AV_PICTURE_TYPE_P;
-        c->pic.key_frame = 0;
-              add_frame_default(&c->pic, c->decomp_buf,
+        c->pic->pict_type = AV_PICTURE_TYPE_P;
+        c->pic->key_frame = 0;
+              add_frame_default(c->pic, c->decomp_buf,
                                 c->linelen, c->height);
     }
 
-    *picture = c->pic;
     *got_frame = 1;
+    if ((ret = av_frame_ref(data, c->pic)) < 0)
+        return ret;
+
     return buf_size;
 }
 
@@ -139,8 +135,6 @@
             return AVERROR_INVALIDDATA;
     }
     c->bpp = avctx->bits_per_coded_sample;
-    avcodec_get_frame_defaults(&c->pic);
-    c->pic.data[0] = NULL;
     c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
     c->height = avctx->height;
     stride = FFALIGN(c->linelen, 4);
@@ -150,14 +144,16 @@
         av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
         return AVERROR(ENOMEM);
     }
+    c->pic = av_frame_alloc();
+    if (!c->pic)
+        return AVERROR(ENOMEM);
     return 0;
 }
 
 static av_cold int decode_end(AVCodecContext *avctx) {
     CamStudioContext *c = avctx->priv_data;
     av_freep(&c->decomp_buf);
-    if (c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
+    av_frame_free(&c->pic);
     return 0;
 }
 
diff --git a/libavcodec/cyuv.c b/libavcodec/cyuv.c
index b3bf4f2..3a30138 100644
--- a/libavcodec/cyuv.c
+++ b/libavcodec/cyuv.c
@@ -40,7 +40,6 @@
 typedef struct CyuvDecodeContext {
     AVCodecContext *avctx;
     int width, height;
-    AVFrame frame;
 } CyuvDecodeContext;
 
 static av_cold int cyuv_decode_init(AVCodecContext *avctx)
@@ -53,7 +52,6 @@
     if (s->width & 0x3)
         return AVERROR_INVALIDDATA;
     s->height = avctx->height;
-    avcodec_get_frame_defaults(&s->frame);
 
     return 0;
 }
@@ -65,6 +63,7 @@
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     CyuvDecodeContext *s=avctx->priv_data;
+    AVFrame *frame = data;
 
     unsigned char *y_plane;
     unsigned char *u_plane;
@@ -106,35 +105,28 @@
     /* pixel data starts 48 bytes in, after 3x16-byte tables */
     stream_ptr = 48;
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
-
-    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
-    s->frame.reference = 0;
-    if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
-    y_plane = s->frame.data[0];
-    u_plane = s->frame.data[1];
-    v_plane = s->frame.data[2];
+    y_plane = frame->data[0];
+    u_plane = frame->data[1];
+    v_plane = frame->data[2];
 
     if (buf_size == rawsize) {
         int linesize = FFALIGN(s->width,2) * 2;
-        y_plane += s->frame.linesize[0] * s->height;
+        y_plane += frame->linesize[0] * s->height;
         for (stream_ptr = 0; stream_ptr < rawsize; stream_ptr += linesize) {
-            y_plane -= s->frame.linesize[0];
+            y_plane -= frame->linesize[0];
             memcpy(y_plane, buf+stream_ptr, linesize);
         }
     } else {
 
     /* iterate through each line in the height */
     for (y_ptr = 0, u_ptr = 0, v_ptr = 0;
-         y_ptr < (s->height * s->frame.linesize[0]);
-         y_ptr += s->frame.linesize[0] - s->width,
-         u_ptr += s->frame.linesize[1] - s->width / 4,
-         v_ptr += s->frame.linesize[2] - s->width / 4) {
+         y_ptr < (s->height * frame->linesize[0]);
+         y_ptr += frame->linesize[0] - s->width,
+         u_ptr += frame->linesize[1] - s->width / 4,
+         v_ptr += frame->linesize[2] - s->width / 4) {
 
         /* reset predictors */
         cur_byte = buf[stream_ptr++];
@@ -179,21 +171,10 @@
     }
 
     *got_frame = 1;
-    *(AVFrame*)data= s->frame;
 
     return buf_size;
 }
 
-static av_cold int cyuv_decode_end(AVCodecContext *avctx)
-{
-    CyuvDecodeContext *s = avctx->priv_data;
-
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
-
-    return 0;
-}
-
 #if CONFIG_AURA_DECODER
 AVCodec ff_aura_decoder = {
     .name           = "aura",
@@ -201,7 +182,6 @@
     .id             = AV_CODEC_ID_AURA,
     .priv_data_size = sizeof(CyuvDecodeContext),
     .init           = cyuv_decode_init,
-    .close          = cyuv_decode_end,
     .decode         = cyuv_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("Auravision AURA"),
@@ -215,7 +195,6 @@
     .id             = AV_CODEC_ID_CYUV,
     .priv_data_size = sizeof(CyuvDecodeContext),
     .init           = cyuv_decode_init,
-    .close          = cyuv_decode_end,
     .decode         = cyuv_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("Creative YUV (CYUV)"),
diff --git a/libavcodec/dca.c b/libavcodec/dca.c
index bbe1f10..1c1c080 100644
--- a/libavcodec/dca.c
+++ b/libavcodec/dca.c
@@ -23,7 +23,9 @@
  */
 
 #include <stdint.h>
+#include <string.h>
 
+#include "put_bits.h"
 #include "dca.h"
 
 const uint32_t avpriv_dca_sample_rates[16] =
@@ -31,3 +33,38 @@
     0, 8000, 16000, 32000, 0, 0, 11025, 22050, 44100, 0, 0,
     12000, 24000, 48000, 96000, 192000
 };
+
+int ff_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
+                             int max_size)
+{
+    uint32_t mrk;
+    int i, tmp;
+    const uint16_t *ssrc = (const uint16_t *) src;
+    uint16_t *sdst = (uint16_t *) dst;
+    PutBitContext pb;
+
+    if ((unsigned) src_size > (unsigned) max_size)
+        src_size = max_size;
+
+    mrk = AV_RB32(src);
+    switch (mrk) {
+    case DCA_MARKER_RAW_BE:
+        memcpy(dst, src, src_size);
+        return src_size;
+    case DCA_MARKER_RAW_LE:
+        for (i = 0; i < (src_size + 1) >> 1; i++)
+            *sdst++ = av_bswap16(*ssrc++);
+        return src_size;
+    case DCA_MARKER_14B_BE:
+    case DCA_MARKER_14B_LE:
+        init_put_bits(&pb, dst, max_size);
+        for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
+            tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
+            put_bits(&pb, 14, tmp);
+        }
+        flush_put_bits(&pb);
+        return (put_bits_count(&pb) + 7) >> 3;
+    default:
+        return AVERROR_INVALIDDATA;
+    }
+}
diff --git a/libavcodec/dca.h b/libavcodec/dca.h
index 3da93aa..d60b282 100644
--- a/libavcodec/dca.h
+++ b/libavcodec/dca.h
@@ -39,4 +39,10 @@
 
 extern av_export const uint32_t avpriv_dca_sample_rates[16];
 
+/**
+ * Convert bitstream to one representation based on sync marker
+ */
+int ff_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
+                             int max_size);
+
 #endif /* AVCODEC_DCA_H */
diff --git a/libavcodec/dca_parser.c b/libavcodec/dca_parser.c
index 266520f..c104268 100644
--- a/libavcodec/dca_parser.c
+++ b/libavcodec/dca_parser.c
@@ -24,9 +24,7 @@
 
 #include "parser.h"
 #include "dca.h"
-#include "dca_parser.h"
 #include "get_bits.h"
-#include "put_bits.h"
 
 typedef struct DCAParseContext {
     ParseContext pc;
@@ -101,41 +99,6 @@
     return 0;
 }
 
-int ff_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
-                             int max_size)
-{
-    uint32_t mrk;
-    int i, tmp;
-    const uint16_t *ssrc = (const uint16_t *) src;
-    uint16_t *sdst = (uint16_t *) dst;
-    PutBitContext pb;
-
-    if ((unsigned) src_size > (unsigned) max_size)
-        src_size = max_size;
-
-    mrk = AV_RB32(src);
-    switch (mrk) {
-    case DCA_MARKER_RAW_BE:
-        memcpy(dst, src, src_size);
-        return src_size;
-    case DCA_MARKER_RAW_LE:
-        for (i = 0; i < (src_size + 1) >> 1; i++)
-            *sdst++ = av_bswap16(*ssrc++);
-        return src_size;
-    case DCA_MARKER_14B_BE:
-    case DCA_MARKER_14B_LE:
-        init_put_bits(&pb, dst, max_size);
-        for (i = 0; i < (src_size + 1) >> 1; i++, src += 2) {
-            tmp = ((mrk == DCA_MARKER_14B_BE) ? AV_RB16(src) : AV_RL16(src)) & 0x3FFF;
-            put_bits(&pb, 14, tmp);
-        }
-        flush_put_bits(&pb);
-        return (put_bits_count(&pb) + 7) >> 3;
-    default:
-        return AVERROR_INVALIDDATA;
-    }
-}
-
 static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration,
                             int *sample_rate)
 {
diff --git a/libavcodec/dca_parser.h b/libavcodec/dca_parser.h
deleted file mode 100644
index f480eab..0000000
--- a/libavcodec/dca_parser.h
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * DCA parser
- * Copyright (C) 2004 Gildas Bazin
- * Copyright (C) 2004 Benjamin Zores
- * Copyright (C) 2006 Benjamin Larsson
- * Copyright (C) 2007 Konstantin Shishkov
- *
- * This file is part of Libav.
- *
- * Libav 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.
- *
- * Libav 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 Libav; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#ifndef AVCODEC_DCA_PARSER_H
-#define AVCODEC_DCA_PARSER_H
-
-#include <stdint.h>
-
-/**
- * Convert bitstream to one representation based on sync marker
- */
-int ff_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
-                             int max_size);
-
-#endif /* AVCODEC_DCA_PARSER_H */
diff --git a/libavcodec/dcadec.c b/libavcodec/dcadec.c
index 1b955e4..8265c0fb 100644
--- a/libavcodec/dcadec.c
+++ b/libavcodec/dcadec.c
@@ -40,7 +40,6 @@
 #include "dcadata.h"
 #include "dcahuff.h"
 #include "dca.h"
-#include "dca_parser.h"
 #include "mathops.h"
 #include "synth_filter.h"
 #include "dcadsp.h"
@@ -739,7 +738,7 @@
 
     if (s->lfe == 3) {
         s->lfe = 0;
-        av_log_ask_for_sample(s->avctx, "LFE is 3\n");
+        avpriv_request_sample(s->avctx, "LFE = 3");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -1012,7 +1011,7 @@
         /* Scale factor index */
         quant7 = get_bits(&s->gb, 8);
         if (quant7 > 127) {
-            av_log_ask_for_sample(s->avctx, "LFEScaleIndex larger than 127\n");
+            avpriv_request_sample(s->avctx, "LFEScaleIndex larger than 127");
             return AVERROR_INVALIDDATA;
         }
         s->lfe_scale_factor = scale_factor_quant7[quant7];
@@ -1985,14 +1984,15 @@
 
         num_audiop = get_bits(&s->gb, 3) + 1;
         if (num_audiop > 1) {
-            av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio presentations.");
+            avpriv_request_sample(s->avctx,
+                                  "Multiple DTS-HD audio presentations");
             /* ignore such streams for now */
             return;
         }
 
         num_assets = get_bits(&s->gb, 3) + 1;
         if (num_assets > 1) {
-            av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio assets.");
+            avpriv_request_sample(s->avctx, "Multiple DTS-HD audio assets");
             /* ignore such streams for now */
             return;
         }
@@ -2166,7 +2166,7 @@
                 }
 
                 if (s->xch_base_channel < 2) {
-                    av_log_ask_for_sample(avctx, "XCh with fewer than 2 base channels is not supported\n");
+                    avpriv_request_sample(avctx, "XCh with fewer than 2 base channels");
                     continue;
                 }
 
@@ -2355,10 +2355,8 @@
 
     /* get output buffer */
     frame->nb_samples = 256 * (s->sample_blocks / 8);
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     samples_flt = (float **)frame->extended_data;
 
     /* allocate buffer for extra channels if downmixing */
diff --git a/libavcodec/dcaenc.c b/libavcodec/dcaenc.c
index 2c9daee..4799ef4 100644
--- a/libavcodec/dcaenc.c
+++ b/libavcodec/dcaenc.c
@@ -497,7 +497,7 @@
     const int16_t *samples;
     int ret, real_channel = 0;
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, DCA_MAX_FRAME_SIZE + DCA_HEADER_SIZE)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, DCA_MAX_FRAME_SIZE + DCA_HEADER_SIZE)) < 0)
         return ret;
 
     samples = (const int16_t *)frame->data[0];
diff --git a/libavcodec/dct-test.c b/libavcodec/dct-test.c
index 29f47e8..3647336 100644
--- a/libavcodec/dct-test.c
+++ b/libavcodec/dct-test.c
@@ -49,9 +49,6 @@
 
 #undef printf
 
-void ff_mmx_idct(int16_t *data);
-void ff_mmxext_idct(int16_t *data);
-
 // BFIN
 void ff_bfin_idct(int16_t *block);
 void ff_bfin_fdct(int16_t *block);
@@ -126,10 +123,6 @@
     { "SIMPLE-C",       ff_simple_idct_8,      NO_PERM  },
 
 #if HAVE_MMX_INLINE
-#if CONFIG_GPL
-    { "LIBMPEG2-MMX",   ff_mmx_idct,           MMX_PERM,  AV_CPU_FLAG_MMX,  1 },
-    { "LIBMPEG2-MMX2",  ff_mmxext_idct,        MMX_PERM,  AV_CPU_FLAG_MMX2, 1 },
-#endif
     { "SIMPLE-MMX",     ff_simple_idct_mmx,  MMX_SIMPLE_PERM, AV_CPU_FLAG_MMX },
     { "XVID-MMX",       ff_idct_xvid_mmx,      NO_PERM,   AV_CPU_FLAG_MMX,  1 },
     { "XVID-MMXEXT",    ff_idct_xvid_mmxext,   NO_PERM,   AV_CPU_FLAG_MMXEXT, 1 },
diff --git a/libavcodec/dfa.c b/libavcodec/dfa.c
index b324c6e..6a095b2 100644
--- a/libavcodec/dfa.c
+++ b/libavcodec/dfa.c
@@ -29,8 +29,6 @@
 #include "libavutil/mem.h"
 
 typedef struct DfaContext {
-    AVFrame pic;
-
     uint32_t pal[256];
     uint8_t *frame_buf;
 } DfaContext;
@@ -256,13 +254,13 @@
             y        += skip_lines;
             segments = bytestream2_get_le16(gb);
         }
-        if (frame_end <= frame)
-            return -1;
         if (segments & 0x8000) {
             frame[width - 1] = segments & 0xFF;
             segments = bytestream2_get_le16(gb);
         }
         line_ptr = frame;
+        if (frame_end - frame < width)
+            return AVERROR_INVALIDDATA;
         frame += width;
         y++;
         while (segments--) {
@@ -290,9 +288,26 @@
     return 0;
 }
 
-static int decode_unk6(GetByteContext *gb, uint8_t *frame, int width, int height)
+static int decode_tdlt(GetByteContext *gb, uint8_t *frame, int width, int height)
 {
-    return AVERROR_PATCHWELCOME;
+    const uint8_t *frame_end = frame + width * height;
+    uint32_t segments = bytestream2_get_le32(gb);
+    int skip, copy;
+
+    while (segments--) {
+        if (bytestream2_get_bytes_left(gb) < 2)
+            return AVERROR_INVALIDDATA;
+        copy = bytestream2_get_byteu(gb) * 2;
+        skip = bytestream2_get_byteu(gb) * 2;
+        if (frame_end - frame < copy + skip ||
+            bytestream2_get_bytes_left(gb) < copy)
+            return AVERROR_INVALIDDATA;
+        frame += skip;
+        bytestream2_get_buffer(gb, frame, copy);
+        frame += copy;
+    }
+
+    return 0;
 }
 
 static int decode_blck(GetByteContext *gb, uint8_t *frame, int width, int height)
@@ -306,17 +321,18 @@
 
 static const chunk_decoder decoder[8] = {
     decode_copy, decode_tsw1, decode_bdlt, decode_wdlt,
-    decode_unk6, decode_dsw1, decode_blck, decode_dds1,
+    decode_tdlt, decode_dsw1, decode_blck, decode_dds1,
 };
 
 static const char* chunk_name[8] = {
-    "COPY", "TSW1", "BDLT", "WDLT", "????", "DSW1", "BLCK", "DDS1"
+    "COPY", "TSW1", "BDLT", "WDLT", "TDLT", "DSW1", "BLCK", "DDS1"
 };
 
 static int dfa_decode_frame(AVCodecContext *avctx,
                             void *data, int *got_frame,
                             AVPacket *avpkt)
 {
+    AVFrame *frame = data;
     DfaContext *s = avctx->priv_data;
     GetByteContext gb;
     const uint8_t *buf = avpkt->data;
@@ -324,14 +340,10 @@
     uint8_t *dst;
     int ret;
     int i, pal_elems;
+    int version = avctx->extradata_size==2 ? AV_RL16(avctx->extradata) : 0;
 
-    if (s->pic.data[0])
-        avctx->release_buffer(avctx, &s->pic);
-
-    if ((ret = ff_get_buffer(avctx, &s->pic))) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     bytestream2_init(&gb, avpkt->data, avpkt->size);
     while (bytestream2_get_bytes_left(&gb) > 0) {
@@ -346,7 +358,7 @@
                 s->pal[i] = bytestream2_get_be24(&gb) << 2;
                 s->pal[i] |= 0xFFU << 24 | (s->pal[i] >> 6) & 0x30303;
             }
-            s->pic.palette_has_changed = 1;
+            frame->palette_has_changed = 1;
         } else if (chunk_type <= 9) {
             if (decoder[chunk_type - 2](&gb, s->frame_buf, avctx->width, avctx->height)) {
                 av_log(avctx, AV_LOG_ERROR, "Error decoding %s chunk\n",
@@ -361,16 +373,23 @@
     }
 
     buf = s->frame_buf;
-    dst = s->pic.data[0];
+    dst = frame->data[0];
     for (i = 0; i < avctx->height; i++) {
-        memcpy(dst, buf, avctx->width);
-        dst += s->pic.linesize[0];
-        buf += avctx->width;
+        if(version == 0x100) {
+            int j;
+            for(j = 0; j < avctx->width; j++) {
+                dst[j] = buf[ (i&3)*(avctx->width /4) + (j/4) +
+                             ((j&3)*(avctx->height/4) + (i/4))*avctx->width];
+            }
+        } else {
+            memcpy(dst, buf, avctx->width);
+            buf += avctx->width;
+        }
+        dst += frame->linesize[0];
     }
-    memcpy(s->pic.data[1], s->pal, sizeof(s->pal));
+    memcpy(frame->data[1], s->pal, sizeof(s->pal));
 
     *got_frame = 1;
-    *(AVFrame*)data = s->pic;
 
     return avpkt->size;
 }
@@ -379,9 +398,6 @@
 {
     DfaContext *s = avctx->priv_data;
 
-    if (s->pic.data[0])
-        avctx->release_buffer(avctx, &s->pic);
-
     av_freep(&s->frame_buf);
 
     return 0;
diff --git a/libavcodec/diracdec.c b/libavcodec/diracdec.c
index a791e88..81d2b65 100644
--- a/libavcodec/diracdec.c
+++ b/libavcodec/diracdec.c
@@ -365,7 +365,7 @@
 
     for (i = 0; i < MAX_FRAMES; i++) {
         if (s->all_frames[i].avframe.data[0]) {
-            s->avctx->release_buffer(s->avctx, &s->all_frames[i].avframe);
+            av_frame_unref(&s->all_frames[i].avframe);
             memset(s->all_frames[i].interpolated, 0, sizeof(s->all_frames[i].interpolated));
         }
 
@@ -1671,7 +1671,7 @@
             for (j = 0; j < MAX_FRAMES; j++)
                 if (!s->all_frames[j].avframe.data[0]) {
                     s->ref_pics[i] = &s->all_frames[j];
-                    ff_get_buffer(s->avctx, &s->ref_pics[i]->avframe);
+                    ff_get_buffer(s->avctx, &s->ref_pics[i]->avframe, AV_GET_BUFFER_FLAG_REF);
                     break;
                 }
     }
@@ -1712,6 +1712,7 @@
 {
     DiracFrame *out = s->delay_frames[0];
     int i, out_idx  = 0;
+    int ret;
 
     /* find frame with lowest picture number */
     for (i = 1; s->delay_frames[i]; i++)
@@ -1726,7 +1727,8 @@
     if (out) {
         out->avframe.reference ^= DELAYED_PIC_REF;
         *got_frame = 1;
-        *(AVFrame *)picture = out->avframe;
+        if((ret = av_frame_ref(picture, &out->avframe)) < 0)
+            return ret;
     }
 
     return 0;
@@ -1745,7 +1747,7 @@
 {
     DiracContext *s   = avctx->priv_data;
     DiracFrame *pic   = NULL;
-    int i, parse_code = buf[4];
+    int ret, i, parse_code = buf[4];
     unsigned tmp;
 
     if (size < DATA_UNIT_HEADER_SIZE)
@@ -1809,10 +1811,8 @@
         pic->avframe.key_frame = s->num_refs == 0;             /* [DIRAC_STD] is_intra()      */
         pic->avframe.pict_type = s->num_refs + 1;              /* Definition of AVPictureType in avutil.h */
 
-        if (ff_get_buffer(avctx, &pic->avframe) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
-            return -1;
-        }
+        if ((ret = ff_get_buffer(avctx, &pic->avframe, (parse_code & 0x0C) == 0x0C ? AV_GET_BUFFER_FLAG_REF : 0)) < 0)
+            return ret;
         s->current_picture = pic;
         s->plane[0].stride = pic->avframe.linesize[0];
         s->plane[1].stride = pic->avframe.linesize[1];
@@ -1836,11 +1836,12 @@
     uint8_t *buf        = pkt->data;
     int buf_size        = pkt->size;
     int i, data_unit_size, buf_idx = 0;
+    int ret;
 
     /* release unused frames */
     for (i = 0; i < MAX_FRAMES; i++)
         if (s->all_frames[i].avframe.data[0] && !s->all_frames[i].avframe.reference) {
-            avctx->release_buffer(avctx, &s->all_frames[i].avframe);
+            av_frame_unref(&s->all_frames[i].avframe);
             memset(s->all_frames[i].interpolated, 0, sizeof(s->all_frames[i].interpolated));
         }
 
@@ -1906,12 +1907,14 @@
 
         if (delayed_frame) {
             delayed_frame->avframe.reference ^= DELAYED_PIC_REF;
-            *(AVFrame*)data = delayed_frame->avframe;
+            if((ret=av_frame_ref(data, &delayed_frame->avframe)) < 0)
+                return ret;
             *got_frame = 1;
         }
     } else if (s->current_picture->avframe.display_picture_number == s->frame_number) {
         /* The right frame at the right time :-) */
-        *(AVFrame*)data = s->current_picture->avframe;
+        if((ret=av_frame_ref(data, &s->current_picture->avframe)) < 0)
+            return ret;
         *got_frame = 1;
     }
 
diff --git a/libavcodec/dnxhddec.c b/libavcodec/dnxhddec.c
index 0af685e..11bed00 100644
--- a/libavcodec/dnxhddec.c
+++ b/libavcodec/dnxhddec.c
@@ -35,7 +35,6 @@
 
 typedef struct DNXHDContext {
     AVCodecContext *avctx;
-    AVFrame picture;
     GetBitContext gb;
     int64_t cid;                        ///< compression id
     unsigned int width, height;
@@ -67,10 +66,6 @@
     DNXHDContext *ctx = avctx->priv_data;
 
     ctx->avctx = avctx;
-    avctx->coded_frame = &ctx->picture;
-    avcodec_get_frame_defaults(&ctx->picture);
-    ctx->picture.type = AV_PICTURE_TYPE_I;
-    ctx->picture.key_frame = 1;
     ctx->cid = -1;
     return 0;
 }
@@ -110,7 +105,8 @@
     return 0;
 }
 
-static int dnxhd_decode_header(DNXHDContext *ctx, const uint8_t *buf, int buf_size, int first_field)
+static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,
+                               const uint8_t *buf, int buf_size, int first_field)
 {
     static const uint8_t header_prefix[] = { 0x00, 0x00, 0x02, 0x80, 0x01 };
     int i, cid;
@@ -124,8 +120,8 @@
     }
     if (buf[5] & 2) { /* interlaced */
         ctx->cur_field = buf[5] & 1;
-        ctx->picture.interlaced_frame = 1;
-        ctx->picture.top_field_first = first_field ^ ctx->cur_field;
+        frame->interlaced_frame = 1;
+        frame->top_field_first  = first_field ^ ctx->cur_field;
         av_log(ctx->avctx, AV_LOG_DEBUG, "interlaced %d, cur field %d\n", buf[5] & 3, ctx->cur_field);
     }
 
@@ -168,11 +164,11 @@
 
     av_dlog(ctx->avctx, "mb width %d, mb height %d\n", ctx->mb_width, ctx->mb_height);
 
-    if ((ctx->height+15)>>4 == ctx->mb_height && ctx->picture.interlaced_frame)
+    if ((ctx->height+15)>>4 == ctx->mb_height && frame->interlaced_frame)
         ctx->height <<= 1;
 
     if (ctx->mb_height > 68 ||
-        (ctx->mb_height<<ctx->picture.interlaced_frame) > (ctx->height+15)>>4) {
+        (ctx->mb_height << frame->interlaced_frame) > (ctx->height+15)>>4) {
         av_log(ctx->avctx, AV_LOG_ERROR, "mb height too big: %d\n", ctx->mb_height);
         return -1;
     }
@@ -284,11 +280,11 @@
     dnxhd_decode_dct_block(ctx, block, n, qscale, 6, 8, 4);
 }
 
-static int dnxhd_decode_macroblock(DNXHDContext *ctx, int x, int y)
+static int dnxhd_decode_macroblock(DNXHDContext *ctx, AVFrame *frame, int x, int y)
 {
     int shift1 = ctx->bit_depth == 10;
-    int dct_linesize_luma   = ctx->picture.linesize[0];
-    int dct_linesize_chroma = ctx->picture.linesize[1];
+    int dct_linesize_luma   = frame->linesize[0];
+    int dct_linesize_chroma = frame->linesize[1];
     uint8_t *dest_y, *dest_u, *dest_v;
     int dct_y_offset, dct_x_offset;
     int qscale, i;
@@ -309,19 +305,19 @@
         ctx->decode_dct_block(ctx, ctx->blocks[i], i, qscale);
     }
 
-    if (ctx->picture.interlaced_frame) {
+    if (frame->interlaced_frame) {
         dct_linesize_luma   <<= 1;
         dct_linesize_chroma <<= 1;
     }
 
-    dest_y = ctx->picture.data[0] + ((y * dct_linesize_luma)   << 4) + (x << (4 + shift1));
-    dest_u = ctx->picture.data[1] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1));
-    dest_v = ctx->picture.data[2] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1));
+    dest_y = frame->data[0] + ((y * dct_linesize_luma)   << 4) + (x << (4 + shift1));
+    dest_u = frame->data[1] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1));
+    dest_v = frame->data[2] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1));
 
     if (ctx->cur_field) {
-        dest_y += ctx->picture.linesize[0];
-        dest_u += ctx->picture.linesize[1];
-        dest_v += ctx->picture.linesize[2];
+        dest_y += frame->linesize[0];
+        dest_u += frame->linesize[1];
+        dest_v += frame->linesize[2];
     }
 
     dct_y_offset = dct_linesize_luma << 3;
@@ -342,7 +338,8 @@
     return 0;
 }
 
-static int dnxhd_decode_macroblocks(DNXHDContext *ctx, const uint8_t *buf, int buf_size)
+static int dnxhd_decode_macroblocks(DNXHDContext *ctx, AVFrame *frame,
+                                    const uint8_t *buf, int buf_size)
 {
     int x, y;
     for (y = 0; y < ctx->mb_height; y++) {
@@ -352,7 +349,7 @@
         init_get_bits(&ctx->gb, buf + ctx->mb_scan_index[y], (buf_size - ctx->mb_scan_index[y]) << 3);
         for (x = 0; x < ctx->mb_width; x++) {
             //START_TIMER;
-            dnxhd_decode_macroblock(ctx, x, y);
+            dnxhd_decode_macroblock(ctx, frame, x, y);
             //STOP_TIMER("decode macroblock");
         }
     }
@@ -365,6 +362,7 @@
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     DNXHDContext *ctx = avctx->priv_data;
+    ThreadFrame frame = { .f = data };
     AVFrame *picture = data;
     int first_field = 1;
     int ret;
@@ -372,7 +370,7 @@
     av_dlog(avctx, "frame size %d\n", buf_size);
 
  decode_coding_unit:
-    if (dnxhd_decode_header(ctx, buf, buf_size, first_field) < 0)
+    if (dnxhd_decode_header(ctx, picture, buf, buf_size, first_field) < 0)
         return -1;
 
     if ((avctx->width || avctx->height) &&
@@ -387,34 +385,29 @@
     avcodec_set_dimensions(avctx, ctx->width, ctx->height);
 
     if (first_field) {
-        if (ctx->picture.data[0])
-            ff_thread_release_buffer(avctx, &ctx->picture);
-        if ((ret = ff_thread_get_buffer(avctx, &ctx->picture)) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
             return ret;
-        }
+        picture->pict_type = AV_PICTURE_TYPE_I;
+        picture->key_frame = 1;
     }
 
-    dnxhd_decode_macroblocks(ctx, buf + 0x280, buf_size - 0x280);
+    dnxhd_decode_macroblocks(ctx, picture, buf + 0x280, buf_size - 0x280);
 
-    if (first_field && ctx->picture.interlaced_frame) {
+    if (first_field && picture->interlaced_frame) {
         buf      += ctx->cid_table->coding_unit_size;
         buf_size -= ctx->cid_table->coding_unit_size;
         first_field = 0;
         goto decode_coding_unit;
     }
 
-    *picture = ctx->picture;
     *got_frame = 1;
-    return buf_size;
+    return avpkt->size;
 }
 
 static av_cold int dnxhd_decode_close(AVCodecContext *avctx)
 {
     DNXHDContext *ctx = avctx->priv_data;
 
-    if (ctx->picture.data[0])
-        ff_thread_release_buffer(avctx, &ctx->picture);
     ff_free_vlc(&ctx->ac_vlc);
     ff_free_vlc(&ctx->dc_vlc);
     ff_free_vlc(&ctx->run_vlc);
diff --git a/libavcodec/dpcm.c b/libavcodec/dpcm.c
index 1ec8370..402b0f9 100644
--- a/libavcodec/dpcm.c
+++ b/libavcodec/dpcm.c
@@ -211,10 +211,8 @@
 
     /* get output buffer */
     frame->nb_samples = (out + avctx->channels - 1) / avctx->channels;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     output_samples = (int16_t *)frame->data[0];
     samples_end = output_samples + out;
 
diff --git a/libavcodec/dpx.c b/libavcodec/dpx.c
index 310036b..2fd5bdc 100644
--- a/libavcodec/dpx.c
+++ b/libavcodec/dpx.c
@@ -25,11 +25,6 @@
 #include "avcodec.h"
 #include "internal.h"
 
-typedef struct DPXContext {
-    AVFrame picture;
-} DPXContext;
-
-
 static unsigned int read32(const uint8_t **ptr, int is_big)
 {
     unsigned int temp;
@@ -64,9 +59,7 @@
 {
     const uint8_t *buf = avpkt->data;
     int buf_size       = avpkt->size;
-    DPXContext *const s = avctx->priv_data;
-    AVFrame *picture  = data;
-    AVFrame *const p = &s->picture;
+    AVFrame *const p = data;
     uint8_t *ptr[AV_NUM_DATA_POINTERS];
 
     unsigned int offset;
@@ -159,7 +152,7 @@
                 return -1;
             }
             avctx->pix_fmt = AV_PIX_FMT_GBRP10;
-            total_size = (avctx->width * avctx->height * elements + 2) / 3 * 4;
+            total_size = (avctx->width * elements + 2) / 3 * 4 * avctx->height;
             break;
         case 12:
             if (!packing) {
@@ -186,12 +179,8 @@
             return AVERROR_INVALIDDATA;
     }
 
-    if (s->picture.data[0])
-        avctx->release_buffer(avctx, &s->picture);
-    if ((ret = ff_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
         return ret;
-    }
 
     // Move pointer to offset from start of file
     buf =  avpkt->data + offset;
@@ -221,6 +210,7 @@
                     read10in32(&buf, &rgbBuffer,
                                &n_datum, endian);
             }
+            n_datum = 0;
             for (i = 0; i < 3; i++)
                 ptr[i] += p->linesize[i];
         }
@@ -262,36 +252,15 @@
         break;
     }
 
-    *picture   = s->picture;
     *got_frame = 1;
 
     return buf_size;
 }
 
-static av_cold int decode_init(AVCodecContext *avctx)
-{
-    DPXContext *s = avctx->priv_data;
-    avcodec_get_frame_defaults(&s->picture);
-    avctx->coded_frame = &s->picture;
-    return 0;
-}
-
-static av_cold int decode_end(AVCodecContext *avctx)
-{
-    DPXContext *s = avctx->priv_data;
-    if (s->picture.data[0])
-        avctx->release_buffer(avctx, &s->picture);
-
-    return 0;
-}
-
 AVCodec ff_dpx_decoder = {
     .name           = "dpx",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_DPX,
-    .priv_data_size = sizeof(DPXContext),
-    .init           = decode_init,
-    .close          = decode_end,
     .decode         = decode_frame,
     .long_name      = NULL_IF_CONFIG_SMALL("DPX image"),
     .capabilities   = CODEC_CAP_DR1,
diff --git a/libavcodec/dpxenc.c b/libavcodec/dpxenc.c
index bd44b16..f210bbc 100644
--- a/libavcodec/dpxenc.c
+++ b/libavcodec/dpxenc.c
@@ -212,6 +212,7 @@
     buf[803] = s->bits_per_component;
     write16(buf + 804, (s->bits_per_component == 10 || s->bits_per_component == 12) ?
                        1 : 0); /* packing method */
+    write32(buf + 808, HEADER_SIZE); /* data offset */
 
     /* Image source information header */
     write32(buf + 1628, avctx->sample_aspect_ratio.num);
diff --git a/libavcodec/dsicinav.c b/libavcodec/dsicinav.c
index 85eafcf..f0e8f02 100644
--- a/libavcodec/dsicinav.c
+++ b/libavcodec/dsicinav.c
@@ -305,11 +305,8 @@
         break;
     }
 
-    cin->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
-    if ((res = avctx->reget_buffer(avctx, &cin->frame))) {
-        av_log(cin->avctx, AV_LOG_ERROR, "failed to allocate a frame\n");
+    if ((res = ff_reget_buffer(avctx, &cin->frame)) < 0)
         return res;
-    }
 
     memcpy(cin->frame.data[1], cin->palette, sizeof(cin->palette));
     cin->frame.palette_has_changed = 1;
@@ -320,8 +317,10 @@
 
     FFSWAP(uint8_t *, cin->bitmap_table[CIN_CUR_BMP], cin->bitmap_table[CIN_PRE_BMP]);
 
+    if ((res = av_frame_ref(data, &cin->frame)) < 0)
+        return res;
+
     *got_frame = 1;
-    *(AVFrame *)data = cin->frame;
 
     return buf_size;
 }
@@ -330,8 +329,7 @@
 {
     CinVideoContext *cin = avctx->priv_data;
 
-    if (cin->frame.data[0])
-        avctx->release_buffer(avctx, &cin->frame);
+    av_frame_unref(&cin->frame);
 
     destroy_buffers(cin);
 
@@ -363,10 +361,8 @@
 
     /* get output buffer */
     frame->nb_samples = avpkt->size - cin->initial_decode_frame;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     samples = (int16_t *)frame->data[0];
 
     delta = cin->delta;
diff --git a/libavcodec/dsputil.c b/libavcodec/dsputil.c
index 2bcc3ee..d1e466a 100644
--- a/libavcodec/dsputil.c
+++ b/libavcodec/dsputil.c
@@ -42,22 +42,9 @@
 #include "config.h"
 #include "diracdsp.h"
 
-uint8_t ff_cropTbl[256 + 2 * MAX_NEG_CROP] = {0, };
 uint32_t ff_squareTbl[512] = {0, };
 
-#define BIT_DEPTH 9
-#include "dsputil_template.c"
-#undef BIT_DEPTH
-
-#define BIT_DEPTH 10
-#include "dsputil_template.c"
-#undef BIT_DEPTH
-
-#define BIT_DEPTH 12
-#include "dsputil_template.c"
-#undef BIT_DEPTH
-
-#define BIT_DEPTH 14
+#define BIT_DEPTH 16
 #include "dsputil_template.c"
 #undef BIT_DEPTH
 
@@ -68,17 +55,6 @@
 #define pb_7f (~0UL/255 * 0x7f)
 #define pb_80 (~0UL/255 * 0x80)
 
-const uint8_t ff_zigzag_direct[64] = {
-    0,   1,  8, 16,  9,  2,  3, 10,
-    17, 24, 32, 25, 18, 11,  4,  5,
-    12, 19, 26, 33, 40, 48, 41, 34,
-    27, 20, 13,  6,  7, 14, 21, 28,
-    35, 42, 49, 56, 57, 50, 43, 36,
-    29, 22, 15, 23, 30, 37, 44, 51,
-    58, 59, 52, 45, 38, 31, 39, 46,
-    53, 60, 61, 54, 47, 55, 62, 63
-};
-
 /* Specific zigzag scan for 248 idct. NOTE that unlike the
    specification, we interleave the fields */
 const uint8_t ff_zigzag248_direct[64] = {
@@ -821,7 +797,7 @@
 
 #define QPEL_MC(r, OPNAME, RND, OP) \
 static void OPNAME ## mpeg4_qpel8_h_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){\
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
     int i;\
     for(i=0; i<h; i++)\
     {\
@@ -840,7 +816,7 @@
 \
 static void OPNAME ## mpeg4_qpel8_v_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
     const int w=8;\
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
     int i;\
     for(i=0; i<w; i++)\
     {\
@@ -867,7 +843,7 @@
 }\
 \
 static void OPNAME ## mpeg4_qpel16_h_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){\
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
     int i;\
     \
     for(i=0; i<h; i++)\
@@ -894,7 +870,7 @@
 }\
 \
 static void OPNAME ## mpeg4_qpel16_v_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
     int i;\
     const int w=16;\
     for(i=0; i<w; i++)\
@@ -937,23 +913,27 @@
     }\
 }\
 \
-static void OPNAME ## qpel8_mc10_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel8_mc10_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t half[64];\
     put ## RND ## mpeg4_qpel8_h_lowpass(half, src, 8, stride, 8);\
     OPNAME ## pixels8_l2_8(dst, src, half, stride, stride, 8, 8);\
 }\
 \
-static void OPNAME ## qpel8_mc20_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel8_mc20_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## mpeg4_qpel8_h_lowpass(dst, src, stride, stride, 8);\
 }\
 \
-static void OPNAME ## qpel8_mc30_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel8_mc30_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t half[64];\
     put ## RND ## mpeg4_qpel8_h_lowpass(half, src, 8, stride, 8);\
     OPNAME ## pixels8_l2_8(dst, src+1, half, stride, stride, 8, 8);\
 }\
 \
-static void OPNAME ## qpel8_mc01_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel8_mc01_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[16*9];\
     uint8_t half[64];\
     copy_block9(full, src, 16, stride, 9);\
@@ -961,20 +941,23 @@
     OPNAME ## pixels8_l2_8(dst, full, half, stride, 16, 8, 8);\
 }\
 \
-static void OPNAME ## qpel8_mc02_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel8_mc02_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[16*9];\
     copy_block9(full, src, 16, stride, 9);\
     OPNAME ## mpeg4_qpel8_v_lowpass(dst, full, stride, 16);\
 }\
 \
-static void OPNAME ## qpel8_mc03_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel8_mc03_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[16*9];\
     uint8_t half[64];\
     copy_block9(full, src, 16, stride, 9);\
     put ## RND ## mpeg4_qpel8_v_lowpass(half, full, 8, 16);\
     OPNAME ## pixels8_l2_8(dst, full+16, half, stride, 16, 8, 8);\
 }\
-void ff_ ## OPNAME ## qpel8_mc11_old_c(uint8_t *dst, uint8_t *src, int stride){\
+void ff_ ## OPNAME ## qpel8_mc11_old_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[16*9];\
     uint8_t halfH[72];\
     uint8_t halfV[64];\
@@ -985,7 +968,8 @@
     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
     OPNAME ## pixels8_l4_8(dst, full, halfH, halfV, halfHV, stride, 16, 8, 8, 8, 8);\
 }\
-static void OPNAME ## qpel8_mc11_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel8_mc11_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[16*9];\
     uint8_t halfH[72];\
     uint8_t halfHV[64];\
@@ -995,7 +979,8 @@
     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
     OPNAME ## pixels8_l2_8(dst, halfH, halfHV, stride, 8, 8, 8);\
 }\
-void ff_ ## OPNAME ## qpel8_mc31_old_c(uint8_t *dst, uint8_t *src, int stride){\
+void ff_ ## OPNAME ## qpel8_mc31_old_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[16*9];\
     uint8_t halfH[72];\
     uint8_t halfV[64];\
@@ -1006,7 +991,8 @@
     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
     OPNAME ## pixels8_l4_8(dst, full+1, halfH, halfV, halfHV, stride, 16, 8, 8, 8, 8);\
 }\
-static void OPNAME ## qpel8_mc31_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel8_mc31_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[16*9];\
     uint8_t halfH[72];\
     uint8_t halfHV[64];\
@@ -1016,7 +1002,8 @@
     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
     OPNAME ## pixels8_l2_8(dst, halfH, halfHV, stride, 8, 8, 8);\
 }\
-void ff_ ## OPNAME ## qpel8_mc13_old_c(uint8_t *dst, uint8_t *src, int stride){\
+void ff_ ## OPNAME ## qpel8_mc13_old_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[16*9];\
     uint8_t halfH[72];\
     uint8_t halfV[64];\
@@ -1027,7 +1014,8 @@
     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
     OPNAME ## pixels8_l4_8(dst, full+16, halfH+8, halfV, halfHV, stride, 16, 8, 8, 8, 8);\
 }\
-static void OPNAME ## qpel8_mc13_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel8_mc13_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[16*9];\
     uint8_t halfH[72];\
     uint8_t halfHV[64];\
@@ -1037,7 +1025,8 @@
     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
     OPNAME ## pixels8_l2_8(dst, halfH+8, halfHV, stride, 8, 8, 8);\
 }\
-void ff_ ## OPNAME ## qpel8_mc33_old_c(uint8_t *dst, uint8_t *src, int stride){\
+void ff_ ## OPNAME ## qpel8_mc33_old_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[16*9];\
     uint8_t halfH[72];\
     uint8_t halfV[64];\
@@ -1048,7 +1037,8 @@
     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
     OPNAME ## pixels8_l4_8(dst, full+17, halfH+8, halfV, halfHV, stride, 16, 8, 8, 8, 8);\
 }\
-static void OPNAME ## qpel8_mc33_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel8_mc33_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[16*9];\
     uint8_t halfH[72];\
     uint8_t halfHV[64];\
@@ -1058,21 +1048,24 @@
     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
     OPNAME ## pixels8_l2_8(dst, halfH+8, halfHV, stride, 8, 8, 8);\
 }\
-static void OPNAME ## qpel8_mc21_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel8_mc21_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t halfH[72];\
     uint8_t halfHV[64];\
     put ## RND ## mpeg4_qpel8_h_lowpass(halfH, src, 8, stride, 9);\
     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
     OPNAME ## pixels8_l2_8(dst, halfH, halfHV, stride, 8, 8, 8);\
 }\
-static void OPNAME ## qpel8_mc23_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel8_mc23_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t halfH[72];\
     uint8_t halfHV[64];\
     put ## RND ## mpeg4_qpel8_h_lowpass(halfH, src, 8, stride, 9);\
     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
     OPNAME ## pixels8_l2_8(dst, halfH+8, halfHV, stride, 8, 8, 8);\
 }\
-void ff_ ## OPNAME ## qpel8_mc12_old_c(uint8_t *dst, uint8_t *src, int stride){\
+void ff_ ## OPNAME ## qpel8_mc12_old_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[16*9];\
     uint8_t halfH[72];\
     uint8_t halfV[64];\
@@ -1083,7 +1076,8 @@
     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
     OPNAME ## pixels8_l2_8(dst, halfV, halfHV, stride, 8, 8, 8);\
 }\
-static void OPNAME ## qpel8_mc12_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel8_mc12_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[16*9];\
     uint8_t halfH[72];\
     copy_block9(full, src, 16, stride, 9);\
@@ -1091,7 +1085,8 @@
     put ## RND ## pixels8_l2_8(halfH, halfH, full, 8, 8, 16, 9);\
     OPNAME ## mpeg4_qpel8_v_lowpass(dst, halfH, stride, 8);\
 }\
-void ff_ ## OPNAME ## qpel8_mc32_old_c(uint8_t *dst, uint8_t *src, int stride){\
+void ff_ ## OPNAME ## qpel8_mc32_old_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[16*9];\
     uint8_t halfH[72];\
     uint8_t halfV[64];\
@@ -1102,7 +1097,8 @@
     put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
     OPNAME ## pixels8_l2_8(dst, halfV, halfHV, stride, 8, 8, 8);\
 }\
-static void OPNAME ## qpel8_mc32_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel8_mc32_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[16*9];\
     uint8_t halfH[72];\
     copy_block9(full, src, 16, stride, 9);\
@@ -1110,29 +1106,34 @@
     put ## RND ## pixels8_l2_8(halfH, halfH, full+1, 8, 8, 16, 9);\
     OPNAME ## mpeg4_qpel8_v_lowpass(dst, halfH, stride, 8);\
 }\
-static void OPNAME ## qpel8_mc22_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel8_mc22_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t halfH[72];\
     put ## RND ## mpeg4_qpel8_h_lowpass(halfH, src, 8, stride, 9);\
     OPNAME ## mpeg4_qpel8_v_lowpass(dst, halfH, stride, 8);\
 }\
 \
-static void OPNAME ## qpel16_mc10_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel16_mc10_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t half[256];\
     put ## RND ## mpeg4_qpel16_h_lowpass(half, src, 16, stride, 16);\
     OPNAME ## pixels16_l2_8(dst, src, half, stride, stride, 16, 16);\
 }\
 \
-static void OPNAME ## qpel16_mc20_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel16_mc20_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## mpeg4_qpel16_h_lowpass(dst, src, stride, stride, 16);\
 }\
 \
-static void OPNAME ## qpel16_mc30_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel16_mc30_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t half[256];\
     put ## RND ## mpeg4_qpel16_h_lowpass(half, src, 16, stride, 16);\
     OPNAME ## pixels16_l2_8(dst, src+1, half, stride, stride, 16, 16);\
 }\
 \
-static void OPNAME ## qpel16_mc01_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel16_mc01_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[24*17];\
     uint8_t half[256];\
     copy_block17(full, src, 24, stride, 17);\
@@ -1140,20 +1141,23 @@
     OPNAME ## pixels16_l2_8(dst, full, half, stride, 24, 16, 16);\
 }\
 \
-static void OPNAME ## qpel16_mc02_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel16_mc02_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[24*17];\
     copy_block17(full, src, 24, stride, 17);\
     OPNAME ## mpeg4_qpel16_v_lowpass(dst, full, stride, 24);\
 }\
 \
-static void OPNAME ## qpel16_mc03_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel16_mc03_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[24*17];\
     uint8_t half[256];\
     copy_block17(full, src, 24, stride, 17);\
     put ## RND ## mpeg4_qpel16_v_lowpass(half, full, 16, 24);\
     OPNAME ## pixels16_l2_8(dst, full+24, half, stride, 24, 16, 16);\
 }\
-void ff_ ## OPNAME ## qpel16_mc11_old_c(uint8_t *dst, uint8_t *src, int stride){\
+void ff_ ## OPNAME ## qpel16_mc11_old_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[24*17];\
     uint8_t halfH[272];\
     uint8_t halfV[256];\
@@ -1164,7 +1168,8 @@
     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
     OPNAME ## pixels16_l4_8(dst, full, halfH, halfV, halfHV, stride, 24, 16, 16, 16, 16);\
 }\
-static void OPNAME ## qpel16_mc11_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel16_mc11_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[24*17];\
     uint8_t halfH[272];\
     uint8_t halfHV[256];\
@@ -1174,7 +1179,8 @@
     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
     OPNAME ## pixels16_l2_8(dst, halfH, halfHV, stride, 16, 16, 16);\
 }\
-void ff_ ## OPNAME ## qpel16_mc31_old_c(uint8_t *dst, uint8_t *src, int stride){\
+void ff_ ## OPNAME ## qpel16_mc31_old_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[24*17];\
     uint8_t halfH[272];\
     uint8_t halfV[256];\
@@ -1185,7 +1191,8 @@
     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
     OPNAME ## pixels16_l4_8(dst, full+1, halfH, halfV, halfHV, stride, 24, 16, 16, 16, 16);\
 }\
-static void OPNAME ## qpel16_mc31_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel16_mc31_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[24*17];\
     uint8_t halfH[272];\
     uint8_t halfHV[256];\
@@ -1195,7 +1202,8 @@
     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
     OPNAME ## pixels16_l2_8(dst, halfH, halfHV, stride, 16, 16, 16);\
 }\
-void ff_ ## OPNAME ## qpel16_mc13_old_c(uint8_t *dst, uint8_t *src, int stride){\
+void ff_ ## OPNAME ## qpel16_mc13_old_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[24*17];\
     uint8_t halfH[272];\
     uint8_t halfV[256];\
@@ -1206,7 +1214,8 @@
     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
     OPNAME ## pixels16_l4_8(dst, full+24, halfH+16, halfV, halfHV, stride, 24, 16, 16, 16, 16);\
 }\
-static void OPNAME ## qpel16_mc13_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel16_mc13_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[24*17];\
     uint8_t halfH[272];\
     uint8_t halfHV[256];\
@@ -1216,7 +1225,8 @@
     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
     OPNAME ## pixels16_l2_8(dst, halfH+16, halfHV, stride, 16, 16, 16);\
 }\
-void ff_ ## OPNAME ## qpel16_mc33_old_c(uint8_t *dst, uint8_t *src, int stride){\
+void ff_ ## OPNAME ## qpel16_mc33_old_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[24*17];\
     uint8_t halfH[272];\
     uint8_t halfV[256];\
@@ -1227,7 +1237,8 @@
     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
     OPNAME ## pixels16_l4_8(dst, full+25, halfH+16, halfV, halfHV, stride, 24, 16, 16, 16, 16);\
 }\
-static void OPNAME ## qpel16_mc33_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel16_mc33_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[24*17];\
     uint8_t halfH[272];\
     uint8_t halfHV[256];\
@@ -1237,21 +1248,24 @@
     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
     OPNAME ## pixels16_l2_8(dst, halfH+16, halfHV, stride, 16, 16, 16);\
 }\
-static void OPNAME ## qpel16_mc21_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel16_mc21_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t halfH[272];\
     uint8_t halfHV[256];\
     put ## RND ## mpeg4_qpel16_h_lowpass(halfH, src, 16, stride, 17);\
     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
     OPNAME ## pixels16_l2_8(dst, halfH, halfHV, stride, 16, 16, 16);\
 }\
-static void OPNAME ## qpel16_mc23_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel16_mc23_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t halfH[272];\
     uint8_t halfHV[256];\
     put ## RND ## mpeg4_qpel16_h_lowpass(halfH, src, 16, stride, 17);\
     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
     OPNAME ## pixels16_l2_8(dst, halfH+16, halfHV, stride, 16, 16, 16);\
 }\
-void ff_ ## OPNAME ## qpel16_mc12_old_c(uint8_t *dst, uint8_t *src, int stride){\
+void ff_ ## OPNAME ## qpel16_mc12_old_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[24*17];\
     uint8_t halfH[272];\
     uint8_t halfV[256];\
@@ -1262,7 +1276,8 @@
     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
     OPNAME ## pixels16_l2_8(dst, halfV, halfHV, stride, 16, 16, 16);\
 }\
-static void OPNAME ## qpel16_mc12_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel16_mc12_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[24*17];\
     uint8_t halfH[272];\
     copy_block17(full, src, 24, stride, 17);\
@@ -1270,7 +1285,8 @@
     put ## RND ## pixels16_l2_8(halfH, halfH, full, 16, 16, 24, 17);\
     OPNAME ## mpeg4_qpel16_v_lowpass(dst, halfH, stride, 16);\
 }\
-void ff_ ## OPNAME ## qpel16_mc32_old_c(uint8_t *dst, uint8_t *src, int stride){\
+void ff_ ## OPNAME ## qpel16_mc32_old_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[24*17];\
     uint8_t halfH[272];\
     uint8_t halfV[256];\
@@ -1281,7 +1297,8 @@
     put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
     OPNAME ## pixels16_l2_8(dst, halfV, halfHV, stride, 16, 16, 16);\
 }\
-static void OPNAME ## qpel16_mc32_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel16_mc32_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[24*17];\
     uint8_t halfH[272];\
     copy_block17(full, src, 24, stride, 17);\
@@ -1289,7 +1306,8 @@
     put ## RND ## pixels16_l2_8(halfH, halfH, full+1, 16, 16, 24, 17);\
     OPNAME ## mpeg4_qpel16_v_lowpass(dst, halfH, stride, 16);\
 }\
-static void OPNAME ## qpel16_mc22_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## qpel16_mc22_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t halfH[272];\
     put ## RND ## mpeg4_qpel16_h_lowpass(halfH, src, 16, stride, 17);\
     OPNAME ## mpeg4_qpel16_v_lowpass(dst, halfH, stride, 16);\
@@ -1309,15 +1327,32 @@
 #undef op_put
 #undef op_put_no_rnd
 
+void ff_put_pixels8x8_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
+{
+    put_pixels8_8_c(dst, src, stride, 8);
+}
+void ff_avg_pixels8x8_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
+{
+    avg_pixels8_8_c(dst, src, stride, 8);
+}
+void ff_put_pixels16x16_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
+{
+    put_pixels16_8_c(dst, src, stride, 16);
+}
+void ff_avg_pixels16x16_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
+{
+    avg_pixels16_8_c(dst, src, stride, 16);
+}
+
 #define put_qpel8_mc00_c  ff_put_pixels8x8_c
 #define avg_qpel8_mc00_c  ff_avg_pixels8x8_c
 #define put_qpel16_mc00_c ff_put_pixels16x16_c
 #define avg_qpel16_mc00_c ff_avg_pixels16x16_c
 #define put_no_rnd_qpel8_mc00_c  ff_put_pixels8x8_c
-#define put_no_rnd_qpel16_mc00_c ff_put_pixels16x16_8_c
+#define put_no_rnd_qpel16_mc00_c ff_put_pixels16x16_c
 
 static void wmv2_mspel8_h_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
     int i;
 
     for(i=0; i<h; i++){
@@ -1335,16 +1370,20 @@
 }
 
 #if CONFIG_RV40_DECODER
-void ff_put_rv40_qpel16_mc33_c(uint8_t *dst, uint8_t *src, int stride){
+void ff_put_rv40_qpel16_mc33_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
+{
     put_pixels16_xy2_8_c(dst, src, stride, 16);
 }
-void ff_avg_rv40_qpel16_mc33_c(uint8_t *dst, uint8_t *src, int stride){
+void ff_avg_rv40_qpel16_mc33_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
+{
     avg_pixels16_xy2_8_c(dst, src, stride, 16);
 }
-void ff_put_rv40_qpel8_mc33_c(uint8_t *dst, uint8_t *src, int stride){
+void ff_put_rv40_qpel8_mc33_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
+{
     put_pixels8_xy2_8_c(dst, src, stride, 8);
 }
-void ff_avg_rv40_qpel8_mc33_c(uint8_t *dst, uint8_t *src, int stride){
+void ff_avg_rv40_qpel8_mc33_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
+{
     avg_pixels8_xy2_8_c(dst, src, stride, 8);
 }
 #endif /* CONFIG_RV40_DECODER */
@@ -1395,7 +1434,7 @@
 #endif
 
 static void wmv2_mspel8_v_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int w){
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
     int i;
 
     for(i=0; i<w; i++){
@@ -1423,27 +1462,32 @@
     }
 }
 
-static void put_mspel8_mc10_c(uint8_t *dst, uint8_t *src, int stride){
+static void put_mspel8_mc10_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
+{
     uint8_t half[64];
     wmv2_mspel8_h_lowpass(half, src, 8, stride, 8);
     put_pixels8_l2_8(dst, src, half, stride, stride, 8, 8);
 }
 
-static void put_mspel8_mc20_c(uint8_t *dst, uint8_t *src, int stride){
+static void put_mspel8_mc20_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
+{
     wmv2_mspel8_h_lowpass(dst, src, stride, stride, 8);
 }
 
-static void put_mspel8_mc30_c(uint8_t *dst, uint8_t *src, int stride){
+static void put_mspel8_mc30_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
+{
     uint8_t half[64];
     wmv2_mspel8_h_lowpass(half, src, 8, stride, 8);
     put_pixels8_l2_8(dst, src+1, half, stride, stride, 8, 8);
 }
 
-static void put_mspel8_mc02_c(uint8_t *dst, uint8_t *src, int stride){
+static void put_mspel8_mc02_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
+{
     wmv2_mspel8_v_lowpass(dst, src, stride, stride, 8);
 }
 
-static void put_mspel8_mc12_c(uint8_t *dst, uint8_t *src, int stride){
+static void put_mspel8_mc12_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
+{
     uint8_t halfH[88];
     uint8_t halfV[64];
     uint8_t halfHV[64];
@@ -1452,7 +1496,8 @@
     wmv2_mspel8_v_lowpass(halfHV, halfH+8, 8, 8, 8);
     put_pixels8_l2_8(dst, halfV, halfHV, stride, 8, 8, 8);
 }
-static void put_mspel8_mc32_c(uint8_t *dst, uint8_t *src, int stride){
+static void put_mspel8_mc32_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
+{
     uint8_t halfH[88];
     uint8_t halfV[64];
     uint8_t halfHV[64];
@@ -1461,7 +1506,8 @@
     wmv2_mspel8_v_lowpass(halfHV, halfH+8, 8, 8, 8);
     put_pixels8_l2_8(dst, halfV, halfHV, stride, 8, 8, 8);
 }
-static void put_mspel8_mc22_c(uint8_t *dst, uint8_t *src, int stride){
+static void put_mspel8_mc22_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
+{
     uint8_t halfH[88];
     wmv2_mspel8_h_lowpass(halfH, src-stride, 8, stride, 11);
     wmv2_mspel8_v_lowpass(dst, halfH+8, stride, 8, 8);
@@ -1541,33 +1587,6 @@
     }
 }
 
-static void h261_loop_filter_c(uint8_t *src, int stride){
-    int x,y,xy,yz;
-    int temp[64];
-
-    for(x=0; x<8; x++){
-        temp[x      ] = 4*src[x           ];
-        temp[x + 7*8] = 4*src[x + 7*stride];
-    }
-    for(y=1; y<7; y++){
-        for(x=0; x<8; x++){
-            xy = y * stride + x;
-            yz = y * 8 + x;
-            temp[yz] = src[xy - stride] + 2*src[xy] + src[xy + stride];
-        }
-    }
-
-    for(y=0; y<8; y++){
-        src[  y*stride] = (temp[  y*8] + 2)>>2;
-        src[7+y*stride] = (temp[7+y*8] + 2)>>2;
-        for(x=1; x<7; x++){
-            xy = y * stride + x;
-            yz = y * 8 + x;
-            src[xy] = (temp[yz-1] + 2*temp[yz] + temp[yz+1] + 8)>>4;
-        }
-    }
-}
-
 static inline int pix_abs16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
 {
     int s, i;
@@ -2626,12 +2645,6 @@
 {
     int i;
 
-    for(i=0;i<256;i++) ff_cropTbl[i + MAX_NEG_CROP] = i;
-    for(i=0;i<MAX_NEG_CROP;i++) {
-        ff_cropTbl[i] = 0;
-        ff_cropTbl[i + MAX_NEG_CROP + 256] = 255;
-    }
-
     for(i=0;i<512;i++) {
         ff_squareTbl[i] = (i - 256) * (i - 256);
     }
@@ -2856,8 +2869,6 @@
         c->h263_v_loop_filter= h263_v_loop_filter_c;
     }
 
-    c->h261_loop_filter= h261_loop_filter_c;
-
     c->try_8x8basis= try_8x8basis_c;
     c->add_8x8basis= add_8x8basis_c;
 
@@ -2874,67 +2885,28 @@
 
     c->add_pixels8 = add_pixels8_c;
 
-#define hpel_funcs(prefix, idx, num) \
-    c->prefix ## _pixels_tab idx [0] = prefix ## _pixels ## num ## _8_c; \
-    c->prefix ## _pixels_tab idx [1] = prefix ## _pixels ## num ## _x2_8_c; \
-    c->prefix ## _pixels_tab idx [2] = prefix ## _pixels ## num ## _y2_8_c; \
-    c->prefix ## _pixels_tab idx [3] = prefix ## _pixels ## num ## _xy2_8_c
-
-    hpel_funcs(put, [0], 16);
-    hpel_funcs(put, [1],  8);
-    hpel_funcs(put, [2],  4);
-    hpel_funcs(put, [3],  2);
-    hpel_funcs(put_no_rnd, [0], 16);
-    hpel_funcs(put_no_rnd, [1],  8);
-    hpel_funcs(avg, [0], 16);
-    hpel_funcs(avg, [1],  8);
-    hpel_funcs(avg, [2],  4);
-    hpel_funcs(avg, [3],  2);
-    hpel_funcs(avg_no_rnd,, 16);
-
 #undef FUNC
 #undef FUNCC
 #define FUNC(f, depth) f ## _ ## depth
 #define FUNCC(f, depth) f ## _ ## depth ## _c
 
-#define BIT_DEPTH_FUNCS(depth, dct)\
-    c->get_pixels                    = FUNCC(get_pixels   ## dct   , depth);\
-    c->draw_edges                    = FUNCC(draw_edges            , depth);\
-    c->clear_block                   = FUNCC(clear_block  ## dct   , depth);\
-    c->clear_blocks                  = FUNCC(clear_blocks ## dct   , depth);\
+    c->draw_edges                    = FUNCC(draw_edges, 8);
+    c->clear_block                   = FUNCC(clear_block, 8);
+    c->clear_blocks                  = FUNCC(clear_blocks, 8);
+
+#define BIT_DEPTH_FUNCS(depth) \
+    c->get_pixels                    = FUNCC(get_pixels,   depth);
 
     switch (avctx->bits_per_raw_sample) {
     case 9:
-        if (c->dct_bits == 32) {
-            BIT_DEPTH_FUNCS(9, _32);
-        } else {
-            BIT_DEPTH_FUNCS(9, _16);
-        }
-        break;
     case 10:
-        if (c->dct_bits == 32) {
-            BIT_DEPTH_FUNCS(10, _32);
-        } else {
-            BIT_DEPTH_FUNCS(10, _16);
-        }
-        break;
     case 12:
-        if (c->dct_bits == 32) {
-            BIT_DEPTH_FUNCS(12, _32);
-        } else {
-            BIT_DEPTH_FUNCS(12, _16);
-        }
-        break;
     case 14:
-        if (c->dct_bits == 32) {
-            BIT_DEPTH_FUNCS(14, _32);
-        } else {
-            BIT_DEPTH_FUNCS(14, _16);
-        }
+        BIT_DEPTH_FUNCS(16);
         break;
     default:
         if(avctx->bits_per_raw_sample<=8 || avctx->codec_type != AVMEDIA_TYPE_VIDEO) {
-            BIT_DEPTH_FUNCS(8, _16);
+            BIT_DEPTH_FUNCS(8);
         }
         break;
     }
@@ -2956,3 +2928,8 @@
 {
     ff_dsputil_init(c, avctx);
 }
+
+av_cold void avpriv_dsputil_init(DSPContext *c, AVCodecContext *avctx)
+{
+    ff_dsputil_init(c, avctx);
+}
diff --git a/libavcodec/dsputil.h b/libavcodec/dsputil.h
index 7d12fe3..07a95af 100644
--- a/libavcodec/dsputil.h
+++ b/libavcodec/dsputil.h
@@ -32,6 +32,7 @@
 
 #include "libavutil/intreadwrite.h"
 #include "avcodec.h"
+#include "rnd_avg.h"
 
 
 //#define DEBUG
@@ -47,30 +48,18 @@
 
 /* temporary */
 extern uint32_t ff_squareTbl[512];
-extern uint8_t ff_cropTbl[256 + 2 * MAX_NEG_CROP];
+extern const uint8_t ff_cropTbl[256 + 2 * MAX_NEG_CROP];
 
-#define PUTAVG_PIXELS(depth)\
-void ff_put_pixels8x8_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);\
-void ff_avg_pixels8x8_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);\
-void ff_put_pixels16x16_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);\
-void ff_avg_pixels16x16_ ## depth ## _c(uint8_t *dst, uint8_t *src, int stride);
-
-PUTAVG_PIXELS( 8)
-PUTAVG_PIXELS( 9)
-PUTAVG_PIXELS(10)
-PUTAVG_PIXELS(12)
-PUTAVG_PIXELS(14)
-
-#define ff_put_pixels8x8_c ff_put_pixels8x8_8_c
-#define ff_avg_pixels8x8_c ff_avg_pixels8x8_8_c
-#define ff_put_pixels16x16_c ff_put_pixels16x16_8_c
-#define ff_avg_pixels16x16_c ff_avg_pixels16x16_8_c
+void ff_put_pixels8x8_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride);
+void ff_avg_pixels8x8_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride);
+void ff_put_pixels16x16_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride);
+void ff_avg_pixels16x16_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride);
 
 /* RV40 functions */
-void ff_put_rv40_qpel16_mc33_c(uint8_t *dst, uint8_t *src, int stride);
-void ff_avg_rv40_qpel16_mc33_c(uint8_t *dst, uint8_t *src, int stride);
-void ff_put_rv40_qpel8_mc33_c(uint8_t *dst, uint8_t *src, int stride);
-void ff_avg_rv40_qpel8_mc33_c(uint8_t *dst, uint8_t *src, int stride);
+void ff_put_rv40_qpel16_mc33_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride);
+void ff_avg_rv40_qpel16_mc33_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride);
+void ff_put_rv40_qpel8_mc33_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride);
+void ff_avg_rv40_qpel8_mc33_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride);
 
 void ff_gmc_c(uint8_t *dst, uint8_t *src, int stride, int h, int ox, int oy,
               int dxx, int dxy, int dyx, int dyy, int shift, int r, int width, int height);
@@ -91,16 +80,15 @@
 /* add and put pixel (decoding) */
 // blocksizes for op_pixels_func are 8x4,8x8 16x8 16x16
 //h for op_pixels_func is limited to {width/2, width} but never larger than 16 and never smaller than 4
-typedef void (*op_pixels_func)(uint8_t *block/*align width (8 or 16)*/, const uint8_t *pixels/*align 1*/, ptrdiff_t line_size, int h);
 typedef void (*tpel_mc_func)(uint8_t *block/*align width (8 or 16)*/, const uint8_t *pixels/*align 1*/, int line_size, int w, int h);
-typedef void (*qpel_mc_func)(uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
+typedef void (*qpel_mc_func)(uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, ptrdiff_t stride);
 
 typedef void (*op_fill_func)(uint8_t *block/*align width (8 or 16)*/, uint8_t value, int line_size, int h);
 
 #define DEF_OLD_QPEL(name)\
-void ff_put_        ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
-void ff_put_no_rnd_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);\
-void ff_avg_        ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, int stride);
+void ff_put_        ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, ptrdiff_t stride);\
+void ff_put_no_rnd_ ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, ptrdiff_t stride);\
+void ff_avg_        ## name (uint8_t *dst/*align width (8 or 16)*/, uint8_t *src/*align 1*/, ptrdiff_t stride);
 
 DEF_OLD_QPEL(qpel16_mc11_old_c)
 DEF_OLD_QPEL(qpel16_mc31_old_c)
@@ -115,12 +103,6 @@
 DEF_OLD_QPEL(qpel8_mc13_old_c)
 DEF_OLD_QPEL(qpel8_mc33_old_c)
 
-#define CALL_2X_PIXELS(a, b, n)\
-static void a(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
-    b(block  , pixels  , line_size, h);\
-    b(block+n, pixels+n, line_size, h);\
-}
-
 /* motion estimation */
 // h is limited to {width/2, width, 2*width} but never larger than 16 and never smaller than 2
 // although currently h<4 is not used as functions with width <8 are neither used nor implemented
@@ -143,11 +125,6 @@
  * DSPContext.
  */
 typedef struct DSPContext {
-    /**
-     * Size of DCT coefficients.
-     */
-    int dct_bits;
-
     /* pixel ops : interface with DCT */
     void (*get_pixels)(int16_t *block/*align 16*/, const uint8_t *pixels/*align 8*/, int line_size);
     void (*diff_pixels)(int16_t *block/*align 16*/, const uint8_t *s1/*align 8*/, const uint8_t *s2/*align 8*/, int stride);
@@ -197,54 +174,6 @@
                              int size);
 
     /**
-     * Halfpel motion compensation with rounding (a+b+1)>>1.
-     * this is an array[4][4] of motion compensation functions for 4
-     * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
-     * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
-     * @param block destination where the result is stored
-     * @param pixels source
-     * @param line_size number of bytes in a horizontal line of block
-     * @param h height
-     */
-    op_pixels_func put_pixels_tab[4][4];
-
-    /**
-     * Halfpel motion compensation with rounding (a+b+1)>>1.
-     * This is an array[4][4] of motion compensation functions for 4
-     * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
-     * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
-     * @param block destination into which the result is averaged (a+b+1)>>1
-     * @param pixels source
-     * @param line_size number of bytes in a horizontal line of block
-     * @param h height
-     */
-    op_pixels_func avg_pixels_tab[4][4];
-
-    /**
-     * Halfpel motion compensation with no rounding (a+b)>>1.
-     * this is an array[2][4] of motion compensation functions for 2
-     * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
-     * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
-     * @param block destination where the result is stored
-     * @param pixels source
-     * @param line_size number of bytes in a horizontal line of block
-     * @param h height
-     */
-    op_pixels_func put_no_rnd_pixels_tab[4][4];
-
-    /**
-     * Halfpel motion compensation with no rounding (a+b)>>1.
-     * this is an array[4] of motion compensation functions for 1
-     * horizontal blocksize (16) and the 4 halfpel positions<br>
-     * *pixels_tab[0][ xhalfpel + 2*yhalfpel ]
-     * @param block destination into which the result is averaged (a+b)>>1
-     * @param pixels source
-     * @param line_size number of bytes in a horizontal line of block
-     * @param h height
-     */
-    op_pixels_func avg_no_rnd_pixels_tab[4];
-
-    /**
      * Thirdpel motion compensation with rounding (a+b+1)>>1.
      * this is an array[12] of motion compensation functions for the 9 thirdpe
      * positions<br>
@@ -282,8 +211,6 @@
     void (*h263_v_loop_filter)(uint8_t *src, int stride, int qscale);
     void (*h263_h_loop_filter)(uint8_t *src, int stride, int qscale);
 
-    void (*h261_loop_filter)(uint8_t *src, int stride);
-
     /* assume len is a multiple of 8, and arrays are 16-byte aligned */
     void (*vector_clipf)(float *dst /* align 16 */, const float *src /* align 16 */, float min, float max, int len /* align 16 */);
 
@@ -388,6 +315,7 @@
 
 void ff_dsputil_static_init(void);
 void ff_dsputil_init(DSPContext* p, AVCodecContext *avctx);
+void avpriv_dsputil_init(DSPContext* p, AVCodecContext *avctx);
 attribute_deprecated void dsputil_init(DSPContext* c, AVCodecContext *avctx);
 
 int ff_check_alignment(void);
diff --git a/libavcodec/dsputil_template.c b/libavcodec/dsputil_template.c
index 8517ded..96be7a6 100644
--- a/libavcodec/dsputil_template.c
+++ b/libavcodec/dsputil_template.c
@@ -29,6 +29,7 @@
 
 #include "bit_depth_template.c"
 
+#if BIT_DEPTH == 8
 /* draw the edges of width 'w' of an image of size width, height */
 //FIXME check that this is ok for mpeg4 interlaced
 static void FUNCC(draw_edges)(uint8_t *p_buf, int p_wrap, int width, int height, int w, int h, int sides)
@@ -41,16 +42,8 @@
     /* left and right */
     ptr = buf;
     for(i=0;i<height;i++) {
-#if BIT_DEPTH > 8
-        int j;
-        for (j = 0; j < w; j++) {
-            ptr[j-w] = ptr[0];
-            ptr[j+width] = ptr[width-1];
-        }
-#else
         memset(ptr - w, ptr[0], w);
         memset(ptr + width, ptr[width-1], w);
-#endif
         ptr += wrap;
     }
 
@@ -64,50 +57,45 @@
         for (i = 0; i < h; i++)
             memcpy(last_line + (i + 1) * wrap, last_line, (width + w + w) * sizeof(pixel)); // bottom
 }
-
-#define DCTELEM_FUNCS(dctcoef, suffix)                                  \
-static void FUNCC(get_pixels ## suffix)(int16_t *av_restrict _block,    \
-                                        const uint8_t *_pixels,         \
-                                        int line_size)                  \
-{                                                                       \
-    const pixel *pixels = (const pixel *) _pixels;                      \
-    dctcoef *av_restrict block = (dctcoef *) _block;                    \
-    int i;                                                              \
-                                                                        \
-    /* read the pixels */                                               \
-    for(i=0;i<8;i++) {                                                  \
-        block[0] = pixels[0];                                           \
-        block[1] = pixels[1];                                           \
-        block[2] = pixels[2];                                           \
-        block[3] = pixels[3];                                           \
-        block[4] = pixels[4];                                           \
-        block[5] = pixels[5];                                           \
-        block[6] = pixels[6];                                           \
-        block[7] = pixels[7];                                           \
-        pixels += line_size / sizeof(pixel);                            \
-        block += 8;                                                     \
-    }                                                                   \
-}                                                                       \
-                                                                        \
-static void FUNCC(clear_block ## suffix)(int16_t *block)                \
-{                                                                       \
-    memset(block, 0, sizeof(dctcoef)*64);                               \
-}                                                                       \
-                                                                        \
-/**                                                                     \
- * memset(blocks, 0, sizeof(int16_t)*6*64)                              \
- */                                                                     \
-static void FUNCC(clear_blocks ## suffix)(int16_t *blocks)              \
-{                                                                       \
-    memset(blocks, 0, sizeof(dctcoef)*6*64);                            \
-}
-
-DCTELEM_FUNCS(int16_t, _16)
-#if BIT_DEPTH > 8
-DCTELEM_FUNCS(dctcoef, _32)
 #endif
 
+static void FUNCC(get_pixels)(int16_t *av_restrict block,
+                              const uint8_t *_pixels,
+                              int line_size)
+{
+    const pixel *pixels = (const pixel *) _pixels;
+    int i;
+
+    /* read the pixels */
+    for(i=0;i<8;i++) {
+        block[0] = pixels[0];
+        block[1] = pixels[1];
+        block[2] = pixels[2];
+        block[3] = pixels[3];
+        block[4] = pixels[4];
+        block[5] = pixels[5];
+        block[6] = pixels[6];
+        block[7] = pixels[7];
+        pixels += line_size / sizeof(pixel);
+        block += 8;
+    }
+}
+
+#if BIT_DEPTH == 8
+static void FUNCC(clear_block)(int16_t *block)
+{
+    memset(block, 0, sizeof(int16_t)*64);
+}
+
+static void FUNCC(clear_blocks)(int16_t *blocks)
+{
+    memset(blocks, 0, sizeof(int16_t)*6*64);
+}
+#endif
+
+#if BIT_DEPTH == 8
 #include "hpel_template.c"
+#endif
 
 #define PIXOP2(OPNAME, OP) \
 static inline void FUNC(OPNAME ## _no_rnd_pixels8_l2)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, \
@@ -130,22 +118,6 @@
     FUNC(OPNAME ## _no_rnd_pixels8_l2)(dst+8*sizeof(pixel), src1+8*sizeof(pixel), src2+8*sizeof(pixel), dst_stride, src_stride1, src_stride2, h);\
 }\
 \
-static inline void FUNCC(OPNAME ## _no_rnd_pixels8_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
-    FUNC(OPNAME ## _no_rnd_pixels8_l2)(block, pixels, pixels+sizeof(pixel), line_size, line_size, line_size, h);\
-}\
-\
-static inline void FUNCC(OPNAME ## _pixels8_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
-    FUNC(OPNAME ## _pixels8_l2)(block, pixels, pixels+sizeof(pixel), line_size, line_size, line_size, h);\
-}\
-\
-static inline void FUNCC(OPNAME ## _no_rnd_pixels8_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
-    FUNC(OPNAME ## _no_rnd_pixels8_l2)(block, pixels, pixels+line_size, line_size, line_size, line_size, h);\
-}\
-\
-static inline void FUNCC(OPNAME ## _pixels8_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
-    FUNC(OPNAME ## _pixels8_l2)(block, pixels, pixels+line_size, line_size, line_size, line_size, h);\
-}\
-\
 static inline void FUNC(OPNAME ## _pixels8_l4)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, const uint8_t *src3, const uint8_t *src4,\
                  int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
     /* FIXME HIGH BIT DEPTH */\
@@ -183,22 +155,6 @@
     }\
 }\
 \
-static inline void FUNCC(OPNAME ## _pixels4_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
-    FUNC(OPNAME ## _pixels4_l2)(block, pixels, pixels+sizeof(pixel), line_size, line_size, line_size, h);\
-}\
-\
-static inline void FUNCC(OPNAME ## _pixels4_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
-    FUNC(OPNAME ## _pixels4_l2)(block, pixels, pixels+line_size, line_size, line_size, line_size, h);\
-}\
-\
-static inline void FUNCC(OPNAME ## _pixels2_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
-    FUNC(OPNAME ## _pixels2_l2)(block, pixels, pixels+sizeof(pixel), line_size, line_size, line_size, h);\
-}\
-\
-static inline void FUNCC(OPNAME ## _pixels2_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
-    FUNC(OPNAME ## _pixels2_l2)(block, pixels, pixels+line_size, line_size, line_size, line_size, h);\
-}\
-\
 static inline void FUNC(OPNAME ## _no_rnd_pixels8_l4)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, const uint8_t *src3, const uint8_t *src4,\
                  int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
     /* FIXME HIGH BIT DEPTH*/\
@@ -246,79 +202,6 @@
     FUNC(OPNAME ## _no_rnd_pixels8_l4)(dst+8*sizeof(pixel), src1+8*sizeof(pixel), src2+8*sizeof(pixel), src3+8*sizeof(pixel), src4+8*sizeof(pixel), dst_stride, src_stride1, src_stride2, src_stride3, src_stride4, h);\
 }\
 \
-static inline void FUNCC(OPNAME ## _pixels2_xy2)(uint8_t *_block, const uint8_t *_pixels, ptrdiff_t line_size, int h)\
-{\
-        int i, a0, b0, a1, b1;\
-        pixel *block = (pixel*)_block;\
-        const pixel *pixels = (const pixel*)_pixels;\
-        line_size >>= sizeof(pixel)-1;\
-        a0= pixels[0];\
-        b0= pixels[1] + 2;\
-        a0 += b0;\
-        b0 += pixels[2];\
-\
-        pixels+=line_size;\
-        for(i=0; i<h; i+=2){\
-            a1= pixels[0];\
-            b1= pixels[1];\
-            a1 += b1;\
-            b1 += pixels[2];\
-\
-            block[0]= (a1+a0)>>2; /* FIXME non put */\
-            block[1]= (b1+b0)>>2;\
-\
-            pixels+=line_size;\
-            block +=line_size;\
-\
-            a0= pixels[0];\
-            b0= pixels[1] + 2;\
-            a0 += b0;\
-            b0 += pixels[2];\
-\
-            block[0]= (a1+a0)>>2;\
-            block[1]= (b1+b0)>>2;\
-            pixels+=line_size;\
-            block +=line_size;\
-        }\
-}\
-\
-static inline void FUNCC(OPNAME ## _pixels4_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)\
-{\
-        /* FIXME HIGH BIT DEPTH */\
-        int i;\
-        const uint32_t a= AV_RN32(pixels  );\
-        const uint32_t b= AV_RN32(pixels+1);\
-        uint32_t l0=  (a&0x03030303UL)\
-                    + (b&0x03030303UL)\
-                    + 0x02020202UL;\
-        uint32_t h0= ((a&0xFCFCFCFCUL)>>2)\
-                   + ((b&0xFCFCFCFCUL)>>2);\
-        uint32_t l1,h1;\
-\
-        pixels+=line_size;\
-        for(i=0; i<h; i+=2){\
-            uint32_t a= AV_RN32(pixels  );\
-            uint32_t b= AV_RN32(pixels+1);\
-            l1=  (a&0x03030303UL)\
-               + (b&0x03030303UL);\
-            h1= ((a&0xFCFCFCFCUL)>>2)\
-              + ((b&0xFCFCFCFCUL)>>2);\
-            OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
-            pixels+=line_size;\
-            block +=line_size;\
-            a= AV_RN32(pixels  );\
-            b= AV_RN32(pixels+1);\
-            l0=  (a&0x03030303UL)\
-               + (b&0x03030303UL)\
-               + 0x02020202UL;\
-            h0= ((a&0xFCFCFCFCUL)>>2)\
-              + ((b&0xFCFCFCFCUL)>>2);\
-            OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
-            pixels+=line_size;\
-            block +=line_size;\
-        }\
-}\
-\
 static inline void FUNCC(OPNAME ## _pixels8_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)\
 {\
     /* FIXME HIGH BIT DEPTH */\
@@ -361,55 +244,7 @@
     }\
 }\
 \
-static inline void FUNCC(OPNAME ## _no_rnd_pixels8_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)\
-{\
-    /* FIXME HIGH BIT DEPTH */\
-    int j;\
-    for(j=0; j<2; j++){\
-        int i;\
-        const uint32_t a= AV_RN32(pixels  );\
-        const uint32_t b= AV_RN32(pixels+1);\
-        uint32_t l0=  (a&0x03030303UL)\
-                    + (b&0x03030303UL)\
-                    + 0x01010101UL;\
-        uint32_t h0= ((a&0xFCFCFCFCUL)>>2)\
-                   + ((b&0xFCFCFCFCUL)>>2);\
-        uint32_t l1,h1;\
-\
-        pixels+=line_size;\
-        for(i=0; i<h; i+=2){\
-            uint32_t a= AV_RN32(pixels  );\
-            uint32_t b= AV_RN32(pixels+1);\
-            l1=  (a&0x03030303UL)\
-               + (b&0x03030303UL);\
-            h1= ((a&0xFCFCFCFCUL)>>2)\
-              + ((b&0xFCFCFCFCUL)>>2);\
-            OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
-            pixels+=line_size;\
-            block +=line_size;\
-            a= AV_RN32(pixels  );\
-            b= AV_RN32(pixels+1);\
-            l0=  (a&0x03030303UL)\
-               + (b&0x03030303UL)\
-               + 0x01010101UL;\
-            h0= ((a&0xFCFCFCFCUL)>>2)\
-              + ((b&0xFCFCFCFCUL)>>2);\
-            OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
-            pixels+=line_size;\
-            block +=line_size;\
-        }\
-        pixels+=4-line_size*(h+1);\
-        block +=4-line_size*h;\
-    }\
-}\
-\
-CALL_2X_PIXELS(FUNCC(OPNAME ## _pixels16_x2) , FUNCC(OPNAME ## _pixels8_x2) , 8*sizeof(pixel))\
-CALL_2X_PIXELS(FUNCC(OPNAME ## _pixels16_y2) , FUNCC(OPNAME ## _pixels8_y2) , 8*sizeof(pixel))\
 CALL_2X_PIXELS(FUNCC(OPNAME ## _pixels16_xy2), FUNCC(OPNAME ## _pixels8_xy2), 8*sizeof(pixel))\
-av_unused CALL_2X_PIXELS(FUNCC(OPNAME ## _no_rnd_pixels16)    , FUNCC(OPNAME ## _pixels8) , 8*sizeof(pixel))\
-CALL_2X_PIXELS(FUNCC(OPNAME ## _no_rnd_pixels16_x2) , FUNCC(OPNAME ## _no_rnd_pixels8_x2) , 8*sizeof(pixel))\
-CALL_2X_PIXELS(FUNCC(OPNAME ## _no_rnd_pixels16_y2) , FUNCC(OPNAME ## _no_rnd_pixels8_y2) , 8*sizeof(pixel))\
-CALL_2X_PIXELS(FUNCC(OPNAME ## _no_rnd_pixels16_xy2), FUNCC(OPNAME ## _no_rnd_pixels8_xy2), 8*sizeof(pixel))\
 
 #define op_avg(a, b) a = rnd_avg_pixel4(a, b)
 #define op_put(a, b) a = b
@@ -420,17 +255,3 @@
 #endif
 #undef op_avg
 #undef op_put
-
-void FUNCC(ff_put_pixels8x8)(uint8_t *dst, uint8_t *src, int stride) {
-    FUNCC(put_pixels8)(dst, src, stride, 8);
-}
-void FUNCC(ff_avg_pixels8x8)(uint8_t *dst, uint8_t *src, int stride) {
-    FUNCC(avg_pixels8)(dst, src, stride, 8);
-}
-void FUNCC(ff_put_pixels16x16)(uint8_t *dst, uint8_t *src, int stride) {
-    FUNCC(put_pixels16)(dst, src, stride, 16);
-}
-void FUNCC(ff_avg_pixels16x16)(uint8_t *dst, uint8_t *src, int stride) {
-    FUNCC(avg_pixels16)(dst, src, stride, 16);
-}
-
diff --git a/libavcodec/dv.c b/libavcodec/dv.c
index 0812dbb..39a87a9 100644
--- a/libavcodec/dv.c
+++ b/libavcodec/dv.c
@@ -320,6 +320,7 @@
     }else
         memcpy(s->dv_zigzag[1], ff_zigzag248_direct, 64);
 
+    avcodec_get_frame_defaults(&s->picture);
     avctx->coded_frame = &s->picture;
     s->avctx = avctx;
     avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
diff --git a/libavcodec/dvd_nav_parser.c b/libavcodec/dvd_nav_parser.c
new file mode 100644
index 0000000..4b03e39
--- /dev/null
+++ b/libavcodec/dvd_nav_parser.c
@@ -0,0 +1,116 @@
+/*
+ * DVD navigation block parser for FFmpeg
+ * Copyright (c) 2013 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
+ */
+#include "avcodec.h"
+#include "dsputil.h"
+#include "get_bits.h"
+#include "parser.h"
+
+#define PCI_SIZE  980
+#define DSI_SIZE 1018
+
+/* parser definition */
+typedef struct DVDNavParseContext {
+    uint32_t     lba;
+    uint8_t      buffer[PCI_SIZE+DSI_SIZE];
+    int          copied;
+} DVDNavParseContext;
+
+static av_cold int dvd_nav_parse_init(AVCodecParserContext *s)
+{
+    DVDNavParseContext *pc = s->priv_data;
+
+    pc->lba    = 0xFFFFFFFF;
+    pc->copied = 0;
+    return 0;
+}
+
+static int dvd_nav_parse(AVCodecParserContext *s,
+                         AVCodecContext *avctx,
+                         const uint8_t **poutbuf, int *poutbuf_size,
+                         const uint8_t *buf, int buf_size)
+{
+    DVDNavParseContext *pc1 = s->priv_data;
+    int lastPacket          = 0;
+    int valid               = 0;
+
+    s->pict_type = AV_PICTURE_TYPE_NONE;
+
+    avctx->time_base.num = 1;
+    avctx->time_base.den = 90000;
+
+    if (buf && buf_size) {
+        switch(buf[0]) {
+            case 0x00:
+                if (buf_size == PCI_SIZE) {
+                    /* PCI */
+                    uint32_t lba      = AV_RB32(&buf[0x01]);
+                    uint32_t startpts = AV_RB32(&buf[0x0D]);
+                    uint32_t endpts   = AV_RB32(&buf[0x11]);
+
+                    if (endpts > startpts) {
+                        pc1->lba    = lba;
+                        s->pts      = (int64_t)startpts;
+                        s->duration = endpts - startpts;
+
+                        memcpy(pc1->buffer, buf, PCI_SIZE);
+                        pc1->copied = PCI_SIZE;
+                        valid       = 1;
+                    }
+                }
+                break;
+
+            case 0x01:
+                if ((buf_size == DSI_SIZE) && (pc1->copied == PCI_SIZE)) {
+                    /* DSI */
+                    uint32_t lba = AV_RB32(&buf[0x05]);
+
+                    if (lba == pc1->lba) {
+                        memcpy(pc1->buffer + pc1->copied, buf, DSI_SIZE);
+                        lastPacket  = 1;
+                        valid       = 1;
+                    }
+                }
+                break;
+        }
+    }
+
+    if (!valid || lastPacket) {
+        pc1->copied = 0;
+        pc1->lba    = 0xFFFFFFFF;
+    }
+
+    if (lastPacket) {
+        *poutbuf      = pc1->buffer;
+        *poutbuf_size = sizeof(pc1->buffer);
+    } else {
+        *poutbuf      = NULL;
+        *poutbuf_size = 0;
+    }
+
+    return buf_size;
+}
+
+AVCodecParser ff_dvd_nav_parser = {
+    .codec_ids      = { AV_CODEC_ID_DVD_NAV },
+    .priv_data_size = sizeof(DVDNavParseContext),
+    .parser_init    = dvd_nav_parse_init,
+    .parser_parse   = dvd_nav_parse,
+};
diff --git a/libavcodec/dvdec.c b/libavcodec/dvdec.c
index 64d2259..48cbbfc 100644
--- a/libavcodec/dvdec.c
+++ b/libavcodec/dvdec.c
@@ -287,9 +287,9 @@
                   int x, y;
                   mb->idct_put(pixels, 8, block);
                   for (y = 0; y < (1 << log2_blocksize); y++, c_ptr += s->picture.linesize[j], pixels += 8) {
-                      ptr1   = pixels + (1 << (log2_blocksize - 1));
+                      ptr1   = pixels + ((1 << (log2_blocksize))>>1);
                       c_ptr1 = c_ptr + (s->picture.linesize[j] << log2_blocksize);
-                      for (x = 0; x < (1 << (log2_blocksize - 1)); x++) {
+                      for (x = 0; x < (1 << FFMAX(log2_blocksize - 1, 0)); x++) {
                           c_ptr[x]  = pixels[x];
                           c_ptr1[x] = ptr1[x];
                       }
@@ -319,7 +319,7 @@
     int buf_size = avpkt->size;
     DVVideoContext *s = avctx->priv_data;
     const uint8_t* vsc_pack;
-    int apt, is16_9;
+    int ret, apt, is16_9;
 
     s->sys = avpriv_dv_frame_profile2(avctx, s->sys, buf, buf_size);
     if (!s->sys || buf_size < s->sys->frame_size || ff_dv_init_dynamic_tables(s->sys)) {
@@ -327,20 +327,13 @@
         return -1; /* NOTE: we only accept several full frames */
     }
 
-    if (s->picture.data[0])
-        avctx->release_buffer(avctx, &s->picture);
-
-    avcodec_get_frame_defaults(&s->picture);
-    s->picture.reference = 0;
     s->picture.key_frame = 1;
     s->picture.pict_type = AV_PICTURE_TYPE_I;
     avctx->pix_fmt   = s->sys->pix_fmt;
     avctx->time_base = s->sys->time_base;
     avcodec_set_dimensions(avctx, s->sys->width, s->sys->height);
-    if (ff_get_buffer(avctx, &s->picture) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
-        return -1;
-    }
+    if ((ret = ff_get_buffer(avctx, &s->picture, 0)) < 0)
+        return ret;
     s->picture.interlaced_frame = 1;
     s->picture.top_field_first  = 0;
 
@@ -361,7 +354,7 @@
 
     /* return image */
     *got_frame = 1;
-    *(AVFrame*)data = s->picture;
+    av_frame_move_ref(data, &s->picture);
 
     return s->sys->frame_size;
 }
@@ -370,8 +363,7 @@
 {
     DVVideoContext *s = c->priv_data;
 
-    if (s->picture.data[0])
-        c->release_buffer(c, &s->picture);
+    av_frame_unref(&s->picture);
 
     return 0;
 }
diff --git a/libavcodec/dvdsubdec.c b/libavcodec/dvdsubdec.c
index a5c90cd..6cc40b7 100644
--- a/libavcodec/dvdsubdec.c
+++ b/libavcodec/dvdsubdec.c
@@ -24,6 +24,7 @@
 #include "libavutil/colorspace.h"
 #include "libavutil/opt.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/avstring.h"
 
 //#define DEBUG
 
@@ -39,7 +40,7 @@
 
 static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
 {
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
     uint8_t r, g, b;
     int i, y, cb, cr;
     int r_add, g_add, b_add;
@@ -523,7 +524,7 @@
     ctx->has_palette = 1;
     for(i=0;i<16;i++) {
         ctx->palette[i] = strtoul(p, &p, 16);
-        while(*p == ',' || isspace(*p))
+        while(*p == ',' || av_isspace(*p))
             p++;
     }
 }
diff --git a/libavcodec/dxa.c b/libavcodec/dxa.c
index a2fe557..2286f33 100644
--- a/libavcodec/dxa.c
+++ b/libavcodec/dxa.c
@@ -39,7 +39,7 @@
  * Decoder context
  */
 typedef struct DxaDecContext {
-    AVFrame pic, prev;
+    AVFrame prev;
 
     int dsize;
     uint8_t *decomp_buf;
@@ -49,12 +49,12 @@
 static const int shift1[6] = { 0, 8, 8, 8, 4, 4 };
 static const int shift2[6] = { 0, 0, 8, 4, 0, 4 };
 
-static int decode_13(AVCodecContext *avctx, DxaDecContext *c, uint8_t* dst, uint8_t *src, uint8_t *ref)
+static int decode_13(AVCodecContext *avctx, DxaDecContext *c, uint8_t* dst,
+                     int stride, uint8_t *src, uint8_t *ref)
 {
     uint8_t *code, *data, *mv, *msk, *tmp, *tmp2;
     int i, j, k;
     int type, x, y, d, d2;
-    int stride = c->pic.linesize[0];
     uint32_t mask;
 
     code = src  + 12;
@@ -192,6 +192,7 @@
 
 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
 {
+    AVFrame *frame = data;
     DxaDecContext * const c = avctx->priv_data;
     uint8_t *outptr, *srcptr, *tmpptr;
     unsigned long dsize;
@@ -211,17 +212,15 @@
         pc = 1;
     }
 
-    if ((ret = ff_get_buffer(avctx, &c->pic)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
         return ret;
-    }
-    memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
-    c->pic.palette_has_changed = pc;
+    memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
+    frame->palette_has_changed = pc;
 
-    outptr = c->pic.data[0];
+    outptr = frame->data[0];
     srcptr = c->decomp_buf;
     tmpptr = c->prev.data[0];
-    stride = c->pic.linesize[0];
+    stride = frame->linesize[0];
 
     if (bytestream2_get_le32(&gb) == MKTAG('N','U','L','L'))
         compr = -1;
@@ -239,22 +238,22 @@
     }
     switch(compr){
     case -1:
-        c->pic.key_frame = 0;
-        c->pic.pict_type = AV_PICTURE_TYPE_P;
+        frame->key_frame = 0;
+        frame->pict_type = AV_PICTURE_TYPE_P;
         if(c->prev.data[0])
-            memcpy(c->pic.data[0], c->prev.data[0], c->pic.linesize[0] * avctx->height);
+            memcpy(frame->data[0], c->prev.data[0], frame->linesize[0] * avctx->height);
         else{ // Should happen only when first frame is 'NULL'
-            memset(c->pic.data[0], 0, c->pic.linesize[0] * avctx->height);
-            c->pic.key_frame = 1;
-            c->pic.pict_type = AV_PICTURE_TYPE_I;
+            memset(frame->data[0], 0, frame->linesize[0] * avctx->height);
+            frame->key_frame = 1;
+            frame->pict_type = AV_PICTURE_TYPE_I;
         }
         break;
     case 2:
     case 3:
     case 4:
     case 5:
-        c->pic.key_frame = !(compr & 1);
-        c->pic.pict_type = (compr & 1) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
+        frame->key_frame = !(compr & 1);
+        frame->pict_type = (compr & 1) ? AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
         for(j = 0; j < avctx->height; j++){
             if((compr & 1) && tmpptr){
                 for(i = 0; i < avctx->width; i++)
@@ -268,25 +267,24 @@
         break;
     case 12: // ScummVM coding
     case 13:
-        c->pic.key_frame = 0;
-        c->pic.pict_type = AV_PICTURE_TYPE_P;
+        frame->key_frame = 0;
+        frame->pict_type = AV_PICTURE_TYPE_P;
         if (!c->prev.data[0]) {
             av_log(avctx, AV_LOG_ERROR, "Missing reference frame\n");
             return AVERROR_INVALIDDATA;
         }
-        decode_13(avctx, c, c->pic.data[0], srcptr, c->prev.data[0]);
+        decode_13(avctx, c, frame->data[0], frame->linesize[0], srcptr, c->prev.data[0]);
         break;
     default:
         av_log(avctx, AV_LOG_ERROR, "Unknown/unsupported compression type %d\n", compr);
         return AVERROR_INVALIDDATA;
     }
 
-    FFSWAP(AVFrame, c->pic, c->prev);
-    if(c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
+    av_frame_unref(&c->prev);
+    if ((ret = av_frame_ref(&c->prev, frame)) < 0)
+        return ret;
 
     *got_frame = 1;
-    *(AVFrame*)data = c->prev;
 
     /* always report that the buffer was completely consumed */
     return avpkt->size;
@@ -298,7 +296,6 @@
 
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
 
-    avcodec_get_frame_defaults(&c->pic);
     avcodec_get_frame_defaults(&c->prev);
 
     c->dsize = avctx->width * avctx->height * 2;
@@ -316,10 +313,7 @@
     DxaDecContext * const c = avctx->priv_data;
 
     av_freep(&c->decomp_buf);
-    if(c->prev.data[0])
-        avctx->release_buffer(avctx, &c->prev);
-    if(c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
+    av_frame_unref(&c->prev);
 
     return 0;
 }
diff --git a/libavcodec/dxtory.c b/libavcodec/dxtory.c
index 30983f8..28e66ba 100644
--- a/libavcodec/dxtory.c
+++ b/libavcodec/dxtory.c
@@ -28,9 +28,6 @@
 static av_cold int decode_init(AVCodecContext *avctx)
 {
     avctx->pix_fmt     = AV_PIX_FMT_YUV420P;
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame)
-        return AVERROR(ENOMEM);
 
     return 0;
 }
@@ -39,28 +36,24 @@
                         AVPacket *avpkt)
 {
     int h, w;
-    AVFrame *pic = avctx->coded_frame;
+    AVFrame *pic = data;
     const uint8_t *src = avpkt->data;
     uint8_t *Y1, *Y2, *U, *V;
     int ret;
 
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
-
     if (avpkt->size < avctx->width * avctx->height * 3 / 2 + 16) {
         av_log(avctx, AV_LOG_ERROR, "packet too small\n");
         return AVERROR_INVALIDDATA;
     }
 
-    pic->reference = 0;
-    if ((ret = ff_get_buffer(avctx, pic)) < 0)
+    if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
         return ret;
 
     pic->pict_type = AV_PICTURE_TYPE_I;
     pic->key_frame = 1;
 
     if (AV_RL32(src) != 0x01000002) {
-        av_log_ask_for_sample(avctx, "Unknown frame header %X\n", AV_RL32(src));
+        avpriv_request_sample(avctx, "Frame header %X", AV_RL32(src));
         return AVERROR_PATCHWELCOME;
     }
     src += 16;
@@ -84,28 +77,16 @@
     }
 
     *got_frame = 1;
-    *(AVFrame*)data = *pic;
 
     return avpkt->size;
 }
 
-static av_cold int decode_close(AVCodecContext *avctx)
-{
-    AVFrame *pic = avctx->coded_frame;
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
-    av_freep(&avctx->coded_frame);
-
-    return 0;
-}
-
 AVCodec ff_dxtory_decoder = {
     .name           = "dxtory",
     .long_name      = NULL_IF_CONFIG_SMALL("Dxtory"),
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_DXTORY,
     .init           = decode_init,
-    .close          = decode_close,
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_DR1,
 };
diff --git a/libavcodec/dxva2_h264.c b/libavcodec/dxva2_h264.c
index ce9e3ff..1090502 100644
--- a/libavcodec/dxva2_h264.c
+++ b/libavcodec/dxva2_h264.c
@@ -69,15 +69,15 @@
                                ff_dxva2_get_surface_index(ctx, r),
                                r->long_ref != 0);
 
-            if ((r->f.reference & PICT_TOP_FIELD) && r->field_poc[0] != INT_MAX)
+            if ((r->reference & PICT_TOP_FIELD) && r->field_poc[0] != INT_MAX)
                 pp->FieldOrderCntList[i][0] = r->field_poc[0];
-            if ((r->f.reference & PICT_BOTTOM_FIELD) && r->field_poc[1] != INT_MAX)
+            if ((r->reference & PICT_BOTTOM_FIELD) && r->field_poc[1] != INT_MAX)
                 pp->FieldOrderCntList[i][1] = r->field_poc[1];
 
             pp->FrameNumList[i] = r->long_ref ? r->pic_id : r->frame_num;
-            if (r->f.reference & PICT_TOP_FIELD)
+            if (r->reference & PICT_TOP_FIELD)
                 pp->UsedForReferenceFlags |= 1 << (2*i + 0);
-            if (r->f.reference & PICT_BOTTOM_FIELD)
+            if (r->reference & PICT_BOTTOM_FIELD)
                 pp->UsedForReferenceFlags |= 1 << (2*i + 1);
         } else {
             pp->RefFrameList[i].bPicEntry = 0xff;
@@ -108,7 +108,7 @@
                                         (h->pps.transform_8x8_mode            << 13) |
                                         ((h->sps.level_idc >= 31)             << 14) |
                                         /* IntraPicFlag (Modified if we detect a non
-                                         * intra slice in decode_slice) */
+                                         * intra slice in dxva2_h264_decode_slice) */
                                         (1                                    << 15);
 
     pp->bit_depth_luma_minus8         = h->sps.bit_depth_luma - 8;
@@ -206,7 +206,7 @@
     slice->SliceBytesInBuffer    = size;
     slice->wBadSliceChopping     = 0;
 
-    slice->first_mb_in_slice     = (h->mb_y >> FIELD_OR_MBAFF_PICTURE) * h->mb_width + h->mb_x;
+    slice->first_mb_in_slice     = (h->mb_y >> FIELD_OR_MBAFF_PICTURE(h)) * h->mb_width + h->mb_x;
     slice->NumMbsForSlice        = 0; /* XXX it is set once we have all slices */
     slice->BitOffsetToSliceData  = get_bits_count(&h->gb);
     slice->slice_type            = ff_h264_get_slice_type(h);
@@ -230,7 +230,7 @@
                 unsigned plane;
                 fill_picture_entry(&slice->RefPicList[list][i],
                                    ff_dxva2_get_surface_index(ctx, r),
-                                   r->f.reference == PICT_BOTTOM_FIELD);
+                                   r->reference == PICT_BOTTOM_FIELD);
                 for (plane = 0; plane < 3; plane++) {
                     int w, o;
                     if (plane == 0 && h->luma_weight_flag[list]) {
@@ -278,7 +278,7 @@
     const unsigned mb_count = h->mb_width * h->mb_height;
     struct dxva_context *ctx = avctx->hwaccel_context;
     const Picture *current_picture = h->cur_pic_ptr;
-    struct dxva2_picture_context *ctx_pic = current_picture->f.hwaccel_picture_private;
+    struct dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
     DXVA_Slice_H264_Short *slice = NULL;
     uint8_t  *dxva_data, *current, *end;
     unsigned dxva_size;
@@ -367,13 +367,13 @@
 }
 
 
-static int start_frame(AVCodecContext *avctx,
-                       av_unused const uint8_t *buffer,
-                       av_unused uint32_t size)
+static int dxva2_h264_start_frame(AVCodecContext *avctx,
+                                  av_unused const uint8_t *buffer,
+                                  av_unused uint32_t size)
 {
     const H264Context *h = avctx->priv_data;
     struct dxva_context *ctx = avctx->hwaccel_context;
-    struct dxva2_picture_context *ctx_pic = h->cur_pic_ptr->f.hwaccel_picture_private;
+    struct dxva2_picture_context *ctx_pic = h->cur_pic_ptr->hwaccel_picture_private;
 
     if (!ctx->decoder || !ctx->cfg || ctx->surface_count <= 0)
         return -1;
@@ -391,13 +391,14 @@
     return 0;
 }
 
-static int decode_slice(AVCodecContext *avctx,
-                        const uint8_t *buffer, uint32_t size)
+static int dxva2_h264_decode_slice(AVCodecContext *avctx,
+                                   const uint8_t *buffer,
+                                   uint32_t size)
 {
     const H264Context *h = avctx->priv_data;
     struct dxva_context *ctx = avctx->hwaccel_context;
     const Picture *current_picture = h->cur_pic_ptr;
-    struct dxva2_picture_context *ctx_pic = current_picture->f.hwaccel_picture_private;
+    struct dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
     unsigned position;
 
     if (ctx_pic->slice_count >= MAX_SLICES)
@@ -421,11 +422,11 @@
     return 0;
 }
 
-static int end_frame(AVCodecContext *avctx)
+static int dxva2_h264_end_frame(AVCodecContext *avctx)
 {
     H264Context *h = avctx->priv_data;
     struct dxva2_picture_context *ctx_pic =
-        h->cur_pic_ptr->f.hwaccel_picture_private;
+        h->cur_pic_ptr->hwaccel_picture_private;
     int ret;
 
     if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
@@ -444,8 +445,8 @@
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_H264,
     .pix_fmt        = AV_PIX_FMT_DXVA2_VLD,
-    .start_frame    = start_frame,
-    .decode_slice   = decode_slice,
-    .end_frame      = end_frame,
+    .start_frame    = dxva2_h264_start_frame,
+    .decode_slice   = dxva2_h264_decode_slice,
+    .end_frame      = dxva2_h264_end_frame,
     .priv_data_size = sizeof(struct dxva2_picture_context),
 };
diff --git a/libavcodec/dxva2_mpeg2.c b/libavcodec/dxva2_mpeg2.c
index 2c012a7..542f17c 100644
--- a/libavcodec/dxva2_mpeg2.c
+++ b/libavcodec/dxva2_mpeg2.c
@@ -151,7 +151,7 @@
     const struct MpegEncContext *s = avctx->priv_data;
     struct dxva_context *ctx = avctx->hwaccel_context;
     struct dxva2_picture_context *ctx_pic =
-        s->current_picture_ptr->f.hwaccel_picture_private;
+        s->current_picture_ptr->hwaccel_picture_private;
     const int is_field = s->picture_structure != PICT_FRAME;
     const unsigned mb_count = s->mb_width * (s->mb_height >> is_field);
     uint8_t  *dxva_data, *current, *end;
@@ -203,14 +203,14 @@
                                   mb_count);
 }
 
-static int start_frame(AVCodecContext *avctx,
-                       av_unused const uint8_t *buffer,
-                       av_unused uint32_t size)
+static int dxva2_mpeg2_start_frame(AVCodecContext *avctx,
+                                   av_unused const uint8_t *buffer,
+                                   av_unused uint32_t size)
 {
     const struct MpegEncContext *s = avctx->priv_data;
     struct dxva_context *ctx = avctx->hwaccel_context;
     struct dxva2_picture_context *ctx_pic =
-        s->current_picture_ptr->f.hwaccel_picture_private;
+        s->current_picture_ptr->hwaccel_picture_private;
 
     if (!ctx->decoder || !ctx->cfg || ctx->surface_count <= 0)
         return -1;
@@ -225,12 +225,12 @@
     return 0;
 }
 
-static int decode_slice(AVCodecContext *avctx,
-                        const uint8_t *buffer, uint32_t size)
+static int dxva2_mpeg2_decode_slice(AVCodecContext *avctx,
+                                    const uint8_t *buffer, uint32_t size)
 {
     const struct MpegEncContext *s = avctx->priv_data;
     struct dxva2_picture_context *ctx_pic =
-        s->current_picture_ptr->f.hwaccel_picture_private;
+        s->current_picture_ptr->hwaccel_picture_private;
     unsigned position;
 
     if (ctx_pic->slice_count >= MAX_SLICES)
@@ -246,11 +246,11 @@
     return 0;
 }
 
-static int end_frame(AVCodecContext *avctx)
+static int dxva2_mpeg2_end_frame(AVCodecContext *avctx)
 {
     struct MpegEncContext *s = avctx->priv_data;
     struct dxva2_picture_context *ctx_pic =
-        s->current_picture_ptr->f.hwaccel_picture_private;
+        s->current_picture_ptr->hwaccel_picture_private;
     int ret;
 
     if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
@@ -269,8 +269,8 @@
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_MPEG2VIDEO,
     .pix_fmt        = AV_PIX_FMT_DXVA2_VLD,
-    .start_frame    = start_frame,
-    .decode_slice   = decode_slice,
-    .end_frame      = end_frame,
+    .start_frame    = dxva2_mpeg2_start_frame,
+    .decode_slice   = dxva2_mpeg2_decode_slice,
+    .end_frame      = dxva2_mpeg2_end_frame,
     .priv_data_size = sizeof(struct dxva2_picture_context),
 };
diff --git a/libavcodec/dxva2_vc1.c b/libavcodec/dxva2_vc1.c
index cba685d..92c78fd 100644
--- a/libavcodec/dxva2_vc1.c
+++ b/libavcodec/dxva2_vc1.c
@@ -162,7 +162,7 @@
     const VC1Context *v = avctx->priv_data;
     struct dxva_context *ctx = avctx->hwaccel_context;
     const MpegEncContext *s = &v->s;
-    struct dxva2_picture_context *ctx_pic = s->current_picture_ptr->f.hwaccel_picture_private;
+    struct dxva2_picture_context *ctx_pic = s->current_picture_ptr->hwaccel_picture_private;
 
     DXVA_SliceInfo *slice = &ctx_pic->si;
 
@@ -211,13 +211,13 @@
                                   slice, sizeof(*slice), bs->NumMBsInBuffer);
 }
 
-static int start_frame(AVCodecContext *avctx,
-                       av_unused const uint8_t *buffer,
-                       av_unused uint32_t size)
+static int dxva2_vc1_start_frame(AVCodecContext *avctx,
+                                 av_unused const uint8_t *buffer,
+                                 av_unused uint32_t size)
 {
     const VC1Context *v = avctx->priv_data;
     struct dxva_context *ctx = avctx->hwaccel_context;
-    struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->f.hwaccel_picture_private;
+    struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
 
     if (!ctx->decoder || !ctx->cfg || ctx->surface_count <= 0)
         return -1;
@@ -230,12 +230,13 @@
     return 0;
 }
 
-static int decode_slice(AVCodecContext *avctx,
-                        const uint8_t *buffer, uint32_t size)
+static int dxva2_vc1_decode_slice(AVCodecContext *avctx,
+                                  const uint8_t *buffer,
+                                  uint32_t size)
 {
     const VC1Context *v = avctx->priv_data;
     const Picture *current_picture = v->s.current_picture_ptr;
-    struct dxva2_picture_context *ctx_pic = current_picture->f.hwaccel_picture_private;
+    struct dxva2_picture_context *ctx_pic = current_picture->hwaccel_picture_private;
 
     if (ctx_pic->bitstream_size > 0)
         return -1;
@@ -253,10 +254,10 @@
     return 0;
 }
 
-static int end_frame(AVCodecContext *avctx)
+static int dxva2_vc1_end_frame(AVCodecContext *avctx)
 {
     VC1Context *v = avctx->priv_data;
-    struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->f.hwaccel_picture_private;
+    struct dxva2_picture_context *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
     int ret;
 
     if (ctx_pic->bitstream_size <= 0)
@@ -277,9 +278,9 @@
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_WMV3,
     .pix_fmt        = AV_PIX_FMT_DXVA2_VLD,
-    .start_frame    = start_frame,
-    .decode_slice   = decode_slice,
-    .end_frame      = end_frame,
+    .start_frame    = dxva2_vc1_start_frame,
+    .decode_slice   = dxva2_vc1_decode_slice,
+    .end_frame      = dxva2_vc1_end_frame,
     .priv_data_size = sizeof(struct dxva2_picture_context),
 };
 #endif
@@ -289,8 +290,8 @@
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_VC1,
     .pix_fmt        = AV_PIX_FMT_DXVA2_VLD,
-    .start_frame    = start_frame,
-    .decode_slice   = decode_slice,
-    .end_frame      = end_frame,
+    .start_frame    = dxva2_vc1_start_frame,
+    .decode_slice   = dxva2_vc1_decode_slice,
+    .end_frame      = dxva2_vc1_end_frame,
     .priv_data_size = sizeof(struct dxva2_picture_context),
 };
diff --git a/libavcodec/eac3dec.c b/libavcodec/eac3dec.c
index 922bf7a..b55ee2a 100644
--- a/libavcodec/eac3dec.c
+++ b/libavcodec/eac3dec.c
@@ -300,7 +300,7 @@
        application can select from. each independent stream can also contain
        dependent streams which are used to add or replace channels. */
     if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
-        av_log_missing_feature(s->avctx, "Dependent substream decoding", 1);
+        avpriv_request_sample(s->avctx, "Dependent substream decoding");
         return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
     } else if (s->frame_type == EAC3_FRAME_TYPE_RESERVED) {
         av_log(s->avctx, AV_LOG_ERROR, "Reserved frame type\n");
@@ -312,7 +312,7 @@
        associated to an independent stream have matching substream id's. */
     if (s->substreamid) {
         /* only decode substream with id=0. skip any additional substreams. */
-        av_log_missing_feature(s->avctx, "Additional substreams", 1);
+        avpriv_request_sample(s->avctx, "Additional substreams");
         return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
     }
 
@@ -321,7 +321,7 @@
            rates in bit allocation.  The best assumption would be that it is
            handled like AC-3 DolbyNet, but we cannot be sure until we have a
            sample which utilizes this feature. */
-        av_log_missing_feature(s->avctx, "Reduced sampling rate", 1);
+        avpriv_request_sample(s->avctx, "Reduced sampling rate");
         return AVERROR_PATCHWELCOME;
     }
     skip_bits(gbc, 5); // skip bitstream id
@@ -593,7 +593,7 @@
            It is likely the offset of each block within the frame. */
         int block_start_bits = (s->num_blocks-1) * (4 + av_log2(s->frame_size-2));
         skip_bits_long(gbc, block_start_bits);
-        av_log_missing_feature(s->avctx, "Block start info", 1);
+        avpriv_request_sample(s->avctx, "Block start info");
     }
 
     /* syntax state initialization */
diff --git a/libavcodec/eacmv.c b/libavcodec/eacmv.c
index 33c6145..4e96c1a 100644
--- a/libavcodec/eacmv.c
+++ b/libavcodec/eacmv.c
@@ -36,31 +36,38 @@
 
 typedef struct CmvContext {
     AVCodecContext *avctx;
-    AVFrame frame;        ///< current
-    AVFrame last_frame;   ///< last
-    AVFrame last2_frame;  ///< second-last
+    AVFrame *last_frame;   ///< last
+    AVFrame *last2_frame;  ///< second-last
     int width, height;
     unsigned int palette[AVPALETTE_COUNT];
 } CmvContext;
 
 static av_cold int cmv_decode_init(AVCodecContext *avctx){
     CmvContext *s = avctx->priv_data;
-    avcodec_get_frame_defaults(&s->frame);
-    avcodec_get_frame_defaults(&s->last_frame);
-    avcodec_get_frame_defaults(&s->last2_frame);
 
     s->avctx = avctx;
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
+
+    s->last_frame  = av_frame_alloc();
+    s->last2_frame = av_frame_alloc();
+    if (!s->last_frame || !s->last2_frame) {
+        av_frame_free(&s->last_frame);
+        av_frame_free(&s->last2_frame);
+        return AVERROR(ENOMEM);
+    }
+
     return 0;
 }
 
-static void cmv_decode_intra(CmvContext * s, const uint8_t *buf, const uint8_t *buf_end){
-    unsigned char *dst = s->frame.data[0];
+static void cmv_decode_intra(CmvContext * s, AVFrame *frame,
+                             const uint8_t *buf, const uint8_t *buf_end)
+{
+    unsigned char *dst = frame->data[0];
     int i;
 
     for (i=0; i < s->avctx->height && buf_end - buf >= s->avctx->width; i++) {
         memcpy(dst, buf, s->avctx->width);
-        dst += s->frame.linesize[0];
+        dst += frame->linesize[0];
         buf += s->avctx->width;
     }
 }
@@ -84,7 +91,9 @@
     }
 }
 
-static void cmv_decode_inter(CmvContext * s, const uint8_t *buf, const uint8_t *buf_end){
+static void cmv_decode_inter(CmvContext *s, AVFrame *frame, const uint8_t *buf,
+                             const uint8_t *buf_end)
+{
     const uint8_t *raw = buf + (s->avctx->width*s->avctx->height/16);
     int x,y,i;
 
@@ -92,29 +101,29 @@
     for(y=0; y<s->avctx->height/4; y++)
     for(x=0; x<s->avctx->width/4 && buf_end - buf > i; x++) {
         if (buf[i]==0xFF) {
-            unsigned char *dst = s->frame.data[0] + (y*4)*s->frame.linesize[0] + x*4;
+            unsigned char *dst = frame->data[0] + (y*4)*frame->linesize[0] + x*4;
             if (raw+16<buf_end && *raw==0xFF) { /* intra */
                 raw++;
                 memcpy(dst, raw, 4);
-                memcpy(dst+s->frame.linesize[0], raw+4, 4);
-                memcpy(dst+2*s->frame.linesize[0], raw+8, 4);
-                memcpy(dst+3*s->frame.linesize[0], raw+12, 4);
+                memcpy(dst +     frame->linesize[0], raw+4, 4);
+                memcpy(dst + 2 * frame->linesize[0], raw+8, 4);
+                memcpy(dst + 3 * frame->linesize[0], raw+12, 4);
                 raw+=16;
             }else if(raw<buf_end) {  /* inter using second-last frame as reference */
                 int xoffset = (*raw & 0xF) - 7;
                 int yoffset = ((*raw >> 4)) - 7;
-                if (s->last2_frame.data[0])
-                    cmv_motcomp(s->frame.data[0], s->frame.linesize[0],
-                                s->last2_frame.data[0], s->last2_frame.linesize[0],
+                if (s->last2_frame->data[0])
+                    cmv_motcomp(frame->data[0], frame->linesize[0],
+                                s->last2_frame->data[0], s->last2_frame->linesize[0],
                                 x*4, y*4, xoffset, yoffset, s->avctx->width, s->avctx->height);
                 raw++;
             }
         }else{  /* inter using last frame as reference */
             int xoffset = (buf[i] & 0xF) - 7;
             int yoffset = ((buf[i] >> 4)) - 7;
-            if (s->last_frame.data[0])
-                cmv_motcomp(s->frame.data[0], s->frame.linesize[0],
-                          s->last_frame.data[0], s->last_frame.linesize[0],
+            if (s->last_frame->data[0])
+                cmv_motcomp(frame->data[0], frame->linesize[0],
+                          s->last_frame->data[0], s->last_frame->linesize[0],
                           x*4, y*4, xoffset, yoffset, s->avctx->width, s->avctx->height);
         }
         i++;
@@ -134,10 +143,8 @@
     s->height = AV_RL16(&buf[6]);
     if (s->avctx->width!=s->width || s->avctx->height!=s->height) {
         avcodec_set_dimensions(s->avctx, s->width, s->height);
-        if (s->frame.data[0])
-            s->avctx->release_buffer(s->avctx, &s->frame);
-        if (s->last_frame.data[0])
-            s->avctx->release_buffer(s->avctx, &s->last_frame);
+        av_frame_unref(s->last_frame);
+        av_frame_unref(s->last2_frame);
     }
 
     s->avctx->time_base.num = 1;
@@ -164,6 +171,8 @@
     int buf_size = avpkt->size;
     CmvContext *s = avctx->priv_data;
     const uint8_t *buf_end = buf + buf_size;
+    AVFrame *frame = data;
+    int ret;
 
     if (buf_end - buf < EA_PREAMBLE_SIZE)
         return AVERROR_INVALIDDATA;
@@ -179,48 +188,37 @@
     if (av_image_check_size(s->width, s->height, 0, s->avctx))
         return -1;
 
-    /* shuffle */
-    if (s->last2_frame.data[0])
-        avctx->release_buffer(avctx, &s->last2_frame);
-    FFSWAP(AVFrame, s->last_frame, s->last2_frame);
-    FFSWAP(AVFrame, s->frame, s->last_frame);
+    if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
+        return ret;
 
-    s->frame.reference = 3;
-    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
-                            FF_BUFFER_HINTS_READABLE |
-                            FF_BUFFER_HINTS_PRESERVE;
-    if (ff_get_buffer(avctx, &s->frame)<0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
-        return -1;
-    }
-
-    memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
+    memcpy(frame->data[1], s->palette, AVPALETTE_SIZE);
 
     buf += EA_PREAMBLE_SIZE;
     if ((buf[0]&1)) {  // subtype
-        cmv_decode_inter(s, buf+2, buf_end);
-        s->frame.key_frame = 0;
-        s->frame.pict_type = AV_PICTURE_TYPE_P;
+        cmv_decode_inter(s, frame, buf+2, buf_end);
+        frame->key_frame = 0;
+        frame->pict_type = AV_PICTURE_TYPE_P;
     }else{
-        s->frame.key_frame = 1;
-        s->frame.pict_type = AV_PICTURE_TYPE_I;
-        cmv_decode_intra(s, buf+2, buf_end);
+        frame->key_frame = 1;
+        frame->pict_type = AV_PICTURE_TYPE_I;
+        cmv_decode_intra(s, frame, buf+2, buf_end);
     }
 
+    av_frame_unref(s->last2_frame);
+    av_frame_move_ref(s->last2_frame, s->last_frame);
+    if ((ret = av_frame_ref(s->last_frame, frame)) < 0)
+        return ret;
+
     *got_frame = 1;
-    *(AVFrame*)data = s->frame;
 
     return buf_size;
 }
 
 static av_cold int cmv_decode_end(AVCodecContext *avctx){
     CmvContext *s = avctx->priv_data;
-    if (s->frame.data[0])
-        s->avctx->release_buffer(avctx, &s->frame);
-    if (s->last_frame.data[0])
-        s->avctx->release_buffer(avctx, &s->last_frame);
-    if (s->last2_frame.data[0])
-        s->avctx->release_buffer(avctx, &s->last2_frame);
+
+    av_frame_free(&s->last_frame);
+    av_frame_free(&s->last2_frame);
 
     return 0;
 }
diff --git a/libavcodec/eamad.c b/libavcodec/eamad.c
index b9679bc..58cdea6 100644
--- a/libavcodec/eamad.c
+++ b/libavcodec/eamad.c
@@ -45,7 +45,6 @@
 typedef struct MadContext {
     AVCodecContext *avctx;
     DSPContext dsp;
-    AVFrame frame;
     AVFrame last_frame;
     GetBitContext gb;
     void *bitstream_buf;
@@ -78,15 +77,16 @@
             dst[j*dst_stride + i] = av_clip_uint8(src[j*src_stride + i] + add);
 }
 
-static inline void comp_block(MadContext *t, int mb_x, int mb_y,
+static inline void comp_block(MadContext *t, AVFrame *frame,
+                              int mb_x, int mb_y,
                               int j, int mv_x, int mv_y, int add)
 {
     if (j < 4) {
         unsigned offset = (mb_y*16 + ((j&2)<<2) + mv_y)*t->last_frame.linesize[0] + mb_x*16 + ((j&1)<<3) + mv_x;
         if (offset >= (t->avctx->height - 7) * t->last_frame.linesize[0] - 7)
             return;
-        comp(t->frame.data[0] + (mb_y*16 + ((j&2)<<2))*t->frame.linesize[0] + mb_x*16 + ((j&1)<<3),
-             t->frame.linesize[0],
+        comp(frame->data[0] + (mb_y*16 + ((j&2)<<2))*frame->linesize[0] + mb_x*16 + ((j&1)<<3),
+             frame->linesize[0],
              t->last_frame.data[0] + offset,
              t->last_frame.linesize[0], add);
     } else if (!(t->avctx->flags & CODEC_FLAG_GRAY)) {
@@ -94,24 +94,25 @@
         unsigned offset = (mb_y * 8 + (mv_y/2))*t->last_frame.linesize[index] + mb_x * 8 + (mv_x/2);
         if (offset >= (t->avctx->height/2 - 7) * t->last_frame.linesize[index] - 7)
             return;
-        comp(t->frame.data[index] + (mb_y*8)*t->frame.linesize[index] + mb_x * 8,
-             t->frame.linesize[index],
+        comp(frame->data[index] + (mb_y*8)*frame->linesize[index] + mb_x * 8,
+             frame->linesize[index],
              t->last_frame.data[index] + offset,
              t->last_frame.linesize[index], add);
     }
 }
 
-static inline void idct_put(MadContext *t, int16_t *block, int mb_x, int mb_y, int j)
+static inline void idct_put(MadContext *t, AVFrame *frame, int16_t *block,
+                            int mb_x, int mb_y, int j)
 {
     if (j < 4) {
         ff_ea_idct_put_c(
-            t->frame.data[0] + (mb_y*16 + ((j&2)<<2))*t->frame.linesize[0] + mb_x*16 + ((j&1)<<3),
-            t->frame.linesize[0], block);
+            frame->data[0] + (mb_y*16 + ((j&2)<<2))*frame->linesize[0] + mb_x*16 + ((j&1)<<3),
+            frame->linesize[0], block);
     } else if (!(t->avctx->flags & CODEC_FLAG_GRAY)) {
         int index = j - 3;
         ff_ea_idct_put_c(
-            t->frame.data[index] + (mb_y*8)*t->frame.linesize[index] + mb_x*8,
-            t->frame.linesize[index], block);
+            frame->data[index] + (mb_y*8)*frame->linesize[index] + mb_x*8,
+            frame->linesize[index], block);
     }
 }
 
@@ -186,7 +187,7 @@
     return value;
 }
 
-static int decode_mb(MadContext *s, int inter)
+static int decode_mb(MadContext *s, AVFrame *frame, int inter)
 {
     int mv_map = 0;
     int mv_x, mv_y;
@@ -205,12 +206,12 @@
         if (mv_map & (1<<j)) {  // mv_x and mv_y are guarded by mv_map
             int add = 2*decode_motion(&s->gb);
             if (s->last_frame.data[0])
-                comp_block(s, s->mb_x, s->mb_y, j, mv_x, mv_y, add);
+                comp_block(s, frame, s->mb_x, s->mb_y, j, mv_x, mv_y, add);
         } else {
             s->dsp.clear_block(s->block);
             if(decode_block_intra(s, s->block) < 0)
                 return -1;
-            idct_put(s, s->block, s->mb_x, s->mb_y, j);
+            idct_put(s, frame, s->block, s->mb_x, s->mb_y, j);
         }
     }
     return 0;
@@ -233,9 +234,10 @@
     int buf_size       = avpkt->size;
     const uint8_t *buf_end = buf+buf_size;
     MadContext *s     = avctx->priv_data;
-    int width, height, ret;
+    AVFrame *frame    = data;
+    int width, height;
     int chunk_type;
-    int inter;
+    int inter, ret;
 
     if (buf_size < 26) {
         av_log(avctx, AV_LOG_ERROR, "Input buffer too small\n");
@@ -261,18 +263,23 @@
         if ((ret = av_image_check_size(width, height, 0, avctx)) < 0)
             return ret;
         avcodec_set_dimensions(avctx, width, height);
-        if (s->frame.data[0])
-            avctx->release_buffer(avctx, &s->frame);
-        if (s->last_frame.data[0])
-            avctx->release_buffer(avctx, &s->last_frame);
+        av_frame_unref(&s->last_frame);
     }
 
-    s->frame.reference = 3;
-    if (!s->frame.data[0]) {
-        if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
+        return ret;
+
+    if (inter && !s->last_frame.data[0]) {
+        av_log(avctx, AV_LOG_WARNING, "Missing reference frame.\n");
+        ret = ff_get_buffer(avctx, &s->last_frame, AV_GET_BUFFER_FLAG_REF);
+        if (ret < 0)
             return ret;
-        }
+        memset(s->last_frame.data[0], 0, s->last_frame.height *
+               s->last_frame.linesize[0]);
+        memset(s->last_frame.data[1], 0x80, s->last_frame.height / 2 *
+               s->last_frame.linesize[1]);
+        memset(s->last_frame.data[2], 0x80, s->last_frame.height / 2 *
+               s->last_frame.linesize[2]);
     }
 
     av_fast_padded_malloc(&s->bitstream_buf, &s->bitstream_buf_size,
@@ -285,14 +292,16 @@
 
     for (s->mb_y=0; s->mb_y < (avctx->height+15)/16; s->mb_y++)
         for (s->mb_x=0; s->mb_x < (avctx->width +15)/16; s->mb_x++)
-            if(decode_mb(s, inter) < 0)
+            if(decode_mb(s, frame, inter) < 0)
                 return AVERROR_INVALIDDATA;
 
     *got_frame = 1;
-    *(AVFrame*)data = s->frame;
 
-    if (chunk_type != MADe_TAG)
-        FFSWAP(AVFrame, s->frame, s->last_frame);
+    if (chunk_type != MADe_TAG) {
+        av_frame_unref(&s->last_frame);
+        if ((ret = av_frame_ref(&s->last_frame, frame)) < 0)
+            return ret;
+    }
 
     return buf_size;
 }
@@ -300,10 +309,7 @@
 static av_cold int decode_end(AVCodecContext *avctx)
 {
     MadContext *t = avctx->priv_data;
-    if (t->frame.data[0])
-        avctx->release_buffer(avctx, &t->frame);
-    if (t->last_frame.data[0])
-        avctx->release_buffer(avctx, &t->last_frame);
+    av_frame_unref(&t->last_frame);
     av_free(t->bitstream_buf);
     return 0;
 }
diff --git a/libavcodec/eatgq.c b/libavcodec/eatgq.c
index 524798b..cf93c06 100644
--- a/libavcodec/eatgq.c
+++ b/libavcodec/eatgq.c
@@ -39,7 +39,6 @@
 
 typedef struct TgqContext {
     AVCodecContext *avctx;
-    AVFrame frame;
     int width, height;
     ScanTable scantable;
     int qtable[64];
@@ -105,21 +104,21 @@
     block[0] += 128 << 4;
 }
 
-static void tgq_idct_put_mb(TgqContext *s, int16_t (*block)[64],
+static void tgq_idct_put_mb(TgqContext *s, int16_t (*block)[64], AVFrame *frame,
                             int mb_x, int mb_y)
 {
-    int linesize = s->frame.linesize[0];
-    uint8_t *dest_y  = s->frame.data[0] + (mb_y * 16 * linesize)             + mb_x * 16;
-    uint8_t *dest_cb = s->frame.data[1] + (mb_y * 8  * s->frame.linesize[1]) + mb_x * 8;
-    uint8_t *dest_cr = s->frame.data[2] + (mb_y * 8  * s->frame.linesize[2]) + mb_x * 8;
+    int linesize = frame->linesize[0];
+    uint8_t *dest_y  = frame->data[0] + (mb_y * 16 * linesize)           + mb_x * 16;
+    uint8_t *dest_cb = frame->data[1] + (mb_y * 8  * frame->linesize[1]) + mb_x * 8;
+    uint8_t *dest_cr = frame->data[2] + (mb_y * 8  * frame->linesize[2]) + mb_x * 8;
 
     ff_ea_idct_put_c(dest_y                   , linesize, block[0]);
     ff_ea_idct_put_c(dest_y                + 8, linesize, block[1]);
     ff_ea_idct_put_c(dest_y + 8 * linesize    , linesize, block[2]);
     ff_ea_idct_put_c(dest_y + 8 * linesize + 8, linesize, block[3]);
     if (!(s->avctx->flags & CODEC_FLAG_GRAY)) {
-         ff_ea_idct_put_c(dest_cb, s->frame.linesize[1], block[4]);
-         ff_ea_idct_put_c(dest_cr, s->frame.linesize[2], block[5]);
+         ff_ea_idct_put_c(dest_cb, frame->linesize[1], block[4]);
+         ff_ea_idct_put_c(dest_cr, frame->linesize[2], block[5]);
     }
 }
 
@@ -132,23 +131,24 @@
         memset(dst + j * dst_stride, level, 8);
 }
 
-static void tgq_idct_put_mb_dconly(TgqContext *s, int mb_x, int mb_y, const int8_t *dc)
+static void tgq_idct_put_mb_dconly(TgqContext *s, AVFrame *frame,
+                                   int mb_x, int mb_y, const int8_t *dc)
 {
-    int linesize = s->frame.linesize[0];
-    uint8_t *dest_y  = s->frame.data[0] + (mb_y * 16 * linesize)             + mb_x * 16;
-    uint8_t *dest_cb = s->frame.data[1] + (mb_y * 8  * s->frame.linesize[1]) + mb_x * 8;
-    uint8_t *dest_cr = s->frame.data[2] + (mb_y * 8  * s->frame.linesize[2]) + mb_x * 8;
+    int linesize = frame->linesize[0];
+    uint8_t *dest_y  = frame->data[0] + (mb_y * 16 * linesize)             + mb_x * 16;
+    uint8_t *dest_cb = frame->data[1] + (mb_y * 8  * frame->linesize[1]) + mb_x * 8;
+    uint8_t *dest_cr = frame->data[2] + (mb_y * 8  * frame->linesize[2]) + mb_x * 8;
     tgq_dconly(s, dest_y,                    linesize, dc[0]);
     tgq_dconly(s, dest_y                + 8, linesize, dc[1]);
     tgq_dconly(s, dest_y + 8 * linesize,     linesize, dc[2]);
     tgq_dconly(s, dest_y + 8 * linesize + 8, linesize, dc[3]);
     if (!(s->avctx->flags & CODEC_FLAG_GRAY)) {
-        tgq_dconly(s, dest_cb, s->frame.linesize[1], dc[4]);
-        tgq_dconly(s, dest_cr, s->frame.linesize[2], dc[5]);
+        tgq_dconly(s, dest_cb, frame->linesize[1], dc[4]);
+        tgq_dconly(s, dest_cr, frame->linesize[2], dc[5]);
     }
 }
 
-static int tgq_decode_mb(TgqContext *s, int mb_y, int mb_x)
+static int tgq_decode_mb(TgqContext *s, AVFrame *frame, int mb_y, int mb_x)
 {
     int mode;
     int i;
@@ -160,7 +160,7 @@
         init_get_bits(&gb, s->gb.buffer, FFMIN(bytestream2_get_bytes_left(&s->gb), mode) * 8);
         for (i = 0; i < 6; i++)
             tgq_decode_block(s, s->block[i], &gb);
-        tgq_idct_put_mb(s, s->block, mb_x, mb_y);
+        tgq_idct_put_mb(s, s->block, frame, mb_x, mb_y);
         bytestream2_skip(&s->gb, mode);
     } else {
         if (mode == 3) {
@@ -178,7 +178,7 @@
             av_log(s->avctx, AV_LOG_ERROR, "unsupported mb mode %i\n", mode);
             return -1;
         }
-        tgq_idct_put_mb_dconly(s, mb_x, mb_y, dc);
+        tgq_idct_put_mb_dconly(s, frame, mb_x, mb_y, dc);
     }
     return 0;
 }
@@ -201,6 +201,7 @@
     const uint8_t *buf = avpkt->data;
     int buf_size       = avpkt->size;
     TgqContext *s      = avctx->priv_data;
+    AVFrame *frame     = data;
     int x, y, ret;
     int big_endian;
 
@@ -220,48 +221,31 @@
 
     if (s->avctx->width!=s->width || s->avctx->height!=s->height) {
         avcodec_set_dimensions(s->avctx, s->width, s->height);
-        if (s->frame.data[0])
-            avctx->release_buffer(avctx, &s->frame);
     }
     tgq_calculate_qtable(s, bytestream2_get_byteu(&s->gb));
     bytestream2_skip(&s->gb, 3);
 
-    if (!s->frame.data[0]) {
-        s->frame.key_frame = 1;
-        s->frame.pict_type = AV_PICTURE_TYPE_I;
-        s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
-        if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
-            return ret;
-        }
-    }
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
+        return ret;
+    frame->key_frame = 1;
+    frame->pict_type = AV_PICTURE_TYPE_I;
 
     for (y = 0; y < FFALIGN(avctx->height, 16) >> 4; y++)
         for (x = 0; x < FFALIGN(avctx->width, 16) >> 4; x++)
-            if (tgq_decode_mb(s, y, x) < 0)
+            if (tgq_decode_mb(s, frame, y, x) < 0)
                 return AVERROR_INVALIDDATA;
 
     *got_frame = 1;
-    *(AVFrame*)data = s->frame;
 
     return avpkt->size;
 }
 
-static av_cold int tgq_decode_end(AVCodecContext *avctx)
-{
-    TgqContext *s = avctx->priv_data;
-    if (s->frame.data[0])
-        s->avctx->release_buffer(avctx, &s->frame);
-    return 0;
-}
-
 AVCodec ff_eatgq_decoder = {
     .name           = "eatgq",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_TGQ,
     .priv_data_size = sizeof(TgqContext),
     .init           = tgq_decode_init,
-    .close          = tgq_decode_end,
     .decode         = tgq_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("Electronic Arts TGQ video"),
diff --git a/libavcodec/eatgv.c b/libavcodec/eatgv.c
index 7092ba6..54074b3 100644
--- a/libavcodec/eatgv.c
+++ b/libavcodec/eatgv.c
@@ -31,6 +31,7 @@
 #include "avcodec.h"
 #define BITSTREAM_READER_LE
 #include "get_bits.h"
+#include "internal.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/mem.h"
 
@@ -39,8 +40,8 @@
 
 typedef struct TgvContext {
     AVCodecContext *avctx;
-    AVFrame frame;
     AVFrame last_frame;
+    uint8_t *frame_buffer;
     int width,height;
     uint32_t palette[AVPALETTE_COUNT];
 
@@ -56,7 +57,6 @@
     s->avctx         = avctx;
     avctx->time_base = (AVRational){1, 15};
     avctx->pix_fmt   = AV_PIX_FMT_PAL8;
-    avcodec_get_frame_defaults(&s->frame);
     avcodec_get_frame_defaults(&s->last_frame);
     return 0;
 }
@@ -140,8 +140,8 @@
  * Decode inter-frame
  * @return 0 on success, -1 on critical buffer underflow
  */
-static int tgv_decode_inter(TgvContext *s, const uint8_t *buf,
-                            const uint8_t *buf_end)
+static int tgv_decode_inter(TgvContext *s, AVFrame *frame,
+                            const uint8_t *buf, const uint8_t *buf_end)
 {
     int num_mvs;
     int num_blocks_raw;
@@ -241,22 +241,13 @@
 
             for (j = 0; j < 4; j++)
                 for (i = 0; i < 4; i++)
-                    s->frame.data[0][(y * 4 + j) * s->frame.linesize[0] + (x * 4 + i)] =
+                    frame->data[0][(y * 4 + j) * frame->linesize[0] + (x * 4 + i)] =
                         src[j * src_stride + i];
     }
 
     return 0;
 }
 
-/** release AVFrame buffers if allocated */
-static void cond_release_buffer(AVFrame *pic)
-{
-    if (pic->data[0]) {
-        av_freep(&pic->data[0]);
-        av_free(pic->data[1]);
-    }
-}
-
 static int tgv_decode_frame(AVCodecContext *avctx,
                             void *data, int *got_frame,
                             AVPacket *avpkt)
@@ -265,6 +256,7 @@
     int buf_size           = avpkt->size;
     TgvContext *s          = avctx->priv_data;
     const uint8_t *buf_end = buf + buf_size;
+    AVFrame *frame         = data;
     int chunk_type, ret;
 
     if (buf_end - buf < EA_PREAMBLE_SIZE)
@@ -284,8 +276,8 @@
         s->height = AV_RL16(&buf[2]);
         if (s->avctx->width != s->width || s->avctx->height != s->height) {
             avcodec_set_dimensions(s->avctx, s->width, s->height);
-            cond_release_buffer(&s->frame);
-            cond_release_buffer(&s->last_frame);
+            av_freep(&s->frame_buffer);
+            av_frame_unref(&s->last_frame);
         }
 
         pal_count = AV_RL16(&buf[6]);
@@ -299,46 +291,46 @@
     if ((ret = av_image_check_size(s->width, s->height, 0, avctx)) < 0)
         return ret;
 
-    /* shuffle */
-    FFSWAP(AVFrame, s->frame, s->last_frame);
-    if (!s->frame.data[0]) {
-        s->frame.reference = 3;
-        s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
-        s->frame.linesize[0] = s->width;
+    if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
+        return ret;
 
-        s->frame.data[0] = av_malloc(s->width * s->height);
-        if (!s->frame.data[0])
-            return AVERROR(ENOMEM);
-        s->frame.data[1] = av_malloc(AVPALETTE_SIZE);
-        if (!s->frame.data[1]) {
-            av_freep(&s->frame.data[0]);
-            return AVERROR(ENOMEM);
-        }
-    }
-    memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
+    memcpy(frame->data[1], s->palette, AVPALETTE_SIZE);
 
     if (chunk_type == kVGT_TAG) {
-        s->frame.key_frame = 1;
-        s->frame.pict_type = AV_PICTURE_TYPE_I;
-        if (unpack(buf, buf_end, s->frame.data[0], s->avctx->width, s->avctx->height) < 0) {
+        int y;
+        frame->key_frame = 1;
+        frame->pict_type = AV_PICTURE_TYPE_I;
+
+        if (!s->frame_buffer &&
+            !(s->frame_buffer = av_malloc(s->width * s->height)))
+            return AVERROR(ENOMEM);
+
+        if (unpack(buf, buf_end, s->frame_buffer, s->avctx->width, s->avctx->height) < 0) {
             av_log(avctx, AV_LOG_WARNING, "truncated intra frame\n");
             return AVERROR_INVALIDDATA;
         }
+        for (y = 0; y < s->height; y++)
+            memcpy(frame->data[0]  + y * frame->linesize[0],
+                   s->frame_buffer + y * s->width,
+                   s->width);
     } else {
         if (!s->last_frame.data[0]) {
             av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
             return buf_size;
         }
-        s->frame.key_frame = 0;
-        s->frame.pict_type = AV_PICTURE_TYPE_P;
-        if (tgv_decode_inter(s, buf, buf_end) < 0) {
+        frame->key_frame = 0;
+        frame->pict_type = AV_PICTURE_TYPE_P;
+        if (tgv_decode_inter(s, frame, buf, buf_end) < 0) {
             av_log(avctx, AV_LOG_WARNING, "truncated inter frame\n");
             return AVERROR_INVALIDDATA;
         }
     }
 
+    av_frame_unref(&s->last_frame);
+    if ((ret = av_frame_ref(&s->last_frame, frame)) < 0)
+        return ret;
+
     *got_frame = 1;
-    *(AVFrame*)data = s->frame;
 
     return buf_size;
 }
@@ -346,8 +338,8 @@
 static av_cold int tgv_decode_end(AVCodecContext *avctx)
 {
     TgvContext *s = avctx->priv_data;
-    cond_release_buffer(&s->frame);
-    cond_release_buffer(&s->last_frame);
+    av_frame_unref(&s->last_frame);
+    av_freep(&s->frame_buffer);
     av_free(s->mv_codebook);
     av_free(s->block_codebook);
     return 0;
@@ -362,4 +354,5 @@
     .close          = tgv_decode_end,
     .decode         = tgv_decode_frame,
     .long_name      = NULL_IF_CONFIG_SMALL("Electronic Arts TGV video"),
+    .capabilities   = CODEC_CAP_DR1,
 };
diff --git a/libavcodec/eatqi.c b/libavcodec/eatqi.c
index 5513848..1484ddc 100644
--- a/libavcodec/eatqi.c
+++ b/libavcodec/eatqi.c
@@ -36,7 +36,6 @@
 
 typedef struct TqiContext {
     MpegEncContext s;
-    AVFrame frame;
     void *bitstream_buf;
     unsigned int bitstream_buf_size;
     DECLARE_ALIGNED(16, int16_t, block)[6][64];
@@ -68,21 +67,21 @@
     return 0;
 }
 
-static inline void tqi_idct_put(TqiContext *t, int16_t (*block)[64])
+static inline void tqi_idct_put(TqiContext *t, AVFrame *frame, int16_t (*block)[64])
 {
     MpegEncContext *s = &t->s;
-    int linesize= t->frame.linesize[0];
-    uint8_t *dest_y  = t->frame.data[0] + (s->mb_y * 16* linesize            ) + s->mb_x * 16;
-    uint8_t *dest_cb = t->frame.data[1] + (s->mb_y * 8 * t->frame.linesize[1]) + s->mb_x * 8;
-    uint8_t *dest_cr = t->frame.data[2] + (s->mb_y * 8 * t->frame.linesize[2]) + s->mb_x * 8;
+    int linesize = frame->linesize[0];
+    uint8_t *dest_y  = frame->data[0] + (s->mb_y * 16* linesize            ) + s->mb_x * 16;
+    uint8_t *dest_cb = frame->data[1] + (s->mb_y * 8 * frame->linesize[1]) + s->mb_x * 8;
+    uint8_t *dest_cr = frame->data[2] + (s->mb_y * 8 * frame->linesize[2]) + s->mb_x * 8;
 
     ff_ea_idct_put_c(dest_y                 , linesize, block[0]);
     ff_ea_idct_put_c(dest_y              + 8, linesize, block[1]);
     ff_ea_idct_put_c(dest_y + 8*linesize    , linesize, block[2]);
     ff_ea_idct_put_c(dest_y + 8*linesize + 8, linesize, block[3]);
     if(!(s->avctx->flags&CODEC_FLAG_GRAY)) {
-        ff_ea_idct_put_c(dest_cb, t->frame.linesize[1], block[4]);
-        ff_ea_idct_put_c(dest_cr, t->frame.linesize[2], block[5]);
+        ff_ea_idct_put_c(dest_cb, frame->linesize[1], block[4]);
+        ff_ea_idct_put_c(dest_cr, frame->linesize[2], block[5]);
     }
 }
 
@@ -104,22 +103,19 @@
     const uint8_t *buf_end = buf+buf_size;
     TqiContext *t = avctx->priv_data;
     MpegEncContext *s = &t->s;
+    AVFrame *frame = data;
+    int ret;
 
     s->width  = AV_RL16(&buf[0]);
     s->height = AV_RL16(&buf[2]);
     tqi_calculate_qtable(s, buf[4]);
     buf += 8;
 
-    if (t->frame.data[0])
-        avctx->release_buffer(avctx, &t->frame);
-
     if (s->avctx->width!=s->width || s->avctx->height!=s->height)
         avcodec_set_dimensions(s->avctx, s->width, s->height);
 
-    if(ff_get_buffer(avctx, &t->frame) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
-        return -1;
-    }
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
+        return ret;
 
     av_fast_padded_malloc(&t->bitstream_buf, &t->bitstream_buf_size,
                           buf_end - buf);
@@ -134,20 +130,17 @@
     {
         if (tqi_decode_mb(s, t->block) < 0)
             goto end;
-        tqi_idct_put(t, t->block);
+        tqi_idct_put(t, frame, t->block);
     }
     end:
 
     *got_frame = 1;
-    *(AVFrame*)data = t->frame;
     return buf_size;
 }
 
 static av_cold int tqi_decode_end(AVCodecContext *avctx)
 {
     TqiContext *t = avctx->priv_data;
-    if(t->frame.data[0])
-        avctx->release_buffer(avctx, &t->frame);
     av_free(t->bitstream_buf);
     return 0;
 }
diff --git a/libavcodec/error_resilience.c b/libavcodec/error_resilience.c
index d5fbcbd..c9f5e1f 100644
--- a/libavcodec/error_resilience.c
+++ b/libavcodec/error_resilience.c
@@ -147,7 +147,7 @@
         for(b_x=0; b_x<w; b_x++){
             int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
             int error_j= s->error_status_table[mb_index_j];
-            int intra_j = IS_INTRA(s->cur_pic->f.mb_type[mb_index_j]);
+            int intra_j = IS_INTRA(s->cur_pic->mb_type[mb_index_j]);
             if(intra_j==0 || !(error_j&ER_DC_ERROR)){
                 color= dc[b_x + b_y*stride];
                 distance= b_x;
@@ -160,7 +160,7 @@
         for(b_x=w-1; b_x>=0; b_x--){
             int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
             int error_j= s->error_status_table[mb_index_j];
-            int intra_j = IS_INTRA(s->cur_pic->f.mb_type[mb_index_j]);
+            int intra_j = IS_INTRA(s->cur_pic->mb_type[mb_index_j]);
             if(intra_j==0 || !(error_j&ER_DC_ERROR)){
                 color= dc[b_x + b_y*stride];
                 distance= b_x;
@@ -175,7 +175,7 @@
         for(b_y=0; b_y<h; b_y++){
             int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
             int error_j= s->error_status_table[mb_index_j];
-            int intra_j = IS_INTRA(s->cur_pic->f.mb_type[mb_index_j]);
+            int intra_j = IS_INTRA(s->cur_pic->mb_type[mb_index_j]);
             if(intra_j==0 || !(error_j&ER_DC_ERROR)){
                 color= dc[b_x + b_y*stride];
                 distance= b_y;
@@ -188,7 +188,7 @@
         for(b_y=h-1; b_y>=0; b_y--){
             int mb_index_j= (b_x>>is_luma) + (b_y>>is_luma)*s->mb_stride;
             int error_j= s->error_status_table[mb_index_j];
-            int intra_j = IS_INTRA(s->cur_pic->f.mb_type[mb_index_j]);
+            int intra_j = IS_INTRA(s->cur_pic->mb_type[mb_index_j]);
             if(intra_j==0 || !(error_j&ER_DC_ERROR)){
                 color= dc[b_x + b_y*stride];
                 distance= b_y;
@@ -205,7 +205,7 @@
             mb_index = (b_x >> is_luma) + (b_y >> is_luma) * s->mb_stride;
             error    = s->error_status_table[mb_index];
 
-            if (IS_INTER(s->cur_pic->f.mb_type[mb_index]))
+            if (IS_INTER(s->cur_pic->mb_type[mb_index]))
                 continue; // inter
             if (!(error & ER_DC_ERROR))
                 continue; // dc-ok
@@ -236,7 +236,7 @@
                            int h, int stride, int is_luma)
 {
     int b_x, b_y, mvx_stride, mvy_stride;
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
     set_mv_strides(s, &mvx_stride, &mvy_stride);
     mvx_stride >>= is_luma;
     mvy_stride *= mvx_stride;
@@ -246,13 +246,13 @@
             int y;
             int left_status  = s->error_status_table[( b_x      >> is_luma) + (b_y >> is_luma) * s->mb_stride];
             int right_status = s->error_status_table[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride];
-            int left_intra   = IS_INTRA(s->cur_pic->f.mb_type[( b_x      >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
-            int right_intra  = IS_INTRA(s->cur_pic->f.mb_type[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
+            int left_intra   = IS_INTRA(s->cur_pic->mb_type[( b_x      >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
+            int right_intra  = IS_INTRA(s->cur_pic->mb_type[((b_x + 1) >> is_luma) + (b_y >> is_luma) * s->mb_stride]);
             int left_damage  = left_status & ER_MB_ERROR;
             int right_damage = right_status & ER_MB_ERROR;
             int offset       = b_x * 8 + b_y * stride * 8;
-            int16_t *left_mv  = s->cur_pic->f.motion_val[0][mvy_stride * b_y + mvx_stride *  b_x];
-            int16_t *right_mv = s->cur_pic->f.motion_val[0][mvy_stride * b_y + mvx_stride * (b_x + 1)];
+            int16_t *left_mv  = s->cur_pic->motion_val[0][mvy_stride * b_y + mvx_stride *  b_x];
+            int16_t *right_mv = s->cur_pic->motion_val[0][mvy_stride * b_y + mvx_stride * (b_x + 1)];
             if (!(left_damage || right_damage))
                 continue; // both undamaged
             if ((!left_intra) && (!right_intra) &&
@@ -304,7 +304,7 @@
                            int stride, int is_luma)
 {
     int b_x, b_y, mvx_stride, mvy_stride;
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
     set_mv_strides(s, &mvx_stride, &mvy_stride);
     mvx_stride >>= is_luma;
     mvy_stride *= mvx_stride;
@@ -314,14 +314,14 @@
             int x;
             int top_status    = s->error_status_table[(b_x >> is_luma) +  (b_y      >> is_luma) * s->mb_stride];
             int bottom_status = s->error_status_table[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride];
-            int top_intra     = IS_INTRA(s->cur_pic->f.mb_type[(b_x >> is_luma) + ( b_y      >> is_luma) * s->mb_stride]);
-            int bottom_intra  = IS_INTRA(s->cur_pic->f.mb_type[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride]);
+            int top_intra     = IS_INTRA(s->cur_pic->mb_type[(b_x >> is_luma) + ( b_y      >> is_luma) * s->mb_stride]);
+            int bottom_intra  = IS_INTRA(s->cur_pic->mb_type[(b_x >> is_luma) + ((b_y + 1) >> is_luma) * s->mb_stride]);
             int top_damage    = top_status & ER_MB_ERROR;
             int bottom_damage = bottom_status & ER_MB_ERROR;
             int offset        = b_x * 8 + b_y * stride * 8;
 
-            int16_t *top_mv    = s->cur_pic->f.motion_val[0][mvy_stride *  b_y      + mvx_stride * b_x];
-            int16_t *bottom_mv = s->cur_pic->f.motion_val[0][mvy_stride * (b_y + 1) + mvx_stride * b_x];
+            int16_t *top_mv    = s->cur_pic->motion_val[0][mvy_stride *  b_y      + mvx_stride * b_x];
+            int16_t *bottom_mv = s->cur_pic->motion_val[0][mvy_stride * (b_y + 1) + mvx_stride * b_x];
 
             if (!(top_damage || bottom_damage))
                 continue; // both undamaged
@@ -386,7 +386,7 @@
         int f = 0;
         int error = s->error_status_table[mb_xy];
 
-        if (IS_INTRA(s->cur_pic->f.mb_type[mb_xy]))
+        if (IS_INTRA(s->cur_pic->mb_type[mb_xy]))
             f = MV_FROZEN; // intra // FIXME check
         if (!(error & ER_MV_ERROR))
             f = MV_FROZEN; // inter with undamaged MV
@@ -394,13 +394,13 @@
         fixed[mb_xy] = f;
         if (f == MV_FROZEN)
             num_avail++;
-        else if(s->last_pic->f.data[0] && s->last_pic->f.motion_val[0]){
+        else if(s->last_pic->f.data[0] && s->last_pic->motion_val[0]){
             const int mb_y= mb_xy / s->mb_stride;
             const int mb_x= mb_xy % s->mb_stride;
             const int mot_index= (mb_x + mb_y*mot_stride) * mot_step;
-            s->cur_pic->f.motion_val[0][mot_index][0]= s->last_pic->f.motion_val[0][mot_index][0];
-            s->cur_pic->f.motion_val[0][mot_index][1]= s->last_pic->f.motion_val[0][mot_index][1];
-            s->cur_pic->f.ref_index[0][4*mb_xy]      = s->last_pic->f.ref_index[0][4*mb_xy];
+            s->cur_pic->motion_val[0][mot_index][0]= s->last_pic->motion_val[0][mot_index][0];
+            s->cur_pic->motion_val[0][mot_index][1]= s->last_pic->motion_val[0][mot_index][1];
+            s->cur_pic->ref_index[0][4*mb_xy]      = s->last_pic->ref_index[0][4*mb_xy];
         }
     }
 
@@ -411,7 +411,7 @@
                 const int mb_xy = mb_x + mb_y * s->mb_stride;
                 int mv_dir = (s->last_pic && s->last_pic->f.data[0]) ? MV_DIR_FORWARD : MV_DIR_BACKWARD;
 
-                if (IS_INTRA(s->cur_pic->f.mb_type[mb_xy]))
+                if (IS_INTRA(s->cur_pic->mb_type[mb_xy]))
                     continue;
                 if (!(s->error_status_table[mb_xy] & ER_MV_ERROR))
                     continue;
@@ -452,7 +452,7 @@
 
                     if (fixed[mb_xy] == MV_FROZEN)
                         continue;
-                    av_assert1(!IS_INTRA(s->cur_pic->f.mb_type[mb_xy]));
+                    av_assert1(!IS_INTRA(s->cur_pic->mb_type[mb_xy]));
                     av_assert1(s->last_pic && s->last_pic->f.data[0]);
 
                     j = 0;
@@ -483,38 +483,38 @@
 
                     if (mb_x > 0 && fixed[mb_xy - 1]) {
                         mv_predictor[pred_count][0] =
-                            s->cur_pic->f.motion_val[0][mot_index - mot_step][0];
+                            s->cur_pic->motion_val[0][mot_index - mot_step][0];
                         mv_predictor[pred_count][1] =
-                            s->cur_pic->f.motion_val[0][mot_index - mot_step][1];
+                            s->cur_pic->motion_val[0][mot_index - mot_step][1];
                         ref[pred_count] =
-                            s->cur_pic->f.ref_index[0][4 * (mb_xy - 1)];
+                            s->cur_pic->ref_index[0][4 * (mb_xy - 1)];
                         pred_count++;
                     }
                     if (mb_x + 1 < mb_width && fixed[mb_xy + 1]) {
                         mv_predictor[pred_count][0] =
-                            s->cur_pic->f.motion_val[0][mot_index + mot_step][0];
+                            s->cur_pic->motion_val[0][mot_index + mot_step][0];
                         mv_predictor[pred_count][1] =
-                            s->cur_pic->f.motion_val[0][mot_index + mot_step][1];
+                            s->cur_pic->motion_val[0][mot_index + mot_step][1];
                         ref[pred_count] =
-                            s->cur_pic->f.ref_index[0][4 * (mb_xy + 1)];
+                            s->cur_pic->ref_index[0][4 * (mb_xy + 1)];
                         pred_count++;
                     }
                     if (mb_y > 0 && fixed[mb_xy - mb_stride]) {
                         mv_predictor[pred_count][0] =
-                            s->cur_pic->f.motion_val[0][mot_index - mot_stride * mot_step][0];
+                            s->cur_pic->motion_val[0][mot_index - mot_stride * mot_step][0];
                         mv_predictor[pred_count][1] =
-                            s->cur_pic->f.motion_val[0][mot_index - mot_stride * mot_step][1];
+                            s->cur_pic->motion_val[0][mot_index - mot_stride * mot_step][1];
                         ref[pred_count] =
-                            s->cur_pic->f.ref_index[0][4 * (mb_xy - s->mb_stride)];
+                            s->cur_pic->ref_index[0][4 * (mb_xy - s->mb_stride)];
                         pred_count++;
                     }
                     if (mb_y + 1<mb_height && fixed[mb_xy + mb_stride]) {
                         mv_predictor[pred_count][0] =
-                            s->cur_pic->f.motion_val[0][mot_index + mot_stride * mot_step][0];
+                            s->cur_pic->motion_val[0][mot_index + mot_stride * mot_step][0];
                         mv_predictor[pred_count][1] =
-                            s->cur_pic->f.motion_val[0][mot_index + mot_stride * mot_step][1];
+                            s->cur_pic->motion_val[0][mot_index + mot_stride * mot_step][1];
                         ref[pred_count] =
-                            s->cur_pic->f.ref_index[0][4 * (mb_xy + s->mb_stride)];
+                            s->cur_pic->ref_index[0][4 * (mb_xy + s->mb_stride)];
                         pred_count++;
                     }
                     if (pred_count == 0)
@@ -572,19 +572,19 @@
                         if (s->avctx->codec_id == AV_CODEC_ID_H264) {
                             // FIXME
                         } else {
-                            ff_thread_await_progress(&s->last_pic->f,
+                            ff_thread_await_progress(&s->last_pic->tf,
                                                      mb_y, 0);
                         }
-                        if (!s->last_pic->f.motion_val[0] ||
-                            !s->last_pic->f.ref_index[0])
+                        if (!s->last_pic->motion_val[0] ||
+                            !s->last_pic->ref_index[0])
                             goto skip_last_mv;
-                        prev_x   = s->last_pic->f.motion_val[0][mot_index][0];
-                        prev_y   = s->last_pic->f.motion_val[0][mot_index][1];
-                        prev_ref = s->last_pic->f.ref_index[0][4 * mb_xy];
+                        prev_x   = s->last_pic->motion_val[0][mot_index][0];
+                        prev_y   = s->last_pic->motion_val[0][mot_index][1];
+                        prev_ref = s->last_pic->ref_index[0][4 * mb_xy];
                     } else {
-                        prev_x   = s->cur_pic->f.motion_val[0][mot_index][0];
-                        prev_y   = s->cur_pic->f.motion_val[0][mot_index][1];
-                        prev_ref = s->cur_pic->f.ref_index[0][4 * mb_xy];
+                        prev_x   = s->cur_pic->motion_val[0][mot_index][0];
+                        prev_y   = s->cur_pic->motion_val[0][mot_index][1];
+                        prev_ref = s->cur_pic->ref_index[0][4 * mb_xy];
                     }
 
                     /* last MV */
@@ -601,9 +601,9 @@
                         uint8_t *src = s->cur_pic->f.data[0] +
                                        mb_x * 16 + mb_y * 16 * linesize[0];
 
-                        s->cur_pic->f.motion_val[0][mot_index][0] =
+                        s->cur_pic->motion_val[0][mot_index][0] =
                             s->mv[0][0][0] = mv_predictor[j][0];
-                        s->cur_pic->f.motion_val[0][mot_index][1] =
+                        s->cur_pic->motion_val[0][mot_index][1] =
                             s->mv[0][0][1] = mv_predictor[j][1];
 
                         // predictor intra or otherwise not available
@@ -648,8 +648,8 @@
 
                     for (i = 0; i < mot_step; i++)
                         for (j = 0; j < mot_step; j++) {
-                            s->cur_pic->f.motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];
-                            s->cur_pic->f.motion_val[0][mot_index + i + j * mot_stride][1] = s->mv[0][0][1];
+                            s->cur_pic->motion_val[0][mot_index + i + j * mot_stride][0] = s->mv[0][0][0];
+                            s->cur_pic->motion_val[0][mot_index + i + j * mot_stride][1] = s->mv[0][0][1];
                         }
 
                     s->decode_mb(s->opaque, ref[best_pred], MV_DIR_FORWARD,
@@ -731,7 +731,7 @@
                 if (s->avctx->codec_id == AV_CODEC_ID_H264) {
                     // FIXME
                 } else {
-                    ff_thread_await_progress(&s->last_pic->f, mb_y, 0);
+                    ff_thread_await_progress(&s->last_pic->tf, mb_y, 0);
                 }
                 is_intra_likely += s->dsp->sad[0](NULL, last_mb_ptr, mb_ptr,
                                                  linesize[0], 16);
@@ -740,7 +740,7 @@
                                                  last_mb_ptr + linesize[0] * 16,
                                                  linesize[0], 16);
             } else {
-                if (IS_INTRA(s->cur_pic->f.mb_type[mb_xy]))
+                if (IS_INTRA(s->cur_pic->mb_type[mb_xy]))
                    is_intra_likely++;
                 else
                    is_intra_likely--;
@@ -827,13 +827,15 @@
 
     s->error_status_table[start_xy] |= VP_START;
 
-    if (start_xy > 0 && s->avctx->thread_count <= 1 &&
+    if (start_xy > 0 && !(s->avctx->active_thread_type & FF_THREAD_SLICE) &&
         s->avctx->skip_top * s->mb_width < start_i) {
         int prev_status = s->error_status_table[s->mb_index2xy[start_i - 1]];
 
         prev_status &= ~ VP_START;
-        if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
+        if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END)) {
+            s->error_occurred = 1;
             s->error_count = INT_MAX;
+        }
     }
 }
 
@@ -875,15 +877,26 @@
         }
     }
 
-    if (s->cur_pic->f.motion_val[0] == NULL) {
+    if (s->cur_pic->motion_val[0] == NULL) {
         av_log(s->avctx, AV_LOG_ERROR, "Warning MVs not available\n");
 
         for (i = 0; i < 2; i++) {
-            s->cur_pic->f.ref_index[i]     = av_mallocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));
-            s->cur_pic->motion_val_base[i] = av_mallocz((size + 4) * 2 * sizeof(uint16_t));
-            s->cur_pic->f.motion_val[i]    = s->cur_pic->motion_val_base[i] + 4;
+            s->cur_pic->ref_index_buf[i]  = av_buffer_allocz(s->mb_stride * s->mb_height * 4 * sizeof(uint8_t));
+            s->cur_pic->motion_val_buf[i] = av_buffer_allocz((size + 4) * 2 * sizeof(uint16_t));
+            if (!s->cur_pic->ref_index_buf[i] || !s->cur_pic->motion_val_buf[i])
+                break;
+            s->cur_pic->ref_index[i]  = s->cur_pic->ref_index_buf[i]->data;
+            s->cur_pic->motion_val[i] = (int16_t (*)[2])s->cur_pic->motion_val_buf[i]->data + 4;
         }
-        s->cur_pic->f.motion_subsample_log2 = 3;
+        if (i < 2) {
+            for (i = 0; i < 2; i++) {
+                av_buffer_unref(&s->cur_pic->ref_index_buf[i]);
+                av_buffer_unref(&s->cur_pic->motion_val_buf[i]);
+                s->cur_pic->ref_index[i]  = NULL;
+                s->cur_pic->motion_val[i] = NULL;
+            }
+            return;
+        }
     }
 
     if (s->avctx->debug & FF_DEBUG_ER) {
@@ -1046,9 +1059,9 @@
             continue;
 
         if (is_intra_likely)
-            s->cur_pic->f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
+            s->cur_pic->mb_type[mb_xy] = MB_TYPE_INTRA4x4;
         else
-            s->cur_pic->f.mb_type[mb_xy] = MB_TYPE_16x16 | MB_TYPE_L0;
+            s->cur_pic->mb_type[mb_xy] = MB_TYPE_16x16 | MB_TYPE_L0;
     }
 
     // change inter to intra blocks if no reference frames are available
@@ -1056,15 +1069,15 @@
         !(s->next_pic && s->next_pic->f.data[0]))
         for (i = 0; i < s->mb_num; i++) {
             const int mb_xy = s->mb_index2xy[i];
-            if (!IS_INTRA(s->cur_pic->f.mb_type[mb_xy]))
-                s->cur_pic->f.mb_type[mb_xy] = MB_TYPE_INTRA4x4;
+            if (!IS_INTRA(s->cur_pic->mb_type[mb_xy]))
+                s->cur_pic->mb_type[mb_xy] = MB_TYPE_INTRA4x4;
         }
 
     /* handle inter blocks with damaged AC */
     for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
         for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
             const int mb_xy   = mb_x + mb_y * s->mb_stride;
-            const int mb_type = s->cur_pic->f.mb_type[mb_xy];
+            const int mb_type = s->cur_pic->mb_type[mb_xy];
             const int dir     = !(s->last_pic && s->last_pic->f.data[0]);
             const int mv_dir  = dir ? MV_DIR_BACKWARD : MV_DIR_FORWARD;
             int mv_type;
@@ -1083,13 +1096,13 @@
                 int j;
                 mv_type = MV_TYPE_8X8;
                 for (j = 0; j < 4; j++) {
-                    s->mv[0][j][0] = s->cur_pic->f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];
-                    s->mv[0][j][1] = s->cur_pic->f.motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];
+                    s->mv[0][j][0] = s->cur_pic->motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][0];
+                    s->mv[0][j][1] = s->cur_pic->motion_val[dir][mb_index + (j & 1) + (j >> 1) * s->b8_stride][1];
                 }
             } else {
                 mv_type     = MV_TYPE_16X16;
-                s->mv[0][0][0] = s->cur_pic->f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];
-                s->mv[0][0][1] = s->cur_pic->f.motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][1];
+                s->mv[0][0][0] = s->cur_pic->motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][0];
+                s->mv[0][0][1] = s->cur_pic->motion_val[dir][mb_x * 2 + mb_y * 2 * s->b8_stride][1];
             }
 
             s->decode_mb(s->opaque, 0 /* FIXME h264 partitioned slices need this set */,
@@ -1103,7 +1116,7 @@
             for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
                 int       xy      = mb_x * 2 + mb_y * 2 * s->b8_stride;
                 const int mb_xy   = mb_x + mb_y * s->mb_stride;
-                const int mb_type = s->cur_pic->f.mb_type[mb_xy];
+                const int mb_type = s->cur_pic->mb_type[mb_xy];
                 int mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
 
                 error = s->error_status_table[mb_xy];
@@ -1125,12 +1138,12 @@
                     int time_pb = s->pb_time;
 
                     av_assert0(s->avctx->codec_id != AV_CODEC_ID_H264);
-                    ff_thread_await_progress(&s->next_pic->f, mb_y, 0);
+                    ff_thread_await_progress(&s->next_pic->tf, mb_y, 0);
 
-                    s->mv[0][0][0] = s->next_pic->f.motion_val[0][xy][0] *  time_pb            / time_pp;
-                    s->mv[0][0][1] = s->next_pic->f.motion_val[0][xy][1] *  time_pb            / time_pp;
-                    s->mv[1][0][0] = s->next_pic->f.motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;
-                    s->mv[1][0][1] = s->next_pic->f.motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;
+                    s->mv[0][0][0] = s->next_pic->motion_val[0][xy][0] *  time_pb            / time_pp;
+                    s->mv[0][0][1] = s->next_pic->motion_val[0][xy][1] *  time_pb            / time_pp;
+                    s->mv[1][0][0] = s->next_pic->motion_val[0][xy][0] * (time_pb - time_pp) / time_pp;
+                    s->mv[1][0][1] = s->next_pic->motion_val[0][xy][1] * (time_pb - time_pp) / time_pp;
                 } else {
                     s->mv[0][0][0] = 0;
                     s->mv[0][0][1] = 0;
@@ -1155,7 +1168,7 @@
             int16_t *dc_ptr;
             uint8_t *dest_y, *dest_cb, *dest_cr;
             const int mb_xy   = mb_x + mb_y * s->mb_stride;
-            const int mb_type = s->cur_pic->f.mb_type[mb_xy];
+            const int mb_type = s->cur_pic->mb_type[mb_xy];
 
             error = s->error_status_table[mb_xy];
 
@@ -1208,7 +1221,7 @@
         for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
             uint8_t *dest_y, *dest_cb, *dest_cr;
             const int mb_xy   = mb_x + mb_y * s->mb_stride;
-            const int mb_type = s->cur_pic->f.mb_type[mb_xy];
+            const int mb_type = s->cur_pic->mb_type[mb_xy];
 
             error = s->error_status_table[mb_xy];
 
diff --git a/libavcodec/escape124.c b/libavcodec/escape124.c
index 1865c38..3c83e58 100644
--- a/libavcodec/escape124.c
+++ b/libavcodec/escape124.c
@@ -79,8 +79,7 @@
     for (i = 0; i < 3; i++)
         av_free(s->codebooks[i].blocks);
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+    av_frame_unref(&s->frame);
 
     return 0;
 }
@@ -204,6 +203,7 @@
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     Escape124Context *s = avctx->priv_data;
+    AVFrame *frame = data;
 
     GetBitContext gb;
     unsigned frame_flags, frame_size;
@@ -216,8 +216,7 @@
     uint16_t* old_frame_data, *new_frame_data;
     unsigned old_stride, new_stride;
 
-    AVFrame new_frame;
-    avcodec_get_frame_defaults(&new_frame);
+    int ret;
 
     init_get_bits(&gb, buf, buf_size * 8);
 
@@ -232,10 +231,14 @@
     // Leave last frame unchanged
     // FIXME: Is this necessary?  I haven't seen it in any real samples
     if (!(frame_flags & 0x114) || !(frame_flags & 0x7800000)) {
+        if (!s->frame.data[0])
+            return AVERROR_INVALIDDATA;
+
         av_log(NULL, AV_LOG_DEBUG, "Skipping frame\n");
 
         *got_frame = 1;
-        *(AVFrame*)data = s->frame;
+        if ((ret = av_frame_ref(frame, &s->frame)) < 0)
+            return ret;
 
         return frame_size;
     }
@@ -268,14 +271,11 @@
         }
     }
 
-    new_frame.reference = 3;
-    if (ff_get_buffer(avctx, &new_frame)) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
-        return -1;
-    }
+    if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
+        return ret;
 
-    new_frame_data = (uint16_t*)new_frame.data[0];
-    new_stride = new_frame.linesize[0] / 2;
+    new_frame_data = (uint16_t*)frame->data[0];
+    new_stride = frame->linesize[0] / 2;
     old_frame_data = (uint16_t*)s->frame.data[0];
     old_stride = s->frame.linesize[0] / 2;
 
@@ -356,10 +356,10 @@
            "Escape sizes: %i, %i, %i\n",
            frame_size, buf_size, get_bits_count(&gb) / 8);
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+    av_frame_unref(&s->frame);
+    if ((ret = av_frame_ref(&s->frame, frame)) < 0)
+        return ret;
 
-    *(AVFrame*)data = s->frame = new_frame;
     *got_frame = 1;
 
     return frame_size;
diff --git a/libavcodec/escape130.c b/libavcodec/escape130.c
index ca6a253..d24af79 100644
--- a/libavcodec/escape130.c
+++ b/libavcodec/escape130.c
@@ -40,6 +40,7 @@
 {
     Escape130Context *s = avctx->priv_data;
     avctx->pix_fmt = AV_PIX_FMT_YUV420P;
+    avcodec_get_frame_defaults(&s->frame);
 
     if((avctx->width&1) || (avctx->height&1)){
         av_log(avctx, AV_LOG_ERROR, "Dimensions are not a multiple of the block size\n");
@@ -55,8 +56,7 @@
 {
     Escape130Context *s = avctx->priv_data;
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+    av_frame_unref(&s->frame);
 
     av_freep(&s->bases);
 
@@ -108,6 +108,7 @@
 
     GetBitContext gb;
     unsigned i;
+    int ret;
 
     uint8_t *old_y, *old_cb, *old_cr,
             *new_y, *new_cb, *new_cr;
@@ -120,7 +121,7 @@
     unsigned y_base = 0;
     uint8_t *yb= s->bases;
 
-    AVFrame new_frame = { { 0 } };
+    AVFrame *frame = data;
 
     init_get_bits(&gb, buf, buf_size * 8);
 
@@ -130,18 +131,15 @@
     // Header; no useful information in here
     skip_bits_long(&gb, 128);
 
-    new_frame.reference = 3;
-    if (ff_get_buffer(avctx, &new_frame)) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
-        return -1;
-    }
+    if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
+        return ret;
 
-    new_y = new_frame.data[0];
-    new_cb = new_frame.data[1];
-    new_cr = new_frame.data[2];
-    new_y_stride = new_frame.linesize[0];
-    new_cb_stride = new_frame.linesize[1];
-    new_cr_stride = new_frame.linesize[2];
+    new_y = frame->data[0];
+    new_cb = frame->data[1];
+    new_cr = frame->data[2];
+    new_y_stride = frame->linesize[0];
+    new_cb_stride = frame->linesize[1];
+    new_cr_stride = frame->linesize[2];
     old_y = s->frame.data[0];
     old_cb = s->frame.data[1];
     old_cr = s->frame.data[2];
@@ -298,10 +296,10 @@
            "Escape sizes: %i, %i\n",
            buf_size, get_bits_count(&gb) / 8);
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+    av_frame_unref(&s->frame);
+    if ((ret = av_frame_ref(&s->frame, frame)) < 0)
+        return ret;
 
-    *(AVFrame*)data = s->frame = new_frame;
     *got_frame = 1;
 
     return buf_size;
diff --git a/libavcodec/evrcdec.c b/libavcodec/evrcdec.c
index 5569ca2..f0e594f 100644
--- a/libavcodec/evrcdec.c
+++ b/libavcodec/evrcdec.c
@@ -640,7 +640,7 @@
     /* Short term postfilter */
     synthesis_filter(temp, wcoef2, e->postfilter_iir, length, out);
 
-    memcpy(e->postfilter_residual,
+    memmove(e->postfilter_residual,
            e->postfilter_residual + length, ACB_SIZE * sizeof(float));
 }
 
@@ -714,7 +714,7 @@
                 e->pitch[ACB_SIZE + j] = e->energy_vector[i];
         }
 
-        memcpy(e->pitch, e->pitch + subframe_size, ACB_SIZE * sizeof(float));
+        memmove(e->pitch, e->pitch + subframe_size, ACB_SIZE * sizeof(float));
 
         if (e->bitrate != RATE_QUANT && e->avg_acb_gain < 0.4) {
             f = 0.1 * e->avg_fcb_gain;
@@ -746,7 +746,7 @@
     int   i, j, ret, error_flag = 0;
 
     frame->nb_samples = 160;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0)
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
     samples = (float *)frame->data[0];
 
@@ -814,7 +814,7 @@
 
                 interpolate_delay(idelay, delay, e->prev_pitch_delay, i);
                 acb_excitation(e, e->pitch + ACB_SIZE, e->avg_acb_gain, idelay, subframe_size);
-                memcpy(e->pitch, e->pitch + subframe_size, ACB_SIZE * sizeof(float));
+                memmove(e->pitch, e->pitch + subframe_size, ACB_SIZE * sizeof(float));
             }
         }
 
@@ -872,7 +872,7 @@
                 e->pitch[ACB_SIZE + j] = e->energy_vector[i];
         }
 
-        memcpy(e->pitch, e->pitch + subframe_size, ACB_SIZE * sizeof(float));
+        memmove(e->pitch, e->pitch + subframe_size, ACB_SIZE * sizeof(float));
 
         synthesis_filter(e->pitch + ACB_SIZE, ilpc,
                          e->synthesis, subframe_size, tmp);
diff --git a/libavcodec/exr.c b/libavcodec/exr.c
index 1432817..af1bee0 100644
--- a/libavcodec/exr.c
+++ b/libavcodec/exr.c
@@ -70,7 +70,7 @@
 } EXRThreadData;
 
 typedef struct EXRContext {
-    AVFrame picture;
+    AVFrame *picture;
     int compr;
     enum ExrPixelType pixel_type;
     int channel_offsets[4]; // 0 = red, 1 = green, 2 = blue and 3 = alpha
@@ -336,7 +336,7 @@
                         int jobnr, int threadnr)
 {
     EXRContext *s = avctx->priv_data;
-    AVFrame *const p = &s->picture;
+    AVFrame *const p = s->picture;
     EXRThreadData *td = &s->thread_data[threadnr];
     const uint8_t *channel_buffer[4] = { 0 };
     const uint8_t *buf = s->buf;
@@ -348,7 +348,8 @@
     const uint8_t *src;
     int axmax = (avctx->width - (s->xmax + 1)) * 2 * s->desc->nb_components;
     int bxmin = s->xmin * 2 * s->desc->nb_components;
-    int ret, i, x, buf_size = s->buf_size;
+    int i, x, buf_size = s->buf_size;
+    int av_unused ret;
 
     line_offset = AV_RL64(s->table + jobnr * 8);
     // Check if the buffer has the required bytes needed from the offset
@@ -458,8 +459,8 @@
     const uint8_t *buf_end  = buf + buf_size;
 
     EXRContext *const s = avctx->priv_data;
+    ThreadFrame frame = { .f = data };
     AVFrame *picture  = data;
-    AVFrame *const p = &s->picture;
     uint8_t *ptr;
 
     int i, y, magic_number, version, flags, ret;
@@ -696,7 +697,7 @@
             avctx->pix_fmt = AV_PIX_FMT_RGB48;
         break;
     case EXR_UINT:
-        av_log_missing_feature(avctx, "32-bit unsigned int", 1);
+        avpriv_request_sample(avctx, "32-bit unsigned int");
         return AVERROR_PATCHWELCOME;
     default:
         av_log(avctx, AV_LOG_ERROR, "Missing channel list\n");
@@ -718,8 +719,6 @@
         return AVERROR_PATCHWELCOME;
     }
 
-    if (s->picture.data[0])
-        ff_thread_release_buffer(avctx, &s->picture);
     if (av_image_check_size(w, h, 0, avctx))
         return AVERROR_INVALIDDATA;
 
@@ -756,54 +755,39 @@
         memset(s->thread_data + prev_size, 0, s->thread_data_size - prev_size);
     }
 
-    if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
         return ret;
-    }
 
     if (buf_end - buf < scan_line_blocks * 8)
         return AVERROR_INVALIDDATA;
     s->table = buf;
-    ptr = p->data[0];
+    ptr = picture->data[0];
 
     // Zero out the start if ymin is not 0
     for (y = 0; y < s->ymin; y++) {
         memset(ptr, 0, out_line_size);
-        ptr += p->linesize[0];
+        ptr += picture->linesize[0];
     }
 
+    s->picture = picture;
     avctx->execute2(avctx, decode_block, s->thread_data, NULL, scan_line_blocks);
 
     // Zero out the end if ymax+1 is not h
     for (y = s->ymax + 1; y < avctx->height; y++) {
         memset(ptr, 0, out_line_size);
-        ptr += p->linesize[0];
+        ptr += picture->linesize[0];
     }
 
-    *picture   = s->picture;
     *got_frame = 1;
 
     return buf_size;
 }
 
-static av_cold int decode_init(AVCodecContext *avctx)
-{
-    EXRContext *s = avctx->priv_data;
-
-    avcodec_get_frame_defaults(&s->picture);
-    avctx->coded_frame = &s->picture;
-
-    return 0;
-}
-
 static av_cold int decode_end(AVCodecContext *avctx)
 {
     EXRContext *s = avctx->priv_data;
     int i;
 
-    if (s->picture.data[0])
-        avctx->release_buffer(avctx, &s->picture);
-
     for (i = 0; i < s->thread_data_size / sizeof(EXRThreadData); i++) {
         EXRThreadData *td = &s->thread_data[i];
         av_free(td->uncompressed_data);
@@ -822,7 +806,6 @@
     .type               = AVMEDIA_TYPE_VIDEO,
     .id                 = AV_CODEC_ID_EXR,
     .priv_data_size     = sizeof(EXRContext),
-    .init               = decode_init,
     .close              = decode_end,
     .decode             = decode_frame,
     .capabilities       = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS,
diff --git a/libavcodec/ffv1.c b/libavcodec/ffv1.c
index c75f2aa..404b0e3 100644
--- a/libavcodec/ffv1.c
+++ b/libavcodec/ffv1.c
@@ -191,10 +191,7 @@
     FFV1Context *s = avctx->priv_data;
     int i, j;
 
-    if (avctx->codec->decode && s->picture.data[0])
-        avctx->release_buffer(avctx, &s->picture);
-    if (avctx->codec->decode && s->last_picture.data[0])
-        avctx->release_buffer(avctx, &s->last_picture);
+    av_frame_unref(&s->last_picture);
 
     for (j = 0; j < s->slice_count; j++) {
         FFV1Context *fs = s->slice_context[j];
diff --git a/libavcodec/ffv1.h b/libavcodec/ffv1.h
index 02c3afa..23633c9 100644
--- a/libavcodec/ffv1.h
+++ b/libavcodec/ffv1.h
@@ -89,8 +89,9 @@
     int transparency;
     int flags;
     int picture_number;
-    AVFrame picture;
-    AVFrame last_picture;
+    AVFrame picture, last_picture;
+
+    AVFrame *cur;
     int plane_count;
     int ac;                              ///< 1=range coder <-> 0=golomb rice
     int ac_byte_count;                   ///< number of bytes used for AC coding
diff --git a/libavcodec/ffv1dec.c b/libavcodec/ffv1dec.c
index cb72203..21afa1d 100644
--- a/libavcodec/ffv1dec.c
+++ b/libavcodec/ffv1dec.c
@@ -306,16 +306,16 @@
 
     ps = get_symbol(c, state, 0);
     if (ps == 1) {
-        f->picture.interlaced_frame = 1;
-        f->picture.top_field_first  = 1;
+        f->cur->interlaced_frame = 1;
+        f->cur->top_field_first  = 1;
     } else if (ps == 2) {
-        f->picture.interlaced_frame = 1;
-        f->picture.top_field_first  = 0;
+        f->cur->interlaced_frame = 1;
+        f->cur->top_field_first  = 0;
     } else if (ps == 3) {
-        f->picture.interlaced_frame = 0;
+        f->cur->interlaced_frame = 0;
     }
-    f->picture.sample_aspect_ratio.num = get_symbol(c, state, 0);
-    f->picture.sample_aspect_ratio.den = get_symbol(c, state, 0);
+    f->cur->sample_aspect_ratio.num = get_symbol(c, state, 0);
+    f->cur->sample_aspect_ratio.den = get_symbol(c, state, 0);
 
     return 0;
 }
@@ -326,7 +326,7 @@
     FFV1Context *f    = fs->avctx->priv_data;
     int width, height, x, y, ret;
     const int ps      = av_pix_fmt_desc_get(c->pix_fmt)->comp[0].step_minus1 + 1;
-    AVFrame * const p = &f->picture;
+    AVFrame * const p = f->cur;
 
     if (f->version > 2) {
         if (ffv1_init_slice_state(f, fs) < 0)
@@ -338,7 +338,7 @@
     }
     if ((ret = ffv1_init_slice_state(f, fs)) < 0)
         return ret;
-    if (f->picture.key_frame)
+    if (f->cur->key_frame)
         ffv1_clear_slice_state(f, fs);
 
     width  = fs->slice_width;
@@ -732,16 +732,12 @@
     int buf_size        = avpkt->size;
     FFV1Context *f      = avctx->priv_data;
     RangeCoder *const c = &f->slice_context[0]->c;
-    AVFrame *const p    = &f->picture;
     int i, ret;
     uint8_t keystate = 128;
     const uint8_t *buf_p;
+    AVFrame *const p    = data;
 
-    AVFrame *picture = data;
-
-    /* release previously stored data */
-    if (p->data[0])
-        avctx->release_buffer(avctx, p);
+    f->cur = p;
 
     ff_init_range_decoder(c, buf, buf_size);
     ff_build_rac_states(c, 0.05 * (1LL << 32), 256 - 8);
@@ -762,11 +758,8 @@
         p->key_frame = 0;
     }
 
-    p->reference = 3; //for error concealment
-    if ((ret = ff_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF)) < 0)
         return ret;
-    }
 
     if (avctx->debug & FF_DEBUG_PICT_INFO)
         av_log(avctx, AV_LOG_DEBUG, "ver:%d keyframe:%d coder:%d ec:%d slices:%d bps:%d\n",
@@ -806,6 +799,8 @@
             ff_init_range_decoder(&fs->c, buf_p, v);
         } else
             fs->c.bytestream_end = (uint8_t *)(buf_p + v);
+
+        fs->cur = p;
     }
 
     avctx->execute(avctx,
@@ -824,14 +819,12 @@
             for (j = 0; j < 4; j++) {
                 int sh = (j==1 || j==2) ? f->chroma_h_shift : 0;
                 int sv = (j==1 || j==2) ? f->chroma_v_shift : 0;
-                dst[j] = f->picture     .data[j] + f->picture     .linesize[j]*
+                dst[j] = p->data[j] + p->linesize[j]*
                          (fs->slice_y>>sv) + (fs->slice_x>>sh);
                 src[j] = f->last_picture.data[j] + f->last_picture.linesize[j]*
                          (fs->slice_y>>sv) + (fs->slice_x>>sh);
             }
-            av_image_copy(dst,
-                          f->picture.linesize,
-                          (const uint8_t **)src,
+            av_image_copy(dst, p->linesize, (const uint8_t **)src,
                           f->last_picture.linesize,
                           avctx->pix_fmt,
                           fs->slice_width,
@@ -841,10 +834,12 @@
 
     f->picture_number++;
 
-    *picture   = *p;
-    *got_frame = 1;
+    av_frame_unref(&f->last_picture);
+    if ((ret = av_frame_ref(&f->last_picture, p)) < 0)
+        return ret;
+    f->cur = NULL;
 
-    FFSWAP(AVFrame, f->picture, f->last_picture);
+    *got_frame = 1;
 
     return buf_size;
 }
diff --git a/libavcodec/ffwavesynth.c b/libavcodec/ffwavesynth.c
index eb3b455..4f392f2 100644
--- a/libavcodec/ffwavesynth.c
+++ b/libavcodec/ffwavesynth.c
@@ -444,7 +444,7 @@
     if (duration <= 0)
         return AVERROR(EINVAL);
     ws->frame.nb_samples = duration;
-    r = ff_get_buffer(avc, &ws->frame);
+    r = ff_get_buffer(avc, &ws->frame, 0);
     if (r < 0)
         return r;
     pcm = (int16_t *)ws->frame.data[0];
diff --git a/libavcodec/flacdec.c b/libavcodec/flacdec.c
index f264d20..1bf6dc7 100644
--- a/libavcodec/flacdec.c
+++ b/libavcodec/flacdec.c
@@ -101,7 +101,7 @@
         return 0;
 
     if (!avpriv_flac_is_extradata_valid(avctx, &format, &streaminfo))
-        return -1;
+        return AVERROR_INVALIDDATA;
 
     /* initialize based on the demuxer-supplied streamdata header */
     avpriv_flac_parse_streaminfo(avctx, (FLACStreaminfo *)s, streaminfo);
@@ -212,7 +212,7 @@
     if (method_type > 1) {
         av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n",
                method_type);
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
     rice_order = get_bits(&s->gb, 4);
@@ -221,7 +221,7 @@
     if (pred_order > samples) {
         av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n",
                pred_order, samples);
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
     rice_bits = 4 + method_type;
@@ -251,14 +251,15 @@
 {
     const int blocksize = s->blocksize;
     int av_uninit(a), av_uninit(b), av_uninit(c), av_uninit(d), i;
+    int ret;
 
     /* warm up samples */
     for (i = 0; i < pred_order; i++) {
         decoded[i] = get_sbits_long(&s->gb, bps);
     }
 
-    if (decode_residuals(s, decoded, pred_order) < 0)
-        return -1;
+    if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
+        return ret;
 
     if (pred_order > 0)
         a = decoded[pred_order-1];
@@ -290,7 +291,7 @@
         break;
     default:
         av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
     return 0;
@@ -299,7 +300,7 @@
 static int decode_subframe_lpc(FLACContext *s, int32_t *decoded, int pred_order,
                                int bps)
 {
-    int i;
+    int i, ret;
     int coeff_prec, qlevel;
     int coeffs[32];
 
@@ -311,21 +312,21 @@
     coeff_prec = get_bits(&s->gb, 4) + 1;
     if (coeff_prec == 16) {
         av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
     qlevel = get_sbits(&s->gb, 5);
     if (qlevel < 0) {
         av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",
                qlevel);
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
     for (i = 0; i < pred_order; i++) {
         coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec);
     }
 
-    if (decode_residuals(s, decoded, pred_order) < 0)
-        return -1;
+    if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
+        return ret;
 
     s->dsp.lpc(decoded, coeffs, pred_order, qlevel, s->blocksize);
 
@@ -337,7 +338,7 @@
     int32_t *decoded = s->decoded[channel];
     int type, wasted = 0;
     int bps = s->bps;
-    int i, tmp;
+    int i, tmp, ret;
 
     if (channel == 0) {
         if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE)
@@ -349,7 +350,7 @@
 
     if (get_bits1(&s->gb)) {
         av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
     type = get_bits(&s->gb, 6);
 
@@ -369,7 +370,7 @@
         bps -= wasted;
     }
     if (bps > 32) {
-        av_log_missing_feature(s->avctx, "Decorrelated bit depth > 32", 0);
+        avpriv_report_missing_feature(s->avctx, "Decorrelated bit depth > 32");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -382,14 +383,14 @@
         for (i = 0; i < s->blocksize; i++)
             decoded[i] = get_sbits_long(&s->gb, bps);
     } else if ((type >= 8) && (type <= 12)) {
-        if (decode_subframe_fixed(s, decoded, type & ~0x8, bps) < 0)
-            return -1;
+        if ((ret = decode_subframe_fixed(s, decoded, type & ~0x8, bps)) < 0)
+            return ret;
     } else if (type >= 32) {
-        if (decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps) < 0)
-            return -1;
+        if ((ret = decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps)) < 0)
+            return ret;
     } else {
         av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
     if (wasted) {
@@ -409,7 +410,7 @@
 
     if (ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) {
         av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
     if (s->channels && fi.channels != s->channels && s->got_streaminfo) {
@@ -426,7 +427,7 @@
 
     if (!s->bps && !fi.bps) {
         av_log(s->avctx, AV_LOG_ERROR, "bps not found in STREAMINFO or frame header\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
     if (!fi.bps) {
         fi.bps = s->bps;
@@ -446,14 +447,14 @@
     if (fi.blocksize > s->max_blocksize) {
         av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", fi.blocksize,
                s->max_blocksize);
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
     s->blocksize = fi.blocksize;
 
     if (!s->samplerate && !fi.samplerate) {
         av_log(s->avctx, AV_LOG_ERROR, "sample rate not found in STREAMINFO"
                                         " or frame header\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
     if (fi.samplerate == 0)
         fi.samplerate = s->samplerate;
@@ -472,8 +473,8 @@
 
     /* subframes */
     for (i = 0; i < s->channels; i++) {
-        if (decode_subframe(s, i) < 0)
-            return -1;
+        if ((ret = decode_subframe(s, i)) < 0)
+            return ret;
     }
 
     align_get_bits(gb);
@@ -522,32 +523,38 @@
     if (AV_RB32(buf) == MKBETAG('f','L','a','C')) {
         if (!s->got_streaminfo && parse_streaminfo(s, buf, buf_size)) {
             av_log(s->avctx, AV_LOG_ERROR, "invalid header\n");
-            return -1;
+            return AVERROR_INVALIDDATA;
         }
         return get_metadata_size(buf, buf_size);
     }
 
     /* decode frame */
     init_get_bits(&s->gb, buf, buf_size*8);
-    if (decode_frame(s) < 0) {
+    if ((ret = decode_frame(s)) < 0) {
         av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
-        return -1;
+        return ret;
     }
-    bytes_read = (get_bits_count(&s->gb)+7)/8;
+    bytes_read = get_bits_count(&s->gb)/8;
+
+    if ((s->avctx->err_recognition & AV_EF_CRCCHECK) &&
+        av_crc(av_crc_get_table(AV_CRC_16_ANSI),
+               0, buf, bytes_read)) {
+        av_log(s->avctx, AV_LOG_ERROR, "CRC error at PTS %"PRId64"\n", avpkt->pts);
+        if (s->avctx->err_recognition & AV_EF_EXPLODE)
+            return AVERROR_INVALIDDATA;
+    }
 
     /* get output buffer */
     frame->nb_samples = s->blocksize;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     s->dsp.decorrelate[s->ch_mode](frame->data, s->decoded, s->channels,
                                    s->blocksize, s->sample_shift);
 
     if (bytes_read > buf_size) {
         av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size);
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
     if (bytes_read < buf_size) {
         av_log(s->avctx, AV_LOG_DEBUG, "underread: %d orig size: %d\n",
diff --git a/libavcodec/flacenc.c b/libavcodec/flacenc.c
index bbdeaad2..dc932c6 100644
--- a/libavcodec/flacenc.c
+++ b/libavcodec/flacenc.c
@@ -399,12 +399,6 @@
     s->frame_count   = 0;
     s->min_framesize = s->max_framesize;
 
-#if FF_API_OLD_ENCODE_AUDIO
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame)
-        return AVERROR(ENOMEM);
-#endif
-
     if (channels == 3 &&
             avctx->channel_layout != (AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER) ||
         channels == 4 &&
@@ -1276,7 +1270,7 @@
         }
     }
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, frame_bytes)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, frame_bytes)) < 0)
         return ret;
 
     out_bytes = write_frame(s, avpkt);
@@ -1310,9 +1304,6 @@
     }
     av_freep(&avctx->extradata);
     avctx->extradata_size = 0;
-#if FF_API_OLD_ENCODE_AUDIO
-    av_freep(&avctx->coded_frame);
-#endif
     return 0;
 }
 
diff --git a/libavcodec/flashsv.c b/libavcodec/flashsv.c
index 21464ed..9982b5e 100644
--- a/libavcodec/flashsv.c
+++ b/libavcodec/flashsv.c
@@ -41,6 +41,7 @@
 #include "avcodec.h"
 #include "bytestream.h"
 #include "get_bits.h"
+#include "internal.h"
 
 typedef struct BlockInfo {
     uint8_t *pos;
@@ -115,7 +116,6 @@
     }
     avctx->pix_fmt = AV_PIX_FMT_BGR24;
     avcodec_get_frame_defaults(&s->frame);
-    s->frame.data[0] = NULL;
 
     return 0;
 }
@@ -243,7 +243,7 @@
 {
     int buf_size       = avpkt->size;
     FlashSVContext *s  = avctx->priv_data;
-    int h_blocks, v_blocks, h_part, v_part, i, j;
+    int h_blocks, v_blocks, h_part, v_part, i, j, ret;
     GetBitContext gb;
     int last_blockwidth = s->block_width;
     int last_blockheight= s->block_height;
@@ -269,11 +269,11 @@
     if (s->ver == 2) {
         skip_bits(&gb, 6);
         if (get_bits1(&gb)) {
-            av_log_missing_feature(avctx, "iframe", 1);
+            avpriv_request_sample(avctx, "iframe");
             return AVERROR_PATCHWELCOME;
         }
         if (get_bits1(&gb)) {
-            av_log_missing_feature(avctx, "Custom palette", 1);
+            avpriv_request_sample(avctx, "Custom palette");
             return AVERROR_PATCHWELCOME;
         }
     }
@@ -337,14 +337,8 @@
             s->image_width, s->image_height, s->block_width, s->block_height,
             h_blocks, v_blocks, h_part, v_part);
 
-    s->frame.reference    = 3;
-    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID    |
-                            FF_BUFFER_HINTS_PRESERVE |
-                            FF_BUFFER_HINTS_REUSABLE;
-    if (avctx->reget_buffer(avctx, &s->frame) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
-        return -1;
-    }
+    if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0)
+        return ret;
 
     /* loop over all block columns */
     for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {
@@ -368,8 +362,7 @@
             s->diff_height    = cur_blk_height;
 
             if (8 * size > get_bits_left(&gb)) {
-                avctx->release_buffer(avctx, &s->frame);
-                s->frame.data[0] = NULL;
+                av_frame_unref(&s->frame);
                 return AVERROR_INVALIDDATA;
             }
 
@@ -408,7 +401,7 @@
                     int row = get_bits(&gb, 8);
                     av_log(avctx, AV_LOG_DEBUG, "%dx%d zlibprime_curr %dx%d\n", i, j, col, row);
                     size -= 2;
-                    av_log_missing_feature(avctx, "zlibprime_curr", 1);
+                    avpriv_request_sample(avctx, "zlibprime_curr");
                     return AVERROR_PATCHWELCOME;
                 }
                 if (!s->blocks && (s->zlibprime_curr || s->zlibprime_prev)) {
@@ -451,8 +444,10 @@
         memcpy(s->keyframe, s->frame.data[0], s->frame.linesize[0] * avctx->height);
     }
 
+    if ((ret = av_frame_ref(data, &s->frame)) < 0)
+        return ret;
+
     *got_frame = 1;
-    *(AVFrame*)data = s->frame;
 
     if ((get_bits_count(&gb) / 8) != buf_size)
         av_log(avctx, AV_LOG_ERROR, "buffer not fully consumed (%d != %d)\n",
@@ -468,8 +463,7 @@
     FlashSVContext *s = avctx->priv_data;
     inflateEnd(&s->zstream);
     /* release the frame if needed */
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+    av_frame_unref(&s->frame);
 
     /* free the tmpblock */
     av_free(s->tmpblock);
diff --git a/libavcodec/flicvideo.c b/libavcodec/flicvideo.c
index 2f8cd33..90885fc 100644
--- a/libavcodec/flicvideo.c
+++ b/libavcodec/flicvideo.c
@@ -42,6 +42,7 @@
 #include "libavutil/intreadwrite.h"
 #include "avcodec.h"
 #include "bytestream.h"
+#include "internal.h"
 #include "mathops.h"
 
 #define FLI_256_COLOR 4
@@ -141,7 +142,6 @@
     }
 
     avcodec_get_frame_defaults(&s->frame);
-    s->frame.data[0] = NULL;
     s->new_palette = 0;
 
     return 0;
@@ -185,12 +185,8 @@
 
     bytestream2_init(&g2, buf, buf_size);
 
-    s->frame.reference = 3;
-    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
-    if ((ret = avctx->reget_buffer(avctx, &s->frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0)
         return ret;
-    }
 
     pixels = s->frame.data[0];
     pixel_limit = s->avctx->height * s->frame.linesize[0];
@@ -466,8 +462,10 @@
         s->new_palette = 0;
     }
 
+    if ((ret = av_frame_ref(data, &s->frame)) < 0)
+        return ret;
+
     *got_frame = 1;
-    *(AVFrame*)data = s->frame;
 
     return buf_size;
 }
@@ -505,12 +503,8 @@
 
     bytestream2_init(&g2, buf, buf_size);
 
-    s->frame.reference = 3;
-    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
-    if ((ret = avctx->reget_buffer(avctx, &s->frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0)
         return ret;
-    }
 
     pixels = s->frame.data[0];
     pixel_limit = s->avctx->height * s->frame.linesize[0];
@@ -752,9 +746,10 @@
         av_log(avctx, AV_LOG_ERROR, "Processed FLI chunk where chunk size = %d " \
                "and final chunk ptr = %d\n", buf_size, bytestream2_tell(&g2));
 
+    if ((ret = av_frame_ref(data, &s->frame)) < 0)
+        return ret;
 
     *got_frame = 1;
-    *(AVFrame*)data = s->frame;
 
     return buf_size;
 }
@@ -800,8 +795,7 @@
 {
     FlicDecodeContext *s = avctx->priv_data;
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+    av_frame_unref(&s->frame);
 
     return 0;
 }
diff --git a/libavcodec/frame_thread_encoder.c b/libavcodec/frame_thread_encoder.c
index 0ac018b..b11a54a 100644
--- a/libavcodec/frame_thread_encoder.c
+++ b/libavcodec/frame_thread_encoder.c
@@ -92,9 +92,9 @@
 
         ret = avcodec_encode_video2(avctx, pkt, frame, &got_packet);
         pthread_mutex_lock(&c->buffer_mutex);
-        c->parent_avctx->release_buffer(c->parent_avctx, frame);
+        av_frame_unref(frame);
         pthread_mutex_unlock(&c->buffer_mutex);
-        av_freep(&frame);
+        av_frame_free(&frame);
         if(got_packet) {
             av_dup_packet(pkt);
         } else {
@@ -222,11 +222,11 @@
 
     if(frame){
         if(!(avctx->flags & CODEC_FLAG_INPUT_PRESERVED)){
-            AVFrame *new = avcodec_alloc_frame();
+            AVFrame *new = av_frame_alloc();
             if(!new)
                 return AVERROR(ENOMEM);
             pthread_mutex_lock(&c->buffer_mutex);
-            ret = ff_get_buffer(c->parent_avctx, new);
+            ret = ff_get_buffer(c->parent_avctx, new, 0);
             pthread_mutex_unlock(&c->buffer_mutex);
             if(ret<0)
                 return ret;
diff --git a/libavcodec/fraps.c b/libavcodec/fraps.c
index 5e2ba90..00a38c6 100644
--- a/libavcodec/fraps.c
+++ b/libavcodec/fraps.c
@@ -36,6 +36,7 @@
 #include "huffman.h"
 #include "bytestream.h"
 #include "dsputil.h"
+#include "internal.h"
 #include "thread.h"
 
 #define FPS_TAG MKTAG('F', 'P', 'S', 'x')
@@ -45,7 +46,6 @@
  */
 typedef struct FrapsContext {
     AVCodecContext *avctx;
-    AVFrame frame;
     uint8_t *tmpbuf;
     int tmpbuf_size;
     DSPContext dsp;
@@ -61,9 +61,6 @@
 {
     FrapsContext * const s = avctx->priv_data;
 
-    avcodec_get_frame_defaults(&s->frame);
-    avctx->coded_frame = &s->frame;
-
     s->avctx  = avctx;
     s->tmpbuf = NULL;
 
@@ -134,8 +131,8 @@
     FrapsContext * const s = avctx->priv_data;
     const uint8_t *buf     = avpkt->data;
     int buf_size           = avpkt->size;
-    AVFrame *frame         = data;
-    AVFrame * const f      = &s->frame;
+    ThreadFrame frame = { .f = data };
+    AVFrame * const f = data;
     uint32_t header;
     unsigned int version,header_size;
     unsigned int x, y;
@@ -145,7 +142,6 @@
     int i, j, ret, is_chroma;
     const int planes = 3;
     uint8_t *out;
-    enum AVPixelFormat pix_fmt;
 
     header      = AV_RL32(buf);
     version     = header & 0xff;
@@ -200,23 +196,13 @@
         }
     }
 
-    if (f->data[0])
-        ff_thread_release_buffer(avctx, f);
     f->pict_type = AV_PICTURE_TYPE_I;
     f->key_frame = 1;
-    f->reference = 0;
-    f->buffer_hints = FF_BUFFER_HINTS_VALID;
 
-    pix_fmt = version & 1 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
-    if (avctx->pix_fmt != pix_fmt && f->data[0]) {
-        avctx->release_buffer(avctx, f);
-    }
-    avctx->pix_fmt = pix_fmt;
+    avctx->pix_fmt = version & 1 ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_YUVJ420P;
 
-    if ((ret = ff_thread_get_buffer(avctx, f))) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
         return ret;
-    }
 
     switch (version) {
     case 0:
@@ -296,7 +282,6 @@
         break;
     }
 
-    *frame = *f;
     *got_frame = 1;
 
     return buf_size;
@@ -312,9 +297,6 @@
 {
     FrapsContext *s = (FrapsContext*)avctx->priv_data;
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
-
     av_freep(&s->tmpbuf);
     return 0;
 }
diff --git a/libavcodec/frwu.c b/libavcodec/frwu.c
index 43feb01..b1c7408 100644
--- a/libavcodec/frwu.c
+++ b/libavcodec/frwu.c
@@ -38,10 +38,6 @@
     }
     avctx->pix_fmt = AV_PIX_FMT_UYVY422;
 
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame)
-        return AVERROR(ENOMEM);
-
     return 0;
 }
 
@@ -50,13 +46,10 @@
 {
     FRWUContext *s = avctx->priv_data;
     int field, ret;
-    AVFrame *pic = avctx->coded_frame;
+    AVFrame *pic = data;
     const uint8_t *buf = avpkt->data;
     const uint8_t *buf_end = buf + avpkt->size;
 
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
-
     if (avpkt->size < avctx->width * 2 * avctx->height + 4 + 2*8) {
         av_log(avctx, AV_LOG_ERROR, "Packet is too small.\n");
         return AVERROR_INVALIDDATA;
@@ -66,11 +59,8 @@
         return AVERROR_INVALIDDATA;
     }
 
-    pic->reference = 0;
-    if ((ret = ff_get_buffer(avctx, pic)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
         return ret;
-    }
 
     pic->pict_type = AV_PICTURE_TYPE_I;
     pic->key_frame = 1;
@@ -108,21 +98,10 @@
     }
 
     *got_frame = 1;
-    *(AVFrame*)data = *pic;
 
     return avpkt->size;
 }
 
-static av_cold int decode_close(AVCodecContext *avctx)
-{
-    AVFrame *pic = avctx->coded_frame;
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
-    av_freep(&avctx->coded_frame);
-
-    return 0;
-}
-
 static const AVOption frwu_options[] = {
     {"change_field_order", "Change field order", offsetof(FRWUContext, change_field_order), FF_OPT_TYPE_INT,
      {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM},
@@ -142,7 +121,6 @@
     .id             = AV_CODEC_ID_FRWU,
     .priv_data_size = sizeof(FRWUContext),
     .init           = decode_init,
-    .close          = decode_close,
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("Forward Uncompressed"),
diff --git a/libavcodec/g722dec.c b/libavcodec/g722dec.c
index b9c634b..1a489a7 100644
--- a/libavcodec/g722dec.c
+++ b/libavcodec/g722dec.c
@@ -94,10 +94,8 @@
 
     /* get output buffer */
     frame->nb_samples = avpkt->size * 2;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     out_buf = (int16_t *)frame->data[0];
 
     init_get_bits(&gb, avpkt->data, avpkt->size * 8);
diff --git a/libavcodec/g722enc.c b/libavcodec/g722enc.c
index 39f83dc..b276b4e 100644
--- a/libavcodec/g722enc.c
+++ b/libavcodec/g722enc.c
@@ -53,9 +53,6 @@
         av_freep(&c->node_buf[i]);
         av_freep(&c->nodep_buf[i]);
     }
-#if FF_API_OLD_ENCODE_AUDIO
-    av_freep(&avctx->coded_frame);
-#endif
     return 0;
 }
 
@@ -123,14 +120,6 @@
         }
     }
 
-#if FF_API_OLD_ENCODE_AUDIO
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame) {
-        ret = AVERROR(ENOMEM);
-        goto error;
-    }
-#endif
-
     return 0;
 error:
     g722_encode_close(avctx);
@@ -368,7 +357,7 @@
     int nb_samples, out_size, ret;
 
     out_size = (frame->nb_samples + 1) / 2;
-    if ((ret = ff_alloc_packet2(avctx, avpkt, out_size)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, out_size)) < 0)
         return ret;
 
     nb_samples = frame->nb_samples - (frame->nb_samples & 1);
diff --git a/libavcodec/g723_1.c b/libavcodec/g723_1.c
index 1af2992..6254c9a 100644
--- a/libavcodec/g723_1.c
+++ b/libavcodec/g723_1.c
@@ -1185,10 +1185,8 @@
     }
 
     frame->nb_samples = FRAME_LEN;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     out = (int16_t *)frame->data[0];
 
@@ -2458,7 +2456,7 @@
         offset += LPC_ORDER;
     }
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, 24)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, 24)) < 0)
         return ret;
 
     *got_packet_ptr = 1;
diff --git a/libavcodec/g726.c b/libavcodec/g726.c
index 638a737..58d0468 100644
--- a/libavcodec/g726.c
+++ b/libavcodec/g726.c
@@ -331,13 +331,6 @@
 
     g726_reset(c);
 
-#if FF_API_OLD_ENCODE_AUDIO
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame)
-        return AVERROR(ENOMEM);
-    avctx->coded_frame->key_frame = 1;
-#endif
-
     /* select a frame size that will end on a byte boundary and have a size of
        approximately 1024 bytes */
     avctx->frame_size = ((int[]){ 4096, 2736, 2048, 1640 })[c->code_size - 2];
@@ -345,14 +338,6 @@
     return 0;
 }
 
-#if FF_API_OLD_ENCODE_AUDIO
-static av_cold int g726_encode_close(AVCodecContext *avctx)
-{
-    av_freep(&avctx->coded_frame);
-    return 0;
-}
-#endif
-
 static int g726_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                              const AVFrame *frame, int *got_packet_ptr)
 {
@@ -362,7 +347,7 @@
     int i, ret, out_size;
 
     out_size = (frame->nb_samples * c->code_size + 7) / 8;
-    if ((ret = ff_alloc_packet2(avctx, avpkt, out_size)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, out_size)) < 0)
         return ret;
     init_put_bits(&pb, avpkt->data, avpkt->size);
 
@@ -402,9 +387,6 @@
     .priv_data_size = sizeof(G726Context),
     .init           = g726_encode_init,
     .encode2        = g726_encode_frame,
-#if FF_API_OLD_ENCODE_AUDIO
-    .close          = g726_encode_close,
-#endif
     .capabilities   = CODEC_CAP_SMALL_LAST_FRAME,
     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
                                                      AV_SAMPLE_FMT_NONE },
@@ -449,10 +431,8 @@
 
     /* get output buffer */
     frame->nb_samples = out_samples;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     samples = (int16_t *)frame->data[0];
 
     init_get_bits(&gb, buf, buf_size * 8);
diff --git a/libavcodec/g729dec.c b/libavcodec/g729dec.c
index db3f013..85323ba 100644
--- a/libavcodec/g729dec.c
+++ b/libavcodec/g729dec.c
@@ -101,7 +101,6 @@
 
 typedef struct {
     DSPContext dsp;
-    AVFrame frame;
 
     /// past excitation signal buffer
     int16_t exc_base[2*SUBFRAME_SIZE+PITCH_DELAY_MAX+INTERPOL_LEN];
@@ -385,9 +384,6 @@
     ff_dsputil_init(&ctx->dsp, avctx);
     ctx->dsp.scalarproduct_int16 = scalarproduct_int16_c;
 
-    avcodec_get_frame_defaults(&ctx->frame);
-    avctx->coded_frame = &ctx->frame;
-
     return 0;
 }
 
@@ -418,13 +414,12 @@
     int j, ret;
     int gain_before, gain_after;
     int is_periodic = 0;         // whether one of the subframes is declared as periodic or not
+    AVFrame *frame = data;
 
-    ctx->frame.nb_samples = SUBFRAME_SIZE<<1;
-    if ((ret = ff_get_buffer(avctx, &ctx->frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    frame->nb_samples = SUBFRAME_SIZE<<1;
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
-    out_frame = (int16_t*) ctx->frame.data[0];
+    out_frame = (int16_t*) frame->data[0];
 
     if (buf_size == 10) {
         packet_type = FORMAT_G729_8K;
@@ -716,7 +711,6 @@
     memmove(ctx->exc_base, ctx->exc_base + 2 * SUBFRAME_SIZE, (PITCH_DELAY_MAX+INTERPOL_LEN)*sizeof(int16_t));
 
     *got_frame_ptr = 1;
-    *(AVFrame*)data = ctx->frame;
     return buf_size;
 }
 
diff --git a/libavcodec/gif.c b/libavcodec/gif.c
index de3e576..52c1d44 100644
--- a/libavcodec/gif.c
+++ b/libavcodec/gif.c
@@ -1,9 +1,10 @@
 /*
- * GIF encoder.
  * Copyright (c) 2000 Fabrice Bellard
  * Copyright (c) 2002 Francois Revol
  * Copyright (c) 2006 Baptiste Coudurier
  *
+ * first version by Francois Revol <revol@free.fr>
+ *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
@@ -21,101 +22,175 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-/*
- * First version by Francois Revol revol@free.fr
- *
- * Features and limitations:
- * - uses only a global standard palette
- * - tested with IE 5.0, Opera for BeOS, NetPositive (BeOS), and Mozilla (BeOS).
- *
- * Reference documents:
- * http://www.goice.co.jp/member/mo/formats/gif.html
- * http://astronomy.swin.edu.au/pbourke/dataformats/gif/
- * http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/GIF89a.txt
+/**
+ * @file
+ * GIF encoder
+ * @see http://www.w3.org/Graphics/GIF/spec-gif89a.txt
  */
 
+#include "libavutil/opt.h"
+#include "libavutil/imgutils.h"
 #include "avcodec.h"
 #include "bytestream.h"
 #include "internal.h"
 #include "lzw.h"
+#include "gif.h"
 
-/* The GIF format uses reversed order for bitstreams... */
-/* at least they don't use PDP_ENDIAN :) */
 #define BITSTREAM_WRITER_LE
-
 #include "put_bits.h"
 
 typedef struct {
+    const AVClass *class;
     AVFrame picture;
     LZWState *lzw;
     uint8_t *buf;
+    AVFrame *last_frame;
+    int flags;
+    uint32_t palette[AVPALETTE_COUNT];  ///< local reference palette for !pal8
+    uint8_t *tmpl;                      ///< temporary line buffer
 } GIFContext;
 
-/* GIF header */
-static int gif_image_write_header(AVCodecContext *avctx,
-                                  uint8_t **bytestream, uint32_t *palette)
+enum {
+    GF_OFFSETTING = 1<<0,
+    GF_TRANSDIFF  = 1<<1,
+};
+
+static int pick_palette_entry(const uint8_t *buf, int linesize, int w, int h)
 {
-    int i;
-    unsigned int v, smallest_alpha = 0xFF, alpha_component = 0;
+    int histogram[AVPALETTE_COUNT] = {0};
+    int x, y, i;
 
-    bytestream_put_buffer(bytestream, "GIF", 3);
-    bytestream_put_buffer(bytestream, "89a", 3);
-    bytestream_put_le16(bytestream, avctx->width);
-    bytestream_put_le16(bytestream, avctx->height);
-
-    bytestream_put_byte(bytestream, 0xf7); /* flags: global clut, 256 entries */
-    bytestream_put_byte(bytestream, 0x1f); /* background color index */
-    bytestream_put_byte(bytestream, 0); /* aspect ratio */
-
-    /* the global palette */
-    for(i=0;i<256;i++) {
-        v = palette[i];
-        bytestream_put_be24(bytestream, v);
-        if (v >> 24 < smallest_alpha) {
-            smallest_alpha = v >> 24;
-            alpha_component = i;
-        }
+    for (y = 0; y < h; y++) {
+        for (x = 0; x < w; x++)
+            histogram[buf[x]]++;
+        buf += linesize;
     }
-
-    if (smallest_alpha < 128) {
-        bytestream_put_byte(bytestream, 0x21); /* Extension Introducer */
-        bytestream_put_byte(bytestream, 0xf9); /* Graphic Control Label */
-        bytestream_put_byte(bytestream, 0x04); /* block length */
-        bytestream_put_byte(bytestream, 0x01); /* Transparent Color Flag */
-        bytestream_put_le16(bytestream, 0x00); /* no delay */
-        bytestream_put_byte(bytestream, alpha_component);
-        bytestream_put_byte(bytestream, 0x00);
-    }
-
-    return 0;
+    for (i = 0; i < FF_ARRAY_ELEMS(histogram); i++)
+        if (!histogram[i])
+            return i;
+    return -1;
 }
 
 static int gif_image_write_image(AVCodecContext *avctx,
                                  uint8_t **bytestream, uint8_t *end,
-                                 const uint8_t *buf, int linesize)
+                                 const uint32_t *palette,
+                                 const uint8_t *buf, const int linesize,
+                                 AVPacket *pkt)
 {
     GIFContext *s = avctx->priv_data;
-    int len = 0, height;
+    int len = 0, height = avctx->height, width = avctx->width, x, y;
+    int x_start = 0, y_start = 0, trans = -1;
     const uint8_t *ptr;
-    /* image block */
 
-    bytestream_put_byte(bytestream, 0x2c);
-    bytestream_put_le16(bytestream, 0);
-    bytestream_put_le16(bytestream, 0);
-    bytestream_put_le16(bytestream, avctx->width);
-    bytestream_put_le16(bytestream, avctx->height);
-    bytestream_put_byte(bytestream, 0x00); /* flags */
-    /* no local clut */
+    /* Crop image */
+    // TODO support with palette change
+    if ((s->flags & GF_OFFSETTING) && s->last_frame && !palette) {
+        const uint8_t *ref = s->last_frame->data[0];
+        const int ref_linesize = s->last_frame->linesize[0];
+        int x_end = avctx->width  - 1,
+            y_end = avctx->height - 1;
+
+        /* skip common lines */
+        while (y_start < y_end) {
+            if (memcmp(ref + y_start*ref_linesize, buf + y_start*linesize, width))
+                break;
+            y_start++;
+        }
+        while (y_end > y_start) {
+            if (memcmp(ref + y_end*ref_linesize, buf + y_end*linesize, width))
+                break;
+            y_end--;
+        }
+        height = y_end + 1 - y_start;
+
+        /* skip common columns */
+        while (x_start < x_end) {
+            int same_column = 1;
+            for (y = y_start; y < y_end; y++) {
+                if (ref[y*ref_linesize + x_start] != buf[y*linesize + x_start]) {
+                    same_column = 0;
+                    break;
+                }
+            }
+            if (!same_column)
+                break;
+            x_start++;
+        }
+        while (x_end > x_start) {
+            int same_column = 1;
+            for (y = y_start; y < y_end; y++) {
+                if (ref[y*ref_linesize + x_end] != buf[y*linesize + x_end]) {
+                    same_column = 0;
+                    break;
+                }
+            }
+            if (!same_column)
+                break;
+            x_end--;
+        }
+        width = x_end + 1 - x_start;
+
+        av_log(avctx, AV_LOG_DEBUG,"%dx%d image at pos (%d;%d) [area:%dx%d]\n",
+               width, height, x_start, y_start, avctx->width, avctx->height);
+    }
+
+    /* image block */
+    bytestream_put_byte(bytestream, GIF_IMAGE_SEPARATOR);
+    bytestream_put_le16(bytestream, x_start);
+    bytestream_put_le16(bytestream, y_start);
+    bytestream_put_le16(bytestream, width);
+    bytestream_put_le16(bytestream, height);
+
+    if (!palette) {
+        bytestream_put_byte(bytestream, 0x00); /* flags */
+    } else {
+        unsigned i;
+        bytestream_put_byte(bytestream, 1<<7 | 0x7); /* flags */
+        for (i = 0; i < AVPALETTE_COUNT; i++) {
+            const uint32_t v = palette[i];
+            bytestream_put_be24(bytestream, v);
+        }
+    }
+
+    /* TODO: support with palette change (pal8) */
+    if ((s->flags & GF_TRANSDIFF) && s->last_frame && !palette) {
+        trans = pick_palette_entry(buf + y_start*linesize + x_start,
+                                   linesize, width, height);
+        if (trans < 0) { // TODO, patch welcome
+            av_log(avctx, AV_LOG_DEBUG, "No available color, can not use transparency\n");
+        } else {
+            uint8_t *pal_exdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
+            if (!pal_exdata)
+                return AVERROR(ENOMEM);
+            memcpy(pal_exdata, s->palette, AVPALETTE_SIZE);
+            pal_exdata[trans*4 + 3] = 0x00;
+        }
+    }
 
     bytestream_put_byte(bytestream, 0x08);
 
-    ff_lzw_encode_init(s->lzw, s->buf, avctx->width*avctx->height,
+    ff_lzw_encode_init(s->lzw, s->buf, width * height,
                        12, FF_LZW_GIF, put_bits);
 
-    ptr = buf;
-    for (height = avctx->height; height--;) {
-        len += ff_lzw_encode(s->lzw, ptr, avctx->width);
-        ptr += linesize;
+    ptr = buf + y_start*linesize + x_start;
+    if (trans >= 0) {
+        const int ref_linesize = s->last_frame->linesize[0];
+        const uint8_t *ref = s->last_frame->data[0] + y_start*ref_linesize + x_start;
+
+        for (y = 0; y < height; y++) {
+            memcpy(s->tmpl, ptr, width);
+            for (x = 0; x < width; x++)
+                if (ref[x] == ptr[x])
+                    s->tmpl[x] = trans;
+            len += ff_lzw_encode(s->lzw, s->tmpl, width);
+            ptr += linesize;
+            ref += ref_linesize;
+        }
+    } else {
+        for (y = 0; y < height; y++) {
+            len += ff_lzw_encode(s->lzw, ptr, width);
+            ptr += linesize;
+        }
     }
     len += ff_lzw_encode_flush(s->lzw, flush_put_bits);
 
@@ -130,7 +205,6 @@
         len -= size;
     }
     bytestream_put_byte(bytestream, 0x00); /* end of image block */
-    bytestream_put_byte(bytestream, 0x3b);
     return 0;
 }
 
@@ -140,26 +214,29 @@
 
     if (avctx->width > 65535 || avctx->height > 65535) {
         av_log(avctx, AV_LOG_ERROR, "GIF does not support resolutions above 65535x65535\n");
-        return -1;
+        return AVERROR(EINVAL);
     }
 
     avctx->coded_frame = &s->picture;
     s->lzw = av_mallocz(ff_lzw_encode_state_size);
-    if (!s->lzw)
-        return AVERROR(ENOMEM);
     s->buf = av_malloc(avctx->width*avctx->height*2);
-    if (!s->buf)
-         return AVERROR(ENOMEM);
+    s->tmpl = av_malloc(avctx->width);
+    if (!s->tmpl || !s->buf || !s->lzw)
+        return AVERROR(ENOMEM);
+
+    if (avpriv_set_systematic_pal2(s->palette, avctx->pix_fmt) < 0)
+        av_assert0(avctx->pix_fmt == AV_PIX_FMT_PAL8);
+
     return 0;
 }
 
-/* better than nothing gif encoder */
 static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
                             const AVFrame *pict, int *got_packet)
 {
     GIFContext *s = avctx->priv_data;
     AVFrame *const p = &s->picture;
     uint8_t *outbuf_ptr, *end;
+    const uint32_t *palette = NULL;
     int ret;
 
     if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*7/5 + FF_MIN_BUFFER_SIZE)) < 0)
@@ -170,8 +247,26 @@
     *p = *pict;
     p->pict_type = AV_PICTURE_TYPE_I;
     p->key_frame = 1;
-    gif_image_write_header(avctx, &outbuf_ptr, (uint32_t *)pict->data[1]);
-    gif_image_write_image(avctx, &outbuf_ptr, end, pict->data[0], pict->linesize[0]);
+
+    if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
+        uint8_t *pal_exdata = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
+        if (!pal_exdata)
+            return AVERROR(ENOMEM);
+        memcpy(pal_exdata, p->data[1], AVPALETTE_SIZE);
+        palette = (uint32_t*)p->data[1];
+    }
+
+    gif_image_write_image(avctx, &outbuf_ptr, end, palette,
+                          pict->data[0], pict->linesize[0], pkt);
+    if (!s->last_frame) {
+        s->last_frame = av_frame_alloc();
+        if (!s->last_frame)
+            return AVERROR(ENOMEM);
+    }
+    av_frame_unref(s->last_frame);
+    ret = av_frame_ref(s->last_frame, (AVFrame*)pict);
+    if (ret < 0)
+        return ret;
 
     pkt->size   = outbuf_ptr - pkt->data;
     pkt->flags |= AV_PKT_FLAG_KEY;
@@ -186,9 +281,27 @@
 
     av_freep(&s->lzw);
     av_freep(&s->buf);
+    av_frame_free(&s->last_frame);
+    av_freep(&s->tmpl);
     return 0;
 }
 
+#define OFFSET(x) offsetof(GIFContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
+static const AVOption gif_options[] = {
+    { "gifflags", "set GIF flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = GF_OFFSETTING|GF_TRANSDIFF}, 0, INT_MAX, FLAGS, "flags" },
+        { "offsetting", "enable picture offsetting", 0, AV_OPT_TYPE_CONST, {.i64=GF_OFFSETTING}, INT_MIN, INT_MAX, FLAGS, "flags" },
+        { "transdiff", "enable transparency detection between frames", 0, AV_OPT_TYPE_CONST, {.i64=GF_TRANSDIFF}, INT_MIN, INT_MAX, FLAGS, "flags" },
+    { NULL }
+};
+
+static const AVClass gif_class = {
+    .class_name = "GIF encoder",
+    .item_name  = av_default_item_name,
+    .option     = gif_options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 AVCodec ff_gif_encoder = {
     .name           = "gif",
     .type           = AVMEDIA_TYPE_VIDEO,
@@ -202,4 +315,5 @@
         AV_PIX_FMT_GRAY8, AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE
     },
     .long_name      = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
+    .priv_class     = &gif_class,
 };
diff --git a/libavcodec/gif.h b/libavcodec/gif.h
index b557534..b4cf665 100644
--- a/libavcodec/gif.h
+++ b/libavcodec/gif.h
@@ -43,5 +43,7 @@
 #define GIF_EXTENSION_INTRODUCER    0x21
 #define GIF_IMAGE_SEPARATOR         0x2c
 #define GIF_GCE_EXT_LABEL           0xf9
+#define GIF_APP_EXT_LABEL           0xff
+#define NETSCAPE_EXT_STR            "NETSCAPE2.0"
 
 #endif /* AVCODEC_GIFDEFS_H */
diff --git a/libavcodec/gifdec.c b/libavcodec/gifdec.c
index 71d3d70..997bd42 100644
--- a/libavcodec/gifdec.c
+++ b/libavcodec/gifdec.c
@@ -39,7 +39,7 @@
 
 typedef struct GifState {
     const AVClass *class;
-    AVFrame picture;
+    AVFrame *frame;
     int screen_width;
     int screen_height;
     int has_global_palette;
@@ -130,7 +130,7 @@
     }
 }
 
-static int gif_read_image(GifState *s)
+static int gif_read_image(GifState *s, AVFrame *frame)
 {
     int left, top, width, height, bits_per_pixel, code_size, flags;
     int is_interleaved, has_local_palette, y, pass, y1, linesize, pal_size;
@@ -173,11 +173,11 @@
     if (s->keyframe) {
         if (s->transparent_color_index == -1 && s->has_global_palette) {
             /* transparency wasn't set before the first frame, fill with background color */
-            gif_fill(&s->picture, s->bg_color);
+            gif_fill(frame, s->bg_color);
         } else {
             /* otherwise fill with transparent color.
              * this is necessary since by default picture filled with 0x80808080. */
-            gif_fill(&s->picture, s->trans_color);
+            gif_fill(frame, s->trans_color);
         }
     }
 
@@ -190,10 +190,10 @@
 
     /* process disposal method */
     if (s->gce_prev_disposal == GCE_DISPOSAL_BACKGROUND) {
-        gif_fill_rect(&s->picture, s->stored_bg_color, s->gce_l, s->gce_t, s->gce_w, s->gce_h);
+        gif_fill_rect(frame, s->stored_bg_color, s->gce_l, s->gce_t, s->gce_w, s->gce_h);
     } else if (s->gce_prev_disposal == GCE_DISPOSAL_RESTORE) {
-        gif_copy_img_rect(s->stored_img, (uint32_t *)s->picture.data[0],
-            s->picture.linesize[0] / sizeof(uint32_t), s->gce_l, s->gce_t, s->gce_w, s->gce_h);
+        gif_copy_img_rect(s->stored_img, (uint32_t *)frame->data[0],
+            frame->linesize[0] / sizeof(uint32_t), s->gce_l, s->gce_t, s->gce_w, s->gce_h);
     }
 
     s->gce_prev_disposal = s->gce_disposal;
@@ -208,12 +208,12 @@
             else
                 s->stored_bg_color = s->bg_color;
         } else if (s->gce_disposal == GCE_DISPOSAL_RESTORE) {
-            av_fast_malloc(&s->stored_img, &s->stored_img_size, s->picture.linesize[0] * s->picture.height);
+            av_fast_malloc(&s->stored_img, &s->stored_img_size, frame->linesize[0] * frame->height);
             if (!s->stored_img)
                 return AVERROR(ENOMEM);
 
-            gif_copy_img_rect((uint32_t *)s->picture.data[0], s->stored_img,
-                s->picture.linesize[0] / sizeof(uint32_t), left, top, width, height);
+            gif_copy_img_rect((uint32_t *)frame->data[0], s->stored_img,
+                frame->linesize[0] / sizeof(uint32_t), left, top, width, height);
         }
     }
 
@@ -230,8 +230,8 @@
     }
 
     /* read all the image */
-    linesize = s->picture.linesize[0] / sizeof(uint32_t);
-    ptr1 = (uint32_t *)s->picture.data[0] + top * linesize + left;
+    linesize = frame->linesize[0] / sizeof(uint32_t);
+    ptr1 = (uint32_t *)frame->data[0] + top * linesize + left;
     ptr = ptr1;
     pass = 0;
     y1 = 0;
@@ -400,17 +400,17 @@
     return 0;
 }
 
-static int gif_parse_next_image(GifState *s)
+static int gif_parse_next_image(GifState *s, AVFrame *frame)
 {
     while (bytestream2_get_bytes_left(&s->gb)) {
         int code = bytestream2_get_byte(&s->gb);
         int ret;
 
-        av_dlog(s->avctx, "code=%02x '%c'\n", code, code);
+        av_log(s->avctx, AV_LOG_DEBUG, "code=%02x '%c'\n", code, code);
 
         switch (code) {
         case GIF_IMAGE_SEPARATOR:
-            return gif_read_image(s);
+            return gif_read_image(s, frame);
         case GIF_EXTENSION_INTRODUCER:
             if ((ret = gif_read_extension(s)) < 0)
                 return ret;
@@ -433,9 +433,9 @@
     s->avctx = avctx;
 
     avctx->pix_fmt = AV_PIX_FMT_RGB32;
-    avcodec_get_frame_defaults(&s->picture);
-    avctx->coded_frame= &s->picture;
-    s->picture.data[0] = NULL;
+    s->frame = av_frame_alloc();
+    if (!s->frame)
+        return AVERROR(ENOMEM);
     ff_lzw_decode_open(&s->lzw);
     return 0;
 }
@@ -443,15 +443,14 @@
 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
 {
     GifState *s = avctx->priv_data;
-    AVFrame *picture = data;
     int ret;
 
     bytestream2_init(&s->gb, avpkt->data, avpkt->size);
 
-    s->picture.pts          = avpkt->pts;
-    s->picture.pkt_pts      = avpkt->pts;
-    s->picture.pkt_dts      = avpkt->dts;
-    s->picture.pkt_duration = avpkt->duration;
+    s->frame->pts     = avpkt->pts;
+    s->frame->pkt_pts = avpkt->pts;
+    s->frame->pkt_dts = avpkt->dts;
+    av_frame_set_pkt_duration(s->frame, avpkt->duration);
 
     if (avpkt->size >= 6) {
         s->keyframe = memcmp(avpkt->data, gif87a_sig, 6) == 0 ||
@@ -469,20 +468,16 @@
             return ret;
         avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
 
-        if (s->picture.data[0])
-            avctx->release_buffer(avctx, &s->picture);
-
-        if ((ret = ff_get_buffer(avctx, &s->picture)) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        av_frame_unref(s->frame);
+        if ((ret = ff_get_buffer(avctx, s->frame, 0)) < 0)
             return ret;
-        }
 
         av_fast_malloc(&s->idx_line, &s->idx_line_size, s->screen_width);
         if (!s->idx_line)
             return AVERROR(ENOMEM);
 
-        s->picture.pict_type = AV_PICTURE_TYPE_I;
-        s->picture.key_frame = 1;
+        s->frame->pict_type = AV_PICTURE_TYPE_I;
+        s->frame->key_frame = 1;
         s->keyframe_ok = 1;
     } else {
         if (!s->keyframe_ok) {
@@ -490,20 +485,19 @@
             return AVERROR_INVALIDDATA;
         }
 
-        if ((ret = avctx->reget_buffer(avctx, &s->picture)) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+        if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
             return ret;
-        }
 
-        s->picture.pict_type = AV_PICTURE_TYPE_P;
-        s->picture.key_frame = 0;
+        s->frame->pict_type = AV_PICTURE_TYPE_P;
+        s->frame->key_frame = 0;
     }
 
-    ret = gif_parse_next_image(s);
+    ret = gif_parse_next_image(s, s->frame);
     if (ret < 0)
         return ret;
 
-    *picture = s->picture;
+    if ((ret = av_frame_ref(data, s->frame)) < 0)
+        return ret;
     *got_frame = 1;
 
     return avpkt->size;
@@ -514,9 +508,7 @@
     GifState *s = avctx->priv_data;
 
     ff_lzw_decode_close(&s->lzw);
-    if(s->picture.data[0])
-        avctx->release_buffer(avctx, &s->picture);
-
+    av_frame_free(&s->frame);
     av_freep(&s->idx_line);
     av_freep(&s->stored_img);
 
diff --git a/libavcodec/gsmdec.c b/libavcodec/gsmdec.c
index f20ef9a..dc64267 100644
--- a/libavcodec/gsmdec.c
+++ b/libavcodec/gsmdec.c
@@ -70,10 +70,8 @@
 
     /* get output buffer */
     frame->nb_samples = avctx->frame_size;
-    if ((res = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
         return res;
-    }
     samples = (int16_t *)frame->data[0];
 
     switch (avctx->codec_id) {
diff --git a/libavcodec/h261.c b/libavcodec/h261.c
index f89d5c7..9836905 100644
--- a/libavcodec/h261.c
+++ b/libavcodec/h261.c
@@ -28,25 +28,65 @@
 #include "avcodec.h"
 #include "h261.h"
 
-#define IS_FIL(a)    ((a)&MB_TYPE_H261_FIL)
+#define IS_FIL(a)    ((a) & MB_TYPE_H261_FIL)
 
-uint8_t ff_h261_rl_table_store[2][2*MAX_RUN + MAX_LEVEL + 3];
+uint8_t ff_h261_rl_table_store[2][2 * MAX_RUN + MAX_LEVEL + 3];
 
-void ff_h261_loop_filter(MpegEncContext *s){
-    H261Context * h= (H261Context*)s;
-    const int linesize  = s->linesize;
-    const int uvlinesize= s->uvlinesize;
-    uint8_t *dest_y = s->dest[0];
-    uint8_t *dest_cb= s->dest[1];
-    uint8_t *dest_cr= s->dest[2];
+static void h261_loop_filter(uint8_t *src, int stride)
+{
+    int x, y, xy, yz;
+    int temp[64];
 
-    if(!(IS_FIL (h->mtype)))
+    for (x = 0; x < 8; x++) {
+        temp[x]         = 4 * src[x];
+        temp[x + 7 * 8] = 4 * src[x + 7 * stride];
+    }
+    for (y = 1; y < 7; y++) {
+        for (x = 0; x < 8; x++) {
+            xy       = y * stride + x;
+            yz       = y * 8      + x;
+            temp[yz] = src[xy - stride] + 2 * src[xy] + src[xy + stride];
+        }
+    }
+
+    for (y = 0; y < 8; y++) {
+        src[y * stride]     = (temp[y * 8]     + 2) >> 2;
+        src[y * stride + 7] = (temp[y * 8 + 7] + 2) >> 2;
+        for (x = 1; x < 7; x++) {
+            xy      = y * stride + x;
+            yz      = y * 8      + x;
+            src[xy] = (temp[yz - 1] + 2 * temp[yz] + temp[yz + 1] + 8) >> 4;
+        }
+    }
+}
+
+void ff_h261_loop_filter(MpegEncContext *s)
+{
+    H261Context *h       = (H261Context *)s;
+    const int linesize   = s->linesize;
+    const int uvlinesize = s->uvlinesize;
+    uint8_t *dest_y      = s->dest[0];
+    uint8_t *dest_cb     = s->dest[1];
+    uint8_t *dest_cr     = s->dest[2];
+
+    if (!(IS_FIL(h->mtype)))
         return;
 
-    s->dsp.h261_loop_filter(dest_y                   , linesize);
-    s->dsp.h261_loop_filter(dest_y                + 8, linesize);
-    s->dsp.h261_loop_filter(dest_y + 8 * linesize    , linesize);
-    s->dsp.h261_loop_filter(dest_y + 8 * linesize + 8, linesize);
-    s->dsp.h261_loop_filter(dest_cb, uvlinesize);
-    s->dsp.h261_loop_filter(dest_cr, uvlinesize);
+    h261_loop_filter(dest_y,                    linesize);
+    h261_loop_filter(dest_y + 8,                linesize);
+    h261_loop_filter(dest_y + 8 * linesize,     linesize);
+    h261_loop_filter(dest_y + 8 * linesize + 8, linesize);
+    h261_loop_filter(dest_cb, uvlinesize);
+    h261_loop_filter(dest_cr, uvlinesize);
+}
+
+av_cold void ff_h261_common_init(void)
+{
+    static int done = 0;
+
+    if (done)
+        return;
+
+    ff_init_rl(&ff_h261_rl_tcoeff, ff_h261_rl_table_store);
+    done = 1;
 }
diff --git a/libavcodec/h261.h b/libavcodec/h261.h
index 5b60dd6..597bf7e 100644
--- a/libavcodec/h261.h
+++ b/libavcodec/h261.h
@@ -29,11 +29,12 @@
 #define AVCODEC_H261_H
 
 #include "mpegvideo.h"
+#include "rl.h"
 
 /**
  * H261Context
  */
-typedef struct H261Context{
+typedef struct H261Context {
     MpegEncContext s;
 
     int current_mba;
@@ -44,8 +45,29 @@
     int current_mv_y;
     int gob_number;
     int gob_start_code_skipped; // 1 if gob start code is already read before gob header is read
-}H261Context;
+} H261Context;
 
 #define MB_TYPE_H261_FIL 0x800000
 
+extern uint8_t ff_h261_rl_table_store[2][2 * MAX_RUN + MAX_LEVEL + 3];
+
+extern const uint8_t ff_h261_mba_code[35];
+extern const uint8_t ff_h261_mba_bits[35];
+extern const uint8_t ff_h261_mtype_code[10];
+extern const uint8_t ff_h261_mtype_bits[10];
+extern const int     ff_h261_mtype_map[10];
+extern const uint8_t ff_h261_mv_tab[17][2];
+extern const uint8_t ff_h261_cbp_tab[63][2];
+extern RLTable ff_h261_rl_tcoeff;
+
+void ff_h261_loop_filter(MpegEncContext *s);
+void ff_h261_common_init(void);
+
+int ff_h261_get_picture_format(int width, int height);
+void ff_h261_reorder_mb_index(MpegEncContext *s);
+void ff_h261_encode_mb(MpegEncContext *s, int16_t block[6][64],
+                       int motion_x, int motion_y);
+void ff_h261_encode_picture_header(MpegEncContext *s, int picture_number);
+void ff_h261_encode_init(MpegEncContext *s);
+
 #endif /* AVCODEC_H261_H */
diff --git a/libavcodec/h261_parser.c b/libavcodec/h261_parser.c
index 753687a..9c31557 100644
--- a/libavcodec/h261_parser.c
+++ b/libavcodec/h261_parser.c
@@ -27,38 +27,39 @@
 
 #include "parser.h"
 
-
-static int h261_find_frame_end(ParseContext *pc, AVCodecContext* avctx, const uint8_t *buf, int buf_size){
+static int h261_find_frame_end(ParseContext *pc, AVCodecContext *avctx,
+                               const uint8_t *buf, int buf_size)
+{
     int vop_found, i, j;
     uint32_t state;
 
-    vop_found= pc->frame_start_found;
-    state= pc->state;
+    vop_found = pc->frame_start_found;
+    state     = pc->state;
 
-    for(i=0; i<buf_size && !vop_found; i++){
-        state= (state<<8) | buf[i];
-        for(j=0; j<8; j++){
-            if(((state>>j)&0xFFFFF0) == 0x000100){
-                vop_found=1;
+    for (i = 0; i < buf_size && !vop_found; i++) {
+        state = (state << 8) | buf[i];
+        for (j = 0; j < 8; j++) {
+            if (((state >> j) & 0xFFFFF0) == 0x000100) {
+                vop_found = 1;
                 break;
             }
         }
     }
-    if(vop_found){
-        for(; i<buf_size; i++){
-            state= (state<<8) | buf[i];
-            for(j=0; j<8; j++){
-                if(((state>>j)&0xFFFFF0) == 0x000100){
-                    pc->frame_start_found=0;
-                    pc->state= (state>>(3*8))+0xFF00;
-                    return i-2;
+    if (vop_found) {
+        for (; i < buf_size; i++) {
+            state = (state << 8) | buf[i];
+            for (j = 0; j < 8; j++) {
+                if (((state >> j) & 0xFFFFF0) == 0x000100) {
+                    pc->frame_start_found = 0;
+                    pc->state             = (state >> (3 * 8)) + 0xFF00;
+                    return i - 2;
                 }
             }
         }
     }
 
-    pc->frame_start_found= vop_found;
-    pc->state= state;
+    pc->frame_start_found = vop_found;
+    pc->state             = state;
     return END_NOT_FOUND;
 }
 
@@ -73,14 +74,14 @@
     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
         next = buf_size;
     } else {
-        next= h261_find_frame_end(pc,avctx, buf, buf_size);
+        next = h261_find_frame_end(pc, avctx, buf, buf_size);
         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
-            *poutbuf = NULL;
+            *poutbuf      = NULL;
             *poutbuf_size = 0;
             return buf_size;
         }
     }
-    *poutbuf = buf;
+    *poutbuf      = buf;
     *poutbuf_size = buf_size;
     return next;
 }
diff --git a/libavcodec/h261data.c b/libavcodec/h261data.c
index a05161f..0d1f305 100644
--- a/libavcodec/h261data.c
+++ b/libavcodec/h261data.c
@@ -25,8 +25,9 @@
  */
 
 #include <stdint.h>
+
+#include "rl.h"
 #include "h261.h"
-#include "h261data.h"
 
 // H.261 VLC table for macroblock addressing
 const uint8_t ff_h261_mba_code[35] = {
@@ -39,8 +40,8 @@
     32, 31, 30, 29,
     28, 27, 26, 25,
     24,
-    15,           //(MBA stuffing)
-    1             //(start code)
+    15, // (MBA stuffing)
+     1  // (start code)
 };
 
 const uint8_t ff_h261_mba_bits[35] = {
@@ -53,97 +54,96 @@
     11, 11, 11, 11,
     11, 11, 11, 11,
     11,
-    11,           //(MBA stuffing)
-    16            //(start code)
+    11, // (MBA stuffing)
+    16  // (start code)
 };
 
-//H.261 VLC table for macroblock type
+// H.261 VLC table for macroblock type
 const uint8_t ff_h261_mtype_code[10] = {
-    1,  1,  1,  1,
-    1,  1,  1,  1,
-    1,  1
+    1, 1, 1, 1,
+    1, 1, 1, 1,
+    1, 1
 };
 
 const uint8_t ff_h261_mtype_bits[10] = {
-    4,  7,  1,  5,
-    9,  8, 10,  3,
-    2,  6
+    4, 7,  1, 5,
+    9, 8, 10, 3,
+    2, 6
 };
 
-const int ff_h261_mtype_map[10]= {
-        MB_TYPE_INTRA4x4,
-        MB_TYPE_INTRA4x4  |  MB_TYPE_QUANT,
-                                               MB_TYPE_CBP,
-                             MB_TYPE_QUANT  |  MB_TYPE_CBP,
-                                                               MB_TYPE_16x16,
-                                               MB_TYPE_CBP  |  MB_TYPE_16x16,
-                             MB_TYPE_QUANT  |  MB_TYPE_CBP  |  MB_TYPE_16x16,
-                                                               MB_TYPE_16x16  |  MB_TYPE_H261_FIL,
-                                               MB_TYPE_CBP  |  MB_TYPE_16x16  |  MB_TYPE_H261_FIL,
-                             MB_TYPE_QUANT  |  MB_TYPE_CBP  |  MB_TYPE_16x16  |  MB_TYPE_H261_FIL
+const int ff_h261_mtype_map[10] = {
+    MB_TYPE_INTRA4x4,
+    MB_TYPE_INTRA4x4 | MB_TYPE_QUANT,
+    MB_TYPE_CBP,
+    MB_TYPE_CBP | MB_TYPE_QUANT,
+    MB_TYPE_16x16,
+    MB_TYPE_16x16 | MB_TYPE_CBP,
+    MB_TYPE_16x16 | MB_TYPE_CBP | MB_TYPE_QUANT,
+    MB_TYPE_16x16 | MB_TYPE_H261_FIL,
+    MB_TYPE_16x16 | MB_TYPE_H261_FIL | MB_TYPE_CBP,
+    MB_TYPE_16x16 | MB_TYPE_H261_FIL | MB_TYPE_CBP | MB_TYPE_QUANT
 };
 
-//H.261 VLC table for motion vectors
+// H.261 VLC table for motion vectors
 const uint8_t ff_h261_mv_tab[17][2] = {
-    {1,1}, {1,2}, {1,3}, {1,4}, {3,6}, {5,7}, {4,7}, {3,7},
-    {11,9}, {10,9}, {9,9}, {17,10}, {16,10}, {15,10}, {14,10}, {13,10}, {12,10}
+    {  1, 1 }, {  1, 2 }, { 1, 3 }, {  1,  4 }, {  3,  6 }, {  5,  7 }, {  4,  7 }, {  3,  7 },
+    { 11, 9 }, { 10, 9 }, { 9, 9 }, { 17, 10 }, { 16, 10 }, { 15, 10 }, { 14, 10 }, { 13, 10 }, { 12, 10 }
 };
 
-//H.261 VLC table for coded block pattern
-const uint8_t ff_h261_cbp_tab[63][2] =
-{
-    {11,5}, {9,5}, {13,6}, {13,4}, {23,7}, {19,7}, {31,8}, {12,4},
-    {22,7}, {18,7}, {30,8}, {19,5}, {27,8}, {23,8}, {19,8}, {11,4},
-    {21,7}, {17,7}, {29,8}, {17,5}, {25,8}, {21,8}, {17,8}, {15,6},
-    {15,8}, {13,8}, {3,9}, {15,5}, {11,8}, {7,8}, {7,9}, {10,4},
-    {20,7}, {16,7}, {28,8}, {14,6}, {14,8}, {12,8}, {2,9}, {16,5},
-    {24,8}, {20,8}, {16,8}, {14,5}, {10,8}, {6,8}, {6,9}, {18,5},
-    {26,8}, {22,8}, {18,8}, {13,5}, {9,8}, {5,8}, {5,9}, {12,5},
-    {8,8}, {4,8}, {4,9}, {7,3}, {10,5}, {8,5}, {12,6}
+// H.261 VLC table for coded block pattern
+const uint8_t ff_h261_cbp_tab[63][2] = {
+    { 11, 5 }, {  9, 5 }, { 13, 6 }, { 13, 4 }, { 23, 7 }, { 19, 7 }, { 31, 8 }, { 12, 4 },
+    { 22, 7 }, { 18, 7 }, { 30, 8 }, { 19, 5 }, { 27, 8 }, { 23, 8 }, { 19, 8 }, { 11, 4 },
+    { 21, 7 }, { 17, 7 }, { 29, 8 }, { 17, 5 }, { 25, 8 }, { 21, 8 }, { 17, 8 }, { 15, 6 },
+    { 15, 8 }, { 13, 8 }, {  3, 9 }, { 15, 5 }, { 11, 8 }, {  7, 8 }, {  7, 9 }, { 10, 4 },
+    { 20, 7 }, { 16, 7 }, { 28, 8 }, { 14, 6 }, { 14, 8 }, { 12, 8 }, {  2, 9 }, { 16, 5 },
+    { 24, 8 }, { 20, 8 }, { 16, 8 }, { 14, 5 }, { 10, 8 }, {  6, 8 }, {  6, 9 }, { 18, 5 },
+    { 26, 8 }, { 22, 8 }, { 18, 8 }, { 13, 5 }, {  9, 8 }, {  5, 8 }, {  5, 9 }, { 12, 5 },
+    {  8, 8 }, {  4, 8 }, {  4, 9 }, {  7, 3 }, { 10, 5 }, {  8, 5 }, { 12, 6 }
 };
 
-//H.261 VLC table for transform coefficients
+// H.261 VLC table for transform coefficients
 static const uint16_t h261_tcoeff_vlc[65][2] = {
-{ 0x2, 2 }, { 0x3, 2 },{ 0x4, 4 },{ 0x5, 5 },
-{ 0x6, 7 },{ 0x26, 8 },{ 0x21, 8 },{ 0xa, 10 },
-{ 0x1d, 12 },{ 0x18, 12 },{ 0x13, 12 },{ 0x10 , 12 },
-{ 0x1a, 13},{ 0x19, 13 }, { 0x18, 13 }, { 0x17, 13 },
-{ 0x3, 3 }, { 0x6, 6 }, { 0x25 , 8 }, { 0xc, 10 },
-{ 0x1b, 12 }, { 0x16, 13 }, { 0x15, 13 }, { 0x5, 4},
-{ 0x4, 7}, { 0xb, 10 }, { 0x14, 12 }, { 0x14, 13 },
-{ 0x7, 5 }, { 0x24, 8 }, { 0x1c, 12 }, { 0x13, 13 },
-{ 0x6, 5 }, { 0xf, 10 }, { 0x12, 12}, { 0x7, 6},
-{ 0x9 , 10 }, { 0x12, 13 }, { 0x5, 6 }, { 0x1e, 12 },
-{ 0x4, 6 }, { 0x15, 12 }, { 0x7, 7 }, { 0x11, 12},
-{ 0x5, 7 }, { 0x11, 13 }, { 0x27, 8 }, { 0x10, 13 },
-{ 0x23, 8 }, { 0x22, 8 }, { 0x20, 8 }, { 0xe , 10 },
-{ 0xd, 10 }, { 0x8, 10 },{ 0x1f, 12 }, { 0x1a, 12 },
-{ 0x19, 12 }, { 0x17, 12 }, { 0x16, 12}, { 0x1f, 13},
-{ 0x1e, 13 }, { 0x1d, 13 }, { 0x1c, 13}, { 0x1b, 13},
-{ 0x1, 6 }                                             //escape
+    {  0x2,  2 }, {  0x3,  2 }, {  0x4,  4 }, {  0x5,  5 },
+    {  0x6,  7 }, { 0x26,  8 }, { 0x21,  8 }, {  0xa, 10 },
+    { 0x1d, 12 }, { 0x18, 12 }, { 0x13, 12 }, { 0x10, 12 },
+    { 0x1a, 13 }, { 0x19, 13 }, { 0x18, 13 }, { 0x17, 13 },
+    {  0x3,  3 }, {  0x6,  6 }, { 0x25,  8 }, {  0xc, 10 },
+    { 0x1b, 12 }, { 0x16, 13 }, { 0x15, 13 }, {  0x5,  4 },
+    {  0x4,  7 }, {  0xb, 10 }, { 0x14, 12 }, { 0x14, 13 },
+    {  0x7,  5 }, { 0x24,  8 }, { 0x1c, 12 }, { 0x13, 13 },
+    {  0x6,  5 }, {  0xf, 10 }, { 0x12, 12 }, {  0x7,  6 },
+    {  0x9, 10 }, { 0x12, 13 }, {  0x5,  6 }, { 0x1e, 12 },
+    {  0x4,  6 }, { 0x15, 12 }, {  0x7,  7 }, { 0x11, 12 },
+    {  0x5,  7 }, { 0x11, 13 }, { 0x27,  8 }, { 0x10, 13 },
+    { 0x23,  8 }, { 0x22,  8 }, { 0x20,  8 }, {  0xe, 10 },
+    {  0xd, 10 }, {  0x8, 10 }, { 0x1f, 12 }, { 0x1a, 12 },
+    { 0x19, 12 }, { 0x17, 12 }, { 0x16, 12 }, { 0x1f, 13 },
+    { 0x1e, 13 }, { 0x1d, 13 }, { 0x1c, 13 }, { 0x1b, 13 },
+    {  0x1,  6 }  // escape
 };
 
 static const int8_t h261_tcoeff_level[64] = {
-    0,  1,  2,  3,  4,  5,  6,  7,
-    8,  9, 10, 11, 12, 13, 14, 15,
-    1,  2,  3,  4,  5,  6,  7,  1,
-    2,  3,  4,  5,  1,  2,  3,  4,
-    1,  2,  3,  1,  2,  3,  1,  2,
-    1,  2,  1,  2,  1,  2,  1,  2,
-    1,  1,  1,  1,  1,  1,  1,  1,
-    1,  1,  1,  1,  1,  1,  1,  1
+    0, 1,  2,  3,  4,  5,  6,  7,
+    8, 9, 10, 11, 12, 13, 14, 15,
+    1, 2,  3,  4,  5,  6,  7,  1,
+    2, 3,  4,  5,  1,  2,  3,  4,
+    1, 2,  3,  1,  2,  3,  1,  2,
+    1, 2,  1,  2,  1,  2,  1,  2,
+    1, 1,  1,  1,  1,  1,  1,  1,
+    1, 1,  1,  1,  1,  1,  1,  1
 };
 
 static const int8_t h261_tcoeff_run[64] = {
-    0,
-    0,  0,  0,  0,  0,  0,  0,  0,
-    0,  0,  0,  0,  0,  0,  0,  1,
-    1,  1,  1,  1,  1,  1,  2,  2,
-    2,  2,  2,  3,  3,  3,  3,  4,
-    4,  4,  5,  5,  5,  6,  6,  7,
-    7,  8,  8,  9,  9, 10, 10, 11,
-   12, 13, 14, 15, 16, 17, 18, 19,
-   20, 21, 22, 23, 24, 25, 26
+     0,
+     0,  0,  0,  0,  0,  0,  0,  0,
+     0,  0,  0,  0,  0,  0,  0,  1,
+     1,  1,  1,  1,  1,  1,  2,  2,
+     2,  2,  2,  3,  3,  3,  3,  4,
+     4,  4,  5,  5,  5,  6,  6,  7,
+     7,  8,  8,  9,  9, 10, 10, 11,
+    12, 13, 14, 15, 16, 17, 18, 19,
+    20, 21, 22, 23, 24, 25, 26
 };
 
 RLTable ff_h261_rl_tcoeff = {
diff --git a/libavcodec/h261data.h b/libavcodec/h261data.h
deleted file mode 100644
index e9e5244..0000000
--- a/libavcodec/h261data.h
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
- * copyright (c) 2004 Maarten Daniels
- *
- * 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
- * H.261 tables.
- */
-
-#ifndef AVCODEC_H261DATA_H
-#define AVCODEC_H261DATA_H
-
-#include <stdint.h>
-#include "h261.h"
-
-extern const uint8_t ff_h261_mba_code[35];
-extern const uint8_t ff_h261_mba_bits[35];
-extern const uint8_t ff_h261_mtype_code[10];
-extern const uint8_t ff_h261_mtype_bits[10];
-extern const int ff_h261_mtype_map[10];
-extern const uint8_t ff_h261_mv_tab[17][2];
-extern const uint8_t ff_h261_cbp_tab[63][2];
-extern RLTable ff_h261_rl_tcoeff;
-
-#endif /* AVCODEC_H261DATA_H */
diff --git a/libavcodec/h261dec.c b/libavcodec/h261dec.c
index eebfd02..8ed16bd 100644
--- a/libavcodec/h261dec.c
+++ b/libavcodec/h261dec.c
@@ -30,7 +30,6 @@
 #include "mpegvideo.h"
 #include "h263.h"
 #include "h261.h"
-#include "h261data.h"
 
 #define H261_MBA_VLC_BITS 9
 #define H261_MTYPE_VLC_BITS 6
@@ -40,55 +39,50 @@
 #define MBA_STUFFING 33
 #define MBA_STARTCODE 34
 
-extern uint8_t ff_h261_rl_table_store[2][2*MAX_RUN + MAX_LEVEL + 3];
-
 static VLC h261_mba_vlc;
 static VLC h261_mtype_vlc;
 static VLC h261_mv_vlc;
 static VLC h261_cbp_vlc;
 
-static int h261_decode_block(H261Context * h, int16_t * block, int n, int coded);
-
-static av_cold void h261_decode_init_vlc(H261Context *h){
+static av_cold void h261_decode_init_vlc(H261Context *h)
+{
     static int done = 0;
 
-    if(!done){
+    if (!done) {
         done = 1;
         INIT_VLC_STATIC(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
-                 ff_h261_mba_bits, 1, 1,
-                 ff_h261_mba_code, 1, 1, 662);
+                        ff_h261_mba_bits, 1, 1,
+                        ff_h261_mba_code, 1, 1, 662);
         INIT_VLC_STATIC(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
-                 ff_h261_mtype_bits, 1, 1,
-                 ff_h261_mtype_code, 1, 1, 80);
+                        ff_h261_mtype_bits, 1, 1,
+                        ff_h261_mtype_code, 1, 1, 80);
         INIT_VLC_STATIC(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
-                 &ff_h261_mv_tab[0][1], 2, 1,
-                 &ff_h261_mv_tab[0][0], 2, 1, 144);
+                        &ff_h261_mv_tab[0][1], 2, 1,
+                        &ff_h261_mv_tab[0][0], 2, 1, 144);
         INIT_VLC_STATIC(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
-                 &ff_h261_cbp_tab[0][1], 2, 1,
-                 &ff_h261_cbp_tab[0][0], 2, 1, 512);
-        ff_init_rl(&ff_h261_rl_tcoeff, ff_h261_rl_table_store);
+                        &ff_h261_cbp_tab[0][1], 2, 1,
+                        &ff_h261_cbp_tab[0][0], 2, 1, 512);
         INIT_VLC_RL(ff_h261_rl_tcoeff, 552);
     }
 }
 
-static av_cold int h261_decode_init(AVCodecContext *avctx){
-    H261Context *h= avctx->priv_data;
-    MpegEncContext * const s = &h->s;
+static av_cold int h261_decode_init(AVCodecContext *avctx)
+{
+    H261Context *h          = avctx->priv_data;
+    MpegEncContext *const s = &h->s;
 
     // set defaults
     ff_MPV_decode_defaults(s);
-    s->avctx = avctx;
+    s->avctx       = avctx;
+    s->width       = s->avctx->coded_width;
+    s->height      = s->avctx->coded_height;
+    s->codec_id    = s->avctx->codec->id;
+    s->out_format  = FMT_H261;
+    s->low_delay   = 1;
+    avctx->pix_fmt = AV_PIX_FMT_YUV420P;
+    s->codec_id    = avctx->codec->id;
 
-    s->width  = s->avctx->coded_width;
-    s->height = s->avctx->coded_height;
-    s->codec_id = s->avctx->codec->id;
-
-    s->out_format = FMT_H261;
-    s->low_delay= 1;
-    avctx->pix_fmt= AV_PIX_FMT_YUV420P;
-
-    s->codec_id= avctx->codec->id;
-
+    ff_h261_common_init();
     h261_decode_init_vlc(h);
 
     h->gob_start_code_skipped = 0;
@@ -100,14 +94,15 @@
  * Decode the group of blocks header or slice header.
  * @return <0 if an error occurred
  */
-static int h261_decode_gob_header(H261Context *h){
+static int h261_decode_gob_header(H261Context *h)
+{
     unsigned int val;
-    MpegEncContext * const s = &h->s;
+    MpegEncContext *const s = &h->s;
 
-    if ( !h->gob_start_code_skipped ){
+    if (!h->gob_start_code_skipped) {
         /* Check for GOB Start Code */
         val = show_bits(&s->gb, 15);
-        if(val)
+        if (val)
             return -1;
 
         /* We have a GBSC */
@@ -117,34 +112,34 @@
     h->gob_start_code_skipped = 0;
 
     h->gob_number = get_bits(&s->gb, 4); /* GN */
-    s->qscale = get_bits(&s->gb, 5); /* GQUANT */
+    s->qscale     = get_bits(&s->gb, 5); /* GQUANT */
 
     /* Check if gob_number is valid */
-    if (s->mb_height==18){ //cif
-        if ((h->gob_number<=0) || (h->gob_number>12))
+    if (s->mb_height == 18) { // CIF
+        if ((h->gob_number <= 0) || (h->gob_number > 12))
             return -1;
-    }
-    else{ //qcif
-        if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5))
+    } else { // QCIF
+        if ((h->gob_number != 1) && (h->gob_number != 3) &&
+            (h->gob_number != 5))
             return -1;
     }
 
     /* GEI */
-    while (get_bits1(&s->gb) != 0) {
+    while (get_bits1(&s->gb) != 0)
         skip_bits(&s->gb, 8);
-    }
 
-    if(s->qscale==0) {
+    if (s->qscale == 0) {
         av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\n");
         if (s->avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
             return -1;
     }
 
-    // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
-    // subsequent macroblocks, MBA is the difference between the absolute addresses of
-    // the macroblock and the last transmitted macroblock.
+    /* For the first transmitted macroblock in a GOB, MBA is the absolute
+     * address. For subsequent macroblocks, MBA is the difference between
+     * the absolute addresses of the macroblock and the last transmitted
+     * macroblock. */
     h->current_mba = 0;
-    h->mba_diff = 0;
+    h->mba_diff    = 0;
 
     return 0;
 }
@@ -153,35 +148,35 @@
  * Decode the group of blocks / video packet header.
  * @return <0 if no resync found
  */
-static int ff_h261_resync(H261Context *h){
-    MpegEncContext * const s = &h->s;
+static int ff_h261_resync(H261Context *h)
+{
+    MpegEncContext *const s = &h->s;
     int left, ret;
 
-    if ( h->gob_start_code_skipped ){
-        ret= h261_decode_gob_header(h);
-        if(ret>=0)
+    if (h->gob_start_code_skipped) {
+        ret = h261_decode_gob_header(h);
+        if (ret >= 0)
             return 0;
-    }
-    else{
-        if(show_bits(&s->gb, 15)==0){
-            ret= h261_decode_gob_header(h);
-            if(ret>=0)
+    } else {
+        if (show_bits(&s->gb, 15) == 0) {
+            ret = h261_decode_gob_header(h);
+            if (ret >= 0)
                 return 0;
         }
-        //OK, it is not where it is supposed to be ...
-        s->gb= s->last_resync_gb;
+        // OK, it is not where it is supposed to be ...
+        s->gb = s->last_resync_gb;
         align_get_bits(&s->gb);
-        left= get_bits_left(&s->gb);
+        left = get_bits_left(&s->gb);
 
-        for(;left>15+1+4+5; left-=8){
-            if(show_bits(&s->gb, 15)==0){
-                GetBitContext bak= s->gb;
+        for (; left > 15 + 1 + 4 + 5; left -= 8) {
+            if (show_bits(&s->gb, 15) == 0) {
+                GetBitContext bak = s->gb;
 
-                ret= h261_decode_gob_header(h);
-                if(ret>=0)
+                ret = h261_decode_gob_header(h);
+                if (ret >= 0)
                     return 0;
 
-                s->gb= bak;
+                s->gb = bak;
             }
             skip_bits(&s->gb, 8);
         }
@@ -194,32 +189,32 @@
  * Decode skipped macroblocks.
  * @return 0
  */
-static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
+static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2)
 {
-    MpegEncContext * const s = &h->s;
+    MpegEncContext *const s = &h->s;
     int i;
 
     s->mb_intra = 0;
 
-    for(i=mba1; i<mba2; i++){
+    for (i = mba1; i < mba2; i++) {
         int j, xy;
 
-        s->mb_x= ((h->gob_number-1) % 2) * 11 + i % 11;
-        s->mb_y= ((h->gob_number-1) / 2) * 3 + i / 11;
-        xy = s->mb_x + s->mb_y * s->mb_stride;
+        s->mb_x = ((h->gob_number - 1) % 2) * 11 + i % 11;
+        s->mb_y = ((h->gob_number - 1) / 2) * 3 + i / 11;
+        xy      = s->mb_x + s->mb_y * s->mb_stride;
         ff_init_block_index(s);
         ff_update_block_index(s);
 
-        for(j=0;j<6;j++)
+        for (j = 0; j < 6; j++)
             s->block_last_index[j] = -1;
 
-        s->mv_dir = MV_DIR_FORWARD;
-        s->mv_type = MV_TYPE_16X16;
-        s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
-        s->mv[0][0][0] = 0;
-        s->mv[0][0][1] = 0;
-        s->mb_skipped = 1;
-        h->mtype &= ~MB_TYPE_H261_FIL;
+        s->mv_dir                      = MV_DIR_FORWARD;
+        s->mv_type                     = MV_TYPE_16X16;
+        s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
+        s->mv[0][0][0]                 = 0;
+        s->mv[0][0][1]                 = 0;
+        s->mb_skipped                  = 1;
+        h->mtype                      &= ~MB_TYPE_H261_FIL;
 
         ff_MPV_decode_mb(s, s->block);
     }
@@ -227,47 +222,140 @@
     return 0;
 }
 
-static int decode_mv_component(GetBitContext *gb, int v){
-    static const int mvmap[17] = {
-        0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16
-    };
+static const int mvmap[17] = {
+    0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16
+};
+
+static int decode_mv_component(GetBitContext *gb, int v)
+{
     int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
 
     /* check if mv_diff is valid */
-    if ( mv_diff < 0 )
+    if (mv_diff < 0)
         return v;
 
     mv_diff = mvmap[mv_diff];
 
-    if(mv_diff && !get_bits1(gb))
-        mv_diff= -mv_diff;
+    if (mv_diff && !get_bits1(gb))
+        mv_diff = -mv_diff;
 
     v += mv_diff;
-    if     (v <=-16) v+= 32;
-    else if(v >= 16) v-= 32;
+    if (v <= -16)
+        v += 32;
+    else if (v >= 16)
+        v -= 32;
 
     return v;
 }
 
-static int h261_decode_mb(H261Context *h){
-    MpegEncContext * const s = &h->s;
+/**
+ * Decode a macroblock.
+ * @return <0 if an error occurred
+ */
+static int h261_decode_block(H261Context *h, int16_t *block, int n, int coded)
+{
+    MpegEncContext *const s = &h->s;
+    int code, level, i, j, run;
+    RLTable *rl = &ff_h261_rl_tcoeff;
+    const uint8_t *scan_table;
+
+    /* For the variable length encoding there are two code tables, one being
+     * used for the first transmitted LEVEL in INTER, INTER + MC and
+     * INTER + MC + FIL blocks, the second for all other LEVELs except the
+     * first one in INTRA blocks which is fixed length coded with 8 bits.
+     * NOTE: The two code tables only differ in one VLC so we handle that
+     * manually. */
+    scan_table = s->intra_scantable.permutated;
+    if (s->mb_intra) {
+        /* DC coef */
+        level = get_bits(&s->gb, 8);
+        // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
+        if ((level & 0x7F) == 0) {
+            av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n",
+                   level, s->mb_x, s->mb_y);
+            return -1;
+        }
+        /* The code 1000 0000 is not used, the reconstruction level of 1024
+         * being coded as 1111 1111. */
+        if (level == 255)
+            level = 128;
+        block[0] = level;
+        i        = 1;
+    } else if (coded) {
+        // Run  Level   Code
+        // EOB          Not possible for first level when cbp is available (that's why the table is different)
+        // 0    1       1s
+        // *    *       0*
+        int check = show_bits(&s->gb, 2);
+        i = 0;
+        if (check & 0x2) {
+            skip_bits(&s->gb, 2);
+            block[0] = (check & 0x1) ? -1 : 1;
+            i        = 1;
+        }
+    } else {
+        i = 0;
+    }
+    if (!coded) {
+        s->block_last_index[n] = i - 1;
+        return 0;
+    }
+    for (;;) {
+        code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
+        if (code < 0) {
+            av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n",
+                   s->mb_x, s->mb_y);
+            return -1;
+        }
+        if (code == rl->n) {
+            /* escape */
+            /* The remaining combinations of (run, level) are encoded with a
+             * 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits
+             * level. */
+            run   = get_bits(&s->gb, 6);
+            level = get_sbits(&s->gb, 8);
+        } else if (code == 0) {
+            break;
+        } else {
+            run   = rl->table_run[code];
+            level = rl->table_level[code];
+            if (get_bits1(&s->gb))
+                level = -level;
+        }
+        i += run;
+        if (i >= 64) {
+            av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n",
+                   s->mb_x, s->mb_y);
+            return -1;
+        }
+        j        = scan_table[i];
+        block[j] = level;
+        i++;
+    }
+    s->block_last_index[n] = i - 1;
+    return 0;
+}
+
+static int h261_decode_mb(H261Context *h)
+{
+    MpegEncContext *const s = &h->s;
     int i, cbp, xy;
 
     cbp = 63;
     // Read mba
-    do{
-        h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
+    do {
+        h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table,
+                               H261_MBA_VLC_BITS, 2);
 
         /* Check for slice end */
         /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
-        if (h->mba_diff == MBA_STARTCODE){ // start code
+        if (h->mba_diff == MBA_STARTCODE) { // start code
             h->gob_start_code_skipped = 1;
             return SLICE_END;
         }
-    }
-    while( h->mba_diff == MBA_STUFFING ); // stuffing
+    } while (h->mba_diff == MBA_STUFFING); // stuffing
 
-    if ( h->mba_diff < 0 ){
+    if (h->mba_diff < 0) {
         if (get_bits_left(&s->gb) <= 7)
             return SLICE_END;
 
@@ -275,15 +363,15 @@
         return SLICE_ERROR;
     }
 
-    h->mba_diff += 1;
+    h->mba_diff    += 1;
     h->current_mba += h->mba_diff;
 
-    if ( h->current_mba > MBA_STUFFING )
+    if (h->current_mba > MBA_STUFFING)
         return SLICE_ERROR;
 
-    s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
-    s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
-    xy = s->mb_x + s->mb_y * s->mb_stride;
+    s->mb_x = ((h->gob_number - 1) % 2) * 11 + ((h->current_mba - 1) % 11);
+    s->mb_y = ((h->gob_number - 1) / 2) * 3 + ((h->current_mba - 1) / 11);
+    xy      = s->mb_x + s->mb_y * s->mb_stride;
     ff_init_block_index(s);
     ff_update_block_index(s);
 
@@ -296,64 +384,62 @@
     h->mtype = ff_h261_mtype_map[h->mtype];
 
     // Read mquant
-    if ( IS_QUANT ( h->mtype ) ){
+    if (IS_QUANT(h->mtype))
         ff_set_qscale(s, get_bits(&s->gb, 5));
-    }
 
     s->mb_intra = IS_INTRA4x4(h->mtype);
 
     // Read mv
-    if ( IS_16X16 ( h->mtype ) ){
-        // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
-        // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
-        // following three situations:
-        // 1) evaluating MVD for macroblocks 1, 12 and 23;
-        // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
-        // 3) MTYPE of the previous macroblock was not MC.
-        if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
-             ( h->mba_diff != 1))
-        {
+    if (IS_16X16(h->mtype)) {
+        /* Motion vector data is included for all MC macroblocks. MVD is
+         * obtained from the macroblock vector by subtracting the vector
+         * of the preceding macroblock. For this calculation the vector
+         * of the preceding macroblock is regarded as zero in the
+         * following three situations:
+         * 1) evaluating MVD for macroblocks 1, 12 and 23;
+         * 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
+         * 3) MTYPE of the previous macroblock was not MC. */
+        if ((h->current_mba ==  1) || (h->current_mba == 12) ||
+            (h->current_mba == 23) || (h->mba_diff != 1)) {
             h->current_mv_x = 0;
             h->current_mv_y = 0;
         }
 
-        h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
-        h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
-    }else{
+        h->current_mv_x = decode_mv_component(&s->gb, h->current_mv_x);
+        h->current_mv_y = decode_mv_component(&s->gb, h->current_mv_y);
+    } else {
         h->current_mv_x = 0;
         h->current_mv_y = 0;
     }
 
     // Read cbp
-    if ( HAS_CBP( h->mtype ) ){
+    if (HAS_CBP(h->mtype))
         cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
-    }
 
-    if(s->mb_intra){
-        s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
+    if (s->mb_intra) {
+        s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
         goto intra;
     }
 
     //set motion vectors
-    s->mv_dir = MV_DIR_FORWARD;
-    s->mv_type = MV_TYPE_16X16;
-    s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
-    s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
-    s->mv[0][0][1] = h->current_mv_y * 2;
+    s->mv_dir                      = MV_DIR_FORWARD;
+    s->mv_type                     = MV_TYPE_16X16;
+    s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
+    s->mv[0][0][0]                 = h->current_mv_x * 2; // gets divided by 2 in motion compensation
+    s->mv[0][0][1]                 = h->current_mv_y * 2;
 
 intra:
     /* decode each block */
-    if(s->mb_intra || HAS_CBP(h->mtype)){
+    if (s->mb_intra || HAS_CBP(h->mtype)) {
         s->dsp.clear_blocks(s->block[0]);
         for (i = 0; i < 6; i++) {
-            if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
+            if (h261_decode_block(h, s->block[i], i, cbp & 32) < 0)
                 return SLICE_ERROR;
-            }
-            cbp+=cbp;
+            cbp += cbp;
         }
-    }else{
+    } else {
         for (i = 0; i < 6; i++)
-            s->block_last_index[i]= -1;
+            s->block_last_index[i] = -1;
     }
 
     ff_MPV_decode_mb(s, s->block);
@@ -362,118 +448,36 @@
 }
 
 /**
- * Decode a macroblock.
- * @return <0 if an error occurred
- */
-static int h261_decode_block(H261Context * h, int16_t * block,
-                             int n, int coded)
-{
-    MpegEncContext * const s = &h->s;
-    int code, level, i, j, run;
-    RLTable *rl = &ff_h261_rl_tcoeff;
-    const uint8_t *scan_table;
-
-    // For the variable length encoding there are two code tables, one being used for
-    // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
-    // for all other LEVELs except the first one in INTRA blocks which is fixed length
-    // coded with 8 bits.
-    // NOTE: the two code tables only differ in one VLC so we handle that manually.
-    scan_table = s->intra_scantable.permutated;
-    if (s->mb_intra){
-        /* DC coef */
-        level = get_bits(&s->gb, 8);
-        // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
-        if((level&0x7F) == 0){
-            av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
-            return -1;
-        }
-        // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
-        if (level == 255)
-            level = 128;
-        block[0] = level;
-        i = 1;
-    }else if(coded){
-        // Run  Level   Code
-        // EOB                  Not possible for first level when cbp is available (that's why the table is different)
-        // 0    1               1s
-        // *    *               0*
-        int check = show_bits(&s->gb, 2);
-        i = 0;
-        if ( check & 0x2 ){
-            skip_bits(&s->gb, 2);
-            block[0] = ( check & 0x1 ) ? -1 : 1;
-            i = 1;
-        }
-    }else{
-        i = 0;
-    }
-    if(!coded){
-        s->block_last_index[n] = i - 1;
-        return 0;
-    }
-    for(;;){
-        code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
-        if (code < 0){
-            av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
-            return -1;
-        }
-        if (code == rl->n) {
-            /* escape */
-            // The remaining combinations of (run, level) are encoded with a 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits level.
-            run = get_bits(&s->gb, 6);
-            level = get_sbits(&s->gb, 8);
-        }else if(code == 0){
-            break;
-        }else{
-            run = rl->table_run[code];
-            level = rl->table_level[code];
-            if (get_bits1(&s->gb))
-                level = -level;
-        }
-        i += run;
-        if (i >= 64){
-            av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
-            return -1;
-        }
-        j = scan_table[i];
-        block[j] = level;
-        i++;
-    }
-    s->block_last_index[n] = i-1;
-    return 0;
-}
-
-/**
  * Decode the H.261 picture header.
  * @return <0 if no startcode found
  */
-static int h261_decode_picture_header(H261Context *h){
-    MpegEncContext * const s = &h->s;
+static int h261_decode_picture_header(H261Context *h)
+{
+    MpegEncContext *const s = &h->s;
     int format, i;
-    uint32_t startcode= 0;
+    uint32_t startcode = 0;
 
-    for(i= get_bits_left(&s->gb); i>24; i-=1){
+    for (i = get_bits_left(&s->gb); i > 24; i -= 1) {
         startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
 
-        if(startcode == 0x10)
+        if (startcode == 0x10)
             break;
     }
 
-    if (startcode != 0x10){
+    if (startcode != 0x10) {
         av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
         return -1;
     }
 
     /* temporal reference */
-    i= get_bits(&s->gb, 5); /* picture timestamp */
-    if(i < (s->picture_number&31))
+    i = get_bits(&s->gb, 5); /* picture timestamp */
+    if (i < (s->picture_number & 31))
         i += 32;
-    s->picture_number = (s->picture_number&~31) + i;
+    s->picture_number = (s->picture_number & ~31) + i;
 
-    s->avctx->time_base= (AVRational){1001, 30000};
+    s->avctx->time_base      = (AVRational) { 1001, 30000 };
     s->current_picture.f.pts = s->picture_number;
 
-
     /* PTYPE starts here */
     skip_bits1(&s->gb); /* split screen off */
     skip_bits1(&s->gb); /* camera  off */
@@ -481,16 +485,16 @@
 
     format = get_bits1(&s->gb);
 
-    //only 2 formats possible
-    if (format == 0){//QCIF
-        s->width = 176;
-        s->height = 144;
-        s->mb_width = 11;
+    // only 2 formats possible
+    if (format == 0) { // QCIF
+        s->width     = 176;
+        s->height    = 144;
+        s->mb_width  = 11;
         s->mb_height = 9;
-    }else{//CIF
-        s->width = 352;
-        s->height = 288;
-        s->mb_width = 22;
+    } else { // CIF
+        s->width     = 352;
+        s->height    = 288;
+        s->mb_width  = 22;
         s->mb_height = 18;
     }
 
@@ -500,39 +504,42 @@
     skip_bits1(&s->gb); /* Reserved */
 
     /* PEI */
-    while (get_bits1(&s->gb) != 0){
+    while (get_bits1(&s->gb) != 0)
         skip_bits(&s->gb, 8);
-    }
 
-    // h261 has no I-FRAMES, but if we pass AV_PICTURE_TYPE_I for the first frame, the codec crashes if it does
-    // not contain all I-blocks (e.g. when a packet is lost)
+    /* H.261 has no I-frames, but if we pass AV_PICTURE_TYPE_I for the first
+     * frame, the codec crashes if it does not contain all I-blocks
+     * (e.g. when a packet is lost). */
     s->pict_type = AV_PICTURE_TYPE_P;
 
     h->gob_number = 0;
     return 0;
 }
 
-static int h261_decode_gob(H261Context *h){
-    MpegEncContext * const s = &h->s;
+static int h261_decode_gob(H261Context *h)
+{
+    MpegEncContext *const s = &h->s;
 
     ff_set_qscale(s, s->qscale);
 
     /* decode mb's */
-    while(h->current_mba <= MBA_STUFFING)
-    {
+    while (h->current_mba <= MBA_STUFFING) {
         int ret;
         /* DCT & quantize */
-        ret= h261_decode_mb(h);
-        if(ret<0){
-            if(ret==SLICE_END){
+        ret = h261_decode_mb(h);
+        if (ret < 0) {
+            if (ret == SLICE_END) {
                 h261_decode_mb_skipped(h, h->current_mba, 33);
                 return 0;
             }
-            av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
+            av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n",
+                   s->mb_x + s->mb_y * s->mb_stride);
             return -1;
         }
 
-        h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
+        h261_decode_mb_skipped(h,
+                               h->current_mba - h->mba_diff,
+                               h->current_mba - 1);
     }
 
     return -1;
@@ -541,62 +548,64 @@
 /**
  * returns the number of bytes consumed for building the current frame
  */
-static int get_consumed_bytes(MpegEncContext *s, int buf_size){
-    int pos= get_bits_count(&s->gb)>>3;
-    if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
-    if(pos+10>buf_size) pos=buf_size; // oops ;)
+static int get_consumed_bytes(MpegEncContext *s, int buf_size)
+{
+    int pos = get_bits_count(&s->gb) >> 3;
+    if (pos == 0)
+        pos = 1;      // avoid infinite loops (i doubt that is needed but ...)
+    if (pos + 10 > buf_size)
+        pos = buf_size;               // oops ;)
 
     return pos;
 }
 
-static int h261_decode_frame(AVCodecContext *avctx,
-                             void *data, int *got_frame,
-                             AVPacket *avpkt)
+static int h261_decode_frame(AVCodecContext *avctx, void *data,
+                             int *got_frame, AVPacket *avpkt)
 {
     const uint8_t *buf = avpkt->data;
-    int buf_size = avpkt->size;
-    H261Context *h= avctx->priv_data;
-    MpegEncContext *s = &h->s;
+    int buf_size       = avpkt->size;
+    H261Context *h     = avctx->priv_data;
+    MpegEncContext *s  = &h->s;
     int ret;
     AVFrame *pict = data;
 
     av_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
     av_dlog(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
-    s->flags= avctx->flags;
-    s->flags2= avctx->flags2;
+    s->flags  = avctx->flags;
+    s->flags2 = avctx->flags2;
 
-    h->gob_start_code_skipped=0;
+    h->gob_start_code_skipped = 0;
 
 retry:
+    init_get_bits(&s->gb, buf, buf_size * 8);
 
-    init_get_bits(&s->gb, buf, buf_size*8);
-
-    if(!s->context_initialized){
-        if (ff_MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
+    if (!s->context_initialized)
+        // we need the IDCT permutaton for reading a custom matrix
+        if (ff_MPV_common_init(s) < 0)
             return -1;
-    }
 
-    //we need to set current_picture_ptr before reading the header, otherwise we cannot store anyting im there
+    /* We need to set current_picture_ptr before reading the header,
+     * otherwise we cannot store anything in there. */
     if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
-        int i= ff_find_unused_picture(s, 0);
+        int i = ff_find_unused_picture(s, 0);
         if (i < 0)
             return i;
-        s->current_picture_ptr= &s->picture[i];
+        s->current_picture_ptr = &s->picture[i];
     }
 
     ret = h261_decode_picture_header(h);
 
     /* skip if the header was thrashed */
-    if (ret < 0){
+    if (ret < 0) {
         av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
         return -1;
     }
 
-    if (s->width != avctx->coded_width || s->height != avctx->coded_height){
-        ParseContext pc= s->parse_context; //FIXME move this demuxing hack to libavformat
-        s->parse_context.buffer=0;
+    if (s->width != avctx->coded_width || s->height != avctx->coded_height) {
+        ParseContext pc = s->parse_context; // FIXME move this demuxing hack to libavformat
+        s->parse_context.buffer = 0;
         ff_MPV_common_end(s);
-        s->parse_context= pc;
+        s->parse_context = pc;
     }
     if (!s->context_initialized) {
         avcodec_set_dimensions(avctx, s->width, s->height);
@@ -608,22 +617,22 @@
     s->current_picture.f.pict_type = s->pict_type;
     s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
 
-    if(  (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==AV_PICTURE_TYPE_B)
-       ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=AV_PICTURE_TYPE_I)
-       || avctx->skip_frame >= AVDISCARD_ALL)
+    if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
+        (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
+         avctx->skip_frame >= AVDISCARD_ALL)
         return get_consumed_bytes(s, buf_size);
 
-    if(ff_MPV_frame_start(s, avctx) < 0)
+    if (ff_MPV_frame_start(s, avctx) < 0)
         return -1;
 
     ff_mpeg_er_frame_start(s);
 
     /* decode each macroblock */
-    s->mb_x=0;
-    s->mb_y=0;
+    s->mb_x = 0;
+    s->mb_y = 0;
 
-    while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
-        if(ff_h261_resync(h)<0)
+    while (h->gob_number < (s->mb_height == 18 ? 12 : 5)) {
+        if (ff_h261_resync(h) < 0)
             break;
         h261_decode_gob(h);
     }
@@ -632,8 +641,9 @@
     av_assert0(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
     av_assert0(s->current_picture.f.pict_type == s->pict_type);
 
-    *pict = s->current_picture_ptr->f;
-    ff_print_debug_info(s, pict);
+    if ((ret = av_frame_ref(pict, &s->current_picture_ptr->f)) < 0)
+        return ret;
+    ff_print_debug_info(s, s->current_picture_ptr, pict);
 
     *got_frame = 1;
 
@@ -642,7 +652,7 @@
 
 static av_cold int h261_decode_end(AVCodecContext *avctx)
 {
-    H261Context *h= avctx->priv_data;
+    H261Context *h    = avctx->priv_data;
     MpegEncContext *s = &h->s;
 
     ff_MPV_common_end(s);
diff --git a/libavcodec/h261enc.c b/libavcodec/h261enc.c
index c22100c..84a8816 100644
--- a/libavcodec/h261enc.c
+++ b/libavcodec/h261enc.c
@@ -30,14 +30,9 @@
 #include "mpegvideo.h"
 #include "h263.h"
 #include "h261.h"
-#include "h261data.h"
 
-extern uint8_t ff_h261_rl_table_store[2][2*MAX_RUN + MAX_LEVEL + 3];
-
-static void h261_encode_block(H261Context * h, int16_t * block,
-                              int n);
-
-int ff_h261_get_picture_format(int width, int height){
+int ff_h261_get_picture_format(int width, int height)
+{
     // QCIF
     if (width == 176 && height == 144)
         return 0;
@@ -49,8 +44,9 @@
         return -1;
 }
 
-void ff_h261_encode_picture_header(MpegEncContext * s, int picture_number){
-    H261Context * h = (H261Context *) s;
+void ff_h261_encode_picture_header(MpegEncContext *s, int picture_number)
+{
+    H261Context *h = (H261Context *)s;
     int format, temp_ref;
 
     avpriv_align_put_bits(&s->pb);
@@ -60,8 +56,8 @@
 
     put_bits(&s->pb, 20, 0x10); /* PSC */
 
-    temp_ref= s->picture_number * (int64_t)30000 * s->avctx->time_base.num /
-                         (1001 * (int64_t)s->avctx->time_base.den); //FIXME maybe this should use a timestamp
+    temp_ref = s->picture_number * (int64_t)30000 * s->avctx->time_base.num /
+               (1001 * (int64_t)s->avctx->time_base.den);   // FIXME maybe this should use a timestamp
     put_sbits(&s->pb, 5, temp_ref); /* TemporalReference */
 
     put_bits(&s->pb, 1, 0); /* split screen off */
@@ -76,7 +72,7 @@
     put_bits(&s->pb, 1, 0); /* reserved */
 
     put_bits(&s->pb, 1, 0); /* no PEI */
-    if(format == 0)
+    if (format == 0)
         h->gob_number = -1;
     else
         h->gob_number = 0;
@@ -86,177 +82,84 @@
 /**
  * Encode a group of blocks header.
  */
-static void h261_encode_gob_header(MpegEncContext * s, int mb_line){
-    H261Context * h = (H261Context *)s;
-    if(ff_h261_get_picture_format(s->width, s->height) == 0){
-        h->gob_number+=2; // QCIF
+static void h261_encode_gob_header(MpegEncContext *s, int mb_line)
+{
+    H261Context *h = (H261Context *)s;
+    if (ff_h261_get_picture_format(s->width, s->height) == 0) {
+        h->gob_number += 2; // QCIF
+    } else {
+        h->gob_number++;    // CIF
     }
-    else{
-        h->gob_number++; // CIF
-    }
-    put_bits(&s->pb, 16, 1); /* GBSC */
+    put_bits(&s->pb, 16, 1);            /* GBSC */
     put_bits(&s->pb, 4, h->gob_number); /* GN */
-    put_bits(&s->pb, 5, s->qscale); /* GQUANT */
-    put_bits(&s->pb, 1, 0); /* no GEI */
-    h->current_mba = 0;
+    put_bits(&s->pb, 5, s->qscale);     /* GQUANT */
+    put_bits(&s->pb, 1, 0);             /* no GEI */
+    h->current_mba  = 0;
     h->previous_mba = 0;
-    h->current_mv_x=0;
-    h->current_mv_y=0;
+    h->current_mv_x = 0;
+    h->current_mv_y = 0;
 }
 
-void ff_h261_reorder_mb_index(MpegEncContext* s){
-    int index= s->mb_x + s->mb_y*s->mb_width;
+void ff_h261_reorder_mb_index(MpegEncContext *s)
+{
+    int index = s->mb_x + s->mb_y * s->mb_width;
 
-    if(index % 33 == 0)
-        h261_encode_gob_header(s,0);
+    if (index % 33 == 0)
+        h261_encode_gob_header(s, 0);
 
     /* for CIF the GOB's are fragmented in the middle of a scanline
-       that's why we need to adjust the x and y index of the macroblocks */
-    if(ff_h261_get_picture_format(s->width,s->height) == 1){ // CIF
-        s->mb_x =     index % 11 ; index /= 11;
-        s->mb_y =     index %  3 ; index /=  3;
-        s->mb_x+= 11*(index %  2); index /=  2;
-        s->mb_y+=  3*index;
+     * that's why we need to adjust the x and y index of the macroblocks */
+    if (ff_h261_get_picture_format(s->width, s->height) == 1) { // CIF
+        s->mb_x  = index % 11;
+        index   /= 11;
+        s->mb_y  = index % 3;
+        index   /= 3;
+        s->mb_x += 11 * (index % 2);
+        index   /= 2;
+        s->mb_y += 3 * index;
 
         ff_init_block_index(s);
         ff_update_block_index(s);
     }
 }
 
-static void h261_encode_motion(H261Context * h, int val){
-    MpegEncContext * const s = &h->s;
+static void h261_encode_motion(H261Context *h, int val)
+{
+    MpegEncContext *const s = &h->s;
     int sign, code;
-    if(val==0){
+    if (val == 0) {
         code = 0;
-        put_bits(&s->pb,ff_h261_mv_tab[code][1],ff_h261_mv_tab[code][0]);
-    }
-    else{
-        if(val > 15)
-            val -=32;
-        if(val < -16)
-            val+=32;
+        put_bits(&s->pb, ff_h261_mv_tab[code][1], ff_h261_mv_tab[code][0]);
+    } else {
+        if (val > 15)
+            val -= 32;
+        if (val < -16)
+            val += 32;
         sign = val < 0;
         code = sign ? -val : val;
-        put_bits(&s->pb,ff_h261_mv_tab[code][1],ff_h261_mv_tab[code][0]);
-        put_bits(&s->pb,1,sign);
+        put_bits(&s->pb, ff_h261_mv_tab[code][1], ff_h261_mv_tab[code][0]);
+        put_bits(&s->pb, 1, sign);
     }
 }
 
-static inline int get_cbp(MpegEncContext * s,
-                      int16_t block[6][64])
+static inline int get_cbp(MpegEncContext *s, int16_t block[6][64])
 {
     int i, cbp;
-    cbp= 0;
-    for (i = 0; i < 6; i++) {
+    cbp = 0;
+    for (i = 0; i < 6; i++)
         if (s->block_last_index[i] >= 0)
             cbp |= 1 << (5 - i);
-    }
     return cbp;
 }
-void ff_h261_encode_mb(MpegEncContext * s,
-         int16_t block[6][64],
-         int motion_x, int motion_y)
-{
-    H261Context * h = (H261Context *)s;
-    int mvd, mv_diff_x, mv_diff_y, i, cbp;
-    cbp = 63; // avoid warning
-    mvd = 0;
-
-    h->current_mba++;
-    h->mtype = 0;
-
-    if (!s->mb_intra){
-        /* compute cbp */
-        cbp= get_cbp(s, block);
-
-        /* mvd indicates if this block is motion compensated */
-        mvd = motion_x | motion_y;
-
-        if((cbp | mvd | s->dquant ) == 0) {
-            /* skip macroblock */
-            s->skip_count++;
-            h->current_mv_x=0;
-            h->current_mv_y=0;
-            return;
-        }
-    }
-
-    /* MB is not skipped, encode MBA */
-    put_bits(&s->pb, ff_h261_mba_bits[(h->current_mba-h->previous_mba)-1], ff_h261_mba_code[(h->current_mba-h->previous_mba)-1]);
-
-    /* calculate MTYPE */
-    if(!s->mb_intra){
-        h->mtype++;
-
-        if(mvd || s->loop_filter)
-            h->mtype+=3;
-        if(s->loop_filter)
-            h->mtype+=3;
-        if(cbp || s->dquant)
-            h->mtype++;
-        av_assert1(h->mtype > 1);
-    }
-
-    if(s->dquant)
-        h->mtype++;
-
-    put_bits(&s->pb, ff_h261_mtype_bits[h->mtype], ff_h261_mtype_code[h->mtype]);
-
-    h->mtype = ff_h261_mtype_map[h->mtype];
-
-    if(IS_QUANT(h->mtype)){
-        ff_set_qscale(s,s->qscale+s->dquant);
-        put_bits(&s->pb, 5, s->qscale);
-    }
-
-    if(IS_16X16(h->mtype)){
-        mv_diff_x = (motion_x >> 1) - h->current_mv_x;
-        mv_diff_y = (motion_y >> 1) - h->current_mv_y;
-        h->current_mv_x = (motion_x >> 1);
-        h->current_mv_y = (motion_y >> 1);
-        h261_encode_motion(h,mv_diff_x);
-        h261_encode_motion(h,mv_diff_y);
-    }
-
-    h->previous_mba = h->current_mba;
-
-    if(HAS_CBP(h->mtype)){
-        av_assert1(cbp>0);
-        put_bits(&s->pb,ff_h261_cbp_tab[cbp-1][1],ff_h261_cbp_tab[cbp-1][0]);
-    }
-    for(i=0; i<6; i++) {
-        /* encode each block */
-        h261_encode_block(h, block[i], i);
-    }
-
-    if ( ( h->current_mba == 11 ) || ( h->current_mba == 22 ) || ( h->current_mba == 33 ) || ( !IS_16X16 ( h->mtype ) )){
-        h->current_mv_x=0;
-        h->current_mv_y=0;
-    }
-}
-
-void ff_h261_encode_init(MpegEncContext *s){
-    static int done = 0;
-
-    if (!done) {
-        done = 1;
-        ff_init_rl(&ff_h261_rl_tcoeff, ff_h261_rl_table_store);
-    }
-
-    s->min_qcoeff= -127;
-    s->max_qcoeff=  127;
-    s->y_dc_scale_table=
-    s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
-}
-
 
 /**
  * Encode an 8x8 block.
  * @param block the 8x8 block
  * @param n block index (0-3 are luma, 4-5 are chroma)
  */
-static void h261_encode_block(H261Context * h, int16_t * block, int n){
-    MpegEncContext * const s = &h->s;
+static void h261_encode_block(H261Context *h, int16_t *block, int n)
+{
+    MpegEncContext *const s = &h->s;
     int level, run, i, j, last_index, last_non_zero, sign, slevel, code;
     RLTable *rl;
 
@@ -266,12 +169,12 @@
         level = block[0];
         /* 255 cannot be represented, so we clamp */
         if (level > 254) {
-            level = 254;
+            level    = 254;
             block[0] = 254;
         }
         /* 0 cannot be represented also */
         else if (level < 1) {
-            level = 1;
+            level    = 1;
             block[0] = 1;
         }
         if (level == 128)
@@ -279,31 +182,33 @@
         else
             put_bits(&s->pb, 8, level);
         i = 1;
-    } else if((block[0]==1 || block[0] == -1) && (s->block_last_index[n] > -1)){
-        //special case
-        put_bits(&s->pb,2,block[0]>0 ? 2 : 3 );
+    } else if ((block[0] == 1 || block[0] == -1) &&
+               (s->block_last_index[n] > -1)) {
+        // special case
+        put_bits(&s->pb, 2, block[0] > 0 ? 2 : 3);
         i = 1;
     } else {
         i = 0;
     }
 
     /* AC coefs */
-    last_index = s->block_last_index[n];
+    last_index    = s->block_last_index[n];
     last_non_zero = i - 1;
     for (; i <= last_index; i++) {
-        j = s->intra_scantable.permutated[i];
+        j     = s->intra_scantable.permutated[i];
         level = block[j];
         if (level) {
-            run = i - last_non_zero - 1;
-            sign = 0;
+            run    = i - last_non_zero - 1;
+            sign   = 0;
             slevel = level;
             if (level < 0) {
-                sign = 1;
+                sign  = 1;
                 level = -level;
             }
-            code = get_rl_index(rl, 0 /*no last in H.261, EOB is used*/, run, level);
-            if(run==0 && level < 16)
-            code+=1;
+            code = get_rl_index(rl, 0 /*no last in H.261, EOB is used*/,
+                                run, level);
+            if (run == 0 && level < 16)
+                code += 1;
             put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
             if (code == rl->n) {
                 put_bits(&s->pb, 6, run);
@@ -316,9 +221,105 @@
             last_non_zero = i;
         }
     }
-    if(last_index > -1){
-        put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]);// END OF BLOCK
+    if (last_index > -1)
+        put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]); // EOB
+}
+
+void ff_h261_encode_mb(MpegEncContext *s, int16_t block[6][64],
+                       int motion_x, int motion_y)
+{
+    H261Context *h = (H261Context *)s;
+    int mvd, mv_diff_x, mv_diff_y, i, cbp;
+    cbp = 63; // avoid warning
+    mvd = 0;
+
+    h->current_mba++;
+    h->mtype = 0;
+
+    if (!s->mb_intra) {
+        /* compute cbp */
+        cbp = get_cbp(s, block);
+
+        /* mvd indicates if this block is motion compensated */
+        mvd = motion_x | motion_y;
+
+        if ((cbp | mvd | s->dquant) == 0) {
+            /* skip macroblock */
+            s->skip_count++;
+            h->current_mv_x = 0;
+            h->current_mv_y = 0;
+            return;
+        }
     }
+
+    /* MB is not skipped, encode MBA */
+    put_bits(&s->pb,
+             ff_h261_mba_bits[(h->current_mba - h->previous_mba) - 1],
+             ff_h261_mba_code[(h->current_mba - h->previous_mba) - 1]);
+
+    /* calculate MTYPE */
+    if (!s->mb_intra) {
+        h->mtype++;
+
+        if (mvd || s->loop_filter)
+            h->mtype += 3;
+        if (s->loop_filter)
+            h->mtype += 3;
+        if (cbp || s->dquant)
+            h->mtype++;
+        av_assert1(h->mtype > 1);
+    }
+
+    if (s->dquant)
+        h->mtype++;
+
+    put_bits(&s->pb,
+             ff_h261_mtype_bits[h->mtype],
+             ff_h261_mtype_code[h->mtype]);
+
+    h->mtype = ff_h261_mtype_map[h->mtype];
+
+    if (IS_QUANT(h->mtype)) {
+        ff_set_qscale(s, s->qscale + s->dquant);
+        put_bits(&s->pb, 5, s->qscale);
+    }
+
+    if (IS_16X16(h->mtype)) {
+        mv_diff_x       = (motion_x >> 1) - h->current_mv_x;
+        mv_diff_y       = (motion_y >> 1) - h->current_mv_y;
+        h->current_mv_x = (motion_x >> 1);
+        h->current_mv_y = (motion_y >> 1);
+        h261_encode_motion(h, mv_diff_x);
+        h261_encode_motion(h, mv_diff_y);
+    }
+
+    h->previous_mba = h->current_mba;
+
+    if (HAS_CBP(h->mtype)) {
+        av_assert1(cbp > 0);
+        put_bits(&s->pb,
+                 ff_h261_cbp_tab[cbp - 1][1],
+                 ff_h261_cbp_tab[cbp - 1][0]);
+    }
+    for (i = 0; i < 6; i++)
+        /* encode each block */
+        h261_encode_block(h, block[i], i);
+
+    if ((h->current_mba == 11) || (h->current_mba == 22) ||
+        (h->current_mba == 33) || (!IS_16X16(h->mtype))) {
+        h->current_mv_x = 0;
+        h->current_mv_y = 0;
+    }
+}
+
+void ff_h261_encode_init(MpegEncContext *s)
+{
+    ff_h261_common_init();
+
+    s->min_qcoeff       = -127;
+    s->max_qcoeff       = 127;
+    s->y_dc_scale_table =
+    s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
 }
 
 FF_MPV_GENERIC_CLASS(h261)
@@ -331,7 +332,8 @@
     .init           = ff_MPV_encode_init,
     .encode2        = ff_MPV_encode_picture,
     .close          = ff_MPV_encode_end,
-    .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
+    .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
+                                                     AV_PIX_FMT_NONE },
     .long_name      = NULL_IF_CONFIG_SMALL("H.261"),
     .priv_class     = &h261_class,
 };
diff --git a/libavcodec/h263.c b/libavcodec/h263.c
index a7e60ea..e60e58e 100644
--- a/libavcodec/h263.c
+++ b/libavcodec/h263.c
@@ -51,7 +51,7 @@
     const int wrap = s->b8_stride;
     const int xy = s->block_index[0];
 
-    s->current_picture.f.mbskip_table[mb_xy] = s->mb_skipped;
+    s->current_picture.mbskip_table[mb_xy] = s->mb_skipped;
 
     if(s->mv_type != MV_TYPE_8X8){
         int motion_x, motion_y;
@@ -70,30 +70,30 @@
                 s->p_field_mv_table[i][0][mb_xy][0]= s->mv[0][i][0];
                 s->p_field_mv_table[i][0][mb_xy][1]= s->mv[0][i][1];
             }
-            s->current_picture.f.ref_index[0][4*mb_xy    ] =
-            s->current_picture.f.ref_index[0][4*mb_xy + 1] = s->field_select[0][0];
-            s->current_picture.f.ref_index[0][4*mb_xy + 2] =
-            s->current_picture.f.ref_index[0][4*mb_xy + 3] = s->field_select[0][1];
+            s->current_picture.ref_index[0][4*mb_xy    ] =
+            s->current_picture.ref_index[0][4*mb_xy + 1] = s->field_select[0][0];
+            s->current_picture.ref_index[0][4*mb_xy + 2] =
+            s->current_picture.ref_index[0][4*mb_xy + 3] = s->field_select[0][1];
         }
 
         /* no update if 8X8 because it has been done during parsing */
-        s->current_picture.f.motion_val[0][xy][0]            = motion_x;
-        s->current_picture.f.motion_val[0][xy][1]            = motion_y;
-        s->current_picture.f.motion_val[0][xy + 1][0]        = motion_x;
-        s->current_picture.f.motion_val[0][xy + 1][1]        = motion_y;
-        s->current_picture.f.motion_val[0][xy + wrap][0]     = motion_x;
-        s->current_picture.f.motion_val[0][xy + wrap][1]     = motion_y;
-        s->current_picture.f.motion_val[0][xy + 1 + wrap][0] = motion_x;
-        s->current_picture.f.motion_val[0][xy + 1 + wrap][1] = motion_y;
+        s->current_picture.motion_val[0][xy][0]            = motion_x;
+        s->current_picture.motion_val[0][xy][1]            = motion_y;
+        s->current_picture.motion_val[0][xy + 1][0]        = motion_x;
+        s->current_picture.motion_val[0][xy + 1][1]        = motion_y;
+        s->current_picture.motion_val[0][xy + wrap][0]     = motion_x;
+        s->current_picture.motion_val[0][xy + wrap][1]     = motion_y;
+        s->current_picture.motion_val[0][xy + 1 + wrap][0] = motion_x;
+        s->current_picture.motion_val[0][xy + 1 + wrap][1] = motion_y;
     }
 
     if(s->encoding){ //FIXME encoding MUST be cleaned up
         if (s->mv_type == MV_TYPE_8X8)
-            s->current_picture.f.mb_type[mb_xy] = MB_TYPE_L0 | MB_TYPE_8x8;
+            s->current_picture.mb_type[mb_xy] = MB_TYPE_L0 | MB_TYPE_8x8;
         else if(s->mb_intra)
-            s->current_picture.f.mb_type[mb_xy] = MB_TYPE_INTRA;
+            s->current_picture.mb_type[mb_xy] = MB_TYPE_INTRA;
         else
-            s->current_picture.f.mb_type[mb_xy] = MB_TYPE_L0 | MB_TYPE_16x16;
+            s->current_picture.mb_type[mb_xy] = MB_TYPE_L0 | MB_TYPE_16x16;
     }
 }
 
@@ -153,7 +153,7 @@
        Diag Top
        Left Center
     */
-    if (!IS_SKIP(s->current_picture.f.mb_type[xy])) {
+    if (!IS_SKIP(s->current_picture.mb_type[xy])) {
         qp_c= s->qscale;
         s->dsp.h263_v_loop_filter(dest_y+8*linesize  , linesize, qp_c);
         s->dsp.h263_v_loop_filter(dest_y+8*linesize+8, linesize, qp_c);
@@ -163,10 +163,10 @@
     if(s->mb_y){
         int qp_dt, qp_tt, qp_tc;
 
-        if (IS_SKIP(s->current_picture.f.mb_type[xy - s->mb_stride]))
+        if (IS_SKIP(s->current_picture.mb_type[xy - s->mb_stride]))
             qp_tt=0;
         else
-            qp_tt = s->current_picture.f.qscale_table[xy - s->mb_stride];
+            qp_tt = s->current_picture.qscale_table[xy - s->mb_stride];
 
         if(qp_c)
             qp_tc= qp_c;
@@ -186,10 +186,10 @@
             s->dsp.h263_h_loop_filter(dest_y-8*linesize+8  ,   linesize, qp_tt);
 
         if(s->mb_x){
-            if (qp_tt || IS_SKIP(s->current_picture.f.mb_type[xy - 1 - s->mb_stride]))
+            if (qp_tt || IS_SKIP(s->current_picture.mb_type[xy - 1 - s->mb_stride]))
                 qp_dt= qp_tt;
             else
-                qp_dt = s->current_picture.f.qscale_table[xy - 1 - s->mb_stride];
+                qp_dt = s->current_picture.qscale_table[xy - 1 - s->mb_stride];
 
             if(qp_dt){
                 const int chroma_qp= s->chroma_qscale_table[qp_dt];
@@ -208,10 +208,10 @@
 
     if(s->mb_x){
         int qp_lc;
-        if (qp_c || IS_SKIP(s->current_picture.f.mb_type[xy - 1]))
+        if (qp_c || IS_SKIP(s->current_picture.mb_type[xy - 1]))
             qp_lc= qp_c;
         else
-            qp_lc = s->current_picture.f.qscale_table[xy - 1];
+            qp_lc = s->current_picture.qscale_table[xy - 1];
 
         if(qp_lc){
             s->dsp.h263_h_loop_filter(dest_y,   linesize, qp_lc);
@@ -320,7 +320,7 @@
     static const int off[4]= {2, 1, 1, -1};
 
     wrap = s->b8_stride;
-    mot_val = s->current_picture.f.motion_val[dir] + s->block_index[block];
+    mot_val = s->current_picture.motion_val[dir] + s->block_index[block];
 
     A = mot_val[ - 1];
     /* special case for first (slice) line */
diff --git a/libavcodec/h263dec.c b/libavcodec/h263dec.c
index 72138e1..6e3d885 100644
--- a/libavcodec/h263dec.c
+++ b/libavcodec/h263dec.c
@@ -28,6 +28,7 @@
 #include "libavutil/cpu.h"
 #include "internal.h"
 #include "avcodec.h"
+#include "error_resilience.h"
 #include "mpegvideo.h"
 #include "h263.h"
 #include "h263_parser.h"
@@ -362,7 +363,8 @@
     if (buf_size == 0) {
         /* special case for last picture */
         if (s->low_delay==0 && s->next_picture_ptr) {
-            *pict = s->next_picture_ptr->f;
+            if ((ret = av_frame_ref(pict, &s->next_picture_ptr->f)) < 0)
+                return ret;
             s->next_picture_ptr= NULL;
 
             *got_frame = 1;
@@ -651,7 +653,8 @@
     if ((ret = ff_MPV_frame_start(s, avctx)) < 0)
         return ret;
 
-    if (!s->divx_packed) ff_thread_finish_setup(avctx);
+    if (!s->divx_packed && !avctx->hwaccel)
+        ff_thread_finish_setup(avctx);
 
     if (CONFIG_MPEG4_VDPAU_DECODER && (s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)) {
         ff_vdpau_mpeg4_decode_picture(s, s->gb.buffer, s->gb.buffer_end - s->gb.buffer);
@@ -740,17 +743,25 @@
 
     ff_MPV_frame_end(s);
 
-    assert(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
-    assert(s->current_picture.f.pict_type == s->pict_type);
+    if (!s->divx_packed && avctx->hwaccel)
+        ff_thread_finish_setup(avctx);
+
+    av_assert1(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
+    av_assert1(s->current_picture.f.pict_type == s->pict_type);
     if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
-        *pict = s->current_picture_ptr->f;
+        if ((ret = av_frame_ref(pict, &s->current_picture_ptr->f)) < 0)
+            return ret;
+        ff_print_debug_info(s, s->current_picture_ptr, pict);
+        ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG1);
     } else if (s->last_picture_ptr != NULL) {
-        *pict = s->last_picture_ptr->f;
+        if ((ret = av_frame_ref(pict, &s->last_picture_ptr->f)) < 0)
+            return ret;
+        ff_print_debug_info(s, s->last_picture_ptr, pict);
+        ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG1);
     }
 
     if(s->last_picture_ptr || s->low_delay){
         *got_frame = 1;
-        ff_print_debug_info(s, pict);
     }
 
 #ifdef PRINT_FRAME_TIME
diff --git a/libavcodec/h264.c b/libavcodec/h264.c
index f88b5a8..a1e0585 100644
--- a/libavcodec/h264.c
+++ b/libavcodec/h264.c
@@ -31,6 +31,7 @@
 #include "cabac.h"
 #include "cabac_functions.h"
 #include "dsputil.h"
+#include "error_resilience.h"
 #include "avcodec.h"
 #include "mpegvideo.h"
 #include "h264.h"
@@ -66,7 +67,7 @@
    14,14,14,14,
 };
 
-static const enum AVPixelFormat hwaccel_pixfmt_list_h264_420[] = {
+static const enum AVPixelFormat h264_hwaccel_pixfmt_list_420[] = {
 #if CONFIG_H264_DXVA2_HWACCEL
     AV_PIX_FMT_DXVA2_VLD,
 #endif
@@ -83,7 +84,7 @@
     AV_PIX_FMT_NONE
 };
 
-static const enum AVPixelFormat hwaccel_pixfmt_list_h264_jpeg_420[] = {
+static const enum AVPixelFormat h264_hwaccel_pixfmt_list_jpeg_420[] = {
 #if CONFIG_H264_DXVA2_HWACCEL
     AV_PIX_FMT_DXVA2_VLD,
 #endif
@@ -127,11 +128,11 @@
         av_log(h->avctx, AV_LOG_DEBUG, "Reference not available for error concealing\n");
         ref = 0;
     }
-    if ((h->ref_list[0][ref].f.reference&3) != 3) {
+    if ((h->ref_list[0][ref].reference&3) != 3) {
         av_log(h->avctx, AV_LOG_DEBUG, "Reference invalid\n");
         return;
     }
-    fill_rectangle(&h->cur_pic.f.ref_index[0][4 * h->mb_xy],
+    fill_rectangle(&h->cur_pic.ref_index[0][4 * h->mb_xy],
                    2, 2, 2, ref, 1);
     fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
     fill_rectangle(h->mv_cache[0][scan8[0]], 4, 4, 8,
@@ -143,35 +144,67 @@
 
 void ff_h264_draw_horiz_band(H264Context *h, int y, int height)
 {
-    ff_draw_horiz_band(h->avctx, NULL, &h->cur_pic,
-                       h->ref_list[0][0].f.data[0] ? &h->ref_list[0][0] : NULL,
-                       y, height, h->picture_structure, h->first_field, 0,
-                       h->low_delay, h->mb_height * 16, h->mb_width * 16);
+    AVCodecContext *avctx = h->avctx;
+    Picture *cur  = &h->cur_pic;
+    Picture *last = h->ref_list[0][0].f.data[0] ? &h->ref_list[0][0] : NULL;
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
+    int vshift = desc->log2_chroma_h;
+    const int field_pic = h->picture_structure != PICT_FRAME;
+    if (field_pic) {
+        height <<= 1;
+        y <<= 1;
+    }
+
+    height = FFMIN(height, avctx->height - y);
+
+    if (field_pic && h->first_field && !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))
+        return;
+
+    if (avctx->draw_horiz_band) {
+        AVFrame *src;
+        int offset[AV_NUM_DATA_POINTERS];
+        int i;
+
+        if (cur->f.pict_type == AV_PICTURE_TYPE_B || h->low_delay ||
+           (avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
+            src = &cur->f;
+        else if (last)
+            src = &last->f;
+        else
+            return;
+
+        offset[0] = y * src->linesize[0];
+        offset[1] =
+        offset[2] = (y >> vshift) * src->linesize[1];
+        for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
+            offset[i] = 0;
+
+        emms_c();
+
+        avctx->draw_horiz_band(avctx, src, offset,
+                               y, h->picture_structure, height);
+    }
 }
 
-static void free_frame_buffer(H264Context *h, Picture *pic)
+static void unref_picture(H264Context *h, Picture *pic)
 {
-    pic->period_since_free = 0;
-    ff_thread_release_buffer(h->avctx, &pic->f);
-    av_freep(&pic->f.hwaccel_picture_private);
-}
-
-static void free_picture(H264Context *h, Picture *pic)
-{
+    int off = offsetof(Picture, tf) + sizeof(pic->tf);
     int i;
 
-    if (pic->f.data[0])
-        free_frame_buffer(h, pic);
+    if (!pic->f.data[0])
+        return;
 
-    av_freep(&pic->qscale_table_base);
-    pic->f.qscale_table = NULL;
-    av_freep(&pic->mb_type_base);
-    pic->f.mb_type = NULL;
+    ff_thread_release_buffer(h->avctx, &pic->tf);
+    av_buffer_unref(&pic->hwaccel_priv_buf);
+
+    av_buffer_unref(&pic->qscale_table_buf);
+    av_buffer_unref(&pic->mb_type_buf);
     for (i = 0; i < 2; i++) {
-        av_freep(&pic->motion_val_base[i]);
-        av_freep(&pic->f.ref_index[i]);
-        pic->f.motion_val[i] = NULL;
+        av_buffer_unref(&pic->motion_val_buf[i]);
+        av_buffer_unref(&pic->ref_index_buf[i]);
     }
+
+    memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
 }
 
 static void release_unused_pictures(H264Context *h, int remove_current)
@@ -179,15 +212,75 @@
     int i;
 
     /* release non reference frames */
-    for (i = 0; i < h->picture_count; i++) {
-        if (h->DPB[i].f.data[0] && !h->DPB[i].f.reference &&
-            (!h->DPB[i].owner2 || h->DPB[i].owner2 == h) &&
+    for (i = 0; i < MAX_PICTURE_COUNT; i++) {
+        if (h->DPB[i].f.data[0] && !h->DPB[i].reference &&
             (remove_current || &h->DPB[i] != h->cur_pic_ptr)) {
-            free_frame_buffer(h, &h->DPB[i]);
+            unref_picture(h, &h->DPB[i]);
         }
     }
 }
 
+static int ref_picture(H264Context *h, Picture *dst, Picture *src)
+{
+    int ret, i;
+
+    av_assert0(!dst->f.buf[0]);
+    av_assert0(src->f.buf[0]);
+
+    src->tf.f = &src->f;
+    dst->tf.f = &dst->f;
+    ret = ff_thread_ref_frame(&dst->tf, &src->tf);
+    if (ret < 0)
+        goto fail;
+
+
+    dst->qscale_table_buf = av_buffer_ref(src->qscale_table_buf);
+    dst->mb_type_buf      = av_buffer_ref(src->mb_type_buf);
+    if (!dst->qscale_table_buf || !dst->mb_type_buf)
+        goto fail;
+    dst->qscale_table = src->qscale_table;
+    dst->mb_type      = src->mb_type;
+
+    for (i = 0; i < 2; i ++) {
+        dst->motion_val_buf[i] = av_buffer_ref(src->motion_val_buf[i]);
+        dst->ref_index_buf[i]  = av_buffer_ref(src->ref_index_buf[i]);
+        if (!dst->motion_val_buf[i] || !dst->ref_index_buf[i])
+            goto fail;
+        dst->motion_val[i] = src->motion_val[i];
+        dst->ref_index[i]  = src->ref_index[i];
+    }
+
+    if (src->hwaccel_picture_private) {
+        dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
+        if (!dst->hwaccel_priv_buf)
+            goto fail;
+        dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
+    }
+
+    for (i = 0; i < 2; i++)
+        dst->field_poc[i] = src->field_poc[i];
+
+    memcpy(dst->ref_poc,   src->ref_poc,   sizeof(src->ref_poc));
+    memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
+
+    dst->poc                     = src->poc;
+    dst->frame_num               = src->frame_num;
+    dst->mmco_reset              = src->mmco_reset;
+    dst->pic_id                  = src->pic_id;
+    dst->long_ref                = src->long_ref;
+    dst->mbaff                   = src->mbaff;
+    dst->field_picture           = src->field_picture;
+    dst->needs_realloc           = src->needs_realloc;
+    dst->reference               = src->reference;
+    dst->sync                    = src->sync;
+
+    return 0;
+fail:
+    unref_picture(h, dst);
+    return ret;
+}
+
+
 static int alloc_scratch_buffers(H264Context *h, int linesize)
 {
     int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
@@ -213,74 +306,95 @@
     return 0;
 }
 
-static int alloc_picture(H264Context *h, Picture *pic)
+static int init_table_pools(H264Context *h)
 {
     const int big_mb_num    = h->mb_stride * (h->mb_height + 1) + 1;
     const int mb_array_size = h->mb_stride * h->mb_height;
     const int b4_stride     = h->mb_width * 4 + 1;
     const int b4_array_size = b4_stride * h->mb_height * 4;
+
+    h->qscale_table_pool = av_buffer_pool_init(big_mb_num + h->mb_stride,
+                                               av_buffer_allocz);
+    h->mb_type_pool      = av_buffer_pool_init((big_mb_num + h->mb_stride) *
+                                               sizeof(uint32_t), av_buffer_allocz);
+    h->motion_val_pool = av_buffer_pool_init(2 * (b4_array_size + 4) *
+                                             sizeof(int16_t), av_buffer_allocz);
+    h->ref_index_pool  = av_buffer_pool_init(4 * mb_array_size, av_buffer_allocz);
+
+    if (!h->qscale_table_pool || !h->mb_type_pool || !h->motion_val_pool ||
+        !h->ref_index_pool) {
+        av_buffer_pool_uninit(&h->qscale_table_pool);
+        av_buffer_pool_uninit(&h->mb_type_pool);
+        av_buffer_pool_uninit(&h->motion_val_pool);
+        av_buffer_pool_uninit(&h->ref_index_pool);
+        return AVERROR(ENOMEM);
+    }
+
+    return 0;
+}
+
+static int alloc_picture(H264Context *h, Picture *pic)
+{
     int i, ret = 0;
 
     av_assert0(!pic->f.data[0]);
 
-    if (h->avctx->hwaccel) {
-        const AVHWAccel *hwaccel = h->avctx->hwaccel;
-        av_assert0(!pic->f.hwaccel_picture_private);
-        if (hwaccel->priv_data_size) {
-            pic->f.hwaccel_picture_private = av_mallocz(hwaccel->priv_data_size);
-            if (!pic->f.hwaccel_picture_private)
-                return AVERROR(ENOMEM);
-        }
-    }
-    ret = ff_thread_get_buffer(h->avctx, &pic->f);
+    pic->tf.f = &pic->f;
+    ret = ff_thread_get_buffer(h->avctx, &pic->tf, pic->reference ?
+                                                   AV_GET_BUFFER_FLAG_REF : 0);
     if (ret < 0)
         goto fail;
 
     h->linesize   = pic->f.linesize[0];
     h->uvlinesize = pic->f.linesize[1];
 
-    if (pic->f.qscale_table == NULL) {
-        FF_ALLOCZ_OR_GOTO(h->avctx, pic->qscale_table_base,
-                          (big_mb_num + h->mb_stride) * sizeof(uint8_t),
-                          fail)
-        FF_ALLOCZ_OR_GOTO(h->avctx, pic->mb_type_base,
-                          (big_mb_num + h->mb_stride) * sizeof(uint32_t),
-                          fail)
-        pic->f.mb_type = pic->mb_type_base + 2 * h->mb_stride + 1;
-        pic->f.qscale_table = pic->qscale_table_base + 2 * h->mb_stride + 1;
-
-        for (i = 0; i < 2; i++) {
-            FF_ALLOCZ_OR_GOTO(h->avctx, pic->motion_val_base[i],
-                              2 * (b4_array_size + 4) * sizeof(int16_t),
-                              fail)
-            pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
-            FF_ALLOCZ_OR_GOTO(h->avctx, pic->f.ref_index[i],
-                              4 * mb_array_size * sizeof(uint8_t), fail)
+    if (h->avctx->hwaccel) {
+        const AVHWAccel *hwaccel = h->avctx->hwaccel;
+        av_assert0(!pic->hwaccel_picture_private);
+        if (hwaccel->priv_data_size) {
+            pic->hwaccel_priv_buf = av_buffer_allocz(hwaccel->priv_data_size);
+            if (!pic->hwaccel_priv_buf)
+                return AVERROR(ENOMEM);
+            pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
         }
-        pic->f.motion_subsample_log2 = 2;
-
-        pic->f.qstride = h->mb_stride;
     }
 
-    pic->owner2 = h;
+    if (!h->qscale_table_pool) {
+        ret = init_table_pools(h);
+        if (ret < 0)
+            goto fail;
+    }
+
+    pic->qscale_table_buf = av_buffer_pool_get(h->qscale_table_pool);
+    pic->mb_type_buf      = av_buffer_pool_get(h->mb_type_pool);
+    if (!pic->qscale_table_buf || !pic->mb_type_buf)
+        goto fail;
+
+    pic->mb_type      = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1;
+    pic->qscale_table = pic->qscale_table_buf->data + 2 * h->mb_stride + 1;
+
+    for (i = 0; i < 2; i++) {
+        pic->motion_val_buf[i] = av_buffer_pool_get(h->motion_val_pool);
+        pic->ref_index_buf[i]  = av_buffer_pool_get(h->ref_index_pool);
+        if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
+            goto fail;
+
+        pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
+        pic->ref_index[i]  = pic->ref_index_buf[i]->data;
+    }
 
     return 0;
 fail:
-    free_frame_buffer(h, pic);
+    unref_picture(h, pic);
     return (ret < 0) ? ret : AVERROR(ENOMEM);
 }
 
 static inline int pic_is_unused(H264Context *h, Picture *pic)
 {
-    if (   (h->avctx->active_thread_type & FF_THREAD_FRAME)
-        && pic->f.qscale_table //check if the frame has anything allocated
-        && pic->period_since_free < h->avctx->thread_count)
-        return 0;
     if (pic->f.data[0] == NULL)
         return 1;
-    if (pic->needs_realloc && !(pic->f.reference & DELAYED_PIC_REF))
-        if (!pic->owner2 || pic->owner2 == h)
-            return 1;
+    if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
+        return 1;
     return 0;
 }
 
@@ -288,17 +402,16 @@
 {
     int i;
 
-    for (i = h->picture_range_start; i < h->picture_range_end; i++) {
+    for (i = 0; i < MAX_PICTURE_COUNT; i++) {
         if (pic_is_unused(h, &h->DPB[i]))
             break;
     }
-    if (i == h->picture_range_end)
+    if (i == MAX_PICTURE_COUNT)
         return AVERROR_INVALIDDATA;
 
     if (h->DPB[i].needs_realloc) {
         h->DPB[i].needs_realloc = 0;
-        free_picture(h, &h->DPB[i]);
-        avcodec_get_frame_defaults(&h->DPB[i].f);
+        unref_picture(h, &h->DPB[i]);
     }
 
     return i;
@@ -546,7 +659,7 @@
 {
     int my;
 
-    y_offset += 16 * (h->mb_y >> MB_FIELD);
+    y_offset += 16 * (h->mb_y >> MB_FIELD(h));
 
     if (list0) {
         int ref_n    = h->ref_cache[0][scan8[n]];
@@ -555,8 +668,8 @@
         // Error resilience puts the current picture in the ref list.
         // Don't try to wait on these as it will cause a deadlock.
         // Fields can wait on each other, though.
-        if (ref->f.thread_opaque   != h->cur_pic.f.thread_opaque ||
-            (ref->f.reference & 3) != h->picture_structure) {
+        if (ref->tf.progress->data   != h->cur_pic.tf.progress->data ||
+            (ref->reference & 3) != h->picture_structure) {
             my = get_lowest_part_list_y(h, ref, n, height, y_offset, 0);
             if (refs[0][ref_n] < 0)
                 nrefs[0] += 1;
@@ -568,8 +681,8 @@
         int ref_n    = h->ref_cache[1][scan8[n]];
         Picture *ref = &h->ref_list[1][ref_n];
 
-        if (ref->f.thread_opaque   != h->cur_pic.f.thread_opaque ||
-            (ref->f.reference & 3) != h->picture_structure) {
+        if (ref->tf.progress->data != h->cur_pic.tf.progress->data ||
+            (ref->reference & 3) != h->picture_structure) {
             my = get_lowest_part_list_y(h, ref, n, height, y_offset, 1);
             if (refs[1][ref_n] < 0)
                 nrefs[1] += 1;
@@ -586,7 +699,7 @@
 static void await_references(H264Context *h)
 {
     const int mb_xy   = h->mb_xy;
-    const int mb_type = h->cur_pic.f.mb_type[mb_xy];
+    const int mb_type = h->cur_pic.mb_type[mb_xy];
     int refs[2][48];
     int nrefs[2] = { 0 };
     int ref, list;
@@ -658,32 +771,32 @@
             int row = refs[list][ref];
             if (row >= 0) {
                 Picture *ref_pic      = &h->ref_list[list][ref];
-                int ref_field         = ref_pic->f.reference - 1;
+                int ref_field         = ref_pic->reference - 1;
                 int ref_field_picture = ref_pic->field_picture;
                 int pic_height        = 16 * h->mb_height >> ref_field_picture;
 
-                row <<= MB_MBAFF;
+                row <<= MB_MBAFF(h);
                 nrefs[list]--;
 
-                if (!FIELD_PICTURE && ref_field_picture) { // frame referencing two fields
-                    ff_thread_await_progress(&ref_pic->f,
+                if (!FIELD_PICTURE(h) && ref_field_picture) { // frame referencing two fields
+                    ff_thread_await_progress(&ref_pic->tf,
                                              FFMIN((row >> 1) - !(row & 1),
                                                    pic_height - 1),
                                              1);
-                    ff_thread_await_progress(&ref_pic->f,
+                    ff_thread_await_progress(&ref_pic->tf,
                                              FFMIN((row >> 1), pic_height - 1),
                                              0);
-                } else if (FIELD_PICTURE && !ref_field_picture) { // field referencing one field of a frame
-                    ff_thread_await_progress(&ref_pic->f,
+                } else if (FIELD_PICTURE(h) && !ref_field_picture) { // field referencing one field of a frame
+                    ff_thread_await_progress(&ref_pic->tf,
                                              FFMIN(row * 2 + ref_field,
                                                    pic_height - 1),
                                              0);
-                } else if (FIELD_PICTURE) {
-                    ff_thread_await_progress(&ref_pic->f,
+                } else if (FIELD_PICTURE(h)) {
+                    ff_thread_await_progress(&ref_pic->tf,
                                              FFMIN(row, pic_height - 1),
                                              ref_field);
                 } else {
-                    ff_thread_await_progress(&ref_pic->f,
+                    ff_thread_await_progress(&ref_pic->tf,
                                              FFMIN(row, pic_height - 1),
                                              0);
                 }
@@ -713,7 +826,7 @@
     const int full_mx    = mx >> 2;
     const int full_my    = my >> 2;
     const int pic_width  = 16 * h->mb_width;
-    const int pic_height = 16 * h->mb_height >> MB_FIELD;
+    const int pic_height = 16 * h->mb_height >> MB_FIELD(h);
     int ysh;
 
     if (mx & 7)
@@ -773,9 +886,9 @@
     }
 
     ysh = 3 - (chroma_idc == 2 /* yuv422 */);
-    if (chroma_idc == 1 /* yuv420 */ && MB_FIELD) {
+    if (chroma_idc == 1 /* yuv420 */ && MB_FIELD(h)) {
         // chroma offset when predicting from a field of opposite parity
-        my  += 2 * ((h->mb_y & 1) - (pic->f.reference - 1));
+        my  += 2 * ((h->mb_y & 1) - (pic->reference - 1));
         emu |= (my >> 3) < 0 || (my >> 3) + 8 >= (pic_height >> 1);
     }
 
@@ -831,7 +944,7 @@
         dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
     }
     x_offset += 8 * h->mb_x;
-    y_offset += 8 * (h->mb_y >> MB_FIELD);
+    y_offset += 8 * (h->mb_y >> MB_FIELD(h));
 
     if (list0) {
         Picture *ref = &h->ref_list[0][h->ref_cache[0][scan8[n]]];
@@ -884,7 +997,7 @@
         dest_cr      += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
     }
     x_offset += 8 * h->mb_x;
-    y_offset += 8 * (h->mb_y >> MB_FIELD);
+    y_offset += 8 * (h->mb_y >> MB_FIELD(h));
 
     if (list0 && list1) {
         /* don't optimize for luma-only case, since B-frames usually
@@ -1004,13 +1117,17 @@
     for (i = 0; i < 3; i++)
         av_freep(&h->visualization_buffer[i]);
 
-    if (free_rbsp) {
-        for (i = 0; i < h->picture_count && !h->avctx->internal->is_copy; i++)
-            free_picture(h, &h->DPB[i]);
+    av_buffer_pool_uninit(&h->qscale_table_pool);
+    av_buffer_pool_uninit(&h->mb_type_pool);
+    av_buffer_pool_uninit(&h->motion_val_pool);
+    av_buffer_pool_uninit(&h->ref_index_pool);
+
+    if (free_rbsp && h->DPB) {
+        for (i = 0; i < MAX_PICTURE_COUNT; i++)
+            unref_picture(h, &h->DPB[i]);
         av_freep(&h->DPB);
-        h->picture_count = 0;
     } else if (h->DPB) {
-        for (i = 0; i < h->picture_count; i++)
+        for (i = 0; i < MAX_PICTURE_COUNT; i++)
             h->DPB[i].needs_realloc = 1;
     }
 
@@ -1159,11 +1276,10 @@
         init_dequant_tables(h);
 
     if (!h->DPB) {
-        h->picture_count = MAX_PICTURE_COUNT * FFMAX(1, h->avctx->thread_count);
-        h->DPB = av_mallocz_array(h->picture_count, sizeof(*h->DPB));
+        h->DPB = av_mallocz_array(MAX_PICTURE_COUNT, sizeof(*h->DPB));
         if (!h->DPB)
             return AVERROR(ENOMEM);
-        for (i = 0; i < h->picture_count; i++)
+        for (i = 0; i < MAX_PICTURE_COUNT; i++)
             avcodec_get_frame_defaults(&h->DPB[i].f);
         avcodec_get_frame_defaults(&h->cur_pic.f);
     }
@@ -1226,45 +1342,47 @@
     h->ref_cache[1][scan8[7]  + 1] =
     h->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
 
-    /* init ER */
-    er->avctx          = h->avctx;
-    er->dsp            = &h->dsp;
-    er->decode_mb      = h264_er_decode_mb;
-    er->opaque         = h;
-    er->quarter_sample = 1;
+    if (CONFIG_ERROR_RESILIENCE) {
+        /* init ER */
+        er->avctx          = h->avctx;
+        er->dsp            = &h->dsp;
+        er->decode_mb      = h264_er_decode_mb;
+        er->opaque         = h;
+        er->quarter_sample = 1;
 
-    er->mb_num      = h->mb_num;
-    er->mb_width    = h->mb_width;
-    er->mb_height   = h->mb_height;
-    er->mb_stride   = h->mb_stride;
-    er->b8_stride   = h->mb_width * 2 + 1;
+        er->mb_num      = h->mb_num;
+        er->mb_width    = h->mb_width;
+        er->mb_height   = h->mb_height;
+        er->mb_stride   = h->mb_stride;
+        er->b8_stride   = h->mb_width * 2 + 1;
 
-    FF_ALLOCZ_OR_GOTO(h->avctx, er->mb_index2xy, (h->mb_num + 1) * sizeof(int),
-                      fail); // error ressilience code looks cleaner with this
-    for (y = 0; y < h->mb_height; y++)
-        for (x = 0; x < h->mb_width; x++)
-            er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
+        FF_ALLOCZ_OR_GOTO(h->avctx, er->mb_index2xy, (h->mb_num + 1) * sizeof(int),
+                          fail); // error ressilience code looks cleaner with this
+        for (y = 0; y < h->mb_height; y++)
+            for (x = 0; x < h->mb_width; x++)
+                er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
 
-    er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
-                                                   h->mb_stride + h->mb_width;
+        er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
+                                                       h->mb_stride + h->mb_width;
 
-    FF_ALLOCZ_OR_GOTO(h->avctx, er->error_status_table,
-                      mb_array_size * sizeof(uint8_t), fail);
+        FF_ALLOCZ_OR_GOTO(h->avctx, er->error_status_table,
+                          mb_array_size * sizeof(uint8_t), fail);
 
-    FF_ALLOC_OR_GOTO(h->avctx, er->mbintra_table, mb_array_size, fail);
-    memset(er->mbintra_table, 1, mb_array_size);
+        FF_ALLOC_OR_GOTO(h->avctx, er->mbintra_table, mb_array_size, fail);
+        memset(er->mbintra_table, 1, mb_array_size);
 
-    FF_ALLOCZ_OR_GOTO(h->avctx, er->mbskip_table, mb_array_size + 2, fail);
+        FF_ALLOCZ_OR_GOTO(h->avctx, er->mbskip_table, mb_array_size + 2, fail);
 
-    FF_ALLOC_OR_GOTO(h->avctx, er->er_temp_buffer, h->mb_height * h->mb_stride,
-                     fail);
+        FF_ALLOC_OR_GOTO(h->avctx, er->er_temp_buffer, h->mb_height * h->mb_stride,
+                         fail);
 
-    FF_ALLOCZ_OR_GOTO(h->avctx, h->dc_val_base, yc_size * sizeof(int16_t), fail);
-    er->dc_val[0] = h->dc_val_base + h->mb_width * 2 + 2;
-    er->dc_val[1] = h->dc_val_base + y_size + h->mb_stride + 1;
-    er->dc_val[2] = er->dc_val[1] + c_size;
-    for (i = 0; i < yc_size; i++)
-        h->dc_val_base[i] = 1024;
+        FF_ALLOCZ_OR_GOTO(h->avctx, h->dc_val_base, yc_size * sizeof(int16_t), fail);
+        er->dc_val[0] = h->dc_val_base + h->mb_width * 2 + 2;
+        er->dc_val[1] = h->dc_val_base + y_size + h->mb_stride + 1;
+        er->dc_val[2] = er->dc_val[1] + c_size;
+        for (i = 0; i < yc_size; i++)
+            h->dc_val_base[i] = 1024;
+    }
 
     return 0;
 
@@ -1275,35 +1393,6 @@
 static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size,
                             int parse_extradata);
 
-static av_cold void common_init(H264Context *h)
-{
-
-    h->width    = h->avctx->width;
-    h->height   = h->avctx->height;
-
-    h->bit_depth_luma    = 8;
-    h->chroma_format_idc = 1;
-
-    h->avctx->bits_per_raw_sample = 8;
-    h->cur_chroma_format_idc = 1;
-
-    ff_h264dsp_init(&h->h264dsp, 8, 1);
-    av_assert0(h->sps.bit_depth_chroma == 0);
-    ff_h264chroma_init(&h->h264chroma, h->sps.bit_depth_chroma);
-    ff_h264qpel_init(&h->h264qpel, 8);
-    ff_h264_pred_init(&h->hpc, h->avctx->codec_id, 8, 1);
-
-    h->dequant_coeff_pps = -1;
-
-    h->dsp.dct_bits = 16;
-    /* needed so that IDCT permutation is known early */
-    ff_dsputil_init(&h->dsp, h->avctx);
-    ff_videodsp_init(&h->vdsp, 8);
-
-    memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t));
-    memset(h->pps.scaling_matrix8, 16, 2 * 64 * sizeof(uint8_t));
-}
-
 int ff_h264_decode_extradata(H264Context *h, const uint8_t *buf, int size)
 {
     AVCodecContext *avctx = h->avctx;
@@ -1367,11 +1456,30 @@
     int i;
 
     h->avctx = avctx;
-    common_init(h);
+
+    h->bit_depth_luma    = 8;
+    h->chroma_format_idc = 1;
+
+    h->avctx->bits_per_raw_sample = 8;
+    h->cur_chroma_format_idc = 1;
+
+    ff_h264dsp_init(&h->h264dsp, 8, 1);
+    av_assert0(h->sps.bit_depth_chroma == 0);
+    ff_h264chroma_init(&h->h264chroma, h->sps.bit_depth_chroma);
+    ff_h264qpel_init(&h->h264qpel, 8);
+    ff_h264_pred_init(&h->hpc, h->avctx->codec_id, 8, 1);
+
+    h->dequant_coeff_pps = -1;
+
+    /* needed so that IDCT permutation is known early */
+    if (CONFIG_ERROR_RESILIENCE)
+        ff_dsputil_init(&h->dsp, h->avctx);
+    ff_videodsp_init(&h->vdsp, 8);
+
+    memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t));
+    memset(h->pps.scaling_matrix8, 16, 2 * 64 * sizeof(uint8_t));
 
     h->picture_structure   = PICT_FRAME;
-    h->picture_range_start = 0;
-    h->picture_range_end   = MAX_PICTURE_COUNT;
     h->slice_context_count = 1;
     h->workaround_bugs     = avctx->workaround_bugs;
     h->flags               = avctx->flags;
@@ -1419,6 +1527,7 @@
     }
 
     ff_init_cabac_states();
+    avctx->internal->allocate_progress = 1;
 
     return 0;
 }
@@ -1427,7 +1536,7 @@
 #undef REBASE_PICTURE
 #define REBASE_PICTURE(pic, new_ctx, old_ctx)             \
     ((pic && pic >= old_ctx->DPB &&                       \
-      pic < old_ctx->DPB + old_ctx->picture_count) ?      \
+      pic < old_ctx->DPB + MAX_PICTURE_COUNT) ?      \
         &new_ctx->DPB[pic - old_ctx->DPB] : NULL)
 
 static void copy_picture_range(Picture **to, Picture **from, int count,
@@ -1439,7 +1548,7 @@
     for (i = 0; i < count; i++) {
         assert((IN_RANGE(from[i], old_base, sizeof(*old_base)) ||
                 IN_RANGE(from[i], old_base->DPB,
-                         sizeof(Picture) * old_base->picture_count) ||
+                         sizeof(Picture) * MAX_PICTURE_COUNT) ||
                 !from[i]));
         to[i] = REBASE_PICTURE(from[i], new_base, old_base);
     }
@@ -1488,7 +1597,7 @@
     H264Context *h = dst->priv_data, *h1 = src->priv_data;
     int inited = h->context_initialized, err = 0;
     int context_reinitialized = 0;
-    int i;
+    int i, ret;
 
     if (dst == src)
         return 0;
@@ -1502,6 +1611,11 @@
          h->sps.chroma_format_idc != h1->sps.chroma_format_idc ||
          h->sps.colorspace        != h1->sps.colorspace)) {
 
+        /* set bits_per_raw_sample to the previous value. the check for changed
+         * bit depth in h264_set_parameter_from_sps() uses it and sets it to
+         * the current value */
+        h->avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
+
         av_freep(&h->bipred_scratchpad);
 
         h->width     = h1->width;
@@ -1556,16 +1670,23 @@
 
         memset(&h->er, 0, sizeof(h->er));
         memset(&h->me, 0, sizeof(h->me));
+        memset(&h->mb, 0, sizeof(h->mb));
+        memset(&h->mb_luma_dc, 0, sizeof(h->mb_luma_dc));
+        memset(&h->mb_padding, 0, sizeof(h->mb_padding));
+
         h->avctx = dst;
         h->DPB   = NULL;
+        h->qscale_table_pool = NULL;
+        h->mb_type_pool = NULL;
+        h->ref_index_pool = NULL;
+        h->motion_val_pool = NULL;
 
         if (h1->context_initialized) {
         h->context_initialized = 0;
 
-        h->picture_range_start  += MAX_PICTURE_COUNT;
-        h->picture_range_end    += MAX_PICTURE_COUNT;
-
-        h->cur_pic.f.extended_data = h->cur_pic.f.data;
+        memset(&h->cur_pic, 0, sizeof(h->cur_pic));
+        avcodec_get_frame_defaults(&h->cur_pic.f);
+        h->cur_pic.tf.f = &h->cur_pic.f;
 
         if (ff_h264_alloc_tables(h) < 0) {
             av_log(dst, AV_LOG_ERROR, "Could not allocate memory for h264\n");
@@ -1597,17 +1718,17 @@
     h->data_partitioning    = h1->data_partitioning;
     h->low_delay            = h1->low_delay;
 
-    memcpy(h->DPB, h1->DPB, h1->picture_count * sizeof(*h1->DPB));
-
-    // reset s->picture[].f.extended_data to s->picture[].f.data
-    for (i = 0; i < h->picture_count; i++) {
-        h->DPB[i].f.extended_data = h->DPB[i].f.data;
-        h->DPB[i].period_since_free ++;
+    for (i = 0; h->DPB && i < MAX_PICTURE_COUNT; i++) {
+        unref_picture(h, &h->DPB[i]);
+        if (h1->DPB[i].f.data[0] &&
+            (ret = ref_picture(h, &h->DPB[i], &h1->DPB[i])) < 0)
+            return ret;
     }
 
     h->cur_pic_ptr     = REBASE_PICTURE(h1->cur_pic_ptr, h, h1);
-    h->cur_pic = h1->cur_pic;
-    h->cur_pic.f.extended_data = h->cur_pic.f.data;
+    unref_picture(h, &h->cur_pic);
+    if (h1->cur_pic.f.buf[0] && (ret = ref_picture(h, &h->cur_pic, &h1->cur_pic)) < 0)
+        return ret;
 
     h->workaround_bugs = h1->workaround_bugs;
     h->low_delay       = h1->low_delay;
@@ -1642,8 +1763,6 @@
     copy_fields(h, h1, poc_lsb, redundant_pic_count);
 
     // reference lists
-    copy_fields(h, h1, ref_count, list_count);
-    copy_fields(h, h1, ref2frm, intra_gb);
     copy_fields(h, h1, short_ref, cabac_init_idc);
 
     copy_picture_range(h->short_ref, h1->short_ref, 32, h, h1);
@@ -1651,9 +1770,7 @@
     copy_picture_range(h->delayed_pic, h1->delayed_pic,
                        MAX_DELAYED_PIC_COUNT + 2, h, h1);
 
-    h->last_slice_type = h1->last_slice_type;
     h->sync            = h1->sync;
-    memcpy(h->last_ref_count, h1->last_ref_count, sizeof(h->last_ref_count));
 
     if (context_reinitialized)
         h264_set_parameter_from_sps(h);
@@ -1673,7 +1790,7 @@
     return err;
 }
 
-int ff_h264_frame_start(H264Context *h)
+static int h264_frame_start(H264Context *h)
 {
     Picture *pic;
     int i, ret;
@@ -1700,7 +1817,7 @@
     }
     pic = &h->DPB[i];
 
-    pic->f.reference            = h->droppable ? 0 : h->picture_structure;
+    pic->reference            = h->droppable ? 0 : h->picture_structure;
     pic->f.coded_picture_number = h->coded_picture_number++;
     pic->field_picture          = h->picture_structure != PICT_FRAME;
 
@@ -1720,12 +1837,15 @@
         avpriv_color_frame(&pic->f, c);
 
     h->cur_pic_ptr = pic;
-    h->cur_pic     = *h->cur_pic_ptr;
-    h->cur_pic.f.extended_data = h->cur_pic.f.data;
+    unref_picture(h, &h->cur_pic);
+    if ((ret = ref_picture(h, &h->cur_pic, h->cur_pic_ptr)) < 0)
+        return ret;
 
-    ff_er_frame_start(&h->er);
-    h->er.last_pic =
-    h->er.next_pic = NULL;
+    if (CONFIG_ERROR_RESILIENCE) {
+        ff_er_frame_start(&h->er);
+        h->er.last_pic =
+        h->er.next_pic = NULL;
+    }
 
     assert(h->linesize && h->uvlinesize);
 
@@ -1740,22 +1860,14 @@
         h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * h->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
     }
 
-    /* Some macroblocks can be accessed before they're available in case
-     * of lost slices, MBAFF or threading. */
-    memset(h->slice_table, -1,
-           (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
-
     // s->decode = (h->flags & CODEC_FLAG_PSNR) || !s->encoding ||
-    //             h->cur_pic.f.reference /* || h->contains_intra */ || 1;
+    //             h->cur_pic.reference /* || h->contains_intra */ || 1;
 
     /* We mark the current picture as non-reference after allocating it, so
      * that if we break out due to an error it can be released automatically
      * in the next ff_MPV_frame_start().
-     * SVQ3 as well as most other codecs have only last/next/current and thus
-     * get released even with set reference, besides SVQ3 and others do not
-     * mark frames as reference later "naturally". */
-    if (h->avctx->codec_id != AV_CODEC_ID_SVQ3)
-        h->cur_pic_ptr->f.reference = 0;
+     */
+    h->cur_pic_ptr->reference = 0;
 
     h->cur_pic_ptr->field_poc[0] = h->cur_pic_ptr->field_poc[1] = INT_MAX;
 
@@ -1780,7 +1892,6 @@
     Picture *cur = h->cur_pic_ptr;
     int i, pics, out_of_order, out_idx;
 
-    h->cur_pic_ptr->f.qscale_type = FF_QSCALE_TYPE_H264;
     h->cur_pic_ptr->f.pict_type   = h->pict_type;
 
     if (h->next_output_pic)
@@ -1813,7 +1924,7 @@
             break;
         case SEI_PIC_STRUCT_TOP_BOTTOM:
         case SEI_PIC_STRUCT_BOTTOM_TOP:
-            if (FIELD_OR_MBAFF_PICTURE)
+            if (FIELD_OR_MBAFF_PICTURE(h))
                 cur->f.interlaced_frame = 1;
             else
                 // try to flag soft telecine progressive
@@ -1839,7 +1950,7 @@
             cur->f.interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0;
     } else {
         /* Derive interlacing flag from used decoding process. */
-        cur->f.interlaced_frame = FIELD_OR_MBAFF_PICTURE;
+        cur->f.interlaced_frame = FIELD_OR_MBAFF_PICTURE(h);
     }
     h->prev_interlaced_frame = cur->f.interlaced_frame;
 
@@ -1911,8 +2022,8 @@
     av_assert0(pics <= MAX_DELAYED_PIC_COUNT);
 
     h->delayed_pic[pics++] = cur;
-    if (cur->f.reference == 0)
-        cur->f.reference = DELAYED_PIC_REF;
+    if (cur->reference == 0)
+        cur->reference = DELAYED_PIC_REF;
 
     out = h->delayed_pic[0];
     out_idx = 0;
@@ -1930,10 +2041,9 @@
     out_of_order = out->poc < h->next_outputed_poc;
 
     if (out_of_order || pics > h->avctx->has_b_frames) {
-        out->f.reference &= ~DELAYED_PIC_REF;
+        out->reference &= ~DELAYED_PIC_REF;
         // for frame threading, the owner must be the second field's thread or
         // else the first thread can release the picture and reuse it unsafely
-        out->owner2       = h;
         for (i = out_idx; h->delayed_pic[i]; i++)
             h->delayed_pic[i] = h->delayed_pic[i + 1];
     }
@@ -1951,7 +2061,7 @@
         h->sync |= 2;
     }
 
-    if (setup_finished)
+    if (setup_finished && !h->avctx->hwaccel)
         ff_thread_finish_setup(h->avctx);
 }
 
@@ -1963,16 +2073,16 @@
     uint8_t *top_border;
     int top_idx = 1;
     const int pixel_shift = h->pixel_shift;
-    int chroma444 = CHROMA444;
-    int chroma422 = CHROMA422;
+    int chroma444 = CHROMA444(h);
+    int chroma422 = CHROMA422(h);
 
     src_y  -= linesize;
     src_cb -= uvlinesize;
     src_cr -= uvlinesize;
 
-    if (!simple && FRAME_MBAFF) {
+    if (!simple && FRAME_MBAFF(h)) {
         if (h->mb_y & 1) {
-            if (!MB_MBAFF) {
+            if (!MB_MBAFF(h)) {
                 top_border = h->top_borders[0][h->mb_x];
                 AV_COPY128(top_border, src_y + 15 * linesize);
                 if (pixel_shift)
@@ -2007,7 +2117,7 @@
                     }
                 }
             }
-        } else if (MB_MBAFF) {
+        } else if (MB_MBAFF(h)) {
             top_idx = 0;
         } else
             return;
@@ -2063,12 +2173,12 @@
     uint8_t *top_border_m1;
     uint8_t *top_border;
 
-    if (!simple && FRAME_MBAFF) {
+    if (!simple && FRAME_MBAFF(h)) {
         if (h->mb_y & 1) {
-            if (!MB_MBAFF)
+            if (!MB_MBAFF(h))
                 return;
         } else {
-            top_idx = MB_MBAFF ? 0 : 1;
+            top_idx = MB_MBAFF(h) ? 0 : 1;
         }
     }
 
@@ -2077,7 +2187,7 @@
         deblock_top     = h->top_type;
     } else {
         deblock_topleft = (h->mb_x > 0);
-        deblock_top     = (h->mb_y > !!MB_FIELD);
+        deblock_top     = (h->mb_y > !!MB_FIELD(h));
     }
 
     src_y  -= linesize   + 1 + pixel_shift;
@@ -2357,10 +2467,10 @@
 void ff_h264_hl_decode_mb(H264Context *h)
 {
     const int mb_xy   = h->mb_xy;
-    const int mb_type = h->cur_pic.f.mb_type[mb_xy];
+    const int mb_type = h->cur_pic.mb_type[mb_xy];
     int is_complex    = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || h->qscale == 0;
 
-    if (CHROMA444) {
+    if (CHROMA444(h)) {
         if (is_complex || h->pixel_shift)
             hl_decode_mb_444_complex(h);
         else
@@ -2455,7 +2565,7 @@
         } else {
             cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure - 1];
         }
-        if (h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF &&
+        if (h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF(h) &&
             h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2 * cur_poc) {
             h->use_weight = 0;
             h->use_weight_chroma = 0;
@@ -2524,9 +2634,10 @@
     h->outputed_poc = h->next_outputed_poc = INT_MIN;
     h->prev_interlaced_frame = 1;
     idr(h);
+
     h->prev_frame_num = -1;
     if (h->cur_pic_ptr) {
-        h->cur_pic_ptr->f.reference = 0;
+        h->cur_pic_ptr->reference = 0;
         for (j=i=0; h->delayed_pic[i]; i++)
             if (h->delayed_pic[i] != h->cur_pic_ptr)
                 h->delayed_pic[j++] = h->delayed_pic[i];
@@ -2552,17 +2663,17 @@
 
     for (i = 0; i <= MAX_DELAYED_PIC_COUNT; i++) {
         if (h->delayed_pic[i])
-            h->delayed_pic[i]->f.reference = 0;
+            h->delayed_pic[i]->reference = 0;
         h->delayed_pic[i] = NULL;
     }
 
     flush_change(h);
 
-    for (i = 0; i < h->picture_count; i++) {
-        if (h->DPB[i].f.data[0])
-            free_frame_buffer(h, &h->DPB[i]);
-    }
+    if (h->DPB)
+        for (i = 0; i < MAX_PICTURE_COUNT; i++)
+            unref_picture(h, &h->DPB[i]);
     h->cur_pic_ptr = NULL;
+    unref_picture(h, &h->cur_pic);
 
     h->mb_x = h->mb_y = 0;
 
@@ -2694,10 +2805,6 @@
     int err = 0;
     h->mb_y = 0;
 
-    if (!in_setup && !h->droppable)
-        ff_thread_report_progress(&h->cur_pic_ptr->f, INT_MAX,
-                                  h->picture_structure == PICT_BOTTOM_FIELD);
-
     if (CONFIG_H264_VDPAU_DECODER &&
         h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
         ff_vdpau_h264_set_reference_frames(h);
@@ -2735,10 +2842,14 @@
      * past end by one (callers fault) and resync_mb_y != 0
      * causes problems for the first MB line, too.
      */
-    if (!FIELD_PICTURE && h->current_slice && !h->sps.new) {
+    if (CONFIG_ERROR_RESILIENCE &&
+        !FIELD_PICTURE(h) && h->current_slice && !h->sps.new) {
         h->er.cur_pic  = h->cur_pic_ptr;
         ff_er_frame_end(&h->er);
     }
+    if (!in_setup && !h->droppable)
+        ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
+                                  h->picture_structure == PICT_BOTTOM_FIELD);
     emms_c();
 
     h->current_slice = 0;
@@ -2816,6 +2927,12 @@
     if (h->avctx->has_b_frames < 2)
         h->avctx->has_b_frames = !h->low_delay;
 
+    if (h->sps.bit_depth_luma != h->sps.bit_depth_chroma) {
+        avpriv_request_sample(h->avctx,
+                              "Different chroma and luma bit depth");
+        return AVERROR_PATCHWELCOME;
+    }
+
     if (h->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
         h->cur_chroma_format_idc      != h->sps.chroma_format_idc) {
         if (h->avctx->codec &&
@@ -2826,8 +2943,7 @@
             return AVERROR_INVALIDDATA;
         }
         if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 14 &&
-            h->sps.bit_depth_luma != 11 && h->sps.bit_depth_luma != 13 &&
-                (h->sps.bit_depth_luma != 9 || !CHROMA422)) {
+            h->sps.bit_depth_luma != 11 && h->sps.bit_depth_luma != 13) {
             h->avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
             h->cur_chroma_format_idc      = h->sps.chroma_format_idc;
             h->pixel_shift                = h->sps.bit_depth_luma > 8;
@@ -2838,8 +2954,9 @@
             ff_h264qpel_init(&h->h264qpel, h->sps.bit_depth_luma);
             ff_h264_pred_init(&h->hpc, h->avctx->codec_id, h->sps.bit_depth_luma,
                               h->sps.chroma_format_idc);
-            h->dsp.dct_bits = h->sps.bit_depth_luma > 8 ? 32 : 16;
-            ff_dsputil_init(&h->dsp, h->avctx);
+
+            if (CONFIG_ERROR_RESILIENCE)
+                ff_dsputil_init(&h->dsp, h->avctx);
             ff_videodsp_init(&h->vdsp, h->sps.bit_depth_luma);
         } else {
             av_log(h->avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n",
@@ -2850,55 +2967,55 @@
     return 0;
 }
 
-static enum PixelFormat get_pixel_format(H264Context *h, int force_callback)
+static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
 {
     switch (h->sps.bit_depth_luma) {
     case 9:
-        if (CHROMA444) {
+        if (CHROMA444(h)) {
             if (h->avctx->colorspace == AVCOL_SPC_RGB) {
                 return AV_PIX_FMT_GBRP9;
             } else
                 return AV_PIX_FMT_YUV444P9;
-        } else if (CHROMA422)
+        } else if (CHROMA422(h))
             return AV_PIX_FMT_YUV422P9;
         else
             return AV_PIX_FMT_YUV420P9;
         break;
     case 10:
-        if (CHROMA444) {
+        if (CHROMA444(h)) {
             if (h->avctx->colorspace == AVCOL_SPC_RGB) {
                 return AV_PIX_FMT_GBRP10;
             } else
                 return AV_PIX_FMT_YUV444P10;
-        } else if (CHROMA422)
+        } else if (CHROMA422(h))
             return AV_PIX_FMT_YUV422P10;
         else
             return AV_PIX_FMT_YUV420P10;
         break;
     case 12:
-        if (CHROMA444) {
+        if (CHROMA444(h)) {
             if (h->avctx->colorspace == AVCOL_SPC_RGB) {
                 return AV_PIX_FMT_GBRP12;
             } else
                 return AV_PIX_FMT_YUV444P12;
-        } else if (CHROMA422)
+        } else if (CHROMA422(h))
             return AV_PIX_FMT_YUV422P12;
         else
             return AV_PIX_FMT_YUV420P12;
         break;
     case 14:
-        if (CHROMA444) {
+        if (CHROMA444(h)) {
             if (h->avctx->colorspace == AVCOL_SPC_RGB) {
                 return AV_PIX_FMT_GBRP14;
             } else
                 return AV_PIX_FMT_YUV444P14;
-        } else if (CHROMA422)
+        } else if (CHROMA422(h))
             return AV_PIX_FMT_YUV422P14;
         else
             return AV_PIX_FMT_YUV420P14;
         break;
     case 8:
-        if (CHROMA444) {
+        if (CHROMA444(h)) {
             if (h->avctx->colorspace == AVCOL_SPC_RGB) {
                 av_log(h->avctx, AV_LOG_DEBUG, "Detected GBR colorspace.\n");
                 return AV_PIX_FMT_GBR24P;
@@ -2907,7 +3024,7 @@
             }
             return h->avctx->color_range == AVCOL_RANGE_JPEG ? AV_PIX_FMT_YUVJ444P
                                                                 : AV_PIX_FMT_YUV444P;
-        } else if (CHROMA422) {
+        } else if (CHROMA422(h)) {
             return h->avctx->color_range == AVCOL_RANGE_JPEG ? AV_PIX_FMT_YUVJ422P
                                                              : AV_PIX_FMT_YUV422P;
         } else {
@@ -2915,13 +3032,13 @@
             const enum AVPixelFormat * fmt = h->avctx->codec->pix_fmts ?
                                         h->avctx->codec->pix_fmts :
                                         h->avctx->color_range == AVCOL_RANGE_JPEG ?
-                                        hwaccel_pixfmt_list_h264_jpeg_420 :
-                                        hwaccel_pixfmt_list_h264_420;
+                                        h264_hwaccel_pixfmt_list_jpeg_420 :
+                                        h264_hwaccel_pixfmt_list_420;
 
             for (i=0; fmt[i] != AV_PIX_FMT_NONE; i++)
                 if (fmt[i] == h->avctx->pix_fmt && !force_callback)
                     return fmt[i];
-            return h->avctx->get_format(h->avctx, fmt);
+            return ff_thread_get_format(h->avctx, fmt);
         }
         break;
     default:
@@ -2931,6 +3048,42 @@
     }
 }
 
+/* export coded and cropped frame dimensions to AVCodecContext */
+static int init_dimensions(H264Context *h)
+{
+    int width  = h->width  - (h->sps.crop_right + h->sps.crop_left);
+    int height = h->height - (h->sps.crop_top   + h->sps.crop_bottom);
+
+    /* handle container cropping */
+    if (!h->sps.crop &&
+        FFALIGN(h->avctx->width,  16) == h->width &&
+        FFALIGN(h->avctx->height, 16) == h->height) {
+        width  = h->avctx->width;
+        height = h->avctx->height;
+    }
+
+    if (width <= 0 || height <= 0) {
+        av_log(h->avctx, AV_LOG_ERROR, "Invalid cropped dimensions: %dx%d.\n",
+               width, height);
+        if (h->avctx->err_recognition & AV_EF_EXPLODE)
+            return AVERROR_INVALIDDATA;
+
+        av_log(h->avctx, AV_LOG_WARNING, "Ignoring cropping information.\n");
+        h->sps.crop_bottom = h->sps.crop_top = h->sps.crop_right = h->sps.crop_left = 0;
+        h->sps.crop = 0;
+
+        width  = h->width;
+        height = h->height;
+    }
+
+    h->avctx->coded_width  = h->width;
+    h->avctx->coded_height = h->height;
+    h->avctx->width        = width;
+    h->avctx->height       = height;
+
+    return 0;
+}
+
 static int h264_slice_header_init(H264Context *h, int reinit)
 {
     int nb_slices = (HAVE_THREADS &&
@@ -2938,20 +3091,6 @@
                     h->avctx->thread_count : 1;
     int i;
 
-    if(    FFALIGN(h->avctx->width , 16                                 ) == h->width
-        && FFALIGN(h->avctx->height, 16*(2 - h->sps.frame_mbs_only_flag)) == h->height
-        && !h->sps.crop_right && !h->sps.crop_bottom
-        && (h->avctx->width != h->width || h->avctx->height && h->height)
-    ) {
-        av_log(h->avctx, AV_LOG_DEBUG, "Using externally provided dimensions\n");
-        h->avctx->coded_width  = h->width;
-        h->avctx->coded_height = h->height;
-    } else{
-        avcodec_set_dimensions(h->avctx, h->width, h->height);
-        h->avctx->width  -= (2>>CHROMA444)*FFMIN(h->sps.crop_right, (8<<CHROMA444)-1);
-        h->avctx->height -= (1<<h->chroma_y_shift)*FFMIN(h->sps.crop_bottom, (16>>h->chroma_y_shift)-1) * (2 - h->sps.frame_mbs_only_flag);
-    }
-
     h->avctx->sample_aspect_ratio = h->sps.sar;
     av_assert0(h->avctx->sample_aspect_ratio.den);
     av_pix_fmt_get_chroma_sub_sample(h->avctx->pix_fmt,
@@ -3001,7 +3140,9 @@
             H264Context *c;
             c = h->thread_context[i] = av_mallocz(sizeof(H264Context));
             c->avctx       = h->avctx;
-            c->dsp         = h->dsp;
+            if (CONFIG_ERROR_RESILIENCE) {
+                c->dsp         = h->dsp;
+            }
             c->vdsp        = h->vdsp;
             c->h264dsp     = h->h264dsp;
             c->h264qpel    = h->h264qpel;
@@ -3061,7 +3202,6 @@
     unsigned int pps_id;
     int num_ref_idx_active_override_flag, ret;
     unsigned int slice_type, tmp, i, j;
-    int default_ref_list_done = 0;
     int last_pic_structure, last_pic_droppable;
     int must_reinit;
     int needs_reinit = 0;
@@ -3072,15 +3212,14 @@
     first_mb_in_slice = get_ue_golomb_long(&h->gb);
 
     if (first_mb_in_slice == 0) { // FIXME better field boundary detection
-        if (h0->current_slice && FIELD_PICTURE) {
+        if (h0->current_slice && FIELD_PICTURE(h)) {
             field_end(h, 1);
         }
 
         h0->current_slice = 0;
         if (!h0->first_field) {
-            if (h->cur_pic_ptr && !h->droppable &&
-                h->cur_pic_ptr->owner2 == h) {
-                ff_thread_report_progress(&h->cur_pic_ptr->f, INT_MAX,
+            if (h->cur_pic_ptr && !h->droppable) {
+                ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
                                           h->picture_structure == PICT_BOTTOM_FIELD);
             }
             h->cur_pic_ptr = NULL;
@@ -3101,12 +3240,6 @@
         h->slice_type_fixed = 0;
 
     slice_type = golomb_to_pict_type[slice_type];
-    if (slice_type == AV_PICTURE_TYPE_I ||
-        (h0->current_slice != 0 &&
-         slice_type == h0->last_slice_type &&
-         !memcmp(h0->last_ref_count, h0->ref_count, sizeof(h0->ref_count)))) {
-        default_ref_list_done = 1;
-    }
     h->slice_type     = slice_type;
     h->slice_type_nos = slice_type & 3;
 
@@ -3182,6 +3315,10 @@
     h->width  = 16 * h->mb_width;
     h->height = 16 * h->mb_height;
 
+    ret = init_dimensions(h);
+    if (ret < 0)
+        return ret;
+
     if (h->sps.video_signal_type_present_flag) {
         h->avctx->color_range = h->sps.full_range>0 ? AVCOL_RANGE_JPEG
                                                     : AVCOL_RANGE_MPEG;
@@ -3195,9 +3332,10 @@
     }
 
     if (h->context_initialized &&
-        (
-         needs_reinit                   ||
-         must_reinit)) {
+        (h->width  != h->avctx->coded_width   ||
+         h->height != h->avctx->coded_height  ||
+         must_reinit ||
+         needs_reinit)) {
 
         if (h != h0) {
             av_log(h->avctx, AV_LOG_ERROR, "changing width/height on "
@@ -3308,20 +3446,20 @@
         if (h0->first_field) {
             assert(h0->cur_pic_ptr);
             assert(h0->cur_pic_ptr->f.data[0]);
-            assert(h0->cur_pic_ptr->f.reference != DELAYED_PIC_REF);
+            assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);
 
             /* Mark old field/frame as completed */
-            if (!last_pic_droppable && h0->cur_pic_ptr->owner2 == h0) {
-                ff_thread_report_progress(&h0->cur_pic_ptr->f, INT_MAX,
+            if (!last_pic_droppable && h0->cur_pic_ptr->tf.owner == h0->avctx) {
+                ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
                                           last_pic_structure == PICT_BOTTOM_FIELD);
             }
 
             /* figure out if we have a complementary field pair */
-            if (!FIELD_PICTURE || h->picture_structure == last_pic_structure) {
+            if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
                 /* Previous field is unmatched. Don't display it, but let it
                  * remain for reference if marked as such. */
                 if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {
-                    ff_thread_report_progress(&h0->cur_pic_ptr->f, INT_MAX,
+                    ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
                                               last_pic_structure == PICT_TOP_FIELD);
                 }
             } else {
@@ -3331,7 +3469,7 @@
                      * pair. Throw away previous field except for reference
                      * purposes. */
                     if (!last_pic_droppable && last_pic_structure != PICT_FRAME) {
-                        ff_thread_report_progress(&h0->cur_pic_ptr->f, INT_MAX,
+                        ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
                                                   last_pic_structure == PICT_TOP_FIELD);
                     }
                 } else {
@@ -3347,21 +3485,12 @@
                         h->droppable         = last_pic_droppable;
                         return AVERROR_INVALIDDATA;
                     } else if (last_pic_droppable != h->droppable) {
-                        av_log(h->avctx, AV_LOG_ERROR,
-                               "Cannot combine reference and non-reference fields in the same frame\n");
-                        av_log_ask_for_sample(h->avctx, NULL);
+                        avpriv_request_sample(h->avctx,
+                                              "Found reference and non-reference fields in the same frame, which");
                         h->picture_structure = last_pic_structure;
                         h->droppable         = last_pic_droppable;
                         return AVERROR_PATCHWELCOME;
                     }
-
-                    /* Take ownership of this buffer. Note that if another thread owned
-                     * the first field of this buffer, we're not operating on that pointer,
-                     * so the original thread is still responsible for reporting progress
-                     * on that first field (or if that was us, we just did that above).
-                     * By taking ownership, we assign responsibility to ourselves to
-                     * report progress on the second field. */
-                    h0->cur_pic_ptr->owner2 = h0;
                 }
             }
         }
@@ -3374,13 +3503,13 @@
             if (!h->sps.gaps_in_frame_num_allowed_flag)
                 for(i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)
                     h->last_pocs[i] = INT_MIN;
-            if (ff_h264_frame_start(h) < 0)
+            if (h264_frame_start(h) < 0)
                 return -1;
             h->prev_frame_num++;
             h->prev_frame_num %= 1 << h->sps.log2_max_frame_num;
             h->cur_pic_ptr->frame_num = h->prev_frame_num;
-            ff_thread_report_progress(&h->cur_pic_ptr->f, INT_MAX, 0);
-            ff_thread_report_progress(&h->cur_pic_ptr->f, INT_MAX, 1);
+            ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 0);
+            ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX, 1);
             if ((ret = ff_generate_sliding_window_mmcos(h, 1)) < 0 &&
                 h->avctx->err_recognition & AV_EF_EXPLODE)
                 return ret;
@@ -3410,17 +3539,17 @@
         if (h0->first_field) {
             assert(h0->cur_pic_ptr);
             assert(h0->cur_pic_ptr->f.data[0]);
-            assert(h0->cur_pic_ptr->f.reference != DELAYED_PIC_REF);
+            assert(h0->cur_pic_ptr->reference != DELAYED_PIC_REF);
 
             /* figure out if we have a complementary field pair */
-            if (!FIELD_PICTURE || h->picture_structure == last_pic_structure) {
+            if (!FIELD_PICTURE(h) || h->picture_structure == last_pic_structure) {
                 /* Previous field is unmatched. Don't display it, but let it
                  * remain for reference if marked as such. */
                 h0->cur_pic_ptr = NULL;
-                h0->first_field = FIELD_PICTURE;
+                h0->first_field = FIELD_PICTURE(h);
             } else {
                 if (h0->cur_pic_ptr->frame_num != h->frame_num) {
-                    ff_thread_report_progress((AVFrame*)h0->cur_pic_ptr, INT_MAX,
+                    ff_thread_report_progress(&h0->cur_pic_ptr->tf, INT_MAX,
                                               h0->picture_structure==PICT_BOTTOM_FIELD);
                     /* This and the previous field had different frame_nums.
                      * Consider this field first in pair. Throw away previous
@@ -3434,17 +3563,27 @@
             }
         } else {
             /* Frame or first field in a potentially complementary pair */
-            h0->first_field = FIELD_PICTURE;
+            h0->first_field = FIELD_PICTURE(h);
         }
 
-        if (!FIELD_PICTURE || h0->first_field) {
-            if (ff_h264_frame_start(h) < 0) {
+        if (!FIELD_PICTURE(h) || h0->first_field) {
+            if (h264_frame_start(h) < 0) {
                 h0->first_field = 0;
                 return -1;
             }
         } else {
             release_unused_pictures(h, 0);
         }
+        /* Some macroblocks can be accessed before they're available in case
+        * of lost slices, MBAFF or threading. */
+        if (FIELD_PICTURE(h)) {
+            for(i = (h->picture_structure == PICT_BOTTOM_FIELD); i<h->mb_height; i++)
+                memset(h->slice_table + i*h->mb_stride, -1, (h->mb_stride - (i+1==h->mb_height)) * sizeof(*h->slice_table));
+        } else {
+            memset(h->slice_table, -1,
+                (h->mb_height * h->mb_stride - 1) * sizeof(*h->slice_table));
+        }
+        h0->last_slice_type = -1;
     }
     if (h != h0 && (ret = clone_slice(h, h0)) < 0)
         return ret;
@@ -3461,13 +3600,13 @@
     h->cur_pic_ptr->frame_num = h->frame_num; // FIXME frame_num cleanup
 
     av_assert1(h->mb_num == h->mb_width * h->mb_height);
-    if (first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= h->mb_num ||
+    if (first_mb_in_slice << FIELD_OR_MBAFF_PICTURE(h) >= h->mb_num ||
         first_mb_in_slice >= h->mb_num) {
         av_log(h->avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
         return -1;
     }
     h->resync_mb_x = h->mb_x =  first_mb_in_slice % h->mb_width;
-    h->resync_mb_y = h->mb_y = (first_mb_in_slice / h->mb_width) << FIELD_OR_MBAFF_PICTURE;
+    h->resync_mb_y = h->mb_y = (first_mb_in_slice / h->mb_width) << FIELD_OR_MBAFF_PICTURE(h);
     if (h->picture_structure == PICT_BOTTOM_FIELD)
         h->resync_mb_y = h->mb_y = h->mb_y + 1;
     av_assert1(h->mb_y < h->mb_height);
@@ -3525,7 +3664,7 @@
 
         if (h->ref_count[0]-1 > max[0] || h->ref_count[1]-1 > max[1]){
             av_log(h->avctx, AV_LOG_ERROR, "reference overflow %u > %u or %u > %u\n", h->ref_count[0]-1, max[0], h->ref_count[1]-1, max[1]);
-            h->ref_count[0] = h->ref_count[1] = 1;
+            h->ref_count[0] = h->ref_count[1] = 0;
             return AVERROR_INVALIDDATA;
         }
 
@@ -3533,11 +3672,16 @@
             h->list_count = 2;
         else
             h->list_count = 1;
-    } else
-        h->ref_count[1]= h->ref_count[0]= h->list_count= 0;
-
-    if (!default_ref_list_done)
+    } else {
+        h->list_count = 0;
+        h->ref_count[0] = h->ref_count[1] = 0;
+    }
+    if (slice_type != AV_PICTURE_TYPE_I &&
+        (h0->current_slice == 0 ||
+         slice_type != h0->last_slice_type ||
+         memcmp(h0->last_ref_count, h0->ref_count, sizeof(h0->ref_count)))) {
         ff_h264_fill_default_ref_list(h);
+    }
 
     if (h->slice_type_nos != AV_PICTURE_TYPE_I &&
         ff_h264_decode_ref_pic_list_reordering(h) < 0) {
@@ -3572,7 +3716,7 @@
         (h->avctx->err_recognition & AV_EF_EXPLODE))
         return AVERROR_INVALIDDATA;
 
-    if (FRAME_MBAFF) {
+    if (FRAME_MBAFF(h)) {
         ff_h264_fill_mbaff_ref_list(h);
 
         if (h->pps.weighted_bipred_idc == 2 && h->slice_type_nos == AV_PICTURE_TYPE_B) {
@@ -3690,16 +3834,16 @@
         int *ref2frm = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][j];
         for (i = 0; i < 16; i++) {
             id_list[i] = 60;
-            if (h->ref_list[j][i].f.data[0]) {
+            if (j < h->list_count && i < h->ref_count[j] && h->ref_list[j][i].f.buf[0]) {
                 int k;
-                uint8_t *base = h->ref_list[j][i].f.base[0];
+                AVBuffer *buf = h->ref_list[j][i].f.buf[0]->buffer;
                 for (k = 0; k < h->short_ref_count; k++)
-                    if (h->short_ref[k]->f.base[0] == base) {
+                    if (h->short_ref[k]->f.buf[0]->buffer == buf) {
                         id_list[i] = k;
                         break;
                     }
                 for (k = 0; k < h->long_ref_count; k++)
-                    if (h->long_ref[k] && h->long_ref[k]->f.base[0] == base) {
+                    if (h->long_ref[k] && h->long_ref[k]->f.buf[0]->buffer == buf) {
                         id_list[i] = h->short_ref_count + k;
                         break;
                     }
@@ -3710,12 +3854,12 @@
             ref2frm[1] = -1;
         for (i = 0; i < 16; i++)
             ref2frm[i + 2] = 4 * id_list[i] +
-                             (h->ref_list[j][i].f.reference & 3);
+                             (h->ref_list[j][i].reference & 3);
         ref2frm[18 + 0]     =
             ref2frm[18 + 1] = -1;
         for (i = 16; i < 48; i++)
             ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
-                             (h->ref_list[j][i].f.reference & 3);
+                             (h->ref_list[j][i].reference & 3);
     }
 
     if (h->ref_count[0]) h->er.last_pic = &h->ref_list[0][0];
@@ -3777,12 +3921,12 @@
         if (USES_LIST(top_type, list)) {
             const int b_xy  = h->mb2b_xy[top_xy] + 3 * b_stride;
             const int b8_xy = 4 * top_xy + 2;
-            int (*ref2frm)[64] = (void*)(h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][0] + (MB_MBAFF ? 20 : 2));
-            AV_COPY128(mv_dst - 1 * 8, h->cur_pic.f.motion_val[list][b_xy + 0]);
+            int (*ref2frm)[64] = (void*)(h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2));
+            AV_COPY128(mv_dst - 1 * 8, h->cur_pic.motion_val[list][b_xy + 0]);
             ref_cache[0 - 1 * 8] =
-            ref_cache[1 - 1 * 8] = ref2frm[list][h->cur_pic.f.ref_index[list][b8_xy + 0]];
+            ref_cache[1 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 0]];
             ref_cache[2 - 1 * 8] =
-            ref_cache[3 - 1 * 8] = ref2frm[list][h->cur_pic.f.ref_index[list][b8_xy + 1]];
+            ref_cache[3 - 1 * 8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 1]];
         } else {
             AV_ZERO128(mv_dst - 1 * 8);
             AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
@@ -3792,15 +3936,15 @@
             if (USES_LIST(left_type[LTOP], list)) {
                 const int b_xy  = h->mb2b_xy[left_xy[LTOP]] + 3;
                 const int b8_xy = 4 * left_xy[LTOP] + 1;
-                int (*ref2frm)[64] =(void*)( h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][0] + (MB_MBAFF ? 20 : 2));
-                AV_COPY32(mv_dst - 1 +  0, h->cur_pic.f.motion_val[list][b_xy + b_stride * 0]);
-                AV_COPY32(mv_dst - 1 +  8, h->cur_pic.f.motion_val[list][b_xy + b_stride * 1]);
-                AV_COPY32(mv_dst - 1 + 16, h->cur_pic.f.motion_val[list][b_xy + b_stride * 2]);
-                AV_COPY32(mv_dst - 1 + 24, h->cur_pic.f.motion_val[list][b_xy + b_stride * 3]);
+                int (*ref2frm)[64] =(void*)( h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2));
+                AV_COPY32(mv_dst - 1 +  0, h->cur_pic.motion_val[list][b_xy + b_stride * 0]);
+                AV_COPY32(mv_dst - 1 +  8, h->cur_pic.motion_val[list][b_xy + b_stride * 1]);
+                AV_COPY32(mv_dst - 1 + 16, h->cur_pic.motion_val[list][b_xy + b_stride * 2]);
+                AV_COPY32(mv_dst - 1 + 24, h->cur_pic.motion_val[list][b_xy + b_stride * 3]);
                 ref_cache[-1 +  0] =
-                ref_cache[-1 +  8] = ref2frm[list][h->cur_pic.f.ref_index[list][b8_xy + 2 * 0]];
+                ref_cache[-1 +  8] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 0]];
                 ref_cache[-1 + 16] =
-                ref_cache[-1 + 24] = ref2frm[list][h->cur_pic.f.ref_index[list][b8_xy + 2 * 1]];
+                ref_cache[-1 + 24] = ref2frm[list][h->cur_pic.ref_index[list][b8_xy + 2 * 1]];
             } else {
                 AV_ZERO32(mv_dst - 1 +  0);
                 AV_ZERO32(mv_dst - 1 +  8);
@@ -3824,8 +3968,8 @@
     }
 
     {
-        int8_t *ref = &h->cur_pic.f.ref_index[list][4 * mb_xy];
-        int (*ref2frm)[64] = (void*)(h->ref2frm[h->slice_num & (MAX_SLICES - 1)][0] + (MB_MBAFF ? 20 : 2));
+        int8_t *ref = &h->cur_pic.ref_index[list][4 * mb_xy];
+        int (*ref2frm)[64] = (void*)(h->ref2frm[h->slice_num & (MAX_SLICES - 1)][0] + (MB_MBAFF(h) ? 20 : 2));
         uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]], ref2frm[list][ref[1]]) & 0x00FF00FF) * 0x0101;
         uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]], ref2frm[list][ref[3]]) & 0x00FF00FF) * 0x0101;
         AV_WN32A(&ref_cache[0 * 8], ref01);
@@ -3835,7 +3979,7 @@
     }
 
     {
-        int16_t(*mv_src)[2] = &h->cur_pic.f.motion_val[list][4 * h->mb_x + 4 * h->mb_y * b_stride];
+        int16_t(*mv_src)[2] = &h->cur_pic.motion_val[list][4 * h->mb_x + 4 * h->mb_y * b_stride];
         AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
         AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
         AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
@@ -3855,14 +3999,14 @@
     uint8_t *nnz;
     uint8_t *nnz_cache;
 
-    top_xy = mb_xy - (h->mb_stride << MB_FIELD);
+    top_xy = mb_xy - (h->mb_stride << MB_FIELD(h));
 
     /* Wow, what a mess, why didn't they simplify the interlacing & intra
      * stuff, I can't imagine that these complex rules are worth it. */
 
     left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
-    if (FRAME_MBAFF) {
-        const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.f.mb_type[mb_xy - 1]);
+    if (FRAME_MBAFF(h)) {
+        const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
         const int curr_mb_field_flag = IS_INTERLACED(mb_type);
         if (h->mb_y & 1) {
             if (left_mb_field_flag != curr_mb_field_flag)
@@ -3870,7 +4014,7 @@
         } else {
             if (curr_mb_field_flag)
                 top_xy += h->mb_stride &
-                    (((h->cur_pic.f.mb_type[top_xy] >> 7) & 1) - 1);
+                    (((h->cur_pic.mb_type[top_xy] >> 7) & 1) - 1);
             if (left_mb_field_flag != curr_mb_field_flag)
                 left_xy[LBOT] += h->mb_stride;
         }
@@ -3884,25 +4028,25 @@
          * This is a conservative estimate: could also check beta_offset
          * and more accurate chroma_qp. */
         int qp_thresh = h->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
-        int qp        = h->cur_pic.f.qscale_table[mb_xy];
+        int qp        = h->cur_pic.qscale_table[mb_xy];
         if (qp <= qp_thresh &&
             (left_xy[LTOP] < 0 ||
-             ((qp + h->cur_pic.f.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
+             ((qp + h->cur_pic.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
             (top_xy < 0 ||
-             ((qp + h->cur_pic.f.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
-            if (!FRAME_MBAFF)
+             ((qp + h->cur_pic.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
+            if (!FRAME_MBAFF(h))
                 return 1;
             if ((left_xy[LTOP] < 0 ||
-                 ((qp + h->cur_pic.f.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
+                 ((qp + h->cur_pic.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
                 (top_xy < h->mb_stride ||
-                 ((qp + h->cur_pic.f.qscale_table[top_xy - h->mb_stride] + 1) >> 1) <= qp_thresh))
+                 ((qp + h->cur_pic.qscale_table[top_xy - h->mb_stride] + 1) >> 1) <= qp_thresh))
                 return 1;
         }
     }
 
-    top_type        = h->cur_pic.f.mb_type[top_xy];
-    left_type[LTOP] = h->cur_pic.f.mb_type[left_xy[LTOP]];
-    left_type[LBOT] = h->cur_pic.f.mb_type[left_xy[LBOT]];
+    top_type        = h->cur_pic.mb_type[top_xy];
+    left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
+    left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
     if (h->deblocking_filter == 2) {
         if (h->slice_table[top_xy] != h->slice_num)
             top_type = 0;
@@ -3950,7 +4094,7 @@
 
     /* CAVLC 8x8dct requires NNZ values for residual decoding that differ
      * from what the loop filter needs */
-    if (!CABAC && h->pps.transform_8x8_mode) {
+    if (!CABAC(h) && h->pps.transform_8x8_mode) {
         if (IS_8x8DCT(top_type)) {
             nnz_cache[4 + 8 * 0]     =
                 nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
@@ -3996,21 +4140,21 @@
 {
     uint8_t *dest_y, *dest_cb, *dest_cr;
     int linesize, uvlinesize, mb_x, mb_y;
-    const int end_mb_y       = h->mb_y + FRAME_MBAFF;
+    const int end_mb_y       = h->mb_y + FRAME_MBAFF(h);
     const int old_slice_type = h->slice_type;
     const int pixel_shift    = h->pixel_shift;
     const int block_h        = 16 >> h->chroma_y_shift;
 
     if (h->deblocking_filter) {
         for (mb_x = start_x; mb_x < end_x; mb_x++)
-            for (mb_y = end_mb_y - FRAME_MBAFF; mb_y <= end_mb_y; mb_y++) {
+            for (mb_y = end_mb_y - FRAME_MBAFF(h); mb_y <= end_mb_y; mb_y++) {
                 int mb_xy, mb_type;
                 mb_xy         = h->mb_xy = mb_x + mb_y * h->mb_stride;
                 h->slice_num  = h->slice_table[mb_xy];
-                mb_type       = h->cur_pic.f.mb_type[mb_xy];
+                mb_type       = h->cur_pic.mb_type[mb_xy];
                 h->list_count = h->list_counts[mb_xy];
 
-                if (FRAME_MBAFF)
+                if (FRAME_MBAFF(h))
                     h->mb_mbaff               =
                     h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
 
@@ -4019,14 +4163,14 @@
                 dest_y  = h->cur_pic.f.data[0] +
                           ((mb_x << pixel_shift) + mb_y * h->linesize) * 16;
                 dest_cb = h->cur_pic.f.data[1] +
-                          (mb_x << pixel_shift) * (8 << CHROMA444) +
+                          (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
                           mb_y * h->uvlinesize * block_h;
                 dest_cr = h->cur_pic.f.data[2] +
-                          (mb_x << pixel_shift) * (8 << CHROMA444) +
+                          (mb_x << pixel_shift) * (8 << CHROMA444(h)) +
                           mb_y * h->uvlinesize * block_h;
                 // FIXME simplify above
 
-                if (MB_FIELD) {
+                if (MB_FIELD(h)) {
                     linesize   = h->mb_linesize   = h->linesize   * 2;
                     uvlinesize = h->mb_uvlinesize = h->uvlinesize * 2;
                     if (mb_y & 1) { // FIXME move out of this function?
@@ -4042,10 +4186,10 @@
                                  uvlinesize, 0);
                 if (fill_filter_caches(h, mb_type))
                     continue;
-                h->chroma_qp[0] = get_chroma_qp(h, 0, h->cur_pic.f.qscale_table[mb_xy]);
-                h->chroma_qp[1] = get_chroma_qp(h, 1, h->cur_pic.f.qscale_table[mb_xy]);
+                h->chroma_qp[0] = get_chroma_qp(h, 0, h->cur_pic.qscale_table[mb_xy]);
+                h->chroma_qp[1] = get_chroma_qp(h, 1, h->cur_pic.qscale_table[mb_xy]);
 
-                if (FRAME_MBAFF) {
+                if (FRAME_MBAFF(h)) {
                     ff_h264_filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr,
                                       linesize, uvlinesize);
                 } else {
@@ -4056,7 +4200,7 @@
     }
     h->slice_type   = old_slice_type;
     h->mb_x         = end_x;
-    h->mb_y         = end_mb_y - FRAME_MBAFF;
+    h->mb_y         = end_mb_y - FRAME_MBAFF(h);
     h->chroma_qp[0] = get_chroma_qp(h, 0, h->qscale);
     h->chroma_qp[1] = get_chroma_qp(h, 1, h->qscale);
 }
@@ -4065,9 +4209,9 @@
 {
     const int mb_xy = h->mb_x + h->mb_y * h->mb_stride;
     int mb_type     = (h->slice_table[mb_xy - 1] == h->slice_num) ?
-                      h->cur_pic.f.mb_type[mb_xy - 1] :
+                      h->cur_pic.mb_type[mb_xy - 1] :
                       (h->slice_table[mb_xy - h->mb_stride] == h->slice_num) ?
-                      h->cur_pic.f.mb_type[mb_xy - h->mb_stride] : 0;
+                      h->cur_pic.mb_type[mb_xy - h->mb_stride] : 0;
     h->mb_mbaff     = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
 }
 
@@ -4076,10 +4220,10 @@
  */
 static void decode_finish_row(H264Context *h)
 {
-    int top            = 16 * (h->mb_y      >> FIELD_PICTURE);
-    int pic_height     = 16 *  h->mb_height >> FIELD_PICTURE;
-    int height         =  16      << FRAME_MBAFF;
-    int deblock_border = (16 + 4) << FRAME_MBAFF;
+    int top            = 16 * (h->mb_y      >> FIELD_PICTURE(h));
+    int pic_height     = 16 *  h->mb_height >> FIELD_PICTURE(h);
+    int height         =  16      << FRAME_MBAFF(h);
+    int deblock_border = (16 + 4) << FRAME_MBAFF(h);
 
     if (h->deblocking_filter) {
         if ((top + height) >= pic_height)
@@ -4098,20 +4242,22 @@
 
     ff_h264_draw_horiz_band(h, top, height);
 
-    if (h->droppable)
+    if (h->droppable || h->er.error_occurred)
         return;
 
-    ff_thread_report_progress(&h->cur_pic_ptr->f, top + height - 1,
+    ff_thread_report_progress(&h->cur_pic_ptr->tf, top + height - 1,
                               h->picture_structure == PICT_BOTTOM_FIELD);
 }
 
 static void er_add_slice(H264Context *h, int startx, int starty,
                          int endx, int endy, int status)
 {
-    ERContext *er = &h->er;
+    if (CONFIG_ERROR_RESILIENCE) {
+        ERContext *er = &h->er;
 
-    er->ref_count = h->ref_count[0];
-    ff_er_add_slice(er, startx, starty, endx, endy, status);
+        er->ref_count = h->ref_count[0];
+        ff_er_add_slice(er, startx, starty, endx, endy, status);
+    }
 }
 
 static int decode_slice(struct AVCodecContext *avctx, void *arg)
@@ -4123,10 +4269,20 @@
 
     av_assert0(h->block_offset[15] == (4 * ((scan8[15] - scan8[0]) & 7) << h->pixel_shift) + 4 * h->linesize * ((scan8[15] - scan8[0]) >> 3));
 
-    h->is_complex = FRAME_MBAFF || h->picture_structure != PICT_FRAME ||
+    h->is_complex = FRAME_MBAFF(h) || h->picture_structure != PICT_FRAME ||
                     avctx->codec_id != AV_CODEC_ID_H264 ||
                     (CONFIG_GRAY && (h->flags & CODEC_FLAG_GRAY));
 
+    if (!(h->avctx->active_thread_type & FF_THREAD_SLICE) && h->picture_structure == PICT_FRAME) {
+        const int start_i  = av_clip(h->resync_mb_x + h->resync_mb_y * h->mb_width, 0, h->mb_num - 1);
+        if (start_i) {
+            int prev_status = h->er.error_status_table[h->er.mb_index2xy[start_i - 1]];
+            prev_status &= ~ VP_START;
+            if (prev_status != (ER_MV_END | ER_DC_END | ER_AC_END))
+                h->er.error_occurred = 1;
+        }
+    }
+
     if (h->pps.cabac) {
         /* realign */
         align_get_bits(&h->gb);
@@ -4148,7 +4304,7 @@
                 ff_h264_hl_decode_mb(h);
 
             // FIXME optimal? or let mb_decode decode 16x32 ?
-            if (ret >= 0 && FRAME_MBAFF) {
+            if (ret >= 0 && FRAME_MBAFF(h)) {
                 h->mb_y++;
 
                 ret = ff_h264_decode_mb_cabac(h);
@@ -4184,9 +4340,9 @@
                 h->mb_x = lf_x_start = 0;
                 decode_finish_row(h);
                 ++h->mb_y;
-                if (FIELD_OR_MBAFF_PICTURE) {
+                if (FIELD_OR_MBAFF_PICTURE(h)) {
                     ++h->mb_y;
-                    if (FRAME_MBAFF && h->mb_y < h->mb_height)
+                    if (FRAME_MBAFF(h) && h->mb_y < h->mb_height)
                         predict_field_decoding_flag(h);
                 }
             }
@@ -4209,7 +4365,7 @@
                 ff_h264_hl_decode_mb(h);
 
             // FIXME optimal? or let mb_decode decode 16x32 ?
-            if (ret >= 0 && FRAME_MBAFF) {
+            if (ret >= 0 && FRAME_MBAFF(h)) {
                 h->mb_y++;
                 ret = ff_h264_decode_mb_cavlc(h);
 
@@ -4231,9 +4387,9 @@
                 h->mb_x = lf_x_start = 0;
                 decode_finish_row(h);
                 ++h->mb_y;
-                if (FIELD_OR_MBAFF_PICTURE) {
+                if (FIELD_OR_MBAFF_PICTURE(h)) {
                     ++h->mb_y;
-                    if (FRAME_MBAFF && h->mb_y < h->mb_height)
+                    if (FRAME_MBAFF(h) && h->mb_y < h->mb_height)
                         predict_field_decoding_flag(h);
                 }
                 if (h->mb_y >= h->mb_height) {
@@ -4300,7 +4456,9 @@
         av_assert0(context_count > 0);
         for (i = 1; i < context_count; i++) {
             hx                    = h->thread_context[i];
-            hx->er.error_count  = 0;
+            if (CONFIG_ERROR_RESILIENCE) {
+                hx->er.error_count = 0;
+            }
             hx->x264_build        = h->x264_build;
         }
 
@@ -4313,8 +4471,10 @@
         h->mb_y              = hx->mb_y;
         h->droppable         = hx->droppable;
         h->picture_structure = hx->picture_structure;
-        for (i = 1; i < context_count; i++)
-            h->er.error_count += h->thread_context[i]->er.error_count;
+        if (CONFIG_ERROR_RESILIENCE) {
+            for (i = 1; i < context_count; i++)
+                h->er.error_count += h->thread_context[i]->er.error_count;
+        }
     }
 
     return 0;
@@ -4658,9 +4818,8 @@
 
 end:
     /* clean up */
-    if (h->cur_pic_ptr && h->cur_pic_ptr->owner2 == h &&
-        !h->droppable) {
-        ff_thread_report_progress(&h->cur_pic_ptr->f, INT_MAX,
+    if (h->cur_pic_ptr && !h->droppable) {
+        ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
                                   h->picture_structure == PICT_BOTTOM_FIELD);
     }
 
@@ -4680,6 +4839,26 @@
     return pos;
 }
 
+static int output_frame(H264Context *h, AVFrame *dst, AVFrame *src)
+{
+    int i;
+    int ret = av_frame_ref(dst, src);
+    if (ret < 0)
+        return ret;
+
+    if (!h->sps.crop)
+        return 0;
+
+    for (i = 0; i < 3; i++) {
+        int hshift = (i > 0) ? h->chroma_x_shift : 0;
+        int vshift = (i > 0) ? h->chroma_y_shift : 0;
+        int off    = ((h->sps.crop_left >> hshift) << h->pixel_shift) +
+            (h->sps.crop_top  >> vshift) * dst->linesize[i];
+        dst->data[i] += off;
+    }
+    return 0;
+}
+
 static int decode_frame(AVCodecContext *avctx, void *data,
                         int *got_frame, AVPacket *avpkt)
 {
@@ -4690,6 +4869,7 @@
     int buf_index      = 0;
     Picture *out;
     int i, out_idx;
+    int ret;
 
     h->flags  = avctx->flags;
 
@@ -4717,9 +4897,11 @@
             h->delayed_pic[i] = h->delayed_pic[i + 1];
 
         if (out) {
-            out->f.reference &= ~DELAYED_PIC_REF;
+            out->reference &= ~DELAYED_PIC_REF;
+            ret = output_frame(h, pict, &out->f);
+            if (ret < 0)
+                return ret;
             *got_frame = 1;
-            *pict      = out->f;
         }
 
         return buf_index;
@@ -4774,16 +4956,20 @@
         /* Wait for second field. */
         *got_frame = 0;
         if (h->next_output_pic && (h->next_output_pic->sync || h->sync>1)) {
+            ret = output_frame(h, pict, &h->next_output_pic->f);
+            if (ret < 0)
+                return ret;
             *got_frame = 1;
-            *pict      = h->next_output_pic->f;
+            if (CONFIG_MPEGVIDEO) {
+                ff_print_debug_info2(h->avctx, h->next_output_pic, pict, h->er.mbskip_table,
+                                    &h->low_delay,
+                                    h->mb_width, h->mb_height, h->mb_stride, 1);
+            }
         }
     }
 
     assert(pict->data[0] || !*got_frame);
 
-    ff_print_debug_info2(h->avctx, pict, h->er.mbskip_table, h->visualization_buffer, &h->low_delay,
-                         h->mb_width, h->mb_height, h->mb_stride, 1);
-
     return get_consumed_bytes(buf_index, buf_size);
 }
 
@@ -4803,17 +4989,11 @@
 static av_cold int h264_decode_end(AVCodecContext *avctx)
 {
     H264Context *h    = avctx->priv_data;
-    int i;
 
     ff_h264_remove_all_refs(h);
     ff_h264_free_context(h);
 
-    if (h->DPB && !h->avctx->internal->is_copy) {
-        for (i = 0; i < h->picture_count; i++) {
-            free_picture(h, &h->DPB[i]);
-        }
-    }
-    av_freep(&h->DPB);
+    unref_picture(h, &h->cur_pic);
 
     return 0;
 }
diff --git a/libavcodec/h264.h b/libavcodec/h264.h
index d391b22..61e1bcb 100644
--- a/libavcodec/h264.h
+++ b/libavcodec/h264.h
@@ -30,6 +30,7 @@
 
 #include "libavutil/intreadwrite.h"
 #include "cabac.h"
+#include "error_resilience.h"
 #include "get_bits.h"
 #include "mpegvideo.h"
 #include "h264chroma.h"
@@ -60,19 +61,19 @@
 #define MAX_SLICES 16
 
 #ifdef ALLOW_INTERLACE
-#define MB_MBAFF    h->mb_mbaff
-#define MB_FIELD    h->mb_field_decoding_flag
-#define FRAME_MBAFF h->mb_aff_frame
-#define FIELD_PICTURE (h->picture_structure != PICT_FRAME)
+#define MB_MBAFF(h)    h->mb_mbaff
+#define MB_FIELD(h)    h->mb_field_decoding_flag
+#define FRAME_MBAFF(h) h->mb_aff_frame
+#define FIELD_PICTURE(h) (h->picture_structure != PICT_FRAME)
 #define LEFT_MBS 2
 #define LTOP     0
 #define LBOT     1
 #define LEFT(i)  (i)
 #else
-#define MB_MBAFF      0
-#define MB_FIELD      0
-#define FRAME_MBAFF   0
-#define FIELD_PICTURE 0
+#define MB_MBAFF(h)      0
+#define MB_FIELD(h)      0
+#define FRAME_MBAFF(h)   0
+#define FIELD_PICTURE(h) 0
 #undef  IS_INTERLACED
 #define IS_INTERLACED(mb_type) 0
 #define LEFT_MBS 1
@@ -80,15 +81,15 @@
 #define LBOT     0
 #define LEFT(i)  0
 #endif
-#define FIELD_OR_MBAFF_PICTURE (FRAME_MBAFF || FIELD_PICTURE)
+#define FIELD_OR_MBAFF_PICTURE(h) (FRAME_MBAFF(h) || FIELD_PICTURE(h))
 
 #ifndef CABAC
-#define CABAC h->pps.cabac
+#define CABAC(h) h->pps.cabac
 #endif
 
-#define CHROMA    (h->sps.chroma_format_idc)
-#define CHROMA422 (h->sps.chroma_format_idc == 2)
-#define CHROMA444 (h->sps.chroma_format_idc == 3)
+#define CHROMA(h)    (h->sps.chroma_format_idc)
+#define CHROMA422(h) (h->sps.chroma_format_idc == 2)
+#define CHROMA444(h) (h->sps.chroma_format_idc == 3)
 
 #define EXTENDED_SAR       255
 
@@ -167,6 +168,8 @@
     int mb_aff;                        ///< mb_adaptive_frame_field_flag
     int direct_8x8_inference_flag;
     int crop;                          ///< frame_cropping_flag
+
+    /* those 4 are already in luma samples */
     unsigned int crop_left;            ///< frame_cropping_rect_left_offset
     unsigned int crop_right;           ///< frame_cropping_rect_right_offset
     unsigned int crop_top;             ///< frame_cropping_rect_top_offset
@@ -256,7 +259,6 @@
  */
 typedef struct H264Context {
     AVCodecContext *avctx;
-    DSPContext       dsp;
     VideoDSPContext vdsp;
     H264DSPContext h264dsp;
     H264ChromaContext h264chroma;
@@ -264,19 +266,19 @@
     MotionEstContext me;
     ParseContext parse_context;
     GetBitContext gb;
+    DSPContext       dsp;
     ERContext er;
 
     Picture *DPB;
     Picture *cur_pic_ptr;
     Picture cur_pic;
-    int picture_count;
-    int picture_range_start, picture_range_end;
 
     int pixel_shift;    ///< 0 for 8-bit H264, 1 for high-bit-depth H264
     int chroma_qp[2];   // QPc
 
     int qp_thresh;      ///< QP threshold to skip loopfilter
 
+    /* coded dimensions -- 16 * mb w/h */
     int width, height;
     int linesize, uvlinesize;
     int chroma_x_shift, chroma_y_shift;
@@ -647,6 +649,11 @@
     int16_t *dc_val_base;
 
     uint8_t *visualization_buffer[3]; ///< temporary buffer vor MV visualization
+
+    AVBufferPool *qscale_table_pool;
+    AVBufferPool *mb_type_pool;
+    AVBufferPool *motion_val_pool;
+    AVBufferPool *ref_index_pool;
 } H264Context;
 
 extern const uint8_t ff_h264_chroma_qp[7][QP_MAX_NUM + 1]; ///< One chroma qp table for each possible bit depth (8-14).
@@ -732,7 +739,6 @@
 int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma);
 
 void ff_h264_hl_decode_mb(H264Context *h);
-int ff_h264_frame_start(H264Context *h);
 int ff_h264_decode_extradata(H264Context *h, const uint8_t *buf, int size);
 int ff_h264_decode_init(AVCodecContext *avctx);
 void ff_h264_decode_init_vlc(void);
@@ -902,13 +908,13 @@
                                                     int b_xy, int b8_xy,
                                                     int mb_type, int list)
 {
-    int16_t(*mv_dst)[2] = &h->cur_pic.f.motion_val[list][b_xy];
+    int16_t(*mv_dst)[2] = &h->cur_pic.motion_val[list][b_xy];
     int16_t(*mv_src)[2] = &h->mv_cache[list][scan8[0]];
     AV_COPY128(mv_dst + 0 * b_stride, mv_src + 8 * 0);
     AV_COPY128(mv_dst + 1 * b_stride, mv_src + 8 * 1);
     AV_COPY128(mv_dst + 2 * b_stride, mv_src + 8 * 2);
     AV_COPY128(mv_dst + 3 * b_stride, mv_src + 8 * 3);
-    if (CABAC) {
+    if (CABAC(h)) {
         uint8_t (*mvd_dst)[2] = &h->mvd_table[list][FMO ? 8 * h->mb_xy
                                                         : h->mb2br_xy[h->mb_xy]];
         uint8_t(*mvd_src)[2]  = &h->mvd_cache[list][scan8[0]];
@@ -923,7 +929,7 @@
     }
 
     {
-        int8_t *ref_index = &h->cur_pic.f.ref_index[list][b8_xy];
+        int8_t *ref_index = &h->cur_pic.ref_index[list][b8_xy];
         int8_t *ref_cache = h->ref_cache[list];
         ref_index[0 + 0 * 2] = ref_cache[scan8[0]];
         ref_index[1 + 0 * 2] = ref_cache[scan8[4]];
@@ -941,13 +947,13 @@
     if (USES_LIST(mb_type, 0)) {
         write_back_motion_list(h, b_stride, b_xy, b8_xy, mb_type, 0);
     } else {
-        fill_rectangle(&h->cur_pic.f.ref_index[0][b8_xy],
+        fill_rectangle(&h->cur_pic.ref_index[0][b8_xy],
                        2, 2, 2, (uint8_t)LIST_NOT_USED, 1);
     }
     if (USES_LIST(mb_type, 1))
         write_back_motion_list(h, b_stride, b_xy, b8_xy, mb_type, 1);
 
-    if (h->slice_type_nos == AV_PICTURE_TYPE_B && CABAC) {
+    if (h->slice_type_nos == AV_PICTURE_TYPE_B && CABAC(h)) {
         if (IS_8X8(mb_type)) {
             uint8_t *direct_table = &h->direct_table[4 * h->mb_xy];
             direct_table[1] = h->sub_mb_type[1] >> 1;
diff --git a/libavcodec/h264_cabac.c b/libavcodec/h264_cabac.c
index bf9cf15..28d2a15 100644
--- a/libavcodec/h264_cabac.c
+++ b/libavcodec/h264_cabac.c
@@ -25,7 +25,7 @@
  * @author Michael Niedermayer <michaelni@gmx.at>
  */
 
-#define CABAC 1
+#define CABAC(h) 1
 
 #include "config.h"
 #include "cabac.h"
@@ -1281,8 +1281,8 @@
 
     unsigned long ctx = 0;
 
-    ctx += h->mb_field_decoding_flag & !!h->mb_x; //for FMO:(s->current_picture.f.mb_type[mba_xy] >> 7) & (h->slice_table[mba_xy] == h->slice_num);
-    ctx += (h->cur_pic.f.mb_type[mbb_xy] >> 7) & (h->slice_table[mbb_xy] == h->slice_num);
+    ctx += h->mb_field_decoding_flag & !!h->mb_x; //for FMO:(s->current_picture.mb_type[mba_xy] >> 7) & (h->slice_table[mba_xy] == h->slice_num);
+    ctx += (h->cur_pic.mb_type[mbb_xy] >> 7) & (h->slice_table[mbb_xy] == h->slice_num);
 
     return get_cabac_noinline( &h->cabac, &(h->cabac_state+70)[ctx] );
 }
@@ -1321,30 +1321,30 @@
     int mba_xy, mbb_xy;
     int ctx = 0;
 
-    if(FRAME_MBAFF){ //FIXME merge with the stuff in fill_caches?
+    if (FRAME_MBAFF(h)) { //FIXME merge with the stuff in fill_caches?
         int mb_xy = mb_x + (mb_y&~1)*h->mb_stride;
         mba_xy = mb_xy - 1;
         if( (mb_y&1)
             && h->slice_table[mba_xy] == h->slice_num
-            && MB_FIELD == !!IS_INTERLACED( h->cur_pic.f.mb_type[mba_xy] ) )
+            && MB_FIELD(h) == !!IS_INTERLACED( h->cur_pic.mb_type[mba_xy] ) )
             mba_xy += h->mb_stride;
-        if( MB_FIELD ){
+        if (MB_FIELD(h)) {
             mbb_xy = mb_xy - h->mb_stride;
             if( !(mb_y&1)
                 && h->slice_table[mbb_xy] == h->slice_num
-                && IS_INTERLACED( h->cur_pic.f.mb_type[mbb_xy] ) )
+                && IS_INTERLACED( h->cur_pic.mb_type[mbb_xy] ) )
                 mbb_xy -= h->mb_stride;
         }else
             mbb_xy = mb_x + (mb_y-1)*h->mb_stride;
     }else{
         int mb_xy = h->mb_xy;
         mba_xy = mb_xy - 1;
-        mbb_xy = mb_xy - (h->mb_stride << FIELD_PICTURE);
+        mbb_xy = mb_xy - (h->mb_stride << FIELD_PICTURE(h));
     }
 
-    if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP(h->cur_pic.f.mb_type[mba_xy] ))
+    if( h->slice_table[mba_xy] == h->slice_num && !IS_SKIP(h->cur_pic.mb_type[mba_xy] ))
         ctx++;
-    if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP(h->cur_pic.f.mb_type[mbb_xy] ))
+    if( h->slice_table[mbb_xy] == h->slice_num && !IS_SKIP(h->cur_pic.mb_type[mbb_xy] ))
         ctx++;
 
     if( h->slice_type_nos == AV_PICTURE_TYPE_B )
@@ -1622,9 +1622,9 @@
 #endif
 
     significant_coeff_ctx_base = h->cabac_state
-        + significant_coeff_flag_offset[MB_FIELD][cat];
+        + significant_coeff_flag_offset[MB_FIELD(h)][cat];
     last_coeff_ctx_base = h->cabac_state
-        + last_coeff_flag_offset[MB_FIELD][cat];
+        + last_coeff_flag_offset[MB_FIELD(h)][cat];
     abs_level_m1_ctx_base = h->cabac_state
         + coeff_abs_level_m1_offset[cat];
 
@@ -1644,7 +1644,7 @@
         if( last == max_coeff -1 ) {\
             index[coeff_count++] = last;\
         }
-        const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD];
+        const uint8_t *sig_off = significant_coeff_flag_offset_8x8[MB_FIELD(h)];
 #ifdef decode_significance
         coeff_count = decode_significance_8x8(CC, significant_coeff_ctx_base, index,
                                                  last_coeff_ctx_base, sig_off);
@@ -1810,7 +1810,7 @@
                                                          int max_coeff)
 {
     /* read coded block flag */
-    if( (cat != 5 || CHROMA444) && get_cabac( &h->cabac, &h->cabac_state[get_cabac_cbf_ctx( h, cat, n, max_coeff, 0 ) ] ) == 0 ) {
+    if( (cat != 5 || CHROMA444(h)) && get_cabac( &h->cabac, &h->cabac_state[get_cabac_cbf_ctx( h, cat, n, max_coeff, 0 ) ] ) == 0 ) {
         if( max_coeff == 64 ) {
             fill_rectangle(&h->non_zero_count_cache[scan8[n]], 2, 2, 8, 0, 1);
         } else {
@@ -1885,14 +1885,14 @@
     if( h->slice_type_nos != AV_PICTURE_TYPE_I ) {
         int skip;
         /* a skipped mb needs the aff flag from the following mb */
-        if( FRAME_MBAFF && (h->mb_y&1)==1 && h->prev_mb_skipped )
+        if (FRAME_MBAFF(h) && (h->mb_y & 1) == 1 && h->prev_mb_skipped)
             skip = h->next_mb_skipped;
         else
             skip = decode_cabac_mb_skip( h, h->mb_x, h->mb_y );
         /* read skip flags */
         if( skip ) {
-            if( FRAME_MBAFF && (h->mb_y&1)==0 ){
-                h->cur_pic.f.mb_type[mb_xy] = MB_TYPE_SKIP;
+            if (FRAME_MBAFF(h) && (h->mb_y & 1) == 0) {
+                h->cur_pic.mb_type[mb_xy] = MB_TYPE_SKIP;
                 h->next_mb_skipped = decode_cabac_mb_skip( h, h->mb_x, h->mb_y+1 );
                 if(!h->next_mb_skipped)
                     h->mb_mbaff = h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
@@ -1908,7 +1908,7 @@
 
         }
     }
-    if(FRAME_MBAFF){
+    if (FRAME_MBAFF(h)) {
         if( (h->mb_y&1) == 0 )
             h->mb_mbaff =
             h->mb_field_decoding_flag = decode_cabac_field_decoding_flag(h);
@@ -1916,7 +1916,7 @@
 
     h->prev_mb_skipped = 0;
 
-    fill_decode_neighbors(h, -(MB_FIELD));
+    fill_decode_neighbors(h, -(MB_FIELD(h)));
 
     if( h->slice_type_nos == AV_PICTURE_TYPE_B ) {
         int ctx = 0;
@@ -1980,7 +1980,7 @@
         h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode;
         mb_type= i_mb_type_info[mb_type].type;
     }
-    if(MB_FIELD)
+    if(MB_FIELD(h))
         mb_type |= MB_TYPE_INTERLACED;
 
     h->slice_table[ mb_xy ]= h->slice_num;
@@ -2011,16 +2011,16 @@
         h->cbp_table[mb_xy] = 0xf7ef;
         h->chroma_pred_mode_table[mb_xy] = 0;
         // In deblocking, the quantizer is 0
-        h->cur_pic.f.qscale_table[mb_xy] = 0;
+        h->cur_pic.qscale_table[mb_xy] = 0;
         // All coeffs are present
         memset(h->non_zero_count[mb_xy], 16, 48);
-        h->cur_pic.f.mb_type[mb_xy] = mb_type;
+        h->cur_pic.mb_type[mb_xy] = mb_type;
         h->last_qscale_diff = 0;
         return 0;
     }
 
-    local_ref_count[0] = h->ref_count[0] << MB_MBAFF;
-    local_ref_count[1] = h->ref_count[1] << MB_MBAFF;
+    local_ref_count[0] = h->ref_count[0] << MB_MBAFF(h);
+    local_ref_count[1] = h->ref_count[1] << MB_MBAFF(h);
 
     fill_decode_caches(h, mb_type);
 
@@ -2295,7 +2295,7 @@
 
     /* It would be better to do this in fill_decode_caches, but we don't know
      * the transform mode of the current macroblock there. */
-    if (CHROMA444 && IS_8x8DCT(mb_type)){
+    if (CHROMA444(h) && IS_8x8DCT(mb_type)){
         int i;
         uint8_t *nnz_cache = h->non_zero_count_cache;
         for (i = 0; i < 2; i++){
@@ -2309,13 +2309,13 @@
             }
         }
         if (h->top_type && !IS_8x8DCT(h->top_type)){
-            uint32_t top_empty = CABAC && !IS_INTRA(mb_type) ? 0 : 0x40404040;
+            uint32_t top_empty = CABAC(h) && !IS_INTRA(mb_type) ? 0 : 0x40404040;
             AV_WN32A(&nnz_cache[4+8* 0], top_empty);
             AV_WN32A(&nnz_cache[4+8* 5], top_empty);
             AV_WN32A(&nnz_cache[4+8*10], top_empty);
         }
     }
-    h->cur_pic.f.mb_type[mb_xy] = mb_type;
+    h->cur_pic.mb_type[mb_xy] = mb_type;
 
     if( cbp || IS_INTRA16x16( mb_type ) ) {
         const uint8_t *scan, *scan8x8;
@@ -2360,10 +2360,10 @@
             h->last_qscale_diff=0;
 
         decode_cabac_luma_residual(h, scan, scan8x8, pixel_shift, mb_type, cbp, 0);
-        if(CHROMA444){
+        if (CHROMA444(h)) {
             decode_cabac_luma_residual(h, scan, scan8x8, pixel_shift, mb_type, cbp, 1);
             decode_cabac_luma_residual(h, scan, scan8x8, pixel_shift, mb_type, cbp, 2);
-        } else if (CHROMA422) {
+        } else if (CHROMA422(h)) {
             if( cbp&0x30 ){
                 int c;
                 for (c = 0; c < 2; c++)
@@ -2417,7 +2417,7 @@
         h->last_qscale_diff = 0;
     }
 
-    h->cur_pic.f.qscale_table[mb_xy] = h->qscale;
+    h->cur_pic.qscale_table[mb_xy] = h->qscale;
     write_back_non_zero_count(h);
 
     return 0;
diff --git a/libavcodec/h264_cavlc.c b/libavcodec/h264_cavlc.c
index 0701a3c..2c23cae 100644
--- a/libavcodec/h264_cavlc.c
+++ b/libavcodec/h264_cavlc.c
@@ -25,7 +25,7 @@
  * @author Michael Niedermayer <michaelni@gmx.at>
  */
 
-#define CABAC 0
+#define CABAC(h) 0
 
 #include "internal.h"
 #include "avcodec.h"
@@ -548,9 +548,15 @@
                 if(prefix<15){
                     level_code = (prefix<<suffix_length) + get_bits(gb, suffix_length);
                 }else{
-                    level_code = (15<<suffix_length) + get_bits(gb, prefix-3);
-                    if(prefix>=16)
+                    level_code = 15<<suffix_length;
+                    if (prefix>=16) {
+                        if(prefix > 25+3){
+                            av_log(h->avctx, AV_LOG_ERROR, "Invalid level prefix\n");
+                            return AVERROR_INVALIDDATA;
+                        }
                         level_code += (1<<(prefix-3))-4096;
+                    }
+                    level_code += get_bits(gb, prefix-3);
                 }
                 mask= -(level_code&1);
                 level_code= (((2+level_code)>>1) ^ mask) - mask;
@@ -708,7 +714,7 @@
             h->mb_skip_run= get_ue_golomb(&h->gb);
 
         if (h->mb_skip_run--) {
-            if(FRAME_MBAFF && (h->mb_y&1) == 0){
+            if(FRAME_MBAFF(h) && (h->mb_y&1) == 0){
                 if(h->mb_skip_run==0)
                     h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&h->gb);
             }
@@ -716,7 +722,7 @@
             return 0;
         }
     }
-    if(FRAME_MBAFF){
+    if (FRAME_MBAFF(h)) {
         if( (h->mb_y&1) == 0 )
             h->mb_mbaff = h->mb_field_decoding_flag = get_bits1(&h->gb);
     }
@@ -755,7 +761,7 @@
         mb_type= i_mb_type_info[mb_type].type;
     }
 
-    if(MB_FIELD)
+    if(MB_FIELD(h))
         mb_type |= MB_TYPE_INTERLACED;
 
     h->slice_table[ mb_xy ]= h->slice_num;
@@ -769,16 +775,16 @@
         skip_bits_long(&h->gb, mb_size);
 
         // In deblocking, the quantizer is 0
-        h->cur_pic.f.qscale_table[mb_xy] = 0;
+        h->cur_pic.qscale_table[mb_xy] = 0;
         // All coeffs are present
         memset(h->non_zero_count[mb_xy], 16, 48);
 
-        h->cur_pic.f.mb_type[mb_xy] = mb_type;
+        h->cur_pic.mb_type[mb_xy] = mb_type;
         return 0;
     }
 
-    local_ref_count[0] = h->ref_count[0] << MB_MBAFF;
-    local_ref_count[1] = h->ref_count[1] << MB_MBAFF;
+    local_ref_count[0] = h->ref_count[0] << MB_MBAFF(h);
+    local_ref_count[1] = h->ref_count[1] << MB_MBAFF(h);
 
     fill_decode_neighbors(h, mb_type);
     fill_decode_caches(h, mb_type);
@@ -1073,7 +1079,7 @@
     }
     h->cbp=
     h->cbp_table[mb_xy]= cbp;
-    h->cur_pic.f.mb_type[mb_xy] = mb_type;
+    h->cur_pic.mb_type[mb_xy] = mb_type;
 
     if(cbp || IS_INTRA16x16(mb_type)){
         int i4x4, i8x8, chroma_idx;
@@ -1111,7 +1117,7 @@
             return -1;
         }
         h->cbp_table[mb_xy] |= ret << 12;
-        if(CHROMA444){
+        if (CHROMA444(h)) {
             if( decode_luma_residual(h, gb, scan, scan8x8, pixel_shift, mb_type, cbp, 1) < 0 ){
                 return -1;
             }
@@ -1125,7 +1131,7 @@
                 for(chroma_idx=0; chroma_idx<2; chroma_idx++)
                     if (decode_residual(h, gb, h->mb + ((256 + 16*16*chroma_idx) << pixel_shift),
                                         CHROMA_DC_BLOCK_INDEX+chroma_idx,
-                                        CHROMA422 ? chroma422_dc_scan : chroma_dc_scan,
+                                        CHROMA422(h) ? chroma422_dc_scan : chroma_dc_scan,
                                         NULL, 4*num_c8x8) < 0) {
                         return -1;
                     }
@@ -1154,7 +1160,7 @@
         fill_rectangle(&h->non_zero_count_cache[scan8[16]], 4, 4, 8, 0, 1);
         fill_rectangle(&h->non_zero_count_cache[scan8[32]], 4, 4, 8, 0, 1);
     }
-    h->cur_pic.f.qscale_table[mb_xy] = h->qscale;
+    h->cur_pic.qscale_table[mb_xy] = h->qscale;
     write_back_non_zero_count(h);
 
     return 0;
diff --git a/libavcodec/h264_direct.c b/libavcodec/h264_direct.c
index c6be455..2d93c2a 100644
--- a/libavcodec/h264_direct.c
+++ b/libavcodec/h264_direct.c
@@ -53,7 +53,7 @@
     const int poc1 = h->ref_list[1][0].poc;
     int i, field;
 
-    if (FRAME_MBAFF)
+    if (FRAME_MBAFF(h))
         for (field = 0; field < 2; field++){
             const int poc  = h->cur_pic_ptr->field_poc[field];
             const int poc1 = h->ref_list[1][0].field_poc[field];
@@ -87,7 +87,7 @@
                 poc= (poc&~3) + rfield + 1;
 
             for(j=start; j<end; j++){
-                if (4 * h->ref_list[0][j].frame_num + (h->ref_list[0][j].f.reference & 3) == poc) {
+                if (4 * h->ref_list[0][j].frame_num + (h->ref_list[0][j].reference & 3) == poc) {
                     int cur_ref= mbafi ? (j-16)^field : j;
                     if (ref1->mbaff)
                         map[list][2 * old_ref + (rfield^field) + 16] = cur_ref;
@@ -105,12 +105,12 @@
     Picture * const cur = h->cur_pic_ptr;
     int list, j, field;
     int sidx= (h->picture_structure&1)^1;
-    int ref1sidx = (ref1->f.reference&1)^1;
+    int ref1sidx = (ref1->reference&1)^1;
 
     for(list=0; list<2; list++){
         cur->ref_count[sidx][list] = h->ref_count[list];
         for(j=0; j<h->ref_count[list]; j++)
-            cur->ref_poc[sidx][list][j] = 4 * h->ref_list[list][j].frame_num + (h->ref_list[list][j].f.reference & 3);
+            cur->ref_poc[sidx][list][j] = 4 * h->ref_list[list][j].frame_num + (h->ref_list[list][j].reference & 3);
     }
 
     if(h->picture_structure == PICT_FRAME){
@@ -118,7 +118,7 @@
         memcpy(cur->ref_poc  [1], cur->ref_poc  [0], sizeof(cur->ref_poc  [0]));
     }
 
-    cur->mbaff= FRAME_MBAFF;
+    cur->mbaff = FRAME_MBAFF(h);
 
     h->col_fieldoff= 0;
     if(h->picture_structure == PICT_FRAME){
@@ -126,8 +126,8 @@
         int *col_poc = h->ref_list[1]->field_poc;
         h->col_parity= (FFABS(col_poc[0] - cur_poc) >= FFABS(col_poc[1] - cur_poc));
         ref1sidx=sidx= h->col_parity;
-    } else if (!(h->picture_structure & h->ref_list[1][0].f.reference) && !h->ref_list[1][0].mbaff) { // FL -> FL & differ parity
-        h->col_fieldoff = 2 * h->ref_list[1][0].f.reference - 3;
+    } else if (!(h->picture_structure & h->ref_list[1][0].reference) && !h->ref_list[1][0].mbaff) { // FL -> FL & differ parity
+        h->col_fieldoff = 2 * h->ref_list[1][0].reference - 3;
     }
 
     if (h->slice_type_nos != AV_PICTURE_TYPE_B || h->direct_spatial_mv_pred)
@@ -135,7 +135,7 @@
 
     for(list=0; list<2; list++){
         fill_colmap(h, h->map_col_to_list0, list, sidx, ref1sidx, 0);
-        if(FRAME_MBAFF)
+        if (FRAME_MBAFF(h))
         for(field=0; field<2; field++)
             fill_colmap(h, h->map_col_to_list0_field[field], list, field, field, 1);
     }
@@ -143,7 +143,7 @@
 
 static void await_reference_mb_row(H264Context * const h, Picture *ref, int mb_y)
 {
-    int ref_field = ref->f.reference - 1;
+    int ref_field = ref->reference - 1;
     int ref_field_picture = ref->field_picture;
     int ref_height = 16*h->mb_height >> ref_field_picture;
 
@@ -153,7 +153,7 @@
     //FIXME it can be safe to access mb stuff
     //even if pixels aren't deblocked yet
 
-    ff_thread_await_progress(&ref->f,
+    ff_thread_await_progress(&ref->tf,
                              FFMIN(16 * mb_y >> ref_field_picture, ref_height - 1),
                              ref_field_picture && ref_field);
 }
@@ -172,7 +172,7 @@
     int mv[2];
     int list;
 
-    assert(h->ref_list[1][0].f.reference & 3);
+    assert(h->ref_list[1][0].reference & 3);
 
     await_reference_mb_row(h, &h->ref_list[1][0], h->mb_y + !!IS_INTERLACED(*mb_type));
 
@@ -209,6 +209,7 @@
                     mv[list]= AV_RN32A(C);
                 }
             }
+            av_assert2(ref[list] < (h->ref_count[list] << !!FRAME_MBAFF(h)));
         }else{
             int mask= ~(MB_TYPE_L0 << (2*list));
             mv[list] = 0;
@@ -234,7 +235,7 @@
         return;
     }
 
-    if (IS_INTERLACED(h->ref_list[1][0].f.mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
+    if (IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
         if (!IS_INTERLACED(*mb_type)) {                          //     AFR/FR    -> AFL/FL
             mb_y = (h->mb_y&~1) + h->col_parity;
             mb_xy= h->mb_x + ((h->mb_y&~1) + h->col_parity)*h->mb_stride;
@@ -248,8 +249,8 @@
         if(IS_INTERLACED(*mb_type)){                     // AFL       /FL -> AFR/FR
             mb_y = h->mb_y&~1;
             mb_xy= h->mb_x + (h->mb_y&~1)*h->mb_stride;
-            mb_type_col[0] = h->ref_list[1][0].f.mb_type[mb_xy];
-            mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy + h->mb_stride];
+            mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
+            mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + h->mb_stride];
             b8_stride = 2+4*h->mb_stride;
             b4_stride *= 6;
             if (IS_INTERLACED(mb_type_col[0]) != IS_INTERLACED(mb_type_col[1])) {
@@ -268,7 +269,7 @@
         }else{                                           //     AFR/FR    -> AFR/FR
 single_col:
             mb_type_col[0] =
-            mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy];
+            mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
 
             sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
             if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
@@ -288,10 +289,10 @@
 
     await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
 
-    l1mv0  = (void*)&h->ref_list[1][0].f.motion_val[0][h->mb2b_xy [mb_xy]];
-    l1mv1  = (void*)&h->ref_list[1][0].f.motion_val[1][h->mb2b_xy [mb_xy]];
-    l1ref0 = &h->ref_list[1][0].f.ref_index [0][4 * mb_xy];
-    l1ref1 = &h->ref_list[1][0].f.ref_index [1][4 * mb_xy];
+    l1mv0  = (void*)&h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
+    l1mv1  = (void*)&h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
+    l1ref0 = &h->ref_list[1][0].ref_index [0][4 * mb_xy];
+    l1ref1 = &h->ref_list[1][0].ref_index [1][4 * mb_xy];
     if(!b8_stride){
         if(h->mb_y&1){
             l1ref0 += 2;
@@ -419,11 +420,11 @@
     unsigned int sub_mb_type;
     int i8, i4;
 
-    assert(h->ref_list[1][0].f.reference & 3);
+    assert(h->ref_list[1][0].reference & 3);
 
     await_reference_mb_row(h, &h->ref_list[1][0], h->mb_y + !!IS_INTERLACED(*mb_type));
 
-    if (IS_INTERLACED(h->ref_list[1][0].f.mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
+    if (IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
         if (!IS_INTERLACED(*mb_type)) {                          //     AFR/FR    -> AFL/FL
             mb_y = (h->mb_y&~1) + h->col_parity;
             mb_xy= h->mb_x + ((h->mb_y&~1) + h->col_parity)*h->mb_stride;
@@ -437,8 +438,8 @@
         if(IS_INTERLACED(*mb_type)){                     // AFL       /FL -> AFR/FR
             mb_y = h->mb_y&~1;
             mb_xy= h->mb_x + (h->mb_y&~1)*h->mb_stride;
-            mb_type_col[0] = h->ref_list[1][0].f.mb_type[mb_xy];
-            mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy + h->mb_stride];
+            mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
+            mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + h->mb_stride];
             b8_stride = 2+4*h->mb_stride;
             b4_stride *= 6;
             if (IS_INTERLACED(mb_type_col[0]) != IS_INTERLACED(mb_type_col[1])) {
@@ -458,7 +459,7 @@
         }else{                                           //     AFR/FR    -> AFR/FR
 single_col:
             mb_type_col[0] =
-            mb_type_col[1] = h->ref_list[1][0].f.mb_type[mb_xy];
+            mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
 
             sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
             if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
@@ -478,10 +479,10 @@
 
     await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
 
-    l1mv0  = (void*)&h->ref_list[1][0].f.motion_val[0][h->mb2b_xy [mb_xy]];
-    l1mv1  = (void*)&h->ref_list[1][0].f.motion_val[1][h->mb2b_xy [mb_xy]];
-    l1ref0 = &h->ref_list[1][0].f.ref_index [0][4 * mb_xy];
-    l1ref1 = &h->ref_list[1][0].f.ref_index [1][4 * mb_xy];
+    l1mv0  = (void*)&h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
+    l1mv1  = (void*)&h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
+    l1ref0 = &h->ref_list[1][0].ref_index [0][4 * mb_xy];
+    l1ref1 = &h->ref_list[1][0].ref_index [1][4 * mb_xy];
     if(!b8_stride){
         if(h->mb_y&1){
             l1ref0 += 2;
@@ -496,7 +497,7 @@
         const int *dist_scale_factor = h->dist_scale_factor;
         int ref_offset;
 
-        if(FRAME_MBAFF && IS_INTERLACED(*mb_type)){
+        if (FRAME_MBAFF(h) && IS_INTERLACED(*mb_type)) {
             map_col_to_list0[0] = h->map_col_to_list0_field[h->mb_y&1][0];
             map_col_to_list0[1] = h->map_col_to_list0_field[h->mb_y&1][1];
             dist_scale_factor   =h->dist_scale_factor_field[h->mb_y&1];
diff --git a/libavcodec/h264_loopfilter.c b/libavcodec/h264_loopfilter.c
index 13d99f2..475070a 100644
--- a/libavcodec/h264_loopfilter.c
+++ b/libavcodec/h264_loopfilter.c
@@ -241,9 +241,9 @@
                                                           unsigned int uvlinesize,
                                                           int pixel_shift)
 {
-    int chroma = CHROMA && !(CONFIG_GRAY && (h->flags&CODEC_FLAG_GRAY));
-    int chroma444 = CHROMA444;
-    int chroma422 = CHROMA422;
+    int chroma = CHROMA(h) && !(CONFIG_GRAY && (h->flags&CODEC_FLAG_GRAY));
+    int chroma444 = CHROMA444(h);
+    int chroma422 = CHROMA422(h);
 
     int mb_xy = h->mb_xy;
     int left_type= h->left_type[LTOP];
@@ -253,10 +253,10 @@
     int a = h->slice_alpha_c0_offset - qp_bd_offset;
     int b = h->slice_beta_offset - qp_bd_offset;
 
-    int mb_type = h->cur_pic.f.mb_type[mb_xy];
-    int qp      = h->cur_pic.f.qscale_table[mb_xy];
-    int qp0     = h->cur_pic.f.qscale_table[mb_xy - 1];
-    int qp1     = h->cur_pic.f.qscale_table[h->top_mb_xy];
+    int mb_type = h->cur_pic.mb_type[mb_xy];
+    int qp      = h->cur_pic.qscale_table[mb_xy];
+    int qp0     = h->cur_pic.qscale_table[mb_xy - 1];
+    int qp1     = h->cur_pic.qscale_table[h->top_mb_xy];
     int qpc = get_chroma_qp( h, 0, qp );
     int qpc0 = get_chroma_qp( h, 0, qp0 );
     int qpc1 = get_chroma_qp( h, 0, qp1 );
@@ -268,7 +268,7 @@
     if( IS_INTRA(mb_type) ) {
         static const int16_t bS4[4] = {4,4,4,4};
         static const int16_t bS3[4] = {3,3,3,3};
-        const int16_t *bSH = FIELD_PICTURE ? bS3 : bS4;
+        const int16_t *bSH = FIELD_PICTURE(h) ? bS3 : bS4;
         if(left_type)
             filter_mb_edgev( &img_y[4*0<<pixel_shift], linesize, bS4, qp0, a, b, h, 1);
         if( IS_8x8DCT(mb_type) ) {
@@ -369,12 +369,12 @@
             int step =  1+(mb_type>>24); //IS_8x8DCT(mb_type) ? 2 : 1;
             edges = 4 - 3*((mb_type>>3) & !(h->cbp & 15)); //(mb_type & MB_TYPE_16x16) && !(h->cbp & 15) ? 1 : 4;
             h->h264dsp.h264_loop_filter_strength( bS, h->non_zero_count_cache, h->ref_cache, h->mv_cache,
-                                              h->list_count==2, edges, step, mask_edge0, mask_edge1, FIELD_PICTURE);
+                                              h->list_count==2, edges, step, mask_edge0, mask_edge1, FIELD_PICTURE(h));
         }
         if( IS_INTRA(left_type) )
             AV_WN64A(bS[0][0], 0x0004000400040004ULL);
         if( IS_INTRA(top_type) )
-            AV_WN64A(bS[1][0], FIELD_PICTURE ? 0x0003000300030003ULL : 0x0004000400040004ULL);
+            AV_WN64A(bS[1][0], FIELD_PICTURE(h) ? 0x0003000300030003ULL : 0x0004000400040004ULL);
 
 #define FILTER(hv,dir,edge,intra)\
         if(AV_RN64A(bS[dir][edge])) {                                   \
@@ -414,7 +414,7 @@
 }
 
 void ff_h264_filter_mb_fast( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
-    av_assert2(!FRAME_MBAFF);
+    av_assert2(!FRAME_MBAFF(h));
     if(!h->h264dsp.h264_loop_filter_strength || h->pps.chroma_qp_diff) {
         ff_h264_filter_mb(h, mb_x, mb_y, img_y, img_cb, img_cr, linesize, uvlinesize);
         return;
@@ -463,8 +463,8 @@
 static av_always_inline void filter_mb_dir(H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize, int mb_xy, int mb_type, int mvy_limit, int first_vertical_edge_done, int a, int b, int chroma, int dir) {
     int edge;
     int chroma_qp_avg[2];
-    int chroma444 = CHROMA444;
-    int chroma422 = CHROMA422;
+    int chroma444 = CHROMA444(h);
+    int chroma422 = CHROMA422(h);
     const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
     const int mbm_type = dir == 0 ? h->left_type[LTOP] : h->top_type;
 
@@ -479,7 +479,7 @@
 
     if(mbm_type && !first_vertical_edge_done){
 
-        if (FRAME_MBAFF && (dir == 1) && ((mb_y&1) == 0)
+        if (FRAME_MBAFF(h) && (dir == 1) && ((mb_y&1) == 0)
             && IS_INTERLACED(mbm_type&~mb_type)
             ) {
             // This is a special case in the norm where the filtering must
@@ -494,10 +494,10 @@
             for(j=0; j<2; j++, mbn_xy += h->mb_stride){
                 DECLARE_ALIGNED(8, int16_t, bS)[4];
                 int qp;
-                if (IS_INTRA(mb_type | h->cur_pic.f.mb_type[mbn_xy])) {
+                if (IS_INTRA(mb_type | h->cur_pic.mb_type[mbn_xy])) {
                     AV_WN64A(bS, 0x0003000300030003ULL);
                 } else {
-                    if (!CABAC && IS_8x8DCT(h->cur_pic.f.mb_type[mbn_xy])) {
+                    if (!CABAC(h) && IS_8x8DCT(h->cur_pic.mb_type[mbn_xy])) {
                         bS[0]= 1+((h->cbp_table[mbn_xy] & 0x4000)||h->non_zero_count_cache[scan8[0]+0]);
                         bS[1]= 1+((h->cbp_table[mbn_xy] & 0x4000)||h->non_zero_count_cache[scan8[0]+1]);
                         bS[2]= 1+((h->cbp_table[mbn_xy] & 0x8000)||h->non_zero_count_cache[scan8[0]+2]);
@@ -512,12 +512,12 @@
                 }
                 // Do not use s->qscale as luma quantizer because it has not the same
                 // value in IPCM macroblocks.
-                qp = (h->cur_pic.f.qscale_table[mb_xy] + h->cur_pic.f.qscale_table[mbn_xy] + 1) >> 1;
+                qp = (h->cur_pic.qscale_table[mb_xy] + h->cur_pic.qscale_table[mbn_xy] + 1) >> 1;
                 tprintf(h->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, tmp_linesize, tmp_uvlinesize);
                 { int i; for (i = 0; i < 4; i++) tprintf(h->avctx, " bS[%d]:%d", i, bS[i]); tprintf(h->avctx, "\n"); }
                 filter_mb_edgeh( &img_y[j*linesize], tmp_linesize, bS, qp, a, b, h, 0 );
-                chroma_qp_avg[0] = (h->chroma_qp[0] + get_chroma_qp(h, 0, h->cur_pic.f.qscale_table[mbn_xy]) + 1) >> 1;
-                chroma_qp_avg[1] = (h->chroma_qp[1] + get_chroma_qp(h, 1, h->cur_pic.f.qscale_table[mbn_xy]) + 1) >> 1;
+                chroma_qp_avg[0] = (h->chroma_qp[0] + get_chroma_qp(h, 0, h->cur_pic.qscale_table[mbn_xy]) + 1) >> 1;
+                chroma_qp_avg[1] = (h->chroma_qp[1] + get_chroma_qp(h, 1, h->cur_pic.qscale_table[mbn_xy]) + 1) >> 1;
                 if (chroma) {
                     if (chroma444) {
                         filter_mb_edgeh (&img_cb[j*uvlinesize], tmp_uvlinesize, bS, chroma_qp_avg[0], a, b, h, 0);
@@ -535,14 +535,14 @@
             if( IS_INTRA(mb_type|mbm_type)) {
                 AV_WN64A(bS, 0x0003000300030003ULL);
                 if (   (!IS_INTERLACED(mb_type|mbm_type))
-                    || ((FRAME_MBAFF || (h->picture_structure != PICT_FRAME)) && (dir == 0))
+                    || ((FRAME_MBAFF(h) || (h->picture_structure != PICT_FRAME)) && (dir == 0))
                 )
                     AV_WN64A(bS, 0x0004000400040004ULL);
             } else {
                 int i;
                 int mv_done;
 
-                if( dir && FRAME_MBAFF && IS_INTERLACED(mb_type ^ mbm_type)) {
+                if( dir && FRAME_MBAFF(h) && IS_INTERLACED(mb_type ^ mbm_type)) {
                     AV_WN64A(bS, 0x0001000100010001ULL);
                     mv_done = 1;
                 }
@@ -577,12 +577,12 @@
             // Do not use s->qscale as luma quantizer because it has not the same
             // value in IPCM macroblocks.
             if(bS[0]+bS[1]+bS[2]+bS[3]){
-                qp = (h->cur_pic.f.qscale_table[mb_xy] + h->cur_pic.f.qscale_table[mbm_xy] + 1) >> 1;
+                qp = (h->cur_pic.qscale_table[mb_xy] + h->cur_pic.qscale_table[mbm_xy] + 1) >> 1;
                 //tprintf(h->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d, QPc:%d, QPcn:%d\n", mb_x, mb_y, dir, edge, qp, h->chroma_qp[0], h->cur_pic.qscale_table[mbn_xy]);
                 tprintf(h->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
                 //{ int i; for (i = 0; i < 4; i++) tprintf(h->avctx, " bS[%d]:%d", i, bS[i]); tprintf(h->avctx, "\n"); }
-                chroma_qp_avg[0] = (h->chroma_qp[0] + get_chroma_qp(h, 0, h->cur_pic.f.qscale_table[mbm_xy]) + 1) >> 1;
-                chroma_qp_avg[1] = (h->chroma_qp[1] + get_chroma_qp(h, 1, h->cur_pic.f.qscale_table[mbm_xy]) + 1) >> 1;
+                chroma_qp_avg[0] = (h->chroma_qp[0] + get_chroma_qp(h, 0, h->cur_pic.qscale_table[mbm_xy]) + 1) >> 1;
+                chroma_qp_avg[1] = (h->chroma_qp[1] + get_chroma_qp(h, 1, h->cur_pic.qscale_table[mbm_xy]) + 1) >> 1;
                 if( dir == 0 ) {
                     filter_mb_edgev( &img_y[0], linesize, bS, qp, a, b, h, 1 );
                     if (chroma) {
@@ -662,7 +662,7 @@
         /* Filter edge */
         // Do not use s->qscale as luma quantizer because it has not the same
         // value in IPCM macroblocks.
-        qp = h->cur_pic.f.qscale_table[mb_xy];
+        qp = h->cur_pic.qscale_table[mb_xy];
         //tprintf(h->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d, QPc:%d, QPcn:%d\n", mb_x, mb_y, dir, edge, qp, h->chroma_qp[0], h->cur_pic.qscale_table[mbn_xy]);
         tprintf(h->avctx, "filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
         //{ int i; for (i = 0; i < 4; i++) tprintf(h->avctx, " bS[%d]:%d", i, bS[i]); tprintf(h->avctx, "\n"); }
@@ -703,16 +703,16 @@
 
 void ff_h264_filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
     const int mb_xy= mb_x + mb_y*h->mb_stride;
-    const int mb_type = h->cur_pic.f.mb_type[mb_xy];
+    const int mb_type = h->cur_pic.mb_type[mb_xy];
     const int mvy_limit = IS_INTERLACED(mb_type) ? 2 : 4;
     int first_vertical_edge_done = 0;
     av_unused int dir;
-    int chroma = CHROMA && !(CONFIG_GRAY && (h->flags&CODEC_FLAG_GRAY));
+    int chroma = CHROMA(h) && !(CONFIG_GRAY && (h->flags&CODEC_FLAG_GRAY));
     int qp_bd_offset = 6 * (h->sps.bit_depth_luma - 8);
     int a = h->slice_alpha_c0_offset - qp_bd_offset;
     int b = h->slice_beta_offset - qp_bd_offset;
 
-    if (FRAME_MBAFF
+    if (FRAME_MBAFF(h)
             // and current and left pair do not have the same interlaced type
             && IS_INTERLACED(mb_type^h->left_type[LTOP])
             // and left mb is in available to us
@@ -741,9 +741,9 @@
                     {3+4*0, 3+4*1, 3+4*2, 3+4*3, 3+4*0, 3+4*1, 3+4*2, 3+4*3},
                 }
             };
-            const uint8_t *off= offset[MB_FIELD][mb_y&1];
+            const uint8_t *off= offset[MB_FIELD(h)][mb_y&1];
             for( i = 0; i < 8; i++ ) {
-                int j= MB_FIELD ? i>>2 : i&1;
+                int j= MB_FIELD(h) ? i>>2 : i&1;
                 int mbn_xy = h->left_mb_xy[LEFT(j)];
                 int mbn_type= h->left_type[LEFT(j)];
 
@@ -752,16 +752,16 @@
                 else{
                     bS[i] = 1 + !!(h->non_zero_count_cache[12+8*(i>>1)] |
                          ((!h->pps.cabac && IS_8x8DCT(mbn_type)) ?
-                            (h->cbp_table[mbn_xy] & (((MB_FIELD ? (i&2) : (mb_y&1)) ? 8 : 2) << 12))
+                            (h->cbp_table[mbn_xy] & (((MB_FIELD(h) ? (i&2) : (mb_y&1)) ? 8 : 2) << 12))
                                                                        :
                             h->non_zero_count[mbn_xy][ off[i] ]));
                 }
             }
         }
 
-        mb_qp   = h->cur_pic.f.qscale_table[mb_xy];
-        mbn0_qp = h->cur_pic.f.qscale_table[h->left_mb_xy[0]];
-        mbn1_qp = h->cur_pic.f.qscale_table[h->left_mb_xy[1]];
+        mb_qp   = h->cur_pic.qscale_table[mb_xy];
+        mbn0_qp = h->cur_pic.qscale_table[h->left_mb_xy[0]];
+        mbn1_qp = h->cur_pic.qscale_table[h->left_mb_xy[1]];
         qp[0] = ( mb_qp + mbn0_qp + 1 ) >> 1;
         bqp[0] = ( get_chroma_qp( h, 0, mb_qp ) +
                    get_chroma_qp( h, 0, mbn0_qp ) + 1 ) >> 1;
@@ -776,16 +776,16 @@
         /* Filter edge */
         tprintf(h->avctx, "filter mb:%d/%d MBAFF, QPy:%d/%d, QPb:%d/%d QPr:%d/%d ls:%d uvls:%d", mb_x, mb_y, qp[0], qp[1], bqp[0], bqp[1], rqp[0], rqp[1], linesize, uvlinesize);
         { int i; for (i = 0; i < 8; i++) tprintf(h->avctx, " bS[%d]:%d", i, bS[i]); tprintf(h->avctx, "\n"); }
-        if(MB_FIELD){
+        if (MB_FIELD(h)) {
             filter_mb_mbaff_edgev ( h, img_y                ,   linesize, bS  , 1, qp [0], a, b, 1 );
             filter_mb_mbaff_edgev ( h, img_y  + 8*  linesize,   linesize, bS+4, 1, qp [1], a, b, 1 );
             if (chroma){
-                if (CHROMA444) {
+                if (CHROMA444(h)) {
                     filter_mb_mbaff_edgev ( h, img_cb,                uvlinesize, bS  , 1, bqp[0], a, b, 1 );
                     filter_mb_mbaff_edgev ( h, img_cb + 8*uvlinesize, uvlinesize, bS+4, 1, bqp[1], a, b, 1 );
                     filter_mb_mbaff_edgev ( h, img_cr,                uvlinesize, bS  , 1, rqp[0], a, b, 1 );
                     filter_mb_mbaff_edgev ( h, img_cr + 8*uvlinesize, uvlinesize, bS+4, 1, rqp[1], a, b, 1 );
-                } else if (CHROMA422) {
+                } else if (CHROMA422(h)) {
                     filter_mb_mbaff_edgecv(h, img_cb,                uvlinesize, bS  , 1, bqp[0], a, b, 1);
                     filter_mb_mbaff_edgecv(h, img_cb + 8*uvlinesize, uvlinesize, bS+4, 1, bqp[1], a, b, 1);
                     filter_mb_mbaff_edgecv(h, img_cr,                uvlinesize, bS  , 1, rqp[0], a, b, 1);
@@ -801,7 +801,7 @@
             filter_mb_mbaff_edgev ( h, img_y              , 2*  linesize, bS  , 2, qp [0], a, b, 1 );
             filter_mb_mbaff_edgev ( h, img_y  +   linesize, 2*  linesize, bS+1, 2, qp [1], a, b, 1 );
             if (chroma){
-                if (CHROMA444) {
+                if (CHROMA444(h)) {
                     filter_mb_mbaff_edgev ( h, img_cb,              2*uvlinesize, bS  , 2, bqp[0], a, b, 1 );
                     filter_mb_mbaff_edgev ( h, img_cb + uvlinesize, 2*uvlinesize, bS+1, 2, bqp[1], a, b, 1 );
                     filter_mb_mbaff_edgev ( h, img_cr,              2*uvlinesize, bS  , 2, rqp[0], a, b, 1 );
diff --git a/libavcodec/h264_mb_template.c b/libavcodec/h264_mb_template.c
index 8830d34..15cb3c9 100644
--- a/libavcodec/h264_mb_template.c
+++ b/libavcodec/h264_mb_template.c
@@ -43,7 +43,7 @@
     const int mb_x    = h->mb_x;
     const int mb_y    = h->mb_y;
     const int mb_xy   = h->mb_xy;
-    const int mb_type = h->cur_pic.f.mb_type[mb_xy];
+    const int mb_type = h->cur_pic.mb_type[mb_xy];
     uint8_t *dest_y, *dest_cb, *dest_cr;
     int linesize, uvlinesize /*dct_offset*/;
     int i, j;
@@ -53,7 +53,7 @@
     const int is_h264 = !CONFIG_SVQ3_DECODER || SIMPLE || h->avctx->codec_id == AV_CODEC_ID_H264;
     void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
     const int block_h   = 16 >> h->chroma_y_shift;
-    const int chroma422 = CHROMA422;
+    const int chroma422 = CHROMA422(h);
 
     dest_y  = h->cur_pic.f.data[0] + ((mb_x << PIXEL_SHIFT)     + mb_y * h->linesize)  * 16;
     dest_cb = h->cur_pic.f.data[1] +  (mb_x << PIXEL_SHIFT) * 8 + mb_y * h->uvlinesize * block_h;
@@ -64,7 +64,7 @@
 
     h->list_counts[mb_xy] = h->list_count;
 
-    if (!SIMPLE && MB_FIELD) {
+    if (!SIMPLE && MB_FIELD(h)) {
         linesize     = h->mb_linesize = h->linesize * 2;
         uvlinesize   = h->mb_uvlinesize = h->uvlinesize * 2;
         block_offset = &h->block_offset[48];
@@ -73,7 +73,7 @@
             dest_cb -= h->uvlinesize * (block_h - 1);
             dest_cr -= h->uvlinesize * (block_h - 1);
         }
-        if (FRAME_MBAFF) {
+        if (FRAME_MBAFF(h)) {
             int list;
             for (list = 0; list < h->list_count; list++) {
                 if (!USES_LIST(mb_type, list))
@@ -272,7 +272,7 @@
     const int mb_x    = h->mb_x;
     const int mb_y    = h->mb_y;
     const int mb_xy   = h->mb_xy;
-    const int mb_type = h->cur_pic.f.mb_type[mb_xy];
+    const int mb_type = h->cur_pic.mb_type[mb_xy];
     uint8_t *dest[3];
     int linesize;
     int i, j, p;
@@ -289,13 +289,13 @@
 
     h->list_counts[mb_xy] = h->list_count;
 
-    if (!SIMPLE && MB_FIELD) {
+    if (!SIMPLE && MB_FIELD(h)) {
         linesize     = h->mb_linesize = h->mb_uvlinesize = h->linesize * 2;
         block_offset = &h->block_offset[48];
         if (mb_y & 1) // FIXME move out of this function?
             for (p = 0; p < 3; p++)
                 dest[p] -= h->linesize * 15;
-        if (FRAME_MBAFF) {
+        if (FRAME_MBAFF(h)) {
             int list;
             for (list = 0; list < h->list_count; list++) {
                 if (!USES_LIST(mb_type, list))
diff --git a/libavcodec/h264_mc_template.c b/libavcodec/h264_mc_template.c
index 2a035af..7aec43b 100644
--- a/libavcodec/h264_mc_template.c
+++ b/libavcodec/h264_mc_template.c
@@ -68,7 +68,7 @@
                               h264_biweight_func *weight_avg)
 {
     const int mb_xy   = h->mb_xy;
-    const int mb_type = h->cur_pic.f.mb_type[mb_xy];
+    const int mb_type = h->cur_pic.mb_type[mb_xy];
 
     av_assert2(IS_INTER(mb_type));
 
diff --git a/libavcodec/h264_mvpred.h b/libavcodec/h264_mvpred.h
index 41670fc..7015d8c 100644
--- a/libavcodec/h264_mvpred.h
+++ b/libavcodec/h264_mvpred.h
@@ -41,30 +41,30 @@
 
     /* there is no consistent mapping of mvs to neighboring locations that will
      * make mbaff happy, so we can't move all this logic to fill_caches */
-    if (FRAME_MBAFF) {
+    if (FRAME_MBAFF(h)) {
 #define SET_DIAG_MV(MV_OP, REF_OP, XY, Y4)                              \
         const int xy = XY, y4 = Y4;                                     \
         const int mb_type = mb_types[xy + (y4 >> 2) * h->mb_stride];    \
         if (!USES_LIST(mb_type, list))                                  \
             return LIST_NOT_USED;                                       \
-        mv = h->cur_pic_ptr->f.motion_val[list][h->mb2b_xy[xy] + 3 + y4 * h->b_stride]; \
+        mv = h->cur_pic_ptr->motion_val[list][h->mb2b_xy[xy] + 3 + y4 * h->b_stride]; \
         h->mv_cache[list][scan8[0] - 2][0] = mv[0];                     \
         h->mv_cache[list][scan8[0] - 2][1] = mv[1] MV_OP;               \
-        return h->cur_pic_ptr->f.ref_index[list][4 * xy + 1 + (y4 & ~1)] REF_OP;
+        return h->cur_pic_ptr->ref_index[list][4 * xy + 1 + (y4 & ~1)] REF_OP;
 
         if (topright_ref == PART_NOT_AVAILABLE
             && i >= scan8[0] + 8 && (i & 7) == 4
             && h->ref_cache[list][scan8[0] - 1] != PART_NOT_AVAILABLE) {
-            const uint32_t *mb_types = h->cur_pic_ptr->f.mb_type;
+            const uint32_t *mb_types = h->cur_pic_ptr->mb_type;
             const int16_t *mv;
             AV_ZERO32(h->mv_cache[list][scan8[0] - 2]);
             *C = h->mv_cache[list][scan8[0] - 2];
 
-            if (!MB_FIELD && IS_INTERLACED(h->left_type[0])) {
+            if (!MB_FIELD(h) && IS_INTERLACED(h->left_type[0])) {
                 SET_DIAG_MV(* 2, >> 1, h->left_mb_xy[0] + h->mb_stride,
                             (h->mb_y & 1) * 2 + (i >> 5));
             }
-            if (MB_FIELD && !IS_INTERLACED(h->left_type[0])) {
+            if (MB_FIELD(h) && !IS_INTERLACED(h->left_type[0])) {
                 // left shift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's OK.
                 SET_DIAG_MV(/ 2, << 1, h->left_mb_xy[i >= 36], ((i >> 2)) & 3);
             }
@@ -230,8 +230,8 @@
 }
 
 #define FIX_MV_MBAFF(type, refn, mvn, idx)      \
-    if (FRAME_MBAFF) {                          \
-        if (MB_FIELD) {                         \
+    if (FRAME_MBAFF(h)) {                       \
+        if (MB_FIELD(h)) {                      \
             if (!IS_INTERLACED(type)) {         \
                 refn <<= 1;                     \
                 AV_COPY32(mvbuf[idx], mvn);     \
@@ -252,8 +252,8 @@
 {
     DECLARE_ALIGNED(4, static const int16_t, zeromv)[2] = { 0 };
     DECLARE_ALIGNED(4, int16_t, mvbuf)[3][2];
-    int8_t *ref     = h->cur_pic.f.ref_index[0];
-    int16_t(*mv)[2] = h->cur_pic.f.motion_val[0];
+    int8_t *ref     = h->cur_pic.ref_index[0];
+    int16_t(*mv)[2] = h->cur_pic.motion_val[0];
     int top_ref, left_ref, diagonal_ref, match_count, mx, my;
     const int16_t *A, *B, *C;
     int b_stride = h->b_stride;
@@ -359,7 +359,7 @@
 
     h->topleft_partition = -1;
 
-    top_xy = mb_xy - (h->mb_stride << MB_FIELD);
+    top_xy = mb_xy - (h->mb_stride << MB_FIELD(h));
 
     /* Wow, what a mess, why didn't they simplify the interlacing & intra
      * stuff, I can't imagine that these complex rules are worth it. */
@@ -368,8 +368,8 @@
     topright_xy   = top_xy + 1;
     left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
     h->left_block = left_block_options[0];
-    if (FRAME_MBAFF) {
-        const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.f.mb_type[mb_xy - 1]);
+    if (FRAME_MBAFF(h)) {
+        const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
         const int curr_mb_field_flag = IS_INTERLACED(mb_type);
         if (h->mb_y & 1) {
             if (left_mb_field_flag != curr_mb_field_flag) {
@@ -387,9 +387,9 @@
             }
         } else {
             if (curr_mb_field_flag) {
-                topleft_xy  += h->mb_stride & (((h->cur_pic.f.mb_type[top_xy - 1] >> 7) & 1) - 1);
-                topright_xy += h->mb_stride & (((h->cur_pic.f.mb_type[top_xy + 1] >> 7) & 1) - 1);
-                top_xy      += h->mb_stride & (((h->cur_pic.f.mb_type[top_xy]     >> 7) & 1) - 1);
+                topleft_xy  += h->mb_stride & (((h->cur_pic.mb_type[top_xy - 1] >> 7) & 1) - 1);
+                topright_xy += h->mb_stride & (((h->cur_pic.mb_type[top_xy + 1] >> 7) & 1) - 1);
+                top_xy      += h->mb_stride & (((h->cur_pic.mb_type[top_xy]     >> 7) & 1) - 1);
             }
             if (left_mb_field_flag != curr_mb_field_flag) {
                 if (curr_mb_field_flag) {
@@ -409,11 +409,11 @@
     h->left_mb_xy[LBOT] = left_xy[LBOT];
     //FIXME do we need all in the context?
 
-    h->topleft_type    = h->cur_pic.f.mb_type[topleft_xy];
-    h->top_type        = h->cur_pic.f.mb_type[top_xy];
-    h->topright_type   = h->cur_pic.f.mb_type[topright_xy];
-    h->left_type[LTOP] = h->cur_pic.f.mb_type[left_xy[LTOP]];
-    h->left_type[LBOT] = h->cur_pic.f.mb_type[left_xy[LBOT]];
+    h->topleft_type    = h->cur_pic.mb_type[topleft_xy];
+    h->top_type        = h->cur_pic.mb_type[top_xy];
+    h->topright_type   = h->cur_pic.mb_type[topright_xy];
+    h->left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
+    h->left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
 
     if (FMO) {
         if (h->slice_table[topleft_xy] != h->slice_num)
@@ -479,7 +479,7 @@
                         h->left_samples_available    &= 0xFF5F;
                     }
                 } else {
-                    int left_typei = h->cur_pic.f.mb_type[left_xy[LTOP] + h->mb_stride];
+                    int left_typei = h->cur_pic.mb_type[left_xy[LTOP] + h->mb_stride];
 
                     av_assert2(left_xy[LTOP] == left_xy[LBOT]);
                     if (!((left_typei & type_mask) && (left_type[LTOP] & type_mask))) {
@@ -544,7 +544,7 @@
                 AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 9]);
             }
         } else {
-            uint32_t top_empty = CABAC && !IS_INTRA(mb_type) ? 0 : 0x40404040;
+            uint32_t top_empty = CABAC(h) && !IS_INTRA(mb_type) ? 0 : 0x40404040;
             AV_WN32A(&nnz_cache[4 + 8 *  0], top_empty);
             AV_WN32A(&nnz_cache[4 + 8 *  5], top_empty);
             AV_WN32A(&nnz_cache[4 + 8 * 10], top_empty);
@@ -555,12 +555,12 @@
                 nnz = h->non_zero_count[left_xy[LEFT(i)]];
                 nnz_cache[3 + 8 * 1 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i]];
                 nnz_cache[3 + 8 * 2 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i]];
-                if (CHROMA444) {
+                if (CHROMA444(h)) {
                     nnz_cache[3 + 8 *  6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 4 * 4];
                     nnz_cache[3 + 8 *  7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 4 * 4];
                     nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 8 * 4];
                     nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 8 * 4];
-                } else if (CHROMA422) {
+                } else if (CHROMA422(h)) {
                     nnz_cache[3 + 8 *  6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 4 * 4];
                     nnz_cache[3 + 8 *  7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 4 * 4];
                     nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 8 * 4];
@@ -575,11 +575,11 @@
                 nnz_cache[3 + 8 *  6 + 2 * 8 * i] =
                 nnz_cache[3 + 8 *  7 + 2 * 8 * i] =
                 nnz_cache[3 + 8 * 11 + 2 * 8 * i] =
-                nnz_cache[3 + 8 * 12 + 2 * 8 * i] = CABAC && !IS_INTRA(mb_type) ? 0 : 64;
+                nnz_cache[3 + 8 * 12 + 2 * 8 * i] = CABAC(h) && !IS_INTRA(mb_type) ? 0 : 64;
             }
         }
 
-        if (CABAC) {
+        if (CABAC(h)) {
             // top_cbp
             if (top_type)
                 h->top_cbp = h->cbp_table[top_xy];
@@ -601,9 +601,9 @@
         int b_stride = h->b_stride;
         for (list = 0; list < h->list_count; list++) {
             int8_t *ref_cache = &h->ref_cache[list][scan8[0]];
-            int8_t *ref       = h->cur_pic.f.ref_index[list];
+            int8_t *ref       = h->cur_pic.ref_index[list];
             int16_t(*mv_cache)[2] = &h->mv_cache[list][scan8[0]];
-            int16_t(*mv)[2]       = h->cur_pic.f.motion_val[list];
+            int16_t(*mv)[2]       = h->cur_pic.motion_val[list];
             if (!USES_LIST(mb_type, list))
                 continue;
             av_assert2(!(IS_DIRECT(mb_type) && !h->direct_spatial_mv_pred));
@@ -677,7 +677,7 @@
                 }
             }
 
-            if ((mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2)) && !FRAME_MBAFF)
+            if ((mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2)) && !FRAME_MBAFF(h))
                 continue;
 
             if (!(mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2))) {
@@ -688,7 +688,7 @@
                 AV_ZERO32(mv_cache[2 + 8 * 0]);
                 AV_ZERO32(mv_cache[2 + 8 * 2]);
 
-                if (CABAC) {
+                if (CABAC(h)) {
                     if (USES_LIST(top_type, list)) {
                         const int b_xy = h->mb2br_xy[top_xy];
                         AV_COPY64(mvd_cache[0 - 1 * 8], mvd[b_xy + 0]);
@@ -759,8 +759,8 @@
     MAP_F2F(scan8[0] - 1 + 2 * 8, left_type[LBOT])                      \
     MAP_F2F(scan8[0] - 1 + 3 * 8, left_type[LBOT])
 
-            if (FRAME_MBAFF) {
-                if (MB_FIELD) {
+            if (FRAME_MBAFF(h)) {
+                if (MB_FIELD(h)) {
 
 #define MAP_F2F(idx, mb_type)                                           \
     if (!IS_INTERLACED(mb_type) && h->ref_cache[list][idx] >= 0) {      \
@@ -800,7 +800,7 @@
 
     memset(h->non_zero_count[mb_xy], 0, 48);
 
-    if (MB_FIELD)
+    if (MB_FIELD(h))
         mb_type |= MB_TYPE_INTERLACED;
 
     if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
@@ -820,8 +820,8 @@
     }
 
     write_back_motion(h, mb_type);
-    h->cur_pic.f.mb_type[mb_xy]      = mb_type;
-    h->cur_pic.f.qscale_table[mb_xy] = h->qscale;
+    h->cur_pic.mb_type[mb_xy]      = mb_type;
+    h->cur_pic.qscale_table[mb_xy] = h->qscale;
     h->slice_table[mb_xy]            = h->slice_num;
     h->prev_mb_skipped               = 1;
 }
diff --git a/libavcodec/h264_parser.c b/libavcodec/h264_parser.c
index 14ae67b..f373a1c 100644
--- a/libavcodec/h264_parser.c
+++ b/libavcodec/h264_parser.c
@@ -28,6 +28,7 @@
 #include "parser.h"
 #include "h264data.h"
 #include "golomb.h"
+#include "internal.h"
 
 
 static int ff_h264_find_frame_end(H264Context *h, const uint8_t *buf, int buf_size)
@@ -174,7 +175,7 @@
             }
             src_length = nalsize;
         } else {
-        buf = avpriv_mpv_find_start_code(buf, buf_end, &state);
+        buf = avpriv_find_start_code(buf, buf_end, &state);
         if(buf >= buf_end)
             break;
         --buf;
diff --git a/libavcodec/h264_ps.c b/libavcodec/h264_ps.c
index 8638ce2..68f504a 100644
--- a/libavcodec/h264_ps.c
+++ b/libavcodec/h264_ps.c
@@ -459,44 +459,45 @@
 #endif
     sps->crop= get_bits1(&h->gb);
     if(sps->crop){
-        int crop_vertical_limit   = sps->chroma_format_idc  & 2 ? 16 : 8;
-        int crop_horizontal_limit = sps->chroma_format_idc == 3 ? 16 : 8;
-        sps->crop_left  = get_ue_golomb(&h->gb);
-        sps->crop_right = get_ue_golomb(&h->gb);
-        sps->crop_top   = get_ue_golomb(&h->gb);
-        sps->crop_bottom= get_ue_golomb(&h->gb);
+        int crop_left   = get_ue_golomb(&h->gb);
+        int crop_right  = get_ue_golomb(&h->gb);
+        int crop_top    = get_ue_golomb(&h->gb);
+        int crop_bottom = get_ue_golomb(&h->gb);
+
         if (h->avctx->flags2 & CODEC_FLAG2_IGNORE_CROP) {
-            av_log(h->avctx, AV_LOG_DEBUG,
-                   "discarding sps cropping, "
-                   "original values are l:%u r:%u t:%u b:%u\n",
-                   sps->crop_left,
-                   sps->crop_right,
-                   sps->crop_top,
-                   sps->crop_bottom);
+            av_log(h->avctx, AV_LOG_DEBUG, "discarding sps cropping, original "
+                   "values are l:%u r:%u t:%u b:%u\n", crop_left, crop_right,
+                   crop_top, crop_bottom);
 
             sps->crop_left   =
             sps->crop_right  =
             sps->crop_top    =
             sps->crop_bottom = 0;
-        }
-        if(sps->crop_left || sps->crop_top){
-            av_log(h->avctx, AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ... (left: %d, top: %d)\n", sps->crop_left, sps->crop_top);
-        }
-        if(sps->crop_right >= crop_horizontal_limit || sps->crop_bottom >= crop_vertical_limit){
-            av_log(h->avctx, AV_LOG_ERROR, "brainfart cropping not supported, cropping disabled (right: %d, bottom: %d)\n", sps->crop_right, sps->crop_bottom);
-        /* It is very unlikely that partial cropping will make anybody happy.
-         * Not cropping at all fixes for example playback of Sisvel 3D streams
-         * in applications supporting Sisvel 3D. */
-        sps->crop_left  =
-        sps->crop_right =
-        sps->crop_top   =
-        sps->crop_bottom= 0;
+        } else {
+            int vsub = (sps->chroma_format_idc == 1) ? 1 : 0;
+            int hsub = (sps->chroma_format_idc == 1 || sps->chroma_format_idc == 2) ? 1 : 0;
+            int step_x = 1 << hsub;
+            int step_y = (2 - sps->frame_mbs_only_flag) << vsub;
+
+            if (crop_left & (0x1F >> (sps->bit_depth_luma > 8)) &&
+                !(h->avctx->flags & CODEC_FLAG_UNALIGNED)) {
+                crop_left &= ~(0x1F >> (sps->bit_depth_luma > 8));
+                av_log(h->avctx, AV_LOG_WARNING, "Reducing left cropping to %d "
+                       "chroma samples to preserve alignment.\n",
+                       crop_left);
+            }
+
+            sps->crop_left   = crop_left   * step_x;
+            sps->crop_right  = crop_right  * step_x;
+            sps->crop_top    = crop_top    * step_y;
+            sps->crop_bottom = crop_bottom * step_y;
         }
     }else{
         sps->crop_left  =
         sps->crop_right =
         sps->crop_top   =
         sps->crop_bottom= 0;
+        sps->crop = 0;
     }
 
     sps->vui_parameters_present_flag= get_bits1(&h->gb);
diff --git a/libavcodec/h264_refs.c b/libavcodec/h264_refs.c
index d87106e..4b69a90 100644
--- a/libavcodec/h264_refs.c
+++ b/libavcodec/h264_refs.c
@@ -34,25 +34,32 @@
 //#undef NDEBUG
 #include <assert.h>
 
+#define COPY_PICTURE(dst, src) \
+do {\
+    *(dst) = *(src);\
+    (dst)->f.extended_data = (dst)->f.data;\
+    (dst)->tf.f = &(dst)->f;\
+} while (0)
+
 
 static void pic_as_field(Picture *pic, const int parity){
     int i;
     for (i = 0; i < 4; ++i) {
         if (parity == PICT_BOTTOM_FIELD)
             pic->f.data[i] += pic->f.linesize[i];
-        pic->f.reference    = parity;
+        pic->reference      = parity;
         pic->f.linesize[i] *= 2;
     }
     pic->poc= pic->field_poc[parity == PICT_BOTTOM_FIELD];
 }
 
-static int split_field_copy(Picture *dest, Picture *src,
-                            int parity, int id_add){
-    int match = !!(src->f.reference & parity);
+static int split_field_copy(Picture *dest, Picture *src, int parity, int id_add)
+{
+    int match = !!(src->reference & parity);
 
     if (match) {
-        *dest = *src;
-        if(parity != PICT_FRAME){
+        COPY_PICTURE(dest, src);
+        if (parity != PICT_FRAME) {
             pic_as_field(dest, parity);
             dest->pic_id *= 2;
             dest->pic_id += id_add;
@@ -62,94 +69,107 @@
     return match;
 }
 
-static int build_def_list(Picture *def, Picture **in, int len, int is_long, int sel){
-    int i[2]={0};
-    int index=0;
+static int build_def_list(Picture *def, Picture **in, int len, int is_long, int sel)
+{
+    int  i[2] = { 0 };
+    int index = 0;
 
-    while(i[0]<len || i[1]<len){
-        while (i[0] < len && !(in[ i[0] ] && (in[ i[0] ]->f.reference & sel)))
+    while (i[0] < len || i[1] < len) {
+        while (i[0] < len && !(in[i[0]] && (in[i[0]]->reference & sel)))
             i[0]++;
-        while (i[1] < len && !(in[ i[1] ] && (in[ i[1] ]->f.reference & (sel^3))))
+        while (i[1] < len && !(in[i[1]] && (in[i[1]]->reference & (sel ^ 3))))
             i[1]++;
-        if(i[0] < len){
-            in[ i[0] ]->pic_id= is_long ? i[0] : in[ i[0] ]->frame_num;
-            split_field_copy(&def[index++], in[ i[0]++ ], sel  , 1);
+        if (i[0] < len) {
+            in[i[0]]->pic_id = is_long ? i[0] : in[i[0]]->frame_num;
+            split_field_copy(&def[index++], in[i[0]++], sel, 1);
         }
-        if(i[1] < len){
-            in[ i[1] ]->pic_id= is_long ? i[1] : in[ i[1] ]->frame_num;
-            split_field_copy(&def[index++], in[ i[1]++ ], sel^3, 0);
+        if (i[1] < len) {
+            in[i[1]]->pic_id = is_long ? i[1] : in[i[1]]->frame_num;
+            split_field_copy(&def[index++], in[i[1]++], sel ^ 3, 0);
         }
     }
 
     return index;
 }
 
-static int add_sorted(Picture **sorted, Picture **src, int len, int limit, int dir){
+static int add_sorted(Picture **sorted, Picture **src, int len, int limit, int dir)
+{
     int i, best_poc;
-    int out_i= 0;
+    int out_i = 0;
 
-    for(;;){
-        best_poc= dir ? INT_MIN : INT_MAX;
+    for (;;) {
+        best_poc = dir ? INT_MIN : INT_MAX;
 
-        for(i=0; i<len; i++){
-            const int poc= src[i]->poc;
-            if(((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)){
-                best_poc= poc;
-                sorted[out_i]= src[i];
+        for (i = 0; i < len; i++) {
+            const int poc = src[i]->poc;
+            if (((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)) {
+                best_poc      = poc;
+                sorted[out_i] = src[i];
             }
         }
-        if(best_poc == (dir ? INT_MIN : INT_MAX))
+        if (best_poc == (dir ? INT_MIN : INT_MAX))
             break;
-        limit= sorted[out_i++]->poc - dir;
+        limit = sorted[out_i++]->poc - dir;
     }
     return out_i;
 }
 
-int ff_h264_fill_default_ref_list(H264Context *h){
+int ff_h264_fill_default_ref_list(H264Context *h)
+{
     int i, len;
 
-    if(h->slice_type_nos==AV_PICTURE_TYPE_B){
+    if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
         Picture *sorted[32];
         int cur_poc, list;
         int lens[2];
 
-        if(FIELD_PICTURE)
-            cur_poc= h->cur_pic_ptr->field_poc[h->picture_structure == PICT_BOTTOM_FIELD];
+        if (FIELD_PICTURE(h))
+            cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure == PICT_BOTTOM_FIELD];
         else
-            cur_poc= h->cur_pic_ptr->poc;
+            cur_poc = h->cur_pic_ptr->poc;
 
-        for(list= 0; list<2; list++){
-            len= add_sorted(sorted    , h->short_ref, h->short_ref_count, cur_poc, 1^list);
-            len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list);
-            av_assert0(len<=32);
-            len= build_def_list(h->default_ref_list[list]    , sorted     , len, 0, h->picture_structure);
-            len+=build_def_list(h->default_ref_list[list]+len, h->long_ref, 16 , 1, h->picture_structure);
-            av_assert0(len<=32);
+        for (list = 0; list < 2; list++) {
+            len  = add_sorted(sorted,       h->short_ref, h->short_ref_count, cur_poc, 1 ^ list);
+            len += add_sorted(sorted + len, h->short_ref, h->short_ref_count, cur_poc, 0 ^ list);
+            av_assert0(len <= 32);
+            len  = build_def_list(h->default_ref_list[list],       sorted,      len, 0, h->picture_structure);
+            len += build_def_list(h->default_ref_list[list] + len, h->long_ref, 16,  1, h->picture_structure);
+            av_assert0(len <= 32);
 
-            if(len < h->ref_count[list])
-                memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len));
-            lens[list]= len;
+            if (len < h->ref_count[list])
+                memset(&h->default_ref_list[list][len], 0, sizeof(Picture) * (h->ref_count[list] - len));
+            lens[list] = len;
         }
 
-        if(lens[0] == lens[1] && lens[1] > 1){
+        if (lens[0] == lens[1] && lens[1] > 1) {
             for (i = 0; h->default_ref_list[0][i].f.data[0] == h->default_ref_list[1][i].f.data[0] && i < lens[0]; i++);
-            if(i == lens[0])
-                FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);
+            if (i == lens[0]) {
+                Picture tmp;
+                COPY_PICTURE(&tmp, &h->default_ref_list[1][0]);
+                COPY_PICTURE(&h->default_ref_list[1][0], &h->default_ref_list[1][1]);
+                COPY_PICTURE(&h->default_ref_list[1][1], &tmp);
+            }
         }
-    }else{
-        len = build_def_list(h->default_ref_list[0]    , h->short_ref, h->short_ref_count, 0, h->picture_structure);
-        len+= build_def_list(h->default_ref_list[0]+len, h-> long_ref, 16                , 1, h->picture_structure);
-        av_assert0(len<=32);
-        if(len < h->ref_count[0])
-            memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len));
+    } else {
+        len  = build_def_list(h->default_ref_list[0],       h->short_ref, h->short_ref_count, 0, h->picture_structure);
+        len += build_def_list(h->default_ref_list[0] + len, h-> long_ref, 16,                 1, h->picture_structure);
+        av_assert0(len <= 32);
+        if (len < h->ref_count[0])
+            memset(&h->default_ref_list[0][len], 0, sizeof(Picture) * (h->ref_count[0] - len));
     }
 #ifdef TRACE
-    for (i=0; i<h->ref_count[0]; i++) {
-        tprintf(h->avctx, "List0: %s fn:%d 0x%p\n", (h->default_ref_list[0][i].long_ref ? "LT" : "ST"), h->default_ref_list[0][i].pic_id, h->default_ref_list[0][i].f.data[0]);
+    for (i = 0; i < h->ref_count[0]; i++) {
+        tprintf(h->avctx, "List0: %s fn:%d 0x%p\n",
+                (h->default_ref_list[0][i].long_ref ? "LT" : "ST"),
+                h->default_ref_list[0][i].pic_id,
+                h->default_ref_list[0][i].f.data[0]);
     }
-    if(h->slice_type_nos==AV_PICTURE_TYPE_B){
-        for (i=0; i<h->ref_count[1]; i++) {
-            tprintf(h->avctx, "List1: %s fn:%d 0x%p\n", (h->default_ref_list[1][i].long_ref ? "LT" : "ST"), h->default_ref_list[1][i].pic_id, h->default_ref_list[1][i].f.data[0]);
+    if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
+        for (i = 0; i < h->ref_count[1]; i++) {
+            tprintf(h->avctx, "List1: %s fn:%d 0x%p\n",
+                    (h->default_ref_list[1][i].long_ref ? "LT" : "ST"),
+                    h->default_ref_list[1][i].pic_id,
+                    h->default_ref_list[1][i].f.data[0]);
         }
     }
 #endif
@@ -169,9 +189,10 @@
  * @return frame number (short term) or long term index of picture
  *         described by pic_num
  */
-static int pic_num_extract(H264Context *h, int pic_num, int *structure){
+static int pic_num_extract(H264Context *h, int pic_num, int *structure)
+{
     *structure = h->picture_structure;
-    if(FIELD_PICTURE){
+    if (FIELD_PICTURE(h)) {
         if (!(pic_num & 1))
             /* opposite field */
             *structure ^= PICT_FRAME;
@@ -181,78 +202,80 @@
     return pic_num;
 }
 
-int ff_h264_decode_ref_pic_list_reordering(H264Context *h){
-    int list, index, pic_structure;
+int ff_h264_decode_ref_pic_list_reordering(H264Context *h)
+{
+    int list, index, pic_structure, i;
 
     print_short_term(h);
     print_long_term(h);
 
-    for(list=0; list<h->list_count; list++){
-        memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
+    for (list = 0; list < h->list_count; list++) {
+        for (i = 0; i < h->ref_count[list]; i++)
+            COPY_PICTURE(&h->ref_list[list][i], &h->default_ref_list[list][i]);
 
-        if(get_bits1(&h->gb)){
-            int pred= h->curr_pic_num;
+        if (get_bits1(&h->gb)) {
+            int pred = h->curr_pic_num;
 
-            for(index=0; ; index++){
-                unsigned int reordering_of_pic_nums_idc= get_ue_golomb_31(&h->gb);
+            for (index = 0; ; index++) {
+                unsigned int reordering_of_pic_nums_idc = get_ue_golomb_31(&h->gb);
                 unsigned int pic_id;
                 int i;
                 Picture *ref = NULL;
 
-                if(reordering_of_pic_nums_idc==3)
+                if (reordering_of_pic_nums_idc == 3)
                     break;
 
-                if(index >= h->ref_count[list]){
+                if (index >= h->ref_count[list]) {
                     av_log(h->avctx, AV_LOG_ERROR, "reference count overflow\n");
                     return -1;
                 }
 
-                if(reordering_of_pic_nums_idc<3){
-                    if(reordering_of_pic_nums_idc<2){
-                        const unsigned int abs_diff_pic_num= get_ue_golomb(&h->gb) + 1;
+                if (reordering_of_pic_nums_idc < 3) {
+                    if (reordering_of_pic_nums_idc < 2) {
+                        const unsigned int abs_diff_pic_num = get_ue_golomb(&h->gb) + 1;
                         int frame_num;
 
-                        if(abs_diff_pic_num > h->max_pic_num){
+                        if (abs_diff_pic_num > h->max_pic_num) {
                             av_log(h->avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
                             return -1;
                         }
 
-                        if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
-                        else                                pred+= abs_diff_pic_num;
+                        if (reordering_of_pic_nums_idc == 0)
+                            pred -= abs_diff_pic_num;
+                        else
+                            pred += abs_diff_pic_num;
                         pred &= h->max_pic_num - 1;
 
                         frame_num = pic_num_extract(h, pred, &pic_structure);
 
-                        for(i= h->short_ref_count-1; i>=0; i--){
+                        for (i = h->short_ref_count - 1; i >= 0; i--) {
                             ref = h->short_ref[i];
-                            assert(ref->f.reference);
+                            assert(ref->reference);
                             assert(!ref->long_ref);
-                            if(
-                                   ref->frame_num == frame_num &&
-                                   (ref->f.reference & pic_structure)
-                              )
+                            if (ref->frame_num == frame_num &&
+                                (ref->reference & pic_structure))
                                 break;
                         }
-                        if(i>=0)
-                            ref->pic_id= pred;
-                    }else{
+                        if (i >= 0)
+                            ref->pic_id = pred;
+                    } else {
                         int long_idx;
-                        pic_id= get_ue_golomb(&h->gb); //long_term_pic_idx
+                        pic_id = get_ue_golomb(&h->gb); //long_term_pic_idx
 
-                        long_idx= pic_num_extract(h, pic_id, &pic_structure);
+                        long_idx = pic_num_extract(h, pic_id, &pic_structure);
 
-                        if(long_idx>31){
+                        if (long_idx > 31) {
                             av_log(h->avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
                             return -1;
                         }
                         ref = h->long_ref[long_idx];
-                        assert(!(ref && !ref->f.reference));
-                        if (ref && (ref->f.reference & pic_structure)) {
-                            ref->pic_id= pic_id;
+                        assert(!(ref && !ref->reference));
+                        if (ref && (ref->reference & pic_structure)) {
+                            ref->pic_id = pic_id;
                             assert(ref->long_ref);
-                            i=0;
-                        }else{
-                            i=-1;
+                            i = 0;
+                        } else {
+                            i = -1;
                         }
                     }
 
@@ -260,65 +283,70 @@
                         av_log(h->avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
                         memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
                     } else {
-                        for(i=index; i+1<h->ref_count[list]; i++){
-                            if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
+                        for (i = index; i + 1 < h->ref_count[list]; i++) {
+                            if (ref->long_ref == h->ref_list[list][i].long_ref &&
+                                ref->pic_id   == h->ref_list[list][i].pic_id)
                                 break;
                         }
-                        for(; i > index; i--){
-                            h->ref_list[list][i]= h->ref_list[list][i-1];
+                        for (; i > index; i--) {
+                            COPY_PICTURE(&h->ref_list[list][i], &h->ref_list[list][i - 1]);
                         }
-                        h->ref_list[list][index]= *ref;
-                        if (FIELD_PICTURE){
+                        COPY_PICTURE(&h->ref_list[list][index], ref);
+                        if (FIELD_PICTURE(h)) {
                             pic_as_field(&h->ref_list[list][index], pic_structure);
                         }
                     }
-                }else{
+                } else {
                     av_log(h->avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
                     return -1;
                 }
             }
         }
     }
-    for(list=0; list<h->list_count; list++){
-        for(index= 0; index < h->ref_count[list]; index++){
-            if (!h->ref_list[list][index].f.data[0]) {
+    for (list = 0; list < h->list_count; list++) {
+        for (index = 0; index < h->ref_count[list]; index++) {
+            if (   !h->ref_list[list][index].f.data[0]
+                || (!FIELD_PICTURE(h) && (h->ref_list[list][index].reference&3) != 3)) {
                 int i;
                 av_log(h->avctx, AV_LOG_ERROR, "Missing reference picture, default is %d\n", h->default_ref_list[list][0].poc);
-                for (i=0; i<FF_ARRAY_ELEMS(h->last_pocs); i++)
+                for (i = 0; i < FF_ARRAY_ELEMS(h->last_pocs); i++)
                     h->last_pocs[i] = INT_MIN;
-                if (h->default_ref_list[list][0].f.data[0])
-                    h->ref_list[list][index]= h->default_ref_list[list][0];
+                if (h->default_ref_list[list][0].f.data[0]
+                    && !(!FIELD_PICTURE(h) && (h->default_ref_list[list][0].reference&3) != 3))
+                    COPY_PICTURE(&h->ref_list[list][index], &h->default_ref_list[list][0]);
                 else
                     return -1;
             }
+            av_assert0(av_buffer_get_ref_count(h->ref_list[list][index].f.buf[0]) > 0);
         }
     }
 
     return 0;
 }
 
-void ff_h264_fill_mbaff_ref_list(H264Context *h){
+void ff_h264_fill_mbaff_ref_list(H264Context *h)
+{
     int list, i, j;
-    for(list=0; list<h->list_count; list++){
-        for(i=0; i<h->ref_count[list]; i++){
+    for (list = 0; list < h->list_count; list++) {
+        for (i = 0; i < h->ref_count[list]; i++) {
             Picture *frame = &h->ref_list[list][i];
-            Picture *field = &h->ref_list[list][16+2*i];
-            field[0] = *frame;
-            for(j=0; j<3; j++)
+            Picture *field = &h->ref_list[list][16 + 2 * i];
+            COPY_PICTURE(field, frame);
+            for (j = 0; j < 3; j++)
                 field[0].f.linesize[j] <<= 1;
-            field[0].f.reference = PICT_TOP_FIELD;
-            field[0].poc= field[0].field_poc[0];
-            field[1] = field[0];
-            for(j=0; j<3; j++)
+            field[0].reference = PICT_TOP_FIELD;
+            field[0].poc       = field[0].field_poc[0];
+            COPY_PICTURE(field + 1, field);
+            for (j = 0; j < 3; j++)
                 field[1].f.data[j] += frame->f.linesize[j];
-            field[1].f.reference = PICT_BOTTOM_FIELD;
-            field[1].poc= field[1].field_poc[1];
+            field[1].reference = PICT_BOTTOM_FIELD;
+            field[1].poc       = field[1].field_poc[1];
 
-            h->luma_weight[16+2*i][list][0] = h->luma_weight[16+2*i+1][list][0] = h->luma_weight[i][list][0];
-            h->luma_weight[16+2*i][list][1] = h->luma_weight[16+2*i+1][list][1] = h->luma_weight[i][list][1];
-            for(j=0; j<2; j++){
-                h->chroma_weight[16+2*i][list][j][0] = h->chroma_weight[16+2*i+1][list][j][0] = h->chroma_weight[i][list][j][0];
-                h->chroma_weight[16+2*i][list][j][1] = h->chroma_weight[16+2*i+1][list][j][1] = h->chroma_weight[i][list][j][1];
+            h->luma_weight[16 + 2 * i][list][0] = h->luma_weight[16 + 2 * i + 1][list][0] = h->luma_weight[i][list][0];
+            h->luma_weight[16 + 2 * i][list][1] = h->luma_weight[16 + 2 * i + 1][list][1] = h->luma_weight[i][list][1];
+            for (j = 0; j < 2; j++) {
+                h->chroma_weight[16 + 2 * i][list][j][0] = h->chroma_weight[16 + 2 * i + 1][list][j][0] = h->chroma_weight[i][list][j][0];
+                h->chroma_weight[16 + 2 * i][list][j][1] = h->chroma_weight[16 + 2 * i + 1][list][j][1] = h->chroma_weight[i][list][j][1];
             }
         }
     }
@@ -335,14 +363,15 @@
  *         for display purposes) zero if one of the fields remains in
  *         reference
  */
-static inline int unreference_pic(H264Context *h, Picture *pic, int refmask){
+static inline int unreference_pic(H264Context *h, Picture *pic, int refmask)
+{
     int i;
-    if (pic->f.reference &= refmask) {
+    if (pic->reference &= refmask) {
         return 0;
     } else {
         for(i = 0; h->delayed_pic[i]; i++)
             if(pic == h->delayed_pic[i]){
-                pic->f.reference = DELAYED_PIC_REF;
+                pic->reference = DELAYED_PIC_REF;
                 break;
             }
         return 1;
@@ -357,14 +386,15 @@
  * @return pointer to the found picture, or NULL if no pic with the provided
  *                 frame number is found
  */
-static Picture * find_short(H264Context *h, int frame_num, int *idx){
+static Picture *find_short(H264Context *h, int frame_num, int *idx)
+{
     int i;
 
-    for(i=0; i<h->short_ref_count; i++){
-        Picture *pic= h->short_ref[i];
-        if(h->avctx->debug&FF_DEBUG_MMCO)
+    for (i = 0; i < h->short_ref_count; i++) {
+        Picture *pic = h->short_ref[i];
+        if (h->avctx->debug & FF_DEBUG_MMCO)
             av_log(h->avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
-        if(pic->frame_num == frame_num) {
+        if (pic->frame_num == frame_num) {
             *idx = i;
             return pic;
         }
@@ -378,28 +408,31 @@
  * to be valid. Other list entries are shifted down.
  * @param i index into h->short_ref of picture to remove.
  */
-static void remove_short_at_index(H264Context *h, int i){
+static void remove_short_at_index(H264Context *h, int i)
+{
     assert(i >= 0 && i < h->short_ref_count);
-    h->short_ref[i]= NULL;
+    h->short_ref[i] = NULL;
     if (--h->short_ref_count)
-        memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i)*sizeof(Picture*));
+        memmove(&h->short_ref[i], &h->short_ref[i + 1],
+                (h->short_ref_count - i) * sizeof(Picture*));
 }
 
 /**
  *
  * @return the removed picture or NULL if an error occurs
  */
-static Picture * remove_short(H264Context *h, int frame_num, int ref_mask){
+static Picture *remove_short(H264Context *h, int frame_num, int ref_mask)
+{
     Picture *pic;
     int i;
 
-    if(h->avctx->debug&FF_DEBUG_MMCO)
+    if (h->avctx->debug & FF_DEBUG_MMCO)
         av_log(h->avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
 
     pic = find_short(h, frame_num, &i);
-    if (pic){
-        if(unreference_pic(h, pic, ref_mask))
-        remove_short_at_index(h, i);
+    if (pic) {
+        if (unreference_pic(h, pic, ref_mask))
+            remove_short_at_index(h, i);
     }
 
     return pic;
@@ -410,15 +443,16 @@
  * that list.
  * @return the removed picture or NULL if an error occurs
  */
-static Picture * remove_long(H264Context *h, int i, int ref_mask){
+static Picture *remove_long(H264Context *h, int i, int ref_mask)
+{
     Picture *pic;
 
-    pic= h->long_ref[i];
-    if (pic){
-        if(unreference_pic(h, pic, ref_mask)){
+    pic = h->long_ref[i];
+    if (pic) {
+        if (unreference_pic(h, pic, ref_mask)) {
             assert(h->long_ref[i]->long_ref == 1);
-            h->long_ref[i]->long_ref= 0;
-            h->long_ref[i]= NULL;
+            h->long_ref[i]->long_ref = 0;
+            h->long_ref[i]           = NULL;
             h->long_ref_count--;
         }
     }
@@ -426,19 +460,20 @@
     return pic;
 }
 
-void ff_h264_remove_all_refs(H264Context *h){
+void ff_h264_remove_all_refs(H264Context *h)
+{
     int i;
 
-    for(i=0; i<16; i++){
+    for (i = 0; i < 16; i++) {
         remove_long(h, i, 0);
     }
-    assert(h->long_ref_count==0);
+    assert(h->long_ref_count == 0);
 
-    for(i=0; i<h->short_ref_count; i++){
+    for (i = 0; i < h->short_ref_count; i++) {
         unreference_pic(h, h->short_ref[i], 0);
-        h->short_ref[i]= NULL;
+        h->short_ref[i] = NULL;
     }
-    h->short_ref_count=0;
+    h->short_ref_count = 0;
 
     memset(h->default_ref_list, 0, sizeof(h->default_ref_list));
     memset(h->ref_list, 0, sizeof(h->ref_list));
@@ -447,12 +482,13 @@
 /**
  * print short term list
  */
-static void print_short_term(H264Context *h) {
+static void print_short_term(H264Context *h)
+{
     uint32_t i;
-    if(h->avctx->debug&FF_DEBUG_MMCO) {
+    if (h->avctx->debug & FF_DEBUG_MMCO) {
         av_log(h->avctx, AV_LOG_DEBUG, "short term list:\n");
-        for(i=0; i<h->short_ref_count; i++){
-            Picture *pic= h->short_ref[i];
+        for (i = 0; i < h->short_ref_count; i++) {
+            Picture *pic = h->short_ref[i];
             av_log(h->avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n",
                    i, pic->frame_num, pic->poc, pic->f.data[0]);
         }
@@ -462,12 +498,13 @@
 /**
  * print long term list
  */
-static void print_long_term(H264Context *h) {
+static void print_long_term(H264Context *h)
+{
     uint32_t i;
-    if(h->avctx->debug&FF_DEBUG_MMCO) {
+    if (h->avctx->debug & FF_DEBUG_MMCO) {
         av_log(h->avctx, AV_LOG_DEBUG, "long term list:\n");
-        for(i = 0; i < 16; i++){
-            Picture *pic= h->long_ref[i];
+        for (i = 0; i < 16; i++) {
+            Picture *pic = h->long_ref[i];
             if (pic) {
                 av_log(h->avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n",
                        i, pic->frame_num, pic->poc, pic->f.data[0]);
@@ -498,15 +535,15 @@
 
     if (h->short_ref_count &&
         h->long_ref_count + h->short_ref_count >= h->sps.ref_frame_count &&
-        !(FIELD_PICTURE && !h->first_field && h->cur_pic_ptr->f.reference)) {
-        mmco[0].opcode = MMCO_SHORT2UNUSED;
+        !(FIELD_PICTURE(h) && !h->first_field && h->cur_pic_ptr->reference)) {
+        mmco[0].opcode        = MMCO_SHORT2UNUSED;
         mmco[0].short_pic_num = h->short_ref[h->short_ref_count - 1]->frame_num;
-        mmco_index = 1;
-        if (FIELD_PICTURE) {
+        mmco_index            = 1;
+        if (FIELD_PICTURE(h)) {
             mmco[0].short_pic_num *= 2;
-            mmco[1].opcode = MMCO_SHORT2UNUSED;
-            mmco[1].short_pic_num = mmco[0].short_pic_num + 1;
-            mmco_index = 2;
+            mmco[1].opcode         = MMCO_SHORT2UNUSED;
+            mmco[1].short_pic_num  = mmco[0].short_pic_num + 1;
+            mmco_index             = 2;
         }
     }
 
@@ -523,26 +560,29 @@
     return 0;
 }
 
-int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
+int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count)
+{
     int i, av_uninit(j);
-    int current_ref_assigned=0, err=0;
+    int current_ref_assigned = 0, err = 0;
     Picture *av_uninit(pic);
 
-    if((h->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
+    if ((h->avctx->debug & FF_DEBUG_MMCO) && mmco_count == 0)
         av_log(h->avctx, AV_LOG_DEBUG, "no mmco here\n");
 
-    for(i=0; i<mmco_count; i++){
+    for (i = 0; i < mmco_count; i++) {
         int av_uninit(structure), av_uninit(frame_num);
-        if(h->avctx->debug&FF_DEBUG_MMCO)
-            av_log(h->avctx, AV_LOG_DEBUG, "mmco:%d %d %d\n", h->mmco[i].opcode, h->mmco[i].short_pic_num, h->mmco[i].long_arg);
+        if (h->avctx->debug & FF_DEBUG_MMCO)
+            av_log(h->avctx, AV_LOG_DEBUG, "mmco:%d %d %d\n", h->mmco[i].opcode,
+                   h->mmco[i].short_pic_num, h->mmco[i].long_arg);
 
-        if(   mmco[i].opcode == MMCO_SHORT2UNUSED
-           || mmco[i].opcode == MMCO_SHORT2LONG){
+        if (mmco[i].opcode == MMCO_SHORT2UNUSED ||
+            mmco[i].opcode == MMCO_SHORT2LONG) {
             frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
-            pic = find_short(h, frame_num, &j);
-            if(!pic){
-                if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]
-                   || h->long_ref[mmco[i].long_arg]->frame_num != frame_num) {
+            pic       = find_short(h, frame_num, &j);
+            if (!pic) {
+                if (mmco[i].opcode != MMCO_SHORT2LONG ||
+                    !h->long_ref[mmco[i].long_arg]    ||
+                    h->long_ref[mmco[i].long_arg]->frame_num != frame_num) {
                     av_log(h->avctx, AV_LOG_ERROR, "mmco: unref short failure\n");
                     err = AVERROR_INVALIDDATA;
                 }
@@ -550,10 +590,11 @@
             }
         }
 
-        switch(mmco[i].opcode){
+        switch (mmco[i].opcode) {
         case MMCO_SHORT2UNUSED:
-            if(h->avctx->debug&FF_DEBUG_MMCO)
-                av_log(h->avctx, AV_LOG_DEBUG, "mmco: unref short %d count %d\n", h->mmco[i].short_pic_num, h->short_ref_count);
+            if (h->avctx->debug & FF_DEBUG_MMCO)
+                av_log(h->avctx, AV_LOG_DEBUG, "mmco: unref short %d count %d\n",
+                       h->mmco[i].short_pic_num, h->short_ref_count);
             remove_short(h, frame_num, structure ^ PICT_FRAME);
             break;
         case MMCO_SHORT2LONG:
@@ -561,18 +602,18 @@
                     remove_long(h, mmco[i].long_arg, 0);
 
                 remove_short_at_index(h, j);
-                h->long_ref[ mmco[i].long_arg ]= pic;
-                if (h->long_ref[ mmco[i].long_arg ]){
-                    h->long_ref[ mmco[i].long_arg ]->long_ref=1;
+                h->long_ref[ mmco[i].long_arg ] = pic;
+                if (h->long_ref[mmco[i].long_arg]) {
+                    h->long_ref[mmco[i].long_arg]->long_ref = 1;
                     h->long_ref_count++;
                 }
             break;
         case MMCO_LONG2UNUSED:
-            j = pic_num_extract(h, mmco[i].long_arg, &structure);
+            j   = pic_num_extract(h, mmco[i].long_arg, &structure);
             pic = h->long_ref[j];
             if (pic) {
                 remove_long(h, j, structure ^ PICT_FRAME);
-            } else if(h->avctx->debug&FF_DEBUG_MMCO)
+            } else if (h->avctx->debug & FF_DEBUG_MMCO)
                 av_log(h->avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
             break;
         case MMCO_LONG:
@@ -585,34 +626,45 @@
                      */
 
             if (h->long_ref[mmco[i].long_arg] != h->cur_pic_ptr) {
+                if (h->cur_pic_ptr->long_ref) {
+                    for(j=0; j<16; j++) {
+                        if(h->long_ref[j] == h->cur_pic_ptr) {
+                            remove_long(h, j, 0);
+                            av_log(h->avctx, AV_LOG_ERROR, "mmco: cannot assign current picture to 2 long term references\n");
+                        }
+                    }
+                }
+                av_assert0(!h->cur_pic_ptr->long_ref);
                 remove_long(h, mmco[i].long_arg, 0);
+                if (remove_short(h, h->cur_pic_ptr->frame_num, 0)) {
+                    av_log(h->avctx, AV_LOG_ERROR, "mmco: cannot assign current picture to short and long at the same time\n");
+                }
 
-                h->long_ref[ mmco[i].long_arg ]= h->cur_pic_ptr;
-                h->long_ref[ mmco[i].long_arg ]->long_ref=1;
+                h->long_ref[mmco[i].long_arg]           = h->cur_pic_ptr;
+                h->long_ref[mmco[i].long_arg]->long_ref = 1;
                 h->long_ref_count++;
             }
 
-            h->cur_pic_ptr->f.reference |= h->picture_structure;
-            current_ref_assigned=1;
+            h->cur_pic_ptr->reference |= h->picture_structure;
+            current_ref_assigned = 1;
             break;
         case MMCO_SET_MAX_LONG:
             assert(mmco[i].long_arg <= 16);
             // just remove the long term which index is greater than new max
-            for(j = mmco[i].long_arg; j<16; j++){
+            for (j = mmco[i].long_arg; j < 16; j++) {
                 remove_long(h, j, 0);
             }
             break;
         case MMCO_RESET:
-            while(h->short_ref_count){
+            while (h->short_ref_count) {
                 remove_short(h, h->short_ref[0]->frame_num, 0);
             }
-            for(j = 0; j < 16; j++) {
+            for (j = 0; j < 16; j++) {
                 remove_long(h, j, 0);
             }
-            h->frame_num=
-            h->cur_pic_ptr->frame_num= 0;
+            h->frame_num  = h->cur_pic_ptr->frame_num = 0;
             h->mmco_reset = 1;
-            h->cur_pic_ptr->mmco_reset=1;
+            h->cur_pic_ptr->mmco_reset = 1;
             for (j = 0; j < MAX_DELAYED_PIC_COUNT; j++)
                 h->last_pocs[j] = INT_MIN;
             break;
@@ -629,7 +681,7 @@
          */
         if (h->short_ref_count && h->short_ref[0] == h->cur_pic_ptr) {
             /* Just mark the second field valid */
-            h->cur_pic_ptr->f.reference = PICT_FRAME;
+            h->cur_pic_ptr->reference = PICT_FRAME;
         } else if (h->cur_pic_ptr->long_ref) {
             av_log(h->avctx, AV_LOG_ERROR, "illegal short term reference "
                                            "assignment for second field "
@@ -637,22 +689,23 @@
                                            "(first field is long term)\n");
             err = AVERROR_INVALIDDATA;
         } else {
-            pic= remove_short(h, h->cur_pic_ptr->frame_num, 0);
-            if(pic){
+            pic = remove_short(h, h->cur_pic_ptr->frame_num, 0);
+            if (pic) {
                 av_log(h->avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
                 err = AVERROR_INVALIDDATA;
             }
 
-            if(h->short_ref_count)
-                memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
+            if (h->short_ref_count)
+                memmove(&h->short_ref[1], &h->short_ref[0],
+                        h->short_ref_count * sizeof(Picture*));
 
-            h->short_ref[0]= h->cur_pic_ptr;
+            h->short_ref[0] = h->cur_pic_ptr;
             h->short_ref_count++;
-            h->cur_pic_ptr->f.reference |= h->picture_structure;
+            h->cur_pic_ptr->reference |= h->picture_structure;
         }
     }
 
-    if (h->long_ref_count + h->short_ref_count > FFMAX(h->sps.ref_frame_count, 1)){
+    if (h->long_ref_count + h->short_ref_count > FFMAX(h->sps.ref_frame_count, 1)) {
 
         /* We have too many reference frames, probably due to corrupted
          * stream. Need to discard one frame. Prevents overrun of the
@@ -696,12 +749,12 @@
     MMCO mmco_temp[MAX_MMCO_COUNT], *mmco = first_slice ? h->mmco : mmco_temp;
     int mmco_index = 0;
 
-    if (h->nal_unit_type == NAL_IDR_SLICE){ // FIXME fields
+    if (h->nal_unit_type == NAL_IDR_SLICE) { // FIXME fields
         skip_bits1(gb); // broken_link
-        if (get_bits1(gb)){
-            mmco[0].opcode = MMCO_LONG;
+        if (get_bits1(gb)) {
+            mmco[0].opcode   = MMCO_LONG;
             mmco[0].long_arg = 0;
-            mmco_index = 1;
+            mmco_index       = 1;
         }
     } else {
         if (get_bits1(gb)) { // adaptive_ref_pic_marking_mode_flag
@@ -709,7 +762,7 @@
                 MMCOOpcode opcode = get_ue_golomb_31(gb);
 
                 mmco[i].opcode = opcode;
-                if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG){
+                if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG) {
                     mmco[i].short_pic_num =
                         (h->curr_pic_num - get_ue_golomb(gb) - 1) &
                             (h->max_pic_num - 1);
@@ -729,7 +782,7 @@
                     if (long_arg >= 32 ||
                         (long_arg >= 16 && !(opcode == MMCO_SET_MAX_LONG &&
                                              long_arg == 16) &&
-                         !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
+                         !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE(h)))) {
                         av_log(h->avctx, AV_LOG_ERROR,
                                "illegal long ref in memory management control "
                                "operation %d\n", opcode);
@@ -738,7 +791,7 @@
                     mmco[i].long_arg = long_arg;
                 }
 
-                if (opcode > (unsigned) MMCO_LONG){
+                if (opcode > (unsigned) MMCO_LONG) {
                     av_log(h->avctx, AV_LOG_ERROR,
                            "illegal memory management control operation %d\n",
                            opcode);
diff --git a/libavcodec/h264_sei.c b/libavcodec/h264_sei.c
index ece54f1..be6cb58 100644
--- a/libavcodec/h264_sei.c
+++ b/libavcodec/h264_sei.c
@@ -161,6 +161,9 @@
     h->sei_recovery_frame_cnt = get_ue_golomb(&h->gb);
     skip_bits(&h->gb, 4);       /* 1b exact_match_flag, 1b broken_link_flag, 2b changing_slice_group_idc */
 
+    if(h->avctx->debug & FF_DEBUG_PICT_INFO)
+        av_log(h->avctx, AV_LOG_DEBUG, "sei_recovery_frame_cnt: %d\n", h->sei_recovery_frame_cnt);
+
     return 0;
 }
 
diff --git a/libavcodec/h264chroma.c b/libavcodec/h264chroma.c
index 3b780a0..ed41afe 100644
--- a/libavcodec/h264chroma.c
+++ b/libavcodec/h264chroma.c
@@ -47,8 +47,6 @@
         ff_h264chroma_init_arm(c, bit_depth);
     if (ARCH_PPC)
         ff_h264chroma_init_ppc(c, bit_depth);
-    if (ARCH_SH4)
-        ff_h264chroma_init_sh4(c, bit_depth);
     if (ARCH_X86)
         ff_h264chroma_init_x86(c, bit_depth);
 }
diff --git a/libavcodec/h264chroma.h b/libavcodec/h264chroma.h
index 4e035b0..46fae42 100644
--- a/libavcodec/h264chroma.h
+++ b/libavcodec/h264chroma.h
@@ -32,7 +32,6 @@
 
 void ff_h264chroma_init_arm(H264ChromaContext *c, int bit_depth);
 void ff_h264chroma_init_ppc(H264ChromaContext *c, int bit_depth);
-void ff_h264chroma_init_sh4(H264ChromaContext *c, int bit_depth);
 void ff_h264chroma_init_x86(H264ChromaContext *c, int bit_depth);
 
 #endif /* AVCODEC_H264CHROMA_H */
diff --git a/libavcodec/h264idct_template.c b/libavcodec/h264idct_template.c
index 9f16e1d..42c32d1 100644
--- a/libavcodec/h264idct_template.c
+++ b/libavcodec/h264idct_template.c
@@ -145,7 +145,7 @@
     pixel *dst = (pixel*)_dst;
     dctcoef *block = (dctcoef*)_block;
     int dc = (block[0] + 32) >> 6;
-    stride >>= sizeof(pixel)-1;
+    stride /= sizeof(pixel);
     block[0] = 0;
     for( j = 0; j < 4; j++ )
     {
@@ -161,7 +161,7 @@
     dctcoef *block = (dctcoef*)_block;
     int dc = (block[0] + 32) >> 6;
     block[0] = 0;
-    stride >>= sizeof(pixel)-1;
+    stride /= sizeof(pixel);
     for( j = 0; j < 8; j++ )
     {
         for( i = 0; i < 8; i++ )
diff --git a/libavcodec/h264pred.c b/libavcodec/h264pred.c
index e5f1f5a..ed4f287 100644
--- a/libavcodec/h264pred.c
+++ b/libavcodec/h264pred.c
@@ -278,12 +278,12 @@
 static void pred4x4_tm_vp8_c(uint8_t *src, const uint8_t *topright,
                              ptrdiff_t stride)
 {
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP - src[-1-stride];
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP - src[-1-stride];
     uint8_t *top = src-stride;
     int y;
 
     for (y = 0; y < 4; y++) {
-        uint8_t *cm_in = cm + src[-1];
+        const uint8_t *cm_in = cm + src[-1];
         src[0] = cm_in[top[0]];
         src[1] = cm_in[top[1]];
         src[2] = cm_in[top[2]];
@@ -304,12 +304,12 @@
 
 static void pred16x16_tm_vp8_c(uint8_t *src, ptrdiff_t stride)
 {
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP - src[-1-stride];
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP - src[-1-stride];
     uint8_t *top = src-stride;
     int y;
 
     for (y = 0; y < 16; y++) {
-        uint8_t *cm_in = cm + src[-1];
+        const uint8_t *cm_in = cm + src[-1];
         src[0]  = cm_in[top[0]];
         src[1]  = cm_in[top[1]];
         src[2]  = cm_in[top[2]];
@@ -386,12 +386,12 @@
 
 static void pred8x8_tm_vp8_c(uint8_t *src, ptrdiff_t stride)
 {
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP - src[-1-stride];
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP - src[-1-stride];
     uint8_t *top = src-stride;
     int y;
 
     for (y = 0; y < 8; y++) {
-        uint8_t *cm_in = cm + src[-1];
+        const uint8_t *cm_in = cm + src[-1];
         src[0] = cm_in[top[0]];
         src[1] = cm_in[top[1]];
         src[2] = cm_in[top[2]];
diff --git a/libavcodec/h264qpel_template.c b/libavcodec/h264qpel_template.c
index 95950f4..48eb8c8 100644
--- a/libavcodec/h264qpel_template.c
+++ b/libavcodec/h264qpel_template.c
@@ -375,27 +375,32 @@
 }\
 
 #define H264_MC(OPNAME, SIZE) \
-static av_unused void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc00)(uint8_t *dst, uint8_t *src, int stride){\
+static av_unused void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc00)(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     FUNCC(OPNAME ## pixels ## SIZE)(dst, src, stride, SIZE);\
 }\
 \
-static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc10)(uint8_t *dst, uint8_t *src, int stride){\
+static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc10)(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t half[SIZE*SIZE*sizeof(pixel)];\
     FUNC(put_h264_qpel ## SIZE ## _h_lowpass)(half, src, SIZE*sizeof(pixel), stride);\
     FUNC(OPNAME ## pixels ## SIZE ## _l2)(dst, src, half, stride, stride, SIZE*sizeof(pixel), SIZE);\
 }\
 \
-static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc20)(uint8_t *dst, uint8_t *src, int stride){\
+static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc20)(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     FUNC(OPNAME ## h264_qpel ## SIZE ## _h_lowpass)(dst, src, stride, stride);\
 }\
 \
-static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc30)(uint8_t *dst, uint8_t *src, int stride){\
+static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc30)(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t half[SIZE*SIZE*sizeof(pixel)];\
     FUNC(put_h264_qpel ## SIZE ## _h_lowpass)(half, src, SIZE*sizeof(pixel), stride);\
     FUNC(OPNAME ## pixels ## SIZE ## _l2)(dst, src+sizeof(pixel), half, stride, stride, SIZE*sizeof(pixel), SIZE);\
 }\
 \
-static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc01)(uint8_t *dst, uint8_t *src, int stride){\
+static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc01)(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[SIZE*(SIZE+5)*sizeof(pixel)];\
     uint8_t * const full_mid= full + SIZE*2*sizeof(pixel);\
     uint8_t half[SIZE*SIZE*sizeof(pixel)];\
@@ -404,14 +409,16 @@
     FUNC(OPNAME ## pixels ## SIZE ## _l2)(dst, full_mid, half, stride, SIZE*sizeof(pixel), SIZE*sizeof(pixel), SIZE);\
 }\
 \
-static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc02)(uint8_t *dst, uint8_t *src, int stride){\
+static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc02)(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[SIZE*(SIZE+5)*sizeof(pixel)];\
     uint8_t * const full_mid= full + SIZE*2*sizeof(pixel);\
     FUNC(copy_block ## SIZE )(full, src - stride*2, SIZE*sizeof(pixel),  stride, SIZE + 5);\
     FUNC(OPNAME ## h264_qpel ## SIZE ## _v_lowpass)(dst, full_mid, stride, SIZE*sizeof(pixel));\
 }\
 \
-static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc03)(uint8_t *dst, uint8_t *src, int stride){\
+static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc03)(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[SIZE*(SIZE+5)*sizeof(pixel)];\
     uint8_t * const full_mid= full + SIZE*2*sizeof(pixel);\
     uint8_t half[SIZE*SIZE*sizeof(pixel)];\
@@ -420,7 +427,8 @@
     FUNC(OPNAME ## pixels ## SIZE ## _l2)(dst, full_mid+SIZE*sizeof(pixel), half, stride, SIZE*sizeof(pixel), SIZE*sizeof(pixel), SIZE);\
 }\
 \
-static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc11)(uint8_t *dst, uint8_t *src, int stride){\
+static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc11)(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[SIZE*(SIZE+5)*sizeof(pixel)];\
     uint8_t * const full_mid= full + SIZE*2*sizeof(pixel);\
     uint8_t halfH[SIZE*SIZE*sizeof(pixel)];\
@@ -431,7 +439,8 @@
     FUNC(OPNAME ## pixels ## SIZE ## _l2)(dst, halfH, halfV, stride, SIZE*sizeof(pixel), SIZE*sizeof(pixel), SIZE);\
 }\
 \
-static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc31)(uint8_t *dst, uint8_t *src, int stride){\
+static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc31)(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[SIZE*(SIZE+5)*sizeof(pixel)];\
     uint8_t * const full_mid= full + SIZE*2*sizeof(pixel);\
     uint8_t halfH[SIZE*SIZE*sizeof(pixel)];\
@@ -442,7 +451,8 @@
     FUNC(OPNAME ## pixels ## SIZE ## _l2)(dst, halfH, halfV, stride, SIZE*sizeof(pixel), SIZE*sizeof(pixel), SIZE);\
 }\
 \
-static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc13)(uint8_t *dst, uint8_t *src, int stride){\
+static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc13)(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[SIZE*(SIZE+5)*sizeof(pixel)];\
     uint8_t * const full_mid= full + SIZE*2*sizeof(pixel);\
     uint8_t halfH[SIZE*SIZE*sizeof(pixel)];\
@@ -453,7 +463,8 @@
     FUNC(OPNAME ## pixels ## SIZE ## _l2)(dst, halfH, halfV, stride, SIZE*sizeof(pixel), SIZE*sizeof(pixel), SIZE);\
 }\
 \
-static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc33)(uint8_t *dst, uint8_t *src, int stride){\
+static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc33)(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[SIZE*(SIZE+5)*sizeof(pixel)];\
     uint8_t * const full_mid= full + SIZE*2*sizeof(pixel);\
     uint8_t halfH[SIZE*SIZE*sizeof(pixel)];\
@@ -464,12 +475,14 @@
     FUNC(OPNAME ## pixels ## SIZE ## _l2)(dst, halfH, halfV, stride, SIZE*sizeof(pixel), SIZE*sizeof(pixel), SIZE);\
 }\
 \
-static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc22)(uint8_t *dst, uint8_t *src, int stride){\
+static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc22)(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     pixeltmp tmp[SIZE*(SIZE+5)*sizeof(pixel)];\
     FUNC(OPNAME ## h264_qpel ## SIZE ## _hv_lowpass)(dst, tmp, src, stride, SIZE*sizeof(pixel), stride);\
 }\
 \
-static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc21)(uint8_t *dst, uint8_t *src, int stride){\
+static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc21)(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     pixeltmp tmp[SIZE*(SIZE+5)*sizeof(pixel)];\
     uint8_t halfH[SIZE*SIZE*sizeof(pixel)];\
     uint8_t halfHV[SIZE*SIZE*sizeof(pixel)];\
@@ -478,7 +491,8 @@
     FUNC(OPNAME ## pixels ## SIZE ## _l2)(dst, halfH, halfHV, stride, SIZE*sizeof(pixel), SIZE*sizeof(pixel), SIZE);\
 }\
 \
-static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc23)(uint8_t *dst, uint8_t *src, int stride){\
+static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc23)(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     pixeltmp tmp[SIZE*(SIZE+5)*sizeof(pixel)];\
     uint8_t halfH[SIZE*SIZE*sizeof(pixel)];\
     uint8_t halfHV[SIZE*SIZE*sizeof(pixel)];\
@@ -487,7 +501,8 @@
     FUNC(OPNAME ## pixels ## SIZE ## _l2)(dst, halfH, halfHV, stride, SIZE*sizeof(pixel), SIZE*sizeof(pixel), SIZE);\
 }\
 \
-static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc12)(uint8_t *dst, uint8_t *src, int stride){\
+static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc12)(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[SIZE*(SIZE+5)*sizeof(pixel)];\
     uint8_t * const full_mid= full + SIZE*2*sizeof(pixel);\
     pixeltmp tmp[SIZE*(SIZE+5)*sizeof(pixel)];\
@@ -499,7 +514,8 @@
     FUNC(OPNAME ## pixels ## SIZE ## _l2)(dst, halfV, halfHV, stride, SIZE*sizeof(pixel), SIZE*sizeof(pixel), SIZE);\
 }\
 \
-static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc32)(uint8_t *dst, uint8_t *src, int stride){\
+static void FUNCC(OPNAME ## h264_qpel ## SIZE ## _mc32)(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[SIZE*(SIZE+5)*sizeof(pixel)];\
     uint8_t * const full_mid= full + SIZE*2*sizeof(pixel);\
     pixeltmp tmp[SIZE*(SIZE+5)*sizeof(pixel)];\
@@ -531,30 +547,3 @@
 #undef op_put
 #undef op2_avg
 #undef op2_put
-
-#if BIT_DEPTH == 8
-#   define put_h264_qpel8_mc00_8_c  ff_put_pixels8x8_8_c
-#   define avg_h264_qpel8_mc00_8_c  ff_avg_pixels8x8_8_c
-#   define put_h264_qpel16_mc00_8_c ff_put_pixels16x16_8_c
-#   define avg_h264_qpel16_mc00_8_c ff_avg_pixels16x16_8_c
-#elif BIT_DEPTH == 9
-#   define put_h264_qpel8_mc00_9_c  ff_put_pixels8x8_9_c
-#   define avg_h264_qpel8_mc00_9_c  ff_avg_pixels8x8_9_c
-#   define put_h264_qpel16_mc00_9_c ff_put_pixels16x16_9_c
-#   define avg_h264_qpel16_mc00_9_c ff_avg_pixels16x16_9_c
-#elif BIT_DEPTH == 10
-#   define put_h264_qpel8_mc00_10_c  ff_put_pixels8x8_10_c
-#   define avg_h264_qpel8_mc00_10_c  ff_avg_pixels8x8_10_c
-#   define put_h264_qpel16_mc00_10_c ff_put_pixels16x16_10_c
-#   define avg_h264_qpel16_mc00_10_c ff_avg_pixels16x16_10_c
-#elif BIT_DEPTH == 12
-#   define put_h264_qpel8_mc00_12_c  ff_put_pixels8x8_12_c
-#   define avg_h264_qpel8_mc00_12_c  ff_avg_pixels8x8_12_c
-#   define put_h264_qpel16_mc00_12_c ff_put_pixels16x16_12_c
-#   define avg_h264_qpel16_mc00_12_c ff_avg_pixels16x16_12_c
-#elif BIT_DEPTH == 14
-#   define put_h264_qpel8_mc00_14_c  ff_put_pixels8x8_14_c
-#   define avg_h264_qpel8_mc00_14_c  ff_avg_pixels8x8_14_c
-#   define put_h264_qpel16_mc00_14_c ff_put_pixels16x16_14_c
-#   define avg_h264_qpel16_mc00_14_c ff_avg_pixels16x16_14_c
-#endif
diff --git a/libavcodec/hpeldsp.c b/libavcodec/hpeldsp.c
new file mode 100644
index 0000000..473102d
--- /dev/null
+++ b/libavcodec/hpeldsp.c
@@ -0,0 +1,69 @@
+/*
+ * Half-pel DSP functions.
+ * Copyright (c) 2000, 2001 Fabrice Bellard
+ * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
+ *
+ * gmc & q-pel & 32/64 bit based MC by 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
+ * Half-pel DSP functions.
+ */
+
+#include "libavutil/attributes.h"
+#include "libavutil/intreadwrite.h"
+#include "hpeldsp.h"
+
+#define BIT_DEPTH 8
+#include "hpeldsp_template.c"
+
+av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
+{
+#define hpel_funcs(prefix, idx, num) \
+    c->prefix ## _pixels_tab idx [0] = prefix ## _pixels ## num ## _8_c; \
+    c->prefix ## _pixels_tab idx [1] = prefix ## _pixels ## num ## _x2_8_c; \
+    c->prefix ## _pixels_tab idx [2] = prefix ## _pixels ## num ## _y2_8_c; \
+    c->prefix ## _pixels_tab idx [3] = prefix ## _pixels ## num ## _xy2_8_c
+
+    hpel_funcs(put, [0], 16);
+    hpel_funcs(put, [1],  8);
+    hpel_funcs(put, [2],  4);
+    hpel_funcs(put, [3],  2);
+    hpel_funcs(put_no_rnd, [0], 16);
+    hpel_funcs(put_no_rnd, [1],  8);
+    hpel_funcs(avg, [0], 16);
+    hpel_funcs(avg, [1],  8);
+    hpel_funcs(avg, [2],  4);
+    hpel_funcs(avg, [3],  2);
+    hpel_funcs(avg_no_rnd,, 16);
+
+    if (ARCH_ALPHA)
+        ff_hpeldsp_init_alpha(c, flags);
+    if (ARCH_ARM)
+        ff_hpeldsp_init_arm(c, flags);
+    if (ARCH_BFIN)
+        ff_hpeldsp_init_bfin(c, flags);
+    if (ARCH_PPC)
+        ff_hpeldsp_init_ppc(c, flags);
+    if (HAVE_VIS)
+        ff_hpeldsp_init_vis(c, flags);
+    if (ARCH_X86)
+        ff_hpeldsp_init_x86(c, flags);
+}
diff --git a/libavcodec/hpeldsp.h b/libavcodec/hpeldsp.h
new file mode 100644
index 0000000..ec9006c
--- /dev/null
+++ b/libavcodec/hpeldsp.h
@@ -0,0 +1,104 @@
+/*
+ * Half-pel DSP functions.
+ * Copyright (c) 2000, 2001, 2002 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
+ * Half-pel DSP functions.
+ */
+
+#ifndef AVCODEC_HPELDSP_H
+#define AVCODEC_HPELDSP_H
+
+#include <stdint.h>
+#include <stddef.h>
+
+/* add and put pixel (decoding) */
+// blocksizes for hpel_pixels_func are 8x4,8x8 16x8 16x16
+// h for hpel_pixels_func is limited to {width/2, width} but never larger
+// than 16 and never smaller than 4
+typedef void (*op_pixels_func)(uint8_t *block /*align width (8 or 16)*/,
+                               const uint8_t *pixels /*align 1*/,
+                               ptrdiff_t line_size, int h);
+
+/**
+ * Half-pel DSP context.
+ */
+typedef struct HpelDSPContext {
+    /**
+     * Halfpel motion compensation with rounding (a+b+1)>>1.
+     * this is an array[4][4] of motion compensation functions for 4
+     * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
+     * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
+     * @param block destination where the result is stored
+     * @param pixels source
+     * @param line_size number of bytes in a horizontal line of block
+     * @param h height
+     */
+    op_pixels_func put_pixels_tab[4][4];
+
+    /**
+     * Halfpel motion compensation with rounding (a+b+1)>>1.
+     * This is an array[4][4] of motion compensation functions for 4
+     * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
+     * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
+     * @param block destination into which the result is averaged (a+b+1)>>1
+     * @param pixels source
+     * @param line_size number of bytes in a horizontal line of block
+     * @param h height
+     */
+    op_pixels_func avg_pixels_tab[4][4];
+
+    /**
+     * Halfpel motion compensation with no rounding (a+b)>>1.
+     * this is an array[4][4] of motion compensation functions for 2
+     * horizontal blocksizes (8,16) and the 4 halfpel positions<br>
+     * *pixels_tab[ 0->16xH 1->8xH ][ xhalfpel + 2*yhalfpel ]
+     * @param block destination where the result is stored
+     * @param pixels source
+     * @param line_size number of bytes in a horizontal line of block
+     * @param h height
+     */
+    op_pixels_func put_no_rnd_pixels_tab[4][4];
+
+    /**
+     * Halfpel motion compensation with no rounding (a+b)>>1.
+     * this is an array[4] of motion compensation functions for 1
+     * horizontal blocksize (16) and the 4 halfpel positions<br>
+     * *pixels_tab[0][ xhalfpel + 2*yhalfpel ]
+     * @param block destination into which the result is averaged (a+b)>>1
+     * @param pixels source
+     * @param line_size number of bytes in a horizontal line of block
+     * @param h height
+     */
+    op_pixels_func avg_no_rnd_pixels_tab[4];
+} HpelDSPContext;
+
+void ff_hpeldsp_init(HpelDSPContext *c, int flags);
+
+void ff_hpeldsp_init_alpha(HpelDSPContext *c, int flags);
+void ff_hpeldsp_init_arm(HpelDSPContext *c, int flags);
+void ff_hpeldsp_init_bfin(HpelDSPContext *c, int flags);
+void ff_hpeldsp_init_ppc(HpelDSPContext *c, int flags);
+void ff_hpeldsp_init_vis(HpelDSPContext *c, int flags);
+void ff_hpeldsp_init_x86(HpelDSPContext *c, int flags);
+
+#endif /* AVCODEC_HPELDSP_H */
diff --git a/libavcodec/hpeldsp_template.c b/libavcodec/hpeldsp_template.c
new file mode 100644
index 0000000..9c9fd2b
--- /dev/null
+++ b/libavcodec/hpeldsp_template.c
@@ -0,0 +1,254 @@
+/*
+ * Half-pel DSP functions.
+ * Copyright (c) 2000, 2001 Fabrice Bellard
+ * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
+ *
+ * gmc & q-pel & 32/64 bit based MC by 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
+ * Half-pel DSP functions.
+ */
+
+#include "bit_depth_template.c"
+
+#include "hpel_template.c"
+
+#define PIXOP2(OPNAME, OP) \
+static inline void FUNC(OPNAME ## _no_rnd_pixels8_l2)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, \
+                                                int src_stride1, int src_stride2, int h){\
+    int i;\
+    for(i=0; i<h; i++){\
+        pixel4 a,b;\
+        a= AV_RN4P(&src1[i*src_stride1  ]);\
+        b= AV_RN4P(&src2[i*src_stride2  ]);\
+        OP(*((pixel4*)&dst[i*dst_stride  ]), no_rnd_avg_pixel4(a, b));\
+        a= AV_RN4P(&src1[i*src_stride1+4*sizeof(pixel)]);\
+        b= AV_RN4P(&src2[i*src_stride2+4*sizeof(pixel)]);\
+        OP(*((pixel4*)&dst[i*dst_stride+4*sizeof(pixel)]), no_rnd_avg_pixel4(a, b));\
+    }\
+}\
+\
+static inline void FUNCC(OPNAME ## _no_rnd_pixels8_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
+    FUNC(OPNAME ## _no_rnd_pixels8_l2)(block, pixels, pixels+sizeof(pixel), line_size, line_size, line_size, h);\
+}\
+\
+static inline void FUNCC(OPNAME ## _pixels8_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
+    FUNC(OPNAME ## _pixels8_l2)(block, pixels, pixels+sizeof(pixel), line_size, line_size, line_size, h);\
+}\
+\
+static inline void FUNCC(OPNAME ## _no_rnd_pixels8_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
+    FUNC(OPNAME ## _no_rnd_pixels8_l2)(block, pixels, pixels+line_size, line_size, line_size, line_size, h);\
+}\
+\
+static inline void FUNCC(OPNAME ## _pixels8_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
+    FUNC(OPNAME ## _pixels8_l2)(block, pixels, pixels+line_size, line_size, line_size, line_size, h);\
+}\
+\
+static inline void FUNCC(OPNAME ## _pixels4_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
+    FUNC(OPNAME ## _pixels4_l2)(block, pixels, pixels+sizeof(pixel), line_size, line_size, line_size, h);\
+}\
+\
+static inline void FUNCC(OPNAME ## _pixels4_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
+    FUNC(OPNAME ## _pixels4_l2)(block, pixels, pixels+line_size, line_size, line_size, line_size, h);\
+}\
+\
+static inline void FUNCC(OPNAME ## _pixels2_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
+    FUNC(OPNAME ## _pixels2_l2)(block, pixels, pixels+sizeof(pixel), line_size, line_size, line_size, h);\
+}\
+\
+static inline void FUNCC(OPNAME ## _pixels2_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
+    FUNC(OPNAME ## _pixels2_l2)(block, pixels, pixels+line_size, line_size, line_size, line_size, h);\
+}\
+\
+static inline void FUNCC(OPNAME ## _pixels2_xy2)(uint8_t *_block, const uint8_t *_pixels, ptrdiff_t line_size, int h)\
+{\
+        int i, a0, b0, a1, b1;\
+        pixel *block = (pixel*)_block;\
+        const pixel *pixels = (const pixel*)_pixels;\
+        line_size >>= sizeof(pixel)-1;\
+        a0= pixels[0];\
+        b0= pixels[1] + 2;\
+        a0 += b0;\
+        b0 += pixels[2];\
+\
+        pixels+=line_size;\
+        for(i=0; i<h; i+=2){\
+            a1= pixels[0];\
+            b1= pixels[1];\
+            a1 += b1;\
+            b1 += pixels[2];\
+\
+            block[0]= (a1+a0)>>2; /* FIXME non put */\
+            block[1]= (b1+b0)>>2;\
+\
+            pixels+=line_size;\
+            block +=line_size;\
+\
+            a0= pixels[0];\
+            b0= pixels[1] + 2;\
+            a0 += b0;\
+            b0 += pixels[2];\
+\
+            block[0]= (a1+a0)>>2;\
+            block[1]= (b1+b0)>>2;\
+            pixels+=line_size;\
+            block +=line_size;\
+        }\
+}\
+\
+static inline void FUNCC(OPNAME ## _pixels4_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)\
+{\
+        /* FIXME HIGH BIT DEPTH */\
+        int i;\
+        const uint32_t a= AV_RN32(pixels  );\
+        const uint32_t b= AV_RN32(pixels+1);\
+        uint32_t l0=  (a&0x03030303UL)\
+                    + (b&0x03030303UL)\
+                    + 0x02020202UL;\
+        uint32_t h0= ((a&0xFCFCFCFCUL)>>2)\
+                   + ((b&0xFCFCFCFCUL)>>2);\
+        uint32_t l1,h1;\
+\
+        pixels+=line_size;\
+        for(i=0; i<h; i+=2){\
+            uint32_t a= AV_RN32(pixels  );\
+            uint32_t b= AV_RN32(pixels+1);\
+            l1=  (a&0x03030303UL)\
+               + (b&0x03030303UL);\
+            h1= ((a&0xFCFCFCFCUL)>>2)\
+              + ((b&0xFCFCFCFCUL)>>2);\
+            OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
+            pixels+=line_size;\
+            block +=line_size;\
+            a= AV_RN32(pixels  );\
+            b= AV_RN32(pixels+1);\
+            l0=  (a&0x03030303UL)\
+               + (b&0x03030303UL)\
+               + 0x02020202UL;\
+            h0= ((a&0xFCFCFCFCUL)>>2)\
+              + ((b&0xFCFCFCFCUL)>>2);\
+            OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
+            pixels+=line_size;\
+            block +=line_size;\
+        }\
+}\
+\
+static inline void FUNCC(OPNAME ## _pixels8_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)\
+{\
+    /* FIXME HIGH BIT DEPTH */\
+    int j;\
+    for(j=0; j<2; j++){\
+        int i;\
+        const uint32_t a= AV_RN32(pixels  );\
+        const uint32_t b= AV_RN32(pixels+1);\
+        uint32_t l0=  (a&0x03030303UL)\
+                    + (b&0x03030303UL)\
+                    + 0x02020202UL;\
+        uint32_t h0= ((a&0xFCFCFCFCUL)>>2)\
+                   + ((b&0xFCFCFCFCUL)>>2);\
+        uint32_t l1,h1;\
+\
+        pixels+=line_size;\
+        for(i=0; i<h; i+=2){\
+            uint32_t a= AV_RN32(pixels  );\
+            uint32_t b= AV_RN32(pixels+1);\
+            l1=  (a&0x03030303UL)\
+               + (b&0x03030303UL);\
+            h1= ((a&0xFCFCFCFCUL)>>2)\
+              + ((b&0xFCFCFCFCUL)>>2);\
+            OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
+            pixels+=line_size;\
+            block +=line_size;\
+            a= AV_RN32(pixels  );\
+            b= AV_RN32(pixels+1);\
+            l0=  (a&0x03030303UL)\
+               + (b&0x03030303UL)\
+               + 0x02020202UL;\
+            h0= ((a&0xFCFCFCFCUL)>>2)\
+              + ((b&0xFCFCFCFCUL)>>2);\
+            OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
+            pixels+=line_size;\
+            block +=line_size;\
+        }\
+        pixels+=4-line_size*(h+1);\
+        block +=4-line_size*h;\
+    }\
+}\
+\
+static inline void FUNCC(OPNAME ## _no_rnd_pixels8_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)\
+{\
+    /* FIXME HIGH BIT DEPTH */\
+    int j;\
+    for(j=0; j<2; j++){\
+        int i;\
+        const uint32_t a= AV_RN32(pixels  );\
+        const uint32_t b= AV_RN32(pixels+1);\
+        uint32_t l0=  (a&0x03030303UL)\
+                    + (b&0x03030303UL)\
+                    + 0x01010101UL;\
+        uint32_t h0= ((a&0xFCFCFCFCUL)>>2)\
+                   + ((b&0xFCFCFCFCUL)>>2);\
+        uint32_t l1,h1;\
+\
+        pixels+=line_size;\
+        for(i=0; i<h; i+=2){\
+            uint32_t a= AV_RN32(pixels  );\
+            uint32_t b= AV_RN32(pixels+1);\
+            l1=  (a&0x03030303UL)\
+               + (b&0x03030303UL);\
+            h1= ((a&0xFCFCFCFCUL)>>2)\
+              + ((b&0xFCFCFCFCUL)>>2);\
+            OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
+            pixels+=line_size;\
+            block +=line_size;\
+            a= AV_RN32(pixels  );\
+            b= AV_RN32(pixels+1);\
+            l0=  (a&0x03030303UL)\
+               + (b&0x03030303UL)\
+               + 0x01010101UL;\
+            h0= ((a&0xFCFCFCFCUL)>>2)\
+              + ((b&0xFCFCFCFCUL)>>2);\
+            OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
+            pixels+=line_size;\
+            block +=line_size;\
+        }\
+        pixels+=4-line_size*(h+1);\
+        block +=4-line_size*h;\
+    }\
+}\
+\
+CALL_2X_PIXELS(FUNCC(OPNAME ## _pixels16_x2) , FUNCC(OPNAME ## _pixels8_x2) , 8*sizeof(pixel))\
+CALL_2X_PIXELS(FUNCC(OPNAME ## _pixels16_y2) , FUNCC(OPNAME ## _pixels8_y2) , 8*sizeof(pixel))\
+CALL_2X_PIXELS(FUNCC(OPNAME ## _pixels16_xy2), FUNCC(OPNAME ## _pixels8_xy2), 8*sizeof(pixel))\
+av_unused CALL_2X_PIXELS(FUNCC(OPNAME ## _no_rnd_pixels16)    , FUNCC(OPNAME ## _pixels8) , 8*sizeof(pixel))\
+CALL_2X_PIXELS(FUNCC(OPNAME ## _no_rnd_pixels16_x2) , FUNCC(OPNAME ## _no_rnd_pixels8_x2) , 8*sizeof(pixel))\
+CALL_2X_PIXELS(FUNCC(OPNAME ## _no_rnd_pixels16_y2) , FUNCC(OPNAME ## _no_rnd_pixels8_y2) , 8*sizeof(pixel))\
+CALL_2X_PIXELS(FUNCC(OPNAME ## _no_rnd_pixels16_xy2), FUNCC(OPNAME ## _no_rnd_pixels8_xy2), 8*sizeof(pixel))\
+
+#define op_avg(a, b) a = rnd_avg_pixel4(a, b)
+#define op_put(a, b) a = b
+#if BIT_DEPTH == 8
+#define put_no_rnd_pixels8_8_c put_pixels8_8_c
+PIXOP2(avg, op_avg)
+PIXOP2(put, op_put)
+#endif
+#undef op_avg
+#undef op_put
diff --git a/libavcodec/huffyuvdec.c b/libavcodec/huffyuvdec.c
index 159a517..e5a9e3b 100644
--- a/libavcodec/huffyuvdec.c
+++ b/libavcodec/huffyuvdec.c
@@ -256,7 +256,6 @@
     ff_huffyuv_common_init(avctx);
     memset(s->vlc, 0, 3 * sizeof(VLC));
 
-    avctx->coded_frame = &s->picture;
     avcodec_get_frame_defaults(&s->picture);
     s->interlaced = s->height > 288;
 
@@ -364,7 +363,6 @@
     HYuvContext *s = avctx->priv_data;
     int i;
 
-    avctx->coded_frame= &s->picture;
     if (ff_huffyuv_alloc_temp(s)) {
         ff_huffyuv_common_end(s);
         return AVERROR(ENOMEM);
@@ -473,7 +471,7 @@
     }
 }
 
-static void draw_slice(HYuvContext *s, int y)
+static void draw_slice(HYuvContext *s, AVFrame *frame, int y)
 {
     int h, cy, i;
     int offset[AV_NUM_DATA_POINTERS];
@@ -490,14 +488,14 @@
         cy = y;
     }
 
-    offset[0] = s->picture.linesize[0]*y;
-    offset[1] = s->picture.linesize[1]*cy;
-    offset[2] = s->picture.linesize[2]*cy;
+    offset[0] = frame->linesize[0] * y;
+    offset[1] = frame->linesize[1] * cy;
+    offset[2] = frame->linesize[2] * cy;
     for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
         offset[i] = 0;
     emms_c();
 
-    s->avctx->draw_horiz_band(s->avctx, &s->picture, offset, y, 3, h);
+    s->avctx->draw_horiz_band(s->avctx, frame, offset, y, 3, h);
 
     s->last_slice_end = y + h;
 }
@@ -512,11 +510,10 @@
     const int width2 = s->width>>1;
     const int height = s->height;
     int fake_ystride, fake_ustride, fake_vstride;
-    AVFrame * const p = &s->picture;
+    ThreadFrame frame = { .f = data };
+    AVFrame * const p = data;
     int table_size = 0, ret;
 
-    AVFrame *picture = data;
-
     av_fast_padded_malloc(&s->bitstream_buffer,
                    &s->bitstream_buffer_size,
                    buf_size);
@@ -526,14 +523,8 @@
     s->dsp.bswap_buf((uint32_t*)s->bitstream_buffer,
                      (const uint32_t*)buf, buf_size / 4);
 
-    if (p->data[0])
-        ff_thread_release_buffer(avctx, p);
-
-    p->reference = 0;
-    if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
         return ret;
-    }
 
     if (s->context) {
         table_size = read_huffman_tables(s, s->bitstream_buffer, buf_size);
@@ -601,7 +592,7 @@
                         if (y >= s->height) break;
                     }
 
-                    draw_slice(s, y);
+                    draw_slice(s, p, y);
 
                     ydst = p->data[0] + p->linesize[0]*y;
                     udst = p->data[1] + p->linesize[1]*cy;
@@ -623,7 +614,7 @@
                         }
                     }
                 }
-                draw_slice(s, height);
+                draw_slice(s, p, height);
 
                 break;
             case MEDIAN:
@@ -680,7 +671,7 @@
                         }
                         if (y >= height) break;
                     }
-                    draw_slice(s, y);
+                    draw_slice(s, p, y);
 
                     decode_422_bitstream(s, width);
 
@@ -695,7 +686,7 @@
                     }
                 }
 
-                draw_slice(s, height);
+                draw_slice(s, p, height);
                 break;
             }
         }
@@ -739,7 +730,7 @@
                     }
                 }
                 // just 1 large slice as this is not possible in reverse order
-                draw_slice(s, height);
+                draw_slice(s, p, height);
                 break;
             default:
                 av_log(avctx, AV_LOG_ERROR,
@@ -753,7 +744,6 @@
     }
     emms_c();
 
-    *picture = *p;
     *got_frame = 1;
 
     return (get_bits_count(&s->gb) + 31) / 32 * 4 + table_size;
@@ -764,9 +754,6 @@
     HYuvContext *s = avctx->priv_data;
     int i;
 
-    if (s->picture.data[0])
-        avctx->release_buffer(avctx, &s->picture);
-
     ff_huffyuv_common_end(s);
     av_freep(&s->bitstream_buffer);
 
diff --git a/libavcodec/idcinvideo.c b/libavcodec/idcinvideo.c
index 2070419..3596080 100644
--- a/libavcodec/idcinvideo.c
+++ b/libavcodec/idcinvideo.c
@@ -66,7 +66,6 @@
 typedef struct IdcinContext {
 
     AVCodecContext *avctx;
-    AVFrame frame;
 
     const unsigned char *buf;
     int size;
@@ -168,12 +167,10 @@
         huff_build_tree(s, i);
     }
 
-    avcodec_get_frame_defaults(&s->frame);
-
     return 0;
 }
 
-static int idcin_decode_vlcs(IdcinContext *s)
+static int idcin_decode_vlcs(IdcinContext *s, AVFrame *frame)
 {
     hnode *hnodes;
     long x, y;
@@ -182,8 +179,8 @@
     int bit_pos, node_num, dat_pos;
 
     prev = bit_pos = dat_pos = 0;
-    for (y = 0; y < (s->frame.linesize[0] * s->avctx->height);
-        y += s->frame.linesize[0]) {
+    for (y = 0; y < (frame->linesize[0] * s->avctx->height);
+        y += frame->linesize[0]) {
         for (x = y; x < y + s->avctx->width; x++) {
             node_num = s->num_huff_nodes[prev];
             hnodes = s->huff_nodes[prev];
@@ -203,7 +200,7 @@
                 bit_pos--;
             }
 
-            s->frame.data[0][x] = node_num;
+            frame->data[0][x] = node_num;
             prev = node_num;
         }
     }
@@ -219,53 +216,37 @@
     int buf_size = avpkt->size;
     IdcinContext *s = avctx->priv_data;
     const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
+    AVFrame *frame = data;
     int ret;
 
     s->buf = buf;
     s->size = buf_size;
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
-
-    if ((ret = ff_get_buffer(avctx, &s->frame))) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
-    if (idcin_decode_vlcs(s))
+    if (idcin_decode_vlcs(s, frame))
         return AVERROR_INVALIDDATA;
 
     if (pal) {
-        s->frame.palette_has_changed = 1;
+        frame->palette_has_changed = 1;
         memcpy(s->pal, pal, AVPALETTE_SIZE);
     }
     /* make the palette available on the way out */
-    memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
+    memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
 
     *got_frame = 1;
-    *(AVFrame*)data = s->frame;
 
     /* report that the buffer was completely consumed */
     return buf_size;
 }
 
-static av_cold int idcin_decode_end(AVCodecContext *avctx)
-{
-    IdcinContext *s = avctx->priv_data;
-
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
-
-    return 0;
-}
-
 AVCodec ff_idcin_decoder = {
     .name           = "idcinvideo",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_IDCIN,
     .priv_data_size = sizeof(IdcinContext),
     .init           = idcin_decode_init,
-    .close          = idcin_decode_end,
     .decode         = idcin_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("id Quake II CIN video"),
diff --git a/libavcodec/iff.c b/libavcodec/iff.c
index 8e7f8ca..716a731 100644
--- a/libavcodec/iff.c
+++ b/libavcodec/iff.c
@@ -40,7 +40,7 @@
 } mask_type;
 
 typedef struct {
-    AVFrame frame;
+    AVFrame *frame;
     int planesize;
     uint8_t * planebuf;
     uint8_t * ham_buf;      ///< temporary buffer for planar to chunky conversation
@@ -345,7 +345,7 @@
             } else if (avctx->bits_per_coded_sample == 32) {
                 avctx->pix_fmt = AV_PIX_FMT_BGR32;
             } else {
-                av_log_ask_for_sample(avctx, "unknown bits_per_coded_sample\n");
+                avpriv_request_sample(avctx, "unknown bits_per_coded_sample");
                 return AVERROR_PATCHWELCOME;
             }
         }
@@ -361,11 +361,12 @@
         return AVERROR(ENOMEM);
 
     s->bpp = avctx->bits_per_coded_sample;
-    avcodec_get_frame_defaults(&s->frame);
+    s->frame = av_frame_alloc();
+    if (!s->frame)
+        return AVERROR(ENOMEM);
 
     if ((err = extract_header(avctx, NULL)) < 0)
         return err;
-    s->frame.reference = 3;
 
     return 0;
 }
@@ -645,7 +646,7 @@
 static int unsupported(AVCodecContext *avctx)
 {
     IffContext *s = avctx->priv_data;
-    av_log_ask_for_sample(avctx, "unsupported bitmap (compression %i, bpp %i, ham %i)\n", s->compression, s->bpp, s->ham);
+    avpriv_request_sample(avctx, "bitmap (compression %i, bpp %i, ham %i)", s->compression, s->bpp, s->ham);
     return AVERROR_INVALIDDATA;
 }
 
@@ -662,18 +663,14 @@
 
     if ((res = extract_header(avctx, avpkt)) < 0)
         return res;
-    if (s->init) {
-        if ((res = avctx->reget_buffer(avctx, &s->frame)) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
-            return res;
-        }
-    } else if ((res = ff_get_buffer(avctx, &s->frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((res = ff_reget_buffer(avctx, s->frame)) < 0)
         return res;
-    } else if (avctx->bits_per_coded_sample <= 8 && avctx->pix_fmt == AV_PIX_FMT_PAL8) {
-        if ((res = cmap_read_palette(avctx, (uint32_t*)s->frame.data[1])) < 0)
+    if (!s->init && avctx->bits_per_coded_sample <= 8 &&
+        avctx->pix_fmt == AV_PIX_FMT_PAL8) {
+        if ((res = cmap_read_palette(avctx, (uint32_t*)s->frame->data[1])) < 0)
             return res;
-    } else if (avctx->pix_fmt == AV_PIX_FMT_RGB32 && avctx->bits_per_coded_sample <= 8) {
+    } else if (!s->init && avctx->bits_per_coded_sample <= 8 &&
+               avctx->pix_fmt == AV_PIX_FMT_RGB32) {
         if ((res = cmap_read_palette(avctx, s->mask_palbuf)) < 0)
             return res;
     }
@@ -683,18 +680,18 @@
     case 0:
         if (avctx->codec_tag == MKTAG('A','C','B','M')) {
             if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
-                memset(s->frame.data[0], 0, avctx->height * s->frame.linesize[0]);
+                memset(s->frame->data[0], 0, avctx->height * s->frame->linesize[0]);
                 for (plane = 0; plane < s->bpp; plane++) {
                     for(y = 0; y < avctx->height && buf < buf_end; y++ ) {
-                        uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
+                        uint8_t *row = &s->frame->data[0][ y*s->frame->linesize[0] ];
                         decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
                         buf += s->planesize;
                     }
                 }
             } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
-                memset(s->frame.data[0], 0, avctx->height * s->frame.linesize[0]);
+                memset(s->frame->data[0], 0, avctx->height * s->frame->linesize[0]);
                 for(y = 0; y < avctx->height; y++) {
-                    uint8_t *row = &s->frame.data[0][y * s->frame.linesize[0]];
+                    uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
                     memset(s->ham_buf, 0, s->planesize * 8);
                     for (plane = 0; plane < s->bpp; plane++) {
                         const uint8_t * start = buf + (plane * avctx->height + y) * s->planesize;
@@ -711,7 +708,7 @@
             int raw_width = avctx->width * (av_get_bits_per_pixel(desc) >> 3);
             int x;
             for(y = 0; y < avctx->height && buf < buf_end; y++ ) {
-                uint8_t *row = &s->frame.data[0][y * s->frame.linesize[0]];
+                uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
                 memcpy(row, buf, FFMIN(raw_width, buf_end - buf));
                 buf += raw_width;
                 if (avctx->pix_fmt == AV_PIX_FMT_BGR32) {
@@ -722,7 +719,7 @@
         } else if (avctx->codec_tag == MKTAG('I','L','B','M')) { // interleaved
             if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
                 for(y = 0; y < avctx->height; y++ ) {
-                    uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
+                    uint8_t *row = &s->frame->data[0][ y*s->frame->linesize[0] ];
                     memset(row, 0, avctx->width);
                     for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
                         decodeplane8(row, buf, FFMIN(s->planesize, buf_end - buf), plane);
@@ -731,7 +728,7 @@
                 }
             } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
                 for (y = 0; y < avctx->height; y++) {
-                    uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
+                    uint8_t *row = &s->frame->data[0][ y*s->frame->linesize[0] ];
                     memset(s->ham_buf, 0, s->planesize * 8);
                     for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
                         decodeplane8(s->ham_buf, buf, FFMIN(s->planesize, buf_end - buf), plane);
@@ -741,7 +738,7 @@
                 }
             } else { // AV_PIX_FMT_BGR32
                 for(y = 0; y < avctx->height; y++ ) {
-                    uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
+                    uint8_t *row = &s->frame->data[0][y*s->frame->linesize[0]];
                     memset(row, 0, avctx->width << 2);
                     for (plane = 0; plane < s->bpp && buf < buf_end; plane++) {
                         decodeplane32((uint32_t *) row, buf, FFMIN(s->planesize, buf_end - buf), plane);
@@ -752,13 +749,13 @@
         } else if (avctx->codec_tag == MKTAG('P','B','M',' ')) { // IFF-PBM
             if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
                 for(y = 0; y < avctx->height && buf_end > buf; y++ ) {
-                    uint8_t *row = &s->frame.data[0][y * s->frame.linesize[0]];
+                    uint8_t *row = &s->frame->data[0][y * s->frame->linesize[0]];
                     memcpy(row, buf, FFMIN(avctx->width, buf_end - buf));
                     buf += avctx->width + (avctx->width % 2); // padding if odd
                 }
             } else if (s->ham) { // IFF-PBM: HAM to AV_PIX_FMT_BGR32
                 for (y = 0; y < avctx->height && buf_end > buf; y++) {
-                    uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
+                    uint8_t *row = &s->frame->data[0][ y*s->frame->linesize[0] ];
                     memcpy(s->ham_buf, buf, FFMIN(avctx->width, buf_end - buf));
                     buf += avctx->width + (avctx->width & 1); // padding if odd
                     decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, s->planesize);
@@ -771,7 +768,7 @@
         if (avctx->codec_tag == MKTAG('I','L','B','M')) { //interleaved
             if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
                 for(y = 0; y < avctx->height ; y++ ) {
-                    uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ];
+                    uint8_t *row = &s->frame->data[0][ y*s->frame->linesize[0] ];
                     memset(row, 0, avctx->width);
                     for (plane = 0; plane < s->bpp; plane++) {
                         buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
@@ -780,7 +777,7 @@
                 }
             } else if (avctx->bits_per_coded_sample <= 8) { //8-bit (+ mask) to AV_PIX_FMT_BGR32
                 for (y = 0; y < avctx->height ; y++ ) {
-                    uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
+                    uint8_t *row = &s->frame->data[0][y*s->frame->linesize[0]];
                     memset(s->mask_buf, 0, avctx->width * sizeof(uint32_t));
                     for (plane = 0; plane < s->bpp; plane++) {
                         buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
@@ -790,7 +787,7 @@
                 }
             } else if (s->ham) { // HAM to AV_PIX_FMT_BGR32
                 for (y = 0; y < avctx->height ; y++) {
-                    uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
+                    uint8_t *row = &s->frame->data[0][y*s->frame->linesize[0]];
                     memset(s->ham_buf, 0, s->planesize * 8);
                     for (plane = 0; plane < s->bpp; plane++) {
                         buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
@@ -800,7 +797,7 @@
                 }
             } else { //AV_PIX_FMT_BGR32
                 for(y = 0; y < avctx->height ; y++ ) {
-                    uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
+                    uint8_t *row = &s->frame->data[0][y*s->frame->linesize[0]];
                     memset(row, 0, avctx->width << 2);
                     for (plane = 0; plane < s->bpp; plane++) {
                         buf += decode_byterun(s->planebuf, s->planesize, buf, buf_end);
@@ -811,12 +808,12 @@
         } else if (avctx->codec_tag == MKTAG('P','B','M',' ')) { // IFF-PBM
             if (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
                 for(y = 0; y < avctx->height ; y++ ) {
-                    uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
+                    uint8_t *row = &s->frame->data[0][y*s->frame->linesize[0]];
                     buf += decode_byterun(row, avctx->width, buf, buf_end);
                 }
             } else if (s->ham) { // IFF-PBM: HAM to AV_PIX_FMT_BGR32
                 for (y = 0; y < avctx->height ; y++) {
-                    uint8_t *row = &s->frame.data[0][y*s->frame.linesize[0]];
+                    uint8_t *row = &s->frame->data[0][y*s->frame->linesize[0]];
                     buf += decode_byterun(s->ham_buf, avctx->width, buf, buf_end);
                     decode_ham_plane32((uint32_t *) row, s->ham_buf, s->ham_palbuf, s->planesize);
                 }
@@ -825,7 +822,7 @@
         } else if (avctx->codec_tag == MKTAG('D','E','E','P')) { // IFF-DEEP
             const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
             if (av_get_bits_per_pixel(desc) == 32)
-                decode_deep_rle32(s->frame.data[0], buf, buf_size, avctx->width, avctx->height, s->frame.linesize[0]);
+                decode_deep_rle32(s->frame->data[0], buf, buf_size, avctx->width, avctx->height, s->frame->linesize[0]);
             else
                 return unsupported(avctx);
         }
@@ -833,9 +830,9 @@
     case 4:
         bytestream2_init(&gb, buf, buf_size);
         if (avctx->codec_tag == MKTAG('R','G','B','8'))
-            decode_rgb8(&gb, s->frame.data[0], avctx->width, avctx->height, s->frame.linesize[0]);
+            decode_rgb8(&gb, s->frame->data[0], avctx->width, avctx->height, s->frame->linesize[0]);
         else if (avctx->codec_tag == MKTAG('R','G','B','N'))
-            decode_rgbn(&gb, s->frame.data[0], avctx->width, avctx->height, s->frame.linesize[0]);
+            decode_rgbn(&gb, s->frame->data[0], avctx->width, avctx->height, s->frame->linesize[0]);
         else
             return unsupported(avctx);
         break;
@@ -843,7 +840,7 @@
         if (avctx->codec_tag == MKTAG('D','E','E','P')) {
             const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
             if (av_get_bits_per_pixel(desc) == 32)
-                decode_deep_tvdc32(s->frame.data[0], buf, buf_size, avctx->width, avctx->height, s->frame.linesize[0], s->tvdc);
+                decode_deep_tvdc32(s->frame->data[0], buf, buf_size, avctx->width, avctx->height, s->frame->linesize[0], s->tvdc);
             else
                 return unsupported(avctx);
         } else
@@ -853,16 +850,18 @@
         return unsupported(avctx);
     }
 
+    if ((res = av_frame_ref(data, s->frame)) < 0)
+        return res;
+
     *got_frame = 1;
-    *(AVFrame*)data = s->frame;
+
     return buf_size;
 }
 
 static av_cold int decode_end(AVCodecContext *avctx)
 {
     IffContext *s = avctx->priv_data;
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+    av_frame_free(&s->frame);
     av_freep(&s->planebuf);
     av_freep(&s->ham_buf);
     av_freep(&s->ham_palbuf);
diff --git a/libavcodec/iirfilter.c b/libavcodec/iirfilter.c
index 610add1..049150e 100644
--- a/libavcodec/iirfilter.c
+++ b/libavcodec/iirfilter.c
@@ -311,6 +311,13 @@
     av_free(coeffs);
 }
 
+void ff_iir_filter_init(FFIIRFilterContext *f) {
+    f->filter_flt = ff_iir_filter_flt;
+
+    if (HAVE_MIPSFPU)
+        ff_iir_filter_init_mips(f);
+}
+
 #ifdef TEST
 #include <stdio.h>
 
diff --git a/libavcodec/iirfilter.h b/libavcodec/iirfilter.h
index b29e035..4ea6642 100644
--- a/libavcodec/iirfilter.h
+++ b/libavcodec/iirfilter.h
@@ -47,6 +47,29 @@
     FF_FILTER_MODE_BANDSTOP,
 };
 
+typedef struct FFIIRFilterContext {
+    /**
+    * Perform IIR filtering on floating-point input samples.
+    *
+    * @param coeffs pointer to filter coefficients
+    * @param state  pointer to filter state
+    * @param size   input length
+    * @param src    source samples
+    * @param sstep  source stride
+    * @param dst    filtered samples (destination may be the same as input)
+    * @param dstep  destination stride
+    */
+    void (*filter_flt)(const struct FFIIRFilterCoeffs *coeffs,
+                        struct FFIIRFilterState *state, int size,
+                        const float *src, int sstep, float *dst, int dstep);
+} FFIIRFilterContext;
+
+/**
+ * Initialize FFIIRFilterContext
+ */
+void ff_iir_filter_init(FFIIRFilterContext *f);
+void ff_iir_filter_init_mips(FFIIRFilterContext *f);
+
 /**
  * Initialize filter coefficients.
  *
diff --git a/libavcodec/imc.c b/libavcodec/imc.c
index 0f43461..eb7c255 100644
--- a/libavcodec/imc.c
+++ b/libavcodec/imc.c
@@ -182,7 +182,7 @@
         avctx->channels = 1;
 
     if (avctx->channels > 2) {
-        av_log_ask_for_sample(avctx, "Number of channels is not supported\n");
+        avpriv_request_sample(avctx, "Number of channels > 2");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -779,8 +779,7 @@
     stream_format_code = get_bits(&q->gb, 3);
 
     if (stream_format_code & 1) {
-        av_log_ask_for_sample(avctx, "Stream format %X is not supported\n",
-                              stream_format_code);
+        avpriv_request_sample(avctx, "Stream format %X", stream_format_code);
         return AVERROR_PATCHWELCOME;
     }
 
@@ -948,10 +947,8 @@
 
     /* get output buffer */
     frame->nb_samples = COEFFS;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     for (i = 0; i < avctx->channels; i++) {
         q->out_samples = (float *)frame->extended_data[i];
diff --git a/libavcodec/imgconvert.c b/libavcodec/imgconvert.c
index 7b5be5c..616cf78 100644
--- a/libavcodec/imgconvert.c
+++ b/libavcodec/imgconvert.c
@@ -215,25 +215,6 @@
     return loss;
 }
 
-#if FF_API_FIND_BEST_PIX_FMT
-enum AVPixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum AVPixelFormat src_pix_fmt,
-                                            int has_alpha, int *loss_ptr)
-{
-    enum AVPixelFormat dst_pix_fmt;
-    int i;
-
-    if (loss_ptr) /* all losses count (for backward compatibility) */
-        *loss_ptr = 0;
-
-    dst_pix_fmt = AV_PIX_FMT_NONE; /* so first iteration doesn't have to be treated special */
-    for(i = 0; i< FFMIN(AV_PIX_FMT_NB, 64); i++){
-        if (pix_fmt_mask & (1ULL << i))
-            dst_pix_fmt = avcodec_find_best_pix_fmt_of_2(dst_pix_fmt, i, src_pix_fmt, has_alpha, loss_ptr);
-    }
-    return dst_pix_fmt;
-}
-#endif /* FF_API_FIND_BEST_PIX_FMT */
-
 enum AVPixelFormat avcodec_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
                                             enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
 {
@@ -492,6 +473,8 @@
     return 0;
 }
 
+#if FF_API_DEINTERLACE
+
 #if !HAVE_MMX_EXTERNAL
 /* filter parameters: [-1 4 2 4 -1] // 8 */
 static void deinterlace_line_c(uint8_t *dst,
@@ -500,7 +483,7 @@
                              const uint8_t *lum,
                              int size)
 {
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
     int sum;
 
     for(;size > 0;size--) {
@@ -523,7 +506,7 @@
                                        uint8_t *lum_m2, uint8_t *lum_m1,
                                        uint8_t *lum, int size)
 {
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
     int sum;
 
     for(;size > 0;size--) {
@@ -651,6 +634,8 @@
     return 0;
 }
 
+#endif /* FF_API_DEINTERLACE */
+
 #ifdef TEST
 
 int main(void){
diff --git a/libavcodec/indeo2.c b/libavcodec/indeo2.c
index ec6ff3c..f8e7415 100644
--- a/libavcodec/indeo2.c
+++ b/libavcodec/indeo2.c
@@ -29,6 +29,7 @@
 #include "avcodec.h"
 #include "get_bits.h"
 #include "indeo2data.h"
+#include "internal.h"
 #include "mathops.h"
 
 typedef struct Ir2Context{
@@ -148,12 +149,8 @@
     AVFrame * const p    = &s->picture;
     int start, ret;
 
-    p->reference = 3;
-    p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
-    if ((ret = avctx->reget_buffer(avctx, p)) < 0) {
-        av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, p)) < 0)
         return ret;
-    }
 
     start = 48; /* hardcoded for now */
 
@@ -203,7 +200,9 @@
             return ret;
     }
 
-    *picture   = s->picture;
+    if ((ret = av_frame_ref(picture, &s->picture)) < 0)
+        return ret;
+
     *got_frame = 1;
 
     return buf_size;
@@ -219,6 +218,8 @@
 
     avctx->pix_fmt= AV_PIX_FMT_YUV410P;
 
+    avcodec_get_frame_defaults(&ic->picture);
+
     ir2_vlc.table = vlc_tables;
     ir2_vlc.table_allocated = 1 << CODE_VLC_BITS;
 #ifdef BITSTREAM_READER_LE
@@ -239,8 +240,7 @@
     Ir2Context * const ic = avctx->priv_data;
     AVFrame *pic = &ic->picture;
 
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
+    av_frame_unref(pic);
 
     return 0;
 }
diff --git a/libavcodec/indeo3.c b/libavcodec/indeo3.c
index a94b087..1ad1e4b 100644
--- a/libavcodec/indeo3.c
+++ b/libavcodec/indeo3.c
@@ -33,9 +33,9 @@
 #include "libavutil/intreadwrite.h"
 #include "avcodec.h"
 #include "copy_block.h"
-#include "dsputil.h"
 #include "bytestream.h"
 #include "get_bits.h"
+#include "hpeldsp.h"
 #include "internal.h"
 
 #include "indeo3data.h"
@@ -82,8 +82,7 @@
 
 typedef struct Indeo3DecodeContext {
     AVCodecContext *avctx;
-    AVFrame         frame;
-    DSPContext      dsp;
+    HpelDSPContext  hdsp;
 
     GetBitContext   gb;
     int             need_resync;
@@ -227,7 +226,7 @@
  *  @param plane    pointer to the plane descriptor
  *  @param cell     pointer to the cell  descriptor
  */
-static void copy_cell(Indeo3DecodeContext *ctx, Plane *plane, Cell *cell)
+static int copy_cell(Indeo3DecodeContext *ctx, Plane *plane, Cell *cell)
 {
     int     h, w, mv_x, mv_y, offset, offset_dst;
     uint8_t *src, *dst;
@@ -240,6 +239,16 @@
     mv_x        = cell->mv_ptr[1];
     }else
         mv_x= mv_y= 0;
+
+    /* -1 because there is an extra line on top for prediction */
+    if ((cell->ypos << 2) + mv_y < -1 || (cell->xpos << 2) + mv_x < 0 ||
+        ((cell->ypos + cell->height) << 2) + mv_y >= plane->height    ||
+        ((cell->xpos + cell->width)  << 2) + mv_x >= plane->width) {
+        av_log(ctx->avctx, AV_LOG_ERROR,
+               "Motion vectors point out of the frame.\n");
+        return AVERROR_INVALIDDATA;
+    }
+
     offset      = offset_dst + mv_y * plane->pitch + mv_x;
     src         = plane->pixels[ctx->buf_sel ^ 1] + offset;
 
@@ -249,12 +258,12 @@
         /* copy using 16xH blocks */
         if (!((cell->xpos << 2) & 15) && w >= 4) {
             for (; w >= 4; src += 16, dst += 16, w -= 4)
-                ctx->dsp.put_no_rnd_pixels_tab[0][0](dst, src, plane->pitch, h);
+                ctx->hdsp.put_no_rnd_pixels_tab[0][0](dst, src, plane->pitch, h);
         }
 
         /* copy using 8xH blocks */
         if (!((cell->xpos << 2) & 7) && w >= 2) {
-            ctx->dsp.put_no_rnd_pixels_tab[1][0](dst, src, plane->pitch, h);
+            ctx->hdsp.put_no_rnd_pixels_tab[1][0](dst, src, plane->pitch, h);
             w -= 2;
             src += 8;
             dst += 8;
@@ -267,6 +276,8 @@
             dst += 4;
         }
     }
+
+    return 0;
 }
 
 
@@ -588,29 +599,29 @@
     offset = (cell->ypos << 2) * plane->pitch + (cell->xpos << 2);
     block  =  plane->pixels[ctx->buf_sel] + offset;
 
-    if (cell->mv_ptr) {
-        mv_y      = cell->mv_ptr[0];
-        mv_x      = cell->mv_ptr[1];
-        if (   mv_x + 4*cell->xpos < 0
-            || mv_y + 4*cell->ypos < 0
-            || mv_x + 4*cell->xpos + 4*cell->width  > plane->width
-            || mv_y + 4*cell->ypos + 4*cell->height > plane->height) {
-            av_log(avctx, AV_LOG_ERROR, "motion vector %d %d outside reference\n", mv_x + 4*cell->xpos, mv_y + 4*cell->ypos);
-            return AVERROR_INVALIDDATA;
-        }
-    }
-
     if (!cell->mv_ptr) {
         /* use previous line as reference for INTRA cells */
         ref_block = block - plane->pitch;
     } else if (mode >= 10) {
         /* for mode 10 and 11 INTER first copy the predicted cell into the current one */
         /* so we don't need to do data copying for each RLE code later */
-        copy_cell(ctx, plane, cell);
+        int ret = copy_cell(ctx, plane, cell);
+        if (ret < 0)
+            return ret;
     } else {
         /* set the pointer to the reference pixels for modes 0-4 INTER */
         mv_y      = cell->mv_ptr[0];
         mv_x      = cell->mv_ptr[1];
+
+        /* -1 because there is an extra line on top for prediction */
+        if ((cell->ypos << 2) + mv_y < -1 || (cell->xpos << 2) + mv_x < 0 ||
+            ((cell->ypos + cell->height) << 2) + mv_y >= plane->height    ||
+            ((cell->xpos + cell->width)  << 2) + mv_x >= plane->width) {
+            av_log(ctx->avctx, AV_LOG_ERROR,
+                   "Motion vectors point out of the frame.\n");
+            return AVERROR_INVALIDDATA;
+        }
+
         offset   += mv_y * plane->pitch + mv_x;
         ref_block = plane->pixels[ctx->buf_sel ^ 1] + offset;
     }
@@ -744,8 +755,7 @@
                          const int depth, const int strip_width)
 {
     Cell    curr_cell;
-    int     bytes_used;
-    int mv_x, mv_y;
+    int     bytes_used, ret;
 
     if (depth <= 0) {
         av_log(avctx, AV_LOG_ERROR, "Stack overflow (corrupted binary tree)!\n");
@@ -797,18 +807,8 @@
                 if (!curr_cell.mv_ptr)
                     return AVERROR_INVALIDDATA;
 
-                mv_y = curr_cell.mv_ptr[0];
-                mv_x = curr_cell.mv_ptr[1];
-                if (   mv_x + 4*curr_cell.xpos < 0
-                    || mv_y + 4*curr_cell.ypos < 0
-                    || mv_x + 4*curr_cell.xpos + 4*curr_cell.width  > plane->width
-                    || mv_y + 4*curr_cell.ypos + 4*curr_cell.height > plane->height) {
-                    av_log(avctx, AV_LOG_ERROR, "motion vector %d %d outside reference\n", mv_x + 4*curr_cell.xpos, mv_y + 4*curr_cell.ypos);
-                    return AVERROR_INVALIDDATA;
-                }
-
-                copy_cell(ctx, plane, &curr_cell);
-                return 0;
+                ret = copy_cell(ctx, plane, &curr_cell);
+                return ret;
             }
             break;
         case INTER_DATA:
@@ -895,17 +895,20 @@
 static int decode_frame_headers(Indeo3DecodeContext *ctx, AVCodecContext *avctx,
                                 const uint8_t *buf, int buf_size)
 {
-    const uint8_t   *buf_ptr = buf, *bs_hdr;
+    GetByteContext gb;
+    const uint8_t   *bs_hdr;
     uint32_t        frame_num, word2, check_sum, data_size;
     uint32_t        y_offset, u_offset, v_offset, starts[3], ends[3];
     uint16_t        height, width;
     int             i, j;
 
+    bytestream2_init(&gb, buf, buf_size);
+
     /* parse and check the OS header */
-    frame_num = bytestream_get_le32(&buf_ptr);
-    word2     = bytestream_get_le32(&buf_ptr);
-    check_sum = bytestream_get_le32(&buf_ptr);
-    data_size = bytestream_get_le32(&buf_ptr);
+    frame_num = bytestream2_get_le32(&gb);
+    word2     = bytestream2_get_le32(&gb);
+    check_sum = bytestream2_get_le32(&gb);
+    data_size = bytestream2_get_le32(&gb);
 
     if ((frame_num ^ word2 ^ data_size ^ OS_HDR_ID) != check_sum) {
         av_log(avctx, AV_LOG_ERROR, "OS header checksum mismatch!\n");
@@ -913,29 +916,27 @@
     }
 
     /* parse the bitstream header */
-    bs_hdr = buf_ptr;
-    buf_size -= 16;
+    bs_hdr = gb.buffer;
 
-    if (bytestream_get_le16(&buf_ptr) != 32) {
+    if (bytestream2_get_le16(&gb) != 32) {
         av_log(avctx, AV_LOG_ERROR, "Unsupported codec version!\n");
         return AVERROR_INVALIDDATA;
     }
 
     ctx->frame_num   =  frame_num;
-    ctx->frame_flags =  bytestream_get_le16(&buf_ptr);
-    ctx->data_size   = (bytestream_get_le32(&buf_ptr) + 7) >> 3;
-    ctx->cb_offset   = *buf_ptr++;
+    ctx->frame_flags =  bytestream2_get_le16(&gb);
+    ctx->data_size   = (bytestream2_get_le32(&gb) + 7) >> 3;
+    ctx->cb_offset   =  bytestream2_get_byte(&gb);
 
     if (ctx->data_size == 16)
         return 4;
-    if (ctx->data_size > buf_size)
-        ctx->data_size = buf_size;
+    ctx->data_size = FFMIN(ctx->data_size, buf_size - 16);
 
-    buf_ptr += 3; // skip reserved byte and checksum
+    bytestream2_skip(&gb, 3); // skip reserved byte and checksum
 
     /* check frame dimensions */
-    height = bytestream_get_le16(&buf_ptr);
-    width  = bytestream_get_le16(&buf_ptr);
+    height = bytestream2_get_le16(&gb);
+    width  = bytestream2_get_le16(&gb);
     if (av_image_check_size(width, height, 0, avctx))
         return AVERROR_INVALIDDATA;
 
@@ -957,9 +958,10 @@
         avcodec_set_dimensions(avctx, width, height);
     }
 
-    y_offset = bytestream_get_le32(&buf_ptr);
-    v_offset = bytestream_get_le32(&buf_ptr);
-    u_offset = bytestream_get_le32(&buf_ptr);
+    y_offset = bytestream2_get_le32(&gb);
+    v_offset = bytestream2_get_le32(&gb);
+    u_offset = bytestream2_get_le32(&gb);
+    bytestream2_skip(&gb, 4);
 
     /* unfortunately there is no common order of planes in the buffer */
     /* so we use that sorting algo for determining planes data sizes  */
@@ -978,6 +980,7 @@
     ctx->v_data_size = ends[1] - starts[1];
     ctx->u_data_size = ends[2] - starts[2];
     if (FFMAX3(y_offset, v_offset, u_offset) >= ctx->data_size - 16 ||
+        FFMIN3(y_offset, v_offset, u_offset) < gb.buffer - bs_hdr + 16 ||
         FFMIN3(ctx->y_data_size, ctx->v_data_size, ctx->u_data_size) <= 0) {
         av_log(avctx, AV_LOG_ERROR, "One of the y/u/v offsets is invalid\n");
         return AVERROR_INVALIDDATA;
@@ -986,7 +989,7 @@
     ctx->y_data_ptr = bs_hdr + y_offset;
     ctx->v_data_ptr = bs_hdr + v_offset;
     ctx->u_data_ptr = bs_hdr + u_offset;
-    ctx->alt_quant  = buf_ptr + sizeof(uint32_t);
+    ctx->alt_quant  = gb.buffer;
 
     if (ctx->data_size == 16) {
         av_log(avctx, AV_LOG_DEBUG, "Sync frame encountered!\n");
@@ -994,12 +997,12 @@
     }
 
     if (ctx->frame_flags & BS_8BIT_PEL) {
-        av_log_ask_for_sample(avctx, "8-bit pixel format\n");
+        avpriv_request_sample(avctx, "8-bit pixel format");
         return AVERROR_PATCHWELCOME;
     }
 
     if (ctx->frame_flags & BS_MV_X_HALF || ctx->frame_flags & BS_MV_Y_HALF) {
-        av_log_ask_for_sample(avctx, "halfpel motion vectors\n");
+        avpriv_request_sample(avctx, "Halfpel motion vectors");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -1048,11 +1051,10 @@
 
     ctx->avctx     = avctx;
     avctx->pix_fmt = AV_PIX_FMT_YUV410P;
-    avcodec_get_frame_defaults(&ctx->frame);
 
     build_requant_tab();
 
-    ff_dsputil_init(&ctx->dsp, avctx);
+    ff_hpeldsp_init(&ctx->hdsp, avctx->flags);
 
     return allocate_frame_buffers(ctx, avctx, avctx->width, avctx->height);
 }
@@ -1064,6 +1066,7 @@
     Indeo3DecodeContext *ctx = avctx->priv_data;
     const uint8_t *buf = avpkt->data;
     int buf_size       = avpkt->size;
+    AVFrame *frame     = data;
     int res;
 
     res = decode_frame_headers(ctx, avctx, buf, buf_size);
@@ -1089,14 +1092,8 @@
     /* use BS_BUFFER flag for buffer switching */
     ctx->buf_sel = (ctx->frame_flags >> BS_BUFFER) & 1;
 
-    if (ctx->frame.data[0])
-        avctx->release_buffer(avctx, &ctx->frame);
-
-    ctx->frame.reference = 0;
-    if ((res = ff_get_buffer(avctx, &ctx->frame)) < 0) {
-        av_log(ctx->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
         return res;
-    }
 
     /* decode luma plane */
     if ((res = decode_plane(ctx, avctx, ctx->planes, ctx->y_data_ptr, ctx->y_data_size, 40)))
@@ -1110,17 +1107,16 @@
         return res;
 
     output_plane(&ctx->planes[0], ctx->buf_sel,
-                 ctx->frame.data[0], ctx->frame.linesize[0],
+                 frame->data[0], frame->linesize[0],
                  avctx->height);
     output_plane(&ctx->planes[1], ctx->buf_sel,
-                 ctx->frame.data[1], ctx->frame.linesize[1],
+                 frame->data[1], frame->linesize[1],
                  (avctx->height + 3) >> 2);
     output_plane(&ctx->planes[2], ctx->buf_sel,
-                 ctx->frame.data[2], ctx->frame.linesize[2],
+                 frame->data[2], frame->linesize[2],
                  (avctx->height + 3) >> 2);
 
     *got_frame = 1;
-    *(AVFrame*)data = ctx->frame;
 
     return buf_size;
 }
@@ -1128,13 +1124,8 @@
 
 static av_cold int decode_close(AVCodecContext *avctx)
 {
-    Indeo3DecodeContext *ctx = avctx->priv_data;
-
     free_frame_buffers(avctx->priv_data);
 
-    if (ctx->frame.data[0])
-        avctx->release_buffer(avctx, &ctx->frame);
-
     return 0;
 }
 
@@ -1146,6 +1137,6 @@
     .init           = decode_init,
     .close          = decode_close,
     .decode         = decode_frame,
-    .long_name      = NULL_IF_CONFIG_SMALL("Intel Indeo 3"),
     .capabilities   = CODEC_CAP_DR1,
+    .long_name      = NULL_IF_CONFIG_SMALL("Intel Indeo 3"),
 };
diff --git a/libavcodec/indeo4.c b/libavcodec/indeo4.c
index e810003..5b107fb 100644
--- a/libavcodec/indeo4.c
+++ b/libavcodec/indeo4.c
@@ -56,8 +56,8 @@
     int             is_2d_trans;
 } transforms[18] = {
     { ff_ivi_inverse_haar_8x8,  ff_ivi_dc_haar_2d,       1 },
-    { NULL, NULL, 0 }, /* inverse Haar 8x1 */
-    { NULL, NULL, 0 }, /* inverse Haar 1x8 */
+    { ff_ivi_inverse_haar_8x1,  ff_ivi_dc_haar_2d,       1 },
+    { ff_ivi_inverse_haar_1x8,  ff_ivi_dc_haar_2d,       1 },
     { ff_ivi_put_pixels_8x8,    ff_ivi_put_dc_pixel_8x8, 1 },
     { ff_ivi_inverse_slant_8x8, ff_ivi_dc_slant_2d,      1 },
     { ff_ivi_row_slant8,        ff_ivi_dc_row_slant,     1 },
@@ -331,12 +331,12 @@
             transform_id = get_bits(&ctx->gb, 5);
             if (transform_id >= FF_ARRAY_ELEMS(transforms) ||
                 !transforms[transform_id].inv_trans) {
-                av_log_ask_for_sample(avctx, "Unimplemented transform: %d!\n", transform_id);
+                avpriv_request_sample(avctx, "Transform %d", transform_id);
                 return AVERROR_PATCHWELCOME;
             }
             if ((transform_id >= 7 && transform_id <= 9) ||
                  transform_id == 17) {
-                av_log_ask_for_sample(avctx, "DCT transform not supported yet!\n");
+                avpriv_request_sample(avctx, "DCT transform");
                 return AVERROR_PATCHWELCOME;
             }
 
diff --git a/libavcodec/indeo5.c b/libavcodec/indeo5.c
index 52dddd2..65bd9f2 100644
--- a/libavcodec/indeo5.c
+++ b/libavcodec/indeo5.c
@@ -643,8 +643,6 @@
     ctx->pic_conf.tile_height   = avctx->height;
     ctx->pic_conf.luma_bands    = ctx->pic_conf.chroma_bands = 1;
 
-    avcodec_get_frame_defaults(&ctx->frame);
-
     result = ff_ivi_init_planes(ctx->planes, &ctx->pic_conf);
     if (result) {
         av_log(avctx, AV_LOG_ERROR, "Couldn't allocate color planes!\n");
diff --git a/libavcodec/internal.h b/libavcodec/internal.h
index 3402cc2..4962f06 100644
--- a/libavcodec/internal.h
+++ b/libavcodec/internal.h
@@ -26,35 +26,35 @@
 
 #include <stdint.h>
 
+#include "libavutil/buffer.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/pixfmt.h"
 #include "avcodec.h"
+#include "config.h"
 
-#define FF_SANE_NB_CHANNELS 128U
+#define FF_SANE_NB_CHANNELS 63U
 
-typedef struct InternalBuffer {
-    uint8_t *base[AV_NUM_DATA_POINTERS];
-    uint8_t *data[AV_NUM_DATA_POINTERS];
-    int linesize[AV_NUM_DATA_POINTERS];
-    int width;
-    int height;
-    enum AVPixelFormat pix_fmt;
-} InternalBuffer;
+typedef struct FramePool {
+    /**
+     * Pools for each data plane. For audio all the planes have the same size,
+     * so only pools[0] is used.
+     */
+    AVBufferPool *pools[4];
+
+    /*
+     * Pool parameters
+     */
+    int format;
+    int width, height;
+    int stride_align[AV_NUM_DATA_POINTERS];
+    int linesize[4];
+    int planes;
+    int channels;
+    int samples;
+} FramePool;
 
 typedef struct AVCodecInternal {
     /**
-     * internal buffer count
-     * used by default get/release/reget_buffer().
-     */
-    int buffer_count;
-
-    /**
-     * internal buffers
-     * used by default get/release/reget_buffer().
-     */
-    InternalBuffer *buffer;
-
-    /**
      * Whether the parent AVCodecContext is a copy of the context which had
      * init() called on it.
      * This is used by multithreading - shared tables and picture pointers
@@ -62,7 +62,22 @@
      */
     int is_copy;
 
-#if FF_API_OLD_DECODE_AUDIO
+    /**
+     * Whether to allocate progress for frame threading.
+     *
+     * The codec must set it to 1 if it uses ff_thread_await/report_progress(),
+     * then progress will be allocated in ff_thread_get_buffer(). The frames
+     * then MUST be freed with ff_thread_release_buffer().
+     *
+     * If the codec does not need to call the progress functions (there are no
+     * dependencies between the frames), it should leave this at 0. Then it can
+     * decode straight to the user-provided frames (which the user will then
+     * free with av_frame_unref()), there is no need to call
+     * ff_thread_release_buffer().
+     */
+    int allocate_progress;
+
+#if FF_API_OLD_ENCODE_AUDIO
     /**
      * Internal sample count used by avcodec_encode_audio() to fabricate pts.
      * Can be removed along with avcodec_encode_audio().
@@ -76,11 +91,9 @@
      */
     int last_audio_frame;
 
-    /**
-     * The data for the last allocated audio frame.
-     * Stored here so we can free it.
-     */
-    uint8_t *audio_data;
+    AVFrame to_free;
+
+    FramePool *pool;
 
     /**
      * temporary buffer used for encoders to store their bitstream
@@ -122,7 +135,7 @@
 /**
  * does needed setup of pkt_pts/pos and such for (re)get_buffer();
  */
-void ff_init_buffer_info(AVCodecContext *s, AVFrame *frame);
+int ff_init_buffer_info(AVCodecContext *s, AVFrame *frame);
 
 
 void avpriv_color_frame(AVFrame *frame, const int color[4]);
@@ -184,7 +197,13 @@
  * AVCodecContext.get_buffer() and should be used instead calling get_buffer()
  * directly.
  */
-int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame);
+int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags);
+
+/**
+ * Identical in function to av_frame_make_writable(), except it uses
+ * ff_get_buffer() to allocate the buffer when needed.
+ */
+int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame);
 
 int ff_thread_can_start_frame(AVCodecContext *avctx);
 
@@ -192,10 +211,6 @@
 
 int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx);
 
-void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, uint8_t *mbskip_table,
-                         uint8_t *visualization_buffer[3], int *low_delay,
-                         int mb_width, int mb_height, int mb_stride, int quarter_sample);
-
 /**
  * Call avcodec_open2 recursively by decrementing counter, unlocking mutex,
  * calling the function and then restoring again. Assumes the mutex is
@@ -213,4 +228,8 @@
  */
 int avpriv_bprint_to_extradata(AVCodecContext *avctx, struct AVBPrint *buf);
 
+const uint8_t *avpriv_find_start_code(const uint8_t *p,
+                                      const uint8_t *end,
+                                      uint32_t *state);
+
 #endif /* AVCODEC_INTERNAL_H */
diff --git a/libavcodec/interplayvideo.c b/libavcodec/interplayvideo.c
index e0550a7..4f710b8 100644
--- a/libavcodec/interplayvideo.c
+++ b/libavcodec/interplayvideo.c
@@ -40,7 +40,7 @@
 
 #include "avcodec.h"
 #include "bytestream.h"
-#include "dsputil.h"
+#include "hpeldsp.h"
 #define BITSTREAM_READER_LE
 #include "get_bits.h"
 #include "internal.h"
@@ -50,10 +50,9 @@
 typedef struct IpvideoContext {
 
     AVCodecContext *avctx;
-    DSPContext dsp;
-    AVFrame second_last_frame;
-    AVFrame last_frame;
-    AVFrame current_frame;
+    HpelDSPContext hdsp;
+    AVFrame *second_last_frame;
+    AVFrame *last_frame;
     const unsigned char *decoding_map;
     int decoding_map_size;
 
@@ -67,10 +66,10 @@
     uint32_t pal[256];
 } IpvideoContext;
 
-static int copy_from(IpvideoContext *s, AVFrame *src, int delta_x, int delta_y)
+static int copy_from(IpvideoContext *s, AVFrame *src, AVFrame *dst, int delta_x, int delta_y)
 {
-    int current_offset = s->pixel_ptr - s->current_frame.data[0];
-    int motion_offset = current_offset + delta_y * s->current_frame.linesize[0]
+    int current_offset = s->pixel_ptr - dst->data[0];
+    int motion_offset = current_offset + delta_y * dst->linesize[0]
                        + delta_x * (1 + s->is_16bpp);
     if (motion_offset < 0) {
         av_log(s->avctx, AV_LOG_ERROR, "motion offset < 0 (%d)\n", motion_offset);
@@ -84,22 +83,22 @@
         av_log(s->avctx, AV_LOG_ERROR, "Invalid decode type, corrupted header?\n");
         return AVERROR(EINVAL);
     }
-    s->dsp.put_pixels_tab[!s->is_16bpp][0](s->pixel_ptr, src->data[0] + motion_offset,
-                                           s->current_frame.linesize[0], 8);
+    s->hdsp.put_pixels_tab[!s->is_16bpp][0](s->pixel_ptr, src->data[0] + motion_offset,
+                                            dst->linesize[0], 8);
     return 0;
 }
 
-static int ipvideo_decode_block_opcode_0x0(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0x0(IpvideoContext *s, AVFrame *frame)
 {
-    return copy_from(s, &s->last_frame, 0, 0);
+    return copy_from(s, s->last_frame, frame, 0, 0);
 }
 
-static int ipvideo_decode_block_opcode_0x1(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0x1(IpvideoContext *s, AVFrame *frame)
 {
-    return copy_from(s, &s->second_last_frame, 0, 0);
+    return copy_from(s, s->second_last_frame, frame, 0, 0);
 }
 
-static int ipvideo_decode_block_opcode_0x2(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0x2(IpvideoContext *s, AVFrame *frame)
 {
     unsigned char B;
     int x, y;
@@ -120,10 +119,10 @@
     }
 
     av_dlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
-    return copy_from(s, &s->second_last_frame, x, y);
+    return copy_from(s, s->second_last_frame, frame, x, y);
 }
 
-static int ipvideo_decode_block_opcode_0x3(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0x3(IpvideoContext *s, AVFrame *frame)
 {
     unsigned char B;
     int x, y;
@@ -146,10 +145,10 @@
     }
 
     av_dlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
-    return copy_from(s, &s->current_frame, x, y);
+    return copy_from(s, frame, frame, x, y);
 }
 
-static int ipvideo_decode_block_opcode_0x4(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0x4(IpvideoContext *s, AVFrame *frame)
 {
     int x, y;
     unsigned char B, BL, BH;
@@ -167,10 +166,10 @@
     y = -8 + BH;
 
     av_dlog(s->avctx, "motion byte = %d, (x, y) = (%d, %d)\n", B, x, y);
-    return copy_from(s, &s->last_frame, x, y);
+    return copy_from(s, s->last_frame, frame, x, y);
 }
 
-static int ipvideo_decode_block_opcode_0x5(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0x5(IpvideoContext *s, AVFrame *frame)
 {
     signed char x, y;
 
@@ -180,10 +179,10 @@
     y = bytestream2_get_byte(&s->stream_ptr);
 
     av_dlog(s->avctx, "motion bytes = %d, %d\n", x, y);
-    return copy_from(s, &s->last_frame, x, y);
+    return copy_from(s, s->last_frame, frame, x, y);
 }
 
-static int ipvideo_decode_block_opcode_0x6(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0x6(IpvideoContext *s, AVFrame *frame)
 {
     /* mystery opcode? skip multiple blocks? */
     av_log(s->avctx, AV_LOG_ERROR, "Help! Mystery opcode 0x6 seen\n");
@@ -192,7 +191,7 @@
     return 0;
 }
 
-static int ipvideo_decode_block_opcode_0x7(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0x7(IpvideoContext *s, AVFrame *frame)
 {
     int x, y;
     unsigned char P[2];
@@ -231,7 +230,7 @@
     return 0;
 }
 
-static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0x8(IpvideoContext *s, AVFrame *frame)
 {
     int x, y;
     unsigned char P[4];
@@ -304,7 +303,7 @@
     return 0;
 }
 
-static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0x9(IpvideoContext *s, AVFrame *frame)
 {
     int x, y;
     unsigned char P[4];
@@ -369,7 +368,7 @@
     return 0;
 }
 
-static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0xA(IpvideoContext *s, AVFrame *frame)
 {
     int x, y;
     unsigned char P[8];
@@ -430,7 +429,7 @@
     return 0;
 }
 
-static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0xB(IpvideoContext *s, AVFrame *frame)
 {
     int y;
 
@@ -444,7 +443,7 @@
     return 0;
 }
 
-static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0xC(IpvideoContext *s, AVFrame *frame)
 {
     int x, y;
 
@@ -463,7 +462,7 @@
     return 0;
 }
 
-static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0xD(IpvideoContext *s, AVFrame *frame)
 {
     int y;
     unsigned char P[2];
@@ -483,7 +482,7 @@
     return 0;
 }
 
-static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0xE(IpvideoContext *s, AVFrame *frame)
 {
     int y;
     unsigned char pix;
@@ -500,7 +499,7 @@
     return 0;
 }
 
-static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0xF(IpvideoContext *s, AVFrame *frame)
 {
     int x, y;
     unsigned char sample[2];
@@ -521,7 +520,7 @@
     return 0;
 }
 
-static int ipvideo_decode_block_opcode_0x6_16(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0x6_16(IpvideoContext *s, AVFrame *frame)
 {
     signed char x, y;
 
@@ -530,10 +529,10 @@
     y = bytestream2_get_byte(&s->stream_ptr);
 
     av_dlog(s->avctx, "motion bytes = %d, %d\n", x, y);
-    return copy_from(s, &s->second_last_frame, x, y);
+    return copy_from(s, s->second_last_frame, frame, x, y);
 }
 
-static int ipvideo_decode_block_opcode_0x7_16(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0x7_16(IpvideoContext *s, AVFrame *frame)
 {
     int x, y;
     uint16_t P[2];
@@ -570,7 +569,7 @@
     return 0;
 }
 
-static int ipvideo_decode_block_opcode_0x8_16(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0x8_16(IpvideoContext *s, AVFrame *frame)
 {
     int x, y;
     uint16_t P[4];
@@ -646,7 +645,7 @@
     return 0;
 }
 
-static int ipvideo_decode_block_opcode_0x9_16(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0x9_16(IpvideoContext *s, AVFrame *frame)
 {
     int x, y;
     uint16_t P[4];
@@ -713,7 +712,7 @@
     return 0;
 }
 
-static int ipvideo_decode_block_opcode_0xA_16(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0xA_16(IpvideoContext *s, AVFrame *frame)
 {
     int x, y;
     uint16_t P[8];
@@ -779,7 +778,7 @@
     return 0;
 }
 
-static int ipvideo_decode_block_opcode_0xB_16(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0xB_16(IpvideoContext *s, AVFrame *frame)
 {
     int x, y;
     uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
@@ -795,7 +794,7 @@
     return 0;
 }
 
-static int ipvideo_decode_block_opcode_0xC_16(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0xC_16(IpvideoContext *s, AVFrame *frame)
 {
     int x, y;
     uint16_t *pixel_ptr = (uint16_t*)s->pixel_ptr;
@@ -815,7 +814,7 @@
     return 0;
 }
 
-static int ipvideo_decode_block_opcode_0xD_16(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0xD_16(IpvideoContext *s, AVFrame *frame)
 {
     int x, y;
     uint16_t P[2];
@@ -836,7 +835,7 @@
     return 0;
 }
 
-static int ipvideo_decode_block_opcode_0xE_16(IpvideoContext *s)
+static int ipvideo_decode_block_opcode_0xE_16(IpvideoContext *s, AVFrame *frame)
 {
     int x, y;
     uint16_t pix;
@@ -855,7 +854,7 @@
     return 0;
 }
 
-static int (* const ipvideo_decode_block[])(IpvideoContext *s) = {
+static int (* const ipvideo_decode_block[])(IpvideoContext *s, AVFrame *frame) = {
     ipvideo_decode_block_opcode_0x0, ipvideo_decode_block_opcode_0x1,
     ipvideo_decode_block_opcode_0x2, ipvideo_decode_block_opcode_0x3,
     ipvideo_decode_block_opcode_0x4, ipvideo_decode_block_opcode_0x5,
@@ -866,7 +865,7 @@
     ipvideo_decode_block_opcode_0xE, ipvideo_decode_block_opcode_0xF,
 };
 
-static int (* const ipvideo_decode_block16[])(IpvideoContext *s) = {
+static int (* const ipvideo_decode_block16[])(IpvideoContext *s, AVFrame *frame) = {
     ipvideo_decode_block_opcode_0x0,    ipvideo_decode_block_opcode_0x1,
     ipvideo_decode_block_opcode_0x2,    ipvideo_decode_block_opcode_0x3,
     ipvideo_decode_block_opcode_0x4,    ipvideo_decode_block_opcode_0x5,
@@ -877,7 +876,7 @@
     ipvideo_decode_block_opcode_0xE_16, ipvideo_decode_block_opcode_0x1,
 };
 
-static void ipvideo_decode_opcodes(IpvideoContext *s)
+static void ipvideo_decode_opcodes(IpvideoContext *s, AVFrame *frame)
 {
     int x, y;
     unsigned char opcode;
@@ -887,16 +886,16 @@
     bytestream2_skip(&s->stream_ptr, 14); /* data starts 14 bytes in */
     if (!s->is_16bpp) {
         /* this is PAL8, so make the palette available */
-        memcpy(s->current_frame.data[1], s->pal, AVPALETTE_SIZE);
+        memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
 
-        s->stride = s->current_frame.linesize[0];
+        s->stride = frame->linesize[0];
     } else {
-        s->stride = s->current_frame.linesize[0] >> 1;
+        s->stride = frame->linesize[0] >> 1;
         s->mv_ptr = s->stream_ptr;
         bytestream2_skip(&s->mv_ptr, bytestream2_get_le16(&s->stream_ptr));
     }
     s->line_inc = s->stride - 8;
-    s->upper_motion_limit_offset = (s->avctx->height - 8) * s->current_frame.linesize[0]
+    s->upper_motion_limit_offset = (s->avctx->height - 8) * frame->linesize[0]
                                   + (s->avctx->width - 8) * (1 + s->is_16bpp);
 
     init_get_bits(&gb, s->decoding_map, s->decoding_map_size * 8);
@@ -909,13 +908,13 @@
                     x, y, opcode, bytestream2_tell(&s->stream_ptr));
 
             if (!s->is_16bpp) {
-                s->pixel_ptr = s->current_frame.data[0] + x
-                              + y*s->current_frame.linesize[0];
-                ret = ipvideo_decode_block[opcode](s);
+                s->pixel_ptr = frame->data[0] + x
+                              + y*frame->linesize[0];
+                ret = ipvideo_decode_block[opcode](s, frame);
             } else {
-                s->pixel_ptr = s->current_frame.data[0] + x*2
-                              + y*s->current_frame.linesize[0];
-                ret = ipvideo_decode_block16[opcode](s);
+                s->pixel_ptr = frame->data[0] + x*2
+                              + y*frame->linesize[0];
+                ret = ipvideo_decode_block16[opcode](s, frame);
             }
             if (ret != 0) {
                 av_log(s->avctx, AV_LOG_ERROR, "decode problem on frame %d, @ block (%d, %d)\n",
@@ -940,14 +939,15 @@
     s->is_16bpp = avctx->bits_per_coded_sample == 16;
     avctx->pix_fmt = s->is_16bpp ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_PAL8;
 
-    ff_dsputil_init(&s->dsp, avctx);
+    ff_hpeldsp_init(&s->hdsp, avctx->flags);
 
-    avcodec_get_frame_defaults(&s->second_last_frame);
-    avcodec_get_frame_defaults(&s->last_frame);
-    avcodec_get_frame_defaults(&s->current_frame);
-
-    s->current_frame.data[0] = s->last_frame.data[0] =
-    s->second_last_frame.data[0] = NULL;
+    s->last_frame        = av_frame_alloc();
+    s->second_last_frame = av_frame_alloc();
+    if (!s->last_frame || !s->second_last_frame) {
+        av_frame_free(&s->last_frame);
+        av_frame_free(&s->second_last_frame);
+        return AVERROR(ENOMEM);
+    }
 
     return 0;
 }
@@ -959,6 +959,7 @@
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     IpvideoContext *s = avctx->priv_data;
+    AVFrame *frame = data;
     int ret;
 
     /* decoding map contains 4 bits of information per 8x8 block */
@@ -969,42 +970,35 @@
     if (buf_size < s->decoding_map_size)
         return buf_size;
 
-    if (s->last_frame.data[0] && av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, NULL)) {
-        if (s->last_frame.data[0])
-            avctx->release_buffer(avctx, &s->last_frame);
-        if (s->second_last_frame.data[0])
-            avctx->release_buffer(avctx, &s->second_last_frame);
+    if (av_packet_get_side_data(avpkt, AV_PKT_DATA_PARAM_CHANGE, NULL)) {
+        av_frame_unref(s->last_frame);
+        av_frame_unref(s->second_last_frame);
     }
 
     s->decoding_map = buf;
     bytestream2_init(&s->stream_ptr, buf + s->decoding_map_size,
                      buf_size - s->decoding_map_size);
 
-    s->current_frame.reference = 3;
-    if ((ret = ff_get_buffer(avctx, &s->current_frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
         return ret;
-    }
 
     if (!s->is_16bpp) {
         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
         if (pal) {
-            s->current_frame.palette_has_changed = 1;
+            frame->palette_has_changed = 1;
             memcpy(s->pal, pal, AVPALETTE_SIZE);
         }
     }
 
-    ipvideo_decode_opcodes(s);
+    ipvideo_decode_opcodes(s, frame);
 
     *got_frame = 1;
-    *(AVFrame*)data = s->current_frame;
 
     /* shuffle frames */
-    if (s->second_last_frame.data[0])
-        avctx->release_buffer(avctx, &s->second_last_frame);
-    s->second_last_frame = s->last_frame;
-    s->last_frame = s->current_frame;
-    s->current_frame.data[0] = NULL;  /* catch any access attempts */
+    av_frame_unref(s->second_last_frame);
+    FFSWAP(AVFrame*, s->second_last_frame, s->last_frame);
+    if ((ret = av_frame_ref(s->last_frame, frame)) < 0)
+        return ret;
 
     /* report that the buffer was completely consumed */
     return buf_size;
@@ -1014,11 +1008,8 @@
 {
     IpvideoContext *s = avctx->priv_data;
 
-    /* release the last frame */
-    if (s->last_frame.data[0])
-        avctx->release_buffer(avctx, &s->last_frame);
-    if (s->second_last_frame.data[0])
-        avctx->release_buffer(avctx, &s->second_last_frame);
+    av_frame_free(&s->last_frame);
+    av_frame_free(&s->second_last_frame);
 
     return 0;
 }
diff --git a/libavcodec/intrax8.c b/libavcodec/intrax8.c
index 5b76942..67e16ab 100644
--- a/libavcodec/intrax8.c
+++ b/libavcodec/intrax8.c
@@ -23,6 +23,7 @@
 
 #include "libavutil/avassert.h"
 #include "avcodec.h"
+#include "error_resilience.h"
 #include "get_bits.h"
 #include "mpegvideo.h"
 #include "msmpeg4data.h"
@@ -770,7 +771,7 @@
                 /*emulate MB info in the relevant tables*/
                 s->mbskip_table [mb_xy]=0;
                 s->mbintra_table[mb_xy]=1;
-                s->current_picture.f.qscale_table[mb_xy] = w->quant;
+                s->current_picture.qscale_table[mb_xy] = w->quant;
                 mb_xy++;
             }
             s->dest[0]+= 8;
diff --git a/libavcodec/ituh263dec.c b/libavcodec/ituh263dec.c
index f8623d9..a28ed52 100644
--- a/libavcodec/ituh263dec.c
+++ b/libavcodec/ituh263dec.c
@@ -351,20 +351,20 @@
     do{
         if (get_bits1(&s->gb)) {
             /* skip mb */
-            mot_val = s->current_picture.f.motion_val[0][s->block_index[0]];
+            mot_val = s->current_picture.motion_val[0][s->block_index[0]];
             mot_val[0       ]= mot_val[2       ]=
             mot_val[0+stride]= mot_val[2+stride]= 0;
             mot_val[1       ]= mot_val[3       ]=
             mot_val[1+stride]= mot_val[3+stride]= 0;
 
-            s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
+            s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
             goto end;
         }
         cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
     }while(cbpc == 20);
 
     if(cbpc & 4){
-        s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
+        s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
     }else{
         get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
         if (cbpc & 8) {
@@ -376,7 +376,7 @@
         }
 
         if ((cbpc & 16) == 0) {
-                s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
+                s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
                 /* 16x16 motion prediction */
                 mot_val= ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
                 if (s->umvplus)
@@ -394,7 +394,7 @@
                 mot_val[1       ]= mot_val[3       ]=
                 mot_val[1+stride]= mot_val[3+stride]= my;
         } else {
-            s->current_picture.f.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
+            s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
             for(i=0;i<4;i++) {
                 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
                 if (s->umvplus)
@@ -619,7 +619,7 @@
                     s->block_last_index[i] = -1;
                 s->mv_dir = MV_DIR_FORWARD;
                 s->mv_type = MV_TYPE_16X16;
-                s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
+                s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
                 s->mv[0][0][0] = 0;
                 s->mv[0][0][1] = 0;
                 s->mb_skipped = !(s->obmc | s->loop_filter);
@@ -652,7 +652,7 @@
 
         s->mv_dir = MV_DIR_FORWARD;
         if ((cbpc & 16) == 0) {
-            s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
+            s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
             /* 16x16 motion prediction */
             s->mv_type = MV_TYPE_16X16;
             ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
@@ -677,7 +677,7 @@
             if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
                skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
         } else {
-            s->current_picture.f.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
+            s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
             s->mv_type = MV_TYPE_8X8;
             for(i=0;i<4;i++) {
                 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
@@ -705,8 +705,8 @@
     } else if(s->pict_type==AV_PICTURE_TYPE_B) {
         int mb_type;
         const int stride= s->b8_stride;
-        int16_t *mot_val0 = s->current_picture.f.motion_val[0][2 * (s->mb_x + s->mb_y * stride)];
-        int16_t *mot_val1 = s->current_picture.f.motion_val[1][2 * (s->mb_x + s->mb_y * stride)];
+        int16_t *mot_val0 = s->current_picture.motion_val[0][2 * (s->mb_x + s->mb_y * stride)];
+        int16_t *mot_val1 = s->current_picture.motion_val[1][2 * (s->mb_x + s->mb_y * stride)];
 //        const int mv_xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
 
         //FIXME ugly
@@ -789,7 +789,7 @@
             }
         }
 
-        s->current_picture.f.mb_type[xy] = mb_type;
+        s->current_picture.mb_type[xy] = mb_type;
     } else { /* I-Frame */
         do{
             cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
@@ -804,11 +804,11 @@
         dquant = cbpc & 4;
         s->mb_intra = 1;
 intra:
-        s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
+        s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
         if (s->h263_aic) {
             s->ac_pred = get_bits1(&s->gb);
             if(s->ac_pred){
-                s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
+                s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
 
                 s->h263_aic_dir = get_bits1(&s->gb);
             }
diff --git a/libavcodec/ituh263enc.c b/libavcodec/ituh263enc.c
index bdcc415..9a03f02 100644
--- a/libavcodec/ituh263enc.c
+++ b/libavcodec/ituh263enc.c
@@ -271,7 +271,7 @@
  */
 void ff_clean_h263_qscales(MpegEncContext *s){
     int i;
-    int8_t * const qscale_table = s->current_picture.f.qscale_table;
+    int8_t * const qscale_table = s->current_picture.qscale_table;
 
     ff_init_qscale_tab(s);
 
@@ -525,8 +525,8 @@
                 /* motion vectors: 8x8 mode*/
                 ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
 
-                motion_x = s->current_picture.f.motion_val[0][s->block_index[i]][0];
-                motion_y = s->current_picture.f.motion_val[0][s->block_index[i]][1];
+                motion_x = s->current_picture.motion_val[0][s->block_index[i]][0];
+                motion_y = s->current_picture.motion_val[0][s->block_index[i]][1];
                 if (!s->umvplus) {
                     ff_h263_encode_motion_vector(s, motion_x - pred_x,
                                                     motion_y - pred_y, 1);
diff --git a/libavcodec/ivi_common.c b/libavcodec/ivi_common.c
index 21954ec..265c645 100644
--- a/libavcodec/ivi_common.c
+++ b/libavcodec/ivi_common.c
@@ -538,8 +538,8 @@
                 /* for intra blocks apply the dc slant transform */
                 /* for inter - perform the motion compensation without delta */
                 if (is_intra) {
-                    band->dc_transform(&prev_dc, band->buf + buf_offs,
-                                       band->pitch, blk_size);
+                        band->dc_transform(&prev_dc, band->buf + buf_offs,
+                                           band->pitch, blk_size);
                 } else
                     mc_no_delta_func(band->buf + buf_offs,
                                      band->ref_buf + buf_offs + mv_y * band->pitch + mv_x,
@@ -846,6 +846,7 @@
 {
     IVI45DecContext *ctx = avctx->priv_data;
     const uint8_t   *buf = avpkt->data;
+    AVFrame       *frame = data;
     int             buf_size = avpkt->size;
     int             result, p, b;
 
@@ -903,30 +904,23 @@
     if (!ctx->is_nonnull_frame(ctx))
         return buf_size;
 
-    if (ctx->frame.data[0])
-        avctx->release_buffer(avctx, &ctx->frame);
-
-    ctx->frame.reference = 0;
     avcodec_set_dimensions(avctx, ctx->planes[0].width, ctx->planes[0].height);
-    if ((result = ff_get_buffer(avctx, &ctx->frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((result = ff_get_buffer(avctx, frame, 0)) < 0)
         return result;
-    }
 
     if (ctx->is_scalable) {
         if (avctx->codec_id == AV_CODEC_ID_INDEO4)
-            ff_ivi_recompose_haar(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0]);
+            ff_ivi_recompose_haar(&ctx->planes[0], frame->data[0], frame->linesize[0]);
         else
-            ff_ivi_recompose53   (&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0]);
+            ff_ivi_recompose53   (&ctx->planes[0], frame->data[0], frame->linesize[0]);
     } else {
-        ivi_output_plane(&ctx->planes[0], ctx->frame.data[0], ctx->frame.linesize[0]);
+        ivi_output_plane(&ctx->planes[0], frame->data[0], frame->linesize[0]);
     }
 
-    ivi_output_plane(&ctx->planes[2], ctx->frame.data[1], ctx->frame.linesize[1]);
-    ivi_output_plane(&ctx->planes[1], ctx->frame.data[2], ctx->frame.linesize[2]);
+    ivi_output_plane(&ctx->planes[2], frame->data[1], frame->linesize[1]);
+    ivi_output_plane(&ctx->planes[1], frame->data[2], frame->linesize[2]);
 
     *got_frame = 1;
-    *(AVFrame*)data = ctx->frame;
 
     return buf_size;
 }
@@ -943,9 +937,6 @@
     if (ctx->mb_vlc.cust_tab.table)
         ff_free_vlc(&ctx->mb_vlc.cust_tab);
 
-    if (ctx->frame.data[0])
-        avctx->release_buffer(avctx, &ctx->frame);
-
 #if IVI4_STREAM_ANALYSER
     if (avctx->codec_id == AV_CODEC_ID_INDEO4) {
     if (ctx->is_scalable)
diff --git a/libavcodec/ivi_common.h b/libavcodec/ivi_common.h
index 86acbe6..130fd12 100644
--- a/libavcodec/ivi_common.h
+++ b/libavcodec/ivi_common.h
@@ -197,7 +197,6 @@
 
 typedef struct IVI45DecContext {
     GetBitContext   gb;
-    AVFrame         frame;
     RVMapDesc       rvmap_tabs[9];   ///< local corrected copy of the static rvmap tables
 
     uint32_t        frame_num;
diff --git a/libavcodec/ivi_dsp.c b/libavcodec/ivi_dsp.c
index 84e2436..9c35aa0 100644
--- a/libavcodec/ivi_dsp.c
+++ b/libavcodec/ivi_dsp.c
@@ -320,6 +320,60 @@
 #undef  COMPENSATE
 }
 
+void ff_ivi_inverse_haar_1x8(const int32_t *in, int16_t *out, uint32_t pitch,
+                             const uint8_t *flags)
+{
+    int     i;
+    const int32_t *src;
+    int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
+
+    /* apply the InvHaar8 to all columns */
+#define COMPENSATE(x) (x)
+    src = in;
+    for (i = 0; i < 8; i++) {
+        if (flags[i]) {
+            INV_HAAR8(src[ 0], src[ 8], src[16], src[24],
+                      src[32], src[40], src[48], src[56],
+                      out[ 0], out[pitch], out[2*pitch], out[3*pitch],
+                      out[4*pitch], out[5*pitch], out[6*pitch], out[7*pitch],
+                      t0, t1, t2, t3, t4, t5, t6, t7, t8);
+        } else
+            out[      0]= out[  pitch]= out[2*pitch]= out[3*pitch]=
+            out[4*pitch]= out[5*pitch]= out[6*pitch]= out[7*pitch]= 0;
+
+        src++;
+        out++;
+    }
+#undef  COMPENSATE
+}
+
+void ff_ivi_inverse_haar_8x1(const int32_t *in, int16_t *out, uint32_t pitch,
+                             const uint8_t *flags)
+{
+    int     i;
+    const int32_t *src;
+    int     t0, t1, t2, t3, t4, t5, t6, t7, t8;
+
+    /* apply the InvHaar8 to all rows */
+#define COMPENSATE(x) (x)
+    src = in;
+    for (i = 0; i < 8; i++) {
+        if (   !src[0] && !src[1] && !src[2] && !src[3]
+            && !src[4] && !src[5] && !src[6] && !src[7]) {
+            memset(out, 0, 8 * sizeof(out[0]));
+        } else {
+            INV_HAAR8(src[0], src[1], src[2], src[3],
+                      src[4], src[5], src[6], src[7],
+                      out[0], out[1], out[2], out[3],
+                      out[4], out[5], out[6], out[7],
+                      t0, t1, t2, t3, t4, t5, t6, t7, t8);
+        }
+        src += 8;
+        out += pitch;
+    }
+#undef  COMPENSATE
+}
+
 void ff_ivi_dc_haar_2d(const int32_t *in, int16_t *out, uint32_t pitch,
                        int blk_size)
 {
diff --git a/libavcodec/ivi_dsp.h b/libavcodec/ivi_dsp.h
index e95bb11..748b3f4 100644
--- a/libavcodec/ivi_dsp.h
+++ b/libavcodec/ivi_dsp.h
@@ -64,6 +64,10 @@
  */
 void ff_ivi_inverse_haar_8x8(const int32_t *in, int16_t *out, uint32_t pitch,
                              const uint8_t *flags);
+void ff_ivi_inverse_haar_8x1(const int32_t *in, int16_t *out, uint32_t pitch,
+                             const uint8_t *flags);
+void ff_ivi_inverse_haar_1x8(const int32_t *in, int16_t *out, uint32_t pitch,
+                             const uint8_t *flags);
 
 /**
  *  DC-only two-dimensional inverse Haar transform for Indeo 4.
diff --git a/libavcodec/j2kdec.c b/libavcodec/j2kdec.c
index 53c4f07..683062d 100644
--- a/libavcodec/j2kdec.c
+++ b/libavcodec/j2kdec.c
@@ -49,7 +49,7 @@
 
 typedef struct {
     AVCodecContext *avctx;
-    AVFrame picture;
+    AVFrame *picture;
     GetByteContext g;
 
     int width, height; ///< image width and height
@@ -281,14 +281,12 @@
         break;
     }
 
-    if (s->picture.data[0])
-        s->avctx->release_buffer(s->avctx, &s->picture);
 
-    if ((ret = ff_get_buffer(s->avctx, &s->picture)) < 0)
+    if ((ret = ff_get_buffer(s->avctx, s->picture, 0)) < 0)
         return ret;
 
-    s->picture.pict_type = AV_PICTURE_TYPE_I;
-    s->picture.key_frame = 1;
+    s->picture->pict_type = AV_PICTURE_TYPE_I;
+    s->picture->key_frame = 1;
 
     return 0;
 }
@@ -859,7 +857,7 @@
     if (s->precision <= 8) {
         for (compno = 0; compno < s->ncomponents; compno++){
             y = tile->comp[compno].coord[1][0] - s->image_offset_y;
-            line = s->picture.data[0] + y * s->picture.linesize[0];
+            line = s->picture->data[0] + y * s->picture->linesize[0];
             for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]){
                 uint8_t *dst;
 
@@ -875,13 +873,13 @@
                     *dst = *src[compno]++;
                     dst += s->ncomponents;
                 }
-                line += s->picture.linesize[0];
+                line += s->picture->linesize[0];
             }
         }
     } else {
         for (compno = 0; compno < s->ncomponents; compno++) {
             y = tile->comp[compno].coord[1][0] - s->image_offset_y;
-            line = s->picture.data[0] + y * s->picture.linesize[0];
+            line = s->picture->data[0] + y * s->picture->linesize[0];
             for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
                 uint16_t *dst;
 
@@ -896,7 +894,7 @@
                     *dst = val;
                     dst += s->ncomponents;
                 }
-                line += s->picture.linesize[0];
+                line += s->picture->linesize[0];
             }
         }
     }
@@ -1025,6 +1023,8 @@
     AVFrame *picture = data;
     int tileno, ret;
 
+    s->picture = picture;
+
     bytestream2_init(&s->g, avpkt->data, avpkt->size);
     s->curtileno = -1;
 
@@ -1062,7 +1062,6 @@
     cleanup(s);
 
     *got_frame = 1;
-    *picture = s->picture;
 
     return bytestream2_tell(&s->g);
 
@@ -1076,31 +1075,18 @@
     J2kDecoderContext *s = avctx->priv_data;
 
     s->avctx = avctx;
-    avcodec_get_frame_defaults((AVFrame*)&s->picture);
-    avctx->coded_frame = (AVFrame*)&s->picture;
 
     ff_j2k_init_tier1_luts();
 
     return 0;
 }
 
-static av_cold int decode_end(AVCodecContext *avctx)
-{
-    J2kDecoderContext *s = avctx->priv_data;
-
-    if (s->picture.data[0])
-        avctx->release_buffer(avctx, &s->picture);
-
-    return 0;
-}
-
-AVCodec ff_jpeg2000_decoder = {
+AVCodec ff_j2k_decoder = {
     .name           = "j2k",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_JPEG2000,
     .priv_data_size = sizeof(J2kDecoderContext),
     .init           = j2kdec_init,
-    .close          = decode_end,
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_EXPERIMENTAL,
     .long_name      = NULL_IF_CONFIG_SMALL("JPEG 2000"),
diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 5d447ed..ffbd503 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -1026,7 +1026,7 @@
     return 0;
 }
 
-AVCodec ff_jpeg2000_encoder = {
+AVCodec ff_j2k_encoder = {
     .name           = "j2k",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_JPEG2000,
diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
new file mode 100644
index 0000000..060b1fd
--- /dev/null
+++ b/libavcodec/jpeg2000.c
@@ -0,0 +1,478 @@
+/*
+ * JPEG 2000 encoder and decoder common functions
+ * Copyright (c) 2007 Kamil Nowosad
+ * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.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
+ */
+
+/**
+ * @file
+ * JPEG 2000 image encoder and decoder common functions
+ */
+
+#include "libavutil/common.h"
+#include "libavutil/mem.h"
+#include "avcodec.h"
+#include "jpeg2000.h"
+
+#define SHL(a, n) ((n) >= 0 ? (a) << (n) : (a) >> -(n))
+
+/* tag tree routines */
+
+/* allocate the memory for tag tree */
+static int32_t tag_tree_size(uint16_t w, uint16_t h)
+{
+    uint32_t res = 0;
+    while (w > 1 || h > 1) {
+        res += w * h;
+        if (res + 1 >= INT32_MAX)
+            return -1;
+        w = (w + 1) >> 1;
+        h = (h + 1) >> 1;
+    }
+    return (int32_t)(res + 1);
+}
+
+static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int h)
+{
+    int pw = w, ph = h;
+    Jpeg2000TgtNode *res, *t, *t2;
+    int32_t tt_size;
+
+    tt_size = tag_tree_size(w, h);
+    if (tt_size == -1)
+        return NULL;
+
+    t = res = av_mallocz_array(tt_size, sizeof(*t));
+    if (!res)
+        return NULL;
+
+    while (w > 1 || h > 1) {
+        int i, j;
+        pw = w;
+        ph = h;
+
+        w  = (w + 1) >> 1;
+        h  = (h + 1) >> 1;
+        t2 = t + pw * ph;
+
+        for (i = 0; i < ph; i++)
+            for (j = 0; j < pw; j++)
+                t[i * pw + j].parent = &t2[(i >> 1) * w + (j >> 1)];
+
+        t = t2;
+    }
+    t[0].parent = NULL;
+    return res;
+}
+
+uint8_t ff_jpeg2000_sigctxno_lut[256][4];
+
+static int getsigctxno(int flag, int bandno)
+{
+    int h, v, d;
+
+    h = ((flag & JPEG2000_T1_SIG_E)  ? 1 : 0) +
+        ((flag & JPEG2000_T1_SIG_W)  ? 1 : 0);
+    v = ((flag & JPEG2000_T1_SIG_N)  ? 1 : 0) +
+        ((flag & JPEG2000_T1_SIG_S)  ? 1 : 0);
+    d = ((flag & JPEG2000_T1_SIG_NE) ? 1 : 0) +
+        ((flag & JPEG2000_T1_SIG_NW) ? 1 : 0) +
+        ((flag & JPEG2000_T1_SIG_SE) ? 1 : 0) +
+        ((flag & JPEG2000_T1_SIG_SW) ? 1 : 0);
+    if (bandno < 3) {
+        if (bandno == 1)
+            FFSWAP(int, h, v);
+        if (h == 2)
+            return 8;
+        if (h == 1) {
+            if (v >= 1)
+                return 7;
+            if (d >= 1)
+                return 6;
+            return 5;
+        }
+        if (v == 2)
+            return 4;
+        if (v == 1)
+            return 3;
+        if (d >= 2)
+            return 2;
+        if (d == 1)
+            return 1;
+        return 0;
+    } else {
+        if (d >= 3)
+            return 8;
+        if (d == 2) {
+            if (h + v >= 1)
+                return 7;
+            return 6;
+        }
+        if (d == 1) {
+            if (h + v >= 2)
+                return 5;
+            if (h + v == 1)
+                return 4;
+            return 3;
+        }
+        if (h + v >= 2)
+            return 2;
+        if (h + v == 1)
+            return 1;
+        return 0;
+    }
+    return 0;
+}
+
+uint8_t ff_jpeg2000_sgnctxno_lut[16][16], ff_jpeg2000_xorbit_lut[16][16];
+
+static const int contribtab[3][3] = { {  0, -1,  1 }, { -1, -1,  0 }, {  1,  0,  1 } };
+static const int  ctxlbltab[3][3] = { { 13, 12, 11 }, { 10,  9, 10 }, { 11, 12, 13 } };
+static const int  xorbittab[3][3] = { {  1,  1,  1 }, {  1,  0,  0 }, {  0,  0,  0 } };
+
+static int getsgnctxno(int flag, uint8_t *xorbit)
+{
+    int vcontrib, hcontrib;
+
+    hcontrib = contribtab[flag & JPEG2000_T1_SIG_E ? flag & JPEG2000_T1_SGN_E ? 1 : 2 : 0]
+                         [flag & JPEG2000_T1_SIG_W ? flag & JPEG2000_T1_SGN_W ? 1 : 2 : 0] + 1;
+    vcontrib = contribtab[flag & JPEG2000_T1_SIG_S ? flag & JPEG2000_T1_SGN_S ? 1 : 2 : 0]
+                         [flag & JPEG2000_T1_SIG_N ? flag & JPEG2000_T1_SGN_N ? 1 : 2 : 0] + 1;
+    *xorbit = xorbittab[hcontrib][vcontrib];
+
+    return ctxlbltab[hcontrib][vcontrib];
+}
+
+void ff_jpeg2000_init_tier1_luts(void)
+{
+    int i, j;
+    for (i = 0; i < 256; i++)
+        for (j = 0; j < 4; j++)
+            ff_jpeg2000_sigctxno_lut[i][j] = getsigctxno(i, j);
+    for (i = 0; i < 16; i++)
+        for (j = 0; j < 16; j++)
+            ff_jpeg2000_sgnctxno_lut[i][j] =
+                getsgnctxno(i + (j << 8), &ff_jpeg2000_xorbit_lut[i][j]);
+}
+
+void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1, int x, int y,
+                                  int negative)
+{
+    x++;
+    y++;
+    t1->flags[y][x] |= JPEG2000_T1_SIG;
+    if (negative) {
+        t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W | JPEG2000_T1_SGN_W;
+        t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E | JPEG2000_T1_SGN_E;
+        t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N | JPEG2000_T1_SGN_N;
+        t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S | JPEG2000_T1_SGN_S;
+    } else {
+        t1->flags[y][x + 1] |= JPEG2000_T1_SIG_W;
+        t1->flags[y][x - 1] |= JPEG2000_T1_SIG_E;
+        t1->flags[y + 1][x] |= JPEG2000_T1_SIG_N;
+        t1->flags[y - 1][x] |= JPEG2000_T1_SIG_S;
+    }
+    t1->flags[y + 1][x + 1] |= JPEG2000_T1_SIG_NW;
+    t1->flags[y + 1][x - 1] |= JPEG2000_T1_SIG_NE;
+    t1->flags[y - 1][x + 1] |= JPEG2000_T1_SIG_SW;
+    t1->flags[y - 1][x - 1] |= JPEG2000_T1_SIG_SE;
+}
+
+static const uint8_t lut_gain[2][4] = { { 0, 0, 0, 0 }, { 0, 1, 1, 2 } };
+
+int ff_jpeg2000_init_component(Jpeg2000Component *comp,
+                               Jpeg2000CodingStyle *codsty,
+                               Jpeg2000QuantStyle *qntsty,
+                               int cbps, int dx, int dy,
+                               AVCodecContext *avctx)
+{
+    uint8_t log2_band_prec_width, log2_band_prec_height;
+    int reslevelno, bandno, gbandno = 0, ret, i, j;
+    uint32_t csize = 1;
+
+    if (ret = ff_jpeg2000_dwt_init(&comp->dwt, comp->coord,
+                                   codsty->nreslevels2decode - 1,
+                                   codsty->transform))
+        return ret;
+    // component size comp->coord is uint16_t so ir cannot overflow
+    csize = (comp->coord[0][1] - comp->coord[0][0]) *
+            (comp->coord[1][1] - comp->coord[1][0]);
+
+    comp->data = av_malloc_array(csize, sizeof(*comp->data));
+    if (!comp->data)
+        return AVERROR(ENOMEM);
+    comp->reslevel = av_malloc_array(codsty->nreslevels, sizeof(*comp->reslevel));
+    if (!comp->reslevel)
+        return AVERROR(ENOMEM);
+    /* LOOP on resolution levels */
+    for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
+        int declvl = codsty->nreslevels - reslevelno;    // N_L -r see  ISO/IEC 15444-1:2002 B.5
+        Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
+
+        /* Compute borders for each resolution level.
+         * Computation of trx_0, trx_1, try_0 and try_1.
+         * see ISO/IEC 15444-1:2002 eq. B.5 and B-14 */
+        for (i = 0; i < 2; i++)
+            for (j = 0; j < 2; j++)
+                reslevel->coord[i][j] =
+                    ff_jpeg2000_ceildivpow2(comp->coord_o[i][j], declvl - 1);
+        // update precincts size: 2^n value
+        reslevel->log2_prec_width  = codsty->log2_prec_widths[reslevelno];
+        reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
+
+        /* Number of bands for each resolution level */
+        if (reslevelno == 0)
+            reslevel->nbands = 1;
+        else
+            reslevel->nbands = 3;
+
+        /* Number of precincts wich span the tile for resolution level reslevelno
+         * see B.6 in ISO/IEC 15444-1:2002 eq. B-16
+         * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -| - (trx_0 / 2 ^ log2_prec_width)
+         * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| - (try_0 / 2 ^ log2_prec_width)
+         * for Dcinema profiles in JPEG 2000
+         * num_precincts_x = |- trx_1 / 2 ^ log2_prec_width) -|
+         * num_precincts_y = |- try_1 / 2 ^ log2_prec_width) -| */
+        if (reslevel->coord[0][1] == reslevel->coord[0][0])
+            reslevel->num_precincts_x = 0;
+        else
+            reslevel->num_precincts_x =
+                ff_jpeg2000_ceildivpow2(reslevel->coord[0][1],
+                                        reslevel->log2_prec_width) -
+                (reslevel->coord[0][0] >> reslevel->log2_prec_width);
+
+        if (reslevel->coord[1][1] == reslevel->coord[1][0])
+            reslevel->num_precincts_y = 0;
+        else
+            reslevel->num_precincts_y =
+                ff_jpeg2000_ceildivpow2(reslevel->coord[1][1],
+                                        reslevel->log2_prec_height) -
+                (reslevel->coord[1][0] >> reslevel->log2_prec_height);
+
+        reslevel->band = av_malloc_array(reslevel->nbands, sizeof(*reslevel->band));
+        if (!reslevel->band)
+            return AVERROR(ENOMEM);
+
+        for (bandno = 0; bandno < reslevel->nbands; bandno++, gbandno++) {
+            Jpeg2000Band *band = reslevel->band + bandno;
+            int cblkno, precno;
+            int nb_precincts;
+
+            /* TODO: Implementation of quantization step not finished,
+             * see ISO/IEC 15444-1:2002 E.1 and A.6.4. */
+            switch (qntsty->quantsty) {
+                uint8_t gain;
+                int numbps;
+            case JPEG2000_QSTY_NONE:
+                /* TODO: to verify. No quantization in this case */
+                numbps = cbps +
+                         lut_gain[codsty->transform][bandno + reslevelno > 0];
+                band->stepsize = (float)SHL(2048 + qntsty->mant[gbandno],
+                                            2 + numbps - qntsty->expn[gbandno]);
+                break;
+            case JPEG2000_QSTY_SI:
+                /*TODO: Compute formula to implement. */
+                band->stepsize = (float) (1 << 13);
+                break;
+            case JPEG2000_QSTY_SE:
+                /* Exponent quantization step.
+                 * Formula:
+                 * delta_b = 2 ^ (R_b - expn_b) * (1 + (mant_b / 2 ^ 11))
+                 * R_b = R_I + log2 (gain_b )
+                 * see ISO/IEC 15444-1:2002 E.1.1 eqn. E-3 and E-4 */
+                /* TODO/WARN: value of log2 (gain_b ) not taken into account
+                 * but it works (compared to OpenJPEG). Why?
+                 * Further investigation needed. */
+                gain            = cbps;
+                band->stepsize  = pow(2.0, gain - qntsty->expn[gbandno]);
+                band->stepsize *= (float)qntsty->mant[gbandno] / 2048.0 + 1.0;
+                /* FIXME: In openjepg code stespize = stepsize * 0.5. Why?
+                 * If not set output of entropic decoder is not correct. */
+                band->stepsize *= 0.5;
+                break;
+            default:
+                band->stepsize = 0;
+                av_log(avctx, AV_LOG_ERROR, "Unknown quantization format\n");
+                break;
+            }
+            /* BITEXACT computing case --> convert to int */
+            if (avctx->flags & CODEC_FLAG_BITEXACT)
+                band->stepsize = (int32_t)(band->stepsize * (1 << 16));
+
+            /* computation of tbx_0, tbx_1, tby_0, tby_1
+             * see ISO/IEC 15444-1:2002 B.5 eq. B-15 and tbl B.1
+             * codeblock width and height is computed for
+             * DCI JPEG 2000 codeblock_width = codeblock_width = 32 = 2 ^ 5 */
+            if (reslevelno == 0) {
+                /* for reslevelno = 0, only one band, x0_b = y0_b = 0 */
+                for (i = 0; i < 2; i++)
+                    for (j = 0; j < 2; j++)
+                        band->coord[i][j] =
+                            ff_jpeg2000_ceildivpow2(comp->coord_o[i][j],
+                                                    declvl - 1);
+
+                log2_band_prec_width  = reslevel->log2_prec_width;
+                log2_band_prec_height = reslevel->log2_prec_height;
+                /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
+                band->log2_cblk_width  = FFMIN(codsty->log2_cblk_width,
+                                               reslevel->log2_prec_width);
+                band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
+                                               reslevel->log2_prec_height);
+            } else {
+                /* 3 bands x0_b = 1 y0_b = 0; x0_b = 0 y0_b = 1; x0_b = y0_b = 1 */
+                /* x0_b and y0_b are computed with ((bandno + 1 >> i) & 1) */
+                for (i = 0; i < 2; i++)
+                    for (j = 0; j < 2; j++)
+                        /* Formula example for tbx_0 = ceildiv((tcx_0 - 2 ^ (declvl - 1) * x0_b) / declvl) */
+                        band->coord[i][j] =
+                            ff_jpeg2000_ceildivpow2(comp->coord_o[i][j] -
+                                                    (((bandno + 1 >> i) & 1) << declvl - 1),
+                                                    declvl);
+                /* TODO: Manage case of 3 band offsets here or
+                 * in coding/decoding function? */
+
+                /* see ISO/IEC 15444-1:2002 eq. B-17 and eq. B-15 */
+                band->log2_cblk_width  = FFMIN(codsty->log2_cblk_width,
+                                               reslevel->log2_prec_width - 1);
+                band->log2_cblk_height = FFMIN(codsty->log2_cblk_height,
+                                               reslevel->log2_prec_height - 1);
+
+                log2_band_prec_width  = reslevel->log2_prec_width  - 1;
+                log2_band_prec_height = reslevel->log2_prec_height - 1;
+            }
+
+            band->prec = av_malloc_array(reslevel->num_precincts_x *
+                                         reslevel->num_precincts_y,
+                                         sizeof(*band->prec));
+            if (!band->prec)
+                return AVERROR(ENOMEM);
+
+            nb_precincts = reslevel->num_precincts_x * reslevel->num_precincts_y;
+
+            for (precno = 0; precno < nb_precincts; precno++) {
+                Jpeg2000Prec *prec = band->prec + precno;
+
+                /* TODO: Explain formula for JPEG200 DCINEMA. */
+                /* TODO: Verify with previous count of codeblocks per band */
+
+                /* Compute P_x0 */
+                prec->coord[0][0] = (precno % reslevel->num_precincts_x) *
+                                    (1 << log2_band_prec_width);
+                prec->coord[0][0] = FFMAX(prec->coord[0][0], band->coord[0][0]);
+
+                /* Compute P_y0 */
+                prec->coord[1][0] = (precno / reslevel->num_precincts_x) *
+                                    (1 << log2_band_prec_height);
+                prec->coord[1][0] = FFMAX(prec->coord[1][0], band->coord[1][0]);
+
+                /* Compute P_x1 */
+                prec->coord[0][1] = prec->coord[0][0] +
+                                    (1 << log2_band_prec_width);
+                prec->coord[0][1] = FFMIN(prec->coord[0][1], band->coord[0][1]);
+
+                /* Compute P_y1 */
+                prec->coord[1][1] = prec->coord[1][0] +
+                                    (1 << log2_band_prec_height);
+                prec->coord[1][1] = FFMIN(prec->coord[1][1], band->coord[1][1]);
+
+                prec->nb_codeblocks_width =
+                    ff_jpeg2000_ceildivpow2(prec->coord[0][1] -
+                                            prec->coord[0][0],
+                                            band->log2_cblk_width);
+                prec->nb_codeblocks_height =
+                    ff_jpeg2000_ceildivpow2(prec->coord[1][1] -
+                                            prec->coord[1][0],
+                                            band->log2_cblk_height);
+
+                /* Tag trees initialization */
+                prec->cblkincl =
+                    ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
+                                              prec->nb_codeblocks_height);
+                if (!prec->cblkincl)
+                    return AVERROR(ENOMEM);
+
+                prec->zerobits =
+                    ff_jpeg2000_tag_tree_init(prec->nb_codeblocks_width,
+                                              prec->nb_codeblocks_height);
+                if (!prec->zerobits)
+                    return AVERROR(ENOMEM);
+
+                prec->cblk = av_malloc_array(prec->nb_codeblocks_width *
+                                             prec->nb_codeblocks_height,
+                                             sizeof(*prec->cblk));
+                if (!prec->cblk)
+                    return AVERROR(ENOMEM);
+                for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
+                    Jpeg2000Cblk *cblk = prec->cblk + cblkno;
+                    uint16_t Cx0, Cy0;
+
+                    /* Compute coordinates of codeblocks */
+                    /* Compute Cx0*/
+                    Cx0 = (prec->coord[0][0] >> band->log2_cblk_width) << band->log2_cblk_width;
+                    Cx0 = Cx0 + ((cblkno % prec->nb_codeblocks_width)  << band->log2_cblk_width);
+                    cblk->coord[0][0] = FFMAX(Cx0, prec->coord[0][0]);
+
+                    /* Compute Cy0*/
+                    Cy0 = (prec->coord[1][0] >> band->log2_cblk_height) << band->log2_cblk_height;
+                    Cy0 = Cy0 + ((cblkno / prec->nb_codeblocks_width)   << band->log2_cblk_height);
+                    cblk->coord[1][0] = FFMAX(Cy0, prec->coord[1][0]);
+
+                    /* Compute Cx1 */
+                    cblk->coord[0][1] = FFMIN(Cx0 + (1 << band->log2_cblk_width),
+                                              prec->coord[0][1]);
+
+                    /* Compute Cy1 */
+                    cblk->coord[1][1] = FFMIN(Cy0 + (1 << band->log2_cblk_height),
+                                              prec->coord[1][1]);
+                    cblk->zero      = 0;
+                    cblk->lblock    = 3;
+                    cblk->length    = 0;
+                    cblk->lengthinc = 0;
+                    cblk->npasses   = 0;
+                }
+            }
+        }
+    }
+    return 0;
+}
+
+void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
+{
+    int reslevelno, bandno, precno;
+    for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
+        Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
+
+        for (bandno = 0; bandno < reslevel->nbands; bandno++) {
+            Jpeg2000Band *band = reslevel->band + bandno;
+            for (precno = 0; precno < reslevel->num_precincts_x * reslevel->num_precincts_y; precno++) {
+                Jpeg2000Prec *prec = band->prec + precno;
+                av_freep(&prec->zerobits);
+                av_freep(&prec->cblkincl);
+                av_freep(&prec->cblk);
+            }
+
+            av_freep(&band->prec);
+        }
+        av_freep(&reslevel->band);
+    }
+
+    ff_dwt_destroy(&comp->dwt);
+    av_freep(&comp->reslevel);
+    av_freep(&comp->data);
+}
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
new file mode 100644
index 0000000..c2d6d90
--- /dev/null
+++ b/libavcodec/jpeg2000.h
@@ -0,0 +1,268 @@
+/*
+ * JPEG 2000 common defines, structures and functions
+ * Copyright (c) 2007 Kamil Nowosad
+ * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.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
+ */
+
+#ifndef AVCODEC_JPEG2000_H
+#define AVCODEC_JPEG2000_H
+
+/**
+ * @file
+ * JPEG 2000 structures and defines common
+ * to encoder and decoder
+ */
+
+#include <stdint.h>
+
+#include "avcodec.h"
+#include "mqc.h"
+#include "jpeg2000dwt.h"
+
+enum Jpeg2000Markers {
+    JPEG2000_SOC = 0xff4f, // start of codestream
+    JPEG2000_SIZ = 0xff51, // image and tile size
+    JPEG2000_COD,          // coding style default
+    JPEG2000_COC,          // coding style component
+    JPEG2000_TLM = 0xff55, // packed packet headers, tile-part header
+    JPEG2000_PLM = 0xff57, // tile-part lengths
+    JPEG2000_PLT,          // packet length, main header
+    JPEG2000_QCD = 0xff5c, // quantization default
+    JPEG2000_QCC,          // quantization component
+    JPEG2000_RGN,          // region of interest
+    JPEG2000_POC,          // progression order change
+    JPEG2000_PPM,          // packet length, tile-part header
+    JPEG2000_PPT,          // packed packet headers, main header
+    JPEG2000_CRG = 0xff63, // component registration
+    JPEG2000_COM,          // comment
+    JPEG2000_SOT = 0xff90, // start of tile-part
+    JPEG2000_SOP,          // start of packet
+    JPEG2000_EPH,          // end of packet header
+    JPEG2000_SOD,          // start of data
+    JPEG2000_EOC = 0xffd9, // end of codestream
+};
+
+enum Jpeg2000Quantsty { // quantization style
+    JPEG2000_QSTY_NONE, // no quantization
+    JPEG2000_QSTY_SI,   // scalar derived
+    JPEG2000_QSTY_SE    // scalar expounded
+};
+
+#define JPEG2000_MAX_CBLKW 64
+#define JPEG2000_MAX_CBLKH 64
+
+#define JPEG2000_MAX_RESLEVELS 33
+
+// T1 flags
+// flags determining significance of neighbor coefficients
+#define JPEG2000_T1_SIG_N  0x0001
+#define JPEG2000_T1_SIG_E  0x0002
+#define JPEG2000_T1_SIG_W  0x0004
+#define JPEG2000_T1_SIG_S  0x0008
+#define JPEG2000_T1_SIG_NE 0x0010
+#define JPEG2000_T1_SIG_NW 0x0020
+#define JPEG2000_T1_SIG_SE 0x0040
+#define JPEG2000_T1_SIG_SW 0x0080
+#define JPEG2000_T1_SIG_NB (JPEG2000_T1_SIG_N  | JPEG2000_T1_SIG_E  |   \
+                            JPEG2000_T1_SIG_S  | JPEG2000_T1_SIG_W  |   \
+                            JPEG2000_T1_SIG_NE | JPEG2000_T1_SIG_NW |   \
+                            JPEG2000_T1_SIG_SE | JPEG2000_T1_SIG_SW)
+// flags determining sign bit of neighbor coefficients
+#define JPEG2000_T1_SGN_N  0x0100
+#define JPEG2000_T1_SGN_S  0x0200
+#define JPEG2000_T1_SGN_W  0x0400
+#define JPEG2000_T1_SGN_E  0x0800
+
+#define JPEG2000_T1_VIS    0x1000
+#define JPEG2000_T1_SIG    0x2000
+#define JPEG2000_T1_REF    0x4000
+
+#define JPEG2000_T1_SGN    0x8000
+
+// Codeblock coding styles
+#define JPEG2000_CBLK_BYPASS    0x01 // Selective arithmetic coding bypass
+#define JPEG2000_CBLK_RESET     0x02 // Reset context probabilities
+#define JPEG2000_CBLK_TERMALL   0x04 // Terminate after each coding pass
+#define JPEG2000_CBLK_VSC       0x08 // Vertical stripe causal context formation
+#define JPEG2000_CBLK_PREDTERM  0x10 // Predictable termination
+#define JPEG2000_CBLK_SEGSYM    0x20 // Segmentation symbols present
+
+// Coding styles
+#define JPEG2000_CSTY_PREC      0x01 // Precincts defined in coding style
+#define JPEG2000_CSTY_SOP       0x02 // SOP marker present
+#define JPEG2000_CSTY_EPH       0x04 // EPH marker present
+
+// Progression orders
+#define JPEG2000_PGOD_LRCP      0x00  // Layer-resolution level-component-position progression
+#define JPEG2000_PGOD_RLCP      0x01  // Resolution level-layer-component-position progression
+#define JPEG2000_PGOD_RPCL      0x02  // Resolution level-position-component-layer progression
+#define JPEG2000_PGOD_PCRL      0x03  // Position-component-resolution level-layer progression
+#define JPEG2000_PGOD_CPRL      0x04  // Component-position-resolution level-layer progression
+
+typedef struct Jpeg2000T1Context {
+    int data[JPEG2000_MAX_CBLKW][JPEG2000_MAX_CBLKH];
+    int flags[JPEG2000_MAX_CBLKW + 2][JPEG2000_MAX_CBLKH + 2];
+    MqcState mqc;
+} Jpeg2000T1Context;
+
+typedef struct Jpeg2000TgtNode {
+    uint8_t val;
+    uint8_t vis;
+    struct Jpeg2000TgtNode *parent;
+} Jpeg2000TgtNode;
+
+typedef struct Jpeg2000CodingStyle {
+    uint8_t nreslevels;       // number of resolution levels
+    uint8_t nreslevels2decode; // number of resolution levels to decode
+    uint8_t log2_cblk_width,
+            log2_cblk_height; // exponent of codeblock size
+    uint8_t transform;        // DWT type
+    uint8_t csty;             // coding style
+    uint8_t log2_prec_width,
+            log2_prec_height; // precinct size
+    uint8_t nlayers;          // number of layers
+    uint8_t mct;              // multiple component transformation
+    uint8_t cblk_style;       // codeblock coding style
+    uint8_t prog_order;       // progression order
+    uint8_t log2_prec_widths[JPEG2000_MAX_RESLEVELS];  // precincts size according resolution levels
+    uint8_t log2_prec_heights[JPEG2000_MAX_RESLEVELS]; // TODO: initialize prec_size array with 0?
+} Jpeg2000CodingStyle;
+
+typedef struct Jpeg2000QuantStyle {
+    uint8_t expn[32 * 3];  // quantization exponent
+    uint32_t mant[32 * 3]; // quantization mantissa
+    uint8_t quantsty;      // quantization style
+    uint8_t nguardbits;    // number of guard bits
+} Jpeg2000QuantStyle;
+
+typedef struct Jpeg2000Pass {
+    uint16_t rate;
+    int64_t disto;
+} Jpeg2000Pass;
+
+typedef struct Jpeg2000Cblk {
+    uint8_t npasses;
+    uint8_t ninclpasses; // number coding of passes included in codestream
+    uint8_t nonzerobits;
+    uint16_t length;
+    uint16_t lengthinc;
+    uint8_t lblock;
+    uint8_t zero;
+    uint8_t data[8192];
+    Jpeg2000Pass passes[100];
+    uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
+} Jpeg2000Cblk; // code block
+
+typedef struct Jpeg2000Prec {
+    uint16_t xi0, yi0; // codeblock indexes ([xi0, xi1))
+    uint16_t nb_codeblocks_width;
+    uint16_t nb_codeblocks_height;
+    Jpeg2000TgtNode *zerobits;
+    Jpeg2000TgtNode *cblkincl;
+    Jpeg2000Cblk *cblk;
+    uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
+} Jpeg2000Prec; // precinct
+
+/* TODO: stepsize can be float or integer depending on
+ * reversible or irreversible transformation. */
+typedef struct Jpeg2000Band {
+    uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
+    uint16_t log2_cblk_width, log2_cblk_height;
+    uint16_t cblknx, cblkny;
+    float stepsize; // quantization stepsize
+    Jpeg2000Prec *prec;
+} Jpeg2000Band; // subband
+
+typedef struct Jpeg2000ResLevel {
+    uint8_t nbands;
+    uint16_t coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
+    uint16_t num_precincts_x, num_precincts_y; // number of precincts in x/y direction
+    uint8_t log2_prec_width, log2_prec_height; // exponent of precinct size
+    Jpeg2000Band *band;
+} Jpeg2000ResLevel; // resolution level
+
+/* TODO: data can be float of integer depending of reversible/irreversible
+ * transformation.
+ */
+typedef struct Jpeg2000Component {
+    Jpeg2000ResLevel *reslevel;
+    DWTContext dwt;
+    float *data;
+    uint16_t coord[2][2];   // border coordinates {{x0, x1}, {y0, y1}} -- can be reduced with lowres option
+    uint16_t coord_o[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- original values from jpeg2000 headers
+} Jpeg2000Component;
+
+/* misc tools */
+static inline int ff_jpeg2000_ceildivpow2(int a, int b)
+{
+    return (a + (1 << b) - 1) >> b;
+}
+
+static inline int ff_jpeg2000_ceildiv(int a, int b)
+{
+    return (a + b - 1) / b;
+}
+
+/* TIER-1 routines */
+
+/* Set up lookup tables used in TIER-1. */
+void ff_jpeg2000_init_tier1_luts(void);
+
+/* Update significance of a coefficient at current position (x,y) and
+ * for neighbors. */
+void ff_jpeg2000_set_significance(Jpeg2000T1Context *t1,
+                                  int x, int y, int negative);
+
+extern uint8_t ff_jpeg2000_sigctxno_lut[256][4];
+
+/* Get context label (number in range[0..8]) of a coefficient for significance
+ * propagation and cleanup coding passes. */
+static inline int ff_jpeg2000_getsigctxno(int flag, int bandno)
+{
+    return ff_jpeg2000_sigctxno_lut[flag & 255][bandno];
+}
+
+static const uint8_t refctxno_lut[2][2] = { { 14, 15 }, { 16, 16 } };
+
+/* Get context label (number in range[14..16]) of a coefficient for magnitude
+ * refinement pass. */
+static inline int ff_jpeg2000_getrefctxno(int flag)
+{
+    return refctxno_lut[(flag >> 14) & 1][(flag & 255) != 0];
+}
+
+extern uint8_t ff_jpeg2000_sgnctxno_lut[16][16];
+extern uint8_t ff_jpeg2000_xorbit_lut[16][16];
+
+/* Get context label (number in range[9..13]) for sign decoding. */
+static inline int ff_jpeg2000_getsgnctxno(int flag, int *xorbit)
+{
+    *xorbit = ff_jpeg2000_xorbit_lut[flag & 15][(flag >> 8) & 15];
+    return ff_jpeg2000_sgnctxno_lut[flag & 15][(flag >> 8) & 15];
+}
+
+int ff_jpeg2000_init_component(Jpeg2000Component *comp,
+                               Jpeg2000CodingStyle *codsty,
+                               Jpeg2000QuantStyle *qntsty,
+                               int cbps, int dx, int dy,
+                               AVCodecContext *ctx);
+
+void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
+
+#endif /* AVCODEC_JPEG2000_H */
diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
new file mode 100644
index 0000000..20e5c9c
--- /dev/null
+++ b/libavcodec/jpeg2000dec.c
@@ -0,0 +1,1339 @@
+/*
+ * JPEG 2000 image decoder
+ * Copyright (c) 2007 Kamil Nowosad
+ * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.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
+ */
+
+/**
+ * @file
+ * JPEG 2000 image decoder
+ */
+
+#include "libavutil/common.h"
+#include "libavutil/opt.h"
+#include "avcodec.h"
+#include "bytestream.h"
+#include "internal.h"
+#include "thread.h"
+#include "jpeg2000.h"
+
+#define JP2_SIG_TYPE    0x6A502020
+#define JP2_SIG_VALUE   0x0D0A870A
+#define JP2_CODESTREAM  0x6A703263
+
+#define HAD_COC 0x01
+#define HAD_QCC 0x02
+
+typedef struct Jpeg2000TilePart {
+    uint16_t tp_idx;                    // Tile-part index
+    uint8_t tile_index;                 // Tile index who refers the tile-part
+    uint32_t tp_len;                    // Length of tile-part
+    const uint8_t *tp_start_bstrm;      // Start address bit stream in tile-part
+    const uint8_t *tp_end_bstrm;        // End address of the bit stream tile part
+} Jpeg2000TilePart;
+
+/* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
+ * one per component, so tile_part elements have a size of 3 */
+typedef struct Jpeg2000Tile {
+    Jpeg2000Component   *comp;
+    uint8_t             properties[4];
+    Jpeg2000CodingStyle codsty[4];
+    Jpeg2000QuantStyle  qntsty[4];
+    Jpeg2000TilePart    tile_part[3];
+} Jpeg2000Tile;
+
+typedef struct Jpeg2000DecoderContext {
+    AVClass         *class;
+    AVCodecContext  *avctx;
+
+    int             width, height;
+    int             image_offset_x, image_offset_y;
+    int             tile_offset_x, tile_offset_y;
+    uint8_t         cbps[4];    // bits per sample in particular components
+    uint8_t         sgnd[4];    // if a component is signed
+    uint8_t         properties[4];
+    int             cdx[4], cdy[4];
+    int             precision;
+    int             ncomponents;
+    int             tile_width, tile_height;
+    int             numXtiles, numYtiles;
+    int             maxtilelen;
+
+    Jpeg2000CodingStyle codsty[4];
+    Jpeg2000QuantStyle  qntsty[4];
+
+    const uint8_t   *buf_start;
+    const uint8_t   *buf;
+    const uint8_t   *buf_end;
+    int             bit_index;
+
+    int16_t         curtileno;
+    Jpeg2000Tile    *tile;
+
+    /*options parameters*/
+    int16_t         lowres;
+    int16_t         reduction_factor;
+} Jpeg2000DecoderContext;
+
+/* get_bits functions for JPEG2000 packet bitstream
+ * It is a get_bit function with a bit-stuffing routine. If the value of the
+ * byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
+ * cf. ISO-15444-1:2002 / B.10.1 Bit-stuffing routine */
+static int get_bits(Jpeg2000DecoderContext *s, int n)
+{
+    int res = 0;
+    if (s->buf_end - s->buf < ((n - s->bit_index) >> 8))
+        return AVERROR(EINVAL);
+    while (--n >= 0) {
+        res <<= 1;
+        if (s->bit_index == 0) {
+            s->bit_index = 7 + (*s->buf != 0xff);
+            s->buf++;
+        }
+        s->bit_index--;
+        res |= (*s->buf >> s->bit_index) & 1;
+    }
+    return res;
+}
+
+static void jpeg2000_flush(Jpeg2000DecoderContext *s)
+{
+    if (*s->buf == 0xff)
+        s->buf++;
+    s->bit_index = 8;
+    s->buf++;
+}
+
+/* decode the value stored in node */
+static int tag_tree_decode(Jpeg2000DecoderContext *s, Jpeg2000TgtNode *node,
+                           int threshold)
+{
+    Jpeg2000TgtNode *stack[30];
+    int sp = -1, curval = 0;
+
+    while (node && !node->vis) {
+        stack[++sp] = node;
+        node        = node->parent;
+    }
+
+    if (node)
+        curval = node->val;
+    else
+        curval = stack[sp]->val;
+
+    while (curval < threshold && sp >= 0) {
+        if (curval < stack[sp]->val)
+            curval = stack[sp]->val;
+        while (curval < threshold) {
+            int ret;
+            if ((ret = get_bits(s, 1)) > 0) {
+                stack[sp]->vis++;
+                break;
+            } else if (!ret)
+                curval++;
+            else
+                return ret;
+        }
+        stack[sp]->val = curval;
+        sp--;
+    }
+    return curval;
+}
+
+/* marker segments */
+/* get sizes and offsets of image, tiles; number of components */
+static int get_siz(Jpeg2000DecoderContext *s)
+{
+    int i;
+
+    if (s->buf_end - s->buf < 36)
+        return AVERROR(EINVAL);
+
+    s->avctx->profile = bytestream_get_be16(&s->buf); // Rsiz
+    s->width          = bytestream_get_be32(&s->buf); // Width
+    s->height         = bytestream_get_be32(&s->buf); // Height
+    s->image_offset_x = bytestream_get_be32(&s->buf); // X0Siz
+    s->image_offset_y = bytestream_get_be32(&s->buf); // Y0Siz
+    s->tile_width     = bytestream_get_be32(&s->buf); // XTSiz
+    s->tile_height    = bytestream_get_be32(&s->buf); // YTSiz
+    s->tile_offset_x  = bytestream_get_be32(&s->buf); // XT0Siz
+    s->tile_offset_y  = bytestream_get_be32(&s->buf); // YT0Siz
+    s->ncomponents    = bytestream_get_be16(&s->buf); // CSiz
+
+    if (s->buf_end - s->buf < 2 * s->ncomponents)
+        return AVERROR(EINVAL);
+
+    for (i = 0; i < s->ncomponents; i++) { // Ssiz_i XRsiz_i, YRsiz_i
+        uint8_t x = bytestream_get_byte(&s->buf);
+        s->cbps[i]   = (x & 0x7f) + 1;
+        s->precision = FFMAX(s->cbps[i], s->precision);
+        s->sgnd[i]   = (x & 0x80) == 1;
+        s->cdx[i]    = bytestream_get_byte(&s->buf);
+        s->cdy[i]    = bytestream_get_byte(&s->buf);
+    }
+
+    s->numXtiles = ff_jpeg2000_ceildiv(s->width  - s->tile_offset_x, s->tile_width);
+    s->numYtiles = ff_jpeg2000_ceildiv(s->height - s->tile_offset_y, s->tile_height);
+
+    s->tile = av_mallocz(s->numXtiles * s->numYtiles * sizeof(*s->tile));
+    if (!s->tile)
+        return AVERROR(ENOMEM);
+
+    for (i = 0; i < s->numXtiles * s->numYtiles; i++) {
+        Jpeg2000Tile *tile = s->tile + i;
+
+        tile->comp = av_mallocz(s->ncomponents * sizeof(*tile->comp));
+        if (!tile->comp)
+            return AVERROR(ENOMEM);
+    }
+
+    /* compute image size with reduction factor */
+    s->avctx->width  = ff_jpeg2000_ceildivpow2(s->width  - s->image_offset_x,
+                                               s->reduction_factor);
+    s->avctx->height = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
+                                               s->reduction_factor);
+
+    switch (s->avctx->profile) {
+    case FF_PROFILE_JPEG2000_DCINEMA_2K:
+    case FF_PROFILE_JPEG2000_DCINEMA_4K:
+        /* XYZ color-space for digital cinema profiles */
+        s->avctx->pix_fmt = AV_PIX_FMT_XYZ12;
+        break;
+    default:
+        /* For other profiles selects color-space according number of
+         * components and bit depth precision. */
+        switch (s->ncomponents) {
+        case 1:
+            if (s->precision > 8)
+                s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
+            else
+                s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
+            break;
+        case 3:
+            if (s->precision > 8)
+                s->avctx->pix_fmt = AV_PIX_FMT_RGB48;
+            else
+                s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
+            break;
+        case 4:
+            s->avctx->pix_fmt = AV_PIX_FMT_BGRA;
+            break;
+        default:
+            /* pixel format can not be identified */
+            s->avctx->pix_fmt = AV_PIX_FMT_NONE;
+            break;
+        }
+        break;
+    }
+    return 0;
+}
+
+/* get common part for COD and COC segments */
+static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
+{
+    uint8_t byte;
+
+    if (s->buf_end - s->buf < 5)
+        return AVERROR(EINVAL);
+    c->nreslevels = bytestream_get_byte(&s->buf) + 1; // num of resolution levels - 1
+
+    /* compute number of resolution levels to decode */
+    if (c->nreslevels < s->reduction_factor)
+        c->nreslevels2decode = 1;
+    else
+        c->nreslevels2decode = c->nreslevels - s->reduction_factor;
+
+    c->log2_cblk_width  = bytestream_get_byte(&s->buf) + 2; // cblk width
+    c->log2_cblk_height = bytestream_get_byte(&s->buf) + 2; // cblk height
+
+    c->cblk_style = bytestream_get_byte(&s->buf);
+    if (c->cblk_style != 0) { // cblk style
+        av_log(s->avctx, AV_LOG_ERROR, "no extra cblk styles supported\n");
+        return -1;
+    }
+    c->transform = bytestream_get_byte(&s->buf); // DWT transformation type
+    /* set integer 9/7 DWT in case of BITEXACT flag */
+    if ((s->avctx->flags & CODEC_FLAG_BITEXACT) && (c->transform == FF_DWT97))
+        c->transform = FF_DWT97_INT;
+
+    if (c->csty & JPEG2000_CSTY_PREC) {
+        int i;
+        for (i = 0; i < c->nreslevels; i++) {
+            byte = bytestream_get_byte(&s->buf);
+            c->log2_prec_widths[i]  =  byte       & 0x0F;    // precinct PPx
+            c->log2_prec_heights[i] = (byte >> 4) & 0x0F;    // precinct PPy
+        }
+    }
+    return 0;
+}
+
+/* get coding parameters for a particular tile or whole image*/
+static int get_cod(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
+                   uint8_t *properties)
+{
+    Jpeg2000CodingStyle tmp;
+    int compno;
+
+    if (s->buf_end - s->buf < 5)
+        return AVERROR(EINVAL);
+
+    tmp.log2_prec_width  =
+    tmp.log2_prec_height = 15;
+
+    tmp.csty = bytestream_get_byte(&s->buf);
+
+    // get progression order
+    tmp.prog_order = bytestream_get_byte(&s->buf);
+
+    tmp.nlayers = bytestream_get_be16(&s->buf);
+    tmp.mct     = bytestream_get_byte(&s->buf); // multiple component transformation
+
+    get_cox(s, &tmp);
+    for (compno = 0; compno < s->ncomponents; compno++)
+        if (!(properties[compno] & HAD_COC))
+            memcpy(c + compno, &tmp, sizeof(tmp));
+    return 0;
+}
+
+/* Get coding parameters for a component in the whole image or a
+ * particular tile. */
+static int get_coc(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c,
+                   uint8_t *properties)
+{
+    int compno;
+
+    if (s->buf_end - s->buf < 2)
+        return AVERROR(EINVAL);
+
+    compno = bytestream_get_byte(&s->buf);
+
+    c      += compno;
+    c->csty = bytestream_get_byte(&s->buf);
+    get_cox(s, c);
+
+    properties[compno] |= HAD_COC;
+    return 0;
+}
+
+/* Get common part for QCD and QCC segments. */
+static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
+{
+    int i, x;
+
+    if (s->buf_end - s->buf < 1)
+        return AVERROR(EINVAL);
+
+    x = bytestream_get_byte(&s->buf); // Sqcd
+
+    q->nguardbits = x >> 5;
+    q->quantsty   = x & 0x1f;
+
+    if (q->quantsty == JPEG2000_QSTY_NONE) {
+        n -= 3;
+        if (s->buf_end - s->buf < n)
+            return AVERROR(EINVAL);
+        for (i = 0; i < n; i++)
+            q->expn[i] = bytestream_get_byte(&s->buf) >> 3;
+    } else if (q->quantsty == JPEG2000_QSTY_SI) {
+        if (s->buf_end - s->buf < 2)
+            return AVERROR(EINVAL);
+        x          = bytestream_get_be16(&s->buf);
+        q->expn[0] = x >> 11;
+        q->mant[0] = x & 0x7ff;
+        for (i = 1; i < 32 * 3; i++) {
+            int curexpn = FFMAX(0, q->expn[0] - (i - 1) / 3);
+            q->expn[i] = curexpn;
+            q->mant[i] = q->mant[0];
+        }
+    } else {
+        n = (n - 3) >> 1;
+        if (s->buf_end - s->buf < n)
+            return AVERROR(EINVAL);
+        for (i = 0; i < n; i++) {
+            x          = bytestream_get_be16(&s->buf);
+            q->expn[i] = x >> 11;
+            q->mant[i] = x & 0x7ff;
+        }
+    }
+    return 0;
+}
+
+/* Get quantization parameters for a particular tile or a whole image. */
+static int get_qcd(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
+                   uint8_t *properties)
+{
+    Jpeg2000QuantStyle tmp;
+    int compno;
+
+    if (get_qcx(s, n, &tmp))
+        return -1;
+    for (compno = 0; compno < s->ncomponents; compno++)
+        if (!(properties[compno] & HAD_QCC))
+            memcpy(q + compno, &tmp, sizeof(tmp));
+    return 0;
+}
+
+/* Get quantization parameters for a component in the whole image
+ * on in a particular tile. */
+static int get_qcc(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q,
+                   uint8_t *properties)
+{
+    int compno;
+
+    if (s->buf_end - s->buf < 1)
+        return AVERROR(EINVAL);
+
+    compno              = bytestream_get_byte(&s->buf);
+    properties[compno] |= HAD_QCC;
+    return get_qcx(s, n - 1, q + compno);
+}
+
+/* Get start of tile segment. */
+static uint8_t get_sot(Jpeg2000DecoderContext *s, int n)
+{
+    Jpeg2000TilePart *tp;
+    uint16_t Isot;
+    uint32_t Psot;
+    uint8_t TPsot;
+
+    if (s->buf_end - s->buf < 4)
+        return AVERROR(EINVAL);
+
+    Isot = bytestream_get_be16(&s->buf);        // Isot
+    if (Isot) {
+        av_log(s->avctx, AV_LOG_ERROR,
+               "Not a DCINEMA JP2K file: more than one tile\n");
+        return -1;
+    }
+    Psot  = bytestream_get_be32(&s->buf);       // Psot
+    TPsot = bytestream_get_byte(&s->buf);       // TPsot
+
+    /* Read TNSot but not used */
+    bytestream_get_byte(&s->buf);               // TNsot
+
+    tp             = s->tile[s->curtileno].tile_part + TPsot;
+    tp->tile_index = Isot;
+    tp->tp_len     = Psot;
+    tp->tp_idx     = TPsot;
+
+    /* Start of bit stream. Pointer to SOD marker
+     * Check SOD marker is present. */
+    if (JPEG2000_SOD == bytestream_get_be16(&s->buf))
+        tp->tp_start_bstrm = s->buf;
+    else {
+        av_log(s->avctx, AV_LOG_ERROR, "SOD marker not found \n");
+        return -1;
+    }
+
+    /* End address of bit stream =
+     *     start address + (Psot - size of SOT HEADER(n)
+     *     - size of SOT MARKER(2)  - size of SOD marker(2) */
+    tp->tp_end_bstrm = s->buf + (tp->tp_len - n - 4);
+
+    // set buffer pointer to end of tile part header
+    s->buf = tp->tp_end_bstrm;
+
+    return 0;
+}
+
+/* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
+ * Used to know the number of tile parts and lengths.
+ * There may be multiple TLMs in the header.
+ * TODO: The function is not used for tile-parts management, nor anywhere else.
+ * It can be useful to allocate memory for tile parts, before managing the SOT
+ * markers. Parsing the TLM header is needed to increment the input header
+ * buffer.
+ * This marker is mandatory for DCI. */
+static uint8_t get_tlm(Jpeg2000DecoderContext *s, int n)
+{
+    uint8_t Stlm, ST, SP, tile_tlm, i;
+    bytestream_get_byte(&s->buf);               /* Ztlm: skipped */
+    Stlm = bytestream_get_byte(&s->buf);
+
+    // too complex ? ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
+    ST = (Stlm >> 4) & 0x03;
+    // TODO: Manage case of ST = 0b11 --> raise error
+    SP       = (Stlm >> 6) & 0x01;
+    tile_tlm = (n - 4) / ((SP + 1) * 2 + ST);
+    for (i = 0; i < tile_tlm; i++) {
+        switch (ST) {
+        case 0:
+            break;
+        case 1:
+            bytestream_get_byte(&s->buf);
+            break;
+        case 2:
+            bytestream_get_be16(&s->buf);
+            break;
+        case 3:
+            bytestream_get_be32(&s->buf);
+            break;
+        }
+        if (SP == 0) {
+            bytestream_get_be16(&s->buf);
+        } else {
+            bytestream_get_be32(&s->buf);
+        }
+    }
+    return 0;
+}
+
+static int init_tile(Jpeg2000DecoderContext *s, int tileno)
+{
+    int compno;
+    int tilex = tileno % s->numXtiles;
+    int tiley = tileno / s->numXtiles;
+    Jpeg2000Tile *tile = s->tile + tileno;
+    Jpeg2000CodingStyle *codsty;
+    Jpeg2000QuantStyle  *qntsty;
+
+    if (!tile->comp)
+        return AVERROR(ENOMEM);
+
+    /* copy codsty, qnsty to tile. TODO: Is it the best way?
+     * codsty, qnsty is an array of 4 structs Jpeg2000CodingStyle
+     * and Jpeg2000QuantStyle */
+    memcpy(tile->codsty, s->codsty, s->ncomponents * sizeof(*codsty));
+    memcpy(tile->qntsty, s->qntsty, s->ncomponents * sizeof(*qntsty));
+
+    for (compno = 0; compno < s->ncomponents; compno++) {
+        Jpeg2000Component *comp = tile->comp + compno;
+        int ret; // global bandno
+        codsty = tile->codsty + compno;
+        qntsty = tile->qntsty + compno;
+
+        comp->coord_o[0][0] = FFMAX(tilex       * s->tile_width  + s->tile_offset_x, s->image_offset_x);
+        comp->coord_o[0][1] = FFMIN((tilex + 1) * s->tile_width  + s->tile_offset_x, s->width);
+        comp->coord_o[1][0] = FFMAX(tiley       * s->tile_height + s->tile_offset_y, s->image_offset_y);
+        comp->coord_o[1][1] = FFMIN((tiley + 1) * s->tile_height + s->tile_offset_y, s->height);
+
+        // FIXME: add a dcinema profile check ?
+        // value is guaranteed by profile (orig=0, 1 tile)
+        comp->coord[0][0] = 0;
+        comp->coord[0][1] = s->avctx->width;
+        comp->coord[1][0] = 0;
+        comp->coord[1][1] = s->avctx->height;
+
+        if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
+                                             s->cbps[compno], s->cdx[compno],
+                                             s->cdy[compno], s->avctx))
+            return ret;
+    }
+    return 0;
+}
+
+/* Read the number of coding passes. */
+static int getnpasses(Jpeg2000DecoderContext *s)
+{
+    int num;
+    if (!get_bits(s, 1))
+        return 1;
+    if (!get_bits(s, 1))
+        return 2;
+    if ((num = get_bits(s, 2)) != 3)
+        return num < 0 ? num : 3 + num;
+    if ((num = get_bits(s, 5)) != 31)
+        return num < 0 ? num : 6 + num;
+    num = get_bits(s, 7);
+    return num < 0 ? num : 37 + num;
+}
+
+static int getlblockinc(Jpeg2000DecoderContext *s)
+{
+    int res = 0, ret;
+    while (ret = get_bits(s, 1)) {
+        if (ret < 0)
+            return ret;
+        res++;
+    }
+    return res;
+}
+
+static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s,
+                                  Jpeg2000CodingStyle *codsty,
+                                  Jpeg2000ResLevel *rlevel, int precno,
+                                  int layno, uint8_t *expn, int numgbits)
+{
+    int bandno, cblkno, ret, nb_code_blocks;
+
+    if (!(ret = get_bits(s, 1))) {
+        jpeg2000_flush(s);
+        return 0;
+    } else if (ret < 0)
+        return ret;
+
+    for (bandno = 0; bandno < rlevel->nbands; bandno++) {
+        Jpeg2000Band *band = rlevel->band + bandno;
+        Jpeg2000Prec *prec = band->prec + precno;
+
+        if (band->coord[0][0] == band->coord[0][1] ||
+            band->coord[1][0] == band->coord[1][1])
+            continue;
+        prec->yi0 = 0;
+        prec->xi0 = 0;
+        nb_code_blocks =  prec->nb_codeblocks_height *
+                          prec->nb_codeblocks_width;
+        for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
+            Jpeg2000Cblk *cblk = prec->cblk + cblkno;
+            int incl, newpasses, llen;
+
+            if (cblk->npasses)
+                incl = get_bits(s, 1);
+            else
+                incl = tag_tree_decode(s, prec->cblkincl + cblkno, layno + 1) == layno;
+            if (!incl)
+                continue;
+            else if (incl < 0)
+                return incl;
+
+            if (!cblk->npasses)
+                cblk->nonzerobits = expn[bandno] + numgbits - 1 -
+                                    tag_tree_decode(s, prec->zerobits + cblkno,
+                                                    100);
+            if ((newpasses = getnpasses(s)) < 0)
+                return newpasses;
+            if ((llen = getlblockinc(s)) < 0)
+                return llen;
+            cblk->lblock += llen;
+            if ((ret = get_bits(s, av_log2(newpasses) + cblk->lblock)) < 0)
+                return ret;
+            cblk->lengthinc = ret;
+            cblk->npasses  += newpasses;
+        }
+    }
+    jpeg2000_flush(s);
+
+    if (codsty->csty & JPEG2000_CSTY_EPH) {
+        if (AV_RB16(s->buf) == JPEG2000_EPH)
+            s->buf += 2;
+        else
+            av_log(s->avctx, AV_LOG_ERROR, "EPH marker not found.\n");
+    }
+
+    for (bandno = 0; bandno < rlevel->nbands; bandno++) {
+        Jpeg2000Band *band = rlevel->band + bandno;
+        Jpeg2000Prec *prec = band->prec + precno;
+
+        nb_code_blocks = prec->nb_codeblocks_height * prec->nb_codeblocks_width;
+        for (cblkno = 0; cblkno < nb_code_blocks; cblkno++) {
+            Jpeg2000Cblk *cblk = prec->cblk + cblkno;
+            if (s->buf_end - s->buf < cblk->lengthinc)
+                return AVERROR(EINVAL);
+            bytestream_get_buffer(&s->buf, cblk->data, cblk->lengthinc);
+            cblk->length   += cblk->lengthinc;
+            cblk->lengthinc = 0;
+        }
+    }
+    return 0;
+}
+
+static int jpeg2000_decode_packets(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
+{
+    int layno, reslevelno, compno, precno, ok_reslevel;
+    uint8_t prog_order = tile->codsty[0].prog_order;
+    uint16_t x;
+    uint16_t y;
+
+    s->bit_index = 8;
+    switch (prog_order) {
+    case JPEG2000_PGOD_LRCP:
+        for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
+            ok_reslevel = 1;
+            for (reslevelno = 0; ok_reslevel; reslevelno++) {
+                ok_reslevel = 0;
+                for (compno = 0; compno < s->ncomponents; compno++) {
+                    Jpeg2000CodingStyle *codsty = tile->codsty + compno;
+                    Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
+                    if (reslevelno < codsty->nreslevels) {
+                        Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel +
+                                                   reslevelno;
+                        ok_reslevel = 1;
+                        for (precno = 0; precno < rlevel->num_precincts_x * rlevel->num_precincts_y; precno++)
+                            if (jpeg2000_decode_packet(s,
+                                                       codsty, rlevel,
+                                                       precno, layno,
+                                                       qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
+                                                       qntsty->nguardbits))
+                                return -1;
+                    }
+                }
+            }
+        }
+        break;
+
+    case JPEG2000_PGOD_CPRL:
+        for (compno = 0; compno < s->ncomponents; compno++) {
+            Jpeg2000CodingStyle *codsty = tile->codsty + compno;
+            Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
+
+            /* Set bit stream buffer address according to tile-part.
+             * For DCinema one tile-part per component, so can be
+             * indexed by component. */
+            s->buf = tile->tile_part[compno].tp_start_bstrm;
+
+            /* Position loop (y axis)
+             * TODO: Automate computing of step 256.
+             * Fixed here, but to be computed before entering here. */
+            for (y = 0; y < s->height; y += 256) {
+                /* Position loop (y axis)
+                 * TODO: automate computing of step 256.
+                 * Fixed here, but to be computed before entering here. */
+                for (x = 0; x < s->width; x += 256) {
+                    for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
+                        uint16_t prcx, prcy;
+                        uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  ==> N_L - r
+                        Jpeg2000ResLevel *rlevel = tile->comp[compno].reslevel + reslevelno;
+
+                        if (!((y % (1 << (rlevel->log2_prec_height + reducedresno)) == 0) ||
+                              (y == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
+                            continue;
+
+                        if (!((x % (1 << (rlevel->log2_prec_width + reducedresno)) == 0) ||
+                              (x == 0))) // TODO: 2nd condition simplified as try0 always =0 for dcinema
+                            continue;
+
+                        // check if a precinct exists
+                        prcx   = ff_jpeg2000_ceildivpow2(x, reducedresno) >> rlevel->log2_prec_width;
+                        prcy   = ff_jpeg2000_ceildivpow2(y, reducedresno) >> rlevel->log2_prec_height;
+                        precno = prcx + rlevel->num_precincts_x * prcy;
+                        for (layno = 0; layno < tile->codsty[0].nlayers; layno++) {
+                            if (jpeg2000_decode_packet(s, codsty, rlevel,
+                                                       precno, layno,
+                                                       qntsty->expn + (reslevelno ? 3 * (reslevelno - 1) + 1 : 0),
+                                                       qntsty->nguardbits))
+                                return -1;
+                        }
+                    }
+                }
+            }
+        }
+        break;
+
+    default:
+        break;
+    }
+
+    /* EOC marker reached */
+    s->buf += 2;
+
+    return 0;
+}
+
+/* TIER-1 routines */
+static void decode_sigpass(Jpeg2000T1Context *t1, int width, int height,
+                           int bpno, int bandno)
+{
+    int mask = 3 << (bpno - 1), y0, x, y;
+
+    for (y0 = 0; y0 < height; y0 += 4)
+        for (x = 0; x < width; x++)
+            for (y = y0; y < height && y < y0 + 4; y++)
+                if ((t1->flags[y + 1][x + 1] & JPEG2000_T1_SIG_NB)
+                    && !(t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS))) {
+                    if (ff_mqc_decode(&t1->mqc,
+                                      t1->mqc.cx_states +
+                                      ff_jpeg2000_getsigctxno(t1->flags[y + 1][x + 1],
+                                                             bandno))) {
+                        int xorbit, ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
+                                                                    &xorbit);
+
+                        t1->data[y][x] =
+                            (ff_mqc_decode(&t1->mqc,
+                                           t1->mqc.cx_states + ctxno) ^ xorbit)
+                            ? -mask : mask;
+
+                        ff_jpeg2000_set_significance(t1, x, y,
+                                                     t1->data[y][x] < 0);
+                    }
+                    t1->flags[y + 1][x + 1] |= JPEG2000_T1_VIS;
+                }
+}
+
+static void decode_refpass(Jpeg2000T1Context *t1, int width, int height,
+                           int bpno)
+{
+    int phalf, nhalf;
+    int y0, x, y;
+
+    phalf = 1 << (bpno - 1);
+    nhalf = -phalf;
+
+    for (y0 = 0; y0 < height; y0 += 4)
+        for (x = 0; x < width; x++)
+            for (y = y0; y < height && y < y0 + 4; y++)
+                if ((t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)) == JPEG2000_T1_SIG) {
+                    int ctxno = ff_jpeg2000_getrefctxno(t1->flags[y + 1][x + 1]);
+                    int r     = ff_mqc_decode(&t1->mqc,
+                                              t1->mqc.cx_states + ctxno)
+                                ? phalf : nhalf;
+                    t1->data[y][x]          += t1->data[y][x] < 0 ? -r : r;
+                    t1->flags[y + 1][x + 1] |= JPEG2000_T1_REF;
+                }
+}
+
+static void decode_clnpass(Jpeg2000DecoderContext *s, Jpeg2000T1Context *t1,
+                           int width, int height, int bpno, int bandno,
+                           int seg_symbols)
+{
+    int mask = 3 << (bpno - 1), y0, x, y, runlen, dec;
+
+    for (y0 = 0; y0 < height; y0 += 4)
+        for (x = 0; x < width; x++) {
+            if (y0 + 3 < height &&
+                !((t1->flags[y0 + 1][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
+                  (t1->flags[y0 + 2][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
+                  (t1->flags[y0 + 3][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)) ||
+                  (t1->flags[y0 + 4][x + 1] & (JPEG2000_T1_SIG_NB | JPEG2000_T1_VIS | JPEG2000_T1_SIG)))) {
+                if (!ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_RL))
+                    continue;
+                runlen = ff_mqc_decode(&t1->mqc,
+                                       t1->mqc.cx_states + MQC_CX_UNI);
+                runlen = (runlen << 1) | ff_mqc_decode(&t1->mqc,
+                                                       t1->mqc.cx_states +
+                                                       MQC_CX_UNI);
+                dec = 1;
+            } else {
+                runlen = 0;
+                dec    = 0;
+            }
+
+            for (y = y0 + runlen; y < y0 + 4 && y < height; y++) {
+                if (!dec) {
+                    if (!(t1->flags[y + 1][x + 1] & (JPEG2000_T1_SIG | JPEG2000_T1_VIS)))
+                        dec = ff_mqc_decode(&t1->mqc,
+                                            t1->mqc.cx_states +
+                                            ff_jpeg2000_getsigctxno(t1->flags[y + 1][x + 1],
+                                                                   bandno));
+                }
+                if (dec) {
+                    int xorbit;
+                    int ctxno = ff_jpeg2000_getsgnctxno(t1->flags[y + 1][x + 1],
+                                                        &xorbit);
+                    t1->data[y][x] = (ff_mqc_decode(&t1->mqc,
+                                                    t1->mqc.cx_states + ctxno) ^
+                                      xorbit)
+                                     ? -mask : mask;
+                    ff_jpeg2000_set_significance(t1, x, y, t1->data[y][x] < 0);
+                }
+                dec = 0;
+                t1->flags[y + 1][x + 1] &= ~JPEG2000_T1_VIS;
+            }
+        }
+    if (seg_symbols) {
+        int val;
+        val = ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
+        val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
+        val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
+        val = (val << 1) + ff_mqc_decode(&t1->mqc, t1->mqc.cx_states + MQC_CX_UNI);
+        if (val != 0xa)
+            av_log(s->avctx, AV_LOG_ERROR,
+                   "Segmentation symbol value incorrect\n");
+    }
+}
+
+static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
+                       Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
+                       int width, int height, int bandpos)
+{
+    int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1, y;
+
+    for (y = 0; y < height + 2; y++)
+        memset(t1->flags[y], 0, (width + 2) * sizeof(width));
+
+    for (y = 0; y < height; y++)
+        memset(t1->data[y], 0, width * sizeof(width));
+
+    ff_mqc_initdec(&t1->mqc, cblk->data);
+    cblk->data[cblk->length]     = 0xff;
+    cblk->data[cblk->length + 1] = 0xff;
+
+    while (passno--) {
+        switch (pass_t) {
+        case 0:
+            decode_sigpass(t1, width, height, bpno + 1, bandpos);
+            break;
+        case 1:
+            decode_refpass(t1, width, height, bpno + 1);
+            break;
+        case 2:
+            decode_clnpass(s, t1, width, height, bpno + 1, bandpos,
+                           codsty->cblk_style & JPEG2000_CBLK_SEGSYM);
+            break;
+        }
+
+        pass_t++;
+        if (pass_t == 3) {
+            bpno--;
+            pass_t = 0;
+        }
+    }
+    return 0;
+}
+
+/* TODO: Verify dequantization for lossless case
+ * comp->data can be float or int
+ * band->stepsize can be float or int
+ * depending on the type of DWT transformation.
+ * see ISO/IEC 15444-1:2002 A.6.1 */
+
+/* Float dequantization of a codeblock.*/
+static void dequantization_float(int x, int y, Jpeg2000Cblk *cblk,
+                                 Jpeg2000Component *comp,
+                                 Jpeg2000T1Context *t1, Jpeg2000Band *band)
+{
+    int i, j, idx;
+    float *datap = &comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x];
+    for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j)
+        for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) {
+            idx        = (comp->coord[0][1] - comp->coord[0][0]) * j + i;
+            datap[idx] = (float)(t1->data[j][i]) * ((float)band->stepsize);
+        }
+    return;
+}
+
+/* Integer dequantization of a codeblock.*/
+static void dequantization_int(int x, int y, Jpeg2000Cblk *cblk,
+                               Jpeg2000Component *comp,
+                               Jpeg2000T1Context *t1, Jpeg2000Band *band)
+{
+    int i, j, idx;
+    int32_t *datap =
+        (int32_t *) &comp->data[(comp->coord[0][1] - comp->coord[0][0]) * y + x];
+    for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j)
+        for (i = 0; i < (cblk->coord[0][1] - cblk->coord[0][0]); ++i) {
+            idx        = (comp->coord[0][1] - comp->coord[0][0]) * j + i;
+            datap[idx] =
+                ((int32_t)(t1->data[j][i]) * ((int32_t)band->stepsize) + (1 << 15)) >> 16;
+        }
+    return;
+}
+
+/* Inverse ICT parameters in float and integer.
+ * int value = (float value) * (1<<16) */
+static const float f_ict_params[4] = {
+    1.402f,
+    0.34413f,
+    0.71414f,
+    1.772f
+};
+static const int   i_ict_params[4] = {
+     91881,
+     22553,
+     46802,
+    116130
+};
+
+static int mct_decode(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile)
+{
+    int i, csize = 1;
+    int ret = 0;
+    int32_t *src[3],  i0,  i1,  i2;
+    float   *srcf[3], i0f, i1f, i2f;
+
+    for (i = 0; i < 3; i++)
+        if (tile->codsty[0].transform == FF_DWT97)
+            srcf[i] = tile->comp[i].data;
+        else
+            src[i] = (int32_t *)tile->comp[i].data;
+
+    for (i = 0; i < 2; i++)
+        csize *= tile->comp[0].coord[i][1] - tile->comp[0].coord[i][0];
+    switch (tile->codsty[0].transform) {
+    case FF_DWT97:
+        for (i = 0; i < csize; i++) {
+            i0f = *srcf[0] + (f_ict_params[0] * *srcf[2]);
+            i1f = *srcf[0] - (f_ict_params[1] * *srcf[1])
+                           - (f_ict_params[2] * *srcf[2]);
+            i2f = *srcf[0] + (f_ict_params[3] * *srcf[1]);
+            *srcf[0]++ = i0f;
+            *srcf[1]++ = i1f;
+            *srcf[2]++ = i2f;
+        }
+        break;
+    case FF_DWT97_INT:
+        for (i = 0; i < csize; i++) {
+            i0 = *src[0] + (((i_ict_params[0] * *src[2]) + (1 << 15)) >> 16);
+            i1 = *src[0] - (((i_ict_params[1] * *src[1]) + (1 << 15)) >> 16)
+                         - (((i_ict_params[2] * *src[2]) + (1 << 15)) >> 16);
+            i2 = *src[0] + (((i_ict_params[3] * *src[1]) + (1 << 15)) >> 16);
+            *src[0]++ = i0;
+            *src[1]++ = i1;
+            *src[2]++ = i2;
+        }
+        break;
+    case FF_DWT53:
+        for (i = 0; i < csize; i++) {
+            i1 = *src[0] - (*src[2] + *src[1] >> 2);
+            i0 = i1 + *src[2];
+            i2 = i1 + *src[1];
+            *src[0]++ = i0;
+            *src[1]++ = i1;
+            *src[2]++ = i2;
+        }
+        break;
+    }
+    return ret;
+}
+
+static int jpeg2000_decode_tile(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+                                AVFrame *picture)
+{
+    int compno, reslevelno, bandno;
+    int x, y;
+
+    uint8_t *line;
+    Jpeg2000T1Context t1;
+    /* Loop on tile components */
+
+    for (compno = 0; compno < s->ncomponents; compno++) {
+        Jpeg2000Component *comp     = tile->comp + compno;
+        Jpeg2000CodingStyle *codsty = tile->codsty + compno;
+        /* Loop on resolution levels */
+        for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
+            Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
+            /* Loop on bands */
+            for (bandno = 0; bandno < rlevel->nbands; bandno++) {
+                uint16_t nb_precincts, precno;
+                Jpeg2000Band *band = rlevel->band + bandno;
+                int cblkno = 0, bandpos;
+                bandpos = bandno + (reslevelno > 0);
+
+                nb_precincts = rlevel->num_precincts_x * rlevel->num_precincts_y;
+                /* Loop on precincts */
+                for (precno = 0; precno < nb_precincts; precno++) {
+                    Jpeg2000Prec *prec = band->prec + precno;
+
+                    /* Loop on codeblocks */
+                    for (cblkno = 0; cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height; cblkno++) {
+                        int x, y;
+                        Jpeg2000Cblk *cblk = prec->cblk + cblkno;
+                        decode_cblk(s, codsty, &t1, cblk,
+                                    cblk->coord[0][1] - cblk->coord[0][0],
+                                    cblk->coord[1][1] - cblk->coord[1][0],
+                                    bandpos);
+
+                        /* Manage band offsets */
+                        x = cblk->coord[0][0];
+                        y = cblk->coord[1][0];
+                        if ((reslevelno > 0) && ((bandno + 1) & 1)) {
+                            Jpeg2000ResLevel *pres = comp->reslevel + (reslevelno - 1);
+                            x += pres->coord[0][1] - pres->coord[0][0];
+                        }
+                        if ((reslevelno > 0) && ((bandno + 1) & 2)) {
+                            Jpeg2000ResLevel *pres = comp->reslevel + (reslevelno - 1);
+                            y += pres->coord[1][1] - pres->coord[1][0];
+                        }
+
+                        if (s->avctx->flags & CODEC_FLAG_BITEXACT)
+                            dequantization_int(x, y, cblk, comp, &t1, band);
+                        else
+                            dequantization_float(x, y, cblk, comp, &t1, band);
+                   } /* end cblk */
+                } /*end prec */
+            } /* end band */
+        } /* end reslevel */
+
+        /* inverse DWT */
+        ff_dwt_decode(&comp->dwt, comp->data);
+    } /*end comp */
+
+    /* inverse MCT transformation */
+    if (tile->codsty[0].mct)
+        mct_decode(s, tile);
+
+    if (s->avctx->pix_fmt == PIX_FMT_BGRA) // RGBA -> BGRA
+        FFSWAP(float *, tile->comp[0].data, tile->comp[2].data);
+
+    if (s->precision <= 8) {
+        for (compno = 0; compno < s->ncomponents; compno++) {
+            Jpeg2000Component *comp = tile->comp + compno;
+            int32_t *datap = (int32_t *)comp->data;
+            y    = tile->comp[compno].coord[1][0] - s->image_offset_y;
+            line = picture->data[0] + y * picture->linesize[0];
+            for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
+                uint8_t *dst;
+
+                x   = tile->comp[compno].coord[0][0] - s->image_offset_x;
+                dst = line + x * s->ncomponents + compno;
+
+                for (; x < tile->comp[compno].coord[0][1] - s->image_offset_x; x += s->cdx[compno]) {
+                    *datap += 1 << (s->cbps[compno] - 1);
+                    if (*datap < 0)
+                        *datap = 0;
+                    else if (*datap >= (1 << s->cbps[compno]))
+                        *datap = (1 << s->cbps[compno]) - 1;
+                    *dst = *datap++;
+                    dst += s->ncomponents;
+                }
+                line += picture->linesize[0];
+            }
+        }
+    } else {
+        for (compno = 0; compno < s->ncomponents; compno++) {
+            Jpeg2000Component *comp = tile->comp + compno;
+            float *datap = comp->data;
+            int32_t *i_datap = (int32_t *) comp->data;
+            uint16_t *linel;
+
+            y     = tile->comp[compno].coord[1][0] - s->image_offset_y;
+            linel = (uint16_t *)picture->data[0] + y * (picture->linesize[0] >> 1);
+            for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; y += s->cdy[compno]) {
+                uint16_t *dst;
+                x   = tile->comp[compno].coord[0][0] - s->image_offset_x;
+                dst = linel + (x * s->ncomponents + compno);
+                for (; x < s->avctx->width; x += s->cdx[compno]) {
+                    int16_t val;
+                    /* DC level shift and clip see ISO 15444-1:2002 G.1.2 */
+                    if (s->avctx->flags & CODEC_FLAG_BITEXACT)
+                        val = *i_datap + (1 << (s->cbps[compno] - 1));
+                    else
+                        val = lrintf(*datap) + (1 << (s->cbps[compno] - 1));
+                    val = av_clip(val, 0, (1 << s->cbps[compno]) - 1);
+                    /* align 12 bit values in little-endian mode */
+                    *dst = val << 4;
+                    datap++;
+                    i_datap++;
+                    dst += s->ncomponents;
+                }
+                linel += picture->linesize[0] >> 1;
+            }
+        }
+    }
+    return 0;
+}
+
+static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext *s)
+{
+    int tileno, compno;
+    for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++) {
+        for (compno = 0; compno < s->ncomponents; compno++) {
+            Jpeg2000Component *comp     = s->tile[tileno].comp   + compno;
+            Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
+
+            ff_jpeg2000_cleanup(comp, codsty);
+        }
+        av_freep(&s->tile[tileno].comp);
+    }
+    av_freep(&s->tile);
+}
+
+static int jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
+{
+    Jpeg2000CodingStyle *codsty = s->codsty;
+    Jpeg2000QuantStyle *qntsty  = s->qntsty;
+    uint8_t *properties         = s->properties;
+
+    for (;;) {
+        int len, ret = 0;
+        uint16_t marker;
+        const uint8_t *oldbuf;
+
+        if (s->buf_end - s->buf < 2) {
+            av_log(s->avctx, AV_LOG_ERROR, "Missing EOC\n");
+            break;
+        }
+
+        marker = bytestream_get_be16(&s->buf);
+        oldbuf = s->buf;
+
+        if (marker == JPEG2000_EOC)
+            break;
+
+        if (s->buf_end - s->buf < 2)
+            return AVERROR(EINVAL);
+        len = bytestream_get_be16(&s->buf);
+        switch (marker) {
+        case JPEG2000_SIZ:
+            ret = get_siz(s);
+            break;
+        case JPEG2000_COC:
+            ret = get_coc(s, codsty, properties);
+            break;
+        case JPEG2000_COD:
+            ret = get_cod(s, codsty, properties);
+            break;
+        case JPEG2000_QCC:
+            ret = get_qcc(s, len, qntsty, properties);
+            break;
+        case JPEG2000_QCD:
+            ret = get_qcd(s, len, qntsty, properties);
+            break;
+        case JPEG2000_SOT:
+            ret = get_sot(s, len);
+            break;
+        case JPEG2000_COM:
+            // the comment is ignored
+            s->buf += len - 2;
+            break;
+        case JPEG2000_TLM:
+            // Tile-part lengths
+            ret = get_tlm(s, len);
+            break;
+        default:
+            av_log(s->avctx, AV_LOG_ERROR,
+                   "unsupported marker 0x%.4X at pos 0x%lX\n",
+                   marker, (uint64_t)(s->buf - s->buf_start - 4));
+            s->buf += len - 2;
+            break;
+        }
+        if (((s->buf - oldbuf != len) && (marker != JPEG2000_SOT)) || ret) {
+            av_log(s->avctx, AV_LOG_ERROR,
+                   "error during processing marker segment %.4x\n", marker);
+            return ret ? ret : -1;
+        }
+    }
+    return 0;
+}
+
+/* Read bit stream packets --> T2 operation. */
+static int jpeg2000_read_bitstream_packets(Jpeg2000DecoderContext *s)
+{
+    int ret = 0;
+    Jpeg2000Tile *tile = s->tile + s->curtileno;
+
+    if (ret = init_tile(s, s->curtileno))
+        return ret;
+    if (ret = jpeg2000_decode_packets(s, tile))
+        return ret;
+
+    return 0;
+}
+
+static int jp2_find_codestream(Jpeg2000DecoderContext *s)
+{
+    int32_t atom_size;
+    int found_codestream = 0, search_range = 10;
+
+    // Skip JPEG 2000 signature atom.
+    s->buf += 12;
+
+    while (!found_codestream && search_range) {
+        atom_size = AV_RB32(s->buf);
+        if (AV_RB32(s->buf + 4) == JP2_CODESTREAM) {
+            found_codestream = 1;
+            s->buf += 8;
+        } else {
+            s->buf += atom_size;
+            search_range--;
+        }
+    }
+
+    if (found_codestream)
+        return 1;
+    return 0;
+}
+
+static int jpeg2000_decode_frame(AVCodecContext *avctx, void *data,
+                                 int *got_frame, AVPacket *avpkt)
+{
+    Jpeg2000DecoderContext *s = avctx->priv_data;
+    ThreadFrame frame = { .f = data };
+    AVFrame *picture = data;
+    int tileno, ret;
+
+    s->avctx     = avctx;
+    s->buf       = s->buf_start = avpkt->data;
+    s->buf_end   = s->buf_start + avpkt->size;
+    s->curtileno = 0; // TODO: only one tile in DCI JP2K. to implement for more tiles
+
+    // reduction factor, i.e number of resolution levels to skip
+    s->reduction_factor = s->lowres;
+
+    ff_jpeg2000_init_tier1_luts();
+
+    if (s->buf_end - s->buf < 2)
+        return AVERROR(EINVAL);
+
+    // check if the image is in jp2 format
+    if ((AV_RB32(s->buf) == 12) &&
+        (AV_RB32(s->buf + 4) == JP2_SIG_TYPE) &&
+        (AV_RB32(s->buf + 8) == JP2_SIG_VALUE)) {
+        if (!jp2_find_codestream(s)) {
+            av_log(avctx, AV_LOG_ERROR,
+                   "couldn't find jpeg2k codestream atom\n");
+            return -1;
+        }
+    }
+
+    if (bytestream_get_be16(&s->buf) != JPEG2000_SOC) {
+        av_log(avctx, AV_LOG_ERROR, "SOC marker not present\n");
+        return -1;
+    }
+    if (ret = jpeg2000_read_main_headers(s))
+        return ret;
+
+    /* get picture buffer */
+    if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) {
+        av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed.\n");
+        return ret;
+    }
+    picture->pict_type = AV_PICTURE_TYPE_I;
+    picture->key_frame = 1;
+
+    if (ret = jpeg2000_read_bitstream_packets(s))
+        return ret;
+    for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++)
+        if (ret = jpeg2000_decode_tile(s, s->tile + tileno, picture))
+            return ret;
+    jpeg2000_dec_cleanup(s);
+
+    *got_frame = 1;
+
+    return s->buf - s->buf_start;
+}
+
+#define OFFSET(x) offsetof(Jpeg2000DecoderContext, x)
+#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
+
+static const AVOption options[] = {
+    { "lowres",  "Lower the decoding resolution by a power of two",
+        OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, JPEG2000_MAX_RESLEVELS - 1, VD },
+    { NULL },
+};
+
+static const AVProfile profiles[] = {
+    { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_0,  "JPEG 2000 codestream restriction 0"   },
+    { FF_PROFILE_JPEG2000_CSTREAM_RESTRICTION_1,  "JPEG 2000 codestream restriction 1"   },
+    { FF_PROFILE_JPEG2000_CSTREAM_NO_RESTRICTION, "JPEG 2000 no codestream restrictions" },
+    { FF_PROFILE_JPEG2000_DCINEMA_2K,             "JPEG 2000 digital cinema 2K"          },
+    { FF_PROFILE_JPEG2000_DCINEMA_4K,             "JPEG 2000 digital cinema 4K"          },
+    { FF_PROFILE_UNKNOWN },
+};
+
+static const AVClass class = {
+    .class_name = "jpeg2000",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+AVCodec ff_jpeg2000_decoder = {
+    .name           = "jpeg2000",
+    .long_name      = NULL_IF_CONFIG_SMALL("JPEG 2000"),
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_JPEG2000,
+    .capabilities   = CODEC_CAP_FRAME_THREADS,
+    .priv_data_size = sizeof(Jpeg2000DecoderContext),
+    .decode         = jpeg2000_decode_frame,
+    .priv_class     = &class,
+    .pix_fmts       = (enum PixelFormat[]) { AV_PIX_FMT_XYZ12,
+                                             AV_PIX_FMT_GRAY8,
+                                             -1 },
+    .profiles       = NULL_IF_CONFIG_SMALL(profiles)
+};
diff --git a/libavcodec/jpeg2000dwt.c b/libavcodec/jpeg2000dwt.c
new file mode 100644
index 0000000..080b0a0
--- /dev/null
+++ b/libavcodec/jpeg2000dwt.c
@@ -0,0 +1,371 @@
+/*
+ * Discrete wavelet transform
+ * Copyright (c) 2007 Kamil Nowosad
+ * Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.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
+ */
+
+/**
+ * @file
+ * Discrete wavelet transform
+ */
+
+#include "libavutil/common.h"
+#include "libavutil/mem.h"
+#include "jpeg2000dwt.h"
+#include "internal.h"
+
+/* Defines for 9/7 DWT lifting parameters.
+ * Parameters are in float. */
+#define F_LFTG_ALPHA  1.586134342059924f
+#define F_LFTG_BETA   0.052980118572961f
+#define F_LFTG_GAMMA  0.882911075530934f
+#define F_LFTG_DELTA  0.443506852043971f
+#define F_LFTG_K      1.230174104914001f
+#define F_LFTG_X      1.625732422f
+/* FIXME: Why use 1.625732422 instead of 1/F_LFTG_K?
+ * Incorrect value in JPEG2000 norm.
+ * see (ISO/IEC 15444:1 (version 2002) F.3.8.2 */
+
+/* Lifting parameters in integer format.
+ * Computed as param = (float param) * (1 << 16) */
+#define I_LFTG_ALPHA  103949
+#define I_LFTG_BETA     3472
+#define I_LFTG_GAMMA   57862
+#define I_LFTG_DELTA   29066
+#define I_LFTG_K       80621
+#define I_LFTG_X      106544
+
+
+static inline void extend53(int *p, int i0, int i1)
+{
+    p[i0 - 1] = p[i0 + 1];
+    p[i1]     = p[i1 - 2];
+    p[i0 - 2] = p[i0 + 2];
+    p[i1 + 1] = p[i1 - 3];
+}
+
+static inline void extend97_float(float *p, int i0, int i1)
+{
+    int i;
+
+    for (i = 1; i <= 4; i++) {
+        p[i0 - i]     = p[i0 + i];
+        p[i1 + i - 1] = p[i1 - i - 1];
+    }
+}
+
+static inline void extend97_int(int32_t *p, int i0, int i1)
+{
+    int i;
+
+    for (i = 1; i <= 4; i++) {
+        p[i0 - i]     = p[i0 + i];
+        p[i1 + i - 1] = p[i1 - i - 1];
+    }
+}
+
+static void sr_1d53(int *p, int i0, int i1)
+{
+    int i;
+
+    if (i1 == i0 + 1)
+        return;
+
+    extend53(p, i0, i1);
+
+    for (i = i0 / 2; i < i1 / 2 + 1; i++)
+        p[2 * i] -= (p[2 * i - 1] + p[2 * i + 1] + 2) >> 2;
+    for (i = i0 / 2; i < i1 / 2; i++)
+        p[2 * i + 1] += (p[2 * i] + p[2 * i + 2]) >> 1;
+}
+
+static void dwt_decode53(DWTContext *s, int *t)
+{
+    int lev;
+    int w     = s->linelen[s->ndeclevels - 1][0];
+    int32_t *line = s->i_linebuf;
+    line += 3;
+
+    for (lev = 0; lev < s->ndeclevels; lev++) {
+        int lh = s->linelen[lev][0],
+            lv = s->linelen[lev][1],
+            mh = s->mod[lev][0],
+            mv = s->mod[lev][1],
+            lp;
+        int *l;
+
+        // HOR_SD
+        l = line + mh;
+        for (lp = 0; lp < lv; lp++) {
+            int i, j = 0;
+            // copy with interleaving
+            for (i = mh; i < lh; i += 2, j++)
+                l[i] = t[w * lp + j];
+            for (i = 1 - mh; i < lh; i += 2, j++)
+                l[i] = t[w * lp + j];
+
+            sr_1d53(line, mh, mh + lh);
+
+            for (i = 0; i < lh; i++)
+                t[w * lp + i] = l[i];
+        }
+
+        // VER_SD
+        l = line + mv;
+        for (lp = 0; lp < lh; lp++) {
+            int i, j = 0;
+            // copy with interleaving
+            for (i = mv; i < lv; i += 2, j++)
+                l[i] = t[w * j + lp];
+            for (i = 1 - mv; i < lv; i += 2, j++)
+                l[i] = t[w * j + lp];
+
+            sr_1d53(line, mv, mv + lv);
+
+            for (i = 0; i < lv; i++)
+                t[w * i + lp] = l[i];
+        }
+    }
+}
+
+static void sr_1d97_float(float *p, int i0, int i1)
+{
+    int i;
+
+    if (i1 == i0 + 1)
+        return;
+
+    extend97_float(p, i0, i1);
+
+    /*step 1*/
+    for (i = i0 / 2 - 1; i < i1 / 2 + 2; i++)
+        p[2 * i]     *= F_LFTG_K;
+    /* step 2*/
+    for (i = i0 / 2 - 2; i < i1 / 2 + 2; i++)
+        p[2 * i + 1] *= F_LFTG_X;
+    /* step 3*/
+    for (i = i0 / 2 - 1; i < i1 / 2 + 2; i++)
+        p[2 * i]     -= F_LFTG_DELTA * (p[2 * i - 1] + p[2 * i + 1]);
+    /* step 4 */
+    for (i = i0 / 2 - 1; i < i1 / 2 + 1; i++)
+        p[2 * i + 1] -= F_LFTG_GAMMA * (p[2 * i]     + p[2 * i + 2]);
+    /*step 5*/
+    for (i = i0 / 2; i < i1 / 2 + 1; i++)
+        p[2 * i]     += F_LFTG_BETA  * (p[2 * i - 1] + p[2 * i + 1]);
+    /* step 6 */
+    for (i = i0 / 2; i < i1 / 2; i++)
+        p[2 * i + 1] += F_LFTG_ALPHA * (p[2 * i]     + p[2 * i + 2]);
+}
+
+static void dwt_decode97_float(DWTContext *s, float *t)
+{
+    int lev;
+    int w       = s->linelen[s->ndeclevels - 1][0];
+    float *line = s->f_linebuf;
+    float *data = t;
+    /* position at index O of line range [0-5,w+5] cf. extend function */
+    line += 5;
+
+    for (lev = 0; lev < s->ndeclevels; lev++) {
+        int lh = s->linelen[lev][0],
+            lv = s->linelen[lev][1],
+            mh = s->mod[lev][0],
+            mv = s->mod[lev][1],
+            lp;
+        float *l;
+        // HOR_SD
+        l = line + mh;
+        for (lp = 0; lp < lv; lp++) {
+            int i, j = 0;
+            // copy with interleaving
+            for (i = mh; i < lh; i += 2, j++)
+                l[i] = data[w * lp + j];
+            for (i = 1 - mh; i < lh; i += 2, j++)
+                l[i] = data[w * lp + j];
+
+            sr_1d97_float(line, mh, mh + lh);
+
+            for (i = 0; i < lh; i++)
+                data[w * lp + i] = l[i];
+        }
+
+        // VER_SD
+        l = line + mv;
+        for (lp = 0; lp < lh; lp++) {
+            int i, j = 0;
+            // copy with interleaving
+            for (i = mv; i < lv; i += 2, j++)
+                l[i] = data[w * j + lp];
+            for (i = 1 - mv; i < lv; i += 2, j++)
+                l[i] = data[w * j + lp];
+
+            sr_1d97_float(line, mv, mv + lv);
+
+            for (i = 0; i < lv; i++)
+                data[w * i + lp] = l[i];
+        }
+    }
+}
+
+static void sr_1d97_int(int32_t *p, int i0, int i1)
+{
+    int i;
+
+    if (i1 == i0 + 1)
+        return;
+
+    extend97_int(p, i0, i1);
+
+    /*step 1*/
+    for (i = i0 / 2 - 1; i < i1 / 2 + 2; i++)
+        p[2 * i]      = ((p[2 * i] * I_LFTG_K)     + (1 << 15)) >> 16;
+    /* step 2*/
+    for (i = i0 / 2 - 2; i < i1 / 2 + 2; i++)
+        p[2 * i + 1]  = ((p[2 * i + 1] * I_LFTG_X) + (1 << 15)) >> 16;
+    /* step 3*/
+    for (i = i0 / 2 - 1; i < i1 / 2 + 2; i++)
+        p[2 * i]     -= (I_LFTG_DELTA * (p[2 * i - 1] + p[2 * i + 1]) + (1 << 15)) >> 16;
+    /* step 4 */
+    for (i = i0 / 2 - 1; i < i1 / 2 + 1; i++)
+        p[2 * i + 1] -= (I_LFTG_GAMMA * (p[2 * i]     + p[2 * i + 2]) + (1 << 15)) >> 16;
+    /*step 5*/
+    for (i = i0 / 2; i < i1 / 2 + 1; i++)
+        p[2 * i]     += (I_LFTG_BETA  * (p[2 * i - 1] + p[2 * i + 1]) + (1 << 15)) >> 16;
+    /* step 6 */
+    for (i = i0 / 2; i < i1 / 2; i++)
+        p[2 * i + 1] += (I_LFTG_ALPHA * (p[2 * i]     + p[2 * i + 2]) + (1 << 15)) >> 16;
+}
+
+static void dwt_decode97_int(DWTContext *s, int32_t *t)
+{
+    int lev;
+    int w       = s->linelen[s->ndeclevels - 1][0];
+    int32_t *line = s->i_linebuf;
+    int32_t *data = t;
+    /* position at index O of line range [0-5,w+5] cf. extend function */
+    line += 5;
+
+    for (lev = 0; lev < s->ndeclevels; lev++) {
+        int lh = s->linelen[lev][0],
+            lv = s->linelen[lev][1],
+            mh = s->mod[lev][0],
+            mv = s->mod[lev][1],
+            lp;
+        int32_t *l;
+        // HOR_SD
+        l = line + mh;
+        for (lp = 0; lp < lv; lp++) {
+            int i, j = 0;
+            // copy with interleaving
+            for (i = mh; i < lh; i += 2, j++)
+                l[i] = data[w * lp + j];
+            for (i = 1 - mh; i < lh; i += 2, j++)
+                l[i] = data[w * lp + j];
+
+            sr_1d97_int(line, mh, mh + lh);
+
+            for (i = 0; i < lh; i++)
+                data[w * lp + i] = l[i];
+        }
+
+        // VER_SD
+        l = line + mv;
+        for (lp = 0; lp < lh; lp++) {
+            int i, j = 0;
+            // copy with interleaving
+            for (i = mv; i < lv; i += 2, j++)
+                l[i] = data[w * j + lp];
+            for (i = 1 - mv; i < lv; i += 2, j++)
+                l[i] = data[w * j + lp];
+
+            sr_1d97_int(line, mv, mv + lv);
+
+            for (i = 0; i < lv; i++)
+                data[w * i + lp] = l[i];
+        }
+    }
+}
+
+int ff_jpeg2000_dwt_init(DWTContext *s, uint16_t border[2][2],
+                         int decomp_levels, int type)
+{
+    int i, j, lev = decomp_levels, maxlen,
+        b[2][2];
+
+    s->ndeclevels = decomp_levels;
+    s->type       = type;
+
+    for (i = 0; i < 2; i++)
+        for (j = 0; j < 2; j++)
+            b[i][j] = border[i][j];
+
+    maxlen = FFMAX(b[0][1] - b[0][0],
+                   b[1][1] - b[1][0]);
+    while (--lev >= 0)
+        for (i = 0; i < 2; i++) {
+            s->linelen[lev][i] = b[i][1] - b[i][0];
+            s->mod[lev][i]     = b[i][0] & 1;
+            for (j = 0; j < 2; j++)
+                b[i][j] = (b[i][j] + 1) >> 1;
+        }
+    switch (type) {
+    case FF_DWT97:
+        s->f_linebuf = av_malloc((maxlen + 12) * sizeof(*s->f_linebuf));
+        if (!s->f_linebuf)
+            return AVERROR(ENOMEM);
+        break;
+     case FF_DWT97_INT:
+        s->i_linebuf = av_malloc((maxlen + 12) * sizeof(*s->i_linebuf));
+        if (!s->i_linebuf)
+            return AVERROR(ENOMEM);
+        break;
+    case FF_DWT53:
+        s->i_linebuf = av_malloc((maxlen +  6) * sizeof(*s->i_linebuf));
+        if (!s->i_linebuf)
+            return AVERROR(ENOMEM);
+        break;
+    default:
+        return -1;
+    }
+    return 0;
+}
+
+int ff_dwt_decode(DWTContext *s, void *t)
+{
+    switch (s->type) {
+    case FF_DWT97:
+        dwt_decode97_float(s, t);
+        break;
+    case FF_DWT97_INT:
+        dwt_decode97_int(s, t);
+        break;
+    case FF_DWT53:
+        dwt_decode53(s, t);
+        break;
+    default:
+        return -1;
+    }
+    return 0;
+}
+
+void ff_dwt_destroy(DWTContext *s)
+{
+    av_freep(&s->f_linebuf);
+    av_freep(&s->i_linebuf);
+}
diff --git a/libavcodec/jpeg2000dwt.h b/libavcodec/jpeg2000dwt.h
new file mode 100644
index 0000000..2abfd15
--- /dev/null
+++ b/libavcodec/jpeg2000dwt.h
@@ -0,0 +1,64 @@
+/*
+ * Discrete wavelet transform
+ * Copyright (c) 2007 Kamil Nowosad
+ *
+ * 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 AVCODEC_JPEG2000DWT_H
+#define AVCODEC_JPEG2000DWT_H
+
+/**
+ * @file
+ * Discrete wavelet transform
+ */
+
+#include <stdint.h>
+
+#define FF_DWT_MAX_DECLVLS 32 ///< max number of decomposition levels
+
+enum DWTType {
+    FF_DWT97,
+    FF_DWT53,
+    FF_DWT97_INT
+};
+
+typedef struct DWTContext {
+    /// line lengths { horizontal, vertical } in consecutive decomposition levels
+    uint16_t linelen[FF_DWT_MAX_DECLVLS][2];
+    uint8_t mod[FF_DWT_MAX_DECLVLS][2];  ///< coordinates (x0, y0) of decomp. levels mod 2
+    uint8_t ndeclevels;                  ///< number of decomposition levels
+    uint8_t type;                        ///< 0 for 9/7; 1 for 5/3
+    int32_t *i_linebuf;                  ///< int buffer used by transform
+    float   *f_linebuf;                  ///< float buffer used by transform
+} DWTContext;
+
+/**
+ * Initialize DWT.
+ * @param s                 DWT context
+ * @param border            coordinates of transformed region {{x0, x1}, {y0, y1}}
+ * @param decomp_levels     number of decomposition levels
+ * @param type              0 for DWT 9/7; 1 for DWT 5/3
+ */
+int ff_jpeg2000_dwt_init(DWTContext *s, uint16_t border[2][2],
+                         int decomp_levels, int type);
+
+int ff_dwt_decode(DWTContext *s, void *t);
+
+void ff_dwt_destroy(DWTContext *s);
+
+#endif /* AVCODEC_JPEG2000DWT_H */
diff --git a/libavcodec/jvdec.c b/libavcodec/jvdec.c
index 53d8285..18cc73f 100644
--- a/libavcodec/jvdec.c
+++ b/libavcodec/jvdec.c
@@ -28,6 +28,7 @@
 #include "avcodec.h"
 #include "dsputil.h"
 #include "get_bits.h"
+#include "internal.h"
 #include "libavutil/intreadwrite.h"
 
 typedef struct JvContext {
@@ -42,6 +43,7 @@
     JvContext *s = avctx->priv_data;
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
     ff_dsputil_init(&s->dsp, avctx);
+    avcodec_get_frame_defaults(&s->frame);
     return 0;
 }
 
@@ -135,7 +137,7 @@
     JvContext *s           = avctx->priv_data;
     const uint8_t *buf     = avpkt->data;
     const uint8_t *buf_end = buf + avpkt->size;
-    int video_size, video_type, ret, i, j;
+    int video_size, video_type, i, j, ret;
 
     if (avpkt->size < 6)
         return AVERROR_INVALIDDATA;
@@ -149,10 +151,8 @@
             av_log(avctx, AV_LOG_ERROR, "video size %d invalid\n", video_size);
             return AVERROR_INVALIDDATA;
         }
-        if ((ret = avctx->reget_buffer(avctx, &s->frame)) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0)
             return ret;
-        }
 
         if (video_type == 0 || video_type == 1) {
             GetBitContext gb;
@@ -190,8 +190,9 @@
         s->palette_has_changed       = 0;
         memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
 
+        if ((ret = av_frame_ref(data, &s->frame)) < 0)
+            return ret;
         *got_frame = 1;
-        *(AVFrame*)data = s->frame;
     }
 
     return avpkt->size;
@@ -201,8 +202,7 @@
 {
     JvContext *s = avctx->priv_data;
 
-    if(s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+    av_frame_unref(&s->frame);
 
     return 0;
 }
diff --git a/libavcodec/kgv1dec.c b/libavcodec/kgv1dec.c
index 008843c..9c48239 100644
--- a/libavcodec/kgv1dec.c
+++ b/libavcodec/kgv1dec.c
@@ -32,25 +32,25 @@
 
 typedef struct {
     AVCodecContext *avctx;
-    AVFrame prev, cur;
+    AVFrame prev;
 } KgvContext;
 
 static void decode_flush(AVCodecContext *avctx)
 {
     KgvContext * const c = avctx->priv_data;
 
-    if (c->prev.data[0])
-        avctx->release_buffer(avctx, &c->prev);
+    av_frame_unref(&c->prev);
 }
 
 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
                         AVPacket *avpkt)
 {
+    AVFrame *frame = data;
     const uint8_t *buf = avpkt->data;
     const uint8_t *buf_end = buf + avpkt->size;
     KgvContext * const c = avctx->priv_data;
     int offsets[8];
-    uint16_t *out, *prev;
+    uint8_t *out, *prev;
     int outcnt = 0, maxcnt;
     int w, h, i, res;
 
@@ -65,19 +65,17 @@
         return res;
 
     if (w != avctx->width || h != avctx->height) {
-        if (c->prev.data[0])
-            avctx->release_buffer(avctx, &c->prev);
+        av_frame_unref(&c->prev);
         avcodec_set_dimensions(avctx, w, h);
     }
 
     maxcnt = w * h;
 
-    c->cur.reference = 3;
-    if ((res = ff_get_buffer(avctx, &c->cur)) < 0)
+    if ((res = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
         return res;
-    out  = (uint16_t *) c->cur.data[0];
+    out  = frame->data[0];
     if (c->prev.data[0]) {
-        prev = (uint16_t *) c->prev.data[0];
+        prev = c->prev.data[0];
     } else {
         prev = NULL;
     }
@@ -90,11 +88,10 @@
         buf += 2;
 
         if (!(code & 0x8000)) {
-            out[outcnt++] = code; // rgb555 pixel coded directly
+            AV_WN16A(&out[2 * outcnt], code); // rgb555 pixel coded directly
+            outcnt++;
         } else {
             int count;
-            int inp_off;
-            uint16_t *inp;
 
             if ((code & 0x6000) == 0x6000) {
                 // copy from previous frame
@@ -112,7 +109,7 @@
 
                 start = (outcnt + offsets[oidx]) % maxcnt;
 
-                if (maxcnt - start < count)
+                if (maxcnt - start < count || maxcnt - outcnt < count)
                     break;
 
                 if (!prev) {
@@ -121,8 +118,7 @@
                     break;
                 }
 
-                inp = prev;
-                inp_off = start;
+                memcpy(out + 2 * outcnt, prev + 2 * start, 2 * count);
             } else {
                 // copy from earlier in this frame
                 int offset = (code & 0x1FFF) + 1;
@@ -137,31 +133,23 @@
                     count = 4 + *buf++;
                 }
 
-                if (outcnt < offset)
+                if (outcnt < offset || maxcnt - outcnt < count)
                     break;
 
-                inp = out;
-                inp_off = outcnt - offset;
+                av_memcpy_backptr(out + 2 * outcnt, 2 * offset, 2 * count);
             }
-
-            if (maxcnt - outcnt < count)
-                break;
-
-            for (i = inp_off; i < count + inp_off; i++) {
-                out[outcnt++] = inp[i];
-            }
+            outcnt += count;
         }
     }
 
     if (outcnt - maxcnt)
         av_log(avctx, AV_LOG_DEBUG, "frame finished with %d diff\n", outcnt - maxcnt);
 
-    *got_frame = 1;
-    *(AVFrame*)data = c->cur;
+    av_frame_unref(&c->prev);
+    if ((res = av_frame_ref(&c->prev, frame)) < 0)
+        return res;
 
-    if (c->prev.data[0])
-        avctx->release_buffer(avctx, &c->prev);
-    FFSWAP(AVFrame, c->cur, c->prev);
+    *got_frame = 1;
 
     return avpkt->size;
 }
diff --git a/libavcodec/kmvc.c b/libavcodec/kmvc.c
index ffef771..20aae67 100644
--- a/libavcodec/kmvc.c
+++ b/libavcodec/kmvc.c
@@ -41,7 +41,6 @@
  */
 typedef struct KmvcContext {
     AVCodecContext *avctx;
-    AVFrame pic;
 
     int setpal;
     int palsize;
@@ -263,6 +262,7 @@
                         AVPacket *avpkt)
 {
     KmvcContext *const ctx = avctx->priv_data;
+    AVFrame *frame = data;
     uint8_t *out, *src;
     int i, ret;
     int header;
@@ -270,15 +270,9 @@
     const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
 
     bytestream2_init(&ctx->g, avpkt->data, avpkt->size);
-    if (ctx->pic.data[0])
-        avctx->release_buffer(avctx, &ctx->pic);
 
-    ctx->pic.reference = 3;
-    ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
-    if ((ret = ff_get_buffer(avctx, &ctx->pic)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     header = bytestream2_get_byte(&ctx->g);
 
@@ -293,15 +287,15 @@
     }
 
     if (header & KMVC_KEYFRAME) {
-        ctx->pic.key_frame = 1;
-        ctx->pic.pict_type = AV_PICTURE_TYPE_I;
+        frame->key_frame = 1;
+        frame->pict_type = AV_PICTURE_TYPE_I;
     } else {
-        ctx->pic.key_frame = 0;
-        ctx->pic.pict_type = AV_PICTURE_TYPE_P;
+        frame->key_frame = 0;
+        frame->pict_type = AV_PICTURE_TYPE_P;
     }
 
     if (header & KMVC_PALETTE) {
-        ctx->pic.palette_has_changed = 1;
+        frame->palette_has_changed = 1;
         // palette starts from index 1 and has 127 entries
         for (i = 1; i <= ctx->palsize; i++) {
             ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24(&ctx->g);
@@ -309,17 +303,17 @@
     }
 
     if (pal) {
-        ctx->pic.palette_has_changed = 1;
+        frame->palette_has_changed = 1;
         memcpy(ctx->pal, pal, AVPALETTE_SIZE);
     }
 
     if (ctx->setpal) {
         ctx->setpal = 0;
-        ctx->pic.palette_has_changed = 1;
+        frame->palette_has_changed = 1;
     }
 
     /* make the palette available on the way out */
-    memcpy(ctx->pic.data[1], ctx->pal, 1024);
+    memcpy(frame->data[1], ctx->pal, 1024);
 
     blocksize = bytestream2_get_byte(&ctx->g);
 
@@ -344,12 +338,12 @@
         return AVERROR_INVALIDDATA;
     }
 
-    out = ctx->pic.data[0];
+    out = frame->data[0];
     src = ctx->cur;
     for (i = 0; i < avctx->height; i++) {
         memcpy(out, src, avctx->width);
         src += 320;
-        out += ctx->pic.linesize[0];
+        out += frame->linesize[0];
     }
 
     /* flip buffers */
@@ -362,7 +356,6 @@
     }
 
     *got_frame = 1;
-    *(AVFrame *) data = ctx->pic;
 
     /* always report that the buffer was completely consumed */
     return avpkt->size;
@@ -416,7 +409,6 @@
         c->setpal = 1;
     }
 
-    avcodec_get_frame_defaults(&c->pic);
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
 
     return 0;
@@ -433,8 +425,6 @@
 
     av_freep(&c->frm0);
     av_freep(&c->frm1);
-    if (c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
 
     return 0;
 }
diff --git a/libavcodec/lagarith.c b/libavcodec/lagarith.c
index eb8a77e..8599784 100644
--- a/libavcodec/lagarith.c
+++ b/libavcodec/lagarith.c
@@ -48,7 +48,6 @@
 
 typedef struct LagarithContext {
     AVCodecContext *avctx;
-    AVFrame picture;
     DSPContext dsp;
     int zeros;                  /**< number of consecutive zero bytes encountered */
     int zeros_rem;              /**< number of zero bytes remaining to output */
@@ -512,7 +511,8 @@
     const uint8_t *buf = avpkt->data;
     unsigned int buf_size = avpkt->size;
     LagarithContext *l = avctx->priv_data;
-    AVFrame *const p = &l->picture;
+    ThreadFrame frame = { .f = data };
+    AVFrame *const p  = data;
     uint8_t frametype = 0;
     uint32_t offset_gu = 0, offset_bv = 0, offset_ry = 9;
     uint32_t offs[4];
@@ -520,12 +520,6 @@
     int i, j, planes = 3;
     int ret;
 
-    AVFrame *picture = data;
-
-    if (p->data[0])
-        ff_thread_release_buffer(avctx, p);
-
-    p->reference = 0;
     p->key_frame = 1;
 
     frametype = buf[0];
@@ -545,10 +539,8 @@
                 planes = 4;
             }
 
-        if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
             return ret;
-        }
 
         dst = p->data[0];
         if (frametype == FRAME_SOLID_RGBA) {
@@ -574,10 +566,8 @@
         if (frametype == FRAME_ARITH_RGB24 || frametype == FRAME_U_RGB24)
             avctx->pix_fmt = AV_PIX_FMT_RGB24;
 
-        if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
             return ret;
-        }
 
         offs[0] = offset_bv;
         offs[1] = offset_gu;
@@ -633,10 +623,8 @@
     case FRAME_ARITH_YUY2:
         avctx->pix_fmt = AV_PIX_FMT_YUV422P;
 
-        if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
             return ret;
-        }
 
         if (offset_ry >= buf_size ||
             offset_gu >= buf_size ||
@@ -659,10 +647,8 @@
     case FRAME_ARITH_YV12:
         avctx->pix_fmt = AV_PIX_FMT_YUV420P;
 
-        if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
             return ret;
-        }
         if (buf_size <= offset_ry || buf_size <= offset_gu || buf_size <= offset_bv) {
             return AVERROR_INVALIDDATA;
         }
@@ -691,7 +677,6 @@
         return AVERROR_PATCHWELCOME;
     }
 
-    *picture = *p;
     *got_frame = 1;
 
     return buf_size;
@@ -711,8 +696,6 @@
 {
     LagarithContext *l = avctx->priv_data;
 
-    if (l->picture.data[0])
-        ff_thread_release_buffer(avctx, &l->picture);
     av_freep(&l->rgb_planes);
 
     return 0;
diff --git a/libavcodec/lcldec.c b/libavcodec/lcldec.c
index f8d45da..7948199 100644
--- a/libavcodec/lcldec.c
+++ b/libavcodec/lcldec.c
@@ -55,8 +55,6 @@
  * Decoder context
  */
 typedef struct LclDecContext {
-    AVFrame pic;
-
     // Image type
     int imgtype;
     // Compression type
@@ -166,6 +164,7 @@
  */
 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
 {
+    AVFrame *frame = data;
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     LclDecContext * const c = avctx->priv_data;
@@ -182,17 +181,10 @@
     unsigned int mthread_inlen, mthread_outlen;
     unsigned int len = buf_size;
 
-    if(c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
-
-    c->pic.reference = 0;
-    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
-    if ((ret = ff_get_buffer(avctx, &c->pic)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
-    outptr = c->pic.data[0]; // Output image pointer
+    outptr = frame->data[0]; // Output image pointer
 
     /* Decompress frame */
     switch (avctx->codec_id) {
@@ -386,9 +378,9 @@
     }
 
     /* Convert colorspace */
-    y_out = c->pic.data[0] + (height - 1) * c->pic.linesize[0];
-    u_out = c->pic.data[1] + (height - 1) * c->pic.linesize[1];
-    v_out = c->pic.data[2] + (height - 1) * c->pic.linesize[2];
+    y_out = frame->data[0] + (height - 1) * frame->linesize[0];
+    u_out = frame->data[1] + (height - 1) * frame->linesize[1];
+    v_out = frame->data[2] + (height - 1) * frame->linesize[2];
     switch (c->imgtype) {
     case IMGTYPE_YUV111:
         for (row = 0; row < height; row++) {
@@ -397,9 +389,9 @@
                 u_out[col] = *encoded++ + 128;
                 v_out[col] = *encoded++ + 128;
             }
-            y_out -= c->pic.linesize[0];
-            u_out -= c->pic.linesize[1];
-            v_out -= c->pic.linesize[2];
+            y_out -= frame->linesize[0];
+            u_out -= frame->linesize[1];
+            v_out -= frame->linesize[2];
         }
         break;
     case IMGTYPE_YUV422:
@@ -412,14 +404,14 @@
                 v_out[ col >> 1     ] = *encoded++ + 128;
                 v_out[(col >> 1) + 1] = *encoded++ + 128;
             }
-            y_out -= c->pic.linesize[0];
-            u_out -= c->pic.linesize[1];
-            v_out -= c->pic.linesize[2];
+            y_out -= frame->linesize[0];
+            u_out -= frame->linesize[1];
+            v_out -= frame->linesize[2];
         }
         break;
     case IMGTYPE_RGB24:
         for (row = height - 1; row >= 0; row--) {
-            pixel_ptr = row * c->pic.linesize[0];
+            pixel_ptr = row * frame->linesize[0];
             memcpy(outptr + pixel_ptr, encoded, 3 * width);
             encoded += 3 * width;
         }
@@ -432,9 +424,9 @@
                 u_out[col >> 2] = *encoded++ + 128;
                 v_out[col >> 2] = *encoded++ + 128;
             }
-            y_out -= c->pic.linesize[0];
-            u_out -= c->pic.linesize[1];
-            v_out -= c->pic.linesize[2];
+            y_out -= frame->linesize[0];
+            u_out -= frame->linesize[1];
+            v_out -= frame->linesize[2];
         }
         break;
     case IMGTYPE_YUV211:
@@ -445,26 +437,26 @@
                 u_out[col >> 1] = *encoded++ + 128;
                 v_out[col >> 1] = *encoded++ + 128;
             }
-            y_out -= c->pic.linesize[0];
-            u_out -= c->pic.linesize[1];
-            v_out -= c->pic.linesize[2];
+            y_out -= frame->linesize[0];
+            u_out -= frame->linesize[1];
+            v_out -= frame->linesize[2];
         }
         break;
     case IMGTYPE_YUV420:
-        u_out = c->pic.data[1] + ((height >> 1) - 1) * c->pic.linesize[1];
-        v_out = c->pic.data[2] + ((height >> 1) - 1) * c->pic.linesize[2];
+        u_out = frame->data[1] + ((height >> 1) - 1) * frame->linesize[1];
+        v_out = frame->data[2] + ((height >> 1) - 1) * frame->linesize[2];
         for (row = 0; row < height - 1; row += 2) {
             for (col = 0; col < width - 1; col += 2) {
                 memcpy(y_out + col, encoded, 2);
                 encoded += 2;
-                memcpy(y_out + col - c->pic.linesize[0], encoded, 2);
+                memcpy(y_out + col - frame->linesize[0], encoded, 2);
                 encoded += 2;
                 u_out[col >> 1] = *encoded++ + 128;
                 v_out[col >> 1] = *encoded++ + 128;
             }
-            y_out -= c->pic.linesize[0] << 1;
-            u_out -= c->pic.linesize[1];
-            v_out -= c->pic.linesize[2];
+            y_out -= frame->linesize[0] << 1;
+            u_out -= frame->linesize[1];
+            v_out -= frame->linesize[2];
         }
         break;
     default:
@@ -473,7 +465,6 @@
     }
 
     *got_frame = 1;
-    *(AVFrame*)data = c->pic;
 
     /* always report that the buffer was completely consumed */
     return buf_size;
@@ -492,7 +483,6 @@
                                 FFALIGN(avctx->height, 4);
     unsigned int max_decomp_size;
 
-    avcodec_get_frame_defaults(&c->pic);
     if (avctx->extradata_size < 8) {
         av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
         return AVERROR_INVALIDDATA;
@@ -638,8 +628,6 @@
     LclDecContext * const c = avctx->priv_data;
 
     av_freep(&c->decomp_buf);
-    if (c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
 #if CONFIG_ZLIB_DECODER
     if (avctx->codec_id == AV_CODEC_ID_ZLIB)
         inflateEnd(&c->zstream);
diff --git a/libavcodec/libaacplus.c b/libavcodec/libaacplus.c
index e9e50f8..68850c5 100644
--- a/libavcodec/libaacplus.c
+++ b/libavcodec/libaacplus.c
@@ -66,7 +66,7 @@
     aacplus_cfg->bitRate = avctx->bit_rate;
     aacplus_cfg->bandWidth = avctx->cutoff;
     aacplus_cfg->outputFormat = !(avctx->flags & CODEC_FLAG_GLOBAL_HEADER);
-    aacplus_cfg->inputFormat = AACPLUS_INPUT_16BIT;
+    aacplus_cfg->inputFormat = avctx->sample_fmt == AV_SAMPLE_FMT_FLT ? AACPLUS_INPUT_FLOAT : AACPLUS_INPUT_16BIT;
     if (!aacplusEncSetConfiguration(s->aacplus_handle, aacplus_cfg)) {
         av_log(avctx, AV_LOG_ERROR, "libaacplus doesn't support this output format!\n");
         return -1;
@@ -74,11 +74,6 @@
 
     avctx->frame_size = s->samples_input / avctx->channels;
 
-#if FF_API_OLD_ENCODE_AUDIO
-    avctx->coded_frame= avcodec_alloc_frame();
-    avctx->coded_frame->key_frame= 1;
-#endif
-
     /* Set decoder specific info */
     avctx->extradata_size = 0;
     if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
@@ -104,7 +99,7 @@
     int32_t *input_buffer = (int32_t *)frame->data[0];
     int ret;
 
-    if ((ret = ff_alloc_packet2(avctx, pkt, s->max_output_bytes)))
+    if ((ret = ff_alloc_packet2(avctx, pkt, s->max_output_bytes)) < 0)
         return ret;
 
     pkt->size = aacplusEncEncode(s->aacplus_handle, input_buffer,
@@ -118,15 +113,17 @@
 {
     aacPlusAudioContext *s = avctx->priv_data;
 
-#if FF_API_OLD_ENCODE_AUDIO
-    av_freep(&avctx->coded_frame);
-#endif
     av_freep(&avctx->extradata);
 
     aacplusEncClose(s->aacplus_handle);
     return 0;
 }
 
+static const AVProfile profiles[] = {
+    { FF_PROFILE_AAC_LOW, "LC" },
+    { FF_PROFILE_UNKNOWN },
+};
+
 AVCodec ff_libaacplus_encoder = {
     .name           = "libaacplus",
     .type           = AVMEDIA_TYPE_AUDIO,
@@ -136,6 +133,11 @@
     .encode2        = aacPlus_encode_frame,
     .close          = aacPlus_encode_close,
     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
+                                                     AV_SAMPLE_FMT_FLT,
                                                      AV_SAMPLE_FMT_NONE },
     .long_name      = NULL_IF_CONFIG_SMALL("libaacplus AAC+ (Advanced Audio Codec with SBR+PS)"),
+    .profiles       = profiles,
+    .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
+                                            AV_CH_LAYOUT_STEREO,
+                                            0 },
 };
diff --git a/libavcodec/libcelt_dec.c b/libavcodec/libcelt_dec.c
index a66f9f5..53bbb72 100644
--- a/libavcodec/libcelt_dec.c
+++ b/libavcodec/libcelt_dec.c
@@ -111,11 +111,8 @@
     int16_t *pcm;
 
     frame->nb_samples = c->frame_size;
-    err = ff_get_buffer(c, frame);
-    if (err < 0) {
-        av_log(c, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((err = ff_get_buffer(c, frame, 0)) < 0)
         return err;
-    }
     pcm = (int16_t *)frame->data[0];
     err = celt_decode(celt->dec, pkt->data, pkt->size, pcm, c->frame_size);
     if (err < 0)
diff --git a/libavcodec/libfaac.c b/libavcodec/libfaac.c
index bf226af..ac18fe2 100644
--- a/libavcodec/libfaac.c
+++ b/libavcodec/libfaac.c
@@ -45,9 +45,6 @@
 {
     FaacAudioContext *s = avctx->priv_data;
 
-#if FF_API_OLD_ENCODE_AUDIO
-    av_freep(&avctx->coded_frame);
-#endif
     av_freep(&avctx->extradata);
     ff_af_queue_close(&s->afq);
 
@@ -132,14 +129,6 @@
 
     avctx->frame_size = samples_input / avctx->channels;
 
-#if FF_API_OLD_ENCODE_AUDIO
-    avctx->coded_frame= avcodec_alloc_frame();
-    if (!avctx->coded_frame) {
-        ret = AVERROR(ENOMEM);
-        goto error;
-    }
-#endif
-
     /* Set decoder specific info */
     avctx->extradata_size = 0;
     if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
@@ -184,10 +173,8 @@
     int num_samples  = frame ? frame->nb_samples : 0;
     void *samples    = frame ? frame->data[0]    : NULL;
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, (7 + 768) * avctx->channels))) {
-        av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
+    if ((ret = ff_alloc_packet2(avctx, avpkt, (7 + 768) * avctx->channels)) < 0)
         return ret;
-    }
 
     bytes_written = faacEncEncode(s->faac_handle, samples,
                                   num_samples * avctx->channels,
diff --git a/libavcodec/libfdk-aacenc.c b/libavcodec/libfdk-aacenc.c
index d76b215..e3992e1 100644
--- a/libavcodec/libfdk-aacenc.c
+++ b/libavcodec/libfdk-aacenc.c
@@ -97,9 +97,6 @@
 
     if (s->handle)
         aacEncClose(&s->handle);
-#if FF_API_OLD_ENCODE_AUDIO
-    av_freep(&avctx->coded_frame);
-#endif
     av_freep(&avctx->extradata);
     ff_af_queue_close(&s->afq);
 
@@ -275,13 +272,6 @@
         goto error;
     }
 
-#if FF_API_OLD_ENCODE_AUDIO
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame) {
-        ret = AVERROR(ENOMEM);
-        goto error;
-    }
-#endif
     avctx->frame_size = info.frameLength;
     avctx->delay      = info.encoderDelay;
     ff_af_queue_init(avctx, &s->afq);
@@ -339,7 +329,7 @@
     }
 
     /* The maximum packet size is 6144 bits aka 768 bytes per channel. */
-    if ((ret = ff_alloc_packet2(avctx, avpkt, FFMAX(8192, 768 * avctx->channels))))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, FFMAX(8192, 768 * avctx->channels))) < 0)
         return ret;
 
     out_ptr                   = avpkt->data;
diff --git a/libavcodec/libgsm.c b/libavcodec/libgsm.c
index 470cd61..8250a15 100644
--- a/libavcodec/libgsm.c
+++ b/libavcodec/libgsm.c
@@ -41,9 +41,6 @@
 #include "gsm.h"
 
 static av_cold int libgsm_encode_close(AVCodecContext *avctx) {
-#if FF_API_OLD_ENCODE_AUDIO
-    av_freep(&avctx->coded_frame);
-#endif
     gsm_destroy(avctx->priv_data);
     avctx->priv_data = NULL;
     return 0;
@@ -88,12 +85,6 @@
         }
     }
 
-#if FF_API_OLD_ENCODE_AUDIO
-    avctx->coded_frame= avcodec_alloc_frame();
-    if (!avctx->coded_frame)
-        goto error;
-#endif
-
     return 0;
 error:
     libgsm_encode_close(avctx);
@@ -107,7 +98,7 @@
     gsm_signal *samples = (gsm_signal *)frame->data[0];
     struct gsm_state *state = avctx->priv_data;
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, avctx->block_align)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, avctx->block_align)) < 0)
         return ret;
 
     switch(avctx->codec_id) {
@@ -207,10 +198,8 @@
 
     /* get output buffer */
     frame->nb_samples = avctx->frame_size;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     samples = (int16_t *)frame->data[0];
 
     for (i = 0; i < avctx->frame_size / GSM_FRAME_SIZE; i++) {
diff --git a/libavcodec/libilbc.c b/libavcodec/libilbc.c
index 56cb51f..b4163c6 100644
--- a/libavcodec/libilbc.c
+++ b/libavcodec/libilbc.c
@@ -93,10 +93,8 @@
     }
 
     frame->nb_samples = s->decoder.blockl;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     WebRtcIlbcfix_DecodeImpl((WebRtc_Word16*) frame->data[0],
                              (const WebRtc_UWord16*) buf, &s->decoder, 1);
@@ -159,33 +157,18 @@
 
     avctx->block_align = s->encoder.no_of_bytes;
     avctx->frame_size  = s->encoder.blockl;
-#if FF_API_OLD_ENCODE_AUDIO
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame)
-        return AVERROR(ENOMEM);
-#endif
 
     return 0;
 }
 
-static av_cold int ilbc_encode_close(AVCodecContext *avctx)
-{
-#if FF_API_OLD_ENCODE_AUDIO
-    av_freep(&avctx->coded_frame);
-#endif
-    return 0;
-}
-
 static int ilbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
                              const AVFrame *frame, int *got_packet_ptr)
 {
     ILBCEncContext *s = avctx->priv_data;
     int ret;
 
-    if ((ret = ff_alloc_packet(avpkt, 50))) {
-        av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
+    if ((ret = ff_alloc_packet2(avctx, avpkt, 50)) < 0)
         return ret;
-    }
 
     WebRtcIlbcfix_EncodeImpl((WebRtc_UWord16*) avpkt->data, (const WebRtc_Word16*) frame->data[0], &s->encoder);
 
@@ -206,7 +189,6 @@
     .priv_data_size = sizeof(ILBCEncContext),
     .init           = ilbc_encode_init,
     .encode2        = ilbc_encode_frame,
-    .close          = ilbc_encode_close,
     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
                                                      AV_SAMPLE_FMT_NONE },
     .long_name      = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
diff --git a/libavcodec/libmp3lame.c b/libavcodec/libmp3lame.c
index 034fee7..95771af 100644
--- a/libavcodec/libmp3lame.c
+++ b/libavcodec/libmp3lame.c
@@ -48,6 +48,7 @@
     int buffer_index;
     int buffer_size;
     int reservoir;
+    int joint_stereo;
     float *samples_flt[2];
     AudioFrameQueue afq;
     AVFloatDSPContext fdsp;
@@ -78,9 +79,6 @@
 {
     LAMEContext *s = avctx->priv_data;
 
-#if FF_API_OLD_ENCODE_AUDIO
-    av_freep(&avctx->coded_frame);
-#endif
     av_freep(&s->samples_flt[0]);
     av_freep(&s->samples_flt[1]);
     av_freep(&s->buffer);
@@ -104,7 +102,7 @@
 
 
     lame_set_num_channels(s->gfp, avctx->channels);
-    lame_set_mode(s->gfp, avctx->channels > 1 ? JOINT_STEREO : MONO);
+    lame_set_mode(s->gfp, avctx->channels > 1 ? s->joint_stereo ? JOINT_STEREO : STEREO : MONO);
 
     /* sample rate */
     lame_set_in_samplerate (s->gfp, avctx->sample_rate);
@@ -143,14 +141,6 @@
 
     avctx->frame_size  = lame_get_framesize(s->gfp);
 
-#if FF_API_OLD_ENCODE_AUDIO
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame) {
-        ret = AVERROR(ENOMEM);
-        goto error;
-    }
-#endif
-
     /* allocate float sample buffers */
     if (avctx->sample_fmt == AV_SAMPLE_FMT_FLTP) {
         int ch;
@@ -218,7 +208,7 @@
         }
     } else {
         lame_result = lame_encode_flush(s->gfp, s->buffer + s->buffer_index,
-                                        BUFFER_SIZE - s->buffer_index);
+                                        s->buffer_size - s->buffer_index);
     }
     if (lame_result < 0) {
         if (lame_result == -1) {
@@ -254,7 +244,7 @@
     av_dlog(avctx, "in:%d packet-len:%d index:%d\n", avctx->frame_size, len,
             s->buffer_index);
     if (len <= s->buffer_index) {
-        if ((ret = ff_alloc_packet2(avctx, avpkt, len)))
+        if ((ret = ff_alloc_packet2(avctx, avpkt, len)) < 0)
             return ret;
         memcpy(avpkt->data, s->buffer, len);
         s->buffer_index -= len;
@@ -274,6 +264,7 @@
 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
 static const AVOption options[] = {
     { "reservoir", "Use bit reservoir.", OFFSET(reservoir), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AE },
+    { "joint_stereo", "Use joint stereo.", OFFSET(joint_stereo), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AE },
     { NULL },
 };
 
diff --git a/libavcodec/libopencore-amr.c b/libavcodec/libopencore-amr.c
index 6877f4c..c991106 100644
--- a/libavcodec/libopencore-amr.c
+++ b/libavcodec/libopencore-amr.c
@@ -35,7 +35,7 @@
         avctx->sample_rate = 8000 * is_amr_wb;
 
     if (avctx->channels > 1) {
-        av_log_missing_feature(avctx, "multi-channel AMR", 0);
+        avpriv_report_missing_feature(avctx, "multi-channel AMR");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -104,10 +104,8 @@
 
     /* get output buffer */
     frame->nb_samples = 160;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     dec_mode    = (buf[0] >> 3) & 0x000F;
     packet_size = block_size[dec_mode] + 1;
@@ -203,11 +201,6 @@
     avctx->frame_size  = 160;
     avctx->delay       =  50;
     ff_af_queue_init(avctx, &s->afq);
-#if FF_API_OLD_ENCODE_AUDIO
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame)
-        return AVERROR(ENOMEM);
-#endif
 
     s->enc_state = Encoder_Interface_init(s->enc_dtx);
     if (!s->enc_state) {
@@ -228,9 +221,6 @@
 
     Encoder_Interface_exit(s->enc_state);
     ff_af_queue_close(&s->afq);
-#if FF_API_OLD_ENCODE_AUDIO
-    av_freep(&avctx->coded_frame);
-#endif
     return 0;
 }
 
@@ -247,7 +237,7 @@
         s->enc_bitrate = avctx->bit_rate;
     }
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, 32)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, 32)) < 0)
         return ret;
 
     if (frame) {
@@ -343,10 +333,8 @@
 
     /* get output buffer */
     frame->nb_samples = 320;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     mode        = (buf[0] >> 3) & 0x000F;
     packet_size = block_size[mode];
diff --git a/libavcodec/libopenjpegdec.c b/libavcodec/libopenjpegdec.c
index eeb3dd6..ec6e275 100644
--- a/libavcodec/libopenjpegdec.c
+++ b/libavcodec/libopenjpegdec.c
@@ -59,15 +59,19 @@
                             AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
                             AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
 
+#define XYZ_PIXEL_FORMATS  AV_PIX_FMT_XYZ12
+
 static const enum AVPixelFormat libopenjpeg_rgb_pix_fmts[]  = {RGB_PIXEL_FORMATS};
 static const enum AVPixelFormat libopenjpeg_gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
 static const enum AVPixelFormat libopenjpeg_yuv_pix_fmts[]  = {YUV_PIXEL_FORMATS};
-static const enum AVPixelFormat libopenjpeg_all_pix_fmts[]  = {RGB_PIXEL_FORMATS,GRAY_PIXEL_FORMATS,YUV_PIXEL_FORMATS};
+static const enum AVPixelFormat libopenjpeg_all_pix_fmts[]  = {RGB_PIXEL_FORMATS,
+                                                               GRAY_PIXEL_FORMATS,
+                                                               YUV_PIXEL_FORMATS,
+                                                               XYZ_PIXEL_FORMATS};
 
 typedef struct {
     AVClass *class;
     opj_dparameters_t dec_params;
-    AVFrame image;
     int lowqual;
 } LibOpenJPEGContext;
 
@@ -221,16 +225,6 @@
     LibOpenJPEGContext *ctx = avctx->priv_data;
 
     opj_set_default_decoder_parameters(&ctx->dec_params);
-    avcodec_get_frame_defaults(&ctx->image);
-    avctx->coded_frame = &ctx->image;
-    return 0;
-}
-
-static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
-{
-    LibOpenJPEGContext *ctx = avctx->priv_data;
-
-    avctx->coded_frame = &ctx->image;
     return 0;
 }
 
@@ -241,7 +235,8 @@
     uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     LibOpenJPEGContext *ctx = avctx->priv_data;
-    AVFrame *picture = &ctx->image, *output = data;
+    ThreadFrame frame = { .f = data };
+    AVFrame *picture  = data;
     const AVPixFmtDescriptor *desc;
     opj_dinfo_t *dec;
     opj_cio_t *stream;
@@ -320,13 +315,8 @@
         if (image->comps[i].prec > avctx->bits_per_raw_sample)
             avctx->bits_per_raw_sample = image->comps[i].prec;
 
-    if (picture->data[0])
-        ff_thread_release_buffer(avctx, picture);
-
-    if (ff_thread_get_buffer(avctx, picture) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
+    if (ff_thread_get_buffer(avctx, &frame, 0) < 0)
         goto done;
-    }
 
     ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
     ctx->dec_params.cp_reduce = avctx->lowres;
@@ -385,7 +375,6 @@
         goto done;
     }
 
-    *output    = ctx->image;
     *got_frame = 1;
     ret        = buf_size;
 
@@ -395,15 +384,6 @@
     return ret;
 }
 
-static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
-{
-    LibOpenJPEGContext *ctx = avctx->priv_data;
-
-    if (ctx->image.data[0])
-        ff_thread_release_buffer(avctx, &ctx->image);
-    return 0;
-}
-
 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
 
@@ -425,11 +405,9 @@
     .id               = AV_CODEC_ID_JPEG2000,
     .priv_data_size   = sizeof(LibOpenJPEGContext),
     .init             = libopenjpeg_decode_init,
-    .close            = libopenjpeg_decode_close,
     .decode           = libopenjpeg_decode_frame,
     .capabilities     = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
     .max_lowres       = 31,
     .long_name        = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
     .priv_class       = &class,
-    .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy),
 };
diff --git a/libavcodec/libopusdec.c b/libavcodec/libopusdec.c
index 686b8a6..9b5cfe7 100644
--- a/libavcodec/libopusdec.c
+++ b/libavcodec/libopusdec.c
@@ -132,11 +132,8 @@
     int ret, nb_samples;
 
     frame->nb_samples = MAX_FRAME_SIZE;
-    ret = ff_get_buffer(avc, frame);
-    if (ret < 0) {
-        av_log(avc, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avc, frame, 0)) < 0)
         return ret;
-    }
 
     if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
         nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
diff --git a/libavcodec/libopusenc.c b/libavcodec/libopusenc.c
index a4aa37c..04c297d 100644
--- a/libavcodec/libopusenc.c
+++ b/libavcodec/libopusenc.c
@@ -331,10 +331,8 @@
     /* Maximum packet size taken from opusenc in opus-tools. 60ms packets
      * consist of 3 frames in one packet. The maximum frame size is 1275
      * bytes along with the largest possible packet header of 7 bytes. */
-    if (ret = ff_alloc_packet(avpkt, (1275 * 3 + 7) * opus->stream_count)) {
-        av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
+    if ((ret = ff_alloc_packet2(avctx, avpkt, (1275 * 3 + 7) * opus->stream_count)) < 0)
         return ret;
-    }
 
     if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
         ret = opus_multistream_encode_float(opus->enc, (float *)audio,
diff --git a/libavcodec/libschroedingerdec.c b/libavcodec/libschroedingerdec.c
index 985ae60..2e0ce5d 100644
--- a/libavcodec/libschroedingerdec.c
+++ b/libavcodec/libschroedingerdec.c
@@ -66,9 +66,6 @@
 
     /** end of sequence pulled */
     int eos_pulled;
-
-    /** decoded picture */
-    AVFrame dec_frame;
 } SchroDecoderParams;
 
 typedef struct SchroParseUnitContext {
@@ -215,6 +212,7 @@
     SchroDecoder *decoder = p_schro_params->decoder;
     SchroBuffer *enc_buf;
     SchroFrame* frame;
+    AVFrame *avframe = data;
     int state;
     int go = 1;
     int outer = 1;
@@ -308,35 +306,29 @@
     framewithpts = ff_schro_queue_pop(&p_schro_params->dec_frame_queue);
 
     if (framewithpts && framewithpts->frame) {
-        if (p_schro_params->dec_frame.data[0])
-            avctx->release_buffer(avctx, &p_schro_params->dec_frame);
-        if (ff_get_buffer(avctx, &p_schro_params->dec_frame) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "Unable to allocate buffer\n");
-            return AVERROR(ENOMEM);
-        }
+        int ret;
 
-        memcpy(p_schro_params->dec_frame.data[0],
+        if ((ret = ff_get_buffer(avctx, avframe, 0)) < 0)
+            return ret;
+
+        memcpy(avframe->data[0],
                framewithpts->frame->components[0].data,
                framewithpts->frame->components[0].length);
 
-        memcpy(p_schro_params->dec_frame.data[1],
+        memcpy(avframe->data[1],
                framewithpts->frame->components[1].data,
                framewithpts->frame->components[1].length);
 
-        memcpy(p_schro_params->dec_frame.data[2],
+        memcpy(avframe->data[2],
                framewithpts->frame->components[2].data,
                framewithpts->frame->components[2].length);
 
         /* Fill frame with current buffer data from Schroedinger. */
-        p_schro_params->dec_frame.format  = -1; /* Unknown -1 */
-        p_schro_params->dec_frame.width   = framewithpts->frame->width;
-        p_schro_params->dec_frame.height  = framewithpts->frame->height;
-        p_schro_params->dec_frame.pkt_pts = framewithpts->pts;
-        p_schro_params->dec_frame.linesize[0] = framewithpts->frame->components[0].stride;
-        p_schro_params->dec_frame.linesize[1] = framewithpts->frame->components[1].stride;
-        p_schro_params->dec_frame.linesize[2] = framewithpts->frame->components[2].stride;
+        avframe->pkt_pts = framewithpts->pts;
+        avframe->linesize[0] = framewithpts->frame->components[0].stride;
+        avframe->linesize[1] = framewithpts->frame->components[1].stride;
+        avframe->linesize[2] = framewithpts->frame->components[2].stride;
 
-        *(AVFrame*)data = p_schro_params->dec_frame;
         *got_frame      = 1;
 
         /* Now free the frame resources. */
@@ -357,9 +349,6 @@
     schro_decoder_free(p_schro_params->decoder);
     av_freep(&p_schro_params->format);
 
-    if (p_schro_params->dec_frame.data[0])
-        avctx->release_buffer(avctx, &p_schro_params->dec_frame);
-
     /* Free data in the output frame queue. */
     ff_schro_queue_free(&p_schro_params->dec_frame_queue,
                         libschroedinger_decode_frame_free);
diff --git a/libavcodec/libshine.c b/libavcodec/libshine.c
new file mode 100644
index 0000000..521ac17
--- /dev/null
+++ b/libavcodec/libshine.c
@@ -0,0 +1,149 @@
+/*
+ * Interface to libshine for mp3 encoding
+ * Copyright (c) 2012 Paul B Mahol
+ *
+ * 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 <shine/layer3.h>
+
+#include "libavutil/intreadwrite.h"
+#include "audio_frame_queue.h"
+#include "avcodec.h"
+#include "internal.h"
+#include "mpegaudio.h"
+#include "mpegaudiodecheader.h"
+
+#define BUFFER_SIZE (4096 * 20)
+
+typedef struct SHINEContext {
+    shine_config_t  config;
+    shine_t         shine;
+    uint8_t         buffer[BUFFER_SIZE];
+    int             buffer_index;
+    AudioFrameQueue afq;
+} SHINEContext;
+
+static av_cold int libshine_encode_init(AVCodecContext *avctx)
+{
+    SHINEContext *s = avctx->priv_data;
+
+    if (avctx->channels <= 0 || avctx->channels > 2){
+        av_log(avctx, AV_LOG_ERROR, "only mono or stereo is supported\n");
+        return AVERROR(EINVAL);
+    }
+
+    shine_set_config_mpeg_defaults(&s->config.mpeg);
+    if (avctx->bit_rate)
+        s->config.mpeg.bitr = avctx->bit_rate / 1000;
+    if (shine_find_bitrate_index(s->config.mpeg.bitr) < 0) {
+        av_log(avctx, AV_LOG_ERROR, "invalid bitrate\n");
+        return AVERROR(EINVAL);
+    }
+    s->config.mpeg.mode = avctx->channels == 2 ? STEREO : MONO;
+    s->config.wave.samplerate = avctx->sample_rate;
+    s->config.wave.channels   = avctx->channels == 2 ? PCM_STEREO : PCM_MONO;
+    s->shine = shine_initialise(&s->config);
+    if (!s->shine)
+        return AVERROR(ENOMEM);
+    avctx->frame_size = samp_per_frame;
+    ff_af_queue_init(avctx, &s->afq);
+    return 0;
+}
+
+static int libshine_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
+                                 const AVFrame *frame, int *got_packet_ptr)
+{
+    SHINEContext *s = avctx->priv_data;
+    MPADecodeHeader hdr;
+    unsigned char *data;
+    long written;
+    int ret, len;
+
+    if (frame)
+        data = shine_encode_frame(s->shine, frame->data[0], &written);
+    else
+        data = shine_flush(s->shine, &written);
+    if (written < 0)
+        return -1;
+    if (written > 0) {
+        if (s->buffer_index + written > BUFFER_SIZE) {
+            av_log(avctx, AV_LOG_ERROR, "internal buffer too small\n");
+            return AVERROR_BUG;
+        }
+        memcpy(s->buffer + s->buffer_index, data, written);
+        s->buffer_index += written;
+    }
+    if (frame) {
+        if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
+            return ret;
+    }
+
+    if (s->buffer_index < 4 || !s->afq.frame_count)
+        return 0;
+    if (avpriv_mpegaudio_decode_header(&hdr, AV_RB32(s->buffer))) {
+        av_log(avctx, AV_LOG_ERROR, "free format output not supported\n");
+        return -1;
+    }
+
+    len = hdr.frame_size;
+    if (len <= s->buffer_index) {
+        if ((ret = ff_alloc_packet2(avctx, avpkt, len)))
+            return ret;
+        memcpy(avpkt->data, s->buffer, len);
+        s->buffer_index -= len;
+        memmove(s->buffer, s->buffer + len, s->buffer_index);
+
+        ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
+                           &avpkt->duration);
+
+        avpkt->size = len;
+        *got_packet_ptr = 1;
+    }
+    return 0;
+}
+
+static av_cold int libshine_encode_close(AVCodecContext *avctx)
+{
+    SHINEContext *s = avctx->priv_data;
+
+    ff_af_queue_close(&s->afq);
+    shine_close(s->shine);
+    return 0;
+}
+
+static const int libshine_sample_rates[] = {
+    44100, 48000, 32000, 0
+};
+
+AVCodec ff_libshine_encoder = {
+    .name                  = "libshine",
+    .type                  = AVMEDIA_TYPE_AUDIO,
+    .id                    = CODEC_ID_MP3,
+    .priv_data_size        = sizeof(SHINEContext),
+    .init                  = libshine_encode_init,
+    .encode2               = libshine_encode_frame,
+    .close                 = libshine_encode_close,
+    .capabilities          = CODEC_CAP_DELAY,
+    .sample_fmts           = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16P,
+                                                            AV_SAMPLE_FMT_NONE },
+    .supported_samplerates = libshine_sample_rates,
+    .channel_layouts       = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
+                                                  AV_CH_LAYOUT_STEREO,
+                                                  0 },
+    .long_name             = NULL_IF_CONFIG_SMALL("libshine MP3 (MPEG audio layer 3)"),
+};
diff --git a/libavcodec/libspeexdec.c b/libavcodec/libspeexdec.c
index 1b2db78..213eee4 100644
--- a/libavcodec/libspeexdec.c
+++ b/libavcodec/libspeexdec.c
@@ -50,7 +50,17 @@
         if (!header)
             av_log(avctx, AV_LOG_WARNING, "Invalid Speex header\n");
     }
-    if (header) {
+    if (avctx->codec_tag == MKTAG('S', 'P', 'X', 'N')) {
+        if (!avctx->extradata || avctx->extradata && avctx->extradata_size < 47) {
+            av_log(avctx, AV_LOG_ERROR, "Missing or invalid extradata.\n");
+            return AVERROR_INVALIDDATA;
+        }
+        if (avctx->extradata[37] != 10) {
+            av_log(avctx, AV_LOG_ERROR, "Unsupported quality mode.\n");
+            return AVERROR_PATCHWELCOME;
+        }
+        spx_mode           = 0;
+    } else if (header) {
         avctx->sample_rate = header->rate;
         avctx->channels    = header->nb_channels;
         spx_mode           = header->mode;
@@ -118,10 +128,8 @@
 
     /* get output buffer */
     frame->nb_samples = s->frame_size;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     output = (int16_t *)frame->data[0];
 
     /* if there is not enough data left for the smallest possible frame or the
diff --git a/libavcodec/libspeexenc.c b/libavcodec/libspeexenc.c
index 23ebe72..b96ca1b 100644
--- a/libavcodec/libspeexenc.c
+++ b/libavcodec/libspeexenc.c
@@ -252,16 +252,6 @@
         av_log(avctx, AV_LOG_ERROR, "memory allocation error\n");
         return AVERROR(ENOMEM);
     }
-#if FF_API_OLD_ENCODE_AUDIO
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame) {
-        av_freep(&avctx->extradata);
-        speex_header_free(header_data);
-        speex_encoder_destroy(s->enc_state);
-        av_log(avctx, AV_LOG_ERROR, "memory allocation error\n");
-        return AVERROR(ENOMEM);
-    }
-#endif
 
     /* copy header packet to extradata */
     memcpy(avctx->extradata, header_data, header_size);
@@ -304,7 +294,7 @@
     /* write output if all frames for the packet have been encoded */
     if (s->pkt_frame_count == s->frames_per_packet) {
         s->pkt_frame_count = 0;
-        if ((ret = ff_alloc_packet2(avctx, avpkt, speex_bits_nbytes(&s->bits))))
+        if ((ret = ff_alloc_packet2(avctx, avpkt, speex_bits_nbytes(&s->bits))) < 0)
             return ret;
         ret = speex_bits_write(&s->bits, avpkt->data, avpkt->size);
         speex_bits_reset(&s->bits);
@@ -328,9 +318,6 @@
     speex_encoder_destroy(s->enc_state);
 
     ff_af_queue_close(&s->afq);
-#if FF_API_OLD_ENCODE_AUDIO
-    av_freep(&avctx->coded_frame);
-#endif
     av_freep(&avctx->extradata);
 
     return 0;
diff --git a/libavcodec/libstagefright.cpp b/libavcodec/libstagefright.cpp
index 4c270b0..a6fd1b9 100644
--- a/libavcodec/libstagefright.cpp
+++ b/libavcodec/libstagefright.cpp
@@ -187,7 +187,6 @@
             }
             ret = ff_get_buffer(avctx, frame->vframe);
             if (ret < 0) {
-                av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
                 frame->status = ret;
                 decode_done   = 1;
                 buffer->release();
diff --git a/libavcodec/libtheoraenc.c b/libavcodec/libtheoraenc.c
index 893370f..a2e5b74 100644
--- a/libavcodec/libtheoraenc.c
+++ b/libavcodec/libtheoraenc.c
@@ -207,11 +207,11 @@
     avcodec_get_chroma_sub_sample(avc_context->pix_fmt, &h->uv_hshift, &h->uv_vshift);
 
     if (avc_context->flags & CODEC_FLAG_QSCALE) {
-        /* to be constant with the libvorbis implementation, clip global_quality to 0 - 10
-           Theora accepts a quality parameter p, which is:
-                * 0 <= p <=63
-                * an int value
-         */
+        /* Clip global_quality in QP units to the [0 - 10] range
+           to be consistent with the libvorbis implementation.
+           Theora accepts a quality parameter which is an int value in
+           the [0 - 63] range.
+        */
         t_info.quality        = av_clipf(avc_context->global_quality / (float)FF_QP2LAMBDA, 0, 10) * 6.3;
         t_info.target_bitrate = 0;
     } else {
diff --git a/libavcodec/libtwolame.c b/libavcodec/libtwolame.c
index b57fbc9..8858714 100644
--- a/libavcodec/libtwolame.c
+++ b/libavcodec/libtwolame.c
@@ -94,7 +94,7 @@
     TWOLAMEContext *s = avctx->priv_data;
     int ret;
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE)) < 0)
         return ret;
 
     if (frame) {
diff --git a/libavcodec/libutvideo.h b/libavcodec/libutvideo.h
index ac665b2..b35d19c 100644
--- a/libavcodec/libutvideo.h
+++ b/libavcodec/libutvideo.h
@@ -31,14 +31,18 @@
 #include <utvideo/utvideo.h>
 #include <utvideo/Codec.h>
 
-/* Ut Video version 12.0.0 removed the _WIN names, so if those are
- * absent, redefine them to maintain compatibility with pre-v12 versions.*/
-#if !defined(UTVF_RGB24_WIN)
-#define UTVF_RGB24_WIN UTVF_NFCC_BGR_BU
+/*
+ * Ut Video version 12.0.0 changed the RGB format names and removed
+ * the _WIN names, so if the new names are absent, define them
+ * against the old names so compatibility with pre-v12 versions
+ * is maintained.
+ */
+#if !defined(UTVF_NFCC_BGR_BU)
+#define UTVF_NFCC_BGR_BU UTVF_RGB24_WIN
 #endif
 
-#if !defined(UTVF_RGB32_WIN)
-#define UTVF_RGB32_WIN UTVF_NFCC_BGRA_BU
+#if !defined(UTVF_NFCC_BGRA_BU)
+#define UTVF_NFCC_BGRA_BU UTVF_RGB32_WIN
 #endif
 
 typedef struct {
diff --git a/libavcodec/libutvideodec.cpp b/libavcodec/libutvideodec.cpp
index f70ac4f..bc491e2 100644
--- a/libavcodec/libutvideodec.cpp
+++ b/libavcodec/libutvideodec.cpp
@@ -61,11 +61,11 @@
         break;
     case MKTAG('U', 'L', 'R', 'G'):
         avctx->pix_fmt = AV_PIX_FMT_BGR24;
-        format = UTVF_RGB24_WIN;
+        format = UTVF_NFCC_BGR_BU;
         break;
     case MKTAG('U', 'L', 'R', 'A'):
         avctx->pix_fmt = AV_PIX_FMT_RGB32;
-        format = UTVF_RGB32_WIN;
+        format = UTVF_NFCC_BGRA_BU;
         break;
     default:
         av_log(avctx, AV_LOG_ERROR,
diff --git a/libavcodec/libutvideoenc.cpp b/libavcodec/libutvideoenc.cpp
index e9af7df..07205f7 100644
--- a/libavcodec/libutvideoenc.cpp
+++ b/libavcodec/libutvideoenc.cpp
@@ -51,12 +51,12 @@
         avctx->codec_tag = MKTAG('U', 'L', 'Y', '2');
         break;
     case AV_PIX_FMT_BGR24:
-        in_format = UTVF_RGB24_WIN;
+        in_format = UTVF_NFCC_BGR_BU;
         avctx->bits_per_coded_sample = 24;
         avctx->codec_tag = MKTAG('U', 'L', 'R', 'G');
         break;
     case AV_PIX_FMT_RGB32:
-        in_format = UTVF_RGB32_WIN;
+        in_format = UTVF_NFCC_BGRA_BU;
         avctx->bits_per_coded_sample = 32;
         avctx->codec_tag = MKTAG('U', 'L', 'R', 'A');
         break;
diff --git a/libavcodec/libvo-aacenc.c b/libavcodec/libvo-aacenc.c
index 1414aad..4f4cbe7 100644
--- a/libavcodec/libvo-aacenc.c
+++ b/libavcodec/libvo-aacenc.c
@@ -47,9 +47,6 @@
     AACContext *s = avctx->priv_data;
 
     s->codec_api.Uninit(s->handle);
-#if FF_API_OLD_ENCODE_AUDIO
-    av_freep(&avctx->coded_frame);
-#endif
     av_freep(&avctx->extradata);
     ff_af_queue_close(&s->afq);
     av_freep(&s->end_buffer);
@@ -63,11 +60,6 @@
     AACENC_PARAM params = { 0 };
     int index, ret;
 
-#if FF_API_OLD_ENCODE_AUDIO
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame)
-        return AVERROR(ENOMEM);
-#endif
     avctx->frame_size = FRAME_SIZE;
     avctx->delay      = ENC_DELAY;
     s->last_frame     = 2;
@@ -161,7 +153,7 @@
             return ret;
     }
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, FFMAX(8192, 768 * avctx->channels))))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, FFMAX(8192, 768 * avctx->channels))) < 0)
         return ret;
 
     input.Buffer  = samples;
diff --git a/libavcodec/libvo-amrwbenc.c b/libavcodec/libvo-amrwbenc.c
index eaf632d..a068cd0 100644
--- a/libavcodec/libvo-amrwbenc.c
+++ b/libavcodec/libvo-amrwbenc.c
@@ -94,11 +94,6 @@
 
     avctx->frame_size  = 320;
     avctx->delay       =  80;
-#if FF_API_OLD_ENCODE_AUDIO
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame)
-        return AVERROR(ENOMEM);
-#endif
 
     s->state     = E_IF_init();
 
@@ -110,7 +105,6 @@
     AMRWBContext *s = avctx->priv_data;
 
     E_IF_exit(s->state);
-    av_freep(&avctx->coded_frame);
     return 0;
 }
 
@@ -121,7 +115,7 @@
     const int16_t *samples = (const int16_t *)frame->data[0];
     int size, ret;
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, MAX_PACKET_SIZE)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, MAX_PACKET_SIZE)) < 0)
         return ret;
 
     if (s->last_bitrate != avctx->bit_rate) {
diff --git a/libavcodec/libvorbisdec.c b/libavcodec/libvorbisdec.c
index 99fb83a..c4142cd 100644
--- a/libavcodec/libvorbisdec.c
+++ b/libavcodec/libvorbisdec.c
@@ -143,10 +143,8 @@
     }
 
     frame->nb_samples = 8192*4;
-    if ((ret = ff_get_buffer(avccontext, frame)) < 0) {
-        av_log(avccontext, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avccontext, frame, 0)) < 0)
         return ret;
-    }
     output = (int16_t *)frame->data[0];
 
 
diff --git a/libavcodec/libvorbisenc.c b/libavcodec/libvorbisenc.c
index 188c8cc..d3f86cc 100644
--- a/libavcodec/libvorbisenc.c
+++ b/libavcodec/libvorbisenc.c
@@ -187,9 +187,6 @@
 
     av_fifo_free(s->pkt_fifo);
     ff_af_queue_close(&s->afq);
-#if FF_API_OLD_ENCODE_AUDIO
-    av_freep(&avctx->coded_frame);
-#endif
     av_freep(&avctx->extradata);
 
     return 0;
@@ -267,14 +264,6 @@
         goto error;
     }
 
-#if FF_API_OLD_ENCODE_AUDIO
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame) {
-        ret = AVERROR(ENOMEM);
-        goto error;
-    }
-#endif
-
     return 0;
 error:
     oggvorbis_encode_close(avctx);
@@ -348,7 +337,7 @@
 
     av_fifo_generic_read(s->pkt_fifo, &op, sizeof(ogg_packet), NULL);
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, op.bytes)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, op.bytes)) < 0)
         return ret;
     av_fifo_generic_read(s->pkt_fifo, avpkt->data, op.bytes, NULL);
 
diff --git a/libavcodec/libvpxdec.c b/libavcodec/libvpxdec.c
index 7e41e80..5e80a9f 100644
--- a/libavcodec/libvpxdec.c
+++ b/libavcodec/libvpxdec.c
@@ -30,6 +30,7 @@
 #include "libavutil/common.h"
 #include "libavutil/imgutils.h"
 #include "avcodec.h"
+#include "internal.h"
 
 typedef struct VP8DecoderContext {
     struct vpx_codec_ctx decoder;
@@ -65,6 +66,7 @@
     AVFrame *picture = data;
     const void *iter = NULL;
     struct vpx_image *img;
+    int ret;
 
     if (vpx_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) !=
         VPX_CODEC_OK) {
@@ -92,14 +94,10 @@
                 return AVERROR_INVALIDDATA;
             avcodec_set_dimensions(avctx, img->d_w, img->d_h);
         }
-        picture->data[0]     = img->planes[0];
-        picture->data[1]     = img->planes[1];
-        picture->data[2]     = img->planes[2];
-        picture->data[3]     = NULL;
-        picture->linesize[0] = img->stride[0];
-        picture->linesize[1] = img->stride[1];
-        picture->linesize[2] = img->stride[2];
-        picture->linesize[3] = 0;
+        if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
+            return ret;
+        av_image_copy(picture->data, picture->linesize, img->planes,
+                      img->stride, avctx->pix_fmt, img->d_w, img->d_h);
         *got_frame           = 1;
     }
     return avpkt->size;
@@ -126,7 +124,7 @@
     .init           = vp8_init,
     .close          = vp8_free,
     .decode         = vp8_decode,
-    .capabilities   = CODEC_CAP_AUTO_THREADS,
+    .capabilities   = CODEC_CAP_AUTO_THREADS | CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("libvpx VP8"),
 };
 #endif /* CONFIG_LIBVPX_VP8_DECODER */
diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index a394213..7e8fa65 100644
--- a/libavcodec/libx264.c
+++ b/libavcodec/libx264.c
@@ -175,7 +175,7 @@
             frame->pict_type == AV_PICTURE_TYPE_P ? X264_TYPE_P :
             frame->pict_type == AV_PICTURE_TYPE_B ? X264_TYPE_B :
                                             X264_TYPE_AUTO;
-        if (x4->params.b_tff != frame->top_field_first) {
+        if (x4->params.b_interlaced && x4->params.b_tff != frame->top_field_first) {
             x4->params.b_tff = frame->top_field_first;
             x264_encoder_reconfig(x4->enc, &x4->params);
         }
diff --git a/libavcodec/libxvid.c b/libavcodec/libxvid.c
index dae8ac8..05a12db 100644
--- a/libavcodec/libxvid.c
+++ b/libavcodec/libxvid.c
@@ -60,6 +60,7 @@
     char *twopassbuffer;           /**< Character buffer for two-pass */
     char *old_twopassbuffer;       /**< Old character buffer (two-pass) */
     char *twopassfile;             /**< second pass temp file name */
+    int twopassfd;
     unsigned char *intra_matrix;   /**< P-Frame Quant Matrix */
     unsigned char *inter_matrix;   /**< I-Frame Quant Matrix */
 };
@@ -72,6 +73,8 @@
     struct xvid_context *context;   /**< Pointer to private context */
 };
 
+static int xvid_encode_close(AVCodecContext *avctx);
+
 /*
  * Xvid 2-Pass Kludge Section
  *
@@ -356,6 +359,8 @@
     xvid_enc_create_t xvid_enc_create = { 0 };
     xvid_enc_plugin_t plugins[7];
 
+    x->twopassfd = -1;
+
     /* Bring in VOP flags from ffmpeg command-line */
     x->vop_flags = XVID_VOP_HALFPEL; /* Bare minimum quality */
     if( xvid_flags & CODEC_FLAG_4MV )
@@ -474,7 +479,7 @@
         if( x->twopassbuffer == NULL || x->old_twopassbuffer == NULL ) {
             av_log(avctx, AV_LOG_ERROR,
                 "Xvid: Cannot allocate 2-pass log buffers\n");
-            return -1;
+            goto fail;
         }
         x->twopassbuffer[0] = x->old_twopassbuffer[0] = 0;
 
@@ -489,24 +494,23 @@
         if( fd == -1 ) {
             av_log(avctx, AV_LOG_ERROR,
                 "Xvid: Cannot write 2-pass pipe\n");
-            return -1;
+            goto fail;
         }
+        x->twopassfd = fd;
 
         if( avctx->stats_in == NULL ) {
             av_log(avctx, AV_LOG_ERROR,
                 "Xvid: No 2-pass information loaded for second pass\n");
-            return -1;
+            goto fail;
         }
 
         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;
+            goto fail;
         }
 
-        close(fd);
         rc2pass2.filename = x->twopassfile;
         plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;
         plugins[xvid_enc_create.num_plugins].param = &rc2pass2;
@@ -604,13 +608,16 @@
     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;
+        goto fail;
     }
 
     x->encoder_handle = xvid_enc_create.handle;
     avctx->coded_frame = &x->encoded_picture;
 
     return 0;
+fail:
+    xvid_encode_close(avctx);
+    return -1;
 }
 
 static int xvid_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
@@ -733,17 +740,24 @@
 static av_cold int xvid_encode_close(AVCodecContext *avctx) {
     struct xvid_context *x = avctx->priv_data;
 
-    xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL);
+    if(x->encoder_handle)
+        xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL);
+    x->encoder_handle = NULL;
 
     av_freep(&avctx->extradata);
     if( x->twopassbuffer != NULL ) {
-        av_free(x->twopassbuffer);
-        av_free(x->old_twopassbuffer);
+        av_freep(&x->twopassbuffer);
+        av_freep(&x->old_twopassbuffer);
         avctx->stats_out = NULL;
     }
-    av_free(x->twopassfile);
-    av_free(x->intra_matrix);
-    av_free(x->inter_matrix);
+    if (x->twopassfd>=0) {
+        unlink(x->twopassfile);
+        close(x->twopassfd);
+        x->twopassfd = -1;
+    }
+    av_freep(&x->twopassfile);
+    av_freep(&x->intra_matrix);
+    av_freep(&x->inter_matrix);
 
     return 0;
 }
diff --git a/libavcodec/ljpegenc.c b/libavcodec/ljpegenc.c
index 064b911..76c3cb9 100644
--- a/libavcodec/ljpegenc.c
+++ b/libavcodec/ljpegenc.c
@@ -68,7 +68,10 @@
 
     init_put_bits(&s->pb, pkt->data, pkt->size);
 
-    *p = *pict;
+    av_frame_unref(p);
+    ret = av_frame_ref(p, pict);
+    if (ret < 0)
+        return ret;
     p->pict_type= AV_PICTURE_TYPE_I;
     p->key_frame= 1;
 
diff --git a/libavcodec/loco.c b/libavcodec/loco.c
index 2b71166..ffb9742 100644
--- a/libavcodec/loco.c
+++ b/libavcodec/loco.c
@@ -45,7 +45,6 @@
 
 typedef struct LOCOContext {
     AVCodecContext *avctx;
-    AVFrame pic;
     int lossy;
     int mode;
 } LOCOContext;
@@ -176,17 +175,11 @@
     LOCOContext * const l = avctx->priv_data;
     const uint8_t *buf    = avpkt->data;
     int buf_size          = avpkt->size;
-    AVFrame * const p     = &l->pic;
+    AVFrame * const p     = data;
     int decoded, ret;
 
-    if (p->data[0])
-        avctx->release_buffer(avctx, p);
-
-    p->reference = 0;
-    if ((ret = ff_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
         return ret;
-    }
     p->key_frame = 1;
 
 #define ADVANCE_BY_DECODED do { \
@@ -247,7 +240,6 @@
     buf_size -= decoded;
 
     *got_frame      = 1;
-    *(AVFrame*)data = l->pic;
 
     return avpkt->size - buf_size;
 buf_too_small:
@@ -276,7 +268,7 @@
         break;
     default:
         l->lossy = AV_RL32(avctx->extradata + 8);
-        av_log_ask_for_sample(avctx, "This is LOCO codec version %i.\n", version);
+        avpriv_request_sample(avctx, "LOCO codec version %i", version);
     }
 
     l->mode = AV_RL32(avctx->extradata + 4);
@@ -305,19 +297,6 @@
     if (avctx->debug & FF_DEBUG_PICT_INFO)
         av_log(avctx, AV_LOG_INFO, "lossy:%i, version:%i, mode: %i\n", l->lossy, version, l->mode);
 
-    avcodec_get_frame_defaults(&l->pic);
-
-    return 0;
-}
-
-static av_cold int decode_end(AVCodecContext *avctx)
-{
-    LOCOContext * const l = avctx->priv_data;
-    AVFrame *pic = &l->pic;
-
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
-
     return 0;
 }
 
@@ -327,7 +306,6 @@
     .id             = AV_CODEC_ID_LOCO,
     .priv_data_size = sizeof(LOCOContext),
     .init           = decode_init,
-    .close          = decode_end,
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("LOCO"),
diff --git a/libavcodec/mace.c b/libavcodec/mace.c
index e78c49f..94bf2e7 100644
--- a/libavcodec/mace.c
+++ b/libavcodec/mace.c
@@ -246,10 +246,8 @@
 
     /* get output buffer */
     frame->nb_samples = 3 * (buf_size << (1 - is_mace3)) / avctx->channels;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     samples = (int16_t **)frame->extended_data;
 
     for(i = 0; i < avctx->channels; i++) {
diff --git a/libavcodec/mathtables.c b/libavcodec/mathtables.c
index 037b135..ec5ba79 100644
--- a/libavcodec/mathtables.c
+++ b/libavcodec/mathtables.c
@@ -87,3 +87,38 @@
 0x07,0x87,0x47,0xC7,0x27,0xA7,0x67,0xE7,0x17,0x97,0x57,0xD7,0x37,0xB7,0x77,0xF7,
 0x0F,0x8F,0x4F,0xCF,0x2F,0xAF,0x6F,0xEF,0x1F,0x9F,0x5F,0xDF,0x3F,0xBF,0x7F,0xFF,
 };
+
+#define times4(x) x, x, x, x
+#define times256(x) times4(times4(times4(times4(times4(x)))))
+
+const uint8_t ff_cropTbl[256 + 2 * 1024] = {
+times256(0x00),
+0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
+0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,
+0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,
+0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,
+0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,
+0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F,
+0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,
+0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F,
+0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,
+0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,
+0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,
+0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF,
+0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,
+0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF,
+0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,
+0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF,
+times256(0xFF)
+};
+
+const uint8_t ff_zigzag_direct[64] = {
+    0,   1,  8, 16,  9,  2,  3, 10,
+    17, 24, 32, 25, 18, 11,  4,  5,
+    12, 19, 26, 33, 40, 48, 41, 34,
+    27, 20, 13,  6,  7, 14, 21, 28,
+    35, 42, 49, 56, 57, 50, 43, 36,
+    29, 22, 15, 23, 30, 37, 44, 51,
+    58, 59, 52, 45, 38, 31, 39, 46,
+    53, 60, 61, 54, 47, 55, 62, 63
+};
diff --git a/libavcodec/mdec.c b/libavcodec/mdec.c
index 7e59c34..8fb29ce 100644
--- a/libavcodec/mdec.c
+++ b/libavcodec/mdec.c
@@ -35,7 +35,7 @@
 typedef struct MDECContext {
     AVCodecContext *avctx;
     DSPContext dsp;
-    AVFrame picture;
+    ThreadFrame frame;
     GetBitContext gb;
     ScanTable scantable;
     int version;
@@ -135,14 +135,14 @@
     return 0;
 }
 
-static inline void idct_put(MDECContext *a, int mb_x, int mb_y)
+static inline void idct_put(MDECContext *a, AVFrame *frame, int mb_x, int mb_y)
 {
     int16_t (*block)[64] = a->block;
-    int linesize = a->picture.linesize[0];
+    int linesize = frame->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;
+    uint8_t *dest_y  = frame->data[0] + (mb_y * 16* linesize              ) + mb_x * 16;
+    uint8_t *dest_cb = frame->data[1] + (mb_y * 8 * frame->linesize[1]) + mb_x * 8;
+    uint8_t *dest_cr = frame->data[2] + (mb_y * 8 * frame->linesize[2]) + mb_x * 8;
 
     a->dsp.idct_put(dest_y,                    linesize, block[0]);
     a->dsp.idct_put(dest_y                + 8, linesize, block[1]);
@@ -150,8 +150,8 @@
     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]);
+        a->dsp.idct_put(dest_cb, frame->linesize[1], block[4]);
+        a->dsp.idct_put(dest_cr, frame->linesize[2], block[5]);
     }
 }
 
@@ -162,20 +162,13 @@
     MDECContext * const a = avctx->priv_data;
     const uint8_t *buf    = avpkt->data;
     int buf_size          = avpkt->size;
-    AVFrame *picture      = data;
-    AVFrame * const p     = &a->picture;
+    ThreadFrame frame     = { .f = data };
     int i, ret;
 
-    if (p->data[0])
-        ff_thread_release_buffer(avctx, p);
-
-    p->reference = 0;
-    if ((ret = ff_thread_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
         return ret;
-    }
-    p->pict_type = AV_PICTURE_TYPE_I;
-    p->key_frame = 1;
+    frame.f->pict_type = AV_PICTURE_TYPE_I;
+    frame.f->key_frame = 1;
 
     av_fast_malloc(&a->bitstream_buffer, &a->bitstream_buffer_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
     if (!a->bitstream_buffer)
@@ -199,14 +192,10 @@
             if ((ret = decode_mb(a, a->block)) < 0)
                 return ret;
 
-            idct_put(a, a->mb_x, a->mb_y);
+            idct_put(a, frame.f, a->mb_x, a->mb_y);
         }
     }
 
-    p->quality = a->qscale * FF_QP2LAMBDA;
-    memset(p->qscale_table, a->qscale, a->mb_width);
-
-    *picture   = a->picture;
     *got_frame = 1;
 
     return (get_bits_count(&a->gb) + 31) / 32 * 4;
@@ -215,13 +204,10 @@
 static av_cold int decode_init(AVCodecContext *avctx)
 {
     MDECContext * const a = avctx->priv_data;
-    AVFrame *p            = &a->picture;
 
     a->mb_width  = (avctx->coded_width  + 15) / 16;
     a->mb_height = (avctx->coded_height + 15) / 16;
 
-    avcodec_get_frame_defaults(&a->picture);
-    avctx->coded_frame = &a->picture;
     a->avctx           = avctx;
 
     ff_dsputil_init(&a->dsp, avctx);
@@ -230,8 +216,6 @@
 
     if (avctx->idct_algo == FF_IDCT_AUTO)
         avctx->idct_algo = FF_IDCT_SIMPLE;
-    p->qstride      = 0;
-    p->qscale_table = av_mallocz(a->mb_width);
     avctx->pix_fmt  = AV_PIX_FMT_YUVJ420P;
 
     return 0;
@@ -240,13 +224,9 @@
 static av_cold int decode_init_thread_copy(AVCodecContext *avctx)
 {
     MDECContext * const a = avctx->priv_data;
-    AVFrame *p            = &a->picture;
 
-    avctx->coded_frame = p;
     a->avctx           = avctx;
 
-    p->qscale_table= av_mallocz(a->mb_width);
-
     return 0;
 }
 
@@ -254,10 +234,7 @@
 {
     MDECContext * const a = avctx->priv_data;
 
-    if (a->picture.data[0])
-        avctx->release_buffer(avctx, &a->picture);
     av_freep(&a->bitstream_buffer);
-    av_freep(&a->picture.qscale_table);
     a->bitstream_buffer_size = 0;
 
     return 0;
diff --git a/libavcodec/mimic.c b/libavcodec/mimic.c
index 54d27e8..85c6a1a 100644
--- a/libavcodec/mimic.c
+++ b/libavcodec/mimic.c
@@ -28,6 +28,7 @@
 #include "get_bits.h"
 #include "bytestream.h"
 #include "dsputil.h"
+#include "hpeldsp.h"
 #include "thread.h"
 
 #define MIMIC_HEADER_SIZE   20
@@ -44,7 +45,7 @@
     int             cur_index;
     int             prev_index;
 
-    AVFrame         buf_ptrs    [16];
+    ThreadFrame     frames     [16];
     AVPicture       flipped_ptrs[16];
 
     DECLARE_ALIGNED(16, int16_t, dct_block)[64];
@@ -52,6 +53,7 @@
     GetBitContext   gb;
     ScanTable       scantable;
     DSPContext      dsp;
+    HpelDSPContext  hdsp;
     VLC             vlc;
 
     /* Kept in the context so multithreading can have a constant to read from */
@@ -109,10 +111,31 @@
     53, 60, 61, 54, 47, 55, 62, 63,
 };
 
+static av_cold int mimic_decode_end(AVCodecContext *avctx)
+{
+    MimicContext *ctx = avctx->priv_data;
+    int i;
+
+    av_free(ctx->swap_buf);
+
+    for (i = 0; i < FF_ARRAY_ELEMS(ctx->frames); i++) {
+        if (ctx->frames[i].f)
+            ff_thread_release_buffer(avctx, &ctx->frames[i]);
+        av_frame_free(&ctx->frames[i].f);
+    }
+
+    if (!avctx->internal->is_copy)
+        ff_free_vlc(&ctx->vlc);
+
+    return 0;
+}
+
 static av_cold int mimic_decode_init(AVCodecContext *avctx)
 {
     MimicContext *ctx = avctx->priv_data;
-    int ret;
+    int ret, i;
+
+    avctx->internal->allocate_progress = 1;
 
     ctx->prev_index = 0;
     ctx->cur_index  = 15;
@@ -123,14 +146,24 @@
         return ret;
     }
     ff_dsputil_init(&ctx->dsp, avctx);
+    ff_hpeldsp_init(&ctx->hdsp, avctx->flags);
     ff_init_scantable(ctx->dsp.idct_permutation, &ctx->scantable, col_zag);
 
+    for (i = 0; i < FF_ARRAY_ELEMS(ctx->frames); i++) {
+        ctx->frames[i].f = av_frame_alloc();
+        if (!ctx->frames[i].f) {
+            mimic_decode_end(avctx);
+            return AVERROR(ENOMEM);
+        }
+    }
+
     return 0;
 }
 
 static int mimic_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from)
 {
     MimicContext *dst = avctx->priv_data, *src = avctx_from->priv_data;
+    int i, ret;
 
     if (avctx == avctx_from)
         return 0;
@@ -138,10 +171,16 @@
     dst->cur_index  = src->next_cur_index;
     dst->prev_index = src->next_prev_index;
 
-    memcpy(dst->buf_ptrs, src->buf_ptrs, sizeof(src->buf_ptrs));
     memcpy(dst->flipped_ptrs, src->flipped_ptrs, sizeof(src->flipped_ptrs));
 
-    memset(&dst->buf_ptrs[dst->cur_index], 0, sizeof(AVFrame));
+    for (i = 0; i < FF_ARRAY_ELEMS(dst->frames); i++) {
+        ff_thread_release_buffer(avctx, &dst->frames[i]);
+        if (src->frames[i].f->data[0]) {
+            ret = ff_thread_ref_frame(&dst->frames[i], &src->frames[i]);
+            if (ret < 0)
+                return ret;
+        }
+    }
 
     return 0;
 }
@@ -264,20 +303,20 @@
                         uint8_t *p           = ctx->flipped_ptrs[index].data[0];
 
                         if (index != ctx->cur_index && p) {
-                            ff_thread_await_progress(&ctx->buf_ptrs[index],
+                            ff_thread_await_progress(&ctx->frames[index],
                                                      cur_row, 0);
                             p += src -
                                  ctx->flipped_ptrs[ctx->prev_index].data[plane];
-                            ctx->dsp.put_pixels_tab[1][0](dst, p, stride, 8);
+                            ctx->hdsp.put_pixels_tab[1][0](dst, p, stride, 8);
                         } else {
                             av_log(ctx->avctx, AV_LOG_ERROR,
                                      "No such backreference! Buggy sample.\n");
                         }
                     }
                 } else {
-                    ff_thread_await_progress(&ctx->buf_ptrs[ctx->prev_index],
+                    ff_thread_await_progress(&ctx->frames[ctx->prev_index],
                                              cur_row, 0);
-                    ctx->dsp.put_pixels_tab[1][0](dst, src, stride, 8);
+                    ctx->hdsp.put_pixels_tab[1][0](dst, src, stride, 8);
                 }
                 src += 8;
                 dst += 8;
@@ -285,7 +324,7 @@
             src += (stride - ctx->num_hblocks[plane]) << 3;
             dst += (stride - ctx->num_hblocks[plane]) << 3;
 
-            ff_thread_report_progress(&ctx->buf_ptrs[ctx->cur_index],
+            ff_thread_report_progress(&ctx->frames[ctx->cur_index],
                                       cur_row++, 0);
         }
     }
@@ -353,28 +392,27 @@
             ctx->num_hblocks[i] =     width   >> (3 + !!i);
         }
     } else if (width != ctx->avctx->width || height != ctx->avctx->height) {
-        av_log_missing_feature(avctx, "resolution changing", 1);
+        avpriv_request_sample(avctx, "Resolution changing");
         return AVERROR_PATCHWELCOME;
     }
 
-    if (is_pframe && !ctx->buf_ptrs[ctx->prev_index].data[0]) {
+    if (is_pframe && !ctx->frames[ctx->prev_index].f->data[0]) {
         av_log(avctx, AV_LOG_ERROR, "decoding must start with keyframe\n");
         return AVERROR_INVALIDDATA;
     }
 
-    ctx->buf_ptrs[ctx->cur_index].reference = 3;
-    ctx->buf_ptrs[ctx->cur_index].pict_type = is_pframe ? AV_PICTURE_TYPE_P :
-                                                          AV_PICTURE_TYPE_I;
-    if ((res = ff_thread_get_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index])) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    ff_thread_release_buffer(avctx, &ctx->frames[ctx->cur_index]);
+    ctx->frames[ctx->cur_index].f->pict_type = is_pframe ? AV_PICTURE_TYPE_P :
+                                                           AV_PICTURE_TYPE_I;
+    if ((res = ff_thread_get_buffer(avctx, &ctx->frames[ctx->cur_index],
+                                    AV_GET_BUFFER_FLAG_REF)) < 0)
         return res;
-    }
 
     ctx->next_prev_index = ctx->cur_index;
     ctx->next_cur_index  = (ctx->cur_index - 1) & 15;
 
     prepare_avpic(ctx, &ctx->flipped_ptrs[ctx->cur_index],
-                  &ctx->buf_ptrs[ctx->cur_index]);
+                  ctx->frames[ctx->cur_index].f);
 
     ff_thread_finish_setup(avctx);
 
@@ -388,41 +426,39 @@
     init_get_bits(&ctx->gb, ctx->swap_buf, swap_buf_size << 3);
 
     res = decode(ctx, quality, num_coeffs, !is_pframe);
-    ff_thread_report_progress(&ctx->buf_ptrs[ctx->cur_index], INT_MAX, 0);
+    ff_thread_report_progress(&ctx->frames[ctx->cur_index], INT_MAX, 0);
     if (res < 0) {
         if (!(avctx->active_thread_type & FF_THREAD_FRAME)) {
-            ff_thread_release_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index]);
+            ff_thread_release_buffer(avctx, &ctx->frames[ctx->cur_index]);
             return res;
         }
     }
 
-    *(AVFrame*)data = ctx->buf_ptrs[ctx->cur_index];
+    if ((res = av_frame_ref(data, ctx->frames[ctx->cur_index].f)) < 0)
+        return res;
     *got_frame      = 1;
 
     ctx->prev_index = ctx->next_prev_index;
     ctx->cur_index  = ctx->next_cur_index;
 
     /* Only release frames that aren't used for backreferences anymore */
-    if (ctx->buf_ptrs[ctx->cur_index].data[0])
-        ff_thread_release_buffer(avctx, &ctx->buf_ptrs[ctx->cur_index]);
+    ff_thread_release_buffer(avctx, &ctx->frames[ctx->cur_index]);
 
     return buf_size;
 }
 
-static av_cold int mimic_decode_end(AVCodecContext *avctx)
+static av_cold int mimic_init_thread_copy(AVCodecContext *avctx)
 {
     MimicContext *ctx = avctx->priv_data;
     int i;
 
-    av_free(ctx->swap_buf);
-
-    if (avctx->internal->is_copy)
-        return 0;
-
-    for (i = 0; i < 16; i++)
-        if (ctx->buf_ptrs[i].data[0])
-            ff_thread_release_buffer(avctx, &ctx->buf_ptrs[i]);
-    ff_free_vlc(&ctx->vlc);
+    for (i = 0; i < FF_ARRAY_ELEMS(ctx->frames); i++) {
+        ctx->frames[i].f = av_frame_alloc();
+        if (!ctx->frames[i].f) {
+            mimic_decode_end(avctx);
+            return AVERROR(ENOMEM);
+        }
+    }
 
     return 0;
 }
@@ -437,5 +473,6 @@
     .decode                = mimic_decode_frame,
     .capabilities          = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
     .long_name             = NULL_IF_CONFIG_SMALL("Mimic"),
-    .update_thread_context = ONLY_IF_THREADS_ENABLED(mimic_decode_update_thread_context)
+    .update_thread_context = ONLY_IF_THREADS_ENABLED(mimic_decode_update_thread_context),
+    .init_thread_copy      = ONLY_IF_THREADS_ENABLED(mimic_init_thread_copy),
 };
diff --git a/libavcodec/mips/Makefile b/libavcodec/mips/Makefile
index 8223f11..a2ea412 100644
--- a/libavcodec/mips/Makefile
+++ b/libavcodec/mips/Makefile
@@ -17,3 +17,5 @@
                                              mips/aacsbr_mips.o            \
                                              mips/sbrdsp_mips.o            \
                                              mips/aacpsdsp_mips.o
+MIPSDSPR1-OBJS-$(CONFIG_AAC_ENCODER)      += mips/aaccoder_mips.o
+MIPSFPU-OBJS-$(CONFIG_AAC_ENCODER)        += mips/iirfilter_mips.o
diff --git a/libavcodec/mips/aaccoder_mips.c b/libavcodec/mips/aaccoder_mips.c
new file mode 100644
index 0000000..d6210d1
--- /dev/null
+++ b/libavcodec/mips/aaccoder_mips.c
@@ -0,0 +1,2498 @@
+/*
+ * Copyright (c) 2012
+ *      MIPS Technologies, Inc., California.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the MIPS Technologies, Inc., nor the names of its
+ *    contributors may be used to endorse or promote products derived from
+ *    this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE MIPS TECHNOLOGIES, INC. ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE MIPS TECHNOLOGIES, INC. BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Author:  Stanislav Ocovaj (socovaj@mips.com)
+ *          Szabolcs Pal     (sabolc@mips.com)
+ *
+ * AAC coefficients encoder optimized for MIPS floating-point architecture
+ *
+ * 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
+ * Reference: libavcodec/aaccoder.c
+ */
+
+#include "libavutil/libm.h"
+
+#include <float.h>
+#include "libavutil/mathematics.h"
+#include "libavcodec/avcodec.h"
+#include "libavcodec/put_bits.h"
+#include "libavcodec/aac.h"
+#include "libavcodec/aacenc.h"
+#include "libavcodec/aactab.h"
+
+#if HAVE_INLINE_ASM
+typedef struct BandCodingPath {
+    int prev_idx;
+    float cost;
+    int run;
+} BandCodingPath;
+
+static const uint8_t run_value_bits_long[64] = {
+     5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,
+     5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5, 10,
+    10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
+    10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 15
+};
+
+static const uint8_t run_value_bits_short[16] = {
+    3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 9
+};
+
+static const uint8_t *run_value_bits[2] = {
+    run_value_bits_long, run_value_bits_short
+};
+
+static const uint8_t uquad_sign_bits[81] = {
+    0, 1, 1, 1, 2, 2, 1, 2, 2,
+    1, 2, 2, 2, 3, 3, 2, 3, 3,
+    1, 2, 2, 2, 3, 3, 2, 3, 3,
+    1, 2, 2, 2, 3, 3, 2, 3, 3,
+    2, 3, 3, 3, 4, 4, 3, 4, 4,
+    2, 3, 3, 3, 4, 4, 3, 4, 4,
+    1, 2, 2, 2, 3, 3, 2, 3, 3,
+    2, 3, 3, 3, 4, 4, 3, 4, 4,
+    2, 3, 3, 3, 4, 4, 3, 4, 4
+};
+
+static const uint8_t upair7_sign_bits[64] = {
+    0, 1, 1, 1, 1, 1, 1, 1,
+    1, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2,
+};
+
+static const uint8_t upair12_sign_bits[169] = {
+    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
+};
+
+static const uint8_t esc_sign_bits[289] = {
+    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
+};
+
+static void abs_pow34_v(float *out, const float *in, const int size) {
+#ifndef USE_REALLY_FULL_SEARCH
+    int i;
+    float a, b, c, d;
+    float ax, bx, cx, dx;
+
+    for (i = 0; i < size; i += 4) {
+        a = fabsf(in[i  ]);
+        b = fabsf(in[i+1]);
+        c = fabsf(in[i+2]);
+        d = fabsf(in[i+3]);
+
+        ax = sqrtf(a);
+        bx = sqrtf(b);
+        cx = sqrtf(c);
+        dx = sqrtf(d);
+
+        a = a * ax;
+        b = b * bx;
+        c = c * cx;
+        d = d * dx;
+
+        out[i  ] = sqrtf(a);
+        out[i+1] = sqrtf(b);
+        out[i+2] = sqrtf(c);
+        out[i+3] = sqrtf(d);
+    }
+#endif /* USE_REALLY_FULL_SEARCH */
+}
+
+static float find_max_val(int group_len, int swb_size, const float *scaled) {
+    float maxval = 0.0f;
+    int w2, i;
+    for (w2 = 0; w2 < group_len; w2++) {
+        for (i = 0; i < swb_size; i++) {
+            maxval = FFMAX(maxval, scaled[w2*128+i]);
+        }
+    }
+    return maxval;
+}
+
+static int find_min_book(float maxval, int sf) {
+    float Q = ff_aac_pow2sf_tab[POW_SF2_ZERO - sf + SCALE_ONE_POS - SCALE_DIV_512];
+    float Q34 = sqrtf(Q * sqrtf(Q));
+    int qmaxval, cb;
+    qmaxval = maxval * Q34 + 0.4054f;
+    if      (qmaxval ==  0) cb = 0;
+    else if (qmaxval ==  1) cb = 1;
+    else if (qmaxval ==  2) cb = 3;
+    else if (qmaxval <=  4) cb = 5;
+    else if (qmaxval <=  7) cb = 7;
+    else if (qmaxval <= 12) cb = 9;
+    else                    cb = 11;
+    return cb;
+}
+
+/**
+ * Functions developed from template function and optimized for quantizing and encoding band
+ */
+static void quantize_and_encode_band_cost_SQUAD_mips(struct AACEncContext *s,
+                                                     PutBitContext *pb, const float *in,
+                                                     const float *scaled, int size, int scale_idx,
+                                                     int cb, const float lambda, const float uplim,
+                                                     int *bits)
+{
+    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
+    int i;
+    int qc1, qc2, qc3, qc4;
+
+    uint8_t  *p_bits  = (uint8_t  *)ff_aac_spectral_bits[cb-1];
+    uint16_t *p_codes = (uint16_t *)ff_aac_spectral_codes[cb-1];
+
+    abs_pow34_v(s->scoefs, in, size);
+    scaled = s->scoefs;
+    for (i = 0; i < size; i += 4) {
+        int curidx;
+        int *in_int = (int *)&in[i];
+
+        qc1 = scaled[i  ] * Q34 + 0.4054f;
+        qc2 = scaled[i+1] * Q34 + 0.4054f;
+        qc3 = scaled[i+2] * Q34 + 0.4054f;
+        qc4 = scaled[i+3] * Q34 + 0.4054f;
+
+        __asm__ volatile (
+            ".set push                      \n\t"
+            ".set noreorder                 \n\t"
+
+            "slt    %[qc1], $zero,  %[qc1]  \n\t"
+            "slt    %[qc2], $zero,  %[qc2]  \n\t"
+            "slt    %[qc3], $zero,  %[qc3]  \n\t"
+            "slt    %[qc4], $zero,  %[qc4]  \n\t"
+            "lw     $t0,    0(%[in_int])    \n\t"
+            "lw     $t1,    4(%[in_int])    \n\t"
+            "lw     $t2,    8(%[in_int])    \n\t"
+            "lw     $t3,    12(%[in_int])   \n\t"
+            "srl    $t0,    $t0,    31      \n\t"
+            "srl    $t1,    $t1,    31      \n\t"
+            "srl    $t2,    $t2,    31      \n\t"
+            "srl    $t3,    $t3,    31      \n\t"
+            "subu   $t4,    $zero,  %[qc1]  \n\t"
+            "subu   $t5,    $zero,  %[qc2]  \n\t"
+            "subu   $t6,    $zero,  %[qc3]  \n\t"
+            "subu   $t7,    $zero,  %[qc4]  \n\t"
+            "movn   %[qc1], $t4,    $t0     \n\t"
+            "movn   %[qc2], $t5,    $t1     \n\t"
+            "movn   %[qc3], $t6,    $t2     \n\t"
+            "movn   %[qc4], $t7,    $t3     \n\t"
+
+            ".set pop                       \n\t"
+
+            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
+              [qc3]"+r"(qc3), [qc4]"+r"(qc4)
+            : [in_int]"r"(in_int)
+            : "t0", "t1", "t2", "t3",
+              "t4", "t5", "t6", "t7",
+              "memory"
+        );
+
+        curidx = qc1;
+        curidx *= 3;
+        curidx += qc2;
+        curidx *= 3;
+        curidx += qc3;
+        curidx *= 3;
+        curidx += qc4;
+        curidx += 40;
+
+        put_bits(pb, p_bits[curidx], p_codes[curidx]);
+    }
+}
+
+static void quantize_and_encode_band_cost_UQUAD_mips(struct AACEncContext *s,
+                                                     PutBitContext *pb, const float *in,
+                                                     const float *scaled, int size, int scale_idx,
+                                                     int cb, const float lambda, const float uplim,
+                                                     int *bits)
+{
+    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
+    int i;
+    int qc1, qc2, qc3, qc4;
+
+    uint8_t  *p_bits  = (uint8_t  *)ff_aac_spectral_bits[cb-1];
+    uint16_t *p_codes = (uint16_t *)ff_aac_spectral_codes[cb-1];
+
+    abs_pow34_v(s->scoefs, in, size);
+    scaled = s->scoefs;
+    for (i = 0; i < size; i += 4) {
+        int curidx, sign, count;
+        int *in_int = (int *)&in[i];
+        uint8_t v_bits;
+        unsigned int v_codes;
+
+        qc1 = scaled[i  ] * Q34 + 0.4054f;
+        qc2 = scaled[i+1] * Q34 + 0.4054f;
+        qc3 = scaled[i+2] * Q34 + 0.4054f;
+        qc4 = scaled[i+3] * Q34 + 0.4054f;
+
+        __asm__ volatile (
+            ".set push                              \n\t"
+            ".set noreorder                         \n\t"
+
+            "ori    $t4,        $zero,      2       \n\t"
+            "ori    %[sign],    $zero,      0       \n\t"
+            "slt    $t0,        $t4,        %[qc1]  \n\t"
+            "slt    $t1,        $t4,        %[qc2]  \n\t"
+            "slt    $t2,        $t4,        %[qc3]  \n\t"
+            "slt    $t3,        $t4,        %[qc4]  \n\t"
+            "movn   %[qc1],     $t4,        $t0     \n\t"
+            "movn   %[qc2],     $t4,        $t1     \n\t"
+            "movn   %[qc3],     $t4,        $t2     \n\t"
+            "movn   %[qc4],     $t4,        $t3     \n\t"
+            "lw     $t0,        0(%[in_int])        \n\t"
+            "lw     $t1,        4(%[in_int])        \n\t"
+            "lw     $t2,        8(%[in_int])        \n\t"
+            "lw     $t3,        12(%[in_int])       \n\t"
+            "slt    $t0,        $t0,        $zero   \n\t"
+            "movn   %[sign],    $t0,        %[qc1]  \n\t"
+            "slt    $t1,        $t1,        $zero   \n\t"
+            "slt    $t2,        $t2,        $zero   \n\t"
+            "slt    $t3,        $t3,        $zero   \n\t"
+            "sll    $t0,        %[sign],    1       \n\t"
+            "or     $t0,        $t0,        $t1     \n\t"
+            "movn   %[sign],    $t0,        %[qc2]  \n\t"
+            "slt    $t4,        $zero,      %[qc1]  \n\t"
+            "slt    $t1,        $zero,      %[qc2]  \n\t"
+            "slt    %[count],   $zero,      %[qc3]  \n\t"
+            "sll    $t0,        %[sign],    1       \n\t"
+            "or     $t0,        $t0,        $t2     \n\t"
+            "movn   %[sign],    $t0,        %[qc3]  \n\t"
+            "slt    $t2,        $zero,      %[qc4]  \n\t"
+            "addu   %[count],   %[count],   $t4     \n\t"
+            "addu   %[count],   %[count],   $t1     \n\t"
+            "sll    $t0,        %[sign],    1       \n\t"
+            "or     $t0,        $t0,        $t3     \n\t"
+            "movn   %[sign],    $t0,        %[qc4]  \n\t"
+            "addu   %[count],   %[count],   $t2     \n\t"
+
+            ".set pop                               \n\t"
+
+            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
+              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
+              [sign]"=&r"(sign), [count]"=&r"(count)
+            : [in_int]"r"(in_int)
+            : "t0", "t1", "t2", "t3", "t4",
+              "memory"
+        );
+
+        curidx = qc1;
+        curidx *= 3;
+        curidx += qc2;
+        curidx *= 3;
+        curidx += qc3;
+        curidx *= 3;
+        curidx += qc4;
+
+        v_codes = (p_codes[curidx] << count) | (sign & ((1 << count) - 1));
+        v_bits  = p_bits[curidx] + count;
+        put_bits(pb, v_bits, v_codes);
+    }
+}
+
+static void quantize_and_encode_band_cost_SPAIR_mips(struct AACEncContext *s,
+                                                     PutBitContext *pb, const float *in,
+                                                     const float *scaled, int size, int scale_idx,
+                                                     int cb, const float lambda, const float uplim,
+                                                     int *bits)
+{
+    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
+    int i;
+    int qc1, qc2, qc3, qc4;
+
+    uint8_t  *p_bits  = (uint8_t  *)ff_aac_spectral_bits[cb-1];
+    uint16_t *p_codes = (uint16_t *)ff_aac_spectral_codes[cb-1];
+
+    abs_pow34_v(s->scoefs, in, size);
+    scaled = s->scoefs;
+    for (i = 0; i < size; i += 4) {
+        int curidx, curidx2;
+        int *in_int = (int *)&in[i];
+        uint8_t v_bits;
+        unsigned int v_codes;
+
+        qc1 = scaled[i  ] * Q34 + 0.4054f;
+        qc2 = scaled[i+1] * Q34 + 0.4054f;
+        qc3 = scaled[i+2] * Q34 + 0.4054f;
+        qc4 = scaled[i+3] * Q34 + 0.4054f;
+
+        __asm__ volatile (
+            ".set push                      \n\t"
+            ".set noreorder                 \n\t"
+
+            "ori    $t4,    $zero,  4       \n\t"
+            "slt    $t0,    $t4,    %[qc1]  \n\t"
+            "slt    $t1,    $t4,    %[qc2]  \n\t"
+            "slt    $t2,    $t4,    %[qc3]  \n\t"
+            "slt    $t3,    $t4,    %[qc4]  \n\t"
+            "movn   %[qc1], $t4,    $t0     \n\t"
+            "movn   %[qc2], $t4,    $t1     \n\t"
+            "movn   %[qc3], $t4,    $t2     \n\t"
+            "movn   %[qc4], $t4,    $t3     \n\t"
+            "lw     $t0,    0(%[in_int])    \n\t"
+            "lw     $t1,    4(%[in_int])    \n\t"
+            "lw     $t2,    8(%[in_int])    \n\t"
+            "lw     $t3,    12(%[in_int])   \n\t"
+            "srl    $t0,    $t0,    31      \n\t"
+            "srl    $t1,    $t1,    31      \n\t"
+            "srl    $t2,    $t2,    31      \n\t"
+            "srl    $t3,    $t3,    31      \n\t"
+            "subu   $t4,    $zero,  %[qc1]  \n\t"
+            "subu   $t5,    $zero,  %[qc2]  \n\t"
+            "subu   $t6,    $zero,  %[qc3]  \n\t"
+            "subu   $t7,    $zero,  %[qc4]  \n\t"
+            "movn   %[qc1], $t4,    $t0     \n\t"
+            "movn   %[qc2], $t5,    $t1     \n\t"
+            "movn   %[qc3], $t6,    $t2     \n\t"
+            "movn   %[qc4], $t7,    $t3     \n\t"
+
+            ".set pop                       \n\t"
+
+            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
+              [qc3]"+r"(qc3), [qc4]"+r"(qc4)
+            : [in_int]"r"(in_int)
+            : "t0", "t1", "t2", "t3",
+              "t4", "t5", "t6", "t7",
+              "memory"
+        );
+
+        curidx = 9 * qc1;
+        curidx += qc2 + 40;
+
+        curidx2 = 9 * qc3;
+        curidx2 += qc4 + 40;
+
+        v_codes = (p_codes[curidx] << p_bits[curidx2]) | (p_codes[curidx2]);
+        v_bits  = p_bits[curidx] + p_bits[curidx2];
+        put_bits(pb, v_bits, v_codes);
+    }
+}
+
+static void quantize_and_encode_band_cost_UPAIR7_mips(struct AACEncContext *s,
+                                                      PutBitContext *pb, const float *in,
+                                                      const float *scaled, int size, int scale_idx,
+                                                      int cb, const float lambda, const float uplim,
+                                                      int *bits)
+{
+    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
+    int i;
+    int qc1, qc2, qc3, qc4;
+
+    uint8_t  *p_bits  = (uint8_t*) ff_aac_spectral_bits[cb-1];
+    uint16_t *p_codes = (uint16_t*)ff_aac_spectral_codes[cb-1];
+
+    abs_pow34_v(s->scoefs, in, size);
+    scaled = s->scoefs;
+    for (i = 0; i < size; i += 4) {
+        int curidx, sign1, count1, sign2, count2;
+        int *in_int = (int *)&in[i];
+        uint8_t v_bits;
+        unsigned int v_codes;
+
+        qc1 = scaled[i  ] * Q34 + 0.4054f;
+        qc2 = scaled[i+1] * Q34 + 0.4054f;
+        qc3 = scaled[i+2] * Q34 + 0.4054f;
+        qc4 = scaled[i+3] * Q34 + 0.4054f;
+
+        __asm__ volatile (
+            ".set push                              \n\t"
+            ".set noreorder                         \n\t"
+
+            "ori    $t4,        $zero,      7       \n\t"
+            "ori    %[sign1],   $zero,      0       \n\t"
+            "ori    %[sign2],   $zero,      0       \n\t"
+            "slt    $t0,        $t4,        %[qc1]  \n\t"
+            "slt    $t1,        $t4,        %[qc2]  \n\t"
+            "slt    $t2,        $t4,        %[qc3]  \n\t"
+            "slt    $t3,        $t4,        %[qc4]  \n\t"
+            "movn   %[qc1],     $t4,        $t0     \n\t"
+            "movn   %[qc2],     $t4,        $t1     \n\t"
+            "movn   %[qc3],     $t4,        $t2     \n\t"
+            "movn   %[qc4],     $t4,        $t3     \n\t"
+            "lw     $t0,        0(%[in_int])        \n\t"
+            "lw     $t1,        4(%[in_int])        \n\t"
+            "lw     $t2,        8(%[in_int])        \n\t"
+            "lw     $t3,        12(%[in_int])       \n\t"
+            "slt    $t0,        $t0,        $zero   \n\t"
+            "movn   %[sign1],   $t0,        %[qc1]  \n\t"
+            "slt    $t2,        $t2,        $zero   \n\t"
+            "movn   %[sign2],   $t2,        %[qc3]  \n\t"
+            "slt    $t1,        $t1,        $zero   \n\t"
+            "sll    $t0,        %[sign1],   1       \n\t"
+            "or     $t0,        $t0,        $t1     \n\t"
+            "movn   %[sign1],   $t0,        %[qc2]  \n\t"
+            "slt    $t3,        $t3,        $zero   \n\t"
+            "sll    $t0,        %[sign2],   1       \n\t"
+            "or     $t0,        $t0,        $t3     \n\t"
+            "movn   %[sign2],   $t0,        %[qc4]  \n\t"
+            "slt    %[count1],  $zero,      %[qc1]  \n\t"
+            "slt    $t1,        $zero,      %[qc2]  \n\t"
+            "slt    %[count2],  $zero,      %[qc3]  \n\t"
+            "slt    $t2,        $zero,      %[qc4]  \n\t"
+            "addu   %[count1],  %[count1],  $t1     \n\t"
+            "addu   %[count2],  %[count2],  $t2     \n\t"
+
+            ".set pop                               \n\t"
+
+            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
+              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
+              [sign1]"=&r"(sign1), [count1]"=&r"(count1),
+              [sign2]"=&r"(sign2), [count2]"=&r"(count2)
+            : [in_int]"r"(in_int)
+            : "t0", "t1", "t2", "t3", "t4",
+              "memory"
+        );
+
+        curidx  = 8 * qc1;
+        curidx += qc2;
+
+        v_codes = (p_codes[curidx] << count1) | sign1;
+        v_bits  = p_bits[curidx] + count1;
+        put_bits(pb, v_bits, v_codes);
+
+        curidx  = 8 * qc3;
+        curidx += qc4;
+
+        v_codes = (p_codes[curidx] << count2) | sign2;
+        v_bits  = p_bits[curidx] + count2;
+        put_bits(pb, v_bits, v_codes);
+    }
+}
+
+static void quantize_and_encode_band_cost_UPAIR12_mips(struct AACEncContext *s,
+                                                       PutBitContext *pb, const float *in,
+                                                       const float *scaled, int size, int scale_idx,
+                                                       int cb, const float lambda, const float uplim,
+                                                       int *bits)
+{
+    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
+    int i;
+    int qc1, qc2, qc3, qc4;
+
+    uint8_t  *p_bits  = (uint8_t*) ff_aac_spectral_bits[cb-1];
+    uint16_t *p_codes = (uint16_t*)ff_aac_spectral_codes[cb-1];
+
+    abs_pow34_v(s->scoefs, in, size);
+    scaled = s->scoefs;
+    for (i = 0; i < size; i += 4) {
+        int curidx, sign1, count1, sign2, count2;
+        int *in_int = (int *)&in[i];
+        uint8_t v_bits;
+        unsigned int v_codes;
+
+        qc1 = scaled[i  ] * Q34 + 0.4054f;
+        qc2 = scaled[i+1] * Q34 + 0.4054f;
+        qc3 = scaled[i+2] * Q34 + 0.4054f;
+        qc4 = scaled[i+3] * Q34 + 0.4054f;
+
+        __asm__ volatile (
+            ".set push                              \n\t"
+            ".set noreorder                         \n\t"
+
+            "ori    $t4,        $zero,      12      \n\t"
+            "ori    %[sign1],   $zero,      0       \n\t"
+            "ori    %[sign2],   $zero,      0       \n\t"
+            "slt    $t0,        $t4,        %[qc1]  \n\t"
+            "slt    $t1,        $t4,        %[qc2]  \n\t"
+            "slt    $t2,        $t4,        %[qc3]  \n\t"
+            "slt    $t3,        $t4,        %[qc4]  \n\t"
+            "movn   %[qc1],     $t4,        $t0     \n\t"
+            "movn   %[qc2],     $t4,        $t1     \n\t"
+            "movn   %[qc3],     $t4,        $t2     \n\t"
+            "movn   %[qc4],     $t4,        $t3     \n\t"
+            "lw     $t0,        0(%[in_int])        \n\t"
+            "lw     $t1,        4(%[in_int])        \n\t"
+            "lw     $t2,        8(%[in_int])        \n\t"
+            "lw     $t3,        12(%[in_int])       \n\t"
+            "slt    $t0,        $t0,        $zero   \n\t"
+            "movn   %[sign1],   $t0,        %[qc1]  \n\t"
+            "slt    $t2,        $t2,        $zero   \n\t"
+            "movn   %[sign2],   $t2,        %[qc3]  \n\t"
+            "slt    $t1,        $t1,        $zero   \n\t"
+            "sll    $t0,        %[sign1],   1       \n\t"
+            "or     $t0,        $t0,        $t1     \n\t"
+            "movn   %[sign1],   $t0,        %[qc2]  \n\t"
+            "slt    $t3,        $t3,        $zero   \n\t"
+            "sll    $t0,        %[sign2],   1       \n\t"
+            "or     $t0,        $t0,        $t3     \n\t"
+            "movn   %[sign2],   $t0,        %[qc4]  \n\t"
+            "slt    %[count1],  $zero,      %[qc1]  \n\t"
+            "slt    $t1,        $zero,      %[qc2]  \n\t"
+            "slt    %[count2],  $zero,      %[qc3]  \n\t"
+            "slt    $t2,        $zero,      %[qc4]  \n\t"
+            "addu   %[count1],  %[count1],  $t1     \n\t"
+            "addu   %[count2],  %[count2],  $t2     \n\t"
+
+            ".set pop                               \n\t"
+
+            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
+              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
+              [sign1]"=&r"(sign1), [count1]"=&r"(count1),
+              [sign2]"=&r"(sign2), [count2]"=&r"(count2)
+            : [in_int]"r"(in_int)
+            : "t0", "t1", "t2", "t3", "t4",
+              "memory"
+        );
+
+        curidx  = 13 * qc1;
+        curidx += qc2;
+
+        v_codes = (p_codes[curidx] << count1) | sign1;
+        v_bits  = p_bits[curidx] + count1;
+        put_bits(pb, v_bits, v_codes);
+
+        curidx  = 13 * qc3;
+        curidx += qc4;
+
+        v_codes = (p_codes[curidx] << count2) | sign2;
+        v_bits  = p_bits[curidx] + count2;
+        put_bits(pb, v_bits, v_codes);
+    }
+}
+
+static void quantize_and_encode_band_cost_ESC_mips(struct AACEncContext *s,
+                                                   PutBitContext *pb, const float *in,
+                                                   const float *scaled, int size, int scale_idx,
+                                                   int cb, const float lambda, const float uplim,
+                                                   int *bits)
+{
+    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
+    int i;
+    int qc1, qc2, qc3, qc4;
+
+    uint8_t  *p_bits    = (uint8_t* )ff_aac_spectral_bits[cb-1];
+    uint16_t *p_codes   = (uint16_t*)ff_aac_spectral_codes[cb-1];
+    float    *p_vectors = (float*   )ff_aac_codebook_vectors[cb-1];
+
+    abs_pow34_v(s->scoefs, in, size);
+    scaled = s->scoefs;
+
+    if (cb < 11) {
+        for (i = 0; i < size; i += 4) {
+            int curidx, curidx2, sign1, count1, sign2, count2;
+            int *in_int = (int *)&in[i];
+            uint8_t v_bits;
+            unsigned int v_codes;
+
+            qc1 = scaled[i  ] * Q34 + 0.4054f;
+            qc2 = scaled[i+1] * Q34 + 0.4054f;
+            qc3 = scaled[i+2] * Q34 + 0.4054f;
+            qc4 = scaled[i+3] * Q34 + 0.4054f;
+
+            __asm__ volatile (
+                ".set push                                  \n\t"
+                ".set noreorder                             \n\t"
+
+                "ori        $t4,        $zero,      16      \n\t"
+                "ori        %[sign1],   $zero,      0       \n\t"
+                "ori        %[sign2],   $zero,      0       \n\t"
+                "slt        $t0,        $t4,        %[qc1]  \n\t"
+                "slt        $t1,        $t4,        %[qc2]  \n\t"
+                "slt        $t2,        $t4,        %[qc3]  \n\t"
+                "slt        $t3,        $t4,        %[qc4]  \n\t"
+                "movn       %[qc1],     $t4,        $t0     \n\t"
+                "movn       %[qc2],     $t4,        $t1     \n\t"
+                "movn       %[qc3],     $t4,        $t2     \n\t"
+                "movn       %[qc4],     $t4,        $t3     \n\t"
+                "lw         $t0,        0(%[in_int])        \n\t"
+                "lw         $t1,        4(%[in_int])        \n\t"
+                "lw         $t2,        8(%[in_int])        \n\t"
+                "lw         $t3,        12(%[in_int])       \n\t"
+                "slt        $t0,        $t0,        $zero   \n\t"
+                "movn       %[sign1],   $t0,        %[qc1]  \n\t"
+                "slt        $t2,        $t2,        $zero   \n\t"
+                "movn       %[sign2],   $t2,        %[qc3]  \n\t"
+                "slt        $t1,        $t1,        $zero   \n\t"
+                "sll        $t0,        %[sign1],   1       \n\t"
+                "or         $t0,        $t0,        $t1     \n\t"
+                "movn       %[sign1],   $t0,        %[qc2]  \n\t"
+                "slt        $t3,        $t3,        $zero   \n\t"
+                "sll        $t0,        %[sign2],   1       \n\t"
+                "or         $t0,        $t0,        $t3     \n\t"
+                "movn       %[sign2],   $t0,        %[qc4]  \n\t"
+                "slt        %[count1],  $zero,      %[qc1]  \n\t"
+                "slt        $t1,        $zero,      %[qc2]  \n\t"
+                "slt        %[count2],  $zero,      %[qc3]  \n\t"
+                "slt        $t2,        $zero,      %[qc4]  \n\t"
+                "addu       %[count1],  %[count1],  $t1     \n\t"
+                "addu       %[count2],  %[count2],  $t2     \n\t"
+
+                ".set pop                                   \n\t"
+
+                : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
+                  [qc3]"+r"(qc3), [qc4]"+r"(qc4),
+                  [sign1]"=&r"(sign1), [count1]"=&r"(count1),
+                  [sign2]"=&r"(sign2), [count2]"=&r"(count2)
+                : [in_int]"r"(in_int)
+                : "t0", "t1", "t2", "t3", "t4",
+                  "memory"
+            );
+
+            curidx = 17 * qc1;
+            curidx += qc2;
+            curidx2 = 17 * qc3;
+            curidx2 += qc4;
+
+            v_codes = (p_codes[curidx] << count1) | sign1;
+            v_bits  = p_bits[curidx] + count1;
+            put_bits(pb, v_bits, v_codes);
+
+            v_codes = (p_codes[curidx2] << count2) | sign2;
+            v_bits  = p_bits[curidx2] + count2;
+            put_bits(pb, v_bits, v_codes);
+        }
+    } else {
+        for (i = 0; i < size; i += 4) {
+            int curidx, curidx2, sign1, count1, sign2, count2;
+            int *in_int = (int *)&in[i];
+            uint8_t v_bits;
+            unsigned int v_codes;
+            int c1, c2, c3, c4;
+
+            qc1 = scaled[i  ] * Q34 + 0.4054f;
+            qc2 = scaled[i+1] * Q34 + 0.4054f;
+            qc3 = scaled[i+2] * Q34 + 0.4054f;
+            qc4 = scaled[i+3] * Q34 + 0.4054f;
+
+            __asm__ volatile (
+                ".set push                                  \n\t"
+                ".set noreorder                             \n\t"
+
+                "ori        $t4,        $zero,      16      \n\t"
+                "ori        %[sign1],   $zero,      0       \n\t"
+                "ori        %[sign2],   $zero,      0       \n\t"
+                "shll_s.w   %[c1],      %[qc1],     18      \n\t"
+                "shll_s.w   %[c2],      %[qc2],     18      \n\t"
+                "shll_s.w   %[c3],      %[qc3],     18      \n\t"
+                "shll_s.w   %[c4],      %[qc4],     18      \n\t"
+                "srl        %[c1],      %[c1],      18      \n\t"
+                "srl        %[c2],      %[c2],      18      \n\t"
+                "srl        %[c3],      %[c3],      18      \n\t"
+                "srl        %[c4],      %[c4],      18      \n\t"
+                "slt        $t0,        $t4,        %[qc1]  \n\t"
+                "slt        $t1,        $t4,        %[qc2]  \n\t"
+                "slt        $t2,        $t4,        %[qc3]  \n\t"
+                "slt        $t3,        $t4,        %[qc4]  \n\t"
+                "movn       %[qc1],     $t4,        $t0     \n\t"
+                "movn       %[qc2],     $t4,        $t1     \n\t"
+                "movn       %[qc3],     $t4,        $t2     \n\t"
+                "movn       %[qc4],     $t4,        $t3     \n\t"
+                "lw         $t0,        0(%[in_int])        \n\t"
+                "lw         $t1,        4(%[in_int])        \n\t"
+                "lw         $t2,        8(%[in_int])        \n\t"
+                "lw         $t3,        12(%[in_int])       \n\t"
+                "slt        $t0,        $t0,        $zero   \n\t"
+                "movn       %[sign1],   $t0,        %[qc1]  \n\t"
+                "slt        $t2,        $t2,        $zero   \n\t"
+                "movn       %[sign2],   $t2,        %[qc3]  \n\t"
+                "slt        $t1,        $t1,        $zero   \n\t"
+                "sll        $t0,        %[sign1],   1       \n\t"
+                "or         $t0,        $t0,        $t1     \n\t"
+                "movn       %[sign1],   $t0,        %[qc2]  \n\t"
+                "slt        $t3,        $t3,        $zero   \n\t"
+                "sll        $t0,        %[sign2],   1       \n\t"
+                "or         $t0,        $t0,        $t3     \n\t"
+                "movn       %[sign2],   $t0,        %[qc4]  \n\t"
+                "slt        %[count1],  $zero,      %[qc1]  \n\t"
+                "slt        $t1,        $zero,      %[qc2]  \n\t"
+                "slt        %[count2],  $zero,      %[qc3]  \n\t"
+                "slt        $t2,        $zero,      %[qc4]  \n\t"
+                "addu       %[count1],  %[count1],  $t1     \n\t"
+                "addu       %[count2],  %[count2],  $t2     \n\t"
+
+                ".set pop                                   \n\t"
+
+                : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
+                  [qc3]"+r"(qc3), [qc4]"+r"(qc4),
+                  [sign1]"=&r"(sign1), [count1]"=&r"(count1),
+                  [sign2]"=&r"(sign2), [count2]"=&r"(count2),
+                  [c1]"=&r"(c1), [c2]"=&r"(c2),
+                  [c3]"=&r"(c3), [c4]"=&r"(c4)
+                : [in_int]"r"(in_int)
+                : "t0", "t1", "t2", "t3", "t4",
+                  "memory"
+            );
+
+            curidx = 17 * qc1;
+            curidx += qc2;
+
+            curidx2 = 17 * qc3;
+            curidx2 += qc4;
+
+            v_codes = (p_codes[curidx] << count1) | sign1;
+            v_bits  = p_bits[curidx] + count1;
+            put_bits(pb, v_bits, v_codes);
+
+            if (p_vectors[curidx*2  ] == 64.0f) {
+                int len = av_log2(c1);
+                v_codes = (((1 << (len - 3)) - 2) << len) | (c1 & ((1 << len) - 1));
+                put_bits(pb, len * 2 - 3, v_codes);
+            }
+            if (p_vectors[curidx*2+1] == 64.0f) {
+                int len = av_log2(c2);
+                v_codes = (((1 << (len - 3)) - 2) << len) | (c2 & ((1 << len) - 1));
+                put_bits(pb, len*2-3, v_codes);
+            }
+
+            v_codes = (p_codes[curidx2] << count2) | sign2;
+            v_bits  = p_bits[curidx2] + count2;
+            put_bits(pb, v_bits, v_codes);
+
+            if (p_vectors[curidx2*2  ] == 64.0f) {
+                int len = av_log2(c3);
+                v_codes = (((1 << (len - 3)) - 2) << len) | (c3 & ((1 << len) - 1));
+                put_bits(pb, len* 2 - 3, v_codes);
+            }
+            if (p_vectors[curidx2*2+1] == 64.0f) {
+                int len = av_log2(c4);
+                v_codes = (((1 << (len - 3)) - 2) << len) | (c4 & ((1 << len) - 1));
+                put_bits(pb, len * 2 - 3, v_codes);
+            }
+        }
+    }
+}
+
+static void (*const quantize_and_encode_band_cost_arr[])(struct AACEncContext *s,
+                                                         PutBitContext *pb, const float *in,
+                                                         const float *scaled, int size, int scale_idx,
+                                                         int cb, const float lambda, const float uplim,
+                                                         int *bits) = {
+    NULL,
+    quantize_and_encode_band_cost_SQUAD_mips,
+    quantize_and_encode_band_cost_SQUAD_mips,
+    quantize_and_encode_band_cost_UQUAD_mips,
+    quantize_and_encode_band_cost_UQUAD_mips,
+    quantize_and_encode_band_cost_SPAIR_mips,
+    quantize_and_encode_band_cost_SPAIR_mips,
+    quantize_and_encode_band_cost_UPAIR7_mips,
+    quantize_and_encode_band_cost_UPAIR7_mips,
+    quantize_and_encode_band_cost_UPAIR12_mips,
+    quantize_and_encode_band_cost_UPAIR12_mips,
+    quantize_and_encode_band_cost_ESC_mips,
+};
+
+#define quantize_and_encode_band_cost(                                  \
+                                s, pb, in, scaled, size, scale_idx, cb, \
+                                lambda, uplim, bits)                    \
+    quantize_and_encode_band_cost_arr[cb](                              \
+                                s, pb, in, scaled, size, scale_idx, cb, \
+                                lambda, uplim, bits)
+
+static void quantize_and_encode_band_mips(struct AACEncContext *s, PutBitContext *pb,
+                                          const float *in, int size, int scale_idx,
+                                          int cb, const float lambda)
+{
+    quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda,
+                                  INFINITY, NULL);
+}
+
+/**
+ * Functions developed from template function and optimized for getting the number of bits
+ */
+static float get_band_numbits_ZERO_mips(struct AACEncContext *s,
+                                        PutBitContext *pb, const float *in,
+                                        const float *scaled, int size, int scale_idx,
+                                        int cb, const float lambda, const float uplim,
+                                        int *bits)
+{
+    return 0;
+}
+
+static float get_band_numbits_SQUAD_mips(struct AACEncContext *s,
+                                         PutBitContext *pb, const float *in,
+                                         const float *scaled, int size, int scale_idx,
+                                         int cb, const float lambda, const float uplim,
+                                         int *bits)
+{
+    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
+    int i;
+    int qc1, qc2, qc3, qc4;
+    int curbits = 0;
+
+    uint8_t *p_bits = (uint8_t *)ff_aac_spectral_bits[cb-1];
+
+    for (i = 0; i < size; i += 4) {
+        int curidx;
+        int *in_int = (int *)&in[i];
+
+        qc1 = scaled[i  ] * Q34 + 0.4054f;
+        qc2 = scaled[i+1] * Q34 + 0.4054f;
+        qc3 = scaled[i+2] * Q34 + 0.4054f;
+        qc4 = scaled[i+3] * Q34 + 0.4054f;
+
+        __asm__ volatile (
+            ".set push                      \n\t"
+            ".set noreorder                 \n\t"
+
+            "slt    %[qc1], $zero,  %[qc1]  \n\t"
+            "slt    %[qc2], $zero,  %[qc2]  \n\t"
+            "slt    %[qc3], $zero,  %[qc3]  \n\t"
+            "slt    %[qc4], $zero,  %[qc4]  \n\t"
+            "lw     $t0,    0(%[in_int])    \n\t"
+            "lw     $t1,    4(%[in_int])    \n\t"
+            "lw     $t2,    8(%[in_int])    \n\t"
+            "lw     $t3,    12(%[in_int])   \n\t"
+            "srl    $t0,    $t0,    31      \n\t"
+            "srl    $t1,    $t1,    31      \n\t"
+            "srl    $t2,    $t2,    31      \n\t"
+            "srl    $t3,    $t3,    31      \n\t"
+            "subu   $t4,    $zero,  %[qc1]  \n\t"
+            "subu   $t5,    $zero,  %[qc2]  \n\t"
+            "subu   $t6,    $zero,  %[qc3]  \n\t"
+            "subu   $t7,    $zero,  %[qc4]  \n\t"
+            "movn   %[qc1], $t4,    $t0     \n\t"
+            "movn   %[qc2], $t5,    $t1     \n\t"
+            "movn   %[qc3], $t6,    $t2     \n\t"
+            "movn   %[qc4], $t7,    $t3     \n\t"
+
+            ".set pop                       \n\t"
+
+            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
+              [qc3]"+r"(qc3), [qc4]"+r"(qc4)
+            : [in_int]"r"(in_int)
+            : "t0", "t1", "t2", "t3",
+              "t4", "t5", "t6", "t7",
+              "memory"
+        );
+
+        curidx = qc1;
+        curidx *= 3;
+        curidx += qc2;
+        curidx *= 3;
+        curidx += qc3;
+        curidx *= 3;
+        curidx += qc4;
+        curidx += 40;
+
+        curbits += p_bits[curidx];
+    }
+    return curbits;
+}
+
+static float get_band_numbits_UQUAD_mips(struct AACEncContext *s,
+                                         PutBitContext *pb, const float *in,
+                                         const float *scaled, int size, int scale_idx,
+                                         int cb, const float lambda, const float uplim,
+                                         int *bits)
+{
+    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
+    int i;
+    int curbits = 0;
+    int qc1, qc2, qc3, qc4;
+
+    uint8_t *p_bits = (uint8_t *)ff_aac_spectral_bits[cb-1];
+
+    for (i = 0; i < size; i += 4) {
+        int curidx;
+
+        qc1 = scaled[i  ] * Q34 + 0.4054f;
+        qc2 = scaled[i+1] * Q34 + 0.4054f;
+        qc3 = scaled[i+2] * Q34 + 0.4054f;
+        qc4 = scaled[i+3] * Q34 + 0.4054f;
+
+        __asm__ volatile (
+            ".set push                      \n\t"
+            ".set noreorder                 \n\t"
+
+            "ori    $t4,    $zero,  2       \n\t"
+            "slt    $t0,    $t4,    %[qc1]  \n\t"
+            "slt    $t1,    $t4,    %[qc2]  \n\t"
+            "slt    $t2,    $t4,    %[qc3]  \n\t"
+            "slt    $t3,    $t4,    %[qc4]  \n\t"
+            "movn   %[qc1], $t4,    $t0     \n\t"
+            "movn   %[qc2], $t4,    $t1     \n\t"
+            "movn   %[qc3], $t4,    $t2     \n\t"
+            "movn   %[qc4], $t4,    $t3     \n\t"
+
+            ".set pop                       \n\t"
+
+            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
+              [qc3]"+r"(qc3), [qc4]"+r"(qc4)
+            :
+            : "t0", "t1", "t2", "t3", "t4"
+        );
+
+        curidx = qc1;
+        curidx *= 3;
+        curidx += qc2;
+        curidx *= 3;
+        curidx += qc3;
+        curidx *= 3;
+        curidx += qc4;
+
+        curbits += p_bits[curidx];
+        curbits += uquad_sign_bits[curidx];
+    }
+    return curbits;
+}
+
+static float get_band_numbits_SPAIR_mips(struct AACEncContext *s,
+                                         PutBitContext *pb, const float *in,
+                                         const float *scaled, int size, int scale_idx,
+                                         int cb, const float lambda, const float uplim,
+                                         int *bits)
+{
+    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
+    int i;
+    int qc1, qc2, qc3, qc4;
+    int curbits = 0;
+
+    uint8_t *p_bits = (uint8_t*)ff_aac_spectral_bits[cb-1];
+
+    for (i = 0; i < size; i += 4) {
+        int curidx, curidx2;
+        int *in_int = (int *)&in[i];
+
+        qc1 = scaled[i  ] * Q34 + 0.4054f;
+        qc2 = scaled[i+1] * Q34 + 0.4054f;
+        qc3 = scaled[i+2] * Q34 + 0.4054f;
+        qc4 = scaled[i+3] * Q34 + 0.4054f;
+
+        __asm__ volatile (
+            ".set push                      \n\t"
+            ".set noreorder                 \n\t"
+
+            "ori    $t4,    $zero,  4       \n\t"
+            "slt    $t0,    $t4,    %[qc1]  \n\t"
+            "slt    $t1,    $t4,    %[qc2]  \n\t"
+            "slt    $t2,    $t4,    %[qc3]  \n\t"
+            "slt    $t3,    $t4,    %[qc4]  \n\t"
+            "movn   %[qc1], $t4,    $t0     \n\t"
+            "movn   %[qc2], $t4,    $t1     \n\t"
+            "movn   %[qc3], $t4,    $t2     \n\t"
+            "movn   %[qc4], $t4,    $t3     \n\t"
+            "lw     $t0,    0(%[in_int])    \n\t"
+            "lw     $t1,    4(%[in_int])    \n\t"
+            "lw     $t2,    8(%[in_int])    \n\t"
+            "lw     $t3,    12(%[in_int])   \n\t"
+            "srl    $t0,    $t0,    31      \n\t"
+            "srl    $t1,    $t1,    31      \n\t"
+            "srl    $t2,    $t2,    31      \n\t"
+            "srl    $t3,    $t3,    31      \n\t"
+            "subu   $t4,    $zero,  %[qc1]  \n\t"
+            "subu   $t5,    $zero,  %[qc2]  \n\t"
+            "subu   $t6,    $zero,  %[qc3]  \n\t"
+            "subu   $t7,    $zero,  %[qc4]  \n\t"
+            "movn   %[qc1], $t4,    $t0     \n\t"
+            "movn   %[qc2], $t5,    $t1     \n\t"
+            "movn   %[qc3], $t6,    $t2     \n\t"
+            "movn   %[qc4], $t7,    $t3     \n\t"
+
+            ".set pop                       \n\t"
+
+            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
+              [qc3]"+r"(qc3), [qc4]"+r"(qc4)
+            : [in_int]"r"(in_int)
+            : "t0", "t1", "t2", "t3",
+              "t4", "t5", "t6", "t7",
+              "memory"
+        );
+
+        curidx  = 9 * qc1;
+        curidx += qc2 + 40;
+
+        curidx2  = 9 * qc3;
+        curidx2 += qc4 + 40;
+
+        curbits += p_bits[curidx] + p_bits[curidx2];
+    }
+    return curbits;
+}
+
+static float get_band_numbits_UPAIR7_mips(struct AACEncContext *s,
+                                          PutBitContext *pb, const float *in,
+                                          const float *scaled, int size, int scale_idx,
+                                          int cb, const float lambda, const float uplim,
+                                          int *bits)
+{
+    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
+    int i;
+    int qc1, qc2, qc3, qc4;
+    int curbits = 0;
+
+    uint8_t *p_bits = (uint8_t *)ff_aac_spectral_bits[cb-1];
+
+    for (i = 0; i < size; i += 4) {
+        int curidx, curidx2;
+
+        qc1 = scaled[i  ] * Q34 + 0.4054f;
+        qc2 = scaled[i+1] * Q34 + 0.4054f;
+        qc3 = scaled[i+2] * Q34 + 0.4054f;
+        qc4 = scaled[i+3] * Q34 + 0.4054f;
+
+        __asm__ volatile (
+            ".set push                      \n\t"
+            ".set noreorder                 \n\t"
+
+            "ori    $t4,    $zero,  7       \n\t"
+            "slt    $t0,    $t4,    %[qc1]  \n\t"
+            "slt    $t1,    $t4,    %[qc2]  \n\t"
+            "slt    $t2,    $t4,    %[qc3]  \n\t"
+            "slt    $t3,    $t4,    %[qc4]  \n\t"
+            "movn   %[qc1], $t4,    $t0     \n\t"
+            "movn   %[qc2], $t4,    $t1     \n\t"
+            "movn   %[qc3], $t4,    $t2     \n\t"
+            "movn   %[qc4], $t4,    $t3     \n\t"
+
+            ".set pop                       \n\t"
+
+            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
+              [qc3]"+r"(qc3), [qc4]"+r"(qc4)
+            :
+            : "t0", "t1", "t2", "t3", "t4"
+        );
+
+        curidx  = 8 * qc1;
+        curidx += qc2;
+
+        curidx2  = 8 * qc3;
+        curidx2 += qc4;
+
+        curbits += p_bits[curidx] +
+                   upair7_sign_bits[curidx] +
+                   p_bits[curidx2] +
+                   upair7_sign_bits[curidx2];
+    }
+    return curbits;
+}
+
+static float get_band_numbits_UPAIR12_mips(struct AACEncContext *s,
+                                           PutBitContext *pb, const float *in,
+                                           const float *scaled, int size, int scale_idx,
+                                           int cb, const float lambda, const float uplim,
+                                           int *bits)
+{
+    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
+    int i;
+    int qc1, qc2, qc3, qc4;
+    int curbits = 0;
+
+    uint8_t *p_bits = (uint8_t *)ff_aac_spectral_bits[cb-1];
+
+    for (i = 0; i < size; i += 4) {
+        int curidx, curidx2;
+
+        qc1 = scaled[i  ] * Q34 + 0.4054f;
+        qc2 = scaled[i+1] * Q34 + 0.4054f;
+        qc3 = scaled[i+2] * Q34 + 0.4054f;
+        qc4 = scaled[i+3] * Q34 + 0.4054f;
+
+        __asm__ volatile (
+            ".set push                      \n\t"
+            ".set noreorder                 \n\t"
+
+            "ori    $t4,    $zero,  12      \n\t"
+            "slt    $t0,    $t4,    %[qc1]  \n\t"
+            "slt    $t1,    $t4,    %[qc2]  \n\t"
+            "slt    $t2,    $t4,    %[qc3]  \n\t"
+            "slt    $t3,    $t4,    %[qc4]  \n\t"
+            "movn   %[qc1], $t4,    $t0     \n\t"
+            "movn   %[qc2], $t4,    $t1     \n\t"
+            "movn   %[qc3], $t4,    $t2     \n\t"
+            "movn   %[qc4], $t4,    $t3     \n\t"
+
+            ".set pop                       \n\t"
+
+            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
+              [qc3]"+r"(qc3), [qc4]"+r"(qc4)
+            :
+            : "t0", "t1", "t2", "t3", "t4"
+        );
+
+        curidx  = 13 * qc1;
+        curidx += qc2;
+
+        curidx2  = 13 * qc3;
+        curidx2 += qc4;
+
+        curbits += p_bits[curidx] +
+                   p_bits[curidx2] +
+                   upair12_sign_bits[curidx] +
+                   upair12_sign_bits[curidx2];
+    }
+    return curbits;
+}
+
+static float get_band_numbits_ESC_mips(struct AACEncContext *s,
+                                       PutBitContext *pb, const float *in,
+                                       const float *scaled, int size, int scale_idx,
+                                       int cb, const float lambda, const float uplim,
+                                       int *bits)
+{
+    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
+    int i;
+    int qc1, qc2, qc3, qc4;
+    int curbits = 0;
+
+    uint8_t *p_bits = (uint8_t*)ff_aac_spectral_bits[cb-1];
+
+    for (i = 0; i < size; i += 4) {
+        int curidx, curidx2;
+        int cond0, cond1, cond2, cond3;
+        int c1, c2, c3, c4;
+
+        qc1 = scaled[i  ] * Q34 + 0.4054f;
+        qc2 = scaled[i+1] * Q34 + 0.4054f;
+        qc3 = scaled[i+2] * Q34 + 0.4054f;
+        qc4 = scaled[i+3] * Q34 + 0.4054f;
+
+        __asm__ volatile (
+            ".set push                                  \n\t"
+            ".set noreorder                             \n\t"
+
+            "ori        $t4,        $zero,  15          \n\t"
+            "ori        $t5,        $zero,  16          \n\t"
+            "shll_s.w   %[c1],      %[qc1], 18          \n\t"
+            "shll_s.w   %[c2],      %[qc2], 18          \n\t"
+            "shll_s.w   %[c3],      %[qc3], 18          \n\t"
+            "shll_s.w   %[c4],      %[qc4], 18          \n\t"
+            "srl        %[c1],      %[c1],  18          \n\t"
+            "srl        %[c2],      %[c2],  18          \n\t"
+            "srl        %[c3],      %[c3],  18          \n\t"
+            "srl        %[c4],      %[c4],  18          \n\t"
+            "slt        %[cond0],   $t4,    %[qc1]      \n\t"
+            "slt        %[cond1],   $t4,    %[qc2]      \n\t"
+            "slt        %[cond2],   $t4,    %[qc3]      \n\t"
+            "slt        %[cond3],   $t4,    %[qc4]      \n\t"
+            "movn       %[qc1],     $t5,    %[cond0]    \n\t"
+            "movn       %[qc2],     $t5,    %[cond1]    \n\t"
+            "movn       %[qc3],     $t5,    %[cond2]    \n\t"
+            "movn       %[qc4],     $t5,    %[cond3]    \n\t"
+            "ori        $t5,        $zero,  31          \n\t"
+            "clz        %[c1],      %[c1]               \n\t"
+            "clz        %[c2],      %[c2]               \n\t"
+            "clz        %[c3],      %[c3]               \n\t"
+            "clz        %[c4],      %[c4]               \n\t"
+            "subu       %[c1],      $t5,    %[c1]       \n\t"
+            "subu       %[c2],      $t5,    %[c2]       \n\t"
+            "subu       %[c3],      $t5,    %[c3]       \n\t"
+            "subu       %[c4],      $t5,    %[c4]       \n\t"
+            "sll        %[c1],      %[c1],  1           \n\t"
+            "sll        %[c2],      %[c2],  1           \n\t"
+            "sll        %[c3],      %[c3],  1           \n\t"
+            "sll        %[c4],      %[c4],  1           \n\t"
+            "addiu      %[c1],      %[c1],  -3          \n\t"
+            "addiu      %[c2],      %[c2],  -3          \n\t"
+            "addiu      %[c3],      %[c3],  -3          \n\t"
+            "addiu      %[c4],      %[c4],  -3          \n\t"
+            "subu       %[cond0],   $zero,  %[cond0]    \n\t"
+            "subu       %[cond1],   $zero,  %[cond1]    \n\t"
+            "subu       %[cond2],   $zero,  %[cond2]    \n\t"
+            "subu       %[cond3],   $zero,  %[cond3]    \n\t"
+            "and        %[c1],      %[c1],  %[cond0]    \n\t"
+            "and        %[c2],      %[c2],  %[cond1]    \n\t"
+            "and        %[c3],      %[c3],  %[cond2]    \n\t"
+            "and        %[c4],      %[c4],  %[cond3]    \n\t"
+
+            ".set pop                                   \n\t"
+
+            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
+              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
+              [cond0]"=&r"(cond0), [cond1]"=&r"(cond1),
+              [cond2]"=&r"(cond2), [cond3]"=&r"(cond3),
+              [c1]"=&r"(c1), [c2]"=&r"(c2),
+              [c3]"=&r"(c3), [c4]"=&r"(c4)
+            :
+            : "t4", "t5"
+        );
+
+        curidx = 17 * qc1;
+        curidx += qc2;
+
+        curidx2 = 17 * qc3;
+        curidx2 += qc4;
+
+        curbits += p_bits[curidx];
+        curbits += esc_sign_bits[curidx];
+        curbits += p_bits[curidx2];
+        curbits += esc_sign_bits[curidx2];
+
+        curbits += c1;
+        curbits += c2;
+        curbits += c3;
+        curbits += c4;
+    }
+    return curbits;
+}
+
+static float (*const get_band_numbits_arr[])(struct AACEncContext *s,
+                                             PutBitContext *pb, const float *in,
+                                             const float *scaled, int size, int scale_idx,
+                                             int cb, const float lambda, const float uplim,
+                                             int *bits) = {
+    get_band_numbits_ZERO_mips,
+    get_band_numbits_SQUAD_mips,
+    get_band_numbits_SQUAD_mips,
+    get_band_numbits_UQUAD_mips,
+    get_band_numbits_UQUAD_mips,
+    get_band_numbits_SPAIR_mips,
+    get_band_numbits_SPAIR_mips,
+    get_band_numbits_UPAIR7_mips,
+    get_band_numbits_UPAIR7_mips,
+    get_band_numbits_UPAIR12_mips,
+    get_band_numbits_UPAIR12_mips,
+    get_band_numbits_ESC_mips,
+};
+
+#define get_band_numbits(                                  \
+                                s, pb, in, scaled, size, scale_idx, cb, \
+                                lambda, uplim, bits)                    \
+    get_band_numbits_arr[cb](                              \
+                                s, pb, in, scaled, size, scale_idx, cb, \
+                                lambda, uplim, bits)
+
+static float quantize_band_cost_bits(struct AACEncContext *s, const float *in,
+                                     const float *scaled, int size, int scale_idx,
+                                     int cb, const float lambda, const float uplim,
+                                     int *bits)
+{
+    return get_band_numbits(s, NULL, in, scaled, size, scale_idx, cb, lambda, uplim, bits);
+}
+
+/**
+ * Functions developed from template function and optimized for getting the band cost
+ */
+#if HAVE_MIPSFPU
+static float get_band_cost_ZERO_mips(struct AACEncContext *s,
+                                     PutBitContext *pb, const float *in,
+                                     const float *scaled, int size, int scale_idx,
+                                     int cb, const float lambda, const float uplim,
+                                     int *bits)
+{
+    int i;
+    float cost = 0;
+
+    for (i = 0; i < size; i += 4) {
+        cost += in[i  ] * in[i  ];
+        cost += in[i+1] * in[i+1];
+        cost += in[i+2] * in[i+2];
+        cost += in[i+3] * in[i+3];
+    }
+    if (bits)
+        *bits = 0;
+    return cost * lambda;
+}
+
+static float get_band_cost_SQUAD_mips(struct AACEncContext *s,
+                                      PutBitContext *pb, const float *in,
+                                      const float *scaled, int size, int scale_idx,
+                                      int cb, const float lambda, const float uplim,
+                                      int *bits)
+{
+    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
+    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
+    int i;
+    float cost = 0;
+    int qc1, qc2, qc3, qc4;
+    int curbits = 0;
+
+    uint8_t *p_bits  = (uint8_t *)ff_aac_spectral_bits[cb-1];
+    float   *p_codes = (float   *)ff_aac_codebook_vectors[cb-1];
+
+    for (i = 0; i < size; i += 4) {
+        const float *vec;
+        int curidx;
+        int   *in_int = (int   *)&in[i];
+        float *in_pos = (float *)&in[i];
+        float di0, di1, di2, di3;
+
+        qc1 = scaled[i  ] * Q34 + 0.4054f;
+        qc2 = scaled[i+1] * Q34 + 0.4054f;
+        qc3 = scaled[i+2] * Q34 + 0.4054f;
+        qc4 = scaled[i+3] * Q34 + 0.4054f;
+
+        __asm__ volatile (
+            ".set push                                  \n\t"
+            ".set noreorder                             \n\t"
+
+            "slt        %[qc1], $zero,  %[qc1]          \n\t"
+            "slt        %[qc2], $zero,  %[qc2]          \n\t"
+            "slt        %[qc3], $zero,  %[qc3]          \n\t"
+            "slt        %[qc4], $zero,  %[qc4]          \n\t"
+            "lw         $t0,    0(%[in_int])            \n\t"
+            "lw         $t1,    4(%[in_int])            \n\t"
+            "lw         $t2,    8(%[in_int])            \n\t"
+            "lw         $t3,    12(%[in_int])           \n\t"
+            "srl        $t0,    $t0,    31              \n\t"
+            "srl        $t1,    $t1,    31              \n\t"
+            "srl        $t2,    $t2,    31              \n\t"
+            "srl        $t3,    $t3,    31              \n\t"
+            "subu       $t4,    $zero,  %[qc1]          \n\t"
+            "subu       $t5,    $zero,  %[qc2]          \n\t"
+            "subu       $t6,    $zero,  %[qc3]          \n\t"
+            "subu       $t7,    $zero,  %[qc4]          \n\t"
+            "movn       %[qc1], $t4,    $t0             \n\t"
+            "movn       %[qc2], $t5,    $t1             \n\t"
+            "movn       %[qc3], $t6,    $t2             \n\t"
+            "movn       %[qc4], $t7,    $t3             \n\t"
+
+            ".set pop                                   \n\t"
+
+            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
+              [qc3]"+r"(qc3), [qc4]"+r"(qc4)
+            : [in_int]"r"(in_int)
+            : "t0", "t1", "t2", "t3",
+              "t4", "t5", "t6", "t7",
+              "memory"
+        );
+
+        curidx = qc1;
+        curidx *= 3;
+        curidx += qc2;
+        curidx *= 3;
+        curidx += qc3;
+        curidx *= 3;
+        curidx += qc4;
+        curidx += 40;
+
+        curbits += p_bits[curidx];
+        vec     = &p_codes[curidx*4];
+
+        __asm__ volatile (
+            ".set push                                  \n\t"
+            ".set noreorder                             \n\t"
+
+            "lwc1       $f0,    0(%[in_pos])            \n\t"
+            "lwc1       $f1,    0(%[vec])               \n\t"
+            "lwc1       $f2,    4(%[in_pos])            \n\t"
+            "lwc1       $f3,    4(%[vec])               \n\t"
+            "lwc1       $f4,    8(%[in_pos])            \n\t"
+            "lwc1       $f5,    8(%[vec])               \n\t"
+            "lwc1       $f6,    12(%[in_pos])           \n\t"
+            "lwc1       $f7,    12(%[vec])              \n\t"
+            "nmsub.s    %[di0], $f0,    $f1,    %[IQ]   \n\t"
+            "nmsub.s    %[di1], $f2,    $f3,    %[IQ]   \n\t"
+            "nmsub.s    %[di2], $f4,    $f5,    %[IQ]   \n\t"
+            "nmsub.s    %[di3], $f6,    $f7,    %[IQ]   \n\t"
+
+            ".set pop                                   \n\t"
+
+            : [di0]"=&f"(di0), [di1]"=&f"(di1),
+              [di2]"=&f"(di2), [di3]"=&f"(di3)
+            : [in_pos]"r"(in_pos), [vec]"r"(vec),
+              [IQ]"f"(IQ)
+            : "$f0", "$f1", "$f2", "$f3",
+              "$f4", "$f5", "$f6", "$f7",
+              "memory"
+        );
+
+        cost += di0 * di0 + di1 * di1
+                + di2 * di2 + di3 * di3;
+    }
+
+    if (bits)
+        *bits = curbits;
+    return cost * lambda + curbits;
+}
+
+static float get_band_cost_UQUAD_mips(struct AACEncContext *s,
+                                      PutBitContext *pb, const float *in,
+                                      const float *scaled, int size, int scale_idx,
+                                      int cb, const float lambda, const float uplim,
+                                      int *bits)
+{
+    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
+    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
+    int i;
+    float cost = 0;
+    int curbits = 0;
+    int qc1, qc2, qc3, qc4;
+
+    uint8_t *p_bits  = (uint8_t*)ff_aac_spectral_bits[cb-1];
+    float   *p_codes = (float  *)ff_aac_codebook_vectors[cb-1];
+
+    for (i = 0; i < size; i += 4) {
+        const float *vec;
+        int curidx;
+        float *in_pos = (float *)&in[i];
+        float di0, di1, di2, di3;
+
+        qc1 = scaled[i  ] * Q34 + 0.4054f;
+        qc2 = scaled[i+1] * Q34 + 0.4054f;
+        qc3 = scaled[i+2] * Q34 + 0.4054f;
+        qc4 = scaled[i+3] * Q34 + 0.4054f;
+
+        __asm__ volatile (
+            ".set push                                  \n\t"
+            ".set noreorder                             \n\t"
+
+            "ori        $t4,    $zero,  2               \n\t"
+            "slt        $t0,    $t4,    %[qc1]          \n\t"
+            "slt        $t1,    $t4,    %[qc2]          \n\t"
+            "slt        $t2,    $t4,    %[qc3]          \n\t"
+            "slt        $t3,    $t4,    %[qc4]          \n\t"
+            "movn       %[qc1], $t4,    $t0             \n\t"
+            "movn       %[qc2], $t4,    $t1             \n\t"
+            "movn       %[qc3], $t4,    $t2             \n\t"
+            "movn       %[qc4], $t4,    $t3             \n\t"
+
+            ".set pop                                   \n\t"
+
+            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
+              [qc3]"+r"(qc3), [qc4]"+r"(qc4)
+            :
+            : "t0", "t1", "t2", "t3", "t4"
+        );
+
+        curidx = qc1;
+        curidx *= 3;
+        curidx += qc2;
+        curidx *= 3;
+        curidx += qc3;
+        curidx *= 3;
+        curidx += qc4;
+
+        curbits += p_bits[curidx];
+        curbits += uquad_sign_bits[curidx];
+        vec     = &p_codes[curidx*4];
+
+        __asm__ volatile (
+            ".set push                                  \n\t"
+            ".set noreorder                             \n\t"
+
+            "lwc1       %[di0], 0(%[in_pos])            \n\t"
+            "lwc1       %[di1], 4(%[in_pos])            \n\t"
+            "lwc1       %[di2], 8(%[in_pos])            \n\t"
+            "lwc1       %[di3], 12(%[in_pos])           \n\t"
+            "abs.s      %[di0], %[di0]                  \n\t"
+            "abs.s      %[di1], %[di1]                  \n\t"
+            "abs.s      %[di2], %[di2]                  \n\t"
+            "abs.s      %[di3], %[di3]                  \n\t"
+            "lwc1       $f0,    0(%[vec])               \n\t"
+            "lwc1       $f1,    4(%[vec])               \n\t"
+            "lwc1       $f2,    8(%[vec])               \n\t"
+            "lwc1       $f3,    12(%[vec])              \n\t"
+            "nmsub.s    %[di0], %[di0], $f0,    %[IQ]   \n\t"
+            "nmsub.s    %[di1], %[di1], $f1,    %[IQ]   \n\t"
+            "nmsub.s    %[di2], %[di2], $f2,    %[IQ]   \n\t"
+            "nmsub.s    %[di3], %[di3], $f3,    %[IQ]   \n\t"
+
+            ".set pop                                   \n\t"
+
+            : [di0]"=&f"(di0), [di1]"=&f"(di1),
+              [di2]"=&f"(di2), [di3]"=&f"(di3)
+            : [in_pos]"r"(in_pos), [vec]"r"(vec),
+              [IQ]"f"(IQ)
+            : "$f0", "$f1", "$f2", "$f3",
+              "memory"
+        );
+
+        cost += di0 * di0 + di1 * di1
+                + di2 * di2 + di3 * di3;
+    }
+
+    if (bits)
+        *bits = curbits;
+    return cost * lambda + curbits;
+}
+
+static float get_band_cost_SPAIR_mips(struct AACEncContext *s,
+                                      PutBitContext *pb, const float *in,
+                                      const float *scaled, int size, int scale_idx,
+                                      int cb, const float lambda, const float uplim,
+                                      int *bits)
+{
+    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
+    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
+    int i;
+    float cost = 0;
+    int qc1, qc2, qc3, qc4;
+    int curbits = 0;
+
+    uint8_t *p_bits  = (uint8_t *)ff_aac_spectral_bits[cb-1];
+    float   *p_codes = (float   *)ff_aac_codebook_vectors[cb-1];
+
+    for (i = 0; i < size; i += 4) {
+        const float *vec, *vec2;
+        int curidx, curidx2;
+        int   *in_int = (int   *)&in[i];
+        float *in_pos = (float *)&in[i];
+        float di0, di1, di2, di3;
+
+        qc1 = scaled[i  ] * Q34 + 0.4054f;
+        qc2 = scaled[i+1] * Q34 + 0.4054f;
+        qc3 = scaled[i+2] * Q34 + 0.4054f;
+        qc4 = scaled[i+3] * Q34 + 0.4054f;
+
+        __asm__ volatile (
+            ".set push                                  \n\t"
+            ".set noreorder                             \n\t"
+
+            "ori        $t4,    $zero,  4               \n\t"
+            "slt        $t0,    $t4,    %[qc1]          \n\t"
+            "slt        $t1,    $t4,    %[qc2]          \n\t"
+            "slt        $t2,    $t4,    %[qc3]          \n\t"
+            "slt        $t3,    $t4,    %[qc4]          \n\t"
+            "movn       %[qc1], $t4,    $t0             \n\t"
+            "movn       %[qc2], $t4,    $t1             \n\t"
+            "movn       %[qc3], $t4,    $t2             \n\t"
+            "movn       %[qc4], $t4,    $t3             \n\t"
+            "lw         $t0,    0(%[in_int])            \n\t"
+            "lw         $t1,    4(%[in_int])            \n\t"
+            "lw         $t2,    8(%[in_int])            \n\t"
+            "lw         $t3,    12(%[in_int])           \n\t"
+            "srl        $t0,    $t0,    31              \n\t"
+            "srl        $t1,    $t1,    31              \n\t"
+            "srl        $t2,    $t2,    31              \n\t"
+            "srl        $t3,    $t3,    31              \n\t"
+            "subu       $t4,    $zero,  %[qc1]          \n\t"
+            "subu       $t5,    $zero,  %[qc2]          \n\t"
+            "subu       $t6,    $zero,  %[qc3]          \n\t"
+            "subu       $t7,    $zero,  %[qc4]          \n\t"
+            "movn       %[qc1], $t4,    $t0             \n\t"
+            "movn       %[qc2], $t5,    $t1             \n\t"
+            "movn       %[qc3], $t6,    $t2             \n\t"
+            "movn       %[qc4], $t7,    $t3             \n\t"
+
+            ".set pop                                   \n\t"
+
+            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
+              [qc3]"+r"(qc3), [qc4]"+r"(qc4)
+            : [in_int]"r"(in_int)
+            : "t0", "t1", "t2", "t3",
+              "t4", "t5", "t6", "t7",
+              "memory"
+        );
+
+        curidx = 9 * qc1;
+        curidx += qc2 + 40;
+
+        curidx2 = 9 * qc3;
+        curidx2 += qc4 + 40;
+
+        curbits += p_bits[curidx];
+        curbits += p_bits[curidx2];
+
+        vec     = &p_codes[curidx*2];
+        vec2    = &p_codes[curidx2*2];
+
+        __asm__ volatile (
+            ".set push                                  \n\t"
+            ".set noreorder                             \n\t"
+
+            "lwc1       $f0,    0(%[in_pos])            \n\t"
+            "lwc1       $f1,    0(%[vec])               \n\t"
+            "lwc1       $f2,    4(%[in_pos])            \n\t"
+            "lwc1       $f3,    4(%[vec])               \n\t"
+            "lwc1       $f4,    8(%[in_pos])            \n\t"
+            "lwc1       $f5,    0(%[vec2])              \n\t"
+            "lwc1       $f6,    12(%[in_pos])           \n\t"
+            "lwc1       $f7,    4(%[vec2])              \n\t"
+            "nmsub.s    %[di0], $f0,    $f1,    %[IQ]   \n\t"
+            "nmsub.s    %[di1], $f2,    $f3,    %[IQ]   \n\t"
+            "nmsub.s    %[di2], $f4,    $f5,    %[IQ]   \n\t"
+            "nmsub.s    %[di3], $f6,    $f7,    %[IQ]   \n\t"
+
+            ".set pop                                   \n\t"
+
+            : [di0]"=&f"(di0), [di1]"=&f"(di1),
+              [di2]"=&f"(di2), [di3]"=&f"(di3)
+            : [in_pos]"r"(in_pos), [vec]"r"(vec),
+              [vec2]"r"(vec2), [IQ]"f"(IQ)
+            : "$f0", "$f1", "$f2", "$f3",
+              "$f4", "$f5", "$f6", "$f7",
+              "memory"
+        );
+
+        cost += di0 * di0 + di1 * di1
+                + di2 * di2 + di3 * di3;
+    }
+
+    if (bits)
+        *bits = curbits;
+    return cost * lambda + curbits;
+}
+
+static float get_band_cost_UPAIR7_mips(struct AACEncContext *s,
+                                       PutBitContext *pb, const float *in,
+                                       const float *scaled, int size, int scale_idx,
+                                       int cb, const float lambda, const float uplim,
+                                       int *bits)
+{
+    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
+    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
+    int i;
+    float cost = 0;
+    int qc1, qc2, qc3, qc4;
+    int curbits = 0;
+
+    uint8_t *p_bits  = (uint8_t *)ff_aac_spectral_bits[cb-1];
+    float   *p_codes = (float   *)ff_aac_codebook_vectors[cb-1];
+
+    for (i = 0; i < size; i += 4) {
+        const float *vec, *vec2;
+        int curidx, curidx2, sign1, count1, sign2, count2;
+        int   *in_int = (int   *)&in[i];
+        float *in_pos = (float *)&in[i];
+        float di0, di1, di2, di3;
+
+        qc1 = scaled[i  ] * Q34 + 0.4054f;
+        qc2 = scaled[i+1] * Q34 + 0.4054f;
+        qc3 = scaled[i+2] * Q34 + 0.4054f;
+        qc4 = scaled[i+3] * Q34 + 0.4054f;
+
+        __asm__ volatile (
+            ".set push                                          \n\t"
+            ".set noreorder                                     \n\t"
+
+            "ori        $t4,        $zero,      7               \n\t"
+            "ori        %[sign1],   $zero,      0               \n\t"
+            "ori        %[sign2],   $zero,      0               \n\t"
+            "slt        $t0,        $t4,        %[qc1]          \n\t"
+            "slt        $t1,        $t4,        %[qc2]          \n\t"
+            "slt        $t2,        $t4,        %[qc3]          \n\t"
+            "slt        $t3,        $t4,        %[qc4]          \n\t"
+            "movn       %[qc1],     $t4,        $t0             \n\t"
+            "movn       %[qc2],     $t4,        $t1             \n\t"
+            "movn       %[qc3],     $t4,        $t2             \n\t"
+            "movn       %[qc4],     $t4,        $t3             \n\t"
+            "lw         $t0,        0(%[in_int])                \n\t"
+            "lw         $t1,        4(%[in_int])                \n\t"
+            "lw         $t2,        8(%[in_int])                \n\t"
+            "lw         $t3,        12(%[in_int])               \n\t"
+            "slt        $t0,        $t0,        $zero           \n\t"
+            "movn       %[sign1],   $t0,        %[qc1]          \n\t"
+            "slt        $t2,        $t2,        $zero           \n\t"
+            "movn       %[sign2],   $t2,        %[qc3]          \n\t"
+            "slt        $t1,        $t1,        $zero           \n\t"
+            "sll        $t0,        %[sign1],   1               \n\t"
+            "or         $t0,        $t0,        $t1             \n\t"
+            "movn       %[sign1],   $t0,        %[qc2]          \n\t"
+            "slt        $t3,        $t3,        $zero           \n\t"
+            "sll        $t0,        %[sign2],   1               \n\t"
+            "or         $t0,        $t0,        $t3             \n\t"
+            "movn       %[sign2],   $t0,        %[qc4]          \n\t"
+            "slt        %[count1],  $zero,      %[qc1]          \n\t"
+            "slt        $t1,        $zero,      %[qc2]          \n\t"
+            "slt        %[count2],  $zero,      %[qc3]          \n\t"
+            "slt        $t2,        $zero,      %[qc4]          \n\t"
+            "addu       %[count1],  %[count1],  $t1             \n\t"
+            "addu       %[count2],  %[count2],  $t2             \n\t"
+
+            ".set pop                                           \n\t"
+
+            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
+              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
+              [sign1]"=&r"(sign1), [count1]"=&r"(count1),
+              [sign2]"=&r"(sign2), [count2]"=&r"(count2)
+            : [in_int]"r"(in_int)
+            : "t0", "t1", "t2", "t3", "t4",
+              "memory"
+        );
+
+        curidx = 8 * qc1;
+        curidx += qc2;
+
+        curidx2 = 8 * qc3;
+        curidx2 += qc4;
+
+        curbits += p_bits[curidx];
+        curbits += upair7_sign_bits[curidx];
+        vec     = &p_codes[curidx*2];
+
+        curbits += p_bits[curidx2];
+        curbits += upair7_sign_bits[curidx2];
+        vec2    = &p_codes[curidx2*2];
+
+        __asm__ volatile (
+            ".set push                                          \n\t"
+            ".set noreorder                                     \n\t"
+
+            "lwc1       %[di0],     0(%[in_pos])                \n\t"
+            "lwc1       %[di1],     4(%[in_pos])                \n\t"
+            "lwc1       %[di2],     8(%[in_pos])                \n\t"
+            "lwc1       %[di3],     12(%[in_pos])               \n\t"
+            "abs.s      %[di0],     %[di0]                      \n\t"
+            "abs.s      %[di1],     %[di1]                      \n\t"
+            "abs.s      %[di2],     %[di2]                      \n\t"
+            "abs.s      %[di3],     %[di3]                      \n\t"
+            "lwc1       $f0,        0(%[vec])                   \n\t"
+            "lwc1       $f1,        4(%[vec])                   \n\t"
+            "lwc1       $f2,        0(%[vec2])                  \n\t"
+            "lwc1       $f3,        4(%[vec2])                  \n\t"
+            "nmsub.s    %[di0],     %[di0],     $f0,    %[IQ]   \n\t"
+            "nmsub.s    %[di1],     %[di1],     $f1,    %[IQ]   \n\t"
+            "nmsub.s    %[di2],     %[di2],     $f2,    %[IQ]   \n\t"
+            "nmsub.s    %[di3],     %[di3],     $f3,    %[IQ]   \n\t"
+
+            ".set pop                                           \n\t"
+
+            : [di0]"=&f"(di0), [di1]"=&f"(di1),
+              [di2]"=&f"(di2), [di3]"=&f"(di3)
+            : [in_pos]"r"(in_pos), [vec]"r"(vec),
+              [vec2]"r"(vec2), [IQ]"f"(IQ)
+            : "$f0", "$f1", "$f2", "$f3",
+              "memory"
+        );
+
+        cost += di0 * di0 + di1 * di1
+                + di2 * di2 + di3 * di3;
+    }
+
+    if (bits)
+        *bits = curbits;
+    return cost * lambda + curbits;
+}
+
+static float get_band_cost_UPAIR12_mips(struct AACEncContext *s,
+                                        PutBitContext *pb, const float *in,
+                                        const float *scaled, int size, int scale_idx,
+                                        int cb, const float lambda, const float uplim,
+                                        int *bits)
+{
+    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
+    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
+    int i;
+    float cost = 0;
+    int qc1, qc2, qc3, qc4;
+    int curbits = 0;
+
+    uint8_t *p_bits  = (uint8_t *)ff_aac_spectral_bits[cb-1];
+    float   *p_codes = (float   *)ff_aac_codebook_vectors[cb-1];
+
+    for (i = 0; i < size; i += 4) {
+        const float *vec, *vec2;
+        int curidx, curidx2;
+        int sign1, count1, sign2, count2;
+        int   *in_int = (int   *)&in[i];
+        float *in_pos = (float *)&in[i];
+        float di0, di1, di2, di3;
+
+        qc1 = scaled[i  ] * Q34 + 0.4054f;
+        qc2 = scaled[i+1] * Q34 + 0.4054f;
+        qc3 = scaled[i+2] * Q34 + 0.4054f;
+        qc4 = scaled[i+3] * Q34 + 0.4054f;
+
+        __asm__ volatile (
+            ".set push                                          \n\t"
+            ".set noreorder                                     \n\t"
+
+            "ori        $t4,        $zero,      12              \n\t"
+            "ori        %[sign1],   $zero,      0               \n\t"
+            "ori        %[sign2],   $zero,      0               \n\t"
+            "slt        $t0,        $t4,        %[qc1]          \n\t"
+            "slt        $t1,        $t4,        %[qc2]          \n\t"
+            "slt        $t2,        $t4,        %[qc3]          \n\t"
+            "slt        $t3,        $t4,        %[qc4]          \n\t"
+            "movn       %[qc1],     $t4,        $t0             \n\t"
+            "movn       %[qc2],     $t4,        $t1             \n\t"
+            "movn       %[qc3],     $t4,        $t2             \n\t"
+            "movn       %[qc4],     $t4,        $t3             \n\t"
+            "lw         $t0,        0(%[in_int])                \n\t"
+            "lw         $t1,        4(%[in_int])                \n\t"
+            "lw         $t2,        8(%[in_int])                \n\t"
+            "lw         $t3,        12(%[in_int])               \n\t"
+            "slt        $t0,        $t0,        $zero           \n\t"
+            "movn       %[sign1],   $t0,        %[qc1]          \n\t"
+            "slt        $t2,        $t2,        $zero           \n\t"
+            "movn       %[sign2],   $t2,        %[qc3]          \n\t"
+            "slt        $t1,        $t1,        $zero           \n\t"
+            "sll        $t0,        %[sign1],   1               \n\t"
+            "or         $t0,        $t0,        $t1             \n\t"
+            "movn       %[sign1],   $t0,        %[qc2]          \n\t"
+            "slt        $t3,        $t3,        $zero           \n\t"
+            "sll        $t0,        %[sign2],   1               \n\t"
+            "or         $t0,        $t0,        $t3             \n\t"
+            "movn       %[sign2],   $t0,        %[qc4]          \n\t"
+            "slt        %[count1],  $zero,      %[qc1]          \n\t"
+            "slt        $t1,        $zero,      %[qc2]          \n\t"
+            "slt        %[count2],  $zero,      %[qc3]          \n\t"
+            "slt        $t2,        $zero,      %[qc4]          \n\t"
+            "addu       %[count1],  %[count1],  $t1             \n\t"
+            "addu       %[count2],  %[count2],  $t2             \n\t"
+
+            ".set pop                                           \n\t"
+
+            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
+              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
+              [sign1]"=&r"(sign1), [count1]"=&r"(count1),
+              [sign2]"=&r"(sign2), [count2]"=&r"(count2)
+            : [in_int]"r"(in_int)
+            : "t0", "t1", "t2", "t3", "t4",
+              "memory"
+        );
+
+        curidx = 13 * qc1;
+        curidx += qc2;
+
+        curidx2 = 13 * qc3;
+        curidx2 += qc4;
+
+        curbits += p_bits[curidx];
+        curbits += p_bits[curidx2];
+        curbits += upair12_sign_bits[curidx];
+        curbits += upair12_sign_bits[curidx2];
+        vec     = &p_codes[curidx*2];
+        vec2    = &p_codes[curidx2*2];
+
+        __asm__ volatile (
+            ".set push                                          \n\t"
+            ".set noreorder                                     \n\t"
+
+            "lwc1       %[di0],     0(%[in_pos])                \n\t"
+            "lwc1       %[di1],     4(%[in_pos])                \n\t"
+            "lwc1       %[di2],     8(%[in_pos])                \n\t"
+            "lwc1       %[di3],     12(%[in_pos])               \n\t"
+            "abs.s      %[di0],     %[di0]                      \n\t"
+            "abs.s      %[di1],     %[di1]                      \n\t"
+            "abs.s      %[di2],     %[di2]                      \n\t"
+            "abs.s      %[di3],     %[di3]                      \n\t"
+            "lwc1       $f0,        0(%[vec])                   \n\t"
+            "lwc1       $f1,        4(%[vec])                   \n\t"
+            "lwc1       $f2,        0(%[vec2])                  \n\t"
+            "lwc1       $f3,        4(%[vec2])                  \n\t"
+            "nmsub.s    %[di0],     %[di0],     $f0,    %[IQ]   \n\t"
+            "nmsub.s    %[di1],     %[di1],     $f1,    %[IQ]   \n\t"
+            "nmsub.s    %[di2],     %[di2],     $f2,    %[IQ]   \n\t"
+            "nmsub.s    %[di3],     %[di3],     $f3,    %[IQ]   \n\t"
+
+            ".set pop                                           \n\t"
+
+            : [di0]"=&f"(di0), [di1]"=&f"(di1),
+              [di2]"=&f"(di2), [di3]"=&f"(di3)
+            : [in_pos]"r"(in_pos), [vec]"r"(vec),
+              [vec2]"r"(vec2), [IQ]"f"(IQ)
+            : "$f0", "$f1", "$f2", "$f3",
+              "memory"
+        );
+
+        cost += di0 * di0 + di1 * di1
+                + di2 * di2 + di3 * di3;
+    }
+
+    if (bits)
+        *bits = curbits;
+    return cost * lambda + curbits;
+}
+
+static float get_band_cost_ESC_mips(struct AACEncContext *s,
+                                    PutBitContext *pb, const float *in,
+                                    const float *scaled, int size, int scale_idx,
+                                    int cb, const float lambda, const float uplim,
+                                    int *bits)
+{
+    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
+    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
+    const float CLIPPED_ESCAPE = 165140.0f * IQ;
+    int i;
+    float cost = 0;
+    int qc1, qc2, qc3, qc4;
+    int curbits = 0;
+
+    uint8_t *p_bits  = (uint8_t*)ff_aac_spectral_bits[cb-1];
+    float   *p_codes = (float*  )ff_aac_codebook_vectors[cb-1];
+
+    for (i = 0; i < size; i += 4) {
+        const float *vec, *vec2;
+        int curidx, curidx2;
+        float t1, t2, t3, t4;
+        float di1, di2, di3, di4;
+        int cond0, cond1, cond2, cond3;
+        int c1, c2, c3, c4;
+
+        qc1 = scaled[i  ] * Q34 + 0.4054f;
+        qc2 = scaled[i+1] * Q34 + 0.4054f;
+        qc3 = scaled[i+2] * Q34 + 0.4054f;
+        qc4 = scaled[i+3] * Q34 + 0.4054f;
+
+        __asm__ volatile (
+            ".set push                                  \n\t"
+            ".set noreorder                             \n\t"
+
+            "ori        $t4,        $zero,  15          \n\t"
+            "ori        $t5,        $zero,  16          \n\t"
+            "shll_s.w   %[c1],      %[qc1], 18          \n\t"
+            "shll_s.w   %[c2],      %[qc2], 18          \n\t"
+            "shll_s.w   %[c3],      %[qc3], 18          \n\t"
+            "shll_s.w   %[c4],      %[qc4], 18          \n\t"
+            "srl        %[c1],      %[c1],  18          \n\t"
+            "srl        %[c2],      %[c2],  18          \n\t"
+            "srl        %[c3],      %[c3],  18          \n\t"
+            "srl        %[c4],      %[c4],  18          \n\t"
+            "slt        %[cond0],   $t4,    %[qc1]      \n\t"
+            "slt        %[cond1],   $t4,    %[qc2]      \n\t"
+            "slt        %[cond2],   $t4,    %[qc3]      \n\t"
+            "slt        %[cond3],   $t4,    %[qc4]      \n\t"
+            "movn       %[qc1],     $t5,    %[cond0]    \n\t"
+            "movn       %[qc2],     $t5,    %[cond1]    \n\t"
+            "movn       %[qc3],     $t5,    %[cond2]    \n\t"
+            "movn       %[qc4],     $t5,    %[cond3]    \n\t"
+
+            ".set pop                                   \n\t"
+
+            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
+              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
+              [cond0]"=&r"(cond0), [cond1]"=&r"(cond1),
+              [cond2]"=&r"(cond2), [cond3]"=&r"(cond3),
+              [c1]"=&r"(c1), [c2]"=&r"(c2),
+              [c3]"=&r"(c3), [c4]"=&r"(c4)
+            :
+            : "t4", "t5"
+        );
+
+        curidx = 17 * qc1;
+        curidx += qc2;
+
+        curidx2 = 17 * qc3;
+        curidx2 += qc4;
+
+        curbits += p_bits[curidx];
+        curbits += esc_sign_bits[curidx];
+        vec     = &p_codes[curidx*2];
+
+        curbits += p_bits[curidx2];
+        curbits += esc_sign_bits[curidx2];
+        vec2     = &p_codes[curidx2*2];
+
+        curbits += (av_log2(c1) * 2 - 3) & (-cond0);
+        curbits += (av_log2(c2) * 2 - 3) & (-cond1);
+        curbits += (av_log2(c3) * 2 - 3) & (-cond2);
+        curbits += (av_log2(c4) * 2 - 3) & (-cond3);
+
+        t1 = fabsf(in[i  ]);
+        t2 = fabsf(in[i+1]);
+        t3 = fabsf(in[i+2]);
+        t4 = fabsf(in[i+3]);
+
+        if (cond0) {
+            if (t1 >= CLIPPED_ESCAPE) {
+                di1 = t1 - CLIPPED_ESCAPE;
+            } else {
+                di1 = t1 - c1 * cbrtf(c1) * IQ;
+            }
+        } else
+            di1 = t1 - vec[0] * IQ;
+
+        if (cond1) {
+            if (t2 >= CLIPPED_ESCAPE) {
+                di2 = t2 - CLIPPED_ESCAPE;
+            } else {
+                di2 = t2 - c2 * cbrtf(c2) * IQ;
+            }
+        } else
+            di2 = t2 - vec[1] * IQ;
+
+        if (cond2) {
+            if (t3 >= CLIPPED_ESCAPE) {
+                di3 = t3 - CLIPPED_ESCAPE;
+            } else {
+                di3 = t3 - c3 * cbrtf(c3) * IQ;
+            }
+        } else
+            di3 = t3 - vec2[0] * IQ;
+
+        if (cond3) {
+            if (t4 >= CLIPPED_ESCAPE) {
+                di4 = t4 - CLIPPED_ESCAPE;
+            } else {
+                di4 = t4 - c4 * cbrtf(c4) * IQ;
+            }
+        } else
+            di4 = t4 - vec2[1]*IQ;
+
+        cost += di1 * di1 + di2 * di2
+                + di3 * di3 + di4 * di4;
+    }
+
+    if (bits)
+        *bits = curbits;
+    return cost * lambda + curbits;
+}
+
+static float (*const get_band_cost_arr[])(struct AACEncContext *s,
+                                          PutBitContext *pb, const float *in,
+                                          const float *scaled, int size, int scale_idx,
+                                          int cb, const float lambda, const float uplim,
+                                          int *bits) = {
+    get_band_cost_ZERO_mips,
+    get_band_cost_SQUAD_mips,
+    get_band_cost_SQUAD_mips,
+    get_band_cost_UQUAD_mips,
+    get_band_cost_UQUAD_mips,
+    get_band_cost_SPAIR_mips,
+    get_band_cost_SPAIR_mips,
+    get_band_cost_UPAIR7_mips,
+    get_band_cost_UPAIR7_mips,
+    get_band_cost_UPAIR12_mips,
+    get_band_cost_UPAIR12_mips,
+    get_band_cost_ESC_mips,
+};
+
+#define get_band_cost(                                  \
+                                s, pb, in, scaled, size, scale_idx, cb, \
+                                lambda, uplim, bits)                    \
+    get_band_cost_arr[cb](                              \
+                                s, pb, in, scaled, size, scale_idx, cb, \
+                                lambda, uplim, bits)
+
+static float quantize_band_cost(struct AACEncContext *s, const float *in,
+                                const float *scaled, int size, int scale_idx,
+                                int cb, const float lambda, const float uplim,
+                                int *bits)
+{
+    return get_band_cost(s, NULL, in, scaled, size, scale_idx, cb, lambda, uplim, bits);
+}
+
+static void search_for_quantizers_twoloop_mips(AVCodecContext *avctx,
+                                               AACEncContext *s,
+                                               SingleChannelElement *sce,
+                                               const float lambda)
+{
+    int start = 0, i, w, w2, g;
+    int destbits = avctx->bit_rate * 1024.0 / avctx->sample_rate / avctx->channels;
+    float dists[128] = { 0 }, uplims[128];
+    float maxvals[128];
+    int fflag, minscaler;
+    int its  = 0;
+    int allz = 0;
+    float minthr = INFINITY;
+
+    destbits = FFMIN(destbits, 5800);
+    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
+        for (g = 0;  g < sce->ics.num_swb; g++) {
+            int nz = 0;
+            float uplim = 0.0f;
+            for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
+                FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
+                uplim += band->threshold;
+                if (band->energy <= band->threshold || band->threshold == 0.0f) {
+                    sce->zeroes[(w+w2)*16+g] = 1;
+                    continue;
+                }
+                nz = 1;
+            }
+            uplims[w*16+g] = uplim *512;
+            sce->zeroes[w*16+g] = !nz;
+            if (nz)
+                minthr = FFMIN(minthr, uplim);
+            allz |= nz;
+        }
+    }
+    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
+        for (g = 0;  g < sce->ics.num_swb; g++) {
+            if (sce->zeroes[w*16+g]) {
+                sce->sf_idx[w*16+g] = SCALE_ONE_POS;
+                continue;
+            }
+            sce->sf_idx[w*16+g] = SCALE_ONE_POS + FFMIN(log2f(uplims[w*16+g]/minthr)*4,59);
+        }
+    }
+
+    if (!allz)
+        return;
+    abs_pow34_v(s->scoefs, sce->coeffs, 1024);
+
+    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
+        start = w*128;
+        for (g = 0;  g < sce->ics.num_swb; g++) {
+            const float *scaled = s->scoefs + start;
+            maxvals[w*16+g] = find_max_val(sce->ics.group_len[w], sce->ics.swb_sizes[g], scaled);
+            start += sce->ics.swb_sizes[g];
+        }
+    }
+
+    do {
+        int tbits, qstep;
+        minscaler = sce->sf_idx[0];
+        qstep = its ? 1 : 32;
+        do {
+            int prev = -1;
+            tbits = 0;
+            fflag = 0;
+
+            if (qstep > 1) {
+                for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
+                    start = w*128;
+                    for (g = 0;  g < sce->ics.num_swb; g++) {
+                        const float *coefs = sce->coeffs + start;
+                        const float *scaled = s->scoefs + start;
+                        int bits = 0;
+                        int cb;
+
+                        if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
+                            start += sce->ics.swb_sizes[g];
+                            continue;
+                        }
+                        minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
+                        cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
+                        for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
+                            int b;
+                            bits += quantize_band_cost_bits(s, coefs + w2*128,
+                                                            scaled + w2*128,
+                                                            sce->ics.swb_sizes[g],
+                                                            sce->sf_idx[w*16+g],
+                                                            cb,
+                                                            1.0f,
+                                                            INFINITY,
+                                                            &b);
+                        }
+                        if (prev != -1) {
+                            bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
+                        }
+                        tbits += bits;
+                        start += sce->ics.swb_sizes[g];
+                        prev = sce->sf_idx[w*16+g];
+                    }
+                }
+            }
+            else {
+                for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
+                    start = w*128;
+                    for (g = 0;  g < sce->ics.num_swb; g++) {
+                        const float *coefs = sce->coeffs + start;
+                        const float *scaled = s->scoefs + start;
+                        int bits = 0;
+                        int cb;
+                        float dist = 0.0f;
+
+                        if (sce->zeroes[w*16+g] || sce->sf_idx[w*16+g] >= 218) {
+                            start += sce->ics.swb_sizes[g];
+                            continue;
+                        }
+                        minscaler = FFMIN(minscaler, sce->sf_idx[w*16+g]);
+                        cb = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
+                        for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
+                            int b;
+                            dist += quantize_band_cost(s, coefs + w2*128,
+                                                       scaled + w2*128,
+                                                       sce->ics.swb_sizes[g],
+                                                       sce->sf_idx[w*16+g],
+                                                       cb,
+                                                       1.0f,
+                                                       INFINITY,
+                                                       &b);
+                            bits += b;
+                        }
+                        dists[w*16+g] = dist - bits;
+                        if (prev != -1) {
+                            bits += ff_aac_scalefactor_bits[sce->sf_idx[w*16+g] - prev + SCALE_DIFF_ZERO];
+                        }
+                        tbits += bits;
+                        start += sce->ics.swb_sizes[g];
+                        prev = sce->sf_idx[w*16+g];
+                    }
+                }
+            }
+            if (tbits > destbits) {
+                for (i = 0; i < 128; i++)
+                    if (sce->sf_idx[i] < 218 - qstep)
+                        sce->sf_idx[i] += qstep;
+            } else {
+                for (i = 0; i < 128; i++)
+                    if (sce->sf_idx[i] > 60 - qstep)
+                        sce->sf_idx[i] -= qstep;
+            }
+            qstep >>= 1;
+            if (!qstep && tbits > destbits*1.02 && sce->sf_idx[0] < 217)
+                qstep = 1;
+        } while (qstep);
+
+        fflag = 0;
+        minscaler = av_clip(minscaler, 60, 255 - SCALE_MAX_DIFF);
+        for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
+            for (g = 0; g < sce->ics.num_swb; g++) {
+                int prevsc = sce->sf_idx[w*16+g];
+                if (dists[w*16+g] > uplims[w*16+g] && sce->sf_idx[w*16+g] > 60) {
+                    if (find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]-1))
+                        sce->sf_idx[w*16+g]--;
+                    else
+                        sce->sf_idx[w*16+g]-=2;
+                }
+                sce->sf_idx[w*16+g] = av_clip(sce->sf_idx[w*16+g], minscaler, minscaler + SCALE_MAX_DIFF);
+                sce->sf_idx[w*16+g] = FFMIN(sce->sf_idx[w*16+g], 219);
+                if (sce->sf_idx[w*16+g] != prevsc)
+                    fflag = 1;
+                sce->band_type[w*16+g] = find_min_book(maxvals[w*16+g], sce->sf_idx[w*16+g]);
+            }
+        }
+        its++;
+    } while (fflag && its < 10);
+}
+
+static void search_for_ms_mips(AACEncContext *s, ChannelElement *cpe,
+                               const float lambda)
+{
+    int start = 0, i, w, w2, g;
+    float M[128], S[128];
+    float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
+    SingleChannelElement *sce0 = &cpe->ch[0];
+    SingleChannelElement *sce1 = &cpe->ch[1];
+    if (!cpe->common_window)
+        return;
+    for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
+        for (g = 0;  g < sce0->ics.num_swb; g++) {
+            if (!cpe->ch[0].zeroes[w*16+g] && !cpe->ch[1].zeroes[w*16+g]) {
+                float dist1 = 0.0f, dist2 = 0.0f;
+                for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
+                    FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
+                    FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g];
+                    float minthr = FFMIN(band0->threshold, band1->threshold);
+                    float maxthr = FFMAX(band0->threshold, band1->threshold);
+                    for (i = 0; i < sce0->ics.swb_sizes[g]; i+=4) {
+                        M[i  ] = (sce0->coeffs[start+w2*128+i  ]
+                                + sce1->coeffs[start+w2*128+i  ]) * 0.5;
+                        M[i+1] = (sce0->coeffs[start+w2*128+i+1]
+                                + sce1->coeffs[start+w2*128+i+1]) * 0.5;
+                        M[i+2] = (sce0->coeffs[start+w2*128+i+2]
+                                + sce1->coeffs[start+w2*128+i+2]) * 0.5;
+                        M[i+3] = (sce0->coeffs[start+w2*128+i+3]
+                                + sce1->coeffs[start+w2*128+i+3]) * 0.5;
+
+                        S[i  ] =  M[i  ]
+                                - sce1->coeffs[start+w2*128+i  ];
+                        S[i+1] =  M[i+1]
+                                - sce1->coeffs[start+w2*128+i+1];
+                        S[i+2] =  M[i+2]
+                                - sce1->coeffs[start+w2*128+i+2];
+                        S[i+3] =  M[i+3]
+                                - sce1->coeffs[start+w2*128+i+3];
+                   }
+                    abs_pow34_v(L34, sce0->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
+                    abs_pow34_v(R34, sce1->coeffs+start+w2*128, sce0->ics.swb_sizes[g]);
+                    abs_pow34_v(M34, M,                         sce0->ics.swb_sizes[g]);
+                    abs_pow34_v(S34, S,                         sce0->ics.swb_sizes[g]);
+                    dist1 += quantize_band_cost(s, sce0->coeffs + start + w2*128,
+                                                L34,
+                                                sce0->ics.swb_sizes[g],
+                                                sce0->sf_idx[(w+w2)*16+g],
+                                                sce0->band_type[(w+w2)*16+g],
+                                                lambda / band0->threshold, INFINITY, NULL);
+                    dist1 += quantize_band_cost(s, sce1->coeffs + start + w2*128,
+                                                R34,
+                                                sce1->ics.swb_sizes[g],
+                                                sce1->sf_idx[(w+w2)*16+g],
+                                                sce1->band_type[(w+w2)*16+g],
+                                                lambda / band1->threshold, INFINITY, NULL);
+                    dist2 += quantize_band_cost(s, M,
+                                                M34,
+                                                sce0->ics.swb_sizes[g],
+                                                sce0->sf_idx[(w+w2)*16+g],
+                                                sce0->band_type[(w+w2)*16+g],
+                                                lambda / maxthr, INFINITY, NULL);
+                    dist2 += quantize_band_cost(s, S,
+                                                S34,
+                                                sce1->ics.swb_sizes[g],
+                                                sce1->sf_idx[(w+w2)*16+g],
+                                                sce1->band_type[(w+w2)*16+g],
+                                                lambda / minthr, INFINITY, NULL);
+                }
+                cpe->ms_mask[w*16+g] = dist2 < dist1;
+            }
+            start += sce0->ics.swb_sizes[g];
+        }
+    }
+}
+#endif /*HAVE_MIPSFPU */
+
+static void codebook_trellis_rate_mips(AACEncContext *s, SingleChannelElement *sce,
+                                       int win, int group_len, const float lambda)
+{
+    BandCodingPath path[120][12];
+    int w, swb, cb, start, size;
+    int i, j;
+    const int max_sfb  = sce->ics.max_sfb;
+    const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
+    const int run_esc  = (1 << run_bits) - 1;
+    int idx, ppos, count;
+    int stackrun[120], stackcb[120], stack_len;
+    float next_minbits = INFINITY;
+    int next_mincb = 0;
+
+    abs_pow34_v(s->scoefs, sce->coeffs, 1024);
+    start = win*128;
+    for (cb = 0; cb < 12; cb++) {
+        path[0][cb].cost     = run_bits+4;
+        path[0][cb].prev_idx = -1;
+        path[0][cb].run      = 0;
+    }
+    for (swb = 0; swb < max_sfb; swb++) {
+        size = sce->ics.swb_sizes[swb];
+        if (sce->zeroes[win*16 + swb]) {
+            float cost_stay_here = path[swb][0].cost;
+            float cost_get_here  = next_minbits + run_bits + 4;
+            if (   run_value_bits[sce->ics.num_windows == 8][path[swb][0].run]
+                != run_value_bits[sce->ics.num_windows == 8][path[swb][0].run+1])
+                cost_stay_here += run_bits;
+            if (cost_get_here < cost_stay_here) {
+                path[swb+1][0].prev_idx = next_mincb;
+                path[swb+1][0].cost     = cost_get_here;
+                path[swb+1][0].run      = 1;
+            } else {
+                path[swb+1][0].prev_idx = 0;
+                path[swb+1][0].cost     = cost_stay_here;
+                path[swb+1][0].run      = path[swb][0].run + 1;
+            }
+            next_minbits = path[swb+1][0].cost;
+            next_mincb = 0;
+            for (cb = 1; cb < 12; cb++) {
+                path[swb+1][cb].cost = 61450;
+                path[swb+1][cb].prev_idx = -1;
+                path[swb+1][cb].run = 0;
+            }
+        } else {
+            float minbits = next_minbits;
+            int mincb = next_mincb;
+            int startcb = sce->band_type[win*16+swb];
+            next_minbits = INFINITY;
+            next_mincb = 0;
+            for (cb = 0; cb < startcb; cb++) {
+                path[swb+1][cb].cost = 61450;
+                path[swb+1][cb].prev_idx = -1;
+                path[swb+1][cb].run = 0;
+            }
+            for (cb = startcb; cb < 12; cb++) {
+                float cost_stay_here, cost_get_here;
+                float bits = 0.0f;
+                for (w = 0; w < group_len; w++) {
+                    bits += quantize_band_cost_bits(s, sce->coeffs + start + w*128,
+                                                    s->scoefs + start + w*128, size,
+                                                    sce->sf_idx[(win+w)*16+swb], cb,
+                                                    0, INFINITY, NULL);
+                }
+                cost_stay_here = path[swb][cb].cost + bits;
+                cost_get_here  = minbits            + bits + run_bits + 4;
+                if (   run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
+                    != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
+                    cost_stay_here += run_bits;
+                if (cost_get_here < cost_stay_here) {
+                    path[swb+1][cb].prev_idx = mincb;
+                    path[swb+1][cb].cost     = cost_get_here;
+                    path[swb+1][cb].run      = 1;
+                } else {
+                    path[swb+1][cb].prev_idx = cb;
+                    path[swb+1][cb].cost     = cost_stay_here;
+                    path[swb+1][cb].run      = path[swb][cb].run + 1;
+                }
+                if (path[swb+1][cb].cost < next_minbits) {
+                    next_minbits = path[swb+1][cb].cost;
+                    next_mincb = cb;
+                }
+            }
+        }
+        start += sce->ics.swb_sizes[swb];
+    }
+
+    stack_len = 0;
+    idx       = 0;
+    for (cb = 1; cb < 12; cb++)
+        if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
+            idx = cb;
+    ppos = max_sfb;
+    while (ppos > 0) {
+        av_assert1(idx >= 0);
+        cb = idx;
+        stackrun[stack_len] = path[ppos][cb].run;
+        stackcb [stack_len] = cb;
+        idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
+        ppos -= path[ppos][cb].run;
+        stack_len++;
+    }
+
+    start = 0;
+    for (i = stack_len - 1; i >= 0; i--) {
+        put_bits(&s->pb, 4, stackcb[i]);
+        count = stackrun[i];
+        memset(sce->zeroes + win*16 + start, !stackcb[i], count);
+        for (j = 0; j < count; j++) {
+            sce->band_type[win*16 + start] =  stackcb[i];
+            start++;
+        }
+        while (count >= run_esc) {
+            put_bits(&s->pb, run_bits, run_esc);
+            count -= run_esc;
+        }
+        put_bits(&s->pb, run_bits, count);
+    }
+}
+#endif /* HAVE_INLINE_ASM */
+
+void ff_aac_coder_init_mips(AACEncContext *c) {
+#if HAVE_INLINE_ASM
+    AACCoefficientsEncoder *e = c->coder;
+    int option = c->options.aac_coder;
+
+    if (option == 2) {
+        e->quantize_and_encode_band = quantize_and_encode_band_mips;
+        e->encode_window_bands_info = codebook_trellis_rate_mips;
+#if HAVE_MIPSFPU
+        e->search_for_quantizers    = search_for_quantizers_twoloop_mips;
+        e->search_for_ms            = search_for_ms_mips;
+#endif /* HAVE_MIPSFPU */
+    }
+#endif /* HAVE_INLINE_ASM */
+}
diff --git a/libavcodec/mips/aacpsy_mips.h b/libavcodec/mips/aacpsy_mips.h
new file mode 100644
index 0000000..d1353c4
--- /dev/null
+++ b/libavcodec/mips/aacpsy_mips.h
@@ -0,0 +1,230 @@
+/*
+ * Copyright (c) 2012
+ *      MIPS Technologies, Inc., California.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the MIPS Technologies, Inc., nor the names of its
+ *    contributors may be used to endorse or promote products derived from
+ *    this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE MIPS TECHNOLOGIES, INC. ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE MIPS TECHNOLOGIES, INC. BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Author:  Bojan Zivkovic   (bojan@mips.com)
+ *
+ * AAC encoder psychoacoustic model routines optimized
+ * for MIPS floating-point architecture
+ *
+ * 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
+ * Reference: libavcodec/aacpsy.c
+ */
+
+#ifndef AVCODEC_MIPS_AACPSY_MIPS_H
+#define AVCODEC_MIPS_AACPSY_MIPS_H
+
+#if HAVE_INLINE_ASM && HAVE_MIPSFPU && ( PSY_LAME_FIR_LEN == 21 )
+static void calc_thr_3gpp_mips(const FFPsyWindowInfo *wi, const int num_bands,
+                               AacPsyChannel *pch, const uint8_t *band_sizes,
+                               const float *coefs)
+{
+    int i, w, g;
+    int start = 0;
+    for (w = 0; w < wi->num_windows*16; w += 16) {
+        for (g = 0; g < num_bands; g++) {
+            AacPsyBand *band = &pch->band[w+g];
+
+            float form_factor = 0.0f;
+            float Temp;
+            band->energy = 0.0f;
+            for (i = 0; i < band_sizes[g]; i+=4) {
+                float a, b, c, d;
+                float ax, bx, cx, dx;
+                float *cf = (float *)&coefs[start+i];
+
+                __asm__ volatile (
+                    "lwc1   %[a],   0(%[cf])                \n\t"
+                    "lwc1   %[b],   4(%[cf])                \n\t"
+                    "lwc1   %[c],   8(%[cf])                \n\t"
+                    "lwc1   %[d],   12(%[cf])               \n\t"
+                    "abs.s  %[a],   %[a]                    \n\t"
+                    "abs.s  %[b],   %[b]                    \n\t"
+                    "abs.s  %[c],   %[c]                    \n\t"
+                    "abs.s  %[d],   %[d]                    \n\t"
+                    "sqrt.s %[ax],  %[a]                    \n\t"
+                    "sqrt.s %[bx],  %[b]                    \n\t"
+                    "sqrt.s %[cx],  %[c]                    \n\t"
+                    "sqrt.s %[dx],  %[d]                    \n\t"
+                    "madd.s %[e],   %[e],   %[a],   %[a]    \n\t"
+                    "madd.s %[e],   %[e],   %[b],   %[b]    \n\t"
+                    "madd.s %[e],   %[e],   %[c],   %[c]    \n\t"
+                    "madd.s %[e],   %[e],   %[d],   %[d]    \n\t"
+                    "add.s  %[f],   %[f],   %[ax]           \n\t"
+                    "add.s  %[f],   %[f],   %[bx]           \n\t"
+                    "add.s  %[f],   %[f],   %[cx]           \n\t"
+                    "add.s  %[f],   %[f],   %[dx]           \n\t"
+
+                    : [a]"=&f"(a), [b]"=&f"(b),
+                      [c]"=&f"(c), [d]"=&f"(d),
+                      [e]"+f"(band->energy), [f]"+f"(form_factor),
+                      [ax]"=&f"(ax), [bx]"=&f"(bx),
+                      [cx]"=&f"(cx), [dx]"=&f"(dx)
+                    : [cf]"r"(cf)
+                    : "memory"
+                );
+            }
+
+            Temp = sqrtf((float)band_sizes[g] / band->energy);
+            band->thr      = band->energy * 0.001258925f;
+            band->nz_lines = form_factor * sqrtf(Temp);
+            start += band_sizes[g];
+        }
+    }
+}
+
+static void psy_hp_filter_mips(const float *firbuf, float *hpfsmpl, const float * psy_fir_coeffs)
+{
+    float sum1, sum2, sum3, sum4;
+    float *fb = (float*)firbuf;
+    float *fb_end = fb + AAC_BLOCK_SIZE_LONG;
+    float *hp = hpfsmpl;
+
+    float coeff0 = psy_fir_coeffs[1];
+    float coeff1 = psy_fir_coeffs[3];
+    float coeff2 = psy_fir_coeffs[5];
+    float coeff3 = psy_fir_coeffs[7];
+    float coeff4 = psy_fir_coeffs[9];
+
+    __asm__ volatile (
+        ".set push                                          \n\t"
+        ".set noreorder                                     \n\t"
+
+        "li.s   $f12,       32768                           \n\t"
+        "1:                                                 \n\t"
+        "lwc1   $f0,        40(%[fb])                       \n\t"
+        "lwc1   $f1,        4(%[fb])                        \n\t"
+        "lwc1   $f2,        80(%[fb])                       \n\t"
+        "lwc1   $f3,        44(%[fb])                       \n\t"
+        "lwc1   $f4,        8(%[fb])                        \n\t"
+        "madd.s %[sum1],    $f0,        $f1,    %[coeff0]   \n\t"
+        "lwc1   $f5,        84(%[fb])                       \n\t"
+        "lwc1   $f6,        48(%[fb])                       \n\t"
+        "madd.s %[sum2],    $f3,        $f4,    %[coeff0]   \n\t"
+        "lwc1   $f7,        12(%[fb])                       \n\t"
+        "madd.s %[sum1],    %[sum1],    $f2,    %[coeff0]   \n\t"
+        "lwc1   $f8,        88(%[fb])                       \n\t"
+        "lwc1   $f9,        52(%[fb])                       \n\t"
+        "madd.s %[sum2],    %[sum2],    $f5,    %[coeff0]   \n\t"
+        "madd.s %[sum3],    $f6,        $f7,    %[coeff0]   \n\t"
+        "lwc1   $f10,       16(%[fb])                       \n\t"
+        "lwc1   $f11,       92(%[fb])                       \n\t"
+        "madd.s %[sum1],    %[sum1],    $f7,    %[coeff1]   \n\t"
+        "lwc1   $f1,        72(%[fb])                       \n\t"
+        "madd.s %[sum3],    %[sum3],    $f8,    %[coeff0]   \n\t"
+        "madd.s %[sum4],    $f9,        $f10,   %[coeff0]   \n\t"
+        "madd.s %[sum2],    %[sum2],    $f10,   %[coeff1]   \n\t"
+        "madd.s %[sum1],    %[sum1],    $f1,    %[coeff1]   \n\t"
+        "lwc1   $f4,        76(%[fb])                       \n\t"
+        "lwc1   $f8,        20(%[fb])                       \n\t"
+        "madd.s %[sum4],    %[sum4],    $f11,   %[coeff0]   \n\t"
+        "lwc1   $f11,       24(%[fb])                       \n\t"
+        "madd.s %[sum2],    %[sum2],    $f4,    %[coeff1]   \n\t"
+        "madd.s %[sum1],    %[sum1],    $f8,    %[coeff2]   \n\t"
+        "madd.s %[sum3],    %[sum3],    $f8,    %[coeff1]   \n\t"
+        "madd.s %[sum4],    %[sum4],    $f11,   %[coeff1]   \n\t"
+        "lwc1   $f7,        64(%[fb])                       \n\t"
+        "madd.s %[sum2],    %[sum2],    $f11,   %[coeff2]   \n\t"
+        "lwc1   $f10,       68(%[fb])                       \n\t"
+        "madd.s %[sum3],    %[sum3],    $f2,    %[coeff1]   \n\t"
+        "madd.s %[sum4],    %[sum4],    $f5,    %[coeff1]   \n\t"
+        "madd.s %[sum1],    %[sum1],    $f7,    %[coeff2]   \n\t"
+        "madd.s %[sum2],    %[sum2],    $f10,   %[coeff2]   \n\t"
+        "lwc1   $f2,        28(%[fb])                       \n\t"
+        "lwc1   $f5,        32(%[fb])                       \n\t"
+        "lwc1   $f8,        56(%[fb])                       \n\t"
+        "lwc1   $f11,       60(%[fb])                       \n\t"
+        "madd.s %[sum3],    %[sum3],    $f2,    %[coeff2]   \n\t"
+        "madd.s %[sum4],    %[sum4],    $f5,    %[coeff2]   \n\t"
+        "madd.s %[sum1],    %[sum1],    $f2,    %[coeff3]   \n\t"
+        "madd.s %[sum2],    %[sum2],    $f5,    %[coeff3]   \n\t"
+        "madd.s %[sum3],    %[sum3],    $f1,    %[coeff2]   \n\t"
+        "madd.s %[sum4],    %[sum4],    $f4,    %[coeff2]   \n\t"
+        "madd.s %[sum1],    %[sum1],    $f8,    %[coeff3]   \n\t"
+        "madd.s %[sum2],    %[sum2],    $f11,   %[coeff3]   \n\t"
+        "lwc1   $f1,        36(%[fb])                       \n\t"
+        "addiu  %[fb],      %[fb],      16                  \n\t"
+        "madd.s %[sum4],    %[sum4],    $f0,    %[coeff3]   \n\t"
+        "madd.s %[sum3],    %[sum3],    $f1,    %[coeff3]   \n\t"
+        "madd.s %[sum1],    %[sum1],    $f1,    %[coeff4]   \n\t"
+        "madd.s %[sum2],    %[sum2],    $f0,    %[coeff4]   \n\t"
+        "madd.s %[sum4],    %[sum4],    $f10,   %[coeff3]   \n\t"
+        "madd.s %[sum3],    %[sum3],    $f7,    %[coeff3]   \n\t"
+        "madd.s %[sum1],    %[sum1],    $f6,    %[coeff4]   \n\t"
+        "madd.s %[sum2],    %[sum2],    $f9,    %[coeff4]   \n\t"
+        "madd.s %[sum4],    %[sum4],    $f6,    %[coeff4]   \n\t"
+        "madd.s %[sum3],    %[sum3],    $f3,    %[coeff4]   \n\t"
+        "mul.s  %[sum1],    %[sum1],    $f12                \n\t"
+        "mul.s  %[sum2],    %[sum2],    $f12                \n\t"
+        "madd.s %[sum4],    %[sum4],    $f11,   %[coeff4]   \n\t"
+        "madd.s %[sum3],    %[sum3],    $f8,    %[coeff4]   \n\t"
+        "swc1   %[sum1],    0(%[hp])                        \n\t"
+        "swc1   %[sum2],    4(%[hp])                        \n\t"
+        "mul.s  %[sum4],    %[sum4],    $f12                \n\t"
+        "mul.s  %[sum3],    %[sum3],    $f12                \n\t"
+        "swc1   %[sum4],    12(%[hp])                       \n\t"
+        "swc1   %[sum3],    8(%[hp])                        \n\t"
+        "bne    %[fb],      %[fb_end],  1b                  \n\t"
+        " addiu %[hp],      %[hp],      16                  \n\t"
+
+        ".set pop                                           \n\t"
+
+        : [sum1]"=&f"(sum1), [sum2]"=&f"(sum2),
+          [sum3]"=&f"(sum3), [sum4]"=&f"(sum4),
+          [fb]"+r"(fb), [hp]"+r"(hp)
+        : [coeff0]"f"(coeff0), [coeff1]"f"(coeff1),
+          [coeff2]"f"(coeff2), [coeff3]"f"(coeff3),
+          [coeff4]"f"(coeff4), [fb_end]"r"(fb_end)
+        : "$f0", "$f1", "$f2", "$f3", "$f4", "$f5", "$f6",
+          "$f7", "$f8", "$f9", "$f10", "$f11", "$f12",
+          "memory"
+    );
+}
+
+#define calc_thr_3gpp calc_thr_3gpp_mips
+#define psy_hp_filter psy_hp_filter_mips
+
+#endif /* HAVE_INLINE_ASM && HAVE_MIPSFPU */
+#endif /* AVCODEC_MIPS_AACPSY_MIPS_H */
diff --git a/libavcodec/mips/acelp_filters_mips.c b/libavcodec/mips/acelp_filters_mips.c
index 1e0845c..c8d980a 100644
--- a/libavcodec/mips/acelp_filters_mips.c
+++ b/libavcodec/mips/acelp_filters_mips.c
@@ -95,6 +95,7 @@
                   [fc_val_p] "=&f" (fc_val_p), [fc_val_m] "=&f" (fc_val_m),
                   [p_filter_coeffs_m] "+r" (p_filter_coeffs_m)
                 : [prec] "r" (prec)
+                : "memory"
             );
         }
         out[n] = v;
@@ -201,7 +202,7 @@
            [pole_coeffs] "r" (pole_coeffs)
          : "$f0", "$f1", "$f2", "$f3", "$f4", "$f5",
            "$f6", "$f7",  "$f8", "$f9", "$f10", "$f11",
-           "$f12", "$f13", "$f14", "$f15", "$f16"
+           "$f12", "$f13", "$f14", "$f15", "$f16", "memory"
     );
 }
 #endif /* HAVE_INLINE_ASM */
diff --git a/libavcodec/mips/acelp_vectors_mips.c b/libavcodec/mips/acelp_vectors_mips.c
index e00b34e..8770df8 100644
--- a/libavcodec/mips/acelp_vectors_mips.c
+++ b/libavcodec/mips/acelp_vectors_mips.c
@@ -88,7 +88,7 @@
         : [weight_coeff_a] "f" (weight_coeff_a),
           [weight_coeff_b] "f" (weight_coeff_b),
           [length] "r" (length), [a_end]"r"(a_end)
-        : "$f0", "$f1", "$f2", "$f3", "$f4", "$f5"
+        : "$f0", "$f1", "$f2", "$f3", "$f4", "$f5", "memory"
     );
 }
 #endif /* HAVE_INLINE_ASM */
diff --git a/libavcodec/mips/amrwbdec_mips.c b/libavcodec/mips/amrwbdec_mips.c
index 77fae6c..1d6ed2d 100644
--- a/libavcodec/mips/amrwbdec_mips.c
+++ b/libavcodec/mips/amrwbdec_mips.c
@@ -178,7 +178,7 @@
 
             : [output]"=&f"(output)
             : [fir_coef]"r"(fir_coef), [p_data]"r"(p_data)
-            : "$f0", "$f1", "$f2", "$f3", "$f4", "$f5"
+            : "$f0", "$f1", "$f2", "$f3", "$f4", "$f5", "memory"
         );
         out[i] = output;
     }
diff --git a/libavcodec/mips/celp_filters_mips.c b/libavcodec/mips/celp_filters_mips.c
index 5ecde00..ef5b07b 100644
--- a/libavcodec/mips/celp_filters_mips.c
+++ b/libavcodec/mips/celp_filters_mips.c
@@ -111,7 +111,7 @@
             : [old_out0]"f"(old_out0), [old_out1]"f"(old_out1),
               [old_out2]"f"(old_out2), [old_out3]"f"(old_out3),
               [filter_coeffs]"r"(filter_coeffs)
-            : "$f0", "$f1", "$f2", "$f3", "$f4"
+            : "$f0", "$f1", "$f2", "$f3", "$f4", "memory"
         );
 
         for (i = 5; i <= filter_length; i += 2) {
@@ -138,7 +138,7 @@
                   [old_out3]"+f"(old_out3),[p_filter_coeffs]"+r"(p_filter_coeffs),
                   [p_out]"+r"(p_out)
                 :
-                : "$f4", "$f5"
+                : "$f4", "$f5", "memory"
             );
             FFSWAP(float, old_out0, old_out2);
         }
@@ -188,6 +188,8 @@
                 : [fc_val]"=&f"(fc_val), [out_val]"+f"(out_val),
                   [out_val_i]"=&f"(out_val_i), [p_out]"+r"(p_out),
                   [p_filter_coeffs]"+r"(p_filter_coeffs)
+                :
+                : "memory"
             );
         }
         out[n] = out_val;
@@ -262,7 +264,7 @@
               [fc_val]"=&f"(fc_val), [p_filter_coeffs]"+r"(p_filter_coeffs),
               [p_in]"+r"(p_in), [i]"+r"(i)
             :
-            : "$f0", "$f1", "$f2", "$f3", "$f4", "$f5", "$f6", "$f7"
+            : "$f0", "$f1", "$f2", "$f3", "$f4", "$f5", "$f6", "$f7", "memory"
         );
 
         out[n+7] = sum_out8;
diff --git a/libavcodec/mips/celp_math_mips.c b/libavcodec/mips/celp_math_mips.c
index f0335c9..d7ccc23 100644
--- a/libavcodec/mips/celp_math_mips.c
+++ b/libavcodec/mips/celp_math_mips.c
@@ -75,7 +75,7 @@
 
         : [sum] "=&f" (sum), [a] "+r" (a), [b] "+r" (b)
         : [a_end]"r"(a_end), [length] "r" (length)
-        : "$f1", "$f2"
+        : "$f1", "$f2", "memory"
     );
     return sum;
 }
diff --git a/libavcodec/mips/compute_antialias_fixed.h b/libavcodec/mips/compute_antialias_fixed.h
index 13684f6..a967f67 100644
--- a/libavcodec/mips/compute_antialias_fixed.h
+++ b/libavcodec/mips/compute_antialias_fixed.h
@@ -237,6 +237,8 @@
               [temp_reg5] "=&r" (temp_reg5), [temp_reg6] "=&r" (temp_reg6)
             : [csa] "r" (csa), [ptr] "r" (ptr),
               [MAX_lo] "r" (MAX_lo)
+            : "memory", "hi", "lo", "$ac1hi", "$ac1lo", "$ac2hi", "$ac2lo",
+              "$ac3hi", "$ac3lo"
          );
 
         ptr += 18;
diff --git a/libavcodec/mips/compute_antialias_float.h b/libavcodec/mips/compute_antialias_float.h
index 289150d..0f6f03f 100644
--- a/libavcodec/mips/compute_antialias_float.h
+++ b/libavcodec/mips/compute_antialias_float.h
@@ -176,6 +176,7 @@
           [out1] "=&f" (out1), [out2] "=&f" (out2),
           [out3] "=&f" (out3), [out4] "=&f" (out4)
         : [csa] "r" (csa), [ptr_end] "r" (ptr_end)
+        : "memory"
     );
 }
 #define compute_antialias compute_antialias_mips_float
diff --git a/libavcodec/mips/fft_mips.c b/libavcodec/mips/fft_mips.c
index 8a2d86e..ae4ed30 100644
--- a/libavcodec/mips/fft_mips.c
+++ b/libavcodec/mips/fft_mips.c
@@ -408,6 +408,8 @@
               [tsin1]"+r"(tsin1), [tcos1]"+r"(tcos1),
               [in1]"+r"(in1), [in2]"+r"(in2),
               [in3]"+r"(in3), [in4]"+r"(in4)
+            :
+            : "memory"
         );
 
         z[j ].re = temp9;
@@ -472,6 +474,7 @@
             : [z1]"r"(z1), [z2]"r"(z2),
               [tsin1]"r"(tsin1), [tcos1]"r"(tcos1),
               [tsin2]"r"(tsin2), [tcos2]"r"(tcos2)
+            : "memory"
         );
 
         z1[1].re = temp9;
@@ -519,6 +522,7 @@
     int n=0;
 
     ff_fft_lut_init(fft_offsets_lut, 0, 1 << 16, &n);
+    ff_init_ff_cos_tabs(16);
 
 #if HAVE_INLINE_ASM
     s->fft_calc     = ff_fft_calc_mips;
diff --git a/libavcodec/mips/iirfilter_mips.c b/libavcodec/mips/iirfilter_mips.c
new file mode 100644
index 0000000..a5646cd
--- /dev/null
+++ b/libavcodec/mips/iirfilter_mips.c
@@ -0,0 +1,204 @@
+/*
+ * Copyright (c) 2012
+ *      MIPS Technologies, Inc., California.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the MIPS Technologies, Inc., nor the names of its
+ *    contributors may be used to endorse or promote products derived from
+ *    this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE MIPS TECHNOLOGIES, INC. ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE MIPS TECHNOLOGIES, INC. BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Author:  Bojan Zivkovic (bojan@mips.com)
+ *
+ * IIR filter optimized for MIPS floating-point architecture
+ *
+ * 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
+ * Reference: libavcodec/iirfilter.c
+ */
+
+#include "libavcodec/iirfilter.h"
+
+#if HAVE_INLINE_ASM
+typedef struct FFIIRFilterCoeffs {
+    int   order;
+    float gain;
+    int   *cx;
+    float *cy;
+} FFIIRFilterCoeffs;
+
+typedef struct FFIIRFilterState {
+    float x[1];
+} FFIIRFilterState;
+
+static void ff_iir_filter_flt_mips(const struct FFIIRFilterCoeffs *c,
+                                   struct FFIIRFilterState *s, int size,
+                                   const float *src, int sstep, float *dst, int dstep)
+{
+    if (c->order == 2) {
+        int i;
+        const float *src0 = src;
+        float       *dst0 = dst;
+        for (i = 0; i < size; i++) {
+            float in = *src0 * c->gain  + s->x[0] * c->cy[0] + s->x[1] * c->cy[1];
+            *dst0 = s->x[0] + in + s->x[1] * c->cx[1];
+            s->x[0] = s->x[1];
+            s->x[1] = in;
+            src0 += sstep;
+            dst0 += dstep;
+        }
+    } else if (c->order == 4) {
+        int i;
+        const float *src0 = src;
+        float       *dst0 = dst;
+        float four = 4.0;
+        float six  = 6.0;
+        for (i = 0; i < size; i += 4) {
+            float in1, in2, in3, in4;
+            float res1, res2, res3, res4;
+            float *x  = s->x;
+            float *cy = c->cy;
+            float gain = c->gain;
+            float src0_0 = src0[0      ];
+            float src0_1 = src0[sstep  ];
+            float src0_2 = src0[2*sstep];
+            float src0_3 = src0[3*sstep];
+
+            __asm__ volatile (
+                "lwc1   $f0,        0(%[cy])                    \n\t"
+                "lwc1   $f4,        0(%[x])                     \n\t"
+                "lwc1   $f5,        4(%[x])                     \n\t"
+                "lwc1   $f6,        8(%[x])                     \n\t"
+                "lwc1   $f7,        12(%[x])                    \n\t"
+                "mul.s  %[in1],     %[src0_0],  %[gain]         \n\t"
+                "mul.s  %[in2],     %[src0_1],  %[gain]         \n\t"
+                "mul.s  %[in3],     %[src0_2],  %[gain]         \n\t"
+                "mul.s  %[in4],     %[src0_3],  %[gain]         \n\t"
+                "lwc1   $f1,        4(%[cy])                    \n\t"
+                "madd.s %[in1],     %[in1],     $f0,    $f4     \n\t"
+                "madd.s %[in2],     %[in2],     $f0,    $f5     \n\t"
+                "madd.s %[in3],     %[in3],     $f0,    $f6     \n\t"
+                "madd.s %[in4],     %[in4],     $f0,    $f7     \n\t"
+                "lwc1   $f2,        8(%[cy])                    \n\t"
+                "madd.s %[in1],     %[in1],     $f1,    $f5     \n\t"
+                "madd.s %[in2],     %[in2],     $f1,    $f6     \n\t"
+                "madd.s %[in3],     %[in3],     $f1,    $f7     \n\t"
+                "lwc1   $f3,        12(%[cy])                   \n\t"
+                "add.s  $f8,        $f5,        $f7             \n\t"
+                "madd.s %[in1],     %[in1],     $f2,    $f6     \n\t"
+                "madd.s %[in2],     %[in2],     $f2,    $f7     \n\t"
+                "mul.s  $f9,        $f6,        %[six]          \n\t"
+                "mul.s  $f10,       $f7,        %[six]          \n\t"
+                "madd.s %[in1],     %[in1],     $f3,    $f7     \n\t"
+                "madd.s %[in2],     %[in2],     $f3,    %[in1]  \n\t"
+                "madd.s %[in3],     %[in3],     $f2,    %[in1]  \n\t"
+                "madd.s %[in4],     %[in4],     $f1,    %[in1]  \n\t"
+                "add.s  %[res1],    $f4,        %[in1]          \n\t"
+                "swc1   %[in1],     0(%[x])                     \n\t"
+                "add.s  $f0,        $f6,        %[in1]          \n\t"
+                "madd.s %[in3],     %[in3],     $f3,    %[in2]  \n\t"
+                "madd.s %[in4],     %[in4],     $f2,    %[in2]  \n\t"
+                "add.s  %[res2],    $f5,        %[in2]          \n\t"
+                "madd.s %[res1],    %[res1],    $f8,    %[four] \n\t"
+                "add.s  $f8,        $f7,        %[in2]          \n\t"
+                "swc1   %[in2],     4(%[x])                     \n\t"
+                "madd.s %[in4],     %[in4],     $f3,    %[in3]  \n\t"
+                "add.s  %[res3],    $f6,        %[in3]          \n\t"
+                "add.s  %[res1],    %[res1],    $f9             \n\t"
+                "madd.s %[res2],    %[res2],    $f0,    %[four] \n\t"
+                "swc1   %[in3],     8(%[x])                     \n\t"
+                "add.s  %[res4],    $f7,        %[in4]          \n\t"
+                "madd.s %[res3],    %[res3],    $f8,    %[four] \n\t"
+                "swc1   %[in4],     12(%[x])                    \n\t"
+                "add.s  %[res2],    %[res2],    $f10            \n\t"
+                "add.s  $f8,        %[in1],     %[in3]          \n\t"
+                "madd.s %[res3],    %[res3],    %[in1], %[six]  \n\t"
+                "madd.s %[res4],    %[res4],    $f8,    %[four] \n\t"
+                "madd.s %[res4],    %[res4],    %[in2], %[six]  \n\t"
+
+                : [in1]"=&f"(in1), [in2]"=&f"(in2),
+                  [in3]"=&f"(in3), [in4]"=&f"(in4),
+                  [res1]"=&f"(res1), [res2]"=&f"(res2),
+                  [res3]"=&f"(res3), [res4]"=&f"(res4)
+                : [src0_0]"f"(src0_0), [src0_1]"f"(src0_1),
+                  [src0_2]"f"(src0_2), [src0_3]"f"(src0_3),
+                  [gain]"f"(gain), [x]"r"(x), [cy]"r"(cy),
+                  [four]"f"(four), [six]"f"(six)
+                : "$f0", "$f1", "$f2", "$f3",
+                  "$f4", "$f5", "$f6", "$f7",
+                  "$f8", "$f9", "$f10",
+                  "memory"
+            );
+
+            dst0[0      ] = res1;
+            dst0[sstep  ] = res2;
+            dst0[2*sstep] = res3;
+            dst0[3*sstep] = res4;
+
+            src0 += 4*sstep;
+            dst0 += 4*dstep;
+        }
+    } else {
+        int i;
+        const float *src0 = src;
+        float       *dst0 = dst;
+        for (i = 0; i < size; i++) {
+            int j;
+            float in, res;
+            in = *src0 * c->gain;
+            for(j = 0; j < c->order; j++)
+                in += c->cy[j] * s->x[j];
+            res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1];
+            for(j = 1; j < c->order >> 1; j++)
+                res += (s->x[j] + s->x[c->order - j]) * c->cx[j];
+            for(j = 0; j < c->order - 1; j++)
+                s->x[j] = s->x[j + 1];
+            *dst0 = res;
+            s->x[c->order - 1] = in;
+            src0 += sstep;
+            dst0 += dstep;
+        }
+    }
+}
+#endif /* HAVE_INLINE_ASM */
+
+void ff_iir_filter_init_mips(FFIIRFilterContext *f) {
+#if HAVE_INLINE_ASM
+    f->filter_flt = ff_iir_filter_flt_mips;
+#endif /* HAVE_INLINE_ASM */
+}
diff --git a/libavcodec/mips/lsp_mips.h b/libavcodec/mips/lsp_mips.h
index 7497706..c55ef03 100644
--- a/libavcodec/mips/lsp_mips.h
+++ b/libavcodec/mips/lsp_mips.h
@@ -99,6 +99,7 @@
               [tmp]"=&f"(tmp), [f_j]"=&f"(f_j), [p_f]"+r"(p_f),
               [j]"+r"(j), [p_fi]"+r"(p_fi)
             : [i]"r"(i)
+            : "memory"
         );
         f[1] += val;
     }
diff --git a/libavcodec/mips/mpegaudiodsp_mips_fixed.c b/libavcodec/mips/mpegaudiodsp_mips_fixed.c
index d1b0277..047a833 100644
--- a/libavcodec/mips/mpegaudiodsp_mips_fixed.c
+++ b/libavcodec/mips/mpegaudiodsp_mips_fixed.c
@@ -1,4 +1,4 @@
-/*
+    /*
  * Copyright (c) 2012
  *      MIPS Technologies, Inc., California.
  *
@@ -166,7 +166,7 @@
           [sum1] "+r" (sum1), [w] "+r" (w), [temp3] "+r" (temp3)
         : [p] "r" (p), [samples] "r" (samples), [min_asm] "r" (min_asm),
           [max_asm] "r" (max_asm)
-        : "hi","lo"
+        : "memory", "hi","lo"
      );
 
      samples += incr;
@@ -290,7 +290,7 @@
               [w] "+r" (w), [w2] "+r" (w2), [samples] "+r" (samples),
               [samples2] "+r" (samples2), [temp3] "+r" (temp3)
             : [min_asm] "r" (min_asm), [max_asm] "r" (max_asm)
-            : "hi", "lo"
+            : "memory", "hi", "lo", "$ac1hi", "$ac1lo"
         );
 
         samples += incr;
@@ -340,7 +340,7 @@
           [w_asm2] "=&r" (w_asm2), [p_asm2] "=&r" (p_asm2), [sum1] "+r" (sum1)
         : [w] "r" (w), [p] "r" (p), [samples] "r" (samples), [min_asm] "r" (min_asm),
           [max_asm] "r" (max_asm)
-        : "hi", "lo"
+        : "memory", "hi", "lo", "$ac1hi", "$ac1lo"
      );
 
     *dither_state= temp1;
@@ -437,6 +437,8 @@
         : [in] "+r" (in), [t1] "=&r" (t1), [t2] "=&r" (t2), [t3] "=&r" (t3),
           [t4] "=&r" (t4), [t5] "=&r" (t5), [t6] "=&r" (t6),
           [t7] "=&r" (t7), [t8] "=&r" (t8)
+        :
+        : "memory"
     );
 
     for(j = 0; j < 2; j++) {
@@ -573,7 +575,8 @@
             : [C_2] "r" (C_2), [in1] "r" (in1), [tmp1] "r" (tmp1), [C_8] "r" (C_8),
               [C_4] "r" (C_4), [C_3] "r" (C_3), [C_1] "r" (C_1), [C_7] "r" (C_7),
               [C_3A] "r" (C_3A), [C_5] "r" (C_5)
-            : "hi", "lo"
+            : "memory", "hi", "lo", "$ac1hi", "$ac1lo", "$ac2hi", "$ac2lo",
+              "$ac3hi", "$ac3lo"
          );
     }
 
@@ -873,7 +876,8 @@
           [temp_reg5] "=&r" (temp_reg5), [temp_reg6] "=&r" (temp_reg6),
           [out] "+r" (out)
         : [tmp] "r" (tmp), [win] "r" (win), [buf] "r" (buf)
-        : "hi", "lo"
+        : "memory", "hi", "lo", "$ac1hi", "$ac1lo", "$ac2hi", "$ac2lo",
+          "$ac3hi", "$ac3lo"
     );
 }
 
diff --git a/libavcodec/mips/mpegaudiodsp_mips_float.c b/libavcodec/mips/mpegaudiodsp_mips_float.c
index 00a7f20..beb8e78 100644
--- a/libavcodec/mips/mpegaudiodsp_mips_float.c
+++ b/libavcodec/mips/mpegaudiodsp_mips_float.c
@@ -273,6 +273,7 @@
           [t_sample] "=&r" (t_sample)
         : [synth_buf] "r" (synth_buf), [window] "r" (window),
           [dither_state] "r" (dither_state), [incr1] "r" (incr1)
+        : "memory"
     );
 }
 
@@ -348,6 +349,7 @@
           [val16] "=f" (val16), [val23] "=f" (val23),
           [val24] "=f" (val24), [val31] "=f" (val31)
         : [tab] "r" (tab)
+        : "memory"
     );
 
     __asm__ volatile (
@@ -410,6 +412,7 @@
           [val19] "=f" (val19), [val20] "=f" (val20),
           [val27] "=f" (val27), [val28] "=f" (val28)
         : [tab] "r" (tab)
+        : "memory"
     );
 
     __asm__ volatile (
@@ -517,6 +520,7 @@
           [val17] "=f" (val17), [val22] "=f" (val22),
           [val25] "=f" (val25), [val30] "=f" (val30)
         : [tab] "r" (tab)
+        : "memory"
     );
 
     __asm__ volatile (
@@ -579,6 +583,7 @@
           [val18] "=f" (val18), [val21] "=f" (val21),
           [val26] "=f" (val26), [val29] "=f" (val29)
         : [tab] "r" (tab)
+        : "memory"
     );
 
     __asm__ volatile (
@@ -868,6 +873,7 @@
           [out3] "=&f" (out3), [out4] "=&f" (out4),
           [out5] "=&f" (out5)
         : [in] "r" (in)
+        : "memory"
     );
 
     /* loop 3 */
@@ -999,6 +1005,7 @@
           [c7] "=&f" (c7), [c8] "=&f" (c8),
           [c9] "=&f" (c9)
         : [in] "r" (in), [tmp] "r" (tmp)
+        : "memory"
     );
 
     /* loop 4 */
@@ -1212,6 +1219,7 @@
           [s2] "=&f" (s2), [s3] "=&f" (s3)
         : [tmp] "r" (tmp), [win] "r" (win),
           [buf] "r" (buf), [out] "r" (out)
+        : "memory"
     );
 }
 
diff --git a/libavcodec/mjpegbdec.c b/libavcodec/mjpegbdec.c
index 5284233..4327c10 100644
--- a/libavcodec/mjpegbdec.c
+++ b/libavcodec/mjpegbdec.c
@@ -45,10 +45,10 @@
     int buf_size = avpkt->size;
     MJpegDecodeContext *s = avctx->priv_data;
     const uint8_t *buf_end, *buf_ptr;
-    AVFrame *picture = data;
     GetBitContext hgb; /* for the header */
     uint32_t dqt_offs, dht_offs, sof_offs, sos_offs, second_field_offs;
     uint32_t field_size, sod_offs;
+    int ret;
 
     buf_ptr = buf;
     buf_end = buf + buf_size;
@@ -141,17 +141,13 @@
         return buf_size;
     }
 
-    *picture= *s->picture_ptr;
+    if ((ret = av_frame_ref(data, s->picture_ptr)) < 0)
+        return ret;
     *got_frame = 1;
 
-    if(!s->lossless){
-        picture->quality= FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]);
-        picture->qstride= 0;
-        picture->qscale_table= s->qscale_table;
-        memset(picture->qscale_table, picture->quality, (s->width+15)/16);
-        if(avctx->debug & FF_DEBUG_QP)
-            av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
-        picture->quality*= FF_QP2LAMBDA;
+    if (!s->lossless && avctx->debug & FF_DEBUG_QP) {
+        av_log(avctx, AV_LOG_DEBUG, "QP: %d\n",
+               FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]));
     }
 
     return buf_size;
diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index ba5753b..b439547 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -89,6 +89,7 @@
     avcodec_get_frame_defaults(&s->picture);
 
     s->avctx = avctx;
+    ff_hpeldsp_init(&s->hdsp, avctx->flags);
     ff_dsputil_init(&s->dsp, avctx);
     ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
     s->buffer_size   = 0;
@@ -259,9 +260,9 @@
         }
     }
     if (s->ls && !(s->bits <= 8 || nb_components == 1)) {
-        av_log_missing_feature(s->avctx,
-                               "For JPEG-LS anything except <= 8 bits/component"
-                               " or 16-bit gray", 0);
+        avpriv_report_missing_feature(s->avctx,
+                                      "JPEG-LS that is not <= 8 "
+                                      "bits/component or 16-bit gray");
         return AVERROR_PATCHWELCOME;
     }
     s->nb_components = nb_components;
@@ -294,7 +295,7 @@
     }
 
     if (s->ls && (s->h_max > 1 || s->v_max > 1)) {
-        av_log_missing_feature(s->avctx, "Subsampling in JPEG-LS", 0);
+        avpriv_report_missing_feature(s->avctx, "Subsampling in JPEG-LS");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -303,7 +304,6 @@
     if (   width != s->width || height != s->height
         || memcmp(s->h_count, h_count, sizeof(h_count))
         || memcmp(s->v_count, v_count, sizeof(v_count))) {
-        av_freep(&s->qscale_table);
 
         s->width      = width;
         s->height     = height;
@@ -325,13 +325,12 @@
 
         avcodec_set_dimensions(s->avctx, width, height);
 
-        s->qscale_table  = av_mallocz((s->width + 15) / 16);
         s->first_picture = 0;
     }
 
     if (s->interlaced && (s->bottom_field == !s->interlace_polarity)) {
         if (s->progressive) {
-            av_log_ask_for_sample(s->avctx, "progressively coded interlaced pictures not supported\n");
+            avpriv_request_sample(s->avctx, "progressively coded interlaced picture");
             return AVERROR_INVALIDDATA;
         }
     } else{
@@ -425,6 +424,10 @@
         s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV420P : AV_PIX_FMT_YUVJ420P;
         s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
         break;
+    case 0x41111100:
+        s->avctx->pix_fmt = AV_PIX_FMT_YUV411P;
+        s->avctx->color_range = s->cs_itu601 ? AVCOL_RANGE_MPEG : AVCOL_RANGE_JPEG;
+        break;
     default:
         av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x\n", pix_fmt_id);
         return AVERROR_PATCHWELCOME;
@@ -443,13 +446,9 @@
             s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
     }
 
-    if (s->picture_ptr->data[0])
-        s->avctx->release_buffer(s->avctx, s->picture_ptr);
-
-    if (ff_get_buffer(s->avctx, s->picture_ptr) < 0) {
-        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    av_frame_unref(s->picture_ptr);
+    if (ff_get_buffer(s->avctx, s->picture_ptr, AV_GET_BUFFER_FLAG_REF) < 0)
         return -1;
-    }
     s->picture_ptr->pict_type = AV_PICTURE_TYPE_I;
     s->picture_ptr->key_frame = 1;
     s->got_picture            = 1;
@@ -747,16 +746,19 @@
 
         i = 8 + ((-get_bits_count(&s->gb)) & 7);
         /* skip RSTn */
-        if (s->restart_count == 0 && show_bits(&s->gb, i) == (1 << i) - 1) {
-            int pos = get_bits_count(&s->gb);
-            align_get_bits(&s->gb);
-            while (get_bits_left(&s->gb) >= 8 && show_bits(&s->gb, 8) == 0xFF)
-                skip_bits(&s->gb, 8);
-            if (get_bits_left(&s->gb) >= 8 && (get_bits(&s->gb, 8) & 0xF8) == 0xD0) {
-                for (i = 0; i < nb_components; i++) /* reset dc */
-                    s->last_dc[i] = 1024;
-            } else
-                skip_bits_long(&s->gb, pos - get_bits_count(&s->gb));
+        if (s->restart_count == 0) {
+            if(   show_bits(&s->gb, i) == (1 << i) - 1
+               || show_bits(&s->gb, i) == 0xFF) {
+                int pos = get_bits_count(&s->gb);
+                align_get_bits(&s->gb);
+                while (get_bits_left(&s->gb) >= 8 && show_bits(&s->gb, 8) == 0xFF)
+                    skip_bits(&s->gb, 8);
+                if (get_bits_left(&s->gb) >= 8 && (get_bits(&s->gb, 8) & 0xF8) == 0xD0) {
+                    for (i = 0; i < nb_components; i++) /* reset dc */
+                        s->last_dc[i] = 1024;
+                } else
+                    skip_bits_long(&s->gb, pos - get_bits_count(&s->gb));
+            }
         }
     }
 }
@@ -996,7 +998,7 @@
                                               int linesize, int lowres)
 {
     switch (lowres) {
-    case 0: s->dsp.put_pixels_tab[1][0](dst, src, linesize, 8);
+    case 0: s->hdsp.put_pixels_tab[1][0](dst, src, linesize, 8);
         break;
     case 1: copy_block4(dst, src, linesize, linesize, 4);
         break;
@@ -1356,7 +1358,6 @@
         return AVERROR_INVALIDDATA;
 
     id   = get_bits_long(&s->gb, 32);
-    id   = av_be2ne32(id);
     len -= 6;
 
     if (s->avctx->debug & FF_DEBUG_STARTCODE)
@@ -1365,7 +1366,7 @@
     /* Buggy AVID, it puts EOI only at every 10th frame. */
     /* Also, this fourcc is used by non-avid files too, it holds some
        information, but it's always present in AVID-created files. */
-    if (id == AV_RL32("AVI1")) {
+    if (id == AV_RB32("AVI1")) {
         /* structure:
             4bytes      AVI1
             1bytes      polarity
@@ -1387,7 +1388,7 @@
 
 //    len -= 2;
 
-    if (id == AV_RL32("JFIF")) {
+    if (id == AV_RB32("JFIF")) {
         int t_w, t_h, v1, v2;
         skip_bits(&s->gb, 8); /* the trailing zero-byte */
         v1 = get_bits(&s->gb, 8);
@@ -1415,7 +1416,7 @@
         goto out;
     }
 
-    if (id == AV_RL32("Adob") && (get_bits(&s->gb, 8) == 'e')) {
+    if (id == AV_RB32("Adob") && (get_bits(&s->gb, 8) == 'e')) {
         if (s->avctx->debug & FF_DEBUG_PICT_INFO)
             av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
         skip_bits(&s->gb, 16); /* version */
@@ -1426,7 +1427,7 @@
         goto out;
     }
 
-    if (id == AV_RL32("LJIF")) {
+    if (id == AV_RB32("LJIF")) {
         if (s->avctx->debug & FF_DEBUG_PICT_INFO)
             av_log(s->avctx, AV_LOG_INFO,
                    "Pegasus lossless jpeg header found\n");
@@ -1453,10 +1454,9 @@
     /* Apple MJPEG-A */
     if ((s->start_code == APP1) && (len > (0x28 - 8))) {
         id   = get_bits_long(&s->gb, 32);
-        id   = av_be2ne32(id);
         len -= 4;
         /* Apple MJPEG-A */
-        if (id == AV_RL32("mjpg")) {
+        if (id == AV_RB32("mjpg")) {
 #if 0
             skip_bits(&s->gb, 32); /* field size */
             skip_bits(&s->gb, 32); /* pad field size */
@@ -1638,11 +1638,11 @@
     MJpegDecodeContext *s = avctx->priv_data;
     const uint8_t *buf_end, *buf_ptr;
     const uint8_t *unescaped_buf_ptr;
+    int hshift, vshift;
     int unescaped_buf_size;
     int start_code;
     int i, index;
     int ret = 0;
-    AVFrame *picture = data;
 
     buf_ptr = buf;
     buf_end = buf + buf_size;
@@ -1661,8 +1661,10 @@
         } else {
             av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n",
                    start_code, buf_end - buf_ptr);
-
-            init_get_bits(&s->gb, unescaped_buf_ptr, unescaped_buf_size * 8);
+            if ((ret = init_get_bits8(&s->gb, unescaped_buf_ptr, unescaped_buf_size)) < 0) {
+                av_log(avctx, AV_LOG_ERROR, "invalid buffer\n");
+                goto fail;
+            }
 
             s->start_code = start_code;
             if (s->avctx->debug & FF_DEBUG_STARTCODE)
@@ -1743,22 +1745,24 @@
                     if (s->bottom_field == !s->interlace_polarity)
                         break;
                 }
-                *picture   = *s->picture_ptr;
+                if ((ret = av_frame_ref(data, s->picture_ptr)) < 0)
+                    return ret;
                 *got_frame = 1;
                 s->got_picture = 0;
 
                 if (!s->lossless) {
-                    picture->quality      = FFMAX3(s->qscale[0],
-                                                   s->qscale[1],
-                                                   s->qscale[2]);
-                    picture->qstride      = 0;
-                    picture->qscale_table = s->qscale_table;
-                    memset(picture->qscale_table, picture->quality,
-                           (s->width + 15) / 16);
-                    if (avctx->debug & FF_DEBUG_QP)
-                        av_log(avctx, AV_LOG_DEBUG,
-                               "QP: %d\n", picture->quality);
-                    picture->quality *= FF_QP2LAMBDA;
+                    int qp = FFMAX3(s->qscale[0],
+                                    s->qscale[1],
+                                    s->qscale[2]);
+                    int qpw = (s->width + 15) / 16;
+                    AVBufferRef *qp_table_buf = av_buffer_alloc(qpw);
+                    if (qp_table_buf) {
+                        memset(qp_table_buf->data, qp, qpw);
+                        av_frame_set_qp_table(data, qp_table_buf, 0, FF_QSCALE_TYPE_MPEG1);
+                    }
+
+                    if(avctx->debug & FF_DEBUG_QP)
+                        av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", qp);
                 }
 
                 goto the_end;
@@ -1816,6 +1820,9 @@
     }
     if (s->upscale_v) {
         uint8_t *dst = &((uint8_t *)s->picture_ptr->data[s->upscale_v])[(s->height - 1) * s->linesize[s->upscale_v]];
+        int w;
+        avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
+        w = s->width >> hshift;
         av_assert0(avctx->pix_fmt == AV_PIX_FMT_YUVJ444P ||
                    avctx->pix_fmt == AV_PIX_FMT_YUV444P  ||
                    avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
@@ -1824,16 +1831,16 @@
             uint8_t *src1 = &((uint8_t *)s->picture_ptr->data[s->upscale_v])[i / 2 * s->linesize[s->upscale_v]];
             uint8_t *src2 = &((uint8_t *)s->picture_ptr->data[s->upscale_v])[(i + 1) / 2 * s->linesize[s->upscale_v]];
             if (src1 == src2) {
-                memcpy(dst, src1, s->width);
+                memcpy(dst, src1, w);
             } else {
-                for (index = 0; index < s->width; index++)
+                for (index = 0; index < w; index++)
                     dst[index] = (src1[index] + src2[index]) >> 1;
             }
             dst -= s->linesize[s->upscale_v];
         }
     }
     if (s->flipped && (s->avctx->flags & CODEC_FLAG_EMU_EDGE)) {
-        int hshift, vshift, j;
+        int j;
         avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &hshift, &vshift);
         for (index=0; index<4; index++) {
             uint8_t *dst = s->picture_ptr->data[index];
@@ -1866,11 +1873,10 @@
     MJpegDecodeContext *s = avctx->priv_data;
     int i, j;
 
-    if (s->picture_ptr && s->picture_ptr->data[0])
-        avctx->release_buffer(avctx, s->picture_ptr);
+    if (s->picture_ptr)
+        av_frame_unref(s->picture_ptr);
 
     av_free(s->buffer);
-    av_free(s->qscale_table);
     av_freep(&s->ljpeg_buffer);
     s->ljpeg_buffer_size = 0;
 
diff --git a/libavcodec/mjpegdec.h b/libavcodec/mjpegdec.h
index 01f4323..17665e4 100644
--- a/libavcodec/mjpegdec.h
+++ b/libavcodec/mjpegdec.h
@@ -34,6 +34,7 @@
 #include "avcodec.h"
 #include "get_bits.h"
 #include "dsputil.h"
+#include "hpeldsp.h"
 
 #define MAX_COMPONENTS 4
 
@@ -97,6 +98,7 @@
     uint64_t coefs_finished[MAX_COMPONENTS]; ///< bitmask of which coefs have been completely decoded (progressive mode)
     ScanTable scantable;
     DSPContext dsp;
+    HpelDSPContext hdsp;
 
     int restart_interval;
     int restart_count;
diff --git a/libavcodec/mlpdec.c b/libavcodec/mlpdec.c
index c763624..a4d5378 100644
--- a/libavcodec/mlpdec.c
+++ b/libavcodec/mlpdec.c
@@ -332,9 +332,10 @@
         return AVERROR_INVALIDDATA;
     }
     if (mh.num_substreams > MAX_SUBSTREAMS) {
-        av_log_ask_for_sample(m->avctx,
-               "Number of substreams %d is larger than the maximum supported "
-               "by the decoder.\n", mh.num_substreams);
+        avpriv_request_sample(m->avctx,
+                              "%d substreams (more than the "
+                              "maximum supported by the decoder)",
+                              mh.num_substreams);
         return AVERROR_PATCHWELCOME;
     }
 
@@ -442,9 +443,10 @@
     /* This should happen for TrueHD streams with >6 channels and MLP's noise
      * type. It is not yet known if this is allowed. */
     if (max_channel > MAX_MATRIX_CHANNEL_MLP && !s->noise_type) {
-        av_log_ask_for_sample(m->avctx,
-               "Number of channels %d is larger than the maximum supported "
-               "by the decoder.\n", max_channel + 2);
+        avpriv_request_sample(m->avctx,
+                              "%d channels (more than the "
+                              "maximum supported by the decoder)",
+                              max_channel + 2);
         return AVERROR_PATCHWELCOME;
     }
 
@@ -507,9 +509,9 @@
                                                             channel);
         }
         if ((unsigned)ch_assign > s->max_matrix_channel) {
-            av_log_ask_for_sample(m->avctx,
-                   "Assignment of matrix channel %d to invalid output channel %d.\n",
-                   ch, ch_assign);
+            avpriv_request_sample(m->avctx,
+                                  "Assignment of matrix channel %d to invalid output channel %d",
+                                  ch, ch_assign);
             return AVERROR_PATCHWELCOME;
         }
         s->ch_assign[ch_assign] = ch;
@@ -633,7 +635,7 @@
             /* TODO: Check validity of state data. */
 
             for (i = 0; i < order; i++)
-                fp->state[i] = get_sbits(gbp, state_bits) << state_shift;
+                fp->state[i] = state_bits ? get_sbits(gbp, state_bits) << state_shift : 0;
         }
     }
 
@@ -855,8 +857,8 @@
     if (s->data_check_present) {
         expected_stream_pos  = get_bits_count(gbp);
         expected_stream_pos += get_bits(gbp, 16);
-        av_log_ask_for_sample(m->avctx, "This file contains some features "
-                              "we have not tested yet.\n");
+        avpriv_request_sample(m->avctx,
+                              "Substreams with VLC block size check info");
     }
 
     if (s->blockpos + s->blocksize > m->access_unit_size) {
@@ -1023,10 +1025,8 @@
 
     /* get output buffer */
     frame->nb_samples = s->blockpos;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     data_32 = (int32_t *)frame->data[0];
     data_16 = (int16_t *)frame->data[0];
 
diff --git a/libavcodec/mmvideo.c b/libavcodec/mmvideo.c
index c61cd57..bf47f65 100644
--- a/libavcodec/mmvideo.c
+++ b/libavcodec/mmvideo.c
@@ -34,6 +34,7 @@
 #include "libavutil/intreadwrite.h"
 #include "avcodec.h"
 #include "bytestream.h"
+#include "internal.h"
 
 #define MM_PREAMBLE_SIZE    6
 
@@ -61,7 +62,6 @@
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
 
     avcodec_get_frame_defaults(&s->frame);
-    s->frame.reference = 3;
 
     return 0;
 }
@@ -188,10 +188,8 @@
     buf_size -= MM_PREAMBLE_SIZE;
     bytestream2_init(&s->gb, buf, buf_size);
 
-    if ((res = avctx->reget_buffer(avctx, &s->frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((res = ff_reget_buffer(avctx, &s->frame)) < 0)
         return res;
-    }
 
     switch(type) {
     case MM_TYPE_PALETTE   : res = mm_decode_pal(s); return avpkt->size;
@@ -210,8 +208,10 @@
 
     memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
 
+    if ((res = av_frame_ref(data, &s->frame)) < 0)
+        return res;
+
     *got_frame      = 1;
-    *(AVFrame*)data = s->frame;
 
     return avpkt->size;
 }
@@ -220,8 +220,7 @@
 {
     MmContext *s = avctx->priv_data;
 
-    if(s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+    av_frame_unref(&s->frame);
 
     return 0;
 }
diff --git a/libavcodec/motion_est.c b/libavcodec/motion_est.c
index d9d6a1f..9c36ab6 100644
--- a/libavcodec/motion_est.c
+++ b/libavcodec/motion_est.c
@@ -338,9 +338,11 @@
         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;
+    c->hpel_avg = s->hdsp.avg_pixels_tab;
+    if (s->no_rounding)
+        c->hpel_put = s->hdsp.put_no_rnd_pixels_tab;
+    else
+        c->hpel_put = s->hdsp.put_pixels_tab;
 
     if(s->linesize){
         c->stride  = s->linesize;
@@ -495,16 +497,16 @@
     if(mv4){
         int mot_xy= s->block_index[0];
 
-        s->current_picture.f.motion_val[0][mot_xy    ][0] = mx;
-        s->current_picture.f.motion_val[0][mot_xy    ][1] = my;
-        s->current_picture.f.motion_val[0][mot_xy + 1][0] = mx;
-        s->current_picture.f.motion_val[0][mot_xy + 1][1] = my;
+        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.f.motion_val[0][mot_xy    ][0] = mx;
-        s->current_picture.f.motion_val[0][mot_xy    ][1] = my;
-        s->current_picture.f.motion_val[0][mot_xy + 1][0] = mx;
-        s->current_picture.f.motion_val[0][mot_xy + 1][1] = my;
+        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;
     }
 }
 
@@ -586,8 +588,8 @@
             c->ymax = - 16*s->mb_y + s->height - 8*(block>>1);
         }
 
-        P_LEFT[0] = s->current_picture.f.motion_val[0][mot_xy - 1][0];
-        P_LEFT[1] = s->current_picture.f.motion_val[0][mot_xy - 1][1];
+        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);
 
@@ -596,10 +598,10 @@
             c->pred_x= pred_x4= P_LEFT[0];
             c->pred_y= pred_y4= P_LEFT[1];
         } else {
-            P_TOP[0]      = s->current_picture.f.motion_val[0][mot_xy - mot_stride             ][0];
-            P_TOP[1]      = s->current_picture.f.motion_val[0][mot_xy - mot_stride             ][1];
-            P_TOPRIGHT[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + off[block]][0];
-            P_TOPRIGHT[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + off[block]][1];
+            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);
@@ -640,9 +642,9 @@
                 dxy = ((my4 & 1) << 1) | (mx4 & 1);
 
                 if(s->no_rounding)
-                    s->dsp.put_no_rnd_pixels_tab[1][dxy](dest_y    , ref    , stride, h);
+                    s->hdsp.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);
+                    s->hdsp.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
@@ -656,8 +658,8 @@
             my4_sum+= my4;
         }
 
-        s->current_picture.f.motion_val[0][s->block_index[block]][0] = mx4;
-        s->current_picture.f.motion_val[0][s->block_index[block]][1] = 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;
     }
@@ -681,11 +683,11 @@
         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.f.data[1] + offset, s->uvlinesize, 8);
-            s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad + 8, s->last_picture.f.data[2] + offset, s->uvlinesize, 8);
+            s->hdsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad    , s->last_picture.f.data[1] + offset, s->uvlinesize, 8);
+            s->hdsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad + 8, s->last_picture.f.data[2] + offset, s->uvlinesize, 8);
         }else{
-            s->dsp.put_pixels_tab       [1][dxy](c->scratchpad    , s->last_picture.f.data[1] + offset, s->uvlinesize, 8);
-            s->dsp.put_pixels_tab       [1][dxy](c->scratchpad + 8, s->last_picture.f.data[2] + offset, s->uvlinesize, 8);
+            s->hdsp.put_pixels_tab       [1][dxy](c->scratchpad    , s->last_picture.f.data[1] + offset, s->uvlinesize, 8);
+            s->hdsp.put_pixels_tab       [1][dxy](c->scratchpad + 8, s->last_picture.f.data[2] + offset, s->uvlinesize, 8);
         }
 
         dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.f.data[1] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, c->scratchpad  , s->uvlinesize, 8);
@@ -793,9 +795,9 @@
                 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);
+                    s->hdsp.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);
+                    s->hdsp.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;
@@ -840,152 +842,6 @@
     }
 }
 
-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.f.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;
-    }
-    av_assert0(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->f.motion_val[0][xy], !!IS_INTERLACED(mb_type));
-        clip_input_mv(s, p->f.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->f.ref_index[0][4*mb_xy  ];
-            int field_select1= p->f.ref_index[0][4*mb_xy+2];
-            av_assert0(field_select0==0 ||field_select0==1);
-            av_assert0(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->f.motion_val[0][xy ];
-                *(uint32_t*)s->p_field_mv_table[1][field_select1][mb_xy] = *(uint32_t*)p->f.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->f.motion_val[0][xy ];
-                *(uint32_t*)s->b_field_mv_table[0][1][field_select1][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy2];
-                s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_FORWARD_I;
-            }
-
-            x = p->f.motion_val[0][xy ][0];
-            y = p->f.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->f.motion_val[0][xy2][0];
-            y = p->f.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->f.ref_index[1][4 * mb_xy    ];
-            int field_select1 = p->f.ref_index[1][4 * mb_xy + 2];
-            av_assert0(field_select0==0 ||field_select0==1);
-            av_assert0(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->f.motion_val[1][xy ];
-            *(uint32_t*)s->b_field_mv_table[1][1][field_select1][mb_xy] = *(uint32_t*)p->f.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->f.motion_val[1][xy ][0];
-            y = p->f.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->f.motion_val[1][xy2][0];
-            y = p->f.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->f.motion_val[0][xy][0];
-            y= p->f.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->f.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->f.motion_val[0][xy];
-                *(uint32_t*)s->b_bidir_back_mv_table[mb_xy] = *(uint32_t*)p->f.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->f.motion_val[0][xy];
-                s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_FORWARD;
-            }
-            x = p->f.motion_val[0][xy][0];
-            y = p->f.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->f.motion_val[1][xy];
-            s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BACKWARD;
-
-            x = p->f.motion_val[1][xy][0];
-            y = p->f.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;
-}
-
 static inline int get_penalty_factor(int lambda, int lambda2, int type){
     switch(type&0xFF){
     default:
@@ -1046,21 +902,6 @@
     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){
-            int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
-            int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
-            pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
-            c->mc_mb_var_sum_temp += (vard+128)>>8;
-            c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
-            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:
@@ -1074,16 +915,16 @@
             const int mot_stride = s->b8_stride;
             const int mot_xy = s->block_index[0];
 
-            P_LEFT[0] = s->current_picture.f.motion_val[0][mot_xy - 1][0];
-            P_LEFT[1] = s->current_picture.f.motion_val[0][mot_xy - 1][1];
+            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.f.motion_val[0][mot_xy - mot_stride    ][0];
-                P_TOP[1]      = s->current_picture.f.motion_val[0][mot_xy - mot_stride    ][1];
-                P_TOPRIGHT[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + 2][0];
-                P_TOPRIGHT[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + 2][1];
+                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);
@@ -1215,9 +1056,9 @@
 
         if(intra_score < dmin){
             mb_type= CANDIDATE_MB_TYPE_INTRA;
-            s->current_picture.f.mb_type[mb_y*s->mb_stride + mb_x] = CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup
+            s->current_picture.mb_type[mb_y*s->mb_stride + mb_x] = CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup
         }else
-            s->current_picture.f.mb_type[mb_y*s->mb_stride + mb_x] = 0;
+            s->current_picture.mb_type[mb_y*s->mb_stride + mb_x] = 0;
 
         {
             int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
@@ -1397,14 +1238,14 @@
         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);
+        s->hdsp.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);
+        s->hdsp.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
@@ -1583,7 +1424,7 @@
     ymin= xmin=(-32)>>shift;
     ymax= xmax=   31>>shift;
 
-    if (IS_8X8(s->next_picture.f.mb_type[mot_xy])) {
+    if (IS_8X8(s->next_picture.mb_type[mot_xy])) {
         s->mv_type= MV_TYPE_8X8;
     }else{
         s->mv_type= MV_TYPE_16X16;
@@ -1593,8 +1434,8 @@
         int index= s->block_index[i];
         int min, max;
 
-        c->co_located_mv[i][0] = s->next_picture.f.motion_val[0][index][0];
-        c->co_located_mv[i][1] = s->next_picture.f.motion_val[0][index][1];
+        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);
@@ -1683,7 +1524,7 @@
 
     c->skip=0;
 
-    if (s->codec_id == AV_CODEC_ID_MPEG4 && s->next_picture.f.mbskip_table[xy]) {
+    if (s->codec_id == AV_CODEC_ID_MPEG4 && s->next_picture.mbskip_table[xy]) {
         int score= direct_search(s, mb_x, mb_y); //FIXME just check 0,0
 
         score= ((unsigned)(score*score + 128*256))>>16;
@@ -1694,57 +1535,6 @@
         return;
     }
 
-    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 == AV_CODEC_ID_MPEG4)
         dmin= direct_search(s, mb_x, mb_y);
     else
@@ -1905,14 +1695,14 @@
                     int block;
                     for(block=0; block<4; block++){
                         int off= (block& 1) + (block>>1)*wrap;
-                        int mx = s->current_picture.f.motion_val[0][ xy + off ][0];
-                        int my = s->current_picture.f.motion_val[0][ xy + off ][1];
+                        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.f.mb_type[i] = CANDIDATE_MB_TYPE_INTRA;
+                            s->current_picture.mb_type[i] = CANDIDATE_MB_TYPE_INTRA;
                         }
                     }
                 }
diff --git a/libavcodec/motionpixels.c b/libavcodec/motionpixels.c
index 4fa45e9..00c1148 100644
--- a/libavcodec/motionpixels.c
+++ b/libavcodec/motionpixels.c
@@ -22,6 +22,7 @@
 #include "avcodec.h"
 #include "get_bits.h"
 #include "dsputil.h"
+#include "internal.h"
 
 #define MAX_HUFF_CODES 16
 
@@ -264,12 +265,8 @@
     GetBitContext gb;
     int i, count1, count2, sz, ret;
 
-    mp->frame.reference = 3;
-    mp->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
-    if ((ret = avctx->reget_buffer(avctx, &mp->frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, &mp->frame)) < 0)
         return ret;
-    }
 
     /* le32 bitstream msb first */
     av_fast_malloc(&mp->bswapbuf, &mp->bswapbuf_size, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
@@ -314,8 +311,9 @@
     ff_free_vlc(&mp->vlc);
 
 end:
+    if ((ret = av_frame_ref(data, &mp->frame)) < 0)
+        return ret;
     *got_frame       = 1;
-    *(AVFrame *)data = mp->frame;
     return buf_size;
 }
 
@@ -327,8 +325,7 @@
     av_freep(&mp->vpt);
     av_freep(&mp->hpt);
     av_freep(&mp->bswapbuf);
-    if (mp->frame.data[0])
-        avctx->release_buffer(avctx, &mp->frame);
+    av_frame_unref(&mp->frame);
 
     return 0;
 }
diff --git a/libavcodec/mpc7.c b/libavcodec/mpc7.c
index d4f5e2d..85d4e09 100644
--- a/libavcodec/mpc7.c
+++ b/libavcodec/mpc7.c
@@ -61,8 +61,7 @@
 
     /* Musepack SV7 is always stereo */
     if (avctx->channels != 2) {
-        av_log_ask_for_sample(avctx, "Unsupported number of channels: %d\n",
-                              avctx->channels);
+        avpriv_request_sample(avctx, "%d channels", avctx->channels);
         return AVERROR_PATCHWELCOME;
     }
 
@@ -225,10 +224,8 @@
 
     /* get output buffer */
     frame->nb_samples = MPC_FRAME_SIZE;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     av_fast_padded_malloc(&c->bits, &c->buf_size, buf_size);
     if (!c->bits)
diff --git a/libavcodec/mpc8.c b/libavcodec/mpc8.c
index 489737a..4d6ca89 100644
--- a/libavcodec/mpc8.c
+++ b/libavcodec/mpc8.c
@@ -134,7 +134,7 @@
     }
     channels = get_bits(&gb, 4) + 1;
     if (channels > 2) {
-        av_log_missing_feature(avctx, "Multichannel MPC SV8", 1);
+        avpriv_request_sample(avctx, "Multichannel MPC SV8");
         return AVERROR_PATCHWELCOME;
     }
     c->MSS = get_bits1(&gb);
@@ -254,10 +254,8 @@
 
     /* get output buffer */
     frame->nb_samples = MPC_FRAME_SIZE;
-    if ((res = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
         return res;
-    }
 
     keyframe = c->cur_frame == 0;
 
diff --git a/libavcodec/mpeg12.c b/libavcodec/mpeg12.c
index 0b09cbe..ecb67ed 100644
--- a/libavcodec/mpeg12.c
+++ b/libavcodec/mpeg12.c
@@ -26,13 +26,14 @@
  */
 
 //#define DEBUG
+#include "libavutil/avassert.h"
+#include "libavutil/timecode.h"
+
 #include "internal.h"
 #include "avcodec.h"
 #include "dsputil.h"
 #include "mpegvideo.h"
-#include "libavutil/avassert.h"
-#include "libavutil/timecode.h"
-
+#include "error_resilience.h"
 #include "mpeg12.h"
 #include "mpeg12data.h"
 #include "mpeg12decdata.h"
@@ -42,568 +43,6 @@
 #include "thread.h"
 
 
-#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
-
-static VLC mv_vlc;
-
-/* as H.263, but only 17 codes */
-static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
-{
-    int code, sign, val, 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 */
-    return sign_extend(val, 5 + shift);
-}
-
-static inline int mpeg1_decode_block_intra(MpegEncContext *s, int16_t *block, int n)
-{
-    int level, dc, diff, i, j, run;
-    int component;
-    RLTable *rl = &ff_rl_mpeg1;
-    uint8_t * const scantable    = s->intra_scantable.permutated;
-    const uint16_t *quant_matrix = s->intra_matrix;
-    const int qscale             = s->qscale;
-
-    /* DC coefficient */
-    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 * quant_matrix[0];
-    av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
-    i = 0;
-    {
-        OPEN_READER(re, &s->gb);
-        /* now quantify & encode AC coefficients */
-        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;
-}
-
-int ff_mpeg1_decode_block_intra(MpegEncContext *s, int16_t *block, int n)
-{
-    return mpeg1_decode_block_intra(s, block, n);
-}
-
-static inline int mpeg1_decode_block_inter(MpegEncContext *s, int16_t *block, int n)
-{
-    int level, i, j, run;
-    RLTable *rl = &ff_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 first coefficient, no need to add 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 coefficients */
-        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;
-}
-
-/**
- * Note: this function can read out of range and crash for corrupt streams.
- * Changing this would eat up any speed benefits it has.
- * Do not use "fast" flag if you need the code to be robust.
- */
-static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, int16_t *block, int n)
-{
-    int level, i, j, run;
-    RLTable *rl = &ff_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 first coefficient, no need to add 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 coefficients */
-        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, int16_t *block, int n)
-{
-    int level, i, j, run;
-    RLTable *rl = &ff_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 first coefficient, no need to add 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 coefficients */
-        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;
-}
-
-/**
- * Note: this function can read out of range and crash for corrupt streams.
- * Changing this would eat up any speed benefits it has.
- * Do not use "fast" flag if you need the code to be robust.
- */
-static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
-                                                    int16_t *block, int n)
-{
-    int level, i, j, run;
-    RLTable *rl = &ff_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 first coefficient, no need to add 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 coefficients */
-    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, int16_t *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 coefficient */
-    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);
-    av_dlog(s->avctx, "dc=%d\n", block[0]);
-    mismatch = block[0] ^ 1;
-    i = 0;
-    if (s->intra_vlc_format)
-        rl = &ff_rl_mpeg2;
-    else
-        rl = &ff_rl_mpeg1;
-
-    {
-        OPEN_READER(re, &s->gb);
-        /* now quantify & encode AC coefficients */
-        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;
-}
-
-/**
- * Note: this function can read out of range and crash for corrupt streams.
- * Changing this would eat up any speed benefits it has.
- * Do not use "fast" flag if you need the code to be robust.
- */
-static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, int16_t *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 coefficient */
-    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 = &ff_rl_mpeg2;
-    else
-        rl = &ff_rl_mpeg1;
-
-    {
-        OPEN_READER(re, &s->gb);
-        /* now quantify & encode AC coefficients */
-        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;
-}
-
 uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
 
 #define INIT_2D_VLC_RL(rl, static_size)\
@@ -670,13 +109,15 @@
 /******************************************/
 /* decoding */
 
+VLC ff_mv_vlc;
+
 VLC ff_dc_lum_vlc;
 VLC ff_dc_chroma_vlc;
 
-static VLC mbincr_vlc;
-static VLC mb_ptype_vlc;
-static VLC mb_btype_vlc;
-static VLC mb_pat_vlc;
+VLC ff_mbincr_vlc;
+VLC ff_mb_ptype_vlc;
+VLC ff_mb_btype_vlc;
+VLC ff_mb_pat_vlc;
 
 av_cold void ff_mpeg12_init_vlcs(void)
 {
@@ -691,20 +132,20 @@
         INIT_VLC_STATIC(&ff_dc_chroma_vlc,  DC_VLC_BITS, 12,
                         ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
                         ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
-        INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 17,
+        INIT_VLC_STATIC(&ff_mv_vlc, MV_VLC_BITS, 17,
                         &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
                         &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
-        INIT_VLC_STATIC(&mbincr_vlc, MBINCR_VLC_BITS, 36,
+        INIT_VLC_STATIC(&ff_mbincr_vlc, MBINCR_VLC_BITS, 36,
                         &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
                         &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
-        INIT_VLC_STATIC(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
+        INIT_VLC_STATIC(&ff_mb_pat_vlc, MB_PAT_VLC_BITS, 64,
                         &ff_mpeg12_mbPatTable[0][1], 2, 1,
                         &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
 
-        INIT_VLC_STATIC(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
+        INIT_VLC_STATIC(&ff_mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
                         &table_mb_ptype[0][1], 2, 1,
                         &table_mb_ptype[0][0], 2, 1, 64);
-        INIT_VLC_STATIC(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
+        INIT_VLC_STATIC(&ff_mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
                         &table_mb_btype[0][1], 2, 1,
                         &table_mb_btype[0][0], 2, 1, 64);
         ff_init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
@@ -715,1484 +156,6 @@
     }
 }
 
-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;
-    }
-}
-
-static void exchange_uv(MpegEncContext *s)
-{
-    int16_t (*tmp)[64];
-
-    tmp           = s->pblocks[4];
-    s->pblocks[4] = s->pblocks[5];
-    s->pblocks[5] = tmp;
-}
-
-/* motion type (for MPEG-2) */
-#define MT_FIELD 1
-#define MT_FRAME 2
-#define MT_16X8  2
-#define MT_DMV   3
-
-static int mpeg_decode_mb(MpegEncContext *s, int16_t block[12][64])
-{
-    int i, j, k, cbp, val, mb_type, motion_type;
-    const int mb_block_count = 4 + (1 << s->chroma_format);
-
-    av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
-
-    av_assert2(s->mb_skipped == 0);
-
-    if (s->mb_skip_run-- != 0) {
-        if (s->pict_type == AV_PICTURE_TYPE_P) {
-            s->mb_skipped = 1;
-            s->current_picture.f.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.f.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
-            else
-                mb_type = s->current_picture.f.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)) {
-                av_log(s->avctx, AV_LOG_ERROR, "skip with previntra\n");
-                return -1;
-            }
-            s->current_picture.f.mb_type[s->mb_x + s->mb_y*s->mb_stride] =
-                mb_type | MB_TYPE_SKIP;
-//            av_assert2(s->current_picture.f.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 AV_PICTURE_TYPE_I:
-        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 AV_PICTURE_TYPE_P:
-        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 AV_PICTURE_TYPE_B:
-        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;
-    }
-    av_dlog(s->avctx, "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 an 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;
-        // if 1, we memcpy blocks in xvmcvideo
-        if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
-            ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
-            if (s->swap_uv) {
-                exchange_uv(s);
-            }
-        }
-
-        if (s->codec_id == AV_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) {
-            av_assert2(mb_type & MB_TYPE_CBP);
-
-            s->mv_dir = MV_DIR_FORWARD;
-            if (s->picture_structure == PICT_FRAME) {
-                if (s->picture_structure == PICT_FRAME
-                    && !s->frame_pred_frame_dct)
-                    s->interlaced_dct = get_bits1(&s->gb);
-                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;
-            }
-
-            if (IS_QUANT(mb_type))
-                s->qscale = get_qscale(s);
-
-            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 {
-            av_assert2(mb_type & MB_TYPE_L0L1);
-            // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
-            /* get additional motion vector type */
-            if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct)
-                motion_type = MT_FRAME;
-            else {
-                motion_type = get_bits(&s->gb, 2);
-                if (s->picture_structure == PICT_FRAME && 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 = (mb_type >> 13) & 3;
-            av_dlog(s->avctx, "motion_type=%d\n", motion_type);
-            switch (motion_type) {
-            case MT_FRAME: /* or MT_16X8 */
-                if (s->picture_structure == PICT_FRAME) {
-                    mb_type |= MB_TYPE_16x16;
-                    s->mv_type = MV_TYPE_16X16;
-                    for (i = 0; i < 2; i++) {
-                        if (USES_LIST(mb_type, i)) {
-                            /* MT_FRAME */
-                            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 MPEG-1 */
-                            if (s->full_pel[i]) {
-                                s->mv[i][0][0] <<= 1;
-                                s->mv[i][0][1] <<= 1;
-                            }
-                        }
-                    }
-                } else {
-                    mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
-                    s->mv_type = MV_TYPE_16X8;
-                    for (i = 0; i < 2; i++) {
-                        if (USES_LIST(mb_type, i)) {
-                            /* MT_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 (i = 0; i < 2; i++) {
-                        if (USES_LIST(mb_type, i)) {
-                            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;
-                                av_dlog(s->avctx, "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;
-                                av_dlog(s->avctx, "fmy=%d\n", val);
-                            }
-                        }
-                    }
-                } else {
-                    av_assert0(!s->progressive_sequence);
-                    mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
-                    for (i = 0; i < 2; i++) {
-                        if (USES_LIST(mb_type, i)) {
-                            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:
-                if(s->progressive_sequence){
-                    av_log(s->avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n");
-                    return -1;
-                }
-                s->mv_type = MV_TYPE_DMV;
-                for (i = 0; i < 2; i++) {
-                    if (USES_LIST(mb_type, i)) {
-                        int dmx, dmy, mx, my, m;
-                        const int my_shift = s->picture_structure == PICT_FRAME;
-
-                        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] >> my_shift);
-                        dmy = get_dmv(s);
-
-
-                        s->last_mv[i][0][1] = my << my_shift;
-                        s->last_mv[i][1][1] = my << my_shift;
-
-                        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]);
-
-            cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
-            if (mb_block_count > 6) {
-                 cbp <<= mb_block_count - 6;
-                 cbp  |= get_bits(&s->gb, mb_block_count - 6);
-                 s->dsp.clear_blocks(s->block[6]);
-            }
-            if (cbp <= 0) {
-                av_log(s->avctx, AV_LOG_ERROR, "invalid cbp %d at %d %d\n", cbp, s->mb_x, s->mb_y);
-                return -1;
-            }
-
-            //if 1, we memcpy blocks in xvmcvideo
-            if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
-                ff_xvmc_pack_pblocks(s, cbp);
-                if (s->swap_uv) {
-                    exchange_uv(s);
-                }
-            }
-
-            if (s->codec_id == AV_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.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
-
-    return 0;
-}
-
-static av_cold int mpeg_decode_init(AVCodecContext *avctx)
-{
-    Mpeg1Context *s = avctx->priv_data;
-    MpegEncContext *s2 = &s->mpeg_enc_ctx;
-    int i;
-
-    /* we need some permutation to store matrices,
-     * until MPV_common_init() sets the real permutation. */
-    for (i = 0; i < 64; i++)
-       s2->dsp.idct_permutation[i]=i;
-
-    ff_MPV_decode_defaults(s2);
-
-    s->mpeg_enc_ctx.avctx  = avctx;
-    s->mpeg_enc_ctx.flags  = avctx->flags;
-    s->mpeg_enc_ctx.flags2 = avctx->flags2;
-    ff_mpeg12_common_init(&s->mpeg_enc_ctx);
-    ff_mpeg12_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;
-    avctx->color_range = AVCOL_RANGE_MPEG;
-    if (avctx->codec->id == AV_CODEC_ID_MPEG1VIDEO)
-        avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
-    else
-        avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
-    return 0;
-}
-
-static int mpeg_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from)
-{
-    Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
-    MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
-    int err;
-
-    if (avctx == avctx_from || !ctx_from->mpeg_enc_ctx_allocated || !s1->context_initialized)
-        return 0;
-
-    err = ff_mpeg_update_thread_context(avctx, avctx_from);
-    if (err) return err;
-
-    if (!ctx->mpeg_enc_ctx_allocated)
-        memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
-
-    if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
-        s->picture_number++;
-
-    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]];
-    }
-}
-
-static const enum AVPixelFormat mpeg1_hwaccel_pixfmt_list_420[] = {
-#if CONFIG_MPEG_XVMC_DECODER
-    AV_PIX_FMT_XVMC_MPEG2_IDCT,
-    AV_PIX_FMT_XVMC_MPEG2_MC,
-#endif
-#if CONFIG_MPEG1_VDPAU_HWACCEL
-    AV_PIX_FMT_VDPAU_MPEG1,
-#endif
-    AV_PIX_FMT_YUV420P,
-    AV_PIX_FMT_NONE
-};
-
-static const enum AVPixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
-#if CONFIG_MPEG_XVMC_DECODER
-    AV_PIX_FMT_XVMC_MPEG2_IDCT,
-    AV_PIX_FMT_XVMC_MPEG2_MC,
-#endif
-#if CONFIG_MPEG2_VDPAU_HWACCEL
-    AV_PIX_FMT_VDPAU_MPEG2,
-#endif
-#if CONFIG_MPEG2_DXVA2_HWACCEL
-    AV_PIX_FMT_DXVA2_VLD,
-#endif
-#if CONFIG_MPEG2_VAAPI_HWACCEL
-    AV_PIX_FMT_VAAPI_VLD,
-#endif
-    AV_PIX_FMT_YUV420P,
-    AV_PIX_FMT_NONE
-};
-
-static inline int uses_vdpau(AVCodecContext *avctx) {
-    return avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG1 || avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG2;
-}
-
-static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
-{
-    Mpeg1Context *s1 = avctx->priv_data;
-    MpegEncContext *s = &s1->mpeg_enc_ctx;
-
-    if(s->chroma_format < 2) {
-        enum AVPixelFormat res;
-        res = avctx->get_format(avctx,
-                                avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO ?
-                                mpeg1_hwaccel_pixfmt_list_420 :
-                                mpeg2_hwaccel_pixfmt_list_420);
-        if (res != AV_PIX_FMT_XVMC_MPEG2_IDCT && res != AV_PIX_FMT_XVMC_MPEG2_MC) {
-            avctx->xvmc_acceleration = 0;
-        } else if (!avctx->xvmc_acceleration) {
-            avctx->xvmc_acceleration = 2;
-        }
-        return res;
-    } else if(s->chroma_format == 2)
-        return AV_PIX_FMT_YUV422P;
-    else
-        return AV_PIX_FMT_YUV444P;
-}
-
-/* Call this function when we know all parameters.
- * It may be called in different places for MPEG-1 and MPEG-2. */
-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_width           != s->width                ||
-        s1->save_height          != s->height               ||
-        s1->save_aspect_info     != s->aspect_ratio_info    ||
-        s1->save_progressive_seq != s->progressive_sequence ||
-        0)
-    {
-
-        if (s1->mpeg_enc_ctx_allocated) {
-            ParseContext pc = s->parse_context;
-            s->parse_context.buffer = 0;
-            ff_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);
-        if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->bit_rate) {
-            avctx->rc_max_rate = s->bit_rate;
-        } else if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && s->bit_rate &&
-                   (s->bit_rate != 0x3FFFF*400 || s->vbv_delay != 0xFFFF)) {
-            avctx->bit_rate = s->bit_rate;
-        }
-        s1->save_aspect_info     = s->aspect_ratio_info;
-        s1->save_width           = s->width;
-        s1->save_height          = s->height;
-        s1->save_progressive_seq = s->progressive_sequence;
-
-        /* 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->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
-            //MPEG-1 fps
-            avctx->time_base.den = ff_mpeg12_frame_rate_tab[s->frame_rate_index].num;
-            avctx->time_base.num = ff_mpeg12_frame_rate_tab[s->frame_rate_index].den;
-            //MPEG-1 aspect
-            avctx->sample_aspect_ratio = av_d2q(1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
-            avctx->ticks_per_frame=1;
-        } else {//MPEG-2
-        //MPEG-2 fps
-            av_reduce(&s->avctx->time_base.den,
-                      &s->avctx->time_base.num,
-                      ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
-                      ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
-                      1 << 30);
-            avctx->ticks_per_frame = 2;
-            //MPEG-2 aspect
-            if (s->aspect_ratio_info > 1) {
-                AVRational dar =
-                    av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
-                                      (AVRational) {s1->pan_scan.width, s1->pan_scan.height}),
-                             (AVRational) {s->width, s->height});
-
-                // we ignore the spec here and guess a bit as reality does not match the spec, see for example
-                // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
-                // issue1613, 621, 562
-                if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
-                   (av_cmp_q(dar, (AVRational) {4, 3}) && av_cmp_q(dar, (AVRational) {16, 9}))) {
-                    s->avctx->sample_aspect_ratio =
-                        av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
-                                 (AVRational) {s->width, s->height});
-                } else {
-                    s->avctx->sample_aspect_ratio =
-                        av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
-                                 (AVRational) {s1->pan_scan.width, s1->pan_scan.height});
-//issue1613 4/3 16/9 -> 16/9
-//res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
-//widescreen-issue562.mpg 4/3 16/9 -> 16/9
-//                    s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
-                    av_dlog(avctx, "A %d/%d\n",
-                            ff_mpeg2_aspect[s->aspect_ratio_info].num, ff_mpeg2_aspect[s->aspect_ratio_info].den);
-                    av_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
-                            s->avctx->sample_aspect_ratio.den);
-                }
-            } else {
-                s->avctx->sample_aspect_ratio =
-                    ff_mpeg2_aspect[s->aspect_ratio_info];
-            }
-        } // MPEG-2
-
-        avctx->pix_fmt = mpeg_get_pixelformat(avctx);
-        avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
-        // until then pix_fmt may be changed right after codec init
-        if (avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT ||
-            avctx->hwaccel || uses_vdpau(avctx))
-            if (avctx->idct_algo == FF_IDCT_AUTO)
-                avctx->idct_algo = FF_IDCT_SIMPLE;
-
-        /* Quantization matrices may need reordering
-         * if DCT permutation is changed. */
-        memcpy(old_permutation, s->dsp.idct_permutation, 64 * sizeof(uint8_t));
-
-        if (ff_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;
-
-    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);
-    s->vbv_delay = vbv_delay;
-    if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
-        s->full_pel[0] = get_bits1(&s->gb);
-        f_code = get_bits(&s->gb, 3);
-        if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
-            return -1;
-        f_code += !f_code;
-        s->mpeg_f_code[0][0] = f_code;
-        s->mpeg_f_code[0][1] = f_code;
-    }
-    if (s->pict_type == AV_PICTURE_TYPE_B) {
-        s->full_pel[1] = get_bits1(&s->gb);
-        f_code = get_bits(&s->gb, 3);
-        if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
-            return -1;
-        f_code += !f_code;
-        s->mpeg_f_code[1][0] = f_code;
-        s->mpeg_f_code[1][1] = f_code;
-    }
-    s->current_picture.f.pict_type = s->pict_type;
-    s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
-
-    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;
-    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); /* profile 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;
-
-    av_dlog(s->avctx, "sequence extension\n");
-    s->codec_id      = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
-
-    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) {
-        s->avctx->color_primaries = get_bits(&s->gb, 8);
-        s->avctx->color_trc       = get_bits(&s->gb, 8);
-        s->avctx->colorspace      = get_bits(&s->gb, 8);
-    }
-    w = get_bits(&s->gb, 14);
-    skip_bits(&s->gb, 1); //marker
-    h = get_bits(&s->gb, 14);
-    // remaining 3 bits are zero padding
-
-    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 int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra)
-{
-    int i;
-
-    for (i = 0; i < 64; i++) {
-        int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
-        int v = get_bits(&s->gb, 8);
-        if (v == 0) {
-            av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
-            return -1;
-        }
-        if (intra && i == 0 && v != 8) {
-            av_log(s->avctx, AV_LOG_DEBUG, "intra matrix specifies invalid DC quantizer %d, ignoring\n", v);
-            v = 8; // needed by pink.mpg / issue1046
-        }
-        matrix0[j] = v;
-        if (matrix1)
-            matrix1[j] = v;
-    }
-    return 0;
-}
-
-static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
-{
-    av_dlog(s->avctx, "matrix extension\n");
-
-    if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
-    if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
-    if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL           , 1);
-    if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL           , 0);
-}
-
-static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
-{
-    MpegEncContext *s = &s1->mpeg_enc_ctx;
-
-    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);
-    if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
-        av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
-        if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
-            if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
-                s->pict_type = AV_PICTURE_TYPE_I;
-            else
-                s->pict_type = AV_PICTURE_TYPE_P;
-        } else
-            s->pict_type = AV_PICTURE_TYPE_B;
-        s->current_picture.f.pict_type = s->pict_type;
-        s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
-    }
-    s->mpeg_f_code[0][0] += !s->mpeg_f_code[0][0];
-    s->mpeg_f_code[0][1] += !s->mpeg_f_code[0][1];
-    s->mpeg_f_code[1][0] += !s->mpeg_f_code[1][0];
-    s->mpeg_f_code[1][1] += !s->mpeg_f_code[1][1];
-
-    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->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 */
-    av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
-    av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
-    av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
-    av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
-    av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
-    av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
-    av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
-    av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
-    av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
-}
-
-static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
-{
-    AVCodecContext *avctx = s->avctx;
-    Mpeg1Context *s1 = (Mpeg1Context*)s;
-
-    /* start frame decoding */
-    if (s->first_field || s->picture_structure == PICT_FRAME) {
-        if (ff_MPV_frame_start(s, avctx) < 0)
-            return -1;
-
-        ff_mpeg_er_frame_start(s);
-
-        /* first check if we must repeat the frame */
-        s->current_picture_ptr->f.repeat_pict = 0;
-        if (s->repeat_first_field) {
-            if (s->progressive_sequence) {
-                if (s->top_field_first)
-                    s->current_picture_ptr->f.repeat_pict = 4;
-                else
-                    s->current_picture_ptr->f.repeat_pict = 2;
-            } else if (s->progressive_frame) {
-                s->current_picture_ptr->f.repeat_pict = 1;
-            }
-        }
-
-        *s->current_picture_ptr->f.pan_scan = s1->pan_scan;
-
-        if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
-            ff_thread_finish_setup(avctx);
-    } else { // second field
-        int i;
-
-        if (!s->current_picture_ptr) {
-            av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
-            return -1;
-        }
-
-        if (s->avctx->hwaccel &&
-            (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
-            if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
-                av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode first field\n");
-        }
-
-        for (i = 0; i < 4; i++) {
-            s->current_picture.f.data[i] = s->current_picture_ptr->f.data[i];
-            if (s->picture_structure == PICT_BOTTOM_FIELD) {
-                s->current_picture.f.data[i] += s->current_picture_ptr->f.linesize[i];
-            }
-        }
-    }
-
-    if (avctx->hwaccel) {
-        if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
-            return -1;
-    }
-
-// MPV_frame_start will call this function too,
-// but we need to call it on every field
-    if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
-        if (ff_xvmc_field_start(s, avctx) < 0)
-            return -1;
-
-    return 0;
-}
-
-#define DECODE_SLICE_ERROR -1
-#define DECODE_SLICE_OK     0
-
-/**
- * Decode a slice.
- * MpegEncContext.mb_y must be set to the MB row from the startcode.
- * @return DECODE_SLICE_ERROR if the slice is damaged,
- *         DECODE_SLICE_OK if this slice is OK
- */
-static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
-                             const uint8_t **buf, int buf_size)
-{
-    AVCodecContext *avctx = s->avctx;
-    const int lowres      = s->avctx->lowres;
-    const int field_pic   = s->picture_structure != PICT_FRAME;
-
-    s->resync_mb_x =
-    s->resync_mb_y = -1;
-
-    av_assert0(mb_y < s->mb_height);
-
-    init_get_bits(&s->gb, *buf, buf_size * 8);
-    if(s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
-        skip_bits(&s->gb, 3);
-
-    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;
-
-    if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
-        skip_bits1(&s->gb);
-    } else {
-        while (get_bits_left(&s->gb) > 0) {
-            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;
-            }
-        }
-    }
-
-    if (s->mb_x >= (unsigned)s->mb_width) {
-        av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
-        return -1;
-    }
-
-    if (avctx->hwaccel) {
-        const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
-        int start_code = -1;
-        buf_end = avpriv_mpv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
-        if (buf_end < *buf + buf_size)
-            buf_end -= 4;
-        s->mb_y = mb_y;
-        if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
-            return DECODE_SLICE_ERROR;
-        *buf = buf_end;
-        return DECODE_SLICE_OK;
-    }
-
-    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 == AV_PICTURE_TYPE_I ? "I" : (s->pict_type == AV_PICTURE_TYPE_P ? "P" : (s->pict_type == AV_PICTURE_TYPE_B ? "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 (;;) {
-        // If 1, we memcpy blocks in xvmcvideo.
-        if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
-            ff_xvmc_init_block(s); // set s->block
-
-        if (mpeg_decode_mb(s, s->block) < 0)
-            return -1;
-
-        if (s->current_picture.f.motion_val[0] && !s->encoding) { // note motion_val is normally NULL unless we want to extract the MVs
-            const int wrap = s->b8_stride;
-            int xy         = s->mb_x * 2 + s->mb_y * 2 * wrap;
-            int b8_xy      = 4 * (s->mb_x + s->mb_y * s->mb_stride);
-            int motion_x, motion_y, dir, i;
-
-            for (i = 0; i < 2; i++) {
-                for (dir = 0; dir < 2; dir++) {
-                    if (s->mb_intra || (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
-                        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.f.motion_val[dir][xy    ][0] = motion_x;
-                    s->current_picture.f.motion_val[dir][xy    ][1] = motion_y;
-                    s->current_picture.f.motion_val[dir][xy + 1][0] = motion_x;
-                    s->current_picture.f.motion_val[dir][xy + 1][1] = motion_y;
-                    s->current_picture.f.ref_index [dir][b8_xy    ] =
-                    s->current_picture.f.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
-                    av_assert2(s->field_select[dir][i] == 0 || s->field_select[dir][i] == 1);
-                }
-                xy += wrap;
-                b8_xy +=2;
-            }
-        }
-
-        s->dest[0] += 16 >> lowres;
-        s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
-        s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
-
-        ff_MPV_decode_mb(s, s->block);
-
-        if (++s->mb_x >= s->mb_width) {
-            const int mb_size = 16 >> s->avctx->lowres;
-
-            ff_mpeg_draw_horiz_band(s, mb_size*(s->mb_y >> field_pic), mb_size);
-            ff_MPV_report_decode_progress(s);
-
-            s->mb_x = 0;
-            s->mb_y += 1 << field_pic;
-
-            if (s->mb_y >= s->mb_height) {
-                int left   = get_bits_left(&s->gb);
-                int is_d10 = s->chroma_format == 2 && s->pict_type == AV_PICTURE_TYPE_I && 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 >= 32 && !is_d10) {
-                    GetBitContext gb = s->gb;
-                    align_get_bits(&gb);
-                    if (show_bits(&gb, 24) == 0x060E2B) {
-                        av_log(avctx, AV_LOG_DEBUG, "Invalid MXF data found in video stream\n");
-                        is_d10 = 1;
-                    }
-                }
-
-                if (left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
-                    || ((avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_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 increment again */
-            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;
-                }
-            }
-            if (s->mb_skip_run) {
-                int i;
-                if (s->pict_type == AV_PICTURE_TYPE_I) {
-                    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 == AV_PICTURE_TYPE_P) {
-                    /* 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) & 1;
-                } else {
-                    /* 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];
-                }
-            }
-        }
-    }
-eos: // end of slice
-    *buf += (get_bits_count(&s->gb)-1)/8;
-    av_dlog(s, "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   = *(void**)arg;
-    const uint8_t *buf  = s->gb.buffer;
-    int mb_y            = s->start_mb_y;
-    const int field_pic = s->picture_structure != PICT_FRAME;
-
-    s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
-
-    for (;;) {
-        uint32_t start_code;
-        int ret;
-
-        ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
-        emms_c();
-        av_dlog(c, "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->er.error_count);
-        if (ret < 0) {
-            if (c->err_recognition & AV_EF_EXPLODE)
-                return ret;
-            if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
-                ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
-        } else {
-            ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_AC_END | ER_DC_END | ER_MV_END);
-        }
-
-        if (s->mb_y == s->end_mb_y)
-            return 0;
-
-        start_code = -1;
-        buf = avpriv_mpv_find_start_code(buf, s->gb.buffer_end, &start_code);
-        mb_y= start_code - SLICE_MIN_START_CODE;
-        if(s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
-            mb_y += (*buf&0xE0)<<2;
-        mb_y <<= field_pic;
-        if (s->picture_structure == PICT_BOTTOM_FIELD)
-            mb_y++;
-        if (mb_y < 0 || mb_y >= s->end_mb_y)
-            return -1;
-    }
-}
-
-/**
- * Handle slice ends.
- * @return 1 if it seems to be the last slice
- */
-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;
-
-    if (s->avctx->hwaccel) {
-        if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
-            av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
-    }
-
-    if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
-        ff_xvmc_field_end(s);
-
-    /* end of slice reached */
-    if (/*s->mb_y << field_pic == s->mb_height &&*/ !s->first_field && !s->first_slice) {
-        /* end of image */
-
-        s->current_picture_ptr->f.qscale_type = FF_QSCALE_TYPE_MPEG2;
-
-        ff_er_frame_end(&s->er);
-
-        ff_MPV_frame_end(s);
-
-        if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
-            *pict = s->current_picture_ptr->f;
-            ff_print_debug_info(s, pict);
-        } else {
-            if (avctx->active_thread_type & FF_THREAD_FRAME)
-                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 = s->last_picture_ptr->f;
-                 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);
-    s->aspect_ratio_info = get_bits(&s->gb, 4);
-    if (s->aspect_ratio_info == 0) {
-        av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
-        if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
-            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)) {
-        load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
-    } 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)) {
-        load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
-    } 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 MPEG-2 parameters so that it emulates MPEG-1 */
-    s->progressive_sequence = 1;
-    s->progressive_frame    = 1;
-    s->picture_structure    = PICT_FRAME;
-    s->first_field          = 0;
-    s->frame_pred_frame_dct = 1;
-    s->chroma_format        = 1;
-    s->codec_id             = s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
-    s->out_format           = FMT_MPEG1;
-    s->swap_uv              = 0; // AFAIK VCR2 does not 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 MPEG-1 context decoding */
-    s->out_format = FMT_MPEG1;
-    if (s1->mpeg_enc_ctx_allocated) {
-        ff_MPV_common_end(s);
-    }
-    s->width  = avctx->coded_width;
-    s->height = avctx->coded_height;
-    avctx->has_b_frames = 0; // true?
-    s->low_delay = 1;
-
-    avctx->pix_fmt = mpeg_get_pixelformat(avctx);
-    avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
-
-    if (avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel || uses_vdpau(avctx))
-        if (avctx->idct_algo == FF_IDCT_AUTO)
-            avctx->idct_algo = FF_IDCT_SIMPLE;
-
-    if (ff_MPV_common_init(s) < 0)
-        return -1;
-    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->first_field           = 0;
-    s->frame_pred_frame_dct  = 1;
-    s->chroma_format         = 1;
-    if (s->codec_tag == AV_RL32("BW10")) {
-        s->codec_id              = s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
-    } else {
-        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
-        s->codec_id              = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
-    }
-    s1->save_width           = s->width;
-    s1->save_height          = s->height;
-    s1->save_progressive_seq = s->progressive_sequence;
-    return 0;
-}
-
-
-static void mpeg_decode_user_data(AVCodecContext *avctx,
-                                  const uint8_t *p, int buf_size)
-{
-    Mpeg1Context *s = avctx->priv_data;
-    const uint8_t *buf_end = p + buf_size;
-
-    if(buf_size > 29){
-        int i;
-        for(i=0; i<20; i++)
-            if(!memcmp(p+i, "\0TMPGEXS\0", 9)){
-                s->tmpgexs= 1;
-            }
-
-/*        for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
-            av_log(avctx, AV_LOG_ERROR, "%c", p[i]);
-        }
-            av_log(avctx, AV_LOG_ERROR, "\n");*/
-    }
-
-    /* we parse the DTG active format information */
-    if (buf_end - p >= 5 &&
-        p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
-        int flags = p[4];
-        p += 5;
-        if (flags & 0x80) {
-            /* skip event id */
-            p += 2;
-        }
-        if (flags & 0x40) {
-            if (buf_end - p < 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 broken_link;
-    int64_t tc;
-
-    init_get_bits(&s->gb, buf, buf_size*8);
-
-    tc = avctx->timecode_frame_start = get_bits(&s->gb, 25);
-
-    s->closed_gop = get_bits1(&s->gb);
-    /*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) {
-        char tcbuf[AV_TIMECODE_STR_SIZE];
-        av_timecode_make_mpeg_tc_string(tcbuf, tc);
-        av_log(s->avctx, AV_LOG_DEBUG,
-               "GOP (%s) closed_gop=%d broken_link=%d\n",
-               tcbuf, s->closed_gop, broken_link);
-    }
-}
 /**
  * Find the end of the current frame in the bitstream.
  * @return the position of the first byte of the next frame, or -1
@@ -2227,7 +190,7 @@
             }
             state++;
         } else {
-            i = avpriv_mpv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
+            i = avpriv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
             if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) {
                 i++;
                 pc->frame_start_found = 4;
@@ -2257,492 +220,3 @@
     return END_NOT_FOUND;
 }
 
-static int decode_chunks(AVCodecContext *avctx,
-                         AVFrame *picture, int *got_output,
-                         const uint8_t *buf, int buf_size)
-{
-    Mpeg1Context *s = avctx->priv_data;
-    MpegEncContext *s2 = &s->mpeg_enc_ctx;
-    const uint8_t *buf_ptr = buf;
-    const uint8_t *buf_end = buf + buf_size;
-    int ret, input_size;
-    int last_code = 0;
-    int picture_start_code_seen = 0;
-
-    for (;;) {
-        /* find next start code */
-        uint32_t start_code = -1;
-        buf_ptr = avpriv_mpv_find_start_code(buf_ptr, buf_end, &start_code);
-        if (start_code > 0x1ff) {
-            if (s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT) {
-                if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
-                    int i;
-                    av_assert0(avctx->thread_count > 1);
-
-                    avctx->execute(avctx, slice_decode_thread,  &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
-                    for (i = 0; i < s->slice_count; i++)
-                        s2->er.error_count += s2->thread_context[i]->er.error_count;
-                }
-
-                if (CONFIG_VDPAU && uses_vdpau(avctx))
-                    ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
-
-
-                if (slice_end(avctx, picture)) {
-                    if (s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
-                        *got_output = 1;
-                }
-            }
-            s2->pict_type = 0;
-            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 %td left %d\n", start_code, buf_ptr-buf, input_size);
-        }
-
-        /* prepare data for next start code */
-        switch (start_code) {
-        case SEQ_START_CODE:
-            if (last_code == 0) {
-                mpeg1_decode_sequence(avctx, buf_ptr, input_size);
-                if(buf != avctx->extradata)
-                    s->sync=1;
-            } else {
-                av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
-                if (avctx->err_recognition & AV_EF_EXPLODE)
-                    return AVERROR_INVALIDDATA;
-            }
-            break;
-
-        case PICTURE_START_CODE:
-            if (picture_start_code_seen && s2->picture_structure == PICT_FRAME) {
-               /* If it's a frame picture, there can't be more than one picture header.
-                  Yet, it does happen and we need to handle it. */
-               av_log(avctx, AV_LOG_WARNING, "ignoring extra picture following a frame-picture\n");
-               break;
-            }
-            picture_start_code_seen = 1;
-
-            if (s2->width <= 0 || s2->height <= 0) {
-                av_log(avctx, AV_LOG_ERROR, "%dx%d is invalid\n", s2->width, s2->height);
-                return AVERROR_INVALIDDATA;
-            }
-
-            if(s->tmpgexs){
-                s2->intra_dc_precision= 3;
-                s2->intra_matrix[0]= 1;
-            }
-            if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && s->slice_count) {
-                int i;
-
-                avctx->execute(avctx, slice_decode_thread,
-                               s2->thread_context, NULL,
-                               s->slice_count, sizeof(void*));
-                for (i = 0; i < s->slice_count; i++)
-                    s2->er.error_count += s2->thread_context[i]->er.error_count;
-                s->slice_count = 0;
-            }
-            if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
-                ret = mpeg_decode_postinit(avctx);
-                if (ret < 0) {
-                    av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
-                    return ret;
-                }
-
-                /* we have a complete image: we try to decompress it */
-                if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
-                    s2->pict_type = 0;
-                s2->first_slice = 1;
-                last_code = PICTURE_START_CODE;
-            } else {
-                av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
-                if (avctx->err_recognition & AV_EF_EXPLODE)
-                    return AVERROR_INVALIDDATA;
-            }
-            break;
-        case EXT_START_CODE:
-            init_get_bits(&s2->gb, buf_ptr, input_size*8);
-
-            switch (get_bits(&s2->gb, 4)) {
-            case 0x1:
-                if (last_code == 0) {
-                mpeg_decode_sequence_extension(s);
-                } else {
-                    av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
-                    if (avctx->err_recognition & AV_EF_EXPLODE)
-                        return AVERROR_INVALIDDATA;
-                }
-                break;
-            case 0x2:
-                mpeg_decode_sequence_display_extension(s);
-                break;
-            case 0x3:
-                mpeg_decode_quant_matrix_extension(s2);
-                break;
-            case 0x7:
-                mpeg_decode_picture_display_extension(s);
-                break;
-            case 0x8:
-                if (last_code == PICTURE_START_CODE) {
-                    mpeg_decode_picture_coding_extension(s);
-                } else {
-                    av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
-                    if (avctx->err_recognition & AV_EF_EXPLODE)
-                        return AVERROR_INVALIDDATA;
-                }
-                break;
-            }
-            break;
-        case USER_START_CODE:
-            mpeg_decode_user_data(avctx, buf_ptr, input_size);
-            break;
-        case GOP_START_CODE:
-            if (last_code == 0) {
-                s2->first_field=0;
-                mpeg_decode_gop(avctx, buf_ptr, input_size);
-                s->sync=1;
-            } else {
-                av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
-                if (avctx->err_recognition & AV_EF_EXPLODE)
-                    return AVERROR_INVALIDDATA;
-            }
-            break;
-        default:
-            if (start_code >= SLICE_MIN_START_CODE &&
-                start_code <= SLICE_MAX_START_CODE && last_code == PICTURE_START_CODE) {
-
-                if (s2->progressive_sequence && !s2->progressive_frame) {
-                    s2->progressive_frame = 1;
-                    av_log(s2->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
-                }
-
-                if (s2->picture_structure == 0 || (s2->progressive_frame && s2->picture_structure != PICT_FRAME)) {
-                    av_log(s2->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s2->picture_structure);
-                    s2->picture_structure = PICT_FRAME;
-                }
-
-                if (s2->progressive_sequence && !s2->frame_pred_frame_dct) {
-                    av_log(s2->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
-                }
-
-                if (s2->picture_structure == PICT_FRAME) {
-                    s2->first_field = 0;
-                    s2->v_edge_pos  = 16 * s2->mb_height;
-                } else {
-                    s2->first_field ^= 1;
-                    s2->v_edge_pos   = 8 * s2->mb_height;
-                    memset(s2->mbskip_table, 0, s2->mb_stride * s2->mb_height);
-                }
-            }
-            if (start_code >= SLICE_MIN_START_CODE &&
-                start_code <= SLICE_MAX_START_CODE && last_code != 0) {
-                const int field_pic = s2->picture_structure != PICT_FRAME;
-                int mb_y = start_code - SLICE_MIN_START_CODE;
-                last_code = SLICE_MIN_START_CODE;
-                if(s2->codec_id != AV_CODEC_ID_MPEG1VIDEO && s2->mb_height > 2800/16)
-                    mb_y += (*buf_ptr&0xE0)<<2;
-
-                mb_y <<= field_pic;
-                if (s2->picture_structure == PICT_BOTTOM_FIELD)
-                    mb_y++;
-
-                if (mb_y >= s2->mb_height) {
-                    av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
-                    return -1;
-                }
-
-                if (s2->last_picture_ptr == NULL) {
-                /* Skip B-frames if we do not have reference frames and gop is not closed */
-                    if (s2->pict_type == AV_PICTURE_TYPE_B) {
-                        if (!s2->closed_gop)
-                            break;
-                    }
-                }
-                if (s2->pict_type == AV_PICTURE_TYPE_I || (s2->flags2 & CODEC_FLAG2_SHOW_ALL))
-                    s->sync=1;
-                if (s2->next_picture_ptr == NULL) {
-                /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
-                    if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) break;
-                }
-                if ((avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type == AV_PICTURE_TYPE_B) ||
-                    (avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type != AV_PICTURE_TYPE_I) ||
-                     avctx->skip_frame >= AVDISCARD_ALL)
-                    break;
-
-                if (!s->mpeg_enc_ctx_allocated)
-                    break;
-
-                if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
-                    if (mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
-                        break;
-                }
-
-                if (!s2->pict_type) {
-                    av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
-                    if (avctx->err_recognition & AV_EF_EXPLODE)
-                        return AVERROR_INVALIDDATA;
-                    break;
-                }
-
-                if (s2->first_slice) {
-                    s2->first_slice = 0;
-                    if (mpeg_field_start(s2, buf, buf_size) < 0)
-                        return -1;
-                }
-                if (!s2->current_picture_ptr) {
-                    av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
-                    return AVERROR_INVALIDDATA;
-                }
-
-                if (uses_vdpau(avctx)) {
-                    s->slice_count++;
-                    break;
-                }
-
-                if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
-                    int threshold = (s2->mb_height * s->slice_count +
-                                     s2->slice_context_count / 2) /
-                                    s2->slice_context_count;
-                    av_assert0(avctx->thread_count > 1);
-                    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;
-                            ret = ff_update_duplicate_context(thread_context,
-                                                              s2);
-                            if (ret < 0)
-                                return ret;
-                        }
-                        init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
-                        s->slice_count++;
-                    }
-                    buf_ptr += 2; // FIXME add minimum number of bytes per slice
-                } else {
-                    ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
-                    emms_c();
-
-                    if (ret < 0) {
-                        if (avctx->err_recognition & AV_EF_EXPLODE)
-                            return ret;
-                        if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
-                            ff_er_add_slice(&s2->er, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
-                    } else {
-                        ff_er_add_slice(&s2->er, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x-1, s2->mb_y, ER_AC_END | ER_DC_END | ER_MV_END);
-                    }
-                }
-            }
-            break;
-        }
-    }
-}
-
-static int mpeg_decode_frame(AVCodecContext *avctx,
-                             void *data, int *got_output,
-                             AVPacket *avpkt)
-{
-    const uint8_t *buf = avpkt->data;
-    int ret;
-    int buf_size = avpkt->size;
-    Mpeg1Context *s = avctx->priv_data;
-    AVFrame *picture = data;
-    MpegEncContext *s2 = &s->mpeg_enc_ctx;
-    av_dlog(avctx, "fill_buffer\n");
-
-    if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
-        /* special case for last picture */
-        if (s2->low_delay == 0 && s2->next_picture_ptr) {
-            *picture = s2->next_picture_ptr->f;
-            s2->next_picture_ptr = NULL;
-
-            *got_output = 1;
-        }
-        return buf_size;
-    }
-
-    if (s2->flags & CODEC_FLAG_TRUNCATED) {
-        int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
-
-        if (ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0)
-            return buf_size;
-    }
-
-    s2->codec_tag = avpriv_toupper4(avctx->codec_tag);
-    if (s->mpeg_enc_ctx_allocated == 0 && (   s2->codec_tag == AV_RL32("VCR2")
-                                           || s2->codec_tag == AV_RL32("BW10")
-                                          ))
-        vcr2_init_sequence(avctx);
-
-    s->slice_count = 0;
-
-    if (avctx->extradata && !s->extradata_decoded) {
-        ret = decode_chunks(avctx, picture, got_output, avctx->extradata, avctx->extradata_size);
-        if(*got_output) {
-            av_log(avctx, AV_LOG_ERROR, "picture in extradata\n");
-            *got_output = 0;
-        }
-        s->extradata_decoded = 1;
-        if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) {
-            s2->current_picture_ptr = NULL;
-            return ret;
-        }
-    }
-
-    ret = decode_chunks(avctx, picture, got_output, buf, buf_size);
-    if (ret<0 || *got_output)
-        s2->current_picture_ptr = NULL;
-
-    return ret;
-}
-
-
-static void flush(AVCodecContext *avctx)
-{
-    Mpeg1Context *s = avctx->priv_data;
-
-    s->sync=0;
-
-    ff_mpeg_flush(avctx);
-}
-
-static int mpeg_decode_end(AVCodecContext *avctx)
-{
-    Mpeg1Context *s = avctx->priv_data;
-
-    if (s->mpeg_enc_ctx_allocated)
-        ff_MPV_common_end(&s->mpeg_enc_ctx);
-    return 0;
-}
-
-static const AVProfile mpeg2_video_profiles[] = {
-    { FF_PROFILE_MPEG2_422,          "4:2:2"              },
-    { FF_PROFILE_MPEG2_HIGH,         "High"               },
-    { FF_PROFILE_MPEG2_SS,           "Spatially Scalable" },
-    { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable"       },
-    { FF_PROFILE_MPEG2_MAIN,         "Main"               },
-    { FF_PROFILE_MPEG2_SIMPLE,       "Simple"             },
-    { FF_PROFILE_RESERVED,           "Reserved"           },
-    { FF_PROFILE_RESERVED,           "Reserved"           },
-    { FF_PROFILE_UNKNOWN },
-};
-
-
-AVCodec ff_mpeg1video_decoder = {
-    .name                  = "mpeg1video",
-    .type                  = AVMEDIA_TYPE_VIDEO,
-    .id                    = AV_CODEC_ID_MPEG1VIDEO,
-    .priv_data_size        = sizeof(Mpeg1Context),
-    .init                  = mpeg_decode_init,
-    .close                 = mpeg_decode_end,
-    .decode                = mpeg_decode_frame,
-    .capabilities          = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
-                             CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
-                             CODEC_CAP_SLICE_THREADS,
-    .flush                 = flush,
-    .max_lowres            = 3,
-    .long_name             = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
-    .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
-};
-
-AVCodec ff_mpeg2video_decoder = {
-    .name           = "mpeg2video",
-    .type           = AVMEDIA_TYPE_VIDEO,
-    .id             = AV_CODEC_ID_MPEG2VIDEO,
-    .priv_data_size = sizeof(Mpeg1Context),
-    .init           = mpeg_decode_init,
-    .close          = mpeg_decode_end,
-    .decode         = mpeg_decode_frame,
-    .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
-                      CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
-                      CODEC_CAP_SLICE_THREADS,
-    .flush          = flush,
-    .max_lowres     = 3,
-    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
-    .profiles       = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
-};
-
-//legacy decoder
-AVCodec ff_mpegvideo_decoder = {
-    .name           = "mpegvideo",
-    .type           = AVMEDIA_TYPE_VIDEO,
-    .id             = AV_CODEC_ID_MPEG2VIDEO,
-    .priv_data_size = sizeof(Mpeg1Context),
-    .init           = mpeg_decode_init,
-    .close          = mpeg_decode_end,
-    .decode         = mpeg_decode_frame,
-    .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
-    .flush          = flush,
-    .max_lowres     = 3,
-    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
-};
-
-#if CONFIG_MPEG_XVMC_DECODER
-static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
-{
-    if (avctx->active_thread_type & FF_THREAD_SLICE)
-        return -1;
-    if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
-        return -1;
-    if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
-        av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
-    }
-    mpeg_decode_init(avctx);
-
-    avctx->pix_fmt           = AV_PIX_FMT_XVMC_MPEG2_IDCT;
-    avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
-
-    return 0;
-}
-
-AVCodec ff_mpeg_xvmc_decoder = {
-    .name           = "mpegvideo_xvmc",
-    .type           = AVMEDIA_TYPE_VIDEO,
-    .id             = AV_CODEC_ID_MPEG2VIDEO_XVMC,
-    .priv_data_size = sizeof(Mpeg1Context),
-    .init           = mpeg_mc_decode_init,
-    .close          = mpeg_decode_end,
-    .decode         = mpeg_decode_frame,
-    .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
-                      CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
-    .flush          = flush,
-    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
-};
-
-#endif
-
-#if CONFIG_MPEG_VDPAU_DECODER
-AVCodec ff_mpeg_vdpau_decoder = {
-    .name           = "mpegvideo_vdpau",
-    .type           = AVMEDIA_TYPE_VIDEO,
-    .id             = AV_CODEC_ID_MPEG2VIDEO,
-    .priv_data_size = sizeof(Mpeg1Context),
-    .init           = mpeg_decode_init,
-    .close          = mpeg_decode_end,
-    .decode         = mpeg_decode_frame,
-    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
-                      CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
-    .flush          = flush,
-    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
-};
-#endif
-
-#if CONFIG_MPEG1_VDPAU_DECODER
-AVCodec ff_mpeg1_vdpau_decoder = {
-    .name           = "mpeg1video_vdpau",
-    .type           = AVMEDIA_TYPE_VIDEO,
-    .id             = AV_CODEC_ID_MPEG1VIDEO,
-    .priv_data_size = sizeof(Mpeg1Context),
-    .init           = mpeg_decode_init,
-    .close          = mpeg_decode_end,
-    .decode         = mpeg_decode_frame,
-    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
-                      CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
-    .flush          = flush,
-    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
-};
-#endif
diff --git a/libavcodec/mpeg12.h b/libavcodec/mpeg12.h
index 1f36917..b4ebd23 100644
--- a/libavcodec/mpeg12.h
+++ b/libavcodec/mpeg12.h
@@ -25,25 +25,21 @@
 #include "mpegvideo.h"
 
 #define DC_VLC_BITS 9
+#define MV_VLC_BITS 9
 #define TEX_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
+
 extern VLC ff_dc_lum_vlc;
 extern VLC ff_dc_chroma_vlc;
-
-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;
-    int save_width, save_height, save_progressive_seq;
-    AVRational frame_rate_ext;       ///< MPEG-2 specific framerate modificator
-    int sync;                        ///< Did we reach a sync point like a GOP/SEQ/KEYFrame?
-    int tmpgexs;
-    int extradata_decoded;
-} Mpeg1Context;
+extern VLC ff_mbincr_vlc;
+extern VLC ff_mb_ptype_vlc;
+extern VLC ff_mb_btype_vlc;
+extern VLC ff_mb_pat_vlc;
+extern VLC ff_mv_vlc;
 
 extern uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
 
@@ -71,6 +67,8 @@
     return diff;
 }
 
-extern int ff_mpeg1_decode_block_intra(MpegEncContext *s, int16_t *block, int n);
+int ff_mpeg1_decode_block_intra(MpegEncContext *s, int16_t *block, int n);
+void ff_mpeg1_clean_buffers(MpegEncContext *s);
+int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s);
 
 #endif /* AVCODEC_MPEG12_H */
diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
new file mode 100644
index 0000000..e969893
--- /dev/null
+++ b/libavcodec/mpeg12dec.c
@@ -0,0 +1,2608 @@
+/*
+ * MPEG-1/2 decoder
+ * Copyright (c) 2000, 2001 Fabrice Bellard
+ * Copyright (c) 2002-2013 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
+ * MPEG-1/2 decoder
+ */
+
+//#define DEBUG
+#include "libavutil/internal.h"
+#include "internal.h"
+#include "avcodec.h"
+#include "dsputil.h"
+#include "mpegvideo.h"
+#include "error_resilience.h"
+#include "mpeg12.h"
+#include "mpeg12data.h"
+#include "mpeg12decdata.h"
+#include "bytestream.h"
+#include "vdpau_internal.h"
+#include "xvmc_internal.h"
+#include "thread.h"
+
+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;
+    int save_width, save_height, save_progressive_seq;
+    AVRational frame_rate_ext;       ///< MPEG-2 specific framerate modificator
+    int sync;                        ///< Did we reach a sync point like a GOP/SEQ/KEYFrame?
+    int tmpgexs;
+    int extradata_decoded;
+} Mpeg1Context;
+
+/* as H.263, but only 17 codes */
+static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
+{
+    int code, sign, val, shift;
+
+    code = get_vlc2(&s->gb, ff_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 */
+    return sign_extend(val, 5 + shift);
+}
+
+static inline int mpeg1_decode_block_intra(MpegEncContext *s, int16_t *block, int n)
+{
+    int level, dc, diff, i, j, run;
+    int component;
+    RLTable *rl = &ff_rl_mpeg1;
+    uint8_t * const scantable    = s->intra_scantable.permutated;
+    const uint16_t *quant_matrix = s->intra_matrix;
+    const int qscale             = s->qscale;
+
+    /* DC coefficient */
+    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 * quant_matrix[0];
+    av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
+    i = 0;
+    {
+        OPEN_READER(re, &s->gb);
+        /* now quantify & encode AC coefficients */
+        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;
+}
+
+int ff_mpeg1_decode_block_intra(MpegEncContext *s, int16_t *block, int n)
+{
+    return mpeg1_decode_block_intra(s, block, n);
+}
+
+static inline int mpeg1_decode_block_inter(MpegEncContext *s, int16_t *block, int n)
+{
+    int level, i, j, run;
+    RLTable *rl = &ff_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 first coefficient, no need to add 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 coefficients */
+        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;
+}
+
+/**
+ * Note: this function can read out of range and crash for corrupt streams.
+ * Changing this would eat up any speed benefits it has.
+ * Do not use "fast" flag if you need the code to be robust.
+ */
+static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, int16_t *block, int n)
+{
+    int level, i, j, run;
+    RLTable *rl = &ff_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 first coefficient, no need to add 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 coefficients */
+        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, int16_t *block, int n)
+{
+    int level, i, j, run;
+    RLTable *rl = &ff_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 first coefficient, no need to add 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 coefficients */
+        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;
+}
+
+/**
+ * Note: this function can read out of range and crash for corrupt streams.
+ * Changing this would eat up any speed benefits it has.
+ * Do not use "fast" flag if you need the code to be robust.
+ */
+static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
+                                                    int16_t *block, int n)
+{
+    int level, i, j, run;
+    RLTable *rl = &ff_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 first coefficient, no need to add 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 coefficients */
+    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, int16_t *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 coefficient */
+    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);
+    av_dlog(s->avctx, "dc=%d\n", block[0]);
+    mismatch = block[0] ^ 1;
+    i = 0;
+    if (s->intra_vlc_format)
+        rl = &ff_rl_mpeg2;
+    else
+        rl = &ff_rl_mpeg1;
+
+    {
+        OPEN_READER(re, &s->gb);
+        /* now quantify & encode AC coefficients */
+        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;
+}
+
+/**
+ * Note: this function can read out of range and crash for corrupt streams.
+ * Changing this would eat up any speed benefits it has.
+ * Do not use "fast" flag if you need the code to be robust.
+ */
+static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, int16_t *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 coefficient */
+    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 = &ff_rl_mpeg2;
+    else
+        rl = &ff_rl_mpeg1;
+
+    {
+        OPEN_READER(re, &s->gb);
+        /* now quantify & encode AC coefficients */
+        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;
+}
+
+/******************************************/
+/* decoding */
+
+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;
+    }
+}
+
+static void exchange_uv(MpegEncContext *s)
+{
+    int16_t (*tmp)[64];
+
+    tmp           = s->pblocks[4];
+    s->pblocks[4] = s->pblocks[5];
+    s->pblocks[5] = tmp;
+}
+
+/* motion type (for MPEG-2) */
+#define MT_FIELD 1
+#define MT_FRAME 2
+#define MT_16X8  2
+#define MT_DMV   3
+
+static int mpeg_decode_mb(MpegEncContext *s, int16_t block[12][64])
+{
+    int i, j, k, cbp, val, mb_type, motion_type;
+    const int mb_block_count = 4 + (1 << s->chroma_format);
+
+    av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
+
+    av_assert2(s->mb_skipped == 0);
+
+    if (s->mb_skip_run-- != 0) {
+        if (s->pict_type == AV_PICTURE_TYPE_P) {
+            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)) {
+                av_log(s->avctx, AV_LOG_ERROR, "skip with previntra\n");
+                return -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 AV_PICTURE_TYPE_I:
+        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 AV_PICTURE_TYPE_P:
+        mb_type = get_vlc2(&s->gb, ff_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 AV_PICTURE_TYPE_B:
+        mb_type = get_vlc2(&s->gb, ff_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;
+    }
+    av_dlog(s->avctx, "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 an 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;
+        // if 1, we memcpy blocks in xvmcvideo
+        if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
+            ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
+            if (s->swap_uv) {
+                exchange_uv(s);
+            }
+        }
+
+        if (s->codec_id == AV_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) {
+            av_assert2(mb_type & MB_TYPE_CBP);
+
+            s->mv_dir = MV_DIR_FORWARD;
+            if (s->picture_structure == PICT_FRAME) {
+                if (s->picture_structure == PICT_FRAME
+                    && !s->frame_pred_frame_dct)
+                    s->interlaced_dct = get_bits1(&s->gb);
+                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;
+            }
+
+            if (IS_QUANT(mb_type))
+                s->qscale = get_qscale(s);
+
+            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 {
+            av_assert2(mb_type & MB_TYPE_L0L1);
+            // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
+            /* get additional motion vector type */
+            if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct)
+                motion_type = MT_FRAME;
+            else {
+                motion_type = get_bits(&s->gb, 2);
+                if (s->picture_structure == PICT_FRAME && 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 = (mb_type >> 13) & 3;
+            av_dlog(s->avctx, "motion_type=%d\n", motion_type);
+            switch (motion_type) {
+            case MT_FRAME: /* or MT_16X8 */
+                if (s->picture_structure == PICT_FRAME) {
+                    mb_type |= MB_TYPE_16x16;
+                    s->mv_type = MV_TYPE_16X16;
+                    for (i = 0; i < 2; i++) {
+                        if (USES_LIST(mb_type, i)) {
+                            /* MT_FRAME */
+                            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 MPEG-1 */
+                            if (s->full_pel[i]) {
+                                s->mv[i][0][0] <<= 1;
+                                s->mv[i][0][1] <<= 1;
+                            }
+                        }
+                    }
+                } else {
+                    mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
+                    s->mv_type = MV_TYPE_16X8;
+                    for (i = 0; i < 2; i++) {
+                        if (USES_LIST(mb_type, i)) {
+                            /* MT_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 (i = 0; i < 2; i++) {
+                        if (USES_LIST(mb_type, i)) {
+                            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;
+                                av_dlog(s->avctx, "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;
+                                av_dlog(s->avctx, "fmy=%d\n", val);
+                            }
+                        }
+                    }
+                } else {
+                    av_assert0(!s->progressive_sequence);
+                    mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
+                    for (i = 0; i < 2; i++) {
+                        if (USES_LIST(mb_type, i)) {
+                            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:
+                if(s->progressive_sequence){
+                    av_log(s->avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n");
+                    return -1;
+                }
+                s->mv_type = MV_TYPE_DMV;
+                for (i = 0; i < 2; i++) {
+                    if (USES_LIST(mb_type, i)) {
+                        int dmx, dmy, mx, my, m;
+                        const int my_shift = s->picture_structure == PICT_FRAME;
+
+                        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] >> my_shift);
+                        dmy = get_dmv(s);
+
+
+                        s->last_mv[i][0][1] = my << my_shift;
+                        s->last_mv[i][1][1] = my << my_shift;
+
+                        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]);
+
+            cbp = get_vlc2(&s->gb, ff_mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
+            if (mb_block_count > 6) {
+                 cbp <<= mb_block_count - 6;
+                 cbp  |= get_bits(&s->gb, mb_block_count - 6);
+                 s->dsp.clear_blocks(s->block[6]);
+            }
+            if (cbp <= 0) {
+                av_log(s->avctx, AV_LOG_ERROR, "invalid cbp %d at %d %d\n", cbp, s->mb_x, s->mb_y);
+                return -1;
+            }
+
+            //if 1, we memcpy blocks in xvmcvideo
+            if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
+                ff_xvmc_pack_pblocks(s, cbp);
+                if (s->swap_uv) {
+                    exchange_uv(s);
+                }
+            }
+
+            if (s->codec_id == AV_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;
+}
+
+static av_cold int mpeg_decode_init(AVCodecContext *avctx)
+{
+    Mpeg1Context *s = avctx->priv_data;
+    MpegEncContext *s2 = &s->mpeg_enc_ctx;
+    int i;
+
+    /* we need some permutation to store matrices,
+     * until MPV_common_init() sets the real permutation. */
+    for (i = 0; i < 64; i++)
+       s2->dsp.idct_permutation[i]=i;
+
+    ff_MPV_decode_defaults(s2);
+
+    s->mpeg_enc_ctx.avctx  = avctx;
+    s->mpeg_enc_ctx.flags  = avctx->flags;
+    s->mpeg_enc_ctx.flags2 = avctx->flags2;
+    ff_mpeg12_common_init(&s->mpeg_enc_ctx);
+    ff_mpeg12_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;
+    avctx->color_range = AVCOL_RANGE_MPEG;
+    if (avctx->codec->id == AV_CODEC_ID_MPEG1VIDEO)
+        avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
+    else
+        avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
+    return 0;
+}
+
+static int mpeg_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from)
+{
+    Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
+    MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
+    int err;
+
+    if (avctx == avctx_from || !ctx_from->mpeg_enc_ctx_allocated || !s1->context_initialized)
+        return 0;
+
+    err = ff_mpeg_update_thread_context(avctx, avctx_from);
+    if (err) return err;
+
+    if (!ctx->mpeg_enc_ctx_allocated)
+        memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
+
+    if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
+        s->picture_number++;
+
+    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]];
+    }
+}
+
+static const enum AVPixelFormat mpeg1_hwaccel_pixfmt_list_420[] = {
+#if CONFIG_MPEG_XVMC_DECODER
+    AV_PIX_FMT_XVMC_MPEG2_IDCT,
+    AV_PIX_FMT_XVMC_MPEG2_MC,
+#endif
+#if CONFIG_MPEG1_VDPAU_HWACCEL
+    AV_PIX_FMT_VDPAU_MPEG1,
+#endif
+    AV_PIX_FMT_YUV420P,
+    AV_PIX_FMT_NONE
+};
+
+static const enum AVPixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
+#if CONFIG_MPEG_XVMC_DECODER
+    AV_PIX_FMT_XVMC_MPEG2_IDCT,
+    AV_PIX_FMT_XVMC_MPEG2_MC,
+#endif
+#if CONFIG_MPEG2_VDPAU_HWACCEL
+    AV_PIX_FMT_VDPAU_MPEG2,
+#endif
+#if CONFIG_MPEG2_DXVA2_HWACCEL
+    AV_PIX_FMT_DXVA2_VLD,
+#endif
+#if CONFIG_MPEG2_VAAPI_HWACCEL
+    AV_PIX_FMT_VAAPI_VLD,
+#endif
+    AV_PIX_FMT_YUV420P,
+    AV_PIX_FMT_NONE
+};
+
+static inline int uses_vdpau(AVCodecContext *avctx) {
+    return avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG1 || avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG2;
+}
+
+static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
+{
+    Mpeg1Context *s1 = avctx->priv_data;
+    MpegEncContext *s = &s1->mpeg_enc_ctx;
+
+    if(s->chroma_format < 2) {
+        return ff_thread_get_format(avctx,
+                                avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO ?
+                                mpeg1_hwaccel_pixfmt_list_420 :
+                                mpeg2_hwaccel_pixfmt_list_420);
+    } else if(s->chroma_format == 2)
+        return AV_PIX_FMT_YUV422P;
+    else
+        return AV_PIX_FMT_YUV444P;
+}
+
+static void setup_hwaccel_for_pixfmt(AVCodecContext *avctx)
+{
+    if (avctx->pix_fmt != AV_PIX_FMT_XVMC_MPEG2_IDCT && avctx->pix_fmt != AV_PIX_FMT_XVMC_MPEG2_MC) {
+        avctx->xvmc_acceleration = 0;
+    } else if (!avctx->xvmc_acceleration) {
+        avctx->xvmc_acceleration = 2;
+    }
+    avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
+    // until then pix_fmt may be changed right after codec init
+    if (avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT ||
+        avctx->hwaccel || uses_vdpau(avctx))
+        if (avctx->idct_algo == FF_IDCT_AUTO)
+            avctx->idct_algo = FF_IDCT_SIMPLE;
+}
+
+/* Call this function when we know all parameters.
+ * It may be called in different places for MPEG-1 and MPEG-2. */
+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_width           != s->width                ||
+        s1->save_height          != s->height               ||
+        s1->save_aspect_info     != s->aspect_ratio_info    ||
+        s1->save_progressive_seq != s->progressive_sequence ||
+        0)
+    {
+
+        if (s1->mpeg_enc_ctx_allocated) {
+            ParseContext pc = s->parse_context;
+            s->parse_context.buffer = 0;
+            ff_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);
+        if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->bit_rate) {
+            avctx->rc_max_rate = s->bit_rate;
+        } else if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && s->bit_rate &&
+                   (s->bit_rate != 0x3FFFF*400 || s->vbv_delay != 0xFFFF)) {
+            avctx->bit_rate = s->bit_rate;
+        }
+        s1->save_aspect_info     = s->aspect_ratio_info;
+        s1->save_width           = s->width;
+        s1->save_height          = s->height;
+        s1->save_progressive_seq = s->progressive_sequence;
+
+        /* 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->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
+            //MPEG-1 fps
+            avctx->time_base.den = ff_mpeg12_frame_rate_tab[s->frame_rate_index].num;
+            avctx->time_base.num = ff_mpeg12_frame_rate_tab[s->frame_rate_index].den;
+            //MPEG-1 aspect
+            avctx->sample_aspect_ratio = av_d2q(1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
+            avctx->ticks_per_frame=1;
+        } else {//MPEG-2
+        //MPEG-2 fps
+            av_reduce(&s->avctx->time_base.den,
+                      &s->avctx->time_base.num,
+                      ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
+                      ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
+                      1 << 30);
+            avctx->ticks_per_frame = 2;
+            //MPEG-2 aspect
+            if (s->aspect_ratio_info > 1) {
+                AVRational dar =
+                    av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
+                                      (AVRational) {s1->pan_scan.width, s1->pan_scan.height}),
+                             (AVRational) {s->width, s->height});
+
+                // we ignore the spec here and guess a bit as reality does not match the spec, see for example
+                // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
+                // issue1613, 621, 562
+                if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
+                   (av_cmp_q(dar, (AVRational) {4, 3}) && av_cmp_q(dar, (AVRational) {16, 9}))) {
+                    s->avctx->sample_aspect_ratio =
+                        av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
+                                 (AVRational) {s->width, s->height});
+                } else {
+                    s->avctx->sample_aspect_ratio =
+                        av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
+                                 (AVRational) {s1->pan_scan.width, s1->pan_scan.height});
+//issue1613 4/3 16/9 -> 16/9
+//res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
+//widescreen-issue562.mpg 4/3 16/9 -> 16/9
+//                    s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
+                    av_dlog(avctx, "A %d/%d\n",
+                            ff_mpeg2_aspect[s->aspect_ratio_info].num, ff_mpeg2_aspect[s->aspect_ratio_info].den);
+                    av_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
+                            s->avctx->sample_aspect_ratio.den);
+                }
+            } else {
+                s->avctx->sample_aspect_ratio =
+                    ff_mpeg2_aspect[s->aspect_ratio_info];
+            }
+        } // MPEG-2
+
+        avctx->pix_fmt = mpeg_get_pixelformat(avctx);
+        setup_hwaccel_for_pixfmt(avctx);
+
+        /* Quantization matrices may need reordering
+         * if DCT permutation is changed. */
+        memcpy(old_permutation, s->dsp.idct_permutation, 64 * sizeof(uint8_t));
+
+        if (ff_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;
+
+    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);
+    s->vbv_delay = vbv_delay;
+    if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
+        s->full_pel[0] = get_bits1(&s->gb);
+        f_code = get_bits(&s->gb, 3);
+        if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
+            return -1;
+        f_code += !f_code;
+        s->mpeg_f_code[0][0] = f_code;
+        s->mpeg_f_code[0][1] = f_code;
+    }
+    if (s->pict_type == AV_PICTURE_TYPE_B) {
+        s->full_pel[1] = get_bits1(&s->gb);
+        f_code = get_bits(&s->gb, 3);
+        if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
+            return -1;
+        f_code += !f_code;
+        s->mpeg_f_code[1][0] = f_code;
+        s->mpeg_f_code[1][1] = f_code;
+    }
+    s->current_picture.f.pict_type = s->pict_type;
+    s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
+
+    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;
+    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); /* profile 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;
+
+    av_dlog(s->avctx, "sequence extension\n");
+    s->codec_id      = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
+
+    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) {
+        s->avctx->color_primaries = get_bits(&s->gb, 8);
+        s->avctx->color_trc       = get_bits(&s->gb, 8);
+        s->avctx->colorspace      = get_bits(&s->gb, 8);
+    }
+    w = get_bits(&s->gb, 14);
+    skip_bits(&s->gb, 1); //marker
+    h = get_bits(&s->gb, 14);
+    // remaining 3 bits are zero padding
+
+    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 int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra)
+{
+    int i;
+
+    for (i = 0; i < 64; i++) {
+        int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
+        int v = get_bits(&s->gb, 8);
+        if (v == 0) {
+            av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
+            return -1;
+        }
+        if (intra && i == 0 && v != 8) {
+            av_log(s->avctx, AV_LOG_DEBUG, "intra matrix specifies invalid DC quantizer %d, ignoring\n", v);
+            v = 8; // needed by pink.mpg / issue1046
+        }
+        matrix0[j] = v;
+        if (matrix1)
+            matrix1[j] = v;
+    }
+    return 0;
+}
+
+static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
+{
+    av_dlog(s->avctx, "matrix extension\n");
+
+    if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
+    if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
+    if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL           , 1);
+    if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL           , 0);
+}
+
+static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
+{
+    MpegEncContext *s = &s1->mpeg_enc_ctx;
+
+    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);
+    if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
+        av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
+        if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
+            if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
+                s->pict_type = AV_PICTURE_TYPE_I;
+            else
+                s->pict_type = AV_PICTURE_TYPE_P;
+        } else
+            s->pict_type = AV_PICTURE_TYPE_B;
+        s->current_picture.f.pict_type = s->pict_type;
+        s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
+    }
+    s->mpeg_f_code[0][0] += !s->mpeg_f_code[0][0];
+    s->mpeg_f_code[0][1] += !s->mpeg_f_code[0][1];
+    s->mpeg_f_code[1][0] += !s->mpeg_f_code[1][0];
+    s->mpeg_f_code[1][1] += !s->mpeg_f_code[1][1];
+
+    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->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 */
+    av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
+    av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
+    av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
+    av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
+    av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
+    av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
+    av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
+    av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
+    av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
+}
+
+static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
+{
+    AVCodecContext *avctx = s->avctx;
+    Mpeg1Context *s1 = (Mpeg1Context*)s;
+
+    /* start frame decoding */
+    if (s->first_field || s->picture_structure == PICT_FRAME) {
+        AVFrameSideData *pan_scan;
+
+        if (ff_MPV_frame_start(s, avctx) < 0)
+            return -1;
+
+        ff_mpeg_er_frame_start(s);
+
+        /* first check if we must repeat the frame */
+        s->current_picture_ptr->f.repeat_pict = 0;
+        if (s->repeat_first_field) {
+            if (s->progressive_sequence) {
+                if (s->top_field_first)
+                    s->current_picture_ptr->f.repeat_pict = 4;
+                else
+                    s->current_picture_ptr->f.repeat_pict = 2;
+            } else if (s->progressive_frame) {
+                s->current_picture_ptr->f.repeat_pict = 1;
+            }
+        }
+
+        pan_scan = av_frame_new_side_data(&s->current_picture_ptr->f,
+                                          AV_FRAME_DATA_PANSCAN,
+                                          sizeof(s1->pan_scan));
+        if (!pan_scan)
+            return AVERROR(ENOMEM);
+        memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
+
+        if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
+            ff_thread_finish_setup(avctx);
+    } else { // second field
+        int i;
+
+        if (!s->current_picture_ptr) {
+            av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
+            return -1;
+        }
+
+        if (s->avctx->hwaccel &&
+            (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
+            if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
+                av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode first field\n");
+        }
+
+        for (i = 0; i < 4; i++) {
+            s->current_picture.f.data[i] = s->current_picture_ptr->f.data[i];
+            if (s->picture_structure == PICT_BOTTOM_FIELD) {
+                s->current_picture.f.data[i] += s->current_picture_ptr->f.linesize[i];
+            }
+        }
+    }
+
+    if (avctx->hwaccel) {
+        if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
+            return -1;
+    }
+
+// MPV_frame_start will call this function too,
+// but we need to call it on every field
+    if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
+        if (ff_xvmc_field_start(s, avctx) < 0)
+            return -1;
+
+    return 0;
+}
+
+#define DECODE_SLICE_ERROR -1
+#define DECODE_SLICE_OK     0
+
+/**
+ * Decode a slice.
+ * MpegEncContext.mb_y must be set to the MB row from the startcode.
+ * @return DECODE_SLICE_ERROR if the slice is damaged,
+ *         DECODE_SLICE_OK if this slice is OK
+ */
+static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
+                             const uint8_t **buf, int buf_size)
+{
+    AVCodecContext *avctx = s->avctx;
+    const int lowres      = s->avctx->lowres;
+    const int field_pic   = s->picture_structure != PICT_FRAME;
+
+    s->resync_mb_x =
+    s->resync_mb_y = -1;
+
+    av_assert0(mb_y < s->mb_height);
+
+    init_get_bits(&s->gb, *buf, buf_size * 8);
+    if(s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
+        skip_bits(&s->gb, 3);
+
+    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;
+
+    if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
+        skip_bits1(&s->gb);
+    } else {
+        while (get_bits_left(&s->gb) > 0) {
+            int code = get_vlc2(&s->gb, ff_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;
+            }
+        }
+    }
+
+    if (s->mb_x >= (unsigned)s->mb_width) {
+        av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
+        return -1;
+    }
+
+    if (avctx->hwaccel) {
+        const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
+        int start_code = -1;
+        buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
+        if (buf_end < *buf + buf_size)
+            buf_end -= 4;
+        s->mb_y = mb_y;
+        if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
+            return DECODE_SLICE_ERROR;
+        *buf = buf_end;
+        return DECODE_SLICE_OK;
+    }
+
+    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 == AV_PICTURE_TYPE_I ? "I" : (s->pict_type == AV_PICTURE_TYPE_P ? "P" : (s->pict_type == AV_PICTURE_TYPE_B ? "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 (;;) {
+        // If 1, we memcpy blocks in xvmcvideo.
+        if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
+            ff_xvmc_init_block(s); // set s->block
+
+        if (mpeg_decode_mb(s, s->block) < 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 = s->b8_stride;
+            int xy         = s->mb_x * 2 + s->mb_y * 2 * wrap;
+            int b8_xy      = 4 * (s->mb_x + s->mb_y * s->mb_stride);
+            int motion_x, motion_y, dir, i;
+
+            for (i = 0; i < 2; i++) {
+                for (dir = 0; dir < 2; dir++) {
+                    if (s->mb_intra || (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
+                        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][b8_xy    ] =
+                    s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
+                    av_assert2(s->field_select[dir][i] == 0 || s->field_select[dir][i] == 1);
+                }
+                xy += wrap;
+                b8_xy +=2;
+            }
+        }
+
+        s->dest[0] += 16 >> lowres;
+        s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
+        s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
+
+        ff_MPV_decode_mb(s, s->block);
+
+        if (++s->mb_x >= s->mb_width) {
+            const int mb_size = 16 >> s->avctx->lowres;
+
+            ff_mpeg_draw_horiz_band(s, mb_size*(s->mb_y >> field_pic), mb_size);
+            ff_MPV_report_decode_progress(s);
+
+            s->mb_x = 0;
+            s->mb_y += 1 << field_pic;
+
+            if (s->mb_y >= s->mb_height) {
+                int left   = get_bits_left(&s->gb);
+                int is_d10 = s->chroma_format == 2 && s->pict_type == AV_PICTURE_TYPE_I && 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 >= 32 && !is_d10) {
+                    GetBitContext gb = s->gb;
+                    align_get_bits(&gb);
+                    if (show_bits(&gb, 24) == 0x060E2B) {
+                        av_log(avctx, AV_LOG_DEBUG, "Invalid MXF data found in video stream\n");
+                        is_d10 = 1;
+                    }
+                }
+
+                if (left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
+                    || ((avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_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 increment again */
+            s->mb_skip_run = 0;
+            for (;;) {
+                int code = get_vlc2(&s->gb, ff_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;
+                }
+            }
+            if (s->mb_skip_run) {
+                int i;
+                if (s->pict_type == AV_PICTURE_TYPE_I) {
+                    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 == AV_PICTURE_TYPE_P) {
+                    /* 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) & 1;
+                } else {
+                    /* 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];
+                }
+            }
+        }
+    }
+eos: // end of slice
+    *buf += (get_bits_count(&s->gb)-1)/8;
+    av_dlog(s, "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   = *(void**)arg;
+    const uint8_t *buf  = s->gb.buffer;
+    int mb_y            = s->start_mb_y;
+    const int field_pic = s->picture_structure != PICT_FRAME;
+
+    s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
+
+    for (;;) {
+        uint32_t start_code;
+        int ret;
+
+        ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
+        emms_c();
+        av_dlog(c, "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->er.error_count);
+        if (ret < 0) {
+            if (c->err_recognition & AV_EF_EXPLODE)
+                return ret;
+            if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
+                ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
+        } else {
+            ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_AC_END | ER_DC_END | ER_MV_END);
+        }
+
+        if (s->mb_y == s->end_mb_y)
+            return 0;
+
+        start_code = -1;
+        buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
+        mb_y= start_code - SLICE_MIN_START_CODE;
+        if(s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
+            mb_y += (*buf&0xE0)<<2;
+        mb_y <<= field_pic;
+        if (s->picture_structure == PICT_BOTTOM_FIELD)
+            mb_y++;
+        if (mb_y < 0 || mb_y >= s->end_mb_y)
+            return -1;
+    }
+}
+
+/**
+ * Handle slice ends.
+ * @return 1 if it seems to be the last slice
+ */
+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;
+
+    if (s->avctx->hwaccel) {
+        if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
+            av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
+    }
+
+    if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
+        ff_xvmc_field_end(s);
+
+    /* end of slice reached */
+    if (/*s->mb_y << field_pic == s->mb_height &&*/ !s->first_field && !s->first_slice) {
+        /* end of image */
+
+        ff_er_frame_end(&s->er);
+
+        ff_MPV_frame_end(s);
+
+        if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
+            int ret = av_frame_ref(pict, &s->current_picture_ptr->f);
+            if (ret < 0)
+                return ret;
+            ff_print_debug_info(s, s->current_picture_ptr, pict);
+            ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG2);
+        } else {
+            if (avctx->active_thread_type & FF_THREAD_FRAME)
+                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) {
+                int ret = av_frame_ref(pict, &s->last_picture_ptr->f);
+                if (ret < 0)
+                    return ret;
+                ff_print_debug_info(s, s->last_picture_ptr, pict);
+                ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG2);
+            }
+        }
+
+        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) {
+        av_log(avctx, AV_LOG_WARNING, "Invalid horizontal or vertical size "
+               "value.\n");
+        if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
+            return AVERROR_INVALIDDATA;
+    }
+    s->aspect_ratio_info = get_bits(&s->gb, 4);
+    if (s->aspect_ratio_info == 0) {
+        av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
+        if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
+            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)) {
+        load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
+    } 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)) {
+        load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
+    } 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 MPEG-2 parameters so that it emulates MPEG-1 */
+    s->progressive_sequence = 1;
+    s->progressive_frame    = 1;
+    s->picture_structure    = PICT_FRAME;
+    s->first_field          = 0;
+    s->frame_pred_frame_dct = 1;
+    s->chroma_format        = 1;
+    s->codec_id             = s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
+    s->out_format           = FMT_MPEG1;
+    s->swap_uv              = 0; // AFAIK VCR2 does not 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 MPEG-1 context decoding */
+    s->out_format = FMT_MPEG1;
+    if (s1->mpeg_enc_ctx_allocated) {
+        ff_MPV_common_end(s);
+    }
+    s->width  = avctx->coded_width;
+    s->height = avctx->coded_height;
+    avctx->has_b_frames = 0; // true?
+    s->low_delay = 1;
+
+    avctx->pix_fmt = mpeg_get_pixelformat(avctx);
+    setup_hwaccel_for_pixfmt(avctx);
+
+    if (ff_MPV_common_init(s) < 0)
+        return -1;
+    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->first_field           = 0;
+    s->frame_pred_frame_dct  = 1;
+    s->chroma_format         = 1;
+    if (s->codec_tag == AV_RL32("BW10")) {
+        s->codec_id              = s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
+    } else {
+        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
+        s->codec_id              = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
+    }
+    s1->save_width           = s->width;
+    s1->save_height          = s->height;
+    s1->save_progressive_seq = s->progressive_sequence;
+    return 0;
+}
+
+
+static void mpeg_decode_user_data(AVCodecContext *avctx,
+                                  const uint8_t *p, int buf_size)
+{
+    Mpeg1Context *s = avctx->priv_data;
+    const uint8_t *buf_end = p + buf_size;
+
+    if(buf_size > 29){
+        int i;
+        for(i=0; i<20; i++)
+            if(!memcmp(p+i, "\0TMPGEXS\0", 9)){
+                s->tmpgexs= 1;
+            }
+
+/*        for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
+            av_log(avctx, AV_LOG_ERROR, "%c", p[i]);
+        }
+            av_log(avctx, AV_LOG_ERROR, "\n");*/
+    }
+
+    /* we parse the DTG active format information */
+    if (buf_end - p >= 5 &&
+        p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
+        int flags = p[4];
+        p += 5;
+        if (flags & 0x80) {
+            /* skip event id */
+            p += 2;
+        }
+        if (flags & 0x40) {
+            if (buf_end - p < 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 broken_link;
+    int64_t tc;
+
+    init_get_bits(&s->gb, buf, buf_size*8);
+
+    tc = avctx->timecode_frame_start = get_bits(&s->gb, 25);
+
+    s->closed_gop = get_bits1(&s->gb);
+    /*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) {
+        char tcbuf[AV_TIMECODE_STR_SIZE];
+        av_timecode_make_mpeg_tc_string(tcbuf, tc);
+        av_log(s->avctx, AV_LOG_DEBUG,
+               "GOP (%s) closed_gop=%d broken_link=%d\n",
+               tcbuf, s->closed_gop, broken_link);
+    }
+}
+
+static int decode_chunks(AVCodecContext *avctx,
+                         AVFrame *picture, int *got_output,
+                         const uint8_t *buf, int buf_size)
+{
+    Mpeg1Context *s = avctx->priv_data;
+    MpegEncContext *s2 = &s->mpeg_enc_ctx;
+    const uint8_t *buf_ptr = buf;
+    const uint8_t *buf_end = buf + buf_size;
+    int ret, input_size;
+    int last_code = 0;
+    int picture_start_code_seen = 0;
+
+    for (;;) {
+        /* find next start code */
+        uint32_t start_code = -1;
+        buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
+        if (start_code > 0x1ff) {
+            if (s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT) {
+                if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
+                    int i;
+                    av_assert0(avctx->thread_count > 1);
+
+                    avctx->execute(avctx, slice_decode_thread,  &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
+                    for (i = 0; i < s->slice_count; i++)
+                        s2->er.error_count += s2->thread_context[i]->er.error_count;
+                }
+
+                if ((CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER)
+                    && uses_vdpau(avctx))
+                    ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
+
+                ret = slice_end(avctx, picture);
+                if (ret < 0)
+                    return ret;
+                else if (ret) {
+                    if (s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
+                        *got_output = 1;
+                }
+            }
+            s2->pict_type = 0;
+            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 %td left %d\n", start_code, buf_ptr-buf, input_size);
+        }
+
+        /* prepare data for next start code */
+        switch (start_code) {
+        case SEQ_START_CODE:
+            if (last_code == 0) {
+                mpeg1_decode_sequence(avctx, buf_ptr, input_size);
+                if(buf != avctx->extradata)
+                    s->sync=1;
+            } else {
+                av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
+                if (avctx->err_recognition & AV_EF_EXPLODE)
+                    return AVERROR_INVALIDDATA;
+            }
+            break;
+
+        case PICTURE_START_CODE:
+            if (picture_start_code_seen && s2->picture_structure == PICT_FRAME) {
+               /* If it's a frame picture, there can't be more than one picture header.
+                  Yet, it does happen and we need to handle it. */
+               av_log(avctx, AV_LOG_WARNING, "ignoring extra picture following a frame-picture\n");
+               break;
+            }
+            picture_start_code_seen = 1;
+
+            if (s2->width <= 0 || s2->height <= 0) {
+                av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
+                       s2->width, s2->height);
+                return AVERROR_INVALIDDATA;
+            }
+
+            if(s->tmpgexs){
+                s2->intra_dc_precision= 3;
+                s2->intra_matrix[0]= 1;
+            }
+            if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && s->slice_count) {
+                int i;
+
+                avctx->execute(avctx, slice_decode_thread,
+                               s2->thread_context, NULL,
+                               s->slice_count, sizeof(void*));
+                for (i = 0; i < s->slice_count; i++)
+                    s2->er.error_count += s2->thread_context[i]->er.error_count;
+                s->slice_count = 0;
+            }
+            if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
+                ret = mpeg_decode_postinit(avctx);
+                if (ret < 0) {
+                    av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
+                    return ret;
+                }
+
+                /* we have a complete image: we try to decompress it */
+                if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
+                    s2->pict_type = 0;
+                s2->first_slice = 1;
+                last_code = PICTURE_START_CODE;
+            } else {
+                av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
+                if (avctx->err_recognition & AV_EF_EXPLODE)
+                    return AVERROR_INVALIDDATA;
+            }
+            break;
+        case EXT_START_CODE:
+            init_get_bits(&s2->gb, buf_ptr, input_size*8);
+
+            switch (get_bits(&s2->gb, 4)) {
+            case 0x1:
+                if (last_code == 0) {
+                mpeg_decode_sequence_extension(s);
+                } else {
+                    av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
+                    if (avctx->err_recognition & AV_EF_EXPLODE)
+                        return AVERROR_INVALIDDATA;
+                }
+                break;
+            case 0x2:
+                mpeg_decode_sequence_display_extension(s);
+                break;
+            case 0x3:
+                mpeg_decode_quant_matrix_extension(s2);
+                break;
+            case 0x7:
+                mpeg_decode_picture_display_extension(s);
+                break;
+            case 0x8:
+                if (last_code == PICTURE_START_CODE) {
+                    mpeg_decode_picture_coding_extension(s);
+                } else {
+                    av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
+                    if (avctx->err_recognition & AV_EF_EXPLODE)
+                        return AVERROR_INVALIDDATA;
+                }
+                break;
+            }
+            break;
+        case USER_START_CODE:
+            mpeg_decode_user_data(avctx, buf_ptr, input_size);
+            break;
+        case GOP_START_CODE:
+            if (last_code == 0) {
+                s2->first_field=0;
+                mpeg_decode_gop(avctx, buf_ptr, input_size);
+                s->sync=1;
+            } else {
+                av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
+                if (avctx->err_recognition & AV_EF_EXPLODE)
+                    return AVERROR_INVALIDDATA;
+            }
+            break;
+        default:
+            if (start_code >= SLICE_MIN_START_CODE &&
+                start_code <= SLICE_MAX_START_CODE && last_code == PICTURE_START_CODE) {
+
+                if (s2->progressive_sequence && !s2->progressive_frame) {
+                    s2->progressive_frame = 1;
+                    av_log(s2->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
+                }
+
+                if (s2->picture_structure == 0 || (s2->progressive_frame && s2->picture_structure != PICT_FRAME)) {
+                    av_log(s2->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s2->picture_structure);
+                    s2->picture_structure = PICT_FRAME;
+                }
+
+                if (s2->progressive_sequence && !s2->frame_pred_frame_dct) {
+                    av_log(s2->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
+                }
+
+                if (s2->picture_structure == PICT_FRAME) {
+                    s2->first_field = 0;
+                    s2->v_edge_pos  = 16 * s2->mb_height;
+                } else {
+                    s2->first_field ^= 1;
+                    s2->v_edge_pos   = 8 * s2->mb_height;
+                    memset(s2->mbskip_table, 0, s2->mb_stride * s2->mb_height);
+                }
+            }
+            if (start_code >= SLICE_MIN_START_CODE &&
+                start_code <= SLICE_MAX_START_CODE && last_code != 0) {
+                const int field_pic = s2->picture_structure != PICT_FRAME;
+                int mb_y = start_code - SLICE_MIN_START_CODE;
+                last_code = SLICE_MIN_START_CODE;
+                if(s2->codec_id != AV_CODEC_ID_MPEG1VIDEO && s2->mb_height > 2800/16)
+                    mb_y += (*buf_ptr&0xE0)<<2;
+
+                mb_y <<= field_pic;
+                if (s2->picture_structure == PICT_BOTTOM_FIELD)
+                    mb_y++;
+
+                if (mb_y >= s2->mb_height) {
+                    av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
+                    return -1;
+                }
+
+                if (s2->last_picture_ptr == NULL) {
+                /* Skip B-frames if we do not have reference frames and gop is not closed */
+                    if (s2->pict_type == AV_PICTURE_TYPE_B) {
+                        if (!s2->closed_gop)
+                            break;
+                    }
+                }
+                if (s2->pict_type == AV_PICTURE_TYPE_I || (s2->flags2 & CODEC_FLAG2_SHOW_ALL))
+                    s->sync=1;
+                if (s2->next_picture_ptr == NULL) {
+                /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
+                    if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) break;
+                }
+                if ((avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type == AV_PICTURE_TYPE_B) ||
+                    (avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type != AV_PICTURE_TYPE_I) ||
+                     avctx->skip_frame >= AVDISCARD_ALL)
+                    break;
+
+                if (!s->mpeg_enc_ctx_allocated)
+                    break;
+
+                if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
+                    if (mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
+                        break;
+                }
+
+                if (!s2->pict_type) {
+                    av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
+                    if (avctx->err_recognition & AV_EF_EXPLODE)
+                        return AVERROR_INVALIDDATA;
+                    break;
+                }
+
+                if (s2->first_slice) {
+                    s2->first_slice = 0;
+                    if (mpeg_field_start(s2, buf, buf_size) < 0)
+                        return -1;
+                }
+                if (!s2->current_picture_ptr) {
+                    av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
+                    return AVERROR_INVALIDDATA;
+                }
+
+                if (uses_vdpau(avctx)) {
+                    s->slice_count++;
+                    break;
+                }
+
+                if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
+                    int threshold = (s2->mb_height * s->slice_count +
+                                     s2->slice_context_count / 2) /
+                                    s2->slice_context_count;
+                    av_assert0(avctx->thread_count > 1);
+                    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;
+                            ret = ff_update_duplicate_context(thread_context,
+                                                              s2);
+                            if (ret < 0)
+                                return ret;
+                        }
+                        init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
+                        s->slice_count++;
+                    }
+                    buf_ptr += 2; // FIXME add minimum number of bytes per slice
+                } else {
+                    ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
+                    emms_c();
+
+                    if (ret < 0) {
+                        if (avctx->err_recognition & AV_EF_EXPLODE)
+                            return ret;
+                        if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
+                            ff_er_add_slice(&s2->er, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
+                    } else {
+                        ff_er_add_slice(&s2->er, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x-1, s2->mb_y, ER_AC_END | ER_DC_END | ER_MV_END);
+                    }
+                }
+            }
+            break;
+        }
+    }
+}
+
+static int mpeg_decode_frame(AVCodecContext *avctx,
+                             void *data, int *got_output,
+                             AVPacket *avpkt)
+{
+    const uint8_t *buf = avpkt->data;
+    int ret;
+    int buf_size = avpkt->size;
+    Mpeg1Context *s = avctx->priv_data;
+    AVFrame *picture = data;
+    MpegEncContext *s2 = &s->mpeg_enc_ctx;
+    av_dlog(avctx, "fill_buffer\n");
+
+    if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
+        /* special case for last picture */
+        if (s2->low_delay == 0 && s2->next_picture_ptr) {
+            int ret = av_frame_ref(picture, &s2->next_picture_ptr->f);
+            if (ret < 0)
+                return ret;
+
+            s2->next_picture_ptr = NULL;
+
+            *got_output = 1;
+        }
+        return buf_size;
+    }
+
+    if (s2->flags & CODEC_FLAG_TRUNCATED) {
+        int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
+
+        if (ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0)
+            return buf_size;
+    }
+
+    s2->codec_tag = avpriv_toupper4(avctx->codec_tag);
+    if (s->mpeg_enc_ctx_allocated == 0 && (   s2->codec_tag == AV_RL32("VCR2")
+                                           || s2->codec_tag == AV_RL32("BW10")
+                                          ))
+        vcr2_init_sequence(avctx);
+
+    s->slice_count = 0;
+
+    if (avctx->extradata && !s->extradata_decoded) {
+        ret = decode_chunks(avctx, picture, got_output, avctx->extradata, avctx->extradata_size);
+        if(*got_output) {
+            av_log(avctx, AV_LOG_ERROR, "picture in extradata\n");
+            *got_output = 0;
+        }
+        s->extradata_decoded = 1;
+        if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) {
+            s2->current_picture_ptr = NULL;
+            return ret;
+        }
+    }
+
+    ret = decode_chunks(avctx, picture, got_output, buf, buf_size);
+    if (ret<0 || *got_output)
+        s2->current_picture_ptr = NULL;
+
+    return ret;
+}
+
+
+static void flush(AVCodecContext *avctx)
+{
+    Mpeg1Context *s = avctx->priv_data;
+
+    s->sync=0;
+
+    ff_mpeg_flush(avctx);
+}
+
+static int mpeg_decode_end(AVCodecContext *avctx)
+{
+    Mpeg1Context *s = avctx->priv_data;
+
+    if (s->mpeg_enc_ctx_allocated)
+        ff_MPV_common_end(&s->mpeg_enc_ctx);
+    return 0;
+}
+
+static const AVProfile mpeg2_video_profiles[] = {
+    { FF_PROFILE_MPEG2_422,          "4:2:2"              },
+    { FF_PROFILE_MPEG2_HIGH,         "High"               },
+    { FF_PROFILE_MPEG2_SS,           "Spatially Scalable" },
+    { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable"       },
+    { FF_PROFILE_MPEG2_MAIN,         "Main"               },
+    { FF_PROFILE_MPEG2_SIMPLE,       "Simple"             },
+    { FF_PROFILE_RESERVED,           "Reserved"           },
+    { FF_PROFILE_RESERVED,           "Reserved"           },
+    { FF_PROFILE_UNKNOWN },
+};
+
+
+AVCodec ff_mpeg1video_decoder = {
+    .name                  = "mpeg1video",
+    .type                  = AVMEDIA_TYPE_VIDEO,
+    .id                    = AV_CODEC_ID_MPEG1VIDEO,
+    .priv_data_size        = sizeof(Mpeg1Context),
+    .init                  = mpeg_decode_init,
+    .close                 = mpeg_decode_end,
+    .decode                = mpeg_decode_frame,
+    .capabilities          = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
+                             CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
+                             CODEC_CAP_SLICE_THREADS,
+    .flush                 = flush,
+    .max_lowres            = 3,
+    .long_name             = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
+    .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
+};
+
+AVCodec ff_mpeg2video_decoder = {
+    .name           = "mpeg2video",
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_MPEG2VIDEO,
+    .priv_data_size = sizeof(Mpeg1Context),
+    .init           = mpeg_decode_init,
+    .close          = mpeg_decode_end,
+    .decode         = mpeg_decode_frame,
+    .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
+                      CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
+                      CODEC_CAP_SLICE_THREADS,
+    .flush          = flush,
+    .max_lowres     = 3,
+    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
+    .profiles       = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
+};
+
+//legacy decoder
+AVCodec ff_mpegvideo_decoder = {
+    .name           = "mpegvideo",
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_MPEG2VIDEO,
+    .priv_data_size = sizeof(Mpeg1Context),
+    .init           = mpeg_decode_init,
+    .close          = mpeg_decode_end,
+    .decode         = mpeg_decode_frame,
+    .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
+    .flush          = flush,
+    .max_lowres     = 3,
+    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
+};
+
+#if CONFIG_MPEG_XVMC_DECODER
+static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
+{
+    if (avctx->active_thread_type & FF_THREAD_SLICE)
+        return -1;
+    if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
+        return -1;
+    if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
+        av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
+    }
+    mpeg_decode_init(avctx);
+
+    avctx->pix_fmt           = AV_PIX_FMT_XVMC_MPEG2_IDCT;
+    avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
+
+    return 0;
+}
+
+AVCodec ff_mpeg_xvmc_decoder = {
+    .name           = "mpegvideo_xvmc",
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_MPEG2VIDEO_XVMC,
+    .priv_data_size = sizeof(Mpeg1Context),
+    .init           = mpeg_mc_decode_init,
+    .close          = mpeg_decode_end,
+    .decode         = mpeg_decode_frame,
+    .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
+                      CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
+    .flush          = flush,
+    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
+};
+
+#endif
+
+#if CONFIG_MPEG_VDPAU_DECODER
+AVCodec ff_mpeg_vdpau_decoder = {
+    .name           = "mpegvideo_vdpau",
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_MPEG2VIDEO,
+    .priv_data_size = sizeof(Mpeg1Context),
+    .init           = mpeg_decode_init,
+    .close          = mpeg_decode_end,
+    .decode         = mpeg_decode_frame,
+    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
+                      CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
+    .flush          = flush,
+    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
+};
+#endif
+
+#if CONFIG_MPEG1_VDPAU_DECODER
+AVCodec ff_mpeg1_vdpau_decoder = {
+    .name           = "mpeg1video_vdpau",
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_MPEG1VIDEO,
+    .priv_data_size = sizeof(Mpeg1Context),
+    .init           = mpeg_decode_init,
+    .close          = mpeg_decode_end,
+    .decode         = mpeg_decode_frame,
+    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
+                      CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
+    .flush          = flush,
+    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
+};
+#endif
diff --git a/libavcodec/mpeg4video.c b/libavcodec/mpeg4video.c
index 7444f26..9b86997 100644
--- a/libavcodec/mpeg4video.c
+++ b/libavcodec/mpeg4video.c
@@ -89,7 +89,7 @@
     uint16_t time_pb= s->pb_time;
     int p_mx, p_my;
 
-    p_mx = s->next_picture.f.motion_val[0][xy][0];
+    p_mx = s->next_picture.motion_val[0][xy][0];
     if((unsigned)(p_mx + tab_bias) < tab_size){
         s->mv[0][i][0] = s->direct_scale_mv[0][p_mx + tab_bias] + mx;
         s->mv[1][i][0] = mx ? s->mv[0][i][0] - p_mx
@@ -99,7 +99,7 @@
         s->mv[1][i][0] = mx ? s->mv[0][i][0] - p_mx
                             : p_mx*(time_pb - time_pp)/time_pp;
     }
-    p_my = s->next_picture.f.motion_val[0][xy][1];
+    p_my = s->next_picture.motion_val[0][xy][1];
     if((unsigned)(p_my + tab_bias) < tab_size){
         s->mv[0][i][1] = s->direct_scale_mv[0][p_my + tab_bias] + my;
         s->mv[1][i][1] = my ? s->mv[0][i][1] - p_my
@@ -120,7 +120,7 @@
  */
 int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my){
     const int mb_index= s->mb_x + s->mb_y*s->mb_stride;
-    const int colocated_mb_type = s->next_picture.f.mb_type[mb_index];
+    const int colocated_mb_type = s->next_picture.mb_type[mb_index];
     uint16_t time_pp;
     uint16_t time_pb;
     int i;
@@ -137,7 +137,7 @@
     } else if(IS_INTERLACED(colocated_mb_type)){
         s->mv_type = MV_TYPE_FIELD;
         for(i=0; i<2; i++){
-            int field_select = s->next_picture.f.ref_index[0][4 * mb_index + 2 * i];
+            int field_select = s->next_picture.ref_index[0][4 * mb_index + 2 * i];
             s->field_select[0][i]= field_select;
             s->field_select[1][i]= i;
             if(s->top_field_first){
diff --git a/libavcodec/mpeg4videodec.c b/libavcodec/mpeg4videodec.c
index 2551b51..342707b 100644
--- a/libavcodec/mpeg4videodec.c
+++ b/libavcodec/mpeg4videodec.c
@@ -21,6 +21,8 @@
  */
 
 #include "libavutil/opt.h"
+#include "error_resilience.h"
+#include "internal.h"
 #include "mpegvideo.h"
 #include "mpeg4video.h"
 #include "h263.h"
@@ -56,7 +58,7 @@
 {
     int i;
     int16_t *ac_val, *ac_val1;
-    int8_t * const qscale_table = s->current_picture.f.qscale_table;
+    int8_t * const qscale_table = s->current_picture.qscale_table;
 
     /* find prediction */
     ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
@@ -571,13 +573,13 @@
                 }while(cbpc == 8);
 
                 s->cbp_table[xy]= cbpc & 3;
-                s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
+                s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
                 s->mb_intra = 1;
 
                 if(cbpc & 4) {
                     ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
                 }
-                s->current_picture.f.qscale_table[xy]= s->qscale;
+                s->current_picture.qscale_table[xy]= s->qscale;
 
                 s->mbintra_table[xy]= 1;
                 for(i=0; i<6; i++){
@@ -593,7 +595,7 @@
                 s->pred_dir_table[xy]= dir;
             }else{ /* P/S_TYPE */
                 int mx, my, pred_x, pred_y, bits;
-                int16_t * const mot_val = s->current_picture.f.motion_val[0][s->block_index[0]];
+                int16_t * const mot_val = s->current_picture.motion_val[0][s->block_index[0]];
                 const int stride= s->b8_stride*2;
 
 try_again:
@@ -605,11 +607,11 @@
                 if(bits&0x10000){
                     /* skip mb */
                     if(s->pict_type==AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE){
-                        s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_GMC | MB_TYPE_L0;
+                        s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_GMC | MB_TYPE_L0;
                         mx= get_amv(s, 0);
                         my= get_amv(s, 1);
                     }else{
-                        s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
+                        s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
                         mx=my=0;
                     }
                     mot_val[0       ]= mot_val[2       ]=
@@ -635,7 +637,7 @@
                 s->mb_intra = ((cbpc & 4) != 0);
 
                 if(s->mb_intra){
-                    s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
+                    s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
                     s->mbintra_table[xy]= 1;
                     mot_val[0       ]= mot_val[2       ]=
                     mot_val[0+stride]= mot_val[2+stride]= 0;
@@ -661,11 +663,11 @@
                             my = ff_h263_decode_motion(s, pred_y, s->f_code);
                             if (my >= 0xffff)
                                 return -1;
-                            s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
+                            s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
                         } else {
                             mx = get_amv(s, 0);
                             my = get_amv(s, 1);
-                            s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_GMC | MB_TYPE_L0;
+                            s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_GMC | MB_TYPE_L0;
                         }
 
                         mot_val[0       ]= mot_val[2       ] =
@@ -674,7 +676,7 @@
                         mot_val[1+stride]= mot_val[3+stride]= my;
                     } else {
                         int i;
-                        s->current_picture.f.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
+                        s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
                         for(i=0;i<4;i++) {
                             int16_t *mot_val= ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
                             mx = ff_h263_decode_motion(s, pred_x, s->f_code);
@@ -726,9 +728,9 @@
                 }
 
                 s->cbp_table[xy]|= cbpy<<2;
-                s->current_picture.f.mb_type[xy] |= ac_pred*MB_TYPE_ACPRED;
+                s->current_picture.mb_type[xy] |= ac_pred*MB_TYPE_ACPRED;
             }else{ /* P || S_TYPE */
-                if (IS_INTRA(s->current_picture.f.mb_type[xy])) {
+                if (IS_INTRA(s->current_picture.mb_type[xy])) {
                     int dir=0,i;
                     int ac_pred = get_bits1(&s->gb);
                     int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
@@ -741,7 +743,7 @@
                     if(s->cbp_table[xy] & 8) {
                         ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
                     }
-                    s->current_picture.f.qscale_table[xy] = s->qscale;
+                    s->current_picture.qscale_table[xy] = s->qscale;
 
                     for(i=0; i<6; i++){
                         int dc_pred_dir;
@@ -755,10 +757,10 @@
                     }
                     s->cbp_table[xy]&= 3; //remove dquant
                     s->cbp_table[xy]|= cbpy<<2;
-                    s->current_picture.f.mb_type[xy] |= ac_pred*MB_TYPE_ACPRED;
+                    s->current_picture.mb_type[xy] |= ac_pred*MB_TYPE_ACPRED;
                     s->pred_dir_table[xy]= dir;
-                } else if (IS_SKIP(s->current_picture.f.mb_type[xy])) {
-                    s->current_picture.f.qscale_table[xy] = s->qscale;
+                } else if (IS_SKIP(s->current_picture.mb_type[xy])) {
+                    s->current_picture.qscale_table[xy] = s->qscale;
                     s->cbp_table[xy]= 0;
                 }else{
                     int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
@@ -771,7 +773,7 @@
                     if(s->cbp_table[xy] & 8) {
                         ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
                     }
-                    s->current_picture.f.qscale_table[xy] = s->qscale;
+                    s->current_picture.qscale_table[xy] = s->qscale;
 
                     s->cbp_table[xy]&= 3; //remove dquant
                     s->cbp_table[xy]|= (cbpy^0xf)<<2;
@@ -1092,20 +1094,20 @@
     int cbp, mb_type;
     const int xy= s->mb_x + s->mb_y*s->mb_stride;
 
-    mb_type = s->current_picture.f.mb_type[xy];
+    mb_type = s->current_picture.mb_type[xy];
     cbp = s->cbp_table[xy];
 
     s->use_intra_dc_vlc= s->qscale < s->intra_dc_threshold;
 
-    if (s->current_picture.f.qscale_table[xy] != s->qscale) {
-        ff_set_qscale(s, s->current_picture.f.qscale_table[xy]);
+    if (s->current_picture.qscale_table[xy] != s->qscale) {
+        ff_set_qscale(s, s->current_picture.qscale_table[xy]);
     }
 
     if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type==AV_PICTURE_TYPE_S) {
         int i;
         for(i=0; i<4; i++){
-            s->mv[0][i][0] = s->current_picture.f.motion_val[0][s->block_index[i]][0];
-            s->mv[0][i][1] = s->current_picture.f.motion_val[0][s->block_index[i]][1];
+            s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
+            s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
         }
         s->mb_intra = IS_INTRA(mb_type);
 
@@ -1123,7 +1125,7 @@
                 s->mb_skipped = 1;
             }
         }else if(s->mb_intra){
-            s->ac_pred = IS_ACPRED(s->current_picture.f.mb_type[xy]);
+            s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
         }else if(!s->mb_intra){
 //            s->mcsel= 0; //FIXME do we need to init that
 
@@ -1136,7 +1138,7 @@
         }
     } else { /* I-Frame */
         s->mb_intra = 1;
-        s->ac_pred = IS_ACPRED(s->current_picture.f.mb_type[xy]);
+        s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
     }
 
     if (!IS_SKIP(mb_type)) {
@@ -1189,14 +1191,14 @@
                 s->mv_dir = MV_DIR_FORWARD;
                 s->mv_type = MV_TYPE_16X16;
                 if(s->pict_type==AV_PICTURE_TYPE_S && s->vol_sprite_usage==GMC_SPRITE){
-                    s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_GMC | MB_TYPE_16x16 | MB_TYPE_L0;
+                    s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_GMC | MB_TYPE_16x16 | MB_TYPE_L0;
                     s->mcsel=1;
                     s->mv[0][0][0]= get_amv(s, 0);
                     s->mv[0][0][1]= get_amv(s, 1);
 
                     s->mb_skipped = 0;
                 }else{
-                    s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
+                    s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
                     s->mcsel=0;
                     s->mv[0][0][0] = 0;
                     s->mv[0][0][1] = 0;
@@ -1231,7 +1233,7 @@
         s->mv_dir = MV_DIR_FORWARD;
         if ((cbpc & 16) == 0) {
             if(s->mcsel){
-                s->current_picture.f.mb_type[xy] = MB_TYPE_GMC | MB_TYPE_16x16 | MB_TYPE_L0;
+                s->current_picture.mb_type[xy] = MB_TYPE_GMC | MB_TYPE_16x16 | MB_TYPE_L0;
                 /* 16x16 global motion prediction */
                 s->mv_type = MV_TYPE_16X16;
                 mx= get_amv(s, 0);
@@ -1239,7 +1241,7 @@
                 s->mv[0][0][0] = mx;
                 s->mv[0][0][1] = my;
             }else if((!s->progressive_sequence) && get_bits1(&s->gb)){
-                s->current_picture.f.mb_type[xy] = MB_TYPE_16x8 | MB_TYPE_L0 | MB_TYPE_INTERLACED;
+                s->current_picture.mb_type[xy] = MB_TYPE_16x8 | MB_TYPE_L0 | MB_TYPE_INTERLACED;
                 /* 16x8 field motion prediction */
                 s->mv_type= MV_TYPE_FIELD;
 
@@ -1261,7 +1263,7 @@
                     s->mv[0][i][1] = my;
                 }
             }else{
-                s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
+                s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
                 /* 16x16 motion prediction */
                 s->mv_type = MV_TYPE_16X16;
                 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
@@ -1278,7 +1280,7 @@
                 s->mv[0][0][1] = my;
             }
         } else {
-            s->current_picture.f.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
+            s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
             s->mv_type = MV_TYPE_8X8;
             for(i=0;i<4;i++) {
                 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
@@ -1311,11 +1313,11 @@
                 s->last_mv[i][1][1]= 0;
             }
 
-            ff_thread_await_progress(&s->next_picture_ptr->f, s->mb_y, 0);
+            ff_thread_await_progress(&s->next_picture_ptr->tf, s->mb_y, 0);
         }
 
         /* if we skipped it in the future P Frame than skip it now too */
-        s->mb_skipped = s->next_picture.f.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC
+        s->mb_skipped = s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC
 
         if(s->mb_skipped){
                 /* skip mb */
@@ -1328,7 +1330,7 @@
             s->mv[0][0][1] = 0;
             s->mv[1][0][0] = 0;
             s->mv[1][0][1] = 0;
-            s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
+            s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
             goto end;
         }
 
@@ -1434,7 +1436,7 @@
             s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
             mb_type |= ff_mpeg4_set_direct_mv(s, mx, my);
         }
-        s->current_picture.f.mb_type[xy] = mb_type;
+        s->current_picture.mb_type[xy] = mb_type;
     } else { /* I-Frame */
         do{
             cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
@@ -1449,9 +1451,9 @@
 intra:
         s->ac_pred = get_bits1(&s->gb);
         if(s->ac_pred)
-            s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
+            s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
         else
-            s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
+            s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
 
         cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
         if(cbpy<0){
@@ -1498,9 +1500,9 @@
 
             if(s->pict_type==AV_PICTURE_TYPE_B){
                 const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
-                ff_thread_await_progress(&s->next_picture_ptr->f,
+                ff_thread_await_progress(&s->next_picture_ptr->tf,
                                         (s->mb_x + delta >= s->mb_width) ? FFMIN(s->mb_y+1, s->mb_height-1) : s->mb_y, 0);
-                if (s->next_picture.f.mbskip_table[xy + delta])
+                if (s->next_picture.mbskip_table[xy + delta])
                     return SLICE_OK;
             }
 
@@ -2300,6 +2302,8 @@
     s->time_increment_bits = 4; /* default value for broken headers */
     avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
 
+    avctx->internal->allocate_progress = 1;
+
     return 0;
 }
 
diff --git a/libavcodec/mpeg4videoenc.c b/libavcodec/mpeg4videoenc.c
index a07f956..5662735 100644
--- a/libavcodec/mpeg4videoenc.c
+++ b/libavcodec/mpeg4videoenc.c
@@ -126,7 +126,7 @@
 {
     int score= 0;
     int i, n;
-    int8_t * const qscale_table = s->current_picture.f.qscale_table;
+    int8_t * const qscale_table = s->current_picture.qscale_table;
 
     memcpy(zigzag_last_index, s->block_last_index, sizeof(int)*6);
 
@@ -203,7 +203,7 @@
  */
 void ff_clean_mpeg4_qscales(MpegEncContext *s){
     int i;
-    int8_t * const qscale_table = s->current_picture.f.qscale_table;
+    int8_t * const qscale_table = s->current_picture.qscale_table;
 
     ff_clean_h263_qscales(s);
 
@@ -499,7 +499,7 @@
             av_assert2(mb_type>=0);
 
             /* nothing to do if this MB was skipped in the next P Frame */
-            if (s->next_picture.f.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]) { //FIXME avoid DCT & ...
+            if (s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]) { //FIXME avoid DCT & ...
                 s->skip_count++;
                 s->mv[0][0][0]=
                 s->mv[0][0][1]=
@@ -641,7 +641,7 @@
                             break;
 
                         b_pic = pic->f.data[0] + offset;
-                        if (pic->f.type != FF_BUFFER_TYPE_SHARED)
+                        if (!pic->shared)
                             b_pic+= INPLACE_OFFSET;
 
                         if(x+16 > s->width || y+16 > s->height){
@@ -759,8 +759,8 @@
                     /* motion vectors: 8x8 mode*/
                     ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
 
-                    ff_h263_encode_motion_vector(s, s->current_picture.f.motion_val[0][ s->block_index[i] ][0] - pred_x,
-                                                    s->current_picture.f.motion_val[0][ s->block_index[i] ][1] - pred_y, s->f_code);
+                    ff_h263_encode_motion_vector(s, s->current_picture.motion_val[0][ s->block_index[i] ][0] - pred_x,
+                                                    s->current_picture.motion_val[0][ s->block_index[i] ][1] - pred_y, s->f_code);
                 }
             }
 
diff --git a/libavcodec/mpegaudio_parser.c b/libavcodec/mpegaudio_parser.c
index 5f97d71..4081936 100644
--- a/libavcodec/mpegaudio_parser.c
+++ b/libavcodec/mpegaudio_parser.c
@@ -30,6 +30,7 @@
     int frame_size;
     uint32_t header;
     int header_count;
+    int no_bitrate;
 } MpegAudioParseContext;
 
 #define MPA_HEADER_SIZE 4
@@ -77,11 +78,14 @@
                     s->header_count++;
                     s->frame_size = ret-4;
 
-                    if (s->header_count > 1) {
+                    if (s->header_count > 0) {
                         avctx->sample_rate= sr;
                         avctx->channels   = channels;
                         s1->duration      = frame_size;
-                        avctx->bit_rate   = bit_rate;
+                        if (s->no_bitrate || !avctx->bit_rate) {
+                            s->no_bitrate = 1;
+                            avctx->bit_rate += (bit_rate - avctx->bit_rate) / s->header_count;
+                        }
                     }
                     break;
                 }
diff --git a/libavcodec/mpegaudiodec.c b/libavcodec/mpegaudiodec.c
index 0266afe..4fffb6c 100644
--- a/libavcodec/mpegaudiodec.c
+++ b/libavcodec/mpegaudiodec.c
@@ -209,7 +209,7 @@
     if (g->block_type == 2) {
         if (g->switch_point) {
             if(s->sample_rate_index == 8)
-                av_log_ask_for_sample(s->avctx, "switch point in 8khz\n");
+                avpriv_request_sample(s->avctx, "switch point in 8khz");
             /* if switched mode, we handle the 36 first samples as
                 long blocks.  For 8000Hz, we handle the 72 first
                 exponents as long blocks */
@@ -1628,10 +1628,8 @@
     if (!samples) {
         av_assert0(s->frame != NULL);
         s->frame->nb_samples = s->avctx->frame_size;
-        if ((ret = ff_get_buffer(s->avctx, s->frame)) < 0) {
-            av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        if ((ret = ff_get_buffer(s->avctx, s->frame, 0)) < 0)
             return ret;
-        }
         samples = (OUT_INT **)s->frame->extended_data;
     }
 
@@ -1935,10 +1933,8 @@
 
     /* get output buffer */
     frame->nb_samples = MPA_FRAME_SIZE;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     out_samples = (OUT_INT **)frame->extended_data;
 
     // Discard too short frames
diff --git a/libavcodec/mpegaudioenc.c b/libavcodec/mpegaudioenc.c
index 75436f8..3a109f0 100644
--- a/libavcodec/mpegaudioenc.c
+++ b/libavcodec/mpegaudioenc.c
@@ -184,12 +184,6 @@
         total_quant_bits[i] = 12 * v;
     }
 
-#if FF_API_OLD_ENCODE_AUDIO
-    avctx->coded_frame= avcodec_alloc_frame();
-    if (!avctx->coded_frame)
-        return AVERROR(ENOMEM);
-#endif
-
     return 0;
 }
 
@@ -754,7 +748,7 @@
     }
     compute_bit_allocation(s, smr, bit_alloc, &padding);
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, MPA_MAX_CODED_FRAME_SIZE)) < 0)
         return ret;
 
     init_put_bits(&s->pb, avpkt->data, avpkt->size);
@@ -769,14 +763,6 @@
     return 0;
 }
 
-static av_cold int MPA_encode_close(AVCodecContext *avctx)
-{
-#if FF_API_OLD_ENCODE_AUDIO
-    av_freep(&avctx->coded_frame);
-#endif
-    return 0;
-}
-
 static const AVCodecDefault mp2_defaults[] = {
     { "b",    "128k" },
     { NULL },
@@ -789,7 +775,6 @@
     .priv_data_size        = sizeof(MpegAudioContext),
     .init                  = MPA_encode_init,
     .encode2               = MPA_encode_frame,
-    .close                 = MPA_encode_close,
     .sample_fmts           = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
                                                             AV_SAMPLE_FMT_NONE },
     .supported_samplerates = (const int[]){
diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 66a7ed5..69b10bb 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -27,6 +27,7 @@
  * The simplest mpeg encoder (well, it was the simplest!).
  */
 
+#include "libavutil/avassert.h"
 #include "libavutil/imgutils.h"
 #include "avcodec.h"
 #include "dsputil.h"
@@ -155,44 +156,12 @@
     ff_MPV_decode_mb(s, s->block);
 }
 
-const uint8_t *avpriv_mpv_find_start_code(const uint8_t *av_restrict p,
-                                          const uint8_t *end,
-                                          uint32_t *av_restrict state)
-{
-    int i;
-
-    assert(p <= end);
-    if (p >= end)
-        return end;
-
-    for (i = 0; i < 3; i++) {
-        uint32_t tmp = *state << 8;
-        *state = tmp + *(p++);
-        if (tmp == 0x100 || p == end)
-            return p;
-    }
-
-    while (p < end) {
-        if      (p[-1] > 1      ) p += 3;
-        else if (p[-2]          ) p += 2;
-        else if (p[-3]|(p[-1]-1)) p++;
-        else {
-            p++;
-            break;
-        }
-    }
-
-    p = FFMIN(p, end) - 4;
-    *state = AV_RB32(p);
-
-    return p + 4;
-}
-
 /* init common dct for both encoder and decoder */
 av_cold int ff_dct_common_init(MpegEncContext *s)
 {
     ff_dsputil_init(&s->dsp, s->avctx);
     ff_h264chroma_init(&s->h264chroma, 8); //for lowres
+    ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
     ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
 
     s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
@@ -232,29 +201,6 @@
     return 0;
 }
 
-void ff_copy_picture(Picture *dst, Picture *src)
-{
-    *dst = *src;
-    dst->f.type = FF_BUFFER_TYPE_COPY;
-}
-
-/**
- * Release a frame buffer
- */
-static void free_frame_buffer(MpegEncContext *s, Picture *pic)
-{
-    pic->period_since_free = 0;
-    /* WM Image / Screen codecs allocate internal buffers with different
-     * dimensions / colorspaces; ignore user-defined callbacks for these. */
-    if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
-        s->codec_id != AV_CODEC_ID_VC1IMAGE  &&
-        s->codec_id != AV_CODEC_ID_MSS2)
-        ff_thread_release_buffer(s->avctx, &pic->f);
-    else
-        avcodec_default_release_buffer(s->avctx, &pic->f);
-    av_freep(&pic->f.hwaccel_picture_private);
-}
-
 int ff_mpv_frame_size_alloc(MpegEncContext *s, int linesize)
 {
     int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
@@ -287,43 +233,49 @@
 {
     int r, ret;
 
-    if (s->avctx->hwaccel) {
-        assert(!pic->f.hwaccel_picture_private);
-        if (s->avctx->hwaccel->priv_data_size) {
-            pic->f.hwaccel_picture_private = av_mallocz(s->avctx->hwaccel->priv_data_size);
-            if (!pic->f.hwaccel_picture_private) {
-                av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
-                return -1;
-            }
-        }
-    }
-
+    pic->tf.f = &pic->f;
     if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
         s->codec_id != AV_CODEC_ID_VC1IMAGE  &&
         s->codec_id != AV_CODEC_ID_MSS2)
-        r = ff_thread_get_buffer(s->avctx, &pic->f);
-    else
-        r = avcodec_default_get_buffer(s->avctx, &pic->f);
+        r = ff_thread_get_buffer(s->avctx, &pic->tf,
+                                 pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
+    else {
+        pic->f.width  = s->avctx->width;
+        pic->f.height = s->avctx->height;
+        pic->f.format = s->avctx->pix_fmt;
+        r = avcodec_default_get_buffer2(s->avctx, &pic->f, 0);
+    }
 
-    if (r < 0 || !pic->f.type || !pic->f.data[0]) {
-        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %p)\n",
-               r, pic->f.type, pic->f.data[0]);
-        av_freep(&pic->f.hwaccel_picture_private);
+    if (r < 0 || !pic->f.data[0]) {
+        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
+               r, pic->f.data[0]);
         return -1;
     }
 
+    if (s->avctx->hwaccel) {
+        assert(!pic->hwaccel_picture_private);
+        if (s->avctx->hwaccel->priv_data_size) {
+            pic->hwaccel_priv_buf = av_buffer_allocz(s->avctx->hwaccel->priv_data_size);
+            if (!pic->hwaccel_priv_buf) {
+                av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
+                return -1;
+            }
+            pic->hwaccel_picture_private = pic->hwaccel_priv_buf->data;
+        }
+    }
+
     if (s->linesize && (s->linesize   != pic->f.linesize[0] ||
                         s->uvlinesize != pic->f.linesize[1])) {
         av_log(s->avctx, AV_LOG_ERROR,
                "get_buffer() failed (stride changed)\n");
-        free_frame_buffer(s, pic);
+        ff_mpeg_unref_picture(s, pic);
         return -1;
     }
 
     if (pic->f.linesize[1] != pic->f.linesize[2]) {
         av_log(s->avctx, AV_LOG_ERROR,
                "get_buffer() failed (uv stride mismatch)\n");
-        free_frame_buffer(s, pic);
+        ff_mpeg_unref_picture(s, pic);
         return -1;
     }
 
@@ -331,33 +283,116 @@
         (ret = ff_mpv_frame_size_alloc(s, pic->f.linesize[0])) < 0) {
         av_log(s->avctx, AV_LOG_ERROR,
                "get_buffer() failed to allocate context scratch buffers.\n");
-        free_frame_buffer(s, pic);
+        ff_mpeg_unref_picture(s, pic);
         return ret;
     }
 
     return 0;
 }
 
+static void free_picture_tables(Picture *pic)
+{
+    int i;
+
+    pic->alloc_mb_width  =
+    pic->alloc_mb_height = 0;
+
+    av_buffer_unref(&pic->mb_var_buf);
+    av_buffer_unref(&pic->mc_mb_var_buf);
+    av_buffer_unref(&pic->mb_mean_buf);
+    av_buffer_unref(&pic->mbskip_table_buf);
+    av_buffer_unref(&pic->qscale_table_buf);
+    av_buffer_unref(&pic->mb_type_buf);
+
+    for (i = 0; i < 2; i++) {
+        av_buffer_unref(&pic->motion_val_buf[i]);
+        av_buffer_unref(&pic->ref_index_buf[i]);
+    }
+}
+
+static int alloc_picture_tables(MpegEncContext *s, Picture *pic)
+{
+    const int big_mb_num    = s->mb_stride * (s->mb_height + 1) + 1;
+    const int mb_array_size = s->mb_stride * s->mb_height;
+    const int b8_array_size = s->b8_stride * s->mb_height * 2;
+    int i;
+
+
+    pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
+    pic->qscale_table_buf = av_buffer_allocz(big_mb_num + s->mb_stride);
+    pic->mb_type_buf      = av_buffer_allocz((big_mb_num + s->mb_stride) *
+                                             sizeof(uint32_t));
+    if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
+        return AVERROR(ENOMEM);
+
+    if (s->encoding) {
+        pic->mb_var_buf    = av_buffer_allocz(mb_array_size * sizeof(int16_t));
+        pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
+        pic->mb_mean_buf   = av_buffer_allocz(mb_array_size);
+        if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
+            return AVERROR(ENOMEM);
+    }
+
+    if (s->out_format == FMT_H263 || s->encoding ||
+               (s->avctx->debug & FF_DEBUG_MV) || s->avctx->debug_mv) {
+        int mv_size        = 2 * (b8_array_size + 4) * sizeof(int16_t);
+        int ref_index_size = 4 * mb_array_size;
+
+        for (i = 0; mv_size && i < 2; i++) {
+            pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
+            pic->ref_index_buf[i]  = av_buffer_allocz(ref_index_size);
+            if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
+                return AVERROR(ENOMEM);
+        }
+    }
+
+    pic->alloc_mb_width  = s->mb_width;
+    pic->alloc_mb_height = s->mb_height;
+
+    return 0;
+}
+
+static int make_tables_writable(Picture *pic)
+{
+    int ret, i;
+#define MAKE_WRITABLE(table) \
+do {\
+    if (pic->table &&\
+       (ret = av_buffer_make_writable(&pic->table)) < 0)\
+    return ret;\
+} while (0)
+
+    MAKE_WRITABLE(mb_var_buf);
+    MAKE_WRITABLE(mc_mb_var_buf);
+    MAKE_WRITABLE(mb_mean_buf);
+    MAKE_WRITABLE(mbskip_table_buf);
+    MAKE_WRITABLE(qscale_table_buf);
+    MAKE_WRITABLE(mb_type_buf);
+
+    for (i = 0; i < 2; i++) {
+        MAKE_WRITABLE(motion_val_buf[i]);
+        MAKE_WRITABLE(ref_index_buf[i]);
+    }
+
+    return 0;
+}
+
 /**
  * Allocate a Picture.
  * The pixels are allocated/set by calling get_buffer() if shared = 0
  */
 int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
 {
-    const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
+    int i, ret;
 
-    // the + 1 is needed so memset(,,stride*height) does not sig11
-
-    const int mb_array_size = s->mb_stride * s->mb_height;
-    const int b8_array_size = s->b8_stride * s->mb_height * 2;
-    const int b4_array_size = s->b4_stride * s->mb_height * 4;
-    int i;
-    int r = -1;
+    if (pic->qscale_table_buf)
+        if (   pic->alloc_mb_width  != s->mb_width
+            || pic->alloc_mb_height != s->mb_height)
+            free_picture_tables(pic);
 
     if (shared) {
         assert(pic->f.data[0]);
-        assert(pic->f.type == 0 || pic->f.type == FF_BUFFER_TYPE_SHARED);
-        pic->f.type = FF_BUFFER_TYPE_SHARED;
+        pic->shared = 1;
     } else {
         assert(!pic->f.data[0]);
 
@@ -368,101 +403,141 @@
         s->uvlinesize = pic->f.linesize[1];
     }
 
-    if (pic->f.qscale_table == NULL) {
-        if (s->encoding) {
-            FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_var,
-                              mb_array_size * sizeof(int16_t), fail)
-            FF_ALLOCZ_OR_GOTO(s->avctx, pic->mc_mb_var,
-                              mb_array_size * sizeof(int16_t), fail)
-            FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_mean,
-                              mb_array_size * sizeof(int8_t ), fail)
-        }
+    if (!pic->qscale_table_buf)
+        ret = alloc_picture_tables(s, pic);
+    else
+        ret = make_tables_writable(pic);
+    if (ret < 0)
+        goto fail;
 
-        FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.mbskip_table,
-                          mb_array_size * sizeof(uint8_t) + 2, fail)// the + 2 is for the slice end check
-        FF_ALLOCZ_OR_GOTO(s->avctx, pic->qscale_table_base,
-                          (big_mb_num + s->mb_stride) * sizeof(uint8_t),
-                          fail)
-        FF_ALLOCZ_OR_GOTO(s->avctx, pic->mb_type_base,
-                          (big_mb_num + s->mb_stride) * sizeof(uint32_t),
-                          fail)
-        pic->f.mb_type = pic->mb_type_base + 2 * s->mb_stride + 1;
-        pic->f.qscale_table = pic->qscale_table_base + 2 * s->mb_stride + 1;
-        if (s->out_format == FMT_H264) {
-            for (i = 0; i < 2; i++) {
-                FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i],
-                                  2 * (b4_array_size + 4) * sizeof(int16_t),
-                                  fail)
-                pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
-                FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.ref_index[i],
-                                  4 * mb_array_size * sizeof(uint8_t), fail)
-            }
-            pic->f.motion_subsample_log2 = 2;
-        } else if (s->out_format == FMT_H263 || s->encoding ||
-                   (s->avctx->debug & FF_DEBUG_MV) || s->avctx->debug_mv) {
-            for (i = 0; i < 2; i++) {
-                FF_ALLOCZ_OR_GOTO(s->avctx, pic->motion_val_base[i],
-                                  2 * (b8_array_size + 4) * sizeof(int16_t),
-                                  fail)
-                pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
-                FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.ref_index[i],
-                                  4 * mb_array_size * sizeof(uint8_t), fail)
-            }
-            pic->f.motion_subsample_log2 = 3;
-        }
-        if (s->avctx->debug&FF_DEBUG_DCT_COEFF) {
-            FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.dct_coeff,
-                              64 * mb_array_size * sizeof(int16_t) * 6, fail)
-        }
-        pic->f.qstride = s->mb_stride;
-        FF_ALLOCZ_OR_GOTO(s->avctx, pic->f.pan_scan,
-                          1 * sizeof(AVPanScan), fail)
+    if (s->encoding) {
+        pic->mb_var    = (uint16_t*)pic->mb_var_buf->data;
+        pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
+        pic->mb_mean   = pic->mb_mean_buf->data;
     }
 
-    pic->owner2 = s;
+    pic->mbskip_table = pic->mbskip_table_buf->data;
+    pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1;
+    pic->mb_type      = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
+
+    if (pic->motion_val_buf[0]) {
+        for (i = 0; i < 2; i++) {
+            pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
+            pic->ref_index[i]  = pic->ref_index_buf[i]->data;
+        }
+    }
 
     return 0;
-fail: // for  the FF_ALLOCZ_OR_GOTO macro
-    if (r >= 0)
-        free_frame_buffer(s, pic);
-    return -1;
+fail:
+    av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
+    ff_mpeg_unref_picture(s, pic);
+    free_picture_tables(pic);
+    return AVERROR(ENOMEM);
 }
 
 /**
  * Deallocate a picture.
  */
-static void free_picture(MpegEncContext *s, Picture *pic)
+void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic)
 {
-    int i;
+    int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
 
-    if (pic->f.data[0] && pic->f.type != FF_BUFFER_TYPE_SHARED) {
-        free_frame_buffer(s, pic);
-    }
+    pic->tf.f = &pic->f;
+    /* WM Image / Screen codecs allocate internal buffers with different
+     * dimensions / colorspaces; ignore user-defined callbacks for these. */
+    if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
+        s->codec_id != AV_CODEC_ID_VC1IMAGE  &&
+        s->codec_id != AV_CODEC_ID_MSS2)
+        ff_thread_release_buffer(s->avctx, &pic->tf);
+    else
+        av_frame_unref(&pic->f);
 
-    av_freep(&pic->mb_var);
-    av_freep(&pic->mc_mb_var);
-    av_freep(&pic->mb_mean);
-    av_freep(&pic->f.mbskip_table);
-    av_freep(&pic->qscale_table_base);
-    pic->f.qscale_table = NULL;
-    av_freep(&pic->mb_type_base);
-    pic->f.mb_type = NULL;
-    av_freep(&pic->f.dct_coeff);
-    av_freep(&pic->f.pan_scan);
-    pic->f.mb_type = NULL;
+    av_buffer_unref(&pic->hwaccel_priv_buf);
+
+    memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
+}
+
+static int update_picture_tables(Picture *dst, Picture *src)
+{
+     int i;
+
+#define UPDATE_TABLE(table)\
+do {\
+    if (src->table &&\
+        (!dst->table || dst->table->buffer != src->table->buffer)) {\
+        av_buffer_unref(&dst->table);\
+        dst->table = av_buffer_ref(src->table);\
+        if (!dst->table) {\
+            free_picture_tables(dst);\
+            return AVERROR(ENOMEM);\
+        }\
+    }\
+} while (0)
+
+    UPDATE_TABLE(mb_var_buf);
+    UPDATE_TABLE(mc_mb_var_buf);
+    UPDATE_TABLE(mb_mean_buf);
+    UPDATE_TABLE(mbskip_table_buf);
+    UPDATE_TABLE(qscale_table_buf);
+    UPDATE_TABLE(mb_type_buf);
     for (i = 0; i < 2; i++) {
-        av_freep(&pic->motion_val_base[i]);
-        av_freep(&pic->f.ref_index[i]);
-        pic->f.motion_val[i] = NULL;
+        UPDATE_TABLE(motion_val_buf[i]);
+        UPDATE_TABLE(ref_index_buf[i]);
     }
 
-    if (pic->f.type == FF_BUFFER_TYPE_SHARED) {
-        for (i = 0; i < 4; i++) {
-            pic->f.base[i] =
-            pic->f.data[i] = NULL;
-        }
-        pic->f.type = 0;
+    dst->mb_var        = src->mb_var;
+    dst->mc_mb_var     = src->mc_mb_var;
+    dst->mb_mean       = src->mb_mean;
+    dst->mbskip_table  = src->mbskip_table;
+    dst->qscale_table  = src->qscale_table;
+    dst->mb_type       = src->mb_type;
+    for (i = 0; i < 2; i++) {
+        dst->motion_val[i] = src->motion_val[i];
+        dst->ref_index[i]  = src->ref_index[i];
     }
+
+    dst->alloc_mb_width  = src->alloc_mb_width;
+    dst->alloc_mb_height = src->alloc_mb_height;
+
+    return 0;
+}
+
+int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src)
+{
+    int ret;
+
+    av_assert0(!dst->f.buf[0]);
+    av_assert0(src->f.buf[0]);
+
+    src->tf.f = &src->f;
+    dst->tf.f = &dst->f;
+    ret = ff_thread_ref_frame(&dst->tf, &src->tf);
+    if (ret < 0)
+        goto fail;
+
+    ret = update_picture_tables(dst, src);
+    if (ret < 0)
+        goto fail;
+
+    if (src->hwaccel_picture_private) {
+        dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
+        if (!dst->hwaccel_priv_buf)
+            goto fail;
+        dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
+    }
+
+    dst->field_picture           = src->field_picture;
+    dst->mb_var_sum              = src->mb_var_sum;
+    dst->mc_mb_var_sum           = src->mc_mb_var_sum;
+    dst->b_frame_score           = src->b_frame_score;
+    dst->needs_realloc           = src->needs_realloc;
+    dst->reference               = src->reference;
+    dst->shared                  = src->shared;
+
+    return 0;
+fail:
+    ff_mpeg_unref_picture(s, dst);
+    return ret;
 }
 
 static int init_duplicate_context(MpegEncContext *s)
@@ -583,8 +658,7 @@
 int ff_mpeg_update_thread_context(AVCodecContext *dst,
                                   const AVCodecContext *src)
 {
-    int i;
-    int err;
+    int i, ret;
     MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
 
     if (dst == src)
@@ -602,12 +676,12 @@
         s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
 
         if (s1->context_initialized){
-            s->picture_range_start  += MAX_PICTURE_COUNT;
-            s->picture_range_end    += MAX_PICTURE_COUNT;
-            if((err = ff_MPV_common_init(s)) < 0){
+//             s->picture_range_start  += MAX_PICTURE_COUNT;
+//             s->picture_range_end    += MAX_PICTURE_COUNT;
+            if((ret = ff_MPV_common_init(s)) < 0){
                 memset(s, 0, sizeof(MpegEncContext));
                 s->avctx = dst;
-                return err;
+                return ret;
             }
         }
     }
@@ -616,8 +690,8 @@
         s->context_reinit = 0;
         s->height = s1->height;
         s->width  = s1->width;
-        if ((err = ff_MPV_common_frame_size_change(s)) < 0)
-            return err;
+        if ((ret = ff_MPV_common_frame_size_change(s)) < 0)
+            return ret;
     }
 
     s->avctx->coded_height  = s1->avctx->coded_height;
@@ -630,16 +704,29 @@
     s->input_picture_number = s1->input_picture_number;
 
     av_assert0(!s->picture || s->picture != s1->picture);
-    memcpy(s->picture, s1->picture, s1->picture_count * sizeof(Picture));
-    memcpy(&s->last_picture, &s1->last_picture,
-           (char *) &s1->last_picture_ptr - (char *) &s1->last_picture);
-
-    // reset s->picture[].f.extended_data to s->picture[].f.data
-    for (i = 0; i < s->picture_count; i++) {
-        s->picture[i].f.extended_data = s->picture[i].f.data;
-        s->picture[i].period_since_free ++;
+    if(s->picture)
+    for (i = 0; i < MAX_PICTURE_COUNT; i++) {
+        ff_mpeg_unref_picture(s, &s->picture[i]);
+        if (s1->picture[i].f.data[0] &&
+            (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0)
+            return ret;
     }
 
+#define UPDATE_PICTURE(pic)\
+do {\
+    ff_mpeg_unref_picture(s, &s->pic);\
+    if (s1->pic.f.data[0])\
+        ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\
+    else\
+        ret = update_picture_tables(&s->pic, &s1->pic);\
+    if (ret < 0)\
+        return ret;\
+} while (0)
+
+    UPDATE_PICTURE(current_picture);
+    UPDATE_PICTURE(last_picture);
+    UPDATE_PICTURE(next_picture);
+
     s->last_picture_ptr    = REBASE_PICTURE(s1->last_picture_ptr,    s, s1);
     s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
     s->next_picture_ptr    = REBASE_PICTURE(s1->next_picture_ptr,    s, s1);
@@ -728,9 +815,6 @@
     s->f_code                = 1;
     s->b_code                = 1;
 
-    s->picture_range_start   = 0;
-    s->picture_range_end     = MAX_PICTURE_COUNT;
-
     s->slice_context_count   = 1;
 }
 
@@ -918,7 +1002,7 @@
 
     if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
         s->mb_height = (s->height + 31) / 32 * 2;
-    else if (s->codec_id != AV_CODEC_ID_H264)
+    else
         s->mb_height = (s->height + 15) / 16;
 
     if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
@@ -978,12 +1062,17 @@
         }
     }
 
-    s->picture_count = MAX_PICTURE_COUNT * FFMAX(1, s->avctx->thread_count);
     FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
-                      s->picture_count * sizeof(Picture), fail);
-    for (i = 0; i < s->picture_count; i++) {
+                      MAX_PICTURE_COUNT * sizeof(Picture), fail);
+    for (i = 0; i < MAX_PICTURE_COUNT; i++) {
         avcodec_get_frame_defaults(&s->picture[i].f);
     }
+    memset(&s->next_picture, 0, sizeof(s->next_picture));
+    memset(&s->last_picture, 0, sizeof(s->last_picture));
+    memset(&s->current_picture, 0, sizeof(s->current_picture));
+    avcodec_get_frame_defaults(&s->next_picture.f);
+    avcodec_get_frame_defaults(&s->last_picture.f);
+    avcodec_get_frame_defaults(&s->current_picture.f);
 
         if (init_context_frame(s))
             goto fail;
@@ -1076,9 +1165,6 @@
 
     s->linesize = s->uvlinesize = 0;
 
-    for (i = 0; i < 3; i++)
-        av_freep(&s->visualization_buffer[i]);
-
     return 0;
 }
 
@@ -1096,10 +1182,11 @@
     } else
         free_duplicate_context(s);
 
-    free_context_frame(s);
+    if ((err = free_context_frame(s)) < 0)
+        return err;
 
     if (s->picture)
-        for (i = 0; i < s->picture_count; i++) {
+        for (i = 0; i < MAX_PICTURE_COUNT; i++) {
                 s->picture[i].needs_realloc = 1;
         }
 
@@ -1110,7 +1197,7 @@
     // init
     if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
         s->mb_height = (s->height + 31) / 32 * 2;
-    else if (s->codec_id != AV_CODEC_ID_H264)
+    else
         s->mb_height = (s->height + 15) / 16;
 
     if ((s->width || s->height) &&
@@ -1189,18 +1276,24 @@
     av_freep(&s->reordered_input_picture);
     av_freep(&s->dct_offset);
 
-    if (s->picture && !s->avctx->internal->is_copy) {
-        for (i = 0; i < s->picture_count; i++) {
-            free_picture(s, &s->picture[i]);
+    if (s->picture) {
+        for (i = 0; i < MAX_PICTURE_COUNT; i++) {
+            free_picture_tables(&s->picture[i]);
+            ff_mpeg_unref_picture(s, &s->picture[i]);
         }
     }
     av_freep(&s->picture);
+    free_picture_tables(&s->last_picture);
+    ff_mpeg_unref_picture(s, &s->last_picture);
+    free_picture_tables(&s->current_picture);
+    ff_mpeg_unref_picture(s, &s->current_picture);
+    free_picture_tables(&s->next_picture);
+    ff_mpeg_unref_picture(s, &s->next_picture);
+    free_picture_tables(&s->new_picture);
+    ff_mpeg_unref_picture(s, &s->new_picture);
 
     free_context_frame(s);
 
-    if (!(s->avctx->active_thread_type & FF_THREAD_FRAME))
-        avcodec_default_free_buffers(s->avctx);
-
     s->context_initialized      = 0;
     s->last_picture_ptr         =
     s->next_picture_ptr         =
@@ -1305,27 +1398,22 @@
     int i;
 
     /* release non reference frames */
-    for (i = 0; i < s->picture_count; i++) {
-        if (s->picture[i].f.data[0] && !s->picture[i].f.reference &&
-            (!s->picture[i].owner2 || s->picture[i].owner2 == s) &&
-            (remove_current || &s->picture[i] !=  s->current_picture_ptr)
-            /* && s->picture[i].type!= FF_BUFFER_TYPE_SHARED */) {
-            free_frame_buffer(s, &s->picture[i]);
+    for (i = 0; i < MAX_PICTURE_COUNT; i++) {
+        if (!s->picture[i].reference &&
+            (remove_current || &s->picture[i] !=  s->current_picture_ptr)) {
+            ff_mpeg_unref_picture(s, &s->picture[i]);
         }
     }
 }
 
 static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
 {
-    if (   (s->avctx->active_thread_type & FF_THREAD_FRAME)
-        && pic->f.qscale_table //check if the frame has anything allocated
-        && pic->period_since_free < s->avctx->thread_count)
+    if (pic == s->last_picture_ptr)
         return 0;
     if (pic->f.data[0] == NULL)
         return 1;
-    if (pic->needs_realloc && !(pic->f.reference & DELAYED_PIC_REF))
-        if (!pic->owner2 || pic->owner2 == s)
-            return 1;
+    if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
+        return 1;
     return 0;
 }
 
@@ -1334,16 +1422,12 @@
     int i;
 
     if (shared) {
-        for (i = s->picture_range_start; i < s->picture_range_end; i++) {
-            if (s->picture[i].f.data[0] == NULL && s->picture[i].f.type == 0)
+        for (i = 0; i < MAX_PICTURE_COUNT; i++) {
+            if (s->picture[i].f.data[0] == NULL && &s->picture[i] != s->last_picture_ptr)
                 return i;
         }
     } else {
-        for (i = s->picture_range_start; i < s->picture_range_end; i++) {
-            if (pic_is_unused(s, &s->picture[i]) && s->picture[i].f.type != 0)
-                return i; // FIXME
-        }
-        for (i = s->picture_range_start; i < s->picture_range_end; i++) {
+        for (i = 0; i < MAX_PICTURE_COUNT; i++) {
             if (pic_is_unused(s, &s->picture[i]))
                 return i;
         }
@@ -1370,10 +1454,11 @@
 {
     int ret = find_unused_picture(s, shared);
 
-    if (ret >= 0 && ret < s->picture_range_end) {
+    if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
         if (s->picture[ret].needs_realloc) {
             s->picture[ret].needs_realloc = 0;
-            free_picture(s, &s->picture[ret]);
+            free_picture_tables(&s->picture[ret]);
+            ff_mpeg_unref_picture(s, &s->picture[ret]);
             avcodec_get_frame_defaults(&s->picture[ret].f);
         }
     }
@@ -1407,7 +1492,7 @@
  */
 int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
 {
-    int i;
+    int i, ret;
     Picture *pic;
     s->mb_skipped = 0;
 
@@ -1417,27 +1502,23 @@
     }
 
     /* mark & release old frames */
-    if (s->out_format != FMT_H264 || s->codec_id == AV_CODEC_ID_SVQ3) {
-        if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
-            s->last_picture_ptr != s->next_picture_ptr &&
-            s->last_picture_ptr->f.data[0]) {
-            if (s->last_picture_ptr->owner2 == s)
-                free_frame_buffer(s, s->last_picture_ptr);
-        }
+    if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
+        s->last_picture_ptr != s->next_picture_ptr &&
+        s->last_picture_ptr->f.data[0]) {
+        ff_mpeg_unref_picture(s, s->last_picture_ptr);
+    }
 
-        /* release forgotten pictures */
-        /* if (mpeg124/h263) */
-        if (!s->encoding) {
-            for (i = 0; i < s->picture_count; i++) {
-                if (s->picture[i].owner2 == s && s->picture[i].f.data[0] &&
-                    &s->picture[i] != s->last_picture_ptr &&
-                    &s->picture[i] != s->next_picture_ptr &&
-                    s->picture[i].f.reference && !s->picture[i].needs_realloc) {
-                    if (!(avctx->active_thread_type & FF_THREAD_FRAME))
-                        av_log(avctx, AV_LOG_ERROR,
-                               "releasing zombie picture\n");
-                    free_frame_buffer(s, &s->picture[i]);
-                }
+    /* release forgotten pictures */
+    /* if (mpeg124/h263) */
+    if (!s->encoding) {
+        for (i = 0; i < MAX_PICTURE_COUNT; i++) {
+            if (&s->picture[i] != s->last_picture_ptr &&
+                &s->picture[i] != s->next_picture_ptr &&
+                s->picture[i].reference && !s->picture[i].needs_realloc) {
+                if (!(avctx->active_thread_type & FF_THREAD_FRAME))
+                    av_log(avctx, AV_LOG_ERROR,
+                           "releasing zombie picture\n");
+                ff_mpeg_unref_picture(s, &s->picture[i]);
             }
         }
     }
@@ -1459,12 +1540,10 @@
             pic = &s->picture[i];
         }
 
-        pic->f.reference = 0;
+        pic->reference = 0;
         if (!s->droppable) {
-            if (s->codec_id == AV_CODEC_ID_H264)
-                pic->f.reference = s->picture_structure;
-            else if (s->pict_type != AV_PICTURE_TYPE_B)
-                pic->f.reference = 3;
+            if (s->pict_type != AV_PICTURE_TYPE_B)
+                pic->reference = 3;
         }
 
         pic->f.coded_picture_number = s->coded_picture_number++;
@@ -1491,7 +1570,10 @@
     //     s->current_picture_ptr->quality = s->new_picture_ptr->quality;
     s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
 
-    ff_copy_picture(&s->current_picture, s->current_picture_ptr);
+    ff_mpeg_unref_picture(s, &s->current_picture);
+    if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
+                                   s->current_picture_ptr)) < 0)
+        return ret;
 
     if (s->pict_type != AV_PICTURE_TYPE_B) {
         s->last_picture_ptr = s->next_picture_ptr;
@@ -1505,91 +1587,92 @@
             s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
             s->pict_type, s->droppable);
 
-    if (s->codec_id != AV_CODEC_ID_H264) {
-        if ((s->last_picture_ptr == NULL ||
-             s->last_picture_ptr->f.data[0] == NULL) &&
-            (s->pict_type != AV_PICTURE_TYPE_I ||
-             s->picture_structure != PICT_FRAME)) {
-            int h_chroma_shift, v_chroma_shift;
-            av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
-                                             &h_chroma_shift, &v_chroma_shift);
-            if (s->pict_type != AV_PICTURE_TYPE_I)
-                av_log(avctx, AV_LOG_ERROR,
-                       "warning: first frame is no keyframe\n");
-            else if (s->picture_structure != PICT_FRAME)
-                av_log(avctx, AV_LOG_INFO,
-                       "allocate dummy last picture for field based first keyframe\n");
+    if ((s->last_picture_ptr == NULL ||
+         s->last_picture_ptr->f.data[0] == NULL) &&
+        (s->pict_type != AV_PICTURE_TYPE_I ||
+         s->picture_structure != PICT_FRAME)) {
+        int h_chroma_shift, v_chroma_shift;
+        av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
+                                         &h_chroma_shift, &v_chroma_shift);
+        if (s->pict_type != AV_PICTURE_TYPE_I)
+            av_log(avctx, AV_LOG_ERROR,
+                   "warning: first frame is no keyframe\n");
+        else if (s->picture_structure != PICT_FRAME)
+            av_log(avctx, AV_LOG_INFO,
+                   "allocate dummy last picture for field based first keyframe\n");
 
-            /* Allocate a dummy frame */
-            i = ff_find_unused_picture(s, 0);
-            if (i < 0) {
-                av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
-                return i;
-            }
-            s->last_picture_ptr = &s->picture[i];
-            s->last_picture_ptr->f.key_frame = 0;
-            if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
-                s->last_picture_ptr = NULL;
-                return -1;
-            }
-
-            memset(s->last_picture_ptr->f.data[0], 0x80,
-                   avctx->height * s->last_picture_ptr->f.linesize[0]);
-            memset(s->last_picture_ptr->f.data[1], 0x80,
-                   (avctx->height >> v_chroma_shift) *
-                   s->last_picture_ptr->f.linesize[1]);
-            memset(s->last_picture_ptr->f.data[2], 0x80,
-                   (avctx->height >> v_chroma_shift) *
-                   s->last_picture_ptr->f.linesize[2]);
-
-            if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
-                for(i=0; i<avctx->height; i++)
-                    memset(s->last_picture_ptr->f.data[0] + s->last_picture_ptr->f.linesize[0]*i, 16, avctx->width);
-            }
-
-            ff_thread_report_progress(&s->last_picture_ptr->f, INT_MAX, 0);
-            ff_thread_report_progress(&s->last_picture_ptr->f, INT_MAX, 1);
-            s->last_picture_ptr->f.reference = 3;
+        /* Allocate a dummy frame */
+        i = ff_find_unused_picture(s, 0);
+        if (i < 0) {
+            av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
+            return i;
         }
-        if ((s->next_picture_ptr == NULL ||
-             s->next_picture_ptr->f.data[0] == NULL) &&
-            s->pict_type == AV_PICTURE_TYPE_B) {
-            /* Allocate a dummy frame */
-            i = ff_find_unused_picture(s, 0);
-            if (i < 0) {
-                av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
-                return i;
-            }
-            s->next_picture_ptr = &s->picture[i];
-            s->next_picture_ptr->f.key_frame = 0;
-            if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
-                s->next_picture_ptr = NULL;
-                return -1;
-            }
-            ff_thread_report_progress(&s->next_picture_ptr->f, INT_MAX, 0);
-            ff_thread_report_progress(&s->next_picture_ptr->f, INT_MAX, 1);
-            s->next_picture_ptr->f.reference = 3;
+        s->last_picture_ptr = &s->picture[i];
+        s->last_picture_ptr->f.key_frame = 0;
+        if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
+            s->last_picture_ptr = NULL;
+            return -1;
         }
+
+        memset(s->last_picture_ptr->f.data[0], 0x80,
+               avctx->height * s->last_picture_ptr->f.linesize[0]);
+        memset(s->last_picture_ptr->f.data[1], 0x80,
+               (avctx->height >> v_chroma_shift) *
+               s->last_picture_ptr->f.linesize[1]);
+        memset(s->last_picture_ptr->f.data[2], 0x80,
+               (avctx->height >> v_chroma_shift) *
+               s->last_picture_ptr->f.linesize[2]);
+
+        if(s->codec_id == AV_CODEC_ID_FLV1 || s->codec_id == AV_CODEC_ID_H263){
+            for(i=0; i<avctx->height; i++)
+            memset(s->last_picture_ptr->f.data[0] + s->last_picture_ptr->f.linesize[0]*i, 16, avctx->width);
+        }
+
+        ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
+        ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
+    }
+    if ((s->next_picture_ptr == NULL ||
+         s->next_picture_ptr->f.data[0] == NULL) &&
+        s->pict_type == AV_PICTURE_TYPE_B) {
+        /* Allocate a dummy frame */
+        i = ff_find_unused_picture(s, 0);
+        if (i < 0) {
+            av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
+            return i;
+        }
+        s->next_picture_ptr = &s->picture[i];
+        s->next_picture_ptr->f.key_frame = 0;
+        if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
+            s->next_picture_ptr = NULL;
+            return -1;
+        }
+        ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
+        ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
     }
 
+#if 0 // BUFREF-FIXME
     memset(s->last_picture.f.data, 0, sizeof(s->last_picture.f.data));
     memset(s->next_picture.f.data, 0, sizeof(s->next_picture.f.data));
-    if (s->last_picture_ptr)
-        ff_copy_picture(&s->last_picture, s->last_picture_ptr);
-    if (s->next_picture_ptr)
-        ff_copy_picture(&s->next_picture, s->next_picture_ptr);
-
-    if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME)) {
-        if (s->next_picture_ptr)
-            s->next_picture_ptr->owner2 = s;
-        if (s->last_picture_ptr)
-            s->last_picture_ptr->owner2 = s;
+#endif
+    if (s->last_picture_ptr) {
+        ff_mpeg_unref_picture(s, &s->last_picture);
+        if (s->last_picture_ptr->f.data[0] &&
+            (ret = ff_mpeg_ref_picture(s, &s->last_picture,
+                                       s->last_picture_ptr)) < 0)
+            return ret;
+    }
+    if (s->next_picture_ptr) {
+        ff_mpeg_unref_picture(s, &s->next_picture);
+        if (s->next_picture_ptr->f.data[0] &&
+            (ret = ff_mpeg_ref_picture(s, &s->next_picture,
+                                       s->next_picture_ptr)) < 0)
+            return ret;
     }
 
     assert(s->pict_type == AV_PICTURE_TYPE_I || (s->last_picture_ptr &&
                                                  s->last_picture_ptr->f.data[0]));
 
-    if (s->picture_structure!= PICT_FRAME && s->out_format != FMT_H264) {
+    if (s->picture_structure!= PICT_FRAME) {
         int i;
         for (i = 0; i < 4; i++) {
             if (s->picture_structure == PICT_BOTTOM_FIELD) {
@@ -1619,7 +1702,7 @@
     }
 
     if (s->dct_error_sum) {
-        assert(s->avctx->noise_reduction && s->encoding);
+        av_assert2(s->avctx->noise_reduction && s->encoding);
         update_noise_reduction(s);
     }
 
@@ -1642,7 +1725,7 @@
               !s->avctx->hwaccel &&
               !(s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
               s->unrestricted_mv &&
-              s->current_picture.f.reference &&
+              s->current_picture.reference &&
               !s->intra_only &&
               !(s->flags & CODEC_FLAG_EMU_EDGE) &&
               !s->avctx->lowres
@@ -1684,11 +1767,9 @@
 
     if (s->encoding) {
         /* release non-reference frames */
-        for (i = 0; i < s->picture_count; i++) {
-            if (s->picture[i].f.data[0] && !s->picture[i].f.reference
-                /* && s->picture[i].type != FF_BUFFER_TYPE_SHARED */) {
-                free_frame_buffer(s, &s->picture[i]);
-            }
+        for (i = 0; i < MAX_PICTURE_COUNT; i++) {
+            if (!s->picture[i].reference)
+                ff_mpeg_unref_picture(s, &s->picture[i]);
         }
     }
     // clear copies, to avoid confusion
@@ -1699,9 +1780,8 @@
 #endif
     s->avctx->coded_frame = &s->current_picture_ptr->f;
 
-    if (s->codec_id != AV_CODEC_ID_H264 && s->current_picture.f.reference) {
-        ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 0);
-    }
+    if (s->current_picture.reference)
+        ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
 }
 
 /**
@@ -1795,11 +1875,11 @@
 /**
  * Print debugging info for the given picture.
  */
-void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, uint8_t *mbskip_table,
-                         uint8_t *visualization_buffer[3], int *low_delay,
+void ff_print_debug_info2(AVCodecContext *avctx, Picture *p, AVFrame *pict, uint8_t *mbskip_table,
+                         int *low_delay,
                          int mb_width, int mb_height, int mb_stride, int quarter_sample)
 {
-    if (   avctx->hwaccel || !pict || !pict->mb_type
+    if (avctx->hwaccel || !p || !p->mb_type
         || (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU))
         return;
 
@@ -1819,10 +1899,10 @@
                 }
                 if (avctx->debug & FF_DEBUG_QP) {
                     av_log(avctx, AV_LOG_DEBUG, "%2d",
-                           pict->qscale_table[x + y * mb_stride]);
+                           p->qscale_table[x + y * mb_stride]);
                 }
                 if (avctx->debug & FF_DEBUG_MB_TYPE) {
-                    int mb_type = pict->mb_type[x + y * mb_stride];
+                    int mb_type = p->mb_type[x + y * mb_stride];
                     // Type & MV direction
                     if (IS_PCM(mb_type))
                         av_log(avctx, AV_LOG_DEBUG, "P");
@@ -1883,22 +1963,17 @@
         int h_chroma_shift, v_chroma_shift, block_height;
         const int width          = avctx->width;
         const int height         = avctx->height;
-        const int mv_sample_log2 = 4 - pict->motion_subsample_log2;
+        const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
         const int mv_stride      = (mb_width << mv_sample_log2) +
                                    (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
+
         *low_delay = 0; // needed to see the vectors without trashing the buffers
 
         avcodec_get_chroma_sub_sample(avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
 
-        for (i = 0; i < 3; i++) {
-            size_t size= (i == 0) ? pict->linesize[i] * FFALIGN(height, 16):
-                         pict->linesize[i] * FFALIGN(height, 16) >> v_chroma_shift;
-            visualization_buffer[i]= av_realloc(visualization_buffer[i], size);
-            memcpy(visualization_buffer[i], pict->data[i], size);
-            pict->data[i] = visualization_buffer[i];
-        }
-        pict->type   = FF_BUFFER_TYPE_COPY;
-        pict->opaque= NULL;
+        av_frame_make_writable(pict);
+
+        pict->opaque = NULL;
         ptr          = pict->data[0];
         block_height = 16 >> v_chroma_shift;
 
@@ -1906,7 +1981,7 @@
             int mb_x;
             for (mb_x = 0; mb_x < mb_width; mb_x++) {
                 const int mb_index = mb_x + mb_y * mb_stride;
-                if ((avctx->debug_mv) && pict->motion_val[0]) {
+                if ((avctx->debug_mv) && p->motion_val[0]) {
                     int type;
                     for (type = 0; type < 3; type++) {
                         int direction = 0;
@@ -1930,46 +2005,46 @@
                             direction = 1;
                             break;
                         }
-                        if (!USES_LIST(pict->mb_type[mb_index], direction))
+                        if (!USES_LIST(p->mb_type[mb_index], direction))
                             continue;
 
-                        if (IS_8X8(pict->mb_type[mb_index])) {
+                        if (IS_8X8(p->mb_type[mb_index])) {
                             int i;
                             for (i = 0; i < 4; i++) {
                                 int sx = mb_x * 16 + 4 + 8 * (i & 1);
                                 int sy = mb_y * 16 + 4 + 8 * (i >> 1);
                                 int xy = (mb_x * 2 + (i & 1) +
                                           (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
-                                int mx = (pict->motion_val[direction][xy][0] >> shift) + sx;
-                                int my = (pict->motion_val[direction][xy][1] >> shift) + sy;
+                                int mx = (p->motion_val[direction][xy][0] >> shift) + sx;
+                                int my = (p->motion_val[direction][xy][1] >> shift) + sy;
                                 draw_arrow(ptr, sx, sy, mx, my, width,
                                            height, pict->linesize[0], 100);
                             }
-                        } else if (IS_16X8(pict->mb_type[mb_index])) {
+                        } else if (IS_16X8(p->mb_type[mb_index])) {
                             int i;
                             for (i = 0; i < 2; i++) {
                                 int sx = mb_x * 16 + 8;
                                 int sy = mb_y * 16 + 4 + 8 * i;
                                 int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
-                                int mx = (pict->motion_val[direction][xy][0] >> shift);
-                                int my = (pict->motion_val[direction][xy][1] >> shift);
+                                int mx = (p->motion_val[direction][xy][0] >> shift);
+                                int my = (p->motion_val[direction][xy][1] >> shift);
 
-                                if (IS_INTERLACED(pict->mb_type[mb_index]))
+                                if (IS_INTERLACED(p->mb_type[mb_index]))
                                     my *= 2;
 
                             draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
                                        height, pict->linesize[0], 100);
                             }
-                        } else if (IS_8X16(pict->mb_type[mb_index])) {
+                        } else if (IS_8X16(p->mb_type[mb_index])) {
                             int i;
                             for (i = 0; i < 2; i++) {
                                 int sx = mb_x * 16 + 4 + 8 * i;
                                 int sy = mb_y * 16 + 8;
                                 int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
-                                int mx = pict->motion_val[direction][xy][0] >> shift;
-                                int my = pict->motion_val[direction][xy][1] >> shift;
+                                int mx = p->motion_val[direction][xy][0] >> shift;
+                                int my = p->motion_val[direction][xy][1] >> shift;
 
-                                if (IS_INTERLACED(pict->mb_type[mb_index]))
+                                if (IS_INTERLACED(p->mb_type[mb_index]))
                                     my *= 2;
 
                                 draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
@@ -1979,14 +2054,14 @@
                               int sx= mb_x * 16 + 8;
                               int sy= mb_y * 16 + 8;
                               int xy= (mb_x + mb_y * mv_stride) << mv_sample_log2;
-                              int mx= (pict->motion_val[direction][xy][0]>>shift) + sx;
-                              int my= (pict->motion_val[direction][xy][1]>>shift) + sy;
+                              int mx= (p->motion_val[direction][xy][0]>>shift) + sx;
+                              int my= (p->motion_val[direction][xy][1]>>shift) + sy;
                               draw_arrow(ptr, sx, sy, mx, my, width, height, pict->linesize[0], 100);
                         }
                     }
                 }
                 if ((avctx->debug & FF_DEBUG_VIS_QP)) {
-                    uint64_t c = (pict->qscale_table[mb_index] * 128 / 31) *
+                    uint64_t c = (p->qscale_table[mb_index] * 128 / 31) *
                                  0x0101010101010101ULL;
                     int y;
                     for (y = 0; y < block_height; y++) {
@@ -1999,8 +2074,8 @@
                     }
                 }
                 if ((avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
-                    pict->motion_val[0]) {
-                    int mb_type = pict->mb_type[mb_index];
+                    p->motion_val[0]) {
+                    int mb_type = p->mb_type[mb_index];
                     uint64_t u,v;
                     int y;
 #define COLOR(theta, r) \
@@ -2064,7 +2139,7 @@
                             int xy = (mb_x * 2 + (i & 1) +
                                      (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
                             // FIXME bidir
-                            int32_t *mv = (int32_t *) &pict->motion_val[0][xy];
+                            int32_t *mv = (int32_t *) &p->motion_val[0][xy];
                             if (mv[0] != mv[dm] ||
                                 mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
                                 for (y = 0; y < 8; y++)
@@ -2086,10 +2161,22 @@
     }
 }
 
-void ff_print_debug_info(MpegEncContext *s, AVFrame *pict)
+void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
 {
-    ff_print_debug_info2(s->avctx, pict, s->mbskip_table, s->visualization_buffer, &s->low_delay,
-                             s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
+    ff_print_debug_info2(s->avctx, p, pict, s->mbskip_table, &s->low_delay,
+                         s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
+}
+
+int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
+{
+    AVBufferRef *ref = av_buffer_ref(p->qscale_table_buf);
+    int offset = 2*s->mb_stride + 1;
+    if(!ref)
+        return AVERROR(ENOMEM);
+    av_assert0(ref->size >= offset + s->mb_stride * ((f->height+15)/16));
+    ref->size -= offset;
+    ref->data += offset;
+    return av_frame_set_qp_table(f, ref, s->mb_stride, qp_type);
 }
 
 static inline int hpel_motion_lowres(MpegEncContext *s,
@@ -2586,20 +2673,18 @@
     }
 
     if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
-       /* save DCT coefficients */
+       /* print DCT coefficients */
        int i,j;
-       int16_t *dct = &s->current_picture.f.dct_coeff[mb_xy * 64 * 6];
        av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
        for(i=0; i<6; i++){
            for(j=0; j<64; j++){
-               *dct++ = block[i][s->dsp.idct_permutation[j]];
-               av_log(s->avctx, AV_LOG_DEBUG, "%5d", dct[-1]);
+               av_log(s->avctx, AV_LOG_DEBUG, "%5d", block[i][s->dsp.idct_permutation[j]]);
            }
            av_log(s->avctx, AV_LOG_DEBUG, "\n");
        }
     }
 
-    s->current_picture.f.qscale_table[mb_xy] = s->qscale;
+    s->current_picture.qscale_table[mb_xy] = s->qscale;
 
     /* update DC predictors for P macroblocks */
     if (!s->mb_intra) {
@@ -2634,7 +2719,7 @@
                 s->mb_skipped= 0;
                 av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
                 *mbskip_ptr = 1;
-            } else if(!s->current_picture.f.reference) {
+            } else if(!s->current_picture.reference) {
                 *mbskip_ptr = 1;
             } else{
                 *mbskip_ptr = 0; /* not skipped */
@@ -2661,12 +2746,12 @@
 
                 if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
                     if (s->mv_dir & MV_DIR_FORWARD) {
-                        ff_thread_await_progress(&s->last_picture_ptr->f,
+                        ff_thread_await_progress(&s->last_picture_ptr->tf,
                                                  ff_MPV_lowest_referenced_row(s, 0),
                                                  0);
                     }
                     if (s->mv_dir & MV_DIR_BACKWARD) {
-                        ff_thread_await_progress(&s->next_picture_ptr->f,
+                        ff_thread_await_progress(&s->next_picture_ptr->tf,
                                                  ff_MPV_lowest_referenced_row(s, 1),
                                                  0);
                     }
@@ -2685,13 +2770,13 @@
                 }else{
                     op_qpix= s->me.qpel_put;
                     if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
-                        op_pix = s->dsp.put_pixels_tab;
+                        op_pix = s->hdsp.put_pixels_tab;
                     }else{
-                        op_pix = s->dsp.put_no_rnd_pixels_tab;
+                        op_pix = s->hdsp.put_no_rnd_pixels_tab;
                     }
                     if (s->mv_dir & MV_DIR_FORWARD) {
                         ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
-                        op_pix = s->dsp.avg_pixels_tab;
+                        op_pix = s->hdsp.avg_pixels_tab;
                         op_qpix= s->me.qpel_avg;
                     }
                     if (s->mv_dir & MV_DIR_BACKWARD) {
@@ -2812,9 +2897,9 @@
         }
 skip_idct:
         if(!readable){
-            s->dsp.put_pixels_tab[0][0](s->dest[0], dest_y ,   linesize,16);
-            s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
-            s->dsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
+            s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y ,   linesize,16);
+            s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
+            s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
         }
     }
 }
@@ -2850,7 +2935,7 @@
     if (!avctx->hwaccel &&
         !(avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) &&
         draw_edges &&
-        cur->f.reference &&
+        cur->reference &&
         !(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
         int *linesize = cur->f.linesize;
         int sides = 0, edge_h;
@@ -2889,8 +2974,7 @@
             return;
 
         if (cur->f.pict_type == AV_PICTURE_TYPE_B &&
-            picture_structure == PICT_FRAME    &&
-            avctx->codec_id != AV_CODEC_ID_H264  &&
+            picture_structure == PICT_FRAME &&
             avctx->codec_id != AV_CODEC_ID_SVQ3) {
             for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
                 offset[i] = 0;
@@ -2986,14 +3070,14 @@
     if(s==NULL || s->picture==NULL)
         return;
 
-    for(i=0; i<s->picture_count; i++){
-       if (s->picture[i].f.data[0] &&
-           (s->picture[i].f.type == FF_BUFFER_TYPE_INTERNAL ||
-            s->picture[i].f.type == FF_BUFFER_TYPE_USER))
-        free_frame_buffer(s, &s->picture[i]);
-    }
+    for (i = 0; i < MAX_PICTURE_COUNT; i++)
+        ff_mpeg_unref_picture(s, &s->picture[i]);
     s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
 
+    ff_mpeg_unref_picture(s, &s->current_picture);
+    ff_mpeg_unref_picture(s, &s->last_picture);
+    ff_mpeg_unref_picture(s, &s->next_picture);
+
     s->mb_x= s->mb_y= 0;
     s->closed_gop= 0;
 
@@ -3160,7 +3244,7 @@
     int i, level, qmul, qadd;
     int nCoeffs;
 
-    assert(s->block_last_index[n]>=0);
+    av_assert2(s->block_last_index[n]>=0);
 
     qmul = qscale << 1;
 
@@ -3194,7 +3278,7 @@
     int i, level, qmul, qadd;
     int nCoeffs;
 
-    assert(s->block_last_index[n]>=0);
+    av_assert2(s->block_last_index[n]>=0);
 
     qadd = (qscale - 1) | 1;
     qmul = qscale << 1;
@@ -3234,9 +3318,10 @@
 void ff_MPV_report_decode_progress(MpegEncContext *s)
 {
     if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->er.error_occurred)
-        ff_thread_report_progress(&s->current_picture_ptr->f, s->mb_y, 0);
+        ff_thread_report_progress(&s->current_picture_ptr->tf, s->mb_y, 0);
 }
 
+#if CONFIG_ERROR_RESILIENCE
 void ff_mpeg_er_frame_start(MpegEncContext *s)
 {
     ERContext *er = &s->er;
@@ -3252,3 +3337,4 @@
 
     ff_er_frame_start(er);
 }
+#endif /* CONFIG_ERROR_RESILIENCE */
diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h
index 4d6487d..30255f5 100644
--- a/libavcodec/mpegvideo.h
+++ b/libavcodec/mpegvideo.h
@@ -33,11 +33,13 @@
 #include "error_resilience.h"
 #include "get_bits.h"
 #include "h264chroma.h"
+#include "hpeldsp.h"
 #include "put_bits.h"
 #include "ratecontrol.h"
 #include "parser.h"
 #include "mpeg12data.h"
 #include "rl.h"
+#include "thread.h"
 #include "videodsp.h"
 
 #include "libavutil/opt.h"
@@ -50,7 +52,6 @@
     FMT_H261,
     FMT_H263,
     FMT_MJPEG,
-    FMT_H264,
 };
 
 #define MPEG_BUF_SIZE (16 * 1024)
@@ -95,10 +96,41 @@
  */
 typedef struct Picture{
     struct AVFrame f;
+    ThreadFrame tf;
 
-    int8_t *qscale_table_base;
-    int16_t (*motion_val_base[2])[2];
-    uint32_t *mb_type_base;
+    AVBufferRef *qscale_table_buf;
+    int8_t *qscale_table;
+
+    AVBufferRef *motion_val_buf[2];
+    int16_t (*motion_val[2])[2];
+
+    AVBufferRef *mb_type_buf;
+    uint32_t *mb_type;
+
+    AVBufferRef *mbskip_table_buf;
+    uint8_t *mbskip_table;
+
+    AVBufferRef *ref_index_buf[2];
+    int8_t *ref_index[2];
+
+    AVBufferRef *mb_var_buf;
+    uint16_t *mb_var;           ///< Table for MB variances
+
+    AVBufferRef *mc_mb_var_buf;
+    uint16_t *mc_mb_var;        ///< Table for motion compensated MB variances
+
+    int alloc_mb_width;         ///< mb_width used to allocate tables
+    int alloc_mb_height;        ///< mb_height used to allocate tables
+
+    AVBufferRef *mb_mean_buf;
+    uint8_t *mb_mean;           ///< Table for MB luminance
+
+    AVBufferRef *hwaccel_priv_buf;
+    /**
+     * hardware accelerator private data
+     */
+    void *hwaccel_picture_private;
+
 #define MB_TYPE_INTRA MB_TYPE_INTRA4x4 //default mb_type if there is just one type
 #define IS_INTRA4x4(a)   ((a)&MB_TYPE_INTRA4x4)
 #define IS_INTRA16x16(a) ((a)&MB_TYPE_INTRA16x16)
@@ -139,13 +171,12 @@
 
     int mb_var_sum;             ///< sum of MB variance for current frame
     int mc_mb_var_sum;          ///< motion compensated MB variance for current frame
-    uint16_t *mb_var;           ///< Table for MB variances
-    uint16_t *mc_mb_var;        ///< Table for motion compensated MB variances
-    uint8_t *mb_mean;           ///< Table for MB luminance
+
     int b_frame_score;          /* */
-    void *owner2;               ///< pointer to the context that allocated this picture
     int needs_realloc;          ///< Picture needs to be reallocated (eg due to a frame size change)
-    int period_since_free;      ///< "cycles" since this Picture has been freed
+
+    int reference;
+    int shared;
 } Picture;
 
 /**
@@ -314,9 +345,6 @@
     Picture *last_picture_ptr;     ///< pointer to the previous picture.
     Picture *next_picture_ptr;     ///< pointer to the next picture (for bidir pred)
     Picture *current_picture_ptr;  ///< pointer to the current picture
-    int picture_count;             ///< number of allocated pictures (MAX_PICTURE_COUNT * avctx->thread_count)
-    int picture_range_start, picture_range_end; ///< the part of picture that this context can allocate in
-    uint8_t *visualization_buffer[3]; ///< temporary buffer vor MV visualization
     int last_dc[3];                ///< last DC values for MPEG1
     int16_t *dc_val_base;
     int16_t *dc_val[3];            ///< used for mpeg4 DC prediction, all 3 arrays must be continuous
@@ -362,6 +390,7 @@
 
     DSPContext dsp;             ///< pointers for accelerated dsp functions
     H264ChromaContext h264chroma;
+    HpelDSPContext hdsp;
     VideoDSPContext vdsp;
     int f_code;                 ///< forward MV resolution
     int b_code;                 ///< backward MV resolution for B Frames (mpeg4)
@@ -715,7 +744,7 @@
 
 #define REBASE_PICTURE(pic, new_ctx, old_ctx)             \
     ((pic && pic >= old_ctx->picture &&                   \
-      pic < old_ctx->picture + old_ctx->picture_count) ?  \
+      pic < old_ctx->picture + MAX_PICTURE_COUNT) ?  \
         &new_ctx->picture[pic - old_ctx->picture] : NULL)
 
 /* mpegvideo_enc common options */
@@ -780,7 +809,14 @@
                         int v_edge_pos, int h_edge_pos);
 void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h);
 void ff_mpeg_flush(AVCodecContext *avctx);
-void ff_print_debug_info(MpegEncContext *s, AVFrame *pict);
+
+void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict);
+void ff_print_debug_info2(AVCodecContext *avctx, Picture *p, AVFrame *pict, uint8_t *mbskip_table,
+                         int *low_delay,
+                         int mb_width, int mb_height, int mb_stride, int quarter_sample);
+
+int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type);
+
 void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix);
 void ff_release_unused_pictures(MpegEncContext *s, int remove_current);
 int ff_find_unused_picture(MpegEncContext *s, int shared);
@@ -789,7 +825,6 @@
 int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir);
 void ff_MPV_report_decode_progress(MpegEncContext *s);
 int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src);
-const uint8_t *avpriv_mpv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state);
 void ff_set_qscale(MpegEncContext * s, int qscale);
 
 void ff_mpeg_er_frame_start(MpegEncContext *s);
@@ -801,7 +836,6 @@
 int ff_dct_quantize_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow);
 
 void ff_init_block_index(MpegEncContext *s);
-void ff_copy_picture(Picture *dst, Picture *src);
 
 void ff_MPV_motion(MpegEncContext *s,
                    uint8_t *dest_y, uint8_t *dest_cb,
@@ -882,24 +916,11 @@
                         int motion_x, int motion_y);
 void ff_mpeg1_encode_init(MpegEncContext *s);
 void ff_mpeg1_encode_slice_header(MpegEncContext *s);
-void ff_mpeg1_clean_buffers(MpegEncContext *s);
-int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s);
 
 extern const uint8_t ff_aic_dc_scale_table[32];
 extern const uint8_t ff_h263_chroma_qscale_table[32];
 extern const uint8_t ff_h263_loop_filter_strength[32];
 
-/* h261.c */
-void ff_h261_loop_filter(MpegEncContext *s);
-void ff_h261_reorder_mb_index(MpegEncContext* s);
-void ff_h261_encode_mb(MpegEncContext *s,
-                    int16_t block[6][64],
-                    int motion_x, int motion_y);
-void ff_h261_encode_picture_header(MpegEncContext * s, int picture_number);
-void ff_h261_encode_init(MpegEncContext *s);
-int ff_h261_get_picture_format(int width, int height);
-
-
 /* rv10.c */
 void ff_rv10_encode_picture_header(MpegEncContext *s, int picture_number);
 int ff_rv_decode_dc(MpegEncContext *s, int n);
@@ -928,4 +949,8 @@
                        int16_t block[6][64],
                        int motion_x, int motion_y);
 
+int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src);
+void ff_mpeg_unref_picture(MpegEncContext *s, Picture *picture);
+
+
 #endif /* AVCODEC_MPEGVIDEO_H */
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index a0e7a54..5858865 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -35,7 +35,9 @@
 #include "avcodec.h"
 #include "dct.h"
 #include "dsputil.h"
+#include "mpeg12.h"
 #include "mpegvideo.h"
+#include "h261.h"
 #include "h263.h"
 #include "mathops.h"
 #include "mjpegenc.h"
@@ -174,7 +176,7 @@
  */
 void ff_init_qscale_tab(MpegEncContext *s)
 {
-    int8_t * const qscale_table = s->current_picture.f.qscale_table;
+    int8_t * const qscale_table = s->current_picture.qscale_table;
     int i;
 
     for (i = 0; i < s->mb_num; i++) {
@@ -188,8 +190,6 @@
 static void copy_picture_attributes(MpegEncContext *s, AVFrame *dst,
                                     const AVFrame *src)
 {
-    int i;
-
     dst->pict_type              = src->pict_type;
     dst->quality                = src->quality;
     dst->coded_picture_number   = src->coded_picture_number;
@@ -198,38 +198,6 @@
     dst->pts                    = src->pts;
     dst->interlaced_frame       = src->interlaced_frame;
     dst->top_field_first        = src->top_field_first;
-
-    if (s->avctx->me_threshold) {
-        if (!src->motion_val[0])
-            av_log(s->avctx, AV_LOG_ERROR, "AVFrame.motion_val not set!\n");
-        if (!src->mb_type)
-            av_log(s->avctx, AV_LOG_ERROR, "AVFrame.mb_type not set!\n");
-        if (!src->ref_index[0])
-            av_log(s->avctx, AV_LOG_ERROR, "AVFrame.ref_index not set!\n");
-        if (src->motion_subsample_log2 != dst->motion_subsample_log2)
-            av_log(s->avctx, AV_LOG_ERROR,
-                   "AVFrame.motion_subsample_log2 doesn't match! (%d!=%d)\n",
-                   src->motion_subsample_log2, dst->motion_subsample_log2);
-
-        memcpy(dst->mb_type, src->mb_type,
-               s->mb_stride * s->mb_height * sizeof(dst->mb_type[0]));
-
-        for (i = 0; i < 2; i++) {
-            int stride = ((16 * s->mb_width ) >>
-                          src->motion_subsample_log2) + 1;
-            int height = ((16 * s->mb_height) >> src->motion_subsample_log2);
-
-            if (src->motion_val[i] &&
-                src->motion_val[i] != dst->motion_val[i]) {
-                memcpy(dst->motion_val[i], src->motion_val[i],
-                       2 * stride * height * sizeof(int16_t));
-            }
-            if (src->ref_index[i] && src->ref_index[i] != dst->ref_index[i]) {
-                memcpy(dst->ref_index[i], src->ref_index[i],
-                       s->mb_stride * 4 * s->mb_height * sizeof(int8_t));
-            }
-        }
-    }
 }
 
 static void update_duplicate_context_after_me(MpegEncContext *dst,
@@ -367,12 +335,6 @@
     s->flags2       = avctx->flags2;
     s->max_b_frames = avctx->max_b_frames;
     s->codec_id     = avctx->codec->id;
-#if FF_API_MPV_GLOBAL_OPTS
-    if (avctx->luma_elim_threshold)
-        s->luma_elim_threshold   = avctx->luma_elim_threshold;
-    if (avctx->chroma_elim_threshold)
-        s->chroma_elim_threshold = avctx->chroma_elim_threshold;
-#endif
     s->strict_std_compliance = avctx->strict_std_compliance;
     s->quarter_sample     = (avctx->flags & CODEC_FLAG_QPEL) != 0;
     s->mpeg_quant         = avctx->mpeg_quant;
@@ -392,11 +354,6 @@
     /* Fixed QSCALE */
     s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
 
-#if FF_API_MPV_GLOBAL_OPTS
-    if (s->flags & CODEC_FLAG_QP_RD)
-        s->mpv_flags |= FF_MPV_FLAG_QP_RD;
-#endif
-
     s->adaptive_quant = (s->avctx->lumi_masking ||
                          s->avctx->dark_masking ||
                          s->avctx->temporal_cplx_masking ||
@@ -589,11 +546,6 @@
         return -1;
     }
 
-#if FF_API_MPV_GLOBAL_OPTS
-    if (s->flags & CODEC_FLAG_CBP_RD)
-        s->mpv_flags |= FF_MPV_FLAG_CBP_RD;
-#endif
-
     if ((s->mpv_flags & FF_MPV_FLAG_CBP_RD) && !avctx->trellis) {
         av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
         return -1;
@@ -664,11 +616,6 @@
     }
 
     i = (INT_MAX / 2 + 128) >> 8;
-    if (avctx->me_threshold >= i) {
-        av_log(avctx, AV_LOG_ERROR, "me_threshold too large, max is %d\n",
-               i - 1);
-        return -1;
-    }
     if (avctx->mb_threshold >= i) {
         av_log(avctx, AV_LOG_ERROR, "mb_threshold too large, max is %d\n",
                i - 1);
@@ -719,15 +666,6 @@
     }
     s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
 
-#if FF_API_MPV_GLOBAL_OPTS
-    if (avctx->flags2 & CODEC_FLAG2_SKIP_RD)
-        s->mpv_flags |= FF_MPV_FLAG_SKIP_RD;
-    if (avctx->flags2 & CODEC_FLAG2_STRICT_GOP)
-        s->mpv_flags |= FF_MPV_FLAG_STRICT_GOP;
-    if (avctx->quantizer_noise_shaping)
-        s->quantizer_noise_shaping = avctx->quantizer_noise_shaping;
-#endif
-
     switch (avctx->codec->id) {
     case AV_CODEC_ID_MPEG1VIDEO:
         s->out_format = FMT_MPEG1;
@@ -1009,9 +947,9 @@
 
 static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
 {
-    AVFrame *pic = NULL;
+    Picture *pic = NULL;
     int64_t pts;
-    int i, display_picture_number = 0;
+    int i, display_picture_number = 0, ret;
     const int encoding_delay = s->max_b_frames ? s->max_b_frames :
                                                  (s->low_delay ? 0 : 1);
     int direct = 1;
@@ -1050,7 +988,7 @@
     }
 
     if (pic_arg) {
-        if (encoding_delay && !(s->flags & CODEC_FLAG_INPUT_PRESERVED))
+        if (!pic_arg->buf[0])
             direct = 0;
         if (pic_arg->linesize[0] != s->linesize)
             direct = 0;
@@ -1067,14 +1005,12 @@
             if (i < 0)
                 return i;
 
-            pic = &s->picture[i].f;
+            pic = &s->picture[i];
             pic->reference = 3;
 
-            for (i = 0; i < 4; i++) {
-                pic->data[i]     = pic_arg->data[i];
-                pic->linesize[i] = pic_arg->linesize[i];
-            }
-            if (ff_alloc_picture(s, (Picture *) pic, 1) < 0) {
+            if ((ret = av_frame_ref(&pic->f, pic_arg)) < 0)
+                return ret;
+            if (ff_alloc_picture(s, pic, 1) < 0) {
                 return -1;
             }
         } else {
@@ -1082,16 +1018,16 @@
             if (i < 0)
                 return i;
 
-            pic = &s->picture[i].f;
+            pic = &s->picture[i];
             pic->reference = 3;
 
-            if (ff_alloc_picture(s, (Picture *) pic, 0) < 0) {
+            if (ff_alloc_picture(s, pic, 0) < 0) {
                 return -1;
             }
 
-            if (pic->data[0] + INPLACE_OFFSET == pic_arg->data[0] &&
-                pic->data[1] + INPLACE_OFFSET == pic_arg->data[1] &&
-                pic->data[2] + INPLACE_OFFSET == pic_arg->data[2]) {
+            if (pic->f.data[0] + INPLACE_OFFSET == pic_arg->data[0] &&
+                pic->f.data[1] + INPLACE_OFFSET == pic_arg->data[1] &&
+                pic->f.data[2] + INPLACE_OFFSET == pic_arg->data[2]) {
                 // empty
             } else {
                 int h_chroma_shift, v_chroma_shift;
@@ -1107,7 +1043,7 @@
                     int w = s->width  >> h_shift;
                     int h = s->height >> v_shift;
                     uint8_t *src = pic_arg->data[i];
-                    uint8_t *dst = pic->data[i];
+                    uint8_t *dst = pic->f.data[i];
 
                     if (s->codec_id == AV_CODEC_ID_AMV && !(s->avctx->flags & CODEC_FLAG_EMU_EDGE)) {
                         h = ((s->height + 15)/16*16) >> v_shift;
@@ -1137,9 +1073,9 @@
                 }
             }
         }
-        copy_picture_attributes(s, pic, pic_arg);
-        pic->display_picture_number = display_picture_number;
-        pic->pts = pts; // we set this here to avoid modifiying pic_arg
+        copy_picture_attributes(s, &pic->f, pic_arg);
+        pic->f.display_picture_number = display_picture_number;
+        pic->f.pts = pts; // we set this here to avoid modifiying pic_arg
     }
 
     /* shift buffer entries */
@@ -1162,7 +1098,7 @@
         const int bw = plane ? 1 : 2;
         for (y = 0; y < s->mb_height * bw; y++) {
             for (x = 0; x < s->mb_width * bw; x++) {
-                int off = p->f.type == FF_BUFFER_TYPE_SHARED ? 0 : 16;
+                int off = p->shared ? 0 : 16;
                 uint8_t *dptr = p->f.data[plane] + 8 * (x + y * stride) + off;
                 uint8_t *rptr = ref->f.data[plane] + 8 * (x + y * stride);
                 int v   = s->dsp.frame_skip_cmp[1](s, dptr, rptr, stride, 8);
@@ -1258,7 +1194,7 @@
         if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
             pre_input = *pre_input_ptr;
 
-            if (pre_input.f.type != FF_BUFFER_TYPE_SHARED && i) {
+            if (!pre_input.shared && i) {
                 pre_input.f.data[0] += INPLACE_OFFSET;
                 pre_input.f.data[1] += INPLACE_OFFSET;
                 pre_input.f.data[2] += INPLACE_OFFSET;
@@ -1329,7 +1265,7 @@
 
 static int select_input_picture(MpegEncContext *s)
 {
-    int i;
+    int i, ret;
 
     for (i = 1; i < MAX_PICTURE_COUNT; i++)
         s->reordered_input_picture[i - 1] = s->reordered_input_picture[i];
@@ -1350,17 +1286,7 @@
                 if (s->picture_in_gop_number < s->gop_size &&
                     skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
                     // FIXME check that te gop check above is +-1 correct
-                    if (s->input_picture[0]->f.type == FF_BUFFER_TYPE_SHARED) {
-                        for (i = 0; i < 4; i++)
-                            s->input_picture[0]->f.data[i] = NULL;
-                        s->input_picture[0]->f.type = 0;
-                    } else {
-                        assert(s->input_picture[0]->f.type == FF_BUFFER_TYPE_USER ||
-                               s->input_picture[0]->f.type == FF_BUFFER_TYPE_INTERNAL);
-
-                        s->avctx->release_buffer(s->avctx,
-                                                 &s->input_picture[0]->f);
-                    }
+                    av_frame_unref(&s->input_picture[0]->f);
 
                     emms_c();
                     ff_vbv_update(s, 0);
@@ -1464,14 +1390,15 @@
     }
 no_output_pic:
     if (s->reordered_input_picture[0]) {
-        s->reordered_input_picture[0]->f.reference =
+        s->reordered_input_picture[0]->reference =
            s->reordered_input_picture[0]->f.pict_type !=
                AV_PICTURE_TYPE_B ? 3 : 0;
 
-        ff_copy_picture(&s->new_picture, s->reordered_input_picture[0]);
+        ff_mpeg_unref_picture(s, &s->new_picture);
+        if ((ret = ff_mpeg_ref_picture(s, &s->new_picture, s->reordered_input_picture[0])))
+            return ret;
 
-        if (s->reordered_input_picture[0]->f.type == FF_BUFFER_TYPE_SHARED ||
-            s->avctx->rc_buffer_size) {
+        if (s->reordered_input_picture[0]->shared || s->avctx->rc_buffer_size) {
             // input is a shared pix, so we can't modifiy it -> alloc a new
             // one & ensure that the shared one is reuseable
 
@@ -1481,41 +1408,34 @@
                 return i;
             pic = &s->picture[i];
 
-            pic->f.reference = s->reordered_input_picture[0]->f.reference;
+            pic->reference = s->reordered_input_picture[0]->reference;
             if (ff_alloc_picture(s, pic, 0) < 0) {
                 return -1;
             }
 
-            /* mark us unused / free shared pic */
-            if (s->reordered_input_picture[0]->f.type == FF_BUFFER_TYPE_INTERNAL)
-                s->avctx->release_buffer(s->avctx,
-                                         &s->reordered_input_picture[0]->f);
-            for (i = 0; i < 4; i++)
-                s->reordered_input_picture[0]->f.data[i] = NULL;
-            s->reordered_input_picture[0]->f.type = 0;
-
             copy_picture_attributes(s, &pic->f,
                                     &s->reordered_input_picture[0]->f);
 
+            /* mark us unused / free shared pic */
+            av_frame_unref(&s->reordered_input_picture[0]->f);
+            s->reordered_input_picture[0]->shared = 0;
+
             s->current_picture_ptr = pic;
         } else {
             // input is not a shared pix -> reuse buffer for current_pix
-
-            assert(s->reordered_input_picture[0]->f.type ==
-                       FF_BUFFER_TYPE_USER ||
-                   s->reordered_input_picture[0]->f.type ==
-                       FF_BUFFER_TYPE_INTERNAL);
-
             s->current_picture_ptr = s->reordered_input_picture[0];
             for (i = 0; i < 4; i++) {
                 s->new_picture.f.data[i] += INPLACE_OFFSET;
             }
         }
-        ff_copy_picture(&s->current_picture, s->current_picture_ptr);
+        ff_mpeg_unref_picture(s, &s->current_picture);
+        if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
+                                       s->current_picture_ptr)) < 0)
+            return ret;
 
         s->picture_number = s->new_picture.f.display_picture_number;
     } else {
-        memset(&s->new_picture, 0, sizeof(Picture));
+        ff_mpeg_unref_picture(s, &s->new_picture);
     }
     return 0;
 }
@@ -1866,7 +1786,7 @@
         update_qscale(s);
 
         if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
-            s->qscale = s->current_picture_ptr->f.qscale_table[mb_xy];
+            s->qscale = s->current_picture_ptr->qscale_table[mb_xy];
             s->dquant = s->qscale - last_qp;
 
             if (s->out_format == FMT_H263) {
@@ -1975,10 +1895,10 @@
         dest_cr = s->dest[2];
 
         if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
-            op_pix  = s->dsp.put_pixels_tab;
+            op_pix  = s->hdsp.put_pixels_tab;
             op_qpix = s->dsp.put_qpel_pixels_tab;
         } else {
-            op_pix  = s->dsp.put_no_rnd_pixels_tab;
+            op_pix  = s->hdsp.put_no_rnd_pixels_tab;
             op_qpix = s->dsp.put_no_rnd_qpel_pixels_tab;
         }
 
@@ -1986,7 +1906,7 @@
             ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0,
                           s->last_picture.f.data,
                           op_pix, op_qpix);
-            op_pix  = s->dsp.avg_pixels_tab;
+            op_pix  = s->hdsp.avg_pixels_tab;
             op_qpix = s->dsp.avg_qpel_pixels_tab;
         }
         if (s->mv_dir & MV_DIR_BACKWARD) {
@@ -2765,8 +2685,8 @@
                     s->mv_type = MV_TYPE_8X8;
                     s->mb_intra= 0;
                     for(i=0; i<4; i++){
-                        s->mv[0][i][0] = s->current_picture.f.motion_val[0][s->block_index[i]][0];
-                        s->mv[0][i][1] = s->current_picture.f.motion_val[0][s->block_index[i]][1];
+                        s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
+                        s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
                     }
                     encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
                                  &dmin, &next_block, 0, 0);
@@ -2953,7 +2873,7 @@
                     }
                 }
 
-                s->current_picture.f.qscale_table[xy] = best_s.qscale;
+                s->current_picture.qscale_table[xy] = best_s.qscale;
 
                 copy_context_after_encode(s, &best_s, -1);
 
@@ -2980,9 +2900,9 @@
                     ff_h263_update_motion_val(s);
 
                 if(next_block==0){ //FIXME 16 vs linesize16
-                    s->dsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad                     , s->linesize  ,16);
-                    s->dsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize    , s->uvlinesize, 8);
-                    s->dsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
+                    s->hdsp.put_pixels_tab[0][0](s->dest[0], s->rd_scratchpad                     , s->linesize  ,16);
+                    s->hdsp.put_pixels_tab[1][0](s->dest[1], s->rd_scratchpad + 16*s->linesize    , s->uvlinesize, 8);
+                    s->hdsp.put_pixels_tab[1][0](s->dest[2], s->rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
                 }
 
                 if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
@@ -3020,8 +2940,8 @@
                     s->mv_type = MV_TYPE_8X8;
                     s->mb_intra= 0;
                     for(i=0; i<4; i++){
-                        s->mv[0][i][0] = s->current_picture.f.motion_val[0][s->block_index[i]][0];
-                        s->mv[0][i][1] = s->current_picture.f.motion_val[0][s->block_index[i]][1];
+                        s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
+                        s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
                     }
                     break;
                 case CANDIDATE_MB_TYPE_DIRECT:
@@ -3312,7 +3232,7 @@
     if(s->pict_type != AV_PICTURE_TYPE_I){
         s->lambda = (s->lambda * s->avctx->me_penalty_compensation + 128)>>8;
         s->lambda2= (s->lambda2* (int64_t)s->avctx->me_penalty_compensation + 128)>>8;
-        if(s->pict_type != AV_PICTURE_TYPE_B && s->avctx->me_threshold==0){
+        if (s->pict_type != AV_PICTURE_TYPE_B) {
             if((s->avctx->pre_me && s->last_non_b_pict_type==AV_PICTURE_TYPE_I) || s->avctx->pre_me==2){
                 s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
             }
@@ -3480,8 +3400,6 @@
         if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
             ff_mpeg1_encode_picture_header(s, picture_number);
         break;
-    case FMT_H264:
-        break;
     default:
         av_assert0(0);
     }
diff --git a/libavcodec/mpegvideo_motion.c b/libavcodec/mpegvideo_motion.c
index 565f6cb..96b3780 100644
--- a/libavcodec/mpegvideo_motion.c
+++ b/libavcodec/mpegvideo_motion.c
@@ -27,6 +27,7 @@
 #include "libavutil/internal.h"
 #include "avcodec.h"
 #include "dsputil.h"
+#include "h261.h"
 #include "mpegvideo.h"
 #include "mjpegenc.h"
 #include "msmpeg4.h"
@@ -59,13 +60,11 @@
 
     ptr = ref_picture[0] + (src_y * linesize) + src_x;
 
-    if(s->flags&CODEC_FLAG_EMU_EDGE){
         if(   (unsigned)src_x >= FFMAX(s->h_edge_pos - 17, 0)
            || (unsigned)src_y >= FFMAX(s->v_edge_pos - 17, 0)){
             s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos);
             ptr= s->edge_emu_buffer;
         }
-    }
 
     if((motion_x|motion_y)&7){
         s->dsp.gmc1(dest_y  , ptr  , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding);
@@ -75,9 +74,9 @@
 
         dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2);
         if (s->no_rounding){
-            s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
+            s->hdsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
         }else{
-            s->dsp.put_pixels_tab       [0][dxy](dest_y, ptr, linesize, 16);
+            s->hdsp.put_pixels_tab       [0][dxy](dest_y, ptr, linesize, 16);
         }
     }
 
@@ -98,14 +97,12 @@
 
     offset = (src_y * uvlinesize) + src_x;
     ptr = ref_picture[1] + offset;
-    if(s->flags&CODEC_FLAG_EMU_EDGE){
         if(   (unsigned)src_x >= FFMAX((s->h_edge_pos>>1) - 9, 0)
            || (unsigned)src_y >= FFMAX((s->v_edge_pos>>1) - 9, 0)){
             s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
             ptr= s->edge_emu_buffer;
             emu=1;
         }
-    }
     s->dsp.gmc1(dest_cb, ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding);
 
     ptr = ref_picture[2] + offset;
@@ -641,7 +638,7 @@
 
     if(!is_mpeg12 && s->obmc && s->pict_type != AV_PICTURE_TYPE_B){
         LOCAL_ALIGNED_8(int16_t, mv_cache, [4], [4][2]);
-        AVFrame *cur_frame = &s->current_picture.f;
+        Picture *cur_frame = &s->current_picture;
         const int xy= s->mb_x + s->mb_y*s->mb_stride;
         const int mot_stride= s->b8_stride;
         const int mot_xy= mb_x*2 + mb_y*2*mot_stride;
@@ -812,7 +809,8 @@
                                   s->mv[dir][1][0], s->mv[dir][1][1], 8, mb_y);
             }
         } else {
-            if(s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != AV_PICTURE_TYPE_B && !s->first_field){
+            if(   s->picture_structure != s->field_select[dir][0] + 1 && s->pict_type != AV_PICTURE_TYPE_B && !s->first_field
+               || !ref_picture[0]){
                 ref_picture = s->current_picture_ptr->f.data;
             }
 
@@ -826,8 +824,8 @@
         for(i=0; i<2; i++){
             uint8_t ** ref2picture;
 
-            if(s->picture_structure == s->field_select[dir][i] + 1
-               || s->pict_type == AV_PICTURE_TYPE_B || s->first_field){
+            if((s->picture_structure == s->field_select[dir][i] + 1
+               || s->pict_type == AV_PICTURE_TYPE_B || s->first_field) && ref_picture[0]){
                 ref2picture= ref_picture;
             }else{
                 ref2picture = s->current_picture_ptr->f.data;
@@ -853,9 +851,12 @@
                                       s->mv[dir][2*i + j][0],
                                       s->mv[dir][2*i + j][1], 8, mb_y);
                 }
-                pix_op = s->dsp.avg_pixels_tab;
+                pix_op = s->hdsp.avg_pixels_tab;
             }
         }else{
+            if (!ref_picture[0]) {
+                ref_picture = s->current_picture_ptr->f.data;
+            }
             for(i=0; i<2; i++){
                 mpeg_motion(s, dest_y, dest_cb, dest_cr,
                             s->picture_structure != i+1,
@@ -863,7 +864,7 @@
                             s->mv[dir][2*i][0],s->mv[dir][2*i][1],16, mb_y>>1);
 
                 // after put we make avg of the same block
-                pix_op=s->dsp.avg_pixels_tab;
+                pix_op=s->hdsp.avg_pixels_tab;
 
                 //opposite parity is always in the same frame if this is second field
                 if(!s->first_field){
diff --git a/libavcodec/mpegvideo_parser.c b/libavcodec/mpegvideo_parser.c
index 35a9160..d36bc7a 100644
--- a/libavcodec/mpegvideo_parser.c
+++ b/libavcodec/mpegvideo_parser.c
@@ -21,7 +21,8 @@
  */
 
 #include "parser.h"
-#include "mpegvideo.h"
+#include "mpeg12.h"
+#include "internal.h"
 
 struct MpvParseContext {
     ParseContext pc;
@@ -50,7 +51,7 @@
 
     while (buf < buf_end) {
         start_code= -1;
-        buf= avpriv_mpv_find_start_code(buf, buf_end, &start_code);
+        buf= avpriv_find_start_code(buf, buf_end, &start_code);
         bytes_left = buf_end - buf;
         switch(start_code) {
         case PICTURE_START_CODE:
diff --git a/libavcodec/mpegvideo_xvmc.c b/libavcodec/mpegvideo_xvmc.c
index f30124c..6b0c6ac 100644
--- a/libavcodec/mpegvideo_xvmc.c
+++ b/libavcodec/mpegvideo_xvmc.c
@@ -178,7 +178,7 @@
 
     // Do I need to export quant when I could not perform postprocessing?
     // Anyway, it doesn't hurt.
-    s->current_picture.f.qscale_table[mb_xy] = s->qscale;
+    s->current_picture.qscale_table[mb_xy] = s->qscale;
 
     // start of XVMC-specific code
     render = (struct xvmc_pix_fmt*)s->current_picture.f.data[2];
diff --git a/libavcodec/mqc.c b/libavcodec/mqc.c
index 700b957..288570d 100644
--- a/libavcodec/mqc.c
+++ b/libavcodec/mqc.c
@@ -20,89 +20,93 @@
  */
 
 /**
- * MQ-coder ecoder and decoder common functions
+ * MQ-coder common (decoder/encoder) functions
  * @file
  * @author Kamil Nowosad
  */
 
+#include <string.h>
+#include <stdint.h>
+
 #include "mqc.h"
 
-typedef struct {
-        uint16_t qe;
-        uint8_t  nmps;
-        uint8_t  nlps;
-        uint8_t  sw;
+/* MQ coder context state structure */
+typedef struct MqcCxState {
+    uint16_t qe;
+    uint8_t  nmps;
+    uint8_t  nlps;
+    uint8_t  sw;
 } MqcCxState;
 
-const static MqcCxState cx_states[47] = {
-    {0x5601,  1,  1, 1},
-    {0x3401,  2,  6, 0},
-    {0x1801,  3,  9, 0},
-    {0x0AC1,  4, 12, 0},
-    {0x0521,  5, 29, 0},
-    {0x0221, 38, 33, 0},
-    {0x5601,  7,  6, 1},
-    {0x5401,  8, 14, 0},
-    {0x4801,  9, 14, 0},
-    {0x3801, 10, 14, 0},
-    {0x3001, 11, 17, 0},
-    {0x2401, 12, 18, 0},
-    {0x1C01, 13, 20, 0},
-    {0x1601, 29, 21, 0},
-    {0x5601, 15, 14, 1},
-    {0x5401, 16, 14, 0},
-    {0x5101, 17, 15, 0},
-    {0x4801, 18, 16, 0},
-    {0x3801, 19, 17, 0},
-    {0x3401, 20, 18, 0},
-    {0x3001, 21, 19, 0},
-    {0x2801, 22, 19, 0},
-    {0x2401, 23, 20, 0},
-    {0x2201, 24, 21, 0},
-    {0x1C01, 25, 22, 0},
-    {0x1801, 26, 23, 0},
-    {0x1601, 27, 24, 0},
-    {0x1401, 28, 25, 0},
-    {0x1201, 29, 26, 0},
-    {0x1101, 30, 27, 0},
-    {0x0AC1, 31, 28, 0},
-    {0x09C1, 32, 29, 0},
-    {0x08A1, 33, 30, 0},
-    {0x0521, 34, 31, 0},
-    {0x0441, 35, 32, 0},
-    {0x02A1, 36, 33, 0},
-    {0x0221, 37, 34, 0},
-    {0x0141, 38, 35, 0},
-    {0x0111, 39, 36, 0},
-    {0x0085, 40, 37, 0},
-    {0x0049, 41, 38, 0},
-    {0x0025, 42, 39, 0},
-    {0x0015, 43, 40, 0},
-    {0x0009, 44, 41, 0},
-    {0x0005, 45, 42, 0},
-    {0x0001, 45, 43, 0},
-    {0x5601, 46, 46, 0}
+static const MqcCxState cx_states[47] = {
+    { 0x5601,  1,  1, 1 },
+    { 0x3401,  2,  6, 0 },
+    { 0x1801,  3,  9, 0 },
+    { 0x0AC1,  4, 12, 0 },
+    { 0x0521,  5, 29, 0 },
+    { 0x0221, 38, 33, 0 },
+    { 0x5601,  7,  6, 1 },
+    { 0x5401,  8, 14, 0 },
+    { 0x4801,  9, 14, 0 },
+    { 0x3801, 10, 14, 0 },
+    { 0x3001, 11, 17, 0 },
+    { 0x2401, 12, 18, 0 },
+    { 0x1C01, 13, 20, 0 },
+    { 0x1601, 29, 21, 0 },
+    { 0x5601, 15, 14, 1 },
+    { 0x5401, 16, 14, 0 },
+    { 0x5101, 17, 15, 0 },
+    { 0x4801, 18, 16, 0 },
+    { 0x3801, 19, 17, 0 },
+    { 0x3401, 20, 18, 0 },
+    { 0x3001, 21, 19, 0 },
+    { 0x2801, 22, 19, 0 },
+    { 0x2401, 23, 20, 0 },
+    { 0x2201, 24, 21, 0 },
+    { 0x1C01, 25, 22, 0 },
+    { 0x1801, 26, 23, 0 },
+    { 0x1601, 27, 24, 0 },
+    { 0x1401, 28, 25, 0 },
+    { 0x1201, 29, 26, 0 },
+    { 0x1101, 30, 27, 0 },
+    { 0x0AC1, 31, 28, 0 },
+    { 0x09C1, 32, 29, 0 },
+    { 0x08A1, 33, 30, 0 },
+    { 0x0521, 34, 31, 0 },
+    { 0x0441, 35, 32, 0 },
+    { 0x02A1, 36, 33, 0 },
+    { 0x0221, 37, 34, 0 },
+    { 0x0141, 38, 35, 0 },
+    { 0x0111, 39, 36, 0 },
+    { 0x0085, 40, 37, 0 },
+    { 0x0049, 41, 38, 0 },
+    { 0x0025, 42, 39, 0 },
+    { 0x0015, 43, 40, 0 },
+    { 0x0009, 44, 41, 0 },
+    { 0x0005, 45, 42, 0 },
+    { 0x0001, 45, 43, 0 },
+    { 0x5601, 46, 46, 0 }
 };
 
-uint16_t ff_mqc_qe [2*47];
-uint8_t ff_mqc_nlps[2*47];
-uint8_t ff_mqc_nmps[2*47];
+uint16_t ff_mqc_qe [2 * 47];
+uint8_t ff_mqc_nlps[2 * 47];
+uint8_t ff_mqc_nmps[2 * 47];
 
 void ff_mqc_init_contexts(MqcState *mqc)
 {
     int i;
     memset(mqc->cx_states, 0, sizeof(mqc->cx_states));
     mqc->cx_states[MQC_CX_UNI] = 2 * 46;
-    mqc->cx_states[MQC_CX_RL] = 2 * 3;
-    mqc->cx_states[0] = 2 * 4;
+    mqc->cx_states[MQC_CX_RL]  = 2 * 3;
+    mqc->cx_states[0]          = 2 * 4;
 
-    for (i = 0; i < 47; i++){
-        ff_mqc_qe[2*i  ] =
-        ff_mqc_qe[2*i+1] = cx_states[i].qe;
+    for (i = 0; i < 47; i++) {
+        ff_mqc_qe[2 * i]     =
+        ff_mqc_qe[2 * i + 1] = cx_states[i].qe;
 
-        ff_mqc_nlps[2*i  ] = 2*cx_states[i].nlps + cx_states[i].sw;
-        ff_mqc_nlps[2*i+1] = 2*cx_states[i].nlps + 1 - cx_states[i].sw;
-        ff_mqc_nmps[2*i  ] = 2*cx_states[i].nmps;
-        ff_mqc_nmps[2*i+1] = 2*cx_states[i].nmps + 1;
+        ff_mqc_nlps[2 * i]     = 2 * cx_states[i].nlps + cx_states[i].sw;
+        ff_mqc_nlps[2 * i + 1] = 2 * cx_states[i].nlps + 1 - cx_states[i].sw;
+        ff_mqc_nmps[2 * i]     = 2 * cx_states[i].nmps;
+        ff_mqc_nmps[2 * i + 1] = 2 * cx_states[i].nmps + 1;
     }
 }
diff --git a/libavcodec/mqc.h b/libavcodec/mqc.h
index b28c13e..a0112d1 100644
--- a/libavcodec/mqc.h
+++ b/libavcodec/mqc.h
@@ -1,5 +1,5 @@
 /*
- * MQ-coder
+ * MQ-coder: structures, common and decoder functions
  * Copyright (c) 2007 Kamil Nowosad
  *
  * This file is part of FFmpeg.
@@ -28,16 +28,16 @@
  * @author Kamil Nowosad
  */
 
-#include "avcodec.h"
+#include <stdint.h>
 
 #define MQC_CX_UNI 17
 #define MQC_CX_RL  18
 
-extern uint16_t  ff_mqc_qe[2*47];
-extern uint8_t ff_mqc_nlps[2*47];
-extern uint8_t ff_mqc_nmps[2*47];
+extern uint16_t ff_mqc_qe[2 * 47];
+extern uint8_t  ff_mqc_nlps[2 * 47];
+extern uint8_t  ff_mqc_nmps[2 * 47];
 
-typedef struct {
+typedef struct MqcState {
     uint8_t *bp, *bpstart;
     unsigned int a;
     unsigned int c;
@@ -61,15 +61,27 @@
 
 /* decoder */
 
-/** initialize the decoder */
+/**
+ * Initialize MQ-decoder.
+ * @param mqc   MQ decoder state
+ * @param bp    byte poiter
+ */
 void ff_mqc_initdec(MqcState *mqc, uint8_t *bp);
 
-/** returns decoded bit with context cx */
+/**
+ * MQ decoder.
+ * @param mqc       MQ decoder state
+ * @param cxstate   Context
+ * @return          Decision (0 ot 1)
+ */
 int ff_mqc_decode(MqcState *mqc, uint8_t *cxstate);
 
 /* common */
 
-/** initialize the contexts */
+/**
+ * MQ-coder context initialisations.
+ * @param mqc       MQ-coder context
+ */
 void ff_mqc_init_contexts(MqcState *mqc);
 
 #endif /* AVCODEC_MQC_H */
diff --git a/libavcodec/mqcdec.c b/libavcodec/mqcdec.c
index 56e22f8..3625069 100644
--- a/libavcodec/mqcdec.c
+++ b/libavcodec/mqcdec.c
@@ -29,14 +29,14 @@
 
 static void bytein(MqcState *mqc)
 {
-    if (*mqc->bp == 0xff){
-        if (*(mqc->bp+1) > 0x8f)
+    if (*mqc->bp == 0xff) {
+        if (*(mqc->bp + 1) > 0x8f)
             mqc->c++;
-        else{
+        else {
             mqc->bp++;
             mqc->c += 2 + 0xfe00 - (*mqc->bp << 9);
         }
-    } else{
+    } else {
         mqc->bp++;
         mqc->c += 1 + 0xff00 - (*mqc->bp << 8);
     }
@@ -45,20 +45,20 @@
 static int exchange(MqcState *mqc, uint8_t *cxstate, int lps)
 {
     int d;
-    if ((mqc->a < ff_mqc_qe[*cxstate]) ^ (!lps)){
+    if ((mqc->a < ff_mqc_qe[*cxstate]) ^ (!lps)) {
         if (lps)
             mqc->a = ff_mqc_qe[*cxstate];
         d = *cxstate & 1;
         *cxstate = ff_mqc_nmps[*cxstate];
-    } else{
+    } else {
         if (lps)
             mqc->a = ff_mqc_qe[*cxstate];
         d = 1 - (*cxstate & 1);
         *cxstate = ff_mqc_nlps[*cxstate];
     }
-    // renormd:
-    do{
-        if (!(mqc->c & 0xff)){
+    // do RENORMD: see ISO/IEC 15444-1:2002 §C.3.3
+    do {
+        if (!(mqc->c & 0xff)) {
             mqc->c -= 0x100;
             bytein(mqc);
         }
@@ -72,7 +72,7 @@
 {
     ff_mqc_init_contexts(mqc);
     mqc->bp = bp;
-    mqc->c = (*mqc->bp ^ 0xff) << 16;
+    mqc->c  = (*mqc->bp ^ 0xff) << 16;
     bytein(mqc);
     mqc->c = mqc->c << 7;
     mqc->a = 0x8000;
@@ -81,7 +81,7 @@
 int ff_mqc_decode(MqcState *mqc, uint8_t *cxstate)
 {
     mqc->a -= ff_mqc_qe[*cxstate];
-    if ((mqc->c >> 16) < mqc->a){
+    if ((mqc->c >> 16) < mqc->a) {
         if (mqc->a & 0x8000)
             return *cxstate & 1;
         else
diff --git a/libavcodec/msmpeg4.c b/libavcodec/msmpeg4.c
index 278c5fc..766415e 100644
--- a/libavcodec/msmpeg4.c
+++ b/libavcodec/msmpeg4.c
@@ -47,15 +47,6 @@
  */
 //#define DEBUG
 
-#define DC_VLC_BITS 9
-#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 DEFAULT_INTER_INDEX 3
-
 /* This table is practically identical to the one from h263
  * except that it is inverted. */
 static av_cold void init_h263_dc_for_msmpeg4(void)
@@ -185,21 +176,6 @@
     return pred;
 }
 
-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;
@@ -252,13 +228,13 @@
         "addl %%eax, %2         \n\t"
         "addl %%eax, %1         \n\t"
         "addl %0, %%eax         \n\t"
-        "mull %4                \n\t"
+        "imull %4               \n\t"
         "movl %%edx, %0         \n\t"
         "movl %1, %%eax         \n\t"
-        "mull %4                \n\t"
+        "imull %4               \n\t"
         "movl %%edx, %1         \n\t"
         "movl %2, %%eax         \n\t"
-        "mull %4                \n\t"
+        "imull %4               \n\t"
         "movl %%edx, %2         \n\t"
         : "+b" (a), "+c" (b), "+D" (c)
         : "g" (scale), "S" (ff_inverse[scale])
@@ -361,909 +337,3 @@
     return pred;
 }
 
-/****************************************/
-/* decoding stuff */
-
-VLC ff_mb_non_intra_vlc[4];
-static VLC v2_dc_lum_vlc;
-static VLC v2_dc_chroma_vlc;
-static VLC v2_intra_cbpc_vlc;
-static VLC v2_mb_type_vlc;
-static VLC v2_mv_vlc;
-VLC ff_inter_intra_vlc;
-
-/* 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);
-    av_dlog(s, "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, int16_t block[6][64])
-{
-    int cbp, code, i;
-    uint32_t * const mb_type_ptr = &s->current_picture.f.mb_type[s->mb_x + s->mb_y*s->mb_stride];
-
-    if (s->pict_type == AV_PICTURE_TYPE_P) {
-        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;
-            }
-        }
-
-        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, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
-        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, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_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, ff_h263_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;
-
-        ff_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;
-        *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
-    } else {
-        if(s->msmpeg4_version==2){
-            s->ac_pred = get_bits1(&s->gb);
-            cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
-        } else{
-            s->ac_pred = 0;
-            cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
-            if(s->pict_type==AV_PICTURE_TYPE_P) cbp^=0x3C;
-        }
-        *mb_type_ptr = MB_TYPE_INTRA;
-    }
-
-    s->dsp.clear_blocks(s->block[0]);
-    for (i = 0; i < 6; i++) {
-        if (ff_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, int16_t block[6][64])
-{
-    int cbp, code, i;
-    uint8_t *coded_val;
-    uint32_t * const mb_type_ptr = &s->current_picture.f.mb_type[s->mb_x + s->mb_y*s->mb_stride];
-
-    if (s->pict_type == AV_PICTURE_TYPE_P) {
-        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, ff_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 = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
-                val = val ^ pred;
-                *coded_val = val;
-            }
-            cbp |= val << (5 - i);
-        }
-    }
-
-    if (!s->mb_intra) {
-        int mx, my;
-        if(s->per_mb_rl_table && cbp){
-            s->rl_table_index = decode012(&s->gb);
-            s->rl_chroma_table_index = s->rl_table_index;
-        }
-        ff_h263_pred_motion(s, 0, 0, &mx, &my);
-        if (ff_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 {
-        av_dlog(s, "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, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
-            av_dlog(s, "%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 (ff_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;
-}
-
-/* init all vlc decoding tables */
-av_cold int ff_msmpeg4_decode_init(AVCodecContext *avctx)
-{
-    MpegEncContext *s = avctx->priv_data;
-    static volatile int done = 0;
-    int i, ret;
-    MVTable *mv;
-
-    if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
-        return ret;
-
-    if (ff_h263_decode_init(avctx) < 0)
-        return -1;
-
-    ff_msmpeg4_common_init(s);
-
-    if (!done) {
-        for(i=0;i<NB_RL_TABLES;i++) {
-            ff_init_rl(&ff_rl_table[i], ff_static_rl_table_store[i]);
-        }
-        INIT_VLC_RL(ff_rl_table[0], 642);
-        INIT_VLC_RL(ff_rl_table[1], 1104);
-        INIT_VLC_RL(ff_rl_table[2], 554);
-        INIT_VLC_RL(ff_rl_table[3], 940);
-        INIT_VLC_RL(ff_rl_table[4], 962);
-        INIT_VLC_RL(ff_rl_table[5], 554);
-
-        mv = &ff_mv_tables[0];
-        INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
-                    mv->table_mv_bits, 1, 1,
-                    mv->table_mv_code, 2, 2, 3714);
-        mv = &ff_mv_tables[1];
-        INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
-                    mv->table_mv_bits, 1, 1,
-                    mv->table_mv_code, 2, 2, 2694);
-
-        INIT_VLC_STATIC(&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, 1158);
-        INIT_VLC_STATIC(&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, 1118);
-        INIT_VLC_STATIC(&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, 1476);
-        INIT_VLC_STATIC(&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, 1216);
-
-        INIT_VLC_STATIC(&v2_dc_lum_vlc, DC_VLC_BITS, 512,
-                 &ff_v2_dc_lum_table[0][1], 8, 4,
-                 &ff_v2_dc_lum_table[0][0], 8, 4, 1472);
-        INIT_VLC_STATIC(&v2_dc_chroma_vlc, DC_VLC_BITS, 512,
-                 &ff_v2_dc_chroma_table[0][1], 8, 4,
-                 &ff_v2_dc_chroma_table[0][0], 8, 4, 1506);
-
-        INIT_VLC_STATIC(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
-                 &ff_v2_intra_cbpc[0][1], 2, 1,
-                 &ff_v2_intra_cbpc[0][0], 2, 1, 8);
-        INIT_VLC_STATIC(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
-                 &ff_v2_mb_type[0][1], 2, 1,
-                 &ff_v2_mb_type[0][0], 2, 1, 128);
-        INIT_VLC_STATIC(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
-                 &ff_mvtab[0][1], 2, 1,
-                 &ff_mvtab[0][0], 2, 1, 538);
-
-        INIT_VLC_STATIC(&ff_mb_non_intra_vlc[0], MB_NON_INTRA_VLC_BITS, 128,
-                     &ff_wmv2_inter_table[0][0][1], 8, 4,
-                     &ff_wmv2_inter_table[0][0][0], 8, 4, 1636);
-        INIT_VLC_STATIC(&ff_mb_non_intra_vlc[1], MB_NON_INTRA_VLC_BITS, 128,
-                     &ff_wmv2_inter_table[1][0][1], 8, 4,
-                     &ff_wmv2_inter_table[1][0][0], 8, 4, 2648);
-        INIT_VLC_STATIC(&ff_mb_non_intra_vlc[2], MB_NON_INTRA_VLC_BITS, 128,
-                     &ff_wmv2_inter_table[2][0][1], 8, 4,
-                     &ff_wmv2_inter_table[2][0][0], 8, 4, 1532);
-        INIT_VLC_STATIC(&ff_mb_non_intra_vlc[3], MB_NON_INTRA_VLC_BITS, 128,
-                     &ff_wmv2_inter_table[3][0][1], 8, 4,
-                     &ff_wmv2_inter_table[3][0][0], 8, 4, 2488);
-
-        INIT_VLC_STATIC(&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, 536);
-
-        INIT_VLC_STATIC(&ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
-                 &ff_table_inter_intra[0][1], 2, 1,
-                 &ff_table_inter_intra[0][0], 2, 1, 8);
-        done = 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:
-        if (CONFIG_WMV2_DECODER)
-            s->decode_mb= ff_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 is not a keyframe
-
-    return 0;
-}
-
-int ff_msmpeg4_decode_picture_header(MpegEncContext * s)
-{
-    int code;
-
-    if(s->msmpeg4_version==1){
-        int start_code = get_bits_long(&s->gb, 32);
-        if(start_code!=0x00000100){
-            av_log(s->avctx, AV_LOG_ERROR, "invalid startcode\n");
-            return -1;
-        }
-
-        skip_bits(&s->gb, 5); // frame number */
-    }
-
-    s->pict_type = get_bits(&s->gb, 2) + 1;
-    if (s->pict_type != AV_PICTURE_TYPE_I &&
-        s->pict_type != AV_PICTURE_TYPE_P){
-        av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n");
-        return -1;
-    }
-#if 0
-{
-    static int had_i=0;
-    if(s->pict_type == AV_PICTURE_TYPE_I) 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 == AV_PICTURE_TYPE_I) {
-        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:
-            ff_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;
-        }
-    }
-    av_dlog(s->avctx, "%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;
-
-    return 0;
-}
-
-int ff_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)
-    {
-        skip_bits(&s->gb, 5); /* fps */
-        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;
-    }
-    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 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   = ff_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;
-}
-
-//#define ERROR_DETAILS
-int ff_msmpeg4_decode_block(MpegEncContext * s, int16_t * block,
-                              int n, int coded, const uint8_t *scan_table)
-{
-    int level, i, last, run, run_diff;
-    int av_uninit(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 = &ff_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 = &ff_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 = s->msmpeg4_version >= 4;
-        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 = &ff_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);
-                        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;
-                            av_dlog(s->avctx, "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){
-                                    ll= 8+SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
-                                }
-                            }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);
-                            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;
-                    }
-
-#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 */
-                    SKIP_BITS(re, &s->gb, 2);
-                    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 */
-                SKIP_BITS(re, &s->gb, 1);
-                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= get_bits_left(&s->gb);
-                if(((i+192 == 64 && level/qmul==-1) || !(s->err_recognition&(AV_EF_BITSTREAM|AV_EF_COMPLIANT))) && left>=0){
-                    av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
-                    i = 63;
-                    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) {
-        ff_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;
-}
-
-int ff_msmpeg4_decode_motion(MpegEncContext * s,
-                                 int *mx_ptr, int *my_ptr)
-{
-    MVTable *mv;
-    int code, mx, my;
-
-    mv = &ff_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) {
-        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;
-}
-
-AVCodec ff_msmpeg4v1_decoder = {
-    .name           = "msmpeg4v1",
-    .type           = AVMEDIA_TYPE_VIDEO,
-    .id             = AV_CODEC_ID_MSMPEG4V1,
-    .priv_data_size = sizeof(MpegEncContext),
-    .init           = ff_msmpeg4_decode_init,
-    .close          = ff_h263_decode_end,
-    .decode         = ff_h263_decode_frame,
-    .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
-    .max_lowres     = 3,
-    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 1"),
-    .pix_fmts       = ff_pixfmt_list_420,
-};
-
-AVCodec ff_msmpeg4v2_decoder = {
-    .name           = "msmpeg4v2",
-    .type           = AVMEDIA_TYPE_VIDEO,
-    .id             = AV_CODEC_ID_MSMPEG4V2,
-    .priv_data_size = sizeof(MpegEncContext),
-    .init           = ff_msmpeg4_decode_init,
-    .close          = ff_h263_decode_end,
-    .decode         = ff_h263_decode_frame,
-    .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
-    .max_lowres     = 3,
-    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
-    .pix_fmts       = ff_pixfmt_list_420,
-};
-
-AVCodec ff_msmpeg4v3_decoder = {
-    .name           = "msmpeg4",
-    .type           = AVMEDIA_TYPE_VIDEO,
-    .id             = AV_CODEC_ID_MSMPEG4V3,
-    .priv_data_size = sizeof(MpegEncContext),
-    .init           = ff_msmpeg4_decode_init,
-    .close          = ff_h263_decode_end,
-    .decode         = ff_h263_decode_frame,
-    .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
-    .max_lowres     = 3,
-    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
-    .pix_fmts       = ff_pixfmt_list_420,
-};
-
-AVCodec ff_wmv1_decoder = {
-    .name           = "wmv1",
-    .type           = AVMEDIA_TYPE_VIDEO,
-    .id             = AV_CODEC_ID_WMV1,
-    .priv_data_size = sizeof(MpegEncContext),
-    .init           = ff_msmpeg4_decode_init,
-    .close          = ff_h263_decode_end,
-    .decode         = ff_h263_decode_frame,
-    .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
-    .max_lowres     = 3,
-    .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
-    .pix_fmts       = ff_pixfmt_list_420,
-};
diff --git a/libavcodec/msmpeg4dec.c b/libavcodec/msmpeg4dec.c
new file mode 100644
index 0000000..768b520
--- /dev/null
+++ b/libavcodec/msmpeg4dec.c
@@ -0,0 +1,964 @@
+/*
+ * MSMPEG4 backend for encoder and decoder
+ * Copyright (c) 2001 Fabrice Bellard
+ * Copyright (c) 2002-2013 Michael Niedermayer <michaelni@gmx.at>
+ *
+ * msmpeg4v1 & v2 stuff by 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 "avcodec.h"
+#include "dsputil.h"
+#include "mpegvideo.h"
+#include "msmpeg4.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/x86/asm.h"
+#include "h263.h"
+#include "mpeg4video.h"
+#include "msmpeg4data.h"
+#include "vc1data.h"
+
+#define DC_VLC_BITS 9
+#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 DEFAULT_INTER_INDEX 3
+
+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];
+}
+
+/****************************************/
+/* decoding stuff */
+
+VLC ff_mb_non_intra_vlc[4];
+static VLC v2_dc_lum_vlc;
+static VLC v2_dc_chroma_vlc;
+static VLC v2_intra_cbpc_vlc;
+static VLC v2_mb_type_vlc;
+static VLC v2_mv_vlc;
+VLC ff_inter_intra_vlc;
+
+/* 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);
+    av_dlog(s, "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, int16_t block[6][64])
+{
+    int cbp, code, i;
+    uint32_t * const mb_type_ptr = &s->current_picture.mb_type[s->mb_x + s->mb_y*s->mb_stride];
+
+    if (s->pict_type == AV_PICTURE_TYPE_P) {
+        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;
+            }
+        }
+
+        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, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
+        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, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_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, ff_h263_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;
+
+        ff_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;
+        *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
+    } else {
+        if(s->msmpeg4_version==2){
+            s->ac_pred = get_bits1(&s->gb);
+            cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
+        } else{
+            s->ac_pred = 0;
+            cbp|= get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
+            if(s->pict_type==AV_PICTURE_TYPE_P) cbp^=0x3C;
+        }
+        *mb_type_ptr = MB_TYPE_INTRA;
+    }
+
+    s->dsp.clear_blocks(s->block[0]);
+    for (i = 0; i < 6; i++) {
+        if (ff_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, int16_t 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 == AV_PICTURE_TYPE_P) {
+        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, ff_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 = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
+                val = val ^ pred;
+                *coded_val = val;
+            }
+            cbp |= val << (5 - i);
+        }
+    }
+
+    if (!s->mb_intra) {
+        int mx, my;
+        if(s->per_mb_rl_table && cbp){
+            s->rl_table_index = decode012(&s->gb);
+            s->rl_chroma_table_index = s->rl_table_index;
+        }
+        ff_h263_pred_motion(s, 0, 0, &mx, &my);
+        if (ff_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 {
+        av_dlog(s, "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, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
+            av_dlog(s, "%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 (ff_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;
+}
+
+/* init all vlc decoding tables */
+av_cold int ff_msmpeg4_decode_init(AVCodecContext *avctx)
+{
+    MpegEncContext *s = avctx->priv_data;
+    static volatile int done = 0;
+    int i, ret;
+    MVTable *mv;
+
+    if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
+        return ret;
+
+    if (ff_h263_decode_init(avctx) < 0)
+        return -1;
+
+    ff_msmpeg4_common_init(s);
+
+    if (!done) {
+        for(i=0;i<NB_RL_TABLES;i++) {
+            ff_init_rl(&ff_rl_table[i], ff_static_rl_table_store[i]);
+        }
+        INIT_VLC_RL(ff_rl_table[0], 642);
+        INIT_VLC_RL(ff_rl_table[1], 1104);
+        INIT_VLC_RL(ff_rl_table[2], 554);
+        INIT_VLC_RL(ff_rl_table[3], 940);
+        INIT_VLC_RL(ff_rl_table[4], 962);
+        INIT_VLC_RL(ff_rl_table[5], 554);
+
+        mv = &ff_mv_tables[0];
+        INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
+                    mv->table_mv_bits, 1, 1,
+                    mv->table_mv_code, 2, 2, 3714);
+        mv = &ff_mv_tables[1];
+        INIT_VLC_STATIC(&mv->vlc, MV_VLC_BITS, mv->n + 1,
+                    mv->table_mv_bits, 1, 1,
+                    mv->table_mv_code, 2, 2, 2694);
+
+        INIT_VLC_STATIC(&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, 1158);
+        INIT_VLC_STATIC(&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, 1118);
+        INIT_VLC_STATIC(&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, 1476);
+        INIT_VLC_STATIC(&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, 1216);
+
+        INIT_VLC_STATIC(&v2_dc_lum_vlc, DC_VLC_BITS, 512,
+                 &ff_v2_dc_lum_table[0][1], 8, 4,
+                 &ff_v2_dc_lum_table[0][0], 8, 4, 1472);
+        INIT_VLC_STATIC(&v2_dc_chroma_vlc, DC_VLC_BITS, 512,
+                 &ff_v2_dc_chroma_table[0][1], 8, 4,
+                 &ff_v2_dc_chroma_table[0][0], 8, 4, 1506);
+
+        INIT_VLC_STATIC(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
+                 &ff_v2_intra_cbpc[0][1], 2, 1,
+                 &ff_v2_intra_cbpc[0][0], 2, 1, 8);
+        INIT_VLC_STATIC(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
+                 &ff_v2_mb_type[0][1], 2, 1,
+                 &ff_v2_mb_type[0][0], 2, 1, 128);
+        INIT_VLC_STATIC(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
+                 &ff_mvtab[0][1], 2, 1,
+                 &ff_mvtab[0][0], 2, 1, 538);
+
+        INIT_VLC_STATIC(&ff_mb_non_intra_vlc[0], MB_NON_INTRA_VLC_BITS, 128,
+                     &ff_wmv2_inter_table[0][0][1], 8, 4,
+                     &ff_wmv2_inter_table[0][0][0], 8, 4, 1636);
+        INIT_VLC_STATIC(&ff_mb_non_intra_vlc[1], MB_NON_INTRA_VLC_BITS, 128,
+                     &ff_wmv2_inter_table[1][0][1], 8, 4,
+                     &ff_wmv2_inter_table[1][0][0], 8, 4, 2648);
+        INIT_VLC_STATIC(&ff_mb_non_intra_vlc[2], MB_NON_INTRA_VLC_BITS, 128,
+                     &ff_wmv2_inter_table[2][0][1], 8, 4,
+                     &ff_wmv2_inter_table[2][0][0], 8, 4, 1532);
+        INIT_VLC_STATIC(&ff_mb_non_intra_vlc[3], MB_NON_INTRA_VLC_BITS, 128,
+                     &ff_wmv2_inter_table[3][0][1], 8, 4,
+                     &ff_wmv2_inter_table[3][0][0], 8, 4, 2488);
+
+        INIT_VLC_STATIC(&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, 536);
+
+        INIT_VLC_STATIC(&ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
+                 &ff_table_inter_intra[0][1], 2, 1,
+                 &ff_table_inter_intra[0][0], 2, 1, 8);
+        done = 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:
+        if (CONFIG_WMV2_DECODER)
+            s->decode_mb= ff_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 is not a keyframe
+
+    return 0;
+}
+
+int ff_msmpeg4_decode_picture_header(MpegEncContext * s)
+{
+    int code;
+
+    if(s->msmpeg4_version==1){
+        int start_code = get_bits_long(&s->gb, 32);
+        if(start_code!=0x00000100){
+            av_log(s->avctx, AV_LOG_ERROR, "invalid startcode\n");
+            return -1;
+        }
+
+        skip_bits(&s->gb, 5); // frame number */
+    }
+
+    s->pict_type = get_bits(&s->gb, 2) + 1;
+    if (s->pict_type != AV_PICTURE_TYPE_I &&
+        s->pict_type != AV_PICTURE_TYPE_P){
+        av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n");
+        return -1;
+    }
+#if 0
+{
+    static int had_i=0;
+    if(s->pict_type == AV_PICTURE_TYPE_I) 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 == AV_PICTURE_TYPE_I) {
+        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:
+            ff_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;
+        }
+    }
+    av_dlog(s->avctx, "%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;
+
+    return 0;
+}
+
+int ff_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)
+    {
+        skip_bits(&s->gb, 5); /* fps */
+        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;
+    }
+    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 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   = ff_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;
+}
+
+//#define ERROR_DETAILS
+int ff_msmpeg4_decode_block(MpegEncContext * s, int16_t * block,
+                              int n, int coded, const uint8_t *scan_table)
+{
+    int level, i, last, run, run_diff;
+    int av_uninit(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;
+        }
+        if (n < 4) {
+            rl = &ff_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 = &ff_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 = s->msmpeg4_version >= 4;
+        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 = &ff_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);
+                        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;
+                            av_dlog(s->avctx, "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){
+                                    ll= 8+SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
+                                }
+                            }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);
+                            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;
+                    }
+
+#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 */
+                    SKIP_BITS(re, &s->gb, 2);
+                    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 */
+                SKIP_BITS(re, &s->gb, 1);
+                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= get_bits_left(&s->gb);
+                if(((i+192 == 64 && level/qmul==-1) || !(s->err_recognition&(AV_EF_BITSTREAM|AV_EF_COMPLIANT))) && left>=0){
+                    av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
+                    i = 63;
+                    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) {
+        ff_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;
+}
+
+int ff_msmpeg4_decode_motion(MpegEncContext * s,
+                                 int *mx_ptr, int *my_ptr)
+{
+    MVTable *mv;
+    int code, mx, my;
+
+    mv = &ff_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) {
+        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;
+}
+
+AVCodec ff_msmpeg4v1_decoder = {
+    .name           = "msmpeg4v1",
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_MSMPEG4V1,
+    .priv_data_size = sizeof(MpegEncContext),
+    .init           = ff_msmpeg4_decode_init,
+    .close          = ff_h263_decode_end,
+    .decode         = ff_h263_decode_frame,
+    .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
+    .max_lowres     = 3,
+    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 1"),
+    .pix_fmts       = ff_pixfmt_list_420,
+};
+
+AVCodec ff_msmpeg4v2_decoder = {
+    .name           = "msmpeg4v2",
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_MSMPEG4V2,
+    .priv_data_size = sizeof(MpegEncContext),
+    .init           = ff_msmpeg4_decode_init,
+    .close          = ff_h263_decode_end,
+    .decode         = ff_h263_decode_frame,
+    .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
+    .max_lowres     = 3,
+    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
+    .pix_fmts       = ff_pixfmt_list_420,
+};
+
+AVCodec ff_msmpeg4v3_decoder = {
+    .name           = "msmpeg4",
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_MSMPEG4V3,
+    .priv_data_size = sizeof(MpegEncContext),
+    .init           = ff_msmpeg4_decode_init,
+    .close          = ff_h263_decode_end,
+    .decode         = ff_h263_decode_frame,
+    .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
+    .max_lowres     = 3,
+    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
+    .pix_fmts       = ff_pixfmt_list_420,
+};
+
+AVCodec ff_wmv1_decoder = {
+    .name           = "wmv1",
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_WMV1,
+    .priv_data_size = sizeof(MpegEncContext),
+    .init           = ff_msmpeg4_decode_init,
+    .close          = ff_h263_decode_end,
+    .decode         = ff_h263_decode_frame,
+    .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
+    .max_lowres     = 3,
+    .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
+    .pix_fmts       = ff_pixfmt_list_420,
+};
diff --git a/libavcodec/msrle.c b/libavcodec/msrle.c
index 7187d1a..674e586 100644
--- a/libavcodec/msrle.c
+++ b/libavcodec/msrle.c
@@ -33,6 +33,7 @@
 #include <string.h>
 
 #include "avcodec.h"
+#include "internal.h"
 #include "msrledec.h"
 
 typedef struct MsrleContext {
@@ -70,7 +71,6 @@
     }
 
     avcodec_get_frame_defaults(&s->frame);
-    s->frame.data[0] = NULL;
 
     if (avctx->extradata_size >= 4)
         for (i = 0; i < FFMIN(avctx->extradata_size, AVPALETTE_SIZE)/4; i++)
@@ -92,12 +92,8 @@
     s->buf = buf;
     s->size = buf_size;
 
-    s->frame.reference = 3;
-    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
-    if ((ret = avctx->reget_buffer(avctx, &s->frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0)
         return ret;
-    }
 
     if (avctx->bits_per_coded_sample > 1 && avctx->bits_per_coded_sample <= 8) {
         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
@@ -136,8 +132,10 @@
         ff_msrle_decode(avctx, (AVPicture*)&s->frame, avctx->bits_per_coded_sample, &s->gb);
     }
 
+    if ((ret = av_frame_ref(data, &s->frame)) < 0)
+        return ret;
+
     *got_frame      = 1;
-    *(AVFrame*)data = s->frame;
 
     /* report that the buffer was completely consumed */
     return buf_size;
@@ -148,8 +146,7 @@
     MsrleContext *s = avctx->priv_data;
 
     /* release the last frame */
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+    av_frame_unref(&s->frame);
 
     return 0;
 }
diff --git a/libavcodec/msrledec.c b/libavcodec/msrledec.c
index cd0a73d..83d7d13 100644
--- a/libavcodec/msrledec.c
+++ b/libavcodec/msrledec.c
@@ -138,7 +138,8 @@
     unsigned int width= FFABS(pic->linesize[0]) / (depth >> 3);
 
     output     = pic->data[0] + (avctx->height - 1) * pic->linesize[0];
-    output_end = pic->data[0] +  avctx->height      * pic->linesize[0];
+    output_end = output + FFABS(pic->linesize[0]);
+
     while (bytestream2_get_bytes_left(gb) > 0) {
         p1 = bytestream2_get_byteu(gb);
         if(p1 == 0) { //Escape code
@@ -155,6 +156,7 @@
                     }
                 }
                 output = pic->data[0] + line * pic->linesize[0];
+                output_end = output + FFABS(pic->linesize[0]);
                 pos = 0;
                 continue;
             } else if(p2 == 1) { //End-of-picture
@@ -169,11 +171,11 @@
                     return -1;
                 }
                 output = pic->data[0] + line * pic->linesize[0] + pos * (depth >> 3);
+                output_end = pic->data[0] + line * pic->linesize[0] + FFABS(pic->linesize[0]);
                 continue;
             }
             // Copy data
-            if ((pic->linesize[0] > 0 && output + p2 * (depth >> 3) > output_end) ||
-                (pic->linesize[0] < 0 && output + p2 * (depth >> 3) < output_end)) {
+            if (output + p2 * (depth >> 3) > output_end) {
                 bytestream2_skip(gb, 2 * (depth >> 3));
                 continue;
             } else if (bytestream2_get_bytes_left(gb) < p2 * (depth >> 3)) {
@@ -203,8 +205,7 @@
             pos += p2;
         } else { //run of pixels
             uint8_t pix[3]; //original pixel
-            if ((pic->linesize[0] > 0 && output + p1 * (depth >> 3) > output_end) ||
-                (pic->linesize[0] < 0 && output + p1 * (depth >> 3) < output_end))
+            if (output + p1 * (depth >> 3) > output_end)
                 continue;
 
             switch(depth){
diff --git a/libavcodec/mss1.c b/libavcodec/mss1.c
index 446339b..bb1858d 100644
--- a/libavcodec/mss1.c
+++ b/libavcodec/mss1.c
@@ -25,6 +25,7 @@
  */
 
 #include "avcodec.h"
+#include "internal.h"
 #include "mss12.h"
 
 typedef struct MSS1Context {
@@ -150,13 +151,8 @@
     init_get_bits(&gb, buf, buf_size * 8);
     arith_init(&acoder, &gb);
 
-    ctx->pic.reference    = 3;
-    ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
-                            FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
-    if ((ret = avctx->reget_buffer(avctx, &ctx->pic)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, &ctx->pic)) < 0)
         return ret;
-    }
 
     c->pal_pic    =  ctx->pic.data[0] + ctx->pic.linesize[0] * (avctx->height - 1);
     c->pal_stride = -ctx->pic.linesize[0];
@@ -180,8 +176,10 @@
     memcpy(ctx->pic.data[1], c->pal, AVPALETTE_SIZE);
     ctx->pic.palette_has_changed = pal_changed;
 
+    if ((ret = av_frame_ref(data, &ctx->pic)) < 0)
+        return ret;
+
     *got_frame      = 1;
-    *(AVFrame*)data = ctx->pic;
 
     /* always report that the buffer was completely consumed */
     return buf_size;
@@ -193,7 +191,8 @@
     int ret;
 
     c->ctx.avctx       = avctx;
-    avctx->coded_frame = &c->pic;
+
+    avcodec_get_frame_defaults(&c->pic);
 
     ret = ff_mss12_decode_init(&c->ctx, 0, &c->sc, NULL);
 
@@ -206,8 +205,7 @@
 {
     MSS1Context * const ctx = avctx->priv_data;
 
-    if (ctx->pic.data[0])
-        avctx->release_buffer(avctx, &ctx->pic);
+    av_frame_unref(&ctx->pic);
     ff_mss12_decode_end(&ctx->ctx);
 
     return 0;
diff --git a/libavcodec/mss2.c b/libavcodec/mss2.c
index afdf33c..fcdbbd2 100644
--- a/libavcodec/mss2.c
+++ b/libavcodec/mss2.c
@@ -24,6 +24,7 @@
  */
 
 #include "libavutil/avassert.h"
+#include "error_resilience.h"
 #include "internal.h"
 #include "msmpeg4data.h"
 #include "vc1.h"
@@ -33,7 +34,6 @@
 typedef struct MSS2Context {
     VC1Context     v;
     int            split_position;
-    AVFrame        pic;
     AVFrame        last_pic;
     MSS12Context   c;
     MSS2DSPContext dsp;
@@ -430,8 +430,8 @@
         ctx->dsp.upsample_plane(f->data[1], f->linesize[1], w >> 1, h >> 1);
         ctx->dsp.upsample_plane(f->data[2], f->linesize[2], w >> 1, h >> 1);
     } else if (v->respic)
-        av_log_ask_for_sample(v->s.avctx,
-                              "Asymmetric WMV9 rectangle subsampling\n");
+        avpriv_request_sample(v->s.avctx,
+                              "Asymmetric WMV9 rectangle subsampling");
 
     av_assert0(f->linesize[1] == f->linesize[2]);
 
@@ -469,6 +469,7 @@
     int buf_size       = avpkt->size;
     MSS2Context *ctx = avctx->priv_data;
     MSS12Context *c  = &ctx->c;
+    AVFrame *frame   = data;
     GetBitContext gb;
     GetByteContext gB;
     ArithCoder acoder;
@@ -522,8 +523,8 @@
         return AVERROR_INVALIDDATA;
 
     avctx->pix_fmt = is_555 ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_RGB24;
-    if (ctx->pic.data[0] && ctx->pic.format != avctx->pix_fmt)
-        avctx->release_buffer(avctx, &ctx->pic);
+    if (ctx->last_pic.format != avctx->pix_fmt)
+        av_frame_unref(&ctx->last_pic);
 
     if (has_wmv9) {
         bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
@@ -595,25 +596,13 @@
     }
 
     if (c->mvX < 0 || c->mvY < 0) {
-        FFSWAP(AVFrame, ctx->pic, ctx->last_pic);
         FFSWAP(uint8_t *, c->pal_pic, c->last_pal_pic);
 
-        if (ctx->pic.data[0])
-            avctx->release_buffer(avctx, &ctx->pic);
-
-        ctx->pic.reference    = 3;
-        ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID    |
-                                FF_BUFFER_HINTS_READABLE |
-                                FF_BUFFER_HINTS_PRESERVE |
-                                FF_BUFFER_HINTS_REUSABLE;
-
-        if ((ret = ff_get_buffer(avctx, &ctx->pic)) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
             return ret;
-        }
 
         if (ctx->last_pic.data[0]) {
-            av_assert0(ctx->pic.linesize[0] == ctx->last_pic.linesize[0]);
+            av_assert0(frame->linesize[0] == ctx->last_pic.linesize[0]);
             c->last_rgb_pic = ctx->last_pic.data[0] +
                               ctx->last_pic.linesize[0] * (avctx->height - 1);
         } else {
@@ -621,28 +610,19 @@
             return AVERROR_INVALIDDATA;
         }
     } else {
-        if (ctx->last_pic.data[0])
-            avctx->release_buffer(avctx, &ctx->last_pic);
-
-        ctx->pic.reference    = 3;
-        ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID    |
-                                FF_BUFFER_HINTS_READABLE |
-                                FF_BUFFER_HINTS_PRESERVE |
-                                FF_BUFFER_HINTS_REUSABLE;
-
-        if ((ret = avctx->reget_buffer(avctx, &ctx->pic)) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+        if ((ret = ff_reget_buffer(avctx, &ctx->last_pic)) < 0)
             return ret;
-        }
+        if ((ret = av_frame_ref(frame, &ctx->last_pic)) < 0)
+            return ret;
 
         c->last_rgb_pic = NULL;
     }
-    c->rgb_pic    = ctx->pic.data[0] +
-                    ctx->pic.linesize[0] * (avctx->height - 1);
-    c->rgb_stride = -ctx->pic.linesize[0];
+    c->rgb_pic    = frame->data[0] +
+                    frame->linesize[0] * (avctx->height - 1);
+    c->rgb_stride = -frame->linesize[0];
 
-    ctx->pic.key_frame = keyframe;
-    ctx->pic.pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
+    frame->key_frame = keyframe;
+    frame->pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
 
     if (is_555) {
         bytestream2_init(&gB, buf, buf_size);
@@ -745,8 +725,14 @@
     if (buf_size)
         av_log(avctx, AV_LOG_WARNING, "buffer not fully consumed\n");
 
+    if (c->mvX < 0 || c->mvY < 0) {
+        av_frame_unref(&ctx->last_pic);
+        ret = av_frame_ref(&ctx->last_pic, frame);
+        if (ret < 0)
+            return ret;
+    }
+
     *got_frame       = 1;
-    *(AVFrame *)data = ctx->pic;
 
     return avpkt->size;
 }
@@ -816,10 +802,7 @@
 {
     MSS2Context *const ctx = avctx->priv_data;
 
-    if (ctx->pic.data[0])
-        avctx->release_buffer(avctx, &ctx->pic);
-    if (ctx->last_pic.data[0])
-        avctx->release_buffer(avctx, &ctx->last_pic);
+    av_frame_unref(&ctx->last_pic);
 
     ff_mss12_decode_end(&ctx->c);
     av_freep(&ctx->c.pal_pic);
@@ -835,7 +818,6 @@
     MSS12Context *c = &ctx->c;
     int ret;
     c->avctx = avctx;
-    avctx->coded_frame = &ctx->pic;
     if (ret = ff_mss12_decode_init(c, 1, &ctx->sc[0], &ctx->sc[1]))
         return ret;
     c->pal_stride   = c->mask_stride;
diff --git a/libavcodec/mss2dsp.c b/libavcodec/mss2dsp.c
index b18bf1f..c5fc1f8 100644
--- a/libavcodec/mss2dsp.c
+++ b/libavcodec/mss2dsp.c
@@ -106,6 +106,9 @@
     uint8_t *src1, *src2, *dst1, *dst2, *p, a, b;
     int i, j;
 
+    if(!w || !h)
+        return;
+
     w += (w & 1);
     h += (h & 1);
 
diff --git a/libavcodec/mss3.c b/libavcodec/mss3.c
index d5bb2d4..3cc484f 100644
--- a/libavcodec/mss3.c
+++ b/libavcodec/mss3.c
@@ -27,6 +27,7 @@
 #include "avcodec.h"
 #include "bytestream.h"
 #include "dsputil.h"
+#include "internal.h"
 #include "mss34dsp.h"
 
 #define HEADER_SIZE 27
@@ -730,18 +731,14 @@
         return buf_size;
     c->got_error = 0;
 
-    c->pic.reference    = 3;
-    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
-                          FF_BUFFER_HINTS_REUSABLE;
-    if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, &c->pic)) < 0)
         return ret;
-    }
     c->pic.key_frame = keyframe;
     c->pic.pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
     if (!bytestream2_get_bytes_left(&gb)) {
+        if ((ret = av_frame_ref(data, &c->pic)) < 0)
+            return ret;
         *got_frame      = 1;
-        *(AVFrame*)data = c->pic;
 
         return buf_size;
     }
@@ -798,8 +795,10 @@
         dst[2] += c->pic.linesize[2] * 8;
     }
 
+    if ((ret = av_frame_ref(data, &c->pic)) < 0)
+        return ret;
+
     *got_frame      = 1;
-    *(AVFrame*)data = c->pic;
 
     return buf_size;
 }
@@ -836,7 +835,6 @@
     }
 
     avctx->pix_fmt     = AV_PIX_FMT_YUV420P;
-    avctx->coded_frame = &c->pic;
 
     init_coders(c);
 
@@ -848,8 +846,7 @@
     MSS3Context * const c = avctx->priv_data;
     int i;
 
-    if (c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
+    av_frame_unref(&c->pic);
     for (i = 0; i < 3; i++)
         av_freep(&c->dct_coder[i].prev_dc);
 
diff --git a/libavcodec/mss4.c b/libavcodec/mss4.c
index 977e1e5..c518fda 100644
--- a/libavcodec/mss4.c
+++ b/libavcodec/mss4.c
@@ -29,6 +29,7 @@
 #include "bytestream.h"
 #include "dsputil.h"
 #include "get_bits.h"
+#include "internal.h"
 #include "mss34dsp.h"
 #include "unary.h"
 
@@ -125,7 +126,7 @@
 #define MAX_ENTRIES  162
 
 typedef struct MSS4Context {
-    AVFrame    pic;
+    AVFrame    *pic;
 
     VLC        dc_vlc[2], ac_vlc[2];
     VLC        vec_entry_vlc[2];
@@ -296,10 +297,10 @@
                 return ret;
             c->prev_dc[0][mb_x * 2 + i] = c->dc_cache[j][LEFT];
 
-            ff_mss34_dct_put(out + xpos * 8, c->pic.linesize[0],
+            ff_mss34_dct_put(out + xpos * 8, c->pic->linesize[0],
                              c->block);
         }
-        out += 8 * c->pic.linesize[0];
+        out += 8 * c->pic->linesize[0];
     }
 
     for (i = 1; i < 3; i++) {
@@ -319,7 +320,7 @@
         for (j = 0; j < 16; j++) {
             for (k = 0; k < 8; k++)
                 AV_WN16A(out + k * 2, c->imgbuf[i][k + (j & ~1) * 4] * 0x101);
-            out += c->pic.linesize[i];
+            out += c->pic->linesize[i];
         }
     }
 
@@ -480,7 +481,7 @@
 
     for (i = 0; i < 3; i++)
         for (j = 0; j < 16; j++)
-            memcpy(picdst[i] + mb_x * 16 + j * ctx->pic.linesize[i],
+            memcpy(picdst[i] + mb_x * 16 + j * ctx->pic->linesize[i],
                    ctx->imgbuf[i] + j * 16, 16);
 
     return 0;
@@ -553,20 +554,15 @@
         return AVERROR_INVALIDDATA;
     }
 
-    c->pic.reference    = 3;
-    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID    |
-                          FF_BUFFER_HINTS_PRESERVE |
-                          FF_BUFFER_HINTS_REUSABLE;
-    if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
         return ret;
-    }
-    c->pic.key_frame = (frame_type == INTRA_FRAME);
-    c->pic.pict_type = (frame_type == INTRA_FRAME) ? AV_PICTURE_TYPE_I
+    c->pic->key_frame = (frame_type == INTRA_FRAME);
+    c->pic->pict_type = (frame_type == INTRA_FRAME) ? AV_PICTURE_TYPE_I
                                                    : AV_PICTURE_TYPE_P;
     if (frame_type == SKIP_FRAME) {
         *got_frame      = 1;
-        *(AVFrame*)data = c->pic;
+        if ((ret = av_frame_ref(data, c->pic)) < 0)
+            return ret;
 
         return buf_size;
     }
@@ -577,13 +573,13 @@
             ff_mss34_gen_quant_mat(c->quant_mat[i], quality, !i);
     }
 
-    init_get_bits(&gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
+    init_get_bits8(&gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE));
 
     mb_width  = FFALIGN(width,  16) >> 4;
     mb_height = FFALIGN(height, 16) >> 4;
-    dst[0] = c->pic.data[0];
-    dst[1] = c->pic.data[1];
-    dst[2] = c->pic.data[2];
+    dst[0] = c->pic->data[0];
+    dst[1] = c->pic->data[1];
+    dst[2] = c->pic->data[2];
 
     memset(c->prev_vec, 0, sizeof(c->prev_vec));
     for (y = 0; y < mb_height; y++) {
@@ -617,13 +613,15 @@
             if (blk_type != DCT_BLOCK)
                 mss4_update_dc_cache(c, x);
         }
-        dst[0] += c->pic.linesize[0] * 16;
-        dst[1] += c->pic.linesize[1] * 16;
-        dst[2] += c->pic.linesize[2] * 16;
+        dst[0] += c->pic->linesize[0] * 16;
+        dst[1] += c->pic->linesize[1] * 16;
+        dst[2] += c->pic->linesize[2] * 16;
     }
 
+    if ((ret = av_frame_ref(data, c->pic)) < 0)
+        return ret;
+
     *got_frame      = 1;
-    *(AVFrame*)data = c->pic;
 
     return buf_size;
 }
@@ -633,6 +631,10 @@
     MSS4Context * const c = avctx->priv_data;
     int i;
 
+    c->pic = av_frame_alloc();
+    if (!c->pic)
+        return AVERROR(ENOMEM);
+
     if (mss4_init_vlcs(c)) {
         av_log(avctx, AV_LOG_ERROR, "Cannot initialise VLCs\n");
         mss4_free_vlcs(c);
@@ -649,7 +651,6 @@
     }
 
     avctx->pix_fmt     = AV_PIX_FMT_YUV444P;
-    avctx->coded_frame = &c->pic;
 
     return 0;
 }
@@ -659,8 +660,8 @@
     MSS4Context * const c = avctx->priv_data;
     int i;
 
-    if (c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
+    av_frame_free(&c->pic);
+
     for (i = 0; i < 3; i++)
         av_freep(&c->prev_dc[i]);
     mss4_free_vlcs(c);
diff --git a/libavcodec/msvideo1.c b/libavcodec/msvideo1.c
index fd98b31..c5a1daa 100644
--- a/libavcodec/msvideo1.c
+++ b/libavcodec/msvideo1.c
@@ -34,6 +34,7 @@
 #include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
 #include "avcodec.h"
+#include "internal.h"
 
 #define PALETTE_COUNT 256
 #define CHECK_STREAM_PTR(n) \
@@ -72,7 +73,6 @@
     }
 
     avcodec_get_frame_defaults(&s->frame);
-    s->frame.data[0] = NULL;
 
     return 0;
 }
@@ -293,16 +293,13 @@
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     Msvideo1Context *s = avctx->priv_data;
+    int ret;
 
     s->buf = buf;
     s->size = buf_size;
 
-    s->frame.reference = 3;
-    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
-    if (avctx->reget_buffer(avctx, &s->frame)) {
-        av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
-        return -1;
-    }
+    if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0)
+        return ret;
 
     if (s->mode_8bit) {
         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
@@ -318,8 +315,10 @@
     else
         msvideo1_decode_16bit(s);
 
+    if ((ret = av_frame_ref(data, &s->frame)) < 0)
+        return ret;
+
     *got_frame      = 1;
-    *(AVFrame*)data = s->frame;
 
     /* report that the buffer was completely consumed */
     return buf_size;
@@ -329,8 +328,7 @@
 {
     Msvideo1Context *s = avctx->priv_data;
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+    av_frame_unref(&s->frame);
 
     return 0;
 }
diff --git a/libavcodec/mvcdec.c b/libavcodec/mvcdec.c
index 5131ae2..c4385fe 100644
--- a/libavcodec/mvcdec.c
+++ b/libavcodec/mvcdec.c
@@ -27,8 +27,10 @@
 #include "libavutil/intreadwrite.h"
 #include "avcodec.h"
 #include "bytestream.h"
+#include "internal.h"
 
 typedef struct MvcContext {
+    AVFrame *frame;
     int vflip;
 } MvcContext;
 
@@ -48,8 +50,8 @@
         avcodec_set_dimensions(avctx, width, height);
 
     avctx->pix_fmt = (avctx->codec_id == AV_CODEC_ID_MVC1) ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_BGRA;
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame)
+    s->frame = av_frame_alloc();
+    if (!s->frame)
         return AVERROR(ENOMEM);
 
     s->vflip = avctx->extradata_size >= 9 && !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9);
@@ -148,7 +150,7 @@
         av_log(avctx, AV_LOG_WARNING, "dimension mismatch\n");
 
     if (bytestream2_get_byteu(gb)) {
-        av_log_ask_for_sample(avctx, "bitmap feature\n");
+        avpriv_request_sample(avctx, "bitmap feature");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -228,33 +230,30 @@
     GetByteContext gb;
     int ret;
 
-    avctx->coded_frame->reference = 3;
-    avctx->coded_frame->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
-                            FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
-    ret = avctx->reget_buffer(avctx, avctx->coded_frame);
-    if (ret < 0) {
-        av_log (avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
-        return AVERROR(ENOMEM);
-    }
+    if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
+        return ret;
 
     bytestream2_init(&gb, avpkt->data, avpkt->size);
     if (avctx->codec_id == AV_CODEC_ID_MVC1)
-        ret = decode_mvc1(avctx, &gb, avctx->coded_frame->data[0], avctx->width, avctx->height, avctx->coded_frame->linesize[0]);
+        ret = decode_mvc1(avctx, &gb, s->frame->data[0], avctx->width, avctx->height, s->frame->linesize[0]);
     else
-        ret = decode_mvc2(avctx, &gb, avctx->coded_frame->data[0], avctx->width, avctx->height, avctx->coded_frame->linesize[0], s->vflip);
+        ret = decode_mvc2(avctx, &gb, s->frame->data[0], avctx->width, avctx->height, s->frame->linesize[0], s->vflip);
     if (ret < 0)
         return ret;
 
-    *got_frame      = 1;
-    *(AVFrame*)data = *avctx->coded_frame;
+    *got_frame = 1;
+    if ((ret = av_frame_ref(data, s->frame)) < 0)
+        return ret;
+
     return avpkt->size;
 }
 
 static av_cold int mvc_decode_end(AVCodecContext *avctx)
 {
-    if (avctx->coded_frame->data[0])
-        avctx->release_buffer(avctx, avctx->coded_frame);
-    av_freep(&avctx->coded_frame);
+    MvcContext *s = avctx->priv_data;
+
+    av_frame_free(&s->frame);
+
     return 0;
 }
 
diff --git a/libavcodec/mxpegdec.c b/libavcodec/mxpegdec.c
index 59d41b6..0069ca5 100644
--- a/libavcodec/mxpegdec.c
+++ b/libavcodec/mxpegdec.c
@@ -46,7 +46,6 @@
 {
     MXpegDecodeContext *s = avctx->priv_data;
 
-    s->picture[0].reference = s->picture[1].reference = 3;
     s->jpg.picture_ptr      = &s->picture[0];
     return ff_mjpeg_decode_init(avctx);
 }
@@ -168,7 +167,6 @@
     const uint8_t *unescaped_buf_ptr;
     int unescaped_buf_size;
     int start_code;
-    AVFrame *picture = data;
     int ret;
 
     buf_ptr = buf;
@@ -249,12 +247,10 @@
                         break;
                     }
                     /* use stored SOF data to allocate current picture */
-                    if (jpg->picture_ptr->data[0])
-                        avctx->release_buffer(avctx, jpg->picture_ptr);
-                    if (ff_get_buffer(avctx, jpg->picture_ptr) < 0) {
-                        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
-                        return AVERROR(ENOMEM);
-                    }
+                    av_frame_unref(jpg->picture_ptr);
+                    if ((ret = ff_get_buffer(avctx, jpg->picture_ptr,
+                                             AV_GET_BUFFER_FLAG_REF)) < 0)
+                        return ret;
                     jpg->picture_ptr->pict_type = AV_PICTURE_TYPE_P;
                     jpg->picture_ptr->key_frame = 0;
                     jpg->got_picture = 1;
@@ -270,10 +266,9 @@
 
                     /* allocate dummy reference picture if needed */
                     if (!reference_ptr->data[0] &&
-                        ff_get_buffer(avctx, reference_ptr) < 0) {
-                        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
-                        return AVERROR(ENOMEM);
-                    }
+                        (ret = ff_get_buffer(avctx, reference_ptr,
+                                             AV_GET_BUFFER_FLAG_REF)) < 0)
+                        return ret;
 
                     ret = ff_mjpeg_decode_sos(jpg, s->mxm_bitmask, reference_ptr);
                     if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
@@ -294,8 +289,11 @@
 
 the_end:
     if (jpg->got_picture) {
+        int ret = av_frame_ref(data, jpg->picture_ptr);
+        if (ret < 0)
+            return ret;
         *got_frame = 1;
-        *picture = *jpg->picture_ptr;
+
         s->picture_index ^= 1;
         jpg->picture_ptr = &s->picture[s->picture_index];
 
@@ -319,10 +317,8 @@
     jpg->picture_ptr = NULL;
     ff_mjpeg_decode_end(avctx);
 
-    for (i = 0; i < 2; ++i) {
-        if (s->picture[i].data[0])
-            avctx->release_buffer(avctx, &s->picture[i]);
-    }
+    for (i = 0; i < 2; ++i)
+        av_frame_unref(&s->picture[i]);
 
     av_freep(&s->mxm_bitmask);
     av_freep(&s->completion_bitmask);
diff --git a/libavcodec/nellymoserdec.c b/libavcodec/nellymoserdec.c
index 4b4b61c..42110a2 100644
--- a/libavcodec/nellymoserdec.c
+++ b/libavcodec/nellymoserdec.c
@@ -171,10 +171,8 @@
 
     /* get output buffer */
     frame->nb_samples = NELLY_SAMPLES * blocks;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     samples_flt = (float *)frame->data[0];
 
     for (i=0 ; i<blocks ; i++) {
diff --git a/libavcodec/nellymoserenc.c b/libavcodec/nellymoserenc.c
index 121bd38..b35820a 100644
--- a/libavcodec/nellymoserenc.c
+++ b/libavcodec/nellymoserenc.c
@@ -140,9 +140,6 @@
         av_free(s->path);
     }
     ff_af_queue_close(&s->afq);
-#if FF_API_OLD_ENCODE_AUDIO
-    av_freep(&avctx->coded_frame);
-#endif
 
     return 0;
 }
@@ -187,14 +184,6 @@
         }
     }
 
-#if FF_API_OLD_ENCODE_AUDIO
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame) {
-        ret = AVERROR(ENOMEM);
-        goto error;
-    }
-#endif
-
     return 0;
 error:
     encode_end(avctx);
@@ -401,7 +390,7 @@
         s->last_frame = 1;
     }
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, NELLY_BLOCK_LEN)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, NELLY_BLOCK_LEN)) < 0)
         return ret;
     encode_block(s, avpkt->data, avpkt->size);
 
diff --git a/libavcodec/noise_bsf.c b/libavcodec/noise_bsf.c
index 763af79..c91e85b 100644
--- a/libavcodec/noise_bsf.c
+++ b/libavcodec/noise_bsf.c
@@ -36,6 +36,8 @@
         return AVERROR(EINVAL);
 
     *poutbuf= av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
+    if (!*poutbuf)
+        return AVERROR(ENOMEM);
 
     memcpy(*poutbuf, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
     for(i=0; i<buf_size; i++){
diff --git a/libavcodec/nuv.c b/libavcodec/nuv.c
index dffe339..edf777e 100644
--- a/libavcodec/nuv.c
+++ b/libavcodec/nuv.c
@@ -28,6 +28,7 @@
 #include "libavutil/lzo.h"
 #include "libavutil/imgutils.h"
 #include "avcodec.h"
+#include "internal.h"
 #include "rtjpeg.h"
 
 typedef struct {
@@ -240,18 +241,13 @@
         buf_size -= RTJPEG_HEADER_SIZE;
     }
 
-    if ((size_change || keyframe) && c->pic.data[0]) {
-        avctx->release_buffer(avctx, &c->pic);
+    if (size_change || keyframe) {
+        av_frame_unref(&c->pic);
         init_frame = 1;
     }
-    c->pic.reference    = 3;
-    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID    | FF_BUFFER_HINTS_READABLE |
-                          FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
-    result = avctx->reget_buffer(avctx, &c->pic);
-    if (result < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+
+    if ((result = ff_reget_buffer(avctx, &c->pic)) < 0)
         return result;
-    }
     if (init_frame) {
         memset(c->pic.data[0], 0,    avctx->height * c->pic.linesize[0]);
         memset(c->pic.data[1], 0x80, avctx->height * c->pic.linesize[1] / 2);
@@ -290,7 +286,9 @@
         return AVERROR_INVALIDDATA;
     }
 
-    *picture   = c->pic;
+    if ((result = av_frame_ref(picture, &c->pic)) < 0)
+        return result;
+
     *got_frame = 1;
     return orig_size;
 }
@@ -325,8 +323,7 @@
     NuvContext *c = avctx->priv_data;
 
     av_freep(&c->decomp_buf);
-    if (c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
+    av_frame_unref(&c->pic);
 
     return 0;
 }
diff --git a/libavcodec/old_codec_ids.h b/libavcodec/old_codec_ids.h
index ded4cc7..d8a8f74 100644
--- a/libavcodec/old_codec_ids.h
+++ b/libavcodec/old_codec_ids.h
@@ -88,7 +88,6 @@
     CODEC_ID_MSZH,
     CODEC_ID_ZLIB,
     CODEC_ID_QTRLE,
-    CODEC_ID_SNOW,
     CODEC_ID_TSCC,
     CODEC_ID_ULTI,
     CODEC_ID_QDRAW,
@@ -213,6 +212,7 @@
     CODEC_ID_YUV4       = MKBETAG('Y','U','V','4'),
     CODEC_ID_SANM       = MKBETAG('S','A','N','M'),
     CODEC_ID_PAF_VIDEO  = MKBETAG('P','A','F','V'),
+    CODEC_ID_SNOW       = AV_CODEC_ID_SNOW,
 
     /* various PCM "codecs" */
     CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
@@ -355,7 +355,6 @@
     CODEC_ID_IAC,
     CODEC_ID_ILBC,
     CODEC_ID_FFWAVESYNTH = MKBETAG('F','F','W','S'),
-    CODEC_ID_8SVX_RAW    = MKBETAG('8','S','V','X'),
     CODEC_ID_SONIC       = MKBETAG('S','O','N','C'),
     CODEC_ID_SONIC_LS    = MKBETAG('S','O','N','L'),
     CODEC_ID_PAF_AUDIO   = MKBETAG('P','A','F','A'),
diff --git a/libavcodec/options.c b/libavcodec/options.c
index a922365..e1349dd 100644
--- a/libavcodec/options.c
+++ b/libavcodec/options.c
@@ -102,6 +102,9 @@
     s->av_class = &av_codec_context_class;
 
     s->codec_type = codec ? codec->type : AVMEDIA_TYPE_UNKNOWN;
+    if (codec)
+        s->codec_id = codec->id;
+
     if(s->codec_type == AVMEDIA_TYPE_AUDIO)
         flags= AV_OPT_FLAG_AUDIO_PARAM;
     else if(s->codec_type == AVMEDIA_TYPE_VIDEO)
@@ -111,8 +114,7 @@
     av_opt_set_defaults2(s, flags, flags);
 
     s->time_base           = (AVRational){0,1};
-    s->get_buffer          = avcodec_default_get_buffer;
-    s->release_buffer      = avcodec_default_release_buffer;
+    s->get_buffer2         = avcodec_default_get_buffer2;
     s->get_format          = avcodec_default_get_format;
     s->execute             = avcodec_default_execute;
     s->execute2            = avcodec_default_execute2;
@@ -121,7 +123,6 @@
     s->sample_fmt          = AV_SAMPLE_FMT_NONE;
     s->timecode_frame_start = -1;
 
-    s->reget_buffer        = avcodec_default_reget_buffer;
     s->reordered_opaque    = AV_NOPTS_VALUE;
     if(codec && codec->priv_data_size){
         if(!s->priv_data){
diff --git a/libavcodec/options_table.h b/libavcodec/options_table.h
index d0c55a2..1be2611 100644
--- a/libavcodec/options_table.h
+++ b/libavcodec/options_table.h
@@ -49,6 +49,7 @@
        "to minimum/maximum bitrate. Lowering tolerance too much has an adverse effect on quality.",
        OFFSET(bit_rate_tolerance), AV_OPT_TYPE_INT, {.i64 = AV_CODEC_DEFAULT_BITRATE*20 }, 1, INT_MAX, V|E},
 {"flags", NULL, OFFSET(flags), AV_OPT_TYPE_FLAGS, {.i64 = DEFAULT }, 0, UINT_MAX, V|A|S|E|D, "flags"},
+{"unaligned", "allow decoders to produce unaligned output", 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_FLAG_UNALIGNED }, INT_MIN, INT_MAX, V | D, "flags" },
 {"mv4", "use four motion vectors per macroblock (MPEG-4)", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC_FLAG_4MV }, INT_MIN, INT_MAX, V|E, "flags"},
 {"qpel", "use 1/4-pel motion compensation", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC_FLAG_QPEL }, INT_MIN, INT_MAX, V|E, "flags"},
 {"loop", "use loop filter", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC_FLAG_LOOP_FILTER }, INT_MIN, INT_MAX, V|E, "flags"},
@@ -68,24 +69,14 @@
 {"global_header", "place global headers in extradata instead of every keyframe", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC_FLAG_GLOBAL_HEADER }, INT_MIN, INT_MAX, V|A|E, "flags"},
 {"bitexact", "use only bitexact functions (except (I)DCT)", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC_FLAG_BITEXACT }, INT_MIN, INT_MAX, A|V|S|D|E, "flags"},
 {"aic", "H.263 advanced intra coding / MPEG-4 AC prediction", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC_FLAG_AC_PRED }, INT_MIN, INT_MAX, V|E, "flags"},
-#if FF_API_MPV_GLOBAL_OPTS
-{"cbp", "Deprecated, use mpegvideo private options instead", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC_FLAG_CBP_RD }, INT_MIN, INT_MAX, V|E, "flags"},
-{"qprd", "Deprecated, use mpegvideo private options instead", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC_FLAG_QP_RD }, INT_MIN, INT_MAX, V|E, "flags"},
-#endif
 {"ilme", "interlaced motion estimation", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC_FLAG_INTERLACED_ME }, INT_MIN, INT_MAX, V|E, "flags"},
 {"cgop", "closed GOP", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC_FLAG_CLOSED_GOP }, INT_MIN, INT_MAX, V|E, "flags"},
 {"fast", "allow non-spec-compliant speedup tricks", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC_FLAG2_FAST }, INT_MIN, INT_MAX, V|E, "flags2"},
-#if FF_API_MPV_GLOBAL_OPTS
-{"sgop", "Deprecated, use mpegvideo private options instead", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC_FLAG2_STRICT_GOP }, INT_MIN, INT_MAX, V|E, "flags2"},
-#endif
 {"noout", "skip bitstream encoding", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC_FLAG2_NO_OUTPUT }, INT_MIN, INT_MAX, V|E, "flags2"},
-{"ignorecrop", "ignore cropping information from sps", 1, AV_OPT_TYPE_CONST, {.i64 = CODEC_FLAG2_IGNORE_CROP }, INT_MIN, INT_MAX, V|D, "flags2"},
+{"ignorecrop", "ignore cropping information from sps", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC_FLAG2_IGNORE_CROP }, INT_MIN, INT_MAX, V|D, "flags2"},
 {"local_header", "place global headers at every keyframe instead of in extradata", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC_FLAG2_LOCAL_HEADER }, INT_MIN, INT_MAX, V|E, "flags2"},
 {"chunks", "Frame data might be split into multiple chunks", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC_FLAG2_CHUNKS }, INT_MIN, INT_MAX, V|D, "flags2"},
 {"showall", "Show all frames before the first keyframe", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC_FLAG2_SHOW_ALL }, INT_MIN, INT_MAX, V|D, "flags2"},
-#if FF_API_SUB_ID
-{"sub_id", NULL, OFFSET(sub_id), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX},
-#endif
 {"me_method", "set motion estimation method", OFFSET(me_method), AV_OPT_TYPE_INT, {.i64 = ME_EPZS }, INT_MIN, INT_MAX, V|E, "me_method"},
 {"zero", "zero motion estimation (fastest)", 0, AV_OPT_TYPE_CONST, {.i64 = ME_ZERO }, INT_MIN, INT_MAX, V|E, "me_method" },
 {"full", "full motion estimation (slowest)", 0, AV_OPT_TYPE_CONST, {.i64 = ME_FULL }, INT_MIN, INT_MAX, V|E, "me_method" },
@@ -147,10 +138,6 @@
 {"dc_clip", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_BUG_DC_CLIP }, INT_MIN, INT_MAX, V|D, "bug"},
 {"ms", "work around various bugs in Microsoft's broken decoders", 0, AV_OPT_TYPE_CONST, {.i64 = FF_BUG_MS }, INT_MIN, INT_MAX, V|D, "bug"},
 {"trunc", "truncated frames", 0, AV_OPT_TYPE_CONST, {.i64 = FF_BUG_TRUNCATED}, INT_MIN, INT_MAX, V|D, "bug"},
-#if FF_API_MPV_GLOBAL_OPTS
-{"lelim", "single coefficient elimination threshold for luminance (negative values also consider DC coefficient)", OFFSET(luma_elim_threshold), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, V|E},
-{"celim", "single coefficient elimination threshold for chrominance (negative values also consider DC coefficient)", OFFSET(chroma_elim_threshold), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, V|E},
-#endif
 {"strict", "how strictly to follow the standards", OFFSET(strict_std_compliance), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, A|V|D|E, "strict"},
 {"very", "strictly conform to a older more strict version of the spec or reference software", 0, AV_OPT_TYPE_CONST, {.i64 = FF_COMPLIANCE_VERY_STRICT }, INT_MIN, INT_MAX, V|D|E, "strict"},
 {"strict", "strictly conform to all the things in the spec no matter what the consequences", 0, AV_OPT_TYPE_CONST, {.i64 = FF_COMPLIANCE_STRICT }, INT_MIN, INT_MAX, V|D|E, "strict"},
@@ -203,10 +190,6 @@
 {"int", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_IDCT_INT }, INT_MIN, INT_MAX, V|E|D, "idct"},
 {"simple", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_IDCT_SIMPLE }, INT_MIN, INT_MAX, V|E|D, "idct"},
 {"simplemmx", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_IDCT_SIMPLEMMX }, INT_MIN, INT_MAX, V|E|D, "idct"},
-{"libmpeg2mmx", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_IDCT_LIBMPEG2MMX }, INT_MIN, INT_MAX, V|E|D, "idct"},
-#if FF_API_MMI
-{"mmi", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_IDCT_MMI }, INT_MIN, INT_MAX, V|E|D, "idct"},
-#endif
 {"arm", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_IDCT_ARM }, INT_MIN, INT_MAX, V|E|D, "idct"},
 {"altivec", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_IDCT_ALTIVEC }, INT_MIN, INT_MAX, V|E|D, "idct"},
 {"sh4", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_IDCT_SH4 }, INT_MIN, INT_MAX, V|E|D, "idct"},
@@ -215,10 +198,6 @@
 {"simplearmv6", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_IDCT_SIMPLEARMV6 }, INT_MIN, INT_MAX, V|E|D, "idct"},
 {"simpleneon", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_IDCT_SIMPLENEON }, INT_MIN, INT_MAX, V|E|D, "idct"},
 {"simplealpha", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_IDCT_SIMPLEALPHA }, INT_MIN, INT_MAX, V|E|D, "idct"},
-#if FF_API_IDCT
-{"h264", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_IDCT_H264 }, INT_MIN, INT_MAX, V|E|D, "idct"},
-{"vp3", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_IDCT_VP3 }, INT_MIN, INT_MAX, V|E|D, "idct"},
-#endif
 {"ipp", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_IDCT_IPP }, INT_MIN, INT_MAX, V|E|D, "idct"},
 {"xvidmmx", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_IDCT_XVIDMMX }, INT_MIN, INT_MAX, V|E|D, "idct"},
 {"faani", "floating point AAN IDCT", 0, AV_OPT_TYPE_CONST, {.i64 = FF_IDCT_FAAN }, INT_MIN, INT_MAX, V|D|E, "idct"},
@@ -285,9 +264,6 @@
 {"me_range", "limit motion vectors range (1023 for DivX player)", OFFSET(me_range), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, V|E},
 {"ibias", "intra quant bias", OFFSET(intra_quant_bias), AV_OPT_TYPE_INT, {.i64 = FF_DEFAULT_QUANT_BIAS }, INT_MIN, INT_MAX, V|E},
 {"pbias", "inter quant bias", OFFSET(inter_quant_bias), AV_OPT_TYPE_INT, {.i64 = FF_DEFAULT_QUANT_BIAS }, INT_MIN, INT_MAX, V|E},
-#if FF_API_COLOR_TABLE_ID
-{"color_table_id", NULL, OFFSET(color_table_id), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX},
-#endif
 {"global_quality", NULL, OFFSET(global_quality), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, V|A|E},
 {"coder", NULL, OFFSET(coder_type), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, V|E, "coder"},
 {"vlc", "variable length coder / Huffman coder", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CODER_TYPE_VLC }, INT_MIN, INT_MAX, V|E, "coder"},
@@ -308,14 +284,8 @@
 {"lmax", "maximum Lagrange factor (VBR)", OFFSET(lmax), AV_OPT_TYPE_INT, {.i64 = 31*FF_QP2LAMBDA }, 0, INT_MAX, V|E},
 {"nr", "noise reduction", OFFSET(noise_reduction), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, V|E},
 {"rc_init_occupancy", "number of bits which should be loaded into the rc buffer before decoding starts", OFFSET(rc_initial_buffer_occupancy), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, V|E},
-#if FF_API_INTER_THRESHOLD
-{"inter_threshold", NULL, OFFSET(inter_threshold), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, V|E},
-#endif
 {"flags2", NULL, OFFSET(flags2), AV_OPT_TYPE_FLAGS, {.i64 = DEFAULT}, 0, UINT_MAX, V|A|E|D, "flags2"},
 {"error", NULL, OFFSET(error_rate), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, V|E},
-#if FF_API_MPV_GLOBAL_OPTS
-{"qns", "deprecated, use mpegvideo private options instead", OFFSET(quantizer_noise_shaping), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, V|E},
-#endif
 {"threads", NULL, OFFSET(thread_count), AV_OPT_TYPE_INT, {.i64 = 1 }, 0, INT_MAX, V|E|D, "threads"},
 {"auto", "autodetect a suitable number of threads to use", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, INT_MIN, INT_MAX, V|E|D, "threads"},
 {"me_threshold", "motion estimation threshold", OFFSET(me_threshold), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, V|E},
@@ -350,24 +320,21 @@
 {"mblmin", "minimum macroblock Lagrange factor (VBR)", OFFSET(mb_lmin), AV_OPT_TYPE_INT, {.i64 = FF_QP2LAMBDA * 2 }, 1, FF_LAMBDA_MAX, V|E},
 {"mblmax", "maximum macroblock Lagrange factor (VBR)", OFFSET(mb_lmax), AV_OPT_TYPE_INT, {.i64 = FF_QP2LAMBDA * 31 }, 1, FF_LAMBDA_MAX, V|E},
 {"mepc", "motion estimation bitrate penalty compensation (1.0 = 256)", OFFSET(me_penalty_compensation), AV_OPT_TYPE_INT, {.i64 = 256 }, INT_MIN, INT_MAX, V|E},
-{"skip_loop_filter", NULL, OFFSET(skip_loop_filter), AV_OPT_TYPE_INT, {.i64 = AVDISCARD_DEFAULT }, INT_MIN, INT_MAX, V|D, "avdiscard"},
-{"skip_idct"       , NULL, OFFSET(skip_idct)       , AV_OPT_TYPE_INT, {.i64 = AVDISCARD_DEFAULT }, INT_MIN, INT_MAX, V|D, "avdiscard"},
-{"skip_frame"      , NULL, OFFSET(skip_frame)      , AV_OPT_TYPE_INT, {.i64 = AVDISCARD_DEFAULT }, INT_MIN, INT_MAX, V|D, "avdiscard"},
-{"none"            , NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_NONE    }, INT_MIN, INT_MAX, V|D, "avdiscard"},
-{"default"         , NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_DEFAULT }, INT_MIN, INT_MAX, V|D, "avdiscard"},
-{"noref"           , NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_NONREF  }, INT_MIN, INT_MAX, V|D, "avdiscard"},
-{"bidir"           , NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_BIDIR   }, INT_MIN, INT_MAX, V|D, "avdiscard"},
-{"nokey"           , NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_NONKEY  }, INT_MIN, INT_MAX, V|D, "avdiscard"},
-{"all"             , NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_ALL     }, INT_MIN, INT_MAX, V|D, "avdiscard"},
+{"skip_loop_filter", "skip loop filtering process for the selected frames", OFFSET(skip_loop_filter), AV_OPT_TYPE_INT, {.i64 = AVDISCARD_DEFAULT }, INT_MIN, INT_MAX, V|D, "avdiscard"},
+{"skip_idct"       , "skip IDCT/dequantization for the selected frames",    OFFSET(skip_idct),        AV_OPT_TYPE_INT, {.i64 = AVDISCARD_DEFAULT }, INT_MIN, INT_MAX, V|D, "avdiscard"},
+{"skip_frame"      , "skip decoding for the selected frames",               OFFSET(skip_frame),       AV_OPT_TYPE_INT, {.i64 = AVDISCARD_DEFAULT }, INT_MIN, INT_MAX, V|D, "avdiscard"},
+{"none"            , "discard no frame",                    0, AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_NONE    }, INT_MIN, INT_MAX, V|D, "avdiscard"},
+{"default"         , "discard useless frames",              0, AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_DEFAULT }, INT_MIN, INT_MAX, V|D, "avdiscard"},
+{"noref"           , "discard all non-reference frames",    0, AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_NONREF  }, INT_MIN, INT_MAX, V|D, "avdiscard"},
+{"bidir"           , "discard all bidirectional frames",    0, AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_BIDIR   }, INT_MIN, INT_MAX, V|D, "avdiscard"},
+{"nokey"           , "discard all frames except keyframes", 0, AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_NONKEY  }, INT_MIN, INT_MAX, V|D, "avdiscard"},
+{"all"             , "discard all frames",                  0, AV_OPT_TYPE_CONST, {.i64 = AVDISCARD_ALL     }, INT_MIN, INT_MAX, V|D, "avdiscard"},
 {"bidir_refine", "refine the two motion vectors used in bidirectional macroblocks", OFFSET(bidir_refine), AV_OPT_TYPE_INT, {.i64 = 1 }, 0, 4, V|E},
 {"brd_scale", "downscale frames for dynamic B-frame decision", OFFSET(brd_scale), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, 0, 10, V|E},
 {"keyint_min", "minimum interval between IDR-frames", OFFSET(keyint_min), AV_OPT_TYPE_INT, {.i64 = 25 }, INT_MIN, INT_MAX, V|E},
 {"refs", "reference frames to consider for motion compensation", OFFSET(refs), AV_OPT_TYPE_INT, {.i64 = 1 }, INT_MIN, INT_MAX, V|E},
 {"chromaoffset", "chroma QP offset from luma", OFFSET(chromaoffset), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, V|E},
 {"trellis", "rate-distortion optimal quantization", OFFSET(trellis), AV_OPT_TYPE_INT, {.i64 = DEFAULT }, INT_MIN, INT_MAX, V|A|E},
-#if FF_API_MPV_GLOBAL_OPTS
-{"skiprd", "Deprecated, use mpegvideo private options instead", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC_FLAG2_SKIP_RD }, INT_MIN, INT_MAX, V|E, "flags2"},
-#endif
 {"sc_factor", "multiplied by qscale for each frame and added to scene_change_score", OFFSET(scenechange_factor), AV_OPT_TYPE_INT, {.i64 = 6 }, 0, INT_MAX, V|E},
 {"mv0_threshold", NULL, OFFSET(mv0_threshold), AV_OPT_TYPE_INT, {.i64 = 256 }, 0, INT_MAX, V|E},
 {"b_sensitivity", "adjust sensitivity of b_frame_strategy 1", OFFSET(b_sensitivity), AV_OPT_TYPE_INT, {.i64 = 40 }, 1, INT_MAX, V|E},
@@ -411,6 +378,7 @@
 {"do_nothing",  NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_SUB_CHARENC_MODE_DO_NOTHING},  INT_MIN, INT_MAX, S|D, "sub_charenc_mode"},
 {"auto",        NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_SUB_CHARENC_MODE_AUTOMATIC},   INT_MIN, INT_MAX, S|D, "sub_charenc_mode"},
 {"pre_decoder", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_SUB_CHARENC_MODE_PRE_DECODER}, INT_MIN, INT_MAX, S|D, "sub_charenc_mode"},
+{"refcounted_frames", NULL, OFFSET(refcounted_frames), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, A|V|D },
 {NULL},
 };
 
diff --git a/libavcodec/paf.c b/libavcodec/paf.c
index 78a5d97..300238e 100644
--- a/libavcodec/paf.c
+++ b/libavcodec/paf.c
@@ -48,7 +48,7 @@
 };
 
 typedef struct PAFVideoDecContext {
-    AVFrame  pic;
+    AVFrame  *pic;
     GetByteContext gb;
 
     int     current_frame;
@@ -59,6 +59,19 @@
     uint8_t *opcodes;
 } PAFVideoDecContext;
 
+static av_cold int paf_vid_close(AVCodecContext *avctx)
+{
+    PAFVideoDecContext *c = avctx->priv_data;
+    int i;
+
+    av_frame_free(&c->pic);
+
+    for (i = 0; i < 4; i++)
+        av_freep(&c->frame[i]);
+
+    return 0;
+}
+
 static av_cold int paf_vid_init(AVCodecContext *avctx)
 {
     PAFVideoDecContext *c = avctx->priv_data;
@@ -71,13 +84,18 @@
 
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
 
-    avcodec_get_frame_defaults(&c->pic);
+    c->pic = av_frame_alloc();
+    if (!c->pic)
+        return AVERROR(ENOMEM);
+
     c->frame_size = FFALIGN(avctx->height, 256) * avctx->width;
     c->video_size = avctx->height * avctx->width;
     for (i = 0; i < 4; i++) {
         c->frame[i] = av_mallocz(c->frame_size);
-        if (!c->frame[i])
+        if (!c->frame[i]) {
+            paf_vid_close(avctx);
             return AVERROR(ENOMEM);
+        }
     }
 
     return 0;
@@ -251,8 +269,7 @@
     uint8_t code, *dst, *src, *end;
     int i, frame, ret;
 
-    c->pic.reference = 3;
-    if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0)
+    if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
         return ret;
 
     bytestream2_init(&c->gb, pkt->data, pkt->size);
@@ -262,17 +279,17 @@
         for (i = 0; i < 4; i++)
             memset(c->frame[i], 0, c->frame_size);
 
-        memset(c->pic.data[1], 0, AVPALETTE_SIZE);
+        memset(c->pic->data[1], 0, AVPALETTE_SIZE);
         c->current_frame = 0;
-        c->pic.key_frame = 1;
-        c->pic.pict_type = AV_PICTURE_TYPE_I;
+        c->pic->key_frame = 1;
+        c->pic->pict_type = AV_PICTURE_TYPE_I;
     } else {
-        c->pic.key_frame = 0;
-        c->pic.pict_type = AV_PICTURE_TYPE_P;
+        c->pic->key_frame = 0;
+        c->pic->pict_type = AV_PICTURE_TYPE_P;
     }
 
     if (code & 0x40) {
-        uint32_t *out = (uint32_t *)c->pic.data[1];
+        uint32_t *out = (uint32_t *)c->pic->data[1];
         int index, count;
 
         index = bytestream2_get_byte(&c->gb);
@@ -295,7 +312,7 @@
             b = b << 2 | b >> 4;
             *out++ = 0xFFU << 24 | r << 16 | g << 8 | b;
         }
-        c->pic.palette_has_changed = 1;
+        c->pic->palette_has_changed = 1;
     }
 
     switch (code & 0x0F) {
@@ -343,40 +360,27 @@
         }
         break;
     default:
-        av_log_ask_for_sample(avctx, "unknown/invalid code\n");
+        avpriv_request_sample(avctx, "unknown/invalid code");
         return AVERROR_INVALIDDATA;
     }
 
-    dst = c->pic.data[0];
+    dst = c->pic->data[0];
     src = c->frame[c->current_frame];
     for (i = 0; i < avctx->height; i++) {
         memcpy(dst, src, avctx->width);
-        dst += c->pic.linesize[0];
+        dst += c->pic->linesize[0];
         src += avctx->width;
     }
 
     c->current_frame = (c->current_frame + 1) & 3;
+    if ((ret = av_frame_ref(data, c->pic)) < 0)
+        return ret;
 
     *got_frame       = 1;
-    *(AVFrame *)data = c->pic;
 
     return pkt->size;
 }
 
-static av_cold int paf_vid_close(AVCodecContext *avctx)
-{
-    PAFVideoDecContext *c = avctx->priv_data;
-    int i;
-
-    if (c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
-
-    for (i = 0; i < 4; i++)
-        av_freep(&c->frame[i]);
-
-    return 0;
-}
-
 static av_cold int paf_aud_init(AVCodecContext *avctx)
 {
     if (avctx->channels != 2) {
@@ -404,7 +408,7 @@
         return AVERROR_INVALIDDATA;
 
     frame->nb_samples = PAF_SOUND_SAMPLES * frames;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0)
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
 
     output_samples = (int16_t *)frame->data[0];
diff --git a/libavcodec/pcm-mpeg.c b/libavcodec/pcm-mpeg.c
index 08aad95..aec056a 100644
--- a/libavcodec/pcm-mpeg.c
+++ b/libavcodec/pcm-mpeg.c
@@ -153,10 +153,8 @@
 
     /* get output buffer */
     frame->nb_samples = samples;
-    if ((retval = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((retval = ff_get_buffer(avctx, frame, 0)) < 0)
         return retval;
-    }
     dst16 = (int16_t *)frame->data[0];
     dst32 = (int32_t *)frame->data[0];
 
diff --git a/libavcodec/pcm.c b/libavcodec/pcm.c
index bae338d..83482dd 100644
--- a/libavcodec/pcm.c
+++ b/libavcodec/pcm.c
@@ -107,7 +107,7 @@
     n           = frame->nb_samples * avctx->channels;
     samples     = (const short *)frame->data[0];
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, n * sample_size)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, n * sample_size)) < 0)
         return ret;
     dst = avpkt->data;
 
@@ -356,10 +356,8 @@
 
     /* get output buffer */
     frame->nb_samples = n * samples_per_block / avctx->channels;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     samples = frame->data[0];
 
     switch (avctx->codec_id) {
diff --git a/libavcodec/pcx.c b/libavcodec/pcx.c
index e7f9c24..ba92332 100644
--- a/libavcodec/pcx.c
+++ b/libavcodec/pcx.c
@@ -28,20 +28,6 @@
 #include "get_bits.h"
 #include "internal.h"
 
-typedef struct PCXContext {
-    AVFrame picture;
-} PCXContext;
-
-static av_cold int pcx_init(AVCodecContext *avctx)
-{
-    PCXContext *s = avctx->priv_data;
-
-    avcodec_get_frame_defaults(&s->picture);
-    avctx->coded_frame= &s->picture;
-
-    return 0;
-}
-
 static void pcx_rle_decode(GetByteContext *gb, uint8_t *dst,
                            unsigned int bytes_per_scanline, int compressed)
 {
@@ -76,12 +62,9 @@
 }
 
 static int pcx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
-                            AVPacket *avpkt)
-{
-    PCXContext * const s = avctx->priv_data;
-    AVFrame *picture = data;
-    AVFrame * const p = &s->picture;
+                            AVPacket *avpkt) {
     GetByteContext gb;
+    AVFrame * const p = data;
     int compressed, xmin, ymin, xmax, ymax, ret;
     unsigned int w, h, bits_per_pixel, bytes_per_line, nplanes, stride, y, x,
                  bytes_per_scanline;
@@ -144,17 +127,12 @@
 
     bytestream2_skipu(&gb, 60);
 
-    if (p->data[0])
-        avctx->release_buffer(avctx, p);
-
     if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
         return ret;
     if (w != avctx->width || h != avctx->height)
         avcodec_set_dimensions(avctx, w, h);
-    if ((ret = ff_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
         return ret;
-    }
 
     p->pict_type = AV_PICTURE_TYPE_I;
 
@@ -239,7 +217,6 @@
         pcx_palette(&gb, (uint32_t *) p->data[1], 16);
     }
 
-    *picture = s->picture;
     *got_frame = 1;
 
 end:
@@ -247,23 +224,10 @@
     return ret;
 }
 
-static av_cold int pcx_end(AVCodecContext *avctx)
-{
-    PCXContext *s = avctx->priv_data;
-
-    if(s->picture.data[0])
-        avctx->release_buffer(avctx, &s->picture);
-
-    return 0;
-}
-
 AVCodec ff_pcx_decoder = {
     .name           = "pcx",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_PCX,
-    .priv_data_size = sizeof(PCXContext),
-    .init           = pcx_init,
-    .close          = pcx_end,
     .decode         = pcx_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("PC Paintbrush PCX image"),
diff --git a/libavcodec/pictordec.c b/libavcodec/pictordec.c
index 69d2382..bf138b2 100644
--- a/libavcodec/pictordec.c
+++ b/libavcodec/pictordec.c
@@ -31,16 +31,16 @@
 #include "internal.h"
 
 typedef struct PicContext {
-    AVFrame frame;
     int width, height;
     int nb_planes;
     GetByteContext g;
 } PicContext;
 
-static void picmemset_8bpp(PicContext *s, int value, int run, int *x, int *y)
+static void picmemset_8bpp(PicContext *s, AVFrame *frame, int value, int run,
+                           int *x, int *y)
 {
     while (run > 0) {
-        uint8_t *d = s->frame.data[0] + *y * s->frame.linesize[0];
+        uint8_t *d = frame->data[0] + *y * frame->linesize[0];
         if (*x + run >= s->width) {
             int n = s->width - *x;
             memset(d + *x, value, n);
@@ -57,7 +57,7 @@
     }
 }
 
-static void picmemset(PicContext *s, int value, int run,
+static void picmemset(PicContext *s, AVFrame *frame, int value, int run,
                       int *x, int *y, int *plane, int bits_per_plane)
 {
     uint8_t *d;
@@ -68,7 +68,7 @@
     while (run > 0) {
         int j;
         for (j = 8-bits_per_plane; j >= 0; j -= bits_per_plane) {
-            d = s->frame.data[0] + *y * s->frame.linesize[0];
+            d = frame->data[0] + *y * frame->linesize[0];
             d[*x] |= (value >> j) & mask;
             *x += 1;
             if (*x == s->width) {
@@ -97,22 +97,15 @@
     [5] = { 0, 11, 12, 15 }, // mode5, high intensity
 };
 
-static av_cold int decode_init(AVCodecContext *avctx)
-{
-    PicContext *s = avctx->priv_data;
-
-    avcodec_get_frame_defaults(&s->frame);
-    return 0;
-}
-
 static int decode_frame(AVCodecContext *avctx,
                         void *data, int *got_frame,
                         AVPacket *avpkt)
 {
     PicContext *s = avctx->priv_data;
+    AVFrame *frame = data;
     uint32_t *palette;
     int bits_per_plane, bpp, etype, esize, npal, pos_after_pal;
-    int i, x, y, plane, tmp, val;
+    int i, x, y, plane, tmp, ret, val;
 
     bytestream2_init(&s->g, avpkt->data, avpkt->size);
 
@@ -130,7 +123,7 @@
     s->nb_planes   = (tmp >> 4) + 1;
     bpp            = bits_per_plane * s->nb_planes;
     if (bits_per_plane > 8 || bpp < 1 || bpp > 32) {
-        av_log_ask_for_sample(avctx, "unsupported bit depth\n");
+        avpriv_request_sample(avctx, "Unsupported bit depth");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -151,20 +144,16 @@
         if (av_image_check_size(s->width, s->height, 0, avctx) < 0)
             return -1;
         avcodec_set_dimensions(avctx, s->width, s->height);
-        if (s->frame.data[0])
-            avctx->release_buffer(avctx, &s->frame);
     }
 
-    if (ff_get_buffer(avctx, &s->frame) < 0){
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
-        return -1;
-    }
-    memset(s->frame.data[0], 0, s->height * s->frame.linesize[0]);
-    s->frame.pict_type           = AV_PICTURE_TYPE_I;
-    s->frame.palette_has_changed = 1;
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
+        return ret;
+    memset(frame->data[0], 0, s->height * frame->linesize[0]);
+    frame->pict_type           = AV_PICTURE_TYPE_I;
+    frame->palette_has_changed = 1;
 
     pos_after_pal = bytestream2_tell(&s->g) + esize;
-    palette = (uint32_t*)s->frame.data[1];
+    palette = (uint32_t*)frame->data[1];
     if (etype == 1 && esize > 1 && bytestream2_peek_byte(&s->g) < 6) {
         int idx = bytestream2_get_byte(&s->g);
         npal = 4;
@@ -236,9 +225,9 @@
                     break;
 
                 if (bits_per_plane == 8) {
-                    picmemset_8bpp(s, val, run, &x, &y);
+                    picmemset_8bpp(s, frame, val, run, &x, &y);
                 } else {
-                    picmemset(s, val, run, &x, &y, &plane, bits_per_plane);
+                    picmemset(s, frame, val, run, &x, &y, &plane, bits_per_plane);
                 }
             }
         }
@@ -246,38 +235,27 @@
         if (x < avctx->width && y >= 0) {
             int run = (y + 1) * avctx->width - x;
             if (bits_per_plane == 8)
-                picmemset_8bpp(s, val, run, &x, &y);
+                picmemset_8bpp(s, frame, val, run, &x, &y);
             else
-                picmemset(s, val, run / (8 / bits_per_plane), &x, &y, &plane, bits_per_plane);
+                picmemset(s, frame, val, run / (8 / bits_per_plane), &x, &y, &plane, bits_per_plane);
         }
     } else {
         while (y >= 0 && bytestream2_get_bytes_left(&s->g) > 0) {
-            memcpy(s->frame.data[0] + y * s->frame.linesize[0], s->g.buffer, FFMIN(avctx->width, bytestream2_get_bytes_left(&s->g)));
+            memcpy(frame->data[0] + y * frame->linesize[0], s->g.buffer, FFMIN(avctx->width, bytestream2_get_bytes_left(&s->g)));
             bytestream2_skip(&s->g, avctx->width);
             y--;
         }
     }
 
     *got_frame      = 1;
-    *(AVFrame*)data = s->frame;
     return avpkt->size;
 }
 
-static av_cold int decode_end(AVCodecContext *avctx)
-{
-    PicContext *s = avctx->priv_data;
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
-    return 0;
-}
-
 AVCodec ff_pictor_decoder = {
     .name           = "pictor",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_PICTOR,
     .priv_data_size = sizeof(PicContext),
-    .init           = decode_init,
-    .close          = decode_end,
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("Pictor/PC Paint"),
diff --git a/libavcodec/png.c b/libavcodec/png.c
index a4287bd..b398d91 100644
--- a/libavcodec/png.c
+++ b/libavcodec/png.c
@@ -38,9 +38,7 @@
 
 void *ff_png_zalloc(void *opaque, unsigned int items, unsigned int size)
 {
-    if(items >= UINT_MAX / size)
-        return NULL;
-    return av_malloc(items * size);
+    return av_mallocz_array(items, size);
 }
 
 void ff_png_zfree(void *opaque, void *ptr)
diff --git a/libavcodec/pngdec.c b/libavcodec/pngdec.c
index 193e35e..086f3b41 100644
--- a/libavcodec/pngdec.c
+++ b/libavcodec/pngdec.c
@@ -42,8 +42,7 @@
     AVCodecContext *avctx;
 
     GetByteContext gb;
-    AVFrame picture1, picture2;
-    AVFrame *current_picture, *last_picture;
+    AVFrame *prev;
 
     int state;
     int width, height;
@@ -508,18 +507,13 @@
     PNGDecContext * const s = avctx->priv_data;
     const uint8_t *buf      = avpkt->data;
     int buf_size            = avpkt->size;
-    AVFrame *picture        = data;
+    AVFrame *p              = data;
     AVDictionary *metadata  = NULL;
     uint8_t *crow_buf_base  = NULL;
-    AVFrame *p;
     uint32_t tag, length;
     int64_t sig;
     int ret;
 
-    FFSWAP(AVFrame *, s->current_picture, s->last_picture);
-    avctx->coded_frame = s->current_picture;
-    p = s->current_picture;
-
     bytestream2_init(&s->gb, buf, buf_size);
 
     /* check signature */
@@ -642,14 +636,9 @@
                                                  s->bit_depth, s->color_type);
                     goto fail;
                 }
-                if (p->data[0])
-                    avctx->release_buffer(avctx, p);
 
-                p->reference = 3;
-                if (ff_get_buffer(avctx, p) < 0) {
-                    av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+                if (ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF) < 0)
                     goto fail;
-                }
                 p->pict_type        = AV_PICTURE_TYPE_I;
                 p->key_frame        = 1;
                 p->interlaced_frame = !!s->interlace_type;
@@ -762,7 +751,7 @@
 
     if (s->bits_per_pixel == 1 && s->color_type == PNG_COLOR_TYPE_PALETTE){
         int i, j, k;
-        uint8_t *pd = s->current_picture->data[0];
+        uint8_t *pd = p->data[0];
         for (j = 0; j < s->height; j++) {
             i = s->width / 8;
             for (k = 7; k >= 1; k--)
@@ -783,7 +772,7 @@
     }
     if (s->bits_per_pixel == 2){
         int i, j;
-        uint8_t *pd = s->current_picture->data[0];
+        uint8_t *pd = p->data[0];
         for (j = 0; j < s->height; j++) {
             i = s->width / 4;
             if (s->color_type == PNG_COLOR_TYPE_PALETTE){
@@ -812,7 +801,7 @@
     }
     if (s->bits_per_pixel == 4){
         int i, j;
-        uint8_t *pd = s->current_picture->data[0];
+        uint8_t *pd = p->data[0];
         for (j = 0; j < s->height; j++) {
             i = s->width/2;
             if (s->color_type == PNG_COLOR_TYPE_PALETTE){
@@ -833,15 +822,15 @@
     }
 
      /* handle p-frames only if a predecessor frame is available */
-     if (s->last_picture->data[0] != NULL) {
+     if (s->prev->data[0]) {
          if (   !(avpkt->flags & AV_PKT_FLAG_KEY)
-            && s->last_picture->width == s->current_picture->width
-            && s->last_picture->height== s->current_picture->height
-            && s->last_picture->format== s->current_picture->format
+            && s->prev->width == p->width
+            && s->prev->height== p->height
+            && s->prev->format== p->format
          ) {
             int i, j;
-            uint8_t *pd      = s->current_picture->data[0];
-            uint8_t *pd_last = s->last_picture->data[0];
+            uint8_t *pd      = p->data[0];
+            uint8_t *pd_last = s->prev->data[0];
 
             for (j = 0; j < s->height; j++) {
                 for (i = 0; i < s->width * s->bpp; i++) {
@@ -853,9 +842,13 @@
         }
     }
 
-    s->current_picture->metadata = metadata;
+    av_frame_set_metadata(p, metadata);
     metadata   = NULL;
-    *picture   = *s->current_picture;
+
+    av_frame_unref(s->prev);
+     if ((ret = av_frame_ref(s->prev, p)) < 0)
+         goto fail;
+
     *got_frame = 1;
 
     ret = bytestream2_tell(&s->gb);
@@ -876,10 +869,9 @@
 {
     PNGDecContext *s = avctx->priv_data;
 
-    s->current_picture = &s->picture1;
-    s->last_picture    = &s->picture2;
-    avcodec_get_frame_defaults(&s->picture1);
-    avcodec_get_frame_defaults(&s->picture2);
+    s->prev = av_frame_alloc();
+    if (!s->prev)
+        return AVERROR(ENOMEM);
 
     ff_pngdsp_init(&s->dsp);
 
@@ -892,10 +884,7 @@
 {
     PNGDecContext *s = avctx->priv_data;
 
-    if (s->picture1.data[0])
-        avctx->release_buffer(avctx, &s->picture1);
-    if (s->picture2.data[0])
-        avctx->release_buffer(avctx, &s->picture2);
+    av_frame_free(&s->prev);
 
     return 0;
 }
diff --git a/libavcodec/pngenc.c b/libavcodec/pngenc.c
index a401c78..5ba5983 100644
--- a/libavcodec/pngenc.c
+++ b/libavcodec/pngenc.c
@@ -57,8 +57,9 @@
     int x, mask, dst_x, j, b, bpp;
     uint8_t *d;
     const uint8_t *s;
+    static const int masks[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
 
-    mask =  (int[]){0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff}[pass];
+    mask = masks[pass];
     switch(bits_per_pixel) {
     case 1:
         memset(dst, 0, row_size);
diff --git a/libavcodec/pnm.c b/libavcodec/pnm.c
index 6d1eb6d..33b8896 100644
--- a/libavcodec/pnm.c
+++ b/libavcodec/pnm.c
@@ -193,16 +193,6 @@
     return 0;
 }
 
-av_cold int ff_pnm_end(AVCodecContext *avctx)
-{
-    PNMContext *s = avctx->priv_data;
-
-    if (s->picture.data[0])
-        avctx->release_buffer(avctx, &s->picture);
-
-    return 0;
-}
-
 av_cold int ff_pnm_init(AVCodecContext *avctx)
 {
     PNMContext *s = avctx->priv_data;
diff --git a/libavcodec/pnm.h b/libavcodec/pnm.h
index bb2cc77..92edf8d 100644
--- a/libavcodec/pnm.h
+++ b/libavcodec/pnm.h
@@ -34,7 +34,6 @@
 } PNMContext;
 
 int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s);
-int ff_pnm_end(AVCodecContext *avctx);
 int ff_pnm_init(AVCodecContext *avctx);
 
 #endif /* AVCODEC_PNM_H */
diff --git a/libavcodec/pnmdec.c b/libavcodec/pnmdec.c
index 3280eef..d0c7295 100644
--- a/libavcodec/pnmdec.c
+++ b/libavcodec/pnmdec.c
@@ -31,8 +31,7 @@
     const uint8_t *buf   = avpkt->data;
     int buf_size         = avpkt->size;
     PNMContext * const s = avctx->priv_data;
-    AVFrame *picture     = data;
-    AVFrame * const p    = &s->picture;
+    AVFrame * const p    = data;
     int i, j, n, linesize, h, upgrade = 0, is_mono = 0;
     unsigned char *ptr;
     int components, sample_len, ret;
@@ -44,14 +43,8 @@
     if ((ret = ff_pnm_decode_header(avctx, s)) < 0)
         return ret;
 
-    if (p->data[0])
-        avctx->release_buffer(avctx, p);
-
-    p->reference = 0;
-    if ((ret = ff_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
         return ret;
-    }
     p->pict_type = AV_PICTURE_TYPE_I;
     p->key_frame = 1;
 
@@ -230,7 +223,6 @@
         }
         break;
     }
-    *picture   = s->picture;
     *got_frame = 1;
 
     return s->bytestream - s->bytestream_start;
@@ -243,8 +235,6 @@
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_PGM,
     .priv_data_size = sizeof(PNMContext),
-    .init           = ff_pnm_init,
-    .close          = ff_pnm_end,
     .decode         = pnm_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
@@ -257,8 +247,6 @@
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_PGMYUV,
     .priv_data_size = sizeof(PNMContext),
-    .init           = ff_pnm_init,
-    .close          = ff_pnm_end,
     .decode         = pnm_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
@@ -271,8 +259,6 @@
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_PPM,
     .priv_data_size = sizeof(PNMContext),
-    .init           = ff_pnm_init,
-    .close          = ff_pnm_end,
     .decode         = pnm_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
@@ -285,8 +271,6 @@
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_PBM,
     .priv_data_size = sizeof(PNMContext),
-    .init           = ff_pnm_init,
-    .close          = ff_pnm_end,
     .decode         = pnm_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
@@ -299,8 +283,6 @@
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_PAM,
     .priv_data_size = sizeof(PNMContext),
-    .init           = ff_pnm_init,
-    .close          = ff_pnm_end,
     .decode         = pnm_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
diff --git a/libavcodec/ppc/Makefile b/libavcodec/ppc/Makefile
index a2ce9eb..febbb0a 100644
--- a/libavcodec/ppc/Makefile
+++ b/libavcodec/ppc/Makefile
@@ -3,6 +3,7 @@
 
 OBJS-$(CONFIG_H264CHROMA)              += ppc/h264chroma_init.o
 OBJS-$(CONFIG_H264QPEL)                += ppc/h264_qpel.o
+OBJS-$(CONFIG_HPELDSP)                 += ppc/hpeldsp_altivec.o
 OBJS-$(CONFIG_VORBIS_DECODER)          += ppc/vorbisdsp_altivec.o
 OBJS-$(CONFIG_VP3DSP)                  += ppc/vp3dsp_altivec.o
 
diff --git a/libavcodec/ppc/dsputil_altivec.c b/libavcodec/ppc/dsputil_altivec.c
index 75a6a6a..f36e394 100644
--- a/libavcodec/ppc/dsputil_altivec.c
+++ b/libavcodec/ppc/dsputil_altivec.c
@@ -607,354 +607,6 @@
     }
 }
 
-/* next one assumes that ((line_size % 16) == 0) */
-void ff_put_pixels16_altivec(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    register vector unsigned char pixelsv1, pixelsv2;
-    register vector unsigned char pixelsv1B, pixelsv2B;
-    register vector unsigned char pixelsv1C, pixelsv2C;
-    register vector unsigned char pixelsv1D, pixelsv2D;
-
-    register vector unsigned char perm = vec_lvsl(0, pixels);
-    int i;
-    register ptrdiff_t line_size_2 = line_size << 1;
-    register ptrdiff_t line_size_3 = line_size + line_size_2;
-    register ptrdiff_t line_size_4 = line_size << 2;
-
-// hand-unrolling the loop by 4 gains about 15%
-// mininum execution time goes from 74 to 60 cycles
-// it's faster than -funroll-loops, but using
-// -funroll-loops w/ this is bad - 74 cycles again.
-// all this is on a 7450, tuning for the 7450
-    for (i = 0; i < h; i += 4) {
-        pixelsv1  = vec_ld( 0, pixels);
-        pixelsv2  = vec_ld(15, pixels);
-        pixelsv1B = vec_ld(line_size, pixels);
-        pixelsv2B = vec_ld(15 + line_size, pixels);
-        pixelsv1C = vec_ld(line_size_2, pixels);
-        pixelsv2C = vec_ld(15 + line_size_2, pixels);
-        pixelsv1D = vec_ld(line_size_3, pixels);
-        pixelsv2D = vec_ld(15 + line_size_3, pixels);
-        vec_st(vec_perm(pixelsv1, pixelsv2, perm),
-               0, (unsigned char*)block);
-        vec_st(vec_perm(pixelsv1B, pixelsv2B, perm),
-               line_size, (unsigned char*)block);
-        vec_st(vec_perm(pixelsv1C, pixelsv2C, perm),
-               line_size_2, (unsigned char*)block);
-        vec_st(vec_perm(pixelsv1D, pixelsv2D, perm),
-               line_size_3, (unsigned char*)block);
-        pixels+=line_size_4;
-        block +=line_size_4;
-    }
-}
-
-/* next one assumes that ((line_size % 16) == 0) */
-#define op_avg(a,b)  a = ( ((a)|(b)) - ((((a)^(b))&0xFEFEFEFEUL)>>1) )
-void ff_avg_pixels16_altivec(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    register vector unsigned char pixelsv1, pixelsv2, pixelsv, blockv;
-    register vector unsigned char perm = vec_lvsl(0, pixels);
-    int i;
-
-    for (i = 0; i < h; i++) {
-        pixelsv1 = vec_ld( 0, pixels);
-        pixelsv2 = vec_ld(16,pixels);
-        blockv = vec_ld(0, block);
-        pixelsv = vec_perm(pixelsv1, pixelsv2, perm);
-        blockv = vec_avg(blockv,pixelsv);
-        vec_st(blockv, 0, (unsigned char*)block);
-        pixels+=line_size;
-        block +=line_size;
-    }
-}
-
-/* next one assumes that ((line_size % 8) == 0) */
-static void avg_pixels8_altivec(uint8_t * block, const uint8_t * pixels, ptrdiff_t line_size, int h)
-{
-    register vector unsigned char pixelsv1, pixelsv2, pixelsv, blockv;
-    int i;
-
-   for (i = 0; i < h; i++) {
-       /* block is 8 bytes-aligned, so we're either in the
-          left block (16 bytes-aligned) or in the right block (not) */
-       int rightside = ((unsigned long)block & 0x0000000F);
-
-       blockv = vec_ld(0, block);
-       pixelsv1 = vec_ld( 0, pixels);
-       pixelsv2 = vec_ld(16, pixels);
-       pixelsv = vec_perm(pixelsv1, pixelsv2, vec_lvsl(0, pixels));
-
-       if (rightside) {
-           pixelsv = vec_perm(blockv, pixelsv, vcprm(0,1,s0,s1));
-       } else {
-           pixelsv = vec_perm(blockv, pixelsv, vcprm(s0,s1,2,3));
-       }
-
-       blockv = vec_avg(blockv, pixelsv);
-
-       vec_st(blockv, 0, block);
-
-       pixels += line_size;
-       block += line_size;
-   }
-}
-
-/* next one assumes that ((line_size % 8) == 0) */
-static void put_pixels8_xy2_altivec(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    register int i;
-    register vector unsigned char pixelsv1, pixelsv2, pixelsavg;
-    register vector unsigned char blockv, temp1, temp2;
-    register vector unsigned short pixelssum1, pixelssum2, temp3;
-    register const vector unsigned char vczero = (const vector unsigned char)vec_splat_u8(0);
-    register const vector unsigned short vctwo = (const vector unsigned short)vec_splat_u16(2);
-
-    temp1 = vec_ld(0, pixels);
-    temp2 = vec_ld(16, pixels);
-    pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(0, pixels));
-    if ((((unsigned long)pixels) & 0x0000000F) ==  0x0000000F) {
-        pixelsv2 = temp2;
-    } else {
-        pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(1, pixels));
-    }
-    pixelsv1 = vec_mergeh(vczero, pixelsv1);
-    pixelsv2 = vec_mergeh(vczero, pixelsv2);
-    pixelssum1 = vec_add((vector unsigned short)pixelsv1,
-                         (vector unsigned short)pixelsv2);
-    pixelssum1 = vec_add(pixelssum1, vctwo);
-
-    for (i = 0; i < h ; i++) {
-        int rightside = ((unsigned long)block & 0x0000000F);
-        blockv = vec_ld(0, block);
-
-        temp1 = vec_ld(line_size, pixels);
-        temp2 = vec_ld(line_size + 16, pixels);
-        pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(line_size, pixels));
-        if (((((unsigned long)pixels) + line_size) & 0x0000000F) ==  0x0000000F) {
-            pixelsv2 = temp2;
-        } else {
-            pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(line_size + 1, pixels));
-        }
-
-        pixelsv1 = vec_mergeh(vczero, pixelsv1);
-        pixelsv2 = vec_mergeh(vczero, pixelsv2);
-        pixelssum2 = vec_add((vector unsigned short)pixelsv1,
-                             (vector unsigned short)pixelsv2);
-        temp3 = vec_add(pixelssum1, pixelssum2);
-        temp3 = vec_sra(temp3, vctwo);
-        pixelssum1 = vec_add(pixelssum2, vctwo);
-        pixelsavg = vec_packsu(temp3, (vector unsigned short) vczero);
-
-        if (rightside) {
-            blockv = vec_perm(blockv, pixelsavg, vcprm(0, 1, s0, s1));
-        } else {
-            blockv = vec_perm(blockv, pixelsavg, vcprm(s0, s1, 2, 3));
-        }
-
-        vec_st(blockv, 0, block);
-
-        block += line_size;
-        pixels += line_size;
-    }
-}
-
-/* next one assumes that ((line_size % 8) == 0) */
-static void put_no_rnd_pixels8_xy2_altivec(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    register int i;
-    register vector unsigned char pixelsv1, pixelsv2, pixelsavg;
-    register vector unsigned char blockv, temp1, temp2;
-    register vector unsigned short pixelssum1, pixelssum2, temp3;
-    register const vector unsigned char vczero = (const vector unsigned char)vec_splat_u8(0);
-    register const vector unsigned short vcone = (const vector unsigned short)vec_splat_u16(1);
-    register const vector unsigned short vctwo = (const vector unsigned short)vec_splat_u16(2);
-
-    temp1 = vec_ld(0, pixels);
-    temp2 = vec_ld(16, pixels);
-    pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(0, pixels));
-    if ((((unsigned long)pixels) & 0x0000000F) ==  0x0000000F) {
-        pixelsv2 = temp2;
-    } else {
-        pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(1, pixels));
-    }
-    pixelsv1 = vec_mergeh(vczero, pixelsv1);
-    pixelsv2 = vec_mergeh(vczero, pixelsv2);
-    pixelssum1 = vec_add((vector unsigned short)pixelsv1,
-                         (vector unsigned short)pixelsv2);
-    pixelssum1 = vec_add(pixelssum1, vcone);
-
-    for (i = 0; i < h ; i++) {
-        int rightside = ((unsigned long)block & 0x0000000F);
-        blockv = vec_ld(0, block);
-
-        temp1 = vec_ld(line_size, pixels);
-        temp2 = vec_ld(line_size + 16, pixels);
-        pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(line_size, pixels));
-        if (((((unsigned long)pixels) + line_size) & 0x0000000F) ==  0x0000000F) {
-            pixelsv2 = temp2;
-        } else {
-            pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(line_size + 1, pixels));
-        }
-
-        pixelsv1 = vec_mergeh(vczero, pixelsv1);
-        pixelsv2 = vec_mergeh(vczero, pixelsv2);
-        pixelssum2 = vec_add((vector unsigned short)pixelsv1,
-                             (vector unsigned short)pixelsv2);
-        temp3 = vec_add(pixelssum1, pixelssum2);
-        temp3 = vec_sra(temp3, vctwo);
-        pixelssum1 = vec_add(pixelssum2, vcone);
-        pixelsavg = vec_packsu(temp3, (vector unsigned short) vczero);
-
-        if (rightside) {
-            blockv = vec_perm(blockv, pixelsavg, vcprm(0, 1, s0, s1));
-        } else {
-            blockv = vec_perm(blockv, pixelsavg, vcprm(s0, s1, 2, 3));
-        }
-
-        vec_st(blockv, 0, block);
-
-        block += line_size;
-        pixels += line_size;
-    }
-}
-
-/* next one assumes that ((line_size % 16) == 0) */
-static void put_pixels16_xy2_altivec(uint8_t * block, const uint8_t * pixels, ptrdiff_t line_size, int h)
-{
-    register int i;
-    register vector unsigned char pixelsv1, pixelsv2, pixelsv3, pixelsv4;
-    register vector unsigned char blockv, temp1, temp2;
-    register vector unsigned short temp3, temp4,
-        pixelssum1, pixelssum2, pixelssum3, pixelssum4;
-    register const vector unsigned char vczero = (const vector unsigned char)vec_splat_u8(0);
-    register const vector unsigned short vctwo = (const vector unsigned short)vec_splat_u16(2);
-
-    temp1 = vec_ld(0, pixels);
-    temp2 = vec_ld(16, pixels);
-    pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(0, pixels));
-    if ((((unsigned long)pixels) & 0x0000000F) ==  0x0000000F) {
-        pixelsv2 = temp2;
-    } else {
-        pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(1, pixels));
-    }
-    pixelsv3 = vec_mergel(vczero, pixelsv1);
-    pixelsv4 = vec_mergel(vczero, pixelsv2);
-    pixelsv1 = vec_mergeh(vczero, pixelsv1);
-    pixelsv2 = vec_mergeh(vczero, pixelsv2);
-    pixelssum3 = vec_add((vector unsigned short)pixelsv3,
-                         (vector unsigned short)pixelsv4);
-    pixelssum3 = vec_add(pixelssum3, vctwo);
-    pixelssum1 = vec_add((vector unsigned short)pixelsv1,
-                         (vector unsigned short)pixelsv2);
-    pixelssum1 = vec_add(pixelssum1, vctwo);
-
-    for (i = 0; i < h ; i++) {
-        blockv = vec_ld(0, block);
-
-        temp1 = vec_ld(line_size, pixels);
-        temp2 = vec_ld(line_size + 16, pixels);
-        pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(line_size, pixels));
-        if (((((unsigned long)pixels) + line_size) & 0x0000000F) ==  0x0000000F) {
-            pixelsv2 = temp2;
-        } else {
-            pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(line_size + 1, pixels));
-        }
-
-        pixelsv3 = vec_mergel(vczero, pixelsv1);
-        pixelsv4 = vec_mergel(vczero, pixelsv2);
-        pixelsv1 = vec_mergeh(vczero, pixelsv1);
-        pixelsv2 = vec_mergeh(vczero, pixelsv2);
-
-        pixelssum4 = vec_add((vector unsigned short)pixelsv3,
-                             (vector unsigned short)pixelsv4);
-        pixelssum2 = vec_add((vector unsigned short)pixelsv1,
-                             (vector unsigned short)pixelsv2);
-        temp4 = vec_add(pixelssum3, pixelssum4);
-        temp4 = vec_sra(temp4, vctwo);
-        temp3 = vec_add(pixelssum1, pixelssum2);
-        temp3 = vec_sra(temp3, vctwo);
-
-        pixelssum3 = vec_add(pixelssum4, vctwo);
-        pixelssum1 = vec_add(pixelssum2, vctwo);
-
-        blockv = vec_packsu(temp3, temp4);
-
-        vec_st(blockv, 0, block);
-
-        block += line_size;
-        pixels += line_size;
-    }
-}
-
-/* next one assumes that ((line_size % 16) == 0) */
-static void put_no_rnd_pixels16_xy2_altivec(uint8_t * block, const uint8_t * pixels, ptrdiff_t line_size, int h)
-{
-    register int i;
-    register vector unsigned char pixelsv1, pixelsv2, pixelsv3, pixelsv4;
-    register vector unsigned char blockv, temp1, temp2;
-    register vector unsigned short temp3, temp4,
-        pixelssum1, pixelssum2, pixelssum3, pixelssum4;
-    register const vector unsigned char vczero = (const vector unsigned char)vec_splat_u8(0);
-    register const vector unsigned short vcone = (const vector unsigned short)vec_splat_u16(1);
-    register const vector unsigned short vctwo = (const vector unsigned short)vec_splat_u16(2);
-
-    temp1 = vec_ld(0, pixels);
-    temp2 = vec_ld(16, pixels);
-    pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(0, pixels));
-    if ((((unsigned long)pixels) & 0x0000000F) ==  0x0000000F) {
-        pixelsv2 = temp2;
-    } else {
-        pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(1, pixels));
-    }
-    pixelsv3 = vec_mergel(vczero, pixelsv1);
-    pixelsv4 = vec_mergel(vczero, pixelsv2);
-    pixelsv1 = vec_mergeh(vczero, pixelsv1);
-    pixelsv2 = vec_mergeh(vczero, pixelsv2);
-    pixelssum3 = vec_add((vector unsigned short)pixelsv3,
-                         (vector unsigned short)pixelsv4);
-    pixelssum3 = vec_add(pixelssum3, vcone);
-    pixelssum1 = vec_add((vector unsigned short)pixelsv1,
-                         (vector unsigned short)pixelsv2);
-    pixelssum1 = vec_add(pixelssum1, vcone);
-
-    for (i = 0; i < h ; i++) {
-        blockv = vec_ld(0, block);
-
-        temp1 = vec_ld(line_size, pixels);
-        temp2 = vec_ld(line_size + 16, pixels);
-        pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(line_size, pixels));
-        if (((((unsigned long)pixels) + line_size) & 0x0000000F) ==  0x0000000F) {
-            pixelsv2 = temp2;
-        } else {
-            pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(line_size + 1, pixels));
-        }
-
-        pixelsv3 = vec_mergel(vczero, pixelsv1);
-        pixelsv4 = vec_mergel(vczero, pixelsv2);
-        pixelsv1 = vec_mergeh(vczero, pixelsv1);
-        pixelsv2 = vec_mergeh(vczero, pixelsv2);
-
-        pixelssum4 = vec_add((vector unsigned short)pixelsv3,
-                             (vector unsigned short)pixelsv4);
-        pixelssum2 = vec_add((vector unsigned short)pixelsv1,
-                             (vector unsigned short)pixelsv2);
-        temp4 = vec_add(pixelssum3, pixelssum4);
-        temp4 = vec_sra(temp4, vctwo);
-        temp3 = vec_add(pixelssum1, pixelssum2);
-        temp3 = vec_sra(temp3, vctwo);
-
-        pixelssum3 = vec_add(pixelssum4, vcone);
-        pixelssum1 = vec_add(pixelssum2, vcone);
-
-        blockv = vec_packsu(temp3, temp4);
-
-        vec_st(blockv, 0, block);
-
-        block += line_size;
-        pixels += line_size;
-    }
-}
-
 static int hadamard8_diff8x8_altivec(/*MpegEncContext*/ void *s, uint8_t *dst, uint8_t *src, int stride, int h){
     int sum;
     register const vector unsigned char vzero =
@@ -1284,69 +936,6 @@
     return score;
 }
 
-/* next one assumes that ((line_size % 8) == 0) */
-static void avg_pixels8_xy2_altivec(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    register int i;
-    register vector unsigned char pixelsv1, pixelsv2, pixelsavg;
-    register vector unsigned char blockv, temp1, temp2, blocktemp;
-    register vector unsigned short pixelssum1, pixelssum2, temp3;
-
-    register const vector unsigned char vczero = (const vector unsigned char)
-                                        vec_splat_u8(0);
-    register const vector unsigned short vctwo = (const vector unsigned short)
-                                        vec_splat_u16(2);
-
-    temp1 = vec_ld(0, pixels);
-    temp2 = vec_ld(16, pixels);
-    pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(0, pixels));
-    if ((((unsigned long)pixels) & 0x0000000F) ==  0x0000000F) {
-        pixelsv2 = temp2;
-    } else {
-        pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(1, pixels));
-    }
-    pixelsv1 = vec_mergeh(vczero, pixelsv1);
-    pixelsv2 = vec_mergeh(vczero, pixelsv2);
-    pixelssum1 = vec_add((vector unsigned short)pixelsv1,
-                         (vector unsigned short)pixelsv2);
-    pixelssum1 = vec_add(pixelssum1, vctwo);
-
-    for (i = 0; i < h ; i++) {
-        int rightside = ((unsigned long)block & 0x0000000F);
-        blockv = vec_ld(0, block);
-
-        temp1 = vec_ld(line_size, pixels);
-        temp2 = vec_ld(line_size + 16, pixels);
-        pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(line_size, pixels));
-        if (((((unsigned long)pixels) + line_size) & 0x0000000F) ==  0x0000000F) {
-            pixelsv2 = temp2;
-        } else {
-            pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(line_size + 1, pixels));
-        }
-
-        pixelsv1 = vec_mergeh(vczero, pixelsv1);
-        pixelsv2 = vec_mergeh(vczero, pixelsv2);
-        pixelssum2 = vec_add((vector unsigned short)pixelsv1,
-                             (vector unsigned short)pixelsv2);
-        temp3 = vec_add(pixelssum1, pixelssum2);
-        temp3 = vec_sra(temp3, vctwo);
-        pixelssum1 = vec_add(pixelssum2, vctwo);
-        pixelsavg = vec_packsu(temp3, (vector unsigned short) vczero);
-
-        if (rightside) {
-            blocktemp = vec_perm(blockv, pixelsavg, vcprm(0, 1, s0, s1));
-        } else {
-            blocktemp = vec_perm(blockv, pixelsavg, vcprm(s0, s1, 2, 3));
-        }
-
-        blockv = vec_avg(blocktemp, blockv);
-        vec_st(blockv, 0, block);
-
-        block += line_size;
-        pixels += line_size;
-    }
-}
-
 av_cold void ff_dsputil_init_altivec(DSPContext *c, AVCodecContext *avctx)
 {
     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
@@ -1367,16 +956,6 @@
     if (!high_bit_depth) {
     c->get_pixels = get_pixels_altivec;
     c->clear_block = clear_block_altivec;
-    c->put_pixels_tab[0][0] = ff_put_pixels16_altivec;
-    /* the two functions do the same thing, so use the same code */
-    c->put_no_rnd_pixels_tab[0][0] = ff_put_pixels16_altivec;
-    c->avg_pixels_tab[0][0] = ff_avg_pixels16_altivec;
-    c->avg_pixels_tab[1][0] = avg_pixels8_altivec;
-    c->avg_pixels_tab[1][3] = avg_pixels8_xy2_altivec;
-    c->put_pixels_tab[1][3] = put_pixels8_xy2_altivec;
-    c->put_no_rnd_pixels_tab[1][3] = put_no_rnd_pixels8_xy2_altivec;
-    c->put_pixels_tab[0][3] = put_pixels16_xy2_altivec;
-    c->put_no_rnd_pixels_tab[0][3] = put_no_rnd_pixels16_xy2_altivec;
     }
 
     c->hadamard8_diff[0] = hadamard8_diff16_altivec;
diff --git a/libavcodec/ppc/dsputil_ppc.c b/libavcodec/ppc/dsputil_ppc.c
index 4b2bd60..6112b0c 100644
--- a/libavcodec/ppc/dsputil_ppc.c
+++ b/libavcodec/ppc/dsputil_ppc.c
@@ -142,13 +142,6 @@
     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
     int mm_flags = av_get_cpu_flags();
 
-    if (avctx->dsp_mask) {
-        if (avctx->dsp_mask & AV_CPU_FLAG_FORCE)
-            mm_flags |= (avctx->dsp_mask & 0xffff);
-        else
-            mm_flags &= ~(avctx->dsp_mask & 0xffff);
-    }
-
     // Common optimizations whether AltiVec is available or not
     if (!high_bit_depth) {
     switch (check_dcbzl_effect()) {
diff --git a/libavcodec/ppc/h264_qpel.c b/libavcodec/ppc/h264_qpel.c
index 8938783..429ae42 100644
--- a/libavcodec/ppc/h264_qpel.c
+++ b/libavcodec/ppc/h264_qpel.c
@@ -65,43 +65,51 @@
 #undef PREFIX_h264_qpel16_hv_lowpass_num
 
 #define H264_MC(OPNAME, SIZE, CODETYPE) \
-static void OPNAME ## h264_qpel ## SIZE ## _mc00_ ## CODETYPE (uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc00_ ## CODETYPE (uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     ff_ ## OPNAME ## pixels ## SIZE ## _ ## CODETYPE(dst, src, stride, SIZE);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc10_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){ \
+static void OPNAME ## h264_qpel ## SIZE ## _mc10_ ## CODETYPE(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{ \
     DECLARE_ALIGNED(16, uint8_t, half)[SIZE*SIZE];\
     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(half, src, SIZE, stride);\
     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, src, half, stride, stride, SIZE);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc20_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc20_ ## CODETYPE(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(dst, src, stride, stride);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc30_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc30_ ## CODETYPE(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(16, uint8_t, half)[SIZE*SIZE];\
     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(half, src, SIZE, stride);\
     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, src+1, half, stride, stride, SIZE);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc01_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc01_ ## CODETYPE(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(16, uint8_t, half)[SIZE*SIZE];\
     put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(half, src, SIZE, stride);\
     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, src, half, stride, stride, SIZE);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc02_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc02_ ## CODETYPE(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(dst, src, stride, stride);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc03_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc03_ ## CODETYPE(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(16, uint8_t, half)[SIZE*SIZE];\
     put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(half, src, SIZE, stride);\
     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, src+stride, half, stride, stride, SIZE);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc11_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc11_ ## CODETYPE(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(16, uint8_t, halfH)[SIZE*SIZE];\
     DECLARE_ALIGNED(16, uint8_t, halfV)[SIZE*SIZE];\
     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src, SIZE, stride);\
@@ -109,7 +117,8 @@
     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfV, stride, SIZE, SIZE);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc31_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc31_ ## CODETYPE(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(16, uint8_t, halfH)[SIZE*SIZE];\
     DECLARE_ALIGNED(16, uint8_t, halfV)[SIZE*SIZE];\
     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src, SIZE, stride);\
@@ -117,7 +126,8 @@
     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfV, stride, SIZE, SIZE);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc13_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc13_ ## CODETYPE(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(16, uint8_t, halfH)[SIZE*SIZE];\
     DECLARE_ALIGNED(16, uint8_t, halfV)[SIZE*SIZE];\
     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src + stride, SIZE, stride);\
@@ -125,7 +135,8 @@
     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfV, stride, SIZE, SIZE);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc33_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc33_ ## CODETYPE(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(16, uint8_t, halfH)[SIZE*SIZE];\
     DECLARE_ALIGNED(16, uint8_t, halfV)[SIZE*SIZE];\
     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src + stride, SIZE, stride);\
@@ -133,12 +144,14 @@
     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfV, stride, SIZE, SIZE);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc22_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc22_ ## CODETYPE(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(16, int16_t, tmp)[SIZE*(SIZE+8)];\
     OPNAME ## h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(dst, tmp, src, stride, SIZE, stride);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc21_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc21_ ## CODETYPE(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(16, uint8_t, halfH)[SIZE*SIZE];\
     DECLARE_ALIGNED(16, uint8_t, halfHV)[SIZE*SIZE];\
     DECLARE_ALIGNED(16, int16_t, tmp)[SIZE*(SIZE+8)];\
@@ -147,7 +160,8 @@
     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfHV, stride, SIZE, SIZE);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc23_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc23_ ## CODETYPE(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(16, uint8_t, halfH)[SIZE*SIZE];\
     DECLARE_ALIGNED(16, uint8_t, halfHV)[SIZE*SIZE];\
     DECLARE_ALIGNED(16, int16_t, tmp)[SIZE*(SIZE+8)];\
@@ -156,7 +170,8 @@
     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfHV, stride, SIZE, SIZE);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc12_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc12_ ## CODETYPE(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(16, uint8_t, halfV)[SIZE*SIZE];\
     DECLARE_ALIGNED(16, uint8_t, halfHV)[SIZE*SIZE];\
     DECLARE_ALIGNED(16, int16_t, tmp)[SIZE*(SIZE+8)];\
@@ -165,7 +180,8 @@
     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfV, halfHV, stride, SIZE, SIZE);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc32_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc32_ ## CODETYPE(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(16, uint8_t, halfV)[SIZE*SIZE];\
     DECLARE_ALIGNED(16, uint8_t, halfHV)[SIZE*SIZE];\
     DECLARE_ALIGNED(16, int16_t, tmp)[SIZE*(SIZE+8)];\
diff --git a/libavcodec/ppc/hpeldsp_altivec.c b/libavcodec/ppc/hpeldsp_altivec.c
new file mode 100644
index 0000000..5bc812a
--- /dev/null
+++ b/libavcodec/ppc/hpeldsp_altivec.c
@@ -0,0 +1,467 @@
+/*
+ * Copyright (c) 2002 Brian Foley
+ * Copyright (c) 2002 Dieter Shirley
+ * Copyright (c) 2003-2004 Romain Dolbeau <romain@dolbeau.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
+ */
+
+#include "config.h"
+#include "libavutil/attributes.h"
+#include "libavutil/cpu.h"
+#include "libavcodec/hpeldsp.h"
+
+#if HAVE_ALTIVEC
+#if HAVE_ALTIVEC_H
+#include <altivec.h>
+#endif
+#include "libavutil/ppc/types_altivec.h"
+#include "libavutil/ppc/util_altivec.h"
+#include "dsputil_altivec.h"
+
+/* next one assumes that ((line_size % 16) == 0) */
+void ff_put_pixels16_altivec(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    register vector unsigned char pixelsv1, pixelsv2;
+    register vector unsigned char pixelsv1B, pixelsv2B;
+    register vector unsigned char pixelsv1C, pixelsv2C;
+    register vector unsigned char pixelsv1D, pixelsv2D;
+
+    register vector unsigned char perm = vec_lvsl(0, pixels);
+    int i;
+    register ptrdiff_t line_size_2 = line_size << 1;
+    register ptrdiff_t line_size_3 = line_size + line_size_2;
+    register ptrdiff_t line_size_4 = line_size << 2;
+
+// hand-unrolling the loop by 4 gains about 15%
+// mininum execution time goes from 74 to 60 cycles
+// it's faster than -funroll-loops, but using
+// -funroll-loops w/ this is bad - 74 cycles again.
+// all this is on a 7450, tuning for the 7450
+    for (i = 0; i < h; i += 4) {
+        pixelsv1  = vec_ld( 0, pixels);
+        pixelsv2  = vec_ld(15, pixels);
+        pixelsv1B = vec_ld(line_size, pixels);
+        pixelsv2B = vec_ld(15 + line_size, pixels);
+        pixelsv1C = vec_ld(line_size_2, pixels);
+        pixelsv2C = vec_ld(15 + line_size_2, pixels);
+        pixelsv1D = vec_ld(line_size_3, pixels);
+        pixelsv2D = vec_ld(15 + line_size_3, pixels);
+        vec_st(vec_perm(pixelsv1, pixelsv2, perm),
+               0, (unsigned char*)block);
+        vec_st(vec_perm(pixelsv1B, pixelsv2B, perm),
+               line_size, (unsigned char*)block);
+        vec_st(vec_perm(pixelsv1C, pixelsv2C, perm),
+               line_size_2, (unsigned char*)block);
+        vec_st(vec_perm(pixelsv1D, pixelsv2D, perm),
+               line_size_3, (unsigned char*)block);
+        pixels+=line_size_4;
+        block +=line_size_4;
+    }
+}
+
+/* next one assumes that ((line_size % 16) == 0) */
+#define op_avg(a,b)  a = ( ((a)|(b)) - ((((a)^(b))&0xFEFEFEFEUL)>>1) )
+void ff_avg_pixels16_altivec(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    register vector unsigned char pixelsv1, pixelsv2, pixelsv, blockv;
+    register vector unsigned char perm = vec_lvsl(0, pixels);
+    int i;
+
+    for (i = 0; i < h; i++) {
+        pixelsv1 = vec_ld( 0, pixels);
+        pixelsv2 = vec_ld(16,pixels);
+        blockv = vec_ld(0, block);
+        pixelsv = vec_perm(pixelsv1, pixelsv2, perm);
+        blockv = vec_avg(blockv,pixelsv);
+        vec_st(blockv, 0, (unsigned char*)block);
+        pixels+=line_size;
+        block +=line_size;
+    }
+}
+
+/* next one assumes that ((line_size % 8) == 0) */
+static void avg_pixels8_altivec(uint8_t * block, const uint8_t * pixels, ptrdiff_t line_size, int h)
+{
+    register vector unsigned char pixelsv1, pixelsv2, pixelsv, blockv;
+    int i;
+
+   for (i = 0; i < h; i++) {
+       /* block is 8 bytes-aligned, so we're either in the
+          left block (16 bytes-aligned) or in the right block (not) */
+       int rightside = ((unsigned long)block & 0x0000000F);
+
+       blockv = vec_ld(0, block);
+       pixelsv1 = vec_ld( 0, pixels);
+       pixelsv2 = vec_ld(16, pixels);
+       pixelsv = vec_perm(pixelsv1, pixelsv2, vec_lvsl(0, pixels));
+
+       if (rightside) {
+           pixelsv = vec_perm(blockv, pixelsv, vcprm(0,1,s0,s1));
+       } else {
+           pixelsv = vec_perm(blockv, pixelsv, vcprm(s0,s1,2,3));
+       }
+
+       blockv = vec_avg(blockv, pixelsv);
+
+       vec_st(blockv, 0, block);
+
+       pixels += line_size;
+       block += line_size;
+   }
+}
+
+/* next one assumes that ((line_size % 8) == 0) */
+static void put_pixels8_xy2_altivec(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    register int i;
+    register vector unsigned char pixelsv1, pixelsv2, pixelsavg;
+    register vector unsigned char blockv, temp1, temp2;
+    register vector unsigned short pixelssum1, pixelssum2, temp3;
+    register const vector unsigned char vczero = (const vector unsigned char)vec_splat_u8(0);
+    register const vector unsigned short vctwo = (const vector unsigned short)vec_splat_u16(2);
+
+    temp1 = vec_ld(0, pixels);
+    temp2 = vec_ld(16, pixels);
+    pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(0, pixels));
+    if ((((unsigned long)pixels) & 0x0000000F) ==  0x0000000F) {
+        pixelsv2 = temp2;
+    } else {
+        pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(1, pixels));
+    }
+    pixelsv1 = vec_mergeh(vczero, pixelsv1);
+    pixelsv2 = vec_mergeh(vczero, pixelsv2);
+    pixelssum1 = vec_add((vector unsigned short)pixelsv1,
+                         (vector unsigned short)pixelsv2);
+    pixelssum1 = vec_add(pixelssum1, vctwo);
+
+    for (i = 0; i < h ; i++) {
+        int rightside = ((unsigned long)block & 0x0000000F);
+        blockv = vec_ld(0, block);
+
+        temp1 = vec_ld(line_size, pixels);
+        temp2 = vec_ld(line_size + 16, pixels);
+        pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(line_size, pixels));
+        if (((((unsigned long)pixels) + line_size) & 0x0000000F) ==  0x0000000F) {
+            pixelsv2 = temp2;
+        } else {
+            pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(line_size + 1, pixels));
+        }
+
+        pixelsv1 = vec_mergeh(vczero, pixelsv1);
+        pixelsv2 = vec_mergeh(vczero, pixelsv2);
+        pixelssum2 = vec_add((vector unsigned short)pixelsv1,
+                             (vector unsigned short)pixelsv2);
+        temp3 = vec_add(pixelssum1, pixelssum2);
+        temp3 = vec_sra(temp3, vctwo);
+        pixelssum1 = vec_add(pixelssum2, vctwo);
+        pixelsavg = vec_packsu(temp3, (vector unsigned short) vczero);
+
+        if (rightside) {
+            blockv = vec_perm(blockv, pixelsavg, vcprm(0, 1, s0, s1));
+        } else {
+            blockv = vec_perm(blockv, pixelsavg, vcprm(s0, s1, 2, 3));
+        }
+
+        vec_st(blockv, 0, block);
+
+        block += line_size;
+        pixels += line_size;
+    }
+}
+
+/* next one assumes that ((line_size % 8) == 0) */
+static void put_no_rnd_pixels8_xy2_altivec(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    register int i;
+    register vector unsigned char pixelsv1, pixelsv2, pixelsavg;
+    register vector unsigned char blockv, temp1, temp2;
+    register vector unsigned short pixelssum1, pixelssum2, temp3;
+    register const vector unsigned char vczero = (const vector unsigned char)vec_splat_u8(0);
+    register const vector unsigned short vcone = (const vector unsigned short)vec_splat_u16(1);
+    register const vector unsigned short vctwo = (const vector unsigned short)vec_splat_u16(2);
+
+    temp1 = vec_ld(0, pixels);
+    temp2 = vec_ld(16, pixels);
+    pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(0, pixels));
+    if ((((unsigned long)pixels) & 0x0000000F) ==  0x0000000F) {
+        pixelsv2 = temp2;
+    } else {
+        pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(1, pixels));
+    }
+    pixelsv1 = vec_mergeh(vczero, pixelsv1);
+    pixelsv2 = vec_mergeh(vczero, pixelsv2);
+    pixelssum1 = vec_add((vector unsigned short)pixelsv1,
+                         (vector unsigned short)pixelsv2);
+    pixelssum1 = vec_add(pixelssum1, vcone);
+
+    for (i = 0; i < h ; i++) {
+        int rightside = ((unsigned long)block & 0x0000000F);
+        blockv = vec_ld(0, block);
+
+        temp1 = vec_ld(line_size, pixels);
+        temp2 = vec_ld(line_size + 16, pixels);
+        pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(line_size, pixels));
+        if (((((unsigned long)pixels) + line_size) & 0x0000000F) ==  0x0000000F) {
+            pixelsv2 = temp2;
+        } else {
+            pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(line_size + 1, pixels));
+        }
+
+        pixelsv1 = vec_mergeh(vczero, pixelsv1);
+        pixelsv2 = vec_mergeh(vczero, pixelsv2);
+        pixelssum2 = vec_add((vector unsigned short)pixelsv1,
+                             (vector unsigned short)pixelsv2);
+        temp3 = vec_add(pixelssum1, pixelssum2);
+        temp3 = vec_sra(temp3, vctwo);
+        pixelssum1 = vec_add(pixelssum2, vcone);
+        pixelsavg = vec_packsu(temp3, (vector unsigned short) vczero);
+
+        if (rightside) {
+            blockv = vec_perm(blockv, pixelsavg, vcprm(0, 1, s0, s1));
+        } else {
+            blockv = vec_perm(blockv, pixelsavg, vcprm(s0, s1, 2, 3));
+        }
+
+        vec_st(blockv, 0, block);
+
+        block += line_size;
+        pixels += line_size;
+    }
+}
+
+/* next one assumes that ((line_size % 16) == 0) */
+static void put_pixels16_xy2_altivec(uint8_t * block, const uint8_t * pixels, ptrdiff_t line_size, int h)
+{
+    register int i;
+    register vector unsigned char pixelsv1, pixelsv2, pixelsv3, pixelsv4;
+    register vector unsigned char blockv, temp1, temp2;
+    register vector unsigned short temp3, temp4,
+        pixelssum1, pixelssum2, pixelssum3, pixelssum4;
+    register const vector unsigned char vczero = (const vector unsigned char)vec_splat_u8(0);
+    register const vector unsigned short vctwo = (const vector unsigned short)vec_splat_u16(2);
+
+    temp1 = vec_ld(0, pixels);
+    temp2 = vec_ld(16, pixels);
+    pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(0, pixels));
+    if ((((unsigned long)pixels) & 0x0000000F) ==  0x0000000F) {
+        pixelsv2 = temp2;
+    } else {
+        pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(1, pixels));
+    }
+    pixelsv3 = vec_mergel(vczero, pixelsv1);
+    pixelsv4 = vec_mergel(vczero, pixelsv2);
+    pixelsv1 = vec_mergeh(vczero, pixelsv1);
+    pixelsv2 = vec_mergeh(vczero, pixelsv2);
+    pixelssum3 = vec_add((vector unsigned short)pixelsv3,
+                         (vector unsigned short)pixelsv4);
+    pixelssum3 = vec_add(pixelssum3, vctwo);
+    pixelssum1 = vec_add((vector unsigned short)pixelsv1,
+                         (vector unsigned short)pixelsv2);
+    pixelssum1 = vec_add(pixelssum1, vctwo);
+
+    for (i = 0; i < h ; i++) {
+        blockv = vec_ld(0, block);
+
+        temp1 = vec_ld(line_size, pixels);
+        temp2 = vec_ld(line_size + 16, pixels);
+        pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(line_size, pixels));
+        if (((((unsigned long)pixels) + line_size) & 0x0000000F) ==  0x0000000F) {
+            pixelsv2 = temp2;
+        } else {
+            pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(line_size + 1, pixels));
+        }
+
+        pixelsv3 = vec_mergel(vczero, pixelsv1);
+        pixelsv4 = vec_mergel(vczero, pixelsv2);
+        pixelsv1 = vec_mergeh(vczero, pixelsv1);
+        pixelsv2 = vec_mergeh(vczero, pixelsv2);
+
+        pixelssum4 = vec_add((vector unsigned short)pixelsv3,
+                             (vector unsigned short)pixelsv4);
+        pixelssum2 = vec_add((vector unsigned short)pixelsv1,
+                             (vector unsigned short)pixelsv2);
+        temp4 = vec_add(pixelssum3, pixelssum4);
+        temp4 = vec_sra(temp4, vctwo);
+        temp3 = vec_add(pixelssum1, pixelssum2);
+        temp3 = vec_sra(temp3, vctwo);
+
+        pixelssum3 = vec_add(pixelssum4, vctwo);
+        pixelssum1 = vec_add(pixelssum2, vctwo);
+
+        blockv = vec_packsu(temp3, temp4);
+
+        vec_st(blockv, 0, block);
+
+        block += line_size;
+        pixels += line_size;
+    }
+}
+
+/* next one assumes that ((line_size % 16) == 0) */
+static void put_no_rnd_pixels16_xy2_altivec(uint8_t * block, const uint8_t * pixels, ptrdiff_t line_size, int h)
+{
+    register int i;
+    register vector unsigned char pixelsv1, pixelsv2, pixelsv3, pixelsv4;
+    register vector unsigned char blockv, temp1, temp2;
+    register vector unsigned short temp3, temp4,
+        pixelssum1, pixelssum2, pixelssum3, pixelssum4;
+    register const vector unsigned char vczero = (const vector unsigned char)vec_splat_u8(0);
+    register const vector unsigned short vcone = (const vector unsigned short)vec_splat_u16(1);
+    register const vector unsigned short vctwo = (const vector unsigned short)vec_splat_u16(2);
+
+    temp1 = vec_ld(0, pixels);
+    temp2 = vec_ld(16, pixels);
+    pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(0, pixels));
+    if ((((unsigned long)pixels) & 0x0000000F) ==  0x0000000F) {
+        pixelsv2 = temp2;
+    } else {
+        pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(1, pixels));
+    }
+    pixelsv3 = vec_mergel(vczero, pixelsv1);
+    pixelsv4 = vec_mergel(vczero, pixelsv2);
+    pixelsv1 = vec_mergeh(vczero, pixelsv1);
+    pixelsv2 = vec_mergeh(vczero, pixelsv2);
+    pixelssum3 = vec_add((vector unsigned short)pixelsv3,
+                         (vector unsigned short)pixelsv4);
+    pixelssum3 = vec_add(pixelssum3, vcone);
+    pixelssum1 = vec_add((vector unsigned short)pixelsv1,
+                         (vector unsigned short)pixelsv2);
+    pixelssum1 = vec_add(pixelssum1, vcone);
+
+    for (i = 0; i < h ; i++) {
+        blockv = vec_ld(0, block);
+
+        temp1 = vec_ld(line_size, pixels);
+        temp2 = vec_ld(line_size + 16, pixels);
+        pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(line_size, pixels));
+        if (((((unsigned long)pixels) + line_size) & 0x0000000F) ==  0x0000000F) {
+            pixelsv2 = temp2;
+        } else {
+            pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(line_size + 1, pixels));
+        }
+
+        pixelsv3 = vec_mergel(vczero, pixelsv1);
+        pixelsv4 = vec_mergel(vczero, pixelsv2);
+        pixelsv1 = vec_mergeh(vczero, pixelsv1);
+        pixelsv2 = vec_mergeh(vczero, pixelsv2);
+
+        pixelssum4 = vec_add((vector unsigned short)pixelsv3,
+                             (vector unsigned short)pixelsv4);
+        pixelssum2 = vec_add((vector unsigned short)pixelsv1,
+                             (vector unsigned short)pixelsv2);
+        temp4 = vec_add(pixelssum3, pixelssum4);
+        temp4 = vec_sra(temp4, vctwo);
+        temp3 = vec_add(pixelssum1, pixelssum2);
+        temp3 = vec_sra(temp3, vctwo);
+
+        pixelssum3 = vec_add(pixelssum4, vcone);
+        pixelssum1 = vec_add(pixelssum2, vcone);
+
+        blockv = vec_packsu(temp3, temp4);
+
+        vec_st(blockv, 0, block);
+
+        block += line_size;
+        pixels += line_size;
+    }
+}
+
+/* next one assumes that ((line_size % 8) == 0) */
+static void avg_pixels8_xy2_altivec(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    register int i;
+    register vector unsigned char pixelsv1, pixelsv2, pixelsavg;
+    register vector unsigned char blockv, temp1, temp2, blocktemp;
+    register vector unsigned short pixelssum1, pixelssum2, temp3;
+
+    register const vector unsigned char vczero = (const vector unsigned char)
+                                        vec_splat_u8(0);
+    register const vector unsigned short vctwo = (const vector unsigned short)
+                                        vec_splat_u16(2);
+
+    temp1 = vec_ld(0, pixels);
+    temp2 = vec_ld(16, pixels);
+    pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(0, pixels));
+    if ((((unsigned long)pixels) & 0x0000000F) ==  0x0000000F) {
+        pixelsv2 = temp2;
+    } else {
+        pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(1, pixels));
+    }
+    pixelsv1 = vec_mergeh(vczero, pixelsv1);
+    pixelsv2 = vec_mergeh(vczero, pixelsv2);
+    pixelssum1 = vec_add((vector unsigned short)pixelsv1,
+                         (vector unsigned short)pixelsv2);
+    pixelssum1 = vec_add(pixelssum1, vctwo);
+
+    for (i = 0; i < h ; i++) {
+        int rightside = ((unsigned long)block & 0x0000000F);
+        blockv = vec_ld(0, block);
+
+        temp1 = vec_ld(line_size, pixels);
+        temp2 = vec_ld(line_size + 16, pixels);
+        pixelsv1 = vec_perm(temp1, temp2, vec_lvsl(line_size, pixels));
+        if (((((unsigned long)pixels) + line_size) & 0x0000000F) ==  0x0000000F) {
+            pixelsv2 = temp2;
+        } else {
+            pixelsv2 = vec_perm(temp1, temp2, vec_lvsl(line_size + 1, pixels));
+        }
+
+        pixelsv1 = vec_mergeh(vczero, pixelsv1);
+        pixelsv2 = vec_mergeh(vczero, pixelsv2);
+        pixelssum2 = vec_add((vector unsigned short)pixelsv1,
+                             (vector unsigned short)pixelsv2);
+        temp3 = vec_add(pixelssum1, pixelssum2);
+        temp3 = vec_sra(temp3, vctwo);
+        pixelssum1 = vec_add(pixelssum2, vctwo);
+        pixelsavg = vec_packsu(temp3, (vector unsigned short) vczero);
+
+        if (rightside) {
+            blocktemp = vec_perm(blockv, pixelsavg, vcprm(0, 1, s0, s1));
+        } else {
+            blocktemp = vec_perm(blockv, pixelsavg, vcprm(s0, s1, 2, 3));
+        }
+
+        blockv = vec_avg(blocktemp, blockv);
+        vec_st(blockv, 0, block);
+
+        block += line_size;
+        pixels += line_size;
+    }
+}
+#endif /* HAVE_ALTIVEC */
+
+av_cold void ff_hpeldsp_init_ppc(HpelDSPContext *c, int flags)
+{
+#if HAVE_ALTIVEC
+    int mm_flags = av_get_cpu_flags();
+
+    if (mm_flags & AV_CPU_FLAG_ALTIVEC) {
+        c->avg_pixels_tab[0][0]        = ff_avg_pixels16_altivec;
+        c->avg_pixels_tab[1][0]        = avg_pixels8_altivec;
+        c->avg_pixels_tab[1][3]        = avg_pixels8_xy2_altivec;
+
+        c->put_pixels_tab[0][0]        = ff_put_pixels16_altivec;
+        c->put_pixels_tab[1][3]        = put_pixels8_xy2_altivec;
+        c->put_pixels_tab[0][3]        = put_pixels16_xy2_altivec;
+
+        c->put_no_rnd_pixels_tab[0][0] = ff_put_pixels16_altivec;
+        c->put_no_rnd_pixels_tab[1][3] = put_no_rnd_pixels8_xy2_altivec;
+        c->put_no_rnd_pixels_tab[0][3] = put_no_rnd_pixels16_xy2_altivec;
+    }
+#endif /* HAVE_ALTIVEC */
+}
diff --git a/libavcodec/ppc/vp3dsp_altivec.c b/libavcodec/ppc/vp3dsp_altivec.c
index 42ae00b..cc587b0 100644
--- a/libavcodec/ppc/vp3dsp_altivec.c
+++ b/libavcodec/ppc/vp3dsp_altivec.c
@@ -184,7 +184,6 @@
     if (av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC) {
         c->idct_put  = vp3_idct_put_altivec;
         c->idct_add  = vp3_idct_add_altivec;
-        c->idct_perm = FF_TRANSPOSE_IDCT_PERM;
     }
 #endif
 }
diff --git a/libavcodec/proresdec.h b/libavcodec/proresdec.h
index 1c56227..f42c448 100644
--- a/libavcodec/proresdec.h
+++ b/libavcodec/proresdec.h
@@ -5,16 +5,16 @@
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public
- * License as published by the Free Software Foundation;
- * version 2 of the License.
+ * 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
- * General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public
+ * 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
  */
@@ -37,7 +37,7 @@
 typedef struct {
     DSPContext dsp;
     ProresDSPContext prodsp;
-    AVFrame frame;
+    AVFrame *frame;
     int frame_type;              ///< 0 = progressive, 1 = tff, 2 = bff
     uint8_t qmat_luma[64];
     uint8_t qmat_chroma[64];
diff --git a/libavcodec/proresdec2.c b/libavcodec/proresdec2.c
index 4634e70..bd6404a 100644
--- a/libavcodec/proresdec2.c
+++ b/libavcodec/proresdec2.c
@@ -73,10 +73,6 @@
     ff_dsputil_init(&ctx->dsp, avctx);
     ff_proresdsp_init(&ctx->prodsp, avctx);
 
-    avctx->coded_frame = &ctx->frame;
-    ctx->frame.type = AV_PICTURE_TYPE_I;
-    ctx->frame.key_frame = 1;
-
     ff_init_scantable_permutation(idct_permutation,
                                   ctx->prodsp.idct_permutation_type);
 
@@ -123,8 +119,8 @@
         ctx->scan = ctx->progressive_scan; // permuted
     } else {
         ctx->scan = ctx->interlaced_scan; // permuted
-        ctx->frame.interlaced_frame = 1;
-        ctx->frame.top_field_first = ctx->frame_type == 1;
+        ctx->frame->interlaced_frame = 1;
+        ctx->frame->top_field_first = ctx->frame_type == 1;
     }
 
     avctx->pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUV444P10 : AV_PIX_FMT_YUV422P10;
@@ -431,7 +427,7 @@
     ProresContext *ctx = avctx->priv_data;
     SliceContext *slice = &ctx->slices[jobnr];
     const uint8_t *buf = slice->data;
-    AVFrame *pic = avctx->coded_frame;
+    AVFrame *pic = ctx->frame;
     int i, hdr_size, qscale, log2_chroma_blocks_per_mb;
     int luma_stride, chroma_stride;
     int y_data_size, u_data_size, v_data_size;
@@ -486,7 +482,7 @@
     dest_u = pic->data[1] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift);
     dest_v = pic->data[2] + (slice->mb_y << 4) * chroma_stride + (slice->mb_x << mb_x_shift);
 
-    if (ctx->frame_type && ctx->first_field ^ ctx->frame.top_field_first) {
+    if (ctx->frame_type && ctx->first_field ^ ctx->frame->top_field_first) {
         dest_y += pic->linesize[0];
         dest_u += pic->linesize[1];
         dest_v += pic->linesize[2];
@@ -526,7 +522,7 @@
                         AVPacket *avpkt)
 {
     ProresContext *ctx = avctx->priv_data;
-    AVFrame *frame = avctx->coded_frame;
+    AVFrame *frame = data;
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     int frame_hdr_size, pic_size;
@@ -536,6 +532,9 @@
         return -1;
     }
 
+    ctx->frame = frame;
+    ctx->frame->pict_type = AV_PICTURE_TYPE_I;
+    ctx->frame->key_frame = 1;
     ctx->first_field = 1;
 
     buf += 8;
@@ -548,10 +547,7 @@
     buf += frame_hdr_size;
     buf_size -= frame_hdr_size;
 
-    if (frame->data[0])
-        avctx->release_buffer(avctx, frame);
-
-    if (ff_get_buffer(avctx, frame) < 0)
+    if (ff_get_buffer(avctx, frame, 0) < 0)
         return -1;
 
  decode_picture:
@@ -575,7 +571,6 @@
     }
 
     *got_frame      = 1;
-    *(AVFrame*)data = *frame;
 
     return avpkt->size;
 }
@@ -584,9 +579,6 @@
 {
     ProresContext *ctx = avctx->priv_data;
 
-    AVFrame *frame = avctx->coded_frame;
-    if (frame->data[0])
-        avctx->release_buffer(avctx, frame);
     av_freep(&ctx->slices);
 
     return 0;
diff --git a/libavcodec/proresdec_lgpl.c b/libavcodec/proresdec_lgpl.c
index 5c53882..59c9843 100644
--- a/libavcodec/proresdec_lgpl.c
+++ b/libavcodec/proresdec_lgpl.c
@@ -53,7 +53,7 @@
 
 typedef struct {
     ProresDSPContext dsp;
-    AVFrame    picture;
+    AVFrame    *frame;
     ScanTable  scantable;
     int        scantable_type;           ///< -1 = uninitialized, 0 = progressive, 1/2 = interlaced
 
@@ -88,11 +88,6 @@
     avctx->bits_per_raw_sample = PRORES_BITS_PER_SAMPLE;
     ff_proresdsp_init(&ctx->dsp, avctx);
 
-    avctx->coded_frame = &ctx->picture;
-    avcodec_get_frame_defaults(&ctx->picture);
-    ctx->picture.type      = AV_PICTURE_TYPE_I;
-    ctx->picture.key_frame = 1;
-
     ctx->scantable_type = -1;   // set scantable type to uninitialized
     memset(ctx->qmat_luma, 4, 64);
     memset(ctx->qmat_chroma, 4, 64);
@@ -163,10 +158,10 @@
     }
 
     if (ctx->frame_type) {      /* if interlaced */
-        ctx->picture.interlaced_frame = 1;
-        ctx->picture.top_field_first  = ctx->frame_type & 1;
+        ctx->frame->interlaced_frame = 1;
+        ctx->frame->top_field_first  = ctx->frame_type & 1;
     } else {
-        ctx->picture.interlaced_frame = 0;
+        ctx->frame->interlaced_frame = 0;
     }
 
     avctx->color_primaries = buf[14];
@@ -175,7 +170,7 @@
 
     ctx->alpha_info = buf[17] & 0xf;
     if (ctx->alpha_info)
-        av_log_missing_feature(avctx, "Alpha channel", 0);
+        avpriv_report_missing_feature(avctx, "Alpha channel");
 
     ctx->qmat_changed = 0;
     ptr   = buf + 20;
@@ -247,8 +242,8 @@
 
     ctx->num_x_mbs = (avctx->width + 15) >> 4;
     ctx->num_y_mbs = (avctx->height +
-                      (1 << (4 + ctx->picture.interlaced_frame)) - 1) >>
-                     (4 + ctx->picture.interlaced_frame);
+                      (1 << (4 + ctx->frame->interlaced_frame)) - 1) >>
+                     (4 + ctx->frame->interlaced_frame);
 
     remainder    = ctx->num_x_mbs & ((1 << slice_width_factor) - 1);
     num_x_slices = (ctx->num_x_mbs >> slice_width_factor) + (remainder & 1) +
@@ -482,7 +477,7 @@
     int mbs_per_slice = td->slice_width;
     const uint8_t *buf;
     uint8_t *y_data, *u_data, *v_data;
-    AVFrame *pic = avctx->coded_frame;
+    AVFrame *pic = ctx->frame;
     int i, sf, slice_width_factor;
     int slice_data_size, hdr_size, y_data_size, u_data_size, v_data_size;
     int y_linesize, u_linesize, v_linesize;
@@ -606,11 +601,14 @@
                         AVPacket *avpkt)
 {
     ProresContext *ctx = avctx->priv_data;
-    AVFrame *picture   = avctx->coded_frame;
     const uint8_t *buf = avpkt->data;
     int buf_size       = avpkt->size;
     int frame_hdr_size, pic_num, pic_data_size;
 
+    ctx->frame            = data;
+    ctx->frame->pict_type = AV_PICTURE_TYPE_I;
+    ctx->frame->key_frame = 1;
+
     /* check frame atom container */
     if (buf_size < 28 || buf_size < AV_RB32(buf) ||
         AV_RB32(buf + 4) != FRAME_ID) {
@@ -626,14 +624,10 @@
 
     MOVE_DATA_PTR(frame_hdr_size);
 
-    if (picture->data[0])
-        avctx->release_buffer(avctx, picture);
-
-    picture->reference = 0;
-    if (ff_get_buffer(avctx, picture) < 0)
+    if (ff_get_buffer(avctx, ctx->frame, 0) < 0)
         return -1;
 
-    for (pic_num = 0; ctx->picture.interlaced_frame - pic_num + 1; pic_num++) {
+    for (pic_num = 0; ctx->frame->interlaced_frame - pic_num + 1; pic_num++) {
         pic_data_size = decode_picture_header(ctx, buf, buf_size, avctx);
         if (pic_data_size < 0)
             return AVERROR_INVALIDDATA;
@@ -644,8 +638,8 @@
         MOVE_DATA_PTR(pic_data_size);
     }
 
-    *got_frame       = 1;
-    *(AVFrame*) data = *avctx->coded_frame;
+    ctx->frame = NULL;
+    *got_frame = 1;
 
     return avpkt->size;
 }
@@ -655,9 +649,6 @@
 {
     ProresContext *ctx = avctx->priv_data;
 
-    if (ctx->picture.data[0])
-        avctx->release_buffer(avctx, &ctx->picture);
-
     av_freep(&ctx->slice_data);
 
     return 0;
diff --git a/libavcodec/proresdsp.c b/libavcodec/proresdsp.c
index d73de46..15e122f 100644
--- a/libavcodec/proresdsp.c
+++ b/libavcodec/proresdsp.c
@@ -56,7 +56,7 @@
 }
 #endif
 
-#if CONFIG_PRORES_KOSTYA_ENCODER
+#if CONFIG_PRORES_KS_ENCODER
 static void prores_fdct_c(const uint16_t *src, int linesize, int16_t *block)
 {
     int x, y;
@@ -82,7 +82,7 @@
     ff_init_scantable_permutation(dsp->idct_permutation,
                                   dsp->idct_permutation_type);
 #endif
-#if CONFIG_PRORES_KOSTYA_ENCODER
+#if CONFIG_PRORES_KS_ENCODER
     dsp->fdct                 = prores_fdct_c;
     dsp->dct_permutation_type = FF_NO_IDCT_PERM;
     ff_init_scantable_permutation(dsp->dct_permutation,
diff --git a/libavcodec/proresenc_anatoliy.c b/libavcodec/proresenc_anatoliy.c
index 7bf71a3..ccd0392 100644
--- a/libavcodec/proresenc_anatoliy.c
+++ b/libavcodec/proresenc_anatoliy.c
@@ -597,8 +597,8 @@
     return 0;
 }
 
-AVCodec ff_prores_anatoliy_encoder = {
-    .name           = "prores_anatoliy",
+AVCodec ff_prores_aw_encoder = {
+    .name           = "prores_aw",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_PRORES,
     .priv_data_size = sizeof(ProresContext),
diff --git a/libavcodec/proresenc_kostya.c b/libavcodec/proresenc_kostya.c
index 8da13ac..2d6dcce 100644
--- a/libavcodec/proresenc_kostya.c
+++ b/libavcodec/proresenc_kostya.c
@@ -1060,8 +1060,8 @@
     .version    = LIBAVUTIL_VERSION_INT,
 };
 
-AVCodec ff_prores_kostya_encoder = {
-    .name           = "prores_kostya",
+AVCodec ff_prores_ks_encoder = {
+    .name           = "prores_ks",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_PRORES,
     .priv_data_size = sizeof(ProresContext),
diff --git a/libavcodec/psymodel.c b/libavcodec/psymodel.c
index a4a7cbb..ea11636 100644
--- a/libavcodec/psymodel.c
+++ b/libavcodec/psymodel.c
@@ -88,6 +88,7 @@
     float stereo_att;
     struct FFIIRFilterCoeffs *fcoeffs;
     struct FFIIRFilterState **fstate;
+    struct FFIIRFilterContext fiir;
 }FFPsyPreprocessContext;
 
 #define FILT_ORDER 4
@@ -106,7 +107,7 @@
     if (!cutoff_coeff && avctx->codec_id == AV_CODEC_ID_AAC)
         cutoff_coeff = 2.0 * AAC_CUTOFF(avctx) / avctx->sample_rate;
 
-    if (cutoff_coeff)
+    if (cutoff_coeff && cutoff_coeff < 0.98)
     ctx->fcoeffs = ff_iir_filter_init_coeffs(avctx, FF_FILTER_TYPE_BUTTERWORTH,
                                              FF_FILTER_MODE_LOWPASS, FILT_ORDER,
                                              cutoff_coeff, 0.0, 0.0);
@@ -115,6 +116,9 @@
         for (i = 0; i < avctx->channels; i++)
             ctx->fstate[i] = ff_iir_filter_init_state(FILT_ORDER);
     }
+
+    ff_iir_filter_init(&ctx->fiir);
+
     return ctx;
 }
 
@@ -122,11 +126,12 @@
 {
     int ch;
     int frame_size = ctx->avctx->frame_size;
+    FFIIRFilterContext *iir = &ctx->fiir;
 
     if (ctx->fstate) {
         for (ch = 0; ch < channels; ch++)
-            ff_iir_filter_flt(ctx->fcoeffs, ctx->fstate[ch], frame_size,
-                              &audio[ch][frame_size], 1, &audio[ch][frame_size], 1);
+            iir->filter_flt(ctx->fcoeffs, ctx->fstate[ch], frame_size,
+                            &audio[ch][frame_size], 1, &audio[ch][frame_size], 1);
     }
 }
 
diff --git a/libavcodec/pthread.c b/libavcodec/pthread.c
index 1ca72b4..1e33fbc 100644
--- a/libavcodec/pthread.c
+++ b/libavcodec/pthread.c
@@ -55,6 +55,7 @@
 #include "avcodec.h"
 #include "internal.h"
 #include "thread.h"
+#include "libavutil/avassert.h"
 #include "libavutil/common.h"
 
 #if HAVE_PTHREADS
@@ -86,9 +87,6 @@
     int done;
 } ThreadContext;
 
-/// Max number of frame buffers that can be allocated when using frame threads.
-#define MAX_BUFFERS (34+1)
-
 /**
  * Context used by codec threads and stored in their AVCodecContext thread_opaque.
  */
@@ -107,7 +105,8 @@
     AVCodecContext *avctx;          ///< Context used to decode packets passed to this thread.
 
     AVPacket       avpkt;           ///< Input packet (for decoding) or output (for encoding).
-    int            allocated_buf_size; ///< Size allocated for avpkt.data
+    uint8_t       *buf;             ///< backup storage for packet data when the input packet is not refcounted
+    int            allocated_buf_size; ///< Size allocated for buf
 
     AVFrame frame;                  ///< Output frame (for decoding) or input (for encoding).
     int     got_frame;              ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
@@ -120,6 +119,10 @@
                                      * Set when the codec calls get_buffer().
                                      * State is returned to STATE_SETTING_UP afterwards.
                                      */
+        STATE_GET_FORMAT,           /**<
+                                     * Set when the codec calls get_format().
+                                     * State is returned to STATE_SETTING_UP afterwards.
+                                     */
         STATE_SETUP_FINISHED        ///< Set after the codec has called ff_thread_finish_setup().
     } state;
 
@@ -127,16 +130,15 @@
      * Array of frames passed to ff_thread_release_buffer().
      * Frames are released after all threads referencing them are finished.
      */
-    AVFrame released_buffers[MAX_BUFFERS];
-    int     num_released_buffers;
-
-    /**
-     * Array of progress values used by ff_thread_get_buffer().
-     */
-    volatile int     progress[MAX_BUFFERS][2];
-    volatile uint8_t progress_used[MAX_BUFFERS];
+    AVFrame *released_buffers;
+    int  num_released_buffers;
+    int      released_buffers_allocated;
 
     AVFrame *requested_frame;       ///< AVFrame the codec passed to get_buffer()
+    int      requested_flags;       ///< flags passed to get_buffer() for requested_frame
+
+    const enum AVPixelFormat *available_formats; ///< Format array for get_format()
+    enum AVPixelFormat result_format;            ///< get_format() result
 } PerThreadContext;
 
 /**
@@ -376,13 +378,16 @@
 
     pthread_mutex_lock(&p->mutex);
     while (1) {
-        int i;
             while (p->state == STATE_INPUT_READY && !fctx->die)
                 pthread_cond_wait(&p->input_cond, &p->mutex);
 
         if (fctx->die) break;
 
-        if (!codec->update_thread_context && (avctx->thread_safe_callbacks || avctx->get_buffer == avcodec_default_get_buffer))
+        if (!codec->update_thread_context && (avctx->thread_safe_callbacks || (
+#if FF_API_GET_BUFFER
+            !avctx->get_buffer &&
+#endif
+            avctx->get_buffer2 == avcodec_default_get_buffer2)))
             ff_thread_finish_setup(avctx);
 
         avcodec_get_frame_defaults(&p->frame);
@@ -396,11 +401,13 @@
         if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
 
         pthread_mutex_lock(&p->progress_mutex);
+#if 0 //BUFREF-FIXME
         for (i = 0; i < MAX_BUFFERS; i++)
             if (p->progress_used[i] && (p->got_frame || p->result<0 || avctx->codec_id != AV_CODEC_ID_H264)) {
                 p->progress[i][0] = INT_MAX;
                 p->progress[i][1] = INT_MAX;
             }
+#endif
         p->state = STATE_INPUT_READY;
 
         pthread_cond_broadcast(&p->progress_cond);
@@ -450,6 +457,9 @@
         dst->colorspace  = src->colorspace;
         dst->color_range = src->color_range;
         dst->chroma_sample_location = src->chroma_sample_location;
+
+        dst->hwaccel = src->hwaccel;
+        dst->hwaccel_context = src->hwaccel_context;
     }
 
     if (for_user) {
@@ -476,8 +486,11 @@
     dst->flags          = src->flags;
 
     dst->draw_horiz_band= src->draw_horiz_band;
+    dst->get_buffer2    = src->get_buffer2;
+#if FF_API_GET_BUFFER
     dst->get_buffer     = src->get_buffer;
     dst->release_buffer = src->release_buffer;
+#endif
 
     dst->opaque   = src->opaque;
     dst->debug    = src->debug;
@@ -510,14 +523,6 @@
 #undef copy_fields
 }
 
-static void free_progress(AVFrame *f)
-{
-    PerThreadContext *p = f->owner->thread_opaque;
-    volatile int *progress = f->thread_opaque;
-
-    p->progress_used[(progress - p->progress[0]) / 2] = 0;
-}
-
 /// Releases the buffers that this decoding thread was the last user of.
 static void release_delayed_buffers(PerThreadContext *p)
 {
@@ -527,11 +532,13 @@
         AVFrame *f;
 
         pthread_mutex_lock(&fctx->buffer_mutex);
-        f = &p->released_buffers[--p->num_released_buffers];
-        free_progress(f);
-        f->thread_opaque = NULL;
 
-        f->owner->release_buffer(f->owner, f);
+        // fix extended data in case the caller screwed it up
+        av_assert0(p->avctx->codec_type == AVMEDIA_TYPE_VIDEO);
+        f = &p->released_buffers[--p->num_released_buffers];
+        f->extended_data = f->data;
+        av_frame_unref(f);
+
         pthread_mutex_unlock(&fctx->buffer_mutex);
     }
 }
@@ -541,7 +548,6 @@
     FrameThreadContext *fctx = p->parent;
     PerThreadContext *prev_thread = fctx->prev_thread;
     const AVCodec *codec = p->avctx->codec;
-    uint8_t *buf = p->avpkt.data;
 
     if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
 
@@ -565,11 +571,16 @@
         }
     }
 
-    av_fast_malloc(&buf, &p->allocated_buf_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
+    av_buffer_unref(&p->avpkt.buf);
     p->avpkt = *avpkt;
-    p->avpkt.data = buf;
-    memcpy(buf, avpkt->data, avpkt->size);
-    memset(buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
+    if (avpkt->buf)
+        p->avpkt.buf = av_buffer_ref(avpkt->buf);
+    else {
+        av_fast_malloc(&p->buf, &p->allocated_buf_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
+        p->avpkt.data = p->buf;
+        memcpy(p->buf, avpkt->data, avpkt->size);
+        memset(p->buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
+    }
 
     p->state = STATE_SETTING_UP;
     pthread_cond_signal(&p->input_cond);
@@ -581,15 +592,30 @@
      * and it calls back to the client here.
      */
 
-    if (!p->avctx->thread_safe_callbacks &&
-         p->avctx->get_buffer != avcodec_default_get_buffer) {
+    if (!p->avctx->thread_safe_callbacks && (
+         p->avctx->get_format != avcodec_default_get_format ||
+#if FF_API_GET_BUFFER
+         p->avctx->get_buffer ||
+#endif
+         p->avctx->get_buffer2 != avcodec_default_get_buffer2)) {
         while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
+            int call_done = 1;
             pthread_mutex_lock(&p->progress_mutex);
             while (p->state == STATE_SETTING_UP)
                 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
 
-            if (p->state == STATE_GET_BUFFER) {
-                p->result = ff_get_buffer(p->avctx, p->requested_frame);
+            switch (p->state) {
+            case STATE_GET_BUFFER:
+                p->result = ff_get_buffer(p->avctx, p->requested_frame, p->requested_flags);
+                break;
+            case STATE_GET_FORMAT:
+                p->result_format = p->avctx->get_format(p->avctx, p->available_formats);
+                break;
+            default:
+                call_done = 0;
+                break;
+            }
+            if (call_done) {
                 p->state  = STATE_SETTING_UP;
                 pthread_cond_signal(&p->progress_cond);
             }
@@ -651,7 +677,7 @@
             pthread_mutex_unlock(&p->progress_mutex);
         }
 
-        *picture = p->frame;
+        av_frame_move_ref(picture, &p->frame);
         *got_picture_ptr = p->got_frame;
         picture->pkt_dts = p->avpkt.dts;
 
@@ -676,10 +702,10 @@
     return (p->result >= 0) ? avpkt->size : p->result;
 }
 
-void ff_thread_report_progress(AVFrame *f, int n, int field)
+void ff_thread_report_progress(ThreadFrame *f, int n, int field)
 {
     PerThreadContext *p;
-    volatile int *progress = f->thread_opaque;
+    volatile int *progress = f->progress ? (int*)f->progress->data : NULL;
 
     if (!progress || progress[field] >= n) return;
 
@@ -694,10 +720,10 @@
     pthread_mutex_unlock(&p->progress_mutex);
 }
 
-void ff_thread_await_progress(AVFrame *f, int n, int field)
+void ff_thread_await_progress(ThreadFrame *f, int n, int field)
 {
     PerThreadContext *p;
-    volatile int *progress = f->thread_opaque;
+    volatile int *progress = f->progress ? (int*)f->progress->data : NULL;
 
     if (!progress || progress[field] >= n) return;
 
@@ -779,19 +805,20 @@
         avctx->codec = NULL;
 
         release_delayed_buffers(p);
+        av_frame_unref(&p->frame);
     }
 
     for (i = 0; i < thread_count; i++) {
         PerThreadContext *p = &fctx->threads[i];
 
-        avcodec_default_free_buffers(p->avctx);
-
         pthread_mutex_destroy(&p->mutex);
         pthread_mutex_destroy(&p->progress_mutex);
         pthread_cond_destroy(&p->input_cond);
         pthread_cond_destroy(&p->progress_cond);
         pthread_cond_destroy(&p->output_cond);
-        av_freep(&p->avpkt.data);
+        av_buffer_unref(&p->avpkt.buf);
+        av_freep(&p->buf);
+        av_freep(&p->released_buffers);
 
         if (i) {
             av_freep(&p->avctx->priv_data);
@@ -923,78 +950,71 @@
         PerThreadContext *p = &fctx->threads[i];
         // Make sure decode flush calls with size=0 won't return old frames
         p->got_frame = 0;
+        av_frame_unref(&p->frame);
 
         release_delayed_buffers(p);
     }
 }
 
-static volatile int *allocate_progress(PerThreadContext *p)
-{
-    int i;
-
-    for (i = 0; i < MAX_BUFFERS; i++)
-        if (!p->progress_used[i]) break;
-
-    if (i == MAX_BUFFERS) {
-        av_log(p->avctx, AV_LOG_ERROR, "allocate_progress() overflow\n");
-        return NULL;
-    }
-
-    p->progress_used[i] = 1;
-
-    return p->progress[i];
-}
-
 int ff_thread_can_start_frame(AVCodecContext *avctx)
 {
     PerThreadContext *p = avctx->thread_opaque;
     if ((avctx->active_thread_type&FF_THREAD_FRAME) && p->state != STATE_SETTING_UP &&
-        (avctx->codec->update_thread_context || (!avctx->thread_safe_callbacks &&
-                avctx->get_buffer != avcodec_default_get_buffer))) {
+        (avctx->codec->update_thread_context || (!avctx->thread_safe_callbacks && (
+#if FF_API_GET_BUFFER
+                avctx->get_buffer ||
+#endif
+                avctx->get_buffer2 != avcodec_default_get_buffer2)))) {
         return 0;
     }
     return 1;
 }
 
-int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
+static int thread_get_buffer_internal(AVCodecContext *avctx, ThreadFrame *f, int flags)
 {
     PerThreadContext *p = avctx->thread_opaque;
     int err;
-    volatile int *progress;
 
     f->owner = avctx;
 
-    ff_init_buffer_info(avctx, f);
+    ff_init_buffer_info(avctx, f->f);
 
-    if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
-        f->thread_opaque = NULL;
-        return ff_get_buffer(avctx, f);
-    }
+    if (!(avctx->active_thread_type & FF_THREAD_FRAME))
+        return ff_get_buffer(avctx, f->f, flags);
 
     if (p->state != STATE_SETTING_UP &&
-        (avctx->codec->update_thread_context || (!avctx->thread_safe_callbacks &&
-                avctx->get_buffer != avcodec_default_get_buffer))) {
+        (avctx->codec->update_thread_context || (!avctx->thread_safe_callbacks && (
+#if FF_API_GET_BUFFER
+                avctx->get_buffer ||
+#endif
+                avctx->get_buffer2 != avcodec_default_get_buffer2)))) {
         av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
         return -1;
     }
 
-    pthread_mutex_lock(&p->parent->buffer_mutex);
-    f->thread_opaque = (int*)(progress = allocate_progress(p));
+    if (avctx->internal->allocate_progress) {
+        int *progress;
+        f->progress = av_buffer_alloc(2 * sizeof(int));
+        if (!f->progress) {
+            return AVERROR(ENOMEM);
+        }
+        progress = (int*)f->progress->data;
 
-    if (!progress) {
-        pthread_mutex_unlock(&p->parent->buffer_mutex);
-        return -1;
+        progress[0] = progress[1] = -1;
     }
 
-    progress[0] =
-    progress[1] = -1;
+    pthread_mutex_lock(&p->parent->buffer_mutex);
 
-    if (avctx->thread_safe_callbacks ||
-        avctx->get_buffer == avcodec_default_get_buffer) {
-        err = ff_get_buffer(avctx, f);
+    if (avctx->thread_safe_callbacks || (
+#if FF_API_GET_BUFFER
+        !avctx->get_buffer &&
+#endif
+        avctx->get_buffer2 == avcodec_default_get_buffer2)) {
+        err = ff_get_buffer(avctx, f->f, flags);
     } else {
         pthread_mutex_lock(&p->progress_mutex);
-        p->requested_frame = f;
+        p->requested_frame = f->f;
+        p->requested_flags = flags;
         p->state = STATE_GET_BUFFER;
         pthread_cond_broadcast(&p->progress_cond);
 
@@ -1005,45 +1025,98 @@
 
         pthread_mutex_unlock(&p->progress_mutex);
 
-        if (!avctx->codec->update_thread_context)
-            ff_thread_finish_setup(avctx);
     }
+    if (!avctx->thread_safe_callbacks && !avctx->codec->update_thread_context)
+        ff_thread_finish_setup(avctx);
 
-    if (err) {
-        free_progress(f);
-        f->thread_opaque = NULL;
-    }
+    if (err)
+        av_buffer_unref(&f->progress);
+
     pthread_mutex_unlock(&p->parent->buffer_mutex);
 
     return err;
 }
 
-void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
+enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
+{
+    enum AVPixelFormat res;
+    PerThreadContext *p = avctx->thread_opaque;
+    if (!(avctx->active_thread_type & FF_THREAD_FRAME) || avctx->thread_safe_callbacks ||
+        avctx->get_format == avcodec_default_get_format)
+        return avctx->get_format(avctx, fmt);
+    if (p->state != STATE_SETTING_UP) {
+        av_log(avctx, AV_LOG_ERROR, "get_format() cannot be called after ff_thread_finish_setup()\n");
+        return -1;
+    }
+    pthread_mutex_lock(&p->progress_mutex);
+    p->available_formats = fmt;
+    p->state = STATE_GET_FORMAT;
+    pthread_cond_broadcast(&p->progress_cond);
+
+    while (p->state != STATE_SETTING_UP)
+        pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
+
+    res = p->result_format;
+
+    pthread_mutex_unlock(&p->progress_mutex);
+
+    return res;
+}
+
+int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
+{
+    int ret = thread_get_buffer_internal(avctx, f, flags);
+    if (ret < 0)
+        av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n");
+    return ret;
+}
+
+void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
 {
     PerThreadContext *p = avctx->thread_opaque;
     FrameThreadContext *fctx;
+    AVFrame *dst, *tmp;
+    int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
+                          avctx->thread_safe_callbacks                   ||
+                          (
+#if FF_API_GET_BUFFER
+                           !avctx->get_buffer &&
+#endif
+                           avctx->get_buffer2 == avcodec_default_get_buffer2);
 
-    if (!f->data[0])
+    if (!f->f->data[0])
         return;
 
-    if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
-        avctx->release_buffer(avctx, f);
-        return;
-    }
-
-    if (p->num_released_buffers >= MAX_BUFFERS) {
-        av_log(p->avctx, AV_LOG_ERROR, "too many thread_release_buffer calls!\n");
-        return;
-    }
-
-    if(avctx->debug & FF_DEBUG_BUFFERS)
+    if (avctx->debug & FF_DEBUG_BUFFERS)
         av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
 
+    av_buffer_unref(&f->progress);
+    f->owner    = NULL;
+
+    if (can_direct_free) {
+        av_frame_unref(f->f);
+        return;
+    }
+
     fctx = p->parent;
     pthread_mutex_lock(&fctx->buffer_mutex);
-    p->released_buffers[p->num_released_buffers++] = *f;
+
+    if (p->num_released_buffers + 1 >= INT_MAX / sizeof(*p->released_buffers))
+        goto fail;
+    tmp = av_fast_realloc(p->released_buffers, &p->released_buffers_allocated,
+                          (p->num_released_buffers + 1) *
+                          sizeof(*p->released_buffers));
+    if (!tmp)
+        goto fail;
+    p->released_buffers = tmp;
+
+    dst = &p->released_buffers[p->num_released_buffers];
+    av_frame_move_ref(dst, f->f);
+
+    p->num_released_buffers++;
+
+fail:
     pthread_mutex_unlock(&fctx->buffer_mutex);
-    memset(f->data, 0, sizeof(f->data));
 }
 
 /**
diff --git a/libavcodec/ptx.c b/libavcodec/ptx.c
index 1a76c55..37135c5 100644
--- a/libavcodec/ptx.c
+++ b/libavcodec/ptx.c
@@ -25,26 +25,11 @@
 #include "avcodec.h"
 #include "internal.h"
 
-typedef struct PTXContext {
-    AVFrame picture;
-} PTXContext;
-
-static av_cold int ptx_init(AVCodecContext *avctx) {
-    PTXContext *s = avctx->priv_data;
-
-    avcodec_get_frame_defaults(&s->picture);
-    avctx->coded_frame= &s->picture;
-
-    return 0;
-}
-
 static int ptx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
                             AVPacket *avpkt) {
     const uint8_t *buf = avpkt->data;
     const uint8_t *buf_end = avpkt->data + avpkt->size;
-    PTXContext * const s = avctx->priv_data;
-    AVFrame *picture = data;
-    AVFrame * const p = &s->picture;
+    AVFrame * const p = data;
     unsigned int offset, w, h, y, stride, bytes_per_pixel;
     int ret;
     uint8_t *ptr;
@@ -57,7 +42,7 @@
     bytes_per_pixel = AV_RL16(buf+12) >> 3;
 
     if (bytes_per_pixel != 2) {
-        av_log_ask_for_sample(avctx, "Image format is not RGB15.\n");
+        avpriv_request_sample(avctx, "Image format not RGB15");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -66,21 +51,16 @@
     if (buf_end - buf < offset)
         return AVERROR_INVALIDDATA;
     if (offset != 0x2c)
-        av_log_ask_for_sample(avctx, "offset != 0x2c\n");
+        avpriv_request_sample(avctx, "offset != 0x2c");
 
     buf += offset;
 
-    if (p->data[0])
-        avctx->release_buffer(avctx, p);
-
     if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
         return ret;
     if (w != avctx->width || h != avctx->height)
         avcodec_set_dimensions(avctx, w, h);
-    if ((ret = ff_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
         return ret;
-    }
 
     p->pict_type = AV_PICTURE_TYPE_I;
 
@@ -93,7 +73,6 @@
         buf += w*bytes_per_pixel;
     }
 
-    *picture = s->picture;
     *got_frame = 1;
 
     if (y < h) {
@@ -104,22 +83,10 @@
     return offset + w*h*bytes_per_pixel;
 }
 
-static av_cold int ptx_end(AVCodecContext *avctx) {
-    PTXContext *s = avctx->priv_data;
-
-    if(s->picture.data[0])
-        avctx->release_buffer(avctx, &s->picture);
-
-    return 0;
-}
-
 AVCodec ff_ptx_decoder = {
     .name           = "ptx",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_PTX,
-    .priv_data_size = sizeof(PTXContext),
-    .init           = ptx_init,
-    .close          = ptx_end,
     .decode         = ptx_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("V.Flash PTX image"),
diff --git a/libavcodec/qcelpdec.c b/libavcodec/qcelpdec.c
index 76b51f8..f8fe85d 100644
--- a/libavcodec/qcelpdec.c
+++ b/libavcodec/qcelpdec.c
@@ -635,8 +635,8 @@
         return I_F_Q;
 
     if (bitrate == SILENCE) {
-        //FIXME: Remove experimental warning when tested with samples.
-        av_log_ask_for_sample(avctx, "'Blank frame handling is experimental.");
+        // FIXME: Remove this warning when tested with samples.
+        avpriv_request_sample(avctx, "Blank frame handling");
     }
     return bitrate;
 }
@@ -695,10 +695,8 @@
 
     /* get output buffer */
     frame->nb_samples = 160;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     outbuffer = (float *)frame->data[0];
 
     if ((q->bitrate = determine_bitrate(avctx, buf_size, &buf)) == I_F_Q) {
diff --git a/libavcodec/qdm2.c b/libavcodec/qdm2.c
index 283d8e6..daf127e 100644
--- a/libavcodec/qdm2.c
+++ b/libavcodec/qdm2.c
@@ -646,7 +646,7 @@
 
     if (!superblocktype_2_3) {
         /* This case is untested, no samples available */
-        av_log_ask_for_sample(NULL, "!superblocktype_2_3");
+        avpriv_request_sample(NULL, "!superblocktype_2_3");
         return;
         for (ch = 0; ch < nb_channels; ch++)
             for (sb = 0; sb < 30; sb++) {
@@ -824,6 +824,11 @@
                                 }
                             } else {
                                 n = get_bits(gb, 8);
+                                if (n >= 243) {
+                                    av_log(NULL, AV_LOG_ERROR, "Invalid 8bit codeword\n");
+                                    return AVERROR_INVALIDDATA;
+                                }
+
                                 for (k = 0; k < 5; k++)
                                     samples[2 * k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
                             }
@@ -860,6 +865,11 @@
                                 }
                             } else {
                                 n = get_bits (gb, 8);
+                                if (n >= 243) {
+                                    av_log(NULL, AV_LOG_ERROR, "Invalid 8bit codeword\n");
+                                    return AVERROR_INVALIDDATA;
+                                }
+
                                 for (k = 0; k < 5; k++)
                                     samples[k] = dequant_1bit[joined_stereo][random_dequant_index[n][k]];
                             }
@@ -873,6 +883,11 @@
                     case 24:
                         if (get_bits_left(gb) >= 7) {
                             n = get_bits(gb, 7);
+                            if (n >= 125) {
+                                av_log(NULL, AV_LOG_ERROR, "Invalid 7bit codeword\n");
+                                return AVERROR_INVALIDDATA;
+                            }
+
                             for (k = 0; k < 3; k++)
                                 samples[k] = (random_dequant_type24[n][k] - 2.0) * 0.5;
                         } else {
@@ -1847,18 +1862,9 @@
     if ((tmp * 2240) < avctx->bit_rate)  tmp_val = 4;
     s->cm_table_select = tmp_val;
 
-    if (s->sub_sampling == 0)
-        tmp = 7999;
-    else
-        tmp = ((-(s->sub_sampling -1)) & 8000) + 20000;
-    /*
-    0: 7999 -> 0
-    1: 20000 -> 2
-    2: 28000 -> 2
-    */
-    if (tmp < 8000)
+    if (avctx->bit_rate <= 8000)
         s->coeff_per_sb_select = 0;
-    else if (tmp <= 16000)
+    else if (avctx->bit_rate < 16000)
         s->coeff_per_sb_select = 1;
     else
         s->coeff_per_sb_select = 2;
@@ -1868,6 +1874,10 @@
         av_log(avctx, AV_LOG_ERROR, "Unknown FFT order (%d), contact the developers!\n", s->fft_order);
         return -1;
     }
+    if (s->fft_size != (1 << (s->fft_order - 1))) {
+        av_log(avctx, AV_LOG_ERROR, "FFT size %d not power of 2.\n", s->fft_size);
+        return AVERROR_INVALIDDATA;
+    }
 
     ff_rdft_init(&s->rdft_ctx, s->fft_order, IDFT_C2R);
     ff_mpadsp_init(&s->mpadsp);
@@ -1970,10 +1980,8 @@
 
     /* get output buffer */
     frame->nb_samples = 16 * s->frame_size;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     out = (int16_t *)frame->data[0];
 
     for (i = 0; i < 16; i++) {
diff --git a/libavcodec/qdm2_tablegen.h b/libavcodec/qdm2_tablegen.h
index a90682f..13ec9be 100644
--- a/libavcodec/qdm2_tablegen.h
+++ b/libavcodec/qdm2_tablegen.h
@@ -54,8 +54,7 @@
 // random generated table
 static av_cold void rnd_table_init(void) {
     int i,j;
-    uint32_t ldw,hdw;
-    uint64_t tmp64_1;
+    uint32_t ldw;
     uint64_t random_seed = 0;
     float delta = 1.0 / 16384.0;
     for(i = 0; i < 4096 ;i++) {
@@ -67,22 +66,18 @@
         random_seed = 81;
         ldw = i;
         for (j = 0; j < 5 ;j++) {
-            random_dequant_index[i][j] = (uint8_t)((ldw / random_seed) & 0xFF);
-            ldw = (uint32_t)ldw % (uint32_t)random_seed;
-            tmp64_1 = (random_seed * 0x55555556);
-            hdw = (uint32_t)(tmp64_1 >> 32);
-            random_seed = (uint64_t)(hdw + (ldw >> 31));
+            random_dequant_index[i][j] = ldw / random_seed;
+            ldw %= random_seed;
+            random_seed /= 3;
         }
     }
     for (i = 0; i < 128 ;i++) {
         random_seed = 25;
         ldw = i;
         for (j = 0; j < 3 ;j++) {
-            random_dequant_type24[i][j] = (uint8_t)((ldw / random_seed) & 0xFF);
-            ldw = (uint32_t)ldw % (uint32_t)random_seed;
-            tmp64_1 = (random_seed * 0x66666667);
-            hdw = (uint32_t)(tmp64_1 >> 33);
-            random_seed = hdw + (ldw >> 31);
+            random_dequant_type24[i][j] = ldw / random_seed;
+            ldw %= random_seed;
+            random_seed /= 5;
         }
     }
 }
diff --git a/libavcodec/qdrw.c b/libavcodec/qdrw.c
index 4ad64aa..6970099 100644
--- a/libavcodec/qdrw.c
+++ b/libavcodec/qdrw.c
@@ -29,11 +29,6 @@
 #include "avcodec.h"
 #include "internal.h"
 
-typedef struct QdrawContext {
-    AVCodecContext *avctx;
-    AVFrame pic;
-} QdrawContext;
-
 static int decode_frame(AVCodecContext *avctx,
                         void *data, int *got_frame,
                         AVPacket *avpkt)
@@ -41,26 +36,19 @@
     const uint8_t *buf     = avpkt->data;
     const uint8_t *buf_end = avpkt->data + avpkt->size;
     int buf_size           = avpkt->size;
-    QdrawContext * const a = avctx->priv_data;
-    AVFrame * const p      = &a->pic;
+    AVFrame * const p      = data;
     uint8_t* outdata;
     int colors;
     int i, ret;
     uint32_t *pal;
     int r, g, b;
 
-    if (p->data[0])
-        avctx->release_buffer(avctx, p);
-
-    p->reference = 0;
-    if ((ret = ff_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
         return ret;
-    }
     p->pict_type = AV_PICTURE_TYPE_I;
     p->key_frame = 1;
 
-    outdata = a->pic.data[0];
+    outdata = p->data[0];
 
     if (buf_end - buf < 0x68 + 4)
         return AVERROR_INVALIDDATA;
@@ -118,14 +106,14 @@
             code = *buf++;
             if (code & 0x80 ) { /* run */
                 pix = *buf++;
-                if ((out + (257 - code)) > (outdata +  a->pic.linesize[0]))
+                if ((out + (257 - code)) > (outdata +  p->linesize[0]))
                     break;
                 memset(out, pix, 257 - code);
                 out   += 257 - code;
                 tsize += 257 - code;
                 left  -= 2;
             } else { /* copy */
-                if ((out + code) > (outdata +  a->pic.linesize[0]))
+                if ((out + code) > (outdata +  p->linesize[0]))
                     break;
                 if (buf_end - buf < code + 1)
                     return AVERROR_INVALIDDATA;
@@ -137,43 +125,26 @@
             }
         }
         buf = next;
-        outdata += a->pic.linesize[0];
+        outdata += p->linesize[0];
     }
 
     *got_frame      = 1;
-    *(AVFrame*)data = a->pic;
 
     return buf_size;
 }
 
 static av_cold int decode_init(AVCodecContext *avctx)
 {
-    QdrawContext * const a = avctx->priv_data;
-
-    avcodec_get_frame_defaults(&a->pic);
     avctx->pix_fmt= AV_PIX_FMT_PAL8;
 
     return 0;
 }
 
-static av_cold int decode_end(AVCodecContext *avctx)
-{
-    QdrawContext * const a = avctx->priv_data;
-    AVFrame *pic = &a->pic;
-
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
-
-    return 0;
-}
-
 AVCodec ff_qdraw_decoder = {
     .name           = "qdraw",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_QDRAW,
-    .priv_data_size = sizeof(QdrawContext),
     .init           = decode_init,
-    .close          = decode_end,
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
diff --git a/libavcodec/qpeg.c b/libavcodec/qpeg.c
index 73d652e..6015b7f 100644
--- a/libavcodec/qpeg.c
+++ b/libavcodec/qpeg.c
@@ -268,15 +268,11 @@
 
     bytestream2_init(&a->buffer, avpkt->data, avpkt->size);
 
-    if(ref->data[0])
-        avctx->release_buffer(avctx, ref);
-    FFSWAP(AVFrame, *ref, *p);
+    av_frame_unref(ref);
+    av_frame_move_ref(ref, p);
 
-    p->reference= 3;
-    if ((ret = ff_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF)) < 0)
         return ret;
-    }
     outdata = a->pic.data[0];
     bytestream2_skip(&a->buffer, 4);
     bytestream2_get_buffer(&a->buffer, ctable, 128);
@@ -296,8 +292,10 @@
     }
     memcpy(a->pic.data[1], a->pal, AVPALETTE_SIZE);
 
+    if ((ret = av_frame_ref(data, &a->pic)) < 0)
+        return ret;
+
     *got_frame      = 1;
-    *(AVFrame*)data = a->pic;
 
     return avpkt->size;
 }
@@ -324,6 +322,8 @@
 
     decode_flush(avctx);
 
+    avcodec_get_frame_defaults(&a->pic);
+
     return 0;
 }
 
@@ -332,10 +332,8 @@
     AVFrame * const p = &a->pic;
     AVFrame * const ref= &a->ref;
 
-    if(p->data[0])
-        avctx->release_buffer(avctx, p);
-    if(ref->data[0])
-        avctx->release_buffer(avctx, ref);
+    av_frame_unref(p);
+    av_frame_unref(ref);
 
     return 0;
 }
diff --git a/libavcodec/qtrle.c b/libavcodec/qtrle.c
index 1b92885..c043249 100644
--- a/libavcodec/qtrle.c
+++ b/libavcodec/qtrle.c
@@ -37,6 +37,7 @@
 
 #include "avcodec.h"
 #include "bytestream.h"
+#include "internal.h"
 
 typedef struct QtrleContext {
     AVCodecContext *avctx;
@@ -396,7 +397,6 @@
     }
 
     avcodec_get_frame_defaults(&s->frame);
-    s->frame.data[0] = NULL;
 
     return 0;
 }
@@ -412,13 +412,8 @@
     int ret;
 
     bytestream2_init(&s->g, avpkt->data, avpkt->size);
-    s->frame.reference = 3;
-    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
-                            FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
-    if ((ret = avctx->reget_buffer(avctx, &s->frame)) < 0) {
-        av_log (s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0)
         return ret;
-    }
 
     /* check if this frame is even supposed to change */
     if (avpkt->size < 8)
@@ -501,8 +496,9 @@
     }
 
 done:
+    if ((ret = av_frame_ref(data, &s->frame)) < 0)
+        return ret;
     *got_frame      = 1;
-    *(AVFrame*)data = s->frame;
 
     /* always report that the buffer was completely consumed */
     return avpkt->size;
@@ -512,8 +508,7 @@
 {
     QtrleContext *s = avctx->priv_data;
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+    av_frame_unref(&s->frame);
 
     return 0;
 }
diff --git a/libavcodec/r210dec.c b/libavcodec/r210dec.c
index b58f11f..198914c 100644
--- a/libavcodec/r210dec.c
+++ b/libavcodec/r210dec.c
@@ -30,10 +30,6 @@
     avctx->pix_fmt             = AV_PIX_FMT_RGB48;
     avctx->bits_per_raw_sample = 10;
 
-    avctx->coded_frame         = avcodec_alloc_frame();
-    if (!avctx->coded_frame)
-        return AVERROR(ENOMEM);
-
     return 0;
 }
 
@@ -41,22 +37,18 @@
                         AVPacket *avpkt)
 {
     int h, w, ret;
-    AVFrame *pic = avctx->coded_frame;
+    AVFrame *pic = data;
     const uint32_t *src = (const uint32_t *)avpkt->data;
     int aligned_width = FFALIGN(avctx->width,
                                 avctx->codec_id == AV_CODEC_ID_R10K ? 1 : 64);
     uint8_t *dst_line;
 
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
-
     if (avpkt->size < 4 * aligned_width * avctx->height) {
         av_log(avctx, AV_LOG_ERROR, "packet too small\n");
         return AVERROR_INVALIDDATA;
     }
 
-    pic->reference = 0;
-    if ((ret = ff_get_buffer(avctx, pic)) < 0)
+    if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
         return ret;
 
     pic->pict_type = AV_PICTURE_TYPE_I;
@@ -91,28 +83,16 @@
     }
 
     *got_frame      = 1;
-    *(AVFrame*)data = *avctx->coded_frame;
 
     return avpkt->size;
 }
 
-static av_cold int decode_close(AVCodecContext *avctx)
-{
-    AVFrame *pic = avctx->coded_frame;
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
-    av_freep(&avctx->coded_frame);
-
-    return 0;
-}
-
 #if CONFIG_R210_DECODER
 AVCodec ff_r210_decoder = {
     .name           = "r210",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_R210,
     .init           = decode_init,
-    .close          = decode_close,
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("Uncompressed RGB 10-bit"),
@@ -124,7 +104,6 @@
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_R10K,
     .init           = decode_init,
-    .close          = decode_close,
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("AJA Kona 10-bit RGB Codec"),
@@ -136,7 +115,6 @@
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_AVRP,
     .init           = decode_init,
-    .close          = decode_close,
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name = NULL_IF_CONFIG_SMALL("Avid 1:1 10-bit RGB Packer"),
diff --git a/libavcodec/r210enc.c b/libavcodec/r210enc.c
index 39069ab..e19a27e 100644
--- a/libavcodec/r210enc.c
+++ b/libavcodec/r210enc.c
@@ -47,7 +47,6 @@
     if ((ret = ff_alloc_packet2(avctx, pkt, 4 * aligned_width * avctx->height)) < 0)
         return ret;
 
-    avctx->coded_frame->reference = 0;
     avctx->coded_frame->key_frame = 1;
     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
     src_line = pic->data[0];
diff --git a/libavcodec/ra144.c b/libavcodec/ra144.c
index 1ec1e10..58cf377 100644
--- a/libavcodec/ra144.c
+++ b/libavcodec/ra144.c
@@ -1694,12 +1694,12 @@
     return 0x20000000 / (ff_t_sqrt(sum) >> 8);
 }
 
-void ff_subblock_synthesis(RA144Context *ractx, const uint16_t *lpc_coefs,
+void ff_subblock_synthesis(RA144Context *ractx, const int16_t *lpc_coefs,
                            int cba_idx, int cb1_idx, int cb2_idx,
                            int gval, int gain)
 {
-    uint16_t buffer_a[BLOCKSIZE];
-    uint16_t *block;
+    int16_t buffer_a[BLOCKSIZE];
+    int16_t *block;
     int m[3];
 
     if (cba_idx) {
diff --git a/libavcodec/ra144.h b/libavcodec/ra144.h
index eb89398..426fea3 100644
--- a/libavcodec/ra144.h
+++ b/libavcodec/ra144.h
@@ -56,7 +56,7 @@
 
     /** Adaptive codebook, its size is two units bigger to avoid a
      *  buffer overflow. */
-    uint16_t adapt_cb[146+2];
+    int16_t adapt_cb[146+2];
 } RA144Context;
 
 void ff_copy_and_dup(int16_t *target, const int16_t *source, int offset);
@@ -69,7 +69,7 @@
               int energy);
 unsigned int ff_rescale_rms(unsigned int rms, unsigned int energy);
 int ff_irms(const int16_t *data);
-void ff_subblock_synthesis(RA144Context *ractx, const uint16_t *lpc_coefs,
+void ff_subblock_synthesis(RA144Context *ractx, const int16_t *lpc_coefs,
                            int cba_idx, int cb1_idx, int cb2_idx,
                            int gval, int gain);
 
diff --git a/libavcodec/ra144dec.c b/libavcodec/ra144dec.c
index f12954b..63f77e0 100644
--- a/libavcodec/ra144dec.c
+++ b/libavcodec/ra144dec.c
@@ -45,7 +45,7 @@
     return 0;
 }
 
-static void do_output_subblock(RA144Context *ractx, const uint16_t  *lpc_coefs,
+static void do_output_subblock(RA144Context *ractx, const int16_t  *lpc_coefs,
                                int gval, GetBitContext *gb)
 {
     int cba_idx = get_bits(gb, 7); // index of the adaptive CB, 0 if none
@@ -66,7 +66,7 @@
     int buf_size = avpkt->size;
     static const uint8_t sizes[LPC_ORDER] = {6, 5, 5, 4, 4, 3, 3, 3, 3, 2};
     unsigned int refl_rms[NBLOCKS];           // RMS of the reflection coefficients
-    uint16_t block_coefs[NBLOCKS][LPC_ORDER]; // LPC coefficients of each sub-block
+    int16_t block_coefs[NBLOCKS][LPC_ORDER];  // LPC coefficients of each sub-block
     unsigned int lpc_refl[LPC_ORDER];         // LPC reflection coefficients of the frame
     int i, j;
     int ret;
@@ -76,20 +76,19 @@
     RA144Context *ractx = avctx->priv_data;
     GetBitContext gb;
 
-    /* get output buffer */
-    frame->nb_samples = NBLOCKS * BLOCKSIZE;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
-        return ret;
-    }
-    samples = (int16_t *)frame->data[0];
-
-    if(buf_size < FRAMESIZE) {
+    if (buf_size < FRAMESIZE) {
         av_log(avctx, AV_LOG_ERROR,
                "Frame too small (%d bytes). Truncated file?\n", buf_size);
         *got_frame_ptr = 0;
-        return buf_size;
+        return AVERROR_INVALIDDATA;
     }
+
+    /* get output buffer */
+    frame->nb_samples = NBLOCKS * BLOCKSIZE;
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
+        return ret;
+    samples = (int16_t *)frame->data[0];
+
     init_get_bits(&gb, buf, FRAMESIZE * 8);
 
     for (i = 0; i < LPC_ORDER; i++)
diff --git a/libavcodec/ra144enc.c b/libavcodec/ra144enc.c
index bbd7b68..2eac343 100644
--- a/libavcodec/ra144enc.c
+++ b/libavcodec/ra144enc.c
@@ -40,9 +40,6 @@
     RA144Context *ractx = avctx->priv_data;
     ff_lpc_end(&ractx->lpc_ctx);
     ff_af_queue_close(&ractx->afq);
-#if FF_API_OLD_ENCODE_AUDIO
-    av_freep(&avctx->coded_frame);
-#endif
     return 0;
 }
 
@@ -71,14 +68,6 @@
 
     ff_af_queue_init(avctx, &ractx->afq);
 
-#if FF_API_OLD_ENCODE_AUDIO
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame) {
-        ret = AVERROR(ENOMEM);
-        goto error;
-    }
-#endif
-
     return 0;
 error:
     ra144_encode_close(avctx);
@@ -458,7 +447,7 @@
     if (ractx->last_frame)
         return 0;
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, FRAMESIZE)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, FRAMESIZE)) < 0)
         return ret;
 
     /**
diff --git a/libavcodec/ra288.c b/libavcodec/ra288.c
index dac8aa6..393ea0b 100644
--- a/libavcodec/ra288.c
+++ b/libavcodec/ra288.c
@@ -68,7 +68,7 @@
     avctx->sample_fmt     = AV_SAMPLE_FMT_FLT;
 
     if (avctx->block_align <= 0) {
-        av_log_ask_for_sample(avctx, "unsupported block align\n");
+        av_log(avctx, AV_LOG_ERROR, "unsupported block align\n");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -198,10 +198,8 @@
 
     /* get output buffer */
     frame->nb_samples = RA288_BLOCK_SIZE * RA288_BLOCKS_PER_FRAME;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     out = (float *)frame->data[0];
 
     init_get_bits(&gb, buf, avctx->block_align * 8);
diff --git a/libavcodec/ralf.c b/libavcodec/ralf.c
index b163a89..d3f82d6 100644
--- a/libavcodec/ralf.c
+++ b/libavcodec/ralf.c
@@ -136,7 +136,7 @@
 
     ctx->version = AV_RB16(avctx->extradata + 4);
     if (ctx->version != 0x103) {
-        av_log_ask_for_sample(avctx, "unknown version %X\n", ctx->version);
+        avpriv_request_sample(avctx, "Unknown version %X", ctx->version);
         return AVERROR_PATCHWELCOME;
     }
 
@@ -460,10 +460,8 @@
     }
 
     frame->nb_samples = ctx->max_frame_size;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "Me fail get_buffer()? That's unpossible!\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     samples0 = (int16_t *)frame->data[0];
     samples1 = (int16_t *)frame->data[1];
 
diff --git a/libavcodec/ratecontrol.h b/libavcodec/ratecontrol.h
index b4132bf..959b6a2 100644
--- a/libavcodec/ratecontrol.h
+++ b/libavcodec/ratecontrol.h
@@ -61,7 +61,6 @@
  * rate control context.
  */
 typedef struct RateControlContext{
-    FILE *stats_file;
     int num_entries;              ///< number of RateControlEntries
     RateControlEntry *entry;
     double buffer_index;          ///< amount of bits in the video/audio buffer
diff --git a/libavcodec/rawdec.c b/libavcodec/rawdec.c
index a8b70ee..f45ff4c 100644
--- a/libavcodec/rawdec.c
+++ b/libavcodec/rawdec.c
@@ -27,6 +27,7 @@
 #include "avcodec.h"
 #include "raw.h"
 #include "libavutil/avassert.h"
+#include "libavutil/buffer.h"
 #include "libavutil/common.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/imgutils.h"
@@ -34,11 +35,11 @@
 
 typedef struct RawVideoContext {
     AVClass *av_class;
-    uint32_t palette[AVPALETTE_COUNT];
-    unsigned char *buffer;  /* block of memory for holding one frame */
-    int            length;  /* number of bytes in buffer */
+    AVBufferRef *palette;
+    int frame_size;  /* size of the frame in bytes */
     int flip;
-    AVFrame pic;             ///< AVCodecContext.coded_frame
+    int is_2_4_bpp; // 2 or 4 bpp raw in avi/mov
+    int is_yuv2;
     int tff;
 } RawVideoContext;
 
@@ -101,6 +102,7 @@
 static av_cold int raw_init_decoder(AVCodecContext *avctx)
 {
     RawVideoContext *context = avctx->priv_data;
+    const AVPixFmtDescriptor *desc;
 
     if (   avctx->codec_tag == MKTAG('r','a','w',' ')
         || avctx->codec_tag == MKTAG('N','O','1','6'))
@@ -115,32 +117,28 @@
         avctx->pix_fmt = avpriv_find_pix_fmt(pix_fmt_bps_avi,
                                       avctx->bits_per_coded_sample);
 
-    if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
-        av_log(avctx, AV_LOG_ERROR, "Pixel format was not specified and cannot be detected\n");
+    desc = av_pix_fmt_desc_get(avctx->pix_fmt);
+    if (!desc) {
+        av_log(avctx, AV_LOG_ERROR, "Invalid pixel format.\n");
         return AVERROR(EINVAL);
     }
 
-    avpriv_set_systematic_pal2(context->palette, avctx->pix_fmt);
+    if (desc->flags & (PIX_FMT_PAL | PIX_FMT_PSEUDOPAL)) {
+        context->palette = av_buffer_alloc(AVPALETTE_SIZE);
+        if (!context->palette)
+            return AVERROR(ENOMEM);
+        if (desc->flags & PIX_FMT_PSEUDOPAL)
+            avpriv_set_systematic_pal2((uint32_t*)context->palette->data, avctx->pix_fmt);
+        else
+            memset(context->palette->data, 0, AVPALETTE_SIZE);
+    }
+
+    context->frame_size = avpicture_get_size(avctx->pix_fmt, avctx->width,
+                                             avctx->height);
     if ((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2) &&
         avctx->pix_fmt == AV_PIX_FMT_PAL8 &&
-       (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))) {
-        context->length = avpicture_get_size(avctx->pix_fmt,
-                                             FFALIGN(avctx->width, 16),
-                                             avctx->height);
-        if (context->length < 0)
-            return context->length;
-        context->buffer = av_malloc(context->length);
-        if (!context->buffer)
-            return AVERROR(ENOMEM);
-    } else {
-        context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
-        if (context->length < 0)
-            return context->length;
-    }
-    context->pic.pict_type = AV_PICTURE_TYPE_I;
-    context->pic.key_frame = 1;
-
-    avctx->coded_frame = &context->pic;
+       (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' ')))
+        context->is_2_4_bpp = 1;
 
     if ((avctx->extradata_size >= 9 &&
          !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) ||
@@ -149,12 +147,9 @@
         avctx->codec_tag == MKTAG('W','R','A','W'))
         context->flip = 1;
 
-    if (avctx->field_order > AV_FIELD_PROGRESSIVE) { /*we have interlaced material flagged in container */
-        avctx->coded_frame->interlaced_frame = 1;
-        if (avctx->field_order == AV_FIELD_TT  || avctx->field_order == AV_FIELD_TB)
-            avctx->coded_frame->top_field_first = 1;
-    }
-
+    if (avctx->codec_tag == AV_RL32("yuv2") &&
+        avctx->pix_fmt   == AV_PIX_FMT_YUYV422)
+        context->is_yuv2 = 1;
 
     return 0;
 }
@@ -174,17 +169,17 @@
     int buf_size                   = avpkt->size;
     int linesize_align             = 4;
     int res, len;
+    int need_copy                  = !avpkt->buf || context->is_2_4_bpp || context->is_yuv2;
 
     AVFrame   *frame   = data;
     AVPicture *picture = data;
 
-    frame->pict_type        = avctx->coded_frame->pict_type;
-    frame->interlaced_frame = avctx->coded_frame->interlaced_frame;
-    frame->top_field_first  = avctx->coded_frame->top_field_first;
+    frame->pict_type        = AV_PICTURE_TYPE_I;
+    frame->key_frame        = 1;
     frame->reordered_opaque = avctx->reordered_opaque;
     frame->pkt_pts          = avctx->pkt->pts;
-    frame->pkt_pos          = avctx->pkt->pos;
-    frame->pkt_duration     = avctx->pkt->duration;
+    av_frame_set_pkt_pos     (frame, avctx->pkt->pos);
+    av_frame_set_pkt_duration(frame, avctx->pkt->duration);
 
     if (context->tff >= 0) {
         frame->interlaced_frame = 1;
@@ -194,11 +189,18 @@
     if ((res = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
         return res;
 
+    if (need_copy)
+        frame->buf[0] = av_buffer_alloc(context->frame_size);
+    else
+        frame->buf[0] = av_buffer_ref(avpkt->buf);
+    if (!frame->buf[0])
+        return AVERROR(ENOMEM);
+
     //2bpp and 4bpp raw in avi and mov (yes this is ugly ...)
-    if (context->buffer) {
+    if (context->is_2_4_bpp) {
         int i;
-        uint8_t *dst = context->buffer;
-        buf_size = context->length - AVPALETTE_SIZE;
+        uint8_t *dst = frame->buf[0]->data;
+        buf_size = context->frame_size - AVPALETTE_SIZE;
         if (avctx->bits_per_coded_sample == 4) {
             for (i = 0; 2 * i + 1 < buf_size && i<avpkt->size; i++) {
                 dst[2 * i + 0] = buf[i] >> 4;
@@ -216,34 +218,44 @@
             linesize_align = 16;
         }
         buf = dst;
+    } else if (need_copy) {
+        memcpy(frame->buf[0]->data, buf, FFMIN(buf_size, context->frame_size));
+        buf = frame->buf[0]->data;
     }
 
     if (avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
         avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
-        buf += buf_size - context->length;
+        buf += buf_size - context->frame_size;
 
-    len = context->length - (avctx->pix_fmt==AV_PIX_FMT_PAL8 ? AVPALETTE_SIZE : 0);
+    len = context->frame_size - (avctx->pix_fmt==AV_PIX_FMT_PAL8 ? AVPALETTE_SIZE : 0);
     if (buf_size < len) {
-        av_log(avctx, AV_LOG_ERROR, "Invalid buffer size, packet size %d < expected length %d\n", buf_size, len);
+        av_log(avctx, AV_LOG_ERROR, "Invalid buffer size, packet size %d < expected frame_size %d\n", buf_size, len);
+        av_buffer_unref(&frame->buf[0]);
         return AVERROR(EINVAL);
     }
 
     if ((res = avpicture_fill(picture, buf, avctx->pix_fmt,
-                              avctx->width, avctx->height)) < 0)
+                              avctx->width, avctx->height)) < 0) {
+        av_buffer_unref(&frame->buf[0]);
         return res;
-    if ((avctx->pix_fmt == AV_PIX_FMT_PAL8 && buf_size < context->length) ||
-        (desc->flags & PIX_FMT_PSEUDOPAL)) {
-        frame->data[1] = (uint8_t*)context->palette;
     }
+
     if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE,
                                                      NULL);
 
         if (pal) {
-            memcpy(frame->data[1], pal, AVPALETTE_SIZE);
+            av_buffer_unref(&context->palette);
+            context->palette = av_buffer_alloc(AVPALETTE_SIZE);
+            if (!context->palette) {
+                av_buffer_unref(&frame->buf[0]);
+                return AVERROR(ENOMEM);
+            }
+            memcpy(context->palette->data, pal, AVPALETTE_SIZE);
             frame->palette_has_changed = 1;
         }
     }
+
     if ((avctx->pix_fmt==AV_PIX_FMT_BGR24    ||
         avctx->pix_fmt==AV_PIX_FMT_GRAY8    ||
         avctx->pix_fmt==AV_PIX_FMT_RGB555LE ||
@@ -263,6 +275,20 @@
         frame->linesize[1] = FFALIGN(frame->linesize[1], linesize_align);
     }
 
+    if ((avctx->pix_fmt == AV_PIX_FMT_PAL8 && buf_size < context->frame_size) ||
+        (desc->flags & PIX_FMT_PSEUDOPAL)) {
+        frame->buf[1]  = av_buffer_ref(context->palette);
+        if (!frame->buf[1]) {
+            av_buffer_unref(&frame->buf[0]);
+            return AVERROR(ENOMEM);
+        }
+        frame->data[1] = frame->buf[1]->data;
+    }
+
+    if (avctx->pix_fmt == AV_PIX_FMT_BGR24 &&
+        ((frame->linesize[0] + 3) & ~3) * avctx->height <= buf_size)
+        frame->linesize[0] = (frame->linesize[0] + 3) & ~3;
+
     if (context->flip)
         flip(avctx, picture);
 
@@ -298,6 +324,12 @@
         }
     }
 
+    if (avctx->field_order > AV_FIELD_PROGRESSIVE) { /* we have interlaced material flagged in container */
+        frame->interlaced_frame = 1;
+        if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
+            frame->top_field_first = 1;
+    }
+
     *got_frame = 1;
     return buf_size;
 }
@@ -306,7 +338,7 @@
 {
     RawVideoContext *context = avctx->priv_data;
 
-    av_freep(&context->buffer);
+    av_buffer_unref(&context->palette);
     return 0;
 }
 
diff --git a/libavcodec/realtextdec.c b/libavcodec/realtextdec.c
index 102e0d9..4578897 100644
--- a/libavcodec/realtextdec.c
+++ b/libavcodec/realtextdec.c
@@ -35,11 +35,11 @@
 
     while (*p) {
         if (*p != '<') {
-            if (!isspace(*p))
+            if (!av_isspace(*p))
                 av_bprint_chars(buf, *p, 1);
             else if (!prev_chr_is_space)
                 av_bprint_chars(buf, ' ', 1);
-            prev_chr_is_space = isspace(*p);
+            prev_chr_is_space = av_isspace(*p);
         } else {
             const char *end = strchr(p, '>');
             if (!end)
diff --git a/libavcodec/rl2.c b/libavcodec/rl2.c
index b908a83..c8853de 100644
--- a/libavcodec/rl2.c
+++ b/libavcodec/rl2.c
@@ -41,7 +41,6 @@
 
 typedef struct Rl2Context {
     AVCodecContext *avctx;
-    AVFrame frame;
 
     uint16_t video_base;  ///< initial drawing offset
     uint32_t clr_count;   ///< number of used colors (currently unused)
@@ -138,7 +137,6 @@
 
     s->avctx       = avctx;
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
-    avcodec_get_frame_defaults(&s->frame);
 
     /** parse extra data */
     if (!avctx->extradata || avctx->extradata_size < EXTRADATA1_SIZE) {
@@ -178,29 +176,22 @@
                             void *data, int *got_frame,
                             AVPacket *avpkt)
 {
+    AVFrame *frame     = data;
     const uint8_t *buf = avpkt->data;
     int ret, buf_size  = avpkt->size;
     Rl2Context *s = avctx->priv_data;
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
-
-    /** get buffer */
-    s->frame.reference = 0;
-    if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
-        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     /** run length decode */
-    rl2_rle_decode(s, buf, buf_size, s->frame.data[0], s->frame.linesize[0],
+    rl2_rle_decode(s, buf, buf_size, frame->data[0], frame->linesize[0],
                    s->video_base);
 
     /** make the palette available on the way out */
-    memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
+    memcpy(frame->data[1], s->palette, AVPALETTE_SIZE);
 
     *got_frame = 1;
-    *(AVFrame*)data = s->frame;
 
     /** report that the buffer was completely consumed */
     return buf_size;
@@ -216,9 +207,6 @@
 {
     Rl2Context *s = avctx->priv_data;
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
-
     av_free(s->back_frame);
 
     return 0;
diff --git a/libavcodec/rnd_avg.h b/libavcodec/rnd_avg.h
index bc663ef..50ab59a 100644
--- a/libavcodec/rnd_avg.h
+++ b/libavcodec/rnd_avg.h
@@ -22,8 +22,15 @@
 #ifndef AVCODEC_RND_AVG_H
 #define AVCODEC_RND_AVG_H
 
+#include <stddef.h>
 #include <stdint.h>
 
+#define CALL_2X_PIXELS(a, b, n)\
+static void a(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
+    b(block  , pixels  , line_size, h);\
+    b(block+n, pixels+n, line_size, h);\
+}
+
 #define         BYTE_VEC32(c)   ((c)*0x01010101UL)
 #define         BYTE_VEC64(c)   ((c)*0x0001000100010001UL)
 
diff --git a/libavcodec/roqaudioenc.c b/libavcodec/roqaudioenc.c
index ed5481f..b68e3f8 100644
--- a/libavcodec/roqaudioenc.c
+++ b/libavcodec/roqaudioenc.c
@@ -46,9 +46,6 @@
 {
     ROQDPCMContext *context = avctx->priv_data;
 
-#if FF_API_OLD_ENCODE_AUDIO
-    av_freep(&avctx->coded_frame);
-#endif
     av_freep(&context->frame_buffer);
 
     return 0;
@@ -81,14 +78,6 @@
 
     context->lastSample[0] = context->lastSample[1] = 0;
 
-#if FF_API_OLD_ENCODE_AUDIO
-    avctx->coded_frame= avcodec_alloc_frame();
-    if (!avctx->coded_frame) {
-        ret = AVERROR(ENOMEM);
-        goto error;
-    }
-#endif
-
     return 0;
 error:
     roq_dpcm_encode_close(avctx);
@@ -173,7 +162,7 @@
     else
         data_size = avctx->channels * avctx->frame_size;
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, ROQ_HEADER_SIZE + data_size)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, ROQ_HEADER_SIZE + data_size)) < 0)
         return ret;
     out = avpkt->data;
 
diff --git a/libavcodec/roqvideodec.c b/libavcodec/roqvideodec.c
index 6812f9a..85fdab7 100644
--- a/libavcodec/roqvideodec.c
+++ b/libavcodec/roqvideodec.c
@@ -28,6 +28,7 @@
 #include "libavutil/avassert.h"
 #include "avcodec.h"
 #include "bytestream.h"
+#include "internal.h"
 #include "roqvideo.h"
 
 static void roqvideo_decode_frame(RoqContext *ri)
@@ -179,10 +180,15 @@
 
     s->width = avctx->width;
     s->height = avctx->height;
-    avcodec_get_frame_defaults(&s->frames[0]);
-    avcodec_get_frame_defaults(&s->frames[1]);
-    s->last_frame    = &s->frames[0];
-    s->current_frame = &s->frames[1];
+
+    s->last_frame    = av_frame_alloc();
+    s->current_frame = av_frame_alloc();
+    if (!s->current_frame || !s->last_frame) {
+        av_frame_free(&s->current_frame);
+        av_frame_free(&s->last_frame);
+        return AVERROR(ENOMEM);
+    }
+
     avctx->pix_fmt = AV_PIX_FMT_YUV444P;
 
     return 0;
@@ -198,11 +204,8 @@
     int copy= !s->current_frame->data[0];
     int ret;
 
-    s->current_frame->reference = 3;
-    if ((ret = avctx->reget_buffer(avctx, s->current_frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, s->current_frame)) < 0)
         return ret;
-    }
 
     if(copy)
         av_picture_copy((AVPicture*)s->current_frame, (AVPicture*)s->last_frame,
@@ -211,8 +214,9 @@
     bytestream2_init(&s->gb, buf, buf_size);
     roqvideo_decode_frame(s);
 
+    if ((ret = av_frame_ref(data, s->current_frame)) < 0)
+        return ret;
     *got_frame      = 1;
-    *(AVFrame*)data = *s->current_frame;
 
     /* shuffle frames */
     FFSWAP(AVFrame *, s->current_frame, s->last_frame);
@@ -224,11 +228,8 @@
 {
     RoqContext *s = avctx->priv_data;
 
-    /* release the last frame */
-    if (s->last_frame->data[0])
-        avctx->release_buffer(avctx, s->last_frame);
-    if (s->current_frame->data[0])
-        avctx->release_buffer(avctx, s->current_frame);
+    av_frame_free(&s->current_frame);
+    av_frame_free(&s->last_frame);
 
     return 0;
 }
diff --git a/libavcodec/roqvideoenc.c b/libavcodec/roqvideoenc.c
index 02f1a45..a652c47 100644
--- a/libavcodec/roqvideoenc.c
+++ b/libavcodec/roqvideoenc.c
@@ -936,6 +936,22 @@
     enc->framesSinceKeyframe++;
 }
 
+static int roq_encode_end(AVCodecContext *avctx)
+{
+    RoqContext *enc = avctx->priv_data;
+
+    av_frame_free(&enc->current_frame);
+    av_frame_free(&enc->last_frame);
+
+    av_free(enc->tmpData);
+    av_free(enc->this_motion4);
+    av_free(enc->last_motion4);
+    av_free(enc->this_motion8);
+    av_free(enc->last_motion8);
+
+    return 0;
+}
+
 static int roq_encode_init(AVCodecContext *avctx)
 {
     RoqContext *enc = avctx->priv_data;
@@ -957,8 +973,12 @@
     enc->framesSinceKeyframe = 0;
     enc->first_frame = 1;
 
-    enc->last_frame    = &enc->frames[0];
-    enc->current_frame = &enc->frames[1];
+    enc->last_frame    = av_frame_alloc();
+    enc->current_frame = av_frame_alloc();
+    if (!enc->last_frame || !enc->current_frame) {
+        roq_encode_end(avctx);
+        return AVERROR(ENOMEM);
+    }
 
     enc->tmpData      = av_malloc(sizeof(RoqTempdata));
 
@@ -1031,11 +1051,9 @@
     if (enc->first_frame) {
         /* Alloc memory for the reconstruction data (we must know the stride
          for that) */
-        if (ff_get_buffer(avctx, enc->current_frame) ||
-            ff_get_buffer(avctx, enc->last_frame)) {
-            av_log(avctx, AV_LOG_ERROR, "  RoQ: get_buffer() failed\n");
-            return -1;
-        }
+        if ((ret = ff_get_buffer(avctx, enc->current_frame, 0)) < 0 ||
+            (ret = ff_get_buffer(avctx, enc->last_frame,    0)) < 0)
+            return ret;
 
         /* Before the first video frame, write a "video info" chunk */
         roq_write_video_info_chunk(enc);
@@ -1054,22 +1072,6 @@
     return 0;
 }
 
-static int roq_encode_end(AVCodecContext *avctx)
-{
-    RoqContext *enc = avctx->priv_data;
-
-    avctx->release_buffer(avctx, enc->last_frame);
-    avctx->release_buffer(avctx, enc->current_frame);
-
-    av_free(enc->tmpData);
-    av_free(enc->this_motion4);
-    av_free(enc->last_motion4);
-    av_free(enc->this_motion8);
-    av_free(enc->last_motion8);
-
-    return 0;
-}
-
 AVCodec ff_roq_encoder = {
     .name                 = "roqvideo",
     .type                 = AVMEDIA_TYPE_VIDEO,
diff --git a/libavcodec/rpza.c b/libavcodec/rpza.c
index a5da967..2aa0091 100644
--- a/libavcodec/rpza.c
+++ b/libavcodec/rpza.c
@@ -41,6 +41,7 @@
 #include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
 #include "avcodec.h"
+#include "internal.h"
 
 typedef struct RpzaContext {
 
@@ -239,7 +240,6 @@
     avctx->pix_fmt = AV_PIX_FMT_RGB555;
 
     avcodec_get_frame_defaults(&s->frame);
-    s->frame.data[0] = NULL;
 
     return 0;
 }
@@ -256,17 +256,15 @@
     s->buf = buf;
     s->size = buf_size;
 
-    s->frame.reference = 3;
-    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
-    if ((ret = avctx->reget_buffer(avctx, &s->frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0)
         return ret;
-    }
 
     rpza_decode_stream(s);
 
+    if ((ret = av_frame_ref(data, &s->frame)) < 0)
+        return ret;
+
     *got_frame      = 1;
-    *(AVFrame*)data = s->frame;
 
     /* always report that the buffer was completely consumed */
     return buf_size;
@@ -276,8 +274,7 @@
 {
     RpzaContext *s = avctx->priv_data;
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+    av_frame_unref(&s->frame);
 
     return 0;
 }
diff --git a/libavcodec/rv10.c b/libavcodec/rv10.c
index 1528780..8ee3391 100644
--- a/libavcodec/rv10.c
+++ b/libavcodec/rv10.c
@@ -27,6 +27,7 @@
 
 #include "libavutil/imgutils.h"
 #include "avcodec.h"
+#include "error_resilience.h"
 #include "mpegvideo.h"
 #include "mpeg4video.h"
 #include "h263.h"
@@ -44,148 +45,144 @@
     int sub_id;
 } RVDecContext;
 
-static const uint16_t rv_lum_code[256] =
-{
- 0x3e7f, 0x0f00, 0x0f01, 0x0f02, 0x0f03, 0x0f04, 0x0f05, 0x0f06,
- 0x0f07, 0x0f08, 0x0f09, 0x0f0a, 0x0f0b, 0x0f0c, 0x0f0d, 0x0f0e,
- 0x0f0f, 0x0f10, 0x0f11, 0x0f12, 0x0f13, 0x0f14, 0x0f15, 0x0f16,
- 0x0f17, 0x0f18, 0x0f19, 0x0f1a, 0x0f1b, 0x0f1c, 0x0f1d, 0x0f1e,
- 0x0f1f, 0x0f20, 0x0f21, 0x0f22, 0x0f23, 0x0f24, 0x0f25, 0x0f26,
- 0x0f27, 0x0f28, 0x0f29, 0x0f2a, 0x0f2b, 0x0f2c, 0x0f2d, 0x0f2e,
- 0x0f2f, 0x0f30, 0x0f31, 0x0f32, 0x0f33, 0x0f34, 0x0f35, 0x0f36,
- 0x0f37, 0x0f38, 0x0f39, 0x0f3a, 0x0f3b, 0x0f3c, 0x0f3d, 0x0f3e,
- 0x0f3f, 0x0380, 0x0381, 0x0382, 0x0383, 0x0384, 0x0385, 0x0386,
- 0x0387, 0x0388, 0x0389, 0x038a, 0x038b, 0x038c, 0x038d, 0x038e,
- 0x038f, 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396,
- 0x0397, 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e,
- 0x039f, 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6,
- 0x00c7, 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce,
- 0x00cf, 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056,
- 0x0057, 0x0020, 0x0021, 0x0022, 0x0023, 0x000c, 0x000d, 0x0004,
- 0x0000, 0x0005, 0x000e, 0x000f, 0x0024, 0x0025, 0x0026, 0x0027,
- 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
- 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7,
- 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df,
- 0x03a0, 0x03a1, 0x03a2, 0x03a3, 0x03a4, 0x03a5, 0x03a6, 0x03a7,
- 0x03a8, 0x03a9, 0x03aa, 0x03ab, 0x03ac, 0x03ad, 0x03ae, 0x03af,
- 0x03b0, 0x03b1, 0x03b2, 0x03b3, 0x03b4, 0x03b5, 0x03b6, 0x03b7,
- 0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03be, 0x03bf,
- 0x0f40, 0x0f41, 0x0f42, 0x0f43, 0x0f44, 0x0f45, 0x0f46, 0x0f47,
- 0x0f48, 0x0f49, 0x0f4a, 0x0f4b, 0x0f4c, 0x0f4d, 0x0f4e, 0x0f4f,
- 0x0f50, 0x0f51, 0x0f52, 0x0f53, 0x0f54, 0x0f55, 0x0f56, 0x0f57,
- 0x0f58, 0x0f59, 0x0f5a, 0x0f5b, 0x0f5c, 0x0f5d, 0x0f5e, 0x0f5f,
- 0x0f60, 0x0f61, 0x0f62, 0x0f63, 0x0f64, 0x0f65, 0x0f66, 0x0f67,
- 0x0f68, 0x0f69, 0x0f6a, 0x0f6b, 0x0f6c, 0x0f6d, 0x0f6e, 0x0f6f,
- 0x0f70, 0x0f71, 0x0f72, 0x0f73, 0x0f74, 0x0f75, 0x0f76, 0x0f77,
- 0x0f78, 0x0f79, 0x0f7a, 0x0f7b, 0x0f7c, 0x0f7d, 0x0f7e, 0x0f7f,
+static const uint16_t rv_lum_code[256] = {
+    0x3e7f, 0x0f00, 0x0f01, 0x0f02, 0x0f03, 0x0f04, 0x0f05, 0x0f06,
+    0x0f07, 0x0f08, 0x0f09, 0x0f0a, 0x0f0b, 0x0f0c, 0x0f0d, 0x0f0e,
+    0x0f0f, 0x0f10, 0x0f11, 0x0f12, 0x0f13, 0x0f14, 0x0f15, 0x0f16,
+    0x0f17, 0x0f18, 0x0f19, 0x0f1a, 0x0f1b, 0x0f1c, 0x0f1d, 0x0f1e,
+    0x0f1f, 0x0f20, 0x0f21, 0x0f22, 0x0f23, 0x0f24, 0x0f25, 0x0f26,
+    0x0f27, 0x0f28, 0x0f29, 0x0f2a, 0x0f2b, 0x0f2c, 0x0f2d, 0x0f2e,
+    0x0f2f, 0x0f30, 0x0f31, 0x0f32, 0x0f33, 0x0f34, 0x0f35, 0x0f36,
+    0x0f37, 0x0f38, 0x0f39, 0x0f3a, 0x0f3b, 0x0f3c, 0x0f3d, 0x0f3e,
+    0x0f3f, 0x0380, 0x0381, 0x0382, 0x0383, 0x0384, 0x0385, 0x0386,
+    0x0387, 0x0388, 0x0389, 0x038a, 0x038b, 0x038c, 0x038d, 0x038e,
+    0x038f, 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396,
+    0x0397, 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e,
+    0x039f, 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6,
+    0x00c7, 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce,
+    0x00cf, 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056,
+    0x0057, 0x0020, 0x0021, 0x0022, 0x0023, 0x000c, 0x000d, 0x0004,
+    0x0000, 0x0005, 0x000e, 0x000f, 0x0024, 0x0025, 0x0026, 0x0027,
+    0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
+    0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7,
+    0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df,
+    0x03a0, 0x03a1, 0x03a2, 0x03a3, 0x03a4, 0x03a5, 0x03a6, 0x03a7,
+    0x03a8, 0x03a9, 0x03aa, 0x03ab, 0x03ac, 0x03ad, 0x03ae, 0x03af,
+    0x03b0, 0x03b1, 0x03b2, 0x03b3, 0x03b4, 0x03b5, 0x03b6, 0x03b7,
+    0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03be, 0x03bf,
+    0x0f40, 0x0f41, 0x0f42, 0x0f43, 0x0f44, 0x0f45, 0x0f46, 0x0f47,
+    0x0f48, 0x0f49, 0x0f4a, 0x0f4b, 0x0f4c, 0x0f4d, 0x0f4e, 0x0f4f,
+    0x0f50, 0x0f51, 0x0f52, 0x0f53, 0x0f54, 0x0f55, 0x0f56, 0x0f57,
+    0x0f58, 0x0f59, 0x0f5a, 0x0f5b, 0x0f5c, 0x0f5d, 0x0f5e, 0x0f5f,
+    0x0f60, 0x0f61, 0x0f62, 0x0f63, 0x0f64, 0x0f65, 0x0f66, 0x0f67,
+    0x0f68, 0x0f69, 0x0f6a, 0x0f6b, 0x0f6c, 0x0f6d, 0x0f6e, 0x0f6f,
+    0x0f70, 0x0f71, 0x0f72, 0x0f73, 0x0f74, 0x0f75, 0x0f76, 0x0f77,
+    0x0f78, 0x0f79, 0x0f7a, 0x0f7b, 0x0f7c, 0x0f7d, 0x0f7e, 0x0f7f,
 };
 
-static const uint8_t rv_lum_bits[256] =
-{
- 14, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 10, 10, 10, 10, 10, 10, 10,
- 10, 10, 10, 10, 10, 10, 10, 10,
- 10, 10, 10, 10, 10, 10, 10, 10,
- 10, 10, 10, 10, 10, 10, 10, 10,
- 10,  8,  8,  8,  8,  8,  8,  8,
-  8,  8,  8,  8,  8,  8,  8,  8,
-  8,  7,  7,  7,  7,  7,  7,  7,
-  7,  6,  6,  6,  6,  5,  5,  4,
-  2,  4,  5,  5,  6,  6,  6,  6,
-  7,  7,  7,  7,  7,  7,  7,  7,
-  8,  8,  8,  8,  8,  8,  8,  8,
-  8,  8,  8,  8,  8,  8,  8,  8,
- 10, 10, 10, 10, 10, 10, 10, 10,
- 10, 10, 10, 10, 10, 10, 10, 10,
- 10, 10, 10, 10, 10, 10, 10, 10,
- 10, 10, 10, 10, 10, 10, 10, 10,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12,
+static const uint8_t rv_lum_bits[256] = {
+    14, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    12, 10, 10, 10, 10, 10, 10, 10,
+    10, 10, 10, 10, 10, 10, 10, 10,
+    10, 10, 10, 10, 10, 10, 10, 10,
+    10, 10, 10, 10, 10, 10, 10, 10,
+    10,  8,  8,  8,  8,  8,  8,  8,
+     8,  8,  8,  8,  8,  8,  8,  8,
+     8,  7,  7,  7,  7,  7,  7,  7,
+     7,  6,  6,  6,  6,  5,  5,  4,
+     2,  4,  5,  5,  6,  6,  6,  6,
+     7,  7,  7,  7,  7,  7,  7,  7,
+     8,  8,  8,  8,  8,  8,  8,  8,
+     8,  8,  8,  8,  8,  8,  8,  8,
+    10, 10, 10, 10, 10, 10, 10, 10,
+    10, 10, 10, 10, 10, 10, 10, 10,
+    10, 10, 10, 10, 10, 10, 10, 10,
+    10, 10, 10, 10, 10, 10, 10, 10,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 12, 12, 12, 12, 12, 12,
 };
 
-static const uint16_t rv_chrom_code[256] =
-{
- 0xfe7f, 0x3f00, 0x3f01, 0x3f02, 0x3f03, 0x3f04, 0x3f05, 0x3f06,
- 0x3f07, 0x3f08, 0x3f09, 0x3f0a, 0x3f0b, 0x3f0c, 0x3f0d, 0x3f0e,
- 0x3f0f, 0x3f10, 0x3f11, 0x3f12, 0x3f13, 0x3f14, 0x3f15, 0x3f16,
- 0x3f17, 0x3f18, 0x3f19, 0x3f1a, 0x3f1b, 0x3f1c, 0x3f1d, 0x3f1e,
- 0x3f1f, 0x3f20, 0x3f21, 0x3f22, 0x3f23, 0x3f24, 0x3f25, 0x3f26,
- 0x3f27, 0x3f28, 0x3f29, 0x3f2a, 0x3f2b, 0x3f2c, 0x3f2d, 0x3f2e,
- 0x3f2f, 0x3f30, 0x3f31, 0x3f32, 0x3f33, 0x3f34, 0x3f35, 0x3f36,
- 0x3f37, 0x3f38, 0x3f39, 0x3f3a, 0x3f3b, 0x3f3c, 0x3f3d, 0x3f3e,
- 0x3f3f, 0x0f80, 0x0f81, 0x0f82, 0x0f83, 0x0f84, 0x0f85, 0x0f86,
- 0x0f87, 0x0f88, 0x0f89, 0x0f8a, 0x0f8b, 0x0f8c, 0x0f8d, 0x0f8e,
- 0x0f8f, 0x0f90, 0x0f91, 0x0f92, 0x0f93, 0x0f94, 0x0f95, 0x0f96,
- 0x0f97, 0x0f98, 0x0f99, 0x0f9a, 0x0f9b, 0x0f9c, 0x0f9d, 0x0f9e,
- 0x0f9f, 0x03c0, 0x03c1, 0x03c2, 0x03c3, 0x03c4, 0x03c5, 0x03c6,
- 0x03c7, 0x03c8, 0x03c9, 0x03ca, 0x03cb, 0x03cc, 0x03cd, 0x03ce,
- 0x03cf, 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6,
- 0x00e7, 0x0030, 0x0031, 0x0032, 0x0033, 0x0008, 0x0009, 0x0002,
- 0x0000, 0x0003, 0x000a, 0x000b, 0x0034, 0x0035, 0x0036, 0x0037,
- 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef,
- 0x03d0, 0x03d1, 0x03d2, 0x03d3, 0x03d4, 0x03d5, 0x03d6, 0x03d7,
- 0x03d8, 0x03d9, 0x03da, 0x03db, 0x03dc, 0x03dd, 0x03de, 0x03df,
- 0x0fa0, 0x0fa1, 0x0fa2, 0x0fa3, 0x0fa4, 0x0fa5, 0x0fa6, 0x0fa7,
- 0x0fa8, 0x0fa9, 0x0faa, 0x0fab, 0x0fac, 0x0fad, 0x0fae, 0x0faf,
- 0x0fb0, 0x0fb1, 0x0fb2, 0x0fb3, 0x0fb4, 0x0fb5, 0x0fb6, 0x0fb7,
- 0x0fb8, 0x0fb9, 0x0fba, 0x0fbb, 0x0fbc, 0x0fbd, 0x0fbe, 0x0fbf,
- 0x3f40, 0x3f41, 0x3f42, 0x3f43, 0x3f44, 0x3f45, 0x3f46, 0x3f47,
- 0x3f48, 0x3f49, 0x3f4a, 0x3f4b, 0x3f4c, 0x3f4d, 0x3f4e, 0x3f4f,
- 0x3f50, 0x3f51, 0x3f52, 0x3f53, 0x3f54, 0x3f55, 0x3f56, 0x3f57,
- 0x3f58, 0x3f59, 0x3f5a, 0x3f5b, 0x3f5c, 0x3f5d, 0x3f5e, 0x3f5f,
- 0x3f60, 0x3f61, 0x3f62, 0x3f63, 0x3f64, 0x3f65, 0x3f66, 0x3f67,
- 0x3f68, 0x3f69, 0x3f6a, 0x3f6b, 0x3f6c, 0x3f6d, 0x3f6e, 0x3f6f,
- 0x3f70, 0x3f71, 0x3f72, 0x3f73, 0x3f74, 0x3f75, 0x3f76, 0x3f77,
- 0x3f78, 0x3f79, 0x3f7a, 0x3f7b, 0x3f7c, 0x3f7d, 0x3f7e, 0x3f7f,
+static const uint16_t rv_chrom_code[256] = {
+    0xfe7f, 0x3f00, 0x3f01, 0x3f02, 0x3f03, 0x3f04, 0x3f05, 0x3f06,
+    0x3f07, 0x3f08, 0x3f09, 0x3f0a, 0x3f0b, 0x3f0c, 0x3f0d, 0x3f0e,
+    0x3f0f, 0x3f10, 0x3f11, 0x3f12, 0x3f13, 0x3f14, 0x3f15, 0x3f16,
+    0x3f17, 0x3f18, 0x3f19, 0x3f1a, 0x3f1b, 0x3f1c, 0x3f1d, 0x3f1e,
+    0x3f1f, 0x3f20, 0x3f21, 0x3f22, 0x3f23, 0x3f24, 0x3f25, 0x3f26,
+    0x3f27, 0x3f28, 0x3f29, 0x3f2a, 0x3f2b, 0x3f2c, 0x3f2d, 0x3f2e,
+    0x3f2f, 0x3f30, 0x3f31, 0x3f32, 0x3f33, 0x3f34, 0x3f35, 0x3f36,
+    0x3f37, 0x3f38, 0x3f39, 0x3f3a, 0x3f3b, 0x3f3c, 0x3f3d, 0x3f3e,
+    0x3f3f, 0x0f80, 0x0f81, 0x0f82, 0x0f83, 0x0f84, 0x0f85, 0x0f86,
+    0x0f87, 0x0f88, 0x0f89, 0x0f8a, 0x0f8b, 0x0f8c, 0x0f8d, 0x0f8e,
+    0x0f8f, 0x0f90, 0x0f91, 0x0f92, 0x0f93, 0x0f94, 0x0f95, 0x0f96,
+    0x0f97, 0x0f98, 0x0f99, 0x0f9a, 0x0f9b, 0x0f9c, 0x0f9d, 0x0f9e,
+    0x0f9f, 0x03c0, 0x03c1, 0x03c2, 0x03c3, 0x03c4, 0x03c5, 0x03c6,
+    0x03c7, 0x03c8, 0x03c9, 0x03ca, 0x03cb, 0x03cc, 0x03cd, 0x03ce,
+    0x03cf, 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6,
+    0x00e7, 0x0030, 0x0031, 0x0032, 0x0033, 0x0008, 0x0009, 0x0002,
+    0x0000, 0x0003, 0x000a, 0x000b, 0x0034, 0x0035, 0x0036, 0x0037,
+    0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef,
+    0x03d0, 0x03d1, 0x03d2, 0x03d3, 0x03d4, 0x03d5, 0x03d6, 0x03d7,
+    0x03d8, 0x03d9, 0x03da, 0x03db, 0x03dc, 0x03dd, 0x03de, 0x03df,
+    0x0fa0, 0x0fa1, 0x0fa2, 0x0fa3, 0x0fa4, 0x0fa5, 0x0fa6, 0x0fa7,
+    0x0fa8, 0x0fa9, 0x0faa, 0x0fab, 0x0fac, 0x0fad, 0x0fae, 0x0faf,
+    0x0fb0, 0x0fb1, 0x0fb2, 0x0fb3, 0x0fb4, 0x0fb5, 0x0fb6, 0x0fb7,
+    0x0fb8, 0x0fb9, 0x0fba, 0x0fbb, 0x0fbc, 0x0fbd, 0x0fbe, 0x0fbf,
+    0x3f40, 0x3f41, 0x3f42, 0x3f43, 0x3f44, 0x3f45, 0x3f46, 0x3f47,
+    0x3f48, 0x3f49, 0x3f4a, 0x3f4b, 0x3f4c, 0x3f4d, 0x3f4e, 0x3f4f,
+    0x3f50, 0x3f51, 0x3f52, 0x3f53, 0x3f54, 0x3f55, 0x3f56, 0x3f57,
+    0x3f58, 0x3f59, 0x3f5a, 0x3f5b, 0x3f5c, 0x3f5d, 0x3f5e, 0x3f5f,
+    0x3f60, 0x3f61, 0x3f62, 0x3f63, 0x3f64, 0x3f65, 0x3f66, 0x3f67,
+    0x3f68, 0x3f69, 0x3f6a, 0x3f6b, 0x3f6c, 0x3f6d, 0x3f6e, 0x3f6f,
+    0x3f70, 0x3f71, 0x3f72, 0x3f73, 0x3f74, 0x3f75, 0x3f76, 0x3f77,
+    0x3f78, 0x3f79, 0x3f7a, 0x3f7b, 0x3f7c, 0x3f7d, 0x3f7e, 0x3f7f,
 };
 
-static const uint8_t rv_chrom_bits[256] =
-{
- 16, 14, 14, 14, 14, 14, 14, 14,
- 14, 14, 14, 14, 14, 14, 14, 14,
- 14, 14, 14, 14, 14, 14, 14, 14,
- 14, 14, 14, 14, 14, 14, 14, 14,
- 14, 14, 14, 14, 14, 14, 14, 14,
- 14, 14, 14, 14, 14, 14, 14, 14,
- 14, 14, 14, 14, 14, 14, 14, 14,
- 14, 14, 14, 14, 14, 14, 14, 14,
- 14, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 10, 10, 10, 10, 10, 10, 10,
- 10, 10, 10, 10, 10, 10, 10, 10,
- 10,  8,  8,  8,  8,  8,  8,  8,
-  8,  6,  6,  6,  6,  4,  4,  3,
-  2,  3,  4,  4,  6,  6,  6,  6,
-  8,  8,  8,  8,  8,  8,  8,  8,
- 10, 10, 10, 10, 10, 10, 10, 10,
- 10, 10, 10, 10, 10, 10, 10, 10,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 12, 12, 12, 12, 12, 12, 12, 12,
- 14, 14, 14, 14, 14, 14, 14, 14,
- 14, 14, 14, 14, 14, 14, 14, 14,
- 14, 14, 14, 14, 14, 14, 14, 14,
- 14, 14, 14, 14, 14, 14, 14, 14,
- 14, 14, 14, 14, 14, 14, 14, 14,
- 14, 14, 14, 14, 14, 14, 14, 14,
- 14, 14, 14, 14, 14, 14, 14, 14,
- 14, 14, 14, 14, 14, 14, 14, 14,
+static const uint8_t rv_chrom_bits[256] = {
+    16, 14, 14, 14, 14, 14, 14, 14,
+    14, 14, 14, 14, 14, 14, 14, 14,
+    14, 14, 14, 14, 14, 14, 14, 14,
+    14, 14, 14, 14, 14, 14, 14, 14,
+    14, 14, 14, 14, 14, 14, 14, 14,
+    14, 14, 14, 14, 14, 14, 14, 14,
+    14, 14, 14, 14, 14, 14, 14, 14,
+    14, 14, 14, 14, 14, 14, 14, 14,
+    14, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    12, 10, 10, 10, 10, 10, 10, 10,
+    10, 10, 10, 10, 10, 10, 10, 10,
+    10,  8,  8,  8,  8,  8,  8,  8,
+     8,  6,  6,  6,  6,  4,  4,  3,
+     2,  3,  4,  4,  6,  6,  6,  6,
+     8,  8,  8,  8,  8,  8,  8,  8,
+    10, 10, 10, 10, 10, 10, 10, 10,
+    10, 10, 10, 10, 10, 10, 10, 10,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    12, 12, 12, 12, 12, 12, 12, 12,
+    14, 14, 14, 14, 14, 14, 14, 14,
+    14, 14, 14, 14, 14, 14, 14, 14,
+    14, 14, 14, 14, 14, 14, 14, 14,
+    14, 14, 14, 14, 14, 14, 14, 14,
+    14, 14, 14, 14, 14, 14, 14, 14,
+    14, 14, 14, 14, 14, 14, 14, 14,
+    14, 14, 14, 14, 14, 14, 14, 14,
+    14, 14, 14, 14, 14, 14, 14, 14,
 };
 
 static VLC rv_dc_lum, rv_dc_chrom;
@@ -251,20 +248,23 @@
         s->pict_type = AV_PICTURE_TYPE_P;
     else
         s->pict_type = AV_PICTURE_TYPE_I;
-    if(!marker) av_log(s->avctx, AV_LOG_ERROR, "marker missing\n");
+
+    if (!marker)
+        av_log(s->avctx, AV_LOG_ERROR, "marker missing\n");
+
     pb_frame = get_bits1(&s->gb);
 
     av_dlog(s->avctx, "pict_type=%d pb_frame=%d\n", s->pict_type, pb_frame);
 
-    if (pb_frame){
-        av_log(s->avctx, AV_LOG_ERROR, "pb frame not supported\n");
-        return -1;
+    if (pb_frame) {
+        avpriv_request_sample(s->avctx, "pb frame");
+        return AVERROR_PATCHWELCOME;
     }
 
     s->qscale = get_bits(&s->gb, 5);
-    if(s->qscale==0){
-        av_log(s->avctx, AV_LOG_ERROR, "error, qscale:0\n");
-        return -1;
+    if (s->qscale == 0) {
+        av_log(s->avctx, AV_LOG_ERROR, "Invalid qscale value: 0\n");
+        return AVERROR_INVALIDDATA;
     }
 
     if (s->pict_type == AV_PICTURE_TYPE_I) {
@@ -280,14 +280,14 @@
     /* if multiple packets per frame are sent, the position at which
        to display the macroblocks is coded here */
 
-    mb_xy= s->mb_x + s->mb_y*s->mb_width;
-    if(show_bits(&s->gb, 12)==0 || (mb_xy && mb_xy < s->mb_num)){
-        s->mb_x = get_bits(&s->gb, 6); /* mb_x */
-        s->mb_y = get_bits(&s->gb, 6); /* mb_y */
+    mb_xy = s->mb_x + s->mb_y * s->mb_width;
+    if (show_bits(&s->gb, 12) == 0 || (mb_xy && mb_xy < s->mb_num)) {
+        s->mb_x  = get_bits(&s->gb, 6); /* mb_x */
+        s->mb_y  = get_bits(&s->gb, 6); /* mb_y */
         mb_count = get_bits(&s->gb, 12);
     } else {
-        s->mb_x = 0;
-        s->mb_y = 0;
+        s->mb_x  = 0;
+        s->mb_y  = 0;
         mb_count = s->mb_width * s->mb_height;
     }
     skip_bits(&s->gb, 3);   /* ignored */
@@ -300,84 +300,73 @@
 static int rv20_decode_picture_header(RVDecContext *rv)
 {
     MpegEncContext *s = &rv->m;
-    int seq, mb_pos, i;
+    int seq, mb_pos, i, ret;
     int rpr_bits;
 
-#if 0
-    GetBitContext gb= s->gb;
-    for(i=0; i<64; i++){
-        av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&gb));
-        if(i%4==3) av_log(s->avctx, AV_LOG_DEBUG, " ");
-    }
-    av_log(s->avctx, AV_LOG_DEBUG, "\n");
-#endif
-#if 0
-    av_log(s->avctx, AV_LOG_DEBUG, "%3dx%03d/%02Xx%02X ", s->width, s->height, s->width/4, s->height/4);
-    for(i=0; i<s->avctx->extradata_size; i++){
-        av_log(s->avctx, AV_LOG_DEBUG, "%02X ", ((uint8_t*)s->avctx->extradata)[i]);
-        if(i%4==3) av_log(s->avctx, AV_LOG_DEBUG, " ");
-    }
-    av_log(s->avctx, AV_LOG_DEBUG, "\n");
-#endif
-
-    i= get_bits(&s->gb, 2);
-    switch(i){
-    case 0: s->pict_type= AV_PICTURE_TYPE_I; break;
-    case 1: s->pict_type= AV_PICTURE_TYPE_I; break; //hmm ...
-    case 2: s->pict_type= AV_PICTURE_TYPE_P; break;
-    case 3: s->pict_type= AV_PICTURE_TYPE_B; break;
+    i = get_bits(&s->gb, 2);
+    switch(i) {
+    case 0: s->pict_type = AV_PICTURE_TYPE_I; break;
+    case 1: s->pict_type = AV_PICTURE_TYPE_I; break; //hmm ...
+    case 2: s->pict_type = AV_PICTURE_TYPE_P; break;
+    case 3: s->pict_type = AV_PICTURE_TYPE_B; break;
     default:
         av_log(s->avctx, AV_LOG_ERROR, "unknown frame type\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
-    if(s->low_delay && s->pict_type==AV_PICTURE_TYPE_B){
+    if (s->low_delay && s->pict_type == AV_PICTURE_TYPE_B) {
         av_log(s->avctx, AV_LOG_ERROR, "low delay B\n");
         return -1;
     }
-    if(s->last_picture_ptr==NULL && s->pict_type==AV_PICTURE_TYPE_B){
-        av_log(s->avctx, AV_LOG_ERROR, "early B pix\n");
-        return -1;
+    if (s->last_picture_ptr == NULL && s->pict_type == AV_PICTURE_TYPE_B) {
+        av_log(s->avctx, AV_LOG_ERROR, "early B-frame\n");
+        return AVERROR_INVALIDDATA;
     }
 
-    if (get_bits1(&s->gb)){
+    if (get_bits1(&s->gb)) {
         av_log(s->avctx, AV_LOG_ERROR, "reserved bit set\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
     s->qscale = get_bits(&s->gb, 5);
-    if(s->qscale==0){
-        av_log(s->avctx, AV_LOG_ERROR, "error, qscale:0\n");
-        return -1;
+    if (s->qscale == 0) {
+        av_log(s->avctx, AV_LOG_ERROR, "Invalid qscale value: 0\n");
+        return AVERROR_INVALIDDATA;
     }
 
-    if(RV_GET_MINOR_VER(rv->sub_id) >= 2)
+    if (RV_GET_MINOR_VER(rv->sub_id) >= 2)
         s->loop_filter = get_bits1(&s->gb) && !s->avctx->lowres;
 
-    if(RV_GET_MINOR_VER(rv->sub_id) <= 1)
+    if (RV_GET_MINOR_VER(rv->sub_id) <= 1)
         seq = get_bits(&s->gb, 8) << 7;
     else
         seq = get_bits(&s->gb, 13) << 2;
 
     rpr_bits = s->avctx->extradata[1] & 7;
-    if(rpr_bits){
+    if (rpr_bits) {
         int f, new_w, new_h;
         rpr_bits = FFMIN((rpr_bits >> 1) + 1, 3);
 
         f = get_bits(&s->gb, rpr_bits);
 
-        if(f){
-            new_w= 4*((uint8_t*)s->avctx->extradata)[6+2*f];
-            new_h= 4*((uint8_t*)s->avctx->extradata)[7+2*f];
-        }else{
-            new_w= s->orig_width ;
-            new_h= s->orig_height;
+        if (f) {
+            if (s->avctx->extradata_size < 8 + 2 * f) {
+                av_log(s->avctx, AV_LOG_ERROR, "Extradata too small.\n");
+                return AVERROR_INVALIDDATA;
+            }
+
+            new_w = 4 * ((uint8_t*)s->avctx->extradata)[6 + 2 * f];
+            new_h = 4 * ((uint8_t*)s->avctx->extradata)[7 + 2 * f];
+        } else {
+            new_w = s->orig_width ;
+            new_h = s->orig_height;
         }
-        if(new_w != s->width || new_h != s->height){
+        if (new_w != s->width || new_h != s->height) {
             AVRational old_aspect = s->avctx->sample_aspect_ratio;
-            av_log(s->avctx, AV_LOG_DEBUG, "attempting to change resolution to %dx%d\n", new_w, new_h);
+            av_log(s->avctx, AV_LOG_DEBUG,
+                   "attempting to change resolution to %dx%d\n", new_w, new_h);
             if (av_image_check_size(new_w, new_h, 0, s->avctx) < 0)
-                return -1;
+                return AVERROR_INVALIDDATA;
             ff_MPV_common_end(s);
 
             // attempt to keep aspect during typical resolution switches
@@ -390,11 +379,11 @@
             avcodec_set_dimensions(s->avctx, new_w, new_h);
             s->width  = new_w;
             s->height = new_h;
-            if (ff_MPV_common_init(s) < 0)
-                return -1;
+            if ((ret = ff_MPV_common_init(s)) < 0)
+                return ret;
         }
 
-        if(s->avctx->debug & FF_DEBUG_PICT_INFO){
+        if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
             av_log(s->avctx, AV_LOG_DEBUG, "F %d/%d\n", f, rpr_bits);
         }
     }
@@ -403,45 +392,47 @@
 
     mb_pos = ff_h263_decode_mba(s);
 
-    seq |= s->time &~0x7FFF;
-    if(seq - s->time >  0x4000) seq -= 0x8000;
-    if(seq - s->time < -0x4000) seq += 0x8000;
-    if(seq != s->time){
-        if(s->pict_type!=AV_PICTURE_TYPE_B){
-            s->time= seq;
-            s->pp_time= s->time - s->last_non_b_time;
-            s->last_non_b_time= s->time;
-        }else{
-            s->time= seq;
-            s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
+    seq |= s->time & ~0x7FFF;
+    if (seq - s->time >  0x4000)
+        seq -= 0x8000;
+    if (seq - s->time < -0x4000)
+        seq += 0x8000;
+
+    if (seq != s->time) {
+        if (s->pict_type != AV_PICTURE_TYPE_B) {
+            s->time            = seq;
+            s->pp_time         = s->time - s->last_non_b_time;
+            s->last_non_b_time = s->time;
+        } else {
+            s->time    = seq;
+            s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
         }
     }
-    if (s->pict_type==AV_PICTURE_TYPE_B) {
-        if(s->pp_time <=s->pb_time || s->pp_time <= s->pp_time - s->pb_time || s->pp_time<=0){
-            av_log(s->avctx, AV_LOG_DEBUG, "messed up order, possible from seeking? skipping current b frame\n");
-            return FRAME_SKIPPED;
+    if (s->pict_type == AV_PICTURE_TYPE_B) {
+        if (s->pp_time <=s->pb_time || s->pp_time <= s->pp_time - s->pb_time || s->pp_time<=0) {
+            av_log(s->avctx, AV_LOG_DEBUG,
+                   "messed up order, possible from seeking? skipping current b frame\n");
+#define ERROR_SKIP_FRAME -123
+            return ERROR_SKIP_FRAME;
         }
         ff_mpeg4_init_direct_mv(s);
     }
 
-    s->no_rounding= get_bits1(&s->gb);
+    s->no_rounding = get_bits1(&s->gb);
 
-    if(RV_GET_MINOR_VER(rv->sub_id) <= 1 && s->pict_type == AV_PICTURE_TYPE_B)
+    if (RV_GET_MINOR_VER(rv->sub_id) <= 1 && s->pict_type == AV_PICTURE_TYPE_B)
         skip_bits(&s->gb, 5); // binary decoder reads 3+2 bits here but they don't seem to be used
 
-    s->f_code = 1;
+    s->f_code          = 1;
     s->unrestricted_mv = 1;
-    s->h263_aic= s->pict_type == AV_PICTURE_TYPE_I;
-//    s->alt_inter_vlc=1;
-//    s->obmc=1;
-//    s->umvplus=1;
-    s->modified_quant=1;
-    if(!s->avctx->lowres)
-        s->loop_filter=1;
+    s->h263_aic        = s->pict_type == AV_PICTURE_TYPE_I;
+    s->modified_quant  = 1;
+    if (!s->avctx->lowres)
+        s->loop_filter = 1;
 
-    if(s->avctx->debug & FF_DEBUG_PICT_INFO){
-            av_log(s->avctx, AV_LOG_INFO, "num:%5d x:%2d y:%2d type:%d qscale:%2d rnd:%d\n",
-                   seq, s->mb_x, s->mb_y, s->pict_type, s->qscale, s->no_rounding);
+    if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
+        av_log(s->avctx, AV_LOG_INFO, "num:%5d x:%2d y:%2d type:%d qscale:%2d rnd:%d\n",
+               seq, s->mb_x, s->mb_y, s->pict_type, s->qscale, s->no_rounding);
     }
 
     av_assert0(s->pict_type != AV_PICTURE_TYPE_B || !s->low_delay);
@@ -453,25 +444,25 @@
 {
     RVDecContext  *rv = avctx->priv_data;
     MpegEncContext *s = &rv->m;
-    static int done=0;
-    int major_ver, minor_ver, micro_ver;
+    static int done = 0;
+    int major_ver, minor_ver, micro_ver, ret;
 
     if (avctx->extradata_size < 8) {
         av_log(avctx, AV_LOG_ERROR, "Extradata is too small.\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
     ff_MPV_decode_defaults(s);
 
-    s->avctx= avctx;
+    s->avctx      = avctx;
     s->out_format = FMT_H263;
-    s->codec_id= avctx->codec_id;
+    s->codec_id   = avctx->codec_id;
 
-    s->orig_width = s->width  = avctx->coded_width;
-    s->orig_height= s->height = avctx->coded_height;
+    s->orig_width  = s->width  = avctx->coded_width;
+    s->orig_height = s->height = avctx->coded_height;
 
-    s->h263_long_vectors= ((uint8_t*)avctx->extradata)[3] & 1;
-    rv->sub_id = AV_RB32((uint8_t*)avctx->extradata + 4);
+    s->h263_long_vectors = ((uint8_t*)avctx->extradata)[3] & 1;
+    rv->sub_id           = AV_RB32((uint8_t*)avctx->extradata + 4);
 
     major_ver = RV_GET_MAJOR_VER(rv->sub_id);
     minor_ver = RV_GET_MINOR_VER(rv->sub_id);
@@ -481,39 +472,40 @@
     switch (major_ver) {
     case 1:
         s->rv10_version = micro_ver ? 3 : 1;
-        s->obmc = micro_ver == 2;
+        s->obmc         = micro_ver == 2;
         break;
     case 2:
         if (minor_ver >= 2) {
-            s->low_delay = 0;
+            s->low_delay           = 0;
             s->avctx->has_b_frames = 1;
         }
         break;
     default:
         av_log(s->avctx, AV_LOG_ERROR, "unknown header %X\n", rv->sub_id);
-        av_log_missing_feature(avctx, "RV1/2 version", 1);
+        avpriv_request_sample(avctx, "RV1/2 version");
         return AVERROR_PATCHWELCOME;
     }
 
-    if(avctx->debug & FF_DEBUG_PICT_INFO){
-        av_log(avctx, AV_LOG_DEBUG, "ver:%X ver0:%X\n", rv->sub_id, ((uint32_t*)avctx->extradata)[0]);
+    if (avctx->debug & FF_DEBUG_PICT_INFO) {
+        av_log(avctx, AV_LOG_DEBUG, "ver:%X ver0:%X\n", rv->sub_id,
+               ((uint32_t*)avctx->extradata)[0]);
     }
 
     avctx->pix_fmt = AV_PIX_FMT_YUV420P;
 
-    if (ff_MPV_common_init(s) < 0)
-        return -1;
+    if ((ret = ff_MPV_common_init(s)) < 0)
+        return ret;
 
     ff_h263_decode_init_vlc();
 
     /* init rv vlc */
     if (!done) {
         INIT_VLC_STATIC(&rv_dc_lum, DC_VLC_BITS, 256,
-                 rv_lum_bits, 1, 1,
-                 rv_lum_code, 2, 2, 16384);
+                        rv_lum_bits, 1, 1,
+                        rv_lum_code, 2, 2, 16384);
         INIT_VLC_STATIC(&rv_dc_chrom, DC_VLC_BITS, 256,
-                 rv_chrom_bits, 1, 1,
-                 rv_chrom_code, 2, 2, 16388);
+                        rv_chrom_bits, 1, 1,
+                        rv_chrom_code, 2, 2, 16388);
         done = 1;
     }
 
@@ -529,48 +521,49 @@
 }
 
 static int rv10_decode_packet(AVCodecContext *avctx,
-                             const uint8_t *buf, int buf_size, int buf_size2)
+                              const uint8_t *buf, int buf_size, int buf_size2)
 {
     RVDecContext  *rv = avctx->priv_data;
     MpegEncContext *s = &rv->m;
-    int mb_count, mb_pos, left, start_mb_x, active_bits_size;
+    int mb_count, mb_pos, left, start_mb_x, active_bits_size, ret;
 
     active_bits_size = buf_size * 8;
     init_get_bits(&s->gb, buf, FFMAX(buf_size, buf_size2) * 8);
-    if(s->codec_id ==AV_CODEC_ID_RV10)
+    if (s->codec_id == AV_CODEC_ID_RV10)
         mb_count = rv10_decode_picture_header(s);
     else
         mb_count = rv20_decode_picture_header(rv);
     if (mb_count < 0) {
-        av_log(s->avctx, AV_LOG_ERROR, "HEADER ERROR\n");
-        return -1;
+        if (mb_count != ERROR_SKIP_FRAME)
+            av_log(s->avctx, AV_LOG_ERROR, "HEADER ERROR\n");
+        return AVERROR_INVALIDDATA;
     }
 
     if (s->mb_x >= s->mb_width ||
         s->mb_y >= s->mb_height) {
         av_log(s->avctx, AV_LOG_ERROR, "POS ERROR %d %d\n", s->mb_x, s->mb_y);
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
     mb_pos = s->mb_y * s->mb_width + s->mb_x;
     left = s->mb_width * s->mb_height - mb_pos;
     if (mb_count > left) {
         av_log(s->avctx, AV_LOG_ERROR, "COUNT ERROR\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
-    if ((s->mb_x == 0 && s->mb_y == 0) || s->current_picture_ptr==NULL) {
-        if(s->current_picture_ptr){ //FIXME write parser so we always have complete frames?
+    if ((s->mb_x == 0 && s->mb_y == 0) || s->current_picture_ptr == NULL) {
+        if (s->current_picture_ptr) { // FIXME write parser so we always have complete frames?
             ff_er_frame_end(&s->er);
             ff_MPV_frame_end(s);
-            s->mb_x= s->mb_y = s->resync_mb_x = s->resync_mb_y= 0;
+            s->mb_x = s->mb_y = s->resync_mb_x = s->resync_mb_y = 0;
         }
-        if(ff_MPV_frame_start(s, avctx) < 0)
-            return -1;
+        if ((ret = ff_MPV_frame_start(s, avctx)) < 0)
+            return ret;
         ff_mpeg_er_frame_start(s);
     } else {
         if (s->current_picture_ptr->f.pict_type != s->pict_type) {
             av_log(s->avctx, AV_LOG_ERROR, "Slice type mismatch\n");
-            return -1;
+            return AVERROR_INVALIDDATA;
         }
     }
 
@@ -578,47 +571,46 @@
     av_dlog(avctx, "qscale=%d\n", s->qscale);
 
     /* default quantization values */
-    if(s->codec_id== AV_CODEC_ID_RV10){
-        if(s->mb_y==0) s->first_slice_line=1;
-    }else{
-        s->first_slice_line=1;
-        s->resync_mb_x= s->mb_x;
+    if (s->codec_id == AV_CODEC_ID_RV10) {
+        if (s->mb_y == 0)
+            s->first_slice_line = 1;
+    } else {
+        s->first_slice_line = 1;
+        s->resync_mb_x      = s->mb_x;
     }
-    start_mb_x= s->mb_x;
-    s->resync_mb_y= s->mb_y;
-    if(s->h263_aic){
-        s->y_dc_scale_table=
-        s->c_dc_scale_table= ff_aic_dc_scale_table;
-    }else{
-        s->y_dc_scale_table=
-        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
+    start_mb_x     = s->mb_x;
+    s->resync_mb_y = s->mb_y;
+    if (s->h263_aic) {
+        s->y_dc_scale_table = s->c_dc_scale_table = ff_aic_dc_scale_table;
+    } else {
+        s->y_dc_scale_table = s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
     }
 
-    if(s->modified_quant)
-        s->chroma_qscale_table= ff_h263_chroma_qscale_table;
+    if (s->modified_quant)
+        s->chroma_qscale_table = ff_h263_chroma_qscale_table;
 
     ff_set_qscale(s, s->qscale);
 
     s->rv10_first_dc_coded[0] = 0;
     s->rv10_first_dc_coded[1] = 0;
     s->rv10_first_dc_coded[2] = 0;
-    s->block_wrap[0]=
-    s->block_wrap[1]=
-    s->block_wrap[2]=
-    s->block_wrap[3]= s->b8_stride;
-    s->block_wrap[4]=
-    s->block_wrap[5]= s->mb_stride;
+    s->block_wrap[0] =
+    s->block_wrap[1] =
+    s->block_wrap[2] =
+    s->block_wrap[3] = s->b8_stride;
+    s->block_wrap[4] =
+    s->block_wrap[5] = s->mb_stride;
     ff_init_block_index(s);
-    /* decode each macroblock */
 
-    for(s->mb_num_left= mb_count; s->mb_num_left>0; s->mb_num_left--) {
+    /* decode each macroblock */
+    for (s->mb_num_left = mb_count; s->mb_num_left > 0; s->mb_num_left--) {
         int ret;
         ff_update_block_index(s);
         av_dlog(avctx, "**mb x=%d y=%d\n", s->mb_x, s->mb_y);
 
-        s->mv_dir = MV_DIR_FORWARD;
+        s->mv_dir  = MV_DIR_FORWARD;
         s->mv_type = MV_TYPE_16X16;
-        ret=ff_h263_decode_mb(s, s->block);
+        ret = ff_h263_decode_mb(s, s->block);
 
         // Repeat the slice end check from ff_h263_decode_mb with our active
         // bitstream size
@@ -636,17 +628,17 @@
             active_bits_size = buf_size2 * 8;
             av_log(avctx, AV_LOG_DEBUG, "update size from %d to %d\n",
                    8 * buf_size, active_bits_size);
-            ret= SLICE_OK;
+            ret = SLICE_OK;
         }
 
         if (ret == SLICE_ERROR || active_bits_size < get_bits_count(&s->gb)) {
             av_log(s->avctx, AV_LOG_ERROR, "ERROR at MB %d %d\n", s->mb_x, s->mb_y);
-            return -1;
+            return AVERROR_INVALIDDATA;
         }
-        if(s->pict_type != AV_PICTURE_TYPE_B)
+        if (s->pict_type != AV_PICTURE_TYPE_B)
             ff_h263_update_motion_val(s);
         ff_MPV_decode_mb(s, s->block);
-        if(s->loop_filter)
+        if (s->loop_filter)
             ff_h263_loop_filter(s);
 
         if (++s->mb_x == s->mb_width) {
@@ -654,20 +646,24 @@
             s->mb_y++;
             ff_init_block_index(s);
         }
-        if(s->mb_x == s->resync_mb_x)
-            s->first_slice_line=0;
-        if(ret == SLICE_END) break;
+        if (s->mb_x == s->resync_mb_x)
+            s->first_slice_line = 0;
+        if (ret == SLICE_END)
+            break;
     }
 
-    ff_er_add_slice(&s->er, start_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END);
+    ff_er_add_slice(&s->er, start_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y,
+                    ER_MB_END);
 
     return active_bits_size;
 }
 
 static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n)
 {
-    if(avctx->slice_count) return avctx->slice_offset[n];
-    else                   return AV_RL32(buf + n*8);
+    if (avctx->slice_count)
+        return avctx->slice_offset[n];
+    else
+        return AV_RL32(buf + n * 8);
 }
 
 static int rv10_decode_frame(AVCodecContext *avctx,
@@ -675,10 +671,10 @@
                              AVPacket *avpkt)
 {
     const uint8_t *buf = avpkt->data;
-    int buf_size = avpkt->size;
-    MpegEncContext *s = avctx->priv_data;
-    int i;
-    AVFrame *pict = data;
+    int buf_size       = avpkt->size;
+    MpegEncContext *s  = avctx->priv_data;
+    AVFrame *pict      = data;
+    int i, ret;
     int slice_count;
     const uint8_t *slices_hdr = NULL;
 
@@ -691,7 +687,7 @@
         return 0;
     }
 
-    if(!avctx->slice_count){
+    if (!avctx->slice_count) {
         slice_count = (*buf++) + 1;
         buf_size--;
 
@@ -701,51 +697,58 @@
         }
 
         slices_hdr = buf + 4;
-        buf += 8 * slice_count;
-        buf_size -= 8 * slice_count;
-    }else
+        buf       += 8 * slice_count;
+        buf_size  -= 8 * slice_count;
+    } else
         slice_count = avctx->slice_count;
 
-    for(i=0; i<slice_count; i++){
+    for (i = 0; i < slice_count; i++) {
         unsigned offset = get_slice_offset(avctx, slices_hdr, i);
         int size, size2;
 
         if (offset >= buf_size)
             return AVERROR_INVALIDDATA;
 
-        if(i+1 == slice_count)
-            size= buf_size - offset;
+        if (i + 1 == slice_count)
+            size = buf_size - offset;
         else
-            size= get_slice_offset(avctx, slices_hdr, i+1) - offset;
+            size = get_slice_offset(avctx, slices_hdr, i + 1) - offset;
 
-        if(i+2 >= slice_count)
-            size2= buf_size - offset;
+        if (i + 2 >= slice_count)
+            size2 = buf_size - offset;
         else
-            size2= get_slice_offset(avctx, slices_hdr, i+2) - offset;
+            size2 = get_slice_offset(avctx, slices_hdr, i + 2) - offset;
 
         if (size <= 0 || size2 <= 0 ||
             offset + FFMAX(size, size2) > buf_size)
             return AVERROR_INVALIDDATA;
 
-        if(rv10_decode_packet(avctx, buf+offset, size, size2) > 8*size)
+        if (rv10_decode_packet(avctx, buf + offset, size, size2) > 8 * size)
             i++;
     }
 
-    if(s->current_picture_ptr != NULL && s->mb_y>=s->mb_height){
+    if (s->current_picture_ptr != NULL && s->mb_y >= s->mb_height) {
         ff_er_frame_end(&s->er);
         ff_MPV_frame_end(s);
 
         if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
-            *pict = s->current_picture_ptr->f;
+            if ((ret = av_frame_ref(pict, &s->current_picture_ptr->f)) < 0)
+                return ret;
+            ff_print_debug_info(s, s->current_picture_ptr, pict);
+            ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG1);
         } else if (s->last_picture_ptr != NULL) {
-            *pict = s->last_picture_ptr->f;
+            if ((ret = av_frame_ref(pict, &s->last_picture_ptr->f)) < 0)
+                return ret;
+            ff_print_debug_info(s, s->last_picture_ptr, pict);
+            ff_mpv_export_qp_table(s, pict,s->last_picture_ptr, FF_QSCALE_TYPE_MPEG1);
         }
 
-        if(s->last_picture_ptr || s->low_delay){
+        if (s->last_picture_ptr || s->low_delay) {
             *got_frame = 1;
-            ff_print_debug_info(s, pict);
         }
-        s->current_picture_ptr= NULL; // so we can detect if frame_end was not called (find some nicer solution...)
+
+        // so we can detect if frame_end was not called (find some nicer solution...)
+        s->current_picture_ptr = NULL;
     }
 
     return avpkt->size;
diff --git a/libavcodec/rv30.c b/libavcodec/rv30.c
index 7f8f8cb..f40870e 100644
--- a/libavcodec/rv30.c
+++ b/libavcodec/rv30.c
@@ -124,7 +124,7 @@
 static inline void rv30_weak_loop_filter(uint8_t *src, const int step,
                                          const int stride, const int lim)
 {
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
     int i, diff;
 
     for(i = 0; i < 4; i++){
@@ -146,7 +146,7 @@
 
     mb_pos = row * s->mb_stride;
     for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){
-        int mbtype = s->current_picture_ptr->f.mb_type[mb_pos];
+        int mbtype = s->current_picture_ptr->mb_type[mb_pos];
         if(IS_INTRA(mbtype) || IS_SEPARATE_DC(mbtype))
             r->deblock_coefs[mb_pos] = 0xFFFF;
         if(IS_INTRA(mbtype))
@@ -158,9 +158,9 @@
      */
     mb_pos = row * s->mb_stride;
     for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){
-        cur_lim = rv30_loop_filt_lim[s->current_picture_ptr->f.qscale_table[mb_pos]];
+        cur_lim = rv30_loop_filt_lim[s->current_picture_ptr->qscale_table[mb_pos]];
         if(mb_x)
-            left_lim = rv30_loop_filt_lim[s->current_picture_ptr->f.qscale_table[mb_pos - 1]];
+            left_lim = rv30_loop_filt_lim[s->current_picture_ptr->qscale_table[mb_pos - 1]];
         for(j = 0; j < 16; j += 4){
             Y = s->current_picture_ptr->f.data[0] + mb_x*16 + (row*16 + j) * s->linesize + 4 * !mb_x;
             for(i = !mb_x; i < 4; i++, Y += 4){
@@ -200,9 +200,9 @@
     }
     mb_pos = row * s->mb_stride;
     for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){
-        cur_lim = rv30_loop_filt_lim[s->current_picture_ptr->f.qscale_table[mb_pos]];
+        cur_lim = rv30_loop_filt_lim[s->current_picture_ptr->qscale_table[mb_pos]];
         if(row)
-            top_lim = rv30_loop_filt_lim[s->current_picture_ptr->f.qscale_table[mb_pos - s->mb_stride]];
+            top_lim = rv30_loop_filt_lim[s->current_picture_ptr->qscale_table[mb_pos - s->mb_stride]];
         for(j = 4*!row; j < 16; j += 4){
             Y = s->current_picture_ptr->f.data[0] + mb_x*16 + (row*16 + j) * s->linesize;
             for(i = 0; i < 4; i++, Y += 4){
diff --git a/libavcodec/rv30dsp.c b/libavcodec/rv30dsp.c
index d13a2d8..5e47b64 100644
--- a/libavcodec/rv30dsp.c
+++ b/libavcodec/rv30dsp.c
@@ -32,7 +32,7 @@
 #define RV30_LOWPASS(OPNAME, OP) \
 static av_unused void OPNAME ## rv30_tpel8_h_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, const int C1, const int C2){\
     const int h = 8;\
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
     int i;\
     for(i = 0; i < h; i++)\
     {\
@@ -51,7 +51,7 @@
 \
 static void OPNAME ## rv30_tpel8_v_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, const int C1, const int C2){\
     const int w = 8;\
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
     int i;\
     for(i = 0; i < w; i++)\
     {\
@@ -82,7 +82,7 @@
 static void OPNAME ## rv30_tpel8_hv_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
     const int w = 8;\
     const int h = 8;\
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
     int i, j;\
     for(j = 0; j < h; j++){\
         for(i = 0; i < w; i++){\
@@ -101,7 +101,7 @@
 static void OPNAME ## rv30_tpel8_hhv_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
     const int w = 8;\
     const int h = 8;\
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
     int i, j;\
     for(j = 0; j < h; j++){\
         for(i = 0; i < w; i++){\
@@ -120,7 +120,7 @@
 static void OPNAME ## rv30_tpel8_hvv_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
     const int w = 8;\
     const int h = 8;\
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
     int i, j;\
     for(j = 0; j < h; j++){\
         for(i = 0; i < w; i++){\
@@ -139,7 +139,7 @@
 static void OPNAME ## rv30_tpel8_hhvv_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
     const int w = 8;\
     const int h = 8;\
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
     int i, j;\
     for(j = 0; j < h; j++){\
         for(i = 0; i < w; i++){\
@@ -210,35 +210,43 @@
 \
 
 #define RV30_MC(OPNAME, SIZE) \
-static void OPNAME ## rv30_tpel ## SIZE ## _mc10_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## rv30_tpel ## SIZE ## _mc10_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## rv30_tpel ## SIZE ## _h_lowpass(dst, src, stride, stride, 12, 6);\
 }\
 \
-static void OPNAME ## rv30_tpel ## SIZE ## _mc20_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## rv30_tpel ## SIZE ## _mc20_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## rv30_tpel ## SIZE ## _h_lowpass(dst, src, stride, stride, 6, 12);\
 }\
 \
-static void OPNAME ## rv30_tpel ## SIZE ## _mc01_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## rv30_tpel ## SIZE ## _mc01_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## rv30_tpel ## SIZE ## _v_lowpass(dst, src, stride, stride, 12, 6);\
 }\
 \
-static void OPNAME ## rv30_tpel ## SIZE ## _mc02_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## rv30_tpel ## SIZE ## _mc02_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## rv30_tpel ## SIZE ## _v_lowpass(dst, src, stride, stride, 6, 12);\
 }\
 \
-static void OPNAME ## rv30_tpel ## SIZE ## _mc11_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## rv30_tpel ## SIZE ## _mc11_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## rv30_tpel ## SIZE ## _hv_lowpass(dst, src, stride, stride);\
 }\
 \
-static void OPNAME ## rv30_tpel ## SIZE ## _mc12_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## rv30_tpel ## SIZE ## _mc12_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## rv30_tpel ## SIZE ## _hvv_lowpass(dst, src, stride, stride);\
 }\
 \
-static void OPNAME ## rv30_tpel ## SIZE ## _mc21_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## rv30_tpel ## SIZE ## _mc21_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## rv30_tpel ## SIZE ## _hhv_lowpass(dst, src, stride, stride);\
 }\
 \
-static void OPNAME ## rv30_tpel ## SIZE ## _mc22_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## rv30_tpel ## SIZE ## _mc22_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## rv30_tpel ## SIZE ## _hhvv_lowpass(dst, src, stride, stride);\
 }\
 \
diff --git a/libavcodec/rv34.c b/libavcodec/rv34.c
index d71c71a..35f9dea 100644
--- a/libavcodec/rv34.c
+++ b/libavcodec/rv34.c
@@ -28,6 +28,7 @@
 #include "libavutil/internal.h"
 
 #include "avcodec.h"
+#include "error_resilience.h"
 #include "mpegvideo.h"
 #include "golomb.h"
 #include "internal.h"
@@ -357,7 +358,7 @@
 
     r->is16 = get_bits1(gb);
     if(r->is16){
-        s->current_picture_ptr->f.mb_type[mb_pos] = MB_TYPE_INTRA16x16;
+        s->current_picture_ptr->mb_type[mb_pos] = MB_TYPE_INTRA16x16;
         r->block_type = RV34_MB_TYPE_INTRA16x16;
         t = get_bits(gb, 2);
         fill_rectangle(intra_types, 4, 4, r->intra_types_stride, t, sizeof(intra_types[0]));
@@ -367,7 +368,7 @@
             if(!get_bits1(gb))
                 av_log(s->avctx, AV_LOG_ERROR, "Need DQUANT\n");
         }
-        s->current_picture_ptr->f.mb_type[mb_pos] = MB_TYPE_INTRA;
+        s->current_picture_ptr->mb_type[mb_pos] = MB_TYPE_INTRA;
         r->block_type = RV34_MB_TYPE_INTRA;
         if(r->decode_intra_types(r, gb, intra_types) < 0)
             return -1;
@@ -393,7 +394,7 @@
     r->block_type = r->decode_mb_info(r);
     if(r->block_type == -1)
         return -1;
-    s->current_picture_ptr->f.mb_type[mb_pos] = rv34_mb_type_to_lavc[r->block_type];
+    s->current_picture_ptr->mb_type[mb_pos] = rv34_mb_type_to_lavc[r->block_type];
     r->mb_type[mb_pos] = r->block_type;
     if(r->block_type == RV34_MB_SKIP){
         if(s->pict_type == AV_PICTURE_TYPE_P)
@@ -401,7 +402,7 @@
         if(s->pict_type == AV_PICTURE_TYPE_B)
             r->mb_type[mb_pos] = RV34_MB_B_DIRECT;
     }
-    r->is16 = !!IS_INTRA16x16(s->current_picture_ptr->f.mb_type[mb_pos]);
+    r->is16 = !!IS_INTRA16x16(s->current_picture_ptr->mb_type[mb_pos]);
     rv34_decode_mv(r, r->block_type);
     if(r->block_type == RV34_MB_SKIP){
         fill_rectangle(intra_types, 4, 4, r->intra_types_stride, 0, sizeof(intra_types[0]));
@@ -410,7 +411,7 @@
     r->chroma_vlc = 1;
     r->luma_vlc   = 0;
 
-    if(IS_INTRA(s->current_picture_ptr->f.mb_type[mb_pos])){
+    if(IS_INTRA(s->current_picture_ptr->mb_type[mb_pos])){
         if(r->is16){
             t = get_bits(gb, 2);
             fill_rectangle(intra_types, 4, 4, r->intra_types_stride, t, sizeof(intra_types[0]));
@@ -475,27 +476,27 @@
         c_off = -1;
 
     if(avail[-1]){
-        A[0] = s->current_picture_ptr->f.motion_val[0][mv_pos-1][0];
-        A[1] = s->current_picture_ptr->f.motion_val[0][mv_pos-1][1];
+        A[0] = s->current_picture_ptr->motion_val[0][mv_pos-1][0];
+        A[1] = s->current_picture_ptr->motion_val[0][mv_pos-1][1];
     }
     if(avail[-4]){
-        B[0] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride][0];
-        B[1] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride][1];
+        B[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride][0];
+        B[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride][1];
     }else{
         B[0] = A[0];
         B[1] = A[1];
     }
     if(!avail[c_off-4]){
         if(avail[-4] && (avail[-1] || r->rv30)){
-            C[0] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride-1][0];
-            C[1] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride-1][1];
+            C[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride-1][0];
+            C[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride-1][1];
         }else{
             C[0] = A[0];
             C[1] = A[1];
         }
     }else{
-        C[0] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride+c_off][0];
-        C[1] = s->current_picture_ptr->f.motion_val[0][mv_pos-s->b8_stride+c_off][1];
+        C[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride+c_off][0];
+        C[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride+c_off][1];
     }
     mx = mid_pred(A[0], B[0], C[0]);
     my = mid_pred(A[1], B[1], C[1]);
@@ -503,8 +504,8 @@
     my += r->dmv[dmv_no][1];
     for(j = 0; j < part_sizes_h[block_type]; j++){
         for(i = 0; i < part_sizes_w[block_type]; i++){
-            s->current_picture_ptr->f.motion_val[0][mv_pos + i + j*s->b8_stride][0] = mx;
-            s->current_picture_ptr->f.motion_val[0][mv_pos + i + j*s->b8_stride][1] = my;
+            s->current_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][0] = mx;
+            s->current_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][1] = my;
         }
     }
 }
@@ -555,25 +556,25 @@
     int i, j;
     Picture *cur_pic = s->current_picture_ptr;
     const int mask = dir ? MB_TYPE_L1 : MB_TYPE_L0;
-    int type = cur_pic->f.mb_type[mb_pos];
+    int type = cur_pic->mb_type[mb_pos];
 
     if((r->avail_cache[6-1] & type) & mask){
-        A[0] = cur_pic->f.motion_val[dir][mv_pos - 1][0];
-        A[1] = cur_pic->f.motion_val[dir][mv_pos - 1][1];
+        A[0] = cur_pic->motion_val[dir][mv_pos - 1][0];
+        A[1] = cur_pic->motion_val[dir][mv_pos - 1][1];
         has_A = 1;
     }
     if((r->avail_cache[6-4] & type) & mask){
-        B[0] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride][0];
-        B[1] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride][1];
+        B[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride][0];
+        B[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride][1];
         has_B = 1;
     }
     if(r->avail_cache[6-4] && (r->avail_cache[6-2] & type) & mask){
-        C[0] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride + 2][0];
-        C[1] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride + 2][1];
+        C[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride + 2][0];
+        C[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride + 2][1];
         has_C = 1;
     }else if((s->mb_x+1) == s->mb_width && (r->avail_cache[6-5] & type) & mask){
-        C[0] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride - 1][0];
-        C[1] = cur_pic->f.motion_val[dir][mv_pos - s->b8_stride - 1][1];
+        C[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride - 1][0];
+        C[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride - 1][1];
         has_C = 1;
     }
 
@@ -584,12 +585,12 @@
 
     for(j = 0; j < 2; j++){
         for(i = 0; i < 2; i++){
-            cur_pic->f.motion_val[dir][mv_pos + i + j*s->b8_stride][0] = mx;
-            cur_pic->f.motion_val[dir][mv_pos + i + j*s->b8_stride][1] = my;
+            cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][0] = mx;
+            cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][1] = my;
         }
     }
     if(block_type == RV34_MB_B_BACKWARD || block_type == RV34_MB_B_FORWARD){
-        ZERO8x2(cur_pic->f.motion_val[!dir][mv_pos], s->b8_stride);
+        ZERO8x2(cur_pic->motion_val[!dir][mv_pos], s->b8_stride);
     }
 }
 
@@ -606,27 +607,27 @@
     int* avail = r->avail_cache + avail_indexes[0];
 
     if(avail[-1]){
-        A[0] = s->current_picture_ptr->f.motion_val[0][mv_pos - 1][0];
-        A[1] = s->current_picture_ptr->f.motion_val[0][mv_pos - 1][1];
+        A[0] = s->current_picture_ptr->motion_val[0][mv_pos - 1][0];
+        A[1] = s->current_picture_ptr->motion_val[0][mv_pos - 1][1];
     }
     if(avail[-4]){
-        B[0] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride][0];
-        B[1] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride][1];
+        B[0] = s->current_picture_ptr->motion_val[0][mv_pos - s->b8_stride][0];
+        B[1] = s->current_picture_ptr->motion_val[0][mv_pos - s->b8_stride][1];
     }else{
         B[0] = A[0];
         B[1] = A[1];
     }
     if(!avail[-4 + 2]){
         if(avail[-4] && (avail[-1])){
-            C[0] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride - 1][0];
-            C[1] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride - 1][1];
+            C[0] = s->current_picture_ptr->motion_val[0][mv_pos - s->b8_stride - 1][0];
+            C[1] = s->current_picture_ptr->motion_val[0][mv_pos - s->b8_stride - 1][1];
         }else{
             C[0] = A[0];
             C[1] = A[1];
         }
     }else{
-        C[0] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride + 2][0];
-        C[1] = s->current_picture_ptr->f.motion_val[0][mv_pos - s->b8_stride + 2][1];
+        C[0] = s->current_picture_ptr->motion_val[0][mv_pos - s->b8_stride + 2][0];
+        C[1] = s->current_picture_ptr->motion_val[0][mv_pos - s->b8_stride + 2][1];
     }
     mx = mid_pred(A[0], B[0], C[0]);
     my = mid_pred(A[1], B[1], C[1]);
@@ -635,8 +636,8 @@
     for(j = 0; j < 2; j++){
         for(i = 0; i < 2; i++){
             for(k = 0; k < 2; k++){
-                s->current_picture_ptr->f.motion_val[k][mv_pos + i + j*s->b8_stride][0] = mx;
-                s->current_picture_ptr->f.motion_val[k][mv_pos + i + j*s->b8_stride][1] = my;
+                s->current_picture_ptr->motion_val[k][mv_pos + i + j*s->b8_stride][0] = mx;
+                s->current_picture_ptr->motion_val[k][mv_pos + i + j*s->b8_stride][1] = my;
             }
         }
     }
@@ -674,24 +675,24 @@
 
     if(thirdpel){
         int chroma_mx, chroma_my;
-        mx = (s->current_picture_ptr->f.motion_val[dir][mv_pos][0] + (3 << 24)) / 3 - (1 << 24);
-        my = (s->current_picture_ptr->f.motion_val[dir][mv_pos][1] + (3 << 24)) / 3 - (1 << 24);
-        lx = (s->current_picture_ptr->f.motion_val[dir][mv_pos][0] + (3 << 24)) % 3;
-        ly = (s->current_picture_ptr->f.motion_val[dir][mv_pos][1] + (3 << 24)) % 3;
-        chroma_mx = s->current_picture_ptr->f.motion_val[dir][mv_pos][0] / 2;
-        chroma_my = s->current_picture_ptr->f.motion_val[dir][mv_pos][1] / 2;
+        mx = (s->current_picture_ptr->motion_val[dir][mv_pos][0] + (3 << 24)) / 3 - (1 << 24);
+        my = (s->current_picture_ptr->motion_val[dir][mv_pos][1] + (3 << 24)) / 3 - (1 << 24);
+        lx = (s->current_picture_ptr->motion_val[dir][mv_pos][0] + (3 << 24)) % 3;
+        ly = (s->current_picture_ptr->motion_val[dir][mv_pos][1] + (3 << 24)) % 3;
+        chroma_mx = s->current_picture_ptr->motion_val[dir][mv_pos][0] / 2;
+        chroma_my = s->current_picture_ptr->motion_val[dir][mv_pos][1] / 2;
         umx = (chroma_mx + (3 << 24)) / 3 - (1 << 24);
         umy = (chroma_my + (3 << 24)) / 3 - (1 << 24);
         uvmx = chroma_coeffs[(chroma_mx + (3 << 24)) % 3];
         uvmy = chroma_coeffs[(chroma_my + (3 << 24)) % 3];
     }else{
         int cx, cy;
-        mx = s->current_picture_ptr->f.motion_val[dir][mv_pos][0] >> 2;
-        my = s->current_picture_ptr->f.motion_val[dir][mv_pos][1] >> 2;
-        lx = s->current_picture_ptr->f.motion_val[dir][mv_pos][0] & 3;
-        ly = s->current_picture_ptr->f.motion_val[dir][mv_pos][1] & 3;
-        cx = s->current_picture_ptr->f.motion_val[dir][mv_pos][0] / 2;
-        cy = s->current_picture_ptr->f.motion_val[dir][mv_pos][1] / 2;
+        mx = s->current_picture_ptr->motion_val[dir][mv_pos][0] >> 2;
+        my = s->current_picture_ptr->motion_val[dir][mv_pos][1] >> 2;
+        lx = s->current_picture_ptr->motion_val[dir][mv_pos][0] & 3;
+        ly = s->current_picture_ptr->motion_val[dir][mv_pos][1] & 3;
+        cx = s->current_picture_ptr->motion_val[dir][mv_pos][0] / 2;
+        cy = s->current_picture_ptr->motion_val[dir][mv_pos][1] / 2;
         umx = cx >> 2;
         umy = cy >> 2;
         uvmx = (cx & 3) << 1;
@@ -704,7 +705,7 @@
     if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
         /* wait for the referenced mb row to be finished */
         int mb_row = s->mb_y + ((yoff + my + 5 + 8 * height) >> 4);
-        AVFrame *f = dir ? &s->next_picture_ptr->f : &s->last_picture_ptr->f;
+        ThreadFrame *f = dir ? &s->next_picture_ptr->tf : &s->last_picture_ptr->tf;
         ff_thread_await_progress(f, mb_row, 0);
     }
 
@@ -853,11 +854,11 @@
     switch(block_type){
     case RV34_MB_TYPE_INTRA:
     case RV34_MB_TYPE_INTRA16x16:
-        ZERO8x2(s->current_picture_ptr->f.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
+        ZERO8x2(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
         return 0;
     case RV34_MB_SKIP:
         if(s->pict_type == AV_PICTURE_TYPE_P){
-            ZERO8x2(s->current_picture_ptr->f.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
+            ZERO8x2(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
             rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
             break;
         }
@@ -865,23 +866,23 @@
         //surprisingly, it uses motion scheme from next reference frame
         /* wait for the current mb row to be finished */
         if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
-            ff_thread_await_progress(&s->next_picture_ptr->f, FFMAX(0, s->mb_y-1), 0);
+            ff_thread_await_progress(&s->next_picture_ptr->tf, FFMAX(0, s->mb_y-1), 0);
 
-        next_bt = s->next_picture_ptr->f.mb_type[s->mb_x + s->mb_y * s->mb_stride];
+        next_bt = s->next_picture_ptr->mb_type[s->mb_x + s->mb_y * s->mb_stride];
         if(IS_INTRA(next_bt) || IS_SKIP(next_bt)){
-            ZERO8x2(s->current_picture_ptr->f.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
-            ZERO8x2(s->current_picture_ptr->f.motion_val[1][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
+            ZERO8x2(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
+            ZERO8x2(s->current_picture_ptr->motion_val[1][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
         }else
             for(j = 0; j < 2; j++)
                 for(i = 0; i < 2; i++)
                     for(k = 0; k < 2; k++)
                         for(l = 0; l < 2; l++)
-                            s->current_picture_ptr->f.motion_val[l][mv_pos + i + j*s->b8_stride][k] = calc_add_mv(r, l, s->next_picture_ptr->f.motion_val[0][mv_pos + i + j*s->b8_stride][k]);
+                            s->current_picture_ptr->motion_val[l][mv_pos + i + j*s->b8_stride][k] = calc_add_mv(r, l, s->next_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][k]);
         if(!(IS_16X8(next_bt) || IS_8X16(next_bt) || IS_8X8(next_bt))) //we can use whole macroblock MC
             rv34_mc_2mv(r, block_type);
         else
             rv34_mc_2mv_skip(r);
-        ZERO8x2(s->current_picture_ptr->f.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
+        ZERO8x2(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
         break;
     case RV34_MB_P_16x16:
     case RV34_MB_P_MIX16x16:
@@ -1149,7 +1150,7 @@
     MpegEncContext *s = &r->s;
     int hmvmask = 0, vmvmask = 0, i, j;
     int midx = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
-    int16_t (*motion_val)[2] = &s->current_picture_ptr->f.motion_val[0][midx];
+    int16_t (*motion_val)[2] = &s->current_picture_ptr->motion_val[0][midx];
     for(j = 0; j < 16; j += 8){
         for(i = 0; i < 2; i++){
             if(is_mv_diff_gt_3(motion_val + i, 1))
@@ -1192,26 +1193,26 @@
     dist = (s->mb_x - s->resync_mb_x) + (s->mb_y - s->resync_mb_y) * s->mb_width;
     if(s->mb_x && dist)
         r->avail_cache[5] =
-        r->avail_cache[9] = s->current_picture_ptr->f.mb_type[mb_pos - 1];
+        r->avail_cache[9] = s->current_picture_ptr->mb_type[mb_pos - 1];
     if(dist >= s->mb_width)
         r->avail_cache[2] =
-        r->avail_cache[3] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride];
+        r->avail_cache[3] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride];
     if(((s->mb_x+1) < s->mb_width) && dist >= s->mb_width - 1)
-        r->avail_cache[4] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride + 1];
+        r->avail_cache[4] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride + 1];
     if(s->mb_x && dist > s->mb_width)
-        r->avail_cache[1] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride - 1];
+        r->avail_cache[1] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride - 1];
 
     s->qscale = r->si.quant;
     cbp = cbp2 = rv34_decode_inter_mb_header(r, intra_types);
     r->cbp_luma  [mb_pos] = cbp;
     r->cbp_chroma[mb_pos] = cbp >> 16;
     r->deblock_coefs[mb_pos] = rv34_set_deblock_coef(r) | r->cbp_luma[mb_pos];
-    s->current_picture_ptr->f.qscale_table[mb_pos] = s->qscale;
+    s->current_picture_ptr->qscale_table[mb_pos] = s->qscale;
 
     if(cbp == -1)
         return -1;
 
-    if (IS_INTRA(s->current_picture_ptr->f.mb_type[mb_pos])){
+    if (IS_INTRA(s->current_picture_ptr->mb_type[mb_pos])){
         if(r->is16) rv34_output_i16x16(r, intra_types, cbp);
         else        rv34_output_intra(r, intra_types, cbp);
         return 0;
@@ -1294,21 +1295,21 @@
     dist = (s->mb_x - s->resync_mb_x) + (s->mb_y - s->resync_mb_y) * s->mb_width;
     if(s->mb_x && dist)
         r->avail_cache[5] =
-        r->avail_cache[9] = s->current_picture_ptr->f.mb_type[mb_pos - 1];
+        r->avail_cache[9] = s->current_picture_ptr->mb_type[mb_pos - 1];
     if(dist >= s->mb_width)
         r->avail_cache[2] =
-        r->avail_cache[3] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride];
+        r->avail_cache[3] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride];
     if(((s->mb_x+1) < s->mb_width) && dist >= s->mb_width - 1)
-        r->avail_cache[4] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride + 1];
+        r->avail_cache[4] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride + 1];
     if(s->mb_x && dist > s->mb_width)
-        r->avail_cache[1] = s->current_picture_ptr->f.mb_type[mb_pos - s->mb_stride - 1];
+        r->avail_cache[1] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride - 1];
 
     s->qscale = r->si.quant;
     cbp = rv34_decode_intra_mb_header(r, intra_types);
     r->cbp_luma  [mb_pos] = cbp;
     r->cbp_chroma[mb_pos] = cbp >> 16;
     r->deblock_coefs[mb_pos] = 0xFFFF;
-    s->current_picture_ptr->f.qscale_table[mb_pos] = s->qscale;
+    s->current_picture_ptr->qscale_table[mb_pos] = s->qscale;
 
     if(cbp == -1)
         return -1;
@@ -1448,7 +1449,7 @@
                 r->loop_filter(r, s->mb_y - 2);
 
             if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
-                ff_thread_report_progress(&s->current_picture_ptr->f,
+                ff_thread_report_progress(&s->current_picture_ptr->tf,
                                           s->mb_y - 2, 0);
 
         }
@@ -1507,6 +1508,8 @@
     if(!intra_vlcs[0].cbppattern[0].bits)
         rv34_init_tables();
 
+    avctx->internal->allocate_progress = 1;
+
     return 0;
 }
 
@@ -1524,6 +1527,7 @@
         if ((err = rv34_decoder_alloc(r)) < 0)
             return err;
     }
+
     return 0;
 }
 
@@ -1567,28 +1571,42 @@
 {
     RV34DecContext *r = avctx->priv_data;
     MpegEncContext *s = &r->s;
-    int got_picture = 0;
+    int got_picture = 0, ret;
 
     ff_er_frame_end(&s->er);
     ff_MPV_frame_end(s);
     s->mb_num_left = 0;
 
     if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
-        ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 0);
+        ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
 
     if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
-        *pict = s->current_picture_ptr->f;
+        if ((ret = av_frame_ref(pict, &s->current_picture_ptr->f)) < 0)
+            return ret;
+        ff_print_debug_info(s, s->current_picture_ptr, pict);
+        ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG1);
         got_picture = 1;
     } else if (s->last_picture_ptr != NULL) {
-        *pict = s->last_picture_ptr->f;
+        if ((ret = av_frame_ref(pict, &s->last_picture_ptr->f)) < 0)
+            return ret;
+        ff_print_debug_info(s, s->last_picture_ptr, pict);
+        ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG1);
         got_picture = 1;
     }
-    if (got_picture)
-        ff_print_debug_info(s, pict);
 
     return got_picture;
 }
 
+static AVRational update_sar(int old_w, int old_h, AVRational sar, int new_w, int new_h)
+{
+    // attempt to keep aspect during typical resolution switches
+    if (!sar.num)
+        sar = (AVRational){1, 1};
+
+    sar = av_mul_q(sar, (AVRational){new_h * old_w, new_w * old_h});
+    return sar;
+}
+
 int ff_rv34_decode_frame(AVCodecContext *avctx,
                             void *data, int *got_picture_ptr,
                             AVPacket *avpkt)
@@ -1599,7 +1617,7 @@
     MpegEncContext *s = &r->s;
     AVFrame *pict = data;
     SliceInfo si;
-    int i;
+    int i, ret;
     int slice_count;
     const uint8_t *slices_hdr = NULL;
     int last = 0;
@@ -1608,7 +1626,8 @@
     if (buf_size == 0) {
         /* special case for last picture */
         if (s->low_delay==0 && s->next_picture_ptr) {
-            *pict = s->next_picture_ptr->f;
+            if ((ret = av_frame_ref(pict, &s->next_picture_ptr->f)) < 0)
+                return ret;
             s->next_picture_ptr = NULL;
 
             *got_picture_ptr = 1;
@@ -1664,6 +1683,9 @@
             if (av_image_check_size(si.width, si.height, 0, s->avctx))
                 return AVERROR_INVALIDDATA;
 
+            s->avctx->sample_aspect_ratio = update_sar(
+                s->width, s->height, s->avctx->sample_aspect_ratio,
+                si.width, si.height);
             s->width  = si.width;
             s->height = si.height;
             avcodec_set_dimensions(s->avctx, s->width, s->height);
@@ -1768,7 +1790,10 @@
             if(r->loop_filter)
                 r->loop_filter(r, s->mb_height - 1);
 
-            *got_picture_ptr = finish_frame(avctx, pict);
+            ret = finish_frame(avctx, pict);
+            if (ret < 0)
+                return ret;
+            *got_picture_ptr = ret;
         } else if (HAVE_THREADS &&
                    (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
             av_log(avctx, AV_LOG_INFO, "marking unfished frame as finished\n");
@@ -1777,7 +1802,7 @@
             ff_er_frame_end(&s->er);
             ff_MPV_frame_end(s);
             s->mb_num_left = 0;
-            ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 0);
+            ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
             return AVERROR_INVALIDDATA;
         }
     }
diff --git a/libavcodec/rv40.c b/libavcodec/rv40.c
index fe3608e..d6dfc5e 100644
--- a/libavcodec/rv40.c
+++ b/libavcodec/rv40.c
@@ -366,7 +366,7 @@
 
     mb_pos = row * s->mb_stride;
     for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){
-        int mbtype = s->current_picture_ptr->f.mb_type[mb_pos];
+        int mbtype = s->current_picture_ptr->mb_type[mb_pos];
         if(IS_INTRA(mbtype) || IS_SEPARATE_DC(mbtype))
             r->cbp_luma  [mb_pos] = r->deblock_coefs[mb_pos] = 0xFFFF;
         if(IS_INTRA(mbtype))
@@ -381,7 +381,7 @@
         unsigned y_to_deblock;
         int c_to_deblock[2];
 
-        q = s->current_picture_ptr->f.qscale_table[mb_pos];
+        q = s->current_picture_ptr->qscale_table[mb_pos];
         alpha = rv40_alpha_tab[q];
         beta  = rv40_beta_tab [q];
         betaY = betaC = beta * 3;
@@ -396,7 +396,7 @@
             if(avail[i]){
                 int pos = mb_pos + neighbour_offs_x[i] + neighbour_offs_y[i]*s->mb_stride;
                 mvmasks[i] = r->deblock_coefs[pos];
-                mbtype [i] = s->current_picture_ptr->f.mb_type[pos];
+                mbtype [i] = s->current_picture_ptr->mb_type[pos];
                 cbp    [i] = r->cbp_luma[pos];
                 uvcbp[i][0] = r->cbp_chroma[pos] & 0xF;
                 uvcbp[i][1] = r->cbp_chroma[pos] >> 4;
diff --git a/libavcodec/rv40dsp.c b/libavcodec/rv40dsp.c
index cea4552..ad985f8 100644
--- a/libavcodec/rv40dsp.c
+++ b/libavcodec/rv40dsp.c
@@ -33,7 +33,7 @@
 #define RV40_LOWPASS(OPNAME, OP) \
 static av_unused void OPNAME ## rv40_qpel8_h_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride,\
                                                      const int h, const int C1, const int C2, const int SHIFT){\
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
     int i;\
     for(i = 0; i < h; i++)\
     {\
@@ -52,7 +52,7 @@
 \
 static void OPNAME ## rv40_qpel8_v_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride,\
                                            const int w, const int C1, const int C2, const int SHIFT){\
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
     int i;\
     for(i = 0; i < w; i++)\
     {\
@@ -104,72 +104,84 @@
 \
 
 #define RV40_MC(OPNAME, SIZE) \
-static void OPNAME ## rv40_qpel ## SIZE ## _mc10_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## rv40_qpel ## SIZE ## _mc10_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## rv40_qpel ## SIZE ## _h_lowpass(dst, src, stride, stride, SIZE, 52, 20, 6);\
 }\
 \
-static void OPNAME ## rv40_qpel ## SIZE ## _mc30_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## rv40_qpel ## SIZE ## _mc30_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## rv40_qpel ## SIZE ## _h_lowpass(dst, src, stride, stride, SIZE, 20, 52, 6);\
 }\
 \
-static void OPNAME ## rv40_qpel ## SIZE ## _mc01_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## rv40_qpel ## SIZE ## _mc01_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## rv40_qpel ## SIZE ## _v_lowpass(dst, src, stride, stride, SIZE, 52, 20, 6);\
 }\
 \
-static void OPNAME ## rv40_qpel ## SIZE ## _mc11_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## rv40_qpel ## SIZE ## _mc11_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[SIZE*(SIZE+5)];\
     uint8_t * const full_mid = full + SIZE*2;\
     put_rv40_qpel ## SIZE ## _h_lowpass(full, src - 2*stride, SIZE, stride, SIZE+5, 52, 20, 6);\
     OPNAME ## rv40_qpel ## SIZE ## _v_lowpass(dst, full_mid, stride, SIZE, SIZE, 52, 20, 6);\
 }\
 \
-static void OPNAME ## rv40_qpel ## SIZE ## _mc21_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## rv40_qpel ## SIZE ## _mc21_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[SIZE*(SIZE+5)];\
     uint8_t * const full_mid = full + SIZE*2;\
     put_rv40_qpel ## SIZE ## _h_lowpass(full, src - 2*stride, SIZE, stride, SIZE+5, 20, 20, 5);\
     OPNAME ## rv40_qpel ## SIZE ## _v_lowpass(dst, full_mid, stride, SIZE, SIZE, 52, 20, 6);\
 }\
 \
-static void OPNAME ## rv40_qpel ## SIZE ## _mc31_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## rv40_qpel ## SIZE ## _mc31_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[SIZE*(SIZE+5)];\
     uint8_t * const full_mid = full + SIZE*2;\
     put_rv40_qpel ## SIZE ## _h_lowpass(full, src - 2*stride, SIZE, stride, SIZE+5, 20, 52, 6);\
     OPNAME ## rv40_qpel ## SIZE ## _v_lowpass(dst, full_mid, stride, SIZE, SIZE, 52, 20, 6);\
 }\
 \
-static void OPNAME ## rv40_qpel ## SIZE ## _mc12_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## rv40_qpel ## SIZE ## _mc12_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[SIZE*(SIZE+5)];\
     uint8_t * const full_mid = full + SIZE*2;\
     put_rv40_qpel ## SIZE ## _h_lowpass(full, src - 2*stride, SIZE, stride, SIZE+5, 52, 20, 6);\
     OPNAME ## rv40_qpel ## SIZE ## _v_lowpass(dst, full_mid, stride, SIZE, SIZE, 20, 20, 5);\
 }\
 \
-static void OPNAME ## rv40_qpel ## SIZE ## _mc22_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## rv40_qpel ## SIZE ## _mc22_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[SIZE*(SIZE+5)];\
     uint8_t * const full_mid = full + SIZE*2;\
     put_rv40_qpel ## SIZE ## _h_lowpass(full, src - 2*stride, SIZE, stride, SIZE+5, 20, 20, 5);\
     OPNAME ## rv40_qpel ## SIZE ## _v_lowpass(dst, full_mid, stride, SIZE, SIZE, 20, 20, 5);\
 }\
 \
-static void OPNAME ## rv40_qpel ## SIZE ## _mc32_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## rv40_qpel ## SIZE ## _mc32_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[SIZE*(SIZE+5)];\
     uint8_t * const full_mid = full + SIZE*2;\
     put_rv40_qpel ## SIZE ## _h_lowpass(full, src - 2*stride, SIZE, stride, SIZE+5, 20, 52, 6);\
     OPNAME ## rv40_qpel ## SIZE ## _v_lowpass(dst, full_mid, stride, SIZE, SIZE, 20, 20, 5);\
 }\
 \
-static void OPNAME ## rv40_qpel ## SIZE ## _mc03_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## rv40_qpel ## SIZE ## _mc03_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## rv40_qpel ## SIZE ## _v_lowpass(dst, src, stride, stride, SIZE, 20, 52, 6);\
 }\
 \
-static void OPNAME ## rv40_qpel ## SIZE ## _mc13_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## rv40_qpel ## SIZE ## _mc13_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[SIZE*(SIZE+5)];\
     uint8_t * const full_mid = full + SIZE*2;\
     put_rv40_qpel ## SIZE ## _h_lowpass(full, src - 2*stride, SIZE, stride, SIZE+5, 52, 20, 6);\
     OPNAME ## rv40_qpel ## SIZE ## _v_lowpass(dst, full_mid, stride, SIZE, SIZE, 20, 52, 6);\
 }\
 \
-static void OPNAME ## rv40_qpel ## SIZE ## _mc23_c(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## rv40_qpel ## SIZE ## _mc23_c(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     uint8_t full[SIZE*(SIZE+5)];\
     uint8_t * const full_mid = full + SIZE*2;\
     put_rv40_qpel ## SIZE ## _h_lowpass(full, src - 2*stride, SIZE, stride, SIZE+5, 20, 20, 5);\
@@ -339,7 +351,7 @@
                                                    const int lim_q1,
                                                    const int lim_p1)
 {
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
     int i, t, u, diff;
 
     for (i = 0; i < 4; i++, src += stride) {
diff --git a/libavcodec/s302m.c b/libavcodec/s302m.c
index d641c2e..5f73148 100644
--- a/libavcodec/s302m.c
+++ b/libavcodec/s302m.c
@@ -106,10 +106,8 @@
     /* get output buffer */
     block_size = (avctx->bits_per_coded_sample + 4) / 4;
     frame->nb_samples = 2 * (buf_size / block_size) / avctx->channels;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     buf_size = (frame->nb_samples * avctx->channels / 2) * block_size;
 
diff --git a/libavcodec/samidec.c b/libavcodec/samidec.c
index c04b8a3..39ac608 100644
--- a/libavcodec/samidec.c
+++ b/libavcodec/samidec.c
@@ -52,7 +52,7 @@
         p = av_stristr(p, "<P");
         if (!p)
             break;
-        if (p[2] != '>' && !isspace(p[2])) { // avoid confusion with tags such as <PRE>
+        if (p[2] != '>' && !av_isspace(p[2])) { // avoid confusion with tags such as <PRE>
             p++;
             continue;
         }
@@ -70,7 +70,7 @@
         }
 
         /* if empty event -> skip subtitle */
-        while (isspace(*p))
+        while (av_isspace(*p))
             p++;
         if (!strncmp(p, "&nbsp;", 6)) {
             ret = -1;
@@ -80,7 +80,7 @@
         /* extract the text, stripping most of the tags */
         while (*p) {
             if (*p == '<') {
-                if (!av_strncasecmp(p, "<P", 2) && (p[2] == '>' || isspace(p[2])))
+                if (!av_strncasecmp(p, "<P", 2) && (p[2] == '>' || av_isspace(p[2])))
                     break;
                 if (!av_strncasecmp(p, "<BR", 3))
                     av_bprintf(dst, "\\N");
@@ -92,11 +92,11 @@
                 if (*p == '>')
                     p++;
             }
-            if (!isspace(*p))
+            if (!av_isspace(*p))
                 av_bprint_chars(dst, *p, 1);
             else if (!prev_chr_is_space)
                 av_bprint_chars(dst, ' ', 1);
-            prev_chr_is_space = isspace(*p);
+            prev_chr_is_space = av_isspace(*p);
             p++;
         }
     }
diff --git a/libavcodec/sanm.c b/libavcodec/sanm.c
index caeaa36..f217ef3 100644
--- a/libavcodec/sanm.c
+++ b/libavcodec/sanm.c
@@ -46,7 +46,7 @@
     int aligned_width, aligned_height;
     int prev_seq;
 
-    AVFrame frame, *output;
+    AVFrame *frame;
     uint16_t *frm0, *frm1, *frm2;
     uint8_t *stored_frame;
     uint32_t frm0_size, frm1_size, frm2_size;
@@ -274,8 +274,6 @@
         av_log(avctx, AV_LOG_ERROR, "error allocating buffers\n");
         return AVERROR(ENOMEM);
     }
-    ctx->output          = &ctx->frame;
-    ctx->output->data[0] = 0;
 
     make_glyphs(ctx->p4x4glyphs[0], glyph4_x, glyph4_y, 4);
     make_glyphs(ctx->p8x8glyphs[0], glyph8_x, glyph8_y, 8);
@@ -302,11 +300,6 @@
 
     destroy_buffers(ctx);
 
-    if (ctx->frame.data[0]) {
-        avctx->release_buffer(avctx, &ctx->frame);
-        ctx->frame.data[0] = 0;
-    }
-
     return 0;
 }
 
@@ -767,7 +760,7 @@
         return old_codec47(ctx, top, left, w, h);
         break;
     default:
-        av_log_ask_for_sample(ctx->avctx, "unknown subcodec %d\n", codec);
+        avpriv_request_sample(ctx->avctx, "unknown subcodec %d", codec);
         return AVERROR_PATCHWELCOME;
     }
 }
@@ -791,7 +784,7 @@
 
 static int decode_nop(SANMVideoContext *ctx)
 {
-    av_log_ask_for_sample(ctx->avctx, "unknown/unsupported compression type\n");
+    avpriv_request_sample(ctx->avctx, "unknown/unsupported compression type");
     return AVERROR_PATCHWELCOME;
 }
 
@@ -1151,13 +1144,11 @@
     int ret, dstpitch, height = ctx->height;
     int srcpitch = ctx->pitch * (hdr ? sizeof(ctx->frm0[0]) : 1);
 
-    if ((ret = ff_get_buffer(ctx->avctx, ctx->output)) < 0) {
-        av_log(ctx->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(ctx->avctx, ctx->frame, 0)) < 0)
         return ret;
-    }
 
-    dst      = ctx->output->data[0];
-    dstpitch = ctx->output->linesize[0];
+    dst      = ctx->frame->data[0];
+    dstpitch = ctx->frame->linesize[0];
 
     while (height--) {
         memcpy(dst, src, srcpitch);
@@ -1174,9 +1165,8 @@
     SANMVideoContext *ctx = avctx->priv_data;
     int i, ret;
 
+    ctx->frame = data;
     bytestream2_init(&ctx->gb, pkt->data, pkt->size);
-    if (ctx->output->data[0])
-        avctx->release_buffer(avctx, ctx->output);
 
     if (!ctx->version) {
         int to_store = 0;
@@ -1258,7 +1248,7 @@
             memcpy(ctx->stored_frame, ctx->frm0, ctx->buf_size);
         if ((ret = copy_output(ctx, NULL)))
             return ret;
-        memcpy(ctx->output->data[1], ctx->pal, 1024);
+        memcpy(ctx->frame->data[1], ctx->pal, 1024);
     } else {
         SANMFrameHeader header;
 
@@ -1266,12 +1256,12 @@
             return ret;
 
         ctx->rotate_code = header.rotate_code;
-        if ((ctx->output->key_frame = !header.seq_num)) {
-            ctx->output->pict_type = AV_PICTURE_TYPE_I;
+        if ((ctx->frame->key_frame = !header.seq_num)) {
+            ctx->frame->pict_type = AV_PICTURE_TYPE_I;
             fill_frame(ctx->frm1, ctx->npixels, header.bg_color);
             fill_frame(ctx->frm2, ctx->npixels, header.bg_color);
         } else {
-            ctx->output->pict_type = AV_PICTURE_TYPE_P;
+            ctx->frame->pict_type = AV_PICTURE_TYPE_P;
         }
 
         if (header.codec < FF_ARRAY_ELEMS(v1_decoders)) {
@@ -1281,7 +1271,7 @@
                 return ret;
             }
         } else {
-            av_log_ask_for_sample(avctx, "subcodec %d is not implemented\n",
+            avpriv_request_sample(avctx, "subcodec %d",
                    header.codec);
             return AVERROR_PATCHWELCOME;
         }
@@ -1293,7 +1283,6 @@
         rotate_bufs(ctx, ctx->rotate_code);
 
     *got_frame_ptr  = 1;
-    *(AVFrame*)data = *ctx->output;
 
     return pkt->size;
 }
diff --git a/libavcodec/sbrdsp.c b/libavcodec/sbrdsp.c
index 6fede79..7207f3b 100644
--- a/libavcodec/sbrdsp.c
+++ b/libavcodec/sbrdsp.c
@@ -22,6 +22,7 @@
 
 #include "config.h"
 #include "libavutil/attributes.h"
+#include "libavutil/intfloat.h"
 #include "sbrdsp.h"
 
 static void sbr_sum64x5_c(float *z)
@@ -51,37 +52,52 @@
 
 static void sbr_neg_odd_64_c(float *x)
 {
+    union av_intfloat32 *xi = (union av_intfloat32*)x;
     int i;
-    for (i = 1; i < 64; i += 2)
-        x[i] = -x[i];
+    for (i = 1; i < 64; i += 4)
+    {
+        xi[i+0].i ^= 1U<<31;
+        xi[i+2].i ^= 1U<<31;
+    }
 }
 
 static void sbr_qmf_pre_shuffle_c(float *z)
 {
+    union av_intfloat32 *zi = (union av_intfloat32*)z;
     int k;
-    z[64] = z[0];
-    z[65] = z[1];
-    for (k = 1; k < 32; k++) {
-        z[64+2*k  ] = -z[64 - k];
-        z[64+2*k+1] =  z[ k + 1];
+    zi[64].i = zi[0].i;
+    zi[65].i = zi[1].i;
+    for (k = 1; k < 31; k+=2) {
+        zi[64+2*k+0].i = zi[64 - k].i ^ (1U<<31);
+        zi[64+2*k+1].i = zi[ k + 1].i;
+        zi[64+2*k+2].i = zi[63 - k].i ^ (1U<<31);
+        zi[64+2*k+3].i = zi[ k + 2].i;
     }
+    zi[64+2*31+0].i = zi[64 - 31].i ^ (1U<<31);
+    zi[64+2*31+1].i = zi[31 + 1].i;
 }
 
 static void sbr_qmf_post_shuffle_c(float W[32][2], const float *z)
 {
+    const union av_intfloat32 *zi = (const union av_intfloat32*)z;
+    union av_intfloat32 *Wi = (union av_intfloat32*)W;
     int k;
-    for (k = 0; k < 32; k++) {
-        W[k][0] = -z[63-k];
-        W[k][1] = z[k];
+    for (k = 0; k < 32; k+=2) {
+        Wi[2*k+0].i = zi[63-k].i ^ (1U<<31);
+        Wi[2*k+1].i = zi[k+0].i;
+        Wi[2*k+2].i = zi[62-k].i ^ (1U<<31);
+        Wi[2*k+3].i = zi[k+1].i;
     }
 }
 
 static void sbr_qmf_deint_neg_c(float *v, const float *src)
 {
+    const union av_intfloat32 *si = (const union av_intfloat32*)src;
+    union av_intfloat32 *vi = (union av_intfloat32*)v;
     int i;
     for (i = 0; i < 32; i++) {
-        v[     i] =  src[63 - 2*i    ];
-        v[63 - i] = -src[63 - 2*i - 1];
+        vi[     i].i = si[63 - 2*i    ].i;
+        vi[63 - i].i = si[63 - 2*i - 1].i ^ (1U<<31);
     }
 }
 
@@ -122,9 +138,34 @@
 
 static void sbr_autocorrelate_c(const float x[40][2], float phi[3][2][2])
 {
+#if 0
+    // This code is slower because it multiplies memory accesses.
+    // It is left as eucational purpose and because it may offer
+    // a better reference for writing arch-specific dsp functions.
     autocorrelate(x, phi, 0);
     autocorrelate(x, phi, 1);
     autocorrelate(x, phi, 2);
+#else
+    float real_sum2 = x[ 0][0] * x[ 2][0] + x[ 0][1] * x[ 2][1];
+    float imag_sum2 = x[ 0][0] * x[ 2][1] - x[ 0][1] * x[ 2][0];
+    float real_sum1 = 0.f, imag_sum1 = 0.f, real_sum0 = 0.0f;
+    int   i;
+    for (i = 1; i < 38; i++) {
+        real_sum0 += x[i][0] * x[i  ][0] + x[i][1] * x[i  ][1];
+        real_sum1 += x[i][0] * x[i+1][0] + x[i][1] * x[i+1][1];
+        imag_sum1 += x[i][0] * x[i+1][1] - x[i][1] * x[i+1][0];
+        real_sum2 += x[i][0] * x[i+2][0] + x[i][1] * x[i+2][1];
+        imag_sum2 += x[i][0] * x[i+2][1] - x[i][1] * x[i+2][0];
+    }
+    phi[2-2][1][0] = real_sum2;
+    phi[2-2][1][1] = imag_sum2;
+    phi[2  ][1][0] = real_sum0 + x[ 0][0] * x[ 0][0] + x[ 0][1] * x[ 0][1];
+    phi[1  ][0][0] = real_sum0 + x[38][0] * x[38][0] + x[38][1] * x[38][1];
+    phi[2-1][1][0] = real_sum1 + x[ 0][0] * x[ 1][0] + x[ 0][1] * x[ 1][1];
+    phi[2-1][1][1] = imag_sum1 + x[ 0][0] * x[ 1][1] - x[ 0][1] * x[ 1][0];
+    phi[0  ][0][0] = real_sum1 + x[38][0] * x[39][0] + x[38][1] * x[39][1];
+    phi[0  ][0][1] = imag_sum1 + x[38][0] * x[39][1] - x[38][1] * x[39][0];
+#endif
 }
 
 static void sbr_hf_gen_c(float (*X_high)[2], const float (*X_low)[2],
diff --git a/libavcodec/sgidec.c b/libavcodec/sgidec.c
index 51bd5d6..e7f453b 100644
--- a/libavcodec/sgidec.c
+++ b/libavcodec/sgidec.c
@@ -27,7 +27,6 @@
 #include "sgi.h"
 
 typedef struct SgiState {
-    AVFrame picture;
     unsigned int width;
     unsigned int height;
     unsigned int depth;
@@ -156,8 +155,7 @@
                         AVPacket *avpkt)
 {
     SgiState *s = avctx->priv_data;
-    AVFrame *picture = data;
-    AVFrame *p = &s->picture;
+    AVFrame *p = data;
     unsigned int dimension, rle;
     int ret = 0;
     uint8_t *out_buf, *out_end;
@@ -207,14 +205,8 @@
         return -1;
     avcodec_set_dimensions(avctx, s->width, s->height);
 
-    if (p->data[0])
-        avctx->release_buffer(avctx, p);
-
-    p->reference = 0;
-    if (ff_get_buffer(avctx, p) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed.\n");
-        return -1;
-    }
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
+        return ret;
 
     p->pict_type = AV_PICTURE_TYPE_I;
     p->key_frame = 1;
@@ -233,7 +225,6 @@
     }
 
     if (ret == 0) {
-        *picture   = s->picture;
         *got_frame = 1;
         return avpkt->size;
     } else {
@@ -241,32 +232,11 @@
     }
 }
 
-static av_cold int sgi_init(AVCodecContext *avctx){
-    SgiState *s = avctx->priv_data;
-
-    avcodec_get_frame_defaults(&s->picture);
-    avctx->coded_frame = &s->picture;
-
-    return 0;
-}
-
-static av_cold int sgi_end(AVCodecContext *avctx)
-{
-    SgiState * const s = avctx->priv_data;
-
-    if (s->picture.data[0])
-        avctx->release_buffer(avctx, &s->picture);
-
-    return 0;
-}
-
 AVCodec ff_sgi_decoder = {
     .name           = "sgi",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_SGI,
     .priv_data_size = sizeof(SgiState),
-    .init           = sgi_init,
-    .close          = sgi_end,
     .decode         = decode_frame,
     .long_name      = NULL_IF_CONFIG_SMALL("SGI image"),
     .capabilities   = CODEC_CAP_DR1,
diff --git a/libavcodec/sgirledec.c b/libavcodec/sgirledec.c
index 0ce6afd..a7fdac8 100644
--- a/libavcodec/sgirledec.c
+++ b/libavcodec/sgirledec.c
@@ -24,15 +24,19 @@
  * SGI RLE 8-bit decoder
  */
 
-#include "libavutil/intreadwrite.h"
 #include "avcodec.h"
-#include "bytestream.h"
+#include "internal.h"
+
+typedef struct SGIRLEContext {
+    AVFrame *frame;
+} SGIRLEContext;
 
 static av_cold int sgirle_decode_init(AVCodecContext *avctx)
 {
+    SGIRLEContext *s = avctx->priv_data;
     avctx->pix_fmt = AV_PIX_FMT_BGR8;
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame)
+    s->frame = av_frame_alloc();
+    if (!s->frame)
         return AVERROR(ENOMEM);
     return 0;
 }
@@ -95,7 +99,7 @@
                 v   -= length;
             } while (v > 0);
         } else {
-            av_log_ask_for_sample(avctx, "unknown opcode\n");
+            avpriv_request_sample(avctx, "opcode %d", v);
             return AVERROR_PATCHWELCOME;
         }
     }
@@ -106,33 +110,29 @@
                             void *data, int *got_frame,
                             AVPacket *avpkt)
 {
-    AVFrame *frame = avctx->coded_frame;
+    SGIRLEContext *s = avctx->priv_data;
     int ret;
 
-    frame->reference = 3;
-    frame->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
-                          FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
-    ret = avctx->reget_buffer(avctx, frame);
-    if (ret < 0) {
-        av_log (avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, s->frame)) < 0)
         return ret;
-    }
 
-    ret = decode_sgirle8(avctx, frame->data[0], avpkt->data, avpkt->size, avctx->width, avctx->height, frame->linesize[0]);
+    ret = decode_sgirle8(avctx, s->frame->data[0], avpkt->data, avpkt->size, avctx->width, avctx->height, s->frame->linesize[0]);
     if (ret < 0)
         return ret;
 
     *got_frame      = 1;
-    *(AVFrame*)data = *frame;
+    if ((ret = av_frame_ref(data, s->frame)) < 0)
+        return ret;
 
     return avpkt->size;
 }
 
 static av_cold int sgirle_decode_end(AVCodecContext *avctx)
 {
-    if (avctx->coded_frame->data[0])
-        avctx->release_buffer(avctx, avctx->coded_frame);
-    av_freep(&avctx->coded_frame);
+    SGIRLEContext *s = avctx->priv_data;
+
+    av_frame_free(&s->frame);
+
     return 0;
 }
 
@@ -140,6 +140,7 @@
     .name           = "sgirle",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_SGIRLE,
+    .priv_data_size = sizeof(SGIRLEContext),
     .init           = sgirle_decode_init,
     .close          = sgirle_decode_end,
     .decode         = sgirle_decode_frame,
diff --git a/libavcodec/sh4/Makefile b/libavcodec/sh4/Makefile
index f907408..01a573b 100644
--- a/libavcodec/sh4/Makefile
+++ b/libavcodec/sh4/Makefile
@@ -1,5 +1,2 @@
-OBJS += sh4/dsputil_align.o                                             \
-        sh4/dsputil_sh4.o                                               \
+OBJS += sh4/dsputil_sh4.o                                               \
         sh4/idct_sh4.o                                                  \
-
-OBJS-$(CONFIG_H264CHROMA)               += sh4/h264chroma_init.o        \
diff --git a/libavcodec/sh4/dsputil_align.c b/libavcodec/sh4/dsputil_align.c
deleted file mode 100644
index 46981f6..0000000
--- a/libavcodec/sh4/dsputil_align.c
+++ /dev/null
@@ -1,386 +0,0 @@
-/*
- * aligned/packed access motion
- *
- * Copyright (c) 2001-2003 BERO <bero@geocities.co.jp>
- *
- * 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 "libavutil/attributes.h"
-#include "libavcodec/avcodec.h"
-#include "libavcodec/dsputil.h"
-#include "libavcodec/rnd_avg.h"
-#include "dsputil_sh4.h"
-
-
-#define         LP(p)           *(uint32_t*)(p)
-#define         LPC(p)          *(const uint32_t*)(p)
-
-
-#define         UNPACK(ph,pl,tt0,tt1) do { \
-        uint32_t t0,t1; t0=tt0;t1=tt1; \
-        ph = ( (t0 & ~BYTE_VEC32(0x03))>>2) + ( (t1 & ~BYTE_VEC32(0x03))>>2); \
-        pl = (t0 & BYTE_VEC32(0x03)) + (t1 & BYTE_VEC32(0x03)); } while(0)
-
-#define         rnd_PACK(ph,pl,nph,npl) ph + nph + (((pl + npl + BYTE_VEC32(0x02))>>2) & BYTE_VEC32(0x03))
-#define         no_rnd_PACK(ph,pl,nph,npl)      ph + nph + (((pl + npl + BYTE_VEC32(0x01))>>2) & BYTE_VEC32(0x03))
-
-/* little-endian */
-#define         MERGE1(a,b,ofs) (ofs==0)?a:( ((a)>>(8*ofs))|((b)<<(32-8*ofs)) )
-#define         MERGE2(a,b,ofs) (ofs==3)?b:( ((a)>>(8*(ofs+1)))|((b)<<(32-8*(ofs+1))) )
-/* big
-#define         MERGE1(a,b,ofs) (ofs==0)?a:( ((a)<<(8*ofs))|((b)>>(32-8*ofs)) )
-#define         MERGE2(a,b,ofs) (ofs==3)?b:( ((a)<<(8+8*ofs))|((b)>>(32-8-8*ofs)) )
-*/
-
-
-#define         put(d,s)        d = s
-#define         avg(d,s)        d = rnd_avg32(s,d)
-
-#define         OP_C4(ofs) \
-        ref-=ofs; \
-        do { \
-                OP(LP(dest),MERGE1(LPC(ref),LPC(ref+4),ofs)); \
-                ref+=stride; \
-                dest+=stride; \
-        } while(--height)
-
-#define        OP_C40() \
-        do { \
-                OP(LP(dest),LPC(ref)); \
-                ref+=stride; \
-                dest+=stride; \
-        } while(--height)
-
-#define         OP_C(ofs,sz,avg2) \
-{ \
-        ref-=ofs; \
-        do { \
-                uint32_t        t0,t1; \
-                t0 = LPC(ref+0); \
-                t1 = LPC(ref+4); \
-                OP(LP(dest+0), MERGE1(t0,t1,ofs)); \
-                t0 = LPC(ref+8); \
-                OP(LP(dest+4), MERGE1(t1,t0,ofs)); \
-if (sz==16) { \
-                t1 = LPC(ref+12); \
-                OP(LP(dest+8), MERGE1(t0,t1,ofs)); \
-                t0 = LPC(ref+16); \
-                OP(LP(dest+12), MERGE1(t1,t0,ofs)); \
-} \
-                ref+=stride; \
-                dest+= stride; \
-        } while(--height); \
-}
-
-/* aligned */
-#define         OP_C0(sz,avg2) \
-{ \
-        do { \
-                OP(LP(dest+0), LPC(ref+0)); \
-                OP(LP(dest+4), LPC(ref+4)); \
-if (sz==16) { \
-                OP(LP(dest+8), LPC(ref+8)); \
-                OP(LP(dest+12), LPC(ref+12)); \
-} \
-                ref+=stride; \
-                dest+= stride; \
-        } while(--height); \
-}
-
-#define         OP_X(ofs,sz,avg2) \
-{ \
-        ref-=ofs; \
-        do { \
-                uint32_t        t0,t1; \
-                t0 = LPC(ref+0); \
-                t1 = LPC(ref+4); \
-                OP(LP(dest+0), avg2(MERGE1(t0,t1,ofs),MERGE2(t0,t1,ofs))); \
-                t0 = LPC(ref+8); \
-                OP(LP(dest+4), avg2(MERGE1(t1,t0,ofs),MERGE2(t1,t0,ofs))); \
-if (sz==16) { \
-                t1 = LPC(ref+12); \
-                OP(LP(dest+8), avg2(MERGE1(t0,t1,ofs),MERGE2(t0,t1,ofs))); \
-                t0 = LPC(ref+16); \
-                OP(LP(dest+12), avg2(MERGE1(t1,t0,ofs),MERGE2(t1,t0,ofs))); \
-} \
-                ref+=stride; \
-                dest+= stride; \
-        } while(--height); \
-}
-
-/* aligned */
-#define         OP_Y0(sz,avg2) \
-{ \
-        uint32_t t0,t1,t2,t3,t; \
-\
-        t0 = LPC(ref+0); \
-        t1 = LPC(ref+4); \
-if (sz==16) { \
-        t2 = LPC(ref+8); \
-        t3 = LPC(ref+12); \
-} \
-        do { \
-                ref += stride; \
-\
-                t = LPC(ref+0); \
-                OP(LP(dest+0), avg2(t0,t)); t0 = t; \
-                t = LPC(ref+4); \
-                OP(LP(dest+4), avg2(t1,t)); t1 = t; \
-if (sz==16) { \
-                t = LPC(ref+8); \
-                OP(LP(dest+8), avg2(t2,t)); t2 = t; \
-                t = LPC(ref+12); \
-                OP(LP(dest+12), avg2(t3,t)); t3 = t; \
-} \
-                dest+= stride; \
-        } while(--height); \
-}
-
-#define         OP_Y(ofs,sz,avg2) \
-{ \
-        uint32_t t0,t1,t2,t3,t,w0,w1; \
-\
-        ref-=ofs; \
-        w0 = LPC(ref+0); \
-        w1 = LPC(ref+4); \
-        t0 = MERGE1(w0,w1,ofs); \
-        w0 = LPC(ref+8); \
-        t1 = MERGE1(w1,w0,ofs); \
-if (sz==16) { \
-        w1 = LPC(ref+12); \
-        t2 = MERGE1(w0,w1,ofs); \
-        w0 = LPC(ref+16); \
-        t3 = MERGE1(w1,w0,ofs); \
-} \
-        do { \
-                ref += stride; \
-\
-                w0 = LPC(ref+0); \
-                w1 = LPC(ref+4); \
-                t = MERGE1(w0,w1,ofs); \
-                OP(LP(dest+0), avg2(t0,t)); t0 = t; \
-                w0 = LPC(ref+8); \
-                t = MERGE1(w1,w0,ofs); \
-                OP(LP(dest+4), avg2(t1,t)); t1 = t; \
-if (sz==16) { \
-                w1 = LPC(ref+12); \
-                t = MERGE1(w0,w1,ofs); \
-                OP(LP(dest+8), avg2(t2,t)); t2 = t; \
-                w0 = LPC(ref+16); \
-                t = MERGE1(w1,w0,ofs); \
-                OP(LP(dest+12), avg2(t3,t)); t3 = t; \
-} \
-                dest+=stride; \
-        } while(--height); \
-}
-
-#define OP_X0(sz,avg2) OP_X(0,sz,avg2)
-#define OP_XY0(sz,PACK) OP_XY(0,sz,PACK)
-#define         OP_XY(ofs,sz,PACK) \
-{ \
-        uint32_t        t2,t3,w0,w1; \
-        uint32_t        a0,a1,a2,a3,a4,a5,a6,a7; \
-\
-        ref -= ofs; \
-        w0 = LPC(ref+0); \
-        w1 = LPC(ref+4); \
-        UNPACK(a0,a1,MERGE1(w0,w1,ofs),MERGE2(w0,w1,ofs)); \
-        w0 = LPC(ref+8); \
-        UNPACK(a2,a3,MERGE1(w1,w0,ofs),MERGE2(w1,w0,ofs)); \
-if (sz==16) { \
-        w1 = LPC(ref+12); \
-        UNPACK(a4,a5,MERGE1(w0,w1,ofs),MERGE2(w0,w1,ofs)); \
-        w0 = LPC(ref+16); \
-        UNPACK(a6,a7,MERGE1(w1,w0,ofs),MERGE2(w1,w0,ofs)); \
-} \
-        do { \
-                ref+=stride; \
-                w0 = LPC(ref+0); \
-                w1 = LPC(ref+4); \
-                UNPACK(t2,t3,MERGE1(w0,w1,ofs),MERGE2(w0,w1,ofs)); \
-                OP(LP(dest+0),PACK(a0,a1,t2,t3)); \
-                a0 = t2; a1 = t3; \
-                w0 = LPC(ref+8); \
-                UNPACK(t2,t3,MERGE1(w1,w0,ofs),MERGE2(w1,w0,ofs)); \
-                OP(LP(dest+4),PACK(a2,a3,t2,t3)); \
-                a2 = t2; a3 = t3; \
-if (sz==16) { \
-                w1 = LPC(ref+12); \
-                UNPACK(t2,t3,MERGE1(w0,w1,ofs),MERGE2(w0,w1,ofs)); \
-                OP(LP(dest+8),PACK(a4,a5,t2,t3)); \
-                a4 = t2; a5 = t3; \
-                w0 = LPC(ref+16); \
-                UNPACK(t2,t3,MERGE1(w1,w0,ofs),MERGE2(w1,w0,ofs)); \
-                OP(LP(dest+12),PACK(a6,a7,t2,t3)); \
-                a6 = t2; a7 = t3; \
-} \
-                dest+=stride; \
-        } while(--height); \
-}
-
-#define         DEFFUNC(op,rnd,xy,sz,OP_N,avgfunc) \
-static void op##_##rnd##_pixels##sz##_##xy (uint8_t * dest, const uint8_t * ref, \
-                                const ptrdiff_t stride, int height) \
-{ \
-        switch((int)ref&3) { \
-        case 0:OP_N##0(sz,rnd##_##avgfunc); return; \
-        case 1:OP_N(1,sz,rnd##_##avgfunc); return; \
-        case 2:OP_N(2,sz,rnd##_##avgfunc); return; \
-        case 3:OP_N(3,sz,rnd##_##avgfunc); return; \
-        } \
-}
-
-#define OP put
-
-DEFFUNC(put,   rnd,o,8,OP_C,avg32)
-DEFFUNC(put,   rnd,x,8,OP_X,avg32)
-DEFFUNC(put,no_rnd,x,8,OP_X,avg32)
-DEFFUNC(put,   rnd,y,8,OP_Y,avg32)
-DEFFUNC(put,no_rnd,y,8,OP_Y,avg32)
-DEFFUNC(put,   rnd,xy,8,OP_XY,PACK)
-DEFFUNC(put,no_rnd,xy,8,OP_XY,PACK)
-DEFFUNC(put,   rnd,o,16,OP_C,avg32)
-DEFFUNC(put,   rnd,x,16,OP_X,avg32)
-DEFFUNC(put,no_rnd,x,16,OP_X,avg32)
-DEFFUNC(put,   rnd,y,16,OP_Y,avg32)
-DEFFUNC(put,no_rnd,y,16,OP_Y,avg32)
-DEFFUNC(put,   rnd,xy,16,OP_XY,PACK)
-DEFFUNC(put,no_rnd,xy,16,OP_XY,PACK)
-
-#undef OP
-#define OP avg
-
-DEFFUNC(avg,   rnd,o,8,OP_C,avg32)
-DEFFUNC(avg,   rnd,x,8,OP_X,avg32)
-DEFFUNC(avg,   rnd,y,8,OP_Y,avg32)
-DEFFUNC(avg,   rnd,xy,8,OP_XY,PACK)
-DEFFUNC(avg,   rnd,o,16,OP_C,avg32)
-DEFFUNC(avg,   rnd,x,16,OP_X,avg32)
-DEFFUNC(avg,no_rnd,x,16,OP_X,avg32)
-DEFFUNC(avg,   rnd,y,16,OP_Y,avg32)
-DEFFUNC(avg,no_rnd,y,16,OP_Y,avg32)
-DEFFUNC(avg,   rnd,xy,16,OP_XY,PACK)
-DEFFUNC(avg,no_rnd,xy,16,OP_XY,PACK)
-
-#undef OP
-
-#define         put_no_rnd_pixels8_o     put_rnd_pixels8_o
-#define         put_no_rnd_pixels16_o    put_rnd_pixels16_o
-#define         avg_no_rnd_pixels16_o    avg_rnd_pixels16_o
-
-#define         put_pixels8_c            put_rnd_pixels8_o
-#define         put_pixels16_c           put_rnd_pixels16_o
-#define         avg_pixels8_c            avg_rnd_pixels8_o
-#define         avg_pixels16_c           avg_rnd_pixels16_o
-#define         put_no_rnd_pixels8_c     put_rnd_pixels8_o
-#define         put_no_rnd_pixels16_c    put_rnd_pixels16_o
-#define         avg_no_rnd_pixels16_c    avg_rnd_pixels16_o
-
-#define         QPEL
-
-#ifdef QPEL
-
-#include "qpel.c"
-
-#endif
-
-av_cold void ff_dsputil_init_align(DSPContext *c, AVCodecContext *avctx)
-{
-        const int high_bit_depth = avctx->bits_per_raw_sample > 8;
-
-        if (!high_bit_depth) {
-        c->put_pixels_tab[0][0] = put_rnd_pixels16_o;
-        c->put_pixels_tab[0][1] = put_rnd_pixels16_x;
-        c->put_pixels_tab[0][2] = put_rnd_pixels16_y;
-        c->put_pixels_tab[0][3] = put_rnd_pixels16_xy;
-        c->put_pixels_tab[1][0] = put_rnd_pixels8_o;
-        c->put_pixels_tab[1][1] = put_rnd_pixels8_x;
-        c->put_pixels_tab[1][2] = put_rnd_pixels8_y;
-        c->put_pixels_tab[1][3] = put_rnd_pixels8_xy;
-
-        c->put_no_rnd_pixels_tab[0][0] = put_no_rnd_pixels16_o;
-        c->put_no_rnd_pixels_tab[0][1] = put_no_rnd_pixels16_x;
-        c->put_no_rnd_pixels_tab[0][2] = put_no_rnd_pixels16_y;
-        c->put_no_rnd_pixels_tab[0][3] = put_no_rnd_pixels16_xy;
-        c->put_no_rnd_pixels_tab[1][0] = put_no_rnd_pixels8_o;
-        c->put_no_rnd_pixels_tab[1][1] = put_no_rnd_pixels8_x;
-        c->put_no_rnd_pixels_tab[1][2] = put_no_rnd_pixels8_y;
-        c->put_no_rnd_pixels_tab[1][3] = put_no_rnd_pixels8_xy;
-
-        c->avg_pixels_tab[0][0] = avg_rnd_pixels16_o;
-        c->avg_pixels_tab[0][1] = avg_rnd_pixels16_x;
-        c->avg_pixels_tab[0][2] = avg_rnd_pixels16_y;
-        c->avg_pixels_tab[0][3] = avg_rnd_pixels16_xy;
-        c->avg_pixels_tab[1][0] = avg_rnd_pixels8_o;
-        c->avg_pixels_tab[1][1] = avg_rnd_pixels8_x;
-        c->avg_pixels_tab[1][2] = avg_rnd_pixels8_y;
-        c->avg_pixels_tab[1][3] = avg_rnd_pixels8_xy;
-
-        c->avg_no_rnd_pixels_tab[0] = avg_no_rnd_pixels16_o;
-        c->avg_no_rnd_pixels_tab[1] = avg_no_rnd_pixels16_x;
-        c->avg_no_rnd_pixels_tab[2] = avg_no_rnd_pixels16_y;
-        c->avg_no_rnd_pixels_tab[3] = avg_no_rnd_pixels16_xy;
-        }
-
-#ifdef QPEL
-
-#define dspfunc(PFX, IDX, NUM) \
-    c->PFX ## _pixels_tab[IDX][ 0] = PFX ## NUM ## _mc00_sh4; \
-    c->PFX ## _pixels_tab[IDX][ 1] = PFX ## NUM ## _mc10_sh4; \
-    c->PFX ## _pixels_tab[IDX][ 2] = PFX ## NUM ## _mc20_sh4; \
-    c->PFX ## _pixels_tab[IDX][ 3] = PFX ## NUM ## _mc30_sh4; \
-    c->PFX ## _pixels_tab[IDX][ 4] = PFX ## NUM ## _mc01_sh4; \
-    c->PFX ## _pixels_tab[IDX][ 5] = PFX ## NUM ## _mc11_sh4; \
-    c->PFX ## _pixels_tab[IDX][ 6] = PFX ## NUM ## _mc21_sh4; \
-    c->PFX ## _pixels_tab[IDX][ 7] = PFX ## NUM ## _mc31_sh4; \
-    c->PFX ## _pixels_tab[IDX][ 8] = PFX ## NUM ## _mc02_sh4; \
-    c->PFX ## _pixels_tab[IDX][ 9] = PFX ## NUM ## _mc12_sh4; \
-    c->PFX ## _pixels_tab[IDX][10] = PFX ## NUM ## _mc22_sh4; \
-    c->PFX ## _pixels_tab[IDX][11] = PFX ## NUM ## _mc32_sh4; \
-    c->PFX ## _pixels_tab[IDX][12] = PFX ## NUM ## _mc03_sh4; \
-    c->PFX ## _pixels_tab[IDX][13] = PFX ## NUM ## _mc13_sh4; \
-    c->PFX ## _pixels_tab[IDX][14] = PFX ## NUM ## _mc23_sh4; \
-    c->PFX ## _pixels_tab[IDX][15] = PFX ## NUM ## _mc33_sh4
-
-    dspfunc(put_qpel, 0, 16);
-    dspfunc(put_no_rnd_qpel, 0, 16);
-
-    dspfunc(avg_qpel, 0, 16);
-    /* dspfunc(avg_no_rnd_qpel, 0, 16); */
-
-    dspfunc(put_qpel, 1, 8);
-    dspfunc(put_no_rnd_qpel, 1, 8);
-
-    dspfunc(avg_qpel, 1, 8);
-    /* dspfunc(avg_no_rnd_qpel, 1, 8); */
-
-#undef dspfunc
-
-    c->put_mspel_pixels_tab[0]= put_mspel8_mc00_sh4;
-    c->put_mspel_pixels_tab[1]= put_mspel8_mc10_sh4;
-    c->put_mspel_pixels_tab[2]= put_mspel8_mc20_sh4;
-    c->put_mspel_pixels_tab[3]= put_mspel8_mc30_sh4;
-    c->put_mspel_pixels_tab[4]= put_mspel8_mc02_sh4;
-    c->put_mspel_pixels_tab[5]= put_mspel8_mc12_sh4;
-    c->put_mspel_pixels_tab[6]= put_mspel8_mc22_sh4;
-    c->put_mspel_pixels_tab[7]= put_mspel8_mc32_sh4;
-
-    c->gmc1 = gmc1_c;
-
-#endif
-}
diff --git a/libavcodec/sh4/dsputil_sh4.c b/libavcodec/sh4/dsputil_sh4.c
index 6e19fa0..82b75ae 100644
--- a/libavcodec/sh4/dsputil_sh4.c
+++ b/libavcodec/sh4/dsputil_sh4.c
@@ -56,7 +56,7 @@
 static void idct_put(uint8_t *dest, int line_size, int16_t *block)
 {
         int i;
-        uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
+        const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
         ff_idct_sh4(block);
         for(i=0;i<8;i++) {
                 dest[0] = cm[block[0]];
@@ -74,7 +74,7 @@
 static void idct_add(uint8_t *dest, int line_size, int16_t *block)
 {
         int i;
-        uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
+        const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
         ff_idct_sh4(block);
         for(i=0;i<8;i++) {
                 dest[0] = cm[dest[0]+block[0]];
@@ -94,7 +94,6 @@
 {
         const int idct_algo= avctx->idct_algo;
         const int high_bit_depth = avctx->bits_per_raw_sample > 8;
-        ff_dsputil_init_align(c,avctx);
 
         if (!high_bit_depth)
         c->clear_blocks = clear_blocks_sh4;
diff --git a/libavcodec/sh4/dsputil_sh4.h b/libavcodec/sh4/dsputil_sh4.h
index 1c041ae..2ba9354 100644
--- a/libavcodec/sh4/dsputil_sh4.h
+++ b/libavcodec/sh4/dsputil_sh4.h
@@ -21,8 +21,8 @@
 
 #include "libavcodec/avcodec.h"
 #include "libavcodec/dsputil.h"
+#include "libavcodec/hpeldsp.h"
 
 void ff_idct_sh4(int16_t *block);
-void ff_dsputil_init_align(DSPContext* c, AVCodecContext *avctx);
 
 #endif /* AVCODEC_SH4_DSPUTIL_SH4_H */
diff --git a/libavcodec/sh4/h264chroma_init.c b/libavcodec/sh4/h264chroma_init.c
deleted file mode 100644
index d15f0ae..0000000
--- a/libavcodec/sh4/h264chroma_init.c
+++ /dev/null
@@ -1,132 +0,0 @@
-/*
- * aligned/packed access motion
- *
- * Copyright (c) 2001-2003 BERO <bero@geocities.co.jp>
- *
- * 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 <assert.h>
-#include <stdint.h>
-
-#include "libavutil/attributes.h"
-#include "libavcodec/h264chroma.h"
-
-#define H264_CHROMA_MC(OPNAME, OP)\
-static void OPNAME ## h264_chroma_mc2_sh4(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int x, int y){\
-    const int A=(8-x)*(8-y);\
-    const int B=(  x)*(8-y);\
-    const int C=(8-x)*(  y);\
-    const int D=(  x)*(  y);\
-    \
-    assert(x<8 && y<8 && x>=0 && y>=0);\
-\
-    do {\
-        int t0,t1,t2,t3; \
-        uint8_t *s0 = src; \
-        uint8_t *s1 = src+stride; \
-        t0 = *s0++; t2 = *s1++; \
-        t1 = *s0++; t3 = *s1++; \
-        OP(dst[0], (A*t0 + B*t1 + C*t2 + D*t3));\
-        t0 = *s0++; t2 = *s1++; \
-        OP(dst[1], (A*t1 + B*t0 + C*t3 + D*t2));\
-        dst+= stride;\
-        src+= stride;\
-    }while(--h);\
-}\
-\
-static void OPNAME ## h264_chroma_mc4_sh4(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int x, int y){\
-    const int A=(8-x)*(8-y);\
-    const int B=(  x)*(8-y);\
-    const int C=(8-x)*(  y);\
-    const int D=(  x)*(  y);\
-    \
-    assert(x<8 && y<8 && x>=0 && y>=0);\
-\
-    do {\
-        int t0,t1,t2,t3; \
-        uint8_t *s0 = src; \
-        uint8_t *s1 = src+stride; \
-        t0 = *s0++; t2 = *s1++; \
-        t1 = *s0++; t3 = *s1++; \
-        OP(dst[0], (A*t0 + B*t1 + C*t2 + D*t3));\
-        t0 = *s0++; t2 = *s1++; \
-        OP(dst[1], (A*t1 + B*t0 + C*t3 + D*t2));\
-        t1 = *s0++; t3 = *s1++; \
-        OP(dst[2], (A*t0 + B*t1 + C*t2 + D*t3));\
-        t0 = *s0++; t2 = *s1++; \
-        OP(dst[3], (A*t1 + B*t0 + C*t3 + D*t2));\
-        dst+= stride;\
-        src+= stride;\
-    }while(--h);\
-}\
-\
-static void OPNAME ## h264_chroma_mc8_sh4(uint8_t *dst/*align 8*/, uint8_t *src/*align 1*/, int stride, int h, int x, int y){\
-    const int A=(8-x)*(8-y);\
-    const int B=(  x)*(8-y);\
-    const int C=(8-x)*(  y);\
-    const int D=(  x)*(  y);\
-    \
-    assert(x<8 && y<8 && x>=0 && y>=0);\
-\
-    do {\
-        int t0,t1,t2,t3; \
-        uint8_t *s0 = src; \
-        uint8_t *s1 = src+stride; \
-        t0 = *s0++; t2 = *s1++; \
-        t1 = *s0++; t3 = *s1++; \
-        OP(dst[0], (A*t0 + B*t1 + C*t2 + D*t3));\
-        t0 = *s0++; t2 = *s1++; \
-        OP(dst[1], (A*t1 + B*t0 + C*t3 + D*t2));\
-        t1 = *s0++; t3 = *s1++; \
-        OP(dst[2], (A*t0 + B*t1 + C*t2 + D*t3));\
-        t0 = *s0++; t2 = *s1++; \
-        OP(dst[3], (A*t1 + B*t0 + C*t3 + D*t2));\
-        t1 = *s0++; t3 = *s1++; \
-        OP(dst[4], (A*t0 + B*t1 + C*t2 + D*t3));\
-        t0 = *s0++; t2 = *s1++; \
-        OP(dst[5], (A*t1 + B*t0 + C*t3 + D*t2));\
-        t1 = *s0++; t3 = *s1++; \
-        OP(dst[6], (A*t0 + B*t1 + C*t2 + D*t3));\
-        t0 = *s0++; t2 = *s1++; \
-        OP(dst[7], (A*t1 + B*t0 + C*t3 + D*t2));\
-        dst+= stride;\
-        src+= stride;\
-    }while(--h);\
-}
-
-#define op_avg(a, b) a = (((a)+(((b) + 32)>>6)+1)>>1)
-#define op_put(a, b) a = (((b) + 32)>>6)
-
-H264_CHROMA_MC(put_       , op_put)
-H264_CHROMA_MC(avg_       , op_avg)
-#undef op_avg
-#undef op_put
-
-av_cold void ff_h264chroma_init_sh4(H264ChromaContext *c, int bit_depth)
-{
-    const int high_bit_depth = bit_depth > 8;
-
-    if (!high_bit_depth) {
-    c->put_h264_chroma_pixels_tab[0]= put_h264_chroma_mc8_sh4;
-    c->put_h264_chroma_pixels_tab[1]= put_h264_chroma_mc4_sh4;
-    c->put_h264_chroma_pixels_tab[2]= put_h264_chroma_mc2_sh4;
-    c->avg_h264_chroma_pixels_tab[0]= avg_h264_chroma_mc8_sh4;
-    c->avg_h264_chroma_pixels_tab[1]= avg_h264_chroma_mc4_sh4;
-    c->avg_h264_chroma_pixels_tab[2]= avg_h264_chroma_mc2_sh4;
-    }
-}
diff --git a/libavcodec/sh4/qpel.c b/libavcodec/sh4/qpel.c
deleted file mode 100644
index 1a5a4cd..0000000
--- a/libavcodec/sh4/qpel.c
+++ /dev/null
@@ -1,862 +0,0 @@
-/*
- * This is optimized for sh, which have post increment addressing (*p++).
- * Some CPU may be index (p[n]) faster than post increment (*p++).
- *
- * copyright (c) 2001-2003 BERO <bero@geocities.co.jp>
- *
- * 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 "libavutil/common.h"
-#include "libavcodec/copy_block.h"
-#include "libavcodec/rnd_avg.h"
-
-#define PIXOP2(OPNAME, OP) \
-\
-static inline void OPNAME ## _pixels4_l2_aligned(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
-{\
-        do {\
-                OP(LP(dst  ),rnd_avg32(LPC(src1  ),LPC(src2  )) ); \
-                src1+=src_stride1; \
-                src2+=src_stride2; \
-                dst+=dst_stride; \
-        } while(--h); \
-}\
-\
-static inline void OPNAME ## _pixels4_l2_aligned2(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
-{\
-        do {\
-                OP(LP(dst  ),rnd_avg32(AV_RN32(src1  ),LPC(src2  )) ); \
-                src1+=src_stride1; \
-                src2+=src_stride2; \
-                dst+=dst_stride; \
-        } while(--h); \
-}\
-\
-static inline void OPNAME ## _no_rnd_pixels16_l2_aligned2(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
-{\
-        do {\
-                OP(LP(dst  ),no_rnd_avg32(AV_RN32(src1  ),LPC(src2  )) ); \
-                OP(LP(dst+4),no_rnd_avg32(AV_RN32(src1+4),LPC(src2+4)) ); \
-                OP(LP(dst+8),no_rnd_avg32(AV_RN32(src1+8),LPC(src2+8)) ); \
-                OP(LP(dst+12),no_rnd_avg32(AV_RN32(src1+12),LPC(src2+12)) ); \
-                src1+=src_stride1; \
-                src2+=src_stride2; \
-                dst+=dst_stride; \
-        } while(--h); \
-}\
-\
-static inline void OPNAME ## _pixels16_l2_aligned2(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
-{\
-        do {\
-                OP(LP(dst  ),rnd_avg32(AV_RN32(src1  ),LPC(src2  )) ); \
-                OP(LP(dst+4),rnd_avg32(AV_RN32(src1+4),LPC(src2+4)) ); \
-                OP(LP(dst+8),rnd_avg32(AV_RN32(src1+8),LPC(src2+8)) ); \
-                OP(LP(dst+12),rnd_avg32(AV_RN32(src1+12),LPC(src2+12)) ); \
-                src1+=src_stride1; \
-                src2+=src_stride2; \
-                dst+=dst_stride; \
-        } while(--h); \
-}\
-\
-static inline void OPNAME ## _no_rnd_pixels8_l2_aligned2(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
-{\
-        do { /* onlye src2 aligned */\
-                OP(LP(dst  ),no_rnd_avg32(AV_RN32(src1  ),LPC(src2  )) ); \
-                OP(LP(dst+4),no_rnd_avg32(AV_RN32(src1+4),LPC(src2+4)) ); \
-                src1+=src_stride1; \
-                src2+=src_stride2; \
-                dst+=dst_stride; \
-        } while(--h); \
-}\
-\
-static inline void OPNAME ## _pixels8_l2_aligned2(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
-{\
-        do {\
-                OP(LP(dst  ),rnd_avg32(AV_RN32(src1  ),LPC(src2  )) ); \
-                OP(LP(dst+4),rnd_avg32(AV_RN32(src1+4),LPC(src2+4)) ); \
-                src1+=src_stride1; \
-                src2+=src_stride2; \
-                dst+=dst_stride; \
-        } while(--h); \
-}\
-\
-static inline void OPNAME ## _no_rnd_pixels8_l2_aligned(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
-{\
-        do {\
-                OP(LP(dst  ),no_rnd_avg32(LPC(src1  ),LPC(src2  )) ); \
-                OP(LP(dst+4),no_rnd_avg32(LPC(src1+4),LPC(src2+4)) ); \
-                src1+=src_stride1; \
-                src2+=src_stride2; \
-                dst+=dst_stride; \
-        } while(--h); \
-}\
-\
-static inline void OPNAME ## _pixels8_l2_aligned(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
-{\
-        do {\
-                OP(LP(dst  ),rnd_avg32(LPC(src1  ),LPC(src2  )) ); \
-                OP(LP(dst+4),rnd_avg32(LPC(src1+4),LPC(src2+4)) ); \
-                src1+=src_stride1; \
-                src2+=src_stride2; \
-                dst+=dst_stride; \
-        } while(--h); \
-}\
-\
-static inline void OPNAME ## _no_rnd_pixels16_l2_aligned(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
-{\
-        do {\
-                OP(LP(dst  ),no_rnd_avg32(LPC(src1  ),LPC(src2  )) ); \
-                OP(LP(dst+4),no_rnd_avg32(LPC(src1+4),LPC(src2+4)) ); \
-                OP(LP(dst+8),no_rnd_avg32(LPC(src1+8),LPC(src2+8)) ); \
-                OP(LP(dst+12),no_rnd_avg32(LPC(src1+12),LPC(src2+12)) ); \
-                src1+=src_stride1; \
-                src2+=src_stride2; \
-                dst+=dst_stride; \
-        } while(--h); \
-}\
-\
-static inline void OPNAME ## _pixels16_l2_aligned(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
-{\
-        do {\
-                OP(LP(dst  ),rnd_avg32(LPC(src1  ),LPC(src2  )) ); \
-                OP(LP(dst+4),rnd_avg32(LPC(src1+4),LPC(src2+4)) ); \
-                OP(LP(dst+8),rnd_avg32(LPC(src1+8),LPC(src2+8)) ); \
-                OP(LP(dst+12),rnd_avg32(LPC(src1+12),LPC(src2+12)) ); \
-                src1+=src_stride1; \
-                src2+=src_stride2; \
-                dst+=dst_stride; \
-        } while(--h); \
-}\
-\
-static inline void OPNAME ## _no_rnd_pixels16_l2_aligned1(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
-{ OPNAME ## _no_rnd_pixels16_l2_aligned2(dst,src2,src1,dst_stride,src_stride2,src_stride1,h); } \
-\
-static inline void OPNAME ## _pixels16_l2_aligned1(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
-{ OPNAME ## _pixels16_l2_aligned2(dst,src2,src1,dst_stride,src_stride2,src_stride1,h); } \
-\
-static inline void OPNAME ## _no_rnd_pixels8_l2_aligned1(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
-{ OPNAME ## _no_rnd_pixels8_l2_aligned2(dst,src2,src1,dst_stride,src_stride2,src_stride1,h); } \
-\
-static inline void OPNAME ## _pixels8_l2_aligned1(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, int src_stride1, int src_stride2, int h) \
-{ OPNAME ## _pixels8_l2_aligned2(dst,src2,src1,dst_stride,src_stride2,src_stride1,h); } \
-\
-static inline void OPNAME ## _pixels8_l4_aligned(uint8_t *dst, const uint8_t *src1, uint8_t *src2, uint8_t *src3, uint8_t *src4,int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
-        do { \
-                uint32_t a0,a1,a2,a3; \
-                UNPACK(a0,a1,LPC(src1),LPC(src2)); \
-                UNPACK(a2,a3,LPC(src3),LPC(src4)); \
-                OP(LP(dst),rnd_PACK(a0,a1,a2,a3)); \
-                UNPACK(a0,a1,LPC(src1+4),LPC(src2+4)); \
-                UNPACK(a2,a3,LPC(src3+4),LPC(src4+4)); \
-                OP(LP(dst+4),rnd_PACK(a0,a1,a2,a3)); \
-                src1+=src_stride1;\
-                src2+=src_stride2;\
-                src3+=src_stride3;\
-                src4+=src_stride4;\
-                dst+=dst_stride;\
-        } while(--h); \
-} \
-\
-static inline void OPNAME ## _no_rnd_pixels8_l4_aligned(uint8_t *dst, const uint8_t *src1, uint8_t *src2, uint8_t *src3, uint8_t *src4,int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
-        do { \
-                uint32_t a0,a1,a2,a3; \
-                UNPACK(a0,a1,LPC(src1),LPC(src2)); \
-                UNPACK(a2,a3,LPC(src3),LPC(src4)); \
-                OP(LP(dst),no_rnd_PACK(a0,a1,a2,a3)); \
-                UNPACK(a0,a1,LPC(src1+4),LPC(src2+4)); \
-                UNPACK(a2,a3,LPC(src3+4),LPC(src4+4)); \
-                OP(LP(dst+4),no_rnd_PACK(a0,a1,a2,a3)); \
-                src1+=src_stride1;\
-                src2+=src_stride2;\
-                src3+=src_stride3;\
-                src4+=src_stride4;\
-                dst+=dst_stride;\
-        } while(--h); \
-} \
-\
-static inline void OPNAME ## _pixels8_l4_aligned0(uint8_t *dst, const uint8_t *src1, uint8_t *src2, uint8_t *src3, uint8_t *src4,int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
-        do { \
-                uint32_t a0,a1,a2,a3; /* src1 only not aligned */\
-                UNPACK(a0,a1,AV_RN32(src1),LPC(src2)); \
-                UNPACK(a2,a3,LPC(src3),LPC(src4)); \
-                OP(LP(dst),rnd_PACK(a0,a1,a2,a3)); \
-                UNPACK(a0,a1,AV_RN32(src1+4),LPC(src2+4)); \
-                UNPACK(a2,a3,LPC(src3+4),LPC(src4+4)); \
-                OP(LP(dst+4),rnd_PACK(a0,a1,a2,a3)); \
-                src1+=src_stride1;\
-                src2+=src_stride2;\
-                src3+=src_stride3;\
-                src4+=src_stride4;\
-                dst+=dst_stride;\
-        } while(--h); \
-} \
-\
-static inline void OPNAME ## _no_rnd_pixels8_l4_aligned0(uint8_t *dst, const uint8_t *src1, uint8_t *src2, uint8_t *src3, uint8_t *src4,int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
-        do { \
-                uint32_t a0,a1,a2,a3; \
-                UNPACK(a0,a1,AV_RN32(src1),LPC(src2)); \
-                UNPACK(a2,a3,LPC(src3),LPC(src4)); \
-                OP(LP(dst),no_rnd_PACK(a0,a1,a2,a3)); \
-                UNPACK(a0,a1,AV_RN32(src1+4),LPC(src2+4)); \
-                UNPACK(a2,a3,LPC(src3+4),LPC(src4+4)); \
-                OP(LP(dst+4),no_rnd_PACK(a0,a1,a2,a3)); \
-                src1+=src_stride1;\
-                src2+=src_stride2;\
-                src3+=src_stride3;\
-                src4+=src_stride4;\
-                dst+=dst_stride;\
-        } while(--h); \
-} \
-\
-static inline void OPNAME ## _pixels16_l4_aligned(uint8_t *dst, const uint8_t *src1, uint8_t *src2, uint8_t *src3, uint8_t *src4,int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
-        do { \
-                uint32_t a0,a1,a2,a3; \
-                UNPACK(a0,a1,LPC(src1),LPC(src2)); \
-                UNPACK(a2,a3,LPC(src3),LPC(src4)); \
-                OP(LP(dst),rnd_PACK(a0,a1,a2,a3)); \
-                UNPACK(a0,a1,LPC(src1+4),LPC(src2+4)); \
-                UNPACK(a2,a3,LPC(src3+4),LPC(src4+4)); \
-                OP(LP(dst+8),rnd_PACK(a0,a1,a2,a3)); \
-                UNPACK(a0,a1,LPC(src1+8),LPC(src2+8)); \
-                UNPACK(a2,a3,LPC(src3+8),LPC(src4+8)); \
-                OP(LP(dst+8),rnd_PACK(a0,a1,a2,a3)); \
-                UNPACK(a0,a1,LPC(src1+12),LPC(src2+12)); \
-                UNPACK(a2,a3,LPC(src3+12),LPC(src4+12)); \
-                OP(LP(dst+12),rnd_PACK(a0,a1,a2,a3)); \
-                src1+=src_stride1;\
-                src2+=src_stride2;\
-                src3+=src_stride3;\
-                src4+=src_stride4;\
-                dst+=dst_stride;\
-        } while(--h); \
-} \
-\
-static inline void OPNAME ## _no_rnd_pixels16_l4_aligned(uint8_t *dst, const uint8_t *src1, uint8_t *src2, uint8_t *src3, uint8_t *src4,int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
-        do { \
-                uint32_t a0,a1,a2,a3; \
-                UNPACK(a0,a1,LPC(src1),LPC(src2)); \
-                UNPACK(a2,a3,LPC(src3),LPC(src4)); \
-                OP(LP(dst),no_rnd_PACK(a0,a1,a2,a3)); \
-                UNPACK(a0,a1,LPC(src1+4),LPC(src2+4)); \
-                UNPACK(a2,a3,LPC(src3+4),LPC(src4+4)); \
-                OP(LP(dst+4),no_rnd_PACK(a0,a1,a2,a3)); \
-                UNPACK(a0,a1,LPC(src1+8),LPC(src2+8)); \
-                UNPACK(a2,a3,LPC(src3+8),LPC(src4+8)); \
-                OP(LP(dst+8),no_rnd_PACK(a0,a1,a2,a3)); \
-                UNPACK(a0,a1,LPC(src1+12),LPC(src2+12)); \
-                UNPACK(a2,a3,LPC(src3+12),LPC(src4+12)); \
-                OP(LP(dst+12),no_rnd_PACK(a0,a1,a2,a3)); \
-                src1+=src_stride1;\
-                src2+=src_stride2;\
-                src3+=src_stride3;\
-                src4+=src_stride4;\
-                dst+=dst_stride;\
-        } while(--h); \
-} \
-\
-static inline void OPNAME ## _pixels16_l4_aligned0(uint8_t *dst, const uint8_t *src1, uint8_t *src2, uint8_t *src3, uint8_t *src4,int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
-        do { /* src1 is unaligned */\
-                uint32_t a0,a1,a2,a3; \
-                UNPACK(a0,a1,AV_RN32(src1),LPC(src2)); \
-                UNPACK(a2,a3,LPC(src3),LPC(src4)); \
-                OP(LP(dst),rnd_PACK(a0,a1,a2,a3)); \
-                UNPACK(a0,a1,AV_RN32(src1+4),LPC(src2+4)); \
-                UNPACK(a2,a3,LPC(src3+4),LPC(src4+4)); \
-                OP(LP(dst+8),rnd_PACK(a0,a1,a2,a3)); \
-                UNPACK(a0,a1,AV_RN32(src1+8),LPC(src2+8)); \
-                UNPACK(a2,a3,LPC(src3+8),LPC(src4+8)); \
-                OP(LP(dst+8),rnd_PACK(a0,a1,a2,a3)); \
-                UNPACK(a0,a1,AV_RN32(src1+12),LPC(src2+12)); \
-                UNPACK(a2,a3,LPC(src3+12),LPC(src4+12)); \
-                OP(LP(dst+12),rnd_PACK(a0,a1,a2,a3)); \
-                src1+=src_stride1;\
-                src2+=src_stride2;\
-                src3+=src_stride3;\
-                src4+=src_stride4;\
-                dst+=dst_stride;\
-        } while(--h); \
-} \
-\
-static inline void OPNAME ## _no_rnd_pixels16_l4_aligned0(uint8_t *dst, const uint8_t *src1, uint8_t *src2, uint8_t *src3, uint8_t *src4,int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
-        do { \
-                uint32_t a0,a1,a2,a3; \
-                UNPACK(a0,a1,AV_RN32(src1),LPC(src2)); \
-                UNPACK(a2,a3,LPC(src3),LPC(src4)); \
-                OP(LP(dst),no_rnd_PACK(a0,a1,a2,a3)); \
-                UNPACK(a0,a1,AV_RN32(src1+4),LPC(src2+4)); \
-                UNPACK(a2,a3,LPC(src3+4),LPC(src4+4)); \
-                OP(LP(dst+4),no_rnd_PACK(a0,a1,a2,a3)); \
-                UNPACK(a0,a1,AV_RN32(src1+8),LPC(src2+8)); \
-                UNPACK(a2,a3,LPC(src3+8),LPC(src4+8)); \
-                OP(LP(dst+8),no_rnd_PACK(a0,a1,a2,a3)); \
-                UNPACK(a0,a1,AV_RN32(src1+12),LPC(src2+12)); \
-                UNPACK(a2,a3,LPC(src3+12),LPC(src4+12)); \
-                OP(LP(dst+12),no_rnd_PACK(a0,a1,a2,a3)); \
-                src1+=src_stride1;\
-                src2+=src_stride2;\
-                src3+=src_stride3;\
-                src4+=src_stride4;\
-                dst+=dst_stride;\
-        } while(--h); \
-} \
-\
-
-#define op_avg(a, b) a = rnd_avg32(a,b)
-#define op_put(a, b) a = b
-
-PIXOP2(avg, op_avg)
-PIXOP2(put, op_put)
-#undef op_avg
-#undef op_put
-
-#define avg2(a,b) ((a+b+1)>>1)
-#define avg4(a,b,c,d) ((a+b+c+d+2)>>2)
-
-
-static void gmc1_c(uint8_t *dst, uint8_t *src, int stride, int h, int x16, int y16, int rounder)
-{
-    const int A=(16-x16)*(16-y16);
-    const int B=(   x16)*(16-y16);
-    const int C=(16-x16)*(   y16);
-    const int D=(   x16)*(   y16);
-
-    do {
-        int t0,t1,t2,t3;
-        uint8_t *s0 = src;
-        uint8_t *s1 = src+stride;
-        t0 = *s0++; t2 = *s1++;
-        t1 = *s0++; t3 = *s1++;
-        dst[0]= (A*t0 + B*t1 + C*t2 + D*t3 + rounder)>>8;
-        t0 = *s0++; t2 = *s1++;
-        dst[1]= (A*t1 + B*t0 + C*t3 + D*t2 + rounder)>>8;
-        t1 = *s0++; t3 = *s1++;
-        dst[2]= (A*t0 + B*t1 + C*t2 + D*t3 + rounder)>>8;
-        t0 = *s0++; t2 = *s1++;
-        dst[3]= (A*t1 + B*t0 + C*t3 + D*t2 + rounder)>>8;
-        t1 = *s0++; t3 = *s1++;
-        dst[4]= (A*t0 + B*t1 + C*t2 + D*t3 + rounder)>>8;
-        t0 = *s0++; t2 = *s1++;
-        dst[5]= (A*t1 + B*t0 + C*t3 + D*t2 + rounder)>>8;
-        t1 = *s0++; t3 = *s1++;
-        dst[6]= (A*t0 + B*t1 + C*t2 + D*t3 + rounder)>>8;
-        t0 = *s0++; t2 = *s1++;
-        dst[7]= (A*t1 + B*t0 + C*t3 + D*t2 + rounder)>>8;
-        dst+= stride;
-        src+= stride;
-    }while(--h);
-}
-
-#define QPEL_MC(r, OPNAME, RND, OP) \
-static void OPNAME ## mpeg4_qpel8_h_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){\
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
-    do {\
-        uint8_t *s = src; \
-        int src0,src1,src2,src3,src4,src5,src6,src7,src8;\
-        src0= *s++;\
-        src1= *s++;\
-        src2= *s++;\
-        src3= *s++;\
-        src4= *s++;\
-        OP(dst[0], (src0+src1)*20 - (src0+src2)*6 + (src1+src3)*3 - (src2+src4));\
-        src5= *s++;\
-        OP(dst[1], (src1+src2)*20 - (src0+src3)*6 + (src0+src4)*3 - (src1+src5));\
-        src6= *s++;\
-        OP(dst[2], (src2+src3)*20 - (src1+src4)*6 + (src0+src5)*3 - (src0+src6));\
-        src7= *s++;\
-        OP(dst[3], (src3+src4)*20 - (src2+src5)*6 + (src1+src6)*3 - (src0+src7));\
-        src8= *s++;\
-        OP(dst[4], (src4+src5)*20 - (src3+src6)*6 + (src2+src7)*3 - (src1+src8));\
-        OP(dst[5], (src5+src6)*20 - (src4+src7)*6 + (src3+src8)*3 - (src2+src8));\
-        OP(dst[6], (src6+src7)*20 - (src5+src8)*6 + (src4+src8)*3 - (src3+src7));\
-        OP(dst[7], (src7+src8)*20 - (src6+src8)*6 + (src5+src7)*3 - (src4+src6));\
-        dst+=dstStride;\
-        src+=srcStride;\
-    }while(--h);\
-}\
-\
-static void OPNAME ## mpeg4_qpel8_v_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
-    int w=8;\
-    do{\
-        uint8_t *s = src, *d=dst;\
-        int src0,src1,src2,src3,src4,src5,src6,src7,src8;\
-        src0 = *s; s+=srcStride; \
-        src1 = *s; s+=srcStride; \
-        src2 = *s; s+=srcStride; \
-        src3 = *s; s+=srcStride; \
-        src4 = *s; s+=srcStride; \
-        OP(*d, (src0+src1)*20 - (src0+src2)*6 + (src1+src3)*3 - (src2+src4));d+=dstStride;\
-        src5 = *s; s+=srcStride; \
-        OP(*d, (src1+src2)*20 - (src0+src3)*6 + (src0+src4)*3 - (src1+src5));d+=dstStride;\
-        src6 = *s; s+=srcStride; \
-        OP(*d, (src2+src3)*20 - (src1+src4)*6 + (src0+src5)*3 - (src0+src6));d+=dstStride;\
-        src7 = *s; s+=srcStride; \
-        OP(*d, (src3+src4)*20 - (src2+src5)*6 + (src1+src6)*3 - (src0+src7));d+=dstStride;\
-        src8 = *s; \
-        OP(*d, (src4+src5)*20 - (src3+src6)*6 + (src2+src7)*3 - (src1+src8));d+=dstStride;\
-        OP(*d, (src5+src6)*20 - (src4+src7)*6 + (src3+src8)*3 - (src2+src8));d+=dstStride;\
-        OP(*d, (src6+src7)*20 - (src5+src8)*6 + (src4+src8)*3 - (src3+src7));d+=dstStride;\
-        OP(*d, (src7+src8)*20 - (src6+src8)*6 + (src5+src7)*3 - (src4+src6));\
-        dst++;\
-        src++;\
-    }while(--w);\
-}\
-\
-static void OPNAME ## mpeg4_qpel16_h_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){\
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
-    do {\
-        uint8_t *s = src;\
-        int src0,src1,src2,src3,src4,src5,src6,src7,src8;\
-        int src9,src10,src11,src12,src13,src14,src15,src16;\
-        src0= *s++;\
-        src1= *s++;\
-        src2= *s++;\
-        src3= *s++;\
-        src4= *s++;\
-        OP(dst[ 0], (src0 +src1 )*20 - (src0 +src2 )*6 + (src1 +src3 )*3 - (src2 +src4 ));\
-        src5= *s++;\
-        OP(dst[ 1], (src1 +src2 )*20 - (src0 +src3 )*6 + (src0 +src4 )*3 - (src1 +src5 ));\
-        src6= *s++;\
-        OP(dst[ 2], (src2 +src3 )*20 - (src1 +src4 )*6 + (src0 +src5 )*3 - (src0 +src6 ));\
-        src7= *s++;\
-        OP(dst[ 3], (src3 +src4 )*20 - (src2 +src5 )*6 + (src1 +src6 )*3 - (src0 +src7 ));\
-        src8= *s++;\
-        OP(dst[ 4], (src4 +src5 )*20 - (src3 +src6 )*6 + (src2 +src7 )*3 - (src1 +src8 ));\
-        src9= *s++;\
-        OP(dst[ 5], (src5 +src6 )*20 - (src4 +src7 )*6 + (src3 +src8 )*3 - (src2 +src9 ));\
-        src10= *s++;\
-        OP(dst[ 6], (src6 +src7 )*20 - (src5 +src8 )*6 + (src4 +src9 )*3 - (src3 +src10));\
-        src11= *s++;\
-        OP(dst[ 7], (src7 +src8 )*20 - (src6 +src9 )*6 + (src5 +src10)*3 - (src4 +src11));\
-        src12= *s++;\
-        OP(dst[ 8], (src8 +src9 )*20 - (src7 +src10)*6 + (src6 +src11)*3 - (src5 +src12));\
-        src13= *s++;\
-        OP(dst[ 9], (src9 +src10)*20 - (src8 +src11)*6 + (src7 +src12)*3 - (src6 +src13));\
-        src14= *s++;\
-        OP(dst[10], (src10+src11)*20 - (src9 +src12)*6 + (src8 +src13)*3 - (src7 +src14));\
-        src15= *s++;\
-        OP(dst[11], (src11+src12)*20 - (src10+src13)*6 + (src9 +src14)*3 - (src8 +src15));\
-        src16= *s++;\
-        OP(dst[12], (src12+src13)*20 - (src11+src14)*6 + (src10+src15)*3 - (src9 +src16));\
-        OP(dst[13], (src13+src14)*20 - (src12+src15)*6 + (src11+src16)*3 - (src10+src16));\
-        OP(dst[14], (src14+src15)*20 - (src13+src16)*6 + (src12+src16)*3 - (src11+src15));\
-        OP(dst[15], (src15+src16)*20 - (src14+src16)*6 + (src13+src15)*3 - (src12+src14));\
-        dst+=dstStride;\
-        src+=srcStride;\
-    }while(--h);\
-}\
-\
-static void OPNAME ## mpeg4_qpel16_v_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride){\
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;\
-    int w=16;\
-    do {\
-        uint8_t *s = src, *d=dst;\
-        int src0,src1,src2,src3,src4,src5,src6,src7,src8;\
-        int src9,src10,src11,src12,src13,src14,src15,src16;\
-        src0 = *s; s+=srcStride; \
-        src1 = *s; s+=srcStride; \
-        src2 = *s; s+=srcStride; \
-        src3 = *s; s+=srcStride; \
-        src4 = *s; s+=srcStride; \
-        OP(*d, (src0 +src1 )*20 - (src0 +src2 )*6 + (src1 +src3 )*3 - (src2 +src4 ));d+=dstStride;\
-        src5 = *s; s+=srcStride; \
-        OP(*d, (src1 +src2 )*20 - (src0 +src3 )*6 + (src0 +src4 )*3 - (src1 +src5 ));d+=dstStride;\
-        src6 = *s; s+=srcStride; \
-        OP(*d, (src2 +src3 )*20 - (src1 +src4 )*6 + (src0 +src5 )*3 - (src0 +src6 ));d+=dstStride;\
-        src7 = *s; s+=srcStride; \
-        OP(*d, (src3 +src4 )*20 - (src2 +src5 )*6 + (src1 +src6 )*3 - (src0 +src7 ));d+=dstStride;\
-        src8 = *s; s+=srcStride; \
-        OP(*d, (src4 +src5 )*20 - (src3 +src6 )*6 + (src2 +src7 )*3 - (src1 +src8 ));d+=dstStride;\
-        src9 = *s; s+=srcStride; \
-        OP(*d, (src5 +src6 )*20 - (src4 +src7 )*6 + (src3 +src8 )*3 - (src2 +src9 ));d+=dstStride;\
-        src10 = *s; s+=srcStride; \
-        OP(*d, (src6 +src7 )*20 - (src5 +src8 )*6 + (src4 +src9 )*3 - (src3 +src10));d+=dstStride;\
-        src11 = *s; s+=srcStride; \
-        OP(*d, (src7 +src8 )*20 - (src6 +src9 )*6 + (src5 +src10)*3 - (src4 +src11));d+=dstStride;\
-        src12 = *s; s+=srcStride; \
-        OP(*d, (src8 +src9 )*20 - (src7 +src10)*6 + (src6 +src11)*3 - (src5 +src12));d+=dstStride;\
-        src13 = *s; s+=srcStride; \
-        OP(*d, (src9 +src10)*20 - (src8 +src11)*6 + (src7 +src12)*3 - (src6 +src13));d+=dstStride;\
-        src14 = *s; s+=srcStride; \
-        OP(*d, (src10+src11)*20 - (src9 +src12)*6 + (src8 +src13)*3 - (src7 +src14));d+=dstStride;\
-        src15 = *s; s+=srcStride; \
-        OP(*d, (src11+src12)*20 - (src10+src13)*6 + (src9 +src14)*3 - (src8 +src15));d+=dstStride;\
-        src16 = *s; \
-        OP(*d, (src12+src13)*20 - (src11+src14)*6 + (src10+src15)*3 - (src9 +src16));d+=dstStride;\
-        OP(*d, (src13+src14)*20 - (src12+src15)*6 + (src11+src16)*3 - (src10+src16));d+=dstStride;\
-        OP(*d, (src14+src15)*20 - (src13+src16)*6 + (src12+src16)*3 - (src11+src15));d+=dstStride;\
-        OP(*d, (src15+src16)*20 - (src14+src16)*6 + (src13+src15)*3 - (src12+src14));\
-        dst++;\
-        src++;\
-    }while(--w);\
-}\
-\
-static void OPNAME ## qpel8_mc00_sh4 (uint8_t *dst, uint8_t *src, int stride){\
-    OPNAME ## pixels8_c(dst, src, stride, 8);\
-}\
-\
-static void OPNAME ## qpel8_mc10_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t half[64];\
-    put ## RND ## mpeg4_qpel8_h_lowpass(half, src, 8, stride, 8);\
-    OPNAME ## pixels8_l2_aligned2(dst, src, half, stride, stride, 8, 8);\
-}\
-\
-static void OPNAME ## qpel8_mc20_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    OPNAME ## mpeg4_qpel8_h_lowpass(dst, src, stride, stride, 8);\
-}\
-\
-static void OPNAME ## qpel8_mc30_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t half[64];\
-    put ## RND ## mpeg4_qpel8_h_lowpass(half, src, 8, stride, 8);\
-    OPNAME ## pixels8_l2_aligned2(dst, src+1, half, stride, stride, 8, 8);\
-}\
-\
-static void OPNAME ## qpel8_mc01_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t full[16*9];\
-    uint8_t half[64];\
-    copy_block9(full, src, 16, stride, 9);\
-    put ## RND ## mpeg4_qpel8_v_lowpass(half, full, 8, 16);\
-    OPNAME ## pixels8_l2_aligned(dst, full, half, stride, 16, 8, 8);\
-}\
-\
-static void OPNAME ## qpel8_mc02_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t full[16*9];\
-    copy_block9(full, src, 16, stride, 9);\
-    OPNAME ## mpeg4_qpel8_v_lowpass(dst, full, stride, 16);\
-}\
-\
-static void OPNAME ## qpel8_mc03_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t full[16*9];\
-    uint8_t half[64];\
-    copy_block9(full, src, 16, stride, 9);\
-    put ## RND ## mpeg4_qpel8_v_lowpass(half, full, 8, 16);\
-    OPNAME ## pixels8_l2_aligned(dst, full+16, half, stride, 16, 8, 8);\
-}\
-static void OPNAME ## qpel8_mc11_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t full[16*9];\
-    uint8_t halfH[72];\
-    uint8_t halfHV[64];\
-    copy_block9(full, src, 16, stride, 9);\
-    put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);\
-    put ## RND ## pixels8_l2_aligned(halfH, halfH, full, 8, 8, 16, 9);\
-    put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
-    OPNAME ## pixels8_l2_aligned(dst, halfH, halfHV, stride, 8, 8, 8);\
-}\
-static void OPNAME ## qpel8_mc31_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t full[16*9];\
-    uint8_t halfH[72];\
-    uint8_t halfHV[64];\
-    copy_block9(full, src, 16, stride, 9);\
-    put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);\
-    put ## RND ## pixels8_l2_aligned1(halfH, halfH, full+1, 8, 8, 16, 9);\
-    put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
-    OPNAME ## pixels8_l2_aligned(dst, halfH, halfHV, stride, 8, 8, 8);\
-}\
-static void OPNAME ## qpel8_mc13_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t full[16*9];\
-    uint8_t halfH[72];\
-    uint8_t halfHV[64];\
-    copy_block9(full, src, 16, stride, 9);\
-    put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);\
-    put ## RND ## pixels8_l2_aligned(halfH, halfH, full, 8, 8, 16, 9);\
-    put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
-    OPNAME ## pixels8_l2_aligned(dst, halfH+8, halfHV, stride, 8, 8, 8);\
-}\
-static void OPNAME ## qpel8_mc33_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t full[16*9];\
-    uint8_t halfH[72];\
-    uint8_t halfHV[64];\
-    copy_block9(full, src, 16, stride, 9);\
-    put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);\
-    put ## RND ## pixels8_l2_aligned1(halfH, halfH, full+1, 8, 8, 16, 9);\
-    put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
-    OPNAME ## pixels8_l2_aligned(dst, halfH+8, halfHV, stride, 8, 8, 8);\
-}\
-static void OPNAME ## qpel8_mc21_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t halfH[72];\
-    uint8_t halfHV[64];\
-    put ## RND ## mpeg4_qpel8_h_lowpass(halfH, src, 8, stride, 9);\
-    put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
-    OPNAME ## pixels8_l2_aligned(dst, halfH, halfHV, stride, 8, 8, 8);\
-}\
-static void OPNAME ## qpel8_mc23_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t halfH[72];\
-    uint8_t halfHV[64];\
-    put ## RND ## mpeg4_qpel8_h_lowpass(halfH, src, 8, stride, 9);\
-    put ## RND ## mpeg4_qpel8_v_lowpass(halfHV, halfH, 8, 8);\
-    OPNAME ## pixels8_l2_aligned(dst, halfH+8, halfHV, stride, 8, 8, 8);\
-}\
-static void OPNAME ## qpel8_mc12_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t full[16*9];\
-    uint8_t halfH[72];\
-    copy_block9(full, src, 16, stride, 9);\
-    put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);\
-    put ## RND ## pixels8_l2_aligned(halfH, halfH, full, 8, 8, 16, 9);\
-    OPNAME ## mpeg4_qpel8_v_lowpass(dst, halfH, stride, 8);\
-}\
-static void OPNAME ## qpel8_mc32_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t full[16*9];\
-    uint8_t halfH[72];\
-    copy_block9(full, src, 16, stride, 9);\
-    put ## RND ## mpeg4_qpel8_h_lowpass(halfH, full, 8, 16, 9);\
-    put ## RND ## pixels8_l2_aligned1(halfH, halfH, full+1, 8, 8, 16, 9);\
-    OPNAME ## mpeg4_qpel8_v_lowpass(dst, halfH, stride, 8);\
-}\
-static void OPNAME ## qpel8_mc22_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t halfH[72];\
-    put ## RND ## mpeg4_qpel8_h_lowpass(halfH, src, 8, stride, 9);\
-    OPNAME ## mpeg4_qpel8_v_lowpass(dst, halfH, stride, 8);\
-}\
-static void OPNAME ## qpel16_mc00_sh4 (uint8_t *dst, uint8_t *src, int stride){\
-    OPNAME ## pixels16_c(dst, src, stride, 16);\
-}\
-\
-static void OPNAME ## qpel16_mc10_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t half[256];\
-    put ## RND ## mpeg4_qpel16_h_lowpass(half, src, 16, stride, 16);\
-    OPNAME ## pixels16_l2_aligned2(dst, src, half, stride, stride, 16, 16);\
-}\
-\
-static void OPNAME ## qpel16_mc20_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    OPNAME ## mpeg4_qpel16_h_lowpass(dst, src, stride, stride, 16);\
-}\
-\
-static void OPNAME ## qpel16_mc30_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t half[256];\
-    put ## RND ## mpeg4_qpel16_h_lowpass(half, src, 16, stride, 16);\
-    OPNAME ## pixels16_l2_aligned2(dst, src+1, half, stride, stride, 16, 16);\
-}\
-\
-static void OPNAME ## qpel16_mc01_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t full[24*17];\
-    uint8_t half[256];\
-    copy_block17(full, src, 24, stride, 17);\
-    put ## RND ## mpeg4_qpel16_v_lowpass(half, full, 16, 24);\
-    OPNAME ## pixels16_l2_aligned(dst, full, half, stride, 24, 16, 16);\
-}\
-\
-static void OPNAME ## qpel16_mc02_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t full[24*17];\
-    copy_block17(full, src, 24, stride, 17);\
-    OPNAME ## mpeg4_qpel16_v_lowpass(dst, full, stride, 24);\
-}\
-\
-static void OPNAME ## qpel16_mc03_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t full[24*17];\
-    uint8_t half[256];\
-    copy_block17(full, src, 24, stride, 17);\
-    put ## RND ## mpeg4_qpel16_v_lowpass(half, full, 16, 24);\
-    OPNAME ## pixels16_l2_aligned(dst, full+24, half, stride, 24, 16, 16);\
-}\
-static void OPNAME ## qpel16_mc11_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t full[24*17];\
-    uint8_t halfH[272];\
-    uint8_t halfHV[256];\
-    copy_block17(full, src, 24, stride, 17);\
-    put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);\
-    put ## RND ## pixels16_l2_aligned(halfH, halfH, full, 16, 16, 24, 17);\
-    put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
-    OPNAME ## pixels16_l2_aligned(dst, halfH, halfHV, stride, 16, 16, 16);\
-}\
-static void OPNAME ## qpel16_mc31_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t full[24*17];\
-    uint8_t halfH[272];\
-    uint8_t halfHV[256];\
-    copy_block17(full, src, 24, stride, 17);\
-    put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);\
-    put ## RND ## pixels16_l2_aligned1(halfH, halfH, full+1, 16, 16, 24, 17);\
-    put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
-    OPNAME ## pixels16_l2_aligned(dst, halfH, halfHV, stride, 16, 16, 16);\
-}\
-static void OPNAME ## qpel16_mc13_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t full[24*17];\
-    uint8_t halfH[272];\
-    uint8_t halfHV[256];\
-    copy_block17(full, src, 24, stride, 17);\
-    put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);\
-    put ## RND ## pixels16_l2_aligned(halfH, halfH, full, 16, 16, 24, 17);\
-    put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
-    OPNAME ## pixels16_l2_aligned(dst, halfH+16, halfHV, stride, 16, 16, 16);\
-}\
-static void OPNAME ## qpel16_mc33_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t full[24*17];\
-    uint8_t halfH[272];\
-    uint8_t halfHV[256];\
-    copy_block17(full, src, 24, stride, 17);\
-    put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);\
-    put ## RND ## pixels16_l2_aligned1(halfH, halfH, full+1, 16, 16, 24, 17);\
-    put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
-    OPNAME ## pixels16_l2_aligned(dst, halfH+16, halfHV, stride, 16, 16, 16);\
-}\
-static void OPNAME ## qpel16_mc21_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t halfH[272];\
-    uint8_t halfHV[256];\
-    put ## RND ## mpeg4_qpel16_h_lowpass(halfH, src, 16, stride, 17);\
-    put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
-    OPNAME ## pixels16_l2_aligned(dst, halfH, halfHV, stride, 16, 16, 16);\
-}\
-static void OPNAME ## qpel16_mc23_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t halfH[272];\
-    uint8_t halfHV[256];\
-    put ## RND ## mpeg4_qpel16_h_lowpass(halfH, src, 16, stride, 17);\
-    put ## RND ## mpeg4_qpel16_v_lowpass(halfHV, halfH, 16, 16);\
-    OPNAME ## pixels16_l2_aligned(dst, halfH+16, halfHV, stride, 16, 16, 16);\
-}\
-static void OPNAME ## qpel16_mc12_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t full[24*17];\
-    uint8_t halfH[272];\
-    copy_block17(full, src, 24, stride, 17);\
-    put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);\
-    put ## RND ## pixels16_l2_aligned(halfH, halfH, full, 16, 16, 24, 17);\
-    OPNAME ## mpeg4_qpel16_v_lowpass(dst, halfH, stride, 16);\
-}\
-static void OPNAME ## qpel16_mc32_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t full[24*17];\
-    uint8_t halfH[272];\
-    copy_block17(full, src, 24, stride, 17);\
-    put ## RND ## mpeg4_qpel16_h_lowpass(halfH, full, 16, 24, 17);\
-    put ## RND ## pixels16_l2_aligned1(halfH, halfH, full+1, 16, 16, 24, 17);\
-    OPNAME ## mpeg4_qpel16_v_lowpass(dst, halfH, stride, 16);\
-}\
-static void OPNAME ## qpel16_mc22_sh4(uint8_t *dst, uint8_t *src, int stride){\
-    uint8_t halfH[272];\
-    put ## RND ## mpeg4_qpel16_h_lowpass(halfH, src, 16, stride, 17);\
-    OPNAME ## mpeg4_qpel16_v_lowpass(dst, halfH, stride, 16);\
-}
-
-#define op_avg(a, b) a = (((a)+cm[((b) + 16)>>5]+1)>>1)
-#define op_avg_no_rnd(a, b) a = (((a)+cm[((b) + 15)>>5])>>1)
-#define op_put(a, b) a = cm[((b) + 16)>>5]
-#define op_put_no_rnd(a, b) a = cm[((b) + 15)>>5]
-
-QPEL_MC(0, put_       , _       , op_put)
-QPEL_MC(1, put_no_rnd_, _no_rnd_, op_put_no_rnd)
-QPEL_MC(0, avg_       , _       , op_avg)
-//QPEL_MC(1, avg_no_rnd , _       , op_avg)
-#undef op_avg
-#undef op_avg_no_rnd
-#undef op_put
-#undef op_put_no_rnd
-
-static void wmv2_mspel8_h_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int h){
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
-
-    do{
-        int src_1,src0,src1,src2,src3,src4,src5,src6,src7,src8,src9;
-        uint8_t *s = src;
-        src_1 = s[-1];
-        src0 = *s++;
-        src1 = *s++;
-        src2 = *s++;
-        dst[0]= cm[(9*(src0 + src1) - (src_1 + src2) + 8)>>4];
-        src3 = *s++;
-        dst[1]= cm[(9*(src1 + src2) - (src0 + src3) + 8)>>4];
-        src4 = *s++;
-        dst[2]= cm[(9*(src2 + src3) - (src1 + src4) + 8)>>4];
-        src5 = *s++;
-        dst[3]= cm[(9*(src3 + src4) - (src2 + src5) + 8)>>4];
-        src6 = *s++;
-        dst[4]= cm[(9*(src4 + src5) - (src3 + src6) + 8)>>4];
-        src7 = *s++;
-        dst[5]= cm[(9*(src5 + src6) - (src4 + src7) + 8)>>4];
-        src8 = *s++;
-        dst[6]= cm[(9*(src6 + src7) - (src5 + src8) + 8)>>4];
-        src9 = *s++;
-        dst[7]= cm[(9*(src7 + src8) - (src6 + src9) + 8)>>4];
-        dst+=dstStride;
-        src+=srcStride;
-    }while(--h);
-}
-
-static void wmv2_mspel8_v_lowpass(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int w){
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
-
-    do{
-        int src_1,src0,src1,src2,src3,src4,src5,src6,src7,src8,src9;
-        uint8_t *s = src,*d = dst;
-        src_1 = *(s-srcStride);
-        src0 = *s; s+=srcStride;
-        src1 = *s; s+=srcStride;
-        src2 = *s; s+=srcStride;
-        *d= cm[(9*(src0 + src1) - (src_1 + src2) + 8)>>4]; d+=dstStride;
-        src3 = *s; s+=srcStride;
-        *d= cm[(9*(src1 + src2) - (src0  + src3) + 8)>>4]; d+=dstStride;
-        src4 = *s; s+=srcStride;
-        *d= cm[(9*(src2 + src3) - (src1  + src4) + 8)>>4]; d+=dstStride;
-        src5 = *s; s+=srcStride;
-        *d= cm[(9*(src3 + src4) - (src2  + src5) + 8)>>4]; d+=dstStride;
-        src6 = *s; s+=srcStride;
-        *d= cm[(9*(src4 + src5) - (src3  + src6) + 8)>>4]; d+=dstStride;
-        src7 = *s; s+=srcStride;
-        *d= cm[(9*(src5 + src6) - (src4  + src7) + 8)>>4]; d+=dstStride;
-        src8 = *s; s+=srcStride;
-        *d= cm[(9*(src6 + src7) - (src5  + src8) + 8)>>4]; d+=dstStride;
-        src9 = *s;
-        *d= cm[(9*(src7 + src8) - (src6  + src9) + 8)>>4]; d+=dstStride;
-        src++;
-        dst++;
-    }while(--w);
-}
-
-static void put_mspel8_mc00_sh4 (uint8_t *dst, uint8_t *src, int stride){
-    put_pixels8_c(dst, src, stride, 8);
-}
-
-static void put_mspel8_mc10_sh4(uint8_t *dst, uint8_t *src, int stride){
-    uint8_t half[64];
-    wmv2_mspel8_h_lowpass(half, src, 8, stride, 8);
-    put_pixels8_l2_aligned2(dst, src, half, stride, stride, 8, 8);
-}
-
-static void put_mspel8_mc20_sh4(uint8_t *dst, uint8_t *src, int stride){
-    wmv2_mspel8_h_lowpass(dst, src, stride, stride, 8);
-}
-
-static void put_mspel8_mc30_sh4(uint8_t *dst, uint8_t *src, int stride){
-    uint8_t half[64];
-    wmv2_mspel8_h_lowpass(half, src, 8, stride, 8);
-    put_pixels8_l2_aligned2(dst, src+1, half, stride, stride, 8, 8);
-}
-
-static void put_mspel8_mc02_sh4(uint8_t *dst, uint8_t *src, int stride){
-    wmv2_mspel8_v_lowpass(dst, src, stride, stride, 8);
-}
-
-static void put_mspel8_mc12_sh4(uint8_t *dst, uint8_t *src, int stride){
-    uint8_t halfH[88];
-    uint8_t halfV[64];
-    uint8_t halfHV[64];
-    wmv2_mspel8_h_lowpass(halfH, src-stride, 8, stride, 11);
-    wmv2_mspel8_v_lowpass(halfV, src, 8, stride, 8);
-    wmv2_mspel8_v_lowpass(halfHV, halfH+8, 8, 8, 8);
-    put_pixels8_l2_aligned(dst, halfV, halfHV, stride, 8, 8, 8);
-}
-static void put_mspel8_mc32_sh4(uint8_t *dst, uint8_t *src, int stride){
-    uint8_t halfH[88];
-    uint8_t halfV[64];
-    uint8_t halfHV[64];
-    wmv2_mspel8_h_lowpass(halfH, src-stride, 8, stride, 11);
-    wmv2_mspel8_v_lowpass(halfV, src+1, 8, stride, 8);
-    wmv2_mspel8_v_lowpass(halfHV, halfH+8, 8, 8, 8);
-    put_pixels8_l2_aligned(dst, halfV, halfHV, stride, 8, 8, 8);
-}
-static void put_mspel8_mc22_sh4(uint8_t *dst, uint8_t *src, int stride){
-    uint8_t halfH[88];
-    wmv2_mspel8_h_lowpass(halfH, src-stride, 8, stride, 11);
-    wmv2_mspel8_v_lowpass(dst, halfH+8, stride, 8, 8);
-}
diff --git a/libavcodec/shorten.c b/libavcodec/shorten.c
index 648df68..1d3b6eb 100644
--- a/libavcodec/shorten.c
+++ b/libavcodec/shorten.c
@@ -87,7 +87,7 @@
     GetBitContext gb;
 
     int min_framesize, max_framesize;
-    int channels;
+    unsigned channels;
 
     int32_t *decoded[MAX_CHANNELS];
     int32_t *decoded_base[MAX_CHANNELS];
@@ -112,10 +112,10 @@
     int got_quit_command;
 } ShortenContext;
 
-static av_cold int shorten_decode_init(AVCodecContext * avctx)
+static av_cold int shorten_decode_init(AVCodecContext *avctx)
 {
     ShortenContext *s = avctx->priv_data;
-    s->avctx = avctx;
+    s->avctx          = avctx;
 
     return 0;
 }
@@ -126,17 +126,20 @@
     int *coeffs;
     void *tmp_ptr;
 
-    for (chan=0; chan<s->channels; chan++) {
-        if(FFMAX(1, s->nmean) >= UINT_MAX/sizeof(int32_t)){
+    for (chan = 0; chan < s->channels; chan++) {
+        if (FFMAX(1, s->nmean) >= UINT_MAX / sizeof(int32_t)) {
             av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
             return AVERROR_INVALIDDATA;
         }
-        if(s->blocksize + s->nwrap >= UINT_MAX/sizeof(int32_t) || s->blocksize + s->nwrap <= (unsigned)s->nwrap){
-            av_log(s->avctx, AV_LOG_ERROR, "s->blocksize + s->nwrap too large\n");
+        if (s->blocksize + s->nwrap >= UINT_MAX / sizeof(int32_t) ||
+            s->blocksize + s->nwrap <= (unsigned)s->nwrap) {
+            av_log(s->avctx, AV_LOG_ERROR,
+                   "s->blocksize + s->nwrap too large\n");
             return AVERROR_INVALIDDATA;
         }
 
-        tmp_ptr = av_realloc(s->offset[chan], sizeof(int32_t)*FFMAX(1, s->nmean));
+        tmp_ptr =
+            av_realloc(s->offset[chan], sizeof(int32_t) * FFMAX(1, s->nmean));
         if (!tmp_ptr)
             return AVERROR(ENOMEM);
         s->offset[chan] = tmp_ptr;
@@ -146,7 +149,7 @@
         if (!tmp_ptr)
             return AVERROR(ENOMEM);
         s->decoded_base[chan] = tmp_ptr;
-        for (i=0; i<s->nwrap; i++)
+        for (i = 0; i < s->nwrap; i++)
             s->decoded_base[chan][i] = 0;
         s->decoded[chan] = s->decoded_base[chan] + s->nwrap;
     }
@@ -159,7 +162,6 @@
     return 0;
 }
 
-
 static inline unsigned int get_uint(ShortenContext *s, int k)
 {
     if (s->version != 0)
@@ -167,7 +169,6 @@
     return get_ur_golomb_shorten(&s->gb, k);
 }
 
-
 static void fix_bitshift(ShortenContext *s, int32_t *buffer)
 {
     int i;
@@ -177,26 +178,24 @@
             buffer[i] <<= s->bitshift;
 }
 
-
 static int init_offset(ShortenContext *s)
 {
     int32_t mean = 0;
-    int  chan, i;
+    int chan, i;
     int nblock = FFMAX(1, s->nmean);
     /* initialise offset */
-    switch (s->internal_ftype)
-    {
-        case TYPE_U8:
-            s->avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
-            mean = 0x80;
-            break;
-        case TYPE_S16HL:
-        case TYPE_S16LH:
-            s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
-            break;
-        default:
-            av_log(s->avctx, AV_LOG_ERROR, "unknown audio type\n");
-            return AVERROR_PATCHWELCOME;
+    switch (s->internal_ftype) {
+    case TYPE_U8:
+        s->avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
+        mean = 0x80;
+        break;
+    case TYPE_S16HL:
+    case TYPE_S16LH:
+        s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
+        break;
+    default:
+        av_log(s->avctx, AV_LOG_ERROR, "unknown audio type\n");
+        return AVERROR_PATCHWELCOME;
     }
 
     for (chan = 0; chan < s->channels; chan++)
@@ -212,21 +211,21 @@
     short wave_format;
     const uint8_t *end= header + header_size;
 
-    if (bytestream_get_le32(&header) != MKTAG('R','I','F','F')) {
+    if (bytestream_get_le32(&header) != MKTAG('R', 'I', 'F', 'F')) {
         av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
         return AVERROR_INVALIDDATA;
     }
 
-    header += 4; /* chunk size */;
+    header += 4; /* chunk size */
 
-    if (bytestream_get_le32(&header) != MKTAG('W','A','V','E')) {
+    if (bytestream_get_le32(&header) != MKTAG('W', 'A', 'V', 'E')) {
         av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
         return AVERROR_INVALIDDATA;
     }
 
-    while (bytestream_get_le32(&header) != MKTAG('f','m','t',' ')) {
-        len = bytestream_get_le32(&header);
-        if(len<0 || end - header - 8 < len)
+    while (bytestream_get_le32(&header) != MKTAG('f', 'm', 't', ' ')) {
+        len     = bytestream_get_le32(&header);
+        if (len<0 || end - header - 8 < len)
             return AVERROR_INVALIDDATA;
         header += len;
     }
@@ -240,11 +239,11 @@
     wave_format = bytestream_get_le16(&header);
 
     switch (wave_format) {
-        case WAVE_FORMAT_PCM:
-            break;
-        default:
-            av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
-            return AVERROR_PATCHWELCOME;
+    case WAVE_FORMAT_PCM:
+        break;
+    default:
+        av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
+        return AVERROR(ENOSYS);
     }
 
     header += 2;        // skip channels    (already got from shorten header)
@@ -256,7 +255,7 @@
 
     if (bps != 16 && bps != 8) {
         av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample: %d\n", bps);
-        return AVERROR_INVALIDDATA;
+        return AVERROR(ENOSYS);
     }
 
     len -= 16;
@@ -282,11 +281,12 @@
         /* read/validate prediction order */
         pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
         if (pred_order > s->nwrap) {
-            av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n", pred_order);
+            av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
+                   pred_order);
             return AVERROR(EINVAL);
         }
         /* read LPC coefficients */
-        for (i=0; i<pred_order; i++)
+        for (i = 0; i < pred_order; i++)
             s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
         coeffs = s->coeffs;
 
@@ -294,7 +294,7 @@
     } else {
         /* fixed LPC coeffs */
         pred_order = command;
-        coeffs     = fixed_coeffs[pred_order-1];
+        coeffs     = fixed_coeffs[pred_order - 1];
         qshift     = 0;
     }
 
@@ -305,11 +305,12 @@
 
     /* decode residual and do LPC prediction */
     init_sum = pred_order ? (command == FN_QLPC ? s->lpcqoffset : 0) : coffset;
-    for (i=0; i < s->blocksize; i++) {
+    for (i = 0; i < s->blocksize; i++) {
         sum = init_sum;
-        for (j=0; j<pred_order; j++)
-            sum += coeffs[j] * s->decoded[channel][i-j-1];
-        s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) + (sum >> qshift);
+        for (j = 0; j < pred_order; j++)
+            sum += coeffs[j] * s->decoded[channel][i - j - 1];
+        s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) +
+                                 (sum >> qshift);
     }
 
     /* add offset to current samples */
@@ -330,14 +331,18 @@
         return AVERROR_INVALIDDATA;
     }
 
-    s->lpcqoffset = 0;
-    s->blocksize = DEFAULT_BLOCK_SIZE;
-    s->nmean = -1;
-    s->version = get_bits(&s->gb, 8);
+    s->lpcqoffset     = 0;
+    s->blocksize      = DEFAULT_BLOCK_SIZE;
+    s->nmean          = -1;
+    s->version        = get_bits(&s->gb, 8);
     s->internal_ftype = get_uint(s, TYPESIZE);
 
     s->channels = get_uint(s, CHANSIZE);
-    if (s->channels <= 0 || s->channels > MAX_CHANNELS) {
+    if (!s->channels) {
+        av_log(s->avctx, AV_LOG_ERROR, "No channels reported\n");
+        return AVERROR_INVALIDDATA;
+    }
+    if (s->channels > MAX_CHANNELS) {
         av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
         s->channels = 0;
         return AVERROR_INVALIDDATA;
@@ -346,23 +351,24 @@
 
     /* get blocksize if version > 0 */
     if (s->version > 0) {
-        int skip_bytes, blocksize;
+        int skip_bytes;
+        unsigned blocksize;
 
         blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
-        if (!blocksize || blocksize > (unsigned)MAX_BLOCKSIZE) {
-            av_log(s->avctx, AV_LOG_ERROR, "invalid or unsupported block size: %d\n",
+        if (!blocksize || blocksize > MAX_BLOCKSIZE) {
+            av_log(s->avctx, AV_LOG_ERROR,
+                   "invalid or unsupported block size: %d\n",
                    blocksize);
             return AVERROR(EINVAL);
         }
         s->blocksize = blocksize;
 
-        maxnlpc = get_uint(s, LPCQSIZE);
+        maxnlpc  = get_uint(s, LPCQSIZE);
         s->nmean = get_uint(s, 0);
 
         skip_bytes = get_uint(s, NSKIPSIZE);
-        for (i=0; i<skip_bytes; i++) {
+        for (i = 0; i < skip_bytes; i++)
             skip_bits(&s->gb, 8);
-        }
     }
     s->nwrap = FFMAX(NWRAP, maxnlpc);
 
@@ -376,17 +382,20 @@
         s->lpcqoffset = V2LPCQOFFSET;
 
     if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
-        av_log(s->avctx, AV_LOG_ERROR, "missing verbatim section at beginning of stream\n");
+        av_log(s->avctx, AV_LOG_ERROR,
+               "missing verbatim section at beginning of stream\n");
         return AVERROR_INVALIDDATA;
     }
 
     s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
-    if (s->header_size >= OUT_BUFFER_SIZE || s->header_size < CANONICAL_HEADER_SIZE) {
-        av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n", s->header_size);
+    if (s->header_size >= OUT_BUFFER_SIZE ||
+        s->header_size < CANONICAL_HEADER_SIZE) {
+        av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n",
+               s->header_size);
         return AVERROR_INVALIDDATA;
     }
 
-    for (i=0; i<s->header_size; i++)
+    for (i = 0; i < s->header_size; i++)
         s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
 
     if ((ret = decode_wave_header(s->avctx, s->header, s->header_size)) < 0)
@@ -405,15 +414,15 @@
 {
     AVFrame *frame     = data;
     const uint8_t *buf = avpkt->data;
-    int buf_size = avpkt->size;
-    ShortenContext *s = avctx->priv_data;
+    int buf_size       = avpkt->size;
+    ShortenContext *s  = avctx->priv_data;
     int i, input_buf_size = 0;
     int ret;
 
     /* allocate internal bitstream buffer */
-    if(s->max_framesize == 0){
+    if (s->max_framesize == 0) {
         void *tmp_ptr;
-        s->max_framesize= 8192; // should hopefully be enough for the first header
+        s->max_framesize = 8192; // should hopefully be enough for the first header
         tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
                                   s->max_framesize);
         if (!tmp_ptr) {
@@ -424,29 +433,32 @@
     }
 
     /* append current packet data to bitstream buffer */
-    if(1 && s->max_framesize){//FIXME truncated
-        buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
-        input_buf_size= buf_size;
+    if (1 && s->max_framesize) { //FIXME truncated
+        buf_size       = FFMIN(buf_size, s->max_framesize - s->bitstream_size);
+        input_buf_size = buf_size;
 
-        if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
-            memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
-            s->bitstream_index=0;
+        if (s->bitstream_index + s->bitstream_size + buf_size >
+            s->allocated_bitstream_size) {
+            memmove(s->bitstream, &s->bitstream[s->bitstream_index],
+                    s->bitstream_size);
+            s->bitstream_index = 0;
         }
         if (buf)
-            memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
-        buf= &s->bitstream[s->bitstream_index];
-        buf_size += s->bitstream_size;
-        s->bitstream_size= buf_size;
+            memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf,
+                   buf_size);
+        buf               = &s->bitstream[s->bitstream_index];
+        buf_size         += s->bitstream_size;
+        s->bitstream_size = buf_size;
 
         /* do not decode until buffer has at least max_framesize bytes or
-           the end of the file has been reached */
+         * the end of the file has been reached */
         if (buf_size < s->max_framesize && avpkt->data) {
             *got_frame_ptr = 0;
             return input_buf_size;
         }
     }
     /* init and position bitstream reader */
-    init_get_bits(&s->gb, buf, buf_size*8);
+    init_get_bits(&s->gb, buf, buf_size * 8);
     skip_bits(&s->gb, s->bitindex);
 
     /* process header or next subblock */
@@ -468,7 +480,7 @@
         unsigned cmd;
         int len;
 
-        if (get_bits_left(&s->gb) < 3+FNSIZE) {
+        if (get_bits_left(&s->gb) < 3 + FNSIZE) {
             *got_frame_ptr = 0;
             break;
         }
@@ -484,32 +496,32 @@
         if (!is_audio_command[cmd]) {
             /* process non-audio command */
             switch (cmd) {
-                case FN_VERBATIM:
-                    len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
-                    while (len--) {
-                        get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
-                    }
-                    break;
-                case FN_BITSHIFT:
-                    s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
-                    break;
-                case FN_BLOCKSIZE: {
-                    int blocksize = get_uint(s, av_log2(s->blocksize));
-                    if (blocksize > s->blocksize) {
-                        av_log(avctx, AV_LOG_ERROR, "Increasing block size is not supported\n");
-                        return AVERROR_PATCHWELCOME;
-                    }
-                    if (!blocksize || blocksize > (unsigned)MAX_BLOCKSIZE) {
-                        av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
-                               "block size: %d\n", blocksize);
-                        return AVERROR(EINVAL);
-                    }
-                    s->blocksize = blocksize;
-                    break;
+            case FN_VERBATIM:
+                len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
+                while (len--)
+                    get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
+                break;
+            case FN_BITSHIFT:
+                s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
+                break;
+            case FN_BLOCKSIZE: {
+                unsigned blocksize = get_uint(s, av_log2(s->blocksize));
+                if (blocksize > s->blocksize) {
+                    av_log(avctx, AV_LOG_ERROR,
+                           "Increasing block size is not supported\n");
+                    return AVERROR_PATCHWELCOME;
                 }
-                case FN_QUIT:
-                    s->got_quit_command = 1;
-                    break;
+                if (!blocksize || blocksize > MAX_BLOCKSIZE) {
+                    av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
+                                                "block size: %d\n", blocksize);
+                    return AVERROR(EINVAL);
+                }
+                s->blocksize = blocksize;
+                break;
+            }
+            case FN_QUIT:
+                s->got_quit_command = 1;
+                break;
             }
             if (cmd == FN_BLOCKSIZE || cmd == FN_QUIT) {
                 *got_frame_ptr = 0;
@@ -535,7 +547,7 @@
                 coffset = s->offset[channel][0];
             else {
                 int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
-                for (i=0; i<s->nmean; i++)
+                for (i = 0; i < s->nmean; i++)
                     sum += s->offset[channel][i];
                 coffset = sum / s->nmean;
                 if (s->version >= 2)
@@ -544,21 +556,22 @@
 
             /* decode samples for this channel */
             if (cmd == FN_ZERO) {
-                for (i=0; i<s->blocksize; i++)
+                for (i = 0; i < s->blocksize; i++)
                     s->decoded[channel][i] = 0;
             } else {
-                if ((ret = decode_subframe_lpc(s, cmd, channel, residual_size, coffset)) < 0)
+                if ((ret = decode_subframe_lpc(s, cmd, channel,
+                                               residual_size, coffset)) < 0)
                     return ret;
             }
 
             /* update means with info from the current block */
             if (s->nmean > 0) {
                 int32_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
-                for (i=0; i<s->blocksize; i++)
+                for (i = 0; i < s->blocksize; i++)
                     sum += s->decoded[channel][i];
 
-                for (i=1; i<s->nmean; i++)
-                    s->offset[channel][i-1] = s->offset[channel][i];
+                for (i = 1; i < s->nmean; i++)
+                    s->offset[channel][i - 1] = s->offset[channel][i];
 
                 if (s->version < 2)
                     s->offset[channel][s->nmean - 1] = sum / s->blocksize;
@@ -567,11 +580,11 @@
             }
 
             /* copy wrap samples for use with next block */
-            for (i=-s->nwrap; i<0; i++)
+            for (i = -s->nwrap; i < 0; i++)
                 s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
 
             /* shift samples to add in unused zero bits which were removed
-               during encoding */
+             * during encoding */
             fix_bitshift(s, s->decoded[channel]);
 
             /* if this is the last channel in the block, output the samples */
@@ -583,10 +596,8 @@
 
                 /* get output buffer */
                 frame->nb_samples = s->blocksize;
-                if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-                    av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+                if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
                     return ret;
-                }
 
                 for (chan = 0; chan < s->channels; chan++) {
                     samples_u8  = ((uint8_t **)frame->extended_data)[chan];
@@ -612,12 +623,12 @@
         *got_frame_ptr = 0;
 
 finish_frame:
-    s->bitindex = get_bits_count(&s->gb) - 8*((get_bits_count(&s->gb))/8);
-    i= (get_bits_count(&s->gb))/8;
+    s->bitindex = get_bits_count(&s->gb) - 8 * (get_bits_count(&s->gb) / 8);
+    i           = get_bits_count(&s->gb) / 8;
     if (i > buf_size) {
         av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
-        s->bitstream_size=0;
-        s->bitstream_index=0;
+        s->bitstream_size  = 0;
+        s->bitstream_index = 0;
         return AVERROR_INVALIDDATA;
     }
     if (s->bitstream_size) {
diff --git a/libavcodec/sipr.c b/libavcodec/sipr.c
index dc16936..35e8bf5 100644
--- a/libavcodec/sipr.c
+++ b/libavcodec/sipr.c
@@ -543,10 +543,8 @@
     /* get output buffer */
     frame->nb_samples = mode_par->frames_per_packet * subframe_size *
                         mode_par->subframe_count;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     samples = (float *)frame->data[0];
 
     init_get_bits(&gb, buf, mode_par->bits_per_frame);
diff --git a/libavcodec/smacker.c b/libavcodec/smacker.c
index ad1d4c3..c070150 100644
--- a/libavcodec/smacker.c
+++ b/libavcodec/smacker.c
@@ -381,12 +381,8 @@
     if (avpkt->size <= 769)
         return AVERROR_INVALIDDATA;
 
-    smk->pic.reference = 3;
-    smk->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
-    if((ret = avctx->reget_buffer(avctx, &smk->pic)) < 0){
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, &smk->pic)) < 0)
         return ret;
-    }
 
     /* make the palette available on the way out */
     pal = (uint32_t*)smk->pic.data[1];
@@ -513,8 +509,10 @@
 
     }
 
+    if ((ret = av_frame_ref(data, &smk->pic)) < 0)
+        return ret;
+
     *got_frame = 1;
-    *(AVFrame*)data = smk->pic;
 
     /* always report that the buffer was completely consumed */
     return avpkt->size;
@@ -534,7 +532,6 @@
     c->avctx = avctx;
 
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
-
     avcodec_get_frame_defaults(&c->pic);
 
     /* decode huffman trees from extradata */
@@ -565,8 +562,7 @@
     av_freep(&smk->full_tbl);
     av_freep(&smk->type_tbl);
 
-    if (smk->pic.data[0])
-        avctx->release_buffer(avctx, &smk->pic);
+    av_frame_unref(&smk->pic);
 
     return 0;
 }
@@ -636,10 +632,8 @@
 
     /* get output buffer */
     frame->nb_samples = unp_size / (avctx->channels * (bits + 1));
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     samples  = (int16_t *)frame->data[0];
     samples8 =            frame->data[0];
 
diff --git a/libavcodec/smc.c b/libavcodec/smc.c
index 6294f09..2c7ab9a 100644
--- a/libavcodec/smc.c
+++ b/libavcodec/smc.c
@@ -35,6 +35,7 @@
 #include "libavutil/intreadwrite.h"
 #include "avcodec.h"
 #include "bytestream.h"
+#include "internal.h"
 
 #define CPAIR 2
 #define CQUAD 4
@@ -401,7 +402,7 @@
             break;
 
         case 0xF0:
-            av_log_missing_feature(s->avctx, "0xF0 opcode", 1);
+            avpriv_request_sample(s->avctx, "0xF0 opcode");
             break;
         }
     }
@@ -417,7 +418,6 @@
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
 
     avcodec_get_frame_defaults(&s->frame);
-    s->frame.data[0] = NULL;
 
     return 0;
 }
@@ -430,16 +430,12 @@
     int buf_size = avpkt->size;
     SmcContext *s = avctx->priv_data;
     const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
+    int ret;
 
     bytestream2_init(&s->gb, buf, buf_size);
 
-    s->frame.reference = 3;
-    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
-                            FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
-    if (avctx->reget_buffer(avctx, &s->frame)) {
-        av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
-        return -1;
-    }
+    if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0)
+        return ret;
 
     if (pal) {
         s->frame.palette_has_changed = 1;
@@ -449,7 +445,8 @@
     smc_decode_stream(s);
 
     *got_frame      = 1;
-    *(AVFrame*)data = s->frame;
+    if ((ret = av_frame_ref(data, &s->frame)) < 0)
+        return ret;
 
     /* always report that the buffer was completely consumed */
     return buf_size;
@@ -459,8 +456,7 @@
 {
     SmcContext *s = avctx->priv_data;
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+    av_frame_unref(&s->frame);
 
     return 0;
 }
diff --git a/libavcodec/snow.c b/libavcodec/snow.c
index b2c6714..9f6399c 100644
--- a/libavcodec/snow.c
+++ b/libavcodec/snow.c
@@ -352,8 +352,6 @@
 
         av_assert2(s->chroma_h_shift == s->chroma_v_shift); // only one mv_scale
 
-//        assert(b_w == b_h || 2*b_w == b_h || b_w == 2*b_h);
-//        assert(!(b_w&(b_w-1)));
         av_assert2(b_w>1 && b_h>1);
         av_assert2((tab_index>=0 && tab_index<4) || b_w==32);
         if((dx&3) || (dy&3) || !(b_w == b_h || 2*b_w == b_h || b_w == 2*b_h) || (b_w&(b_w-1)) || !s->plane[plane_index].fast_mc )
@@ -401,6 +399,7 @@
     s->max_ref_frames=1; //just make sure it's not an invalid value in case of no initial keyframe
 
     ff_dsputil_init(&s->dsp, avctx);
+    ff_hpeldsp_init(&s->hdsp, avctx->flags);
     ff_videodsp_init(&s->vdsp, 8);
     ff_dwt_init(&s->dwt);
     ff_h264qpel_init(&s->h264qpel, 8);
@@ -431,11 +430,11 @@
     mcf(12,12)
 
 #define mcfh(dx,dy)\
-    s->dsp.put_pixels_tab       [0][dy/4+dx/8]=\
-    s->dsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
+    s->hdsp.put_pixels_tab       [0][dy/4+dx/8]=\
+    s->hdsp.put_no_rnd_pixels_tab[0][dy/4+dx/8]=\
         mc_block_hpel ## dx ## dy ## 16;\
-    s->dsp.put_pixels_tab       [1][dy/4+dx/8]=\
-    s->dsp.put_no_rnd_pixels_tab[1][dy/4+dx/8]=\
+    s->hdsp.put_pixels_tab       [1][dy/4+dx/8]=\
+    s->hdsp.put_no_rnd_pixels_tab[1][dy/4+dx/8]=\
         mc_block_hpel ## dx ## dy ## 8;
 
     mcfh(0, 0)
@@ -456,9 +455,14 @@
     FF_ALLOCZ_OR_GOTO(avctx, s->temp_idwt_buffer,    width * sizeof(IDWTELEM), fail);
     FF_ALLOC_OR_GOTO(avctx,  s->run_buffer,          ((width + 1) >> 1) * ((height + 1) >> 1) * sizeof(*s->run_buffer), fail);
 
-    for(i=0; i<MAX_REF_FRAMES; i++)
+    for(i=0; i<MAX_REF_FRAMES; i++) {
         for(j=0; j<MAX_REF_FRAMES; j++)
             ff_scale_mv_ref[i][j] = 256*(i+1)/(j+1);
+        avcodec_get_frame_defaults(&s->last_picture[i]);
+    }
+
+    avcodec_get_frame_defaults(&s->mconly_picture);
+    avcodec_get_frame_defaults(&s->current_picture);
 
     return 0;
 fail:
@@ -471,10 +475,9 @@
     int ret, emu_buf_size;
 
     if(!s->scratchbuf) {
-        if ((ret = ff_get_buffer(s->avctx, &s->mconly_picture)) < 0) {
-            av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        if ((ret = ff_get_buffer(s->avctx, &s->mconly_picture,
+                                 AV_GET_BUFFER_FLAG_REF)) < 0)
             return ret;
-        }
         FF_ALLOCZ_OR_GOTO(avctx, s->scratchbuf, FFMAX(s->mconly_picture.linesize[0], 2*avctx->width+256)*7*MB_SIZE, fail);
         emu_buf_size = FFMAX(s->mconly_picture.linesize[0], 2*avctx->width+256) * (2 * MB_SIZE + HTAPS_MAX - 1);
         FF_ALLOC_OR_GOTO(avctx, s->emu_edge_buffer, emu_buf_size, fail);
@@ -586,7 +589,7 @@
     int i;
 
     if(s->last_picture[s->max_ref_frames-1].data[0]){
-        avctx->release_buffer(avctx, &s->last_picture[s->max_ref_frames-1]);
+        av_frame_unref(&s->last_picture[s->max_ref_frames-1]);
         for(i=0; i<9; i++)
             if(s->halfpel_plane[s->max_ref_frames-1][1+i/3][i%3])
                 av_free(s->halfpel_plane[s->max_ref_frames-1][1+i/3][i%3] - EDGE_WIDTH*(1+s->current_picture.linesize[i%3]));
@@ -595,6 +598,7 @@
 
 int ff_snow_frame_start(SnowContext *s){
    AVFrame tmp;
+   int i, ret;
    int w= s->avctx->width; //FIXME round up to x16 ?
    int h= s->avctx->height;
 
@@ -612,13 +616,14 @@
 
     ff_snow_release_buffer(s->avctx);
 
-    tmp= s->last_picture[s->max_ref_frames-1];
-    memmove(s->last_picture+1, s->last_picture, (s->max_ref_frames-1)*sizeof(AVFrame));
+    av_frame_move_ref(&tmp, &s->last_picture[s->max_ref_frames-1]);
+    for(i=s->max_ref_frames-1; i>0; i--)
+        av_frame_move_ref(&s->last_picture[i+1], &s->last_picture[i]);
     memmove(s->halfpel_plane+1, s->halfpel_plane, (s->max_ref_frames-1)*sizeof(void*)*4*4);
     if(USE_HALFPEL_PLANE && s->current_picture.data[0])
         halfpel_interpol(s, s->halfpel_plane[0], &s->current_picture);
-    s->last_picture[0]= s->current_picture;
-    s->current_picture= tmp;
+    av_frame_move_ref(&s->last_picture[0], &s->current_picture);
+    av_frame_move_ref(&s->current_picture, &tmp);
 
     if(s->keyframe){
         s->ref_frames= 0;
@@ -634,11 +639,8 @@
         }
     }
 
-    s->current_picture.reference= 3;
-    if(ff_get_buffer(s->avctx, &s->current_picture) < 0){
-        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
-        return -1;
-    }
+    if ((ret = ff_get_buffer(s->avctx, &s->current_picture, AV_GET_BUFFER_FLAG_REF)) < 0)
+        return ret;
 
     s->current_picture.key_frame= s->keyframe;
 
@@ -670,7 +672,7 @@
         av_freep(&s->ref_scores[i]);
         if(s->last_picture[i].data[0]) {
             av_assert0(s->last_picture[i].data[0] != s->current_picture.data[0]);
-            s->avctx->release_buffer(s->avctx, &s->last_picture[i]);
+            av_frame_unref(&s->last_picture[i]);
         }
     }
 
@@ -683,8 +685,6 @@
             }
         }
     }
-    if (s->mconly_picture.data[0])
-        s->avctx->release_buffer(s->avctx, &s->mconly_picture);
-    if (s->current_picture.data[0])
-        s->avctx->release_buffer(s->avctx, &s->current_picture);
+    av_frame_unref(&s->mconly_picture);
+    av_frame_unref(&s->current_picture);
 }
diff --git a/libavcodec/snow.h b/libavcodec/snow.h
index a3b0512..922a48e 100644
--- a/libavcodec/snow.h
+++ b/libavcodec/snow.h
@@ -23,6 +23,7 @@
 #define AVCODEC_SNOW_H
 
 #include "dsputil.h"
+#include "hpeldsp.h"
 #include "snow_dwt.h"
 
 #include "rangecoder.h"
@@ -109,6 +110,7 @@
     AVCodecContext *avctx;
     RangeCoder c;
     DSPContext dsp;
+    HpelDSPContext hdsp;
     VideoDSPContext vdsp;
     H264QpelContext h264qpel;
     SnowDWTContext dwt;
diff --git a/libavcodec/snowdec.c b/libavcodec/snowdec.c
index e211506..c8a0327 100644
--- a/libavcodec/snowdec.c
+++ b/libavcodec/snowdec.c
@@ -345,7 +345,7 @@
         return -1;
     }
     if(FFMIN(s->avctx-> width>>s->chroma_h_shift,
-             s->avctx->height>>s->chroma_v_shift) >> (s->spatial_decomposition_count-1) <= 0){
+             s->avctx->height>>s->chroma_v_shift) >> (s->spatial_decomposition_count-1) <= 1){
         av_log(s->avctx, AV_LOG_ERROR, "spatial_decomposition_count %d too large for size\n", s->spatial_decomposition_count);
         return -1;
     }
@@ -551,9 +551,9 @@
     ff_snow_release_buffer(avctx);
 
     if(!(s->avctx->debug&2048))
-        *picture= s->current_picture;
+        av_frame_ref(picture, &s->current_picture);
     else
-        *picture= s->mconly_picture;
+        av_frame_ref(picture, &s->mconly_picture);
 
     *got_frame = 1;
 
diff --git a/libavcodec/snowenc.c b/libavcodec/snowenc.c
index 1831cf6..7266ee1 100644
--- a/libavcodec/snowenc.c
+++ b/libavcodec/snowenc.c
@@ -239,7 +239,7 @@
     ff_set_cmp(&s->dsp, s->dsp.me_cmp, s->avctx->me_cmp);
     ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp);
 
-    if ((ret = ff_get_buffer(s->avctx, &s->input_picture)) < 0)
+    if ((ret = ff_get_buffer(s->avctx, &s->input_picture, AV_GET_BUFFER_FLAG_REF)) < 0)
         return ret;
 
     if(s->avctx->me_method == ME_ITER){
@@ -1671,7 +1671,13 @@
             memcpy(&s->input_picture.data[i][y * s->input_picture.linesize[i]],
                    &pict->data[i][y * pict->linesize[i]],
                    width>>hshift);
+        s->dsp.draw_edges(s->input_picture.data[i], s->input_picture.linesize[i],
+                            width >> hshift, height >> vshift,
+                            EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
+                            EDGE_TOP | EDGE_BOTTOM);
+
     }
+    emms_c();
     s->new_picture = *pict;
 
     s->m.picture_number= avctx->frame_number;
@@ -1742,7 +1748,9 @@
         s->lambda2= s->m.lambda2= (s->m.lambda*s->m.lambda + FF_LAMBDA_SCALE/2) >> FF_LAMBDA_SHIFT;
 
         s->m.dsp= s->dsp; //move
+        s->m.hdsp = s->hdsp;
         ff_init_me(&s->m);
+        s->hdsp = s->m.hdsp;
         s->dsp= s->m.dsp;
     }
 
@@ -1942,8 +1950,7 @@
     SnowContext *s = avctx->priv_data;
 
     ff_snow_common_end(s);
-    if (s->input_picture.data[0])
-        avctx->release_buffer(avctx, &s->input_picture);
+    av_frame_unref(&s->input_picture);
     av_free(avctx->stats_out);
 
     return 0;
diff --git a/libavcodec/sonic.c b/libavcodec/sonic.c
index 0bc8428..b67434f 100644
--- a/libavcodec/sonic.c
+++ b/libavcodec/sonic.c
@@ -45,7 +45,6 @@
 #define RIGHT_SIDE 2
 
 typedef struct SonicContext {
-    AVFrame frame;
     int lossless, decorrelation;
 
     int num_taps, downsampling;
@@ -513,6 +512,8 @@
 
     if (avctx->channels == 2)
         s->decorrelation = MID_SIDE;
+    else
+        s->decorrelation = 3;
 
     if (avctx->codec->id == AV_CODEC_ID_SONIC_LS)
     {
@@ -632,7 +633,7 @@
     int ret;
     const short *samples = (const int16_t*)frame->data[0];
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, s->frame_size * 5 + 1000)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, s->frame_size * 5 + 1000)) < 0)
         return ret;
 
     init_put_bits(&pb, avpkt->data, avpkt->size);
@@ -765,9 +766,6 @@
     s->channels = avctx->channels;
     s->samplerate = avctx->sample_rate;
 
-    avcodec_get_frame_defaults(&s->frame);
-    avctx->coded_frame = &s->frame;
-
     if (!avctx->extradata)
     {
         av_log(avctx, AV_LOG_ERROR, "No mandatory headers present\n");
@@ -801,6 +799,10 @@
     if (!s->lossless)
         skip_bits(&gb, 3); // XXX FIXME
     s->decorrelation = get_bits(&gb, 2);
+    if (s->decorrelation != 3 && s->channels != 2) {
+        av_log(avctx, AV_LOG_ERROR, "invalid decorrelation %d\n", s->decorrelation);
+        return AVERROR_INVALIDDATA;
+    }
 
     s->downsampling = get_bits(&gb, 2);
     if (!s->downsampling) {
@@ -873,15 +875,14 @@
     GetBitContext gb;
     int i, quant, ch, j, ret;
     int16_t *samples;
+    AVFrame *frame = data;
 
     if (buf_size == 0) return 0;
 
-    s->frame.nb_samples = s->frame_size;
-    if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    frame->nb_samples = s->frame_size / avctx->channels;
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
-    samples = (int16_t *)s->frame.data[0];
+    samples = (int16_t *)frame->data[0];
 
 //    av_log(NULL, AV_LOG_INFO, "buf_size: %d\n", buf_size);
 
@@ -954,7 +955,6 @@
     align_get_bits(&gb);
 
     *got_frame_ptr = 1;
-    *(AVFrame*)data = s->frame;
 
     return (get_bits_count(&gb)+7)/8;
 }
diff --git a/libavcodec/sparc/Makefile b/libavcodec/sparc/Makefile
index 1b6ac81..aeb0f47 100644
--- a/libavcodec/sparc/Makefile
+++ b/libavcodec/sparc/Makefile
@@ -1,2 +1,4 @@
 VIS-OBJS += sparc/dsputil_vis.o                                         \
             sparc/simple_idct_vis.o                                     \
+
+VIS-OBJS-$(CONFIG_HPELDSP) += sparc/hpeldsp_vis.o
diff --git a/libavcodec/sparc/dsputil_vis.c b/libavcodec/sparc/dsputil_vis.c
index 0ade237..414d6ca 100644
--- a/libavcodec/sparc/dsputil_vis.c
+++ b/libavcodec/sparc/dsputil_vis.c
@@ -18,3483 +18,11 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-/* The *no_round* functions have been added by James A. Morrison, 2003,2004.
-   The vis code from libmpeg2 was adapted for libavcodec by James A. Morrison.
- */
-
-#include "config.h"
-
-#include <inttypes.h>
-
 #include "libavutil/attributes.h"
 #include "libavcodec/dsputil.h"
-#include "libavutil/mem.h"
 #include "dsputil_vis.h"
-
 #include "vis.h"
 
-/* The trick used in some of this file is the formula from the MMX
- * motion comp code, which is:
- *
- * (x+y+1)>>1 == (x|y)-((x^y)>>1)
- *
- * This allows us to average 8 bytes at a time in a 64-bit FPU reg.
- * We avoid overflows by masking before we do the shift, and we
- * implement the shift by multiplying by 1/2 using mul8x16.  So in
- * VIS this is (assume 'x' is in f0, 'y' is in f2, a repeating mask
- * of '0xfe' is in f4, a repeating mask of '0x7f' is in f6, and
- * the value 0x80808080 is in f8):
- *
- *      fxor            f0,   f2, f10
- *      fand            f10,  f4, f10
- *      fmul8x16        f8,  f10, f10
- *      fand            f10,  f6, f10
- *      for             f0,   f2, f12
- *      fpsub16         f12, f10, f10
- */
-
-#define DUP4(x) {x, x, x, x}
-#define DUP8(x) {x, x, x, x, x, x, x, x}
-DECLARE_ALIGNED(8, static const int16_t, constants1)[] = DUP4 (1);
-DECLARE_ALIGNED(8, static const int16_t, constants2)[] = DUP4 (2);
-DECLARE_ALIGNED(8, static const int16_t, constants3)[] = DUP4 (3);
-DECLARE_ALIGNED(8, static const int16_t, constants6)[] = DUP4 (6);
-DECLARE_ALIGNED(8, static const int8_t, constants_fe)[] = DUP8 (0xfe);
-DECLARE_ALIGNED(8, static const int8_t, constants_7f)[] = DUP8 (0x7f);
-DECLARE_ALIGNED(8, static const int8_t, constants128)[] = DUP8 (128);
-DECLARE_ALIGNED(8, static const int16_t, constants256_512)[] =
-        {256, 512, 256, 512};
-DECLARE_ALIGNED(8, static const int16_t, constants256_1024)[] =
-        {256, 1024, 256, 1024};
-
-#define REF_0           0
-#define REF_0_1         1
-#define REF_2           2
-#define REF_2_1         3
-#define REF_4           4
-#define REF_4_1         5
-#define REF_6           6
-#define REF_6_1         7
-#define REF_S0          8
-#define REF_S0_1        9
-#define REF_S2          10
-#define REF_S2_1        11
-#define REF_S4          12
-#define REF_S4_1        13
-#define REF_S6          14
-#define REF_S6_1        15
-#define DST_0           16
-#define DST_1           17
-#define DST_2           18
-#define DST_3           19
-#define CONST_1         20
-#define CONST_2         20
-#define CONST_3         20
-#define CONST_6         20
-#define MASK_fe         20
-#define CONST_128       22
-#define CONST_256       22
-#define CONST_512       22
-#define CONST_1024      22
-#define TMP0            24
-#define TMP1            25
-#define TMP2            26
-#define TMP3            27
-#define TMP4            28
-#define TMP5            29
-#define ZERO            30
-#define MASK_7f         30
-
-#define TMP6            32
-#define TMP8            34
-#define TMP10           36
-#define TMP12           38
-#define TMP14           40
-#define TMP16           42
-#define TMP18           44
-#define TMP20           46
-#define TMP22           48
-#define TMP24           50
-#define TMP26           52
-#define TMP28           54
-#define TMP30           56
-#define TMP32           58
-
-static void MC_put_o_16_vis (uint8_t * dest, const uint8_t * ref,
-                             const ptrdiff_t stride, int height)
-{
-        ref = vis_alignaddr(ref);
-        do {    /* 5 cycles */
-                vis_ld64(ref[0], TMP0);
-
-                vis_ld64_2(ref, 8, TMP2);
-
-                vis_ld64_2(ref, 16, TMP4);
-                ref += stride;
-
-                vis_faligndata(TMP0, TMP2, REF_0);
-                vis_st64(REF_0, dest[0]);
-
-                vis_faligndata(TMP2, TMP4, REF_2);
-                vis_st64_2(REF_2, dest, 8);
-                dest += stride;
-        } while (--height);
-}
-
-static void MC_put_o_8_vis (uint8_t * dest, const uint8_t * ref,
-                            const ptrdiff_t stride, int height)
-{
-        ref = vis_alignaddr(ref);
-        do {    /* 4 cycles */
-                vis_ld64(ref[0], TMP0);
-
-                vis_ld64(ref[8], TMP2);
-                ref += stride;
-
-                /* stall */
-
-                vis_faligndata(TMP0, TMP2, REF_0);
-                vis_st64(REF_0, dest[0]);
-                dest += stride;
-        } while (--height);
-}
-
-
-static void MC_avg_o_16_vis (uint8_t * dest, const uint8_t * ref,
-                             const ptrdiff_t stride, int height)
-{
-        int stride_8 = stride + 8;
-
-        ref = vis_alignaddr(ref);
-
-        vis_ld64(ref[0], TMP0);
-
-        vis_ld64(ref[8], TMP2);
-
-        vis_ld64(ref[16], TMP4);
-
-        vis_ld64(dest[0], DST_0);
-
-        vis_ld64(dest[8], DST_2);
-
-        vis_ld64(constants_fe[0], MASK_fe);
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        vis_ld64(constants_7f[0], MASK_7f);
-        vis_faligndata(TMP2, TMP4, REF_2);
-
-        vis_ld64(constants128[0], CONST_128);
-
-        ref += stride;
-        height = (height >> 1) - 1;
-
-        do {    /* 24 cycles */
-                vis_ld64(ref[0], TMP0);
-                vis_xor(DST_0, REF_0, TMP6);
-
-                vis_ld64_2(ref, 8, TMP2);
-                vis_and(TMP6, MASK_fe, TMP6);
-
-                vis_ld64_2(ref, 16, TMP4);
-                ref += stride;
-                vis_mul8x16(CONST_128, TMP6, TMP6);
-                vis_xor(DST_2, REF_2, TMP8);
-
-                vis_and(TMP8, MASK_fe, TMP8);
-
-                vis_or(DST_0, REF_0, TMP10);
-                vis_ld64_2(dest, stride, DST_0);
-                vis_mul8x16(CONST_128, TMP8, TMP8);
-
-                vis_or(DST_2, REF_2, TMP12);
-                vis_ld64_2(dest, stride_8, DST_2);
-
-                vis_ld64(ref[0], TMP14);
-                vis_and(TMP6, MASK_7f, TMP6);
-
-                vis_and(TMP8, MASK_7f, TMP8);
-
-                vis_psub16(TMP10, TMP6, TMP6);
-                vis_st64(TMP6, dest[0]);
-
-                vis_psub16(TMP12, TMP8, TMP8);
-                vis_st64_2(TMP8, dest, 8);
-
-                dest += stride;
-                vis_ld64_2(ref, 8, TMP16);
-                vis_faligndata(TMP0, TMP2, REF_0);
-
-                vis_ld64_2(ref, 16, TMP18);
-                vis_faligndata(TMP2, TMP4, REF_2);
-                ref += stride;
-
-                vis_xor(DST_0, REF_0, TMP20);
-
-                vis_and(TMP20, MASK_fe, TMP20);
-
-                vis_xor(DST_2, REF_2, TMP22);
-                vis_mul8x16(CONST_128, TMP20, TMP20);
-
-                vis_and(TMP22, MASK_fe, TMP22);
-
-                vis_or(DST_0, REF_0, TMP24);
-                vis_mul8x16(CONST_128, TMP22, TMP22);
-
-                vis_or(DST_2, REF_2, TMP26);
-
-                vis_ld64_2(dest, stride, DST_0);
-                vis_faligndata(TMP14, TMP16, REF_0);
-
-                vis_ld64_2(dest, stride_8, DST_2);
-                vis_faligndata(TMP16, TMP18, REF_2);
-
-                vis_and(TMP20, MASK_7f, TMP20);
-
-                vis_and(TMP22, MASK_7f, TMP22);
-
-                vis_psub16(TMP24, TMP20, TMP20);
-                vis_st64(TMP20, dest[0]);
-
-                vis_psub16(TMP26, TMP22, TMP22);
-                vis_st64_2(TMP22, dest, 8);
-                dest += stride;
-        } while (--height);
-
-        vis_ld64(ref[0], TMP0);
-        vis_xor(DST_0, REF_0, TMP6);
-
-        vis_ld64_2(ref, 8, TMP2);
-        vis_and(TMP6, MASK_fe, TMP6);
-
-        vis_ld64_2(ref, 16, TMP4);
-        vis_mul8x16(CONST_128, TMP6, TMP6);
-        vis_xor(DST_2, REF_2, TMP8);
-
-        vis_and(TMP8, MASK_fe, TMP8);
-
-        vis_or(DST_0, REF_0, TMP10);
-        vis_ld64_2(dest, stride, DST_0);
-        vis_mul8x16(CONST_128, TMP8, TMP8);
-
-        vis_or(DST_2, REF_2, TMP12);
-        vis_ld64_2(dest, stride_8, DST_2);
-
-        vis_ld64(ref[0], TMP14);
-        vis_and(TMP6, MASK_7f, TMP6);
-
-        vis_and(TMP8, MASK_7f, TMP8);
-
-        vis_psub16(TMP10, TMP6, TMP6);
-        vis_st64(TMP6, dest[0]);
-
-        vis_psub16(TMP12, TMP8, TMP8);
-        vis_st64_2(TMP8, dest, 8);
-
-        dest += stride;
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        vis_faligndata(TMP2, TMP4, REF_2);
-
-        vis_xor(DST_0, REF_0, TMP20);
-
-        vis_and(TMP20, MASK_fe, TMP20);
-
-        vis_xor(DST_2, REF_2, TMP22);
-        vis_mul8x16(CONST_128, TMP20, TMP20);
-
-        vis_and(TMP22, MASK_fe, TMP22);
-
-        vis_or(DST_0, REF_0, TMP24);
-        vis_mul8x16(CONST_128, TMP22, TMP22);
-
-        vis_or(DST_2, REF_2, TMP26);
-
-        vis_and(TMP20, MASK_7f, TMP20);
-
-        vis_and(TMP22, MASK_7f, TMP22);
-
-        vis_psub16(TMP24, TMP20, TMP20);
-        vis_st64(TMP20, dest[0]);
-
-        vis_psub16(TMP26, TMP22, TMP22);
-        vis_st64_2(TMP22, dest, 8);
-}
-
-static void MC_avg_o_8_vis (uint8_t * dest, const uint8_t * ref,
-                            const ptrdiff_t stride, int height)
-{
-        ref = vis_alignaddr(ref);
-
-        vis_ld64(ref[0], TMP0);
-
-        vis_ld64(ref[8], TMP2);
-
-        vis_ld64(dest[0], DST_0);
-
-        vis_ld64(constants_fe[0], MASK_fe);
-
-        vis_ld64(constants_7f[0], MASK_7f);
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        vis_ld64(constants128[0], CONST_128);
-
-        ref += stride;
-        height = (height >> 1) - 1;
-
-        do {    /* 12 cycles */
-                vis_ld64(ref[0], TMP0);
-                vis_xor(DST_0, REF_0, TMP4);
-
-                vis_ld64(ref[8], TMP2);
-                vis_and(TMP4, MASK_fe, TMP4);
-
-                vis_or(DST_0, REF_0, TMP6);
-                vis_ld64_2(dest, stride, DST_0);
-                ref += stride;
-                vis_mul8x16(CONST_128, TMP4, TMP4);
-
-                vis_ld64(ref[0], TMP12);
-                vis_faligndata(TMP0, TMP2, REF_0);
-
-                vis_ld64(ref[8], TMP2);
-                vis_xor(DST_0, REF_0, TMP0);
-                ref += stride;
-
-                vis_and(TMP0, MASK_fe, TMP0);
-
-                vis_and(TMP4, MASK_7f, TMP4);
-
-                vis_psub16(TMP6, TMP4, TMP4);
-                vis_st64(TMP4, dest[0]);
-                dest += stride;
-                vis_mul8x16(CONST_128, TMP0, TMP0);
-
-                vis_or(DST_0, REF_0, TMP6);
-                vis_ld64_2(dest, stride, DST_0);
-
-                vis_faligndata(TMP12, TMP2, REF_0);
-
-                vis_and(TMP0, MASK_7f, TMP0);
-
-                vis_psub16(TMP6, TMP0, TMP4);
-                vis_st64(TMP4, dest[0]);
-                dest += stride;
-        } while (--height);
-
-        vis_ld64(ref[0], TMP0);
-        vis_xor(DST_0, REF_0, TMP4);
-
-        vis_ld64(ref[8], TMP2);
-        vis_and(TMP4, MASK_fe, TMP4);
-
-        vis_or(DST_0, REF_0, TMP6);
-        vis_ld64_2(dest, stride, DST_0);
-        vis_mul8x16(CONST_128, TMP4, TMP4);
-
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        vis_xor(DST_0, REF_0, TMP0);
-
-        vis_and(TMP0, MASK_fe, TMP0);
-
-        vis_and(TMP4, MASK_7f, TMP4);
-
-        vis_psub16(TMP6, TMP4, TMP4);
-        vis_st64(TMP4, dest[0]);
-        dest += stride;
-        vis_mul8x16(CONST_128, TMP0, TMP0);
-
-        vis_or(DST_0, REF_0, TMP6);
-
-        vis_and(TMP0, MASK_7f, TMP0);
-
-        vis_psub16(TMP6, TMP0, TMP4);
-        vis_st64(TMP4, dest[0]);
-}
-
-static void MC_put_x_16_vis (uint8_t * dest, const uint8_t * ref,
-                             const ptrdiff_t stride, int height)
-{
-        unsigned long off = (unsigned long) ref & 0x7;
-        unsigned long off_plus_1 = off + 1;
-
-        ref = vis_alignaddr(ref);
-
-        vis_ld64(ref[0],    TMP0);
-
-        vis_ld64_2(ref, 8,  TMP2);
-
-        vis_ld64_2(ref, 16, TMP4);
-
-        vis_ld64(constants_fe[0], MASK_fe);
-
-        vis_ld64(constants_7f[0], MASK_7f);
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        vis_ld64(constants128[0], CONST_128);
-        vis_faligndata(TMP2, TMP4, REF_4);
-
-        if (off != 0x7) {
-                vis_alignaddr_g0((void *)off_plus_1);
-                vis_faligndata(TMP0, TMP2, REF_2);
-                vis_faligndata(TMP2, TMP4, REF_6);
-        } else {
-                vis_src1(TMP2, REF_2);
-                vis_src1(TMP4, REF_6);
-        }
-
-        ref += stride;
-        height = (height >> 1) - 1;
-
-        do {    /* 34 cycles */
-                vis_ld64(ref[0],    TMP0);
-                vis_xor(REF_0, REF_2, TMP6);
-
-                vis_ld64_2(ref, 8,  TMP2);
-                vis_xor(REF_4, REF_6, TMP8);
-
-                vis_ld64_2(ref, 16, TMP4);
-                vis_and(TMP6, MASK_fe, TMP6);
-                ref += stride;
-
-                vis_ld64(ref[0],    TMP14);
-                vis_mul8x16(CONST_128, TMP6, TMP6);
-                vis_and(TMP8, MASK_fe, TMP8);
-
-                vis_ld64_2(ref, 8,  TMP16);
-                vis_mul8x16(CONST_128, TMP8, TMP8);
-                vis_or(REF_0, REF_2, TMP10);
-
-                vis_ld64_2(ref, 16, TMP18);
-                ref += stride;
-                vis_or(REF_4, REF_6, TMP12);
-
-                vis_alignaddr_g0((void *)off);
-
-                vis_faligndata(TMP0, TMP2, REF_0);
-
-                vis_faligndata(TMP2, TMP4, REF_4);
-
-                if (off != 0x7) {
-                        vis_alignaddr_g0((void *)off_plus_1);
-                        vis_faligndata(TMP0, TMP2, REF_2);
-                        vis_faligndata(TMP2, TMP4, REF_6);
-                } else {
-                        vis_src1(TMP2, REF_2);
-                        vis_src1(TMP4, REF_6);
-                }
-
-                vis_and(TMP6, MASK_7f, TMP6);
-
-                vis_and(TMP8, MASK_7f, TMP8);
-
-                vis_psub16(TMP10, TMP6, TMP6);
-                vis_st64(TMP6, dest[0]);
-
-                vis_psub16(TMP12, TMP8, TMP8);
-                vis_st64_2(TMP8, dest, 8);
-                dest += stride;
-
-                vis_xor(REF_0, REF_2, TMP6);
-
-                vis_xor(REF_4, REF_6, TMP8);
-
-                vis_and(TMP6, MASK_fe, TMP6);
-
-                vis_mul8x16(CONST_128, TMP6, TMP6);
-                vis_and(TMP8, MASK_fe, TMP8);
-
-                vis_mul8x16(CONST_128, TMP8, TMP8);
-                vis_or(REF_0, REF_2, TMP10);
-
-                vis_or(REF_4, REF_6, TMP12);
-
-                vis_alignaddr_g0((void *)off);
-
-                vis_faligndata(TMP14, TMP16, REF_0);
-
-                vis_faligndata(TMP16, TMP18, REF_4);
-
-                if (off != 0x7) {
-                        vis_alignaddr_g0((void *)off_plus_1);
-                        vis_faligndata(TMP14, TMP16, REF_2);
-                        vis_faligndata(TMP16, TMP18, REF_6);
-                } else {
-                        vis_src1(TMP16, REF_2);
-                        vis_src1(TMP18, REF_6);
-                }
-
-                vis_and(TMP6, MASK_7f, TMP6);
-
-                vis_and(TMP8, MASK_7f, TMP8);
-
-                vis_psub16(TMP10, TMP6, TMP6);
-                vis_st64(TMP6, dest[0]);
-
-                vis_psub16(TMP12, TMP8, TMP8);
-                vis_st64_2(TMP8, dest, 8);
-                dest += stride;
-        } while (--height);
-
-        vis_ld64(ref[0],    TMP0);
-        vis_xor(REF_0, REF_2, TMP6);
-
-        vis_ld64_2(ref, 8,  TMP2);
-        vis_xor(REF_4, REF_6, TMP8);
-
-        vis_ld64_2(ref, 16, TMP4);
-        vis_and(TMP6, MASK_fe, TMP6);
-
-        vis_mul8x16(CONST_128, TMP6, TMP6);
-        vis_and(TMP8, MASK_fe, TMP8);
-
-        vis_mul8x16(CONST_128, TMP8, TMP8);
-        vis_or(REF_0, REF_2, TMP10);
-
-        vis_or(REF_4, REF_6, TMP12);
-
-        vis_alignaddr_g0((void *)off);
-
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        vis_faligndata(TMP2, TMP4, REF_4);
-
-        if (off != 0x7) {
-                vis_alignaddr_g0((void *)off_plus_1);
-                vis_faligndata(TMP0, TMP2, REF_2);
-                vis_faligndata(TMP2, TMP4, REF_6);
-        } else {
-                vis_src1(TMP2, REF_2);
-                vis_src1(TMP4, REF_6);
-        }
-
-        vis_and(TMP6, MASK_7f, TMP6);
-
-        vis_and(TMP8, MASK_7f, TMP8);
-
-        vis_psub16(TMP10, TMP6, TMP6);
-        vis_st64(TMP6, dest[0]);
-
-        vis_psub16(TMP12, TMP8, TMP8);
-        vis_st64_2(TMP8, dest, 8);
-        dest += stride;
-
-        vis_xor(REF_0, REF_2, TMP6);
-
-        vis_xor(REF_4, REF_6, TMP8);
-
-        vis_and(TMP6, MASK_fe, TMP6);
-
-        vis_mul8x16(CONST_128, TMP6, TMP6);
-        vis_and(TMP8, MASK_fe, TMP8);
-
-        vis_mul8x16(CONST_128, TMP8, TMP8);
-        vis_or(REF_0, REF_2, TMP10);
-
-        vis_or(REF_4, REF_6, TMP12);
-
-        vis_and(TMP6, MASK_7f, TMP6);
-
-        vis_and(TMP8, MASK_7f, TMP8);
-
-        vis_psub16(TMP10, TMP6, TMP6);
-        vis_st64(TMP6, dest[0]);
-
-        vis_psub16(TMP12, TMP8, TMP8);
-        vis_st64_2(TMP8, dest, 8);
-}
-
-static void MC_put_x_8_vis (uint8_t * dest, const uint8_t * ref,
-                            const ptrdiff_t stride, int height)
-{
-        unsigned long off = (unsigned long) ref & 0x7;
-        unsigned long off_plus_1 = off + 1;
-
-        ref = vis_alignaddr(ref);
-
-        vis_ld64(ref[0], TMP0);
-
-        vis_ld64(ref[8], TMP2);
-
-        vis_ld64(constants_fe[0], MASK_fe);
-
-        vis_ld64(constants_7f[0], MASK_7f);
-
-        vis_ld64(constants128[0], CONST_128);
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        if (off != 0x7) {
-                vis_alignaddr_g0((void *)off_plus_1);
-                vis_faligndata(TMP0, TMP2, REF_2);
-        } else {
-                vis_src1(TMP2, REF_2);
-        }
-
-        ref += stride;
-        height = (height >> 1) - 1;
-
-        do {    /* 20 cycles */
-                vis_ld64(ref[0], TMP0);
-                vis_xor(REF_0, REF_2, TMP4);
-
-                vis_ld64_2(ref, 8, TMP2);
-                vis_and(TMP4, MASK_fe, TMP4);
-                ref += stride;
-
-                vis_ld64(ref[0], TMP8);
-                vis_or(REF_0, REF_2, TMP6);
-                vis_mul8x16(CONST_128, TMP4, TMP4);
-
-                vis_alignaddr_g0((void *)off);
-
-                vis_ld64_2(ref, 8, TMP10);
-                ref += stride;
-                vis_faligndata(TMP0, TMP2, REF_0);
-
-                if (off != 0x7) {
-                        vis_alignaddr_g0((void *)off_plus_1);
-                        vis_faligndata(TMP0, TMP2, REF_2);
-                } else {
-                        vis_src1(TMP2, REF_2);
-                }
-
-                vis_and(TMP4, MASK_7f, TMP4);
-
-                vis_psub16(TMP6, TMP4, DST_0);
-                vis_st64(DST_0, dest[0]);
-                dest += stride;
-
-                vis_xor(REF_0, REF_2, TMP12);
-
-                vis_and(TMP12, MASK_fe, TMP12);
-
-                vis_or(REF_0, REF_2, TMP14);
-                vis_mul8x16(CONST_128, TMP12, TMP12);
-
-                vis_alignaddr_g0((void *)off);
-                vis_faligndata(TMP8, TMP10, REF_0);
-                if (off != 0x7) {
-                        vis_alignaddr_g0((void *)off_plus_1);
-                        vis_faligndata(TMP8, TMP10, REF_2);
-                } else {
-                        vis_src1(TMP10, REF_2);
-                }
-
-                vis_and(TMP12, MASK_7f, TMP12);
-
-                vis_psub16(TMP14, TMP12, DST_0);
-                vis_st64(DST_0, dest[0]);
-                dest += stride;
-        } while (--height);
-
-        vis_ld64(ref[0], TMP0);
-        vis_xor(REF_0, REF_2, TMP4);
-
-        vis_ld64_2(ref, 8, TMP2);
-        vis_and(TMP4, MASK_fe, TMP4);
-
-        vis_or(REF_0, REF_2, TMP6);
-        vis_mul8x16(CONST_128, TMP4, TMP4);
-
-        vis_alignaddr_g0((void *)off);
-
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        if (off != 0x7) {
-                vis_alignaddr_g0((void *)off_plus_1);
-                vis_faligndata(TMP0, TMP2, REF_2);
-        } else {
-                vis_src1(TMP2, REF_2);
-        }
-
-        vis_and(TMP4, MASK_7f, TMP4);
-
-        vis_psub16(TMP6, TMP4, DST_0);
-        vis_st64(DST_0, dest[0]);
-        dest += stride;
-
-        vis_xor(REF_0, REF_2, TMP12);
-
-        vis_and(TMP12, MASK_fe, TMP12);
-
-        vis_or(REF_0, REF_2, TMP14);
-        vis_mul8x16(CONST_128, TMP12, TMP12);
-
-        vis_and(TMP12, MASK_7f, TMP12);
-
-        vis_psub16(TMP14, TMP12, DST_0);
-        vis_st64(DST_0, dest[0]);
-        dest += stride;
-}
-
-static void MC_avg_x_16_vis (uint8_t * dest, const uint8_t * ref,
-                             const ptrdiff_t stride, int height)
-{
-        unsigned long off = (unsigned long) ref & 0x7;
-        unsigned long off_plus_1 = off + 1;
-
-        vis_set_gsr(5 << VIS_GSR_SCALEFACT_SHIFT);
-
-        vis_ld64(constants3[0], CONST_3);
-        vis_fzero(ZERO);
-        vis_ld64(constants256_512[0], CONST_256);
-
-        ref = vis_alignaddr(ref);
-        do {    /* 26 cycles */
-                vis_ld64(ref[0], TMP0);
-
-                vis_ld64(ref[8], TMP2);
-
-                vis_alignaddr_g0((void *)off);
-
-                vis_ld64(ref[16], TMP4);
-
-                vis_ld64(dest[0], DST_0);
-                vis_faligndata(TMP0, TMP2, REF_0);
-
-                vis_ld64(dest[8], DST_2);
-                vis_faligndata(TMP2, TMP4, REF_4);
-
-                if (off != 0x7) {
-                        vis_alignaddr_g0((void *)off_plus_1);
-                        vis_faligndata(TMP0, TMP2, REF_2);
-                        vis_faligndata(TMP2, TMP4, REF_6);
-                } else {
-                        vis_src1(TMP2, REF_2);
-                        vis_src1(TMP4, REF_6);
-                }
-
-                vis_mul8x16au(REF_0,   CONST_256, TMP0);
-
-                vis_pmerge(ZERO,     REF_2,     TMP4);
-                vis_mul8x16au(REF_0_1, CONST_256, TMP2);
-
-                vis_pmerge(ZERO, REF_2_1, TMP6);
-
-                vis_padd16(TMP0, TMP4, TMP0);
-
-                vis_mul8x16al(DST_0,   CONST_512, TMP4);
-                vis_padd16(TMP2, TMP6, TMP2);
-
-                vis_mul8x16al(DST_1,   CONST_512, TMP6);
-
-                vis_mul8x16au(REF_6,   CONST_256, TMP12);
-
-                vis_padd16(TMP0, TMP4, TMP0);
-                vis_mul8x16au(REF_6_1, CONST_256, TMP14);
-
-                vis_padd16(TMP2, TMP6, TMP2);
-                vis_mul8x16au(REF_4,   CONST_256, TMP16);
-
-                vis_padd16(TMP0, CONST_3, TMP8);
-                vis_mul8x16au(REF_4_1, CONST_256, TMP18);
-
-                vis_padd16(TMP2, CONST_3, TMP10);
-                vis_pack16(TMP8, DST_0);
-
-                vis_pack16(TMP10, DST_1);
-                vis_padd16(TMP16, TMP12, TMP0);
-
-                vis_st64(DST_0, dest[0]);
-                vis_mul8x16al(DST_2,   CONST_512, TMP4);
-                vis_padd16(TMP18, TMP14, TMP2);
-
-                vis_mul8x16al(DST_3,   CONST_512, TMP6);
-                vis_padd16(TMP0, CONST_3, TMP0);
-
-                vis_padd16(TMP2, CONST_3, TMP2);
-
-                vis_padd16(TMP0, TMP4, TMP0);
-
-                vis_padd16(TMP2, TMP6, TMP2);
-                vis_pack16(TMP0, DST_2);
-
-                vis_pack16(TMP2, DST_3);
-                vis_st64(DST_2, dest[8]);
-
-                ref += stride;
-                dest += stride;
-        } while (--height);
-}
-
-static void MC_avg_x_8_vis (uint8_t * dest, const uint8_t * ref,
-                            const ptrdiff_t stride, int height)
-{
-        unsigned long off = (unsigned long) ref & 0x7;
-        unsigned long off_plus_1 = off + 1;
-        int stride_times_2 = stride << 1;
-
-        vis_set_gsr(5 << VIS_GSR_SCALEFACT_SHIFT);
-
-        vis_ld64(constants3[0], CONST_3);
-        vis_fzero(ZERO);
-        vis_ld64(constants256_512[0], CONST_256);
-
-        ref = vis_alignaddr(ref);
-        height >>= 2;
-        do {    /* 47 cycles */
-                vis_ld64(ref[0],   TMP0);
-
-                vis_ld64_2(ref, 8, TMP2);
-                ref += stride;
-
-                vis_alignaddr_g0((void *)off);
-
-                vis_ld64(ref[0],   TMP4);
-                vis_faligndata(TMP0, TMP2, REF_0);
-
-                vis_ld64_2(ref, 8, TMP6);
-                ref += stride;
-
-                vis_ld64(ref[0],   TMP8);
-
-                vis_ld64_2(ref, 8, TMP10);
-                ref += stride;
-                vis_faligndata(TMP4, TMP6, REF_4);
-
-                vis_ld64(ref[0],   TMP12);
-
-                vis_ld64_2(ref, 8, TMP14);
-                ref += stride;
-                vis_faligndata(TMP8, TMP10, REF_S0);
-
-                vis_faligndata(TMP12, TMP14, REF_S4);
-
-                if (off != 0x7) {
-                        vis_alignaddr_g0((void *)off_plus_1);
-
-                        vis_ld64(dest[0], DST_0);
-                        vis_faligndata(TMP0, TMP2, REF_2);
-
-                        vis_ld64_2(dest, stride, DST_2);
-                        vis_faligndata(TMP4, TMP6, REF_6);
-
-                        vis_faligndata(TMP8, TMP10, REF_S2);
-
-                        vis_faligndata(TMP12, TMP14, REF_S6);
-                } else {
-                        vis_ld64(dest[0], DST_0);
-                        vis_src1(TMP2, REF_2);
-
-                        vis_ld64_2(dest, stride, DST_2);
-                        vis_src1(TMP6, REF_6);
-
-                        vis_src1(TMP10, REF_S2);
-
-                        vis_src1(TMP14, REF_S6);
-                }
-
-                vis_pmerge(ZERO,     REF_0,     TMP0);
-                vis_mul8x16au(REF_0_1, CONST_256, TMP2);
-
-                vis_pmerge(ZERO,     REF_2,     TMP4);
-                vis_mul8x16au(REF_2_1, CONST_256, TMP6);
-
-                vis_padd16(TMP0, CONST_3, TMP0);
-                vis_mul8x16al(DST_0,   CONST_512, TMP16);
-
-                vis_padd16(TMP2, CONST_3, TMP2);
-                vis_mul8x16al(DST_1,   CONST_512, TMP18);
-
-                vis_padd16(TMP0, TMP4, TMP0);
-                vis_mul8x16au(REF_4, CONST_256, TMP8);
-
-                vis_padd16(TMP2, TMP6, TMP2);
-                vis_mul8x16au(REF_4_1, CONST_256, TMP10);
-
-                vis_padd16(TMP0, TMP16, TMP0);
-                vis_mul8x16au(REF_6, CONST_256, TMP12);
-
-                vis_padd16(TMP2, TMP18, TMP2);
-                vis_mul8x16au(REF_6_1, CONST_256, TMP14);
-
-                vis_padd16(TMP8, CONST_3, TMP8);
-                vis_mul8x16al(DST_2, CONST_512, TMP16);
-
-                vis_padd16(TMP8, TMP12, TMP8);
-                vis_mul8x16al(DST_3, CONST_512, TMP18);
-
-                vis_padd16(TMP10, TMP14, TMP10);
-                vis_pack16(TMP0, DST_0);
-
-                vis_pack16(TMP2, DST_1);
-                vis_st64(DST_0, dest[0]);
-                dest += stride;
-                vis_padd16(TMP10, CONST_3, TMP10);
-
-                vis_ld64_2(dest, stride, DST_0);
-                vis_padd16(TMP8, TMP16, TMP8);
-
-                vis_ld64_2(dest, stride_times_2, TMP4/*DST_2*/);
-                vis_padd16(TMP10, TMP18, TMP10);
-                vis_pack16(TMP8, DST_2);
-
-                vis_pack16(TMP10, DST_3);
-                vis_st64(DST_2, dest[0]);
-                dest += stride;
-
-                vis_mul8x16au(REF_S0_1, CONST_256, TMP2);
-                vis_pmerge(ZERO,     REF_S0,     TMP0);
-
-                vis_pmerge(ZERO,     REF_S2,     TMP24);
-                vis_mul8x16au(REF_S2_1, CONST_256, TMP6);
-
-                vis_padd16(TMP0, CONST_3, TMP0);
-                vis_mul8x16au(REF_S4, CONST_256, TMP8);
-
-                vis_padd16(TMP2, CONST_3, TMP2);
-                vis_mul8x16au(REF_S4_1, CONST_256, TMP10);
-
-                vis_padd16(TMP0, TMP24, TMP0);
-                vis_mul8x16au(REF_S6, CONST_256, TMP12);
-
-                vis_padd16(TMP2, TMP6, TMP2);
-                vis_mul8x16au(REF_S6_1, CONST_256, TMP14);
-
-                vis_padd16(TMP8, CONST_3, TMP8);
-                vis_mul8x16al(DST_0,   CONST_512, TMP16);
-
-                vis_padd16(TMP10, CONST_3, TMP10);
-                vis_mul8x16al(DST_1,   CONST_512, TMP18);
-
-                vis_padd16(TMP8, TMP12, TMP8);
-                vis_mul8x16al(TMP4/*DST_2*/, CONST_512, TMP20);
-
-                vis_mul8x16al(TMP5/*DST_3*/, CONST_512, TMP22);
-                vis_padd16(TMP0, TMP16, TMP0);
-
-                vis_padd16(TMP2, TMP18, TMP2);
-                vis_pack16(TMP0, DST_0);
-
-                vis_padd16(TMP10, TMP14, TMP10);
-                vis_pack16(TMP2, DST_1);
-                vis_st64(DST_0, dest[0]);
-                dest += stride;
-
-                vis_padd16(TMP8, TMP20, TMP8);
-
-                vis_padd16(TMP10, TMP22, TMP10);
-                vis_pack16(TMP8, DST_2);
-
-                vis_pack16(TMP10, DST_3);
-                vis_st64(DST_2, dest[0]);
-                dest += stride;
-        } while (--height);
-}
-
-static void MC_put_y_16_vis (uint8_t * dest, const uint8_t * ref,
-                             const ptrdiff_t stride, int height)
-{
-        ref = vis_alignaddr(ref);
-        vis_ld64(ref[0], TMP0);
-
-        vis_ld64_2(ref, 8, TMP2);
-
-        vis_ld64_2(ref, 16, TMP4);
-        ref += stride;
-
-        vis_ld64(ref[0], TMP6);
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        vis_ld64_2(ref, 8, TMP8);
-        vis_faligndata(TMP2, TMP4, REF_4);
-
-        vis_ld64_2(ref, 16, TMP10);
-        ref += stride;
-
-        vis_ld64(constants_fe[0], MASK_fe);
-        vis_faligndata(TMP6, TMP8, REF_2);
-
-        vis_ld64(constants_7f[0], MASK_7f);
-        vis_faligndata(TMP8, TMP10, REF_6);
-
-        vis_ld64(constants128[0], CONST_128);
-        height = (height >> 1) - 1;
-        do {    /* 24 cycles */
-                vis_ld64(ref[0], TMP0);
-                vis_xor(REF_0, REF_2, TMP12);
-
-                vis_ld64_2(ref, 8, TMP2);
-                vis_xor(REF_4, REF_6, TMP16);
-
-                vis_ld64_2(ref, 16, TMP4);
-                ref += stride;
-                vis_or(REF_0, REF_2, TMP14);
-
-                vis_ld64(ref[0], TMP6);
-                vis_or(REF_4, REF_6, TMP18);
-
-                vis_ld64_2(ref, 8, TMP8);
-                vis_faligndata(TMP0, TMP2, REF_0);
-
-                vis_ld64_2(ref, 16, TMP10);
-                ref += stride;
-                vis_faligndata(TMP2, TMP4, REF_4);
-
-                vis_and(TMP12, MASK_fe, TMP12);
-
-                vis_and(TMP16, MASK_fe, TMP16);
-                vis_mul8x16(CONST_128, TMP12, TMP12);
-
-                vis_mul8x16(CONST_128, TMP16, TMP16);
-                vis_xor(REF_0, REF_2, TMP0);
-
-                vis_xor(REF_4, REF_6, TMP2);
-
-                vis_or(REF_0, REF_2, TMP20);
-
-                vis_and(TMP12, MASK_7f, TMP12);
-
-                vis_and(TMP16, MASK_7f, TMP16);
-
-                vis_psub16(TMP14, TMP12, TMP12);
-                vis_st64(TMP12, dest[0]);
-
-                vis_psub16(TMP18, TMP16, TMP16);
-                vis_st64_2(TMP16, dest, 8);
-                dest += stride;
-
-                vis_or(REF_4, REF_6, TMP18);
-
-                vis_and(TMP0, MASK_fe, TMP0);
-
-                vis_and(TMP2, MASK_fe, TMP2);
-                vis_mul8x16(CONST_128, TMP0, TMP0);
-
-                vis_faligndata(TMP6, TMP8, REF_2);
-                vis_mul8x16(CONST_128, TMP2, TMP2);
-
-                vis_faligndata(TMP8, TMP10, REF_6);
-
-                vis_and(TMP0, MASK_7f, TMP0);
-
-                vis_and(TMP2, MASK_7f, TMP2);
-
-                vis_psub16(TMP20, TMP0, TMP0);
-                vis_st64(TMP0, dest[0]);
-
-                vis_psub16(TMP18, TMP2, TMP2);
-                vis_st64_2(TMP2, dest, 8);
-                dest += stride;
-        } while (--height);
-
-        vis_ld64(ref[0], TMP0);
-        vis_xor(REF_0, REF_2, TMP12);
-
-        vis_ld64_2(ref, 8, TMP2);
-        vis_xor(REF_4, REF_6, TMP16);
-
-        vis_ld64_2(ref, 16, TMP4);
-        vis_or(REF_0, REF_2, TMP14);
-
-        vis_or(REF_4, REF_6, TMP18);
-
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        vis_faligndata(TMP2, TMP4, REF_4);
-
-        vis_and(TMP12, MASK_fe, TMP12);
-
-        vis_and(TMP16, MASK_fe, TMP16);
-        vis_mul8x16(CONST_128, TMP12, TMP12);
-
-        vis_mul8x16(CONST_128, TMP16, TMP16);
-        vis_xor(REF_0, REF_2, TMP0);
-
-        vis_xor(REF_4, REF_6, TMP2);
-
-        vis_or(REF_0, REF_2, TMP20);
-
-        vis_and(TMP12, MASK_7f, TMP12);
-
-        vis_and(TMP16, MASK_7f, TMP16);
-
-        vis_psub16(TMP14, TMP12, TMP12);
-        vis_st64(TMP12, dest[0]);
-
-        vis_psub16(TMP18, TMP16, TMP16);
-        vis_st64_2(TMP16, dest, 8);
-        dest += stride;
-
-        vis_or(REF_4, REF_6, TMP18);
-
-        vis_and(TMP0, MASK_fe, TMP0);
-
-        vis_and(TMP2, MASK_fe, TMP2);
-        vis_mul8x16(CONST_128, TMP0, TMP0);
-
-        vis_mul8x16(CONST_128, TMP2, TMP2);
-
-        vis_and(TMP0, MASK_7f, TMP0);
-
-        vis_and(TMP2, MASK_7f, TMP2);
-
-        vis_psub16(TMP20, TMP0, TMP0);
-        vis_st64(TMP0, dest[0]);
-
-        vis_psub16(TMP18, TMP2, TMP2);
-        vis_st64_2(TMP2, dest, 8);
-}
-
-static void MC_put_y_8_vis (uint8_t * dest, const uint8_t * ref,
-                            const ptrdiff_t stride, int height)
-{
-        ref = vis_alignaddr(ref);
-        vis_ld64(ref[0], TMP0);
-
-        vis_ld64_2(ref, 8, TMP2);
-        ref += stride;
-
-        vis_ld64(ref[0], TMP4);
-
-        vis_ld64_2(ref, 8, TMP6);
-        ref += stride;
-
-        vis_ld64(constants_fe[0], MASK_fe);
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        vis_ld64(constants_7f[0], MASK_7f);
-        vis_faligndata(TMP4, TMP6, REF_2);
-
-        vis_ld64(constants128[0], CONST_128);
-        height = (height >> 1) - 1;
-        do {    /* 12 cycles */
-                vis_ld64(ref[0], TMP0);
-                vis_xor(REF_0, REF_2, TMP4);
-
-                vis_ld64_2(ref, 8, TMP2);
-                ref += stride;
-                vis_and(TMP4, MASK_fe, TMP4);
-
-                vis_or(REF_0, REF_2, TMP6);
-                vis_mul8x16(CONST_128, TMP4, TMP4);
-
-                vis_faligndata(TMP0, TMP2, REF_0);
-                vis_ld64(ref[0], TMP0);
-
-                vis_ld64_2(ref, 8, TMP2);
-                ref += stride;
-                vis_xor(REF_0, REF_2, TMP12);
-
-                vis_and(TMP4, MASK_7f, TMP4);
-
-                vis_and(TMP12, MASK_fe, TMP12);
-
-                vis_mul8x16(CONST_128, TMP12, TMP12);
-                vis_or(REF_0, REF_2, TMP14);
-
-                vis_psub16(TMP6, TMP4, DST_0);
-                vis_st64(DST_0, dest[0]);
-                dest += stride;
-
-                vis_faligndata(TMP0, TMP2, REF_2);
-
-                vis_and(TMP12, MASK_7f, TMP12);
-
-                vis_psub16(TMP14, TMP12, DST_0);
-                vis_st64(DST_0, dest[0]);
-                dest += stride;
-        } while (--height);
-
-        vis_ld64(ref[0], TMP0);
-        vis_xor(REF_0, REF_2, TMP4);
-
-        vis_ld64_2(ref, 8, TMP2);
-        vis_and(TMP4, MASK_fe, TMP4);
-
-        vis_or(REF_0, REF_2, TMP6);
-        vis_mul8x16(CONST_128, TMP4, TMP4);
-
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        vis_xor(REF_0, REF_2, TMP12);
-
-        vis_and(TMP4, MASK_7f, TMP4);
-
-        vis_and(TMP12, MASK_fe, TMP12);
-
-        vis_mul8x16(CONST_128, TMP12, TMP12);
-        vis_or(REF_0, REF_2, TMP14);
-
-        vis_psub16(TMP6, TMP4, DST_0);
-        vis_st64(DST_0, dest[0]);
-        dest += stride;
-
-        vis_and(TMP12, MASK_7f, TMP12);
-
-        vis_psub16(TMP14, TMP12, DST_0);
-        vis_st64(DST_0, dest[0]);
-}
-
-static void MC_avg_y_16_vis (uint8_t * dest, const uint8_t * ref,
-                             const ptrdiff_t stride, int height)
-{
-        int stride_8 = stride + 8;
-        int stride_16 = stride + 16;
-
-        vis_set_gsr(5 << VIS_GSR_SCALEFACT_SHIFT);
-
-        ref = vis_alignaddr(ref);
-
-        vis_ld64(ref[ 0], TMP0);
-        vis_fzero(ZERO);
-
-        vis_ld64(ref[ 8], TMP2);
-
-        vis_ld64(ref[16], TMP4);
-
-        vis_ld64(constants3[0], CONST_3);
-        vis_faligndata(TMP0, TMP2, REF_2);
-
-        vis_ld64(constants256_512[0], CONST_256);
-        vis_faligndata(TMP2, TMP4, REF_6);
-        height >>= 1;
-
-        do {    /* 31 cycles */
-                vis_ld64_2(ref, stride, TMP0);
-                vis_pmerge(ZERO,       REF_2,     TMP12);
-                vis_mul8x16au(REF_2_1, CONST_256, TMP14);
-
-                vis_ld64_2(ref, stride_8, TMP2);
-                vis_pmerge(ZERO,       REF_6,     TMP16);
-                vis_mul8x16au(REF_6_1, CONST_256, TMP18);
-
-                vis_ld64_2(ref, stride_16, TMP4);
-                ref += stride;
-
-                vis_ld64(dest[0], DST_0);
-                vis_faligndata(TMP0, TMP2, REF_0);
-
-                vis_ld64_2(dest, 8, DST_2);
-                vis_faligndata(TMP2, TMP4, REF_4);
-
-                vis_ld64_2(ref, stride, TMP6);
-                vis_pmerge(ZERO,     REF_0,     TMP0);
-                vis_mul8x16au(REF_0_1, CONST_256, TMP2);
-
-                vis_ld64_2(ref, stride_8, TMP8);
-                vis_pmerge(ZERO,     REF_4,     TMP4);
-
-                vis_ld64_2(ref, stride_16, TMP10);
-                ref += stride;
-
-                vis_ld64_2(dest, stride, REF_S0/*DST_4*/);
-                vis_faligndata(TMP6, TMP8, REF_2);
-                vis_mul8x16au(REF_4_1, CONST_256, TMP6);
-
-                vis_ld64_2(dest, stride_8, REF_S2/*DST_6*/);
-                vis_faligndata(TMP8, TMP10, REF_6);
-                vis_mul8x16al(DST_0,   CONST_512, TMP20);
-
-                vis_padd16(TMP0, CONST_3, TMP0);
-                vis_mul8x16al(DST_1,   CONST_512, TMP22);
-
-                vis_padd16(TMP2, CONST_3, TMP2);
-                vis_mul8x16al(DST_2,   CONST_512, TMP24);
-
-                vis_padd16(TMP4, CONST_3, TMP4);
-                vis_mul8x16al(DST_3,   CONST_512, TMP26);
-
-                vis_padd16(TMP6, CONST_3, TMP6);
-
-                vis_padd16(TMP12, TMP20, TMP12);
-                vis_mul8x16al(REF_S0,   CONST_512, TMP20);
-
-                vis_padd16(TMP14, TMP22, TMP14);
-                vis_mul8x16al(REF_S0_1, CONST_512, TMP22);
-
-                vis_padd16(TMP16, TMP24, TMP16);
-                vis_mul8x16al(REF_S2,   CONST_512, TMP24);
-
-                vis_padd16(TMP18, TMP26, TMP18);
-                vis_mul8x16al(REF_S2_1, CONST_512, TMP26);
-
-                vis_padd16(TMP12, TMP0, TMP12);
-                vis_mul8x16au(REF_2,   CONST_256, TMP28);
-
-                vis_padd16(TMP14, TMP2, TMP14);
-                vis_mul8x16au(REF_2_1, CONST_256, TMP30);
-
-                vis_padd16(TMP16, TMP4, TMP16);
-                vis_mul8x16au(REF_6,   CONST_256, REF_S4);
-
-                vis_padd16(TMP18, TMP6, TMP18);
-                vis_mul8x16au(REF_6_1, CONST_256, REF_S6);
-
-                vis_pack16(TMP12, DST_0);
-                vis_padd16(TMP28, TMP0, TMP12);
-
-                vis_pack16(TMP14, DST_1);
-                vis_st64(DST_0, dest[0]);
-                vis_padd16(TMP30, TMP2, TMP14);
-
-                vis_pack16(TMP16, DST_2);
-                vis_padd16(REF_S4, TMP4, TMP16);
-
-                vis_pack16(TMP18, DST_3);
-                vis_st64_2(DST_2, dest, 8);
-                dest += stride;
-                vis_padd16(REF_S6, TMP6, TMP18);
-
-                vis_padd16(TMP12, TMP20, TMP12);
-
-                vis_padd16(TMP14, TMP22, TMP14);
-                vis_pack16(TMP12, DST_0);
-
-                vis_padd16(TMP16, TMP24, TMP16);
-                vis_pack16(TMP14, DST_1);
-                vis_st64(DST_0, dest[0]);
-
-                vis_padd16(TMP18, TMP26, TMP18);
-                vis_pack16(TMP16, DST_2);
-
-                vis_pack16(TMP18, DST_3);
-                vis_st64_2(DST_2, dest, 8);
-                dest += stride;
-        } while (--height);
-}
-
-static void MC_avg_y_8_vis (uint8_t * dest, const uint8_t * ref,
-                            const ptrdiff_t stride, int height)
-{
-        int stride_8 = stride + 8;
-
-        vis_set_gsr(5 << VIS_GSR_SCALEFACT_SHIFT);
-
-        ref = vis_alignaddr(ref);
-
-        vis_ld64(ref[ 0], TMP0);
-        vis_fzero(ZERO);
-
-        vis_ld64(ref[ 8], TMP2);
-
-        vis_ld64(constants3[0], CONST_3);
-        vis_faligndata(TMP0, TMP2, REF_2);
-
-        vis_ld64(constants256_512[0], CONST_256);
-
-        height >>= 1;
-        do {    /* 20 cycles */
-                vis_ld64_2(ref, stride, TMP0);
-                vis_pmerge(ZERO,       REF_2,     TMP8);
-                vis_mul8x16au(REF_2_1, CONST_256, TMP10);
-
-                vis_ld64_2(ref, stride_8, TMP2);
-                ref += stride;
-
-                vis_ld64(dest[0], DST_0);
-
-                vis_ld64_2(dest, stride, DST_2);
-                vis_faligndata(TMP0, TMP2, REF_0);
-
-                vis_ld64_2(ref, stride, TMP4);
-                vis_mul8x16al(DST_0,   CONST_512, TMP16);
-                vis_pmerge(ZERO,       REF_0,     TMP12);
-
-                vis_ld64_2(ref, stride_8, TMP6);
-                ref += stride;
-                vis_mul8x16al(DST_1,   CONST_512, TMP18);
-                vis_pmerge(ZERO,       REF_0_1,   TMP14);
-
-                vis_padd16(TMP12, CONST_3, TMP12);
-                vis_mul8x16al(DST_2,   CONST_512, TMP24);
-
-                vis_padd16(TMP14, CONST_3, TMP14);
-                vis_mul8x16al(DST_3,   CONST_512, TMP26);
-
-                vis_faligndata(TMP4, TMP6, REF_2);
-
-                vis_padd16(TMP8, TMP12, TMP8);
-
-                vis_padd16(TMP10, TMP14, TMP10);
-                vis_mul8x16au(REF_2,   CONST_256, TMP20);
-
-                vis_padd16(TMP8, TMP16, TMP0);
-                vis_mul8x16au(REF_2_1, CONST_256, TMP22);
-
-                vis_padd16(TMP10, TMP18, TMP2);
-                vis_pack16(TMP0, DST_0);
-
-                vis_pack16(TMP2, DST_1);
-                vis_st64(DST_0, dest[0]);
-                dest += stride;
-                vis_padd16(TMP12, TMP20, TMP12);
-
-                vis_padd16(TMP14, TMP22, TMP14);
-
-                vis_padd16(TMP12, TMP24, TMP0);
-
-                vis_padd16(TMP14, TMP26, TMP2);
-                vis_pack16(TMP0, DST_2);
-
-                vis_pack16(TMP2, DST_3);
-                vis_st64(DST_2, dest[0]);
-                dest += stride;
-        } while (--height);
-}
-
-static void MC_put_xy_16_vis (uint8_t * dest, const uint8_t * ref,
-                              const ptrdiff_t stride, int height)
-{
-        unsigned long off = (unsigned long) ref & 0x7;
-        unsigned long off_plus_1 = off + 1;
-        int stride_8 = stride + 8;
-        int stride_16 = stride + 16;
-
-        vis_set_gsr(5 << VIS_GSR_SCALEFACT_SHIFT);
-
-        ref = vis_alignaddr(ref);
-
-        vis_ld64(ref[ 0], TMP0);
-        vis_fzero(ZERO);
-
-        vis_ld64(ref[ 8], TMP2);
-
-        vis_ld64(ref[16], TMP4);
-
-        vis_ld64(constants2[0], CONST_2);
-        vis_faligndata(TMP0, TMP2, REF_S0);
-
-        vis_ld64(constants256_512[0], CONST_256);
-        vis_faligndata(TMP2, TMP4, REF_S4);
-
-        if (off != 0x7) {
-                vis_alignaddr_g0((void *)off_plus_1);
-                vis_faligndata(TMP0, TMP2, REF_S2);
-                vis_faligndata(TMP2, TMP4, REF_S6);
-        } else {
-                vis_src1(TMP2, REF_S2);
-                vis_src1(TMP4, REF_S6);
-        }
-
-        height >>= 1;
-        do {
-                vis_ld64_2(ref, stride, TMP0);
-                vis_mul8x16au(REF_S0, CONST_256, TMP12);
-                vis_pmerge(ZERO,      REF_S0_1,  TMP14);
-
-                vis_alignaddr_g0((void *)off);
-
-                vis_ld64_2(ref, stride_8, TMP2);
-                vis_mul8x16au(REF_S2, CONST_256, TMP16);
-                vis_pmerge(ZERO,      REF_S2_1,  TMP18);
-
-                vis_ld64_2(ref, stride_16, TMP4);
-                ref += stride;
-                vis_mul8x16au(REF_S4, CONST_256, TMP20);
-                vis_pmerge(ZERO,      REF_S4_1,  TMP22);
-
-                vis_ld64_2(ref, stride, TMP6);
-                vis_mul8x16au(REF_S6, CONST_256, TMP24);
-                vis_pmerge(ZERO,      REF_S6_1,  TMP26);
-
-                vis_ld64_2(ref, stride_8, TMP8);
-                vis_faligndata(TMP0, TMP2, REF_0);
-
-                vis_ld64_2(ref, stride_16, TMP10);
-                ref += stride;
-                vis_faligndata(TMP2, TMP4, REF_4);
-
-                vis_faligndata(TMP6, TMP8, REF_S0);
-
-                vis_faligndata(TMP8, TMP10, REF_S4);
-
-                if (off != 0x7) {
-                        vis_alignaddr_g0((void *)off_plus_1);
-                        vis_faligndata(TMP0, TMP2, REF_2);
-                        vis_faligndata(TMP2, TMP4, REF_6);
-                        vis_faligndata(TMP6, TMP8, REF_S2);
-                        vis_faligndata(TMP8, TMP10, REF_S6);
-                } else {
-                        vis_src1(TMP2, REF_2);
-                        vis_src1(TMP4, REF_6);
-                        vis_src1(TMP8, REF_S2);
-                        vis_src1(TMP10, REF_S6);
-                }
-
-                vis_mul8x16au(REF_0, CONST_256, TMP0);
-                vis_pmerge(ZERO,      REF_0_1,  TMP2);
-
-                vis_mul8x16au(REF_2, CONST_256, TMP4);
-                vis_pmerge(ZERO,      REF_2_1,  TMP6);
-
-                vis_padd16(TMP0, CONST_2, TMP8);
-                vis_mul8x16au(REF_4, CONST_256, TMP0);
-
-                vis_padd16(TMP2, CONST_2, TMP10);
-                vis_mul8x16au(REF_4_1, CONST_256, TMP2);
-
-                vis_padd16(TMP8, TMP4, TMP8);
-                vis_mul8x16au(REF_6, CONST_256, TMP4);
-
-                vis_padd16(TMP10, TMP6, TMP10);
-                vis_mul8x16au(REF_6_1, CONST_256, TMP6);
-
-                vis_padd16(TMP12, TMP8, TMP12);
-
-                vis_padd16(TMP14, TMP10, TMP14);
-
-                vis_padd16(TMP12, TMP16, TMP12);
-
-                vis_padd16(TMP14, TMP18, TMP14);
-                vis_pack16(TMP12, DST_0);
-
-                vis_pack16(TMP14, DST_1);
-                vis_st64(DST_0, dest[0]);
-                vis_padd16(TMP0, CONST_2, TMP12);
-
-                vis_mul8x16au(REF_S0, CONST_256, TMP0);
-                vis_padd16(TMP2, CONST_2, TMP14);
-
-                vis_mul8x16au(REF_S0_1, CONST_256, TMP2);
-                vis_padd16(TMP12, TMP4, TMP12);
-
-                vis_mul8x16au(REF_S2, CONST_256, TMP4);
-                vis_padd16(TMP14, TMP6, TMP14);
-
-                vis_mul8x16au(REF_S2_1, CONST_256, TMP6);
-                vis_padd16(TMP20, TMP12, TMP20);
-
-                vis_padd16(TMP22, TMP14, TMP22);
-
-                vis_padd16(TMP20, TMP24, TMP20);
-
-                vis_padd16(TMP22, TMP26, TMP22);
-                vis_pack16(TMP20, DST_2);
-
-                vis_pack16(TMP22, DST_3);
-                vis_st64_2(DST_2, dest, 8);
-                dest += stride;
-                vis_padd16(TMP0, TMP4, TMP24);
-
-                vis_mul8x16au(REF_S4, CONST_256, TMP0);
-                vis_padd16(TMP2, TMP6, TMP26);
-
-                vis_mul8x16au(REF_S4_1, CONST_256, TMP2);
-                vis_padd16(TMP24, TMP8, TMP24);
-
-                vis_padd16(TMP26, TMP10, TMP26);
-                vis_pack16(TMP24, DST_0);
-
-                vis_pack16(TMP26, DST_1);
-                vis_st64(DST_0, dest[0]);
-                vis_pmerge(ZERO, REF_S6, TMP4);
-
-                vis_pmerge(ZERO,      REF_S6_1,  TMP6);
-
-                vis_padd16(TMP0, TMP4, TMP0);
-
-                vis_padd16(TMP2, TMP6, TMP2);
-
-                vis_padd16(TMP0, TMP12, TMP0);
-
-                vis_padd16(TMP2, TMP14, TMP2);
-                vis_pack16(TMP0, DST_2);
-
-                vis_pack16(TMP2, DST_3);
-                vis_st64_2(DST_2, dest, 8);
-                dest += stride;
-        } while (--height);
-}
-
-static void MC_put_xy_8_vis (uint8_t * dest, const uint8_t * ref,
-                             const ptrdiff_t stride, int height)
-{
-        unsigned long off = (unsigned long) ref & 0x7;
-        unsigned long off_plus_1 = off + 1;
-        int stride_8 = stride + 8;
-
-        vis_set_gsr(5 << VIS_GSR_SCALEFACT_SHIFT);
-
-        ref = vis_alignaddr(ref);
-
-        vis_ld64(ref[ 0], TMP0);
-        vis_fzero(ZERO);
-
-        vis_ld64(ref[ 8], TMP2);
-
-        vis_ld64(constants2[0], CONST_2);
-
-        vis_ld64(constants256_512[0], CONST_256);
-        vis_faligndata(TMP0, TMP2, REF_S0);
-
-        if (off != 0x7) {
-                vis_alignaddr_g0((void *)off_plus_1);
-                vis_faligndata(TMP0, TMP2, REF_S2);
-        } else {
-                vis_src1(TMP2, REF_S2);
-        }
-
-        height >>= 1;
-        do {    /* 26 cycles */
-                vis_ld64_2(ref, stride, TMP0);
-                vis_mul8x16au(REF_S0,   CONST_256, TMP8);
-                vis_pmerge(ZERO,        REF_S2,    TMP12);
-
-                vis_alignaddr_g0((void *)off);
-
-                vis_ld64_2(ref, stride_8, TMP2);
-                ref += stride;
-                vis_mul8x16au(REF_S0_1, CONST_256, TMP10);
-                vis_pmerge(ZERO,        REF_S2_1,  TMP14);
-
-                vis_ld64_2(ref, stride, TMP4);
-
-                vis_ld64_2(ref, stride_8, TMP6);
-                ref += stride;
-                vis_faligndata(TMP0, TMP2, REF_S4);
-
-                vis_pmerge(ZERO, REF_S4, TMP18);
-
-                vis_pmerge(ZERO, REF_S4_1, TMP20);
-
-                vis_faligndata(TMP4, TMP6, REF_S0);
-
-                if (off != 0x7) {
-                        vis_alignaddr_g0((void *)off_plus_1);
-                        vis_faligndata(TMP0, TMP2, REF_S6);
-                        vis_faligndata(TMP4, TMP6, REF_S2);
-                } else {
-                        vis_src1(TMP2, REF_S6);
-                        vis_src1(TMP6, REF_S2);
-                }
-
-                vis_padd16(TMP18, CONST_2, TMP18);
-                vis_mul8x16au(REF_S6,   CONST_256, TMP22);
-
-                vis_padd16(TMP20, CONST_2, TMP20);
-                vis_mul8x16au(REF_S6_1, CONST_256, TMP24);
-
-                vis_mul8x16au(REF_S0,   CONST_256, TMP26);
-                vis_pmerge(ZERO, REF_S0_1, TMP28);
-
-                vis_mul8x16au(REF_S2,   CONST_256, TMP30);
-                vis_padd16(TMP18, TMP22, TMP18);
-
-                vis_mul8x16au(REF_S2_1, CONST_256, TMP32);
-                vis_padd16(TMP20, TMP24, TMP20);
-
-                vis_padd16(TMP8,  TMP18, TMP8);
-
-                vis_padd16(TMP10, TMP20, TMP10);
-
-                vis_padd16(TMP8,  TMP12, TMP8);
-
-                vis_padd16(TMP10, TMP14, TMP10);
-                vis_pack16(TMP8,  DST_0);
-
-                vis_pack16(TMP10, DST_1);
-                vis_st64(DST_0, dest[0]);
-                dest += stride;
-                vis_padd16(TMP18, TMP26, TMP18);
-
-                vis_padd16(TMP20, TMP28, TMP20);
-
-                vis_padd16(TMP18, TMP30, TMP18);
-
-                vis_padd16(TMP20, TMP32, TMP20);
-                vis_pack16(TMP18, DST_2);
-
-                vis_pack16(TMP20, DST_3);
-                vis_st64(DST_2, dest[0]);
-                dest += stride;
-        } while (--height);
-}
-
-static void MC_avg_xy_16_vis (uint8_t * dest, const uint8_t * ref,
-                              const ptrdiff_t stride, int height)
-{
-        unsigned long off = (unsigned long) ref & 0x7;
-        unsigned long off_plus_1 = off + 1;
-        int stride_8 = stride + 8;
-        int stride_16 = stride + 16;
-
-        vis_set_gsr(4 << VIS_GSR_SCALEFACT_SHIFT);
-
-        ref = vis_alignaddr(ref);
-
-        vis_ld64(ref[ 0], TMP0);
-        vis_fzero(ZERO);
-
-        vis_ld64(ref[ 8], TMP2);
-
-        vis_ld64(ref[16], TMP4);
-
-        vis_ld64(constants6[0], CONST_6);
-        vis_faligndata(TMP0, TMP2, REF_S0);
-
-        vis_ld64(constants256_1024[0], CONST_256);
-        vis_faligndata(TMP2, TMP4, REF_S4);
-
-        if (off != 0x7) {
-                vis_alignaddr_g0((void *)off_plus_1);
-                vis_faligndata(TMP0, TMP2, REF_S2);
-                vis_faligndata(TMP2, TMP4, REF_S6);
-        } else {
-                vis_src1(TMP2, REF_S2);
-                vis_src1(TMP4, REF_S6);
-        }
-
-        height >>= 1;
-        do {    /* 55 cycles */
-                vis_ld64_2(ref, stride, TMP0);
-                vis_mul8x16au(REF_S0, CONST_256, TMP12);
-                vis_pmerge(ZERO,      REF_S0_1,  TMP14);
-
-                vis_alignaddr_g0((void *)off);
-
-                vis_ld64_2(ref, stride_8, TMP2);
-                vis_mul8x16au(REF_S2, CONST_256, TMP16);
-                vis_pmerge(ZERO,      REF_S2_1,  TMP18);
-
-                vis_ld64_2(ref, stride_16, TMP4);
-                ref += stride;
-                vis_mul8x16au(REF_S4, CONST_256, TMP20);
-                vis_pmerge(ZERO,      REF_S4_1,  TMP22);
-
-                vis_ld64_2(ref, stride, TMP6);
-                vis_mul8x16au(REF_S6, CONST_256, TMP24);
-                vis_pmerge(ZERO,      REF_S6_1,  TMP26);
-
-                vis_ld64_2(ref, stride_8, TMP8);
-                vis_faligndata(TMP0, TMP2, REF_0);
-
-                vis_ld64_2(ref, stride_16, TMP10);
-                ref += stride;
-                vis_faligndata(TMP2, TMP4, REF_4);
-
-                vis_ld64(dest[0], DST_0);
-                vis_faligndata(TMP6, TMP8, REF_S0);
-
-                vis_ld64_2(dest, 8, DST_2);
-                vis_faligndata(TMP8, TMP10, REF_S4);
-
-                if (off != 0x7) {
-                        vis_alignaddr_g0((void *)off_plus_1);
-                        vis_faligndata(TMP0, TMP2, REF_2);
-                        vis_faligndata(TMP2, TMP4, REF_6);
-                        vis_faligndata(TMP6, TMP8, REF_S2);
-                        vis_faligndata(TMP8, TMP10, REF_S6);
-                } else {
-                        vis_src1(TMP2, REF_2);
-                        vis_src1(TMP4, REF_6);
-                        vis_src1(TMP8, REF_S2);
-                        vis_src1(TMP10, REF_S6);
-                }
-
-                vis_mul8x16al(DST_0,   CONST_1024, TMP30);
-                vis_pmerge(ZERO, REF_0, TMP0);
-
-                vis_mul8x16al(DST_1,   CONST_1024, TMP32);
-                vis_pmerge(ZERO,      REF_0_1,  TMP2);
-
-                vis_mul8x16au(REF_2, CONST_256, TMP4);
-                vis_pmerge(ZERO,      REF_2_1,  TMP6);
-
-                vis_mul8x16al(DST_2,   CONST_1024, REF_0);
-                vis_padd16(TMP0, CONST_6, TMP0);
-
-                vis_mul8x16al(DST_3,   CONST_1024, REF_2);
-                vis_padd16(TMP2, CONST_6, TMP2);
-
-                vis_padd16(TMP0, TMP4, TMP0);
-                vis_mul8x16au(REF_4, CONST_256, TMP4);
-
-                vis_padd16(TMP2, TMP6, TMP2);
-                vis_mul8x16au(REF_4_1, CONST_256, TMP6);
-
-                vis_padd16(TMP12, TMP0, TMP12);
-                vis_mul8x16au(REF_6, CONST_256, TMP8);
-
-                vis_padd16(TMP14, TMP2, TMP14);
-                vis_mul8x16au(REF_6_1, CONST_256, TMP10);
-
-                vis_padd16(TMP12, TMP16, TMP12);
-                vis_mul8x16au(REF_S0, CONST_256, REF_4);
-
-                vis_padd16(TMP14, TMP18, TMP14);
-                vis_mul8x16au(REF_S0_1, CONST_256, REF_6);
-
-                vis_padd16(TMP12, TMP30, TMP12);
-
-                vis_padd16(TMP14, TMP32, TMP14);
-                vis_pack16(TMP12, DST_0);
-
-                vis_pack16(TMP14, DST_1);
-                vis_st64(DST_0, dest[0]);
-                vis_padd16(TMP4, CONST_6, TMP4);
-
-                vis_ld64_2(dest, stride, DST_0);
-                vis_padd16(TMP6, CONST_6, TMP6);
-                vis_mul8x16au(REF_S2, CONST_256, TMP12);
-
-                vis_padd16(TMP4, TMP8, TMP4);
-                vis_mul8x16au(REF_S2_1, CONST_256,  TMP14);
-
-                vis_padd16(TMP6, TMP10, TMP6);
-
-                vis_padd16(TMP20, TMP4, TMP20);
-
-                vis_padd16(TMP22, TMP6, TMP22);
-
-                vis_padd16(TMP20, TMP24, TMP20);
-
-                vis_padd16(TMP22, TMP26, TMP22);
-
-                vis_padd16(TMP20, REF_0, TMP20);
-                vis_mul8x16au(REF_S4, CONST_256, REF_0);
-
-                vis_padd16(TMP22, REF_2, TMP22);
-                vis_pack16(TMP20, DST_2);
-
-                vis_pack16(TMP22, DST_3);
-                vis_st64_2(DST_2, dest, 8);
-                dest += stride;
-
-                vis_ld64_2(dest, 8, DST_2);
-                vis_mul8x16al(DST_0,   CONST_1024, TMP30);
-                vis_pmerge(ZERO,      REF_S4_1,  REF_2);
-
-                vis_mul8x16al(DST_1,   CONST_1024, TMP32);
-                vis_padd16(REF_4, TMP0, TMP8);
-
-                vis_mul8x16au(REF_S6, CONST_256, REF_4);
-                vis_padd16(REF_6, TMP2, TMP10);
-
-                vis_mul8x16au(REF_S6_1, CONST_256, REF_6);
-                vis_padd16(TMP8, TMP12, TMP8);
-
-                vis_padd16(TMP10, TMP14, TMP10);
-
-                vis_padd16(TMP8, TMP30, TMP8);
-
-                vis_padd16(TMP10, TMP32, TMP10);
-                vis_pack16(TMP8, DST_0);
-
-                vis_pack16(TMP10, DST_1);
-                vis_st64(DST_0, dest[0]);
-
-                vis_padd16(REF_0, TMP4, REF_0);
-
-                vis_mul8x16al(DST_2,   CONST_1024, TMP30);
-                vis_padd16(REF_2, TMP6, REF_2);
-
-                vis_mul8x16al(DST_3,   CONST_1024, TMP32);
-                vis_padd16(REF_0, REF_4, REF_0);
-
-                vis_padd16(REF_2, REF_6, REF_2);
-
-                vis_padd16(REF_0, TMP30, REF_0);
-
-                /* stall */
-
-                vis_padd16(REF_2, TMP32, REF_2);
-                vis_pack16(REF_0, DST_2);
-
-                vis_pack16(REF_2, DST_3);
-                vis_st64_2(DST_2, dest, 8);
-                dest += stride;
-        } while (--height);
-}
-
-static void MC_avg_xy_8_vis (uint8_t * dest, const uint8_t * ref,
-                             const ptrdiff_t stride, int height)
-{
-        unsigned long off = (unsigned long) ref & 0x7;
-        unsigned long off_plus_1 = off + 1;
-        int stride_8 = stride + 8;
-
-        vis_set_gsr(4 << VIS_GSR_SCALEFACT_SHIFT);
-
-        ref = vis_alignaddr(ref);
-
-        vis_ld64(ref[0], TMP0);
-        vis_fzero(ZERO);
-
-        vis_ld64_2(ref, 8, TMP2);
-
-        vis_ld64(constants6[0], CONST_6);
-
-        vis_ld64(constants256_1024[0], CONST_256);
-        vis_faligndata(TMP0, TMP2, REF_S0);
-
-        if (off != 0x7) {
-                vis_alignaddr_g0((void *)off_plus_1);
-                vis_faligndata(TMP0, TMP2, REF_S2);
-        } else {
-                vis_src1(TMP2, REF_S2);
-        }
-
-        height >>= 1;
-        do {    /* 31 cycles */
-                vis_ld64_2(ref, stride, TMP0);
-                vis_mul8x16au(REF_S0, CONST_256, TMP8);
-                vis_pmerge(ZERO,      REF_S0_1,  TMP10);
-
-                vis_ld64_2(ref, stride_8, TMP2);
-                ref += stride;
-                vis_mul8x16au(REF_S2, CONST_256, TMP12);
-                vis_pmerge(ZERO,      REF_S2_1,  TMP14);
-
-                vis_alignaddr_g0((void *)off);
-
-                vis_ld64_2(ref, stride, TMP4);
-                vis_faligndata(TMP0, TMP2, REF_S4);
-
-                vis_ld64_2(ref, stride_8, TMP6);
-                ref += stride;
-
-                vis_ld64(dest[0], DST_0);
-                vis_faligndata(TMP4, TMP6, REF_S0);
-
-                vis_ld64_2(dest, stride, DST_2);
-
-                if (off != 0x7) {
-                        vis_alignaddr_g0((void *)off_plus_1);
-                        vis_faligndata(TMP0, TMP2, REF_S6);
-                        vis_faligndata(TMP4, TMP6, REF_S2);
-                } else {
-                        vis_src1(TMP2, REF_S6);
-                        vis_src1(TMP6, REF_S2);
-                }
-
-                vis_mul8x16al(DST_0,   CONST_1024, TMP30);
-                vis_pmerge(ZERO, REF_S4, TMP22);
-
-                vis_mul8x16al(DST_1,   CONST_1024, TMP32);
-                vis_pmerge(ZERO,      REF_S4_1,  TMP24);
-
-                vis_mul8x16au(REF_S6, CONST_256, TMP26);
-                vis_pmerge(ZERO,      REF_S6_1,  TMP28);
-
-                vis_mul8x16au(REF_S0, CONST_256, REF_S4);
-                vis_padd16(TMP22, CONST_6, TMP22);
-
-                vis_mul8x16au(REF_S0_1, CONST_256, REF_S6);
-                vis_padd16(TMP24, CONST_6, TMP24);
-
-                vis_mul8x16al(DST_2,   CONST_1024, REF_0);
-                vis_padd16(TMP22, TMP26, TMP22);
-
-                vis_mul8x16al(DST_3,   CONST_1024, REF_2);
-                vis_padd16(TMP24, TMP28, TMP24);
-
-                vis_mul8x16au(REF_S2, CONST_256, TMP26);
-                vis_padd16(TMP8, TMP22, TMP8);
-
-                vis_mul8x16au(REF_S2_1, CONST_256, TMP28);
-                vis_padd16(TMP10, TMP24, TMP10);
-
-                vis_padd16(TMP8, TMP12, TMP8);
-
-                vis_padd16(TMP10, TMP14, TMP10);
-
-                vis_padd16(TMP8, TMP30, TMP8);
-
-                vis_padd16(TMP10, TMP32, TMP10);
-                vis_pack16(TMP8, DST_0);
-
-                vis_pack16(TMP10, DST_1);
-                vis_st64(DST_0, dest[0]);
-                dest += stride;
-
-                vis_padd16(REF_S4, TMP22, TMP12);
-
-                vis_padd16(REF_S6, TMP24, TMP14);
-
-                vis_padd16(TMP12, TMP26, TMP12);
-
-                vis_padd16(TMP14, TMP28, TMP14);
-
-                vis_padd16(TMP12, REF_0, TMP12);
-
-                vis_padd16(TMP14, REF_2, TMP14);
-                vis_pack16(TMP12, DST_2);
-
-                vis_pack16(TMP14, DST_3);
-                vis_st64(DST_2, dest[0]);
-                dest += stride;
-        } while (--height);
-}
-
-/* End of rounding code */
-
-/* Start of no rounding code */
-/* The trick used in some of this file is the formula from the MMX
- * motion comp code, which is:
- *
- * (x+y)>>1 == (x&y)+((x^y)>>1)
- *
- * This allows us to average 8 bytes at a time in a 64-bit FPU reg.
- * We avoid overflows by masking before we do the shift, and we
- * implement the shift by multiplying by 1/2 using mul8x16.  So in
- * VIS this is (assume 'x' is in f0, 'y' is in f2, a repeating mask
- * of '0xfe' is in f4, a repeating mask of '0x7f' is in f6, and
- * the value 0x80808080 is in f8):
- *
- *      fxor            f0,   f2, f10
- *      fand            f10,  f4, f10
- *      fmul8x16        f8,  f10, f10
- *      fand            f10,  f6, f10
- *      fand            f0,   f2, f12
- *      fpadd16         f12, f10, f10
- */
-
-static void MC_put_no_round_o_16_vis (uint8_t * dest, const uint8_t * ref,
-                                      const ptrdiff_t stride, int height)
-{
-        ref = vis_alignaddr(ref);
-        do {    /* 5 cycles */
-                vis_ld64(ref[0], TMP0);
-
-                vis_ld64_2(ref, 8, TMP2);
-
-                vis_ld64_2(ref, 16, TMP4);
-                ref += stride;
-
-                vis_faligndata(TMP0, TMP2, REF_0);
-                vis_st64(REF_0, dest[0]);
-
-                vis_faligndata(TMP2, TMP4, REF_2);
-                vis_st64_2(REF_2, dest, 8);
-                dest += stride;
-        } while (--height);
-}
-
-static void MC_put_no_round_o_8_vis (uint8_t * dest, const uint8_t * ref,
-                                     const ptrdiff_t stride, int height)
-{
-        ref = vis_alignaddr(ref);
-        do {    /* 4 cycles */
-                vis_ld64(ref[0], TMP0);
-
-                vis_ld64(ref[8], TMP2);
-                ref += stride;
-
-                /* stall */
-
-                vis_faligndata(TMP0, TMP2, REF_0);
-                vis_st64(REF_0, dest[0]);
-                dest += stride;
-        } while (--height);
-}
-
-
-static void MC_avg_no_round_o_16_vis (uint8_t * dest, const uint8_t * ref,
-                                      const ptrdiff_t stride, int height)
-{
-        int stride_8 = stride + 8;
-
-        ref = vis_alignaddr(ref);
-
-        vis_ld64(ref[0], TMP0);
-
-        vis_ld64(ref[8], TMP2);
-
-        vis_ld64(ref[16], TMP4);
-
-        vis_ld64(dest[0], DST_0);
-
-        vis_ld64(dest[8], DST_2);
-
-        vis_ld64(constants_fe[0], MASK_fe);
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        vis_ld64(constants_7f[0], MASK_7f);
-        vis_faligndata(TMP2, TMP4, REF_2);
-
-        vis_ld64(constants128[0], CONST_128);
-
-        ref += stride;
-        height = (height >> 1) - 1;
-
-        do {    /* 24 cycles */
-                vis_ld64(ref[0], TMP0);
-                vis_xor(DST_0, REF_0, TMP6);
-
-                vis_ld64_2(ref, 8, TMP2);
-                vis_and(TMP6, MASK_fe, TMP6);
-
-                vis_ld64_2(ref, 16, TMP4);
-                ref += stride;
-                vis_mul8x16(CONST_128, TMP6, TMP6);
-                vis_xor(DST_2, REF_2, TMP8);
-
-                vis_and(TMP8, MASK_fe, TMP8);
-
-                vis_and(DST_0, REF_0, TMP10);
-                vis_ld64_2(dest, stride, DST_0);
-                vis_mul8x16(CONST_128, TMP8, TMP8);
-
-                vis_and(DST_2, REF_2, TMP12);
-                vis_ld64_2(dest, stride_8, DST_2);
-
-                vis_ld64(ref[0], TMP14);
-                vis_and(TMP6, MASK_7f, TMP6);
-
-                vis_and(TMP8, MASK_7f, TMP8);
-
-                vis_padd16(TMP10, TMP6, TMP6);
-                vis_st64(TMP6, dest[0]);
-
-                vis_padd16(TMP12, TMP8, TMP8);
-                vis_st64_2(TMP8, dest, 8);
-
-                dest += stride;
-                vis_ld64_2(ref, 8, TMP16);
-                vis_faligndata(TMP0, TMP2, REF_0);
-
-                vis_ld64_2(ref, 16, TMP18);
-                vis_faligndata(TMP2, TMP4, REF_2);
-                ref += stride;
-
-                vis_xor(DST_0, REF_0, TMP20);
-
-                vis_and(TMP20, MASK_fe, TMP20);
-
-                vis_xor(DST_2, REF_2, TMP22);
-                vis_mul8x16(CONST_128, TMP20, TMP20);
-
-                vis_and(TMP22, MASK_fe, TMP22);
-
-                vis_and(DST_0, REF_0, TMP24);
-                vis_mul8x16(CONST_128, TMP22, TMP22);
-
-                vis_and(DST_2, REF_2, TMP26);
-
-                vis_ld64_2(dest, stride, DST_0);
-                vis_faligndata(TMP14, TMP16, REF_0);
-
-                vis_ld64_2(dest, stride_8, DST_2);
-                vis_faligndata(TMP16, TMP18, REF_2);
-
-                vis_and(TMP20, MASK_7f, TMP20);
-
-                vis_and(TMP22, MASK_7f, TMP22);
-
-                vis_padd16(TMP24, TMP20, TMP20);
-                vis_st64(TMP20, dest[0]);
-
-                vis_padd16(TMP26, TMP22, TMP22);
-                vis_st64_2(TMP22, dest, 8);
-                dest += stride;
-        } while (--height);
-
-        vis_ld64(ref[0], TMP0);
-        vis_xor(DST_0, REF_0, TMP6);
-
-        vis_ld64_2(ref, 8, TMP2);
-        vis_and(TMP6, MASK_fe, TMP6);
-
-        vis_ld64_2(ref, 16, TMP4);
-        vis_mul8x16(CONST_128, TMP6, TMP6);
-        vis_xor(DST_2, REF_2, TMP8);
-
-        vis_and(TMP8, MASK_fe, TMP8);
-
-        vis_and(DST_0, REF_0, TMP10);
-        vis_ld64_2(dest, stride, DST_0);
-        vis_mul8x16(CONST_128, TMP8, TMP8);
-
-        vis_and(DST_2, REF_2, TMP12);
-        vis_ld64_2(dest, stride_8, DST_2);
-
-        vis_ld64(ref[0], TMP14);
-        vis_and(TMP6, MASK_7f, TMP6);
-
-        vis_and(TMP8, MASK_7f, TMP8);
-
-        vis_padd16(TMP10, TMP6, TMP6);
-        vis_st64(TMP6, dest[0]);
-
-        vis_padd16(TMP12, TMP8, TMP8);
-        vis_st64_2(TMP8, dest, 8);
-
-        dest += stride;
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        vis_faligndata(TMP2, TMP4, REF_2);
-
-        vis_xor(DST_0, REF_0, TMP20);
-
-        vis_and(TMP20, MASK_fe, TMP20);
-
-        vis_xor(DST_2, REF_2, TMP22);
-        vis_mul8x16(CONST_128, TMP20, TMP20);
-
-        vis_and(TMP22, MASK_fe, TMP22);
-
-        vis_and(DST_0, REF_0, TMP24);
-        vis_mul8x16(CONST_128, TMP22, TMP22);
-
-        vis_and(DST_2, REF_2, TMP26);
-
-        vis_and(TMP20, MASK_7f, TMP20);
-
-        vis_and(TMP22, MASK_7f, TMP22);
-
-        vis_padd16(TMP24, TMP20, TMP20);
-        vis_st64(TMP20, dest[0]);
-
-        vis_padd16(TMP26, TMP22, TMP22);
-        vis_st64_2(TMP22, dest, 8);
-}
-
-static void MC_put_no_round_x_16_vis (uint8_t * dest, const uint8_t * ref,
-                                      const ptrdiff_t stride, int height)
-{
-        unsigned long off = (unsigned long) ref & 0x7;
-        unsigned long off_plus_1 = off + 1;
-
-        ref = vis_alignaddr(ref);
-
-        vis_ld64(ref[0],    TMP0);
-
-        vis_ld64_2(ref, 8,  TMP2);
-
-        vis_ld64_2(ref, 16, TMP4);
-
-        vis_ld64(constants_fe[0], MASK_fe);
-
-        vis_ld64(constants_7f[0], MASK_7f);
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        vis_ld64(constants128[0], CONST_128);
-        vis_faligndata(TMP2, TMP4, REF_4);
-
-        if (off != 0x7) {
-                vis_alignaddr_g0((void *)off_plus_1);
-                vis_faligndata(TMP0, TMP2, REF_2);
-                vis_faligndata(TMP2, TMP4, REF_6);
-        } else {
-                vis_src1(TMP2, REF_2);
-                vis_src1(TMP4, REF_6);
-        }
-
-        ref += stride;
-        height = (height >> 1) - 1;
-
-        do {    /* 34 cycles */
-                vis_ld64(ref[0],    TMP0);
-                vis_xor(REF_0, REF_2, TMP6);
-
-                vis_ld64_2(ref, 8,  TMP2);
-                vis_xor(REF_4, REF_6, TMP8);
-
-                vis_ld64_2(ref, 16, TMP4);
-                vis_and(TMP6, MASK_fe, TMP6);
-                ref += stride;
-
-                vis_ld64(ref[0],    TMP14);
-                vis_mul8x16(CONST_128, TMP6, TMP6);
-                vis_and(TMP8, MASK_fe, TMP8);
-
-                vis_ld64_2(ref, 8,  TMP16);
-                vis_mul8x16(CONST_128, TMP8, TMP8);
-                vis_and(REF_0, REF_2, TMP10);
-
-                vis_ld64_2(ref, 16, TMP18);
-                ref += stride;
-                vis_and(REF_4, REF_6, TMP12);
-
-                vis_alignaddr_g0((void *)off);
-
-                vis_faligndata(TMP0, TMP2, REF_0);
-
-                vis_faligndata(TMP2, TMP4, REF_4);
-
-                if (off != 0x7) {
-                        vis_alignaddr_g0((void *)off_plus_1);
-                        vis_faligndata(TMP0, TMP2, REF_2);
-                        vis_faligndata(TMP2, TMP4, REF_6);
-                } else {
-                        vis_src1(TMP2, REF_2);
-                        vis_src1(TMP4, REF_6);
-                }
-
-                vis_and(TMP6, MASK_7f, TMP6);
-
-                vis_and(TMP8, MASK_7f, TMP8);
-
-                vis_padd16(TMP10, TMP6, TMP6);
-                vis_st64(TMP6, dest[0]);
-
-                vis_padd16(TMP12, TMP8, TMP8);
-                vis_st64_2(TMP8, dest, 8);
-                dest += stride;
-
-                vis_xor(REF_0, REF_2, TMP6);
-
-                vis_xor(REF_4, REF_6, TMP8);
-
-                vis_and(TMP6, MASK_fe, TMP6);
-
-                vis_mul8x16(CONST_128, TMP6, TMP6);
-                vis_and(TMP8, MASK_fe, TMP8);
-
-                vis_mul8x16(CONST_128, TMP8, TMP8);
-                vis_and(REF_0, REF_2, TMP10);
-
-                vis_and(REF_4, REF_6, TMP12);
-
-                vis_alignaddr_g0((void *)off);
-
-                vis_faligndata(TMP14, TMP16, REF_0);
-
-                vis_faligndata(TMP16, TMP18, REF_4);
-
-                if (off != 0x7) {
-                        vis_alignaddr_g0((void *)off_plus_1);
-                        vis_faligndata(TMP14, TMP16, REF_2);
-                        vis_faligndata(TMP16, TMP18, REF_6);
-                } else {
-                        vis_src1(TMP16, REF_2);
-                        vis_src1(TMP18, REF_6);
-                }
-
-                vis_and(TMP6, MASK_7f, TMP6);
-
-                vis_and(TMP8, MASK_7f, TMP8);
-
-                vis_padd16(TMP10, TMP6, TMP6);
-                vis_st64(TMP6, dest[0]);
-
-                vis_padd16(TMP12, TMP8, TMP8);
-                vis_st64_2(TMP8, dest, 8);
-                dest += stride;
-        } while (--height);
-
-        vis_ld64(ref[0],    TMP0);
-        vis_xor(REF_0, REF_2, TMP6);
-
-        vis_ld64_2(ref, 8,  TMP2);
-        vis_xor(REF_4, REF_6, TMP8);
-
-        vis_ld64_2(ref, 16, TMP4);
-        vis_and(TMP6, MASK_fe, TMP6);
-
-        vis_mul8x16(CONST_128, TMP6, TMP6);
-        vis_and(TMP8, MASK_fe, TMP8);
-
-        vis_mul8x16(CONST_128, TMP8, TMP8);
-        vis_and(REF_0, REF_2, TMP10);
-
-        vis_and(REF_4, REF_6, TMP12);
-
-        vis_alignaddr_g0((void *)off);
-
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        vis_faligndata(TMP2, TMP4, REF_4);
-
-        if (off != 0x7) {
-                vis_alignaddr_g0((void *)off_plus_1);
-                vis_faligndata(TMP0, TMP2, REF_2);
-                vis_faligndata(TMP2, TMP4, REF_6);
-        } else {
-                vis_src1(TMP2, REF_2);
-                vis_src1(TMP4, REF_6);
-        }
-
-        vis_and(TMP6, MASK_7f, TMP6);
-
-        vis_and(TMP8, MASK_7f, TMP8);
-
-        vis_padd16(TMP10, TMP6, TMP6);
-        vis_st64(TMP6, dest[0]);
-
-        vis_padd16(TMP12, TMP8, TMP8);
-        vis_st64_2(TMP8, dest, 8);
-        dest += stride;
-
-        vis_xor(REF_0, REF_2, TMP6);
-
-        vis_xor(REF_4, REF_6, TMP8);
-
-        vis_and(TMP6, MASK_fe, TMP6);
-
-        vis_mul8x16(CONST_128, TMP6, TMP6);
-        vis_and(TMP8, MASK_fe, TMP8);
-
-        vis_mul8x16(CONST_128, TMP8, TMP8);
-        vis_and(REF_0, REF_2, TMP10);
-
-        vis_and(REF_4, REF_6, TMP12);
-
-        vis_and(TMP6, MASK_7f, TMP6);
-
-        vis_and(TMP8, MASK_7f, TMP8);
-
-        vis_padd16(TMP10, TMP6, TMP6);
-        vis_st64(TMP6, dest[0]);
-
-        vis_padd16(TMP12, TMP8, TMP8);
-        vis_st64_2(TMP8, dest, 8);
-}
-
-static void MC_put_no_round_x_8_vis (uint8_t * dest, const uint8_t * ref,
-                                     const ptrdiff_t stride, int height)
-{
-        unsigned long off = (unsigned long) ref & 0x7;
-        unsigned long off_plus_1 = off + 1;
-
-        ref = vis_alignaddr(ref);
-
-        vis_ld64(ref[0], TMP0);
-
-        vis_ld64(ref[8], TMP2);
-
-        vis_ld64(constants_fe[0], MASK_fe);
-
-        vis_ld64(constants_7f[0], MASK_7f);
-
-        vis_ld64(constants128[0], CONST_128);
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        if (off != 0x7) {
-                vis_alignaddr_g0((void *)off_plus_1);
-                vis_faligndata(TMP0, TMP2, REF_2);
-        } else {
-                vis_src1(TMP2, REF_2);
-        }
-
-        ref += stride;
-        height = (height >> 1) - 1;
-
-        do {    /* 20 cycles */
-                vis_ld64(ref[0], TMP0);
-                vis_xor(REF_0, REF_2, TMP4);
-
-                vis_ld64_2(ref, 8, TMP2);
-                vis_and(TMP4, MASK_fe, TMP4);
-                ref += stride;
-
-                vis_ld64(ref[0], TMP8);
-                vis_and(REF_0, REF_2, TMP6);
-                vis_mul8x16(CONST_128, TMP4, TMP4);
-
-                vis_alignaddr_g0((void *)off);
-
-                vis_ld64_2(ref, 8, TMP10);
-                ref += stride;
-                vis_faligndata(TMP0, TMP2, REF_0);
-
-                if (off != 0x7) {
-                        vis_alignaddr_g0((void *)off_plus_1);
-                        vis_faligndata(TMP0, TMP2, REF_2);
-                } else {
-                        vis_src1(TMP2, REF_2);
-                }
-
-                vis_and(TMP4, MASK_7f, TMP4);
-
-                vis_padd16(TMP6, TMP4, DST_0);
-                vis_st64(DST_0, dest[0]);
-                dest += stride;
-
-                vis_xor(REF_0, REF_2, TMP12);
-
-                vis_and(TMP12, MASK_fe, TMP12);
-
-                vis_and(REF_0, REF_2, TMP14);
-                vis_mul8x16(CONST_128, TMP12, TMP12);
-
-                vis_alignaddr_g0((void *)off);
-                vis_faligndata(TMP8, TMP10, REF_0);
-                if (off != 0x7) {
-                        vis_alignaddr_g0((void *)off_plus_1);
-                        vis_faligndata(TMP8, TMP10, REF_2);
-                } else {
-                        vis_src1(TMP10, REF_2);
-                }
-
-                vis_and(TMP12, MASK_7f, TMP12);
-
-                vis_padd16(TMP14, TMP12, DST_0);
-                vis_st64(DST_0, dest[0]);
-                dest += stride;
-        } while (--height);
-
-        vis_ld64(ref[0], TMP0);
-        vis_xor(REF_0, REF_2, TMP4);
-
-        vis_ld64_2(ref, 8, TMP2);
-        vis_and(TMP4, MASK_fe, TMP4);
-
-        vis_and(REF_0, REF_2, TMP6);
-        vis_mul8x16(CONST_128, TMP4, TMP4);
-
-        vis_alignaddr_g0((void *)off);
-
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        if (off != 0x7) {
-                vis_alignaddr_g0((void *)off_plus_1);
-                vis_faligndata(TMP0, TMP2, REF_2);
-        } else {
-                vis_src1(TMP2, REF_2);
-        }
-
-        vis_and(TMP4, MASK_7f, TMP4);
-
-        vis_padd16(TMP6, TMP4, DST_0);
-        vis_st64(DST_0, dest[0]);
-        dest += stride;
-
-        vis_xor(REF_0, REF_2, TMP12);
-
-        vis_and(TMP12, MASK_fe, TMP12);
-
-        vis_and(REF_0, REF_2, TMP14);
-        vis_mul8x16(CONST_128, TMP12, TMP12);
-
-        vis_and(TMP12, MASK_7f, TMP12);
-
-        vis_padd16(TMP14, TMP12, DST_0);
-        vis_st64(DST_0, dest[0]);
-        dest += stride;
-}
-
-static void MC_avg_no_round_x_16_vis (uint8_t * dest, const uint8_t * ref,
-                                      const ptrdiff_t stride, int height)
-{
-        unsigned long off = (unsigned long) ref & 0x7;
-        unsigned long off_plus_1 = off + 1;
-
-        vis_set_gsr(5 << VIS_GSR_SCALEFACT_SHIFT);
-
-        vis_ld64(constants3[0], CONST_3);
-        vis_fzero(ZERO);
-        vis_ld64(constants256_512[0], CONST_256);
-
-        ref = vis_alignaddr(ref);
-        do {    /* 26 cycles */
-                vis_ld64(ref[0], TMP0);
-
-                vis_ld64(ref[8], TMP2);
-
-                vis_alignaddr_g0((void *)off);
-
-                vis_ld64(ref[16], TMP4);
-
-                vis_ld64(dest[0], DST_0);
-                vis_faligndata(TMP0, TMP2, REF_0);
-
-                vis_ld64(dest[8], DST_2);
-                vis_faligndata(TMP2, TMP4, REF_4);
-
-                if (off != 0x7) {
-                        vis_alignaddr_g0((void *)off_plus_1);
-                        vis_faligndata(TMP0, TMP2, REF_2);
-                        vis_faligndata(TMP2, TMP4, REF_6);
-                } else {
-                        vis_src1(TMP2, REF_2);
-                        vis_src1(TMP4, REF_6);
-                }
-
-                vis_mul8x16au(REF_0,   CONST_256, TMP0);
-
-                vis_pmerge(ZERO,     REF_2,     TMP4);
-                vis_mul8x16au(REF_0_1, CONST_256, TMP2);
-
-                vis_pmerge(ZERO, REF_2_1, TMP6);
-
-                vis_padd16(TMP0, TMP4, TMP0);
-
-                vis_mul8x16al(DST_0,   CONST_512, TMP4);
-                vis_padd16(TMP2, TMP6, TMP2);
-
-                vis_mul8x16al(DST_1,   CONST_512, TMP6);
-
-                vis_mul8x16au(REF_6,   CONST_256, TMP12);
-
-                vis_padd16(TMP0, TMP4, TMP0);
-                vis_mul8x16au(REF_6_1, CONST_256, TMP14);
-
-                vis_padd16(TMP2, TMP6, TMP2);
-                vis_mul8x16au(REF_4,   CONST_256, TMP16);
-
-                vis_padd16(TMP0, CONST_3, TMP8);
-                vis_mul8x16au(REF_4_1, CONST_256, TMP18);
-
-                vis_padd16(TMP2, CONST_3, TMP10);
-                vis_pack16(TMP8, DST_0);
-
-                vis_pack16(TMP10, DST_1);
-                vis_padd16(TMP16, TMP12, TMP0);
-
-                vis_st64(DST_0, dest[0]);
-                vis_mul8x16al(DST_2,   CONST_512, TMP4);
-                vis_padd16(TMP18, TMP14, TMP2);
-
-                vis_mul8x16al(DST_3,   CONST_512, TMP6);
-                vis_padd16(TMP0, CONST_3, TMP0);
-
-                vis_padd16(TMP2, CONST_3, TMP2);
-
-                vis_padd16(TMP0, TMP4, TMP0);
-
-                vis_padd16(TMP2, TMP6, TMP2);
-                vis_pack16(TMP0, DST_2);
-
-                vis_pack16(TMP2, DST_3);
-                vis_st64(DST_2, dest[8]);
-
-                ref += stride;
-                dest += stride;
-        } while (--height);
-}
-
-static void MC_put_no_round_y_16_vis (uint8_t * dest, const uint8_t * ref,
-                                      const ptrdiff_t stride, int height)
-{
-        ref = vis_alignaddr(ref);
-        vis_ld64(ref[0], TMP0);
-
-        vis_ld64_2(ref, 8, TMP2);
-
-        vis_ld64_2(ref, 16, TMP4);
-        ref += stride;
-
-        vis_ld64(ref[0], TMP6);
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        vis_ld64_2(ref, 8, TMP8);
-        vis_faligndata(TMP2, TMP4, REF_4);
-
-        vis_ld64_2(ref, 16, TMP10);
-        ref += stride;
-
-        vis_ld64(constants_fe[0], MASK_fe);
-        vis_faligndata(TMP6, TMP8, REF_2);
-
-        vis_ld64(constants_7f[0], MASK_7f);
-        vis_faligndata(TMP8, TMP10, REF_6);
-
-        vis_ld64(constants128[0], CONST_128);
-        height = (height >> 1) - 1;
-        do {    /* 24 cycles */
-                vis_ld64(ref[0], TMP0);
-                vis_xor(REF_0, REF_2, TMP12);
-
-                vis_ld64_2(ref, 8, TMP2);
-                vis_xor(REF_4, REF_6, TMP16);
-
-                vis_ld64_2(ref, 16, TMP4);
-                ref += stride;
-                vis_and(REF_0, REF_2, TMP14);
-
-                vis_ld64(ref[0], TMP6);
-                vis_and(REF_4, REF_6, TMP18);
-
-                vis_ld64_2(ref, 8, TMP8);
-                vis_faligndata(TMP0, TMP2, REF_0);
-
-                vis_ld64_2(ref, 16, TMP10);
-                ref += stride;
-                vis_faligndata(TMP2, TMP4, REF_4);
-
-                vis_and(TMP12, MASK_fe, TMP12);
-
-                vis_and(TMP16, MASK_fe, TMP16);
-                vis_mul8x16(CONST_128, TMP12, TMP12);
-
-                vis_mul8x16(CONST_128, TMP16, TMP16);
-                vis_xor(REF_0, REF_2, TMP0);
-
-                vis_xor(REF_4, REF_6, TMP2);
-
-                vis_and(REF_0, REF_2, TMP20);
-
-                vis_and(TMP12, MASK_7f, TMP12);
-
-                vis_and(TMP16, MASK_7f, TMP16);
-
-                vis_padd16(TMP14, TMP12, TMP12);
-                vis_st64(TMP12, dest[0]);
-
-                vis_padd16(TMP18, TMP16, TMP16);
-                vis_st64_2(TMP16, dest, 8);
-                dest += stride;
-
-                vis_and(REF_4, REF_6, TMP18);
-
-                vis_and(TMP0, MASK_fe, TMP0);
-
-                vis_and(TMP2, MASK_fe, TMP2);
-                vis_mul8x16(CONST_128, TMP0, TMP0);
-
-                vis_faligndata(TMP6, TMP8, REF_2);
-                vis_mul8x16(CONST_128, TMP2, TMP2);
-
-                vis_faligndata(TMP8, TMP10, REF_6);
-
-                vis_and(TMP0, MASK_7f, TMP0);
-
-                vis_and(TMP2, MASK_7f, TMP2);
-
-                vis_padd16(TMP20, TMP0, TMP0);
-                vis_st64(TMP0, dest[0]);
-
-                vis_padd16(TMP18, TMP2, TMP2);
-                vis_st64_2(TMP2, dest, 8);
-                dest += stride;
-        } while (--height);
-
-        vis_ld64(ref[0], TMP0);
-        vis_xor(REF_0, REF_2, TMP12);
-
-        vis_ld64_2(ref, 8, TMP2);
-        vis_xor(REF_4, REF_6, TMP16);
-
-        vis_ld64_2(ref, 16, TMP4);
-        vis_and(REF_0, REF_2, TMP14);
-
-        vis_and(REF_4, REF_6, TMP18);
-
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        vis_faligndata(TMP2, TMP4, REF_4);
-
-        vis_and(TMP12, MASK_fe, TMP12);
-
-        vis_and(TMP16, MASK_fe, TMP16);
-        vis_mul8x16(CONST_128, TMP12, TMP12);
-
-        vis_mul8x16(CONST_128, TMP16, TMP16);
-        vis_xor(REF_0, REF_2, TMP0);
-
-        vis_xor(REF_4, REF_6, TMP2);
-
-        vis_and(REF_0, REF_2, TMP20);
-
-        vis_and(TMP12, MASK_7f, TMP12);
-
-        vis_and(TMP16, MASK_7f, TMP16);
-
-        vis_padd16(TMP14, TMP12, TMP12);
-        vis_st64(TMP12, dest[0]);
-
-        vis_padd16(TMP18, TMP16, TMP16);
-        vis_st64_2(TMP16, dest, 8);
-        dest += stride;
-
-        vis_and(REF_4, REF_6, TMP18);
-
-        vis_and(TMP0, MASK_fe, TMP0);
-
-        vis_and(TMP2, MASK_fe, TMP2);
-        vis_mul8x16(CONST_128, TMP0, TMP0);
-
-        vis_mul8x16(CONST_128, TMP2, TMP2);
-
-        vis_and(TMP0, MASK_7f, TMP0);
-
-        vis_and(TMP2, MASK_7f, TMP2);
-
-        vis_padd16(TMP20, TMP0, TMP0);
-        vis_st64(TMP0, dest[0]);
-
-        vis_padd16(TMP18, TMP2, TMP2);
-        vis_st64_2(TMP2, dest, 8);
-}
-
-static void MC_put_no_round_y_8_vis (uint8_t * dest, const uint8_t * ref,
-                                     const ptrdiff_t stride, int height)
-{
-        ref = vis_alignaddr(ref);
-        vis_ld64(ref[0], TMP0);
-
-        vis_ld64_2(ref, 8, TMP2);
-        ref += stride;
-
-        vis_ld64(ref[0], TMP4);
-
-        vis_ld64_2(ref, 8, TMP6);
-        ref += stride;
-
-        vis_ld64(constants_fe[0], MASK_fe);
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        vis_ld64(constants_7f[0], MASK_7f);
-        vis_faligndata(TMP4, TMP6, REF_2);
-
-        vis_ld64(constants128[0], CONST_128);
-        height = (height >> 1) - 1;
-        do {    /* 12 cycles */
-                vis_ld64(ref[0], TMP0);
-                vis_xor(REF_0, REF_2, TMP4);
-
-                vis_ld64_2(ref, 8, TMP2);
-                ref += stride;
-                vis_and(TMP4, MASK_fe, TMP4);
-
-                vis_and(REF_0, REF_2, TMP6);
-                vis_mul8x16(CONST_128, TMP4, TMP4);
-
-                vis_faligndata(TMP0, TMP2, REF_0);
-                vis_ld64(ref[0], TMP0);
-
-                vis_ld64_2(ref, 8, TMP2);
-                ref += stride;
-                vis_xor(REF_0, REF_2, TMP12);
-
-                vis_and(TMP4, MASK_7f, TMP4);
-
-                vis_and(TMP12, MASK_fe, TMP12);
-
-                vis_mul8x16(CONST_128, TMP12, TMP12);
-                vis_and(REF_0, REF_2, TMP14);
-
-                vis_padd16(TMP6, TMP4, DST_0);
-                vis_st64(DST_0, dest[0]);
-                dest += stride;
-
-                vis_faligndata(TMP0, TMP2, REF_2);
-
-                vis_and(TMP12, MASK_7f, TMP12);
-
-                vis_padd16(TMP14, TMP12, DST_0);
-                vis_st64(DST_0, dest[0]);
-                dest += stride;
-        } while (--height);
-
-        vis_ld64(ref[0], TMP0);
-        vis_xor(REF_0, REF_2, TMP4);
-
-        vis_ld64_2(ref, 8, TMP2);
-        vis_and(TMP4, MASK_fe, TMP4);
-
-        vis_and(REF_0, REF_2, TMP6);
-        vis_mul8x16(CONST_128, TMP4, TMP4);
-
-        vis_faligndata(TMP0, TMP2, REF_0);
-
-        vis_xor(REF_0, REF_2, TMP12);
-
-        vis_and(TMP4, MASK_7f, TMP4);
-
-        vis_and(TMP12, MASK_fe, TMP12);
-
-        vis_mul8x16(CONST_128, TMP12, TMP12);
-        vis_and(REF_0, REF_2, TMP14);
-
-        vis_padd16(TMP6, TMP4, DST_0);
-        vis_st64(DST_0, dest[0]);
-        dest += stride;
-
-        vis_and(TMP12, MASK_7f, TMP12);
-
-        vis_padd16(TMP14, TMP12, DST_0);
-        vis_st64(DST_0, dest[0]);
-}
-
-static void MC_avg_no_round_y_16_vis (uint8_t * dest, const uint8_t * ref,
-                                      const ptrdiff_t stride, int height)
-{
-        int stride_8 = stride + 8;
-        int stride_16 = stride + 16;
-
-        vis_set_gsr(5 << VIS_GSR_SCALEFACT_SHIFT);
-
-        ref = vis_alignaddr(ref);
-
-        vis_ld64(ref[ 0], TMP0);
-        vis_fzero(ZERO);
-
-        vis_ld64(ref[ 8], TMP2);
-
-        vis_ld64(ref[16], TMP4);
-
-        vis_ld64(constants3[0], CONST_3);
-        vis_faligndata(TMP0, TMP2, REF_2);
-
-        vis_ld64(constants256_512[0], CONST_256);
-        vis_faligndata(TMP2, TMP4, REF_6);
-        height >>= 1;
-
-        do {    /* 31 cycles */
-                vis_ld64_2(ref, stride, TMP0);
-                vis_pmerge(ZERO,       REF_2,     TMP12);
-                vis_mul8x16au(REF_2_1, CONST_256, TMP14);
-
-                vis_ld64_2(ref, stride_8, TMP2);
-                vis_pmerge(ZERO,       REF_6,     TMP16);
-                vis_mul8x16au(REF_6_1, CONST_256, TMP18);
-
-                vis_ld64_2(ref, stride_16, TMP4);
-                ref += stride;
-
-                vis_ld64(dest[0], DST_0);
-                vis_faligndata(TMP0, TMP2, REF_0);
-
-                vis_ld64_2(dest, 8, DST_2);
-                vis_faligndata(TMP2, TMP4, REF_4);
-
-                vis_ld64_2(ref, stride, TMP6);
-                vis_pmerge(ZERO,     REF_0,     TMP0);
-                vis_mul8x16au(REF_0_1, CONST_256, TMP2);
-
-                vis_ld64_2(ref, stride_8, TMP8);
-                vis_pmerge(ZERO,     REF_4,     TMP4);
-
-                vis_ld64_2(ref, stride_16, TMP10);
-                ref += stride;
-
-                vis_ld64_2(dest, stride, REF_S0/*DST_4*/);
-                vis_faligndata(TMP6, TMP8, REF_2);
-                vis_mul8x16au(REF_4_1, CONST_256, TMP6);
-
-                vis_ld64_2(dest, stride_8, REF_S2/*DST_6*/);
-                vis_faligndata(TMP8, TMP10, REF_6);
-                vis_mul8x16al(DST_0,   CONST_512, TMP20);
-
-                vis_padd16(TMP0, CONST_3, TMP0);
-                vis_mul8x16al(DST_1,   CONST_512, TMP22);
-
-                vis_padd16(TMP2, CONST_3, TMP2);
-                vis_mul8x16al(DST_2,   CONST_512, TMP24);
-
-                vis_padd16(TMP4, CONST_3, TMP4);
-                vis_mul8x16al(DST_3,   CONST_512, TMP26);
-
-                vis_padd16(TMP6, CONST_3, TMP6);
-
-                vis_padd16(TMP12, TMP20, TMP12);
-                vis_mul8x16al(REF_S0,   CONST_512, TMP20);
-
-                vis_padd16(TMP14, TMP22, TMP14);
-                vis_mul8x16al(REF_S0_1, CONST_512, TMP22);
-
-                vis_padd16(TMP16, TMP24, TMP16);
-                vis_mul8x16al(REF_S2,   CONST_512, TMP24);
-
-                vis_padd16(TMP18, TMP26, TMP18);
-                vis_mul8x16al(REF_S2_1, CONST_512, TMP26);
-
-                vis_padd16(TMP12, TMP0, TMP12);
-                vis_mul8x16au(REF_2,   CONST_256, TMP28);
-
-                vis_padd16(TMP14, TMP2, TMP14);
-                vis_mul8x16au(REF_2_1, CONST_256, TMP30);
-
-                vis_padd16(TMP16, TMP4, TMP16);
-                vis_mul8x16au(REF_6,   CONST_256, REF_S4);
-
-                vis_padd16(TMP18, TMP6, TMP18);
-                vis_mul8x16au(REF_6_1, CONST_256, REF_S6);
-
-                vis_pack16(TMP12, DST_0);
-                vis_padd16(TMP28, TMP0, TMP12);
-
-                vis_pack16(TMP14, DST_1);
-                vis_st64(DST_0, dest[0]);
-                vis_padd16(TMP30, TMP2, TMP14);
-
-                vis_pack16(TMP16, DST_2);
-                vis_padd16(REF_S4, TMP4, TMP16);
-
-                vis_pack16(TMP18, DST_3);
-                vis_st64_2(DST_2, dest, 8);
-                dest += stride;
-                vis_padd16(REF_S6, TMP6, TMP18);
-
-                vis_padd16(TMP12, TMP20, TMP12);
-
-                vis_padd16(TMP14, TMP22, TMP14);
-                vis_pack16(TMP12, DST_0);
-
-                vis_padd16(TMP16, TMP24, TMP16);
-                vis_pack16(TMP14, DST_1);
-                vis_st64(DST_0, dest[0]);
-
-                vis_padd16(TMP18, TMP26, TMP18);
-                vis_pack16(TMP16, DST_2);
-
-                vis_pack16(TMP18, DST_3);
-                vis_st64_2(DST_2, dest, 8);
-                dest += stride;
-        } while (--height);
-}
-
-static void MC_put_no_round_xy_16_vis (uint8_t * dest, const uint8_t * ref,
-                                       const ptrdiff_t stride, int height)
-{
-        unsigned long off = (unsigned long) ref & 0x7;
-        unsigned long off_plus_1 = off + 1;
-        int stride_8 = stride + 8;
-        int stride_16 = stride + 16;
-
-        vis_set_gsr(5 << VIS_GSR_SCALEFACT_SHIFT);
-
-        ref = vis_alignaddr(ref);
-
-        vis_ld64(ref[ 0], TMP0);
-        vis_fzero(ZERO);
-
-        vis_ld64(ref[ 8], TMP2);
-
-        vis_ld64(ref[16], TMP4);
-
-        vis_ld64(constants1[0], CONST_1);
-        vis_faligndata(TMP0, TMP2, REF_S0);
-
-        vis_ld64(constants256_512[0], CONST_256);
-        vis_faligndata(TMP2, TMP4, REF_S4);
-
-        if (off != 0x7) {
-                vis_alignaddr_g0((void *)off_plus_1);
-                vis_faligndata(TMP0, TMP2, REF_S2);
-                vis_faligndata(TMP2, TMP4, REF_S6);
-        } else {
-                vis_src1(TMP2, REF_S2);
-                vis_src1(TMP4, REF_S6);
-        }
-
-        height >>= 1;
-        do {
-                vis_ld64_2(ref, stride, TMP0);
-                vis_mul8x16au(REF_S0, CONST_256, TMP12);
-                vis_pmerge(ZERO,      REF_S0_1,  TMP14);
-
-                vis_alignaddr_g0((void *)off);
-
-                vis_ld64_2(ref, stride_8, TMP2);
-                vis_mul8x16au(REF_S2, CONST_256, TMP16);
-                vis_pmerge(ZERO,      REF_S2_1,  TMP18);
-
-                vis_ld64_2(ref, stride_16, TMP4);
-                ref += stride;
-                vis_mul8x16au(REF_S4, CONST_256, TMP20);
-                vis_pmerge(ZERO,      REF_S4_1,  TMP22);
-
-                vis_ld64_2(ref, stride, TMP6);
-                vis_mul8x16au(REF_S6, CONST_256, TMP24);
-                vis_pmerge(ZERO,      REF_S6_1,  TMP26);
-
-                vis_ld64_2(ref, stride_8, TMP8);
-                vis_faligndata(TMP0, TMP2, REF_0);
-
-                vis_ld64_2(ref, stride_16, TMP10);
-                ref += stride;
-                vis_faligndata(TMP2, TMP4, REF_4);
-
-                vis_faligndata(TMP6, TMP8, REF_S0);
-
-                vis_faligndata(TMP8, TMP10, REF_S4);
-
-                if (off != 0x7) {
-                        vis_alignaddr_g0((void *)off_plus_1);
-                        vis_faligndata(TMP0, TMP2, REF_2);
-                        vis_faligndata(TMP2, TMP4, REF_6);
-                        vis_faligndata(TMP6, TMP8, REF_S2);
-                        vis_faligndata(TMP8, TMP10, REF_S6);
-                } else {
-                        vis_src1(TMP2, REF_2);
-                        vis_src1(TMP4, REF_6);
-                        vis_src1(TMP8, REF_S2);
-                        vis_src1(TMP10, REF_S6);
-                }
-
-                vis_mul8x16au(REF_0, CONST_256, TMP0);
-                vis_pmerge(ZERO,      REF_0_1,  TMP2);
-
-                vis_mul8x16au(REF_2, CONST_256, TMP4);
-                vis_pmerge(ZERO,      REF_2_1,  TMP6);
-
-                vis_padd16(TMP0, CONST_2, TMP8);
-                vis_mul8x16au(REF_4, CONST_256, TMP0);
-
-                vis_padd16(TMP2, CONST_1, TMP10);
-                vis_mul8x16au(REF_4_1, CONST_256, TMP2);
-
-                vis_padd16(TMP8, TMP4, TMP8);
-                vis_mul8x16au(REF_6, CONST_256, TMP4);
-
-                vis_padd16(TMP10, TMP6, TMP10);
-                vis_mul8x16au(REF_6_1, CONST_256, TMP6);
-
-                vis_padd16(TMP12, TMP8, TMP12);
-
-                vis_padd16(TMP14, TMP10, TMP14);
-
-                vis_padd16(TMP12, TMP16, TMP12);
-
-                vis_padd16(TMP14, TMP18, TMP14);
-                vis_pack16(TMP12, DST_0);
-
-                vis_pack16(TMP14, DST_1);
-                vis_st64(DST_0, dest[0]);
-                vis_padd16(TMP0, CONST_1, TMP12);
-
-                vis_mul8x16au(REF_S0, CONST_256, TMP0);
-                vis_padd16(TMP2, CONST_1, TMP14);
-
-                vis_mul8x16au(REF_S0_1, CONST_256, TMP2);
-                vis_padd16(TMP12, TMP4, TMP12);
-
-                vis_mul8x16au(REF_S2, CONST_256, TMP4);
-                vis_padd16(TMP14, TMP6, TMP14);
-
-                vis_mul8x16au(REF_S2_1, CONST_256, TMP6);
-                vis_padd16(TMP20, TMP12, TMP20);
-
-                vis_padd16(TMP22, TMP14, TMP22);
-
-                vis_padd16(TMP20, TMP24, TMP20);
-
-                vis_padd16(TMP22, TMP26, TMP22);
-                vis_pack16(TMP20, DST_2);
-
-                vis_pack16(TMP22, DST_3);
-                vis_st64_2(DST_2, dest, 8);
-                dest += stride;
-                vis_padd16(TMP0, TMP4, TMP24);
-
-                vis_mul8x16au(REF_S4, CONST_256, TMP0);
-                vis_padd16(TMP2, TMP6, TMP26);
-
-                vis_mul8x16au(REF_S4_1, CONST_256, TMP2);
-                vis_padd16(TMP24, TMP8, TMP24);
-
-                vis_padd16(TMP26, TMP10, TMP26);
-                vis_pack16(TMP24, DST_0);
-
-                vis_pack16(TMP26, DST_1);
-                vis_st64(DST_0, dest[0]);
-                vis_pmerge(ZERO, REF_S6, TMP4);
-
-                vis_pmerge(ZERO,      REF_S6_1,  TMP6);
-
-                vis_padd16(TMP0, TMP4, TMP0);
-
-                vis_padd16(TMP2, TMP6, TMP2);
-
-                vis_padd16(TMP0, TMP12, TMP0);
-
-                vis_padd16(TMP2, TMP14, TMP2);
-                vis_pack16(TMP0, DST_2);
-
-                vis_pack16(TMP2, DST_3);
-                vis_st64_2(DST_2, dest, 8);
-                dest += stride;
-        } while (--height);
-}
-
-static void MC_put_no_round_xy_8_vis (uint8_t * dest, const uint8_t * ref,
-                                      const ptrdiff_t stride, int height)
-{
-        unsigned long off = (unsigned long) ref & 0x7;
-        unsigned long off_plus_1 = off + 1;
-        int stride_8 = stride + 8;
-
-        vis_set_gsr(5 << VIS_GSR_SCALEFACT_SHIFT);
-
-        ref = vis_alignaddr(ref);
-
-        vis_ld64(ref[ 0], TMP0);
-        vis_fzero(ZERO);
-
-        vis_ld64(ref[ 8], TMP2);
-
-        vis_ld64(constants1[0], CONST_1);
-
-        vis_ld64(constants256_512[0], CONST_256);
-        vis_faligndata(TMP0, TMP2, REF_S0);
-
-        if (off != 0x7) {
-                vis_alignaddr_g0((void *)off_plus_1);
-                vis_faligndata(TMP0, TMP2, REF_S2);
-        } else {
-                vis_src1(TMP2, REF_S2);
-        }
-
-        height >>= 1;
-        do {    /* 26 cycles */
-                vis_ld64_2(ref, stride, TMP0);
-                vis_mul8x16au(REF_S0,   CONST_256, TMP8);
-                vis_pmerge(ZERO,        REF_S2,    TMP12);
-
-                vis_alignaddr_g0((void *)off);
-
-                vis_ld64_2(ref, stride_8, TMP2);
-                ref += stride;
-                vis_mul8x16au(REF_S0_1, CONST_256, TMP10);
-                vis_pmerge(ZERO,        REF_S2_1,  TMP14);
-
-                vis_ld64_2(ref, stride, TMP4);
-
-                vis_ld64_2(ref, stride_8, TMP6);
-                ref += stride;
-                vis_faligndata(TMP0, TMP2, REF_S4);
-
-                vis_pmerge(ZERO, REF_S4, TMP18);
-
-                vis_pmerge(ZERO, REF_S4_1, TMP20);
-
-                vis_faligndata(TMP4, TMP6, REF_S0);
-
-                if (off != 0x7) {
-                        vis_alignaddr_g0((void *)off_plus_1);
-                        vis_faligndata(TMP0, TMP2, REF_S6);
-                        vis_faligndata(TMP4, TMP6, REF_S2);
-                } else {
-                        vis_src1(TMP2, REF_S6);
-                        vis_src1(TMP6, REF_S2);
-                }
-
-                vis_padd16(TMP18, CONST_1, TMP18);
-                vis_mul8x16au(REF_S6,   CONST_256, TMP22);
-
-                vis_padd16(TMP20, CONST_1, TMP20);
-                vis_mul8x16au(REF_S6_1, CONST_256, TMP24);
-
-                vis_mul8x16au(REF_S0,   CONST_256, TMP26);
-                vis_pmerge(ZERO, REF_S0_1, TMP28);
-
-                vis_mul8x16au(REF_S2,   CONST_256, TMP30);
-                vis_padd16(TMP18, TMP22, TMP18);
-
-                vis_mul8x16au(REF_S2_1, CONST_256, TMP32);
-                vis_padd16(TMP20, TMP24, TMP20);
-
-                vis_padd16(TMP8,  TMP18, TMP8);
-
-                vis_padd16(TMP10, TMP20, TMP10);
-
-                vis_padd16(TMP8,  TMP12, TMP8);
-
-                vis_padd16(TMP10, TMP14, TMP10);
-                vis_pack16(TMP8,  DST_0);
-
-                vis_pack16(TMP10, DST_1);
-                vis_st64(DST_0, dest[0]);
-                dest += stride;
-                vis_padd16(TMP18, TMP26, TMP18);
-
-                vis_padd16(TMP20, TMP28, TMP20);
-
-                vis_padd16(TMP18, TMP30, TMP18);
-
-                vis_padd16(TMP20, TMP32, TMP20);
-                vis_pack16(TMP18, DST_2);
-
-                vis_pack16(TMP20, DST_3);
-                vis_st64(DST_2, dest[0]);
-                dest += stride;
-        } while (--height);
-}
-
-static void MC_avg_no_round_xy_16_vis (uint8_t * dest, const uint8_t * ref,
-                                       const ptrdiff_t stride, int height)
-{
-        unsigned long off = (unsigned long) ref & 0x7;
-        unsigned long off_plus_1 = off + 1;
-        int stride_8 = stride + 8;
-        int stride_16 = stride + 16;
-
-        vis_set_gsr(4 << VIS_GSR_SCALEFACT_SHIFT);
-
-        ref = vis_alignaddr(ref);
-
-        vis_ld64(ref[ 0], TMP0);
-        vis_fzero(ZERO);
-
-        vis_ld64(ref[ 8], TMP2);
-
-        vis_ld64(ref[16], TMP4);
-
-        vis_ld64(constants6[0], CONST_6);
-        vis_faligndata(TMP0, TMP2, REF_S0);
-
-        vis_ld64(constants256_1024[0], CONST_256);
-        vis_faligndata(TMP2, TMP4, REF_S4);
-
-        if (off != 0x7) {
-                vis_alignaddr_g0((void *)off_plus_1);
-                vis_faligndata(TMP0, TMP2, REF_S2);
-                vis_faligndata(TMP2, TMP4, REF_S6);
-        } else {
-                vis_src1(TMP2, REF_S2);
-                vis_src1(TMP4, REF_S6);
-        }
-
-        height >>= 1;
-        do {    /* 55 cycles */
-                vis_ld64_2(ref, stride, TMP0);
-                vis_mul8x16au(REF_S0, CONST_256, TMP12);
-                vis_pmerge(ZERO,      REF_S0_1,  TMP14);
-
-                vis_alignaddr_g0((void *)off);
-
-                vis_ld64_2(ref, stride_8, TMP2);
-                vis_mul8x16au(REF_S2, CONST_256, TMP16);
-                vis_pmerge(ZERO,      REF_S2_1,  TMP18);
-
-                vis_ld64_2(ref, stride_16, TMP4);
-                ref += stride;
-                vis_mul8x16au(REF_S4, CONST_256, TMP20);
-                vis_pmerge(ZERO,      REF_S4_1,  TMP22);
-
-                vis_ld64_2(ref, stride, TMP6);
-                vis_mul8x16au(REF_S6, CONST_256, TMP24);
-                vis_pmerge(ZERO,      REF_S6_1,  TMP26);
-
-                vis_ld64_2(ref, stride_8, TMP8);
-                vis_faligndata(TMP0, TMP2, REF_0);
-
-                vis_ld64_2(ref, stride_16, TMP10);
-                ref += stride;
-                vis_faligndata(TMP2, TMP4, REF_4);
-
-                vis_ld64(dest[0], DST_0);
-                vis_faligndata(TMP6, TMP8, REF_S0);
-
-                vis_ld64_2(dest, 8, DST_2);
-                vis_faligndata(TMP8, TMP10, REF_S4);
-
-                if (off != 0x7) {
-                        vis_alignaddr_g0((void *)off_plus_1);
-                        vis_faligndata(TMP0, TMP2, REF_2);
-                        vis_faligndata(TMP2, TMP4, REF_6);
-                        vis_faligndata(TMP6, TMP8, REF_S2);
-                        vis_faligndata(TMP8, TMP10, REF_S6);
-                } else {
-                        vis_src1(TMP2, REF_2);
-                        vis_src1(TMP4, REF_6);
-                        vis_src1(TMP8, REF_S2);
-                        vis_src1(TMP10, REF_S6);
-                }
-
-                vis_mul8x16al(DST_0,   CONST_1024, TMP30);
-                vis_pmerge(ZERO, REF_0, TMP0);
-
-                vis_mul8x16al(DST_1,   CONST_1024, TMP32);
-                vis_pmerge(ZERO,      REF_0_1,  TMP2);
-
-                vis_mul8x16au(REF_2, CONST_256, TMP4);
-                vis_pmerge(ZERO,      REF_2_1,  TMP6);
-
-                vis_mul8x16al(DST_2,   CONST_1024, REF_0);
-                vis_padd16(TMP0, CONST_6, TMP0);
-
-                vis_mul8x16al(DST_3,   CONST_1024, REF_2);
-                vis_padd16(TMP2, CONST_6, TMP2);
-
-                vis_padd16(TMP0, TMP4, TMP0);
-                vis_mul8x16au(REF_4, CONST_256, TMP4);
-
-                vis_padd16(TMP2, TMP6, TMP2);
-                vis_mul8x16au(REF_4_1, CONST_256, TMP6);
-
-                vis_padd16(TMP12, TMP0, TMP12);
-                vis_mul8x16au(REF_6, CONST_256, TMP8);
-
-                vis_padd16(TMP14, TMP2, TMP14);
-                vis_mul8x16au(REF_6_1, CONST_256, TMP10);
-
-                vis_padd16(TMP12, TMP16, TMP12);
-                vis_mul8x16au(REF_S0, CONST_256, REF_4);
-
-                vis_padd16(TMP14, TMP18, TMP14);
-                vis_mul8x16au(REF_S0_1, CONST_256, REF_6);
-
-                vis_padd16(TMP12, TMP30, TMP12);
-
-                vis_padd16(TMP14, TMP32, TMP14);
-                vis_pack16(TMP12, DST_0);
-
-                vis_pack16(TMP14, DST_1);
-                vis_st64(DST_0, dest[0]);
-                vis_padd16(TMP4, CONST_6, TMP4);
-
-                vis_ld64_2(dest, stride, DST_0);
-                vis_padd16(TMP6, CONST_6, TMP6);
-                vis_mul8x16au(REF_S2, CONST_256, TMP12);
-
-                vis_padd16(TMP4, TMP8, TMP4);
-                vis_mul8x16au(REF_S2_1, CONST_256,  TMP14);
-
-                vis_padd16(TMP6, TMP10, TMP6);
-
-                vis_padd16(TMP20, TMP4, TMP20);
-
-                vis_padd16(TMP22, TMP6, TMP22);
-
-                vis_padd16(TMP20, TMP24, TMP20);
-
-                vis_padd16(TMP22, TMP26, TMP22);
-
-                vis_padd16(TMP20, REF_0, TMP20);
-                vis_mul8x16au(REF_S4, CONST_256, REF_0);
-
-                vis_padd16(TMP22, REF_2, TMP22);
-                vis_pack16(TMP20, DST_2);
-
-                vis_pack16(TMP22, DST_3);
-                vis_st64_2(DST_2, dest, 8);
-                dest += stride;
-
-                vis_ld64_2(dest, 8, DST_2);
-                vis_mul8x16al(DST_0,   CONST_1024, TMP30);
-                vis_pmerge(ZERO,      REF_S4_1,  REF_2);
-
-                vis_mul8x16al(DST_1,   CONST_1024, TMP32);
-                vis_padd16(REF_4, TMP0, TMP8);
-
-                vis_mul8x16au(REF_S6, CONST_256, REF_4);
-                vis_padd16(REF_6, TMP2, TMP10);
-
-                vis_mul8x16au(REF_S6_1, CONST_256, REF_6);
-                vis_padd16(TMP8, TMP12, TMP8);
-
-                vis_padd16(TMP10, TMP14, TMP10);
-
-                vis_padd16(TMP8, TMP30, TMP8);
-
-                vis_padd16(TMP10, TMP32, TMP10);
-                vis_pack16(TMP8, DST_0);
-
-                vis_pack16(TMP10, DST_1);
-                vis_st64(DST_0, dest[0]);
-
-                vis_padd16(REF_0, TMP4, REF_0);
-
-                vis_mul8x16al(DST_2,   CONST_1024, TMP30);
-                vis_padd16(REF_2, TMP6, REF_2);
-
-                vis_mul8x16al(DST_3,   CONST_1024, TMP32);
-                vis_padd16(REF_0, REF_4, REF_0);
-
-                vis_padd16(REF_2, REF_6, REF_2);
-
-                vis_padd16(REF_0, TMP30, REF_0);
-
-                /* stall */
-
-                vis_padd16(REF_2, TMP32, REF_2);
-                vis_pack16(REF_0, DST_2);
-
-                vis_pack16(REF_2, DST_3);
-                vis_st64_2(DST_2, dest, 8);
-                dest += stride;
-        } while (--height);
-}
-
-/* End of no rounding code */
-
-#define ACCEL_SPARC_VIS 1
-#define ACCEL_SPARC_VIS2 2
-
-static int vis_level(void)
-{
-    int accel = 0;
-    accel |= ACCEL_SPARC_VIS;
-    accel |= ACCEL_SPARC_VIS2;
-    return accel;
-}
-
-/* libavcodec initialization code */
 av_cold void ff_dsputil_init_vis(DSPContext *c, AVCodecContext *avctx)
 {
   /* VIS-specific optimizations */
@@ -3508,40 +36,5 @@
           c->idct     = ff_simple_idct_vis;
           c->idct_permutation_type = FF_TRANSPOSE_IDCT_PERM;
       }
-
-      c->put_pixels_tab[0][0] = MC_put_o_16_vis;
-      c->put_pixels_tab[0][1] = MC_put_x_16_vis;
-      c->put_pixels_tab[0][2] = MC_put_y_16_vis;
-      c->put_pixels_tab[0][3] = MC_put_xy_16_vis;
-
-      c->put_pixels_tab[1][0] = MC_put_o_8_vis;
-      c->put_pixels_tab[1][1] = MC_put_x_8_vis;
-      c->put_pixels_tab[1][2] = MC_put_y_8_vis;
-      c->put_pixels_tab[1][3] = MC_put_xy_8_vis;
-
-      c->avg_pixels_tab[0][0] = MC_avg_o_16_vis;
-      c->avg_pixels_tab[0][1] = MC_avg_x_16_vis;
-      c->avg_pixels_tab[0][2] = MC_avg_y_16_vis;
-      c->avg_pixels_tab[0][3] = MC_avg_xy_16_vis;
-
-      c->avg_pixels_tab[1][0] = MC_avg_o_8_vis;
-      c->avg_pixels_tab[1][1] = MC_avg_x_8_vis;
-      c->avg_pixels_tab[1][2] = MC_avg_y_8_vis;
-      c->avg_pixels_tab[1][3] = MC_avg_xy_8_vis;
-
-      c->put_no_rnd_pixels_tab[0][0] = MC_put_no_round_o_16_vis;
-      c->put_no_rnd_pixels_tab[0][1] = MC_put_no_round_x_16_vis;
-      c->put_no_rnd_pixels_tab[0][2] = MC_put_no_round_y_16_vis;
-      c->put_no_rnd_pixels_tab[0][3] = MC_put_no_round_xy_16_vis;
-
-      c->put_no_rnd_pixels_tab[1][0] = MC_put_no_round_o_8_vis;
-      c->put_no_rnd_pixels_tab[1][1] = MC_put_no_round_x_8_vis;
-      c->put_no_rnd_pixels_tab[1][2] = MC_put_no_round_y_8_vis;
-      c->put_no_rnd_pixels_tab[1][3] = MC_put_no_round_xy_8_vis;
-
-      c->avg_no_rnd_pixels_tab[0] = MC_avg_no_round_o_16_vis;
-      c->avg_no_rnd_pixels_tab[1] = MC_avg_no_round_x_16_vis;
-      c->avg_no_rnd_pixels_tab[2] = MC_avg_no_round_y_16_vis;
-      c->avg_no_rnd_pixels_tab[3] = MC_avg_no_round_xy_16_vis;
   }
 }
diff --git a/libavcodec/sparc/hpeldsp_vis.c b/libavcodec/sparc/hpeldsp_vis.c
new file mode 100644
index 0000000..be2a85d
--- /dev/null
+++ b/libavcodec/sparc/hpeldsp_vis.c
@@ -0,0 +1,3524 @@
+/*
+ * Copyright (C) 2003 David S. Miller <davem@redhat.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
+ */
+
+/* The *no_round* functions have been added by James A. Morrison, 2003,2004.
+   The vis code from libmpeg2 was adapted for libavcodec by James A. Morrison.
+ */
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "libavutil/attributes.h"
+#include "libavutil/mem.h"
+#include "libavcodec/hpeldsp.h"
+#include "vis.h"
+
+/* The trick used in some of this file is the formula from the MMX
+ * motion comp code, which is:
+ *
+ * (x+y+1)>>1 == (x|y)-((x^y)>>1)
+ *
+ * This allows us to average 8 bytes at a time in a 64-bit FPU reg.
+ * We avoid overflows by masking before we do the shift, and we
+ * implement the shift by multiplying by 1/2 using mul8x16.  So in
+ * VIS this is (assume 'x' is in f0, 'y' is in f2, a repeating mask
+ * of '0xfe' is in f4, a repeating mask of '0x7f' is in f6, and
+ * the value 0x80808080 is in f8):
+ *
+ *      fxor            f0,   f2, f10
+ *      fand            f10,  f4, f10
+ *      fmul8x16        f8,  f10, f10
+ *      fand            f10,  f6, f10
+ *      for             f0,   f2, f12
+ *      fpsub16         f12, f10, f10
+ */
+
+#define DUP4(x) {x, x, x, x}
+#define DUP8(x) {x, x, x, x, x, x, x, x}
+DECLARE_ALIGNED(8, static const int16_t, constants1)[] = DUP4 (1);
+DECLARE_ALIGNED(8, static const int16_t, constants2)[] = DUP4 (2);
+DECLARE_ALIGNED(8, static const int16_t, constants3)[] = DUP4 (3);
+DECLARE_ALIGNED(8, static const int16_t, constants6)[] = DUP4 (6);
+DECLARE_ALIGNED(8, static const int8_t, constants_fe)[] = DUP8 (0xfe);
+DECLARE_ALIGNED(8, static const int8_t, constants_7f)[] = DUP8 (0x7f);
+DECLARE_ALIGNED(8, static const int8_t, constants128)[] = DUP8 (128);
+DECLARE_ALIGNED(8, static const int16_t, constants256_512)[] =
+        {256, 512, 256, 512};
+DECLARE_ALIGNED(8, static const int16_t, constants256_1024)[] =
+        {256, 1024, 256, 1024};
+
+#define REF_0           0
+#define REF_0_1         1
+#define REF_2           2
+#define REF_2_1         3
+#define REF_4           4
+#define REF_4_1         5
+#define REF_6           6
+#define REF_6_1         7
+#define REF_S0          8
+#define REF_S0_1        9
+#define REF_S2          10
+#define REF_S2_1        11
+#define REF_S4          12
+#define REF_S4_1        13
+#define REF_S6          14
+#define REF_S6_1        15
+#define DST_0           16
+#define DST_1           17
+#define DST_2           18
+#define DST_3           19
+#define CONST_1         20
+#define CONST_2         20
+#define CONST_3         20
+#define CONST_6         20
+#define MASK_fe         20
+#define CONST_128       22
+#define CONST_256       22
+#define CONST_512       22
+#define CONST_1024      22
+#define TMP0            24
+#define TMP1            25
+#define TMP2            26
+#define TMP3            27
+#define TMP4            28
+#define TMP5            29
+#define ZERO            30
+#define MASK_7f         30
+
+#define TMP6            32
+#define TMP8            34
+#define TMP10           36
+#define TMP12           38
+#define TMP14           40
+#define TMP16           42
+#define TMP18           44
+#define TMP20           46
+#define TMP22           48
+#define TMP24           50
+#define TMP26           52
+#define TMP28           54
+#define TMP30           56
+#define TMP32           58
+
+static void MC_put_o_16_vis (uint8_t * dest, const uint8_t * ref,
+                             const ptrdiff_t stride, int height)
+{
+        ref = vis_alignaddr(ref);
+        do {    /* 5 cycles */
+                vis_ld64(ref[0], TMP0);
+
+                vis_ld64_2(ref, 8, TMP2);
+
+                vis_ld64_2(ref, 16, TMP4);
+                ref += stride;
+
+                vis_faligndata(TMP0, TMP2, REF_0);
+                vis_st64(REF_0, dest[0]);
+
+                vis_faligndata(TMP2, TMP4, REF_2);
+                vis_st64_2(REF_2, dest, 8);
+                dest += stride;
+        } while (--height);
+}
+
+static void MC_put_o_8_vis (uint8_t * dest, const uint8_t * ref,
+                            const ptrdiff_t stride, int height)
+{
+        ref = vis_alignaddr(ref);
+        do {    /* 4 cycles */
+                vis_ld64(ref[0], TMP0);
+
+                vis_ld64(ref[8], TMP2);
+                ref += stride;
+
+                /* stall */
+
+                vis_faligndata(TMP0, TMP2, REF_0);
+                vis_st64(REF_0, dest[0]);
+                dest += stride;
+        } while (--height);
+}
+
+
+static void MC_avg_o_16_vis (uint8_t * dest, const uint8_t * ref,
+                             const ptrdiff_t stride, int height)
+{
+        int stride_8 = stride + 8;
+
+        ref = vis_alignaddr(ref);
+
+        vis_ld64(ref[0], TMP0);
+
+        vis_ld64(ref[8], TMP2);
+
+        vis_ld64(ref[16], TMP4);
+
+        vis_ld64(dest[0], DST_0);
+
+        vis_ld64(dest[8], DST_2);
+
+        vis_ld64(constants_fe[0], MASK_fe);
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        vis_ld64(constants_7f[0], MASK_7f);
+        vis_faligndata(TMP2, TMP4, REF_2);
+
+        vis_ld64(constants128[0], CONST_128);
+
+        ref += stride;
+        height = (height >> 1) - 1;
+
+        do {    /* 24 cycles */
+                vis_ld64(ref[0], TMP0);
+                vis_xor(DST_0, REF_0, TMP6);
+
+                vis_ld64_2(ref, 8, TMP2);
+                vis_and(TMP6, MASK_fe, TMP6);
+
+                vis_ld64_2(ref, 16, TMP4);
+                ref += stride;
+                vis_mul8x16(CONST_128, TMP6, TMP6);
+                vis_xor(DST_2, REF_2, TMP8);
+
+                vis_and(TMP8, MASK_fe, TMP8);
+
+                vis_or(DST_0, REF_0, TMP10);
+                vis_ld64_2(dest, stride, DST_0);
+                vis_mul8x16(CONST_128, TMP8, TMP8);
+
+                vis_or(DST_2, REF_2, TMP12);
+                vis_ld64_2(dest, stride_8, DST_2);
+
+                vis_ld64(ref[0], TMP14);
+                vis_and(TMP6, MASK_7f, TMP6);
+
+                vis_and(TMP8, MASK_7f, TMP8);
+
+                vis_psub16(TMP10, TMP6, TMP6);
+                vis_st64(TMP6, dest[0]);
+
+                vis_psub16(TMP12, TMP8, TMP8);
+                vis_st64_2(TMP8, dest, 8);
+
+                dest += stride;
+                vis_ld64_2(ref, 8, TMP16);
+                vis_faligndata(TMP0, TMP2, REF_0);
+
+                vis_ld64_2(ref, 16, TMP18);
+                vis_faligndata(TMP2, TMP4, REF_2);
+                ref += stride;
+
+                vis_xor(DST_0, REF_0, TMP20);
+
+                vis_and(TMP20, MASK_fe, TMP20);
+
+                vis_xor(DST_2, REF_2, TMP22);
+                vis_mul8x16(CONST_128, TMP20, TMP20);
+
+                vis_and(TMP22, MASK_fe, TMP22);
+
+                vis_or(DST_0, REF_0, TMP24);
+                vis_mul8x16(CONST_128, TMP22, TMP22);
+
+                vis_or(DST_2, REF_2, TMP26);
+
+                vis_ld64_2(dest, stride, DST_0);
+                vis_faligndata(TMP14, TMP16, REF_0);
+
+                vis_ld64_2(dest, stride_8, DST_2);
+                vis_faligndata(TMP16, TMP18, REF_2);
+
+                vis_and(TMP20, MASK_7f, TMP20);
+
+                vis_and(TMP22, MASK_7f, TMP22);
+
+                vis_psub16(TMP24, TMP20, TMP20);
+                vis_st64(TMP20, dest[0]);
+
+                vis_psub16(TMP26, TMP22, TMP22);
+                vis_st64_2(TMP22, dest, 8);
+                dest += stride;
+        } while (--height);
+
+        vis_ld64(ref[0], TMP0);
+        vis_xor(DST_0, REF_0, TMP6);
+
+        vis_ld64_2(ref, 8, TMP2);
+        vis_and(TMP6, MASK_fe, TMP6);
+
+        vis_ld64_2(ref, 16, TMP4);
+        vis_mul8x16(CONST_128, TMP6, TMP6);
+        vis_xor(DST_2, REF_2, TMP8);
+
+        vis_and(TMP8, MASK_fe, TMP8);
+
+        vis_or(DST_0, REF_0, TMP10);
+        vis_ld64_2(dest, stride, DST_0);
+        vis_mul8x16(CONST_128, TMP8, TMP8);
+
+        vis_or(DST_2, REF_2, TMP12);
+        vis_ld64_2(dest, stride_8, DST_2);
+
+        vis_ld64(ref[0], TMP14);
+        vis_and(TMP6, MASK_7f, TMP6);
+
+        vis_and(TMP8, MASK_7f, TMP8);
+
+        vis_psub16(TMP10, TMP6, TMP6);
+        vis_st64(TMP6, dest[0]);
+
+        vis_psub16(TMP12, TMP8, TMP8);
+        vis_st64_2(TMP8, dest, 8);
+
+        dest += stride;
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        vis_faligndata(TMP2, TMP4, REF_2);
+
+        vis_xor(DST_0, REF_0, TMP20);
+
+        vis_and(TMP20, MASK_fe, TMP20);
+
+        vis_xor(DST_2, REF_2, TMP22);
+        vis_mul8x16(CONST_128, TMP20, TMP20);
+
+        vis_and(TMP22, MASK_fe, TMP22);
+
+        vis_or(DST_0, REF_0, TMP24);
+        vis_mul8x16(CONST_128, TMP22, TMP22);
+
+        vis_or(DST_2, REF_2, TMP26);
+
+        vis_and(TMP20, MASK_7f, TMP20);
+
+        vis_and(TMP22, MASK_7f, TMP22);
+
+        vis_psub16(TMP24, TMP20, TMP20);
+        vis_st64(TMP20, dest[0]);
+
+        vis_psub16(TMP26, TMP22, TMP22);
+        vis_st64_2(TMP22, dest, 8);
+}
+
+static void MC_avg_o_8_vis (uint8_t * dest, const uint8_t * ref,
+                            const ptrdiff_t stride, int height)
+{
+        ref = vis_alignaddr(ref);
+
+        vis_ld64(ref[0], TMP0);
+
+        vis_ld64(ref[8], TMP2);
+
+        vis_ld64(dest[0], DST_0);
+
+        vis_ld64(constants_fe[0], MASK_fe);
+
+        vis_ld64(constants_7f[0], MASK_7f);
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        vis_ld64(constants128[0], CONST_128);
+
+        ref += stride;
+        height = (height >> 1) - 1;
+
+        do {    /* 12 cycles */
+                vis_ld64(ref[0], TMP0);
+                vis_xor(DST_0, REF_0, TMP4);
+
+                vis_ld64(ref[8], TMP2);
+                vis_and(TMP4, MASK_fe, TMP4);
+
+                vis_or(DST_0, REF_0, TMP6);
+                vis_ld64_2(dest, stride, DST_0);
+                ref += stride;
+                vis_mul8x16(CONST_128, TMP4, TMP4);
+
+                vis_ld64(ref[0], TMP12);
+                vis_faligndata(TMP0, TMP2, REF_0);
+
+                vis_ld64(ref[8], TMP2);
+                vis_xor(DST_0, REF_0, TMP0);
+                ref += stride;
+
+                vis_and(TMP0, MASK_fe, TMP0);
+
+                vis_and(TMP4, MASK_7f, TMP4);
+
+                vis_psub16(TMP6, TMP4, TMP4);
+                vis_st64(TMP4, dest[0]);
+                dest += stride;
+                vis_mul8x16(CONST_128, TMP0, TMP0);
+
+                vis_or(DST_0, REF_0, TMP6);
+                vis_ld64_2(dest, stride, DST_0);
+
+                vis_faligndata(TMP12, TMP2, REF_0);
+
+                vis_and(TMP0, MASK_7f, TMP0);
+
+                vis_psub16(TMP6, TMP0, TMP4);
+                vis_st64(TMP4, dest[0]);
+                dest += stride;
+        } while (--height);
+
+        vis_ld64(ref[0], TMP0);
+        vis_xor(DST_0, REF_0, TMP4);
+
+        vis_ld64(ref[8], TMP2);
+        vis_and(TMP4, MASK_fe, TMP4);
+
+        vis_or(DST_0, REF_0, TMP6);
+        vis_ld64_2(dest, stride, DST_0);
+        vis_mul8x16(CONST_128, TMP4, TMP4);
+
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        vis_xor(DST_0, REF_0, TMP0);
+
+        vis_and(TMP0, MASK_fe, TMP0);
+
+        vis_and(TMP4, MASK_7f, TMP4);
+
+        vis_psub16(TMP6, TMP4, TMP4);
+        vis_st64(TMP4, dest[0]);
+        dest += stride;
+        vis_mul8x16(CONST_128, TMP0, TMP0);
+
+        vis_or(DST_0, REF_0, TMP6);
+
+        vis_and(TMP0, MASK_7f, TMP0);
+
+        vis_psub16(TMP6, TMP0, TMP4);
+        vis_st64(TMP4, dest[0]);
+}
+
+static void MC_put_x_16_vis (uint8_t * dest, const uint8_t * ref,
+                             const ptrdiff_t stride, int height)
+{
+        unsigned long off = (unsigned long) ref & 0x7;
+        unsigned long off_plus_1 = off + 1;
+
+        ref = vis_alignaddr(ref);
+
+        vis_ld64(ref[0],    TMP0);
+
+        vis_ld64_2(ref, 8,  TMP2);
+
+        vis_ld64_2(ref, 16, TMP4);
+
+        vis_ld64(constants_fe[0], MASK_fe);
+
+        vis_ld64(constants_7f[0], MASK_7f);
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        vis_ld64(constants128[0], CONST_128);
+        vis_faligndata(TMP2, TMP4, REF_4);
+
+        if (off != 0x7) {
+                vis_alignaddr_g0((void *)off_plus_1);
+                vis_faligndata(TMP0, TMP2, REF_2);
+                vis_faligndata(TMP2, TMP4, REF_6);
+        } else {
+                vis_src1(TMP2, REF_2);
+                vis_src1(TMP4, REF_6);
+        }
+
+        ref += stride;
+        height = (height >> 1) - 1;
+
+        do {    /* 34 cycles */
+                vis_ld64(ref[0],    TMP0);
+                vis_xor(REF_0, REF_2, TMP6);
+
+                vis_ld64_2(ref, 8,  TMP2);
+                vis_xor(REF_4, REF_6, TMP8);
+
+                vis_ld64_2(ref, 16, TMP4);
+                vis_and(TMP6, MASK_fe, TMP6);
+                ref += stride;
+
+                vis_ld64(ref[0],    TMP14);
+                vis_mul8x16(CONST_128, TMP6, TMP6);
+                vis_and(TMP8, MASK_fe, TMP8);
+
+                vis_ld64_2(ref, 8,  TMP16);
+                vis_mul8x16(CONST_128, TMP8, TMP8);
+                vis_or(REF_0, REF_2, TMP10);
+
+                vis_ld64_2(ref, 16, TMP18);
+                ref += stride;
+                vis_or(REF_4, REF_6, TMP12);
+
+                vis_alignaddr_g0((void *)off);
+
+                vis_faligndata(TMP0, TMP2, REF_0);
+
+                vis_faligndata(TMP2, TMP4, REF_4);
+
+                if (off != 0x7) {
+                        vis_alignaddr_g0((void *)off_plus_1);
+                        vis_faligndata(TMP0, TMP2, REF_2);
+                        vis_faligndata(TMP2, TMP4, REF_6);
+                } else {
+                        vis_src1(TMP2, REF_2);
+                        vis_src1(TMP4, REF_6);
+                }
+
+                vis_and(TMP6, MASK_7f, TMP6);
+
+                vis_and(TMP8, MASK_7f, TMP8);
+
+                vis_psub16(TMP10, TMP6, TMP6);
+                vis_st64(TMP6, dest[0]);
+
+                vis_psub16(TMP12, TMP8, TMP8);
+                vis_st64_2(TMP8, dest, 8);
+                dest += stride;
+
+                vis_xor(REF_0, REF_2, TMP6);
+
+                vis_xor(REF_4, REF_6, TMP8);
+
+                vis_and(TMP6, MASK_fe, TMP6);
+
+                vis_mul8x16(CONST_128, TMP6, TMP6);
+                vis_and(TMP8, MASK_fe, TMP8);
+
+                vis_mul8x16(CONST_128, TMP8, TMP8);
+                vis_or(REF_0, REF_2, TMP10);
+
+                vis_or(REF_4, REF_6, TMP12);
+
+                vis_alignaddr_g0((void *)off);
+
+                vis_faligndata(TMP14, TMP16, REF_0);
+
+                vis_faligndata(TMP16, TMP18, REF_4);
+
+                if (off != 0x7) {
+                        vis_alignaddr_g0((void *)off_plus_1);
+                        vis_faligndata(TMP14, TMP16, REF_2);
+                        vis_faligndata(TMP16, TMP18, REF_6);
+                } else {
+                        vis_src1(TMP16, REF_2);
+                        vis_src1(TMP18, REF_6);
+                }
+
+                vis_and(TMP6, MASK_7f, TMP6);
+
+                vis_and(TMP8, MASK_7f, TMP8);
+
+                vis_psub16(TMP10, TMP6, TMP6);
+                vis_st64(TMP6, dest[0]);
+
+                vis_psub16(TMP12, TMP8, TMP8);
+                vis_st64_2(TMP8, dest, 8);
+                dest += stride;
+        } while (--height);
+
+        vis_ld64(ref[0],    TMP0);
+        vis_xor(REF_0, REF_2, TMP6);
+
+        vis_ld64_2(ref, 8,  TMP2);
+        vis_xor(REF_4, REF_6, TMP8);
+
+        vis_ld64_2(ref, 16, TMP4);
+        vis_and(TMP6, MASK_fe, TMP6);
+
+        vis_mul8x16(CONST_128, TMP6, TMP6);
+        vis_and(TMP8, MASK_fe, TMP8);
+
+        vis_mul8x16(CONST_128, TMP8, TMP8);
+        vis_or(REF_0, REF_2, TMP10);
+
+        vis_or(REF_4, REF_6, TMP12);
+
+        vis_alignaddr_g0((void *)off);
+
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        vis_faligndata(TMP2, TMP4, REF_4);
+
+        if (off != 0x7) {
+                vis_alignaddr_g0((void *)off_plus_1);
+                vis_faligndata(TMP0, TMP2, REF_2);
+                vis_faligndata(TMP2, TMP4, REF_6);
+        } else {
+                vis_src1(TMP2, REF_2);
+                vis_src1(TMP4, REF_6);
+        }
+
+        vis_and(TMP6, MASK_7f, TMP6);
+
+        vis_and(TMP8, MASK_7f, TMP8);
+
+        vis_psub16(TMP10, TMP6, TMP6);
+        vis_st64(TMP6, dest[0]);
+
+        vis_psub16(TMP12, TMP8, TMP8);
+        vis_st64_2(TMP8, dest, 8);
+        dest += stride;
+
+        vis_xor(REF_0, REF_2, TMP6);
+
+        vis_xor(REF_4, REF_6, TMP8);
+
+        vis_and(TMP6, MASK_fe, TMP6);
+
+        vis_mul8x16(CONST_128, TMP6, TMP6);
+        vis_and(TMP8, MASK_fe, TMP8);
+
+        vis_mul8x16(CONST_128, TMP8, TMP8);
+        vis_or(REF_0, REF_2, TMP10);
+
+        vis_or(REF_4, REF_6, TMP12);
+
+        vis_and(TMP6, MASK_7f, TMP6);
+
+        vis_and(TMP8, MASK_7f, TMP8);
+
+        vis_psub16(TMP10, TMP6, TMP6);
+        vis_st64(TMP6, dest[0]);
+
+        vis_psub16(TMP12, TMP8, TMP8);
+        vis_st64_2(TMP8, dest, 8);
+}
+
+static void MC_put_x_8_vis (uint8_t * dest, const uint8_t * ref,
+                            const ptrdiff_t stride, int height)
+{
+        unsigned long off = (unsigned long) ref & 0x7;
+        unsigned long off_plus_1 = off + 1;
+
+        ref = vis_alignaddr(ref);
+
+        vis_ld64(ref[0], TMP0);
+
+        vis_ld64(ref[8], TMP2);
+
+        vis_ld64(constants_fe[0], MASK_fe);
+
+        vis_ld64(constants_7f[0], MASK_7f);
+
+        vis_ld64(constants128[0], CONST_128);
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        if (off != 0x7) {
+                vis_alignaddr_g0((void *)off_plus_1);
+                vis_faligndata(TMP0, TMP2, REF_2);
+        } else {
+                vis_src1(TMP2, REF_2);
+        }
+
+        ref += stride;
+        height = (height >> 1) - 1;
+
+        do {    /* 20 cycles */
+                vis_ld64(ref[0], TMP0);
+                vis_xor(REF_0, REF_2, TMP4);
+
+                vis_ld64_2(ref, 8, TMP2);
+                vis_and(TMP4, MASK_fe, TMP4);
+                ref += stride;
+
+                vis_ld64(ref[0], TMP8);
+                vis_or(REF_0, REF_2, TMP6);
+                vis_mul8x16(CONST_128, TMP4, TMP4);
+
+                vis_alignaddr_g0((void *)off);
+
+                vis_ld64_2(ref, 8, TMP10);
+                ref += stride;
+                vis_faligndata(TMP0, TMP2, REF_0);
+
+                if (off != 0x7) {
+                        vis_alignaddr_g0((void *)off_plus_1);
+                        vis_faligndata(TMP0, TMP2, REF_2);
+                } else {
+                        vis_src1(TMP2, REF_2);
+                }
+
+                vis_and(TMP4, MASK_7f, TMP4);
+
+                vis_psub16(TMP6, TMP4, DST_0);
+                vis_st64(DST_0, dest[0]);
+                dest += stride;
+
+                vis_xor(REF_0, REF_2, TMP12);
+
+                vis_and(TMP12, MASK_fe, TMP12);
+
+                vis_or(REF_0, REF_2, TMP14);
+                vis_mul8x16(CONST_128, TMP12, TMP12);
+
+                vis_alignaddr_g0((void *)off);
+                vis_faligndata(TMP8, TMP10, REF_0);
+                if (off != 0x7) {
+                        vis_alignaddr_g0((void *)off_plus_1);
+                        vis_faligndata(TMP8, TMP10, REF_2);
+                } else {
+                        vis_src1(TMP10, REF_2);
+                }
+
+                vis_and(TMP12, MASK_7f, TMP12);
+
+                vis_psub16(TMP14, TMP12, DST_0);
+                vis_st64(DST_0, dest[0]);
+                dest += stride;
+        } while (--height);
+
+        vis_ld64(ref[0], TMP0);
+        vis_xor(REF_0, REF_2, TMP4);
+
+        vis_ld64_2(ref, 8, TMP2);
+        vis_and(TMP4, MASK_fe, TMP4);
+
+        vis_or(REF_0, REF_2, TMP6);
+        vis_mul8x16(CONST_128, TMP4, TMP4);
+
+        vis_alignaddr_g0((void *)off);
+
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        if (off != 0x7) {
+                vis_alignaddr_g0((void *)off_plus_1);
+                vis_faligndata(TMP0, TMP2, REF_2);
+        } else {
+                vis_src1(TMP2, REF_2);
+        }
+
+        vis_and(TMP4, MASK_7f, TMP4);
+
+        vis_psub16(TMP6, TMP4, DST_0);
+        vis_st64(DST_0, dest[0]);
+        dest += stride;
+
+        vis_xor(REF_0, REF_2, TMP12);
+
+        vis_and(TMP12, MASK_fe, TMP12);
+
+        vis_or(REF_0, REF_2, TMP14);
+        vis_mul8x16(CONST_128, TMP12, TMP12);
+
+        vis_and(TMP12, MASK_7f, TMP12);
+
+        vis_psub16(TMP14, TMP12, DST_0);
+        vis_st64(DST_0, dest[0]);
+        dest += stride;
+}
+
+static void MC_avg_x_16_vis (uint8_t * dest, const uint8_t * ref,
+                             const ptrdiff_t stride, int height)
+{
+        unsigned long off = (unsigned long) ref & 0x7;
+        unsigned long off_plus_1 = off + 1;
+
+        vis_set_gsr(5 << VIS_GSR_SCALEFACT_SHIFT);
+
+        vis_ld64(constants3[0], CONST_3);
+        vis_fzero(ZERO);
+        vis_ld64(constants256_512[0], CONST_256);
+
+        ref = vis_alignaddr(ref);
+        do {    /* 26 cycles */
+                vis_ld64(ref[0], TMP0);
+
+                vis_ld64(ref[8], TMP2);
+
+                vis_alignaddr_g0((void *)off);
+
+                vis_ld64(ref[16], TMP4);
+
+                vis_ld64(dest[0], DST_0);
+                vis_faligndata(TMP0, TMP2, REF_0);
+
+                vis_ld64(dest[8], DST_2);
+                vis_faligndata(TMP2, TMP4, REF_4);
+
+                if (off != 0x7) {
+                        vis_alignaddr_g0((void *)off_plus_1);
+                        vis_faligndata(TMP0, TMP2, REF_2);
+                        vis_faligndata(TMP2, TMP4, REF_6);
+                } else {
+                        vis_src1(TMP2, REF_2);
+                        vis_src1(TMP4, REF_6);
+                }
+
+                vis_mul8x16au(REF_0,   CONST_256, TMP0);
+
+                vis_pmerge(ZERO,     REF_2,     TMP4);
+                vis_mul8x16au(REF_0_1, CONST_256, TMP2);
+
+                vis_pmerge(ZERO, REF_2_1, TMP6);
+
+                vis_padd16(TMP0, TMP4, TMP0);
+
+                vis_mul8x16al(DST_0,   CONST_512, TMP4);
+                vis_padd16(TMP2, TMP6, TMP2);
+
+                vis_mul8x16al(DST_1,   CONST_512, TMP6);
+
+                vis_mul8x16au(REF_6,   CONST_256, TMP12);
+
+                vis_padd16(TMP0, TMP4, TMP0);
+                vis_mul8x16au(REF_6_1, CONST_256, TMP14);
+
+                vis_padd16(TMP2, TMP6, TMP2);
+                vis_mul8x16au(REF_4,   CONST_256, TMP16);
+
+                vis_padd16(TMP0, CONST_3, TMP8);
+                vis_mul8x16au(REF_4_1, CONST_256, TMP18);
+
+                vis_padd16(TMP2, CONST_3, TMP10);
+                vis_pack16(TMP8, DST_0);
+
+                vis_pack16(TMP10, DST_1);
+                vis_padd16(TMP16, TMP12, TMP0);
+
+                vis_st64(DST_0, dest[0]);
+                vis_mul8x16al(DST_2,   CONST_512, TMP4);
+                vis_padd16(TMP18, TMP14, TMP2);
+
+                vis_mul8x16al(DST_3,   CONST_512, TMP6);
+                vis_padd16(TMP0, CONST_3, TMP0);
+
+                vis_padd16(TMP2, CONST_3, TMP2);
+
+                vis_padd16(TMP0, TMP4, TMP0);
+
+                vis_padd16(TMP2, TMP6, TMP2);
+                vis_pack16(TMP0, DST_2);
+
+                vis_pack16(TMP2, DST_3);
+                vis_st64(DST_2, dest[8]);
+
+                ref += stride;
+                dest += stride;
+        } while (--height);
+}
+
+static void MC_avg_x_8_vis (uint8_t * dest, const uint8_t * ref,
+                            const ptrdiff_t stride, int height)
+{
+        unsigned long off = (unsigned long) ref & 0x7;
+        unsigned long off_plus_1 = off + 1;
+        int stride_times_2 = stride << 1;
+
+        vis_set_gsr(5 << VIS_GSR_SCALEFACT_SHIFT);
+
+        vis_ld64(constants3[0], CONST_3);
+        vis_fzero(ZERO);
+        vis_ld64(constants256_512[0], CONST_256);
+
+        ref = vis_alignaddr(ref);
+        height >>= 2;
+        do {    /* 47 cycles */
+                vis_ld64(ref[0],   TMP0);
+
+                vis_ld64_2(ref, 8, TMP2);
+                ref += stride;
+
+                vis_alignaddr_g0((void *)off);
+
+                vis_ld64(ref[0],   TMP4);
+                vis_faligndata(TMP0, TMP2, REF_0);
+
+                vis_ld64_2(ref, 8, TMP6);
+                ref += stride;
+
+                vis_ld64(ref[0],   TMP8);
+
+                vis_ld64_2(ref, 8, TMP10);
+                ref += stride;
+                vis_faligndata(TMP4, TMP6, REF_4);
+
+                vis_ld64(ref[0],   TMP12);
+
+                vis_ld64_2(ref, 8, TMP14);
+                ref += stride;
+                vis_faligndata(TMP8, TMP10, REF_S0);
+
+                vis_faligndata(TMP12, TMP14, REF_S4);
+
+                if (off != 0x7) {
+                        vis_alignaddr_g0((void *)off_plus_1);
+
+                        vis_ld64(dest[0], DST_0);
+                        vis_faligndata(TMP0, TMP2, REF_2);
+
+                        vis_ld64_2(dest, stride, DST_2);
+                        vis_faligndata(TMP4, TMP6, REF_6);
+
+                        vis_faligndata(TMP8, TMP10, REF_S2);
+
+                        vis_faligndata(TMP12, TMP14, REF_S6);
+                } else {
+                        vis_ld64(dest[0], DST_0);
+                        vis_src1(TMP2, REF_2);
+
+                        vis_ld64_2(dest, stride, DST_2);
+                        vis_src1(TMP6, REF_6);
+
+                        vis_src1(TMP10, REF_S2);
+
+                        vis_src1(TMP14, REF_S6);
+                }
+
+                vis_pmerge(ZERO,     REF_0,     TMP0);
+                vis_mul8x16au(REF_0_1, CONST_256, TMP2);
+
+                vis_pmerge(ZERO,     REF_2,     TMP4);
+                vis_mul8x16au(REF_2_1, CONST_256, TMP6);
+
+                vis_padd16(TMP0, CONST_3, TMP0);
+                vis_mul8x16al(DST_0,   CONST_512, TMP16);
+
+                vis_padd16(TMP2, CONST_3, TMP2);
+                vis_mul8x16al(DST_1,   CONST_512, TMP18);
+
+                vis_padd16(TMP0, TMP4, TMP0);
+                vis_mul8x16au(REF_4, CONST_256, TMP8);
+
+                vis_padd16(TMP2, TMP6, TMP2);
+                vis_mul8x16au(REF_4_1, CONST_256, TMP10);
+
+                vis_padd16(TMP0, TMP16, TMP0);
+                vis_mul8x16au(REF_6, CONST_256, TMP12);
+
+                vis_padd16(TMP2, TMP18, TMP2);
+                vis_mul8x16au(REF_6_1, CONST_256, TMP14);
+
+                vis_padd16(TMP8, CONST_3, TMP8);
+                vis_mul8x16al(DST_2, CONST_512, TMP16);
+
+                vis_padd16(TMP8, TMP12, TMP8);
+                vis_mul8x16al(DST_3, CONST_512, TMP18);
+
+                vis_padd16(TMP10, TMP14, TMP10);
+                vis_pack16(TMP0, DST_0);
+
+                vis_pack16(TMP2, DST_1);
+                vis_st64(DST_0, dest[0]);
+                dest += stride;
+                vis_padd16(TMP10, CONST_3, TMP10);
+
+                vis_ld64_2(dest, stride, DST_0);
+                vis_padd16(TMP8, TMP16, TMP8);
+
+                vis_ld64_2(dest, stride_times_2, TMP4/*DST_2*/);
+                vis_padd16(TMP10, TMP18, TMP10);
+                vis_pack16(TMP8, DST_2);
+
+                vis_pack16(TMP10, DST_3);
+                vis_st64(DST_2, dest[0]);
+                dest += stride;
+
+                vis_mul8x16au(REF_S0_1, CONST_256, TMP2);
+                vis_pmerge(ZERO,     REF_S0,     TMP0);
+
+                vis_pmerge(ZERO,     REF_S2,     TMP24);
+                vis_mul8x16au(REF_S2_1, CONST_256, TMP6);
+
+                vis_padd16(TMP0, CONST_3, TMP0);
+                vis_mul8x16au(REF_S4, CONST_256, TMP8);
+
+                vis_padd16(TMP2, CONST_3, TMP2);
+                vis_mul8x16au(REF_S4_1, CONST_256, TMP10);
+
+                vis_padd16(TMP0, TMP24, TMP0);
+                vis_mul8x16au(REF_S6, CONST_256, TMP12);
+
+                vis_padd16(TMP2, TMP6, TMP2);
+                vis_mul8x16au(REF_S6_1, CONST_256, TMP14);
+
+                vis_padd16(TMP8, CONST_3, TMP8);
+                vis_mul8x16al(DST_0,   CONST_512, TMP16);
+
+                vis_padd16(TMP10, CONST_3, TMP10);
+                vis_mul8x16al(DST_1,   CONST_512, TMP18);
+
+                vis_padd16(TMP8, TMP12, TMP8);
+                vis_mul8x16al(TMP4/*DST_2*/, CONST_512, TMP20);
+
+                vis_mul8x16al(TMP5/*DST_3*/, CONST_512, TMP22);
+                vis_padd16(TMP0, TMP16, TMP0);
+
+                vis_padd16(TMP2, TMP18, TMP2);
+                vis_pack16(TMP0, DST_0);
+
+                vis_padd16(TMP10, TMP14, TMP10);
+                vis_pack16(TMP2, DST_1);
+                vis_st64(DST_0, dest[0]);
+                dest += stride;
+
+                vis_padd16(TMP8, TMP20, TMP8);
+
+                vis_padd16(TMP10, TMP22, TMP10);
+                vis_pack16(TMP8, DST_2);
+
+                vis_pack16(TMP10, DST_3);
+                vis_st64(DST_2, dest[0]);
+                dest += stride;
+        } while (--height);
+}
+
+static void MC_put_y_16_vis (uint8_t * dest, const uint8_t * ref,
+                             const ptrdiff_t stride, int height)
+{
+        ref = vis_alignaddr(ref);
+        vis_ld64(ref[0], TMP0);
+
+        vis_ld64_2(ref, 8, TMP2);
+
+        vis_ld64_2(ref, 16, TMP4);
+        ref += stride;
+
+        vis_ld64(ref[0], TMP6);
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        vis_ld64_2(ref, 8, TMP8);
+        vis_faligndata(TMP2, TMP4, REF_4);
+
+        vis_ld64_2(ref, 16, TMP10);
+        ref += stride;
+
+        vis_ld64(constants_fe[0], MASK_fe);
+        vis_faligndata(TMP6, TMP8, REF_2);
+
+        vis_ld64(constants_7f[0], MASK_7f);
+        vis_faligndata(TMP8, TMP10, REF_6);
+
+        vis_ld64(constants128[0], CONST_128);
+        height = (height >> 1) - 1;
+        do {    /* 24 cycles */
+                vis_ld64(ref[0], TMP0);
+                vis_xor(REF_0, REF_2, TMP12);
+
+                vis_ld64_2(ref, 8, TMP2);
+                vis_xor(REF_4, REF_6, TMP16);
+
+                vis_ld64_2(ref, 16, TMP4);
+                ref += stride;
+                vis_or(REF_0, REF_2, TMP14);
+
+                vis_ld64(ref[0], TMP6);
+                vis_or(REF_4, REF_6, TMP18);
+
+                vis_ld64_2(ref, 8, TMP8);
+                vis_faligndata(TMP0, TMP2, REF_0);
+
+                vis_ld64_2(ref, 16, TMP10);
+                ref += stride;
+                vis_faligndata(TMP2, TMP4, REF_4);
+
+                vis_and(TMP12, MASK_fe, TMP12);
+
+                vis_and(TMP16, MASK_fe, TMP16);
+                vis_mul8x16(CONST_128, TMP12, TMP12);
+
+                vis_mul8x16(CONST_128, TMP16, TMP16);
+                vis_xor(REF_0, REF_2, TMP0);
+
+                vis_xor(REF_4, REF_6, TMP2);
+
+                vis_or(REF_0, REF_2, TMP20);
+
+                vis_and(TMP12, MASK_7f, TMP12);
+
+                vis_and(TMP16, MASK_7f, TMP16);
+
+                vis_psub16(TMP14, TMP12, TMP12);
+                vis_st64(TMP12, dest[0]);
+
+                vis_psub16(TMP18, TMP16, TMP16);
+                vis_st64_2(TMP16, dest, 8);
+                dest += stride;
+
+                vis_or(REF_4, REF_6, TMP18);
+
+                vis_and(TMP0, MASK_fe, TMP0);
+
+                vis_and(TMP2, MASK_fe, TMP2);
+                vis_mul8x16(CONST_128, TMP0, TMP0);
+
+                vis_faligndata(TMP6, TMP8, REF_2);
+                vis_mul8x16(CONST_128, TMP2, TMP2);
+
+                vis_faligndata(TMP8, TMP10, REF_6);
+
+                vis_and(TMP0, MASK_7f, TMP0);
+
+                vis_and(TMP2, MASK_7f, TMP2);
+
+                vis_psub16(TMP20, TMP0, TMP0);
+                vis_st64(TMP0, dest[0]);
+
+                vis_psub16(TMP18, TMP2, TMP2);
+                vis_st64_2(TMP2, dest, 8);
+                dest += stride;
+        } while (--height);
+
+        vis_ld64(ref[0], TMP0);
+        vis_xor(REF_0, REF_2, TMP12);
+
+        vis_ld64_2(ref, 8, TMP2);
+        vis_xor(REF_4, REF_6, TMP16);
+
+        vis_ld64_2(ref, 16, TMP4);
+        vis_or(REF_0, REF_2, TMP14);
+
+        vis_or(REF_4, REF_6, TMP18);
+
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        vis_faligndata(TMP2, TMP4, REF_4);
+
+        vis_and(TMP12, MASK_fe, TMP12);
+
+        vis_and(TMP16, MASK_fe, TMP16);
+        vis_mul8x16(CONST_128, TMP12, TMP12);
+
+        vis_mul8x16(CONST_128, TMP16, TMP16);
+        vis_xor(REF_0, REF_2, TMP0);
+
+        vis_xor(REF_4, REF_6, TMP2);
+
+        vis_or(REF_0, REF_2, TMP20);
+
+        vis_and(TMP12, MASK_7f, TMP12);
+
+        vis_and(TMP16, MASK_7f, TMP16);
+
+        vis_psub16(TMP14, TMP12, TMP12);
+        vis_st64(TMP12, dest[0]);
+
+        vis_psub16(TMP18, TMP16, TMP16);
+        vis_st64_2(TMP16, dest, 8);
+        dest += stride;
+
+        vis_or(REF_4, REF_6, TMP18);
+
+        vis_and(TMP0, MASK_fe, TMP0);
+
+        vis_and(TMP2, MASK_fe, TMP2);
+        vis_mul8x16(CONST_128, TMP0, TMP0);
+
+        vis_mul8x16(CONST_128, TMP2, TMP2);
+
+        vis_and(TMP0, MASK_7f, TMP0);
+
+        vis_and(TMP2, MASK_7f, TMP2);
+
+        vis_psub16(TMP20, TMP0, TMP0);
+        vis_st64(TMP0, dest[0]);
+
+        vis_psub16(TMP18, TMP2, TMP2);
+        vis_st64_2(TMP2, dest, 8);
+}
+
+static void MC_put_y_8_vis (uint8_t * dest, const uint8_t * ref,
+                            const ptrdiff_t stride, int height)
+{
+        ref = vis_alignaddr(ref);
+        vis_ld64(ref[0], TMP0);
+
+        vis_ld64_2(ref, 8, TMP2);
+        ref += stride;
+
+        vis_ld64(ref[0], TMP4);
+
+        vis_ld64_2(ref, 8, TMP6);
+        ref += stride;
+
+        vis_ld64(constants_fe[0], MASK_fe);
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        vis_ld64(constants_7f[0], MASK_7f);
+        vis_faligndata(TMP4, TMP6, REF_2);
+
+        vis_ld64(constants128[0], CONST_128);
+        height = (height >> 1) - 1;
+        do {    /* 12 cycles */
+                vis_ld64(ref[0], TMP0);
+                vis_xor(REF_0, REF_2, TMP4);
+
+                vis_ld64_2(ref, 8, TMP2);
+                ref += stride;
+                vis_and(TMP4, MASK_fe, TMP4);
+
+                vis_or(REF_0, REF_2, TMP6);
+                vis_mul8x16(CONST_128, TMP4, TMP4);
+
+                vis_faligndata(TMP0, TMP2, REF_0);
+                vis_ld64(ref[0], TMP0);
+
+                vis_ld64_2(ref, 8, TMP2);
+                ref += stride;
+                vis_xor(REF_0, REF_2, TMP12);
+
+                vis_and(TMP4, MASK_7f, TMP4);
+
+                vis_and(TMP12, MASK_fe, TMP12);
+
+                vis_mul8x16(CONST_128, TMP12, TMP12);
+                vis_or(REF_0, REF_2, TMP14);
+
+                vis_psub16(TMP6, TMP4, DST_0);
+                vis_st64(DST_0, dest[0]);
+                dest += stride;
+
+                vis_faligndata(TMP0, TMP2, REF_2);
+
+                vis_and(TMP12, MASK_7f, TMP12);
+
+                vis_psub16(TMP14, TMP12, DST_0);
+                vis_st64(DST_0, dest[0]);
+                dest += stride;
+        } while (--height);
+
+        vis_ld64(ref[0], TMP0);
+        vis_xor(REF_0, REF_2, TMP4);
+
+        vis_ld64_2(ref, 8, TMP2);
+        vis_and(TMP4, MASK_fe, TMP4);
+
+        vis_or(REF_0, REF_2, TMP6);
+        vis_mul8x16(CONST_128, TMP4, TMP4);
+
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        vis_xor(REF_0, REF_2, TMP12);
+
+        vis_and(TMP4, MASK_7f, TMP4);
+
+        vis_and(TMP12, MASK_fe, TMP12);
+
+        vis_mul8x16(CONST_128, TMP12, TMP12);
+        vis_or(REF_0, REF_2, TMP14);
+
+        vis_psub16(TMP6, TMP4, DST_0);
+        vis_st64(DST_0, dest[0]);
+        dest += stride;
+
+        vis_and(TMP12, MASK_7f, TMP12);
+
+        vis_psub16(TMP14, TMP12, DST_0);
+        vis_st64(DST_0, dest[0]);
+}
+
+static void MC_avg_y_16_vis (uint8_t * dest, const uint8_t * ref,
+                             const ptrdiff_t stride, int height)
+{
+        int stride_8 = stride + 8;
+        int stride_16 = stride + 16;
+
+        vis_set_gsr(5 << VIS_GSR_SCALEFACT_SHIFT);
+
+        ref = vis_alignaddr(ref);
+
+        vis_ld64(ref[ 0], TMP0);
+        vis_fzero(ZERO);
+
+        vis_ld64(ref[ 8], TMP2);
+
+        vis_ld64(ref[16], TMP4);
+
+        vis_ld64(constants3[0], CONST_3);
+        vis_faligndata(TMP0, TMP2, REF_2);
+
+        vis_ld64(constants256_512[0], CONST_256);
+        vis_faligndata(TMP2, TMP4, REF_6);
+        height >>= 1;
+
+        do {    /* 31 cycles */
+                vis_ld64_2(ref, stride, TMP0);
+                vis_pmerge(ZERO,       REF_2,     TMP12);
+                vis_mul8x16au(REF_2_1, CONST_256, TMP14);
+
+                vis_ld64_2(ref, stride_8, TMP2);
+                vis_pmerge(ZERO,       REF_6,     TMP16);
+                vis_mul8x16au(REF_6_1, CONST_256, TMP18);
+
+                vis_ld64_2(ref, stride_16, TMP4);
+                ref += stride;
+
+                vis_ld64(dest[0], DST_0);
+                vis_faligndata(TMP0, TMP2, REF_0);
+
+                vis_ld64_2(dest, 8, DST_2);
+                vis_faligndata(TMP2, TMP4, REF_4);
+
+                vis_ld64_2(ref, stride, TMP6);
+                vis_pmerge(ZERO,     REF_0,     TMP0);
+                vis_mul8x16au(REF_0_1, CONST_256, TMP2);
+
+                vis_ld64_2(ref, stride_8, TMP8);
+                vis_pmerge(ZERO,     REF_4,     TMP4);
+
+                vis_ld64_2(ref, stride_16, TMP10);
+                ref += stride;
+
+                vis_ld64_2(dest, stride, REF_S0/*DST_4*/);
+                vis_faligndata(TMP6, TMP8, REF_2);
+                vis_mul8x16au(REF_4_1, CONST_256, TMP6);
+
+                vis_ld64_2(dest, stride_8, REF_S2/*DST_6*/);
+                vis_faligndata(TMP8, TMP10, REF_6);
+                vis_mul8x16al(DST_0,   CONST_512, TMP20);
+
+                vis_padd16(TMP0, CONST_3, TMP0);
+                vis_mul8x16al(DST_1,   CONST_512, TMP22);
+
+                vis_padd16(TMP2, CONST_3, TMP2);
+                vis_mul8x16al(DST_2,   CONST_512, TMP24);
+
+                vis_padd16(TMP4, CONST_3, TMP4);
+                vis_mul8x16al(DST_3,   CONST_512, TMP26);
+
+                vis_padd16(TMP6, CONST_3, TMP6);
+
+                vis_padd16(TMP12, TMP20, TMP12);
+                vis_mul8x16al(REF_S0,   CONST_512, TMP20);
+
+                vis_padd16(TMP14, TMP22, TMP14);
+                vis_mul8x16al(REF_S0_1, CONST_512, TMP22);
+
+                vis_padd16(TMP16, TMP24, TMP16);
+                vis_mul8x16al(REF_S2,   CONST_512, TMP24);
+
+                vis_padd16(TMP18, TMP26, TMP18);
+                vis_mul8x16al(REF_S2_1, CONST_512, TMP26);
+
+                vis_padd16(TMP12, TMP0, TMP12);
+                vis_mul8x16au(REF_2,   CONST_256, TMP28);
+
+                vis_padd16(TMP14, TMP2, TMP14);
+                vis_mul8x16au(REF_2_1, CONST_256, TMP30);
+
+                vis_padd16(TMP16, TMP4, TMP16);
+                vis_mul8x16au(REF_6,   CONST_256, REF_S4);
+
+                vis_padd16(TMP18, TMP6, TMP18);
+                vis_mul8x16au(REF_6_1, CONST_256, REF_S6);
+
+                vis_pack16(TMP12, DST_0);
+                vis_padd16(TMP28, TMP0, TMP12);
+
+                vis_pack16(TMP14, DST_1);
+                vis_st64(DST_0, dest[0]);
+                vis_padd16(TMP30, TMP2, TMP14);
+
+                vis_pack16(TMP16, DST_2);
+                vis_padd16(REF_S4, TMP4, TMP16);
+
+                vis_pack16(TMP18, DST_3);
+                vis_st64_2(DST_2, dest, 8);
+                dest += stride;
+                vis_padd16(REF_S6, TMP6, TMP18);
+
+                vis_padd16(TMP12, TMP20, TMP12);
+
+                vis_padd16(TMP14, TMP22, TMP14);
+                vis_pack16(TMP12, DST_0);
+
+                vis_padd16(TMP16, TMP24, TMP16);
+                vis_pack16(TMP14, DST_1);
+                vis_st64(DST_0, dest[0]);
+
+                vis_padd16(TMP18, TMP26, TMP18);
+                vis_pack16(TMP16, DST_2);
+
+                vis_pack16(TMP18, DST_3);
+                vis_st64_2(DST_2, dest, 8);
+                dest += stride;
+        } while (--height);
+}
+
+static void MC_avg_y_8_vis (uint8_t * dest, const uint8_t * ref,
+                            const ptrdiff_t stride, int height)
+{
+        int stride_8 = stride + 8;
+
+        vis_set_gsr(5 << VIS_GSR_SCALEFACT_SHIFT);
+
+        ref = vis_alignaddr(ref);
+
+        vis_ld64(ref[ 0], TMP0);
+        vis_fzero(ZERO);
+
+        vis_ld64(ref[ 8], TMP2);
+
+        vis_ld64(constants3[0], CONST_3);
+        vis_faligndata(TMP0, TMP2, REF_2);
+
+        vis_ld64(constants256_512[0], CONST_256);
+
+        height >>= 1;
+        do {    /* 20 cycles */
+                vis_ld64_2(ref, stride, TMP0);
+                vis_pmerge(ZERO,       REF_2,     TMP8);
+                vis_mul8x16au(REF_2_1, CONST_256, TMP10);
+
+                vis_ld64_2(ref, stride_8, TMP2);
+                ref += stride;
+
+                vis_ld64(dest[0], DST_0);
+
+                vis_ld64_2(dest, stride, DST_2);
+                vis_faligndata(TMP0, TMP2, REF_0);
+
+                vis_ld64_2(ref, stride, TMP4);
+                vis_mul8x16al(DST_0,   CONST_512, TMP16);
+                vis_pmerge(ZERO,       REF_0,     TMP12);
+
+                vis_ld64_2(ref, stride_8, TMP6);
+                ref += stride;
+                vis_mul8x16al(DST_1,   CONST_512, TMP18);
+                vis_pmerge(ZERO,       REF_0_1,   TMP14);
+
+                vis_padd16(TMP12, CONST_3, TMP12);
+                vis_mul8x16al(DST_2,   CONST_512, TMP24);
+
+                vis_padd16(TMP14, CONST_3, TMP14);
+                vis_mul8x16al(DST_3,   CONST_512, TMP26);
+
+                vis_faligndata(TMP4, TMP6, REF_2);
+
+                vis_padd16(TMP8, TMP12, TMP8);
+
+                vis_padd16(TMP10, TMP14, TMP10);
+                vis_mul8x16au(REF_2,   CONST_256, TMP20);
+
+                vis_padd16(TMP8, TMP16, TMP0);
+                vis_mul8x16au(REF_2_1, CONST_256, TMP22);
+
+                vis_padd16(TMP10, TMP18, TMP2);
+                vis_pack16(TMP0, DST_0);
+
+                vis_pack16(TMP2, DST_1);
+                vis_st64(DST_0, dest[0]);
+                dest += stride;
+                vis_padd16(TMP12, TMP20, TMP12);
+
+                vis_padd16(TMP14, TMP22, TMP14);
+
+                vis_padd16(TMP12, TMP24, TMP0);
+
+                vis_padd16(TMP14, TMP26, TMP2);
+                vis_pack16(TMP0, DST_2);
+
+                vis_pack16(TMP2, DST_3);
+                vis_st64(DST_2, dest[0]);
+                dest += stride;
+        } while (--height);
+}
+
+static void MC_put_xy_16_vis (uint8_t * dest, const uint8_t * ref,
+                              const ptrdiff_t stride, int height)
+{
+        unsigned long off = (unsigned long) ref & 0x7;
+        unsigned long off_plus_1 = off + 1;
+        int stride_8 = stride + 8;
+        int stride_16 = stride + 16;
+
+        vis_set_gsr(5 << VIS_GSR_SCALEFACT_SHIFT);
+
+        ref = vis_alignaddr(ref);
+
+        vis_ld64(ref[ 0], TMP0);
+        vis_fzero(ZERO);
+
+        vis_ld64(ref[ 8], TMP2);
+
+        vis_ld64(ref[16], TMP4);
+
+        vis_ld64(constants2[0], CONST_2);
+        vis_faligndata(TMP0, TMP2, REF_S0);
+
+        vis_ld64(constants256_512[0], CONST_256);
+        vis_faligndata(TMP2, TMP4, REF_S4);
+
+        if (off != 0x7) {
+                vis_alignaddr_g0((void *)off_plus_1);
+                vis_faligndata(TMP0, TMP2, REF_S2);
+                vis_faligndata(TMP2, TMP4, REF_S6);
+        } else {
+                vis_src1(TMP2, REF_S2);
+                vis_src1(TMP4, REF_S6);
+        }
+
+        height >>= 1;
+        do {
+                vis_ld64_2(ref, stride, TMP0);
+                vis_mul8x16au(REF_S0, CONST_256, TMP12);
+                vis_pmerge(ZERO,      REF_S0_1,  TMP14);
+
+                vis_alignaddr_g0((void *)off);
+
+                vis_ld64_2(ref, stride_8, TMP2);
+                vis_mul8x16au(REF_S2, CONST_256, TMP16);
+                vis_pmerge(ZERO,      REF_S2_1,  TMP18);
+
+                vis_ld64_2(ref, stride_16, TMP4);
+                ref += stride;
+                vis_mul8x16au(REF_S4, CONST_256, TMP20);
+                vis_pmerge(ZERO,      REF_S4_1,  TMP22);
+
+                vis_ld64_2(ref, stride, TMP6);
+                vis_mul8x16au(REF_S6, CONST_256, TMP24);
+                vis_pmerge(ZERO,      REF_S6_1,  TMP26);
+
+                vis_ld64_2(ref, stride_8, TMP8);
+                vis_faligndata(TMP0, TMP2, REF_0);
+
+                vis_ld64_2(ref, stride_16, TMP10);
+                ref += stride;
+                vis_faligndata(TMP2, TMP4, REF_4);
+
+                vis_faligndata(TMP6, TMP8, REF_S0);
+
+                vis_faligndata(TMP8, TMP10, REF_S4);
+
+                if (off != 0x7) {
+                        vis_alignaddr_g0((void *)off_plus_1);
+                        vis_faligndata(TMP0, TMP2, REF_2);
+                        vis_faligndata(TMP2, TMP4, REF_6);
+                        vis_faligndata(TMP6, TMP8, REF_S2);
+                        vis_faligndata(TMP8, TMP10, REF_S6);
+                } else {
+                        vis_src1(TMP2, REF_2);
+                        vis_src1(TMP4, REF_6);
+                        vis_src1(TMP8, REF_S2);
+                        vis_src1(TMP10, REF_S6);
+                }
+
+                vis_mul8x16au(REF_0, CONST_256, TMP0);
+                vis_pmerge(ZERO,      REF_0_1,  TMP2);
+
+                vis_mul8x16au(REF_2, CONST_256, TMP4);
+                vis_pmerge(ZERO,      REF_2_1,  TMP6);
+
+                vis_padd16(TMP0, CONST_2, TMP8);
+                vis_mul8x16au(REF_4, CONST_256, TMP0);
+
+                vis_padd16(TMP2, CONST_2, TMP10);
+                vis_mul8x16au(REF_4_1, CONST_256, TMP2);
+
+                vis_padd16(TMP8, TMP4, TMP8);
+                vis_mul8x16au(REF_6, CONST_256, TMP4);
+
+                vis_padd16(TMP10, TMP6, TMP10);
+                vis_mul8x16au(REF_6_1, CONST_256, TMP6);
+
+                vis_padd16(TMP12, TMP8, TMP12);
+
+                vis_padd16(TMP14, TMP10, TMP14);
+
+                vis_padd16(TMP12, TMP16, TMP12);
+
+                vis_padd16(TMP14, TMP18, TMP14);
+                vis_pack16(TMP12, DST_0);
+
+                vis_pack16(TMP14, DST_1);
+                vis_st64(DST_0, dest[0]);
+                vis_padd16(TMP0, CONST_2, TMP12);
+
+                vis_mul8x16au(REF_S0, CONST_256, TMP0);
+                vis_padd16(TMP2, CONST_2, TMP14);
+
+                vis_mul8x16au(REF_S0_1, CONST_256, TMP2);
+                vis_padd16(TMP12, TMP4, TMP12);
+
+                vis_mul8x16au(REF_S2, CONST_256, TMP4);
+                vis_padd16(TMP14, TMP6, TMP14);
+
+                vis_mul8x16au(REF_S2_1, CONST_256, TMP6);
+                vis_padd16(TMP20, TMP12, TMP20);
+
+                vis_padd16(TMP22, TMP14, TMP22);
+
+                vis_padd16(TMP20, TMP24, TMP20);
+
+                vis_padd16(TMP22, TMP26, TMP22);
+                vis_pack16(TMP20, DST_2);
+
+                vis_pack16(TMP22, DST_3);
+                vis_st64_2(DST_2, dest, 8);
+                dest += stride;
+                vis_padd16(TMP0, TMP4, TMP24);
+
+                vis_mul8x16au(REF_S4, CONST_256, TMP0);
+                vis_padd16(TMP2, TMP6, TMP26);
+
+                vis_mul8x16au(REF_S4_1, CONST_256, TMP2);
+                vis_padd16(TMP24, TMP8, TMP24);
+
+                vis_padd16(TMP26, TMP10, TMP26);
+                vis_pack16(TMP24, DST_0);
+
+                vis_pack16(TMP26, DST_1);
+                vis_st64(DST_0, dest[0]);
+                vis_pmerge(ZERO, REF_S6, TMP4);
+
+                vis_pmerge(ZERO,      REF_S6_1,  TMP6);
+
+                vis_padd16(TMP0, TMP4, TMP0);
+
+                vis_padd16(TMP2, TMP6, TMP2);
+
+                vis_padd16(TMP0, TMP12, TMP0);
+
+                vis_padd16(TMP2, TMP14, TMP2);
+                vis_pack16(TMP0, DST_2);
+
+                vis_pack16(TMP2, DST_3);
+                vis_st64_2(DST_2, dest, 8);
+                dest += stride;
+        } while (--height);
+}
+
+static void MC_put_xy_8_vis (uint8_t * dest, const uint8_t * ref,
+                             const ptrdiff_t stride, int height)
+{
+        unsigned long off = (unsigned long) ref & 0x7;
+        unsigned long off_plus_1 = off + 1;
+        int stride_8 = stride + 8;
+
+        vis_set_gsr(5 << VIS_GSR_SCALEFACT_SHIFT);
+
+        ref = vis_alignaddr(ref);
+
+        vis_ld64(ref[ 0], TMP0);
+        vis_fzero(ZERO);
+
+        vis_ld64(ref[ 8], TMP2);
+
+        vis_ld64(constants2[0], CONST_2);
+
+        vis_ld64(constants256_512[0], CONST_256);
+        vis_faligndata(TMP0, TMP2, REF_S0);
+
+        if (off != 0x7) {
+                vis_alignaddr_g0((void *)off_plus_1);
+                vis_faligndata(TMP0, TMP2, REF_S2);
+        } else {
+                vis_src1(TMP2, REF_S2);
+        }
+
+        height >>= 1;
+        do {    /* 26 cycles */
+                vis_ld64_2(ref, stride, TMP0);
+                vis_mul8x16au(REF_S0,   CONST_256, TMP8);
+                vis_pmerge(ZERO,        REF_S2,    TMP12);
+
+                vis_alignaddr_g0((void *)off);
+
+                vis_ld64_2(ref, stride_8, TMP2);
+                ref += stride;
+                vis_mul8x16au(REF_S0_1, CONST_256, TMP10);
+                vis_pmerge(ZERO,        REF_S2_1,  TMP14);
+
+                vis_ld64_2(ref, stride, TMP4);
+
+                vis_ld64_2(ref, stride_8, TMP6);
+                ref += stride;
+                vis_faligndata(TMP0, TMP2, REF_S4);
+
+                vis_pmerge(ZERO, REF_S4, TMP18);
+
+                vis_pmerge(ZERO, REF_S4_1, TMP20);
+
+                vis_faligndata(TMP4, TMP6, REF_S0);
+
+                if (off != 0x7) {
+                        vis_alignaddr_g0((void *)off_plus_1);
+                        vis_faligndata(TMP0, TMP2, REF_S6);
+                        vis_faligndata(TMP4, TMP6, REF_S2);
+                } else {
+                        vis_src1(TMP2, REF_S6);
+                        vis_src1(TMP6, REF_S2);
+                }
+
+                vis_padd16(TMP18, CONST_2, TMP18);
+                vis_mul8x16au(REF_S6,   CONST_256, TMP22);
+
+                vis_padd16(TMP20, CONST_2, TMP20);
+                vis_mul8x16au(REF_S6_1, CONST_256, TMP24);
+
+                vis_mul8x16au(REF_S0,   CONST_256, TMP26);
+                vis_pmerge(ZERO, REF_S0_1, TMP28);
+
+                vis_mul8x16au(REF_S2,   CONST_256, TMP30);
+                vis_padd16(TMP18, TMP22, TMP18);
+
+                vis_mul8x16au(REF_S2_1, CONST_256, TMP32);
+                vis_padd16(TMP20, TMP24, TMP20);
+
+                vis_padd16(TMP8,  TMP18, TMP8);
+
+                vis_padd16(TMP10, TMP20, TMP10);
+
+                vis_padd16(TMP8,  TMP12, TMP8);
+
+                vis_padd16(TMP10, TMP14, TMP10);
+                vis_pack16(TMP8,  DST_0);
+
+                vis_pack16(TMP10, DST_1);
+                vis_st64(DST_0, dest[0]);
+                dest += stride;
+                vis_padd16(TMP18, TMP26, TMP18);
+
+                vis_padd16(TMP20, TMP28, TMP20);
+
+                vis_padd16(TMP18, TMP30, TMP18);
+
+                vis_padd16(TMP20, TMP32, TMP20);
+                vis_pack16(TMP18, DST_2);
+
+                vis_pack16(TMP20, DST_3);
+                vis_st64(DST_2, dest[0]);
+                dest += stride;
+        } while (--height);
+}
+
+static void MC_avg_xy_16_vis (uint8_t * dest, const uint8_t * ref,
+                              const ptrdiff_t stride, int height)
+{
+        unsigned long off = (unsigned long) ref & 0x7;
+        unsigned long off_plus_1 = off + 1;
+        int stride_8 = stride + 8;
+        int stride_16 = stride + 16;
+
+        vis_set_gsr(4 << VIS_GSR_SCALEFACT_SHIFT);
+
+        ref = vis_alignaddr(ref);
+
+        vis_ld64(ref[ 0], TMP0);
+        vis_fzero(ZERO);
+
+        vis_ld64(ref[ 8], TMP2);
+
+        vis_ld64(ref[16], TMP4);
+
+        vis_ld64(constants6[0], CONST_6);
+        vis_faligndata(TMP0, TMP2, REF_S0);
+
+        vis_ld64(constants256_1024[0], CONST_256);
+        vis_faligndata(TMP2, TMP4, REF_S4);
+
+        if (off != 0x7) {
+                vis_alignaddr_g0((void *)off_plus_1);
+                vis_faligndata(TMP0, TMP2, REF_S2);
+                vis_faligndata(TMP2, TMP4, REF_S6);
+        } else {
+                vis_src1(TMP2, REF_S2);
+                vis_src1(TMP4, REF_S6);
+        }
+
+        height >>= 1;
+        do {    /* 55 cycles */
+                vis_ld64_2(ref, stride, TMP0);
+                vis_mul8x16au(REF_S0, CONST_256, TMP12);
+                vis_pmerge(ZERO,      REF_S0_1,  TMP14);
+
+                vis_alignaddr_g0((void *)off);
+
+                vis_ld64_2(ref, stride_8, TMP2);
+                vis_mul8x16au(REF_S2, CONST_256, TMP16);
+                vis_pmerge(ZERO,      REF_S2_1,  TMP18);
+
+                vis_ld64_2(ref, stride_16, TMP4);
+                ref += stride;
+                vis_mul8x16au(REF_S4, CONST_256, TMP20);
+                vis_pmerge(ZERO,      REF_S4_1,  TMP22);
+
+                vis_ld64_2(ref, stride, TMP6);
+                vis_mul8x16au(REF_S6, CONST_256, TMP24);
+                vis_pmerge(ZERO,      REF_S6_1,  TMP26);
+
+                vis_ld64_2(ref, stride_8, TMP8);
+                vis_faligndata(TMP0, TMP2, REF_0);
+
+                vis_ld64_2(ref, stride_16, TMP10);
+                ref += stride;
+                vis_faligndata(TMP2, TMP4, REF_4);
+
+                vis_ld64(dest[0], DST_0);
+                vis_faligndata(TMP6, TMP8, REF_S0);
+
+                vis_ld64_2(dest, 8, DST_2);
+                vis_faligndata(TMP8, TMP10, REF_S4);
+
+                if (off != 0x7) {
+                        vis_alignaddr_g0((void *)off_plus_1);
+                        vis_faligndata(TMP0, TMP2, REF_2);
+                        vis_faligndata(TMP2, TMP4, REF_6);
+                        vis_faligndata(TMP6, TMP8, REF_S2);
+                        vis_faligndata(TMP8, TMP10, REF_S6);
+                } else {
+                        vis_src1(TMP2, REF_2);
+                        vis_src1(TMP4, REF_6);
+                        vis_src1(TMP8, REF_S2);
+                        vis_src1(TMP10, REF_S6);
+                }
+
+                vis_mul8x16al(DST_0,   CONST_1024, TMP30);
+                vis_pmerge(ZERO, REF_0, TMP0);
+
+                vis_mul8x16al(DST_1,   CONST_1024, TMP32);
+                vis_pmerge(ZERO,      REF_0_1,  TMP2);
+
+                vis_mul8x16au(REF_2, CONST_256, TMP4);
+                vis_pmerge(ZERO,      REF_2_1,  TMP6);
+
+                vis_mul8x16al(DST_2,   CONST_1024, REF_0);
+                vis_padd16(TMP0, CONST_6, TMP0);
+
+                vis_mul8x16al(DST_3,   CONST_1024, REF_2);
+                vis_padd16(TMP2, CONST_6, TMP2);
+
+                vis_padd16(TMP0, TMP4, TMP0);
+                vis_mul8x16au(REF_4, CONST_256, TMP4);
+
+                vis_padd16(TMP2, TMP6, TMP2);
+                vis_mul8x16au(REF_4_1, CONST_256, TMP6);
+
+                vis_padd16(TMP12, TMP0, TMP12);
+                vis_mul8x16au(REF_6, CONST_256, TMP8);
+
+                vis_padd16(TMP14, TMP2, TMP14);
+                vis_mul8x16au(REF_6_1, CONST_256, TMP10);
+
+                vis_padd16(TMP12, TMP16, TMP12);
+                vis_mul8x16au(REF_S0, CONST_256, REF_4);
+
+                vis_padd16(TMP14, TMP18, TMP14);
+                vis_mul8x16au(REF_S0_1, CONST_256, REF_6);
+
+                vis_padd16(TMP12, TMP30, TMP12);
+
+                vis_padd16(TMP14, TMP32, TMP14);
+                vis_pack16(TMP12, DST_0);
+
+                vis_pack16(TMP14, DST_1);
+                vis_st64(DST_0, dest[0]);
+                vis_padd16(TMP4, CONST_6, TMP4);
+
+                vis_ld64_2(dest, stride, DST_0);
+                vis_padd16(TMP6, CONST_6, TMP6);
+                vis_mul8x16au(REF_S2, CONST_256, TMP12);
+
+                vis_padd16(TMP4, TMP8, TMP4);
+                vis_mul8x16au(REF_S2_1, CONST_256,  TMP14);
+
+                vis_padd16(TMP6, TMP10, TMP6);
+
+                vis_padd16(TMP20, TMP4, TMP20);
+
+                vis_padd16(TMP22, TMP6, TMP22);
+
+                vis_padd16(TMP20, TMP24, TMP20);
+
+                vis_padd16(TMP22, TMP26, TMP22);
+
+                vis_padd16(TMP20, REF_0, TMP20);
+                vis_mul8x16au(REF_S4, CONST_256, REF_0);
+
+                vis_padd16(TMP22, REF_2, TMP22);
+                vis_pack16(TMP20, DST_2);
+
+                vis_pack16(TMP22, DST_3);
+                vis_st64_2(DST_2, dest, 8);
+                dest += stride;
+
+                vis_ld64_2(dest, 8, DST_2);
+                vis_mul8x16al(DST_0,   CONST_1024, TMP30);
+                vis_pmerge(ZERO,      REF_S4_1,  REF_2);
+
+                vis_mul8x16al(DST_1,   CONST_1024, TMP32);
+                vis_padd16(REF_4, TMP0, TMP8);
+
+                vis_mul8x16au(REF_S6, CONST_256, REF_4);
+                vis_padd16(REF_6, TMP2, TMP10);
+
+                vis_mul8x16au(REF_S6_1, CONST_256, REF_6);
+                vis_padd16(TMP8, TMP12, TMP8);
+
+                vis_padd16(TMP10, TMP14, TMP10);
+
+                vis_padd16(TMP8, TMP30, TMP8);
+
+                vis_padd16(TMP10, TMP32, TMP10);
+                vis_pack16(TMP8, DST_0);
+
+                vis_pack16(TMP10, DST_1);
+                vis_st64(DST_0, dest[0]);
+
+                vis_padd16(REF_0, TMP4, REF_0);
+
+                vis_mul8x16al(DST_2,   CONST_1024, TMP30);
+                vis_padd16(REF_2, TMP6, REF_2);
+
+                vis_mul8x16al(DST_3,   CONST_1024, TMP32);
+                vis_padd16(REF_0, REF_4, REF_0);
+
+                vis_padd16(REF_2, REF_6, REF_2);
+
+                vis_padd16(REF_0, TMP30, REF_0);
+
+                /* stall */
+
+                vis_padd16(REF_2, TMP32, REF_2);
+                vis_pack16(REF_0, DST_2);
+
+                vis_pack16(REF_2, DST_3);
+                vis_st64_2(DST_2, dest, 8);
+                dest += stride;
+        } while (--height);
+}
+
+static void MC_avg_xy_8_vis (uint8_t * dest, const uint8_t * ref,
+                             const ptrdiff_t stride, int height)
+{
+        unsigned long off = (unsigned long) ref & 0x7;
+        unsigned long off_plus_1 = off + 1;
+        int stride_8 = stride + 8;
+
+        vis_set_gsr(4 << VIS_GSR_SCALEFACT_SHIFT);
+
+        ref = vis_alignaddr(ref);
+
+        vis_ld64(ref[0], TMP0);
+        vis_fzero(ZERO);
+
+        vis_ld64_2(ref, 8, TMP2);
+
+        vis_ld64(constants6[0], CONST_6);
+
+        vis_ld64(constants256_1024[0], CONST_256);
+        vis_faligndata(TMP0, TMP2, REF_S0);
+
+        if (off != 0x7) {
+                vis_alignaddr_g0((void *)off_plus_1);
+                vis_faligndata(TMP0, TMP2, REF_S2);
+        } else {
+                vis_src1(TMP2, REF_S2);
+        }
+
+        height >>= 1;
+        do {    /* 31 cycles */
+                vis_ld64_2(ref, stride, TMP0);
+                vis_mul8x16au(REF_S0, CONST_256, TMP8);
+                vis_pmerge(ZERO,      REF_S0_1,  TMP10);
+
+                vis_ld64_2(ref, stride_8, TMP2);
+                ref += stride;
+                vis_mul8x16au(REF_S2, CONST_256, TMP12);
+                vis_pmerge(ZERO,      REF_S2_1,  TMP14);
+
+                vis_alignaddr_g0((void *)off);
+
+                vis_ld64_2(ref, stride, TMP4);
+                vis_faligndata(TMP0, TMP2, REF_S4);
+
+                vis_ld64_2(ref, stride_8, TMP6);
+                ref += stride;
+
+                vis_ld64(dest[0], DST_0);
+                vis_faligndata(TMP4, TMP6, REF_S0);
+
+                vis_ld64_2(dest, stride, DST_2);
+
+                if (off != 0x7) {
+                        vis_alignaddr_g0((void *)off_plus_1);
+                        vis_faligndata(TMP0, TMP2, REF_S6);
+                        vis_faligndata(TMP4, TMP6, REF_S2);
+                } else {
+                        vis_src1(TMP2, REF_S6);
+                        vis_src1(TMP6, REF_S2);
+                }
+
+                vis_mul8x16al(DST_0,   CONST_1024, TMP30);
+                vis_pmerge(ZERO, REF_S4, TMP22);
+
+                vis_mul8x16al(DST_1,   CONST_1024, TMP32);
+                vis_pmerge(ZERO,      REF_S4_1,  TMP24);
+
+                vis_mul8x16au(REF_S6, CONST_256, TMP26);
+                vis_pmerge(ZERO,      REF_S6_1,  TMP28);
+
+                vis_mul8x16au(REF_S0, CONST_256, REF_S4);
+                vis_padd16(TMP22, CONST_6, TMP22);
+
+                vis_mul8x16au(REF_S0_1, CONST_256, REF_S6);
+                vis_padd16(TMP24, CONST_6, TMP24);
+
+                vis_mul8x16al(DST_2,   CONST_1024, REF_0);
+                vis_padd16(TMP22, TMP26, TMP22);
+
+                vis_mul8x16al(DST_3,   CONST_1024, REF_2);
+                vis_padd16(TMP24, TMP28, TMP24);
+
+                vis_mul8x16au(REF_S2, CONST_256, TMP26);
+                vis_padd16(TMP8, TMP22, TMP8);
+
+                vis_mul8x16au(REF_S2_1, CONST_256, TMP28);
+                vis_padd16(TMP10, TMP24, TMP10);
+
+                vis_padd16(TMP8, TMP12, TMP8);
+
+                vis_padd16(TMP10, TMP14, TMP10);
+
+                vis_padd16(TMP8, TMP30, TMP8);
+
+                vis_padd16(TMP10, TMP32, TMP10);
+                vis_pack16(TMP8, DST_0);
+
+                vis_pack16(TMP10, DST_1);
+                vis_st64(DST_0, dest[0]);
+                dest += stride;
+
+                vis_padd16(REF_S4, TMP22, TMP12);
+
+                vis_padd16(REF_S6, TMP24, TMP14);
+
+                vis_padd16(TMP12, TMP26, TMP12);
+
+                vis_padd16(TMP14, TMP28, TMP14);
+
+                vis_padd16(TMP12, REF_0, TMP12);
+
+                vis_padd16(TMP14, REF_2, TMP14);
+                vis_pack16(TMP12, DST_2);
+
+                vis_pack16(TMP14, DST_3);
+                vis_st64(DST_2, dest[0]);
+                dest += stride;
+        } while (--height);
+}
+
+/* End of rounding code */
+
+/* Start of no rounding code */
+/* The trick used in some of this file is the formula from the MMX
+ * motion comp code, which is:
+ *
+ * (x+y)>>1 == (x&y)+((x^y)>>1)
+ *
+ * This allows us to average 8 bytes at a time in a 64-bit FPU reg.
+ * We avoid overflows by masking before we do the shift, and we
+ * implement the shift by multiplying by 1/2 using mul8x16.  So in
+ * VIS this is (assume 'x' is in f0, 'y' is in f2, a repeating mask
+ * of '0xfe' is in f4, a repeating mask of '0x7f' is in f6, and
+ * the value 0x80808080 is in f8):
+ *
+ *      fxor            f0,   f2, f10
+ *      fand            f10,  f4, f10
+ *      fmul8x16        f8,  f10, f10
+ *      fand            f10,  f6, f10
+ *      fand            f0,   f2, f12
+ *      fpadd16         f12, f10, f10
+ */
+
+static void MC_put_no_round_o_16_vis (uint8_t * dest, const uint8_t * ref,
+                                      const ptrdiff_t stride, int height)
+{
+        ref = vis_alignaddr(ref);
+        do {    /* 5 cycles */
+                vis_ld64(ref[0], TMP0);
+
+                vis_ld64_2(ref, 8, TMP2);
+
+                vis_ld64_2(ref, 16, TMP4);
+                ref += stride;
+
+                vis_faligndata(TMP0, TMP2, REF_0);
+                vis_st64(REF_0, dest[0]);
+
+                vis_faligndata(TMP2, TMP4, REF_2);
+                vis_st64_2(REF_2, dest, 8);
+                dest += stride;
+        } while (--height);
+}
+
+static void MC_put_no_round_o_8_vis (uint8_t * dest, const uint8_t * ref,
+                                     const ptrdiff_t stride, int height)
+{
+        ref = vis_alignaddr(ref);
+        do {    /* 4 cycles */
+                vis_ld64(ref[0], TMP0);
+
+                vis_ld64(ref[8], TMP2);
+                ref += stride;
+
+                /* stall */
+
+                vis_faligndata(TMP0, TMP2, REF_0);
+                vis_st64(REF_0, dest[0]);
+                dest += stride;
+        } while (--height);
+}
+
+
+static void MC_avg_no_round_o_16_vis (uint8_t * dest, const uint8_t * ref,
+                                      const ptrdiff_t stride, int height)
+{
+        int stride_8 = stride + 8;
+
+        ref = vis_alignaddr(ref);
+
+        vis_ld64(ref[0], TMP0);
+
+        vis_ld64(ref[8], TMP2);
+
+        vis_ld64(ref[16], TMP4);
+
+        vis_ld64(dest[0], DST_0);
+
+        vis_ld64(dest[8], DST_2);
+
+        vis_ld64(constants_fe[0], MASK_fe);
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        vis_ld64(constants_7f[0], MASK_7f);
+        vis_faligndata(TMP2, TMP4, REF_2);
+
+        vis_ld64(constants128[0], CONST_128);
+
+        ref += stride;
+        height = (height >> 1) - 1;
+
+        do {    /* 24 cycles */
+                vis_ld64(ref[0], TMP0);
+                vis_xor(DST_0, REF_0, TMP6);
+
+                vis_ld64_2(ref, 8, TMP2);
+                vis_and(TMP6, MASK_fe, TMP6);
+
+                vis_ld64_2(ref, 16, TMP4);
+                ref += stride;
+                vis_mul8x16(CONST_128, TMP6, TMP6);
+                vis_xor(DST_2, REF_2, TMP8);
+
+                vis_and(TMP8, MASK_fe, TMP8);
+
+                vis_and(DST_0, REF_0, TMP10);
+                vis_ld64_2(dest, stride, DST_0);
+                vis_mul8x16(CONST_128, TMP8, TMP8);
+
+                vis_and(DST_2, REF_2, TMP12);
+                vis_ld64_2(dest, stride_8, DST_2);
+
+                vis_ld64(ref[0], TMP14);
+                vis_and(TMP6, MASK_7f, TMP6);
+
+                vis_and(TMP8, MASK_7f, TMP8);
+
+                vis_padd16(TMP10, TMP6, TMP6);
+                vis_st64(TMP6, dest[0]);
+
+                vis_padd16(TMP12, TMP8, TMP8);
+                vis_st64_2(TMP8, dest, 8);
+
+                dest += stride;
+                vis_ld64_2(ref, 8, TMP16);
+                vis_faligndata(TMP0, TMP2, REF_0);
+
+                vis_ld64_2(ref, 16, TMP18);
+                vis_faligndata(TMP2, TMP4, REF_2);
+                ref += stride;
+
+                vis_xor(DST_0, REF_0, TMP20);
+
+                vis_and(TMP20, MASK_fe, TMP20);
+
+                vis_xor(DST_2, REF_2, TMP22);
+                vis_mul8x16(CONST_128, TMP20, TMP20);
+
+                vis_and(TMP22, MASK_fe, TMP22);
+
+                vis_and(DST_0, REF_0, TMP24);
+                vis_mul8x16(CONST_128, TMP22, TMP22);
+
+                vis_and(DST_2, REF_2, TMP26);
+
+                vis_ld64_2(dest, stride, DST_0);
+                vis_faligndata(TMP14, TMP16, REF_0);
+
+                vis_ld64_2(dest, stride_8, DST_2);
+                vis_faligndata(TMP16, TMP18, REF_2);
+
+                vis_and(TMP20, MASK_7f, TMP20);
+
+                vis_and(TMP22, MASK_7f, TMP22);
+
+                vis_padd16(TMP24, TMP20, TMP20);
+                vis_st64(TMP20, dest[0]);
+
+                vis_padd16(TMP26, TMP22, TMP22);
+                vis_st64_2(TMP22, dest, 8);
+                dest += stride;
+        } while (--height);
+
+        vis_ld64(ref[0], TMP0);
+        vis_xor(DST_0, REF_0, TMP6);
+
+        vis_ld64_2(ref, 8, TMP2);
+        vis_and(TMP6, MASK_fe, TMP6);
+
+        vis_ld64_2(ref, 16, TMP4);
+        vis_mul8x16(CONST_128, TMP6, TMP6);
+        vis_xor(DST_2, REF_2, TMP8);
+
+        vis_and(TMP8, MASK_fe, TMP8);
+
+        vis_and(DST_0, REF_0, TMP10);
+        vis_ld64_2(dest, stride, DST_0);
+        vis_mul8x16(CONST_128, TMP8, TMP8);
+
+        vis_and(DST_2, REF_2, TMP12);
+        vis_ld64_2(dest, stride_8, DST_2);
+
+        vis_ld64(ref[0], TMP14);
+        vis_and(TMP6, MASK_7f, TMP6);
+
+        vis_and(TMP8, MASK_7f, TMP8);
+
+        vis_padd16(TMP10, TMP6, TMP6);
+        vis_st64(TMP6, dest[0]);
+
+        vis_padd16(TMP12, TMP8, TMP8);
+        vis_st64_2(TMP8, dest, 8);
+
+        dest += stride;
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        vis_faligndata(TMP2, TMP4, REF_2);
+
+        vis_xor(DST_0, REF_0, TMP20);
+
+        vis_and(TMP20, MASK_fe, TMP20);
+
+        vis_xor(DST_2, REF_2, TMP22);
+        vis_mul8x16(CONST_128, TMP20, TMP20);
+
+        vis_and(TMP22, MASK_fe, TMP22);
+
+        vis_and(DST_0, REF_0, TMP24);
+        vis_mul8x16(CONST_128, TMP22, TMP22);
+
+        vis_and(DST_2, REF_2, TMP26);
+
+        vis_and(TMP20, MASK_7f, TMP20);
+
+        vis_and(TMP22, MASK_7f, TMP22);
+
+        vis_padd16(TMP24, TMP20, TMP20);
+        vis_st64(TMP20, dest[0]);
+
+        vis_padd16(TMP26, TMP22, TMP22);
+        vis_st64_2(TMP22, dest, 8);
+}
+
+static void MC_put_no_round_x_16_vis (uint8_t * dest, const uint8_t * ref,
+                                      const ptrdiff_t stride, int height)
+{
+        unsigned long off = (unsigned long) ref & 0x7;
+        unsigned long off_plus_1 = off + 1;
+
+        ref = vis_alignaddr(ref);
+
+        vis_ld64(ref[0],    TMP0);
+
+        vis_ld64_2(ref, 8,  TMP2);
+
+        vis_ld64_2(ref, 16, TMP4);
+
+        vis_ld64(constants_fe[0], MASK_fe);
+
+        vis_ld64(constants_7f[0], MASK_7f);
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        vis_ld64(constants128[0], CONST_128);
+        vis_faligndata(TMP2, TMP4, REF_4);
+
+        if (off != 0x7) {
+                vis_alignaddr_g0((void *)off_plus_1);
+                vis_faligndata(TMP0, TMP2, REF_2);
+                vis_faligndata(TMP2, TMP4, REF_6);
+        } else {
+                vis_src1(TMP2, REF_2);
+                vis_src1(TMP4, REF_6);
+        }
+
+        ref += stride;
+        height = (height >> 1) - 1;
+
+        do {    /* 34 cycles */
+                vis_ld64(ref[0],    TMP0);
+                vis_xor(REF_0, REF_2, TMP6);
+
+                vis_ld64_2(ref, 8,  TMP2);
+                vis_xor(REF_4, REF_6, TMP8);
+
+                vis_ld64_2(ref, 16, TMP4);
+                vis_and(TMP6, MASK_fe, TMP6);
+                ref += stride;
+
+                vis_ld64(ref[0],    TMP14);
+                vis_mul8x16(CONST_128, TMP6, TMP6);
+                vis_and(TMP8, MASK_fe, TMP8);
+
+                vis_ld64_2(ref, 8,  TMP16);
+                vis_mul8x16(CONST_128, TMP8, TMP8);
+                vis_and(REF_0, REF_2, TMP10);
+
+                vis_ld64_2(ref, 16, TMP18);
+                ref += stride;
+                vis_and(REF_4, REF_6, TMP12);
+
+                vis_alignaddr_g0((void *)off);
+
+                vis_faligndata(TMP0, TMP2, REF_0);
+
+                vis_faligndata(TMP2, TMP4, REF_4);
+
+                if (off != 0x7) {
+                        vis_alignaddr_g0((void *)off_plus_1);
+                        vis_faligndata(TMP0, TMP2, REF_2);
+                        vis_faligndata(TMP2, TMP4, REF_6);
+                } else {
+                        vis_src1(TMP2, REF_2);
+                        vis_src1(TMP4, REF_6);
+                }
+
+                vis_and(TMP6, MASK_7f, TMP6);
+
+                vis_and(TMP8, MASK_7f, TMP8);
+
+                vis_padd16(TMP10, TMP6, TMP6);
+                vis_st64(TMP6, dest[0]);
+
+                vis_padd16(TMP12, TMP8, TMP8);
+                vis_st64_2(TMP8, dest, 8);
+                dest += stride;
+
+                vis_xor(REF_0, REF_2, TMP6);
+
+                vis_xor(REF_4, REF_6, TMP8);
+
+                vis_and(TMP6, MASK_fe, TMP6);
+
+                vis_mul8x16(CONST_128, TMP6, TMP6);
+                vis_and(TMP8, MASK_fe, TMP8);
+
+                vis_mul8x16(CONST_128, TMP8, TMP8);
+                vis_and(REF_0, REF_2, TMP10);
+
+                vis_and(REF_4, REF_6, TMP12);
+
+                vis_alignaddr_g0((void *)off);
+
+                vis_faligndata(TMP14, TMP16, REF_0);
+
+                vis_faligndata(TMP16, TMP18, REF_4);
+
+                if (off != 0x7) {
+                        vis_alignaddr_g0((void *)off_plus_1);
+                        vis_faligndata(TMP14, TMP16, REF_2);
+                        vis_faligndata(TMP16, TMP18, REF_6);
+                } else {
+                        vis_src1(TMP16, REF_2);
+                        vis_src1(TMP18, REF_6);
+                }
+
+                vis_and(TMP6, MASK_7f, TMP6);
+
+                vis_and(TMP8, MASK_7f, TMP8);
+
+                vis_padd16(TMP10, TMP6, TMP6);
+                vis_st64(TMP6, dest[0]);
+
+                vis_padd16(TMP12, TMP8, TMP8);
+                vis_st64_2(TMP8, dest, 8);
+                dest += stride;
+        } while (--height);
+
+        vis_ld64(ref[0],    TMP0);
+        vis_xor(REF_0, REF_2, TMP6);
+
+        vis_ld64_2(ref, 8,  TMP2);
+        vis_xor(REF_4, REF_6, TMP8);
+
+        vis_ld64_2(ref, 16, TMP4);
+        vis_and(TMP6, MASK_fe, TMP6);
+
+        vis_mul8x16(CONST_128, TMP6, TMP6);
+        vis_and(TMP8, MASK_fe, TMP8);
+
+        vis_mul8x16(CONST_128, TMP8, TMP8);
+        vis_and(REF_0, REF_2, TMP10);
+
+        vis_and(REF_4, REF_6, TMP12);
+
+        vis_alignaddr_g0((void *)off);
+
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        vis_faligndata(TMP2, TMP4, REF_4);
+
+        if (off != 0x7) {
+                vis_alignaddr_g0((void *)off_plus_1);
+                vis_faligndata(TMP0, TMP2, REF_2);
+                vis_faligndata(TMP2, TMP4, REF_6);
+        } else {
+                vis_src1(TMP2, REF_2);
+                vis_src1(TMP4, REF_6);
+        }
+
+        vis_and(TMP6, MASK_7f, TMP6);
+
+        vis_and(TMP8, MASK_7f, TMP8);
+
+        vis_padd16(TMP10, TMP6, TMP6);
+        vis_st64(TMP6, dest[0]);
+
+        vis_padd16(TMP12, TMP8, TMP8);
+        vis_st64_2(TMP8, dest, 8);
+        dest += stride;
+
+        vis_xor(REF_0, REF_2, TMP6);
+
+        vis_xor(REF_4, REF_6, TMP8);
+
+        vis_and(TMP6, MASK_fe, TMP6);
+
+        vis_mul8x16(CONST_128, TMP6, TMP6);
+        vis_and(TMP8, MASK_fe, TMP8);
+
+        vis_mul8x16(CONST_128, TMP8, TMP8);
+        vis_and(REF_0, REF_2, TMP10);
+
+        vis_and(REF_4, REF_6, TMP12);
+
+        vis_and(TMP6, MASK_7f, TMP6);
+
+        vis_and(TMP8, MASK_7f, TMP8);
+
+        vis_padd16(TMP10, TMP6, TMP6);
+        vis_st64(TMP6, dest[0]);
+
+        vis_padd16(TMP12, TMP8, TMP8);
+        vis_st64_2(TMP8, dest, 8);
+}
+
+static void MC_put_no_round_x_8_vis (uint8_t * dest, const uint8_t * ref,
+                                     const ptrdiff_t stride, int height)
+{
+        unsigned long off = (unsigned long) ref & 0x7;
+        unsigned long off_plus_1 = off + 1;
+
+        ref = vis_alignaddr(ref);
+
+        vis_ld64(ref[0], TMP0);
+
+        vis_ld64(ref[8], TMP2);
+
+        vis_ld64(constants_fe[0], MASK_fe);
+
+        vis_ld64(constants_7f[0], MASK_7f);
+
+        vis_ld64(constants128[0], CONST_128);
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        if (off != 0x7) {
+                vis_alignaddr_g0((void *)off_plus_1);
+                vis_faligndata(TMP0, TMP2, REF_2);
+        } else {
+                vis_src1(TMP2, REF_2);
+        }
+
+        ref += stride;
+        height = (height >> 1) - 1;
+
+        do {    /* 20 cycles */
+                vis_ld64(ref[0], TMP0);
+                vis_xor(REF_0, REF_2, TMP4);
+
+                vis_ld64_2(ref, 8, TMP2);
+                vis_and(TMP4, MASK_fe, TMP4);
+                ref += stride;
+
+                vis_ld64(ref[0], TMP8);
+                vis_and(REF_0, REF_2, TMP6);
+                vis_mul8x16(CONST_128, TMP4, TMP4);
+
+                vis_alignaddr_g0((void *)off);
+
+                vis_ld64_2(ref, 8, TMP10);
+                ref += stride;
+                vis_faligndata(TMP0, TMP2, REF_0);
+
+                if (off != 0x7) {
+                        vis_alignaddr_g0((void *)off_plus_1);
+                        vis_faligndata(TMP0, TMP2, REF_2);
+                } else {
+                        vis_src1(TMP2, REF_2);
+                }
+
+                vis_and(TMP4, MASK_7f, TMP4);
+
+                vis_padd16(TMP6, TMP4, DST_0);
+                vis_st64(DST_0, dest[0]);
+                dest += stride;
+
+                vis_xor(REF_0, REF_2, TMP12);
+
+                vis_and(TMP12, MASK_fe, TMP12);
+
+                vis_and(REF_0, REF_2, TMP14);
+                vis_mul8x16(CONST_128, TMP12, TMP12);
+
+                vis_alignaddr_g0((void *)off);
+                vis_faligndata(TMP8, TMP10, REF_0);
+                if (off != 0x7) {
+                        vis_alignaddr_g0((void *)off_plus_1);
+                        vis_faligndata(TMP8, TMP10, REF_2);
+                } else {
+                        vis_src1(TMP10, REF_2);
+                }
+
+                vis_and(TMP12, MASK_7f, TMP12);
+
+                vis_padd16(TMP14, TMP12, DST_0);
+                vis_st64(DST_0, dest[0]);
+                dest += stride;
+        } while (--height);
+
+        vis_ld64(ref[0], TMP0);
+        vis_xor(REF_0, REF_2, TMP4);
+
+        vis_ld64_2(ref, 8, TMP2);
+        vis_and(TMP4, MASK_fe, TMP4);
+
+        vis_and(REF_0, REF_2, TMP6);
+        vis_mul8x16(CONST_128, TMP4, TMP4);
+
+        vis_alignaddr_g0((void *)off);
+
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        if (off != 0x7) {
+                vis_alignaddr_g0((void *)off_plus_1);
+                vis_faligndata(TMP0, TMP2, REF_2);
+        } else {
+                vis_src1(TMP2, REF_2);
+        }
+
+        vis_and(TMP4, MASK_7f, TMP4);
+
+        vis_padd16(TMP6, TMP4, DST_0);
+        vis_st64(DST_0, dest[0]);
+        dest += stride;
+
+        vis_xor(REF_0, REF_2, TMP12);
+
+        vis_and(TMP12, MASK_fe, TMP12);
+
+        vis_and(REF_0, REF_2, TMP14);
+        vis_mul8x16(CONST_128, TMP12, TMP12);
+
+        vis_and(TMP12, MASK_7f, TMP12);
+
+        vis_padd16(TMP14, TMP12, DST_0);
+        vis_st64(DST_0, dest[0]);
+        dest += stride;
+}
+
+static void MC_avg_no_round_x_16_vis (uint8_t * dest, const uint8_t * ref,
+                                      const ptrdiff_t stride, int height)
+{
+        unsigned long off = (unsigned long) ref & 0x7;
+        unsigned long off_plus_1 = off + 1;
+
+        vis_set_gsr(5 << VIS_GSR_SCALEFACT_SHIFT);
+
+        vis_ld64(constants3[0], CONST_3);
+        vis_fzero(ZERO);
+        vis_ld64(constants256_512[0], CONST_256);
+
+        ref = vis_alignaddr(ref);
+        do {    /* 26 cycles */
+                vis_ld64(ref[0], TMP0);
+
+                vis_ld64(ref[8], TMP2);
+
+                vis_alignaddr_g0((void *)off);
+
+                vis_ld64(ref[16], TMP4);
+
+                vis_ld64(dest[0], DST_0);
+                vis_faligndata(TMP0, TMP2, REF_0);
+
+                vis_ld64(dest[8], DST_2);
+                vis_faligndata(TMP2, TMP4, REF_4);
+
+                if (off != 0x7) {
+                        vis_alignaddr_g0((void *)off_plus_1);
+                        vis_faligndata(TMP0, TMP2, REF_2);
+                        vis_faligndata(TMP2, TMP4, REF_6);
+                } else {
+                        vis_src1(TMP2, REF_2);
+                        vis_src1(TMP4, REF_6);
+                }
+
+                vis_mul8x16au(REF_0,   CONST_256, TMP0);
+
+                vis_pmerge(ZERO,     REF_2,     TMP4);
+                vis_mul8x16au(REF_0_1, CONST_256, TMP2);
+
+                vis_pmerge(ZERO, REF_2_1, TMP6);
+
+                vis_padd16(TMP0, TMP4, TMP0);
+
+                vis_mul8x16al(DST_0,   CONST_512, TMP4);
+                vis_padd16(TMP2, TMP6, TMP2);
+
+                vis_mul8x16al(DST_1,   CONST_512, TMP6);
+
+                vis_mul8x16au(REF_6,   CONST_256, TMP12);
+
+                vis_padd16(TMP0, TMP4, TMP0);
+                vis_mul8x16au(REF_6_1, CONST_256, TMP14);
+
+                vis_padd16(TMP2, TMP6, TMP2);
+                vis_mul8x16au(REF_4,   CONST_256, TMP16);
+
+                vis_padd16(TMP0, CONST_3, TMP8);
+                vis_mul8x16au(REF_4_1, CONST_256, TMP18);
+
+                vis_padd16(TMP2, CONST_3, TMP10);
+                vis_pack16(TMP8, DST_0);
+
+                vis_pack16(TMP10, DST_1);
+                vis_padd16(TMP16, TMP12, TMP0);
+
+                vis_st64(DST_0, dest[0]);
+                vis_mul8x16al(DST_2,   CONST_512, TMP4);
+                vis_padd16(TMP18, TMP14, TMP2);
+
+                vis_mul8x16al(DST_3,   CONST_512, TMP6);
+                vis_padd16(TMP0, CONST_3, TMP0);
+
+                vis_padd16(TMP2, CONST_3, TMP2);
+
+                vis_padd16(TMP0, TMP4, TMP0);
+
+                vis_padd16(TMP2, TMP6, TMP2);
+                vis_pack16(TMP0, DST_2);
+
+                vis_pack16(TMP2, DST_3);
+                vis_st64(DST_2, dest[8]);
+
+                ref += stride;
+                dest += stride;
+        } while (--height);
+}
+
+static void MC_put_no_round_y_16_vis (uint8_t * dest, const uint8_t * ref,
+                                      const ptrdiff_t stride, int height)
+{
+        ref = vis_alignaddr(ref);
+        vis_ld64(ref[0], TMP0);
+
+        vis_ld64_2(ref, 8, TMP2);
+
+        vis_ld64_2(ref, 16, TMP4);
+        ref += stride;
+
+        vis_ld64(ref[0], TMP6);
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        vis_ld64_2(ref, 8, TMP8);
+        vis_faligndata(TMP2, TMP4, REF_4);
+
+        vis_ld64_2(ref, 16, TMP10);
+        ref += stride;
+
+        vis_ld64(constants_fe[0], MASK_fe);
+        vis_faligndata(TMP6, TMP8, REF_2);
+
+        vis_ld64(constants_7f[0], MASK_7f);
+        vis_faligndata(TMP8, TMP10, REF_6);
+
+        vis_ld64(constants128[0], CONST_128);
+        height = (height >> 1) - 1;
+        do {    /* 24 cycles */
+                vis_ld64(ref[0], TMP0);
+                vis_xor(REF_0, REF_2, TMP12);
+
+                vis_ld64_2(ref, 8, TMP2);
+                vis_xor(REF_4, REF_6, TMP16);
+
+                vis_ld64_2(ref, 16, TMP4);
+                ref += stride;
+                vis_and(REF_0, REF_2, TMP14);
+
+                vis_ld64(ref[0], TMP6);
+                vis_and(REF_4, REF_6, TMP18);
+
+                vis_ld64_2(ref, 8, TMP8);
+                vis_faligndata(TMP0, TMP2, REF_0);
+
+                vis_ld64_2(ref, 16, TMP10);
+                ref += stride;
+                vis_faligndata(TMP2, TMP4, REF_4);
+
+                vis_and(TMP12, MASK_fe, TMP12);
+
+                vis_and(TMP16, MASK_fe, TMP16);
+                vis_mul8x16(CONST_128, TMP12, TMP12);
+
+                vis_mul8x16(CONST_128, TMP16, TMP16);
+                vis_xor(REF_0, REF_2, TMP0);
+
+                vis_xor(REF_4, REF_6, TMP2);
+
+                vis_and(REF_0, REF_2, TMP20);
+
+                vis_and(TMP12, MASK_7f, TMP12);
+
+                vis_and(TMP16, MASK_7f, TMP16);
+
+                vis_padd16(TMP14, TMP12, TMP12);
+                vis_st64(TMP12, dest[0]);
+
+                vis_padd16(TMP18, TMP16, TMP16);
+                vis_st64_2(TMP16, dest, 8);
+                dest += stride;
+
+                vis_and(REF_4, REF_6, TMP18);
+
+                vis_and(TMP0, MASK_fe, TMP0);
+
+                vis_and(TMP2, MASK_fe, TMP2);
+                vis_mul8x16(CONST_128, TMP0, TMP0);
+
+                vis_faligndata(TMP6, TMP8, REF_2);
+                vis_mul8x16(CONST_128, TMP2, TMP2);
+
+                vis_faligndata(TMP8, TMP10, REF_6);
+
+                vis_and(TMP0, MASK_7f, TMP0);
+
+                vis_and(TMP2, MASK_7f, TMP2);
+
+                vis_padd16(TMP20, TMP0, TMP0);
+                vis_st64(TMP0, dest[0]);
+
+                vis_padd16(TMP18, TMP2, TMP2);
+                vis_st64_2(TMP2, dest, 8);
+                dest += stride;
+        } while (--height);
+
+        vis_ld64(ref[0], TMP0);
+        vis_xor(REF_0, REF_2, TMP12);
+
+        vis_ld64_2(ref, 8, TMP2);
+        vis_xor(REF_4, REF_6, TMP16);
+
+        vis_ld64_2(ref, 16, TMP4);
+        vis_and(REF_0, REF_2, TMP14);
+
+        vis_and(REF_4, REF_6, TMP18);
+
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        vis_faligndata(TMP2, TMP4, REF_4);
+
+        vis_and(TMP12, MASK_fe, TMP12);
+
+        vis_and(TMP16, MASK_fe, TMP16);
+        vis_mul8x16(CONST_128, TMP12, TMP12);
+
+        vis_mul8x16(CONST_128, TMP16, TMP16);
+        vis_xor(REF_0, REF_2, TMP0);
+
+        vis_xor(REF_4, REF_6, TMP2);
+
+        vis_and(REF_0, REF_2, TMP20);
+
+        vis_and(TMP12, MASK_7f, TMP12);
+
+        vis_and(TMP16, MASK_7f, TMP16);
+
+        vis_padd16(TMP14, TMP12, TMP12);
+        vis_st64(TMP12, dest[0]);
+
+        vis_padd16(TMP18, TMP16, TMP16);
+        vis_st64_2(TMP16, dest, 8);
+        dest += stride;
+
+        vis_and(REF_4, REF_6, TMP18);
+
+        vis_and(TMP0, MASK_fe, TMP0);
+
+        vis_and(TMP2, MASK_fe, TMP2);
+        vis_mul8x16(CONST_128, TMP0, TMP0);
+
+        vis_mul8x16(CONST_128, TMP2, TMP2);
+
+        vis_and(TMP0, MASK_7f, TMP0);
+
+        vis_and(TMP2, MASK_7f, TMP2);
+
+        vis_padd16(TMP20, TMP0, TMP0);
+        vis_st64(TMP0, dest[0]);
+
+        vis_padd16(TMP18, TMP2, TMP2);
+        vis_st64_2(TMP2, dest, 8);
+}
+
+static void MC_put_no_round_y_8_vis (uint8_t * dest, const uint8_t * ref,
+                                     const ptrdiff_t stride, int height)
+{
+        ref = vis_alignaddr(ref);
+        vis_ld64(ref[0], TMP0);
+
+        vis_ld64_2(ref, 8, TMP2);
+        ref += stride;
+
+        vis_ld64(ref[0], TMP4);
+
+        vis_ld64_2(ref, 8, TMP6);
+        ref += stride;
+
+        vis_ld64(constants_fe[0], MASK_fe);
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        vis_ld64(constants_7f[0], MASK_7f);
+        vis_faligndata(TMP4, TMP6, REF_2);
+
+        vis_ld64(constants128[0], CONST_128);
+        height = (height >> 1) - 1;
+        do {    /* 12 cycles */
+                vis_ld64(ref[0], TMP0);
+                vis_xor(REF_0, REF_2, TMP4);
+
+                vis_ld64_2(ref, 8, TMP2);
+                ref += stride;
+                vis_and(TMP4, MASK_fe, TMP4);
+
+                vis_and(REF_0, REF_2, TMP6);
+                vis_mul8x16(CONST_128, TMP4, TMP4);
+
+                vis_faligndata(TMP0, TMP2, REF_0);
+                vis_ld64(ref[0], TMP0);
+
+                vis_ld64_2(ref, 8, TMP2);
+                ref += stride;
+                vis_xor(REF_0, REF_2, TMP12);
+
+                vis_and(TMP4, MASK_7f, TMP4);
+
+                vis_and(TMP12, MASK_fe, TMP12);
+
+                vis_mul8x16(CONST_128, TMP12, TMP12);
+                vis_and(REF_0, REF_2, TMP14);
+
+                vis_padd16(TMP6, TMP4, DST_0);
+                vis_st64(DST_0, dest[0]);
+                dest += stride;
+
+                vis_faligndata(TMP0, TMP2, REF_2);
+
+                vis_and(TMP12, MASK_7f, TMP12);
+
+                vis_padd16(TMP14, TMP12, DST_0);
+                vis_st64(DST_0, dest[0]);
+                dest += stride;
+        } while (--height);
+
+        vis_ld64(ref[0], TMP0);
+        vis_xor(REF_0, REF_2, TMP4);
+
+        vis_ld64_2(ref, 8, TMP2);
+        vis_and(TMP4, MASK_fe, TMP4);
+
+        vis_and(REF_0, REF_2, TMP6);
+        vis_mul8x16(CONST_128, TMP4, TMP4);
+
+        vis_faligndata(TMP0, TMP2, REF_0);
+
+        vis_xor(REF_0, REF_2, TMP12);
+
+        vis_and(TMP4, MASK_7f, TMP4);
+
+        vis_and(TMP12, MASK_fe, TMP12);
+
+        vis_mul8x16(CONST_128, TMP12, TMP12);
+        vis_and(REF_0, REF_2, TMP14);
+
+        vis_padd16(TMP6, TMP4, DST_0);
+        vis_st64(DST_0, dest[0]);
+        dest += stride;
+
+        vis_and(TMP12, MASK_7f, TMP12);
+
+        vis_padd16(TMP14, TMP12, DST_0);
+        vis_st64(DST_0, dest[0]);
+}
+
+static void MC_avg_no_round_y_16_vis (uint8_t * dest, const uint8_t * ref,
+                                      const ptrdiff_t stride, int height)
+{
+        int stride_8 = stride + 8;
+        int stride_16 = stride + 16;
+
+        vis_set_gsr(5 << VIS_GSR_SCALEFACT_SHIFT);
+
+        ref = vis_alignaddr(ref);
+
+        vis_ld64(ref[ 0], TMP0);
+        vis_fzero(ZERO);
+
+        vis_ld64(ref[ 8], TMP2);
+
+        vis_ld64(ref[16], TMP4);
+
+        vis_ld64(constants3[0], CONST_3);
+        vis_faligndata(TMP0, TMP2, REF_2);
+
+        vis_ld64(constants256_512[0], CONST_256);
+        vis_faligndata(TMP2, TMP4, REF_6);
+        height >>= 1;
+
+        do {    /* 31 cycles */
+                vis_ld64_2(ref, stride, TMP0);
+                vis_pmerge(ZERO,       REF_2,     TMP12);
+                vis_mul8x16au(REF_2_1, CONST_256, TMP14);
+
+                vis_ld64_2(ref, stride_8, TMP2);
+                vis_pmerge(ZERO,       REF_6,     TMP16);
+                vis_mul8x16au(REF_6_1, CONST_256, TMP18);
+
+                vis_ld64_2(ref, stride_16, TMP4);
+                ref += stride;
+
+                vis_ld64(dest[0], DST_0);
+                vis_faligndata(TMP0, TMP2, REF_0);
+
+                vis_ld64_2(dest, 8, DST_2);
+                vis_faligndata(TMP2, TMP4, REF_4);
+
+                vis_ld64_2(ref, stride, TMP6);
+                vis_pmerge(ZERO,     REF_0,     TMP0);
+                vis_mul8x16au(REF_0_1, CONST_256, TMP2);
+
+                vis_ld64_2(ref, stride_8, TMP8);
+                vis_pmerge(ZERO,     REF_4,     TMP4);
+
+                vis_ld64_2(ref, stride_16, TMP10);
+                ref += stride;
+
+                vis_ld64_2(dest, stride, REF_S0/*DST_4*/);
+                vis_faligndata(TMP6, TMP8, REF_2);
+                vis_mul8x16au(REF_4_1, CONST_256, TMP6);
+
+                vis_ld64_2(dest, stride_8, REF_S2/*DST_6*/);
+                vis_faligndata(TMP8, TMP10, REF_6);
+                vis_mul8x16al(DST_0,   CONST_512, TMP20);
+
+                vis_padd16(TMP0, CONST_3, TMP0);
+                vis_mul8x16al(DST_1,   CONST_512, TMP22);
+
+                vis_padd16(TMP2, CONST_3, TMP2);
+                vis_mul8x16al(DST_2,   CONST_512, TMP24);
+
+                vis_padd16(TMP4, CONST_3, TMP4);
+                vis_mul8x16al(DST_3,   CONST_512, TMP26);
+
+                vis_padd16(TMP6, CONST_3, TMP6);
+
+                vis_padd16(TMP12, TMP20, TMP12);
+                vis_mul8x16al(REF_S0,   CONST_512, TMP20);
+
+                vis_padd16(TMP14, TMP22, TMP14);
+                vis_mul8x16al(REF_S0_1, CONST_512, TMP22);
+
+                vis_padd16(TMP16, TMP24, TMP16);
+                vis_mul8x16al(REF_S2,   CONST_512, TMP24);
+
+                vis_padd16(TMP18, TMP26, TMP18);
+                vis_mul8x16al(REF_S2_1, CONST_512, TMP26);
+
+                vis_padd16(TMP12, TMP0, TMP12);
+                vis_mul8x16au(REF_2,   CONST_256, TMP28);
+
+                vis_padd16(TMP14, TMP2, TMP14);
+                vis_mul8x16au(REF_2_1, CONST_256, TMP30);
+
+                vis_padd16(TMP16, TMP4, TMP16);
+                vis_mul8x16au(REF_6,   CONST_256, REF_S4);
+
+                vis_padd16(TMP18, TMP6, TMP18);
+                vis_mul8x16au(REF_6_1, CONST_256, REF_S6);
+
+                vis_pack16(TMP12, DST_0);
+                vis_padd16(TMP28, TMP0, TMP12);
+
+                vis_pack16(TMP14, DST_1);
+                vis_st64(DST_0, dest[0]);
+                vis_padd16(TMP30, TMP2, TMP14);
+
+                vis_pack16(TMP16, DST_2);
+                vis_padd16(REF_S4, TMP4, TMP16);
+
+                vis_pack16(TMP18, DST_3);
+                vis_st64_2(DST_2, dest, 8);
+                dest += stride;
+                vis_padd16(REF_S6, TMP6, TMP18);
+
+                vis_padd16(TMP12, TMP20, TMP12);
+
+                vis_padd16(TMP14, TMP22, TMP14);
+                vis_pack16(TMP12, DST_0);
+
+                vis_padd16(TMP16, TMP24, TMP16);
+                vis_pack16(TMP14, DST_1);
+                vis_st64(DST_0, dest[0]);
+
+                vis_padd16(TMP18, TMP26, TMP18);
+                vis_pack16(TMP16, DST_2);
+
+                vis_pack16(TMP18, DST_3);
+                vis_st64_2(DST_2, dest, 8);
+                dest += stride;
+        } while (--height);
+}
+
+static void MC_put_no_round_xy_16_vis (uint8_t * dest, const uint8_t * ref,
+                                       const ptrdiff_t stride, int height)
+{
+        unsigned long off = (unsigned long) ref & 0x7;
+        unsigned long off_plus_1 = off + 1;
+        int stride_8 = stride + 8;
+        int stride_16 = stride + 16;
+
+        vis_set_gsr(5 << VIS_GSR_SCALEFACT_SHIFT);
+
+        ref = vis_alignaddr(ref);
+
+        vis_ld64(ref[ 0], TMP0);
+        vis_fzero(ZERO);
+
+        vis_ld64(ref[ 8], TMP2);
+
+        vis_ld64(ref[16], TMP4);
+
+        vis_ld64(constants1[0], CONST_1);
+        vis_faligndata(TMP0, TMP2, REF_S0);
+
+        vis_ld64(constants256_512[0], CONST_256);
+        vis_faligndata(TMP2, TMP4, REF_S4);
+
+        if (off != 0x7) {
+                vis_alignaddr_g0((void *)off_plus_1);
+                vis_faligndata(TMP0, TMP2, REF_S2);
+                vis_faligndata(TMP2, TMP4, REF_S6);
+        } else {
+                vis_src1(TMP2, REF_S2);
+                vis_src1(TMP4, REF_S6);
+        }
+
+        height >>= 1;
+        do {
+                vis_ld64_2(ref, stride, TMP0);
+                vis_mul8x16au(REF_S0, CONST_256, TMP12);
+                vis_pmerge(ZERO,      REF_S0_1,  TMP14);
+
+                vis_alignaddr_g0((void *)off);
+
+                vis_ld64_2(ref, stride_8, TMP2);
+                vis_mul8x16au(REF_S2, CONST_256, TMP16);
+                vis_pmerge(ZERO,      REF_S2_1,  TMP18);
+
+                vis_ld64_2(ref, stride_16, TMP4);
+                ref += stride;
+                vis_mul8x16au(REF_S4, CONST_256, TMP20);
+                vis_pmerge(ZERO,      REF_S4_1,  TMP22);
+
+                vis_ld64_2(ref, stride, TMP6);
+                vis_mul8x16au(REF_S6, CONST_256, TMP24);
+                vis_pmerge(ZERO,      REF_S6_1,  TMP26);
+
+                vis_ld64_2(ref, stride_8, TMP8);
+                vis_faligndata(TMP0, TMP2, REF_0);
+
+                vis_ld64_2(ref, stride_16, TMP10);
+                ref += stride;
+                vis_faligndata(TMP2, TMP4, REF_4);
+
+                vis_faligndata(TMP6, TMP8, REF_S0);
+
+                vis_faligndata(TMP8, TMP10, REF_S4);
+
+                if (off != 0x7) {
+                        vis_alignaddr_g0((void *)off_plus_1);
+                        vis_faligndata(TMP0, TMP2, REF_2);
+                        vis_faligndata(TMP2, TMP4, REF_6);
+                        vis_faligndata(TMP6, TMP8, REF_S2);
+                        vis_faligndata(TMP8, TMP10, REF_S6);
+                } else {
+                        vis_src1(TMP2, REF_2);
+                        vis_src1(TMP4, REF_6);
+                        vis_src1(TMP8, REF_S2);
+                        vis_src1(TMP10, REF_S6);
+                }
+
+                vis_mul8x16au(REF_0, CONST_256, TMP0);
+                vis_pmerge(ZERO,      REF_0_1,  TMP2);
+
+                vis_mul8x16au(REF_2, CONST_256, TMP4);
+                vis_pmerge(ZERO,      REF_2_1,  TMP6);
+
+                vis_padd16(TMP0, CONST_2, TMP8);
+                vis_mul8x16au(REF_4, CONST_256, TMP0);
+
+                vis_padd16(TMP2, CONST_1, TMP10);
+                vis_mul8x16au(REF_4_1, CONST_256, TMP2);
+
+                vis_padd16(TMP8, TMP4, TMP8);
+                vis_mul8x16au(REF_6, CONST_256, TMP4);
+
+                vis_padd16(TMP10, TMP6, TMP10);
+                vis_mul8x16au(REF_6_1, CONST_256, TMP6);
+
+                vis_padd16(TMP12, TMP8, TMP12);
+
+                vis_padd16(TMP14, TMP10, TMP14);
+
+                vis_padd16(TMP12, TMP16, TMP12);
+
+                vis_padd16(TMP14, TMP18, TMP14);
+                vis_pack16(TMP12, DST_0);
+
+                vis_pack16(TMP14, DST_1);
+                vis_st64(DST_0, dest[0]);
+                vis_padd16(TMP0, CONST_1, TMP12);
+
+                vis_mul8x16au(REF_S0, CONST_256, TMP0);
+                vis_padd16(TMP2, CONST_1, TMP14);
+
+                vis_mul8x16au(REF_S0_1, CONST_256, TMP2);
+                vis_padd16(TMP12, TMP4, TMP12);
+
+                vis_mul8x16au(REF_S2, CONST_256, TMP4);
+                vis_padd16(TMP14, TMP6, TMP14);
+
+                vis_mul8x16au(REF_S2_1, CONST_256, TMP6);
+                vis_padd16(TMP20, TMP12, TMP20);
+
+                vis_padd16(TMP22, TMP14, TMP22);
+
+                vis_padd16(TMP20, TMP24, TMP20);
+
+                vis_padd16(TMP22, TMP26, TMP22);
+                vis_pack16(TMP20, DST_2);
+
+                vis_pack16(TMP22, DST_3);
+                vis_st64_2(DST_2, dest, 8);
+                dest += stride;
+                vis_padd16(TMP0, TMP4, TMP24);
+
+                vis_mul8x16au(REF_S4, CONST_256, TMP0);
+                vis_padd16(TMP2, TMP6, TMP26);
+
+                vis_mul8x16au(REF_S4_1, CONST_256, TMP2);
+                vis_padd16(TMP24, TMP8, TMP24);
+
+                vis_padd16(TMP26, TMP10, TMP26);
+                vis_pack16(TMP24, DST_0);
+
+                vis_pack16(TMP26, DST_1);
+                vis_st64(DST_0, dest[0]);
+                vis_pmerge(ZERO, REF_S6, TMP4);
+
+                vis_pmerge(ZERO,      REF_S6_1,  TMP6);
+
+                vis_padd16(TMP0, TMP4, TMP0);
+
+                vis_padd16(TMP2, TMP6, TMP2);
+
+                vis_padd16(TMP0, TMP12, TMP0);
+
+                vis_padd16(TMP2, TMP14, TMP2);
+                vis_pack16(TMP0, DST_2);
+
+                vis_pack16(TMP2, DST_3);
+                vis_st64_2(DST_2, dest, 8);
+                dest += stride;
+        } while (--height);
+}
+
+static void MC_put_no_round_xy_8_vis (uint8_t * dest, const uint8_t * ref,
+                                      const ptrdiff_t stride, int height)
+{
+        unsigned long off = (unsigned long) ref & 0x7;
+        unsigned long off_plus_1 = off + 1;
+        int stride_8 = stride + 8;
+
+        vis_set_gsr(5 << VIS_GSR_SCALEFACT_SHIFT);
+
+        ref = vis_alignaddr(ref);
+
+        vis_ld64(ref[ 0], TMP0);
+        vis_fzero(ZERO);
+
+        vis_ld64(ref[ 8], TMP2);
+
+        vis_ld64(constants1[0], CONST_1);
+
+        vis_ld64(constants256_512[0], CONST_256);
+        vis_faligndata(TMP0, TMP2, REF_S0);
+
+        if (off != 0x7) {
+                vis_alignaddr_g0((void *)off_plus_1);
+                vis_faligndata(TMP0, TMP2, REF_S2);
+        } else {
+                vis_src1(TMP2, REF_S2);
+        }
+
+        height >>= 1;
+        do {    /* 26 cycles */
+                vis_ld64_2(ref, stride, TMP0);
+                vis_mul8x16au(REF_S0,   CONST_256, TMP8);
+                vis_pmerge(ZERO,        REF_S2,    TMP12);
+
+                vis_alignaddr_g0((void *)off);
+
+                vis_ld64_2(ref, stride_8, TMP2);
+                ref += stride;
+                vis_mul8x16au(REF_S0_1, CONST_256, TMP10);
+                vis_pmerge(ZERO,        REF_S2_1,  TMP14);
+
+                vis_ld64_2(ref, stride, TMP4);
+
+                vis_ld64_2(ref, stride_8, TMP6);
+                ref += stride;
+                vis_faligndata(TMP0, TMP2, REF_S4);
+
+                vis_pmerge(ZERO, REF_S4, TMP18);
+
+                vis_pmerge(ZERO, REF_S4_1, TMP20);
+
+                vis_faligndata(TMP4, TMP6, REF_S0);
+
+                if (off != 0x7) {
+                        vis_alignaddr_g0((void *)off_plus_1);
+                        vis_faligndata(TMP0, TMP2, REF_S6);
+                        vis_faligndata(TMP4, TMP6, REF_S2);
+                } else {
+                        vis_src1(TMP2, REF_S6);
+                        vis_src1(TMP6, REF_S2);
+                }
+
+                vis_padd16(TMP18, CONST_1, TMP18);
+                vis_mul8x16au(REF_S6,   CONST_256, TMP22);
+
+                vis_padd16(TMP20, CONST_1, TMP20);
+                vis_mul8x16au(REF_S6_1, CONST_256, TMP24);
+
+                vis_mul8x16au(REF_S0,   CONST_256, TMP26);
+                vis_pmerge(ZERO, REF_S0_1, TMP28);
+
+                vis_mul8x16au(REF_S2,   CONST_256, TMP30);
+                vis_padd16(TMP18, TMP22, TMP18);
+
+                vis_mul8x16au(REF_S2_1, CONST_256, TMP32);
+                vis_padd16(TMP20, TMP24, TMP20);
+
+                vis_padd16(TMP8,  TMP18, TMP8);
+
+                vis_padd16(TMP10, TMP20, TMP10);
+
+                vis_padd16(TMP8,  TMP12, TMP8);
+
+                vis_padd16(TMP10, TMP14, TMP10);
+                vis_pack16(TMP8,  DST_0);
+
+                vis_pack16(TMP10, DST_1);
+                vis_st64(DST_0, dest[0]);
+                dest += stride;
+                vis_padd16(TMP18, TMP26, TMP18);
+
+                vis_padd16(TMP20, TMP28, TMP20);
+
+                vis_padd16(TMP18, TMP30, TMP18);
+
+                vis_padd16(TMP20, TMP32, TMP20);
+                vis_pack16(TMP18, DST_2);
+
+                vis_pack16(TMP20, DST_3);
+                vis_st64(DST_2, dest[0]);
+                dest += stride;
+        } while (--height);
+}
+
+static void MC_avg_no_round_xy_16_vis (uint8_t * dest, const uint8_t * ref,
+                                       const ptrdiff_t stride, int height)
+{
+        unsigned long off = (unsigned long) ref & 0x7;
+        unsigned long off_plus_1 = off + 1;
+        int stride_8 = stride + 8;
+        int stride_16 = stride + 16;
+
+        vis_set_gsr(4 << VIS_GSR_SCALEFACT_SHIFT);
+
+        ref = vis_alignaddr(ref);
+
+        vis_ld64(ref[ 0], TMP0);
+        vis_fzero(ZERO);
+
+        vis_ld64(ref[ 8], TMP2);
+
+        vis_ld64(ref[16], TMP4);
+
+        vis_ld64(constants6[0], CONST_6);
+        vis_faligndata(TMP0, TMP2, REF_S0);
+
+        vis_ld64(constants256_1024[0], CONST_256);
+        vis_faligndata(TMP2, TMP4, REF_S4);
+
+        if (off != 0x7) {
+                vis_alignaddr_g0((void *)off_plus_1);
+                vis_faligndata(TMP0, TMP2, REF_S2);
+                vis_faligndata(TMP2, TMP4, REF_S6);
+        } else {
+                vis_src1(TMP2, REF_S2);
+                vis_src1(TMP4, REF_S6);
+        }
+
+        height >>= 1;
+        do {    /* 55 cycles */
+                vis_ld64_2(ref, stride, TMP0);
+                vis_mul8x16au(REF_S0, CONST_256, TMP12);
+                vis_pmerge(ZERO,      REF_S0_1,  TMP14);
+
+                vis_alignaddr_g0((void *)off);
+
+                vis_ld64_2(ref, stride_8, TMP2);
+                vis_mul8x16au(REF_S2, CONST_256, TMP16);
+                vis_pmerge(ZERO,      REF_S2_1,  TMP18);
+
+                vis_ld64_2(ref, stride_16, TMP4);
+                ref += stride;
+                vis_mul8x16au(REF_S4, CONST_256, TMP20);
+                vis_pmerge(ZERO,      REF_S4_1,  TMP22);
+
+                vis_ld64_2(ref, stride, TMP6);
+                vis_mul8x16au(REF_S6, CONST_256, TMP24);
+                vis_pmerge(ZERO,      REF_S6_1,  TMP26);
+
+                vis_ld64_2(ref, stride_8, TMP8);
+                vis_faligndata(TMP0, TMP2, REF_0);
+
+                vis_ld64_2(ref, stride_16, TMP10);
+                ref += stride;
+                vis_faligndata(TMP2, TMP4, REF_4);
+
+                vis_ld64(dest[0], DST_0);
+                vis_faligndata(TMP6, TMP8, REF_S0);
+
+                vis_ld64_2(dest, 8, DST_2);
+                vis_faligndata(TMP8, TMP10, REF_S4);
+
+                if (off != 0x7) {
+                        vis_alignaddr_g0((void *)off_plus_1);
+                        vis_faligndata(TMP0, TMP2, REF_2);
+                        vis_faligndata(TMP2, TMP4, REF_6);
+                        vis_faligndata(TMP6, TMP8, REF_S2);
+                        vis_faligndata(TMP8, TMP10, REF_S6);
+                } else {
+                        vis_src1(TMP2, REF_2);
+                        vis_src1(TMP4, REF_6);
+                        vis_src1(TMP8, REF_S2);
+                        vis_src1(TMP10, REF_S6);
+                }
+
+                vis_mul8x16al(DST_0,   CONST_1024, TMP30);
+                vis_pmerge(ZERO, REF_0, TMP0);
+
+                vis_mul8x16al(DST_1,   CONST_1024, TMP32);
+                vis_pmerge(ZERO,      REF_0_1,  TMP2);
+
+                vis_mul8x16au(REF_2, CONST_256, TMP4);
+                vis_pmerge(ZERO,      REF_2_1,  TMP6);
+
+                vis_mul8x16al(DST_2,   CONST_1024, REF_0);
+                vis_padd16(TMP0, CONST_6, TMP0);
+
+                vis_mul8x16al(DST_3,   CONST_1024, REF_2);
+                vis_padd16(TMP2, CONST_6, TMP2);
+
+                vis_padd16(TMP0, TMP4, TMP0);
+                vis_mul8x16au(REF_4, CONST_256, TMP4);
+
+                vis_padd16(TMP2, TMP6, TMP2);
+                vis_mul8x16au(REF_4_1, CONST_256, TMP6);
+
+                vis_padd16(TMP12, TMP0, TMP12);
+                vis_mul8x16au(REF_6, CONST_256, TMP8);
+
+                vis_padd16(TMP14, TMP2, TMP14);
+                vis_mul8x16au(REF_6_1, CONST_256, TMP10);
+
+                vis_padd16(TMP12, TMP16, TMP12);
+                vis_mul8x16au(REF_S0, CONST_256, REF_4);
+
+                vis_padd16(TMP14, TMP18, TMP14);
+                vis_mul8x16au(REF_S0_1, CONST_256, REF_6);
+
+                vis_padd16(TMP12, TMP30, TMP12);
+
+                vis_padd16(TMP14, TMP32, TMP14);
+                vis_pack16(TMP12, DST_0);
+
+                vis_pack16(TMP14, DST_1);
+                vis_st64(DST_0, dest[0]);
+                vis_padd16(TMP4, CONST_6, TMP4);
+
+                vis_ld64_2(dest, stride, DST_0);
+                vis_padd16(TMP6, CONST_6, TMP6);
+                vis_mul8x16au(REF_S2, CONST_256, TMP12);
+
+                vis_padd16(TMP4, TMP8, TMP4);
+                vis_mul8x16au(REF_S2_1, CONST_256,  TMP14);
+
+                vis_padd16(TMP6, TMP10, TMP6);
+
+                vis_padd16(TMP20, TMP4, TMP20);
+
+                vis_padd16(TMP22, TMP6, TMP22);
+
+                vis_padd16(TMP20, TMP24, TMP20);
+
+                vis_padd16(TMP22, TMP26, TMP22);
+
+                vis_padd16(TMP20, REF_0, TMP20);
+                vis_mul8x16au(REF_S4, CONST_256, REF_0);
+
+                vis_padd16(TMP22, REF_2, TMP22);
+                vis_pack16(TMP20, DST_2);
+
+                vis_pack16(TMP22, DST_3);
+                vis_st64_2(DST_2, dest, 8);
+                dest += stride;
+
+                vis_ld64_2(dest, 8, DST_2);
+                vis_mul8x16al(DST_0,   CONST_1024, TMP30);
+                vis_pmerge(ZERO,      REF_S4_1,  REF_2);
+
+                vis_mul8x16al(DST_1,   CONST_1024, TMP32);
+                vis_padd16(REF_4, TMP0, TMP8);
+
+                vis_mul8x16au(REF_S6, CONST_256, REF_4);
+                vis_padd16(REF_6, TMP2, TMP10);
+
+                vis_mul8x16au(REF_S6_1, CONST_256, REF_6);
+                vis_padd16(TMP8, TMP12, TMP8);
+
+                vis_padd16(TMP10, TMP14, TMP10);
+
+                vis_padd16(TMP8, TMP30, TMP8);
+
+                vis_padd16(TMP10, TMP32, TMP10);
+                vis_pack16(TMP8, DST_0);
+
+                vis_pack16(TMP10, DST_1);
+                vis_st64(DST_0, dest[0]);
+
+                vis_padd16(REF_0, TMP4, REF_0);
+
+                vis_mul8x16al(DST_2,   CONST_1024, TMP30);
+                vis_padd16(REF_2, TMP6, REF_2);
+
+                vis_mul8x16al(DST_3,   CONST_1024, TMP32);
+                vis_padd16(REF_0, REF_4, REF_0);
+
+                vis_padd16(REF_2, REF_6, REF_2);
+
+                vis_padd16(REF_0, TMP30, REF_0);
+
+                /* stall */
+
+                vis_padd16(REF_2, TMP32, REF_2);
+                vis_pack16(REF_0, DST_2);
+
+                vis_pack16(REF_2, DST_3);
+                vis_st64_2(DST_2, dest, 8);
+                dest += stride;
+        } while (--height);
+}
+
+/* End of no rounding code */
+
+av_cold void ff_hpeldsp_init_vis(HpelDSPContext *c, int flags)
+{
+  /* VIS-specific optimizations */
+  int accel = vis_level ();
+
+  if (accel & ACCEL_SPARC_VIS) {
+      c->put_pixels_tab[0][0] = MC_put_o_16_vis;
+      c->put_pixels_tab[0][1] = MC_put_x_16_vis;
+      c->put_pixels_tab[0][2] = MC_put_y_16_vis;
+      c->put_pixels_tab[0][3] = MC_put_xy_16_vis;
+
+      c->put_pixels_tab[1][0] = MC_put_o_8_vis;
+      c->put_pixels_tab[1][1] = MC_put_x_8_vis;
+      c->put_pixels_tab[1][2] = MC_put_y_8_vis;
+      c->put_pixels_tab[1][3] = MC_put_xy_8_vis;
+
+      c->avg_pixels_tab[0][0] = MC_avg_o_16_vis;
+      c->avg_pixels_tab[0][1] = MC_avg_x_16_vis;
+      c->avg_pixels_tab[0][2] = MC_avg_y_16_vis;
+      c->avg_pixels_tab[0][3] = MC_avg_xy_16_vis;
+
+      c->avg_pixels_tab[1][0] = MC_avg_o_8_vis;
+      c->avg_pixels_tab[1][1] = MC_avg_x_8_vis;
+      c->avg_pixels_tab[1][2] = MC_avg_y_8_vis;
+      c->avg_pixels_tab[1][3] = MC_avg_xy_8_vis;
+
+      c->put_no_rnd_pixels_tab[0][0] = MC_put_no_round_o_16_vis;
+      c->put_no_rnd_pixels_tab[0][1] = MC_put_no_round_x_16_vis;
+      c->put_no_rnd_pixels_tab[0][2] = MC_put_no_round_y_16_vis;
+      c->put_no_rnd_pixels_tab[0][3] = MC_put_no_round_xy_16_vis;
+
+      c->put_no_rnd_pixels_tab[1][0] = MC_put_no_round_o_8_vis;
+      c->put_no_rnd_pixels_tab[1][1] = MC_put_no_round_x_8_vis;
+      c->put_no_rnd_pixels_tab[1][2] = MC_put_no_round_y_8_vis;
+      c->put_no_rnd_pixels_tab[1][3] = MC_put_no_round_xy_8_vis;
+
+      c->avg_no_rnd_pixels_tab[0] = MC_avg_no_round_o_16_vis;
+      c->avg_no_rnd_pixels_tab[1] = MC_avg_no_round_x_16_vis;
+      c->avg_no_rnd_pixels_tab[2] = MC_avg_no_round_y_16_vis;
+      c->avg_no_rnd_pixels_tab[3] = MC_avg_no_round_xy_16_vis;
+  }
+}
diff --git a/libavcodec/sparc/vis.h b/libavcodec/sparc/vis.h
index adee91b..af5b5fa 100644
--- a/libavcodec/sparc/vis.h
+++ b/libavcodec/sparc/vis.h
@@ -42,6 +42,17 @@
 #ifndef AVCODEC_SPARC_VIS_H
 #define AVCODEC_SPARC_VIS_H
 
+#define ACCEL_SPARC_VIS 1
+#define ACCEL_SPARC_VIS2 2
+
+static inline int vis_level(void)
+{
+    int accel = 0;
+    accel |= ACCEL_SPARC_VIS;
+    accel |= ACCEL_SPARC_VIS2;
+    return accel;
+}
+
 #define vis_opc_base    ((0x1 << 31) | (0x36 << 19))
 #define vis_opf(X)      ((X) << 5)
 #define vis_sreg(X)     (X)
diff --git a/libavcodec/sunrast.c b/libavcodec/sunrast.c
index bfdf2b7..1e25b60 100644
--- a/libavcodec/sunrast.c
+++ b/libavcodec/sunrast.c
@@ -26,27 +26,12 @@
 #include "internal.h"
 #include "sunrast.h"
 
-typedef struct SUNRASTContext {
-    AVFrame picture;
-} SUNRASTContext;
-
-static av_cold int sunrast_init(AVCodecContext *avctx) {
-    SUNRASTContext *s = avctx->priv_data;
-
-    avcodec_get_frame_defaults(&s->picture);
-    avctx->coded_frame = &s->picture;
-
-    return 0;
-}
-
 static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
                                 int *got_frame, AVPacket *avpkt)
 {
     const uint8_t *buf       = avpkt->data;
     const uint8_t *buf_end   = avpkt->data + avpkt->size;
-    SUNRASTContext * const s = avctx->priv_data;
-    AVFrame *picture         = data;
-    AVFrame * const p        = &s->picture;
+    AVFrame * const p        = data;
     unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
     uint8_t *ptr, *ptr2 = NULL;
     const uint8_t *bufstart = buf;
@@ -69,7 +54,7 @@
     buf      += 32;
 
     if (type == RT_EXPERIMENTAL) {
-        av_log_ask_for_sample(avctx, "unsupported (compression) type\n");
+        avpriv_request_sample(avctx, "TIFF/IFF/EXPERIMENTAL (compression) type");
         return AVERROR_PATCHWELCOME;
     }
     if (type > RT_FORMAT_IFF) {
@@ -81,7 +66,7 @@
         return AVERROR_INVALIDDATA;
     }
     if (maptype == RMT_RAW) {
-        av_log_ask_for_sample(avctx, "unsupported colormap type\n");
+        avpriv_request_sample(avctx, "Unknown colormap type");
         return AVERROR_PATCHWELCOME;
     }
     if (maptype > RMT_RAW) {
@@ -115,15 +100,10 @@
             return AVERROR_INVALIDDATA;
     }
 
-    if (p->data[0])
-        avctx->release_buffer(avctx, p);
-
     if (w != avctx->width || h != avctx->height)
         avcodec_set_dimensions(avctx, w, h);
-    if ((ret = ff_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
         return ret;
-    }
 
     p->pict_type = AV_PICTURE_TYPE_I;
 
@@ -222,28 +202,15 @@
         av_freep(&ptr_free);
     }
 
-    *picture   = s->picture;
     *got_frame = 1;
 
     return buf - bufstart;
 }
 
-static av_cold int sunrast_end(AVCodecContext *avctx) {
-    SUNRASTContext *s = avctx->priv_data;
-
-    if(s->picture.data[0])
-        avctx->release_buffer(avctx, &s->picture);
-
-    return 0;
-}
-
 AVCodec ff_sunrast_decoder = {
     .name           = "sunrast",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_SUNRAST,
-    .priv_data_size = sizeof(SUNRASTContext),
-    .init           = sunrast_init,
-    .close          = sunrast_end,
     .decode         = sunrast_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
diff --git a/libavcodec/svq1dec.c b/libavcodec/svq1dec.c
index 5b9a620..5f43a97 100644
--- a/libavcodec/svq1dec.c
+++ b/libavcodec/svq1dec.c
@@ -33,8 +33,8 @@
  */
 
 #include "avcodec.h"
-#include "dsputil.h"
 #include "get_bits.h"
+#include "hpeldsp.h"
 #include "internal.h"
 #include "mathops.h"
 #include "svq1.h"
@@ -58,9 +58,9 @@
 } svq1_pmv;
 
 typedef struct SVQ1Context {
-    DSPContext dsp;
+    HpelDSPContext hdsp;
     GetBitContext gb;
-    AVFrame *cur, *prev;
+    AVFrame *prev;
     int width;
     int height;
     int frame_code;
@@ -319,9 +319,10 @@
     }
 }
 
-static int svq1_motion_inter_block(DSPContext *dsp, GetBitContext *bitbuf,
+static int svq1_motion_inter_block(HpelDSPContext *hdsp, GetBitContext *bitbuf,
                                    uint8_t *current, uint8_t *previous,
-                                   int pitch, svq1_pmv *motion, int x, int y)
+                                   int pitch, svq1_pmv *motion, int x, int y,
+                                   int width, int height)
 {
     uint8_t *src;
     uint8_t *dst;
@@ -350,22 +351,21 @@
     motion[x / 8 + 2].y =
     motion[x / 8 + 3].y = mv.y;
 
-    if (y + (mv.y >> 1) < 0)
-        mv.y = 0;
-    if (x + (mv.x >> 1) < 0)
-        mv.x = 0;
+    mv.x = av_clip(mv.x, -2 * x, 2 * (width  - x - 16));
+    mv.y = av_clip(mv.y, -2 * y, 2 * (height - y - 16));
 
     src = &previous[(x + (mv.x >> 1)) + (y + (mv.y >> 1)) * pitch];
     dst = current;
 
-    dsp->put_pixels_tab[0][(mv.y & 1) << 1 | (mv.x & 1)](dst, src, pitch, 16);
+    hdsp->put_pixels_tab[0][(mv.y & 1) << 1 | (mv.x & 1)](dst, src, pitch, 16);
 
     return 0;
 }
 
-static int svq1_motion_inter_4v_block(DSPContext *dsp, GetBitContext *bitbuf,
+static int svq1_motion_inter_4v_block(HpelDSPContext *hdsp, GetBitContext *bitbuf,
                                       uint8_t *current, uint8_t *previous,
-                                      int pitch, svq1_pmv *motion, int x, int y)
+                                      int pitch, svq1_pmv *motion, int x, int y,
+                                      int width, int height)
 {
     uint8_t *src;
     uint8_t *dst;
@@ -421,15 +421,13 @@
         int mvy = pmv[i]->y + (i >> 1) * 16;
 
         // FIXME: clipping or padding?
-        if (y + (mvy >> 1) < 0)
-            mvy = 0;
-        if (x + (mvx >> 1) < 0)
-            mvx = 0;
+        mvx = av_clip(mvx, -2 * x, 2 * (width  - x - 8));
+        mvy = av_clip(mvy, -2 * y, 2 * (height - y - 8));
 
         src = &previous[(x + (mvx >> 1)) + (y + (mvy >> 1)) * pitch];
         dst = current;
 
-        dsp->put_pixels_tab[1][((mvy & 1) << 1) | (mvx & 1)](dst, src, pitch, 8);
+        hdsp->put_pixels_tab[1][((mvy & 1) << 1) | (mvx & 1)](dst, src, pitch, 8);
 
         /* select next block */
         if (i & 1)
@@ -441,10 +439,11 @@
     return 0;
 }
 
-static int svq1_decode_delta_block(AVCodecContext *avctx, DSPContext *dsp,
+static int svq1_decode_delta_block(AVCodecContext *avctx, HpelDSPContext *hdsp,
                                    GetBitContext *bitbuf,
                                    uint8_t *current, uint8_t *previous,
-                                   int pitch, svq1_pmv *motion, int x, int y)
+                                   int pitch, svq1_pmv *motion, int x, int y,
+                                   int width, int height)
 {
     uint32_t block_type;
     int result = 0;
@@ -468,8 +467,8 @@
         break;
 
     case SVQ1_BLOCK_INTER:
-        result = svq1_motion_inter_block(dsp, bitbuf, current, previous,
-                                         pitch, motion, x, y);
+        result = svq1_motion_inter_block(hdsp, bitbuf, current, previous,
+                                         pitch, motion, x, y, width, height);
 
         if (result != 0) {
             av_dlog(avctx, "Error in svq1_motion_inter_block %i\n", result);
@@ -479,8 +478,8 @@
         break;
 
     case SVQ1_BLOCK_INTER_4V:
-        result = svq1_motion_inter_4v_block(dsp, bitbuf, current, previous,
-                                            pitch, motion, x, y);
+        result = svq1_motion_inter_4v_block(hdsp, bitbuf, current, previous,
+                                            pitch, motion, x, y, width, height);
 
         if (result != 0) {
             av_dlog(avctx, "Error in svq1_motion_inter_4v_block %i\n", result);
@@ -610,14 +609,11 @@
     const uint8_t *buf = avpkt->data;
     int buf_size       = avpkt->size;
     SVQ1Context     *s = avctx->priv_data;
-    AVFrame       *cur = s->cur;
+    AVFrame       *cur = data;
     uint8_t *current;
     int result, i, x, y, width, height;
     svq1_pmv *pmv;
 
-    if (cur->data[0])
-        avctx->release_buffer(avctx, cur);
-
     /* initialize bit buffer */
     init_get_bits(&s->gb, buf, buf_size * 8);
 
@@ -651,7 +647,7 @@
         avctx->skip_frame >= AVDISCARD_ALL)
         return buf_size;
 
-    result = ff_get_buffer(avctx, cur);
+    result = ff_get_buffer(avctx, cur, s->nonref ? 0 : AV_GET_BUFFER_FLAG_REF);
     if (result < 0)
         return result;
 
@@ -692,7 +688,8 @@
         } else {
             /* delta frame */
             uint8_t *previous = s->prev->data[i];
-            if (!previous || s->prev->width != s->cur->width || s->prev->height != s->cur->height) {
+            if (!previous ||
+                s->prev->width != s->width || s->prev->height != s->height) {
                 av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
                 result = AVERROR_INVALIDDATA;
                 goto err;
@@ -702,11 +699,11 @@
 
             for (y = 0; y < height; y += 16) {
                 for (x = 0; x < width; x += 16) {
-                    result = svq1_decode_delta_block(avctx, &s->dsp,
+                    result = svq1_decode_delta_block(avctx, &s->hdsp,
                                                      &s->gb, &current[x],
                                                      previous, linesize,
-                                                     pmv, x, y);
-                    if (result) {
+                                                     pmv, x, y, width, height);
+                    if (result != 0) {
                         av_dlog(avctx,
                                 "Error in svq1_decode_delta_block %i\n",
                                 result);
@@ -722,10 +719,12 @@
         }
     }
 
-    *(AVFrame*)data = *cur;
-    cur->qscale_table = NULL;
-    if (!s->nonref)
-        FFSWAP(AVFrame*, s->cur, s->prev);
+    if (!s->nonref) {
+        av_frame_unref(s->prev);
+        result = av_frame_ref(s->prev, cur);
+        if (result < 0)
+            goto err;
+    }
 
     *got_frame = 1;
     result     = buf_size;
@@ -741,19 +740,15 @@
     int i;
     int offset = 0;
 
-    s->cur  = avcodec_alloc_frame();
     s->prev = avcodec_alloc_frame();
-    if (!s->cur || !s->prev) {
-        avcodec_free_frame(&s->cur);
-        avcodec_free_frame(&s->prev);
+    if (!s->prev)
         return AVERROR(ENOMEM);
-    }
 
     s->width            = avctx->width  + 3 & ~3;
     s->height           = avctx->height + 3 & ~3;
     avctx->pix_fmt      = AV_PIX_FMT_YUV410P;
 
-    ff_dsputil_init(&s->dsp, avctx);
+    ff_hpeldsp_init(&s->hdsp, avctx->flags);
 
     INIT_VLC_STATIC(&svq1_block_type, 2, 4,
                     &ff_svq1_block_type_vlc[0][1], 2, 1,
@@ -798,12 +793,7 @@
 {
     SVQ1Context *s = avctx->priv_data;
 
-    if (s->cur->data[0])
-        avctx->release_buffer(avctx, s->cur);
-    if (s->prev->data[0])
-        avctx->release_buffer(avctx, s->prev);
-    avcodec_free_frame(&s->cur);
-    avcodec_free_frame(&s->prev);
+    av_frame_free(&s->prev);
 
     return 0;
 }
@@ -812,10 +802,7 @@
 {
     SVQ1Context *s = avctx->priv_data;
 
-    if (s->cur->data[0])
-        avctx->release_buffer(avctx, s->cur);
-    if (s->prev->data[0])
-        avctx->release_buffer(avctx, s->prev);
+    av_frame_unref(s->prev);
 }
 
 AVCodec ff_svq1_decoder = {
diff --git a/libavcodec/svq1enc.c b/libavcodec/svq1enc.c
index d12dfcd..afef854 100644
--- a/libavcodec/svq1enc.c
+++ b/libavcodec/svq1enc.c
@@ -27,6 +27,8 @@
  */
 
 #include "avcodec.h"
+#include "dsputil.h"
+#include "hpeldsp.h"
 #include "mpegvideo.h"
 #include "h263.h"
 #include "internal.h"
@@ -42,6 +44,7 @@
     MpegEncContext m;
     AVCodecContext *avctx;
     DSPContext dsp;
+    HpelDSPContext hdsp;
     AVFrame picture;
     AVFrame current_picture;
     AVFrame last_picture;
@@ -323,9 +326,9 @@
         s->m.current_picture.mb_mean   = (uint8_t *)s->dummy;
         s->m.current_picture.mb_var    = (uint16_t *)s->dummy;
         s->m.current_picture.mc_mb_var = (uint16_t *)s->dummy;
-        s->m.current_picture.f.mb_type = s->dummy;
+        s->m.current_picture.mb_type = s->dummy;
 
-        s->m.current_picture.f.motion_val[0] = s->motion_val8[plane] + 2;
+        s->m.current_picture.motion_val[0]   = s->motion_val8[plane] + 2;
         s->m.p_mv_table                      = s->motion_val16[plane] +
                                                s->m.mb_stride + 1;
         s->m.dsp                             = s->dsp; // move
@@ -443,10 +446,10 @@
 
                     dxy = (mx & 1) + 2 * (my & 1);
 
-                    s->dsp.put_pixels_tab[0][dxy](temp + 16,
-                                                  ref + (mx >> 1) +
-                                                  stride * (my >> 1),
-                                                  stride, 16);
+                    s->hdsp.put_pixels_tab[0][dxy](temp + 16,
+                                                   ref + (mx >> 1) +
+                                                   stride * (my >> 1),
+                                                   stride, 16);
 
                     score[1] += encode_block(s, src + 16 * x, temp + 16,
                                              decoded, stride, 5, 64, lambda, 0);
@@ -458,7 +461,7 @@
                     score[2] += vlc[1] * lambda;
                     if (score[2] < score[best] && mx == 0 && my == 0) {
                         best = 2;
-                        s->dsp.put_pixels_tab[0][0](decoded, ref, stride, 16);
+                        s->hdsp.put_pixels_tab[0][0](decoded, ref, stride, 16);
                         for (i = 0; i < 6; i++)
                             count[2][i] = 0;
                         put_bits(&s->pb, vlc[1], vlc[0]);
@@ -488,7 +491,7 @@
                 avpriv_copy_bits(&s->pb, reorder_buffer[best][i],
                                  count[best][i]);
             if (best == 0)
-                s->dsp.put_pixels_tab[0][0](decoded, temp, stride, 16);
+                s->hdsp.put_pixels_tab[0][0](decoded, temp, stride, 16);
         }
         s->m.first_slice_line = 0;
     }
@@ -500,6 +503,7 @@
     SVQ1Context *const s = avctx->priv_data;
 
     ff_dsputil_init(&s->dsp, avctx);
+    ff_hpeldsp_init(&s->hdsp, avctx->flags);
     avctx->coded_frame = &s->picture;
 
     s->frame_width  = avctx->width;
@@ -546,8 +550,8 @@
     }
 
     if (!s->current_picture.data[0]) {
-        if ((ret = ff_get_buffer(avctx, &s->current_picture))< 0 ||
-            (ret = ff_get_buffer(avctx, &s->last_picture))   < 0) {
+        if ((ret = ff_get_buffer(avctx, &s->current_picture, 0))< 0 ||
+            (ret = ff_get_buffer(avctx, &s->last_picture, 0))   < 0) {
             return ret;
         }
         s->scratchbuf = av_malloc(s->current_picture.linesize[0] * 16 * 2);
@@ -610,10 +614,9 @@
         av_freep(&s->motion_val8[i]);
         av_freep(&s->motion_val16[i]);
     }
-    if(s->current_picture.data[0])
-        avctx->release_buffer(avctx, &s->current_picture);
-    if(s->last_picture.data[0])
-        avctx->release_buffer(avctx, &s->last_picture);
+
+    av_frame_unref(&s->current_picture);
+    av_frame_unref(&s->last_picture);
 
     return 0;
 }
@@ -626,7 +629,7 @@
     .init           = svq1_encode_init,
     .encode2        = svq1_encode_frame,
     .close          = svq1_encode_end,
-    .pix_fmts       = (const enum PixelFormat[]) { AV_PIX_FMT_YUV410P,
-                                                   AV_PIX_FMT_NONE },
+    .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV410P,
+                                                     AV_PIX_FMT_NONE },
     .long_name      = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1"),
 };
diff --git a/libavcodec/svq3.c b/libavcodec/svq3.c
index 0481c80..f584f3a 100644
--- a/libavcodec/svq3.c
+++ b/libavcodec/svq3.c
@@ -48,6 +48,7 @@
 
 #include "h264_mvpred.h"
 #include "golomb.h"
+#include "hpeldsp.h"
 #include "rectangle.h"
 #include "vdpau_internal.h"
 
@@ -65,6 +66,7 @@
 
 typedef struct {
     H264Context h;
+    HpelDSPContext hdsp;
     Picture *cur_pic;
     Picture *next_pic;
     Picture *last_pic;
@@ -316,9 +318,9 @@
              : h->dsp.put_tpel_pixels_tab)[dxy](dest, src, h->linesize,
                                                 width, height);
     else
-        (avg ? h->dsp.avg_pixels_tab
-             : h->dsp.put_pixels_tab)[blocksize][dxy](dest, src, h->linesize,
-                                                      height);
+        (avg ? s->hdsp.avg_pixels_tab
+             : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src, h->linesize,
+                                                       height);
 
     if (!(h->flags & CODEC_FLAG_GRAY)) {
         mx     = mx + (mx < (int) x) >> 1;
@@ -344,10 +346,10 @@
                                                         h->uvlinesize,
                                                         width, height);
             else
-                (avg ? h->dsp.avg_pixels_tab
-                     : h->dsp.put_pixels_tab)[blocksize][dxy](dest, src,
-                                                              h->uvlinesize,
-                                                              height);
+                (avg ? s->hdsp.avg_pixels_tab
+                     : s->hdsp.put_pixels_tab)[blocksize][dxy](dest, src,
+                                                               h->uvlinesize,
+                                                               height);
         }
     }
 }
@@ -376,8 +378,8 @@
             if (mode != PREDICT_MODE) {
                 pred_motion(h, k, part_width >> 2, dir, 1, &mx, &my);
             } else {
-                mx = s->next_pic->f.motion_val[0][b_xy][0] << 1;
-                my = s->next_pic->f.motion_val[0][b_xy][1] << 1;
+                mx = s->next_pic->motion_val[0][b_xy][0] << 1;
+                my = s->next_pic->motion_val[0][b_xy][1] << 1;
 
                 if (dir == 0) {
                     mx = mx * h->frame_num_offset /
@@ -458,7 +460,7 @@
             }
 
             /* write back motion vectors */
-            fill_rectangle(h->cur_pic.f.motion_val[dir][b_xy],
+            fill_rectangle(h->cur_pic.motion_val[dir][b_xy],
                            part_width >> 2, part_height >> 2, h->b_stride,
                            pack16to32(mx, my), 4);
         }
@@ -482,7 +484,7 @@
 
     if (mb_type == 0) {           /* SKIP */
         if (h->pict_type == AV_PICTURE_TYPE_P ||
-            s->next_pic->f.mb_type[mb_xy] == -1) {
+            s->next_pic->mb_type[mb_xy] == -1) {
             svq3_mc_dir_part(s, 16 * h->mb_x, 16 * h->mb_y, 16, 16,
                              0, 0, 0, 0, 0, 0);
 
@@ -492,7 +494,7 @@
 
             mb_type = MB_TYPE_SKIP;
         } else {
-            mb_type = FFMIN(s->next_pic->f.mb_type[mb_xy], 6);
+            mb_type = FFMIN(s->next_pic->mb_type[mb_xy], 6);
             if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 0, 0) < 0)
                 return -1;
             if (svq3_mc_dir(s, mb_type, PREDICT_MODE, 1, 1) < 0)
@@ -522,21 +524,21 @@
             if (h->mb_x > 0 && h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - 1] + 6] != -1) {
                 for (i = 0; i < 4; i++)
                     AV_COPY32(h->mv_cache[m][scan8[0] - 1 + i * 8],
-                              h->cur_pic.f.motion_val[m][b_xy - 1 + i * h->b_stride]);
+                              h->cur_pic.motion_val[m][b_xy - 1 + i * h->b_stride]);
             } else {
                 for (i = 0; i < 4; i++)
                     AV_ZERO32(h->mv_cache[m][scan8[0] - 1 + i * 8]);
             }
             if (h->mb_y > 0) {
                 memcpy(h->mv_cache[m][scan8[0] - 1 * 8],
-                       h->cur_pic.f.motion_val[m][b_xy - h->b_stride],
+                       h->cur_pic.motion_val[m][b_xy - h->b_stride],
                        4 * 2 * sizeof(int16_t));
                 memset(&h->ref_cache[m][scan8[0] - 1 * 8],
                        (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1, 4);
 
                 if (h->mb_x < h->mb_width - 1) {
                     AV_COPY32(h->mv_cache[m][scan8[0] + 4 - 1 * 8],
-                              h->cur_pic.f.motion_val[m][b_xy - h->b_stride + 4]);
+                              h->cur_pic.motion_val[m][b_xy - h->b_stride + 4]);
                     h->ref_cache[m][scan8[0] + 4 - 1 * 8] =
                         (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride + 1] + 6] == -1 ||
                          h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride]] == -1) ? PART_NOT_AVAILABLE : 1;
@@ -544,7 +546,7 @@
                     h->ref_cache[m][scan8[0] + 4 - 1 * 8] = PART_NOT_AVAILABLE;
                 if (h->mb_x > 0) {
                     AV_COPY32(h->mv_cache[m][scan8[0] - 1 - 1 * 8],
-                              h->cur_pic.f.motion_val[m][b_xy - h->b_stride - 1]);
+                              h->cur_pic.motion_val[m][b_xy - h->b_stride - 1]);
                     h->ref_cache[m][scan8[0] - 1 - 1 * 8] =
                         (h->intra4x4_pred_mode[h->mb2br_xy[mb_xy - h->mb_stride - 1] + 3] == -1) ? PART_NOT_AVAILABLE : 1;
                 } else
@@ -567,7 +569,7 @@
                     return -1;
             } else {
                 for (i = 0; i < 4; i++)
-                    memset(h->cur_pic.f.motion_val[0][b_xy + i * h->b_stride],
+                    memset(h->cur_pic.motion_val[0][b_xy + i * h->b_stride],
                            0, 4 * 2 * sizeof(int16_t));
             }
             if (mb_type != 1) {
@@ -575,7 +577,7 @@
                     return -1;
             } else {
                 for (i = 0; i < 4; i++)
-                    memset(h->cur_pic.f.motion_val[1][b_xy + i * h->b_stride],
+                    memset(h->cur_pic.motion_val[1][b_xy + i * h->b_stride],
                            0, 4 * 2 * sizeof(int16_t));
             }
         }
@@ -657,11 +659,11 @@
 
     if (!IS_INTER(mb_type) && h->pict_type != AV_PICTURE_TYPE_I) {
         for (i = 0; i < 4; i++)
-            memset(h->cur_pic.f.motion_val[0][b_xy + i * h->b_stride],
+            memset(h->cur_pic.motion_val[0][b_xy + i * h->b_stride],
                    0, 4 * 2 * sizeof(int16_t));
         if (h->pict_type == AV_PICTURE_TYPE_B) {
             for (i = 0; i < 4; i++)
-                memset(h->cur_pic.f.motion_val[1][b_xy + i * h->b_stride],
+                memset(h->cur_pic.motion_val[1][b_xy + i * h->b_stride],
                        0, 4 * 2 * sizeof(int16_t));
         }
     }
@@ -747,7 +749,7 @@
     }
 
     h->cbp                              = cbp;
-    h->cur_pic.f.mb_type[mb_xy] = mb_type;
+    h->cur_pic.mb_type[mb_xy] = mb_type;
 
     if (IS_INTRA(mb_type))
         h->chroma_pred_mode = ff_h264_check_intra_pred_mode(h, DC_PRED8x8, 1);
@@ -790,8 +792,8 @@
                     header ^ s->watermark_key);
         }
         if (length > 0) {
-            memcpy((uint8_t *) &h->gb.buffer[get_bits_count(&h->gb) >> 3],
-                   &h->gb.buffer[h->gb.size_in_bits >> 3], length - 1);
+            memmove((uint8_t *) &h->gb.buffer[get_bits_count(&h->gb) >> 3],
+                    &h->gb.buffer[h->gb.size_in_bits >> 3], length - 1);
         }
         skip_bits_long(&h->gb, 0);
     }
@@ -869,6 +871,7 @@
     if (ff_h264_decode_init(avctx) < 0)
         return -1;
 
+    ff_hpeldsp_init(&s->hdsp, avctx->flags);
     h->flags           = avctx->flags;
     h->is_complex      = 1;
     h->sps.chroma_format_idc = 1;
@@ -1021,6 +1024,18 @@
     return 0;
 }
 
+static void free_picture(AVCodecContext *avctx, Picture *pic)
+{
+    int i;
+    for (i = 0; i < 2; i++) {
+        av_buffer_unref(&pic->motion_val_buf[i]);
+        av_buffer_unref(&pic->ref_index_buf[i]);
+    }
+    av_buffer_unref(&pic->mb_type_buf);
+
+    av_frame_unref(&pic->f);
+}
+
 static int get_buffer(AVCodecContext *avctx, Picture *pic)
 {
     SVQ3Context *s = avctx->priv_data;
@@ -1031,27 +1046,33 @@
     const int b4_array_size = b4_stride * h->mb_height * 4;
     int ret;
 
-    if (!pic->motion_val_base[0]) {
+    if (!pic->motion_val_buf[0]) {
         int i;
 
-        pic->mb_type_base = av_mallocz((big_mb_num + h->mb_stride) * sizeof(uint32_t));
-        if (!pic->mb_type_base)
+        pic->mb_type_buf = av_buffer_allocz((big_mb_num + h->mb_stride) * sizeof(uint32_t));
+        if (!pic->mb_type_buf)
             return AVERROR(ENOMEM);
-        pic->f.mb_type = pic->mb_type_base + 2 * h->mb_stride + 1;
+        pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * h->mb_stride + 1;
 
         for (i = 0; i < 2; i++) {
-            pic->motion_val_base[i] = av_mallocz(2 * (b4_array_size + 4) * sizeof(int16_t));
-            pic->f.ref_index[i]     = av_mallocz(4 * mb_array_size);
-            if (!pic->motion_val_base[i] || !pic->f.ref_index[i])
-                return AVERROR(ENOMEM);
+            pic->motion_val_buf[i] = av_buffer_allocz(2 * (b4_array_size + 4) * sizeof(int16_t));
+            pic->ref_index_buf[i]  = av_buffer_allocz(4 * mb_array_size);
+            if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i]) {
+                ret = AVERROR(ENOMEM);
+                goto fail;
+            }
 
-            pic->f.motion_val[i] = pic->motion_val_base[i] + 4;
+            pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
+            pic->ref_index[i]  = pic->ref_index_buf[i]->data;
         }
     }
-    pic->f.motion_subsample_log2 = 2;
-    pic->f.reference = !(h->pict_type == AV_PICTURE_TYPE_B);
+    pic->reference = !(h->pict_type == AV_PICTURE_TYPE_B);
 
-    ret = ff_get_buffer(avctx, &pic->f);
+    ret = ff_get_buffer(avctx, &pic->f,
+                        pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
+    if (ret < 0)
+        goto fail;
+
     if (!h->edge_emu_buffer) {
         h->edge_emu_buffer = av_mallocz(pic->f.linesize[0] * 17);
         if (!h->edge_emu_buffer)
@@ -1061,6 +1082,9 @@
     h->linesize   = pic->f.linesize[0];
     h->uvlinesize = pic->f.linesize[1];
 
+    return 0;
+fail:
+    free_picture(avctx, pic);
     return ret;
 }
 
@@ -1077,7 +1101,9 @@
     /* special case for last picture */
     if (buf_size == 0) {
         if (s->next_pic->f.data[0] && !h->low_delay && !s->last_frame_output) {
-            *(AVFrame *) data   = s->next_pic->f;
+            ret = av_frame_ref(data, &s->next_pic->f);
+            if (ret < 0)
+                return ret;
             s->last_frame_output = 1;
             *got_frame          = 1;
         }
@@ -1107,8 +1133,7 @@
     if (h->pict_type != AV_PICTURE_TYPE_B)
         FFSWAP(Picture*, s->next_pic, s->last_pic);
 
-    if (s->cur_pic->f.data[0])
-        avctx->release_buffer(avctx, &s->cur_pic->f);
+    av_frame_unref(&s->cur_pic->f);
 
     /* for skipping the frame */
     s->cur_pic->f.pict_type = h->pict_type;
@@ -1119,7 +1144,11 @@
         return ret;
 
     h->cur_pic_ptr = s->cur_pic;
+    av_frame_unref(&h->cur_pic.f);
     h->cur_pic     = *s->cur_pic;
+    ret = av_frame_ref(&h->cur_pic.f, &s->cur_pic->f);
+    if (ret < 0)
+        return ret;
 
     for (i = 0; i < 16; i++) {
         h->block_offset[i]           = (4 * ((scan8[i] - scan8[0]) & 7)) + 4 * h->linesize * ((scan8[i] - scan8[0]) >> 3);
@@ -1240,7 +1269,7 @@
                 ff_h264_hl_decode_mb(h);
 
             if (h->pict_type != AV_PICTURE_TYPE_B && !h->low_delay)
-                h->cur_pic.f.mb_type[h->mb_x + h->mb_y * h->mb_stride] =
+                h->cur_pic.mb_type[h->mb_x + h->mb_y * h->mb_stride] =
                     (h->pict_type == AV_PICTURE_TYPE_P && mb_type < 8) ? (mb_type - 1) : -1;
         }
 
@@ -1262,9 +1291,11 @@
     }
 
     if (h->pict_type == AV_PICTURE_TYPE_B || h->low_delay)
-        *(AVFrame *)data = s->cur_pic->f;
-    else
-        *(AVFrame *)data = s->last_pic->f;
+        ret = av_frame_ref(data, &s->cur_pic->f);
+    else if (s->last_pic->f.data[0])
+        ret = av_frame_ref(data, &s->last_pic->f);
+    if (ret < 0)
+        return ret;
 
     /* Do not output the last pic after seeking. */
     if (s->last_pic->f.data[0] || h->low_delay)
@@ -1272,25 +1303,13 @@
 
     if (h->pict_type != AV_PICTURE_TYPE_B) {
         FFSWAP(Picture*, s->cur_pic, s->next_pic);
+    } else {
+        av_frame_unref(&s->cur_pic->f);
     }
 
     return buf_size;
 }
 
-static void free_picture(AVCodecContext *avctx, Picture *pic)
-{
-    int i;
-    for (i = 0; i < 2; i++) {
-        av_freep(&pic->motion_val_base[i]);
-        av_freep(&pic->f.ref_index[i]);
-    }
-    av_freep(&pic->mb_type_base);
-
-    if (pic->f.data[0])
-        avctx->release_buffer(avctx, &pic->f);
-    av_freep(&pic);
-}
-
 static int svq3_decode_end(AVCodecContext *avctx)
 {
     SVQ3Context *s = avctx->priv_data;
@@ -1299,6 +1318,11 @@
     free_picture(avctx, s->cur_pic);
     free_picture(avctx, s->next_pic);
     free_picture(avctx, s->last_pic);
+    av_freep(&s->cur_pic);
+    av_freep(&s->next_pic);
+    av_freep(&s->last_pic);
+
+    av_frame_unref(&h->cur_pic.f);
 
     ff_h264_free_context(h);
 
diff --git a/libavcodec/takdec.c b/libavcodec/takdec.c
index ae751fe..43382f1 100644
--- a/libavcodec/takdec.c
+++ b/libavcodec/takdec.c
@@ -749,7 +749,7 @@
                                              : s->ti.frame_samples;
 
     frame->nb_samples = s->nb_samples;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0)
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
 
     if (avctx->bits_per_raw_sample <= 16) {
diff --git a/libavcodec/targa.c b/libavcodec/targa.c
index 2d1d3df..ff4390f 100644
--- a/libavcodec/targa.c
+++ b/libavcodec/targa.c
@@ -27,7 +27,6 @@
 #include "targa.h"
 
 typedef struct TargaContext {
-    AVFrame picture;
     GetByteContext gb;
 } TargaContext;
 
@@ -112,8 +111,7 @@
                         AVPacket *avpkt)
 {
     TargaContext * const s = avctx->priv_data;
-    AVFrame *picture = data;
-    AVFrame * const p = &s->picture;
+    AVFrame * const p = data;
     uint8_t *dst;
     int stride;
     int idlen, pal, compr, y, w, h, bpp, flags, ret;
@@ -170,9 +168,6 @@
         return AVERROR_INVALIDDATA;
     }
 
-    if (s->picture.data[0])
-        avctx->release_buffer(avctx, &s->picture);
-
     if (colors && (colors + first_clr) > 256) {
         av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
         return AVERROR_INVALIDDATA;
@@ -182,10 +177,8 @@
         return ret;
     if (w != avctx->width || h != avctx->height)
         avcodec_set_dimensions(avctx, w, h);
-    if ((ret = ff_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
         return ret;
-    }
 
     if (flags & TGA_TOPTOBOTTOM) {
         dst = p->data[0];
@@ -298,39 +291,16 @@
         }
     }
 
-    *picture   = s->picture;
     *got_frame = 1;
 
     return avpkt->size;
 }
 
-static av_cold int targa_init(AVCodecContext *avctx)
-{
-    TargaContext *s = avctx->priv_data;
-
-    avcodec_get_frame_defaults(&s->picture);
-    avctx->coded_frame = &s->picture;
-
-    return 0;
-}
-
-static av_cold int targa_end(AVCodecContext *avctx)
-{
-    TargaContext *s = avctx->priv_data;
-
-    if (s->picture.data[0])
-        avctx->release_buffer(avctx, &s->picture);
-
-    return 0;
-}
-
 AVCodec ff_targa_decoder = {
     .name           = "targa",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_TARGA,
     .priv_data_size = sizeof(TargaContext),
-    .init           = targa_init,
-    .close          = targa_end,
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
diff --git a/libavcodec/targa_y216dec.c b/libavcodec/targa_y216dec.c
index 3b97000..38694ce 100644
--- a/libavcodec/targa_y216dec.c
+++ b/libavcodec/targa_y216dec.c
@@ -27,38 +27,24 @@
     avctx->pix_fmt             = AV_PIX_FMT_YUV422P16;
     avctx->bits_per_raw_sample = 14;
 
-    avctx->coded_frame = avcodec_alloc_frame();
-
-    if (!avctx->coded_frame) {
-        av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
-        return AVERROR(ENOMEM);
-    }
-
     return 0;
 }
 
 static int y216_decode_frame(AVCodecContext *avctx, void *data,
                              int *got_frame, AVPacket *avpkt)
 {
-    AVFrame *pic = avctx->coded_frame;
+    AVFrame *pic = data;
     const uint16_t *src = (uint16_t *)avpkt->data;
     uint16_t *y, *u, *v, aligned_width = FFALIGN(avctx->width, 4);
-    int i, j;
-
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
+    int i, j, ret;
 
     if (avpkt->size < 4 * avctx->height * aligned_width) {
         av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
         return AVERROR(EINVAL);
     }
 
-    pic->reference = 0;
-
-    if (ff_get_buffer(avctx, pic) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
-        return AVERROR(ENOMEM);
-    }
+    if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
+        return ret;
 
     pic->key_frame = 1;
     pic->pict_type = AV_PICTURE_TYPE_I;
@@ -82,17 +68,12 @@
     }
 
     *got_frame = 1;
-    *(AVFrame *)data = *pic;
 
     return avpkt->size;
 }
 
 static av_cold int y216_decode_close(AVCodecContext *avctx)
 {
-    if (avctx->coded_frame->data[0])
-        avctx->release_buffer(avctx, avctx->coded_frame);
-
-    av_freep(&avctx->coded_frame);
 
     return 0;
 }
diff --git a/libavcodec/thread.h b/libavcodec/thread.h
index e838030..0dc04e0 100644
--- a/libavcodec/thread.h
+++ b/libavcodec/thread.h
@@ -27,9 +27,19 @@
 #ifndef AVCODEC_THREAD_H
 #define AVCODEC_THREAD_H
 
+#include "libavutil/buffer.h"
+
 #include "config.h"
 #include "avcodec.h"
 
+typedef struct ThreadFrame {
+    AVFrame *f;
+    AVCodecContext *owner;
+    // progress->data is an array of 2 ints holding progress for top/bottom
+    // fields
+    AVBufferRef *progress;
+} ThreadFrame;
+
 /**
  * Wait for decoding threads to finish and reset internal state.
  * Called by avcodec_flush_buffers().
@@ -71,7 +81,7 @@
  * @param field The field being decoded, for field-picture codecs.
  * 0 for top field or frame pictures, 1 for bottom field.
  */
-void ff_thread_report_progress(AVFrame *f, int progress, int field);
+void ff_thread_report_progress(ThreadFrame *f, int progress, int field);
 
 /**
  * Wait for earlier decoding threads to finish reference pictures.
@@ -85,7 +95,17 @@
  * @param field The field being referenced, for field-picture codecs.
  * 0 for top field or frame pictures, 1 for bottom field.
  */
-void ff_thread_await_progress(AVFrame *f, int progress, int field);
+void ff_thread_await_progress(ThreadFrame *f, int progress, int field);
+
+/**
+ * Wrapper around get_format() for frame-multithreaded codecs.
+ * Call this function instead of avctx->get_format().
+ * Cannot be called after the codec has called ff_thread_finish_setup().
+ *
+ * @param avctx The current context.
+ * @param fmt The list of available formats.
+ */
+enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt);
 
 /**
  * Wrapper around get_buffer() for frame-multithreaded codecs.
@@ -95,7 +115,7 @@
  * @param avctx The current context.
  * @param f The frame to write into.
  */
-int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f);
+int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags);
 
 /**
  * Wrapper around release_buffer() frame-for multithreaded codecs.
@@ -108,7 +128,9 @@
  * @param avctx The current context.
  * @param f The picture being released.
  */
-void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f);
+void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f);
+
+int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src);
 
 int ff_thread_init(AVCodecContext *s);
 void ff_thread_free(AVCodecContext *s);
diff --git a/libavcodec/tiertexseqv.c b/libavcodec/tiertexseqv.c
index 3ae7c88..f892e4d 100644
--- a/libavcodec/tiertexseqv.c
+++ b/libavcodec/tiertexseqv.c
@@ -27,6 +27,7 @@
 #include "avcodec.h"
 #define BITSTREAM_READER_LE
 #include "get_bits.h"
+#include "internal.h"
 
 
 typedef struct SeqVideoContext {
@@ -217,7 +218,6 @@
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
 
     avcodec_get_frame_defaults(&seq->frame);
-    seq->frame.data[0] = NULL;
 
     return 0;
 }
@@ -228,21 +228,19 @@
 {
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
+    int ret;
 
     SeqVideoContext *seq = avctx->priv_data;
 
-    seq->frame.reference = 3;
-    seq->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
-    if (avctx->reget_buffer(avctx, &seq->frame)) {
-        av_log(seq->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
-        return -1;
-    }
+    if ((ret = ff_reget_buffer(avctx, &seq->frame)) < 0)
+        return ret;
 
     if (seqvideo_decode(seq, buf, buf_size))
         return AVERROR_INVALIDDATA;
 
+    if ((ret = av_frame_ref(data, &seq->frame)) < 0)
+        return ret;
     *got_frame       = 1;
-    *(AVFrame *)data = seq->frame;
 
     return buf_size;
 }
@@ -251,8 +249,7 @@
 {
     SeqVideoContext *seq = avctx->priv_data;
 
-    if (seq->frame.data[0])
-        avctx->release_buffer(avctx, &seq->frame);
+    av_frame_unref(&seq->frame);
 
     return 0;
 }
diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index 77706c0..6c2dc23 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -43,7 +43,6 @@
 
 typedef struct TiffContext {
     AVCodecContext *avctx;
-    AVFrame picture;
     GetByteContext gb;
 
     int width, height;
@@ -268,7 +267,7 @@
 
 static int add_doubles_metadata(int count,
                                 const char *name, const char *sep,
-                                TiffContext *s)
+                                TiffContext *s, AVFrame *frame)
 {
     char *ap;
     int i;
@@ -289,12 +288,12 @@
     av_freep(&dp);
     if (!ap)
         return AVERROR(ENOMEM);
-    av_dict_set(&s->picture.metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
+    av_dict_set(avpriv_frame_get_metadatap(frame), name, ap, AV_DICT_DONT_STRDUP_VAL);
     return 0;
 }
 
 static int add_shorts_metadata(int count, const char *name,
-                               const char *sep, TiffContext *s)
+                               const char *sep, TiffContext *s, AVFrame *frame)
 {
     char *ap;
     int i;
@@ -315,12 +314,12 @@
     av_freep(&sp);
     if (!ap)
         return AVERROR(ENOMEM);
-    av_dict_set(&s->picture.metadata, name, ap, AV_DICT_DONT_STRDUP_VAL);
+    av_dict_set(avpriv_frame_get_metadatap(frame), name, ap, AV_DICT_DONT_STRDUP_VAL);
     return 0;
 }
 
 static int add_string_metadata(int count, const char *name,
-                               TiffContext *s)
+                               TiffContext *s, AVFrame *frame)
 {
     char *value;
 
@@ -334,17 +333,17 @@
     bytestream2_get_bufferu(&s->gb, value, count);
     value[count] = 0;
 
-    av_dict_set(&s->picture.metadata, name, value, AV_DICT_DONT_STRDUP_VAL);
+    av_dict_set(avpriv_frame_get_metadatap(frame), name, value, AV_DICT_DONT_STRDUP_VAL);
     return 0;
 }
 
 static int add_metadata(int count, int type,
-                        const char *name, const char *sep, TiffContext *s)
+                        const char *name, const char *sep, TiffContext *s, AVFrame *frame)
 {
     switch(type) {
-    case TIFF_DOUBLE: return add_doubles_metadata(count, name, sep, s);
-    case TIFF_SHORT : return add_shorts_metadata(count, name, sep, s);
-    case TIFF_STRING: return add_string_metadata(count, name, s);
+    case TIFF_DOUBLE: return add_doubles_metadata(count, name, sep, s, frame);
+    case TIFF_SHORT : return add_shorts_metadata(count, name, sep, s, frame);
+    case TIFF_STRING: return add_string_metadata(count, name, s, frame);
     default         : return AVERROR_INVALIDDATA;
     };
 }
@@ -597,7 +596,7 @@
     return 0;
 }
 
-static int init_image(TiffContext *s)
+static int init_image(TiffContext *s, AVFrame *frame)
 {
     int i, ret;
     uint32_t *pal;
@@ -642,18 +641,14 @@
             return ret;
         avcodec_set_dimensions(s->avctx, s->width, s->height);
     }
-    if (s->picture.data[0])
-        s->avctx->release_buffer(s->avctx, &s->picture);
-    if ((ret = ff_get_buffer(s->avctx, &s->picture)) < 0) {
-        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(s->avctx, frame, 0)) < 0)
         return ret;
-    }
     if (s->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
         if (s->palette_is_set) {
-            memcpy(s->picture.data[1], s->palette, sizeof(s->palette));
+            memcpy(frame->data[1], s->palette, sizeof(s->palette));
         } else {
             /* make default grayscale pal */
-            pal = (uint32_t *) s->picture.data[1];
+            pal = (uint32_t *) frame->data[1];
             for (i = 0; i < 1<<s->bpp; i++)
                 pal[i] = 0xFFU << 24 | i * 255 / ((1<<s->bpp) - 1) * 0x010101;
         }
@@ -661,7 +656,7 @@
     return 0;
 }
 
-static int tiff_decode_tag(TiffContext *s)
+static int tiff_decode_tag(TiffContext *s, AVFrame *frame)
 {
     unsigned tag, type, count, off, value = 0;
     int i, j, k, pos, start;
@@ -899,7 +894,7 @@
             s->fax_opts = value;
         break;
 #define ADD_METADATA(count, name, sep)\
-    if ((ret = add_metadata(count, type, name, sep, s)) < 0) {\
+    if ((ret = add_metadata(count, type, name, sep, s, frame)) < 0) {\
         av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");\
         return ret;\
     }
@@ -1041,8 +1036,7 @@
                         void *data, int *got_frame, AVPacket *avpkt)
 {
     TiffContext *const s = avctx->priv_data;
-    AVFrame *picture = data;
-    AVFrame *const p = &s->picture;
+    AVFrame *const p = data;
     unsigned off;
     int id, le, ret;
     int i, j, entries;
@@ -1073,9 +1067,6 @@
     s->compr = TIFF_RAW;
     s->fill_order = 0;
     free_geotags(s);
-    /* metadata has been destroyed from lavc internals, that pointer is not
-     * valid anymore */
-    s->picture.metadata = NULL;
 
     // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number
     // that further identifies the file as a TIFF file"
@@ -1097,7 +1088,7 @@
     if (bytestream2_get_bytes_left(&s->gb) < entries * 12)
         return AVERROR_INVALIDDATA;
     for (i = 0; i < entries; i++) {
-        if ((ret = tiff_decode_tag(s)) < 0)
+        if ((ret = tiff_decode_tag(s, p)) < 0)
             return ret;
     }
 
@@ -1111,7 +1102,7 @@
             av_log(avctx, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[i].key);
             continue;
         }
-        ret = av_dict_set(&s->picture.metadata, keyname, s->geotags[i].val, 0);
+        ret = av_dict_set(avpriv_frame_get_metadatap(p), keyname, s->geotags[i].val, 0);
         if (ret<0) {
             av_log(avctx, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname);
             return ret;
@@ -1123,7 +1114,7 @@
         return AVERROR_INVALIDDATA;
     }
     /* now we have the data and may start decoding */
-    if ((ret = init_image(s)) < 0)
+    if ((ret = init_image(s, p)) < 0)
         return ret;
 
     if (s->strips == 1 && !s->stripsize) {
@@ -1197,14 +1188,13 @@
     }
 
     if (s->invert) {
-        dst = s->picture.data[0];
+        dst = p->data[0];
         for (i = 0; i < s->height; i++) {
-            for (j = 0; j < s->picture.linesize[0]; j++)
+            for (j = 0; j < p->linesize[0]; j++)
                 dst[j] = (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255) - dst[j];
-            dst += s->picture.linesize[0];
+            dst += p->linesize[0];
         }
     }
-    *picture   = s->picture;
     *got_frame = 1;
 
     return avpkt->size;
@@ -1217,8 +1207,6 @@
     s->width = 0;
     s->height = 0;
     s->avctx = avctx;
-    avcodec_get_frame_defaults(&s->picture);
-    avctx->coded_frame = &s->picture;
     ff_lzw_decode_open(&s->lzw);
     ff_ccitt_unpack_init();
 
@@ -1233,8 +1221,6 @@
 
     ff_lzw_decode_close(&s->lzw);
     av_freep(&s->deinvert_buf);
-    if (s->picture.data[0])
-        avctx->release_buffer(avctx, &s->picture);
     return 0;
 }
 
diff --git a/libavcodec/tmv.c b/libavcodec/tmv.c
index f8d7969..279298c 100644
--- a/libavcodec/tmv.c
+++ b/libavcodec/tmv.c
@@ -35,14 +35,10 @@
 
 #include "cga_data.h"
 
-typedef struct TMVContext {
-    AVFrame pic;
-} TMVContext;
-
 static int tmv_decode_frame(AVCodecContext *avctx, void *data,
                             int *got_frame, AVPacket *avpkt)
 {
-    TMVContext *tmv    = avctx->priv_data;
+    AVFrame *frame     = data;
     const uint8_t *src = avpkt->data;
     uint8_t *dst;
     unsigned char_cols = avctx->width >> 3;
@@ -50,13 +46,8 @@
     unsigned x, y, fg, bg, c;
     int ret;
 
-    if (tmv->pic.data[0])
-        avctx->release_buffer(avctx, &tmv->pic);
-
-    if ((ret = ff_get_buffer(avctx, &tmv->pic)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     if (avpkt->size < 2*char_rows*char_cols) {
         av_log(avctx, AV_LOG_ERROR,
@@ -65,45 +56,33 @@
         return AVERROR_INVALIDDATA;
     }
 
-    tmv->pic.pict_type = AV_PICTURE_TYPE_I;
-    tmv->pic.key_frame = 1;
-    dst                = tmv->pic.data[0];
+    frame->pict_type = AV_PICTURE_TYPE_I;
+    frame->key_frame = 1;
+    dst              = frame->data[0];
 
-    tmv->pic.palette_has_changed = 1;
-    memcpy(tmv->pic.data[1], ff_cga_palette, 16 * 4);
-    memset(tmv->pic.data[1] + 16 * 4, 0, AVPALETTE_SIZE - 16 * 4);
+    frame->palette_has_changed = 1;
+    memcpy(frame->data[1], ff_cga_palette, 16 * 4);
+    memset(frame->data[1] + 16 * 4, 0, AVPALETTE_SIZE - 16 * 4);
 
     for (y = 0; y < char_rows; y++) {
         for (x = 0; x < char_cols; x++) {
             c  = *src++;
             bg = *src  >> 4;
             fg = *src++ & 0xF;
-            ff_draw_pc_font(dst + x * 8, tmv->pic.linesize[0],
+            ff_draw_pc_font(dst + x * 8, frame->linesize[0],
                             avpriv_cga_font, 8, c, fg, bg);
         }
-        dst += tmv->pic.linesize[0] * 8;
+        dst += frame->linesize[0] * 8;
     }
 
     *got_frame = 1;
-    *(AVFrame *)data = tmv->pic;
+
     return avpkt->size;
 }
 
 static av_cold int tmv_decode_init(AVCodecContext *avctx)
 {
-    TMVContext *tmv = avctx->priv_data;
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
-    avcodec_get_frame_defaults(&tmv->pic);
-    return 0;
-}
-
-static av_cold int tmv_decode_close(AVCodecContext *avctx)
-{
-    TMVContext *tmv = avctx->priv_data;
-
-    if (tmv->pic.data[0])
-        avctx->release_buffer(avctx, &tmv->pic);
-
     return 0;
 }
 
@@ -111,9 +90,7 @@
     .name           = "tmv",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_TMV,
-    .priv_data_size = sizeof(TMVContext),
     .init           = tmv_decode_init,
-    .close          = tmv_decode_close,
     .decode         = tmv_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("8088flex TMV"),
diff --git a/libavcodec/truemotion1.c b/libavcodec/truemotion1.c
index ed6b5d9..5a387ca 100644
--- a/libavcodec/truemotion1.c
+++ b/libavcodec/truemotion1.c
@@ -34,6 +34,7 @@
 #include <string.h>
 
 #include "avcodec.h"
+#include "internal.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
 #include "libavutil/intreadwrite.h"
@@ -354,7 +355,7 @@
         s->flags = FLAG_KEYFRAME;
 
     if (s->flags & FLAG_SPRITE) {
-        av_log_ask_for_sample(s->avctx, "SPRITE frame found.\n");
+        avpriv_request_sample(s->avctx, "Frame with sprite");
         /* FIXME header.width, height, xoffset and yoffset aren't initialized */
         return AVERROR_PATCHWELCOME;
     } else {
@@ -364,7 +365,7 @@
             if ((s->w < 213) && (s->h >= 176))
             {
                 s->flags |= FLAG_INTERPOLATED;
-                av_log_ask_for_sample(s->avctx, "INTERPOLATION selected.\n");
+                avpriv_request_sample(s->avctx, "Interpolated frame");
             }
         }
     }
@@ -401,8 +402,7 @@
 
     if (s->w != s->avctx->width || s->h != s->avctx->height ||
         new_pix_fmt != s->avctx->pix_fmt) {
-        if (s->frame.data[0])
-            s->avctx->release_buffer(s->avctx, &s->frame);
+        av_frame_unref(&s->frame);
         s->avctx->sample_aspect_ratio = (AVRational){ 1 << width_shift, 1 };
         s->avctx->pix_fmt = new_pix_fmt;
         avcodec_set_dimensions(s->avctx, s->w, s->h);
@@ -469,7 +469,6 @@
 //        avctx->pix_fmt = AV_PIX_FMT_RGB555;
 
     avcodec_get_frame_defaults(&s->frame);
-    s->frame.data[0] = NULL;
 
     /* there is a vertical predictor for each pixel in a line; each vertical
      * predictor is 0 to start with */
@@ -869,13 +868,8 @@
     if ((ret = truemotion1_decode_header(s)) < 0)
         return ret;
 
-    s->frame.reference = 3;
-    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID |
-        FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
-    if ((ret = avctx->reget_buffer(avctx, &s->frame)) < 0) {
-        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0)
         return ret;
-    }
 
     if (compression_types[s->compression].algorithm == ALGO_RGB24H) {
         truemotion1_decode_24bit(s);
@@ -883,8 +877,10 @@
         truemotion1_decode_16bit(s);
     }
 
+    if ((ret = av_frame_ref(data, &s->frame)) < 0)
+        return ret;
+
     *got_frame      = 1;
-    *(AVFrame*)data = s->frame;
 
     /* report that the buffer was completely consumed */
     return buf_size;
@@ -894,9 +890,7 @@
 {
     TrueMotion1Context *s = avctx->priv_data;
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
-
+    av_frame_unref(&s->frame);
     av_free(s->vert_pred);
 
     return 0;
diff --git a/libavcodec/truemotion2.c b/libavcodec/truemotion2.c
index eacd728..7783386 100644
--- a/libavcodec/truemotion2.c
+++ b/libavcodec/truemotion2.c
@@ -28,6 +28,7 @@
 #include "bytestream.h"
 #include "get_bits.h"
 #include "dsputil.h"
+#include "internal.h"
 
 #define TM2_ESCAPE 0x80000000
 #define TM2_DELTAS 64
@@ -230,7 +231,7 @@
 
     switch (magic) {
     case TM2_OLD_HEADER_MAGIC:
-        av_log_missing_feature(ctx->avctx, "TM2 old header", 1);
+        avpriv_request_sample(ctx->avctx, "Old TM2 header");
         return 0;
     case TM2_NEW_HEADER_MAGIC:
         return 0;
@@ -866,12 +867,9 @@
         av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
         return AVERROR(ENOMEM);
     }
-    p->reference = 3;
-    p->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
-    if ((ret = avctx->reget_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+
+    if ((ret = ff_reget_buffer(avctx, p)) < 0)
         return ret;
-    }
 
     l->dsp.bswap_buf((uint32_t*)l->buffer, (const uint32_t*)buf, buf_size >> 2);
 
@@ -902,9 +900,9 @@
 
     l->cur = !l->cur;
     *got_frame      = 1;
-    *(AVFrame*)data = l->pic;
+    ret = av_frame_ref(data, &l->pic);
 
-    return buf_size;
+    return (ret < 0) ? ret : buf_size;
 }
 
 static av_cold int decode_init(AVCodecContext *avctx)
@@ -918,9 +916,8 @@
     }
 
     l->avctx       = avctx;
-    l->pic.data[0] = NULL;
-    avctx->pix_fmt = AV_PIX_FMT_BGR24;
     avcodec_get_frame_defaults(&l->pic);
+    avctx->pix_fmt = AV_PIX_FMT_BGR24;
 
     ff_dsputil_init(&l->dsp, avctx);
 
@@ -989,8 +986,7 @@
     av_freep(&l->buffer);
     l->buffer_size = 0;
 
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
+    av_frame_unref(pic);
 
     return 0;
 }
diff --git a/libavcodec/truespeech.c b/libavcodec/truespeech.c
index fea9ae4..2eb218c 100644
--- a/libavcodec/truespeech.c
+++ b/libavcodec/truespeech.c
@@ -63,7 +63,7 @@
     TSContext *c = avctx->priv_data;
 
     if (avctx->channels != 1) {
-        av_log_ask_for_sample(avctx, "Unsupported channel count: %d\n", avctx->channels);
+        avpriv_request_sample(avctx, "Channel count %d", avctx->channels);
         return AVERROR_PATCHWELCOME;
     }
 
@@ -325,10 +325,8 @@
 
     /* get output buffer */
     frame->nb_samples = iterations * 240;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     samples = (int16_t *)frame->data[0];
 
     memset(samples, 0, iterations * 240 * sizeof(*samples));
diff --git a/libavcodec/tscc.c b/libavcodec/tscc.c
index 7c2d231..6e1e077 100644
--- a/libavcodec/tscc.c
+++ b/libavcodec/tscc.c
@@ -47,7 +47,7 @@
 typedef struct TsccContext {
 
     AVCodecContext *avctx;
-    AVFrame pic;
+    AVFrame *frame;
 
     // Bits per pixel
     int bpp;
@@ -69,15 +69,12 @@
     int buf_size = avpkt->size;
     CamtasiaContext * const c = avctx->priv_data;
     const unsigned char *encoded = buf;
+    AVFrame *frame = c->frame;
     int zret; // Zlib return code
     int ret, len = buf_size;
 
-    c->pic.reference = 3;
-    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
-    if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, frame)) < 0)
         return ret;
-    }
 
     zret = inflateReset(&c->zstream);
     if (zret != Z_OK) {
@@ -99,7 +96,7 @@
     if (zret != Z_DATA_ERROR) {
         bytestream2_init(&c->gb, c->decomp_buf,
                          c->decomp_size - c->zstream.avail_out);
-        ff_msrle_decode(avctx, (AVPicture*)&c->pic, c->bpp, &c->gb);
+        ff_msrle_decode(avctx, (AVPicture*)frame, c->bpp, &c->gb);
     }
 
     /* make the palette available on the way out */
@@ -107,14 +104,15 @@
         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
 
         if (pal) {
-            c->pic.palette_has_changed = 1;
+            frame->palette_has_changed = 1;
             memcpy(c->pal, pal, AVPALETTE_SIZE);
         }
-        memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
+        memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
     }
 
+    if ((ret = av_frame_ref(data, frame)) < 0)
+        return ret;
     *got_frame      = 1;
-    *(AVFrame*)data = c->pic;
 
     /* always report that the buffer was completely consumed */
     return buf_size;
@@ -129,7 +127,6 @@
 
     c->height = avctx->height;
 
-    avcodec_get_frame_defaults(&c->pic);
     // Needed if zlib unused or init aborted before inflateInit
     memset(&c->zstream, 0, sizeof(z_stream));
     switch(avctx->bits_per_coded_sample){
@@ -163,6 +160,8 @@
         return AVERROR_UNKNOWN;
     }
 
+    c->frame = av_frame_alloc();
+
     return 0;
 }
 
@@ -171,9 +170,8 @@
     CamtasiaContext * const c = avctx->priv_data;
 
     av_freep(&c->decomp_buf);
+    av_frame_free(&c->frame);
 
-    if (c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
     inflateEnd(&c->zstream);
 
     return 0;
diff --git a/libavcodec/tscc2.c b/libavcodec/tscc2.c
index b71e7fa..291693a 100644
--- a/libavcodec/tscc2.c
+++ b/libavcodec/tscc2.c
@@ -28,11 +28,12 @@
 #include "avcodec.h"
 #include "get_bits.h"
 #include "bytestream.h"
+#include "internal.h"
 #include "tscc2data.h"
 
 typedef struct TSCC2Context {
     AVCodecContext *avctx;
-    AVFrame        pic;
+    AVFrame       *pic;
     int            mb_width, mb_height;
     uint8_t        *slice_quants;
     int            quant[2];
@@ -199,9 +200,9 @@
         if (q == 0 || q == 3) // skip block
             continue;
         for (i = 0; i < 3; i++) {
-            off = mb_x * 16 + mb_y * 8 * c->pic.linesize[i];
+            off = mb_x * 16 + mb_y * 8 * c->pic->linesize[i];
             ret = tscc2_decode_mb(c, c->q[q - 1], c->quant[q - 1] - 2,
-                                  c->pic.data[i] + off, c->pic.linesize[i], i);
+                                  c->pic->data[i] + off, c->pic->linesize[i], i);
             if (ret)
                 return ret;
         }
@@ -229,17 +230,14 @@
         return AVERROR_INVALIDDATA;
     }
 
-    c->pic.reference    = 3;
-    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
-                          FF_BUFFER_HINTS_REUSABLE;
-    if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, c->pic)) < 0) {
         return ret;
     }
 
     if (frame_type == 0) {
         *got_frame      = 1;
-        *(AVFrame*)data = c->pic;
+        if ((ret = av_frame_ref(data, c->pic)) < 0)
+            return ret;
 
         return buf_size;
     }
@@ -323,12 +321,24 @@
     }
 
     *got_frame      = 1;
-    *(AVFrame*)data = c->pic;
+    if ((ret = av_frame_ref(data, c->pic)) < 0)
+        return ret;
 
     /* always report that the buffer was completely consumed */
     return buf_size;
 }
 
+static av_cold int tscc2_decode_end(AVCodecContext *avctx)
+{
+    TSCC2Context * const c = avctx->priv_data;
+
+    av_frame_free(&c->pic);
+    av_freep(&c->slice_quants);
+    free_vlcs(c);
+
+    return 0;
+}
+
 static av_cold int tscc2_decode_init(AVCodecContext *avctx)
 {
     TSCC2Context * const c = avctx->priv_data;
@@ -352,19 +362,11 @@
         return AVERROR(ENOMEM);
     }
 
-    avctx->coded_frame = &c->pic;
-
-    return 0;
-}
-
-static av_cold int tscc2_decode_end(AVCodecContext *avctx)
-{
-    TSCC2Context * const c = avctx->priv_data;
-
-    if (c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
-    av_freep(&c->slice_quants);
-    free_vlcs(c);
+    c->pic = av_frame_alloc();
+    if (!c->pic) {
+        tscc2_decode_end(avctx);
+        return AVERROR(ENOMEM);
+    }
 
     return 0;
 }
diff --git a/libavcodec/tta.c b/libavcodec/tta.c
index 4240946..8b4ca5a 100644
--- a/libavcodec/tta.c
+++ b/libavcodec/tta.c
@@ -334,10 +334,8 @@
 
     /* get output buffer */
     frame->nb_samples = framelen;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     // decode directly to output buffer for 24-bit sample format
     if (s->bps == 3)
diff --git a/libavcodec/twinvq.c b/libavcodec/twinvq.c
index e9d206e..8b9c79f 100644
--- a/libavcodec/twinvq.c
+++ b/libavcodec/twinvq.c
@@ -832,10 +832,8 @@
     /* get output buffer */
     if (tctx->discarded_packets >= 2) {
         frame->nb_samples = mtab->size;
-        if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
             return ret;
-        }
         out = (float **)frame->extended_data;
     }
 
diff --git a/libavcodec/txd.c b/libavcodec/txd.c
index b5b34ac..e8d483f 100644
--- a/libavcodec/txd.c
+++ b/libavcodec/txd.c
@@ -28,25 +28,10 @@
 #include "internal.h"
 #include "s3tc.h"
 
-typedef struct TXDContext {
-    AVFrame picture;
-} TXDContext;
-
-static av_cold int txd_init(AVCodecContext *avctx) {
-    TXDContext *s = avctx->priv_data;
-
-    avcodec_get_frame_defaults(&s->picture);
-    avctx->coded_frame = &s->picture;
-
-    return 0;
-}
-
 static int txd_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
                             AVPacket *avpkt) {
-    TXDContext * const s = avctx->priv_data;
     GetByteContext gb;
-    AVFrame *picture = data;
-    AVFrame * const p = &s->picture;
+    AVFrame * const p = data;
     unsigned int version, w, h, d3d_format, depth, stride, flags;
     unsigned int y, v;
     uint8_t *ptr;
@@ -78,17 +63,12 @@
         return AVERROR_PATCHWELCOME;
     }
 
-    if (p->data[0])
-        avctx->release_buffer(avctx, p);
-
     if ((ret = av_image_check_size(w, h, 0, avctx)) < 0)
         return ret;
     if (w != avctx->width || h != avctx->height)
         avcodec_set_dimensions(avctx, w, h);
-    if ((ret = ff_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
         return ret;
-    }
 
     p->pict_type = AV_PICTURE_TYPE_I;
 
@@ -143,7 +123,6 @@
         }
     }
 
-    *picture   = s->picture;
     *got_frame = 1;
 
     return avpkt->size;
@@ -153,22 +132,10 @@
     return AVERROR_PATCHWELCOME;
 }
 
-static av_cold int txd_end(AVCodecContext *avctx) {
-    TXDContext *s = avctx->priv_data;
-
-    if (s->picture.data[0])
-        avctx->release_buffer(avctx, &s->picture);
-
-    return 0;
-}
-
 AVCodec ff_txd_decoder = {
     .name           = "txd",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_TXD,
-    .priv_data_size = sizeof(TXDContext),
-    .init           = txd_init,
-    .close          = txd_end,
     .decode         = txd_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("Renderware TXD (TeXture Dictionary) image"),
diff --git a/libavcodec/ulti.c b/libavcodec/ulti.c
index a9413ad..2120d97 100644
--- a/libavcodec/ulti.c
+++ b/libavcodec/ulti.c
@@ -30,6 +30,7 @@
 
 #include "avcodec.h"
 #include "bytestream.h"
+#include "internal.h"
 
 #include "ulti_cb.h"
 
@@ -53,6 +54,7 @@
     avctx->coded_frame = &s->frame;
     avctx->coded_frame = (AVFrame*) &s->frame;
     s->ulti_codebook = ulti_codebook;
+    avcodec_get_frame_defaults(&s->frame);
 
     return 0;
 }
@@ -61,8 +63,7 @@
     UltimotionDecodeContext *s = avctx->priv_data;
     AVFrame *pic = &s->frame;
 
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
+    av_frame_unref(pic);
 
     return 0;
 }
@@ -222,16 +223,12 @@
     int blocks = 0;
     int done = 0;
     int x = 0, y = 0;
-    int i;
+    int i, ret;
     int skip;
     int tmp;
 
-    s->frame.reference = 3;
-    s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
-    if (avctx->reget_buffer(avctx, &s->frame) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
-        return -1;
-    }
+    if ((ret = ff_reget_buffer(avctx, &s->frame)) < 0)
+        return ret;
 
     bytestream2_init(&s->gb, buf, buf_size);
 
@@ -408,7 +405,8 @@
     }
 
     *got_frame = 1;
-    *(AVFrame*)data= s->frame;
+    if ((ret = av_frame_ref(data, &s->frame)) < 0)
+        return ret;
 
     return buf_size;
 
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index adee2ea..1c4280d 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -31,6 +31,7 @@
 #include "libavutil/bprint.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/crc.h"
+#include "libavutil/frame.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/imgutils.h"
@@ -44,6 +45,7 @@
 #include "frame_thread_encoder.h"
 #include "internal.h"
 #include "bytestream.h"
+#include "version.h"
 #include <stdlib.h>
 #include <stdarg.h>
 #include <limits.h>
@@ -139,7 +141,8 @@
         return;
     initialized = 1;
 
-    ff_dsputil_static_init();
+    if (CONFIG_DSPUTIL)
+        ff_dsputil_static_init();
 }
 
 int av_codec_is_encoder(const AVCodec *codec)
@@ -179,8 +182,6 @@
     s->height       = -((-height) >> s->lowres);
 }
 
-#define INTERNAL_BUFFER_SIZE (32 + 1)
-
 #if (ARCH_ARM && HAVE_NEON) || ARCH_PPC || HAVE_MMX
 #   define STRIDE_ALIGN 16
 #else
@@ -358,89 +359,26 @@
     return ret;
 }
 
-static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
+static int update_frame_pool(AVCodecContext *avctx, AVFrame *frame)
 {
-    AVCodecInternal *avci = avctx->internal;
-    int buf_size, ret;
+    FramePool *pool = avctx->internal->pool;
+    int i, ret;
 
-    av_freep(&avci->audio_data);
-    buf_size = av_samples_get_buffer_size(NULL, avctx->channels,
-                                          frame->nb_samples, avctx->sample_fmt,
-                                          0);
-    if (buf_size < 0)
-        return AVERROR(EINVAL);
-
-    frame->data[0] = av_mallocz(buf_size);
-    if (!frame->data[0])
-        return AVERROR(ENOMEM);
-
-    ret = avcodec_fill_audio_frame(frame, avctx->channels, avctx->sample_fmt,
-                                   frame->data[0], buf_size, 0);
-    if (ret < 0) {
-        av_freep(&frame->data[0]);
-        return ret;
-    }
-
-    avci->audio_data = frame->data[0];
-    if (avctx->debug & FF_DEBUG_BUFFERS)
-        av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
-                                    "internal audio buffer used\n", frame);
-
-    return 0;
-}
-
-static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
-{
-    int i;
-    int w = s->width;
-    int h = s->height;
-    InternalBuffer *buf;
-    AVCodecInternal *avci = s->internal;
-
-    if (pic->data[0] != NULL) {
-        av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
-        return -1;
-    }
-    if (avci->buffer_count >= INTERNAL_BUFFER_SIZE) {
-        av_log(s, AV_LOG_ERROR, "buffer_count overflow (missing release_buffer?)\n");
-        return -1;
-    }
-
-    if (av_image_check_size(w, h, 0, s) || s->pix_fmt<0) {
-        av_log(s, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
-        return -1;
-    }
-
-    if (!avci->buffer) {
-        avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
-                                  sizeof(InternalBuffer));
-    }
-
-    buf = &avci->buffer[avci->buffer_count];
-
-    if (buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)) {
-        for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
-            av_freep(&buf->base[i]);
-            buf->data[i] = NULL;
-        }
-    }
-
-    if (!buf->base[0]) {
-        int h_chroma_shift, v_chroma_shift;
-        int size[4] = { 0 };
-        int tmpsize;
-        int unaligned;
+    switch (avctx->codec_type) {
+    case AVMEDIA_TYPE_VIDEO: {
         AVPicture picture;
-        int stride_align[AV_NUM_DATA_POINTERS];
-        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->pix_fmt);
-        const int pixel_size = desc->comp[0].step_minus1 + 1;
+        int size[4] = { 0 };
+        int w = frame->width;
+        int h = frame->height;
+        int tmpsize, unaligned;
 
-        av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift,
-                                         &v_chroma_shift);
+        if (pool->format == frame->format &&
+            pool->width == frame->width && pool->height == frame->height)
+            return 0;
 
-        avcodec_align_dimensions2(s, &w, &h, stride_align);
+        avcodec_align_dimensions2(avctx, &w, &h, pool->stride_align);
 
-        if (!(s->flags & CODEC_FLAG_EMU_EDGE)) {
+        if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
             w += EDGE_WIDTH * 2;
             h += EDGE_WIDTH * 2;
         }
@@ -448,16 +386,17 @@
         do {
             // NOTE: do not align linesizes individually, this breaks e.g. assumptions
             // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
-            av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
+            av_image_fill_linesizes(picture.linesize, avctx->pix_fmt, w);
             // increase alignment of w for next try (rhs gives the lowest bit set in w)
             w += w & ~(w - 1);
 
             unaligned = 0;
             for (i = 0; i < 4; i++)
-                unaligned |= picture.linesize[i] % stride_align[i];
+                unaligned |= picture.linesize[i] % pool->stride_align[i];
         } while (unaligned);
 
-        tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
+        tmpsize = av_image_fill_pointers(picture.data, avctx->pix_fmt, h,
+                                         NULL, picture.linesize);
         if (tmpsize < 0)
             return -1;
 
@@ -465,49 +404,161 @@
             size[i] = picture.data[i + 1] - picture.data[i];
         size[i] = tmpsize - (picture.data[i] - picture.data[0]);
 
-        memset(buf->base, 0, sizeof(buf->base));
-        memset(buf->data, 0, sizeof(buf->data));
-
-        for (i = 0; i < 4 && size[i]; i++) {
-            const int h_shift = i == 0 ? 0 : h_chroma_shift;
-            const int v_shift = i == 0 ? 0 : v_chroma_shift;
-
-            buf->linesize[i] = picture.linesize[i];
-
-            buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
-            if (buf->base[i] == NULL)
-                return AVERROR(ENOMEM);
-
-            // no edge if EDGE EMU or not planar YUV
-            if ((s->flags & CODEC_FLAG_EMU_EDGE) || !size[2])
-                buf->data[i] = buf->base[i];
-            else
-                buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i] * EDGE_WIDTH >> v_shift) + (pixel_size * EDGE_WIDTH >> h_shift), stride_align[i]);
+        for (i = 0; i < 4; i++) {
+            av_buffer_pool_uninit(&pool->pools[i]);
+            pool->linesize[i] = picture.linesize[i];
+            if (size[i]) {
+                pool->pools[i] = av_buffer_pool_init(size[i] + 16,
+                                                     CONFIG_MEMORY_POISONING ?
+                                                        NULL :
+                                                        av_buffer_allocz);
+                if (!pool->pools[i]) {
+                    ret = AVERROR(ENOMEM);
+                    goto fail;
+                }
+            }
         }
-        for (; i < AV_NUM_DATA_POINTERS; i++) {
-            buf->base[i]     = buf->data[i] = NULL;
-            buf->linesize[i] = 0;
+        pool->format = frame->format;
+        pool->width  = frame->width;
+        pool->height = frame->height;
+
+        break;
         }
-        if (size[1] && !size[2])
-            avpriv_set_systematic_pal2((uint32_t *)buf->data[1], s->pix_fmt);
-        buf->width   = s->width;
-        buf->height  = s->height;
-        buf->pix_fmt = s->pix_fmt;
+    case AVMEDIA_TYPE_AUDIO: {
+        int ch     = av_frame_get_channels(frame); //av_get_channel_layout_nb_channels(frame->channel_layout);
+        int planar = av_sample_fmt_is_planar(frame->format);
+        int planes = planar ? ch : 1;
+
+        if (pool->format == frame->format && pool->planes == planes &&
+            pool->channels == ch && frame->nb_samples == pool->samples)
+            return 0;
+
+        av_buffer_pool_uninit(&pool->pools[0]);
+        ret = av_samples_get_buffer_size(&pool->linesize[0], ch,
+                                         frame->nb_samples, frame->format, 0);
+        if (ret < 0)
+            goto fail;
+
+        pool->pools[0] = av_buffer_pool_init(pool->linesize[0], NULL);
+        if (!pool->pools[0]) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
+
+        pool->format     = frame->format;
+        pool->planes     = planes;
+        pool->channels   = ch;
+        pool->samples = frame->nb_samples;
+        break;
+        }
+    default: av_assert0(0);
+    }
+    return 0;
+fail:
+    for (i = 0; i < 4; i++)
+        av_buffer_pool_uninit(&pool->pools[i]);
+    pool->format = -1;
+    pool->planes = pool->channels = pool->samples = 0;
+    pool->width  = pool->height = 0;
+    return ret;
+}
+
+static int audio_get_buffer(AVCodecContext *avctx, AVFrame *frame)
+{
+    FramePool *pool = avctx->internal->pool;
+    int planes = pool->planes;
+    int i;
+
+    frame->linesize[0] = pool->linesize[0];
+
+    if (planes > AV_NUM_DATA_POINTERS) {
+        frame->extended_data = av_mallocz(planes * sizeof(*frame->extended_data));
+        frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
+        frame->extended_buf  = av_mallocz(frame->nb_extended_buf *
+                                          sizeof(*frame->extended_buf));
+        if (!frame->extended_data || !frame->extended_buf) {
+            av_freep(&frame->extended_data);
+            av_freep(&frame->extended_buf);
+            return AVERROR(ENOMEM);
+        }
+    } else {
+        frame->extended_data = frame->data;
+        av_assert0(frame->nb_extended_buf == 0);
     }
 
-    for (i = 0; i < AV_NUM_DATA_POINTERS; i++) {
-        pic->base[i]     = buf->base[i];
-        pic->data[i]     = buf->data[i];
-        pic->linesize[i] = buf->linesize[i];
+    for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
+        frame->buf[i] = av_buffer_pool_get(pool->pools[0]);
+        if (!frame->buf[i])
+            goto fail;
+        frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
     }
-    pic->extended_data = pic->data;
-    avci->buffer_count++;
+    for (i = 0; i < frame->nb_extended_buf; i++) {
+        frame->extended_buf[i] = av_buffer_pool_get(pool->pools[0]);
+        if (!frame->extended_buf[i])
+            goto fail;
+        frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
+    }
 
-    if (s->debug & FF_DEBUG_BUFFERS)
-        av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
-                                "buffers used\n", pic, avci->buffer_count);
+    if (avctx->debug & FF_DEBUG_BUFFERS)
+        av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p", frame);
 
     return 0;
+fail:
+    av_frame_unref(frame);
+    return AVERROR(ENOMEM);
+}
+
+static int video_get_buffer(AVCodecContext *s, AVFrame *pic)
+{
+    FramePool *pool = s->internal->pool;
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pic->format);
+    int pixel_size = desc->comp[0].step_minus1 + 1;
+    int h_chroma_shift, v_chroma_shift;
+    int i;
+
+    if (pic->data[0] != NULL) {
+        av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
+        return -1;
+    }
+
+    memset(pic->data, 0, sizeof(pic->data));
+    pic->extended_data = pic->data;
+
+    av_pix_fmt_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
+
+    for (i = 0; i < 4 && pool->pools[i]; i++) {
+        const int h_shift = i == 0 ? 0 : h_chroma_shift;
+        const int v_shift = i == 0 ? 0 : v_chroma_shift;
+
+        pic->linesize[i] = pool->linesize[i];
+
+        pic->buf[i] = av_buffer_pool_get(pool->pools[i]);
+        if (!pic->buf[i])
+            goto fail;
+
+        // no edge if EDGE EMU or not planar YUV
+        if ((s->flags & CODEC_FLAG_EMU_EDGE) || !pool->pools[2])
+            pic->data[i] = pic->buf[i]->data;
+        else {
+            pic->data[i] = pic->buf[i]->data +
+                FFALIGN((pic->linesize[i] * EDGE_WIDTH >> v_shift) +
+                        (pixel_size * EDGE_WIDTH >> h_shift), pool->stride_align[i]);
+        }
+    }
+    for (; i < AV_NUM_DATA_POINTERS; i++) {
+        pic->data[i] = NULL;
+        pic->linesize[i] = 0;
+    }
+    if (pic->data[1] && !pic->data[2])
+        avpriv_set_systematic_pal2((uint32_t *)pic->data[1], s->pix_fmt);
+
+    if (s->debug & FF_DEBUG_BUFFERS)
+        av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p\n", pic);
+
+    return 0;
+fail:
+    av_frame_unref(pic);
+    return AVERROR(ENOMEM);
 }
 
 void avpriv_color_frame(AVFrame *frame, const int c[4])
@@ -532,9 +583,17 @@
     }
 }
 
-int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
+int avcodec_default_get_buffer2(AVCodecContext *avctx, AVFrame *frame, int flags)
 {
+    int ret;
+
+    if ((ret = update_frame_pool(avctx, frame)) < 0)
+        return ret;
+
+#if FF_API_GET_BUFFER
     frame->type = FF_BUFFER_TYPE_INTERNAL;
+#endif
+
     switch (avctx->codec_type) {
     case AVMEDIA_TYPE_VIDEO:
         return video_get_buffer(avctx, frame);
@@ -545,124 +604,288 @@
     }
 }
 
-void ff_init_buffer_info(AVCodecContext *s, AVFrame *frame)
+int ff_init_buffer_info(AVCodecContext *avctx, AVFrame *frame)
 {
-    if (s->pkt) {
-        frame->pkt_pts = s->pkt->pts;
-        frame->pkt_pos = s->pkt->pos;
-        frame->pkt_duration = s->pkt->duration;
-        frame->pkt_size = s->pkt->size;
+    if (avctx->pkt) {
+        frame->pkt_pts = avctx->pkt->pts;
+        av_frame_set_pkt_pos     (frame, avctx->pkt->pos);
+        av_frame_set_pkt_duration(frame, avctx->pkt->duration);
+        av_frame_set_pkt_size    (frame, avctx->pkt->size);
     } else {
         frame->pkt_pts = AV_NOPTS_VALUE;
-        frame->pkt_pos = -1;
-        frame->pkt_duration = 0;
-        frame->pkt_size = -1;
+        av_frame_set_pkt_pos     (frame, -1);
+        av_frame_set_pkt_duration(frame, 0);
+        av_frame_set_pkt_size    (frame, -1);
     }
-    frame->reordered_opaque = s->reordered_opaque;
+    frame->reordered_opaque = avctx->reordered_opaque;
 
-    switch (s->codec->type) {
+    switch (avctx->codec->type) {
     case AVMEDIA_TYPE_VIDEO:
-        frame->width               = s->width;
-        frame->height              = s->height;
-        frame->format              = s->pix_fmt;
-        frame->sample_aspect_ratio = s->sample_aspect_ratio;
+        frame->width  = FFMAX(avctx->width , -((-avctx->coded_width )>>avctx->lowres));
+        frame->height = FFMAX(avctx->height, -((-avctx->coded_height)>>avctx->lowres));
+        if (frame->format < 0)
+            frame->format              = avctx->pix_fmt;
+        if (!frame->sample_aspect_ratio.num)
+            frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
         break;
     case AVMEDIA_TYPE_AUDIO:
-        frame->sample_rate    = s->sample_rate;
-        frame->format         = s->sample_fmt;
-        frame->channel_layout = s->channel_layout;
-        frame->channels       = s->channels;
+        if (!frame->sample_rate)
+            frame->sample_rate    = avctx->sample_rate;
+        if (frame->format < 0)
+            frame->format         = avctx->sample_fmt;
+        if (!frame->channel_layout) {
+            if (avctx->channel_layout) {
+                 if (av_get_channel_layout_nb_channels(avctx->channel_layout) !=
+                     avctx->channels) {
+                     av_log(avctx, AV_LOG_ERROR, "Inconsistent channel "
+                            "configuration.\n");
+                     return AVERROR(EINVAL);
+                 }
+
+                frame->channel_layout = avctx->channel_layout;
+            } else {
+                if (avctx->channels > FF_SANE_NB_CHANNELS) {
+                    av_log(avctx, AV_LOG_ERROR, "Too many channels: %d.\n",
+                           avctx->channels);
+                    return AVERROR(ENOSYS);
+                }
+            }
+        }
+        av_frame_set_channels(frame, avctx->channels);
         break;
     }
+    return 0;
 }
 
-int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
+#if FF_API_GET_BUFFER
+int avcodec_default_get_buffer(AVCodecContext *avctx, AVFrame *frame)
 {
-    ff_init_buffer_info(avctx, frame);
-
-    return avctx->get_buffer(avctx, frame);
+    return avcodec_default_get_buffer2(avctx, frame, 0);
 }
 
-void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
+typedef struct CompatReleaseBufPriv {
+    AVCodecContext avctx;
+    AVFrame frame;
+} CompatReleaseBufPriv;
+
+static void compat_free_buffer(void *opaque, uint8_t *data)
 {
-    int i;
-    InternalBuffer *buf, *last;
-    AVCodecInternal *avci = s->internal;
+    CompatReleaseBufPriv *priv = opaque;
+    if (priv->avctx.release_buffer)
+        priv->avctx.release_buffer(&priv->avctx, &priv->frame);
+    av_freep(&priv);
+}
 
-    av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
+static void compat_release_buffer(void *opaque, uint8_t *data)
+{
+    AVBufferRef *buf = opaque;
+    av_buffer_unref(&buf);
+}
+#endif
 
-    assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
-    assert(avci->buffer_count);
+static int get_buffer_internal(AVCodecContext *avctx, AVFrame *frame, int flags)
+{
+    int ret;
 
-    if (avci->buffer) {
-        buf = NULL; /* avoids warning */
-        for (i = 0; i < avci->buffer_count; i++) { //just 3-5 checks so is not worth to optimize
-            buf = &avci->buffer[i];
-            if (buf->data[0] == pic->data[0])
-                break;
+    if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
+        if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0 || avctx->pix_fmt<0) {
+            av_log(avctx, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
+            return AVERROR(EINVAL);
         }
-        av_assert0(i < avci->buffer_count);
-        avci->buffer_count--;
-        last = &avci->buffer[avci->buffer_count];
+    }
+    if ((ret = ff_init_buffer_info(avctx, frame)) < 0)
+        return ret;
 
-        if (buf != last)
-            FFSWAP(InternalBuffer, *buf, *last);
+#if FF_API_GET_BUFFER
+    /*
+     * Wrap an old get_buffer()-allocated buffer in an bunch of AVBuffers.
+     * We wrap each plane in its own AVBuffer. Each of those has a reference to
+     * a dummy AVBuffer as its private data, unreffing it on free.
+     * When all the planes are freed, the dummy buffer's free callback calls
+     * release_buffer().
+     */
+    if (avctx->get_buffer) {
+        CompatReleaseBufPriv *priv = NULL;
+        AVBufferRef *dummy_buf = NULL;
+        int planes, i, ret;
+
+        if (flags & AV_GET_BUFFER_FLAG_REF)
+            frame->reference    = 1;
+
+        ret = avctx->get_buffer(avctx, frame);
+        if (ret < 0)
+            return ret;
+
+        /* return if the buffers are already set up
+         * this would happen e.g. when a custom get_buffer() calls
+         * avcodec_default_get_buffer
+         */
+        if (frame->buf[0])
+            return 0;
+
+        priv = av_mallocz(sizeof(*priv));
+        if (!priv) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
+        priv->avctx = *avctx;
+        priv->frame = *frame;
+
+        dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, priv, 0);
+        if (!dummy_buf) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
+
+#define WRAP_PLANE(ref_out, data, data_size)                            \
+do {                                                                    \
+    AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf);                  \
+    if (!dummy_ref) {                                                   \
+        ret = AVERROR(ENOMEM);                                          \
+        goto fail;                                                      \
+    }                                                                   \
+    ref_out = av_buffer_create(data, data_size, compat_release_buffer,  \
+                               dummy_ref, 0);                           \
+    if (!ref_out) {                                                     \
+        av_frame_unref(frame);                                          \
+        ret = AVERROR(ENOMEM);                                          \
+        goto fail;                                                      \
+    }                                                                   \
+} while (0)
+
+        if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
+            const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
+
+            planes = av_pix_fmt_count_planes(frame->format);
+            /* workaround for AVHWAccel plane count of 0, buf[0] is used as
+               check for allocated buffers: make libavcodec happy */
+            if (desc && desc->flags & PIX_FMT_HWACCEL)
+                planes = 1;
+            if (!desc || planes <= 0) {
+                ret = AVERROR(EINVAL);
+                goto fail;
+            }
+
+            for (i = 0; i < planes; i++) {
+                int v_shift    = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
+                int plane_size = (frame->height >> v_shift) * frame->linesize[i];
+
+                WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
+            }
+        } else {
+            int planar = av_sample_fmt_is_planar(frame->format);
+            planes = planar ? avctx->channels : 1;
+
+            if (planes > FF_ARRAY_ELEMS(frame->buf)) {
+                frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
+                frame->extended_buf = av_malloc(sizeof(*frame->extended_buf) *
+                                                frame->nb_extended_buf);
+                if (!frame->extended_buf) {
+                    ret = AVERROR(ENOMEM);
+                    goto fail;
+                }
+            }
+
+            for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
+                WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
+
+            for (i = 0; i < frame->nb_extended_buf; i++)
+                WRAP_PLANE(frame->extended_buf[i],
+                           frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
+                           frame->linesize[0]);
+        }
+
+        av_buffer_unref(&dummy_buf);
+
+        frame->width  = avctx->width;
+        frame->height = avctx->height;
+
+        return 0;
+
+fail:
+        avctx->release_buffer(avctx, frame);
+        av_freep(&priv);
+        av_buffer_unref(&dummy_buf);
+        return ret;
+    }
+#endif
+
+    ret = avctx->get_buffer2(avctx, frame, flags);
+
+    if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
+        frame->width  = avctx->width;
+        frame->height = avctx->height;
     }
 
-    for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
-        pic->data[i] = NULL;
-//        pic->base[i]=NULL;
+    return ret;
+}
 
-    if (s->debug & FF_DEBUG_BUFFERS)
-        av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d "
-                                "buffers used\n", pic, avci->buffer_count);
+int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
+{
+    int ret = get_buffer_internal(avctx, frame, flags);
+    if (ret < 0)
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    return ret;
+}
+
+static int reget_buffer_internal(AVCodecContext *avctx, AVFrame *frame)
+{
+    AVFrame tmp;
+    int ret;
+
+    av_assert0(avctx->codec_type == AVMEDIA_TYPE_VIDEO);
+
+    if (frame->data[0] && (frame->width != avctx->width || frame->height != avctx->height || frame->format != avctx->pix_fmt)) {
+        av_log(avctx, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
+               frame->width, frame->height, av_get_pix_fmt_name(frame->format), avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
+        av_frame_unref(frame);
+    }
+
+    ff_init_buffer_info(avctx, frame);
+
+    if (!frame->data[0])
+        return ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
+
+    if (av_frame_is_writable(frame))
+        return 0;
+
+    av_frame_move_ref(&tmp, frame);
+
+    ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF);
+    if (ret < 0) {
+        av_frame_unref(&tmp);
+        return ret;
+    }
+
+    av_image_copy(frame->data, frame->linesize, tmp.data, tmp.linesize,
+                  frame->format, frame->width, frame->height);
+
+    av_frame_unref(&tmp);
+
+    return 0;
+}
+
+int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
+{
+    int ret = reget_buffer_internal(avctx, frame);
+    if (ret < 0)
+        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    return ret;
+}
+
+#if FF_API_GET_BUFFER
+void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic)
+{
+    av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
+
+    av_frame_unref(pic);
 }
 
 int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic)
 {
-    AVFrame temp_pic;
-    int i, ret;
-
-    av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
-
-    if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
-        av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
-               pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
-        s->release_buffer(s, pic);
-    }
-
-    ff_init_buffer_info(s, pic);
-
-    /* If no picture return a new buffer */
-    if (pic->data[0] == NULL) {
-        /* We will copy from buffer, so must be readable */
-        pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
-        return ff_get_buffer(s, pic);
-    }
-
-    assert(s->pix_fmt == pic->format);
-
-    /* If internal buffer type return the same buffer */
-    if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
-        return 0;
-    }
-
-    /*
-     * Not internal type and reget_buffer not overridden, emulate cr buffer
-     */
-    temp_pic = *pic;
-    for (i = 0; i < AV_NUM_DATA_POINTERS; i++)
-        pic->data[i] = pic->base[i] = NULL;
-    pic->opaque = NULL;
-    /* Allocate new frame */
-    if ((ret = ff_get_buffer(s, pic)))
-        return ret;
-    /* Copy image data from old buffer to new buffer */
-    av_picture_copy((AVPicture *)pic, (AVPicture *)&temp_pic, s->pix_fmt, s->width,
-                    s->height);
-    s->release_buffer(s, &temp_pic); // Release old frame
-    return 0;
+    av_assert0(0);
 }
+#endif
 
 int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
 {
@@ -714,11 +937,11 @@
 
     frame->pts                   =
     frame->pkt_dts               =
-    frame->pkt_pts               =
-    frame->best_effort_timestamp = AV_NOPTS_VALUE;
-    frame->pkt_duration        = 0;
-    frame->pkt_pos             = -1;
-    frame->pkt_size            = -1;
+    frame->pkt_pts               = AV_NOPTS_VALUE;
+    av_frame_set_best_effort_timestamp(frame, AV_NOPTS_VALUE);
+    av_frame_set_pkt_duration         (frame, 0);
+    av_frame_set_pkt_pos              (frame, -1);
+    av_frame_set_pkt_size             (frame, -1);
     frame->key_frame           = 1;
     frame->sample_aspect_ratio = (AVRational) {0, 1 };
     frame->format              = -1; /* unknown */
@@ -757,16 +980,6 @@
     type av_##name##_get_##field(const str *s) { return s->field; } \
     void av_##name##_set_##field(str *s, type v) { s->field = v; }
 
-MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
-MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
-MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
-MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
-MAKE_ACCESSORS(AVFrame, frame, int,     channels)
-MAKE_ACCESSORS(AVFrame, frame, int,     sample_rate)
-MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
-MAKE_ACCESSORS(AVFrame, frame, int,     decode_error_flags)
-MAKE_ACCESSORS(AVFrame, frame, int,     pkt_size)
-
 MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
 MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
 
@@ -854,6 +1067,12 @@
         goto end;
     }
 
+    avctx->internal->pool = av_mallocz(sizeof(*avctx->internal->pool));
+    if (!avctx->internal->pool) {
+        ret = AVERROR(ENOMEM);
+        goto free_and_end;
+    }
+
     if (codec->priv_data_size > 0) {
         if (!avctx->priv_data) {
             avctx->priv_data = av_mallocz(codec->priv_data_size);
@@ -874,9 +1093,9 @@
     if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
         goto free_and_end;
 
-    //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
-    if (!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == AV_CODEC_ID_H264)){
-
+    // only call avcodec_set_dimensions() for non H.264/VP6F codecs so as not to overwrite previously setup dimensions
+    if (!(avctx->coded_width && avctx->coded_height && avctx->width && avctx->height &&
+          (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_VP6F))) {
     if (avctx->coded_width && avctx->coded_height)
         avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
     else if (avctx->width && avctx->height)
@@ -919,13 +1138,13 @@
         avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
         const char *codec_string = av_codec_is_encoder(codec) ? "encoder" : "decoder";
         AVCodec *codec2;
-        av_log(NULL, AV_LOG_ERROR,
+        av_log(avctx, AV_LOG_ERROR,
                "The %s '%s' is experimental but experimental codecs are not enabled, "
                "add '-strict %d' if you want to use it.\n",
                codec_string, codec->name, FF_COMPLIANCE_EXPERIMENTAL);
         codec2 = av_codec_is_encoder(codec) ? avcodec_find_encoder(codec->id) : avcodec_find_decoder(codec->id);
         if (!(codec2->capabilities & CODEC_CAP_EXPERIMENTAL))
-            av_log(NULL, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
+            av_log(avctx, AV_LOG_ERROR, "Alternatively use the non experimental %s '%s'.\n",
                 codec_string, codec2->name);
         ret = AVERROR_EXPERIMENTAL;
         goto free_and_end;
@@ -940,7 +1159,7 @@
     if (!HAVE_THREADS)
         av_log(avctx, AV_LOG_WARNING, "Warning: not compiled with thread support, using thread emulation\n");
 
-    if (HAVE_THREADS) {
+    if (CONFIG_FRAME_THREAD_ENCODER) {
         ff_unlock_avcodec(); //we will instanciate a few encoders thus kick the counter to prevent false detection of a problem
         ret = ff_frame_thread_encoder_init(avctx, options ? *options : NULL);
         ff_lock_avcodec(avctx);
@@ -1054,7 +1273,7 @@
         }
         if (   (avctx->codec_type == AVMEDIA_TYPE_VIDEO || avctx->codec_type == AVMEDIA_TYPE_AUDIO)
             && avctx->bit_rate>0 && avctx->bit_rate<1000) {
-            av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extreemly low, did you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
+            av_log(avctx, AV_LOG_WARNING, "Bitrate %d is extremely low, maybe you mean %dk\n", avctx->bit_rate, avctx->bit_rate);
         }
 
         if (!avctx->rc_initial_buffer_occupancy)
@@ -1116,12 +1335,23 @@
                 if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_AUTOMATIC)
                     avctx->sub_charenc_mode = FF_SUB_CHARENC_MODE_PRE_DECODER;
 
-                if (!CONFIG_ICONV && avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
+                if (avctx->sub_charenc_mode == FF_SUB_CHARENC_MODE_PRE_DECODER) {
+#if CONFIG_ICONV
+                    iconv_t cd = iconv_open("UTF-8", avctx->sub_charenc);
+                    if (cd == (iconv_t)-1) {
+                        av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
+                               "with input character encoding \"%s\"\n", avctx->sub_charenc);
+                        ret = AVERROR(errno);
+                        goto free_and_end;
+                    }
+                    iconv_close(cd);
+#else
                     av_log(avctx, AV_LOG_ERROR, "Character encoding subtitles "
                            "conversion needs a libavcodec built with iconv support "
                            "for this codec\n");
                     ret = AVERROR(ENOSYS);
                     goto free_and_end;
+#endif
                 }
             }
         }
@@ -1137,6 +1367,8 @@
 free_and_end:
     av_dict_free(&tmp);
     av_freep(&avctx->priv_data);
+    if (avctx->internal)
+        av_freep(&avctx->internal->pool);
     av_freep(&avctx->internal);
     avctx->codec = NULL;
     goto end;
@@ -1160,7 +1392,10 @@
     }
 
     if (avpkt->data) {
+        AVBufferRef *buf = avpkt->buf;
+#if FF_API_DESTRUCT_PACKET
         void *destruct = avpkt->destruct;
+#endif
 
         if (avpkt->size < size) {
             av_log(avctx, AV_LOG_ERROR, "User packet is too small (%d < %d)\n", avpkt->size, size);
@@ -1168,7 +1403,10 @@
         }
 
         av_init_packet(avpkt);
+#if FF_API_DESTRUCT_PACKET
         avpkt->destruct = destruct;
+#endif
+        avpkt->buf      = buf;
         avpkt->size     = size;
         return 0;
     } else {
@@ -1316,6 +1554,7 @@
                 avpkt->size = user_pkt.size;
                 ret = -1;
             }
+            avpkt->buf      = user_pkt.buf;
             avpkt->data     = user_pkt.data;
             avpkt->destruct = user_pkt.destruct;
         } else {
@@ -1327,9 +1566,9 @@
 
     if (!ret) {
         if (needs_realloc && avpkt->data) {
-            uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
-            if (new_data)
-                avpkt->data = new_data;
+            ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
+            if (ret >= 0)
+                avpkt->data = avpkt->buf->data;
         }
 
         avctx->frame_number++;
@@ -1482,7 +1721,8 @@
 
     *got_packet_ptr = 0;
 
-    if(HAVE_THREADS && avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
+    if(CONFIG_FRAME_THREAD_ENCODER &&
+       avctx->internal->frame_thread_encoder && (avctx->active_thread_type&FF_THREAD_FRAME))
         return ff_thread_video_encode_frame(avctx, avpkt, frame, got_packet_ptr);
 
     if ((avctx->flags&CODEC_FLAG_PASS1) && avctx->stats_out)
@@ -1513,6 +1753,7 @@
                 avpkt->size = user_pkt.size;
                 ret = -1;
             }
+            avpkt->buf      = user_pkt.buf;
             avpkt->data     = user_pkt.data;
             avpkt->destruct = user_pkt.destruct;
         } else {
@@ -1528,11 +1769,10 @@
         else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
             avpkt->pts = avpkt->dts = frame->pts;
 
-        if (needs_realloc && avpkt->data &&
-            avpkt->destruct == av_destruct_packet) {
-            uint8_t *new_data = av_realloc(avpkt->data, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
-            if (new_data)
-                avpkt->data = new_data;
+        if (needs_realloc && avpkt->data) {
+            ret = av_buffer_realloc(&avpkt->buf, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
+            if (ret >= 0)
+                avpkt->data = avpkt->buf->data;
         }
 
         avctx->frame_number++;
@@ -1540,6 +1780,8 @@
 
     if (ret < 0 || !*got_packet_ptr)
         av_free_packet(avpkt);
+    else
+        av_packet_merge_side_data(avpkt);
 
     emms_c();
     return ret;
@@ -1639,7 +1881,6 @@
     const uint8_t *side_metadata;
     const uint8_t *end;
 
-    av_dict_free(&avctx->metadata);
     side_metadata = av_packet_get_side_data(avctx->pkt,
                                             AV_PKT_DATA_STRINGS_METADATA, &size);
     if (!side_metadata)
@@ -1648,13 +1889,12 @@
     while (side_metadata < end) {
         const uint8_t *key = side_metadata;
         const uint8_t *val = side_metadata + strlen(key) + 1;
-        int ret = av_dict_set(&frame->metadata, key, val, 0);
+        int ret = av_dict_set(avpriv_frame_get_metadatap(frame), key, val, 0);
         if (ret < 0)
             break;
         side_metadata = val + strlen(val) + 1;
     }
 end:
-    avctx->metadata = frame->metadata;
     return ret;
 }
 
@@ -1662,6 +1902,7 @@
                                               int *got_picture_ptr,
                                               const AVPacket *avpkt)
 {
+    AVCodecInternal *avci = avctx->internal;
     int ret;
     // copy to ensure we do not change avpkt
     AVPacket tmp = *avpkt;
@@ -1677,6 +1918,9 @@
 
     avcodec_get_frame_defaults(picture);
 
+    if (!avctx->refcounted_frames)
+        av_frame_unref(&avci->to_free);
+
     if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
         int did_split = av_packet_split_side_data(&tmp);
         apply_param_change(avctx, &tmp);
@@ -1690,7 +1934,7 @@
             picture->pkt_dts = avpkt->dts;
 
             if(!avctx->has_b_frames){
-                picture->pkt_pos         = avpkt->pos;
+                av_frame_set_pkt_pos(picture, avpkt->pos);
             }
             //FIXME these should be under if(!avctx->has_b_frames)
             /* get_buffer is supposed to set frame parameters */
@@ -1712,11 +1956,20 @@
                 ret = avpkt->size;
         }
 
-        if (*got_picture_ptr){
+        if (ret < 0 && picture->data[0])
+            av_frame_unref(picture);
+
+        if (*got_picture_ptr) {
+            if (!avctx->refcounted_frames) {
+                avci->to_free = *picture;
+                avci->to_free.extended_data = avci->to_free.data;
+            }
+
             avctx->frame_number++;
-            picture->best_effort_timestamp = guess_correct_pts(avctx,
-                                                            picture->pkt_pts,
-                                                            picture->pkt_dts);
+            av_frame_set_best_effort_timestamp(picture,
+                                               guess_correct_pts(avctx,
+                                                                 picture->pkt_pts,
+                                                                 picture->pkt_dts));
         }
     } else
         ret = 0;
@@ -1782,6 +2035,7 @@
                                               int *got_frame_ptr,
                                               const AVPacket *avpkt)
 {
+    AVCodecInternal *avci = avctx->internal;
     int planar, channels;
     int ret = 0;
 
@@ -1798,6 +2052,9 @@
 
     avcodec_get_frame_defaults(frame);
 
+    if (!avctx->refcounted_frames)
+        av_frame_unref(&avci->to_free);
+
     if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size) {
         uint8_t *side;
         int side_size;
@@ -1809,21 +2066,26 @@
         avctx->pkt = &tmp;
         ret = avctx->codec->decode(avctx, frame, got_frame_ptr, &tmp);
         if (ret >= 0 && *got_frame_ptr) {
+            add_metadata_from_side_data(avctx, frame);
             avctx->frame_number++;
             frame->pkt_dts = avpkt->dts;
-            frame->best_effort_timestamp = guess_correct_pts(avctx,
-                                                             frame->pkt_pts,
-                                                             frame->pkt_dts);
+            av_frame_set_best_effort_timestamp(frame,
+                                               guess_correct_pts(avctx,
+                                                                 frame->pkt_pts,
+                                                                 frame->pkt_dts));
             if (frame->format == AV_SAMPLE_FMT_NONE)
                 frame->format = avctx->sample_fmt;
             if (!frame->channel_layout)
                 frame->channel_layout = avctx->channel_layout;
-            if (!frame->channels)
-                frame->channels = avctx->channels;
+            if (!av_frame_get_channels(frame))
+                av_frame_set_channels(frame, avctx->channels);
             if (!frame->sample_rate)
                 frame->sample_rate = avctx->sample_rate;
+            if (!avctx->refcounted_frames) {
+                avci->to_free = *frame;
+                avci->to_free.extended_data = avci->to_free.data;
+            }
         }
-        add_metadata_from_side_data(avctx, frame);
 
         side= av_packet_get_side_data(avctx->pkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
         if(side && side_size>=10) {
@@ -1848,8 +2110,8 @@
                         frame->pkt_pts += diff_ts;
                     if(frame->pkt_dts!=AV_NOPTS_VALUE)
                         frame->pkt_dts += diff_ts;
-                    if (frame->pkt_duration >= diff_ts)
-                        frame->pkt_duration -= diff_ts;
+                    if (av_frame_get_pkt_duration(frame) >= diff_ts)
+                        av_frame_set_pkt_duration(frame, av_frame_get_pkt_duration(frame) - diff_ts);
                 } else {
                     av_log(avctx, AV_LOG_WARNING, "Could not update timestamps for skipped samples.\n");
                 }
@@ -1866,6 +2128,9 @@
             if(ret == tmp.size)
                 ret = avpkt->size;
         }
+
+        if (ret < 0 && frame->data[0])
+            av_frame_unref(frame);
     }
 
     /* many decoders assign whole AVFrames, thus overwriting extended_data;
@@ -1873,7 +2138,7 @@
      * extended_data are doing it correctly */
     if (*got_frame_ptr) {
         planar   = av_sample_fmt_is_planar(frame->format);
-        channels = frame->channels;
+        channels = av_frame_get_channels(frame);
         if (!(planar && channels > AV_NUM_DATA_POINTERS))
             frame->extended_data = frame->data;
     } else {
@@ -1900,12 +2165,7 @@
 
 #if CONFIG_ICONV
     cd = iconv_open("UTF-8", avctx->sub_charenc);
-    if (cd == (iconv_t)-1) {
-        av_log(avctx, AV_LOG_ERROR, "Unable to open iconv context "
-               "with input character encoding \"%s\"\n", avctx->sub_charenc);
-        ret = AVERROR(errno);
-        goto end;
-    }
+    av_assert0(cd != (iconv_t)-1);
 
     inb = inpkt->data;
     inl = inpkt->size;
@@ -1919,6 +2179,7 @@
     ret = av_new_packet(&tmp, inl * UTF8_MAX_BYTES);
     if (ret < 0)
         goto end;
+    outpkt->buf  = tmp.buf;
     outpkt->data = tmp.data;
     outpkt->size = tmp.size;
     outb = outpkt->data;
@@ -1934,7 +2195,7 @@
         goto end;
     }
     outpkt->size -= outl;
-    outpkt->data[outpkt->size - 1] = '\0';
+    memset(outpkt->data + outpkt->size, 0, outl);
 
 end:
     if (cd != (iconv_t)-1)
@@ -1980,8 +2241,21 @@
             ret = avctx->codec->decode(avctx, sub, got_sub_ptr, &pkt_recoded);
             av_assert1((ret >= 0) >= !!*got_sub_ptr &&
                        !!*got_sub_ptr >= !!sub->num_rects);
-            if (tmp.data != pkt_recoded.data)
-                av_free(pkt_recoded.data);
+
+            if (sub->num_rects && !sub->end_display_time && avpkt->duration &&
+                avctx->pkt_timebase.num) {
+                AVRational ms = { 1, 1000 };
+                sub->end_display_time = av_rescale_q(avpkt->duration,
+                                                     avctx->pkt_timebase, ms);
+            }
+
+            if (tmp.data != pkt_recoded.data) { // did we recode?
+                /* prevent from destroying side data from original packet */
+                pkt_recoded.side_data = NULL;
+                pkt_recoded.side_data_elems = 0;
+
+                av_free_packet(&pkt_recoded);
+            }
             sub->format = !(avctx->codec_descriptor->props & AV_CODEC_PROP_BITMAP_SUB);
             avctx->pkt = NULL;
         }
@@ -2037,7 +2311,10 @@
         return ret;
 
     if (avcodec_is_open(avctx)) {
-        if (HAVE_THREADS && avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
+        FramePool *pool = avctx->internal->pool;
+        int i;
+        if (CONFIG_FRAME_THREAD_ENCODER &&
+            avctx->internal->frame_thread_encoder && avctx->thread_count > 1) {
             ff_unlock_avcodec();
             ff_frame_thread_encoder_free(avctx);
             ff_lock_avcodec(avctx);
@@ -2046,12 +2323,15 @@
             ff_thread_free(avctx);
         if (avctx->codec && avctx->codec->close)
             avctx->codec->close(avctx);
-        avcodec_default_free_buffers(avctx);
         avctx->coded_frame = NULL;
         avctx->internal->byte_buffer_size = 0;
         av_freep(&avctx->internal->byte_buffer);
+        if (!avctx->refcounted_frames)
+            av_frame_unref(&avctx->internal->to_free);
+        for (i = 0; i < FF_ARRAY_ELEMS(pool->pools); i++)
+            av_buffer_pool_uninit(&pool->pools[i]);
+        av_freep(&avctx->internal->pool);
         av_freep(&avctx->internal);
-        av_dict_free(&avctx->metadata);
     }
 
     if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
@@ -2159,14 +2439,14 @@
 {
     int i, len, ret = 0;
 
-#define IS_PRINT(x)                                               \
+#define TAG_PRINT(x)                                              \
     (((x) >= '0' && (x) <= '9') ||                                \
      ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') ||  \
      ((x) == '.' || (x) == ' ' || (x) == '-' || (x) == '_'))
 
     for (i = 0; i < 4; i++) {
         len = snprintf(buf, buf_size,
-                       IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
+                       TAG_PRINT(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
         buf        += len;
         buf_size    = buf_size > len ? buf_size - len : 0;
         ret        += len;
@@ -2308,6 +2588,11 @@
     av_assert0(AV_CODEC_ID_SRT==94216);
     av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
 
+    av_assert0(CODEC_ID_CLLC == AV_CODEC_ID_CLLC);
+    av_assert0(CODEC_ID_PCM_S8_PLANAR == AV_CODEC_ID_PCM_S8_PLANAR);
+    av_assert0(CODEC_ID_ADPCM_IMA_APC == AV_CODEC_ID_ADPCM_IMA_APC);
+    av_assert0(CODEC_ID_ILBC == AV_CODEC_ID_ILBC);
+    av_assert0(CODEC_ID_SRT == AV_CODEC_ID_SRT);
     return LIBAVCODEC_VERSION_INT;
 }
 
@@ -2333,49 +2618,6 @@
     avctx->pts_correction_last_dts = INT64_MIN;
 }
 
-static void video_free_buffers(AVCodecContext *s)
-{
-    AVCodecInternal *avci = s->internal;
-    int i, j;
-
-    if (!avci->buffer)
-        return;
-
-    if (avci->buffer_count)
-        av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n",
-               avci->buffer_count);
-    for (i = 0; i < INTERNAL_BUFFER_SIZE; i++) {
-        InternalBuffer *buf = &avci->buffer[i];
-        for (j = 0; j < 4; j++) {
-            av_freep(&buf->base[j]);
-            buf->data[j] = NULL;
-        }
-    }
-    av_freep(&avci->buffer);
-
-    avci->buffer_count = 0;
-}
-
-static void audio_free_buffers(AVCodecContext *avctx)
-{
-    AVCodecInternal *avci = avctx->internal;
-    av_freep(&avci->audio_data);
-}
-
-void avcodec_default_free_buffers(AVCodecContext *avctx)
-{
-    switch (avctx->codec_type) {
-    case AVMEDIA_TYPE_VIDEO:
-        video_free_buffers(avctx);
-        break;
-    case AVMEDIA_TYPE_AUDIO:
-        audio_free_buffers(avctx);
-        break;
-    default:
-        break;
-    }
-}
-
 int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
 {
     switch (codec_id) {
@@ -2653,6 +2895,7 @@
     return i;
 }
 
+#if FF_API_MISSING_SAMPLE
 void av_log_missing_feature(void *avc, const char *feature, int want_sample)
 {
     av_log(avc, AV_LOG_WARNING, "%s is not implemented. Update your FFmpeg "
@@ -2677,6 +2920,7 @@
 
     va_end(argument_list);
 }
+#endif /* FF_API_MISSING_SAMPLE */
 
 static AVHWAccel *first_hwaccel = NULL;
 
@@ -2775,35 +3019,58 @@
 
 unsigned int avpriv_toupper4(unsigned int x)
 {
-    return toupper(x & 0xFF)
-           + (toupper((x >> 8) & 0xFF) << 8)
-           + (toupper((x >> 16) & 0xFF) << 16)
-           + (toupper((x >> 24) & 0xFF) << 24);
+    return av_toupper(x & 0xFF) +
+          (av_toupper((x >>  8) & 0xFF) << 8)  +
+          (av_toupper((x >> 16) & 0xFF) << 16) +
+          (av_toupper((x >> 24) & 0xFF) << 24);
+}
+
+int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
+{
+    int ret;
+
+    dst->owner = src->owner;
+
+    ret = av_frame_ref(dst->f, src->f);
+    if (ret < 0)
+        return ret;
+
+    if (src->progress &&
+        !(dst->progress = av_buffer_ref(src->progress))) {
+        ff_thread_release_buffer(dst->owner, dst);
+        return AVERROR(ENOMEM);
+    }
+
+    return 0;
 }
 
 #if !HAVE_THREADS
 
-int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
+enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
 {
-    f->owner = avctx;
-
-    return ff_get_buffer(avctx, f);
+    return avctx->get_format(avctx, fmt);
 }
 
-void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
+int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
 {
-    f->owner->release_buffer(f->owner, f);
+    f->owner = avctx;
+    return ff_get_buffer(avctx, f->f, flags);
+}
+
+void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
+{
+    av_frame_unref(f->f);
 }
 
 void ff_thread_finish_setup(AVCodecContext *avctx)
 {
 }
 
-void ff_thread_report_progress(AVFrame *f, int progress, int field)
+void ff_thread_report_progress(ThreadFrame *f, int progress, int field)
 {
 }
 
-void ff_thread_await_progress(AVFrame *f, int progress, int field)
+void ff_thread_await_progress(ThreadFrame *f, int progress, int field)
 {
 }
 
@@ -2856,3 +3123,36 @@
     avctx->extradata_size = buf->len;
     return 0;
 }
+
+const uint8_t *avpriv_find_start_code(const uint8_t *av_restrict p,
+                                      const uint8_t *end,
+                                      uint32_t *av_restrict state)
+{
+    int i;
+
+    assert(p <= end);
+    if (p >= end)
+        return end;
+
+    for (i = 0; i < 3; i++) {
+        uint32_t tmp = *state << 8;
+        *state = tmp + *(p++);
+        if (tmp == 0x100 || p == end)
+            return p;
+    }
+
+    while (p < end) {
+        if      (p[-1] > 1      ) p += 3;
+        else if (p[-2]          ) p += 2;
+        else if (p[-3]|(p[-1]-1)) p++;
+        else {
+            p++;
+            break;
+        }
+    }
+
+    p = FFMIN(p, end) - 4;
+    *state = AV_RB32(p);
+
+    return p + 4;
+}
diff --git a/libavcodec/utvideo.h b/libavcodec/utvideo.h
index 266291c..c00179e 100644
--- a/libavcodec/utvideo.h
+++ b/libavcodec/utvideo.h
@@ -65,7 +65,6 @@
 
 typedef struct UtvideoContext {
     AVCodecContext *avctx;
-    AVFrame        pic;
     DSPContext     dsp;
 
     uint32_t frame_info_size, flags, frame_info;
diff --git a/libavcodec/utvideodec.c b/libavcodec/utvideodec.c
index 259030a..4efad1e 100644
--- a/libavcodec/utvideodec.c
+++ b/libavcodec/utvideodec.c
@@ -331,16 +331,10 @@
     int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
     int ret;
     GetByteContext gb;
+    ThreadFrame frame = { .f = data };
 
-    if (c->pic.data[0])
-        ff_thread_release_buffer(avctx, &c->pic);
-
-    c->pic.reference = 3;
-    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
-    if ((ret = ff_thread_get_buffer(avctx, &c->pic)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
         return ret;
-    }
 
     /* parse plane structure to get frame flags and validate slice offsets */
     bytestream2_init(&gb, buf, buf_size);
@@ -378,7 +372,7 @@
     c->frame_pred = (c->frame_info >> 8) & 3;
 
     if (c->frame_pred == PRED_GRADIENT) {
-        av_log_ask_for_sample(avctx, "Frame uses gradient prediction\n");
+        avpriv_request_sample(avctx, "Frame with gradient prediction");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -394,42 +388,42 @@
     case AV_PIX_FMT_RGB24:
     case AV_PIX_FMT_RGBA:
         for (i = 0; i < c->planes; i++) {
-            ret = decode_plane(c, i, c->pic.data[0] + ff_ut_rgb_order[i],
-                               c->planes, c->pic.linesize[0], avctx->width,
+            ret = decode_plane(c, i, frame.f->data[0] + ff_ut_rgb_order[i],
+                               c->planes, frame.f->linesize[0], avctx->width,
                                avctx->height, plane_start[i],
                                c->frame_pred == PRED_LEFT);
             if (ret)
                 return ret;
             if (c->frame_pred == PRED_MEDIAN) {
                 if (!c->interlaced) {
-                    restore_median(c->pic.data[0] + ff_ut_rgb_order[i],
-                                   c->planes, c->pic.linesize[0], avctx->width,
+                    restore_median(frame.f->data[0] + ff_ut_rgb_order[i],
+                                   c->planes, frame.f->linesize[0], avctx->width,
                                    avctx->height, c->slices, 0);
                 } else {
-                    restore_median_il(c->pic.data[0] + ff_ut_rgb_order[i],
-                                      c->planes, c->pic.linesize[0],
+                    restore_median_il(frame.f->data[0] + ff_ut_rgb_order[i],
+                                      c->planes, frame.f->linesize[0],
                                       avctx->width, avctx->height, c->slices,
                                       0);
                 }
             }
         }
-        restore_rgb_planes(c->pic.data[0], c->planes, c->pic.linesize[0],
+        restore_rgb_planes(frame.f->data[0], c->planes, frame.f->linesize[0],
                            avctx->width, avctx->height);
         break;
     case AV_PIX_FMT_YUV420P:
         for (i = 0; i < 3; i++) {
-            ret = decode_plane(c, i, c->pic.data[i], 1, c->pic.linesize[i],
+            ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
                                avctx->width >> !!i, avctx->height >> !!i,
                                plane_start[i], c->frame_pred == PRED_LEFT);
             if (ret)
                 return ret;
             if (c->frame_pred == PRED_MEDIAN) {
                 if (!c->interlaced) {
-                    restore_median(c->pic.data[i], 1, c->pic.linesize[i],
+                    restore_median(frame.f->data[i], 1, frame.f->linesize[i],
                                    avctx->width >> !!i, avctx->height >> !!i,
                                    c->slices, !i);
                 } else {
-                    restore_median_il(c->pic.data[i], 1, c->pic.linesize[i],
+                    restore_median_il(frame.f->data[i], 1, frame.f->linesize[i],
                                       avctx->width  >> !!i,
                                       avctx->height >> !!i,
                                       c->slices, !i);
@@ -439,18 +433,18 @@
         break;
     case AV_PIX_FMT_YUV422P:
         for (i = 0; i < 3; i++) {
-            ret = decode_plane(c, i, c->pic.data[i], 1, c->pic.linesize[i],
+            ret = decode_plane(c, i, frame.f->data[i], 1, frame.f->linesize[i],
                                avctx->width >> !!i, avctx->height,
                                plane_start[i], c->frame_pred == PRED_LEFT);
             if (ret)
                 return ret;
             if (c->frame_pred == PRED_MEDIAN) {
                 if (!c->interlaced) {
-                    restore_median(c->pic.data[i], 1, c->pic.linesize[i],
+                    restore_median(frame.f->data[i], 1, frame.f->linesize[i],
                                    avctx->width >> !!i, avctx->height,
                                    c->slices, 0);
                 } else {
-                    restore_median_il(c->pic.data[i], 1, c->pic.linesize[i],
+                    restore_median_il(frame.f->data[i], 1, frame.f->linesize[i],
                                       avctx->width >> !!i, avctx->height,
                                       c->slices, 0);
                 }
@@ -459,12 +453,11 @@
         break;
     }
 
-    c->pic.key_frame = 1;
-    c->pic.pict_type = AV_PICTURE_TYPE_I;
-    c->pic.interlaced_frame = !!c->interlaced;
+    frame.f->key_frame = 1;
+    frame.f->pict_type = AV_PICTURE_TYPE_I;
+    frame.f->interlaced_frame = !!c->interlaced;
 
-    *got_frame      = 1;
-    *(AVFrame*)data = c->pic;
+    *got_frame = 1;
 
     /* always report that the buffer was completely consumed */
     return buf_size;
@@ -494,7 +487,7 @@
     c->flags           = AV_RL32(avctx->extradata + 12);
 
     if (c->frame_info_size != 4)
-        av_log_ask_for_sample(avctx, "Frame info is not 4 bytes\n");
+        avpriv_request_sample(avctx, "Frame info not 4 bytes");
     av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08X\n", c->flags);
     c->slices      = (c->flags >> 24) + 1;
     c->compression = c->flags & 1;
@@ -532,9 +525,6 @@
 {
     UtvideoContext * const c = avctx->priv_data;
 
-    if (c->pic.data[0])
-        ff_thread_release_buffer(avctx, &c->pic);
-
     av_freep(&c->slice_bits);
 
     return 0;
diff --git a/libavcodec/v210dec.c b/libavcodec/v210dec.c
index 132b42a..42e25cc 100644
--- a/libavcodec/v210dec.c
+++ b/libavcodec/v210dec.c
@@ -60,10 +60,6 @@
     avctx->pix_fmt             = AV_PIX_FMT_YUV422P10;
     avctx->bits_per_raw_sample = 10;
 
-    avctx->coded_frame         = avcodec_alloc_frame();
-    if (!avctx->coded_frame)
-        return AVERROR(ENOMEM);
-
     s->unpack_frame            = v210_planar_unpack_c;
 
     if (HAVE_MMX)
@@ -78,7 +74,7 @@
     V210DecContext *s = avctx->priv_data;
 
     int h, w, ret, stride, aligned_input;
-    AVFrame *pic = avctx->coded_frame;
+    AVFrame *pic = data;
     const uint8_t *psrc = avpkt->data;
     uint16_t *y, *u, *v;
 
@@ -108,11 +104,7 @@
             v210_x86_init(s);
     }
 
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
-
-    pic->reference = 0;
-    if ((ret = ff_get_buffer(avctx, pic)) < 0)
+    if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
         return ret;
 
     y = (uint16_t*)pic->data[0];
@@ -155,21 +147,10 @@
     }
 
     *got_frame      = 1;
-    *(AVFrame*)data = *avctx->coded_frame;
 
     return avpkt->size;
 }
 
-static av_cold int decode_close(AVCodecContext *avctx)
-{
-    AVFrame *pic = avctx->coded_frame;
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
-    av_freep(&avctx->coded_frame);
-
-    return 0;
-}
-
 #define V210DEC_FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
 static const AVOption v210dec_options[] = {
     {"custom_stride", "Custom V210 stride", offsetof(V210DecContext, custom_stride), FF_OPT_TYPE_INT,
@@ -190,7 +171,6 @@
     .id             = AV_CODEC_ID_V210,
     .priv_data_size = sizeof(V210DecContext),
     .init           = decode_init,
-    .close          = decode_close,
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
diff --git a/libavcodec/v210x.c b/libavcodec/v210x.c
index cb9fc6f..e4d5460 100644
--- a/libavcodec/v210x.c
+++ b/libavcodec/v210x.c
@@ -33,10 +33,6 @@
     avctx->pix_fmt             = AV_PIX_FMT_YUV422P16;
     avctx->bits_per_raw_sample = 10;
 
-    avctx->coded_frame= avcodec_alloc_frame();
-    if (!avctx->coded_frame)
-        return AVERROR(ENOMEM);
-
     return 0;
 }
 
@@ -44,26 +40,22 @@
                         AVPacket *avpkt)
 {
     const uint32_t *src = (const uint32_t *)avpkt->data;
-    AVFrame *pic        = avctx->coded_frame;
+    AVFrame *pic        = data;
     int width           = avctx->width;
     int y               = 0;
     uint16_t *ydst, *udst, *vdst, *yend;
     int ret;
 
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
-
     if (avpkt->size < avctx->width * avctx->height * 8 / 3) {
         av_log(avctx, AV_LOG_ERROR, "Packet too small\n");
         return AVERROR_INVALIDDATA;
     }
 
     if (avpkt->size > avctx->width * avctx->height * 8 / 3) {
-        av_log_ask_for_sample(avctx, "Probably padded data\n");
+        avpriv_request_sample(avctx, "(Probably) padded data");
     }
 
-    pic->reference = 0;
-    if ((ret = ff_get_buffer(avctx, pic)) < 0)
+    if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
         return ret;
 
     ydst = (uint16_t *)pic->data[0];
@@ -124,27 +116,15 @@
     }
 
     *got_frame = 1;
-    *(AVFrame*)data= *avctx->coded_frame;
 
     return avpkt->size;
 }
 
-static av_cold int decode_close(AVCodecContext *avctx)
-{
-    AVFrame *pic = avctx->coded_frame;
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
-    av_freep(&avctx->coded_frame);
-
-    return 0;
-}
-
 AVCodec ff_v210x_decoder = {
     .name           = "v210x",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_V210X,
     .init           = decode_init,
-    .close          = decode_close,
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
diff --git a/libavcodec/v308dec.c b/libavcodec/v308dec.c
index b5567f8..75d65e8 100644
--- a/libavcodec/v308dec.c
+++ b/libavcodec/v308dec.c
@@ -29,38 +29,24 @@
     if (avctx->width & 1)
         av_log(avctx, AV_LOG_WARNING, "v308 requires width to be even.\n");
 
-    avctx->coded_frame = avcodec_alloc_frame();
-
-    if (!avctx->coded_frame) {
-        av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
-        return AVERROR(ENOMEM);
-    }
-
     return 0;
 }
 
 static int v308_decode_frame(AVCodecContext *avctx, void *data,
                              int *got_frame, AVPacket *avpkt)
 {
-    AVFrame *pic = avctx->coded_frame;
+    AVFrame *pic = data;
     const uint8_t *src = avpkt->data;
     uint8_t *y, *u, *v;
-    int i, j;
-
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
+    int i, j, ret;
 
     if (avpkt->size < 3 * avctx->height * avctx->width) {
         av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
         return AVERROR(EINVAL);
     }
 
-    pic->reference = 0;
-
-    if (ff_get_buffer(avctx, pic) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
-        return AVERROR(ENOMEM);
-    }
+    if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
+        return ret;
 
     pic->key_frame = 1;
     pic->pict_type = AV_PICTURE_TYPE_I;
@@ -82,28 +68,16 @@
     }
 
     *got_frame = 1;
-    *(AVFrame *)data = *pic;
 
     return avpkt->size;
 }
 
-static av_cold int v308_decode_close(AVCodecContext *avctx)
-{
-    if (avctx->coded_frame->data[0])
-        avctx->release_buffer(avctx, avctx->coded_frame);
-
-    av_freep(&avctx->coded_frame);
-
-    return 0;
-}
-
 AVCodec ff_v308_decoder = {
     .name         = "v308",
     .type         = AVMEDIA_TYPE_VIDEO,
     .id           = AV_CODEC_ID_V308,
     .init         = v308_decode_init,
     .decode       = v308_decode_frame,
-    .close        = v308_decode_close,
     .capabilities = CODEC_CAP_DR1,
     .long_name    = NULL_IF_CONFIG_SMALL("Uncompressed packed 4:4:4"),
 };
diff --git a/libavcodec/v308enc.c b/libavcodec/v308enc.c
index 7a0ca40..10437cc 100644
--- a/libavcodec/v308enc.c
+++ b/libavcodec/v308enc.c
@@ -52,7 +52,6 @@
         return ret;
     dst = pkt->data;
 
-    avctx->coded_frame->reference = 0;
     avctx->coded_frame->key_frame = 1;
     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
 
diff --git a/libavcodec/v408dec.c b/libavcodec/v408dec.c
index 470853d..751650d 100644
--- a/libavcodec/v408dec.c
+++ b/libavcodec/v408dec.c
@@ -26,38 +26,24 @@
 {
     avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
 
-    avctx->coded_frame = avcodec_alloc_frame();
-
-    if (!avctx->coded_frame) {
-        av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
-        return AVERROR(ENOMEM);
-    }
-
     return 0;
 }
 
 static int v408_decode_frame(AVCodecContext *avctx, void *data,
                              int *got_frame, AVPacket *avpkt)
 {
-    AVFrame *pic = avctx->coded_frame;
+    AVFrame *pic = data;
     const uint8_t *src = avpkt->data;
     uint8_t *y, *u, *v, *a;
-    int i, j;
-
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
+    int i, j, ret;
 
     if (avpkt->size < 4 * avctx->height * avctx->width) {
         av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
         return AVERROR(EINVAL);
     }
 
-    pic->reference = 0;
-
-    if (ff_get_buffer(avctx, pic) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
-        return AVERROR(ENOMEM);
-    }
+    if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
+        return ret;
 
     pic->key_frame = 1;
     pic->pict_type = AV_PICTURE_TYPE_I;
@@ -89,21 +75,10 @@
     }
 
     *got_frame = 1;
-    *(AVFrame *)data = *pic;
 
     return avpkt->size;
 }
 
-static av_cold int v408_decode_close(AVCodecContext *avctx)
-{
-    if (avctx->coded_frame->data[0])
-        avctx->release_buffer(avctx, avctx->coded_frame);
-
-    av_freep(&avctx->coded_frame);
-
-    return 0;
-}
-
 #if CONFIG_AYUV_DECODER
 AVCodec ff_ayuv_decoder = {
     .name         = "ayuv",
@@ -111,7 +86,6 @@
     .id           = AV_CODEC_ID_AYUV,
     .init         = v408_decode_init,
     .decode       = v408_decode_frame,
-    .close        = v408_decode_close,
     .capabilities = CODEC_CAP_DR1,
     .long_name    = NULL_IF_CONFIG_SMALL("Uncompressed packed MS 4:4:4:4"),
 };
@@ -123,7 +97,6 @@
     .id           = AV_CODEC_ID_V408,
     .init         = v408_decode_init,
     .decode       = v408_decode_frame,
-    .close        = v408_decode_close,
     .capabilities = CODEC_CAP_DR1,
     .long_name    = NULL_IF_CONFIG_SMALL("Uncompressed packed QT 4:4:4:4"),
 };
diff --git a/libavcodec/v408enc.c b/libavcodec/v408enc.c
index 9b0ebce..694bdbf 100644
--- a/libavcodec/v408enc.c
+++ b/libavcodec/v408enc.c
@@ -47,7 +47,6 @@
         return ret;
     dst = pkt->data;
 
-    avctx->coded_frame->reference = 0;
     avctx->coded_frame->key_frame = 1;
     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
 
diff --git a/libavcodec/v410dec.c b/libavcodec/v410dec.c
index ff23817..f8229e2 100644
--- a/libavcodec/v410dec.c
+++ b/libavcodec/v410dec.c
@@ -39,39 +39,25 @@
         }
     }
 
-    avctx->coded_frame = avcodec_alloc_frame();
-
-    if (!avctx->coded_frame) {
-        av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
-        return AVERROR(ENOMEM);
-    }
-
     return 0;
 }
 
 static int v410_decode_frame(AVCodecContext *avctx, void *data,
                              int *got_frame, AVPacket *avpkt)
 {
-    AVFrame *pic = avctx->coded_frame;
+    AVFrame *pic = data;
     uint8_t *src = avpkt->data;
     uint16_t *y, *u, *v;
     uint32_t val;
-    int i, j;
-
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
+    int i, j, ret;
 
     if (avpkt->size < 4 * avctx->height * avctx->width) {
         av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
         return AVERROR(EINVAL);
     }
 
-    pic->reference = 0;
-
-    if (ff_get_buffer(avctx, pic) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
-        return AVERROR(ENOMEM);
-    }
+    if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
+        return ret;
 
     pic->key_frame = 1;
     pic->pict_type = AV_PICTURE_TYPE_I;
@@ -97,28 +83,16 @@
     }
 
     *got_frame = 1;
-    *(AVFrame *)data = *pic;
 
     return avpkt->size;
 }
 
-static av_cold int v410_decode_close(AVCodecContext *avctx)
-{
-    if (avctx->coded_frame->data[0])
-        avctx->release_buffer(avctx, avctx->coded_frame);
-
-    av_freep(&avctx->coded_frame);
-
-    return 0;
-}
-
 AVCodec ff_v410_decoder = {
     .name         = "v410",
     .type         = AVMEDIA_TYPE_VIDEO,
     .id           = AV_CODEC_ID_V410,
     .init         = v410_decode_init,
     .decode       = v410_decode_frame,
-    .close        = v410_decode_close,
     .capabilities = CODEC_CAP_DR1,
     .long_name    = NULL_IF_CONFIG_SMALL("Uncompressed 4:4:4 10-bit"),
 };
diff --git a/libavcodec/vaapi.c b/libavcodec/vaapi.c
index fc869bd..a220a9d 100644
--- a/libavcodec/vaapi.c
+++ b/libavcodec/vaapi.c
@@ -212,7 +212,7 @@
     ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
 
 finish:
-    ff_vaapi_common_end_frame(avctx->priv_data);
+    ff_vaapi_common_end_frame(avctx);
     return ret;
 }
 
diff --git a/libavcodec/vaapi_h264.c b/libavcodec/vaapi_h264.c
index 78ae315..ea0f434 100644
--- a/libavcodec/vaapi_h264.c
+++ b/libavcodec/vaapi_h264.c
@@ -55,7 +55,7 @@
                            int            pic_structure)
 {
     if (pic_structure == 0)
-        pic_structure = pic->f.reference;
+        pic_structure = pic->reference;
     pic_structure &= PICT_FRAME; /* PICT_TOP_FIELD|PICT_BOTTOM_FIELD */
 
     va_pic->picture_id = ff_vaapi_get_surface_id(pic);
@@ -64,7 +64,7 @@
     va_pic->flags      = 0;
     if (pic_structure != PICT_FRAME)
         va_pic->flags |= (pic_structure & PICT_TOP_FIELD) ? VA_PICTURE_H264_TOP_FIELD : VA_PICTURE_H264_BOTTOM_FIELD;
-    if (pic->f.reference)
+    if (pic->reference)
         va_pic->flags |= pic->long_ref ? VA_PICTURE_H264_LONG_TERM_REFERENCE : VA_PICTURE_H264_SHORT_TERM_REFERENCE;
 
     va_pic->TopFieldOrderCnt = 0;
@@ -134,13 +134,13 @@
 
     for (i = 0; i < h->short_ref_count; i++) {
         Picture * const pic = h->short_ref[i];
-        if (pic && pic->f.reference && dpb_add(&dpb, pic) < 0)
+        if (pic && pic->reference && dpb_add(&dpb, pic) < 0)
             return -1;
     }
 
     for (i = 0; i < 16; i++) {
         Picture * const pic = h->long_ref[i];
-        if (pic && pic->f.reference && dpb_add(&dpb, pic) < 0)
+        if (pic && pic->reference && dpb_add(&dpb, pic) < 0)
             return -1;
     }
     return 0;
@@ -160,7 +160,7 @@
 {
     unsigned int i, n = 0;
     for (i = 0; i < ref_count; i++)
-        if (ref_list[i].f.reference)
+        if (ref_list[i].reference)
             fill_vaapi_pic(&RefPicList[n++], &ref_list[i], 0);
 
     for (; n < 32; n++)
@@ -219,16 +219,16 @@
 }
 
 /** Initialize and start decoding a frame with VA API. */
-static int start_frame(AVCodecContext          *avctx,
-                       av_unused const uint8_t *buffer,
-                       av_unused uint32_t       size)
+static int vaapi_h264_start_frame(AVCodecContext          *avctx,
+                                  av_unused const uint8_t *buffer,
+                                  av_unused uint32_t       size)
 {
     H264Context * const h = avctx->priv_data;
     struct vaapi_context * const vactx = avctx->hwaccel_context;
     VAPictureParameterBufferH264 *pic_param;
     VAIQMatrixBufferH264 *iq_matrix;
 
-    av_dlog(avctx, "start_frame()\n");
+    av_dlog(avctx, "vaapi_h264_start_frame()\n");
 
     vactx->slice_param_size = sizeof(VASliceParameterBufferH264);
 
@@ -287,13 +287,13 @@
 }
 
 /** End a hardware decoding based frame. */
-static int end_frame(AVCodecContext *avctx)
+static int vaapi_h264_end_frame(AVCodecContext *avctx)
 {
     struct vaapi_context * const vactx = avctx->hwaccel_context;
     H264Context * const h = avctx->priv_data;
     int ret;
 
-    av_dlog(avctx, "end_frame()\n");
+    av_dlog(avctx, "vaapi_h264_end_frame()\n");
     ret = ff_vaapi_commit_slices(vactx);
     if (ret < 0)
         goto finish;
@@ -310,21 +310,22 @@
 }
 
 /** Decode the given H.264 slice with VA API. */
-static int decode_slice(AVCodecContext *avctx,
-                        const uint8_t  *buffer,
-                        uint32_t        size)
+static int vaapi_h264_decode_slice(AVCodecContext *avctx,
+                                   const uint8_t  *buffer,
+                                   uint32_t        size)
 {
     H264Context * const h = avctx->priv_data;
     VASliceParameterBufferH264 *slice_param;
 
-    av_dlog(avctx, "decode_slice(): buffer %p, size %d\n", buffer, size);
+    av_dlog(avctx, "vaapi_h264_decode_slice(): buffer %p, size %d\n",
+            buffer, size);
 
     /* Fill in VASliceParameterBufferH264. */
     slice_param = (VASliceParameterBufferH264 *)ff_vaapi_alloc_slice(avctx->hwaccel_context, buffer, size);
     if (!slice_param)
         return -1;
     slice_param->slice_data_bit_offset          = get_bits_count(&h->gb) + 8; /* bit buffer started beyond nal_unit_type */
-    slice_param->first_mb_in_slice              = (h->mb_y >> FIELD_OR_MBAFF_PICTURE) * h->mb_width + h->mb_x;
+    slice_param->first_mb_in_slice              = (h->mb_y >> FIELD_OR_MBAFF_PICTURE(h)) * h->mb_width + h->mb_x;
     slice_param->slice_type                     = ff_h264_get_slice_type(h);
     slice_param->direct_spatial_mv_pred_flag    = h->slice_type == AV_PICTURE_TYPE_B ? h->direct_spatial_mv_pred : 0;
     slice_param->num_ref_idx_l0_active_minus1   = h->list_count > 0 ? h->ref_count[0] - 1 : 0;
@@ -354,7 +355,7 @@
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_H264,
     .pix_fmt        = AV_PIX_FMT_VAAPI_VLD,
-    .start_frame    = start_frame,
-    .end_frame      = end_frame,
-    .decode_slice   = decode_slice,
+    .start_frame    = vaapi_h264_start_frame,
+    .end_frame      = vaapi_h264_end_frame,
+    .decode_slice   = vaapi_h264_decode_slice,
 };
diff --git a/libavcodec/vb.c b/libavcodec/vb.c
index 3b5a83b..29cd104 100644
--- a/libavcodec/vb.c
+++ b/libavcodec/vb.c
@@ -41,7 +41,6 @@
 
 typedef struct VBDecContext {
     AVCodecContext *avctx;
-    AVFrame pic;
 
     uint8_t *frame, *prev_frame;
     uint32_t pal[AVPALETTE_COUNT];
@@ -189,6 +188,7 @@
                         AVPacket *avpkt)
 {
     VBDecContext * const c = avctx->priv_data;
+    AVFrame *frame         = data;
     uint8_t *outptr, *srcptr;
     int i, j, ret;
     int flags;
@@ -197,13 +197,8 @@
 
     bytestream2_init(&c->stream, avpkt->data, avpkt->size);
 
-    if (c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
-    c->pic.reference = 3;
-    if ((ret = ff_get_buffer(avctx, &c->pic)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     flags = bytestream2_get_le16(&c->stream);
 
@@ -226,22 +221,21 @@
         vb_decode_palette(c, size);
     }
 
-    memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
-    c->pic.palette_has_changed = flags & VB_HAS_PALETTE;
+    memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
+    frame->palette_has_changed = flags & VB_HAS_PALETTE;
 
-    outptr = c->pic.data[0];
+    outptr = frame->data[0];
     srcptr = c->frame;
 
     for (i = 0; i < avctx->height; i++) {
         memcpy(outptr, srcptr, avctx->width);
         srcptr += avctx->width;
-        outptr += c->pic.linesize[0];
+        outptr += frame->linesize[0];
     }
 
     FFSWAP(uint8_t*, c->frame, c->prev_frame);
 
     *got_frame = 1;
-    *(AVFrame*)data = c->pic;
 
     /* always report that the buffer was completely consumed */
     return avpkt->size;
@@ -253,7 +247,6 @@
 
     c->avctx       = avctx;
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
-    avcodec_get_frame_defaults(&c->pic);
 
     c->frame      = av_mallocz(avctx->width * avctx->height);
     c->prev_frame = av_mallocz(avctx->width * avctx->height);
@@ -267,8 +260,6 @@
 
     av_freep(&c->frame);
     av_freep(&c->prev_frame);
-    if(c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
 
     return 0;
 }
diff --git a/libavcodec/vble.c b/libavcodec/vble.c
index c8037bd..cbb2ffc 100644
--- a/libavcodec/vble.c
+++ b/libavcodec/vble.c
@@ -80,10 +80,10 @@
     return 0;
 }
 
-static void vble_restore_plane(VBLEContext *ctx, GetBitContext *gb, int plane,
+static void vble_restore_plane(VBLEContext *ctx, AVFrame *pic,
+                               GetBitContext *gb, int plane,
                                int offset, int width, int height)
 {
-    AVFrame *pic = ctx->avctx->coded_frame;
     uint8_t *dst = pic->data[plane];
     uint8_t *val = ctx->val + offset;
     int stride = pic->linesize[plane];
@@ -115,18 +115,13 @@
                              AVPacket *avpkt)
 {
     VBLEContext *ctx = avctx->priv_data;
-    AVFrame *pic = avctx->coded_frame;
+    AVFrame *pic     = data;
     GetBitContext gb;
     const uint8_t *src = avpkt->data;
     int version;
     int offset = 0;
     int width_uv = avctx->width / 2, height_uv = avctx->height / 2;
-
-    pic->reference = 0;
-
-    /* Clear buffer if need be */
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
+    int ret;
 
     if (avpkt->size < 4 || avpkt->size - 4 > INT_MAX/8) {
         av_log(avctx, AV_LOG_ERROR, "Invalid packet size\n");
@@ -134,10 +129,8 @@
     }
 
     /* Allocate buffer */
-    if (ff_get_buffer(avctx, pic) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
-        return AVERROR(ENOMEM);
-    }
+    if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
+        return ret;
 
     /* Set flags */
     pic->key_frame = 1;
@@ -158,19 +151,18 @@
     }
 
     /* Restore planes. Should be almost identical to Huffyuv's. */
-    vble_restore_plane(ctx, &gb, 0, offset, avctx->width, avctx->height);
+    vble_restore_plane(ctx, pic, &gb, 0, offset, avctx->width, avctx->height);
 
     /* Chroma */
     if (!(ctx->avctx->flags & CODEC_FLAG_GRAY)) {
         offset += avctx->width * avctx->height;
-        vble_restore_plane(ctx, &gb, 1, offset, width_uv, height_uv);
+        vble_restore_plane(ctx, pic, &gb, 1, offset, width_uv, height_uv);
 
         offset += width_uv * height_uv;
-        vble_restore_plane(ctx, &gb, 2, offset, width_uv, height_uv);
+        vble_restore_plane(ctx, pic, &gb, 2, offset, width_uv, height_uv);
     }
 
     *got_frame       = 1;
-    *(AVFrame *)data = *pic;
 
     return avpkt->size;
 }
@@ -178,12 +170,6 @@
 static av_cold int vble_decode_close(AVCodecContext *avctx)
 {
     VBLEContext *ctx = avctx->priv_data;
-    AVFrame *pic = avctx->coded_frame;
-
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
-
-    av_freep(&avctx->coded_frame);
     av_freep(&ctx->val);
 
     return 0;
@@ -199,12 +185,6 @@
 
     avctx->pix_fmt = AV_PIX_FMT_YUV420P;
     avctx->bits_per_raw_sample = 8;
-    avctx->coded_frame = avcodec_alloc_frame();
-
-    if (!avctx->coded_frame) {
-        av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
-        return AVERROR(ENOMEM);
-    }
 
     ctx->size = avpicture_get_size(avctx->pix_fmt,
                                    avctx->width, avctx->height);
diff --git a/libavcodec/vc1.c b/libavcodec/vc1.c
index a6a7bac..208fef3 100644
--- a/libavcodec/vc1.c
+++ b/libavcodec/vc1.c
@@ -576,6 +576,49 @@
     return 0;
 }
 
+static void rotate_luts(VC1Context *v)
+{
+#define ROTATE(DEF, L, N, C, A) do {\
+        if (v->s.pict_type == AV_PICTURE_TYPE_BI || v->s.pict_type == AV_PICTURE_TYPE_B) {\
+            C = A;\
+        } else {\
+            DEF;\
+            memcpy(&tmp, &L  , sizeof(tmp));\
+            memcpy(&L  , &N  , sizeof(tmp));\
+            memcpy(&N  , &tmp, sizeof(tmp));\
+            C = N;\
+        }\
+    }while(0)
+
+        ROTATE(int tmp            , v->last_use_ic, v->next_use_ic, v->curr_use_ic, v->aux_use_ic);
+        ROTATE(uint8_t tmp[2][256], v->last_luty , v->next_luty , v->curr_luty , v->aux_luty);
+        ROTATE(uint8_t tmp[2][256], v->last_lutuv, v->next_lutuv, v->curr_lutuv, v->aux_lutuv);
+}
+
+/* fill lookup tables for intensity compensation */
+#define INIT_LUT(lumscale, lumshift, luty, lutuv, chain)   do {\
+    int scale, shift, i;                            \
+    if (!lumscale) {                                \
+        scale = -64;                                \
+        shift = (255 - lumshift * 2) << 6;          \
+        if (lumshift > 31)                          \
+            shift += 128 << 6;                      \
+    } else {                                        \
+        scale = lumscale + 32;                      \
+        if (lumshift > 31)                          \
+            shift = (lumshift - 64) << 6;           \
+        else                                        \
+            shift = lumshift << 6;                  \
+    }                                               \
+    for (i = 0; i < 256; i++) {                     \
+        int iy = chain ? luty[i] : i;               \
+        int iu = chain ? lutuv[i] : i;              \
+        luty[i]  = av_clip_uint8((scale * iy + shift + 32) >> 6);           \
+        lutuv[i] = av_clip_uint8((scale * (iu - 128) + 128*64 + 32) >> 6);  \
+    } \
+    }while(0)
+
+
 int ff_vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
 {
     int pqindex, lowquant, status;
@@ -664,8 +707,12 @@
             (v->s.pict_type == AV_PICTURE_TYPE_P) ? 'P' : ((v->s.pict_type == AV_PICTURE_TYPE_I) ? 'I' : 'B'),
             pqindex, v->pq, v->halfpq, v->rangeredfrm);
 
-    if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_P)
-        v->use_ic = 0;
+    if(v->first_pic_header_flag) {
+        rotate_luts(v);
+        INIT_LUT(32, 0 , v->curr_luty[0] , v->curr_lutuv[0] , 0);
+        INIT_LUT(32, 0 , v->curr_luty[1] , v->curr_lutuv[1] , 0);
+        v->curr_use_ic = 0;
+    }
 
     switch (v->s.pict_type) {
     case AV_PICTURE_TYPE_P:
@@ -676,28 +723,13 @@
         lowquant = (v->pq > 12) ? 0 : 1;
         v->mv_mode = ff_vc1_mv_pmode_table[lowquant][get_unary(gb, 1, 4)];
         if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
-            int scale, shift, i;
             v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][get_unary(gb, 1, 3)];
             v->lumscale = get_bits(gb, 6);
             v->lumshift = get_bits(gb, 6);
-            v->use_ic   = 1;
+            v->last_use_ic   = 1;
             /* fill lookup tables for intensity compensation */
-            if (!v->lumscale) {
-                scale = -64;
-                shift = (255 - v->lumshift * 2) << 6;
-                if (v->lumshift > 31)
-                    shift += 128 << 6;
-            } else {
-                scale = v->lumscale + 32;
-                if (v->lumshift > 31)
-                    shift = (v->lumshift - 64) << 6;
-                else
-                    shift = v->lumshift << 6;
-            }
-            for (i = 0; i < 256; i++) {
-                v->luty[i]  = av_clip_uint8((scale * i + shift + 32) >> 6);
-                v->lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);
-            }
+            INIT_LUT(v->lumscale, v->lumshift , v->last_luty[0] , v->last_lutuv[0] , 1);
+            INIT_LUT(v->lumscale, v->lumshift , v->last_luty[1] , v->last_lutuv[1] , 1);
         }
         v->qs_last = v->s.quarter_sample;
         if (v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
@@ -808,31 +840,11 @@
     return 0;
 }
 
-/* fill lookup tables for intensity compensation */
-#define INIT_LUT(lumscale, lumshift, luty, lutuv)   \
-    if (!lumscale) {                                \
-        scale = -64;                                \
-        shift = (255 - lumshift * 2) << 6;          \
-        if (lumshift > 31)                          \
-            shift += 128 << 6;                      \
-    } else {                                        \
-        scale = lumscale + 32;                      \
-        if (lumshift > 31)                          \
-            shift = (lumshift - 64) << 6;           \
-        else                                        \
-            shift = lumshift << 6;                  \
-    }                                               \
-    for (i = 0; i < 256; i++) {                     \
-        luty[i]  = av_clip_uint8((scale * i + shift + 32) >> 6);           \
-        lutuv[i] = av_clip_uint8((scale * (i - 128) + 128*64 + 32) >> 6);  \
-    }
-
 int ff_vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb)
 {
     int pqindex, lowquant;
     int status;
     int mbmodetab, imvtab, icbptab, twomvbptab, fourmvbptab; /* useful only for debugging */
-    int scale, shift, i; /* for initializing LUT for intensity compensation */
     int field_mode, fcm;
 
     v->numref=0;
@@ -900,9 +912,11 @@
             v->tff = get_bits1(gb);
             v->rff = get_bits1(gb);
         }
+    } else {
+        v->tff = 1;
     }
     if (v->panscanflag) {
-        av_log_missing_feature(v->s.avctx, "Pan-scan", 0);
+        avpriv_report_missing_feature(v->s.avctx, "Pan-scan");
         //...
     }
     if (v->p_frame_skipped) {
@@ -970,12 +984,16 @@
     if (v->postprocflag)
         v->postproc = get_bits(gb, 2);
 
-    if (v->s.pict_type == AV_PICTURE_TYPE_I || v->s.pict_type == AV_PICTURE_TYPE_P)
-        v->use_ic = 0;
-
     if (v->parse_only)
         return 0;
 
+    if(v->first_pic_header_flag) {
+        rotate_luts(v);
+        INIT_LUT(32, 0 , v->curr_luty[0] , v->curr_lutuv[0] , 0);
+        INIT_LUT(32, 0 , v->curr_luty[1] , v->curr_lutuv[1] , 0);
+        v->curr_use_ic = 0;
+    }
+
     switch (v->s.pict_type) {
     case AV_PICTURE_TYPE_I:
     case AV_PICTURE_TYPE_BI:
@@ -1026,7 +1044,9 @@
                 if (v->intcomp) {
                     v->lumscale = get_bits(gb, 6);
                     v->lumshift = get_bits(gb, 6);
-                    INIT_LUT(v->lumscale, v->lumshift, v->luty, v->lutuv);
+                    INIT_LUT(v->lumscale, v->lumshift, v->last_luty[0], v->last_lutuv[0], 1);
+                    INIT_LUT(v->lumscale, v->lumshift, v->last_luty[1], v->last_lutuv[1], 1);
+                    v->last_use_ic = 1;
                 }
                 status = bitplane_decoding(v->s.mbskip_table, &v->skip_is_raw, v);
                 av_log(v->s.avctx, AV_LOG_DEBUG, "SKIPMB plane encoding: "
@@ -1069,17 +1089,38 @@
                 int mvmode2;
                 mvmode2 = get_unary(gb, 1, 3);
                 v->mv_mode2 = ff_vc1_mv_pmode_table2[lowquant][mvmode2];
-                if (v->field_mode)
-                    v->intcompfield = decode210(gb);
-                v->lumscale = get_bits(gb, 6);
-                v->lumshift = get_bits(gb, 6);
-                INIT_LUT(v->lumscale, v->lumshift, v->luty, v->lutuv);
-                if ((v->field_mode) && !v->intcompfield) {
+                if (v->field_mode) {
+                    v->intcompfield = decode210(gb)^3;
+                } else
+                    v->intcompfield = 3;
+
+                v->lumscale2 = v->lumscale = 32;
+                v->lumshift2 = v->lumshift =  0;
+                if (v->intcompfield & 1) {
+                    v->lumscale = get_bits(gb, 6);
+                    v->lumshift = get_bits(gb, 6);
+                }
+                if ((v->intcompfield & 2) && v->field_mode) {
                     v->lumscale2 = get_bits(gb, 6);
                     v->lumshift2 = get_bits(gb, 6);
-                    INIT_LUT(v->lumscale2, v->lumshift2, v->luty2, v->lutuv2);
+                } else if(!v->field_mode) {
+                    v->lumscale2 = v->lumscale;
+                    v->lumshift2 = v->lumshift;
                 }
-                v->use_ic = 1;
+                if (v->field_mode && v->second_field) {
+                    if (v->cur_field_type) {
+                        INIT_LUT(v->lumscale , v->lumshift , v->curr_luty[v->cur_field_type^1], v->curr_lutuv[v->cur_field_type^1], 0);
+                        INIT_LUT(v->lumscale2, v->lumshift2, v->last_luty[v->cur_field_type  ], v->last_lutuv[v->cur_field_type  ], 1);
+                    } else {
+                        INIT_LUT(v->lumscale2, v->lumshift2, v->curr_luty[v->cur_field_type^1], v->curr_lutuv[v->cur_field_type^1], 0);
+                        INIT_LUT(v->lumscale , v->lumshift , v->last_luty[v->cur_field_type  ], v->last_lutuv[v->cur_field_type  ], 1);
+                    }
+                    v->curr_use_ic = 1;
+                } else {
+                    INIT_LUT(v->lumscale , v->lumshift , v->last_luty[0], v->last_lutuv[0], 1);
+                    INIT_LUT(v->lumscale2, v->lumshift2, v->last_luty[1], v->last_lutuv[1], 1);
+                }
+                v->last_use_ic = 1;
             }
             v->qs_last = v->s.quarter_sample;
             if (v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
diff --git a/libavcodec/vc1.h b/libavcodec/vc1.h
index 596d4d3..9e2ec68 100644
--- a/libavcodec/vc1.h
+++ b/libavcodec/vc1.h
@@ -296,8 +296,11 @@
     int dmb_is_raw;                 ///< direct mb plane is raw
     int fmb_is_raw;                 ///< forward mb plane is raw
     int skip_is_raw;                ///< skip mb plane is not coded
-    uint8_t luty[256], lutuv[256];  ///< lookup tables used for intensity compensation
-    int use_ic;                     ///< use intensity compensation in B-frames
+    uint8_t last_luty[2][256], last_lutuv[2][256];  ///< lookup tables used for intensity compensation
+    uint8_t  aux_luty[2][256],  aux_lutuv[2][256];  ///< lookup tables used for intensity compensation
+    uint8_t next_luty[2][256], next_lutuv[2][256];  ///< lookup tables used for intensity compensation
+    uint8_t (*curr_luty)[256]  ,(*curr_lutuv)[256];
+    int last_use_ic, curr_use_ic, next_use_ic, aux_use_ic;
     int rnd;                        ///< rounding control
 
     /** Frame decoding info for S/M profiles only */
@@ -340,7 +343,6 @@
     int intcomp;
     uint8_t lumscale2;  ///< for interlaced field P picture
     uint8_t lumshift2;
-    uint8_t luty2[256], lutuv2[256]; // lookup tables used for intensity compensation
     VLC* mbmode_vlc;
     VLC* imv_vlc;
     VLC* twomvbp_vlc;
diff --git a/libavcodec/vc1dec.c b/libavcodec/vc1dec.c
index 585918c..442a69b 100644
--- a/libavcodec/vc1dec.c
+++ b/libavcodec/vc1dec.c
@@ -28,6 +28,7 @@
 
 #include "internal.h"
 #include "avcodec.h"
+#include "error_resilience.h"
 #include "mpegvideo.h"
 #include "h263.h"
 #include "h264chroma.h"
@@ -77,7 +78,7 @@
 {
     MpegEncContext *s = &v->s;
     ff_init_block_index(s);
-    if (v->field_mode && v->second_field) {
+    if (v->field_mode && !(v->second_field ^ v->tff)) {
         s->dest[0] += s->current_picture_ptr->f.linesize[0];
         s->dest[1] += s->current_picture_ptr->f.linesize[1];
         s->dest[2] += s->current_picture_ptr->f.linesize[2];
@@ -344,12 +345,14 @@
 static void vc1_mc_1mv(VC1Context *v, int dir)
 {
     MpegEncContext *s = &v->s;
-    DSPContext *dsp   = &v->s.dsp;
     H264ChromaContext *h264chroma = &v->h264chroma;
     uint8_t *srcY, *srcU, *srcV;
     int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
     int off, off_uv;
     int v_edge_pos = s->v_edge_pos >> v->field_mode;
+    int i;
+    const uint8_t *luty, *lutuv;
+    int use_ic;
 
     if ((!v->field_mode ||
          (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
@@ -361,8 +364,10 @@
 
     // store motion vectors for further use in B frames
     if (s->pict_type == AV_PICTURE_TYPE_P) {
-        s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = mx;
-        s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = my;
+        for (i = 0; i < 4; i++) {
+            s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][0] = mx;
+            s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][1] = my;
+        }
     }
 
     uvmx = (mx + ((mx & 3) == 3)) >> 1;
@@ -381,32 +386,29 @@
         uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
         uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
     }
-    if (v->field_mode) { // interlaced field picture
-        if (!dir) {
-            if ((v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
-                srcY = s->current_picture.f.data[0];
-                srcU = s->current_picture.f.data[1];
-                srcV = s->current_picture.f.data[2];
-            } else {
-                srcY = s->last_picture.f.data[0];
-                srcU = s->last_picture.f.data[1];
-                srcV = s->last_picture.f.data[2];
-            }
+    if (!dir) {
+        if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
+            srcY = s->current_picture.f.data[0];
+            srcU = s->current_picture.f.data[1];
+            srcV = s->current_picture.f.data[2];
+            luty = v->curr_luty [v->ref_field_type[dir]];
+            lutuv= v->curr_lutuv[v->ref_field_type[dir]];
+            use_ic=v->curr_use_ic;
         } else {
-            srcY = s->next_picture.f.data[0];
-            srcU = s->next_picture.f.data[1];
-            srcV = s->next_picture.f.data[2];
-        }
-    } else {
-        if (!dir) {
             srcY = s->last_picture.f.data[0];
             srcU = s->last_picture.f.data[1];
             srcV = s->last_picture.f.data[2];
-        } else {
-            srcY = s->next_picture.f.data[0];
-            srcU = s->next_picture.f.data[1];
-            srcV = s->next_picture.f.data[2];
+            luty = v->last_luty [v->ref_field_type[dir]];
+            lutuv= v->last_lutuv[v->ref_field_type[dir]];
+            use_ic=v->last_use_ic;
         }
+    } else {
+        srcY = s->next_picture.f.data[0];
+        srcU = s->next_picture.f.data[1];
+        srcV = s->next_picture.f.data[2];
+        luty = v->next_luty [v->ref_field_type[dir]];
+        lutuv= v->next_lutuv[v->ref_field_type[dir]];
+        use_ic=v->next_use_ic;
     }
 
     if(!srcY)
@@ -445,7 +447,7 @@
         srcV = s->edge_emu_buffer + 18 * s->linesize;
     }
 
-    if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
+    if (v->rangeredfrm || use_ic
         || s->h_edge_pos < 22 || v_edge_pos < 22
         || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
         || (unsigned)(src_y - 1)        > v_edge_pos    - (my&3) - 16 - 3) {
@@ -486,22 +488,22 @@
             }
         }
         /* if we deal with intensity compensation we need to scale source blocks */
-        if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
+        if (use_ic) {
             int i, j;
             uint8_t *src, *src2;
 
             src = srcY;
             for (j = 0; j < 17 + s->mspel * 2; j++) {
                 for (i = 0; i < 17 + s->mspel * 2; i++)
-                    src[i] = v->luty[src[i]];
+                    src[i] = luty[src[i]];
                 src += s->linesize;
             }
             src  = srcU;
             src2 = srcV;
             for (j = 0; j < 9; j++) {
                 for (i = 0; i < 9; i++) {
-                    src[i]  = v->lutuv[src[i]];
-                    src2[i] = v->lutuv[src2[i]];
+                    src[i]  = lutuv[src[i]];
+                    src2[i] = lutuv[src2[i]];
                 }
                 src  += s->uvlinesize;
                 src2 += s->uvlinesize;
@@ -522,9 +524,9 @@
     } else { // hpel mc - always used for luma
         dxy = (my & 2) | ((mx & 2) >> 1);
         if (!v->rnd)
-            dsp->put_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
+            s->hdsp.put_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
         else
-            dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
+            s->hdsp.put_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
     }
 
     if (s->flags & CODEC_FLAG_GRAY) return;
@@ -553,15 +555,16 @@
 
 /** Do motion compensation for 4-MV macroblock - luminance block
  */
-static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir)
+static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
 {
     MpegEncContext *s = &v->s;
-    DSPContext *dsp = &v->s.dsp;
     uint8_t *srcY;
     int dxy, mx, my, src_x, src_y;
     int off;
     int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
     int v_edge_pos = s->v_edge_pos >> v->field_mode;
+    const uint8_t *luty;
+    int use_ic;
 
     if ((!v->field_mode ||
          (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
@@ -572,15 +575,20 @@
     my = s->mv[dir][n][1];
 
     if (!dir) {
-        if (v->field_mode) {
-            if ((v->cur_field_type != v->ref_field_type[dir]) && v->second_field)
-                srcY = s->current_picture.f.data[0];
-            else
-                srcY = s->last_picture.f.data[0];
-        } else
+        if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
+            srcY = s->current_picture.f.data[0];
+            luty = v->curr_luty[v->ref_field_type[dir]];
+            use_ic=v->curr_use_ic;
+        } else {
             srcY = s->last_picture.f.data[0];
-    } else
+            luty = v->last_luty[v->ref_field_type[dir]];
+            use_ic=v->last_use_ic;
+        }
+    } else {
         srcY = s->next_picture.f.data[0];
+        luty = v->next_luty[v->ref_field_type[dir]];
+        use_ic=v->next_use_ic;
+    }
 
     if(!srcY)
         return;
@@ -620,8 +628,8 @@
         default:
             av_assert2(0);
         }
-        s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
-        s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
+        s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
+        s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
         for (k = 0; k < 4; k++)
             v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
     }
@@ -630,6 +638,10 @@
         int qx, qy;
         int width  = s->avctx->coded_width;
         int height = s->avctx->coded_height >> 1;
+        if (s->pict_type == AV_PICTURE_TYPE_P) {
+            s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][0] = mx;
+            s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][1] = my;
+        }
         qx = (s->mb_x * 16) + (mx >> 2);
         qy = (s->mb_y *  8) + (my >> 3);
 
@@ -677,7 +689,7 @@
         v_edge_pos--;
     if (fieldmv && (src_y & 1) && src_y < 4)
         src_y--;
-    if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
+    if (v->rangeredfrm || use_ic
         || s->h_edge_pos < 13 || v_edge_pos < 23
         || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
         || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
@@ -701,14 +713,14 @@
             }
         }
         /* if we deal with intensity compensation we need to scale source blocks */
-        if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
+        if (use_ic) {
             int i, j;
             uint8_t *src;
 
             src = srcY;
             for (j = 0; j < 9 + s->mspel * 2; j++) {
                 for (i = 0; i < 9 + s->mspel * 2; i++)
-                    src[i] = v->luty[src[i]];
+                    src[i] = luty[src[i]];
                 src += s->linesize << fieldmv;
             }
         }
@@ -717,13 +729,16 @@
 
     if (s->mspel) {
         dxy = ((my & 3) << 2) | (mx & 3);
-        v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
+        if (avg)
+            v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
+        else
+            v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
     } else { // hpel mc - always used for luma
         dxy = (my & 2) | ((mx & 2) >> 1);
         if (!v->rnd)
-            dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
+            s->hdsp.put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
         else
-            dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
+            s->hdsp.put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
     }
 }
 
@@ -793,6 +808,8 @@
     int valid_count;
     int chroma_ref_type = v->cur_field_type, off = 0;
     int v_edge_pos = s->v_edge_pos >> v->field_mode;
+    const uint8_t *lutuv;
+    int use_ic;
 
     if (!v->field_mode && !v->s.last_picture.f.data[0])
         return;
@@ -812,8 +829,8 @@
         valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
         chroma_ref_type = v->reffield;
         if (!valid_count) {
-            s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
-            s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
+            s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
+            s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
             v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
             return; //no need to do MC for intra blocks
         }
@@ -827,8 +844,8 @@
     }
     if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f.data[0])
         return;
-    s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
-    s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
+    s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
+    s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
     uvmx = (tx + ((tx & 3) == 3)) >> 1;
     uvmy = (ty + ((ty & 3) == 3)) >> 1;
 
@@ -855,21 +872,22 @@
     }
 
     if (!dir) {
-        if (v->field_mode) {
-            if ((v->cur_field_type != chroma_ref_type) && v->cur_field_type) {
-                srcU = s->current_picture.f.data[1];
-                srcV = s->current_picture.f.data[2];
-            } else {
-                srcU = s->last_picture.f.data[1];
-                srcV = s->last_picture.f.data[2];
-            }
+        if (v->field_mode && (v->cur_field_type != chroma_ref_type) && v->second_field) {
+            srcU = s->current_picture.f.data[1];
+            srcV = s->current_picture.f.data[2];
+            lutuv= v->curr_lutuv[chroma_ref_type];
+            use_ic=v->curr_use_ic;
         } else {
             srcU = s->last_picture.f.data[1];
             srcV = s->last_picture.f.data[2];
+            lutuv= v->last_lutuv[chroma_ref_type];
+            use_ic=v->last_use_ic;
         }
     } else {
         srcU = s->next_picture.f.data[1];
         srcV = s->next_picture.f.data[2];
+        lutuv= v->next_lutuv[chroma_ref_type];
+        use_ic=v->next_use_ic;
     }
 
     if(!srcU)
@@ -886,7 +904,7 @@
         off = 0;
     }
 
-    if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
+    if (v->rangeredfrm || use_ic
         || s->h_edge_pos < 18 || v_edge_pos < 18
         || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
         || (unsigned)uvsrc_y > (v_edge_pos    >> 1) - 9) {
@@ -916,7 +934,7 @@
             }
         }
         /* if we deal with intensity compensation we need to scale source blocks */
-        if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
+        if (use_ic) {
             int i, j;
             uint8_t *src, *src2;
 
@@ -924,8 +942,8 @@
             src2 = srcV;
             for (j = 0; j < 9; j++) {
                 for (i = 0; i < 9; i++) {
-                    src[i]  = v->lutuv[src[i]];
-                    src2[i] = v->lutuv[src2[i]];
+                    src[i]  = lutuv[src[i]];
+                    src2[i] = lutuv[src2[i]];
                 }
                 src  += s->uvlinesize;
                 src2 += s->uvlinesize;
@@ -959,6 +977,7 @@
     static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
     int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
     int v_edge_pos = s->v_edge_pos >> 1;
+    int use_ic = v->last_use_ic;
 
     if (!v->s.last_picture.f.data[0])
         return;
@@ -992,7 +1011,7 @@
 
         if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
             uvsrc_y--;
-        if ((v->mv_mode == MV_PMODE_INTENSITY_COMP)
+        if ((use_ic)
             || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
             || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
             || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
@@ -1006,16 +1025,17 @@
             srcV = s->edge_emu_buffer + 16;
 
             /* if we deal with intensity compensation we need to scale source blocks */
-            if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
+            if (use_ic) {
                 int i, j;
                 uint8_t *src, *src2;
+                const uint8_t *lutuv = v->last_lutuv[v->ref_field_type[0]];
 
                 src  = srcU;
                 src2 = srcV;
                 for (j = 0; j < 5; j++) {
                     for (i = 0; i < 5; i++) {
-                        src[i]  = v->lutuv[src[i]];
-                        src2[i] = v->lutuv[src2[i]];
+                        src[i]  = lutuv[src[i]];
+                        src2[i] = lutuv[src2[i]];
                     }
                     src  += s->uvlinesize << 1;
                     src2 += s->uvlinesize << 1;
@@ -1398,30 +1418,30 @@
     xy   = s->block_index[n];
 
     if (s->mb_intra) {
-        s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = 0;
-        s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = 0;
-        s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = 0;
-        s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
+        s->mv[0][n][0] = s->current_picture.motion_val[0][xy + v->blocks_off][0] = 0;
+        s->mv[0][n][1] = s->current_picture.motion_val[0][xy + v->blocks_off][1] = 0;
+        s->current_picture.motion_val[1][xy + v->blocks_off][0] = 0;
+        s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
         if (mv1) { /* duplicate motion data for 1-MV block */
-            s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][0]        = 0;
-            s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][1]        = 0;
-            s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][0]     = 0;
-            s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][1]     = 0;
-            s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
-            s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
+            s->current_picture.motion_val[0][xy + 1 + v->blocks_off][0]        = 0;
+            s->current_picture.motion_val[0][xy + 1 + v->blocks_off][1]        = 0;
+            s->current_picture.motion_val[0][xy + wrap + v->blocks_off][0]     = 0;
+            s->current_picture.motion_val[0][xy + wrap + v->blocks_off][1]     = 0;
+            s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
+            s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
             v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
-            s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][0]        = 0;
-            s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][1]        = 0;
-            s->current_picture.f.motion_val[1][xy + wrap][0]                     = 0;
-            s->current_picture.f.motion_val[1][xy + wrap + v->blocks_off][1]     = 0;
-            s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
-            s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
+            s->current_picture.motion_val[1][xy + 1 + v->blocks_off][0]        = 0;
+            s->current_picture.motion_val[1][xy + 1 + v->blocks_off][1]        = 0;
+            s->current_picture.motion_val[1][xy + wrap][0]                     = 0;
+            s->current_picture.motion_val[1][xy + wrap + v->blocks_off][1]     = 0;
+            s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
+            s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
         }
         return;
     }
 
-    C = s->current_picture.f.motion_val[dir][xy -    1 + v->blocks_off];
-    A = s->current_picture.f.motion_val[dir][xy - wrap + v->blocks_off];
+    C = s->current_picture.motion_val[dir][xy -    1 + v->blocks_off];
+    A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
     if (mv1) {
         if (v->field_mode && mixedmv_pic)
             off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
@@ -1443,7 +1463,7 @@
             off = -1;
         }
     }
-    B = s->current_picture.f.motion_val[dir][xy - wrap + off + v->blocks_off];
+    B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
 
     a_valid = !s->first_slice_line || (n == 2 || n == 3);
     b_valid = a_valid && (s->mb_width > 1);
@@ -1606,15 +1626,15 @@
     if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
         y_bias = 1;
     /* store MV using signed modulus of MV range defined in 4.11 */
-    s->mv[dir][n][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
-    s->mv[dir][n][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias;
+    s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
+    s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias;
     if (mv1) { /* duplicate motion data for 1-MV block */
-        s->current_picture.f.motion_val[dir][xy +    1 +     v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
-        s->current_picture.f.motion_val[dir][xy +    1 +     v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
-        s->current_picture.f.motion_val[dir][xy + wrap +     v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
-        s->current_picture.f.motion_val[dir][xy + wrap +     v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
-        s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
-        s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
+        s->current_picture.motion_val[dir][xy +    1 +     v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
+        s->current_picture.motion_val[dir][xy +    1 +     v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
+        s->current_picture.motion_val[dir][xy + wrap +     v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
+        s->current_picture.motion_val[dir][xy + wrap +     v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
+        s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
+        s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
         v->mv_f[dir][xy +    1 + v->blocks_off] = v->mv_f[dir][xy +            v->blocks_off];
         v->mv_f[dir][xy + wrap + v->blocks_off] = v->mv_f[dir][xy + wrap + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
     }
@@ -1623,7 +1643,7 @@
 /** Predict and set motion vector for interlaced frame picture MBs
  */
 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
-                                     int mvn, int r_x, int r_y, uint8_t* is_intra)
+                                     int mvn, int r_x, int r_y, uint8_t* is_intra, int dir)
 {
     MpegEncContext *s = &v->s;
     int xy, wrap, off = 0;
@@ -1638,24 +1658,24 @@
     xy = s->block_index[n];
 
     if (s->mb_intra) {
-        s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = 0;
-        s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = 0;
-        s->current_picture.f.motion_val[1][xy][0] = 0;
-        s->current_picture.f.motion_val[1][xy][1] = 0;
+        s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
+        s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
+        s->current_picture.motion_val[1][xy][0] = 0;
+        s->current_picture.motion_val[1][xy][1] = 0;
         if (mvn == 1) { /* duplicate motion data for 1-MV block */
-            s->current_picture.f.motion_val[0][xy + 1][0]        = 0;
-            s->current_picture.f.motion_val[0][xy + 1][1]        = 0;
-            s->current_picture.f.motion_val[0][xy + wrap][0]     = 0;
-            s->current_picture.f.motion_val[0][xy + wrap][1]     = 0;
-            s->current_picture.f.motion_val[0][xy + wrap + 1][0] = 0;
-            s->current_picture.f.motion_val[0][xy + wrap + 1][1] = 0;
+            s->current_picture.motion_val[0][xy + 1][0]        = 0;
+            s->current_picture.motion_val[0][xy + 1][1]        = 0;
+            s->current_picture.motion_val[0][xy + wrap][0]     = 0;
+            s->current_picture.motion_val[0][xy + wrap][1]     = 0;
+            s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
+            s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
             v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
-            s->current_picture.f.motion_val[1][xy + 1][0]        = 0;
-            s->current_picture.f.motion_val[1][xy + 1][1]        = 0;
-            s->current_picture.f.motion_val[1][xy + wrap][0]     = 0;
-            s->current_picture.f.motion_val[1][xy + wrap][1]     = 0;
-            s->current_picture.f.motion_val[1][xy + wrap + 1][0] = 0;
-            s->current_picture.f.motion_val[1][xy + wrap + 1][1] = 0;
+            s->current_picture.motion_val[1][xy + 1][0]        = 0;
+            s->current_picture.motion_val[1][xy + 1][1]        = 0;
+            s->current_picture.motion_val[1][xy + wrap][0]     = 0;
+            s->current_picture.motion_val[1][xy + wrap][1]     = 0;
+            s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
+            s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
         }
         return;
     }
@@ -1665,14 +1685,14 @@
     if (s->mb_x || (n == 1) || (n == 3)) {
         if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
             || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
-            A[0] = s->current_picture.f.motion_val[0][xy - 1][0];
-            A[1] = s->current_picture.f.motion_val[0][xy - 1][1];
+            A[0] = s->current_picture.motion_val[dir][xy - 1][0];
+            A[1] = s->current_picture.motion_val[dir][xy - 1][1];
             a_valid = 1;
         } else { // current block has frame mv and cand. has field MV (so average)
-            A[0] = (s->current_picture.f.motion_val[0][xy - 1][0]
-                    + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][0] + 1) >> 1;
-            A[1] = (s->current_picture.f.motion_val[0][xy - 1][1]
-                    + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][1] + 1) >> 1;
+            A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
+                    + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
+            A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
+                    + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
             a_valid = 1;
         }
         if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
@@ -1692,11 +1712,11 @@
                 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
                     n_adj = (n & 2) | (n & 1);
                 }
-                B[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][0];
-                B[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][1];
+                B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
+                B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
                 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
-                    B[0] = (B[0] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
-                    B[1] = (B[1] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
+                    B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
+                    B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
                 }
             }
             if (s->mb_width > 1) {
@@ -1707,11 +1727,11 @@
                     if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
                         n_adj = n & 2;
                     }
-                    C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][0];
-                    C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][1];
+                    C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
+                    C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
                     if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
-                        C[0] = (1 + C[0] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
-                        C[1] = (1 + C[1] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
+                        C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
+                        C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
                     }
                     if (s->mb_x == s->mb_width - 1) {
                         if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
@@ -1721,11 +1741,11 @@
                             if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
                                 n_adj = n | 1;
                             }
-                            C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][0];
-                            C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][1];
+                            C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
+                            C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
                             if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
-                                C[0] = (1 + C[0] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
-                                C[1] = (1 + C[1] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
+                                C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
+                                C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
                             }
                         } else
                             c_valid = 0;
@@ -1736,12 +1756,12 @@
     } else {
         pos_b   = s->block_index[1];
         b_valid = 1;
-        B[0]    = s->current_picture.f.motion_val[0][pos_b][0];
-        B[1]    = s->current_picture.f.motion_val[0][pos_b][1];
+        B[0]    = s->current_picture.motion_val[dir][pos_b][0];
+        B[1]    = s->current_picture.motion_val[dir][pos_b][1];
         pos_c   = s->block_index[0];
         c_valid = 1;
-        C[0]    = s->current_picture.f.motion_val[0][pos_c][0];
-        C[1]    = s->current_picture.f.motion_val[0][pos_c][1];
+        C[0]    = s->current_picture.motion_val[dir][pos_c][0];
+        C[1]    = s->current_picture.motion_val[dir][pos_c][1];
     }
 
     total_valid = a_valid + b_valid + c_valid;
@@ -1830,20 +1850,20 @@
     }
 
     /* store MV using signed modulus of MV range defined in 4.11 */
-    s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
-    s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
+    s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
+    s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
     if (mvn == 1) { /* duplicate motion data for 1-MV block */
-        s->current_picture.f.motion_val[0][xy +    1    ][0] = s->current_picture.f.motion_val[0][xy][0];
-        s->current_picture.f.motion_val[0][xy +    1    ][1] = s->current_picture.f.motion_val[0][xy][1];
-        s->current_picture.f.motion_val[0][xy + wrap    ][0] = s->current_picture.f.motion_val[0][xy][0];
-        s->current_picture.f.motion_val[0][xy + wrap    ][1] = s->current_picture.f.motion_val[0][xy][1];
-        s->current_picture.f.motion_val[0][xy + wrap + 1][0] = s->current_picture.f.motion_val[0][xy][0];
-        s->current_picture.f.motion_val[0][xy + wrap + 1][1] = s->current_picture.f.motion_val[0][xy][1];
+        s->current_picture.motion_val[dir][xy +    1    ][0] = s->current_picture.motion_val[dir][xy][0];
+        s->current_picture.motion_val[dir][xy +    1    ][1] = s->current_picture.motion_val[dir][xy][1];
+        s->current_picture.motion_val[dir][xy + wrap    ][0] = s->current_picture.motion_val[dir][xy][0];
+        s->current_picture.motion_val[dir][xy + wrap    ][1] = s->current_picture.motion_val[dir][xy][1];
+        s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
+        s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
     } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
-        s->current_picture.f.motion_val[0][xy + 1][0] = s->current_picture.f.motion_val[0][xy][0];
-        s->current_picture.f.motion_val[0][xy + 1][1] = s->current_picture.f.motion_val[0][xy][1];
-        s->mv[0][n + 1][0] = s->mv[0][n][0];
-        s->mv[0][n + 1][1] = s->mv[0][n][1];
+        s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
+        s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
+        s->mv[dir][n + 1][0] = s->mv[dir][n][0];
+        s->mv[dir][n + 1][1] = s->mv[dir][n][1];
     }
 }
 
@@ -1852,12 +1872,12 @@
 static void vc1_interp_mc(VC1Context *v)
 {
     MpegEncContext *s = &v->s;
-    DSPContext *dsp = &v->s.dsp;
     H264ChromaContext *h264chroma = &v->h264chroma;
     uint8_t *srcY, *srcU, *srcV;
     int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
     int off, off_uv;
     int v_edge_pos = s->v_edge_pos >> v->field_mode;
+    int use_ic = v->next_use_ic;
 
     if (!v->field_mode && !v->s.next_picture.f.data[0])
         return;
@@ -1912,7 +1932,7 @@
         srcV = s->edge_emu_buffer + 18 * s->linesize;
     }
 
-    if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22
+    if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22 || use_ic
         || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
         || (unsigned)(src_y - 1) > v_edge_pos    - (my & 3) - 16 - 3) {
         uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
@@ -1951,6 +1971,30 @@
                 src2 += s->uvlinesize;
             }
         }
+
+        if (use_ic) {
+            const uint8_t *luty = v->next_luty [v->ref_field_type[1]];
+            const uint8_t *lutuv= v->next_lutuv[v->ref_field_type[1]];
+            int i, j;
+            uint8_t *src, *src2;
+
+            src = srcY;
+            for (j = 0; j < 17 + s->mspel * 2; j++) {
+                for (i = 0; i < 17 + s->mspel * 2; i++)
+                    src[i] = luty[src[i]];
+                src += s->linesize;
+            }
+            src  = srcU;
+            src2 = srcV;
+            for (j = 0; j < 9; j++) {
+                for (i = 0; i < 9; i++) {
+                    src[i]  = lutuv[src[i]];
+                    src2[i] = lutuv[src2[i]];
+                }
+                src  += s->uvlinesize;
+                src2 += s->uvlinesize;
+            }
+        }
         srcY += s->mspel * (1 + s->linesize);
     }
 
@@ -1968,9 +2012,9 @@
         dxy = (my & 2) | ((mx & 2) >> 1);
 
         if (!v->rnd)
-            dsp->avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
+            s->hdsp.avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
         else
-            dsp->avg_no_rnd_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, 16);
+            s->hdsp.avg_no_rnd_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, 16);
     }
 
     if (s->flags & CODEC_FLAG_GRAY) return;
@@ -2010,29 +2054,30 @@
 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
                             int direct, int mode)
 {
-    if (v->use_ic) {
+    int use_ic = v->next_use_ic || v->curr_use_ic || v->last_use_ic;
+    if (use_ic) {
         v->mv_mode2 = v->mv_mode;
         v->mv_mode  = MV_PMODE_INTENSITY_COMP;
     }
     if (direct) {
         vc1_mc_1mv(v, 0);
         vc1_interp_mc(v);
-        if (v->use_ic)
+        if (use_ic)
             v->mv_mode = v->mv_mode2;
         return;
     }
     if (mode == BMV_TYPE_INTERPOLATED) {
         vc1_mc_1mv(v, 0);
         vc1_interp_mc(v);
-        if (v->use_ic)
+        if (use_ic)
             v->mv_mode = v->mv_mode2;
         return;
     }
 
-    if (v->use_ic && (mode == BMV_TYPE_BACKWARD))
+    if (use_ic && (mode == BMV_TYPE_BACKWARD))
         v->mv_mode = v->mv_mode2;
     vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
-    if (v->use_ic)
+    if (use_ic)
         v->mv_mode = v->mv_mode2;
 }
 
@@ -2059,17 +2104,17 @@
     xy = s->block_index[0];
 
     if (s->mb_intra) {
-        s->current_picture.f.motion_val[0][xy + v->blocks_off][0] =
-        s->current_picture.f.motion_val[0][xy + v->blocks_off][1] =
-        s->current_picture.f.motion_val[1][xy + v->blocks_off][0] =
-        s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
+        s->current_picture.motion_val[0][xy + v->blocks_off][0] =
+        s->current_picture.motion_val[0][xy + v->blocks_off][1] =
+        s->current_picture.motion_val[1][xy + v->blocks_off][0] =
+        s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
         return;
     }
     if (!v->field_mode) {
-        s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
-        s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
-        s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
-        s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
+        s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
+        s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
+        s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
+        s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
 
         /* Pullback predicted motion vectors as specified in 8.4.5.4 */
         s->mv[0][0][0] = av_clip(s->mv[0][0][0], -60 - (s->mb_x << 6), (s->mb_width  << 6) - 4 - (s->mb_x << 6));
@@ -2078,18 +2123,18 @@
         s->mv[1][0][1] = av_clip(s->mv[1][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
     }
     if (direct) {
-        s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
-        s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
-        s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
-        s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
+        s->current_picture.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
+        s->current_picture.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
+        s->current_picture.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
+        s->current_picture.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
         return;
     }
 
     if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
-        C   = s->current_picture.f.motion_val[0][xy - 2];
-        A   = s->current_picture.f.motion_val[0][xy - wrap * 2];
+        C   = s->current_picture.motion_val[0][xy - 2];
+        A   = s->current_picture.motion_val[0][xy - wrap * 2];
         off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
-        B   = s->current_picture.f.motion_val[0][xy - wrap * 2 + off];
+        B   = s->current_picture.motion_val[0][xy - wrap * 2 + off];
 
         if (!s->mb_x) C[0] = C[1] = 0;
         if (!s->first_slice_line) { // predictor A is not out of bounds
@@ -2164,10 +2209,10 @@
         s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
     }
     if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
-        C   = s->current_picture.f.motion_val[1][xy - 2];
-        A   = s->current_picture.f.motion_val[1][xy - wrap * 2];
+        C   = s->current_picture.motion_val[1][xy - 2];
+        A   = s->current_picture.motion_val[1][xy - wrap * 2];
         off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
-        B   = s->current_picture.f.motion_val[1][xy - wrap * 2 + off];
+        B   = s->current_picture.motion_val[1][xy - wrap * 2 + off];
 
         if (!s->mb_x)
             C[0] = C[1] = 0;
@@ -2243,10 +2288,10 @@
         s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
         s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
     }
-    s->current_picture.f.motion_val[0][xy][0] = s->mv[0][0][0];
-    s->current_picture.f.motion_val[0][xy][1] = s->mv[0][0][1];
-    s->current_picture.f.motion_val[1][xy][0] = s->mv[1][0][0];
-    s->current_picture.f.motion_val[1][xy][1] = s->mv[1][0][1];
+    s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
+    s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
+    s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
+    s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
 }
 
 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
@@ -2257,14 +2302,14 @@
 
     if (v->bmvtype == BMV_TYPE_DIRECT) {
         int total_opp, k, f;
-        if (s->next_picture.f.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
-            s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
+        if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
+            s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
                                       v->bfraction, 0, s->quarter_sample);
-            s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
+            s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
                                       v->bfraction, 0, s->quarter_sample);
-            s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
+            s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
                                       v->bfraction, 1, s->quarter_sample);
-            s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
+            s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
                                       v->bfraction, 1, s->quarter_sample);
 
             total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
@@ -2279,10 +2324,10 @@
         }
         v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
         for (k = 0; k < 4; k++) {
-            s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
-            s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
-            s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
-            s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
+            s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
+            s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
+            s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
+            s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
             v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
             v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
         }
@@ -2400,17 +2445,17 @@
     b = dc_val[ - 1 - wrap];
     a = dc_val[ - wrap];
     /* scale predictors if needed */
-    q1 = s->current_picture.f.qscale_table[mb_pos];
+    q1 = s->current_picture.qscale_table[mb_pos];
     dqscale_index = s->y_dc_scale_table[q1] - 1;
     if (dqscale_index < 0)
         return 0;
     if (c_avail && (n != 1 && n != 3)) {
-        q2 = s->current_picture.f.qscale_table[mb_pos - 1];
+        q2 = s->current_picture.qscale_table[mb_pos - 1];
         if (q2 && q2 != q1)
             c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
     }
     if (a_avail && (n != 2 && n != 3)) {
-        q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
+        q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
         if (q2 && q2 != q1)
             a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
     }
@@ -2420,7 +2465,7 @@
             off--;
         if (n != 2)
             off -= s->mb_stride;
-        q2 = s->current_picture.f.qscale_table[off];
+        q2 = s->current_picture.qscale_table[off];
         if (q2 && q2 != q1)
             b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
     }
@@ -2791,11 +2836,11 @@
     else // top
         ac_val -= 16 * s->block_wrap[n];
 
-    q1 = s->current_picture.f.qscale_table[mb_pos];
+    q1 = s->current_picture.qscale_table[mb_pos];
     if ( dc_pred_dir && c_avail && mb_pos)
-        q2 = s->current_picture.f.qscale_table[mb_pos - 1];
+        q2 = s->current_picture.qscale_table[mb_pos - 1];
     if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
-        q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
+        q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
     if ( dc_pred_dir && n == 1)
         q2 = q1;
     if (!dc_pred_dir && n == 2)
@@ -3014,11 +3059,11 @@
     else //top
         ac_val -= 16 * s->block_wrap[n];
 
-    q1 = s->current_picture.f.qscale_table[mb_pos];
+    q1 = s->current_picture.qscale_table[mb_pos];
     if (dc_pred_dir && c_avail && mb_pos)
-        q2 = s->current_picture.f.qscale_table[mb_pos - 1];
+        q2 = s->current_picture.qscale_table[mb_pos - 1];
     if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
-        q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
+        q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
     if ( dc_pred_dir && n == 1)
         q2 = q1;
     if (!dc_pred_dir && n == 2)
@@ -3336,7 +3381,7 @@
             bottom_is_intra = (block_num < 2) ? (mb_is_intra          >> ((block_num + 2) * 4))
                                               : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
             mv_stride       = s->b8_stride;
-            mv              = &s->current_picture.f.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
+            mv              = &s->current_picture.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
         }
 
         if (bottom_is_intra & 1 || block_is_intra & 1 ||
@@ -3398,7 +3443,7 @@
                                              : (mb_cbp                              >> ((block_num + 1) * 4));
             right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
                                              : (mb_is_intra                         >> ((block_num + 1) * 4));
-            mv             = &s->current_picture.f.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
+            mv             = &s->current_picture.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
         }
         if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
             v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
@@ -3492,10 +3537,10 @@
             GET_MVDATA(dmv_x, dmv_y);
 
             if (s->mb_intra) {
-                s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
-                s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
+                s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
+                s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
             }
-            s->current_picture.f.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
+            s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
             vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
 
             /* FIXME Set DC val for inter block ? */
@@ -3512,7 +3557,7 @@
                 mquant = v->pq;
                 cbp    = 0;
             }
-            s->current_picture.f.qscale_table[mb_pos] = mquant;
+            s->current_picture.qscale_table[mb_pos] = mquant;
 
             if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
@@ -3566,8 +3611,8 @@
                 v->mb_type[0][s->block_index[i]] = 0;
                 s->dc_val[0][s->block_index[i]]  = 0;
             }
-            s->current_picture.f.mb_type[mb_pos]      = MB_TYPE_SKIP;
-            s->current_picture.f.qscale_table[mb_pos] = 0;
+            s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
+            s->current_picture.qscale_table[mb_pos] = 0;
             vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
             vc1_mc_1mv(v, 0);
         }
@@ -3590,7 +3635,7 @@
                     }
                     vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
                     if (!s->mb_intra)
-                        vc1_mc_4mv_luma(v, i, 0);
+                        vc1_mc_4mv_luma(v, i, 0, 0);
                     intra_count += s->mb_intra;
                     is_intra[i]  = s->mb_intra;
                     is_coded[i]  = mb_has_coeffs;
@@ -3610,7 +3655,7 @@
             if (!intra_count && !coded_inter)
                 goto end;
             GET_MQUANT();
-            s->current_picture.f.qscale_table[mb_pos] = mquant;
+            s->current_picture.qscale_table[mb_pos] = mquant;
             /* test if block is intra and has pred */
             {
                 int intrapred = 0;
@@ -3673,17 +3718,17 @@
             }
         } else { // skipped MB
             s->mb_intra                               = 0;
-            s->current_picture.f.qscale_table[mb_pos] = 0;
+            s->current_picture.qscale_table[mb_pos] = 0;
             for (i = 0; i < 6; i++) {
                 v->mb_type[0][s->block_index[i]] = 0;
                 s->dc_val[0][s->block_index[i]]  = 0;
             }
             for (i = 0; i < 4; i++) {
                 vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
-                vc1_mc_4mv_luma(v, i, 0);
+                vc1_mc_4mv_luma(v, i, 0, 0);
             }
             vc1_mc_4mv_chroma(v, 0);
-            s->current_picture.f.qscale_table[mb_pos] = 0;
+            s->current_picture.qscale_table[mb_pos] = 0;
         }
     }
 end:
@@ -3758,9 +3803,11 @@
             break;
         }
         if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
-            s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
-            s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
-            s->current_picture.f.mb_type[mb_pos]                     = MB_TYPE_INTRA;
+            for (i = 0; i < 4; i++) {
+                s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
+                s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
+            }
+            s->current_picture.mb_type[mb_pos]                     = MB_TYPE_INTRA;
             s->mb_intra = v->is_intra[s->mb_x] = 1;
             for (i = 0; i < 6; i++)
                 v->mb_type[0][s->block_index[i]] = 1;
@@ -3770,7 +3817,7 @@
                 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
             v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
             GET_MQUANT();
-            s->current_picture.f.qscale_table[mb_pos] = mquant;
+            s->current_picture.qscale_table[mb_pos] = mquant;
             /* Set DC scale - y and c use the same (not sure if necessary here) */
             s->y_dc_scale = s->y_dc_scale_table[mquant];
             s->c_dc_scale = s->c_dc_scale_table[mquant];
@@ -3828,8 +3875,8 @@
                         if (val) {
                             get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
                         }
-                        vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
-                        vc1_mc_4mv_luma(v, i, 0);
+                        vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
+                        vc1_mc_4mv_luma(v, i, 0, 0);
                     } else if (i == 4) {
                         vc1_mc_4mv_chroma4(v);
                     }
@@ -3840,16 +3887,16 @@
                 if (mvbp & 2) {
                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
                 }
-                vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
-                vc1_mc_4mv_luma(v, 0, 0);
-                vc1_mc_4mv_luma(v, 1, 0);
+                vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
+                vc1_mc_4mv_luma(v, 0, 0, 0);
+                vc1_mc_4mv_luma(v, 1, 0, 0);
                 dmv_x = dmv_y = 0;
                 if (mvbp & 1) {
                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
                 }
-                vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
-                vc1_mc_4mv_luma(v, 2, 0);
-                vc1_mc_4mv_luma(v, 3, 0);
+                vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
+                vc1_mc_4mv_luma(v, 2, 0, 0);
+                vc1_mc_4mv_luma(v, 3, 0, 0);
                 vc1_mc_4mv_chroma4(v);
             } else {
                 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
@@ -3857,12 +3904,12 @@
                 if (mvbp) {
                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
                 }
-                vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
+                vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
                 vc1_mc_1mv(v, 0);
             }
             if (cbp)
                 GET_MQUANT();  // p. 227
-            s->current_picture.f.qscale_table[mb_pos] = mquant;
+            s->current_picture.qscale_table[mb_pos] = mquant;
             if (!v->ttmbf && cbp)
                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
             for (i = 0; i < 6; i++) {
@@ -3891,13 +3938,13 @@
             v->mb_type[0][s->block_index[i]] = 0;
             s->dc_val[0][s->block_index[i]] = 0;
         }
-        s->current_picture.f.mb_type[mb_pos]      = MB_TYPE_SKIP;
-        s->current_picture.f.qscale_table[mb_pos] = 0;
+        s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
+        s->current_picture.qscale_table[mb_pos] = 0;
         v->blk_mv_type[s->block_index[0]] = 0;
         v->blk_mv_type[s->block_index[1]] = 0;
         v->blk_mv_type[s->block_index[2]] = 0;
         v->blk_mv_type[s->block_index[3]] = 0;
-        vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
+        vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
         vc1_mc_1mv(v, 0);
     }
     if (s->mb_x == s->mb_width - 1)
@@ -3929,11 +3976,11 @@
     idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
     if (idx_mbmode <= 1) { // intra MB
         s->mb_intra = v->is_intra[s->mb_x] = 1;
-        s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
-        s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
-        s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
+        s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
+        s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
+        s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
         GET_MQUANT();
-        s->current_picture.f.qscale_table[mb_pos] = mquant;
+        s->current_picture.qscale_table[mb_pos] = mquant;
         /* Set DC scale - y and c use the same (not sure if necessary here) */
         s->y_dc_scale = s->y_dc_scale_table[mquant];
         s->c_dc_scale = s->c_dc_scale_table[mquant];
@@ -3964,7 +4011,7 @@
         }
     } else {
         s->mb_intra = v->is_intra[s->mb_x] = 0;
-        s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
+        s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
         for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
         if (idx_mbmode <= 5) { // 1-MV
             dmv_x = dmv_y = pred_flag = 0;
@@ -3984,7 +4031,7 @@
                         get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
                     }
                     vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
-                    vc1_mc_4mv_luma(v, i, 0);
+                    vc1_mc_4mv_luma(v, i, 0, 0);
                 } else if (i == 4)
                     vc1_mc_4mv_chroma(v, 0);
             }
@@ -3995,7 +4042,7 @@
         if (cbp) {
             GET_MQUANT();
         }
-        s->current_picture.f.qscale_table[mb_pos] = mquant;
+        s->current_picture.qscale_table[mb_pos] = mquant;
         if (!v->ttmbf && cbp) {
             ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
         }
@@ -4059,7 +4106,7 @@
         v->mb_type[0][s->block_index[i]] = 0;
         s->dc_val[0][s->block_index[i]]  = 0;
     }
-    s->current_picture.f.qscale_table[mb_pos] = 0;
+    s->current_picture.qscale_table[mb_pos] = 0;
 
     if (!direct) {
         if (!skipped) {
@@ -4096,7 +4143,7 @@
         cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
         GET_MQUANT();
         s->mb_intra = 0;
-        s->current_picture.f.qscale_table[mb_pos] = mquant;
+        s->current_picture.qscale_table[mb_pos] = mquant;
         if (!v->ttmbf)
             ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
         dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
@@ -4111,7 +4158,7 @@
         }
         if (s->mb_intra && !mb_has_coeffs) {
             GET_MQUANT();
-            s->current_picture.f.qscale_table[mb_pos] = mquant;
+            s->current_picture.qscale_table[mb_pos] = mquant;
             s->ac_pred = get_bits1(gb);
             cbp = 0;
             vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
@@ -4133,7 +4180,7 @@
                 s->ac_pred = get_bits1(gb);
             cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
             GET_MQUANT();
-            s->current_picture.f.qscale_table[mb_pos] = mquant;
+            s->current_picture.qscale_table[mb_pos] = mquant;
             if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
         }
@@ -4200,11 +4247,11 @@
     idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
     if (idx_mbmode <= 1) { // intra MB
         s->mb_intra = v->is_intra[s->mb_x] = 1;
-        s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
-        s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
-        s->current_picture.f.mb_type[mb_pos + v->mb_off]         = MB_TYPE_INTRA;
+        s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
+        s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
+        s->current_picture.mb_type[mb_pos + v->mb_off]         = MB_TYPE_INTRA;
         GET_MQUANT();
-        s->current_picture.f.qscale_table[mb_pos] = mquant;
+        s->current_picture.qscale_table[mb_pos] = mquant;
         /* Set DC scale - y and c use the same (not sure if necessary here) */
         s->y_dc_scale = s->y_dc_scale_table[mquant];
         s->c_dc_scale = s->c_dc_scale_table[mquant];
@@ -4238,7 +4285,7 @@
         }
     } else {
         s->mb_intra = v->is_intra[s->mb_x] = 0;
-        s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
+        s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
         for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
         if (v->fmb_is_raw)
             fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
@@ -4293,7 +4340,7 @@
                                              &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
                     }
                     vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
-                    vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD);
+                    vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
                 } else if (i == 4)
                     vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
             }
@@ -4304,7 +4351,7 @@
         if (cbp) {
             GET_MQUANT();
         }
-        s->current_picture.f.qscale_table[mb_pos] = mquant;
+        s->current_picture.qscale_table[mb_pos] = mquant;
         if (!v->ttmbf && cbp) {
             ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
         }
@@ -4327,6 +4374,355 @@
     }
 }
 
+/** Decode one B-frame MB (in interlaced frame B picture)
+ */
+static int vc1_decode_b_mb_intfr(VC1Context *v)
+{
+    MpegEncContext *s = &v->s;
+    GetBitContext *gb = &s->gb;
+    int i, j;
+    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
+    int cbp = 0; /* cbp decoding stuff */
+    int mqdiff, mquant; /* MB quantization */
+    int ttmb = v->ttfrm; /* MB Transform type */
+    int mvsw = 0; /* motion vector switch */
+    int mb_has_coeffs = 1; /* last_flag */
+    int dmv_x, dmv_y; /* Differential MV components */
+    int val; /* temp value */
+    int first_block = 1;
+    int dst_idx, off;
+    int skipped, direct, twomv = 0;
+    int block_cbp = 0, pat, block_tt = 0;
+    int idx_mbmode = 0, mvbp;
+    int stride_y, fieldtx;
+    int bmvtype = BMV_TYPE_BACKWARD;
+    int dir, dir2;
+
+    mquant = v->pq; /* Lossy initialization */
+    s->mb_intra = 0;
+    if (v->skip_is_raw)
+        skipped = get_bits1(gb);
+    else
+        skipped = v->s.mbskip_table[mb_pos];
+
+    if (!skipped) {
+        idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
+        if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD)
+        {
+            twomv = 1;
+            v->blk_mv_type[s->block_index[0]] = 1;
+            v->blk_mv_type[s->block_index[1]] = 1;
+            v->blk_mv_type[s->block_index[2]] = 1;
+            v->blk_mv_type[s->block_index[3]] = 1;
+        } else {
+            v->blk_mv_type[s->block_index[0]] = 0;
+            v->blk_mv_type[s->block_index[1]] = 0;
+            v->blk_mv_type[s->block_index[2]] = 0;
+            v->blk_mv_type[s->block_index[3]] = 0;
+        }
+    }
+
+    if (v->dmb_is_raw)
+        direct = get_bits1(gb);
+    else
+        direct = v->direct_mb_plane[mb_pos];
+
+    if (direct) {
+        s->mv[0][0][0] = s->current_picture.motion_val[0][s->block_index[0]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][0], v->bfraction, 0, s->quarter_sample);
+        s->mv[0][0][1] = s->current_picture.motion_val[0][s->block_index[0]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][1], v->bfraction, 0, s->quarter_sample);
+        s->mv[1][0][0] = s->current_picture.motion_val[1][s->block_index[0]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][0], v->bfraction, 1, s->quarter_sample);
+        s->mv[1][0][1] = s->current_picture.motion_val[1][s->block_index[0]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][1], v->bfraction, 1, s->quarter_sample);
+
+        if (twomv) {
+            s->mv[0][2][0] = s->current_picture.motion_val[0][s->block_index[2]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][0], v->bfraction, 0, s->quarter_sample);
+            s->mv[0][2][1] = s->current_picture.motion_val[0][s->block_index[2]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][1], v->bfraction, 0, s->quarter_sample);
+            s->mv[1][2][0] = s->current_picture.motion_val[1][s->block_index[2]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][0], v->bfraction, 1, s->quarter_sample);
+            s->mv[1][2][1] = s->current_picture.motion_val[1][s->block_index[2]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][1], v->bfraction, 1, s->quarter_sample);
+
+            for (i = 1; i < 4; i+=2) {
+                s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
+                s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
+                s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
+                s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
+            }
+        } else {
+            for (i = 1; i < 4; i++) {
+                s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
+                s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
+                s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
+                s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
+            }
+        }
+    }
+
+    if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
+        for (i = 0; i < 4; i++) {
+            s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
+            s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
+            s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
+            s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
+        }
+        s->current_picture.mb_type[mb_pos]                     = MB_TYPE_INTRA;
+        s->mb_intra = v->is_intra[s->mb_x] = 1;
+        for (i = 0; i < 6; i++)
+            v->mb_type[0][s->block_index[i]] = 1;
+        fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
+        mb_has_coeffs = get_bits1(gb);
+        if (mb_has_coeffs)
+            cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
+        v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
+        GET_MQUANT();
+        s->current_picture.qscale_table[mb_pos] = mquant;
+        /* Set DC scale - y and c use the same (not sure if necessary here) */
+        s->y_dc_scale = s->y_dc_scale_table[mquant];
+        s->c_dc_scale = s->c_dc_scale_table[mquant];
+        dst_idx = 0;
+        for (i = 0; i < 6; i++) {
+            s->dc_val[0][s->block_index[i]] = 0;
+            dst_idx += i >> 2;
+            val = ((cbp >> (5 - i)) & 1);
+            v->mb_type[0][s->block_index[i]] = s->mb_intra;
+            v->a_avail = v->c_avail = 0;
+            if (i == 2 || i == 3 || !s->first_slice_line)
+                v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
+            if (i == 1 || i == 3 || s->mb_x)
+                v->c_avail = v->mb_type[0][s->block_index[i] - 1];
+
+            vc1_decode_intra_block(v, s->block[i], i, val, mquant,
+                                   (i & 4) ? v->codingset2 : v->codingset);
+            if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
+            v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
+            if (i < 4) {
+                stride_y = s->linesize << fieldtx;
+                off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
+            } else {
+                stride_y = s->uvlinesize;
+                off = 0;
+            }
+            s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
+        }
+    } else {
+        s->mb_intra = v->is_intra[s->mb_x] = 0;
+        if (!direct) {
+            if (skipped || !s->mb_intra) {
+                bmvtype = decode012(gb);
+                switch (bmvtype) {
+                case 0:
+                    bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
+                    break;
+                case 1:
+                    bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
+                    break;
+                case 2:
+                    bmvtype  = BMV_TYPE_INTERPOLATED;
+                }
+            }
+
+            if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
+                mvsw = get_bits1(gb);
+        }
+
+        if (!skipped) { // inter MB
+            mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
+            if (mb_has_coeffs)
+                cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
+            if (!direct) {
+                if (bmvtype == BMV_TYPE_INTERPOLATED & twomv) {
+                    v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
+                }
+                else if (bmvtype == BMV_TYPE_INTERPOLATED | twomv) {
+                    v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
+                }
+            }
+
+            for (i = 0; i < 6; i++)
+                v->mb_type[0][s->block_index[i]] = 0;
+            fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
+            /* for all motion vector read MVDATA and motion compensate each block */
+            dst_idx = 0;
+            if (direct) {
+                if (twomv) {
+                    for (i = 0; i < 4; i++) {
+                        vc1_mc_4mv_luma(v, i, 0, 0);
+                        vc1_mc_4mv_luma(v, i, 1, 1);
+                    }
+                    vc1_mc_4mv_chroma4(v);
+                } else {
+                    vc1_mc_1mv(v, 0);
+                    vc1_interp_mc(v);
+                }
+            } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
+                mvbp = v->fourmvbp;
+                for (i = 0; i < 4; i++) {
+                    dir = i==1 || i==3;
+                    dmv_x = dmv_y = 0;
+                    val = ((mvbp >> (3 - i)) & 1);
+                    if (val) {
+                        get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
+                    }
+                    j = i > 1 ? 2 : 0;
+                    vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
+                    vc1_mc_4mv_luma(v, j, dir, dir);
+                    vc1_mc_4mv_luma(v, j+1, dir, dir);
+                }
+
+                vc1_mc_4mv_chroma4(v);
+            } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
+                mvbp = v->twomvbp;
+                dmv_x = dmv_y = 0;
+                if (mvbp & 2) {
+                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
+                }
+
+                vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
+                vc1_mc_1mv(v, 0);
+
+                dmv_x = dmv_y = 0;
+                if (mvbp & 1) {
+                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
+                }
+
+                vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
+                vc1_interp_mc(v);
+            } else if (twomv) {
+                dir = bmvtype == BMV_TYPE_BACKWARD;
+                dir2 = dir;
+                if (mvsw)
+                    dir2 = !dir;
+                mvbp = v->twomvbp;
+                dmv_x = dmv_y = 0;
+                if (mvbp & 2) {
+                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
+                }
+                vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
+
+                dmv_x = dmv_y = 0;
+                if (mvbp & 1) {
+                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
+                }
+                vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
+
+                if (mvsw) {
+                    for (i = 0; i<2; i++) {
+                        s->mv[dir][i+2][0] = s->mv[dir][i][0] = s->current_picture.motion_val[dir][s->block_index[i+2]][0] = s->current_picture.motion_val[dir][s->block_index[i]][0];
+                        s->mv[dir][i+2][1] = s->mv[dir][i][1] = s->current_picture.motion_val[dir][s->block_index[i+2]][1] = s->current_picture.motion_val[dir][s->block_index[i]][1];
+                        s->mv[dir2][i+2][0] = s->mv[dir2][i][0] = s->current_picture.motion_val[dir2][s->block_index[i]][0] = s->current_picture.motion_val[dir2][s->block_index[i+2]][0];
+                        s->mv[dir2][i+2][1] = s->mv[dir2][i][1] = s->current_picture.motion_val[dir2][s->block_index[i]][1] = s->current_picture.motion_val[dir2][s->block_index[i+2]][1];
+                    }
+                } else {
+                    vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
+                    vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
+                }
+
+                vc1_mc_4mv_luma(v, 0, dir, 0);
+                vc1_mc_4mv_luma(v, 1, dir, 0);
+                vc1_mc_4mv_luma(v, 2, dir2, 0);
+                vc1_mc_4mv_luma(v, 3, dir2, 0);
+                vc1_mc_4mv_chroma4(v);
+            } else {
+                dir = bmvtype == BMV_TYPE_BACKWARD;
+
+                mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
+                dmv_x = dmv_y = 0;
+                if (mvbp) {
+                    get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
+                }
+
+                vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
+                v->blk_mv_type[s->block_index[0]] = 1;
+                v->blk_mv_type[s->block_index[1]] = 1;
+                v->blk_mv_type[s->block_index[2]] = 1;
+                v->blk_mv_type[s->block_index[3]] = 1;
+                vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
+                for (i = 0; i<2; i++) {
+                    s->mv[!dir][i+2][0] = s->mv[!dir][i][0] = s->current_picture.motion_val[!dir][s->block_index[i+2]][0] = s->current_picture.motion_val[!dir][s->block_index[i]][0];
+                    s->mv[!dir][i+2][1] = s->mv[!dir][i][1] = s->current_picture.motion_val[!dir][s->block_index[i+2]][1] = s->current_picture.motion_val[!dir][s->block_index[i]][1];
+                }
+                vc1_mc_1mv(v, dir);
+            }
+
+            if (cbp)
+                GET_MQUANT();  // p. 227
+            s->current_picture.qscale_table[mb_pos] = mquant;
+            if (!v->ttmbf && cbp)
+                ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
+            for (i = 0; i < 6; i++) {
+                s->dc_val[0][s->block_index[i]] = 0;
+                dst_idx += i >> 2;
+                val = ((cbp >> (5 - i)) & 1);
+                if (!fieldtx)
+                    off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
+                else
+                    off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
+                if (val) {
+                    pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
+                                             first_block, s->dest[dst_idx] + off,
+                                             (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
+                                             (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
+                    block_cbp |= pat << (i << 2);
+                    if (!v->ttmbf && ttmb < 8)
+                        ttmb = -1;
+                    first_block = 0;
+                }
+            }
+
+        } else { // skipped
+            dir = 0;
+            for (i = 0; i < 6; i++) {
+                v->mb_type[0][s->block_index[i]] = 0;
+                s->dc_val[0][s->block_index[i]] = 0;
+            }
+            s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
+            s->current_picture.qscale_table[mb_pos] = 0;
+            v->blk_mv_type[s->block_index[0]] = 0;
+            v->blk_mv_type[s->block_index[1]] = 0;
+            v->blk_mv_type[s->block_index[2]] = 0;
+            v->blk_mv_type[s->block_index[3]] = 0;
+
+            if (!direct) {
+                if (bmvtype == BMV_TYPE_INTERPOLATED) {
+                    vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
+                    vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
+                } else {
+                    dir = bmvtype == BMV_TYPE_BACKWARD;
+                    vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
+                    if (mvsw) {
+                        int dir2 = dir;
+                        if (mvsw)
+                            dir2 = !dir;
+                        for (i = 0; i<2; i++) {
+                            s->mv[dir][i+2][0] = s->mv[dir][i][0] = s->current_picture.motion_val[dir][s->block_index[i+2]][0] = s->current_picture.motion_val[dir][s->block_index[i]][0];
+                            s->mv[dir][i+2][1] = s->mv[dir][i][1] = s->current_picture.motion_val[dir][s->block_index[i+2]][1] = s->current_picture.motion_val[dir][s->block_index[i]][1];
+                            s->mv[dir2][i+2][0] = s->mv[dir2][i][0] = s->current_picture.motion_val[dir2][s->block_index[i]][0] = s->current_picture.motion_val[dir2][s->block_index[i+2]][0];
+                            s->mv[dir2][i+2][1] = s->mv[dir2][i][1] = s->current_picture.motion_val[dir2][s->block_index[i]][1] = s->current_picture.motion_val[dir2][s->block_index[i+2]][1];
+                        }
+                    } else {
+                        v->blk_mv_type[s->block_index[0]] = 1;
+                        v->blk_mv_type[s->block_index[1]] = 1;
+                        v->blk_mv_type[s->block_index[2]] = 1;
+                        v->blk_mv_type[s->block_index[3]] = 1;
+                        vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
+                        for (i = 0; i<2; i++) {
+                            s->mv[!dir][i+2][0] = s->mv[!dir][i][0] = s->current_picture.motion_val[!dir][s->block_index[i+2]][0] = s->current_picture.motion_val[!dir][s->block_index[i]][0];
+                            s->mv[!dir][i+2][1] = s->mv[!dir][i][1] = s->current_picture.motion_val[!dir][s->block_index[i+2]][1] = s->current_picture.motion_val[!dir][s->block_index[i]][1];
+                        }
+                    }
+                }
+            }
+
+            vc1_mc_1mv(v, dir);
+            if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
+                vc1_interp_mc(v);
+            }
+        }
+    }
+    if (s->mb_x == s->mb_width - 1)
+        memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
+    v->cbp[s->mb_x]      = block_cbp;
+    v->ttblk[s->mb_x]    = block_tt;
+    return 0;
+}
+
 /** Decode blocks of I-frame
  */
 static void vc1_decode_i_blocks(VC1Context *v)
@@ -4384,10 +4780,10 @@
             dst[5] = s->dest[2];
             s->dsp.clear_blocks(s->block[0]);
             mb_pos = s->mb_x + s->mb_y * s->mb_width;
-            s->current_picture.f.mb_type[mb_pos]                     = MB_TYPE_INTRA;
-            s->current_picture.f.qscale_table[mb_pos]                = v->pq;
-            s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
-            s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
+            s->current_picture.mb_type[mb_pos]                     = MB_TYPE_INTRA;
+            s->current_picture.qscale_table[mb_pos]                = v->pq;
+            s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
+            s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
 
             // do actual MB decoding and displaying
             cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
@@ -4524,9 +4920,9 @@
             ff_update_block_index(s);
             s->dsp.clear_blocks(block[0]);
             mb_pos = s->mb_x + s->mb_y * s->mb_stride;
-            s->current_picture.f.mb_type[mb_pos + v->mb_off]                         = MB_TYPE_INTRA;
-            s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
-            s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
+            s->current_picture.mb_type[mb_pos + v->mb_off]                         = MB_TYPE_INTRA;
+            s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
+            s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
 
             // do actual MB decoding and displaying
             if (v->fieldtx_is_raw)
@@ -4542,7 +4938,7 @@
 
             GET_MQUANT();
 
-            s->current_picture.f.qscale_table[mb_pos] = mquant;
+            s->current_picture.qscale_table[mb_pos] = mquant;
             /* Set DC scale - y and c use the same */
             s->y_dc_scale = s->y_dc_scale_table[mquant];
             s->c_dc_scale = s->c_dc_scale_table[mquant];
@@ -4716,6 +5112,8 @@
 
             if (v->fcm == ILACE_FIELD)
                 vc1_decode_b_mb_intfi(v);
+            else if (v->fcm == ILACE_FRAME)
+                vc1_decode_b_mb_intfr(v);
             else
                 vc1_decode_b_mb(v);
             if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
@@ -4865,7 +5263,7 @@
     for (sprite = 0; sprite <= v->two_sprites; sprite++) {
         vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
         if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
-            av_log_ask_for_sample(avctx, "Rotation coefficients are not zero");
+            avpriv_request_sample(avctx, "Non-zero rotation coefficients");
         av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
         for (i = 0; i < 7; i++)
             av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
@@ -5023,6 +5421,7 @@
 
 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
 {
+    int ret;
     MpegEncContext *s     = &v->s;
     AVCodecContext *avctx = s->avctx;
     SpriteData sd;
@@ -5039,15 +5438,9 @@
         v->two_sprites = 0;
     }
 
-    if (v->sprite_output_frame.data[0])
-        avctx->release_buffer(avctx, &v->sprite_output_frame);
-
-    v->sprite_output_frame.buffer_hints = FF_BUFFER_HINTS_VALID;
-    v->sprite_output_frame.reference = 0;
-    if (ff_get_buffer(avctx, &v->sprite_output_frame) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
-        return -1;
-    }
+    av_frame_unref(&v->sprite_output_frame);
+    if ((ret = ff_get_buffer(avctx, &v->sprite_output_frame, 0)) < 0)
+        return ret;
 
     vc1_draw_sprites(v, &sd);
 
@@ -5292,6 +5685,11 @@
             v->sprite_height > 1 << 14 ||
             v->output_width  > 1 << 14 ||
             v->output_height > 1 << 14) return -1;
+
+        if ((v->sprite_width&1) || (v->sprite_height&1)) {
+            avpriv_request_sample(avctx, "odd sprites support");
+            return AVERROR_PATCHWELCOME;
+        }
     }
     return 0;
 }
@@ -5304,9 +5702,8 @@
     VC1Context *v = avctx->priv_data;
     int i;
 
-    if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
-        && v->sprite_output_frame.data[0])
-        avctx->release_buffer(avctx, &v->sprite_output_frame);
+    av_frame_unref(&v->sprite_output_frame);
+
     for (i = 0; i < 4; i++)
         av_freep(&v->sr_rows[i >> 1][i & 1]);
     av_freep(&v->hrd_rate);
@@ -5340,7 +5737,7 @@
                             int *got_frame, AVPacket *avpkt)
 {
     const uint8_t *buf = avpkt->data;
-    int buf_size = avpkt->size, n_slices = 0, i;
+    int buf_size = avpkt->size, n_slices = 0, i, ret;
     VC1Context *v = avctx->priv_data;
     MpegEncContext *s = &v->s;
     AVFrame *pict = data;
@@ -5362,7 +5759,8 @@
     if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
         /* special case for last picture */
         if (s->low_delay == 0 && s->next_picture_ptr) {
-            *pict = s->next_picture_ptr->f;
+            if ((ret = av_frame_ref(pict, &s->next_picture_ptr->f)) < 0)
+                return ret;
             s->next_picture_ptr = NULL;
 
             *got_frame = 1;
@@ -5641,28 +6039,17 @@
     } else {
         int header_ret = 0;
 
-        if (v->fcm == ILACE_FRAME && s->pict_type == AV_PICTURE_TYPE_B)
-            goto err; // This codepath is still incomplete thus it is disabled
 
         ff_mpeg_er_frame_start(s);
 
         v->bits = buf_size * 8;
         v->end_mb_x = s->mb_width;
         if (v->field_mode) {
-            uint8_t *tmp[2];
             s->current_picture.f.linesize[0] <<= 1;
             s->current_picture.f.linesize[1] <<= 1;
             s->current_picture.f.linesize[2] <<= 1;
             s->linesize                      <<= 1;
             s->uvlinesize                    <<= 1;
-            tmp[0]          = v->mv_f_last[0];
-            tmp[1]          = v->mv_f_last[1];
-            v->mv_f_last[0] = v->mv_f_next[0];
-            v->mv_f_last[1] = v->mv_f_next[1];
-            v->mv_f_next[0] = v->mv_f[0];
-            v->mv_f_next[1] = v->mv_f[1];
-            v->mv_f[0] = tmp[0];
-            v->mv_f[1] = tmp[1];
         }
         mb_height = s->mb_height >> v->field_mode;
         for (i = 0; i <= n_slices; i++) {
@@ -5722,15 +6109,22 @@
         }
         if (v->field_mode) {
             v->second_field = 0;
-            if (s->pict_type == AV_PICTURE_TYPE_B) {
-                memcpy(v->mv_f_base, v->mv_f_next_base,
-                       2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
-            }
             s->current_picture.f.linesize[0] >>= 1;
             s->current_picture.f.linesize[1] >>= 1;
             s->current_picture.f.linesize[2] >>= 1;
             s->linesize                      >>= 1;
             s->uvlinesize                    >>= 1;
+            if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) {
+                uint8_t *tmp[2];
+                tmp[0]          = v->mv_f_last[0];
+                tmp[1]          = v->mv_f_last[1];
+                v->mv_f_last[0] = v->mv_f_next[0];
+                v->mv_f_last[1] = v->mv_f_next[1];
+                v->mv_f_next[0] = v->mv_f[0];
+                v->mv_f_next[1] = v->mv_f[1];
+                v->mv_f[0] = tmp[0];
+                v->mv_f[1] = tmp[1];
+            }
         }
         av_dlog(s->avctx, "Consumed %i/%i bits\n",
                 get_bits_count(&s->gb), s->gb.size_in_bits);
@@ -5754,17 +6148,21 @@
         if (vc1_decode_sprites(v, &s->gb))
             goto err;
 #endif
-        *pict      = v->sprite_output_frame;
+        if ((ret = av_frame_ref(pict, &v->sprite_output_frame)) < 0)
+            goto err;
         *got_frame = 1;
     } else {
         if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
-            *pict = s->current_picture_ptr->f;
+            if ((ret = av_frame_ref(pict, &s->current_picture_ptr->f)) < 0)
+                goto err;
+            ff_print_debug_info(s, s->current_picture_ptr, pict);
         } else if (s->last_picture_ptr != NULL) {
-            *pict = s->last_picture_ptr->f;
+            if ((ret = av_frame_ref(pict, &s->last_picture_ptr->f)) < 0)
+                goto err;
+            ff_print_debug_info(s, s->last_picture_ptr, pict);
         }
         if (s->last_picture_ptr || s->low_delay) {
             *got_frame = 1;
-            ff_print_debug_info(s, pict);
         }
     }
 
diff --git a/libavcodec/vc1dsp.c b/libavcodec/vc1dsp.c
index 260eda4..f66921a 100644
--- a/libavcodec/vc1dsp.c
+++ b/libavcodec/vc1dsp.c
@@ -565,7 +565,7 @@
 /** Function used to do motion compensation with bicubic interpolation
  */
 #define VC1_MSPEL_MC(OP, OP4, OPNAME)\
-static av_always_inline void OPNAME ## vc1_mspel_mc(uint8_t *dst, const uint8_t *src, int stride, int hmode, int vmode, int rnd)\
+static av_always_inline void OPNAME ## vc1_mspel_mc(uint8_t *dst, const uint8_t *src, ptrdiff_t stride, int hmode, int vmode, int rnd)\
 {\
     int     i, j;\
 \
@@ -619,7 +619,7 @@
         src += stride;\
     }\
 }\
-static void OPNAME ## pixels8x8_c(uint8_t *block, const uint8_t *pixels, int line_size, int rnd){\
+static void OPNAME ## pixels8x8_c(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int rnd){\
     int i;\
     for(i=0; i<8; i++){\
         OP4(*(uint32_t*)(block  ), AV_RN32(pixels  ));\
@@ -640,11 +640,17 @@
 /* pixel functions - really are entry points to vc1_mspel_mc */
 
 #define PUT_VC1_MSPEL(a, b)\
-static void put_vc1_mspel_mc ## a ## b ##_c(uint8_t *dst, const uint8_t *src, int stride, int rnd) { \
-     put_vc1_mspel_mc(dst, src, stride, a, b, rnd);                         \
-}\
-static void avg_vc1_mspel_mc ## a ## b ##_c(uint8_t *dst, const uint8_t *src, int stride, int rnd) { \
-     avg_vc1_mspel_mc(dst, src, stride, a, b, rnd);                         \
+static void put_vc1_mspel_mc ## a ## b ##_c(uint8_t *dst,               \
+                                            const uint8_t *src,         \
+                                            ptrdiff_t stride, int rnd)  \
+{                                                                       \
+    put_vc1_mspel_mc(dst, src, stride, a, b, rnd);                      \
+}                                                                       \
+static void avg_vc1_mspel_mc ## a ## b ##_c(uint8_t *dst,               \
+                                            const uint8_t *src,         \
+                                            ptrdiff_t stride, int rnd)  \
+{                                                                       \
+    avg_vc1_mspel_mc(dst, src, stride, a, b, rnd);                      \
 }
 
 PUT_VC1_MSPEL(1, 0)
diff --git a/libavcodec/vc1dsp.h b/libavcodec/vc1dsp.h
index 6540eff..5e332a0 100644
--- a/libavcodec/vc1dsp.h
+++ b/libavcodec/vc1dsp.h
@@ -29,9 +29,10 @@
 #define AVCODEC_VC1DSP_H
 
 #include "dsputil.h"
+#include "hpeldsp.h"
 #include "h264chroma.h"
 
-typedef void (*vc1op_pixels_func)(uint8_t *block/*align width (8 or 16)*/, const uint8_t *pixels/*align 1*/, int line_size, int h);
+typedef void (*vc1op_pixels_func)(uint8_t *block/*align width (8 or 16)*/, const uint8_t *pixels/*align 1*/, ptrdiff_t line_size, int h);
 
 typedef struct VC1DSPContext {
     /* vc1 functions */
diff --git a/libavcodec/vcr1.c b/libavcodec/vcr1.c
index e9b1a7b..60bfcce 100644
--- a/libavcodec/vcr1.c
+++ b/libavcodec/vcr1.c
@@ -29,68 +29,38 @@
 #include "libavutil/internal.h"
 
 typedef struct VCR1Context {
-    AVFrame picture;
     int delta[16];
     int offset[4];
 } VCR1Context;
 
-static av_cold int vcr1_common_init(AVCodecContext *avctx)
-{
-    VCR1Context *const a = avctx->priv_data;
-
-    avctx->coded_frame = &a->picture;
-    avcodec_get_frame_defaults(&a->picture);
-
-    return 0;
-}
-
 static av_cold int vcr1_decode_init(AVCodecContext *avctx)
 {
-    vcr1_common_init(avctx);
-
     avctx->pix_fmt = AV_PIX_FMT_YUV410P;
 
     if (avctx->width % 8 || avctx->height%4) {
-        av_log_ask_for_sample(avctx, "odd dimensions are not supported\n");
+        avpriv_request_sample(avctx, "odd dimensions support");
         return AVERROR_PATCHWELCOME;
     }
     return 0;
 }
 
-static av_cold int vcr1_decode_end(AVCodecContext *avctx)
-{
-    VCR1Context *s = avctx->priv_data;
-
-    if (s->picture.data[0])
-        avctx->release_buffer(avctx, &s->picture);
-
-    return 0;
-}
-
 static int vcr1_decode_frame(AVCodecContext *avctx, void *data,
                              int *got_frame, AVPacket *avpkt)
 {
     const uint8_t *buf        = avpkt->data;
     int buf_size              = avpkt->size;
     VCR1Context *const a      = avctx->priv_data;
-    AVFrame *picture          = data;
-    AVFrame *const p          = &a->picture;
+    AVFrame *const p          = data;
     const uint8_t *bytestream = buf;
     int i, x, y, ret;
 
-    if (p->data[0])
-        avctx->release_buffer(avctx, p);
-
     if(buf_size < 16 + avctx->height + avctx->width*avctx->height*5/8){
         av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
         return AVERROR(EINVAL);
     }
 
-    p->reference = 0;
-    if ((ret = ff_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
         return ret;
-    }
     p->pict_type = AV_PICTURE_TYPE_I;
     p->key_frame = 1;
 
@@ -101,11 +71,11 @@
 
     for (y = 0; y < avctx->height; y++) {
         int offset;
-        uint8_t *luma = &a->picture.data[0][y * a->picture.linesize[0]];
+        uint8_t *luma = &p->data[0][y * p->linesize[0]];
 
         if ((y & 3) == 0) {
-            uint8_t *cb = &a->picture.data[1][(y >> 2) * a->picture.linesize[1]];
-            uint8_t *cr = &a->picture.data[2][(y >> 2) * a->picture.linesize[2]];
+            uint8_t *cb = &p->data[1][(y >> 2) * p->linesize[1]];
+            uint8_t *cr = &p->data[2][(y >> 2) * p->linesize[2]];
 
             for (i = 0; i < 4; i++)
                 a->offset[i] = *bytestream++;
@@ -141,7 +111,6 @@
         }
     }
 
-    *picture   = a->picture;
     *got_frame = 1;
 
     return buf_size;
@@ -153,7 +122,6 @@
     .id             = AV_CODEC_ID_VCR1,
     .priv_data_size = sizeof(VCR1Context),
     .init           = vcr1_decode_init,
-    .close          = vcr1_decode_end,
     .decode         = vcr1_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("ATI VCR1"),
diff --git a/libavcodec/vda.h b/libavcodec/vda.h
index de7070e..281785f 100644
--- a/libavcodec/vda.h
+++ b/libavcodec/vda.h
@@ -41,10 +41,6 @@
 
 #include "libavcodec/version.h"
 
-#if FF_API_VDA_ASYNC
-#include <pthread.h>
-#endif
-
 /**
  * @defgroup lavc_codec_hwaccel_vda VDA
  * @ingroup lavc_codec_hwaccel
@@ -52,39 +48,6 @@
  * @{
  */
 
-#if FF_API_VDA_ASYNC
-/**
- * This structure is used to store decoded frame information and data.
- *
- * @deprecated Use synchronous decoding mode.
- */
-typedef struct {
-    /**
-     * The PTS of the frame.
-     *
-     * - encoding: unused
-     * - decoding: Set/Unset by libavcodec.
-     */
-    int64_t             pts;
-
-    /**
-     * The CoreVideo buffer that contains the decoded data.
-     *
-     * - encoding: unused
-     * - decoding: Set/Unset by libavcodec.
-     */
-    CVPixelBufferRef    cv_buffer;
-
-    /**
-     * A pointer to the next frame.
-     *
-     * - encoding: unused
-     * - decoding: Set/Unset by libavcodec.
-     */
-    struct vda_frame    *next_frame;
-} vda_frame;
-#endif
-
 /**
  * This structure is used to provide the necessary configurations and data
  * to the VDA FFmpeg HWAccel implementation.
@@ -116,28 +79,6 @@
      */
     int                 use_sync_decoding;
 
-#if FF_API_VDA_ASYNC
-    /**
-     * VDA frames queue ordered by presentation timestamp.
-     *
-     * @deprecated Use synchronous decoding mode.
-     *
-     * - encoding: unused
-     * - decoding: Set/Unset by libavcodec.
-     */
-    vda_frame           *queue;
-
-    /**
-     * Mutex for locking queue operations.
-     *
-     * @deprecated Use synchronous decoding mode.
-     *
-     * - encoding: unused
-     * - decoding: Set/Unset by libavcodec.
-     */
-    pthread_mutex_t     queue_mutex;
-#endif
-
     /**
      * The frame width.
      *
@@ -203,22 +144,6 @@
 /** Destroy the video decoder. */
 int ff_vda_destroy_decoder(struct vda_context *vda_ctx);
 
-#if FF_API_VDA_ASYNC
-/**
- * Return the top frame of the queue.
- *
- * @deprecated Use synchronous decoding mode.
- */
-vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx);
-
-/**
- * Release the given frame.
- *
- * @deprecated Use synchronous decoding mode.
- */
-void ff_vda_release_vda_frame(vda_frame *frame);
-#endif
-
 /**
  * @}
  */
diff --git a/libavcodec/vda_h264.c b/libavcodec/vda_h264.c
index c24f4b1..d0237c2 100644
--- a/libavcodec/vda_h264.c
+++ b/libavcodec/vda_h264.c
@@ -28,100 +28,6 @@
 #include "libavutil/avutil.h"
 #include "h264.h"
 
-#if FF_API_VDA_ASYNC
-#include <CoreFoundation/CFString.h>
-
-/* Helper to create a dictionary according to the given pts. */
-static CFDictionaryRef vda_dictionary_with_pts(int64_t i_pts)
-{
-    CFStringRef key = CFSTR("FF_VDA_DECODER_PTS_KEY");
-    CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &i_pts);
-    CFDictionaryRef user_info = CFDictionaryCreate(kCFAllocatorDefault,
-                                                   (const void **)&key,
-                                                   (const void **)&value,
-                                                   1,
-                                                   &kCFTypeDictionaryKeyCallBacks,
-                                                   &kCFTypeDictionaryValueCallBacks);
-    CFRelease(value);
-    return user_info;
-}
-
-/* Helper to retrieve the pts from the given dictionary. */
-static int64_t vda_pts_from_dictionary(CFDictionaryRef user_info)
-{
-    CFNumberRef pts;
-    int64_t outValue = 0;
-
-    if (!user_info)
-        return 0;
-
-    pts = CFDictionaryGetValue(user_info, CFSTR("FF_VDA_DECODER_PTS_KEY"));
-
-    if (pts)
-        CFNumberGetValue(pts, kCFNumberSInt64Type, &outValue);
-
-    return outValue;
-}
-
-/* Removes and releases all frames from the queue. */
-static void vda_clear_queue(struct vda_context *vda_ctx)
-{
-    vda_frame *top_frame;
-
-    pthread_mutex_lock(&vda_ctx->queue_mutex);
-
-    while (vda_ctx->queue) {
-        top_frame = vda_ctx->queue;
-        vda_ctx->queue = top_frame->next_frame;
-        ff_vda_release_vda_frame(top_frame);
-    }
-
-    pthread_mutex_unlock(&vda_ctx->queue_mutex);
-}
-
-static int vda_decoder_decode(struct vda_context *vda_ctx,
-                              uint8_t *bitstream,
-                              int bitstream_size,
-                              int64_t frame_pts)
-{
-    OSStatus status;
-    CFDictionaryRef user_info;
-    CFDataRef coded_frame;
-
-    coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
-    user_info = vda_dictionary_with_pts(frame_pts);
-
-    status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
-
-    CFRelease(user_info);
-    CFRelease(coded_frame);
-
-    return status;
-}
-
-vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
-{
-    vda_frame *top_frame;
-
-    if (!vda_ctx->queue)
-        return NULL;
-
-    pthread_mutex_lock(&vda_ctx->queue_mutex);
-    top_frame = vda_ctx->queue;
-    vda_ctx->queue = top_frame->next_frame;
-    pthread_mutex_unlock(&vda_ctx->queue_mutex);
-
-    return top_frame;
-}
-
-void ff_vda_release_vda_frame(vda_frame *frame)
-{
-    if (frame) {
-        CVPixelBufferRelease(frame->cv_buffer);
-        av_freep(&frame);
-    }
-}
-#endif
 
 /* Decoder callback that adds the vda frame to the queue in display order. */
 static void vda_decoder_callback (void *vda_hw_ctx,
@@ -138,45 +44,7 @@
     if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
         return;
 
-    if (vda_ctx->use_sync_decoding) {
-        vda_ctx->cv_buffer = CVPixelBufferRetain(image_buffer);
-    } else {
-        vda_frame *new_frame;
-        vda_frame *queue_walker;
-
-        if (!(new_frame = av_mallocz(sizeof(*new_frame))))
-            return;
-
-        new_frame->next_frame = NULL;
-        new_frame->cv_buffer = CVPixelBufferRetain(image_buffer);
-        new_frame->pts = vda_pts_from_dictionary(user_info);
-
-        pthread_mutex_lock(&vda_ctx->queue_mutex);
-
-        queue_walker = vda_ctx->queue;
-
-        if (!queue_walker || (new_frame->pts < queue_walker->pts)) {
-            /* we have an empty queue, or this frame earlier than the current queue head */
-            new_frame->next_frame = queue_walker;
-            vda_ctx->queue = new_frame;
-        } else {
-            /* walk the queue and insert this frame where it belongs in display order */
-            vda_frame *next_frame;
-
-            while (1) {
-                next_frame = queue_walker->next_frame;
-
-                if (!next_frame || (new_frame->pts < next_frame->pts)) {
-                    new_frame->next_frame = next_frame;
-                    queue_walker->next_frame = new_frame;
-                    break;
-                }
-                queue_walker = next_frame;
-            }
-        }
-
-        pthread_mutex_unlock(&vda_ctx->queue_mutex);
-    }
+    vda_ctx->cv_buffer = CVPixelBufferRetain(image_buffer);
 }
 
 static int vda_sync_decode(struct vda_context *vda_ctx)
@@ -199,9 +67,10 @@
     return status;
 }
 
-static int start_frame(AVCodecContext *avctx,
-                       av_unused const uint8_t *buffer,
-                       av_unused uint32_t size)
+
+static int vda_h264_start_frame(AVCodecContext *avctx,
+                                av_unused const uint8_t *buffer,
+                                av_unused uint32_t size)
 {
     struct vda_context *vda_ctx = avctx->hwaccel_context;
 
@@ -213,9 +82,9 @@
     return 0;
 }
 
-static int decode_slice(AVCodecContext *avctx,
-                        const uint8_t *buffer,
-                        uint32_t size)
+static int vda_h264_decode_slice(AVCodecContext *avctx,
+                                 const uint8_t *buffer,
+                                 uint32_t size)
 {
     struct vda_context *vda_ctx = avctx->hwaccel_context;
     void *tmp;
@@ -239,7 +108,7 @@
     return 0;
 }
 
-static int end_frame(AVCodecContext *avctx)
+static int vda_h264_end_frame(AVCodecContext *avctx)
 {
     H264Context *h                      = avctx->priv_data;
     struct vda_context *vda_ctx         = avctx->hwaccel_context;
@@ -249,14 +118,8 @@
     if (!vda_ctx->decoder || !vda_ctx->priv_bitstream)
         return -1;
 
-    if (vda_ctx->use_sync_decoding) {
-        status = vda_sync_decode(vda_ctx);
-        frame->data[3] = (void*)vda_ctx->cv_buffer;
-    } else {
-        status = vda_decoder_decode(vda_ctx, vda_ctx->priv_bitstream,
-                                    vda_ctx->priv_bitstream_size,
-                                    frame->reordered_opaque);
-    }
+    status = vda_sync_decode(vda_ctx);
+    frame->data[3] = (void*)vda_ctx->cv_buffer;
 
     if (status)
         av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
@@ -281,10 +144,6 @@
     vda_ctx->priv_bitstream = NULL;
     vda_ctx->priv_allocated_size = 0;
 
-#if FF_API_VDA_ASYNC
-    pthread_mutex_init(&vda_ctx->queue_mutex, NULL);
-#endif
-
     /* Each VCL NAL in the bitstream sent to the decoder
      * is preceded by a 4 bytes length header.
      * Change the avcC atom header if needed, to signal headers of 4 bytes. */
@@ -362,10 +221,6 @@
     if (vda_ctx->decoder)
         status = VDADecoderDestroy(vda_ctx->decoder);
 
-#if FF_API_VDA_ASYNC
-    vda_clear_queue(vda_ctx);
-    pthread_mutex_destroy(&vda_ctx->queue_mutex);
-#endif
     av_freep(&vda_ctx->priv_bitstream);
 
     return status;
@@ -376,7 +231,7 @@
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_H264,
     .pix_fmt        = AV_PIX_FMT_VDA_VLD,
-    .start_frame    = start_frame,
-    .decode_slice   = decode_slice,
-    .end_frame      = end_frame,
+    .start_frame    = vda_h264_start_frame,
+    .decode_slice   = vda_h264_decode_slice,
+    .end_frame      = vda_h264_end_frame,
 };
diff --git a/libavcodec/vda_h264_dec.c b/libavcodec/vda_h264_dec.c
index d6c8f37..4e60de0 100644
--- a/libavcodec/vda_h264_dec.c
+++ b/libavcodec/vda_h264_dec.c
@@ -64,23 +64,30 @@
     return AV_PIX_FMT_VDA_VLD;
 }
 
-static int get_buffer(AVCodecContext *avctx, AVFrame *pic)
+typedef struct {
+    CVPixelBufferRef cv_buffer;
+} VDABufferContext;
+
+static void release_buffer(void *opaque, uint8_t *data)
 {
-    pic->type = FF_BUFFER_TYPE_USER;
-    pic->data[0] = (void *)1;
-    return 0;
+    VDABufferContext *context = opaque;
+    CVPixelBufferUnlockBaseAddress(context->cv_buffer, 0);
+    CVPixelBufferRelease(context->cv_buffer);
+    av_free(context);
 }
 
-static void release_buffer(AVCodecContext *avctx, AVFrame *pic)
+static int get_buffer2(AVCodecContext *avctx, AVFrame *pic, int flag)
 {
-    int i;
+    VDABufferContext *context = av_mallocz(sizeof(VDABufferContext));
+    AVBufferRef *buffer = av_buffer_create(NULL, 0, release_buffer, context, 0);
+    if (!context || !buffer) {
+        av_free(context);
+        return AVERROR(ENOMEM);
+    }
 
-    CVPixelBufferRef cv_buffer = (CVPixelBufferRef)pic->data[3];
-    CVPixelBufferUnlockBaseAddress(cv_buffer, 0);
-    CVPixelBufferRelease(cv_buffer);
-
-    for (i = 0; i < 4; i++)
-        pic->data[i] = NULL;
+    pic->buf[0] = buffer;
+    pic->data[0] = (void *)1;
+    return 0;
 }
 
 static int vdadec_decode(AVCodecContext *avctx,
@@ -92,8 +99,11 @@
 
     ret = ff_h264_decoder.decode(avctx, data, got_frame, avpkt);
     if (*got_frame) {
+        AVBufferRef *buffer = pic->buf[0];
+        VDABufferContext *context = av_buffer_get_opaque(buffer);
         CVPixelBufferRef cv_buffer = (CVPixelBufferRef)pic->data[3];
         CVPixelBufferLockBaseAddress(cv_buffer, 0);
+        context->cv_buffer = cv_buffer;
         pic->format = ctx->pix_fmt;
         if (CVPixelBufferIsPlanar(cv_buffer)) {
             int i, count = CVPixelBufferGetPlaneCount(cv_buffer);
@@ -149,7 +159,7 @@
     h = parser->priv_data;
     switch (h->sps.bit_depth_luma) {
     case 8:
-        if (!CHROMA444 && !CHROMA422) {
+        if (!CHROMA444(h) && !CHROMA422(h)) {
             // only this will H.264 decoder switch to hwaccel
             ret = 0;
             break;
@@ -221,8 +231,11 @@
 
     /* changes callback functions */
     avctx->get_format = get_format;
-    avctx->get_buffer = get_buffer;
-    avctx->release_buffer = release_buffer;
+    avctx->get_buffer2 = get_buffer2;
+#if FF_API_GET_BUFFER
+    // force the old get_buffer to be empty
+    avctx->get_buffer = NULL;
+#endif
 
     /* init H.264 decoder */
     ret = ff_h264_decoder.init(avctx);
diff --git a/libavcodec/vdpau.c b/libavcodec/vdpau.c
index 6df7f4a..b9a07cd 100644
--- a/libavcodec/vdpau.c
+++ b/libavcodec/vdpau.c
@@ -48,6 +48,9 @@
     return 0;
 }
 
+#if CONFIG_H263_VDPAU_HWACCEL  || CONFIG_MPEG1_VDPAU_HWACCEL || \
+    CONFIG_MPEG2_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL || \
+    CONFIG_VC1_VDPAU_HWACCEL   || CONFIG_WMV3_VDPAU_HWACCEL
 int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx)
 {
     AVVDPAUContext *hwctx = avctx->hwaccel_context;
@@ -62,6 +65,7 @@
 
     return 0;
 }
+#endif
 
 int ff_vdpau_add_buffer(AVCodecContext *avctx,
                         const uint8_t *buf, uint32_t size)
@@ -104,7 +108,7 @@
 
         for (i = 0; i < ls; ++i) {
             pic = lp[i];
-            if (!pic || !pic->f.reference)
+            if (!pic || !pic->reference)
                 continue;
             pic_frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
 
@@ -122,8 +126,8 @@
                 ++rf2;
             }
             if (rf2 != rf) {
-                rf2->top_is_reference    |= (pic->f.reference & PICT_TOP_FIELD)    ? VDP_TRUE : VDP_FALSE;
-                rf2->bottom_is_reference |= (pic->f.reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
+                rf2->top_is_reference    |= (pic->reference & PICT_TOP_FIELD)    ? VDP_TRUE : VDP_FALSE;
+                rf2->bottom_is_reference |= (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
                 continue;
             }
 
@@ -132,8 +136,8 @@
 
             rf->surface             = render_ref->surface;
             rf->is_long_term        = pic->long_ref;
-            rf->top_is_reference    = (pic->f.reference & PICT_TOP_FIELD)    ? VDP_TRUE : VDP_FALSE;
-            rf->bottom_is_reference = (pic->f.reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
+            rf->top_is_reference    = (pic->reference & PICT_TOP_FIELD)    ? VDP_TRUE : VDP_FALSE;
+            rf->bottom_is_reference = (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
             rf->field_order_cnt[0]  = pic->field_poc[0];
             rf->field_order_cnt[1]  = pic->field_poc[1];
             rf->frame_idx           = pic_frame_idx;
@@ -170,6 +174,7 @@
     render->bitstream_buffers_used++;
 }
 
+#if CONFIG_H264_VDPAU_DECODER
 void ff_vdpau_h264_picture_start(H264Context *h)
 {
     struct vdpau_render_state *render;
@@ -199,7 +204,7 @@
     if (render->info.h264.slice_count < 1)
         return;
 
-    render->info.h264.is_reference                           = (h->cur_pic_ptr->f.reference & 3) ? VDP_TRUE : VDP_FALSE;
+    render->info.h264.is_reference                           = (h->cur_pic_ptr->reference & 3) ? VDP_TRUE : VDP_FALSE;
     render->info.h264.field_pic_flag                         = h->picture_structure != PICT_FRAME;
     render->info.h264.bottom_field_flag                      = h->picture_structure == PICT_BOTTOM_FIELD;
     render->info.h264.num_ref_frames                         = h->sps.ref_frame_count;
@@ -230,7 +235,9 @@
     ff_h264_draw_horiz_band(h, 0, h->avctx->height);
     render->bitstream_buffers_used = 0;
 }
+#endif /* CONFIG_H264_VDPAU_DECODER */
 
+#if CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER
 void ff_vdpau_mpeg_picture_complete(MpegEncContext *s, const uint8_t *buf,
                                     int buf_size, int slice_count)
 {
@@ -287,7 +294,9 @@
         ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
     render->bitstream_buffers_used               = 0;
 }
+#endif /* CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER */
 
+#if CONFIG_VC1_VDPAU_DECODER
 void ff_vdpau_vc1_decode_picture(MpegEncContext *s, const uint8_t *buf,
                                  int buf_size)
 {
@@ -356,7 +365,9 @@
     ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
     render->bitstream_buffers_used        = 0;
 }
+#endif /* (CONFIG_VC1_VDPAU_DECODER */
 
+#if CONFIG_MPEG4_VDPAU_DECODER
 void ff_vdpau_mpeg4_decode_picture(MpegEncContext *s, const uint8_t *buf,
                                    int buf_size)
 {
@@ -410,5 +421,6 @@
     ff_mpeg_draw_horiz_band(s, 0, s->avctx->height);
     render->bitstream_buffers_used = 0;
 }
+#endif /* CONFIG_MPEG4_VDPAU_DECODER */
 
 /* @}*/
diff --git a/libavcodec/vdpau.h b/libavcodec/vdpau.h
index df2aace..f5b2317 100644
--- a/libavcodec/vdpau.h
+++ b/libavcodec/vdpau.h
@@ -51,6 +51,7 @@
 
 #include <vdpau/vdpau.h>
 #include <vdpau/vdpau_x11.h>
+#include "libavutil/avconfig.h"
 
 union FFVdpPictureInfo {
     VdpPictureInfoH264        h264;
@@ -135,6 +136,11 @@
 
     int state; ///< Holds FF_VDPAU_STATE_* values.
 
+#if AV_HAVE_INCOMPATIBLE_FORK_ABI
+    /** picture parameter information for all supported codecs */
+    union FFVdpPictureInfo info;
+#endif
+
     /** Describe size/location of the compressed video data.
         Set to 0 when freeing bitstream_buffers. */
     int bitstream_buffers_allocated;
@@ -142,8 +148,10 @@
     /** The user is responsible for freeing this buffer using av_freep(). */
     VdpBitstreamBuffer *bitstream_buffers;
 
+#if !AV_HAVE_INCOMPATIBLE_FORK_ABI
     /** picture parameter information for all supported codecs */
     union FFVdpPictureInfo info;
+#endif
 };
 
 /* @}*/
diff --git a/libavcodec/vdpau_h264.c b/libavcodec/vdpau_h264.c
index 0f79c58..ad1aff0 100644
--- a/libavcodec/vdpau_h264.c
+++ b/libavcodec/vdpau_h264.c
@@ -52,10 +52,10 @@
     VdpVideoSurface surface = ff_vdpau_get_surface_id(pic);
 
     if (pic_structure == 0)
-        pic_structure = pic->f.reference;
+        pic_structure = pic->reference;
 
     rf->surface             = surface;
-    rf->is_long_term        = pic->f.reference && pic->long_ref;
+    rf->is_long_term        = pic->reference && pic->long_ref;
     rf->top_is_reference    = (pic_structure & PICT_TOP_FIELD)    != 0;
     rf->bottom_is_reference = (pic_structure & PICT_BOTTOM_FIELD) != 0;
     rf->field_order_cnt[0]  = h264_foc(pic->field_poc[0]);
@@ -83,7 +83,7 @@
             VdpVideoSurface surface_ref;
             int pic_frame_idx;
 
-            if (!pic || !pic->f.reference)
+            if (!pic || !pic->reference)
                 continue;
             pic_frame_idx = pic->long_ref ? pic->pic_id : pic->frame_num;
             surface_ref = ff_vdpau_get_surface_id(pic);
@@ -97,15 +97,15 @@
                 ++rf2;
             }
             if (rf2 != rf) {
-                rf2->top_is_reference    |= (pic->f.reference & PICT_TOP_FIELD)    ? VDP_TRUE : VDP_FALSE;
-                rf2->bottom_is_reference |= (pic->f.reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
+                rf2->top_is_reference    |= (pic->reference & PICT_TOP_FIELD)    ? VDP_TRUE : VDP_FALSE;
+                rf2->bottom_is_reference |= (pic->reference & PICT_BOTTOM_FIELD) ? VDP_TRUE : VDP_FALSE;
                 continue;
             }
 
             if (rf >= &info->referenceFrames[H264_RF_COUNT])
                 continue;
 
-            vdpau_h264_set_rf(rf, pic, pic->f.reference);
+            vdpau_h264_set_rf(rf, pic, pic->reference);
             ++rf;
         }
     }
diff --git a/libavcodec/version.h b/libavcodec/version.h
index dceeaa4..d6f7b8e 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -28,8 +28,8 @@
 
 #include "libavutil/avutil.h"
 
-#define LIBAVCODEC_VERSION_MAJOR 54
-#define LIBAVCODEC_VERSION_MINOR 92
+#define LIBAVCODEC_VERSION_MAJOR 55
+#define LIBAVCODEC_VERSION_MINOR  6
 #define LIBAVCODEC_VERSION_MICRO 100
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
@@ -49,7 +49,7 @@
  */
 
 #ifndef FF_API_REQUEST_CHANNELS
-#define FF_API_REQUEST_CHANNELS (LIBAVCODEC_VERSION_MAJOR < 55)
+#define FF_API_REQUEST_CHANNELS (LIBAVCODEC_VERSION_MAJOR < 56)
 #endif
 #ifndef FF_API_ALLOC_CONTEXT
 #define FF_API_ALLOC_CONTEXT    (LIBAVCODEC_VERSION_MAJOR < 55)
@@ -58,53 +58,35 @@
 #define FF_API_AVCODEC_OPEN     (LIBAVCODEC_VERSION_MAJOR < 55)
 #endif
 #ifndef FF_API_OLD_DECODE_AUDIO
-#define FF_API_OLD_DECODE_AUDIO (LIBAVCODEC_VERSION_MAJOR < 55)
+#define FF_API_OLD_DECODE_AUDIO (LIBAVCODEC_VERSION_MAJOR < 56)
 #endif
 #ifndef FF_API_OLD_TIMECODE
 #define FF_API_OLD_TIMECODE (LIBAVCODEC_VERSION_MAJOR < 55)
 #endif
 
 #ifndef FF_API_OLD_ENCODE_AUDIO
-#define FF_API_OLD_ENCODE_AUDIO (LIBAVCODEC_VERSION_MAJOR < 55)
+#define FF_API_OLD_ENCODE_AUDIO (LIBAVCODEC_VERSION_MAJOR < 56)
 #endif
 #ifndef FF_API_OLD_ENCODE_VIDEO
-#define FF_API_OLD_ENCODE_VIDEO (LIBAVCODEC_VERSION_MAJOR < 55)
-#endif
-#ifndef FF_API_MPV_GLOBAL_OPTS
-#define FF_API_MPV_GLOBAL_OPTS  (LIBAVCODEC_VERSION_MAJOR < 55)
-#endif
-#ifndef FF_API_COLOR_TABLE_ID
-#define FF_API_COLOR_TABLE_ID   (LIBAVCODEC_VERSION_MAJOR < 55)
-#endif
-#ifndef FF_API_INTER_THRESHOLD
-#define FF_API_INTER_THRESHOLD  (LIBAVCODEC_VERSION_MAJOR < 55)
-#endif
-#ifndef FF_API_SUB_ID
-#define FF_API_SUB_ID           (LIBAVCODEC_VERSION_MAJOR < 55)
-#endif
-#ifndef FF_API_DSP_MASK
-#define FF_API_DSP_MASK         (LIBAVCODEC_VERSION_MAJOR < 55)
-#endif
-#ifndef FF_API_FIND_BEST_PIX_FMT
-#define FF_API_FIND_BEST_PIX_FMT (LIBAVCODEC_VERSION_MAJOR < 55)
+#define FF_API_OLD_ENCODE_VIDEO (LIBAVCODEC_VERSION_MAJOR < 56)
 #endif
 #ifndef FF_API_CODEC_ID
-#define FF_API_CODEC_ID          (LIBAVCODEC_VERSION_MAJOR < 55)
-#endif
-#ifndef FF_API_VDA_ASYNC
-#define FF_API_VDA_ASYNC         (LIBAVCODEC_VERSION_MAJOR < 55)
+#define FF_API_CODEC_ID          (LIBAVCODEC_VERSION_MAJOR < 56)
 #endif
 #ifndef FF_API_AVCODEC_RESAMPLE
-#define FF_API_AVCODEC_RESAMPLE  (LIBAVCODEC_VERSION_MAJOR < 55)
+#define FF_API_AVCODEC_RESAMPLE  (LIBAVCODEC_VERSION_MAJOR < 56)
 #endif
-#ifndef FF_API_LIBMPEG2
-#define FF_API_LIBMPEG2          (LIBAVCODEC_VERSION_MAJOR < 55)
+#ifndef FF_API_DEINTERLACE
+#define FF_API_DEINTERLACE       (LIBAVCODEC_VERSION_MAJOR < 56)
 #endif
-#ifndef FF_API_MMI
-#define FF_API_MMI               (LIBAVCODEC_VERSION_MAJOR < 55)
+#ifndef FF_API_DESTRUCT_PACKET
+#define FF_API_DESTRUCT_PACKET   (LIBAVCODEC_VERSION_MAJOR < 56)
 #endif
-#ifndef FF_API_IDCT
-#define FF_API_IDCT              (LIBAVCODEC_VERSION_MAJOR < 55)
+#ifndef FF_API_GET_BUFFER
+#define FF_API_GET_BUFFER        (LIBAVCODEC_VERSION_MAJOR < 56)
+#endif
+#ifndef FF_API_MISSING_SAMPLE
+#define FF_API_MISSING_SAMPLE    (LIBAVCODEC_VERSION_MAJOR < 56)
 #endif
 
 #endif /* AVCODEC_VERSION_H */
diff --git a/libavcodec/vima.c b/libavcodec/vima.c
index 604e0ce..705839e 100644
--- a/libavcodec/vima.c
+++ b/libavcodec/vima.c
@@ -170,10 +170,8 @@
     }
 
     frame->nb_samples = samples;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     for (chan = 0; chan < channels; chan++) {
         uint16_t *dest = (uint16_t*)frame->data[0] + chan;
diff --git a/libavcodec/vmdav.c b/libavcodec/vmdav.c
index d7f136c..867d8c76 100644
--- a/libavcodec/vmdav.c
+++ b/libavcodec/vmdav.c
@@ -48,6 +48,7 @@
 #include "libavutil/intreadwrite.h"
 #include "avcodec.h"
 #include "internal.h"
+#include "bytestream.h"
 
 #define VMD_HEADER_SIZE 0x330
 #define PALETTE_COUNT 256
@@ -59,7 +60,6 @@
 typedef struct VmdVideoContext {
 
     AVCodecContext *avctx;
-    AVFrame frame;
     AVFrame prev_frame;
 
     const unsigned char *buf;
@@ -78,8 +78,6 @@
 static void lz_unpack(const unsigned char *src, int src_len,
                       unsigned char *dest, int dest_len)
 {
-    const unsigned char *s;
-    const unsigned char *s_end;
     unsigned char *d;
     unsigned char *d_end;
     unsigned char queue[QUEUE_SIZE];
@@ -90,19 +88,17 @@
     unsigned int speclen;
     unsigned char tag;
     unsigned int i, j;
+    GetByteContext gb;
 
-    s = src;
-    s_end = src + src_len;
+    bytestream2_init(&gb, src, src_len);
     d = dest;
     d_end = d + dest_len;
-
-    if (s_end - s < 8)
-        return;
-    dataleft = AV_RL32(s);
-    s += 4;
+    dataleft = bytestream2_get_le32(&gb);
     memset(queue, 0x20, QUEUE_SIZE);
-    if (AV_RL32(s) == 0x56781234) {
-        s += 4;
+    if (bytestream2_get_bytes_left(&gb) < 4)
+        return;
+    if (bytestream2_peek_le32(&gb) == 0x56781234) {
+        bytestream2_skipu(&gb, 4);
         qpos = 0x111;
         speclen = 0xF + 3;
     } else {
@@ -110,13 +106,13 @@
         speclen = 100;  /* no speclen */
     }
 
-    while (s_end - s > 0 && dataleft > 0) {
-        tag = *s++;
+    while (dataleft > 0 && bytestream2_get_bytes_left(&gb) > 0) {
+        tag = bytestream2_get_byteu(&gb);
         if ((tag == 0xFF) && (dataleft > 8)) {
-            if (d_end - d < 8 || s_end - s < 8)
+            if (d_end - d < 8 || bytestream2_get_bytes_left(&gb) < 8)
                 return;
             for (i = 0; i < 8; i++) {
-                queue[qpos++] = *d++ = *s++;
+                queue[qpos++] = *d++ = bytestream2_get_byteu(&gb);
                 qpos &= QUEUE_MASK;
             }
             dataleft -= 8;
@@ -125,21 +121,17 @@
                 if (dataleft == 0)
                     break;
                 if (tag & 0x01) {
-                    if (d_end - d < 1 || s_end - s < 1)
+                    if (d_end - d < 1 || bytestream2_get_bytes_left(&gb) < 1)
                         return;
-                    queue[qpos++] = *d++ = *s++;
+                    queue[qpos++] = *d++ = bytestream2_get_byteu(&gb);
                     qpos &= QUEUE_MASK;
                     dataleft--;
                 } else {
-                    if (s_end - s < 2)
-                        return;
-                    chainofs = *s++;
-                    chainofs |= ((*s & 0xF0) << 4);
-                    chainlen = (*s++ & 0x0F) + 3;
+                    chainofs = bytestream2_get_byte(&gb);
+                    chainofs |= ((bytestream2_peek_byte(&gb) & 0xF0) << 4);
+                    chainlen = (bytestream2_get_byte(&gb) & 0x0F) + 3;
                     if (chainlen == speclen) {
-                        if (s_end - s < 1)
-                            return;
-                        chainlen = *s++ + 0xF + 3;
+                        chainlen = bytestream2_get_byte(&gb) + 0xF + 3;
                     }
                     if (d_end - d < chainlen)
                         return;
@@ -155,65 +147,57 @@
         }
     }
 }
-
-static int rle_unpack(const unsigned char *src, int src_len, int src_count,
-                      unsigned char *dest, int dest_len)
+static int rle_unpack(const unsigned char *src, unsigned char *dest,
+                      int src_count, int src_size, int dest_len)
 {
-    const unsigned char *ps;
-    const unsigned char *ps_end;
     unsigned char *pd;
     int i, l;
     unsigned char *dest_end = dest + dest_len;
+    GetByteContext gb;
 
-    ps = src;
-    ps_end = src + src_len;
+    bytestream2_init(&gb, src, src_size);
     pd = dest;
     if (src_count & 1) {
-        if (ps_end - ps < 1)
+        if (bytestream2_get_bytes_left(&gb) < 1)
             return 0;
-        *pd++ = *ps++;
+        *pd++ = bytestream2_get_byteu(&gb);
     }
 
     src_count >>= 1;
     i = 0;
     do {
-        if (ps_end - ps < 1)
+        if (bytestream2_get_bytes_left(&gb) < 1)
             break;
-        l = *ps++;
+        l = bytestream2_get_byteu(&gb);
         if (l & 0x80) {
             l = (l & 0x7F) * 2;
-            if (dest_end - pd < l || ps_end - ps < l)
-                return ps - src;
-            memcpy(pd, ps, l);
-            ps += l;
+            if (dest_end - pd < l || bytestream2_get_bytes_left(&gb) < l)
+                return bytestream2_tell(&gb);
+            bytestream2_get_bufferu(&gb, pd, l);
             pd += l;
         } else {
-            if (dest_end - pd < i || ps_end - ps < 2)
-                return ps - src;
+            if (dest_end - pd < i || bytestream2_get_bytes_left(&gb) < 2)
+                return bytestream2_tell(&gb);
             for (i = 0; i < l; i++) {
-                *pd++ = ps[0];
-                *pd++ = ps[1];
+                *pd++ = bytestream2_get_byteu(&gb);
+                *pd++ = bytestream2_get_byteu(&gb);
             }
-            ps += 2;
+            bytestream2_skip(&gb, 2);
         }
         i += l;
     } while (i < src_count);
 
-    return ps - src;
+    return bytestream2_tell(&gb);
 }
 
-static void vmd_decode(VmdVideoContext *s)
+static void vmd_decode(VmdVideoContext *s, AVFrame *frame)
 {
     int i;
     unsigned int *palette32;
     unsigned char r, g, b;
 
-    /* point to the start of the encoded data */
-    const unsigned char *p = s->buf + 16;
-    const unsigned char *p_end = s->buf + s->size;
+    GetByteContext gb;
 
-    const unsigned char *pb;
-    const unsigned char *pb_end;
     unsigned char meth;
     unsigned char *dp;   /* pointer to current frame */
     unsigned char *pp;   /* pointer to previous frame */
@@ -253,52 +237,51 @@
         (frame_x || frame_y || (frame_width != s->avctx->width) ||
         (frame_height != s->avctx->height))) {
 
-        memcpy(s->frame.data[0], s->prev_frame.data[0],
-            s->avctx->height * s->frame.linesize[0]);
+        memcpy(frame->data[0], s->prev_frame.data[0],
+            s->avctx->height * frame->linesize[0]);
     }
 
     /* check if there is a new palette */
+    bytestream2_init(&gb, s->buf + 16, s->size - 16);
     if (s->buf[15] & 0x02) {
-        if (p_end - p < 2 + 3 * PALETTE_COUNT)
-            return;
-        p += 2;
+        bytestream2_skip(&gb, 2);
         palette32 = (unsigned int *)s->palette;
-        for (i = 0; i < PALETTE_COUNT; i++) {
-            r = *p++ * 4;
-            g = *p++ * 4;
-            b = *p++ * 4;
-            palette32[i] = 0xFFU << 24 | r << 16 | g << 8 | b;
-            palette32[i] |= palette32[i] >> 6 & 0x30303;
+        if (bytestream2_get_bytes_left(&gb) >= PALETTE_COUNT * 3) {
+            for (i = 0; i < PALETTE_COUNT; i++) {
+                r = bytestream2_get_byteu(&gb) * 4;
+                g = bytestream2_get_byteu(&gb) * 4;
+                b = bytestream2_get_byteu(&gb) * 4;
+                palette32[i] = 0xFFU << 24 | (r << 16) | (g << 8) | (b);
+                palette32[i] |= palette32[i] >> 6 & 0x30303;
+            }
         }
     }
-    if (p < p_end) {
+    if (s->size > 0) {
         /* originally UnpackFrame in VAG's code */
-        pb = p;
-        pb_end = p_end;
-        meth = *pb++;
+        bytestream2_init(&gb, gb.buffer, s->buf + s->size - gb.buffer);
+        if (bytestream2_get_bytes_left(&gb) < 1)
+            return;
+        meth = bytestream2_get_byteu(&gb);
         if (meth & 0x80) {
-            lz_unpack(pb, p_end - pb, s->unpack_buffer, s->unpack_buffer_size);
+            lz_unpack(gb.buffer, bytestream2_get_bytes_left(&gb),
+                      s->unpack_buffer, s->unpack_buffer_size);
             meth &= 0x7F;
-            pb = s->unpack_buffer;
-            pb_end = s->unpack_buffer + s->unpack_buffer_size;
+            bytestream2_init(&gb, s->unpack_buffer, s->unpack_buffer_size);
         }
 
-        dp = &s->frame.data[0][frame_y * s->frame.linesize[0] + frame_x];
+        dp = &frame->data[0][frame_y * frame->linesize[0] + frame_x];
         pp = &s->prev_frame.data[0][frame_y * s->prev_frame.linesize[0] + frame_x];
         switch (meth) {
         case 1:
             for (i = 0; i < frame_height; i++) {
                 ofs = 0;
                 do {
-                    if (pb_end - pb < 1)
-                        return;
-                    len = *pb++;
+                    len = bytestream2_get_byte(&gb);
                     if (len & 0x80) {
                         len = (len & 0x7F) + 1;
-                        if (ofs + len > frame_width || pb_end - pb < len)
+                        if (ofs + len > frame_width || bytestream2_get_bytes_left(&gb) < len)
                             return;
-                        memcpy(&dp[ofs], pb, len);
-                        pb += len;
+                        bytestream2_get_bufferu(&gb, &dp[ofs], len);
                         ofs += len;
                     } else {
                         /* interframe pixel copy */
@@ -313,18 +296,15 @@
                         ofs, frame_width);
                     break;
                 }
-                dp += s->frame.linesize[0];
+                dp += frame->linesize[0];
                 pp += s->prev_frame.linesize[0];
             }
             break;
 
         case 2:
             for (i = 0; i < frame_height; i++) {
-                if (pb_end -pb < frame_width)
-                    return;
-                memcpy(dp, pb, frame_width);
-                pb += frame_width;
-                dp += s->frame.linesize[0];
+                bytestream2_get_buffer(&gb, dp, frame_width);
+                dp += frame->linesize[0];
                 pp += s->prev_frame.linesize[0];
             }
             break;
@@ -333,22 +313,16 @@
             for (i = 0; i < frame_height; i++) {
                 ofs = 0;
                 do {
-                    if (pb_end - pb < 1)
-                        return;
-                    len = *pb++;
+                    len = bytestream2_get_byte(&gb);
                     if (len & 0x80) {
                         len = (len & 0x7F) + 1;
-                        if (pb_end - pb < 1)
-                            return;
-                        if (*pb++ == 0xFF)
-                            len = rle_unpack(pb, pb_end - pb, len, &dp[ofs], frame_width - ofs);
-                        else {
-                        if (pb_end - pb < len)
-                            return;
-                            memcpy(&dp[ofs], pb, len);
-                        }
-                        pb += len;
-                        ofs += len;
+                        if (bytestream2_get_byte(&gb) == 0xFF)
+                            len = rle_unpack(gb.buffer, &dp[ofs],
+                                             len, bytestream2_get_bytes_left(&gb),
+                                             frame_width - ofs);
+                        else
+                            bytestream2_get_buffer(&gb, &dp[ofs], len);
+                        bytestream2_skip(&gb, len);
                     } else {
                         /* interframe pixel copy */
                         if (ofs + len + 1 > frame_width || !s->prev_frame.data[0])
@@ -361,7 +335,7 @@
                     av_log(s->avctx, AV_LOG_ERROR, "offset > width (%d > %d)\n",
                         ofs, frame_width);
                 }
-                dp += s->frame.linesize[0];
+                dp += frame->linesize[0];
                 pp += s->prev_frame.linesize[0];
             }
             break;
@@ -405,7 +379,6 @@
         palette32[i] = (r << 16) | (g << 8) | (b);
     }
 
-    avcodec_get_frame_defaults(&s->frame);
     avcodec_get_frame_defaults(&s->prev_frame);
 
     return 0;
@@ -418,6 +391,8 @@
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     VmdVideoContext *s = avctx->priv_data;
+    AVFrame *frame = data;
+    int ret;
 
     s->buf = buf;
     s->size = buf_size;
@@ -425,24 +400,20 @@
     if (buf_size < 16)
         return buf_size;
 
-    s->frame.reference = 3;
-    if (ff_get_buffer(avctx, &s->frame)) {
-        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
-        return -1;
-    }
+    if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
+        return ret;
 
-    vmd_decode(s);
+    vmd_decode(s, frame);
 
     /* make the palette available on the way out */
-    memcpy(s->frame.data[1], s->palette, PALETTE_COUNT * 4);
+    memcpy(frame->data[1], s->palette, PALETTE_COUNT * 4);
 
     /* shuffle frames */
-    FFSWAP(AVFrame, s->frame, s->prev_frame);
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
+    av_frame_unref(&s->prev_frame);
+    if ((ret = av_frame_ref(&s->prev_frame, frame)) < 0)
+        return ret;
 
     *got_frame      = 1;
-    *(AVFrame*)data = s->prev_frame;
 
     /* report that the buffer was completely consumed */
     return buf_size;
@@ -452,8 +423,7 @@
 {
     VmdVideoContext *s = avctx->priv_data;
 
-    if (s->prev_frame.data[0])
-        avctx->release_buffer(avctx, &s->prev_frame);
+    av_frame_unref(&s->prev_frame);
     av_free(s->unpack_buffer);
 
     return 0;
@@ -600,10 +570,8 @@
     /* get output buffer */
     frame->nb_samples = ((silent_chunks + audio_chunks) * avctx->block_align) /
                         avctx->channels;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     output_samples_u8  =            frame->data[0];
     output_samples_s16 = (int16_t *)frame->data[0];
 
@@ -622,7 +590,7 @@
     /* decode audio chunks */
     if (audio_chunks > 0) {
         buf_end = buf + buf_size;
-        while ( buf_end - buf >= s->chunk_size) {
+        while (buf_end - buf >= s->chunk_size) {
             if (s->out_bps == 2) {
                 decode_audio_s16(output_samples_s16, buf, s->chunk_size,
                                  avctx->channels);
diff --git a/libavcodec/vmnc.c b/libavcodec/vmnc.c
index eb39fc9..99571a1 100644
--- a/libavcodec/vmnc.c
+++ b/libavcodec/vmnc.c
@@ -31,6 +31,7 @@
 #include "libavutil/common.h"
 #include "libavutil/intreadwrite.h"
 #include "avcodec.h"
+#include "internal.h"
 
 enum EncTypes {
     MAGIC_WMVd = 0x574D5664,
@@ -293,14 +294,10 @@
     VmncContext * const c = avctx->priv_data;
     uint8_t *outptr;
     const uint8_t *src = buf;
-    int dx, dy, w, h, depth, enc, chunks, res, size_left;
+    int dx, dy, w, h, depth, enc, chunks, res, size_left, ret;
 
-    c->pic.reference = 3;
-    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
-    if(avctx->reget_buffer(avctx, &c->pic) < 0){
-        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
-        return -1;
-    }
+    if ((ret = ff_reget_buffer(avctx, &c->pic)) < 0)
+        return ret;
 
     c->pic.key_frame = 0;
     c->pic.pict_type = AV_PICTURE_TYPE_P;
@@ -456,7 +453,8 @@
         }
     }
     *got_frame      = 1;
-    *(AVFrame*)data = c->pic;
+    if ((ret = av_frame_ref(data, &c->pic)) < 0)
+        return ret;
 
     /* always report that the buffer was completely consumed */
     return buf_size;
@@ -497,6 +495,8 @@
         return AVERROR_INVALIDDATA;
     }
 
+    avcodec_get_frame_defaults(&c->pic);
+
     return 0;
 }
 
@@ -511,8 +511,7 @@
 {
     VmncContext * const c = avctx->priv_data;
 
-    if (c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
+    av_frame_unref(&c->pic);
 
     av_free(c->curbits);
     av_free(c->curmask);
diff --git a/libavcodec/vorbis_parser.c b/libavcodec/vorbis_parser.c
index 6979d6f..22e0461 100644
--- a/libavcodec/vorbis_parser.c
+++ b/libavcodec/vorbis_parser.c
@@ -141,8 +141,9 @@
      * we may need to approach this the long way and parse the whole Setup
      * header, but I hope very much that it never comes to that. */
     if (last_mode_count > 2) {
-        av_log_ask_for_sample(avctx, "%d modes found. This is either a false "
-                              "positive or a sample from an unknown encoder.\n",
+        avpriv_request_sample(avctx,
+                              "%d modes (either a false positive or a "
+                              "sample from an unknown encoder)",
                               last_mode_count);
     }
     /* We're limiting the mode count to 63 so that we know that the previous
diff --git a/libavcodec/vorbisdec.c b/libavcodec/vorbisdec.c
index e738654..f30745d 100644
--- a/libavcodec/vorbisdec.c
+++ b/libavcodec/vorbisdec.c
@@ -239,10 +239,10 @@
 static int vorbis_parse_setup_hdr_codebooks(vorbis_context *vc)
 {
     unsigned cb;
-    uint8_t  *tmp_vlc_bits;
-    uint32_t *tmp_vlc_codes;
+    uint8_t  *tmp_vlc_bits  = NULL;
+    uint32_t *tmp_vlc_codes = NULL;
     GetBitContext *gb = &vc->gb;
-    uint16_t *codebook_multiplicands;
+    uint16_t *codebook_multiplicands = NULL;
     int ret = 0;
 
     vc->codebook_count = get_bits(gb, 8) + 1;
@@ -253,6 +253,11 @@
     tmp_vlc_bits  = av_mallocz(V_MAX_VLCS * sizeof(*tmp_vlc_bits));
     tmp_vlc_codes = av_mallocz(V_MAX_VLCS * sizeof(*tmp_vlc_codes));
     codebook_multiplicands = av_malloc(V_MAX_VLCS * sizeof(*codebook_multiplicands));
+    if (!vc->codebooks ||
+        !tmp_vlc_bits || !tmp_vlc_codes || !codebook_multiplicands) {
+        ret = AVERROR(ENOMEM);
+        goto error;
+    }
 
     for (cb = 0; cb < vc->codebook_count; ++cb) {
         vorbis_codebook *codebook_setup = &vc->codebooks[cb];
@@ -482,17 +487,19 @@
 
 static int vorbis_floor0_decode(vorbis_context *vc,
                                 vorbis_floor_data *vfu, float *vec);
-static void create_map(vorbis_context *vc, unsigned floor_number);
+static int create_map(vorbis_context *vc, unsigned floor_number);
 static int vorbis_floor1_decode(vorbis_context *vc,
                                 vorbis_floor_data *vfu, float *vec);
 static int vorbis_parse_setup_hdr_floors(vorbis_context *vc)
 {
     GetBitContext *gb = &vc->gb;
-    int i,j,k;
+    int i, j, k, ret;
 
     vc->floor_count = get_bits(gb, 6) + 1;
 
     vc->floors = av_mallocz(vc->floor_count * sizeof(*vc->floors));
+    if (!vc->floors)
+        return AVERROR(ENOMEM);
 
     for (i = 0; i < vc->floor_count; ++i) {
         vorbis_floor *floor_setup = &vc->floors[i];
@@ -556,7 +563,8 @@
 
             floor_setup->data.t1.list = av_mallocz(floor_setup->data.t1.x_list_dim *
                                                    sizeof(*floor_setup->data.t1.list));
-
+            if (!floor_setup->data.t1.list)
+                return AVERROR(ENOMEM);
 
             rangebits = get_bits(gb, 4);
             rangemax = (1 << rangebits);
@@ -626,7 +634,8 @@
                 }
             }
 
-            create_map(vc, i);
+            if ((ret = create_map(vc, i)) < 0)
+                return ret;
 
             /* codebook dim is for padding if codebook dim doesn't *
              * divide order+1 then we need to read more data       */
@@ -673,6 +682,8 @@
 
     vc->residue_count = get_bits(gb, 6)+1;
     vc->residues      = av_mallocz(vc->residue_count * sizeof(*vc->residues));
+    if (!vc->residues)
+        return AVERROR(ENOMEM);
 
     av_dlog(NULL, " There are %d residues. \n", vc->residue_count);
 
@@ -753,6 +764,8 @@
 
     vc->mapping_count = get_bits(gb, 6)+1;
     vc->mappings      = av_mallocz(vc->mapping_count * sizeof(*vc->mappings));
+    if (!vc->mappings)
+        return AVERROR(ENOMEM);
 
     av_dlog(NULL, " There are %d mappings. \n", vc->mapping_count);
 
@@ -775,6 +788,9 @@
                                                        sizeof(*mapping_setup->magnitude));
             mapping_setup->angle          = av_mallocz(mapping_setup->coupling_steps *
                                                        sizeof(*mapping_setup->angle));
+            if (!mapping_setup->angle || !mapping_setup->magnitude)
+                return AVERROR(ENOMEM);
+
             for (j = 0; j < mapping_setup->coupling_steps; ++j) {
                 GET_VALIDATED_INDEX(mapping_setup->magnitude[j], ilog(vc->audio_channels - 1), vc->audio_channels)
                 GET_VALIDATED_INDEX(mapping_setup->angle[j],     ilog(vc->audio_channels - 1), vc->audio_channels)
@@ -794,6 +810,9 @@
         if (mapping_setup->submaps>1) {
             mapping_setup->mux = av_mallocz(vc->audio_channels *
                                             sizeof(*mapping_setup->mux));
+            if (!mapping_setup->mux)
+                return AVERROR(ENOMEM);
+
             for (j = 0; j < vc->audio_channels; ++j)
                 mapping_setup->mux[j] = get_bits(gb, 4);
         }
@@ -813,7 +832,7 @@
 
 // Process modes part
 
-static void create_map(vorbis_context *vc, unsigned floor_number)
+static int create_map(vorbis_context *vc, unsigned floor_number)
 {
     vorbis_floor *floors = vc->floors;
     vorbis_floor0 *vf;
@@ -825,6 +844,8 @@
         n = vc->blocksize[blockflag] / 2;
         floors[floor_number].data.t0.map[blockflag] =
             av_malloc((n + 1) * sizeof(int32_t)); // n + sentinel
+        if (!floors[floor_number].data.t0.map[blockflag])
+            return AVERROR(ENOMEM);
 
         map =  floors[floor_number].data.t0.map[blockflag];
         vf  = &floors[floor_number].data.t0;
@@ -842,6 +863,8 @@
     for (idx = 0; idx <= n; ++idx) {
         av_dlog(NULL, "floor0 map: map at pos %d is %d\n", idx, map[idx]);
     }
+
+    return 0;
 }
 
 static int vorbis_parse_setup_hdr_modes(vorbis_context *vc)
@@ -851,6 +874,8 @@
 
     vc->mode_count = get_bits(gb, 6) + 1;
     vc->modes      = av_mallocz(vc->mode_count * sizeof(*vc->modes));
+    if (!vc->modes)
+        return AVERROR(ENOMEM);
 
     av_dlog(NULL, " There are %d modes.\n", vc->mode_count);
 
@@ -961,6 +986,9 @@
 
     vc->channel_residues =  av_malloc((vc->blocksize[1]  / 2) * vc->audio_channels * sizeof(*vc->channel_residues));
     vc->saved            =  av_mallocz((vc->blocksize[1] / 4) * vc->audio_channels * sizeof(*vc->saved));
+    if (!vc->channel_residues || !vc->saved)
+        return AVERROR(ENOMEM);
+
     vc->previous_window  = 0;
 
     ff_mdct_init(&vc->mdct[0], bl0, 1, -1.0);
@@ -1701,10 +1729,8 @@
 
     /* get output buffer */
     frame->nb_samples = vc->blocksize[1] / 2;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
 
     if (vc->audio_channels > 8) {
         for (i = 0; i < vc->audio_channels; i++)
@@ -1724,6 +1750,7 @@
     if (!vc->first_frame) {
         vc->first_frame = 1;
         *got_frame_ptr = 0;
+        av_frame_unref(frame);
         return buf_size;
     }
 
diff --git a/libavcodec/vorbisenc.c b/libavcodec/vorbisenc.c
index 22b6681..d685996 100644
--- a/libavcodec/vorbisenc.c
+++ b/libavcodec/vorbisenc.c
@@ -1028,7 +1028,7 @@
         return 0;
     samples = 1 << (venc->log2_blocksize[0] - 1);
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, 8192)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, 8192)) < 0)
         return ret;
 
     init_put_bits(&pb, avpkt->data, avpkt->size);
@@ -1153,9 +1153,6 @@
     ff_mdct_end(&venc->mdct[0]);
     ff_mdct_end(&venc->mdct[1]);
 
-#if FF_API_OLD_ENCODE_AUDIO
-    av_freep(&avctx->coded_frame);
-#endif
     av_freep(&avctx->extradata);
 
     return 0 ;
@@ -1187,14 +1184,6 @@
 
     avctx->frame_size = 1 << (venc->log2_blocksize[0] - 1);
 
-#if FF_API_OLD_ENCODE_AUDIO
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame) {
-        ret = AVERROR(ENOMEM);
-        goto error;
-    }
-#endif
-
     return 0;
 error:
     vorbis_encode_close(avctx);
diff --git a/libavcodec/vp3.c b/libavcodec/vp3.c
index 00a77e9..5bf2fee 100644
--- a/libavcodec/vp3.c
+++ b/libavcodec/vp3.c
@@ -38,6 +38,7 @@
 #include "internal.h"
 #include "dsputil.h"
 #include "get_bits.h"
+#include "hpeldsp.h"
 #include "videodsp.h"
 #include "vp3data.h"
 #include "vp3dsp.h"
@@ -135,11 +136,13 @@
     int version;
     int width, height;
     int chroma_x_shift, chroma_y_shift;
-    AVFrame golden_frame;
-    AVFrame last_frame;
-    AVFrame current_frame;
+    ThreadFrame golden_frame;
+    ThreadFrame last_frame;
+    ThreadFrame current_frame;
     int keyframe;
-    DSPContext dsp;
+    uint8_t idct_permutation[64];
+    uint8_t idct_scantable[64];
+    HpelDSPContext hdsp;
     VideoDSPContext vdsp;
     VP3DSPContext vp3dsp;
     DECLARE_ALIGNED(16, int16_t, block)[64];
@@ -176,8 +179,6 @@
 
     int8_t (*motion_val[2])[2];
 
-    ScanTable scantable;
-
     /* tables */
     uint16_t coded_dc_scale_factor[64];
     uint32_t coded_ac_scale_factor[64];
@@ -265,19 +266,11 @@
 {
     Vp3DecodeContext *s = avctx->priv_data;
 
-    if (s->golden_frame.data[0]) {
-        if (s->golden_frame.data[0] == s->last_frame.data[0])
-            memset(&s->last_frame, 0, sizeof(AVFrame));
-        if (s->current_frame.data[0] == s->golden_frame.data[0])
-            memset(&s->current_frame, 0, sizeof(AVFrame));
+    if (s->golden_frame.f)
         ff_thread_release_buffer(avctx, &s->golden_frame);
-    }
-    if (s->last_frame.data[0]) {
-        if (s->current_frame.data[0] == s->last_frame.data[0])
-            memset(&s->current_frame, 0, sizeof(AVFrame));
+    if (s->last_frame.f)
         ff_thread_release_buffer(avctx, &s->last_frame);
-    }
-    if (s->current_frame.data[0])
+    if (s->current_frame.f)
         ff_thread_release_buffer(avctx, &s->current_frame);
 }
 
@@ -298,6 +291,12 @@
 
     s->theora_tables = 0;
 
+    /* release all frames */
+    vp3_decode_flush(avctx);
+    av_frame_free(&s->current_frame.f);
+    av_frame_free(&s->last_frame.f);
+    av_frame_free(&s->golden_frame.f);
+
     if (avctx->internal->is_copy)
         return 0;
 
@@ -314,8 +313,6 @@
     ff_free_vlc(&s->mode_code_vlc);
     ff_free_vlc(&s->motion_vector_vlc);
 
-    /* release all frames */
-    vp3_decode_flush(avctx);
 
     return 0;
 }
@@ -384,7 +381,8 @@
                 int qmin= 8<<(inter + !i);
                 int qscale= i ? ac_scale_factor : dc_scale_factor;
 
-                s->qmat[qpi][inter][plane][s->dsp.idct_permutation[i]]= av_clip((qscale * coeff)/100 * 4, qmin, 4096);
+                s->qmat[qpi][inter][plane][s->idct_permutation[i]] =
+                    av_clip((qscale * coeff) / 100 * 4, qmin, 4096);
             }
             // all DC coefficients use the same quant so as not to interfere with DC prediction
             s->qmat[qpi][inter][plane][0] = s->qmat[0][inter][plane][0];
@@ -1296,8 +1294,8 @@
     int width           = s->fragment_width[!!plane];
     int height          = s->fragment_height[!!plane];
     int fragment        = s->fragment_start        [plane] + ystart * width;
-    int stride          = s->current_frame.linesize[plane];
-    uint8_t *plane_data = s->current_frame.data    [plane];
+    int stride          = s->current_frame.f->linesize[plane];
+    uint8_t *plane_data = s->current_frame.f->data    [plane];
     if (!s->flipped_image) stride = -stride;
     plane_data += s->data_offset[plane] + 8*ystart*stride;
 
@@ -1359,7 +1357,7 @@
                               int plane, int inter, int16_t block[64])
 {
     int16_t *dequantizer = s->qmat[frag->qpi][inter][plane];
-    uint8_t *perm = s->scantable.permutated;
+    uint8_t *perm = s->idct_scantable;
     int i = 0;
 
     do {
@@ -1426,14 +1424,14 @@
     }
 
     cy = y >> s->chroma_y_shift;
-    offset[0] = s->current_frame.linesize[0]*y;
-    offset[1] = s->current_frame.linesize[1]*cy;
-    offset[2] = s->current_frame.linesize[2]*cy;
+    offset[0] = s->current_frame.f->linesize[0]*y;
+    offset[1] = s->current_frame.f->linesize[1]*cy;
+    offset[2] = s->current_frame.f->linesize[2]*cy;
     for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
         offset[i] = 0;
 
     emms_c();
-    s->avctx->draw_horiz_band(s->avctx, &s->current_frame, offset, y, 3, h);
+    s->avctx->draw_horiz_band(s->avctx, s->current_frame.f, offset, y, 3, h);
 }
 
 /**
@@ -1442,7 +1440,7 @@
  */
 static void await_reference_row(Vp3DecodeContext *s, Vp3Fragment *fragment, int motion_y, int y)
 {
-    AVFrame *ref_frame;
+    ThreadFrame *ref_frame;
     int ref_row;
     int border = motion_y&1;
 
@@ -1475,10 +1473,10 @@
         return;
 
     for (plane = 0; plane < 3; plane++) {
-        uint8_t *output_plane = s->current_frame.data    [plane] + s->data_offset[plane];
-        uint8_t *  last_plane = s->   last_frame.data    [plane] + s->data_offset[plane];
-        uint8_t *golden_plane = s-> golden_frame.data    [plane] + s->data_offset[plane];
-        int stride            = s->current_frame.linesize[plane];
+        uint8_t *output_plane = s->current_frame.f->data    [plane] + s->data_offset[plane];
+        uint8_t *  last_plane = s->   last_frame.f->data    [plane] + s->data_offset[plane];
+        uint8_t *golden_plane = s-> golden_frame.f->data    [plane] + s->data_offset[plane];
+        int stride            = s->current_frame.f->linesize[plane];
         int plane_width       = s->width  >> (plane && s->chroma_x_shift);
         int plane_height      = s->height >> (plane && s->chroma_y_shift);
         int8_t (*motion_val)[2] = s->motion_val[!!plane];
@@ -1565,7 +1563,7 @@
                            VP3 source but this would be slower as
                            put_no_rnd_pixels_tab is better optimzed */
                         if(motion_halfpel_index != 3){
-                            s->dsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
+                            s->hdsp.put_no_rnd_pixels_tab[1][motion_halfpel_index](
                                 output_plane + first_pixel,
                                 motion_source, stride, 8);
                         }else{
@@ -1599,7 +1597,7 @@
                 } else {
 
                     /* copy directly from the previous frame */
-                    s->dsp.put_pixels_tab[1][0](
+                    s->hdsp.put_pixels_tab[1][0](
                         output_plane + first_pixel,
                         last_plane + first_pixel,
                         stride, 8);
@@ -1657,14 +1655,36 @@
     return 0;
 }
 
+static av_cold int init_frames(Vp3DecodeContext *s)
+{
+    s->current_frame.f = av_frame_alloc();
+    s->last_frame.f    = av_frame_alloc();
+    s->golden_frame.f  = av_frame_alloc();
+
+    if (!s->current_frame.f || !s->last_frame.f || !s->golden_frame.f) {
+        av_frame_free(&s->current_frame.f);
+        av_frame_free(&s->last_frame.f);
+        av_frame_free(&s->golden_frame.f);
+        return AVERROR(ENOMEM);
+    }
+
+    return 0;
+}
+
 static av_cold int vp3_decode_init(AVCodecContext *avctx)
 {
     Vp3DecodeContext *s = avctx->priv_data;
-    int i, inter, plane;
+    int i, inter, plane, ret;
     int c_width;
     int c_height;
     int y_fragment_count, c_fragment_count;
 
+    ret = init_frames(s);
+    if (ret < 0)
+        return ret;
+
+    avctx->internal->allocate_progress = 1;
+
     if (avctx->codec_tag == MKTAG('V','P','3','0'))
         s->version = 0;
     else
@@ -1676,12 +1696,16 @@
     if (avctx->codec_id != AV_CODEC_ID_THEORA)
         avctx->pix_fmt = AV_PIX_FMT_YUV420P;
     avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
-    ff_dsputil_init(&s->dsp, avctx);
+    ff_hpeldsp_init(&s->hdsp, avctx->flags | CODEC_FLAG_BITEXACT);
     ff_videodsp_init(&s->vdsp, 8);
     ff_vp3dsp_init(&s->vp3dsp, avctx->flags);
 
-    ff_init_scantable_permutation(s->dsp.idct_permutation, s->vp3dsp.idct_perm);
-    ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
+    for (i = 0; i < 64; i++) {
+#define T(x) (x >> 3) | ((x & 7) << 3)
+        s->idct_permutation[i] = T(i);
+        s->idct_scantable[i] = T(ff_zigzag_direct[i]);
+#undef T
+    }
 
     /* initialize to an impossible value which will force a recalculation
      * in the first frame decode */
@@ -1820,12 +1844,6 @@
         &motion_vector_vlc_table[0][1], 2, 1,
         &motion_vector_vlc_table[0][0], 2, 1, 0);
 
-    for (i = 0; i < 3; i++) {
-        s->current_frame.data[i] = NULL;
-        s->last_frame.data[i] = NULL;
-        s->golden_frame.data[i] = NULL;
-    }
-
     return allocate_tables(avctx);
 
 vlc_fail:
@@ -1834,26 +1852,44 @@
 }
 
 /// Release and shuffle frames after decode finishes
-static void update_frames(AVCodecContext *avctx)
+static int update_frames(AVCodecContext *avctx)
 {
     Vp3DecodeContext *s = avctx->priv_data;
+    int ret = 0;
 
-    /* release the last frame, if it is allocated and if it is not the
-     * golden frame */
-    if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
-        ff_thread_release_buffer(avctx, &s->last_frame);
 
     /* shuffle frames (last = current) */
-    s->last_frame= s->current_frame;
+    ff_thread_release_buffer(avctx, &s->last_frame);
+    ret = ff_thread_ref_frame(&s->last_frame, &s->current_frame);
+    if (ret < 0)
+        goto fail;
 
     if (s->keyframe) {
-        if (s->golden_frame.data[0])
-            ff_thread_release_buffer(avctx, &s->golden_frame);
-        s->golden_frame = s->current_frame;
-        s->last_frame.type = FF_BUFFER_TYPE_COPY;
+        ff_thread_release_buffer(avctx, &s->golden_frame);
+        ret = ff_thread_ref_frame(&s->golden_frame, &s->current_frame);
     }
 
-    s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
+fail:
+    ff_thread_release_buffer(avctx, &s->current_frame);
+    return ret;
+}
+
+static int ref_frame(Vp3DecodeContext *s, ThreadFrame *dst, ThreadFrame *src)
+{
+    ff_thread_release_buffer(s->avctx, dst);
+    if (src->f->data[0])
+        return ff_thread_ref_frame(dst, src);
+    return 0;
+}
+
+static int ref_frames(Vp3DecodeContext *dst, Vp3DecodeContext *src)
+{
+    int ret;
+    if ((ret = ref_frame(dst, &dst->current_frame, &src->current_frame)) < 0 ||
+        (ret = ref_frame(dst, &dst->golden_frame,  &src->golden_frame)) < 0  ||
+        (ret = ref_frame(dst, &dst->last_frame,    &src->last_frame)) < 0)
+        return ret;
+    return 0;
 }
 
 static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
@@ -1863,17 +1899,17 @@
 
 #define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
 
-    if (!s1->current_frame.data[0]
+    if (!s1->current_frame.f->data[0]
         ||s->width != s1->width
         ||s->height!= s1->height) {
         if (s != s1)
-            copy_fields(s, s1, golden_frame, keyframe);
+            ref_frames(s, s1);
         return -1;
     }
 
     if (s != s1) {
         // init tables if the first frame hasn't been decoded
-        if (!s->current_frame.data[0]) {
+        if (!s->current_frame.f->data[0]) {
             int y_fragment_count, c_fragment_count;
             s->avctx = dst;
             err = allocate_tables(dst);
@@ -1886,7 +1922,10 @@
         }
 
         // copy previous frame data
-        copy_fields(s, s1, golden_frame, dsp);
+        if ((err = ref_frames(s, s1)) < 0)
+            return err;
+
+        s->keyframe = s1->keyframe;
 
         // copy qscale data if necessary
         for (i = 0; i < 3; i++) {
@@ -1904,9 +1943,7 @@
 #undef copy_fields
     }
 
-    update_frames(dst);
-
-    return 0;
+    return update_frames(dst);
 }
 
 static int vp3_decode_frame(AVCodecContext *avctx,
@@ -1917,8 +1954,7 @@
     int buf_size = avpkt->size;
     Vp3DecodeContext *s = avctx->priv_data;
     GetBitContext gb;
-    int i;
-    int ret;
+    int i, ret;
 
     init_get_bits(&gb, buf, buf_size * 8);
 
@@ -1991,16 +2027,13 @@
     if (avctx->skip_frame >= AVDISCARD_NONKEY && !s->keyframe)
         return buf_size;
 
-    s->current_frame.reference = 3;
-    s->current_frame.pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
-    s->current_frame.key_frame = s->keyframe;
-    if (ff_thread_get_buffer(avctx, &s->current_frame) < 0) {
-        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    s->current_frame.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
+    s->current_frame.f->key_frame = s->keyframe;
+    if (ff_thread_get_buffer(avctx, &s->current_frame, AV_GET_BUFFER_FLAG_REF) < 0)
         goto error;
-    }
 
     if (!s->edge_emu_buffer)
-        s->edge_emu_buffer = av_malloc(9*FFABS(s->current_frame.linesize[0]));
+        s->edge_emu_buffer = av_malloc(9*FFABS(s->current_frame.f->linesize[0]));
 
     if (s->keyframe) {
         if (!s->theora)
@@ -2021,17 +2054,15 @@
             skip_bits(&gb, 2); /* reserved? */
         }
     } else {
-        if (!s->golden_frame.data[0]) {
+        if (!s->golden_frame.f->data[0]) {
             av_log(s->avctx, AV_LOG_WARNING, "vp3: first frame not a keyframe\n");
 
-            s->golden_frame.reference = 3;
-            s->golden_frame.pict_type = AV_PICTURE_TYPE_I;
-            if (ff_thread_get_buffer(avctx, &s->golden_frame) < 0) {
-                av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+            s->golden_frame.f->pict_type = AV_PICTURE_TYPE_I;
+            if (ff_thread_get_buffer(avctx, &s->golden_frame, AV_GET_BUFFER_FLAG_REF) < 0)
                 goto error;
-            }
-            s->last_frame = s->golden_frame;
-            s->last_frame.type = FF_BUFFER_TYPE_COPY;
+            ff_thread_release_buffer(avctx, &s->last_frame);
+            if ((ret = ff_thread_ref_frame(&s->last_frame, &s->golden_frame)) < 0)
+                goto error;
             ff_thread_report_progress(&s->last_frame, INT_MAX, 0);
         }
     }
@@ -2065,7 +2096,7 @@
         if (s->flipped_image)
             s->data_offset[i] = 0;
         else
-            s->data_offset[i] = (height-1) * s->current_frame.linesize[i];
+            s->data_offset[i] = (height-1) * s->current_frame.f->linesize[i];
     }
 
     s->last_slice_end = 0;
@@ -2079,11 +2110,15 @@
     }
     vp3_draw_horiz_band(s, s->avctx->height);
 
+    if ((ret = av_frame_ref(data, s->current_frame.f)) < 0)
+        return ret;
     *got_frame = 1;
-    *(AVFrame*)data= s->current_frame;
 
-    if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
-        update_frames(avctx);
+    if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME)) {
+        ret = update_frames(avctx);
+        if (ret < 0)
+            return ret;
+    }
 
     return buf_size;
 
@@ -2091,7 +2126,7 @@
     ff_thread_report_progress(&s->current_frame, INT_MAX, 0);
 
     if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
-        avctx->release_buffer(avctx, &s->current_frame);
+        av_frame_unref(s->current_frame.f);
 
     return -1;
 }
@@ -2145,7 +2180,7 @@
     s->motion_val[1]          = NULL;
     s->edge_emu_buffer        = NULL;
 
-    return 0;
+    return init_frames(s);
 }
 
 #if CONFIG_THEORA_DECODER
@@ -2190,7 +2225,7 @@
 
     fps.num = get_bits_long(gb, 32);
     fps.den = get_bits_long(gb, 32);
-    if (fps.num && fps.den) {
+    if (fps.num>0 && fps.den>0) {
         av_reduce(&avctx->time_base.num, &avctx->time_base.den,
                   fps.den, fps.num, 1<<30);
     }
diff --git a/libavcodec/vp3dsp.c b/libavcodec/vp3dsp.c
index 051812e..5656629 100644
--- a/libavcodec/vp3dsp.c
+++ b/libavcodec/vp3dsp.c
@@ -54,7 +54,58 @@
     /* Inverse DCT on the rows now */
     for (i = 0; i < 8; i++) {
         /* Check for non-zero values */
-        if ( ip[0] | ip[1] | ip[2] | ip[3] | ip[4] | ip[5] | ip[6] | ip[7] ) {
+        if ( ip[0 * 8] | ip[1 * 8] | ip[2 * 8] | ip[3 * 8] |
+             ip[4 * 8] | ip[5 * 8] | ip[6 * 8] | ip[7 * 8] ) {
+            A = M(xC1S7, ip[1 * 8]) + M(xC7S1, ip[7 * 8]);
+            B = M(xC7S1, ip[1 * 8]) - M(xC1S7, ip[7 * 8]);
+            C = M(xC3S5, ip[3 * 8]) + M(xC5S3, ip[5 * 8]);
+            D = M(xC3S5, ip[5 * 8]) - M(xC5S3, ip[3 * 8]);
+
+            Ad = M(xC4S4, (A - C));
+            Bd = M(xC4S4, (B - D));
+
+            Cd = A + C;
+            Dd = B + D;
+
+            E = M(xC4S4, (ip[0 * 8] + ip[4 * 8]));
+            F = M(xC4S4, (ip[0 * 8] - ip[4 * 8]));
+
+            G = M(xC2S6, ip[2 * 8]) + M(xC6S2, ip[6 * 8]);
+            H = M(xC6S2, ip[2 * 8]) - M(xC2S6, ip[6 * 8]);
+
+            Ed = E - G;
+            Gd = E + G;
+
+            Add = F + Ad;
+            Bdd = Bd - H;
+
+            Fd = F - Ad;
+            Hd = Bd + H;
+
+            /*  Final sequence of operations over-write original inputs. */
+            ip[0 * 8] = Gd + Cd ;
+            ip[7 * 8] = Gd - Cd ;
+
+            ip[1 * 8] = Add + Hd;
+            ip[2 * 8] = Add - Hd;
+
+            ip[3 * 8] = Ed + Dd ;
+            ip[4 * 8] = Ed - Dd ;
+
+            ip[5 * 8] = Fd + Bdd;
+            ip[6 * 8] = Fd - Bdd;
+        }
+
+        ip += 1;            /* next row */
+    }
+
+    ip = input;
+
+    for ( i = 0; i < 8; i++) {
+        /* Check for non-zero values (bitwise or faster than ||) */
+        if ( ip[1] | ip[2] | ip[3] |
+             ip[4] | ip[5] | ip[6] | ip[7] ) {
+
             A = M(xC1S7, ip[1]) + M(xC7S1, ip[7]);
             B = M(xC7S1, ip[1]) - M(xC1S7, ip[7]);
             C = M(xC3S5, ip[3]) + M(xC5S3, ip[5]);
@@ -66,8 +117,13 @@
             Cd = A + C;
             Dd = B + D;
 
-            E = M(xC4S4, (ip[0] + ip[4]));
-            F = M(xC4S4, (ip[0] - ip[4]));
+            E = M(xC4S4, (ip[0] + ip[4])) + 8;
+            F = M(xC4S4, (ip[0] - ip[4])) + 8;
+
+            if(type==1){  //HACK
+                E += 16*128;
+                F += 16*128;
+            }
 
             G = M(xC2S6, ip[2]) + M(xC6S2, ip[6]);
             H = M(xC6S2, ip[2]) - M(xC2S6, ip[6]);
@@ -81,75 +137,8 @@
             Fd = F - Ad;
             Hd = Bd + H;
 
-            /*  Final sequence of operations over-write original inputs. */
-            ip[0] = Gd + Cd ;
-            ip[7] = Gd - Cd ;
-
-            ip[1] = Add + Hd;
-            ip[2] = Add - Hd;
-
-            ip[3] = Ed + Dd ;
-            ip[4] = Ed - Dd ;
-
-            ip[5] = Fd + Bdd;
-            ip[6] = Fd - Bdd;
-        }
-
-        ip += 8;            /* next row */
-    }
-
-    ip = input;
-
-    for ( i = 0; i < 8; i++) {
-        /* Check for non-zero values (bitwise or faster than ||) */
-        if ( ip[1 * 8] | ip[2 * 8] | ip[3 * 8] |
-             ip[4 * 8] | ip[5 * 8] | ip[6 * 8] | ip[7 * 8] ) {
-
-            A = M(xC1S7, ip[1*8]) + M(xC7S1, ip[7*8]);
-            B = M(xC7S1, ip[1*8]) - M(xC1S7, ip[7*8]);
-            C = M(xC3S5, ip[3*8]) + M(xC5S3, ip[5*8]);
-            D = M(xC3S5, ip[5*8]) - M(xC5S3, ip[3*8]);
-
-            Ad = M(xC4S4, (A - C));
-            Bd = M(xC4S4, (B - D));
-
-            Cd = A + C;
-            Dd = B + D;
-
-            E = M(xC4S4, (ip[0*8] + ip[4*8])) + 8;
-            F = M(xC4S4, (ip[0*8] - ip[4*8])) + 8;
-
-            if(type==1){  //HACK
-                E += 16*128;
-                F += 16*128;
-            }
-
-            G = M(xC2S6, ip[2*8]) + M(xC6S2, ip[6*8]);
-            H = M(xC6S2, ip[2*8]) - M(xC2S6, ip[6*8]);
-
-            Ed = E - G;
-            Gd = E + G;
-
-            Add = F + Ad;
-            Bdd = Bd - H;
-
-            Fd = F - Ad;
-            Hd = Bd + H;
-
             /* Final sequence of operations over-write original inputs. */
-            if(type==0){
-                ip[0*8] = (Gd + Cd )  >> 4;
-                ip[7*8] = (Gd - Cd )  >> 4;
-
-                ip[1*8] = (Add + Hd ) >> 4;
-                ip[2*8] = (Add - Hd ) >> 4;
-
-                ip[3*8] = (Ed + Dd )  >> 4;
-                ip[4*8] = (Ed - Dd )  >> 4;
-
-                ip[5*8] = (Fd + Bdd ) >> 4;
-                ip[6*8] = (Fd - Bdd ) >> 4;
-            }else if(type==1){
+            if (type == 1) {
                 dst[0*stride] = av_clip_uint8((Gd + Cd )  >> 4);
                 dst[7*stride] = av_clip_uint8((Gd - Cd )  >> 4);
 
@@ -176,16 +165,7 @@
             }
 
         } else {
-            if(type==0){
-                ip[0*8] =
-                ip[1*8] =
-                ip[2*8] =
-                ip[3*8] =
-                ip[4*8] =
-                ip[5*8] =
-                ip[6*8] =
-                ip[7*8] = ((xC4S4 * ip[0*8] + (IdctAdjustBeforeShift<<16))>>20);
-            }else if(type==1){
+            if (type == 1) {
                 dst[0*stride]=
                 dst[1*stride]=
                 dst[2*stride]=
@@ -193,10 +173,10 @@
                 dst[4*stride]=
                 dst[5*stride]=
                 dst[6*stride]=
-                dst[7*stride]= av_clip_uint8(128 + ((xC4S4 * ip[0*8] + (IdctAdjustBeforeShift<<16))>>20));
+                dst[7*stride]= av_clip_uint8(128 + ((xC4S4 * ip[0] + (IdctAdjustBeforeShift<<16))>>20));
             }else{
-                if(ip[0*8]){
-                    int v= ((xC4S4 * ip[0*8] + (IdctAdjustBeforeShift<<16))>>20);
+                if(ip[0]){
+                    int v= ((xC4S4 * ip[0] + (IdctAdjustBeforeShift<<16))>>20);
                     dst[0*stride] = av_clip_uint8(dst[0*stride] + v);
                     dst[1*stride] = av_clip_uint8(dst[1*stride] + v);
                     dst[2*stride] = av_clip_uint8(dst[2*stride] + v);
@@ -209,7 +189,7 @@
             }
         }
 
-        ip++;            /* next column */
+        ip += 8;            /* next column */
         dst++;
     }
 }
@@ -307,8 +287,6 @@
     c->v_loop_filter = vp3_v_loop_filter_c;
     c->h_loop_filter = vp3_h_loop_filter_c;
 
-    c->idct_perm = FF_NO_IDCT_PERM;
-
     if (ARCH_ARM)
         ff_vp3dsp_init_arm(c, flags);
     if (ARCH_BFIN)
diff --git a/libavcodec/vp3dsp.h b/libavcodec/vp3dsp.h
index 558077f..3aded99 100644
--- a/libavcodec/vp3dsp.h
+++ b/libavcodec/vp3dsp.h
@@ -43,8 +43,6 @@
     void (*idct_dc_add)(uint8_t *dest, int line_size, int16_t *block);
     void (*v_loop_filter)(uint8_t *src, int stride, int *bounding_values);
     void (*h_loop_filter)(uint8_t *src, int stride, int *bounding_values);
-
-    int idct_perm;
 } VP3DSPContext;
 
 void ff_vp3dsp_init(VP3DSPContext *c, int flags);
diff --git a/libavcodec/vp5.c b/libavcodec/vp5.c
index 10dab39..4eecbe3 100644
--- a/libavcodec/vp5.c
+++ b/libavcodec/vp5.c
@@ -40,10 +40,10 @@
     int rows, cols;
 
     ff_vp56_init_range_decoder(&s->c, buf, buf_size);
-    s->framep[VP56_FRAME_CURRENT]->key_frame = !vp56_rac_get(c);
+    s->frames[VP56_FRAME_CURRENT]->key_frame = !vp56_rac_get(c);
     vp56_rac_get(c);
     ff_vp56_init_dequant(s, vp56_rac_gets(c, 6));
-    if (s->framep[VP56_FRAME_CURRENT]->key_frame)
+    if (s->frames[VP56_FRAME_CURRENT]->key_frame)
     {
         vp56_rac_gets(c, 8);
         if(vp56_rac_gets(c, 5) > 5)
@@ -137,7 +137,7 @@
             if (vp56_rac_get_prob(c, vp5_dccv_pct[pt][node])) {
                 def_prob[node] = vp56_rac_gets_nn(c, 7);
                 model->coeff_dccv[pt][node] = def_prob[node];
-            } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {
+            } else if (s->frames[VP56_FRAME_CURRENT]->key_frame) {
                 model->coeff_dccv[pt][node] = def_prob[node];
             }
 
@@ -148,7 +148,7 @@
                     if (vp56_rac_get_prob(c, vp5_ract_pct[ct][pt][cg][node])) {
                         def_prob[node] = vp56_rac_gets_nn(c, 7);
                         model->coeff_ract[pt][ct][cg][node] = def_prob[node];
-                    } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {
+                    } else if (s->frames[VP56_FRAME_CURRENT]->key_frame) {
                         model->coeff_ract[pt][ct][cg][node] = def_prob[node];
                     }
 
@@ -172,7 +172,7 @@
 {
     VP56RangeCoder *c = &s->c;
     VP56Model *model = s->modelp;
-    uint8_t *permute = s->scantable.permutated;
+    uint8_t *permute = s->idct_scantable;
     uint8_t *model1, *model2;
     int coeff, sign, coeff_idx;
     int b, i, cg, idx, ctx, ctx_last;
@@ -263,8 +263,10 @@
 static av_cold int vp5_decode_init(AVCodecContext *avctx)
 {
     VP56Context *s = avctx->priv_data;
+    int ret;
 
-    ff_vp56_init(avctx, 1, 0);
+    if ((ret = ff_vp56_init(avctx, 1, 0)) < 0)
+        return ret;
     s->vp56_coord_div = vp5_coord_div;
     s->parse_vector_adjustment = vp5_parse_vector_adjustment;
     s->parse_coeff = vp5_parse_coeff;
diff --git a/libavcodec/vp56.c b/libavcodec/vp56.c
index b4af2ca..c707295 100644
--- a/libavcodec/vp56.c
+++ b/libavcodec/vp56.c
@@ -36,7 +36,6 @@
     s->quantizer = quantizer;
     s->dequant_dc = vp56_dc_dequant[quantizer] << 2;
     s->dequant_ac = vp56_ac_dequant[quantizer] << 2;
-    memset(s->qscale_table, quantizer, s->mb_width);
 }
 
 static int vp56_get_vectors_predictors(VP56Context *s, int row, int col,
@@ -264,7 +263,7 @@
 
 static void vp56_add_predictors_dc(VP56Context *s, VP56Frame ref_frame)
 {
-    int idx = s->scantable.permutated[0];
+    int idx = s->idct_scantable[0];
     int b;
 
     for (b=0; b<6; b++) {
@@ -314,7 +313,7 @@
 static void vp56_mc(VP56Context *s, int b, int plane, uint8_t *src,
                     int stride, int x, int y)
 {
-    uint8_t *dst=s->framep[VP56_FRAME_CURRENT]->data[plane]+s->block_offset[b];
+    uint8_t *dst = s->frames[VP56_FRAME_CURRENT]->data[plane] + s->block_offset[b];
     uint8_t *src_block;
     int src_offset;
     int overlap_offset = 0;
@@ -325,7 +324,7 @@
 
     if (s->avctx->skip_loop_filter >= AVDISCARD_ALL ||
         (s->avctx->skip_loop_filter >= AVDISCARD_NONKEY
-         && !s->framep[VP56_FRAME_CURRENT]->key_frame))
+         && !s->frames[VP56_FRAME_CURRENT]->key_frame))
         deblock_filtering = 0;
 
     dx = s->mv[b].x / s->vp56_coord_div[b];
@@ -350,9 +349,9 @@
     } else if (deblock_filtering) {
         /* only need a 12x12 block, but there is no such dsp function, */
         /* so copy a 16x12 block */
-        s->dsp.put_pixels_tab[0][0](s->edge_emu_buffer,
-                                    src + s->block_offset[b] + (dy-2)*stride + (dx-2),
-                                    stride, 12);
+        s->hdsp.put_pixels_tab[0][0](s->edge_emu_buffer,
+                                     src + s->block_offset[b] + (dy-2)*stride + (dx-2),
+                                     stride, 12);
         src_block = s->edge_emu_buffer;
         src_offset = 2 + 2*stride;
     } else {
@@ -377,7 +376,7 @@
                                            src_block+src_offset+overlap_offset,
                                            stride, 8);
     } else {
-        s->dsp.put_pixels_tab[1][0](dst, src_block+src_offset, stride, 8);
+        s->hdsp.put_pixels_tab[1][0](dst, src_block+src_offset, stride, 8);
     }
 }
 
@@ -388,7 +387,7 @@
     VP56Frame ref_frame;
     int b, ab, b_max, plane, off;
 
-    if (s->framep[VP56_FRAME_CURRENT]->key_frame)
+    if (s->frames[VP56_FRAME_CURRENT]->key_frame)
         mb_type = VP56_MB_INTRA;
     else
         mb_type = vp56_decode_mv(s, row, col);
@@ -398,8 +397,8 @@
 
     vp56_add_predictors_dc(s, ref_frame);
 
-    frame_current = s->framep[VP56_FRAME_CURRENT];
-    frame_ref = s->framep[ref_frame];
+    frame_current = s->frames[VP56_FRAME_CURRENT];
+    frame_ref = s->frames[ref_frame];
     if (mb_type != VP56_MB_INTRA && !frame_ref->data[0])
         return;
 
@@ -420,9 +419,9 @@
             for (b=0; b<b_max; b++) {
                 plane = ff_vp56_b2p[b+ab];
                 off = s->block_offset[b];
-                s->dsp.put_pixels_tab[1][0](frame_current->data[plane] + off,
-                                            frame_ref->data[plane] + off,
-                                            s->stride[plane], 8);
+                s->hdsp.put_pixels_tab[1][0](frame_current->data[plane] + off,
+                                             frame_ref->data[plane] + off,
+                                             s->stride[plane], 8);
                 s->vp3dsp.idct_add(frame_current->data[plane] + off,
                                 s->stride[plane], s->block_coeff[b]);
             }
@@ -456,7 +455,7 @@
 static int vp56_size_changed(VP56Context *s)
 {
     AVCodecContext *avctx = s->avctx;
-    int stride = s->framep[VP56_FRAME_CURRENT]->linesize[0];
+    int stride = s->frames[VP56_FRAME_CURRENT]->linesize[0];
     int i;
 
     s->plane_width[0]  = s->plane_width[3]  = avctx->coded_width;
@@ -465,7 +464,7 @@
     s->plane_height[1] = s->plane_height[2] = avctx->coded_height/2;
 
     for (i=0; i<4; i++)
-        s->stride[i] = s->flip * s->framep[VP56_FRAME_CURRENT]->linesize[i];
+        s->stride[i] = s->flip * s->frames[VP56_FRAME_CURRENT]->linesize[i];
 
     s->mb_width  = (avctx->coded_width +15) / 16;
     s->mb_height = (avctx->coded_height+15) / 16;
@@ -476,7 +475,6 @@
         return -1;
     }
 
-    s->qscale_table = av_realloc(s->qscale_table, s->mb_width);
     s->above_blocks = av_realloc(s->above_blocks,
                                  (4*s->mb_width+6) * sizeof(*s->above_blocks));
     s->macroblocks = av_realloc(s->macroblocks,
@@ -500,22 +498,11 @@
 {
     const uint8_t *buf = avpkt->data;
     VP56Context *s = avctx->priv_data;
-    AVFrame *p = 0;
+    AVFrame *const p = s->frames[VP56_FRAME_CURRENT];
     int remaining_buf_size = avpkt->size;
     int av_uninit(alpha_offset);
     int i, res;
-
-    /* select a current frame from the unused frames */
-    for (i = 0; i < 4; ++i) {
-        if (!s->frames[i].data[0]) {
-            p = &s->frames[i];
-            break;
-        }
-    }
-    av_assert0(p != 0);
-    s->framep[VP56_FRAME_CURRENT] = p;
-    if (s->alpha_context)
-        s->alpha_context->framep[VP56_FRAME_CURRENT] = p;
+    int ret;
 
     if (s->has_alpha) {
         if (remaining_buf_size < 3)
@@ -532,20 +519,26 @@
 
     if (res == VP56_SIZE_CHANGE) {
         for (i = 0; i < 4; i++) {
-            if (s->frames[i].data[0])
-                avctx->release_buffer(avctx, &s->frames[i]);
+            av_frame_unref(s->frames[i]);
+            if (s->alpha_context)
+                av_frame_unref(s->alpha_context->frames[i]);
         }
     }
 
-    p->reference = 3;
-    if (ff_get_buffer(avctx, p) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if (ff_get_buffer(avctx, p, AV_GET_BUFFER_FLAG_REF) < 0)
         return -1;
+
+    if (s->has_alpha) {
+        av_frame_unref(s->alpha_context->frames[VP56_FRAME_CURRENT]);
+        if ((ret = av_frame_ref(s->alpha_context->frames[VP56_FRAME_CURRENT], p)) < 0) {
+            av_frame_unref(p);
+            return ret;
+        }
     }
 
     if (res == VP56_SIZE_CHANGE) {
         if (vp56_size_changed(s)) {
-            avctx->release_buffer(avctx, p);
+            av_frame_unref(p);
             return -1;
         }
     }
@@ -567,28 +560,15 @@
                 avctx->coded_width  = bak_cw;
                 avctx->coded_height = bak_ch;
             }
-            avctx->release_buffer(avctx, p);
+            av_frame_unref(p);
             return -1;
         }
     }
 
     avctx->execute2(avctx, ff_vp56_decode_mbs, 0, 0, s->has_alpha + 1);
 
-    /* release frames that aren't in use */
-    for (i = 0; i < 4; ++i) {
-        AVFrame *victim = &s->frames[i];
-        if (!victim->data[0])
-            continue;
-        if (victim != s->framep[VP56_FRAME_PREVIOUS] &&
-            victim != s->framep[VP56_FRAME_GOLDEN] &&
-            (!s->has_alpha || victim != s->alpha_context->framep[VP56_FRAME_GOLDEN]))
-            avctx->release_buffer(avctx, victim);
-    }
-
-    p->qstride = 0;
-    p->qscale_table = s->qscale_table;
-    p->qscale_type = FF_QSCALE_TYPE_VP56;
-    *(AVFrame*)data = *p;
+    if ((res = av_frame_ref(data, p)) < 0)
+        return res;
     *got_frame = 1;
 
     return avpkt->size;
@@ -600,9 +580,10 @@
     VP56Context *s0 = avctx->priv_data;
     int is_alpha = (jobnr == 1);
     VP56Context *s = is_alpha ? s0->alpha_context : s0;
-    AVFrame *const p = s->framep[VP56_FRAME_CURRENT];
+    AVFrame *const p = s->frames[VP56_FRAME_CURRENT];
     int mb_row, mb_col, mb_row_flip, mb_offset = 0;
     int block, y, uv, stride_y, stride_uv;
+    int res;
 
     if (p->key_frame) {
         p->pict_type = AV_PICTURE_TYPE_I;
@@ -683,21 +664,24 @@
 
 next:
     if (p->key_frame || s->golden_frame) {
-        s->framep[VP56_FRAME_GOLDEN] = p;
+        av_frame_unref(s->frames[VP56_FRAME_GOLDEN]);
+        if ((res = av_frame_ref(s->frames[VP56_FRAME_GOLDEN], p)) < 0)
+            return res;
     }
 
-    FFSWAP(AVFrame *, s->framep[VP56_FRAME_CURRENT],
-                      s->framep[VP56_FRAME_PREVIOUS]);
+    av_frame_unref(s->frames[VP56_FRAME_PREVIOUS]);
+    FFSWAP(AVFrame *, s->frames[VP56_FRAME_CURRENT],
+                      s->frames[VP56_FRAME_PREVIOUS]);
     return 0;
 }
 
-av_cold void ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha)
+av_cold int ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha)
 {
     VP56Context *s = avctx->priv_data;
-    ff_vp56_init_context(avctx, s, flip, has_alpha);
+    return ff_vp56_init_context(avctx, s, flip, has_alpha);
 }
 
-av_cold void ff_vp56_init_context(AVCodecContext *avctx, VP56Context *s,
+av_cold int ff_vp56_init_context(AVCodecContext *avctx, VP56Context *s,
                                   int flip, int has_alpha)
 {
     int i;
@@ -705,20 +689,24 @@
     s->avctx = avctx;
     avctx->pix_fmt = has_alpha ? AV_PIX_FMT_YUVA420P : AV_PIX_FMT_YUV420P;
 
-    ff_dsputil_init(&s->dsp, avctx);
     ff_h264chroma_init(&s->h264chroma, 8);
+    ff_hpeldsp_init(&s->hdsp, avctx->flags);
     ff_videodsp_init(&s->vdsp, 8);
     ff_vp3dsp_init(&s->vp3dsp, avctx->flags);
     ff_vp56dsp_init(&s->vp56dsp, avctx->codec->id);
-    ff_init_scantable_permutation(s->dsp.idct_permutation, s->vp3dsp.idct_perm);
-    ff_init_scantable(s->dsp.idct_permutation, &s->scantable,ff_zigzag_direct);
-
-    for (i=0; i<4; i++) {
-        s->framep[i] = &s->frames[i];
-        avcodec_get_frame_defaults(&s->frames[i]);
+    for (i = 0; i < 64; i++) {
+#define T(x) (x >> 3) | ((x & 7) << 3)
+        s->idct_scantable[i] = T(ff_zigzag_direct[i]);
+#undef T
     }
-    s->framep[VP56_FRAME_UNUSED] = s->framep[VP56_FRAME_GOLDEN];
-    s->framep[VP56_FRAME_UNUSED2] = s->framep[VP56_FRAME_GOLDEN2];
+
+    for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++) {
+        s->frames[i] = av_frame_alloc();
+        if (!s->frames[i]) {
+            ff_vp56_free(avctx);
+            return AVERROR(ENOMEM);
+        }
+    }
     s->edge_emu_buffer_alloc = NULL;
 
     s->above_blocks = NULL;
@@ -742,6 +730,8 @@
         s->frbi = 0;
         s->srbi = 2;
     }
+
+    return 0;
 }
 
 av_cold int ff_vp56_free(AVCodecContext *avctx)
@@ -752,16 +742,14 @@
 
 av_cold int ff_vp56_free_context(VP56Context *s)
 {
-    AVCodecContext *avctx = s->avctx;
     int i;
 
-    av_freep(&s->qscale_table);
     av_freep(&s->above_blocks);
     av_freep(&s->macroblocks);
     av_freep(&s->edge_emu_buffer_alloc);
-    for (i = 0; i < 4; ++i) {
-        if (s->frames[i].data[0])
-            avctx->release_buffer(avctx, &s->frames[i]);
-    }
+
+    for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++)
+        av_frame_free(&s->frames[i]);
+
     return 0;
 }
diff --git a/libavcodec/vp56.h b/libavcodec/vp56.h
index 14c130f..4b0f5e1 100644
--- a/libavcodec/vp56.h
+++ b/libavcodec/vp56.h
@@ -29,6 +29,7 @@
 #include "vp56data.h"
 #include "dsputil.h"
 #include "get_bits.h"
+#include "hpeldsp.h"
 #include "bytestream.h"
 #include "h264chroma.h"
 #include "videodsp.h"
@@ -95,14 +96,13 @@
 
 struct vp56_context {
     AVCodecContext *avctx;
-    DSPContext dsp;
     H264ChromaContext h264chroma;
+    HpelDSPContext hdsp;
     VideoDSPContext vdsp;
     VP3DSPContext vp3dsp;
     VP56DSPContext vp56dsp;
-    ScanTable scantable;
-    AVFrame frames[4];
-    AVFrame *framep[6];
+    uint8_t idct_scantable[64];
+    AVFrame *frames[4];
     uint8_t *edge_emu_buffer_alloc;
     uint8_t *edge_emu_buffer;
     VP56RangeCoder c;
@@ -121,7 +121,6 @@
     int quantizer;
     uint16_t dequant_dc;
     uint16_t dequant_ac;
-    int8_t *qscale_table;
 
     /* DC predictors management */
     VP56RefDc *above_blocks;
@@ -183,8 +182,8 @@
 };
 
 
-void ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha);
-void ff_vp56_init_context(AVCodecContext *avctx, VP56Context *s,
+int ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha);
+int ff_vp56_init_context(AVCodecContext *avctx, VP56Context *s,
                           int flip, int has_alpha);
 int ff_vp56_free(AVCodecContext *avctx);
 int ff_vp56_free_context(VP56Context *s);
diff --git a/libavcodec/vp56data.h b/libavcodec/vp56data.h
index f30732e..3cafaaf 100644
--- a/libavcodec/vp56data.h
+++ b/libavcodec/vp56data.h
@@ -34,8 +34,6 @@
     VP56_FRAME_PREVIOUS = 1,
     VP56_FRAME_GOLDEN   = 2,
     VP56_FRAME_GOLDEN2  = 3,
-    VP56_FRAME_UNUSED   = 4,
-    VP56_FRAME_UNUSED2  = 5,
 } VP56Frame;
 
 typedef enum {
diff --git a/libavcodec/vp6.c b/libavcodec/vp6.c
index f761aa7..6e385ce 100644
--- a/libavcodec/vp6.c
+++ b/libavcodec/vp6.c
@@ -53,16 +53,16 @@
     int res = 0;
     int separated_coeff = buf[0] & 1;
 
-    s->framep[VP56_FRAME_CURRENT]->key_frame = !(buf[0] & 0x80);
+    s->frames[VP56_FRAME_CURRENT]->key_frame = !(buf[0] & 0x80);
     ff_vp56_init_dequant(s, (buf[0] >> 1) & 0x3F);
 
-    if (s->framep[VP56_FRAME_CURRENT]->key_frame) {
+    if (s->frames[VP56_FRAME_CURRENT]->key_frame) {
         sub_version = buf[1] >> 3;
         if (sub_version > 8)
             return AVERROR_INVALIDDATA;
         s->filter_header = buf[1] & 0x06;
         if (buf[1] & 1) {
-            av_log_missing_feature(s->avctx, "Interlacing", 0);
+            avpriv_report_missing_feature(s->avctx, "Interlacing");
             return AVERROR_PATCHWELCOME;
         }
         if (separated_coeff || !s->filter_header) {
@@ -143,7 +143,7 @@
         buf      += coeff_offset;
         buf_size -= coeff_offset;
         if (buf_size < 0) {
-            if (s->framep[VP56_FRAME_CURRENT]->key_frame)
+            if (s->frames[VP56_FRAME_CURRENT]->key_frame)
                 avcodec_set_dimensions(s->avctx, 0, 0);
             return AVERROR_INVALIDDATA;
         }
@@ -258,7 +258,7 @@
             if (vp56_rac_get_prob(c, vp6_dccv_pct[pt][node])) {
                 def_prob[node] = vp56_rac_gets_nn(c, 7);
                 model->coeff_dccv[pt][node] = def_prob[node];
-            } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {
+            } else if (s->frames[VP56_FRAME_CURRENT]->key_frame) {
                 model->coeff_dccv[pt][node] = def_prob[node];
             }
 
@@ -281,7 +281,7 @@
                     if (vp56_rac_get_prob(c, vp6_ract_pct[ct][pt][cg][node])) {
                         def_prob[node] = vp56_rac_gets_nn(c, 7);
                         model->coeff_ract[pt][ct][cg][node] = def_prob[node];
-                    } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {
+                    } else if (s->frames[VP56_FRAME_CURRENT]->key_frame) {
                         model->coeff_ract[pt][ct][cg][node] = def_prob[node];
                     }
 
@@ -368,7 +368,7 @@
 static void vp6_parse_coeff_huffman(VP56Context *s)
 {
     VP56Model *model = s->modelp;
-    uint8_t *permute = s->scantable.permutated;
+    uint8_t *permute = s->idct_scantable;
     VLC *vlc_coeff;
     int coeff, sign, coeff_idx;
     int b, cg, idx;
@@ -428,7 +428,7 @@
 {
     VP56RangeCoder *c = s->ccp;
     VP56Model *model = s->modelp;
-    uint8_t *permute = s->scantable.permutated;
+    uint8_t *permute = s->idct_scantable;
     uint8_t *model1, *model2, *model3;
     int coeff, sign, coeff_idx;
     int b, i, cg, idx, ctx;
@@ -594,20 +594,19 @@
 static av_cold int vp6_decode_init(AVCodecContext *avctx)
 {
     VP56Context *s = avctx->priv_data;
+    int ret;
 
-    ff_vp56_init(avctx, avctx->codec->id == AV_CODEC_ID_VP6,
-                        avctx->codec->id == AV_CODEC_ID_VP6A);
+    if ((ret = ff_vp56_init(avctx, avctx->codec->id == AV_CODEC_ID_VP6,
+                            avctx->codec->id == AV_CODEC_ID_VP6A)) < 0)
+        return ret;
+
     vp6_decode_init_context(s);
 
     if (s->has_alpha) {
-        int i;
-
         s->alpha_context = av_mallocz(sizeof(VP56Context));
         ff_vp56_init_context(avctx, s->alpha_context,
                              s->flip == -1, s->has_alpha);
         vp6_decode_init_context(s->alpha_context);
-        for (i = 0; i < 6; ++i)
-            s->alpha_context->framep[i] = s->framep[i];
     }
 
     return 0;
diff --git a/libavcodec/vp8.c b/libavcodec/vp8.c
index 5da1a9f..553e5c8 100644
--- a/libavcodec/vp8.c
+++ b/libavcodec/vp8.c
@@ -56,64 +56,59 @@
     s->macroblocks = NULL;
 }
 
-static int vp8_alloc_frame(VP8Context *s, AVFrame *f)
+static int vp8_alloc_frame(VP8Context *s, VP8Frame *f, int ref)
 {
     int ret;
-    if ((ret = ff_thread_get_buffer(s->avctx, f)) < 0)
+    if ((ret = ff_thread_get_buffer(s->avctx, &f->tf,
+                                    ref ? AV_GET_BUFFER_FLAG_REF : 0)) < 0)
         return ret;
-    if (s->num_maps_to_be_freed && !s->maps_are_invalid) {
-        f->ref_index[0] = s->segmentation_maps[--s->num_maps_to_be_freed];
-    } else if (!(f->ref_index[0] = av_mallocz(s->mb_width * s->mb_height))) {
-        ff_thread_release_buffer(s->avctx, f);
+    if (!(f->seg_map = av_buffer_allocz(s->mb_width * s->mb_height))) {
+        ff_thread_release_buffer(s->avctx, &f->tf);
         return AVERROR(ENOMEM);
     }
     return 0;
 }
 
-static void vp8_release_frame(VP8Context *s, AVFrame *f, int prefer_delayed_free, int can_direct_free)
+static void vp8_release_frame(VP8Context *s, VP8Frame *f)
 {
-    if (f->ref_index[0]) {
-        if (prefer_delayed_free) {
-            /* Upon a size change, we want to free the maps but other threads may still
-             * be using them, so queue them. Upon a seek, all threads are inactive so
-             * we want to cache one to prevent re-allocation in the next decoding
-             * iteration, but the rest we can free directly. */
-            int max_queued_maps = can_direct_free ? 1 : FF_ARRAY_ELEMS(s->segmentation_maps);
-            if (s->num_maps_to_be_freed < max_queued_maps) {
-                s->segmentation_maps[s->num_maps_to_be_freed++] = f->ref_index[0];
-            } else if (can_direct_free) /* vp8_decode_flush(), but our queue is full */ {
-                av_free(f->ref_index[0]);
-            } /* else: MEMLEAK (should never happen, but better that than crash) */
-            f->ref_index[0] = NULL;
-        } else /* vp8_decode_free() */ {
-            av_free(f->ref_index[0]);
-        }
-    }
-    ff_thread_release_buffer(s->avctx, f);
+    av_buffer_unref(&f->seg_map);
+    ff_thread_release_buffer(s->avctx, &f->tf);
 }
 
-static void vp8_decode_flush_impl(AVCodecContext *avctx,
-                                  int prefer_delayed_free, int can_direct_free, int free_mem)
+static int vp8_ref_frame(VP8Context *s, VP8Frame *dst, VP8Frame *src)
+{
+    int ret;
+
+    vp8_release_frame(s, dst);
+
+    if ((ret = ff_thread_ref_frame(&dst->tf, &src->tf)) < 0)
+        return ret;
+    if (src->seg_map &&
+        !(dst->seg_map = av_buffer_ref(src->seg_map))) {
+        vp8_release_frame(s, dst);
+        return AVERROR(ENOMEM);
+    }
+
+    return 0;
+}
+
+
+static void vp8_decode_flush_impl(AVCodecContext *avctx, int free_mem)
 {
     VP8Context *s = avctx->priv_data;
     int i;
 
-    if (!avctx->internal->is_copy) {
-        for (i = 0; i < 5; i++)
-            if (s->frames[i].data[0])
-                vp8_release_frame(s, &s->frames[i], prefer_delayed_free, can_direct_free);
-    }
+    for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++)
+        vp8_release_frame(s, &s->frames[i]);
     memset(s->framep, 0, sizeof(s->framep));
 
-    if (free_mem) {
+    if (free_mem)
         free_buffers(s);
-        s->maps_are_invalid = 1;
-    }
 }
 
 static void vp8_decode_flush(AVCodecContext *avctx)
 {
-    vp8_decode_flush_impl(avctx, 1, 1, 0);
+    vp8_decode_flush_impl(avctx, 0);
 }
 
 static int update_dimensions(VP8Context *s, int width, int height)
@@ -126,7 +121,7 @@
         if (av_image_check_size(width, height, 0, s->avctx))
             return AVERROR_INVALIDDATA;
 
-        vp8_decode_flush_impl(s->avctx, 1, 0, 1);
+        vp8_decode_flush_impl(s->avctx, 1);
 
         avcodec_set_dimensions(s->avctx, width, height);
     }
@@ -346,7 +341,7 @@
         buf_size -= 7;
 
         if (hscale || vscale)
-            av_log_missing_feature(s->avctx, "Upscaling", 1);
+            avpriv_request_sample(s->avctx, "Upscaling");
 
         s->update_golden = s->update_altref = VP56_FRAME_CURRENT;
         for (i = 0; i < 4; i++)
@@ -1183,12 +1178,12 @@
  */
 static av_always_inline
 void vp8_mc_luma(VP8Context *s, VP8ThreadData *td, uint8_t *dst,
-                 AVFrame *ref, const VP56mv *mv,
+                 ThreadFrame *ref, const VP56mv *mv,
                  int x_off, int y_off, int block_w, int block_h,
                  int width, int height, int linesize,
                  vp8_mc_func mc_func[3][3])
 {
-    uint8_t *src = ref->data[0];
+    uint8_t *src = ref->f->data[0];
 
     if (AV_RN32A(mv)) {
 
@@ -1234,11 +1229,11 @@
  */
 static av_always_inline
 void vp8_mc_chroma(VP8Context *s, VP8ThreadData *td, uint8_t *dst1, uint8_t *dst2,
-                   AVFrame *ref, const VP56mv *mv, int x_off, int y_off,
+                   ThreadFrame *ref, const VP56mv *mv, int x_off, int y_off,
                    int block_w, int block_h, int width, int height, int linesize,
                    vp8_mc_func mc_func[3][3])
 {
-    uint8_t *src1 = ref->data[1], *src2 = ref->data[2];
+    uint8_t *src1 = ref->f->data[1], *src2 = ref->f->data[2];
 
     if (AV_RN32A(mv)) {
         int mx = mv->x&7, mx_idx = subpel_idx[0][mx];
@@ -1277,7 +1272,7 @@
 
 static av_always_inline
 void vp8_mc_part(VP8Context *s, VP8ThreadData *td, uint8_t *dst[3],
-                 AVFrame *ref_frame, int x_off, int y_off,
+                 ThreadFrame *ref_frame, int x_off, int y_off,
                  int bx_off, int by_off,
                  int block_w, int block_h,
                  int width, int height, VP56mv *mv)
@@ -1315,7 +1310,7 @@
         int x_off = mb_x << 4, y_off = mb_y << 4;
         int mx = (mb->mv.x>>2) + x_off + 8;
         int my = (mb->mv.y>>2) + y_off;
-        uint8_t **src= s->framep[ref]->data;
+        uint8_t **src= s->framep[ref]->tf.f->data;
         int off= mx + (my + (mb_x&3)*4)*s->linesize + 64;
         /* For threading, a ff_thread_await_progress here might be useful, but
          * it actually slows down the decoder. Since a bad prefetch doesn't
@@ -1335,7 +1330,7 @@
 {
     int x_off = mb_x << 4, y_off = mb_y << 4;
     int width = 16*s->mb_width, height = 16*s->mb_height;
-    AVFrame *ref = s->framep[mb->ref_frame];
+    ThreadFrame *ref = &s->framep[mb->ref_frame]->tf;
     VP56mv *bmv = mb->bmv;
 
     switch (mb->partitioning) {
@@ -1594,17 +1589,9 @@
     }
 }
 
-static void release_queued_segmaps(VP8Context *s, int is_close)
-{
-    int leave_behind = is_close ? 0 : !s->maps_are_invalid;
-    while (s->num_maps_to_be_freed > leave_behind)
-        av_freep(&s->segmentation_maps[--s->num_maps_to_be_freed]);
-    s->maps_are_invalid = 0;
-}
-
 #define MARGIN (16 << 2)
-static void vp8_decode_mv_mb_modes(AVCodecContext *avctx, AVFrame *curframe,
-                                   AVFrame *prev_frame)
+static void vp8_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *curframe,
+                                   VP8Frame *prev_frame)
 {
     VP8Context *s = avctx->priv_data;
     int mb_x, mb_y;
@@ -1622,8 +1609,9 @@
         for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb_xy++, mb++) {
             if (mb_y == 0)
                 AV_WN32A((mb-s->mb_width-1)->intra4x4_pred_mode_top, DC_PRED*0x01010101);
-            decode_mb_mode(s, mb, mb_x, mb_y, curframe->ref_index[0] + mb_xy,
-                           prev_frame && prev_frame->ref_index[0] ? prev_frame->ref_index[0] + mb_xy : NULL, 1);
+            decode_mb_mode(s, mb, mb_x, mb_y, curframe->seg_map->data + mb_xy,
+                           prev_frame && prev_frame->seg_map ?
+                           prev_frame->seg_map->data + mb_xy : NULL, 1);
             s->mv_min.x -= 64;
             s->mv_max.x -= 64;
         }
@@ -1677,13 +1665,13 @@
     int mb_y = td->thread_mb_pos>>16;
     int i, y, mb_x, mb_xy = mb_y*s->mb_width;
     int num_jobs = s->num_jobs;
-    AVFrame *curframe = s->curframe, *prev_frame = s->prev_frame;
+    VP8Frame *curframe = s->curframe, *prev_frame = s->prev_frame;
     VP56RangeCoder *c = &s->coeff_partition[mb_y & (s->num_coeff_partitions-1)];
     VP8Macroblock *mb;
     uint8_t *dst[3] = {
-        curframe->data[0] + 16*mb_y*s->linesize,
-        curframe->data[1] +  8*mb_y*s->uvlinesize,
-        curframe->data[2] +  8*mb_y*s->uvlinesize
+        curframe->tf.f->data[0] + 16*mb_y*s->linesize,
+        curframe->tf.f->data[1] +  8*mb_y*s->uvlinesize,
+        curframe->tf.f->data[2] +  8*mb_y*s->uvlinesize
     };
     if (mb_y == 0) prev_td = td;
     else           prev_td = &s->thread_data[(jobnr + num_jobs - 1)%num_jobs];
@@ -1702,7 +1690,7 @@
     if (!(avctx->flags & CODEC_FLAG_EMU_EDGE)) {
         for (i = 0; i < 3; i++)
             for (y = 0; y < 16>>!!i; y++)
-                dst[i][y*curframe->linesize[i]-1] = 129;
+                dst[i][y*curframe->tf.f->linesize[i]-1] = 129;
         if (mb_y == 1) {
             s->top_border[0][15] = s->top_border[0][23] = s->top_border[0][31] = 129;
         }
@@ -1725,8 +1713,9 @@
         s->vdsp.prefetch(dst[1] + (mb_x&7)*s->uvlinesize + 64, dst[2] - dst[1], 2);
 
         if (!s->mb_layout)
-            decode_mb_mode(s, mb, mb_x, mb_y, curframe->ref_index[0] + mb_xy,
-                           prev_frame && prev_frame->ref_index[0] ? prev_frame->ref_index[0] + mb_xy : NULL, 0);
+            decode_mb_mode(s, mb, mb_x, mb_y, curframe->seg_map->data + mb_xy,
+                           prev_frame && prev_frame->seg_map ?
+                           prev_frame->seg_map->data + mb_xy : NULL, 0);
 
         prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_PREVIOUS);
 
@@ -1785,7 +1774,7 @@
     VP8Context *s = avctx->priv_data;
     VP8ThreadData *td = &s->thread_data[threadnr];
     int mb_x, mb_y = td->thread_mb_pos>>16, num_jobs = s->num_jobs;
-    AVFrame *curframe = s->curframe;
+    AVFrame *curframe = s->curframe->tf.f;
     VP8Macroblock *mb;
     VP8ThreadData *prev_td, *next_td;
     uint8_t *dst[3] = {
@@ -1839,7 +1828,7 @@
     VP8Context *s = avctx->priv_data;
     VP8ThreadData *td = &s->thread_data[jobnr];
     VP8ThreadData *next_td = NULL, *prev_td = NULL;
-    AVFrame *curframe = s->curframe;
+    VP8Frame *curframe = s->curframe;
     int mb_y, num_jobs = s->num_jobs;
     td->thread_nr = threadnr;
     for (mb_y = jobnr; mb_y < s->mb_height; mb_y += num_jobs) {
@@ -1854,7 +1843,7 @@
         s->mv_max.y -= 64;
 
         if (avctx->active_thread_type == FF_THREAD_FRAME)
-            ff_thread_report_progress(curframe, mb_y, 0);
+            ff_thread_report_progress(&curframe->tf, mb_y, 0);
     }
 
     return 0;
@@ -1866,9 +1855,7 @@
     VP8Context *s = avctx->priv_data;
     int ret, i, referenced, num_jobs;
     enum AVDiscard skip_thresh;
-    AVFrame *av_uninit(curframe), *prev_frame;
-
-    release_queued_segmaps(s, 0);
+    VP8Frame *av_uninit(curframe), *prev_frame;
 
     if ((ret = decode_frame_header(s, avpkt->data, avpkt->size)) < 0)
         goto err;
@@ -1890,12 +1877,12 @@
 
     // release no longer referenced frames
     for (i = 0; i < 5; i++)
-        if (s->frames[i].data[0] &&
+        if (s->frames[i].tf.f->data[0] &&
             &s->frames[i] != prev_frame &&
             &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
             &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
             &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2])
-            vp8_release_frame(s, &s->frames[i], 1, 0);
+            vp8_release_frame(s, &s->frames[i]);
 
     // find a free buffer
     for (i = 0; i < 5; i++)
@@ -1910,8 +1897,8 @@
         av_log(avctx, AV_LOG_FATAL, "Ran out of free frames!\n");
         abort();
     }
-    if (curframe->data[0])
-        vp8_release_frame(s, curframe, 1, 0);
+    if (curframe->tf.f->data[0])
+        vp8_release_frame(s, curframe);
 
     // Given that arithmetic probabilities are updated every frame, it's quite likely
     // that the values we have on a random interframe are complete junk if we didn't
@@ -1924,13 +1911,10 @@
         goto err;
     }
 
-    curframe->key_frame = s->keyframe;
-    curframe->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
-    curframe->reference = referenced ? 3 : 0;
-    if ((ret = vp8_alloc_frame(s, curframe))) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed!\n");
+    curframe->tf.f->key_frame = s->keyframe;
+    curframe->tf.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
+    if ((ret = vp8_alloc_frame(s, curframe, referenced)) < 0)
         goto err;
-    }
 
     // check if golden and altref are swapped
     if (s->update_altref != VP56_FRAME_NONE) {
@@ -1952,8 +1936,8 @@
 
     ff_thread_finish_setup(avctx);
 
-    s->linesize   = curframe->linesize[0];
-    s->uvlinesize = curframe->linesize[1];
+    s->linesize   = curframe->tf.f->linesize[0];
+    s->uvlinesize = curframe->tf.f->linesize[1];
 
     if (!s->thread_data[0].edge_emu_buffer)
         for (i = 0; i < MAX_THREADS; i++)
@@ -1978,7 +1962,7 @@
     // Make sure the previous frame has read its segmentation map,
     // if we re-use the same map.
     if (prev_frame && s->segmentation.enabled && !s->segmentation.update_map)
-        ff_thread_await_progress(prev_frame, 1, 0);
+        ff_thread_await_progress(&prev_frame->tf, 1, 0);
 
     if (s->mb_layout == 1)
         vp8_decode_mv_mb_modes(avctx, curframe, prev_frame);
@@ -1998,7 +1982,7 @@
     }
     avctx->execute2(avctx, vp8_decode_mb_row_sliced, s->thread_data, NULL, num_jobs);
 
-    ff_thread_report_progress(curframe, INT_MAX, 0);
+    ff_thread_report_progress(&curframe->tf, INT_MAX, 0);
     memcpy(&s->framep[0], &s->next_framep[0], sizeof(s->framep[0]) * 4);
 
 skip_decode:
@@ -2008,7 +1992,8 @@
         s->prob[0] = s->prob[1];
 
     if (!s->invisible) {
-        *(AVFrame*)data = *curframe;
+        if ((ret = av_frame_ref(data, curframe->tf.f)) < 0)
+            return ret;
         *got_frame      = 1;
     }
 
@@ -2018,33 +2003,62 @@
     return ret;
 }
 
+static av_cold int vp8_decode_free(AVCodecContext *avctx)
+{
+    VP8Context *s = avctx->priv_data;
+    int i;
+
+    vp8_decode_flush_impl(avctx, 1);
+    for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++)
+        av_frame_free(&s->frames[i].tf.f);
+
+    return 0;
+}
+
+static av_cold int vp8_init_frames(VP8Context *s)
+{
+    int i;
+    for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++) {
+        s->frames[i].tf.f = av_frame_alloc();
+        if (!s->frames[i].tf.f)
+            return AVERROR(ENOMEM);
+    }
+    return 0;
+}
+
 static av_cold int vp8_decode_init(AVCodecContext *avctx)
 {
     VP8Context *s = avctx->priv_data;
+    int ret;
 
     s->avctx = avctx;
     avctx->pix_fmt = AV_PIX_FMT_YUV420P;
+    avctx->internal->allocate_progress = 1;
 
     ff_videodsp_init(&s->vdsp, 8);
     ff_h264_pred_init(&s->hpc, AV_CODEC_ID_VP8, 8, 1);
     ff_vp8dsp_init(&s->vp8dsp);
 
-    return 0;
-}
+    if ((ret = vp8_init_frames(s)) < 0) {
+        vp8_decode_free(avctx);
+        return ret;
+    }
 
-static av_cold int vp8_decode_free(AVCodecContext *avctx)
-{
-    vp8_decode_flush_impl(avctx, 0, 1, 1);
-    release_queued_segmaps(avctx->priv_data, 1);
     return 0;
 }
 
 static av_cold int vp8_decode_init_thread_copy(AVCodecContext *avctx)
 {
     VP8Context *s = avctx->priv_data;
+    int ret;
 
     s->avctx = avctx;
 
+    if ((ret = vp8_init_frames(s)) < 0) {
+        vp8_decode_free(avctx);
+        return ret;
+    }
+
     return 0;
 }
 
@@ -2054,11 +2068,11 @@
 static int vp8_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
 {
     VP8Context *s = dst->priv_data, *s_src = src->priv_data;
+    int i;
 
     if (s->macroblocks_base &&
         (s_src->mb_width != s->mb_width || s_src->mb_height != s->mb_height)) {
         free_buffers(s);
-        s->maps_are_invalid = 1;
         s->mb_width  = s_src->mb_width;
         s->mb_height = s_src->mb_height;
     }
@@ -2068,7 +2082,14 @@
     s->lf_delta = s_src->lf_delta;
     memcpy(s->sign_bias, s_src->sign_bias, sizeof(s->sign_bias));
 
-    memcpy(&s->frames, &s_src->frames, sizeof(s->frames));
+    for (i = 0; i < FF_ARRAY_ELEMS(s_src->frames); i++) {
+        if (s_src->frames[i].tf.f->data[0]) {
+            int ret = vp8_ref_frame(s, &s->frames[i], &s_src->frames[i]);
+            if (ret < 0)
+                return ret;
+        }
+    }
+
     s->framep[0] = REBASE(s_src->next_framep[0]);
     s->framep[1] = REBASE(s_src->next_framep[1]);
     s->framep[2] = REBASE(s_src->next_framep[2]);
@@ -2077,6 +2098,52 @@
     return 0;
 }
 
+static unsigned apply_padding(unsigned size) { return size + (size & 1); }
+
+static int webp_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
+                             AVPacket *avpkt)
+{
+    const uint8_t *buf = avpkt->data;
+    int buf_size       = avpkt->size;
+    AVPacket pkt       = *avpkt;
+
+    if (buf_size >= 16
+        && AV_RL32(buf   ) == AV_RL32("RIFF")
+        && AV_RL32(buf+ 8) == AV_RL32("WEBP")) {
+        unsigned riff_size = apply_padding(AV_RL32(buf+4)) + 8;
+        buf += 12;   // Skip over main header
+        buf_size -= 12;
+        if (buf_size < 8 || riff_size < 8) {
+            av_log(avctx, AV_LOG_ERROR, "Incomplete header.\n");
+            return AVERROR_INVALIDDATA;
+        }
+        if (AV_RL32(buf) == AV_RL32("VP8L")) {
+            av_log(avctx, AV_LOG_ERROR, "Unsupported WebP lossless format.\n");
+            return AVERROR_PATCHWELCOME;
+        }
+        if (AV_RL32(buf) == AV_RL32("VP8X") && AV_RL32(buf+4) < (unsigned)buf_size) {
+            unsigned size = apply_padding(AV_RL32(buf+4) + 8);
+            buf      += size;
+            buf_size -= size;
+        }
+        if (buf_size >= 8
+            && AV_RL32(buf) == AV_RL32("ALPH") && AV_RL32(buf+4) < (unsigned)buf_size) {
+            unsigned size = apply_padding(AV_RL32(buf+4) + 8);
+            buf      += size;
+            buf_size -= size;
+            av_log(avctx, AV_LOG_WARNING, "Skipping alpha plane\n");
+        }
+        if (buf_size >= 8 && AV_RL32(buf) == AV_RL32("VP8 ")) {
+            buf      += 8;
+            buf_size -= 8;
+        }
+    }
+    pkt.data = buf;
+    pkt.size = buf_size;
+
+    return vp8_decode_frame(avctx, data, data_size, &pkt);
+}
+
 AVCodec ff_vp8_decoder = {
     .name                  = "vp8",
     .type                  = AVMEDIA_TYPE_VIDEO,
@@ -2091,3 +2158,18 @@
     .init_thread_copy      = ONLY_IF_THREADS_ENABLED(vp8_decode_init_thread_copy),
     .update_thread_context = ONLY_IF_THREADS_ENABLED(vp8_decode_update_thread_context),
 };
+
+AVCodec ff_webp_decoder = {
+    .name                  = "webp",
+    .type                  = AVMEDIA_TYPE_VIDEO,
+    .id                    = AV_CODEC_ID_WEBP,
+    .priv_data_size        = sizeof(VP8Context),
+    .init                  = vp8_decode_init,
+    .close                 = vp8_decode_free,
+    .decode                = webp_decode_frame,
+    .capabilities          = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS,
+    .flush                 = vp8_decode_flush,
+    .long_name             = NULL_IF_CONFIG_SMALL("WebP"),
+    .init_thread_copy      = ONLY_IF_THREADS_ENABLED(vp8_decode_init_thread_copy),
+    .update_thread_context = ONLY_IF_THREADS_ENABLED(vp8_decode_update_thread_context),
+};
diff --git a/libavcodec/vp8.h b/libavcodec/vp8.h
index 1976390..90109ad 100644
--- a/libavcodec/vp8.h
+++ b/libavcodec/vp8.h
@@ -26,10 +26,13 @@
 #ifndef AVCODEC_VP8_H
 #define AVCODEC_VP8_H
 
+#include "libavutil/buffer.h"
+
 #include "vp56.h"
 #include "vp56data.h"
 #include "vp8dsp.h"
 #include "h264pred.h"
+#include "thread.h"
 #if HAVE_PTHREADS
 #include <pthread.h>
 #elif HAVE_W32THREADS
@@ -124,14 +127,19 @@
     VP8FilterStrength *filter_strength;
 } VP8ThreadData;
 
+typedef struct VP8Frame {
+    ThreadFrame tf;
+    AVBufferRef *seg_map;
+} VP8Frame;
+
 #define MAX_THREADS 8
 typedef struct VP8Context {
     VP8ThreadData *thread_data;
     AVCodecContext *avctx;
-    AVFrame *framep[4];
-    AVFrame *next_framep[4];
-    AVFrame *curframe;
-    AVFrame *prev_frame;
+    VP8Frame *framep[4];
+    VP8Frame *next_framep[4];
+    VP8Frame *curframe;
+    VP8Frame *prev_frame;
 
     uint16_t mb_width;   /* number of horizontal MB */
     uint16_t mb_height;  /* number of vertical MB */
@@ -253,17 +261,8 @@
     VP8DSPContext vp8dsp;
     H264PredContext hpc;
     vp8_mc_func put_pixels_tab[3][3][3];
-    AVFrame frames[5];
+    VP8Frame frames[5];
 
-    /**
-     * A list of segmentation_map buffers that are to be free()'ed in
-     * the next decoding iteration. We can't free() them right away
-     * because the map may still be used by subsequent decoding threads.
-     * Unused if frame threading is off.
-     */
-    uint8_t *segmentation_maps[5];
-    int num_maps_to_be_freed;
-    int maps_are_invalid;
     int num_jobs;
     /**
      * This describes the macroblock memory layout.
diff --git a/libavcodec/vp8dsp.c b/libavcodec/vp8dsp.c
index 017278e..a61a59e 100644
--- a/libavcodec/vp8dsp.c
+++ b/libavcodec/vp8dsp.c
@@ -160,7 +160,7 @@
 {
     LOAD_PIXELS
     int a, f1, f2;
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
 
     a = 3*(q0 - p0);
 
@@ -215,7 +215,7 @@
 static av_always_inline void filter_mbedge(uint8_t *p, ptrdiff_t stride)
 {
     int a0, a1, a2, w;
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
 
     LOAD_PIXELS
 
@@ -337,7 +337,7 @@
 static void put_vp8_epel ## SIZE ## _h ## TAPS ## _c(uint8_t *dst, ptrdiff_t dststride, uint8_t *src, ptrdiff_t srcstride, int h, int mx, int my) \
 { \
     const uint8_t *filter = subpel_filters[mx-1]; \
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP; \
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP; \
     int x, y; \
 \
     for (y = 0; y < h; y++) { \
@@ -351,7 +351,7 @@
 static void put_vp8_epel ## SIZE ## _v ## TAPS ## _c(uint8_t *dst, ptrdiff_t dststride, uint8_t *src, ptrdiff_t srcstride, int h, int mx, int my) \
 { \
     const uint8_t *filter = subpel_filters[my-1]; \
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP; \
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP; \
     int x, y; \
 \
     for (y = 0; y < h; y++) { \
@@ -365,7 +365,7 @@
 static void put_vp8_epel ## SIZE ## _h ## HTAPS ## v ## VTAPS ## _c(uint8_t *dst, ptrdiff_t dststride, uint8_t *src, ptrdiff_t srcstride, int h, int mx, int my) \
 { \
     const uint8_t *filter = subpel_filters[mx-1]; \
-    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP; \
+    const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP; \
     int x, y; \
     uint8_t tmp_array[(2*SIZE+VTAPS-1)*SIZE]; \
     uint8_t *tmp = tmp_array; \
diff --git a/libavcodec/vqavideo.c b/libavcodec/vqavideo.c
index 43b3aaf..a47e2db 100644
--- a/libavcodec/vqavideo.c
+++ b/libavcodec/vqavideo.c
@@ -94,7 +94,6 @@
 typedef struct VqaContext {
 
     AVCodecContext *avctx;
-    AVFrame frame;
     GetByteContext gb;
 
     uint32_t palette[PALETTE_COUNT];
@@ -191,9 +190,6 @@
     }
     s->next_codebook_buffer_index = 0;
 
-    avcodec_get_frame_defaults(&s->frame);
-    s->frame.data[0] = NULL;
-
     return 0;
 fail:
     av_freep(&s->codebook);
@@ -307,7 +303,7 @@
     return 0; // let's display what we decoded anyway
 }
 
-static int vqa_decode_chunk(VqaContext *s)
+static int vqa_decode_chunk(VqaContext *s, AVFrame *frame)
 {
     unsigned int chunk_type;
     unsigned int chunk_size;
@@ -476,7 +472,7 @@
         index_shift = 3;
     for (y = 0; y < s->height; y += s->vector_height) {
         for (x = 0; x < s->width; x += 4, lobytes++, hibytes++) {
-            pixel_ptr = y * s->frame.linesize[0] + x;
+            pixel_ptr = y * frame->linesize[0] + x;
 
             /* get the vector index, the method for which varies according to
              * VQA file version */
@@ -491,11 +487,11 @@
                 /* uniform color fill - a quick hack */
                 if (hibyte == 0xFF) {
                     while (lines--) {
-                        s->frame.data[0][pixel_ptr + 0] = 255 - lobyte;
-                        s->frame.data[0][pixel_ptr + 1] = 255 - lobyte;
-                        s->frame.data[0][pixel_ptr + 2] = 255 - lobyte;
-                        s->frame.data[0][pixel_ptr + 3] = 255 - lobyte;
-                        pixel_ptr += s->frame.linesize[0];
+                        frame->data[0][pixel_ptr + 0] = 255 - lobyte;
+                        frame->data[0][pixel_ptr + 1] = 255 - lobyte;
+                        frame->data[0][pixel_ptr + 2] = 255 - lobyte;
+                        frame->data[0][pixel_ptr + 3] = 255 - lobyte;
+                        pixel_ptr += frame->linesize[0];
                     }
                     lines=0;
                 }
@@ -516,11 +512,11 @@
             }
 
             while (lines--) {
-                s->frame.data[0][pixel_ptr + 0] = s->codebook[vector_index++];
-                s->frame.data[0][pixel_ptr + 1] = s->codebook[vector_index++];
-                s->frame.data[0][pixel_ptr + 2] = s->codebook[vector_index++];
-                s->frame.data[0][pixel_ptr + 3] = s->codebook[vector_index++];
-                pixel_ptr += s->frame.linesize[0];
+                frame->data[0][pixel_ptr + 0] = s->codebook[vector_index++];
+                frame->data[0][pixel_ptr + 1] = s->codebook[vector_index++];
+                frame->data[0][pixel_ptr + 2] = s->codebook[vector_index++];
+                frame->data[0][pixel_ptr + 3] = s->codebook[vector_index++];
+                pixel_ptr += frame->linesize[0];
             }
         }
     }
@@ -599,26 +595,21 @@
                             AVPacket *avpkt)
 {
     VqaContext *s = avctx->priv_data;
+    AVFrame *frame = data;
     int res;
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
-
-    if ((res = ff_get_buffer(avctx, &s->frame)) < 0) {
-        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((res = ff_get_buffer(avctx, frame, 0)) < 0)
         return res;
-    }
 
     bytestream2_init(&s->gb, avpkt->data, avpkt->size);
-    if ((res = vqa_decode_chunk(s)) < 0)
+    if ((res = vqa_decode_chunk(s, frame)) < 0)
         return res;
 
     /* make the palette available on the way out */
-    memcpy(s->frame.data[1], s->palette, PALETTE_COUNT * 4);
-    s->frame.palette_has_changed = 1;
+    memcpy(frame->data[1], s->palette, PALETTE_COUNT * 4);
+    frame->palette_has_changed = 1;
 
     *got_frame      = 1;
-    *(AVFrame*)data = s->frame;
 
     /* report that the buffer was completely consumed */
     return avpkt->size;
@@ -632,9 +623,6 @@
     av_freep(&s->next_codebook_buffer);
     av_freep(&s->decode_buffer);
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
-
     return 0;
 }
 
diff --git a/libavcodec/wavpack.c b/libavcodec/wavpack.c
index b5663b4..481d323 100644
--- a/libavcodec/wavpack.c
+++ b/libavcodec/wavpack.c
@@ -373,6 +373,10 @@
         INC_MED(2);
     }
     if (!c->error_limit) {
+        if (add >= 0x2000000U) {
+            av_log(ctx->avctx, AV_LOG_ERROR, "k %d is too large\n", add);
+            goto error;
+        }
         ret = base + get_tail(gb, add);
         if (get_bits_left(gb) <= 0)
             goto error;
@@ -1216,10 +1220,8 @@
 
     /* get output buffer */
     frame->nb_samples = s->samples + 1;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     frame->nb_samples = s->samples;
 
     while (buf_size > 0) {
diff --git a/libavcodec/wma.c b/libavcodec/wma.c
index e704223..1e6ca61 100644
--- a/libavcodec/wma.c
+++ b/libavcodec/wma.c
@@ -390,11 +390,6 @@
         av_free(s->int_table[i]);
     }
 
-#if FF_API_OLD_ENCODE_AUDIO
-    if (av_codec_is_encoder(avctx->codec))
-        av_freep(&avctx->coded_frame);
-#endif
-
     return 0;
 }
 
diff --git a/libavcodec/wmadec.c b/libavcodec/wmadec.c
index df5089f..0648c1e 100644
--- a/libavcodec/wmadec.c
+++ b/libavcodec/wmadec.c
@@ -72,6 +72,11 @@
     int i, flags2;
     uint8_t *extradata;
 
+    if (!avctx->block_align) {
+        av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
+        return AVERROR(EINVAL);
+    }
+
     s->avctx = avctx;
 
     /* extract flag infos */
@@ -827,16 +832,18 @@
         /* read super frame header */
         skip_bits(&s->gb, 4); /* super frame index */
         nb_frames = get_bits(&s->gb, 4) - (s->last_superframe_len <= 0);
+        if (nb_frames <= 0) {
+            av_log(avctx, AV_LOG_ERROR, "nb_frames is %d\n", nb_frames);
+            return AVERROR_INVALIDDATA;
+        }
     } else {
         nb_frames = 1;
     }
 
     /* get output buffer */
     frame->nb_samples = nb_frames * s->frame_len;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     samples = (float **)frame->extended_data;
     samples_offset = 0;
 
diff --git a/libavcodec/wmaenc.c b/libavcodec/wmaenc.c
index 0ee5dda..799535e 100644
--- a/libavcodec/wmaenc.c
+++ b/libavcodec/wmaenc.c
@@ -50,11 +50,6 @@
         return AVERROR(EINVAL);
     }
 
-#if FF_API_OLD_ENCODE_AUDIO
-    if (!(avctx->coded_frame = avcodec_alloc_frame()))
-        return AVERROR(ENOMEM);
-#endif
-
     /* extract flag infos */
     flags1 = 0;
     flags2 = 1;
@@ -366,7 +361,7 @@
         }
     }
 
-    if ((ret = ff_alloc_packet2(avctx, avpkt, 2 * MAX_CODED_SUPERFRAME_SIZE)))
+    if ((ret = ff_alloc_packet2(avctx, avpkt, 2 * MAX_CODED_SUPERFRAME_SIZE)) < 0)
         return ret;
 
     total_gain= 128;
diff --git a/libavcodec/wmalosslessdec.c b/libavcodec/wmalosslessdec.c
index 331a027..90a0109 100644
--- a/libavcodec/wmalosslessdec.c
+++ b/libavcodec/wmalosslessdec.c
@@ -189,7 +189,7 @@
             avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
         else if (s->bits_per_sample == 24) {
             avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
-            av_log_missing_feature(avctx, "Bit-depth higher than 16", 0);
+            avpriv_report_missing_feature(avctx, "Bit-depth higher than 16");
             return AVERROR_PATCHWELCOME;
         } else {
             av_log(avctx, AV_LOG_ERROR, "Unknown bit-depth: %d\n",
@@ -202,7 +202,7 @@
         av_dlog(avctx, "\n");
 
     } else {
-        av_log_ask_for_sample(avctx, "Unsupported extradata size\n");
+        avpriv_request_sample(avctx, "Unsupported extradata size");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -256,7 +256,8 @@
                s->num_channels);
         return AVERROR_INVALIDDATA;
     } else if (s->num_channels > WMALL_MAX_CHANNELS) {
-        av_log_ask_for_sample(avctx, "unsupported number of channels\n");
+        avpriv_request_sample(avctx,
+                              "More than %d channels", WMALL_MAX_CHANNELS);
         return AVERROR_PATCHWELCOME;
     }
 
@@ -885,7 +886,7 @@
 
         s->do_arith_coding    = get_bits1(&s->gb);
         if (s->do_arith_coding) {
-            av_log_missing_feature(s->avctx, "Arithmetic coding", 1);
+            avpriv_request_sample(s->avctx, "Arithmetic coding");
             return AVERROR_PATCHWELCOME;
         }
         s->do_ac_filter       = get_bits1(&s->gb);
@@ -925,8 +926,8 @@
             s->do_lpc = get_bits1(&s->gb);
             if (s->do_lpc) {
                 decode_lpc(s);
-                av_log_ask_for_sample(s->avctx, "Inverse LPC filter not "
-                                      "implemented. Expect wrong output.\n");
+                avpriv_request_sample(s->avctx, "Expect wrong output since "
+                                      "inverse LPC filter");
             }
         } else
             s->do_lpc = 0;
@@ -1015,10 +1016,8 @@
     int more_frames = 0, len = 0, i, ret;
 
     s->frame.nb_samples = s->samples_per_frame;
-    if ((ret = ff_get_buffer(s->avctx, &s->frame)) < 0) {
+    if ((ret = ff_get_buffer(s->avctx, &s->frame, 0)) < 0) {
         /* return an error if no frame could be decoded at all */
-        av_log(s->avctx, AV_LOG_ERROR,
-               "not enough space for the output samples\n");
         s->packet_loss = 1;
         return ret;
     }
@@ -1137,7 +1136,7 @@
     buflen = (s->num_saved_bits + len + 8) >> 3;
 
     if (len <= 0 || buflen > MAX_FRAMESIZE) {
-        av_log_ask_for_sample(s->avctx, "input buffer too small\n");
+        avpriv_request_sample(s->avctx, "Too small input buffer");
         s->packet_loss = 1;
         return;
     }
@@ -1190,7 +1189,7 @@
         skip_bits(gb, 1);   // Skip seekable_frame_in_packet, currently ununused
         spliced_packet = get_bits1(gb);
         if (spliced_packet)
-            av_log_missing_feature(avctx, "Bitstream splicing", 1);
+            avpriv_request_sample(avctx, "Bitstream splicing");
 
         /* get number of bits that need to be added to the previous frame */
         num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
@@ -1264,8 +1263,9 @@
         save_bits(s, gb, remaining_bits(s, gb), 0);
     }
 
-    *(AVFrame *)data = s->frame;
     *got_frame_ptr   = s->frame.nb_samples > 0;
+    av_frame_move_ref(data, &s->frame);
+
     s->packet_offset = get_bits_count(gb) & 7;
 
     return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
diff --git a/libavcodec/wmaprodec.c b/libavcodec/wmaprodec.c
index a772e73..07cc223 100644
--- a/libavcodec/wmaprodec.c
+++ b/libavcodec/wmaprodec.c
@@ -277,6 +277,11 @@
     int log2_max_num_subframes;
     int num_possible_block_sizes;
 
+    if (!avctx->block_align) {
+        av_log(avctx, AV_LOG_ERROR, "block_align is not set\n");
+        return AVERROR(EINVAL);
+    }
+
     s->avctx = avctx;
     avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
 
@@ -294,7 +299,7 @@
         av_dlog(avctx, "\n");
 
     } else {
-        av_log_ask_for_sample(avctx, "Unknown extradata size\n");
+        avpriv_request_sample(avctx, "Unknown extradata size");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -309,7 +314,7 @@
     /** get frame len */
     bits = ff_wma_get_frame_len_bits(avctx->sample_rate, 3, s->decode_flags);
     if (bits > WMAPRO_BLOCK_MAX_BITS) {
-        av_log_missing_feature(avctx, "14-bits block sizes", 1);
+        avpriv_request_sample(avctx, "14-bit block sizes");
         return AVERROR_PATCHWELCOME;
     }
     s->samples_per_frame = 1 << bits;
@@ -347,7 +352,8 @@
                avctx->channels);
         return AVERROR_INVALIDDATA;
     } else if (avctx->channels > WMAPRO_MAX_CHANNELS) {
-        av_log_ask_for_sample(avctx, "unsupported number of channels\n");
+        avpriv_request_sample(avctx,
+                              "More than %d channels", WMAPRO_MAX_CHANNELS);
         return AVERROR_PATCHWELCOME;
     }
 
@@ -681,8 +687,8 @@
         int remaining_channels = s->channels_for_cur_subframe;
 
         if (get_bits1(&s->gb)) {
-            av_log_ask_for_sample(s->avctx,
-                                  "unsupported channel transform bit\n");
+            avpriv_request_sample(s->avctx,
+                                  "Channel transform bit");
             return AVERROR_PATCHWELCOME;
         }
 
@@ -718,8 +724,8 @@
             if (chgroup->num_channels == 2) {
                 if (get_bits1(&s->gb)) {
                     if (get_bits1(&s->gb)) {
-                        av_log_ask_for_sample(s->avctx,
-                                              "unsupported channel transform type\n");
+                        avpriv_request_sample(s->avctx,
+                                              "Unknown channel transform type");
                     }
                 } else {
                     chgroup->transform = 1;
@@ -744,8 +750,8 @@
                     } else {
                         /** FIXME: more than 6 coupled channels not supported */
                         if (chgroup->num_channels > 6) {
-                            av_log_ask_for_sample(s->avctx,
-                                                  "coupled channels > 6\n");
+                            avpriv_request_sample(s->avctx,
+                                                  "Coupled channels > 6");
                         } else {
                             memcpy(chgroup->decorrelation_matrix,
                                    default_decorrelation[chgroup->num_channels],
@@ -1152,7 +1158,7 @@
 
     /** no idea for what the following bit is used */
     if (get_bits1(&s->gb)) {
-        av_log_ask_for_sample(s->avctx, "reserved bit set\n");
+        avpriv_request_sample(s->avctx, "Reserved bit");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -1373,8 +1379,7 @@
 
     /* get output buffer */
     frame->nb_samples = s->samples_per_frame;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
         s->packet_loss = 1;
         return 0;
     }
@@ -1394,6 +1399,7 @@
     if (s->skip_frame) {
         s->skip_frame = 0;
         *got_frame_ptr = 0;
+        av_frame_unref(frame);
     } else {
         *got_frame_ptr = 1;
     }
@@ -1458,7 +1464,7 @@
     buflen = (put_bits_count(&s->pb) + len + 8) >> 3;
 
     if (len <= 0 || buflen > MAX_FRAMESIZE) {
-        av_log_ask_for_sample(s->avctx, "input buffer too small\n");
+        avpriv_request_sample(s->avctx, "Too small input buffer");
         s->packet_loss = 1;
         return;
     }
@@ -1508,8 +1514,11 @@
         s->packet_done = 0;
 
         /** sanity check for the buffer length */
-        if (buf_size < avctx->block_align)
-            return 0;
+        if (buf_size < avctx->block_align) {
+            av_log(avctx, AV_LOG_ERROR, "Input packet too small (%d < %d)\n",
+                   buf_size, avctx->block_align);
+            return AVERROR_INVALIDDATA;
+        }
 
         s->next_packet_start = buf_size - avctx->block_align;
         buf_size = avctx->block_align;
diff --git a/libavcodec/wmavoice.c b/libavcodec/wmavoice.c
index a82ee94..19f8965 100644
--- a/libavcodec/wmavoice.c
+++ b/libavcodec/wmavoice.c
@@ -1763,7 +1763,7 @@
      * are really WMAPro-in-WMAVoice-superframes. I've never seen those in
      * the wild yet. */
     if (!get_bits1(gb)) {
-        av_log_missing_feature(ctx, "WMAPro-in-WMAVoice", 1);
+        avpriv_request_sample(ctx, "WMAPro-in-WMAVoice");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -1799,10 +1799,8 @@
 
     /* get output buffer */
     frame->nb_samples = 480;
-    if ((res = ff_get_buffer(ctx, frame)) < 0) {
-        av_log(ctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((res = ff_get_buffer(ctx, frame, 0)) < 0)
         return res;
-    }
     frame->nb_samples = n_samples;
     samples = (float *)frame->data[0];
 
diff --git a/libavcodec/wmv2.c b/libavcodec/wmv2.c
index 6676652..918d438 100644
--- a/libavcodec/wmv2.c
+++ b/libavcodec/wmv2.c
@@ -116,7 +116,6 @@
     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){
             s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr - 1 - s->linesize, s->linesize, 19, 19,
@@ -124,7 +123,6 @@
             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);
diff --git a/libavcodec/wmv2dec.c b/libavcodec/wmv2dec.c
index fc7a1b3..099cc36 100644
--- a/libavcodec/wmv2dec.c
+++ b/libavcodec/wmv2dec.c
@@ -31,7 +31,7 @@
 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->f.mb_type;
+    uint32_t * const mb_type = s->current_picture_ptr->mb_type;
 
     w->skip_type= get_bits(&s->gb, 2);
     switch(w->skip_type){
@@ -254,11 +254,11 @@
     wrap = s->b8_stride;
     xy = s->block_index[0];
 
-    mot_val = s->current_picture.f.motion_val[0][xy];
+    mot_val = s->current_picture.motion_val[0][xy];
 
-    A = s->current_picture.f.motion_val[0][xy - 1];
-    B = s->current_picture.f.motion_val[0][xy - wrap];
-    C = s->current_picture.f.motion_val[0][xy + 2 - wrap];
+    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]));
@@ -339,7 +339,7 @@
     if(w->j_type) return 0;
 
     if (s->pict_type == AV_PICTURE_TYPE_P) {
-        if (IS_SKIP(s->current_picture.f.mb_type[s->mb_y * s->mb_stride + s->mb_x])) {
+        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++)
@@ -446,6 +446,8 @@
 static av_cold int wmv2_decode_init(AVCodecContext *avctx){
     Wmv2Context * const w= avctx->priv_data;
 
+    avctx->flags |= CODEC_FLAG_EMU_EDGE;
+
     if(ff_msmpeg4_decode_init(avctx) < 0)
         return -1;
 
diff --git a/libavcodec/wnv1.c b/libavcodec/wnv1.c
index c59ceb7..162dd1f 100644
--- a/libavcodec/wnv1.c
+++ b/libavcodec/wnv1.c
@@ -32,7 +32,6 @@
 
 typedef struct WNV1Context {
     AVCodecContext *avctx;
-    AVFrame pic;
 
     int shift;
     GetBitContext gb;
@@ -65,7 +64,7 @@
     WNV1Context * const l = avctx->priv_data;
     const uint8_t *buf    = avpkt->data;
     int buf_size          = avpkt->size;
-    AVFrame * const p     = &l->pic;
+    AVFrame * const p     = data;
     unsigned char *Y,*U,*V;
     int i, j, ret;
     int prev_y = 0, prev_u = 0, prev_v = 0;
@@ -82,12 +81,7 @@
         return AVERROR(ENOMEM);
     }
 
-    if (p->data[0])
-        avctx->release_buffer(avctx, p);
-
-    p->reference = 0;
-    if ((ret = ff_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
         av_free(rbuf);
         return ret;
     }
@@ -102,12 +96,14 @@
     else {
         l->shift = 8 - (buf[2] >> 4);
         if (l->shift > 4) {
-            av_log_ask_for_sample(avctx, "Unknown WNV1 frame header value %i\n",
+            avpriv_request_sample(avctx,
+                                  "Unknown WNV1 frame header value %i",
                                   buf[2] >> 4);
             l->shift = 4;
         }
         if (l->shift < 1) {
-            av_log_ask_for_sample(avctx, "Unknown WNV1 frame header value %i\n",
+            avpriv_request_sample(avctx,
+                                  "Unknown WNV1 frame header value %i",
                                   buf[2] >> 4);
             l->shift = 1;
         }
@@ -130,7 +126,6 @@
 
 
     *got_frame      = 1;
-    *(AVFrame*)data = l->pic;
     av_free(rbuf);
 
     return buf_size;
@@ -143,7 +138,6 @@
 
     l->avctx       = avctx;
     avctx->pix_fmt = AV_PIX_FMT_YUV422P;
-    avcodec_get_frame_defaults(&l->pic);
 
     code_vlc.table           = code_table;
     code_vlc.table_allocated = 1 << CODE_VLC_BITS;
@@ -154,24 +148,12 @@
     return 0;
 }
 
-static av_cold int decode_end(AVCodecContext *avctx)
-{
-    WNV1Context * const l = avctx->priv_data;
-    AVFrame *pic = &l->pic;
-
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
-
-    return 0;
-}
-
 AVCodec ff_wnv1_decoder = {
     .name           = "wnv1",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_WNV1,
     .priv_data_size = sizeof(WNV1Context),
     .init           = decode_init,
-    .close          = decode_end,
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("Winnov WNV1"),
diff --git a/libavcodec/ws-snd1.c b/libavcodec/ws-snd1.c
index 24ebceb..d27df75 100644
--- a/libavcodec/ws-snd1.c
+++ b/libavcodec/ws-snd1.c
@@ -81,10 +81,8 @@
 
     /* get output buffer */
     frame->nb_samples = out_size;
-    if ((ret = ff_get_buffer(avctx, frame)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    }
     samples     = frame->data[0];
     samples_end = samples + out_size;
 
diff --git a/libavcodec/x86/Makefile b/libavcodec/x86/Makefile
index ff7ea77..50e64f7 100644
--- a/libavcodec/x86/Makefile
+++ b/libavcodec/x86/Makefile
@@ -1,15 +1,16 @@
-OBJS                                   += x86/fmtconvert_init.o
+OBJS                                   += x86/constants.o               \
+                                          x86/fmtconvert_init.o         \
 
 OBJS-$(CONFIG_AAC_DECODER)             += x86/sbrdsp_init.o
 OBJS-$(CONFIG_AC3DSP)                  += x86/ac3dsp_init.o
 OBJS-$(CONFIG_CAVS_DECODER)            += x86/cavsdsp.o
 OBJS-$(CONFIG_DNXHD_ENCODER)           += x86/dnxhdenc.o
 OBJS-$(CONFIG_FFT)                     += x86/fft_init.o
-OBJS-$(CONFIG_GPL)                     += x86/idct_mmx.o
 OBJS-$(CONFIG_H264CHROMA)              += x86/h264chroma_init.o
 OBJS-$(CONFIG_H264DSP)                 += x86/h264dsp_init.o
 OBJS-$(CONFIG_H264PRED)                += x86/h264_intrapred_init.o
 OBJS-$(CONFIG_H264QPEL)                += x86/h264_qpel.o
+OBJS-$(CONFIG_HPELDSP)                 += x86/hpeldsp_init.o
 OBJS-$(CONFIG_LPC)                     += x86/lpc.o
 OBJS-$(CONFIG_MLP_DECODER)             += x86/mlpdsp.o
 OBJS-$(CONFIG_MPEGAUDIODSP)            += x86/mpegaudiodec.o
@@ -30,9 +31,10 @@
 OBJS-$(CONFIG_VP5_DECODER)             += x86/vp56dsp_init.o
 OBJS-$(CONFIG_VP6_DECODER)             += x86/vp56dsp_init.o
 OBJS-$(CONFIG_VP8_DECODER)             += x86/vp8dsp_init.o
+OBJS-$(CONFIG_WEBP_DECODER)            += x86/vp8dsp_init.o
 OBJS-$(CONFIG_XMM_CLOBBER_TEST)        += x86/w64xmmtest.o
 
-MMX-OBJS                               += x86/dsputil_mmx.o             \
+MMX-OBJS-$(CONFIG_DSPUTIL)             += x86/dsputil_mmx.o             \
                                           x86/fdct.o                    \
                                           x86/idct_mmx_xvid.o           \
                                           x86/idct_sse2_xvid.o          \
@@ -50,6 +52,10 @@
 YASM-OBJS-$(CONFIG_DCT)                += x86/dct32.o
 YASM-OBJS-$(CONFIG_DIRAC_DECODER)      += x86/diracdsp_mmx.o x86/diracdsp_yasm.o\
                                           x86/dwt_yasm.o
+YASM-OBJS-$(CONFIG_DSPUTIL)            += x86/dsputil.o                 \
+                                          x86/fpel.o                    \
+                                          x86/mpeg4qpel.o               \
+                                          x86/qpel.o
 YASM-OBJS-$(CONFIG_ENCODERS)           += x86/dsputilenc.o
 YASM-OBJS-$(CONFIG_FFT)                += x86/fft.o
 YASM-OBJS-$(CONFIG_H263_DECODER)       += x86/h263_loopfilter.o
@@ -65,7 +71,11 @@
 YASM-OBJS-$(CONFIG_H264PRED)           += x86/h264_intrapred.o          \
                                           x86/h264_intrapred_10bit.o
 YASM-OBJS-$(CONFIG_H264QPEL)           += x86/h264_qpel_8bit.o          \
-                                          x86/h264_qpel_10bit.o
+                                          x86/h264_qpel_10bit.o         \
+                                          x86/fpel.o                    \
+                                          x86/qpel.o
+YASM-OBJS-$(CONFIG_HPELDSP)            += x86/fpel.o                    \
+                                          x86/hpeldsp.o
 YASM-OBJS-$(CONFIG_MPEGAUDIODSP)       += x86/imdct36.o
 YASM-OBJS-$(CONFIG_PNG_DECODER)        += x86/pngdsp.o
 YASM-OBJS-$(CONFIG_PRORES_DECODER)     += x86/proresdsp.o
@@ -80,9 +90,7 @@
 YASM-OBJS-$(CONFIG_VP3DSP)             += x86/vp3dsp.o
 YASM-OBJS-$(CONFIG_VP6_DECODER)        += x86/vp56dsp.o
 YASM-OBJS-$(CONFIG_VP8_DECODER)        += x86/vp8dsp.o
+YASM-OBJS-$(CONFIG_WEBP_DECODER)       += x86/vp8dsp.o
 
-YASM-OBJS                              += x86/dsputil.o                 \
-                                          x86/deinterlace.o             \
+YASM-OBJS                              += x86/deinterlace.o             \
                                           x86/fmtconvert.o              \
-                                          x86/hpeldsp.o                 \
-                                          x86/mpeg4qpel.o               \
diff --git a/libavcodec/x86/ac3dsp_init.c b/libavcodec/x86/ac3dsp_init.c
index e2a190e..d7c5902 100644
--- a/libavcodec/x86/ac3dsp_init.c
+++ b/libavcodec/x86/ac3dsp_init.c
@@ -26,30 +26,30 @@
 #include "libavcodec/ac3.h"
 #include "libavcodec/ac3dsp.h"
 
-extern void ff_ac3_exponent_min_mmx   (uint8_t *exp, int num_reuse_blocks, int nb_coefs);
-extern void ff_ac3_exponent_min_mmxext(uint8_t *exp, int num_reuse_blocks, int nb_coefs);
-extern void ff_ac3_exponent_min_sse2  (uint8_t *exp, int num_reuse_blocks, int nb_coefs);
+void ff_ac3_exponent_min_mmx   (uint8_t *exp, int num_reuse_blocks, int nb_coefs);
+void ff_ac3_exponent_min_mmxext(uint8_t *exp, int num_reuse_blocks, int nb_coefs);
+void ff_ac3_exponent_min_sse2  (uint8_t *exp, int num_reuse_blocks, int nb_coefs);
 
-extern int ff_ac3_max_msb_abs_int16_mmx  (const int16_t *src, int len);
-extern int ff_ac3_max_msb_abs_int16_mmxext(const int16_t *src, int len);
-extern int ff_ac3_max_msb_abs_int16_sse2 (const int16_t *src, int len);
-extern int ff_ac3_max_msb_abs_int16_ssse3(const int16_t *src, int len);
+int ff_ac3_max_msb_abs_int16_mmx  (const int16_t *src, int len);
+int ff_ac3_max_msb_abs_int16_mmxext(const int16_t *src, int len);
+int ff_ac3_max_msb_abs_int16_sse2 (const int16_t *src, int len);
+int ff_ac3_max_msb_abs_int16_ssse3(const int16_t *src, int len);
 
-extern void ff_ac3_lshift_int16_mmx (int16_t *src, unsigned int len, unsigned int shift);
-extern void ff_ac3_lshift_int16_sse2(int16_t *src, unsigned int len, unsigned int shift);
+void ff_ac3_lshift_int16_mmx (int16_t *src, unsigned int len, unsigned int shift);
+void ff_ac3_lshift_int16_sse2(int16_t *src, unsigned int len, unsigned int shift);
 
-extern void ff_ac3_rshift_int32_mmx (int32_t *src, unsigned int len, unsigned int shift);
-extern void ff_ac3_rshift_int32_sse2(int32_t *src, unsigned int len, unsigned int shift);
+void ff_ac3_rshift_int32_mmx (int32_t *src, unsigned int len, unsigned int shift);
+void ff_ac3_rshift_int32_sse2(int32_t *src, unsigned int len, unsigned int shift);
 
-extern void ff_float_to_fixed24_3dnow(int32_t *dst, const float *src, unsigned int len);
-extern void ff_float_to_fixed24_sse  (int32_t *dst, const float *src, unsigned int len);
-extern void ff_float_to_fixed24_sse2 (int32_t *dst, const float *src, unsigned int len);
+void ff_float_to_fixed24_3dnow(int32_t *dst, const float *src, unsigned int len);
+void ff_float_to_fixed24_sse  (int32_t *dst, const float *src, unsigned int len);
+void ff_float_to_fixed24_sse2 (int32_t *dst, const float *src, unsigned int len);
 
-extern int ff_ac3_compute_mantissa_size_sse2(uint16_t mant_cnt[6][16]);
+int ff_ac3_compute_mantissa_size_sse2(uint16_t mant_cnt[6][16]);
 
-extern void ff_ac3_extract_exponents_3dnow(uint8_t *exp, int32_t *coef, int nb_coefs);
-extern void ff_ac3_extract_exponents_sse2 (uint8_t *exp, int32_t *coef, int nb_coefs);
-extern void ff_ac3_extract_exponents_ssse3(uint8_t *exp, int32_t *coef, int nb_coefs);
+void ff_ac3_extract_exponents_3dnow(uint8_t *exp, int32_t *coef, int nb_coefs);
+void ff_ac3_extract_exponents_sse2 (uint8_t *exp, int32_t *coef, int nb_coefs);
+void ff_ac3_extract_exponents_ssse3(uint8_t *exp, int32_t *coef, int nb_coefs);
 
 #if ARCH_X86_32 && defined(__INTEL_COMPILER)
 #       undef HAVE_7REGS
diff --git a/libavcodec/x86/cavsdsp.c b/libavcodec/x86/cavsdsp.c
index dd75584..0401e27 100644
--- a/libavcodec/x86/cavsdsp.c
+++ b/libavcodec/x86/cavsdsp.c
@@ -31,7 +31,7 @@
 #include "dsputil_mmx.h"
 #include "config.h"
 
-#if (HAVE_MMXEXT_INLINE || HAVE_AMD3DNOW_INLINE)
+#if HAVE_MMX_INLINE
 
 /* in/out: mma=mma+mmb, mmb=mmb-mma */
 #define SUMSUB_BA( a, b ) \
@@ -187,6 +187,10 @@
     ff_add_pixels_clamped_mmx(b2, dst, stride);
 }
 
+#endif /* HAVE_MMX_INLINE */
+
+#if (HAVE_MMXEXT_INLINE || HAVE_AMD3DNOW_INLINE)
+
 /*****************************************************************************
  *
  * motion compensation
@@ -409,19 +413,23 @@
 }\
 
 #define CAVS_MC(OPNAME, SIZE, MMX) \
-static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc20_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
+static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc20_ ## MMX(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## cavs_qpel ## SIZE ## _h_ ## MMX(dst, src, stride, stride);\
 }\
 \
-static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc01_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
+static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc01_ ## MMX(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## cavs_qpel ## SIZE ## _v1_ ## MMX(dst, src, stride, stride);\
 }\
 \
-static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc02_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
+static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc02_ ## MMX(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## cavs_qpel ## SIZE ## _v2_ ## MMX(dst, src, stride, stride);\
 }\
 \
-static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc03_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
+static void ff_ ## OPNAME ## cavs_qpel ## SIZE ## _mc03_ ## MMX(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     OPNAME ## cavs_qpel ## SIZE ## _v3_ ## MMX(dst, src, stride, stride);\
 }\
 
@@ -437,6 +445,26 @@
 
 #endif /* (HAVE_MMXEXT_INLINE || HAVE_AMD3DNOW_INLINE) */
 
+#if HAVE_MMX_INLINE
+static av_cold void cavsdsp_init_mmx(CAVSDSPContext *c,
+                                     AVCodecContext *avctx)
+{
+    c->put_cavs_qpel_pixels_tab[0][0] = ff_put_cavs_qpel16_mc00_mmx;
+    c->put_cavs_qpel_pixels_tab[1][0] = ff_put_cavs_qpel8_mc00_mmx;
+    c->avg_cavs_qpel_pixels_tab[0][0] = ff_avg_cavs_qpel16_mc00_mmx;
+    c->avg_cavs_qpel_pixels_tab[1][0] = ff_avg_cavs_qpel8_mc00_mmx;
+
+    c->cavs_idct8_add = cavs_idct8_add_mmx;
+    c->idct_perm      = FF_TRANSPOSE_IDCT_PERM;
+}
+#endif /* HAVE_MMX_INLINE */
+
+#define DSPFUNC(PFX, IDX, NUM, EXT)                                                              \
+    c->PFX ## _cavs_qpel_pixels_tab[IDX][ 2] = ff_ ## PFX ## _cavs_qpel ## NUM ## _mc20_ ## EXT; \
+    c->PFX ## _cavs_qpel_pixels_tab[IDX][ 4] = ff_ ## PFX ## _cavs_qpel ## NUM ## _mc01_ ## EXT; \
+    c->PFX ## _cavs_qpel_pixels_tab[IDX][ 8] = ff_ ## PFX ## _cavs_qpel ## NUM ## _mc02_ ## EXT; \
+    c->PFX ## _cavs_qpel_pixels_tab[IDX][12] = ff_ ## PFX ## _cavs_qpel ## NUM ## _mc03_ ## EXT; \
+
 #if HAVE_MMXEXT_INLINE
 QPEL_CAVS(put_,        PUT_OP, mmxext)
 QPEL_CAVS(avg_, AVG_MMXEXT_OP, mmxext)
@@ -449,20 +477,10 @@
 static av_cold void ff_cavsdsp_init_mmxext(CAVSDSPContext *c,
                                            AVCodecContext *avctx)
 {
-#define dspfunc(PFX, IDX, NUM) \
-    c->PFX ## _pixels_tab[IDX][ 0] = ff_ ## PFX ## NUM ## _mc00_mmxext; \
-    c->PFX ## _pixels_tab[IDX][ 2] = ff_ ## PFX ## NUM ## _mc20_mmxext; \
-    c->PFX ## _pixels_tab[IDX][ 4] = ff_ ## PFX ## NUM ## _mc01_mmxext; \
-    c->PFX ## _pixels_tab[IDX][ 8] = ff_ ## PFX ## NUM ## _mc02_mmxext; \
-    c->PFX ## _pixels_tab[IDX][12] = ff_ ## PFX ## NUM ## _mc03_mmxext; \
-
-    dspfunc(put_cavs_qpel, 0, 16);
-    dspfunc(put_cavs_qpel, 1, 8);
-    dspfunc(avg_cavs_qpel, 0, 16);
-    dspfunc(avg_cavs_qpel, 1, 8);
-#undef dspfunc
-    c->cavs_idct8_add = cavs_idct8_add_mmx;
-    c->idct_perm = FF_TRANSPOSE_IDCT_PERM;
+    DSPFUNC(put, 0, 16, mmxext);
+    DSPFUNC(put, 1,  8, mmxext);
+    DSPFUNC(avg, 0, 16, mmxext);
+    DSPFUNC(avg, 1,  8, mmxext);
 }
 #endif /* HAVE_MMXEXT_INLINE */
 
@@ -478,27 +496,21 @@
 static av_cold void ff_cavsdsp_init_3dnow(CAVSDSPContext *c,
                                           AVCodecContext *avctx)
 {
-#define dspfunc(PFX, IDX, NUM) \
-    c->PFX ## _pixels_tab[IDX][ 0] = ff_ ## PFX ## NUM ## _mc00_mmxext; \
-    c->PFX ## _pixels_tab[IDX][ 2] = ff_ ## PFX ## NUM ## _mc20_3dnow; \
-    c->PFX ## _pixels_tab[IDX][ 4] = ff_ ## PFX ## NUM ## _mc01_3dnow; \
-    c->PFX ## _pixels_tab[IDX][ 8] = ff_ ## PFX ## NUM ## _mc02_3dnow; \
-    c->PFX ## _pixels_tab[IDX][12] = ff_ ## PFX ## NUM ## _mc03_3dnow; \
-
-    dspfunc(put_cavs_qpel, 0, 16);
-    dspfunc(put_cavs_qpel, 1, 8);
-    dspfunc(avg_cavs_qpel, 0, 16);
-    dspfunc(avg_cavs_qpel, 1, 8);
-#undef dspfunc
-    c->cavs_idct8_add = cavs_idct8_add_mmx;
-    c->idct_perm = FF_TRANSPOSE_IDCT_PERM;
+    DSPFUNC(put, 0, 16, 3dnow);
+    DSPFUNC(put, 1,  8, 3dnow);
+    DSPFUNC(avg, 0, 16, 3dnow);
+    DSPFUNC(avg, 1,  8, 3dnow);
 }
 #endif /* HAVE_AMD3DNOW_INLINE */
 
 av_cold void ff_cavsdsp_init_x86(CAVSDSPContext *c, AVCodecContext *avctx)
 {
+#if HAVE_MMX_INLINE
     int mm_flags = av_get_cpu_flags();
 
+    if (mm_flags & AV_CPU_FLAG_MMX)
+        cavsdsp_init_mmx(c, avctx);
+#endif /* HAVE_MMX_INLINE */
 #if HAVE_MMXEXT_INLINE
     if (mm_flags & AV_CPU_FLAG_MMXEXT) ff_cavsdsp_init_mmxext(c, avctx);
 #endif /* HAVE_MMXEXT_INLINE */
diff --git a/libavcodec/x86/constants.c b/libavcodec/x86/constants.c
new file mode 100644
index 0000000..7c2ddc6
--- /dev/null
+++ b/libavcodec/x86/constants.c
@@ -0,0 +1,42 @@
+/*
+ * MMX/SSE constants used across x86 dsp optimizations.
+ *
+ * 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 "libavutil/mem.h"
+#include "libavutil/x86/asm.h" // for xmm_reg
+
+DECLARE_ALIGNED(8,  const uint64_t, ff_bone) = 0x0101010101010101ULL;
+DECLARE_ALIGNED(8,  const uint64_t, ff_wtwo) = 0x0002000200020002ULL;
+
+DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_1)    = { 0x0001000100010001ULL, 0x0001000100010001ULL };
+DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_2)    = { 0x0002000200020002ULL, 0x0002000200020002ULL };
+DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_3)    = { 0x0003000300030003ULL, 0x0003000300030003ULL };
+DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_4)    = { 0x0004000400040004ULL, 0x0004000400040004ULL };
+DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_5)    = { 0x0005000500050005ULL, 0x0005000500050005ULL };
+DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_8)    = { 0x0008000800080008ULL, 0x0008000800080008ULL };
+DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_9)    = { 0x0009000900090009ULL, 0x0009000900090009ULL };
+DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_16)   = { 0x0010001000100010ULL, 0x0010001000100010ULL };
+DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_18)   = { 0x0012001200120012ULL, 0x0012001200120012ULL };
+DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_32)   = { 0x0020002000200020ULL, 0x0020002000200020ULL };
+DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_64)   = { 0x0040004000400040ULL, 0x0040004000400040ULL };
+
+DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_0)    = { 0x0000000000000000ULL, 0x0000000000000000ULL };
+DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_1)    = { 0x0101010101010101ULL, 0x0101010101010101ULL };
+DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_3)    = { 0x0303030303030303ULL, 0x0303030303030303ULL };
+DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_80)   = { 0x8080808080808080ULL, 0x8080808080808080ULL };
diff --git a/libavcodec/x86/dsputil.asm b/libavcodec/x86/dsputil.asm
index aa18bdd..77069e2 100644
--- a/libavcodec/x86/dsputil.asm
+++ b/libavcodec/x86/dsputil.asm
@@ -554,8 +554,8 @@
 %if cpuflag(ssse3)
     pshufb   m0, m2
     pshufb   m1, m2
-    mova     [r0 +  0], m0
-    mova     [r0 + 16], m1
+    mov%1    [r0 +  0], m0
+    mov%1    [r0 + 16], m1
 %else
     pshuflw  m0, m0, 10110001b
     pshuflw  m1, m1, 10110001b
@@ -569,8 +569,8 @@
     psrlw    m3, 8
     por      m2, m0
     por      m3, m1
-    mova     [r0 +  0], m2
-    mova     [r0 + 16], m3
+    mov%1    [r0 +  0], m2
+    mov%1    [r0 + 16], m3
 %endif
     add      r0, 32
     add      r1, 32
@@ -583,7 +583,7 @@
     mov%1    m0, [r1]
 %if cpuflag(ssse3)
     pshufb   m0, m2
-    mova     [r0], m0
+    mov%1    [r0], m0
 %else
     pshuflw  m0, m0, 10110001b
     pshufhw  m0, m0, 10110001b
@@ -591,7 +591,7 @@
     psllw    m0, 8
     psrlw    m2, 8
     por      m2, m0
-    mova     [r0], m2
+    mov%1    [r0], m2
 %endif
     add      r1, 16
     add      r0, 16
@@ -607,6 +607,7 @@
 cglobal bswap32_buf, 3,4,5
     mov      r3, r1
 %endif
+    or       r3, r0
     and      r3, 15
     jz       .start_align
     BSWAP_LOOPS  u
@@ -650,195 +651,3 @@
 
 INIT_XMM ssse3
 BSWAP32_BUF
-
-
-; FIXME: All of the code below should be put back in h264_qpel_8bit.asm.
-; Unfortunately it is unconditionally used from dsputil_mmx.c since 71155d7 ..
-
-%macro op_avgh 3
-    movh   %3, %2
-    pavgb  %1, %3
-    movh   %2, %1
-%endmacro
-
-%macro op_avg 2
-    pavgb  %1, %2
-    mova   %2, %1
-%endmacro
-
-%macro op_puth 2-3
-    movh   %2, %1
-%endmacro
-
-%macro op_put 2
-    mova   %2, %1
-%endmacro
-
-; void pixels4_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
-%macro PIXELS4_L2 1
-%define OP op_%1h
-cglobal %1_pixels4_l2, 6,6
-    movsxdifnidn r3, r3d
-    movsxdifnidn r4, r4d
-    test        r5d, 1
-    je        .loop
-    movd         m0, [r1]
-    movd         m1, [r2]
-    add          r1, r4
-    add          r2, 4
-    pavgb        m0, m1
-    OP           m0, [r0], m3
-    add          r0, r3
-    dec         r5d
-.loop:
-    mova         m0, [r1]
-    mova         m1, [r1+r4]
-    lea          r1, [r1+2*r4]
-    pavgb        m0, [r2]
-    pavgb        m1, [r2+4]
-    OP           m0, [r0], m3
-    OP           m1, [r0+r3], m3
-    lea          r0, [r0+2*r3]
-    mova         m0, [r1]
-    mova         m1, [r1+r4]
-    lea          r1, [r1+2*r4]
-    pavgb        m0, [r2+8]
-    pavgb        m1, [r2+12]
-    OP           m0, [r0], m3
-    OP           m1, [r0+r3], m3
-    lea          r0, [r0+2*r3]
-    add          r2, 16
-    sub         r5d, 4
-    jne       .loop
-    REP_RET
-%endmacro
-
-INIT_MMX mmxext
-PIXELS4_L2 put
-PIXELS4_L2 avg
-
-; void pixels8_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
-%macro PIXELS8_L2 1
-%define OP op_%1
-cglobal %1_pixels8_l2, 6,6
-    movsxdifnidn r3, r3d
-    movsxdifnidn r4, r4d
-    test        r5d, 1
-    je        .loop
-    mova         m0, [r1]
-    mova         m1, [r2]
-    add          r1, r4
-    add          r2, 8
-    pavgb        m0, m1
-    OP           m0, [r0]
-    add          r0, r3
-    dec         r5d
-.loop:
-    mova         m0, [r1]
-    mova         m1, [r1+r4]
-    lea          r1, [r1+2*r4]
-    pavgb        m0, [r2]
-    pavgb        m1, [r2+8]
-    OP           m0, [r0]
-    OP           m1, [r0+r3]
-    lea          r0, [r0+2*r3]
-    mova         m0, [r1]
-    mova         m1, [r1+r4]
-    lea          r1, [r1+2*r4]
-    pavgb        m0, [r2+16]
-    pavgb        m1, [r2+24]
-    OP           m0, [r0]
-    OP           m1, [r0+r3]
-    lea          r0, [r0+2*r3]
-    add          r2, 32
-    sub         r5d, 4
-    jne       .loop
-    REP_RET
-%endmacro
-
-INIT_MMX mmxext
-PIXELS8_L2 put
-PIXELS8_L2 avg
-
-; void pixels16_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
-%macro PIXELS16_L2 1
-%define OP op_%1
-cglobal %1_pixels16_l2, 6,6
-    movsxdifnidn r3, r3d
-    movsxdifnidn r4, r4d
-    test        r5d, 1
-    je        .loop
-    mova         m0, [r1]
-    mova         m1, [r1+8]
-    pavgb        m0, [r2]
-    pavgb        m1, [r2+8]
-    add          r1, r4
-    add          r2, 16
-    OP           m0, [r0]
-    OP           m1, [r0+8]
-    add          r0, r3
-    dec         r5d
-.loop:
-    mova         m0, [r1]
-    mova         m1, [r1+8]
-    add          r1, r4
-    pavgb        m0, [r2]
-    pavgb        m1, [r2+8]
-    OP           m0, [r0]
-    OP           m1, [r0+8]
-    add          r0, r3
-    mova         m0, [r1]
-    mova         m1, [r1+8]
-    add          r1, r4
-    pavgb        m0, [r2+16]
-    pavgb        m1, [r2+24]
-    OP           m0, [r0]
-    OP           m1, [r0+8]
-    add          r0, r3
-    add          r2, 32
-    sub         r5d, 2
-    jne       .loop
-    REP_RET
-%endmacro
-
-INIT_MMX mmxext
-PIXELS16_L2 put
-PIXELS16_L2 avg
-
-INIT_MMX mmxext
-; void pixels(uint8_t *block, const uint8_t *pixels, int line_size, int h)
-%macro PIXELS48 2
-%if %2 == 4
-%define OP movh
-%else
-%define OP mova
-%endif
-cglobal %1_pixels%2, 4,5
-    movsxdifnidn r2, r2d
-    lea          r4, [r2*3]
-.loop:
-    OP           m0, [r1]
-    OP           m1, [r1+r2]
-    OP           m2, [r1+r2*2]
-    OP           m3, [r1+r4]
-    lea          r1, [r1+r2*4]
-%ifidn %1, avg
-    pavgb        m0, [r0]
-    pavgb        m1, [r0+r2]
-    pavgb        m2, [r0+r2*2]
-    pavgb        m3, [r0+r4]
-%endif
-    OP         [r0], m0
-    OP      [r0+r2], m1
-    OP    [r0+r2*2], m2
-    OP      [r0+r4], m3
-    sub         r3d, 4
-    lea          r0, [r0+r2*4]
-    jne       .loop
-    RET
-%endmacro
-
-PIXELS48 put, 4
-PIXELS48 avg, 4
-PIXELS48 put, 8
-PIXELS48 avg, 8
diff --git a/libavcodec/x86/dsputil_mmx.c b/libavcodec/x86/dsputil_mmx.c
index 15e46c7..a43a4ff 100644
--- a/libavcodec/x86/dsputil_mmx.c
+++ b/libavcodec/x86/dsputil_mmx.c
@@ -38,57 +38,25 @@
 //#include <assert.h>
 
 /* pixel operations */
-DECLARE_ALIGNED(8,  const uint64_t, ff_bone) = 0x0101010101010101ULL;
-DECLARE_ALIGNED(8,  const uint64_t, ff_wtwo) = 0x0002000200020002ULL;
-
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_1)    = { 0x0001000100010001ULL, 0x0001000100010001ULL };
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_2)    = { 0x0002000200020002ULL, 0x0002000200020002ULL };
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_3)    = { 0x0003000300030003ULL, 0x0003000300030003ULL };
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_4)    = { 0x0004000400040004ULL, 0x0004000400040004ULL };
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_5)    = { 0x0005000500050005ULL, 0x0005000500050005ULL };
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_8)    = { 0x0008000800080008ULL, 0x0008000800080008ULL };
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_9)    = { 0x0009000900090009ULL, 0x0009000900090009ULL };
 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_15)   =   0x000F000F000F000FULL;
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_16)   = { 0x0010001000100010ULL, 0x0010001000100010ULL };
 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_17)   = { 0x0011001100110011ULL, 0x0011001100110011ULL };
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_18)   = { 0x0012001200120012ULL, 0x0012001200120012ULL };
 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_20)   =   0x0014001400140014ULL;
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_27)   = { 0x001B001B001B001BULL, 0x001B001B001B001BULL };
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_28)   = { 0x001C001C001C001CULL, 0x001C001C001C001CULL };
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_32)   = { 0x0020002000200020ULL, 0x0020002000200020ULL };
 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_42)   =   0x002A002A002A002AULL;
 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_53)   =   0x0035003500350035ULL;
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_63)   = { 0x003F003F003F003FULL, 0x003F003F003F003FULL };
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_64)   = { 0x0040004000400040ULL, 0x0040004000400040ULL };
 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_96)   =   0x0060006000600060ULL;
 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_128)  =   0x0080008000800080ULL;
 DECLARE_ALIGNED(8,  const uint64_t, ff_pw_255)  =   0x00ff00ff00ff00ffULL;
 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_512)  = { 0x0200020002000200ULL, 0x0200020002000200ULL };
 DECLARE_ALIGNED(16, const xmm_reg,  ff_pw_1019) = { 0x03FB03FB03FB03FBULL, 0x03FB03FB03FB03FBULL };
 
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_0)    = { 0x0000000000000000ULL, 0x0000000000000000ULL };
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_1)    = { 0x0101010101010101ULL, 0x0101010101010101ULL };
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_3)    = { 0x0303030303030303ULL, 0x0303030303030303ULL };
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_4)    = { 0x0404040404040404ULL, 0x0404040404040404ULL };
-DECLARE_ALIGNED(8,  const uint64_t, ff_pb_7)    =   0x0707070707070707ULL;
-DECLARE_ALIGNED(8,  const uint64_t, ff_pb_1F)   =   0x1F1F1F1F1F1F1F1FULL;
 DECLARE_ALIGNED(8,  const uint64_t, ff_pb_3F)   =   0x3F3F3F3F3F3F3F3FULL;
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_80)   = { 0x8080808080808080ULL, 0x8080808080808080ULL };
-DECLARE_ALIGNED(8,  const uint64_t, ff_pb_81)   =   0x8181818181818181ULL;
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_A1)   = { 0xA1A1A1A1A1A1A1A1ULL, 0xA1A1A1A1A1A1A1A1ULL };
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_F8)   = { 0xF8F8F8F8F8F8F8F8ULL, 0xF8F8F8F8F8F8F8F8ULL };
 DECLARE_ALIGNED(8,  const uint64_t, ff_pb_FC)   =   0xFCFCFCFCFCFCFCFCULL;
-DECLARE_ALIGNED(16, const xmm_reg,  ff_pb_FE)   = { 0xFEFEFEFEFEFEFEFEULL, 0xFEFEFEFEFEFEFEFEULL };
 
 DECLARE_ALIGNED(16, const double, ff_pd_1)[2] = { 1.0, 1.0 };
 DECLARE_ALIGNED(16, const double, ff_pd_2)[2] = { 2.0, 2.0 };
 
 
 #if HAVE_YASM
-void ff_put_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
-                              ptrdiff_t line_size, int h);
-void ff_put_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels,
-                             ptrdiff_t line_size, int h);
 void ff_put_pixels8_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
                               int dstStride, int src1Stride, int h);
 void ff_put_no_rnd_pixels8_l2_mmxext(uint8_t *dst, uint8_t *src1,
@@ -96,56 +64,13 @@
                                      int src1Stride, int h);
 void ff_avg_pixels8_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
                               int dstStride, int src1Stride, int h);
-void ff_put_pixels16_x2_mmxext(uint8_t *block, const uint8_t *pixels,
-                               ptrdiff_t line_size, int h);
-void ff_put_pixels16_x2_3dnow(uint8_t *block, const uint8_t *pixels,
-                              ptrdiff_t line_size, int h);
 void ff_put_pixels16_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
                                int dstStride, int src1Stride, int h);
 void ff_avg_pixels16_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
                                int dstStride, int src1Stride, int h);
 void ff_put_no_rnd_pixels16_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
                                       int dstStride, int src1Stride, int h);
-void ff_put_no_rnd_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
-                                     ptrdiff_t line_size, int h);
-void ff_put_no_rnd_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels,
-                                    ptrdiff_t line_size, int h);
-void ff_put_no_rnd_pixels8_x2_exact_mmxext(uint8_t *block,
-                                           const uint8_t *pixels,
-                                           ptrdiff_t line_size, int h);
-void ff_put_no_rnd_pixels8_x2_exact_3dnow(uint8_t *block,
-                                          const uint8_t *pixels,
-                                          ptrdiff_t line_size, int h);
-void ff_put_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
-                              ptrdiff_t line_size, int h);
-void ff_put_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels,
-                             ptrdiff_t line_size, int h);
-void ff_put_no_rnd_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
-                                     ptrdiff_t line_size, int h);
-void ff_put_no_rnd_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels,
-                                    ptrdiff_t line_size, int h);
-void ff_put_no_rnd_pixels8_y2_exact_mmxext(uint8_t *block,
-                                           const uint8_t *pixels,
-                                           ptrdiff_t line_size, int h);
-void ff_put_no_rnd_pixels8_y2_exact_3dnow(uint8_t *block,
-                                          const uint8_t *pixels,
-                                          ptrdiff_t line_size, int h);
-void ff_avg_pixels8_3dnow(uint8_t *block, const uint8_t *pixels,
-                          ptrdiff_t line_size, int h);
-void ff_avg_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
-                              ptrdiff_t line_size, int h);
-void ff_avg_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels,
-                             ptrdiff_t line_size, int h);
-void ff_avg_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
-                              ptrdiff_t line_size, int h);
-void ff_avg_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels,
-                             ptrdiff_t line_size, int h);
-void ff_avg_pixels8_xy2_mmxext(uint8_t *block, const uint8_t *pixels,
-                               ptrdiff_t line_size, int h);
-void ff_avg_pixels8_xy2_3dnow(uint8_t *block, const uint8_t *pixels,
-                              ptrdiff_t line_size, int h);
 
-void ff_put_pixels8_mmxext(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h);
 static void ff_put_pixels16_mmxext(uint8_t *block, const uint8_t *pixels,
                                    ptrdiff_t line_size, int h)
 {
@@ -186,98 +111,6 @@
 
 #if HAVE_INLINE_ASM
 
-#define JUMPALIGN()     __asm__ volatile (".p2align 3"::)
-#define MOVQ_ZERO(regd) __asm__ volatile ("pxor %%"#regd", %%"#regd ::)
-
-#define MOVQ_BFE(regd)                                  \
-    __asm__ volatile (                                  \
-        "pcmpeqd %%"#regd", %%"#regd"   \n\t"           \
-        "paddb   %%"#regd", %%"#regd"   \n\t" ::)
-
-#ifndef PIC
-#define MOVQ_BONE(regd) __asm__ volatile ("movq %0, %%"#regd" \n\t" :: "m"(ff_bone))
-#define MOVQ_WTWO(regd) __asm__ volatile ("movq %0, %%"#regd" \n\t" :: "m"(ff_wtwo))
-#else
-// for shared library it's better to use this way for accessing constants
-// pcmpeqd -> -1
-#define MOVQ_BONE(regd)                                 \
-    __asm__ volatile (                                  \
-        "pcmpeqd  %%"#regd", %%"#regd"  \n\t"           \
-        "psrlw          $15, %%"#regd"  \n\t"           \
-        "packuswb %%"#regd", %%"#regd"  \n\t" ::)
-
-#define MOVQ_WTWO(regd)                                 \
-    __asm__ volatile (                                  \
-        "pcmpeqd %%"#regd", %%"#regd"   \n\t"           \
-        "psrlw         $15, %%"#regd"   \n\t"           \
-        "psllw          $1, %%"#regd"   \n\t"::)
-
-#endif
-
-// using regr as temporary and for the output result
-// first argument is unmodifed and second is trashed
-// regfe is supposed to contain 0xfefefefefefefefe
-#define PAVGB_MMX_NO_RND(rega, regb, regr, regfe)                \
-    "movq   "#rega", "#regr"            \n\t"                    \
-    "pand   "#regb", "#regr"            \n\t"                    \
-    "pxor   "#rega", "#regb"            \n\t"                    \
-    "pand  "#regfe", "#regb"            \n\t"                    \
-    "psrlq       $1, "#regb"            \n\t"                    \
-    "paddb  "#regb", "#regr"            \n\t"
-
-#define PAVGB_MMX(rega, regb, regr, regfe)                       \
-    "movq   "#rega", "#regr"            \n\t"                    \
-    "por    "#regb", "#regr"            \n\t"                    \
-    "pxor   "#rega", "#regb"            \n\t"                    \
-    "pand  "#regfe", "#regb"            \n\t"                    \
-    "psrlq       $1, "#regb"            \n\t"                    \
-    "psubb  "#regb", "#regr"            \n\t"
-
-// mm6 is supposed to contain 0xfefefefefefefefe
-#define PAVGBP_MMX_NO_RND(rega, regb, regr,  regc, regd, regp)   \
-    "movq  "#rega", "#regr"             \n\t"                    \
-    "movq  "#regc", "#regp"             \n\t"                    \
-    "pand  "#regb", "#regr"             \n\t"                    \
-    "pand  "#regd", "#regp"             \n\t"                    \
-    "pxor  "#rega", "#regb"             \n\t"                    \
-    "pxor  "#regc", "#regd"             \n\t"                    \
-    "pand    %%mm6, "#regb"             \n\t"                    \
-    "pand    %%mm6, "#regd"             \n\t"                    \
-    "psrlq      $1, "#regb"             \n\t"                    \
-    "psrlq      $1, "#regd"             \n\t"                    \
-    "paddb "#regb", "#regr"             \n\t"                    \
-    "paddb "#regd", "#regp"             \n\t"
-
-#define PAVGBP_MMX(rega, regb, regr, regc, regd, regp)           \
-    "movq  "#rega", "#regr"             \n\t"                    \
-    "movq  "#regc", "#regp"             \n\t"                    \
-    "por   "#regb", "#regr"             \n\t"                    \
-    "por   "#regd", "#regp"             \n\t"                    \
-    "pxor  "#rega", "#regb"             \n\t"                    \
-    "pxor  "#regc", "#regd"             \n\t"                    \
-    "pand    %%mm6, "#regb"             \n\t"                    \
-    "pand    %%mm6, "#regd"             \n\t"                    \
-    "psrlq      $1, "#regd"             \n\t"                    \
-    "psrlq      $1, "#regb"             \n\t"                    \
-    "psubb "#regb", "#regr"             \n\t"                    \
-    "psubb "#regd", "#regp"             \n\t"
-
-/***********************************/
-/* MMX no rounding */
-#define NO_RND 1
-#define DEF(x, y) x ## _no_rnd_ ## y ## _mmx
-#define SET_RND  MOVQ_WONE
-#define PAVGBP(a, b, c, d, e, f)        PAVGBP_MMX_NO_RND(a, b, c, d, e, f)
-#define PAVGB(a, b, c, e)               PAVGB_MMX_NO_RND(a, b, c, e)
-#define OP_AVG(a, b, c, e)              PAVGB_MMX(a, b, c, e)
-
-#include "dsputil_rnd_template.c"
-
-#undef DEF
-#undef SET_RND
-#undef PAVGBP
-#undef PAVGB
-#undef NO_RND
 /***********************************/
 /* MMX rounding */
 
@@ -285,6 +118,7 @@
 #define SET_RND  MOVQ_WTWO
 #define PAVGBP(a, b, c, d, e, f)        PAVGBP_MMX(a, b, c, d, e, f)
 #define PAVGB(a, b, c, e)               PAVGB_MMX(a, b, c, e)
+#define OP_AVG(a, b, c, e)              PAVGB_MMX(a, b, c, e)
 
 #include "dsputil_rnd_template.c"
 
@@ -300,30 +134,20 @@
 #if HAVE_YASM
 
 /***********************************/
-/* 3Dnow specific */
-
-#define DEF(x) x ## _3dnow
-
-#include "dsputil_avg_template.c"
-
-#undef DEF
-
-/***********************************/
 /* MMXEXT specific */
 
-#define DEF(x) x ## _mmxext
-
-#include "dsputil_avg_template.c"
-
-#undef DEF
+//FIXME the following could be optimized too ...
+static void ff_avg_pixels16_mmxext(uint8_t *block, const uint8_t *pixels,
+                                   int line_size, int h)
+{
+    ff_avg_pixels8_mmxext(block,     pixels,     line_size, h);
+    ff_avg_pixels8_mmxext(block + 8, pixels + 8, line_size, h);
+}
 
 #endif /* HAVE_YASM */
 
 
 #if HAVE_INLINE_ASM
-#define put_no_rnd_pixels16_mmx put_pixels16_mmx
-#define put_no_rnd_pixels8_mmx put_pixels8_mmx
-
 /***********************************/
 /* standard MMX */
 
@@ -645,12 +469,7 @@
     *left_top = tl;
 }
 #endif
-#endif /* HAVE_INLINE_ASM */
 
-void ff_h263_v_loop_filter_mmx(uint8_t *src, int stride, int qscale);
-void ff_h263_h_loop_filter_mmx(uint8_t *src, int stride, int qscale);
-
-#if HAVE_INLINE_ASM
 /* Draw the edges of width 'w' of an image of size width, height
  * this MMX version can only handle w == 8 || w == 16. */
 static void draw_edges_mmx(uint8_t *buf, int wrap, int width, int height,
@@ -771,13 +590,13 @@
 #if HAVE_YASM
 #define QPEL_OP(OPNAME, ROUNDER, RND, MMX)                              \
 static void OPNAME ## qpel8_mc00_ ## MMX (uint8_t *dst, uint8_t *src,   \
-                                          int stride)                   \
+                                          ptrdiff_t stride)             \
 {                                                                       \
     ff_ ## OPNAME ## pixels8_ ## MMX(dst, src, stride, 8);              \
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel8_mc10_ ## MMX(uint8_t *dst, uint8_t *src,    \
-                                         int stride)                    \
+                                         ptrdiff_t stride)              \
 {                                                                       \
     uint64_t temp[8];                                                   \
     uint8_t * const half = (uint8_t*)temp;                              \
@@ -788,14 +607,14 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel8_mc20_ ## MMX(uint8_t *dst, uint8_t *src,    \
-                                         int stride)                    \
+                                         ptrdiff_t stride)              \
 {                                                                       \
     ff_ ## OPNAME ## mpeg4_qpel8_h_lowpass_ ## MMX(dst, src, stride,    \
                                                    stride, 8);          \
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel8_mc30_ ## MMX(uint8_t *dst, uint8_t *src,    \
-                                         int stride)                    \
+                                         ptrdiff_t stride)              \
 {                                                                       \
     uint64_t temp[8];                                                   \
     uint8_t * const half = (uint8_t*)temp;                              \
@@ -806,7 +625,7 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel8_mc01_ ## MMX(uint8_t *dst, uint8_t *src,    \
-                                         int stride)                    \
+                                         ptrdiff_t stride)              \
 {                                                                       \
     uint64_t temp[8];                                                   \
     uint8_t * const half = (uint8_t*)temp;                              \
@@ -817,14 +636,14 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel8_mc02_ ## MMX(uint8_t *dst, uint8_t *src,    \
-                                         int stride)                    \
+                                         ptrdiff_t stride)              \
 {                                                                       \
     ff_ ## OPNAME ## mpeg4_qpel8_v_lowpass_ ## MMX(dst, src,            \
                                                    stride, stride);     \
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel8_mc03_ ## MMX(uint8_t *dst, uint8_t *src,    \
-                                         int stride)                    \
+                                         ptrdiff_t stride)              \
 {                                                                       \
     uint64_t temp[8];                                                   \
     uint8_t * const half = (uint8_t*)temp;                              \
@@ -835,7 +654,7 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel8_mc11_ ## MMX(uint8_t *dst, uint8_t *src,    \
-                                         int stride)                    \
+                                         ptrdiff_t stride)              \
 {                                                                       \
     uint64_t half[8 + 9];                                               \
     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
@@ -850,7 +669,7 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel8_mc31_ ## MMX(uint8_t *dst, uint8_t *src,    \
-                                         int stride)                    \
+                                         ptrdiff_t stride)              \
 {                                                                       \
     uint64_t half[8 + 9];                                               \
     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
@@ -865,7 +684,7 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel8_mc13_ ## MMX(uint8_t *dst, uint8_t *src,    \
-                                         int stride)                    \
+                                         ptrdiff_t stride)              \
 {                                                                       \
     uint64_t half[8 + 9];                                               \
     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
@@ -880,7 +699,7 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel8_mc33_ ## MMX(uint8_t *dst, uint8_t *src,    \
-                                         int stride)                    \
+                                         ptrdiff_t stride)              \
 {                                                                       \
     uint64_t half[8 + 9];                                               \
     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
@@ -895,7 +714,7 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel8_mc21_ ## MMX(uint8_t *dst, uint8_t *src,    \
-                                         int stride)                    \
+                                         ptrdiff_t stride)              \
 {                                                                       \
     uint64_t half[8 + 9];                                               \
     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
@@ -908,7 +727,7 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel8_mc23_ ## MMX(uint8_t *dst, uint8_t *src,    \
-                                         int stride)                    \
+                                         ptrdiff_t stride)              \
 {                                                                       \
     uint64_t half[8 + 9];                                               \
     uint8_t * const halfH  = ((uint8_t*)half) + 64;                     \
@@ -921,7 +740,7 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel8_mc12_ ## MMX(uint8_t *dst, uint8_t *src,    \
-                                         int stride)                    \
+                                         ptrdiff_t stride)              \
 {                                                                       \
     uint64_t half[8 + 9];                                               \
     uint8_t * const halfH = ((uint8_t*)half);                           \
@@ -934,7 +753,7 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel8_mc32_ ## MMX(uint8_t *dst, uint8_t *src,    \
-                                         int stride)                    \
+                                         ptrdiff_t stride)              \
 {                                                                       \
     uint64_t half[8 + 9];                                               \
     uint8_t * const halfH = ((uint8_t*)half);                           \
@@ -947,7 +766,7 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel8_mc22_ ## MMX(uint8_t *dst, uint8_t *src,    \
-                                         int stride)                    \
+                                         ptrdiff_t stride)              \
 {                                                                       \
     uint64_t half[9];                                                   \
     uint8_t * const halfH = ((uint8_t*)half);                           \
@@ -958,13 +777,13 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel16_mc00_ ## MMX (uint8_t *dst, uint8_t *src,  \
-                                           int stride)                  \
+                                           ptrdiff_t stride)            \
 {                                                                       \
     ff_ ## OPNAME ## pixels16_ ## MMX(dst, src, stride, 16);            \
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel16_mc10_ ## MMX(uint8_t *dst, uint8_t *src,   \
-                                          int stride)                   \
+                                          ptrdiff_t stride)             \
 {                                                                       \
     uint64_t temp[32];                                                  \
     uint8_t * const half = (uint8_t*)temp;                              \
@@ -975,14 +794,14 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel16_mc20_ ## MMX(uint8_t *dst, uint8_t *src,   \
-                                          int stride)                   \
+                                          ptrdiff_t stride)             \
 {                                                                       \
     ff_ ## OPNAME ## mpeg4_qpel16_h_lowpass_ ## MMX(dst, src,           \
                                                     stride, stride, 16);\
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel16_mc30_ ## MMX(uint8_t *dst, uint8_t *src,   \
-                                          int stride)                   \
+                                          ptrdiff_t stride)             \
 {                                                                       \
     uint64_t temp[32];                                                  \
     uint8_t * const half = (uint8_t*)temp;                              \
@@ -993,7 +812,7 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel16_mc01_ ## MMX(uint8_t *dst, uint8_t *src,   \
-                                          int stride)                   \
+                                          ptrdiff_t stride)             \
 {                                                                       \
     uint64_t temp[32];                                                  \
     uint8_t * const half = (uint8_t*)temp;                              \
@@ -1004,14 +823,14 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel16_mc02_ ## MMX(uint8_t *dst, uint8_t *src,   \
-                                          int stride)                   \
+                                          ptrdiff_t stride)             \
 {                                                                       \
     ff_ ## OPNAME ## mpeg4_qpel16_v_lowpass_ ## MMX(dst, src,           \
                                                     stride, stride);    \
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel16_mc03_ ## MMX(uint8_t *dst, uint8_t *src,   \
-                                          int stride)                   \
+                                          ptrdiff_t stride)             \
 {                                                                       \
     uint64_t temp[32];                                                  \
     uint8_t * const half = (uint8_t*)temp;                              \
@@ -1022,7 +841,7 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel16_mc11_ ## MMX(uint8_t *dst, uint8_t *src,   \
-                                          int stride)                   \
+                                          ptrdiff_t stride)             \
 {                                                                       \
     uint64_t half[16 * 2 + 17 * 2];                                     \
     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
@@ -1038,7 +857,7 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel16_mc31_ ## MMX(uint8_t *dst, uint8_t *src,   \
-                                          int stride)                   \
+                                          ptrdiff_t stride)             \
 {                                                                       \
     uint64_t half[16 * 2 + 17 * 2];                                     \
     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
@@ -1054,7 +873,7 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel16_mc13_ ## MMX(uint8_t *dst, uint8_t *src,   \
-                                          int stride)                   \
+                                          ptrdiff_t stride)             \
 {                                                                       \
     uint64_t half[16 * 2 + 17 * 2];                                     \
     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
@@ -1070,7 +889,7 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel16_mc33_ ## MMX(uint8_t *dst, uint8_t *src,   \
-                                          int stride)                   \
+                                          ptrdiff_t stride)             \
 {                                                                       \
     uint64_t half[16 * 2 + 17 * 2];                                     \
     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
@@ -1086,7 +905,7 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel16_mc21_ ## MMX(uint8_t *dst, uint8_t *src,   \
-                                          int stride)                   \
+                                          ptrdiff_t stride)             \
 {                                                                       \
     uint64_t half[16 * 2 + 17 * 2];                                     \
     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
@@ -1100,7 +919,7 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel16_mc23_ ## MMX(uint8_t *dst, uint8_t *src,   \
-                                          int stride)                   \
+                                          ptrdiff_t stride)             \
 {                                                                       \
     uint64_t half[16 * 2 + 17 * 2];                                     \
     uint8_t * const halfH  = ((uint8_t*)half) + 256;                    \
@@ -1114,7 +933,7 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel16_mc12_ ## MMX(uint8_t *dst, uint8_t *src,   \
-                                          int stride)                   \
+                                          ptrdiff_t stride)             \
 {                                                                       \
     uint64_t half[17 * 2];                                              \
     uint8_t * const halfH = ((uint8_t*)half);                           \
@@ -1127,7 +946,7 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel16_mc32_ ## MMX(uint8_t *dst, uint8_t *src,   \
-                                          int stride)                   \
+                                          ptrdiff_t stride)             \
 {                                                                       \
     uint64_t half[17 * 2];                                              \
     uint8_t * const halfH = ((uint8_t*)half);                           \
@@ -1140,7 +959,7 @@
 }                                                                       \
                                                                         \
 static void OPNAME ## qpel16_mc22_ ## MMX(uint8_t *dst, uint8_t *src,   \
-                                          int stride)                   \
+                                          ptrdiff_t stride)             \
 {                                                                       \
     uint64_t half[17 * 2];                                              \
     uint8_t * const halfH = ((uint8_t*)half);                           \
@@ -1157,19 +976,19 @@
 
 
 #if HAVE_INLINE_ASM
-void ff_put_rv40_qpel8_mc33_mmx(uint8_t *dst, uint8_t *src, int stride)
+void ff_put_rv40_qpel8_mc33_mmx(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
 {
   put_pixels8_xy2_mmx(dst, src, stride, 8);
 }
-void ff_put_rv40_qpel16_mc33_mmx(uint8_t *dst, uint8_t *src, int stride)
+void ff_put_rv40_qpel16_mc33_mmx(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
 {
   put_pixels16_xy2_mmx(dst, src, stride, 16);
 }
-void ff_avg_rv40_qpel8_mc33_mmx(uint8_t *dst, uint8_t *src, int stride)
+void ff_avg_rv40_qpel8_mc33_mmx(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
 {
   avg_pixels8_xy2_mmx(dst, src, stride, 8);
 }
-void ff_avg_rv40_qpel16_mc33_mmx(uint8_t *dst, uint8_t *src, int stride)
+void ff_avg_rv40_qpel16_mc33_mmx(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
 {
   avg_pixels16_xy2_mmx(dst, src, stride, 16);
 }
@@ -1304,6 +1123,7 @@
     }
 }
 
+
 #if CONFIG_VIDEODSP
 #if HAVE_YASM
 #if ARCH_X86_32
@@ -1336,39 +1156,30 @@
 #endif
 #endif
 
-#endif /* HAVE_INLINE_ASM */
-
-void ff_put_pixels16_sse2(uint8_t *block, const uint8_t *pixels,
-                          ptrdiff_t line_size, int h);
-void ff_avg_pixels16_sse2(uint8_t *block, const uint8_t *pixels,
-                          ptrdiff_t line_size, int h);
-
-#if HAVE_INLINE_ASM
-
 /* CAVS-specific */
-void ff_put_cavs_qpel8_mc00_mmxext(uint8_t *dst, uint8_t *src, int stride)
+void ff_put_cavs_qpel8_mc00_mmx(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
 {
     put_pixels8_mmx(dst, src, stride, 8);
 }
 
-void ff_avg_cavs_qpel8_mc00_mmxext(uint8_t *dst, uint8_t *src, int stride)
+void ff_avg_cavs_qpel8_mc00_mmx(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
 {
     avg_pixels8_mmx(dst, src, stride, 8);
 }
 
-void ff_put_cavs_qpel16_mc00_mmxext(uint8_t *dst, uint8_t *src, int stride)
+void ff_put_cavs_qpel16_mc00_mmx(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
 {
     put_pixels16_mmx(dst, src, stride, 16);
 }
 
-void ff_avg_cavs_qpel16_mc00_mmxext(uint8_t *dst, uint8_t *src, int stride)
+void ff_avg_cavs_qpel16_mc00_mmx(uint8_t *dst, uint8_t *src, ptrdiff_t stride)
 {
     avg_pixels16_mmx(dst, src, stride, 16);
 }
 
 /* VC-1-specific */
 void ff_put_vc1_mspel_mc00_mmx(uint8_t *dst, const uint8_t *src,
-                               int stride, int rnd)
+                               ptrdiff_t stride, int rnd)
 {
     put_pixels8_mmx(dst, src, stride, 8);
 }
@@ -1442,38 +1253,6 @@
 #endif
 #endif
 
-/* XXX: Those functions should be suppressed ASAP when all IDCTs are
- * converted. */
-#if CONFIG_GPL
-static void ff_libmpeg2mmx_idct_put(uint8_t *dest, int line_size,
-                                    int16_t *block)
-{
-    ff_mmx_idct(block);
-    ff_put_pixels_clamped_mmx(block, dest, line_size);
-}
-
-static void ff_libmpeg2mmx_idct_add(uint8_t *dest, int line_size,
-                                    int16_t *block)
-{
-    ff_mmx_idct(block);
-    ff_add_pixels_clamped_mmx(block, dest, line_size);
-}
-
-static void ff_libmpeg2mmx2_idct_put(uint8_t *dest, int line_size,
-                                     int16_t *block)
-{
-    ff_mmxext_idct(block);
-    ff_put_pixels_clamped_mmx(block, dest, line_size);
-}
-
-static void ff_libmpeg2mmx2_idct_add(uint8_t *dest, int line_size,
-                                     int16_t *block)
-{
-    ff_mmxext_idct(block);
-    ff_add_pixels_clamped_mmx(block, dest, line_size);
-}
-#endif
-
 static void vector_clipf_sse(float *dst, const float *src,
                              float min, float max, int len)
 {
@@ -1510,6 +1289,9 @@
 
 #endif /* HAVE_INLINE_ASM */
 
+void ff_h263_v_loop_filter_mmx(uint8_t *src, int stride, int qscale);
+void ff_h263_h_loop_filter_mmx(uint8_t *src, int stride, int qscale);
+
 int32_t ff_scalarproduct_int16_mmxext(const int16_t *v1, const int16_t *v2,
                                       int order);
 int32_t ff_scalarproduct_int16_sse2(const int16_t *v1, const int16_t *v2,
@@ -1577,20 +1359,12 @@
     c->PFX ## _pixels_tab[IDX][15] = PREFIX ## PFX ## SIZE ## _mc33_ ## CPU; \
     } while (0)
 
-#define SET_HPEL_FUNCS(PFX, IDX, SIZE, CPU)                                     \
-    do {                                                                        \
-        c->PFX ## _pixels_tab IDX [0] = PFX ## _pixels ## SIZE ## _     ## CPU; \
-        c->PFX ## _pixels_tab IDX [1] = PFX ## _pixels ## SIZE ## _x2_  ## CPU; \
-        c->PFX ## _pixels_tab IDX [2] = PFX ## _pixels ## SIZE ## _y2_  ## CPU; \
-        c->PFX ## _pixels_tab IDX [3] = PFX ## _pixels ## SIZE ## _xy2_ ## CPU; \
-    } while (0)
-
 static av_cold void dsputil_init_mmx(DSPContext *c, AVCodecContext *avctx,
                                      int mm_flags)
 {
+#if HAVE_INLINE_ASM
     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
 
-#if HAVE_INLINE_ASM
     c->put_pixels_clamped        = ff_put_pixels_clamped_mmx;
     c->put_signed_pixels_clamped = ff_put_signed_pixels_clamped_mmx;
     c->add_pixels_clamped        = ff_add_pixels_clamped_mmx;
@@ -1599,14 +1373,6 @@
         c->clear_block  = clear_block_mmx;
         c->clear_blocks = clear_blocks_mmx;
         c->draw_edges   = draw_edges_mmx;
-
-        SET_HPEL_FUNCS(put,        [0], 16, mmx);
-        SET_HPEL_FUNCS(put_no_rnd, [0], 16, mmx);
-        SET_HPEL_FUNCS(avg,        [0], 16, mmx);
-        SET_HPEL_FUNCS(avg_no_rnd,    , 16, mmx);
-        SET_HPEL_FUNCS(put,        [1],  8, mmx);
-        SET_HPEL_FUNCS(put_no_rnd, [1],  8, mmx);
-        SET_HPEL_FUNCS(avg,        [1],  8, mmx);
     }
 
 #if CONFIG_VIDEODSP && (ARCH_X86_32 || !HAVE_YASM)
@@ -1623,17 +1389,13 @@
     }
 
     c->vector_clip_int32 = ff_vector_clip_int32_mmx;
-#endif
-
+#endif /* HAVE_YASM */
 }
 
 static av_cold void dsputil_init_mmxext(DSPContext *c, AVCodecContext *avctx,
                                         int mm_flags)
 {
-    const int bit_depth      = avctx->bits_per_raw_sample;
-    const int high_bit_depth = bit_depth > 8;
-
-#if HAVE_YASM
+#if HAVE_MMXEXT_EXTERNAL
     SET_QPEL_FUNCS(avg_qpel,        0, 16, mmxext, );
     SET_QPEL_FUNCS(avg_qpel,        1,  8, mmxext, );
 
@@ -1642,42 +1404,6 @@
     SET_QPEL_FUNCS(put_no_rnd_qpel, 0, 16, mmxext, );
     SET_QPEL_FUNCS(put_no_rnd_qpel, 1,  8, mmxext, );
 
-    if (!high_bit_depth) {
-        c->put_pixels_tab[0][1] = ff_put_pixels16_x2_mmxext;
-        c->put_pixels_tab[0][2] = ff_put_pixels16_y2_mmxext;
-
-        c->avg_pixels_tab[0][0] = ff_avg_pixels16_mmxext;
-        c->avg_pixels_tab[0][1] = ff_avg_pixels16_x2_mmxext;
-        c->avg_pixels_tab[0][2] = ff_avg_pixels16_y2_mmxext;
-
-        c->put_pixels_tab[1][1] = ff_put_pixels8_x2_mmxext;
-        c->put_pixels_tab[1][2] = ff_put_pixels8_y2_mmxext;
-
-        c->avg_pixels_tab[1][0] = ff_avg_pixels8_mmxext;
-        c->avg_pixels_tab[1][1] = ff_avg_pixels8_x2_mmxext;
-        c->avg_pixels_tab[1][2] = ff_avg_pixels8_y2_mmxext;
-    }
-
-    if (!(avctx->flags & CODEC_FLAG_BITEXACT)) {
-        if (!high_bit_depth) {
-            c->put_no_rnd_pixels_tab[0][1] = ff_put_no_rnd_pixels16_x2_mmxext;
-            c->put_no_rnd_pixels_tab[0][2] = ff_put_no_rnd_pixels16_y2_mmxext;
-            c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_mmxext;
-            c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_mmxext;
-
-            c->avg_pixels_tab[0][3] = ff_avg_pixels16_xy2_mmxext;
-            c->avg_pixels_tab[1][3] = ff_avg_pixels8_xy2_mmxext;
-        }
-    }
-#endif /* HAVE_YASM */
-
-#if HAVE_MMXEXT_EXTERNAL
-    if (CONFIG_VP3_DECODER && (avctx->codec_id == AV_CODEC_ID_VP3 ||
-                               avctx->codec_id == AV_CODEC_ID_THEORA)) {
-        c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_exact_mmxext;
-        c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_exact_mmxext;
-    }
-
     /* slower than cmov version on AMD */
     if (!(mm_flags & AV_CPU_FLAG_3DNOW))
         c->add_hfyu_median_prediction = ff_add_hfyu_median_prediction_mmxext;
@@ -1693,52 +1419,12 @@
 #endif /* HAVE_MMXEXT_EXTERNAL */
 }
 
-static av_cold void dsputil_init_3dnow(DSPContext *c, AVCodecContext *avctx,
-                                       int mm_flags)
-{
-    const int high_bit_depth = avctx->bits_per_raw_sample > 8;
-
-#if HAVE_YASM
-    if (!high_bit_depth) {
-        c->put_pixels_tab[0][1] = ff_put_pixels16_x2_3dnow;
-        c->put_pixels_tab[0][2] = ff_put_pixels16_y2_3dnow;
-
-        c->avg_pixels_tab[0][0] = ff_avg_pixels16_3dnow;
-        c->avg_pixels_tab[0][1] = ff_avg_pixels16_x2_3dnow;
-        c->avg_pixels_tab[0][2] = ff_avg_pixels16_y2_3dnow;
-
-        c->put_pixels_tab[1][1] = ff_put_pixels8_x2_3dnow;
-        c->put_pixels_tab[1][2] = ff_put_pixels8_y2_3dnow;
-
-        c->avg_pixels_tab[1][0] = ff_avg_pixels8_3dnow;
-        c->avg_pixels_tab[1][1] = ff_avg_pixels8_x2_3dnow;
-        c->avg_pixels_tab[1][2] = ff_avg_pixels8_y2_3dnow;
-
-        if (!(avctx->flags & CODEC_FLAG_BITEXACT)){
-            c->put_no_rnd_pixels_tab[0][1] = ff_put_no_rnd_pixels16_x2_3dnow;
-            c->put_no_rnd_pixels_tab[0][2] = ff_put_no_rnd_pixels16_y2_3dnow;
-            c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_3dnow;
-            c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_3dnow;
-
-            c->avg_pixels_tab[0][3] = ff_avg_pixels16_xy2_3dnow;
-            c->avg_pixels_tab[1][3] = ff_avg_pixels8_xy2_3dnow;
-        }
-    }
-
-    if (CONFIG_VP3_DECODER && (avctx->codec_id == AV_CODEC_ID_VP3 ||
-                               avctx->codec_id == AV_CODEC_ID_THEORA)) {
-        c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_exact_3dnow;
-        c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_exact_3dnow;
-    }
-#endif /* HAVE_YASM */
-}
-
 static av_cold void dsputil_init_sse(DSPContext *c, AVCodecContext *avctx,
                                      int mm_flags)
 {
+#if HAVE_INLINE_ASM
     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
 
-#if HAVE_INLINE_ASM
     if (!high_bit_depth) {
         if (!(CONFIG_MPEG_XVMC_DECODER && avctx->xvmc_acceleration > 1)) {
             /* XvMCCreateBlocks() may not allocate 16-byte aligned blocks */
@@ -1760,10 +1446,9 @@
 static av_cold void dsputil_init_sse2(DSPContext *c, AVCodecContext *avctx,
                                       int mm_flags)
 {
-    const int bit_depth      = avctx->bits_per_raw_sample;
-    const int high_bit_depth = bit_depth > 8;
-
 #if HAVE_SSE2_INLINE
+    const int high_bit_depth = avctx->bits_per_raw_sample > 8;
+
     if (!high_bit_depth && avctx->idct_algo == FF_IDCT_XVIDMMX) {
         c->idct_put              = ff_idct_xvid_sse2_put;
         c->idct_add              = ff_idct_xvid_sse2_add;
@@ -1773,15 +1458,6 @@
 #endif /* HAVE_SSE2_INLINE */
 
 #if HAVE_SSE2_EXTERNAL
-    if (!(mm_flags & AV_CPU_FLAG_SSE2SLOW)) {
-        // these functions are slower than mmx on AMD, but faster on Intel
-        if (!high_bit_depth) {
-            c->put_pixels_tab[0][0]        = ff_put_pixels16_sse2;
-            c->put_no_rnd_pixels_tab[0][0] = ff_put_pixels16_sse2;
-            c->avg_pixels_tab[0][0]        = ff_avg_pixels16_sse2;
-        }
-    }
-
     c->scalarproduct_int16          = ff_scalarproduct_int16_sse2;
     c->scalarproduct_and_madd_int16 = ff_scalarproduct_and_madd_int16_sse2;
     if (mm_flags & AV_CPU_FLAG_ATOM) {
@@ -1843,19 +1519,6 @@
                 c->idct_add              = ff_simple_idct_add_mmx;
                 c->idct                  = ff_simple_idct_mmx;
                 c->idct_permutation_type = FF_SIMPLE_IDCT_PERM;
-#if CONFIG_GPL
-            } else if (idct_algo == FF_IDCT_LIBMPEG2MMX) {
-                if (mm_flags & AV_CPU_FLAG_MMX2) {
-                    c->idct_put = ff_libmpeg2mmx2_idct_put;
-                    c->idct_add = ff_libmpeg2mmx2_idct_add;
-                    c->idct     = ff_mmxext_idct;
-                } else {
-                    c->idct_put = ff_libmpeg2mmx_idct_put;
-                    c->idct_add = ff_libmpeg2mmx_idct_add;
-                    c->idct     = ff_mmx_idct;
-                }
-                c->idct_permutation_type = FF_LIBMPEG2_IDCT_PERM;
-#endif
             } else if (idct_algo == FF_IDCT_XVIDMMX) {
                 if (mm_flags & AV_CPU_FLAG_SSE2) {
                     c->idct_put              = ff_idct_xvid_sse2_put;
@@ -1881,9 +1544,6 @@
     if (mm_flags & AV_CPU_FLAG_MMXEXT)
         dsputil_init_mmxext(c, avctx, mm_flags);
 
-    if (mm_flags & AV_CPU_FLAG_3DNOW)
-        dsputil_init_3dnow(c, avctx, mm_flags);
-
     if (mm_flags & AV_CPU_FLAG_SSE)
         dsputil_init_sse(c, avctx, mm_flags);
 
diff --git a/libavcodec/x86/dsputil_mmx.h b/libavcodec/x86/dsputil_mmx.h
index f365970..1b68161 100644
--- a/libavcodec/x86/dsputil_mmx.h
+++ b/libavcodec/x86/dsputil_mmx.h
@@ -28,8 +28,6 @@
 #include "libavcodec/dsputil.h"
 #include "libavutil/x86/asm.h"
 
-typedef struct xmm_reg { uint64_t a, b; } xmm_reg;
-
 extern const uint64_t ff_bone;
 extern const uint64_t ff_wtwo;
 
@@ -41,12 +39,9 @@
 extern const xmm_reg  ff_pw_16;
 extern const xmm_reg  ff_pw_18;
 extern const uint64_t ff_pw_20;
-extern const xmm_reg  ff_pw_27;
-extern const xmm_reg  ff_pw_28;
 extern const xmm_reg  ff_pw_32;
 extern const uint64_t ff_pw_42;
 extern const uint64_t ff_pw_53;
-extern const xmm_reg  ff_pw_63;
 extern const xmm_reg  ff_pw_64;
 extern const uint64_t ff_pw_96;
 extern const uint64_t ff_pw_128;
@@ -54,14 +49,9 @@
 
 extern const xmm_reg  ff_pb_1;
 extern const xmm_reg  ff_pb_3;
-extern const uint64_t ff_pb_7;
-extern const uint64_t ff_pb_1F;
 extern const uint64_t ff_pb_3F;
-extern const uint64_t ff_pb_81;
-extern const xmm_reg  ff_pb_A1;
 extern const xmm_reg  ff_pb_F8;
 extern const uint64_t ff_pb_FC;
-extern const xmm_reg  ff_pb_FE;
 
 extern const double ff_pd_1[2];
 extern const double ff_pd_2[2];
@@ -82,6 +72,82 @@
     "pcmpeqd %%" #regd ", %%" #regd " \n\t" \
     "psrlw $15, %%" #regd ::)
 
+#define JUMPALIGN()     __asm__ volatile (".p2align 3"::)
+#define MOVQ_ZERO(regd) __asm__ volatile ("pxor %%"#regd", %%"#regd ::)
+
+#define MOVQ_BFE(regd)                                  \
+    __asm__ volatile (                                  \
+        "pcmpeqd %%"#regd", %%"#regd"   \n\t"           \
+        "paddb   %%"#regd", %%"#regd"   \n\t" ::)
+
+#ifndef PIC
+#define MOVQ_BONE(regd) __asm__ volatile ("movq %0, %%"#regd" \n\t" :: "m"(ff_bone))
+#define MOVQ_WTWO(regd) __asm__ volatile ("movq %0, %%"#regd" \n\t" :: "m"(ff_wtwo))
+#else
+// for shared library it's better to use this way for accessing constants
+// pcmpeqd -> -1
+#define MOVQ_BONE(regd)                                 \
+    __asm__ volatile (                                  \
+        "pcmpeqd  %%"#regd", %%"#regd"  \n\t"           \
+        "psrlw          $15, %%"#regd"  \n\t"           \
+        "packuswb %%"#regd", %%"#regd"  \n\t" ::)
+
+#define MOVQ_WTWO(regd)                                 \
+    __asm__ volatile (                                  \
+        "pcmpeqd %%"#regd", %%"#regd"   \n\t"           \
+        "psrlw         $15, %%"#regd"   \n\t"           \
+        "psllw          $1, %%"#regd"   \n\t"::)
+
+#endif
+
+// using regr as temporary and for the output result
+// first argument is unmodifed and second is trashed
+// regfe is supposed to contain 0xfefefefefefefefe
+#define PAVGB_MMX_NO_RND(rega, regb, regr, regfe)                \
+    "movq   "#rega", "#regr"            \n\t"                    \
+    "pand   "#regb", "#regr"            \n\t"                    \
+    "pxor   "#rega", "#regb"            \n\t"                    \
+    "pand  "#regfe", "#regb"            \n\t"                    \
+    "psrlq       $1, "#regb"            \n\t"                    \
+    "paddb  "#regb", "#regr"            \n\t"
+
+#define PAVGB_MMX(rega, regb, regr, regfe)                       \
+    "movq   "#rega", "#regr"            \n\t"                    \
+    "por    "#regb", "#regr"            \n\t"                    \
+    "pxor   "#rega", "#regb"            \n\t"                    \
+    "pand  "#regfe", "#regb"            \n\t"                    \
+    "psrlq       $1, "#regb"            \n\t"                    \
+    "psubb  "#regb", "#regr"            \n\t"
+
+// mm6 is supposed to contain 0xfefefefefefefefe
+#define PAVGBP_MMX_NO_RND(rega, regb, regr,  regc, regd, regp)   \
+    "movq  "#rega", "#regr"             \n\t"                    \
+    "movq  "#regc", "#regp"             \n\t"                    \
+    "pand  "#regb", "#regr"             \n\t"                    \
+    "pand  "#regd", "#regp"             \n\t"                    \
+    "pxor  "#rega", "#regb"             \n\t"                    \
+    "pxor  "#regc", "#regd"             \n\t"                    \
+    "pand    %%mm6, "#regb"             \n\t"                    \
+    "pand    %%mm6, "#regd"             \n\t"                    \
+    "psrlq      $1, "#regb"             \n\t"                    \
+    "psrlq      $1, "#regd"             \n\t"                    \
+    "paddb "#regb", "#regr"             \n\t"                    \
+    "paddb "#regd", "#regp"             \n\t"
+
+#define PAVGBP_MMX(rega, regb, regr, regc, regd, regp)           \
+    "movq  "#rega", "#regr"             \n\t"                    \
+    "movq  "#regc", "#regp"             \n\t"                    \
+    "por   "#regb", "#regr"             \n\t"                    \
+    "por   "#regd", "#regp"             \n\t"                    \
+    "pxor  "#rega", "#regb"             \n\t"                    \
+    "pxor  "#regc", "#regd"             \n\t"                    \
+    "pand    %%mm6, "#regb"             \n\t"                    \
+    "pand    %%mm6, "#regd"             \n\t"                    \
+    "psrlq      $1, "#regd"             \n\t"                    \
+    "psrlq      $1, "#regb"             \n\t"                    \
+    "psubb "#regb", "#regr"             \n\t"                    \
+    "psubb "#regd", "#regp"             \n\t"
+
 void ff_dsputilenc_init_mmx(DSPContext* c, AVCodecContext *avctx);
 void ff_dsputil_init_pix_mmx(DSPContext* c, AVCodecContext *avctx);
 
@@ -91,18 +157,24 @@
 
 void ff_avg_pixels8_mmxext(uint8_t *block, const uint8_t *pixels,
                            ptrdiff_t line_size, int h);
+void ff_put_pixels8_mmxext(uint8_t *block, const uint8_t *pixels,
+                           ptrdiff_t line_size, int h);
+void ff_avg_pixels16_sse2(uint8_t *block, const uint8_t *pixels,
+                          ptrdiff_t line_size, int h);
+void ff_put_pixels16_sse2(uint8_t *block, const uint8_t *pixels,
+                          ptrdiff_t line_size, int h);
 
-void ff_put_cavs_qpel8_mc00_mmxext(uint8_t *dst, uint8_t *src, int stride);
-void ff_avg_cavs_qpel8_mc00_mmxext(uint8_t *dst, uint8_t *src, int stride);
-void ff_put_cavs_qpel16_mc00_mmxext(uint8_t *dst, uint8_t *src, int stride);
-void ff_avg_cavs_qpel16_mc00_mmxext(uint8_t *dst, uint8_t *src, int stride);
+void ff_put_cavs_qpel8_mc00_mmx(uint8_t *dst, uint8_t *src, ptrdiff_t stride);
+void ff_avg_cavs_qpel8_mc00_mmx(uint8_t *dst, uint8_t *src, ptrdiff_t stride);
+void ff_put_cavs_qpel16_mc00_mmx(uint8_t *dst, uint8_t *src, ptrdiff_t stride);
+void ff_avg_cavs_qpel16_mc00_mmx(uint8_t *dst, uint8_t *src, ptrdiff_t stride);
 
-void ff_put_vc1_mspel_mc00_mmx(uint8_t *dst, const uint8_t *src, int stride, int rnd);
+void ff_put_vc1_mspel_mc00_mmx(uint8_t *dst, const uint8_t *src, ptrdiff_t stride, int rnd);
 
-void ff_put_rv40_qpel8_mc33_mmx(uint8_t *block, uint8_t *pixels, int line_size);
-void ff_put_rv40_qpel16_mc33_mmx(uint8_t *block, uint8_t *pixels, int line_size);
-void ff_avg_rv40_qpel8_mc33_mmx(uint8_t *block, uint8_t *pixels, int line_size);
-void ff_avg_rv40_qpel16_mc33_mmx(uint8_t *block, uint8_t *pixels, int line_size);
+void ff_put_rv40_qpel8_mc33_mmx(uint8_t *block, uint8_t *pixels, ptrdiff_t stride);
+void ff_put_rv40_qpel16_mc33_mmx(uint8_t *block, uint8_t *pixels, ptrdiff_t stride);
+void ff_avg_rv40_qpel8_mc33_mmx(uint8_t *block, uint8_t *pixels, ptrdiff_t stride);
+void ff_avg_rv40_qpel16_mc33_mmx(uint8_t *block, uint8_t *pixels, ptrdiff_t stride);
 
 void ff_mmx_idct(int16_t *block);
 void ff_mmxext_idct(int16_t *block);
diff --git a/libavcodec/x86/dsputil_qns_template.c b/libavcodec/x86/dsputil_qns_template.c
index 77a41b9..bde6b0a 100644
--- a/libavcodec/x86/dsputil_qns_template.c
+++ b/libavcodec/x86/dsputil_qns_template.c
@@ -28,7 +28,7 @@
 {
     x86_reg i=0;
 
-    assert(FFABS(scale) < MAX_ABS);
+    av_assert2(FFABS(scale) < MAX_ABS);
     scale<<= 16 + SCALE_OFFSET - BASIS_SHIFT + RECON_SHIFT;
 
     SET_RND(mm6);
diff --git a/libavcodec/x86/dsputil_rnd_template.c b/libavcodec/x86/dsputil_rnd_template.c
index 4568207..7765309 100644
--- a/libavcodec/x86/dsputil_rnd_template.c
+++ b/libavcodec/x86/dsputil_rnd_template.c
@@ -25,212 +25,6 @@
  */
 
 // put_pixels
-static void DEF(put, pixels8_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    MOVQ_BFE(mm6);
-    __asm__ volatile(
-        "lea    (%3, %3), %%"REG_a"     \n\t"
-        ".p2align 3                     \n\t"
-        "1:                             \n\t"
-        "movq   (%1), %%mm0             \n\t"
-        "movq   1(%1), %%mm1            \n\t"
-        "movq   (%1, %3), %%mm2         \n\t"
-        "movq   1(%1, %3), %%mm3        \n\t"
-        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
-        "movq   %%mm4, (%2)             \n\t"
-        "movq   %%mm5, (%2, %3)         \n\t"
-        "add    %%"REG_a", %1           \n\t"
-        "add    %%"REG_a", %2           \n\t"
-        "movq   (%1), %%mm0             \n\t"
-        "movq   1(%1), %%mm1            \n\t"
-        "movq   (%1, %3), %%mm2         \n\t"
-        "movq   1(%1, %3), %%mm3        \n\t"
-        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
-        "movq   %%mm4, (%2)             \n\t"
-        "movq   %%mm5, (%2, %3)         \n\t"
-        "add    %%"REG_a", %1           \n\t"
-        "add    %%"REG_a", %2           \n\t"
-        "subl   $4, %0                  \n\t"
-        "jnz    1b                      \n\t"
-        :"+g"(h), "+S"(pixels), "+D"(block)
-        :"r"((x86_reg)line_size)
-        :REG_a, "memory");
-}
-
-static void av_unused DEF(put, pixels8_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
-{
-    MOVQ_BFE(mm6);
-    __asm__ volatile(
-        "testl $1, %0                   \n\t"
-        " jz 1f                         \n\t"
-        "movq   (%1), %%mm0             \n\t"
-        "movq   (%2), %%mm1             \n\t"
-        "add    %4, %1                  \n\t"
-        "add    $8, %2                  \n\t"
-        PAVGB(%%mm0, %%mm1, %%mm4, %%mm6)
-        "movq   %%mm4, (%3)             \n\t"
-        "add    %5, %3                  \n\t"
-        "decl   %0                      \n\t"
-        ".p2align 3                     \n\t"
-        "1:                             \n\t"
-        "movq   (%1), %%mm0             \n\t"
-        "movq   (%2), %%mm1             \n\t"
-        "add    %4, %1                  \n\t"
-        "movq   (%1), %%mm2             \n\t"
-        "movq   8(%2), %%mm3            \n\t"
-        "add    %4, %1                  \n\t"
-        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
-        "movq   %%mm4, (%3)             \n\t"
-        "add    %5, %3                  \n\t"
-        "movq   %%mm5, (%3)             \n\t"
-        "add    %5, %3                  \n\t"
-        "movq   (%1), %%mm0             \n\t"
-        "movq   16(%2), %%mm1           \n\t"
-        "add    %4, %1                  \n\t"
-        "movq   (%1), %%mm2             \n\t"
-        "movq   24(%2), %%mm3           \n\t"
-        "add    %4, %1                  \n\t"
-        "add    $32, %2                 \n\t"
-        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
-        "movq   %%mm4, (%3)             \n\t"
-        "add    %5, %3                  \n\t"
-        "movq   %%mm5, (%3)             \n\t"
-        "add    %5, %3                  \n\t"
-        "subl   $4, %0                  \n\t"
-        "jnz    1b                      \n\t"
-#if !HAVE_EBX_AVAILABLE //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cannot be used
-        :"+m"(h), "+a"(src1), "+c"(src2), "+d"(dst)
-#else
-        :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst)
-#endif
-        :"S"((x86_reg)src1Stride), "D"((x86_reg)dstStride)
-        :"memory");
-}
-
-static void DEF(put, pixels16_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    MOVQ_BFE(mm6);
-    __asm__ volatile(
-        "lea        (%3, %3), %%"REG_a" \n\t"
-        ".p2align 3                     \n\t"
-        "1:                             \n\t"
-        "movq   (%1), %%mm0             \n\t"
-        "movq   1(%1), %%mm1            \n\t"
-        "movq   (%1, %3), %%mm2         \n\t"
-        "movq   1(%1, %3), %%mm3        \n\t"
-        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
-        "movq   %%mm4, (%2)             \n\t"
-        "movq   %%mm5, (%2, %3)         \n\t"
-        "movq   8(%1), %%mm0            \n\t"
-        "movq   9(%1), %%mm1            \n\t"
-        "movq   8(%1, %3), %%mm2        \n\t"
-        "movq   9(%1, %3), %%mm3        \n\t"
-        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
-        "movq   %%mm4, 8(%2)            \n\t"
-        "movq   %%mm5, 8(%2, %3)        \n\t"
-        "add    %%"REG_a", %1           \n\t"
-        "add    %%"REG_a", %2           \n\t"
-        "movq   (%1), %%mm0             \n\t"
-        "movq   1(%1), %%mm1            \n\t"
-        "movq   (%1, %3), %%mm2         \n\t"
-        "movq   1(%1, %3), %%mm3        \n\t"
-        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
-        "movq   %%mm4, (%2)             \n\t"
-        "movq   %%mm5, (%2, %3)         \n\t"
-        "movq   8(%1), %%mm0            \n\t"
-        "movq   9(%1), %%mm1            \n\t"
-        "movq   8(%1, %3), %%mm2        \n\t"
-        "movq   9(%1, %3), %%mm3        \n\t"
-        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
-        "movq   %%mm4, 8(%2)            \n\t"
-        "movq   %%mm5, 8(%2, %3)        \n\t"
-        "add    %%"REG_a", %1           \n\t"
-        "add    %%"REG_a", %2           \n\t"
-        "subl   $4, %0                  \n\t"
-        "jnz    1b                      \n\t"
-        :"+g"(h), "+S"(pixels), "+D"(block)
-        :"r"((x86_reg)line_size)
-        :REG_a, "memory");
-}
-
-static void av_unused DEF(put, pixels16_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
-{
-    MOVQ_BFE(mm6);
-    __asm__ volatile(
-        "testl $1, %0                   \n\t"
-        " jz 1f                         \n\t"
-        "movq   (%1), %%mm0             \n\t"
-        "movq   (%2), %%mm1             \n\t"
-        "movq   8(%1), %%mm2            \n\t"
-        "movq   8(%2), %%mm3            \n\t"
-        "add    %4, %1                  \n\t"
-        "add    $16, %2                 \n\t"
-        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
-        "movq   %%mm4, (%3)             \n\t"
-        "movq   %%mm5, 8(%3)            \n\t"
-        "add    %5, %3                  \n\t"
-        "decl   %0                      \n\t"
-        ".p2align 3                     \n\t"
-        "1:                             \n\t"
-        "movq   (%1), %%mm0             \n\t"
-        "movq   (%2), %%mm1             \n\t"
-        "movq   8(%1), %%mm2            \n\t"
-        "movq   8(%2), %%mm3            \n\t"
-        "add    %4, %1                  \n\t"
-        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
-        "movq   %%mm4, (%3)             \n\t"
-        "movq   %%mm5, 8(%3)            \n\t"
-        "add    %5, %3                  \n\t"
-        "movq   (%1), %%mm0             \n\t"
-        "movq   16(%2), %%mm1           \n\t"
-        "movq   8(%1), %%mm2            \n\t"
-        "movq   24(%2), %%mm3           \n\t"
-        "add    %4, %1                  \n\t"
-        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
-        "movq   %%mm4, (%3)             \n\t"
-        "movq   %%mm5, 8(%3)            \n\t"
-        "add    %5, %3                  \n\t"
-        "add    $32, %2                 \n\t"
-        "subl   $2, %0                  \n\t"
-        "jnz    1b                      \n\t"
-#if !HAVE_EBX_AVAILABLE  //Note "+bm" and "+mb" are buggy too (with gcc 3.2.2 at least) and cannot be used
-        :"+m"(h), "+a"(src1), "+c"(src2), "+d"(dst)
-#else
-        :"+b"(h), "+a"(src1), "+c"(src2), "+d"(dst)
-#endif
-        :"S"((x86_reg)src1Stride), "D"((x86_reg)dstStride)
-        :"memory");
-}
-
-static void DEF(put, pixels8_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    MOVQ_BFE(mm6);
-    __asm__ volatile(
-        "lea (%3, %3), %%"REG_a"        \n\t"
-        "movq (%1), %%mm0               \n\t"
-        ".p2align 3                     \n\t"
-        "1:                             \n\t"
-        "movq   (%1, %3), %%mm1         \n\t"
-        "movq   (%1, %%"REG_a"),%%mm2   \n\t"
-        PAVGBP(%%mm1, %%mm0, %%mm4,   %%mm2, %%mm1, %%mm5)
-        "movq   %%mm4, (%2)             \n\t"
-        "movq   %%mm5, (%2, %3)         \n\t"
-        "add    %%"REG_a", %1           \n\t"
-        "add    %%"REG_a", %2           \n\t"
-        "movq   (%1, %3), %%mm1         \n\t"
-        "movq   (%1, %%"REG_a"),%%mm0   \n\t"
-        PAVGBP(%%mm1, %%mm2, %%mm4,   %%mm0, %%mm1, %%mm5)
-        "movq   %%mm4, (%2)             \n\t"
-        "movq   %%mm5, (%2, %3)         \n\t"
-        "add    %%"REG_a", %1           \n\t"
-        "add    %%"REG_a", %2           \n\t"
-        "subl   $4, %0                  \n\t"
-        "jnz    1b                      \n\t"
-        :"+g"(h), "+S"(pixels), "+D"(block)
-        :"r"((x86_reg)line_size)
-        :REG_a, "memory");
-}
-
 static void DEF(put, pixels8_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
 {
     MOVQ_ZERO(mm7);
@@ -298,25 +92,6 @@
 }
 
 // avg_pixels
-static void av_unused DEF(avg, pixels4)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    MOVQ_BFE(mm6);
-    JUMPALIGN();
-    do {
-        __asm__ volatile(
-             "movd  %0, %%mm0           \n\t"
-             "movd  %1, %%mm1           \n\t"
-             OP_AVG(%%mm0, %%mm1, %%mm2, %%mm6)
-             "movd  %%mm2, %0           \n\t"
-             :"+m"(*block)
-             :"m"(*pixels)
-             :"memory");
-        pixels += line_size;
-        block += line_size;
-    }
-    while (--h);
-}
-
 #ifndef NO_RND
 // in case more speed is needed - unroling would certainly help
 static void DEF(avg, pixels8)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
@@ -337,7 +112,7 @@
     }
     while (--h);
 }
-#endif // NO_RND
+#endif /* NO_RND */
 
 static void DEF(avg, pixels16)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
 {
@@ -362,141 +137,6 @@
     while (--h);
 }
 
-#ifndef NO_RND
-static void DEF(avg, pixels8_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    MOVQ_BFE(mm6);
-    JUMPALIGN();
-    do {
-        __asm__ volatile(
-            "movq  %1, %%mm0            \n\t"
-            "movq  1%1, %%mm1           \n\t"
-            "movq  %0, %%mm3            \n\t"
-            PAVGB(%%mm0, %%mm1, %%mm2, %%mm6)
-            OP_AVG(%%mm3, %%mm2, %%mm0, %%mm6)
-            "movq  %%mm0, %0            \n\t"
-            :"+m"(*block)
-            :"m"(*pixels)
-            :"memory");
-        pixels += line_size;
-        block += line_size;
-    } while (--h);
-}
-#endif // NO_RND
-
-static av_unused void DEF(avg, pixels8_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
-{
-    MOVQ_BFE(mm6);
-    JUMPALIGN();
-    do {
-        __asm__ volatile(
-            "movq  %1, %%mm0            \n\t"
-            "movq  %2, %%mm1            \n\t"
-            "movq  %0, %%mm3            \n\t"
-            PAVGB(%%mm0, %%mm1, %%mm2, %%mm6)
-            OP_AVG(%%mm3, %%mm2, %%mm0, %%mm6)
-            "movq  %%mm0, %0            \n\t"
-            :"+m"(*dst)
-            :"m"(*src1), "m"(*src2)
-            :"memory");
-        dst += dstStride;
-        src1 += src1Stride;
-        src2 += 8;
-    } while (--h);
-}
-
-static void DEF(avg, pixels16_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    MOVQ_BFE(mm6);
-    JUMPALIGN();
-    do {
-        __asm__ volatile(
-            "movq  %1, %%mm0            \n\t"
-            "movq  1%1, %%mm1           \n\t"
-            "movq  %0, %%mm3            \n\t"
-            PAVGB(%%mm0, %%mm1, %%mm2, %%mm6)
-            OP_AVG(%%mm3, %%mm2, %%mm0, %%mm6)
-            "movq  %%mm0, %0            \n\t"
-            "movq  8%1, %%mm0           \n\t"
-            "movq  9%1, %%mm1           \n\t"
-            "movq  8%0, %%mm3           \n\t"
-            PAVGB(%%mm0, %%mm1, %%mm2, %%mm6)
-            OP_AVG(%%mm3, %%mm2, %%mm0, %%mm6)
-            "movq  %%mm0, 8%0           \n\t"
-            :"+m"(*block)
-            :"m"(*pixels)
-            :"memory");
-        pixels += line_size;
-        block += line_size;
-    } while (--h);
-}
-
-static av_unused void DEF(avg, pixels16_l2)(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
-{
-    MOVQ_BFE(mm6);
-    JUMPALIGN();
-    do {
-        __asm__ volatile(
-            "movq  %1, %%mm0            \n\t"
-            "movq  %2, %%mm1            \n\t"
-            "movq  %0, %%mm3            \n\t"
-            PAVGB(%%mm0, %%mm1, %%mm2, %%mm6)
-            OP_AVG(%%mm3, %%mm2, %%mm0, %%mm6)
-            "movq  %%mm0, %0            \n\t"
-            "movq  8%1, %%mm0           \n\t"
-            "movq  8%2, %%mm1           \n\t"
-            "movq  8%0, %%mm3           \n\t"
-            PAVGB(%%mm0, %%mm1, %%mm2, %%mm6)
-            OP_AVG(%%mm3, %%mm2, %%mm0, %%mm6)
-            "movq  %%mm0, 8%0           \n\t"
-            :"+m"(*dst)
-            :"m"(*src1), "m"(*src2)
-            :"memory");
-        dst += dstStride;
-        src1 += src1Stride;
-        src2 += 16;
-    } while (--h);
-}
-
-static void DEF(avg, pixels8_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-{
-    MOVQ_BFE(mm6);
-    __asm__ volatile(
-        "lea    (%3, %3), %%"REG_a"     \n\t"
-        "movq   (%1), %%mm0             \n\t"
-        ".p2align 3                     \n\t"
-        "1:                             \n\t"
-        "movq   (%1, %3), %%mm1         \n\t"
-        "movq   (%1, %%"REG_a"), %%mm2  \n\t"
-        PAVGBP(%%mm1, %%mm0, %%mm4,   %%mm2, %%mm1, %%mm5)
-        "movq   (%2), %%mm3             \n\t"
-        OP_AVG(%%mm3, %%mm4, %%mm0, %%mm6)
-        "movq   (%2, %3), %%mm3         \n\t"
-        OP_AVG(%%mm3, %%mm5, %%mm1, %%mm6)
-        "movq   %%mm0, (%2)             \n\t"
-        "movq   %%mm1, (%2, %3)         \n\t"
-        "add    %%"REG_a", %1           \n\t"
-        "add    %%"REG_a", %2           \n\t"
-
-        "movq   (%1, %3), %%mm1         \n\t"
-        "movq   (%1, %%"REG_a"), %%mm0  \n\t"
-        PAVGBP(%%mm1, %%mm2, %%mm4,   %%mm0, %%mm1, %%mm5)
-        "movq   (%2), %%mm3             \n\t"
-        OP_AVG(%%mm3, %%mm4, %%mm2, %%mm6)
-        "movq   (%2, %3), %%mm3         \n\t"
-        OP_AVG(%%mm3, %%mm5, %%mm1, %%mm6)
-        "movq   %%mm2, (%2)             \n\t"
-        "movq   %%mm1, (%2, %3)         \n\t"
-        "add    %%"REG_a", %1           \n\t"
-        "add    %%"REG_a", %2           \n\t"
-
-        "subl   $4, %0                  \n\t"
-        "jnz    1b                      \n\t"
-        :"+g"(h), "+S"(pixels), "+D"(block)
-        :"r"((x86_reg)line_size)
-        :REG_a, "memory");
-}
-
 // this routine is 'slightly' suboptimal but mostly unused
 static void DEF(avg, pixels8_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
 {
@@ -573,21 +213,11 @@
 }
 
 //FIXME optimize
-static void DEF(put, pixels16_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){
-    DEF(put, pixels8_y2)(block  , pixels  , line_size, h);
-    DEF(put, pixels8_y2)(block+8, pixels+8, line_size, h);
-}
-
 static void DEF(put, pixels16_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){
     DEF(put, pixels8_xy2)(block  , pixels  , line_size, h);
     DEF(put, pixels8_xy2)(block+8, pixels+8, line_size, h);
 }
 
-static void DEF(avg, pixels16_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){
-    DEF(avg, pixels8_y2)(block  , pixels  , line_size, h);
-    DEF(avg, pixels8_y2)(block+8, pixels+8, line_size, h);
-}
-
 static void DEF(avg, pixels16_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){
     DEF(avg, pixels8_xy2)(block  , pixels  , line_size, h);
     DEF(avg, pixels8_xy2)(block+8, pixels+8, line_size, h);
diff --git a/libavcodec/x86/dsputilenc_mmx.c b/libavcodec/x86/dsputilenc_mmx.c
index a3f268e..49e3019 100644
--- a/libavcodec/x86/dsputilenc_mmx.c
+++ b/libavcodec/x86/dsputilenc_mmx.c
@@ -3,6 +3,8 @@
  * Copyright (c) 2000, 2001 Fabrice Bellard
  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  *
+ * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
+ *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
@@ -18,8 +20,6 @@
  * 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
- *
- * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
  */
 
 #include "libavutil/attributes.h"
@@ -947,9 +947,10 @@
 av_cold void ff_dsputilenc_init_mmx(DSPContext *c, AVCodecContext *avctx)
 {
     int mm_flags = av_get_cpu_flags();
-    int bit_depth = avctx->bits_per_raw_sample;
 
 #if HAVE_YASM
+    int bit_depth = avctx->bits_per_raw_sample;
+
     if (EXTERNAL_MMX(mm_flags)) {
         if (bit_depth <= 8)
             c->get_pixels = ff_get_pixels_mmx;
diff --git a/libavcodec/x86/fmtconvert_init.c b/libavcodec/x86/fmtconvert_init.c
index 4a4c017..91a4cb7 100644
--- a/libavcodec/x86/fmtconvert_init.c
+++ b/libavcodec/x86/fmtconvert_init.c
@@ -3,6 +3,8 @@
  * Copyright (c) 2000, 2001 Fabrice Bellard
  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  *
+ * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
+ *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
@@ -18,8 +20,6 @@
  * 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
- *
- * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
  */
 
 #include "libavutil/attributes.h"
diff --git a/libavcodec/x86/fpel.asm b/libavcodec/x86/fpel.asm
new file mode 100644
index 0000000..dc363d1
--- /dev/null
+++ b/libavcodec/x86/fpel.asm
@@ -0,0 +1,106 @@
+;******************************************************************************
+;* MMX optimized DSP utils
+;* Copyright (c) 2008 Loren Merritt
+;* Copyright (c) 2003-2013 Michael Niedermayer
+;* Copyright (c) 2013 Daniel Kang
+;*
+;* 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 "libavutil/x86/x86util.asm"
+
+SECTION .text
+
+INIT_MMX mmxext
+; void pixels(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+%macro PIXELS48 2
+%if %2 == 4
+%define OP movh
+%else
+%define OP mova
+%endif
+cglobal %1_pixels%2, 4,5
+    movsxdifnidn r2, r2d
+    lea          r4, [r2*3]
+.loop:
+    OP           m0, [r1]
+    OP           m1, [r1+r2]
+    OP           m2, [r1+r2*2]
+    OP           m3, [r1+r4]
+    lea          r1, [r1+r2*4]
+%ifidn %1, avg
+    pavgb        m0, [r0]
+    pavgb        m1, [r0+r2]
+    pavgb        m2, [r0+r2*2]
+    pavgb        m3, [r0+r4]
+%endif
+    OP         [r0], m0
+    OP      [r0+r2], m1
+    OP    [r0+r2*2], m2
+    OP      [r0+r4], m3
+    sub         r3d, 4
+    lea          r0, [r0+r2*4]
+    jne       .loop
+    RET
+%endmacro
+
+PIXELS48 put, 4
+PIXELS48 avg, 4
+PIXELS48 put, 8
+PIXELS48 avg, 8
+
+
+INIT_XMM sse2
+; void put_pixels16_sse2(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+cglobal put_pixels16, 4,5,4
+    lea          r4, [r2*3]
+.loop:
+    movu         m0, [r1]
+    movu         m1, [r1+r2]
+    movu         m2, [r1+r2*2]
+    movu         m3, [r1+r4]
+    lea          r1, [r1+r2*4]
+    mova       [r0], m0
+    mova    [r0+r2], m1
+    mova  [r0+r2*2], m2
+    mova    [r0+r4], m3
+    sub         r3d, 4
+    lea          r0, [r0+r2*4]
+    jnz       .loop
+    REP_RET
+
+; void avg_pixels16_sse2(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+cglobal avg_pixels16, 4,5,4
+    lea          r4, [r2*3]
+.loop:
+    movu         m0, [r1]
+    movu         m1, [r1+r2]
+    movu         m2, [r1+r2*2]
+    movu         m3, [r1+r4]
+    lea          r1, [r1+r2*4]
+    pavgb        m0, [r0]
+    pavgb        m1, [r0+r2]
+    pavgb        m2, [r0+r2*2]
+    pavgb        m3, [r0+r4]
+    mova       [r0], m0
+    mova    [r0+r2], m1
+    mova  [r0+r2*2], m2
+    mova    [r0+r4], m3
+    sub         r3d, 4
+    lea          r0, [r0+r2*4]
+    jnz       .loop
+    REP_RET
diff --git a/libavcodec/x86/h264_chromamc.asm b/libavcodec/x86/h264_chromamc.asm
index 867ba49..32681aa 100644
--- a/libavcodec/x86/h264_chromamc.asm
+++ b/libavcodec/x86/h264_chromamc.asm
@@ -60,7 +60,7 @@
 cextern pw_3
 cextern pw_4
 cextern pw_8
-cextern pw_28
+pw_28: times 8 dw 28
 cextern pw_32
 cextern pw_64
 
diff --git a/libavcodec/x86/h264_deblock.asm b/libavcodec/x86/h264_deblock.asm
index 5f2374d..d58e16c 100644
--- a/libavcodec/x86/h264_deblock.asm
+++ b/libavcodec/x86/h264_deblock.asm
@@ -28,6 +28,7 @@
 
 SECTION_RODATA
 
+pb_A1: times 16 db 0xA1
 pb_3_1: times 4 db 3, 1
 
 SECTION .text
@@ -35,7 +36,6 @@
 cextern pb_0
 cextern pb_1
 cextern pb_3
-cextern pb_A1
 
 ; expands to [base],...,[base+7*stride]
 %define PASS8ROWS(base, base3, stride, stride3) \
diff --git a/libavcodec/x86/h264_idct.asm b/libavcodec/x86/h264_idct.asm
index 7bb1653..17bf794 100644
--- a/libavcodec/x86/h264_idct.asm
+++ b/libavcodec/x86/h264_idct.asm
@@ -312,7 +312,7 @@
 %if ARCH_X86_64
 cglobal h264_idct_dc_add_8, 3, 4, 0
     movsx        r3, word [r1]
-    mov   word [r1], 0
+    mov  dword [r1], 0
     DC_ADD_MMXEXT_INIT r3, r2
     DC_ADD_MMXEXT_OP movh, r0, r2, r3
     RET
@@ -320,7 +320,7 @@
 ; ff_h264_idct8_dc_add_mmxext(uint8_t *dst, int16_t *block, int stride)
 cglobal h264_idct8_dc_add_8, 3, 4, 0
     movsx        r3, word [r1]
-    mov   word [r1], 0
+    mov  dword [r1], 0
     DC_ADD_MMXEXT_INIT r3, r2
     DC_ADD_MMXEXT_OP mova, r0, r2, r3
     lea          r0, [r0+r2*4]
@@ -329,7 +329,7 @@
 %else
 cglobal h264_idct_dc_add_8, 2, 3, 0
     movsx        r2, word [r1]
-    mov   word [r1], 0
+    mov  dword [r1], 0
     mov          r1, r2m
     DC_ADD_MMXEXT_INIT r2, r1
     DC_ADD_MMXEXT_OP movh, r0, r1, r2
@@ -338,7 +338,7 @@
 ; ff_h264_idct8_dc_add_mmxext(uint8_t *dst, int16_t *block, int stride)
 cglobal h264_idct8_dc_add_8, 2, 3, 0
     movsx        r2, word [r1]
-    mov   word [r1], 0
+    mov  dword [r1], 0
     mov          r1, r2m
     DC_ADD_MMXEXT_INIT r2, r1
     DC_ADD_MMXEXT_OP mova, r0, r1, r2
diff --git a/libavcodec/x86/h264_qpel.c b/libavcodec/x86/h264_qpel.c
index c1e7fb7..85f1420 100644
--- a/libavcodec/x86/h264_qpel.c
+++ b/libavcodec/x86/h264_qpel.c
@@ -28,17 +28,18 @@
 #include "dsputil_mmx.h"
 
 #if HAVE_YASM
-void ff_put_pixels4_mmxext(uint8_t *block, const uint8_t *pixels, int line_size, int h);
-void ff_avg_pixels4_mmxext(uint8_t *block, const uint8_t *pixels, int line_size, int h);
-void ff_put_pixels8_mmxext(uint8_t *block, const uint8_t *pixels, int line_size, int h);
+void ff_put_pixels4_mmxext(uint8_t *block, const uint8_t *pixels,
+                           ptrdiff_t line_size, int h);
+void ff_avg_pixels4_mmxext(uint8_t *block, const uint8_t *pixels,
+                           ptrdiff_t line_size, int h);
 static void ff_put_pixels16_mmxext(uint8_t *block, const uint8_t *pixels,
-                                   int line_size, int h)
+                                   ptrdiff_t line_size, int h)
 {
     ff_put_pixels8_mmxext(block,     pixels,     line_size, h);
     ff_put_pixels8_mmxext(block + 8, pixels + 8, line_size, h);
 }
 static void ff_avg_pixels16_mmxext(uint8_t *block, const uint8_t *pixels,
-                                   int line_size, int h)
+                                   ptrdiff_t line_size, int h)
 {
     ff_avg_pixels8_mmxext(block,     pixels,     line_size, h);
     ff_avg_pixels8_mmxext(block + 8, pixels + 8, line_size, h);
@@ -55,10 +56,6 @@
                                int dstStride, int src1Stride, int h);
 void ff_avg_pixels16_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2,
                                int dstStride, int src1Stride, int h);
-void ff_put_pixels16_sse2(uint8_t *block, const uint8_t *pixels,
-                          int line_size, int h);
-void ff_avg_pixels16_sse2(uint8_t *block, const uint8_t *pixels,
-                          int line_size, int h);
 #define ff_put_pixels8_l2_sse2  ff_put_pixels8_l2_mmxext
 #define ff_avg_pixels8_l2_sse2  ff_avg_pixels8_l2_mmxext
 #define ff_put_pixels16_l2_sse2 ff_put_pixels16_l2_mmxext
@@ -250,112 +247,132 @@
 H264_MC_H(OPNAME, SIZE, MMX, ALIGN)\
 H264_MC_HV(OPNAME, SIZE, MMX, ALIGN)\
 
-static void put_h264_qpel16_mc00_sse2 (uint8_t *dst, uint8_t *src, int stride){
+static void put_h264_qpel16_mc00_sse2 (uint8_t *dst, uint8_t *src,
+                                       ptrdiff_t stride)
+{
     ff_put_pixels16_sse2(dst, src, stride, 16);
 }
-static void avg_h264_qpel16_mc00_sse2 (uint8_t *dst, uint8_t *src, int stride){
+static void avg_h264_qpel16_mc00_sse2 (uint8_t *dst, uint8_t *src,
+                                       ptrdiff_t stride)
+{
     ff_avg_pixels16_sse2(dst, src, stride, 16);
 }
 #define put_h264_qpel8_mc00_sse2 put_h264_qpel8_mc00_mmxext
 #define avg_h264_qpel8_mc00_sse2 avg_h264_qpel8_mc00_mmxext
 
 #define H264_MC_C(OPNAME, SIZE, MMX, ALIGN) \
-static void OPNAME ## h264_qpel ## SIZE ## _mc00_ ## MMX (uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc00_ ## MMX (uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     ff_ ## OPNAME ## pixels ## SIZE ## _ ## MMX(dst, src, stride, SIZE);\
 }\
 
 #define H264_MC_H(OPNAME, SIZE, MMX, ALIGN) \
-static void OPNAME ## h264_qpel ## SIZE ## _mc10_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc10_ ## MMX(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     ff_ ## OPNAME ## h264_qpel ## SIZE ## _h_lowpass_l2_ ## MMX(dst, src, src, stride, stride);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc20_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc20_ ## MMX(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     ff_ ## OPNAME ## h264_qpel ## SIZE ## _h_lowpass_ ## MMX(dst, src, stride, stride);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc30_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc30_ ## MMX(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     ff_ ## OPNAME ## h264_qpel ## SIZE ## _h_lowpass_l2_ ## MMX(dst, src, src+1, stride, stride);\
 }\
 
 #define H264_MC_V(OPNAME, SIZE, MMX, ALIGN) \
-static void OPNAME ## h264_qpel ## SIZE ## _mc01_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc01_ ## MMX(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(ALIGN, uint8_t, temp)[SIZE*SIZE];\
     ff_put_h264_qpel ## SIZE ## _v_lowpass_ ## MMX(temp, src, SIZE, stride);\
     ff_ ## OPNAME ## pixels ## SIZE ## _l2_ ## MMX(dst, src, temp, stride, stride, SIZE);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc02_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc02_ ## MMX(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     ff_ ## OPNAME ## h264_qpel ## SIZE ## _v_lowpass_ ## MMX(dst, src, stride, stride);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc03_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc03_ ## MMX(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(ALIGN, uint8_t, temp)[SIZE*SIZE];\
     ff_put_h264_qpel ## SIZE ## _v_lowpass_ ## MMX(temp, src, SIZE, stride);\
     ff_ ## OPNAME ## pixels ## SIZE ## _l2_ ## MMX(dst, src+stride, temp, stride, stride, SIZE);\
 }\
 
 #define H264_MC_HV(OPNAME, SIZE, MMX, ALIGN) \
-static void OPNAME ## h264_qpel ## SIZE ## _mc11_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc11_ ## MMX(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(ALIGN, uint8_t, temp)[SIZE*SIZE];\
     ff_put_h264_qpel ## SIZE ## _v_lowpass_ ## MMX(temp, src, SIZE, stride);\
     ff_ ## OPNAME ## h264_qpel ## SIZE ## _h_lowpass_l2_ ## MMX(dst, src, temp, stride, SIZE);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc31_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc31_ ## MMX(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(ALIGN, uint8_t, temp)[SIZE*SIZE];\
     ff_put_h264_qpel ## SIZE ## _v_lowpass_ ## MMX(temp, src+1, SIZE, stride);\
     ff_ ## OPNAME ## h264_qpel ## SIZE ## _h_lowpass_l2_ ## MMX(dst, src, temp, stride, SIZE);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc13_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc13_ ## MMX(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(ALIGN, uint8_t, temp)[SIZE*SIZE];\
     ff_put_h264_qpel ## SIZE ## _v_lowpass_ ## MMX(temp, src, SIZE, stride);\
     ff_ ## OPNAME ## h264_qpel ## SIZE ## _h_lowpass_l2_ ## MMX(dst, src+stride, temp, stride, SIZE);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc33_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc33_ ## MMX(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(ALIGN, uint8_t, temp)[SIZE*SIZE];\
     ff_put_h264_qpel ## SIZE ## _v_lowpass_ ## MMX(temp, src+1, SIZE, stride);\
     ff_ ## OPNAME ## h264_qpel ## SIZE ## _h_lowpass_l2_ ## MMX(dst, src+stride, temp, stride, SIZE);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc22_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc22_ ## MMX(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(ALIGN, uint16_t, temp)[SIZE*(SIZE<8?12:24)];\
     ff_ ## OPNAME ## h264_qpel ## SIZE ## _hv_lowpass_ ## MMX(dst, temp, src, stride, SIZE, stride);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc21_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc21_ ## MMX(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(ALIGN, uint8_t, temp)[SIZE*(SIZE<8?12:24)*2 + SIZE*SIZE];\
     uint8_t * const halfHV= temp;\
     int16_t * const halfV= (int16_t*)(temp + SIZE*SIZE);\
-    assert(((int)temp & 7) == 0);\
+    av_assert2(((int)temp & 7) == 0);\
     ff_put_h264_qpel ## SIZE ## _hv_lowpass_ ## MMX(halfHV, halfV, src, SIZE, SIZE, stride);\
     ff_ ## OPNAME ## h264_qpel ## SIZE ## _h_lowpass_l2_ ## MMX(dst, src, halfHV, stride, SIZE);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc23_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc23_ ## MMX(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(ALIGN, uint8_t, temp)[SIZE*(SIZE<8?12:24)*2 + SIZE*SIZE];\
     uint8_t * const halfHV= temp;\
     int16_t * const halfV= (int16_t*)(temp + SIZE*SIZE);\
-    assert(((int)temp & 7) == 0);\
+    av_assert2(((int)temp & 7) == 0);\
     ff_put_h264_qpel ## SIZE ## _hv_lowpass_ ## MMX(halfHV, halfV, src, SIZE, SIZE, stride);\
     ff_ ## OPNAME ## h264_qpel ## SIZE ## _h_lowpass_l2_ ## MMX(dst, src+stride, halfHV, stride, SIZE);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc12_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc12_ ## MMX(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(ALIGN, uint8_t, temp)[SIZE*(SIZE<8?12:24)*2 + SIZE*SIZE];\
     uint8_t * const halfHV= temp;\
     int16_t * const halfV= (int16_t*)(temp + SIZE*SIZE);\
-    assert(((int)temp & 7) == 0);\
+    av_assert2(((int)temp & 7) == 0);\
     ff_put_h264_qpel ## SIZE ## _hv_lowpass_ ## MMX(halfHV, halfV, src, SIZE, SIZE, stride);\
     ff_ ## OPNAME ## pixels ## SIZE ## _l2_shift5_mmxext(dst, halfV+2, halfHV, stride, SIZE, SIZE);\
 }\
 \
-static void OPNAME ## h264_qpel ## SIZE ## _mc32_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
+static void OPNAME ## h264_qpel ## SIZE ## _mc32_ ## MMX(uint8_t *dst, uint8_t *src, ptrdiff_t stride)\
+{\
     DECLARE_ALIGNED(ALIGN, uint8_t, temp)[SIZE*(SIZE<8?12:24)*2 + SIZE*SIZE];\
     uint8_t * const halfHV= temp;\
     int16_t * const halfV= (int16_t*)(temp + SIZE*SIZE);\
-    assert(((int)temp & 7) == 0);\
+    av_assert2(((int)temp & 7) == 0);\
     ff_put_h264_qpel ## SIZE ## _hv_lowpass_ ## MMX(halfHV, halfV, src, SIZE, SIZE, stride);\
     ff_ ## OPNAME ## pixels ## SIZE ## _l2_shift5_mmxext(dst, halfV+3, halfHV, stride, SIZE, SIZE);\
 }\
@@ -374,8 +391,6 @@
 QPEL(avg_, 8, XMM, 16)\
 QPEL(avg_, 16,XMM, 16)\
 
-#undef PAVGB
-#define PAVGB "pavgb"
 QPEL_H264(put_,        PUT_OP, mmxext)
 QPEL_H264(avg_, AVG_MMXEXT_OP, mmxext)
 QPEL_H264_V_XMM(put_,       PUT_OP, sse2)
@@ -386,7 +401,6 @@
 QPEL_H264_H_XMM(avg_,AVG_MMXEXT_OP, ssse3)
 QPEL_H264_HV_XMM(put_,       PUT_OP, ssse3)
 QPEL_H264_HV_XMM(avg_,AVG_MMXEXT_OP, ssse3)
-#undef PAVGB
 
 H264_MC_4816(mmxext)
 H264_MC_816(H264_MC_V, sse2)
@@ -398,7 +412,7 @@
 //10bit
 #define LUMA_MC_OP(OP, NUM, DEPTH, TYPE, OPT) \
 void ff_ ## OP ## _h264_qpel ## NUM ## _ ## TYPE ## _ ## DEPTH ## _ ## OPT \
-    (uint8_t *dst, uint8_t *src, int stride);
+    (uint8_t *dst, uint8_t *src, ptrdiff_t stride);
 
 #define LUMA_MC_ALL(DEPTH, TYPE, OPT) \
     LUMA_MC_OP(put,  4, DEPTH, TYPE, OPT) \
@@ -455,7 +469,7 @@
 LUMA_MC_816(10, mc33, sse2)
 
 #define QPEL16_OPMC(OP, MC, MMX)\
-void ff_ ## OP ## _h264_qpel16_ ## MC ## _10_ ## MMX(uint8_t *dst, uint8_t *src, int stride){\
+void ff_ ## OP ## _h264_qpel16_ ## MC ## _10_ ## MMX(uint8_t *dst, uint8_t *src, ptrdiff_t stride){\
     ff_ ## OP ## _h264_qpel8_ ## MC ## _10_ ## MMX(dst   , src   , stride);\
     ff_ ## OP ## _h264_qpel8_ ## MC ## _10_ ## MMX(dst+16, src+16, stride);\
     src += 8*stride;\
diff --git a/libavcodec/x86/hpeldsp.asm b/libavcodec/x86/hpeldsp.asm
index c83c388..1a572a3 100644
--- a/libavcodec/x86/hpeldsp.asm
+++ b/libavcodec/x86/hpeldsp.asm
@@ -459,44 +459,3 @@
 AVG_PIXELS8_XY2
 INIT_MMX 3dnow
 AVG_PIXELS8_XY2
-
-INIT_XMM sse2
-; void put_pixels16_sse2(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-cglobal put_pixels16, 4,5,4
-    lea          r4, [r2*3]
-.loop:
-    movu         m0, [r1]
-    movu         m1, [r1+r2]
-    movu         m2, [r1+r2*2]
-    movu         m3, [r1+r4]
-    lea          r1, [r1+r2*4]
-    mova       [r0], m0
-    mova    [r0+r2], m1
-    mova  [r0+r2*2], m2
-    mova    [r0+r4], m3
-    sub         r3d, 4
-    lea          r0, [r0+r2*4]
-    jnz       .loop
-    REP_RET
-
-; void avg_pixels16_sse2(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
-cglobal avg_pixels16, 4,5,4
-    lea          r4, [r2*3]
-.loop:
-    movu         m0, [r1]
-    movu         m1, [r1+r2]
-    movu         m2, [r1+r2*2]
-    movu         m3, [r1+r4]
-    lea          r1, [r1+r2*4]
-    pavgb        m0, [r0]
-    pavgb        m1, [r0+r2]
-    pavgb        m2, [r0+r2*2]
-    pavgb        m3, [r0+r4]
-    mova       [r0], m0
-    mova    [r0+r2], m1
-    mova  [r0+r2*2], m2
-    mova    [r0+r4], m3
-    sub         r3d, 4
-    lea          r0, [r0+r2*4]
-    jnz       .loop
-    REP_RET
diff --git a/libavcodec/x86/dsputil_avg_template.c b/libavcodec/x86/hpeldsp_avg_template.c
similarity index 100%
rename from libavcodec/x86/dsputil_avg_template.c
rename to libavcodec/x86/hpeldsp_avg_template.c
diff --git a/libavcodec/x86/hpeldsp_init.c b/libavcodec/x86/hpeldsp_init.c
new file mode 100644
index 0000000..e313265
--- /dev/null
+++ b/libavcodec/x86/hpeldsp_init.c
@@ -0,0 +1,327 @@
+/*
+ * MMX optimized DSP utils
+ * 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
+ *
+ * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
+ */
+
+#include "libavutil/cpu.h"
+#include "libavutil/x86/asm.h"
+#include "libavcodec/hpeldsp.h"
+#include "dsputil_mmx.h"
+
+void ff_put_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
+                              ptrdiff_t line_size, int h);
+void ff_put_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels,
+                             ptrdiff_t line_size, int h);
+void ff_put_pixels16_x2_mmxext(uint8_t *block, const uint8_t *pixels,
+                               ptrdiff_t line_size, int h);
+void ff_put_pixels16_x2_3dnow(uint8_t *block, const uint8_t *pixels,
+                              ptrdiff_t line_size, int h);
+void ff_put_no_rnd_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
+                                     ptrdiff_t line_size, int h);
+void ff_put_no_rnd_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels,
+                                    ptrdiff_t line_size, int h);
+void ff_put_no_rnd_pixels8_x2_exact_mmxext(uint8_t *block,
+                                           const uint8_t *pixels,
+                                           ptrdiff_t line_size, int h);
+void ff_put_no_rnd_pixels8_x2_exact_3dnow(uint8_t *block,
+                                          const uint8_t *pixels,
+                                          ptrdiff_t line_size, int h);
+void ff_put_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
+                              ptrdiff_t line_size, int h);
+void ff_put_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels,
+                             ptrdiff_t line_size, int h);
+void ff_put_no_rnd_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
+                                     ptrdiff_t line_size, int h);
+void ff_put_no_rnd_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels,
+                                    ptrdiff_t line_size, int h);
+void ff_put_no_rnd_pixels8_y2_exact_mmxext(uint8_t *block,
+                                           const uint8_t *pixels,
+                                           ptrdiff_t line_size, int h);
+void ff_put_no_rnd_pixels8_y2_exact_3dnow(uint8_t *block,
+                                          const uint8_t *pixels,
+                                          ptrdiff_t line_size, int h);
+void ff_avg_pixels8_3dnow(uint8_t *block, const uint8_t *pixels,
+                          ptrdiff_t line_size, int h);
+void ff_avg_pixels8_x2_mmxext(uint8_t *block, const uint8_t *pixels,
+                              ptrdiff_t line_size, int h);
+void ff_avg_pixels8_x2_3dnow(uint8_t *block, const uint8_t *pixels,
+                             ptrdiff_t line_size, int h);
+void ff_avg_pixels8_y2_mmxext(uint8_t *block, const uint8_t *pixels,
+                              ptrdiff_t line_size, int h);
+void ff_avg_pixels8_y2_3dnow(uint8_t *block, const uint8_t *pixels,
+                             ptrdiff_t line_size, int h);
+void ff_avg_pixels8_xy2_mmxext(uint8_t *block, const uint8_t *pixels,
+                               ptrdiff_t line_size, int h);
+void ff_avg_pixels8_xy2_3dnow(uint8_t *block, const uint8_t *pixels,
+                              ptrdiff_t line_size, int h);
+
+
+#if HAVE_INLINE_ASM
+
+/***********************************/
+/* MMX no rounding */
+#define NO_RND 1
+#define DEF(x, y) x ## _no_rnd_ ## y ## _mmx
+#define SET_RND  MOVQ_WONE
+#define PAVGBP(a, b, c, d, e, f)        PAVGBP_MMX_NO_RND(a, b, c, d, e, f)
+#define PAVGB(a, b, c, e)               PAVGB_MMX_NO_RND(a, b, c, e)
+#define OP_AVG(a, b, c, e)              PAVGB_MMX(a, b, c, e)
+
+#include "hpeldsp_rnd_template.c"
+
+#undef DEF
+#undef SET_RND
+#undef PAVGBP
+#undef PAVGB
+#undef NO_RND
+/***********************************/
+/* MMX rounding */
+
+#define DEF(x, y) x ## _ ## y ## _mmx
+#define SET_RND  MOVQ_WTWO
+#define PAVGBP(a, b, c, d, e, f)        PAVGBP_MMX(a, b, c, d, e, f)
+#define PAVGB(a, b, c, e)               PAVGB_MMX(a, b, c, e)
+
+#include "hpeldsp_rnd_template.c"
+
+#undef DEF
+#undef SET_RND
+#undef PAVGBP
+#undef PAVGB
+#undef OP_AVG
+
+#endif /* HAVE_INLINE_ASM */
+
+
+#if HAVE_YASM
+#define ff_put_pixels8_mmx ff_put_pixels8_mmxext
+
+/***********************************/
+/* 3Dnow specific */
+
+#define DEF(x) x ## _3dnow
+
+#include "hpeldsp_avg_template.c"
+
+#undef DEF
+
+/***********************************/
+/* MMXEXT specific */
+
+#define DEF(x) x ## _mmxext
+
+#include "hpeldsp_avg_template.c"
+
+#undef DEF
+
+#endif /* HAVE_YASM */
+
+
+#if HAVE_INLINE_ASM
+#define put_no_rnd_pixels16_mmx put_pixels16_mmx
+#define put_no_rnd_pixels8_mmx put_pixels8_mmx
+#define put_pixels16_mmxext put_pixels16_mmx
+#define put_pixels8_mmxext put_pixels8_mmx
+#define put_pixels4_mmxext put_pixels4_mmx
+#define put_no_rnd_pixels16_mmxext put_no_rnd_pixels16_mmx
+#define put_no_rnd_pixels8_mmxext put_no_rnd_pixels8_mmx
+
+static void put_pixels8_mmx(uint8_t *block, const uint8_t *pixels,
+                            ptrdiff_t line_size, int h)
+{
+    __asm__ volatile (
+        "lea   (%3, %3), %%"REG_a"      \n\t"
+        ".p2align     3                 \n\t"
+        "1:                             \n\t"
+        "movq  (%1    ), %%mm0          \n\t"
+        "movq  (%1, %3), %%mm1          \n\t"
+        "movq     %%mm0, (%2)           \n\t"
+        "movq     %%mm1, (%2, %3)       \n\t"
+        "add  %%"REG_a", %1             \n\t"
+        "add  %%"REG_a", %2             \n\t"
+        "movq  (%1    ), %%mm0          \n\t"
+        "movq  (%1, %3), %%mm1          \n\t"
+        "movq     %%mm0, (%2)           \n\t"
+        "movq     %%mm1, (%2, %3)       \n\t"
+        "add  %%"REG_a", %1             \n\t"
+        "add  %%"REG_a", %2             \n\t"
+        "subl        $4, %0             \n\t"
+        "jnz         1b                 \n\t"
+        : "+g"(h), "+r"(pixels),  "+r"(block)
+        : "r"((x86_reg)line_size)
+        : "%"REG_a, "memory"
+        );
+}
+
+static void put_pixels16_mmx(uint8_t *block, const uint8_t *pixels,
+                             ptrdiff_t line_size, int h)
+{
+    __asm__ volatile (
+        "lea   (%3, %3), %%"REG_a"      \n\t"
+        ".p2align     3                 \n\t"
+        "1:                             \n\t"
+        "movq  (%1    ), %%mm0          \n\t"
+        "movq 8(%1    ), %%mm4          \n\t"
+        "movq  (%1, %3), %%mm1          \n\t"
+        "movq 8(%1, %3), %%mm5          \n\t"
+        "movq     %%mm0,  (%2)          \n\t"
+        "movq     %%mm4, 8(%2)          \n\t"
+        "movq     %%mm1,  (%2, %3)      \n\t"
+        "movq     %%mm5, 8(%2, %3)      \n\t"
+        "add  %%"REG_a", %1             \n\t"
+        "add  %%"REG_a", %2             \n\t"
+        "movq  (%1    ), %%mm0          \n\t"
+        "movq 8(%1    ), %%mm4          \n\t"
+        "movq  (%1, %3), %%mm1          \n\t"
+        "movq 8(%1, %3), %%mm5          \n\t"
+        "movq     %%mm0,  (%2)          \n\t"
+        "movq     %%mm4, 8(%2)          \n\t"
+        "movq     %%mm1,  (%2, %3)      \n\t"
+        "movq     %%mm5, 8(%2, %3)      \n\t"
+        "add  %%"REG_a", %1             \n\t"
+        "add  %%"REG_a", %2             \n\t"
+        "subl        $4, %0             \n\t"
+        "jnz         1b                 \n\t"
+        : "+g"(h), "+r"(pixels),  "+r"(block)
+        : "r"((x86_reg)line_size)
+        : "%"REG_a, "memory"
+        );
+}
+#endif /* HAVE_INLINE_ASM */
+
+#define SET_HPEL_FUNCS(PFX, IDX, SIZE, CPU)                                     \
+    do {                                                                        \
+        c->PFX ## _pixels_tab IDX [0] = PFX ## _pixels ## SIZE ## _     ## CPU; \
+        c->PFX ## _pixels_tab IDX [1] = PFX ## _pixels ## SIZE ## _x2_  ## CPU; \
+        c->PFX ## _pixels_tab IDX [2] = PFX ## _pixels ## SIZE ## _y2_  ## CPU; \
+        c->PFX ## _pixels_tab IDX [3] = PFX ## _pixels ## SIZE ## _xy2_ ## CPU; \
+    } while (0)
+
+static void hpeldsp_init_mmx(HpelDSPContext *c, int flags, int mm_flags)
+{
+#if HAVE_INLINE_ASM
+    SET_HPEL_FUNCS(put,        [0], 16, mmx);
+    SET_HPEL_FUNCS(put_no_rnd, [0], 16, mmx);
+    SET_HPEL_FUNCS(avg,        [0], 16, mmx);
+    SET_HPEL_FUNCS(avg_no_rnd,    , 16, mmx);
+    SET_HPEL_FUNCS(put,        [1],  8, mmx);
+    SET_HPEL_FUNCS(put_no_rnd, [1],  8, mmx);
+    SET_HPEL_FUNCS(avg,        [1],  8, mmx);
+#endif /* HAVE_INLINE_ASM */
+}
+
+static void hpeldsp_init_mmxext(HpelDSPContext *c, int flags, int mm_flags)
+{
+#if HAVE_YASM
+    c->put_pixels_tab[0][1] = ff_put_pixels16_x2_mmxext;
+    c->put_pixels_tab[0][2] = ff_put_pixels16_y2_mmxext;
+
+    c->avg_pixels_tab[0][0] = ff_avg_pixels16_mmxext;
+    c->avg_pixels_tab[0][1] = ff_avg_pixels16_x2_mmxext;
+    c->avg_pixels_tab[0][2] = ff_avg_pixels16_y2_mmxext;
+
+    c->put_pixels_tab[1][1] = ff_put_pixels8_x2_mmxext;
+    c->put_pixels_tab[1][2] = ff_put_pixels8_y2_mmxext;
+
+    c->avg_pixels_tab[1][0] = ff_avg_pixels8_mmxext;
+    c->avg_pixels_tab[1][1] = ff_avg_pixels8_x2_mmxext;
+    c->avg_pixels_tab[1][2] = ff_avg_pixels8_y2_mmxext;
+
+    if (!(flags & CODEC_FLAG_BITEXACT)) {
+        c->put_no_rnd_pixels_tab[0][1] = ff_put_no_rnd_pixels16_x2_mmxext;
+        c->put_no_rnd_pixels_tab[0][2] = ff_put_no_rnd_pixels16_y2_mmxext;
+        c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_mmxext;
+        c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_mmxext;
+
+        c->avg_pixels_tab[0][3] = ff_avg_pixels16_xy2_mmxext;
+        c->avg_pixels_tab[1][3] = ff_avg_pixels8_xy2_mmxext;
+    }
+
+    if (flags & CODEC_FLAG_BITEXACT && CONFIG_VP3_DECODER) {
+        c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_exact_mmxext;
+        c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_exact_mmxext;
+    }
+#endif /* HAVE_YASM */
+}
+
+static void hpeldsp_init_3dnow(HpelDSPContext *c, int flags, int mm_flags)
+{
+#if HAVE_YASM
+    c->put_pixels_tab[0][1] = ff_put_pixels16_x2_3dnow;
+    c->put_pixels_tab[0][2] = ff_put_pixels16_y2_3dnow;
+
+    c->avg_pixels_tab[0][0] = ff_avg_pixels16_3dnow;
+    c->avg_pixels_tab[0][1] = ff_avg_pixels16_x2_3dnow;
+    c->avg_pixels_tab[0][2] = ff_avg_pixels16_y2_3dnow;
+
+    c->put_pixels_tab[1][1] = ff_put_pixels8_x2_3dnow;
+    c->put_pixels_tab[1][2] = ff_put_pixels8_y2_3dnow;
+
+    c->avg_pixels_tab[1][0] = ff_avg_pixels8_3dnow;
+    c->avg_pixels_tab[1][1] = ff_avg_pixels8_x2_3dnow;
+    c->avg_pixels_tab[1][2] = ff_avg_pixels8_y2_3dnow;
+
+    if (!(flags & CODEC_FLAG_BITEXACT)){
+        c->put_no_rnd_pixels_tab[0][1] = ff_put_no_rnd_pixels16_x2_3dnow;
+        c->put_no_rnd_pixels_tab[0][2] = ff_put_no_rnd_pixels16_y2_3dnow;
+        c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_3dnow;
+        c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_3dnow;
+
+        c->avg_pixels_tab[0][3] = ff_avg_pixels16_xy2_3dnow;
+        c->avg_pixels_tab[1][3] = ff_avg_pixels8_xy2_3dnow;
+    }
+
+    if (flags & CODEC_FLAG_BITEXACT && CONFIG_VP3_DECODER) {
+        c->put_no_rnd_pixels_tab[1][1] = ff_put_no_rnd_pixels8_x2_exact_3dnow;
+        c->put_no_rnd_pixels_tab[1][2] = ff_put_no_rnd_pixels8_y2_exact_3dnow;
+    }
+#endif /* HAVE_YASM */
+}
+
+static void hpeldsp_init_sse2(HpelDSPContext *c, int flags, int mm_flags)
+{
+#if HAVE_YASM
+    if (!(mm_flags & AV_CPU_FLAG_SSE2SLOW)) {
+        // these functions are slower than mmx on AMD, but faster on Intel
+        c->put_pixels_tab[0][0]        = ff_put_pixels16_sse2;
+        c->put_no_rnd_pixels_tab[0][0] = ff_put_pixels16_sse2;
+        c->avg_pixels_tab[0][0]        = ff_avg_pixels16_sse2;
+    }
+#endif /* HAVE_YASM */
+}
+
+void ff_hpeldsp_init_x86(HpelDSPContext *c, int flags)
+{
+    int mm_flags = av_get_cpu_flags();
+
+    if (HAVE_MMX && mm_flags & AV_CPU_FLAG_MMX)
+        hpeldsp_init_mmx(c, flags, mm_flags);
+
+    if (mm_flags & AV_CPU_FLAG_MMXEXT)
+        hpeldsp_init_mmxext(c, flags, mm_flags);
+
+    if (mm_flags & AV_CPU_FLAG_3DNOW)
+        hpeldsp_init_3dnow(c, flags, mm_flags);
+
+    if (mm_flags & AV_CPU_FLAG_SSE2)
+        hpeldsp_init_sse2(c, flags, mm_flags);
+}
diff --git a/libavcodec/x86/hpeldsp_rnd_template.c b/libavcodec/x86/hpeldsp_rnd_template.c
new file mode 100644
index 0000000..5b5866d
--- /dev/null
+++ b/libavcodec/x86/hpeldsp_rnd_template.c
@@ -0,0 +1,233 @@
+/*
+ * DSP utils mmx functions are compiled twice for rnd/no_rnd
+ * Copyright (c) 2000, 2001 Fabrice Bellard
+ * Copyright (c) 2003-2004 Michael Niedermayer <michaelni@gmx.at>
+ *
+ * MMX optimization by Nick Kurshev <nickols_k@mail.ru>
+ * mostly rewritten by Michael Niedermayer <michaelni@gmx.at>
+ * and improved by Zdenek Kabelac <kabi@users.sf.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
+ */
+
+#include "dsputil_rnd_template.c"
+
+// put_pixels
+static void DEF(put, pixels8_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    MOVQ_BFE(mm6);
+    __asm__ volatile(
+        "lea    (%3, %3), %%"REG_a"     \n\t"
+        ".p2align 3                     \n\t"
+        "1:                             \n\t"
+        "movq   (%1), %%mm0             \n\t"
+        "movq   1(%1), %%mm1            \n\t"
+        "movq   (%1, %3), %%mm2         \n\t"
+        "movq   1(%1, %3), %%mm3        \n\t"
+        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
+        "movq   %%mm4, (%2)             \n\t"
+        "movq   %%mm5, (%2, %3)         \n\t"
+        "add    %%"REG_a", %1           \n\t"
+        "add    %%"REG_a", %2           \n\t"
+        "movq   (%1), %%mm0             \n\t"
+        "movq   1(%1), %%mm1            \n\t"
+        "movq   (%1, %3), %%mm2         \n\t"
+        "movq   1(%1, %3), %%mm3        \n\t"
+        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
+        "movq   %%mm4, (%2)             \n\t"
+        "movq   %%mm5, (%2, %3)         \n\t"
+        "add    %%"REG_a", %1           \n\t"
+        "add    %%"REG_a", %2           \n\t"
+        "subl   $4, %0                  \n\t"
+        "jnz    1b                      \n\t"
+        :"+g"(h), "+S"(pixels), "+D"(block)
+        :"r"((x86_reg)line_size)
+        :REG_a, "memory");
+}
+
+static void DEF(put, pixels16_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    MOVQ_BFE(mm6);
+    __asm__ volatile(
+        "lea        (%3, %3), %%"REG_a" \n\t"
+        ".p2align 3                     \n\t"
+        "1:                             \n\t"
+        "movq   (%1), %%mm0             \n\t"
+        "movq   1(%1), %%mm1            \n\t"
+        "movq   (%1, %3), %%mm2         \n\t"
+        "movq   1(%1, %3), %%mm3        \n\t"
+        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
+        "movq   %%mm4, (%2)             \n\t"
+        "movq   %%mm5, (%2, %3)         \n\t"
+        "movq   8(%1), %%mm0            \n\t"
+        "movq   9(%1), %%mm1            \n\t"
+        "movq   8(%1, %3), %%mm2        \n\t"
+        "movq   9(%1, %3), %%mm3        \n\t"
+        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
+        "movq   %%mm4, 8(%2)            \n\t"
+        "movq   %%mm5, 8(%2, %3)        \n\t"
+        "add    %%"REG_a", %1           \n\t"
+        "add    %%"REG_a", %2           \n\t"
+        "movq   (%1), %%mm0             \n\t"
+        "movq   1(%1), %%mm1            \n\t"
+        "movq   (%1, %3), %%mm2         \n\t"
+        "movq   1(%1, %3), %%mm3        \n\t"
+        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
+        "movq   %%mm4, (%2)             \n\t"
+        "movq   %%mm5, (%2, %3)         \n\t"
+        "movq   8(%1), %%mm0            \n\t"
+        "movq   9(%1), %%mm1            \n\t"
+        "movq   8(%1, %3), %%mm2        \n\t"
+        "movq   9(%1, %3), %%mm3        \n\t"
+        PAVGBP(%%mm0, %%mm1, %%mm4,   %%mm2, %%mm3, %%mm5)
+        "movq   %%mm4, 8(%2)            \n\t"
+        "movq   %%mm5, 8(%2, %3)        \n\t"
+        "add    %%"REG_a", %1           \n\t"
+        "add    %%"REG_a", %2           \n\t"
+        "subl   $4, %0                  \n\t"
+        "jnz    1b                      \n\t"
+        :"+g"(h), "+S"(pixels), "+D"(block)
+        :"r"((x86_reg)line_size)
+        :REG_a, "memory");
+}
+
+static void DEF(put, pixels8_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    MOVQ_BFE(mm6);
+    __asm__ volatile(
+        "lea (%3, %3), %%"REG_a"        \n\t"
+        "movq (%1), %%mm0               \n\t"
+        ".p2align 3                     \n\t"
+        "1:                             \n\t"
+        "movq   (%1, %3), %%mm1         \n\t"
+        "movq   (%1, %%"REG_a"),%%mm2   \n\t"
+        PAVGBP(%%mm1, %%mm0, %%mm4,   %%mm2, %%mm1, %%mm5)
+        "movq   %%mm4, (%2)             \n\t"
+        "movq   %%mm5, (%2, %3)         \n\t"
+        "add    %%"REG_a", %1           \n\t"
+        "add    %%"REG_a", %2           \n\t"
+        "movq   (%1, %3), %%mm1         \n\t"
+        "movq   (%1, %%"REG_a"),%%mm0   \n\t"
+        PAVGBP(%%mm1, %%mm2, %%mm4,   %%mm0, %%mm1, %%mm5)
+        "movq   %%mm4, (%2)             \n\t"
+        "movq   %%mm5, (%2, %3)         \n\t"
+        "add    %%"REG_a", %1           \n\t"
+        "add    %%"REG_a", %2           \n\t"
+        "subl   $4, %0                  \n\t"
+        "jnz    1b                      \n\t"
+        :"+g"(h), "+S"(pixels), "+D"(block)
+        :"r"((x86_reg)line_size)
+        :REG_a, "memory");
+}
+
+#ifndef NO_RND
+static void DEF(avg, pixels8_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    MOVQ_BFE(mm6);
+    JUMPALIGN();
+    do {
+        __asm__ volatile(
+            "movq  %1, %%mm0            \n\t"
+            "movq  1%1, %%mm1           \n\t"
+            "movq  %0, %%mm3            \n\t"
+            PAVGB(%%mm0, %%mm1, %%mm2, %%mm6)
+            OP_AVG(%%mm3, %%mm2, %%mm0, %%mm6)
+            "movq  %%mm0, %0            \n\t"
+            :"+m"(*block)
+            :"m"(*pixels)
+            :"memory");
+        pixels += line_size;
+        block += line_size;
+    } while (--h);
+}
+#endif // NO_RND
+
+static void DEF(avg, pixels16_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    MOVQ_BFE(mm6);
+    JUMPALIGN();
+    do {
+        __asm__ volatile(
+            "movq  %1, %%mm0            \n\t"
+            "movq  1%1, %%mm1           \n\t"
+            "movq  %0, %%mm3            \n\t"
+            PAVGB(%%mm0, %%mm1, %%mm2, %%mm6)
+            OP_AVG(%%mm3, %%mm2, %%mm0, %%mm6)
+            "movq  %%mm0, %0            \n\t"
+            "movq  8%1, %%mm0           \n\t"
+            "movq  9%1, %%mm1           \n\t"
+            "movq  8%0, %%mm3           \n\t"
+            PAVGB(%%mm0, %%mm1, %%mm2, %%mm6)
+            OP_AVG(%%mm3, %%mm2, %%mm0, %%mm6)
+            "movq  %%mm0, 8%0           \n\t"
+            :"+m"(*block)
+            :"m"(*pixels)
+            :"memory");
+        pixels += line_size;
+        block += line_size;
+    } while (--h);
+}
+
+static void DEF(avg, pixels8_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
+{
+    MOVQ_BFE(mm6);
+    __asm__ volatile(
+        "lea    (%3, %3), %%"REG_a"     \n\t"
+        "movq   (%1), %%mm0             \n\t"
+        ".p2align 3                     \n\t"
+        "1:                             \n\t"
+        "movq   (%1, %3), %%mm1         \n\t"
+        "movq   (%1, %%"REG_a"), %%mm2  \n\t"
+        PAVGBP(%%mm1, %%mm0, %%mm4,   %%mm2, %%mm1, %%mm5)
+        "movq   (%2), %%mm3             \n\t"
+        OP_AVG(%%mm3, %%mm4, %%mm0, %%mm6)
+        "movq   (%2, %3), %%mm3         \n\t"
+        OP_AVG(%%mm3, %%mm5, %%mm1, %%mm6)
+        "movq   %%mm0, (%2)             \n\t"
+        "movq   %%mm1, (%2, %3)         \n\t"
+        "add    %%"REG_a", %1           \n\t"
+        "add    %%"REG_a", %2           \n\t"
+
+        "movq   (%1, %3), %%mm1         \n\t"
+        "movq   (%1, %%"REG_a"), %%mm0  \n\t"
+        PAVGBP(%%mm1, %%mm2, %%mm4,   %%mm0, %%mm1, %%mm5)
+        "movq   (%2), %%mm3             \n\t"
+        OP_AVG(%%mm3, %%mm4, %%mm2, %%mm6)
+        "movq   (%2, %3), %%mm3         \n\t"
+        OP_AVG(%%mm3, %%mm5, %%mm1, %%mm6)
+        "movq   %%mm2, (%2)             \n\t"
+        "movq   %%mm1, (%2, %3)         \n\t"
+        "add    %%"REG_a", %1           \n\t"
+        "add    %%"REG_a", %2           \n\t"
+
+        "subl   $4, %0                  \n\t"
+        "jnz    1b                      \n\t"
+        :"+g"(h), "+S"(pixels), "+D"(block)
+        :"r"((x86_reg)line_size)
+        :REG_a, "memory");
+}
+
+//FIXME optimize
+static void DEF(put, pixels16_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){
+    DEF(put, pixels8_y2)(block  , pixels  , line_size, h);
+    DEF(put, pixels8_y2)(block+8, pixels+8, line_size, h);
+}
+
+static void DEF(avg, pixels16_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){
+    DEF(avg, pixels8_y2)(block  , pixels  , line_size, h);
+    DEF(avg, pixels8_y2)(block+8, pixels+8, line_size, h);
+}
diff --git a/libavcodec/x86/idct_mmx.c b/libavcodec/x86/idct_mmx.c
deleted file mode 100644
index 4b92f5c..0000000
--- a/libavcodec/x86/idct_mmx.c
+++ /dev/null
@@ -1,632 +0,0 @@
-/*
- * Copyright (C) 1999-2001 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
- *
- * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
- * See http://libmpeg2.sourceforge.net/ for updates.
- *
- * mpeg2dec is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * mpeg2dec 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with mpeg2dec; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "libavutil/common.h"
-#include "libavcodec/dsputil.h"
-
-#include "libavutil/x86/asm.h"
-#include "dsputil_mmx.h"
-
-#if HAVE_INLINE_ASM
-
-#define ROW_SHIFT 11
-#define COL_SHIFT 6
-
-#define round(bias) ((int)(((bias)+0.5) * (1<<ROW_SHIFT)))
-#define rounder(bias) {round (bias), round (bias)}
-
-
-#if 0
-/* C row IDCT - it is just here to document the MMXEXT and MMX versions */
-static inline void idct_row (int16_t * row, int offset,
-                             int16_t * table, int32_t * rounder)
-{
-    int C1, C2, C3, C4, C5, C6, C7;
-    int a0, a1, a2, a3, b0, b1, b2, b3;
-
-    row += offset;
-
-    C1 = table[1];
-    C2 = table[2];
-    C3 = table[3];
-    C4 = table[4];
-    C5 = table[5];
-    C6 = table[6];
-    C7 = table[7];
-
-    a0 = C4*row[0] + C2*row[2] + C4*row[4] + C6*row[6] + *rounder;
-    a1 = C4*row[0] + C6*row[2] - C4*row[4] - C2*row[6] + *rounder;
-    a2 = C4*row[0] - C6*row[2] - C4*row[4] + C2*row[6] + *rounder;
-    a3 = C4*row[0] - C2*row[2] + C4*row[4] - C6*row[6] + *rounder;
-
-    b0 = C1*row[1] + C3*row[3] + C5*row[5] + C7*row[7];
-    b1 = C3*row[1] - C7*row[3] - C1*row[5] - C5*row[7];
-    b2 = C5*row[1] - C1*row[3] + C7*row[5] + C3*row[7];
-    b3 = C7*row[1] - C5*row[3] + C3*row[5] - C1*row[7];
-
-    row[0] = (a0 + b0) >> ROW_SHIFT;
-    row[1] = (a1 + b1) >> ROW_SHIFT;
-    row[2] = (a2 + b2) >> ROW_SHIFT;
-    row[3] = (a3 + b3) >> ROW_SHIFT;
-    row[4] = (a3 - b3) >> ROW_SHIFT;
-    row[5] = (a2 - b2) >> ROW_SHIFT;
-    row[6] = (a1 - b1) >> ROW_SHIFT;
-    row[7] = (a0 - b0) >> ROW_SHIFT;
-}
-#endif
-
-
-/* MMXEXT row IDCT */
-
-#define mmxext_table(c1,c2,c3,c4,c5,c6,c7)      {  c4,  c2, -c4, -c2,   \
-                                                   c4,  c6,  c4,  c6,   \
-                                                   c1,  c3, -c1, -c5,   \
-                                                   c5,  c7,  c3, -c7,   \
-                                                   c4, -c6,  c4, -c6,   \
-                                                  -c4,  c2,  c4, -c2,   \
-                                                   c5, -c1,  c3, -c1,   \
-                                                   c7,  c3,  c7, -c5 }
-
-static inline void mmxext_row_head (int16_t * const row, const int offset,
-                                    const int16_t * const table)
-{
-    __asm__ volatile(
-        "movq     (%0), %%mm2        \n\t"  /* mm2 = x6 x4 x2 x0 */
-
-        "movq    8(%0), %%mm5        \n\t"  /* mm5 = x7 x5 x3 x1 */
-        "movq    %%mm2, %%mm0        \n\t"  /* mm0 = x6 x4 x2 x0 */
-
-        "movq     (%1), %%mm3        \n\t"  /* mm3 = -C2 -C4 C2 C4 */
-        "movq    %%mm5, %%mm6        \n\t"  /* mm6 = x7 x5 x3 x1 */
-
-        "movq    8(%1), %%mm4        \n\t"  /* mm4 = C6 C4 C6 C4 */
-        "pmaddwd %%mm0, %%mm3        \n\t"  /* mm3 = -C4*x4-C2*x6 C4*x0+C2*x2 */
-
-        "pshufw  $0x4e, %%mm2, %%mm2 \n\t"  /* mm2 = x2 x0 x6 x4 */
-        :: "r" ((row+offset)), "r" (table)
-    );
-}
-
-static inline void mmxext_row (const int16_t * const table,
-                               const int32_t * const rounder)
-{
-    __asm__ volatile (
-        "movq    16(%0), %%mm1         \n\t" /* mm1 = -C5 -C1 C3 C1 */
-        "pmaddwd  %%mm2, %%mm4         \n\t" /* mm4 = C4*x0+C6*x2 C4*x4+C6*x6 */
-
-        "pmaddwd 32(%0), %%mm0         \n\t" /* mm0 = C4*x4-C6*x6 C4*x0-C6*x2 */
-        "pshufw   $0x4e, %%mm6, %%mm6  \n\t" /* mm6 = x3 x1 x7 x5 */
-
-        "movq    24(%0), %%mm7         \n\t" /* mm7 = -C7 C3 C7 C5 */
-        "pmaddwd  %%mm5, %%mm1         \n\t" /* mm1= -C1*x5-C5*x7 C1*x1+C3*x3 */
-
-        "paddd     (%1), %%mm3         \n\t" /* mm3 += rounder */
-        "pmaddwd  %%mm6, %%mm7         \n\t" /* mm7 = C3*x1-C7*x3 C5*x5+C7*x7 */
-
-        "pmaddwd 40(%0), %%mm2         \n\t" /* mm2= C4*x0-C2*x2 -C4*x4+C2*x6 */
-        "paddd    %%mm4, %%mm3         \n\t" /* mm3 = a1 a0 + rounder */
-
-        "pmaddwd 48(%0), %%mm5         \n\t" /* mm5 = C3*x5-C1*x7 C5*x1-C1*x3 */
-        "movq     %%mm3, %%mm4         \n\t" /* mm4 = a1 a0 + rounder */
-
-        "pmaddwd 56(%0), %%mm6         \n\t" /* mm6 = C7*x1-C5*x3 C7*x5+C3*x7 */
-        "paddd    %%mm7, %%mm1         \n\t" /* mm1 = b1 b0 */
-
-        "paddd     (%1), %%mm0         \n\t" /* mm0 += rounder */
-        "psubd    %%mm1, %%mm3         \n\t" /* mm3 = a1-b1 a0-b0 + rounder */
-
-        "psrad $" AV_STRINGIFY(ROW_SHIFT) ", %%mm3         \n\t" /* mm3 = y6 y7 */
-        "paddd    %%mm4, %%mm1         \n\t" /* mm1 = a1+b1 a0+b0 + rounder */
-
-        "paddd    %%mm2, %%mm0         \n\t" /* mm0 = a3 a2 + rounder */
-        "psrad $" AV_STRINGIFY(ROW_SHIFT) ", %%mm1         \n\t" /* mm1 = y1 y0 */
-
-        "paddd    %%mm6, %%mm5         \n\t" /* mm5 = b3 b2 */
-        "movq     %%mm0, %%mm4         \n\t" /* mm4 = a3 a2 + rounder */
-
-        "paddd    %%mm5, %%mm0         \n\t" /* mm0 = a3+b3 a2+b2 + rounder */
-        "psubd    %%mm5, %%mm4         \n\t" /* mm4 = a3-b3 a2-b2 + rounder */
-        : : "r" (table), "r" (rounder));
-}
-
-static inline void mmxext_row_tail (int16_t * const row, const int store)
-{
-    __asm__ volatile (
-        "psrad $" AV_STRINGIFY(ROW_SHIFT) ", %%mm0        \n\t"  /* mm0 = y3 y2 */
-
-        "psrad $" AV_STRINGIFY(ROW_SHIFT) ", %%mm4  \n\t"  /* mm4 = y4 y5 */
-
-        "packssdw %%mm0, %%mm1        \n\t"  /* mm1 = y3 y2 y1 y0 */
-
-        "packssdw %%mm3, %%mm4        \n\t"  /* mm4 = y6 y7 y4 y5 */
-
-        "movq     %%mm1, (%0)         \n\t"  /* save y3 y2 y1 y0 */
-        "pshufw   $0xb1, %%mm4, %%mm4 \n\t"  /* mm4 = y7 y6 y5 y4 */
-
-        /* slot */
-
-        "movq     %%mm4, 8(%0)        \n\t"  /* save y7 y6 y5 y4 */
-        :: "r" (row+store)
-        );
-}
-
-static inline void mmxext_row_mid (int16_t * const row, const int store,
-                                   const int offset,
-                                   const int16_t * const table)
-{
-    __asm__ volatile (
-        "movq     (%0,%1), %%mm2       \n\t" /* mm2 = x6 x4 x2 x0 */
-        "psrad $" AV_STRINGIFY(ROW_SHIFT) ", %%mm0 \n\t"   /* mm0 = y3 y2 */
-
-        "movq    8(%0,%1), %%mm5       \n\t" /* mm5 = x7 x5 x3 x1 */
-        "psrad $" AV_STRINGIFY(ROW_SHIFT) ", %%mm4 \n\t" /* mm4 = y4 y5 */
-
-        "packssdw   %%mm0, %%mm1       \n\t" /* mm1 = y3 y2 y1 y0 */
-        "movq       %%mm5, %%mm6       \n\t" /* mm6 = x7 x5 x3 x1 */
-
-        "packssdw   %%mm3, %%mm4       \n\t" /* mm4 = y6 y7 y4 y5 */
-        "movq       %%mm2, %%mm0       \n\t" /* mm0 = x6 x4 x2 x0 */
-
-        "movq       %%mm1, (%0,%2)     \n\t" /* save y3 y2 y1 y0 */
-        "pshufw     $0xb1, %%mm4, %%mm4\n\t" /* mm4 = y7 y6 y5 y4 */
-
-        "movq        (%3), %%mm3       \n\t" /* mm3 = -C2 -C4 C2 C4 */
-        "movq       %%mm4, 8(%0,%2)    \n\t" /* save y7 y6 y5 y4 */
-
-        "pmaddwd    %%mm0, %%mm3       \n\t" /* mm3= -C4*x4-C2*x6 C4*x0+C2*x2 */
-
-        "movq       8(%3), %%mm4       \n\t" /* mm4 = C6 C4 C6 C4 */
-        "pshufw     $0x4e, %%mm2, %%mm2\n\t" /* mm2 = x2 x0 x6 x4 */
-        :: "r" (row), "r" ((x86_reg) (2*offset)), "r" ((x86_reg) (2*store)), "r" (table)
-        );
-}
-
-
-/* MMX row IDCT */
-
-#define mmx_table(c1,c2,c3,c4,c5,c6,c7) {  c4,  c2,  c4,  c6,   \
-                                           c4,  c6, -c4, -c2,   \
-                                           c1,  c3,  c3, -c7,   \
-                                           c5,  c7, -c1, -c5,   \
-                                           c4, -c6,  c4, -c2,   \
-                                          -c4,  c2,  c4, -c6,   \
-                                           c5, -c1,  c7, -c5,   \
-                                           c7,  c3,  c3, -c1 }
-
-static inline void mmx_row_head (int16_t * const row, const int offset,
-                                 const int16_t * const table)
-{
-    __asm__ volatile (
-        "movq (%0), %%mm2       \n\t"    /* mm2 = x6 x4 x2 x0 */
-
-        "movq 8(%0), %%mm5      \n\t"    /* mm5 = x7 x5 x3 x1 */
-        "movq %%mm2, %%mm0      \n\t"    /* mm0 = x6 x4 x2 x0 */
-
-        "movq (%1), %%mm3       \n\t"    /* mm3 = C6 C4 C2 C4 */
-        "movq %%mm5, %%mm6      \n\t"    /* mm6 = x7 x5 x3 x1 */
-
-        "punpckldq %%mm0, %%mm0 \n\t"    /* mm0 = x2 x0 x2 x0 */
-
-        "movq 8(%1), %%mm4      \n\t"    /* mm4 = -C2 -C4 C6 C4 */
-        "pmaddwd %%mm0, %%mm3   \n\t"    /* mm3 = C4*x0+C6*x2 C4*x0+C2*x2 */
-
-        "movq 16(%1), %%mm1     \n\t"    /* mm1 = -C7 C3 C3 C1 */
-        "punpckhdq %%mm2, %%mm2 \n\t"    /* mm2 = x6 x4 x6 x4 */
-        :: "r" ((row+offset)), "r" (table)
-        );
-}
-
-static inline void mmx_row (const int16_t * const table,
-                            const int32_t * const rounder)
-{
-    __asm__ volatile (
-        "pmaddwd   %%mm2, %%mm4    \n\t"  /* mm4 = -C4*x4-C2*x6 C4*x4+C6*x6 */
-        "punpckldq %%mm5, %%mm5    \n\t"  /* mm5 = x3 x1 x3 x1 */
-
-        "pmaddwd  32(%0), %%mm0    \n\t"  /* mm0 = C4*x0-C2*x2 C4*x0-C6*x2 */
-        "punpckhdq %%mm6, %%mm6    \n\t"  /* mm6 = x7 x5 x7 x5 */
-
-        "movq     24(%0), %%mm7    \n\t"  /* mm7 = -C5 -C1 C7 C5 */
-        "pmaddwd   %%mm5, %%mm1    \n\t"  /* mm1 = C3*x1-C7*x3 C1*x1+C3*x3 */
-
-        "paddd      (%1), %%mm3    \n\t"  /* mm3 += rounder */
-        "pmaddwd   %%mm6, %%mm7    \n\t"  /* mm7 = -C1*x5-C5*x7 C5*x5+C7*x7 */
-
-        "pmaddwd  40(%0), %%mm2    \n\t"  /* mm2 = C4*x4-C6*x6 -C4*x4+C2*x6 */
-        "paddd     %%mm4, %%mm3    \n\t"  /* mm3 = a1 a0 + rounder */
-
-        "pmaddwd  48(%0), %%mm5    \n\t"  /* mm5 = C7*x1-C5*x3 C5*x1-C1*x3 */
-        "movq      %%mm3, %%mm4    \n\t"  /* mm4 = a1 a0 + rounder */
-
-        "pmaddwd  56(%0), %%mm6    \n\t"  /* mm6 = C3*x5-C1*x7 C7*x5+C3*x7 */
-        "paddd     %%mm7, %%mm1    \n\t"  /* mm1 = b1 b0 */
-
-        "paddd      (%1), %%mm0    \n\t"  /* mm0 += rounder */
-        "psubd     %%mm1, %%mm3    \n\t"  /* mm3 = a1-b1 a0-b0 + rounder */
-
-        "psrad $" AV_STRINGIFY(ROW_SHIFT) ", %%mm3    \n\t"  /* mm3 = y6 y7 */
-        "paddd     %%mm4, %%mm1    \n\t"  /* mm1 = a1+b1 a0+b0 + rounder */
-
-        "paddd     %%mm2, %%mm0    \n\t"  /* mm0 = a3 a2 + rounder */
-        "psrad $" AV_STRINGIFY(ROW_SHIFT) ", %%mm1    \n\t"  /* mm1 = y1 y0 */
-
-        "paddd     %%mm6, %%mm5    \n\t"  /* mm5 = b3 b2 */
-        "movq      %%mm0, %%mm7    \n\t"  /* mm7 = a3 a2 + rounder */
-
-        "paddd     %%mm5, %%mm0    \n\t"  /* mm0 = a3+b3 a2+b2 + rounder */
-        "psubd     %%mm5, %%mm7    \n\t"  /* mm7 = a3-b3 a2-b2 + rounder */
-        :: "r" (table), "r" (rounder)
-        );
-}
-
-static inline void mmx_row_tail (int16_t * const row, const int store)
-{
-    __asm__ volatile (
-        "psrad $" AV_STRINGIFY(ROW_SHIFT) ", %%mm0      \n\t" /* mm0 = y3 y2 */
-
-        "psrad $" AV_STRINGIFY(ROW_SHIFT) ", %%mm7      \n\t" /* mm7 = y4 y5 */
-
-        "packssdw %%mm0, %%mm1 \n\t" /* mm1 = y3 y2 y1 y0 */
-
-        "packssdw %%mm3, %%mm7 \n\t" /* mm7 = y6 y7 y4 y5 */
-
-        "movq %%mm1, (%0)      \n\t" /* save y3 y2 y1 y0 */
-        "movq %%mm7, %%mm4     \n\t" /* mm4 = y6 y7 y4 y5 */
-
-        "pslld $16, %%mm7      \n\t" /* mm7 = y7 0 y5 0 */
-
-        "psrld $16, %%mm4      \n\t" /* mm4 = 0 y6 0 y4 */
-
-        "por %%mm4, %%mm7      \n\t" /* mm7 = y7 y6 y5 y4 */
-
-        /* slot */
-
-        "movq %%mm7, 8(%0)     \n\t" /* save y7 y6 y5 y4 */
-        :: "r" (row+store)
-        );
-}
-
-static inline void mmx_row_mid (int16_t * const row, const int store,
-                                const int offset, const int16_t * const table)
-{
-
-    __asm__ volatile (
-        "movq    (%0,%1), %%mm2    \n\t" /* mm2 = x6 x4 x2 x0 */
-        "psrad $" AV_STRINGIFY(ROW_SHIFT) ", %%mm0 \n\t" /* mm0 = y3 y2 */
-
-        "movq   8(%0,%1), %%mm5    \n\t" /* mm5 = x7 x5 x3 x1 */
-        "psrad $" AV_STRINGIFY(ROW_SHIFT) ", %%mm7 \n\t" /* mm7 = y4 y5 */
-
-        "packssdw  %%mm0, %%mm1    \n\t" /* mm1 = y3 y2 y1 y0 */
-        "movq      %%mm5, %%mm6    \n\t" /* mm6 = x7 x5 x3 x1 */
-
-        "packssdw  %%mm3, %%mm7    \n\t" /* mm7 = y6 y7 y4 y5 */
-        "movq      %%mm2, %%mm0    \n\t" /* mm0 = x6 x4 x2 x0 */
-
-        "movq      %%mm1, (%0,%2)  \n\t" /* save y3 y2 y1 y0 */
-        "movq      %%mm7, %%mm1    \n\t" /* mm1 = y6 y7 y4 y5 */
-
-        "punpckldq %%mm0, %%mm0    \n\t" /* mm0 = x2 x0 x2 x0 */
-        "psrld       $16, %%mm7    \n\t" /* mm7 = 0 y6 0 y4 */
-
-        "movq       (%3), %%mm3    \n\t" /* mm3 = C6 C4 C2 C4 */
-        "pslld       $16, %%mm1    \n\t" /* mm1 = y7 0 y5 0 */
-
-        "movq      8(%3), %%mm4    \n\t" /* mm4 = -C2 -C4 C6 C4 */
-        "por       %%mm1, %%mm7    \n\t" /* mm7 = y7 y6 y5 y4 */
-
-        "movq     16(%3), %%mm1    \n\t" /* mm1 = -C7 C3 C3 C1 */
-        "punpckhdq %%mm2, %%mm2    \n\t" /* mm2 = x6 x4 x6 x4 */
-
-        "movq      %%mm7, 8(%0,%2) \n\t" /* save y7 y6 y5 y4 */
-        "pmaddwd   %%mm0, %%mm3    \n\t" /* mm3 = C4*x0+C6*x2 C4*x0+C2*x2 */
-        : : "r" (row), "r" ((x86_reg) (2*offset)), "r" ((x86_reg) (2*store)), "r" (table)
-        );
-}
-
-
-#if 0
-/* C column IDCT - it is just here to document the MMXEXT and MMX versions */
-static inline void idct_col (int16_t * col, int offset)
-{
-/* multiplication - as implemented on mmx */
-#define F(c,x) (((c) * (x)) >> 16)
-
-/* saturation - it helps us handle torture test cases */
-#define S(x) (((x)>32767) ? 32767 : ((x)<-32768) ? -32768 : (x))
-
-    int16_t x0, x1, x2, x3, x4, x5, x6, x7;
-    int16_t y0, y1, y2, y3, y4, y5, y6, y7;
-    int16_t a0, a1, a2, a3, b0, b1, b2, b3;
-    int16_t u04, v04, u26, v26, u17, v17, u35, v35, u12, v12;
-
-    col += offset;
-
-    x0 = col[0*8];
-    x1 = col[1*8];
-    x2 = col[2*8];
-    x3 = col[3*8];
-    x4 = col[4*8];
-    x5 = col[5*8];
-    x6 = col[6*8];
-    x7 = col[7*8];
-
-    u04 = S (x0 + x4);
-    v04 = S (x0 - x4);
-    u26 = S (F (T2, x6) + x2);
-    v26 = S (F (T2, x2) - x6);
-
-    a0 = S (u04 + u26);
-    a1 = S (v04 + v26);
-    a2 = S (v04 - v26);
-    a3 = S (u04 - u26);
-
-    u17 = S (F (T1, x7) + x1);
-    v17 = S (F (T1, x1) - x7);
-    u35 = S (F (T3, x5) + x3);
-    v35 = S (F (T3, x3) - x5);
-
-    b0 = S (u17 + u35);
-    b3 = S (v17 - v35);
-    u12 = S (u17 - u35);
-    v12 = S (v17 + v35);
-    u12 = S (2 * F (C4, u12));
-    v12 = S (2 * F (C4, v12));
-    b1 = S (u12 + v12);
-    b2 = S (u12 - v12);
-
-    y0 = S (a0 + b0) >> COL_SHIFT;
-    y1 = S (a1 + b1) >> COL_SHIFT;
-    y2 = S (a2 + b2) >> COL_SHIFT;
-    y3 = S (a3 + b3) >> COL_SHIFT;
-
-    y4 = S (a3 - b3) >> COL_SHIFT;
-    y5 = S (a2 - b2) >> COL_SHIFT;
-    y6 = S (a1 - b1) >> COL_SHIFT;
-    y7 = S (a0 - b0) >> COL_SHIFT;
-
-    col[0*8] = y0;
-    col[1*8] = y1;
-    col[2*8] = y2;
-    col[3*8] = y3;
-    col[4*8] = y4;
-    col[5*8] = y5;
-    col[6*8] = y6;
-    col[7*8] = y7;
-}
-#endif
-
-
-/* MMX column IDCT */
-static inline void idct_col (int16_t * const col, const int offset)
-{
-#define T1 13036
-#define T2 27146
-#define T3 43790
-#define C4 23170
-
-    DECLARE_ALIGNED(8, static const short, t1_vector)[] = {
-        T1,T1,T1,T1,
-        T2,T2,T2,T2,
-        T3,T3,T3,T3,
-        C4,C4,C4,C4
-    };
-
-    /* column code adapted from Peter Gubanov */
-    /* http://www.elecard.com/peter/idct.shtml */
-
-    __asm__ volatile (
-        "movq      (%0), %%mm0    \n\t" /* mm0 = T1 */
-
-        "movq   2*8(%1), %%mm1    \n\t" /* mm1 = x1 */
-        "movq     %%mm0, %%mm2    \n\t" /* mm2 = T1 */
-
-        "movq 7*2*8(%1), %%mm4    \n\t" /* mm4 = x7 */
-        "pmulhw   %%mm1, %%mm0    \n\t" /* mm0 = T1*x1 */
-
-        "movq    16(%0), %%mm5    \n\t" /* mm5 = T3 */
-        "pmulhw   %%mm4, %%mm2    \n\t" /* mm2 = T1*x7 */
-
-        "movq 2*5*8(%1), %%mm6    \n\t" /* mm6 = x5 */
-        "movq     %%mm5, %%mm7    \n\t" /* mm7 = T3-1 */
-
-        "movq 3*8*2(%1), %%mm3    \n\t" /* mm3 = x3 */
-        "psubsw   %%mm4, %%mm0    \n\t" /* mm0 = v17 */
-
-        "movq     8(%0), %%mm4    \n\t" /* mm4 = T2 */
-        "pmulhw   %%mm3, %%mm5    \n\t" /* mm5 = (T3-1)*x3 */
-
-        "paddsw   %%mm2, %%mm1    \n\t" /* mm1 = u17 */
-        "pmulhw   %%mm6, %%mm7    \n\t" /* mm7 = (T3-1)*x5 */
-
-        /* slot */
-
-        "movq     %%mm4, %%mm2    \n\t" /* mm2 = T2 */
-        "paddsw   %%mm3, %%mm5    \n\t" /* mm5 = T3*x3 */
-
-        "pmulhw 2*8*2(%1), %%mm4  \n\t" /* mm4 = T2*x2 */
-        "paddsw   %%mm6, %%mm7    \n\t" /* mm7 = T3*x5 */
-
-        "psubsw   %%mm6, %%mm5    \n\t" /* mm5 = v35 */
-        "paddsw   %%mm3, %%mm7    \n\t" /* mm7 = u35 */
-
-        "movq 6*8*2(%1), %%mm3    \n\t" /* mm3 = x6 */
-        "movq     %%mm0, %%mm6    \n\t" /* mm6 = v17 */
-
-        "pmulhw   %%mm3, %%mm2    \n\t" /* mm2 = T2*x6 */
-        "psubsw   %%mm5, %%mm0    \n\t" /* mm0 = b3 */
-
-        "psubsw   %%mm3, %%mm4    \n\t" /* mm4 = v26 */
-        "paddsw   %%mm6, %%mm5    \n\t" /* mm5 = v12 */
-
-        "movq     %%mm0, 3*8*2(%1)\n\t" /* save b3 in scratch0 */
-        "movq     %%mm1, %%mm6    \n\t" /* mm6 = u17 */
-
-        "paddsw 2*8*2(%1), %%mm2  \n\t" /* mm2 = u26 */
-        "paddsw   %%mm7, %%mm6    \n\t" /* mm6 = b0 */
-
-        "psubsw   %%mm7, %%mm1    \n\t" /* mm1 = u12 */
-        "movq     %%mm1, %%mm7    \n\t" /* mm7 = u12 */
-
-        "movq   0*8(%1), %%mm3    \n\t" /* mm3 = x0 */
-        "paddsw   %%mm5, %%mm1    \n\t" /* mm1 = u12+v12 */
-
-        "movq    24(%0), %%mm0    \n\t" /* mm0 = C4/2 */
-        "psubsw   %%mm5, %%mm7    \n\t" /* mm7 = u12-v12 */
-
-        "movq     %%mm6, 5*8*2(%1)\n\t" /* save b0 in scratch1 */
-        "pmulhw   %%mm0, %%mm1    \n\t" /* mm1 = b1/2 */
-
-        "movq     %%mm4, %%mm6    \n\t" /* mm6 = v26 */
-        "pmulhw   %%mm0, %%mm7    \n\t" /* mm7 = b2/2 */
-
-        "movq 4*8*2(%1), %%mm5    \n\t" /* mm5 = x4 */
-        "movq     %%mm3, %%mm0    \n\t" /* mm0 = x0 */
-
-        "psubsw   %%mm5, %%mm3    \n\t" /* mm3 = v04 */
-        "paddsw   %%mm5, %%mm0    \n\t" /* mm0 = u04 */
-
-        "paddsw   %%mm3, %%mm4    \n\t" /* mm4 = a1 */
-        "movq     %%mm0, %%mm5    \n\t" /* mm5 = u04 */
-
-        "psubsw   %%mm6, %%mm3    \n\t" /* mm3 = a2 */
-        "paddsw   %%mm2, %%mm5    \n\t" /* mm5 = a0 */
-
-        "paddsw   %%mm1, %%mm1    \n\t" /* mm1 = b1 */
-        "psubsw   %%mm2, %%mm0    \n\t" /* mm0 = a3 */
-
-        "paddsw   %%mm7, %%mm7    \n\t" /* mm7 = b2 */
-        "movq     %%mm3, %%mm2    \n\t" /* mm2 = a2 */
-
-        "movq     %%mm4, %%mm6    \n\t" /* mm6 = a1 */
-        "paddsw   %%mm7, %%mm3    \n\t" /* mm3 = a2+b2 */
-
-        "psraw $" AV_STRINGIFY(COL_SHIFT) ", %%mm3\n\t" /* mm3 = y2 */
-        "paddsw   %%mm1, %%mm4\n\t" /* mm4 = a1+b1 */
-
-        "psraw $" AV_STRINGIFY(COL_SHIFT) ", %%mm4\n\t" /* mm4 = y1 */
-        "psubsw   %%mm1, %%mm6    \n\t" /* mm6 = a1-b1 */
-
-        "movq 5*8*2(%1), %%mm1    \n\t" /* mm1 = b0 */
-        "psubsw   %%mm7, %%mm2    \n\t" /* mm2 = a2-b2 */
-
-        "psraw $" AV_STRINGIFY(COL_SHIFT) ", %%mm6\n\t" /* mm6 = y6 */
-        "movq     %%mm5, %%mm7    \n\t" /* mm7 = a0 */
-
-        "movq     %%mm4, 1*8*2(%1)\n\t" /* save y1 */
-        "psraw $" AV_STRINGIFY(COL_SHIFT) ", %%mm2\n\t" /* mm2 = y5 */
-
-        "movq     %%mm3, 2*8*2(%1)\n\t" /* save y2 */
-        "paddsw   %%mm1, %%mm5    \n\t" /* mm5 = a0+b0 */
-
-        "movq 3*8*2(%1), %%mm4    \n\t" /* mm4 = b3 */
-        "psubsw   %%mm1, %%mm7    \n\t" /* mm7 = a0-b0 */
-
-        "psraw $" AV_STRINGIFY(COL_SHIFT) ", %%mm5\n\t" /* mm5 = y0 */
-        "movq     %%mm0, %%mm3    \n\t" /* mm3 = a3 */
-
-        "movq     %%mm2, 5*8*2(%1)\n\t" /* save y5 */
-        "psubsw   %%mm4, %%mm3    \n\t" /* mm3 = a3-b3 */
-
-        "psraw $" AV_STRINGIFY(COL_SHIFT) ", %%mm7\n\t" /* mm7 = y7 */
-        "paddsw   %%mm0, %%mm4    \n\t" /* mm4 = a3+b3 */
-
-        "movq     %%mm5, 0*8*2(%1)\n\t" /* save y0 */
-        "psraw $" AV_STRINGIFY(COL_SHIFT) ", %%mm3\n\t" /* mm3 = y4 */
-
-        "movq     %%mm6, 6*8*2(%1)\n\t" /* save y6 */
-        "psraw $" AV_STRINGIFY(COL_SHIFT) ", %%mm4\n\t" /* mm4 = y3 */
-
-        "movq     %%mm7, 7*8*2(%1)\n\t" /* save y7 */
-
-        "movq     %%mm3, 4*8*2(%1)\n\t" /* save y4 */
-
-        "movq     %%mm4, 3*8*2(%1)\n\t" /* save y3 */
-        :: "r" (t1_vector), "r" (col+offset)
-        );
-
-#undef T1
-#undef T2
-#undef T3
-#undef C4
-}
-
-
-DECLARE_ALIGNED(8, static const int32_t, rounder0)[] =
-    rounder ((1 << (COL_SHIFT - 1)) - 0.5);
-DECLARE_ALIGNED(8, static const int32_t, rounder4)[] = rounder (0);
-DECLARE_ALIGNED(8, static const int32_t, rounder1)[] =
-    rounder (1.25683487303);        /* C1*(C1/C4+C1+C7)/2 */
-DECLARE_ALIGNED(8, static const int32_t, rounder7)[] =
-    rounder (-0.25);                /* C1*(C7/C4+C7-C1)/2 */
-DECLARE_ALIGNED(8, static const int32_t, rounder2)[] =
-    rounder (0.60355339059);        /* C2 * (C6+C2)/2 */
-DECLARE_ALIGNED(8, static const int32_t, rounder6)[] =
-    rounder (-0.25);                /* C2 * (C6-C2)/2 */
-DECLARE_ALIGNED(8, static const int32_t, rounder3)[] =
-    rounder (0.087788325588);       /* C3*(-C3/C4+C3+C5)/2 */
-DECLARE_ALIGNED(8, static const int32_t, rounder5)[] =
-    rounder (-0.441341716183);      /* C3*(-C5/C4+C5-C3)/2 */
-
-#undef COL_SHIFT
-#undef ROW_SHIFT
-
-#define declare_idct(idct,table,idct_row_head,idct_row,idct_row_tail,idct_row_mid) \
-void idct (int16_t * const block)                                       \
-{                                                                       \
-    DECLARE_ALIGNED(16, static const int16_t, table04)[] =              \
-        table (22725, 21407, 19266, 16384, 12873,  8867, 4520);         \
-    DECLARE_ALIGNED(16, static const int16_t, table17)[] =              \
-        table (31521, 29692, 26722, 22725, 17855, 12299, 6270);         \
-    DECLARE_ALIGNED(16, static const int16_t, table26)[] =              \
-        table (29692, 27969, 25172, 21407, 16819, 11585, 5906);         \
-    DECLARE_ALIGNED(16, static const int16_t, table35)[] =              \
-        table (26722, 25172, 22654, 19266, 15137, 10426, 5315);         \
-                                                                        \
-    idct_row_head (block, 0*8, table04);                                \
-    idct_row (table04, rounder0);                                       \
-    idct_row_mid (block, 0*8, 4*8, table04);                            \
-    idct_row (table04, rounder4);                                       \
-    idct_row_mid (block, 4*8, 1*8, table17);                            \
-    idct_row (table17, rounder1);                                       \
-    idct_row_mid (block, 1*8, 7*8, table17);                            \
-    idct_row (table17, rounder7);                                       \
-    idct_row_mid (block, 7*8, 2*8, table26);                            \
-    idct_row (table26, rounder2);                                       \
-    idct_row_mid (block, 2*8, 6*8, table26);                            \
-    idct_row (table26, rounder6);                                       \
-    idct_row_mid (block, 6*8, 3*8, table35);                            \
-    idct_row (table35, rounder3);                                       \
-    idct_row_mid (block, 3*8, 5*8, table35);                            \
-    idct_row (table35, rounder5);                                       \
-    idct_row_tail (block, 5*8);                                         \
-                                                                        \
-    idct_col (block, 0);                                                \
-    idct_col (block, 4);                                                \
-}
-
-declare_idct (ff_mmxext_idct, mmxext_table,
-              mmxext_row_head, mmxext_row, mmxext_row_tail, mmxext_row_mid)
-
-declare_idct (ff_mmx_idct, mmx_table,
-              mmx_row_head, mmx_row, mmx_row_tail, mmx_row_mid)
-
-#endif /* HAVE_INLINE_ASM */
diff --git a/libavcodec/x86/mpegvideo.c b/libavcodec/x86/mpegvideo.c
index 903ad62..49e9fd3 100644
--- a/libavcodec/x86/mpegvideo.c
+++ b/libavcodec/x86/mpegvideo.c
@@ -111,7 +111,7 @@
     qmul = qscale << 1;
     qadd = (qscale - 1) | 1;
 
-    assert(s->block_last_index[n]>=0 || s->h263_aic);
+    av_assert2(s->block_last_index[n]>=0 || s->h263_aic);
 
     nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
 
diff --git a/libavcodec/x86/mpegvideoenc.c b/libavcodec/x86/mpegvideoenc.c
index 6219667..eda0b76 100644
--- a/libavcodec/x86/mpegvideoenc.c
+++ b/libavcodec/x86/mpegvideoenc.c
@@ -83,11 +83,11 @@
 
 av_cold void ff_dct_encode_init_x86(MpegEncContext *s)
 {
-    int mm_flags = av_get_cpu_flags();
     const int dct_algo = s->avctx->dct_algo;
 
     if (dct_algo == FF_DCT_AUTO || dct_algo == FF_DCT_MMX) {
 #if HAVE_MMX_INLINE
+        int mm_flags = av_get_cpu_flags();
         if (INLINE_MMX(mm_flags))
             s->dct_quantize = dct_quantize_MMX;
 #endif
diff --git a/libavcodec/x86/qpel.asm b/libavcodec/x86/qpel.asm
new file mode 100644
index 0000000..c2ffb86
--- /dev/null
+++ b/libavcodec/x86/qpel.asm
@@ -0,0 +1,176 @@
+;******************************************************************************
+;* MMX optimized DSP utils
+;* Copyright (c) 2008 Loren Merritt
+;* Copyright (c) 2003-2013 Michael Niedermayer
+;* Copyright (c) 2013 Daniel Kang
+;*
+;* 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 "libavutil/x86/x86util.asm"
+
+SECTION .text
+
+%macro op_avgh 3
+    movh   %3, %2
+    pavgb  %1, %3
+    movh   %2, %1
+%endmacro
+
+%macro op_avg 2
+    pavgb  %1, %2
+    mova   %2, %1
+%endmacro
+
+%macro op_puth 2-3
+    movh   %2, %1
+%endmacro
+
+%macro op_put 2
+    mova   %2, %1
+%endmacro
+
+; void pixels4_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
+%macro PIXELS4_L2 1
+%define OP op_%1h
+cglobal %1_pixels4_l2, 6,6
+    movsxdifnidn r3, r3d
+    movsxdifnidn r4, r4d
+    test        r5d, 1
+    je        .loop
+    movd         m0, [r1]
+    movd         m1, [r2]
+    add          r1, r4
+    add          r2, 4
+    pavgb        m0, m1
+    OP           m0, [r0], m3
+    add          r0, r3
+    dec         r5d
+.loop:
+    mova         m0, [r1]
+    mova         m1, [r1+r4]
+    lea          r1, [r1+2*r4]
+    pavgb        m0, [r2]
+    pavgb        m1, [r2+4]
+    OP           m0, [r0], m3
+    OP           m1, [r0+r3], m3
+    lea          r0, [r0+2*r3]
+    mova         m0, [r1]
+    mova         m1, [r1+r4]
+    lea          r1, [r1+2*r4]
+    pavgb        m0, [r2+8]
+    pavgb        m1, [r2+12]
+    OP           m0, [r0], m3
+    OP           m1, [r0+r3], m3
+    lea          r0, [r0+2*r3]
+    add          r2, 16
+    sub         r5d, 4
+    jne       .loop
+    REP_RET
+%endmacro
+
+INIT_MMX mmxext
+PIXELS4_L2 put
+PIXELS4_L2 avg
+
+; void pixels8_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
+%macro PIXELS8_L2 1
+%define OP op_%1
+cglobal %1_pixels8_l2, 6,6
+    movsxdifnidn r3, r3d
+    movsxdifnidn r4, r4d
+    test        r5d, 1
+    je        .loop
+    mova         m0, [r1]
+    mova         m1, [r2]
+    add          r1, r4
+    add          r2, 8
+    pavgb        m0, m1
+    OP           m0, [r0]
+    add          r0, r3
+    dec         r5d
+.loop:
+    mova         m0, [r1]
+    mova         m1, [r1+r4]
+    lea          r1, [r1+2*r4]
+    pavgb        m0, [r2]
+    pavgb        m1, [r2+8]
+    OP           m0, [r0]
+    OP           m1, [r0+r3]
+    lea          r0, [r0+2*r3]
+    mova         m0, [r1]
+    mova         m1, [r1+r4]
+    lea          r1, [r1+2*r4]
+    pavgb        m0, [r2+16]
+    pavgb        m1, [r2+24]
+    OP           m0, [r0]
+    OP           m1, [r0+r3]
+    lea          r0, [r0+2*r3]
+    add          r2, 32
+    sub         r5d, 4
+    jne       .loop
+    REP_RET
+%endmacro
+
+INIT_MMX mmxext
+PIXELS8_L2 put
+PIXELS8_L2 avg
+
+; void pixels16_l2_mmxext(uint8_t *dst, uint8_t *src1, uint8_t *src2, int dstStride, int src1Stride, int h)
+%macro PIXELS16_L2 1
+%define OP op_%1
+cglobal %1_pixels16_l2, 6,6
+    movsxdifnidn r3, r3d
+    movsxdifnidn r4, r4d
+    test        r5d, 1
+    je        .loop
+    mova         m0, [r1]
+    mova         m1, [r1+8]
+    pavgb        m0, [r2]
+    pavgb        m1, [r2+8]
+    add          r1, r4
+    add          r2, 16
+    OP           m0, [r0]
+    OP           m1, [r0+8]
+    add          r0, r3
+    dec         r5d
+.loop:
+    mova         m0, [r1]
+    mova         m1, [r1+8]
+    add          r1, r4
+    pavgb        m0, [r2]
+    pavgb        m1, [r2+8]
+    OP           m0, [r0]
+    OP           m1, [r0+8]
+    add          r0, r3
+    mova         m0, [r1]
+    mova         m1, [r1+8]
+    add          r1, r4
+    pavgb        m0, [r2+16]
+    pavgb        m1, [r2+24]
+    OP           m0, [r0]
+    OP           m1, [r0+8]
+    add          r0, r3
+    add          r2, 32
+    sub         r5d, 2
+    jne       .loop
+    REP_RET
+%endmacro
+
+INIT_MMX mmxext
+PIXELS16_L2 put
+PIXELS16_L2 avg
diff --git a/libavcodec/x86/rv40dsp_init.c b/libavcodec/x86/rv40dsp_init.c
index 51e9852..2f97518 100644
--- a/libavcodec/x86/rv40dsp_init.c
+++ b/libavcodec/x86/rv40dsp_init.c
@@ -71,7 +71,7 @@
 #define QPEL_FUNC_DECL(OP, SIZE, PH, PV, OPT)                           \
 static void OP ## rv40_qpel ##SIZE ##_mc ##PH ##PV ##OPT(uint8_t *dst,  \
                                                          uint8_t *src,  \
-                                                         int stride)    \
+                                                         ptrdiff_t stride)  \
 {                                                                       \
     int i;                                                              \
     if (PH && PV) {                                                     \
diff --git a/libavcodec/x86/sbrdsp.asm b/libavcodec/x86/sbrdsp.asm
index 1b7f3a8..1ce5777 100644
--- a/libavcodec/x86/sbrdsp.asm
+++ b/libavcodec/x86/sbrdsp.asm
@@ -24,7 +24,14 @@
 SECTION_RODATA
 ; mask equivalent for multiply by -1.0 1.0
 ps_mask         times 2 dd 1<<31, 0
+ps_mask2        times 2 dd 0, 1<<31
 ps_neg          times 4 dd 1<<31
+ps_noise0       times 2 dd  1.0,  0.0,
+ps_noise2       times 2 dd -1.0,  0.0
+ps_noise13      dd  0.0,  1.0, 0.0, -1.0
+                dd  0.0, -1.0, 0.0,  1.0
+                dd  0.0,  1.0, 0.0, -1.0
+cextern         sbr_noise_table
 
 SECTION_TEXT
 
@@ -220,3 +227,208 @@
     cmp               zq, r2q
     jl             .loop
     REP_RET
+
+INIT_XMM sse
+cglobal sbr_neg_odd_64, 1,2,4,z
+    lea        r1q, [zq+256]
+.loop:
+    mova        m0, [zq+ 0]
+    mova        m1, [zq+16]
+    mova        m2, [zq+32]
+    mova        m3, [zq+48]
+    xorps       m0, [ps_mask2]
+    xorps       m1, [ps_mask2]
+    xorps       m2, [ps_mask2]
+    xorps       m3, [ps_mask2]
+    mova   [zq+ 0], m0
+    mova   [zq+16], m1
+    mova   [zq+32], m2
+    mova   [zq+48], m3
+    add         zq, 64
+    cmp         zq, r1q
+    jne      .loop
+    REP_RET
+
+; sbr_qmf_deint_bfly(float *v, const float *src0, const float *src1)
+%macro SBR_QMF_DEINT_BFLY  0
+cglobal sbr_qmf_deint_bfly, 3,5,8, v,src0,src1,vrev,c
+    mov        cq, 64*4-2*mmsize
+    lea     vrevq, [vq + 64*4]
+.loop:
+    mova       m0, [src0q+cq]
+    mova       m1, [src1q]
+    mova       m4, [src0q+cq+mmsize]
+    mova       m5, [src1q+mmsize]
+%if cpuflag(sse2)
+    pshufd     m2, m0, q0123
+    pshufd     m3, m1, q0123
+    pshufd     m6, m4, q0123
+    pshufd     m7, m5, q0123
+%else
+    shufps     m2, m0, m0, q0123
+    shufps     m3, m1, m1, q0123
+    shufps     m6, m4, m4, q0123
+    shufps     m7, m5, m5, q0123
+%endif
+    addps      m5, m2
+    subps      m0, m7
+    addps      m1, m6
+    subps      m4, m3
+    mova  [vrevq], m1
+    mova  [vrevq+mmsize], m5
+    mova  [vq+cq], m0
+    mova  [vq+cq+mmsize], m4
+    add     src1q, 2*mmsize
+    add     vrevq, 2*mmsize
+    sub        cq, 2*mmsize
+    jge     .loop
+    REP_RET
+%endmacro
+
+INIT_XMM sse
+SBR_QMF_DEINT_BFLY
+
+INIT_XMM sse2
+SBR_QMF_DEINT_BFLY
+
+INIT_XMM sse2
+cglobal sbr_qmf_pre_shuffle, 1,4,7,z
+%define OFFSET  (32*4-2*mmsize)
+    mov       r3q, OFFSET
+    lea       r1q, [zq + (32+1)*4]
+    lea       r2q, [zq + 64*4]
+    mova       m6, [ps_neg]
+.loop:
+    movu       m0, [r1q]
+    movu       m2, [r1q + mmsize]
+    movu       m1, [zq + r3q + 4 + mmsize]
+    movu       m3, [zq + r3q + 4]
+
+    pxor       m2, m6
+    pxor       m0, m6
+    pshufd     m2, m2, q0123
+    pshufd     m0, m0, q0123
+    SBUTTERFLY dq, 2, 3, 5
+    SBUTTERFLY dq, 0, 1, 4
+    mova  [r2q + 2*r3q + 0*mmsize], m2
+    mova  [r2q + 2*r3q + 1*mmsize], m3
+    mova  [r2q + 2*r3q + 2*mmsize], m0
+    mova  [r2q + 2*r3q + 3*mmsize], m1
+    add       r1q, 2*mmsize
+    sub       r3q, 2*mmsize
+    jge      .loop
+    mova       m2, [zq]
+    movq    [r2q], m2
+    REP_RET
+
+%if WIN64 == 0
+
+%if WIN64
+%define NREGS 0
+%define NOISE_TABLE sbr_noise_table
+%else
+%ifdef PIC
+%define NREGS 1
+%if UNIX64
+%define NOISE_TABLE r6q ; r5q is m_max
+%else
+%define NOISE_TABLE r5q
+%endif
+%else
+%define NREGS 0
+%define NOISE_TABLE sbr_noise_table
+%endif
+%endif
+
+%macro LOAD_NST  1
+%if NREGS
+    lea  NOISE_TABLE, [%1]
+    mova          m0, [kxq + NOISE_TABLE]
+%else
+    mova          m0, [kxq + %1]
+%endif
+%endmacro
+
+INIT_XMM sse2
+; sbr_hf_apply_noise_0(float (*Y)[2], const float *s_m,
+;                      const float *q_filt, int noise,
+;                      int kx, int m_max)
+cglobal sbr_hf_apply_noise_0, 5,5+NREGS+UNIX64,8, Y,s_m,q_filt,noise,kx,m_max
+    mova       m0, [ps_noise0]
+    jmp apply_noise_main
+
+; sbr_hf_apply_noise_1(float (*Y)[2], const float *s_m,
+;                      const float *q_filt, int noise,
+;                      int kx, int m_max)
+cglobal sbr_hf_apply_noise_1, 5,5+NREGS+UNIX64,8, Y,s_m,q_filt,noise,kx,m_max
+    and       kxq, 1
+    shl       kxq, 4
+    LOAD_NST  ps_noise13
+    jmp apply_noise_main
+
+; sbr_hf_apply_noise_2(float (*Y)[2], const float *s_m,
+;                      const float *q_filt, int noise,
+;                      int kx, int m_max)
+cglobal sbr_hf_apply_noise_2, 5,5+NREGS+UNIX64,8, Y,s_m,q_filt,noise,kx,m_max
+    mova       m0, [ps_noise2]
+    jmp apply_noise_main
+
+; sbr_hf_apply_noise_3(float (*Y)[2], const float *s_m,
+;                      const float *q_filt, int noise,
+;                      int kx, int m_max)
+cglobal sbr_hf_apply_noise_3, 5,5+NREGS+UNIX64,8, Y,s_m,q_filt,noise,kx,m_max
+    and       kxq, 1
+    shl       kxq, 4
+    LOAD_NST  ps_noise13+16
+
+apply_noise_main:
+%if ARCH_X86_64 == 0 || WIN64
+    mov       kxd, m_maxm
+%define count kxq
+%else
+%define count m_maxq
+%endif
+    dec    noiseq
+    shl    count, 2
+%if NREGS
+    lea NOISE_TABLE, [sbr_noise_table]
+%endif
+    lea        Yq, [Yq + 2*count]
+    add      s_mq, count
+    add   q_filtq, count
+    shl    noiseq, 3
+    pxor       m5, m5
+    neg    count
+.loop:
+    mova       m1, [q_filtq + count]
+    movu       m3, [noiseq + NOISE_TABLE + 1*mmsize]
+    movu       m4, [noiseq + NOISE_TABLE + 2*mmsize]
+    add    noiseq, 2*mmsize
+    and    noiseq, 0x1ff<<3
+    punpckhdq  m2, m1, m1
+    punpckldq  m1, m1
+    mulps      m1, m3 ; m2 = q_filt[m] * ff_sbr_noise_table[noise]
+    mulps      m2, m4 ; m2 = q_filt[m] * ff_sbr_noise_table[noise]
+    mova       m3, [s_mq + count]
+    ; TODO: replace by a vpermd in AVX2
+    punpckhdq  m4, m3, m3
+    punpckldq  m3, m3
+    pcmpeqd    m6, m3, m5 ; m6 == 0
+    pcmpeqd    m7, m4, m5 ; m7 == 0
+    mulps      m3, m0 ; s_m[m] * phi_sign
+    mulps      m4, m0 ; s_m[m] * phi_sign
+    pand       m1, m6
+    pand       m2, m7
+    movu       m6, [Yq + 2*count]
+    movu       m7, [Yq + 2*count + mmsize]
+    addps      m3, m1
+    addps      m4, m2
+    addps      m6, m3
+    addps      m7, m4
+    movu    [Yq + 2*count], m6
+    movu    [Yq + 2*count + mmsize], m7
+    add    count, mmsize
+    jl      .loop
+    RET
+
+%endif ; WIN64 == 0
diff --git a/libavcodec/x86/sbrdsp_init.c b/libavcodec/x86/sbrdsp_init.c
index 27fade1..4eff496 100644
--- a/libavcodec/x86/sbrdsp_init.c
+++ b/libavcodec/x86/sbrdsp_init.c
@@ -32,17 +32,47 @@
 void ff_sbr_hf_gen_sse(float (*X_high)[2], const float (*X_low)[2],
                        const float alpha0[2], const float alpha1[2],
                        float bw, int start, int end);
+void ff_sbr_neg_odd_64_sse(float *z);
 void ff_sbr_qmf_post_shuffle_sse(float W[32][2], const float *z);
+void ff_sbr_qmf_deint_bfly_sse(float *v, const float *src0, const float *src1);
+void ff_sbr_qmf_deint_bfly_sse2(float *v, const float *src0, const float *src1);
+void ff_sbr_qmf_pre_shuffle_sse2(float *z);
+
+void ff_sbr_hf_apply_noise_0_sse2(float (*Y)[2], const float *s_m,
+                                  const float *q_filt, int noise,
+                                  int kx, int m_max);
+void ff_sbr_hf_apply_noise_1_sse2(float (*Y)[2], const float *s_m,
+                                  const float *q_filt, int noise,
+                                  int kx, int m_max);
+void ff_sbr_hf_apply_noise_2_sse2(float (*Y)[2], const float *s_m,
+                                  const float *q_filt, int noise,
+                                  int kx, int m_max);
+void ff_sbr_hf_apply_noise_3_sse2(float (*Y)[2], const float *s_m,
+                                  const float *q_filt, int noise,
+                                  int kx, int m_max);
 
 av_cold void ff_sbrdsp_init_x86(SBRDSPContext *s)
 {
     int mm_flags = av_get_cpu_flags();
 
     if (EXTERNAL_SSE(mm_flags)) {
+        s->neg_odd_64 = ff_sbr_neg_odd_64_sse;
         s->sum_square = ff_sbr_sum_square_sse;
         s->sum64x5    = ff_sbr_sum64x5_sse;
         s->hf_g_filt  = ff_sbr_hf_g_filt_sse;
         s->hf_gen     = ff_sbr_hf_gen_sse;
         s->qmf_post_shuffle = ff_sbr_qmf_post_shuffle_sse;
+        s->qmf_deint_bfly   = ff_sbr_qmf_deint_bfly_sse;
+    }
+
+    if (EXTERNAL_SSE2(mm_flags)) {
+        s->qmf_deint_bfly   = ff_sbr_qmf_deint_bfly_sse2;
+        s->qmf_pre_shuffle  = ff_sbr_qmf_pre_shuffle_sse2;
+#if ARCH_X86_32 || !defined(_WIN32)
+        s->hf_apply_noise[0] = ff_sbr_hf_apply_noise_0_sse2;
+        s->hf_apply_noise[1] = ff_sbr_hf_apply_noise_1_sse2;
+        s->hf_apply_noise[2] = ff_sbr_hf_apply_noise_2_sse2;
+        s->hf_apply_noise[3] = ff_sbr_hf_apply_noise_3_sse2;
+#endif
     }
 }
diff --git a/libavcodec/x86/vc1dsp_init.c b/libavcodec/x86/vc1dsp_init.c
index 3f0b67c..228f4dc 100644
--- a/libavcodec/x86/vc1dsp_init.c
+++ b/libavcodec/x86/vc1dsp_init.c
@@ -63,7 +63,7 @@
 }
 
 static void avg_vc1_mspel_mc00_mmxext(uint8_t *dst, const uint8_t *src,
-                                      int stride, int rnd)
+                                      ptrdiff_t stride, int rnd)
 {
     ff_avg_pixels8_mmxext(dst, src, stride, 8);
 }
diff --git a/libavcodec/x86/vc1dsp_mmx.c b/libavcodec/x86/vc1dsp_mmx.c
index d1e4b9d..92f3e20 100644
--- a/libavcodec/x86/vc1dsp_mmx.c
+++ b/libavcodec/x86/vc1dsp_mmx.c
@@ -462,12 +462,17 @@
 
 /** Macro to ease bicubic filter interpolation functions declarations */
 #define DECLARE_FUNCTION(a, b)                                          \
-static void put_vc1_mspel_mc ## a ## b ## _mmx(uint8_t *dst, const uint8_t *src, int stride, int rnd) { \
+static void put_vc1_mspel_mc ## a ## b ## _mmx(uint8_t *dst,            \
+                                               const uint8_t *src,      \
+                                               ptrdiff_t stride,        \
+                                               int rnd)                 \
+{                                                                       \
      put_vc1_mspel_mc(dst, src, stride, a, b, rnd);                     \
 }\
 static void avg_vc1_mspel_mc ## a ## b ## _mmxext(uint8_t *dst,         \
                                                   const uint8_t *src,   \
-                                                  int stride, int rnd)  \
+                                                  ptrdiff_t stride,     \
+                                                  int rnd)              \
 {                                                                       \
      avg_vc1_mspel_mc(dst, src, stride, a, b, rnd);                     \
 }
@@ -695,51 +700,51 @@
 
 av_cold void ff_vc1dsp_init_mmx(VC1DSPContext *dsp)
 {
-        dsp->put_vc1_mspel_pixels_tab[ 0] = ff_put_vc1_mspel_mc00_mmx;
-        dsp->put_vc1_mspel_pixels_tab[ 4] = put_vc1_mspel_mc01_mmx;
-        dsp->put_vc1_mspel_pixels_tab[ 8] = put_vc1_mspel_mc02_mmx;
-        dsp->put_vc1_mspel_pixels_tab[12] = put_vc1_mspel_mc03_mmx;
+    dsp->put_vc1_mspel_pixels_tab[ 0] = ff_put_vc1_mspel_mc00_mmx;
+    dsp->put_vc1_mspel_pixels_tab[ 4] = put_vc1_mspel_mc01_mmx;
+    dsp->put_vc1_mspel_pixels_tab[ 8] = put_vc1_mspel_mc02_mmx;
+    dsp->put_vc1_mspel_pixels_tab[12] = put_vc1_mspel_mc03_mmx;
 
-        dsp->put_vc1_mspel_pixels_tab[ 1] = put_vc1_mspel_mc10_mmx;
-        dsp->put_vc1_mspel_pixels_tab[ 5] = put_vc1_mspel_mc11_mmx;
-        dsp->put_vc1_mspel_pixels_tab[ 9] = put_vc1_mspel_mc12_mmx;
-        dsp->put_vc1_mspel_pixels_tab[13] = put_vc1_mspel_mc13_mmx;
+    dsp->put_vc1_mspel_pixels_tab[ 1] = put_vc1_mspel_mc10_mmx;
+    dsp->put_vc1_mspel_pixels_tab[ 5] = put_vc1_mspel_mc11_mmx;
+    dsp->put_vc1_mspel_pixels_tab[ 9] = put_vc1_mspel_mc12_mmx;
+    dsp->put_vc1_mspel_pixels_tab[13] = put_vc1_mspel_mc13_mmx;
 
-        dsp->put_vc1_mspel_pixels_tab[ 2] = put_vc1_mspel_mc20_mmx;
-        dsp->put_vc1_mspel_pixels_tab[ 6] = put_vc1_mspel_mc21_mmx;
-        dsp->put_vc1_mspel_pixels_tab[10] = put_vc1_mspel_mc22_mmx;
-        dsp->put_vc1_mspel_pixels_tab[14] = put_vc1_mspel_mc23_mmx;
+    dsp->put_vc1_mspel_pixels_tab[ 2] = put_vc1_mspel_mc20_mmx;
+    dsp->put_vc1_mspel_pixels_tab[ 6] = put_vc1_mspel_mc21_mmx;
+    dsp->put_vc1_mspel_pixels_tab[10] = put_vc1_mspel_mc22_mmx;
+    dsp->put_vc1_mspel_pixels_tab[14] = put_vc1_mspel_mc23_mmx;
 
-        dsp->put_vc1_mspel_pixels_tab[ 3] = put_vc1_mspel_mc30_mmx;
-        dsp->put_vc1_mspel_pixels_tab[ 7] = put_vc1_mspel_mc31_mmx;
-        dsp->put_vc1_mspel_pixels_tab[11] = put_vc1_mspel_mc32_mmx;
-        dsp->put_vc1_mspel_pixels_tab[15] = put_vc1_mspel_mc33_mmx;
+    dsp->put_vc1_mspel_pixels_tab[ 3] = put_vc1_mspel_mc30_mmx;
+    dsp->put_vc1_mspel_pixels_tab[ 7] = put_vc1_mspel_mc31_mmx;
+    dsp->put_vc1_mspel_pixels_tab[11] = put_vc1_mspel_mc32_mmx;
+    dsp->put_vc1_mspel_pixels_tab[15] = put_vc1_mspel_mc33_mmx;
 }
 
 av_cold void ff_vc1dsp_init_mmxext(VC1DSPContext *dsp)
 {
-        dsp->avg_vc1_mspel_pixels_tab[ 4] = avg_vc1_mspel_mc01_mmxext;
-        dsp->avg_vc1_mspel_pixels_tab[ 8] = avg_vc1_mspel_mc02_mmxext;
-        dsp->avg_vc1_mspel_pixels_tab[12] = avg_vc1_mspel_mc03_mmxext;
+    dsp->avg_vc1_mspel_pixels_tab[ 4] = avg_vc1_mspel_mc01_mmxext;
+    dsp->avg_vc1_mspel_pixels_tab[ 8] = avg_vc1_mspel_mc02_mmxext;
+    dsp->avg_vc1_mspel_pixels_tab[12] = avg_vc1_mspel_mc03_mmxext;
 
-        dsp->avg_vc1_mspel_pixels_tab[ 1] = avg_vc1_mspel_mc10_mmxext;
-        dsp->avg_vc1_mspel_pixels_tab[ 5] = avg_vc1_mspel_mc11_mmxext;
-        dsp->avg_vc1_mspel_pixels_tab[ 9] = avg_vc1_mspel_mc12_mmxext;
-        dsp->avg_vc1_mspel_pixels_tab[13] = avg_vc1_mspel_mc13_mmxext;
+    dsp->avg_vc1_mspel_pixels_tab[ 1] = avg_vc1_mspel_mc10_mmxext;
+    dsp->avg_vc1_mspel_pixels_tab[ 5] = avg_vc1_mspel_mc11_mmxext;
+    dsp->avg_vc1_mspel_pixels_tab[ 9] = avg_vc1_mspel_mc12_mmxext;
+    dsp->avg_vc1_mspel_pixels_tab[13] = avg_vc1_mspel_mc13_mmxext;
 
-        dsp->avg_vc1_mspel_pixels_tab[ 2] = avg_vc1_mspel_mc20_mmxext;
-        dsp->avg_vc1_mspel_pixels_tab[ 6] = avg_vc1_mspel_mc21_mmxext;
-        dsp->avg_vc1_mspel_pixels_tab[10] = avg_vc1_mspel_mc22_mmxext;
-        dsp->avg_vc1_mspel_pixels_tab[14] = avg_vc1_mspel_mc23_mmxext;
+    dsp->avg_vc1_mspel_pixels_tab[ 2] = avg_vc1_mspel_mc20_mmxext;
+    dsp->avg_vc1_mspel_pixels_tab[ 6] = avg_vc1_mspel_mc21_mmxext;
+    dsp->avg_vc1_mspel_pixels_tab[10] = avg_vc1_mspel_mc22_mmxext;
+    dsp->avg_vc1_mspel_pixels_tab[14] = avg_vc1_mspel_mc23_mmxext;
 
-        dsp->avg_vc1_mspel_pixels_tab[ 3] = avg_vc1_mspel_mc30_mmxext;
-        dsp->avg_vc1_mspel_pixels_tab[ 7] = avg_vc1_mspel_mc31_mmxext;
-        dsp->avg_vc1_mspel_pixels_tab[11] = avg_vc1_mspel_mc32_mmxext;
-        dsp->avg_vc1_mspel_pixels_tab[15] = avg_vc1_mspel_mc33_mmxext;
+    dsp->avg_vc1_mspel_pixels_tab[ 3] = avg_vc1_mspel_mc30_mmxext;
+    dsp->avg_vc1_mspel_pixels_tab[ 7] = avg_vc1_mspel_mc31_mmxext;
+    dsp->avg_vc1_mspel_pixels_tab[11] = avg_vc1_mspel_mc32_mmxext;
+    dsp->avg_vc1_mspel_pixels_tab[15] = avg_vc1_mspel_mc33_mmxext;
 
-        dsp->vc1_inv_trans_8x8_dc = vc1_inv_trans_8x8_dc_mmxext;
-        dsp->vc1_inv_trans_4x8_dc = vc1_inv_trans_4x8_dc_mmxext;
-        dsp->vc1_inv_trans_8x4_dc = vc1_inv_trans_8x4_dc_mmxext;
-        dsp->vc1_inv_trans_4x4_dc = vc1_inv_trans_4x4_dc_mmxext;
+    dsp->vc1_inv_trans_8x8_dc = vc1_inv_trans_8x8_dc_mmxext;
+    dsp->vc1_inv_trans_4x8_dc = vc1_inv_trans_4x8_dc_mmxext;
+    dsp->vc1_inv_trans_8x4_dc = vc1_inv_trans_8x4_dc_mmxext;
+    dsp->vc1_inv_trans_4x4_dc = vc1_inv_trans_4x4_dc_mmxext;
 }
 #endif /* HAVE_INLINE_ASM */
diff --git a/libavcodec/x86/videodsp.asm b/libavcodec/x86/videodsp.asm
index 0eb4721..3f2f01a 100644
--- a/libavcodec/x86/videodsp.asm
+++ b/libavcodec/x86/videodsp.asm
@@ -23,9 +23,9 @@
 
 SECTION .text
 
-; extern void ff_emu_edge_core(uint8_t *buf, const uint8_t *src, x86_reg linesize,
-;                              x86_reg start_y, x86_reg end_y, x86_reg block_h,
-;                              x86_reg start_x, x86_reg end_x, x86_reg block_w);
+; void ff_emu_edge_core(uint8_t *buf, const uint8_t *src, x86_reg linesize,
+;                       x86_reg start_y, x86_reg end_y, x86_reg block_h,
+;                       x86_reg start_x, x86_reg end_x, x86_reg block_w);
 ;
 ; The actual function itself is below. It basically wraps a very simple
 ; w = end_x - start_x
diff --git a/libavcodec/x86/vp3dsp.asm b/libavcodec/x86/vp3dsp.asm
index be5f21b..24496ae 100644
--- a/libavcodec/x86/vp3dsp.asm
+++ b/libavcodec/x86/vp3dsp.asm
@@ -33,12 +33,13 @@
                times 8 dw 25080
                times 8 dw 12785
 
+pb_7:  times 8 db 0x07
+pb_1F: times 8 db 0x1f
+pb_81: times 8 db 0x81
+
 cextern pb_1
 cextern pb_3
-cextern pb_7
-cextern pb_1F
 cextern pb_80
-cextern pb_81
 
 cextern pw_8
 
@@ -500,22 +501,22 @@
 
     ; at this point, function has completed dequantization + dezigzag +
     ; partial transposition; now do the idct itself
-%define I(x) [%1+16* x     ]
-%define J(x) [%1+16*(x-4)+8]
-    RowIDCT
-    Transpose
-
-%define I(x) [%1+16* x   +64]
-%define J(x) [%1+16*(x-4)+72]
-    RowIDCT
-    Transpose
-
 %define I(x) [%1+16*x]
 %define J(x) [%1+16*x]
-    ColumnIDCT
+    RowIDCT
+    Transpose
 
 %define I(x) [%1+16*x+8]
 %define J(x) [%1+16*x+8]
+    RowIDCT
+    Transpose
+
+%define I(x) [%1+16* x]
+%define J(x) [%1+16*(x-4)+8]
+    ColumnIDCT
+
+%define I(x) [%1+16* x   +64]
+%define J(x) [%1+16*(x-4)+72]
     ColumnIDCT
 %endif ; mmsize == 16/8
 %endmacro
@@ -533,10 +534,17 @@
     mova          m1, [r2+mmsize*2+%%i]
     mova          m2, [r2+mmsize*4+%%i]
     mova          m3, [r2+mmsize*6+%%i]
+%if mmsize == 8
+    packsswb      m0, [r2+mmsize*8+%%i]
+    packsswb      m1, [r2+mmsize*10+%%i]
+    packsswb      m2, [r2+mmsize*12+%%i]
+    packsswb      m3, [r2+mmsize*14+%%i]
+%else
     packsswb      m0, [r2+mmsize*1+%%i]
     packsswb      m1, [r2+mmsize*3+%%i]
     packsswb      m2, [r2+mmsize*5+%%i]
     packsswb      m3, [r2+mmsize*7+%%i]
+%endif
     paddb         m0, m4
     paddb         m1, m4
     paddb         m2, m4
@@ -560,7 +568,7 @@
     movq   [r0+r1*2], m3
     movhps [r0+r3  ], m3
 %endif
-%assign %%i %%i+64
+%assign %%i %%i+8
 %endrep
 
     pxor          m0, m0
@@ -574,47 +582,81 @@
 cglobal vp3_idct_add, 3, 4, 9
     VP3_IDCT      r2
 
-    mov           r3, 4
-    pxor          m4, m4
     movsxdifnidn  r1, r1d
-.loop:
+    lea           r3, [r1*3]
+    pxor          m4, m4
+%if mmsize == 16
+%assign %%i 0
+%rep 2
     movq          m0, [r0]
     movq          m1, [r0+r1]
-%if mmsize == 8
-    mova          m2, m0
-    mova          m3, m1
-%endif
+    movq          m2, [r0+r1*2]
+    movq          m3, [r0+r3]
     punpcklbw     m0, m4
     punpcklbw     m1, m4
-%if mmsize == 8
-    punpckhbw     m2, m4
-    punpckhbw     m3, m4
-%endif
-    paddsw        m0, [r2+ 0]
-    paddsw        m1, [r2+16]
-%if mmsize == 8
-    paddsw        m2, [r2+ 8]
-    paddsw        m3, [r2+24]
-    packuswb      m0, m2
-    packuswb      m1, m3
-%else ; mmsize == 16
+    punpcklbw     m2, m4
+    punpcklbw     m3, m4
+    paddsw        m0, [r2+ 0+%%i]
+    paddsw        m1, [r2+16+%%i]
+    paddsw        m2, [r2+32+%%i]
+    paddsw        m3, [r2+48+%%i]
     packuswb      m0, m1
+    packuswb      m2, m3
+    movq   [r0     ], m0
+    movhps [r0+r1  ], m0
+    movq   [r0+r1*2], m2
+    movhps [r0+r3  ], m2
+%if %%i == 0
+    lea           r0, [r0+r1*4]
 %endif
-    movq     [r0   ], m0
-%if mmsize == 8
-    movq     [r0+r1], m1
-%else ; mmsize == 16
-    movhps   [r0+r1], m0
-%endif
-    lea           r0, [r0+r1*2]
-%assign %%offset 0
-%rep 32/mmsize
-    mova [r2+%%offset], m4
-%assign %%offset %%offset+mmsize
+%assign %%i %%i+64
 %endrep
-    add           r2, 32
-    dec           r3
-    jg .loop
+%else
+%assign %%i 0
+%rep 2
+    movq          m0, [r0]
+    movq          m1, [r0+r1]
+    movq          m2, [r0+r1*2]
+    movq          m3, [r0+r3]
+    movq          m5, m0
+    movq          m6, m1
+    movq          m7, m2
+    punpcklbw     m0, m4
+    punpcklbw     m1, m4
+    punpcklbw     m2, m4
+    punpckhbw     m5, m4
+    punpckhbw     m6, m4
+    punpckhbw     m7, m4
+    paddsw        m0, [r2+ 0+%%i]
+    paddsw        m1, [r2+16+%%i]
+    paddsw        m2, [r2+32+%%i]
+    paddsw        m5, [r2+64+%%i]
+    paddsw        m6, [r2+80+%%i]
+    paddsw        m7, [r2+96+%%i]
+    packuswb      m0, m5
+    movq          m5, m3
+    punpcklbw     m3, m4
+    punpckhbw     m5, m4
+    packuswb      m1, m6
+    paddsw        m3, [r2+48+%%i]
+    paddsw        m5, [r2+112+%%i]
+    packuswb      m2, m7
+    packuswb      m3, m5
+    movq   [r0     ], m0
+    movq   [r0+r1  ], m1
+    movq   [r0+r1*2], m2
+    movq   [r0+r3  ], m3
+%if %%i == 0
+    lea           r0, [r0+r1*4]
+%endif
+%assign %%i %%i+8
+%endrep
+%endif
+%assign %%i 0
+%rep 128/mmsize
+    mova    [r2+%%i], m4
+%assign %%i %%i+mmsize
+%endrep
     RET
 %endmacro
 
diff --git a/libavcodec/x86/vp3dsp_init.c b/libavcodec/x86/vp3dsp_init.c
index 288dbcc..252b40a 100644
--- a/libavcodec/x86/vp3dsp_init.c
+++ b/libavcodec/x86/vp3dsp_init.c
@@ -109,7 +109,6 @@
     if (EXTERNAL_MMX(cpuflags)) {
         c->idct_put  = ff_vp3_idct_put_mmx;
         c->idct_add  = ff_vp3_idct_add_mmx;
-        c->idct_perm = FF_PARTTRANS_IDCT_PERM;
     }
 #endif
 
@@ -125,6 +124,5 @@
     if (EXTERNAL_SSE2(cpuflags)) {
         c->idct_put  = ff_vp3_idct_put_sse2;
         c->idct_add  = ff_vp3_idct_add_sse2;
-        c->idct_perm = FF_TRANSPOSE_IDCT_PERM;
     }
 }
diff --git a/libavcodec/x86/vp8dsp.asm b/libavcodec/x86/vp8dsp.asm
index c5fb455..ca07333 100644
--- a/libavcodec/x86/vp8dsp.asm
+++ b/libavcodec/x86/vp8dsp.asm
@@ -143,11 +143,15 @@
 filter_h6_shuf2: db 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,  7, 7,  8,  8,  9
 filter_h6_shuf3: db 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,  9, 9, 10, 10, 11
 
-pw_256:  times 8 dw 256
-
+pw_27:    times 8 dw 27
+pw_63:    times 8 dw 63
+pw_256:   times 8 dw 256
 pw_20091: times 4 dw 20091
 pw_17734: times 4 dw 17734
 
+pb_4:     times 16 db 4
+pb_F8:    times 16 db 0xF8
+pb_FE:    times 16 db 0xFE
 pb_27_63: times 8 db 27, 63
 pb_18_63: times 8 db 18, 63
 pb_9_63:  times 8 db  9, 63
@@ -156,15 +160,10 @@
 cextern pw_3
 cextern pb_3
 cextern pw_4
-cextern pb_4
 cextern pw_9
 cextern pw_18
-cextern pw_27
-cextern pw_63
 cextern pw_64
 cextern pb_80
-cextern pb_F8
-cextern pb_FE
 
 SECTION .text
 
diff --git a/libavcodec/x86/vp8dsp_init.c b/libavcodec/x86/vp8dsp_init.c
index 09e2d91..745d95a 100644
--- a/libavcodec/x86/vp8dsp_init.c
+++ b/libavcodec/x86/vp8dsp_init.c
@@ -30,93 +30,93 @@
 /*
  * MC functions
  */
-extern void ff_put_vp8_epel4_h4_mmxext(uint8_t *dst, ptrdiff_t dststride,
-                                       uint8_t *src, ptrdiff_t srcstride,
-                                       int height, int mx, int my);
-extern void ff_put_vp8_epel4_h6_mmxext(uint8_t *dst, ptrdiff_t dststride,
-                                       uint8_t *src, ptrdiff_t srcstride,
-                                       int height, int mx, int my);
-extern void ff_put_vp8_epel4_v4_mmxext(uint8_t *dst, ptrdiff_t dststride,
-                                       uint8_t *src, ptrdiff_t srcstride,
-                                       int height, int mx, int my);
-extern void ff_put_vp8_epel4_v6_mmxext(uint8_t *dst, ptrdiff_t dststride,
-                                       uint8_t *src, ptrdiff_t srcstride,
-                                       int height, int mx, int my);
+void ff_put_vp8_epel4_h4_mmxext(uint8_t *dst, ptrdiff_t dststride,
+                                uint8_t *src, ptrdiff_t srcstride,
+                                int height, int mx, int my);
+void ff_put_vp8_epel4_h6_mmxext(uint8_t *dst, ptrdiff_t dststride,
+                                uint8_t *src, ptrdiff_t srcstride,
+                                int height, int mx, int my);
+void ff_put_vp8_epel4_v4_mmxext(uint8_t *dst, ptrdiff_t dststride,
+                                uint8_t *src, ptrdiff_t srcstride,
+                                int height, int mx, int my);
+void ff_put_vp8_epel4_v6_mmxext(uint8_t *dst, ptrdiff_t dststride,
+                                uint8_t *src, ptrdiff_t srcstride,
+                                int height, int mx, int my);
 
-extern void ff_put_vp8_epel8_h4_sse2  (uint8_t *dst, ptrdiff_t dststride,
-                                       uint8_t *src, ptrdiff_t srcstride,
-                                       int height, int mx, int my);
-extern void ff_put_vp8_epel8_h6_sse2  (uint8_t *dst, ptrdiff_t dststride,
-                                       uint8_t *src, ptrdiff_t srcstride,
-                                       int height, int mx, int my);
-extern void ff_put_vp8_epel8_v4_sse2  (uint8_t *dst, ptrdiff_t dststride,
-                                       uint8_t *src, ptrdiff_t srcstride,
-                                       int height, int mx, int my);
-extern void ff_put_vp8_epel8_v6_sse2  (uint8_t *dst, ptrdiff_t dststride,
-                                       uint8_t *src, ptrdiff_t srcstride,
-                                       int height, int mx, int my);
+void ff_put_vp8_epel8_h4_sse2  (uint8_t *dst, ptrdiff_t dststride,
+                                uint8_t *src, ptrdiff_t srcstride,
+                                int height, int mx, int my);
+void ff_put_vp8_epel8_h6_sse2  (uint8_t *dst, ptrdiff_t dststride,
+                                uint8_t *src, ptrdiff_t srcstride,
+                                int height, int mx, int my);
+void ff_put_vp8_epel8_v4_sse2  (uint8_t *dst, ptrdiff_t dststride,
+                                uint8_t *src, ptrdiff_t srcstride,
+                                int height, int mx, int my);
+void ff_put_vp8_epel8_v6_sse2  (uint8_t *dst, ptrdiff_t dststride,
+                                uint8_t *src, ptrdiff_t srcstride,
+                                int height, int mx, int my);
 
-extern void ff_put_vp8_epel4_h4_ssse3 (uint8_t *dst, ptrdiff_t dststride,
-                                       uint8_t *src, ptrdiff_t srcstride,
-                                       int height, int mx, int my);
-extern void ff_put_vp8_epel4_h6_ssse3 (uint8_t *dst, ptrdiff_t dststride,
-                                       uint8_t *src, ptrdiff_t srcstride,
-                                       int height, int mx, int my);
-extern void ff_put_vp8_epel4_v4_ssse3 (uint8_t *dst, ptrdiff_t dststride,
-                                       uint8_t *src, ptrdiff_t srcstride,
-                                       int height, int mx, int my);
-extern void ff_put_vp8_epel4_v6_ssse3 (uint8_t *dst, ptrdiff_t dststride,
-                                       uint8_t *src, ptrdiff_t srcstride,
-                                       int height, int mx, int my);
-extern void ff_put_vp8_epel8_h4_ssse3 (uint8_t *dst, ptrdiff_t dststride,
-                                       uint8_t *src, ptrdiff_t srcstride,
-                                       int height, int mx, int my);
-extern void ff_put_vp8_epel8_h6_ssse3 (uint8_t *dst, ptrdiff_t dststride,
-                                       uint8_t *src, ptrdiff_t srcstride,
-                                       int height, int mx, int my);
-extern void ff_put_vp8_epel8_v4_ssse3 (uint8_t *dst, ptrdiff_t dststride,
-                                       uint8_t *src, ptrdiff_t srcstride,
-                                       int height, int mx, int my);
-extern void ff_put_vp8_epel8_v6_ssse3 (uint8_t *dst, ptrdiff_t dststride,
-                                       uint8_t *src, ptrdiff_t srcstride,
-                                       int height, int mx, int my);
+void ff_put_vp8_epel4_h4_ssse3 (uint8_t *dst, ptrdiff_t dststride,
+                                uint8_t *src, ptrdiff_t srcstride,
+                                int height, int mx, int my);
+void ff_put_vp8_epel4_h6_ssse3 (uint8_t *dst, ptrdiff_t dststride,
+                                uint8_t *src, ptrdiff_t srcstride,
+                                int height, int mx, int my);
+void ff_put_vp8_epel4_v4_ssse3 (uint8_t *dst, ptrdiff_t dststride,
+                                uint8_t *src, ptrdiff_t srcstride,
+                                int height, int mx, int my);
+void ff_put_vp8_epel4_v6_ssse3 (uint8_t *dst, ptrdiff_t dststride,
+                                uint8_t *src, ptrdiff_t srcstride,
+                                int height, int mx, int my);
+void ff_put_vp8_epel8_h4_ssse3 (uint8_t *dst, ptrdiff_t dststride,
+                                uint8_t *src, ptrdiff_t srcstride,
+                                int height, int mx, int my);
+void ff_put_vp8_epel8_h6_ssse3 (uint8_t *dst, ptrdiff_t dststride,
+                                uint8_t *src, ptrdiff_t srcstride,
+                                int height, int mx, int my);
+void ff_put_vp8_epel8_v4_ssse3 (uint8_t *dst, ptrdiff_t dststride,
+                                uint8_t *src, ptrdiff_t srcstride,
+                                int height, int mx, int my);
+void ff_put_vp8_epel8_v6_ssse3 (uint8_t *dst, ptrdiff_t dststride,
+                                uint8_t *src, ptrdiff_t srcstride,
+                                int height, int mx, int my);
 
-extern void ff_put_vp8_bilinear4_h_mmxext(uint8_t *dst, ptrdiff_t dststride,
-                                          uint8_t *src, ptrdiff_t srcstride,
-                                          int height, int mx, int my);
-extern void ff_put_vp8_bilinear8_h_sse2  (uint8_t *dst, ptrdiff_t dststride,
-                                          uint8_t *src, ptrdiff_t srcstride,
-                                          int height, int mx, int my);
-extern void ff_put_vp8_bilinear4_h_ssse3 (uint8_t *dst, ptrdiff_t dststride,
-                                          uint8_t *src, ptrdiff_t srcstride,
-                                          int height, int mx, int my);
-extern void ff_put_vp8_bilinear8_h_ssse3 (uint8_t *dst, ptrdiff_t dststride,
-                                          uint8_t *src, ptrdiff_t srcstride,
-                                          int height, int mx, int my);
+void ff_put_vp8_bilinear4_h_mmxext(uint8_t *dst, ptrdiff_t dststride,
+                                   uint8_t *src, ptrdiff_t srcstride,
+                                   int height, int mx, int my);
+void ff_put_vp8_bilinear8_h_sse2  (uint8_t *dst, ptrdiff_t dststride,
+                                   uint8_t *src, ptrdiff_t srcstride,
+                                   int height, int mx, int my);
+void ff_put_vp8_bilinear4_h_ssse3 (uint8_t *dst, ptrdiff_t dststride,
+                                   uint8_t *src, ptrdiff_t srcstride,
+                                   int height, int mx, int my);
+void ff_put_vp8_bilinear8_h_ssse3 (uint8_t *dst, ptrdiff_t dststride,
+                                   uint8_t *src, ptrdiff_t srcstride,
+                                   int height, int mx, int my);
 
-extern void ff_put_vp8_bilinear4_v_mmxext(uint8_t *dst, ptrdiff_t dststride,
-                                          uint8_t *src, ptrdiff_t srcstride,
-                                          int height, int mx, int my);
-extern void ff_put_vp8_bilinear8_v_sse2  (uint8_t *dst, ptrdiff_t dststride,
-                                          uint8_t *src, ptrdiff_t srcstride,
-                                          int height, int mx, int my);
-extern void ff_put_vp8_bilinear4_v_ssse3 (uint8_t *dst, ptrdiff_t dststride,
-                                          uint8_t *src, ptrdiff_t srcstride,
-                                          int height, int mx, int my);
-extern void ff_put_vp8_bilinear8_v_ssse3 (uint8_t *dst, ptrdiff_t dststride,
-                                          uint8_t *src, ptrdiff_t srcstride,
-                                          int height, int mx, int my);
+void ff_put_vp8_bilinear4_v_mmxext(uint8_t *dst, ptrdiff_t dststride,
+                                   uint8_t *src, ptrdiff_t srcstride,
+                                   int height, int mx, int my);
+void ff_put_vp8_bilinear8_v_sse2  (uint8_t *dst, ptrdiff_t dststride,
+                                   uint8_t *src, ptrdiff_t srcstride,
+                                   int height, int mx, int my);
+void ff_put_vp8_bilinear4_v_ssse3 (uint8_t *dst, ptrdiff_t dststride,
+                                   uint8_t *src, ptrdiff_t srcstride,
+                                   int height, int mx, int my);
+void ff_put_vp8_bilinear8_v_ssse3 (uint8_t *dst, ptrdiff_t dststride,
+                                   uint8_t *src, ptrdiff_t srcstride,
+                                   int height, int mx, int my);
 
 
-extern void ff_put_vp8_pixels8_mmx (uint8_t *dst, ptrdiff_t dststride,
-                                    uint8_t *src, ptrdiff_t srcstride,
-                                    int height, int mx, int my);
-extern void ff_put_vp8_pixels16_mmx(uint8_t *dst, ptrdiff_t dststride,
-                                    uint8_t *src, ptrdiff_t srcstride,
-                                    int height, int mx, int my);
-extern void ff_put_vp8_pixels16_sse(uint8_t *dst, ptrdiff_t dststride,
-                                    uint8_t *src, ptrdiff_t srcstride,
-                                    int height, int mx, int my);
+void ff_put_vp8_pixels8_mmx (uint8_t *dst, ptrdiff_t dststride,
+                             uint8_t *src, ptrdiff_t srcstride,
+                             int height, int mx, int my);
+void ff_put_vp8_pixels16_mmx(uint8_t *dst, ptrdiff_t dststride,
+                             uint8_t *src, ptrdiff_t srcstride,
+                             int height, int mx, int my);
+void ff_put_vp8_pixels16_sse(uint8_t *dst, ptrdiff_t dststride,
+                             uint8_t *src, ptrdiff_t srcstride,
+                             int height, int mx, int my);
 
 #define TAP_W16(OPT, FILTERTYPE, TAPTYPE) \
 static void ff_put_vp8_ ## FILTERTYPE ## 16_ ## TAPTYPE ## _ ## OPT( \
@@ -230,58 +230,56 @@
 HVBILIN(ssse3, 8,  8, 16)
 HVBILIN(ssse3, 8, 16, 16)
 
-extern void ff_vp8_idct_dc_add_mmx(uint8_t *dst, int16_t block[16],
-                                   ptrdiff_t stride);
-extern void ff_vp8_idct_dc_add_sse4(uint8_t *dst, int16_t block[16],
-                                    ptrdiff_t stride);
-extern void ff_vp8_idct_dc_add4y_mmx(uint8_t *dst, int16_t block[4][16],
-                                      ptrdiff_t stride);
-extern void ff_vp8_idct_dc_add4y_sse2(uint8_t *dst, int16_t block[4][16],
-                                      ptrdiff_t stride);
-extern void ff_vp8_idct_dc_add4uv_mmx(uint8_t *dst, int16_t block[2][16],
-                                      ptrdiff_t stride);
-extern void ff_vp8_luma_dc_wht_mmx(int16_t block[4][4][16], int16_t dc[16]);
-extern void ff_vp8_luma_dc_wht_sse(int16_t block[4][4][16], int16_t dc[16]);
-extern void ff_vp8_idct_add_mmx(uint8_t *dst, int16_t block[16],
-                                ptrdiff_t stride);
-extern void ff_vp8_idct_add_sse(uint8_t *dst, int16_t block[16],
-                                ptrdiff_t stride);
+void ff_vp8_idct_dc_add_mmx(uint8_t *dst, int16_t block[16],
+                            ptrdiff_t stride);
+void ff_vp8_idct_dc_add_sse4(uint8_t *dst, int16_t block[16],
+                             ptrdiff_t stride);
+void ff_vp8_idct_dc_add4y_mmx(uint8_t *dst, int16_t block[4][16],
+                               ptrdiff_t stride);
+void ff_vp8_idct_dc_add4y_sse2(uint8_t *dst, int16_t block[4][16],
+                               ptrdiff_t stride);
+void ff_vp8_idct_dc_add4uv_mmx(uint8_t *dst, int16_t block[2][16],
+                               ptrdiff_t stride);
+void ff_vp8_luma_dc_wht_mmx(int16_t block[4][4][16], int16_t dc[16]);
+void ff_vp8_luma_dc_wht_sse(int16_t block[4][4][16], int16_t dc[16]);
+void ff_vp8_idct_add_mmx(uint8_t *dst, int16_t block[16], ptrdiff_t stride);
+void ff_vp8_idct_add_sse(uint8_t *dst, int16_t block[16], ptrdiff_t stride);
 
-#define DECLARE_LOOP_FILTER(NAME)\
-extern void ff_vp8_v_loop_filter_simple_ ## NAME(uint8_t *dst, \
-                                                 ptrdiff_t stride, \
-                                                 int flim);\
-extern void ff_vp8_h_loop_filter_simple_ ## NAME(uint8_t *dst, \
-                                                 ptrdiff_t stride, \
-                                                 int flim);\
-extern void ff_vp8_v_loop_filter16y_inner_ ## NAME (uint8_t *dst, \
-                                                     ptrdiff_t stride,\
-                                                    int e, int i, int hvt);\
-extern void ff_vp8_h_loop_filter16y_inner_ ## NAME (uint8_t *dst, \
-                                                    ptrdiff_t stride,\
-                                                    int e, int i, int hvt);\
-extern void ff_vp8_v_loop_filter8uv_inner_ ## NAME (uint8_t *dstU, \
-                                                    uint8_t *dstV,\
-                                                    ptrdiff_t s, \
-                                                    int e, int i, int hvt);\
-extern void ff_vp8_h_loop_filter8uv_inner_ ## NAME (uint8_t *dstU, \
-                                                    uint8_t *dstV,\
-                                                    ptrdiff_t s, \
-                                                    int e, int i, int hvt);\
-extern void ff_vp8_v_loop_filter16y_mbedge_ ## NAME(uint8_t *dst, \
-                                                    ptrdiff_t stride,\
-                                                    int e, int i, int hvt);\
-extern void ff_vp8_h_loop_filter16y_mbedge_ ## NAME(uint8_t *dst, \
-                                                    ptrdiff_t stride,\
-                                                    int e, int i, int hvt);\
-extern void ff_vp8_v_loop_filter8uv_mbedge_ ## NAME(uint8_t *dstU, \
-                                                    uint8_t *dstV,\
-                                                    ptrdiff_t s, \
-                                                    int e, int i, int hvt);\
-extern void ff_vp8_h_loop_filter8uv_mbedge_ ## NAME(uint8_t *dstU, \
-                                                    uint8_t *dstV,\
-                                                    ptrdiff_t s, \
-                                                    int e, int i, int hvt);
+#define DECLARE_LOOP_FILTER(NAME)                                       \
+void ff_vp8_v_loop_filter_simple_ ## NAME(uint8_t *dst,                 \
+                                          ptrdiff_t stride,             \
+                                          int flim);                    \
+void ff_vp8_h_loop_filter_simple_ ## NAME(uint8_t *dst,                 \
+                                          ptrdiff_t stride,             \
+                                          int flim);                    \
+void ff_vp8_v_loop_filter16y_inner_ ## NAME (uint8_t *dst,              \
+                                             ptrdiff_t stride,          \
+                                             int e, int i, int hvt);    \
+void ff_vp8_h_loop_filter16y_inner_ ## NAME (uint8_t *dst,              \
+                                             ptrdiff_t stride,          \
+                                             int e, int i, int hvt);    \
+void ff_vp8_v_loop_filter8uv_inner_ ## NAME (uint8_t *dstU,             \
+                                             uint8_t *dstV,             \
+                                             ptrdiff_t s,               \
+                                             int e, int i, int hvt);    \
+void ff_vp8_h_loop_filter8uv_inner_ ## NAME (uint8_t *dstU,             \
+                                             uint8_t *dstV,             \
+                                             ptrdiff_t s,               \
+                                             int e, int i, int hvt);    \
+void ff_vp8_v_loop_filter16y_mbedge_ ## NAME(uint8_t *dst,              \
+                                             ptrdiff_t stride,          \
+                                             int e, int i, int hvt);    \
+void ff_vp8_h_loop_filter16y_mbedge_ ## NAME(uint8_t *dst,              \
+                                             ptrdiff_t stride,          \
+                                             int e, int i, int hvt);    \
+void ff_vp8_v_loop_filter8uv_mbedge_ ## NAME(uint8_t *dstU,             \
+                                             uint8_t *dstV,             \
+                                             ptrdiff_t s,               \
+                                             int e, int i, int hvt);    \
+void ff_vp8_h_loop_filter8uv_mbedge_ ## NAME(uint8_t *dstU,             \
+                                             uint8_t *dstV,             \
+                                             ptrdiff_t s,               \
+                                             int e, int i, int hvt);
 
 DECLARE_LOOP_FILTER(mmx)
 DECLARE_LOOP_FILTER(mmxext)
diff --git a/libavcodec/xan.c b/libavcodec/xan.c
index 219eedd..41925aa 100644
--- a/libavcodec/xan.c
+++ b/libavcodec/xan.c
@@ -52,8 +52,7 @@
 typedef struct XanContext {
 
     AVCodecContext *avctx;
-    AVFrame last_frame;
-    AVFrame current_frame;
+    AVFrame *last_frame;
 
     const unsigned char *buf;
     int size;
@@ -72,6 +71,8 @@
 
 } XanContext;
 
+static av_cold int xan_decode_end(AVCodecContext *avctx);
+
 static av_cold int xan_decode_init(AVCodecContext *avctx)
 {
     XanContext *s = avctx->priv_data;
@@ -91,8 +92,11 @@
         av_freep(&s->buffer1);
         return AVERROR(ENOMEM);
     }
-    avcodec_get_frame_defaults(&s->last_frame);
-    avcodec_get_frame_defaults(&s->current_frame);
+    s->last_frame = av_frame_alloc();
+    if (!s->last_frame) {
+        xan_decode_end(avctx);
+        return AVERROR(ENOMEM);
+    }
 
     return 0;
 }
@@ -189,7 +193,7 @@
     }
 }
 
-static inline void xan_wc3_output_pixel_run(XanContext *s,
+static inline void xan_wc3_output_pixel_run(XanContext *s, AVFrame *frame,
     const unsigned char *pixel_buffer, int x, int y, int pixel_count)
 {
     int stride;
@@ -199,8 +203,8 @@
     int width = s->avctx->width;
     unsigned char *palette_plane;
 
-    palette_plane = s->current_frame.data[0];
-    stride = s->current_frame.linesize[0];
+    palette_plane = frame->data[0];
+    stride = frame->linesize[0];
     line_inc = stride - width;
     index = y * stride + x;
     current_x = x;
@@ -219,7 +223,8 @@
     }
 }
 
-static inline void xan_wc3_copy_pixel_run(XanContext *s, int x, int y,
+static inline void xan_wc3_copy_pixel_run(XanContext *s, AVFrame *frame,
+                                          int x, int y,
                                           int pixel_count, int motion_x,
                                           int motion_y)
 {
@@ -234,16 +239,22 @@
         x + motion_x < 0 || x + motion_x >= s->avctx->width)
         return;
 
-    palette_plane = s->current_frame.data[0];
-    prev_palette_plane = s->last_frame.data[0];
+    palette_plane = frame->data[0];
+    prev_palette_plane = s->last_frame->data[0];
     if (!prev_palette_plane)
         prev_palette_plane = palette_plane;
-    stride = s->current_frame.linesize[0];
+    stride = frame->linesize[0];
     line_inc = stride - width;
     curframe_index = y * stride + x;
     curframe_x = x;
     prevframe_index = (y + motion_y) * stride + x + motion_x;
     prevframe_x = x + motion_x;
+
+    if (prev_palette_plane == palette_plane && FFABS(curframe_index - prevframe_index) < pixel_count) {
+         avpriv_request_sample(s->avctx, "Overlapping copy\n");
+         return ;
+    }
+
     while (pixel_count &&
            curframe_index  < s->frame_size &&
            prevframe_index < s->frame_size) {
@@ -270,7 +281,8 @@
     }
 }
 
-static int xan_wc3_decode_frame(XanContext *s) {
+static int xan_wc3_decode_frame(XanContext *s, AVFrame *frame)
+{
 
     int width  = s->avctx->width;
     int height = s->avctx->height;
@@ -398,12 +410,12 @@
             flag ^= 1;
             if (flag) {
                 /* run of (size) pixels is unchanged from last frame */
-                xan_wc3_copy_pixel_run(s, x, y, size, 0, 0);
+                xan_wc3_copy_pixel_run(s, frame, x, y, size, 0, 0);
             } else {
                 /* output a run of pixels from imagedata_buffer */
                 if (imagedata_size < size)
                     break;
-                xan_wc3_output_pixel_run(s, imagedata_buffer, x, y, size);
+                xan_wc3_output_pixel_run(s, frame, imagedata_buffer, x, y, size);
                 imagedata_buffer += size;
                 imagedata_size -= size;
             }
@@ -418,7 +430,7 @@
             vector_segment++;
 
             /* copy a run of pixels from the previous frame */
-            xan_wc3_copy_pixel_run(s, x, y, size, motion_x, motion_y);
+            xan_wc3_copy_pixel_run(s, frame, x, y, size, motion_x, motion_y);
 
             flag = 0;
         }
@@ -518,6 +530,7 @@
                             void *data, int *got_frame,
                             AVPacket *avpkt)
 {
+    AVFrame *frame = data;
     const uint8_t *buf = avpkt->data;
     int ret, buf_size = avpkt->size;
     XanContext *s = avctx->priv_data;
@@ -586,33 +599,26 @@
         return AVERROR_INVALIDDATA;
     }
 
-    if ((ret = ff_get_buffer(avctx, &s->current_frame))) {
-        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
         return ret;
-    }
-    s->current_frame.reference = 3;
 
     if (!s->frame_size)
-        s->frame_size = s->current_frame.linesize[0] * s->avctx->height;
+        s->frame_size = frame->linesize[0] * s->avctx->height;
 
-    memcpy(s->current_frame.data[1],
+    memcpy(frame->data[1],
            s->palettes + s->cur_palette * AVPALETTE_COUNT, AVPALETTE_SIZE);
 
     s->buf = ctx.buffer;
     s->size = buf_size;
 
-    if (xan_wc3_decode_frame(s) < 0)
+    if (xan_wc3_decode_frame(s, frame) < 0)
         return AVERROR_INVALIDDATA;
 
-    /* release the last frame if it is allocated */
-    if (s->last_frame.data[0])
-        avctx->release_buffer(avctx, &s->last_frame);
+    av_frame_unref(s->last_frame);
+    if ((ret = av_frame_ref(s->last_frame, frame)) < 0)
+        return ret;
 
     *got_frame = 1;
-    *(AVFrame*)data = s->current_frame;
-
-    /* shuffle frames */
-    FFSWAP(AVFrame, s->current_frame, s->last_frame);
 
     /* always report that the buffer was completely consumed */
     return buf_size;
@@ -622,11 +628,7 @@
 {
     XanContext *s = avctx->priv_data;
 
-    /* release the frames */
-    if (s->last_frame.data[0])
-        avctx->release_buffer(avctx, &s->last_frame);
-    if (s->current_frame.data[0])
-        avctx->release_buffer(avctx, &s->current_frame);
+    av_frame_free(&s->last_frame);
 
     av_freep(&s->buffer1);
     av_freep(&s->buffer2);
diff --git a/libavcodec/xbmdec.c b/libavcodec/xbmdec.c
index 2a41836..24fe550 100644
--- a/libavcodec/xbmdec.c
+++ b/libavcodec/xbmdec.c
@@ -23,12 +23,11 @@
 #include "avcodec.h"
 #include "internal.h"
 #include "mathops.h"
+#include "libavutil/avstring.h"
 
 static av_cold int xbm_decode_init(AVCodecContext *avctx)
 {
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame)
-        return AVERROR(ENOMEM);
+    avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
 
     return 0;
 }
@@ -47,7 +46,7 @@
 static int xbm_decode_frame(AVCodecContext *avctx, void *data,
                             int *got_frame, AVPacket *avpkt)
 {
-    AVFrame *p = avctx->coded_frame;
+    AVFrame *p = data;
     const uint8_t *end, *ptr = avpkt->data;
     uint8_t *dst;
     int ret, linesize, i, j;
@@ -75,13 +74,7 @@
         ptr += strcspn(ptr, "\n\r") + 1;
     }
 
-    avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
-
-    if (p->data[0])
-        avctx->release_buffer(avctx, p);
-
-    p->reference = 0;
-    if ((ret = ff_get_buffer(avctx, p)) < 0)
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
         return ret;
 
     // goto start of image data
@@ -94,10 +87,10 @@
             uint8_t val;
 
             ptr += strcspn(ptr, "x") + 1;
-            if (ptr < end && isxdigit(*ptr)) {
+            if (ptr < end && av_isxdigit(*ptr)) {
                 val = convert(*ptr);
                 ptr++;
-                if (isxdigit(*ptr))
+                if (av_isxdigit(*ptr))
                     val = (val << 4) + convert(*ptr);
                 *dst++ = ff_reverse[val];
             } else {
@@ -111,27 +104,15 @@
     p->pict_type = AV_PICTURE_TYPE_I;
 
     *got_frame       = 1;
-    *(AVFrame *)data = *p;
 
     return avpkt->size;
 }
 
-static av_cold int xbm_decode_close(AVCodecContext *avctx)
-{
-    if (avctx->coded_frame->data[0])
-        avctx->release_buffer(avctx, avctx->coded_frame);
-
-    av_freep(&avctx->coded_frame);
-
-    return 0;
-}
-
 AVCodec ff_xbm_decoder = {
     .name         = "xbm",
     .type         = AVMEDIA_TYPE_VIDEO,
     .id           = AV_CODEC_ID_XBM,
     .init         = xbm_decode_init,
-    .close        = xbm_decode_close,
     .decode       = xbm_decode_frame,
     .capabilities = CODEC_CAP_DR1,
     .long_name    = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
diff --git a/libavcodec/xbmenc.c b/libavcodec/xbmenc.c
index 8e39969..e7cec1d 100644
--- a/libavcodec/xbmenc.c
+++ b/libavcodec/xbmenc.c
@@ -24,16 +24,6 @@
 #include "internal.h"
 #include "mathops.h"
 
-static av_cold int xbm_encode_init(AVCodecContext *avctx)
-{
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame)
-        return AVERROR(ENOMEM);
-    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
-
-    return 0;
-}
-
 static int xbm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
                             const AVFrame *p, int *got_packet)
 {
@@ -65,20 +55,11 @@
     return 0;
 }
 
-static av_cold int xbm_encode_close(AVCodecContext *avctx)
-{
-    av_freep(&avctx->coded_frame);
-
-    return 0;
-}
-
 AVCodec ff_xbm_encoder = {
     .name         = "xbm",
     .type         = AVMEDIA_TYPE_VIDEO,
     .id           = AV_CODEC_ID_XBM,
-    .init         = xbm_encode_init,
     .encode2      = xbm_encode_frame,
-    .close        = xbm_encode_close,
     .pix_fmts     = (const enum AVPixelFormat[]) { AV_PIX_FMT_MONOWHITE,
                                                    AV_PIX_FMT_NONE },
     .long_name    = NULL_IF_CONFIG_SMALL("XBM (X BitMap) image"),
diff --git a/libavcodec/xfacedec.c b/libavcodec/xfacedec.c
index a2f8636..fbaabdb 100644
--- a/libavcodec/xfacedec.c
+++ b/libavcodec/xfacedec.c
@@ -87,16 +87,11 @@
 }
 
 typedef struct XFaceContext {
-    AVFrame frame;
     uint8_t bitmap[XFACE_PIXELS]; ///< image used internally for decoding
 } XFaceContext;
 
 static av_cold int xface_decode_init(AVCodecContext *avctx)
 {
-    XFaceContext *xface = avctx->priv_data;
-
-    avcodec_get_frame_defaults(&xface->frame);
-
     if (avctx->width || avctx->height) {
         if (avctx->width != XFACE_WIDTH || avctx->height != XFACE_HEIGHT) {
             av_log(avctx, AV_LOG_ERROR,
@@ -113,16 +108,6 @@
     return 0;
 }
 
-static av_cold int xface_decode_close(AVCodecContext *avctx)
-{
-    XFaceContext *xface = avctx->priv_data;
-
-    if (xface->frame.data[0])
-        avctx->release_buffer(avctx, &xface->frame);
-
-    return 0;
-}
-
 static int xface_decode_frame(AVCodecContext *avctx,
                               void *data, int *got_frame,
                               AVPacket *avpkt)
@@ -133,13 +118,10 @@
     BigInt b = {0};
     char *buf;
     int64_t c;
+    AVFrame *frame = data;
 
-    if (xface->frame.data[0])
-        avctx->release_buffer(avctx, &xface->frame);
-    xface->frame.data[0] = NULL;
-    if ((ret = ff_get_buffer(avctx, &xface->frame)) < 0)
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
         return ret;
-    xface->frame.reference = 0;
 
     for (i = 0, k = 0; avpkt->data[i] && i < avpkt->size; i++) {
         c = avpkt->data[i];
@@ -173,7 +155,7 @@
     ff_xface_generate_face(xface->bitmap, xface->bitmap);
 
     /* convert image from 1=black 0=white bitmap to MONOWHITE */
-    buf = xface->frame.data[0];
+    buf = frame->data[0];
     for (i = 0, j = 0, k = 0, byte = 0; i < XFACE_PIXELS; i++) {
         byte += xface->bitmap[i];
         if (k == 7) {
@@ -185,12 +167,11 @@
         }
         if (j == XFACE_WIDTH/8) {
             j = 0;
-            buf += xface->frame.linesize[0];
+            buf += frame->linesize[0];
         }
     }
 
     *got_frame = 1;
-    *(AVFrame*)data = xface->frame;
 
     return avpkt->size;
 }
@@ -201,7 +182,6 @@
     .id             = AV_CODEC_ID_XFACE,
     .priv_data_size = sizeof(XFaceContext),
     .init           = xface_decode_init,
-    .close          = xface_decode_close,
     .decode         = xface_decode_frame,
     .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_MONOWHITE, AV_PIX_FMT_NONE },
     .long_name      = NULL_IF_CONFIG_SMALL("X-face image"),
diff --git a/libavcodec/xl.c b/libavcodec/xl.c
index e2701d6..f50b650 100644
--- a/libavcodec/xl.c
+++ b/libavcodec/xl.c
@@ -29,11 +29,6 @@
 #include "avcodec.h"
 #include "internal.h"
 
-typedef struct VideoXLContext{
-    AVCodecContext *avctx;
-    AVFrame pic;
-} VideoXLContext;
-
 static const int xl_table[32] = {
    0,   1,   2,   3,   4,   5,   6,   7,
    8,   9,  12,  15,  20,  25,  34,  46,
@@ -47,8 +42,7 @@
 {
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
-    VideoXLContext * const a = avctx->priv_data;
-    AVFrame * const p = &a->pic;
+    AVFrame * const p = data;
     uint8_t *Y, *U, *V;
     int i, j, ret;
     int stride;
@@ -65,20 +59,14 @@
         return AVERROR_INVALIDDATA;
     }
 
-    if (p->data[0])
-        avctx->release_buffer(avctx, p);
-
-    p->reference = 0;
-    if ((ret = ff_get_buffer(avctx, p)) < 0){
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
         return ret;
-    }
     p->pict_type = AV_PICTURE_TYPE_I;
     p->key_frame = 1;
 
-    Y = a->pic.data[0];
-    U = a->pic.data[1];
-    V = a->pic.data[2];
+    Y = p->data[0];
+    U = p->data[1];
+    V = p->data[2];
 
     stride = avctx->width - 4;
 
@@ -123,45 +111,28 @@
         }
 
         buf += avctx->width + 4;
-        Y += a->pic.linesize[0];
-        U += a->pic.linesize[1];
-        V += a->pic.linesize[2];
+        Y += p->linesize[0];
+        U += p->linesize[1];
+        V += p->linesize[2];
     }
 
     *got_frame = 1;
-    *(AVFrame*)data = a->pic;
 
     return buf_size;
 }
 
 static av_cold int decode_init(AVCodecContext *avctx)
 {
-    VideoXLContext * const a = avctx->priv_data;
-
-    avcodec_get_frame_defaults(&a->pic);
     avctx->pix_fmt = AV_PIX_FMT_YUV411P;
 
     return 0;
 }
 
-static av_cold int decode_end(AVCodecContext *avctx)
-{
-    VideoXLContext * const a = avctx->priv_data;
-    AVFrame *pic = &a->pic;
-
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
-
-    return 0;
-}
-
 AVCodec ff_xl_decoder = {
     .name           = "xl",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_VIXL,
-    .priv_data_size = sizeof(VideoXLContext),
     .init           = decode_init,
-    .close          = decode_end,
     .decode         = decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("Miro VideoXL"),
diff --git a/libavcodec/xwddec.c b/libavcodec/xwddec.c
index 9923cdc..66a2fe9 100644
--- a/libavcodec/xwddec.c
+++ b/libavcodec/xwddec.c
@@ -26,19 +26,10 @@
 #include "internal.h"
 #include "xwd.h"
 
-static av_cold int xwd_decode_init(AVCodecContext *avctx)
-{
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame)
-        return AVERROR(ENOMEM);
-
-    return 0;
-}
-
 static int xwd_decode_frame(AVCodecContext *avctx, void *data,
                             int *got_frame, AVPacket *avpkt)
 {
-    AVFrame *p = avctx->coded_frame;
+    AVFrame *p = data;
     const uint8_t *buf = avpkt->data;
     int i, ret, buf_size = avpkt->size;
     uint32_t version, header_size, vclass, ncolors;
@@ -101,7 +92,7 @@
     }
 
     if (xoffset) {
-        av_log_ask_for_sample(avctx, "unsupported xoffset %d\n", xoffset);
+        avpriv_request_sample(avctx, "xoffset %d", xoffset);
         return AVERROR_PATCHWELCOME;
     }
 
@@ -204,18 +195,14 @@
     }
 
     if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
-        av_log_ask_for_sample(avctx, "unknown file: bpp %d, pixdepth %d, vclass %d\n", bpp, pixdepth, vclass);
+        avpriv_request_sample(avctx,
+                              "Unknown file: bpp %d, pixdepth %d, vclass %d",
+                              bpp, pixdepth, vclass);
         return AVERROR_PATCHWELCOME;
     }
 
-    if (p->data[0])
-        avctx->release_buffer(avctx, p);
-
-    p->reference = 0;
-    if ((ret = ff_get_buffer(avctx, p)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
         return ret;
-    }
 
     p->key_frame = 1;
     p->pict_type = AV_PICTURE_TYPE_I;
@@ -246,27 +233,14 @@
     }
 
     *got_frame       = 1;
-    *(AVFrame *)data = *p;
 
     return buf_size;
 }
 
-static av_cold int xwd_decode_close(AVCodecContext *avctx)
-{
-    if (avctx->coded_frame->data[0])
-        avctx->release_buffer(avctx, avctx->coded_frame);
-
-    av_freep(&avctx->coded_frame);
-
-    return 0;
-}
-
 AVCodec ff_xwd_decoder = {
     .name           = "xwd",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_XWD,
-    .init           = xwd_decode_init,
-    .close          = xwd_decode_close,
     .decode         = xwd_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
     .long_name      = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
diff --git a/libavcodec/xwdenc.c b/libavcodec/xwdenc.c
index fac3d0b..9d0da29 100644
--- a/libavcodec/xwdenc.c
+++ b/libavcodec/xwdenc.c
@@ -30,17 +30,8 @@
 #define WINDOW_NAME         "lavcxwdenc"
 #define WINDOW_NAME_SIZE    11
 
-static av_cold int xwd_encode_init(AVCodecContext *avctx)
-{
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame)
-        return AVERROR(ENOMEM);
-
-    return 0;
-}
-
 static int xwd_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
-                            const AVFrame *p, int *got_packet)
+                            const AVFrame *pict, int *got_packet)
 {
     enum AVPixelFormat pix_fmt = avctx->pix_fmt;
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
@@ -49,6 +40,7 @@
     uint32_t header_size;
     int i, out_size, ret;
     uint8_t *ptr, *buf;
+    AVFrame * const p = (AVFrame *)pict;
 
     pixdepth = av_get_bits_per_pixel(desc);
     if (desc->flags & PIX_FMT_BE)
@@ -158,8 +150,8 @@
         return ret;
     buf = pkt->data;
 
-    avctx->coded_frame->key_frame = 1;
-    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
+    p->key_frame = 1;
+    p->pict_type = AV_PICTURE_TYPE_I;
 
     bytestream_put_be32(&buf, header_size);
     bytestream_put_be32(&buf, XWD_VERSION);   // file version
@@ -216,20 +208,11 @@
     return 0;
 }
 
-static av_cold int xwd_encode_close(AVCodecContext *avctx)
-{
-    av_freep(&avctx->coded_frame);
-
-    return 0;
-}
-
 AVCodec ff_xwd_encoder = {
     .name         = "xwd",
     .type         = AVMEDIA_TYPE_VIDEO,
     .id           = AV_CODEC_ID_XWD,
-    .init         = xwd_encode_init,
     .encode2      = xwd_encode_frame,
-    .close        = xwd_encode_close,
     .pix_fmts     = (const enum AVPixelFormat[]) { AV_PIX_FMT_BGRA,
                                                  AV_PIX_FMT_RGBA,
                                                  AV_PIX_FMT_ARGB,
diff --git a/libavcodec/xxan.c b/libavcodec/xxan.c
index 9168327..cb913da 100644
--- a/libavcodec/xxan.c
+++ b/libavcodec/xxan.c
@@ -26,10 +26,11 @@
 #include "bytestream.h"
 #define BITSTREAM_READER_LE
 #include "get_bits.h"
+#include "internal.h"
 
 typedef struct XanContext {
     AVCodecContext *avctx;
-    AVFrame pic;
+    AVFrame *pic;
 
     uint8_t *y_buffer;
     uint8_t *scratch_buffer;
@@ -37,6 +38,8 @@
     GetByteContext gb;
 } XanContext;
 
+static av_cold int xan_decode_end(AVCodecContext *avctx);
+
 static av_cold int xan_decode_init(AVCodecContext *avctx)
 {
     XanContext *s = avctx->priv_data;
@@ -56,7 +59,13 @@
         return AVERROR(ENOMEM);
     s->scratch_buffer = av_malloc(s->buffer_size + 130);
     if (!s->scratch_buffer) {
-        av_freep(&s->y_buffer);
+        xan_decode_end(avctx);
+        return AVERROR(ENOMEM);
+    }
+
+    s->pic = av_frame_alloc();
+    if (!s->pic) {
+        xan_decode_end(avctx);
         return AVERROR(ENOMEM);
     }
 
@@ -194,8 +203,8 @@
         return dec_size;
     }
 
-    U = s->pic.data[1];
-    V = s->pic.data[2];
+    U = s->pic->data[1];
+    V = s->pic->data[2];
     src     = s->scratch_buffer;
     src_end = src + dec_size;
     if (mode) {
@@ -214,16 +223,16 @@
                     V[i] = vval | (vval >> 5);
                 }
             }
-            U += s->pic.linesize[1];
-            V += s->pic.linesize[2];
+            U += s->pic->linesize[1];
+            V += s->pic->linesize[2];
         }
         if (avctx->height & 1) {
-            memcpy(U, U - s->pic.linesize[1], avctx->width >> 1);
-            memcpy(V, V - s->pic.linesize[2], avctx->width >> 1);
+            memcpy(U, U - s->pic->linesize[1], avctx->width >> 1);
+            memcpy(V, V - s->pic->linesize[2], avctx->width >> 1);
         }
     } else {
-        uint8_t *U2 = U + s->pic.linesize[1];
-        uint8_t *V2 = V + s->pic.linesize[2];
+        uint8_t *U2 = U + s->pic->linesize[1];
+        uint8_t *V2 = V + s->pic->linesize[2];
 
         for (j = 0; j < avctx->height >> 2; j++) {
             for (i = 0; i < avctx->width >> 1; i += 2) {
@@ -240,16 +249,16 @@
                     V[i] = V[i+1] = V2[i] = V2[i+1] = vval | (vval >> 5);
                 }
             }
-            U  += s->pic.linesize[1] * 2;
-            V  += s->pic.linesize[2] * 2;
-            U2 += s->pic.linesize[1] * 2;
-            V2 += s->pic.linesize[2] * 2;
+            U  += s->pic->linesize[1] * 2;
+            V  += s->pic->linesize[2] * 2;
+            U2 += s->pic->linesize[1] * 2;
+            V2 += s->pic->linesize[2] * 2;
         }
         if (avctx->height & 3) {
             int lines = ((avctx->height + 1) >> 1) - (avctx->height >> 2) * 2;
 
-            memcpy(U, U - lines * s->pic.linesize[1], lines * s->pic.linesize[1]);
-            memcpy(V, V - lines * s->pic.linesize[2], lines * s->pic.linesize[2]);
+            memcpy(U, U - lines * s->pic->linesize[1], lines * s->pic->linesize[1]);
+            memcpy(V, V - lines * s->pic->linesize[2], lines * s->pic->linesize[2]);
         }
     }
 
@@ -315,7 +324,7 @@
         int dec_size;
 
         bytestream2_seek(&s->gb, 8 + corr_off, SEEK_SET);
-        dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size);
+        dec_size = xan_unpack(s, s->scratch_buffer, s->buffer_size / 2);
         if (dec_size < 0)
             dec_size = 0;
         else
@@ -326,12 +335,12 @@
     }
 
     src  = s->y_buffer;
-    ybuf = s->pic.data[0];
+    ybuf = s->pic->data[0];
     for (j = 0; j < avctx->height; j++) {
         for (i = 0; i < avctx->width; i++)
             ybuf[i] = (src[i] << 2) | (src[i] >> 3);
         src  += avctx->width;
-        ybuf += s->pic.linesize[0];
+        ybuf += s->pic->linesize[0];
     }
 
     return 0;
@@ -372,12 +381,12 @@
     }
 
     src = s->y_buffer;
-    ybuf = s->pic.data[0];
+    ybuf = s->pic->data[0];
     for (j = 0; j < avctx->height; j++) {
         for (i = 0; i < avctx->width; i++)
             ybuf[i] = (src[i] << 2) | (src[i] >> 3);
         src  += avctx->width;
-        ybuf += s->pic.linesize[0];
+        ybuf += s->pic->linesize[0];
     }
 
     return 0;
@@ -391,14 +400,8 @@
     int ftype;
     int ret;
 
-    s->pic.reference = 3;
-    s->pic.buffer_hints = FF_BUFFER_HINTS_VALID |
-                          FF_BUFFER_HINTS_PRESERVE |
-                          FF_BUFFER_HINTS_REUSABLE;
-    if ((ret = avctx->reget_buffer(avctx, &s->pic))) {
-        av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, s->pic)) < 0)
         return ret;
-    }
 
     bytestream2_init(&s->gb, avpkt->data, avpkt->size);
     ftype = bytestream2_get_le32(&s->gb);
@@ -416,8 +419,10 @@
     if (ret)
         return ret;
 
+    if ((ret = av_frame_ref(data, s->pic)) < 0)
+        return ret;
+
     *got_frame = 1;
-    *(AVFrame*)data = s->pic;
 
     return avpkt->size;
 }
@@ -426,8 +431,7 @@
 {
     XanContext *s = avctx->priv_data;
 
-    if (s->pic.data[0])
-        avctx->release_buffer(avctx, &s->pic);
+    av_frame_free(&s->pic);
 
     av_freep(&s->y_buffer);
     av_freep(&s->scratch_buffer);
diff --git a/libavcodec/y41pdec.c b/libavcodec/y41pdec.c
index 655a426..70b68aa 100644
--- a/libavcodec/y41pdec.c
+++ b/libavcodec/y41pdec.c
@@ -32,37 +32,24 @@
         av_log(avctx, AV_LOG_WARNING, "y41p requires width to be divisible by 8.\n");
     }
 
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame) {
-        av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
-        return AVERROR(ENOMEM);
-    }
-
     return 0;
 }
 
 static int y41p_decode_frame(AVCodecContext *avctx, void *data,
                              int *got_frame, AVPacket *avpkt)
 {
-    AVFrame *pic = avctx->coded_frame;
+    AVFrame *pic = data;
     uint8_t *src = avpkt->data;
     uint8_t *y, *u, *v;
-    int i, j;
-
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
+    int i, j, ret;
 
     if (avpkt->size < 1.5 * avctx->height * avctx->width) {
         av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
         return AVERROR(EINVAL);
     }
 
-    pic->reference = 0;
-
-    if (ff_get_buffer(avctx, pic) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
-        return AVERROR(ENOMEM);
-    }
+    if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
+        return ret;
 
     pic->key_frame = 1;
     pic->pict_type = AV_PICTURE_TYPE_I;
@@ -90,28 +77,16 @@
     }
 
     *got_frame = 1;
-    *(AVFrame *)data = *pic;
 
     return avpkt->size;
 }
 
-static av_cold int y41p_decode_close(AVCodecContext *avctx)
-{
-    if (avctx->coded_frame->data[0])
-        avctx->release_buffer(avctx, avctx->coded_frame);
-
-    av_freep(&avctx->coded_frame);
-
-    return 0;
-}
-
 AVCodec ff_y41p_decoder = {
     .name         = "y41p",
     .type         = AVMEDIA_TYPE_VIDEO,
     .id           = AV_CODEC_ID_Y41P,
     .init         = y41p_decode_init,
     .decode       = y41p_decode_frame,
-    .close        = y41p_decode_close,
     .capabilities = CODEC_CAP_DR1,
     .long_name    = NULL_IF_CONFIG_SMALL("Uncompressed YUV 4:1:1 12-bit"),
 };
diff --git a/libavcodec/y41penc.c b/libavcodec/y41penc.c
index f0c212b..1a8f0fb 100644
--- a/libavcodec/y41penc.c
+++ b/libavcodec/y41penc.c
@@ -51,7 +51,6 @@
     if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width * avctx->height * 1.5)) < 0)
         return ret;
 
-    avctx->coded_frame->reference = 0;
     avctx->coded_frame->key_frame = 1;
     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
     dst = pkt->data;
diff --git a/libavcodec/yop.c b/libavcodec/yop.c
index 234868a..807e170 100644
--- a/libavcodec/yop.c
+++ b/libavcodec/yop.c
@@ -30,8 +30,8 @@
 #include "internal.h"
 
 typedef struct YopDecContext {
-    AVFrame frame;
     AVCodecContext *avctx;
+    AVFrame *frame;
 
     int num_pal_colors;
     int first_color[2];
@@ -79,6 +79,15 @@
      { 4, -2}, {-2,  0},
     };
 
+static av_cold int yop_decode_close(AVCodecContext *avctx)
+{
+    YopDecContext *s = avctx->priv_data;
+
+    av_frame_free(&s->frame);
+
+    return 0;
+}
+
 static av_cold int yop_decode_init(AVCodecContext *avctx)
 {
     YopDecContext *s = avctx->priv_data;
@@ -97,7 +106,6 @@
 
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
 
-    avcodec_get_frame_defaults(&s->frame);
     s->num_pal_colors = avctx->extradata[0];
     s->first_color[0] = avctx->extradata[1];
     s->first_color[1] = avctx->extradata[2];
@@ -109,14 +117,8 @@
         return AVERROR_INVALIDDATA;
     }
 
-    return 0;
-}
+    s->frame = av_frame_alloc();
 
-static av_cold int yop_decode_close(AVCodecContext *avctx)
-{
-    YopDecContext *s = avctx->priv_data;
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
     return 0;
 }
 
@@ -125,17 +127,17 @@
  * @param s codec context
  * @param tag the tag that was in the nibble
  */
-static int yop_paint_block(YopDecContext *s, int tag)
+static int yop_paint_block(YopDecContext *s, int linesize, int tag)
 {
     if (s->src_end - s->srcptr < paint_lut[tag][3]) {
         av_log(s->avctx, AV_LOG_ERROR, "Packet too small.\n");
         return AVERROR_INVALIDDATA;
     }
 
-    s->dstptr[0]                        = s->srcptr[0];
-    s->dstptr[1]                        = s->srcptr[paint_lut[tag][0]];
-    s->dstptr[s->frame.linesize[0]]     = s->srcptr[paint_lut[tag][1]];
-    s->dstptr[s->frame.linesize[0] + 1] = s->srcptr[paint_lut[tag][2]];
+    s->dstptr[0]            = s->srcptr[0];
+    s->dstptr[1]            = s->srcptr[paint_lut[tag][0]];
+    s->dstptr[linesize]     = s->srcptr[paint_lut[tag][1]];
+    s->dstptr[linesize + 1] = s->srcptr[paint_lut[tag][2]];
 
     // The number of src bytes consumed is in the last part of the lut entry.
     s->srcptr += paint_lut[tag][3];
@@ -146,22 +148,22 @@
  * Copy a previously painted macroblock to the current_block.
  * @param copy_tag the tag that was in the nibble
  */
-static int yop_copy_previous_block(YopDecContext *s, int copy_tag)
+static int yop_copy_previous_block(YopDecContext *s, int linesize, int copy_tag)
 {
     uint8_t *bufptr;
 
     // Calculate position for the copy source
     bufptr = s->dstptr + motion_vector[copy_tag][0] +
-             s->frame.linesize[0] * motion_vector[copy_tag][1];
+             linesize * motion_vector[copy_tag][1];
     if (bufptr < s->dstbuf) {
         av_log(s->avctx, AV_LOG_ERROR, "File probably corrupt\n");
         return AVERROR_INVALIDDATA;
     }
 
-    s->dstptr[0]                        = bufptr[0];
-    s->dstptr[1]                        = bufptr[1];
-    s->dstptr[s->frame.linesize[0]]     = bufptr[s->frame.linesize[0]];
-    s->dstptr[s->frame.linesize[0] + 1] = bufptr[s->frame.linesize[0] + 1];
+    s->dstptr[0]            = bufptr[0];
+    s->dstptr[1]            = bufptr[1];
+    s->dstptr[linesize]     = bufptr[linesize];
+    s->dstptr[linesize + 1] = bufptr[linesize + 1];
 
     return 0;
 }
@@ -188,6 +190,7 @@
                             AVPacket *avpkt)
 {
     YopDecContext *s = avctx->priv_data;
+    AVFrame *frame = s->frame;
     int tag, firstcolor, is_odd_frame;
     int ret, i, x, y;
     uint32_t *palette;
@@ -197,20 +200,14 @@
         return AVERROR_INVALIDDATA;
     }
 
-    if (s->frame.data[0])
-        avctx->release_buffer(avctx, &s->frame);
-
-    ret = ff_get_buffer(avctx, &s->frame);
-    if (ret < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+    if ((ret = ff_reget_buffer(avctx, frame)) < 0)
         return ret;
-    }
 
     if (!avctx->frame_number)
-        memset(s->frame.data[1], 0, AVPALETTE_SIZE);
+        memset(frame->data[1], 0, AVPALETTE_SIZE);
 
-    s->dstbuf     = s->frame.data[0];
-    s->dstptr     = s->frame.data[0];
+    s->dstbuf     = frame->data[0];
+    s->dstptr     = frame->data[0];
     s->srcptr     = avpkt->data + 4;
     s->src_end    = avpkt->data + avpkt->size;
     s->low_nibble = NULL;
@@ -221,7 +218,7 @@
         return AVERROR_INVALIDDATA;
     }
     firstcolor   = s->first_color[is_odd_frame];
-    palette      = (uint32_t *)s->frame.data[1];
+    palette      = (uint32_t *)frame->data[1];
 
     for (i = 0; i < s->num_pal_colors; i++, s->srcptr += 3) {
         palette[i + firstcolor] = (s->srcptr[0] << 18) |
@@ -231,7 +228,7 @@
                                    (palette[i + firstcolor] >> 6) & 0x30303;
     }
 
-    s->frame.palette_has_changed = 1;
+    frame->palette_has_changed = 1;
 
     for (y = 0; y < avctx->height; y += 2) {
         for (x = 0; x < avctx->width; x += 2) {
@@ -243,24 +240,24 @@
             tag = yop_get_next_nibble(s);
 
             if (tag != 0xf) {
-                ret = yop_paint_block(s, tag);
+                ret = yop_paint_block(s, frame->linesize[0], tag);
                 if (ret < 0)
                     return ret;
             } else {
                 tag = yop_get_next_nibble(s);
-                ret = yop_copy_previous_block(s, tag);
-                if (ret < 0) {
-                    avctx->release_buffer(avctx, &s->frame);
+                ret = yop_copy_previous_block(s, frame->linesize[0], tag);
+                if (ret < 0)
                     return ret;
-                }
             }
             s->dstptr += 2;
         }
-        s->dstptr += 2*s->frame.linesize[0] - x;
+        s->dstptr += 2*frame->linesize[0] - x;
     }
 
+    if ((ret = av_frame_ref(data, s->frame)) < 0)
+        return ret;
+
     *got_frame = 1;
-    *(AVFrame *) data = s->frame;
     return avpkt->size;
 }
 
diff --git a/libavcodec/yuv4dec.c b/libavcodec/yuv4dec.c
index 8eb72c2..1d1fdd0 100644
--- a/libavcodec/yuv4dec.c
+++ b/libavcodec/yuv4dec.c
@@ -27,38 +27,24 @@
 {
     avctx->pix_fmt = AV_PIX_FMT_YUV420P;
 
-    avctx->coded_frame = avcodec_alloc_frame();
-
-    if (!avctx->coded_frame) {
-        av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
-        return AVERROR(ENOMEM);
-    }
-
     return 0;
 }
 
 static int yuv4_decode_frame(AVCodecContext *avctx, void *data,
                              int *got_frame, AVPacket *avpkt)
 {
-    AVFrame *pic = avctx->coded_frame;
+    AVFrame *pic = data;
     const uint8_t *src = avpkt->data;
     uint8_t *y, *u, *v;
-    int i, j;
-
-    if (pic->data[0])
-        avctx->release_buffer(avctx, pic);
+    int i, j, ret;
 
     if (avpkt->size < 6 * (avctx->width + 1 >> 1) * (avctx->height + 1 >> 1)) {
         av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
         return AVERROR(EINVAL);
     }
 
-    pic->reference = 0;
-
-    if (ff_get_buffer(avctx, pic) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
-        return AVERROR(ENOMEM);
-    }
+    if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
+        return ret;
 
     pic->key_frame = 1;
     pic->pict_type = AV_PICTURE_TYPE_I;
@@ -83,28 +69,16 @@
     }
 
     *got_frame = 1;
-    *(AVFrame *)data = *pic;
 
     return avpkt->size;
 }
 
-static av_cold int yuv4_decode_close(AVCodecContext *avctx)
-{
-    if (avctx->coded_frame->data[0])
-        avctx->release_buffer(avctx, avctx->coded_frame);
-
-    av_freep(&avctx->coded_frame);
-
-    return 0;
-}
-
 AVCodec ff_yuv4_decoder = {
     .name         = "yuv4",
     .type         = AVMEDIA_TYPE_VIDEO,
     .id           = AV_CODEC_ID_YUV4,
     .init         = yuv4_decode_init,
     .decode       = yuv4_decode_frame,
-    .close        = yuv4_decode_close,
     .capabilities = CODEC_CAP_DR1,
     .long_name    = NULL_IF_CONFIG_SMALL("Uncompressed packed 4:2:0"),
 };
diff --git a/libavcodec/yuv4enc.c b/libavcodec/yuv4enc.c
index 340310a..6e2f9bc 100644
--- a/libavcodec/yuv4enc.c
+++ b/libavcodec/yuv4enc.c
@@ -46,7 +46,6 @@
         return ret;
     dst = pkt->data;
 
-    avctx->coded_frame->reference = 0;
     avctx->coded_frame->key_frame = 1;
     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
 
diff --git a/libavcodec/zerocodec.c b/libavcodec/zerocodec.c
index 8122cca..4a23b67 100644
--- a/libavcodec/zerocodec.c
+++ b/libavcodec/zerocodec.c
@@ -31,14 +31,12 @@
                                   int *got_frame, AVPacket *avpkt)
 {
     ZeroCodecContext *zc = avctx->priv_data;
-    AVFrame *pic         = avctx->coded_frame;
+    AVFrame *pic         = data;
     AVFrame *prev_pic    = &zc->previous_frame;
     z_stream *zstream    = &zc->zstream;
     uint8_t *prev        = prev_pic->data[0];
     uint8_t *dst;
-    int i, j, zret;
-
-    pic->reference = 3;
+    int i, j, zret, ret;
 
     if (avpkt->flags & AV_PKT_FLAG_KEY) {
         pic->key_frame = 1;
@@ -61,10 +59,8 @@
         return AVERROR_INVALIDDATA;
     }
 
-    if (ff_get_buffer(avctx, pic) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
-        return AVERROR(ENOMEM);
-    }
+    if ((ret = ff_get_buffer(avctx, pic, AV_GET_BUFFER_FLAG_REF)) < 0)
+        return ret;
 
     zstream->next_in  = avpkt->data;
     zstream->avail_in = avpkt->size;
@@ -82,7 +78,6 @@
 
         zret = inflate(zstream, Z_SYNC_FLUSH);
         if (zret != Z_OK && zret != Z_STREAM_END) {
-            avctx->release_buffer(avctx, pic);
             av_log(avctx, AV_LOG_ERROR,
                    "Inflate failed with return code: %d.\n", zret);
             return AVERROR_INVALIDDATA;
@@ -96,16 +91,11 @@
         dst  -= pic->linesize[0];
     }
 
-    /* Release the previous buffer if need be */
-    if (prev_pic->data[0])
-        avctx->release_buffer(avctx, prev_pic);
+    av_frame_unref(&zc->previous_frame);
+    if ((ret = av_frame_ref(&zc->previous_frame, pic)) < 0)
+        return ret;
 
     *got_frame = 1;
-    *(AVFrame *)data = *pic;
-
-    /* Store the previous frame for use later.
-     * FFSWAP ensures that e.g. pic->data is NULLed. */
-    FFSWAP(AVFrame, *pic, *prev_pic);
 
     return avpkt->size;
 }
@@ -113,16 +103,11 @@
 static av_cold int zerocodec_decode_close(AVCodecContext *avctx)
 {
     ZeroCodecContext *zc = avctx->priv_data;
-    AVFrame *prev_pic    = &zc->previous_frame;
+
+    av_frame_unref(&zc->previous_frame);
 
     inflateEnd(&zc->zstream);
 
-    /* Release last frame */
-    if (prev_pic->data[0])
-        avctx->release_buffer(avctx, prev_pic);
-
-    av_freep(&avctx->coded_frame);
-
     return 0;
 }
 
@@ -145,13 +130,6 @@
         return AVERROR(ENOMEM);
     }
 
-    avctx->coded_frame = avcodec_alloc_frame();
-    if (!avctx->coded_frame) {
-        av_log(avctx, AV_LOG_ERROR, "Could not allocate frame buffer.\n");
-        zerocodec_decode_close(avctx);
-        return AVERROR(ENOMEM);
-    }
-
     return 0;
 }
 
diff --git a/libavcodec/zmbv.c b/libavcodec/zmbv.c
index 03821cd..7f3b326 100644
--- a/libavcodec/zmbv.c
+++ b/libavcodec/zmbv.c
@@ -54,7 +54,6 @@
  */
 typedef struct ZmbvContext {
     AVCodecContext *avctx;
-    AVFrame pic;
 
     int bpp;
     unsigned int decomp_size;
@@ -65,6 +64,7 @@
     int fmt;
     int comp;
     int flags;
+    int stride;
     int bw, bh, bx, by;
     int decomp_len;
     z_stream zstream;
@@ -400,6 +400,7 @@
 
 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
 {
+    AVFrame *frame = data;
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     ZmbvContext * const c = avctx->priv_data;
@@ -407,16 +408,6 @@
     int len = buf_size;
     int hi_ver, lo_ver, ret;
 
-    if (c->pic.data[0])
-            avctx->release_buffer(avctx, &c->pic);
-
-    c->pic.reference = 3;
-    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
-    if ((ret = ff_get_buffer(avctx, &c->pic)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
-        return ret;
-    }
-
     /* parse header */
     c->flags = buf[0];
     buf++; len--;
@@ -438,18 +429,15 @@
                "Flags=%X ver=%i.%i comp=%i fmt=%i blk=%ix%i\n",
                c->flags,hi_ver,lo_ver,c->comp,c->fmt,c->bw,c->bh);
         if (hi_ver != 0 || lo_ver != 1) {
-            av_log_ask_for_sample(avctx, "Unsupported version %i.%i\n",
-                                  hi_ver, lo_ver);
+            avpriv_request_sample(avctx, "Version %i.%i", hi_ver, lo_ver);
             return AVERROR_PATCHWELCOME;
         }
         if (c->bw == 0 || c->bh == 0) {
-            av_log_ask_for_sample(avctx, "Unsupported block size %ix%i\n",
-                                  c->bw, c->bh);
+            avpriv_request_sample(avctx, "Block size %ix%i", c->bw, c->bh);
             return AVERROR_PATCHWELCOME;
         }
         if (c->comp != 0 && c->comp != 1) {
-            av_log_ask_for_sample(avctx, "Unsupported compression type %i\n",
-                                  c->comp);
+            avpriv_request_sample(avctx, "Compression type %i", c->comp);
             return AVERROR_PATCHWELCOME;
         }
 
@@ -458,29 +446,39 @@
             c->bpp = 8;
             decode_intra = zmbv_decode_intra;
             c->decode_xor = zmbv_decode_xor_8;
+            avctx->pix_fmt = AV_PIX_FMT_PAL8;
+            c->stride = c->width;
             break;
         case ZMBV_FMT_15BPP:
         case ZMBV_FMT_16BPP:
             c->bpp = 16;
             decode_intra = zmbv_decode_intra;
             c->decode_xor = zmbv_decode_xor_16;
+            if (c->fmt == ZMBV_FMT_15BPP)
+                avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
+            else
+                avctx->pix_fmt = AV_PIX_FMT_RGB565LE;
+            c->stride = c->width * 2;
             break;
 #ifdef ZMBV_ENABLE_24BPP
         case ZMBV_FMT_24BPP:
             c->bpp = 24;
             decode_intra = zmbv_decode_intra;
             c->decode_xor = zmbv_decode_xor_24;
+            avctx->pix_fmt = AV_PIX_FMT_RGB24;
+            c->stride = c->width * 3;
             break;
 #endif //ZMBV_ENABLE_24BPP
         case ZMBV_FMT_32BPP:
             c->bpp = 32;
             decode_intra = zmbv_decode_intra;
             c->decode_xor = zmbv_decode_xor_32;
+            avctx->pix_fmt = AV_PIX_FMT_BGR0;
+            c->stride = c->width * 4;
             break;
         default:
             c->decode_xor = NULL;
-            av_log_ask_for_sample(avctx, "Unsupported (for now) format %i\n",
-                                  c->fmt);
+            avpriv_request_sample(avctx, "Format %i", c->fmt);
             return AVERROR_PATCHWELCOME;
         }
 
@@ -506,6 +504,9 @@
         return AVERROR_INVALIDDATA;
     }
 
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
+        return ret;
+
     if (c->comp == 0) { //Uncompressed data
         if (c->decomp_size < len) {
             av_log(avctx, AV_LOG_ERROR, "decomp buffer too small\n");
@@ -526,12 +527,12 @@
         c->decomp_len = c->zstream.total_out;
     }
     if (c->flags & ZMBV_KEYFRAME) {
-        c->pic.key_frame = 1;
-        c->pic.pict_type = AV_PICTURE_TYPE_I;
+        frame->key_frame = 1;
+        frame->pict_type = AV_PICTURE_TYPE_I;
         c->decode_intra(c);
     } else {
-        c->pic.key_frame = 0;
-        c->pic.pict_type = AV_PICTURE_TYPE_P;
+        frame->key_frame = 0;
+        frame->pict_type = AV_PICTURE_TYPE_P;
         if (c->decomp_len)
             c->decode_xor(c);
     }
@@ -539,63 +540,24 @@
     /* update frames */
     {
         uint8_t *out, *src;
-        int i, j;
+        int j;
 
-        out = c->pic.data[0];
+        out = frame->data[0];
         src = c->cur;
         switch (c->fmt) {
         case ZMBV_FMT_8BPP:
-            for (j = 0; j < c->height; j++) {
-                for (i = 0; i < c->width; i++) {
-                    out[i * 3 + 0] = c->pal[(*src) * 3 + 0];
-                    out[i * 3 + 1] = c->pal[(*src) * 3 + 1];
-                    out[i * 3 + 2] = c->pal[(*src) * 3 + 2];
-                    src++;
-                }
-                out += c->pic.linesize[0];
-            }
-            break;
+            for (j = 0; j < 256; j++)
+                AV_WN32(&frame->data[1][j * 4], 0xFFU << 24 | AV_RB24(&c->pal[j * 3]));
         case ZMBV_FMT_15BPP:
-            for (j = 0; j < c->height; j++) {
-                for (i = 0; i < c->width; i++) {
-                    uint16_t tmp = AV_RL16(src);
-                    src += 2;
-                    out[i * 3 + 0] = (tmp & 0x7C00) >> 7;
-                    out[i * 3 + 1] = (tmp & 0x03E0) >> 2;
-                    out[i * 3 + 2] = (tmp & 0x001F) << 3;
-                }
-                out += c->pic.linesize[0];
-            }
-            break;
         case ZMBV_FMT_16BPP:
-            for (j = 0; j < c->height; j++) {
-                for (i = 0; i < c->width; i++) {
-                    uint16_t tmp = AV_RL16(src);
-                    src += 2;
-                    out[i * 3 + 0] = (tmp & 0xF800) >> 8;
-                    out[i * 3 + 1] = (tmp & 0x07E0) >> 3;
-                    out[i * 3 + 2] = (tmp & 0x001F) << 3;
-                }
-                out += c->pic.linesize[0];
-            }
-            break;
 #ifdef ZMBV_ENABLE_24BPP
         case ZMBV_FMT_24BPP:
-            for (j = 0; j < c->height; j++) {
-                memcpy(out, src, c->width * 3);
-                src += c->width * 3;
-                out += c->pic.linesize[0];
-            }
-            break;
-#endif //ZMBV_ENABLE_24BPP
+#endif
         case ZMBV_FMT_32BPP:
             for (j = 0; j < c->height; j++) {
-                for (i = 0; i < c->width; i++) {
-                    uint32_t tmp = AV_RL32(src);
-                    src += 4;
-                    AV_WB24(out+(i*3), tmp);
-                }
-                out += c->pic.linesize[0];
+                memcpy(out, src, c->stride);
+                src += c->stride;
+                out += frame->linesize[0];
             }
             break;
         default:
@@ -604,7 +566,6 @@
         FFSWAP(uint8_t *, c->cur, c->prev);
     }
     *got_frame = 1;
-    *(AVFrame*)data = c->pic;
 
     /* always report that the buffer was completely consumed */
     return buf_size;
@@ -619,14 +580,12 @@
 
     c->width = avctx->width;
     c->height = avctx->height;
-    avcodec_get_frame_defaults(&c->pic);
 
     c->bpp = avctx->bits_per_coded_sample;
 
     // Needed if zlib unused or init aborted before inflateInit
     memset(&c->zstream, 0, sizeof(z_stream));
 
-    avctx->pix_fmt = AV_PIX_FMT_RGB24;
     c->decomp_size = (avctx->width + 255) * 4 * (avctx->height + 64);
 
     /* Allocate decompression buffer */
@@ -656,8 +615,6 @@
 
     av_freep(&c->decomp_buf);
 
-    if (c->pic.data[0])
-        avctx->release_buffer(avctx, &c->pic);
     inflateEnd(&c->zstream);
     av_freep(&c->cur);
     av_freep(&c->prev);
diff --git a/libavdevice/alsa-audio-dec.c b/libavdevice/alsa-audio-dec.c
index b781daf..03154b0 100644
--- a/libavdevice/alsa-audio-dec.c
+++ b/libavdevice/alsa-audio-dec.c
@@ -49,6 +49,7 @@
 #include "libavformat/internal.h"
 #include "libavutil/opt.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/time.h"
 
 #include "avdevice.h"
 #include "alsa-audio.h"
diff --git a/libavdevice/alsa-audio-enc.c b/libavdevice/alsa-audio-enc.c
index 403a44f..0f4e4a2 100644
--- a/libavdevice/alsa-audio-enc.c
+++ b/libavdevice/alsa-audio-enc.c
@@ -39,6 +39,7 @@
 
 #include <alsa/asoundlib.h>
 
+#include "libavutil/time.h"
 #include "libavformat/internal.h"
 #include "avdevice.h"
 #include "alsa-audio.h"
diff --git a/libavdevice/bktr.c b/libavdevice/bktr.c
index d5f3d9f..b0dbe60 100644
--- a/libavdevice/bktr.c
+++ b/libavdevice/bktr.c
@@ -28,6 +28,7 @@
 #include "libavutil/log.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
+#include "libavutil/time.h"
 #if HAVE_DEV_BKTR_IOCTL_METEOR_H && HAVE_DEV_BKTR_IOCTL_BT848_H
 # include <dev/bktr/ioctl_meteor.h>
 # include <dev/bktr/ioctl_bt848.h>
diff --git a/libavdevice/dshow.c b/libavdevice/dshow.c
index 4991414..8cc12f5 100644
--- a/libavdevice/dshow.c
+++ b/libavdevice/dshow.c
@@ -774,9 +774,9 @@
         codec->width      = bih->biWidth;
         codec->height     = bih->biHeight;
         codec->pix_fmt    = dshow_pixfmt(bih->biCompression, bih->biBitCount);
-        if(bih->biCompression == MKTAG('H', 'D', 'Y', 'C')) {
-          av_log(avctx, AV_LOG_DEBUG, "attempt use full range for HDYC...");
-          codec->color_range = AVCOL_RANGE_MPEG; // just in case it needs this...
+        if (bih->biCompression == MKTAG('H', 'D', 'Y', 'C')) {
+            av_log(avctx, AV_LOG_DEBUG, "attempt to use full range for HDYC...\n");
+            codec->color_range = AVCOL_RANGE_MPEG; // just in case it needs this...
         }
         if (codec->pix_fmt == AV_PIX_FMT_NONE) {
             codec->codec_id = ff_codec_get_id(avformat_get_riff_video_tags(), bih->biCompression);
diff --git a/libavdevice/dshow_pin.c b/libavdevice/dshow_pin.c
index 4953642..30e4d95 100644
--- a/libavdevice/dshow_pin.c
+++ b/libavdevice/dshow_pin.c
@@ -358,5 +358,5 @@
 {
     libAVPin *pin = (libAVPin *) ((uint8_t *) this - imemoffset);
     dshowdebug("libAVMemInputPin_Destroy(%p)\n", this);
-    return libAVPin_Destroy(pin);
+    libAVPin_Destroy(pin);
 }
diff --git a/libavdevice/lavfi.c b/libavdevice/lavfi.c
index 3b6f0c3..c1c9cf5 100644
--- a/libavdevice/lavfi.c
+++ b/libavdevice/lavfi.c
@@ -51,6 +51,7 @@
     int *sink_stream_map;
     int *sink_eof;
     int *stream_sink_map;
+    AVFrame *decoded_frame;
 } LavfiContext;
 
 static int *create_all_formats(int n)
@@ -83,6 +84,7 @@
     av_freep(&lavfi->stream_sink_map);
     av_freep(&lavfi->sinks);
     avfilter_graph_free(&lavfi->graph);
+    av_frame_free(&lavfi->decoded_frame);
 
     return 0;
 }
@@ -103,8 +105,8 @@
 
     avfilter_register_all();
 
-    buffersink = avfilter_get_by_name("ffbuffersink");
-    abuffersink = avfilter_get_by_name("ffabuffersink");
+    buffersink = avfilter_get_by_name("buffersink");
+    abuffersink = avfilter_get_by_name("abuffersink");
 
     if (lavfi->graph_filename && lavfi->graph_str) {
         av_log(avctx, AV_LOG_ERROR,
@@ -225,14 +227,11 @@
         }
 
         if (type == AVMEDIA_TYPE_VIDEO) {
-            AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
-
-            buffersink_params->pixel_fmts = pix_fmts;
             ret = avfilter_graph_create_filter(&sink, buffersink,
                                                inout->name, NULL,
-                                               buffersink_params, lavfi->graph);
-            av_freep(&buffersink_params);
-
+                                               NULL, lavfi->graph);
+            if (ret >= 0)
+                ret = av_opt_set_int_list(sink, "pix_fmts", pix_fmts,  AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
             if (ret < 0)
                 goto end;
         } else if (type == AVMEDIA_TYPE_AUDIO) {
@@ -241,13 +240,12 @@
                                                   AV_SAMPLE_FMT_S32,
                                                   AV_SAMPLE_FMT_FLT,
                                                   AV_SAMPLE_FMT_DBL, -1 };
-            AVABufferSinkParams *abuffersink_params = av_abuffersink_params_alloc();
-            abuffersink_params->sample_fmts = sample_fmts;
 
             ret = avfilter_graph_create_filter(&sink, abuffersink,
                                                inout->name, NULL,
-                                               abuffersink_params, lavfi->graph);
-            av_free(abuffersink_params);
+                                               NULL, lavfi->graph);
+            if (ret >= 0)
+                ret = av_opt_set_int_list(sink, "sample_fmts", sample_fmts,  AV_SAMPLE_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
             if (ret < 0)
                 goto end;
         }
@@ -300,6 +298,9 @@
         }
     }
 
+    if (!(lavfi->decoded_frame = av_frame_alloc()))
+        FAIL(AVERROR(ENOMEM));
+
 end:
     av_free(pix_fmts);
     avfilter_inout_free(&input_links);
@@ -314,8 +315,9 @@
     LavfiContext *lavfi = avctx->priv_data;
     double min_pts = DBL_MAX;
     int stream_idx, min_pts_sink_idx = 0;
-    AVFilterBufferRef *ref;
+    AVFrame *frame = lavfi->decoded_frame;
     AVPicture pict;
+    AVDictionary *frame_metadata;
     int ret, i;
     int size = 0;
 
@@ -329,16 +331,17 @@
         if (lavfi->sink_eof[i])
             continue;
 
-        ret = av_buffersink_get_buffer_ref(lavfi->sinks[i],
-                                       &ref, AV_BUFFERSINK_FLAG_PEEK);
+        ret = av_buffersink_get_frame_flags(lavfi->sinks[i], frame,
+                                            AV_BUFFERSINK_FLAG_PEEK);
         if (ret == AVERROR_EOF) {
             av_dlog(avctx, "EOF sink_idx:%d\n", i);
             lavfi->sink_eof[i] = 1;
             continue;
         } else if (ret < 0)
             return ret;
-        d = av_rescale_q(ref->pts, tb, AV_TIME_BASE_Q);
+        d = av_rescale_q(frame->pts, tb, AV_TIME_BASE_Q);
         av_dlog(avctx, "sink_idx:%d time:%f\n", i, d);
+        av_frame_unref(frame);
 
         if (d < min_pts) {
             min_pts = d;
@@ -350,35 +353,35 @@
 
     av_dlog(avctx, "min_pts_sink_idx:%i\n", min_pts_sink_idx);
 
-    av_buffersink_get_buffer_ref(lavfi->sinks[min_pts_sink_idx], &ref, 0);
+    av_buffersink_get_frame_flags(lavfi->sinks[min_pts_sink_idx], frame, 0);
     stream_idx = lavfi->sink_stream_map[min_pts_sink_idx];
 
-    if (ref->video) {
-        size = avpicture_get_size(ref->format, ref->video->w, ref->video->h);
+    if (frame->width /* FIXME best way of testing a video */) {
+        size = avpicture_get_size(frame->format, frame->width, frame->height);
         if ((ret = av_new_packet(pkt, size)) < 0)
             return ret;
 
-        memcpy(pict.data,     ref->data,     4*sizeof(ref->data[0]));
-        memcpy(pict.linesize, ref->linesize, 4*sizeof(ref->linesize[0]));
+        memcpy(pict.data,     frame->data,     4*sizeof(frame->data[0]));
+        memcpy(pict.linesize, frame->linesize, 4*sizeof(frame->linesize[0]));
 
-        avpicture_layout(&pict, ref->format, ref->video->w,
-                         ref->video->h, pkt->data, size);
-    } else if (ref->audio) {
-        size = ref->audio->nb_samples *
-            av_get_bytes_per_sample(ref->format) *
-            av_get_channel_layout_nb_channels(ref->audio->channel_layout);
+        avpicture_layout(&pict, frame->format, frame->width, frame->height,
+                         pkt->data, size);
+    } else if (av_frame_get_channels(frame) /* FIXME test audio */) {
+        size = frame->nb_samples * av_get_bytes_per_sample(frame->format) *
+                                   av_frame_get_channels(frame);
         if ((ret = av_new_packet(pkt, size)) < 0)
             return ret;
-        memcpy(pkt->data, ref->data[0], size);
+        memcpy(pkt->data, frame->data[0], size);
     }
 
-    if (ref->metadata) {
+    frame_metadata = av_frame_get_metadata(frame);
+    if (frame_metadata) {
         uint8_t *metadata;
         AVDictionaryEntry *e = NULL;
         AVBPrint meta_buf;
 
         av_bprint_init(&meta_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
-        while ((e = av_dict_get(ref->metadata, "", e, AV_DICT_IGNORE_SUFFIX))) {
+        while ((e = av_dict_get(frame_metadata, "", e, AV_DICT_IGNORE_SUFFIX))) {
             av_bprintf(&meta_buf, "%s", e->key);
             av_bprint_chars(&meta_buf, '\0', 1);
             av_bprintf(&meta_buf, "%s", e->value);
@@ -395,10 +398,10 @@
     }
 
     pkt->stream_index = stream_idx;
-    pkt->pts = ref->pts;
-    pkt->pos = ref->pos;
+    pkt->pts = frame->pts;
+    pkt->pos = av_frame_get_pkt_pos(frame);
     pkt->size = size;
-    avfilter_unref_buffer(ref);
+    av_frame_unref(frame);
     return size;
 }
 
diff --git a/libavdevice/openal-dec.c b/libavdevice/openal-dec.c
index 6069edc..93633ff 100644
--- a/libavdevice/openal-dec.c
+++ b/libavdevice/openal-dec.c
@@ -25,6 +25,7 @@
 #include <AL/alc.h>
 
 #include "libavutil/opt.h"
+#include "libavutil/time.h"
 #include "libavformat/internal.h"
 #include "avdevice.h"
 
diff --git a/libavdevice/sdl.c b/libavdevice/sdl.c
index 1867139..e708dfd 100644
--- a/libavdevice/sdl.c
+++ b/libavdevice/sdl.c
@@ -37,6 +37,7 @@
     char *window_title;
     char *icon_title;
     int window_width,  window_height;  /**< size of the window */
+    int window_fullscreen;
     int overlay_width, overlay_height; /**< size of the video in the window */
     int overlay_x, overlay_y;
     int overlay_fmt;
@@ -76,6 +77,7 @@
     AVCodecContext *encctx = st->codec;
     AVRational sar, dar; /* sample and display aspect ratios */
     int i, ret;
+    int flags = SDL_SWSURFACE | sdl->window_fullscreen ? SDL_FULLSCREEN : 0;
 
     if (!sdl->window_title)
         sdl->window_title = av_strdup(s->filename);
@@ -151,7 +153,7 @@
 
     SDL_WM_SetCaption(sdl->window_title, sdl->icon_title);
     sdl->surface = SDL_SetVideoMode(sdl->window_width, sdl->window_height,
-                                    24, SDL_SWSURFACE);
+                                    24, flags);
     if (!sdl->surface) {
         av_log(s, AV_LOG_ERROR, "Unable to set video mode: %s\n", SDL_GetError());
         ret = AVERROR(EINVAL);
@@ -209,6 +211,7 @@
     { "window_title", "set SDL window title",           OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
     { "icon_title",   "set SDL iconified window title", OFFSET(icon_title)  , AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
     { "window_size",  "set SDL window forced size",     OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE,{.str=NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
+    { "window_fullscreen", "set SDL window fullscreen", OFFSET(window_fullscreen), AV_OPT_TYPE_INT,{.i64=0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
     { NULL },
 };
 
diff --git a/libavdevice/sndio_dec.c b/libavdevice/sndio_dec.c
index 2d7b848..806f478 100644
--- a/libavdevice/sndio_dec.c
+++ b/libavdevice/sndio_dec.c
@@ -25,6 +25,7 @@
 #include "libavformat/avformat.h"
 #include "libavformat/internal.h"
 #include "libavutil/opt.h"
+#include "libavutil/time.h"
 
 #include "sndio_common.h"
 
diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c
index 5287bc1..34e3d9c 100644
--- a/libavdevice/v4l2.c
+++ b/libavdevice/v4l2.c
@@ -46,6 +46,7 @@
 #endif
 #include <linux/videodev2.h>
 #endif
+#include "libavutil/atomic.h"
 #include "libavutil/avassert.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/log.h"
@@ -54,6 +55,7 @@
 #include "timefilter.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/pixdesc.h"
+#include "libavutil/time.h"
 #include "libavutil/avstring.h"
 
 #if CONFIG_LIBV4L2
@@ -109,6 +111,7 @@
     int64_t last_time_m;
 
     int buffers;
+    volatile int buffers_queued;
     void **buf_start;
     unsigned int *buf_len;
     char *standard;
@@ -121,8 +124,8 @@
 };
 
 struct buff_data {
+    struct video_data *s;
     int index;
-    int fd;
 };
 
 struct fmt_map {
@@ -320,7 +323,7 @@
 {
     struct v4l2_frmsizeenum vfse = { .pixel_format = pixelformat };
 
-    while(!ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &vfse)) {
+    while(!v4l2_ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &vfse)) {
         switch (vfse.type) {
         case V4L2_FRMSIZE_TYPE_DISCRETE:
             av_log(ctx, AV_LOG_INFO, " %ux%u",
@@ -345,7 +348,7 @@
 {
     struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
 
-    while(!ioctl(fd, VIDIOC_ENUM_FMT, &vfd)) {
+    while(!v4l2_ioctl(fd, VIDIOC_ENUM_FMT, &vfd)) {
         enum AVCodecID codec_id = fmt_v4l2codec(vfd.pixelformat);
         enum AVPixelFormat pix_fmt = fmt_v4l2ff(vfd.pixelformat, codec_id);
 
@@ -368,10 +371,8 @@
         }
 
 #ifdef V4L2_FMT_FLAG_EMULATED
-        if (vfd.flags & V4L2_FMT_FLAG_EMULATED) {
-            av_log(ctx, AV_LOG_WARNING, "%s", "Emulated");
-            continue;
-        }
+        if (vfd.flags & V4L2_FMT_FLAG_EMULATED)
+            av_log(ctx, AV_LOG_INFO, " Emulated :");
 #endif
 #if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
         list_framesizes(ctx, fd, vfd.pixelformat);
@@ -470,28 +471,32 @@
     return 0;
 }
 
-static void mmap_release_buffer(AVPacket *pkt)
+#if FF_API_DESTRUCT_PACKET
+static void dummy_release_buffer(AVPacket *pkt)
+{
+    av_assert0(0);
+}
+#endif
+
+static void mmap_release_buffer(void *opaque, uint8_t *data)
 {
     struct v4l2_buffer buf = { 0 };
-    int res, fd;
-    struct buff_data *buf_descriptor = pkt->priv;
-
-    if (pkt->data == NULL)
-        return;
+    int res;
+    struct buff_data *buf_descriptor = opaque;
+    struct video_data *s = buf_descriptor->s;
 
     buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
     buf.memory = V4L2_MEMORY_MMAP;
     buf.index = buf_descriptor->index;
-    fd = buf_descriptor->fd;
     av_free(buf_descriptor);
 
-    if (v4l2_ioctl(fd, VIDIOC_QBUF, &buf) < 0) {
+    if (v4l2_ioctl(s->fd, VIDIOC_QBUF, &buf) < 0) {
         res = AVERROR(errno);
-        av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", av_err2str(res));
+        av_log(NULL, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n",
+               av_err2str(res));
     }
 
-    pkt->data = NULL;
-    pkt->size = 0;
+    avpriv_atomic_int_add_and_fetch(&s->buffers_queued, 1);
 }
 
 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
@@ -520,8 +525,8 @@
     now = av_gettime_monotonic();
     if (s->ts_mode == V4L_TS_MONO2ABS ||
         (ts <= now + 1 * AV_TIME_BASE && ts >= now - 10 * AV_TIME_BASE)) {
-        int64_t period = av_rescale_q(1, AV_TIME_BASE_Q,
-                                      ctx->streams[0]->avg_frame_rate);
+        AVRational tb = {AV_TIME_BASE, 1};
+        int64_t period = av_rescale_q(1, tb, ctx->streams[0]->avg_frame_rate);
         av_log(ctx, AV_LOG_INFO, "Detected monotonic timestamps, converting\n");
         /* microseconds instead of seconds, MHz instead of Hz */
         s->timefilter = ff_timefilter_new(1, period, 1.0E-6);
@@ -561,7 +566,6 @@
         .type   = V4L2_BUF_TYPE_VIDEO_CAPTURE,
         .memory = V4L2_MEMORY_MMAP
     };
-    struct buff_data *buf_descriptor;
     int res;
 
     /* FIXME: Some special treatment might be needed in case of loss of signal... */
@@ -580,6 +584,9 @@
         av_log(ctx, AV_LOG_ERROR, "Invalid buffer index received.\n");
         return AVERROR(EINVAL);
     }
+    avpriv_atomic_int_add_and_fetch(&s->buffers_queued, -1);
+    // always keep at least one buffer queued
+    av_assert0(avpriv_atomic_int_get(&s->buffers_queued) >= 1);
 
     /* CPIA is a compressed format and we don't know the exact number of bytes
      * used by a frame, so set it here as the driver announces it.
@@ -595,25 +602,59 @@
     }
 
     /* Image is at s->buff_start[buf.index] */
-    pkt->data= s->buf_start[buf.index];
-    pkt->size = buf.bytesused;
-    pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
-    res = convert_timestamp(ctx, &pkt->pts);
-    if (res < 0)
-        return res;
-    pkt->destruct = mmap_release_buffer;
-    buf_descriptor = av_malloc(sizeof(struct buff_data));
-    if (buf_descriptor == NULL) {
-        /* Something went wrong... Since av_malloc() failed, we cannot even
-         * allocate a buffer for memcopying into it
-         */
-        av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
-        res = v4l2_ioctl(s->fd, VIDIOC_QBUF, &buf);
-        return AVERROR(ENOMEM);
+    if (avpriv_atomic_int_get(&s->buffers_queued) == FFMAX(s->buffers / 8, 1)) {
+        /* when we start getting low on queued buffers, fallback to copying data */
+        res = av_new_packet(pkt, buf.bytesused);
+        if (res < 0) {
+            av_log(ctx, AV_LOG_ERROR, "Error allocating a packet.\n");
+            if (v4l2_ioctl(s->fd, VIDIOC_QBUF, &buf) == 0)
+                avpriv_atomic_int_add_and_fetch(&s->buffers_queued, 1);
+            return res;
+        }
+        memcpy(pkt->data, s->buf_start[buf.index], buf.bytesused);
+
+        if (v4l2_ioctl(s->fd, VIDIOC_QBUF, &buf) < 0) {
+            res = AVERROR(errno);
+            av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QBUF): %s\n", av_err2str(res));
+            av_free_packet(pkt);
+            return res;
+        }
+        avpriv_atomic_int_add_and_fetch(&s->buffers_queued, 1);
+    } else {
+        struct buff_data *buf_descriptor;
+
+        pkt->data     = s->buf_start[buf.index];
+        pkt->size     = buf.bytesused;
+#if FF_API_DESTRUCT_PACKET
+        pkt->destruct = dummy_release_buffer;
+#endif
+
+        buf_descriptor = av_malloc(sizeof(struct buff_data));
+        if (buf_descriptor == NULL) {
+            /* Something went wrong... Since av_malloc() failed, we cannot even
+             * allocate a buffer for memcpying into it
+             */
+            av_log(ctx, AV_LOG_ERROR, "Failed to allocate a buffer descriptor\n");
+            if (v4l2_ioctl(s->fd, VIDIOC_QBUF, &buf) == 0)
+                avpriv_atomic_int_add_and_fetch(&s->buffers_queued, 1);
+
+            return AVERROR(ENOMEM);
+        }
+        buf_descriptor->index = buf.index;
+        buf_descriptor->s     = s;
+
+        pkt->buf = av_buffer_create(pkt->data, pkt->size, mmap_release_buffer,
+                                    buf_descriptor, 0);
+        if (!pkt->buf) {
+            av_log(ctx, AV_LOG_ERROR, "Failed to create a buffer\n");
+            if (v4l2_ioctl(s->fd, VIDIOC_QBUF, &buf) == 0)
+                avpriv_atomic_int_add_and_fetch(&s->buffers_queued, 1);
+            av_freep(&buf_descriptor);
+            return AVERROR(ENOMEM);
+        }
     }
-    buf_descriptor->fd = s->fd;
-    buf_descriptor->index = buf.index;
-    pkt->priv = buf_descriptor;
+    pkt->pts = buf.timestamp.tv_sec * INT64_C(1000000) + buf.timestamp.tv_usec;
+    convert_timestamp(ctx, &pkt->pts);
 
     return s->buf_len[buf.index];
 }
@@ -637,6 +678,7 @@
             return res;
         }
     }
+    s->buffers_queued = s->buffers;
 
     type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
     if (v4l2_ioctl(s->fd, VIDIOC_STREAMON, &type) < 0) {
@@ -769,6 +811,7 @@
     }
     s1->streams[0]->avg_frame_rate.num = tpf->denominator;
     s1->streams[0]->avg_frame_rate.den = tpf->numerator;
+    s1->streams[0]->r_frame_rate = s1->streams[0]->avg_frame_rate;
 
     return 0;
 }
@@ -839,18 +882,34 @@
     if (!st)
         return AVERROR(ENOMEM);
 
+#if CONFIG_LIBV4L2
+    /* silence libv4l2 logging. if fopen() fails v4l2_log_file will be NULL
+       and errors will get sent to stderr */
+    v4l2_log_file = fopen("/dev/null", "w");
+#endif
+
     s->fd = device_open(s1);
     if (s->fd < 0)
         return s->fd;
 
-    /* set tv video input */
-    av_log(s1, AV_LOG_DEBUG, "Selecting input_channel: %d\n", s->channel);
-    if (v4l2_ioctl(s->fd, VIDIOC_S_INPUT, &s->channel) < 0) {
-        res = AVERROR(errno);
-        av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_S_INPUT): %s\n", av_err2str(res));
-        return res;
+    if (s->channel != -1) {
+        /* set video input */
+        av_log(s1, AV_LOG_DEBUG, "Selecting input_channel: %d\n", s->channel);
+        if (v4l2_ioctl(s->fd, VIDIOC_S_INPUT, &s->channel) < 0) {
+            res = AVERROR(errno);
+            av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_S_INPUT): %s\n", av_err2str(res));
+            return res;
+        }
+    } else {
+        /* get current video input */
+        if (v4l2_ioctl(s->fd, VIDIOC_G_INPUT, &s->channel) < 0) {
+            res = AVERROR(errno);
+            av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_INPUT): %s\n", av_err2str(res));
+            return res;
+        }
     }
 
+    /* enum input */
     input.index = s->channel;
     if (v4l2_ioctl(s->fd, VIDIOC_ENUMINPUT, &input) < 0) {
         res = AVERROR(errno);
@@ -858,7 +917,7 @@
         return res;
     }
     s->std_id = input.std;
-    av_log(s1, AV_LOG_DEBUG, "input_channel: %d, input_name: %s\n",
+    av_log(s1, AV_LOG_DEBUG, "Current input_channel: %d, input_name: %s\n",
            s->channel, input.name);
 
     if (s->list_format) {
@@ -890,11 +949,10 @@
     }
 
     if (!s->width && !s->height) {
-        struct v4l2_format fmt;
+        struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
 
         av_log(s1, AV_LOG_VERBOSE,
                "Querying the device for the current frame size\n");
-        fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
         if (v4l2_ioctl(s->fd, VIDIOC_G_FMT, &fmt) < 0) {
             res = AVERROR(errno);
             av_log(s1, AV_LOG_ERROR, "ioctl(VIDIOC_G_FMT): %s\n", av_err2str(res));
@@ -979,6 +1037,10 @@
 {
     struct video_data *s = s1->priv_data;
 
+    if (avpriv_atomic_int_get(&s->buffers_queued) != s->buffers)
+        av_log(s1, AV_LOG_WARNING, "Some buffers are still owned by the caller on "
+               "close.\n");
+
     mmap_close(s);
 
     v4l2_close(s->fd);
@@ -990,7 +1052,7 @@
 
 static const AVOption options[] = {
     { "standard",     "set TV standard, used only by analog frame grabber",       OFFSET(standard),     AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0,       DEC },
-    { "channel",      "set TV channel, used only by frame grabber",               OFFSET(channel),      AV_OPT_TYPE_INT,    {.i64 = 0 },    0, INT_MAX, DEC },
+    { "channel",      "set TV channel, used only by frame grabber",               OFFSET(channel),      AV_OPT_TYPE_INT,    {.i64 = -1 },  -1, INT_MAX, DEC },
     { "video_size",   "set frame size",                                           OFFSET(width),        AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL},  0, 0,   DEC },
     { "pixel_format", "set preferred pixel format",                               OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       DEC },
     { "input_format", "set preferred pixel format (for raw video) or codec name", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       DEC },
diff --git a/libavdevice/version.h b/libavdevice/version.h
index 9e0c0ae..c03e733 100644
--- a/libavdevice/version.h
+++ b/libavdevice/version.h
@@ -27,9 +27,9 @@
 
 #include "libavutil/avutil.h"
 
-#define LIBAVDEVICE_VERSION_MAJOR  54
-#define LIBAVDEVICE_VERSION_MINOR   3
-#define LIBAVDEVICE_VERSION_MICRO 103
+#define LIBAVDEVICE_VERSION_MAJOR  55
+#define LIBAVDEVICE_VERSION_MINOR   0
+#define LIBAVDEVICE_VERSION_MICRO 100
 
 #define LIBAVDEVICE_VERSION_INT AV_VERSION_INT(LIBAVDEVICE_VERSION_MAJOR, \
                                                LIBAVDEVICE_VERSION_MINOR, \
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 04ed8b8..084bd30 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -40,8 +40,7 @@
        formats.o                                                        \
        graphdump.o                                                      \
        graphparser.o                                                    \
-       sink_buffer.o                                                    \
-       src_buffer.o                                                     \
+       opencl_allkernels.o                                              \
        transform.o                                                      \
        video.o                                                          \
 
@@ -53,16 +52,20 @@
 OBJS-$(CONFIG_ACONVERT_FILTER)               += af_aconvert.o
 OBJS-$(CONFIG_AFADE_FILTER)                  += af_afade.o
 OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
+OBJS-$(CONFIG_AINTERLEAVE_FILTER)            += f_interleave.o
 OBJS-$(CONFIG_ALLPASS_FILTER)                += af_biquads.o
 OBJS-$(CONFIG_AMERGE_FILTER)                 += af_amerge.o
 OBJS-$(CONFIG_AMIX_FILTER)                   += af_amix.o
 OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
 OBJS-$(CONFIG_APAD_FILTER)                   += af_apad.o
+OBJS-$(CONFIG_APERMS_FILTER)                 += f_perms.o
+OBJS-$(CONFIG_APHASER_FILTER)                += af_aphaser.o
 OBJS-$(CONFIG_ARESAMPLE_FILTER)              += af_aresample.o
 OBJS-$(CONFIG_ASELECT_FILTER)                += f_select.o
 OBJS-$(CONFIG_ASENDCMD_FILTER)               += f_sendcmd.o
 OBJS-$(CONFIG_ASETNSAMPLES_FILTER)           += af_asetnsamples.o
 OBJS-$(CONFIG_ASETPTS_FILTER)                += f_setpts.o
+OBJS-$(CONFIG_ASETRATE_FILTER)               += af_asetrate.o
 OBJS-$(CONFIG_ASETTB_FILTER)                 += f_settb.o
 OBJS-$(CONFIG_ASHOWINFO_FILTER)              += af_ashowinfo.o
 OBJS-$(CONFIG_ASPLIT_FILTER)                 += split.o
@@ -91,6 +94,7 @@
 OBJS-$(CONFIG_AEVALSRC_FILTER)               += asrc_aevalsrc.o
 OBJS-$(CONFIG_ANULLSRC_FILTER)               += asrc_anullsrc.o
 OBJS-$(CONFIG_FLITE_FILTER)                  += asrc_flite.o
+OBJS-$(CONFIG_SINE_FILTER)                   += asrc_sine.o
 
 OBJS-$(CONFIG_ANULLSINK_FILTER)              += asink_anullsink.o
 
@@ -102,10 +106,13 @@
 OBJS-$(CONFIG_BLACKFRAME_FILTER)             += vf_blackframe.o
 OBJS-$(CONFIG_BLEND_FILTER)                  += vf_blend.o
 OBJS-$(CONFIG_BOXBLUR_FILTER)                += vf_boxblur.o
+OBJS-$(CONFIG_COLORBALANCE_FILTER)           += vf_colorbalance.o
+OBJS-$(CONFIG_COLORCHANNELMIXER_FILTER)      += vf_colorchannelmixer.o
 OBJS-$(CONFIG_COLORMATRIX_FILTER)            += vf_colormatrix.o
 OBJS-$(CONFIG_COPY_FILTER)                   += vf_copy.o
 OBJS-$(CONFIG_CROP_FILTER)                   += vf_crop.o
 OBJS-$(CONFIG_CROPDETECT_FILTER)             += vf_cropdetect.o
+OBJS-$(CONFIG_CURVES_FILTER)                 += vf_curves.o
 OBJS-$(CONFIG_DECIMATE_FILTER)               += vf_decimate.o
 OBJS-$(CONFIG_DELOGO_FILTER)                 += vf_delogo.o
 OBJS-$(CONFIG_DESHAKE_FILTER)                += vf_deshake.o
@@ -114,6 +121,7 @@
 OBJS-$(CONFIG_EDGEDETECT_FILTER)             += vf_edgedetect.o
 OBJS-$(CONFIG_FADE_FILTER)                   += vf_fade.o
 OBJS-$(CONFIG_FIELD_FILTER)                  += vf_field.o
+OBJS-$(CONFIG_FIELDMATCH_FILTER)             += vf_fieldmatch.o
 OBJS-$(CONFIG_FIELDORDER_FILTER)             += vf_fieldorder.o
 OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
 OBJS-$(CONFIG_FRAMESTEP_FILTER)              += vf_framestep.o
@@ -128,21 +136,27 @@
 OBJS-$(CONFIG_HUE_FILTER)                    += vf_hue.o
 OBJS-$(CONFIG_IDET_FILTER)                   += vf_idet.o
 OBJS-$(CONFIG_IL_FILTER)                     += vf_il.o
+OBJS-$(CONFIG_INTERLACE_FILTER)              += vf_interlace.o
+OBJS-$(CONFIG_INTERLEAVE_FILTER)             += f_interleave.o
 OBJS-$(CONFIG_KERNDEINT_FILTER)              += vf_kerndeint.o
 OBJS-$(CONFIG_LUT_FILTER)                    += vf_lut.o
 OBJS-$(CONFIG_LUTRGB_FILTER)                 += vf_lut.o
 OBJS-$(CONFIG_LUTYUV_FILTER)                 += vf_lut.o
 OBJS-$(CONFIG_MP_FILTER)                     += vf_mp.o
+OBJS-$(CONFIG_MPDECIMATE_FILTER)             += vf_mpdecimate.o
 OBJS-$(CONFIG_NEGATE_FILTER)                 += vf_lut.o
 OBJS-$(CONFIG_NOFORMAT_FILTER)               += vf_format.o
 OBJS-$(CONFIG_NOISE_FILTER)                  += vf_noise.o
 OBJS-$(CONFIG_NULL_FILTER)                   += vf_null.o
 OBJS-$(CONFIG_OCV_FILTER)                    += vf_libopencv.o
+OBJS-$(CONFIG_OPENCL)                        += deshake_opencl.o
 OBJS-$(CONFIG_OVERLAY_FILTER)                += vf_overlay.o
 OBJS-$(CONFIG_PAD_FILTER)                    += vf_pad.o
+OBJS-$(CONFIG_PERMS_FILTER)                  += f_perms.o
 OBJS-$(CONFIG_PIXDESCTEST_FILTER)            += vf_pixdesctest.o
 OBJS-$(CONFIG_PP_FILTER)                     += vf_pp.o
 OBJS-$(CONFIG_REMOVELOGO_FILTER)             += bbox.o lswsutils.o lavfutils.o vf_removelogo.o
+OBJS-$(CONFIG_SEPARATEFIELDS_FILTER)         += vf_separatefields.o
 OBJS-$(CONFIG_SCALE_FILTER)                  += vf_scale.o
 OBJS-$(CONFIG_SELECT_FILTER)                 += f_select.o
 OBJS-$(CONFIG_SENDCMD_FILTER)                += f_sendcmd.o
@@ -154,15 +168,19 @@
 OBJS-$(CONFIG_SHOWINFO_FILTER)               += vf_showinfo.o
 OBJS-$(CONFIG_SMARTBLUR_FILTER)              += vf_smartblur.o
 OBJS-$(CONFIG_SPLIT_FILTER)                  += split.o
+OBJS-$(CONFIG_STEREO3D_FILTER)               += vf_stereo3d.o
 OBJS-$(CONFIG_SUBTITLES_FILTER)              += vf_subtitles.o
 OBJS-$(CONFIG_SUPER2XSAI_FILTER)             += vf_super2xsai.o
 OBJS-$(CONFIG_SWAPUV_FILTER)                 += vf_swapuv.o
+OBJS-$(CONFIG_TELECINE_FILTER)               += vf_telecine.o
 OBJS-$(CONFIG_THUMBNAIL_FILTER)              += vf_thumbnail.o
 OBJS-$(CONFIG_TILE_FILTER)                   += vf_tile.o
 OBJS-$(CONFIG_TINTERLACE_FILTER)             += vf_tinterlace.o
 OBJS-$(CONFIG_TRANSPOSE_FILTER)              += vf_transpose.o
 OBJS-$(CONFIG_UNSHARP_FILTER)                += vf_unsharp.o
 OBJS-$(CONFIG_VFLIP_FILTER)                  += vf_vflip.o
+OBJS-$(CONFIG_VIDSTABDETECT_FILTER)          += vidstabutils.o vf_vidstabdetect.o
+OBJS-$(CONFIG_VIDSTABTRANSFORM_FILTER)       += vidstabutils.o vf_vidstabtransform.o
 OBJS-$(CONFIG_YADIF_FILTER)                  += vf_yadif.o
 
 OBJS-$(CONFIG_CELLAUTO_FILTER)               += vsrc_cellauto.o
@@ -174,26 +192,20 @@
 OBJS-$(CONFIG_NULLSRC_FILTER)                += vsrc_testsrc.o
 OBJS-$(CONFIG_RGBTESTSRC_FILTER)             += vsrc_testsrc.o
 OBJS-$(CONFIG_SMPTEBARS_FILTER)              += vsrc_testsrc.o
+OBJS-$(CONFIG_SMPTEHDBARS_FILTER)            += vsrc_testsrc.o
 OBJS-$(CONFIG_TESTSRC_FILTER)                += vsrc_testsrc.o
 
 OBJS-$(CONFIG_NULLSINK_FILTER)               += vsink_nullsink.o
 
 OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/mp_image.o
 OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/img_format.o
-OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_detc.o
 OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_dint.o
-OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_divtc.o
-OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_down3dright.o
 OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_eq2.o
 OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_eq.o
 OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_fil.o
-#OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_filmdint.o
 OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_fspp.o
-OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_harddup.o
 OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_ilpack.o
-OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_ivtc.o
 OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_mcdeint.o
-OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_noise.o
 OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_ow.o
 OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_perspective.o
 OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_phase.o
@@ -203,8 +215,6 @@
 OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_sab.o
 OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_softpulldown.o
 OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_spp.o
-OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_stereo3d.o
-OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_telecine.o
 OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_tinterlace.o
 OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/vf_uspp.o
 OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/pullup.o
@@ -218,6 +228,9 @@
 OBJS-$(CONFIG_AMOVIE_FILTER)                 += src_movie.o
 OBJS-$(CONFIG_MOVIE_FILTER)                  += src_movie.o
 
+SKIPHEADERS-$(CONFIG_LIBVIDSTAB)             += vidstabutils.h
+SKIPHEADERS-$(CONFIG_OPENCL)                 += deshake_kernel.h
+
 TOOLS     = graph2dot
 TESTPROGS = drawutils filtfmts formats
 
diff --git a/libavfilter/af_aconvert.c b/libavfilter/af_aconvert.c
index e41095f..8a9dc6f 100644
--- a/libavfilter/af_aconvert.c
+++ b/libavfilter/af_aconvert.c
@@ -38,12 +38,14 @@
     struct SwrContext *swr;
 } AConvertContext;
 
-static av_cold int init(AVFilterContext *ctx, const char *args0)
+static av_cold int init(AVFilterContext *ctx)
 {
     AConvertContext *aconvert = ctx->priv;
     char *arg, *ptr = NULL;
     int ret = 0;
-    char *args = av_strdup(args0);
+    char *args = av_strdup(NULL);
+
+    av_log(ctx, AV_LOG_WARNING, "This filter is deprecated, use aformat instead\n");
 
     aconvert->out_sample_fmt  = AV_SAMPLE_FMT_NONE;
     aconvert->out_chlayout    = 0;
@@ -135,23 +137,25 @@
     return 0;
 }
 
-static int  filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
+static int  filter_frame(AVFilterLink *inlink, AVFrame *insamplesref)
 {
     AConvertContext *aconvert = inlink->dst->priv;
-    const int n = insamplesref->audio->nb_samples;
+    const int n = insamplesref->nb_samples;
     AVFilterLink *const outlink = inlink->dst->outputs[0];
-    AVFilterBufferRef *outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n);
+    AVFrame *outsamplesref = ff_get_audio_buffer(outlink, n);
     int ret;
 
-    swr_convert(aconvert->swr, outsamplesref->data, n,
-                        (void *)insamplesref->data, n);
+    if (!outsamplesref)
+        return AVERROR(ENOMEM);
+    swr_convert(aconvert->swr, outsamplesref->extended_data, n,
+                        (void *)insamplesref->extended_data, n);
 
-    avfilter_copy_buffer_ref_props(outsamplesref, insamplesref);
-    outsamplesref->audio->channels       = outlink->channels;
-    outsamplesref->audio->channel_layout = outlink->channel_layout;
+    av_frame_copy_props(outsamplesref, insamplesref);
+    av_frame_set_channels(outsamplesref, outlink->channels);
+    outsamplesref->channel_layout = outlink->channel_layout;
 
     ret = ff_filter_frame(outlink, outsamplesref);
-    avfilter_unref_buffer(insamplesref);
+    av_frame_free(&insamplesref);
     return ret;
 }
 
@@ -160,7 +164,6 @@
         .name         = "default",
         .type         = AVMEDIA_TYPE_AUDIO,
         .filter_frame = filter_frame,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL }
 };
diff --git a/libavfilter/af_afade.c b/libavfilter/af_afade.c
index 00a05e2..35c6537 100644
--- a/libavfilter/af_afade.c
+++ b/libavfilter/af_afade.c
@@ -34,8 +34,8 @@
     int curve;
     int nb_samples;
     int64_t start_sample;
-    double duration;
-    double start_time;
+    int64_t duration;
+    int64_t start_time;
 
     void (*fade_samples)(uint8_t **dst, uint8_t * const *src,
                          int nb_samples, int channels, int direction,
@@ -56,10 +56,10 @@
     { "ss",           "set expression of sample to start fading",    OFFSET(start_sample), AV_OPT_TYPE_INT64,  {.i64 = 0    }, 0, INT64_MAX, FLAGS },
     { "nb_samples",   "set expression for fade duration in samples", OFFSET(nb_samples),   AV_OPT_TYPE_INT,    {.i64 = 44100}, 1, INT32_MAX, FLAGS },
     { "ns",           "set expression for fade duration in samples", OFFSET(nb_samples),   AV_OPT_TYPE_INT,    {.i64 = 44100}, 1, INT32_MAX, FLAGS },
-    { "start_time",   "set expression of second to start fading",    OFFSET(start_time),   AV_OPT_TYPE_DOUBLE, {.dbl = 0.   }, 0, 7*24*60*60,FLAGS },
-    { "st",           "set expression of second to start fading",    OFFSET(start_time),   AV_OPT_TYPE_DOUBLE, {.dbl = 0.   }, 0, 7*24*60*60,FLAGS },
-    { "duration",     "set expression for fade duration in seconds", OFFSET(duration),     AV_OPT_TYPE_DOUBLE, {.dbl = 0.   }, 0, 24*60*60,  FLAGS },
-    { "d",            "set expression for fade duration in seconds", OFFSET(duration),     AV_OPT_TYPE_DOUBLE, {.dbl = 0.   }, 0, 24*60*60,  FLAGS },
+    { "start_time",   "set expression of time to start fading",      OFFSET(start_time),   AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
+    { "st",           "set expression of time to start fading",      OFFSET(start_time),   AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
+    { "duration",     "set expression for fade duration",            OFFSET(duration),     AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
+    { "d",            "set expression for fade duration",            OFFSET(duration),     AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
     { "curve",        "set expression for fade curve",               OFFSET(curve),        AV_OPT_TYPE_INT,    {.i64 = TRI  }, TRI, CBR, FLAGS, "curve" },
     { "c",            "set expression for fade curve",               OFFSET(curve),        AV_OPT_TYPE_INT,    {.i64 = TRI  }, TRI, CBR, FLAGS, "curve" },
     { "tri",          "linear slope",                                0,                    AV_OPT_TYPE_CONST,  {.i64 = TRI  }, 0, 0, FLAGS, "curve" },
@@ -77,16 +77,9 @@
 
 AVFILTER_DEFINE_CLASS(afade);
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     AudioFadeContext *afade = ctx->priv;
-    int ret;
-
-    afade->class = &afade_class;
-    av_opt_set_defaults(afade);
-
-    if ((ret = av_set_options_string(afade, args, "=", ":")) < 0)
-        return ret;
 
     if (INT64_MAX - afade->nb_samples < afade->start_sample)
         return AVERROR(EINVAL);
@@ -225,38 +218,38 @@
     }
 
     if (afade->duration)
-        afade->nb_samples = afade->duration * inlink->sample_rate;
+        afade->nb_samples = afade->duration * inlink->sample_rate / AV_TIME_BASE;
     if (afade->start_time)
-        afade->start_sample = afade->start_time * inlink->sample_rate;
+        afade->start_sample = afade->start_time * inlink->sample_rate / AV_TIME_BASE;
 
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
 {
     AudioFadeContext *afade = inlink->dst->priv;
     AVFilterLink *outlink   = inlink->dst->outputs[0];
-    int nb_samples          = buf->audio->nb_samples;
-    AVFilterBufferRef *out_buf;
+    int nb_samples          = buf->nb_samples;
+    AVFrame *out_buf;
     int64_t cur_sample = av_rescale_q(buf->pts, (AVRational){1, outlink->sample_rate}, outlink->time_base);
 
     if ((!afade->type && (afade->start_sample + afade->nb_samples < cur_sample)) ||
         ( afade->type && (cur_sample + afade->nb_samples < afade->start_sample)))
         return ff_filter_frame(outlink, buf);
 
-    if (buf->perms & AV_PERM_WRITE) {
+    if (av_frame_is_writable(buf)) {
         out_buf = buf;
     } else {
-        out_buf = ff_get_audio_buffer(inlink, AV_PERM_WRITE, nb_samples);
+        out_buf = ff_get_audio_buffer(inlink, nb_samples);
         if (!out_buf)
             return AVERROR(ENOMEM);
-        out_buf->pts = buf->pts;
+        av_frame_copy_props(out_buf, buf);
     }
 
     if ((!afade->type && (cur_sample + nb_samples < afade->start_sample)) ||
         ( afade->type && (afade->start_sample + afade->nb_samples < cur_sample))) {
         av_samples_set_silence(out_buf->extended_data, 0, nb_samples,
-                               out_buf->audio->channels, out_buf->format);
+                               av_frame_get_channels(out_buf), out_buf->format);
     } else {
         int64_t start;
 
@@ -266,13 +259,13 @@
             start = afade->start_sample + afade->nb_samples - cur_sample;
 
         afade->fade_samples(out_buf->extended_data, buf->extended_data,
-                            nb_samples, buf->audio->channels,
+                            nb_samples, av_frame_get_channels(buf),
                             afade->type ? -1 : 1, start,
                             afade->nb_samples, afade->curve);
     }
 
     if (buf != out_buf)
-        avfilter_unref_buffer(buf);
+        av_frame_free(&buf);
 
     return ff_filter_frame(outlink, out_buf);
 }
diff --git a/libavfilter/af_aformat.c b/libavfilter/af_aformat.c
index 9ac381f..39bd850 100644
--- a/libavfilter/af_aformat.c
+++ b/libavfilter/af_aformat.c
@@ -59,17 +59,24 @@
 
 #define PARSE_FORMATS(str, type, list, add_to_list, get_fmt, none, desc)    \
 do {                                                                        \
-    char *next, *cur = str;                                                 \
+    char *next, *cur = str, sep;                                            \
+                                                                            \
+    if (str && strchr(str, ',')) {                                          \
+        av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use '|' to "\
+               "separate %s.\n", desc);                                     \
+        sep = ',';                                                          \
+    } else                                                                  \
+        sep = '|';                                                          \
+                                                                            \
     while (cur) {                                                           \
         type fmt;                                                           \
-        next = strchr(cur, ',');                                            \
+        next = strchr(cur, sep);                                            \
         if (next)                                                           \
             *next++ = 0;                                                    \
                                                                             \
         if ((fmt = get_fmt(cur)) == none) {                                 \
             av_log(ctx, AV_LOG_ERROR, "Error parsing " desc ": %s.\n", cur);\
-            ret = AVERROR(EINVAL);                                          \
-            goto fail;                                                      \
+            return AVERROR(EINVAL);                                         \
         }                                                                   \
         add_to_list(&list, fmt);                                            \
                                                                             \
@@ -83,21 +90,9 @@
     return FFMAX(ret, 0);
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     AFormatContext *s = ctx->priv;
-    int ret;
-
-    if (!args) {
-        av_log(ctx, AV_LOG_ERROR, "No parameters supplied.\n");
-        return AVERROR(EINVAL);
-    }
-
-    s->class = &aformat_class;
-    av_opt_set_defaults(s);
-
-    if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
-        return ret;
 
     PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats,
                   ff_add_format, av_get_sample_fmt, AV_SAMPLE_FMT_NONE, "sample format");
@@ -107,9 +102,7 @@
                   ff_add_channel_layout, av_get_channel_layout, 0,
                   "channel layout");
 
-fail:
-    av_opt_free(s);
-    return ret;
+    return 0;
 }
 
 static int query_formats(AVFilterContext *ctx)
@@ -148,8 +141,8 @@
     .init          = init,
     .query_formats = query_formats,
     .priv_size     = sizeof(AFormatContext),
+    .priv_class    = &aformat_class,
 
     .inputs        = avfilter_af_aformat_inputs,
     .outputs       = avfilter_af_aformat_outputs,
-    .priv_class    = &aformat_class,
 };
diff --git a/libavfilter/af_amerge.c b/libavfilter/af_amerge.c
index 2d68ea6..6244423 100644
--- a/libavfilter/af_amerge.c
+++ b/libavfilter/af_amerge.c
@@ -82,9 +82,9 @@
     for (i = 0; i < am->nb_inputs; i++) {
         if (!ctx->inputs[i]->in_channel_layouts ||
             !ctx->inputs[i]->in_channel_layouts->nb_channel_layouts) {
-            av_log(ctx, AV_LOG_ERROR,
+            av_log(ctx, AV_LOG_WARNING,
                    "No channel layout for input %d\n", i + 1);
-            return AVERROR(EINVAL);
+            return AVERROR(EAGAIN);
         }
         inlayout[i] = ctx->inputs[i]->in_channel_layouts->channel_layouts[0];
         if (ctx->inputs[i]->in_channel_layouts->nb_channel_layouts > 1) {
@@ -219,14 +219,14 @@
     }
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamples)
+static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
 {
     AVFilterContext *ctx = inlink->dst;
     AMergeContext *am = ctx->priv;
     AVFilterLink *const outlink = ctx->outputs[0];
     int input_number;
     int nb_samples, ns, i;
-    AVFilterBufferRef *outbuf, *inbuf[SWR_CH_MAX];
+    AVFrame *outbuf, *inbuf[SWR_CH_MAX];
     uint8_t *ins[SWR_CH_MAX], *outs;
 
     for (input_number = 0; input_number < am->nb_inputs; input_number++)
@@ -234,40 +234,42 @@
             break;
     av_assert1(input_number < am->nb_inputs);
     if (ff_bufqueue_is_full(&am->in[input_number].queue)) {
-        av_log(ctx, AV_LOG_ERROR, "Buffer queue overflow\n");
-        avfilter_unref_buffer(insamples);
+        av_frame_free(&insamples);
         return AVERROR(ENOMEM);
     }
-    ff_bufqueue_add(ctx, &am->in[input_number].queue, insamples);
-    am->in[input_number].nb_samples += insamples->audio->nb_samples;
+    ff_bufqueue_add(ctx, &am->in[input_number].queue, av_frame_clone(insamples));
+    am->in[input_number].nb_samples += insamples->nb_samples;
+    av_frame_free(&insamples);
     nb_samples = am->in[0].nb_samples;
     for (i = 1; i < am->nb_inputs; i++)
         nb_samples = FFMIN(nb_samples, am->in[i].nb_samples);
     if (!nb_samples)
         return 0;
 
-    outbuf = ff_get_audio_buffer(ctx->outputs[0], AV_PERM_WRITE, nb_samples);
+    outbuf = ff_get_audio_buffer(ctx->outputs[0], nb_samples);
+    if (!outbuf)
+        return AVERROR(ENOMEM);
     outs = outbuf->data[0];
     for (i = 0; i < am->nb_inputs; i++) {
         inbuf[i] = ff_bufqueue_peek(&am->in[i].queue, 0);
         ins[i] = inbuf[i]->data[0] +
                  am->in[i].pos * am->in[i].nb_ch * am->bps;
     }
-    avfilter_copy_buffer_ref_props(outbuf, inbuf[0]);
+    av_frame_copy_props(outbuf, inbuf[0]);
     outbuf->pts = inbuf[0]->pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
                   inbuf[0]->pts +
                   av_rescale_q(am->in[0].pos,
                                (AVRational){ 1, ctx->inputs[0]->sample_rate },
                                ctx->outputs[0]->time_base);
 
-    outbuf->audio->nb_samples     = nb_samples;
-    outbuf->audio->channel_layout = outlink->channel_layout;
-    outbuf->audio->channels       = outlink->channels;
+    outbuf->nb_samples     = nb_samples;
+    outbuf->channel_layout = outlink->channel_layout;
+    av_frame_set_channels(outbuf, outlink->channels);
 
     while (nb_samples) {
         ns = nb_samples;
         for (i = 0; i < am->nb_inputs; i++)
-            ns = FFMIN(ns, inbuf[i]->audio->nb_samples - am->in[i].pos);
+            ns = FFMIN(ns, inbuf[i]->nb_samples - am->in[i].pos);
         /* Unroll the most common sample formats: speed +~350% for the loop,
            +~13% overall (including two common decoders) */
         switch (am->bps) {
@@ -289,9 +291,9 @@
         for (i = 0; i < am->nb_inputs; i++) {
             am->in[i].nb_samples -= ns;
             am->in[i].pos += ns;
-            if (am->in[i].pos == inbuf[i]->audio->nb_samples) {
+            if (am->in[i].pos == inbuf[i]->nb_samples) {
                 am->in[i].pos = 0;
-                avfilter_unref_buffer(inbuf[i]);
+                av_frame_free(&inbuf[i]);
                 ff_bufqueue_get(&am->in[i].queue);
                 inbuf[i] = ff_bufqueue_peek(&am->in[i].queue, 0);
                 ins[i] = inbuf[i] ? inbuf[i]->data[0] : NULL;
@@ -301,18 +303,11 @@
     return ff_filter_frame(ctx->outputs[0], outbuf);
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     AMergeContext *am = ctx->priv;
-    int ret, i;
+    int i;
 
-    am->class = &amerge_class;
-    av_opt_set_defaults(am);
-    ret = av_set_options_string(am, args, "=", ":");
-    if (ret < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Error parsing options: '%s'\n", args);
-        return ret;
-    }
     am->in = av_calloc(am->nb_inputs, sizeof(*am->in));
     if (!am->in)
         return AVERROR(ENOMEM);
@@ -322,7 +317,6 @@
             .name             = name,
             .type             = AVMEDIA_TYPE_AUDIO,
             .filter_frame     = filter_frame,
-            .min_perms        = AV_PERM_READ | AV_PERM_PRESERVE,
         };
         if (!name)
             return AVERROR(ENOMEM);
@@ -352,4 +346,5 @@
     .inputs        = NULL,
     .outputs       = amerge_outputs,
     .priv_class    = &amerge_class,
+    .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS,
 };
diff --git a/libavfilter/af_amix.c b/libavfilter/af_amix.c
index aeefff8..75736e6 100644
--- a/libavfilter/af_amix.c
+++ b/libavfilter/af_amix.c
@@ -270,18 +270,18 @@
 {
     AVFilterContext *ctx = outlink->src;
     MixContext      *s = ctx->priv;
-    AVFilterBufferRef *out_buf, *in_buf;
+    AVFrame *out_buf, *in_buf;
     int i;
 
     calculate_scales(s, nb_samples);
 
-    out_buf = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
+    out_buf = ff_get_audio_buffer(outlink, nb_samples);
     if (!out_buf)
         return AVERROR(ENOMEM);
 
-    in_buf = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
+    in_buf = ff_get_audio_buffer(outlink, nb_samples);
     if (!in_buf) {
-        avfilter_unref_buffer(out_buf);
+        av_frame_free(&out_buf);
         return AVERROR(ENOMEM);
     }
 
@@ -303,7 +303,7 @@
             }
         }
     }
-    avfilter_unref_buffer(in_buf);
+    av_frame_free(&in_buf);
 
     out_buf->pts = s->next_pts;
     if (s->next_pts != AV_NOPTS_VALUE)
@@ -450,7 +450,7 @@
     return output_frame(outlink, available_samples);
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
 {
     AVFilterContext  *ctx = inlink->dst;
     MixContext       *s = ctx->priv;
@@ -469,31 +469,24 @@
     if (i == 0) {
         int64_t pts = av_rescale_q(buf->pts, inlink->time_base,
                                    outlink->time_base);
-        ret = frame_list_add_frame(s->frame_list, buf->audio->nb_samples, pts);
+        ret = frame_list_add_frame(s->frame_list, buf->nb_samples, pts);
         if (ret < 0)
             goto fail;
     }
 
     ret = av_audio_fifo_write(s->fifos[i], (void **)buf->extended_data,
-                              buf->audio->nb_samples);
+                              buf->nb_samples);
 
 fail:
-    avfilter_unref_buffer(buf);
+    av_frame_free(&buf);
 
     return ret;
 }
 
-static int init(AVFilterContext *ctx, const char *args)
+static int init(AVFilterContext *ctx)
 {
     MixContext *s = ctx->priv;
-    int i, ret;
-
-    s->class = &amix_class;
-    av_opt_set_defaults(s);
-
-    if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
-        return ret;
-    av_opt_free(s);
+    int i;
 
     for (i = 0; i < s->nb_inputs; i++) {
         char name[32];
@@ -556,6 +549,7 @@
     .name          = "amix",
     .description   = NULL_IF_CONFIG_SMALL("Audio mixing."),
     .priv_size     = sizeof(MixContext),
+    .priv_class    = &amix_class,
 
     .init           = init,
     .uninit         = uninit,
@@ -563,5 +557,6 @@
 
     .inputs    = NULL,
     .outputs   = avfilter_af_amix_outputs,
-    .priv_class = &amix_class,
+
+    .flags     = AVFILTER_FLAG_DYNAMIC_INPUTS,
 };
diff --git a/libavfilter/af_apad.c b/libavfilter/af_apad.c
index 18a0170..66738f6 100644
--- a/libavfilter/af_apad.c
+++ b/libavfilter/af_apad.c
@@ -56,19 +56,11 @@
 
 AVFILTER_DEFINE_CLASS(apad);
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
-    int ret;
     APadContext *apad = ctx->priv;
 
-    apad->class = &apad_class;
     apad->next_pts = AV_NOPTS_VALUE;
-
-    av_opt_set_defaults(apad);
-
-    if ((ret = av_opt_set_from_string(apad, args, NULL, "=", ":")) < 0)
-        return ret;
-
     if (apad->whole_len && apad->pad_len) {
         av_log(ctx, AV_LOG_ERROR, "Both whole and pad length are set, this is not possible\n");
         return AVERROR(EINVAL);
@@ -77,15 +69,15 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
 {
     AVFilterContext *ctx = inlink->dst;
     APadContext *apad = ctx->priv;
 
     if (apad->whole_len)
-        apad->whole_len -= frame->audio->nb_samples;
+        apad->whole_len -= frame->nb_samples;
 
-    apad->next_pts = frame->pts + av_rescale_q(frame->audio->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
+    apad->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
     return ff_filter_frame(ctx->outputs[0], frame);
 }
 
@@ -99,7 +91,7 @@
 
     if (ret == AVERROR_EOF) {
         int n_out = apad->packet_size;
-        AVFilterBufferRef *outsamplesref;
+        AVFrame *outsamplesref;
 
         if (apad->whole_len > 0) {
             apad->pad_len = apad->whole_len;
@@ -113,16 +105,16 @@
         if(!n_out)
             return AVERROR_EOF;
 
-        outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n_out);
+        outsamplesref = ff_get_audio_buffer(outlink, n_out);
         if (!outsamplesref)
             return AVERROR(ENOMEM);
 
-        av_assert0(outsamplesref->audio->sample_rate == outlink->sample_rate);
-        av_assert0(outsamplesref->audio->nb_samples  == n_out);
+        av_assert0(outsamplesref->sample_rate == outlink->sample_rate);
+        av_assert0(outsamplesref->nb_samples  == n_out);
 
         av_samples_set_silence(outsamplesref->extended_data, 0,
                                n_out,
-                               outsamplesref->audio->channels,
+                               av_frame_get_channels(outsamplesref),
                                outsamplesref->format);
 
         outsamplesref->pts = apad->next_pts;
@@ -139,7 +131,6 @@
         .name         = "default",
         .type         = AVMEDIA_TYPE_AUDIO,
         .filter_frame = filter_frame,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL },
 };
diff --git a/libavfilter/af_aphaser.c b/libavfilter/af_aphaser.c
new file mode 100644
index 0000000..b1043b0
--- /dev/null
+++ b/libavfilter/af_aphaser.c
@@ -0,0 +1,358 @@
+/*
+ * Copyright (c) 2013 Paul B Mahol
+ *
+ * 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
+ * phaser audio filter
+ */
+
+#include "libavutil/avassert.h"
+#include "libavutil/opt.h"
+#include "audio.h"
+#include "avfilter.h"
+#include "internal.h"
+
+enum WaveType {
+    WAVE_SIN,
+    WAVE_TRI,
+    WAVE_NB,
+};
+
+typedef struct AudioPhaserContext {
+    const AVClass *class;
+    double in_gain, out_gain;
+    double delay;
+    double decay;
+    double speed;
+
+    enum WaveType type;
+
+    int delay_buffer_length;
+    double *delay_buffer;
+
+    int modulation_buffer_length;
+    int32_t *modulation_buffer;
+
+    int delay_pos, modulation_pos;
+
+    void (*phaser)(struct AudioPhaserContext *p,
+                   uint8_t * const *src, uint8_t **dst,
+                   int nb_samples, int channels);
+} AudioPhaserContext;
+
+#define OFFSET(x) offsetof(AudioPhaserContext, x)
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption aphaser_options[] = {
+    { "in_gain",  "set input gain",            OFFSET(in_gain),  AV_OPT_TYPE_DOUBLE, {.dbl=.4},  0,  1,   FLAGS },
+    { "out_gain", "set output gain",           OFFSET(out_gain), AV_OPT_TYPE_DOUBLE, {.dbl=.74}, 0,  1e9, FLAGS },
+    { "delay",    "set delay in milliseconds", OFFSET(delay),    AV_OPT_TYPE_DOUBLE, {.dbl=3.},  0,  5,   FLAGS },
+    { "decay",    "set decay",                 OFFSET(decay),    AV_OPT_TYPE_DOUBLE, {.dbl=.4},  0, .99,  FLAGS },
+    { "speed",    "set modulation speed",      OFFSET(speed),    AV_OPT_TYPE_DOUBLE, {.dbl=.5}, .1,  2,   FLAGS },
+    { "type",     "set modulation type",       OFFSET(type),     AV_OPT_TYPE_INT,    {.i64=WAVE_TRI}, 0, WAVE_NB-1, FLAGS, "type" },
+    { "triangular",  NULL, 0, AV_OPT_TYPE_CONST,  {.i64=WAVE_TRI}, 0, 0, FLAGS, "type" },
+    { "t",           NULL, 0, AV_OPT_TYPE_CONST,  {.i64=WAVE_TRI}, 0, 0, FLAGS, "type" },
+    { "sinusoidal",  NULL, 0, AV_OPT_TYPE_CONST,  {.i64=WAVE_SIN}, 0, 0, FLAGS, "type" },
+    { "s",           NULL, 0, AV_OPT_TYPE_CONST,  {.i64=WAVE_SIN}, 0, 0, FLAGS, "type" },
+    { NULL },
+};
+
+AVFILTER_DEFINE_CLASS(aphaser);
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    AudioPhaserContext *p = ctx->priv;
+
+    if (p->in_gain > (1 - p->decay * p->decay))
+        av_log(ctx, AV_LOG_WARNING, "in_gain may cause clipping\n");
+    if (p->in_gain / (1 - p->decay) > 1 / p->out_gain)
+        av_log(ctx, AV_LOG_WARNING, "out_gain may cause clipping\n");
+
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    AVFilterFormats *formats;
+    AVFilterChannelLayouts *layouts;
+    static const enum AVSampleFormat sample_fmts[] = {
+        AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
+        AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
+        AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P,
+        AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,
+        AV_SAMPLE_FMT_NONE
+    };
+
+    layouts = ff_all_channel_layouts();
+    if (!layouts)
+        return AVERROR(ENOMEM);
+    ff_set_common_channel_layouts(ctx, layouts);
+
+    formats = ff_make_format_list(sample_fmts);
+    if (!formats)
+        return AVERROR(ENOMEM);
+    ff_set_common_formats(ctx, formats);
+
+    formats = ff_all_samplerates();
+    if (!formats)
+        return AVERROR(ENOMEM);
+    ff_set_common_samplerates(ctx, formats);
+
+    return 0;
+}
+
+static void generate_wave_table(enum WaveType wave_type, enum AVSampleFormat sample_fmt,
+                                void *table, int table_size,
+                                double min, double max, double phase)
+{
+    uint32_t i, phase_offset = phase / M_PI / 2 * table_size + 0.5;
+
+    for (i = 0; i < table_size; i++) {
+        uint32_t point = (i + phase_offset) % table_size;
+        double d;
+
+        switch (wave_type) {
+        case WAVE_SIN:
+            d = (sin((double)point / table_size * 2 * M_PI) + 1) / 2;
+            break;
+        case WAVE_TRI:
+            d = (double)point * 2 / table_size;
+            switch (4 * point / table_size) {
+            case 0: d = d + 0.5; break;
+            case 1:
+            case 2: d = 1.5 - d; break;
+            case 3: d = d - 1.5; break;
+            }
+            break;
+        default:
+            av_assert0(0);
+        }
+
+        d  = d * (max - min) + min;
+        switch (sample_fmt) {
+        case AV_SAMPLE_FMT_FLT: {
+            float *fp = (float *)table;
+            *fp++ = (float)d;
+            table = fp;
+            continue; }
+        case AV_SAMPLE_FMT_DBL: {
+            double *dp = (double *)table;
+            *dp++ = d;
+            table = dp;
+            continue; }
+        }
+
+        d += d < 0 ? -0.5 : 0.5;
+        switch (sample_fmt) {
+        case AV_SAMPLE_FMT_S16: {
+            int16_t *sp = table;
+            *sp++ = (int16_t)d;
+            table = sp;
+            continue; }
+        case AV_SAMPLE_FMT_S32: {
+            int32_t *ip = table;
+            *ip++ = (int32_t)d;
+            table = ip;
+            continue; }
+        default:
+            av_assert0(0);
+        }
+    }
+}
+
+#define MOD(a, b) (((a) >= (b)) ? (a) - (b) : (a))
+
+#define PHASER_PLANAR(name, type)                                      \
+static void phaser_## name ##p(AudioPhaserContext *p,                  \
+                               uint8_t * const *src, uint8_t **dst,    \
+                               int nb_samples, int channels)           \
+{                                                                      \
+    int i, c, delay_pos, modulation_pos;                               \
+                                                                       \
+    av_assert0(channels > 0);                                          \
+    for (c = 0; c < channels; c++) {                                   \
+        type *s = (type *)src[c];                                      \
+        type *d = (type *)dst[c];                                      \
+        double *buffer = p->delay_buffer +                             \
+                         c * p->delay_buffer_length;                   \
+                                                                       \
+        delay_pos      = p->delay_pos;                                 \
+        modulation_pos = p->modulation_pos;                            \
+                                                                       \
+        for (i = 0; i < nb_samples; i++, s++, d++) {                   \
+            double v = *s * p->in_gain + buffer[                       \
+                       MOD(delay_pos + p->modulation_buffer[           \
+                       modulation_pos],                                \
+                       p->delay_buffer_length)] * p->decay;            \
+                                                                       \
+            modulation_pos = MOD(modulation_pos + 1,                   \
+                             p->modulation_buffer_length);             \
+            delay_pos = MOD(delay_pos + 1, p->delay_buffer_length);    \
+            buffer[delay_pos] = v;                                     \
+                                                                       \
+            *d = v * p->out_gain;                                      \
+        }                                                              \
+    }                                                                  \
+                                                                       \
+    p->delay_pos      = delay_pos;                                     \
+    p->modulation_pos = modulation_pos;                                \
+}
+
+#define PHASER(name, type)                                              \
+static void phaser_## name (AudioPhaserContext *p,                      \
+                            uint8_t * const *src, uint8_t **dst,        \
+                            int nb_samples, int channels)               \
+{                                                                       \
+    int i, c, delay_pos, modulation_pos;                                \
+    type *s = (type *)src[0];                                           \
+    type *d = (type *)dst[0];                                           \
+    double *buffer = p->delay_buffer;                                   \
+                                                                        \
+    delay_pos      = p->delay_pos;                                      \
+    modulation_pos = p->modulation_pos;                                 \
+                                                                        \
+    for (i = 0; i < nb_samples; i++) {                                  \
+        int pos = MOD(delay_pos + p->modulation_buffer[modulation_pos], \
+                   p->delay_buffer_length) * channels;                  \
+        int npos;                                                       \
+                                                                        \
+        delay_pos = MOD(delay_pos + 1, p->delay_buffer_length);         \
+        npos = delay_pos * channels;                                    \
+        for (c = 0; c < channels; c++, s++, d++) {                      \
+            double v = *s * p->in_gain + buffer[pos + c] * p->decay;    \
+                                                                        \
+            buffer[npos + c] = v;                                       \
+                                                                        \
+            *d = v * p->out_gain;                                       \
+        }                                                               \
+                                                                        \
+        modulation_pos = MOD(modulation_pos + 1,                        \
+                         p->modulation_buffer_length);                  \
+    }                                                                   \
+                                                                        \
+    p->delay_pos      = delay_pos;                                      \
+    p->modulation_pos = modulation_pos;                                 \
+}
+
+PHASER_PLANAR(dbl, double)
+PHASER_PLANAR(flt, float)
+PHASER_PLANAR(s16, int16_t)
+PHASER_PLANAR(s32, int32_t)
+
+PHASER(dbl, double)
+PHASER(flt, float)
+PHASER(s16, int16_t)
+PHASER(s32, int32_t)
+
+static int config_output(AVFilterLink *outlink)
+{
+    AudioPhaserContext *p = outlink->src->priv;
+    AVFilterLink *inlink = outlink->src->inputs[0];
+
+    p->delay_buffer_length = p->delay * 0.001 * inlink->sample_rate + 0.5;
+    p->delay_buffer = av_calloc(p->delay_buffer_length, sizeof(*p->delay_buffer) * inlink->channels);
+    p->modulation_buffer_length = inlink->sample_rate / p->speed + 0.5;
+    p->modulation_buffer = av_malloc(p->modulation_buffer_length * sizeof(*p->modulation_buffer));
+
+    if (!p->modulation_buffer || !p->delay_buffer)
+        return AVERROR(ENOMEM);
+
+    generate_wave_table(p->type, AV_SAMPLE_FMT_S32,
+                        p->modulation_buffer, p->modulation_buffer_length,
+                        1., p->delay_buffer_length, M_PI / 2.0);
+
+    p->delay_pos = p->modulation_pos = 0;
+
+    switch (inlink->format) {
+    case AV_SAMPLE_FMT_DBL:  p->phaser = phaser_dbl;  break;
+    case AV_SAMPLE_FMT_DBLP: p->phaser = phaser_dblp; break;
+    case AV_SAMPLE_FMT_FLT:  p->phaser = phaser_flt;  break;
+    case AV_SAMPLE_FMT_FLTP: p->phaser = phaser_fltp; break;
+    case AV_SAMPLE_FMT_S16:  p->phaser = phaser_s16;  break;
+    case AV_SAMPLE_FMT_S16P: p->phaser = phaser_s16p; break;
+    case AV_SAMPLE_FMT_S32:  p->phaser = phaser_s32;  break;
+    case AV_SAMPLE_FMT_S32P: p->phaser = phaser_s32p; break;
+    default: av_assert0(0);
+    }
+
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
+{
+    AudioPhaserContext *p = inlink->dst->priv;
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+    AVFrame *outbuf;
+
+    if (av_frame_is_writable(inbuf)) {
+        outbuf = inbuf;
+    } else {
+        outbuf = ff_get_audio_buffer(inlink, inbuf->nb_samples);
+        if (!outbuf)
+            return AVERROR(ENOMEM);
+        av_frame_copy_props(outbuf, inbuf);
+    }
+
+    p->phaser(p, inbuf->extended_data, outbuf->extended_data,
+              outbuf->nb_samples, av_frame_get_channels(outbuf));
+
+    if (inbuf != outbuf)
+        av_frame_free(&inbuf);
+
+    return ff_filter_frame(outlink, outbuf);
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    AudioPhaserContext *p = ctx->priv;
+
+    av_freep(&p->delay_buffer);
+    av_freep(&p->modulation_buffer);
+}
+
+static const AVFilterPad aphaser_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_AUDIO,
+        .filter_frame = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad aphaser_outputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_AUDIO,
+        .config_props = config_output,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_af_aphaser = {
+    .name          = "aphaser",
+    .description   = NULL_IF_CONFIG_SMALL("Add a phasing effect to the audio."),
+    .query_formats = query_formats,
+    .priv_size     = sizeof(AudioPhaserContext),
+    .init          = init,
+    .uninit        = uninit,
+    .inputs        = aphaser_inputs,
+    .outputs       = aphaser_outputs,
+    .priv_class    = &aphaser_class,
+};
diff --git a/libavfilter/af_aresample.c b/libavfilter/af_aresample.c
index 66a8a53..b88c5f8 100644
--- a/libavfilter/af_aresample.c
+++ b/libavfilter/af_aresample.c
@@ -35,17 +35,18 @@
 #include "internal.h"
 
 typedef struct {
+    const AVClass *class;
+    int sample_rate_arg;
     double ratio;
     struct SwrContext *swr;
     int64_t next_pts;
     int req_fullfilled;
 } AResampleContext;
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init_dict(AVFilterContext *ctx, AVDictionary **opts)
 {
     AResampleContext *aresample = ctx->priv;
     int ret = 0;
-    char *argd = av_strdup(args);
 
     aresample->next_pts = AV_NOPTS_VALUE;
     aresample->swr = swr_alloc();
@@ -54,27 +55,20 @@
         goto end;
     }
 
-    if (args) {
-        char *ptr = argd, *token;
+    if (opts) {
+        AVDictionaryEntry *e = NULL;
 
-        while (token = av_strtok(ptr, ":", &ptr)) {
-            char *value;
-            av_strtok(token, "=", &value);
-
-            if (value) {
-                if ((ret = av_opt_set(aresample->swr, token, value, 0)) < 0)
-                    goto end;
-            } else {
-                int out_rate;
-                if ((ret = ff_parse_sample_rate(&out_rate, token, ctx)) < 0)
-                    goto end;
-                if ((ret = av_opt_set_int(aresample->swr, "osr", out_rate, 0)) < 0)
-                    goto end;
-            }
+        while ((e = av_dict_get(*opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
+            const char *token = e->key;
+            const char *value = e->value;
+            if ((ret = av_opt_set(aresample->swr, token, value, 0)) < 0)
+                goto end;
         }
+        av_dict_free(opts);
     }
+    if (aresample->sample_rate_arg > 0)
+        av_opt_set_int(aresample->swr, "osr", aresample->sample_rate_arg, 0);
 end:
-    av_free(argd);
     return ret;
 }
 
@@ -174,23 +168,23 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
+static int filter_frame(AVFilterLink *inlink, AVFrame *insamplesref)
 {
     AResampleContext *aresample = inlink->dst->priv;
-    const int n_in  = insamplesref->audio->nb_samples;
+    const int n_in  = insamplesref->nb_samples;
     int n_out       = n_in * aresample->ratio * 2 + 256;
     AVFilterLink *const outlink = inlink->dst->outputs[0];
-    AVFilterBufferRef *outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n_out);
+    AVFrame *outsamplesref = ff_get_audio_buffer(outlink, n_out);
     int ret;
 
     if(!outsamplesref)
         return AVERROR(ENOMEM);
 
-    avfilter_copy_buffer_ref_props(outsamplesref, insamplesref);
+    av_frame_copy_props(outsamplesref, insamplesref);
     outsamplesref->format                = outlink->format;
-    outsamplesref->audio->channels       = outlink->channels;
-    outsamplesref->audio->channel_layout = outlink->channel_layout;
-    outsamplesref->audio->sample_rate    = outlink->sample_rate;
+    av_frame_set_channels(outsamplesref, outlink->channels);
+    outsamplesref->channel_layout        = outlink->channel_layout;
+    outsamplesref->sample_rate           = outlink->sample_rate;
 
     if(insamplesref->pts != AV_NOPTS_VALUE) {
         int64_t inpts = av_rescale(insamplesref->pts, inlink->time_base.num * (int64_t)outlink->sample_rate * inlink->sample_rate, inlink->time_base.den);
@@ -203,16 +197,16 @@
     n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out,
                                  (void *)insamplesref->extended_data, n_in);
     if (n_out <= 0) {
-        avfilter_unref_buffer(outsamplesref);
-        avfilter_unref_buffer(insamplesref);
+        av_frame_free(&outsamplesref);
+        av_frame_free(&insamplesref);
         return 0;
     }
 
-    outsamplesref->audio->nb_samples  = n_out;
+    outsamplesref->nb_samples  = n_out;
 
     ret = ff_filter_frame(outlink, outsamplesref);
     aresample->req_fullfilled= 1;
-    avfilter_unref_buffer(insamplesref);
+    av_frame_free(&insamplesref);
     return ret;
 }
 
@@ -229,20 +223,20 @@
     }while(!aresample->req_fullfilled && ret>=0);
 
     if (ret == AVERROR_EOF) {
-        AVFilterBufferRef *outsamplesref;
+        AVFrame *outsamplesref;
         int n_out = 4096;
 
-        outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n_out);
+        outsamplesref = ff_get_audio_buffer(outlink, n_out);
         if (!outsamplesref)
             return AVERROR(ENOMEM);
         n_out = swr_convert(aresample->swr, outsamplesref->extended_data, n_out, 0, 0);
         if (n_out <= 0) {
-            avfilter_unref_buffer(outsamplesref);
+            av_frame_free(&outsamplesref);
             return (n_out == 0) ? AVERROR_EOF : n_out;
         }
 
-        outsamplesref->audio->sample_rate = outlink->sample_rate;
-        outsamplesref->audio->nb_samples  = n_out;
+        outsamplesref->sample_rate = outlink->sample_rate;
+        outsamplesref->nb_samples  = n_out;
 #if 0
         outsamplesref->pts = aresample->next_pts;
         if(aresample->next_pts != AV_NOPTS_VALUE)
@@ -252,18 +246,44 @@
         outsamplesref->pts = ROUNDED_DIV(outsamplesref->pts, inlink->sample_rate);
 #endif
 
-        ff_filter_frame(outlink, outsamplesref);
-        return 0;
+        return ff_filter_frame(outlink, outsamplesref);
     }
     return ret;
 }
 
+static const AVClass *resample_child_class_next(const AVClass *prev)
+{
+    return prev ? NULL : swr_get_class();
+}
+
+static void *resample_child_next(void *obj, void *prev)
+{
+    AResampleContext *s = obj;
+    return prev ? NULL : s->swr;
+}
+
+#define OFFSET(x) offsetof(AResampleContext, x)
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption options[] = {
+    {"sample_rate", NULL, OFFSET(sample_rate_arg), AV_OPT_TYPE_INT, {.i64=0},  0,        INT_MAX, FLAGS },
+    {NULL}
+};
+
+static const AVClass aresample_class = {
+    .class_name       = "aresample",
+    .item_name        = av_default_item_name,
+    .option           = options,
+    .version          = LIBAVUTIL_VERSION_INT,
+    .child_class_next = resample_child_class_next,
+    .child_next       = resample_child_next,
+};
+
 static const AVFilterPad aresample_inputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_AUDIO,
         .filter_frame = filter_frame,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL },
 };
@@ -281,10 +301,11 @@
 AVFilter avfilter_af_aresample = {
     .name          = "aresample",
     .description   = NULL_IF_CONFIG_SMALL("Resample audio data."),
-    .init          = init,
+    .init_dict     = init_dict,
     .uninit        = uninit,
     .query_formats = query_formats,
     .priv_size     = sizeof(AResampleContext),
+    .priv_class    = &aresample_class,
     .inputs        = aresample_inputs,
     .outputs       = aresample_outputs,
 };
diff --git a/libavfilter/af_asetnsamples.c b/libavfilter/af_asetnsamples.c
index ee80c1c..25e062a 100644
--- a/libavfilter/af_asetnsamples.c
+++ b/libavfilter/af_asetnsamples.c
@@ -38,7 +38,6 @@
     int nb_out_samples;  ///< how many samples to output
     AVAudioFifo *fifo;   ///< samples are queued here
     int64_t next_out_pts;
-    int req_fullfilled;
     int pad;
 } ASNSContext;
 
@@ -46,25 +45,18 @@
 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
 
 static const AVOption asetnsamples_options[] = {
-{ "pad", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
-{ "p",   "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
-{ "nb_out_samples", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
-{ "n",              "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
-{ NULL }
+    { "nb_out_samples", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
+    { "n",              "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
+    { "pad", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
+    { "p",   "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
+    { NULL }
 };
 
 AVFILTER_DEFINE_CLASS(asetnsamples);
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     ASNSContext *asns = ctx->priv;
-    int err;
-
-    asns->class = &asetnsamples_class;
-    av_opt_set_defaults(asns);
-
-    if ((err = av_set_options_string(asns, args, "=", ":")) < 0)
-        return err;
 
     asns->next_out_pts = AV_NOPTS_VALUE;
     av_log(ctx, AV_LOG_VERBOSE, "nb_out_samples:%d pad:%d\n", asns->nb_out_samples, asns->pad);
@@ -86,6 +78,7 @@
     asns->fifo = av_audio_fifo_alloc(outlink->format, nb_channels, asns->nb_out_samples);
     if (!asns->fifo)
         return AVERROR(ENOMEM);
+    outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
 
     return 0;
 }
@@ -93,8 +86,8 @@
 static int push_samples(AVFilterLink *outlink)
 {
     ASNSContext *asns = outlink->src->priv;
-    AVFilterBufferRef *outsamples = NULL;
-    int nb_out_samples, nb_pad_samples;
+    AVFrame *outsamples = NULL;
+    int ret, nb_out_samples, nb_pad_samples;
 
     if (asns->pad) {
         nb_out_samples = av_audio_fifo_size(asns->fifo) ? asns->nb_out_samples : 0;
@@ -107,8 +100,9 @@
     if (!nb_out_samples)
         return 0;
 
-    outsamples = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_out_samples);
-    av_assert0(outsamples);
+    outsamples = ff_get_audio_buffer(outlink, nb_out_samples);
+    if (!outsamples)
+        return AVERROR(ENOMEM);
 
     av_audio_fifo_read(asns->fifo,
                        (void **)outsamples->extended_data, nb_out_samples);
@@ -117,26 +111,27 @@
         av_samples_set_silence(outsamples->extended_data, nb_out_samples - nb_pad_samples,
                                nb_pad_samples, av_get_channel_layout_nb_channels(outlink->channel_layout),
                                outlink->format);
-    outsamples->audio->nb_samples     = nb_out_samples;
-    outsamples->audio->channel_layout = outlink->channel_layout;
-    outsamples->audio->sample_rate    = outlink->sample_rate;
+    outsamples->nb_samples     = nb_out_samples;
+    outsamples->channel_layout = outlink->channel_layout;
+    outsamples->sample_rate    = outlink->sample_rate;
     outsamples->pts = asns->next_out_pts;
 
     if (asns->next_out_pts != AV_NOPTS_VALUE)
         asns->next_out_pts += nb_out_samples;
 
-    ff_filter_frame(outlink, outsamples);
-    asns->req_fullfilled = 1;
+    ret = ff_filter_frame(outlink, outsamples);
+    if (ret < 0)
+        return ret;
     return nb_out_samples;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamples)
+static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
 {
     AVFilterContext *ctx = inlink->dst;
     ASNSContext *asns = ctx->priv;
     AVFilterLink *outlink = ctx->outputs[0];
     int ret;
-    int nb_samples = insamples->audio->nb_samples;
+    int nb_samples = insamples->nb_samples;
 
     if (av_audio_fifo_space(asns->fifo) < nb_samples) {
         av_log(ctx, AV_LOG_DEBUG, "No space for %d samples, stretching audio fifo\n", nb_samples);
@@ -150,7 +145,7 @@
     av_audio_fifo_write(asns->fifo, (void **)insamples->extended_data, nb_samples);
     if (asns->next_out_pts == AV_NOPTS_VALUE)
         asns->next_out_pts = insamples->pts;
-    avfilter_unref_buffer(insamples);
+    av_frame_free(&insamples);
 
     while (av_audio_fifo_size(asns->fifo) >= asns->nb_out_samples)
         push_samples(outlink);
@@ -159,28 +154,24 @@
 
 static int request_frame(AVFilterLink *outlink)
 {
-    ASNSContext *asns = outlink->src->priv;
     AVFilterLink *inlink = outlink->src->inputs[0];
     int ret;
 
-    asns->req_fullfilled = 0;
-    do {
-        ret = ff_request_frame(inlink);
-    } while (!asns->req_fullfilled && ret >= 0);
-
-    if (ret == AVERROR_EOF)
-        while (push_samples(outlink))
-            ;
+    ret = ff_request_frame(inlink);
+    if (ret == AVERROR_EOF) {
+        ret = push_samples(outlink);
+        return ret < 0 ? ret : ret > 0 ? 0 : AVERROR_EOF;
+    }
 
     return ret;
 }
 
 static const AVFilterPad asetnsamples_inputs[] = {
     {
-        .name         = "default",
-        .type         = AVMEDIA_TYPE_AUDIO,
-        .filter_frame = filter_frame,
-        .min_perms    = AV_PERM_READ | AV_PERM_WRITE,
+        .name           = "default",
+        .type           = AVMEDIA_TYPE_AUDIO,
+        .filter_frame   = filter_frame,
+        .needs_writable = 1,
     },
     {  NULL }
 };
diff --git a/libavfilter/af_asetrate.c b/libavfilter/af_asetrate.c
new file mode 100644
index 0000000..e031b89
--- /dev/null
+++ b/libavfilter/af_asetrate.c
@@ -0,0 +1,119 @@
+/*
+ * Copyright (c) 2013 Nicolas George
+ *
+ * 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 "libavutil/opt.h"
+#include "avfilter.h"
+#include "internal.h"
+
+typedef struct {
+    const AVClass *class;
+    int sample_rate;
+    int rescale_pts;
+} ASetRateContext;
+
+#define CONTEXT ASetRateContext
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+#define OPT_GENERIC(name, field, def, min, max, descr, type, deffield, ...) \
+    { name, descr, offsetof(CONTEXT, field), AV_OPT_TYPE_ ## type,          \
+      { .deffield = def }, min, max, FLAGS, __VA_ARGS__ }
+
+#define OPT_INT(name, field, def, min, max, descr, ...) \
+    OPT_GENERIC(name, field, def, min, max, descr, INT, i64, __VA_ARGS__)
+
+static const AVOption asetrate_options[] = {
+    OPT_INT("sample_rate", sample_rate, 44100, 1, INT_MAX, "set the sample rate"),
+    OPT_INT("r",           sample_rate, 44100, 1, INT_MAX, "set the sample rate"),
+    {NULL},
+};
+
+AVFILTER_DEFINE_CLASS(asetrate);
+
+static av_cold int query_formats(AVFilterContext *ctx)
+{
+    ASetRateContext *sr = ctx->priv;
+    int sample_rates[] = { sr->sample_rate, -1 };
+
+    ff_formats_ref(ff_make_format_list(sample_rates),
+                   &ctx->outputs[0]->in_samplerates);
+    return 0;
+}
+
+static av_cold int config_props(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    ASetRateContext *sr = ctx->priv;
+    AVFilterLink *inlink = ctx->inputs[0];
+    AVRational intb = ctx->inputs[0]->time_base;
+    int inrate = inlink->sample_rate;
+
+    if (intb.num == 1 && intb.den == inrate) {
+        outlink->time_base.num = 1;
+        outlink->time_base.den = outlink->sample_rate;
+    } else {
+        outlink->time_base = intb;
+        sr->rescale_pts = 1;
+        if (av_q2d(intb) > 1.0 / FFMAX(inrate, outlink->sample_rate))
+            av_log(ctx, AV_LOG_WARNING, "Time base is inaccurate\n");
+    }
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
+{
+    AVFilterContext *ctx = inlink->dst;
+    ASetRateContext *sr = ctx->priv;
+    AVFilterLink *outlink = ctx->outputs[0];
+
+    frame->sample_rate = outlink->sample_rate;
+    if (sr->rescale_pts)
+        frame->pts = av_rescale(frame->pts, inlink->sample_rate,
+                                           outlink->sample_rate);
+    return ff_filter_frame(outlink, frame);
+}
+
+static const AVFilterPad asetrate_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_AUDIO,
+        .filter_frame = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad asetrate_outputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_AUDIO,
+        .config_props = config_props,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_af_asetrate = {
+    .name          = "asetrate",
+    .description   = NULL_IF_CONFIG_SMALL("Change the sample rate without "
+                                          "altering the data."),
+    .query_formats = query_formats,
+    .priv_size     = sizeof(ASetRateContext),
+    .inputs        = asetrate_inputs,
+    .outputs       = asetrate_outputs,
+    .priv_class    = &asetrate_class,
+};
diff --git a/libavfilter/af_ashowinfo.c b/libavfilter/af_ashowinfo.c
index 7e7543f..f53584e 100644
--- a/libavfilter/af_ashowinfo.c
+++ b/libavfilter/af_ashowinfo.c
@@ -55,16 +55,16 @@
     av_freep(&s->plane_checksums);
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
 {
     AVFilterContext *ctx = inlink->dst;
     AShowInfoContext *s  = ctx->priv;
     char chlayout_str[128];
     uint32_t checksum = 0;
-    int channels    = av_get_channel_layout_nb_channels(buf->audio->channel_layout);
+    int channels    = av_get_channel_layout_nb_channels(buf->channel_layout);
     int planar      = av_sample_fmt_is_planar(buf->format);
     int block_align = av_get_bytes_per_sample(buf->format) * (planar ? 1 : channels);
-    int data_size   = buf->audio->nb_samples * block_align;
+    int data_size   = buf->nb_samples * block_align;
     int planes      = planar ? channels : 1;
     int i;
     void *tmp_ptr = av_realloc(s->plane_checksums, channels * sizeof(*s->plane_checksums));
@@ -82,7 +82,7 @@
     }
 
     av_get_channel_layout_string(chlayout_str, sizeof(chlayout_str), -1,
-                                 buf->audio->channel_layout);
+                                 buf->channel_layout);
 
     av_log(ctx, AV_LOG_INFO,
            "n:%"PRIu64" pts:%s pts_time:%s pos:%"PRId64" "
@@ -90,9 +90,9 @@
            "checksum:%08X ",
            s->frame,
            av_ts2str(buf->pts), av_ts2timestr(buf->pts, &inlink->time_base),
-           buf->pos,
-           av_get_sample_fmt_name(buf->format), buf->audio->channels, chlayout_str,
-           buf->audio->sample_rate, buf->audio->nb_samples,
+           av_frame_get_pkt_pos(buf),
+           av_get_sample_fmt_name(buf->format), av_frame_get_channels(buf), chlayout_str,
+           buf->sample_rate, buf->nb_samples,
            checksum);
 
     av_log(ctx, AV_LOG_INFO, "plane_checksums: [ ");
@@ -110,7 +110,6 @@
         .type             = AVMEDIA_TYPE_AUDIO,
         .get_audio_buffer = ff_null_get_audio_buffer,
         .filter_frame     = filter_frame,
-        .min_perms        = AV_PERM_READ,
     },
     { NULL },
 };
diff --git a/libavfilter/af_astreamsync.c b/libavfilter/af_astreamsync.c
index 269ffc1..1ea3a8a 100644
--- a/libavfilter/af_astreamsync.c
+++ b/libavfilter/af_astreamsync.c
@@ -24,6 +24,7 @@
  */
 
 #include "libavutil/eval.h"
+#include "libavutil/opt.h"
 #include "avfilter.h"
 #include "audio.h"
 #include "internal.h"
@@ -45,10 +46,12 @@
 };
 
 typedef struct {
+    const AVClass *class;
     AVExpr *expr;
+    char *expr_str;
     double var_values[VAR_NB];
     struct buf_queue {
-        AVFilterBufferRef *buf[QUEUE_SIZE];
+        AVFrame *buf[QUEUE_SIZE];
         unsigned tail, nb;
         /* buf[tail] is the oldest,
            buf[(tail + nb) % QUEUE_SIZE] is where the next is added */
@@ -58,18 +61,25 @@
     int eof; /* bitmask, one bit for each stream */
 } AStreamSyncContext;
 
-static const char *default_expr = "t1-t2";
+#define OFFSET(x) offsetof(AStreamSyncContext, x)
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+static const AVOption astreamsync_options[] = {
+    { "expr", "set stream selection expression", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "t1-t2" }, .flags = FLAGS },
+    { "e",    "set stream selection expression", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "t1-t2" }, .flags = FLAGS },
+    { NULL }
+};
 
-static av_cold int init(AVFilterContext *ctx, const char *args0)
+AVFILTER_DEFINE_CLASS(astreamsync);
+
+static av_cold int init(AVFilterContext *ctx)
 {
     AStreamSyncContext *as = ctx->priv;
-    const char *expr = args0 ? args0 : default_expr;
     int r, i;
 
-    r = av_expr_parse(&as->expr, expr, var_names,
+    r = av_expr_parse(&as->expr, as->expr_str, var_names,
                       NULL, NULL, NULL, NULL, 0, ctx);
     if (r < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Error in expression \"%s\"\n", expr);
+        av_log(ctx, AV_LOG_ERROR, "Error in expression \"%s\"\n", as->expr_str);
         return r;
     }
     for (i = 0; i < 42; i++)
@@ -111,16 +121,16 @@
 {
     AStreamSyncContext *as = ctx->priv;
     struct buf_queue *queue = &as->queue[out_id];
-    AVFilterBufferRef *buf = queue->buf[queue->tail];
+    AVFrame *buf = queue->buf[queue->tail];
     int ret;
 
     queue->buf[queue->tail] = NULL;
     as->var_values[VAR_B1 + out_id]++;
-    as->var_values[VAR_S1 + out_id] += buf->audio->nb_samples;
+    as->var_values[VAR_S1 + out_id] += buf->nb_samples;
     if (buf->pts != AV_NOPTS_VALUE)
         as->var_values[VAR_T1 + out_id] =
             av_q2d(ctx->outputs[out_id]->time_base) * buf->pts;
-    as->var_values[VAR_T1 + out_id] += buf->audio->nb_samples /
+    as->var_values[VAR_T1 + out_id] += buf->nb_samples /
                                    (double)ctx->inputs[out_id]->sample_rate;
     ret = ff_filter_frame(ctx->outputs[out_id], buf);
     queue->nb--;
@@ -167,7 +177,7 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamples)
+static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
 {
     AVFilterContext *ctx = inlink->dst;
     AStreamSyncContext *as = ctx->priv;
@@ -180,17 +190,23 @@
     return 0;
 }
 
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    AStreamSyncContext *as = ctx->priv;
+
+    av_expr_free(as->expr);
+    as->expr = NULL;
+}
+
 static const AVFilterPad astreamsync_inputs[] = {
     {
         .name         = "in1",
         .type         = AVMEDIA_TYPE_AUDIO,
         .filter_frame = filter_frame,
-        .min_perms    = AV_PERM_READ | AV_PERM_PRESERVE,
     },{
         .name         = "in2",
         .type         = AVMEDIA_TYPE_AUDIO,
         .filter_frame = filter_frame,
-        .min_perms    = AV_PERM_READ | AV_PERM_PRESERVE,
     },
     { NULL }
 };
@@ -216,7 +232,9 @@
                                           "in a configurable order."),
     .priv_size     = sizeof(AStreamSyncContext),
     .init          = init,
+    .uninit        = uninit,
     .query_formats = query_formats,
     .inputs        = astreamsync_inputs,
     .outputs       = astreamsync_outputs,
+    .priv_class    = &astreamsync_class,
 };
diff --git a/libavfilter/af_asyncts.c b/libavfilter/af_asyncts.c
index 500be0f..13c5a91 100644
--- a/libavfilter/af_asyncts.c
+++ b/libavfilter/af_asyncts.c
@@ -35,6 +35,7 @@
     int min_delta;          ///< pad/trim min threshold in samples
     int first_frame;        ///< 1 until filter_frame() has processed at least 1 frame with a pts != AV_NOPTS_VALUE
     int64_t first_pts;      ///< user-specified first expected pts, in samples
+    int comp;               ///< current resample compensation
 
     /* options */
     int resample;
@@ -59,17 +60,9 @@
 
 AVFILTER_DEFINE_CLASS(asyncts);
 
-static int init(AVFilterContext *ctx, const char *args)
+static int init(AVFilterContext *ctx)
 {
     ASyncContext *s = ctx->priv;
-    int ret;
-
-    s->class = &asyncts_class;
-    av_opt_set_defaults(s);
-
-    if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
-        return ret;
-    av_opt_free(s);
 
     s->pts         = AV_NOPTS_VALUE;
     s->first_frame = 1;
@@ -152,14 +145,13 @@
             handle_trimming(ctx);
 
         if (nb_samples = get_delay(s)) {
-            AVFilterBufferRef *buf = ff_get_audio_buffer(link, AV_PERM_WRITE,
-                                                         nb_samples);
+            AVFrame *buf = ff_get_audio_buffer(link, nb_samples);
             if (!buf)
                 return AVERROR(ENOMEM);
             ret = avresample_convert(s->avr, buf->extended_data,
                                      buf->linesize[0], nb_samples, NULL, 0, 0);
             if (ret <= 0) {
-                avfilter_unref_bufferp(&buf);
+                av_frame_free(&buf);
                 return (ret < 0) ? ret : AVERROR_EOF;
             }
 
@@ -171,24 +163,25 @@
     return ret;
 }
 
-static int write_to_fifo(ASyncContext *s, AVFilterBufferRef *buf)
+static int write_to_fifo(ASyncContext *s, AVFrame *buf)
 {
     int ret = avresample_convert(s->avr, NULL, 0, 0, buf->extended_data,
-                                 buf->linesize[0], buf->audio->nb_samples);
-    avfilter_unref_buffer(buf);
+                                 buf->linesize[0], buf->nb_samples);
+    av_frame_free(&buf);
     return ret;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
 {
     AVFilterContext  *ctx = inlink->dst;
     ASyncContext       *s = ctx->priv;
     AVFilterLink *outlink = ctx->outputs[0];
-    int nb_channels = av_get_channel_layout_nb_channels(buf->audio->channel_layout);
+    int nb_channels = av_get_channel_layout_nb_channels(buf->channel_layout);
     int64_t pts = (buf->pts == AV_NOPTS_VALUE) ? buf->pts :
                   av_rescale_q(buf->pts, inlink->time_base, outlink->time_base);
     int out_size, ret;
     int64_t delta;
+    int64_t new_pts;
 
     /* buffer data until we get the next timestamp */
     if (s->pts == AV_NOPTS_VALUE || pts == AV_NOPTS_VALUE) {
@@ -215,16 +208,24 @@
         out_size = av_clipl_int32((int64_t)out_size + delta);
     } else {
         if (s->resample) {
-            int comp = av_clip(delta, -s->max_comp, s->max_comp);
-            av_log(ctx, AV_LOG_VERBOSE, "Compensating %d samples per second.\n", comp);
-            avresample_set_compensation(s->avr, comp, inlink->sample_rate);
+            // adjust the compensation if delta is non-zero
+            int delay = get_delay(s);
+            int comp = s->comp + av_clip(delta * inlink->sample_rate / delay,
+                                         -s->max_comp, s->max_comp);
+            if (comp != s->comp) {
+                av_log(ctx, AV_LOG_VERBOSE, "Compensating %d samples per second.\n", comp);
+                if (avresample_set_compensation(s->avr, comp, inlink->sample_rate) == 0) {
+                    s->comp = comp;
+                }
+            }
         }
+        // adjust PTS to avoid monotonicity errors with input PTS jitter
+        pts -= delta;
         delta = 0;
     }
 
     if (out_size > 0) {
-        AVFilterBufferRef *buf_out = ff_get_audio_buffer(outlink, AV_PERM_WRITE,
-                                                         out_size);
+        AVFrame *buf_out = ff_get_audio_buffer(outlink, out_size);
         if (!buf_out) {
             ret = AVERROR(ENOMEM);
             goto fail;
@@ -264,13 +265,21 @@
     /* drain any remaining buffered data */
     avresample_read(s->avr, NULL, avresample_available(s->avr));
 
-    s->pts = pts - avresample_get_delay(s->avr);
-    ret = avresample_convert(s->avr, NULL, 0, 0, buf->extended_data,
-                             buf->linesize[0], buf->audio->nb_samples);
+    new_pts = pts - avresample_get_delay(s->avr);
+    /* check for s->pts monotonicity */
+    if (new_pts > s->pts) {
+        s->pts = new_pts;
+        ret = avresample_convert(s->avr, NULL, 0, 0, buf->extended_data,
+                                 buf->linesize[0], buf->nb_samples);
+    } else {
+        av_log(ctx, AV_LOG_WARNING, "Non-monotonous timestamps, dropping "
+               "whole buffer.\n");
+        ret = 0;
+    }
 
     s->first_frame = 0;
 fail:
-    avfilter_unref_buffer(buf);
+    av_frame_free(&buf);
 
     return ret;
 }
@@ -302,8 +311,8 @@
     .uninit      = uninit,
 
     .priv_size   = sizeof(ASyncContext),
+    .priv_class  = &asyncts_class,
 
     .inputs      = avfilter_af_asyncts_inputs,
     .outputs     = avfilter_af_asyncts_outputs,
-    .priv_class = &asyncts_class,
 };
diff --git a/libavfilter/af_atempo.c b/libavfilter/af_atempo.c
index d186aaf..ad12786 100644
--- a/libavfilter/af_atempo.c
+++ b/libavfilter/af_atempo.c
@@ -85,6 +85,8 @@
  * Filter state machine
  */
 typedef struct {
+    const AVClass *class;
+
     // ring-buffer of input samples, necessary because some times
     // input fragment position may be adjusted backwards:
     uint8_t *buffer;
@@ -121,8 +123,9 @@
     // tempo scaling factor:
     double tempo;
 
-    // cumulative alignment drift:
-    int drift;
+    // a snapshot of previous fragment input and output position values
+    // captured when the tempo scale factor was set most recently:
+    int64_t origin[2];
 
     // current/previous fragment ring-buffer:
     AudioFragment frag[2];
@@ -139,14 +142,34 @@
     FFTSample *correlation;
 
     // for managing AVFilterPad.request_frame and AVFilterPad.filter_frame
-    int request_fulfilled;
-    AVFilterBufferRef *dst_buffer;
+    AVFrame *dst_buffer;
     uint8_t *dst;
     uint8_t *dst_end;
     uint64_t nsamples_in;
     uint64_t nsamples_out;
 } ATempoContext;
 
+#define OFFSET(x) offsetof(ATempoContext, x)
+
+static const AVOption atempo_options[] = {
+    { "tempo", "set tempo scale factor",
+      OFFSET(tempo), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 }, 0.5, 2.0,
+      AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(atempo);
+
+inline static AudioFragment *yae_curr_frag(ATempoContext *atempo)
+{
+    return &atempo->frag[atempo->nfrag % 2];
+}
+
+inline static AudioFragment *yae_prev_frag(ATempoContext *atempo)
+{
+    return &atempo->frag[(atempo->nfrag + 1) % 2];
+}
+
 /**
  * Reset filter to initial state, do not deallocate existing local buffers.
  */
@@ -156,13 +179,15 @@
     atempo->head = 0;
     atempo->tail = 0;
 
-    atempo->drift = 0;
     atempo->nfrag = 0;
     atempo->state = YAE_LOAD_FRAGMENT;
 
     atempo->position[0] = 0;
     atempo->position[1] = 0;
 
+    atempo->origin[0] = 0;
+    atempo->origin[1] = 0;
+
     atempo->frag[0].position[0] = 0;
     atempo->frag[0].position[1] = 0;
     atempo->frag[0].nsamples    = 0;
@@ -177,11 +202,10 @@
     atempo->frag[0].position[0] = -(int64_t)(atempo->window / 2);
     atempo->frag[0].position[1] = -(int64_t)(atempo->window / 2);
 
-    avfilter_unref_bufferp(&atempo->dst_buffer);
+    av_frame_free(&atempo->dst_buffer);
     atempo->dst     = NULL;
     atempo->dst_end = NULL;
 
-    atempo->request_fulfilled = 0;
     atempo->nsamples_in       = 0;
     atempo->nsamples_out      = 0;
 }
@@ -297,6 +321,7 @@
 
 static int yae_set_tempo(AVFilterContext *ctx, const char *arg_tempo)
 {
+    const AudioFragment *prev;
     ATempoContext *atempo = ctx->priv;
     char   *tail = NULL;
     double tempo = av_strtod(arg_tempo, &tail);
@@ -312,20 +337,13 @@
         return AVERROR(EINVAL);
     }
 
+    prev = yae_prev_frag(atempo);
+    atempo->origin[0] = prev->position[0] + atempo->window / 2;
+    atempo->origin[1] = prev->position[1] + atempo->window / 2;
     atempo->tempo = tempo;
     return 0;
 }
 
-inline static AudioFragment *yae_curr_frag(ATempoContext *atempo)
-{
-    return &atempo->frag[atempo->nfrag % 2];
-}
-
-inline static AudioFragment *yae_prev_frag(ATempoContext *atempo)
-{
-    return &atempo->frag[(atempo->nfrag + 1) % 2];
-}
-
 /**
  * A helper macro for initializing complex data buffer with scalar data
  * of a given type.
@@ -678,12 +696,21 @@
     const AudioFragment *prev = yae_prev_frag(atempo);
     AudioFragment       *frag = yae_curr_frag(atempo);
 
+    const double prev_output_position =
+        (double)(prev->position[1] - atempo->origin[1] + atempo->window / 2);
+
+    const double ideal_output_position =
+        (double)(prev->position[0] - atempo->origin[0] + atempo->window / 2) /
+        atempo->tempo;
+
+    const int drift = (int)(prev_output_position - ideal_output_position);
+
     const int delta_max  = atempo->window / 2;
     const int correction = yae_align(frag,
                                      prev,
                                      atempo->window,
                                      delta_max,
-                                     atempo->drift,
+                                     drift,
                                      atempo->correlation,
                                      atempo->complex_to_real);
 
@@ -693,9 +720,6 @@
 
         // clear so that the fragment can be reloaded:
         frag->nsamples = 0;
-
-        // update cumulative correction drift counter:
-        atempo->drift += correction;
     }
 
     return correction;
@@ -949,16 +973,12 @@
     return atempo->position[1] == stop_here ? 0 : AVERROR(EAGAIN);
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     ATempoContext *atempo = ctx->priv;
-
-    // NOTE: this assumes that the caller has memset ctx->priv to 0:
     atempo->format = AV_SAMPLE_FMT_NONE;
-    atempo->tempo  = 1.0;
     atempo->state  = YAE_LOAD_FRAGMENT;
-
-    return args ? yae_set_tempo(ctx, args) : 0;
+    return 0;
 }
 
 static av_cold void uninit(AVFilterContext *ctx)
@@ -1017,15 +1037,19 @@
     int sample_rate = (int)inlink->sample_rate;
     int channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
 
+    ctx->outputs[0]->flags |= FF_LINK_FLAG_REQUEST_LOOP;
+
     return yae_reset(atempo, format, sample_rate, channels);
 }
 
-static void push_samples(ATempoContext *atempo,
-                         AVFilterLink *outlink,
-                         int n_out)
+static int push_samples(ATempoContext *atempo,
+                        AVFilterLink *outlink,
+                        int n_out)
 {
-    atempo->dst_buffer->audio->sample_rate = outlink->sample_rate;
-    atempo->dst_buffer->audio->nb_samples  = n_out;
+    int ret;
+
+    atempo->dst_buffer->sample_rate = outlink->sample_rate;
+    atempo->dst_buffer->nb_samples  = n_out;
 
     // adjust the PTS:
     atempo->dst_buffer->pts =
@@ -1033,22 +1057,25 @@
                      (AVRational){ 1, outlink->sample_rate },
                      outlink->time_base);
 
-    ff_filter_frame(outlink, atempo->dst_buffer);
+    ret = ff_filter_frame(outlink, atempo->dst_buffer);
+    if (ret < 0)
+        return ret;
     atempo->dst_buffer = NULL;
     atempo->dst        = NULL;
     atempo->dst_end    = NULL;
 
     atempo->nsamples_out += n_out;
+    return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink,
-                           AVFilterBufferRef *src_buffer)
+static int filter_frame(AVFilterLink *inlink, AVFrame *src_buffer)
 {
     AVFilterContext  *ctx = inlink->dst;
     ATempoContext *atempo = ctx->priv;
     AVFilterLink *outlink = ctx->outputs[0];
 
-    int n_in = src_buffer->audio->nb_samples;
+    int ret = 0;
+    int n_in = src_buffer->nb_samples;
     int n_out = (int)(0.5 + ((double)n_in) / atempo->tempo);
 
     const uint8_t *src = src_buffer->data[0];
@@ -1056,10 +1083,10 @@
 
     while (src < src_end) {
         if (!atempo->dst_buffer) {
-            atempo->dst_buffer = ff_get_audio_buffer(outlink,
-                                                     AV_PERM_WRITE,
-                                                     n_out);
-            avfilter_copy_buffer_ref_props(atempo->dst_buffer, src_buffer);
+            atempo->dst_buffer = ff_get_audio_buffer(outlink, n_out);
+            if (!atempo->dst_buffer)
+                return AVERROR(ENOMEM);
+            av_frame_copy_props(atempo->dst_buffer, src_buffer);
 
             atempo->dst = atempo->dst_buffer->data[0];
             atempo->dst_end = atempo->dst + n_out * atempo->stride;
@@ -1068,14 +1095,18 @@
         yae_apply(atempo, &src, src_end, &atempo->dst, atempo->dst_end);
 
         if (atempo->dst == atempo->dst_end) {
-            push_samples(atempo, outlink, n_out);
-            atempo->request_fulfilled = 1;
+            int n_samples = ((atempo->dst - atempo->dst_buffer->data[0]) /
+                             atempo->stride);
+            ret = push_samples(atempo, outlink, n_samples);
+            if (ret < 0)
+                goto end;
         }
     }
 
     atempo->nsamples_in += n_in;
-    avfilter_unref_bufferp(&src_buffer);
-    return 0;
+end:
+    av_frame_free(&src_buffer);
+    return ret;
 }
 
 static int request_frame(AVFilterLink *outlink)
@@ -1084,11 +1115,7 @@
     ATempoContext *atempo = ctx->priv;
     int ret;
 
-    atempo->request_fulfilled = 0;
-    do {
-        ret = ff_request_frame(ctx->inputs[0]);
-    }
-    while (!atempo->request_fulfilled && ret >= 0);
+    ret = ff_request_frame(ctx->inputs[0]);
 
     if (ret == AVERROR_EOF) {
         // flush the filter:
@@ -1098,9 +1125,9 @@
 
         while (err == AVERROR(EAGAIN)) {
             if (!atempo->dst_buffer) {
-                atempo->dst_buffer = ff_get_audio_buffer(outlink,
-                                                         AV_PERM_WRITE,
-                                                         n_max);
+                atempo->dst_buffer = ff_get_audio_buffer(outlink, n_max);
+                if (!atempo->dst_buffer)
+                    return AVERROR(ENOMEM);
 
                 atempo->dst = atempo->dst_buffer->data[0];
                 atempo->dst_end = atempo->dst + n_max * atempo->stride;
@@ -1112,11 +1139,11 @@
                      atempo->stride);
 
             if (n_out) {
-                push_samples(atempo, outlink, n_out);
+                ret = push_samples(atempo, outlink, n_out);
             }
         }
 
-        avfilter_unref_bufferp(&atempo->dst_buffer);
+        av_frame_free(&atempo->dst_buffer);
         atempo->dst     = NULL;
         atempo->dst_end = NULL;
 
@@ -1142,7 +1169,6 @@
         .type         = AVMEDIA_TYPE_AUDIO,
         .filter_frame = filter_frame,
         .config_props = config_props,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL }
 };
@@ -1164,6 +1190,7 @@
     .query_formats   = query_formats,
     .process_command = process_command,
     .priv_size       = sizeof(ATempoContext),
+    .priv_class      = &atempo_class,
     .inputs          = atempo_inputs,
     .outputs         = atempo_outputs,
 };
diff --git a/libavfilter/af_biquads.c b/libavfilter/af_biquads.c
index cae3e02..0141ffd 100644
--- a/libavfilter/af_biquads.c
+++ b/libavfilter/af_biquads.c
@@ -116,15 +116,9 @@
                    double b0, double b1, double b2, double a1, double a2);
 } BiquadsContext;
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     BiquadsContext *p = ctx->priv;
-    int ret;
-
-    av_opt_set_defaults(p);
-
-    if ((ret = av_set_options_string(p, args, "=", ":")) < 0)
-        return ret;
 
     if (p->filter_type != biquad) {
         if (p->frequency <= 0 || p->width <= 0) {
@@ -392,24 +386,24 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
 {
     BiquadsContext *p       = inlink->dst->priv;
     AVFilterLink *outlink   = inlink->dst->outputs[0];
-    AVFilterBufferRef *out_buf;
-    int nb_samples = buf->audio->nb_samples;
+    AVFrame *out_buf;
+    int nb_samples = buf->nb_samples;
     int ch;
 
-    if (buf->perms & AV_PERM_WRITE) {
+    if (av_frame_is_writable(buf)) {
         out_buf = buf;
     } else {
-        out_buf = ff_get_audio_buffer(inlink, AV_PERM_WRITE, nb_samples);
+        out_buf = ff_get_audio_buffer(inlink, nb_samples);
         if (!out_buf)
             return AVERROR(ENOMEM);
-        out_buf->pts = buf->pts;
+        av_frame_copy_props(out_buf, buf);
     }
 
-    for (ch = 0; ch < buf->audio->channels; ch++)
+    for (ch = 0; ch < av_frame_get_channels(buf); ch++)
         p->filter(buf->extended_data[ch],
                   out_buf->extended_data[ch], nb_samples,
                   &p->cache[ch].i1, &p->cache[ch].i2,
@@ -417,7 +411,7 @@
                   p->b0, p->b1, p->b2, p->a1, p->a2);
 
     if (buf != out_buf)
-        avfilter_unref_buffer(buf);
+        av_frame_free(&buf);
 
     return ff_filter_frame(outlink, out_buf);
 }
@@ -427,7 +421,6 @@
     BiquadsContext *p = ctx->priv;
 
     av_freep(&p->cache);
-    av_opt_free(p);
 }
 
 static const AVFilterPad inputs[] = {
@@ -453,12 +446,12 @@
 
 #define DEFINE_BIQUAD_FILTER(name_, description_)                       \
 AVFILTER_DEFINE_CLASS(name_);                                           \
-static av_cold int name_##_init(AVFilterContext *ctx, const char *args) \
+static av_cold int name_##_init(AVFilterContext *ctx) \
 {                                                                       \
     BiquadsContext *p = ctx->priv;                                      \
     p->class = &name_##_class;                                          \
     p->filter_type = name_;                                             \
-    return init(ctx, args);                                             \
+    return init(ctx);                                             \
 }                                                                       \
                                                          \
 AVFilter avfilter_af_##name_ = {                         \
@@ -614,12 +607,12 @@
 #endif  /* CONFIG_ALLPASS_FILTER */
 #if CONFIG_BIQUAD_FILTER
 static const AVOption biquad_options[] = {
-    {"a0", NULL, OFFSET(a0), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MAX, INT16_MAX, FLAGS},
-    {"a1", NULL, OFFSET(a1), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MAX, INT16_MAX, FLAGS},
-    {"a2", NULL, OFFSET(a2), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MAX, INT16_MAX, FLAGS},
-    {"b0", NULL, OFFSET(b0), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MAX, INT16_MAX, FLAGS},
-    {"b1", NULL, OFFSET(b1), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MAX, INT16_MAX, FLAGS},
-    {"b2", NULL, OFFSET(b2), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MAX, INT16_MAX, FLAGS},
+    {"a0", NULL, OFFSET(a0), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
+    {"a1", NULL, OFFSET(a1), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
+    {"a2", NULL, OFFSET(a2), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
+    {"b0", NULL, OFFSET(b0), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
+    {"b1", NULL, OFFSET(b1), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
+    {"b2", NULL, OFFSET(b2), AV_OPT_TYPE_DOUBLE, {.dbl=1}, INT16_MIN, INT16_MAX, FLAGS},
     {NULL},
 };
 
diff --git a/libavfilter/af_channelmap.c b/libavfilter/af_channelmap.c
index 6fe8704..c6ba347 100644
--- a/libavfilter/af_channelmap.c
+++ b/libavfilter/af_channelmap.c
@@ -120,28 +120,17 @@
     return 0;
 }
 
-static av_cold int channelmap_init(AVFilterContext *ctx, const char *args)
+static av_cold int channelmap_init(AVFilterContext *ctx)
 {
     ChannelMapContext *s = ctx->priv;
-    int ret;
-    char *mapping;
+    int ret = 0;
+    char *mapping, separator = '|';
     int map_entries = 0;
     char buf[256];
     enum MappingMode mode;
     uint64_t out_ch_mask = 0;
     int i;
 
-    if (!args) {
-        av_log(ctx, AV_LOG_ERROR, "No parameters supplied.\n");
-        return AVERROR(EINVAL);
-    }
-
-    s->class = &channelmap_class;
-    av_opt_set_defaults(s);
-
-    if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
-        return ret;
-
     mapping = s->mapping_str;
 
     if (!mapping) {
@@ -149,28 +138,35 @@
     } else {
         char *dash = strchr(mapping, '-');
         if (!dash) {  // short mapping
-            if (isdigit(*mapping))
+            if (av_isdigit(*mapping))
                 mode = MAP_ONE_INT;
             else
                 mode = MAP_ONE_STR;
-        } else if (isdigit(*mapping)) {
-            if (isdigit(*(dash+1)))
+        } else if (av_isdigit(*mapping)) {
+            if (av_isdigit(*(dash+1)))
                 mode = MAP_PAIR_INT_INT;
             else
                 mode = MAP_PAIR_INT_STR;
         } else {
-            if (isdigit(*(dash+1)))
+            if (av_isdigit(*(dash+1)))
                 mode = MAP_PAIR_STR_INT;
             else
                 mode = MAP_PAIR_STR_STR;
         }
+#if FF_API_OLD_FILTER_OPTS
+        if (strchr(mapping, ',')) {
+            av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use "
+                   "'|' to separate the mappings.\n");
+            separator = ',';
+        }
+#endif
     }
 
     if (mode != MAP_NONE) {
-        char *comma = mapping;
+        char *sep = mapping;
         map_entries = 1;
-        while ((comma = strchr(comma, ','))) {
-            if (*++comma)  // Allow trailing comma
+        while ((sep = strchr(sep, separator))) {
+            if (*++sep)  // Allow trailing comma
                 map_entries++;
         }
     }
@@ -187,7 +183,7 @@
         static const char err[] = "Failed to parse channel map\n";
         switch (mode) {
         case MAP_ONE_INT:
-            if (get_channel_idx(&mapping, &in_ch_idx, ',', MAX_CH) < 0) {
+            if (get_channel_idx(&mapping, &in_ch_idx, separator, MAX_CH) < 0) {
                 ret = AVERROR(EINVAL);
                 av_log(ctx, AV_LOG_ERROR, err);
                 goto fail;
@@ -196,7 +192,7 @@
             s->map[i].out_channel_idx = i;
             break;
         case MAP_ONE_STR:
-            if (!get_channel(&mapping, &in_ch, ',')) {
+            if (!get_channel(&mapping, &in_ch, separator)) {
                 av_log(ctx, AV_LOG_ERROR, err);
                 ret = AVERROR(EINVAL);
                 goto fail;
@@ -206,7 +202,7 @@
             break;
         case MAP_PAIR_INT_INT:
             if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
-                get_channel_idx(&mapping, &out_ch_idx, ',', MAX_CH) < 0) {
+                get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
                 av_log(ctx, AV_LOG_ERROR, err);
                 ret = AVERROR(EINVAL);
                 goto fail;
@@ -216,7 +212,7 @@
             break;
         case MAP_PAIR_INT_STR:
             if (get_channel_idx(&mapping, &in_ch_idx, '-', MAX_CH) < 0 ||
-                get_channel(&mapping, &out_ch, ',') < 0 ||
+                get_channel(&mapping, &out_ch, separator) < 0 ||
                 out_ch & out_ch_mask) {
                 av_log(ctx, AV_LOG_ERROR, err);
                 ret = AVERROR(EINVAL);
@@ -228,7 +224,7 @@
             break;
         case MAP_PAIR_STR_INT:
             if (get_channel(&mapping, &in_ch, '-') < 0 ||
-                get_channel_idx(&mapping, &out_ch_idx, ',', MAX_CH) < 0) {
+                get_channel_idx(&mapping, &out_ch_idx, separator, MAX_CH) < 0) {
                 av_log(ctx, AV_LOG_ERROR, err);
                 ret = AVERROR(EINVAL);
                 goto fail;
@@ -238,7 +234,7 @@
             break;
         case MAP_PAIR_STR_STR:
             if (get_channel(&mapping, &in_ch, '-') < 0 ||
-                get_channel(&mapping, &out_ch, ',') < 0 ||
+                get_channel(&mapping, &out_ch, separator) < 0 ||
                 out_ch & out_ch_mask) {
                 av_log(ctx, AV_LOG_ERROR, err);
                 ret = AVERROR(EINVAL);
@@ -312,7 +308,7 @@
     return 0;
 }
 
-static int channelmap_filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
+static int channelmap_filter_frame(AVFilterLink *inlink, AVFrame *buf)
 {
     AVFilterContext  *ctx = inlink->dst;
     AVFilterLink *outlink = ctx->outputs[0];
@@ -330,7 +326,7 @@
             uint8_t **new_extended_data =
                 av_mallocz(nch_out * sizeof(*buf->extended_data));
             if (!new_extended_data) {
-                avfilter_unref_buffer(buf);
+                av_frame_free(&buf);
                 return AVERROR(ENOMEM);
             }
             if (buf->extended_data == buf->data) {
@@ -361,23 +357,33 @@
 {
     AVFilterContext *ctx = inlink->dst;
     ChannelMapContext *s = ctx->priv;
+    int nb_channels = av_get_channel_layout_nb_channels(inlink->channel_layout);
     int i, err = 0;
     const char *channel_name;
     char layout_name[256];
 
-    if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) {
-        for (i = 0; i < s->nch; i++) {
-            s->map[i].in_channel_idx = av_get_channel_layout_channel_index(
-                inlink->channel_layout, s->map[i].in_channel);
-            if (s->map[i].in_channel_idx < 0) {
-                channel_name = av_get_channel_name(s->map[i].in_channel);
-                av_get_channel_layout_string(layout_name, sizeof(layout_name),
-                                             0, inlink->channel_layout);
+    for (i = 0; i < s->nch; i++) {
+        struct ChannelMap *m = &s->map[i];
+
+        if (s->mode == MAP_PAIR_STR_INT || s->mode == MAP_PAIR_STR_STR) {
+            m->in_channel_idx = av_get_channel_layout_channel_index(
+                inlink->channel_layout, m->in_channel);
+        }
+
+        if (m->in_channel_idx < 0 || m->in_channel_idx >= nb_channels) {
+            av_get_channel_layout_string(layout_name, sizeof(layout_name),
+                                         0, inlink->channel_layout);
+            if (m->in_channel) {
+                channel_name = av_get_channel_name(m->in_channel);
                 av_log(ctx, AV_LOG_ERROR,
                        "input channel '%s' not available from input layout '%s'\n",
                        channel_name, layout_name);
-                err = AVERROR(EINVAL);
+            } else {
+                av_log(ctx, AV_LOG_ERROR,
+                       "input channel #%d not available from input layout '%s'\n",
+                       m->in_channel_idx, layout_name);
             }
+            err = AVERROR(EINVAL);
         }
     }
 
@@ -388,9 +394,9 @@
     {
         .name           = "default",
         .type           = AVMEDIA_TYPE_AUDIO,
-        .min_perms      = AV_PERM_READ | AV_PERM_WRITE,
         .filter_frame   = channelmap_filter_frame,
-        .config_props   = channelmap_config_input
+        .config_props   = channelmap_config_input,
+        .needs_writable = 1,
     },
     { NULL }
 };
@@ -409,8 +415,8 @@
     .init          = channelmap_init,
     .query_formats = channelmap_query_formats,
     .priv_size     = sizeof(ChannelMapContext),
+    .priv_class    = &channelmap_class,
 
     .inputs        = avfilter_af_channelmap_inputs,
     .outputs       = avfilter_af_channelmap_outputs,
-    .priv_class    = &channelmap_class,
 };
diff --git a/libavfilter/af_channelsplit.c b/libavfilter/af_channelsplit.c
index 9ca9dad..c14c676 100644
--- a/libavfilter/af_channelsplit.c
+++ b/libavfilter/af_channelsplit.c
@@ -49,16 +49,12 @@
 
 AVFILTER_DEFINE_CLASS(channelsplit);
 
-static int init(AVFilterContext *ctx, const char *arg)
+static int init(AVFilterContext *ctx)
 {
     ChannelSplitContext *s = ctx->priv;
     int nb_channels;
     int ret = 0, i;
 
-    s->class = &channelsplit_class;
-    av_opt_set_defaults(s);
-    if ((ret = av_set_options_string(s, arg, "=", ":")) < 0)
-        return ret;
     if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
         av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
                s->channel_layout_str);
@@ -78,7 +74,6 @@
     }
 
 fail:
-    av_opt_free(s);
     return ret;
 }
 
@@ -105,13 +100,13 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
 {
     AVFilterContext *ctx = inlink->dst;
     int i, ret = 0;
 
     for (i = 0; i < ctx->nb_outputs; i++) {
-        AVFilterBufferRef *buf_out = avfilter_ref_buffer(buf, ~AV_PERM_WRITE);
+        AVFrame *buf_out = av_frame_clone(buf);
 
         if (!buf_out) {
             ret = AVERROR(ENOMEM);
@@ -119,14 +114,15 @@
         }
 
         buf_out->data[0] = buf_out->extended_data[0] = buf_out->extended_data[i];
-        buf_out->audio->channel_layout =
-            av_channel_layout_extract_channel(buf->audio->channel_layout, i);
+        buf_out->channel_layout =
+            av_channel_layout_extract_channel(buf->channel_layout, i);
+        av_frame_set_channels(buf_out, 1);
 
         ret = ff_filter_frame(ctx->outputs[i], buf_out);
         if (ret < 0)
             break;
     }
-    avfilter_unref_buffer(buf);
+    av_frame_free(&buf);
     return ret;
 }
 
@@ -141,13 +137,15 @@
 
 AVFilter avfilter_af_channelsplit = {
     .name           = "channelsplit",
-    .description    = NULL_IF_CONFIG_SMALL("Split audio into per-channel streams"),
+    .description    = NULL_IF_CONFIG_SMALL("Split audio into per-channel streams."),
     .priv_size      = sizeof(ChannelSplitContext),
+    .priv_class     = &channelsplit_class,
 
     .init           = init,
     .query_formats  = query_formats,
 
     .inputs  = avfilter_af_channelsplit_inputs,
     .outputs = NULL,
-    .priv_class = &channelsplit_class,
+
+    .flags   = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
 };
diff --git a/libavfilter/af_earwax.c b/libavfilter/af_earwax.c
index a169d2a..189243a 100644
--- a/libavfilter/af_earwax.c
+++ b/libavfilter/af_earwax.c
@@ -98,10 +98,10 @@
     int16_t j;
 
     while (in < endin) {
-        sample = 32;
+        sample = 0;
         for (j = 0; j < NUMTAPS; j++)
             sample += in[j] * filt[j];
-        *out = sample >> 6;
+        *out = av_clip_int16(sample >> 6);
         out++;
         in++;
     }
@@ -109,18 +109,17 @@
     return out;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamples)
+static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
 {
     AVFilterLink *outlink = inlink->dst->outputs[0];
     int16_t *taps, *endin, *in, *out;
-    AVFilterBufferRef *outsamples =
-        ff_get_audio_buffer(inlink, AV_PERM_WRITE,
-                                  insamples->audio->nb_samples);
-    int ret;
+    AVFrame *outsamples = ff_get_audio_buffer(inlink, insamples->nb_samples);
 
-    if (!outsamples)
+    if (!outsamples) {
+        av_frame_free(&insamples);
         return AVERROR(ENOMEM);
-    avfilter_copy_buffer_ref_props(outsamples, insamples);
+    }
+    av_frame_copy_props(outsamples, insamples);
 
     taps  = ((EarwaxContext *)inlink->dst->priv)->taps;
     out   = (int16_t *)outsamples->data[0];
@@ -131,15 +130,14 @@
     out   = scalarproduct(taps, taps + NUMTAPS, out);
 
     // process current input
-    endin = in + insamples->audio->nb_samples * 2 - NUMTAPS;
+    endin = in + insamples->nb_samples * 2 - NUMTAPS;
     scalarproduct(in, endin, out);
 
     // save part of input for next round
     memcpy(taps, endin, NUMTAPS * sizeof(*taps));
 
-    ret = ff_filter_frame(outlink, outsamples);
-    avfilter_unref_buffer(insamples);
-    return ret;
+    av_frame_free(&insamples);
+    return ff_filter_frame(outlink, outsamples);
 }
 
 static const AVFilterPad earwax_inputs[] = {
@@ -147,7 +145,6 @@
         .name         = "default",
         .type         = AVMEDIA_TYPE_AUDIO,
         .filter_frame = filter_frame,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL }
 };
diff --git a/libavfilter/af_join.c b/libavfilter/af_join.c
index 864663b..cea448b 100644
--- a/libavfilter/af_join.c
+++ b/libavfilter/af_join.c
@@ -56,24 +56,14 @@
     /**
      * Temporary storage for input frames, until we get one on each input.
      */
-    AVFilterBufferRef **input_frames;
+    AVFrame **input_frames;
 
     /**
-     *  Temporary storage for data pointers, for assembling the output buffer.
+     *  Temporary storage for buffer references, for assembling the output frame.
      */
-    uint8_t **data;
+    AVBufferRef **buffers;
 } JoinContext;
 
-/**
- * To avoid copying the data from input buffers, this filter creates
- * a custom output buffer that stores references to all inputs and
- * unrefs them on free.
- */
-typedef struct JoinBufferPriv {
-    AVFilterBufferRef **in_buffers;
-    int              nb_in_buffers;
-} JoinBufferPriv;
-
 #define OFFSET(x) offsetof(JoinContext, x)
 #define A AV_OPT_FLAG_AUDIO_PARAM
 #define F AV_OPT_FLAG_FILTERING_PARAM
@@ -94,7 +84,7 @@
     .version    = LIBAVUTIL_VERSION_INT,
 };
 
-static int filter_frame(AVFilterLink *link, AVFilterBufferRef *buf)
+static int filter_frame(AVFilterLink *link, AVFrame *frame)
 {
     AVFilterContext *ctx = link->dst;
     JoinContext       *s = ctx->priv;
@@ -105,7 +95,7 @@
             break;
     av_assert0(i < ctx->nb_inputs);
     av_assert0(!s->input_frames[i]);
-    s->input_frames[i] = buf;
+    s->input_frames[i] = frame;
 
     return 0;
 }
@@ -113,14 +103,23 @@
 static int parse_maps(AVFilterContext *ctx)
 {
     JoinContext *s = ctx->priv;
+    char separator = '|';
     char *cur      = s->map;
 
+#if FF_API_OLD_FILTER_OPTS
+    if (cur && strchr(cur, ',')) {
+        av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use '|' to "
+               "separate the mappings.\n");
+        separator = ',';
+    }
+#endif
+
     while (cur && *cur) {
         char *sep, *next, *p;
         uint64_t in_channel = 0, out_channel = 0;
         int input_idx, out_ch_idx, in_ch_idx;
 
-        next = strchr(cur, ',');
+        next = strchr(cur, separator);
         if (next)
             *next++ = 0;
 
@@ -188,16 +187,11 @@
     return 0;
 }
 
-static int join_init(AVFilterContext *ctx, const char *args)
+static int join_init(AVFilterContext *ctx)
 {
     JoinContext *s = ctx->priv;
     int ret, i;
 
-    s->class = &join_class;
-    av_opt_set_defaults(s);
-    if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
-        return ret;
-
     if (!(s->channel_layout = av_get_channel_layout(s->channel_layout_str))) {
         av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
                s->channel_layout_str);
@@ -207,9 +201,9 @@
 
     s->nb_channels  = av_get_channel_layout_nb_channels(s->channel_layout);
     s->channels     = av_mallocz(sizeof(*s->channels) * s->nb_channels);
-    s->data         = av_mallocz(sizeof(*s->data)     * s->nb_channels);
+    s->buffers      = av_mallocz(sizeof(*s->buffers)  * s->nb_channels);
     s->input_frames = av_mallocz(sizeof(*s->input_frames) * s->inputs);
-    if (!s->channels || !s->data || !s->input_frames) {
+    if (!s->channels || !s->buffers|| !s->input_frames) {
         ret = AVERROR(ENOMEM);
         goto fail;
     }
@@ -248,11 +242,11 @@
 
     for (i = 0; i < ctx->nb_inputs; i++) {
         av_freep(&ctx->input_pads[i].name);
-        avfilter_unref_bufferp(&s->input_frames[i]);
+        av_frame_free(&s->input_frames[i]);
     }
 
     av_freep(&s->channels);
-    av_freep(&s->data);
+    av_freep(&s->buffers);
     av_freep(&s->input_frames);
 }
 
@@ -394,34 +388,14 @@
     return ret;
 }
 
-static void join_free_buffer(AVFilterBuffer *buf)
-{
-    JoinBufferPriv *priv = buf->priv;
-
-    if (priv) {
-        int i;
-
-        for (i = 0; i < priv->nb_in_buffers; i++)
-            avfilter_unref_bufferp(&priv->in_buffers[i]);
-
-        av_freep(&priv->in_buffers);
-        av_freep(&buf->priv);
-    }
-
-    if (buf->extended_data != buf->data)
-        av_freep(&buf->extended_data);
-    av_freep(&buf);
-}
-
 static int join_request_frame(AVFilterLink *outlink)
 {
     AVFilterContext *ctx = outlink->src;
     JoinContext *s       = ctx->priv;
-    AVFilterBufferRef *buf;
-    JoinBufferPriv *priv;
+    AVFrame *frame;
     int linesize   = INT_MAX;
-    int perms      = ~0;
     int nb_samples = 0;
+    int nb_buffers = 0;
     int i, j, ret;
 
     /* get a frame on each input */
@@ -434,54 +408,98 @@
 
         /* request the same number of samples on all inputs */
         if (i == 0) {
-            nb_samples = s->input_frames[0]->audio->nb_samples;
+            nb_samples = s->input_frames[0]->nb_samples;
 
             for (j = 1; !i && j < ctx->nb_inputs; j++)
                 ctx->inputs[j]->request_samples = nb_samples;
         }
     }
 
-    for (i = 0; i < s->nb_channels; i++) {
-        ChannelMap *ch = &s->channels[i];
-        AVFilterBufferRef *cur_buf = s->input_frames[ch->input];
-
-        s->data[i] = cur_buf->extended_data[ch->in_channel_idx];
-        linesize   = FFMIN(linesize, cur_buf->linesize[0]);
-        perms     &= cur_buf->perms;
+    /* setup the output frame */
+    frame = av_frame_alloc();
+    if (!frame)
+        return AVERROR(ENOMEM);
+    if (s->nb_channels > FF_ARRAY_ELEMS(frame->data)) {
+        frame->extended_data = av_mallocz(s->nb_channels *
+                                          sizeof(*frame->extended_data));
+        if (!frame->extended_data) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
     }
 
-    av_assert0(nb_samples > 0);
-    buf = avfilter_get_audio_buffer_ref_from_arrays(s->data, linesize, perms,
-                                                    nb_samples, outlink->format,
-                                                    outlink->channel_layout);
-    if (!buf)
-        return AVERROR(ENOMEM);
+    /* copy the data pointers */
+    for (i = 0; i < s->nb_channels; i++) {
+        ChannelMap *ch = &s->channels[i];
+        AVFrame *cur   = s->input_frames[ch->input];
+        AVBufferRef *buf;
 
-    buf->buf->free = join_free_buffer;
-    buf->pts       = s->input_frames[0]->pts;
+        frame->extended_data[i] = cur->extended_data[ch->in_channel_idx];
+        linesize = FFMIN(linesize, cur->linesize[0]);
 
-    if (!(priv = av_mallocz(sizeof(*priv))))
-        goto fail;
-    if (!(priv->in_buffers = av_mallocz(sizeof(*priv->in_buffers) * ctx->nb_inputs)))
-        goto fail;
+        /* add the buffer where this plan is stored to the list if it's
+         * not already there */
+        buf = av_frame_get_plane_buffer(cur, ch->in_channel_idx);
+        if (!buf) {
+            ret = AVERROR(EINVAL);
+            goto fail;
+        }
+        for (j = 0; j < nb_buffers; j++)
+            if (s->buffers[j]->buffer == buf->buffer)
+                break;
+        if (j == i)
+            s->buffers[nb_buffers++] = buf;
+    }
+
+    /* create references to the buffers we copied to output */
+    if (nb_buffers > FF_ARRAY_ELEMS(frame->buf)) {
+        frame->nb_extended_buf = nb_buffers - FF_ARRAY_ELEMS(frame->buf);
+        frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
+                                         frame->nb_extended_buf);
+        if (!frame->extended_buf) {
+            frame->nb_extended_buf = 0;
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
+    }
+    for (i = 0; i < FFMIN(FF_ARRAY_ELEMS(frame->buf), nb_buffers); i++) {
+        frame->buf[i] = av_buffer_ref(s->buffers[i]);
+        if (!frame->buf[i]) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
+    }
+    for (i = 0; i < frame->nb_extended_buf; i++) {
+        frame->extended_buf[i] = av_buffer_ref(s->buffers[i +
+                                               FF_ARRAY_ELEMS(frame->buf)]);
+        if (!frame->extended_buf[i]) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
+    }
+
+    frame->nb_samples     = nb_samples;
+    frame->channel_layout = outlink->channel_layout;
+    av_frame_set_channels(frame, outlink->channels);
+    frame->format         = outlink->format;
+    frame->sample_rate    = outlink->sample_rate;
+    frame->pts            = s->input_frames[0]->pts;
+    frame->linesize[0]    = linesize;
+    if (frame->data != frame->extended_data) {
+        memcpy(frame->data, frame->extended_data, sizeof(*frame->data) *
+               FFMIN(FF_ARRAY_ELEMS(frame->data), s->nb_channels));
+    }
+
+    ret = ff_filter_frame(outlink, frame);
 
     for (i = 0; i < ctx->nb_inputs; i++)
-        priv->in_buffers[i] = s->input_frames[i];
-    priv->nb_in_buffers = ctx->nb_inputs;
-    buf->buf->priv      = priv;
-
-    ret = ff_filter_frame(outlink, buf);
-
-    memset(s->input_frames, 0, sizeof(*s->input_frames) * ctx->nb_inputs);
+        av_frame_free(&s->input_frames[i]);
 
     return ret;
 
 fail:
-    avfilter_unref_buffer(buf);
-    if (priv)
-        av_freep(&priv->in_buffers);
-    av_freep(&priv);
-    return AVERROR(ENOMEM);
+    av_frame_free(&frame);
+    return ret;
 }
 
 static const AVFilterPad avfilter_af_join_outputs[] = {
@@ -497,8 +515,9 @@
 AVFilter avfilter_af_join = {
     .name           = "join",
     .description    = NULL_IF_CONFIG_SMALL("Join multiple audio streams into "
-                                           "multi-channel output"),
+                                           "multi-channel output."),
     .priv_size      = sizeof(JoinContext),
+    .priv_class     = &join_class,
 
     .init           = join_init,
     .uninit         = join_uninit,
@@ -506,5 +525,6 @@
 
     .inputs  = NULL,
     .outputs = avfilter_af_join_outputs,
-    .priv_class = &join_class,
+
+    .flags   = AVFILTER_FLAG_DYNAMIC_INPUTS,
 };
diff --git a/libavfilter/af_pan.c b/libavfilter/af_pan.c
index 77ca549..800c40a 100644
--- a/libavfilter/af_pan.c
+++ b/libavfilter/af_pan.c
@@ -40,6 +40,8 @@
 #define MAX_CHANNELS 63
 
 typedef struct PanContext {
+    const AVClass *class;
+    char *args;
     int64_t out_channel_layout;
     double gain[MAX_CHANNELS][MAX_CHANNELS];
     int64_t need_renorm;
@@ -53,12 +55,21 @@
     struct SwrContext *swr;
 } PanContext;
 
+static void skip_spaces(char **arg)
+{
+    int len = 0;
+
+    sscanf(*arg, " %n", &len);
+    *arg += len;
+}
+
 static int parse_channel_name(char **arg, int *rchannel, int *rnamed)
 {
     char buf[8];
     int len, i, channel_id = 0;
     int64_t layout, layout0;
 
+    skip_spaces(arg);
     /* try to parse a channel name, e.g. "FL" */
     if (sscanf(*arg, "%7[A-Z]%n", buf, &len)) {
         layout0 = layout = av_get_channel_layout(buf);
@@ -88,23 +99,15 @@
     return AVERROR(EINVAL);
 }
 
-static void skip_spaces(char **arg)
-{
-    int len = 0;
-
-    sscanf(*arg, " %n", &len);
-    *arg += len;
-}
-
-static av_cold int init(AVFilterContext *ctx, const char *args0)
+static av_cold int init(AVFilterContext *ctx)
 {
     PanContext *const pan = ctx->priv;
-    char *arg, *arg0, *tokenizer, *args = av_strdup(args0);
+    char *arg, *arg0, *tokenizer, *args = av_strdup(pan->args);
     int out_ch_id, in_ch_id, len, named, ret;
     int nb_in_channels[2] = { 0, 0 }; // number of unnamed and named input channels
     double gain;
 
-    if (!args0) {
+    if (!pan->args) {
         av_log(ctx, AV_LOG_ERROR,
                "pan filter needs a channel layout and a set "
                "of channels definitions as parameter\n");
@@ -112,14 +115,14 @@
     }
     if (!args)
         return AVERROR(ENOMEM);
-    arg = av_strtok(args, ":", &tokenizer);
+    arg = av_strtok(args, "|", &tokenizer);
     ret = ff_parse_channel_layout(&pan->out_channel_layout, arg, ctx);
     if (ret < 0)
         goto fail;
     pan->nb_output_channels = av_get_channel_layout_nb_channels(pan->out_channel_layout);
 
     /* parse channel specifications */
-    while ((arg = arg0 = av_strtok(NULL, ":", &tokenizer))) {
+    while ((arg = arg0 = av_strtok(NULL, "|", &tokenizer))) {
         /* channel name */
         if (parse_channel_name(&arg, &out_ch_id, &named)) {
             av_log(ctx, AV_LOG_ERROR,
@@ -353,21 +356,23 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamples)
+static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
 {
     int ret;
-    int n = insamples->audio->nb_samples;
+    int n = insamples->nb_samples;
     AVFilterLink *const outlink = inlink->dst->outputs[0];
-    AVFilterBufferRef *outsamples = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n);
+    AVFrame *outsamples = ff_get_audio_buffer(outlink, n);
     PanContext *pan = inlink->dst->priv;
 
+    if (!outsamples)
+        return AVERROR(ENOMEM);
     swr_convert(pan->swr, outsamples->data, n, (void *)insamples->data, n);
-    avfilter_copy_buffer_ref_props(outsamples, insamples);
-    outsamples->audio->channel_layout = outlink->channel_layout;
-    outsamples->audio->channels       = outlink->channels;
+    av_frame_copy_props(outsamples, insamples);
+    outsamples->channel_layout = outlink->channel_layout;
+    av_frame_set_channels(outsamples, outlink->channels);
 
     ret = ff_filter_frame(outlink, outsamples);
-    avfilter_unref_buffer(insamples);
+    av_frame_free(&insamples);
     return ret;
 }
 
@@ -377,13 +382,22 @@
     swr_free(&pan->swr);
 }
 
+#define OFFSET(x) offsetof(PanContext, x)
+
+static const AVOption pan_options[] = {
+    { "args", NULL, OFFSET(args), AV_OPT_TYPE_STRING, { .str = NULL }, CHAR_MIN, CHAR_MAX, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(pan);
+
+
 static const AVFilterPad pan_inputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_AUDIO,
         .config_props = config_props,
         .filter_frame = filter_frame,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL }
 };
@@ -400,6 +414,7 @@
     .name          = "pan",
     .description   = NULL_IF_CONFIG_SMALL("Remix channels with coefficients (panning)."),
     .priv_size     = sizeof(PanContext),
+    .priv_class    = &pan_class,
     .init          = init,
     .uninit        = uninit,
     .query_formats = query_formats,
diff --git a/libavfilter/af_resample.c b/libavfilter/af_resample.c
index 84ca8f5..7e86684 100644
--- a/libavfilter/af_resample.c
+++ b/libavfilter/af_resample.c
@@ -37,6 +37,7 @@
 #include "internal.h"
 
 typedef struct ResampleContext {
+    const AVClass *class;
     AVAudioResampleContext *avr;
     AVDictionary *options;
 
@@ -46,26 +47,30 @@
     int got_output;
 } ResampleContext;
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx, AVDictionary **opts)
 {
     ResampleContext *s = ctx->priv;
+    const AVClass *avr_class = avresample_get_class();
+    AVDictionaryEntry *e = NULL;
 
-    if (args) {
-        int ret = av_dict_parse_string(&s->options, args, "=", ":", 0);
-        if (ret < 0) {
-            av_log(ctx, AV_LOG_ERROR, "error setting option string: %s\n", args);
-            return ret;
-        }
-
-        /* do not allow the user to override basic format options */
-        av_dict_set(&s->options,  "in_channel_layout", NULL, 0);
-        av_dict_set(&s->options, "out_channel_layout", NULL, 0);
-        av_dict_set(&s->options,  "in_sample_fmt",     NULL, 0);
-        av_dict_set(&s->options, "out_sample_fmt",     NULL, 0);
-        av_dict_set(&s->options,  "in_sample_rate",    NULL, 0);
-        av_dict_set(&s->options, "out_sample_rate",    NULL, 0);
+    while ((e = av_dict_get(*opts, "", e, AV_DICT_IGNORE_SUFFIX))) {
+        if (av_opt_find(&avr_class, e->key, NULL, 0,
+                        AV_OPT_SEARCH_FAKE_OBJ | AV_OPT_SEARCH_CHILDREN))
+            av_dict_set(&s->options, e->key, e->value, 0);
     }
 
+    e = NULL;
+    while ((e = av_dict_get(s->options, "", e, AV_DICT_IGNORE_SUFFIX)))
+        av_dict_set(opts, e->key, NULL, 0);
+
+    /* do not allow the user to override basic format options */
+    av_dict_set(&s->options,  "in_channel_layout", NULL, 0);
+    av_dict_set(&s->options, "out_channel_layout", NULL, 0);
+    av_dict_set(&s->options,  "in_sample_fmt",     NULL, 0);
+    av_dict_set(&s->options, "out_sample_fmt",     NULL, 0);
+    av_dict_set(&s->options,  "in_sample_rate",    NULL, 0);
+    av_dict_set(&s->options, "out_sample_rate",    NULL, 0);
+
     return 0;
 }
 
@@ -174,7 +179,7 @@
 
     /* flush the lavr delay buffer */
     if (ret == AVERROR_EOF && s->avr) {
-        AVFilterBufferRef *buf;
+        AVFrame *frame;
         int nb_samples = av_rescale_rnd(avresample_get_delay(s->avr),
                                         outlink->sample_rate,
                                         ctx->inputs[0]->sample_rate,
@@ -183,25 +188,25 @@
         if (!nb_samples)
             return ret;
 
-        buf = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
-        if (!buf)
+        frame = ff_get_audio_buffer(outlink, nb_samples);
+        if (!frame)
             return AVERROR(ENOMEM);
 
-        ret = avresample_convert(s->avr, buf->extended_data,
-                                 buf->linesize[0], nb_samples,
+        ret = avresample_convert(s->avr, frame->extended_data,
+                                 frame->linesize[0], nb_samples,
                                  NULL, 0, 0);
         if (ret <= 0) {
-            avfilter_unref_buffer(buf);
+            av_frame_free(&frame);
             return (ret == 0) ? AVERROR_EOF : ret;
         }
 
-        buf->pts = s->next_pts;
-        return ff_filter_frame(outlink, buf);
+        frame->pts = s->next_pts;
+        return ff_filter_frame(outlink, frame);
     }
     return ret;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     AVFilterContext  *ctx = inlink->dst;
     ResampleContext    *s = ctx->priv;
@@ -209,27 +214,26 @@
     int ret;
 
     if (s->avr) {
-        AVFilterBufferRef *buf_out;
+        AVFrame *out;
         int delay, nb_samples;
 
         /* maximum possible samples lavr can output */
         delay      = avresample_get_delay(s->avr);
-        nb_samples = av_rescale_rnd(buf->audio->nb_samples + delay,
+        nb_samples = av_rescale_rnd(in->nb_samples + delay,
                                     outlink->sample_rate, inlink->sample_rate,
                                     AV_ROUND_UP);
 
-        buf_out = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
-        if (!buf_out) {
+        out = ff_get_audio_buffer(outlink, nb_samples);
+        if (!out) {
             ret = AVERROR(ENOMEM);
             goto fail;
         }
 
-        ret     = avresample_convert(s->avr, buf_out->extended_data,
-                                     buf_out->linesize[0], nb_samples,
-                                     buf->extended_data, buf->linesize[0],
-                                     buf->audio->nb_samples);
+        ret = avresample_convert(s->avr, out->extended_data, out->linesize[0],
+                                 nb_samples, in->extended_data, in->linesize[0],
+                                 in->nb_samples);
         if (ret <= 0) {
-            avfilter_unref_buffer(buf_out);
+            av_frame_free(&out);
             if (ret < 0)
                 goto fail;
         }
@@ -237,48 +241,66 @@
         av_assert0(!avresample_available(s->avr));
 
         if (s->next_pts == AV_NOPTS_VALUE) {
-            if (buf->pts == AV_NOPTS_VALUE) {
+            if (in->pts == AV_NOPTS_VALUE) {
                 av_log(ctx, AV_LOG_WARNING, "First timestamp is missing, "
                        "assuming 0.\n");
                 s->next_pts = 0;
             } else
-                s->next_pts = av_rescale_q(buf->pts, inlink->time_base,
+                s->next_pts = av_rescale_q(in->pts, inlink->time_base,
                                            outlink->time_base);
         }
 
         if (ret > 0) {
-            buf_out->audio->nb_samples = ret;
-            if (buf->pts != AV_NOPTS_VALUE) {
-                buf_out->pts = av_rescale_q(buf->pts, inlink->time_base,
+            out->nb_samples = ret;
+            if (in->pts != AV_NOPTS_VALUE) {
+                out->pts = av_rescale_q(in->pts, inlink->time_base,
                                             outlink->time_base) -
                                av_rescale(delay, outlink->sample_rate,
                                           inlink->sample_rate);
             } else
-                buf_out->pts = s->next_pts;
+                out->pts = s->next_pts;
 
-            s->next_pts = buf_out->pts + buf_out->audio->nb_samples;
+            s->next_pts = out->pts + out->nb_samples;
 
-            ret = ff_filter_frame(outlink, buf_out);
+            ret = ff_filter_frame(outlink, out);
             s->got_output = 1;
         }
 
 fail:
-        avfilter_unref_buffer(buf);
+        av_frame_free(&in);
     } else {
-        buf->format = outlink->format;
-        ret = ff_filter_frame(outlink, buf);
+        in->format = outlink->format;
+        ret = ff_filter_frame(outlink, in);
         s->got_output = 1;
     }
 
     return ret;
 }
 
+static const AVClass *resample_child_class_next(const AVClass *prev)
+{
+    return prev ? NULL : avresample_get_class();
+}
+
+static void *resample_child_next(void *obj, void *prev)
+{
+    ResampleContext *s = obj;
+    return prev ? NULL : s->avr;
+}
+
+static const AVClass resample_class = {
+    .class_name       = "resample",
+    .item_name        = av_default_item_name,
+    .version          = LIBAVUTIL_VERSION_INT,
+    .child_class_next = resample_child_class_next,
+    .child_next       = resample_child_next,
+};
+
 static const AVFilterPad avfilter_af_resample_inputs[] = {
     {
         .name           = "default",
         .type           = AVMEDIA_TYPE_AUDIO,
         .filter_frame   = filter_frame,
-        .min_perms      = AV_PERM_READ
     },
     { NULL }
 };
@@ -297,8 +319,9 @@
     .name          = "resample",
     .description   = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
     .priv_size     = sizeof(ResampleContext),
+    .priv_class    = &resample_class,
 
-    .init           = init,
+    .init_dict      = init,
     .uninit         = uninit,
     .query_formats  = query_formats,
 
diff --git a/libavfilter/af_silencedetect.c b/libavfilter/af_silencedetect.c
index 8a60176..eb4718b 100644
--- a/libavfilter/af_silencedetect.c
+++ b/libavfilter/af_silencedetect.c
@@ -54,36 +54,20 @@
 
 AVFILTER_DEFINE_CLASS(silencedetect);
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    int ret;
-    SilenceDetectContext *silence = ctx->priv;
-
-    silence->class = &silencedetect_class;
-    av_opt_set_defaults(silence);
-
-    if ((ret = av_set_options_string(silence, args, "=", ":")) < 0)
-        return ret;
-
-    av_opt_free(silence);
-
-    return 0;
-}
-
-static char *get_metadata_val(AVFilterBufferRef *insamples, const char *key)
+static char *get_metadata_val(AVFrame *insamples, const char *key)
 {
     AVDictionaryEntry *e = av_dict_get(insamples->metadata, key, NULL, 0);
     return e && e->value ? e->value : NULL;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamples)
+static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
 {
     int i;
     SilenceDetectContext *silence = inlink->dst->priv;
     const int nb_channels           = av_get_channel_layout_nb_channels(inlink->channel_layout);
     const int srate                 = inlink->sample_rate;
-    const int nb_samples            = insamples->audio->nb_samples * nb_channels;
-    const int64_t nb_samples_notify = srate * silence->duration    * nb_channels;
+    const int nb_samples            = insamples->nb_samples     * nb_channels;
+    const int64_t nb_samples_notify = srate * silence->duration * nb_channels;
 
     // scale number of null samples to the new sample rate
     if (silence->last_sample_rate && silence->last_sample_rate != srate)
@@ -176,7 +160,6 @@
     .name          = "silencedetect",
     .description   = NULL_IF_CONFIG_SMALL("Detect silence."),
     .priv_size     = sizeof(SilenceDetectContext),
-    .init          = init,
     .query_formats = query_formats,
     .inputs        = silencedetect_inputs,
     .outputs       = silencedetect_outputs,
diff --git a/libavfilter/af_volume.c b/libavfilter/af_volume.c
index 5ffa1fe..a55e1a3 100644
--- a/libavfilter/af_volume.c
+++ b/libavfilter/af_volume.c
@@ -24,7 +24,7 @@
  * audio volume filter
  */
 
-#include "libavutil/audioconvert.h"
+#include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
 #include "libavutil/eval.h"
 #include "libavutil/float_dsp.h"
@@ -56,17 +56,9 @@
 
 AVFILTER_DEFINE_CLASS(volume);
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     VolumeContext *vol = ctx->priv;
-    static const char *shorthand[] = { "volume", "precision", NULL };
-    int ret;
-
-    vol->class = &volume_class;
-    av_opt_set_defaults(vol);
-
-    if ((ret = av_opt_set_from_string(vol, args, shorthand, "=", ":")) < 0)
-        return ret;
 
     if (vol->precision == PRECISION_FIXED) {
         vol->volume_i = (int)(vol->volume * 256 + 0.5);
@@ -79,8 +71,7 @@
                precision_str[vol->precision]);
     }
 
-    av_opt_free(vol);
-    return ret;
+    return 0;
 }
 
 static int query_formats(AVFilterContext *ctx)
@@ -89,8 +80,7 @@
     AVFilterFormats *formats = NULL;
     AVFilterChannelLayouts *layouts;
     static const enum AVSampleFormat sample_fmts[][7] = {
-        /* PRECISION_FIXED */
-        {
+        [PRECISION_FIXED] = {
             AV_SAMPLE_FMT_U8,
             AV_SAMPLE_FMT_U8P,
             AV_SAMPLE_FMT_S16,
@@ -99,14 +89,12 @@
             AV_SAMPLE_FMT_S32P,
             AV_SAMPLE_FMT_NONE
         },
-        /* PRECISION_FLOAT */
-        {
+        [PRECISION_FLOAT] = {
             AV_SAMPLE_FMT_FLT,
             AV_SAMPLE_FMT_FLTP,
             AV_SAMPLE_FMT_NONE
         },
-        /* PRECISION_DOUBLE */
-        {
+        [PRECISION_DOUBLE] = {
             AV_SAMPLE_FMT_DBL,
             AV_SAMPLE_FMT_DBLP,
             AV_SAMPLE_FMT_NONE
@@ -226,24 +214,24 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
 {
     VolumeContext *vol    = inlink->dst->priv;
     AVFilterLink *outlink = inlink->dst->outputs[0];
-    int nb_samples        = buf->audio->nb_samples;
-    AVFilterBufferRef *out_buf;
+    int nb_samples        = buf->nb_samples;
+    AVFrame *out_buf;
 
     if (vol->volume == 1.0 || vol->volume_i == 256)
         return ff_filter_frame(outlink, buf);
 
     /* do volume scaling in-place if input buffer is writable */
-    if (buf->perms & AV_PERM_WRITE) {
+    if (av_frame_is_writable(buf)) {
         out_buf = buf;
     } else {
-        out_buf = ff_get_audio_buffer(inlink, AV_PERM_WRITE, nb_samples);
+        out_buf = ff_get_audio_buffer(inlink, nb_samples);
         if (!out_buf)
             return AVERROR(ENOMEM);
-        out_buf->pts = buf->pts;
+        av_frame_copy_props(out_buf, buf);
     }
 
     if (vol->precision != PRECISION_FIXED || vol->volume_i > 0) {
@@ -276,7 +264,7 @@
     }
 
     if (buf != out_buf)
-        avfilter_unref_buffer(buf);
+        av_frame_free(&buf);
 
     return ff_filter_frame(outlink, out_buf);
 }
@@ -304,8 +292,9 @@
     .description    = NULL_IF_CONFIG_SMALL("Change input volume."),
     .query_formats  = query_formats,
     .priv_size      = sizeof(VolumeContext),
+    .priv_class     = &volume_class,
     .init           = init,
     .inputs         = avfilter_af_volume_inputs,
     .outputs        = avfilter_af_volume_outputs,
-    .priv_class     = &volume_class,
+    .flags          = AVFILTER_FLAG_SUPPORT_TIMELINE,
 };
diff --git a/libavfilter/af_volumedetect.c b/libavfilter/af_volumedetect.c
index 39265c0..79d992e 100644
--- a/libavfilter/af_volumedetect.c
+++ b/libavfilter/af_volumedetect.c
@@ -49,12 +49,12 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *samples)
+static int filter_frame(AVFilterLink *inlink, AVFrame *samples)
 {
     AVFilterContext *ctx = inlink->dst;
     VolDetectContext *vd = ctx->priv;
-    int64_t layout  = samples->audio->channel_layout;
-    int nb_samples  = samples->audio->nb_samples;
+    int64_t layout  = samples->channel_layout;
+    int nb_samples  = samples->nb_samples;
     int nb_channels = av_get_channel_layout_nb_channels(layout);
     int nb_planes   = nb_channels;
     int plane, i;
@@ -137,7 +137,6 @@
         .type             = AVMEDIA_TYPE_AUDIO,
         .get_audio_buffer = ff_null_get_audio_buffer,
         .filter_frame     = filter_frame,
-        .min_perms        = AV_PERM_READ,
     },
     { NULL }
 };
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index f05f8ae..2fc8b48 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -21,6 +21,7 @@
 
 #include "avfilter.h"
 #include "config.h"
+#include "opencl_allkernels.h"
 
 
 #define REGISTER_FILTER(X, x, y)                                        \
@@ -44,19 +45,25 @@
         return;
     initialized = 1;
 
+#if FF_API_ACONVERT_FILTER
     REGISTER_FILTER(ACONVERT,       aconvert,       af);
+#endif
     REGISTER_FILTER(AFADE,          afade,          af);
     REGISTER_FILTER(AFORMAT,        aformat,        af);
+    REGISTER_FILTER(AINTERLEAVE,    ainterleave,    af);
     REGISTER_FILTER(ALLPASS,        allpass,        af);
     REGISTER_FILTER(AMERGE,         amerge,         af);
     REGISTER_FILTER(AMIX,           amix,           af);
     REGISTER_FILTER(ANULL,          anull,          af);
     REGISTER_FILTER(APAD,           apad,           af);
+    REGISTER_FILTER(APERMS,         aperms,         af);
+    REGISTER_FILTER(APHASER,        aphaser,        af);
     REGISTER_FILTER(ARESAMPLE,      aresample,      af);
     REGISTER_FILTER(ASELECT,        aselect,        af);
     REGISTER_FILTER(ASENDCMD,       asendcmd,       af);
     REGISTER_FILTER(ASETNSAMPLES,   asetnsamples,   af);
     REGISTER_FILTER(ASETPTS,        asetpts,        af);
+    REGISTER_FILTER(ASETRATE,       asetrate,       af);
     REGISTER_FILTER(ASETTB,         asettb,         af);
     REGISTER_FILTER(ASHOWINFO,      ashowinfo,      af);
     REGISTER_FILTER(ASPLIT,         asplit,         af);
@@ -85,6 +92,7 @@
     REGISTER_FILTER(AEVALSRC,       aevalsrc,       asrc);
     REGISTER_FILTER(ANULLSRC,       anullsrc,       asrc);
     REGISTER_FILTER(FLITE,          flite,          asrc);
+    REGISTER_FILTER(SINE,           sine,           asrc);
 
     REGISTER_FILTER(ANULLSINK,      anullsink,      asink);
 
@@ -96,10 +104,13 @@
     REGISTER_FILTER(BLACKFRAME,     blackframe,     vf);
     REGISTER_FILTER(BLEND,          blend,          vf);
     REGISTER_FILTER(BOXBLUR,        boxblur,        vf);
+    REGISTER_FILTER(COLORBALANCE,   colorbalance,   vf);
+    REGISTER_FILTER(COLORCHANNELMIXER, colorchannelmixer, vf);
     REGISTER_FILTER(COLORMATRIX,    colormatrix,    vf);
     REGISTER_FILTER(COPY,           copy,           vf);
     REGISTER_FILTER(CROP,           crop,           vf);
     REGISTER_FILTER(CROPDETECT,     cropdetect,     vf);
+    REGISTER_FILTER(CURVES,         curves,         vf);
     REGISTER_FILTER(DECIMATE,       decimate,       vf);
     REGISTER_FILTER(DELOGO,         delogo,         vf);
     REGISTER_FILTER(DESHAKE,        deshake,        vf);
@@ -108,6 +119,7 @@
     REGISTER_FILTER(EDGEDETECT,     edgedetect,     vf);
     REGISTER_FILTER(FADE,           fade,           vf);
     REGISTER_FILTER(FIELD,          field,          vf);
+    REGISTER_FILTER(FIELDMATCH,     fieldmatch,     vf);
     REGISTER_FILTER(FIELDORDER,     fieldorder,     vf);
     REGISTER_FILTER(FORMAT,         format,         vf);
     REGISTER_FILTER(FPS,            fps,            vf);
@@ -122,11 +134,14 @@
     REGISTER_FILTER(HUE,            hue,            vf);
     REGISTER_FILTER(IDET,           idet,           vf);
     REGISTER_FILTER(IL,             il,             vf);
+    REGISTER_FILTER(INTERLACE,      interlace,      vf);
+    REGISTER_FILTER(INTERLEAVE,     interleave,     vf);
     REGISTER_FILTER(KERNDEINT,      kerndeint,      vf);
     REGISTER_FILTER(LUT,            lut,            vf);
     REGISTER_FILTER(LUTRGB,         lutrgb,         vf);
     REGISTER_FILTER(LUTYUV,         lutyuv,         vf);
     REGISTER_FILTER(MP,             mp,             vf);
+    REGISTER_FILTER(MPDECIMATE,     mpdecimate,     vf);
     REGISTER_FILTER(NEGATE,         negate,         vf);
     REGISTER_FILTER(NOFORMAT,       noformat,       vf);
     REGISTER_FILTER(NOISE,          noise,          vf);
@@ -134,12 +149,14 @@
     REGISTER_FILTER(OCV,            ocv,            vf);
     REGISTER_FILTER(OVERLAY,        overlay,        vf);
     REGISTER_FILTER(PAD,            pad,            vf);
+    REGISTER_FILTER(PERMS,          perms,          vf);
     REGISTER_FILTER(PIXDESCTEST,    pixdesctest,    vf);
     REGISTER_FILTER(PP,             pp,             vf);
     REGISTER_FILTER(REMOVELOGO,     removelogo,     vf);
     REGISTER_FILTER(SCALE,          scale,          vf);
     REGISTER_FILTER(SELECT,         select,         vf);
     REGISTER_FILTER(SENDCMD,        sendcmd,        vf);
+    REGISTER_FILTER(SEPARATEFIELDS, separatefields, vf);
     REGISTER_FILTER(SETDAR,         setdar,         vf);
     REGISTER_FILTER(SETFIELD,       setfield,       vf);
     REGISTER_FILTER(SETPTS,         setpts,         vf);
@@ -148,15 +165,19 @@
     REGISTER_FILTER(SHOWINFO,       showinfo,       vf);
     REGISTER_FILTER(SMARTBLUR,      smartblur,      vf);
     REGISTER_FILTER(SPLIT,          split,          vf);
+    REGISTER_FILTER(STEREO3D,       stereo3d,       vf);
     REGISTER_FILTER(SUBTITLES,      subtitles,      vf);
     REGISTER_FILTER(SUPER2XSAI,     super2xsai,     vf);
     REGISTER_FILTER(SWAPUV,         swapuv,         vf);
+    REGISTER_FILTER(TELECINE,       telecine,       vf);
     REGISTER_FILTER(THUMBNAIL,      thumbnail,      vf);
     REGISTER_FILTER(TILE,           tile,           vf);
     REGISTER_FILTER(TINTERLACE,     tinterlace,     vf);
     REGISTER_FILTER(TRANSPOSE,      transpose,      vf);
     REGISTER_FILTER(UNSHARP,        unsharp,        vf);
     REGISTER_FILTER(VFLIP,          vflip,          vf);
+    REGISTER_FILTER(VIDSTABDETECT,  vidstabdetect,  vf);
+    REGISTER_FILTER(VIDSTABTRANSFORM, vidstabtransform, vf);
     REGISTER_FILTER(YADIF,          yadif,          vf);
 
     REGISTER_FILTER(CELLAUTO,       cellauto,       vsrc);
@@ -168,6 +189,7 @@
     REGISTER_FILTER(NULLSRC,        nullsrc,        vsrc);
     REGISTER_FILTER(RGBTESTSRC,     rgbtestsrc,     vsrc);
     REGISTER_FILTER(SMPTEBARS,      smptebars,      vsrc);
+    REGISTER_FILTER(SMPTEHDBARS,    smptehdbars,    vsrc);
     REGISTER_FILTER(TESTSRC,        testsrc,        vsrc);
 
     REGISTER_FILTER(NULLSINK,       nullsink,       vsink);
@@ -181,11 +203,9 @@
     REGISTER_FILTER(AMOVIE,         amovie,         avsrc);
     REGISTER_FILTER(MOVIE,          movie,          avsrc);
 
+#if FF_API_AVFILTERBUFFER
     REGISTER_FILTER_UNCONDITIONAL(vsink_ffbuffersink);
     REGISTER_FILTER_UNCONDITIONAL(asink_ffabuffersink);
-#if !AV_HAVE_INCOMPATIBLE_FORK_ABI
-    REGISTER_FILTER_UNCONDITIONAL(vsink_buffersink);
-    REGISTER_FILTER_UNCONDITIONAL(asink_abuffersink);
 #endif
 
     /* those filters are part of public or internal API => registered
@@ -196,4 +216,5 @@
     REGISTER_FILTER_UNCONDITIONAL(vsink_buffer);
     REGISTER_FILTER_UNCONDITIONAL(af_afifo);
     REGISTER_FILTER_UNCONDITIONAL(vf_fifo);
+    ff_opencl_register_filter_kernel_code_all();
 }
diff --git a/libavfilter/asink_anullsink.c b/libavfilter/asink_anullsink.c
index 5a324fc..8015da2 100644
--- a/libavfilter/asink_anullsink.c
+++ b/libavfilter/asink_anullsink.c
@@ -22,9 +22,9 @@
 #include "avfilter.h"
 #include "internal.h"
 
-static int null_filter_frame(AVFilterLink *link, AVFilterBufferRef *samplesref)
+static int null_filter_frame(AVFilterLink *link, AVFrame *frame)
 {
-    avfilter_unref_bufferp(&samplesref);
+    av_frame_free(&frame);
     return 0;
 }
 
diff --git a/libavfilter/asrc_aevalsrc.c b/libavfilter/asrc_aevalsrc.c
index 2e5fa98..6e338ad 100644
--- a/libavfilter/asrc_aevalsrc.c
+++ b/libavfilter/asrc_aevalsrc.c
@@ -56,7 +56,7 @@
     int nb_channels;
     int64_t pts;
     AVExpr *expr[8];
-    char *expr_str[8];
+    char *exprs;
     int nb_samples;             ///< number of samples per requested frame
     char *duration_str;         ///< total duration of the generated audio
     double duration;
@@ -68,6 +68,7 @@
 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
 
 static const AVOption aevalsrc_options[]= {
+    { "exprs",       "set the '|'-separated list of channels expressions", OFFSET(exprs), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
     { "nb_samples",  "set the number of samples per requested frame", OFFSET(nb_samples),      AV_OPT_TYPE_INT,    {.i64 = 1024},    0,        INT_MAX, FLAGS },
     { "n",           "set the number of samples per requested frame", OFFSET(nb_samples),      AV_OPT_TYPE_INT,    {.i64 = 1024},    0,        INT_MAX, FLAGS },
     { "sample_rate", "set the sample rate",                           OFFSET(sample_rate_str), AV_OPT_TYPE_STRING, {.str = "44100"}, CHAR_MIN, CHAR_MAX, FLAGS },
@@ -81,42 +82,31 @@
 
 AVFILTER_DEFINE_CLASS(aevalsrc);
 
-static int init(AVFilterContext *ctx, const char *args)
+static int init(AVFilterContext *ctx)
 {
     EvalContext *eval = ctx->priv;
-    char *args1 = av_strdup(args);
-    char *expr, *buf, *bufptr;
+    char *args1 = av_strdup(eval->exprs);
+    char *expr, *buf;
     int ret, i;
 
-    eval->class = &aevalsrc_class;
-    av_opt_set_defaults(eval);
-
     if (!args1) {
-        av_log(ctx, AV_LOG_ERROR, "Argument is empty\n");
-        ret = args ? AVERROR(ENOMEM) : AVERROR(EINVAL);
+        av_log(ctx, AV_LOG_ERROR, "Channels expressions list is empty\n");
+        ret = eval->exprs ? AVERROR(ENOMEM) : AVERROR(EINVAL);
         goto end;
     }
 
     /* parse expressions */
     buf = args1;
     i = 0;
-    while (expr = av_strtok(buf, ":", &bufptr)) {
+    while (i < FF_ARRAY_ELEMS(eval->expr) && (expr = av_strtok(buf, "|", &buf))) {
         ret = av_expr_parse(&eval->expr[i], expr, var_names,
                             NULL, NULL, NULL, NULL, 0, ctx);
         if (ret < 0)
             goto end;
         i++;
-        if (bufptr && *bufptr == ':') { /* found last expression */
-            bufptr++;
-            break;
-        }
-        buf = NULL;
     }
     eval->nb_channels = i;
 
-    if (bufptr && (ret = av_set_options_string(eval, bufptr, "=", ":")) < 0)
-        goto end;
-
     if (eval->chlayout_str) {
         int n;
         ret = ff_parse_channel_layout(&eval->chlayout, eval->chlayout_str, ctx);
@@ -212,14 +202,16 @@
 static int request_frame(AVFilterLink *outlink)
 {
     EvalContext *eval = outlink->src->priv;
-    AVFilterBufferRef *samplesref;
+    AVFrame *samplesref;
     int i, j;
     double t = eval->n * (double)1/eval->sample_rate;
 
     if (eval->duration >= 0 && t >= eval->duration)
         return AVERROR_EOF;
 
-    samplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, eval->nb_samples);
+    samplesref = ff_get_audio_buffer(outlink, eval->nb_samples);
+    if (!samplesref)
+        return AVERROR(ENOMEM);
 
     /* evaluate expression for each single sample and for each channel */
     for (i = 0; i < eval->nb_samples; i++, eval->n++) {
@@ -233,13 +225,10 @@
     }
 
     samplesref->pts = eval->pts;
-    samplesref->pos = -1;
-    samplesref->audio->sample_rate = eval->sample_rate;
+    samplesref->sample_rate = eval->sample_rate;
     eval->pts += eval->nb_samples;
 
-    ff_filter_frame(outlink, samplesref);
-
-    return 0;
+    return ff_filter_frame(outlink, samplesref);
 }
 
 static const AVFilterPad aevalsrc_outputs[] = {
diff --git a/libavfilter/asrc_anullsrc.c b/libavfilter/asrc_anullsrc.c
index 43e9a7c..5a009c3 100644
--- a/libavfilter/asrc_anullsrc.c
+++ b/libavfilter/asrc_anullsrc.c
@@ -59,17 +59,11 @@
 
 AVFILTER_DEFINE_CLASS(anullsrc);
 
-static int init(AVFilterContext *ctx, const char *args)
+static int init(AVFilterContext *ctx)
 {
     ANullContext *null = ctx->priv;
     int ret;
 
-    null->class = &anullsrc_class;
-    av_opt_set_defaults(null);
-
-    if ((ret = (av_set_options_string(null, args, "=", ":"))) < 0)
-        return ret;
-
     if ((ret = ff_parse_sample_rate(&null->sample_rate,
                                      null->sample_rate_str, ctx)) < 0)
         return ret;
@@ -81,17 +75,25 @@
     return 0;
 }
 
+static int query_formats(AVFilterContext *ctx)
+{
+    ANullContext *null = ctx->priv;
+    int64_t chlayouts[] = { null->channel_layout, -1 };
+    int sample_rates[] = { null->sample_rate, -1 };
+
+    ff_set_common_formats        (ctx, ff_all_formats(AVMEDIA_TYPE_AUDIO));
+    ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
+    ff_set_common_samplerates    (ctx, ff_make_format_list(sample_rates));
+
+    return 0;
+}
+
 static int config_props(AVFilterLink *outlink)
 {
     ANullContext *null = outlink->src->priv;
     char buf[128];
-    int chans_nb;
 
-    outlink->sample_rate = null->sample_rate;
-    outlink->channel_layout = null->channel_layout;
-
-    chans_nb = av_get_channel_layout_nb_channels(null->channel_layout);
-    av_get_channel_layout_string(buf, sizeof(buf), chans_nb, null->channel_layout);
+    av_get_channel_layout_string(buf, sizeof(buf), 0, null->channel_layout);
     av_log(outlink->src, AV_LOG_VERBOSE,
            "sample_rate:%d channel_layout:'%s' nb_samples:%d\n",
            null->sample_rate, buf, null->nb_samples);
@@ -101,21 +103,25 @@
 
 static int request_frame(AVFilterLink *outlink)
 {
+    int ret;
     ANullContext *null = outlink->src->priv;
-    AVFilterBufferRef *samplesref;
+    AVFrame *samplesref;
 
-    samplesref =
-        ff_get_audio_buffer(outlink, AV_PERM_WRITE, null->nb_samples);
+    samplesref = ff_get_audio_buffer(outlink, null->nb_samples);
+    if (!samplesref)
+        return AVERROR(ENOMEM);
+
     samplesref->pts = null->pts;
-    samplesref->pos = -1;
-    samplesref->audio->channel_layout = null->channel_layout;
-    samplesref->audio->sample_rate = outlink->sample_rate;
+    samplesref->channel_layout = null->channel_layout;
+    samplesref->sample_rate = outlink->sample_rate;
 
-    ff_filter_frame(outlink, avfilter_ref_buffer(samplesref, ~0));
-    avfilter_unref_buffer(samplesref);
+    ret = ff_filter_frame(outlink, av_frame_clone(samplesref));
+    av_frame_free(&samplesref);
+    if (ret < 0)
+        return ret;
 
     null->pts += null->nb_samples;
-    return 0;
+    return ret;
 }
 
 static const AVFilterPad avfilter_asrc_anullsrc_outputs[] = {
@@ -133,6 +139,7 @@
     .description = NULL_IF_CONFIG_SMALL("Null audio source, return empty audio frames."),
 
     .init        = init,
+    .query_formats = query_formats,
     .priv_size   = sizeof(ANullContext),
 
     .inputs      = NULL,
diff --git a/libavfilter/asrc_flite.c b/libavfilter/asrc_flite.c
index 04901da..1d5b9e6 100644
--- a/libavfilter/asrc_flite.c
+++ b/libavfilter/asrc_flite.c
@@ -131,17 +131,11 @@
     return AVERROR(EINVAL);
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     FliteContext *flite = ctx->priv;
     int ret = 0;
 
-    flite->class = &flite_class;
-    av_opt_set_defaults(flite);
-
-    if ((ret = av_set_options_string(flite, args, "=", ":")) < 0)
-        return ret;
-
     if (flite->list_voices) {
         list_voices(ctx, "\n");
         return AVERROR_EXIT;
@@ -200,8 +194,6 @@
 {
     FliteContext *flite = ctx->priv;
 
-    av_opt_free(flite);
-
     if (!--flite->voice_entry->usage_count)
         flite->voice_entry->unregister_fn(flite->voice);
     flite->voice = NULL;
@@ -245,22 +237,22 @@
 
 static int request_frame(AVFilterLink *outlink)
 {
-    AVFilterBufferRef *samplesref;
+    AVFrame *samplesref;
     FliteContext *flite = outlink->src->priv;
     int nb_samples = FFMIN(flite->wave_nb_samples, flite->frame_nb_samples);
 
     if (!nb_samples)
         return AVERROR_EOF;
 
-    samplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
+    samplesref = ff_get_audio_buffer(outlink, nb_samples);
     if (!samplesref)
         return AVERROR(ENOMEM);
 
     memcpy(samplesref->data[0], flite->wave_samples,
            nb_samples * flite->wave->num_channels * 2);
     samplesref->pts = flite->pts;
-    samplesref->pos = -1;
-    samplesref->audio->sample_rate = flite->wave->sample_rate;
+    av_frame_set_pkt_pos(samplesref, -1);
+    av_frame_set_sample_rate(samplesref, flite->wave->sample_rate);
     flite->pts += nb_samples;
     flite->wave_samples += nb_samples * flite->wave->num_channels;
     flite->wave_nb_samples -= nb_samples;
diff --git a/libavfilter/asrc_sine.c b/libavfilter/asrc_sine.c
new file mode 100644
index 0000000..86af7cc
--- /dev/null
+++ b/libavfilter/asrc_sine.c
@@ -0,0 +1,223 @@
+/*
+ * Copyright (c) 2013 Nicolas George
+ *
+ * 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 <float.h>
+
+#include "libavutil/avassert.h"
+#include "libavutil/channel_layout.h"
+#include "libavutil/opt.h"
+#include "audio.h"
+#include "avfilter.h"
+#include "internal.h"
+
+typedef struct {
+    const AVClass *class;
+    double frequency;
+    double beep_factor;
+    int samples_per_frame;
+    int sample_rate;
+    int64_t duration;
+    int16_t *sin;
+    int64_t pts;
+    uint32_t phi;  ///< current phase of the sine (2pi = 1<<32)
+    uint32_t dphi; ///< phase increment between two samples
+    unsigned beep_period;
+    unsigned beep_index;
+    unsigned beep_length;
+    uint32_t phi_beep;  ///< current phase of the beep
+    uint32_t dphi_beep; ///< phase increment of the beep
+} SineContext;
+
+#define CONTEXT SineContext
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+#define OPT_GENERIC(name, field, def, min, max, descr, type, deffield, ...) \
+    { name, descr, offsetof(CONTEXT, field), AV_OPT_TYPE_ ## type,          \
+      { .deffield = def }, min, max, FLAGS, __VA_ARGS__ }
+
+#define OPT_INT(name, field, def, min, max, descr, ...) \
+    OPT_GENERIC(name, field, def, min, max, descr, INT, i64, __VA_ARGS__)
+
+#define OPT_DBL(name, field, def, min, max, descr, ...) \
+    OPT_GENERIC(name, field, def, min, max, descr, DOUBLE, dbl, __VA_ARGS__)
+
+#define OPT_DUR(name, field, def, min, max, descr, ...) \
+    OPT_GENERIC(name, field, def, min, max, descr, DURATION, str, __VA_ARGS__)
+
+static const AVOption sine_options[] = {
+    OPT_DBL("frequency",         frequency,            440, 0, DBL_MAX,   "set the sine frequency"),
+    OPT_DBL("f",                 frequency,            440, 0, DBL_MAX,   "set the sine frequency"),
+    OPT_DBL("beep_factor",       beep_factor,            0, 0, DBL_MAX,   "set the beep fequency factor"),
+    OPT_DBL("b",                 beep_factor,            0, 0, DBL_MAX,   "set the beep fequency factor"),
+    OPT_INT("sample_rate",       sample_rate,        44100, 1, INT_MAX,   "set the sample rate"),
+    OPT_INT("r",                 sample_rate,        44100, 1, INT_MAX,   "set the sample rate"),
+    OPT_DUR("duration",          duration,               0, 0, INT64_MAX, "set the audio duration"),
+    OPT_DUR("d",                 duration,               0, 0, INT64_MAX, "set the audio duration"),
+    OPT_INT("samples_per_frame", samples_per_frame,   1024, 0, INT_MAX,   "set the number of samples per frame"),
+    {NULL},
+};
+
+AVFILTER_DEFINE_CLASS(sine);
+
+#define LOG_PERIOD 15
+#define AMPLITUDE 4095
+#define AMPLITUDE_SHIFT 3
+
+static void make_sin_table(int16_t *sin)
+{
+    unsigned half_pi = 1 << (LOG_PERIOD - 2);
+    unsigned ampls = AMPLITUDE << AMPLITUDE_SHIFT;
+    uint64_t unit2 = (uint64_t)(ampls * ampls) << 32;
+    unsigned step, i, c, s, k, new_k, n2;
+
+    /* Principle: if u = exp(i*a1) and v = exp(i*a2), then
+       exp(i*(a1+a2)/2) = (u+v) / length(u+v) */
+    sin[0] = 0;
+    sin[half_pi] = ampls;
+    for (step = half_pi; step > 1; step /= 2) {
+        /* k = (1 << 16) * amplitude / length(u+v)
+           In exact values, k is constant at a given step */
+        k = 0x10000;
+        for (i = 0; i < half_pi / 2; i += step) {
+            s = sin[i] + sin[i + step];
+            c = sin[half_pi - i] + sin[half_pi - i - step];
+            n2 = s * s + c * c;
+            /* Newton's method to solve n² * k² = unit² */
+            while (1) {
+                new_k = (k + unit2 / ((uint64_t)k * n2) + 1) >> 1;
+                if (k == new_k)
+                    break;
+                k = new_k;
+            }
+            sin[i + step / 2] = (k * s + 0x7FFF) >> 16;
+            sin[half_pi - i - step / 2] = (k * c + 0x8000) >> 16;
+        }
+    }
+    /* Unshift amplitude */
+    for (i = 0; i <= half_pi; i++)
+        sin[i] = (sin[i] + (1 << (AMPLITUDE_SHIFT - 1))) >> AMPLITUDE_SHIFT;
+    /* Use symmetries to fill the other three quarters */
+    for (i = 0; i < half_pi; i++)
+        sin[half_pi * 2 - i] = sin[i];
+    for (i = 0; i < 2 * half_pi; i++)
+        sin[i + 2 * half_pi] = -sin[i];
+}
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    SineContext *sine = ctx->priv;
+
+    if (!(sine->sin = av_malloc(sizeof(*sine->sin) << LOG_PERIOD)))
+        return AVERROR(ENOMEM);
+    sine->dphi = ldexp(sine->frequency, 32) / sine->sample_rate + 0.5;
+    make_sin_table(sine->sin);
+
+    if (sine->beep_factor) {
+        sine->beep_period = sine->sample_rate;
+        sine->beep_length = sine->beep_period / 25;
+        sine->dphi_beep = ldexp(sine->beep_factor * sine->frequency, 32) /
+                          sine->sample_rate + 0.5;
+    }
+
+    return 0;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    SineContext *sine = ctx->priv;
+
+    av_freep(&sine->sin);
+}
+
+static av_cold int query_formats(AVFilterContext *ctx)
+{
+    SineContext *sine = ctx->priv;
+    static const int64_t chlayouts[] = { AV_CH_LAYOUT_MONO, -1 };
+    int sample_rates[] = { sine->sample_rate, -1 };
+    static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16,
+                                                       AV_SAMPLE_FMT_NONE };
+
+    ff_set_common_formats (ctx, ff_make_format_list(sample_fmts));
+    ff_set_common_channel_layouts(ctx, avfilter_make_format64_list(chlayouts));
+    ff_set_common_samplerates(ctx, ff_make_format_list(sample_rates));
+    return 0;
+}
+
+static av_cold int config_props(AVFilterLink *outlink)
+{
+    SineContext *sine = outlink->src->priv;
+    sine->duration = av_rescale(sine->duration, sine->sample_rate, AV_TIME_BASE);
+    return 0;
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+    SineContext *sine = outlink->src->priv;
+    AVFrame *frame;
+    int i, nb_samples = sine->samples_per_frame;
+    int16_t *samples;
+
+    if (sine->duration) {
+        nb_samples = FFMIN(nb_samples, sine->duration - sine->pts);
+        av_assert1(nb_samples >= 0);
+        if (!nb_samples)
+            return AVERROR_EOF;
+    }
+    if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
+        return AVERROR(ENOMEM);
+    samples = (int16_t *)frame->data[0];
+
+    for (i = 0; i < nb_samples; i++) {
+        samples[i] = sine->sin[sine->phi >> (32 - LOG_PERIOD)];
+        sine->phi += sine->dphi;
+        if (sine->beep_index < sine->beep_length) {
+            samples[i] += sine->sin[sine->phi_beep >> (32 - LOG_PERIOD)] << 1;
+            sine->phi_beep += sine->dphi_beep;
+        }
+        if (++sine->beep_index == sine->beep_period)
+            sine->beep_index = 0;
+    }
+
+    frame->pts = sine->pts;
+    sine->pts += nb_samples;
+    return ff_filter_frame(outlink, frame);
+}
+
+static const AVFilterPad sine_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_AUDIO,
+        .request_frame = request_frame,
+        .config_props  = config_props,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_asrc_sine = {
+    .name          = "sine",
+    .description   = NULL_IF_CONFIG_SMALL("Generate sine wave audio signal."),
+    .query_formats = query_formats,
+    .init          = init,
+    .uninit        = uninit,
+    .priv_size     = sizeof(SineContext),
+    .inputs        = NULL,
+    .outputs       = sine_outputs,
+    .priv_class    = &sine_class,
+};
diff --git a/libavfilter/audio.c b/libavfilter/audio.c
index c72979d..1075217 100644
--- a/libavfilter/audio.c
+++ b/libavfilter/audio.c
@@ -22,6 +22,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
+#include "libavcodec/avcodec.h"
 
 #include "audio.h"
 #include "avfilter.h"
@@ -32,69 +33,70 @@
     return ref->audio ? ref->audio->channels : 0;
 }
 
-AVFilterBufferRef *ff_null_get_audio_buffer(AVFilterLink *link, int perms,
-                                            int nb_samples)
+AVFrame *ff_null_get_audio_buffer(AVFilterLink *link, int nb_samples)
 {
-    return ff_get_audio_buffer(link->dst->outputs[0], perms, nb_samples);
+    return ff_get_audio_buffer(link->dst->outputs[0], nb_samples);
 }
 
-AVFilterBufferRef *ff_default_get_audio_buffer(AVFilterLink *link, int perms,
-                                               int nb_samples)
+AVFrame *ff_default_get_audio_buffer(AVFilterLink *link, int nb_samples)
 {
-    AVFilterBufferRef *samplesref = NULL;
-    uint8_t **data;
-    int planar      = av_sample_fmt_is_planar(link->format);
-    int nb_channels = link->channels;
-    int planes      = planar ? nb_channels : 1;
-    int linesize;
-    int full_perms = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE |
-                     AV_PERM_REUSE | AV_PERM_REUSE2 | AV_PERM_ALIGN;
+    AVFrame *frame = av_frame_alloc();
+    int channels = link->channels;
+    int buf_size, ret;
 
-    av_assert1(!(perms & ~(full_perms | AV_PERM_NEG_LINESIZES)));
+    av_assert0(channels == av_get_channel_layout_nb_channels(link->channel_layout) || !av_get_channel_layout_nb_channels(link->channel_layout));
 
-    if (!(data = av_mallocz(sizeof(*data) * planes)))
+    if (!frame)
+        return NULL;
+
+    buf_size = av_samples_get_buffer_size(NULL, channels, nb_samples,
+                                          link->format, 0);
+    if (buf_size < 0)
         goto fail;
 
-    if (av_samples_alloc(data, &linesize, nb_channels, nb_samples, link->format, 0) < 0)
+    frame->buf[0] = av_buffer_alloc(buf_size);
+    if (!frame->buf[0])
         goto fail;
 
-    samplesref = avfilter_get_audio_buffer_ref_from_arrays_channels(
-        data, linesize, full_perms, nb_samples, link->format,
-        link->channels, link->channel_layout);
-    if (!samplesref)
+    frame->nb_samples = nb_samples;
+    ret = avcodec_fill_audio_frame(frame, channels, link->format,
+                                   frame->buf[0]->data, buf_size, 0);
+    if (ret < 0)
         goto fail;
 
-    samplesref->audio->sample_rate = link->sample_rate;
+    av_samples_set_silence(frame->extended_data, 0, nb_samples, channels,
+                           link->format);
 
-    av_freep(&data);
+    frame->nb_samples     = nb_samples;
+    frame->format         = link->format;
+    av_frame_set_channels(frame, link->channels);
+    frame->channel_layout = link->channel_layout;
+    frame->sample_rate    = link->sample_rate;
+
+    return frame;
 
 fail:
-    if (data)
-        av_freep(&data[0]);
-    av_freep(&data);
-    return samplesref;
+    av_buffer_unref(&frame->buf[0]);
+    av_frame_free(&frame);
+    return NULL;
 }
 
-AVFilterBufferRef *ff_get_audio_buffer(AVFilterLink *link, int perms,
-                                       int nb_samples)
+AVFrame *ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
 {
-    AVFilterBufferRef *ret = NULL;
+    AVFrame *ret = NULL;
 
     if (link->dstpad->get_audio_buffer)
-        ret = link->dstpad->get_audio_buffer(link, perms, nb_samples);
+        ret = link->dstpad->get_audio_buffer(link, nb_samples);
 
     if (!ret)
-        ret = ff_default_get_audio_buffer(link, perms, nb_samples);
-
-    if (ret)
-        ret->type = AVMEDIA_TYPE_AUDIO;
+        ret = ff_default_get_audio_buffer(link, nb_samples);
 
     return ret;
 }
 
+#if FF_API_AVFILTERBUFFER
 AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays_channels(uint8_t **data,
-                                                                      int linesize,
-                                                                      int perms,
+                                                                      int linesize,int perms,
                                                                       int nb_samples,
                                                                       enum AVSampleFormat sample_fmt,
                                                                       int channels,
@@ -179,3 +181,4 @@
                                                               nb_samples, sample_fmt,
                                                               channels, channel_layout);
 }
+#endif
diff --git a/libavfilter/audio.h b/libavfilter/audio.h
index 8fe4d8e..3335c96 100644
--- a/libavfilter/audio.h
+++ b/libavfilter/audio.h
@@ -44,25 +44,21 @@
 };
 
 /** default handler for get_audio_buffer() for audio inputs */
-AVFilterBufferRef *ff_default_get_audio_buffer(AVFilterLink *link, int perms,
-                                                     int nb_samples);
+AVFrame *ff_default_get_audio_buffer(AVFilterLink *link, int nb_samples);
 
 /** get_audio_buffer() handler for filters which simply pass audio along */
-AVFilterBufferRef *ff_null_get_audio_buffer(AVFilterLink *link, int perms,
-                                                  int nb_samples);
+AVFrame *ff_null_get_audio_buffer(AVFilterLink *link, int nb_samples);
 
 /**
  * Request an audio samples buffer with a specific set of permissions.
  *
  * @param link           the output link to the filter from which the buffer will
  *                       be requested
- * @param perms          the required access permissions
  * @param nb_samples     the number of samples per channel
  * @return               A reference to the samples. This must be unreferenced with
  *                       avfilter_unref_buffer when you are finished with it.
  */
-AVFilterBufferRef *ff_get_audio_buffer(AVFilterLink *link, int perms,
-                                             int nb_samples);
+AVFrame *ff_get_audio_buffer(AVFilterLink *link, int nb_samples);
 
 /**
  * Send a buffer of audio samples to the next filter.
diff --git a/libavfilter/avcodec.c b/libavfilter/avcodec.c
index dd3c886..605e5d2 100644
--- a/libavfilter/avcodec.c
+++ b/libavfilter/avcodec.c
@@ -25,54 +25,10 @@
 
 #include "avcodec.h"
 #include "libavutil/avassert.h"
+#include "libavutil/channel_layout.h"
 #include "libavutil/opt.h"
 
-int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
-{
-    dst->pts    = src->pts;
-    dst->pos    = av_frame_get_pkt_pos(src);
-    dst->format = src->format;
-
-    av_dict_free(&dst->metadata);
-    av_dict_copy(&dst->metadata, av_frame_get_metadata(src), 0);
-
-    switch (dst->type) {
-    case AVMEDIA_TYPE_VIDEO:
-        dst->video->w                   = src->width;
-        dst->video->h                   = src->height;
-        dst->video->sample_aspect_ratio = src->sample_aspect_ratio;
-        dst->video->interlaced          = src->interlaced_frame;
-        dst->video->top_field_first     = src->top_field_first;
-        dst->video->key_frame           = src->key_frame;
-        dst->video->pict_type           = src->pict_type;
-        av_freep(&dst->video->qp_table);
-        dst->video->qp_table_linesize = 0;
-        if (src->qscale_table) {
-            int qsize = src->qstride ? src->qstride * ((src->height+15)/16) : (src->width+15)/16;
-            dst->video->qp_table = av_malloc(qsize);
-            if (!dst->video->qp_table)
-                return AVERROR(ENOMEM);
-            dst->video->qp_table_linesize = src->qstride;
-            dst->video->qp_table_size     = qsize;
-            memcpy(dst->video->qp_table, src->qscale_table, qsize);
-        }
-        break;
-    case AVMEDIA_TYPE_AUDIO:
-        dst->audio->sample_rate         = src->sample_rate;
-        dst->audio->channel_layout      = src->channel_layout;
-        dst->audio->channels            = src->channels;
-        if(src->channels < av_get_channel_layout_nb_channels(src->channel_layout)) {
-            av_log(NULL, AV_LOG_ERROR, "libavfilter does not support this channel layout\n");
-            return AVERROR(EINVAL);
-        }
-        break;
-    default:
-        return AVERROR(EINVAL);
-    }
-
-    return 0;
-}
-
+#if FF_API_AVFILTERBUFFER
 AVFilterBufferRef *avfilter_get_video_buffer_ref_from_frame(const AVFrame *frame,
                                                             int perms)
 {
@@ -178,8 +134,9 @@
 
     return 0;
 }
+#endif
 
-#ifdef FF_API_FILL_FRAME
+#if FF_API_FILL_FRAME
 int avfilter_fill_frame_from_audio_buffer_ref(AVFrame *frame,
                                               const AVFilterBufferRef *samplesref)
 {
diff --git a/libavfilter/avcodec.h b/libavfilter/avcodec.h
index 5f4209a..ae55df7 100644
--- a/libavfilter/avcodec.h
+++ b/libavfilter/avcodec.h
@@ -28,31 +28,17 @@
  * symbols defined below will not be available.
  */
 
-#include "libavcodec/avcodec.h" // AVFrame
 #include "avfilter.h"
 
-/**
- * Copy the frame properties of src to dst, without copying the actual
- * image data.
- *
- * @return 0 on success, a negative number on error.
- */
-int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src);
-
-/**
- * Copy the frame properties and data pointers of src to dst, without copying
- * the actual data.
- *
- * @return 0 on success, a negative number on error.
- */
-int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src);
-
+#if FF_API_AVFILTERBUFFER
 /**
  * Create and return a picref reference from the data and properties
  * contained in frame.
  *
  * @param perms permissions to assign to the new buffer reference
+ * @deprecated avfilter APIs work natively with AVFrame instead.
  */
+attribute_deprecated
 AVFilterBufferRef *avfilter_get_video_buffer_ref_from_frame(const AVFrame *frame, int perms);
 
 
@@ -61,7 +47,9 @@
  * contained in frame.
  *
  * @param perms permissions to assign to the new buffer reference
+ * @deprecated avfilter APIs work natively with AVFrame instead.
  */
+attribute_deprecated
 AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_frame(const AVFrame *frame,
                                                             int perms);
 
@@ -70,12 +58,15 @@
  * contained in frame.
  *
  * @param perms permissions to assign to the new buffer reference
+ * @deprecated avfilter APIs work natively with AVFrame instead.
  */
+attribute_deprecated
 AVFilterBufferRef *avfilter_get_buffer_ref_from_frame(enum AVMediaType type,
                                                       const AVFrame *frame,
                                                       int perms);
+#endif
 
-#ifdef FF_API_FILL_FRAME
+#if FF_API_FILL_FRAME
 /**
  * Fill an AVFrame with the information stored in samplesref.
  *
@@ -116,16 +107,4 @@
                                         const AVFilterBufferRef *ref);
 #endif
 
-/**
- * Add frame data to buffer_src.
- *
- * @param buffer_src  pointer to a buffer source context
- * @param frame       a frame, or NULL to mark EOF
- * @param flags       a combination of AV_BUFFERSRC_FLAG_*
- * @return            >= 0 in case of success, a negative AVERROR code
- *                    in case of failure
- */
-int av_buffersrc_add_frame(AVFilterContext *buffer_src,
-                           const AVFrame *frame, int flags);
-
 #endif /* AVFILTER_AVCODEC_H */
diff --git a/libavfilter/avf_concat.c b/libavfilter/avf_concat.c
index 079d55d..04812aa 100644
--- a/libavfilter/avf_concat.c
+++ b/libavfilter/avf_concat.c
@@ -156,8 +156,7 @@
     return 0;
 }
 
-static void push_frame(AVFilterContext *ctx, unsigned in_no,
-                       AVFilterBufferRef *buf)
+static int push_frame(AVFilterContext *ctx, unsigned in_no, AVFrame *buf)
 {
     ConcatContext *cat = ctx->priv;
     unsigned out_no = in_no % ctx->nb_outputs;
@@ -171,7 +170,7 @@
     /* add duration to input PTS */
     if (inlink->sample_rate)
         /* use number of audio samples */
-        in->pts += av_rescale_q(buf->audio->nb_samples,
+        in->pts += av_rescale_q(buf->nb_samples,
                                 (AVRational){ 1, inlink->sample_rate },
                                 outlink->time_base);
     else if (in->nb_frames >= 2)
@@ -179,10 +178,10 @@
         in->pts = av_rescale(in->pts, in->nb_frames, in->nb_frames - 1);
 
     buf->pts += cat->delta_ts;
-    ff_filter_frame(outlink, buf);
+    return ff_filter_frame(outlink, buf);
 }
 
-static void process_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
+static int process_frame(AVFilterLink *inlink, AVFrame *buf)
 {
     AVFilterContext *ctx  = inlink->dst;
     ConcatContext *cat    = ctx->priv;
@@ -191,38 +190,36 @@
     if (in_no < cat->cur_idx) {
         av_log(ctx, AV_LOG_ERROR, "Frame after EOF on input %s\n",
                ctx->input_pads[in_no].name);
-        avfilter_unref_buffer(buf);
+        av_frame_free(&buf);
     } else if (in_no >= cat->cur_idx + ctx->nb_outputs) {
         ff_bufqueue_add(ctx, &cat->in[in_no].queue, buf);
     } else {
-        push_frame(ctx, in_no, buf);
+        return push_frame(ctx, in_no, buf);
     }
+    return 0;
 }
 
-static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms,
-                                           int w, int h)
+static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
 {
     AVFilterContext *ctx = inlink->dst;
     unsigned in_no = FF_INLINK_IDX(inlink);
     AVFilterLink *outlink = ctx->outputs[in_no % ctx->nb_outputs];
 
-    return ff_get_video_buffer(outlink, perms, w, h);
+    return ff_get_video_buffer(outlink, w, h);
 }
 
-static AVFilterBufferRef *get_audio_buffer(AVFilterLink *inlink, int perms,
-                                           int nb_samples)
+static AVFrame *get_audio_buffer(AVFilterLink *inlink, int nb_samples)
 {
     AVFilterContext *ctx = inlink->dst;
     unsigned in_no = FF_INLINK_IDX(inlink);
     AVFilterLink *outlink = ctx->outputs[in_no % ctx->nb_outputs];
 
-    return ff_get_audio_buffer(outlink, perms, nb_samples);
+    return ff_get_audio_buffer(outlink, nb_samples);
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
 {
-    process_frame(inlink, buf);
-    return 0; /* enhancement: handle error return */
+    return process_frame(inlink, buf);
 }
 
 static void close_input(AVFilterContext *ctx, unsigned in_no)
@@ -235,7 +232,7 @@
            ctx->input_pads[in_no].name, cat->nb_in_active);
 }
 
-static void find_next_delta_ts(AVFilterContext *ctx)
+static void find_next_delta_ts(AVFilterContext *ctx, int64_t *seg_delta)
 {
     ConcatContext *cat = ctx->priv;
     unsigned i = cat->cur_idx;
@@ -246,44 +243,51 @@
     for (; i < imax; i++)
         pts = FFMAX(pts, cat->in[i].pts);
     cat->delta_ts += pts;
+    *seg_delta = pts;
 }
 
-static void send_silence(AVFilterContext *ctx, unsigned in_no, unsigned out_no)
+static int send_silence(AVFilterContext *ctx, unsigned in_no, unsigned out_no,
+                        int64_t seg_delta)
 {
     ConcatContext *cat = ctx->priv;
     AVFilterLink *outlink = ctx->outputs[out_no];
-    int64_t base_pts = cat->in[in_no].pts + cat->delta_ts;
+    int64_t base_pts = cat->in[in_no].pts + cat->delta_ts - seg_delta;
     int64_t nb_samples, sent = 0;
-    int frame_nb_samples;
+    int frame_nb_samples, ret;
     AVRational rate_tb = { 1, ctx->inputs[in_no]->sample_rate };
-    AVFilterBufferRef *buf;
+    AVFrame *buf;
     int nb_channels = av_get_channel_layout_nb_channels(outlink->channel_layout);
 
     if (!rate_tb.den)
-        return;
-    nb_samples = av_rescale_q(cat->delta_ts - base_pts,
+        return AVERROR_BUG;
+    nb_samples = av_rescale_q(seg_delta - cat->in[in_no].pts,
                               outlink->time_base, rate_tb);
     frame_nb_samples = FFMAX(9600, rate_tb.den / 5); /* arbitrary */
     while (nb_samples) {
         frame_nb_samples = FFMIN(frame_nb_samples, nb_samples);
-        buf = ff_get_audio_buffer(outlink, AV_PERM_WRITE, frame_nb_samples);
+        buf = ff_get_audio_buffer(outlink, frame_nb_samples);
         if (!buf)
-            return;
+            return AVERROR(ENOMEM);
         av_samples_set_silence(buf->extended_data, 0, frame_nb_samples,
                                nb_channels, outlink->format);
         buf->pts = base_pts + av_rescale_q(sent, rate_tb, outlink->time_base);
-        ff_filter_frame(outlink, buf);
+        ret = ff_filter_frame(outlink, buf);
+        if (ret < 0)
+            return ret;
         sent       += frame_nb_samples;
         nb_samples -= frame_nb_samples;
     }
+    return 0;
 }
 
-static void flush_segment(AVFilterContext *ctx)
+static int flush_segment(AVFilterContext *ctx)
 {
+    int ret;
     ConcatContext *cat = ctx->priv;
     unsigned str, str_max;
+    int64_t seg_delta;
 
-    find_next_delta_ts(ctx);
+    find_next_delta_ts(ctx, &seg_delta);
     cat->cur_idx += ctx->nb_outputs;
     cat->nb_in_active = ctx->nb_outputs;
     av_log(ctx, AV_LOG_VERBOSE, "Segment finished at pts=%"PRId64"\n",
@@ -293,15 +297,24 @@
         /* pad audio streams with silence */
         str = cat->nb_streams[AVMEDIA_TYPE_VIDEO];
         str_max = str + cat->nb_streams[AVMEDIA_TYPE_AUDIO];
-        for (; str < str_max; str++)
-            send_silence(ctx, cat->cur_idx - ctx->nb_outputs + str, str);
+        for (; str < str_max; str++) {
+            ret = send_silence(ctx, cat->cur_idx - ctx->nb_outputs + str, str,
+                               seg_delta);
+            if (ret < 0)
+                return ret;
+        }
         /* flush queued buffers */
         /* possible enhancement: flush in PTS order */
         str_max = cat->cur_idx + ctx->nb_outputs;
-        for (str = cat->cur_idx; str < str_max; str++)
-            while (cat->in[str].queue.available)
-                push_frame(ctx, str, ff_bufqueue_get(&cat->in[str].queue));
+        for (str = cat->cur_idx; str < str_max; str++) {
+            while (cat->in[str].queue.available) {
+                ret = push_frame(ctx, str, ff_bufqueue_get(&cat->in[str].queue));
+                if (ret < 0)
+                    return ret;
+            }
+        }
     }
+    return 0;
 }
 
 static int request_frame(AVFilterLink *outlink)
@@ -335,32 +348,24 @@
             else if (ret < 0)
                 return ret;
         }
-        flush_segment(ctx);
+        ret = flush_segment(ctx);
+        if (ret < 0)
+            return ret;
         in_no += ctx->nb_outputs;
     }
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     ConcatContext *cat = ctx->priv;
-    int ret;
     unsigned seg, type, str;
 
-    cat->class = &concat_class;
-    av_opt_set_defaults(cat);
-    ret = av_set_options_string(cat, args, "=", ":");
-    if (ret < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Error parsing options: '%s'\n", args);
-        return ret;
-    }
-
     /* create input pads */
     for (seg = 0; seg < cat->nb_segments; seg++) {
         for (type = 0; type < TYPE_ALL; type++) {
             for (str = 0; str < cat->nb_streams[type]; str++) {
                 AVFilterPad pad = {
                     .type             = type,
-                    .min_perms        = AV_PERM_READ | AV_PERM_PRESERVE,
                     .get_video_buffer = get_video_buffer,
                     .get_audio_buffer = get_audio_buffer,
                     .filter_frame     = filter_frame,
@@ -414,4 +419,5 @@
     .inputs        = NULL,
     .outputs       = NULL,
     .priv_class    = &concat_class,
+    .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS,
 };
diff --git a/libavfilter/avf_showspectrum.c b/libavfilter/avf_showspectrum.c
index bb8b62f..058f22c 100644
--- a/libavfilter/avf_showspectrum.c
+++ b/libavfilter/avf_showspectrum.c
@@ -41,7 +41,7 @@
 typedef struct {
     const AVClass *class;
     int w, h;
-    AVFilterBufferRef *outpicref;
+    AVFrame *outpicref;
     int req_fullfilled;
     int nb_display_channels;
     int channel_height;
@@ -97,20 +97,6 @@
     {    1,                  1,                  0,                   0 }
 };
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    ShowSpectrumContext *showspectrum = ctx->priv;
-    int err;
-
-    showspectrum->class = &showspectrum_class;
-    av_opt_set_defaults(showspectrum);
-
-    if ((err = av_set_options_string(showspectrum, args, "=", ":")) < 0)
-        return err;
-
-    return 0;
-}
-
 static av_cold void uninit(AVFilterContext *ctx)
 {
     ShowSpectrumContext *showspectrum = ctx->priv;
@@ -122,7 +108,7 @@
         av_freep(&showspectrum->rdft_data[i]);
     av_freep(&showspectrum->rdft_data);
     av_freep(&showspectrum->window_func_lut);
-    avfilter_unref_bufferp(&showspectrum->outpicref);
+    av_frame_free(&showspectrum->outpicref);
 }
 
 static int query_formats(AVFilterContext *ctx)
@@ -179,7 +165,7 @@
     /* (re-)configuration if the video output changed (or first init) */
     if (rdft_bits != showspectrum->rdft_bits) {
         size_t rdft_size, rdft_listsize;
-        AVFilterBufferRef *outpicref;
+        AVFrame *outpicref;
 
         av_rdft_end(showspectrum->rdft);
         showspectrum->rdft = av_rdft_init(rdft_bits, DFT_R2C);
@@ -219,14 +205,15 @@
             showspectrum->window_func_lut[i] = .5f * (1 - cos(2*M_PI*i / (win_size-1)));
 
         /* prepare the initial picref buffer (black frame) */
-        avfilter_unref_bufferp(&showspectrum->outpicref);
+        av_frame_free(&showspectrum->outpicref);
         showspectrum->outpicref = outpicref =
-            ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_PRESERVE|AV_PERM_REUSE2,
-                                outlink->w, outlink->h);
+            ff_get_video_buffer(outlink, outlink->w, outlink->h);
         if (!outpicref)
             return AVERROR(ENOMEM);
         outlink->sample_aspect_ratio = (AVRational){1,1};
-        memset(outpicref->data[0], 0, outlink->h * outpicref->linesize[0]);
+        memset(outpicref->data[0],   0, outlink->h * outpicref->linesize[0]);
+        memset(outpicref->data[1], 128, outlink->h * outpicref->linesize[1]);
+        memset(outpicref->data[2], 128, outlink->h * outpicref->linesize[2]);
     }
 
     if (showspectrum->xpos >= outlink->w)
@@ -241,7 +228,7 @@
     return 0;
 }
 
-inline static void push_frame(AVFilterLink *outlink)
+inline static int push_frame(AVFilterLink *outlink)
 {
     ShowSpectrumContext *showspectrum = outlink->src->priv;
 
@@ -251,7 +238,7 @@
     showspectrum->filled = 0;
     showspectrum->req_fullfilled = 1;
 
-    ff_filter_frame(outlink, avfilter_ref_buffer(showspectrum->outpicref, ~AV_PERM_WRITE));
+    return ff_filter_frame(outlink, av_frame_clone(showspectrum->outpicref));
 }
 
 static int request_frame(AVFilterLink *outlink)
@@ -270,12 +257,13 @@
     return ret;
 }
 
-static int plot_spectrum_column(AVFilterLink *inlink, AVFilterBufferRef *insamples, int nb_samples)
+static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples, int nb_samples)
 {
+    int ret;
     AVFilterContext *ctx = inlink->dst;
     AVFilterLink *outlink = ctx->outputs[0];
     ShowSpectrumContext *showspectrum = ctx->priv;
-    AVFilterBufferRef *outpicref = showspectrum->outpicref;
+    AVFrame *outpicref = showspectrum->outpicref;
 
     /* nb_freq contains the power of two superior or equal to the output image
      * height (or half the RDFT window size) */
@@ -454,27 +442,31 @@
             av_rescale_q(showspectrum->consumed,
                          (AVRational){ 1, inlink->sample_rate },
                          outlink->time_base);
-        push_frame(outlink);
+        ret = push_frame(outlink);
+        if (ret < 0)
+            return ret;
     }
 
     return add_samples;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamples)
+static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
 {
     AVFilterContext *ctx = inlink->dst;
     ShowSpectrumContext *showspectrum = ctx->priv;
-    int left_samples = insamples->audio->nb_samples;
+    int ret = 0, left_samples = insamples->nb_samples;
 
     showspectrum->consumed = 0;
     while (left_samples) {
-        const int added_samples = plot_spectrum_column(inlink, insamples, left_samples);
-        showspectrum->consumed += added_samples;
-        left_samples -= added_samples;
+        int ret = plot_spectrum_column(inlink, insamples, left_samples);
+        if (ret < 0)
+            break;
+        showspectrum->consumed += ret;
+        left_samples -= ret;
     }
 
-    avfilter_unref_buffer(insamples);
-    return 0;
+    av_frame_free(&insamples);
+    return ret;
 }
 
 static const AVFilterPad showspectrum_inputs[] = {
@@ -482,7 +474,6 @@
         .name         = "default",
         .type         = AVMEDIA_TYPE_AUDIO,
         .filter_frame = filter_frame,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL }
 };
@@ -500,7 +491,6 @@
 AVFilter avfilter_avf_showspectrum = {
     .name           = "showspectrum",
     .description    = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output."),
-    .init           = init,
     .uninit         = uninit,
     .query_formats  = query_formats,
     .priv_size      = sizeof(ShowSpectrumContext),
diff --git a/libavfilter/avf_showwaves.c b/libavfilter/avf_showwaves.c
index 1b9d28d..5e89146 100644
--- a/libavfilter/avf_showwaves.c
+++ b/libavfilter/avf_showwaves.c
@@ -41,10 +41,9 @@
 typedef struct {
     const AVClass *class;
     int w, h;
-    char *rate_str;
     AVRational rate;
     int buf_idx;
-    AVFilterBufferRef *outpicref;
+    AVFrame *outpicref;
     int req_fullfilled;
     int n;
     int sample_count_mod;
@@ -55,41 +54,24 @@
 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
 static const AVOption showwaves_options[] = {
-    { "rate", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
-    { "r",    "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
     { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
     { "s",    "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "600x240"}, 0, 0, FLAGS },
+    { "mode", "select display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_POINT}, 0, MODE_NB-1, FLAGS, "mode"},
+        { "point", "draw a point for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_POINT}, .flags=FLAGS, .unit="mode"},
+        { "line",  "draw a line for each sample",  0, AV_OPT_TYPE_CONST, {.i64=MODE_LINE},  .flags=FLAGS, .unit="mode"},
     { "n",    "set how many samples to show in the same point", OFFSET(n), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
-
-    {"mode",  "select display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_POINT}, 0, MODE_NB-1, FLAGS, "mode"},
-    {"point", "draw a point for each sample", 0, AV_OPT_TYPE_CONST, {.i64=MODE_POINT}, .flags=FLAGS, .unit="mode"},
-    {"line",  "draw a line for each sample",  0, AV_OPT_TYPE_CONST, {.i64=MODE_LINE},  .flags=FLAGS, .unit="mode"},
+    { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
+    { "r",    "set video rate", OFFSET(rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
     { NULL },
 };
 
 AVFILTER_DEFINE_CLASS(showwaves);
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    ShowWavesContext *showwaves = ctx->priv;
-    int err;
-
-    showwaves->class = &showwaves_class;
-    av_opt_set_defaults(showwaves);
-    showwaves->buf_idx = 0;
-
-    if ((err = av_set_options_string(showwaves, args, "=", ":")) < 0)
-        return err;
-
-    return 0;
-}
-
 static av_cold void uninit(AVFilterContext *ctx)
 {
     ShowWavesContext *showwaves = ctx->priv;
 
-    av_freep(&showwaves->rate_str);
-    avfilter_unref_bufferp(&showwaves->outpicref);
+    av_frame_free(&showwaves->outpicref);
 }
 
 static int query_formats(AVFilterContext *ctx)
@@ -131,23 +113,11 @@
     AVFilterContext *ctx = outlink->src;
     AVFilterLink *inlink = ctx->inputs[0];
     ShowWavesContext *showwaves = ctx->priv;
-    int err;
 
-    if (showwaves->n && showwaves->rate_str) {
-        av_log(ctx, AV_LOG_ERROR, "Options 'n' and 'rate' cannot be set at the same time\n");
-        return AVERROR(EINVAL);
-    }
-
-    if (!showwaves->n) {
-        if (!showwaves->rate_str)
-            showwaves->rate = (AVRational){25,1}; /* set default value */
-        else if ((err = av_parse_video_rate(&showwaves->rate, showwaves->rate_str)) < 0) {
-            av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", showwaves->rate_str);
-            return err;
-        }
+    if (!showwaves->n)
         showwaves->n = FFMAX(1, ((double)inlink->sample_rate / (showwaves->w * av_q2d(showwaves->rate))) + 0.5);
-    }
 
+    showwaves->buf_idx = 0;
     outlink->w = showwaves->w;
     outlink->h = showwaves->h;
     outlink->sample_aspect_ratio = (AVRational){1,1};
@@ -190,16 +160,16 @@
 
 #define MAX_INT16 ((1<<15) -1)
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamples)
+static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
 {
     AVFilterContext *ctx = inlink->dst;
     AVFilterLink *outlink = ctx->outputs[0];
     ShowWavesContext *showwaves = ctx->priv;
-    const int nb_samples = insamples->audio->nb_samples;
-    AVFilterBufferRef *outpicref = showwaves->outpicref;
+    const int nb_samples = insamples->nb_samples;
+    AVFrame *outpicref = showwaves->outpicref;
     int linesize = outpicref ? outpicref->linesize[0] : 0;
     int16_t *p = (int16_t *)insamples->data[0];
-    int nb_channels = av_get_channel_layout_nb_channels(insamples->audio->channel_layout);
+    int nb_channels = av_get_channel_layout_nb_channels(insamples->channel_layout);
     int i, j, k, h, ret = 0;
     const int n = showwaves->n;
     const int x = 255 / (nb_channels * n); /* multiplication factor, pre-computed to avoid in-loop divisions */
@@ -208,12 +178,11 @@
     for (i = 0; i < nb_samples; i++) {
         if (!showwaves->outpicref) {
             showwaves->outpicref = outpicref =
-                ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN,
-                                    outlink->w, outlink->h);
+                ff_get_video_buffer(outlink, outlink->w, outlink->h);
             if (!outpicref)
                 return AVERROR(ENOMEM);
-            outpicref->video->w = outlink->w;
-            outpicref->video->h = outlink->h;
+            outpicref->width  = outlink->w;
+            outpicref->height = outlink->h;
             outpicref->pts = insamples->pts +
                              av_rescale_q((p - (int16_t *)insamples->data[0]) / nb_channels,
                                           (AVRational){ 1, inlink->sample_rate },
@@ -251,7 +220,7 @@
         outpicref = showwaves->outpicref;
     }
 
-    avfilter_unref_buffer(insamples);
+    av_frame_free(&insamples);
     return ret;
 }
 
@@ -260,7 +229,6 @@
         .name         = "default",
         .type         = AVMEDIA_TYPE_AUDIO,
         .filter_frame = filter_frame,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL }
 };
@@ -278,7 +246,6 @@
 AVFilter avfilter_avf_showwaves = {
     .name           = "showwaves",
     .description    = NULL_IF_CONFIG_SMALL("Convert input audio to a video output."),
-    .init           = init,
     .uninit         = uninit,
     .query_formats  = query_formats,
     .priv_size      = sizeof(ShowWavesContext),
diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c
index ffd1b4e..06d92b2 100644
--- a/libavfilter/avfilter.c
+++ b/libavfilter/avfilter.c
@@ -23,7 +23,9 @@
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
+#include "libavutil/eval.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/rational.h"
 #include "libavutil/samplefmt.h"
@@ -34,43 +36,31 @@
 #include "internal.h"
 #include "audio.h"
 
-static int ff_filter_frame_framed(AVFilterLink *link, AVFilterBufferRef *frame);
+static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame);
 
-char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
-{
-    snprintf(buf, buf_size, "%s%s%s%s%s%s",
-             perms & AV_PERM_READ      ? "r" : "",
-             perms & AV_PERM_WRITE     ? "w" : "",
-             perms & AV_PERM_PRESERVE  ? "p" : "",
-             perms & AV_PERM_REUSE     ? "u" : "",
-             perms & AV_PERM_REUSE2    ? "U" : "",
-             perms & AV_PERM_NEG_LINESIZES ? "n" : "");
-    return buf;
-}
-
-void ff_tlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
+void ff_tlog_ref(void *ctx, AVFrame *ref, int end)
 {
     av_unused char buf[16];
     ff_tlog(ctx,
-            "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
-            ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
+            "ref[%p buf:%p data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
+            ref, ref->buf, ref->data[0],
             ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
-            ref->pts, ref->pos);
+            ref->pts, av_frame_get_pkt_pos(ref));
 
-    if (ref->video) {
+    if (ref->width) {
         ff_tlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
-                ref->video->sample_aspect_ratio.num, ref->video->sample_aspect_ratio.den,
-                ref->video->w, ref->video->h,
-                !ref->video->interlaced     ? 'P' :         /* Progressive  */
-                ref->video->top_field_first ? 'T' : 'B',    /* Top / Bottom */
-                ref->video->key_frame,
-                av_get_picture_type_char(ref->video->pict_type));
+                ref->sample_aspect_ratio.num, ref->sample_aspect_ratio.den,
+                ref->width, ref->height,
+                !ref->interlaced_frame     ? 'P' :         /* Progressive  */
+                ref->top_field_first ? 'T' : 'B',    /* Top / Bottom */
+                ref->key_frame,
+                av_get_picture_type_char(ref->pict_type));
     }
-    if (ref->audio) {
+    if (ref->nb_samples) {
         ff_tlog(ctx, " cl:%"PRId64"d n:%d r:%d",
-                ref->audio->channel_layout,
-                ref->audio->nb_samples,
-                ref->audio->sample_rate);
+                ref->channel_layout,
+                ref->nb_samples,
+                ref->sample_rate);
     }
 
     ff_tlog(ctx, "]%s", end ? "\n" : "");
@@ -158,10 +148,7 @@
     if (!*link)
         return;
 
-    if ((*link)->pool)
-        ff_free_pool((*link)->pool);
-
-    avfilter_unref_bufferp(&(*link)->partial_buf);
+    av_frame_free(&(*link)->partial_buf);
 
     av_freep(link);
 }
@@ -337,18 +324,27 @@
 
     if (link->closed)
         return AVERROR_EOF;
-    if (link->srcpad->request_frame)
-        ret = link->srcpad->request_frame(link);
-    else if (link->src->inputs[0])
-        ret = ff_request_frame(link->src->inputs[0]);
-    if (ret == AVERROR_EOF && link->partial_buf) {
-        AVFilterBufferRef *pbuf = link->partial_buf;
-        link->partial_buf = NULL;
-        ff_filter_frame_framed(link, pbuf);
-        return 0;
+    av_assert0(!link->frame_requested);
+    link->frame_requested = 1;
+    while (link->frame_requested) {
+        if (link->srcpad->request_frame)
+            ret = link->srcpad->request_frame(link);
+        else if (link->src->inputs[0])
+            ret = ff_request_frame(link->src->inputs[0]);
+        if (ret == AVERROR_EOF && link->partial_buf) {
+            AVFrame *pbuf = link->partial_buf;
+            link->partial_buf = NULL;
+            ret = ff_filter_frame_framed(link, pbuf);
+        }
+        if (ret < 0) {
+            link->frame_requested = 0;
+            if (ret == AVERROR_EOF)
+                link->closed = 1;
+        } else {
+            av_assert0(!link->frame_requested ||
+                       link->flags & FF_LINK_FLAG_REQUEST_LOOP);
+        }
     }
-    if (ret == AVERROR_EOF)
-        link->closed = 1;
     return ret;
 }
 
@@ -370,6 +366,49 @@
     return min;
 }
 
+static const char *const var_names[] = {   "t",   "n",   "pos",        NULL };
+enum                                   { VAR_T, VAR_N, VAR_POS, VAR_VARS_NB };
+
+static int set_enable_expr(AVFilterContext *ctx, const char *expr)
+{
+    int ret;
+    char *expr_dup;
+    AVExpr *old = ctx->enable;
+
+    if (!(ctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)) {
+        av_log(ctx, AV_LOG_ERROR, "Timeline ('enable' option) not supported "
+               "with filter '%s'\n", ctx->filter->name);
+        return AVERROR_PATCHWELCOME;
+    }
+
+    expr_dup = av_strdup(expr);
+    if (!expr_dup)
+        return AVERROR(ENOMEM);
+
+    if (!ctx->var_values) {
+        ctx->var_values = av_calloc(VAR_VARS_NB, sizeof(*ctx->var_values));
+        if (!ctx->var_values) {
+            av_free(expr_dup);
+            return AVERROR(ENOMEM);
+        }
+    }
+
+    ret = av_expr_parse((AVExpr**)&ctx->enable, expr_dup, var_names,
+                        NULL, NULL, NULL, NULL, 0, ctx->priv);
+    if (ret < 0) {
+        av_log(ctx->priv, AV_LOG_ERROR,
+               "Error when evaluating the expression '%s' for enable\n",
+               expr_dup);
+        av_free(expr_dup);
+        return ret;
+    }
+
+    av_expr_free(old);
+    av_free(ctx->enable_str);
+    ctx->enable_str = expr_dup;
+    return 0;
+}
+
 void ff_update_link_current_pts(AVFilterLink *link, int64_t pts)
 {
     if (pts == AV_NOPTS_VALUE)
@@ -385,63 +424,66 @@
     if(!strcmp(cmd, "ping")){
         av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
         return 0;
+    }else if(!strcmp(cmd, "enable")) {
+        return set_enable_expr(filter, arg);
     }else if(filter->filter->process_command) {
         return filter->filter->process_command(filter, cmd, arg, res, res_len, flags);
     }
     return AVERROR(ENOSYS);
 }
 
-#define MAX_REGISTERED_AVFILTERS_NB 256
-
-static AVFilter *registered_avfilters[MAX_REGISTERED_AVFILTERS_NB + 1];
-
-static int next_registered_avfilter_idx = 0;
+static AVFilter *first_filter;
 
 AVFilter *avfilter_get_by_name(const char *name)
 {
-    int i;
+    AVFilter *f = NULL;
 
-    for (i = 0; registered_avfilters[i]; i++)
-        if (!strcmp(registered_avfilters[i]->name, name))
-            return registered_avfilters[i];
+    if (!name)
+        return NULL;
+
+    while ((f = avfilter_next(f)))
+        if (!strcmp(f->name, name))
+            return f;
 
     return NULL;
 }
 
 int avfilter_register(AVFilter *filter)
 {
+    AVFilter **f = &first_filter;
     int i;
 
-    if (next_registered_avfilter_idx == MAX_REGISTERED_AVFILTERS_NB) {
-        av_log(NULL, AV_LOG_ERROR,
-               "Maximum number of registered filters %d reached, "
-               "impossible to register filter with name '%s'\n",
-               MAX_REGISTERED_AVFILTERS_NB, filter->name);
-        return AVERROR(ENOMEM);
-    }
-
     for(i=0; filter->inputs && filter->inputs[i].name; i++) {
         const AVFilterPad *input = &filter->inputs[i];
         av_assert0(     !input->filter_frame
                     || (!input->start_frame && !input->end_frame));
     }
 
-    registered_avfilters[next_registered_avfilter_idx++] = filter;
+    while (*f)
+        f = &(*f)->next;
+    *f = filter;
+    filter->next = NULL;
+
     return 0;
 }
 
+const AVFilter *avfilter_next(const AVFilter *prev)
+{
+    return prev ? prev->next : first_filter;
+}
+
+#if FF_API_OLD_FILTER_REGISTER
 AVFilter **av_filter_next(AVFilter **filter)
 {
-    return filter ? ++filter : &registered_avfilters[0];
+    return filter ? &(*filter)->next : &first_filter;
 }
 
 void avfilter_uninit(void)
 {
-    memset(registered_avfilters, 0, sizeof(registered_avfilters));
-    next_registered_avfilter_idx = 0;
 }
+#endif
 
-static int pad_count(const AVFilterPad *pads)
+int avfilter_pad_count(const AVFilterPad *pads)
 {
     int count;
 
@@ -461,56 +503,59 @@
 static void *filter_child_next(void *obj, void *prev)
 {
     AVFilterContext *ctx = obj;
-    if (!prev && ctx->filter && ctx->filter->priv_class)
+    if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
         return ctx->priv;
     return NULL;
 }
 
 static const AVClass *filter_child_class_next(const AVClass *prev)
 {
-    AVFilter **filter_ptr = NULL;
+    AVFilter *f = NULL;
 
     /* find the filter that corresponds to prev */
-    while (prev && *(filter_ptr = av_filter_next(filter_ptr)))
-        if ((*filter_ptr)->priv_class == prev)
+    while (prev && (f = avfilter_next(f)))
+        if (f->priv_class == prev)
             break;
 
     /* could not find filter corresponding to prev */
-    if (prev && !(*filter_ptr))
+    if (prev && !f)
         return NULL;
 
     /* find next filter with specific options */
-    while (*(filter_ptr = av_filter_next(filter_ptr)))
-        if ((*filter_ptr)->priv_class)
-            return (*filter_ptr)->priv_class;
+    while ((f = avfilter_next(f)))
+        if (f->priv_class)
+            return f->priv_class;
+
     return NULL;
 }
 
+#define OFFSET(x) offsetof(AVFilterContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM
+static const AVOption filters_common_options[] = {
+    { "enable", "set enable expression", OFFSET(enable_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
+    { NULL }
+};
+
 static const AVClass avfilter_class = {
     .class_name = "AVFilter",
     .item_name  = default_filter_name,
     .version    = LIBAVUTIL_VERSION_INT,
     .category   = AV_CLASS_CATEGORY_FILTER,
     .child_next = filter_child_next,
+    .option     = filters_common_options,
     .child_class_next = filter_child_class_next,
 };
 
-const AVClass *avfilter_get_class(void)
-{
-    return &avfilter_class;
-}
-
-int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
+AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
 {
     AVFilterContext *ret;
-    *filter_ctx = NULL;
 
     if (!filter)
-        return AVERROR(EINVAL);
+        return NULL;
 
     ret = av_mallocz(sizeof(AVFilterContext));
     if (!ret)
-        return AVERROR(ENOMEM);
+        return NULL;
 
     ret->av_class = &avfilter_class;
     ret->filter   = filter;
@@ -521,7 +566,12 @@
             goto err;
     }
 
-    ret->nb_inputs = pad_count(filter->inputs);
+    if (filter->priv_class) {
+        *(const AVClass**)ret->priv = filter->priv_class;
+        av_opt_set_defaults(ret->priv);
+    }
+
+    ret->nb_inputs = avfilter_pad_count(filter->inputs);
     if (ret->nb_inputs ) {
         ret->input_pads   = av_malloc(sizeof(AVFilterPad) * ret->nb_inputs);
         if (!ret->input_pads)
@@ -532,7 +582,7 @@
             goto err;
     }
 
-    ret->nb_outputs = pad_count(filter->outputs);
+    ret->nb_outputs = avfilter_pad_count(filter->outputs);
     if (ret->nb_outputs) {
         ret->output_pads  = av_malloc(sizeof(AVFilterPad) * ret->nb_outputs);
         if (!ret->output_pads)
@@ -547,8 +597,7 @@
     ret->input_count  = ret->nb_inputs;
 #endif
 
-    *filter_ctx = ret;
-    return 0;
+    return ret;
 
 err:
     av_freep(&ret->inputs);
@@ -559,9 +608,17 @@
     ret->nb_outputs = 0;
     av_freep(&ret->priv);
     av_free(ret);
-    return AVERROR(ENOMEM);
+    return NULL;
 }
 
+#if FF_API_AVFILTER_OPEN
+int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name)
+{
+    *filter_ctx = ff_filter_alloc(filter, inst_name);
+    return *filter_ctx ? 0 : AVERROR(ENOMEM);
+}
+#endif
+
 void avfilter_free(AVFilterContext *filter)
 {
     int i;
@@ -570,6 +627,9 @@
     if (!filter)
         return;
 
+    if (filter->graph)
+        ff_filter_graph_remove_filter(filter->graph, filter);
+
     if (filter->filter->uninit)
         filter->filter->uninit(filter);
 
@@ -600,6 +660,9 @@
         avfilter_link_free(&link);
     }
 
+    if (filter->filter->priv_class)
+        av_opt_free(filter->priv);
+
     av_freep(&filter->name);
     av_freep(&filter->input_pads);
     av_freep(&filter->output_pads);
@@ -609,100 +672,309 @@
     while(filter->command_queue){
         ff_command_queue_pop(filter);
     }
+    av_opt_free(filter);
+    av_expr_free(filter->enable);
+    filter->enable = NULL;
+    av_freep(&filter->var_values);
     av_free(filter);
 }
 
+static int process_options(AVFilterContext *ctx, AVDictionary **options,
+                           const char *args)
+{
+    const AVOption *o = NULL;
+    int ret, count = 0;
+    char *av_uninit(parsed_key), *av_uninit(value);
+    const char *key;
+    int offset= -1;
+
+    av_opt_set_defaults(ctx);
+
+    if (!args)
+        return 0;
+
+    while (*args) {
+        const char *shorthand = NULL;
+
+        o = av_opt_next(ctx->priv, o);
+        if (o) {
+            if (o->type == AV_OPT_TYPE_CONST || o->offset == offset)
+                continue;
+            offset = o->offset;
+            shorthand = o->name;
+        }
+
+        ret = av_opt_get_key_value(&args, "=", ":",
+                                   shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
+                                   &parsed_key, &value);
+        if (ret < 0) {
+            if (ret == AVERROR(EINVAL))
+                av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", args);
+            else
+                av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", args,
+                       av_err2str(ret));
+            return ret;
+        }
+        if (*args)
+            args++;
+        if (parsed_key) {
+            key = parsed_key;
+            while ((o = av_opt_next(ctx->priv, o))); /* discard all remaining shorthand */
+        } else {
+            key = shorthand;
+        }
+
+        av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
+
+        if (av_opt_find(ctx, key, NULL, 0, 0)) {
+            ret = av_opt_set(ctx, key, value, 0);
+            if (ret < 0)
+                return ret;
+        } else {
+        av_dict_set(options, key, value, 0);
+        if ((ret = av_opt_set(ctx->priv, key, value, 0)) < 0) {
+            if (!av_opt_find(ctx->priv, key, NULL, 0, AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ)) {
+            if (ret == AVERROR_OPTION_NOT_FOUND)
+                av_log(ctx, AV_LOG_ERROR, "Option '%s' not found\n", key);
+            av_free(value);
+            av_free(parsed_key);
+            return ret;
+            }
+        }
+        }
+
+        av_free(value);
+        av_free(parsed_key);
+        count++;
+    }
+
+    if (ctx->enable_str) {
+        ret = set_enable_expr(ctx, ctx->enable_str);
+        if (ret < 0)
+            return ret;
+    }
+    return count;
+}
+
+#if FF_API_AVFILTER_INIT_FILTER
 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque)
 {
-    int ret=0;
+    return avfilter_init_str(filter, args);
+}
+#endif
 
-    if (filter->filter->init_opaque)
-        ret = filter->filter->init_opaque(filter, args, opaque);
-    else if (filter->filter->init)
-        ret = filter->filter->init(filter, args);
+int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
+{
+    int ret = 0;
+
+    if (ctx->filter->priv_class) {
+        ret = av_opt_set_dict(ctx->priv, options);
+        if (ret < 0) {
+            av_log(ctx, AV_LOG_ERROR, "Error applying options to the filter.\n");
+            return ret;
+        }
+    }
+
+    if (ctx->filter->init_opaque)
+        ret = ctx->filter->init_opaque(ctx, NULL);
+    else if (ctx->filter->init)
+        ret = ctx->filter->init(ctx);
+    else if (ctx->filter->init_dict)
+        ret = ctx->filter->init_dict(ctx, options);
+
     return ret;
 }
 
-const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx)
+int avfilter_init_str(AVFilterContext *filter, const char *args)
+{
+    AVDictionary *options = NULL;
+    AVDictionaryEntry *e;
+    int ret=0;
+
+    if (args && *args) {
+        if (!filter->filter->priv_class) {
+            av_log(filter, AV_LOG_ERROR, "This filter does not take any "
+                   "options, but options were provided: %s.\n", args);
+            return AVERROR(EINVAL);
+        }
+
+#if FF_API_OLD_FILTER_OPTS
+            if (   !strcmp(filter->filter->name, "format")     ||
+                   !strcmp(filter->filter->name, "noformat")   ||
+                   !strcmp(filter->filter->name, "frei0r")     ||
+                   !strcmp(filter->filter->name, "frei0r_src") ||
+                   !strcmp(filter->filter->name, "ocv")        ||
+                   !strcmp(filter->filter->name, "pan")        ||
+                   !strcmp(filter->filter->name, "pp")         ||
+                   !strcmp(filter->filter->name, "aevalsrc")) {
+            /* a hack for compatibility with the old syntax
+             * replace colons with |s */
+            char *copy = av_strdup(args);
+            char *p    = copy;
+            int nb_leading = 0; // number of leading colons to skip
+            int deprecated = 0;
+
+            if (!copy) {
+                ret = AVERROR(ENOMEM);
+                goto fail;
+            }
+
+            if (!strcmp(filter->filter->name, "frei0r") ||
+                !strcmp(filter->filter->name, "ocv"))
+                nb_leading = 1;
+            else if (!strcmp(filter->filter->name, "frei0r_src"))
+                nb_leading = 3;
+
+            while (nb_leading--) {
+                p = strchr(p, ':');
+                if (!p) {
+                    p = copy + strlen(copy);
+                    break;
+                }
+                p++;
+            }
+
+            deprecated = strchr(p, ':') != NULL;
+
+            if (!strcmp(filter->filter->name, "aevalsrc")) {
+                deprecated = 0;
+                while ((p = strchr(p, ':')) && p[1] != ':') {
+                    const char *epos = strchr(p + 1, '=');
+                    const char *spos = strchr(p + 1, ':');
+                    const int next_token_is_opt = epos && (!spos || epos < spos);
+                    if (next_token_is_opt) {
+                        p++;
+                        break;
+                    }
+                    /* next token does not contain a '=', assume a channel expression */
+                    deprecated = 1;
+                    *p++ = '|';
+                }
+                if (p && *p == ':') { // double sep '::' found
+                    deprecated = 1;
+                    memmove(p, p + 1, strlen(p));
+                }
+            } else
+            while ((p = strchr(p, ':')))
+                *p++ = '|';
+
+            if (deprecated)
+                av_log(filter, AV_LOG_WARNING, "This syntax is deprecated. Use "
+                       "'|' to separate the list items.\n");
+
+            av_log(filter, AV_LOG_DEBUG, "compat: called with args=[%s]\n", copy);
+            ret = process_options(filter, &options, copy);
+            av_freep(&copy);
+
+            if (ret < 0)
+                goto fail;
+#endif
+        } else {
+#if CONFIG_MP_FILTER
+            if (!strcmp(filter->filter->name, "mp")) {
+                char *escaped;
+
+                if (!strncmp(args, "filter=", 7))
+                    args += 7;
+                ret = av_escape(&escaped, args, ":=", AV_ESCAPE_MODE_BACKSLASH, 0);
+                if (ret < 0) {
+                    av_log(filter, AV_LOG_ERROR, "Unable to escape MPlayer filters arg '%s'\n", args);
+                    goto fail;
+                }
+                ret = process_options(filter, &options, escaped);
+                av_free(escaped);
+            } else
+#endif
+            ret = process_options(filter, &options, args);
+            if (ret < 0)
+                goto fail;
+        }
+    }
+
+    ret = avfilter_init_dict(filter, &options);
+    if (ret < 0)
+        goto fail;
+
+    if ((e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
+        av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
+        ret = AVERROR_OPTION_NOT_FOUND;
+        goto fail;
+    }
+
+fail:
+    av_dict_free(&options);
+
+    return ret;
+}
+
+const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
 {
     return pads[pad_idx].name;
 }
 
-enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx)
+enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
 {
     return pads[pad_idx].type;
 }
 
-static int default_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
+static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
 {
     return ff_filter_frame(link->dst->outputs[0], frame);
 }
 
-static int ff_filter_frame_framed(AVFilterLink *link, AVFilterBufferRef *frame)
+static int ff_filter_frame_framed(AVFilterLink *link, AVFrame *frame)
 {
-    int (*filter_frame)(AVFilterLink *, AVFilterBufferRef *);
-    AVFilterPad *src = link->srcpad;
+    int (*filter_frame)(AVFilterLink *, AVFrame *);
+    AVFilterContext *dstctx = link->dst;
     AVFilterPad *dst = link->dstpad;
-    AVFilterBufferRef *out;
-    int perms, ret;
+    AVFrame *out;
+    int ret;
     AVFilterCommand *cmd= link->dst->command_queue;
     int64_t pts;
 
     if (link->closed) {
-        avfilter_unref_buffer(frame);
+        av_frame_free(&frame);
         return AVERROR_EOF;
     }
 
     if (!(filter_frame = dst->filter_frame))
         filter_frame = default_filter_frame;
 
-    av_assert1((frame->perms & src->min_perms) == src->min_perms);
-    frame->perms &= ~ src->rej_perms;
-    perms = frame->perms;
-
-    if (frame->linesize[0] < 0)
-        perms |= AV_PERM_NEG_LINESIZES;
-
-    /* prepare to copy the frame if the buffer has insufficient permissions */
-    if ((dst->min_perms & perms) != dst->min_perms ||
-        dst->rej_perms & perms) {
-        av_log(link->dst, AV_LOG_DEBUG,
-               "Copying data in avfilter (have perms %x, need %x, reject %x)\n",
-               perms, link->dstpad->min_perms, link->dstpad->rej_perms);
+    /* copy the frame if needed */
+    if (dst->needs_writable && !av_frame_is_writable(frame)) {
+        av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
 
         /* Maybe use ff_copy_buffer_ref instead? */
         switch (link->type) {
         case AVMEDIA_TYPE_VIDEO:
-            out = ff_get_video_buffer(link, dst->min_perms,
-                                      link->w, link->h);
+            out = ff_get_video_buffer(link, link->w, link->h);
             break;
         case AVMEDIA_TYPE_AUDIO:
-            out = ff_get_audio_buffer(link, dst->min_perms,
-                                      frame->audio->nb_samples);
+            out = ff_get_audio_buffer(link, frame->nb_samples);
             break;
         default: return AVERROR(EINVAL);
         }
         if (!out) {
-            avfilter_unref_buffer(frame);
+            av_frame_free(&frame);
             return AVERROR(ENOMEM);
         }
-        avfilter_copy_buffer_ref_props(out, frame);
+        av_frame_copy_props(out, frame);
 
         switch (link->type) {
         case AVMEDIA_TYPE_VIDEO:
             av_image_copy(out->data, out->linesize, (const uint8_t **)frame->data, frame->linesize,
-                          frame->format, frame->video->w, frame->video->h);
+                          frame->format, frame->width, frame->height);
             break;
         case AVMEDIA_TYPE_AUDIO:
             av_samples_copy(out->extended_data, frame->extended_data,
-                            0, 0, frame->audio->nb_samples,
-                            av_get_channel_layout_nb_channels(frame->audio->channel_layout),
+                            0, 0, frame->nb_samples,
+                            av_get_channel_layout_nb_channels(frame->channel_layout),
                             frame->format);
             break;
         default: return AVERROR(EINVAL);
         }
 
-        avfilter_unref_buffer(frame);
+        av_frame_free(&frame);
     } else
         out = frame;
 
@@ -716,53 +988,64 @@
     }
 
     pts = out->pts;
+    if (dstctx->enable_str) {
+        int64_t pos = av_frame_get_pkt_pos(out);
+        dstctx->var_values[VAR_N] = link->frame_count;
+        dstctx->var_values[VAR_T] = pts == AV_NOPTS_VALUE ? NAN : pts * av_q2d(link->time_base);
+        dstctx->var_values[VAR_POS] = pos == -1 ? NAN : pos;
+        if (!av_expr_eval(dstctx->enable, dstctx->var_values, NULL))
+            filter_frame = dst->passthrough_filter_frame ? dst->passthrough_filter_frame
+                                                         : default_filter_frame;
+    }
     ret = filter_frame(link, out);
+    link->frame_count++;
+    link->frame_requested = 0;
     ff_update_link_current_pts(link, pts);
     return ret;
 }
 
-static int ff_filter_frame_needs_framing(AVFilterLink *link, AVFilterBufferRef *frame)
+static int ff_filter_frame_needs_framing(AVFilterLink *link, AVFrame *frame)
 {
-    int insamples = frame->audio->nb_samples, inpos = 0, nb_samples;
-    AVFilterBufferRef *pbuf = link->partial_buf;
-    int nb_channels = frame->audio->channels;
+    int insamples = frame->nb_samples, inpos = 0, nb_samples;
+    AVFrame *pbuf = link->partial_buf;
+    int nb_channels = av_frame_get_channels(frame);
     int ret = 0;
 
+    link->flags |= FF_LINK_FLAG_REQUEST_LOOP;
     /* Handle framing (min_samples, max_samples) */
     while (insamples) {
         if (!pbuf) {
             AVRational samples_tb = { 1, link->sample_rate };
-            int perms = link->dstpad->min_perms | AV_PERM_WRITE;
-            pbuf = ff_get_audio_buffer(link, perms, link->partial_buf_size);
+            pbuf = ff_get_audio_buffer(link, link->partial_buf_size);
             if (!pbuf) {
                 av_log(link->dst, AV_LOG_WARNING,
                        "Samples dropped due to memory allocation failure.\n");
                 return 0;
             }
-            avfilter_copy_buffer_ref_props(pbuf, frame);
+            av_frame_copy_props(pbuf, frame);
             pbuf->pts = frame->pts +
                         av_rescale_q(inpos, samples_tb, link->time_base);
-            pbuf->audio->nb_samples = 0;
+            pbuf->nb_samples = 0;
         }
         nb_samples = FFMIN(insamples,
-                           link->partial_buf_size - pbuf->audio->nb_samples);
+                           link->partial_buf_size - pbuf->nb_samples);
         av_samples_copy(pbuf->extended_data, frame->extended_data,
-                        pbuf->audio->nb_samples, inpos,
+                        pbuf->nb_samples, inpos,
                         nb_samples, nb_channels, link->format);
         inpos                   += nb_samples;
         insamples               -= nb_samples;
-        pbuf->audio->nb_samples += nb_samples;
-        if (pbuf->audio->nb_samples >= link->min_samples) {
+        pbuf->nb_samples += nb_samples;
+        if (pbuf->nb_samples >= link->min_samples) {
             ret = ff_filter_frame_framed(link, pbuf);
             pbuf = NULL;
         }
     }
-    avfilter_unref_buffer(frame);
+    av_frame_free(&frame);
     link->partial_buf = pbuf;
     return ret;
 }
 
-int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
+int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
 {
     FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); ff_tlog_ref(NULL, frame, 1);
 
@@ -770,24 +1053,29 @@
     if (link->type == AVMEDIA_TYPE_VIDEO) {
         if (strcmp(link->dst->filter->name, "scale")) {
             av_assert1(frame->format                 == link->format);
-            av_assert1(frame->video->w               == link->w);
-            av_assert1(frame->video->h               == link->h);
+            av_assert1(frame->width               == link->w);
+            av_assert1(frame->height               == link->h);
         }
     } else {
         av_assert1(frame->format                == link->format);
-        av_assert1(frame->audio->channels       == link->channels);
-        av_assert1(frame->audio->channel_layout == link->channel_layout);
-        av_assert1(frame->audio->sample_rate    == link->sample_rate);
+        av_assert1(av_frame_get_channels(frame) == link->channels);
+        av_assert1(frame->channel_layout        == link->channel_layout);
+        av_assert1(frame->sample_rate           == link->sample_rate);
     }
 
     /* Go directly to actual filtering if possible */
     if (link->type == AVMEDIA_TYPE_AUDIO &&
         link->min_samples &&
         (link->partial_buf ||
-         frame->audio->nb_samples < link->min_samples ||
-         frame->audio->nb_samples > link->max_samples)) {
+         frame->nb_samples < link->min_samples ||
+         frame->nb_samples > link->max_samples)) {
         return ff_filter_frame_needs_framing(link, frame);
     } else {
         return ff_filter_frame_framed(link, frame);
     }
 }
+
+const AVClass *avfilter_get_class(void)
+{
+    return &avfilter_class;
+}
diff --git a/libavfilter/avfilter.h b/libavfilter/avfilter.h
index 1c80167..e7e979e 100644
--- a/libavfilter/avfilter.h
+++ b/libavfilter/avfilter.h
@@ -37,6 +37,7 @@
 
 #include "libavutil/avutil.h"
 #include "libavutil/dict.h"
+#include "libavutil/frame.h"
 #include "libavutil/log.h"
 #include "libavutil/samplefmt.h"
 #include "libavutil/pixfmt.h"
@@ -59,16 +60,12 @@
  */
 const char *avfilter_license(void);
 
-/**
- * Get the class for the AVFilterContext struct.
- */
-const AVClass *avfilter_get_class(void);
-
 typedef struct AVFilterContext AVFilterContext;
 typedef struct AVFilterLink    AVFilterLink;
 typedef struct AVFilterPad     AVFilterPad;
 typedef struct AVFilterFormats AVFilterFormats;
 
+#if FF_API_AVFILTERBUFFER
 /**
  * A reference-counted buffer data type used by the filter system. Filters
  * should not store pointers to this structure directly, but instead use the
@@ -200,6 +197,7 @@
 /**
  * Copy properties of src to dst, without copying the actual data
  */
+attribute_deprecated
 void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src);
 
 /**
@@ -211,6 +209,7 @@
  * @return      a new reference to the buffer with the same properties as the
  *              old, excluding any permissions denied by pmask
  */
+attribute_deprecated
 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask);
 
 /**
@@ -222,6 +221,7 @@
  * @note it is recommended to use avfilter_unref_bufferp() instead of this
  * function
  */
+attribute_deprecated
 void avfilter_unref_buffer(AVFilterBufferRef *ref);
 
 /**
@@ -231,11 +231,14 @@
  *
  * @param ref pointer to the buffer reference
  */
+attribute_deprecated
 void avfilter_unref_bufferp(AVFilterBufferRef **ref);
+#endif
 
 /**
  * Get the number of channels of a buffer reference.
  */
+attribute_deprecated
 int avfilter_ref_get_channels(AVFilterBufferRef *ref);
 
 #if FF_API_AVFILTERPAD_PUBLIC
@@ -273,7 +276,7 @@
      * link must have at least these permissions; this fact is checked by
      * asserts. It can be used to optimize buffer allocation.
      */
-    int min_perms;
+    attribute_deprecated int min_perms;
 
     /**
      * Input pads:
@@ -287,7 +290,7 @@
      * Permissions which are automatically removed on outgoing buffers. It
      * can be used to optimize buffer allocation.
      */
-    int rej_perms;
+    attribute_deprecated int rej_perms;
 
     /**
      * @deprecated unused
@@ -300,7 +303,7 @@
      *
      * Input video pads only.
      */
-    AVFilterBufferRef *(*get_video_buffer)(AVFilterLink *link, int perms, int w, int h);
+    AVFrame *(*get_video_buffer)(AVFilterLink *link, int w, int h);
 
     /**
      * Callback function to get an audio buffer. If NULL, the filter system will
@@ -308,8 +311,7 @@
      *
      * Input audio pads only.
      */
-    AVFilterBufferRef *(*get_audio_buffer)(AVFilterLink *link, int perms,
-                                           int nb_samples);
+    AVFrame *(*get_audio_buffer)(AVFilterLink *link, int nb_samples);
 
     /**
      * @deprecated unused
@@ -331,7 +333,7 @@
      * must ensure that frame is properly unreferenced on error if it
      * hasn't been passed on to another filter.
      */
-    int (*filter_frame)(AVFilterLink *link, AVFilterBufferRef *frame);
+    int (*filter_frame)(AVFilterLink *link, AVFrame *frame);
 
     /**
      * Frame poll callback. This returns the number of immediately available
@@ -381,10 +383,31 @@
      * input pads only.
      */
     int needs_fifo;
+
+    int needs_writable;
+
+    /**
+     * Passthrough filtering callback.
+     *
+     * If a filter supports timeline editing (in case
+     * AVFILTER_FLAG_SUPPORT_TIMELINE is enabled) then it can implement a
+     * custom passthrough callback to update its local context (for example to
+     * keep a frame reference, or simply send the filter to a custom outlink).
+     * The filter must not do any change to the frame in this callback.
+     *
+     * Input pads only.
+     */
+    int (*passthrough_filter_frame)(AVFilterLink *link, AVFrame *frame);
 };
 #endif
 
 /**
+ * Get the number of elements in a NULL-terminated array of AVFilterPads (e.g.
+ * AVFilter.inputs/outputs).
+ */
+int avfilter_pad_count(const AVFilterPad *pads);
+
+/**
  * Get the name of an AVFilterPad.
  *
  * @param pads an array of AVFilterPads
@@ -393,7 +416,7 @@
  *
  * @return name of the pad_idx'th pad in pads
  */
-const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx);
+const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx);
 
 /**
  * Get the type of an AVFilterPad.
@@ -404,7 +427,26 @@
  *
  * @return type of the pad_idx'th pad in pads
  */
-enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx);
+enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx);
+
+/**
+ * The number of the filter inputs is not determined just by AVFilter.inputs.
+ * The filter might add additional inputs during initialization depending on the
+ * options supplied to it.
+ */
+#define AVFILTER_FLAG_DYNAMIC_INPUTS        (1 << 0)
+/**
+ * The number of the filter outputs is not determined just by AVFilter.outputs.
+ * The filter might add additional outputs during initialization depending on
+ * the options supplied to it.
+ */
+#define AVFILTER_FLAG_DYNAMIC_OUTPUTS       (1 << 1)
+/**
+ * Some filters support a generic "enable" expression option that can be used
+ * to enable or disable a filter in the timeline. Filters supporting this
+ * option have this flag set.
+ */
+#define AVFILTER_FLAG_SUPPORT_TIMELINE      (1 << 16)
 
 /**
  * Filter definition. This defines the pads a filter contains, and all the
@@ -422,6 +464,17 @@
     const AVFilterPad *inputs;  ///< NULL terminated list of inputs. NULL if none
     const AVFilterPad *outputs; ///< NULL terminated list of outputs. NULL if none
 
+    /**
+     * A class for the private data, used to access filter private
+     * AVOptions.
+     */
+    const AVClass *priv_class;
+
+    /**
+     * A combination of AVFILTER_FLAG_*
+     */
+    int flags;
+
     /*****************************************************************
      * All fields below this line are not part of the public API. They
      * may not be used outside of libavfilter and can be changed and
@@ -431,10 +484,17 @@
      */
 
     /**
-     * Filter initialization function. Args contains the user-supplied
-     * parameters. FIXME: maybe an AVOption-based system would be better?
+     * Filter initialization function. Called when all the options have been
+     * set.
      */
-    int (*init)(AVFilterContext *ctx, const char *args);
+    int (*init)(AVFilterContext *ctx);
+
+    /**
+     * Should be set instead of init by the filters that want to pass a
+     * dictionary of AVOptions to nested contexts that are allocated in
+     * init.
+     */
+    int (*init_dict)(AVFilterContext *ctx, AVDictionary **options);
 
     /**
      * Filter uninitialization function. Should deallocate any memory held
@@ -455,6 +515,8 @@
 
     int priv_size;      ///< size of private data to allocate for the filter
 
+    struct AVFilter *next;
+
     /**
      * Make the filter instance process a command.
      *
@@ -474,16 +536,14 @@
      * callback. Args contains the user-supplied parameters, opaque is
      * used for providing binary data.
      */
-    int (*init_opaque)(AVFilterContext *ctx, const char *args, void *opaque);
-
-    const AVClass *priv_class;      ///< private class, containing filter specific options
+    int (*init_opaque)(AVFilterContext *ctx, void *opaque);
 } AVFilter;
 
 /** An instance of a filter */
 struct AVFilterContext {
-    const AVClass *av_class;        ///< needed for av_log()
+    const AVClass *av_class;        ///< needed for av_log() and filters common options
 
-    AVFilter *filter;               ///< the AVFilter of which this is an instance
+    const AVFilter *filter;         ///< the AVFilter of which this is an instance
 
     char *name;                     ///< name of this filter instance
 
@@ -503,7 +563,13 @@
 
     void *priv;                     ///< private data for use by the filter
 
+    struct AVFilterGraph *graph;    ///< filtergraph this filter belongs to
+
     struct AVFilterCommand *command_queue;
+
+    char *enable_str;               ///< enable expression string
+    void *enable;                   ///< parsed expression (AVExpr*)
+    double *var_values;             ///< variable values for the enable expression
 };
 
 /**
@@ -616,7 +682,7 @@
     /**
      * Buffer partially filled with samples to achieve a fixed/minimum size.
      */
-    AVFilterBufferRef *partial_buf;
+    AVFrame *partial_buf;
 
     /**
      * Size of the partial buffer to allocate.
@@ -664,6 +730,22 @@
      * Number of channels.
      */
     int channels;
+
+    /**
+     * True if a frame is being requested on the link.
+     * Used internally by the framework.
+     */
+    unsigned frame_requested;
+
+    /**
+     * Link processing flags.
+     */
+    unsigned flags;
+
+    /**
+     * Number of past frames sent through the link.
+     */
+    int64_t frame_count;
 };
 
 /**
@@ -701,6 +783,7 @@
  */
 int avfilter_config_links(AVFilterContext *filter);
 
+#if FF_API_AVFILTERBUFFER
 /**
  * Create a buffer reference wrapped around an already allocated image
  * buffer.
@@ -712,6 +795,7 @@
  * @param h the height of the image specified by the data and linesize arrays
  * @param format the pixel format of the image specified by the data and linesize arrays
  */
+attribute_deprecated
 AVFilterBufferRef *
 avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
                                           int w, int h, enum AVPixelFormat format);
@@ -730,6 +814,7 @@
  * @param sample_fmt     the format of each sample in the buffer to allocate
  * @param channel_layout the channel layout of the buffer
  */
+attribute_deprecated
 AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
                                                              int linesize,
                                                              int perms,
@@ -749,6 +834,7 @@
  * @param channel_layout the channel layout of the buffer,
  *                       must be either 0 or consistent with channels
  */
+attribute_deprecated
 AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_arrays_channels(uint8_t **data,
                                                                       int linesize,
                                                                       int perms,
@@ -757,6 +843,7 @@
                                                                       int channels,
                                                                       uint64_t channel_layout);
 
+#endif
 
 
 #define AVFILTER_CMD_FLAG_ONE   1 ///< Stop once a filter understood the command (for target=all for example), fast filters are favored automatically
@@ -771,14 +858,17 @@
 /** Initialize the filter system. Register all builtin filters. */
 void avfilter_register_all(void);
 
+#if FF_API_OLD_FILTER_REGISTER
 /** Uninitialize the filter system. Unregister all filters. */
+attribute_deprecated
 void avfilter_uninit(void);
+#endif
 
 /**
  * Register a filter. This is only needed if you plan to use
  * avfilter_get_by_name later to lookup the AVFilter structure by name. A
- * filter can still by instantiated with avfilter_open even if it is not
- * registered.
+ * filter can still by instantiated with avfilter_graph_alloc_filter even if it
+ * is not registered.
  *
  * @param filter the filter to register
  * @return 0 if the registration was successful, a negative value
@@ -796,13 +886,25 @@
 AVFilter *avfilter_get_by_name(const char *name);
 
 /**
+ * Iterate over all registered filters.
+ * @return If prev is non-NULL, next registered filter after prev or NULL if
+ * prev is the last filter. If prev is NULL, return the first registered filter.
+ */
+const AVFilter *avfilter_next(const AVFilter *prev);
+
+#if FF_API_OLD_FILTER_REGISTER
+/**
  * If filter is NULL, returns a pointer to the first registered filter pointer,
  * if filter is non-NULL, returns the next pointer after filter.
  * If the returned pointer points to NULL, the last registered filter
  * was already reached.
+ * @deprecated use avfilter_next()
  */
+attribute_deprecated
 AVFilter **av_filter_next(AVFilter **filter);
+#endif
 
+#if FF_API_AVFILTER_OPEN
 /**
  * Create a filter instance.
  *
@@ -811,9 +913,14 @@
  * @param filter    the filter to create an instance of
  * @param inst_name Name to give to the new instance. Can be NULL for none.
  * @return >= 0 in case of success, a negative error code otherwise
+ * @deprecated use avfilter_graph_alloc_filter() instead
  */
+attribute_deprecated
 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name);
+#endif
 
+
+#if FF_API_AVFILTER_INIT_FILTER
 /**
  * Initialize a filter.
  *
@@ -824,10 +931,47 @@
  *               of this parameter varies by filter.
  * @return       zero on success
  */
+attribute_deprecated
 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque);
+#endif
 
 /**
- * Free a filter context.
+ * Initialize a filter with the supplied parameters.
+ *
+ * @param ctx  uninitialized filter context to initialize
+ * @param args Options to initialize the filter with. This must be a
+ *             ':'-separated list of options in the 'key=value' form.
+ *             May be NULL if the options have been set directly using the
+ *             AVOptions API or there are no options that need to be set.
+ * @return 0 on success, a negative AVERROR on failure
+ */
+int avfilter_init_str(AVFilterContext *ctx, const char *args);
+
+/**
+ * Initialize a filter with the supplied dictionary of options.
+ *
+ * @param ctx     uninitialized filter context to initialize
+ * @param options An AVDictionary filled with options for this filter. On
+ *                return this parameter will be destroyed and replaced with
+ *                a dict containing options that were not found. This dictionary
+ *                must be freed by the caller.
+ *                May be NULL, then this function is equivalent to
+ *                avfilter_init_str() with the second parameter set to NULL.
+ * @return 0 on success, a negative AVERROR on failure
+ *
+ * @note This function and avfilter_init_str() do essentially the same thing,
+ * the difference is in manner in which the options are passed. It is up to the
+ * calling code to choose whichever is more preferable. The two functions also
+ * behave differently when some of the provided options are not declared as
+ * supported by the filter. In such a case, avfilter_init_str() will fail, but
+ * this function will leave those extra options in the options AVDictionary and
+ * continue as usual.
+ */
+int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options);
+
+/**
+ * Free a filter context. This will also remove the filter from its
+ * filtergraph's list of filters.
  *
  * @param filter the filter to free
  */
@@ -845,8 +989,309 @@
 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
                            unsigned filt_srcpad_idx, unsigned filt_dstpad_idx);
 
+#if FF_API_AVFILTERBUFFER
+/**
+ * Copy the frame properties of src to dst, without copying the actual
+ * image data.
+ *
+ * @return 0 on success, a negative number on error.
+ */
+attribute_deprecated
+int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src);
+
+/**
+ * Copy the frame properties and data pointers of src to dst, without copying
+ * the actual data.
+ *
+ * @return 0 on success, a negative number on error.
+ */
+attribute_deprecated
+int avfilter_copy_buf_props(AVFrame *dst, const AVFilterBufferRef *src);
+#endif
+
+/**
+ * @return AVClass for AVFilterContext.
+ *
+ * @see av_opt_find().
+ */
+const AVClass *avfilter_get_class(void);
+
+typedef struct AVFilterGraph {
+    const AVClass *av_class;
+#if FF_API_FOO_COUNT
+    attribute_deprecated
+    unsigned filter_count_unused;
+#endif
+    AVFilterContext **filters;
+#if !FF_API_FOO_COUNT
+    unsigned nb_filters;
+#endif
+
+    char *scale_sws_opts; ///< sws options to use for the auto-inserted scale filters
+    char *resample_lavr_opts;   ///< libavresample options to use for the auto-inserted resample filters
+#if FF_API_FOO_COUNT
+    unsigned nb_filters;
+#endif
+    char *aresample_swr_opts; ///< swr options to use for the auto-inserted aresample filters, Access ONLY through AVOptions
+
+    /**
+     * Private fields
+     *
+     * The following fields are for internal use only.
+     * Their type, offset, number and semantic can change without notice.
+     */
+
+    AVFilterLink **sink_links;
+    int sink_links_count;
+
+    unsigned disable_auto_convert;
+} AVFilterGraph;
+
+/**
+ * Allocate a filter graph.
+ */
+AVFilterGraph *avfilter_graph_alloc(void);
+
+/**
+ * Create a new filter instance in a filter graph.
+ *
+ * @param graph graph in which the new filter will be used
+ * @param filter the filter to create an instance of
+ * @param name Name to give to the new instance (will be copied to
+ *             AVFilterContext.name). This may be used by the caller to identify
+ *             different filters, libavfilter itself assigns no semantics to
+ *             this parameter. May be NULL.
+ *
+ * @return the context of the newly created filter instance (note that it is
+ *         also retrievable directly through AVFilterGraph.filters or with
+ *         avfilter_graph_get_filter()) on success or NULL or failure.
+ */
+AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
+                                             const AVFilter *filter,
+                                             const char *name);
+
+/**
+ * Get a filter instance with name name from graph.
+ *
+ * @return the pointer to the found filter instance or NULL if it
+ * cannot be found.
+ */
+AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name);
+
+#if FF_API_AVFILTER_OPEN
+/**
+ * Add an existing filter instance to a filter graph.
+ *
+ * @param graphctx  the filter graph
+ * @param filter the filter to be added
+ *
+ * @deprecated use avfilter_graph_alloc_filter() to allocate a filter in a
+ * filter graph
+ */
+attribute_deprecated
+int avfilter_graph_add_filter(AVFilterGraph *graphctx, AVFilterContext *filter);
+#endif
+
+/**
+ * Create and add a filter instance into an existing graph.
+ * The filter instance is created from the filter filt and inited
+ * with the parameters args and opaque.
+ *
+ * In case of success put in *filt_ctx the pointer to the created
+ * filter instance, otherwise set *filt_ctx to NULL.
+ *
+ * @param name the instance name to give to the created filter instance
+ * @param graph_ctx the filter graph
+ * @return a negative AVERROR error code in case of failure, a non
+ * negative value otherwise
+ */
+int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
+                                 const char *name, const char *args, void *opaque,
+                                 AVFilterGraph *graph_ctx);
+
+/**
+ * Enable or disable automatic format conversion inside the graph.
+ *
+ * Note that format conversion can still happen inside explicitly inserted
+ * scale and aresample filters.
+ *
+ * @param flags  any of the AVFILTER_AUTO_CONVERT_* constants
+ */
+void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags);
+
+enum {
+    AVFILTER_AUTO_CONVERT_ALL  =  0, /**< all automatic conversions enabled */
+    AVFILTER_AUTO_CONVERT_NONE = -1, /**< all automatic conversions disabled */
+};
+
+/**
+ * Check validity and configure all the links and formats in the graph.
+ *
+ * @param graphctx the filter graph
+ * @param log_ctx context used for logging
+ * @return 0 in case of success, a negative AVERROR code otherwise
+ */
+int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx);
+
+/**
+ * Free a graph, destroy its links, and set *graph to NULL.
+ * If *graph is NULL, do nothing.
+ */
+void avfilter_graph_free(AVFilterGraph **graph);
+
+/**
+ * A linked-list of the inputs/outputs of the filter chain.
+ *
+ * This is mainly useful for avfilter_graph_parse() / avfilter_graph_parse2(),
+ * where it is used to communicate open (unlinked) inputs and outputs from and
+ * to the caller.
+ * This struct specifies, per each not connected pad contained in the graph, the
+ * filter context and the pad index required for establishing a link.
+ */
+typedef struct AVFilterInOut {
+    /** unique name for this input/output in the list */
+    char *name;
+
+    /** filter context associated to this input/output */
+    AVFilterContext *filter_ctx;
+
+    /** index of the filt_ctx pad to use for linking */
+    int pad_idx;
+
+    /** next input/input in the list, NULL if this is the last */
+    struct AVFilterInOut *next;
+} AVFilterInOut;
+
+/**
+ * Allocate a single AVFilterInOut entry.
+ * Must be freed with avfilter_inout_free().
+ * @return allocated AVFilterInOut on success, NULL on failure.
+ */
+AVFilterInOut *avfilter_inout_alloc(void);
+
+/**
+ * Free the supplied list of AVFilterInOut and set *inout to NULL.
+ * If *inout is NULL, do nothing.
+ */
+void avfilter_inout_free(AVFilterInOut **inout);
+
+/**
+ * Add a graph described by a string to a graph.
+ *
+ * @param graph   the filter graph where to link the parsed graph context
+ * @param filters string to be parsed
+ * @param inputs  pointer to a linked list to the inputs of the graph, may be NULL.
+ *                If non-NULL, *inputs is updated to contain the list of open inputs
+ *                after the parsing, should be freed with avfilter_inout_free().
+ * @param outputs pointer to a linked list to the outputs of the graph, may be NULL.
+ *                If non-NULL, *outputs is updated to contain the list of open outputs
+ *                after the parsing, should be freed with avfilter_inout_free().
+ * @return non negative on success, a negative AVERROR code on error
+ */
+int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
+                         AVFilterInOut **inputs, AVFilterInOut **outputs,
+                         void *log_ctx);
+
+/**
+ * Add a graph described by a string to a graph.
+ *
+ * @param[in]  graph   the filter graph where to link the parsed graph context
+ * @param[in]  filters string to be parsed
+ * @param[out] inputs  a linked list of all free (unlinked) inputs of the
+ *                     parsed graph will be returned here. It is to be freed
+ *                     by the caller using avfilter_inout_free().
+ * @param[out] outputs a linked list of all free (unlinked) outputs of the
+ *                     parsed graph will be returned here. It is to be freed by the
+ *                     caller using avfilter_inout_free().
+ * @return zero on success, a negative AVERROR code on error
+ *
+ * @note the difference between avfilter_graph_parse2() and
+ * avfilter_graph_parse() is that in avfilter_graph_parse(), the caller provides
+ * the lists of inputs and outputs, which therefore must be known before calling
+ * the function. On the other hand, avfilter_graph_parse2() \em returns the
+ * inputs and outputs that are left unlinked after parsing the graph and the
+ * caller then deals with them. Another difference is that in
+ * avfilter_graph_parse(), the inputs parameter describes inputs of the
+ * <em>already existing</em> part of the graph; i.e. from the point of view of
+ * the newly created part, they are outputs. Similarly the outputs parameter
+ * describes outputs of the already existing filters, which are provided as
+ * inputs to the parsed filters.
+ * avfilter_graph_parse2() takes the opposite approach -- it makes no reference
+ * whatsoever to already existing parts of the graph and the inputs parameter
+ * will on return contain inputs of the newly parsed part of the graph.
+ * Analogously the outputs parameter will contain outputs of the newly created
+ * filters.
+ */
+int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
+                          AVFilterInOut **inputs,
+                          AVFilterInOut **outputs);
+
+/**
+ * Send a command to one or more filter instances.
+ *
+ * @param graph  the filter graph
+ * @param target the filter(s) to which the command should be sent
+ *               "all" sends to all filters
+ *               otherwise it can be a filter or filter instance name
+ *               which will send the command to all matching filters.
+ * @param cmd    the command to sent, for handling simplicity all commands must be alphanumeric only
+ * @param arg    the argument for the command
+ * @param res    a buffer with size res_size where the filter(s) can return a response.
+ *
+ * @returns >=0 on success otherwise an error code.
+ *              AVERROR(ENOSYS) on unsupported commands
+ */
+int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags);
+
+/**
+ * Queue a command for one or more filter instances.
+ *
+ * @param graph  the filter graph
+ * @param target the filter(s) to which the command should be sent
+ *               "all" sends to all filters
+ *               otherwise it can be a filter or filter instance name
+ *               which will send the command to all matching filters.
+ * @param cmd    the command to sent, for handling simplicity all commands must be alphanummeric only
+ * @param arg    the argument for the command
+ * @param ts     time at which the command should be sent to the filter
+ *
+ * @note As this executes commands after this function returns, no return code
+ *       from the filter is provided, also AVFILTER_CMD_FLAG_ONE is not supported.
+ */
+int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, int flags, double ts);
+
+
+/**
+ * Dump a graph into a human-readable string representation.
+ *
+ * @param graph    the graph to dump
+ * @param options  formatting options; currently ignored
+ * @return  a string, or NULL in case of memory allocation failure;
+ *          the string must be freed using av_free
+ */
+char *avfilter_graph_dump(AVFilterGraph *graph, const char *options);
+
+/**
+ * Request a frame on the oldest sink link.
+ *
+ * If the request returns AVERROR_EOF, try the next.
+ *
+ * Note that this function is not meant to be the sole scheduling mechanism
+ * of a filtergraph, only a convenience function to help drain a filtergraph
+ * in a balanced way under normal circumstances.
+ *
+ * Also note that AVERROR_EOF does not mean that frames did not arrive on
+ * some of the sinks during the process.
+ * When there are multiple sink links, in case the requested link
+ * returns an EOF, this may cause a filter to flush pending frames
+ * which are sent to another sink link, although unrequested.
+ *
+ * @return  the return value of ff_request_frame(),
+ *          or AVERROR_EOF if all links returned AVERROR_EOF
+ */
+int avfilter_graph_request_oldest(AVFilterGraph *graph);
+
 /**
  * @}
  */
-
 #endif /* AVFILTER_AVFILTER_H */
diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c
index 8b18c6f..582a870 100644
--- a/libavfilter/avfiltergraph.c
+++ b/libavfilter/avfiltergraph.c
@@ -20,16 +20,16 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include <ctype.h>
 #include <string.h>
 
 #include "libavutil/avassert.h"
+#include "libavutil/avstring.h"
+#include "libavutil/bprint.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavcodec/avcodec.h" // avcodec_find_best_pix_fmt_of_2()
 #include "avfilter.h"
-#include "avfiltergraph.h"
 #include "formats.h"
 #include "internal.h"
 
@@ -52,19 +52,34 @@
 
 AVFilterGraph *avfilter_graph_alloc(void)
 {
-    AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
+    AVFilterGraph *ret = av_mallocz(sizeof(*ret));
     if (!ret)
         return NULL;
     ret->av_class = &filtergraph_class;
     return ret;
 }
 
+void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter)
+{
+    int i;
+    for (i = 0; i < graph->nb_filters; i++) {
+        if (graph->filters[i] == filter) {
+            FFSWAP(AVFilterContext*, graph->filters[i],
+                   graph->filters[graph->nb_filters - 1]);
+            graph->nb_filters--;
+            return;
+        }
+    }
+}
+
 void avfilter_graph_free(AVFilterGraph **graph)
 {
     if (!*graph)
         return;
-    for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
-        avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
+
+    while ((*graph)->nb_filters)
+        avfilter_free((*graph)->filters[0]);
+
     av_freep(&(*graph)->sink_links);
     av_freep(&(*graph)->scale_sws_opts);
     av_freep(&(*graph)->aresample_swr_opts);
@@ -73,18 +88,26 @@
     av_freep(graph);
 }
 
+#if FF_API_AVFILTER_OPEN
 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
 {
     AVFilterContext **filters = av_realloc(graph->filters,
-                                           sizeof(AVFilterContext*) * (graph->filter_count+1));
+                                           sizeof(*filters) * (graph->nb_filters + 1));
     if (!filters)
         return AVERROR(ENOMEM);
 
     graph->filters = filters;
-    graph->filters[graph->filter_count++] = filter;
+    graph->filters[graph->nb_filters++] = filter;
+
+#if FF_API_FOO_COUNT
+    graph->filter_count_unused = graph->nb_filters;
+#endif
+
+    filter->graph = graph;
 
     return 0;
 }
+#endif
 
 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
                                  const char *name, const char *args, void *opaque,
@@ -92,12 +115,14 @@
 {
     int ret;
 
-    if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
+    *filt_ctx = avfilter_graph_alloc_filter(graph_ctx, filt, name);
+    if (!*filt_ctx)
+        return AVERROR(ENOMEM);
+
+    ret = avfilter_init_str(*filt_ctx, args);
+    if (ret < 0)
         goto fail;
-    if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
-        goto fail;
-    if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
-        goto fail;
+
     return 0;
 
 fail:
@@ -112,6 +137,34 @@
     graph->disable_auto_convert = flags;
 }
 
+AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
+                                             const AVFilter *filter,
+                                             const char *name)
+{
+    AVFilterContext **filters, *s;
+
+    s = ff_filter_alloc(filter, name);
+    if (!s)
+        return NULL;
+
+    filters = av_realloc(graph->filters, sizeof(*filters) * (graph->nb_filters + 1));
+    if (!filters) {
+        avfilter_free(s);
+        return NULL;
+    }
+
+    graph->filters = filters;
+    graph->filters[graph->nb_filters++] = s;
+
+#if FF_API_FOO_COUNT
+    graph->filter_count_unused = graph->nb_filters;
+#endif
+
+    s->graph = graph;
+
+    return s;
+}
+
 /**
  * Check for the validity of graph.
  *
@@ -125,7 +178,7 @@
     AVFilterContext *filt;
     int i, j;
 
-    for (i = 0; i < graph->filter_count; i++) {
+    for (i = 0; i < graph->nb_filters; i++) {
         const AVFilterPad *pad;
         filt = graph->filters[i];
 
@@ -163,7 +216,7 @@
     AVFilterContext *filt;
     int i, ret;
 
-    for (i=0; i < graph->filter_count; i++) {
+    for (i = 0; i < graph->nb_filters; i++) {
         filt = graph->filters[i];
 
         if (!filt->nb_outputs) {
@@ -179,7 +232,7 @@
 {
     int i;
 
-    for (i = 0; i < graph->filter_count; i++)
+    for (i = 0; i < graph->nb_filters; i++)
         if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
             return graph->filters[i];
 
@@ -212,8 +265,9 @@
                             AVMEDIA_TYPE_VIDEO;
 
     if ((ret = ctx->filter->query_formats(ctx)) < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
-               ctx->name, av_err2str(ret));
+        if (ret != AVERROR(EAGAIN))
+            av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
+                   ctx->name, av_err2str(ret));
         return ret;
     }
 
@@ -239,30 +293,51 @@
     return 0;
 }
 
+static int formats_declared(AVFilterContext *f)
+{
+    int i;
+
+    for (i = 0; i < f->nb_inputs; i++) {
+        if (!f->inputs[i]->out_formats)
+            return 0;
+        if (f->inputs[i]->type == AVMEDIA_TYPE_AUDIO &&
+            !(f->inputs[i]->out_samplerates &&
+              f->inputs[i]->out_channel_layouts))
+            return 0;
+    }
+    for (i = 0; i < f->nb_outputs; i++) {
+        if (!f->outputs[i]->in_formats)
+            return 0;
+        if (f->outputs[i]->type == AVMEDIA_TYPE_AUDIO &&
+            !(f->outputs[i]->in_samplerates &&
+              f->outputs[i]->in_channel_layouts))
+            return 0;
+    }
+    return 1;
+}
+
 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
 {
     int i, j, ret;
     int scaler_count = 0, resampler_count = 0;
+    int count_queried = 0, count_merged = 0, count_already_merged = 0,
+        count_delayed = 0;
 
-    for (j = 0; j < 2; j++) {
-    /* ask all the sub-filters for their supported media formats */
-    for (i = 0; i < graph->filter_count; i++) {
-        /* Call query_formats on sources first.
-           This is a temporary workaround for amerge,
-           until format renegociation is implemented. */
-        if (!graph->filters[i]->nb_inputs == j)
+    for (i = 0; i < graph->nb_filters; i++) {
+        AVFilterContext *f = graph->filters[i];
+        if (formats_declared(f))
             continue;
-        if (graph->filters[i]->filter->query_formats)
-            ret = filter_query_formats(graph->filters[i]);
+        if (f->filter->query_formats)
+            ret = filter_query_formats(f);
         else
-            ret = ff_default_query_formats(graph->filters[i]);
-        if (ret < 0)
+            ret = ff_default_query_formats(f);
+        if (ret < 0 && ret != AVERROR(EAGAIN))
             return ret;
-    }
+        count_queried++;
     }
 
     /* go through and merge as many format lists as possible */
-    for (i = 0; i < graph->filter_count; i++) {
+    for (i = 0; i < graph->nb_filters; i++) {
         AVFilterContext *filter = graph->filters[i];
 
         for (j = 0; j < filter->nb_inputs; j++) {
@@ -272,20 +347,33 @@
             if (!link)
                 continue;
 
-            if (link->in_formats != link->out_formats &&
-                !ff_merge_formats(link->in_formats, link->out_formats,
-                                  link->type))
-                convert_needed = 1;
-            if (link->type == AVMEDIA_TYPE_AUDIO) {
-                if (link->in_channel_layouts != link->out_channel_layouts &&
-                    !ff_merge_channel_layouts(link->in_channel_layouts,
-                                              link->out_channel_layouts))
-                    convert_needed = 1;
-                if (link->in_samplerates != link->out_samplerates &&
-                    !ff_merge_samplerates(link->in_samplerates,
-                                          link->out_samplerates))
-                    convert_needed = 1;
+#define MERGE_DISPATCH(field, statement)                                     \
+            if (!(link->in_ ## field && link->out_ ## field)) {              \
+                count_delayed++;                                             \
+            } else if (link->in_ ## field == link->out_ ## field) {          \
+                count_already_merged++;                                      \
+            } else {                                                         \
+                count_merged++;                                              \
+                statement                                                    \
             }
+            MERGE_DISPATCH(formats,
+                if (!ff_merge_formats(link->in_formats, link->out_formats,
+                                      link->type))
+                    convert_needed = 1;
+            )
+            if (link->type == AVMEDIA_TYPE_AUDIO) {
+                MERGE_DISPATCH(channel_layouts,
+                    if (!ff_merge_channel_layouts(link->in_channel_layouts,
+                                                  link->out_channel_layouts))
+                        convert_needed = 1;
+                )
+                MERGE_DISPATCH(samplerates,
+                    if (!ff_merge_samplerates(link->in_samplerates,
+                                              link->out_samplerates))
+                        convert_needed = 1;
+                )
+            }
+#undef MERGE_DISPATCH
 
             if (convert_needed) {
                 AVFilterContext *convert;
@@ -305,11 +393,11 @@
 
                     snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
                              scaler_count++);
-                    if (graph->scale_sws_opts)
-                        snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
-                    else
-                        snprintf(scale_args, sizeof(scale_args), "0:0");
-
+                    av_strlcpy(scale_args, "0:0", sizeof(scale_args));
+                    if (graph->scale_sws_opts) {
+                        av_strlcat(scale_args, ":", sizeof(scale_args));
+                        av_strlcat(scale_args, graph->scale_sws_opts, sizeof(scale_args));
+                    }
                     if ((ret = avfilter_graph_create_filter(&convert, filter,
                                                             inst_name, scale_args, NULL,
                                                             graph)) < 0)
@@ -369,6 +457,25 @@
         }
     }
 
+    av_log(graph, AV_LOG_DEBUG, "query_formats: "
+           "%d queried, %d merged, %d already done, %d delayed\n",
+           count_queried, count_merged, count_already_merged, count_delayed);
+    if (count_delayed) {
+        AVBPrint bp;
+
+        if (count_queried || count_merged)
+            return AVERROR(EAGAIN);
+        av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
+        for (i = 0; i < graph->nb_filters; i++)
+            if (!formats_declared(graph->filters[i]))
+                av_bprintf(&bp, "%s%s", bp.len ? ", " : "",
+                          graph->filters[i]->name);
+        av_log(graph, AV_LOG_ERROR,
+               "The following filters could not choose their formats: %s\n"
+               "Consider inserting the (a)format filter near their input or "
+               "output.\n", bp.str);
+        return AVERROR(EIO);
+    }
     return 0;
 }
 
@@ -520,7 +627,7 @@
     do {
         reduced = 0;
 
-        for (i = 0; i < graph->filter_count; i++)
+        for (i = 0; i < graph->nb_filters; i++)
             reduced |= reduce_formats_on_filter(graph->filters[i]);
     } while (reduced);
 }
@@ -568,7 +675,7 @@
 {
     int i;
 
-    for (i = 0; i < graph->filter_count; i++)
+    for (i = 0; i < graph->nb_filters; i++)
         swap_samplerates_on_filter(graph->filters[i]);
 }
 
@@ -699,7 +806,7 @@
 {
     int i;
 
-    for (i = 0; i < graph->filter_count; i++)
+    for (i = 0; i < graph->nb_filters; i++)
         swap_channel_layouts_on_filter(graph->filters[i]);
 }
 
@@ -767,7 +874,7 @@
 {
     int i;
 
-    for (i = 0; i < graph->filter_count; i++)
+    for (i = 0; i < graph->nb_filters; i++)
         swap_sample_fmts_on_filter(graph->filters[i]);
 
 }
@@ -779,7 +886,7 @@
 
     do{
         change = 0;
-        for (i = 0; i < graph->filter_count; i++) {
+        for (i = 0; i < graph->nb_filters; i++) {
             AVFilterContext *filter = graph->filters[i];
             if (filter->nb_inputs){
                 for (j = 0; j < filter->nb_inputs; j++){
@@ -811,7 +918,7 @@
         }
     }while(change);
 
-    for (i = 0; i < graph->filter_count; i++) {
+    for (i = 0; i < graph->nb_filters; i++) {
         AVFilterContext *filter = graph->filters[i];
 
         for (j = 0; j < filter->nb_inputs; j++)
@@ -832,7 +939,9 @@
     int ret;
 
     /* find supported formats from sub-filters, and merge along links */
-    if ((ret = query_formats(graph, log_ctx)) < 0)
+    while ((ret = query_formats(graph, log_ctx)) == AVERROR(EAGAIN))
+        av_log(graph, AV_LOG_DEBUG, "query_formats not finished\n");
+    if (ret < 0)
         return ret;
 
     /* Once everything is merged, it's possible that we'll still have
@@ -860,7 +969,7 @@
     AVFilterContext *f;
     AVFilterLink **sinks;
 
-    for (i = 0; i < graph->filter_count; i++) {
+    for (i = 0; i < graph->nb_filters; i++) {
         f = graph->filters[i];
         for (j = 0; j < f->nb_inputs; j++) {
             f->inputs[j]->graph     = graph;
@@ -879,7 +988,7 @@
     sinks = av_calloc(sink_links_count, sizeof(*sinks));
     if (!sinks)
         return AVERROR(ENOMEM);
-    for (i = 0; i < graph->filter_count; i++) {
+    for (i = 0; i < graph->nb_filters; i++) {
         f = graph->filters[i];
         if (!f->nb_outputs) {
             for (j = 0; j < f->nb_inputs; j++) {
@@ -900,7 +1009,7 @@
     int i, j, ret;
     int fifo_count = 0;
 
-    for (i = 0; i < graph->filter_count; i++) {
+    for (i = 0; i < graph->nb_filters; i++) {
         f = graph->filters[i];
 
         for (j = 0; j < f->nb_inputs; j++) {
@@ -966,7 +1075,7 @@
     if(res_len && res)
         res[0]= 0;
 
-    for (i = 0; i < graph->filter_count; i++) {
+    for (i = 0; i < graph->nb_filters; i++) {
         AVFilterContext *filter = graph->filters[i];
         if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
             r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
@@ -987,7 +1096,7 @@
     if(!graph)
         return 0;
 
-    for (i = 0; i < graph->filter_count; i++) {
+    for (i = 0; i < graph->nb_filters; i++) {
         AVFilterContext *filter = graph->filters[i];
         if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
             AVFilterCommand **queue = &filter->command_queue, *next;
diff --git a/libavfilter/avfiltergraph.h b/libavfilter/avfiltergraph.h
index 3965412..b31d581 100644
--- a/libavfilter/avfiltergraph.h
+++ b/libavfilter/avfiltergraph.h
@@ -25,247 +25,4 @@
 #include "avfilter.h"
 #include "libavutil/log.h"
 
-typedef struct AVFilterGraph {
-    const AVClass *av_class;
-    unsigned filter_count;
-    AVFilterContext **filters;
-
-    char *scale_sws_opts; ///< sws options to use for the auto-inserted scale filters
-    char *resample_lavr_opts;   ///< libavresample options to use for the auto-inserted resample filters
-    char *aresample_swr_opts; ///< swr options to use for the auto-inserted aresample filters, Access ONLY through AVOptions
-
-    /**
-     * Private fields
-     *
-     * The following fields are for internal use only.
-     * Their type, offset, number and semantic can change without notice.
-     */
-
-    AVFilterLink **sink_links;
-    int sink_links_count;
-
-    unsigned disable_auto_convert;
-} AVFilterGraph;
-
-/**
- * Allocate a filter graph.
- */
-AVFilterGraph *avfilter_graph_alloc(void);
-
-/**
- * Get a filter instance with name name from graph.
- *
- * @return the pointer to the found filter instance or NULL if it
- * cannot be found.
- */
-AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name);
-
-/**
- * Add an existing filter instance to a filter graph.
- *
- * @param graphctx  the filter graph
- * @param filter the filter to be added
- */
-int avfilter_graph_add_filter(AVFilterGraph *graphctx, AVFilterContext *filter);
-
-/**
- * Create and add a filter instance into an existing graph.
- * The filter instance is created from the filter filt and inited
- * with the parameters args and opaque.
- *
- * In case of success put in *filt_ctx the pointer to the created
- * filter instance, otherwise set *filt_ctx to NULL.
- *
- * @param name the instance name to give to the created filter instance
- * @param graph_ctx the filter graph
- * @return a negative AVERROR error code in case of failure, a non
- * negative value otherwise
- */
-int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
-                                 const char *name, const char *args, void *opaque,
-                                 AVFilterGraph *graph_ctx);
-
-/**
- * Enable or disable automatic format conversion inside the graph.
- *
- * Note that format conversion can still happen inside explicitly inserted
- * scale and aconvert filters.
- *
- * @param flags  any of the AVFILTER_AUTO_CONVERT_* constants
- */
-void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags);
-
-enum {
-    AVFILTER_AUTO_CONVERT_ALL  =  0, /**< all automatic conversions enabled */
-    AVFILTER_AUTO_CONVERT_NONE = -1, /**< all automatic conversions disabled */
-};
-
-/**
- * Check validity and configure all the links and formats in the graph.
- *
- * @param graphctx the filter graph
- * @param log_ctx context used for logging
- * @return 0 in case of success, a negative AVERROR code otherwise
- */
-int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx);
-
-/**
- * Free a graph, destroy its links, and set *graph to NULL.
- * If *graph is NULL, do nothing.
- */
-void avfilter_graph_free(AVFilterGraph **graph);
-
-/**
- * A linked-list of the inputs/outputs of the filter chain.
- *
- * This is mainly useful for avfilter_graph_parse() / avfilter_graph_parse2(),
- * where it is used to communicate open (unlinked) inputs and outputs from and
- * to the caller.
- * This struct specifies, per each not connected pad contained in the graph, the
- * filter context and the pad index required for establishing a link.
- */
-typedef struct AVFilterInOut {
-    /** unique name for this input/output in the list */
-    char *name;
-
-    /** filter context associated to this input/output */
-    AVFilterContext *filter_ctx;
-
-    /** index of the filt_ctx pad to use for linking */
-    int pad_idx;
-
-    /** next input/input in the list, NULL if this is the last */
-    struct AVFilterInOut *next;
-} AVFilterInOut;
-
-/**
- * Allocate a single AVFilterInOut entry.
- * Must be freed with avfilter_inout_free().
- * @return allocated AVFilterInOut on success, NULL on failure.
- */
-AVFilterInOut *avfilter_inout_alloc(void);
-
-/**
- * Free the supplied list of AVFilterInOut and set *inout to NULL.
- * If *inout is NULL, do nothing.
- */
-void avfilter_inout_free(AVFilterInOut **inout);
-
-/**
- * Add a graph described by a string to a graph.
- *
- * @param graph   the filter graph where to link the parsed graph context
- * @param filters string to be parsed
- * @param inputs  pointer to a linked list to the inputs of the graph, may be NULL.
- *                If non-NULL, *inputs is updated to contain the list of open inputs
- *                after the parsing, should be freed with avfilter_inout_free().
- * @param outputs pointer to a linked list to the outputs of the graph, may be NULL.
- *                If non-NULL, *outputs is updated to contain the list of open outputs
- *                after the parsing, should be freed with avfilter_inout_free().
- * @return non negative on success, a negative AVERROR code on error
- */
-int avfilter_graph_parse(AVFilterGraph *graph, const char *filters,
-                         AVFilterInOut **inputs, AVFilterInOut **outputs,
-                         void *log_ctx);
-
-/**
- * Add a graph described by a string to a graph.
- *
- * @param[in]  graph   the filter graph where to link the parsed graph context
- * @param[in]  filters string to be parsed
- * @param[out] inputs  a linked list of all free (unlinked) inputs of the
- *                     parsed graph will be returned here. It is to be freed
- *                     by the caller using avfilter_inout_free().
- * @param[out] outputs a linked list of all free (unlinked) outputs of the
- *                     parsed graph will be returned here. It is to be freed by the
- *                     caller using avfilter_inout_free().
- * @return zero on success, a negative AVERROR code on error
- *
- * @note the difference between avfilter_graph_parse2() and
- * avfilter_graph_parse() is that in avfilter_graph_parse(), the caller provides
- * the lists of inputs and outputs, which therefore must be known before calling
- * the function. On the other hand, avfilter_graph_parse2() \em returns the
- * inputs and outputs that are left unlinked after parsing the graph and the
- * caller then deals with them. Another difference is that in
- * avfilter_graph_parse(), the inputs parameter describes inputs of the
- * <em>already existing</em> part of the graph; i.e. from the point of view of
- * the newly created part, they are outputs. Similarly the outputs parameter
- * describes outputs of the already existing filters, which are provided as
- * inputs to the parsed filters.
- * avfilter_graph_parse2() takes the opposite approach -- it makes no reference
- * whatsoever to already existing parts of the graph and the inputs parameter
- * will on return contain inputs of the newly parsed part of the graph.
- * Analogously the outputs parameter will contain outputs of the newly created
- * filters.
- */
-int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters,
-                          AVFilterInOut **inputs,
-                          AVFilterInOut **outputs);
-
-
-/**
- * Send a command to one or more filter instances.
- *
- * @param graph  the filter graph
- * @param target the filter(s) to which the command should be sent
- *               "all" sends to all filters
- *               otherwise it can be a filter or filter instance name
- *               which will send the command to all matching filters.
- * @param cmd    the command to sent, for handling simplicity all commands must be alphanumeric only
- * @param arg    the argument for the command
- * @param res    a buffer with size res_size where the filter(s) can return a response.
- *
- * @returns >=0 on success otherwise an error code.
- *              AVERROR(ENOSYS) on unsupported commands
- */
-int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags);
-
-/**
- * Queue a command for one or more filter instances.
- *
- * @param graph  the filter graph
- * @param target the filter(s) to which the command should be sent
- *               "all" sends to all filters
- *               otherwise it can be a filter or filter instance name
- *               which will send the command to all matching filters.
- * @param cmd    the command to sent, for handling simplicity all commands must be alphanummeric only
- * @param arg    the argument for the command
- * @param ts     time at which the command should be sent to the filter
- *
- * @note As this executes commands after this function returns, no return code
- *       from the filter is provided, also AVFILTER_CMD_FLAG_ONE is not supported.
- */
-int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, int flags, double ts);
-
-
-/**
- * Dump a graph into a human-readable string representation.
- *
- * @param graph    the graph to dump
- * @param options  formatting options; currently ignored
- * @return  a string, or NULL in case of memory allocation failure;
- *          the string must be freed using av_free
- */
-char *avfilter_graph_dump(AVFilterGraph *graph, const char *options);
-
-/**
- * Request a frame on the oldest sink link.
- *
- * If the request returns AVERROR_EOF, try the next.
- *
- * Note that this function is not meant to be the sole scheduling mechanism
- * of a filtergraph, only a convenience function to help drain a filtergraph
- * in a balanced way under normal circumstances.
- *
- * Also note that AVERROR_EOF does not mean that frames did not arrive on
- * some of the sinks during the process.
- * When there are multiple sink links, in case the requested link
- * returns an EOF, this may cause a filter to flush pending frames
- * which are sent to another sink link, although unrequested.
- *
- * @return  the return value of ff_request_frame(),
- *          or AVERROR_EOF if all links returned AVERROR_EOF
- */
-int avfilter_graph_request_oldest(AVFilterGraph *graph);
-
 #endif /* AVFILTER_AVFILTERGRAPH_H */
diff --git a/libavfilter/buffer.c b/libavfilter/buffer.c
index 9a3f131..29fedc4 100644
--- a/libavfilter/buffer.c
+++ b/libavfilter/buffer.c
@@ -92,84 +92,13 @@
     return ret;
 }
 
-void ff_free_pool(AVFilterPool *pool)
-{
-    int i;
-
-    av_assert0(pool->refcount > 0);
-
-    for (i = 0; i < POOL_SIZE; i++) {
-        if (pool->pic[i]) {
-            AVFilterBufferRef *picref = pool->pic[i];
-            /* free buffer: picrefs stored in the pool are not
-             * supposed to contain a free callback */
-            av_assert0(!picref->buf->refcount);
-            av_freep(&picref->buf->data[0]);
-            av_freep(&picref->buf);
-
-            av_freep(&picref->audio);
-            av_assert0(!picref->video || !picref->video->qp_table);
-            av_freep(&picref->video);
-            av_freep(&pool->pic[i]);
-            pool->count--;
-        }
-    }
-    pool->draining = 1;
-
-    if (!--pool->refcount) {
-        av_assert0(!pool->count);
-        av_free(pool);
-    }
-}
-
-static void store_in_pool(AVFilterBufferRef *ref)
-{
-    int i;
-    AVFilterPool *pool= ref->buf->priv;
-
-    av_assert0(ref->buf->data[0]);
-    av_assert0(pool->refcount>0);
-
-    if (ref->video)
-        av_freep(&ref->video->qp_table);
-
-    if (pool->count == POOL_SIZE) {
-        AVFilterBufferRef *ref1 = pool->pic[0];
-        av_freep(&ref1->video);
-        av_freep(&ref1->audio);
-        av_freep(&ref1->buf->data[0]);
-        av_freep(&ref1->buf);
-        av_free(ref1);
-        memmove(&pool->pic[0], &pool->pic[1], sizeof(void*)*(POOL_SIZE-1));
-        pool->count--;
-        pool->pic[POOL_SIZE-1] = NULL;
-    }
-
-    for (i = 0; i < POOL_SIZE; i++) {
-        if (!pool->pic[i]) {
-            pool->pic[i] = ref;
-            pool->count++;
-            break;
-        }
-    }
-    if (pool->draining) {
-        ff_free_pool(pool);
-    } else
-        --pool->refcount;
-}
-
 void avfilter_unref_buffer(AVFilterBufferRef *ref)
 {
     if (!ref)
         return;
     av_assert0(ref->buf->refcount > 0);
-    if (!(--ref->buf->refcount)) {
-        if (!ref->buf->free) {
-            store_in_pool(ref);
-            return;
-        }
+    if (!(--ref->buf->refcount))
         ref->buf->free(ref->buf);
-    }
     if (ref->extended_data != ref->data)
         av_freep(&ref->extended_data);
     if (ref->video)
@@ -186,6 +115,36 @@
     *ref = NULL;
 }
 
+int avfilter_copy_frame_props(AVFilterBufferRef *dst, const AVFrame *src)
+{
+    dst->pts    = src->pts;
+    dst->pos    = av_frame_get_pkt_pos(src);
+    dst->format = src->format;
+
+    av_dict_free(&dst->metadata);
+    av_dict_copy(&dst->metadata, av_frame_get_metadata(src), 0);
+
+    switch (dst->type) {
+    case AVMEDIA_TYPE_VIDEO:
+        dst->video->w                   = src->width;
+        dst->video->h                   = src->height;
+        dst->video->sample_aspect_ratio = src->sample_aspect_ratio;
+        dst->video->interlaced          = src->interlaced_frame;
+        dst->video->top_field_first     = src->top_field_first;
+        dst->video->key_frame           = src->key_frame;
+        dst->video->pict_type           = src->pict_type;
+        break;
+    case AVMEDIA_TYPE_AUDIO:
+        dst->audio->sample_rate         = src->sample_rate;
+        dst->audio->channel_layout      = src->channel_layout;
+        break;
+    default:
+        return AVERROR(EINVAL);
+    }
+
+    return 0;
+}
+
 void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
 {
     // copy common properties
@@ -206,40 +165,3 @@
     av_dict_free(&dst->metadata);
     av_dict_copy(&dst->metadata, src->metadata, 0);
 }
-
-AVFilterBufferRef *ff_copy_buffer_ref(AVFilterLink *outlink,
-                                      AVFilterBufferRef *ref)
-{
-    AVFilterBufferRef *buf;
-    int channels;
-
-    switch (outlink->type) {
-
-    case AVMEDIA_TYPE_VIDEO:
-        buf = ff_get_video_buffer(outlink, AV_PERM_WRITE,
-                                  ref->video->w, ref->video->h);
-        if(!buf)
-            return NULL;
-        av_image_copy(buf->data, buf->linesize,
-                      (void*)ref->data, ref->linesize,
-                      ref->format, ref->video->w, ref->video->h);
-        break;
-
-    case AVMEDIA_TYPE_AUDIO:
-        buf = ff_get_audio_buffer(outlink, AV_PERM_WRITE,
-                                        ref->audio->nb_samples);
-        if(!buf)
-            return NULL;
-        channels = ref->audio->channels;
-        av_samples_copy(buf->extended_data, ref->buf->extended_data,
-                        0, 0, ref->audio->nb_samples,
-                        channels,
-                        ref->format);
-        break;
-
-    default:
-        return NULL;
-    }
-    avfilter_copy_buffer_ref_props(buf, ref);
-    return buf;
-}
diff --git a/libavfilter/bufferqueue.h b/libavfilter/bufferqueue.h
index 34c4c0f..adbc0fd 100644
--- a/libavfilter/bufferqueue.h
+++ b/libavfilter/bufferqueue.h
@@ -23,7 +23,7 @@
 #define AVFILTER_BUFFERQUEUE_H
 
 /**
- * FFBufQueue: simple AVFilterBufferRef queue API
+ * FFBufQueue: simple AVFrame queue API
  *
  * Note: this API is not thread-safe. Concurrent access to the same queue
  * must be protected by a mutex or any synchronization mechanism.
@@ -47,7 +47,7 @@
  * Structure holding the queue
  */
 struct FFBufQueue {
-    AVFilterBufferRef *queue[FF_BUFQUEUE_SIZE];
+    AVFrame *queue[FF_BUFQUEUE_SIZE];
     unsigned short head;
     unsigned short available; /**< number of available buffers */
 };
@@ -69,11 +69,11 @@
  * (and unrefed) with a warning before adding the new buffer.
  */
 static inline void ff_bufqueue_add(void *log, struct FFBufQueue *queue,
-                                   AVFilterBufferRef *buf)
+                                   AVFrame *buf)
 {
     if (ff_bufqueue_is_full(queue)) {
         av_log(log, AV_LOG_WARNING, "Buffer queue overflow, dropping.\n");
-        avfilter_unref_buffer(BUCKET(--queue->available));
+        av_frame_free(&BUCKET(--queue->available));
     }
     BUCKET(queue->available++) = buf;
 }
@@ -84,8 +84,8 @@
  * Buffer with index 0 is the first buffer in the queue.
  * Return NULL if the queue has not enough buffers.
  */
-static inline AVFilterBufferRef *ff_bufqueue_peek(struct FFBufQueue *queue,
-                                                  unsigned index)
+static inline AVFrame *ff_bufqueue_peek(struct FFBufQueue *queue,
+                                        unsigned index)
 {
     return index < queue->available ? BUCKET(index) : NULL;
 }
@@ -95,9 +95,9 @@
  *
  * Do not use on an empty queue.
  */
-static inline AVFilterBufferRef *ff_bufqueue_get(struct FFBufQueue *queue)
+static inline AVFrame *ff_bufqueue_get(struct FFBufQueue *queue)
 {
-    AVFilterBufferRef *ret = queue->queue[queue->head];
+    AVFrame *ret = queue->queue[queue->head];
     av_assert0(queue->available);
     queue->available--;
     queue->queue[queue->head] = NULL;
@@ -110,8 +110,10 @@
  */
 static inline void ff_bufqueue_discard_all(struct FFBufQueue *queue)
 {
-    while (queue->available)
-        avfilter_unref_buffer(ff_bufqueue_get(queue));
+    while (queue->available) {
+        AVFrame *buf = ff_bufqueue_get(queue);
+        av_frame_free(&buf);
+    }
 }
 
 #undef BUCKET
diff --git a/libavfilter/buffersink.c b/libavfilter/buffersink.c
index 282be30..234027d 100644
--- a/libavfilter/buffersink.c
+++ b/libavfilter/buffersink.c
@@ -28,6 +28,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/opt.h"
 
 #include "audio.h"
 #include "avfilter.h"
@@ -35,131 +36,547 @@
 #include "internal.h"
 
 typedef struct {
-    AVFilterBufferRef *cur_buf;  ///< last buffer delivered on the sink
+    const AVClass *class;
+    AVFifoBuffer *fifo;                      ///< FIFO buffer of video frame references
+    unsigned warning_limit;
+
+    /* only used for video */
+    enum AVPixelFormat *pixel_fmts;           ///< list of accepted pixel formats, must be terminated with -1
+    int pixel_fmts_size;
+
+    /* only used for audio */
+    enum AVSampleFormat *sample_fmts;       ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
+    int sample_fmts_size;
+    int64_t *channel_layouts;               ///< list of accepted channel layouts, terminated by -1
+    int channel_layouts_size;
+    int *channel_counts;                    ///< list of accepted channel counts, terminated by -1
+    int channel_counts_size;
+    int all_channel_counts;
+    int *sample_rates;                      ///< list of accepted sample rates, terminated by -1
+    int sample_rates_size;
+
+    /* only used for compat API */
     AVAudioFifo  *audio_fifo;    ///< FIFO for audio samples
     int64_t next_pts;            ///< interpolating audio pts
 } BufferSinkContext;
 
+#define NB_ITEMS(list) (list ## _size / sizeof(*list))
+
 static av_cold void uninit(AVFilterContext *ctx)
 {
     BufferSinkContext *sink = ctx->priv;
+    AVFrame *frame;
 
     if (sink->audio_fifo)
         av_audio_fifo_free(sink->audio_fifo);
+
+    if (sink->fifo) {
+        while (av_fifo_size(sink->fifo) >= sizeof(AVFilterBufferRef *)) {
+            av_fifo_generic_read(sink->fifo, &frame, sizeof(frame), NULL);
+            av_frame_free(&frame);
+        }
+        av_fifo_free(sink->fifo);
+        sink->fifo = NULL;
+    }
 }
 
-static int filter_frame(AVFilterLink *link, AVFilterBufferRef *buf)
+static int add_buffer_ref(AVFilterContext *ctx, AVFrame *ref)
 {
-    BufferSinkContext *s = link->dst->priv;
+    BufferSinkContext *buf = ctx->priv;
 
-//     av_assert0(!s->cur_buf);
-    s->cur_buf    = buf;
+    if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
+        /* realloc fifo size */
+        if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
+            av_log(ctx, AV_LOG_ERROR,
+                   "Cannot buffer more frames. Consume some available frames "
+                   "before adding new ones.\n");
+            return AVERROR(ENOMEM);
+        }
+    }
 
+    /* cache frame */
+    av_fifo_generic_write(buf->fifo, &ref, sizeof(AVFilterBufferRef *), NULL);
     return 0;
 }
 
-int ff_buffersink_read_compat(AVFilterContext *ctx, AVFilterBufferRef **buf)
+static int filter_frame(AVFilterLink *link, AVFrame *frame)
 {
-    BufferSinkContext *s    = ctx->priv;
-    AVFilterLink      *link = ctx->inputs[0];
+    AVFilterContext *ctx = link->dst;
+    BufferSinkContext *buf = link->dst->priv;
     int ret;
 
-    if (!buf)
-        return ff_poll_frame(ctx->inputs[0]);
-
-    if ((ret = ff_request_frame(link)) < 0)
+    if ((ret = add_buffer_ref(ctx, frame)) < 0)
         return ret;
+    if (buf->warning_limit &&
+        av_fifo_size(buf->fifo) / sizeof(AVFilterBufferRef *) >= buf->warning_limit) {
+        av_log(ctx, AV_LOG_WARNING,
+               "%d buffers queued in %s, something may be wrong.\n",
+               buf->warning_limit,
+               (char *)av_x_if_null(ctx->name, ctx->filter->name));
+        buf->warning_limit *= 10;
+    }
+    return 0;
+}
 
-    if (!s->cur_buf)
+int av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame)
+{
+    return av_buffersink_get_frame_flags(ctx, frame, 0);
+}
+
+int attribute_align_arg av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
+{
+    BufferSinkContext *buf = ctx->priv;
+    AVFilterLink *inlink = ctx->inputs[0];
+    int ret;
+    AVFrame *cur_frame;
+
+    /* no picref available, fetch it from the filterchain */
+    if (!av_fifo_size(buf->fifo)) {
+        if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
+            return AVERROR(EAGAIN);
+        if ((ret = ff_request_frame(inlink)) < 0)
+            return ret;
+    }
+
+    if (!av_fifo_size(buf->fifo))
         return AVERROR(EINVAL);
 
-    *buf       = s->cur_buf;
-    s->cur_buf = NULL;
+    if (flags & AV_BUFFERSINK_FLAG_PEEK) {
+        cur_frame = *((AVFrame **)av_fifo_peek2(buf->fifo, 0));
+        if ((ret = av_frame_ref(frame, cur_frame)) < 0)
+            return ret;
+    } else {
+        av_fifo_generic_read(buf->fifo, &cur_frame, sizeof(cur_frame), NULL);
+        av_frame_move_ref(frame, cur_frame);
+        av_frame_free(&cur_frame);
+    }
 
     return 0;
 }
 
-static int read_from_fifo(AVFilterContext *ctx, AVFilterBufferRef **pbuf,
+static int read_from_fifo(AVFilterContext *ctx, AVFrame *frame,
                           int nb_samples)
 {
     BufferSinkContext *s = ctx->priv;
     AVFilterLink   *link = ctx->inputs[0];
-    AVFilterBufferRef *buf;
+    AVFrame *tmp;
 
-    if (!(buf = ff_get_audio_buffer(link, AV_PERM_WRITE, nb_samples)))
+    if (!(tmp = ff_get_audio_buffer(link, nb_samples)))
         return AVERROR(ENOMEM);
-    av_audio_fifo_read(s->audio_fifo, (void**)buf->extended_data, nb_samples);
+    av_audio_fifo_read(s->audio_fifo, (void**)tmp->extended_data, nb_samples);
 
-    buf->pts = s->next_pts;
+    tmp->pts = s->next_pts;
     s->next_pts += av_rescale_q(nb_samples, (AVRational){1, link->sample_rate},
                                 link->time_base);
 
-    *pbuf = buf;
+    av_frame_move_ref(frame, tmp);
+    av_frame_free(&tmp);
+
     return 0;
 
 }
 
-int ff_buffersink_read_samples_compat(AVFilterContext *ctx, AVFilterBufferRef **pbuf,
-                                      int nb_samples)
+int attribute_align_arg av_buffersink_get_samples(AVFilterContext *ctx, AVFrame *frame, int nb_samples)
 {
     BufferSinkContext *s = ctx->priv;
     AVFilterLink   *link = ctx->inputs[0];
+    AVFrame *cur_frame;
     int ret = 0;
 
     if (!s->audio_fifo) {
-        int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
+        int nb_channels = link->channels;
         if (!(s->audio_fifo = av_audio_fifo_alloc(link->format, nb_channels, nb_samples)))
             return AVERROR(ENOMEM);
     }
 
     while (ret >= 0) {
-        AVFilterBufferRef *buf;
-
         if (av_audio_fifo_size(s->audio_fifo) >= nb_samples)
-            return read_from_fifo(ctx, pbuf, nb_samples);
+            return read_from_fifo(ctx, frame, nb_samples);
 
-        ret = av_buffersink_read(ctx, &buf);
-        if (ret == AVERROR_EOF && av_audio_fifo_size(s->audio_fifo))
-            return read_from_fifo(ctx, pbuf, av_audio_fifo_size(s->audio_fifo));
-        else if (ret < 0)
+        if (!(cur_frame = av_frame_alloc()))
+            return AVERROR(ENOMEM);
+        ret = av_buffersink_get_frame_flags(ctx, cur_frame, 0);
+        if (ret == AVERROR_EOF && av_audio_fifo_size(s->audio_fifo)) {
+            av_frame_free(&cur_frame);
+            return read_from_fifo(ctx, frame, av_audio_fifo_size(s->audio_fifo));
+        } else if (ret < 0) {
+            av_frame_free(&cur_frame);
             return ret;
+        }
 
-        if (buf->pts != AV_NOPTS_VALUE) {
-            s->next_pts = buf->pts -
+        if (cur_frame->pts != AV_NOPTS_VALUE) {
+            s->next_pts = cur_frame->pts -
                           av_rescale_q(av_audio_fifo_size(s->audio_fifo),
                                        (AVRational){ 1, link->sample_rate },
                                        link->time_base);
         }
 
-        ret = av_audio_fifo_write(s->audio_fifo, (void**)buf->extended_data,
-                                  buf->audio->nb_samples);
-        avfilter_unref_buffer(buf);
+        ret = av_audio_fifo_write(s->audio_fifo, (void**)cur_frame->extended_data,
+                                  cur_frame->nb_samples);
+        av_frame_free(&cur_frame);
     }
 
     return ret;
+
 }
 
+AVBufferSinkParams *av_buffersink_params_alloc(void)
+{
+    static const int pixel_fmts[] = { AV_PIX_FMT_NONE };
+    AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
+    if (!params)
+        return NULL;
+
+    params->pixel_fmts = pixel_fmts;
+    return params;
+}
+
+AVABufferSinkParams *av_abuffersink_params_alloc(void)
+{
+    AVABufferSinkParams *params = av_mallocz(sizeof(AVABufferSinkParams));
+
+    if (!params)
+        return NULL;
+    return params;
+}
+
+#define FIFO_INIT_SIZE 8
+
+static av_cold int common_init(AVFilterContext *ctx)
+{
+    BufferSinkContext *buf = ctx->priv;
+
+    buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
+    if (!buf->fifo) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
+        return AVERROR(ENOMEM);
+    }
+    buf->warning_limit = 100;
+    return 0;
+}
+
+void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
+{
+    AVFilterLink *inlink = ctx->inputs[0];
+
+    inlink->min_samples = inlink->max_samples =
+    inlink->partial_buf_size = frame_size;
+}
+
+#if FF_API_AVFILTERBUFFER
+static void compat_free_buffer(AVFilterBuffer *buf)
+{
+    AVFrame *frame = buf->priv;
+    av_frame_free(&frame);
+    av_free(buf);
+}
+
+static int attribute_align_arg compat_read(AVFilterContext *ctx, AVFilterBufferRef **pbuf, int nb_samples, int flags)
+{
+    AVFilterBufferRef *buf;
+    AVFrame *frame;
+    int ret;
+
+    if (!pbuf)
+        return ff_poll_frame(ctx->inputs[0]);
+
+    frame = av_frame_alloc();
+    if (!frame)
+        return AVERROR(ENOMEM);
+
+    if (!nb_samples)
+        ret = av_buffersink_get_frame_flags(ctx, frame, flags);
+    else
+        ret = av_buffersink_get_samples(ctx, frame, nb_samples);
+
+    if (ret < 0)
+        goto fail;
+
+    AV_NOWARN_DEPRECATED(
+    if (ctx->inputs[0]->type == AVMEDIA_TYPE_VIDEO) {
+        buf = avfilter_get_video_buffer_ref_from_arrays(frame->data, frame->linesize,
+                                                        AV_PERM_READ,
+                                                        frame->width, frame->height,
+                                                        frame->format);
+    } else {
+        buf = avfilter_get_audio_buffer_ref_from_arrays(frame->extended_data,
+                                                        frame->linesize[0], AV_PERM_READ,
+                                                        frame->nb_samples,
+                                                        frame->format,
+                                                        frame->channel_layout);
+    }
+    if (!buf) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+
+    avfilter_copy_frame_props(buf, frame);
+    )
+
+    buf->buf->priv = frame;
+    buf->buf->free = compat_free_buffer;
+
+    *pbuf = buf;
+
+    return 0;
+fail:
+    av_frame_free(&frame);
+    return ret;
+}
+
+int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
+{
+    return compat_read(ctx, buf, 0, 0);
+}
+
+int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
+                               int nb_samples)
+{
+    return compat_read(ctx, buf, nb_samples, 0);
+}
+
+int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
+                                  AVFilterBufferRef **bufref, int flags)
+{
+    *bufref = NULL;
+
+    av_assert0(    !strcmp(ctx->filter->name, "buffersink")
+                || !strcmp(ctx->filter->name, "abuffersink")
+                || !strcmp(ctx->filter->name, "ffbuffersink")
+                || !strcmp(ctx->filter->name, "ffabuffersink"));
+
+    return compat_read(ctx, bufref, 0, flags);
+}
+#endif
+
+AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
+{
+    av_assert0(   !strcmp(ctx->filter->name, "buffersink")
+               || !strcmp(ctx->filter->name, "ffbuffersink"));
+
+    return ctx->inputs[0]->frame_rate;
+}
+
+int attribute_align_arg av_buffersink_poll_frame(AVFilterContext *ctx)
+{
+    BufferSinkContext *buf = ctx->priv;
+    AVFilterLink *inlink = ctx->inputs[0];
+
+    av_assert0(   !strcmp(ctx->filter->name, "buffersink")
+               || !strcmp(ctx->filter->name, "abuffersink")
+               || !strcmp(ctx->filter->name, "ffbuffersink")
+               || !strcmp(ctx->filter->name, "ffabuffersink"));
+
+    return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
+}
+
+static av_cold int vsink_init(AVFilterContext *ctx, void *opaque)
+{
+    BufferSinkContext *buf = ctx->priv;
+    AVBufferSinkParams *params = opaque;
+    int ret;
+
+    if (params) {
+        if ((ret = av_opt_set_int_list(buf, "pix_fmts", params->pixel_fmts, AV_PIX_FMT_NONE, 0)) < 0)
+            return ret;
+    }
+
+    return common_init(ctx);
+}
+
+#define CHECK_LIST_SIZE(field) \
+        if (buf->field ## _size % sizeof(*buf->field)) { \
+            av_log(ctx, AV_LOG_ERROR, "Invalid size for " #field ": %d, " \
+                   "should be multiple of %d\n", \
+                   buf->field ## _size, (int)sizeof(*buf->field)); \
+            return AVERROR(EINVAL); \
+        }
+static int vsink_query_formats(AVFilterContext *ctx)
+{
+    BufferSinkContext *buf = ctx->priv;
+    AVFilterFormats *formats = NULL;
+    unsigned i;
+    int ret;
+
+    CHECK_LIST_SIZE(pixel_fmts)
+    if (buf->pixel_fmts_size) {
+        for (i = 0; i < NB_ITEMS(buf->pixel_fmts); i++)
+            if ((ret = ff_add_format(&formats, buf->pixel_fmts[i])) < 0) {
+                ff_formats_unref(&formats);
+                return ret;
+            }
+        ff_set_common_formats(ctx, formats);
+    } else {
+        ff_default_query_formats(ctx);
+    }
+
+    return 0;
+}
+
+static av_cold int asink_init(AVFilterContext *ctx, void *opaque)
+{
+    BufferSinkContext *buf = ctx->priv;
+    AVABufferSinkParams *params = opaque;
+    int ret;
+
+    if (params) {
+        if ((ret = av_opt_set_int_list(buf, "sample_fmts",     params->sample_fmts,  AV_SAMPLE_FMT_NONE, 0)) < 0 ||
+            (ret = av_opt_set_int_list(buf, "sample_rates",    params->sample_rates,    -1, 0)) < 0 ||
+            (ret = av_opt_set_int_list(buf, "channel_layouts", params->channel_layouts, -1, 0)) < 0 ||
+            (ret = av_opt_set_int_list(buf, "channel_counts",  params->channel_counts,  -1, 0)) < 0 ||
+            (ret = av_opt_set_int(buf, "all_channel_counts", params->all_channel_counts, 0)) < 0)
+            return ret;
+    }
+    return common_init(ctx);
+}
+
+static int asink_query_formats(AVFilterContext *ctx)
+{
+    BufferSinkContext *buf = ctx->priv;
+    AVFilterFormats *formats = NULL;
+    AVFilterChannelLayouts *layouts = NULL;
+    unsigned i;
+    int ret;
+
+    CHECK_LIST_SIZE(sample_fmts)
+    CHECK_LIST_SIZE(sample_rates)
+    CHECK_LIST_SIZE(channel_layouts)
+    CHECK_LIST_SIZE(channel_counts)
+
+    if (buf->sample_fmts_size) {
+        for (i = 0; i < NB_ITEMS(buf->sample_fmts); i++)
+            if ((ret = ff_add_format(&formats, buf->sample_fmts[i])) < 0) {
+                ff_formats_unref(&formats);
+                return ret;
+            }
+        ff_set_common_formats(ctx, formats);
+    }
+
+    if (buf->channel_layouts_size || buf->channel_counts_size ||
+        buf->all_channel_counts) {
+        for (i = 0; i < NB_ITEMS(buf->channel_layouts); i++)
+            if ((ret = ff_add_channel_layout(&layouts, buf->channel_layouts[i])) < 0) {
+                ff_channel_layouts_unref(&layouts);
+                return ret;
+            }
+        for (i = 0; i < NB_ITEMS(buf->channel_counts); i++)
+            if ((ret = ff_add_channel_layout(&layouts, FF_COUNT2LAYOUT(buf->channel_counts[i]))) < 0) {
+                ff_channel_layouts_unref(&layouts);
+                return ret;
+            }
+        if (buf->all_channel_counts) {
+            if (layouts)
+                av_log(ctx, AV_LOG_WARNING,
+                       "Conflicting all_channel_counts and list in options\n");
+            else if (!(layouts = ff_all_channel_counts()))
+                return AVERROR(ENOMEM);
+        }
+        ff_set_common_channel_layouts(ctx, layouts);
+    }
+
+    if (buf->sample_rates_size) {
+        formats = NULL;
+        for (i = 0; i < NB_ITEMS(buf->sample_rates); i++)
+            if ((ret = ff_add_format(&formats, buf->sample_rates[i])) < 0) {
+                ff_formats_unref(&formats);
+                return ret;
+            }
+        ff_set_common_samplerates(ctx, formats);
+    }
+
+    return 0;
+}
+
+#define OFFSET(x) offsetof(BufferSinkContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption buffersink_options[] = {
+    { "pix_fmts", "set the supported pixel formats", OFFSET(pixel_fmts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
+    { NULL },
+};
+#undef FLAGS
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption abuffersink_options[] = {
+    { "sample_fmts",     "set the supported sample formats",  OFFSET(sample_fmts),     AV_OPT_TYPE_BINARY, .flags = FLAGS },
+    { "sample_rates",    "set the supported sample rates",    OFFSET(sample_rates),    AV_OPT_TYPE_BINARY, .flags = FLAGS },
+    { "channel_layouts", "set the supported channel layouts", OFFSET(channel_layouts), AV_OPT_TYPE_BINARY, .flags = FLAGS },
+    { "channel_counts",  "set the supported channel counts",  OFFSET(channel_counts),  AV_OPT_TYPE_BINARY, .flags = FLAGS },
+    { "all_channel_counts", "accept all channel counts", OFFSET(all_channel_counts), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
+    { NULL },
+};
+#undef FLAGS
+
+AVFILTER_DEFINE_CLASS(buffersink);
+AVFILTER_DEFINE_CLASS(abuffersink);
+
+#if FF_API_AVFILTERBUFFER
+
+#define ffbuffersink_options buffersink_options
+#define ffabuffersink_options abuffersink_options
+AVFILTER_DEFINE_CLASS(ffbuffersink);
+AVFILTER_DEFINE_CLASS(ffabuffersink);
+
+static const AVFilterPad ffbuffersink_inputs[] = {
+    {
+        .name      = "default",
+        .type      = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+    },
+    { NULL },
+};
+
+AVFilter avfilter_vsink_ffbuffersink = {
+    .name      = "ffbuffersink",
+    .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
+    .priv_size = sizeof(BufferSinkContext),
+    .priv_class = &ffbuffersink_class,
+    .init_opaque = vsink_init,
+    .uninit    = uninit,
+
+    .query_formats = vsink_query_formats,
+    .inputs        = ffbuffersink_inputs,
+    .outputs       = NULL,
+};
+
+static const AVFilterPad ffabuffersink_inputs[] = {
+    {
+        .name           = "default",
+        .type           = AVMEDIA_TYPE_AUDIO,
+        .filter_frame   = filter_frame,
+    },
+    { NULL },
+};
+
+AVFilter avfilter_asink_ffabuffersink = {
+    .name      = "ffabuffersink",
+    .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
+    .init_opaque = asink_init,
+    .uninit    = uninit,
+    .priv_size = sizeof(BufferSinkContext),
+    .priv_class = &ffabuffersink_class,
+    .query_formats = asink_query_formats,
+    .inputs        = ffabuffersink_inputs,
+    .outputs       = NULL,
+};
+#endif /* FF_API_AVFILTERBUFFER */
+
 static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
     {
         .name        = "default",
         .type        = AVMEDIA_TYPE_VIDEO,
         .filter_frame = filter_frame,
-        .min_perms   = AV_PERM_READ,
-        .needs_fifo  = 1
     },
     { NULL }
 };
 
 AVFilter avfilter_vsink_buffer = {
-#if AV_HAVE_INCOMPATIBLE_FORK_ABI
     .name      = "buffersink",
-#else
-    .name      = "buffersink_old",
-#endif
     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
     .priv_size = sizeof(BufferSinkContext),
+    .priv_class = &buffersink_class,
+    .init_opaque = vsink_init,
     .uninit    = uninit,
 
+    .query_formats = vsink_query_formats,
     .inputs    = avfilter_vsink_buffer_inputs,
     .outputs   = NULL,
 };
@@ -169,22 +586,19 @@
         .name           = "default",
         .type           = AVMEDIA_TYPE_AUDIO,
         .filter_frame   = filter_frame,
-        .min_perms      = AV_PERM_READ,
-        .needs_fifo     = 1
     },
     { NULL }
 };
 
 AVFilter avfilter_asink_abuffer = {
-#if AV_HAVE_INCOMPATIBLE_FORK_ABI
     .name      = "abuffersink",
-#else
-    .name      = "abuffersink_old",
-#endif
     .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
+    .priv_class = &abuffersink_class,
     .priv_size = sizeof(BufferSinkContext),
+    .init_opaque = asink_init,
     .uninit    = uninit,
 
+    .query_formats = asink_query_formats,
     .inputs    = avfilter_asink_abuffer_inputs,
     .outputs   = NULL,
 };
diff --git a/libavfilter/buffersink.h b/libavfilter/buffersink.h
index 6f8ac5c..ce96d08 100644
--- a/libavfilter/buffersink.h
+++ b/libavfilter/buffersink.h
@@ -26,6 +26,89 @@
 
 #include "avfilter.h"
 
+#if FF_API_AVFILTERBUFFER
+/**
+ * Get an audio/video buffer data from buffer_sink and put it in bufref.
+ *
+ * This function works with both audio and video buffer sinks.
+ *
+ * @param buffer_sink pointer to a buffersink or abuffersink context
+ * @param flags a combination of AV_BUFFERSINK_FLAG_* flags
+ * @return >= 0 in case of success, a negative AVERROR code in case of
+ * failure
+ */
+attribute_deprecated
+int av_buffersink_get_buffer_ref(AVFilterContext *buffer_sink,
+                                 AVFilterBufferRef **bufref, int flags);
+
+/**
+ * Get the number of immediately available frames.
+ */
+attribute_deprecated
+int av_buffersink_poll_frame(AVFilterContext *ctx);
+
+/**
+ * Get a buffer with filtered data from sink and put it in buf.
+ *
+ * @param ctx pointer to a context of a buffersink or abuffersink AVFilter.
+ * @param buf pointer to the buffer will be written here if buf is non-NULL. buf
+ *            must be freed by the caller using avfilter_unref_buffer().
+ *            Buf may also be NULL to query whether a buffer is ready to be
+ *            output.
+ *
+ * @return >= 0 in case of success, a negative AVERROR code in case of
+ *         failure.
+ */
+attribute_deprecated
+int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf);
+
+/**
+ * Same as av_buffersink_read, but with the ability to specify the number of
+ * samples read. This function is less efficient than av_buffersink_read(),
+ * because it copies the data around.
+ *
+ * @param ctx pointer to a context of the abuffersink AVFilter.
+ * @param buf pointer to the buffer will be written here if buf is non-NULL. buf
+ *            must be freed by the caller using avfilter_unref_buffer(). buf
+ *            will contain exactly nb_samples audio samples, except at the end
+ *            of stream, when it can contain less than nb_samples.
+ *            Buf may also be NULL to query whether a buffer is ready to be
+ *            output.
+ *
+ * @warning do not mix this function with av_buffersink_read(). Use only one or
+ * the other with a single sink, not both.
+ */
+attribute_deprecated
+int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
+                               int nb_samples);
+#endif
+
+/**
+ * Get a frame with filtered data from sink and put it in frame.
+ *
+ * @param ctx    pointer to a buffersink or abuffersink filter context.
+ * @param frame  pointer to an allocated frame that will be filled with data.
+ *               The data must be freed using av_frame_unref() / av_frame_free()
+ * @param flags  a combination of AV_BUFFERSINK_FLAG_* flags
+ *
+ * @return  >= 0 in for success, a negative AVERROR code for failure.
+ */
+int av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags);
+
+/**
+ * Tell av_buffersink_get_buffer_ref() to read video/samples buffer
+ * reference, but not remove it from the buffer. This is useful if you
+ * need only to read a video/samples buffer, without to fetch it.
+ */
+#define AV_BUFFERSINK_FLAG_PEEK 1
+
+/**
+ * Tell av_buffersink_get_buffer_ref() not to request a frame from its input.
+ * If a frame is already buffered, it is read (and removed from the buffer),
+ * but if no frame is present, return AVERROR(EAGAIN).
+ */
+#define AV_BUFFERSINK_FLAG_NO_REQUEST 2
+
 /**
  * Struct to use for initializing a buffersink context.
  */
@@ -68,83 +151,36 @@
 void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size);
 
 /**
- * Tell av_buffersink_get_buffer_ref() to read video/samples buffer
- * reference, but not remove it from the buffer. This is useful if you
- * need only to read a video/samples buffer, without to fetch it.
- */
-#define AV_BUFFERSINK_FLAG_PEEK 1
-
-/**
- * Tell av_buffersink_get_buffer_ref() not to request a frame from its input.
- * If a frame is already buffered, it is read (and removed from the buffer),
- * but if no frame is present, return AVERROR(EAGAIN).
- */
-#define AV_BUFFERSINK_FLAG_NO_REQUEST 2
-
-/**
- * Get an audio/video buffer data from buffer_sink and put it in bufref.
- *
- * This function works with both audio and video buffer sinks.
- *
- * @param buffer_sink pointer to a buffersink or abuffersink context
- * @param flags a combination of AV_BUFFERSINK_FLAG_* flags
- * @return >= 0 in case of success, a negative AVERROR code in case of
- * failure
- */
-int av_buffersink_get_buffer_ref(AVFilterContext *buffer_sink,
-                                 AVFilterBufferRef **bufref, int flags);
-
-
-/**
- * Get the number of immediately available frames.
- */
-int av_buffersink_poll_frame(AVFilterContext *ctx);
-
-/**
  * Get the frame rate of the input.
  */
 AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx);
 
 /**
- * @defgroup libav_api Libav API
- * @{
- */
-
-/**
- * Get a buffer with filtered data from sink and put it in buf.
+ * Get a frame with filtered data from sink and put it in frame.
  *
  * @param ctx pointer to a context of a buffersink or abuffersink AVFilter.
- * @param buf pointer to the buffer will be written here if buf is non-NULL. buf
- *            must be freed by the caller using avfilter_unref_buffer().
- *            Buf may also be NULL to query whether a buffer is ready to be
- *            output.
+ * @param frame pointer to an allocated frame that will be filled with data.
+ *              The data must be freed using av_frame_unref() / av_frame_free()
  *
  * @return >= 0 in case of success, a negative AVERROR code in case of
  *         failure.
  */
-int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf);
+int av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame);
 
 /**
- * Same as av_buffersink_read, but with the ability to specify the number of
- * samples read. This function is less efficient than av_buffersink_read(),
- * because it copies the data around.
+ * Same as av_buffersink_get_frame(), but with the ability to specify the number
+ * of samples read. This function is less efficient than
+ * av_buffersink_get_frame(), because it copies the data around.
  *
  * @param ctx pointer to a context of the abuffersink AVFilter.
- * @param buf pointer to the buffer will be written here if buf is non-NULL. buf
- *            must be freed by the caller using avfilter_unref_buffer(). buf
- *            will contain exactly nb_samples audio samples, except at the end
- *            of stream, when it can contain less than nb_samples.
- *            Buf may also be NULL to query whether a buffer is ready to be
- *            output.
+ * @param frame pointer to an allocated frame that will be filled with data.
+ *              The data must be freed using av_frame_unref() / av_frame_free()
+ *              frame will contain exactly nb_samples audio samples, except at
+ *              the end of stream, when it can contain less than nb_samples.
  *
- * @warning do not mix this function with av_buffersink_read(). Use only one or
+ * @warning do not mix this function with av_buffersink_get_frame(). Use only one or
  * the other with a single sink, not both.
  */
-int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
-                               int nb_samples);
-
-/**
- * @}
- */
+int av_buffersink_get_samples(AVFilterContext *ctx, AVFrame *frame, int nb_samples);
 
 #endif /* AVFILTER_BUFFERSINK_H */
diff --git a/libavfilter/buffersrc.c b/libavfilter/buffersrc.c
index d150357..5d834b5 100644
--- a/libavfilter/buffersrc.c
+++ b/libavfilter/buffersrc.c
@@ -23,9 +23,12 @@
  * memory buffer source filter
  */
 
+#include <float.h>
+
 #include "libavutil/channel_layout.h"
 #include "libavutil/common.h"
 #include "libavutil/fifo.h"
+#include "libavutil/frame.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/opt.h"
 #include "libavutil/samplefmt.h"
@@ -48,6 +51,7 @@
     /* video only */
     int               w, h;
     enum AVPixelFormat  pix_fmt;
+    char               *pix_fmt_str;
     AVRational        pixel_aspect;
     char              *sws_param;
 
@@ -67,164 +71,237 @@
         av_log(s, AV_LOG_INFO, "Changing frame properties on the fly is not supported by all filters.\n");\
     }
 
-#define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, format)\
+#define CHECK_AUDIO_PARAM_CHANGE(s, c, srate, ch_layout, ch_count, format)\
     if (c->sample_fmt != format || c->sample_rate != srate ||\
-        c->channel_layout != ch_layout) {\
+        c->channel_layout != ch_layout || c->channels != ch_count) {\
         av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
         return AVERROR(EINVAL);\
     }
 
-int av_buffersrc_add_frame(AVFilterContext *buffer_src,
-                           const AVFrame *frame, int flags)
+int av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame)
 {
-    AVFilterBufferRef *picref;
-    int ret;
+    return av_buffersrc_add_frame_flags(ctx, (AVFrame *)frame,
+                                        AV_BUFFERSRC_FLAG_KEEP_REF);
+}
 
-    if (!frame) /* NULL for EOF */
-        return av_buffersrc_add_ref(buffer_src, NULL, flags);
+int av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame)
+{
+    return av_buffersrc_add_frame_flags(ctx, frame, 0);
+}
 
-    picref = avfilter_get_buffer_ref_from_frame(buffer_src->outputs[0]->type,
-                                                frame, AV_PERM_WRITE);
-    if (!picref)
+static int av_buffersrc_add_frame_internal(AVFilterContext *ctx,
+                                           AVFrame *frame, int flags);
+
+int av_buffersrc_add_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
+{
+    AVFrame *copy = NULL;
+    int ret = 0;
+
+    if (frame && frame->channel_layout &&
+        av_get_channel_layout_nb_channels(frame->channel_layout) != av_frame_get_channels(frame)) {
+        av_log(0, AV_LOG_ERROR, "Layout indicates a different number of channels than actually present\n");
+        return AVERROR(EINVAL);
+    }
+
+    if (!(flags & AV_BUFFERSRC_FLAG_KEEP_REF) || !frame)
+        return av_buffersrc_add_frame_internal(ctx, frame, flags);
+
+    if (!(copy = av_frame_alloc()))
         return AVERROR(ENOMEM);
-    ret = av_buffersrc_add_ref(buffer_src, picref, flags);
-    picref->buf->data[0] = NULL;
-    avfilter_unref_buffer(picref);
+    ret = av_frame_ref(copy, frame);
+    if (ret >= 0)
+        ret = av_buffersrc_add_frame_internal(ctx, copy, flags);
+
+    av_frame_free(&copy);
     return ret;
 }
 
-int av_buffersrc_write_frame(AVFilterContext *buffer_filter, const AVFrame *frame)
+static int attribute_align_arg av_buffersrc_add_frame_internal(AVFilterContext *ctx,
+                                                               AVFrame *frame, int flags)
 {
-    return av_buffersrc_add_frame(buffer_filter, frame, 0);
-}
-
-int av_buffersrc_add_ref(AVFilterContext *s, AVFilterBufferRef *buf, int flags)
-{
-    BufferSourceContext *c = s->priv;
-    AVFilterBufferRef *to_free = NULL;
+    BufferSourceContext *s = ctx->priv;
+    AVFrame *copy;
     int ret;
 
-    if (!buf) {
-        c->eof = 1;
+    s->nb_failed_requests = 0;
+
+    if (!frame) {
+        s->eof = 1;
         return 0;
-    } else if (c->eof)
+    } else if (s->eof)
         return AVERROR(EINVAL);
 
-    if (!av_fifo_space(c->fifo) &&
-        (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
-                                         sizeof(buf))) < 0)
-        return ret;
-
     if (!(flags & AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT)) {
-        switch (s->outputs[0]->type) {
-        case AVMEDIA_TYPE_VIDEO:
-            CHECK_VIDEO_PARAM_CHANGE(s, c, buf->video->w, buf->video->h, buf->format);
-            break;
-        case AVMEDIA_TYPE_AUDIO:
-            if (!buf->audio->channel_layout)
-                buf->audio->channel_layout = c->channel_layout;
-            CHECK_AUDIO_PARAM_CHANGE(s, c, buf->audio->sample_rate, buf->audio->channel_layout,
-                                     buf->format);
-            break;
-        default:
-            return AVERROR(EINVAL);
-        }
-    }
-    if (!(flags & AV_BUFFERSRC_FLAG_NO_COPY))
-        to_free = buf = ff_copy_buffer_ref(s->outputs[0], buf);
-    if(!buf)
-        return -1;
 
-    if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0) {
-        avfilter_unref_buffer(to_free);
-        return ret;
+    switch (ctx->outputs[0]->type) {
+    case AVMEDIA_TYPE_VIDEO:
+        CHECK_VIDEO_PARAM_CHANGE(ctx, s, frame->width, frame->height,
+                                 frame->format);
+        break;
+    case AVMEDIA_TYPE_AUDIO:
+        /* For layouts unknown on input but known on link after negotiation. */
+        if (!frame->channel_layout)
+            frame->channel_layout = s->channel_layout;
+        CHECK_AUDIO_PARAM_CHANGE(ctx, s, frame->sample_rate, frame->channel_layout,
+                                 av_frame_get_channels(frame), frame->format);
+        break;
+    default:
+        return AVERROR(EINVAL);
     }
-    c->nb_failed_requests = 0;
-    if (c->warning_limit &&
-        av_fifo_size(c->fifo) / sizeof(buf) >= c->warning_limit) {
-        av_log(s, AV_LOG_WARNING,
-               "%d buffers queued in %s, something may be wrong.\n",
-               c->warning_limit,
-               (char *)av_x_if_null(s->name, s->filter->name));
-        c->warning_limit *= 10;
+
+    }
+
+    if (!av_fifo_space(s->fifo) &&
+        (ret = av_fifo_realloc2(s->fifo, av_fifo_size(s->fifo) +
+                                         sizeof(copy))) < 0)
+        return ret;
+
+    if (!(copy = av_frame_alloc()))
+        return AVERROR(ENOMEM);
+    av_frame_move_ref(copy, frame);
+
+    if ((ret = av_fifo_generic_write(s->fifo, &copy, sizeof(copy), NULL)) < 0) {
+        av_frame_move_ref(frame, copy);
+        av_frame_free(&copy);
+        return ret;
     }
 
     if ((flags & AV_BUFFERSRC_FLAG_PUSH))
-        if ((ret = s->output_pads[0].request_frame(s->outputs[0])) < 0)
+        if ((ret = ctx->output_pads[0].request_frame(ctx->outputs[0])) < 0)
             return ret;
 
     return 0;
 }
 
-#ifdef FF_API_BUFFERSRC_BUFFER
-int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
+#if FF_API_AVFILTERBUFFER
+static void compat_free_buffer(void *opaque, uint8_t *data)
 {
-    return av_buffersrc_add_ref(s, buf, AV_BUFFERSRC_FLAG_NO_COPY);
+    AVFilterBufferRef *buf = opaque;
+    AV_NOWARN_DEPRECATED(
+    avfilter_unref_buffer(buf);
+    )
+}
+
+static void compat_unref_buffer(void *opaque, uint8_t *data)
+{
+    AVBufferRef *buf = opaque;
+    AV_NOWARN_DEPRECATED(
+    av_buffer_unref(&buf);
+    )
+}
+
+int av_buffersrc_add_ref(AVFilterContext *ctx, AVFilterBufferRef *buf,
+                         int flags)
+{
+    BufferSourceContext *s = ctx->priv;
+    AVFrame *frame = NULL;
+    AVBufferRef *dummy_buf = NULL;
+    int ret = 0, planes, i;
+
+    if (!buf) {
+        s->eof = 1;
+        return 0;
+    } else if (s->eof)
+        return AVERROR(EINVAL);
+
+    frame = av_frame_alloc();
+    if (!frame)
+        return AVERROR(ENOMEM);
+
+    dummy_buf = av_buffer_create(NULL, 0, compat_free_buffer, buf,
+                                 (buf->perms & AV_PERM_WRITE) ? 0 : AV_BUFFER_FLAG_READONLY);
+    if (!dummy_buf) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+
+    AV_NOWARN_DEPRECATED(
+    if ((ret = avfilter_copy_buf_props(frame, buf)) < 0)
+        goto fail;
+    )
+
+#define WRAP_PLANE(ref_out, data, data_size)                            \
+do {                                                                    \
+    AVBufferRef *dummy_ref = av_buffer_ref(dummy_buf);                  \
+    if (!dummy_ref) {                                                   \
+        ret = AVERROR(ENOMEM);                                          \
+        goto fail;                                                      \
+    }                                                                   \
+    ref_out = av_buffer_create(data, data_size, compat_unref_buffer,    \
+                               dummy_ref, (buf->perms & AV_PERM_WRITE) ? 0 : AV_BUFFER_FLAG_READONLY);                           \
+    if (!ref_out) {                                                     \
+        av_frame_unref(frame);                                          \
+        ret = AVERROR(ENOMEM);                                          \
+        goto fail;                                                      \
+    }                                                                   \
+} while (0)
+
+    if (ctx->outputs[0]->type  == AVMEDIA_TYPE_VIDEO) {
+        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
+
+        planes = av_pix_fmt_count_planes(frame->format);
+        if (!desc || planes <= 0) {
+            ret = AVERROR(EINVAL);
+            goto fail;
+        }
+
+        for (i = 0; i < planes; i++) {
+            int v_shift    = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
+            int plane_size = (frame->height >> v_shift) * frame->linesize[i];
+
+            WRAP_PLANE(frame->buf[i], frame->data[i], plane_size);
+        }
+    } else {
+        int planar = av_sample_fmt_is_planar(frame->format);
+        int channels = av_get_channel_layout_nb_channels(frame->channel_layout);
+
+        planes = planar ? channels : 1;
+
+        if (planes > FF_ARRAY_ELEMS(frame->buf)) {
+            frame->nb_extended_buf = planes - FF_ARRAY_ELEMS(frame->buf);
+            frame->extended_buf = av_mallocz(sizeof(*frame->extended_buf) *
+                                             frame->nb_extended_buf);
+            if (!frame->extended_buf) {
+                ret = AVERROR(ENOMEM);
+                goto fail;
+            }
+        }
+
+        for (i = 0; i < FFMIN(planes, FF_ARRAY_ELEMS(frame->buf)); i++)
+            WRAP_PLANE(frame->buf[i], frame->extended_data[i], frame->linesize[0]);
+
+        for (i = 0; i < planes - FF_ARRAY_ELEMS(frame->buf); i++)
+            WRAP_PLANE(frame->extended_buf[i],
+                       frame->extended_data[i + FF_ARRAY_ELEMS(frame->buf)],
+                       frame->linesize[0]);
+    }
+
+    ret = av_buffersrc_add_frame_flags(ctx, frame, flags);
+
+fail:
+    av_buffer_unref(&dummy_buf);
+    av_frame_free(&frame);
+
+    return ret;
+}
+
+int av_buffersrc_buffer(AVFilterContext *ctx, AVFilterBufferRef *buf)
+{
+    return av_buffersrc_add_ref(ctx, buf, 0);
 }
 #endif
 
-unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
-{
-    return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
-}
-
-#define OFFSET(x) offsetof(BufferSourceContext, x)
-#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
-static const AVOption buffer_options[] = {
-    { "time_base",      NULL, OFFSET(time_base),           AV_OPT_TYPE_RATIONAL,   { .dbl = 0 }, 0, INT_MAX, FLAGS },
-    { "frame_rate",     NULL, OFFSET(frame_rate),          AV_OPT_TYPE_RATIONAL,   { .dbl = 0 }, 0, INT_MAX, FLAGS },
-    { "video_size",     NULL, OFFSET(w),                   AV_OPT_TYPE_IMAGE_SIZE, .flags = FLAGS },
-    { "pix_fmt",        NULL, OFFSET(pix_fmt),             AV_OPT_TYPE_PIXEL_FMT,  .flags = FLAGS },
-    { "pixel_aspect",   NULL, OFFSET(pixel_aspect),        AV_OPT_TYPE_RATIONAL,   { .dbl = 0 }, 0, INT_MAX, FLAGS },
-    { "sws_param",      NULL, OFFSET(sws_param),           AV_OPT_TYPE_STRING,     .flags = FLAGS },
-    { NULL },
-};
-#undef FLAGS
-
-AVFILTER_DEFINE_CLASS(buffer);
-
-static av_cold int init_video(AVFilterContext *ctx, const char *args)
+static av_cold int init_video(AVFilterContext *ctx)
 {
     BufferSourceContext *c = ctx->priv;
-    char pix_fmt_str[128], sws_param[256] = "", *colon, *equal;
-    int ret, n = 0;
 
-    c->class = &buffer_class;
-
-    if (!args) {
-        av_log(ctx, AV_LOG_ERROR, "Arguments required\n");
+    if (c->pix_fmt == AV_PIX_FMT_NONE || !c->w || !c->h || av_q2d(c->time_base) <= 0) {
+        av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n");
         return AVERROR(EINVAL);
     }
-    colon = strchr(args, ':');
-    equal = strchr(args, '=');
-    if (equal && (!colon || equal < colon)) {
-        av_opt_set_defaults(c);
-        ret = av_set_options_string(c, args, "=", ":");
-        if (ret < 0)
-            goto fail;
-    } else {
-    if ((n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
-                    &c->time_base.num, &c->time_base.den,
-                    &c->pixel_aspect.num, &c->pixel_aspect.den, sws_param)) < 7) {
-        av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
-        ret = AVERROR(EINVAL);
-        goto fail;
-    }
-    av_log(ctx, AV_LOG_WARNING, "Flat options syntax is deprecated, use key=value pairs\n");
 
-    if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
-        goto fail;
-    c->sws_param = av_strdup(sws_param);
-    if (!c->sws_param) {
-        ret = AVERROR(ENOMEM);
-        goto fail;
-    }
-    }
-
-    if (!(c->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*)))) {
-        ret = AVERROR(ENOMEM);
-        goto fail;
-    }
+    if (!(c->fifo = av_fifo_alloc(sizeof(AVFrame*))))
+        return AVERROR(ENOMEM);
 
     av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d pixfmt:%s tb:%d/%d fr:%d/%d sar:%d/%d sws_param:%s\n",
            c->w, c->h, av_get_pix_fmt_name(c->pix_fmt),
@@ -232,41 +309,61 @@
            c->pixel_aspect.num, c->pixel_aspect.den, (char *)av_x_if_null(c->sws_param, ""));
     c->warning_limit = 100;
     return 0;
-
-fail:
-    av_opt_free(c);
-    return ret;
 }
 
-#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
+unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src)
+{
+    return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
+}
+
+#define OFFSET(x) offsetof(BufferSourceContext, x)
+#define A AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
+#define V AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption buffer_options[] = {
+    { "width",         NULL,                     OFFSET(w),                AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, V },
+    { "video_size",    NULL,                     OFFSET(w),                AV_OPT_TYPE_IMAGE_SIZE,                .flags = V },
+    { "height",        NULL,                     OFFSET(h),                AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, V },
+    { "pix_fmt",       NULL,                     OFFSET(pix_fmt),          AV_OPT_TYPE_PIXEL_FMT,                 .flags = V },
+#if FF_API_OLD_FILTER_OPTS
+    /* those 4 are for compatibility with the old option passing system where each filter
+     * did its own parsing */
+    { "time_base_num", "deprecated, do not use", OFFSET(time_base.num),    AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, V },
+    { "time_base_den", "deprecated, do not use", OFFSET(time_base.den),    AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, V },
+    { "sar_num",       "deprecated, do not use", OFFSET(pixel_aspect.num), AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, V },
+    { "sar_den",       "deprecated, do not use", OFFSET(pixel_aspect.den), AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, V },
+#endif
+    { "sar",           "sample aspect ratio",    OFFSET(pixel_aspect),     AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V },
+    { "pixel_aspect",  "sample aspect ratio",    OFFSET(pixel_aspect),     AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V },
+    { "time_base",     NULL,                     OFFSET(time_base),        AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
+    { "frame_rate",    NULL,                     OFFSET(frame_rate),       AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V },
+    { "sws_param",     NULL,                     OFFSET(sws_param),        AV_OPT_TYPE_STRING,                    .flags = V },
+    { NULL },
+};
+
+AVFILTER_DEFINE_CLASS(buffer);
+
 static const AVOption abuffer_options[] = {
-    { "time_base",      NULL, OFFSET(time_base),           AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, FLAGS },
-    { "sample_rate",    NULL, OFFSET(sample_rate),         AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, FLAGS },
-    { "sample_fmt",     NULL, OFFSET(sample_fmt_str),      AV_OPT_TYPE_STRING, .flags = FLAGS },
-    { "channels",       NULL, OFFSET(channels),            AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, FLAGS },
-    { "channel_layout", NULL, OFFSET(channel_layout_str),  AV_OPT_TYPE_STRING, .flags = FLAGS },
+    { "time_base",      NULL, OFFSET(time_base),           AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, INT_MAX, A },
+    { "sample_rate",    NULL, OFFSET(sample_rate),         AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, A },
+    { "sample_fmt",     NULL, OFFSET(sample_fmt_str),      AV_OPT_TYPE_STRING,             .flags = A },
+    { "channel_layout", NULL, OFFSET(channel_layout_str),  AV_OPT_TYPE_STRING,             .flags = A },
+    { "channels",       NULL, OFFSET(channels),            AV_OPT_TYPE_INT,      { .i64 = 0 }, 0, INT_MAX, A },
     { NULL },
 };
 
 AVFILTER_DEFINE_CLASS(abuffer);
 
-static av_cold int init_audio(AVFilterContext *ctx, const char *args)
+static av_cold int init_audio(AVFilterContext *ctx)
 {
     BufferSourceContext *s = ctx->priv;
     int ret = 0;
 
-    s->class = &abuffer_class;
-    av_opt_set_defaults(s);
-
-    if ((ret = av_set_options_string(s, args, "=", ":")) < 0)
-        goto fail;
-
     s->sample_fmt = av_get_sample_fmt(s->sample_fmt_str);
     if (s->sample_fmt == AV_SAMPLE_FMT_NONE) {
-        av_log(ctx, AV_LOG_ERROR, "Invalid sample format '%s'\n",
+        av_log(ctx, AV_LOG_ERROR, "Invalid sample format %s\n",
                s->sample_fmt_str);
-        ret = AVERROR(EINVAL);
-        goto fail;
+        return AVERROR(EINVAL);
     }
 
     if (s->channel_layout_str) {
@@ -274,10 +371,9 @@
         /* TODO reindent */
     s->channel_layout = av_get_channel_layout(s->channel_layout_str);
     if (!s->channel_layout) {
-        av_log(ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n",
+        av_log(ctx, AV_LOG_ERROR, "Invalid channel layout %s.\n",
                s->channel_layout_str);
-        ret = AVERROR(EINVAL);
-        goto fail;
+        return AVERROR(EINVAL);
     }
         n = av_get_channel_layout_nb_channels(s->channel_layout);
         if (s->channels) {
@@ -286,22 +382,18 @@
                        "Mismatching channel count %d and layout '%s' "
                        "(%d channels)\n",
                        s->channels, s->channel_layout_str, n);
-                ret = AVERROR(EINVAL);
-                goto fail;
+                return AVERROR(EINVAL);
             }
         }
         s->channels = n;
     } else if (!s->channels) {
         av_log(ctx, AV_LOG_ERROR, "Neither number of channels nor "
                                   "channel layout specified\n");
-        ret = AVERROR(EINVAL);
-        goto fail;
+        return AVERROR(EINVAL);
     }
 
-    if (!(s->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*)))) {
-        ret = AVERROR(ENOMEM);
-        goto fail;
-    }
+    if (!(s->fifo = av_fifo_alloc(sizeof(AVFrame*))))
+        return AVERROR(ENOMEM);
 
     if (!s->time_base.num)
         s->time_base = (AVRational){1, s->sample_rate};
@@ -312,8 +404,6 @@
            s->sample_rate, s->channel_layout_str);
     s->warning_limit = 100;
 
-fail:
-    av_opt_free(s);
     return ret;
 }
 
@@ -321,13 +411,12 @@
 {
     BufferSourceContext *s = ctx->priv;
     while (s->fifo && av_fifo_size(s->fifo)) {
-        AVFilterBufferRef *buf;
-        av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
-        avfilter_unref_buffer(buf);
+        AVFrame *frame;
+        av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL);
+        av_frame_free(&frame);
     }
     av_fifo_free(s->fifo);
     s->fifo = NULL;
-    av_freep(&s->sws_param);
 }
 
 static int query_formats(AVFilterContext *ctx)
@@ -387,7 +476,7 @@
 static int request_frame(AVFilterLink *link)
 {
     BufferSourceContext *c = link->src->priv;
-    AVFilterBufferRef *buf;
+    AVFrame *frame;
 
     if (!av_fifo_size(c->fifo)) {
         if (c->eof)
@@ -395,9 +484,9 @@
         c->nb_failed_requests++;
         return AVERROR(EAGAIN);
     }
-    av_fifo_generic_read(c->fifo, &buf, sizeof(buf), NULL);
+    av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL);
 
-    return ff_filter_frame(link, buf);
+    return ff_filter_frame(link, frame);
 }
 
 static int poll_frame(AVFilterLink *link)
@@ -406,7 +495,7 @@
     int size = av_fifo_size(c->fifo);
     if (!size && c->eof)
         return AVERROR_EOF;
-    return size/sizeof(AVFilterBufferRef*);
+    return size/sizeof(AVFrame*);
 }
 
 static const AVFilterPad avfilter_vsrc_buffer_outputs[] = {
diff --git a/libavfilter/buffersrc.h b/libavfilter/buffersrc.h
index 7f3c8d8..66361b3 100644
--- a/libavfilter/buffersrc.h
+++ b/libavfilter/buffersrc.h
@@ -35,16 +35,25 @@
      */
     AV_BUFFERSRC_FLAG_NO_CHECK_FORMAT = 1,
 
+#if FF_API_AVFILTERBUFFER
     /**
-     * Do not copy buffer data.
+     * Ignored
      */
     AV_BUFFERSRC_FLAG_NO_COPY = 2,
+#endif
 
     /**
      * Immediately push the frame to the output.
      */
     AV_BUFFERSRC_FLAG_PUSH = 4,
 
+    /**
+     * Keep a reference to the frame.
+     * If the frame if reference-counted, create a new reference; otherwise
+     * copy the frame data.
+     */
+    AV_BUFFERSRC_FLAG_KEEP_REF = 8,
+
 };
 
 /**
@@ -68,14 +77,15 @@
  */
 unsigned av_buffersrc_get_nb_failed_requests(AVFilterContext *buffer_src);
 
-#ifdef FF_API_BUFFERSRC_BUFFER
+#if FF_API_AVFILTERBUFFER
 /**
  * Add a buffer to the filtergraph s.
  *
  * @param buf buffer containing frame data to be passed down the filtergraph.
  * This function will take ownership of buf, the user must not free it.
  * A NULL buf signals EOF -- i.e. no more frames will be sent to this filter.
- * @deprecated Use av_buffersrc_add_ref(s, picref, AV_BUFFERSRC_FLAG_NO_COPY) instead.
+ *
+ * @deprecated use av_buffersrc_write_frame() or av_buffersrc_add_frame()
  */
 attribute_deprecated
 int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf);
@@ -85,11 +95,54 @@
  * Add a frame to the buffer source.
  *
  * @param s an instance of the buffersrc filter.
- * @param frame frame to be added.
+ * @param frame frame to be added. If the frame is reference counted, this
+ * function will make a new reference to it. Otherwise the frame data will be
+ * copied.
  *
- * @warning frame data will be memcpy()ed, which may be a big performance
- *          hit. Use av_buffersrc_buffer() to avoid copying the data.
+ * @return 0 on success, a negative AVERROR on error
+ *
+ * This function is equivalent to av_buffersrc_add_frame_flags() with the
+ * AV_BUFFERSRC_FLAG_KEEP_REF flag.
  */
 int av_buffersrc_write_frame(AVFilterContext *s, const AVFrame *frame);
 
+/**
+ * Add a frame to the buffer source.
+ *
+ * @param s an instance of the buffersrc filter.
+ * @param frame frame to be added. If the frame is reference counted, this
+ * function will take ownership of the reference(s) and reset the frame.
+ * Otherwise the frame data will be copied. If this function returns an error,
+ * the input frame is not touched.
+ *
+ * @return 0 on success, a negative AVERROR on error.
+ *
+ * @note the difference between this function and av_buffersrc_write_frame() is
+ * that av_buffersrc_write_frame() creates a new reference to the input frame,
+ * while this function takes ownership of the reference passed to it.
+ *
+ * This function is equivalent to av_buffersrc_add_frame_flags() without the
+ * AV_BUFFERSRC_FLAG_KEEP_REF flag.
+ */
+int av_buffersrc_add_frame(AVFilterContext *ctx, AVFrame *frame);
+
+/**
+ * Add a frame to the buffer source.
+ *
+ * By default, if the frame is reference-counted, this function will take
+ * ownership of the reference(s) and reset the frame. This can be controled
+ * using the flags.
+ *
+ * If this function returns an error, the input frame is not touched.
+ *
+ * @param buffer_src  pointer to a buffer source context
+ * @param frame       a frame, or NULL to mark EOF
+ * @param flags       a combination of AV_BUFFERSRC_FLAG_*
+ * @return            >= 0 in case of success, a negative AVERROR code
+ *                    in case of failure
+ */
+int av_buffersrc_add_frame_flags(AVFilterContext *buffer_src,
+                                 AVFrame *frame, int flags);
+
+
 #endif /* AVFILTER_BUFFERSRC_H */
diff --git a/libavfilter/deshake.h b/libavfilter/deshake.h
new file mode 100644
index 0000000..c24090e
--- /dev/null
+++ b/libavfilter/deshake.h
@@ -0,0 +1,104 @@
+/*
+ * Copyright (C) 2013 Wei Gao <weigao@multicorewareinc.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
+ */
+
+#ifndef AVFILTER_DESHAKE_H
+#define AVFILTER_DESHAKE_H
+
+#include "config.h"
+#include "avfilter.h"
+#include "libavcodec/dsputil.h"
+#include "transform.h"
+#if CONFIG_OPENCL
+#include "libavutil/opencl.h"
+#endif
+
+
+enum SearchMethod {
+    EXHAUSTIVE,        ///< Search all possible positions
+    SMART_EXHAUSTIVE,  ///< Search most possible positions (faster)
+    SEARCH_COUNT
+};
+
+typedef struct {
+    int x;             ///< Horizontal shift
+    int y;             ///< Vertical shift
+} IntMotionVector;
+
+typedef struct {
+    double x;             ///< Horizontal shift
+    double y;             ///< Vertical shift
+} MotionVector;
+
+typedef struct {
+    MotionVector vector;  ///< Motion vector
+    double angle;         ///< Angle of rotation
+    double zoom;          ///< Zoom percentage
+} Transform;
+
+#if CONFIG_OPENCL
+
+typedef struct {
+    size_t matrix_size;
+    float matrix_y[9];
+    float matrix_uv[9];
+    cl_mem cl_matrix_y;
+    cl_mem cl_matrix_uv;
+    int in_plane_size[8];
+    int out_plane_size[8];
+    int plane_num;
+    cl_mem cl_inbuf;
+    size_t cl_inbuf_size;
+    cl_mem cl_outbuf;
+    size_t cl_outbuf_size;
+    AVOpenCLKernelEnv kernel_env;
+} DeshakeOpenclContext;
+
+#endif
+
+typedef struct {
+    const AVClass *class;
+    AVFrame *ref;              ///< Previous frame
+    int rx;                    ///< Maximum horizontal shift
+    int ry;                    ///< Maximum vertical shift
+    int edge;                  ///< Edge fill method
+    int blocksize;             ///< Size of blocks to compare
+    int contrast;              ///< Contrast threshold
+    int search;                ///< Motion search method
+    AVCodecContext *avctx;
+    DSPContext c;              ///< Context providing optimized SAD methods
+    Transform last;            ///< Transform from last frame
+    int refcount;              ///< Number of reference frames (defines averaging window)
+    FILE *fp;
+    Transform avg;
+    int cw;                    ///< Crop motion search to this box
+    int ch;
+    int cx;
+    int cy;
+    char *filename;            ///< Motion search detailed log filename
+    int opencl;
+#if CONFIG_OPENCL
+    DeshakeOpenclContext opencl_ctx;
+#endif
+    int (* transform)(AVFilterContext *ctx, int width, int height, int cw, int ch,
+                      const float *matrix_y, const float *matrix_uv, enum InterpolateMethod interpolate,
+                      enum FillMethod fill, AVFrame *in, AVFrame *out);
+} DeshakeContext;
+
+#endif /* AVFILTER_DESHAKE_H */
diff --git a/libavfilter/deshake_kernel.h b/libavfilter/deshake_kernel.h
new file mode 100644
index 0000000..1eb06fe
--- /dev/null
+++ b/libavfilter/deshake_kernel.h
@@ -0,0 +1,217 @@
+/*
+ * Copyright (C) 2013 Wei Gao <weigao@multicorewareinc.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
+ */
+
+#ifndef AVFILTER_DESHAKE_KERNEL_H
+#define AVFILTER_DESHAKE_KERNEL_H
+
+#include "libavutil/opencl.h"
+
+const char *ff_kernel_deshake_opencl = AV_OPENCL_KERNEL(
+
+inline unsigned char pixel(global const unsigned char *src, float x, float y,
+                           int w, int h,int stride, unsigned char def)
+{
+    return (x < 0 || y < 0 || x >= w || y >= h) ? def : src[(int)x + (int)y * stride];
+}
+unsigned char interpolate_nearest(float x, float y, global const unsigned char *src,
+                                  int width, int height, int stride, unsigned char def)
+{
+    return pixel(src, (int)(x + 0.5), (int)(y + 0.5), width, height, stride, def);
+}
+
+unsigned char interpolate_bilinear(float x, float y, global const unsigned char *src,
+                                   int width, int height, int stride, unsigned char def)
+{
+    int x_c, x_f, y_c, y_f;
+    int v1, v2, v3, v4;
+
+    if (x < -1 || x > width || y < -1 || y > height) {
+        return def;
+    } else {
+        x_f = (int)x;
+        x_c = x_f + 1;
+
+        y_f = (int)y;
+        y_c = y_f + 1;
+
+        v1 = pixel(src, x_c, y_c, width, height, stride, def);
+        v2 = pixel(src, x_c, y_f, width, height, stride, def);
+        v3 = pixel(src, x_f, y_c, width, height, stride, def);
+        v4 = pixel(src, x_f, y_f, width, height, stride, def);
+
+        return (v1*(x - x_f)*(y - y_f) + v2*((x - x_f)*(y_c - y)) +
+                v3*(x_c - x)*(y - y_f) + v4*((x_c - x)*(y_c - y)));
+    }
+}
+
+unsigned char interpolate_biquadratic(float x, float y, global const unsigned char *src,
+                                      int width, int height, int stride, unsigned char def)
+{
+    int     x_c, x_f, y_c, y_f;
+    unsigned char v1,  v2,  v3,  v4;
+    float   f1,  f2,  f3,  f4;
+
+    if (x < - 1 || x > width || y < -1 || y > height)
+        return def;
+    else {
+        x_f = (int)x;
+        x_c = x_f + 1;
+        y_f = (int)y;
+        y_c = y_f + 1;
+
+        v1 = pixel(src, x_c, y_c, width, height, stride, def);
+        v2 = pixel(src, x_c, y_f, width, height, stride, def);
+        v3 = pixel(src, x_f, y_c, width, height, stride, def);
+        v4 = pixel(src, x_f, y_f, width, height, stride, def);
+
+        f1 = 1 - sqrt((x_c - x) * (y_c - y));
+        f2 = 1 - sqrt((x_c - x) * (y - y_f));
+        f3 = 1 - sqrt((x - x_f) * (y_c - y));
+        f4 = 1 - sqrt((x - x_f) * (y - y_f));
+        return (v1 * f1 + v2 * f2 + v3 * f3 + v4 * f4) / (f1 + f2 + f3 + f4);
+    }
+}
+
+inline const float clipf(float a, float amin, float amax)
+{
+    if      (a < amin) return amin;
+    else if (a > amax) return amax;
+    else               return a;
+}
+
+inline int mirror(int v, int m)
+{
+    while ((unsigned)v > (unsigned)m) {
+        v = -v;
+        if (v < 0)
+            v += 2 * m;
+    }
+    return v;
+}
+
+kernel void avfilter_transform(global  unsigned char *src,
+                               global  unsigned char *dst,
+                               global          float *matrix,
+                               global          float *matrix2,
+                                                 int interpolate,
+                                                 int fillmethod,
+                                                 int src_stride_lu,
+                                                 int dst_stride_lu,
+                                                 int src_stride_ch,
+                                                 int dst_stride_ch,
+                                                 int height,
+                                                 int width,
+                                                 int ch,
+                                                 int cw)
+{
+     int global_id = get_global_id(0);
+
+     global unsigned char *dst_y = dst;
+     global unsigned char *dst_u = dst_y + height * dst_stride_lu;
+     global unsigned char *dst_v = dst_u + ch * dst_stride_ch;
+
+     global unsigned char *src_y = src;
+     global unsigned char *src_u = src_y + height * src_stride_lu;
+     global unsigned char *src_v = src_u + ch * src_stride_ch;
+
+     global unsigned char *tempdst;
+     global unsigned char *tempsrc;
+
+     int x;
+     int y;
+     float x_s;
+     float y_s;
+     int tempsrc_stride;
+     int tempdst_stride;
+     int temp_height;
+     int temp_width;
+     int curpos;
+     unsigned char def = 0;
+     if (global_id < width*height) {
+        y = global_id/width;
+        x = global_id%width;
+        x_s = x * matrix[0] + y * matrix[1] + matrix[2];
+        y_s = x * matrix[3] + y * matrix[4] + matrix[5];
+        tempdst = dst_y;
+        tempsrc = src_y;
+        tempsrc_stride = src_stride_lu;
+        tempdst_stride = dst_stride_lu;
+        temp_height = height;
+        temp_width = width;
+     } else if ((global_id >= width*height)&&(global_id < width*height + ch*cw)) {
+        y = (global_id - width*height)/cw;
+        x = (global_id - width*height)%cw;
+        x_s = x * matrix2[0] + y * matrix2[1] + matrix2[2];
+        y_s = x * matrix2[3] + y * matrix2[4] + matrix2[5];
+        tempdst = dst_u;
+        tempsrc = src_u;
+        tempsrc_stride = src_stride_ch;
+        tempdst_stride = dst_stride_ch;
+        temp_height = ch;
+        temp_width = cw;
+     } else {
+        y = (global_id - width*height - ch*cw)/cw;
+        x = (global_id - width*height - ch*cw)%cw;
+        x_s = x * matrix2[0] + y * matrix2[1] + matrix2[2];
+        y_s = x * matrix2[3] + y * matrix2[4] + matrix2[5];
+        tempdst = dst_v;
+        tempsrc = src_v;
+        tempsrc_stride = src_stride_ch;
+        tempdst_stride = dst_stride_ch;
+        temp_height = ch;
+        temp_width = cw;
+     }
+     curpos = y * tempdst_stride + x;
+     switch (fillmethod) {
+        case 0: //FILL_BLANK
+            def = 0;
+            break;
+        case 1: //FILL_ORIGINAL
+            def = tempsrc[y*tempsrc_stride+x];
+            break;
+        case 2: //FILL_CLAMP
+            y_s = clipf(y_s, 0, temp_height - 1);
+            x_s = clipf(x_s, 0, temp_width - 1);
+            def = tempsrc[(int)y_s * tempsrc_stride + (int)x_s];
+            break;
+        case 3: //FILL_MIRROR
+            y_s = mirror(y_s,temp_height - 1);
+            x_s = mirror(x_s,temp_width - 1);
+            def = tempsrc[(int)y_s * tempsrc_stride + (int)x_s];
+            break;
+    }
+    switch (interpolate) {
+        case 0: //INTERPOLATE_NEAREST
+            tempdst[curpos] = interpolate_nearest(x_s, y_s, tempsrc, temp_width, temp_height, tempsrc_stride, def);
+            break;
+        case 1: //INTERPOLATE_BILINEAR
+            tempdst[curpos] = interpolate_bilinear(x_s, y_s, tempsrc, temp_width, temp_height, tempsrc_stride, def);
+            break;
+        case 2: //INTERPOLATE_BIQUADRATIC
+            tempdst[curpos] = interpolate_biquadratic(x_s, y_s, tempsrc, temp_width, temp_height, tempsrc_stride, def);
+            break;
+        default:
+            return;
+    }
+}
+);
+
+#endif /* AVFILTER_DESHAKE_KERNEL_H */
diff --git a/libavfilter/deshake_opencl.c b/libavfilter/deshake_opencl.c
new file mode 100644
index 0000000..0f6dcc4
--- /dev/null
+++ b/libavfilter/deshake_opencl.c
@@ -0,0 +1,178 @@
+/*
+ * Copyright (C) 2013 Wei Gao <weigao@multicorewareinc.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
+ */
+
+/**
+ * @file
+ * transform input video
+ */
+
+#include "libavutil/common.h"
+#include "libavutil/dict.h"
+#include "libavutil/pixdesc.h"
+#include "deshake_opencl.h"
+
+#define MATRIX_SIZE 6
+#define PLANE_NUM 3
+
+#define TRANSFORM_OPENCL_CHECK(method, ...)                                                                  \
+    status = method(__VA_ARGS__);                                                                            \
+    if (status != CL_SUCCESS) {                                                                              \
+        av_log(ctx, AV_LOG_ERROR, "error %s %d\n", # method, status);                                        \
+        return AVERROR_EXTERNAL;                                                                             \
+    }
+
+#define TRANSFORM_OPENCL_SET_KERNEL_ARG(arg_ptr)                                                             \
+    status = clSetKernelArg((kernel),(arg_no++),(sizeof(arg_ptr)),(void*)(&(arg_ptr)));                      \
+    if (status != CL_SUCCESS) {                                                                              \
+        av_log(ctx, AV_LOG_ERROR, "cannot set kernel argument: %d\n", status );                              \
+        return AVERROR_EXTERNAL;                                                                             \
+    }
+
+int ff_opencl_transform(AVFilterContext *ctx,
+                        int width, int height, int cw, int ch,
+                        const float *matrix_y, const float *matrix_uv,
+                        enum InterpolateMethod interpolate,
+                        enum FillMethod fill, AVFrame *in, AVFrame *out)
+{
+    int arg_no, ret = 0;
+    const size_t global_work_size = width * height + 2 * ch * cw;
+    cl_kernel kernel;
+    cl_int status;
+    DeshakeContext *deshake = ctx->priv;
+    ret = av_opencl_buffer_write(deshake->opencl_ctx.cl_matrix_y, (uint8_t *)matrix_y, deshake->opencl_ctx.matrix_size * sizeof(cl_float));
+    if (ret < 0)
+        return ret;
+    ret = av_opencl_buffer_write(deshake->opencl_ctx.cl_matrix_uv, (uint8_t *)matrix_uv, deshake->opencl_ctx.matrix_size * sizeof(cl_float));
+    if (ret < 0)
+        return ret;
+    kernel = deshake->opencl_ctx.kernel_env.kernel;
+    arg_no = 0;
+
+    if ((unsigned int)interpolate > INTERPOLATE_BIQUADRATIC) {
+        av_log(ctx, AV_LOG_ERROR, "Selected interpolate method is invalid\n");
+        return AVERROR(EINVAL);
+    }
+    TRANSFORM_OPENCL_SET_KERNEL_ARG(deshake->opencl_ctx.cl_inbuf);
+    TRANSFORM_OPENCL_SET_KERNEL_ARG(deshake->opencl_ctx.cl_outbuf);
+    TRANSFORM_OPENCL_SET_KERNEL_ARG(deshake->opencl_ctx.cl_matrix_y);
+    TRANSFORM_OPENCL_SET_KERNEL_ARG(deshake->opencl_ctx.cl_matrix_uv);
+    TRANSFORM_OPENCL_SET_KERNEL_ARG(interpolate);
+    TRANSFORM_OPENCL_SET_KERNEL_ARG(fill);
+    TRANSFORM_OPENCL_SET_KERNEL_ARG(in->linesize[0]);
+    TRANSFORM_OPENCL_SET_KERNEL_ARG(out->linesize[0]);
+    TRANSFORM_OPENCL_SET_KERNEL_ARG(in->linesize[1]);
+    TRANSFORM_OPENCL_SET_KERNEL_ARG(out->linesize[1]);
+    TRANSFORM_OPENCL_SET_KERNEL_ARG(height);
+    TRANSFORM_OPENCL_SET_KERNEL_ARG(width);
+    TRANSFORM_OPENCL_SET_KERNEL_ARG(ch);
+    TRANSFORM_OPENCL_SET_KERNEL_ARG(cw);
+    TRANSFORM_OPENCL_CHECK(clEnqueueNDRangeKernel, deshake->opencl_ctx.kernel_env.command_queue, deshake->opencl_ctx.kernel_env.kernel, 1, NULL,
+                           &global_work_size, NULL, 0, NULL, NULL);
+    clFinish(deshake->opencl_ctx.kernel_env.command_queue);
+    ret = av_opencl_buffer_read_image(out->data, deshake->opencl_ctx.out_plane_size,
+                                      deshake->opencl_ctx.plane_num, deshake->opencl_ctx.cl_outbuf,
+                                      deshake->opencl_ctx.cl_outbuf_size);
+    if (ret < 0)
+        return ret;
+    return ret;
+}
+
+int ff_opencl_deshake_init(AVFilterContext *ctx)
+{
+    int ret = 0;
+    DeshakeContext *deshake = ctx->priv;
+    ret = av_opencl_init(NULL);
+    if (ret < 0)
+        return ret;
+    deshake->opencl_ctx.matrix_size = MATRIX_SIZE;
+    deshake->opencl_ctx.plane_num   = PLANE_NUM;
+    ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_matrix_y,
+        deshake->opencl_ctx.matrix_size*sizeof(cl_float), CL_MEM_READ_ONLY, NULL);
+    if (ret < 0)
+        return ret;
+    ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_matrix_uv,
+        deshake->opencl_ctx.matrix_size*sizeof(cl_float), CL_MEM_READ_ONLY, NULL);
+    if (ret < 0)
+        return ret;
+    if (!deshake->opencl_ctx.kernel_env.kernel) {
+        ret =  av_opencl_create_kernel(&deshake->opencl_ctx.kernel_env, "avfilter_transform");
+        if (ret < 0) {
+            av_log(ctx, AV_LOG_ERROR, "OpenCL failed to create kernel for name 'avfilter_transform'\n");
+            return ret;
+        }
+    }
+    return ret;
+}
+
+void ff_opencl_deshake_uninit(AVFilterContext *ctx)
+{
+    DeshakeContext *deshake = ctx->priv;
+    av_opencl_buffer_release(&deshake->opencl_ctx.cl_inbuf);
+    av_opencl_buffer_release(&deshake->opencl_ctx.cl_outbuf);
+    av_opencl_buffer_release(&deshake->opencl_ctx.cl_matrix_y);
+    av_opencl_buffer_release(&deshake->opencl_ctx.cl_matrix_uv);
+    av_opencl_release_kernel(&deshake->opencl_ctx.kernel_env);
+    av_opencl_uninit();
+}
+
+
+int ff_opencl_deshake_process_inout_buf(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
+{
+    int ret = 0;
+    AVFilterLink *link = ctx->inputs[0];
+    DeshakeContext *deshake = ctx->priv;
+    int chroma_height = -((-link->h) >> av_pix_fmt_desc_get(link->format)->log2_chroma_h);
+
+    if ((!deshake->opencl_ctx.cl_inbuf) || (!deshake->opencl_ctx.cl_outbuf)) {
+        deshake->opencl_ctx.in_plane_size[0]  = (in->linesize[0] * in->height);
+        deshake->opencl_ctx.in_plane_size[1]  = (in->linesize[1] * chroma_height);
+        deshake->opencl_ctx.in_plane_size[2]  = (in->linesize[2] * chroma_height);
+        deshake->opencl_ctx.out_plane_size[0] = (out->linesize[0] * out->height);
+        deshake->opencl_ctx.out_plane_size[1] = (out->linesize[1] * chroma_height);
+        deshake->opencl_ctx.out_plane_size[2] = (out->linesize[2] * chroma_height);
+        deshake->opencl_ctx.cl_inbuf_size  = deshake->opencl_ctx.in_plane_size[0] +
+                                             deshake->opencl_ctx.in_plane_size[1] +
+                                             deshake->opencl_ctx.in_plane_size[2];
+        deshake->opencl_ctx.cl_outbuf_size = deshake->opencl_ctx.out_plane_size[0] +
+                                             deshake->opencl_ctx.out_plane_size[1] +
+                                             deshake->opencl_ctx.out_plane_size[2];
+        if (!deshake->opencl_ctx.cl_inbuf) {
+            ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_inbuf,
+                                            deshake->opencl_ctx.cl_inbuf_size,
+                                            CL_MEM_READ_ONLY, NULL);
+            if (ret < 0)
+                return ret;
+        }
+        if (!deshake->opencl_ctx.cl_outbuf) {
+            ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_outbuf,
+                                            deshake->opencl_ctx.cl_outbuf_size,
+                                            CL_MEM_READ_WRITE, NULL);
+            if (ret < 0)
+                return ret;
+        }
+    }
+    ret = av_opencl_buffer_write_image(deshake->opencl_ctx.cl_inbuf,
+                                 deshake->opencl_ctx.cl_inbuf_size,
+                                 0, in->data,deshake->opencl_ctx.in_plane_size,
+                                 deshake->opencl_ctx.plane_num);
+    if(ret < 0)
+        return ret;
+    return ret;
+}
diff --git a/libavfilter/deshake_opencl.h b/libavfilter/deshake_opencl.h
new file mode 100644
index 0000000..30d17d4
--- /dev/null
+++ b/libavfilter/deshake_opencl.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2013 Wei Gao <weigao@multicorewareinc.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
+ */
+
+#ifndef AVFILTER_DESHAKE_OPENCL_H
+#define AVFILTER_DESHAKE_OPENCL_H
+
+#include "deshake.h"
+
+int ff_opencl_deshake_init(AVFilterContext *ctx);
+
+void ff_opencl_deshake_uninit(AVFilterContext *ctx);
+
+int ff_opencl_deshake_process_inout_buf(AVFilterContext *ctx, AVFrame *in, AVFrame *out);
+
+int ff_opencl_transform(AVFilterContext *ctx,
+                        int width, int height, int cw, int ch,
+                        const float *matrix_y, const float *matrix_uv,
+                        enum InterpolateMethod interpolate,
+                        enum FillMethod fill, AVFrame *in, AVFrame *out);
+
+#endif /* AVFILTER_DESHAKE_OPENCL_H */
diff --git a/libavfilter/f_ebur128.c b/libavfilter/f_ebur128.c
index 66cc133..88d37e8 100644
--- a/libavfilter/f_ebur128.c
+++ b/libavfilter/f_ebur128.c
@@ -33,6 +33,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
+#include "libavutil/dict.h"
 #include "libavutil/xga_font_data.h"
 #include "libavutil/opt.h"
 #include "libavutil/timestamp.h"
@@ -97,7 +98,7 @@
     struct rect text;               ///< rectangle for the LU legend on the left
     struct rect graph;              ///< rectangle for the main graph in the center
     struct rect gauge;              ///< rectangle for the gauge on the right
-    AVFilterBufferRef *outpicref;   ///< output picture reference, updated regularly
+    AVFrame *outpicref;             ///< output picture reference, updated regularly
     int meter;                      ///< select a EBU mode between +9 and +18
     int scale_range;                ///< the range of LU values according to the meter
     int y_zero_lu;                  ///< the y value (pixel position) for 0 LU
@@ -123,6 +124,10 @@
     double integrated_loudness;     ///< integrated loudness in LUFS (I)
     double loudness_range;          ///< loudness range in LU (LRA)
     double lra_low, lra_high;       ///< low and high LRA values
+
+    /* misc */
+    int loglevel;                   ///< log level for frame logging
+    int metadata;                   ///< whether or not to inject loudness results in frames
 } EBUR128Context;
 
 #define OFFSET(x) offsetof(EBUR128Context, x)
@@ -133,6 +138,10 @@
     { "video", "set video output", OFFSET(do_video), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, V|F },
     { "size",  "set video size",   OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x480"}, 0, 0, V|F },
     { "meter", "set scale meter (+9 to +18)",  OFFSET(meter), AV_OPT_TYPE_INT, {.i64 = 9}, 9, 18, V|F },
+    { "framelog", "force frame logging level", OFFSET(loglevel), AV_OPT_TYPE_INT, {.i64 = -1},   INT_MIN, INT_MAX, A|V|F, "level" },
+        { "info",    "information logging level", 0, AV_OPT_TYPE_CONST, {.i64 = AV_LOG_INFO},    INT_MIN, INT_MAX, A|V|F, "level" },
+        { "verbose", "verbose logging level",     0, AV_OPT_TYPE_CONST, {.i64 = AV_LOG_VERBOSE}, INT_MIN, INT_MAX, A|V|F, "level" },
+    { "metadata", "inject metadata in the filtergraph", OFFSET(metadata), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, A|V|F },
     { NULL },
 };
 
@@ -174,7 +183,7 @@
     0x00, 0x96, 0x96,
 };
 
-static void drawtext(AVFilterBufferRef *pic, int x, int y, int ftid, const uint8_t *color, const char *fmt, ...)
+static void drawtext(AVFrame *pic, int x, int y, int ftid, const uint8_t *color, const char *fmt, ...)
 {
     int i;
     char buf[128] = {0};
@@ -207,7 +216,7 @@
     }
 }
 
-static void drawline(AVFilterBufferRef *pic, int x, int y, int len, int step)
+static void drawline(AVFrame *pic, int x, int y, int len, int step)
 {
     int i;
     uint8_t *p = pic->data[0] + y*pic->linesize[0] + x*3;
@@ -224,7 +233,7 @@
     uint8_t *p;
     AVFilterContext *ctx = outlink->src;
     EBUR128Context *ebur128 = ctx->priv;
-    AVFilterBufferRef *outpicref;
+    AVFrame *outpicref;
 
     /* check if there is enough space to represent everything decently */
     if (ebur128->w < 640 || ebur128->h < 480) {
@@ -259,10 +268,9 @@
     av_assert0(ebur128->graph.h == ebur128->gauge.h);
 
     /* prepare the initial picref buffer */
-    avfilter_unref_bufferp(&ebur128->outpicref);
+    av_frame_free(&ebur128->outpicref);
     ebur128->outpicref = outpicref =
-        ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_PRESERVE|AV_PERM_REUSE2,
-                            outlink->w, outlink->h);
+        ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!outpicref)
         return AVERROR(ENOMEM);
     outlink->sample_aspect_ratio = (AVRational){1,1};
@@ -308,6 +316,22 @@
     DRAW_RECT(ebur128->graph);
     DRAW_RECT(ebur128->gauge);
 
+    outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
+
+    return 0;
+}
+
+static int config_audio_input(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    EBUR128Context *ebur128 = ctx->priv;
+
+    /* force 100ms framing in case of metadata injection: the frames must have
+     * a granularity of the window overlap to be accurately exploited */
+    if (ebur128->metadata)
+        inlink->min_samples =
+        inlink->max_samples =
+        inlink->partial_buf_size = inlink->sample_rate / 10;
     return 0;
 }
 
@@ -357,6 +381,8 @@
             return AVERROR(ENOMEM);
     }
 
+    outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
+
     return 0;
 }
 
@@ -368,6 +394,8 @@
     int i;
     struct hist_entry *h = av_calloc(HIST_SIZE, sizeof(*h));
 
+    if (!h)
+        return NULL;
     for (i = 0; i < HIST_SIZE; i++) {
         h[i].loudness = i / (double)HIST_GRAIN + ABS_THRES;
         h[i].energy   = ENERGY(h[i].loudness);
@@ -375,17 +403,18 @@
     return h;
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
-    int ret;
     EBUR128Context *ebur128 = ctx->priv;
     AVFilterPad pad;
 
-    ebur128->class = &ebur128_class;
-    av_opt_set_defaults(ebur128);
-
-    if ((ret = av_set_options_string(ebur128, args, "=", ":")) < 0)
-        return ret;
+    if (ebur128->loglevel != AV_LOG_INFO &&
+        ebur128->loglevel != AV_LOG_VERBOSE) {
+        if (ebur128->do_video || ebur128->metadata)
+            ebur128->loglevel = AV_LOG_VERBOSE;
+        else
+            ebur128->loglevel = AV_LOG_INFO;
+    }
 
     // if meter is  +9 scale, scale range is from -18 LU to  +9 LU (or 3*9)
     // if meter is +18 scale, scale range is from -36 LU to +18 LU (or 3*18)
@@ -393,6 +422,8 @@
 
     ebur128->i400.histogram  = get_histogram();
     ebur128->i3000.histogram = get_histogram();
+    if (!ebur128->i400.histogram || !ebur128->i3000.histogram)
+        return AVERROR(ENOMEM);
 
     ebur128->integrated_loudness = ABS_THRES;
     ebur128->loudness_range = 0;
@@ -450,15 +481,15 @@
     return gate_hist_pos;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *insamples)
+static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
 {
     int i, ch, idx_insample;
     AVFilterContext *ctx = inlink->dst;
     EBUR128Context *ebur128 = ctx->priv;
     const int nb_channels = ebur128->nb_channels;
-    const int nb_samples  = insamples->audio->nb_samples;
+    const int nb_samples  = insamples->nb_samples;
     const double *samples = (double *)insamples->data[0];
-    AVFilterBufferRef *pic = ebur128->outpicref;
+    AVFrame *pic = ebur128->outpicref;
 
     for (idx_insample = 0; idx_insample < nb_samples; idx_insample++) {
         const int bin_id_400  = ebur128->i400.cache_pos;
@@ -639,13 +670,27 @@
 
                 /* set pts and push frame */
                 pic->pts = pts;
-                ret = ff_filter_frame(outlink, avfilter_ref_buffer(pic, ~AV_PERM_WRITE));
+                ret = ff_filter_frame(outlink, av_frame_clone(pic));
                 if (ret < 0)
                     return ret;
             }
 
-            av_log(ctx, ebur128->do_video ? AV_LOG_VERBOSE : AV_LOG_INFO,
-                   "t: %-10s " LOG_FMT "\n", av_ts2timestr(pts, &outlink->time_base),
+            if (ebur128->metadata) { /* happens only once per filter_frame call */
+                char metabuf[128];
+#define SET_META(name, var) do {                                            \
+    snprintf(metabuf, sizeof(metabuf), "%.3f", var);                        \
+    av_dict_set(&insamples->metadata, "lavfi.r128." name, metabuf, 0);      \
+} while (0)
+                SET_META("M",        loudness_400);
+                SET_META("S",        loudness_3000);
+                SET_META("I",        ebur128->integrated_loudness);
+                SET_META("LRA",      ebur128->loudness_range);
+                SET_META("LRA.low",  ebur128->lra_low);
+                SET_META("LRA.high", ebur128->lra_high);
+            }
+
+            av_log(ctx, ebur128->loglevel, "t: %-10s " LOG_FMT "\n",
+                   av_ts2timestr(pts, &outlink->time_base),
                    loudness_400, loudness_3000,
                    ebur128->integrated_loudness, ebur128->loudness_range);
         }
@@ -666,22 +711,6 @@
     static const int input_srate[] = {48000, -1}; // ITU-R BS.1770 provides coeff only for 48kHz
     static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE };
 
-    /* set input audio formats */
-    formats = ff_make_format_list(sample_fmts);
-    if (!formats)
-        return AVERROR(ENOMEM);
-    ff_formats_ref(formats, &inlink->out_formats);
-
-    layouts = ff_all_channel_layouts();
-    if (!layouts)
-        return AVERROR(ENOMEM);
-    ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
-
-    formats = ff_make_format_list(input_srate);
-    if (!formats)
-        return AVERROR(ENOMEM);
-    ff_formats_ref(formats, &inlink->out_samplerates);
-
     /* set optional output video format */
     if (ebur128->do_video) {
         formats = ff_make_format_list(pix_fmts);
@@ -691,20 +720,25 @@
         outlink = ctx->outputs[1];
     }
 
-    /* set audio output formats (same as input since it's just a passthrough) */
+    /* set input and output audio formats
+     * Note: ff_set_common_* functions are not used because they affect all the
+     * links, and thus break the video format negociation */
     formats = ff_make_format_list(sample_fmts);
     if (!formats)
         return AVERROR(ENOMEM);
+    ff_formats_ref(formats, &inlink->out_formats);
     ff_formats_ref(formats, &outlink->in_formats);
 
     layouts = ff_all_channel_layouts();
     if (!layouts)
         return AVERROR(ENOMEM);
+    ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
     ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
 
     formats = ff_make_format_list(input_srate);
     if (!formats)
         return AVERROR(ENOMEM);
+    ff_formats_ref(formats, &inlink->out_samplerates);
     ff_formats_ref(formats, &outlink->in_samplerates);
 
     return 0;
@@ -738,7 +772,7 @@
     }
     for (i = 0; i < ctx->nb_outputs; i++)
         av_freep(&ctx->output_pads[i].name);
-    avfilter_unref_bufferp(&ebur128->outpicref);
+    av_frame_free(&ebur128->outpicref);
 }
 
 static const AVFilterPad ebur128_inputs[] = {
@@ -747,6 +781,7 @@
         .type             = AVMEDIA_TYPE_AUDIO,
         .get_audio_buffer = ff_null_get_audio_buffer,
         .filter_frame     = filter_frame,
+        .config_props     = config_audio_input,
     },
     { NULL }
 };
@@ -761,4 +796,5 @@
     .inputs        = ebur128_inputs,
     .outputs       = NULL,
     .priv_class    = &ebur128_class,
+    .flags         = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
 };
diff --git a/libavfilter/f_interleave.c b/libavfilter/f_interleave.c
new file mode 100644
index 0000000..04d7677
--- /dev/null
+++ b/libavfilter/f_interleave.c
@@ -0,0 +1,259 @@
+/*
+ * Copyright (c) 2013 Stefano Sabatini
+ *
+ * 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
+ * audio and video interleaver
+ */
+
+#include "libavutil/avassert.h"
+#include "libavutil/avstring.h"
+#include "libavutil/opt.h"
+#include "avfilter.h"
+#include "bufferqueue.h"
+#include "formats.h"
+#include "internal.h"
+#include "audio.h"
+#include "video.h"
+
+typedef struct {
+    const AVClass *class;
+    int nb_inputs;
+    struct FFBufQueue *queues;
+} InterleaveContext;
+
+#define OFFSET(x) offsetof(InterleaveContext, x)
+
+#define DEFINE_OPTIONS(filt_name, flags_)                           \
+static const AVOption filt_name##_options[] = {                     \
+   { "nb_inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
+   { "n",         "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, .flags = flags_ }, \
+   { NULL },                                                        \
+}
+
+inline static int push_frame(AVFilterContext *ctx)
+{
+    InterleaveContext *interleave = ctx->priv;
+    AVFrame *frame;
+    int i, queue_idx = -1;
+    int64_t pts_min = INT64_MAX;
+
+    /* look for oldest frame */
+    for (i = 0; i < ctx->nb_inputs; i++) {
+        struct FFBufQueue *q = &interleave->queues[i];
+
+        if (!q->available && !ctx->inputs[i]->closed)
+            return 0;
+        if (q->available) {
+            frame = ff_bufqueue_peek(q, 0);
+            if (frame->pts < pts_min) {
+                pts_min = frame->pts;
+                queue_idx = i;
+            }
+        }
+    }
+
+    /* all inputs are closed */
+    if (queue_idx < 0)
+        return AVERROR_EOF;
+
+    frame = ff_bufqueue_get(&interleave->queues[queue_idx]);
+    av_log(ctx, AV_LOG_DEBUG, "queue:%d -> frame time:%f\n",
+           queue_idx, frame->pts * av_q2d(AV_TIME_BASE_Q));
+    return ff_filter_frame(ctx->outputs[0], frame);
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
+{
+    AVFilterContext *ctx = inlink->dst;
+    InterleaveContext *interleave = ctx->priv;
+    unsigned in_no = FF_INLINK_IDX(inlink);
+
+    if (frame->pts == AV_NOPTS_VALUE) {
+        av_log(ctx, AV_LOG_WARNING,
+               "NOPTS value for input frame cannot be accepted, frame discarded\n");
+        av_frame_free(&frame);
+        return AVERROR_INVALIDDATA;
+    }
+
+    /* queue frame */
+    frame->pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q);
+    av_log(ctx, AV_LOG_DEBUG, "frame pts:%f -> queue idx:%d available:%d\n",
+           frame->pts * av_q2d(AV_TIME_BASE_Q), in_no, interleave->queues[in_no].available);
+    ff_bufqueue_add(ctx, &interleave->queues[in_no], frame);
+
+    return push_frame(ctx);
+}
+
+static int init(AVFilterContext *ctx)
+{
+    InterleaveContext *interleave = ctx->priv;
+    const AVFilterPad *outpad = &ctx->filter->outputs[0];
+    int i;
+
+    interleave->queues = av_calloc(interleave->nb_inputs, sizeof(interleave->queues[0]));
+    if (!interleave->queues)
+        return AVERROR(ENOMEM);
+
+    for (i = 0; i < interleave->nb_inputs; i++) {
+        AVFilterPad inpad = { 0 };
+
+        inpad.name = av_asprintf("input%d", i);
+        if (!inpad.name)
+            return AVERROR(ENOMEM);
+        inpad.type         = outpad->type;
+        inpad.filter_frame = filter_frame;
+
+        switch (outpad->type) {
+        case AVMEDIA_TYPE_VIDEO:
+            inpad.get_video_buffer = ff_null_get_video_buffer; break;
+        case AVMEDIA_TYPE_AUDIO:
+            inpad.get_audio_buffer = ff_null_get_audio_buffer; break;
+        default:
+            av_assert0(0);
+        }
+        ff_insert_inpad(ctx, i, &inpad);
+    }
+
+    return 0;
+}
+
+static void uninit(AVFilterContext *ctx)
+{
+    InterleaveContext *interleave = ctx->priv;
+    int i;
+
+    for (i = 0; i < ctx->nb_inputs; i++) {
+        ff_bufqueue_discard_all(&interleave->queues[i]);
+        av_freep(&interleave->queues[i]);
+        av_freep(&ctx->input_pads[i].name);
+    }
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    AVFilterLink *inlink0 = ctx->inputs[0];
+    int i;
+
+    if (outlink->type == AVMEDIA_TYPE_VIDEO) {
+        outlink->time_base           = AV_TIME_BASE_Q;
+        outlink->w                   = inlink0->w;
+        outlink->h                   = inlink0->h;
+        outlink->sample_aspect_ratio = inlink0->sample_aspect_ratio;
+        outlink->format              = inlink0->format;
+        outlink->frame_rate = (AVRational) {1, 0};
+        for (i = 1; i < ctx->nb_inputs; i++) {
+            AVFilterLink *inlink = ctx->inputs[i];
+
+            if (outlink->w                       != inlink->w                       ||
+                outlink->h                       != inlink->h                       ||
+                outlink->sample_aspect_ratio.num != inlink->sample_aspect_ratio.num ||
+                outlink->sample_aspect_ratio.den != inlink->sample_aspect_ratio.den) {
+                av_log(ctx, AV_LOG_ERROR, "Parameters for input link %s "
+                       "(size %dx%d, SAR %d:%d) do not match the corresponding "
+                       "output link parameters (%dx%d, SAR %d:%d)\n",
+                       ctx->input_pads[i].name, inlink->w, inlink->h,
+                       inlink->sample_aspect_ratio.num,
+                       inlink->sample_aspect_ratio.den,
+                       outlink->w, outlink->h,
+                       outlink->sample_aspect_ratio.num,
+                       outlink->sample_aspect_ratio.den);
+                return AVERROR(EINVAL);
+            }
+        }
+    }
+
+    outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
+    return 0;
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    InterleaveContext *interleave = ctx->priv;
+    int i, ret;
+
+    for (i = 0; i < ctx->nb_inputs; i++) {
+        if (!interleave->queues[i].available && !ctx->inputs[i]->closed) {
+            ret = ff_request_frame(ctx->inputs[i]);
+            if (ret != AVERROR_EOF)
+                return ret;
+        }
+    }
+
+    return push_frame(ctx);
+}
+
+#if CONFIG_INTERLEAVE_FILTER
+
+DEFINE_OPTIONS(interleave, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
+AVFILTER_DEFINE_CLASS(interleave);
+
+static const AVFilterPad interleave_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .config_props  = config_output,
+        .request_frame = request_frame,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_vf_interleave = {
+    .name        = "interleave",
+    .description = NULL_IF_CONFIG_SMALL("Temporally interleave video inputs."),
+    .priv_size   = sizeof(InterleaveContext),
+    .init        = init,
+    .uninit      = uninit,
+    .outputs     = interleave_outputs,
+    .priv_class  = &interleave_class,
+    .flags       = AVFILTER_FLAG_DYNAMIC_INPUTS,
+};
+
+#endif
+
+#if CONFIG_AINTERLEAVE_FILTER
+
+DEFINE_OPTIONS(ainterleave, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
+AVFILTER_DEFINE_CLASS(ainterleave);
+
+static const AVFilterPad ainterleave_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_AUDIO,
+        .config_props  = config_output,
+        .request_frame = request_frame,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_af_ainterleave = {
+    .name        = "ainterleave",
+    .description = NULL_IF_CONFIG_SMALL("Temporally interleave audio inputs."),
+    .priv_size   = sizeof(InterleaveContext),
+    .init        = init,
+    .uninit      = uninit,
+    .outputs     = ainterleave_outputs,
+    .priv_class  = &ainterleave_class,
+    .flags       = AVFILTER_FLAG_DYNAMIC_INPUTS,
+};
+
+#endif
diff --git a/libavfilter/f_perms.c b/libavfilter/f_perms.c
new file mode 100644
index 0000000..8dc2ed8
--- /dev/null
+++ b/libavfilter/f_perms.c
@@ -0,0 +1,178 @@
+/*
+ * Copyright (c) 2013 Clément Bœsch
+ *
+ * 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 "libavutil/lfg.h"
+#include "libavutil/opt.h"
+#include "libavutil/random_seed.h"
+#include "audio.h"
+#include "video.h"
+
+enum mode {
+    MODE_NONE,
+    MODE_RO,
+    MODE_RW,
+    MODE_TOGGLE,
+    MODE_RANDOM,
+    NB_MODES
+};
+
+typedef struct {
+    const AVClass *class;
+    AVLFG lfg;
+    int64_t random_seed;
+    enum mode mode;
+} PermsContext;
+
+#define OFFSET(x) offsetof(PermsContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption options[] = {
+    { "mode", "select permissions mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64 = MODE_NONE}, MODE_NONE, NB_MODES-1, FLAGS, "mode" },
+        { "none",   "do nothing",                       0, AV_OPT_TYPE_CONST, {.i64 = MODE_NONE},       INT_MIN, INT_MAX, FLAGS, "mode" },
+        { "ro",     "set all output frames read-only",  0, AV_OPT_TYPE_CONST, {.i64 = MODE_RO},         INT_MIN, INT_MAX, FLAGS, "mode" },
+        { "rw",     "set all output frames writable",   0, AV_OPT_TYPE_CONST, {.i64 = MODE_RW},         INT_MIN, INT_MAX, FLAGS, "mode" },
+        { "toggle", "switch permissions",               0, AV_OPT_TYPE_CONST, {.i64 = MODE_TOGGLE},     INT_MIN, INT_MAX, FLAGS, "mode" },
+        { "random", "set permissions randomly",         0, AV_OPT_TYPE_CONST, {.i64 = MODE_RANDOM},     INT_MIN, INT_MAX, FLAGS, "mode" },
+    { "seed", "set the seed for the random mode", OFFSET(random_seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
+    { NULL }
+};
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    PermsContext *perms = ctx->priv;
+
+    if (perms->mode == MODE_RANDOM) {
+        uint32_t seed;
+
+        if (perms->random_seed == -1)
+            perms->random_seed = av_get_random_seed();
+        seed = perms->random_seed;
+        av_log(ctx, AV_LOG_INFO, "random seed: 0x%08x\n", seed);
+        av_lfg_init(&perms->lfg, seed);
+    }
+
+    return 0;
+}
+
+enum perm                        {  RO,   RW  };
+static const char *perm_str[2] = { "RO", "RW" };
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
+{
+    int ret;
+    AVFilterContext *ctx = inlink->dst;
+    PermsContext *perms = ctx->priv;
+    AVFrame *out = frame;
+    enum perm in_perm = av_frame_is_writable(frame) ? RW : RO;
+    enum perm out_perm;
+
+    switch (perms->mode) {
+    case MODE_TOGGLE:   out_perm = in_perm == RO ? RW : RO;                 break;
+    case MODE_RANDOM:   out_perm = av_lfg_get(&perms->lfg) & 1 ? RW : RO;   break;
+    case MODE_RO:       out_perm = RO;                                      break;
+    case MODE_RW:       out_perm = RW;                                      break;
+    default:            out_perm = in_perm;                                 break;
+    }
+
+    av_log(ctx, AV_LOG_VERBOSE, "%s -> %s%s\n",
+           perm_str[in_perm], perm_str[out_perm],
+           in_perm == out_perm ? " (no-op)" : "");
+
+    if (in_perm == RO && out_perm == RW) {
+        if ((ret = av_frame_make_writable(frame)) < 0)
+            return ret;
+    } else if (in_perm == RW && out_perm == RO) {
+        out = av_frame_clone(frame);
+        if (!out)
+            return AVERROR(ENOMEM);
+    }
+
+    ret = ff_filter_frame(ctx->outputs[0], out);
+
+    if (in_perm == RW && out_perm == RO)
+        av_frame_free(&frame);
+    return ret;
+}
+
+#if CONFIG_APERMS_FILTER
+
+#define aperms_options options
+AVFILTER_DEFINE_CLASS(aperms);
+
+static const AVFilterPad aperms_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_AUDIO,
+        .filter_frame = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad aperms_outputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_AUDIO,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_af_aperms = {
+    .name        = "aperms",
+    .description = NULL_IF_CONFIG_SMALL("Set permissions for the output audio frame."),
+    .init        = init,
+    .priv_size   = sizeof(PermsContext),
+    .inputs      = aperms_inputs,
+    .outputs     = aperms_outputs,
+    .priv_class  = &aperms_class,
+};
+#endif /* CONFIG_APERMS_FILTER */
+
+#if CONFIG_PERMS_FILTER
+
+#define perms_options options
+AVFILTER_DEFINE_CLASS(perms);
+
+static const AVFilterPad perms_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad perms_outputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_vf_perms = {
+    .name        = "perms",
+    .description = NULL_IF_CONFIG_SMALL("Set permissions for the output video frame."),
+    .init        = init,
+    .priv_size   = sizeof(PermsContext),
+    .inputs      = perms_inputs,
+    .outputs     = perms_outputs,
+    .priv_class  = &perms_class,
+};
+#endif /* CONFIG_PERMS_FILTER */
diff --git a/libavfilter/f_select.c b/libavfilter/f_select.c
index 0f21145..b0df507 100644
--- a/libavfilter/f_select.c
+++ b/libavfilter/f_select.c
@@ -23,6 +23,7 @@
  * filter for selecting which frame passes in the filterchain
  */
 
+#include "libavutil/avstring.h"
 #include "libavutil/eval.h"
 #include "libavutil/fifo.h"
 #include "libavutil/internal.h"
@@ -125,8 +126,8 @@
 
 typedef struct {
     const AVClass *class;
-    AVExpr *expr;
     char *expr_str;
+    AVExpr *expr;
     double var_values[VAR_VARS_NB];
     int do_scene_detect;            ///< 1 if the expression requires scene detection variables, 0 otherwise
 #if CONFIG_AVCODEC
@@ -134,37 +135,48 @@
     DSPContext c;                   ///< context providing optimized SAD methods   (scene detect only)
     double prev_mafd;               ///< previous MAFD                             (scene detect only)
 #endif
-    AVFilterBufferRef *prev_picref; ///< previous frame                            (scene detect only)
+    AVFrame *prev_picref; ///< previous frame                            (scene detect only)
     double select;
+    int select_out;                 ///< mark the selected output pad index
+    int nb_outputs;
 } SelectContext;
 
 #define OFFSET(x) offsetof(SelectContext, x)
-#define FLAGS AV_OPT_FLAG_FILTERING_PARAM
-static const AVOption options[] = {
-    { "expr", "set selection expression", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str = "1"}, 0, 0, FLAGS },
-    { "e",    "set selection expression", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str = "1"}, 0, 0, FLAGS },
-    {NULL},
-};
+#define DEFINE_OPTIONS(filt_name, FLAGS)                            \
+static const AVOption filt_name##_options[] = {                     \
+    { "expr", "set an expression to use for selecting frames", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "1" }, .flags=FLAGS }, \
+    { "e",    "set an expression to use for selecting frames", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "1" }, .flags=FLAGS }, \
+    { "outputs", "set the number of outputs", OFFSET(nb_outputs), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, .flags=FLAGS }, \
+    { "n",       "set the number of outputs", OFFSET(nb_outputs), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, .flags=FLAGS }, \
+    { NULL }                                                            \
+}
 
-static av_cold int init(AVFilterContext *ctx, const char *args, const AVClass *class)
+static int request_frame(AVFilterLink *outlink);
+
+static av_cold int init(AVFilterContext *ctx)
 {
     SelectContext *select = ctx->priv;
-    const char *shorthand[] = { "expr", NULL };
-    int ret;
-
-    select->class = class;
-    av_opt_set_defaults(select);
-
-    if ((ret = av_opt_set_from_string(select, args, shorthand, "=", ":")) < 0)
-        return ret;
+    int i, ret;
 
     if ((ret = av_expr_parse(&select->expr, select->expr_str,
                              var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", select->expr_str);
+        av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n",
+               select->expr_str);
         return ret;
     }
     select->do_scene_detect = !!strstr(select->expr_str, "scene");
 
+    for (i = 0; i < select->nb_outputs; i++) {
+        AVFilterPad pad = { 0 };
+
+        pad.name = av_asprintf("output%d", i);
+        if (!pad.name)
+            return AVERROR(ENOMEM);
+        pad.type = ctx->filter->inputs[0].type;
+        pad.request_frame = request_frame;
+        ff_insert_outpad(ctx, i, &pad);
+    }
+
     return 0;
 }
 
@@ -212,32 +224,32 @@
         select->avctx = avcodec_alloc_context3(NULL);
         if (!select->avctx)
             return AVERROR(ENOMEM);
-        dsputil_init(&select->c, select->avctx);
+        avpriv_dsputil_init(&select->c, select->avctx);
     }
 #endif
     return 0;
 }
 
 #if CONFIG_AVCODEC
-static double get_scene_score(AVFilterContext *ctx, AVFilterBufferRef *picref)
+static double get_scene_score(AVFilterContext *ctx, AVFrame *frame)
 {
     double ret = 0;
     SelectContext *select = ctx->priv;
-    AVFilterBufferRef *prev_picref = select->prev_picref;
+    AVFrame *prev_picref = select->prev_picref;
 
     if (prev_picref &&
-        picref->video->h    == prev_picref->video->h &&
-        picref->video->w    == prev_picref->video->w &&
-        picref->linesize[0] == prev_picref->linesize[0]) {
+        frame->height    == prev_picref->height &&
+        frame->width    == prev_picref->width &&
+        frame->linesize[0] == prev_picref->linesize[0]) {
         int x, y, nb_sad = 0;
         int64_t sad = 0;
         double mafd, diff;
-        uint8_t *p1 =      picref->data[0];
+        uint8_t *p1 =      frame->data[0];
         uint8_t *p2 = prev_picref->data[0];
-        const int linesize = picref->linesize[0];
+        const int linesize = frame->linesize[0];
 
-        for (y = 0; y < picref->video->h - 8; y += 8) {
-            for (x = 0; x < picref->video->w*3 - 8; x += 8) {
+        for (y = 0; y < frame->height - 8; y += 8) {
+            for (x = 0; x < frame->width*3 - 8; x += 8) {
                 sad += select->c.sad[1](select, p1 + x, p2 + x,
                                         linesize, 8);
                 nb_sad += 8 * 8;
@@ -250,9 +262,9 @@
         diff = fabs(mafd - select->prev_mafd);
         ret  = av_clipf(FFMIN(mafd, diff) / 100., 0, 1);
         select->prev_mafd = mafd;
-        avfilter_unref_buffer(prev_picref);
+        av_frame_free(&prev_picref);
     }
-    select->prev_picref = avfilter_ref_buffer(picref, ~0);
+    select->prev_picref = av_frame_clone(frame);
     return ret;
 }
 #endif
@@ -260,50 +272,50 @@
 #define D2TS(d)  (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
 
-static int select_frame(AVFilterContext *ctx, AVFilterBufferRef *ref)
+static void select_frame(AVFilterContext *ctx, AVFrame *frame)
 {
     SelectContext *select = ctx->priv;
     AVFilterLink *inlink = ctx->inputs[0];
     double res;
 
     if (isnan(select->var_values[VAR_START_PTS]))
-        select->var_values[VAR_START_PTS] = TS2D(ref->pts);
+        select->var_values[VAR_START_PTS] = TS2D(frame->pts);
     if (isnan(select->var_values[VAR_START_T]))
-        select->var_values[VAR_START_T] = TS2D(ref->pts) * av_q2d(inlink->time_base);
+        select->var_values[VAR_START_T] = TS2D(frame->pts) * av_q2d(inlink->time_base);
 
-    select->var_values[VAR_PTS] = TS2D(ref->pts);
-    select->var_values[VAR_T  ] = TS2D(ref->pts) * av_q2d(inlink->time_base);
-    select->var_values[VAR_POS] = ref->pos == -1 ? NAN : ref->pos;
+    select->var_values[VAR_N  ] = inlink->frame_count;
+    select->var_values[VAR_PTS] = TS2D(frame->pts);
+    select->var_values[VAR_T  ] = TS2D(frame->pts) * av_q2d(inlink->time_base);
+    select->var_values[VAR_POS] = av_frame_get_pkt_pos(frame) == -1 ? NAN : av_frame_get_pkt_pos(frame);
 
     switch (inlink->type) {
     case AVMEDIA_TYPE_AUDIO:
-        select->var_values[VAR_SAMPLES_N] = ref->audio->nb_samples;
+        select->var_values[VAR_SAMPLES_N] = frame->nb_samples;
         break;
 
     case AVMEDIA_TYPE_VIDEO:
         select->var_values[VAR_INTERLACE_TYPE] =
-            !ref->video->interlaced ? INTERLACE_TYPE_P :
-        ref->video->top_field_first ? INTERLACE_TYPE_T : INTERLACE_TYPE_B;
-        select->var_values[VAR_PICT_TYPE] = ref->video->pict_type;
+            !frame->interlaced_frame ? INTERLACE_TYPE_P :
+        frame->top_field_first ? INTERLACE_TYPE_T : INTERLACE_TYPE_B;
+        select->var_values[VAR_PICT_TYPE] = frame->pict_type;
 #if CONFIG_AVCODEC
         if (select->do_scene_detect) {
             char buf[32];
-            select->var_values[VAR_SCENE] = get_scene_score(ctx, ref);
+            select->var_values[VAR_SCENE] = get_scene_score(ctx, frame);
             // TODO: document metadata
             snprintf(buf, sizeof(buf), "%f", select->var_values[VAR_SCENE]);
-            av_dict_set(&ref->metadata, "lavfi.scene_score", buf, 0);
+            av_dict_set(avpriv_frame_get_metadatap(frame), "lavfi.scene_score", buf, 0);
         }
 #endif
         break;
     }
 
-    res = av_expr_eval(select->expr, select->var_values, NULL);
+    select->select = res = av_expr_eval(select->expr, select->var_values, NULL);
     av_log(inlink->dst, AV_LOG_DEBUG,
-           "n:%f pts:%f t:%f pos:%f key:%d",
+           "n:%f pts:%f t:%f key:%d",
            select->var_values[VAR_N],
            select->var_values[VAR_PTS],
            select->var_values[VAR_T],
-           select->var_values[VAR_POS],
            (int)select->var_values[VAR_KEY]);
 
     switch (inlink->type) {
@@ -322,7 +334,15 @@
         break;
     }
 
-    av_log(inlink->dst, AV_LOG_DEBUG, " -> select:%f\n", res);
+    if (res == 0) {
+        select->select_out = -1; /* drop */
+    } else if (isnan(res) || res < 0) {
+        select->select_out = 0; /* first output */
+    } else {
+        select->select_out = FFMIN(ceilf(res)-1, select->nb_outputs-1); /* other outputs */
+    }
+
+    av_log(inlink->dst, AV_LOG_DEBUG, " -> select:%f select_out:%d\n", res, select->select_out);
 
     if (res) {
         select->var_values[VAR_PREV_SELECTED_N]   = select->var_values[VAR_N];
@@ -330,25 +350,23 @@
         select->var_values[VAR_PREV_SELECTED_T]   = select->var_values[VAR_T];
         select->var_values[VAR_SELECTED_N] += 1.0;
         if (inlink->type == AVMEDIA_TYPE_AUDIO)
-            select->var_values[VAR_CONSUMED_SAMPLES_N] += ref->audio->nb_samples;
+            select->var_values[VAR_CONSUMED_SAMPLES_N] += frame->nb_samples;
     }
 
-    select->var_values[VAR_N] += 1.0;
     select->var_values[VAR_PREV_PTS] = select->var_values[VAR_PTS];
     select->var_values[VAR_PREV_T]   = select->var_values[VAR_T];
-
-    return res;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
 {
-    SelectContext *select = inlink->dst->priv;
+    AVFilterContext *ctx = inlink->dst;
+    SelectContext *select = ctx->priv;
 
-    select->select = select_frame(inlink->dst, frame);
+    select_frame(ctx, frame);
     if (select->select)
-        return ff_filter_frame(inlink->dst->outputs[0], frame);
+        return ff_filter_frame(ctx->outputs[select->select_out], frame);
 
-    avfilter_unref_bufferp(&frame);
+    av_frame_free(&frame);
     return 0;
 }
 
@@ -357,13 +375,13 @@
     AVFilterContext *ctx = outlink->src;
     SelectContext *select = ctx->priv;
     AVFilterLink *inlink = outlink->src->inputs[0];
-    select->select = 0;
+    int out_no = FF_OUTLINK_IDX(outlink);
 
     do {
         int ret = ff_request_frame(inlink);
         if (ret < 0)
             return ret;
-    } while (!select->select);
+    } while (select->select_out != out_no);
 
     return 0;
 }
@@ -371,14 +389,17 @@
 static av_cold void uninit(AVFilterContext *ctx)
 {
     SelectContext *select = ctx->priv;
+    int i;
 
     av_expr_free(select->expr);
     select->expr = NULL;
-    av_opt_free(select);
+
+    for (i = 0; i < ctx->nb_outputs; i++)
+        av_freep(&ctx->output_pads[i].name);
 
 #if CONFIG_AVCODEC
     if (select->do_scene_detect) {
-        avfilter_unref_bufferp(&select->prev_picref);
+        av_frame_free(&select->prev_picref);
         if (select->avctx) {
             avcodec_close(select->avctx);
             av_freep(&select->avctx);
@@ -405,15 +426,15 @@
 
 #if CONFIG_ASELECT_FILTER
 
-#define aselect_options options
+DEFINE_OPTIONS(aselect, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
 AVFILTER_DEFINE_CLASS(aselect);
 
-static av_cold int aselect_init(AVFilterContext *ctx, const char *args)
+static av_cold int aselect_init(AVFilterContext *ctx)
 {
     SelectContext *select = ctx->priv;
     int ret;
 
-    if ((ret = init(ctx, args, &aselect_class)) < 0)
+    if ((ret = init(ctx)) < 0)
         return ret;
 
     if (select->do_scene_detect) {
@@ -435,14 +456,6 @@
     { NULL }
 };
 
-static const AVFilterPad avfilter_af_aselect_outputs[] = {
-    {
-        .name = "default",
-        .type = AVMEDIA_TYPE_AUDIO,
-    },
-    { NULL }
-};
-
 AVFilter avfilter_af_aselect = {
     .name      = "aselect",
     .description = NULL_IF_CONFIG_SMALL("Select audio frames to pass in output."),
@@ -450,22 +463,22 @@
     .uninit    = uninit,
     .priv_size = sizeof(SelectContext),
     .inputs    = avfilter_af_aselect_inputs,
-    .outputs   = avfilter_af_aselect_outputs,
     .priv_class = &aselect_class,
+    .flags     = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
 };
 #endif /* CONFIG_ASELECT_FILTER */
 
 #if CONFIG_SELECT_FILTER
 
-#define select_options options
+DEFINE_OPTIONS(select, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM);
 AVFILTER_DEFINE_CLASS(select);
 
-static av_cold int select_init(AVFilterContext *ctx, const char *args)
+static av_cold int select_init(AVFilterContext *ctx)
 {
     SelectContext *select = ctx->priv;
     int ret;
 
-    if ((ret = init(ctx, args, &select_class)) < 0)
+    if ((ret = init(ctx)) < 0)
         return ret;
 
     if (select->do_scene_detect && !CONFIG_AVCODEC) {
@@ -481,22 +494,12 @@
         .name             = "default",
         .type             = AVMEDIA_TYPE_VIDEO,
         .get_video_buffer = ff_null_get_video_buffer,
-        .min_perms        = AV_PERM_PRESERVE,
         .config_props     = config_input,
         .filter_frame     = filter_frame,
     },
     { NULL }
 };
 
-static const AVFilterPad avfilter_vf_select_outputs[] = {
-    {
-        .name          = "default",
-        .type          = AVMEDIA_TYPE_VIDEO,
-        .request_frame = request_frame,
-    },
-    { NULL }
-};
-
 AVFilter avfilter_vf_select = {
     .name      = "select",
     .description = NULL_IF_CONFIG_SMALL("Select video frames to pass in output."),
@@ -505,9 +508,9 @@
     .query_formats = query_formats,
 
     .priv_size = sizeof(SelectContext),
+    .priv_class = &select_class,
 
     .inputs    = avfilter_vf_select_inputs,
-    .outputs   = avfilter_vf_select_outputs,
-    .priv_class = &select_class,
+    .flags     = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
 };
 #endif /* CONFIG_SELECT_FILTER */
diff --git a/libavfilter/f_sendcmd.c b/libavfilter/f_sendcmd.c
index b5cf01c..319d503 100644
--- a/libavfilter/f_sendcmd.c
+++ b/libavfilter/f_sendcmd.c
@@ -80,7 +80,7 @@
 } SendCmdContext;
 
 #define OFFSET(x) offsetof(SendCmdContext, x)
-#define FLAGS AV_OPT_FLAG_FILTERING_PARAM
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
 static const AVOption options[] = {
     { "commands", "set commands", OFFSET(commands_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
     { "c",        "set commands", OFFSET(commands_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
@@ -134,7 +134,7 @@
                 char flag_buf[64];
                 av_strlcpy(flag_buf, *buf, sizeof(flag_buf));
                 av_log(log_ctx, AV_LOG_ERROR,
-                       "Unknown flag '%s' in in interval #%d, command #%d\n",
+                       "Unknown flag '%s' in interval #%d, command #%d\n",
                        flag_buf, interval_count, cmd_count);
                 return AVERROR(EINVAL);
             }
@@ -166,7 +166,7 @@
     cmd->target = av_get_token(buf, COMMAND_DELIMS);
     if (!cmd->target || !cmd->target[0]) {
         av_log(log_ctx, AV_LOG_ERROR,
-               "No target specified in in interval #%d, command #%d\n",
+               "No target specified in interval #%d, command #%d\n",
                interval_count, cmd_count);
         ret = AVERROR(EINVAL);
         goto fail;
@@ -176,7 +176,7 @@
     cmd->command = av_get_token(buf, COMMAND_DELIMS);
     if (!cmd->command || !cmd->command[0]) {
         av_log(log_ctx, AV_LOG_ERROR,
-               "No command specified in in interval #%d, command #%d\n",
+               "No command specified in interval #%d, command #%d\n",
                interval_count, cmd_count);
         ret = AVERROR(EINVAL);
         goto fail;
@@ -368,17 +368,11 @@
     return ret == 0 ? i1->index - i2->index : ret;
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args, const AVClass *class)
+static av_cold int init(AVFilterContext *ctx)
 {
     SendCmdContext *sendcmd = ctx->priv;
     int ret, i, j;
 
-    sendcmd->class = class;
-    av_opt_set_defaults(sendcmd);
-
-    if ((ret = av_set_options_string(sendcmd, args, "=", ":")) < 0)
-        return ret;
-
     if (sendcmd->commands_filename && sendcmd->commands_str) {
         av_log(ctx, AV_LOG_ERROR,
                "Only one of the filename or commands options must be specified\n");
@@ -433,8 +427,6 @@
     SendCmdContext *sendcmd = ctx->priv;
     int i, j;
 
-    av_opt_free(sendcmd);
-
     for (i = 0; i < sendcmd->nb_intervals; i++) {
         Interval *interval = &sendcmd->intervals[i];
         for (j = 0; j < interval->nb_commands; j++) {
@@ -448,7 +440,7 @@
     av_freep(&sendcmd->intervals);
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *ref)
+static int filter_frame(AVFilterLink *inlink, AVFrame *ref)
 {
     AVFilterContext *ctx = inlink->dst;
     SendCmdContext *sendcmd = ctx->priv;
@@ -518,11 +510,6 @@
 #define sendcmd_options options
 AVFILTER_DEFINE_CLASS(sendcmd);
 
-static av_cold int sendcmd_init(AVFilterContext *ctx, const char *args)
-{
-    return init(ctx, args, &sendcmd_class);
-}
-
 static const AVFilterPad sendcmd_inputs[] = {
     {
         .name             = "default",
@@ -544,8 +531,7 @@
 AVFilter avfilter_vf_sendcmd = {
     .name      = "sendcmd",
     .description = NULL_IF_CONFIG_SMALL("Send commands to filters."),
-
-    .init = sendcmd_init,
+    .init   = init,
     .uninit = uninit,
     .priv_size = sizeof(SendCmdContext),
     .inputs    = sendcmd_inputs,
@@ -560,11 +546,6 @@
 #define asendcmd_options options
 AVFILTER_DEFINE_CLASS(asendcmd);
 
-static av_cold int asendcmd_init(AVFilterContext *ctx, const char *args)
-{
-    return init(ctx, args, &asendcmd_class);
-}
-
 static const AVFilterPad asendcmd_inputs[] = {
     {
         .name             = "default",
@@ -586,8 +567,7 @@
 AVFilter avfilter_af_asendcmd = {
     .name      = "asendcmd",
     .description = NULL_IF_CONFIG_SMALL("Send commands to filters."),
-
-    .init = asendcmd_init,
+    .init   = init,
     .uninit = uninit,
     .priv_size = sizeof(SendCmdContext),
     .inputs    = asendcmd_inputs,
diff --git a/libavfilter/f_setpts.c b/libavfilter/f_setpts.c
index 1c2edb8..c0fd422 100644
--- a/libavfilter/f_setpts.c
+++ b/libavfilter/f_setpts.c
@@ -27,6 +27,7 @@
 #include "libavutil/eval.h"
 #include "libavutil/internal.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/opt.h"
 #include "libavutil/time.h"
 #include "avfilter.h"
 #include "internal.h"
@@ -78,19 +79,21 @@
 };
 
 typedef struct {
+    const AVClass *class;
+    char *expr_str;
     AVExpr *expr;
     double var_values[VAR_VARS_NB];
     enum AVMediaType type;
 } SetPTSContext;
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     SetPTSContext *setpts = ctx->priv;
     int ret;
 
-    if ((ret = av_expr_parse(&setpts->expr, args ? args : "PTS",
+    if ((ret = av_expr_parse(&setpts->expr, setpts->expr_str,
                              var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
+        av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", setpts->expr_str);
         return ret;
     }
 
@@ -138,7 +141,7 @@
 
 #define d2istr(v) double2int64str((char[BUF_SIZE]){0}, v)
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
 {
     SetPTSContext *setpts = inlink->dst->priv;
     int64_t in_pts = frame->pts;
@@ -150,16 +153,16 @@
     }
     setpts->var_values[VAR_PTS       ] = TS2D(frame->pts);
     setpts->var_values[VAR_T         ] = TS2T(frame->pts, inlink->time_base);
-    setpts->var_values[VAR_POS       ] = frame->pos == -1 ? NAN : frame->pos;
+    setpts->var_values[VAR_POS       ] = av_frame_get_pkt_pos(frame) == -1 ? NAN : av_frame_get_pkt_pos(frame);
     setpts->var_values[VAR_RTCTIME   ] = av_gettime();
 
     switch (inlink->type) {
     case AVMEDIA_TYPE_VIDEO:
-        setpts->var_values[VAR_INTERLACED] = frame->video->interlaced;
+        setpts->var_values[VAR_INTERLACED] = frame->interlaced_frame;
         break;
 
     case AVMEDIA_TYPE_AUDIO:
-        setpts->var_values[VAR_NB_SAMPLES] = frame->audio->nb_samples;
+        setpts->var_values[VAR_NB_SAMPLES] = frame->nb_samples;
         break;
     }
 
@@ -192,7 +195,7 @@
     setpts->var_values[VAR_PREV_OUTT]   = TS2T(frame->pts, inlink->time_base);
     setpts->var_values[VAR_N] += 1.0;
     if (setpts->type == AVMEDIA_TYPE_AUDIO) {
-        setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += frame->audio->nb_samples;
+        setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += frame->nb_samples;
     }
     return ff_filter_frame(inlink->dst->outputs[0], frame);
 }
@@ -205,6 +208,21 @@
 }
 
 #if CONFIG_ASETPTS_FILTER
+
+#define OFFSET(x) offsetof(SetPTSContext, x)
+#define AFLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
+static const AVOption aoptions[] = {
+    { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = AFLAGS },
+    { NULL },
+};
+
+static const AVClass asetpts_class = {
+    .class_name = "asetpts",
+    .item_name  = av_default_item_name,
+    .option     = aoptions,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 static const AVFilterPad avfilter_af_asetpts_inputs[] = {
     {
         .name             = "default",
@@ -230,12 +248,28 @@
     .init      = init,
     .uninit    = uninit,
     .priv_size = sizeof(SetPTSContext),
+    .priv_class= &asetpts_class,
     .inputs    = avfilter_af_asetpts_inputs,
     .outputs   = avfilter_af_asetpts_outputs,
 };
 #endif /* CONFIG_ASETPTS_FILTER */
 
 #if CONFIG_SETPTS_FILTER
+
+#define OFFSET(x) offsetof(SetPTSContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
+static const AVOption options[] = {
+    { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = FLAGS },
+    { NULL },
+};
+
+static const AVClass setpts_class = {
+    .class_name = "setpts",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 static const AVFilterPad avfilter_vf_setpts_inputs[] = {
     {
         .name             = "default",
@@ -262,6 +296,7 @@
     .uninit    = uninit,
 
     .priv_size = sizeof(SetPTSContext),
+    .priv_class = &setpts_class,
 
     .inputs    = avfilter_vf_setpts_inputs,
     .outputs   = avfilter_vf_setpts_outputs,
diff --git a/libavfilter/f_settb.c b/libavfilter/f_settb.c
index 99ea7a7..b1eac71 100644
--- a/libavfilter/f_settb.c
+++ b/libavfilter/f_settb.c
@@ -30,6 +30,7 @@
 #include "libavutil/eval.h"
 #include "libavutil/internal.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/opt.h"
 #include "libavutil/rational.h"
 #include "avfilter.h"
 #include "internal.h"
@@ -51,19 +52,19 @@
 };
 
 typedef struct {
-    char tb_expr[256];
+    const AVClass *class;
+    char *tb_expr;
     double var_values[VAR_VARS_NB];
 } SetTBContext;
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    SetTBContext *settb = ctx->priv;
-    av_strlcpy(settb->tb_expr, "intb", sizeof(settb->tb_expr));
-
-    if (args)
-        sscanf(args, "%255[^:]", settb->tb_expr);
-
-    return 0;
+#define OFFSET(x) offsetof(SetTBContext, x)
+#define DEFINE_OPTIONS(filt_name, filt_type)                                               \
+static const AVOption filt_name##_options[] = {                                            \
+    { "expr", "set expression determining the output timebase", OFFSET(tb_expr), AV_OPT_TYPE_STRING, {.str="intb"}, \
+           .flags=AV_OPT_FLAG_##filt_type##_PARAM|AV_OPT_FLAG_FILTERING_PARAM },           \
+    { "tb",   "set expression determining the output timebase", OFFSET(tb_expr), AV_OPT_TYPE_STRING, {.str="intb"}, \
+           .flags=AV_OPT_FLAG_##filt_type##_PARAM|AV_OPT_FLAG_FILTERING_PARAM },           \
+    { NULL }                                                                               \
 }
 
 static int config_output_props(AVFilterLink *outlink)
@@ -103,7 +104,7 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
 {
     AVFilterContext *ctx = inlink->dst;
     AVFilterLink *outlink = ctx->outputs[0];
@@ -120,6 +121,10 @@
 }
 
 #if CONFIG_SETTB_FILTER
+
+DEFINE_OPTIONS(settb, VIDEO);
+AVFILTER_DEFINE_CLASS(settb);
+
 static const AVFilterPad avfilter_vf_settb_inputs[] = {
     {
         .name             = "default",
@@ -142,9 +147,9 @@
 AVFilter avfilter_vf_settb = {
     .name      = "settb",
     .description = NULL_IF_CONFIG_SMALL("Set timebase for the video output link."),
-    .init      = init,
 
     .priv_size = sizeof(SetTBContext),
+    .priv_class = &settb_class,
 
     .inputs    = avfilter_vf_settb_inputs,
     .outputs   = avfilter_vf_settb_outputs,
@@ -152,6 +157,10 @@
 #endif
 
 #if CONFIG_ASETTB_FILTER
+
+DEFINE_OPTIONS(asettb, AUDIO);
+AVFILTER_DEFINE_CLASS(asettb);
+
 static const AVFilterPad avfilter_af_asettb_inputs[] = {
     {
         .name             = "default",
@@ -174,10 +183,10 @@
 AVFilter avfilter_af_asettb = {
     .name      = "asettb",
     .description = NULL_IF_CONFIG_SMALL("Set timebase for the audio output link."),
-    .init      = init,
 
     .priv_size = sizeof(SetTBContext),
     .inputs    = avfilter_af_asettb_inputs,
     .outputs   = avfilter_af_asettb_outputs,
+    .priv_class = &asettb_class,
 };
 #endif
diff --git a/libavfilter/fifo.c b/libavfilter/fifo.c
index 9597fb3..b06720e 100644
--- a/libavfilter/fifo.c
+++ b/libavfilter/fifo.c
@@ -35,7 +35,7 @@
 #include "video.h"
 
 typedef struct Buf {
-    AVFilterBufferRef *buf;
+    AVFrame *frame;
     struct Buf        *next;
 } Buf;
 
@@ -47,11 +47,11 @@
      * When a specific number of output samples is requested, the partial
      * buffer is stored here
      */
-    AVFilterBufferRef *buf_out;
-    int allocated_samples;      ///< number of samples buf_out was allocated for
+    AVFrame *out;
+    int allocated_samples;      ///< number of samples out was allocated for
 } FifoContext;
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     FifoContext *fifo = ctx->priv;
     fifo->last = &fifo->root;
@@ -66,25 +66,25 @@
 
     for (buf = fifo->root.next; buf; buf = tmp) {
         tmp = buf->next;
-        avfilter_unref_bufferp(&buf->buf);
+        av_frame_free(&buf->frame);
         av_free(buf);
     }
 
-    avfilter_unref_bufferp(&fifo->buf_out);
+    av_frame_free(&fifo->out);
 }
 
-static int add_to_queue(AVFilterLink *inlink, AVFilterBufferRef *buf)
+static int add_to_queue(AVFilterLink *inlink, AVFrame *frame)
 {
     FifoContext *fifo = inlink->dst->priv;
 
     fifo->last->next = av_mallocz(sizeof(Buf));
     if (!fifo->last->next) {
-        avfilter_unref_buffer(buf);
+        av_frame_free(&frame);
         return AVERROR(ENOMEM);
     }
 
     fifo->last = fifo->last->next;
-    fifo->last->buf = buf;
+    fifo->last->frame = frame;
 
     return 0;
 }
@@ -101,7 +101,7 @@
 /**
  * Move data pointers and pts offset samples forward.
  */
-static void buffer_offset(AVFilterLink *link, AVFilterBufferRef *buf,
+static void buffer_offset(AVFilterLink *link, AVFrame *frame,
                           int offset)
 {
     int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
@@ -110,32 +110,32 @@
     int block_align = av_get_bytes_per_sample(link->format) * (planar ? 1 : nb_channels);
     int i;
 
-    av_assert0(buf->audio->nb_samples > offset);
+    av_assert0(frame->nb_samples > offset);
 
     for (i = 0; i < planes; i++)
-        buf->extended_data[i] += block_align*offset;
-    if (buf->data != buf->extended_data)
-        memcpy(buf->data, buf->extended_data,
-               FFMIN(planes, FF_ARRAY_ELEMS(buf->data)) * sizeof(*buf->data));
-    buf->linesize[0] -= block_align*offset;
-    buf->audio->nb_samples -= offset;
+        frame->extended_data[i] += block_align * offset;
+    if (frame->data != frame->extended_data)
+        memcpy(frame->data, frame->extended_data,
+               FFMIN(planes, FF_ARRAY_ELEMS(frame->data)) * sizeof(*frame->data));
+    frame->linesize[0] -= block_align*offset;
+    frame->nb_samples -= offset;
 
-    if (buf->pts != AV_NOPTS_VALUE) {
-        buf->pts += av_rescale_q(offset, (AVRational){1, link->sample_rate},
-                                 link->time_base);
+    if (frame->pts != AV_NOPTS_VALUE) {
+        frame->pts += av_rescale_q(offset, (AVRational){1, link->sample_rate},
+                                   link->time_base);
     }
 }
 
-static int calc_ptr_alignment(AVFilterBufferRef *buf)
+static int calc_ptr_alignment(AVFrame *frame)
 {
-    int planes = av_sample_fmt_is_planar(buf->format) ?
-                 av_get_channel_layout_nb_channels(buf->audio->channel_layout) : 1;
+    int planes = av_sample_fmt_is_planar(frame->format) ?
+                 av_get_channel_layout_nb_channels(frame->channel_layout) : 1;
     int min_align = 128;
     int p;
 
     for (p = 0; p < planes; p++) {
         int cur_align = 128;
-        while ((intptr_t)buf->extended_data[p] % cur_align)
+        while ((intptr_t)frame->extended_data[p] % cur_align)
             cur_align >>= 1;
         if (cur_align < min_align)
             min_align = cur_align;
@@ -147,35 +147,34 @@
 {
     AVFilterLink *link = ctx->outputs[0];
     FifoContext *s = ctx->priv;
-    AVFilterBufferRef *head = s->root.next->buf;
-    AVFilterBufferRef *buf_out;
+    AVFrame *head = s->root.next->frame;
+    AVFrame *out;
     int ret;
 
-    if (!s->buf_out &&
-        head->audio->nb_samples >= link->request_samples &&
+    if (!s->out &&
+        head->nb_samples >= link->request_samples &&
         calc_ptr_alignment(head) >= 32) {
-        if (head->audio->nb_samples == link->request_samples) {
-            buf_out = head;
+        if (head->nb_samples == link->request_samples) {
+            out = head;
             queue_pop(s);
         } else {
-            buf_out = avfilter_ref_buffer(head, AV_PERM_READ);
-            if (!buf_out)
+            out = av_frame_clone(head);
+            if (!out)
                 return AVERROR(ENOMEM);
 
-            buf_out->audio->nb_samples = link->request_samples;
+            out->nb_samples = link->request_samples;
             buffer_offset(link, head, link->request_samples);
         }
     } else {
         int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
 
-        if (!s->buf_out) {
-            s->buf_out = ff_get_audio_buffer(link, AV_PERM_WRITE,
-                                             link->request_samples);
-            if (!s->buf_out)
+        if (!s->out) {
+            s->out = ff_get_audio_buffer(link, link->request_samples);
+            if (!s->out)
                 return AVERROR(ENOMEM);
 
-            s->buf_out->audio->nb_samples = 0;
-            s->buf_out->pts               = head->pts;
+            s->out->nb_samples = 0;
+            s->out->pts                   = head->pts;
             s->allocated_samples          = link->request_samples;
         } else if (link->request_samples != s->allocated_samples) {
             av_log(ctx, AV_LOG_ERROR, "request_samples changed before the "
@@ -183,41 +182,43 @@
             return AVERROR(EINVAL);
         }
 
-        while (s->buf_out->audio->nb_samples < s->allocated_samples) {
-            int len = FFMIN(s->allocated_samples - s->buf_out->audio->nb_samples,
-                            head->audio->nb_samples);
+        while (s->out->nb_samples < s->allocated_samples) {
+            int len;
 
-            av_samples_copy(s->buf_out->extended_data, head->extended_data,
-                            s->buf_out->audio->nb_samples, 0, len, nb_channels,
-                            link->format);
-            s->buf_out->audio->nb_samples += len;
-
-            if (len == head->audio->nb_samples) {
-                avfilter_unref_buffer(head);
-                queue_pop(s);
-
-                if (!s->root.next &&
-                    (ret = ff_request_frame(ctx->inputs[0])) < 0) {
-                    if (ret == AVERROR_EOF) {
-                        av_samples_set_silence(s->buf_out->extended_data,
-                                               s->buf_out->audio->nb_samples,
-                                               s->allocated_samples -
-                                               s->buf_out->audio->nb_samples,
-                                               nb_channels, link->format);
-                        s->buf_out->audio->nb_samples = s->allocated_samples;
-                        break;
-                    }
+            if (!s->root.next) {
+                ret = ff_request_frame(ctx->inputs[0]);
+                if (ret == AVERROR_EOF) {
+                    av_samples_set_silence(s->out->extended_data,
+                                           s->out->nb_samples,
+                                           s->allocated_samples -
+                                           s->out->nb_samples,
+                                           nb_channels, link->format);
+                    s->out->nb_samples = s->allocated_samples;
+                    break;
+                } else if (ret < 0)
                     return ret;
-                }
-                head = s->root.next->buf;
+            }
+            head = s->root.next->frame;
+
+            len = FFMIN(s->allocated_samples - s->out->nb_samples,
+                        head->nb_samples);
+
+            av_samples_copy(s->out->extended_data, head->extended_data,
+                            s->out->nb_samples, 0, len, nb_channels,
+                            link->format);
+            s->out->nb_samples += len;
+
+            if (len == head->nb_samples) {
+                av_frame_free(&head);
+                queue_pop(s);
             } else {
                 buffer_offset(link, head, len);
             }
         }
-        buf_out = s->buf_out;
-        s->buf_out = NULL;
+        out = s->out;
+        s->out = NULL;
     }
-    return ff_filter_frame(link, buf_out);
+    return ff_filter_frame(link, out);
 }
 
 static int request_frame(AVFilterLink *outlink)
@@ -234,7 +235,7 @@
     if (outlink->request_samples) {
         return return_audio_frame(outlink->src);
     } else {
-        ret = ff_filter_frame(outlink, fifo->root.next->buf);
+        ret = ff_filter_frame(outlink, fifo->root.next->frame);
         queue_pop(fifo);
     }
 
@@ -247,7 +248,6 @@
         .type             = AVMEDIA_TYPE_VIDEO,
         .get_video_buffer = ff_null_get_video_buffer,
         .filter_frame     = add_to_queue,
-        .min_perms        = AV_PERM_PRESERVE,
     },
     { NULL }
 };
@@ -280,7 +280,6 @@
         .type             = AVMEDIA_TYPE_AUDIO,
         .get_audio_buffer = ff_null_get_audio_buffer,
         .filter_frame     = add_to_queue,
-        .min_perms        = AV_PERM_PRESERVE,
     },
     { NULL }
 };
diff --git a/libavfilter/filtfmts.c b/libavfilter/filtfmts.c
index 7286729..61b002d 100644
--- a/libavfilter/filtfmts.c
+++ b/libavfilter/filtfmts.c
@@ -97,7 +97,7 @@
                 filter_name);
         return 1;
     }
-    if (avfilter_init_filter(filter_ctx, filter_args, NULL) < 0) {
+    if (avfilter_init_str(filter_ctx, filter_args) < 0) {
         fprintf(stderr, "Impossible to init filter '%s' with arguments '%s'\n",
                 filter_name, filter_args);
         return 1;
diff --git a/libavfilter/formats.c b/libavfilter/formats.c
index 43718e4..ea24627 100644
--- a/libavfilter/formats.c
+++ b/libavfilter/formats.c
@@ -184,6 +184,10 @@
             for (i = j = 0; i < b->nb_channel_layouts; i++)
                 if (KNOWN(b->channel_layouts[i]))
                     b->channel_layouts[j++] = b->channel_layouts[i];
+            /* Not optimal: the unknown layouts of b may become known after
+               another merge. */
+            if (!j)
+                return NULL;
             b->nb_channel_layouts = j;
         }
         MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, fail);
diff --git a/libavfilter/gradfun.h b/libavfilter/gradfun.h
index 801dddd..7b1140f 100644
--- a/libavfilter/gradfun.h
+++ b/libavfilter/gradfun.h
@@ -27,7 +27,7 @@
 /// Holds instance-specific information for gradfun.
 typedef struct GradFunContext {
     const AVClass *class;
-    double strength;    ///< user specified strength, used to define thresh
+    float strength;
     int thresh;    ///< threshold for gradient algorithm
     int radius;    ///< blur radius
     int chroma_w;  ///< width of the chroma planes
diff --git a/libavfilter/graphdump.c b/libavfilter/graphdump.c
index 45f64c0..756f63d 100644
--- a/libavfilter/graphdump.c
+++ b/libavfilter/graphdump.c
@@ -45,7 +45,7 @@
 
         case AVMEDIA_TYPE_AUDIO:
             av_get_channel_layout_string(layout, sizeof(layout),
-                                         -1, link->channel_layout);
+                                         link->channels, link->channel_layout);
             format = av_x_if_null(av_get_sample_fmt_name(link->format), "?");
             av_bprintf(buf, "[%dHz %s:%s]",
                        (int)link->sample_rate, format, layout);
@@ -62,7 +62,7 @@
 {
     unsigned i, j, x, e;
 
-    for (i = 0; i < graph->filter_count; i++) {
+    for (i = 0; i < graph->nb_filters; i++) {
         AVFilterContext *filter = graph->filters[i];
         unsigned max_src_name = 0, max_dst_name = 0;
         unsigned max_in_name  = 0, max_out_name = 0;
diff --git a/libavfilter/graphparser.c b/libavfilter/graphparser.c
index 0ce823a..3718d09 100644
--- a/libavfilter/graphparser.c
+++ b/libavfilter/graphparser.c
@@ -20,14 +20,12 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include <ctype.h>
 #include <string.h>
 #include <stdio.h>
 
 #include "libavutil/avstring.h"
 #include "libavutil/mem.h"
 #include "avfilter.h"
-#include "avfiltergraph.h"
 
 #define WHITESPACES " \n\t"
 
@@ -111,26 +109,22 @@
         return AVERROR(EINVAL);
     }
 
-    ret = avfilter_open(filt_ctx, filt, inst_name);
+    *filt_ctx = avfilter_graph_alloc_filter(ctx, filt, inst_name);
     if (!*filt_ctx) {
         av_log(log_ctx, AV_LOG_ERROR,
                "Error creating filter '%s'\n", filt_name);
-        return ret;
+        return AVERROR(ENOMEM);
     }
 
-    if ((ret = avfilter_graph_add_filter(ctx, *filt_ctx)) < 0) {
-        avfilter_free(*filt_ctx);
-        return ret;
-    }
-
-    if (!strcmp(filt_name, "scale") && args && !strstr(args, "flags")
-        && ctx->scale_sws_opts) {
+    if (!strcmp(filt_name, "scale") && args && !strstr(args, "flags") &&
+        ctx->scale_sws_opts) {
         snprintf(tmp_args, sizeof(tmp_args), "%s:%s",
                  args, ctx->scale_sws_opts);
         args = tmp_args;
     }
 
-    if ((ret = avfilter_init_filter(*filt_ctx, args, NULL)) < 0) {
+    ret = avfilter_init_str(*filt_ctx, args);
+    if (ret < 0) {
         av_log(log_ctx, AV_LOG_ERROR,
                "Error initializing filter '%s' with args '%s'\n", filt_name, args);
         return ret;
@@ -437,8 +431,8 @@
     return 0;
 
  fail:end:
-    for (; graph->filter_count > 0; graph->filter_count--)
-        avfilter_free(graph->filters[graph->filter_count - 1]);
+    while (graph->nb_filters)
+        avfilter_free(graph->filters[0]);
     av_freep(&graph->filters);
     avfilter_inout_free(&open_inputs);
     avfilter_inout_free(&open_outputs);
@@ -505,8 +499,8 @@
 
  fail:
     if (ret < 0) {
-        for (; graph->filter_count > 0; graph->filter_count--)
-            avfilter_free(graph->filters[graph->filter_count - 1]);
+        while (graph->nb_filters)
+            avfilter_free(graph->filters[0]);
         av_freep(&graph->filters);
     }
     avfilter_inout_free(&inputs);
@@ -592,8 +586,8 @@
     avfilter_inout_free(&curr_inputs);
 
     if (ret < 0) {
-        for (; graph->filter_count > 0; graph->filter_count--)
-            avfilter_free(graph->filters[graph->filter_count - 1]);
+        while (graph->nb_filters)
+            avfilter_free(graph->filters[0]);
         av_freep(&graph->filters);
     }
     return ret;
diff --git a/libavfilter/internal.h b/libavfilter/internal.h
index d03de56..fbe9356 100644
--- a/libavfilter/internal.h
+++ b/libavfilter/internal.h
@@ -68,32 +68,12 @@
     enum AVMediaType type;
 
     /**
-     * Minimum required permissions on incoming buffers. Any buffer with
-     * insufficient permissions will be automatically copied by the filter
-     * system to a new buffer which provides the needed access permissions.
-     *
-     * Input pads only.
-     */
-    int min_perms;
-
-    /**
-     * Permissions which are not accepted on incoming buffers. Any buffer
-     * which has any of these permissions set will be automatically copied
-     * by the filter system to a new buffer which does not have those
-     * permissions. This can be used to easily disallow buffers with
-     * AV_PERM_REUSE.
-     *
-     * Input pads only.
-     */
-    int rej_perms;
-
-    /**
      * Callback function to get a video buffer. If NULL, the filter system will
      * use ff_default_get_video_buffer().
      *
      * Input video pads only.
      */
-    AVFilterBufferRef *(*get_video_buffer)(AVFilterLink *link, int perms, int w, int h);
+    AVFrame *(*get_video_buffer)(AVFilterLink *link, int w, int h);
 
     /**
      * Callback function to get an audio buffer. If NULL, the filter system will
@@ -101,8 +81,7 @@
      *
      * Input audio pads only.
      */
-    AVFilterBufferRef *(*get_audio_buffer)(AVFilterLink *link, int perms,
-                                           int nb_samples);
+    AVFrame *(*get_audio_buffer)(AVFilterLink *link, int nb_samples);
 
     /**
      * Filtering callback. This is where a filter receives a frame with
@@ -114,7 +93,7 @@
      * must ensure that samplesref is properly unreferenced on error if it
      * hasn't been passed on to another filter.
      */
-    int (*filter_frame)(AVFilterLink *link, AVFilterBufferRef *frame);
+    int (*filter_frame)(AVFilterLink *link, AVFrame *frame);
 
     /**
      * Frame poll callback. This returns the number of immediately available
@@ -234,8 +213,6 @@
 
 void ff_update_link_current_pts(AVFilterLink *link, int64_t pts);
 
-void ff_free_pool(AVFilterPool *pool);
-
 void ff_command_queue_pop(AVFilterContext *filter);
 
 /* misc trace functions */
@@ -252,7 +229,7 @@
 
 char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms);
 
-void ff_tlog_ref(void *ctx, AVFilterBufferRef *ref, int end);
+void ff_tlog_ref(void *ctx, AVFrame *ref, int end);
 
 void ff_tlog_link(void *ctx, AVFilterLink *link, int end);
 
@@ -346,6 +323,35 @@
  * @return >= 0 on success, a negative AVERROR on error. The receiving filter
  * is responsible for unreferencing frame in case of error.
  */
-int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame);
+int ff_filter_frame(AVFilterLink *link, AVFrame *frame);
+
+/**
+ * Flags for AVFilterLink.flags.
+ */
+enum {
+
+    /**
+     * Frame requests may need to loop in order to be fulfilled.
+     * A filter must set this flags on an output link if it may return 0 in
+     * request_frame() without filtering a frame.
+     */
+    FF_LINK_FLAG_REQUEST_LOOP = 1,
+
+};
+
+/**
+ * Allocate a new filter context and return it.
+ *
+ * @param filter what filter to create an instance of
+ * @param inst_name name to give to the new filter context
+ *
+ * @return newly created filter context or NULL on failure
+ */
+AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name);
+
+/**
+ * Remove a filter from a graph;
+ */
+void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter);
 
 #endif /* AVFILTER_INTERNAL_H */
diff --git a/libavfilter/libmpcodecs/vf_detc.c b/libavfilter/libmpcodecs/vf_detc.c
deleted file mode 100644
index 751e2b8..0000000
--- a/libavfilter/libmpcodecs/vf_detc.c
+++ /dev/null
@@ -1,453 +0,0 @@
-/*
- * This file is part of MPlayer.
- *
- * MPlayer is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * MPlayer 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with MPlayer; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "config.h"
-#include "mp_msg.h"
-
-#include "img_format.h"
-#include "mp_image.h"
-#include "vf.h"
-
-#include "libvo/fastmemcpy.h"
-
-struct metrics {
-        int even;
-        int odd;
-        int noise;
-        int temp;
-};
-
-struct vf_priv_s {
-        int frame;
-        int drop, lastdrop;
-        struct metrics pm;
-        int thres[5];
-        int inframes, outframes;
-        int mode;
-        int (*analyze)(struct vf_priv_s *, mp_image_t *, mp_image_t *);
-        int needread;
-};
-
-#define COMPE(a,b,e) (abs((a)-(b)) < (((a)+(b))>>(e)))
-#define COMPARABLE(a,b) COMPE((a),(b),2)
-#define VERYCLOSE(a,b) COMPE((a),(b),3)
-
-#define OUTER_TC_NBHD(s) ( \
- COMPARABLE((s)[-1].m.even,(s)[-1].m.odd) && \
- COMPARABLE((s)[1].m.even,(s)[0].m.odd) && \
- COMPARABLE((s)[2].m.even,(s)[1].m.odd) && \
- COMPARABLE((s)[-1].m.noise,(s)[0].m.temp) && \
- COMPARABLE((s)[2].m.noise,(s)[2].m.temp) )
-
-#define INNER_TC_NBHD(s,l,h) ( \
- COMPARABLE((s)[0].m.even,(l)) && \
- COMPARABLE((s)[2].m.odd,(l)) && ( \
- COMPARABLE((s)[0].m.noise,(h)) || \
- COMPARABLE((s)[1].m.noise,(h)) ) )
-
-enum {
-        TC_DROP,
-        TC_PROG,
-        TC_IL1,
-        TC_IL2
-};
-
-static void block_diffs(struct metrics *m, unsigned char *old, unsigned char *new, int os, int ns)
-{
-        int x, y, even=0, odd=0, noise, temp;
-        unsigned char *oldp, *newp;
-        m->noise = m->temp = 0;
-        for (x = 8; x; x--) {
-                oldp = old++;
-                newp = new++;
-                noise = temp = 0;
-                for (y = 4; y; y--) {
-                        even += abs(newp[0]-oldp[0]);
-                        odd += abs(newp[ns]-oldp[os]);
-                        noise += newp[ns]-newp[0];
-                        temp += oldp[os]-newp[0];
-                        oldp += os<<1;
-                        newp += ns<<1;
-                }
-                m->noise += abs(noise);
-                m->temp += abs(temp);
-        }
-        m->even = even;
-        m->odd = odd;
-}
-
-static void diff_planes(struct metrics *m, unsigned char *old, unsigned char *new, int w, int h, int os, int ns)
-{
-        int x, y, me=0, mo=0, mn=0, mt=0;
-        struct metrics l;
-        for (y = 0; y < h-7; y += 8) {
-                for (x = 0; x < w-7; x += 8) {
-                        block_diffs(&l, old+x+y*os, new+x+y*ns, os, ns);
-                        if (l.even > me) me = l.even;
-                        if (l.odd > mo) mo = l.odd;
-                        if (l.noise > mn) mn = l.noise;
-                        if (l.temp > mt) mt = l.temp;
-                }
-        }
-        m->even = me;
-        m->odd = mo;
-        m->noise = mn;
-        m->temp = mt;
-}
-
-static void diff_fields(struct metrics *metr, mp_image_t *old, mp_image_t *new)
-{
-        struct metrics m, mu, mv;
-        diff_planes(&m, old->planes[0], new->planes[0],
-                new->w, new->h, old->stride[0], new->stride[0]);
-        if (new->flags & MP_IMGFLAG_PLANAR) {
-                diff_planes(&mu, old->planes[1], new->planes[1],
-                        new->chroma_width, new->chroma_height,
-                        old->stride[1], new->stride[1]);
-                diff_planes(&mv, old->planes[2], new->planes[2],
-                        new->chroma_width, new->chroma_height,
-                        old->stride[2], new->stride[2]);
-                if (mu.even > m.even) m.even = mu.even;
-                if (mu.odd > m.odd) m.odd = mu.odd;
-                if (mu.noise > m.noise) m.noise = mu.noise;
-                if (mu.temp > m.temp) m.temp = mu.temp;
-                if (mv.even > m.even) m.even = mv.even;
-                if (mv.odd > m.odd) m.odd = mv.odd;
-                if (mv.noise > m.noise) m.noise = mv.noise;
-                if (mv.temp > m.temp) m.temp = mv.temp;
-        }
-        *metr = m;
-}
-
-static void status(int f, struct metrics *m)
-{
-        ff_mp_msg(MSGT_VFILTER, MSGL_V, "frame %d: e=%d o=%d n=%d t=%d\n",
-                f, m->even, m->odd, m->noise, m->temp);
-}
-
-static int analyze_fixed_pattern(struct vf_priv_s *p, mp_image_t *new, mp_image_t *old)
-{
-        if (p->frame >= 0) p->frame = (p->frame+1)%5;
-        ff_mp_msg(MSGT_VFILTER, MSGL_V, "frame %d\n", p->frame);
-        switch (p->frame) {
-        case -1: case 0: case 1: case 2:
-                return TC_PROG;
-        case 3:
-                return TC_IL1;
-        case 4:
-                return TC_IL2;
-        }
-        return 0;
-}
-
-static int analyze_aggressive(struct vf_priv_s *p, mp_image_t *new, mp_image_t *old)
-{
-        struct metrics m, pm;
-
-        if (p->frame >= 0) p->frame = (p->frame+1)%5;
-
-        diff_fields(&m, old, new);
-
-        status(p->frame, &m);
-
-        pm = p->pm;
-        p->pm = m;
-
-        if (p->frame == 4) {
-                /* We need to break at scene changes, but is this a valid test? */
-                if ((m.even > p->thres[2]) && (m.odd > p->thres[2]) && (m.temp > p->thres[3])
-                        && (m.temp > 5*pm.temp) && (m.temp*2 > m.noise)) {
-                        ff_mp_msg(MSGT_VFILTER, MSGL_V, "scene change breaking telecine!\n");
-                        p->frame = -1;
-                        return TC_DROP;
-                }
-                /* Thres. is to compensate for quantization errors when noise is low */
-                if (m.noise - m.temp > -p->thres[4]) {
-                        if (COMPARABLE(m.even, pm.odd)) {
-                                //ff_mp_msg(MSGT_VFILTER, MSGL_V, "confirmed field match!\n");
-                                return TC_IL2;
-                        } else if ((m.even < p->thres[0]) && (m.odd < p->thres[0]) && VERYCLOSE(m.even, m.odd)
-                                && VERYCLOSE(m.noise,m.temp) && VERYCLOSE(m.noise,pm.noise)) {
-                                ff_mp_msg(MSGT_VFILTER, MSGL_V, "interlaced frame appears in duplicate!!!\n");
-                                p->pm = pm; /* hack :) */
-                                p->frame = 3;
-                                return TC_IL1;
-                        }
-                } else {
-                        ff_mp_msg(MSGT_VFILTER, MSGL_V, "mismatched telecine fields!\n");
-                        p->frame = -1;
-                }
-        }
-
-        if (2*m.even*m.temp < m.odd*m.noise) {
-                ff_mp_msg(MSGT_VFILTER, MSGL_V, "caught telecine sync!\n");
-                p->frame = 3;
-                return TC_IL1;
-        }
-
-        if (p->frame < 3) {
-                if (m.noise > p->thres[3]) {
-                        if (m.noise > 2*m.temp) {
-                                ff_mp_msg(MSGT_VFILTER, MSGL_V, "merging fields out of sequence!\n");
-                                return TC_IL2;
-                        }
-                        if ((m.noise > 2*pm.noise) && (m.even > p->thres[2]) && (m.odd > p->thres[2])) {
-                                ff_mp_msg(MSGT_VFILTER, MSGL_V, "dropping horrible interlaced frame!\n");
-                                return TC_DROP;
-                        }
-                }
-        }
-
-        switch (p->frame) {
-        case -1:
-                if (4*m.noise > 5*m.temp) {
-                        ff_mp_msg(MSGT_VFILTER, MSGL_V, "merging fields out of sequence!\n");
-                        return TC_IL2;
-                }
-        case 0:
-        case 1:
-        case 2:
-                return TC_PROG;
-        case 3:
-                if ((m.even > p->thres[1]) && (m.even > m.odd) && (m.temp > m.noise)) {
-                        ff_mp_msg(MSGT_VFILTER, MSGL_V, "lost telecine tracking!\n");
-                        p->frame = -1;
-                        return TC_PROG;
-                }
-                return TC_IL1;
-        case 4:
-                return TC_IL2;
-        }
-        return 0;
-}
-
-static void copy_image(mp_image_t *dmpi, mp_image_t *mpi, int field)
-{
-        switch (field) {
-        case 0:
-                my_memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h/2,
-                        dmpi->stride[0]*2, mpi->stride[0]*2);
-                if (mpi->flags & MP_IMGFLAG_PLANAR) {
-                        my_memcpy_pic(dmpi->planes[1], mpi->planes[1],
-                                mpi->chroma_width, mpi->chroma_height/2,
-                                dmpi->stride[1]*2, mpi->stride[1]*2);
-                        my_memcpy_pic(dmpi->planes[2], mpi->planes[2],
-                                mpi->chroma_width, mpi->chroma_height/2,
-                                dmpi->stride[2]*2, mpi->stride[2]*2);
-                }
-                break;
-        case 1:
-                my_memcpy_pic(dmpi->planes[0]+dmpi->stride[0],
-                        mpi->planes[0]+mpi->stride[0], mpi->w, mpi->h/2,
-                        dmpi->stride[0]*2, mpi->stride[0]*2);
-                if (mpi->flags & MP_IMGFLAG_PLANAR) {
-                        my_memcpy_pic(dmpi->planes[1]+dmpi->stride[1],
-                                mpi->planes[1]+mpi->stride[1],
-                                mpi->chroma_width, mpi->chroma_height/2,
-                                dmpi->stride[1]*2, mpi->stride[1]*2);
-                        my_memcpy_pic(dmpi->planes[2]+dmpi->stride[2],
-                                mpi->planes[2]+mpi->stride[2],
-                                mpi->chroma_width, mpi->chroma_height/2,
-                                dmpi->stride[2]*2, mpi->stride[2]*2);
-                }
-                break;
-        case 2:
-                memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h,
-                        dmpi->stride[0], mpi->stride[0]);
-                if (mpi->flags & MP_IMGFLAG_PLANAR) {
-                        memcpy_pic(dmpi->planes[1], mpi->planes[1],
-                                mpi->chroma_width, mpi->chroma_height,
-                                dmpi->stride[1], mpi->stride[1]);
-                        memcpy_pic(dmpi->planes[2], mpi->planes[2],
-                                mpi->chroma_width, mpi->chroma_height,
-                                dmpi->stride[2], mpi->stride[2]);
-                }
-                break;
-        }
-}
-
-static int do_put_image(struct vf_instance *vf, mp_image_t *dmpi)
-{
-        struct vf_priv_s *p = vf->priv;
-        int dropflag;
-
-        switch (p->drop) {
-        default:
-                dropflag = 0;
-                break;
-        case 1:
-                dropflag = (++p->lastdrop >= 5);
-                break;
-        case 2:
-                dropflag = (++p->lastdrop >= 5) && (4*p->inframes <= 5*p->outframes);
-                break;
-        }
-
-        if (dropflag) {
-                ff_mp_msg(MSGT_VFILTER, MSGL_V, "drop! [%d/%d=%g]\n",
-                        p->outframes, p->inframes, (float)p->outframes/p->inframes);
-                p->lastdrop = 0;
-                return 0;
-        }
-
-        p->outframes++;
-        return ff_vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
-}
-
-static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
-{
-        int ret=0;
-        mp_image_t *dmpi;
-        struct vf_priv_s *p = vf->priv;
-
-        p->inframes++;
-
-        if (p->needread) dmpi = ff_vf_get_image(vf->next, mpi->imgfmt,
-                MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
-                MP_IMGFLAG_PRESERVE | MP_IMGFLAG_READABLE,
-                mpi->width, mpi->height);
-        /* FIXME: is there a good way to get rid of static type? */
-        else dmpi = ff_vf_get_image(vf->next, mpi->imgfmt,
-                MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
-                MP_IMGFLAG_PRESERVE, mpi->width, mpi->height);
-
-        switch (p->analyze(p, mpi, dmpi)) {
-        case TC_DROP:
-                /* Don't copy anything unless we'll need to read it. */
-                if (p->needread) copy_image(dmpi, mpi, 2);
-                p->lastdrop = 0;
-                break;
-        case TC_PROG:
-                /* Copy and display the whole frame. */
-                copy_image(dmpi, mpi, 2);
-                ret = do_put_image(vf, dmpi);
-                break;
-        case TC_IL1:
-                /* Only copy bottom field unless we need to read. */
-                if (p->needread) copy_image(dmpi, mpi, 2);
-                else copy_image(dmpi, mpi, 1);
-                p->lastdrop = 0;
-                break;
-        case TC_IL2:
-                /* Copy top field and show frame, then copy bottom if needed. */
-                copy_image(dmpi, mpi, 0);
-                ret = do_put_image(vf, dmpi);
-                if (p->needread) copy_image(dmpi, mpi, 1);
-                break;
-        }
-        return ret;
-}
-
-static int query_format(struct vf_instance *vf, unsigned int fmt)
-{
-        /* FIXME - figure out which other formats work */
-        switch (fmt) {
-        case IMGFMT_YV12:
-        case IMGFMT_IYUV:
-        case IMGFMT_I420:
-                return ff_vf_next_query_format(vf, fmt);
-        }
-        return 0;
-}
-
-static int config(struct vf_instance *vf,
-        int width, int height, int d_width, int d_height,
-        unsigned int flags, unsigned int outfmt)
-{
-        return ff_vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
-}
-
-static void uninit(struct vf_instance *vf)
-{
-        free(vf->priv);
-}
-
-static struct {
-        const char *name;
-        int (*func)(struct vf_priv_s *p, mp_image_t *new, mp_image_t *old);
-        int needread;
-} anal_funcs[] = {
-        { "fixed", analyze_fixed_pattern, 0 },
-        { "aggressive", analyze_aggressive, 1 },
-        { NULL, NULL, 0 }
-};
-
-#define STARTVARS if (0)
-#define GETVAR(str, name, out, func) \
- else if (!strncmp((str), name "=", sizeof(name))) \
- (out) = (func)((str) + sizeof(name))
-
-static void parse_var(struct vf_priv_s *p, char *var)
-{
-        STARTVARS;
-        GETVAR(var, "dr", p->drop, atoi);
-        GETVAR(var, "t0", p->thres[0], atoi);
-        GETVAR(var, "t1", p->thres[1], atoi);
-        GETVAR(var, "t2", p->thres[2], atoi);
-        GETVAR(var, "t3", p->thres[3], atoi);
-        GETVAR(var, "t4", p->thres[4], atoi);
-        GETVAR(var, "fr", p->frame, atoi);
-        GETVAR(var, "am", p->mode, atoi);
-}
-
-static void parse_args(struct vf_priv_s *p, char *args)
-{
-        char *next, *orig;
-        for (args=orig=strdup(args); args; args=next) {
-                next = strchr(args, ':');
-                if (next) *next++ = 0;
-                parse_var(p, args);
-        }
-        free(orig);
-}
-
-static int vf_open(vf_instance_t *vf, char *args)
-{
-        struct vf_priv_s *p;
-        vf->config = config;
-        vf->put_image = put_image;
-        vf->query_format = query_format;
-        vf->uninit = uninit;
-        vf->default_reqs = VFCAP_ACCEPT_STRIDE;
-        vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
-        p->frame = -1;
-        p->thres[0] = 440;
-        p->thres[1] = 720;
-        p->thres[2] = 2500;
-        p->thres[3] = 2500;
-        p->thres[4] = 800;
-        p->drop = 0;
-        p->mode = 1;
-        if (args) parse_args(p, args);
-        p->analyze = anal_funcs[p->mode].func;
-        p->needread = anal_funcs[p->mode].needread;
-        return 1;
-}
-
-const vf_info_t ff_vf_info_detc = {
-    "de-telecine filter",
-    "detc",
-    "Rich Felker",
-    "",
-    vf_open,
-    NULL
-};
diff --git a/libavfilter/libmpcodecs/vf_divtc.c b/libavfilter/libmpcodecs/vf_divtc.c
deleted file mode 100644
index 61f6e35..0000000
--- a/libavfilter/libmpcodecs/vf_divtc.c
+++ /dev/null
@@ -1,722 +0,0 @@
-/*
- * This file is part of MPlayer.
- *
- * MPlayer is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * MPlayer 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with MPlayer; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <limits.h>
-#include <math.h>
-
-#include "config.h"
-#include "mp_msg.h"
-#include "cpudetect.h"
-#include "libavutil/common.h"
-#include "libavutil/x86/asm.h"
-#include "mpbswap.h"
-
-#include "img_format.h"
-#include "mp_image.h"
-#include "vf.h"
-
-#include "libvo/fastmemcpy.h"
-
-const vf_info_t ff_vf_info_divtc;
-
-struct vf_priv_s
-   {
-   int deghost, pass, phase, window, fcount, bcount, frameno, misscount,
-      ocount, sum[5];
-   double threshold;
-   FILE *file;
-   int8_t *bdata;
-   unsigned int *csdata;
-   int *history;
-   };
-
-/*
- * diff_MMX and diff_C stolen from vf_decimate.c
- */
-
-#if HAVE_MMX && HAVE_EBX_AVAILABLE
-static int diff_MMX(unsigned char *old, unsigned char *new, int os, int ns)
-   {
-   volatile short out[4];
-   __asm__ (
-        "movl $8, %%ecx \n\t"
-        "pxor %%mm4, %%mm4 \n\t"
-        "pxor %%mm7, %%mm7 \n\t"
-
-        ASMALIGN(4)
-        "1: \n\t"
-
-        "movq (%%"REG_S"), %%mm0 \n\t"
-        "movq (%%"REG_S"), %%mm2 \n\t"
-        "add %%"REG_a", %%"REG_S" \n\t"
-        "movq (%%"REG_D"), %%mm1 \n\t"
-        "add %%"REG_b", %%"REG_D" \n\t"
-        "psubusb %%mm1, %%mm2 \n\t"
-        "psubusb %%mm0, %%mm1 \n\t"
-        "movq %%mm2, %%mm0 \n\t"
-        "movq %%mm1, %%mm3 \n\t"
-        "punpcklbw %%mm7, %%mm0 \n\t"
-        "punpcklbw %%mm7, %%mm1 \n\t"
-        "punpckhbw %%mm7, %%mm2 \n\t"
-        "punpckhbw %%mm7, %%mm3 \n\t"
-        "paddw %%mm0, %%mm4 \n\t"
-        "paddw %%mm1, %%mm4 \n\t"
-        "paddw %%mm2, %%mm4 \n\t"
-        "paddw %%mm3, %%mm4 \n\t"
-
-        "decl %%ecx \n\t"
-        "jnz 1b \n\t"
-        "movq %%mm4, (%%"REG_d") \n\t"
-        "emms \n\t"
-        :
-        : "S" (old), "D" (new), "a" ((long)os), "b" ((long)ns), "d" (out)
-        : "%ecx", "memory"
-        );
-   return out[0]+out[1]+out[2]+out[3];
-   }
-#endif
-
-static int diff_C(unsigned char *old, unsigned char *new, int os, int ns)
-   {
-   int x, y, d=0;
-
-   for(y=8; y; y--, new+=ns, old+=os)
-      for(x=8; x; x--)
-         d+=abs(new[x]-old[x]);
-
-   return d;
-   }
-
-static int (*diff)(unsigned char *, unsigned char *, int, int);
-
-static int diff_plane(unsigned char *old, unsigned char *new,
-                      int w, int h, int os, int ns, int arg)
-   {
-   int x, y, d, max=0, sum=0, n=0;
-
-   for(y=0; y<h-7; y+=8)
-      {
-      for(x=0; x<w-7; x+=8)
-         {
-         d=diff(old+x+y*os, new+x+y*ns, os, ns);
-         if(d>max) max=d;
-         sum+=d;
-         n++;
-         }
-      }
-
-   return (sum+n*max)/2;
-   }
-
-/*
-static unsigned int checksum_plane(unsigned char *p, unsigned char *z,
-                                   int w, int h, int s, int zs, int arg)
-   {
-   unsigned int shift, sum;
-   unsigned char *e;
-
-   for(sum=0; h; h--, p+=s-w)
-      for(e=p+w, shift=32; p<e;)
-         sum^=(*p++)<<(shift=(shift-8)&31);
-
-   return sum;
-   }
-*/
-
-static unsigned int checksum_plane(unsigned char *p, unsigned char *z,
-                                   int w, int h, int s, int zs, int arg)
-   {
-   unsigned int shift;
-   uint32_t sum, t;
-   unsigned char *e, *e2;
-#if HAVE_FAST_64BIT
-   typedef uint64_t wsum_t;
-#else
-   typedef uint32_t wsum_t;
-#endif
-   wsum_t wsum;
-
-   for(sum=0; h; h--, p+=s-w)
-      {
-      for(shift=0, e=p+w; (int)p&(sizeof(wsum_t)-1) && p<e;)
-         sum^=*p++<<(shift=(shift-8)&31);
-
-      for(wsum=0, e2=e-sizeof(wsum_t)+1; p<e2; p+=sizeof(wsum_t))
-         wsum^=*(wsum_t *)p;
-
-#if HAVE_FAST_64BIT
-      t=be2me_32((uint32_t)(wsum>>32^wsum));
-#else
-      t=be2me_32(wsum);
-#endif
-
-      for(sum^=(t<<shift|t>>(32-shift)); p<e;)
-         sum^=*p++<<(shift=(shift-8)&31);
-      }
-
-   return sum;
-   }
-
-static int deghost_plane(unsigned char *d, unsigned char *s,
-                         int w, int h, int ds, int ss, int threshold)
-   {
-   int t;
-   unsigned char *e;
-
-   for(; h; h--, s+=ss-w, d+=ds-w)
-      for(e=d+w; d<e; d++, s++)
-         if(abs(*d-*s)>=threshold)
-            *d=(t=(*d<<1)-*s)<0?0:t>255?255:t;
-
-   return 0;
-   }
-
-static int copyop(unsigned char *d, unsigned char *s, int bpl, int h, int dstride, int sstride, int dummy) {
-  memcpy_pic(d, s, bpl, h, dstride, sstride);
-  return 0;
-}
-
-static int imgop(int(*planeop)(unsigned char *, unsigned char *,
-                               int, int, int, int, int),
-                 mp_image_t *dst, mp_image_t *src, int arg)
-   {
-   if(dst->flags&MP_IMGFLAG_PLANAR)
-      return planeop(dst->planes[0], src?src->planes[0]:0,
-                     dst->w, dst->h,
-                     dst->stride[0], src?src->stride[0]:0, arg)+
-             planeop(dst->planes[1], src?src->planes[1]:0,
-                     dst->chroma_width, dst->chroma_height,
-                     dst->stride[1], src?src->stride[1]:0, arg)+
-             planeop(dst->planes[2], src?src->planes[2]:0,
-                     dst->chroma_width, dst->chroma_height,
-                     dst->stride[2], src?src->stride[2]:0, arg);
-
-   return planeop(dst->planes[0], src?src->planes[0]:0,
-                  dst->w*(dst->bpp/8), dst->h,
-                  dst->stride[0], src?src->stride[0]:0, arg);
-   }
-
-/*
- * Find the phase in which the telecine pattern fits best to the
- * given 5 frame slice of frame difference measurements.
- *
- * If phase1 and phase2 are not negative, only the two specified
- * phases are tested.
- */
-
-static int match(struct vf_priv_s *p, int *diffs,
-                 int phase1, int phase2, double *strength)
-   {
-   static const int pattern1[]={ -4,  1, 1, 1, 1 },
-      pattern2[]={ -2, -3, 4, 4, -3 }, *pattern;
-   int f, m, n, t[5];
-
-   pattern=p->deghost>0?pattern2:pattern1;
-
-   for(f=0; f<5; f++)
-      {
-      if(phase1<0 || phase2<0 || f==phase1 || f==phase2)
-         {
-         for(n=t[f]=0; n<5; n++)
-            t[f]+=diffs[n]*pattern[(n-f+5)%5];
-         }
-      else
-         t[f]=INT_MIN;
-      }
-
-   /* find the best match */
-   for(m=0, n=1; n<5; n++)
-      if(t[n]>t[m]) m=n;
-
-   if(strength)
-      {
-      /* the second best match */
-      for(f=m?0:1, n=f+1; n<5; n++)
-         if(n!=m && t[n]>t[f]) f=n;
-
-      *strength=(t[m]>0?(double)(t[m]-t[f])/t[m]:0.0);
-      }
-
-   return m;
-   }
-
-static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
-   {
-   mp_image_t *dmpi, *tmpi=0;
-   int n, m, f, newphase;
-   struct vf_priv_s *p=vf->priv;
-   unsigned int checksum;
-   double d;
-
-   dmpi=ff_vf_get_image(vf->next, mpi->imgfmt,
-                     MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
-                     MP_IMGFLAG_PRESERVE | MP_IMGFLAG_READABLE,
-                     mpi->width, mpi->height);
-   ff_vf_clone_mpi_attributes(dmpi, mpi);
-
-   newphase=p->phase;
-
-   switch(p->pass)
-      {
-      case 1:
-         fprintf(p->file, "%08x %d\n",
-                 (unsigned int)imgop((void *)checksum_plane, mpi, 0, 0),
-                 p->frameno?imgop(diff_plane, dmpi, mpi, 0):0);
-         break;
-
-      case 2:
-         if(p->frameno/5>p->bcount)
-            {
-            ff_mp_msg(MSGT_VFILTER, MSGL_ERR,
-                   "\n%s: Log file ends prematurely! "
-                   "Switching to one pass mode.\n", vf->info->name);
-            p->pass=0;
-            break;
-            }
-
-         checksum=(unsigned int)imgop((void *)checksum_plane, mpi, 0, 0);
-
-         if(checksum!=p->csdata[p->frameno])
-            {
-            for(f=0; f<100; f++)
-               if(p->frameno+f<p->fcount && p->csdata[p->frameno+f]==checksum)
-                  break;
-               else if(p->frameno-f>=0 && p->csdata[p->frameno-f]==checksum)
-                  {
-                  f=-f;
-                  break;
-                  }
-
-            if(f<100)
-               {
-               ff_mp_msg(MSGT_VFILTER, MSGL_INFO,
-                      "\n%s: Mismatch with pass-1: %+d frame(s).\n",
-                      vf->info->name, f);
-
-               p->frameno+=f;
-               p->misscount=0;
-               }
-            else if(p->misscount++>=30)
-               {
-               ff_mp_msg(MSGT_VFILTER, MSGL_ERR,
-                      "\n%s: Sync with pass-1 lost! "
-                      "Switching to one pass mode.\n", vf->info->name);
-               p->pass=0;
-               break;
-               }
-            }
-
-         n=(p->frameno)/5;
-         if(n>=p->bcount) n=p->bcount-1;
-
-         newphase=p->bdata[n];
-         break;
-
-      default:
-         if(p->frameno)
-            {
-            int *sump=p->sum+p->frameno%5,
-               *histp=p->history+p->frameno%p->window;
-
-            *sump-=*histp;
-            *sump+=(*histp=imgop(diff_plane, dmpi, mpi, 0));
-            }
-
-         m=match(p, p->sum, -1, -1, &d);
-
-         if(d>=p->threshold)
-            newphase=m;
-      }
-
-   n=p->ocount++%5;
-
-   if(newphase!=p->phase && ((p->phase+4)%5<n)==((newphase+4)%5<n))
-      {
-      p->phase=newphase;
-      ff_mp_msg(MSGT_VFILTER, MSGL_STATUS,
-             "\n%s: Telecine phase %d.\n", vf->info->name, p->phase);
-      }
-
-   switch((p->frameno++-p->phase+10)%5)
-      {
-      case 0:
-         imgop(copyop, dmpi, mpi, 0);
-         return 0;
-
-      case 4:
-         if(p->deghost>0)
-            {
-            tmpi=ff_vf_get_image(vf->next, mpi->imgfmt,
-                              MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE |
-                              MP_IMGFLAG_READABLE,
-                              mpi->width, mpi->height);
-            ff_vf_clone_mpi_attributes(tmpi, mpi);
-
-            imgop(copyop, tmpi, mpi, 0);
-            imgop(deghost_plane, tmpi, dmpi, p->deghost);
-            imgop(copyop, dmpi, mpi, 0);
-            return ff_vf_next_put_image(vf, tmpi, MP_NOPTS_VALUE);
-            }
-      }
-
-   imgop(copyop, dmpi, mpi, 0);
-   return ff_vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
-   }
-
-static int analyze(struct vf_priv_s *p)
-   {
-   int *buf=0, *bp, bufsize=0, n, b, f, i, j, m, s;
-   unsigned int *cbuf=0, *cp;
-   int8_t *pbuf;
-   int8_t lbuf[256];
-   int sum[5];
-   double d;
-
-   /* read the file */
-
-   n=15;
-   while(fgets(lbuf, 256, p->file))
-      {
-      if(n>=bufsize-19)
-         {
-         bufsize=bufsize?bufsize*2:30000;
-         if((bp=realloc(buf, bufsize*sizeof *buf))) buf=bp;
-         if((cp=realloc(cbuf, bufsize*sizeof *cbuf))) cbuf=cp;
-
-         if(!bp || !cp)
-            {
-            ff_mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: Not enough memory.\n",
-                   ff_vf_info_divtc.name);
-            free(buf);
-            free(cbuf);
-            return 0;
-            }
-         }
-      sscanf(lbuf, "%x %d", cbuf+n, buf+n);
-      n++;
-      }
-
-   if(n <= 15)
-      {
-      ff_mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: Empty 2-pass log file.\n",
-             ff_vf_info_divtc.name);
-      free(buf);
-      free(cbuf);
-      return 0;
-      }
-
-   /* generate some dummy data past the beginning and end of the array */
-
-   buf+=15, cbuf+=15;
-   n-=15;
-
-   memcpy(buf-15, buf, 15*sizeof *buf);
-   memset(cbuf-15, 0, 15*sizeof *cbuf);
-
-   while(n%5)
-      buf[n]=buf[n-5], cbuf[n]=0, n++;
-
-   memcpy(buf+n, buf+n-15, 15*sizeof *buf);
-   memset(cbuf+n, 0, 15*sizeof *cbuf);
-
-   p->csdata=cbuf;
-   p->fcount=n;
-
-   /* array with one slot for each slice of 5 frames */
-
-   p->bdata=pbuf=malloc(p->bcount=b=(n/5));
-   memset(pbuf, 255, b);
-
-   /* resolve the automatic mode */
-
-   if(p->deghost<0)
-      {
-      int deghost=-p->deghost;
-      double s0=0.0, s1=0.0;
-
-      for(f=0; f<n; f+=5)
-         {
-         p->deghost=0; match(p, buf+f, -1, -1, &d); s0+=d;
-         p->deghost=1; match(p, buf+f, -1, -1, &d); s1+=d;
-         }
-
-      p->deghost=s1>s0?deghost:0;
-
-      ff_mp_msg(MSGT_VFILTER, MSGL_INFO,
-             "%s: Deghosting %-3s (relative pattern strength %+.2fdB).\n",
-             ff_vf_info_divtc.name,
-             p->deghost?"ON":"OFF",
-             10.0*log10(s1/s0));
-      }
-
-   /* analyze the data */
-
-   for(f=0; f<5; f++)
-      for(sum[f]=0, n=-15; n<20; n+=5)
-         sum[f]+=buf[n+f];
-
-   for(f=0; f<b; f++)
-      {
-      m=match(p, sum, -1, -1, &d);
-
-      if(d>=p->threshold)
-         pbuf[f]=m;
-
-      if(f<b-1)
-         for(n=0; n<5; n++)
-            sum[n]=sum[n]-buf[5*(f-3)+n]+buf[5*(f+4)+n];
-      }
-
-   /* fill in the gaps */
-
-   /* the beginning */
-   for(f=0; f<b && pbuf[f]==-1; f++);
-
-   if(f==b)
-      {
-      free(buf-15);
-      ff_mp_msg(MSGT_VFILTER, MSGL_FATAL, "%s: No telecine pattern found!\n",
-             ff_vf_info_divtc.name);
-      return 0;
-      }
-
-   for(n=0; n<f; pbuf[n++]=pbuf[f]);
-
-   /* the end */
-   for(f=b-1; pbuf[f]==-1; f--);
-   for(n=f+1; n<b; pbuf[n++]=pbuf[f]);
-
-   /* the rest */
-   for(f=0;;)
-      {
-      while(f<b && pbuf[f]!=-1) f++;
-      if(f==b) break;
-      for(n=f; pbuf[n]==-1; n++);
-
-      if(pbuf[f-1]==pbuf[n])
-         {
-         /* just a gap */
-         while(f<n) pbuf[f++]=pbuf[n];
-         }
-      else
-         {
-         /* phase change, reanalyze the original data in the gap with zero
-            threshold for only the two phases that appear at the ends */
-
-         for(i=0; i<5; i++)
-            for(sum[i]=0, j=5*f-15; j<5*f; j+=5)
-               sum[i]+=buf[i+j];
-
-         for(i=f; i<n; i++)
-            {
-            pbuf[i]=match(p, sum, pbuf[f-1], pbuf[n], 0);
-
-            for(j=0; j<5; j++)
-               sum[j]=sum[j]-buf[5*(i-3)+j]+buf[5*(i+4)+j];
-            }
-
-         /* estimate the transition point by dividing the gap
-            in the same proportion as the number of matches of each kind */
-
-         for(i=f, m=f; i<n; i++)
-            if(pbuf[i]==pbuf[f-1]) m++;
-
-         /* find the transition of the right direction nearest to the
-            estimated point */
-
-         if(m>f && m<n)
-            {
-            for(j=m; j>f; j--)
-               if(pbuf[j-1]==pbuf[f-1] && pbuf[j]==pbuf[n]) break;
-            for(s=m; s<n; s++)
-               if(pbuf[s-1]==pbuf[f-1] && pbuf[s]==pbuf[n]) break;
-
-            m=(s-m<m-j)?s:j;
-            }
-
-         /* and rewrite the data to allow only this one transition */
-
-         for(i=f; i<m; i++)
-            pbuf[i]=pbuf[f-1];
-
-         for(; i<n; i++)
-            pbuf[i]=pbuf[n];
-
-         f=n;
-         }
-      }
-
-   free(buf-15);
-
-   return 1;
-   }
-
-static int query_format(struct vf_instance *vf, unsigned int fmt)
-   {
-   switch(fmt)
-      {
-      case IMGFMT_444P: case IMGFMT_IYUV: case IMGFMT_RGB24:
-      case IMGFMT_422P: case IMGFMT_UYVY: case IMGFMT_BGR24:
-      case IMGFMT_411P: case IMGFMT_YUY2: case IMGFMT_IF09:
-      case IMGFMT_YV12: case IMGFMT_I420: case IMGFMT_YVU9:
-      case IMGFMT_IUYV: case IMGFMT_Y800: case IMGFMT_Y8:
-         return ff_vf_next_query_format(vf,fmt);
-      }
-
-   return 0;
-   }
-
-static void uninit(struct vf_instance *vf)
-   {
-   if(vf->priv)
-      {
-      if(vf->priv->file) fclose(vf->priv->file);
-      if(vf->priv->csdata) free(vf->priv->csdata-15);
-      free(vf->priv->bdata);
-      free(vf->priv->history);
-      free(vf->priv);
-      }
-   }
-
-static int vf_open(vf_instance_t *vf, char *args)
-   {
-   struct vf_priv_s *p;
-   const char *filename="framediff.log";
-   char *ap, *q, *a;
-
-   if(args && !(args=strdup(args)))
-      {
-   nomem:
-      ff_mp_msg(MSGT_VFILTER, MSGL_FATAL,
-             "%s: Not enough memory.\n", vf->info->name);
-   fail:
-      uninit(vf);
-      free(args);
-      return 0;
-      }
-
-   vf->put_image=put_image;
-   vf->uninit=uninit;
-   vf->query_format=query_format;
-   vf->default_reqs=VFCAP_ACCEPT_STRIDE;
-   if(!(vf->priv=p=calloc(1, sizeof(struct vf_priv_s))))
-      goto nomem;
-
-   p->phase=5;
-   p->threshold=0.5;
-   p->window=30;
-
-   if((ap=args))
-      while(*ap)
-         {
-         q=ap;
-         if((ap=strchr(q, ':'))) *ap++=0; else ap=q+strlen(q);
-         if((a=strchr(q, '='))) *a++=0; else a=q+strlen(q);
-
-         switch(*q)
-            {
-            case 0:                              break;
-            case 'f': filename=a;                break;
-            case 't': p->threshold=atof(a);      break;
-            case 'w': p->window=5*(atoi(a)+4)/5; break;
-            case 'd': p->deghost=atoi(a);        break;
-            case 'p':
-               if(q[1]=='h') p->phase=atoi(a);
-               else p->pass=atoi(a);
-               break;
-
-            case 'h':
-               ff_mp_msg(MSGT_VFILTER, MSGL_INFO,
-                      "\n%s options:\n\n"
-                      "pass=1|2         - Use 2-pass mode.\n"
-                      "file=filename    - Set the 2-pass log file name "
-                      "(default %s).\n"
-                      "threshold=value  - Set the pattern recognition "
-                      "sensitivity (default %g).\n"
-                      "deghost=value    - Select deghosting threshold "
-                      "(default %d).\n"
-                      "window=numframes - Set the statistics window "
-                      "for 1-pass mode (default %d).\n"
-                      "phase=0|1|2|3|4  - Set the initial phase "
-                      "for 1-pass mode (default %d).\n\n"
-                      "The option names can be abbreviated to the shortest "
-                      "unique prefix.\n\n",
-                      vf->info->name, filename, p->threshold, p->deghost,
-                      p->window, p->phase%5);
-               break;
-
-            default:
-               ff_mp_msg(MSGT_VFILTER, MSGL_FATAL,
-                      "%s: Unknown argument %s.\n", vf->info->name, q);
-               goto fail;
-            }
-         }
-
-   switch(p->pass)
-      {
-      case 1:
-         if(!(p->file=fopen(filename, "w")))
-            {
-            ff_mp_msg(MSGT_VFILTER, MSGL_FATAL,
-                   "%s: Can't create file %s.\n", vf->info->name, filename);
-            goto fail;
-            }
-
-         break;
-
-      case 2:
-         if(!(p->file=fopen(filename, "r")))
-            {
-            ff_mp_msg(MSGT_VFILTER, MSGL_FATAL,
-                   "%s: Can't open file %s.\n", vf->info->name, filename);
-            goto fail;
-            }
-
-         if(!analyze(p))
-            goto fail;
-
-         fclose(p->file);
-         p->file=0;
-         break;
-      }
-
-   if(p->window<5) p->window=5;
-   if(!(p->history=calloc(sizeof *p->history, p->window)))
-      goto nomem;
-
-   diff = diff_C;
-#if HAVE_MMX && HAVE_EBX_AVAILABLE
-   if(ff_gCpuCaps.hasMMX) diff = diff_MMX;
-#endif
-
-   free(args);
-   return 1;
-   }
-
-const vf_info_t ff_vf_info_divtc =
-   {
-   "inverse telecine for deinterlaced video",
-   "divtc",
-   "Ville Saari",
-   "",
-   vf_open,
-   NULL
-   };
diff --git a/libavfilter/libmpcodecs/vf_down3dright.c b/libavfilter/libmpcodecs/vf_down3dright.c
deleted file mode 100644
index 5c95ce6..0000000
--- a/libavfilter/libmpcodecs/vf_down3dright.c
+++ /dev/null
@@ -1,166 +0,0 @@
-/*
- * This file is part of MPlayer.
- *
- * MPlayer is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * MPlayer 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with MPlayer; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <inttypes.h>
-
-#include "config.h"
-#include "mp_msg.h"
-#include "cpudetect.h"
-
-#include "img_format.h"
-#include "mp_image.h"
-#include "vf.h"
-
-#include "libvo/fastmemcpy.h"
-
-struct vf_priv_s {
-        int skipline;
-        int scalew;
-        int scaleh;
-};
-
-static void toright(unsigned char *dst[3], unsigned char *src[3],
-                    int dststride[3], int srcstride[3],
-                    int w, int h, struct vf_priv_s* p)
-{
-        int k;
-
-        for (k = 0; k < 3; k++) {
-                unsigned char* fromL = src[k];
-                unsigned char* fromR = src[k];
-                unsigned char* to = dst[k];
-                int src = srcstride[k];
-                int dst = dststride[k];
-                int ss;
-                unsigned int dd;
-                int i;
-
-                if (k > 0) {
-                        i = h / 4 - p->skipline / 2;
-                        ss = src * (h / 4 + p->skipline / 2);
-                        dd = w / 4;
-                } else {
-                        i = h / 2 - p->skipline;
-                        ss = src * (h / 2 + p->skipline);
-                        dd = w / 2;
-                }
-                fromR += ss;
-                for ( ; i > 0; i--) {
-                        int j;
-                        unsigned char* t = to;
-                        unsigned char* sL = fromL;
-                        unsigned char* sR = fromR;
-
-                        if (p->scalew == 1) {
-                                for (j = dd; j > 0; j--) {
-                                        *t++ = (sL[0] + sL[1]) / 2;
-                                        sL+=2;
-                                }
-                                for (j = dd ; j > 0; j--) {
-                                        *t++ = (sR[0] + sR[1]) / 2;
-                                        sR+=2;
-                                }
-                        } else {
-                                for (j = dd * 2 ; j > 0; j--)
-                                        *t++ = *sL++;
-                                for (j = dd * 2 ; j > 0; j--)
-                                        *t++ = *sR++;
-                        }
-                        if (p->scaleh == 1) {
-                                fast_memcpy(to + dst, to, dst);
-                                to += dst;
-                        }
-                        to += dst;
-                        fromL += src;
-                        fromR += src;
-                }
-                //printf("K %d  %d   %d   %d  %d \n", k, w, h,  src, dst);
-        }
-}
-
-static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
-{
-        mp_image_t *dmpi;
-
-        // hope we'll get DR buffer:
-        dmpi=ff_vf_get_image(vf->next, IMGFMT_YV12,
-                          MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE |
-                          ((vf->priv->scaleh == 1) ? MP_IMGFLAG_READABLE : 0),
-                          mpi->w * vf->priv->scalew,
-                          mpi->h / vf->priv->scaleh - vf->priv->skipline);
-
-        toright(dmpi->planes, mpi->planes, dmpi->stride,
-                mpi->stride, mpi->w, mpi->h, vf->priv);
-
-        return ff_vf_next_put_image(vf,dmpi, pts);
-}
-
-static int config(struct vf_instance *vf,
-                  int width, int height, int d_width, int d_height,
-                  unsigned int flags, unsigned int outfmt)
-{
-        /* FIXME - also support UYVY output? */
-        return ff_vf_next_config(vf, width * vf->priv->scalew,
-                              height / vf->priv->scaleh - vf->priv->skipline, d_width, d_height, flags, IMGFMT_YV12);
-}
-
-
-static int query_format(struct vf_instance *vf, unsigned int fmt)
-{
-        /* FIXME - really any YUV 4:2:0 input format should work */
-        switch (fmt) {
-        case IMGFMT_YV12:
-        case IMGFMT_IYUV:
-        case IMGFMT_I420:
-                return ff_vf_next_query_format(vf, IMGFMT_YV12);
-        }
-        return 0;
-}
-
-static void uninit(struct vf_instance *vf)
-{
-        free(vf->priv);
-}
-
-static int vf_open(vf_instance_t *vf, char *args)
-{
-        vf->config=config;
-        vf->query_format=query_format;
-        vf->put_image=put_image;
-        vf->uninit=uninit;
-
-        vf->priv = calloc(1, sizeof (struct vf_priv_s));
-        vf->priv->skipline = 0;
-        vf->priv->scalew = 1;
-        vf->priv->scaleh = 2;
-        if (args) sscanf(args, "%d:%d:%d", &vf->priv->skipline, &vf->priv->scalew, &vf->priv->scaleh);
-
-        return 1;
-}
-
-const vf_info_t ff_vf_info_down3dright = {
-        "convert stereo movie from top-bottom to left-right field",
-        "down3dright",
-        "Zdenek Kabelac",
-        "",
-        vf_open,
-        NULL
-};
diff --git a/libavfilter/libmpcodecs/vf_filmdint.c b/libavfilter/libmpcodecs/vf_filmdint.c
deleted file mode 100644
index 93354e2..0000000
--- a/libavfilter/libmpcodecs/vf_filmdint.c
+++ /dev/null
@@ -1,1461 +0,0 @@
-/*
- * This file is part of MPlayer.
- *
- * MPlayer is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * MPlayer 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with MPlayer; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/time.h>
-
-#include "config.h"
-#include "mp_msg.h"
-#include "cpudetect.h"
-
-#include "img_format.h"
-#include "mp_image.h"
-#include "vd.h"
-#include "vf.h"
-#include "cmmx.h"
-#include "libavutil/x86/asm.h"
-#include "libvo/fastmemcpy.h"
-
-#define NUM_STORED 4
-
-enum pu_field_type_t {
-    PU_1ST_OF_3,
-    PU_2ND_OF_3,
-    PU_3RD_OF_3,
-    PU_1ST_OF_2,
-    PU_2ND_OF_2,
-    PU_INTERLACED
-};
-
-struct metrics {
-    /* This struct maps to a packed word 64-bit MMX register */
-    unsigned short int even;
-    unsigned short int odd;
-    unsigned short int noise;
-    unsigned short int temp;
-} __attribute__ ((aligned (8)));
-
-struct frame_stats {
-    struct metrics tiny, low, high, bigger, twox, max;
-    struct { unsigned int even, odd, noise, temp; } sad;
-    unsigned short interlaced_high;
-    unsigned short interlaced_low;
-    unsigned short num_blocks;
-};
-
-struct vf_priv_s {
-    unsigned long inframes;
-    unsigned long outframes;
-    enum pu_field_type_t prev_type;
-    unsigned swapped, chroma_swapped;
-    unsigned luma_only;
-    unsigned verbose;
-    unsigned fast;
-    unsigned long w, h, cw, ch, stride, chroma_stride, nplanes;
-    unsigned long sad_thres;
-    unsigned long dint_thres;
-    unsigned char *memory_allocated;
-    unsigned char *planes[2*NUM_STORED][4];
-    unsigned char **old_planes;
-    unsigned long static_idx;
-    unsigned long temp_idx;
-    unsigned long crop_x, crop_y, crop_cx, crop_cy;
-    unsigned long export_count, merge_count;
-    unsigned long num_breaks;
-    unsigned long num_copies;
-    long in_inc, out_dec, iosync;
-    long num_fields;
-    long prev_fields;
-    long notout;
-    long mmx2;
-    unsigned small_bytes[2];
-    unsigned mmx_temp[2];
-    struct frame_stats stats[2];
-    struct metrics thres;
-    char chflag;
-    double diff_time, merge_time, decode_time, vo_time, filter_time;
-};
-
-#define PPZ { 2000, 2000, 0, 2000 }
-#define PPR { 2000, 2000, 0, 2000 }
-static const struct frame_stats ppzs = {PPZ,PPZ,PPZ,PPZ,PPZ,PPZ,PPZ,0,0,9999};
-static const struct frame_stats pprs = {PPR,PPR,PPR,PPR,PPR,PPR,PPR,0,0,9999};
-
-#ifndef MIN
-#define        MIN(a,b) (((a)<(b))?(a):(b))
-#endif
-#ifndef MAX
-#define        MAX(a,b) (((a)>(b))?(a):(b))
-#endif
-
-#define PDIFFUB(X,Y,T) "movq "    #X "," #T "\n\t" \
-                       "psubusb " #Y "," #T "\n\t" \
-                       "psubusb " #X "," #Y "\n\t" \
-                       "paddusb " #Y "," #T "\n\t"
-
-#define PDIFFUBT(X,Y,T) "movq "    #X "," #T "\n\t" \
-                        "psubusb " #Y "," #T "\n\t" \
-                        "psubusb " #X "," #Y "\n\t" \
-                        "paddusb " #T "," #Y "\n\t"
-
-#define PSUMBW(X,T,Z)        "movq " #X "," #T "\n\t" \
-                        "punpcklbw " #Z "," #X "\n\t" \
-                        "punpckhbw " #Z "," #T "\n\t" \
-                        "paddw " #T "," #X "\n\t" \
-                        "movq " #X "," #T "\n\t" \
-                        "psllq $32, " #T "\n\t" \
-                        "paddw " #T "," #X "\n\t" \
-                        "movq " #X "," #T "\n\t" \
-                        "psllq $16, " #T "\n\t" \
-                        "paddw " #T "," #X "\n\t" \
-                        "psrlq $48, " #X "\n\t"
-
-#define PSADBW(X,Y,T,Z)        PDIFFUBT(X,Y,T) PSUMBW(Y,T,Z)
-
-#define PMAXUB(X,Y) "psubusb " #X "," #Y "\n\tpaddusb " #X "," #Y "\n\t"
-#define PMAXUW(X,Y) "psubusw " #X "," #Y "\n\tpaddusw " #X "," #Y "\n\t"
-#define PMINUBT(X,Y,T)        "movq " #Y "," #T "\n\t" \
-                        "psubusb " #X "," #T "\n\t" \
-                        "psubusb " #T "," #Y "\n\t"
-#define PAVGB(X,Y)        "pavgusb " #X "," #Y "\n\t"
-
-static inline void
-get_metrics_c(unsigned char *a, unsigned char *b, int as, int bs, int lines,
-              struct metrics *m)
-{
-    a -= as;
-    b -= bs;
-    do {
-        cmmx_t old_po = *(cmmx_t*)(a      );
-        cmmx_t     po = *(cmmx_t*)(b      );
-        cmmx_t      e = *(cmmx_t*)(b +   bs);
-        cmmx_t  old_o = *(cmmx_t*)(a + 2*as);
-        cmmx_t      o = *(cmmx_t*)(b + 2*bs);
-        cmmx_t     ne = *(cmmx_t*)(b + 3*bs);
-        cmmx_t old_no = *(cmmx_t*)(a + 4*as);
-        cmmx_t     no = *(cmmx_t*)(b + 4*bs);
-
-        cmmx_t   qup_old_odd = p31avgb(old_o, old_po);
-        cmmx_t       qup_odd = p31avgb(    o,     po);
-        cmmx_t qdown_old_odd = p31avgb(old_o, old_no);
-        cmmx_t     qdown_odd = p31avgb(    o,     no);
-
-        cmmx_t   qup_even = p31avgb(ne, e);
-        cmmx_t qdown_even = p31avgb(e, ne);
-
-        cmmx_t    temp_up_diff = pdiffub(qdown_even, qup_old_odd);
-        cmmx_t   noise_up_diff = pdiffub(qdown_even, qup_odd);
-        cmmx_t  temp_down_diff = pdiffub(qup_even, qdown_old_odd);
-        cmmx_t noise_down_diff = pdiffub(qup_even, qdown_odd);
-
-        cmmx_t odd_diff = pdiffub(o, old_o);
-        m->odd  += psumbw(odd_diff);
-        m->even += psadbw(e, *(cmmx_t*)(a+as));
-
-        temp_up_diff  = pminub(temp_up_diff, temp_down_diff);
-        temp_up_diff  = pminub(temp_up_diff, odd_diff);
-        m->temp  += psumbw(temp_up_diff);
-        noise_up_diff = pminub(noise_up_diff, odd_diff);
-        noise_up_diff = pminub(noise_up_diff, noise_down_diff);
-
-        m->noise += psumbw(noise_up_diff);
-        a += 2*as;
-        b += 2*bs;
-    } while (--lines);
-}
-
-static inline void
-get_metrics_fast_c(unsigned char *a, unsigned char *b, int as, int bs,
-                   int lines, struct metrics *m)
-{
-    a -= as;
-    b -= bs;
-    do {
-        cmmx_t old_po = (*(cmmx_t*)(a       ) >> 1) & ~SIGN_BITS;
-        cmmx_t     po = (*(cmmx_t*)(b       ) >> 1) & ~SIGN_BITS;
-        cmmx_t  old_e = (*(cmmx_t*)(a +   as) >> 1) & ~SIGN_BITS;
-        cmmx_t      e = (*(cmmx_t*)(b +   bs) >> 1) & ~SIGN_BITS;
-        cmmx_t  old_o = (*(cmmx_t*)(a + 2*as) >> 1) & ~SIGN_BITS;
-        cmmx_t      o = (*(cmmx_t*)(b + 2*bs) >> 1) & ~SIGN_BITS;
-        cmmx_t     ne = (*(cmmx_t*)(b + 3*bs) >> 1) & ~SIGN_BITS;
-        cmmx_t old_no = (*(cmmx_t*)(a + 4*as) >> 1) & ~SIGN_BITS;
-        cmmx_t     no = (*(cmmx_t*)(b + 4*bs) >> 1) & ~SIGN_BITS;
-
-        cmmx_t   qup_old_odd = p31avgb_s(old_o, old_po);
-        cmmx_t       qup_odd = p31avgb_s(    o,     po);
-        cmmx_t qdown_old_odd = p31avgb_s(old_o, old_no);
-        cmmx_t     qdown_odd = p31avgb_s(    o,     no);
-
-        cmmx_t   qup_even = p31avgb_s(ne, e);
-        cmmx_t qdown_even = p31avgb_s(e, ne);
-
-        cmmx_t    temp_up_diff = pdiffub_s(qdown_even, qup_old_odd);
-        cmmx_t   noise_up_diff = pdiffub_s(qdown_even, qup_odd);
-        cmmx_t  temp_down_diff = pdiffub_s(qup_even, qdown_old_odd);
-        cmmx_t noise_down_diff = pdiffub_s(qup_even, qdown_odd);
-
-        cmmx_t odd_diff = pdiffub_s(o, old_o);
-        m->odd  += psumbw_s(odd_diff) << 1;
-        m->even += psadbw_s(e, old_e) << 1;
-
-        temp_up_diff  = pminub_s(temp_up_diff, temp_down_diff);
-        temp_up_diff  = pminub_s(temp_up_diff, odd_diff);
-        m->temp      += psumbw_s(temp_up_diff) << 1;
-        noise_up_diff = pminub_s(noise_up_diff, odd_diff);
-        noise_up_diff = pminub_s(noise_up_diff, noise_down_diff);
-
-        m->noise += psumbw_s(noise_up_diff) << 1;
-        a += 2*as;
-        b += 2*bs;
-    } while (--lines);
-}
-
-static inline void
-get_metrics_faster_c(unsigned char *a, unsigned char *b, int as, int bs,
-                   int lines, struct metrics *m)
-{
-    a -= as;
-    b -= bs;
-    do {
-        cmmx_t old_po = (*(cmmx_t*)(a       )>>1) & ~SIGN_BITS;
-        cmmx_t     po = (*(cmmx_t*)(b       )>>1) & ~SIGN_BITS;
-        cmmx_t  old_e = (*(cmmx_t*)(a +   as)>>1) & ~SIGN_BITS;
-        cmmx_t      e = (*(cmmx_t*)(b +   bs)>>1) & ~SIGN_BITS;
-        cmmx_t  old_o = (*(cmmx_t*)(a + 2*as)>>1) & ~SIGN_BITS;
-        cmmx_t      o = (*(cmmx_t*)(b + 2*bs)>>1) & ~SIGN_BITS;
-        cmmx_t     ne = (*(cmmx_t*)(b + 3*bs)>>1) & ~SIGN_BITS;
-
-        cmmx_t  down_even = p31avgb_s(e, ne);
-        cmmx_t     up_odd = p31avgb_s(o, po);
-        cmmx_t up_old_odd = p31avgb_s(old_o, old_po);
-
-        cmmx_t   odd_diff = pdiffub_s(o, old_o);
-        cmmx_t  temp_diff = pdiffub_s(down_even, up_old_odd);
-        cmmx_t noise_diff = pdiffub_s(down_even, up_odd);
-
-        m->even += psadbw_s(e, old_e) << 1;
-        m->odd  += psumbw_s(odd_diff) << 1;
-
-        temp_diff  = pminub_s(temp_diff, odd_diff);
-        noise_diff = pminub_s(noise_diff, odd_diff);
-
-        m->noise += psumbw_s(noise_diff) << 1;
-        m->temp  += psumbw_s(temp_diff) << 1;
-        a += 2*as;
-        b += 2*bs;
-    } while (--lines);
-
-}
-
-static inline void
-get_block_stats(struct metrics *m, struct vf_priv_s *p, struct frame_stats *s)
-{
-    unsigned two_e = m->even  + MAX(m->even , p->thres.even );
-    unsigned two_o = m->odd   + MAX(m->odd  , p->thres.odd  );
-    unsigned two_n = m->noise + MAX(m->noise, p->thres.noise);
-    unsigned two_t = m->temp  + MAX(m->temp , p->thres.temp );
-
-    unsigned e_big   = m->even  >= (m->odd   + two_o + 1)/2;
-    unsigned o_big   = m->odd   >= (m->even  + two_e + 1)/2;
-    unsigned n_big   = m->noise >= (m->temp  + two_t + 1)/2;
-    unsigned t_big   = m->temp  >= (m->noise + two_n + 1)/2;
-
-    unsigned e2x     = m->even  >= two_o;
-    unsigned o2x     = m->odd   >= two_e;
-    unsigned n2x     = m->noise >= two_t;
-    unsigned t2x     = m->temp  >= two_n;
-
-    unsigned ntiny_e = m->even  > p->thres.even ;
-    unsigned ntiny_o = m->odd   > p->thres.odd  ;
-    unsigned ntiny_n = m->noise > p->thres.noise;
-    unsigned ntiny_t = m->temp  > p->thres.temp ;
-
-    unsigned nlow_e  = m->even  > 2*p->thres.even ;
-    unsigned nlow_o  = m->odd   > 2*p->thres.odd  ;
-    unsigned nlow_n  = m->noise > 2*p->thres.noise;
-    unsigned nlow_t  = m->temp  > 2*p->thres.temp ;
-
-    unsigned high_e  = m->even  > 4*p->thres.even ;
-    unsigned high_o  = m->odd   > 4*p->thres.odd  ;
-    unsigned high_n  = m->noise > 4*p->thres.noise;
-    unsigned high_t  = m->temp  > 4*p->thres.temp ;
-
-    unsigned low_il  = !n_big && !t_big && ntiny_n && ntiny_t;
-    unsigned high_il = !n_big && !t_big && nlow_n  && nlow_t;
-
-    if (low_il | high_il) {
-        s->interlaced_low  += low_il;
-        s->interlaced_high += high_il;
-    } else {
-        s->tiny.even  += ntiny_e;
-        s->tiny.odd   += ntiny_o;
-        s->tiny.noise += ntiny_n;
-        s->tiny.temp  += ntiny_t;
-
-        s->low .even  += nlow_e ;
-        s->low .odd   += nlow_o ;
-        s->low .noise += nlow_n ;
-        s->low .temp  += nlow_t ;
-
-        s->high.even  += high_e ;
-        s->high.odd   += high_o ;
-        s->high.noise += high_n ;
-        s->high.temp  += high_t ;
-
-        if (m->even  >=        p->sad_thres) s->sad.even  += m->even ;
-        if (m->odd   >=        p->sad_thres) s->sad.odd   += m->odd  ;
-        if (m->noise >=        p->sad_thres) s->sad.noise += m->noise;
-        if (m->temp  >=        p->sad_thres) s->sad.temp  += m->temp ;
-    }
-    s->num_blocks++;
-    s->max.even  = MAX(s->max.even , m->even );
-    s->max.odd   = MAX(s->max.odd  , m->odd  );
-    s->max.noise = MAX(s->max.noise, m->noise);
-    s->max.temp  = MAX(s->max.temp , m->temp );
-
-    s->bigger.even  += e_big  ;
-    s->bigger.odd   += o_big  ;
-    s->bigger.noise += n_big  ;
-    s->bigger.temp  += t_big  ;
-
-    s->twox.even  += e2x    ;
-    s->twox.odd   += o2x    ;
-    s->twox.noise += n2x    ;
-    s->twox.temp  += t2x    ;
-
-}
-
-static inline struct metrics
-block_metrics_c(unsigned char *a, unsigned char *b, int as, int bs,
-                int lines, struct vf_priv_s *p, struct frame_stats *s)
-{
-    struct metrics tm;
-    tm.even = tm.odd = tm.noise = tm.temp = 0;
-    get_metrics_c(a, b, as, bs, lines, &tm);
-    if (sizeof(cmmx_t) < 8)
-        get_metrics_c(a+4, b+4, as, bs, lines, &tm);
-    get_block_stats(&tm, p, s);
-    return tm;
-}
-
-static inline struct metrics
-block_metrics_fast_c(unsigned char *a, unsigned char *b, int as, int bs,
-                int lines, struct vf_priv_s *p, struct frame_stats *s)
-{
-    struct metrics tm;
-    tm.even = tm.odd = tm.noise = tm.temp = 0;
-    get_metrics_fast_c(a, b, as, bs, lines, &tm);
-    if (sizeof(cmmx_t) < 8)
-        get_metrics_fast_c(a+4, b+4, as, bs, lines, &tm);
-    get_block_stats(&tm, p, s);
-    return tm;
-}
-
-static inline struct metrics
-block_metrics_faster_c(unsigned char *a, unsigned char *b, int as, int bs,
-                int lines, struct vf_priv_s *p, struct frame_stats *s)
-{
-    struct metrics tm;
-    tm.even = tm.odd = tm.noise = tm.temp = 0;
-    get_metrics_faster_c(a, b, as, bs, lines, &tm);
-    if (sizeof(cmmx_t) < 8)
-        get_metrics_faster_c(a+4, b+4, as, bs, lines, &tm);
-    get_block_stats(&tm, p, s);
-    return tm;
-}
-
-#define MEQ(X,Y) ((X).even == (Y).even && (X).odd == (Y).odd && (X).temp == (Y).temp && (X).noise == (Y).noise)
-
-#define BLOCK_METRICS_TEMPLATE() \
-    __asm__ volatile("pxor %mm7, %mm7\n\t"   /* The result is colleted in mm7 */ \
-                 "pxor %mm6, %mm6\n\t"   /* Temp to stay at 0 */             \
-        );                                                                     \
-    a -= as;                                                                     \
-    b -= bs;                                                                     \
-    do {                                                                     \
-        __asm__ volatile(                                                     \
-            "movq (%0,%2), %%mm0\n\t"                                             \
-            "movq (%1,%3), %%mm1\n\t"   /* mm1 = even */                     \
-            PSADBW(%%mm1, %%mm0, %%mm4, %%mm6)                                     \
-            "paddusw %%mm0, %%mm7\n\t"  /* even diff */                             \
-            "movq (%0,%2,2), %%mm0\n\t" /* mm0 = old odd */                     \
-            "movq (%1,%3,2), %%mm2\n\t" /* mm2 = odd */                             \
-            "movq (%0), %%mm3\n\t"                                             \
-            "psubusb %4, %%mm3\n\t"                                             \
-            PAVGB(%%mm0, %%mm3)                                                     \
-            PAVGB(%%mm0, %%mm3)    /* mm3 = qup old odd */                     \
-            "movq %%mm0, %%mm5\n\t"                                             \
-            PSADBW(%%mm2, %%mm0, %%mm4, %%mm6)                                     \
-            "psllq $16, %%mm0\n\t"                                             \
-            "paddusw %%mm0, %%mm7\n\t"                                             \
-            "movq (%1), %%mm4\n\t"                                             \
-            "lea (%0,%2,2), %0\n\t"                                             \
-            "lea (%1,%3,2), %1\n\t"                                             \
-            "psubusb %4, %%mm4\n\t"                                             \
-            PAVGB(%%mm2, %%mm4)                                                     \
-            PAVGB(%%mm2, %%mm4)    /* mm4 = qup odd */                             \
-            PDIFFUBT(%%mm5, %%mm2, %%mm0) /* mm2 =abs(oldodd-odd) */             \
-            "movq (%1,%3), %%mm5\n\t"                                             \
-            "psubusb %4, %%mm5\n\t"                                             \
-            PAVGB(%%mm1, %%mm5)                                                     \
-            PAVGB(%%mm5, %%mm1)    /* mm1 = qdown even */                     \
-            PAVGB((%1,%3), %%mm5)  /* mm5 = qup next even */                     \
-            PDIFFUBT(%%mm1, %%mm3, %%mm0) /* mm3 = abs(qupoldo-qde) */             \
-            PDIFFUBT(%%mm1, %%mm4, %%mm0) /* mm4 = abs(qupodd-qde) */             \
-            PMINUBT(%%mm2, %%mm3, %%mm0)  /* limit temp to odd diff */             \
-            PMINUBT(%%mm2, %%mm4, %%mm0)  /* limit noise to odd diff */             \
-            "movq (%1,%3,2), %%mm2\n\t"                                             \
-            "psubusb %4, %%mm2\n\t"                                             \
-            PAVGB((%1), %%mm2)                                                     \
-            PAVGB((%1), %%mm2)    /* mm2 = qdown odd */                             \
-            "movq (%0,%2,2), %%mm1\n\t"                                             \
-            "psubusb %4, %%mm1\n\t"                                             \
-            PAVGB((%0), %%mm1)                                                     \
-            PAVGB((%0), %%mm1)  /* mm1 = qdown old odd */                     \
-            PDIFFUBT(%%mm5, %%mm2, %%mm0) /* mm2 = abs(qdo-qune) */             \
-            PDIFFUBT(%%mm5, %%mm1, %%mm0) /* mm1 = abs(qdoo-qune) */             \
-            PMINUBT(%%mm4, %%mm2, %%mm0)  /* current */                             \
-            PMINUBT(%%mm3, %%mm1, %%mm0)  /* old */                             \
-            PSUMBW(%%mm2, %%mm0, %%mm6)                                             \
-            PSUMBW(%%mm1, %%mm0, %%mm6)                                             \
-            "psllq $32, %%mm2\n\t"                                             \
-            "psllq $48, %%mm1\n\t"                                             \
-            "paddusw %%mm2, %%mm7\n\t"                                             \
-            "paddusw %%mm1, %%mm7\n\t"                                             \
-            : "=r" (a), "=r" (b)                                             \
-            : "r"((x86_reg)as), "r"((x86_reg)bs), "m" (ones), "0"(a), "1"(b), "X"(*a), "X"(*b) \
-            );                                                                     \
-    } while (--lines);
-
-static inline struct metrics
-block_metrics_3dnow(unsigned char *a, unsigned char *b, int as, int bs,
-                    int lines, struct vf_priv_s *p, struct frame_stats *s)
-{
-    struct metrics tm;
-#if !HAVE_AMD3DNOW
-    ff_mp_msg(MSGT_VFILTER, MSGL_FATAL, "block_metrics_3dnow: internal error\n");
-#else
-    static const unsigned long long ones = 0x0101010101010101ull;
-
-    BLOCK_METRICS_TEMPLATE();
-    __asm__ volatile("movq %%mm7, %0\n\temms" : "=m" (tm));
-    get_block_stats(&tm, p, s);
-#endif
-    return tm;
-}
-
-#undef PSUMBW
-#undef PSADBW
-#undef PMAXUB
-#undef PMINUBT
-#undef PAVGB
-
-#define PSUMBW(X,T,Z)        "psadbw " #Z "," #X "\n\t"
-#define PSADBW(X,Y,T,Z) "psadbw " #X "," #Y "\n\t"
-#define PMAXUB(X,Y)        "pmaxub " #X "," #Y "\n\t"
-#define PMINUBT(X,Y,T)        "pminub " #X "," #Y "\n\t"
-#define PAVGB(X,Y)        "pavgb "  #X "," #Y "\n\t"
-
-static inline struct metrics
-block_metrics_mmx2(unsigned char *a, unsigned char *b, int as, int bs,
-                   int lines, struct vf_priv_s *p, struct frame_stats *s)
-{
-    struct metrics tm;
-#if !HAVE_MMX
-    ff_mp_msg(MSGT_VFILTER, MSGL_FATAL, "block_metrics_mmx2: internal error\n");
-#else
-    static const unsigned long long ones = 0x0101010101010101ull;
-    x86_reg interlaced;
-    x86_reg prefetch_line = (((long)a>>3) & 7) + 10;
-#ifdef DEBUG
-    struct frame_stats ts = *s;
-#endif
-    __asm__ volatile("prefetcht0 (%0,%2)\n\t"
-                 "prefetcht0 (%1,%3)\n\t" :
-                 : "r" (a), "r" (b),
-                 "r" (prefetch_line * as), "r" (prefetch_line * bs));
-
-    BLOCK_METRICS_TEMPLATE();
-
-    s->num_blocks++;
-    __asm__ volatile(
-        "movq %3, %%mm0\n\t"
-        "movq %%mm7, %%mm1\n\t"
-        "psubusw %%mm0, %%mm1\n\t"
-        "movq %%mm1, %%mm2\n\t"
-        "paddusw %%mm0, %%mm2\n\t"
-        "paddusw %%mm7, %%mm2\n\t"
-        "pshufw $0xb1, %%mm2, %%mm3\n\t"
-        "pavgw %%mm7, %%mm2\n\t"
-        "pshufw $0xb1, %%mm2, %%mm2\n\t"
-        "psubusw %%mm7, %%mm2\n\t"
-        "pcmpeqw %%mm6, %%mm2\n\t" /* 1 if >= 1.5x */
-        "psubusw %%mm7, %%mm3\n\t"
-        "pcmpeqw %%mm6, %%mm3\n\t" /* 1 if >= 2x */
-        "movq %1, %%mm4\n\t"
-        "movq %2, %%mm5\n\t"
-        "psubw %%mm2, %%mm4\n\t"
-        "psubw %%mm3, %%mm5\n\t"
-        "movq %%mm4, %1\n\t"
-        "movq %%mm5, %2\n\t"
-        "pxor %%mm4, %%mm4\n\t"
-        "pcmpeqw %%mm1, %%mm4\n\t" /* 1 if <= t */
-        "psubusw %%mm0, %%mm1\n\t"
-        "pxor %%mm5, %%mm5\n\t"
-        "pcmpeqw %%mm1, %%mm5\n\t" /* 1 if <= 2t */
-        "psubusw %%mm0, %%mm1\n\t"
-        "psubusw %%mm0, %%mm1\n\t"
-        "pcmpeqw %%mm6, %%mm1\n\t" /* 1 if <= 4t */
-        "pshufw $0xb1, %%mm2, %%mm0\n\t"
-        "por %%mm2, %%mm0\n\t"     /* 1 if not close */
-        "punpckhdq %%mm0, %%mm0\n\t"
-        "movq %%mm4, %%mm2\n\t"      /* tttt */
-        "punpckhdq %%mm5, %%mm2\n\t" /* ttll */
-        "por %%mm2, %%mm0\n\t"
-        "pcmpeqd %%mm6, %%mm0\n\t" /* close && big */
-        "psrlq $16, %%mm0\n\t"
-        "psrlw $15, %%mm0\n\t"
-        "movd %%mm0, %0\n\t"
-        : "=r" (interlaced), "=m" (s->bigger), "=m" (s->twox)
-        : "m" (p->thres)
-        );
-
-    if (interlaced) {
-        s->interlaced_high += interlaced >> 16;
-        s->interlaced_low += interlaced;
-    } else {
-        __asm__ volatile(
-            "pcmpeqw %%mm0, %%mm0\n\t" /* -1 */
-            "psubw         %%mm0, %%mm4\n\t"
-            "psubw         %%mm0, %%mm5\n\t"
-            "psubw         %%mm0, %%mm1\n\t"
-            "paddw %0, %%mm4\n\t"
-            "paddw %1, %%mm5\n\t"
-            "paddw %2, %%mm1\n\t"
-            "movq %%mm4, %0\n\t"
-            "movq %%mm5, %1\n\t"
-            "movq %%mm1, %2\n\t"
-            : "=m" (s->tiny), "=m" (s->low), "=m" (s->high)
-            );
-
-        __asm__ volatile(
-            "pshufw $0, %2, %%mm0\n\t"
-            "psubusw %%mm7, %%mm0\n\t"
-            "pcmpeqw %%mm6, %%mm0\n\t"   /* 0 if below sad_thres */
-            "pand %%mm7, %%mm0\n\t"
-            "movq %%mm0, %%mm1\n\t"
-            "punpcklwd %%mm6, %%mm0\n\t" /* sad even, odd */
-            "punpckhwd %%mm6, %%mm1\n\t" /* sad noise, temp */
-            "paddd %0, %%mm0\n\t"
-            "paddd %1, %%mm1\n\t"
-            "movq %%mm0, %0\n\t"
-            "movq %%mm1, %1\n\t"
-            : "=m" (s->sad.even), "=m" (s->sad.noise)
-            : "m" (p->sad_thres)
-            );
-    }
-
-    __asm__ volatile(
-        "movq %%mm7, (%1)\n\t"
-        PMAXUW((%0), %%mm7)
-        "movq %%mm7, (%0)\n\t"
-        "emms"
-        : : "r" (&s->max), "r" (&tm), "X" (s->max)
-        : "memory"
-        );
-#ifdef DEBUG
-    if (1) {
-        struct metrics cm;
-        a -= 7*as;
-        b -= 7*bs;
-        cm = block_metrics_c(a, b, as, bs, 4, p, &ts);
-        if (!MEQ(tm, cm))
-            ff_mp_msg(MSGT_VFILTER, MSGL_WARN, "Bad metrics\n");
-        if (s) {
-#           define CHECK(X) if (!MEQ(s->X, ts.X)) \
-                ff_mp_msg(MSGT_VFILTER, MSGL_WARN, "Bad " #X "\n");
-            CHECK(tiny);
-            CHECK(low);
-            CHECK(high);
-            CHECK(sad);
-            CHECK(max);
-        }
-    }
-#endif
-#endif
-    return tm;
-}
-
-static inline int
-dint_copy_line_mmx2(unsigned char *dst, unsigned char *a, long bos,
-                    long cos, int ds, int ss, int w, int t)
-{
-#if !HAVE_MMX
-    ff_mp_msg(MSGT_VFILTER, MSGL_FATAL, "dint_copy_line_mmx2: internal error\n");
-    return 0;
-#else
-    unsigned long len = (w+7) >> 3;
-    int ret;
-    __asm__ volatile (
-        "pxor %%mm6, %%mm6 \n\t"       /* deinterlaced pixel counter */
-        "movd %0, %%mm7 \n\t"
-        "punpcklbw %%mm7, %%mm7 \n\t"
-        "punpcklwd %%mm7, %%mm7 \n\t"
-        "punpckldq %%mm7, %%mm7 \n\t"  /* mm7 = threshold */
-        : /* no output */
-        : "rm" (t)
-        );
-    do {
-        __asm__ volatile (
-            "movq (%0), %%mm0\n\t"
-            "movq (%0,%3,2), %%mm1\n\t"
-            "movq %%mm0, (%2)\n\t"
-            "pmaxub %%mm1, %%mm0\n\t"
-            "pavgb (%0), %%mm1\n\t"
-            "psubusb %%mm1, %%mm0\n\t"
-            "paddusb %%mm7, %%mm0\n\t"  /* mm0 = max-avg+thr */
-            "movq (%0,%1), %%mm2\n\t"
-            "movq (%0,%5), %%mm3\n\t"
-            "movq %%mm2, %%mm4\n\t"
-            PDIFFUBT(%%mm1, %%mm2, %%mm5)
-            PDIFFUBT(%%mm1, %%mm3, %%mm5)
-            "pminub %%mm2, %%mm3\n\t"
-            "pcmpeqb %%mm3, %%mm2\n\t"  /* b = min */
-            "pand %%mm2, %%mm4\n\t"
-            "pandn (%0,%5), %%mm2\n\t"
-            "por %%mm4, %%mm2\n\t"
-            "pminub %%mm0, %%mm3\n\t"
-            "pcmpeqb %%mm0, %%mm3\n\t"  /* set to 1s if >= threshold */
-            "psubb %%mm3, %%mm6\n\t"    /* count pixels above thr. */
-            "pand %%mm3, %%mm1 \n\t"
-            "pandn %%mm2, %%mm3 \n\t"
-            "por %%mm3, %%mm1 \n\t"     /* avg if >= threshold */
-            "movq %%mm1, (%2,%4) \n\t"
-            : /* no output */
-            : "r" (a), "r" ((x86_reg)bos), "r" ((x86_reg)dst), "r" ((x86_reg)ss), "r" ((x86_reg)ds), "r" ((x86_reg)cos)
-            );
-        a += 8;
-        dst += 8;
-    } while (--len);
-
-    __asm__ volatile ("pxor %%mm7, %%mm7 \n\t"
-                  "psadbw %%mm6, %%mm7 \n\t"
-                  "movd %%mm7, %0 \n\t"
-                  "emms \n\t"
-                  : "=r" (ret)
-        );
-    return ret;
-#endif
-}
-
-static inline int
-dint_copy_line(unsigned char *dst, unsigned char *a, long bos,
-               long cos, int ds, int ss, int w, int t)
-{
-    unsigned long len = ((unsigned long)w+sizeof(cmmx_t)-1) / sizeof(cmmx_t);
-    cmmx_t dint_count = 0;
-    cmmx_t thr;
-    t |= t <<  8;
-    thr = t | (t << 16);
-    if (sizeof(cmmx_t) > 4)
-        thr |= thr << (sizeof(cmmx_t)*4);
-    do {
-        cmmx_t e = *(cmmx_t*)a;
-        cmmx_t ne = *(cmmx_t*)(a+2*ss);
-        cmmx_t o = *(cmmx_t*)(a+bos);
-        cmmx_t oo = *(cmmx_t*)(a+cos);
-        cmmx_t maxe = pmaxub(e, ne);
-        cmmx_t avge = pavgb(e, ne);
-        cmmx_t max_diff = maxe - avge + thr; /* 0<=max-avg<128, thr<128 */
-        cmmx_t diffo  = pdiffub(avge, o);
-        cmmx_t diffoo = pdiffub(avge, oo);
-        cmmx_t diffcmp = pcmpgtub(diffo, diffoo);
-        cmmx_t bo = ((oo ^ o) & diffcmp) ^ o;
-        cmmx_t diffbo = ((diffoo ^ diffo) & diffcmp) ^ diffo;
-        cmmx_t above_thr = ~pcmpgtub(max_diff, diffbo);
-        cmmx_t bo_or_avg = ((avge ^ bo) & above_thr) ^ bo;
-        dint_count += above_thr & ONE_BYTES;
-        *(cmmx_t*)(dst) = e;
-        *(cmmx_t*)(dst+ds) = bo_or_avg;
-        a += sizeof(cmmx_t);
-        dst += sizeof(cmmx_t);
-    } while (--len);
-    return psumbw(dint_count);
-}
-
-static int
-dint_copy_plane(unsigned char *d, unsigned char *a, unsigned char *b,
-                unsigned char *c, unsigned long w, unsigned long h,
-                unsigned long ds, unsigned long ss, unsigned long threshold,
-                long field, long mmx2)
-{
-    unsigned long ret = 0;
-    long bos = b - a;
-    long cos = c - a;
-    if (field) {
-        fast_memcpy(d, b, w);
-        h--;
-        d += ds;
-        a += ss;
-    }
-    bos += ss;
-    cos += ss;
-    while (h > 2) {
-        if (threshold >= 128) {
-            fast_memcpy(d, a, w);
-            fast_memcpy(d+ds, a+bos, w);
-        } else if (mmx2 == 1) {
-            ret += dint_copy_line_mmx2(d, a, bos, cos, ds, ss, w, threshold);
-        } else
-            ret += dint_copy_line(d, a, bos, cos, ds, ss, w, threshold);
-        h -= 2;
-        d += 2*ds;
-        a += 2*ss;
-    }
-    fast_memcpy(d, a, w);
-    if (h == 2)
-        fast_memcpy(d+ds, a+bos, w);
-    return ret;
-}
-
-static void
-copy_merge_fields(struct vf_priv_s *p, mp_image_t *dmpi,
-                  unsigned char **old, unsigned char **new, unsigned long show)
-{
-    unsigned long threshold = 256;
-    unsigned long field = p->swapped;
-    unsigned long dint_pixels = 0;
-    unsigned char **other = old;
-    if (show >= 12 || !(show & 3))
-        show >>= 2, other = new, new = old;
-    if (show <= 2) {  /* Single field: de-interlace */
-        threshold = p->dint_thres;
-        field ^= show & 1;
-        old = new;
-    } else if (show == 3)
-        old = new;
-    else
-        field ^= 1;
-    dint_pixels +=dint_copy_plane(dmpi->planes[0], old[0], new[0],
-                                  other[0], p->w, p->h, dmpi->stride[0],
-                                  p->stride, threshold, field, p->mmx2);
-    if (dmpi->flags & MP_IMGFLAG_PLANAR) {
-        if (p->luma_only)
-            old = new, other = new;
-        else
-            threshold = threshold/2 + 1;
-        field ^= p->chroma_swapped;
-        dint_copy_plane(dmpi->planes[1], old[1], new[1],
-                        other[1], p->cw, p->ch,        dmpi->stride[1],
-                        p->chroma_stride, threshold, field, p->mmx2);
-        dint_copy_plane(dmpi->planes[2], old[2], new[2],
-                        other[2], p->cw, p->ch, dmpi->stride[2],
-                        p->chroma_stride, threshold, field, p->mmx2);
-    }
-    if (dint_pixels > 0 && p->verbose)
-        ff_mp_msg(MSGT_VFILTER,MSGL_INFO,"Deinterlaced %lu pixels\n",dint_pixels);
-}
-
-static void diff_planes(struct vf_priv_s *p, struct frame_stats *s,
-                        unsigned char *of, unsigned char *nf,
-                        int w, int h, int os, int ns, int swapped)
-{
-    int i, y;
-    int align = -(long)nf & 7;
-    of += align;
-    nf += align;
-    w -= align;
-    if (swapped)
-        of -= os, nf -= ns;
-    i = (h*3 >> 7) & ~1;
-    of += i*os + 8;
-    nf += i*ns + 8;
-    h -= i;
-    w -= 16;
-
-    memset(s, 0, sizeof(*s));
-
-    for (y = (h-8) >> 3; y; y--) {
-        if (p->mmx2 == 1) {
-            for (i = 0; i < w; i += 8)
-                block_metrics_mmx2(of+i, nf+i, os, ns, 4, p, s);
-        } else if (p->mmx2 == 2) {
-            for (i = 0; i < w; i += 8)
-                block_metrics_3dnow(of+i, nf+i, os, ns, 4, p, s);
-        } else if (p->fast > 3) {
-            for (i = 0; i < w; i += 8)
-                block_metrics_faster_c(of+i, nf+i, os, ns, 4, p, s);
-        } else if (p->fast > 1) {
-            for (i = 0; i < w; i += 8)
-                block_metrics_fast_c(of+i, nf+i, os, ns, 4, p, s);
-        } else {
-            for (i = 0; i < w; i += 8)
-                block_metrics_c(of+i, nf+i, os, ns, 4, p, s);
-        }
-        of += 8*os;
-        nf += 8*ns;
-    }
-}
-
-#define METRICS(X) (X).even, (X).odd, (X).noise, (X).temp
-
-static void diff_fields(struct vf_priv_s *p, struct frame_stats *s,
-                        unsigned char **old, unsigned char **new)
-{
-    diff_planes(p, s, old[0], new[0], p->w, p->h,
-                p->stride, p->stride, p->swapped);
-    s->sad.even  = (s->sad.even  * 16ul) / s->num_blocks;
-    s->sad.odd   = (s->sad.odd   * 16ul) / s->num_blocks;
-    s->sad.noise = (s->sad.noise * 16ul) / s->num_blocks;
-    s->sad.temp  = (s->sad.temp  * 16ul) / s->num_blocks;
-    if (p->verbose)
-        ff_mp_msg(MSGT_VFILTER, MSGL_INFO, "%lu%c M:%d/%d/%d/%d - %d, "
-               "t:%d/%d/%d/%d, l:%d/%d/%d/%d, h:%d/%d/%d/%d, bg:%d/%d/%d/%d, "
-               "2x:%d/%d/%d/%d, sad:%d/%d/%d/%d, lil:%d, hil:%d, ios:%.1f\n",
-               p->inframes, p->chflag, METRICS(s->max), s->num_blocks,
-               METRICS(s->tiny), METRICS(s->low), METRICS(s->high),
-               METRICS(s->bigger), METRICS(s->twox), METRICS(s->sad),
-               s->interlaced_low, s->interlaced_high,
-               p->iosync / (double) p->in_inc);
-}
-
-static const char *parse_args(struct vf_priv_s *p, const char *args)
-{
-    args--;
-    while (args && *++args &&
-           (sscanf(args, "io=%lu:%lu", &p->out_dec, &p->in_inc) == 2 ||
-            sscanf(args, "diff_thres=%hu", &p->thres.even ) == 1 ||
-            sscanf(args, "comb_thres=%hu", &p->thres.noise) == 1 ||
-            sscanf(args, "sad_thres=%lu",  &p->sad_thres  ) == 1 ||
-            sscanf(args, "dint_thres=%lu", &p->dint_thres ) == 1 ||
-            sscanf(args, "fast=%u",        &p->fast       ) == 1 ||
-            sscanf(args, "mmx2=%lu",       &p->mmx2       ) == 1 ||
-            sscanf(args, "luma_only=%u",   &p->luma_only  ) == 1 ||
-            sscanf(args, "verbose=%u",     &p->verbose    ) == 1 ||
-            sscanf(args, "crop=%lu:%lu:%lu:%lu", &p->w,
-                   &p->h, &p->crop_x, &p->crop_y) == 4))
-        args = strchr(args, '/');
-    return args;
-}
-
-static unsigned long gcd(unsigned long x, unsigned long y)
-{
-    unsigned long t;
-    if (x > y)
-        t = x, x = y, y = t;
-
-    while (x) {
-        t = y % x;
-        y = x;
-        x = t;
-    }
-    return y;
-}
-
-static void init(struct vf_priv_s *p, mp_image_t *mpi)
-{
-    unsigned long i;
-    unsigned long plane_size, chroma_plane_size;
-    unsigned char *plane;
-    unsigned long cos, los;
-    p->crop_cx = p->crop_x >> mpi->chroma_x_shift;
-    p->crop_cy = p->crop_y >> mpi->chroma_y_shift;
-    if (mpi->flags & MP_IMGFLAG_ACCEPT_STRIDE) {
-        p->stride = (mpi->w + 15) & ~15;
-        p->chroma_stride = p->stride >> mpi->chroma_x_shift;
-    } else {
-        p->stride = mpi->width;
-        p->chroma_stride = mpi->chroma_width;
-    }
-    p->cw = p->w >> mpi->chroma_x_shift;
-    p->ch = p->h >> mpi->chroma_y_shift;
-    p->nplanes = 1;
-    p->static_idx = 0;
-    p->temp_idx = 0;
-    p->old_planes = p->planes[0];
-    plane_size = mpi->h * p->stride;
-    chroma_plane_size = mpi->flags & MP_IMGFLAG_PLANAR ?
-        mpi->chroma_height * p->chroma_stride : 0;
-    p->memory_allocated =
-        malloc(NUM_STORED * (plane_size+2*chroma_plane_size) +
-               8*p->chroma_stride + 4096);
-    /* align to page boundary */
-    plane = p->memory_allocated + (-(long)p->memory_allocated & 4095);
-    memset(plane, 0, NUM_STORED * plane_size);
-    los = p->crop_x  + p->crop_y  * p->stride;
-    cos = p->crop_cx + p->crop_cy * p->chroma_stride;
-    for (i = 0; i != NUM_STORED; i++, plane += plane_size) {
-        p->planes[i][0] = plane;
-        p->planes[NUM_STORED + i][0] = plane + los;
-    }
-    if (mpi->flags & MP_IMGFLAG_PLANAR) {
-        p->nplanes = 3;
-        memset(plane, 0x80, NUM_STORED * 2 * chroma_plane_size);
-        for (i = 0; i != NUM_STORED; i++) {
-            p->planes[i][1] = plane;
-            p->planes[NUM_STORED + i][1] = plane + cos;
-            plane += chroma_plane_size;
-            p->planes[i][2] = plane;
-            p->planes[NUM_STORED + i][2] = plane + cos;
-            plane += chroma_plane_size;
-        }
-    }
-    p->out_dec <<= 2;
-    i = gcd(p->in_inc, p->out_dec);
-    p->in_inc /= i;
-    p->out_dec /= i;
-    p->iosync = 0;
-    p->num_fields = 3;
-}
-
-static inline double get_time(void)
-{
-    struct timeval tv;
-    gettimeofday(&tv, 0);
-    return tv.tv_sec + tv.tv_usec * 1e-6;
-}
-
-static void get_image(struct vf_instance *vf, mp_image_t *mpi)
-{
-    struct vf_priv_s *p = vf->priv;
-    static unsigned char **planes, planes_idx;
-
-    if (mpi->type == MP_IMGTYPE_STATIC) return;
-
-    if (!p->planes[0][0]) init(p, mpi);
-
-    if (mpi->type == MP_IMGTYPE_TEMP ||
-        (mpi->type == MP_IMGTYPE_IPB && !(mpi->flags & MP_IMGFLAG_READABLE)))
-        planes_idx = NUM_STORED/2 + (++p->temp_idx % (NUM_STORED/2));
-    else
-        planes_idx = ++p->static_idx % (NUM_STORED/2);
-    planes = p->planes[planes_idx];
-    mpi->priv = p->planes[NUM_STORED + planes_idx];
-    if (mpi->priv == p->old_planes) {
-        unsigned char **old_planes =
-            p->planes[NUM_STORED + 2 + (++p->temp_idx & 1)];
-        my_memcpy_pic(old_planes[0], p->old_planes[0],
-                      p->w, p->h, p->stride, p->stride);
-        if (mpi->flags & MP_IMGFLAG_PLANAR) {
-            my_memcpy_pic(old_planes[1], p->old_planes[1],
-                          p->cw, p->ch, p->chroma_stride, p->chroma_stride);
-            my_memcpy_pic(old_planes[2], p->old_planes[2],
-                          p->cw, p->ch, p->chroma_stride, p->chroma_stride);
-        }
-        p->old_planes = old_planes;
-        p->num_copies++;
-    }
-    mpi->planes[0] = planes[0];
-    mpi->stride[0] = p->stride;
-    if (mpi->flags & MP_IMGFLAG_PLANAR) {
-        mpi->planes[1] = planes[1];
-        mpi->planes[2] = planes[2];
-        mpi->stride[1] = mpi->stride[2] = p->chroma_stride;
-    }
-    mpi->width = p->stride;
-
-    mpi->flags |= MP_IMGFLAG_DIRECT;
-    mpi->flags &= ~MP_IMGFLAG_DRAW_CALLBACK;
-}
-
-static inline long
-cmpe(unsigned long x, unsigned long y, unsigned long err, unsigned long e)
-{
-    long diff = x-y;
-    long unit = ((x+y+err) >> e);
-    long ret = (diff > unit) - (diff < -unit);
-    unit >>= 1;
-    return ret + (diff > unit) - (diff < -unit);
-}
-
-static unsigned long
-find_breaks(struct vf_priv_s *p, struct frame_stats *s)
-{
-    struct frame_stats *ps = &p->stats[(p->inframes-1) & 1];
-    long notfilm = 5*p->in_inc - p->out_dec;
-    unsigned long n = s->num_blocks >> 8;
-    unsigned long sad_comb_cmp = cmpe(s->sad.temp, s->sad.noise, 512, 1);
-    unsigned long ret = 8;
-
-    if (cmpe(s->sad.temp, s->sad.even, 512, 1) > 0)
-        ff_mp_msg(MSGT_VFILTER, MSGL_WARN,
-               "@@@@@@@@ Bottom-first field??? @@@@@@@@\n");
-    if (s->sad.temp > 1000 && s->sad.noise > 1000)
-        return 3;
-    if (s->interlaced_high >= 2*n && s->sad.temp > 256 && s->sad.noise > 256)
-        return 3;
-    if (s->high.noise > s->num_blocks/4 && s->sad.noise > 10000 &&
-        s->sad.noise > 2*s->sad.even && s->sad.noise > 2*ps->sad.odd) {
-        // Mid-frame scene change
-        if (s->tiny.temp + s->interlaced_low  < n   ||
-            s->low.temp  + s->interlaced_high < n/4 ||
-            s->high.temp + s->interlaced_high < n/8 ||
-            s->sad.temp < 160)
-            return 1;
-        return 3;
-    }
-    if (s->high.temp > s->num_blocks/4 && s->sad.temp > 10000 &&
-        s->sad.temp > 2*ps->sad.odd && s->sad.temp > 2*ps->sad.even) {
-        // Start frame scene change
-        if (s->tiny.noise + s->interlaced_low  < n   ||
-            s->low.noise  + s->interlaced_high < n/4 ||
-            s->high.noise + s->interlaced_high < n/8 ||
-            s->sad.noise < 160)
-            return 2;
-        return 3;
-    }
-    if (sad_comb_cmp == 2)
-        return 2;
-    if (sad_comb_cmp == -2)
-        return 1;
-
-    if (s->tiny.odd > 3*MAX(n,s->tiny.even) + s->interlaced_low)
-        return 1;
-    if (s->tiny.even > 3*MAX(n,s->tiny.odd)+s->interlaced_low &&
-        (!sad_comb_cmp || (s->low.noise <= n/4 && s->low.temp <= n/4)))
-        return 4;
-
-    if (s->sad.noise < 64 && s->sad.temp < 64 &&
-        s->low.noise <= n/2 && s->high.noise <= n/4 &&
-        s->low.temp  <= n/2 && s->high.temp  <= n/4)
-        goto still;
-
-    if (s->tiny.temp > 3*MAX(n,s->tiny.noise) + s->interlaced_low)
-        return 2;
-    if (s->tiny.noise > 3*MAX(n,s->tiny.temp) + s->interlaced_low)
-        return 1;
-
-    if (s->low.odd > 3*MAX(n/4,s->low.even) + s->interlaced_high)
-        return 1;
-    if (s->low.even > 3*MAX(n/4,s->low.odd)+s->interlaced_high &&
-        s->sad.even > 2*s->sad.odd &&
-        (!sad_comb_cmp || (s->low.noise <= n/4 && s->low.temp <= n/4)))
-        return 4;
-
-    if (s->low.temp > 3*MAX(n/4,s->low.noise) + s->interlaced_high)
-        return 2;
-    if (s->low.noise > 3*MAX(n/4,s->low.temp) + s->interlaced_high)
-        return 1;
-
-    if (sad_comb_cmp == 1 && s->sad.noise < 64)
-        return 2;
-    if (sad_comb_cmp == -1 && s->sad.temp < 64)
-        return 1;
-
-    if (s->tiny.odd <= n || (s->tiny.noise <= n/2 && s->tiny.temp <= n/2)) {
-        if (s->interlaced_low <= n) {
-            if (p->num_fields == 1)
-                goto still;
-            if (s->tiny.even <= n || ps->tiny.noise <= n/2)
-                /* Still frame */
-                goto still;
-            if (s->bigger.even >= 2*MAX(n,s->bigger.odd) + s->interlaced_low)
-                return 4;
-            if (s->low.even >= 2*n + s->interlaced_low)
-                return 4;
-            goto still;
-        }
-    }
-    if (s->low.odd <= n/4) {
-        if (s->interlaced_high <= n/4) {
-            if (p->num_fields == 1)
-                goto still;
-            if (s->low.even <= n/4)
-                /* Still frame */
-                goto still;
-            if (s->bigger.even >= 2*MAX(n/4,s->bigger.odd)+s->interlaced_high)
-                return 4;
-            if (s->low.even >= n/2 + s->interlaced_high)
-                return 4;
-            goto still;
-        }
-    }
-    if (s->bigger.temp > 2*MAX(n,s->bigger.noise) + s->interlaced_low)
-        return 2;
-    if (s->bigger.noise > 2*MAX(n,s->bigger.temp) + s->interlaced_low)
-        return 1;
-    if (s->bigger.temp > 2*MAX(n,s->bigger.noise) + s->interlaced_high)
-        return 2;
-    if (s->bigger.noise > 2*MAX(n,s->bigger.temp) + s->interlaced_high)
-        return 1;
-    if (s->twox.temp > 2*MAX(n,s->twox.noise) + s->interlaced_high)
-        return 2;
-    if (s->twox.noise > 2*MAX(n,s->twox.temp) + s->interlaced_high)
-        return 1;
-    if (s->bigger.even > 2*MAX(n,s->bigger.odd) + s->interlaced_low &&
-        s->bigger.temp < n && s->bigger.noise < n)
-        return 4;
-    if (s->interlaced_low > MIN(2*n, s->tiny.odd))
-        return 3;
-    ret = 8 + (1 << (s->sad.temp > s->sad.noise));
-  still:
-    if (p->num_fields == 1 && p->prev_fields == 3 && notfilm >= 0 &&
-        (s->tiny.temp <= s->tiny.noise || s->sad.temp < s->sad.noise+16))
-        return 1;
-    if (p->notout < p->num_fields && p->iosync > 2*p->in_inc && notfilm < 0)
-        notfilm = 0;
-    if (p->num_fields < 2 ||
-        (p->num_fields == 2 && p->prev_fields == 2 && notfilm < 0))
-        return ret;
-    if (!notfilm && (p->prev_fields&~1) == 2) {
-        if (p->prev_fields + p->num_fields == 5) {
-            if (s->tiny.noise <= s->tiny.temp ||
-                s->low.noise == 0 || s->low.noise < s->low.temp ||
-                s->sad.noise < s->sad.temp+16)
-                return 2;
-        }
-        if (p->prev_fields + p->num_fields == 4) {
-            if (s->tiny.temp <= s->tiny.noise ||
-                s->low.temp == 0 || s->low.temp < s->low.noise ||
-                s->sad.temp < s->sad.noise+16)
-                return 1;
-        }
-    }
-    if (p->num_fields > 2 &&
-        ps->sad.noise > s->sad.noise && ps->sad.noise > s->sad.temp)
-        return 4;
-    return 2 >> (s->sad.noise > s->sad.temp);
-}
-
-#define ITOC(X) (!(X) ? ' ' : (X) + ((X)>9 ? 'a'-10 : '0'))
-
-static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
-{
-    mp_image_t *dmpi;
-    struct vf_priv_s *p = vf->priv;
-    unsigned char **planes, **old_planes;
-    struct frame_stats *s  = &p->stats[p->inframes & 1];
-    struct frame_stats *ps = &p->stats[(p->inframes-1) & 1];
-    int swapped = 0;
-    const int flags = mpi->fields;
-    int breaks, prev;
-    int show_fields = 0;
-    int dropped_fields = 0;
-    double start_time, diff_time;
-    char prev_chflag = p->chflag;
-    int keep_rate;
-
-    if (!p->planes[0][0]) init(p, mpi);
-
-    old_planes = p->old_planes;
-
-    if ((mpi->flags & MP_IMGFLAG_DIRECT) && mpi->priv) {
-        planes = mpi->priv;
-        mpi->priv = 0;
-    } else {
-        planes = p->planes[2 + (++p->temp_idx & 1)];
-        my_memcpy_pic(planes[0],
-                      mpi->planes[0] + p->crop_x + p->crop_y * mpi->stride[0],
-                      p->w, p->h, p->stride, mpi->stride[0]);
-        if (mpi->flags & MP_IMGFLAG_PLANAR) {
-            my_memcpy_pic(planes[1],
-                          mpi->planes[1] + p->crop_cx + p->crop_cy * mpi->stride[1],
-                          p->cw, p->ch, p->chroma_stride, mpi->stride[1]);
-            my_memcpy_pic(planes[2],
-                          mpi->planes[2] + p->crop_cx + p->crop_cy * mpi->stride[2],
-                          p->cw, p->ch, p->chroma_stride, mpi->stride[2]);
-            p->num_copies++;
-        }
-    }
-
-    p->old_planes = planes;
-    p->chflag = ';';
-    if (flags & MP_IMGFIELD_ORDERED) {
-        swapped = !(flags & MP_IMGFIELD_TOP_FIRST);
-        p->chflag = (flags & MP_IMGFIELD_REPEAT_FIRST ? '|' :
-                     flags & MP_IMGFIELD_TOP_FIRST ? ':' : '.');
-    }
-    p->swapped = swapped;
-
-    start_time = get_time();
-    if (p->chflag == '|') {
-        *s = ppzs;
-        p->iosync += p->in_inc;
-    } else if ((p->fast & 1) && prev_chflag == '|')
-        *s = pprs;
-    else
-        diff_fields(p, s, old_planes, planes);
-    diff_time = get_time();
-    p->diff_time += diff_time - start_time;
-    breaks = p->inframes ? find_breaks(p, s) : 2;
-    p->inframes++;
-    keep_rate = 4*p->in_inc == p->out_dec;
-
-    switch (breaks) {
-      case 0:
-      case 8:
-      case 9:
-      case 10:
-        if (!keep_rate && p->notout < p->num_fields && p->iosync < 2*p->in_inc)
-            break;
-        if (p->notout < p->num_fields)
-            dropped_fields = -2;
-      case 4:
-        if (keep_rate || p->iosync >= -2*p->in_inc)
-            show_fields = (4<<p->num_fields)-1;
-        break;
-      case 3:
-        if (keep_rate)
-            show_fields = 2;
-        else if (p->iosync > 0) {
-            if (p->notout >= p->num_fields && p->iosync > 2*p->in_inc) {
-                show_fields = 4; /* prev odd only */
-                if (p->num_fields > 1)
-                    show_fields |= 8; /* + prev even */
-            } else {
-                show_fields = 2; /* even only */
-                if (p->notout >= p->num_fields)
-                    dropped_fields += p->num_fields;
-            }
-        }
-        break;
-      case 2:
-        if (p->iosync <= -3*p->in_inc) {
-            if (p->notout >= p->num_fields)
-                dropped_fields = p->num_fields;
-            break;
-        }
-        if (p->num_fields == 1) {
-            int prevbreak = ps->sad.noise >= 128;
-            if (p->iosync < 4*p->in_inc) {
-                show_fields = 3;
-                dropped_fields = prevbreak;
-            } else {
-                show_fields = 4 | (!prevbreak << 3);
-                if (p->notout < 1 + p->prev_fields)
-                    dropped_fields = -!prevbreak;
-            }
-            break;
-        }
-      default:
-        if (keep_rate)
-            show_fields = 3 << (breaks & 1);
-        else if (p->notout >= p->num_fields &&
-            p->iosync >= (breaks == 1 ? -p->in_inc :
-                          p->in_inc << (p->num_fields == 1))) {
-            show_fields = (1 << (2 + p->num_fields)) - (1<<breaks);
-        } else {
-            if (p->notout >= p->num_fields)
-                dropped_fields += p->num_fields + 2 - breaks;
-            if (breaks == 1) {
-                if (p->iosync >= 4*p->in_inc)
-                    show_fields = 6;
-            } else if (p->iosync > -3*p->in_inc)
-                show_fields = 3;  /* odd+even */
-        }
-        break;
-    }
-
-    show_fields &= 15;
-    prev = p->prev_fields;
-    if (breaks < 8) {
-        if (p->num_fields == 1)
-            breaks &= ~4;
-        if (breaks)
-            p->num_breaks++;
-        if (breaks == 3)
-            p->prev_fields = p->num_fields = 1;
-        else if (breaks) {
-            p->prev_fields = p->num_fields + (breaks==1) - (breaks==4);
-            p->num_fields = breaks - (breaks == 4) + (p->chflag == '|');
-        } else
-            p->num_fields += 2;
-    } else
-        p->num_fields += 2;
-
-    p->iosync += 4 * p->in_inc;
-    if (p->chflag == '|')
-        p->iosync += p->in_inc;
-
-    if (show_fields) {
-        p->iosync -= p->out_dec;
-        p->notout = !(show_fields & 1) + !(show_fields & 3);
-        if (((show_fields &  3) ==  3 &&
-             (s->low.noise + s->interlaced_low < (s->num_blocks>>8) ||
-              s->sad.noise < 160)) ||
-            ((show_fields & 12) == 12 &&
-             (ps->low.noise + ps->interlaced_low < (s->num_blocks>>8) ||
-              ps->sad.noise < 160))) {
-            p->export_count++;
-            dmpi = ff_vf_get_image(vf->next, mpi->imgfmt, MP_IMGTYPE_EXPORT,
-                                MP_IMGFLAG_PRESERVE|MP_IMGFLAG_READABLE,
-                                p->w, p->h);
-            if ((show_fields & 3) != 3) planes = old_planes;
-            dmpi->planes[0] = planes[0];
-            dmpi->stride[0] = p->stride;
-            dmpi->width = mpi->width;
-            if (mpi->flags & MP_IMGFLAG_PLANAR) {
-                dmpi->planes[1] = planes[1];
-                dmpi->planes[2] = planes[2];
-                dmpi->stride[1] = p->chroma_stride;
-                dmpi->stride[2] = p->chroma_stride;
-            }
-        } else {
-            p->merge_count++;
-            dmpi = ff_vf_get_image(vf->next, mpi->imgfmt,
-                                MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
-                                p->w, p->h);
-            copy_merge_fields(p, dmpi, old_planes, planes, show_fields);
-        }
-        p->outframes++;
-    } else
-        p->notout += 2;
-
-    if (p->verbose)
-        ff_mp_msg(MSGT_VFILTER, MSGL_INFO, "%lu %lu: %x %c %c %lu%s%s%c%s\n",
-               p->inframes, p->outframes,
-               breaks, breaks<8 && breaks>0 ? (int) p->prev_fields+'0' : ' ',
-               ITOC(show_fields),
-               p->num_breaks, 5*p->in_inc == p->out_dec && breaks<8 &&
-               breaks>0 && ((prev&~1)!=2 || prev+p->prev_fields!=5) ?
-               " ######## bad telecine ########" : "",
-               dropped_fields ? " ======== dropped ":"", ITOC(dropped_fields),
-               !show_fields || (show_fields & (show_fields-1)) ?
-               "" : " @@@@@@@@@@@@@@@@@");
-
-    p->merge_time += get_time() - diff_time;
-    return show_fields ? ff_vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE) : 0;
-}
-
-static int query_format(struct vf_instance *vf, unsigned int fmt)
-{
-    /* FIXME - support more formats */
-    switch (fmt) {
-      case IMGFMT_YV12:
-      case IMGFMT_IYUV:
-      case IMGFMT_I420:
-      case IMGFMT_411P:
-      case IMGFMT_422P:
-      case IMGFMT_444P:
-        return ff_vf_next_query_format(vf, fmt);
-    }
-    return 0;
-}
-
-static int config(struct vf_instance *vf,
-                  int width, int height, int d_width, int d_height,
-                  unsigned int flags, unsigned int outfmt)
-{
-    unsigned long cxm = 0;
-    unsigned long cym = 0;
-    struct vf_priv_s *p = vf->priv;
-    // rounding:
-    if(!IMGFMT_IS_RGB(outfmt) && !IMGFMT_IS_BGR(outfmt)){
-        switch(outfmt){
-          case IMGFMT_444P:
-          case IMGFMT_Y800:
-          case IMGFMT_Y8:
-            break;
-          case IMGFMT_YVU9:
-          case IMGFMT_IF09:
-            cym = 3;
-          case IMGFMT_411P:
-            cxm = 3;
-            break;
-          case IMGFMT_YV12:
-          case IMGFMT_I420:
-          case IMGFMT_IYUV:
-            cym = 1;
-          default:
-            cxm = 1;
-        }
-    }
-    p->chroma_swapped = !!(p->crop_y & (cym+1));
-    if (p->w) p->w += p->crop_x & cxm;
-    if (p->h) p->h += p->crop_y & cym;
-    p->crop_x &= ~cxm;
-    p->crop_y &= ~cym;
-    if (!p->w || p->w > width ) p->w = width;
-    if (!p->h || p->h > height) p->h = height;
-    if (p->crop_x + p->w > width ) p->crop_x = 0;
-    if (p->crop_y + p->h > height) p->crop_y = 0;
-
-    if(!opt_screen_size_x && !opt_screen_size_y){
-        d_width = d_width * p->w/width;
-        d_height = d_height * p->h/height;
-    }
-    return ff_vf_next_config(vf, p->w, p->h, d_width, d_height, flags, outfmt);
-}
-
-static void uninit(struct vf_instance *vf)
-{
-    struct vf_priv_s *p = vf->priv;
-    ff_mp_msg(MSGT_VFILTER, MSGL_INFO, "diff_time: %.3f, merge_time: %.3f, "
-           "export: %lu, merge: %lu, copy: %lu\n", p->diff_time, p->merge_time,
-           p->export_count, p->merge_count, p->num_copies);
-    free(p->memory_allocated);
-    free(p);
-}
-
-static int vf_open(vf_instance_t *vf, char *args)
-{
-    struct vf_priv_s *p;
-    vf->get_image = get_image;
-    vf->put_image = put_image;
-    vf->config = config;
-    vf->query_format = query_format;
-    vf->uninit = uninit;
-    vf->default_reqs = VFCAP_ACCEPT_STRIDE;
-    vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
-    p->out_dec = 5;
-    p->in_inc = 4;
-    p->thres.noise = 128;
-    p->thres.even  = 128;
-    p->sad_thres = 64;
-    p->dint_thres = 4;
-    p->luma_only = 0;
-    p->fast = 3;
-    p->mmx2 = ff_gCpuCaps.hasMMX2 ? 1 : ff_gCpuCaps.has3DNow ? 2 : 0;
-    if (args) {
-        const char *args_remain = parse_args(p, args);
-        if (args_remain) {
-            ff_mp_msg(MSGT_VFILTER, MSGL_FATAL,
-                   "filmdint: unknown suboption: %s\n", args_remain);
-            return 0;
-        }
-        if (p->out_dec < p->in_inc) {
-            ff_mp_msg(MSGT_VFILTER, MSGL_FATAL,
-                   "filmdint: increasing the frame rate is not supported\n");
-            return 0;
-        }
-    }
-    if (p->mmx2 > 2)
-        p->mmx2 = 0;
-#if !HAVE_MMX
-    p->mmx2 = 0;
-#endif
-#if !HAVE_AMD3DNOW
-    p->mmx2 &= 1;
-#endif
-    p->thres.odd  = p->thres.even;
-    p->thres.temp = p->thres.noise;
-    p->diff_time = 0;
-    p->merge_time = 0;
-    return 1;
-}
-
-const vf_info_t ff_vf_info_filmdint = {
-    "Advanced inverse telecine filer",
-    "filmdint",
-    "Zoltan Hidvegi",
-    "",
-    vf_open,
-    NULL
-};
diff --git a/libavfilter/libmpcodecs/vf_harddup.c b/libavfilter/libmpcodecs/vf_harddup.c
deleted file mode 100644
index 7ba62d4..0000000
--- a/libavfilter/libmpcodecs/vf_harddup.c
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * This file is part of MPlayer.
- *
- * MPlayer is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * MPlayer 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with MPlayer; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "config.h"
-#include "mp_msg.h"
-
-#include "img_format.h"
-#include "mp_image.h"
-#include "vf.h"
-
-struct vf_priv_s {
-    mp_image_t *last_mpi;
-};
-
-static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
-{
-    mp_image_t *dmpi;
-
-    vf->priv->last_mpi = mpi;
-
-    dmpi = ff_vf_get_image(vf->next, mpi->imgfmt,
-        MP_IMGTYPE_EXPORT, 0, mpi->width, mpi->height);
-
-    dmpi->planes[0] = mpi->planes[0];
-    dmpi->stride[0] = mpi->stride[0];
-    if (dmpi->flags&MP_IMGFLAG_PLANAR) {
-        dmpi->planes[1] = mpi->planes[1];
-        dmpi->stride[1] = mpi->stride[1];
-        dmpi->planes[2] = mpi->planes[2];
-        dmpi->stride[2] = mpi->stride[2];
-    }
-
-    return ff_vf_next_put_image(vf, dmpi, pts);
-}
-
-static int control(struct vf_instance *vf, int request, void* data)
-{
-    switch (request) {
-    case VFCTRL_DUPLICATE_FRAME:
-        if (!vf->priv->last_mpi) break;
-        // This is a huge hack. We assume nothing
-        // has been called earlier in the filter chain
-        // since the last put_image. This is reasonable
-        // because we're handling a duplicate frame!
-        if (put_image(vf, vf->priv->last_mpi, MP_NOPTS_VALUE))
-            return CONTROL_TRUE;
-        break;
-    }
-    return ff_vf_next_control(vf, request, data);
-}
-
-static void uninit(struct vf_instance *vf)
-{
-    free(vf->priv);
-}
-
-static int vf_open(vf_instance_t *vf, char *args)
-{
-    vf->put_image = put_image;
-    vf->control = control;
-    vf->uninit = uninit;
-    vf->priv = calloc(1, sizeof(struct vf_priv_s));
-    return 1;
-}
-
-const vf_info_t ff_vf_info_harddup = {
-    "resubmit duplicate frames for encoding",
-    "harddup",
-    "Rich Felker",
-    "",
-    vf_open,
-    NULL
-};
diff --git a/libavfilter/libmpcodecs/vf_ivtc.c b/libavfilter/libmpcodecs/vf_ivtc.c
deleted file mode 100644
index 8a47a57..0000000
--- a/libavfilter/libmpcodecs/vf_ivtc.c
+++ /dev/null
@@ -1,550 +0,0 @@
-/*
- * This file is part of MPlayer.
- *
- * MPlayer is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * MPlayer 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with MPlayer; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "config.h"
-#include "mp_msg.h"
-#include "cpudetect.h"
-
-#include "img_format.h"
-#include "mp_image.h"
-#include "vf.h"
-#include "libavutil/x86/asm.h"
-#include "libvo/fastmemcpy.h"
-
-
-struct metrics {
-    /* difference: total, even lines, odd lines */
-    int d, e, o;
-    /* noise: temporal, spacial (current), spacial (past) */
-    int t, s, p;
-};
-
-struct frameinfo {
-    /* peak, relative, mean */
-    struct metrics p, r, m;
-};
-
-struct vf_priv_s {
-    struct frameinfo fi[2];
-    mp_image_t *dmpi;
-    int first;
-    int drop, lastdrop, dropnext;
-    int inframes, outframes;
-};
-
-enum {
-    F_DROP,
-    F_MERGE,
-    F_NEXT,
-    F_SHOW
-};
-
-#if HAVE_MMX && HAVE_EBX_AVAILABLE
-static void block_diffs_MMX(struct metrics *m, unsigned char *old, unsigned char *new, int os, int ns)
-{
-    int i;
-    short out[24]; // output buffer for the partial metrics from the mmx code
-
-    __asm__ (
-        "movl $4, %%ecx \n\t"
-        "pxor %%mm4, %%mm4 \n\t" // 4 even difference sums
-        "pxor %%mm5, %%mm5 \n\t" // 4 odd difference sums
-        "pxor %%mm7, %%mm7 \n\t" // all zeros
-
-        ASMALIGN(4)
-        "1: \n\t"
-
-        // Even difference
-        "movq (%%"REG_S"), %%mm0 \n\t"
-        "movq (%%"REG_S"), %%mm2 \n\t"
-        "add %%"REG_a", %%"REG_S" \n\t"
-        "movq (%%"REG_D"), %%mm1 \n\t"
-        "add %%"REG_b", %%"REG_D" \n\t"
-        "psubusb %%mm1, %%mm2 \n\t"
-        "psubusb %%mm0, %%mm1 \n\t"
-        "movq %%mm2, %%mm0 \n\t"
-        "movq %%mm1, %%mm3 \n\t"
-        "punpcklbw %%mm7, %%mm0 \n\t"
-        "punpcklbw %%mm7, %%mm1 \n\t"
-        "punpckhbw %%mm7, %%mm2 \n\t"
-        "punpckhbw %%mm7, %%mm3 \n\t"
-        "paddw %%mm0, %%mm4 \n\t"
-        "paddw %%mm1, %%mm4 \n\t"
-        "paddw %%mm2, %%mm4 \n\t"
-        "paddw %%mm3, %%mm4 \n\t"
-
-        // Odd difference
-        "movq (%%"REG_S"), %%mm0 \n\t"
-        "movq (%%"REG_S"), %%mm2 \n\t"
-        "add %%"REG_a", %%"REG_S" \n\t"
-        "movq (%%"REG_D"), %%mm1 \n\t"
-        "add %%"REG_b", %%"REG_D" \n\t"
-        "psubusb %%mm1, %%mm2 \n\t"
-        "psubusb %%mm0, %%mm1 \n\t"
-        "movq %%mm2, %%mm0 \n\t"
-        "movq %%mm1, %%mm3 \n\t"
-        "punpcklbw %%mm7, %%mm0 \n\t"
-        "punpcklbw %%mm7, %%mm1 \n\t"
-        "punpckhbw %%mm7, %%mm2 \n\t"
-        "punpckhbw %%mm7, %%mm3 \n\t"
-        "paddw %%mm0, %%mm5 \n\t"
-        "paddw %%mm1, %%mm5 \n\t"
-        "paddw %%mm2, %%mm5 \n\t"
-        "paddw %%mm3, %%mm5 \n\t"
-
-        "decl %%ecx \n\t"
-        "jnz 1b \n\t"
-        "movq %%mm4, (%%"REG_d") \n\t"
-        "movq %%mm5, 8(%%"REG_d") \n\t"
-        :
-        : "S" (old), "D" (new), "a" (os), "b" (ns), "d" (out)
-        : "memory"
-        );
-    m->e = out[0]+out[1]+out[2]+out[3];
-    m->o = out[4]+out[5]+out[6]+out[7];
-    m->d = m->e + m->o;
-
-    __asm__ (
-        // First loop to measure first four columns
-        "movl $4, %%ecx \n\t"
-        "pxor %%mm4, %%mm4 \n\t" // Past spacial noise
-        "pxor %%mm5, %%mm5 \n\t" // Temporal noise
-        "pxor %%mm6, %%mm6 \n\t" // Current spacial noise
-
-        ASMALIGN(4)
-        "2: \n\t"
-
-        "movq (%%"REG_S"), %%mm0 \n\t"
-        "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
-        "add %%"REG_a", %%"REG_S" \n\t"
-        "add %%"REG_a", %%"REG_S" \n\t"
-        "movq (%%"REG_D"), %%mm2 \n\t"
-        "movq (%%"REG_D",%%"REG_b"), %%mm3 \n\t"
-        "add %%"REG_b", %%"REG_D" \n\t"
-        "add %%"REG_b", %%"REG_D" \n\t"
-        "punpcklbw %%mm7, %%mm0 \n\t"
-        "punpcklbw %%mm7, %%mm1 \n\t"
-        "punpcklbw %%mm7, %%mm2 \n\t"
-        "punpcklbw %%mm7, %%mm3 \n\t"
-        "paddw %%mm1, %%mm4 \n\t"
-        "paddw %%mm1, %%mm5 \n\t"
-        "paddw %%mm3, %%mm6 \n\t"
-        "psubw %%mm0, %%mm4 \n\t"
-        "psubw %%mm2, %%mm5 \n\t"
-        "psubw %%mm2, %%mm6 \n\t"
-
-        "decl %%ecx \n\t"
-        "jnz 2b \n\t"
-
-        "movq %%mm0, %%mm1 \n\t"
-        "movq %%mm0, %%mm2 \n\t"
-        "movq %%mm0, %%mm3 \n\t"
-        "pcmpgtw %%mm4, %%mm1 \n\t"
-        "pcmpgtw %%mm5, %%mm2 \n\t"
-        "pcmpgtw %%mm6, %%mm3 \n\t"
-        "pxor %%mm1, %%mm4 \n\t"
-        "pxor %%mm2, %%mm5 \n\t"
-        "pxor %%mm3, %%mm6 \n\t"
-        "psubw %%mm1, %%mm4 \n\t"
-        "psubw %%mm2, %%mm5 \n\t"
-        "psubw %%mm3, %%mm6 \n\t"
-        "movq %%mm4, (%%"REG_d") \n\t"
-        "movq %%mm5, 16(%%"REG_d") \n\t"
-        "movq %%mm6, 32(%%"REG_d") \n\t"
-
-        "mov %%"REG_a", %%"REG_c" \n\t"
-        "shl $3, %%"REG_c" \n\t"
-        "sub %%"REG_c", %%"REG_S" \n\t"
-        "mov %%"REG_b", %%"REG_c" \n\t"
-        "shl $3, %%"REG_c" \n\t"
-        "sub %%"REG_c", %%"REG_D" \n\t"
-
-        // Second loop for the last four columns
-        "movl $4, %%ecx \n\t"
-        "pxor %%mm4, %%mm4 \n\t"
-        "pxor %%mm5, %%mm5 \n\t"
-        "pxor %%mm6, %%mm6 \n\t"
-
-        ASMALIGN(4)
-        "3: \n\t"
-
-        "movq (%%"REG_S"), %%mm0 \n\t"
-        "movq (%%"REG_S",%%"REG_a"), %%mm1 \n\t"
-        "add %%"REG_a", %%"REG_S" \n\t"
-        "add %%"REG_a", %%"REG_S" \n\t"
-        "movq (%%"REG_D"), %%mm2 \n\t"
-        "movq (%%"REG_D",%%"REG_b"), %%mm3 \n\t"
-        "add %%"REG_b", %%"REG_D" \n\t"
-        "add %%"REG_b", %%"REG_D" \n\t"
-        "punpckhbw %%mm7, %%mm0 \n\t"
-        "punpckhbw %%mm7, %%mm1 \n\t"
-        "punpckhbw %%mm7, %%mm2 \n\t"
-        "punpckhbw %%mm7, %%mm3 \n\t"
-        "paddw %%mm1, %%mm4 \n\t"
-        "paddw %%mm1, %%mm5 \n\t"
-        "paddw %%mm3, %%mm6 \n\t"
-        "psubw %%mm0, %%mm4 \n\t"
-        "psubw %%mm2, %%mm5 \n\t"
-        "psubw %%mm2, %%mm6 \n\t"
-
-        "decl %%ecx \n\t"
-        "jnz 3b \n\t"
-
-        "movq %%mm0, %%mm1 \n\t"
-        "movq %%mm0, %%mm2 \n\t"
-        "movq %%mm0, %%mm3 \n\t"
-        "pcmpgtw %%mm4, %%mm1 \n\t"
-        "pcmpgtw %%mm5, %%mm2 \n\t"
-        "pcmpgtw %%mm6, %%mm3 \n\t"
-        "pxor %%mm1, %%mm4 \n\t"
-        "pxor %%mm2, %%mm5 \n\t"
-        "pxor %%mm3, %%mm6 \n\t"
-        "psubw %%mm1, %%mm4 \n\t"
-        "psubw %%mm2, %%mm5 \n\t"
-        "psubw %%mm3, %%mm6 \n\t"
-        "movq %%mm4, 8(%%"REG_d") \n\t"
-        "movq %%mm5, 24(%%"REG_d") \n\t"
-        "movq %%mm6, 40(%%"REG_d") \n\t"
-
-        "emms \n\t"
-        :
-        : "S" (old), "D" (new), "a" ((long)os), "b" ((long)ns), "d" (out)
-        : "memory"
-        );
-    m->p = m->t = m->s = 0;
-    for (i=0; i<8; i++) {
-        m->p += out[i];
-        m->t += out[8+i];
-        m->s += out[16+i];
-    }
-    //printf("e=%d o=%d d=%d p=%d t=%d s=%d\n", m->e, m->o, m->d, m->p, m->t, m->s);
-}
-#endif
-
-//#define MAG(a) ((a)*(a))
-//#define MAG(a) (abs(a))
-#define MAG(a) (((a)^((a)>>31))-((a)>>31))
-
-//#define LOWPASS(s) (((s)[-2] + 4*(s)[-1] + 6*(s)[0] + 4*(s)[1] + (s)[2])>>4)
-//#define LOWPASS(s) (((s)[-1] + 2*(s)[0] + (s)[1])>>2)
-#define LOWPASS(s) ((s)[0])
-
-
-static void block_diffs_C(struct metrics *m, unsigned char *old, unsigned char *new, int os, int ns)
-{
-    int x, y, e=0, o=0, s=0, p=0, t=0;
-    unsigned char *oldp, *newp;
-    m->s = m->p = m->t = 0;
-    for (x = 8; x; x--) {
-        oldp = old++;
-        newp = new++;
-        s = p = t = 0;
-        for (y = 4; y; y--) {
-            e += MAG(newp[0]-oldp[0]);
-            o += MAG(newp[ns]-oldp[os]);
-            s += newp[ns]-newp[0];
-            p += oldp[os]-oldp[0];
-            t += oldp[os]-newp[0];
-            oldp += os<<1;
-            newp += ns<<1;
-        }
-        m->s += MAG(s);
-        m->p += MAG(p);
-        m->t += MAG(t);
-    }
-    m->e = e;
-    m->o = o;
-    m->d = e+o;
-}
-
-static void (*block_diffs)(struct metrics *, unsigned char *, unsigned char *, int, int);
-
-#define MAXUP(a,b) ((a) = ((a)>(b)) ? (a) : (b))
-
-static void diff_planes(struct frameinfo *fi,
-    unsigned char *old, unsigned char *new, int w, int h, int os, int ns)
-{
-    int x, y;
-    struct metrics l;
-    struct metrics *peak=&fi->p, *rel=&fi->r, *mean=&fi->m;
-    memset(peak, 0, sizeof(struct metrics));
-    memset(rel, 0, sizeof(struct metrics));
-    memset(mean, 0, sizeof(struct metrics));
-    for (y = 0; y < h-7; y += 8) {
-        for (x = 8; x < w-8-7; x += 8) {
-            block_diffs(&l, old+x+y*os, new+x+y*ns, os, ns);
-            mean->d += l.d;
-            mean->e += l.e;
-            mean->o += l.o;
-            mean->s += l.s;
-            mean->p += l.p;
-            mean->t += l.t;
-            MAXUP(peak->d, l.d);
-            MAXUP(peak->e, l.e);
-            MAXUP(peak->o, l.o);
-            MAXUP(peak->s, l.s);
-            MAXUP(peak->p, l.p);
-            MAXUP(peak->t, l.t);
-            MAXUP(rel->e, l.e-l.o);
-            MAXUP(rel->o, l.o-l.e);
-            MAXUP(rel->s, l.s-l.t);
-            MAXUP(rel->p, l.p-l.t);
-            MAXUP(rel->t, l.t-l.p);
-            MAXUP(rel->d, l.t-l.s); /* hack */
-        }
-    }
-    x = (w/8-2)*(h/8);
-    mean->d /= x;
-    mean->e /= x;
-    mean->o /= x;
-    mean->s /= x;
-    mean->p /= x;
-    mean->t /= x;
-}
-
-static void diff_fields(struct frameinfo *fi, mp_image_t *old, mp_image_t *new)
-{
-    diff_planes(fi, old->planes[0], new->planes[0],
-        new->w, new->h, old->stride[0], new->stride[0]);
-}
-
-static void stats(struct frameinfo *f)
-{
-    ff_mp_msg(MSGT_VFILTER, MSGL_V, "       pd=%d re=%d ro=%d rp=%d rt=%d rs=%d rd=%d pp=%d pt=%d ps=%d\r",
-        f->p.d, f->r.e, f->r.o, f->r.p, f->r.t, f->r.s, f->r.d, f->p.p, f->p.t, f->p.s);
-}
-
-static int foo(struct vf_priv_s *p, mp_image_t *new, mp_image_t *cur)
-{
-    struct frameinfo *f = p->fi;
-
-    f[0] = f[1];
-    diff_fields(&f[1], cur, new);
-    stats(&f[1]);
-
-    // Immediately drop this frame if it's already been used.
-    if (p->dropnext) {
-        p->dropnext = 0;
-        return F_DROP;
-    }
-
-    // Sometimes a pulldown frame comes all by itself, so both
-    // its top and bottom field are duplicates from the adjacent
-    // two frames. We can just drop such a frame, but we
-    // immediately show the next frame instead to keep the frame
-    // drops evenly spaced during normal 3:2 pulldown sequences.
-    if ((3*f[1].r.o < f[1].r.e) && (f[1].r.s < f[1].r.d)) {
-        p->dropnext = 1;
-        return F_NEXT;
-    }
-
-    // If none of these conditions hold, we will consider the frame
-    // progressive and just show it as-is.
-    if (!(  (3*f[0].r.e < f[0].r.o) ||
-        ((2*f[0].r.d < f[0].r.s) && (f[0].r.s > 1200)) ||
-        ((2*f[1].r.t < f[1].r.p) && (f[1].r.p > 1200))  ))
-        return F_SHOW;
-
-    // Otherwise, we have to decide whether to merge or drop.
-    // If the noise metric only increases minimally, we're off
-    // to a good start...
-    if (((2*f[1].r.t < 3*f[1].r.p) && (f[1].r.t < 3600)) ||
-        (f[1].r.t < 900) || (f[1].r.d < 900)) {
-        // ...and if noise decreases or the duplicate even field
-        // is detected, we go ahead with the merge.
-        if ((3*f[0].r.e < f[0].r.o) || (2*f[1].r.t < f[1].r.p)) {
-            p->dropnext = 1;
-            return F_MERGE;
-        }
-    }
-    return F_DROP;
-}
-
-
-
-static void copy_image(mp_image_t *dmpi, mp_image_t *mpi, int field)
-{
-    switch (field) {
-    case 0:
-        my_memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h/2,
-            dmpi->stride[0]*2, mpi->stride[0]*2);
-        if (mpi->flags & MP_IMGFLAG_PLANAR) {
-            my_memcpy_pic(dmpi->planes[1], mpi->planes[1],
-                mpi->chroma_width, mpi->chroma_height/2,
-                dmpi->stride[1]*2, mpi->stride[1]*2);
-            my_memcpy_pic(dmpi->planes[2], mpi->planes[2],
-                mpi->chroma_width, mpi->chroma_height/2,
-                dmpi->stride[2]*2, mpi->stride[2]*2);
-        }
-        break;
-    case 1:
-        my_memcpy_pic(dmpi->planes[0]+dmpi->stride[0],
-            mpi->planes[0]+mpi->stride[0], mpi->w, mpi->h/2,
-            dmpi->stride[0]*2, mpi->stride[0]*2);
-        if (mpi->flags & MP_IMGFLAG_PLANAR) {
-            my_memcpy_pic(dmpi->planes[1]+dmpi->stride[1],
-                mpi->planes[1]+mpi->stride[1],
-                mpi->chroma_width, mpi->chroma_height/2,
-                dmpi->stride[1]*2, mpi->stride[1]*2);
-            my_memcpy_pic(dmpi->planes[2]+dmpi->stride[2],
-                mpi->planes[2]+mpi->stride[2],
-                mpi->chroma_width, mpi->chroma_height/2,
-                dmpi->stride[2]*2, mpi->stride[2]*2);
-        }
-        break;
-    case 2:
-        memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h,
-            dmpi->stride[0], mpi->stride[0]);
-        if (mpi->flags & MP_IMGFLAG_PLANAR) {
-            memcpy_pic(dmpi->planes[1], mpi->planes[1],
-                mpi->chroma_width, mpi->chroma_height,
-                dmpi->stride[1], mpi->stride[1]);
-            memcpy_pic(dmpi->planes[2], mpi->planes[2],
-                mpi->chroma_width, mpi->chroma_height,
-                dmpi->stride[2], mpi->stride[2]);
-        }
-        break;
-    }
-}
-
-static int do_put_image(struct vf_instance *vf, mp_image_t *dmpi)
-{
-    struct vf_priv_s *p = vf->priv;
-    int dropflag=0;
-
-    if (!p->dropnext) switch (p->drop) {
-    case 0:
-        dropflag = 0;
-        break;
-    case 1:
-        dropflag = (++p->lastdrop >= 5);
-        break;
-    case 2:
-        dropflag = (++p->lastdrop >= 5) && (4*p->inframes <= 5*p->outframes);
-        break;
-    }
-
-    if (dropflag) {
-        //ff_mp_msg(MSGT_VFILTER, MSGL_V, "drop! [%d/%d=%g]\n",
-        //    p->outframes, p->inframes, (float)p->outframes/p->inframes);
-        ff_mp_msg(MSGT_VFILTER, MSGL_V, "!");
-        p->lastdrop = 0;
-        return 0;
-    }
-
-    p->outframes++;
-    return ff_vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
-}
-
-static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
-{
-    int ret=0;
-    struct vf_priv_s *p = vf->priv;
-
-    p->inframes++;
-
-    if (p->first) { /* hack */
-        p->first = 0;
-        return 1;
-    }
-
-    if (!p->dmpi) p->dmpi = ff_vf_get_image(vf->next, mpi->imgfmt,
-        MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
-        MP_IMGFLAG_PRESERVE | MP_IMGFLAG_READABLE,
-        mpi->width, mpi->height);
-    /* FIXME -- not correct, off by one frame! */
-    p->dmpi->qscale = mpi->qscale;
-    p->dmpi->qstride = mpi->qstride;
-    p->dmpi->qscale_type = mpi->qscale_type;
-
-    switch (foo(p, mpi, p->dmpi)) {
-    case F_DROP:
-        copy_image(p->dmpi, mpi, 2);
-        ret = 0;
-        p->lastdrop = 0;
-        ff_mp_msg(MSGT_VFILTER, MSGL_V, "DROP\n");
-        break;
-    case F_MERGE:
-        copy_image(p->dmpi, mpi, 0);
-        ret = do_put_image(vf, p->dmpi);
-        copy_image(p->dmpi, mpi, 1);
-        ff_mp_msg(MSGT_VFILTER, MSGL_V, "MERGE\n");
-        p->dmpi = NULL;
-        break;
-    case F_NEXT:
-        copy_image(p->dmpi, mpi, 2);
-        ret = do_put_image(vf, p->dmpi);
-        ff_mp_msg(MSGT_VFILTER, MSGL_V, "NEXT\n");
-        p->dmpi = NULL;
-        break;
-    case F_SHOW:
-        ret = do_put_image(vf, p->dmpi);
-        copy_image(p->dmpi, mpi, 2);
-        ff_mp_msg(MSGT_VFILTER, MSGL_V, "OK\n");
-        p->dmpi = NULL;
-        break;
-    }
-    return ret;
-}
-
-static int query_format(struct vf_instance *vf, unsigned int fmt)
-{
-    switch (fmt) {
-    case IMGFMT_YV12:
-    case IMGFMT_IYUV:
-    case IMGFMT_I420:
-        return ff_vf_next_query_format(vf, fmt);
-    }
-    return 0;
-}
-
-static void uninit(struct vf_instance *vf)
-{
-    free(vf->priv);
-}
-
-static int vf_open(vf_instance_t *vf, char *args)
-{
-    struct vf_priv_s *p;
-    vf->put_image = put_image;
-    vf->query_format = query_format;
-    vf->uninit = uninit;
-    vf->default_reqs = VFCAP_ACCEPT_STRIDE;
-    vf->priv = p = calloc(1, sizeof(struct vf_priv_s));
-    p->drop = 0;
-    p->first = 1;
-    if (args) sscanf(args, "%d", &p->drop);
-    block_diffs = block_diffs_C;
-#if HAVE_MMX && HAVE_EBX_AVAILABLE
-    if(ff_gCpuCaps.hasMMX) block_diffs = block_diffs_MMX;
-#endif
-    return 1;
-}
-
-const vf_info_t ff_vf_info_ivtc = {
-    "inverse telecine, take 2",
-    "ivtc",
-    "Rich Felker",
-    "",
-    vf_open,
-    NULL
-};
diff --git a/libavfilter/libmpcodecs/vf_noise.c b/libavfilter/libmpcodecs/vf_noise.c
deleted file mode 100644
index 3b946e9..0000000
--- a/libavfilter/libmpcodecs/vf_noise.c
+++ /dev/null
@@ -1,475 +0,0 @@
-/*
- * Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at>
- *
- * This file is part of MPlayer.
- *
- * MPlayer is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * MPlayer 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with MPlayer; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <inttypes.h>
-#include <math.h>
-
-#include "config.h"
-#include "mp_msg.h"
-#include "cpudetect.h"
-
-#if HAVE_MALLOC_H
-#include <malloc.h>
-#endif
-
-#include "img_format.h"
-#include "mp_image.h"
-#include "vf.h"
-#include "libvo/fastmemcpy.h"
-#include "libavutil/mem.h"
-#include "libavutil/x86/asm.h"
-
-#define MAX_NOISE 4096
-#define MAX_SHIFT 1024
-#define MAX_RES (MAX_NOISE-MAX_SHIFT)
-
-//===========================================================================//
-
-static inline void lineNoise_C(uint8_t *dst, uint8_t *src, int8_t *noise, int len, int shift);
-static inline void lineNoiseAvg_C(uint8_t *dst, uint8_t *src, int len, int8_t **shift);
-
-static void (*lineNoise)(uint8_t *dst, uint8_t *src, int8_t *noise, int len, int shift)= lineNoise_C;
-static void (*lineNoiseAvg)(uint8_t *dst, uint8_t *src, int len, int8_t **shift)= lineNoiseAvg_C;
-
-typedef struct FilterParam{
-        int strength;
-        int uniform;
-        int temporal;
-        int quality;
-        int averaged;
-        int pattern;
-        int shiftptr;
-        int8_t *noise;
-        int8_t *prev_shift[MAX_RES][3];
-}FilterParam;
-
-struct vf_priv_s {
-        FilterParam lumaParam;
-        FilterParam chromaParam;
-        unsigned int outfmt;
-};
-
-static int nonTempRandShift_init;
-static int nonTempRandShift[MAX_RES];
-
-static int patt[4] = {
-    -1,0,1,0
-};
-
-#define RAND_N(range) ((int) ((double)range*rand()/(RAND_MAX+1.0)))
-static int8_t *initNoise(FilterParam *fp){
-        int strength= fp->strength;
-        int uniform= fp->uniform;
-        int averaged= fp->averaged;
-        int pattern= fp->pattern;
-        int8_t *noise= av_malloc(MAX_NOISE*sizeof(int8_t));
-        int i, j;
-
-        srand(123457);
-
-        for(i=0,j=0; i<MAX_NOISE; i++,j++)
-        {
-                if(uniform) {
-                        if (averaged) {
-                                    if (pattern) {
-                                        noise[i]= (RAND_N(strength) - strength/2)/6
-                                                +patt[j%4]*strength*0.25/3;
-                                } else {
-                                        noise[i]= (RAND_N(strength) - strength/2)/3;
-                                    }
-                        } else {
-                                    if (pattern) {
-                                    noise[i]= (RAND_N(strength) - strength/2)/2
-                                            + patt[j%4]*strength*0.25;
-                                } else {
-                                        noise[i]= RAND_N(strength) - strength/2;
-                                    }
-                        }
-                    } else {
-                        double x1, x2, w, y1;
-                        do {
-                                x1 = 2.0 * rand()/(float)RAND_MAX - 1.0;
-                                x2 = 2.0 * rand()/(float)RAND_MAX - 1.0;
-                                w = x1 * x1 + x2 * x2;
-                        } while ( w >= 1.0 );
-
-                        w = sqrt( (-2.0 * log( w ) ) / w );
-                        y1= x1 * w;
-                        y1*= strength / sqrt(3.0);
-                        if (pattern) {
-                            y1 /= 2;
-                            y1 += patt[j%4]*strength*0.35;
-                        }
-                        if     (y1<-128) y1=-128;
-                        else if(y1> 127) y1= 127;
-                        if (averaged) y1 /= 3.0;
-                        noise[i]= (int)y1;
-                }
-                if (RAND_N(6) == 0) j--;
-        }
-
-
-        for (i = 0; i < MAX_RES; i++)
-            for (j = 0; j < 3; j++)
-                fp->prev_shift[i][j] = noise + (rand()&(MAX_SHIFT-1));
-
-        if(!nonTempRandShift_init){
-                for(i=0; i<MAX_RES; i++){
-                        nonTempRandShift[i]= rand()&(MAX_SHIFT-1);
-                }
-                nonTempRandShift_init = 1;
-        }
-
-        fp->noise= noise;
-        fp->shiftptr= 0;
-        return noise;
-}
-
-/***************************************************************************/
-
-#if HAVE_MMX
-static inline void lineNoise_MMX(uint8_t *dst, uint8_t *src, int8_t *noise, int len, int shift){
-        x86_reg mmx_len= len&(~7);
-        noise+=shift;
-
-        __asm__ volatile(
-                "mov %3, %%"REG_a"                \n\t"
-                "pcmpeqb %%mm7, %%mm7                \n\t"
-                "psllw $15, %%mm7                \n\t"
-                "packsswb %%mm7, %%mm7                \n\t"
-                ASMALIGN(4)
-                "1:                                \n\t"
-                "movq (%0, %%"REG_a"), %%mm0        \n\t"
-                "movq (%1, %%"REG_a"), %%mm1        \n\t"
-                "pxor %%mm7, %%mm0                \n\t"
-                "paddsb %%mm1, %%mm0                \n\t"
-                "pxor %%mm7, %%mm0                \n\t"
-                "movq %%mm0, (%2, %%"REG_a")        \n\t"
-                "add $8, %%"REG_a"                \n\t"
-                " js 1b                                \n\t"
-                :: "r" (src+mmx_len), "r" (noise+mmx_len), "r" (dst+mmx_len), "g" (-mmx_len)
-                : "%"REG_a
-        );
-        if(mmx_len!=len)
-                lineNoise_C(dst+mmx_len, src+mmx_len, noise+mmx_len, len-mmx_len, 0);
-}
-#endif
-
-//duplicate of previous except movntq
-#if HAVE_MMX2
-static inline void lineNoise_MMX2(uint8_t *dst, uint8_t *src, int8_t *noise, int len, int shift){
-        x86_reg mmx_len= len&(~7);
-        noise+=shift;
-
-        __asm__ volatile(
-                "mov %3, %%"REG_a"                \n\t"
-                "pcmpeqb %%mm7, %%mm7                \n\t"
-                "psllw $15, %%mm7                \n\t"
-                "packsswb %%mm7, %%mm7                \n\t"
-                ASMALIGN(4)
-                "1:                                \n\t"
-                "movq (%0, %%"REG_a"), %%mm0        \n\t"
-                "movq (%1, %%"REG_a"), %%mm1        \n\t"
-                "pxor %%mm7, %%mm0                \n\t"
-                "paddsb %%mm1, %%mm0                \n\t"
-                "pxor %%mm7, %%mm0                \n\t"
-                "movntq %%mm0, (%2, %%"REG_a")        \n\t"
-                "add $8, %%"REG_a"                \n\t"
-                " js 1b                                \n\t"
-                :: "r" (src+mmx_len), "r" (noise+mmx_len), "r" (dst+mmx_len), "g" (-mmx_len)
-                : "%"REG_a
-        );
-        if(mmx_len!=len)
-                lineNoise_C(dst+mmx_len, src+mmx_len, noise+mmx_len, len-mmx_len, 0);
-}
-#endif
-
-static inline void lineNoise_C(uint8_t *dst, uint8_t *src, int8_t *noise, int len, int shift){
-        int i;
-        noise+= shift;
-        for(i=0; i<len; i++)
-        {
-                int v= src[i]+ noise[i];
-                if(v>255)         dst[i]=255; //FIXME optimize
-                else if(v<0)         dst[i]=0;
-                else                dst[i]=v;
-        }
-}
-
-/***************************************************************************/
-
-#if HAVE_MMX
-static inline void lineNoiseAvg_MMX(uint8_t *dst, uint8_t *src, int len, int8_t **shift){
-        x86_reg mmx_len= len&(~7);
-
-        __asm__ volatile(
-                "mov %5, %%"REG_a"                \n\t"
-                ASMALIGN(4)
-                "1:                                \n\t"
-                "movq (%1, %%"REG_a"), %%mm1        \n\t"
-                "movq (%0, %%"REG_a"), %%mm0        \n\t"
-                "paddb (%2, %%"REG_a"), %%mm1        \n\t"
-                "paddb (%3, %%"REG_a"), %%mm1        \n\t"
-                "movq %%mm0, %%mm2                \n\t"
-                "movq %%mm1, %%mm3                \n\t"
-                "punpcklbw %%mm0, %%mm0                \n\t"
-                "punpckhbw %%mm2, %%mm2                \n\t"
-                "punpcklbw %%mm1, %%mm1                \n\t"
-                "punpckhbw %%mm3, %%mm3                \n\t"
-                "pmulhw %%mm0, %%mm1                \n\t"
-                "pmulhw %%mm2, %%mm3                \n\t"
-                "paddw %%mm1, %%mm1                \n\t"
-                "paddw %%mm3, %%mm3                \n\t"
-                "paddw %%mm0, %%mm1                \n\t"
-                "paddw %%mm2, %%mm3                \n\t"
-                "psrlw $8, %%mm1                \n\t"
-                "psrlw $8, %%mm3                \n\t"
-                "packuswb %%mm3, %%mm1                \n\t"
-                "movq %%mm1, (%4, %%"REG_a")        \n\t"
-                "add $8, %%"REG_a"                \n\t"
-                " js 1b                                \n\t"
-                :: "r" (src+mmx_len), "r" (shift[0]+mmx_len), "r" (shift[1]+mmx_len), "r" (shift[2]+mmx_len),
-                   "r" (dst+mmx_len), "g" (-mmx_len)
-                : "%"REG_a
-        );
-
-        if(mmx_len!=len){
-                int8_t *shift2[3]={shift[0]+mmx_len, shift[1]+mmx_len, shift[2]+mmx_len};
-                lineNoiseAvg_C(dst+mmx_len, src+mmx_len, len-mmx_len, shift2);
-        }
-}
-#endif
-
-static inline void lineNoiseAvg_C(uint8_t *dst, uint8_t *src, int len, int8_t **shift){
-        int i;
-        int8_t *src2= (int8_t*)src;
-
-        for(i=0; i<len; i++)
-        {
-            const int n= shift[0][i] + shift[1][i] + shift[2][i];
-            dst[i]= src2[i]+((n*src2[i])>>7);
-        }
-}
-
-/***************************************************************************/
-
-static void noise(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int width, int height, FilterParam *fp){
-        int8_t *noise= fp->noise;
-        int y;
-        int shift=0;
-
-        if(!noise)
-        {
-                if(src==dst) return;
-
-                if(dstStride==srcStride) fast_memcpy(dst, src, srcStride*height);
-                else
-                {
-                        for(y=0; y<height; y++)
-                        {
-                                fast_memcpy(dst, src, width);
-                                dst+= dstStride;
-                                src+= srcStride;
-                        }
-                }
-                return;
-        }
-
-        for(y=0; y<height; y++)
-        {
-                if(fp->temporal)        shift=  rand()&(MAX_SHIFT  -1);
-                else                        shift= nonTempRandShift[y];
-
-                if(fp->quality==0) shift&= ~7;
-                if (fp->averaged) {
-                    lineNoiseAvg(dst, src, width, fp->prev_shift[y]);
-                    fp->prev_shift[y][fp->shiftptr] = noise + shift;
-                } else {
-                    lineNoise(dst, src, noise, width, shift);
-                }
-                dst+= dstStride;
-                src+= srcStride;
-        }
-        fp->shiftptr++;
-        if (fp->shiftptr == 3) fp->shiftptr = 0;
-}
-
-static int config(struct vf_instance *vf,
-        int width, int height, int d_width, int d_height,
-        unsigned int flags, unsigned int outfmt){
-
-        return ff_vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
-}
-
-static void get_image(struct vf_instance *vf, mp_image_t *mpi){
-    if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
-    if(mpi->imgfmt!=vf->priv->outfmt) return; // colorspace differ
-    // ok, we can do pp in-place (or pp disabled):
-    vf->dmpi=ff_vf_get_image(vf->next,mpi->imgfmt,
-        mpi->type, mpi->flags, mpi->w, mpi->h);
-    mpi->planes[0]=vf->dmpi->planes[0];
-    mpi->stride[0]=vf->dmpi->stride[0];
-    mpi->width=vf->dmpi->width;
-    if(mpi->flags&MP_IMGFLAG_PLANAR){
-        mpi->planes[1]=vf->dmpi->planes[1];
-        mpi->planes[2]=vf->dmpi->planes[2];
-        mpi->stride[1]=vf->dmpi->stride[1];
-        mpi->stride[2]=vf->dmpi->stride[2];
-    }
-    mpi->flags|=MP_IMGFLAG_DIRECT;
-}
-
-static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
-        mp_image_t *dmpi;
-
-        if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
-                // no DR, so get a new image! hope we'll get DR buffer:
-                vf->dmpi=ff_vf_get_image(vf->next,vf->priv->outfmt,
-                MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
-                mpi->w,mpi->h);
-//printf("nodr\n");
-        }
-//else printf("dr\n");
-        dmpi= vf->dmpi;
-
-        noise(dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h, &vf->priv->lumaParam);
-        noise(dmpi->planes[1], mpi->planes[1], dmpi->stride[1], mpi->stride[1], mpi->w/2, mpi->h/2, &vf->priv->chromaParam);
-        noise(dmpi->planes[2], mpi->planes[2], dmpi->stride[2], mpi->stride[2], mpi->w/2, mpi->h/2, &vf->priv->chromaParam);
-
-        ff_vf_clone_mpi_attributes(dmpi, mpi);
-
-#if HAVE_MMX
-        if(ff_gCpuCaps.hasMMX) __asm__ volatile ("emms\n\t");
-#endif
-#if HAVE_MMX2
-        if(ff_gCpuCaps.hasMMX2) __asm__ volatile ("sfence\n\t");
-#endif
-
-        return ff_vf_next_put_image(vf,dmpi, pts);
-}
-
-static void uninit(struct vf_instance *vf){
-        if(!vf->priv) return;
-
-        av_free(vf->priv->chromaParam.noise);
-        vf->priv->chromaParam.noise= NULL;
-
-        av_free(vf->priv->lumaParam.noise);
-        vf->priv->lumaParam.noise= NULL;
-
-        free(vf->priv);
-        vf->priv=NULL;
-}
-
-//===========================================================================//
-
-static int query_format(struct vf_instance *vf, unsigned int fmt){
-        switch(fmt)
-        {
-        case IMGFMT_YV12:
-        case IMGFMT_I420:
-        case IMGFMT_IYUV:
-                return ff_vf_next_query_format(vf,vf->priv->outfmt);
-        }
-        return 0;
-}
-
-static void parse(FilterParam *fp, char* args){
-        char *pos;
-        char *max= strchr(args, ':');
-
-        if(!max) max= args + strlen(args);
-
-        fp->strength= atoi(args);
-        pos= strchr(args, 'u');
-        if(pos && pos<max) fp->uniform=1;
-        pos= strchr(args, 't');
-        if(pos && pos<max) fp->temporal=1;
-        pos= strchr(args, 'h');
-        if(pos && pos<max) fp->quality=1;
-        pos= strchr(args, 'p');
-        if(pos && pos<max) fp->pattern=1;
-        pos= strchr(args, 'a');
-        if(pos && pos<max) {
-            fp->temporal=1;
-            fp->averaged=1;
-        }
-
-        if(fp->strength) initNoise(fp);
-}
-
-static const unsigned int fmt_list[]={
-    IMGFMT_YV12,
-    IMGFMT_I420,
-    IMGFMT_IYUV,
-    0
-};
-
-static int vf_open(vf_instance_t *vf, char *args){
-    vf->config=config;
-    vf->put_image=put_image;
-    vf->get_image=get_image;
-    vf->query_format=query_format;
-    vf->uninit=uninit;
-    vf->priv=malloc(sizeof(struct vf_priv_s));
-    memset(vf->priv, 0, sizeof(struct vf_priv_s));
-    if(args)
-    {
-        char *arg2= strchr(args,':');
-        if(arg2) parse(&vf->priv->chromaParam, arg2+1);
-        parse(&vf->priv->lumaParam, args);
-    }
-
-    // check csp:
-    vf->priv->outfmt=ff_vf_match_csp(&vf->next,fmt_list,IMGFMT_YV12);
-    if(!vf->priv->outfmt)
-    {
-        uninit(vf);
-        return 0; // no csp match :(
-    }
-
-
-#if HAVE_MMX
-    if(ff_gCpuCaps.hasMMX){
-        lineNoise= lineNoise_MMX;
-        lineNoiseAvg= lineNoiseAvg_MMX;
-    }
-#endif
-#if HAVE_MMX2
-    if(ff_gCpuCaps.hasMMX2) lineNoise= lineNoise_MMX2;
-//    if(ff_gCpuCaps.hasMMX) lineNoiseAvg= lineNoiseAvg_MMX2;
-#endif
-
-    return 1;
-}
-
-const vf_info_t ff_vf_info_noise = {
-    "noise generator",
-    "noise",
-    "Michael Niedermayer",
-    "",
-    vf_open,
-    NULL
-};
-
-//===========================================================================//
diff --git a/libavfilter/libmpcodecs/vf_stereo3d.c b/libavfilter/libmpcodecs/vf_stereo3d.c
deleted file mode 100644
index fe75bd0..0000000
--- a/libavfilter/libmpcodecs/vf_stereo3d.c
+++ /dev/null
@@ -1,540 +0,0 @@
-/*
- * Copyright (C) 2010 Gordon Schmidt <gordon.schmidt <at> s2000.tu-chemnitz.de>
- *
- * This file is part of MPlayer.
- *
- * MPlayer is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * MPlayer 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with MPlayer; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-//==includes==//
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "config.h"
-#include "mp_msg.h"
-#include "help_mp.h"
-
-#include "img_format.h"
-#include "mp_image.h"
-#include "vf.h"
-
-#include "libavutil/common.h"
-#include "libvo/fastmemcpy.h"
-
-//==types==//
-typedef enum stereo_code {
-    ANAGLYPH_RC_GRAY,   //anaglyph red/cyan gray
-    ANAGLYPH_RC_HALF,   //anaglyph red/cyan half colored
-    ANAGLYPH_RC_COLOR,  //anaglyph red/cyan colored
-    ANAGLYPH_RC_DUBOIS, //anaglyph red/cyan dubois
-    ANAGLYPH_GM_GRAY,   //anaglyph green/magenta gray
-    ANAGLYPH_GM_HALF,   //anaglyph green/magenta half colored
-    ANAGLYPH_GM_COLOR,  //anaglyph green/magenta colored
-    ANAGLYPH_GM_DUBOIS, //anaglyph green/magenta dubois
-    ANAGLYPH_YB_GRAY,   //anaglyph yellow/blue gray
-    ANAGLYPH_YB_HALF,   //anaglyph yellow/blue half colored
-    ANAGLYPH_YB_COLOR,  //anaglyph yellow/blue colored
-    ANAGLYPH_YB_DUBOIS, //anaglyph yellow/blue dubois
-    MONO_L,             //mono output for debugging (left eye only)
-    MONO_R,             //mono output for debugging (right eye only)
-    SIDE_BY_SIDE_LR,    //side by side parallel (left eye left, right eye right)
-    SIDE_BY_SIDE_RL,    //side by side crosseye (right eye left, left eye right)
-    SIDE_BY_SIDE_2_LR,  //side by side parallel with half width resolution
-    SIDE_BY_SIDE_2_RL,  //side by side crosseye with half width resolution
-    ABOVE_BELOW_LR,     //above-below (left eye above, right eye below)
-    ABOVE_BELOW_RL,     //above-below (right eye above, left eye below)
-    ABOVE_BELOW_2_LR,   //above-below with half height resolution
-    ABOVE_BELOW_2_RL,   //above-below with half height resolution
-    INTERLEAVE_ROWS_LR, //row-interleave (left eye has top row)
-    INTERLEAVE_ROWS_RL, //row-interleave (right eye has top row)
-    STEREO_CODE_COUNT   //no value set - TODO: needs autodetection
-} stereo_code;
-
-typedef struct component {
-    stereo_code  fmt;
-    unsigned int width;
-    unsigned int height;
-    unsigned int off_left;
-    unsigned int off_right;
-    unsigned int row_left;
-    unsigned int row_right;
-} component;
-
-//==global variables==//
-static const int ana_coeff[][3][6] = {
-  [ANAGLYPH_RC_GRAY]   =
-    {{19595, 38470,  7471,     0,     0,     0},
-     {    0,     0,     0, 19595, 38470,  7471},
-     {    0,     0,     0, 19595, 38470,  7471}},
-  [ANAGLYPH_RC_HALF]   =
-    {{19595, 38470,  7471,     0,     0,     0},
-     {    0,     0,     0,     0, 65536,     0},
-     {    0,     0,     0,     0,     0, 65536}},
-  [ANAGLYPH_RC_COLOR]  =
-    {{65536,     0,     0,     0,     0,     0},
-     {    0,     0,     0,     0, 65536,     0},
-     {    0,     0,     0,     0,     0, 65536}},
-  [ANAGLYPH_RC_DUBOIS] =
-    {{29891, 32800, 11559, -2849, -5763,  -102},
-     {-2627, -2479, -1033, 24804, 48080, -1209},
-     { -997, -1350,  -358, -4729, -7403, 80373}},
-  [ANAGLYPH_GM_GRAY]   =
-    {{    0,     0,     0, 19595, 38470,  7471},
-     {19595, 38470,  7471,     0,     0,     0},
-     {    0,     0,     0, 19595, 38470,  7471}},
-  [ANAGLYPH_GM_HALF]   =
-    {{    0,     0,     0, 65536,     0,     0},
-     {19595, 38470,  7471,     0,     0,     0},
-     {    0,     0,     0,     0,     0, 65536}},
-  [ANAGLYPH_GM_COLOR]  =
-    {{    0,     0,     0, 65536,     0,     0},
-     {    0, 65536,     0,     0,     0,     0},
-     {    0,     0,     0,     0,     0, 65536}},
-  [ANAGLYPH_GM_DUBOIS]  =
-    {{-4063,-10354, -2556, 34669, 46203,  1573},
-     {18612, 43778,  9372, -1049,  -983, -4260},
-     { -983, -1769,  1376,   590,  4915, 61407}},
-  [ANAGLYPH_YB_GRAY]   =
-    {{    0,     0,     0, 19595, 38470,  7471},
-     {    0,     0,     0, 19595, 38470,  7471},
-     {19595, 38470,  7471,     0,     0,     0}},
-  [ANAGLYPH_YB_HALF]   =
-    {{    0,     0,     0, 65536,     0,     0},
-     {    0,     0,     0,     0, 65536,     0},
-     {19595, 38470,  7471,     0,     0,     0}},
-  [ANAGLYPH_YB_COLOR]  =
-    {{    0,     0,     0, 65536,     0,     0},
-     {    0,     0,     0,     0, 65536,     0},
-     {    0,     0, 65536,     0,     0,     0}},
-  [ANAGLYPH_YB_DUBOIS] =
-    {{65535,-12650,18451,   -987, -7590, -1049},
-     {-1604, 56032, 4196,    370,  3826, -1049},
-     {-2345,-10676, 1358,   5801, 11416, 56217}},
-};
-
-struct vf_priv_s {
-    component in;
-    component out;
-    int ana_matrix[3][6];
-    unsigned int width;
-    unsigned int height;
-    unsigned int row_step;
-} const ff_vf_priv_default = {
-  {SIDE_BY_SIDE_LR},
-  {ANAGLYPH_RC_DUBOIS}
-};
-
-//==functions==//
-static inline uint8_t ana_convert(int coeff[6], uint8_t left[3], uint8_t right[3])
-{
-    int sum;
-
-    sum  = coeff[0] * left[0] + coeff[3] * right[0]; //red in
-    sum += coeff[1] * left[1] + coeff[4] * right[1]; //green in
-    sum += coeff[2] * left[2] + coeff[5] * right[2]; //blue in
-    return av_clip_uint8(sum >> 16);
-}
-
-static int config(struct vf_instance *vf, int width, int height, int d_width,
-                  int d_height, unsigned int flags, unsigned int outfmt)
-{
-    if ((width & 1) || (height & 1)) {
-        ff_mp_msg(MSGT_VFILTER, MSGL_WARN, "[stereo3d] invalid height or width\n");
-        return 0;
-    }
-    //default input values
-    vf->priv->width             = width;
-    vf->priv->height            = height;
-    vf->priv->row_step          = 1;
-    vf->priv->in.width          = width;
-    vf->priv->in.height         = height;
-    vf->priv->in.off_left       = 0;
-    vf->priv->in.off_right      = 0;
-    vf->priv->in.row_left       = 0;
-    vf->priv->in.row_right      = 0;
-
-    //check input format
-    switch (vf->priv->in.fmt) {
-    case SIDE_BY_SIDE_2_LR:
-        d_width                *= 2;
-    case SIDE_BY_SIDE_LR:
-        vf->priv->width         = width / 2;
-        vf->priv->in.off_right  = vf->priv->width * 3;
-        break;
-    case SIDE_BY_SIDE_2_RL:
-        d_width                *= 2;
-    case SIDE_BY_SIDE_RL:
-        vf->priv->width         = width / 2;
-        vf->priv->in.off_left   = vf->priv->width * 3;
-        break;
-    case ABOVE_BELOW_2_LR:
-        d_height               *= 2;
-    case ABOVE_BELOW_LR:
-        vf->priv->height        = height / 2;
-        vf->priv->in.row_right  = vf->priv->height;
-        break;
-    case ABOVE_BELOW_2_RL:
-        d_height               *= 2;
-    case ABOVE_BELOW_RL:
-        vf->priv->height        = height / 2;
-        vf->priv->in.row_left   = vf->priv->height;
-        break;
-    default:
-        ff_mp_msg(MSGT_VFILTER, MSGL_WARN,
-               "[stereo3d] stereo format of input is not supported\n");
-        return 0;
-        break;
-    }
-    //default output values
-    vf->priv->out.width         = vf->priv->width;
-    vf->priv->out.height        = vf->priv->height;
-    vf->priv->out.off_left      = 0;
-    vf->priv->out.off_right     = 0;
-    vf->priv->out.row_left      = 0;
-    vf->priv->out.row_right     = 0;
-
-    //check output format
-    switch (vf->priv->out.fmt) {
-    case ANAGLYPH_RC_GRAY:
-    case ANAGLYPH_RC_HALF:
-    case ANAGLYPH_RC_COLOR:
-    case ANAGLYPH_RC_DUBOIS:
-    case ANAGLYPH_GM_GRAY:
-    case ANAGLYPH_GM_HALF:
-    case ANAGLYPH_GM_COLOR:
-    case ANAGLYPH_GM_DUBOIS:
-    case ANAGLYPH_YB_GRAY:
-    case ANAGLYPH_YB_HALF:
-    case ANAGLYPH_YB_COLOR:
-    case ANAGLYPH_YB_DUBOIS:
-        memcpy(vf->priv->ana_matrix, ana_coeff[vf->priv->out.fmt],
-               sizeof(vf->priv->ana_matrix));
-        break;
-    case SIDE_BY_SIDE_2_LR:
-        d_width                /= 2;
-    case SIDE_BY_SIDE_LR:
-        vf->priv->out.width     = vf->priv->width * 2;
-        vf->priv->out.off_right = vf->priv->width * 3;
-        break;
-    case SIDE_BY_SIDE_2_RL:
-        d_width                /= 2;
-    case SIDE_BY_SIDE_RL:
-        vf->priv->out.width     = vf->priv->width * 2;
-        vf->priv->out.off_left  = vf->priv->width * 3;
-        break;
-    case ABOVE_BELOW_2_LR:
-        d_height               /= 2;
-    case ABOVE_BELOW_LR:
-        vf->priv->out.height    = vf->priv->height * 2;
-        vf->priv->out.row_right = vf->priv->height;
-        break;
-    case ABOVE_BELOW_2_RL:
-        d_height               /= 2;
-    case ABOVE_BELOW_RL:
-        vf->priv->out.height    = vf->priv->height * 2;
-        vf->priv->out.row_left  = vf->priv->height;
-        break;
-    case INTERLEAVE_ROWS_LR:
-        vf->priv->row_step      = 2;
-        vf->priv->height        = vf->priv->height / 2;
-        vf->priv->out.off_right = vf->priv->width * 3;
-        vf->priv->in.off_right += vf->priv->in.width * 3;
-        break;
-    case INTERLEAVE_ROWS_RL:
-        vf->priv->row_step      = 2;
-        vf->priv->height        = vf->priv->height / 2;
-        vf->priv->out.off_left  = vf->priv->width * 3;
-        vf->priv->in.off_left  += vf->priv->in.width * 3;
-        break;
-    case MONO_R:
-        //same as MONO_L only needs switching of input offsets
-        vf->priv->in.off_left   = vf->priv->in.off_right;
-        vf->priv->in.row_left   = vf->priv->in.row_right;
-        //nobreak;
-    case MONO_L:
-        //use default settings
-        break;
-    default:
-        ff_mp_msg(MSGT_VFILTER, MSGL_WARN,
-            "[stereo3d] stereo format of output is not supported\n");
-        return 0;
-        break;
-    }
-//    if (!opt_screen_size_x && !opt_screen_size_y) {
-        d_width     = d_width  * vf->priv->out.width  / width;
-        d_height    = d_height * vf->priv->out.height / height;
-//    }
-
-    return ff_vf_next_config(vf, vf->priv->out.width, vf->priv->out.height,
-                          d_width, d_height, flags, outfmt);
-}
-
-static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
-{
-    mp_image_t *dmpi;
-    if (vf->priv->in.fmt == vf->priv->out.fmt) { //nothing to do
-        dmpi = mpi;
-    } else {
-        int out_off_left, out_off_right;
-        int in_off_left  = vf->priv->in.row_left   * mpi->stride[0]  +
-                           vf->priv->in.off_left;
-        int in_off_right = vf->priv->in.row_right  * mpi->stride[0]  +
-                           vf->priv->in.off_right;
-
-        dmpi = ff_vf_get_image(vf->next, IMGFMT_RGB24, MP_IMGTYPE_TEMP,
-                            MP_IMGFLAG_ACCEPT_STRIDE,
-                            vf->priv->out.width, vf->priv->out.height);
-        out_off_left   = vf->priv->out.row_left  * dmpi->stride[0] +
-                         vf->priv->out.off_left;
-        out_off_right  = vf->priv->out.row_right * dmpi->stride[0] +
-                         vf->priv->out.off_right;
-
-        switch (vf->priv->out.fmt) {
-        case SIDE_BY_SIDE_LR:
-        case SIDE_BY_SIDE_RL:
-        case SIDE_BY_SIDE_2_LR:
-        case SIDE_BY_SIDE_2_RL:
-        case ABOVE_BELOW_LR:
-        case ABOVE_BELOW_RL:
-        case ABOVE_BELOW_2_LR:
-        case ABOVE_BELOW_2_RL:
-        case INTERLEAVE_ROWS_LR:
-        case INTERLEAVE_ROWS_RL:
-            memcpy_pic2(dmpi->planes[0] + out_off_left,
-                       mpi->planes[0] + in_off_left,
-                       3 * vf->priv->width,
-                       vf->priv->height,
-                       dmpi->stride[0] * vf->priv->row_step,
-                       mpi->stride[0] * vf->priv->row_step,
-                       vf->priv->row_step != 1);
-            memcpy_pic2(dmpi->planes[0] + out_off_right,
-                       mpi->planes[0] + in_off_right,
-                       3 * vf->priv->width,
-                       vf->priv->height,
-                       dmpi->stride[0] * vf->priv->row_step,
-                       mpi->stride[0] * vf->priv->row_step,
-                       vf->priv->row_step != 1);
-            break;
-        case MONO_L:
-        case MONO_R:
-            memcpy_pic(dmpi->planes[0],
-                       mpi->planes[0] + in_off_left,
-                       3 * vf->priv->width,
-                       vf->priv->height,
-                       dmpi->stride[0],
-                       mpi->stride[0]);
-            break;
-        case ANAGLYPH_RC_GRAY:
-        case ANAGLYPH_RC_HALF:
-        case ANAGLYPH_RC_COLOR:
-        case ANAGLYPH_RC_DUBOIS:
-        case ANAGLYPH_GM_GRAY:
-        case ANAGLYPH_GM_HALF:
-        case ANAGLYPH_GM_COLOR:
-        case ANAGLYPH_GM_DUBOIS:
-        case ANAGLYPH_YB_GRAY:
-        case ANAGLYPH_YB_HALF:
-        case ANAGLYPH_YB_COLOR:
-        case ANAGLYPH_YB_DUBOIS: {
-            int i,x,y,il,ir,o;
-            unsigned char *source     = mpi->planes[0];
-            unsigned char *dest       = dmpi->planes[0];
-            unsigned int   out_width  = vf->priv->out.width;
-            int           *ana_matrix[3];
-
-            for(i = 0; i < 3; i++)
-                ana_matrix[i] = vf->priv->ana_matrix[i];
-
-            for (y = 0; y < vf->priv->out.height; y++) {
-                o   = dmpi->stride[0] * y;
-                il  = in_off_left  + y * mpi->stride[0];
-                ir  = in_off_right + y * mpi->stride[0];
-                for (x = 0; x < out_width; x++) {
-                    dest[o    ]  = ana_convert(
-                                   ana_matrix[0], source + il, source + ir); //red out
-                    dest[o + 1]  = ana_convert(
-                                   ana_matrix[1], source + il, source + ir); //green out
-                    dest[o + 2]  = ana_convert(
-                                   ana_matrix[2], source + il, source + ir); //blue out
-                    il += 3;
-                    ir += 3;
-                    o  += 3;
-                }
-            }
-            break;
-        }
-        default:
-            ff_mp_msg(MSGT_VFILTER, MSGL_WARN,
-                   "[stereo3d] stereo format of output is not supported\n");
-            return 0;
-            break;
-        }
-    }
-    return ff_vf_next_put_image(vf, dmpi, pts);
-}
-
-static int query_format(struct vf_instance *vf, unsigned int fmt)
-{
-    switch (fmt)
-    case IMGFMT_RGB24:
-        return ff_vf_next_query_format(vf, fmt);
-    return 0;
-}
-
-static void uninit(vf_instance_t *vf)
-{
-    free(vf->priv);
-}
-
-static int vf_open(vf_instance_t *vf, char *args)
-{
-    vf->config          = config;
-    vf->uninit          = uninit;
-    vf->put_image       = put_image;
-    vf->query_format    = query_format;
-    vf->priv=malloc(sizeof(struct vf_priv_s));
-    memset(vf->priv, 0, sizeof(struct vf_priv_s));
-
-    vf->priv->in.fmt = SIDE_BY_SIDE_LR;
-    vf->priv->out.fmt= ANAGLYPH_RC_DUBOIS;
-    if (args) sscanf(args, "%d:%d", &vf->priv->in.fmt, &vf->priv->out.fmt);
-
-    return 1;
-}
-#if 0
-///Presets usage
-static const struct format_preset {
-  char* name;
-  stereo_code scode;
-} vf_format_presets_defs[] = {
-    {"arcg",                             ANAGLYPH_RC_GRAY},
-    {"anaglyph_red_cyan_gray",           ANAGLYPH_RC_GRAY},
-    {"arch",                             ANAGLYPH_RC_HALF},
-    {"anaglyph_red_cyan_half_color",     ANAGLYPH_RC_HALF},
-    {"arcc",                             ANAGLYPH_RC_COLOR},
-    {"anaglyph_red_cyan_color",          ANAGLYPH_RC_COLOR},
-    {"arcd",                             ANAGLYPH_RC_DUBOIS},
-    {"anaglyph_red_cyan_dubios",         ANAGLYPH_RC_DUBOIS},
-    {"agmg",                             ANAGLYPH_GM_GRAY},
-    {"anaglyph_green_magenta_gray",      ANAGLYPH_GM_GRAY},
-    {"agmh",                             ANAGLYPH_GM_HALF},
-    {"anaglyph_green_magenta_half_color",ANAGLYPH_GM_HALF},
-    {"agmc",                             ANAGLYPH_GM_COLOR},
-    {"anaglyph_green_magenta_color",     ANAGLYPH_GM_COLOR},
-    {"agmd",                             ANAGLYPH_GM_DUBOIS},
-    {"anaglyph_green_magenta_dubois",    ANAGLYPH_GM_DUBOIS},
-    {"aybg",                             ANAGLYPH_YB_GRAY},
-    {"anaglyph_yellow_blue_gray",        ANAGLYPH_YB_GRAY},
-    {"aybh",                             ANAGLYPH_YB_HALF},
-    {"anaglyph_yellow_blue_half_color",  ANAGLYPH_YB_HALF},
-    {"aybc",                             ANAGLYPH_YB_COLOR},
-    {"anaglyph_yellow_blue_color",       ANAGLYPH_YB_COLOR},
-    {"aybd",                             ANAGLYPH_YB_DUBOIS},
-    {"anaglyph_yellow_blue_dubois",      ANAGLYPH_YB_DUBOIS},
-    {"ml",                               MONO_L},
-    {"mono_left",                        MONO_L},
-    {"mr",                               MONO_R},
-    {"mono_right",                       MONO_R},
-    {"sbsl",                             SIDE_BY_SIDE_LR},
-    {"side_by_side_left_first",          SIDE_BY_SIDE_LR},
-    {"sbsr",                             SIDE_BY_SIDE_RL},
-    {"side_by_side_right_first",         SIDE_BY_SIDE_RL},
-    {"sbs2l",                              SIDE_BY_SIDE_2_LR},
-    {"side_by_side_half_width_left_first", SIDE_BY_SIDE_2_LR},
-    {"sbs2r",                              SIDE_BY_SIDE_2_RL},
-    {"side_by_side_half_width_right_first",SIDE_BY_SIDE_2_RL},
-    {"abl",                              ABOVE_BELOW_LR},
-    {"above_below_left_first",           ABOVE_BELOW_LR},
-    {"abr",                              ABOVE_BELOW_RL},
-    {"above_below_right_first",          ABOVE_BELOW_RL},
-    {"ab2l",                               ABOVE_BELOW_2_LR},
-    {"above_below_half_height_left_first", ABOVE_BELOW_2_LR},
-    {"ab2r",                               ABOVE_BELOW_2_RL},
-    {"above_below_half_height_right_first",ABOVE_BELOW_2_RL},
-    {"irl",                                INTERLEAVE_ROWS_LR},
-    {"interleave_rows_left_first",         INTERLEAVE_ROWS_LR},
-    {"irr",                                INTERLEAVE_ROWS_RL},
-    {"interleave_rows_right_first",        INTERLEAVE_ROWS_RL},
-    { NULL, 0}
-};
-
-#define ST_OFF(f) M_ST_OFF(struct format_preset,f)
-static const m_option_t vf_format_preset_fields_in[] = {
-  {"in", ST_OFF(scode), CONF_TYPE_INT, 0,0,0, NULL},
-  { NULL, NULL, 0, 0, 0, 0,  NULL }
-};
-static const m_option_t vf_format_preset_fields_out[] = {
-  {"out", ST_OFF(scode), CONF_TYPE_INT, 0,0,0, NULL},
-  { NULL, NULL, 0, 0, 0, 0,  NULL }
-};
-
-static const m_struct_t vf_format_preset_in = {
-  "stereo_format_preset_in",
-  sizeof(struct format_preset),
-  NULL,
-  vf_format_preset_fields_in
-};
-static const m_struct_t vf_format_preset_out = {
-  "stereo_format_preset_out",
-  sizeof(struct format_preset),
-  NULL,
-  vf_format_preset_fields_out
-};
-
-static const m_struct_t vf_opts;
-static const m_obj_presets_t format_preset_in = {
-  (struct m_struct_st*)&vf_format_preset_in,
-  (struct m_struct_st*)&vf_opts,
-  (struct format_preset*)vf_format_presets_defs,
-  ST_OFF(name)
-};
-static const m_obj_presets_t format_preset_out = {
-  (struct m_struct_st*)&vf_format_preset_out,
-  (struct m_struct_st*)&vf_opts,
-  (struct format_preset*)vf_format_presets_defs,
-  ST_OFF(name)
-};
-
-/// Now the options
-#undef ST_OFF
-#define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
-static const m_option_t vf_opts_fields[] = {
-  {"stereo_in", 0, CONF_TYPE_OBJ_PRESETS, 0, 0, 0,
-                   (m_obj_presets_t*)&format_preset_in},
-  {"stereo_out", 0, CONF_TYPE_OBJ_PRESETS, 0, 0, 0,
-                    (m_obj_presets_t*)&format_preset_out},
-  {"in", ST_OFF(in.fmt), CONF_TYPE_INT, 0,0,0, NULL},
-  {"out", ST_OFF(out.fmt), CONF_TYPE_INT, 0,0,0, NULL},
-  { NULL, NULL, 0, 0, 0, 0,  NULL }
-};
-
-static const m_struct_t vf_opts = {
-  "stereo3d",
-  sizeof(struct vf_priv_s),
-  &ff_vf_priv_default,
-  vf_opts_fields
-};
-#endif
-
-//==info struct==//
-const vf_info_t ff_vf_info_stereo3d = {
-    "stereoscopic 3d view",
-    "stereo3d",
-    "Gordon Schmidt",
-    "view stereoscopic videos",
-    vf_open,
-//    &vf_opts
-};
diff --git a/libavfilter/libmpcodecs/vf_telecine.c b/libavfilter/libmpcodecs/vf_telecine.c
deleted file mode 100644
index 77f75f0..0000000
--- a/libavfilter/libmpcodecs/vf_telecine.c
+++ /dev/null
@@ -1,158 +0,0 @@
-/*
- * This file is part of MPlayer.
- *
- * MPlayer is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * MPlayer 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with MPlayer; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "config.h"
-#include "mp_msg.h"
-
-#include "img_format.h"
-#include "mp_image.h"
-#include "vf.h"
-
-#include "libvo/fastmemcpy.h"
-
-struct vf_priv_s {
-    int frame;
-};
-
-static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
-{
-    mp_image_t *dmpi;
-    int ret;
-    int w            = (IMGFMT_IS_YUVP16(mpi->imgfmt) ? 2 : 1) * mpi->w;
-    int chroma_width = (IMGFMT_IS_YUVP16(mpi->imgfmt) ? 2 : 1) * mpi->chroma_width;
-
-    vf->priv->frame = (vf->priv->frame+1)%4;
-
-    dmpi = ff_vf_get_image(vf->next, mpi->imgfmt,
-        MP_IMGTYPE_STATIC, MP_IMGFLAG_ACCEPT_STRIDE |
-        MP_IMGFLAG_PRESERVE, mpi->width, mpi->height);
-
-    ret = 0;
-    //    0/0  1/1  2/2  2/3  3/0
-    switch (vf->priv->frame) {
-    case 0:
-        my_memcpy_pic(dmpi->planes[0]+dmpi->stride[0],
-            mpi->planes[0]+mpi->stride[0], w, mpi->h/2,
-            dmpi->stride[0]*2, mpi->stride[0]*2);
-        if (mpi->flags & MP_IMGFLAG_PLANAR) {
-            my_memcpy_pic(dmpi->planes[1]+dmpi->stride[1],
-                mpi->planes[1]+mpi->stride[1],
-                chroma_width, mpi->chroma_height/2,
-                dmpi->stride[1]*2, mpi->stride[1]*2);
-            my_memcpy_pic(dmpi->planes[2]+dmpi->stride[2],
-                mpi->planes[2]+mpi->stride[2],
-                chroma_width, mpi->chroma_height/2,
-                dmpi->stride[2]*2, mpi->stride[2]*2);
-        }
-        ret = ff_vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
-        /* Fallthrough */
-    case 1:
-    case 2:
-        memcpy_pic(dmpi->planes[0], mpi->planes[0], w, mpi->h,
-            dmpi->stride[0], mpi->stride[0]);
-        if (mpi->flags & MP_IMGFLAG_PLANAR) {
-            memcpy_pic(dmpi->planes[1], mpi->planes[1],
-                chroma_width, mpi->chroma_height,
-                dmpi->stride[1], mpi->stride[1]);
-            memcpy_pic(dmpi->planes[2], mpi->planes[2],
-                chroma_width, mpi->chroma_height,
-                dmpi->stride[2], mpi->stride[2]);
-        }
-        return ff_vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE) || ret;
-    case 3:
-        my_memcpy_pic(dmpi->planes[0]+dmpi->stride[0],
-            mpi->planes[0]+mpi->stride[0], w, mpi->h/2,
-            dmpi->stride[0]*2, mpi->stride[0]*2);
-        if (mpi->flags & MP_IMGFLAG_PLANAR) {
-            my_memcpy_pic(dmpi->planes[1]+dmpi->stride[1],
-                mpi->planes[1]+mpi->stride[1],
-                chroma_width, mpi->chroma_height/2,
-                dmpi->stride[1]*2, mpi->stride[1]*2);
-            my_memcpy_pic(dmpi->planes[2]+dmpi->stride[2],
-                mpi->planes[2]+mpi->stride[2],
-                chroma_width, mpi->chroma_height/2,
-                dmpi->stride[2]*2, mpi->stride[2]*2);
-        }
-        ret = ff_vf_next_put_image(vf, dmpi, MP_NOPTS_VALUE);
-        my_memcpy_pic(dmpi->planes[0], mpi->planes[0], w, mpi->h/2,
-            dmpi->stride[0]*2, mpi->stride[0]*2);
-        if (mpi->flags & MP_IMGFLAG_PLANAR) {
-            my_memcpy_pic(dmpi->planes[1], mpi->planes[1],
-                chroma_width, mpi->chroma_height/2,
-                dmpi->stride[1]*2, mpi->stride[1]*2);
-            my_memcpy_pic(dmpi->planes[2], mpi->planes[2],
-                chroma_width, mpi->chroma_height/2,
-                dmpi->stride[2]*2, mpi->stride[2]*2);
-        }
-        return ret;
-    }
-    return 0;
-}
-
-#if 0
-static int query_format(struct vf_instance *vf, unsigned int fmt)
-{
-    /* FIXME - figure out which other formats work */
-    switch (fmt) {
-    case IMGFMT_YV12:
-    case IMGFMT_IYUV:
-    case IMGFMT_I420:
-        return ff_vf_next_query_format(vf, fmt);
-    }
-    return 0;
-}
-
-static int config(struct vf_instance *vf,
-        int width, int height, int d_width, int d_height,
-    unsigned int flags, unsigned int outfmt)
-{
-    return ff_vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
-}
-#endif
-
-static void uninit(struct vf_instance *vf)
-{
-    free(vf->priv);
-}
-
-static int vf_open(vf_instance_t *vf, char *args)
-{
-    //vf->config = config;
-    vf->put_image = put_image;
-    //vf->query_format = query_format;
-    vf->uninit = uninit;
-    vf->default_reqs = VFCAP_ACCEPT_STRIDE;
-    vf->priv = calloc(1, sizeof(struct vf_priv_s));
-    vf->priv->frame = 1;
-    if (args) sscanf(args, "%d", &vf->priv->frame);
-    vf->priv->frame--;
-    return 1;
-}
-
-const vf_info_t ff_vf_info_telecine = {
-    "telecine filter",
-    "telecine",
-    "Rich Felker",
-    "",
-    vf_open,
-    NULL
-};
diff --git a/libavfilter/opencl_allkernels.c b/libavfilter/opencl_allkernels.c
new file mode 100644
index 0000000..021eec2
--- /dev/null
+++ b/libavfilter/opencl_allkernels.c
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2013 Wei Gao <weigao@multicorewareinc.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 "opencl_allkernels.h"
+#if CONFIG_OPENCL
+#include "libavutil/opencl.h"
+#include "deshake_kernel.h"
+#endif
+
+#define OPENCL_REGISTER_KERNEL_CODE(X, x)                                              \
+    {                                                                                  \
+        if (CONFIG_##X##_FILTER) {                                                     \
+            av_opencl_register_kernel_code(ff_kernel_##x##_opencl);                    \
+        }                                                                              \
+    }
+
+void ff_opencl_register_filter_kernel_code_all(void)
+{
+ #if CONFIG_OPENCL
+   OPENCL_REGISTER_KERNEL_CODE(DESHAKE,     deshake);
+ #endif
+}
diff --git a/libavfilter/opencl_allkernels.h b/libavfilter/opencl_allkernels.h
new file mode 100644
index 0000000..aca02e0
--- /dev/null
+++ b/libavfilter/opencl_allkernels.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2013 Wei Gao <weigao@multicorewareinc.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
+ */
+
+#ifndef AVFILTER_OPENCL_ALLKERNEL_H
+#define AVFILTER_OPENCL_ALLKERNEL_H
+
+#include "avfilter.h"
+#include "config.h"
+
+void ff_opencl_register_filter_kernel_code_all(void);
+
+#endif /* AVFILTER_OPENCL_ALLKERNEL_H */
diff --git a/libavfilter/sink_buffer.c b/libavfilter/sink_buffer.c
deleted file mode 100644
index 9a99b56..0000000
--- a/libavfilter/sink_buffer.c
+++ /dev/null
@@ -1,499 +0,0 @@
-/*
- * Copyright (c) 2011 Stefano Sabatini
- *
- * 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
- * buffer sink
- */
-
-#include "libavutil/avassert.h"
-#include "libavutil/channel_layout.h"
-#include "libavutil/fifo.h"
-#include "avfilter.h"
-#include "buffersink.h"
-#include "audio.h"
-#include "internal.h"
-
-AVBufferSinkParams *av_buffersink_params_alloc(void)
-{
-    static const int pixel_fmts[] = { AV_PIX_FMT_NONE };
-    AVBufferSinkParams *params = av_malloc(sizeof(AVBufferSinkParams));
-    if (!params)
-        return NULL;
-
-    params->pixel_fmts = pixel_fmts;
-    return params;
-}
-
-AVABufferSinkParams *av_abuffersink_params_alloc(void)
-{
-    AVABufferSinkParams *params = av_mallocz(sizeof(AVABufferSinkParams));
-
-    if (!params)
-        return NULL;
-    return params;
-}
-
-typedef struct {
-    AVFifoBuffer *fifo;                      ///< FIFO buffer of video frame references
-    unsigned warning_limit;
-
-    /* only used for video */
-    enum AVPixelFormat *pixel_fmts;           ///< list of accepted pixel formats, must be terminated with -1
-
-    /* only used for audio */
-    enum AVSampleFormat *sample_fmts;       ///< list of accepted sample formats, terminated by AV_SAMPLE_FMT_NONE
-    int64_t *channel_layouts;               ///< list of accepted channel layouts, terminated by -1
-    int all_channel_counts;
-    int *sample_rates;                      ///< list of accepted sample rates, terminated by -1
-} BufferSinkContext;
-
-#define FIFO_INIT_SIZE 8
-
-static av_cold int common_init(AVFilterContext *ctx)
-{
-    BufferSinkContext *buf = ctx->priv;
-
-    buf->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef *));
-    if (!buf->fifo) {
-        av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
-        return AVERROR(ENOMEM);
-    }
-    buf->warning_limit = 100;
-    return 0;
-}
-
-static av_cold void common_uninit(AVFilterContext *ctx)
-{
-    BufferSinkContext *buf = ctx->priv;
-    AVFilterBufferRef *picref;
-
-    if (buf->fifo) {
-        while (av_fifo_size(buf->fifo) >= sizeof(AVFilterBufferRef *)) {
-            av_fifo_generic_read(buf->fifo, &picref, sizeof(picref), NULL);
-            avfilter_unref_buffer(picref);
-        }
-        av_fifo_free(buf->fifo);
-        buf->fifo = NULL;
-    }
-}
-
-static int add_buffer_ref(AVFilterContext *ctx, AVFilterBufferRef *ref)
-{
-    BufferSinkContext *buf = ctx->priv;
-
-    if (av_fifo_space(buf->fifo) < sizeof(AVFilterBufferRef *)) {
-        /* realloc fifo size */
-        if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) {
-            av_log(ctx, AV_LOG_ERROR,
-                   "Cannot buffer more frames. Consume some available frames "
-                   "before adding new ones.\n");
-            return AVERROR(ENOMEM);
-        }
-    }
-
-    /* cache frame */
-    av_fifo_generic_write(buf->fifo, &ref, sizeof(AVFilterBufferRef *), NULL);
-    return 0;
-}
-
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *ref)
-{
-    AVFilterContext *ctx = inlink->dst;
-    BufferSinkContext *buf = inlink->dst->priv;
-    int ret;
-
-    if ((ret = add_buffer_ref(ctx, ref)) < 0)
-        return ret;
-    if (buf->warning_limit &&
-        av_fifo_size(buf->fifo) / sizeof(AVFilterBufferRef *) >= buf->warning_limit) {
-        av_log(ctx, AV_LOG_WARNING,
-               "%d buffers queued in %s, something may be wrong.\n",
-               buf->warning_limit,
-               (char *)av_x_if_null(ctx->name, ctx->filter->name));
-        buf->warning_limit *= 10;
-    }
-    return 0;
-}
-
-void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size)
-{
-    AVFilterLink *inlink = ctx->inputs[0];
-
-    inlink->min_samples = inlink->max_samples =
-    inlink->partial_buf_size = frame_size;
-}
-
-int av_buffersink_get_buffer_ref(AVFilterContext *ctx,
-                                  AVFilterBufferRef **bufref, int flags)
-{
-    BufferSinkContext *buf = ctx->priv;
-    AVFilterLink *inlink = ctx->inputs[0];
-    int ret;
-    *bufref = NULL;
-
-    av_assert0(    !strcmp(ctx->filter->name, "buffersink")
-                || !strcmp(ctx->filter->name, "abuffersink")
-                || !strcmp(ctx->filter->name, "ffbuffersink")
-                || !strcmp(ctx->filter->name, "ffabuffersink"));
-
-    /* no picref available, fetch it from the filterchain */
-    if (!av_fifo_size(buf->fifo)) {
-        if (flags & AV_BUFFERSINK_FLAG_NO_REQUEST)
-            return AVERROR(EAGAIN);
-        if ((ret = ff_request_frame(inlink)) < 0)
-            return ret;
-    }
-
-    if (!av_fifo_size(buf->fifo))
-        return AVERROR(EINVAL);
-
-    if (flags & AV_BUFFERSINK_FLAG_PEEK)
-        *bufref = *((AVFilterBufferRef **)av_fifo_peek2(buf->fifo, 0));
-    else
-        av_fifo_generic_read(buf->fifo, bufref, sizeof(*bufref), NULL);
-
-    return 0;
-}
-
-AVRational av_buffersink_get_frame_rate(AVFilterContext *ctx)
-{
-    av_assert0(   !strcmp(ctx->filter->name, "buffersink")
-               || !strcmp(ctx->filter->name, "ffbuffersink"));
-
-    return ctx->inputs[0]->frame_rate;
-}
-
-int av_buffersink_poll_frame(AVFilterContext *ctx)
-{
-    BufferSinkContext *buf = ctx->priv;
-    AVFilterLink *inlink = ctx->inputs[0];
-
-    av_assert0(   !strcmp(ctx->filter->name, "buffersink")
-               || !strcmp(ctx->filter->name, "abuffersink")
-               || !strcmp(ctx->filter->name, "ffbuffersink")
-               || !strcmp(ctx->filter->name, "ffabuffersink"));
-
-    return av_fifo_size(buf->fifo)/sizeof(AVFilterBufferRef *) + ff_poll_frame(inlink);
-}
-
-static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaque)
-{
-    BufferSinkContext *buf = ctx->priv;
-    AVBufferSinkParams *params = opaque;
-
-    if (params && params->pixel_fmts) {
-        const int *pixel_fmts = params->pixel_fmts;
-
-        buf->pixel_fmts = ff_copy_int_list(pixel_fmts);
-        if (!buf->pixel_fmts)
-            return AVERROR(ENOMEM);
-    }
-
-    return common_init(ctx);
-}
-
-static av_cold void vsink_uninit(AVFilterContext *ctx)
-{
-    BufferSinkContext *buf = ctx->priv;
-    av_freep(&buf->pixel_fmts);
-    common_uninit(ctx);
-}
-
-static int vsink_query_formats(AVFilterContext *ctx)
-{
-    BufferSinkContext *buf = ctx->priv;
-
-    if (buf->pixel_fmts)
-        ff_set_common_formats(ctx, ff_make_format_list(buf->pixel_fmts));
-    else
-        ff_default_query_formats(ctx);
-
-    return 0;
-}
-
-static const AVFilterPad ffbuffersink_inputs[] = {
-    {
-        .name      = "default",
-        .type      = AVMEDIA_TYPE_VIDEO,
-        .filter_frame = filter_frame,
-        .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
-    },
-    { NULL },
-};
-
-AVFilter avfilter_vsink_ffbuffersink = {
-    .name      = "ffbuffersink",
-    .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
-    .priv_size = sizeof(BufferSinkContext),
-    .init_opaque = vsink_init,
-    .uninit    = vsink_uninit,
-
-    .query_formats = vsink_query_formats,
-    .inputs        = ffbuffersink_inputs,
-    .outputs       = NULL,
-};
-
-static const AVFilterPad buffersink_inputs[] = {
-    {
-        .name      = "default",
-        .type      = AVMEDIA_TYPE_VIDEO,
-        .filter_frame = filter_frame,
-        .min_perms = AV_PERM_READ | AV_PERM_PRESERVE,
-    },
-    { NULL },
-};
-
-AVFilter avfilter_vsink_buffersink = {
-    .name      = "buffersink",
-    .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
-    .priv_size = sizeof(BufferSinkContext),
-    .init_opaque = vsink_init,
-    .uninit    = vsink_uninit,
-
-    .query_formats = vsink_query_formats,
-    .inputs        = buffersink_inputs,
-    .outputs       = NULL,
-};
-
-static int64_t *concat_channels_lists(const int64_t *layouts, const int *counts)
-{
-    int nb_layouts = 0, nb_counts = 0, i;
-    int64_t *list;
-
-    if (layouts)
-        for (; layouts[nb_layouts] != -1; nb_layouts++);
-    if (counts)
-        for (; counts[nb_counts] != -1; nb_counts++);
-    if (nb_counts > INT_MAX - 1 - nb_layouts)
-        return NULL;
-    if (!(list = av_calloc(nb_layouts + nb_counts + 1, sizeof(*list))))
-        return NULL;
-    for (i = 0; i < nb_layouts; i++)
-        list[i] = layouts[i];
-    for (i = 0; i < nb_counts; i++)
-        list[nb_layouts + i] = FF_COUNT2LAYOUT(counts[i]);
-    list[nb_layouts + nb_counts] = -1;
-    return list;
-}
-
-static av_cold int asink_init(AVFilterContext *ctx, const char *args, void *opaque)
-{
-    BufferSinkContext *buf = ctx->priv;
-    AVABufferSinkParams *params = opaque;
-
-    if (params && params->sample_fmts) {
-        buf->sample_fmts = ff_copy_int_list(params->sample_fmts);
-        if (!buf->sample_fmts)
-            return AVERROR(ENOMEM);
-    }
-    if (params && params->sample_rates) {
-        buf->sample_rates = ff_copy_int_list(params->sample_rates);
-        if (!buf->sample_rates)
-            return AVERROR(ENOMEM);
-    }
-    if (params && (params->channel_layouts || params->channel_counts)) {
-        if (params->all_channel_counts) {
-            av_log(ctx, AV_LOG_ERROR,
-                   "Conflicting all_channel_counts and list in parameters\n");
-            return AVERROR(EINVAL);
-        }
-        buf->channel_layouts = concat_channels_lists(params->channel_layouts,
-                                                     params->channel_counts);
-        if (!buf->channel_layouts)
-            return AVERROR(ENOMEM);
-    }
-    if (params)
-        buf->all_channel_counts = params->all_channel_counts;
-    return common_init(ctx);
-}
-
-static av_cold void asink_uninit(AVFilterContext *ctx)
-{
-    BufferSinkContext *buf = ctx->priv;
-
-    av_freep(&buf->sample_fmts);
-    av_freep(&buf->sample_rates);
-    av_freep(&buf->channel_layouts);
-    common_uninit(ctx);
-}
-
-static int asink_query_formats(AVFilterContext *ctx)
-{
-    BufferSinkContext *buf = ctx->priv;
-    AVFilterFormats *formats = NULL;
-    AVFilterChannelLayouts *layouts = NULL;
-
-    if (buf->sample_fmts) {
-        if (!(formats = ff_make_format_list(buf->sample_fmts)))
-            return AVERROR(ENOMEM);
-        ff_set_common_formats(ctx, formats);
-    }
-
-    if (buf->channel_layouts || buf->all_channel_counts) {
-            layouts = buf->all_channel_counts ? ff_all_channel_counts() :
-                      avfilter_make_format64_list(buf->channel_layouts);
-        if (!layouts)
-            return AVERROR(ENOMEM);
-        ff_set_common_channel_layouts(ctx, layouts);
-    }
-
-    if (buf->sample_rates) {
-        formats = ff_make_format_list(buf->sample_rates);
-        if (!formats)
-            return AVERROR(ENOMEM);
-        ff_set_common_samplerates(ctx, formats);
-    }
-
-    return 0;
-}
-
-static const AVFilterPad ffabuffersink_inputs[] = {
-    {
-        .name           = "default",
-        .type           = AVMEDIA_TYPE_AUDIO,
-        .filter_frame   = filter_frame,
-        .min_perms      = AV_PERM_READ | AV_PERM_PRESERVE,
-    },
-    { NULL },
-};
-
-AVFilter avfilter_asink_ffabuffersink = {
-    .name      = "ffabuffersink",
-    .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
-    .init_opaque = asink_init,
-    .uninit    = asink_uninit,
-    .priv_size = sizeof(BufferSinkContext),
-    .query_formats = asink_query_formats,
-    .inputs        = ffabuffersink_inputs,
-    .outputs       = NULL,
-};
-
-static const AVFilterPad abuffersink_inputs[] = {
-    {
-        .name           = "default",
-        .type           = AVMEDIA_TYPE_AUDIO,
-        .filter_frame   = filter_frame,
-        .min_perms      = AV_PERM_READ | AV_PERM_PRESERVE,
-    },
-    { NULL },
-};
-
-AVFilter avfilter_asink_abuffersink = {
-    .name      = "abuffersink",
-    .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
-    .init_opaque = asink_init,
-    .uninit    = asink_uninit,
-    .priv_size = sizeof(BufferSinkContext),
-    .query_formats = asink_query_formats,
-    .inputs        = abuffersink_inputs,
-    .outputs       = NULL,
-};
-
-/* Libav compatibility API */
-
-extern AVFilter avfilter_vsink_buffer;
-extern AVFilter avfilter_asink_abuffer;
-
-int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
-{
-    AVFilterBufferRef *tbuf;
-    int ret;
-
-    if (ctx->filter->          inputs[0].start_frame ==
-        avfilter_vsink_buffer. inputs[0].start_frame ||
-        ctx->filter->          inputs[0].filter_frame ==
-        avfilter_asink_abuffer.inputs[0].filter_frame)
-        return ff_buffersink_read_compat(ctx, buf);
-    av_assert0(ctx->filter->                inputs[0].end_frame ==
-               avfilter_vsink_ffbuffersink. inputs[0].end_frame ||
-               ctx->filter->                inputs[0].filter_frame ==
-               avfilter_asink_ffabuffersink.inputs[0].filter_frame);
-
-    ret = av_buffersink_get_buffer_ref(ctx, &tbuf,
-                                       buf ? 0 : AV_BUFFERSINK_FLAG_PEEK);
-    if (!buf)
-        return ret >= 0;
-    if (ret < 0)
-        return ret;
-    *buf = tbuf;
-    return 0;
-}
-
-int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf,
-                               int nb_samples)
-{
-    BufferSinkContext *sink = ctx->priv;
-    int ret = 0, have_samples = 0, need_samples;
-    AVFilterBufferRef *tbuf, *in_buf;
-    AVFilterLink *link = ctx->inputs[0];
-    int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
-
-    if (ctx->filter->          inputs[0].filter_frame ==
-        avfilter_asink_abuffer.inputs[0].filter_frame)
-        return ff_buffersink_read_samples_compat(ctx, buf, nb_samples);
-    av_assert0(ctx->filter->                inputs[0].filter_frame ==
-               avfilter_asink_ffabuffersink.inputs[0].filter_frame);
-
-    tbuf = ff_get_audio_buffer(link, AV_PERM_WRITE, nb_samples);
-    if (!tbuf)
-        return AVERROR(ENOMEM);
-
-    while (have_samples < nb_samples) {
-        ret = av_buffersink_get_buffer_ref(ctx, &in_buf,
-                                           AV_BUFFERSINK_FLAG_PEEK);
-        if (ret < 0) {
-            if (ret == AVERROR_EOF && have_samples) {
-                nb_samples = have_samples;
-                ret = 0;
-            }
-            break;
-        }
-
-        need_samples = FFMIN(in_buf->audio->nb_samples,
-                             nb_samples - have_samples);
-        av_samples_copy(tbuf->extended_data, in_buf->extended_data,
-                        have_samples, 0, need_samples,
-                        nb_channels, in_buf->format);
-        have_samples += need_samples;
-        if (need_samples < in_buf->audio->nb_samples) {
-            in_buf->audio->nb_samples -= need_samples;
-            av_samples_copy(in_buf->extended_data, in_buf->extended_data,
-                            0, need_samples, in_buf->audio->nb_samples,
-                            nb_channels, in_buf->format);
-        } else {
-            av_buffersink_get_buffer_ref(ctx, &in_buf, 0);
-            avfilter_unref_buffer(in_buf);
-        }
-    }
-    tbuf->audio->nb_samples = have_samples;
-
-    if (ret < 0) {
-        av_assert0(!av_fifo_size(sink->fifo));
-        if (have_samples)
-            add_buffer_ref(ctx, tbuf);
-        else
-            avfilter_unref_buffer(tbuf);
-        return ret;
-    }
-
-    *buf = tbuf;
-    return 0;
-}
diff --git a/libavfilter/split.c b/libavfilter/split.c
index a54bef9..251ea2e 100644
--- a/libavfilter/split.c
+++ b/libavfilter/split.c
@@ -27,32 +27,30 @@
 
 #include "libavutil/internal.h"
 #include "libavutil/mem.h"
+#include "libavutil/opt.h"
+
 #include "avfilter.h"
 #include "audio.h"
 #include "internal.h"
 #include "video.h"
 
-static int split_init(AVFilterContext *ctx, const char *args)
+typedef struct SplitContext {
+    const AVClass *class;
+    int nb_outputs;
+} SplitContext;
+
+static int split_init(AVFilterContext *ctx)
 {
-    int i, nb_outputs = 2;
+    SplitContext *s = ctx->priv;
+    int i;
 
-    if (args) {
-        nb_outputs = strtol(args, NULL, 0);
-        if (nb_outputs <= 0) {
-            av_log(ctx, AV_LOG_ERROR, "Invalid number of outputs specified: %d.\n",
-                   nb_outputs);
-            return AVERROR(EINVAL);
-        }
-    }
-
-    for (i = 0; i < nb_outputs; i++) {
+    for (i = 0; i < s->nb_outputs; i++) {
         char name[32];
         AVFilterPad pad = { 0 };
 
         snprintf(name, sizeof(name), "output%d", i);
         pad.type = ctx->filter->inputs[0].type;
         pad.name = av_strdup(name);
-        pad.rej_perms = AV_PERM_WRITE;
 
         ff_insert_outpad(ctx, i, &pad);
     }
@@ -68,17 +66,17 @@
         av_freep(&ctx->output_pads[i].name);
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
 {
     AVFilterContext *ctx = inlink->dst;
     int i, ret = AVERROR_EOF;
 
     for (i = 0; i < ctx->nb_outputs; i++) {
-        AVFilterBufferRef *buf_out;
+        AVFrame *buf_out;
 
         if (ctx->outputs[i]->closed)
             continue;
-        buf_out = avfilter_ref_buffer(frame, ~AV_PERM_WRITE);
+        buf_out = av_frame_clone(frame);
         if (!buf_out) {
             ret = AVERROR(ENOMEM);
             break;
@@ -88,10 +86,23 @@
         if (ret < 0)
             break;
     }
-    avfilter_unref_bufferp(&frame);
+    av_frame_free(&frame);
     return ret;
 }
 
+#define OFFSET(x) offsetof(SplitContext, x)
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption options[] = {
+    { "outputs", "set number of outputs", OFFSET(nb_outputs), AV_OPT_TYPE_INT, { .i64 = 2 }, 1, INT_MAX, FLAGS },
+    { NULL },
+};
+
+#define split_options options
+AVFILTER_DEFINE_CLASS(split);
+
+#define asplit_options options
+AVFILTER_DEFINE_CLASS(asplit);
+
 static const AVFilterPad avfilter_vf_split_inputs[] = {
     {
         .name             = "default",
@@ -106,11 +117,16 @@
     .name      = "split",
     .description = NULL_IF_CONFIG_SMALL("Pass on the input video to N outputs."),
 
+    .priv_size  = sizeof(SplitContext),
+    .priv_class = &split_class,
+
     .init   = split_init,
     .uninit = split_uninit,
 
     .inputs    = avfilter_vf_split_inputs,
     .outputs   = NULL,
+
+    .flags     = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
 };
 
 static const AVFilterPad avfilter_af_asplit_inputs[] = {
@@ -127,9 +143,14 @@
     .name        = "asplit",
     .description = NULL_IF_CONFIG_SMALL("Pass on the audio input to N audio outputs."),
 
+    .priv_size  = sizeof(SplitContext),
+    .priv_class = &asplit_class,
+
     .init   = split_init,
     .uninit = split_uninit,
 
     .inputs  = avfilter_af_asplit_inputs,
     .outputs = NULL,
+
+    .flags   = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
 };
diff --git a/libavfilter/src_buffer.c b/libavfilter/src_buffer.c
deleted file mode 100644
index a997034..0000000
--- a/libavfilter/src_buffer.c
+++ /dev/null
@@ -1,123 +0,0 @@
-/*
- * Copyright (c) 2008 Vitor Sessak
- * Copyright (c) 2010 S.N. Hemanth Meenakshisundaram
- * Copyright (c) 2011 Mina Nagy Zaki
- *
- * 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
- * memory buffer source filter
- */
-
-#include "avfilter.h"
-#include "internal.h"
-#include "audio.h"
-#include "avcodec.h"
-#include "buffersrc.h"
-#include "asrc_abuffer.h"
-#include "libavutil/avstring.h"
-#include "libavutil/channel_layout.h"
-#include "libavutil/fifo.h"
-#include "libavutil/imgutils.h"
-
-typedef struct {
-    AVFifoBuffer     *fifo;
-    AVRational        time_base;     ///< time_base to set in the output link
-    int eof;
-    unsigned          nb_failed_requests;
-
-    /* Video only */
-    AVFilterContext  *scale;
-    int               h, w;
-    enum AVPixelFormat  pix_fmt;
-    AVRational        sample_aspect_ratio;
-    char              sws_param[256];
-
-    /* Audio only */
-    // Audio format of incoming buffers
-    int sample_rate;
-    unsigned int sample_format;
-    int64_t channel_layout;
-
-    // Normalization filters
-    AVFilterContext *aconvert;
-    AVFilterContext *aresample;
-} BufferSourceContext;
-
-static void buf_free(AVFilterBuffer *ptr)
-{
-    av_free(ptr);
-    return;
-}
-
-int av_asrc_buffer_add_audio_buffer_ref(AVFilterContext *ctx,
-                                        AVFilterBufferRef *samplesref,
-                                        int av_unused flags)
-{
-    return av_buffersrc_add_ref(ctx, samplesref, AV_BUFFERSRC_FLAG_NO_COPY);
-}
-
-int av_asrc_buffer_add_samples(AVFilterContext *ctx,
-                               uint8_t *data[8], int linesize[8],
-                               int nb_samples, int sample_rate,
-                               int sample_fmt, int64_t channel_layout, int planar,
-                               int64_t pts, int av_unused flags)
-{
-    AVFilterBufferRef *samplesref;
-
-    if (!channel_layout)
-        return AVERROR(EINVAL);
-    samplesref = avfilter_get_audio_buffer_ref_from_arrays(
-                     data, linesize[0], AV_PERM_WRITE,
-                     nb_samples,
-                     sample_fmt, channel_layout);
-    if (!samplesref)
-        return AVERROR(ENOMEM);
-
-    samplesref->buf->free  = buf_free;
-    samplesref->pts = pts;
-    samplesref->audio->sample_rate = sample_rate;
-
-    AV_NOWARN_DEPRECATED(
-    return av_asrc_buffer_add_audio_buffer_ref(ctx, samplesref, 0);
-    )
-}
-
-int av_asrc_buffer_add_buffer(AVFilterContext *ctx,
-                              uint8_t *buf, int buf_size, int sample_rate,
-                              int sample_fmt, int64_t channel_layout, int planar,
-                              int64_t pts, int av_unused flags)
-{
-    uint8_t *data[8] = {0};
-    int linesize[8];
-    int nb_channels = av_get_channel_layout_nb_channels(channel_layout),
-        nb_samples  = buf_size / nb_channels / av_get_bytes_per_sample(sample_fmt);
-
-    av_samples_fill_arrays(data, linesize,
-                           buf, nb_channels, nb_samples,
-                           sample_fmt, 16);
-
-    AV_NOWARN_DEPRECATED(
-    return av_asrc_buffer_add_samples(ctx,
-                                      data, linesize, nb_samples,
-                                      sample_rate,
-                                      sample_fmt, channel_layout, planar,
-                                      pts, flags);
-    )
-}
diff --git a/libavfilter/src_movie.c b/libavfilter/src_movie.c
index bd45766..7dc4d20 100644
--- a/libavfilter/src_movie.c
+++ b/libavfilter/src_movie.c
@@ -70,19 +70,20 @@
 } MovieContext;
 
 #define OFFSET(x) offsetof(MovieContext, x)
-#define F AV_OPT_FLAG_FILTERING_PARAM
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_VIDEO_PARAM
 
 static const AVOption movie_options[]= {
-{"format_name",  "set format name",         OFFSET(format_name),  AV_OPT_TYPE_STRING, {.str =  0},  CHAR_MIN, CHAR_MAX, F },
-{"f",            "set format name",         OFFSET(format_name),  AV_OPT_TYPE_STRING, {.str =  0},  CHAR_MIN, CHAR_MAX, F },
-{"streams",      "set streams",             OFFSET(stream_specs), AV_OPT_TYPE_STRING, {.str =  0},  CHAR_MAX, CHAR_MAX, F },
-{"s",            "set streams",             OFFSET(stream_specs), AV_OPT_TYPE_STRING, {.str =  0},  CHAR_MAX, CHAR_MAX, F },
-{"si",           "set stream index",        OFFSET(stream_index), AV_OPT_TYPE_INT,    {.i64 = -1},  -1,       INT_MAX, F },
-{"stream_index", "set stream index",        OFFSET(stream_index), AV_OPT_TYPE_INT,    {.i64 = -1},  -1,       INT_MAX, F },
-{"seek_point",   "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl =  0},  0,        (INT64_MAX-1) / 1000000, F },
-{"sp",           "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, {.dbl =  0},  0,        (INT64_MAX-1) / 1000000, F },
-{"loop",         "set loop count",          OFFSET(loop_count),   AV_OPT_TYPE_INT,    {.i64 =  1},  0,        INT_MAX, F },
-{NULL},
+    { "filename",     NULL,                      OFFSET(file_name),    AV_OPT_TYPE_STRING,                                    .flags = FLAGS },
+    { "format_name",  "set format name",         OFFSET(format_name),  AV_OPT_TYPE_STRING,                                    .flags = FLAGS },
+    { "f",            "set format name",         OFFSET(format_name),  AV_OPT_TYPE_STRING,                                    .flags = FLAGS },
+    { "stream_index", "set stream index",        OFFSET(stream_index), AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, INT_MAX,                 FLAGS  },
+    { "si",           "set stream index",        OFFSET(stream_index), AV_OPT_TYPE_INT,    { .i64 = -1 }, -1, INT_MAX,                 FLAGS  },
+    { "seek_point",   "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl =  0 },  0, (INT64_MAX-1) / 1000000, FLAGS },
+    { "sp",           "set seekpoint (seconds)", OFFSET(seek_point_d), AV_OPT_TYPE_DOUBLE, { .dbl =  0 },  0, (INT64_MAX-1) / 1000000, FLAGS },
+    { "streams",      "set streams",             OFFSET(stream_specs), AV_OPT_TYPE_STRING, {.str =  0},  CHAR_MAX, CHAR_MAX, FLAGS },
+    { "s",            "set streams",             OFFSET(stream_specs), AV_OPT_TYPE_STRING, {.str =  0},  CHAR_MAX, CHAR_MAX, FLAGS },
+    { "loop",         "set loop count",          OFFSET(loop_count),   AV_OPT_TYPE_INT,    {.i64 =  1},  0,        INT_MAX, FLAGS },
+    { NULL },
 };
 
 static int movie_config_output_props(AVFilterLink *outlink);
@@ -153,6 +154,8 @@
         return AVERROR(EINVAL);
     }
 
+    st->st->codec->refcounted_frames = 1;
+
     if ((ret = avcodec_open2(st->st->codec, codec, NULL)) < 0) {
         av_log(log, AV_LOG_ERROR, "Failed to open codec\n");
         return ret;
@@ -184,7 +187,7 @@
     return 0;
 }
 
-static av_cold int movie_common_init(AVFilterContext *ctx, const char *args, const AVClass *class)
+static av_cold int movie_common_init(AVFilterContext *ctx)
 {
     MovieContext *movie = ctx->priv;
     AVInputFormat *iformat = NULL;
@@ -194,22 +197,11 @@
     char name[16];
     AVStream *st;
 
-    movie->class = class;
-    av_opt_set_defaults(movie);
-
-    if (args) {
-        movie->file_name = av_get_token(&args, ":");
-        if (!movie->file_name)
-            return AVERROR(ENOMEM);
-    }
-    if (!args || !*movie->file_name) {
+    if (!*movie->file_name) {
         av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
         return AVERROR(EINVAL);
     }
 
-    if (*args++ == ':' && (ret = av_set_options_string(movie, args, "=", ":")) < 0)
-        return ret;
-
     movie->seek_point = movie->seek_point_d * 1000000 + 0.5;
 
     stream_specs = movie->stream_specs;
@@ -313,11 +305,6 @@
         }
     }
 
-    if (!(movie->frame = avcodec_alloc_frame()) ) {
-        av_log(log, AV_LOG_ERROR, "Failed to alloc frame\n");
-        return AVERROR(ENOMEM);
-    }
-
     av_log(ctx, AV_LOG_VERBOSE, "seek_point:%"PRIi64" format_name:%s file_name:%s stream_index:%d\n",
            movie->seek_point, movie->format_name, movie->file_name,
            movie->stream_index);
@@ -335,11 +322,10 @@
         if (movie->st[i].st)
             avcodec_close(movie->st[i].st->codec);
     }
-    av_opt_free(movie);
     av_freep(&movie->file_name);
     av_freep(&movie->st);
     av_freep(&movie->out_index);
-    avcodec_free_frame(&movie->frame);
+    av_frame_free(&movie->frame);
     if (movie->format_ctx)
         avformat_close_input(&movie->format_ctx);
 }
@@ -399,54 +385,34 @@
     return 0;
 }
 
-static AVFilterBufferRef *frame_to_buf(enum AVMediaType type, AVFrame *frame,
-                                       AVFilterLink *outlink)
-{
-    AVFilterBufferRef *buf, *copy;
-
-    buf = avfilter_get_buffer_ref_from_frame(type, frame,
-                                             AV_PERM_WRITE |
-                                             AV_PERM_PRESERVE |
-                                             AV_PERM_REUSE2);
-    if (!buf)
-        return NULL;
-    buf->pts = av_frame_get_best_effort_timestamp(frame);
-    copy = ff_copy_buffer_ref(outlink, buf);
-    if (!copy)
-        return NULL;
-    buf->buf->data[0] = NULL; /* it belongs to the frame */
-    avfilter_unref_buffer(buf);
-    return copy;
-}
-
-static char *describe_bufref_to_str(char *dst, size_t dst_size,
-                                    AVFilterBufferRef *buf,
+static char *describe_frame_to_str(char *dst, size_t dst_size,
+                                    AVFrame *frame,
                                     AVFilterLink *link)
 {
-    switch (buf->type) {
+    switch (frame->type) {
     case AVMEDIA_TYPE_VIDEO:
         snprintf(dst, dst_size,
-                 "video pts:%s time:%s pos:%"PRId64" size:%dx%d aspect:%d/%d",
-                 av_ts2str(buf->pts), av_ts2timestr(buf->pts, &link->time_base),
-                 buf->pos, buf->video->w, buf->video->h,
-                 buf->video->sample_aspect_ratio.num,
-                 buf->video->sample_aspect_ratio.den);
+                 "video pts:%s time:%s size:%dx%d aspect:%d/%d",
+                 av_ts2str(frame->pts), av_ts2timestr(frame->pts, &link->time_base),
+                 frame->width, frame->height,
+                 frame->sample_aspect_ratio.num,
+                 frame->sample_aspect_ratio.den);
                  break;
     case AVMEDIA_TYPE_AUDIO:
         snprintf(dst, dst_size,
-                 "audio pts:%s time:%s pos:%"PRId64" samples:%d",
-                 av_ts2str(buf->pts), av_ts2timestr(buf->pts, &link->time_base),
-                 buf->pos, buf->audio->nb_samples);
+                 "audio pts:%s time:%s samples:%d",
+                 av_ts2str(frame->pts), av_ts2timestr(frame->pts, &link->time_base),
+                 frame->nb_samples);
                  break;
     default:
-        snprintf(dst, dst_size, "%s BUG", av_get_media_type_string(buf->type));
+        snprintf(dst, dst_size, "%s BUG", av_get_media_type_string(frame->type));
         break;
     }
     return dst;
 }
 
-#define describe_bufref(buf, link) \
-    describe_bufref_to_str((char[1024]){0}, 1024, buf, link)
+#define describe_frameref(f, link) \
+    describe_frame_to_str((char[1024]){0}, 1024, f, link)
 
 static int rewind_file(AVFilterContext *ctx)
 {
@@ -489,7 +455,6 @@
     MovieStream *st;
     int ret, got_frame = 0, pkt_out_id;
     AVFilterLink *outlink;
-    AVFilterBufferRef *buf;
 
     if (!pkt->size) {
         if (movie->eof) {
@@ -532,6 +497,10 @@
     st = &movie->st[pkt_out_id];
     outlink = ctx->outputs[pkt_out_id];
 
+    movie->frame = av_frame_alloc();
+    if (!movie->frame)
+        return AVERROR(ENOMEM);
+
     switch (st->st->codec->codec_type) {
     case AVMEDIA_TYPE_VIDEO:
         ret = avcodec_decode_video2(st->st->codec, movie->frame, &got_frame, pkt);
@@ -545,6 +514,7 @@
     }
     if (ret < 0) {
         av_log(ctx, AV_LOG_WARNING, "Decode error: %s\n", av_err2str(ret));
+        av_frame_free(&movie->frame);
         return 0;
     }
     if (!ret)
@@ -560,24 +530,19 @@
     if (!got_frame) {
         if (!ret)
             st->done = 1;
+        av_frame_free(&movie->frame);
         return 0;
     }
 
-    buf = frame_to_buf(st->st->codec->codec_type, movie->frame, outlink);
-    if (!buf)
-        return AVERROR(ENOMEM);
     av_dlog(ctx, "movie_push_frame(): file:'%s' %s\n", movie->file_name,
-            describe_bufref(buf, outlink));
-    switch (st->st->codec->codec_type) {
-    case AVMEDIA_TYPE_VIDEO:
-        if (!movie->frame->sample_aspect_ratio.num)
-            buf->video->sample_aspect_ratio = st->st->sample_aspect_ratio;
-        /* Fall through */
-    case AVMEDIA_TYPE_AUDIO:
-        ff_filter_frame(outlink, buf);
-        break;
-    }
+            describe_frameref(movie->frame, outlink));
 
+    movie->frame->pts = av_frame_get_best_effort_timestamp(movie->frame);
+    ret = ff_filter_frame(outlink, movie->frame);
+    movie->frame = NULL;
+
+    if (ret < 0)
+        return ret;
     return pkt_out_id == out_id;
 }
 
@@ -598,22 +563,23 @@
 
 AVFILTER_DEFINE_CLASS(movie);
 
-static av_cold int movie_init(AVFilterContext *ctx, const char *args)
+static av_cold int movie_init(AVFilterContext *ctx)
 {
-    return movie_common_init(ctx, args, &movie_class);
+    return movie_common_init(ctx);
 }
 
 AVFilter avfilter_avsrc_movie = {
     .name          = "movie",
     .description   = NULL_IF_CONFIG_SMALL("Read from a movie source."),
     .priv_size     = sizeof(MovieContext),
+    .priv_class    = &movie_class,
     .init          = movie_init,
     .uninit        = movie_uninit,
     .query_formats = movie_query_formats,
 
     .inputs    = NULL,
     .outputs   = NULL,
-    .priv_class = &movie_class,
+    .flags     = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
 };
 
 #endif  /* CONFIG_MOVIE_FILTER */
@@ -623,9 +589,9 @@
 #define amovie_options movie_options
 AVFILTER_DEFINE_CLASS(amovie);
 
-static av_cold int amovie_init(AVFilterContext *ctx, const char *args)
+static av_cold int amovie_init(AVFilterContext *ctx)
 {
-    return movie_common_init(ctx, args, &amovie_class);
+    return movie_common_init(ctx);
 }
 
 AVFilter avfilter_avsrc_amovie = {
@@ -639,6 +605,7 @@
     .inputs     = NULL,
     .outputs    = NULL,
     .priv_class = &amovie_class,
+    .flags      = AVFILTER_FLAG_DYNAMIC_OUTPUTS,
 };
 
 #endif /* CONFIG_AMOVIE_FILTER */
diff --git a/libavfilter/version.h b/libavfilter/version.h
index 93bdf91..9e8e83a 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -29,8 +29,8 @@
 #include "libavutil/avutil.h"
 
 #define LIBAVFILTER_VERSION_MAJOR  3
-#define LIBAVFILTER_VERSION_MINOR  41
-#define LIBAVFILTER_VERSION_MICRO 100
+#define LIBAVFILTER_VERSION_MINOR  60
+#define LIBAVFILTER_VERSION_MICRO 101
 
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
                                                LIBAVFILTER_VERSION_MINOR, \
@@ -60,5 +60,23 @@
 #ifndef FF_API_BUFFERSRC_BUFFER
 #define FF_API_BUFFERSRC_BUFFER             (LIBAVFILTER_VERSION_MAJOR < 4)
 #endif
+#ifndef FF_API_AVFILTERBUFFER
+#define FF_API_AVFILTERBUFFER               (LIBAVFILTER_VERSION_MAJOR < 4)
+#endif
+#ifndef FF_API_OLD_FILTER_OPTS
+#define FF_API_OLD_FILTER_OPTS              (LIBAVFILTER_VERSION_MAJOR < 4)
+#endif
+#ifndef FF_API_ACONVERT_FILTER
+#define FF_API_ACONVERT_FILTER              (LIBAVFILTER_VERSION_MAJOR < 4)
+#endif
+#ifndef FF_API_AVFILTER_OPEN
+#define FF_API_AVFILTER_OPEN                (LIBAVFILTER_VERSION_MAJOR < 4)
+#endif
+#ifndef FF_API_AVFILTER_INIT_FILTER
+#define FF_API_AVFILTER_INIT_FILTER         (LIBAVFILTER_VERSION_MAJOR < 4)
+#endif
+#ifndef FF_API_OLD_FILTER_REGISTER
+#define FF_API_OLD_FILTER_REGISTER          (LIBAVFILTER_VERSION_MAJOR < 4)
+#endif
 
 #endif /* AVFILTER_VERSION_H */
diff --git a/libavfilter/vf_alphaextract.c b/libavfilter/vf_alphaextract.c
index 45d3dd4..62ceecf 100644
--- a/libavfilter/vf_alphaextract.c
+++ b/libavfilter/vf_alphaextract.c
@@ -60,19 +60,18 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *cur_buf)
+static int filter_frame(AVFilterLink *inlink, AVFrame *cur_buf)
 {
     AlphaExtractContext *extract = inlink->dst->priv;
     AVFilterLink *outlink = inlink->dst->outputs[0];
-    AVFilterBufferRef *out_buf =
-        ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+    AVFrame *out_buf = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     int ret;
 
     if (!out_buf) {
         ret = AVERROR(ENOMEM);
         goto end;
     }
-    avfilter_copy_buffer_ref_props(out_buf, cur_buf);
+    av_frame_copy_props(out_buf, cur_buf);
 
     if (extract->is_packed_rgb) {
         int x, y;
@@ -99,7 +98,7 @@
     ret = ff_filter_frame(outlink, out_buf);
 
 end:
-    avfilter_unref_buffer(cur_buf);
+    av_frame_free(&cur_buf);
     return ret;
 }
 
@@ -109,7 +108,6 @@
         .type         = AVMEDIA_TYPE_VIDEO,
         .config_props = config_input,
         .filter_frame = filter_frame,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL }
 };
diff --git a/libavfilter/vf_alphamerge.c b/libavfilter/vf_alphamerge.c
index 99fd61e..644c7f4 100644
--- a/libavfilter/vf_alphamerge.c
+++ b/libavfilter/vf_alphamerge.c
@@ -96,11 +96,11 @@
 }
 
 static void draw_frame(AVFilterContext *ctx,
-                       AVFilterBufferRef *main_buf,
-                       AVFilterBufferRef *alpha_buf)
+                       AVFrame *main_buf,
+                       AVFrame *alpha_buf)
 {
     AlphaMergeContext *merge = ctx->priv;
-    int h = main_buf->video->h;
+    int h = main_buf->height;
 
     if (merge->is_packed_rgb) {
         int x, y;
@@ -108,7 +108,7 @@
         for (y = 0; y < h; y++) {
             pin = alpha_buf->data[0] + y * alpha_buf->linesize[0];
             pout = main_buf->data[0] + y * main_buf->linesize[0] + merge->rgba_map[A];
-            for (x = 0; x < main_buf->video->w; x++) {
+            for (x = 0; x < main_buf->width; x++) {
                 *pout = *pin;
                 pin += 1;
                 pout += 4;
@@ -118,7 +118,7 @@
         int y;
         const int main_linesize = main_buf->linesize[A];
         const int alpha_linesize = alpha_buf->linesize[Y];
-        for (y = 0; y < h && y < alpha_buf->video->h; y++) {
+        for (y = 0; y < h && y < alpha_buf->height; y++) {
             memcpy(main_buf->data[A] + y * main_linesize,
                    alpha_buf->data[Y] + y * alpha_linesize,
                    FFMIN(main_linesize, alpha_linesize));
@@ -126,18 +126,19 @@
     }
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
 {
     AVFilterContext *ctx = inlink->dst;
     AlphaMergeContext *merge = ctx->priv;
 
+    int ret = 0;
     int is_alpha = (inlink == ctx->inputs[1]);
     struct FFBufQueue *queue =
         (is_alpha ? &merge->queue_alpha : &merge->queue_main);
     ff_bufqueue_add(ctx, queue, buf);
 
-    while (1) {
-        AVFilterBufferRef *main_buf, *alpha_buf;
+    do {
+        AVFrame *main_buf, *alpha_buf;
 
         if (!ff_bufqueue_peek(&merge->queue_main, 0) ||
             !ff_bufqueue_peek(&merge->queue_alpha, 0)) break;
@@ -147,10 +148,10 @@
 
         merge->frame_requested = 0;
         draw_frame(ctx, main_buf, alpha_buf);
-        ff_filter_frame(ctx->outputs[0], main_buf);
-        avfilter_unref_buffer(alpha_buf);
-    }
-    return 0;
+        ret = ff_filter_frame(ctx->outputs[0], main_buf);
+        av_frame_free(&alpha_buf);
+    } while (ret >= 0);
+    return ret;
 }
 
 static int request_frame(AVFilterLink *outlink)
@@ -176,12 +177,11 @@
         .config_props     = config_input_main,
         .get_video_buffer = ff_null_get_video_buffer,
         .filter_frame     = filter_frame,
-        .min_perms        = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE,
+        .needs_writable   = 1,
     },{
         .name             = "alpha",
         .type             = AVMEDIA_TYPE_VIDEO,
         .filter_frame     = filter_frame,
-        .min_perms        = AV_PERM_READ | AV_PERM_PRESERVE,
     },
     { NULL }
 };
diff --git a/libavfilter/vf_aspect.c b/libavfilter/vf_aspect.c
index 8e19162..25dde7f 100644
--- a/libavfilter/vf_aspect.c
+++ b/libavfilter/vf_aspect.c
@@ -1,6 +1,6 @@
 /*
  * Copyright (c) 2010 Bobby Bingham
-
+ *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
@@ -23,105 +23,117 @@
  * aspect ratio modification video filters
  */
 
+#include <float.h>
+
 #include "libavutil/common.h"
-#include "libavutil/opt.h"
+#include "libavutil/eval.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
+
 #include "avfilter.h"
 #include "internal.h"
 #include "video.h"
 
 typedef struct {
     const AVClass *class;
-    AVRational ratio;
-    char *ratio_str;
+    AVRational aspect;
     int max;
+#if FF_API_OLD_FILTER_OPTS
+    float aspect_den;
+#endif
+    char *ratio_str;
 } AspectContext;
 
+static av_cold int init(AVFilterContext *ctx)
+{
+    AspectContext *s = ctx->priv;
+    int ret;
+
+#if FF_API_OLD_FILTER_OPTS
+    if (s->ratio_str && s->aspect_den > 0) {
+        double num;
+        av_log(ctx, AV_LOG_WARNING,
+               "num:den syntax is deprecated, please use num/den or named options instead\n");
+        ret = av_expr_parse_and_eval(&num, s->ratio_str, NULL, NULL,
+                                     NULL, NULL, NULL, NULL, NULL, 0, ctx);
+        if (ret < 0) {
+            av_log(ctx, AV_LOG_ERROR, "Unable to parse ratio numerator \"%s\"\n", s->ratio_str);
+            return AVERROR(EINVAL);
+        }
+        s->aspect = av_d2q(num / s->aspect_den, s->max);
+    } else
+#endif
+    if (s->ratio_str) {
+        ret = av_parse_ratio(&s->aspect, s->ratio_str, s->max, 0, ctx);
+        if (ret < 0 || s->aspect.num < 0 || s->aspect.den <= 0) {
+            av_log(ctx, AV_LOG_ERROR,
+                   "Invalid string '%s' for aspect ratio\n", s->ratio_str);
+            return AVERROR(EINVAL);
+        }
+    }
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *link, AVFrame *frame)
+{
+    AspectContext *aspect = link->dst->priv;
+
+    frame->sample_aspect_ratio = aspect->aspect;
+    return ff_filter_frame(link->dst->outputs[0], frame);
+}
+
 #define OFFSET(x) offsetof(AspectContext, x)
 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
-static const AVOption options[] = {
-    {"max", "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
-    {"ratio", "set ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
-    {"r",     "set ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, FLAGS },
-    {NULL}
-};
-
-static av_cold int init(AVFilterContext *ctx, const char *args, const AVClass *class)
+static inline void compute_dar(AVRational *dar, AVRational sar, int w, int h)
 {
-    AspectContext *aspect = ctx->priv;
-    static const char *shorthand[] = { "ratio", "max", NULL };
-    char c;
-    int ret;
-    AVRational q;
-
-    aspect->class = class;
-    av_opt_set_defaults(aspect);
-
-    if (args && sscanf(args, "%d:%d%c", &q.num, &q.den, &c) == 2) {
-        aspect->ratio_str = av_strdup(args);
-        av_log(ctx, AV_LOG_WARNING,
-               "num:den syntax is deprecated, please use num/den or named options instead\n");
-    } else if ((ret = av_opt_set_from_string(aspect, args, shorthand, "=", ":")) < 0) {
-        return ret;
+    if (sar.num && sar.den) {
+        av_reduce(&dar->num, &dar->den, sar.num * w, sar.den * h, INT_MAX);
+    } else {
+        av_reduce(&dar->num, &dar->den, w, h, INT_MAX);
     }
-
-    if (aspect->ratio_str) {
-        ret = av_parse_ratio(&aspect->ratio, aspect->ratio_str, aspect->max, 0, ctx);
-        if (ret < 0 || aspect->ratio.num < 0 || aspect->ratio.den <= 0) {
-            av_log(ctx, AV_LOG_ERROR,
-                   "Invalid string '%s' for aspect ratio\n", args);
-            return AVERROR(EINVAL);
-        }
-    }
-
-    av_log(ctx, AV_LOG_VERBOSE, "a:%d/%d\n", aspect->ratio.num, aspect->ratio.den);
-    return 0;
-}
-
-static int filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
-{
-    AspectContext *aspect = link->dst->priv;
-
-    frame->video->sample_aspect_ratio = aspect->ratio;
-    return ff_filter_frame(link->dst->outputs[0], frame);
-}
-
-static av_cold void uninit(AVFilterContext *ctx)
-{
-    AspectContext *aspect = ctx->priv;
-
-    av_opt_free(aspect);
 }
 
 #if CONFIG_SETDAR_FILTER
 
-#define setdar_options options
-AVFILTER_DEFINE_CLASS(setdar);
-
-static av_cold int setdar_init(AVFilterContext *ctx, const char *args)
-{
-    return init(ctx, args, &setdar_class);
-}
-
 static int setdar_config_props(AVFilterLink *inlink)
 {
     AspectContext *aspect = inlink->dst->priv;
-    AVRational dar = aspect->ratio;
+    AVRational dar = aspect->aspect, old_dar;
+    AVRational old_sar = inlink->sample_aspect_ratio;
 
-    av_reduce(&aspect->ratio.num, &aspect->ratio.den,
-               aspect->ratio.num * inlink->h,
-               aspect->ratio.den * inlink->w, 100);
+    if (aspect->aspect.num && aspect->aspect.den) {
+        av_reduce(&aspect->aspect.num, &aspect->aspect.den,
+                   aspect->aspect.num * inlink->h,
+                   aspect->aspect.den * inlink->w, INT_MAX);
+        inlink->sample_aspect_ratio = aspect->aspect;
+    } else {
+        inlink->sample_aspect_ratio = (AVRational){ 1, 1 };
+        dar = (AVRational){ inlink->w, inlink->h };
+    }
 
-    av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d -> dar:%d/%d sar:%d/%d\n",
-           inlink->w, inlink->h, dar.num, dar.den, aspect->ratio.num, aspect->ratio.den);
-
-    inlink->sample_aspect_ratio = aspect->ratio;
+    compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
+    av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d dar:%d/%d sar:%d/%d -> dar:%d/%d sar:%d/%d\n",
+           inlink->w, inlink->h, old_dar.num, old_dar.den, old_sar.num, old_sar.den,
+           dar.num, dar.den, inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den);
 
     return 0;
 }
 
+static const AVOption setdar_options[] = {
+    { "dar",   "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
+    { "ratio", "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
+    { "r",     "set display aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
+#if FF_API_OLD_FILTER_OPTS
+    { "dar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
+#endif
+    { "max",   "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(setdar);
+
 static const AVFilterPad avfilter_vf_setdar_inputs[] = {
     {
         .name             = "default",
@@ -144,39 +156,49 @@
 AVFilter avfilter_vf_setdar = {
     .name      = "setdar",
     .description = NULL_IF_CONFIG_SMALL("Set the frame display aspect ratio."),
-
-    .init      = setdar_init,
-    .uninit    = uninit,
-
+    .init      = init,
     .priv_size = sizeof(AspectContext),
+    .priv_class = &setdar_class,
 
     .inputs    = avfilter_vf_setdar_inputs,
 
     .outputs   = avfilter_vf_setdar_outputs,
-    .priv_class = &setdar_class,
 };
 
 #endif /* CONFIG_SETDAR_FILTER */
 
 #if CONFIG_SETSAR_FILTER
 
-#define setsar_options options
-AVFILTER_DEFINE_CLASS(setsar);
-
-static av_cold int setsar_init(AVFilterContext *ctx, const char *args)
-{
-    return init(ctx, args, &setsar_class);
-}
-
 static int setsar_config_props(AVFilterLink *inlink)
 {
     AspectContext *aspect = inlink->dst->priv;
+    AVRational old_sar = inlink->sample_aspect_ratio;
+    AVRational old_dar, dar;
 
-    inlink->sample_aspect_ratio = aspect->ratio;
+    inlink->sample_aspect_ratio = aspect->aspect;
+
+    compute_dar(&old_dar, old_sar, inlink->w, inlink->h);
+    compute_dar(&dar, aspect->aspect, inlink->w, inlink->h);
+    av_log(inlink->dst, AV_LOG_VERBOSE, "w:%d h:%d sar:%d/%d dar:%d/%d -> sar:%d/%d dar:%d/%d\n",
+           inlink->w, inlink->h, old_sar.num, old_sar.den, old_dar.num, old_dar.den,
+           inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den, dar.num, dar.den);
 
     return 0;
 }
 
+static const AVOption setsar_options[] = {
+    { "sar",   "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
+    { "ratio", "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
+    { "r",     "set sample (pixel) aspect ratio", OFFSET(ratio_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags=FLAGS },
+#if FF_API_OLD_FILTER_OPTS
+    { "sar_den", NULL, OFFSET(aspect_den), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, FLT_MAX, FLAGS },
+#endif
+    { "max",   "set max value for nominator or denominator in the ratio", OFFSET(max), AV_OPT_TYPE_INT, {.i64=100}, 1, INT_MAX, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(setsar);
+
 static const AVFilterPad avfilter_vf_setsar_inputs[] = {
     {
         .name             = "default",
@@ -199,16 +221,13 @@
 AVFilter avfilter_vf_setsar = {
     .name      = "setsar",
     .description = NULL_IF_CONFIG_SMALL("Set the pixel sample aspect ratio."),
-
-    .init      = setsar_init,
-    .uninit    = uninit,
-
+    .init      = init,
     .priv_size = sizeof(AspectContext),
+    .priv_class = &setsar_class,
 
     .inputs    = avfilter_vf_setsar_inputs,
 
     .outputs   = avfilter_vf_setsar_outputs,
-    .priv_class = &setsar_class,
 };
 
 #endif /* CONFIG_SETSAR_FILTER */
diff --git a/libavfilter/vf_bbox.c b/libavfilter/vf_bbox.c
index 33b96b5..6b4cdd0 100644
--- a/libavfilter/vf_bbox.c
+++ b/libavfilter/vf_bbox.c
@@ -31,16 +31,8 @@
 
 typedef struct {
     unsigned int frame;
-    int vsub, hsub;
 } BBoxContext;
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    BBoxContext *bbox = ctx->priv;
-    bbox->frame = 0;
-    return 0;
-}
-
 static int query_formats(AVFilterContext *ctx)
 {
     static const enum AVPixelFormat pix_fmts[] = {
@@ -56,7 +48,7 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
 {
     AVFilterContext *ctx = inlink->dst;
     BBoxContext *bbox = ctx->priv;
@@ -65,14 +57,14 @@
 
     has_bbox =
         ff_calculate_bounding_box(&box,
-                                  picref->data[0], picref->linesize[0],
+                                  frame->data[0], frame->linesize[0],
                                   inlink->w, inlink->h, 16);
     w = box.x2 - box.x1 + 1;
     h = box.y2 - box.y1 + 1;
 
     av_log(ctx, AV_LOG_INFO,
            "n:%d pts:%s pts_time:%s", bbox->frame,
-           av_ts2str(picref->pts), av_ts2timestr(picref->pts, &inlink->time_base));
+           av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base));
 
     if (has_bbox) {
         av_log(ctx, AV_LOG_INFO,
@@ -85,16 +77,14 @@
     av_log(ctx, AV_LOG_INFO, "\n");
 
     bbox->frame++;
-    return ff_filter_frame(inlink->dst->outputs[0], picref);
+    return ff_filter_frame(inlink->dst->outputs[0], frame);
 }
 
 static const AVFilterPad bbox_inputs[] = {
     {
         .name             = "default",
         .type             = AVMEDIA_TYPE_VIDEO,
-        .get_video_buffer = ff_null_get_video_buffer,
         .filter_frame     = filter_frame,
-        .min_perms        = AV_PERM_READ,
     },
     { NULL }
 };
@@ -112,7 +102,6 @@
     .description   = NULL_IF_CONFIG_SMALL("Compute bounding box for each frame."),
     .priv_size     = sizeof(BBoxContext),
     .query_formats = query_formats,
-    .init          = init,
     .inputs        = bbox_inputs,
     .outputs       = bbox_outputs,
 };
diff --git a/libavfilter/vf_blackdetect.c b/libavfilter/vf_blackdetect.c
index f2f2cfa..ddbf082 100644
--- a/libavfilter/vf_blackdetect.c
+++ b/libavfilter/vf_blackdetect.c
@@ -43,7 +43,6 @@
     double       pixel_black_th;
     unsigned int pixel_black_th_i;
 
-    unsigned int frame_count;       ///< frame number
     unsigned int nb_black_pixels;   ///< number of black pixels counted so far
 } BlackDetectContext;
 
@@ -82,20 +81,6 @@
     return 0;
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    int ret;
-    BlackDetectContext *blackdetect = ctx->priv;
-
-    blackdetect->class = &blackdetect_class;
-    av_opt_set_defaults(blackdetect);
-
-    if ((ret = av_set_options_string(blackdetect, args, "=", ":")) < 0)
-        return ret;
-
-    return 0;
-}
-
 static int config_input(AVFilterLink *inlink)
 {
     AVFilterContext *ctx = inlink->dst;
@@ -146,7 +131,7 @@
     return ret;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
+static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
 {
     AVFilterContext *ctx = inlink->dst;
     BlackDetectContext *blackdetect = ctx->priv;
@@ -163,10 +148,10 @@
     picture_black_ratio = (double)blackdetect->nb_black_pixels / (inlink->w * inlink->h);
 
     av_log(ctx, AV_LOG_DEBUG,
-           "frame:%u picture_black_ratio:%f pos:%"PRId64" pts:%s t:%s type:%c\n",
-           blackdetect->frame_count, picture_black_ratio,
-           picref->pos, av_ts2str(picref->pts), av_ts2timestr(picref->pts, &inlink->time_base),
-           av_get_picture_type_char(picref->video->pict_type));
+           "frame:%"PRId64" picture_black_ratio:%f pts:%s t:%s type:%c\n",
+           inlink->frame_count, picture_black_ratio,
+           av_ts2str(picref->pts), av_ts2timestr(picref->pts, &inlink->time_base),
+           av_get_picture_type_char(picref->pict_type));
 
     if (picture_black_ratio >= blackdetect->picture_black_ratio_th) {
         if (!blackdetect->black_started) {
@@ -182,7 +167,6 @@
     }
 
     blackdetect->last_picref_pts = picref->pts;
-    blackdetect->frame_count++;
     blackdetect->nb_black_pixels = 0;
     return ff_filter_frame(inlink->dst->outputs[0], picref);
 }
@@ -211,7 +195,6 @@
     .name          = "blackdetect",
     .description   = NULL_IF_CONFIG_SMALL("Detect video intervals that are (almost) black."),
     .priv_size     = sizeof(BlackDetectContext),
-    .init          = init,
     .query_formats = query_formats,
     .inputs        = blackdetect_inputs,
     .outputs       = blackdetect_outputs,
diff --git a/libavfilter/vf_blackframe.c b/libavfilter/vf_blackframe.c
index cf1bcd3..7b38fcb 100644
--- a/libavfilter/vf_blackframe.c
+++ b/libavfilter/vf_blackframe.c
@@ -31,6 +31,7 @@
 #include <inttypes.h>
 
 #include "libavutil/internal.h"
+#include "libavutil/opt.h"
 #include "avfilter.h"
 #include "internal.h"
 #include "formats.h"
@@ -38,8 +39,9 @@
 #include "video.h"
 
 typedef struct {
-    unsigned int bamount; ///< black amount
-    unsigned int bthresh; ///< black threshold
+    const AVClass *class;
+    int bamount;          ///< black amount
+    int bthresh;          ///< black threshold
     unsigned int frame;   ///< frame number
     unsigned int nblack;  ///< number of black pixels counted so far
     unsigned int last_keyframe; ///< frame number of the last received key-frame
@@ -57,31 +59,7 @@
     return 0;
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    BlackFrameContext *blackframe = ctx->priv;
-
-    blackframe->bamount = 98;
-    blackframe->bthresh = 32;
-    blackframe->nblack = 0;
-    blackframe->frame = 0;
-    blackframe->last_keyframe = 0;
-
-    if (args)
-        sscanf(args, "%u:%u", &blackframe->bamount, &blackframe->bthresh);
-
-    av_log(ctx, AV_LOG_VERBOSE, "bamount:%u bthresh:%u\n",
-           blackframe->bamount, blackframe->bthresh);
-
-    if (blackframe->bamount > 100 || blackframe->bthresh > 255) {
-        av_log(ctx, AV_LOG_ERROR, "Too big value for bamount (max is 100) or bthresh (max is 255)\n");
-        return AVERROR(EINVAL);
-    }
-
-    return 0;
-}
-
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
 {
     AVFilterContext *ctx = inlink->dst;
     BlackFrameContext *blackframe = ctx->priv;
@@ -89,28 +67,42 @@
     int pblack = 0;
     uint8_t *p = frame->data[0];
 
-    for (i = 0; i < frame->video->h; i++) {
+    for (i = 0; i < frame->height; i++) {
         for (x = 0; x < inlink->w; x++)
             blackframe->nblack += p[x] < blackframe->bthresh;
         p += frame->linesize[0];
     }
 
-    if (frame->video->key_frame)
+    if (frame->key_frame)
         blackframe->last_keyframe = blackframe->frame;
 
     pblack = blackframe->nblack * 100 / (inlink->w * inlink->h);
     if (pblack >= blackframe->bamount)
-        av_log(ctx, AV_LOG_INFO, "frame:%u pblack:%u pos:%"PRId64" pts:%"PRId64" t:%f "
+        av_log(ctx, AV_LOG_INFO, "frame:%u pblack:%u pts:%"PRId64" t:%f "
                "type:%c last_keyframe:%d\n",
-               blackframe->frame, pblack, frame->pos, frame->pts,
+               blackframe->frame, pblack, frame->pts,
                frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
-               av_get_picture_type_char(frame->video->pict_type), blackframe->last_keyframe);
+               av_get_picture_type_char(frame->pict_type), blackframe->last_keyframe);
 
     blackframe->frame++;
     blackframe->nblack = 0;
     return ff_filter_frame(inlink->dst->outputs[0], frame);
 }
 
+#define OFFSET(x) offsetof(BlackFrameContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+static const AVOption blackframe_options[] = {
+    { "amount", "Percentage of the pixels that have to be below the threshold "
+        "for the frame to be considered black.", OFFSET(bamount), AV_OPT_TYPE_INT, { .i64 = 98 }, 0, 100,     FLAGS },
+    { "threshold", "threshold below which a pixel value is considered black",
+                                                 OFFSET(bthresh), AV_OPT_TYPE_INT, { .i64 = 32 }, 0, 255,     FLAGS },
+    { "thresh", "threshold below which a pixel value is considered black",
+                                                 OFFSET(bthresh), AV_OPT_TYPE_INT, { .i64 = 32 }, 0, 255,     FLAGS },
+    { NULL },
+};
+
+AVFILTER_DEFINE_CLASS(blackframe);
+
 static const AVFilterPad avfilter_vf_blackframe_inputs[] = {
     {
         .name             = "default",
@@ -134,7 +126,7 @@
     .description = NULL_IF_CONFIG_SMALL("Detect frames that are (almost) black."),
 
     .priv_size = sizeof(BlackFrameContext),
-    .init      = init,
+    .priv_class = &blackframe_class,
 
     .query_formats = query_formats,
 
diff --git a/libavfilter/vf_blend.c b/libavfilter/vf_blend.c
index 27d94b4..93b68be 100644
--- a/libavfilter/vf_blend.c
+++ b/libavfilter/vf_blend.c
@@ -60,8 +60,8 @@
     BLEND_NB
 };
 
-static const char *const var_names[] = {   "X",   "Y",   "W",   "H",   "SW",   "SH",   "T",     "A",        "B",   "TOP",   "BOTTOM",        NULL };
-enum                                   { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_SW, VAR_SH, VAR_T,   VAR_A,      VAR_B, VAR_TOP, VAR_BOTTOM, VAR_VARS_NB };
+static const char *const var_names[] = {   "X",   "Y",   "W",   "H",   "SW",   "SH",   "T",   "N",   "A",   "B",   "TOP",   "BOTTOM",        NULL };
+enum                                   { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_SW, VAR_SH, VAR_T, VAR_N, VAR_A, VAR_B, VAR_TOP, VAR_BOTTOM, VAR_VARS_NB };
 
 typedef struct FilterParams {
     enum BlendMode mode;
@@ -219,17 +219,11 @@
     }
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     BlendContext *b = ctx->priv;
     int ret, plane;
 
-    b->class = &blend_class;
-    av_opt_set_defaults(b);
-
-    if ((ret = av_set_options_string(b, args, "=", ":")) < 0)
-        return ret;
-
     for (plane = 0; plane < FF_ARRAY_ELEMS(b->params); plane++) {
         FilterParams *param = &b->params[plane];
 
@@ -286,7 +280,8 @@
 {
     static const enum AVPixelFormat pix_fmts[] = {
         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
-        AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
+        AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P,
+        AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV410P,
         AV_PIX_FMT_GBRP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
     };
 
@@ -343,7 +338,6 @@
     BlendContext *b = ctx->priv;
     int i;
 
-    av_opt_free(b);
     ff_bufqueue_discard_all(&b->queue_top);
     ff_bufqueue_discard_all(&b->queue_bottom);
 
@@ -359,7 +353,7 @@
 
     b->frame_requested = 1;
     while (b->frame_requested) {
-        in = ff_bufqueue_peek(&b->queue_top, TOP) ? BOTTOM : TOP;
+        in = ff_bufqueue_peek(&b->queue_top, 0) ? BOTTOM : TOP;
         ret = ff_request_frame(ctx->inputs[in]);
         if (ret < 0)
             return ret;
@@ -368,9 +362,9 @@
 }
 
 static void blend_frame(AVFilterContext *ctx,
-                        AVFilterBufferRef *top_buf,
-                        AVFilterBufferRef *bottom_buf,
-                        AVFilterBufferRef *dst_buf)
+                        AVFrame *top_buf,
+                        AVFrame *bottom_buf,
+                        AVFrame *dst_buf)
 {
     BlendContext *b = ctx->priv;
     AVFilterLink *inlink = ctx->inputs[0];
@@ -380,25 +374,26 @@
     for (plane = 0; dst_buf->data[plane]; plane++) {
         int hsub = plane == 1 || plane == 2 ? b->hsub : 0;
         int vsub = plane == 1 || plane == 2 ? b->vsub : 0;
-        int outw = dst_buf->video->w >> hsub;
-        int outh = dst_buf->video->h >> vsub;
+        int outw = dst_buf->width  >> hsub;
+        int outh = dst_buf->height >> vsub;
         uint8_t *dst    = dst_buf->data[plane];
         uint8_t *top    = top_buf->data[plane];
         uint8_t *bottom = bottom_buf->data[plane];
 
         param = &b->params[plane];
+        param->values[VAR_N]  = inlink->frame_count;
         param->values[VAR_T]  = dst_buf->pts == AV_NOPTS_VALUE ? NAN : dst_buf->pts * av_q2d(inlink->time_base);
         param->values[VAR_W]  = outw;
         param->values[VAR_H]  = outh;
-        param->values[VAR_SW] = outw / dst_buf->video->w;
-        param->values[VAR_SH] = outh / dst_buf->video->h;
+        param->values[VAR_SW] = outw / dst_buf->width;
+        param->values[VAR_SH] = outh / dst_buf->height;
         param->blend(top, top_buf->linesize[plane],
                      bottom, bottom_buf->linesize[plane],
                      dst, dst_buf->linesize[plane], outw, outh, param);
     }
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
 {
     AVFilterContext *ctx = inlink->dst;
     AVFilterLink *outlink = ctx->outputs[0];
@@ -411,26 +406,25 @@
     ff_bufqueue_add(ctx, queue, buf);
 
     while (1) {
-        AVFilterBufferRef *top_buf, *bottom_buf, *out_buf;
+        AVFrame *top_buf, *bottom_buf, *out_buf;
 
-        if (!ff_bufqueue_peek(&b->queue_top, TOP) ||
-            !ff_bufqueue_peek(&b->queue_bottom, BOTTOM)) break;
+        if (!ff_bufqueue_peek(&b->queue_top, 0) ||
+            !ff_bufqueue_peek(&b->queue_bottom, 0)) break;
 
         top_buf = ff_bufqueue_get(&b->queue_top);
         bottom_buf = ff_bufqueue_get(&b->queue_bottom);
 
-        out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE,
-                                      outlink->w, outlink->h);
+        out_buf = ff_get_video_buffer(outlink, outlink->w, outlink->h);
         if (!out_buf) {
             return AVERROR(ENOMEM);
         }
-        avfilter_copy_buffer_ref_props(out_buf, top_buf);
+        av_frame_copy_props(out_buf, top_buf);
 
         b->frame_requested = 0;
         blend_frame(ctx, top_buf, bottom_buf, out_buf);
         ret = ff_filter_frame(ctx->outputs[0], out_buf);
-        avfilter_unref_buffer(top_buf);
-        avfilter_unref_buffer(bottom_buf);
+        av_frame_free(&top_buf);
+        av_frame_free(&bottom_buf);
     }
     return ret;
 }
@@ -441,12 +435,10 @@
         .type             = AVMEDIA_TYPE_VIDEO,
         .config_props     = config_input_top,
         .filter_frame     = filter_frame,
-        .min_perms        = AV_PERM_READ | AV_PERM_PRESERVE,
     },{
         .name             = "bottom",
         .type             = AVMEDIA_TYPE_VIDEO,
         .filter_frame     = filter_frame,
-        .min_perms        = AV_PERM_READ | AV_PERM_PRESERVE,
     },
     { NULL }
 };
diff --git a/libavfilter/vf_boxblur.c b/libavfilter/vf_boxblur.c
index a4ac50a..b010670 100644
--- a/libavfilter/vf_boxblur.c
+++ b/libavfilter/vf_boxblur.c
@@ -73,51 +73,19 @@
     uint8_t *temp[2]; ///< temporary buffer used in blur_power()
 } BoxBlurContext;
 
-#define OFFSET(x) offsetof(BoxBlurContext, x)
-#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
-
-static const AVOption boxblur_options[] = {
-    { "luma_radius", "set luma radius", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
-    { "lr",          "set luma radius", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
-    { "luma_power",  "set luma power",  OFFSET(luma_param.power), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, .flags = FLAGS },
-    { "lp",          "set luma power",  OFFSET(luma_param.power), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, .flags = FLAGS },
-
-    { "chroma_radius", "set chroma radius", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
-    { "cr",            "set chroma radius", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
-    { "chroma_power",  "set chroma power",  OFFSET(chroma_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
-    { "cp",            "set chroma power",  OFFSET(chroma_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
-
-    { "alpha_radius", "set alpha radius", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
-    { "ar",           "set alpha radius", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
-    { "alpha_power",  "set alpha power",  OFFSET(alpha_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
-    { "ap",           "set alpha power",  OFFSET(alpha_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
-
-    { NULL }
-};
-
-AVFILTER_DEFINE_CLASS(boxblur);
-
 #define Y 0
 #define U 1
 #define V 2
 #define A 3
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     BoxBlurContext *boxblur = ctx->priv;
-    static const char *shorthand[] = {
-        "luma_radius",   "luma_power",
-        "chroma_radius", "chroma_power",
-        "alpha_radius",  "alpha_power",
-        NULL
-    };
-    int ret;
 
-    boxblur->class = &boxblur_class;
-    av_opt_set_defaults(boxblur);
-
-    if ((ret = av_opt_set_from_string(boxblur, args, shorthand, "=", ":")) < 0)
-        return ret;
+    if (!boxblur->luma_param.radius_expr) {
+        av_log(ctx, AV_LOG_ERROR, "Luma radius expression is not set.\n");
+        return AVERROR(EINVAL);
+    }
 
     /* fill missing params */
     if (!boxblur->chroma_param.radius_expr) {
@@ -145,7 +113,6 @@
 
     av_freep(&boxblur->temp[0]);
     av_freep(&boxblur->temp[1]);
-    av_opt_free(boxblur);
 }
 
 static int query_formats(AVFilterContext *ctx)
@@ -328,23 +295,23 @@
                    h, radius, power, temp);
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     AVFilterContext *ctx = inlink->dst;
     BoxBlurContext *boxblur = ctx->priv;
     AVFilterLink *outlink = inlink->dst->outputs[0];
-    AVFilterBufferRef *out;
+    AVFrame *out;
     int plane;
-    int cw = inlink->w >> boxblur->hsub, ch = in->video->h >> boxblur->vsub;
+    int cw = inlink->w >> boxblur->hsub, ch = in->height >> boxblur->vsub;
     int w[4] = { inlink->w, cw, cw, inlink->w };
-    int h[4] = { in->video->h, ch, ch, in->video->h };
+    int h[4] = { in->height, ch, ch, in->height };
 
-    out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!out) {
-        avfilter_unref_bufferp(&in);
+        av_frame_free(&in);
         return AVERROR(ENOMEM);
     }
-    avfilter_copy_buffer_ref_props(out, in);
+    av_frame_copy_props(out, in);
 
     for (plane = 0; in->data[plane] && plane < 4; plane++)
         hblur(out->data[plane], out->linesize[plane],
@@ -358,18 +325,41 @@
               w[plane], h[plane], boxblur->radius[plane], boxblur->power[plane],
               boxblur->temp);
 
-    avfilter_unref_bufferp(&in);
+    av_frame_free(&in);
 
     return ff_filter_frame(outlink, out);
 }
 
+#define OFFSET(x) offsetof(BoxBlurContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption boxblur_options[] = {
+    { "luma_radius", "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
+    { "lr",          "Radius of the luma blurring box", OFFSET(luma_param.radius_expr), AV_OPT_TYPE_STRING, {.str="2"}, .flags = FLAGS },
+    { "luma_power",  "How many times should the boxblur be applied to luma",  OFFSET(luma_param.power), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, .flags = FLAGS },
+    { "lp",          "How many times should the boxblur be applied to luma",  OFFSET(luma_param.power), AV_OPT_TYPE_INT, {.i64=2}, 0, INT_MAX, .flags = FLAGS },
+
+    { "chroma_radius", "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
+    { "cr",            "Radius of the chroma blurring box", OFFSET(chroma_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
+    { "chroma_power",  "How many times should the boxblur be applied to chroma",  OFFSET(chroma_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
+    { "cp",            "How many times should the boxblur be applied to chroma",  OFFSET(chroma_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
+
+    { "alpha_radius", "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
+    { "ar",           "Radius of the alpha blurring box", OFFSET(alpha_param.radius_expr), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
+    { "alpha_power",  "How many times should the boxblur be applied to alpha",  OFFSET(alpha_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
+    { "ap",           "How many times should the boxblur be applied to alpha",  OFFSET(alpha_param.power), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
+
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(boxblur);
+
 static const AVFilterPad avfilter_vf_boxblur_inputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
         .config_props = config_input,
         .filter_frame = filter_frame,
-        .min_perms    = AV_PERM_READ
     },
     { NULL }
 };
@@ -386,12 +376,12 @@
     .name          = "boxblur",
     .description   = NULL_IF_CONFIG_SMALL("Blur the input."),
     .priv_size     = sizeof(BoxBlurContext),
+    .priv_class    = &boxblur_class,
     .init          = init,
     .uninit        = uninit,
     .query_formats = query_formats,
 
     .inputs    = avfilter_vf_boxblur_inputs,
     .outputs   = avfilter_vf_boxblur_outputs,
-
-    .priv_class = &boxblur_class,
+    .flags     = AVFILTER_FLAG_SUPPORT_TIMELINE,
 };
diff --git a/libavfilter/vf_colorbalance.c b/libavfilter/vf_colorbalance.c
new file mode 100644
index 0000000..be0f4f4
--- /dev/null
+++ b/libavfilter/vf_colorbalance.c
@@ -0,0 +1,212 @@
+/*
+ * Copyright (c) 2013 Paul B Mahol
+ *
+ * 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 "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "avfilter.h"
+#include "drawutils.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+#define R 0
+#define G 1
+#define B 2
+#define A 3
+
+typedef struct {
+    double shadows;
+    double midtones;
+    double highlights;
+} Range;
+
+typedef struct {
+    const AVClass *class;
+    Range cyan_red;
+    Range magenta_green;
+    Range yellow_blue;
+
+    uint8_t lut[3][256];
+
+    uint8_t rgba_map[4];
+    int step;
+} ColorBalanceContext;
+
+#define OFFSET(x) offsetof(ColorBalanceContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption colorbalance_options[] = {
+    { "rs", "set red shadows",      OFFSET(cyan_red.shadows),         AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
+    { "gs", "set green shadows",    OFFSET(magenta_green.shadows),    AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
+    { "bs", "set blue shadows",     OFFSET(yellow_blue.shadows),      AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
+    { "rm", "set red midtones",     OFFSET(cyan_red.midtones),        AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
+    { "gm", "set green midtones",   OFFSET(magenta_green.midtones),   AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
+    { "bm", "set blue midtones",    OFFSET(yellow_blue.midtones),     AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
+    { "rh", "set red highlights",   OFFSET(cyan_red.highlights),      AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
+    { "gh", "set green highlights", OFFSET(magenta_green.highlights), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
+    { "bh", "set blue highlights",  OFFSET(yellow_blue.highlights),   AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(colorbalance);
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
+        AV_PIX_FMT_RGBA,  AV_PIX_FMT_BGRA,
+        AV_PIX_FMT_ABGR,  AV_PIX_FMT_ARGB,
+        AV_PIX_FMT_0BGR,  AV_PIX_FMT_0RGB,
+        AV_PIX_FMT_RGB0,  AV_PIX_FMT_BGR0,
+        AV_PIX_FMT_NONE
+    };
+
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+    return 0;
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    ColorBalanceContext *cb = ctx->priv;
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
+    double *shadows, *midtones, *highlights, *buffer;
+    int i, r, g, b;
+
+    buffer = av_malloc(256 * 3 * sizeof(*buffer));
+    if (!buffer)
+        return AVERROR(ENOMEM);
+
+    shadows    = buffer + 256 * 0;
+    midtones   = buffer + 256 * 1;
+    highlights = buffer + 256 * 2;
+
+    for (i = 0; i < 256; i++) {
+        double low = av_clipd((i - 85.0) / -64.0 + 0.5, 0, 1) * 178.5;
+        double mid = av_clipd((i - 85.0) /  64.0 + 0.5, 0, 1) *
+                     av_clipd((i + 85.0 - 255.0) / -64.0 + 0.5, 0, 1) * 178.5;
+
+        shadows[i] = low;
+        midtones[i] = mid;
+        highlights[255 - i] = low;
+    }
+
+    for (i = 0; i < 256; i++) {
+        r = g = b = i;
+
+        r = av_clip_uint8(r + cb->cyan_red.shadows         * shadows[r]);
+        r = av_clip_uint8(r + cb->cyan_red.midtones        * midtones[r]);
+        r = av_clip_uint8(r + cb->cyan_red.highlights      * highlights[r]);
+
+        g = av_clip_uint8(g + cb->magenta_green.shadows    * shadows[g]);
+        g = av_clip_uint8(g + cb->magenta_green.midtones   * midtones[g]);
+        g = av_clip_uint8(g + cb->magenta_green.highlights * highlights[g]);
+
+        b = av_clip_uint8(b + cb->yellow_blue.shadows      * shadows[b]);
+        b = av_clip_uint8(b + cb->yellow_blue.midtones     * midtones[b]);
+        b = av_clip_uint8(b + cb->yellow_blue.highlights   * highlights[b]);
+
+        cb->lut[R][i] = r;
+        cb->lut[G][i] = g;
+        cb->lut[B][i] = b;
+    }
+
+    av_free(buffer);
+
+    ff_fill_rgba_map(cb->rgba_map, outlink->format);
+    cb->step = av_get_padded_bits_per_pixel(desc) >> 3;
+
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx = inlink->dst;
+    ColorBalanceContext *cb = ctx->priv;
+    AVFilterLink *outlink = ctx->outputs[0];
+    const uint8_t roffset = cb->rgba_map[R];
+    const uint8_t goffset = cb->rgba_map[G];
+    const uint8_t boffset = cb->rgba_map[B];
+    const uint8_t aoffset = cb->rgba_map[A];
+    const int step = cb->step;
+    const uint8_t *srcrow = in->data[0];
+    uint8_t *dstrow;
+    AVFrame *out;
+    int i, j;
+
+    if (av_frame_is_writable(in)) {
+        out = in;
+    } else {
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+        if (!out) {
+            av_frame_free(&in);
+            return AVERROR(ENOMEM);
+        }
+        av_frame_copy_props(out, in);
+    }
+
+    dstrow = out->data[0];
+    for (i = 0; i < outlink->h; i++) {
+        const uint8_t *src = srcrow;
+        uint8_t *dst = dstrow;
+
+        for (j = 0; j < outlink->w * step; j += step) {
+            dst[j + roffset] = cb->lut[R][src[j + roffset]];
+            dst[j + goffset] = cb->lut[G][src[j + goffset]];
+            dst[j + boffset] = cb->lut[B][src[j + boffset]];
+            if (in != out && step == 4)
+                dst[j + aoffset] = src[j + aoffset];
+        }
+
+        srcrow += in->linesize[0];
+        dstrow += out->linesize[0];
+    }
+
+    if (in != out)
+        av_frame_free(&in);
+    return ff_filter_frame(ctx->outputs[0], out);
+}
+
+static const AVFilterPad colorbalance_inputs[] = {
+    {
+        .name           = "default",
+        .type           = AVMEDIA_TYPE_VIDEO,
+        .filter_frame   = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad colorbalance_outputs[] = {
+     {
+         .name         = "default",
+         .type         = AVMEDIA_TYPE_VIDEO,
+         .config_props = config_output,
+     },
+     { NULL }
+};
+
+AVFilter avfilter_vf_colorbalance = {
+    .name          = "colorbalance",
+    .description   = NULL_IF_CONFIG_SMALL("Adjust the color balance."),
+    .priv_size     = sizeof(ColorBalanceContext),
+    .priv_class    = &colorbalance_class,
+    .query_formats = query_formats,
+    .inputs        = colorbalance_inputs,
+    .outputs       = colorbalance_outputs,
+};
diff --git a/libavfilter/vf_colorchannelmixer.c b/libavfilter/vf_colorchannelmixer.c
new file mode 100644
index 0000000..963d415
--- /dev/null
+++ b/libavfilter/vf_colorchannelmixer.c
@@ -0,0 +1,370 @@
+/*
+ * Copyright (c) 2013 Paul B Mahol
+ *
+ * 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 "libavutil/opt.h"
+#include "avfilter.h"
+#include "drawutils.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+#define R 0
+#define G 1
+#define B 2
+#define A 3
+
+typedef struct {
+    const AVClass *class;
+    double rr, rg, rb, ra;
+    double gr, gg, gb, ga;
+    double br, bg, bb, ba;
+    double ar, ag, ab, aa;
+
+    int *lut[4][4];
+
+    int *buffer;
+
+    uint8_t rgba_map[4];
+} ColorChannelMixerContext;
+
+#define OFFSET(x) offsetof(ColorChannelMixerContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption colorchannelmixer_options[] = {
+    { "rr", "set the red gain for the red channel",     OFFSET(rr), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
+    { "rg", "set the green gain for the red channel",   OFFSET(rg), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
+    { "rb", "set the blue gain for the red channel",    OFFSET(rb), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
+    { "ra", "set the alpha gain for the red channel",   OFFSET(ra), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
+    { "gr", "set the red gain for the green channel",   OFFSET(gr), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
+    { "gg", "set the green gain for the green channel", OFFSET(gg), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
+    { "gb", "set the blue gain for the green channel",  OFFSET(gb), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
+    { "ga", "set the alpha gain for the green channel", OFFSET(ga), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
+    { "br", "set the red gain for the blue channel",    OFFSET(br), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
+    { "bg", "set the green gain for the blue channel",  OFFSET(bg), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
+    { "bb", "set the blue gain for the blue channel",   OFFSET(bb), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
+    { "ba", "set the alpha gain for the blue channel",  OFFSET(ba), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
+    { "ar", "set the red gain for the alpha channel",   OFFSET(ar), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
+    { "ag", "set the green gain for the alpha channel", OFFSET(ag), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
+    { "ab", "set the blue gain for the alpha channel",  OFFSET(ab), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
+    { "aa", "set the alpha gain for the alpha channel", OFFSET(aa), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(colorchannelmixer);
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_RGB24,  AV_PIX_FMT_BGR24,
+        AV_PIX_FMT_RGBA,   AV_PIX_FMT_BGRA,
+        AV_PIX_FMT_ARGB,   AV_PIX_FMT_ABGR,
+        AV_PIX_FMT_0RGB,   AV_PIX_FMT_0BGR,
+        AV_PIX_FMT_RGB0,   AV_PIX_FMT_BGR0,
+        AV_PIX_FMT_RGB48,  AV_PIX_FMT_BGR48,
+        AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
+        AV_PIX_FMT_NONE
+    };
+
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+    return 0;
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    ColorChannelMixerContext *cm = ctx->priv;
+    int i, j, size, *buffer;
+
+    switch (outlink->format) {
+    case AV_PIX_FMT_RGB48:
+    case AV_PIX_FMT_BGR48:
+    case AV_PIX_FMT_RGBA64:
+    case AV_PIX_FMT_BGRA64:
+        if (outlink->format == AV_PIX_FMT_RGB48 ||
+            outlink->format == AV_PIX_FMT_RGBA64) {
+            cm->rgba_map[R] = 0;
+            cm->rgba_map[G] = 1;
+            cm->rgba_map[B] = 2;
+            cm->rgba_map[A] = 3;
+        } else {
+            cm->rgba_map[R] = 2;
+            cm->rgba_map[G] = 1;
+            cm->rgba_map[B] = 0;
+            cm->rgba_map[A] = 3;
+        }
+        size = 65536;
+        break;
+    default:
+        ff_fill_rgba_map(cm->rgba_map, outlink->format);
+        size = 256;
+    }
+
+    cm->buffer = buffer = av_malloc(16 * size * sizeof(*cm->buffer));
+    if (!cm->buffer)
+        return AVERROR(ENOMEM);
+
+    for (i = 0; i < 4; i++)
+        for (j = 0; j < 4; j++, buffer += size)
+            cm->lut[i][j] = buffer;
+
+    for (i = 0; i < size; i++) {
+        cm->lut[R][R][i] = i * cm->rr;
+        cm->lut[R][G][i] = i * cm->rg;
+        cm->lut[R][B][i] = i * cm->rb;
+        cm->lut[R][A][i] = i * cm->ra;
+
+        cm->lut[G][R][i] = i * cm->gr;
+        cm->lut[G][G][i] = i * cm->gg;
+        cm->lut[G][B][i] = i * cm->gb;
+        cm->lut[G][A][i] = i * cm->ga;
+
+        cm->lut[B][R][i] = i * cm->br;
+        cm->lut[B][G][i] = i * cm->bg;
+        cm->lut[B][B][i] = i * cm->bb;
+        cm->lut[B][A][i] = i * cm->ba;
+
+        cm->lut[A][R][i] = i * cm->ar;
+        cm->lut[A][G][i] = i * cm->ag;
+        cm->lut[A][B][i] = i * cm->ab;
+        cm->lut[A][A][i] = i * cm->aa;
+    }
+
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx = inlink->dst;
+    ColorChannelMixerContext *cm = ctx->priv;
+    AVFilterLink *outlink = ctx->outputs[0];
+    const uint8_t roffset = cm->rgba_map[R];
+    const uint8_t goffset = cm->rgba_map[G];
+    const uint8_t boffset = cm->rgba_map[B];
+    const uint8_t aoffset = cm->rgba_map[A];
+    const uint8_t *srcrow = in->data[0];
+    uint8_t *dstrow;
+    AVFrame *out;
+    int i, j;
+
+    if (av_frame_is_writable(in)) {
+        out = in;
+    } else {
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+        if (!out) {
+            av_frame_free(&in);
+            return AVERROR(ENOMEM);
+        }
+        av_frame_copy_props(out, in);
+    }
+
+    dstrow = out->data[0];
+    switch (outlink->format) {
+    case AV_PIX_FMT_BGR24:
+    case AV_PIX_FMT_RGB24:
+        for (i = 0; i < outlink->h; i++) {
+            const uint8_t *src = srcrow;
+            uint8_t *dst = dstrow;
+
+            for (j = 0; j < outlink->w * 3; j += 3) {
+                const uint8_t rin = src[j + roffset];
+                const uint8_t gin = src[j + goffset];
+                const uint8_t bin = src[j + boffset];
+
+                dst[j + roffset] = av_clip_uint8(cm->lut[R][R][rin] +
+                                                 cm->lut[R][G][gin] +
+                                                 cm->lut[R][B][bin]);
+                dst[j + goffset] = av_clip_uint8(cm->lut[G][R][rin] +
+                                                 cm->lut[G][G][gin] +
+                                                 cm->lut[G][B][bin]);
+                dst[j + boffset] = av_clip_uint8(cm->lut[B][R][rin] +
+                                                 cm->lut[B][G][gin] +
+                                                 cm->lut[B][B][bin]);
+            }
+
+            srcrow += in->linesize[0];
+            dstrow += out->linesize[0];
+        }
+        break;
+    case AV_PIX_FMT_0BGR:
+    case AV_PIX_FMT_0RGB:
+    case AV_PIX_FMT_BGR0:
+    case AV_PIX_FMT_RGB0:
+        for (i = 0; i < outlink->h; i++) {
+            const uint8_t *src = srcrow;
+            uint8_t *dst = dstrow;
+
+            for (j = 0; j < outlink->w * 4; j += 4) {
+                const uint8_t rin = src[j + roffset];
+                const uint8_t gin = src[j + goffset];
+                const uint8_t bin = src[j + boffset];
+
+                dst[j + roffset] = av_clip_uint8(cm->lut[R][R][rin] +
+                                                 cm->lut[R][G][gin] +
+                                                 cm->lut[R][B][bin]);
+                dst[j + goffset] = av_clip_uint8(cm->lut[G][R][rin] +
+                                                 cm->lut[G][G][gin] +
+                                                 cm->lut[G][B][bin]);
+                dst[j + boffset] = av_clip_uint8(cm->lut[B][R][rin] +
+                                                 cm->lut[B][G][gin] +
+                                                 cm->lut[B][B][bin]);
+                if (in != out)
+                    dst[j + aoffset] = 0;
+            }
+
+            srcrow += in->linesize[0];
+            dstrow += out->linesize[0];
+        }
+        break;
+    case AV_PIX_FMT_ABGR:
+    case AV_PIX_FMT_ARGB:
+    case AV_PIX_FMT_BGRA:
+    case AV_PIX_FMT_RGBA:
+        for (i = 0; i < outlink->h; i++) {
+            const uint8_t *src = srcrow;
+            uint8_t *dst = dstrow;
+
+            for (j = 0; j < outlink->w * 4; j += 4) {
+                const uint8_t rin = src[j + roffset];
+                const uint8_t gin = src[j + goffset];
+                const uint8_t bin = src[j + boffset];
+                const uint8_t ain = src[j + aoffset];
+
+                dst[j + roffset] = av_clip_uint8(cm->lut[R][R][rin] +
+                                                 cm->lut[R][G][gin] +
+                                                 cm->lut[R][B][bin] +
+                                                 cm->lut[R][A][ain]);
+                dst[j + goffset] = av_clip_uint8(cm->lut[G][R][rin] +
+                                                 cm->lut[G][G][gin] +
+                                                 cm->lut[G][B][bin] +
+                                                 cm->lut[G][A][ain]);
+                dst[j + boffset] = av_clip_uint8(cm->lut[B][R][rin] +
+                                                 cm->lut[B][G][gin] +
+                                                 cm->lut[B][B][bin] +
+                                                 cm->lut[B][A][ain]);
+                dst[j + aoffset] = av_clip_uint8(cm->lut[A][R][rin] +
+                                                 cm->lut[A][G][gin] +
+                                                 cm->lut[A][B][bin] +
+                                                 cm->lut[A][A][ain]);
+            }
+
+            srcrow += in->linesize[0];
+            dstrow += out->linesize[0];
+        }
+        break;
+    case AV_PIX_FMT_BGR48:
+    case AV_PIX_FMT_RGB48:
+        for (i = 0; i < outlink->h; i++) {
+            const uint16_t *src = (const uint16_t *)srcrow;
+            uint16_t *dst = (uint16_t *)dstrow;
+
+            for (j = 0; j < outlink->w * 3; j += 3) {
+                const uint16_t rin = src[j + roffset];
+                const uint16_t gin = src[j + goffset];
+                const uint16_t bin = src[j + boffset];
+
+                dst[j + roffset] = av_clip_uint16(cm->lut[R][R][rin] +
+                                                  cm->lut[R][G][gin] +
+                                                  cm->lut[R][B][bin]);
+                dst[j + goffset] = av_clip_uint16(cm->lut[G][R][rin] +
+                                                  cm->lut[G][G][gin] +
+                                                  cm->lut[G][B][bin]);
+                dst[j + boffset] = av_clip_uint16(cm->lut[B][R][rin] +
+                                                  cm->lut[B][G][gin] +
+                                                  cm->lut[B][B][bin]);
+            }
+
+            srcrow += in->linesize[0];
+            dstrow += out->linesize[0];
+        }
+        break;
+    case AV_PIX_FMT_BGRA64:
+    case AV_PIX_FMT_RGBA64:
+        for (i = 0; i < outlink->h; i++) {
+            const uint16_t *src = (const uint16_t *)srcrow;
+            uint16_t *dst = (uint16_t *)dstrow;
+
+            for (j = 0; j < outlink->w * 4; j += 4) {
+                const uint16_t rin = src[j + roffset];
+                const uint16_t gin = src[j + goffset];
+                const uint16_t bin = src[j + boffset];
+                const uint16_t ain = src[j + aoffset];
+
+                dst[j + roffset] = av_clip_uint16(cm->lut[R][R][rin] +
+                                                  cm->lut[R][G][gin] +
+                                                  cm->lut[R][B][bin] +
+                                                  cm->lut[R][A][ain]);
+                dst[j + goffset] = av_clip_uint16(cm->lut[G][R][rin] +
+                                                  cm->lut[G][G][gin] +
+                                                  cm->lut[G][B][bin] +
+                                                  cm->lut[G][A][ain]);
+                dst[j + boffset] = av_clip_uint16(cm->lut[B][R][rin] +
+                                                  cm->lut[B][G][gin] +
+                                                  cm->lut[B][B][bin] +
+                                                  cm->lut[B][A][ain]);
+                dst[j + aoffset] = av_clip_uint16(cm->lut[A][R][rin] +
+                                                  cm->lut[A][G][gin] +
+                                                  cm->lut[A][B][bin] +
+                                                  cm->lut[A][A][ain]);
+            }
+
+            srcrow += in->linesize[0];
+            dstrow += out->linesize[0];
+        }
+    }
+
+    if (in != out)
+        av_frame_free(&in);
+    return ff_filter_frame(ctx->outputs[0], out);
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    ColorChannelMixerContext *cm = ctx->priv;
+
+    av_freep(&cm->buffer);
+}
+
+static const AVFilterPad colorchannelmixer_inputs[] = {
+    {
+        .name           = "default",
+        .type           = AVMEDIA_TYPE_VIDEO,
+        .filter_frame   = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad colorchannelmixer_outputs[] = {
+     {
+         .name         = "default",
+         .type         = AVMEDIA_TYPE_VIDEO,
+         .config_props = config_output,
+     },
+     { NULL }
+};
+
+AVFilter avfilter_vf_colorchannelmixer = {
+    .name          = "colorchannelmixer",
+    .description   = NULL_IF_CONFIG_SMALL("Adjust colors by mixing color channels."),
+    .priv_size     = sizeof(ColorChannelMixerContext),
+    .priv_class    = &colorchannelmixer_class,
+    .uninit        = uninit,
+    .query_formats = query_formats,
+    .inputs        = colorchannelmixer_inputs,
+    .outputs       = colorchannelmixer_outputs,
+};
diff --git a/libavfilter/vf_colormatrix.c b/libavfilter/vf_colormatrix.c
index 571b9d4..8db5fcf 100644
--- a/libavfilter/vf_colormatrix.c
+++ b/libavfilter/vf_colormatrix.c
@@ -33,6 +33,7 @@
 #include "formats.h"
 #include "internal.h"
 #include "video.h"
+#include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/avstring.h"
 
@@ -54,15 +55,39 @@
       { -0.4450, -0.0550, +0.5000 } },
 };
 
+enum ColorMode {
+    COLOR_MODE_NONE = -1,
+    COLOR_MODE_BT709,
+    COLOR_MODE_FCC,
+    COLOR_MODE_BT601,
+    COLOR_MODE_SMPTE240M,
+    COLOR_MODE_COUNT
+};
+
 typedef struct {
+    const AVClass *class;
     int yuv_convert[16][3][3];
     int interlaced;
-    int source, dest, mode;
-    char src[256];
-    char dst[256];
+    enum ColorMode source, dest;
+    int mode;
     int hsub, vsub;
 } ColorMatrixContext;
 
+#define OFFSET(x) offsetof(ColorMatrixContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption colormatrix_options[] = {
+    { "src", "set source color matrix",      OFFSET(source), AV_OPT_TYPE_INT, {.i64=COLOR_MODE_NONE}, COLOR_MODE_NONE, COLOR_MODE_COUNT-1, .flags=FLAGS, .unit="color_mode" },
+    { "dst", "set destination color matrix", OFFSET(dest),   AV_OPT_TYPE_INT, {.i64=COLOR_MODE_NONE}, COLOR_MODE_NONE, COLOR_MODE_COUNT-1, .flags=FLAGS, .unit="color_mode" },
+    { "bt709",     "set BT.709 colorspace",      0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_BT709},       .flags=FLAGS, .unit="color_mode" },
+    { "fcc",       "set FCC colorspace   ",      0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_FCC},         .flags=FLAGS, .unit="color_mode" },
+    { "bt601",     "set BT.601 colorspace",      0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_BT601},       .flags=FLAGS, .unit="color_mode" },
+    { "smpte240m", "set SMPTE-240M colorspace",  0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_SMPTE240M},   .flags=FLAGS, .unit="color_mode" },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(colormatrix);
+
 #define ma m[0][0]
 #define mb m[0][1]
 #define mc m[0][2]
@@ -133,45 +158,19 @@
     }
 }
 
-static const char *color_modes[] = {"bt709", "FCC", "bt601", "smpte240m"};
+static const char *color_modes[] = {"bt709", "fcc", "bt601", "smpte240m"};
 
-static int get_color_mode_index(const char *name)
-{
-    int i;
-
-    for (i = 0; i < FF_ARRAY_ELEMS(color_modes); i++)
-        if (!av_strcasecmp(color_modes[i], name))
-            return i;
-    return -1;
-}
-
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     ColorMatrixContext *color = ctx->priv;
 
-    if (!args)
-        goto usage;
-    if (sscanf(args, "%255[^:]:%255[^:]", color->src, color->dst) != 2) {
-    usage:
-        av_log(ctx, AV_LOG_ERROR, "usage: <src>:<dst>\n");
-        av_log(ctx, AV_LOG_ERROR, "possible options: bt709,bt601,smpte240m,fcc\n");
-        return -1;
-    }
-
-    color->source = get_color_mode_index(color->src);
-    if (color->source < 0) {
-        av_log(ctx, AV_LOG_ERROR, "unknown color space %s\n", color->src);
-        return AVERROR(EINVAL);
-    }
-
-    color->dest = get_color_mode_index(color->dst);
-    if (color->dest < 0) {
-        av_log(ctx, AV_LOG_ERROR, "unknown color space %s\n", color->dst);
+    if (color->source == COLOR_MODE_NONE || color->dest == COLOR_MODE_NONE) {
+        av_log(ctx, AV_LOG_ERROR, "Unspecified source or destination color space\n");
         return AVERROR(EINVAL);
     }
 
     if (color->source == color->dest) {
-        av_log(ctx, AV_LOG_ERROR, "source and destination color space are identical\n");
+        av_log(ctx, AV_LOG_ERROR, "Source and destination color space must not be identical\n");
         return AVERROR(EINVAL);
     }
 
@@ -183,12 +182,12 @@
 }
 
 static void process_frame_uyvy422(ColorMatrixContext *color,
-                                  AVFilterBufferRef *dst, AVFilterBufferRef *src)
+                                  AVFrame *dst, AVFrame *src)
 {
     const unsigned char *srcp = src->data[0];
     const int src_pitch = src->linesize[0];
-    const int height = src->video->h;
-    const int width = src->video->w*2;
+    const int height = src->height;
+    const int width = src->width*2;
     unsigned char *dstp = dst->data[0];
     const int dst_pitch = dst->linesize[0];
     const int c2 = color->yuv_convert[color->mode][0][1];
@@ -215,15 +214,15 @@
 }
 
 static void process_frame_yuv422p(ColorMatrixContext *color,
-                                  AVFilterBufferRef *dst, AVFilterBufferRef *src)
+                                  AVFrame *dst, AVFrame *src)
 {
     const unsigned char *srcpU = src->data[1];
     const unsigned char *srcpV = src->data[2];
     const unsigned char *srcpY = src->data[0];
     const int src_pitchY  = src->linesize[0];
     const int src_pitchUV = src->linesize[1];
-    const int height = src->video->h;
-    const int width = src->video->w;
+    const int height = src->height;
+    const int width = src->width;
     unsigned char *dstpU = dst->data[1];
     unsigned char *dstpV = dst->data[2];
     unsigned char *dstpY = dst->data[0];
@@ -257,7 +256,7 @@
 }
 
 static void process_frame_yuv420p(ColorMatrixContext *color,
-                                  AVFilterBufferRef *dst, AVFilterBufferRef *src)
+                                  AVFrame *dst, AVFrame *src)
 {
     const unsigned char *srcpU = src->data[1];
     const unsigned char *srcpV = src->data[2];
@@ -265,8 +264,8 @@
     const unsigned char *srcpN = src->data[0] + src->linesize[0];
     const int src_pitchY  = src->linesize[0];
     const int src_pitchUV = src->linesize[1];
-    const int height = src->video->h;
-    const int width = src->video->w;
+    const int height = src->height;
+    const int width = src->width;
     unsigned char *dstpU = dst->data[1];
     unsigned char *dstpV = dst->data[2];
     unsigned char *dstpY = dst->data[0];
@@ -313,7 +312,8 @@
     color->hsub = pix_desc->log2_chroma_w;
     color->vsub = pix_desc->log2_chroma_h;
 
-    av_log(ctx, AV_LOG_VERBOSE, "%s -> %s\n", color->src, color->dst);
+    av_log(ctx, AV_LOG_VERBOSE, "%s -> %s\n",
+           color_modes[color->source], color_modes[color->dest]);
 
     return 0;
 }
@@ -332,19 +332,19 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *link, AVFilterBufferRef *in)
+static int filter_frame(AVFilterLink *link, AVFrame *in)
 {
     AVFilterContext *ctx = link->dst;
     ColorMatrixContext *color = ctx->priv;
     AVFilterLink *outlink = ctx->outputs[0];
-    AVFilterBufferRef *out;
+    AVFrame *out;
 
-    out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!out) {
-        avfilter_unref_bufferp(&in);
+        av_frame_free(&in);
         return AVERROR(ENOMEM);
     }
-    avfilter_copy_buffer_ref_props(out, in);
+    av_frame_copy_props(out, in);
 
     if (in->format == AV_PIX_FMT_YUV422P)
         process_frame_yuv422p(color, out, in);
@@ -353,7 +353,7 @@
     else
         process_frame_uyvy422(color, out, in);
 
-    avfilter_unref_bufferp(&in);
+    av_frame_free(&in);
     return ff_filter_frame(outlink, out);
 }
 
@@ -362,7 +362,6 @@
         .name             = "default",
         .type             = AVMEDIA_TYPE_VIDEO,
         .config_props     = config_input,
-        .min_perms        = AV_PERM_READ,
         .filter_frame     = filter_frame,
     },
     { NULL }
@@ -378,11 +377,13 @@
 
 AVFilter avfilter_vf_colormatrix = {
     .name          = "colormatrix",
-    .description   = NULL_IF_CONFIG_SMALL("Color matrix conversion"),
+    .description   = NULL_IF_CONFIG_SMALL("Convert color matrix."),
 
     .priv_size     = sizeof(ColorMatrixContext),
     .init          = init,
     .query_formats = query_formats,
     .inputs        = colormatrix_inputs,
     .outputs       = colormatrix_outputs,
+    .priv_class    = &colormatrix_class,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE,
 };
diff --git a/libavfilter/vf_copy.c b/libavfilter/vf_copy.c
index a25e282..df7ec31 100644
--- a/libavfilter/vf_copy.c
+++ b/libavfilter/vf_copy.c
@@ -21,17 +21,35 @@
  * copy video filter
  */
 
+#include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
 #include "avfilter.h"
 #include "internal.h"
 #include "video.h"
 
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+    AVFrame *out = ff_get_video_buffer(outlink, in->width, in->height);
+
+    if (!out) {
+        av_frame_free(&in);
+        return AVERROR(ENOMEM);
+    }
+    av_frame_copy_props(out, in);
+    av_image_copy(out->data, out->linesize, in->data, in->linesize,
+                  in->format, in->width, in->height);
+
+    av_frame_free(&in);
+    return ff_filter_frame(outlink, out);
+}
+
 static const AVFilterPad avfilter_vf_copy_inputs[] = {
     {
         .name             = "default",
         .type             = AVMEDIA_TYPE_VIDEO,
         .get_video_buffer = ff_null_get_video_buffer,
-        .rej_perms        = ~0
+        .filter_frame     = filter_frame,
     },
     { NULL }
 };
diff --git a/libavfilter/vf_crop.c b/libavfilter/vf_crop.c
index 8df9595..5d7dd2c 100644
--- a/libavfilter/vf_crop.c
+++ b/libavfilter/vf_crop.c
@@ -92,42 +92,6 @@
     double var_values[VAR_VARS_NB];
 } CropContext;
 
-#define OFFSET(x) offsetof(CropContext, x)
-#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
-
-static const AVOption crop_options[] = {
-    { "x",           "set the x crop area expression",       OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "(in_w-out_w)/2"}, CHAR_MIN, CHAR_MAX, FLAGS },
-    { "y",           "set the y crop area expression",       OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "(in_h-out_h)/2"}, CHAR_MIN, CHAR_MAX, FLAGS },
-    { "out_w",       "set the width crop area expression",   OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
-    { "w",           "set the width crop area expression",   OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
-    { "out_h",       "set the height crop area expression",  OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
-    { "h",           "set the height crop area expression",  OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
-    { "keep_aspect", "keep aspect ratio",                    OFFSET(keep_aspect), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
-    {NULL}
-};
-
-AVFILTER_DEFINE_CLASS(crop);
-
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    CropContext *crop = ctx->priv;
-    static const char *shorthand[] = { "w", "h", "x", "y", "keep_aspect", NULL };
-
-    crop->class = &crop_class;
-    av_opt_set_defaults(crop);
-
-    return av_opt_set_from_string(crop, args, shorthand, "=", ":");
-}
-
-static av_cold void uninit(AVFilterContext *ctx)
-{
-    CropContext *crop = ctx->priv;
-
-    av_expr_free(crop->x_pexpr); crop->x_pexpr = NULL;
-    av_expr_free(crop->y_pexpr); crop->y_pexpr = NULL;
-    av_opt_free(crop);
-}
-
 static int query_formats(AVFilterContext *ctx)
 {
     static const enum AVPixelFormat pix_fmts[] = {
@@ -161,6 +125,14 @@
     return 0;
 }
 
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    CropContext *crop = ctx->priv;
+
+    av_expr_free(crop->x_pexpr); crop->x_pexpr = NULL;
+    av_expr_free(crop->y_pexpr); crop->y_pexpr = NULL;
+}
+
 static inline int normalize_double(int *n, double d)
 {
     int ret = 0;
@@ -277,19 +249,21 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
+static int filter_frame(AVFilterLink *link, AVFrame *frame)
 {
     AVFilterContext *ctx = link->dst;
     CropContext *crop = ctx->priv;
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
     int i;
 
-    frame->video->w = crop->w;
-    frame->video->h = crop->h;
+    frame->width  = crop->w;
+    frame->height = crop->h;
 
+    crop->var_values[VAR_N] = link->frame_count;
     crop->var_values[VAR_T] = frame->pts == AV_NOPTS_VALUE ?
         NAN : frame->pts * av_q2d(link->time_base);
-    crop->var_values[VAR_POS] = frame->pos == -1 ? NAN : frame->pos;
+    crop->var_values[VAR_POS] = av_frame_get_pkt_pos(frame) == -1 ?
+        NAN : av_frame_get_pkt_pos(frame);
     crop->var_values[VAR_X] = av_expr_eval(crop->x_pexpr, crop->var_values, NULL);
     crop->var_values[VAR_Y] = av_expr_eval(crop->y_pexpr, crop->var_values, NULL);
     crop->var_values[VAR_X] = av_expr_eval(crop->x_pexpr, crop->var_values, NULL);
@@ -304,9 +278,9 @@
     crop->x &= ~((1 << crop->hsub) - 1);
     crop->y &= ~((1 << crop->vsub) - 1);
 
-    av_dlog(ctx, "n:%d t:%f x:%d y:%d x+w:%d y+h:%d\n",
-            (int)crop->var_values[VAR_N], crop->var_values[VAR_T], crop->x,
-            crop->y, crop->x+crop->w, crop->y+crop->h);
+    av_dlog(ctx, "n:%d t:%f pos:%f x:%d y:%d x+w:%d y+h:%d\n",
+            (int)crop->var_values[VAR_N], crop->var_values[VAR_T], crop->var_values[VAR_POS],
+            crop->x, crop->y, crop->x+crop->w, crop->y+crop->h);
 
     frame->data[0] += crop->y * frame->linesize[0];
     frame->data[0] += crop->x * crop->max_step[0];
@@ -326,11 +300,25 @@
         frame->data[3] += crop->x * crop->max_step[3];
     }
 
-    crop->var_values[VAR_N] += 1.0;
-
     return ff_filter_frame(link->dst->outputs[0], frame);
 }
 
+#define OFFSET(x) offsetof(CropContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption crop_options[] = {
+    { "out_w",       "set the width crop area expression",   OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "w",           "set the width crop area expression",   OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "out_h",       "set the height crop area expression",  OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "h",           "set the height crop area expression",  OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "x",           "set the x crop area expression",       OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "(in_w-out_w)/2"}, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "y",           "set the y crop area expression",       OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "(in_h-out_h)/2"}, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "keep_aspect", "keep aspect ratio",                    OFFSET(keep_aspect), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
+    {NULL}
+};
+
+AVFILTER_DEFINE_CLASS(crop);
+
 static const AVFilterPad avfilter_vf_crop_inputs[] = {
     {
         .name             = "default",
@@ -356,12 +344,11 @@
     .description = NULL_IF_CONFIG_SMALL("Crop the input video to width:height:x:y."),
 
     .priv_size = sizeof(CropContext),
+    .priv_class = &crop_class,
 
     .query_formats = query_formats,
-    .init          = init,
     .uninit        = uninit,
 
     .inputs    = avfilter_vf_crop_inputs,
     .outputs   = avfilter_vf_crop_outputs,
-    .priv_class = &crop_class,
 };
diff --git a/libavfilter/vf_cropdetect.c b/libavfilter/vf_cropdetect.c
index f91c522..cb170d0 100644
--- a/libavfilter/vf_cropdetect.c
+++ b/libavfilter/vf_cropdetect.c
@@ -23,16 +23,17 @@
  * Ported from MPlayer libmpcodecs/vf_cropdetect.c.
  */
 
-#include <stdio.h>
-
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
+#include "libavutil/opt.h"
+
 #include "avfilter.h"
 #include "formats.h"
 #include "internal.h"
 #include "video.h"
 
 typedef struct {
+    const AVClass *class;
     int x1, y1, x2, y2;
     int limit;
     int round;
@@ -83,18 +84,12 @@
     return total;
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     CropDetectContext *cd = ctx->priv;
 
-    cd->limit = 24;
-    cd->round = 0;
-    cd->reset_count = 0;
     cd->frame_nb = -2;
 
-    if (args)
-        sscanf(args, "%d:%d:%d", &cd->limit, &cd->round, &cd->reset_count);
-
     av_log(ctx, AV_LOG_VERBOSE, "limit:%d round:%d reset_count:%d\n",
            cd->limit, cd->round, cd->reset_count);
 
@@ -117,7 +112,7 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
 {
     AVFilterContext *ctx = inlink->dst;
     CropDetectContext *cd = ctx->priv;
@@ -128,36 +123,36 @@
     if (++cd->frame_nb > 0) {
         // Reset the crop area every reset_count frames, if reset_count is > 0
         if (cd->reset_count > 0 && cd->frame_nb > cd->reset_count) {
-            cd->x1 = frame->video->w-1;
-            cd->y1 = frame->video->h-1;
+            cd->x1 = frame->width  - 1;
+            cd->y1 = frame->height - 1;
             cd->x2 = 0;
             cd->y2 = 0;
             cd->frame_nb = 1;
         }
 
         for (y = 0; y < cd->y1; y++) {
-            if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->video->w, bpp) > cd->limit) {
+            if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->width, bpp) > cd->limit) {
                 cd->y1 = y;
                 break;
             }
         }
 
-        for (y = frame->video->h-1; y > cd->y2; y--) {
-            if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->video->w, bpp) > cd->limit) {
+        for (y = frame->height - 1; y > cd->y2; y--) {
+            if (checkline(ctx, frame->data[0] + frame->linesize[0] * y, bpp, frame->width, bpp) > cd->limit) {
                 cd->y2 = y;
                 break;
             }
         }
 
         for (y = 0; y < cd->x1; y++) {
-            if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->video->h, bpp) > cd->limit) {
+            if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->height, bpp) > cd->limit) {
                 cd->x1 = y;
                 break;
             }
         }
 
-        for (y = frame->video->w-1; y > cd->x2; y--) {
-            if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->video->h, bpp) > cd->limit) {
+        for (y = frame->width - 1; y > cd->x2; y--) {
+            if (checkline(ctx, frame->data[0] + bpp*y, frame->linesize[0], frame->height, bpp) > cd->limit) {
                 cd->x2 = y;
                 break;
             }
@@ -187,8 +182,8 @@
         y += (shrink_by/2 + 1) & ~1;
 
         av_log(ctx, AV_LOG_INFO,
-               "x1:%d x2:%d y1:%d y2:%d w:%d h:%d x:%d y:%d pos:%"PRId64" pts:%"PRId64" t:%f crop=%d:%d:%d:%d\n",
-               cd->x1, cd->x2, cd->y1, cd->y2, w, h, x, y, frame->pos, frame->pts,
+               "x1:%d x2:%d y1:%d y2:%d w:%d h:%d x:%d y:%d pts:%"PRId64" t:%f crop=%d:%d:%d:%d\n",
+               cd->x1, cd->x2, cd->y1, cd->y2, w, h, x, y, frame->pts,
                frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base),
                w, h, x, y);
     }
@@ -196,6 +191,19 @@
     return ff_filter_frame(inlink->dst->outputs[0], frame);
 }
 
+#define OFFSET(x) offsetof(CropDetectContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption cropdetect_options[] = {
+    { "limit", "Threshold below which the pixel is considered black", OFFSET(limit),       AV_OPT_TYPE_INT, { .i64 = 24 }, 0, 255, FLAGS },
+    { "round", "Value by which the width/height should be divisible", OFFSET(round),       AV_OPT_TYPE_INT, { .i64 = 16 }, 0, INT_MAX, FLAGS },
+    { "reset", "Recalculate the crop area after this many frames",    OFFSET(reset_count), AV_OPT_TYPE_INT, { .i64 = 0 },  0, INT_MAX, FLAGS },
+    { "reset_count", "Recalculate the crop area after this many frames",OFFSET(reset_count),AV_OPT_TYPE_INT,{ .i64 = 0 },  0, INT_MAX, FLAGS },
+    { NULL },
+};
+
+AVFILTER_DEFINE_CLASS(cropdetect);
+
 static const AVFilterPad avfilter_vf_cropdetect_inputs[] = {
     {
         .name             = "default",
@@ -220,11 +228,10 @@
     .description = NULL_IF_CONFIG_SMALL("Auto-detect crop size."),
 
     .priv_size = sizeof(CropDetectContext),
+    .priv_class = &cropdetect_class,
     .init      = init,
-
     .query_formats = query_formats,
-
     .inputs    = avfilter_vf_cropdetect_inputs,
-
     .outputs   = avfilter_vf_cropdetect_outputs,
+    .flags     = AVFILTER_FLAG_SUPPORT_TIMELINE,
 };
diff --git a/libavfilter/vf_curves.c b/libavfilter/vf_curves.c
new file mode 100644
index 0000000..626b4ea
--- /dev/null
+++ b/libavfilter/vf_curves.c
@@ -0,0 +1,518 @@
+/*
+ * Copyright (c) 2013 Clément Bœsch
+ *
+ * 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 "libavutil/opt.h"
+#include "libavutil/bprint.h"
+#include "libavutil/eval.h"
+#include "libavutil/file.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/avassert.h"
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+struct keypoint {
+    double x, y;
+    struct keypoint *next;
+};
+
+#define NB_COMP 3
+
+enum preset {
+    PRESET_NONE,
+    PRESET_COLOR_NEGATIVE,
+    PRESET_CROSS_PROCESS,
+    PRESET_DARKER,
+    PRESET_INCREASE_CONTRAST,
+    PRESET_LIGHTER,
+    PRESET_LINEAR_CONTRAST,
+    PRESET_MEDIUM_CONTRAST,
+    PRESET_NEGATIVE,
+    PRESET_STRONG_CONTRAST,
+    PRESET_VINTAGE,
+    NB_PRESETS,
+};
+
+typedef struct {
+    const AVClass *class;
+    enum preset preset;
+    char *comp_points_str[NB_COMP + 1];
+    char *comp_points_str_all;
+    uint8_t graph[NB_COMP + 1][256];
+    char *psfile;
+} CurvesContext;
+
+#define OFFSET(x) offsetof(CurvesContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption curves_options[] = {
+    { "preset", "select a color curves preset", OFFSET(preset), AV_OPT_TYPE_INT, {.i64=PRESET_NONE}, PRESET_NONE, NB_PRESETS-1, FLAGS, "preset_name" },
+        { "none",               NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_NONE},                 INT_MIN, INT_MAX, FLAGS, "preset_name" },
+        { "color_negative",     NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_COLOR_NEGATIVE},       INT_MIN, INT_MAX, FLAGS, "preset_name" },
+        { "cross_process",      NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_CROSS_PROCESS},        INT_MIN, INT_MAX, FLAGS, "preset_name" },
+        { "darker",             NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_DARKER},               INT_MIN, INT_MAX, FLAGS, "preset_name" },
+        { "increase_contrast",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_INCREASE_CONTRAST},    INT_MIN, INT_MAX, FLAGS, "preset_name" },
+        { "lighter",            NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_LIGHTER},              INT_MIN, INT_MAX, FLAGS, "preset_name" },
+        { "linear_contrast",    NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_LINEAR_CONTRAST},      INT_MIN, INT_MAX, FLAGS, "preset_name" },
+        { "medium_contrast",    NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_MEDIUM_CONTRAST},      INT_MIN, INT_MAX, FLAGS, "preset_name" },
+        { "negative",           NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_NEGATIVE},             INT_MIN, INT_MAX, FLAGS, "preset_name" },
+        { "strong_contrast",    NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_STRONG_CONTRAST},      INT_MIN, INT_MAX, FLAGS, "preset_name" },
+        { "vintage",            NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_VINTAGE},              INT_MIN, INT_MAX, FLAGS, "preset_name" },
+    { "master","set master points coordinates",OFFSET(comp_points_str[NB_COMP]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
+    { "m",     "set master points coordinates",OFFSET(comp_points_str[NB_COMP]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
+    { "red",   "set red points coordinates",   OFFSET(comp_points_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
+    { "r",     "set red points coordinates",   OFFSET(comp_points_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
+    { "green", "set green points coordinates", OFFSET(comp_points_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
+    { "g",     "set green points coordinates", OFFSET(comp_points_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
+    { "blue",  "set blue points coordinates",  OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
+    { "b",     "set blue points coordinates",  OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
+    { "all",   "set points coordinates for all components", OFFSET(comp_points_str_all), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
+    { "psfile", "set Photoshop curves file name", OFFSET(psfile), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(curves);
+
+static const struct {
+    const char *r;
+    const char *g;
+    const char *b;
+    const char *master;
+} curves_presets[] = {
+    [PRESET_COLOR_NEGATIVE] = {
+        "0/1 0.129/1 0.466/0.498 0.725/0 1/0",
+        "0/1 0.109/1 0.301/0.498 0.517/0 1/0",
+        "0/1 0.098/1 0.235/0.498 0.423/0 1/0",
+    },
+    [PRESET_CROSS_PROCESS] = {
+        "0.25/0.156 0.501/0.501 0.686/0.745",
+        "0.25/0.188 0.38/0.501 0.745/0.815 1/0.815",
+        "0.231/0.094 0.709/0.874",
+    },
+    [PRESET_DARKER]             = { .master = "0.5/0.4" },
+    [PRESET_INCREASE_CONTRAST]  = { .master = "0.149/0.066 0.831/0.905 0.905/0.98" },
+    [PRESET_LIGHTER]            = { .master = "0.4/0.5" },
+    [PRESET_LINEAR_CONTRAST]    = { .master = "0.305/0.286 0.694/0.713" },
+    [PRESET_MEDIUM_CONTRAST]    = { .master = "0.286/0.219 0.639/0.643" },
+    [PRESET_NEGATIVE]           = { .master = "0/1 1/0" },
+    [PRESET_STRONG_CONTRAST]    = { .master = "0.301/0.196 0.592/0.6 0.686/0.737" },
+    [PRESET_VINTAGE] = {
+        "0/0.11 0.42/0.51 1/0.95",
+        "0.50/0.48",
+        "0/0.22 0.49/0.44 1/0.8",
+    }
+};
+
+static struct keypoint *make_point(double x, double y, struct keypoint *next)
+{
+    struct keypoint *point = av_mallocz(sizeof(*point));
+
+    if (!point)
+        return NULL;
+    point->x = x;
+    point->y = y;
+    point->next = next;
+    return point;
+}
+
+static int parse_points_str(AVFilterContext *ctx, struct keypoint **points, const char *s)
+{
+    char *p = (char *)s; // strtod won't alter the string
+    struct keypoint *last = NULL;
+
+    /* construct a linked list based on the key points string */
+    while (p && *p) {
+        struct keypoint *point = make_point(0, 0, NULL);
+        if (!point)
+            return AVERROR(ENOMEM);
+        point->x = av_strtod(p, &p); if (p && *p) p++;
+        point->y = av_strtod(p, &p); if (p && *p) p++;
+        if (point->x < 0 || point->x > 1 || point->y < 0 || point->y > 1) {
+            av_log(ctx, AV_LOG_ERROR, "Invalid key point coordinates (%f;%f), "
+                   "x and y must be in the [0;1] range.\n", point->x, point->y);
+            return AVERROR(EINVAL);
+        }
+        if (!*points)
+            *points = point;
+        if (last) {
+            if ((int)(last->x * 255) >= (int)(point->x * 255)) {
+                av_log(ctx, AV_LOG_ERROR, "Key point coordinates (%f;%f) "
+                       "and (%f;%f) are too close from each other or not "
+                       "strictly increasing on the x-axis\n",
+                       last->x, last->y, point->x, point->y);
+                return AVERROR(EINVAL);
+            }
+            last->next = point;
+        }
+        last = point;
+    }
+
+    /* auto insert first key point if missing at x=0 */
+    if (!*points) {
+        last = make_point(0, 0, NULL);
+        if (!last)
+            return AVERROR(ENOMEM);
+        last->x = last->y = 0;
+        *points = last;
+    } else if ((*points)->x != 0.) {
+        struct keypoint *newfirst = make_point(0, 0, *points);
+        if (!newfirst)
+            return AVERROR(ENOMEM);
+        *points = newfirst;
+    }
+
+    av_assert0(last);
+
+    /* auto insert last key point if missing at x=1 */
+    if (last->x != 1.) {
+        struct keypoint *point = make_point(1, 1, NULL);
+        if (!point)
+            return AVERROR(ENOMEM);
+        last->next = point;
+    }
+
+    return 0;
+}
+
+static int get_nb_points(const struct keypoint *d)
+{
+    int n = 0;
+    while (d) {
+        n++;
+        d = d->next;
+    }
+    return n;
+}
+
+/**
+ * Natural cubic spline interpolation
+ * Finding curves using Cubic Splines notes by Steven Rauch and John Stockie.
+ * @see http://people.math.sfu.ca/~stockie/teaching/macm316/notes/splines.pdf
+ */
+static int interpolate(AVFilterContext *ctx, uint8_t *y, const struct keypoint *points)
+{
+    int i, ret = 0;
+    const struct keypoint *point;
+    double xprev = 0;
+
+    int n = get_nb_points(points); // number of splines
+
+    double (*matrix)[3] = av_calloc(n, sizeof(*matrix));
+    double *h = av_malloc((n - 1) * sizeof(*h));
+    double *r = av_calloc(n, sizeof(*r));
+
+    if (!matrix || !h || !r) {
+        ret = AVERROR(ENOMEM);
+        goto end;
+    }
+
+    /* h(i) = x(i+1) - x(i) */
+    i = -1;
+    for (point = points; point; point = point->next) {
+        if (i != -1)
+            h[i] = point->x - xprev;
+        xprev = point->x;
+        i++;
+    }
+
+    /* right-side of the polynomials, will be modified to contains the solution */
+    point = points;
+    for (i = 1; i < n - 1; i++) {
+        double yp = point->y,
+               yc = point->next->y,
+               yn = point->next->next->y;
+        r[i] = 6 * ((yn-yc)/h[i] - (yc-yp)/h[i-1]);
+        point = point->next;
+    }
+
+#define B 0 /* sub  diagonal (below main) */
+#define M 1 /* main diagonal (center) */
+#define A 2 /* sup  diagonal (above main) */
+
+    /* left side of the polynomials into a tridiagonal matrix. */
+    matrix[0][M] = matrix[n - 1][M] = 1;
+    for (i = 1; i < n - 1; i++) {
+        matrix[i][B] = h[i-1];
+        matrix[i][M] = 2 * (h[i-1] + h[i]);
+        matrix[i][A] = h[i];
+    }
+
+    /* tridiagonal solving of the linear system */
+    for (i = 1; i < n; i++) {
+        double den = matrix[i][M] - matrix[i][B] * matrix[i-1][A];
+        double k = den ? 1./den : 1.;
+        matrix[i][A] *= k;
+        r[i] = (r[i] - matrix[i][B] * r[i - 1]) * k;
+    }
+    for (i = n - 2; i >= 0; i--)
+        r[i] = r[i] - matrix[i][A] * r[i + 1];
+
+    /* compute the graph with x=[0..255] */
+    i = 0;
+    point = points;
+    av_assert0(point->next); // always at least 2 key points
+    while (point->next) {
+        double yc = point->y;
+        double yn = point->next->y;
+
+        double a = yc;
+        double b = (yn-yc)/h[i] - h[i]*r[i]/2. - h[i]*(r[i+1]-r[i])/6.;
+        double c = r[i] / 2.;
+        double d = (r[i+1] - r[i]) / (6.*h[i]);
+
+        int x;
+        int x_start = point->x       * 255;
+        int x_end   = point->next->x * 255;
+
+        av_assert0(x_start >= 0 && x_start <= 255 &&
+                   x_end   >= 0 && x_end   <= 255);
+
+        for (x = x_start; x <= x_end; x++) {
+            double xx = (x - x_start) * 1/255.;
+            double yy = a + b*xx + c*xx*xx + d*xx*xx*xx;
+            y[x] = av_clipf(yy, 0, 1) * 255;
+            av_log(ctx, AV_LOG_DEBUG, "f(%f)=%f -> y[%d]=%d\n", xx, yy, x, y[x]);
+        }
+
+        point = point->next;
+        i++;
+    }
+
+end:
+    av_free(matrix);
+    av_free(h);
+    av_free(r);
+    return ret;
+}
+
+static int parse_psfile(AVFilterContext *ctx, const char *fname)
+{
+    CurvesContext *curves = ctx->priv;
+    uint8_t *buf;
+    size_t size;
+    int i, ret, av_unused(version), nb_curves;
+    AVBPrint ptstr;
+    static const int comp_ids[] = {3, 0, 1, 2};
+
+    av_bprint_init(&ptstr, 0, AV_BPRINT_SIZE_AUTOMATIC);
+
+    ret = av_file_map(fname, &buf, &size, 0, NULL);
+    if (ret < 0)
+        return ret;
+
+#define READ16(dst) do {                \
+    if (size < 2)                       \
+        return AVERROR_INVALIDDATA;     \
+    dst = AV_RB16(buf);                 \
+    buf  += 2;                          \
+    size -= 2;                          \
+} while (0)
+
+    READ16(version);
+    READ16(nb_curves);
+    for (i = 0; i < FFMIN(nb_curves, FF_ARRAY_ELEMS(comp_ids)); i++) {
+        int nb_points, n;
+        av_bprint_clear(&ptstr);
+        READ16(nb_points);
+        for (n = 0; n < nb_points; n++) {
+            int y, x;
+            READ16(y);
+            READ16(x);
+            av_bprintf(&ptstr, "%f/%f ", x / 255., y / 255.);
+        }
+        if (*ptstr.str) {
+            char **pts = &curves->comp_points_str[comp_ids[i]];
+            if (!*pts) {
+                *pts = av_strdup(ptstr.str);
+                av_log(ctx, AV_LOG_DEBUG, "curves %d (intid=%d) [%d points]: [%s]\n",
+                       i, comp_ids[i], nb_points, *pts);
+                if (!*pts) {
+                    ret = AVERROR(ENOMEM);
+                    goto end;
+                }
+            }
+        }
+    }
+end:
+    av_bprint_finalize(&ptstr, NULL);
+    av_file_unmap(buf, size);
+    return ret;
+}
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    int i, j, ret;
+    CurvesContext *curves = ctx->priv;
+    struct keypoint *comp_points[NB_COMP + 1] = {0};
+    char **pts = curves->comp_points_str;
+    const char *allp = curves->comp_points_str_all;
+
+    //if (!allp && curves->preset != PRESET_NONE && curves_presets[curves->preset].all)
+    //    allp = curves_presets[curves->preset].all;
+
+    if (allp) {
+        for (i = 0; i < NB_COMP; i++) {
+            if (!pts[i])
+                pts[i] = av_strdup(allp);
+            if (!pts[i])
+                return AVERROR(ENOMEM);
+        }
+    }
+
+    if (curves->psfile) {
+        ret = parse_psfile(ctx, curves->psfile);
+        if (ret < 0)
+            return ret;
+    }
+
+    if (curves->preset != PRESET_NONE) {
+#define SET_COMP_IF_NOT_SET(n, name) do {                           \
+    if (!pts[n] && curves_presets[curves->preset].name) {           \
+        pts[n] = av_strdup(curves_presets[curves->preset].name);    \
+        if (!pts[n])                                                \
+            return AVERROR(ENOMEM);                                 \
+    }                                                               \
+} while (0)
+        SET_COMP_IF_NOT_SET(0, r);
+        SET_COMP_IF_NOT_SET(1, g);
+        SET_COMP_IF_NOT_SET(2, b);
+        SET_COMP_IF_NOT_SET(3, master);
+    }
+
+    for (i = 0; i < NB_COMP + 1; i++) {
+        ret = parse_points_str(ctx, comp_points + i, curves->comp_points_str[i]);
+        if (ret < 0)
+            return ret;
+        ret = interpolate(ctx, curves->graph[i], comp_points[i]);
+        if (ret < 0)
+            return ret;
+    }
+
+    if (pts[NB_COMP]) {
+        for (i = 0; i < NB_COMP; i++)
+            for (j = 0; j < 256; j++)
+                curves->graph[i][j] = curves->graph[NB_COMP][curves->graph[i][j]];
+    }
+
+    if (av_log_get_level() >= AV_LOG_VERBOSE) {
+        for (i = 0; i < NB_COMP; i++) {
+            struct keypoint *point = comp_points[i];
+            av_log(ctx, AV_LOG_VERBOSE, "#%d points:", i);
+            while (point) {
+                av_log(ctx, AV_LOG_VERBOSE, " (%f;%f)", point->x, point->y);
+                point = point->next;
+            }
+            av_log(ctx, AV_LOG_VERBOSE, "\n");
+            av_log(ctx, AV_LOG_VERBOSE, "#%d values:", i);
+            for (j = 0; j < 256; j++)
+                av_log(ctx, AV_LOG_VERBOSE, " %02X", curves->graph[i][j]);
+            av_log(ctx, AV_LOG_VERBOSE, "\n");
+        }
+    }
+
+    for (i = 0; i < NB_COMP + 1; i++) {
+        struct keypoint *point = comp_points[i];
+        while (point) {
+            struct keypoint *next = point->next;
+            av_free(point);
+            point = next;
+        }
+    }
+
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE};
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    int x, y, i, direct = 0;
+    AVFilterContext *ctx = inlink->dst;
+    CurvesContext *curves = ctx->priv;
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+    AVFrame *out;
+    uint8_t *dst;
+    const uint8_t *src;
+
+    if (av_frame_is_writable(in)) {
+        direct = 1;
+        out = in;
+    } else {
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+        if (!out) {
+            av_frame_free(&in);
+            return AVERROR(ENOMEM);
+        }
+        av_frame_copy_props(out, in);
+    }
+
+    dst = out->data[0];
+    src = in ->data[0];
+
+    for (y = 0; y < inlink->h; y++) {
+        uint8_t *dstp = dst;
+        const uint8_t *srcp = src;
+
+        for (x = 0; x < inlink->w; x++)
+            for (i = 0; i < NB_COMP; i++, dstp++, srcp++)
+                *dstp = curves->graph[i][*srcp];
+        dst += out->linesize[0];
+        src += in ->linesize[0];
+    }
+
+    if (!direct)
+        av_frame_free(&in);
+
+    return ff_filter_frame(outlink, out);
+}
+
+static const AVFilterPad curves_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad curves_outputs[] = {
+     {
+         .name = "default",
+         .type = AVMEDIA_TYPE_VIDEO,
+     },
+     { NULL }
+};
+
+AVFilter avfilter_vf_curves = {
+    .name          = "curves",
+    .description   = NULL_IF_CONFIG_SMALL("Adjust components curves."),
+    .priv_size     = sizeof(CurvesContext),
+    .init          = init,
+    .query_formats = query_formats,
+    .inputs        = curves_inputs,
+    .outputs       = curves_outputs,
+    .priv_class    = &curves_class,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE,
+};
diff --git a/libavfilter/vf_decimate.c b/libavfilter/vf_decimate.c
index c5761ad..9548531 100644
--- a/libavfilter/vf_decimate.c
+++ b/libavfilter/vf_decimate.c
@@ -1,266 +1,397 @@
 /*
- * Copyright (c) 2003 Rich Felker
- * Copyright (c) 2012 Stefano Sabatini
+ * Copyright (c) 2012 Fredrik Mellbin
+ * Copyright (c) 2013 Clément Bœsch
  *
  * This file is part of FFmpeg.
  *
- * FFmpeg is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * 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 General Public License for more details.
+ * 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 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 decimate filter, ported from libmpcodecs/vf_decimate.c by
- * Rich Felker.
+ * 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 "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/timestamp.h"
-#include "libavcodec/dsputil.h"
 #include "avfilter.h"
 #include "internal.h"
-#include "formats.h"
-#include "video.h"
+
+#define INPUT_MAIN     0
+#define INPUT_CLEANSRC 1
+
+struct qitem {
+    AVFrame *frame;
+    int64_t maxbdiff;
+    int64_t totdiff;
+};
 
 typedef struct {
     const AVClass *class;
-    int lo, hi;                    ///< lower and higher threshold number of differences
-                                   ///< values for 8x8 blocks
+    struct qitem *queue;    ///< window of cycle frames and the associated data diff
+    int fid;                ///< current frame id in the queue
+    int filled;             ///< 1 if the queue is filled, 0 otherwise
+    AVFrame *last;          ///< last frame from the previous queue
+    AVFrame **clean_src;    ///< frame queue for the clean source
+    int got_frame[2];       ///< frame request flag for each input stream
+    double ts_unit;         ///< timestamp units for the output frames
+    uint32_t eof;           ///< bitmask for end of stream
+    int hsub, vsub;         ///< chroma subsampling values
+    int depth;
+    int nxblocks, nyblocks;
+    int bdiffsize;
+    int64_t *bdiffs;
 
-    float frac;                    ///< threshold of changed pixels over the total fraction
-
-    int max_drop_count;            ///< if positive: maximum number of sequential frames to drop
-                                   ///< if negative: minimum number of frames between two drops
-
-    int drop_count;                ///< if positive: number of frames sequentially dropped
-                                   ///< if negative: number of sequential frames which were not dropped
-
-    int hsub, vsub;                ///< chroma subsampling values
-    AVFilterBufferRef *ref;        ///< reference picture
-    DSPContext dspctx;             ///< context providing optimized diff routines
-    AVCodecContext *avctx;         ///< codec context required for the DSPContext
+    /* options */
+    int cycle;
+    double dupthresh_flt;
+    double scthresh_flt;
+    int64_t dupthresh;
+    int64_t scthresh;
+    int blockx, blocky;
+    int ppsrc;
+    int chroma;
 } DecimateContext;
 
 #define OFFSET(x) offsetof(DecimateContext, x)
 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
 
 static const AVOption decimate_options[] = {
-    { "max",  "set the maximum number of consecutive dropped frames (positive), or the minimum interval between dropped frames (negative)",
-      OFFSET(max_drop_count), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, FLAGS },
-    { "hi",   "set high dropping threshold", OFFSET(hi), AV_OPT_TYPE_INT, {.i64=64*12}, INT_MIN, INT_MAX, FLAGS },
-    { "lo",   "set low dropping threshold", OFFSET(lo), AV_OPT_TYPE_INT, {.i64=64*5}, INT_MIN, INT_MAX, FLAGS },
-    { "frac", "set fraction dropping threshold",  OFFSET(frac), AV_OPT_TYPE_FLOAT, {.dbl=0.33}, 0, 1, FLAGS },
+    { "cycle",     "set the number of frame from which one will be dropped", OFFSET(cycle), AV_OPT_TYPE_INT, {.i64 = 5}, 2, 25, FLAGS },
+    { "dupthresh", "set duplicate threshold",    OFFSET(dupthresh_flt), AV_OPT_TYPE_DOUBLE, {.dbl =  1.1}, 0, 100, FLAGS },
+    { "scthresh",  "set scene change threshold", OFFSET(scthresh_flt),  AV_OPT_TYPE_DOUBLE, {.dbl = 15.0}, 0, 100, FLAGS },
+    { "blockx",    "set the size of the x-axis blocks used during metric calculations", OFFSET(blockx), AV_OPT_TYPE_INT, {.i64 = 32}, 4, 1<<9, FLAGS },
+    { "blocky",    "set the size of the y-axis blocks used during metric calculations", OFFSET(blocky), AV_OPT_TYPE_INT, {.i64 = 32}, 4, 1<<9, FLAGS },
+    { "ppsrc",     "mark main input as a pre-processed input and activate clean source input stream", OFFSET(ppsrc), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
+    { "chroma",    "set whether or not chroma is considered in the metric calculations", OFFSET(chroma), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
     { NULL }
 };
 
 AVFILTER_DEFINE_CLASS(decimate);
 
-/**
- * Return 1 if the two planes are different, 0 otherwise.
- */
-static int diff_planes(AVFilterContext *ctx,
-                       uint8_t *cur, uint8_t *ref, int linesize,
-                       int w, int h)
+static void calc_diffs(const DecimateContext *dm, struct qitem *q,
+                       const AVFrame *f1, const AVFrame *f2)
 {
-    DecimateContext *decimate = ctx->priv;
-    DSPContext *dspctx = &decimate->dspctx;
+    int64_t maxdiff = -1;
+    int64_t *bdiffs = dm->bdiffs;
+    int plane, i, j;
 
-    int x, y;
-    int d, c = 0;
-    int t = (w/16)*(h/16)*decimate->frac;
-    int16_t block[8*8];
+    memset(bdiffs, 0, dm->bdiffsize * sizeof(*bdiffs));
 
-    /* compute difference for blocks of 8x8 bytes */
-    for (y = 0; y < h-7; y += 4) {
-        for (x = 8; x < w-7; x += 4) {
-            dspctx->diff_pixels(block,
-                                cur+x+y*linesize,
-                                ref+x+y*linesize, linesize);
-            d = dspctx->sum_abs_dctelem(block);
-            if (d > decimate->hi)
-                return 1;
-            if (d > decimate->lo) {
-                c++;
-                if (c > t)
-                    return 1;
-            }
+    for (plane = 0; plane < (dm->chroma ? 3 : 1); plane++) {
+        int x, y, xl;
+        const int linesize1 = f1->linesize[plane];
+        const int linesize2 = f2->linesize[plane];
+        const uint8_t *f1p = f1->data[plane];
+        const uint8_t *f2p = f2->data[plane];
+        int width    = plane ? f1->width  >> dm->hsub : f1->width;
+        int height   = plane ? f1->height >> dm->vsub : f1->height;
+        int hblockx  = dm->blockx / 2;
+        int hblocky  = dm->blocky / 2;
+
+        if (plane) {
+            hblockx >>= dm->hsub;
+            hblocky >>= dm->vsub;
+        }
+
+        for (y = 0; y < height; y++) {
+            int ydest = y / hblocky;
+            int xdest = 0;
+
+#define CALC_DIFF(nbits) do {                               \
+    for (x = 0; x < width; x += hblockx) {                  \
+        int64_t acc = 0;                                    \
+        int m = FFMIN(width, x + hblockx);                  \
+        for (xl = x; xl < m; xl++)                          \
+            acc += abs(((const uint##nbits##_t *)f1p)[xl] - \
+                       ((const uint##nbits##_t *)f2p)[xl]); \
+        bdiffs[ydest * dm->nxblocks + xdest] += acc;        \
+        xdest++;                                            \
+    }                                                       \
+} while (0)
+            if (dm->depth == 8) CALC_DIFF(8);
+            else                CALC_DIFF(16);
+
+            f1p += linesize1;
+            f2p += linesize2;
         }
     }
-    return 0;
-}
 
-/**
- * Tell if the frame should be decimated, for example if it is no much
- * different with respect to the reference frame ref.
- */
-static int decimate_frame(AVFilterContext *ctx,
-                          AVFilterBufferRef *cur, AVFilterBufferRef *ref)
-{
-    DecimateContext *decimate = ctx->priv;
-    int plane;
-
-    if (decimate->max_drop_count > 0 &&
-        decimate->drop_count >= decimate->max_drop_count)
-        return 0;
-    if (decimate->max_drop_count < 0 &&
-        (decimate->drop_count-1) > decimate->max_drop_count)
-        return 0;
-
-    for (plane = 0; ref->data[plane] && ref->linesize[plane]; plane++) {
-        int vsub = plane == 1 || plane == 2 ? decimate->vsub : 0;
-        int hsub = plane == 1 || plane == 2 ? decimate->hsub : 0;
-        if (diff_planes(ctx,
-                        cur->data[plane], ref->data[plane], ref->linesize[plane],
-                        ref->video->w>>hsub, ref->video->h>>vsub))
-            return 0;
+    for (i = 0; i < dm->nyblocks - 1; i++) {
+        for (j = 0; j < dm->nxblocks - 1; j++) {
+            int64_t tmp = bdiffs[      i * dm->nxblocks + j    ]
+                        + bdiffs[      i * dm->nxblocks + j + 1]
+                        + bdiffs[(i + 1) * dm->nxblocks + j    ]
+                        + bdiffs[(i + 1) * dm->nxblocks + j + 1];
+            if (tmp > maxdiff)
+                maxdiff = tmp;
+        }
     }
 
-    return 1;
+    q->totdiff = 0;
+    for (i = 0; i < dm->bdiffsize; i++)
+        q->totdiff += bdiffs[i];
+    q->maxbdiff = maxdiff;
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
-    DecimateContext *decimate = ctx->priv;
-    static const char *shorthand[] = { "max", "hi", "lo", "frac", NULL };
-    int ret;
+    int scpos = -1, duppos = -1;
+    int drop = INT_MIN, i, lowest = 0, ret;
+    AVFilterContext *ctx  = inlink->dst;
+    AVFilterLink *outlink = ctx->outputs[0];
+    DecimateContext *dm   = ctx->priv;
+    AVFrame *prv;
 
-    decimate->class = &decimate_class;
-    av_opt_set_defaults(decimate);
+    /* update frames queue(s) */
+    if (FF_INLINK_IDX(inlink) == INPUT_MAIN) {
+        dm->queue[dm->fid].frame = in;
+        dm->got_frame[INPUT_MAIN] = 1;
+    } else {
+        dm->clean_src[dm->fid] = in;
+        dm->got_frame[INPUT_CLEANSRC] = 1;
+    }
+    if (!dm->got_frame[INPUT_MAIN] || (dm->ppsrc && !dm->got_frame[INPUT_CLEANSRC]))
+        return 0;
+    dm->got_frame[INPUT_MAIN] = dm->got_frame[INPUT_CLEANSRC] = 0;
 
-    if ((ret = av_opt_set_from_string(decimate, args, shorthand, "=", ":")) < 0)
-        return ret;
+    if (in) {
+        /* update frame metrics */
+        prv = dm->fid ? dm->queue[dm->fid - 1].frame : dm->last;
+        if (!prv)
+            prv = in;
+        calc_diffs(dm, &dm->queue[dm->fid], prv, in);
+        if (++dm->fid != dm->cycle)
+            return 0;
+        av_frame_free(&dm->last);
+        dm->last = av_frame_clone(in);
+        dm->fid = 0;
 
-    av_log(ctx, AV_LOG_VERBOSE, "max_drop_count:%d hi:%d lo:%d frac:%f\n",
-           decimate->max_drop_count, decimate->hi, decimate->lo, decimate->frac);
+        /* we have a complete cycle, select the frame to drop */
+        lowest = 0;
+        for (i = 0; i < dm->cycle; i++) {
+            if (dm->queue[i].totdiff > dm->scthresh)
+                scpos = i;
+            if (dm->queue[i].maxbdiff < dm->queue[lowest].maxbdiff)
+                lowest = i;
+        }
+        if (dm->queue[lowest].maxbdiff < dm->dupthresh)
+            duppos = lowest;
+        drop = scpos >= 0 && duppos < 0 ? scpos : lowest;
+    }
 
-    decimate->avctx = avcodec_alloc_context3(NULL);
-    if (!decimate->avctx)
+    /* metrics debug */
+    if (av_log_get_level() >= AV_LOG_DEBUG) {
+        av_log(ctx, AV_LOG_DEBUG, "1/%d frame drop:\n", dm->cycle);
+        for (i = 0; i < dm->cycle && dm->queue[i].frame; i++) {
+            av_log(ctx, AV_LOG_DEBUG,"  #%d: totdiff=%08"PRIx64" maxbdiff=%08"PRIx64"%s%s%s%s\n",
+                   i + 1, dm->queue[i].totdiff, dm->queue[i].maxbdiff,
+                   i == scpos  ? " sc"     : "",
+                   i == duppos ? " dup"    : "",
+                   i == lowest ? " lowest" : "",
+                   i == drop   ? " [DROP]" : "");
+        }
+    }
+
+    /* push all frames except the drop */
+    ret = 0;
+    for (i = 0; i < dm->cycle && dm->queue[i].frame; i++) {
+        if (i == drop) {
+            if (dm->ppsrc)
+                av_frame_free(&dm->clean_src[i]);
+            av_frame_free(&dm->queue[i].frame);
+        } else {
+            AVFrame *frame = dm->queue[i].frame;
+            if (dm->ppsrc) {
+                av_frame_free(&frame);
+                frame = dm->clean_src[i];
+            }
+            frame->pts = outlink->frame_count * dm->ts_unit;
+            ret = ff_filter_frame(outlink, frame);
+            if (ret < 0)
+                break;
+        }
+    }
+
+    return ret;
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    int max_value;
+    AVFilterContext *ctx = inlink->dst;
+    DecimateContext *dm = ctx->priv;
+    const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
+    const int w = inlink->w;
+    const int h = inlink->h;
+
+    dm->hsub      = pix_desc->log2_chroma_w;
+    dm->vsub      = pix_desc->log2_chroma_h;
+    dm->depth     = pix_desc->comp[0].depth_minus1 + 1;
+    max_value     = (1 << dm->depth) - 1;
+    dm->scthresh  = (int64_t)(((int64_t)max_value *          w * h          * dm->scthresh_flt)  / 100);
+    dm->dupthresh = (int64_t)(((int64_t)max_value * dm->blockx * dm->blocky * dm->dupthresh_flt) / 100);
+    dm->nxblocks  = (w + dm->blockx/2 - 1) / (dm->blockx/2);
+    dm->nyblocks  = (h + dm->blocky/2 - 1) / (dm->blocky/2);
+    dm->bdiffsize = dm->nxblocks * dm->nyblocks;
+    dm->bdiffs    = av_malloc(dm->bdiffsize * sizeof(*dm->bdiffs));
+    dm->queue     = av_calloc(dm->cycle, sizeof(*dm->queue));
+
+    if (!dm->bdiffs || !dm->queue)
         return AVERROR(ENOMEM);
-    dsputil_init(&decimate->dspctx, decimate->avctx);
+
+    if (dm->ppsrc) {
+        dm->clean_src = av_calloc(dm->cycle, sizeof(*dm->clean_src));
+        if (!dm->clean_src)
+            return AVERROR(ENOMEM);
+    }
 
     return 0;
 }
 
-static av_cold void uninit(AVFilterContext *ctx)
+static av_cold int decimate_init(AVFilterContext *ctx)
 {
-    DecimateContext *decimate = ctx->priv;
-    avfilter_unref_bufferp(&decimate->ref);
-    avcodec_close(decimate->avctx);
-    av_opt_free(decimate);
-    av_freep(&decimate->avctx);
+    const DecimateContext *dm = ctx->priv;
+    AVFilterPad pad = {
+        .name         = av_strdup("main"),
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+        .config_props = config_input,
+    };
+
+    if (!pad.name)
+        return AVERROR(ENOMEM);
+    ff_insert_inpad(ctx, INPUT_MAIN, &pad);
+
+    if (dm->ppsrc) {
+        pad.name = av_strdup("clean_src");
+        pad.config_props = NULL;
+        if (!pad.name)
+            return AVERROR(ENOMEM);
+        ff_insert_inpad(ctx, INPUT_CLEANSRC, &pad);
+    }
+
+    if ((dm->blockx & (dm->blockx - 1)) ||
+        (dm->blocky & (dm->blocky - 1))) {
+        av_log(ctx, AV_LOG_ERROR, "blockx and blocky settings must be power of two\n");
+        return AVERROR(EINVAL);
+    }
+
+    return 0;
+}
+
+static av_cold void decimate_uninit(AVFilterContext *ctx)
+{
+    int i;
+    DecimateContext *dm = ctx->priv;
+
+    av_frame_free(&dm->last);
+    av_freep(&dm->bdiffs);
+    av_freep(&dm->queue);
+    av_freep(&dm->clean_src);
+    for (i = 0; i < ctx->nb_inputs; i++)
+        av_freep(&ctx->input_pads[i].name);
+}
+
+static int request_inlink(AVFilterContext *ctx, int lid)
+{
+    int ret = 0;
+    DecimateContext *dm = ctx->priv;
+
+    if (!dm->got_frame[lid]) {
+        AVFilterLink *inlink = ctx->inputs[lid];
+        ret = ff_request_frame(inlink);
+        if (ret == AVERROR_EOF) { // flushing
+            dm->eof |= 1 << lid;
+            ret = filter_frame(inlink, NULL);
+        }
+    }
+    return ret;
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+    int ret;
+    AVFilterContext *ctx = outlink->src;
+    DecimateContext *dm = ctx->priv;
+    const uint32_t eof_mask = 1<<INPUT_MAIN | dm->ppsrc<<INPUT_CLEANSRC;
+
+    if ((dm->eof & eof_mask) == eof_mask) // flush done?
+        return AVERROR_EOF;
+    if ((ret = request_inlink(ctx, INPUT_MAIN)) < 0)
+        return ret;
+    if (dm->ppsrc && (ret = request_inlink(ctx, INPUT_CLEANSRC)) < 0)
+        return ret;
+    return 0;
 }
 
 static int query_formats(AVFilterContext *ctx)
 {
     static const enum AVPixelFormat pix_fmts[] = {
-        AV_PIX_FMT_YUV444P,      AV_PIX_FMT_YUV422P,
-        AV_PIX_FMT_YUV420P,      AV_PIX_FMT_YUV411P,
-        AV_PIX_FMT_YUV410P,      AV_PIX_FMT_YUV440P,
-        AV_PIX_FMT_YUVJ444P,     AV_PIX_FMT_YUVJ422P,
-        AV_PIX_FMT_YUVJ420P,     AV_PIX_FMT_YUVJ440P,
-        AV_PIX_FMT_YUVA420P,
+#define PF_NOALPHA(suf) AV_PIX_FMT_YUV420##suf,  AV_PIX_FMT_YUV422##suf,  AV_PIX_FMT_YUV444##suf
+#define PF_ALPHA(suf)   AV_PIX_FMT_YUVA420##suf, AV_PIX_FMT_YUVA422##suf, AV_PIX_FMT_YUVA444##suf
+#define PF(suf)         PF_NOALPHA(suf), PF_ALPHA(suf)
+        PF(P), PF(P9), PF(P10), PF_NOALPHA(P12), PF_NOALPHA(P14), PF(P16),
+        AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
+        AV_PIX_FMT_GRAY8,
         AV_PIX_FMT_NONE
     };
-
     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
-
     return 0;
 }
 
-static int config_input(AVFilterLink *inlink)
+static int config_output(AVFilterLink *outlink)
 {
-    AVFilterContext *ctx = inlink->dst;
-    DecimateContext *decimate = ctx->priv;
-    const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
-    decimate->hsub = pix_desc->log2_chroma_w;
-    decimate->vsub = pix_desc->log2_chroma_h;
+    AVFilterContext *ctx = outlink->src;
+    DecimateContext *dm = ctx->priv;
+    const AVFilterLink *inlink =
+        ctx->inputs[dm->ppsrc ? INPUT_CLEANSRC : INPUT_MAIN];
+    AVRational fps = inlink->frame_rate;
 
-    return 0;
-}
-
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *cur)
-{
-    DecimateContext *decimate = inlink->dst->priv;
-    AVFilterLink *outlink = inlink->dst->outputs[0];
-    int ret;
-
-    if (decimate->ref && decimate_frame(inlink->dst, cur, decimate->ref)) {
-        decimate->drop_count = FFMAX(1, decimate->drop_count+1);
-    } else {
-        avfilter_unref_buffer(decimate->ref);
-        decimate->ref = cur;
-        decimate->drop_count = FFMIN(-1, decimate->drop_count-1);
-
-        if (ret = ff_filter_frame(outlink, avfilter_ref_buffer(cur, ~AV_PERM_WRITE)) < 0)
-            return ret;
+    if (!fps.num || !fps.den) {
+        av_log(ctx, AV_LOG_ERROR, "The input needs a constant frame rate; "
+               "current rate of %d/%d is invalid\n", fps.num, fps.den);
+        return AVERROR(EINVAL);
     }
-
-    av_log(inlink->dst, AV_LOG_DEBUG,
-           "%s pts:%s pts_time:%s drop_count:%d\n",
-           decimate->drop_count > 0 ? "drop" : "keep",
-           av_ts2str(cur->pts), av_ts2timestr(cur->pts, &inlink->time_base),
-           decimate->drop_count);
-
-    if (decimate->drop_count > 0)
-        avfilter_unref_buffer(cur);
-
+    fps = av_mul_q(fps, (AVRational){dm->cycle - 1, dm->cycle});
+    av_log(ctx, AV_LOG_VERBOSE, "FPS: %d/%d -> %d/%d\n",
+           inlink->frame_rate.num, inlink->frame_rate.den, fps.num, fps.den);
+    outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
+    outlink->time_base  = inlink->time_base;
+    outlink->frame_rate = fps;
+    outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
+    outlink->w = inlink->w;
+    outlink->h = inlink->h;
+    dm->ts_unit = av_q2d(av_inv_q(av_mul_q(fps, outlink->time_base)));
     return 0;
 }
 
-static int request_frame(AVFilterLink *outlink)
-{
-    DecimateContext *decimate = outlink->src->priv;
-    AVFilterLink *inlink = outlink->src->inputs[0];
-    int ret;
-
-    do {
-        ret = ff_request_frame(inlink);
-    } while (decimate->drop_count > 0 && ret >= 0);
-
-    return ret;
-}
-
-static const AVFilterPad decimate_inputs[] = {
-    {
-        .name             = "default",
-        .type             = AVMEDIA_TYPE_VIDEO,
-        .get_video_buffer = ff_null_get_video_buffer,
-        .config_props     = config_input,
-        .filter_frame     = filter_frame,
-        .min_perms        = AV_PERM_READ | AV_PERM_PRESERVE,
-    },
-    { NULL }
-};
-
 static const AVFilterPad decimate_outputs[] = {
     {
         .name          = "default",
         .type          = AVMEDIA_TYPE_VIDEO,
         .request_frame = request_frame,
+        .config_props  = config_output,
     },
     { NULL }
 };
 
 AVFilter avfilter_vf_decimate = {
-    .name        = "decimate",
-    .description = NULL_IF_CONFIG_SMALL("Remove near-duplicate frames."),
-    .init        = init,
-    .uninit      = uninit,
-
-    .priv_size = sizeof(DecimateContext),
+    .name          = "decimate",
+    .description   = NULL_IF_CONFIG_SMALL("Decimate frames (post field matching filter)."),
+    .init          = decimate_init,
+    .uninit        = decimate_uninit,
+    .priv_size     = sizeof(DecimateContext),
     .query_formats = query_formats,
-    .inputs        = decimate_inputs,
     .outputs       = decimate_outputs,
     .priv_class    = &decimate_class,
+    .flags         = AVFILTER_FLAG_DYNAMIC_INPUTS,
 };
diff --git a/libavfilter/vf_delogo.c b/libavfilter/vf_delogo.c
index bf0ac62..c747b02 100644
--- a/libavfilter/vf_delogo.c
+++ b/libavfilter/vf_delogo.c
@@ -143,14 +143,14 @@
 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
 static const AVOption delogo_options[]= {
-    {"x",    "set logo x position",       OFFSET(x),    AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS},
-    {"y",    "set logo y position",       OFFSET(y),    AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS},
-    {"w",    "set logo width",            OFFSET(w),    AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS},
-    {"h",    "set logo height",           OFFSET(h),    AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, FLAGS},
-    {"band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, {.i64 =  4}, -1, INT_MAX, FLAGS},
-    {"t",    "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, {.i64 =  4}, -1, INT_MAX, FLAGS},
-    {"show", "show delogo area",          OFFSET(show), AV_OPT_TYPE_INT, {.i64 =  0},  0,       1, FLAGS},
-    {NULL},
+    { "x",    "set logo x position",       OFFSET(x),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
+    { "y",    "set logo y position",       OFFSET(y),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
+    { "w",    "set logo width",            OFFSET(w),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
+    { "h",    "set logo height",           OFFSET(h),    AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
+    { "band", "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 =  4 }, -1, INT_MAX, FLAGS },
+    { "t",    "set delogo area band size", OFFSET(band), AV_OPT_TYPE_INT, { .i64 =  4 }, -1, INT_MAX, FLAGS },
+    { "show", "show delogo area",          OFFSET(show), AV_OPT_TYPE_INT, { .i64 =  0 },  0, 1,       FLAGS },
+    { NULL },
 };
 
 AVFILTER_DEFINE_CLASS(delogo);
@@ -168,22 +168,9 @@
     return 0;
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     DelogoContext *delogo = ctx->priv;
-    int ret = 0;
-
-    delogo->class = &delogo_class;
-    av_opt_set_defaults(delogo);
-
-    if (args)
-        ret = sscanf(args, "%d:%d:%d:%d:%d",
-                     &delogo->x, &delogo->y, &delogo->w, &delogo->h, &delogo->band);
-    if (ret == 5) {
-        if (delogo->band < 0)
-            delogo->show = 1;
-    } else if ((ret = (av_set_options_string(delogo, args, "=", ":"))) < 0)
-        return ret;
 
 #define CHECK_UNSET_OPT(opt)                                            \
     if (delogo->opt == -1) {                                            \
@@ -195,8 +182,10 @@
     CHECK_UNSET_OPT(w);
     CHECK_UNSET_OPT(h);
 
-    if (delogo->show)
+    if (delogo->band < 0 || delogo->show) {
+        delogo->show = 1;
         delogo->band = 4;
+    }
 
     av_log(ctx, AV_LOG_VERBOSE, "x:%d y:%d, w:%d h:%d band:%d show:%d\n",
            delogo->x, delogo->y, delogo->w, delogo->h, delogo->band, delogo->show);
@@ -209,27 +198,28 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     DelogoContext *delogo = inlink->dst->priv;
     AVFilterLink *outlink = inlink->dst->outputs[0];
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
-    AVFilterBufferRef *out;
+    AVFrame *out;
     int hsub0 = desc->log2_chroma_w;
     int vsub0 = desc->log2_chroma_h;
     int direct = 0;
     int plane;
 
-    if (in->perms & AV_PERM_WRITE) {
+    if (av_frame_is_writable(in)) {
         direct = 1;
         out = in;
     } else {
-        out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
         if (!out) {
-            avfilter_unref_bufferp(&in);
+            av_frame_free(&in);
             return AVERROR(ENOMEM);
         }
-        avfilter_copy_buffer_ref_props(out, in);
+
+        av_frame_copy_props(out, in);
     }
 
     for (plane = 0; plane < 4 && in->data[plane]; plane++) {
@@ -246,7 +236,7 @@
     }
 
     if (!direct)
-        avfilter_unref_bufferp(&in);
+        av_frame_free(&in);
 
     return ff_filter_frame(outlink, out);
 }
@@ -257,7 +247,6 @@
         .type             = AVMEDIA_TYPE_VIDEO,
         .get_video_buffer = ff_null_get_video_buffer,
         .filter_frame     = filter_frame,
-        .min_perms        = AV_PERM_WRITE | AV_PERM_READ,
     },
     { NULL }
 };
@@ -274,10 +263,11 @@
     .name          = "delogo",
     .description   = NULL_IF_CONFIG_SMALL("Remove logo from input video."),
     .priv_size     = sizeof(DelogoContext),
+    .priv_class    = &delogo_class,
     .init          = init,
     .query_formats = query_formats,
 
     .inputs    = avfilter_vf_delogo_inputs,
     .outputs   = avfilter_vf_delogo_outputs,
-    .priv_class = &delogo_class,
+    .flags     = AVFILTER_FLAG_SUPPORT_TIMELINE,
 };
diff --git a/libavfilter/vf_deshake.c b/libavfilter/vf_deshake.c
index c03919c..fd72132 100644
--- a/libavfilter/vf_deshake.c
+++ b/libavfilter/vf_deshake.c
@@ -55,56 +55,44 @@
 #include "video.h"
 #include "libavutil/common.h"
 #include "libavutil/mem.h"
+#include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavcodec/dsputil.h"
 
-#include "transform.h"
+#include "deshake.h"
+#include "deshake_opencl.h"
 
 #define CHROMA_WIDTH(link)  -((-link->w) >> av_pix_fmt_desc_get(link->format)->log2_chroma_w)
 #define CHROMA_HEIGHT(link) -((-link->h) >> av_pix_fmt_desc_get(link->format)->log2_chroma_h)
 
-enum SearchMethod {
-    EXHAUSTIVE,        ///< Search all possible positions
-    SMART_EXHAUSTIVE,  ///< Search most possible positions (faster)
-    SEARCH_COUNT
+#define OFFSET(x) offsetof(DeshakeContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+#define MAX_R 64
+
+static const AVOption deshake_options[] = {
+    { "x", "set x for the rectangular search area",      OFFSET(cx), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
+    { "y", "set y for the rectangular search area",      OFFSET(cy), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
+    { "w", "set width for the rectangular search area",  OFFSET(cw), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
+    { "h", "set height for the rectangular search area", OFFSET(ch), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX, .flags = FLAGS },
+    { "rx", "set x for the rectangular search area",     OFFSET(rx), AV_OPT_TYPE_INT, {.i64=16}, 0, MAX_R, .flags = FLAGS },
+    { "ry", "set y for the rectangular search area",     OFFSET(ry), AV_OPT_TYPE_INT, {.i64=16}, 0, MAX_R, .flags = FLAGS },
+    { "edge", "set edge mode", OFFSET(edge), AV_OPT_TYPE_INT, {.i64=FILL_MIRROR}, FILL_BLANK, FILL_COUNT-1, FLAGS, "edge"},
+        { "blank",    "fill zeroes at blank locations",         0, AV_OPT_TYPE_CONST, {.i64=FILL_BLANK},    INT_MIN, INT_MAX, FLAGS, "edge" },
+        { "original", "original image at blank locations",      0, AV_OPT_TYPE_CONST, {.i64=FILL_ORIGINAL}, INT_MIN, INT_MAX, FLAGS, "edge" },
+        { "clamp",    "extruded edge value at blank locations", 0, AV_OPT_TYPE_CONST, {.i64=FILL_CLAMP},    INT_MIN, INT_MAX, FLAGS, "edge" },
+        { "mirror",   "mirrored edge at blank locations",       0, AV_OPT_TYPE_CONST, {.i64=FILL_MIRROR},   INT_MIN, INT_MAX, FLAGS, "edge" },
+    { "blocksize", "set motion search blocksize",       OFFSET(blocksize), AV_OPT_TYPE_INT, {.i64=8},   4, 128, .flags = FLAGS },
+    { "contrast",  "set contrast threshold for blocks", OFFSET(contrast),  AV_OPT_TYPE_INT, {.i64=125}, 1, 255, .flags = FLAGS },
+    { "search",  "set search strategy", OFFSET(search), AV_OPT_TYPE_INT, {.i64=EXHAUSTIVE}, EXHAUSTIVE, SEARCH_COUNT-1, FLAGS, "smode" },
+        { "exhaustive", "exhaustive search",      0, AV_OPT_TYPE_CONST, {.i64=EXHAUSTIVE},       INT_MIN, INT_MAX, FLAGS, "smode" },
+        { "less",       "less exhaustive search", 0, AV_OPT_TYPE_CONST, {.i64=SMART_EXHAUSTIVE}, INT_MIN, INT_MAX, FLAGS, "smode" },
+    { "filename", "set motion search detailed log file name", OFFSET(filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
+    { "opencl", "use OpenCL filtering capabilities", OFFSET(opencl), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, .flags = FLAGS },
+    { NULL }
 };
 
-typedef struct {
-    int x;             ///< Horizontal shift
-    int y;             ///< Vertical shift
-} IntMotionVector;
-
-typedef struct {
-    double x;             ///< Horizontal shift
-    double y;             ///< Vertical shift
-} MotionVector;
-
-typedef struct {
-    MotionVector vector;  ///< Motion vector
-    double angle;         ///< Angle of rotation
-    double zoom;          ///< Zoom percentage
-} Transform;
-
-typedef struct {
-    AVClass av_class;
-    AVFilterBufferRef *ref;    ///< Previous frame
-    int rx;                    ///< Maximum horizontal shift
-    int ry;                    ///< Maximum vertical shift
-    int edge;                  ///< Edge fill method
-    int blocksize;             ///< Size of blocks to compare
-    int contrast;              ///< Contrast threshold
-    int search;                ///< Motion search method
-    AVCodecContext *avctx;
-    DSPContext c;              ///< Context providing optimized SAD methods
-    Transform last;            ///< Transform from last frame
-    int refcount;              ///< Number of reference frames (defines averaging window)
-    FILE *fp;
-    Transform avg;
-    int cw;                    ///< Crop motion search to this box
-    int ch;
-    int cx;
-    int cy;
-} DeshakeContext;
+AVFILTER_DEFINE_CLASS(deshake);
 
 static int cmp(const double *a, const double *b)
 {
@@ -162,8 +150,8 @@
         }
     } else if (deshake->search == SMART_EXHAUSTIVE) {
         // Compare every other possible position and find the best match
-        for (y = -deshake->ry + 1; y < deshake->ry - 2; y += 2) {
-            for (x = -deshake->rx + 1; x < deshake->rx - 2; x += 2) {
+        for (y = -deshake->ry + 1; y < deshake->ry; y += 2) {
+            for (x = -deshake->rx + 1; x < deshake->rx; x += 2) {
                 diff = CMP(cx - x, cy - y);
                 if (diff < smallest) {
                     smallest = diff;
@@ -256,7 +244,7 @@
 {
     int x, y;
     IntMotionVector mv = {0, 0};
-    int counts[128][128];
+    int counts[2*MAX_R+1][2*MAX_R+1];
     int count_max_value = 0;
     int contrast;
 
@@ -332,42 +320,43 @@
     av_free(angles);
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static int deshake_transform_c(AVFilterContext *ctx,
+                                    int width, int height, int cw, int ch,
+                                    const float *matrix_y, const float *matrix_uv,
+                                    enum InterpolateMethod interpolate,
+                                    enum FillMethod fill, AVFrame *in, AVFrame *out)
 {
-    DeshakeContext *deshake = ctx->priv;
-    char filename[256] = {0};
+    int i = 0, ret = 0;
+    const float *matrixs[3];
+    int plane_w[3], plane_h[3];
+    matrixs[0] = matrix_y;
+    matrixs[1] =  matrixs[2] = matrix_uv;
+    plane_w[0] = width;
+    plane_w[1] = plane_w[2] = cw;
+    plane_h[0] = height;
+    plane_h[1] = plane_h[2] = ch;
 
-    deshake->rx = 16;
-    deshake->ry = 16;
-    deshake->edge = FILL_MIRROR;
-    deshake->blocksize = 8;
-    deshake->contrast = 125;
-    deshake->search = EXHAUSTIVE;
-    deshake->refcount = 20;
-
-    deshake->cw = -1;
-    deshake->ch = -1;
-    deshake->cx = -1;
-    deshake->cy = -1;
-
-    if (args) {
-        sscanf(args, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%255s",
-               &deshake->cx, &deshake->cy, &deshake->cw, &deshake->ch,
-               &deshake->rx, &deshake->ry, &deshake->edge,
-               &deshake->blocksize, &deshake->contrast, &deshake->search, filename);
-
-        deshake->blocksize /= 2;
-
-        deshake->rx = av_clip(deshake->rx, 0, 64);
-        deshake->ry = av_clip(deshake->ry, 0, 64);
-        deshake->edge = av_clip(deshake->edge, FILL_BLANK, FILL_COUNT - 1);
-        deshake->blocksize = av_clip(deshake->blocksize, 4, 128);
-        deshake->contrast = av_clip(deshake->contrast, 1, 255);
-        deshake->search = av_clip(deshake->search, EXHAUSTIVE, SEARCH_COUNT - 1);
-
+    for (i = 0; i < 3; i++) {
+        // Transform the luma and chroma planes
+        ret = avfilter_transform(in->data[i], out->data[i], in->linesize[i], out->linesize[i],
+                                 plane_w[i], plane_h[i], matrixs[i], interpolate, fill);
+        if (ret < 0)
+            return ret;
     }
-    if (*filename)
-        deshake->fp = fopen(filename, "w");
+    return ret;
+}
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    int ret;
+    DeshakeContext *deshake = ctx->priv;
+
+    deshake->refcount = 20; // XXX: add to options?
+    deshake->blocksize /= 2;
+    deshake->blocksize = av_clip(deshake->blocksize, 4, 128);
+
+    if (deshake->filename)
+        deshake->fp = fopen(deshake->filename, "w");
     if (deshake->fp)
         fwrite("Ori x, Avg x, Fin x, Ori y, Avg y, Fin y, Ori angle, Avg angle, Fin angle, Ori zoom, Avg zoom, Fin zoom\n", sizeof(char), 104, deshake->fp);
 
@@ -377,7 +366,18 @@
         deshake->cw += deshake->cx - (deshake->cx & ~15);
         deshake->cx &= ~15;
     }
+    deshake->transform = deshake_transform_c;
+    if (!CONFIG_OPENCL && deshake->opencl) {
+        av_log(ctx, AV_LOG_ERROR, "OpenCL support was not enabled in this build, cannot be selected\n");
+        return AVERROR(EINVAL);
+    }
 
+    if (CONFIG_OPENCL && deshake->opencl) {
+        deshake->transform = ff_opencl_transform;
+        ret = ff_opencl_deshake_init(ctx);
+        if (ret < 0)
+            return ret;
+    }
     av_log(ctx, AV_LOG_VERBOSE, "cx: %d, cy: %d, cw: %d, ch: %d, rx: %d, ry: %d, edge: %d blocksize: %d contrast: %d search: %d\n",
            deshake->cx, deshake->cy, deshake->cw, deshake->ch,
            deshake->rx, deshake->ry, deshake->edge, deshake->blocksize * 2, deshake->contrast, deshake->search);
@@ -409,7 +409,7 @@
     deshake->last.zoom = 0;
 
     deshake->avctx = avcodec_alloc_context3(NULL);
-    dsputil_init(&deshake->c, deshake->avctx);
+    avpriv_dsputil_init(&deshake->c, deshake->avctx);
 
     return 0;
 }
@@ -417,8 +417,10 @@
 static av_cold void uninit(AVFilterContext *ctx)
 {
     DeshakeContext *deshake = ctx->priv;
-
-    avfilter_unref_buffer(deshake->ref);
+    if (CONFIG_OPENCL && deshake->opencl) {
+        ff_opencl_deshake_uninit(ctx);
+    }
+    av_frame_free(&deshake->ref);
     if (deshake->fp)
         fclose(deshake->fp);
     if (deshake->avctx)
@@ -426,22 +428,29 @@
     av_freep(&deshake->avctx);
 }
 
-static int filter_frame(AVFilterLink *link, AVFilterBufferRef *in)
+static int filter_frame(AVFilterLink *link, AVFrame *in)
 {
     DeshakeContext *deshake = link->dst->priv;
     AVFilterLink *outlink = link->dst->outputs[0];
-    AVFilterBufferRef *out;
+    AVFrame *out;
     Transform t = {{0},0}, orig = {{0},0};
-    float matrix[9];
+    float matrix_y[9], matrix_uv[9];
     float alpha = 2.0 / deshake->refcount;
     char tmp[256];
+    int ret = 0;
 
-    out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!out) {
-        avfilter_unref_bufferp(&in);
+        av_frame_free(&in);
         return AVERROR(ENOMEM);
     }
-    avfilter_copy_buffer_ref_props(out, in);
+    av_frame_copy_props(out, in);
+
+    if (CONFIG_OPENCL && deshake->opencl) {
+        ret = ff_opencl_deshake_process_inout_buf(link->dst,in, out);
+        if (ret < 0)
+            return ret;
+    }
 
     if (deshake->cx < 0 || deshake->cy < 0 || deshake->cw < 0 || deshake->ch < 0) {
         // Find the most likely global motion for the current frame
@@ -515,20 +524,18 @@
     deshake->last.zoom = t.zoom;
 
     // Generate a luma transformation matrix
-    avfilter_get_matrix(t.vector.x, t.vector.y, t.angle, 1.0 + t.zoom / 100.0, matrix);
-
-    // Transform the luma plane
-    avfilter_transform(in->data[0], out->data[0], in->linesize[0], out->linesize[0], link->w, link->h, matrix, INTERPOLATE_BILINEAR, deshake->edge);
-
+    avfilter_get_matrix(t.vector.x, t.vector.y, t.angle, 1.0 + t.zoom / 100.0, matrix_y);
     // Generate a chroma transformation matrix
-    avfilter_get_matrix(t.vector.x / (link->w / CHROMA_WIDTH(link)), t.vector.y / (link->h / CHROMA_HEIGHT(link)), t.angle, 1.0 + t.zoom / 100.0, matrix);
-
-    // Transform the chroma planes
-    avfilter_transform(in->data[1], out->data[1], in->linesize[1], out->linesize[1], CHROMA_WIDTH(link), CHROMA_HEIGHT(link), matrix, INTERPOLATE_BILINEAR, deshake->edge);
-    avfilter_transform(in->data[2], out->data[2], in->linesize[2], out->linesize[2], CHROMA_WIDTH(link), CHROMA_HEIGHT(link), matrix, INTERPOLATE_BILINEAR, deshake->edge);
+    avfilter_get_matrix(t.vector.x / (link->w / CHROMA_WIDTH(link)), t.vector.y / (link->h / CHROMA_HEIGHT(link)), t.angle, 1.0 + t.zoom / 100.0, matrix_uv);
+    // Transform the luma and chroma planes
+    ret = deshake->transform(link->dst, link->w, link->h, CHROMA_WIDTH(link), CHROMA_HEIGHT(link),
+                             matrix_y, matrix_uv, INTERPOLATE_BILINEAR, deshake->edge, in, out);
 
     // Cleanup the old reference frame
-    avfilter_unref_buffer(deshake->ref);
+    av_frame_free(&deshake->ref);
+
+    if (ret < 0)
+        return ret;
 
     // Store the current frame as the reference frame for calculating the
     // motion of the next frame
@@ -543,7 +550,6 @@
         .type         = AVMEDIA_TYPE_VIDEO,
         .filter_frame = filter_frame,
         .config_props = config_props,
-        .min_perms    = AV_PERM_READ | AV_PERM_PRESERVE,
     },
     { NULL }
 };
@@ -565,4 +571,5 @@
     .query_formats = query_formats,
     .inputs        = deshake_inputs,
     .outputs       = deshake_outputs,
+    .priv_class    = &deshake_class,
 };
diff --git a/libavfilter/vf_drawbox.c b/libavfilter/vf_drawbox.c
index af8eca4..c2ffbf3 100644
--- a/libavfilter/vf_drawbox.c
+++ b/libavfilter/vf_drawbox.c
@@ -45,37 +45,10 @@
     int vsub, hsub;   ///< chroma subsampling
 } DrawBoxContext;
 
-#define OFFSET(x) offsetof(DrawBoxContext, x)
-#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
-
-static const AVOption drawbox_options[] = {
-    { "x",           "set the box top-left corner x position", OFFSET(x), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, FLAGS },
-    { "y",           "set the box top-left corner y position", OFFSET(y), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, FLAGS },
-    { "width",       "set the box width",  OFFSET(w), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
-    { "w",           "set the box width",  OFFSET(w), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
-    { "height",      "set the box height", OFFSET(h), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
-    { "h",           "set the box height", OFFSET(h), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
-    { "color",       "set the box edge color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS },
-    { "c",           "set the box edge color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS },
-    { "thickness",   "set the box maximum thickness", OFFSET(thickness), AV_OPT_TYPE_INT, {.i64=4}, 0, INT_MAX, FLAGS },
-    { "t",           "set the box maximum thickness", OFFSET(thickness), AV_OPT_TYPE_INT, {.i64=4}, 0, INT_MAX, FLAGS },
-    {NULL},
-};
-
-AVFILTER_DEFINE_CLASS(drawbox);
-
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     DrawBoxContext *drawbox = ctx->priv;
     uint8_t rgba_color[4];
-    static const char *shorthand[] = { "x", "y", "w", "h", "color", "thickness", NULL };
-    int ret;
-
-    drawbox->class = &drawbox_class;
-    av_opt_set_defaults(drawbox);
-
-    if ((ret = av_opt_set_from_string(drawbox, args, shorthand, "=", ":")) < 0)
-        return ret;
 
     if (!strcmp(drawbox->color_str, "invert"))
         drawbox->invert_color = 1;
@@ -92,12 +65,6 @@
     return 0;
 }
 
-static av_cold void uninit(AVFilterContext *ctx)
-{
-    DrawBoxContext *drawbox = ctx->priv;
-    av_opt_free(drawbox);
-}
-
 static int query_formats(AVFilterContext *ctx)
 {
     static const enum AVPixelFormat pix_fmts[] = {
@@ -130,13 +97,13 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
 {
     DrawBoxContext *drawbox = inlink->dst->priv;
     int plane, x, y, xb = drawbox->x, yb = drawbox->y;
     unsigned char *row[4];
 
-    for (y = FFMAX(yb, 0); y < frame->video->h && y < (yb + drawbox->h); y++) {
+    for (y = FFMAX(yb, 0); y < frame->height && y < (yb + drawbox->h); y++) {
         row[0] = frame->data[0] + y * frame->linesize[0];
 
         for (plane = 1; plane < 3; plane++)
@@ -144,12 +111,12 @@
                  frame->linesize[plane] * (y >> drawbox->vsub);
 
         if (drawbox->invert_color) {
-            for (x = FFMAX(xb, 0); x < xb + drawbox->w && x < frame->video->w; x++)
+            for (x = FFMAX(xb, 0); x < xb + drawbox->w && x < frame->width; x++)
                 if ((y - yb < drawbox->thickness-1) || (yb + drawbox->h - y < drawbox->thickness) ||
                     (x - xb < drawbox->thickness-1) || (xb + drawbox->w - x < drawbox->thickness))
                     row[0][x] = 0xff - row[0][x];
         } else {
-            for (x = FFMAX(xb, 0); x < xb + drawbox->w && x < frame->video->w; x++) {
+            for (x = FFMAX(xb, 0); x < xb + drawbox->w && x < frame->width; x++) {
                 double alpha = (double)drawbox->yuv_color[A] / 255;
 
                 if ((y - yb < drawbox->thickness-1) || (yb + drawbox->h - y < drawbox->thickness) ||
@@ -165,6 +132,25 @@
     return ff_filter_frame(inlink->dst->outputs[0], frame);
 }
 
+#define OFFSET(x) offsetof(DrawBoxContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption drawbox_options[] = {
+    { "x",      "Horizontal position of the left box edge", OFFSET(x),         AV_OPT_TYPE_INT,    { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS },
+    { "y",      "Vertical position of the top box edge",    OFFSET(y),         AV_OPT_TYPE_INT,    { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS },
+    { "width",  "Width of the box",                         OFFSET(w),         AV_OPT_TYPE_INT,    { .i64 = 0 }, 0,       INT_MAX, FLAGS },
+    { "w",      "Width of the box",                         OFFSET(w),         AV_OPT_TYPE_INT,    { .i64 = 0 }, 0,       INT_MAX, FLAGS },
+    { "height", "Height of the box",                        OFFSET(h),         AV_OPT_TYPE_INT,    { .i64 = 0 }, 0,       INT_MAX, FLAGS },
+    { "h",      "Height of the box",                        OFFSET(h),         AV_OPT_TYPE_INT,    { .i64 = 0 }, 0,       INT_MAX, FLAGS },
+    { "color",  "Color of the box",                         OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, .flags = FLAGS },
+    { "c",      "Color of the box",                         OFFSET(color_str), AV_OPT_TYPE_STRING, { .str = "black" }, CHAR_MIN, CHAR_MAX, .flags = FLAGS },
+    { "thickness",   "set the box maximum thickness",       OFFSET(thickness), AV_OPT_TYPE_INT, {.i64=4}, 0, INT_MAX, FLAGS },
+    { "t",           "set the box maximum thickness",       OFFSET(thickness), AV_OPT_TYPE_INT, {.i64=4}, 0, INT_MAX, FLAGS },
+    { NULL },
+};
+
+AVFILTER_DEFINE_CLASS(drawbox);
+
 static const AVFilterPad avfilter_vf_drawbox_inputs[] = {
     {
         .name             = "default",
@@ -172,7 +158,7 @@
         .config_props     = config_input,
         .get_video_buffer = ff_null_get_video_buffer,
         .filter_frame     = filter_frame,
-        .min_perms        = AV_PERM_WRITE | AV_PERM_READ,
+        .needs_writable   = 1,
     },
     { NULL }
 };
@@ -189,11 +175,11 @@
     .name      = "drawbox",
     .description = NULL_IF_CONFIG_SMALL("Draw a colored box on the input video."),
     .priv_size = sizeof(DrawBoxContext),
+    .priv_class = &drawbox_class,
     .init      = init,
-    .uninit    = uninit,
 
     .query_formats   = query_formats,
     .inputs    = avfilter_vf_drawbox_inputs,
     .outputs   = avfilter_vf_drawbox_outputs,
-    .priv_class = &drawbox_class,
+    .flags     = AVFILTER_FLAG_SUPPORT_TIMELINE,
 };
diff --git a/libavfilter/vf_drawtext.c b/libavfilter/vf_drawtext.c
index 2358e35..ef5bb03 100644
--- a/libavfilter/vf_drawtext.c
+++ b/libavfilter/vf_drawtext.c
@@ -72,6 +72,7 @@
     "text_w", "tw",           ///< width  of the rendered text
     "x",
     "y",
+    "pict_type",
     NULL
 };
 
@@ -108,6 +109,7 @@
     VAR_TEXT_W, VAR_TW,
     VAR_X,
     VAR_Y,
+    VAR_PICT_TYPE,
     VAR_VARS_NB
 };
 
@@ -164,7 +166,6 @@
     AVRational  tc_rate;            ///< frame rate for timecode
     AVTimecode  tc;                 ///< timecode context
     int tc24hmax;                   ///< 1 if timecode is wrapped to 24 hours, 0 otherwise
-    int frame_id;
     int reload;                     ///< reload text file for each frame
 } DrawTextContext;
 
@@ -172,53 +173,53 @@
 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
 static const AVOption drawtext_options[]= {
-{"fontfile", "set font file",        OFFSET(fontfile),           AV_OPT_TYPE_STRING, {.str=NULL},  CHAR_MIN, CHAR_MAX, FLAGS},
-{"text",     "set text",             OFFSET(text),               AV_OPT_TYPE_STRING, {.str=NULL},  CHAR_MIN, CHAR_MAX, FLAGS},
-{"textfile", "set text file",        OFFSET(textfile),           AV_OPT_TYPE_STRING, {.str=NULL},  CHAR_MIN, CHAR_MAX, FLAGS},
-{"fontcolor",   "set foreground color", OFFSET(fontcolor_string),   AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS},
-{"boxcolor",    "set box color",        OFFSET(boxcolor_string),    AV_OPT_TYPE_STRING, {.str="white"}, CHAR_MIN, CHAR_MAX, FLAGS},
-{"shadowcolor", "set shadow color",     OFFSET(shadowcolor_string), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS},
-{"box",      "set box",              OFFSET(draw_box),           AV_OPT_TYPE_INT,    {.i64=0},     0,        1       , FLAGS},
-{"fontsize", "set font size",        OFFSET(fontsize),           AV_OPT_TYPE_INT,    {.i64=0},     0,        INT_MAX , FLAGS},
-{"x",        "set x expression",     OFFSET(x_expr),             AV_OPT_TYPE_STRING, {.str="0"},   CHAR_MIN, CHAR_MAX, FLAGS},
-{"y",        "set y expression",     OFFSET(y_expr),             AV_OPT_TYPE_STRING, {.str="0"},   CHAR_MIN, CHAR_MAX, FLAGS},
-{"shadowx",  "set x",                OFFSET(shadowx),            AV_OPT_TYPE_INT,    {.i64=0},     INT_MIN,  INT_MAX , FLAGS},
-{"shadowy",  "set y",                OFFSET(shadowy),            AV_OPT_TYPE_INT,    {.i64=0},     INT_MIN,  INT_MAX , FLAGS},
-{"tabsize",  "set tab size",         OFFSET(tabsize),            AV_OPT_TYPE_INT,    {.i64=4},     0,        INT_MAX , FLAGS},
-{"basetime", "set base time",        OFFSET(basetime),           AV_OPT_TYPE_INT64,  {.i64=AV_NOPTS_VALUE}, INT64_MIN, INT64_MAX , FLAGS},
-{"draw",     "if false do not draw", OFFSET(draw_expr),          AV_OPT_TYPE_STRING, {.str="1"},   CHAR_MIN, CHAR_MAX, FLAGS},
+    {"fontfile",    "set font file",        OFFSET(fontfile),           AV_OPT_TYPE_STRING, {.str=NULL},  CHAR_MIN, CHAR_MAX, FLAGS},
+    {"text",        "set text",             OFFSET(text),               AV_OPT_TYPE_STRING, {.str=NULL},  CHAR_MIN, CHAR_MAX, FLAGS},
+    {"textfile",    "set text file",        OFFSET(textfile),           AV_OPT_TYPE_STRING, {.str=NULL},  CHAR_MIN, CHAR_MAX, FLAGS},
+    {"fontcolor",   "set foreground color", OFFSET(fontcolor_string),   AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS},
+    {"boxcolor",    "set box color",        OFFSET(boxcolor_string),    AV_OPT_TYPE_STRING, {.str="white"}, CHAR_MIN, CHAR_MAX, FLAGS},
+    {"shadowcolor", "set shadow color",     OFFSET(shadowcolor_string), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS},
+    {"box",         "set box",              OFFSET(draw_box),           AV_OPT_TYPE_INT,    {.i64=0},     0,        1       , FLAGS},
+    {"fontsize",    "set font size",        OFFSET(fontsize),           AV_OPT_TYPE_INT,    {.i64=0},     0,        INT_MAX , FLAGS},
+    {"x",           "set x expression",     OFFSET(x_expr),             AV_OPT_TYPE_STRING, {.str="0"},   CHAR_MIN, CHAR_MAX, FLAGS},
+    {"y",           "set y expression",     OFFSET(y_expr),             AV_OPT_TYPE_STRING, {.str="0"},   CHAR_MIN, CHAR_MAX, FLAGS},
+    {"shadowx",     "set x",                OFFSET(shadowx),            AV_OPT_TYPE_INT,    {.i64=0},     INT_MIN,  INT_MAX , FLAGS},
+    {"shadowy",     "set y",                OFFSET(shadowy),            AV_OPT_TYPE_INT,    {.i64=0},     INT_MIN,  INT_MAX , FLAGS},
+    {"tabsize",     "set tab size",         OFFSET(tabsize),            AV_OPT_TYPE_INT,    {.i64=4},     0,        INT_MAX , FLAGS},
+    {"basetime",    "set base time",        OFFSET(basetime),           AV_OPT_TYPE_INT64,  {.i64=AV_NOPTS_VALUE}, INT64_MIN, INT64_MAX , FLAGS},
+    {"draw",        "if false do not draw", OFFSET(draw_expr),          AV_OPT_TYPE_STRING, {.str="1"},   CHAR_MIN, CHAR_MAX, FLAGS},
 
-{"expansion","set the expansion mode", OFFSET(exp_mode),         AV_OPT_TYPE_INT,    {.i64=EXP_NORMAL},   0,        2, FLAGS, "expansion"},
-{"none",     "set no expansion",     OFFSET(exp_mode),           AV_OPT_TYPE_CONST,  {.i64=EXP_NONE},     0,        0, FLAGS, "expansion"},
-{"normal",   "set normal expansion", OFFSET(exp_mode),           AV_OPT_TYPE_CONST,  {.i64=EXP_NORMAL},   0,        0, FLAGS, "expansion"},
-{"strftime", "set strftime expansion (deprecated)", OFFSET(exp_mode), AV_OPT_TYPE_CONST, {.i64=EXP_STRFTIME}, 0,    0, FLAGS, "expansion"},
+    {"expansion", "set the expansion mode", OFFSET(exp_mode), AV_OPT_TYPE_INT, {.i64=EXP_NORMAL}, 0, 2, FLAGS, "expansion"},
+        {"none",     "set no expansion",                    OFFSET(exp_mode), AV_OPT_TYPE_CONST, {.i64=EXP_NONE},     0, 0, FLAGS, "expansion"},
+        {"normal",   "set normal expansion",                OFFSET(exp_mode), AV_OPT_TYPE_CONST, {.i64=EXP_NORMAL},   0, 0, FLAGS, "expansion"},
+        {"strftime", "set strftime expansion (deprecated)", OFFSET(exp_mode), AV_OPT_TYPE_CONST, {.i64=EXP_STRFTIME}, 0, 0, FLAGS, "expansion"},
 
-{"timecode", "set initial timecode", OFFSET(tc_opt_string),      AV_OPT_TYPE_STRING, {.str=NULL},  CHAR_MIN, CHAR_MAX, FLAGS},
-{"tc24hmax", "set 24 hours max (timecode only)", OFFSET(tc24hmax), AV_OPT_TYPE_INT,  {.i64=0},            0,        1, FLAGS},
-{"timecode_rate", "set rate (timecode only)", OFFSET(tc_rate),   AV_OPT_TYPE_RATIONAL, {.dbl=0},          0,  INT_MAX, FLAGS},
-{"r",        "set rate (timecode only)", OFFSET(tc_rate),        AV_OPT_TYPE_RATIONAL, {.dbl=0},          0,  INT_MAX, FLAGS},
-{"rate",     "set rate (timecode only)", OFFSET(tc_rate),        AV_OPT_TYPE_RATIONAL, {.dbl=0},          0,  INT_MAX, FLAGS},
-{"reload",   "reload text file for each frame", OFFSET(reload),  AV_OPT_TYPE_INT,    {.i64=0},            0,        1, FLAGS},
-{"fix_bounds", "if true, check and fix text coords to avoid clipping", OFFSET(fix_bounds), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS},
+    {"timecode",        "set initial timecode",             OFFSET(tc_opt_string), AV_OPT_TYPE_STRING,   {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
+    {"tc24hmax",        "set 24 hours max (timecode only)", OFFSET(tc24hmax),      AV_OPT_TYPE_INT,      {.i64=0},           0,        1, FLAGS},
+    {"timecode_rate",   "set rate (timecode only)",         OFFSET(tc_rate),       AV_OPT_TYPE_RATIONAL, {.dbl=0},           0,  INT_MAX, FLAGS},
+    {"r",               "set rate (timecode only)",         OFFSET(tc_rate),       AV_OPT_TYPE_RATIONAL, {.dbl=0},           0,  INT_MAX, FLAGS},
+    {"rate",            "set rate (timecode only)",         OFFSET(tc_rate),       AV_OPT_TYPE_RATIONAL, {.dbl=0},           0,  INT_MAX, FLAGS},
+    {"reload",     "reload text file for each frame",                       OFFSET(reload),     AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS},
+    {"fix_bounds", "if true, check and fix text coords to avoid clipping",  OFFSET(fix_bounds), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS},
 
-/* FT_LOAD_* flags */
-{"ft_load_flags", "set font loading flags for libfreetype",   OFFSET(ft_load_flags),  AV_OPT_TYPE_FLAGS,  {.i64=FT_LOAD_DEFAULT|FT_LOAD_RENDER}, 0, INT_MAX, FLAGS, "ft_load_flags"},
-{"default",                     "set default",                     0, AV_OPT_TYPE_CONST, {.i64=FT_LOAD_DEFAULT},                     INT_MIN, INT_MAX, FLAGS, "ft_load_flags"},
-{"no_scale",                    "set no_scale",                    0, AV_OPT_TYPE_CONST, {.i64=FT_LOAD_NO_SCALE},                    INT_MIN, INT_MAX, FLAGS, "ft_load_flags"},
-{"no_hinting",                  "set no_hinting",                  0, AV_OPT_TYPE_CONST, {.i64=FT_LOAD_NO_HINTING},                  INT_MIN, INT_MAX, FLAGS, "ft_load_flags"},
-{"render",                      "set render",                      0, AV_OPT_TYPE_CONST, {.i64=FT_LOAD_RENDER},                      INT_MIN, INT_MAX, FLAGS, "ft_load_flags"},
-{"no_bitmap",                   "set no_bitmap",                   0, AV_OPT_TYPE_CONST, {.i64=FT_LOAD_NO_BITMAP},                   INT_MIN, INT_MAX, FLAGS, "ft_load_flags"},
-{"vertical_layout",             "set vertical_layout",             0, AV_OPT_TYPE_CONST, {.i64=FT_LOAD_VERTICAL_LAYOUT},             INT_MIN, INT_MAX, FLAGS, "ft_load_flags"},
-{"force_autohint",              "set force_autohint",              0, AV_OPT_TYPE_CONST, {.i64=FT_LOAD_FORCE_AUTOHINT},              INT_MIN, INT_MAX, FLAGS, "ft_load_flags"},
-{"crop_bitmap",                 "set crop_bitmap",                 0, AV_OPT_TYPE_CONST, {.i64=FT_LOAD_CROP_BITMAP},                 INT_MIN, INT_MAX, FLAGS, "ft_load_flags"},
-{"pedantic",                    "set pedantic",                    0, AV_OPT_TYPE_CONST, {.i64=FT_LOAD_PEDANTIC},                    INT_MIN, INT_MAX, FLAGS, "ft_load_flags"},
-{"ignore_global_advance_width", "set ignore_global_advance_width", 0, AV_OPT_TYPE_CONST, {.i64=FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH}, INT_MIN, INT_MAX, FLAGS, "ft_load_flags"},
-{"no_recurse",                  "set no_recurse",                  0, AV_OPT_TYPE_CONST, {.i64=FT_LOAD_NO_RECURSE},                  INT_MIN, INT_MAX, FLAGS, "ft_load_flags"},
-{"ignore_transform",            "set ignore_transform",            0, AV_OPT_TYPE_CONST, {.i64=FT_LOAD_IGNORE_TRANSFORM},            INT_MIN, INT_MAX, FLAGS, "ft_load_flags"},
-{"monochrome",                  "set monochrome",                  0, AV_OPT_TYPE_CONST, {.i64=FT_LOAD_MONOCHROME},                  INT_MIN, INT_MAX, FLAGS, "ft_load_flags"},
-{"linear_design",               "set linear_design",               0, AV_OPT_TYPE_CONST, {.i64=FT_LOAD_LINEAR_DESIGN},               INT_MIN, INT_MAX, FLAGS, "ft_load_flags"},
-{"no_autohint",                 "set no_autohint",                 0, AV_OPT_TYPE_CONST, {.i64=FT_LOAD_NO_AUTOHINT},                 INT_MIN, INT_MAX, FLAGS, "ft_load_flags"},
-{NULL},
+    /* FT_LOAD_* flags */
+    { "ft_load_flags", "set font loading flags for libfreetype", OFFSET(ft_load_flags), AV_OPT_TYPE_FLAGS, { .i64 = FT_LOAD_DEFAULT | FT_LOAD_RENDER}, 0, INT_MAX, FLAGS, "ft_load_flags" },
+        { "default",                     NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_DEFAULT },                     .flags = FLAGS, .unit = "ft_load_flags" },
+        { "no_scale",                    NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_SCALE },                    .flags = FLAGS, .unit = "ft_load_flags" },
+        { "no_hinting",                  NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_HINTING },                  .flags = FLAGS, .unit = "ft_load_flags" },
+        { "render",                      NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_RENDER },                      .flags = FLAGS, .unit = "ft_load_flags" },
+        { "no_bitmap",                   NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_BITMAP },                   .flags = FLAGS, .unit = "ft_load_flags" },
+        { "vertical_layout",             NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_VERTICAL_LAYOUT },             .flags = FLAGS, .unit = "ft_load_flags" },
+        { "force_autohint",              NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_FORCE_AUTOHINT },              .flags = FLAGS, .unit = "ft_load_flags" },
+        { "crop_bitmap",                 NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_CROP_BITMAP },                 .flags = FLAGS, .unit = "ft_load_flags" },
+        { "pedantic",                    NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_PEDANTIC },                    .flags = FLAGS, .unit = "ft_load_flags" },
+        { "ignore_global_advance_width", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH }, .flags = FLAGS, .unit = "ft_load_flags" },
+        { "no_recurse",                  NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_RECURSE },                  .flags = FLAGS, .unit = "ft_load_flags" },
+        { "ignore_transform",            NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_IGNORE_TRANSFORM },            .flags = FLAGS, .unit = "ft_load_flags" },
+        { "monochrome",                  NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_MONOCHROME },                  .flags = FLAGS, .unit = "ft_load_flags" },
+        { "linear_design",               NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_LINEAR_DESIGN },               .flags = FLAGS, .unit = "ft_load_flags" },
+        { "no_autohint",                 NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_AUTOHINT },                 .flags = FLAGS, .unit = "ft_load_flags" },
+    { NULL},
 };
 
 AVFILTER_DEFINE_CLASS(drawtext);
@@ -416,18 +417,12 @@
     return 0;
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     int err;
     DrawTextContext *dtext = ctx->priv;
     Glyph *glyph;
 
-    dtext->class = &drawtext_class;
-    av_opt_set_defaults(dtext);
-
-    if ((err = av_set_options_string(dtext, args, "=", ":")) < 0)
-        return err;
-
     if (!dtext->fontfile && !CONFIG_FONTCONFIG) {
         av_log(ctx, AV_LOG_ERROR, "No font filename provided\n");
         return AVERROR(EINVAL);
@@ -542,11 +537,11 @@
     av_expr_free(dtext->x_pexpr); dtext->x_pexpr = NULL;
     av_expr_free(dtext->y_pexpr); dtext->y_pexpr = NULL;
     av_expr_free(dtext->draw_pexpr); dtext->draw_pexpr = NULL;
-    av_opt_free(dtext);
 
     av_freep(&dtext->positions);
     dtext->nb_positions = 0;
 
+
     av_tree_enumerate(dtext->glyphs, NULL, NULL, glyph_enu_free);
     av_tree_destroy(dtext->glyphs);
     dtext->glyphs = NULL;
@@ -607,7 +602,7 @@
         int ret;
         uninit(ctx);
         dtext->reinit = 1;
-        if ((ret = init(ctx, arg)) < 0)
+        if ((ret = init(ctx)) < 0)
             return ret;
         return config_input(ctx->inputs[0]);
     }
@@ -615,6 +610,15 @@
     return AVERROR(ENOSYS);
 }
 
+static int func_pict_type(AVFilterContext *ctx, AVBPrint *bp,
+                          char *fct, unsigned argc, char **argv, int tag)
+{
+    DrawTextContext *dtext = ctx->priv;
+
+    av_bprintf(bp, "%c", av_get_picture_type_char(dtext->var_values[VAR_PICT_TYPE]));
+    return 0;
+}
+
 static int func_pts(AVFilterContext *ctx, AVBPrint *bp,
                     char *fct, unsigned argc, char **argv, int tag)
 {
@@ -684,6 +688,7 @@
 } functions[] = {
     { "expr",      1, 1, 0,   func_eval_expr },
     { "e",         1, 1, 0,   func_eval_expr },
+    { "pict_type", 0, 0, 0,   func_pict_type },
     { "pts",       0, 0, 0,   func_pts      },
     { "gmtime",    0, 1, 'G', func_strftime },
     { "localtime", 0, 1, 'L', func_strftime },
@@ -784,7 +789,7 @@
     return 0;
 }
 
-static int draw_glyphs(DrawTextContext *dtext, AVFilterBufferRef *picref,
+static int draw_glyphs(DrawTextContext *dtext, AVFrame *frame,
                        int width, int height, const uint8_t rgbcolor[4], FFDrawColor *color, int x, int y)
 {
     char *text = dtext->expanded_text.str;
@@ -812,7 +817,7 @@
         y1 = dtext->positions[i].y+dtext->y+y;
 
         ff_blend_mask(&dtext->dc, color,
-                      picref->data, picref->linesize, width, height,
+                      frame->data, frame->linesize, width, height,
                       glyph->bitmap.buffer, glyph->bitmap.pitch,
                       glyph->bitmap.width, glyph->bitmap.rows,
                       glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO ? 0 : 3,
@@ -822,10 +827,11 @@
     return 0;
 }
 
-static int draw_text(AVFilterContext *ctx, AVFilterBufferRef *picref,
+static int draw_text(AVFilterContext *ctx, AVFrame *frame,
                      int width, int height)
 {
     DrawTextContext *dtext = ctx->priv;
+    AVFilterLink *inlink = ctx->inputs[0];
     uint32_t code = 0, prev_code = 0;
     int x = 0, y = 0, i = 0, ret;
     int max_text_line_w = 0, len;
@@ -845,7 +851,7 @@
     av_bprint_clear(bp);
 
     if(dtext->basetime != AV_NOPTS_VALUE)
-        now= picref->pts*av_q2d(ctx->inputs[0]->time_base) + dtext->basetime/1000000;
+        now= frame->pts*av_q2d(ctx->inputs[0]->time_base) + dtext->basetime/1000000;
 
     switch (dtext->exp_mode) {
     case EXP_NONE:
@@ -863,7 +869,7 @@
 
     if (dtext->tc_opt_string) {
         char tcbuf[AV_TIMECODE_STR_SIZE];
-        av_timecode_make_string(&dtext->tc, tcbuf, dtext->frame_id++);
+        av_timecode_make_string(&dtext->tc, tcbuf, inlink->frame_count);
         av_bprint_clear(bp);
         av_bprintf(bp, "%s%s", dtext->text, tcbuf);
     }
@@ -962,23 +968,23 @@
     /* draw box */
     if (dtext->draw_box)
         ff_blend_rectangle(&dtext->dc, &dtext->boxcolor,
-                           picref->data, picref->linesize, width, height,
+                           frame->data, frame->linesize, width, height,
                            dtext->x, dtext->y, box_w, box_h);
 
     if (dtext->shadowx || dtext->shadowy) {
-        if ((ret = draw_glyphs(dtext, picref, width, height, dtext->shadowcolor.rgba,
+        if ((ret = draw_glyphs(dtext, frame, width, height, dtext->shadowcolor.rgba,
                                &dtext->shadowcolor, dtext->shadowx, dtext->shadowy)) < 0)
             return ret;
     }
 
-    if ((ret = draw_glyphs(dtext, picref, width, height, dtext->fontcolor.rgba,
+    if ((ret = draw_glyphs(dtext, frame, width, height, dtext->fontcolor.rgba,
                            &dtext->fontcolor, 0, 0)) < 0)
         return ret;
 
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
 {
     AVFilterContext *ctx = inlink->dst;
     AVFilterLink *outlink = ctx->outputs[0];
@@ -989,18 +995,19 @@
         if ((ret = load_textfile(ctx)) < 0)
             return ret;
 
+    dtext->var_values[VAR_N] = inlink->frame_count;
     dtext->var_values[VAR_T] = frame->pts == AV_NOPTS_VALUE ?
         NAN : frame->pts * av_q2d(inlink->time_base);
 
-    draw_text(ctx, frame, frame->video->w, frame->video->h);
+    dtext->var_values[VAR_PICT_TYPE] = frame->pict_type;
+
+    draw_text(ctx, frame, frame->width, frame->height);
 
     av_log(ctx, AV_LOG_DEBUG, "n:%d t:%f text_w:%d text_h:%d x:%d y:%d\n",
            (int)dtext->var_values[VAR_N], dtext->var_values[VAR_T],
            (int)dtext->var_values[VAR_TEXT_W], (int)dtext->var_values[VAR_TEXT_H],
            dtext->x, dtext->y);
 
-    dtext->var_values[VAR_N] += 1.0;
-
     return ff_filter_frame(outlink, frame);
 }
 
@@ -1011,8 +1018,7 @@
         .get_video_buffer = ff_null_get_video_buffer,
         .filter_frame     = filter_frame,
         .config_props     = config_input,
-        .min_perms        = AV_PERM_WRITE |
-                            AV_PERM_READ,
+        .needs_writable   = 1,
     },
     { NULL }
 };
@@ -1029,6 +1035,7 @@
     .name          = "drawtext",
     .description   = NULL_IF_CONFIG_SMALL("Draw text on top of video frames using libfreetype library."),
     .priv_size     = sizeof(DrawTextContext),
+    .priv_class    = &drawtext_class,
     .init          = init,
     .uninit        = uninit,
     .query_formats = query_formats,
@@ -1036,5 +1043,4 @@
     .inputs    = avfilter_vf_drawtext_inputs,
     .outputs   = avfilter_vf_drawtext_outputs,
     .process_command = command,
-    .priv_class = &drawtext_class,
 };
diff --git a/libavfilter/vf_edgedetect.c b/libavfilter/vf_edgedetect.c
index 5837ccc..b4698a8 100644
--- a/libavfilter/vf_edgedetect.c
+++ b/libavfilter/vf_edgedetect.c
@@ -50,17 +50,10 @@
 
 AVFILTER_DEFINE_CLASS(edgedetect);
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
-    int ret;
     EdgeDetectContext *edgedetect = ctx->priv;
 
-    edgedetect->class = &edgedetect_class;
-    av_opt_set_defaults(edgedetect);
-
-    if ((ret = av_set_options_string(edgedetect, args, "=", ":")) < 0)
-        return ret;
-
     edgedetect->low_u8  = edgedetect->low  * 255. + .5;
     edgedetect->high_u8 = edgedetect->high * 255. + .5;
     return 0;
@@ -249,21 +242,27 @@
     }
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     AVFilterContext *ctx = inlink->dst;
     EdgeDetectContext *edgedetect = ctx->priv;
     AVFilterLink *outlink = inlink->dst->outputs[0];
     uint8_t  *tmpbuf    = edgedetect->tmpbuf;
     uint16_t *gradients = edgedetect->gradients;
-    AVFilterBufferRef *out;
+    int direct = 0;
+    AVFrame *out;
 
-    out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
-    if (!out) {
-        avfilter_unref_bufferp(&in);
-        return AVERROR(ENOMEM);
+    if (av_frame_is_writable(in)) {
+        direct = 1;
+        out = in;
+    } else {
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+        if (!out) {
+            av_frame_free(&in);
+            return AVERROR(ENOMEM);
+        }
+        av_frame_copy_props(out, in);
     }
-    avfilter_copy_buffer_ref_props(out, in);
 
     /* gaussian filter to reduce noise  */
     gaussian_blur(ctx, inlink->w, inlink->h,
@@ -287,7 +286,8 @@
                      out->data[0], out->linesize[0],
                      tmpbuf,       inlink->w);
 
-    avfilter_unref_bufferp(&in);
+    if (!direct)
+        av_frame_free(&in);
     return ff_filter_frame(outlink, out);
 }
 
@@ -305,7 +305,6 @@
         .type         = AVMEDIA_TYPE_VIDEO,
         .config_props = config_props,
         .filter_frame = filter_frame,
-        .min_perms    = AV_PERM_READ,
      },
      { NULL }
 };
@@ -328,4 +327,5 @@
     .inputs        = edgedetect_inputs,
     .outputs       = edgedetect_outputs,
     .priv_class    = &edgedetect_class,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE,
 };
diff --git a/libavfilter/vf_fade.c b/libavfilter/vf_fade.c
index df22274..4dbd4c7 100644
--- a/libavfilter/vf_fade.c
+++ b/libavfilter/vf_fade.c
@@ -46,73 +46,53 @@
 #define U 1
 #define V 2
 
+#define FADE_IN  0
+#define FADE_OUT 1
+
 typedef struct {
     const AVClass *class;
+    int type;
     int factor, fade_per_frame;
-    unsigned int frame_index, start_frame, stop_frame, nb_frames;
+    int start_frame, nb_frames;
+    unsigned int frame_index;
     int hsub, vsub, bpp;
     unsigned int black_level, black_level_scaled;
     uint8_t is_packed_rgb;
     uint8_t rgba_map[4];
     int alpha;
-
-    char *type;
+    uint64_t start_time, duration;
+    enum {VF_FADE_WAITING=0, VF_FADE_FADING, VF_FADE_DONE} fade_state;
 } FadeContext;
 
-#define OFFSET(x) offsetof(FadeContext, x)
-#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
-
-static const AVOption fade_options[] = {
-    { "type",        "set the fade direction",                     OFFSET(type),        AV_OPT_TYPE_STRING, {.str = "in" }, CHAR_MIN, CHAR_MAX, FLAGS },
-    { "t",           "set the fade direction",                     OFFSET(type),        AV_OPT_TYPE_STRING, {.str = "in" }, CHAR_MIN, CHAR_MAX, FLAGS },
-    { "start_frame", "set expression of frame to start fading",    OFFSET(start_frame), AV_OPT_TYPE_INT, {.i64 = 0    }, 0, INT_MAX, FLAGS },
-    { "s",           "set expression of frame to start fading",    OFFSET(start_frame), AV_OPT_TYPE_INT, {.i64 = 0    }, 0, INT_MAX, FLAGS },
-    { "nb_frames",   "set expression for fade duration in frames", OFFSET(nb_frames),   AV_OPT_TYPE_INT, {.i64 = 25   }, 0, INT_MAX, FLAGS },
-    { "n",           "set expression for fade duration in frames", OFFSET(nb_frames),   AV_OPT_TYPE_INT, {.i64 = 25   }, 0, INT_MAX, FLAGS },
-    { "alpha",       "fade alpha if it is available on the input", OFFSET(alpha),       AV_OPT_TYPE_INT, {.i64 = 0    }, 0,       1, FLAGS },
-    {NULL},
-};
-
-AVFILTER_DEFINE_CLASS(fade);
-
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     FadeContext *fade = ctx->priv;
-    static const char *shorthand[] = { "type", "start_frame", "nb_frames", NULL };
-    int ret;
-
-    fade->class = &fade_class;
-    av_opt_set_defaults(fade);
-
-    if ((ret = av_opt_set_from_string(fade, args, shorthand, "=", ":")) < 0)
-        return ret;
 
     fade->fade_per_frame = (1 << 16) / fade->nb_frames;
-    if (!strcmp(fade->type, "in"))
-        fade->factor = 0;
-    else if (!strcmp(fade->type, "out")) {
-        fade->fade_per_frame = -fade->fade_per_frame;
-        fade->factor = (1 << 16);
-    } else {
-        av_log(ctx, AV_LOG_ERROR,
-               "Type argument must be 'in' or 'out' but '%s' was specified\n", fade->type);
-        return AVERROR(EINVAL);
+    fade->fade_state = VF_FADE_WAITING;
+
+    if (fade->duration != 0) {
+        // If duration (seconds) is non-zero, assume that we are not fading based on frames
+        fade->nb_frames = 0; // Mostly to clean up logging
     }
-    fade->stop_frame = fade->start_frame + fade->nb_frames;
 
-    av_log(ctx, AV_LOG_VERBOSE,
-           "type:%s start_frame:%d nb_frames:%d alpha:%d\n",
-           fade->type, fade->start_frame, fade->nb_frames, fade->alpha);
+    // Choose what to log. If both time-based and frame-based options, both lines will be in the log
+    if (fade->start_frame || fade->nb_frames) {
+        av_log(ctx, AV_LOG_VERBOSE,
+               "type:%s start_frame:%d nb_frames:%d alpha:%d\n",
+               fade->type == FADE_IN ? "in" : "out", fade->start_frame,
+               fade->nb_frames,fade->alpha);
+    }
+    if (fade->start_time || fade->duration) {
+        av_log(ctx, AV_LOG_VERBOSE,
+               "type:%s start_time:%f duration:%f alpha:%d\n",
+               fade->type == FADE_IN ? "in" : "out", (fade->start_time / (double)AV_TIME_BASE),
+               (fade->duration / (double)AV_TIME_BASE),fade->alpha);
+    }
+
     return 0;
 }
 
-static av_cold void uninit(AVFilterContext *ctx)
-{
-    FadeContext *fade = ctx->priv;
-
-    av_opt_free(fade);
-}
-
 static int query_formats(AVFilterContext *ctx)
 {
     static const enum AVPixelFormat pix_fmts[] = {
@@ -178,32 +158,81 @@
     }
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
 {
     FadeContext *fade = inlink->dst->priv;
     uint8_t *p;
     int i, j, plane;
+    double frame_timestamp = frame->pts == AV_NOPTS_VALUE ? -1 : frame->pts * av_q2d(inlink->time_base);
+
+    // Calculate Fade assuming this is a Fade In
+    if (fade->fade_state == VF_FADE_WAITING) {
+        fade->factor=0;
+        if ((frame_timestamp >= (fade->start_time/(double)AV_TIME_BASE))
+            && (fade->frame_index >= fade->start_frame)) {
+            // Time to start fading
+            fade->fade_state = VF_FADE_FADING;
+
+            // Save start time in case we are starting based on frames and fading based on time
+            if ((fade->start_time == 0) && (fade->start_frame != 0)) {
+                fade->start_time = frame_timestamp*(double)AV_TIME_BASE;
+            }
+
+            // Save start frame in case we are starting based on time and fading based on frames
+            if ((fade->start_time != 0) && (fade->start_frame == 0)) {
+                fade->start_frame = fade->frame_index;
+            }
+        }
+    }
+    if (fade->fade_state == VF_FADE_FADING) {
+        if (fade->duration == 0) {
+            // Fading based on frame count
+            fade->factor = (fade->frame_index - fade->start_frame) * fade->fade_per_frame;
+            if (fade->frame_index > (fade->start_frame + fade->nb_frames)) {
+                fade->fade_state = VF_FADE_DONE;
+            }
+
+        } else {
+            // Fading based on duration
+            fade->factor = (frame_timestamp - (fade->start_time/(double)AV_TIME_BASE))
+                            * (float) UINT16_MAX / (fade->duration/(double)AV_TIME_BASE);
+            if (frame_timestamp > ((fade->start_time/(double)AV_TIME_BASE)
+                                    + (fade->duration/(double)AV_TIME_BASE))) {
+                fade->fade_state = VF_FADE_DONE;
+            }
+        }
+    }
+    if (fade->fade_state == VF_FADE_DONE) {
+        fade->factor=UINT16_MAX;
+    }
+
+    fade->factor = av_clip_uint16(fade->factor);
+
+    // Invert fade_factor if Fading Out
+    if (fade->type == 1) {
+        fade->factor=UINT16_MAX-fade->factor;
+    }
 
     if (fade->factor < UINT16_MAX) {
         if (fade->alpha) {
             // alpha only
             plane = fade->is_packed_rgb ? 0 : A; // alpha is on plane 0 for packed formats
                                                  // or plane 3 for planar formats
-            fade_plane(0, frame->video->h, inlink->w,
+            fade_plane(0, frame->height, inlink->w,
                        fade->factor, fade->black_level, fade->black_level_scaled,
                        fade->is_packed_rgb ? fade->rgba_map[A] : 0, // alpha offset for packed formats
                        fade->is_packed_rgb ? 4 : 1,                 // pixstep for 8 bit packed formats
                        1, frame->data[plane], frame->linesize[plane]);
         } else {
             /* luma or rgb plane */
-            fade_plane(0, frame->video->h, inlink->w,
+            fade_plane(0, frame->height, inlink->w,
                        fade->factor, fade->black_level, fade->black_level_scaled,
                        0, 1, // offset & pixstep for Y plane or RGB packed format
                        fade->bpp, frame->data[0], frame->linesize[0]);
             if (frame->data[1] && frame->data[2]) {
                 /* chroma planes */
                 for (plane = 1; plane < 3; plane++) {
-                    for (i = 0; i < frame->video->h; i++) {
+                    for (i = 0; i < frame->height; i++) {
                         p = frame->data[plane] + (i >> fade->vsub) * frame->linesize[plane];
                         for (j = 0; j < inlink->w >> fade->hsub; j++) {
                             /* 8421367 = ((128 << 1) + 1) << 15. It is an integer
@@ -218,15 +247,42 @@
         }
     }
 
-    if (fade->frame_index >= fade->start_frame &&
-        fade->frame_index <= fade->stop_frame)
-        fade->factor += fade->fade_per_frame;
-    fade->factor = av_clip_uint16(fade->factor);
     fade->frame_index++;
 
     return ff_filter_frame(inlink->dst->outputs[0], frame);
 }
 
+
+#define OFFSET(x) offsetof(FadeContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption fade_options[] = {
+    { "type", "'in' or 'out' for fade-in/fade-out", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = FADE_IN }, FADE_IN, FADE_OUT, FLAGS, "type" },
+    { "t",    "'in' or 'out' for fade-in/fade-out", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = FADE_IN }, FADE_IN, FADE_OUT, FLAGS, "type" },
+        { "in",  "fade-in",  0, AV_OPT_TYPE_CONST, { .i64 = FADE_IN },  .unit = "type" },
+        { "out", "fade-out", 0, AV_OPT_TYPE_CONST, { .i64 = FADE_OUT }, .unit = "type" },
+    { "start_frame", "Number of the first frame to which to apply the effect.",
+                                                    OFFSET(start_frame), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
+    { "s",           "Number of the first frame to which to apply the effect.",
+                                                    OFFSET(start_frame), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
+    { "nb_frames",   "Number of frames to which the effect should be applied.",
+                                                    OFFSET(nb_frames),   AV_OPT_TYPE_INT, { .i64 = 25 }, 0, INT_MAX, FLAGS },
+    { "n",           "Number of frames to which the effect should be applied.",
+                                                    OFFSET(nb_frames),   AV_OPT_TYPE_INT, { .i64 = 25 }, 0, INT_MAX, FLAGS },
+    { "alpha",       "fade alpha if it is available on the input", OFFSET(alpha),       AV_OPT_TYPE_INT, {.i64 = 0    }, 0,       1, FLAGS },
+    { "start_time",  "Number of seconds of the beginning of the effect.",
+                                                    OFFSET(start_time),  AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
+    { "st",          "Number of seconds of the beginning of the effect.",
+                                                    OFFSET(start_time),  AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
+    { "duration",    "Duration of the effect in seconds.",
+                                                    OFFSET(duration),    AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
+    { "d",           "Duration of the effect in seconds.",
+                                                    OFFSET(duration),    AV_OPT_TYPE_DURATION, {.i64 = 0. }, 0, INT32_MAX, FLAGS },
+    { NULL },
+};
+
+AVFILTER_DEFINE_CLASS(fade);
+
 static const AVFilterPad avfilter_vf_fade_inputs[] = {
     {
         .name             = "default",
@@ -234,7 +290,7 @@
         .config_props     = config_props,
         .get_video_buffer = ff_null_get_video_buffer,
         .filter_frame     = filter_frame,
-        .min_perms        = AV_PERM_READ | AV_PERM_WRITE,
+        .needs_writable   = 1,
     },
     { NULL }
 };
@@ -251,11 +307,10 @@
     .name          = "fade",
     .description   = NULL_IF_CONFIG_SMALL("Fade in/out input video."),
     .init          = init,
-    .uninit        = uninit,
     .priv_size     = sizeof(FadeContext),
+    .priv_class    = &fade_class,
     .query_formats = query_formats,
 
     .inputs    = avfilter_vf_fade_inputs,
     .outputs   = avfilter_vf_fade_outputs,
-    .priv_class = &fade_class,
 };
diff --git a/libavfilter/vf_field.c b/libavfilter/vf_field.c
index 67c0025..bbe66de 100644
--- a/libavfilter/vf_field.c
+++ b/libavfilter/vf_field.c
@@ -50,17 +50,6 @@
 
 AVFILTER_DEFINE_CLASS(field);
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    FieldContext *field = ctx->priv;
-    static const char *shorthand[] = { "type", NULL };
-
-    field->class = &field_class;
-    av_opt_set_defaults(field);
-
-    return av_opt_set_from_string(field, args, shorthand, "=", ":");
-}
-
 static int config_props_output(AVFilterLink *outlink)
 {
     AVFilterContext *ctx = outlink->src;
@@ -82,14 +71,14 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
+static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
 {
     FieldContext *field = inlink->dst->priv;
     AVFilterLink *outlink = inlink->dst->outputs[0];
     int i;
 
-    inpicref->video->h = outlink->h;
-    inpicref->video->interlaced = 0;
+    inpicref->height = outlink->h;
+    inpicref->interlaced_frame = 0;
 
     for (i = 0; i < field->nb_planes; i++) {
         if (field->type == FIELD_TYPE_BOTTOM)
@@ -123,8 +112,6 @@
     .description   = NULL_IF_CONFIG_SMALL("Extract a field from the input video."),
 
     .priv_size     = sizeof(FieldContext),
-    .init          = init,
-
     .inputs        = field_inputs,
     .outputs       = field_outputs,
     .priv_class    = &field_class,
diff --git a/libavfilter/vf_fieldmatch.c b/libavfilter/vf_fieldmatch.c
new file mode 100644
index 0000000..3495895
--- /dev/null
+++ b/libavfilter/vf_fieldmatch.c
@@ -0,0 +1,984 @@
+/*
+ * Copyright (c) 2012 Fredrik Mellbin
+ * Copyright (c) 2013 Clément Bœsch
+ *
+ * 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
+ * Fieldmatching filter, ported from VFM filter (VapouSsynth) by Clément.
+ * Fredrik Mellbin is the author of the VIVTC/VFM filter, which is itself a
+ * light clone of the TIVTC/TFM (AviSynth) filter written by Kevin Stone
+ * (tritical), the original author.
+ *
+ * @see http://bengal.missouri.edu/~kes25c/
+ * @see http://www.vapoursynth.com/about/
+ */
+
+#include <inttypes.h>
+
+#include "libavutil/avassert.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/opt.h"
+#include "libavutil/timestamp.h"
+#include "avfilter.h"
+#include "internal.h"
+
+#define INPUT_MAIN     0
+#define INPUT_CLEANSRC 1
+
+enum fieldmatch_parity {
+    FM_PARITY_AUTO   = -1,
+    FM_PARITY_BOTTOM =  0,
+    FM_PARITY_TOP    =  1,
+};
+
+enum matching_mode {
+    MODE_PC,
+    MODE_PC_N,
+    MODE_PC_U,
+    MODE_PC_N_UB,
+    MODE_PCN,
+    MODE_PCN_UB,
+    NB_MODE
+};
+
+enum comb_matching_mode {
+    COMBMATCH_NONE,
+    COMBMATCH_SC,
+    COMBMATCH_FULL,
+    NB_COMBMATCH
+};
+
+enum comb_dbg {
+    COMBDBG_NONE,
+    COMBDBG_PCN,
+    COMBDBG_PCNUB,
+    NB_COMBDBG
+};
+
+typedef struct {
+    const AVClass *class;
+
+    AVFrame *prv,  *src,  *nxt;     ///< main sliding window of 3 frames
+    AVFrame *prv2, *src2, *nxt2;    ///< sliding window of the optional second stream
+    int got_frame[2];               ///< frame request flag for each input stream
+    int hsub, vsub;                 ///< chroma subsampling values
+    uint32_t eof;                   ///< bitmask for end of stream
+    int64_t lastscdiff;
+    int64_t lastn;
+
+    /* options */
+    int order;
+    int ppsrc;
+    enum matching_mode mode;
+    int field;
+    int mchroma;
+    int y0, y1;
+    int64_t scthresh;
+    double scthresh_flt;
+    enum comb_matching_mode combmatch;
+    int combdbg;
+    int cthresh;
+    int chroma;
+    int blockx, blocky;
+    int combpel;
+
+    /* misc buffers */
+    uint8_t *map_data[4];
+    int map_linesize[4];
+    uint8_t *cmask_data[4];
+    int cmask_linesize[4];
+    int *c_array;
+    int tpitchy, tpitchuv;
+    uint8_t *tbuffer;
+} FieldMatchContext;
+
+#define OFFSET(x) offsetof(FieldMatchContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption fieldmatch_options[] = {
+    { "order", "specify the assumed field order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=FM_PARITY_AUTO}, -1, 1, FLAGS, "order" },
+        { "auto", "auto detect parity",        0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_AUTO},    INT_MIN, INT_MAX, FLAGS, "order" },
+        { "bff",  "assume bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_BOTTOM},  INT_MIN, INT_MAX, FLAGS, "order" },
+        { "tff",  "assume top field first",    0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_TOP},     INT_MIN, INT_MAX, FLAGS, "order" },
+    { "mode", "set the matching mode or strategy to use", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_PC_N}, MODE_PC, NB_MODE-1, FLAGS, "mode" },
+        { "pc",      "2-way match (p/c)",                                                                    0, AV_OPT_TYPE_CONST, {.i64=MODE_PC},      INT_MIN, INT_MAX, FLAGS, "mode" },
+        { "pc_n",    "2-way match + 3rd match on combed (p/c + u)",                                          0, AV_OPT_TYPE_CONST, {.i64=MODE_PC_N},    INT_MIN, INT_MAX, FLAGS, "mode" },
+        { "pc_u",    "2-way match + 3rd match (same order) on combed (p/c + u)",                             0, AV_OPT_TYPE_CONST, {.i64=MODE_PC_U},    INT_MIN, INT_MAX, FLAGS, "mode" },
+        { "pc_n_ub", "2-way match + 3rd match on combed + 4th/5th matches if still combed (p/c + u + u/b)",  0, AV_OPT_TYPE_CONST, {.i64=MODE_PC_N_UB}, INT_MIN, INT_MAX, FLAGS, "mode" },
+        { "pcn",     "3-way match (p/c/n)",                                                                  0, AV_OPT_TYPE_CONST, {.i64=MODE_PCN},     INT_MIN, INT_MAX, FLAGS, "mode" },
+        { "pcn_ub",  "3-way match + 4th/5th matches on combed (p/c/n + u/b)",                                0, AV_OPT_TYPE_CONST, {.i64=MODE_PCN_UB},  INT_MIN, INT_MAX, FLAGS, "mode" },
+    { "ppsrc", "mark main input as a pre-processed input and activate clean source input stream", OFFSET(ppsrc), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
+    { "field", "set the field to match from", OFFSET(field), AV_OPT_TYPE_INT, {.i64=FM_PARITY_AUTO}, -1, 1, FLAGS, "field" },
+        { "auto",   "automatic (same value as 'order')",    0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_AUTO},    INT_MIN, INT_MAX, FLAGS, "field" },
+        { "bottom", "bottom field",                         0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_BOTTOM},  INT_MIN, INT_MAX, FLAGS, "field" },
+        { "top",    "top field",                            0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_TOP},     INT_MIN, INT_MAX, FLAGS, "field" },
+    { "mchroma", "set whether or not chroma is included during the match comparisons", OFFSET(mchroma), AV_OPT_TYPE_INT, {.i64=1}, 0, 1,  FLAGS },
+    { "y0", "define an exclusion band which excludes the lines between y0 and y1 from the field matching decision", OFFSET(y0), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
+    { "y1", "define an exclusion band which excludes the lines between y0 and y1 from the field matching decision", OFFSET(y1), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
+    { "scthresh", "set scene change detection threshold", OFFSET(scthresh_flt), AV_OPT_TYPE_DOUBLE, {.dbl=12}, 0, 100, FLAGS },
+    { "combmatch", "set combmatching mode", OFFSET(combmatch), AV_OPT_TYPE_INT, {.i64=COMBMATCH_SC}, COMBMATCH_NONE, NB_COMBMATCH-1, FLAGS, "combmatching" },
+        { "none", "disable combmatching",                     0, AV_OPT_TYPE_CONST, {.i64=COMBMATCH_NONE}, INT_MIN, INT_MAX, FLAGS, "combmatching" },
+        { "sc",   "enable combmatching only on scene change", 0, AV_OPT_TYPE_CONST, {.i64=COMBMATCH_SC},   INT_MIN, INT_MAX, FLAGS, "combmatching" },
+        { "full", "enable combmatching all the time",         0, AV_OPT_TYPE_CONST, {.i64=COMBMATCH_FULL}, INT_MIN, INT_MAX, FLAGS, "combmatching" },
+    { "combdbg",   "enable comb debug", OFFSET(combdbg), AV_OPT_TYPE_INT, {.i64=COMBDBG_NONE}, COMBDBG_NONE, NB_COMBDBG-1, FLAGS, "dbglvl" },
+        { "none",  "no forced calculation", 0, AV_OPT_TYPE_CONST, {.i64=COMBDBG_NONE},  INT_MIN, INT_MAX, FLAGS, "dbglvl" },
+        { "pcn",   "calculate p/c/n",       0, AV_OPT_TYPE_CONST, {.i64=COMBDBG_PCN},   INT_MIN, INT_MAX, FLAGS, "dbglvl" },
+        { "pcnub", "calculate p/c/n/u/b",   0, AV_OPT_TYPE_CONST, {.i64=COMBDBG_PCNUB}, INT_MIN, INT_MAX, FLAGS, "dbglvl" },
+    { "cthresh", "set the area combing threshold used for combed frame detection",       OFFSET(cthresh), AV_OPT_TYPE_INT, {.i64= 9}, -1, 0xff, FLAGS },
+    { "chroma",  "set whether or not chroma is considered in the combed frame decision", OFFSET(chroma),  AV_OPT_TYPE_INT, {.i64= 0},  0,    1, FLAGS },
+    { "blockx",  "set the x-axis size of the window used during combed frame detection", OFFSET(blockx),  AV_OPT_TYPE_INT, {.i64=16},  4, 1<<9, FLAGS },
+    { "blocky",  "set the y-axis size of the window used during combed frame detection", OFFSET(blocky),  AV_OPT_TYPE_INT, {.i64=16},  4, 1<<9, FLAGS },
+    { "combpel", "set the number of combed pixels inside any of the blocky by blockx size blocks on the frame for the frame to be detected as combed", OFFSET(combpel), AV_OPT_TYPE_INT, {.i64=80}, 0, INT_MAX, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(fieldmatch);
+
+static int get_width(const FieldMatchContext *fm, const AVFrame *f, int plane)
+{
+    return plane ? f->width >> fm->hsub : f->width;
+}
+
+static int get_height(const FieldMatchContext *fm, const AVFrame *f, int plane)
+{
+    return plane ? f->height >> fm->vsub : f->height;
+}
+
+static int64_t luma_abs_diff(const AVFrame *f1, const AVFrame *f2)
+{
+    int x, y;
+    const uint8_t *srcp1 = f1->data[0];
+    const uint8_t *srcp2 = f2->data[0];
+    const int src1_linesize = f1->linesize[0];
+    const int src2_linesize = f2->linesize[0];
+    const int width  = f1->width;
+    const int height = f1->height;
+    int64_t acc = 0;
+
+    for (y = 0; y < height; y++) {
+        for (x = 0; x < width; x++)
+            acc += abs(srcp1[x] - srcp2[x]);
+        srcp1 += src1_linesize;
+        srcp2 += src2_linesize;
+    }
+    return acc;
+}
+
+static void fill_buf(uint8_t *data, int w, int h, int linesize, uint8_t v)
+{
+    int y;
+
+    for (y = 0; y < h; y++) {
+        memset(data, v, w);
+        data += linesize;
+    }
+}
+
+static int calc_combed_score(const FieldMatchContext *fm, const AVFrame *src)
+{
+    int x, y, plane, max_v = 0;
+    const int cthresh = fm->cthresh;
+    const int cthresh6 = cthresh * 6;
+
+    for (plane = 0; plane < (fm->chroma ? 3 : 1); plane++) {
+        const uint8_t *srcp = src->data[plane];
+        const int src_linesize = src->linesize[plane];
+        const int width  = get_width (fm, src, plane);
+        const int height = get_height(fm, src, plane);
+        uint8_t *cmkp = fm->cmask_data[plane];
+        const int cmk_linesize = fm->cmask_linesize[plane];
+
+        if (cthresh < 0) {
+            fill_buf(cmkp, width, height, cmk_linesize, 0xff);
+            continue;
+        }
+        fill_buf(cmkp, width, height, cmk_linesize, 0);
+
+        /* [1 -3 4 -3 1] vertical filter */
+#define FILTER(xm2, xm1, xp1, xp2) \
+        abs(  4 * srcp[x] \
+             -3 * (srcp[x + (xm1)*src_linesize] + srcp[x + (xp1)*src_linesize]) \
+             +    (srcp[x + (xm2)*src_linesize] + srcp[x + (xp2)*src_linesize])) > cthresh6
+
+        /* first line */
+        for (x = 0; x < width; x++) {
+            const int s1 = abs(srcp[x] - srcp[x + src_linesize]);
+            if (s1 > cthresh && FILTER(2, 1, 1, 2))
+                cmkp[x] = 0xff;
+        }
+        srcp += src_linesize;
+        cmkp += cmk_linesize;
+
+        /* second line */
+        for (x = 0; x < width; x++) {
+            const int s1 = abs(srcp[x] - srcp[x - src_linesize]);
+            const int s2 = abs(srcp[x] - srcp[x + src_linesize]);
+            if (s1 > cthresh && s2 > cthresh && FILTER(2, -1, 1, 2))
+                cmkp[x] = 0xff;
+        }
+        srcp += src_linesize;
+        cmkp += cmk_linesize;
+
+        /* all lines minus first two and last two */
+        for (y = 2; y < height-2; y++) {
+            for (x = 0; x < width; x++) {
+                const int s1 = abs(srcp[x] - srcp[x - src_linesize]);
+                const int s2 = abs(srcp[x] - srcp[x + src_linesize]);
+                if (s1 > cthresh && s2 > cthresh && FILTER(-2, -1, 1, 2))
+                    cmkp[x] = 0xff;
+            }
+            srcp += src_linesize;
+            cmkp += cmk_linesize;
+        }
+
+        /* before-last line */
+        for (x = 0; x < width; x++) {
+            const int s1 = abs(srcp[x] - srcp[x - src_linesize]);
+            const int s2 = abs(srcp[x] - srcp[x + src_linesize]);
+            if (s1 > cthresh && s2 > cthresh && FILTER(-2, -1, 1, -2))
+                cmkp[x] = 0xff;
+        }
+        srcp += src_linesize;
+        cmkp += cmk_linesize;
+
+        /* last line */
+        for (x = 0; x < width; x++) {
+            const int s1 = abs(srcp[x] - srcp[x - src_linesize]);
+            if (s1 > cthresh && FILTER(-2, -1, -1, -2))
+                cmkp[x] = 0xff;
+        }
+    }
+
+    if (fm->chroma) {
+        uint8_t *cmkp  = fm->cmask_data[0];
+        uint8_t *cmkpU = fm->cmask_data[1];
+        uint8_t *cmkpV = fm->cmask_data[2];
+        const int width  = src->width  >> fm->hsub;
+        const int height = src->height >> fm->vsub;
+        const int cmk_linesize   = fm->cmask_linesize[0] << 1;
+        const int cmk_linesizeUV = fm->cmask_linesize[2];
+        uint8_t *cmkpp  = cmkp - (cmk_linesize>>1);
+        uint8_t *cmkpn  = cmkp + (cmk_linesize>>1);
+        uint8_t *cmkpnn = cmkp +  cmk_linesize;
+        for (y = 1; y < height - 1; y++) {
+            cmkpp  += cmk_linesize;
+            cmkp   += cmk_linesize;
+            cmkpn  += cmk_linesize;
+            cmkpnn += cmk_linesize;
+            cmkpV  += cmk_linesizeUV;
+            cmkpU  += cmk_linesizeUV;
+            for (x = 1; x < width - 1; x++) {
+#define HAS_FF_AROUND(p, lz) (p[x-1 - lz] == 0xff || p[x - lz] == 0xff || p[x+1 - lz] == 0xff || \
+                              p[x-1     ] == 0xff ||                      p[x+1     ] == 0xff || \
+                              p[x-1 + lz] == 0xff || p[x + lz] == 0xff || p[x+1 + lz] == 0xff)
+                if ((cmkpV[x] == 0xff && HAS_FF_AROUND(cmkpV, cmk_linesizeUV)) ||
+                    (cmkpU[x] == 0xff && HAS_FF_AROUND(cmkpU, cmk_linesizeUV))) {
+                    ((uint16_t*)cmkp)[x]  = 0xffff;
+                    ((uint16_t*)cmkpn)[x] = 0xffff;
+                    if (y&1) ((uint16_t*)cmkpp)[x]  = 0xffff;
+                    else     ((uint16_t*)cmkpnn)[x] = 0xffff;
+                }
+            }
+        }
+    }
+
+    {
+        const int blockx = fm->blockx;
+        const int blocky = fm->blocky;
+        const int xhalf = blockx/2;
+        const int yhalf = blocky/2;
+        const int cmk_linesize = fm->cmask_linesize[0];
+        const uint8_t *cmkp    = fm->cmask_data[0] + cmk_linesize;
+        const int width  = src->width;
+        const int height = src->height;
+        const int xblocks = ((width+xhalf)/blockx) + 1;
+        const int xblocks4 = xblocks<<2;
+        const int yblocks = ((height+yhalf)/blocky) + 1;
+        int *c_array = fm->c_array;
+        const int arraysize = (xblocks*yblocks)<<2;
+        int      heighta = (height/(blocky/2))*(blocky/2);
+        const int widtha = (width /(blockx/2))*(blockx/2);
+        if (heighta == height)
+            heighta = height - yhalf;
+        memset(c_array, 0, arraysize * sizeof(*c_array));
+
+#define C_ARRAY_ADD(v) do {                         \
+    const int box1 = (x / blockx) * 4;              \
+    const int box2 = ((x + xhalf) / blockx) * 4;    \
+    c_array[temp1 + box1    ] += v;                 \
+    c_array[temp1 + box2 + 1] += v;                 \
+    c_array[temp2 + box1 + 2] += v;                 \
+    c_array[temp2 + box2 + 3] += v;                 \
+} while (0)
+
+#define VERTICAL_HALF(y_start, y_end) do {                                  \
+    for (y = y_start; y < y_end; y++) {                                     \
+        const int temp1 = (y / blocky) * xblocks4;                          \
+        const int temp2 = ((y + yhalf) / blocky) * xblocks4;                \
+        for (x = 0; x < width; x++)                                         \
+            if (cmkp[x - cmk_linesize] == 0xff &&                           \
+                cmkp[x               ] == 0xff &&                           \
+                cmkp[x + cmk_linesize] == 0xff)                             \
+                C_ARRAY_ADD(1);                                             \
+        cmkp += cmk_linesize;                                               \
+    }                                                                       \
+} while (0)
+
+        VERTICAL_HALF(1, yhalf);
+
+        for (y = yhalf; y < heighta; y += yhalf) {
+            const int temp1 = (y / blocky) * xblocks4;
+            const int temp2 = ((y + yhalf) / blocky) * xblocks4;
+
+            for (x = 0; x < widtha; x += xhalf) {
+                const uint8_t *cmkp_tmp = cmkp + x;
+                int u, v, sum = 0;
+                for (u = 0; u < yhalf; u++) {
+                    for (v = 0; v < xhalf; v++)
+                        if (cmkp_tmp[v - cmk_linesize] == 0xff &&
+                            cmkp_tmp[v               ] == 0xff &&
+                            cmkp_tmp[v + cmk_linesize] == 0xff)
+                            sum++;
+                    cmkp_tmp += cmk_linesize;
+                }
+                if (sum)
+                    C_ARRAY_ADD(sum);
+            }
+
+            for (x = widtha; x < width; x++) {
+                const uint8_t *cmkp_tmp = cmkp + x;
+                int u, sum = 0;
+                for (u = 0; u < yhalf; u++) {
+                    if (cmkp_tmp[-cmk_linesize] == 0xff &&
+                        cmkp_tmp[            0] == 0xff &&
+                        cmkp_tmp[ cmk_linesize] == 0xff)
+                        sum++;
+                    cmkp_tmp += cmk_linesize;
+                }
+                if (sum)
+                    C_ARRAY_ADD(sum);
+            }
+
+            cmkp += cmk_linesize * yhalf;
+        }
+
+        VERTICAL_HALF(heighta, height - 1);
+
+        for (x = 0; x < arraysize; x++)
+            if (c_array[x] > max_v)
+                max_v = c_array[x];
+    }
+    return max_v;
+}
+
+// the secret is that tbuffer is an interlaced, offset subset of all the lines
+static void build_abs_diff_mask(const uint8_t *prvp, int prv_linesize,
+                                const uint8_t *nxtp, int nxt_linesize,
+                                uint8_t *tbuffer,    int tbuf_linesize,
+                                int width, int height)
+{
+    int y, x;
+
+    prvp -= prv_linesize;
+    nxtp -= nxt_linesize;
+    for (y = 0; y < height; y++) {
+        for (x = 0; x < width; x++)
+            tbuffer[x] = FFABS(prvp[x] - nxtp[x]);
+        prvp += prv_linesize;
+        nxtp += nxt_linesize;
+        tbuffer += tbuf_linesize;
+    }
+}
+
+/**
+ * Build a map over which pixels differ a lot/a little
+ */
+static void build_diff_map(FieldMatchContext *fm,
+                           const uint8_t *prvp, int prv_linesize,
+                           const uint8_t *nxtp, int nxt_linesize,
+                           uint8_t *dstp, int dst_linesize, int height,
+                           int width, int plane)
+{
+    int x, y, u, diff, count;
+    int tpitch = plane ? fm->tpitchuv : fm->tpitchy;
+    const uint8_t *dp = fm->tbuffer + tpitch;
+
+    build_abs_diff_mask(prvp, prv_linesize, nxtp, nxt_linesize,
+                        fm->tbuffer, tpitch, width, height>>1);
+
+    for (y = 2; y < height - 2; y += 2) {
+        for (x = 1; x < width - 1; x++) {
+            diff = dp[x];
+            if (diff > 3) {
+                for (count = 0, u = x-1; u < x+2 && count < 2; u++) {
+                    count += dp[u-tpitch] > 3;
+                    count += dp[u       ] > 3;
+                    count += dp[u+tpitch] > 3;
+                }
+                if (count > 1) {
+                    dstp[x] = 1;
+                    if (diff > 19) {
+                        int upper = 0, lower = 0;
+                        for (count = 0, u = x-1; u < x+2 && count < 6; u++) {
+                            if (dp[u-tpitch] > 19) { count++; upper = 1; }
+                            if (dp[u       ] > 19)   count++;
+                            if (dp[u+tpitch] > 19) { count++; lower = 1; }
+                        }
+                        if (count > 3) {
+                            if (upper && lower) {
+                                dstp[x] |= 1<<1;
+                            } else {
+                                int upper2 = 0, lower2 = 0;
+                                for (u = FFMAX(x-4,0); u < FFMIN(x+5,width); u++) {
+                                    if (y != 2 &&        dp[u-2*tpitch] > 19) upper2 = 1;
+                                    if (                 dp[u-  tpitch] > 19) upper  = 1;
+                                    if (                 dp[u+  tpitch] > 19) lower  = 1;
+                                    if (y != height-4 && dp[u+2*tpitch] > 19) lower2 = 1;
+                                }
+                                if ((upper && (lower || upper2)) ||
+                                    (lower && (upper || lower2)))
+                                    dstp[x] |= 1<<1;
+                                else if (count > 5)
+                                    dstp[x] |= 1<<2;
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        dp += tpitch;
+        dstp += dst_linesize;
+    }
+}
+
+enum { mP, mC, mN, mB, mU };
+
+static int get_field_base(int match, int field)
+{
+    return match < 3 ? 2 - field : 1 + field;
+}
+
+static AVFrame *select_frame(FieldMatchContext *fm, int match)
+{
+    if      (match == mP || match == mB) return fm->prv;
+    else if (match == mN || match == mU) return fm->nxt;
+    else  /* match == mC */              return fm->src;
+}
+
+static int compare_fields(FieldMatchContext *fm, int match1, int match2, int field)
+{
+    int plane, ret;
+    uint64_t accumPc = 0, accumPm = 0, accumPml = 0;
+    uint64_t accumNc = 0, accumNm = 0, accumNml = 0;
+    int norm1, norm2, mtn1, mtn2;
+    float c1, c2, mr;
+    const AVFrame *src = fm->src;
+
+    for (plane = 0; plane < (fm->mchroma ? 3 : 1); plane++) {
+        int x, y, temp1, temp2, fbase;
+        const AVFrame *prev, *next;
+        uint8_t *mapp    = fm->map_data[plane];
+        int map_linesize = fm->map_linesize[plane];
+        const uint8_t *srcp = src->data[plane];
+        const int src_linesize  = src->linesize[plane];
+        const int srcf_linesize = src_linesize << 1;
+        int prv_linesize,  nxt_linesize;
+        int prvf_linesize, nxtf_linesize;
+        const int width  = get_width (fm, src, plane);
+        const int height = get_height(fm, src, plane);
+        const int y0a = fm->y0 >> (plane != 0);
+        const int y1a = fm->y1 >> (plane != 0);
+        const int startx = (plane == 0 ? 8 : 4);
+        const int stopx  = width - startx;
+        const uint8_t *srcpf, *srcf, *srcnf;
+        const uint8_t *prvpf, *prvnf, *nxtpf, *nxtnf;
+
+        fill_buf(mapp, width, height, map_linesize, 0);
+
+        /* match1 */
+        fbase = get_field_base(match1, field);
+        srcf  = srcp + (fbase + 1) * src_linesize;
+        srcpf = srcf - srcf_linesize;
+        srcnf = srcf + srcf_linesize;
+        mapp  = mapp + fbase * map_linesize;
+        prev = select_frame(fm, match1);
+        prv_linesize  = prev->linesize[plane];
+        prvf_linesize = prv_linesize << 1;
+        prvpf = prev->data[plane] + fbase * prv_linesize;   // previous frame, previous field
+        prvnf = prvpf + prvf_linesize;                      // previous frame, next     field
+
+        /* match2 */
+        fbase = get_field_base(match2, field);
+        next = select_frame(fm, match2);
+        nxt_linesize  = next->linesize[plane];
+        nxtf_linesize = nxt_linesize << 1;
+        nxtpf = next->data[plane] + fbase * nxt_linesize;   // next frame, previous field
+        nxtnf = nxtpf + nxtf_linesize;                      // next frame, next     field
+
+        map_linesize <<= 1;
+        if ((match1 >= 3 && field == 1) || (match1 < 3 && field != 1))
+            build_diff_map(fm, prvpf, prvf_linesize, nxtpf, nxtf_linesize,
+                           mapp, map_linesize, height, width, plane);
+        else
+            build_diff_map(fm, prvnf, prvf_linesize, nxtnf, nxtf_linesize,
+                           mapp + map_linesize, map_linesize, height, width, plane);
+
+        for (y = 2; y < height - 2; y += 2) {
+            if (y0a == y1a || y < y0a || y > y1a) {
+                for (x = startx; x < stopx; x++) {
+                    if (mapp[x] > 0 || mapp[x + map_linesize] > 0) {
+                        temp1 = srcpf[x] + (srcf[x] << 2) + srcnf[x]; // [1 4 1]
+
+                        temp2 = abs(3 * (prvpf[x] + prvnf[x]) - temp1);
+                        if (temp2 > 23 && ((mapp[x]&1) || (mapp[x + map_linesize]&1)))
+                            accumPc += temp2;
+                        if (temp2 > 42) {
+                            if ((mapp[x]&2) || (mapp[x + map_linesize]&2))
+                                accumPm += temp2;
+                            if ((mapp[x]&4) || (mapp[x + map_linesize]&4))
+                                accumPml += temp2;
+                        }
+
+                        temp2 = abs(3 * (nxtpf[x] + nxtnf[x]) - temp1);
+                        if (temp2 > 23 && ((mapp[x]&1) || (mapp[x + map_linesize]&1)))
+                            accumNc += temp2;
+                        if (temp2 > 42) {
+                            if ((mapp[x]&2) || (mapp[x + map_linesize]&2))
+                                accumNm += temp2;
+                            if ((mapp[x]&4) || (mapp[x + map_linesize]&4))
+                                accumNml += temp2;
+                        }
+                    }
+                }
+            }
+            prvpf += prvf_linesize;
+            prvnf += prvf_linesize;
+            srcpf += srcf_linesize;
+            srcf  += srcf_linesize;
+            srcnf += srcf_linesize;
+            nxtpf += nxtf_linesize;
+            nxtnf += nxtf_linesize;
+            mapp  += map_linesize;
+        }
+    }
+
+    if (accumPm < 500 && accumNm < 500 && (accumPml >= 500 || accumNml >= 500) &&
+        FFMAX(accumPml,accumNml) > 3*FFMIN(accumPml,accumNml)) {
+        accumPm = accumPml;
+        accumNm = accumNml;
+    }
+
+    norm1 = (int)((accumPc / 6.0f) + 0.5f);
+    norm2 = (int)((accumNc / 6.0f) + 0.5f);
+    mtn1  = (int)((accumPm / 6.0f) + 0.5f);
+    mtn2  = (int)((accumNm / 6.0f) + 0.5f);
+    c1 = ((float)FFMAX(norm1,norm2)) / ((float)FFMAX(FFMIN(norm1,norm2),1));
+    c2 = ((float)FFMAX(mtn1, mtn2))  / ((float)FFMAX(FFMIN(mtn1, mtn2), 1));
+    mr = ((float)FFMAX(mtn1, mtn2))  / ((float)FFMAX(FFMAX(norm1,norm2),1));
+    if (((mtn1 >=  500 || mtn2 >=  500) && (mtn1*2 < mtn2*1 || mtn2*2 < mtn1*1)) ||
+        ((mtn1 >= 1000 || mtn2 >= 1000) && (mtn1*3 < mtn2*2 || mtn2*3 < mtn1*2)) ||
+        ((mtn1 >= 2000 || mtn2 >= 2000) && (mtn1*5 < mtn2*4 || mtn2*5 < mtn1*4)) ||
+        ((mtn1 >= 4000 || mtn2 >= 4000) && c2 > c1))
+        ret = mtn1 > mtn2 ? match2 : match1;
+    else if (mr > 0.005 && FFMAX(mtn1, mtn2) > 150 && (mtn1*2 < mtn2*1 || mtn2*2 < mtn1*1))
+        ret = mtn1 > mtn2 ? match2 : match1;
+    else
+        ret = norm1 > norm2 ? match2 : match1;
+    return ret;
+}
+
+static void copy_fields(const FieldMatchContext *fm, AVFrame *dst,
+                        const AVFrame *src, int field)
+{
+    int plane;
+    for (plane = 0; plane < 4 && src->data[plane]; plane++)
+        av_image_copy_plane(dst->data[plane] + field*dst->linesize[plane], dst->linesize[plane] << 1,
+                            src->data[plane] + field*src->linesize[plane], src->linesize[plane] << 1,
+                            get_width(fm, src, plane), get_height(fm, src, plane) / 2);
+}
+
+static AVFrame *create_weave_frame(AVFilterContext *ctx, int match, int field,
+                                   const AVFrame *prv, AVFrame *src, const AVFrame *nxt)
+{
+    AVFrame *dst;
+    FieldMatchContext *fm = ctx->priv;
+
+    if (match == mC) {
+        dst = av_frame_clone(src);
+    } else {
+        AVFilterLink *outlink = ctx->outputs[0];
+
+        dst = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+        if (!dst)
+            return NULL;
+        av_frame_copy_props(dst, src);
+
+        switch (match) {
+        case mP: copy_fields(fm, dst, src, 1-field); copy_fields(fm, dst, prv,   field); break;
+        case mN: copy_fields(fm, dst, src, 1-field); copy_fields(fm, dst, nxt,   field); break;
+        case mB: copy_fields(fm, dst, src,   field); copy_fields(fm, dst, prv, 1-field); break;
+        case mU: copy_fields(fm, dst, src,   field); copy_fields(fm, dst, nxt, 1-field); break;
+        default: av_assert0(0);
+        }
+    }
+    return dst;
+}
+
+static int checkmm(AVFilterContext *ctx, int *combs, int m1, int m2,
+                   AVFrame **gen_frames, int field)
+{
+    const FieldMatchContext *fm = ctx->priv;
+
+#define LOAD_COMB(mid) do {                                                     \
+    if (combs[mid] < 0) {                                                       \
+        if (!gen_frames[mid])                                                   \
+            gen_frames[mid] = create_weave_frame(ctx, mid, field,               \
+                                                 fm->prv, fm->src, fm->nxt);    \
+        combs[mid] = calc_combed_score(fm, gen_frames[mid]);                    \
+    }                                                                           \
+} while (0)
+
+    LOAD_COMB(m1);
+    LOAD_COMB(m2);
+
+    if ((combs[m2] * 3 < combs[m1] || (combs[m2] * 2 < combs[m1] && combs[m1] > fm->combpel)) &&
+        abs(combs[m2] - combs[m1]) >= 30 && combs[m2] < fm->combpel)
+        return m2;
+    else
+        return m1;
+}
+
+static const int fxo0m[] = { mP, mC, mN, mB, mU };
+static const int fxo1m[] = { mN, mC, mP, mU, mB };
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx  = inlink->dst;
+    AVFilterLink *outlink = ctx->outputs[0];
+    FieldMatchContext *fm = ctx->priv;
+    int combs[] = { -1, -1, -1, -1, -1 };
+    int order, field, i, match, sc = 0;
+    const int *fxo;
+    AVFrame *gen_frames[] = { NULL, NULL, NULL, NULL, NULL };
+    AVFrame *dst;
+
+    /* update frames queue(s) */
+#define SLIDING_FRAME_WINDOW(prv, src, nxt) do {                \
+        if (prv != src) /* 2nd loop exception (1st has prv==src and we don't want to loose src) */ \
+            av_frame_free(&prv);                                \
+        prv = src;                                              \
+        src = nxt;                                              \
+        if (in)                                                 \
+            nxt = in;                                           \
+        if (!prv)                                               \
+            prv = src;                                          \
+        if (!prv) /* received only one frame at that point */   \
+            return 0;                                           \
+        av_assert0(prv && src && nxt);                          \
+} while (0)
+    if (FF_INLINK_IDX(inlink) == INPUT_MAIN) {
+        SLIDING_FRAME_WINDOW(fm->prv, fm->src, fm->nxt);
+        fm->got_frame[INPUT_MAIN] = 1;
+    } else {
+        SLIDING_FRAME_WINDOW(fm->prv2, fm->src2, fm->nxt2);
+        fm->got_frame[INPUT_CLEANSRC] = 1;
+    }
+    if (!fm->got_frame[INPUT_MAIN] || (fm->ppsrc && !fm->got_frame[INPUT_CLEANSRC]))
+        return 0;
+    fm->got_frame[INPUT_MAIN] = fm->got_frame[INPUT_CLEANSRC] = 0;
+    in = fm->src;
+
+    /* parity */
+    order = fm->order != FM_PARITY_AUTO ? fm->order : (in->interlaced_frame ? in->top_field_first : 1);
+    field = fm->field != FM_PARITY_AUTO ? fm->field : order;
+    av_assert0(order == 0 || order == 1 || field == 0 || field == 1);
+    fxo = field ^ order ? fxo1m : fxo0m;
+
+    /* debug mode: we generate all the fields combinations and their associated
+     * combed score. XXX: inject as frame metadata? */
+    if (fm->combdbg) {
+        for (i = 0; i < FF_ARRAY_ELEMS(combs); i++) {
+            if (i > mN && fm->combdbg == COMBDBG_PCN)
+                break;
+            gen_frames[i] = create_weave_frame(ctx, i, field, fm->prv, fm->src, fm->nxt);
+            if (!gen_frames[i])
+                return AVERROR(ENOMEM);
+            combs[i] = calc_combed_score(fm, gen_frames[i]);
+        }
+        av_log(ctx, AV_LOG_INFO, "COMBS: %3d %3d %3d %3d %3d\n",
+               combs[0], combs[1], combs[2], combs[3], combs[4]);
+    } else {
+        gen_frames[mC] = av_frame_clone(fm->src);
+        if (!gen_frames[mC])
+            return AVERROR(ENOMEM);
+    }
+
+    /* p/c selection and optional 3-way p/c/n matches */
+    match = compare_fields(fm, fxo[mC], fxo[mP], field);
+    if (fm->mode == MODE_PCN || fm->mode == MODE_PCN_UB)
+        match = compare_fields(fm, match, fxo[mN], field);
+
+    /* scene change check */
+    if (fm->combmatch == COMBMATCH_SC) {
+        if (fm->lastn == outlink->frame_count - 1) {
+            if (fm->lastscdiff > fm->scthresh)
+                sc = 1;
+        } else if (luma_abs_diff(fm->prv, fm->src) > fm->scthresh) {
+            sc = 1;
+        }
+
+        if (!sc) {
+            fm->lastn = outlink->frame_count;
+            fm->lastscdiff = luma_abs_diff(fm->src, fm->nxt);
+            sc = fm->lastscdiff > fm->scthresh;
+        }
+    }
+
+    if (fm->combmatch == COMBMATCH_FULL || (fm->combmatch == COMBMATCH_SC && sc)) {
+        switch (fm->mode) {
+        /* 2-way p/c matches */
+        case MODE_PC:
+            match = checkmm(ctx, combs, match, match == fxo[mP] ? fxo[mC] : fxo[mP], gen_frames, field);
+            break;
+        case MODE_PC_N:
+            match = checkmm(ctx, combs, match, fxo[mN], gen_frames, field);
+            break;
+        case MODE_PC_U:
+            match = checkmm(ctx, combs, match, fxo[mU], gen_frames, field);
+            break;
+        case MODE_PC_N_UB:
+            match = checkmm(ctx, combs, match, fxo[mN], gen_frames, field);
+            match = checkmm(ctx, combs, match, fxo[mU], gen_frames, field);
+            match = checkmm(ctx, combs, match, fxo[mB], gen_frames, field);
+            break;
+        /* 3-way p/c/n matches */
+        case MODE_PCN:
+            match = checkmm(ctx, combs, match, match == fxo[mP] ? fxo[mC] : fxo[mP], gen_frames, field);
+            break;
+        case MODE_PCN_UB:
+            match = checkmm(ctx, combs, match, fxo[mU], gen_frames, field);
+            match = checkmm(ctx, combs, match, fxo[mB], gen_frames, field);
+            break;
+        default:
+            av_assert0(0);
+        }
+    }
+
+    /* get output frame and drop the others */
+    if (fm->ppsrc) {
+        /* field matching was based on a filtered/post-processed input, we now
+         * pick the untouched fields from the clean source */
+        dst = create_weave_frame(ctx, match, field, fm->prv2, fm->src2, fm->nxt2);
+    } else {
+        if (!gen_frames[match]) { // XXX: is that possible?
+            dst = create_weave_frame(ctx, match, field, fm->prv, fm->src, fm->nxt);
+        } else {
+            dst = gen_frames[match];
+            gen_frames[match] = NULL;
+        }
+    }
+    if (!dst)
+        return AVERROR(ENOMEM);
+    for (i = 0; i < FF_ARRAY_ELEMS(gen_frames); i++)
+        av_frame_free(&gen_frames[i]);
+
+    /* mark the frame we are unable to match properly as interlaced so a proper
+     * de-interlacer can take the relay */
+    dst->interlaced_frame = combs[match] >= fm->combpel;
+    if (dst->interlaced_frame) {
+        av_log(ctx, AV_LOG_WARNING, "Frame #%"PRId64" at %s is still interlaced\n",
+               outlink->frame_count, av_ts2timestr(in->pts, &inlink->time_base));
+        dst->top_field_first = field;
+    }
+
+    av_log(ctx, AV_LOG_DEBUG, "SC:%d | COMBS: %3d %3d %3d %3d %3d (combpel=%d)"
+           " match=%d combed=%s\n", sc, combs[0], combs[1], combs[2], combs[3], combs[4],
+           fm->combpel, match, dst->interlaced_frame ? "YES" : "NO");
+
+    return ff_filter_frame(outlink, dst);
+}
+
+static int request_inlink(AVFilterContext *ctx, int lid)
+{
+    int ret = 0;
+    FieldMatchContext *fm = ctx->priv;
+
+    if (!fm->got_frame[lid]) {
+        AVFilterLink *inlink = ctx->inputs[lid];
+        ret = ff_request_frame(inlink);
+        if (ret == AVERROR_EOF) { // flushing
+            fm->eof |= 1 << lid;
+            ret = filter_frame(inlink, NULL);
+        }
+    }
+    return ret;
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+    int ret;
+    AVFilterContext *ctx = outlink->src;
+    FieldMatchContext *fm = ctx->priv;
+    const uint32_t eof_mask = 1<<INPUT_MAIN | fm->ppsrc<<INPUT_CLEANSRC;
+
+    if ((fm->eof & eof_mask) == eof_mask) // flush done?
+        return AVERROR_EOF;
+    if ((ret = request_inlink(ctx, INPUT_MAIN)) < 0)
+        return ret;
+    if (fm->ppsrc && (ret = request_inlink(ctx, INPUT_CLEANSRC)) < 0)
+        return ret;
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    // TODO: second input source can support >8bit depth
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
+        AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P,
+        AV_PIX_FMT_NONE
+    };
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+    return 0;
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    int ret;
+    AVFilterContext *ctx = inlink->dst;
+    FieldMatchContext *fm = ctx->priv;
+    const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
+    const int w = inlink->w;
+    const int h = inlink->h;
+
+    fm->scthresh = (int64_t)((w * h * 255.0 * fm->scthresh_flt) / 100.0);
+
+    if ((ret = av_image_alloc(fm->map_data,   fm->map_linesize,   w, h, inlink->format, 32)) < 0 ||
+        (ret = av_image_alloc(fm->cmask_data, fm->cmask_linesize, w, h, inlink->format, 32)) < 0)
+        return ret;
+
+    fm->hsub = pix_desc->log2_chroma_w;
+    fm->vsub = pix_desc->log2_chroma_h;
+
+    fm->tpitchy  = FFALIGN(w,      16);
+    fm->tpitchuv = FFALIGN(w >> 1, 16);
+
+    fm->tbuffer = av_malloc(h/2 * fm->tpitchy);
+    fm->c_array = av_malloc((((w + fm->blockx/2)/fm->blockx)+1) *
+                            (((h + fm->blocky/2)/fm->blocky)+1) *
+                            4 * sizeof(*fm->c_array));
+    if (!fm->tbuffer || !fm->c_array)
+        return AVERROR(ENOMEM);
+
+    return 0;
+}
+
+static av_cold int fieldmatch_init(AVFilterContext *ctx)
+{
+    const FieldMatchContext *fm = ctx->priv;
+    AVFilterPad pad = {
+        .name         = av_strdup("main"),
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+        .config_props = config_input,
+    };
+
+    if (!pad.name)
+        return AVERROR(ENOMEM);
+    ff_insert_inpad(ctx, INPUT_MAIN, &pad);
+
+    if (fm->ppsrc) {
+        pad.name = av_strdup("clean_src");
+        pad.config_props = NULL;
+        if (!pad.name)
+            return AVERROR(ENOMEM);
+        ff_insert_inpad(ctx, INPUT_CLEANSRC, &pad);
+    }
+
+    if ((fm->blockx & (fm->blockx - 1)) ||
+        (fm->blocky & (fm->blocky - 1))) {
+        av_log(ctx, AV_LOG_ERROR, "blockx and blocky settings must be power of two\n");
+        return AVERROR(EINVAL);
+    }
+
+    if (fm->combpel > fm->blockx * fm->blocky) {
+        av_log(ctx, AV_LOG_ERROR, "Combed pixel should not be larger than blockx x blocky\n");
+        return AVERROR(EINVAL);
+    }
+
+    return 0;
+}
+
+static av_cold void fieldmatch_uninit(AVFilterContext *ctx)
+{
+    int i;
+    FieldMatchContext *fm = ctx->priv;
+
+    if (fm->prv != fm->src)
+        av_frame_free(&fm->prv);
+    if (fm->nxt != fm->src)
+        av_frame_free(&fm->nxt);
+    av_frame_free(&fm->src);
+    av_freep(&fm->map_data[0]);
+    av_freep(&fm->cmask_data[0]);
+    av_freep(&fm->tbuffer);
+    av_freep(&fm->c_array);
+    for (i = 0; i < ctx->nb_inputs; i++)
+        av_freep(&ctx->input_pads[i].name);
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx  = outlink->src;
+    const FieldMatchContext *fm = ctx->priv;
+    const AVFilterLink *inlink =
+        ctx->inputs[fm->ppsrc ? INPUT_CLEANSRC : INPUT_MAIN];
+
+    outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
+    outlink->time_base = inlink->time_base;
+    outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
+    outlink->frame_rate = inlink->frame_rate;
+    outlink->w = inlink->w;
+    outlink->h = inlink->h;
+    return 0;
+}
+
+static const AVFilterPad fieldmatch_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .request_frame = request_frame,
+        .config_props  = config_output,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_vf_fieldmatch = {
+    .name           = "fieldmatch",
+    .description    = NULL_IF_CONFIG_SMALL("Field matching for inverse telecine."),
+    .query_formats  = query_formats,
+    .priv_size      = sizeof(FieldMatchContext),
+    .init           = fieldmatch_init,
+    .uninit         = fieldmatch_uninit,
+    .inputs         = NULL,
+    .outputs        = fieldmatch_outputs,
+    .priv_class     = &fieldmatch_class,
+    .flags          = AVFILTER_FLAG_DYNAMIC_INPUTS,
+};
diff --git a/libavfilter/vf_fieldorder.c b/libavfilter/vf_fieldorder.c
index 06e0369..653411d 100644
--- a/libavfilter/vf_fieldorder.c
+++ b/libavfilter/vf_fieldorder.c
@@ -23,51 +23,22 @@
  * video field order filter, heavily influenced by vf_pad.c
  */
 
-/* #define DEBUG */
-
-#include <stdio.h>
-#include <string.h>
-
+#include "libavutil/opt.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
+#include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "avfilter.h"
 #include "formats.h"
 #include "internal.h"
 #include "video.h"
 
-typedef struct
-{
-    unsigned int dst_tff;      ///< output bff/tff
+typedef struct {
+    const AVClass *class;
+    int dst_tff;               ///< output bff/tff
     int          line_size[4]; ///< bytes of pixel data per line for each plane
 } FieldOrderContext;
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    FieldOrderContext *fieldorder = ctx->priv;
-
-    const char *tff = "tff";
-    const char *bff = "bff";
-
-    if (!args) {
-        fieldorder->dst_tff = 1;
-    } else if (sscanf(args, "%u", &fieldorder->dst_tff) == 1) {
-        fieldorder->dst_tff = !!fieldorder->dst_tff;
-    } else if (!strcmp(tff, args)) {
-        fieldorder->dst_tff = 1;
-    } else if (!strcmp(bff, args)) {
-        fieldorder->dst_tff = 0;
-    } else {
-        av_log(ctx, AV_LOG_ERROR, "Invalid argument '%s'.\n", args);
-        return AVERROR(EINVAL);
-    }
-
-    av_log(ctx, AV_LOG_VERBOSE, "output field order: %s\n",
-            fieldorder->dst_tff ? tff : bff);
-
-    return 0;
-}
-
 static int query_formats(AVFilterContext *ctx)
 {
     AVFilterFormats  *formats;
@@ -113,15 +84,15 @@
     return 0;
 }
 
-static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
+static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
 {
     AVFilterContext   *ctx        = inlink->dst;
     AVFilterLink      *outlink    = ctx->outputs[0];
 
-    return ff_get_video_buffer(outlink, perms, w, h);
+    return ff_get_video_buffer(outlink, w, h);
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
 {
     AVFilterContext   *ctx     = inlink->dst;
     FieldOrderContext *s       = ctx->priv;
@@ -129,14 +100,14 @@
     int h, plane, line_step, line_size, line;
     uint8_t *data;
 
-    if (!frame->video->interlaced ||
-        frame->video->top_field_first == s->dst_tff)
+    if (!frame->interlaced_frame ||
+        frame->top_field_first == s->dst_tff)
         return ff_filter_frame(outlink, frame);
 
     av_dlog(ctx,
             "picture will move %s one line\n",
             s->dst_tff ? "up" : "down");
-    h = frame->video->h;
+    h = frame->height;
     for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
         line_step = frame->linesize[plane];
         line_size = s->line_size[plane];
@@ -148,7 +119,7 @@
              *  The new last line is created as a copy of the
              *  penultimate line from that field. */
             for (line = 0; line < h; line++) {
-                if (1 + line < frame->video->h) {
+                if (1 + line < frame->height) {
                     memcpy(data, data + line_step, line_size);
                 } else {
                     memcpy(data, data - line_step - line_step, line_size);
@@ -172,11 +143,23 @@
             }
         }
     }
-    frame->video->top_field_first = s->dst_tff;
+    frame->top_field_first = s->dst_tff;
 
     return ff_filter_frame(outlink, frame);
 }
 
+#define OFFSET(x) offsetof(FieldOrderContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption fieldorder_options[] = {
+    { "order", "output field order", OFFSET(dst_tff), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "order" },
+        { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .flags=FLAGS, .unit = "order" },
+        { "tff", "top field first",    0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .flags=FLAGS, .unit = "order" },
+    { NULL },
+};
+
+AVFILTER_DEFINE_CLASS(fieldorder);
+
 static const AVFilterPad avfilter_vf_fieldorder_inputs[] = {
     {
         .name             = "default",
@@ -184,7 +167,7 @@
         .config_props     = config_input,
         .get_video_buffer = get_video_buffer,
         .filter_frame     = filter_frame,
-        .min_perms        = AV_PERM_READ | AV_PERM_WRITE,
+        .needs_writable   = 1,
     },
     { NULL }
 };
@@ -200,8 +183,8 @@
 AVFilter avfilter_vf_fieldorder = {
     .name          = "fieldorder",
     .description   = NULL_IF_CONFIG_SMALL("Set the field order."),
-    .init          = init,
     .priv_size     = sizeof(FieldOrderContext),
+    .priv_class    = &fieldorder_class,
     .query_formats = query_formats,
     .inputs        = avfilter_vf_fieldorder_inputs,
     .outputs       = avfilter_vf_fieldorder_outputs,
diff --git a/libavfilter/vf_format.c b/libavfilter/vf_format.c
index df3c77a..56ce74c 100644
--- a/libavfilter/vf_format.c
+++ b/libavfilter/vf_format.c
@@ -28,6 +28,8 @@
 #include "libavutil/internal.h"
 #include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
+#include "libavutil/opt.h"
+
 #include "avfilter.h"
 #include "internal.h"
 #include "formats.h"
@@ -35,6 +37,8 @@
 #include "video.h"
 
 typedef struct {
+    const AVClass *class;
+    char *pix_fmts;
     /**
      * List of flags telling if a given image format has been listed
      * as argument to the filter.
@@ -44,7 +48,7 @@
 
 #define AV_PIX_FMT_NAME_MAXSIZE 32
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     FormatContext *format = ctx->priv;
     const char *cur, *sep;
@@ -53,8 +57,8 @@
     enum AVPixelFormat pix_fmt;
 
     /* parse the list of formats */
-    for (cur = args; cur; cur = sep ? sep+1 : NULL) {
-        if (!(sep = strchr(cur, ':')))
+    for (cur = format->pix_fmts; cur; cur = sep ? sep + 1 : NULL) {
+        if (!(sep = strchr(cur, '|')))
             pix_fmt_name_len = strlen(cur);
         else
             pix_fmt_name_len = sep - cur;
@@ -90,6 +94,12 @@
     return formats;
 }
 
+#define OFFSET(x) offsetof(FormatContext, x)
+static const AVOption options[] = {
+    { "pix_fmts", "A '|'-separated list of pixel formats", OFFSET(pix_fmts), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM },
+    { NULL },
+};
+
 #if CONFIG_FORMAT_FILTER
 static int query_formats_format(AVFilterContext *ctx)
 {
@@ -97,6 +107,13 @@
     return 0;
 }
 
+static const AVClass format_class = {
+    .class_name = "format",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 static const AVFilterPad avfilter_vf_format_inputs[] = {
     {
         .name             = "default",
@@ -123,6 +140,7 @@
     .query_formats = query_formats_format,
 
     .priv_size = sizeof(FormatContext),
+    .priv_class = &format_class,
 
     .inputs    = avfilter_vf_format_inputs,
     .outputs   = avfilter_vf_format_outputs,
@@ -136,6 +154,13 @@
     return 0;
 }
 
+static const AVClass noformat_class = {
+    .class_name = "noformat",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 static const AVFilterPad avfilter_vf_noformat_inputs[] = {
     {
         .name             = "default",
@@ -162,6 +187,7 @@
     .query_formats = query_formats_noformat,
 
     .priv_size = sizeof(FormatContext),
+    .priv_class = &noformat_class,
 
     .inputs    = avfilter_vf_noformat_inputs,
     .outputs   = avfilter_vf_noformat_outputs,
diff --git a/libavfilter/vf_fps.c b/libavfilter/vf_fps.c
index 29eedc7..6a67158 100644
--- a/libavfilter/vf_fps.c
+++ b/libavfilter/vf_fps.c
@@ -45,7 +45,6 @@
     int64_t pts;            ///< pts of the first frame currently in the fifo
 
     AVRational framerate;   ///< target framerate
-    char *fps;              ///< a string describing target framerate
     int rounding;           ///< AVRounding method for timestamps
 
     /* statistics */
@@ -59,7 +58,7 @@
 #define V AV_OPT_FLAG_VIDEO_PARAM
 #define F AV_OPT_FLAG_FILTERING_PARAM
 static const AVOption fps_options[] = {
-    { "fps", "A string describing desired output framerate", OFFSET(fps), AV_OPT_TYPE_STRING, { .str = "25" }, .flags = V|F },
+    { "fps", "A string describing desired output framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, .flags = V|F },
     { "round", "set rounding method for timestamps", OFFSET(rounding), AV_OPT_TYPE_INT, { .i64 = AV_ROUND_NEAR_INF }, 0, 5, V|F, "round" },
     { "zero", "round towards 0",      OFFSET(rounding), AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_ZERO     }, 0, 5, V|F, "round" },
     { "inf",  "round away from 0",    OFFSET(rounding), AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_INF      }, 0, 5, V|F, "round" },
@@ -71,25 +70,11 @@
 
 AVFILTER_DEFINE_CLASS(fps);
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     FPSContext *s = ctx->priv;
-    const char *shorthand[] = { "fps", "round", NULL };
-    int ret;
 
-    s->class = &fps_class;
-    av_opt_set_defaults(s);
-
-    if ((ret = av_opt_set_from_string(s, args, shorthand, "=", ":")) < 0)
-        return ret;
-
-    if ((ret = av_parse_video_rate(&s->framerate, s->fps)) < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Error parsing framerate %s.\n", s->fps);
-        return ret;
-    }
-    av_opt_free(s);
-
-    if (!(s->fifo = av_fifo_alloc(2*sizeof(AVFilterBufferRef*))))
+    if (!(s->fifo = av_fifo_alloc(2*sizeof(AVFrame*))))
         return AVERROR(ENOMEM);
 
     av_log(ctx, AV_LOG_VERBOSE, "fps=%d/%d\n", s->framerate.num, s->framerate.den);
@@ -99,9 +84,9 @@
 static void flush_fifo(AVFifoBuffer *fifo)
 {
     while (av_fifo_size(fifo)) {
-        AVFilterBufferRef *tmp;
+        AVFrame *tmp;
         av_fifo_generic_read(fifo, &tmp, sizeof(tmp), NULL);
-        avfilter_unref_buffer(tmp);
+        av_frame_free(&tmp);
     }
 }
 
@@ -109,7 +94,7 @@
 {
     FPSContext *s = ctx->priv;
     if (s->fifo) {
-        s->drop += av_fifo_size(s->fifo) / sizeof(AVFilterBufferRef*);
+        s->drop += av_fifo_size(s->fifo) / sizeof(AVFrame*);
         flush_fifo(s->fifo);
         av_fifo_free(s->fifo);
     }
@@ -145,7 +130,7 @@
     if (ret == AVERROR_EOF && av_fifo_size(s->fifo)) {
         int i;
         for (i = 0; av_fifo_size(s->fifo); i++) {
-            AVFilterBufferRef *buf;
+            AVFrame *buf;
 
             av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
             buf->pts = av_rescale_q(s->first_pts, ctx->inputs[0]->time_base,
@@ -162,13 +147,13 @@
     return ret;
 }
 
-static int write_to_fifo(AVFifoBuffer *fifo, AVFilterBufferRef *buf)
+static int write_to_fifo(AVFifoBuffer *fifo, AVFrame *buf)
 {
     int ret;
 
     if (!av_fifo_space(fifo) &&
         (ret = av_fifo_realloc2(fifo, 2*av_fifo_size(fifo)))) {
-        avfilter_unref_bufferp(&buf);
+        av_frame_free(&buf);
         return ret;
     }
 
@@ -176,7 +161,7 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
 {
     AVFilterContext    *ctx = inlink->dst;
     FPSContext           *s = ctx->priv;
@@ -196,7 +181,7 @@
         } else {
             av_log(ctx, AV_LOG_WARNING, "Discarding initial frame(s) with no "
                    "timestamp.\n");
-            avfilter_unref_buffer(buf);
+            av_frame_free(&buf);
             s->drop++;
         }
         return 0;
@@ -213,8 +198,8 @@
 
     if (delta < 1) {
         /* drop the frame and everything buffered except the first */
-        AVFilterBufferRef *tmp;
-        int drop = av_fifo_size(s->fifo)/sizeof(AVFilterBufferRef*);
+        AVFrame *tmp;
+        int drop = av_fifo_size(s->fifo)/sizeof(AVFrame*);
 
         av_log(ctx, AV_LOG_DEBUG, "Dropping %d frame(s).\n", drop);
         s->drop += drop;
@@ -223,18 +208,18 @@
         flush_fifo(s->fifo);
         ret = write_to_fifo(s->fifo, tmp);
 
-        avfilter_unref_buffer(buf);
+        av_frame_free(&buf);
         return ret;
     }
 
     /* can output >= 1 frames */
     for (i = 0; i < delta; i++) {
-        AVFilterBufferRef *buf_out;
+        AVFrame *buf_out;
         av_fifo_generic_read(s->fifo, &buf_out, sizeof(buf_out), NULL);
 
         /* duplicate the frame if needed */
         if (!av_fifo_size(s->fifo) && i < delta - 1) {
-            AVFilterBufferRef *dup = avfilter_ref_buffer(buf_out, ~0);
+            AVFrame *dup = av_frame_clone(buf_out);
 
             av_log(ctx, AV_LOG_DEBUG, "Duplicating frame.\n");
             if (dup)
@@ -243,8 +228,8 @@
                 ret = AVERROR(ENOMEM);
 
             if (ret < 0) {
-                avfilter_unref_bufferp(&buf_out);
-                avfilter_unref_bufferp(&buf);
+                av_frame_free(&buf_out);
+                av_frame_free(&buf);
                 return ret;
             }
 
@@ -255,7 +240,7 @@
                                     outlink->time_base) + s->frames_out;
 
         if ((ret = ff_filter_frame(outlink, buf_out)) < 0) {
-            avfilter_unref_bufferp(&buf);
+            av_frame_free(&buf);
             return ret;
         }
 
@@ -273,7 +258,6 @@
     {
         .name        = "default",
         .type        = AVMEDIA_TYPE_VIDEO,
-        .min_perms   = AV_PERM_READ | AV_PERM_PRESERVE,
         .filter_frame = filter_frame,
     },
     { NULL }
@@ -283,7 +267,6 @@
     {
         .name          = "default",
         .type          = AVMEDIA_TYPE_VIDEO,
-        .rej_perms     = AV_PERM_WRITE,
         .request_frame = request_frame,
         .config_props  = config_props
     },
@@ -292,14 +275,14 @@
 
 AVFilter avfilter_vf_fps = {
     .name        = "fps",
-    .description = NULL_IF_CONFIG_SMALL("Force constant framerate"),
+    .description = NULL_IF_CONFIG_SMALL("Force constant framerate."),
 
     .init      = init,
     .uninit    = uninit,
 
     .priv_size = sizeof(FPSContext),
+    .priv_class = &fps_class,
 
     .inputs    = avfilter_vf_fps_inputs,
     .outputs   = avfilter_vf_fps_outputs,
-    .priv_class = &fps_class,
 };
diff --git a/libavfilter/vf_framestep.c b/libavfilter/vf_framestep.c
index f848196..fb20411 100644
--- a/libavfilter/vf_framestep.c
+++ b/libavfilter/vf_framestep.c
@@ -23,32 +23,25 @@
  * Daniele Fornighieri <guru AT digitalfantasy it>.
  */
 
+#include "libavutil/opt.h"
 #include "avfilter.h"
 #include "internal.h"
 #include "video.h"
 
 typedef struct {
-    int frame_step, frame_count, frame_selected;
+    const AVClass *class;
+    int frame_step;
 } FrameStepContext;
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    FrameStepContext *framestep = ctx->priv;
-    char *tailptr;
-    long int n = 1;
+#define OFFSET(x) offsetof(FrameStepContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
-    if (args) {
-        n = strtol(args, &tailptr, 10);
-        if (*tailptr || n <= 0 || n >= INT_MAX) {
-            av_log(ctx, AV_LOG_ERROR,
-                   "Invalid argument '%s', must be a positive integer <= INT_MAX\n", args);
-            return AVERROR(EINVAL);
-        }
-    }
+static const AVOption framestep_options[] = {
+    { "step", "set frame step",  OFFSET(frame_step), AV_OPT_TYPE_INT, {.i64=1}, 1, INT_MAX, FLAGS},
+    {NULL},
+};
 
-    framestep->frame_step = n;
-    return 0;
-}
+AVFILTER_DEFINE_CLASS(framestep);
 
 static int config_output_props(AVFilterLink *outlink)
 {
@@ -56,6 +49,7 @@
     FrameStepContext *framestep = ctx->priv;
     AVFilterLink *inlink = ctx->inputs[0];
 
+    outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
     outlink->frame_rate =
         av_div_q(inlink->frame_rate, (AVRational){framestep->frame_step, 1});
 
@@ -66,39 +60,22 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *ref)
+static int filter_frame(AVFilterLink *inlink, AVFrame *ref)
 {
     FrameStepContext *framestep = inlink->dst->priv;
 
-    if (!(framestep->frame_count++ % framestep->frame_step)) {
-        framestep->frame_selected = 1;
+    if (!(inlink->frame_count % framestep->frame_step)) {
         return ff_filter_frame(inlink->dst->outputs[0], ref);
     } else {
-        framestep->frame_selected = 0;
-        avfilter_unref_buffer(ref);
+        av_frame_free(&ref);
         return 0;
     }
 }
 
-static int request_frame(AVFilterLink *outlink)
-{
-    FrameStepContext *framestep = outlink->src->priv;
-    AVFilterLink *inlink = outlink->src->inputs[0];
-    int ret;
-
-    framestep->frame_selected = 0;
-    do {
-        ret = ff_request_frame(inlink);
-    } while (!framestep->frame_selected && ret >= 0);
-
-    return ret;
-}
-
 static const AVFilterPad framestep_inputs[] = {
     {
         .name             = "default",
         .type             = AVMEDIA_TYPE_VIDEO,
-        .get_video_buffer = ff_null_get_video_buffer,
         .filter_frame     = filter_frame,
     },
     { NULL }
@@ -109,7 +86,6 @@
         .name          = "default",
         .type          = AVMEDIA_TYPE_VIDEO,
         .config_props  = config_output_props,
-        .request_frame = request_frame,
     },
     { NULL }
 };
@@ -117,8 +93,8 @@
 AVFilter avfilter_vf_framestep = {
     .name      = "framestep",
     .description = NULL_IF_CONFIG_SMALL("Select one frame every N frames."),
-    .init      = init,
     .priv_size = sizeof(FrameStepContext),
+    .priv_class = &framestep_class,
     .inputs    = framestep_inputs,
     .outputs   = framestep_outputs,
 };
diff --git a/libavfilter/vf_frei0r.c b/libavfilter/vf_frei0r.c
index 7ed78fa..399f09e 100644
--- a/libavfilter/vf_frei0r.c
+++ b/libavfilter/vf_frei0r.c
@@ -35,6 +35,7 @@
 #include "libavutil/internal.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/mem.h"
+#include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "avfilter.h"
 #include "formats.h"
@@ -53,6 +54,7 @@
 typedef void (*f0r_get_param_value_f)(f0r_instance_t instance, f0r_param_t param, int param_index);
 
 typedef struct Frei0rContext {
+    const AVClass *class;
     f0r_update_f update;
     void *dl_handle;            /* dynamic library handle   */
     f0r_instance_t instance;
@@ -64,7 +66,11 @@
     f0r_construct_f       construct;
     f0r_destruct_f        destruct;
     f0r_deinit_f          deinit;
-    char params[256];
+
+    char *dl_name;
+    char *params;
+    char *size;
+    char *framerate;
 
     /* only used by the source */
     int w, h;
@@ -143,7 +149,7 @@
         frei0r->get_param_info(&info, i);
 
         if (*params) {
-            if (!(param = av_get_token(&params, ":")))
+            if (!(param = av_get_token(&params, "|")))
                 return AVERROR(ENOMEM);
             params++;               /* skip ':' */
             ret = set_param(ctx, info, i, param);
@@ -225,6 +231,11 @@
     char *path;
     int ret = 0;
 
+    if (!dl_name) {
+        av_log(ctx, AV_LOG_ERROR, "No filter name provided.\n");
+        return AVERROR(EINVAL);
+    }
+
     /* see: http://frei0r.dyne.org/codedoc/html/group__pluglocations.html */
     if ((path = av_strdup(getenv("FREI0R_PATH")))) {
 #ifdef _WIN32
@@ -317,16 +328,11 @@
     return 0;
 }
 
-static av_cold int filter_init(AVFilterContext *ctx, const char *args)
+static av_cold int filter_init(AVFilterContext *ctx)
 {
     Frei0rContext *frei0r = ctx->priv;
-    char dl_name[1024], c;
-    *frei0r->params = 0;
 
-    if (args)
-        sscanf(args, "%1023[^:=]%c%255c", dl_name, &c, frei0r->params);
-
-    return frei0r_init(ctx, dl_name, F0R_PLUGIN_TYPE_FILTER);
+    return frei0r_init(ctx, frei0r->dl_name, F0R_PLUGIN_TYPE_FILTER);
 }
 
 static av_cold void uninit(AVFilterContext *ctx)
@@ -339,8 +345,6 @@
         frei0r->deinit();
     if (frei0r->dl_handle)
         dlclose(frei0r->dl_handle);
-
-    memset(frei0r, 0, sizeof(*frei0r));
 }
 
 static int config_input_props(AVFilterLink *inlink)
@@ -379,35 +383,49 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     Frei0rContext *frei0r = inlink->dst->priv;
     AVFilterLink *outlink = inlink->dst->outputs[0];
-    AVFilterBufferRef *out;
+    AVFrame *out;
 
-    out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!out) {
-        avfilter_unref_bufferp(&in);
+        av_frame_free(&in);
         return AVERROR(ENOMEM);
     }
-    avfilter_copy_buffer_ref_props(out, in);
+    av_frame_copy_props(out, in);
 
     frei0r->update(frei0r->instance, in->pts * av_q2d(inlink->time_base) * 1000,
                    (const uint32_t *)in->data[0],
                    (uint32_t *)out->data[0]);
 
-    avfilter_unref_bufferp(&in);
+    av_frame_free(&in);
 
     return ff_filter_frame(outlink, out);
 }
 
+#define OFFSET(x) offsetof(Frei0rContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
+static const AVOption filter_options[] = {
+    { "filter_name",   NULL, OFFSET(dl_name), AV_OPT_TYPE_STRING, .flags = FLAGS },
+    { "filter_params", NULL, OFFSET(params),  AV_OPT_TYPE_STRING, .flags = FLAGS },
+    { NULL },
+};
+
+static const AVClass filter_class = {
+    .class_name = "frei0r",
+    .item_name  = av_default_item_name,
+    .option     = filter_options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 static const AVFilterPad avfilter_vf_frei0r_inputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
         .config_props = config_input_props,
         .filter_frame = filter_frame,
-        .min_perms    = AV_PERM_READ
     },
     { NULL }
 };
@@ -429,39 +447,31 @@
     .uninit = uninit,
 
     .priv_size = sizeof(Frei0rContext),
+    .priv_class = &filter_class,
 
     .inputs    = avfilter_vf_frei0r_inputs,
 
     .outputs   = avfilter_vf_frei0r_outputs,
 };
 
-static av_cold int source_init(AVFilterContext *ctx, const char *args)
+static av_cold int source_init(AVFilterContext *ctx)
 {
     Frei0rContext *frei0r = ctx->priv;
-    char dl_name[1024], c;
-    char frame_size[128] = "";
-    char frame_rate[128] = "";
     AVRational frame_rate_q;
 
-    memset(frei0r->params, 0, sizeof(frei0r->params));
-
-    if (args)
-        sscanf(args, "%127[^:]:%127[^:]:%1023[^:=]%c%255c",
-               frame_size, frame_rate, dl_name, &c, frei0r->params);
-
-    if (av_parse_video_size(&frei0r->w, &frei0r->h, frame_size) < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", frame_size);
+    if (av_parse_video_size(&frei0r->w, &frei0r->h, frei0r->size) < 0) {
+        av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", frei0r->size);
         return AVERROR(EINVAL);
     }
 
-    if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", frame_rate);
+    if (av_parse_video_rate(&frame_rate_q, frei0r->framerate) < 0) {
+        av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", frei0r->framerate);
         return AVERROR(EINVAL);
     }
     frei0r->time_base.num = frame_rate_q.den;
     frei0r->time_base.den = frame_rate_q.num;
 
-    return frei0r_init(ctx, dl_name, F0R_PLUGIN_TYPE_SOURCE);
+    return frei0r_init(ctx, frei0r->dl_name, F0R_PLUGIN_TYPE_SOURCE);
 }
 
 static int source_config_props(AVFilterLink *outlink)
@@ -487,21 +497,35 @@
 static int source_request_frame(AVFilterLink *outlink)
 {
     Frei0rContext *frei0r = outlink->src->priv;
-    AVFilterBufferRef *picref = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+    AVFrame *frame = ff_get_video_buffer(outlink, outlink->w, outlink->h);
 
-    if (!picref)
+    if (!frame)
         return AVERROR(ENOMEM);
 
-    picref->video->sample_aspect_ratio = (AVRational) {1, 1};
-    picref->pts = frei0r->pts++;
-    picref->pos = -1;
+    frame->sample_aspect_ratio = (AVRational) {1, 1};
+    frame->pts = frei0r->pts++;
 
-    frei0r->update(frei0r->instance, av_rescale_q(picref->pts, frei0r->time_base, (AVRational){1,1000}),
-                   NULL, (uint32_t *)picref->data[0]);
+    frei0r->update(frei0r->instance, av_rescale_q(frame->pts, frei0r->time_base, (AVRational){1,1000}),
+                   NULL, (uint32_t *)frame->data[0]);
 
-    return ff_filter_frame(outlink, picref);
+    return ff_filter_frame(outlink, frame);
 }
 
+static const AVOption src_options[] = {
+    { "size",          "Dimensions of the generated video.", OFFSET(size),      AV_OPT_TYPE_STRING, { .str = "" },   .flags = FLAGS },
+    { "framerate",     NULL,                                 OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = "25" }, .flags = FLAGS },
+    { "filter_name",   NULL,                                 OFFSET(dl_name),   AV_OPT_TYPE_STRING,                  .flags = FLAGS },
+    { "filter_params", NULL,                                 OFFSET(params),    AV_OPT_TYPE_STRING,                  .flags = FLAGS },
+    { NULL },
+};
+
+static const AVClass src_class = {
+    .class_name = "frei0r_src",
+    .item_name  = av_default_item_name,
+    .option     = src_options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 static const AVFilterPad avfilter_vsrc_frei0r_src_outputs[] = {
     {
         .name          = "default",
@@ -517,6 +541,7 @@
     .description = NULL_IF_CONFIG_SMALL("Generate a frei0r source."),
 
     .priv_size = sizeof(Frei0rContext),
+    .priv_class = &src_class,
     .init      = source_init,
     .uninit    = uninit,
 
diff --git a/libavfilter/vf_geq.c b/libavfilter/vf_geq.c
index 72ecda0..89467bc 100644
--- a/libavfilter/vf_geq.c
+++ b/libavfilter/vf_geq.c
@@ -35,11 +35,11 @@
 typedef struct {
     const AVClass *class;
     AVExpr *e[4];               ///< expressions for each plane
-    char *expr_str[4];          ///< expression strings for each plane
-    int framenum;               ///< frame counter
-    AVFilterBufferRef *picref;  ///< current input buffer
+    char *expr_str[4+3];        ///< expression strings for each plane
+    AVFrame *picref;            ///< current input buffer
     int hsub, vsub;             ///< chroma subsampling
     int planes;                 ///< number of planes
+    int is_rgb;
 } GEQContext;
 
 #define OFFSET(x) offsetof(GEQContext, x)
@@ -50,6 +50,10 @@
     { "cb_expr",    "set chroma blue expression", OFFSET(expr_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "cr_expr",    "set chroma red expression",  OFFSET(expr_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "alpha_expr", "set alpha expression",       OFFSET(expr_str[3]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
+
+    { "r",          "set red expression",   OFFSET(expr_str[6]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "g",          "set green expression", OFFSET(expr_str[4]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "b",          "set blue expression",  OFFSET(expr_str[5]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
     {NULL},
 };
 
@@ -59,11 +63,11 @@
 {
     int xi, yi;
     GEQContext *geq = priv;
-    AVFilterBufferRef *picref = geq->picref;
+    AVFrame *picref = geq->picref;
     const uint8_t *src = picref->data[plane];
     const int linesize = picref->linesize[plane];
-    const int w = picref->video->w >> ((plane == 1 || plane == 2) ? geq->hsub : 0);
-    const int h = picref->video->h >> ((plane == 1 || plane == 2) ? geq->vsub : 0);
+    const int w = picref->width  >> ((plane == 1 || plane == 2) ? geq->hsub : 0);
+    const int h = picref->height >> ((plane == 1 || plane == 2) ? geq->vsub : 0);
 
     if (!src)
         return 0;
@@ -88,20 +92,20 @@
 static const char *const var_names[] = {   "X",   "Y",   "W",   "H",   "N",   "SW",   "SH",   "T",        NULL };
 enum                                   { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_N, VAR_SW, VAR_SH, VAR_T, VAR_VARS_NB };
 
-static av_cold int geq_init(AVFilterContext *ctx, const char *args)
+static av_cold int geq_init(AVFilterContext *ctx)
 {
     GEQContext *geq = ctx->priv;
     int plane, ret = 0;
-    static const char *shorthand[] = { "lum_expr", "cb_expr", "cr_expr", "alpha_expr", NULL };
 
-    geq->class = &geq_class;
-    av_opt_set_defaults(geq);
+    if (!geq->expr_str[0] && !geq->expr_str[4] && !geq->expr_str[5] && !geq->expr_str[6]) {
+        av_log(ctx, AV_LOG_ERROR, "A luminance or RGB expression is mandatory\n");
+        ret = AVERROR(EINVAL);
+        goto end;
+    }
+    geq->is_rgb = !geq->expr_str[0];
 
-    if ((ret = av_opt_set_from_string(geq, args, shorthand, "=", ":")) < 0)
-        return ret;
-
-    if (!geq->expr_str[0]) {
-        av_log(ctx, AV_LOG_ERROR, "Luminance expression is mandatory\n");
+    if ((geq->expr_str[0] || geq->expr_str[1] || geq->expr_str[2]) && (geq->expr_str[4] || geq->expr_str[5] || geq->expr_str[6])) {
+        av_log(ctx, AV_LOG_ERROR, "Either YCbCr or RGB but not both must be specified\n");
         ret = AVERROR(EINVAL);
         goto end;
     }
@@ -118,18 +122,29 @@
 
     if (!geq->expr_str[3])
         geq->expr_str[3] = av_strdup("255");
+    if (!geq->expr_str[4])
+        geq->expr_str[4] = av_strdup("g(X,Y)");
+    if (!geq->expr_str[5])
+        geq->expr_str[5] = av_strdup("b(X,Y)");
+    if (!geq->expr_str[6])
+        geq->expr_str[6] = av_strdup("r(X,Y)");
 
-    if (!geq->expr_str[1] || !geq->expr_str[2] || !geq->expr_str[3]) {
+    if (geq->is_rgb ?
+            (!geq->expr_str[4] || !geq->expr_str[5] || !geq->expr_str[6])
+                    :
+            (!geq->expr_str[1] || !geq->expr_str[2] || !geq->expr_str[3])) {
         ret = AVERROR(ENOMEM);
         goto end;
     }
 
     for (plane = 0; plane < 4; plane++) {
         static double (*p[])(void *, double, double) = { lum, cb, cr, alpha };
-        static const char *const func2_names[]    = { "lum", "cb", "cr", "alpha", "p", NULL };
+        static const char *const func2_yuv_names[]    = { "lum", "cb", "cr", "alpha", "p", NULL };
+        static const char *const func2_rgb_names[]    = { "g", "b", "r", "alpha", "p", NULL };
+        const char *const *func2_names       = geq->is_rgb ? func2_rgb_names : func2_yuv_names;
         double (*func2[])(void *, double, double) = { lum, cb, cr, alpha, p[plane], NULL };
 
-        ret = av_expr_parse(&geq->e[plane], geq->expr_str[plane], var_names,
+        ret = av_expr_parse(&geq->e[plane], geq->expr_str[plane < 3 && geq->is_rgb ? plane+4 : plane], var_names,
                             NULL, NULL, func2_names, func2, 0, ctx);
         if (ret < 0)
             break;
@@ -141,14 +156,22 @@
 
 static int geq_query_formats(AVFilterContext *ctx)
 {
-    static const enum PixelFormat pix_fmts[] = {
+    GEQContext *geq = ctx->priv;
+    static const enum PixelFormat yuv_pix_fmts[] = {
         AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
         AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P,  AV_PIX_FMT_YUV440P,
         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
         AV_PIX_FMT_GRAY8,
         AV_PIX_FMT_NONE
     };
-    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+    static const enum PixelFormat rgb_pix_fmts[] = {
+        AV_PIX_FMT_GBRP,
+        AV_PIX_FMT_NONE
+    };
+    if (geq->is_rgb) {
+        ff_set_common_formats(ctx, ff_make_format_list(rgb_pix_fmts));
+    } else
+        ff_set_common_formats(ctx, ff_make_format_list(yuv_pix_fmts));
     return 0;
 }
 
@@ -163,24 +186,24 @@
     return 0;
 }
 
-static int geq_filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
+static int geq_filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     int plane;
     GEQContext *geq = inlink->dst->priv;
     AVFilterLink *outlink = inlink->dst->outputs[0];
-    AVFilterBufferRef *out;
+    AVFrame *out;
     double values[VAR_VARS_NB] = {
-        [VAR_N] = geq->framenum++,
+        [VAR_N] = inlink->frame_count,
         [VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base),
     };
 
     geq->picref = in;
-    out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!out) {
-        avfilter_unref_bufferp(&in);
+        av_frame_free(&in);
         return AVERROR(ENOMEM);
     }
-    avfilter_copy_buffer_ref_props(out, in);
+    av_frame_copy_props(out, in);
 
     for (plane = 0; plane < geq->planes && out->data[plane]; plane++) {
         int x, y;
@@ -204,7 +227,7 @@
         }
     }
 
-    avfilter_unref_bufferp(&geq->picref);
+    av_frame_free(&geq->picref);
     return ff_filter_frame(outlink, out);
 }
 
@@ -215,7 +238,6 @@
 
     for (i = 0; i < FF_ARRAY_ELEMS(geq->e); i++)
         av_expr_free(geq->e[i]);
-    av_opt_free(geq);
 }
 
 static const AVFilterPad geq_inputs[] = {
@@ -224,7 +246,6 @@
         .type         = AVMEDIA_TYPE_VIDEO,
         .config_props = geq_config_props,
         .filter_frame = geq_filter_frame,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL }
 };
diff --git a/libavfilter/vf_gradfun.c b/libavfilter/vf_gradfun.c
index 13154f0..48ac378 100644
--- a/libavfilter/vf_gradfun.c
+++ b/libavfilter/vf_gradfun.c
@@ -35,6 +35,7 @@
 #include "libavutil/imgutils.h"
 #include "libavutil/common.h"
 #include "libavutil/cpu.h"
+#include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/opt.h"
 #include "avfilter.h"
@@ -43,17 +44,6 @@
 #include "internal.h"
 #include "video.h"
 
-#define OFFSET(x) offsetof(GradFunContext, x)
-#define F AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
-
-static const AVOption gradfun_options[] = {
-    { "strength", "set the maximum amount by which the filter will change any one pixel", OFFSET(strength), AV_OPT_TYPE_DOUBLE, {.dbl = 1.2}, 0.51, 64, F },
-    { "radius",   "set the neighborhood to fit the gradient to",                          OFFSET(radius),   AV_OPT_TYPE_INT,    {.i64 =  16},    4, 32, F },
-    { NULL }
-};
-
-AVFILTER_DEFINE_CLASS(gradfun);
-
 DECLARE_ALIGNED(16, static const uint16_t, dither)[8][8] = {
     {0x00,0x60,0x18,0x78,0x06,0x66,0x1E,0x7E},
     {0x40,0x20,0x58,0x38,0x46,0x26,0x5E,0x3E},
@@ -131,20 +121,12 @@
     }
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
-    int ret;
     GradFunContext *gf = ctx->priv;
-    static const char *shorthand[] = { "strength", "radius", NULL };
 
-    gf->class = &gradfun_class;
-    av_opt_set_defaults(gf);
-
-    if ((ret = av_opt_set_from_string(gf, args, shorthand, "=", ":")) < 0)
-        return ret;
-
-    gf->thresh = (1 << 15) / gf->strength;
-    gf->radius = av_clip((gf->radius + 1) & ~1, 4, 32);
+    gf->thresh  = (1 << 15) / gf->strength;
+    gf->radius  = av_clip((gf->radius + 1) & ~1, 4, 32);
 
     gf->blur_line   = ff_gradfun_blur_line_c;
     gf->filter_line = ff_gradfun_filter_line_c;
@@ -197,23 +179,24 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     GradFunContext *gf = inlink->dst->priv;
     AVFilterLink *outlink = inlink->dst->outputs[0];
-    AVFilterBufferRef *out;
-    int p, direct = 0;
+    AVFrame *out;
+    int p, direct;
 
-    if (in->perms & AV_PERM_WRITE) {
+    if (av_frame_is_writable(in)) {
         direct = 1;
         out = in;
     } else {
-        out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+        direct = 0;
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
         if (!out) {
-            avfilter_unref_bufferp(&in);
+            av_frame_free(&in);
             return AVERROR(ENOMEM);
         }
-        avfilter_copy_buffer_ref_props(out, in);
+        av_frame_copy_props(out, in);
     }
 
     for (p = 0; p < 4 && in->data[p]; p++) {
@@ -233,18 +216,28 @@
     }
 
     if (!direct)
-        avfilter_unref_bufferp(&in);
+        av_frame_free(&in);
 
     return ff_filter_frame(outlink, out);
 }
 
+#define OFFSET(x) offsetof(GradFunContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption gradfun_options[] = {
+    { "strength", "The maximum amount by which the filter will change any one pixel.", OFFSET(strength), AV_OPT_TYPE_FLOAT, { .dbl = 1.2 }, 0.51, 64, FLAGS },
+    { "radius",   "The neighborhood to fit the gradient to.",                          OFFSET(radius),   AV_OPT_TYPE_INT,   { .i64 = 16  }, 4,    32, FLAGS },
+    { NULL },
+};
+
+AVFILTER_DEFINE_CLASS(gradfun);
+
 static const AVFilterPad avfilter_vf_gradfun_inputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
         .config_props = config_input,
         .filter_frame = filter_frame,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL }
 };
@@ -261,10 +254,11 @@
     .name          = "gradfun",
     .description   = NULL_IF_CONFIG_SMALL("Debands video quickly using gradients."),
     .priv_size     = sizeof(GradFunContext),
+    .priv_class    = &gradfun_class,
     .init          = init,
     .uninit        = uninit,
     .query_formats = query_formats,
     .inputs        = avfilter_vf_gradfun_inputs,
     .outputs       = avfilter_vf_gradfun_outputs,
-    .priv_class    = &gradfun_class,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE,
 };
diff --git a/libavfilter/vf_hflip.c b/libavfilter/vf_hflip.c
index c3b92c2..cb51981 100644
--- a/libavfilter/vf_hflip.c
+++ b/libavfilter/vf_hflip.c
@@ -70,21 +70,21 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     AVFilterContext *ctx  = inlink->dst;
     FlipContext *flip     = ctx->priv;
     AVFilterLink *outlink = ctx->outputs[0];
-    AVFilterBufferRef *out;
+    AVFrame *out;
     uint8_t *inrow, *outrow;
     int i, j, plane, step, hsub, vsub;
 
-    out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!out) {
-        avfilter_unref_bufferp(&in);
+        av_frame_free(&in);
         return AVERROR(ENOMEM);
     }
-    avfilter_copy_buffer_ref_props(out, in);
+    av_frame_copy_props(out, in);
 
     /* copy palette if required */
     if (av_pix_fmt_desc_get(inlink->format)->flags & PIX_FMT_PAL)
@@ -97,7 +97,7 @@
 
         outrow = out->data[plane];
         inrow  = in ->data[plane] + ((inlink->w >> hsub) - 1) * step;
-        for (i = 0; i < in->video->h >> vsub; i++) {
+        for (i = 0; i < in->height >> vsub; i++) {
             switch (step) {
             case 1:
                 for (j = 0; j < (inlink->w >> hsub); j++)
@@ -143,7 +143,7 @@
         }
     }
 
-    avfilter_unref_bufferp(&in);
+    av_frame_free(&in);
     return ff_filter_frame(outlink, out);
 }
 
@@ -153,7 +153,6 @@
         .type         = AVMEDIA_TYPE_VIDEO,
         .filter_frame = filter_frame,
         .config_props = config_props,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL }
 };
diff --git a/libavfilter/vf_histeq.c b/libavfilter/vf_histeq.c
index 556680c..a4166c6 100644
--- a/libavfilter/vf_histeq.c
+++ b/libavfilter/vf_histeq.c
@@ -82,17 +82,9 @@
 
 AVFILTER_DEFINE_CLASS(histeq);
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     HisteqContext *histeq = ctx->priv;
-    const char *shorthand[] = { "strength", "intensity", "antibanding", NULL };
-    int ret;
-
-    histeq->class = &histeq_class;
-    av_opt_set_defaults(histeq);
-
-    if ((ret = av_opt_set_from_string(histeq, args, shorthand, "=", ":")) < 0)
-        return ret;
 
     av_log(ctx, AV_LOG_VERBOSE,
            "strength:%0.3f intensity:%0.3f antibanding:%d\n",
@@ -101,12 +93,6 @@
     return 0;
 }
 
-static av_cold void uninit(AVFilterContext *ctx)
-{
-    HisteqContext *histeq = ctx->priv;
-    av_opt_free(histeq);
-}
-
 static int query_formats(AVFilterContext *ctx)
 {
     static const enum PixelFormat pix_fmts[] = {
@@ -142,7 +128,7 @@
     b = src[x + map[B]];                       \
 } while (0)
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpic)
+static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
 {
     AVFilterContext   *ctx     = inlink->dst;
     HisteqContext     *histeq  = ctx->priv;
@@ -150,16 +136,16 @@
     int strength  = histeq->strength  * 1000;
     int intensity = histeq->intensity * 1000;
     int x, y, i, luthi, lutlo, lut, luma, oluma, m;
-    AVFilterBufferRef *outpic;
+    AVFrame *outpic;
     unsigned int r, g, b, jran;
     uint8_t *src, *dst;
 
-    outpic = ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN, outlink->w, outlink->h);
+    outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!outpic) {
-        avfilter_unref_bufferp(&inpic);
+        av_frame_free(&inpic);
         return AVERROR(ENOMEM);
     }
-    avfilter_copy_buffer_ref_props(outpic, inpic);
+    av_frame_copy_props(outpic, inpic);
 
     /* Seed random generator for antibanding. */
     jran = LCG_SEED;
@@ -249,7 +235,7 @@
                 dst[x + histeq->rgba_map[R]] = r;
                 dst[x + histeq->rgba_map[G]] = g;
                 dst[x + histeq->rgba_map[B]] = b;
-                oluma = (55 * r + 182 * g + 19 * b) >> 8;
+                oluma = av_clip_uint8((55 * r + 182 * g + 19 * b) >> 8);
                 histeq->out_histogram[oluma]++;
             }
         }
@@ -261,7 +247,7 @@
         av_dlog(ctx, "out[%d]: %u\n", x, histeq->out_histogram[x]);
 #endif
 
-    avfilter_unref_bufferp(&inpic);
+    av_frame_free(&inpic);
     return ff_filter_frame(outlink, outpic);
 }
 
@@ -271,7 +257,6 @@
         .type             = AVMEDIA_TYPE_VIDEO,
         .config_props     = config_input,
         .filter_frame     = filter_frame,
-        .min_perms        = AV_PERM_READ,
     },
     { NULL }
 };
@@ -289,10 +274,10 @@
     .description   = NULL_IF_CONFIG_SMALL("Apply global color histogram equalization."),
     .priv_size     = sizeof(HisteqContext),
     .init          = init,
-    .uninit        = uninit,
     .query_formats = query_formats,
 
     .inputs        = histeq_inputs,
     .outputs       = histeq_outputs,
     .priv_class    = &histeq_class,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE,
 };
diff --git a/libavfilter/vf_histogram.c b/libavfilter/vf_histogram.c
index 279e44a..3daa482 100644
--- a/libavfilter/vf_histogram.c
+++ b/libavfilter/vf_histogram.c
@@ -73,20 +73,6 @@
 
 AVFILTER_DEFINE_CLASS(histogram);
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    HistogramContext *h = ctx->priv;
-    int ret;
-
-    h->class = &histogram_class;
-    av_opt_set_defaults(h);
-
-    if ((ret = (av_set_options_string(h, args, "=", ":"))) < 0)
-        return ret;
-
-    return 0;
-}
-
 static const enum AVPixelFormat color_pix_fmts[] = {
     AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVJ444P,
     AV_PIX_FMT_NONE
@@ -174,24 +160,23 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     HistogramContext *h   = inlink->dst->priv;
     AVFilterContext *ctx  = inlink->dst;
     AVFilterLink *outlink = ctx->outputs[0];
-    AVFilterBufferRef *out;
+    AVFrame *out;
     const uint8_t *src;
     uint8_t *dst;
-    int i, j, k, l, ret;
+    int i, j, k, l;
 
-    out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!out) {
-        avfilter_unref_bufferp(&in);
+        av_frame_free(&in);
         return AVERROR(ENOMEM);
     }
 
     out->pts = in->pts;
-    out->pos = in->pos;
 
     for (k = 0; k < h->ncomp; k++)
         for (i = 0; i < outlink->h; i++)
@@ -202,9 +187,9 @@
         for (k = 0; k < h->ncomp; k++) {
             int start = k * (h->level_height + h->scale_height) * h->display_mode;
 
-            for (i = 0; i < in->video->h; i++) {
+            for (i = 0; i < in->height; i++) {
                 src = in->data[k] + i * in->linesize[k];
-                for (j = 0; j < in->video->w; j++)
+                for (j = 0; j < in->width; j++)
                     h->histogram[src[j]]++;
             }
 
@@ -212,7 +197,7 @@
                 h->max_hval = FFMAX(h->max_hval, h->histogram[i]);
 
             for (i = 0; i < outlink->w; i++) {
-                int col_height = h->level_height - (float)h->histogram[i] / h->max_hval * h->level_height;
+                int col_height = h->level_height - (h->histogram[i] * (int64_t)h->level_height + h->max_hval - 1) / h->max_hval;
 
                 for (j = h->level_height - 1; j >= col_height; j--) {
                     if (h->display_mode) {
@@ -300,18 +285,8 @@
         av_assert0(0);
     }
 
-    ret = ff_filter_frame(outlink, out);
-    avfilter_unref_bufferp(&in);
-    if (ret < 0)
-        return ret;
-    return 0;
-}
-
-static av_cold void uninit(AVFilterContext *ctx)
-{
-    HistogramContext *h = ctx->priv;
-
-    av_opt_free(h);
+    av_frame_free(&in);
+    return ff_filter_frame(outlink, out);
 }
 
 static const AVFilterPad inputs[] = {
@@ -320,7 +295,6 @@
         .type         = AVMEDIA_TYPE_VIDEO,
         .filter_frame = filter_frame,
         .config_props = config_input,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL }
 };
@@ -338,8 +312,6 @@
     .name          = "histogram",
     .description   = NULL_IF_CONFIG_SMALL("Compute and draw a histogram."),
     .priv_size     = sizeof(HistogramContext),
-    .init          = init,
-    .uninit        = uninit,
     .query_formats = query_formats,
     .inputs        = inputs,
     .outputs       = outputs,
diff --git a/libavfilter/vf_hqdn3d.c b/libavfilter/vf_hqdn3d.c
index 4381586..003f175 100644
--- a/libavfilter/vf_hqdn3d.c
+++ b/libavfilter/vf_hqdn3d.c
@@ -26,10 +26,14 @@
  * libmpcodecs/vf_hqdn3d.c.
  */
 
+#include <float.h>
+
 #include "config.h"
 #include "libavutil/common.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/opt.h"
+
 #include "avfilter.h"
 #include "formats.h"
 #include "internal.h"
@@ -37,9 +41,10 @@
 #include "vf_hqdn3d.h"
 
 #define LUT_BITS (depth==16 ? 8 : 4)
-#define LOAD(x) (((depth==8 ? src[x] : AV_RN16A(src+(x)*2)) << (16-depth)) + (((1<<(16-depth))-1)>>1))
-#define STORE(x,val) (depth==8 ? dst[x] = (val) >> (16-depth)\
-                    : AV_WN16A(dst+(x)*2, (val) >> (16-depth)))
+#define LOAD(x) (((depth == 8 ? src[x] : AV_RN16A(src + (x) * 2)) << (16 - depth))\
+                 + (((1 << (16 - depth)) - 1) >> 1))
+#define STORE(x,val) (depth == 8 ? dst[x] = (val) >> (16 - depth) : \
+                                   AV_WN16A(dst + (x) * 2, (val) >> (16 - depth)))
 
 av_always_inline
 static uint32_t lowpass(int prev, int cur, int16_t *coef, int depth)
@@ -176,60 +181,22 @@
 #define PARAM2_DEFAULT 3.0
 #define PARAM3_DEFAULT 6.0
 
-static int init(AVFilterContext *ctx, const char *args)
+static int init(AVFilterContext *ctx)
 {
     HQDN3DContext *hqdn3d = ctx->priv;
-    double lum_spac, lum_tmp, chrom_spac, chrom_tmp;
-    double param1, param2, param3, param4;
 
-    lum_spac   = PARAM1_DEFAULT;
-    chrom_spac = PARAM2_DEFAULT;
-    lum_tmp    = PARAM3_DEFAULT;
-    chrom_tmp  = lum_tmp * chrom_spac / lum_spac;
-
-    if (args) {
-        switch (sscanf(args, "%lf:%lf:%lf:%lf",
-                       &param1, &param2, &param3, &param4)) {
-        case 1:
-            lum_spac   = param1;
-            chrom_spac = PARAM2_DEFAULT * param1 / PARAM1_DEFAULT;
-            lum_tmp    = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
-            chrom_tmp  = lum_tmp * chrom_spac / lum_spac;
-            break;
-        case 2:
-            lum_spac   = param1;
-            chrom_spac = param2;
-            lum_tmp    = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
-            chrom_tmp  = lum_tmp * chrom_spac / lum_spac;
-            break;
-        case 3:
-            lum_spac   = param1;
-            chrom_spac = param2;
-            lum_tmp    = param3;
-            chrom_tmp  = lum_tmp * chrom_spac / lum_spac;
-            break;
-        case 4:
-            lum_spac   = param1;
-            chrom_spac = param2;
-            lum_tmp    = param3;
-            chrom_tmp  = param4;
-            break;
-        }
-    }
-
-    hqdn3d->strength[0] = lum_spac;
-    hqdn3d->strength[1] = lum_tmp;
-    hqdn3d->strength[2] = chrom_spac;
-    hqdn3d->strength[3] = chrom_tmp;
+    if (!hqdn3d->strength[LUMA_SPATIAL])
+        hqdn3d->strength[LUMA_SPATIAL] = PARAM1_DEFAULT;
+    if (!hqdn3d->strength[CHROMA_SPATIAL])
+        hqdn3d->strength[CHROMA_SPATIAL] = PARAM2_DEFAULT * hqdn3d->strength[LUMA_SPATIAL] / PARAM1_DEFAULT;
+    if (!hqdn3d->strength[LUMA_TMP])
+        hqdn3d->strength[LUMA_TMP]   = PARAM3_DEFAULT * hqdn3d->strength[LUMA_SPATIAL] / PARAM1_DEFAULT;
+    if (!hqdn3d->strength[CHROMA_TMP])
+        hqdn3d->strength[CHROMA_TMP] = hqdn3d->strength[LUMA_TMP] * hqdn3d->strength[CHROMA_SPATIAL] / hqdn3d->strength[LUMA_SPATIAL];
 
     av_log(ctx, AV_LOG_VERBOSE, "ls:%f cs:%f lt:%f ct:%f\n",
-           lum_spac, chrom_spac, lum_tmp, chrom_tmp);
-    if (lum_spac < 0 || chrom_spac < 0 || isnan(chrom_tmp)) {
-        av_log(ctx, AV_LOG_ERROR,
-               "Invalid negative value for luma or chroma spatial strength, "
-               "or resulting value for chroma temporal strength is nan.\n");
-        return AVERROR(EINVAL);
-    }
+           hqdn3d->strength[LUMA_SPATIAL], hqdn3d->strength[CHROMA_SPATIAL],
+           hqdn3d->strength[LUMA_TMP], hqdn3d->strength[CHROMA_TMP]);
 
     return 0;
 }
@@ -304,41 +271,60 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     HQDN3DContext *hqdn3d = inlink->dst->priv;
     AVFilterLink *outlink = inlink->dst->outputs[0];
 
-    AVFilterBufferRef *out;
-    int direct = 0, c;
+    AVFrame *out;
+    int direct, c;
 
-    if (in->perms & AV_PERM_WRITE) {
+    if (av_frame_is_writable(in)) {
         direct = 1;
         out = in;
     } else {
-        out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+        direct = 0;
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
         if (!out) {
-            avfilter_unref_bufferp(&in);
+            av_frame_free(&in);
             return AVERROR(ENOMEM);
         }
-        avfilter_copy_buffer_ref_props(out, in);
+
+        av_frame_copy_props(out, in);
     }
 
     for (c = 0; c < 3; c++) {
         denoise(hqdn3d, in->data[c], out->data[c],
                 hqdn3d->line, &hqdn3d->frame_prev[c],
-                in->video->w >> (!!c * hqdn3d->hsub),
-                in->video->h >> (!!c * hqdn3d->vsub),
+                in->width  >> (!!c * hqdn3d->hsub),
+                in->height >> (!!c * hqdn3d->vsub),
                 in->linesize[c], out->linesize[c],
                 hqdn3d->coefs[c?2:0], hqdn3d->coefs[c?3:1]);
     }
 
     if (!direct)
-        avfilter_unref_bufferp(&in);
+        av_frame_free(&in);
 
     return ff_filter_frame(outlink, out);
 }
 
+#define OFFSET(x) offsetof(HQDN3DContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
+static const AVOption options[] = {
+    { "luma_spatial",   "spatial luma strength",    OFFSET(strength[LUMA_SPATIAL]),   AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
+    { "chroma_spatial", "spatial chroma strength",  OFFSET(strength[CHROMA_SPATIAL]), AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
+    { "luma_tmp",       "temporal luma strength",   OFFSET(strength[LUMA_TMP]),       AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
+    { "chroma_tmp",     "temporal chroma strength", OFFSET(strength[CHROMA_TMP]),     AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0, DBL_MAX, FLAGS },
+    { NULL },
+};
+
+static const AVClass hqdn3d_class = {
+    .class_name = "hqdn3d",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 static const AVFilterPad avfilter_vf_hqdn3d_inputs[] = {
     {
         .name         = "default",
@@ -363,11 +349,12 @@
     .description   = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
 
     .priv_size     = sizeof(HQDN3DContext),
+    .priv_class    = &hqdn3d_class,
     .init          = init,
     .uninit        = uninit,
     .query_formats = query_formats,
 
     .inputs    = avfilter_vf_hqdn3d_inputs,
-
     .outputs   = avfilter_vf_hqdn3d_outputs,
+    .flags     = AVFILTER_FLAG_SUPPORT_TIMELINE,
 };
diff --git a/libavfilter/vf_hqdn3d.h b/libavfilter/vf_hqdn3d.h
index dfc69e1..268ac9a 100644
--- a/libavfilter/vf_hqdn3d.h
+++ b/libavfilter/vf_hqdn3d.h
@@ -26,7 +26,10 @@
 #include <stddef.h>
 #include <stdint.h>
 
+#include "libavutil/opt.h"
+
 typedef struct {
+    const AVClass *class;
     int16_t *coefs[4];
     uint16_t *line;
     uint16_t *frame_prev[3];
@@ -36,6 +39,11 @@
     void (*denoise_row[17])(uint8_t *src, uint8_t *dst, uint16_t *line_ant, uint16_t *frame_ant, ptrdiff_t w, int16_t *spatial, int16_t *temporal);
 } HQDN3DContext;
 
+#define LUMA_SPATIAL   0
+#define LUMA_TMP       1
+#define CHROMA_SPATIAL 2
+#define CHROMA_TMP     3
+
 void ff_hqdn3d_init_x86(HQDN3DContext *hqdn3d);
 
 #endif /* AVFILTER_VF_HQDN3D_H */
diff --git a/libavfilter/vf_hue.c b/libavfilter/vf_hue.c
index 59fc62c..c62cba8 100644
--- a/libavfilter/vf_hue.c
+++ b/libavfilter/vf_hue.c
@@ -36,12 +36,6 @@
 #include "internal.h"
 #include "video.h"
 
-#define HUE_DEFAULT_VAL 0
-#define SAT_DEFAULT_VAL 1
-
-#define HUE_DEFAULT_VAL_STRING AV_STRINGIFY(HUE_DEFAULT_VAL)
-#define SAT_DEFAULT_VAL_STRING AV_STRINGIFY(SAT_DEFAULT_VAL)
-
 #define SAT_MIN_VAL -10
 #define SAT_MAX_VAL 10
 
@@ -78,7 +72,6 @@
     int      vsub;
     int32_t hue_sin;
     int32_t hue_cos;
-    int      flat_syntax;
     double   var_values[VAR_NB];
 } HueContext;
 
@@ -87,9 +80,9 @@
 static const AVOption hue_options[] = {
     { "h", "set the hue angle degrees expression", OFFSET(hue_deg_expr), AV_OPT_TYPE_STRING,
       { .str = NULL }, .flags = FLAGS },
-    { "H", "set the hue angle radians expression", OFFSET(hue_expr), AV_OPT_TYPE_STRING,
-      { .str = NULL }, .flags = FLAGS },
     { "s", "set the saturation expression", OFFSET(saturation_expr), AV_OPT_TYPE_STRING,
+      { .str = "1" }, .flags = FLAGS },
+    { "H", "set the hue angle radians expression", OFFSET(hue_expr), AV_OPT_TYPE_STRING,
       { .str = NULL }, .flags = FLAGS },
     { NULL }
 };
@@ -107,99 +100,73 @@
     hue->hue_cos = rint(cos(hue->hue) * (1 << 16) * hue->saturation);
 }
 
-#define SET_EXPRESSION(attr, name) do {                                           \
-    if (hue->attr##_expr) {                                                       \
-        if ((ret = av_expr_parse(&hue->attr##_pexpr, hue->attr##_expr, var_names, \
-                                 NULL, NULL, NULL, NULL, 0, ctx)) < 0) {          \
-            av_log(ctx, AV_LOG_ERROR,                                             \
-                   "Parsing failed for expression " #name "='%s'",                \
-                   hue->attr##_expr);                                             \
-            hue->attr##_expr  = old_##attr##_expr;                                \
-            hue->attr##_pexpr = old_##attr##_pexpr;                               \
-            return AVERROR(EINVAL);                                               \
-        } else if (old_##attr##_pexpr) {                                          \
-            av_freep(&old_##attr##_expr);                                         \
-            av_expr_free(old_##attr##_pexpr);                                     \
-            old_##attr##_pexpr = NULL;                                            \
-        }                                                                         \
-    } else {                                                                      \
-        hue->attr##_expr = old_##attr##_expr;                                     \
-    }                                                                             \
-} while (0)
-
-static inline int set_options(AVFilterContext *ctx, const char *args)
+static int set_expr(AVExpr **pexpr_ptr, char **expr_ptr,
+                    const char *expr, const char *option, void *log_ctx)
 {
-    HueContext *hue = ctx->priv;
     int ret;
-    char   *old_hue_expr,  *old_hue_deg_expr,  *old_saturation_expr;
-    AVExpr *old_hue_pexpr, *old_hue_deg_pexpr, *old_saturation_pexpr;
-    static const char *shorthand[] = { "h", "s", NULL };
-    old_hue_expr        = hue->hue_expr;
-    old_hue_deg_expr    = hue->hue_deg_expr;
-    old_saturation_expr = hue->saturation_expr;
+    AVExpr *new_pexpr;
+    char *new_expr;
 
-    old_hue_pexpr        = hue->hue_pexpr;
-    old_hue_deg_pexpr    = hue->hue_deg_pexpr;
-    old_saturation_pexpr = hue->saturation_pexpr;
-
-    hue->hue_expr     = NULL;
-    hue->hue_deg_expr = NULL;
-    hue->saturation_expr = NULL;
-
-    if ((ret = av_opt_set_from_string(hue, args, shorthand, "=", ":")) < 0)
+    new_expr = av_strdup(expr);
+    if (!new_expr)
+        return AVERROR(ENOMEM);
+    ret = av_expr_parse(&new_pexpr, expr, var_names,
+                        NULL, NULL, NULL, NULL, 0, log_ctx);
+    if (ret < 0) {
+        av_log(log_ctx, AV_LOG_ERROR,
+               "Error when evaluating the expression '%s' for %s\n",
+               expr, option);
+        av_free(new_expr);
         return ret;
-    if (hue->hue_expr && hue->hue_deg_expr) {
-        av_log(ctx, AV_LOG_ERROR,
-               "H and h options are incompatible and cannot be specified "
-               "at the same time\n");
-        hue->hue_expr     = old_hue_expr;
-        hue->hue_deg_expr = old_hue_deg_expr;
-
-        return AVERROR(EINVAL);
     }
 
-    SET_EXPRESSION(hue_deg, h);
-    SET_EXPRESSION(hue, H);
-    SET_EXPRESSION(saturation, s);
-
-    hue->flat_syntax = 0;
-
-    av_log(ctx, AV_LOG_VERBOSE,
-           "H_expr:%s h_deg_expr:%s s_expr:%s\n",
-           hue->hue_expr, hue->hue_deg_expr, hue->saturation_expr);
-
-    compute_sin_and_cos(hue);
+    if (*pexpr_ptr)
+        av_expr_free(*pexpr_ptr);
+    *pexpr_ptr = new_pexpr;
+    av_freep(expr_ptr);
+    *expr_ptr = new_expr;
 
     return 0;
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     HueContext *hue = ctx->priv;
+    int ret;
 
-    hue->class = &hue_class;
-    av_opt_set_defaults(hue);
+    if (hue->hue_expr && hue->hue_deg_expr) {
+        av_log(ctx, AV_LOG_ERROR,
+               "H and h options are incompatible and cannot be specified "
+               "at the same time\n");
+        return AVERROR(EINVAL);
+    }
 
-    hue->saturation    = SAT_DEFAULT_VAL;
-    hue->hue           = HUE_DEFAULT_VAL;
-    hue->hue_deg_pexpr = NULL;
-    hue->hue_pexpr     = NULL;
-    hue->flat_syntax   = 1;
+#define SET_EXPR(expr, option)                                          \
+    if (hue->expr##_expr) do {                                          \
+        ret = set_expr(&hue->expr##_pexpr, &hue->expr##_expr,           \
+                       hue->expr##_expr, option, ctx);                  \
+        if (ret < 0)                                                    \
+            return ret;                                                 \
+    } while (0)
+    SET_EXPR(saturation, "s");
+    SET_EXPR(hue_deg,    "h");
+    SET_EXPR(hue,        "H");
+#undef SET_EXPR
 
-    return set_options(ctx, args);
+    av_log(ctx, AV_LOG_VERBOSE,
+           "H_expr:%s h_deg_expr:%s s_expr:%s\n",
+           hue->hue_expr, hue->hue_deg_expr, hue->saturation_expr);
+    compute_sin_and_cos(hue);
+
+    return 0;
 }
 
 static av_cold void uninit(AVFilterContext *ctx)
 {
     HueContext *hue = ctx->priv;
 
-    av_opt_free(hue);
-
-    av_free(hue->hue_deg_expr);
     av_expr_free(hue->hue_deg_pexpr);
-    av_free(hue->hue_expr);
     av_expr_free(hue->hue_pexpr);
-    av_free(hue->saturation_expr);
     av_expr_free(hue->saturation_pexpr);
 }
 
@@ -209,6 +176,7 @@
         AV_PIX_FMT_YUV444P,      AV_PIX_FMT_YUV422P,
         AV_PIX_FMT_YUV420P,      AV_PIX_FMT_YUV411P,
         AV_PIX_FMT_YUV410P,      AV_PIX_FMT_YUV440P,
+        AV_PIX_FMT_YUVA444P,     AV_PIX_FMT_YUVA422P,
         AV_PIX_FMT_YUVA420P,
         AV_PIX_FMT_NONE
     };
@@ -276,71 +244,101 @@
 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
 #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts) * av_q2d(tb))
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpic)
+static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
 {
     HueContext *hue = inlink->dst->priv;
     AVFilterLink *outlink = inlink->dst->outputs[0];
-    AVFilterBufferRef *outpic;
+    AVFrame *outpic;
+    int direct = 0;
 
-    outpic = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
-    if (!outpic) {
-        avfilter_unref_bufferp(&inpic);
-        return AVERROR(ENOMEM);
-    }
-    avfilter_copy_buffer_ref_props(outpic, inpic);
-
-    if (!hue->flat_syntax) {
-        hue->var_values[VAR_T]   = TS2T(inpic->pts, inlink->time_base);
-        hue->var_values[VAR_PTS] = TS2D(inpic->pts);
-
-        if (hue->saturation_expr) {
-            hue->saturation = av_expr_eval(hue->saturation_pexpr, hue->var_values, NULL);
-
-            if (hue->saturation < SAT_MIN_VAL || hue->saturation > SAT_MAX_VAL) {
-                hue->saturation = av_clip(hue->saturation, SAT_MIN_VAL, SAT_MAX_VAL);
-                av_log(inlink->dst, AV_LOG_WARNING,
-                       "Saturation value not in range [%d,%d]: clipping value to %0.1f\n",
-                       SAT_MIN_VAL, SAT_MAX_VAL, hue->saturation);
-            }
+    if (av_frame_is_writable(inpic)) {
+        direct = 1;
+        outpic = inpic;
+    } else {
+        outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+        if (!outpic) {
+            av_frame_free(&inpic);
+            return AVERROR(ENOMEM);
         }
-
-        if (hue->hue_deg_expr) {
-            hue->hue_deg = av_expr_eval(hue->hue_deg_pexpr, hue->var_values, NULL);
-            hue->hue = hue->hue_deg * M_PI / 180;
-        } else if (hue->hue_expr) {
-            hue->hue = av_expr_eval(hue->hue_pexpr, hue->var_values, NULL);
-        }
-
-        av_log(inlink->dst, AV_LOG_DEBUG,
-               "H:%0.1f s:%0.f t:%0.1f n:%d\n",
-               hue->hue, hue->saturation,
-               hue->var_values[VAR_T], (int)hue->var_values[VAR_N]);
-
-        compute_sin_and_cos(hue);
+        av_frame_copy_props(outpic, inpic);
     }
 
-    hue->var_values[VAR_N] += 1;
+    hue->var_values[VAR_N]   = inlink->frame_count;
+    hue->var_values[VAR_T]   = TS2T(inpic->pts, inlink->time_base);
+    hue->var_values[VAR_PTS] = TS2D(inpic->pts);
 
-    av_image_copy_plane(outpic->data[0], outpic->linesize[0],
-                        inpic->data[0],  inpic->linesize[0],
-                        inlink->w, inlink->h);
+    if (hue->saturation_expr) {
+        hue->saturation = av_expr_eval(hue->saturation_pexpr, hue->var_values, NULL);
+
+        if (hue->saturation < SAT_MIN_VAL || hue->saturation > SAT_MAX_VAL) {
+            hue->saturation = av_clip(hue->saturation, SAT_MIN_VAL, SAT_MAX_VAL);
+            av_log(inlink->dst, AV_LOG_WARNING,
+                   "Saturation value not in range [%d,%d]: clipping value to %0.1f\n",
+                   SAT_MIN_VAL, SAT_MAX_VAL, hue->saturation);
+        }
+    }
+
+    if (hue->hue_deg_expr) {
+        hue->hue_deg = av_expr_eval(hue->hue_deg_pexpr, hue->var_values, NULL);
+        hue->hue = hue->hue_deg * M_PI / 180;
+    } else if (hue->hue_expr) {
+        hue->hue = av_expr_eval(hue->hue_pexpr, hue->var_values, NULL);
+        hue->hue_deg = hue->hue * 180 / M_PI;
+    }
+
+    av_log(inlink->dst, AV_LOG_DEBUG,
+           "H:%0.1f*PI h:%0.1f s:%0.f t:%0.1f n:%d\n",
+           hue->hue/M_PI, hue->hue_deg, hue->saturation,
+           hue->var_values[VAR_T], (int)hue->var_values[VAR_N]);
+
+    compute_sin_and_cos(hue);
+
+    if (!direct) {
+        av_image_copy_plane(outpic->data[0], outpic->linesize[0],
+                            inpic->data[0],  inpic->linesize[0],
+                            inlink->w, inlink->h);
+        if (inpic->data[3])
+            av_image_copy_plane(outpic->data[3], outpic->linesize[3],
+                                inpic->data[3],  inpic->linesize[3],
+                                inlink->w, inlink->h);
+    }
 
     process_chrominance(outpic->data[1], outpic->data[2], outpic->linesize[1],
                         inpic->data[1],  inpic->data[2],  inpic->linesize[1],
                         inlink->w >> hue->hsub, inlink->h >> hue->vsub,
                         hue->hue_cos, hue->hue_sin);
 
-    avfilter_unref_bufferp(&inpic);
+    if (!direct)
+        av_frame_free(&inpic);
     return ff_filter_frame(outlink, outpic);
 }
 
 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
                            char *res, int res_len, int flags)
 {
-    if (!strcmp(cmd, "reinit"))
-        return set_options(ctx, args);
-    else
+    HueContext *hue = ctx->priv;
+    int ret;
+
+#define SET_EXPR(expr, option)                                          \
+    do {                                                                \
+        ret = set_expr(&hue->expr##_pexpr, &hue->expr##_expr,           \
+                       args, option, ctx);                              \
+        if (ret < 0)                                                    \
+            return ret;                                                 \
+    } while (0)
+
+    if (!strcmp(cmd, "h")) {
+        SET_EXPR(hue_deg, "h");
+        av_freep(&hue->hue_expr);
+    } else if (!strcmp(cmd, "H")) {
+        SET_EXPR(hue, "H");
+        av_freep(&hue->hue_deg_expr);
+    } else if (!strcmp(cmd, "s")) {
+        SET_EXPR(saturation, "s");
+    } else
         return AVERROR(ENOSYS);
+
+    return 0;
 }
 
 static const AVFilterPad hue_inputs[] = {
@@ -349,7 +347,6 @@
         .type         = AVMEDIA_TYPE_VIDEO,
         .filter_frame = filter_frame,
         .config_props = config_props,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL }
 };
@@ -375,4 +372,5 @@
     .inputs          = hue_inputs,
     .outputs         = hue_outputs,
     .priv_class      = &hue_class,
+    .flags           = AVFILTER_FLAG_SUPPORT_TIMELINE,
 };
diff --git a/libavfilter/vf_idet.c b/libavfilter/vf_idet.c
index 5023630..cd8bbe3 100644
--- a/libavfilter/vf_idet.c
+++ b/libavfilter/vf_idet.c
@@ -47,9 +47,9 @@
 
     uint8_t history[HIST_SIZE];
 
-    AVFilterBufferRef *cur;
-    AVFilterBufferRef *next;
-    AVFilterBufferRef *prev;
+    AVFrame *cur;
+    AVFrame *next;
+    AVFrame *prev;
     int (*filter_line)(const uint8_t *prev, const uint8_t *cur, const uint8_t *next, int w);
 
     const AVPixFmtDescriptor *csp;
@@ -113,8 +113,8 @@
     int match = 0;
 
     for (i = 0; i < idet->csp->nb_components; i++) {
-        int w = idet->cur->video->w;
-        int h = idet->cur->video->h;
+        int w = idet->cur->width;
+        int h = idet->cur->height;
         int refs = idet->cur->linesize[i];
 
         if (i && i<3) {
@@ -165,13 +165,13 @@
     }
 
     if      (idet->last_type == TFF){
-        idet->cur->video->top_field_first = 1;
-        idet->cur->video->interlaced = 1;
+        idet->cur->top_field_first = 1;
+        idet->cur->interlaced_frame = 1;
     }else if(idet->last_type == BFF){
-        idet->cur->video->top_field_first = 0;
-        idet->cur->video->interlaced = 1;
+        idet->cur->top_field_first = 0;
+        idet->cur->interlaced_frame = 1;
     }else if(idet->last_type == PROGRSSIVE){
-        idet->cur->video->interlaced = 0;
+        idet->cur->interlaced_frame = 0;
     }
 
     idet->prestat [           type] ++;
@@ -179,13 +179,13 @@
     av_log(ctx, AV_LOG_DEBUG, "Single frame:%s, Multi frame:%s\n", type2str(type), type2str(idet->last_type));
 }
 
-static int filter_frame(AVFilterLink *link, AVFilterBufferRef *picref)
+static int filter_frame(AVFilterLink *link, AVFrame *picref)
 {
     AVFilterContext *ctx = link->dst;
     IDETContext *idet = ctx->priv;
 
     if (idet->prev)
-        avfilter_unref_buffer(idet->prev);
+        av_frame_free(&idet->prev);
     idet->prev = idet->cur;
     idet->cur  = idet->next;
     idet->next = picref;
@@ -194,7 +194,7 @@
         return 0;
 
     if (!idet->prev)
-        idet->prev = avfilter_ref_buffer(idet->cur, ~0);
+        idet->prev = av_frame_clone(idet->cur);
 
     if (!idet->csp)
         idet->csp = av_pix_fmt_desc_get(link->format);
@@ -203,7 +203,7 @@
 
     filter(ctx);
 
-    return ff_filter_frame(ctx->outputs[0], avfilter_ref_buffer(idet->cur, ~0));
+    return ff_filter_frame(ctx->outputs[0], av_frame_clone(idet->cur));
 }
 
 static int request_frame(AVFilterLink *link)
@@ -238,9 +238,9 @@
            idet->poststat[UNDETERMINED]
     );
 
-    avfilter_unref_bufferp(&idet->prev);
-    avfilter_unref_bufferp(&idet->cur );
-    avfilter_unref_bufferp(&idet->next);
+    av_frame_free(&idet->prev);
+    av_frame_free(&idet->cur );
+    av_frame_free(&idet->next);
 }
 
 static int query_formats(AVFilterContext *ctx)
@@ -273,17 +273,9 @@
     return 0;
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     IDETContext *idet = ctx->priv;
-    static const char *shorthand[] = { "intl_thres", "prog_thres", NULL };
-    int ret;
-
-    idet->class = &idet_class;
-    av_opt_set_defaults(idet);
-
-    if ((ret = av_opt_set_from_string(idet, args, shorthand, "=", ":")) < 0)
-        return ret;
 
     idet->last_type = UNDETERMINED;
     memset(idet->history, UNDETERMINED, HIST_SIZE);
@@ -299,7 +291,6 @@
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
         .filter_frame = filter_frame,
-        .min_perms    = AV_PERM_PRESERVE,
     },
     { NULL }
 };
@@ -308,7 +299,6 @@
     {
         .name          = "default",
         .type          = AVMEDIA_TYPE_VIDEO,
-        .rej_perms     = AV_PERM_WRITE,
         .request_frame = request_frame,
     },
     { NULL }
diff --git a/libavfilter/vf_il.c b/libavfilter/vf_il.c
index 44b5a3b..7edd042 100644
--- a/libavfilter/vf_il.c
+++ b/libavfilter/vf_il.c
@@ -81,20 +81,6 @@
 
 AVFILTER_DEFINE_CLASS(il);
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    IlContext *il = ctx->priv;
-    int ret;
-
-    il->class = &il_class;
-    av_opt_set_defaults(il);
-
-    if ((ret = av_set_options_string(il, args, "=", ":")) < 0)
-        return ret;
-
-    return 0;
-}
-
 static int query_formats(AVFilterContext *ctx)
 {
     AVFilterFormats *formats = NULL;
@@ -160,19 +146,19 @@
     }
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
+static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
 {
     IlContext *il = inlink->dst->priv;
     AVFilterLink *outlink = inlink->dst->outputs[0];
-    AVFilterBufferRef *out;
-    int ret, comp;
+    AVFrame *out;
+    int comp;
 
-    out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!out) {
-        avfilter_unref_bufferp(&inpicref);
+        av_frame_free(&inpicref);
         return AVERROR(ENOMEM);
     }
-    avfilter_copy_buffer_ref_props(out, inpicref);
+    av_frame_copy_props(out, inpicref);
 
     interleave(out->data[0], inpicref->data[0],
                il->linesize[0], inlink->h,
@@ -187,16 +173,15 @@
     }
 
     if (il->has_alpha) {
-        int comp = il->nb_planes - 1;
+        comp = il->nb_planes - 1;
         interleave(out->data[comp], inpicref->data[comp],
                    il->linesize[comp], inlink->h,
                    out->linesize[comp], inpicref->linesize[comp],
                    il->alpha_mode, il->alpha_swap);
     }
 
-    ret = ff_filter_frame(outlink, out);
-    avfilter_unref_bufferp(&inpicref);
-    return ret;
+    av_frame_free(&inpicref);
+    return ff_filter_frame(outlink, out);
 }
 
 static const AVFilterPad inputs[] = {
@@ -222,7 +207,6 @@
     .name          = "il",
     .description   = NULL_IF_CONFIG_SMALL("Deinterleave or interleave fields."),
     .priv_size     = sizeof(IlContext),
-    .init          = init,
     .query_formats = query_formats,
     .inputs        = inputs,
     .outputs       = outputs,
diff --git a/libavfilter/vf_interlace.c b/libavfilter/vf_interlace.c
new file mode 100644
index 0000000..3e787ab
--- /dev/null
+++ b/libavfilter/vf_interlace.c
@@ -0,0 +1,257 @@
+/*
+ * Copyright (c) 2003 Michael Zucchi <notzed@ximian.com>
+ * Copyright (c) 2010 Baptiste Coudurier
+ * Copyright (c) 2011 Stefano Sabatini
+ * Copyright (c) 2013 Vittorio Giovara <vittorio.giovara@gmail.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 General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU 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
+ * progressive to interlaced content filter, inspired by heavy debugging of tinterlace filter
+ */
+
+#include "libavutil/common.h"
+#include "libavutil/opt.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/avassert.h"
+
+#include "formats.h"
+#include "avfilter.h"
+#include "internal.h"
+#include "video.h"
+
+enum ScanMode {
+    MODE_TFF = 0,
+    MODE_BFF = 1,
+};
+
+enum FieldType {
+    FIELD_UPPER = 0,
+    FIELD_LOWER = 1,
+};
+
+typedef struct {
+    const AVClass *class;
+    enum ScanMode scan;    // top or bottom field first scanning
+    int lowpass;           // enable or disable low pass filterning
+    AVFrame *cur, *next;   // the two frames from which the new one is obtained
+    int got_output;        // signal an output frame is reday to request_frame()
+} InterlaceContext;
+
+#define OFFSET(x) offsetof(InterlaceContext, x)
+#define V AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption options[] = {
+    { "scan", "scanning mode", OFFSET(scan),
+        AV_OPT_TYPE_INT,   {.i64 = MODE_TFF }, 0, 1, .flags = V, .unit = "scan" },
+    { "tff", "top field first", 0,
+        AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
+    { "bff", "bottom field first", 0,
+        AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
+    { "lowpass", "enable vertical low-pass filter", OFFSET(lowpass),
+        AV_OPT_TYPE_INT,   {.i64 = 1 },        0, 1, .flags = V },
+    { NULL }
+};
+
+static const AVClass class = {
+    .class_name = "interlace filter",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+
+static const enum AVPixelFormat formats_supported[] = {
+    AV_PIX_FMT_YUV420P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV444P,
+    AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV410P,  AV_PIX_FMT_YUVA420P,
+    AV_PIX_FMT_GRAY8,    AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
+    AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
+};
+
+static int query_formats(AVFilterContext *ctx)
+{
+    ff_set_common_formats(ctx, ff_make_format_list(formats_supported));
+    return 0;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    InterlaceContext *s = ctx->priv;
+
+    av_frame_free(&s->cur);
+    av_frame_free(&s->next);
+}
+
+static int config_out_props(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    AVFilterLink *inlink = outlink->src->inputs[0];
+    InterlaceContext *s = ctx->priv;
+
+    if (inlink->h < 2) {
+        av_log(ctx, AV_LOG_ERROR, "input video height is too small\n");
+        return AVERROR_INVALIDDATA;
+    }
+    // same input size
+    outlink->w = inlink->w;
+    outlink->h = inlink->h;
+    outlink->time_base = inlink->time_base;
+    outlink->frame_rate = inlink->frame_rate;
+    // half framerate
+    outlink->time_base.num *= 2;
+    outlink->frame_rate.den *= 2;
+
+    av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
+           s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
+
+    return 0;
+}
+
+static void copy_picture_field(AVFrame *src_frame, AVFrame *dst_frame,
+                               AVFilterLink *inlink, enum FieldType field_type,
+                               int lowpass)
+{
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+    int vsub = desc->log2_chroma_h;
+    int plane, i, j;
+
+    for (plane = 0; plane < desc->nb_components; plane++) {
+        int lines = (plane == 1 || plane == 2) ? inlink->h >> vsub : inlink->h;
+        int linesize = av_image_get_linesize(inlink->format, inlink->w, plane);
+        uint8_t *dstp = dst_frame->data[plane];
+        const uint8_t *srcp = src_frame->data[plane];
+
+        av_assert0(linesize >= 0);
+
+        lines /= 2;
+        if (field_type == FIELD_LOWER)
+            srcp += src_frame->linesize[plane];
+        if (field_type == FIELD_LOWER)
+            dstp += dst_frame->linesize[plane];
+        if (lowpass) {
+            int srcp_linesize = src_frame->linesize[plane] * 2;
+            int dstp_linesize = dst_frame->linesize[plane] * 2;
+            for (j = lines; j > 0; j--) {
+                const uint8_t *srcp_above = srcp - src_frame->linesize[plane];
+                const uint8_t *srcp_below = srcp + src_frame->linesize[plane];
+                if (j == lines)
+                    srcp_above = srcp; // there is no line above
+                if (j == 1)
+                    srcp_below = srcp; // there is no line below
+                for (i = 0; i < linesize; i++) {
+                    // this calculation is an integer representation of
+                    // '0.5 * current + 0.25 * above + 0.25 + below'
+                    // '1 +' is for rounding.
+                    dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
+                }
+                dstp += dstp_linesize;
+                srcp += srcp_linesize;
+            }
+        } else {
+            av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2,
+                                srcp, src_frame->linesize[plane] * 2,
+                                linesize, lines);
+        }
+    }
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
+{
+    AVFilterContext *ctx = inlink->dst;
+    AVFilterLink *outlink = ctx->outputs[0];
+    InterlaceContext *s = ctx->priv;
+    AVFrame *out;
+    int tff, ret;
+
+    av_frame_free(&s->cur);
+    s->cur  = s->next;
+    s->next = buf;
+
+    /* we need at least two frames */
+    if (!s->cur || !s->next)
+        return 0;
+
+    tff = (s->scan == MODE_TFF);
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+    if (!out)
+        return AVERROR(ENOMEM);
+
+    av_frame_copy_props(out, s->cur);
+    out->interlaced_frame = 1;
+    out->top_field_first  = tff;
+    out->pts             /= 2;  // adjust pts to new framerate
+
+    /* copy upper/lower field from cur */
+    copy_picture_field(s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
+    av_frame_free(&s->cur);
+
+    /* copy lower/upper field from next */
+    copy_picture_field(s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
+    av_frame_free(&s->next);
+
+    ret = ff_filter_frame(outlink, out);
+    s->got_output = 1;
+
+    return ret;
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    InterlaceContext *s  = ctx->priv;
+    int ret = 0;
+
+    s->got_output = 0;
+    while (ret >= 0 && !s->got_output)
+        ret = ff_request_frame(ctx->inputs[0]);
+
+    return ret;
+}
+
+static const AVFilterPad inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .config_props  = config_out_props,
+        .request_frame = request_frame,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_vf_interlace = {
+    .name          = "interlace",
+    .description   = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
+    .uninit        = uninit,
+
+    .priv_class    = &class,
+    .priv_size     = sizeof(InterlaceContext),
+    .query_formats = query_formats,
+
+    .inputs        = inputs,
+    .outputs       = outputs,
+};
+
diff --git a/libavfilter/vf_kerndeint.c b/libavfilter/vf_kerndeint.c
index 9b77e09..8a44868 100644
--- a/libavfilter/vf_kerndeint.c
+++ b/libavfilter/vf_kerndeint.c
@@ -59,23 +59,11 @@
 
 AVFILTER_DEFINE_CLASS(kerndeint);
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    KerndeintContext *kerndeint = ctx->priv;
-    const char const * shorthand[] = { "thresh", "map", "order", "sharp", "twoway", NULL };
-
-    kerndeint->class = &kerndeint_class;
-    av_opt_set_defaults(kerndeint);
-
-    return av_opt_set_from_string(kerndeint, args, shorthand, "=", ":");
-}
-
 static av_cold void uninit(AVFilterContext *ctx)
 {
     KerndeintContext *kerndeint = ctx->priv;
 
     av_free(kerndeint->tmp_data[0]);
-    av_opt_free(kerndeint);
 }
 
 static int query_formats(AVFilterContext *ctx)
@@ -116,11 +104,11 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpic)
+static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
 {
     KerndeintContext *kerndeint = inlink->dst->priv;
     AVFilterLink *outlink = inlink->dst->outputs[0];
-    AVFilterBufferRef *outpic;
+    AVFrame *outpic;
     const uint8_t *prvp;   ///< Previous field's pixel line number n
     const uint8_t *prvpp;  ///< Previous field's pixel line number (n - 1)
     const uint8_t *prvpn;  ///< Previous field's pixel line number (n + 1)
@@ -154,13 +142,13 @@
 
     const int is_packed_rgb = kerndeint->is_packed_rgb;
 
-    outpic = ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN, outlink->w, outlink->h);
+    outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!outpic) {
-        avfilter_unref_bufferp(&inpic);
+        av_frame_free(&inpic);
         return AVERROR(ENOMEM);
     }
-    avfilter_copy_buffer_ref_props(outpic, inpic);
-    outpic->video->interlaced = 0;
+    av_frame_copy_props(outpic, inpic);
+    outpic->interlaced_frame = 0;
 
     for (plane = 0; inpic->data[plane] && plane < 4; plane++) {
         h = plane == 0 ? inlink->h : inlink->h >> kerndeint->vsub;
@@ -295,7 +283,7 @@
         av_image_copy_plane(dstp, psrc_linesize, srcp, src_linesize, bwidth, h);
     }
 
-    avfilter_unref_buffer(inpic);
+    av_frame_free(&inpic);
     return ff_filter_frame(outlink, outpic);
 }
 
@@ -305,7 +293,6 @@
         .type         = AVMEDIA_TYPE_VIDEO,
         .filter_frame = filter_frame,
         .config_props = config_props,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL }
 };
@@ -318,16 +305,16 @@
     { NULL }
 };
 
+
 AVFilter avfilter_vf_kerndeint = {
     .name          = "kerndeint",
     .description   = NULL_IF_CONFIG_SMALL("Apply kernel deinterlacing to the input."),
     .priv_size     = sizeof(KerndeintContext),
-    .init          = init,
     .uninit        = uninit,
     .query_formats = query_formats,
 
     .inputs        = kerndeint_inputs,
     .outputs       = kerndeint_outputs,
 
-    .priv_class = &kerndeint_class,
+    .priv_class    = &kerndeint_class,
 };
diff --git a/libavfilter/vf_libopencv.c b/libavfilter/vf_libopencv.c
index 1b8a5dc..b731a1f 100644
--- a/libavfilter/vf_libopencv.c
+++ b/libavfilter/vf_libopencv.c
@@ -30,12 +30,13 @@
 #include "libavutil/avstring.h"
 #include "libavutil/common.h"
 #include "libavutil/file.h"
+#include "libavutil/opt.h"
 #include "avfilter.h"
 #include "formats.h"
 #include "internal.h"
 #include "video.h"
 
-static void fill_iplimage_from_picref(IplImage *img, const AVFilterBufferRef *picref, enum AVPixelFormat pixfmt)
+static void fill_iplimage_from_frame(IplImage *img, const AVFrame *frame, enum AVPixelFormat pixfmt)
 {
     IplImage *tmpimg;
     int depth, channels_nb;
@@ -45,18 +46,18 @@
     else if (pixfmt == AV_PIX_FMT_BGR24) { depth = IPL_DEPTH_8U;  channels_nb = 3; }
     else return;
 
-    tmpimg = cvCreateImageHeader((CvSize){picref->video->w, picref->video->h}, depth, channels_nb);
+    tmpimg = cvCreateImageHeader((CvSize){frame->width, frame->height}, depth, channels_nb);
     *img = *tmpimg;
-    img->imageData = img->imageDataOrigin = picref->data[0];
+    img->imageData = img->imageDataOrigin = frame->data[0];
     img->dataOrder = IPL_DATA_ORDER_PIXEL;
     img->origin    = IPL_ORIGIN_TL;
-    img->widthStep = picref->linesize[0];
+    img->widthStep = frame->linesize[0];
 }
 
-static void fill_picref_from_iplimage(AVFilterBufferRef *picref, const IplImage *img, enum AVPixelFormat pixfmt)
+static void fill_frame_from_iplimage(AVFrame *frame, const IplImage *img, enum AVPixelFormat pixfmt)
 {
-    picref->linesize[0] = img->widthStep;
-    picref->data[0]     = img->imageData;
+    frame->linesize[0] = img->widthStep;
+    frame->data[0]     = img->imageData;
 }
 
 static int query_formats(AVFilterContext *ctx)
@@ -70,7 +71,9 @@
 }
 
 typedef struct {
-    const char *name;
+    const AVClass *class;
+    char *name;
+    char *params;
     int (*init)(AVFilterContext *ctx, const char *args);
     void (*uninit)(AVFilterContext *ctx);
     void (*end_frame_filter)(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg);
@@ -95,7 +98,7 @@
     smooth->param4 = 0.0;
 
     if (args)
-        sscanf(args, "%127[^:]:%d:%d:%lf:%lf", type_str, &smooth->param1, &smooth->param2, &smooth->param3, &smooth->param4);
+        sscanf(args, "%127[^|]|%d|%d|%lf|%lf", type_str, &smooth->param1, &smooth->param2, &smooth->param3, &smooth->param4);
 
     if      (!strcmp(type_str, "blur"         )) smooth->type = CV_BLUR;
     else if (!strcmp(type_str, "blur_no_scale")) smooth->type = CV_BLUR_NO_SCALE;
@@ -177,7 +180,7 @@
                 p++;
                 break;
             } else
-                (*values)[*cols*i + j] = !!isgraph(*(p++));
+                (*values)[*cols*i + j] = !!av_isgraph(*(p++));
         }
     }
     av_file_unmap(buf, size);
@@ -258,17 +261,18 @@
     const char *buf = args;
     int ret;
 
-    dilate->nb_iterations = 1;
-
     if (args)
-        kernel_str = av_get_token(&buf, ":");
-    if ((ret = parse_iplconvkernel(&dilate->kernel,
-                                   *kernel_str ? kernel_str : default_kernel_str,
-                                   ctx)) < 0)
+        kernel_str = av_get_token(&buf, "|");
+    else
+        kernel_str = av_strdup(default_kernel_str);
+    if (!kernel_str)
+        return AVERROR(ENOMEM);
+    if ((ret = parse_iplconvkernel(&dilate->kernel, kernel_str, ctx)) < 0)
         return ret;
     av_free(kernel_str);
 
-    sscanf(buf, ":%d", &dilate->nb_iterations);
+    if (!buf || sscanf(buf, "|%d", &dilate->nb_iterations) != 1)
+        dilate->nb_iterations = 1;
     av_log(ctx, AV_LOG_VERBOSE, "iterations_nb:%d\n", dilate->nb_iterations);
     if (dilate->nb_iterations <= 0) {
         av_log(ctx, AV_LOG_ERROR, "Invalid non-positive value '%d' for nb_iterations\n",
@@ -314,30 +318,29 @@
     { "smooth", sizeof(SmoothContext), smooth_init, NULL, smooth_end_frame_filter },
 };
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     OCVContext *ocv = ctx->priv;
-    char name[128], priv_args[1024];
     int i;
-    char c;
 
-    sscanf(args, "%127[^=:]%c%1023s", name, &c, priv_args);
-
+    if (!ocv->name) {
+        av_log(ctx, AV_LOG_ERROR, "No libopencv filter name specified\n");
+        return AVERROR(EINVAL);
+    }
     for (i = 0; i < FF_ARRAY_ELEMS(ocv_filter_entries); i++) {
         OCVFilterEntry *entry = &ocv_filter_entries[i];
-        if (!strcmp(name, entry->name)) {
-            ocv->name             = entry->name;
+        if (!strcmp(ocv->name, entry->name)) {
             ocv->init             = entry->init;
             ocv->uninit           = entry->uninit;
             ocv->end_frame_filter = entry->end_frame_filter;
 
             if (!(ocv->priv = av_mallocz(entry->priv_size)))
                 return AVERROR(ENOMEM);
-            return ocv->init(ctx, priv_args);
+            return ocv->init(ctx, ocv->params);
         }
     }
 
-    av_log(ctx, AV_LOG_ERROR, "No libopencv filter named '%s'\n", name);
+    av_log(ctx, AV_LOG_ERROR, "No libopencv filter named '%s'\n", ocv->name);
     return AVERROR(EINVAL);
 }
 
@@ -348,40 +351,53 @@
     if (ocv->uninit)
         ocv->uninit(ctx);
     av_free(ocv->priv);
-    memset(ocv, 0, sizeof(*ocv));
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     AVFilterContext *ctx = inlink->dst;
     OCVContext *ocv = ctx->priv;
     AVFilterLink *outlink= inlink->dst->outputs[0];
-    AVFilterBufferRef *out;
+    AVFrame *out;
     IplImage inimg, outimg;
 
-    out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!out) {
-        avfilter_unref_bufferp(&in);
+        av_frame_free(&in);
         return AVERROR(ENOMEM);
     }
-    avfilter_copy_buffer_ref_props(out, in);
+    av_frame_copy_props(out, in);
 
-    fill_iplimage_from_picref(&inimg , in , inlink->format);
-    fill_iplimage_from_picref(&outimg, out, inlink->format);
+    fill_iplimage_from_frame(&inimg , in , inlink->format);
+    fill_iplimage_from_frame(&outimg, out, inlink->format);
     ocv->end_frame_filter(ctx, &inimg, &outimg);
-    fill_picref_from_iplimage(out, &outimg, inlink->format);
+    fill_frame_from_iplimage(out, &outimg, inlink->format);
 
-    avfilter_unref_bufferp(&in);
+    av_frame_free(&in);
 
     return ff_filter_frame(outlink, out);
 }
 
+#define OFFSET(x) offsetof(OCVContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
+static const AVOption options[] = {
+    { "filter_name",   NULL, OFFSET(name),   AV_OPT_TYPE_STRING, .flags = FLAGS },
+    { "filter_params", NULL, OFFSET(params), AV_OPT_TYPE_STRING, .flags = FLAGS },
+    { NULL },
+};
+
+static const AVClass ocv_class = {
+    .class_name = "ocv",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 static const AVFilterPad avfilter_vf_ocv_inputs[] = {
     {
         .name       = "default",
         .type       = AVMEDIA_TYPE_VIDEO,
         .filter_frame = filter_frame,
-        .min_perms  = AV_PERM_READ
     },
     { NULL }
 };
@@ -399,6 +415,7 @@
     .description = NULL_IF_CONFIG_SMALL("Apply transform using libopencv."),
 
     .priv_size = sizeof(OCVContext),
+    .priv_class = &ocv_class,
 
     .query_formats = query_formats,
     .init = init,
diff --git a/libavfilter/vf_lut.c b/libavfilter/vf_lut.c
index bdfe712..4313e77 100644
--- a/libavfilter/vf_lut.c
+++ b/libavfilter/vf_lut.c
@@ -80,18 +80,18 @@
 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
 static const AVOption options[] = {
-    {"c0", "set component #0 expression", OFFSET(comp_expr_str[0]),  AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
-    {"c1", "set component #1 expression", OFFSET(comp_expr_str[1]),  AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
-    {"c2", "set component #2 expression", OFFSET(comp_expr_str[2]),  AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
-    {"c3", "set component #3 expression", OFFSET(comp_expr_str[3]),  AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
-    {"y",  "set Y expression", OFFSET(comp_expr_str[Y]),  AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
-    {"u",  "set U expression", OFFSET(comp_expr_str[U]),  AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
-    {"v",  "set V expression", OFFSET(comp_expr_str[V]),  AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
-    {"r",  "set R expression", OFFSET(comp_expr_str[R]),  AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
-    {"g",  "set G expression", OFFSET(comp_expr_str[G]),  AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
-    {"b",  "set B expression", OFFSET(comp_expr_str[B]),  AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
-    {"a",  "set A expression", OFFSET(comp_expr_str[A]),  AV_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX, FLAGS},
-    {NULL},
+    { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
+    { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
+    { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
+    { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
+    { "y",  "set Y expression",            OFFSET(comp_expr_str[Y]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
+    { "u",  "set U expression",            OFFSET(comp_expr_str[U]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
+    { "v",  "set V expression",            OFFSET(comp_expr_str[V]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
+    { "r",  "set R expression",            OFFSET(comp_expr_str[R]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
+    { "g",  "set G expression",            OFFSET(comp_expr_str[G]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
+    { "b",  "set B expression",            OFFSET(comp_expr_str[B]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
+    { "a",  "set A expression",            OFFSET(comp_expr_str[A]),  AV_OPT_TYPE_STRING, { .str = "val" }, .flags = FLAGS },
+    { NULL },
 };
 
 static av_cold void uninit(AVFilterContext *ctx)
@@ -253,42 +253,43 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     AVFilterContext *ctx = inlink->dst;
     LutContext *lut = ctx->priv;
     AVFilterLink *outlink = ctx->outputs[0];
-    AVFilterBufferRef *out;
+    AVFrame *out;
     uint8_t *inrow, *outrow, *inrow0, *outrow0;
-    int i, j, plane;
+    int i, j, plane, direct = 0;
 
-    out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
-    if (!out) {
-        avfilter_unref_bufferp(&in);
-        return AVERROR(ENOMEM);
+    if (av_frame_is_writable(in)) {
+        direct = 1;
+        out = in;
+    } else {
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+        if (!out) {
+            av_frame_free(&in);
+            return AVERROR(ENOMEM);
+        }
+        av_frame_copy_props(out, in);
     }
-    avfilter_copy_buffer_ref_props(out, in);
 
     if (lut->is_rgb) {
         /* packed */
         inrow0  = in ->data[0];
         outrow0 = out->data[0];
 
-        for (i = 0; i < in->video->h; i ++) {
+        for (i = 0; i < in->height; i ++) {
             int w = inlink->w;
             const uint8_t (*tab)[256] = (const uint8_t (*)[256])lut->lut;
             inrow  = inrow0;
             outrow = outrow0;
             for (j = 0; j < w; j++) {
-                outrow[0] = tab[0][inrow[0]];
-                if (lut->step>1) {
-                    outrow[1] = tab[1][inrow[1]];
-                    if (lut->step>2) {
-                        outrow[2] = tab[2][inrow[2]];
-                        if (lut->step>3) {
-                            outrow[3] = tab[3][inrow[3]];
-                        }
-                    }
+                switch (lut->step) {
+                case 4:  outrow[3] = tab[3][inrow[3]]; // Fall-through
+                case 3:  outrow[2] = tab[2][inrow[2]]; // Fall-through
+                case 2:  outrow[1] = tab[1][inrow[1]]; // Fall-through
+                default: outrow[0] = tab[0][inrow[0]];
                 }
                 outrow += lut->step;
                 inrow  += lut->step;
@@ -305,7 +306,7 @@
             inrow  = in ->data[plane];
             outrow = out->data[plane];
 
-            for (i = 0; i < (in->video->h + (1<<vsub) - 1)>>vsub; i ++) {
+            for (i = 0; i < (in->height + (1<<vsub) - 1)>>vsub; i ++) {
                 const uint8_t *tab = lut->lut[plane];
                 int w = (inlink->w + (1<<hsub) - 1)>>hsub;
                 for (j = 0; j < w; j++)
@@ -316,7 +317,9 @@
         }
     }
 
-    avfilter_unref_bufferp(&in);
+    if (!direct)
+        av_frame_free(&in);
+
     return ff_filter_frame(outlink, out);
 }
 
@@ -325,7 +328,7 @@
       .type            = AVMEDIA_TYPE_VIDEO,
       .filter_frame    = filter_frame,
       .config_props    = config_props,
-      .min_perms       = AV_PERM_READ, },
+    },
     { .name = NULL}
 };
 static const AVFilterPad outputs[] = {
@@ -333,11 +336,13 @@
       .type            = AVMEDIA_TYPE_VIDEO, },
     { .name = NULL}
 };
+
 #define DEFINE_LUT_FILTER(name_, description_)                          \
     AVFilter avfilter_vf_##name_ = {                                    \
         .name          = #name_,                                        \
         .description   = NULL_IF_CONFIG_SMALL(description_),            \
         .priv_size     = sizeof(LutContext),                            \
+        .priv_class    = &name_ ## _class,                              \
                                                                         \
         .init          = name_##_init,                                  \
         .uninit        = uninit,                                        \
@@ -345,7 +350,7 @@
                                                                         \
         .inputs        = inputs,                                        \
         .outputs       = outputs,                                       \
-        .priv_class    = &name_##_class,                                \
+        .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE,                \
     }
 
 #if CONFIG_LUT_FILTER
@@ -353,17 +358,8 @@
 #define lut_options options
 AVFILTER_DEFINE_CLASS(lut);
 
-static int lut_init(AVFilterContext *ctx, const char *args)
+static int lut_init(AVFilterContext *ctx)
 {
-    LutContext *lut = ctx->priv;
-    int ret;
-
-    lut->class = &lut_class;
-    av_opt_set_defaults(lut);
-
-    if (args && (ret = av_set_options_string(lut, args, "=", ":")) < 0)
-        return ret;
-
     return 0;
 }
 
@@ -375,17 +371,11 @@
 #define lutyuv_options options
 AVFILTER_DEFINE_CLASS(lutyuv);
 
-static int lutyuv_init(AVFilterContext *ctx, const char *args)
+static int lutyuv_init(AVFilterContext *ctx)
 {
     LutContext *lut = ctx->priv;
-    int ret;
 
-    lut->class = &lutyuv_class;
     lut->is_yuv = 1;
-    av_opt_set_defaults(lut);
-
-    if (args && (ret = av_set_options_string(lut, args, "=", ":")) < 0)
-        return ret;
 
     return 0;
 }
@@ -398,17 +388,11 @@
 #define lutrgb_options options
 AVFILTER_DEFINE_CLASS(lutrgb);
 
-static int lutrgb_init(AVFilterContext *ctx, const char *args)
+static int lutrgb_init(AVFilterContext *ctx)
 {
     LutContext *lut = ctx->priv;
-    int ret;
 
-    lut->class = &lutrgb_class;
     lut->is_rgb = 1;
-    av_opt_set_defaults(lut);
-
-    if (args && (ret = av_set_options_string(lut, args, "=", ":")) < 0)
-        return ret;
 
     return 0;
 }
@@ -418,26 +402,30 @@
 
 #if CONFIG_NEGATE_FILTER
 
-#define negate_options options
+static const AVOption negate_options[] = {
+    { "negate_alpha", NULL, OFFSET(negate_alpha), AV_OPT_TYPE_INT, { .i64 = 0 }, .flags = FLAGS },
+    { NULL },
+};
+
 AVFILTER_DEFINE_CLASS(negate);
 
-static int negate_init(AVFilterContext *ctx, const char *args)
+static int negate_init(AVFilterContext *ctx)
 {
     LutContext *lut = ctx->priv;
-    char lut_params[64];
-
-    if (args)
-        sscanf(args, "%d", &lut->negate_alpha);
+    int i;
 
     av_log(ctx, AV_LOG_DEBUG, "negate_alpha:%d\n", lut->negate_alpha);
 
-    snprintf(lut_params, sizeof(lut_params), "c0=negval:c1=negval:c2=negval:a=%s",
-             lut->negate_alpha ? "negval" : "val");
+    for (i = 0; i < 4; i++) {
+        lut->comp_expr_str[i] = av_strdup((i == 3 && !lut->negate_alpha) ?
+                                          "val" : "negval");
+        if (!lut->comp_expr_str[i]) {
+            uninit(ctx);
+            return AVERROR(ENOMEM);
+        }
+    }
 
-    lut->class = &negate_class;
-    av_opt_set_defaults(lut);
-
-    return av_set_options_string(lut, lut_params, "=", ":");
+    return 0;
 }
 
 DEFINE_LUT_FILTER(negate, "Negate input video.");
diff --git a/libavfilter/vf_mp.c b/libavfilter/vf_mp.c
index 8edb20f..ccab7c8 100644
--- a/libavfilter/vf_mp.c
+++ b/libavfilter/vf_mp.c
@@ -32,6 +32,7 @@
 #include "libavutil/pixdesc.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/opt.h"
 
 #include "libmpcodecs/vf.h"
 #include "libmpcodecs/img_format.h"
@@ -122,20 +123,13 @@
     {0, AV_PIX_FMT_NONE}
 };
 
-extern const vf_info_t ff_vf_info_detc;
 extern const vf_info_t ff_vf_info_dint;
-extern const vf_info_t ff_vf_info_divtc;
-extern const vf_info_t ff_vf_info_down3dright;
 extern const vf_info_t ff_vf_info_eq2;
 extern const vf_info_t ff_vf_info_eq;
 extern const vf_info_t ff_vf_info_fil;
-//extern const vf_info_t ff_vf_info_filmdint;
 extern const vf_info_t ff_vf_info_fspp;
-extern const vf_info_t ff_vf_info_harddup;
 extern const vf_info_t ff_vf_info_ilpack;
-extern const vf_info_t ff_vf_info_ivtc;
 extern const vf_info_t ff_vf_info_mcdeint;
-extern const vf_info_t ff_vf_info_noise;
 extern const vf_info_t ff_vf_info_ow;
 extern const vf_info_t ff_vf_info_perspective;
 extern const vf_info_t ff_vf_info_phase;
@@ -145,27 +139,18 @@
 extern const vf_info_t ff_vf_info_sab;
 extern const vf_info_t ff_vf_info_softpulldown;
 extern const vf_info_t ff_vf_info_spp;
-extern const vf_info_t ff_vf_info_stereo3d;
-extern const vf_info_t ff_vf_info_telecine;
 extern const vf_info_t ff_vf_info_tinterlace;
 extern const vf_info_t ff_vf_info_uspp;
 
 
 static const vf_info_t* const filters[]={
-    &ff_vf_info_detc,
     &ff_vf_info_dint,
-    &ff_vf_info_divtc,
-    &ff_vf_info_down3dright,
     &ff_vf_info_eq2,
     &ff_vf_info_eq,
     &ff_vf_info_fil,
-//    &ff_vf_info_filmdint, cmmx.h vd.h ‘opt_screen_size_x’
     &ff_vf_info_fspp,
-    &ff_vf_info_harddup,
     &ff_vf_info_ilpack,
-    &ff_vf_info_ivtc,
     &ff_vf_info_mcdeint,
-    &ff_vf_info_noise,
     &ff_vf_info_ow,
     &ff_vf_info_perspective,
     &ff_vf_info_phase,
@@ -175,8 +160,6 @@
     &ff_vf_info_sab,
     &ff_vf_info_softpulldown,
     &ff_vf_info_spp,
-    &ff_vf_info_stereo3d,
-    &ff_vf_info_telecine,
     &ff_vf_info_tinterlace,
     &ff_vf_info_uspp,
 
@@ -270,12 +253,23 @@
 }
 
 typedef struct {
+    const AVClass *class;
     vf_instance_t vf;
     vf_instance_t next_vf;
     AVFilterContext *avfctx;
     int frame_returned;
+    char *filter;
 } MPContext;
 
+#define OFFSET(x) offsetof(MPContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption mp_options[] = {
+    { "filter", "set MPlayer filter name and parameters", OFFSET(filter), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(mp);
+
 void ff_mp_msg(int mod, int lev, const char *format, ... ){
     va_list va;
     va_start(va, format);
@@ -538,58 +532,50 @@
   return mpi;
 }
 
+static void dummy_free(void *opaque, uint8_t *data){}
 
 int ff_vf_next_put_image(struct vf_instance *vf,mp_image_t *mpi, double pts){
-    MPContext *m= (void*)vf;
+    MPContext *m= (MPContext*)(((uint8_t*)vf) - offsetof(MPContext, vf));
     AVFilterLink *outlink     = m->avfctx->outputs[0];
-    AVFilterBuffer    *pic    = av_mallocz(sizeof(AVFilterBuffer));
-    AVFilterBufferRef *picref = av_mallocz(sizeof(AVFilterBufferRef));
+    AVFrame *picref = av_frame_alloc();
     int i;
 
     av_assert0(vf->next);
 
     av_log(m->avfctx, AV_LOG_DEBUG, "ff_vf_next_put_image\n");
 
-    if (!pic || !picref)
+    if (!picref)
         goto fail;
 
-    picref->buf = pic;
-    picref->buf->free= (void*)av_free;
-    if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
-        goto fail;
+    picref->width  = mpi->w;
+    picref->height = mpi->h;
 
-    pic->w = picref->video->w = mpi->w;
-    pic->h = picref->video->h = mpi->h;
-
-    /* make sure the buffer gets read permission or it's useless for output */
-    picref->perms = AV_PERM_READ | AV_PERM_REUSE2;
-//    av_assert0(mpi->flags&MP_IMGFLAG_READABLE);
-    if(!(mpi->flags&MP_IMGFLAG_PRESERVE))
-        picref->perms |= AV_PERM_WRITE;
-
-    pic->refcount = 1;
     picref->type = AVMEDIA_TYPE_VIDEO;
 
     for(i=0; conversion_map[i].fmt && mpi->imgfmt != conversion_map[i].fmt; i++);
-    pic->format = picref->format = conversion_map[i].pix_fmt;
+    picref->format = conversion_map[i].pix_fmt;
 
-    memcpy(pic->data,        mpi->planes,   FFMIN(sizeof(pic->data)    , sizeof(mpi->planes)));
-    memcpy(pic->linesize,    mpi->stride,   FFMIN(sizeof(pic->linesize), sizeof(mpi->stride)));
-    memcpy(picref->data,     pic->data,     sizeof(picref->data));
-    memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
+    memcpy(picref->linesize, mpi->stride, FFMIN(sizeof(picref->linesize), sizeof(mpi->stride)));
+
+    for(i=0; i<4 && mpi->stride[i]; i++){
+        picref->data[i] = mpi->planes[i];
+    }
 
     if(pts != MP_NOPTS_VALUE)
         picref->pts= pts * av_q2d(outlink->time_base);
 
+    if(1) { // mp buffers are currently unsupported in libavfilter, we thus must copy
+        AVFrame *tofree = picref;
+        picref = av_frame_clone(picref);
+        av_frame_free(&tofree);
+    }
+
     ff_filter_frame(outlink, picref);
     m->frame_returned++;
 
     return 1;
 fail:
-    if (picref && picref->video)
-        av_free(picref->video);
-    av_free(picref);
-    av_free(pic);
+    av_frame_free(&picref);
     return 0;
 }
 
@@ -623,13 +609,13 @@
 }
 
 int ff_vf_next_control(struct vf_instance *vf, int request, void* data){
-    MPContext *m= (void*)vf;
+    MPContext *m= (MPContext*)(((uint8_t*)vf) - offsetof(MPContext, vf));
     av_log(m->avfctx, AV_LOG_DEBUG, "Received control %d\n", request);
     return 0;
 }
 
 static int vf_default_query_format(struct vf_instance *vf, unsigned int fmt){
-    MPContext *m= (void*)vf;
+    MPContext *m= (MPContext*)(((uint8_t*)vf) - offsetof(MPContext, vf));
     int i;
     av_log(m->avfctx, AV_LOG_DEBUG, "query %X\n", fmt);
 
@@ -641,14 +627,29 @@
 }
 
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     MPContext *m = ctx->priv;
+    int cpu_flags = av_get_cpu_flags();
     char name[256];
+    const char *args;
     int i;
 
+    ff_gCpuCaps.hasMMX      = cpu_flags & AV_CPU_FLAG_MMX;
+    ff_gCpuCaps.hasMMX2     = cpu_flags & AV_CPU_FLAG_MMX2;
+    ff_gCpuCaps.hasSSE      = cpu_flags & AV_CPU_FLAG_SSE;
+    ff_gCpuCaps.hasSSE2     = cpu_flags & AV_CPU_FLAG_SSE2;
+    ff_gCpuCaps.hasSSE3     = cpu_flags & AV_CPU_FLAG_SSE3;
+    ff_gCpuCaps.hasSSSE3    = cpu_flags & AV_CPU_FLAG_SSSE3;
+    ff_gCpuCaps.hasSSE4     = cpu_flags & AV_CPU_FLAG_SSE4;
+    ff_gCpuCaps.hasSSE42    = cpu_flags & AV_CPU_FLAG_SSE42;
+    ff_gCpuCaps.hasAVX      = cpu_flags & AV_CPU_FLAG_AVX;
+    ff_gCpuCaps.has3DNow    = cpu_flags & AV_CPU_FLAG_3DNOW;
+    ff_gCpuCaps.has3DNowExt = cpu_flags & AV_CPU_FLAG_3DNOWEXT;
+
     m->avfctx= ctx;
 
+    args = m->filter;
     if(!args || 1!=sscanf(args, "%255[^:=]", name)){
         av_log(ctx, AV_LOG_ERROR, "Invalid parameter.\n");
         return AVERROR(EINVAL);
@@ -795,12 +796,12 @@
     return ret;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpic)
+static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
 {
     MPContext *m = inlink->dst->priv;
     int i;
     double pts= MP_NOPTS_VALUE;
-    mp_image_t* mpi = ff_new_mp_image(inpic->video->w, inpic->video->h);
+    mp_image_t* mpi = ff_new_mp_image(inpic->width, inpic->height);
 
     if(inpic->pts != AV_NOPTS_VALUE)
         pts= inpic->pts / av_q2d(inlink->time_base);
@@ -815,12 +816,12 @@
 
     // mpi->flags|=MP_IMGFLAG_ALLOCATED; ?
     mpi->flags |= MP_IMGFLAG_READABLE;
-    if(!(inpic->perms & AV_PERM_WRITE))
+    if(!av_frame_is_writable(inpic))
         mpi->flags |= MP_IMGFLAG_PRESERVE;
     if(m->vf.put_image(&m->vf, mpi, pts) == 0){
         av_log(m->avfctx, AV_LOG_DEBUG, "put_image() says skip\n");
     }else{
-        avfilter_unref_buffer(inpic);
+        av_frame_free(&inpic);
     }
     ff_free_mp_image(mpi);
     return 0;
@@ -832,7 +833,6 @@
         .type         = AVMEDIA_TYPE_VIDEO,
         .filter_frame = filter_frame,
         .config_props = config_inprops,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL }
 };
@@ -856,4 +856,5 @@
     .query_formats = query_formats,
     .inputs        = mp_inputs,
     .outputs       = mp_outputs,
+    .priv_class    = &mp_class,
 };
diff --git a/libavfilter/vf_mpdecimate.c b/libavfilter/vf_mpdecimate.c
new file mode 100644
index 0000000..2e386f7
--- /dev/null
+++ b/libavfilter/vf_mpdecimate.c
@@ -0,0 +1,258 @@
+/*
+ * Copyright (c) 2003 Rich Felker
+ * Copyright (c) 2012 Stefano Sabatini
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU 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 mpdecimate filter, ported from libmpcodecs/vf_decimate.c by
+ * Rich Felker.
+ */
+
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/timestamp.h"
+#include "libavcodec/dsputil.h"
+#include "avfilter.h"
+#include "internal.h"
+#include "formats.h"
+#include "video.h"
+
+typedef struct {
+    const AVClass *class;
+    int lo, hi;                    ///< lower and higher threshold number of differences
+                                   ///< values for 8x8 blocks
+
+    float frac;                    ///< threshold of changed pixels over the total fraction
+
+    int max_drop_count;            ///< if positive: maximum number of sequential frames to drop
+                                   ///< if negative: minimum number of frames between two drops
+
+    int drop_count;                ///< if positive: number of frames sequentially dropped
+                                   ///< if negative: number of sequential frames which were not dropped
+
+    int hsub, vsub;                ///< chroma subsampling values
+    AVFrame *ref;                  ///< reference picture
+    DSPContext dspctx;             ///< context providing optimized diff routines
+    AVCodecContext *avctx;         ///< codec context required for the DSPContext
+} DecimateContext;
+
+#define OFFSET(x) offsetof(DecimateContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption mpdecimate_options[] = {
+    { "max",  "set the maximum number of consecutive dropped frames (positive), or the minimum interval between dropped frames (negative)",
+      OFFSET(max_drop_count), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, FLAGS },
+    { "hi",   "set high dropping threshold", OFFSET(hi), AV_OPT_TYPE_INT, {.i64=64*12}, INT_MIN, INT_MAX, FLAGS },
+    { "lo",   "set low dropping threshold", OFFSET(lo), AV_OPT_TYPE_INT, {.i64=64*5}, INT_MIN, INT_MAX, FLAGS },
+    { "frac", "set fraction dropping threshold",  OFFSET(frac), AV_OPT_TYPE_FLOAT, {.dbl=0.33}, 0, 1, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(mpdecimate);
+
+/**
+ * Return 1 if the two planes are different, 0 otherwise.
+ */
+static int diff_planes(AVFilterContext *ctx,
+                       uint8_t *cur, uint8_t *ref, int linesize,
+                       int w, int h)
+{
+    DecimateContext *decimate = ctx->priv;
+    DSPContext *dspctx = &decimate->dspctx;
+
+    int x, y;
+    int d, c = 0;
+    int t = (w/16)*(h/16)*decimate->frac;
+    int16_t block[8*8];
+
+    /* compute difference for blocks of 8x8 bytes */
+    for (y = 0; y < h-7; y += 4) {
+        for (x = 8; x < w-7; x += 4) {
+            dspctx->diff_pixels(block,
+                                cur+x+y*linesize,
+                                ref+x+y*linesize, linesize);
+            d = dspctx->sum_abs_dctelem(block);
+            if (d > decimate->hi)
+                return 1;
+            if (d > decimate->lo) {
+                c++;
+                if (c > t)
+                    return 1;
+            }
+        }
+    }
+    return 0;
+}
+
+/**
+ * Tell if the frame should be decimated, for example if it is no much
+ * different with respect to the reference frame ref.
+ */
+static int decimate_frame(AVFilterContext *ctx,
+                          AVFrame *cur, AVFrame *ref)
+{
+    DecimateContext *decimate = ctx->priv;
+    int plane;
+
+    if (decimate->max_drop_count > 0 &&
+        decimate->drop_count >= decimate->max_drop_count)
+        return 0;
+    if (decimate->max_drop_count < 0 &&
+        (decimate->drop_count-1) > decimate->max_drop_count)
+        return 0;
+
+    for (plane = 0; ref->data[plane] && ref->linesize[plane]; plane++) {
+        int vsub = plane == 1 || plane == 2 ? decimate->vsub : 0;
+        int hsub = plane == 1 || plane == 2 ? decimate->hsub : 0;
+        if (diff_planes(ctx,
+                        cur->data[plane], ref->data[plane], ref->linesize[plane],
+                        ref->width>>hsub, ref->height>>vsub))
+            return 0;
+    }
+
+    return 1;
+}
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    DecimateContext *decimate = ctx->priv;
+
+    av_log(ctx, AV_LOG_VERBOSE, "max_drop_count:%d hi:%d lo:%d frac:%f\n",
+           decimate->max_drop_count, decimate->hi, decimate->lo, decimate->frac);
+
+    decimate->avctx = avcodec_alloc_context3(NULL);
+    if (!decimate->avctx)
+        return AVERROR(ENOMEM);
+    avpriv_dsputil_init(&decimate->dspctx, decimate->avctx);
+
+    return 0;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    DecimateContext *decimate = ctx->priv;
+    av_frame_free(&decimate->ref);
+    if (decimate->avctx) {
+        avcodec_close(decimate->avctx);
+        av_freep(&decimate->avctx);
+    }
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_YUV444P,      AV_PIX_FMT_YUV422P,
+        AV_PIX_FMT_YUV420P,      AV_PIX_FMT_YUV411P,
+        AV_PIX_FMT_YUV410P,      AV_PIX_FMT_YUV440P,
+        AV_PIX_FMT_YUVJ444P,     AV_PIX_FMT_YUVJ422P,
+        AV_PIX_FMT_YUVJ420P,     AV_PIX_FMT_YUVJ440P,
+        AV_PIX_FMT_YUVA420P,
+        AV_PIX_FMT_NONE
+    };
+
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+
+    return 0;
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    DecimateContext *decimate = ctx->priv;
+    const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
+    decimate->hsub = pix_desc->log2_chroma_w;
+    decimate->vsub = pix_desc->log2_chroma_h;
+
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *cur)
+{
+    DecimateContext *decimate = inlink->dst->priv;
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+    int ret;
+
+    if (decimate->ref && decimate_frame(inlink->dst, cur, decimate->ref)) {
+        decimate->drop_count = FFMAX(1, decimate->drop_count+1);
+    } else {
+        av_frame_free(&decimate->ref);
+        decimate->ref = cur;
+        decimate->drop_count = FFMIN(-1, decimate->drop_count-1);
+
+        if (ret = ff_filter_frame(outlink, av_frame_clone(cur)) < 0)
+            return ret;
+    }
+
+    av_log(inlink->dst, AV_LOG_DEBUG,
+           "%s pts:%s pts_time:%s drop_count:%d\n",
+           decimate->drop_count > 0 ? "drop" : "keep",
+           av_ts2str(cur->pts), av_ts2timestr(cur->pts, &inlink->time_base),
+           decimate->drop_count);
+
+    if (decimate->drop_count > 0)
+        av_frame_free(&cur);
+
+    return 0;
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+    DecimateContext *decimate = outlink->src->priv;
+    AVFilterLink *inlink = outlink->src->inputs[0];
+    int ret;
+
+    do {
+        ret = ff_request_frame(inlink);
+    } while (decimate->drop_count > 0 && ret >= 0);
+
+    return ret;
+}
+
+static const AVFilterPad mpdecimate_inputs[] = {
+    {
+        .name             = "default",
+        .type             = AVMEDIA_TYPE_VIDEO,
+        .get_video_buffer = ff_null_get_video_buffer,
+        .config_props     = config_input,
+        .filter_frame     = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad mpdecimate_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .request_frame = request_frame,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_vf_mpdecimate = {
+    .name        = "mpdecimate",
+    .description = NULL_IF_CONFIG_SMALL("Remove near-duplicate frames."),
+    .init        = init,
+    .uninit      = uninit,
+
+    .priv_size = sizeof(DecimateContext),
+    .query_formats = query_formats,
+    .inputs        = mpdecimate_inputs,
+    .outputs       = mpdecimate_outputs,
+    .priv_class    = &mpdecimate_class,
+};
diff --git a/libavfilter/vf_noise.c b/libavfilter/vf_noise.c
index d3e443b..996311a 100644
--- a/libavfilter/vf_noise.c
+++ b/libavfilter/vf_noise.c
@@ -29,6 +29,7 @@
 #include "libavutil/lfg.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/pixdesc.h"
+#include "libavutil/x86/asm.h"
 #include "avfilter.h"
 #include "formats.h"
 #include "internal.h"
@@ -40,7 +41,6 @@
 
 #define NOISE_UNIFORM  1
 #define NOISE_TEMPORAL 2
-#define NOISE_QUALITY  4
 #define NOISE_AVERAGED 8
 #define NOISE_PATTERN  16
 
@@ -63,6 +63,8 @@
     FilterParams param[4];
     int rand_shift[MAX_RES];
     int rand_shift_init;
+    void (*line_noise)(uint8_t *dst, const uint8_t *src, int8_t *noise, int len, int shift);
+    void (*line_noise_avg)(uint8_t *dst, const uint8_t *src, int len, int8_t **shift);
 } NoiseContext;
 
 #define OFFSET(x) offsetof(NoiseContext, x)
@@ -76,7 +78,6 @@
     {#name"f", "set component #"#x" flags", OFFSET(param.flags), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, 31, FLAGS, #name"_flags"},      \
     {"a", "averaged noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_AVERAGED}, 0, 0, FLAGS, #name"_flags"},                            \
     {"p", "(semi)regular pattern", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_PATTERN},  0, 0, FLAGS, #name"_flags"},                     \
-    {"q", "high quality",   0, AV_OPT_TYPE_CONST, {.i64=NOISE_QUALITY},  0, 0, FLAGS, #name"_flags"},                            \
     {"t", "temporal noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_TEMPORAL}, 0, 0, FLAGS, #name"_flags"},                            \
     {"u", "uniform noise",  0, AV_OPT_TYPE_CONST, {.i64=NOISE_UNIFORM},  0, 0, FLAGS, #name"_flags"},
 
@@ -128,8 +129,8 @@
         } else {
             double x1, x2, w, y1;
             do {
-                x1 = 2.0 * av_lfg_get(lfg) / (float)RAND_MAX - 1.0;
-                x2 = 2.0 * av_lfg_get(lfg) / (float)RAND_MAX - 1.0;
+                x1 = 2.0 * av_lfg_get(lfg) / (float)UINT_MAX - 1.0;
+                x2 = 2.0 * av_lfg_get(lfg) / (float)UINT_MAX - 1.0;
                 w = x1 * x1 + x2 * x2;
             } while (w >= 1.0);
 
@@ -164,36 +165,6 @@
     return 0;
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    NoiseContext *n = ctx->priv;
-    int ret, i;
-
-    n->class = &noise_class;
-    av_opt_set_defaults(n);
-
-    if ((ret = av_set_options_string(n, args, "=", ":")) < 0)
-        return ret;
-
-    for (i = 0; i < 4; i++) {
-        if (n->all.seed >= 0)
-            n->param[i].seed = n->all.seed;
-        else
-            n->param[i].seed = 123457;
-        if (n->all.strength)
-            n->param[i].strength = n->all.strength;
-        if (n->all.flags)
-            n->param[i].flags = n->all.flags;
-    }
-
-    for (i = 0; i < 4; i++) {
-        if (n->param[i].strength && ((ret = init_noise(n, i)) < 0))
-            return ret;
-    }
-
-    return 0;
-}
-
 static int query_formats(AVFilterContext *ctx)
 {
     AVFilterFormats *formats = NULL;
@@ -228,7 +199,7 @@
     return 0;
 }
 
-static void line_noise(uint8_t *dst, const uint8_t *src, int8_t *noise,
+static inline void line_noise_c(uint8_t *dst, const uint8_t *src, int8_t *noise,
                        int len, int shift)
 {
     int i;
@@ -241,7 +212,69 @@
     }
 }
 
-static void line_noise_avg(uint8_t *dst, const uint8_t *src,
+#define ASMALIGN(ZEROBITS) ".p2align " #ZEROBITS "\n\t"
+
+static void line_noise_mmx(uint8_t *dst, const uint8_t *src,
+                           int8_t *noise, int len, int shift)
+{
+#if HAVE_MMX_INLINE
+    x86_reg mmx_len= len&(~7);
+    noise+=shift;
+
+    __asm__ volatile(
+            "mov %3, %%"REG_a"               \n\t"
+            "pcmpeqb %%mm7, %%mm7            \n\t"
+            "psllw $15, %%mm7                \n\t"
+            "packsswb %%mm7, %%mm7           \n\t"
+            ASMALIGN(4)
+            "1:                              \n\t"
+            "movq (%0, %%"REG_a"), %%mm0     \n\t"
+            "movq (%1, %%"REG_a"), %%mm1     \n\t"
+            "pxor %%mm7, %%mm0               \n\t"
+            "paddsb %%mm1, %%mm0             \n\t"
+            "pxor %%mm7, %%mm0               \n\t"
+            "movq %%mm0, (%2, %%"REG_a")     \n\t"
+            "add $8, %%"REG_a"               \n\t"
+            " js 1b                          \n\t"
+            :: "r" (src+mmx_len), "r" (noise+mmx_len), "r" (dst+mmx_len), "g" (-mmx_len)
+            : "%"REG_a
+    );
+    if (mmx_len!=len)
+        line_noise_c(dst+mmx_len, src+mmx_len, noise+mmx_len, len-mmx_len, 0);
+#endif
+}
+
+static void line_noise_mmxext(uint8_t *dst, const uint8_t *src,
+                              int8_t *noise, int len, int shift)
+{
+#if HAVE_MMXEXT_INLINE
+    x86_reg mmx_len= len&(~7);
+    noise+=shift;
+
+    __asm__ volatile(
+            "mov %3, %%"REG_a"                \n\t"
+            "pcmpeqb %%mm7, %%mm7             \n\t"
+            "psllw $15, %%mm7                 \n\t"
+            "packsswb %%mm7, %%mm7            \n\t"
+            ASMALIGN(4)
+            "1:                               \n\t"
+            "movq (%0, %%"REG_a"), %%mm0      \n\t"
+            "movq (%1, %%"REG_a"), %%mm1      \n\t"
+            "pxor %%mm7, %%mm0                \n\t"
+            "paddsb %%mm1, %%mm0              \n\t"
+            "pxor %%mm7, %%mm0                \n\t"
+            "movntq %%mm0, (%2, %%"REG_a")    \n\t"
+            "add $8, %%"REG_a"                \n\t"
+            " js 1b                           \n\t"
+            :: "r" (src+mmx_len), "r" (noise+mmx_len), "r" (dst+mmx_len), "g" (-mmx_len)
+            : "%"REG_a
+            );
+    if (mmx_len != len)
+        line_noise_c(dst+mmx_len, src+mmx_len, noise+mmx_len, len-mmx_len, 0);
+#endif
+}
+
+static inline void line_noise_avg_c(uint8_t *dst, const uint8_t *src,
                            int len, int8_t **shift)
 {
     int i;
@@ -253,6 +286,50 @@
     }
 }
 
+static inline void line_noise_avg_mmx(uint8_t *dst, const uint8_t *src,
+                                      int len, int8_t **shift)
+{
+#if HAVE_MMX_INLINE
+    x86_reg mmx_len= len&(~7);
+
+    __asm__ volatile(
+            "mov %5, %%"REG_a"              \n\t"
+            ASMALIGN(4)
+            "1:                             \n\t"
+            "movq (%1, %%"REG_a"), %%mm1    \n\t"
+            "movq (%0, %%"REG_a"), %%mm0    \n\t"
+            "paddb (%2, %%"REG_a"), %%mm1   \n\t"
+            "paddb (%3, %%"REG_a"), %%mm1   \n\t"
+            "movq %%mm0, %%mm2              \n\t"
+            "movq %%mm1, %%mm3              \n\t"
+            "punpcklbw %%mm0, %%mm0         \n\t"
+            "punpckhbw %%mm2, %%mm2         \n\t"
+            "punpcklbw %%mm1, %%mm1         \n\t"
+            "punpckhbw %%mm3, %%mm3         \n\t"
+            "pmulhw %%mm0, %%mm1            \n\t"
+            "pmulhw %%mm2, %%mm3            \n\t"
+            "paddw %%mm1, %%mm1             \n\t"
+            "paddw %%mm3, %%mm3             \n\t"
+            "paddw %%mm0, %%mm1             \n\t"
+            "paddw %%mm2, %%mm3             \n\t"
+            "psrlw $8, %%mm1                \n\t"
+            "psrlw $8, %%mm3                \n\t"
+            "packuswb %%mm3, %%mm1          \n\t"
+            "movq %%mm1, (%4, %%"REG_a")    \n\t"
+            "add $8, %%"REG_a"              \n\t"
+            " js 1b                         \n\t"
+            :: "r" (src+mmx_len), "r" (shift[0]+mmx_len), "r" (shift[1]+mmx_len), "r" (shift[2]+mmx_len),
+               "r" (dst+mmx_len), "g" (-mmx_len)
+            : "%"REG_a
+        );
+
+    if (mmx_len != len){
+        int8_t *shift2[3]={shift[0]+mmx_len, shift[1]+mmx_len, shift[2]+mmx_len};
+        line_noise_avg_c(dst+mmx_len, src+mmx_len, len-mmx_len, shift2);
+    }
+#endif
+}
+
 static void noise(uint8_t *dst, const uint8_t *src,
                   int dst_linesize, int src_linesize,
                   int width, int height, NoiseContext *n, int comp)
@@ -280,14 +357,11 @@
         else
             shift = n->rand_shift[y];
 
-        if (!(flags & NOISE_QUALITY))
-            shift &= ~7;
-
         if (flags & NOISE_AVERAGED) {
-            line_noise_avg(dst, src, width, n->param[comp].prev_shift[y]);
+            n->line_noise_avg(dst, src, width, n->param[comp].prev_shift[y]);
             n->param[comp].prev_shift[y][n->param[comp].shiftptr] = noise + shift;
         } else {
-            line_noise(dst, src, noise, width, shift);
+            n->line_noise(dst, src, noise, width, shift);
         }
         dst += dst_linesize;
         src += src_linesize;
@@ -298,32 +372,65 @@
         n->param[comp].shiftptr = 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
+static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
 {
     NoiseContext *n = inlink->dst->priv;
     AVFilterLink *outlink = inlink->dst->outputs[0];
-    AVFilterBufferRef *out;
-    int ret, i;
+    AVFrame *out;
+    int i;
 
-    if (inpicref->perms & AV_PERM_WRITE) {
+    if (av_frame_is_writable(inpicref)) {
         out = inpicref;
     } else {
-        out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
         if (!out) {
-            avfilter_unref_bufferp(&inpicref);
+            av_frame_free(&inpicref);
             return AVERROR(ENOMEM);
         }
-        avfilter_copy_buffer_ref_props(out, inpicref);
+        av_frame_copy_props(out, inpicref);
     }
 
     for (i = 0; i < n->nb_planes; i++)
         noise(out->data[i], inpicref->data[i], out->linesize[i],
               inpicref->linesize[i], n->linesize[i], n->height[i], n, i);
 
-    ret = ff_filter_frame(outlink, out);
     if (inpicref != out)
-        avfilter_unref_buffer(inpicref);
-    return ret;
+        av_frame_free(&inpicref);
+    return ff_filter_frame(outlink, out);
+}
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    NoiseContext *n = ctx->priv;
+    int ret, i;
+    int cpu_flags = av_get_cpu_flags();
+
+    for (i = 0; i < 4; i++) {
+        if (n->all.seed >= 0)
+            n->param[i].seed = n->all.seed;
+        else
+            n->param[i].seed = 123457;
+        if (n->all.strength)
+            n->param[i].strength = n->all.strength;
+        if (n->all.flags)
+            n->param[i].flags = n->all.flags;
+    }
+
+    for (i = 0; i < 4; i++) {
+        if (n->param[i].strength && ((ret = init_noise(n, i)) < 0))
+            return ret;
+    }
+
+    if (HAVE_MMX_INLINE &&
+        cpu_flags & AV_CPU_FLAG_MMX) {
+        n->line_noise = line_noise_mmx;
+        n->line_noise_avg = line_noise_avg_mmx;
+    }
+    if (HAVE_MMXEXT_INLINE &&
+        cpu_flags & AV_CPU_FLAG_MMXEXT)
+        n->line_noise = line_noise_mmxext;
+
+    return 0;
 }
 
 static av_cold void uninit(AVFilterContext *ctx)
@@ -333,7 +440,6 @@
 
     for (i = 0; i < 4; i++)
         av_freep(&n->param[i].noise);
-    av_opt_free(n);
 }
 
 static const AVFilterPad noise_inputs[] = {
@@ -343,7 +449,6 @@
         .get_video_buffer = ff_null_get_video_buffer,
         .filter_frame     = filter_frame,
         .config_props     = config_input,
-        .min_perms        = AV_PERM_READ,
     },
     { NULL }
 };
@@ -366,4 +471,5 @@
     .inputs        = noise_inputs,
     .outputs       = noise_outputs,
     .priv_class    = &noise_class,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE,
 };
diff --git a/libavfilter/vf_overlay.c b/libavfilter/vf_overlay.c
index 9812255..dbb87f8 100644
--- a/libavfilter/vf_overlay.c
+++ b/libavfilter/vf_overlay.c
@@ -36,6 +36,7 @@
 #include "libavutil/pixdesc.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/opt.h"
 #include "libavutil/timestamp.h"
 #include "internal.h"
 #include "bufferqueue.h"
@@ -47,6 +48,13 @@
     "main_h",    "H", ///< height of the main    video
     "overlay_w", "w", ///< width  of the overlay video
     "overlay_h", "h", ///< height of the overlay video
+    "hsub",
+    "vsub",
+    "x",
+    "y",
+    "n",            ///< number of frame
+    "pos",          ///< position in the file
+    "t",            ///< timestamp expressed in seconds
     NULL
 };
 
@@ -55,6 +63,13 @@
     VAR_MAIN_H,    VAR_MH,
     VAR_OVERLAY_W, VAR_OW,
     VAR_OVERLAY_H, VAR_OH,
+    VAR_HSUB,
+    VAR_VSUB,
+    VAR_X,
+    VAR_Y,
+    VAR_N,
+    VAR_POS,
+    VAR_T,
     VAR_VARS_NB
 };
 
@@ -73,6 +88,7 @@
 typedef struct {
     const AVClass *class;
     int x, y;                   ///< position of overlayed picture
+    int enable;                 ///< tells if blending is enabled
 
     int allow_packed_rgb;
     uint8_t frame_requested;
@@ -84,8 +100,9 @@
     uint8_t overlay_rgba_map[4];
     uint8_t overlay_has_alpha;
     enum OverlayFormat { OVERLAY_FORMAT_YUV420, OVERLAY_FORMAT_YUV444, OVERLAY_FORMAT_RGB, OVERLAY_FORMAT_NB} format;
+    enum EvalMode { EVAL_MODE_INIT, EVAL_MODE_FRAME, EVAL_MODE_NB } eval_mode;
 
-    AVFilterBufferRef *overpicref;
+    AVFrame *overpicref;
     struct FFBufQueue queue_main;
     struct FFBufQueue queue_over;
 
@@ -93,41 +110,16 @@
     int overlay_pix_step[4];    ///< steps per pixel for each plane of the overlay
     int hsub, vsub;             ///< chroma subsampling values
     int shortest;               ///< terminate stream when the shortest input terminates
+    int repeatlast;             ///< repeat last overlay frame
 
+    double var_values[VAR_VARS_NB];
     char *x_expr, *y_expr;
+    AVExpr *x_pexpr, *y_pexpr;
 } OverlayContext;
 
-#define OFFSET(x) offsetof(OverlayContext, x)
-#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
-
-static const AVOption overlay_options[] = {
-    { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
-    { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
-    { "rgb", "force packed RGB in input and output (deprecated)", OFFSET(allow_packed_rgb), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
-    { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
-
-    { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, "format" },
-    { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
-    { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
-    { "rgb",    "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB},    .flags = FLAGS, .unit = "format" },
-
-    { NULL }
-};
-
-AVFILTER_DEFINE_CLASS(overlay);
-
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     OverlayContext *over = ctx->priv;
-    static const char *shorthand[] = { "x", "y", NULL };
-    int ret;
-
-    over->class = &overlay_class;
-    av_opt_set_defaults(over);
-
-    ret = av_opt_set_from_string(over, args, shorthand, "=", ":");
-    if (ret < 0)
-        return ret;
 
     if (over->allow_packed_rgb) {
         av_log(ctx, AV_LOG_WARNING,
@@ -141,11 +133,76 @@
 {
     OverlayContext *over = ctx->priv;
 
-    av_opt_free(over);
-
-    avfilter_unref_bufferp(&over->overpicref);
+    av_frame_free(&over->overpicref);
     ff_bufqueue_discard_all(&over->queue_main);
     ff_bufqueue_discard_all(&over->queue_over);
+    av_expr_free(over->x_pexpr); over->x_pexpr = NULL;
+    av_expr_free(over->y_pexpr); over->y_pexpr = NULL;
+}
+
+static inline int normalize_xy(double d, int chroma_sub)
+{
+    if (isnan(d))
+        return INT_MAX;
+    return (int)d & ~((1 << chroma_sub) - 1);
+}
+
+static void eval_expr(AVFilterContext *ctx)
+{
+    OverlayContext  *over = ctx->priv;
+
+    /* TODO: reindent */
+        over->var_values[VAR_X] = av_expr_eval(over->x_pexpr, over->var_values, NULL);
+        over->var_values[VAR_Y] = av_expr_eval(over->y_pexpr, over->var_values, NULL);
+        over->var_values[VAR_X] = av_expr_eval(over->x_pexpr, over->var_values, NULL);
+        over->x = normalize_xy(over->var_values[VAR_X], over->hsub);
+        over->y = normalize_xy(over->var_values[VAR_Y], over->vsub);
+}
+
+static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
+{
+    int ret;
+    AVExpr *old = NULL;
+
+    if (*pexpr)
+        old = *pexpr;
+    ret = av_expr_parse(pexpr, expr, var_names,
+                        NULL, NULL, NULL, NULL, 0, log_ctx);
+    if (ret < 0) {
+        av_log(log_ctx, AV_LOG_ERROR,
+               "Error when evaluating the expression '%s' for %s\n",
+               expr, option);
+        *pexpr = old;
+        return ret;
+    }
+
+    av_expr_free(old);
+    return 0;
+}
+
+static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
+                           char *res, int res_len, int flags)
+{
+    OverlayContext *over = ctx->priv;
+    int ret;
+
+    if      (!strcmp(cmd, "x"))
+        ret = set_expr(&over->x_pexpr, args, cmd, ctx);
+    else if (!strcmp(cmd, "y"))
+        ret = set_expr(&over->y_pexpr, args, cmd, ctx);
+    else
+        ret = AVERROR(ENOSYS);
+
+    if (ret < 0)
+        return ret;
+
+    if (over->eval_mode == EVAL_MODE_INIT) {
+        eval_expr(ctx);
+        av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
+               over->var_values[VAR_X], over->x,
+               over->var_values[VAR_Y], over->y);
+    }
+    return ret;
 }
 
 static int query_formats(AVFilterContext *ctx)
@@ -232,8 +289,6 @@
 {
     AVFilterContext *ctx  = inlink->dst;
     OverlayContext  *over = inlink->dst->priv;
-    char *expr;
-    double var_values[VAR_VARS_NB], res;
     int ret;
     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
 
@@ -241,54 +296,40 @@
 
     /* Finish the configuration by evaluating the expressions
        now when both inputs are configured. */
-    var_values[VAR_MAIN_W   ] = var_values[VAR_MW] = ctx->inputs[MAIN   ]->w;
-    var_values[VAR_MAIN_H   ] = var_values[VAR_MH] = ctx->inputs[MAIN   ]->h;
-    var_values[VAR_OVERLAY_W] = var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
-    var_values[VAR_OVERLAY_H] = var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
+    over->var_values[VAR_MAIN_W   ] = over->var_values[VAR_MW] = ctx->inputs[MAIN   ]->w;
+    over->var_values[VAR_MAIN_H   ] = over->var_values[VAR_MH] = ctx->inputs[MAIN   ]->h;
+    over->var_values[VAR_OVERLAY_W] = over->var_values[VAR_OW] = ctx->inputs[OVERLAY]->w;
+    over->var_values[VAR_OVERLAY_H] = over->var_values[VAR_OH] = ctx->inputs[OVERLAY]->h;
+    over->var_values[VAR_HSUB]  = 1<<pix_desc->log2_chroma_w;
+    over->var_values[VAR_VSUB]  = 1<<pix_desc->log2_chroma_h;
+    over->var_values[VAR_X]     = NAN;
+    over->var_values[VAR_Y]     = NAN;
+    over->var_values[VAR_N]     = 0;
+    over->var_values[VAR_T]     = NAN;
+    over->var_values[VAR_POS]   = NAN;
 
-    if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
-                                      NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
-        goto fail;
-    over->x = res;
-    if ((ret = av_expr_parse_and_eval(&res, (expr = over->y_expr), var_names, var_values,
-                                      NULL, NULL, NULL, NULL, NULL, 0, ctx)))
-        goto fail;
-    over->y = res;
-    /* x may depend on y */
-    if ((ret = av_expr_parse_and_eval(&res, (expr = over->x_expr), var_names, var_values,
-                                      NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
-        goto fail;
-    over->x = res;
+    if ((ret = set_expr(&over->x_pexpr,      over->x_expr,      "x",      ctx)) < 0 ||
+        (ret = set_expr(&over->y_pexpr,      over->y_expr,      "y",      ctx)) < 0)
+        return ret;
 
     over->overlay_is_packed_rgb =
         ff_fill_rgba_map(over->overlay_rgba_map, inlink->format) >= 0;
     over->overlay_has_alpha = ff_fmt_is_in(inlink->format, alpha_pix_fmts);
 
+    if (over->eval_mode == EVAL_MODE_INIT) {
+        eval_expr(ctx);
+        av_log(ctx, AV_LOG_VERBOSE, "x:%f xi:%d y:%f yi:%d\n",
+               over->var_values[VAR_X], over->x,
+               over->var_values[VAR_Y], over->y);
+    }
+
     av_log(ctx, AV_LOG_VERBOSE,
-           "main w:%d h:%d fmt:%s overlay x:%d y:%d w:%d h:%d fmt:%s\n",
+           "main w:%d h:%d fmt:%s overlay w:%d h:%d fmt:%s\n",
            ctx->inputs[MAIN]->w, ctx->inputs[MAIN]->h,
            av_get_pix_fmt_name(ctx->inputs[MAIN]->format),
-           over->x, over->y,
            ctx->inputs[OVERLAY]->w, ctx->inputs[OVERLAY]->h,
            av_get_pix_fmt_name(ctx->inputs[OVERLAY]->format));
-
-    if (over->x < 0 || over->y < 0 ||
-        over->x + var_values[VAR_OVERLAY_W] > var_values[VAR_MAIN_W] ||
-        over->y + var_values[VAR_OVERLAY_H] > var_values[VAR_MAIN_H]) {
-        av_log(ctx, AV_LOG_ERROR,
-               "Overlay area (%d,%d)<->(%d,%d) not within the main area (0,0)<->(%d,%d) or zero-sized\n",
-               over->x, over->y,
-               (int)(over->x + var_values[VAR_OVERLAY_W]),
-               (int)(over->y + var_values[VAR_OVERLAY_H]),
-               (int)var_values[VAR_MAIN_W], (int)var_values[VAR_MAIN_H]);
-        return AVERROR(EINVAL);
-    }
     return 0;
-
-fail:
-    av_log(NULL, AV_LOG_ERROR,
-           "Error when evaluating the expression '%s'\n", expr);
-    return ret;
 }
 
 static int config_output(AVFilterLink *outlink)
@@ -314,23 +355,23 @@
 
 /**
  * Blend image in src to destination buffer dst at position (x, y).
- *
- * It is assumed that the src image at position (x, y) is contained in
- * dst.
  */
 static void blend_image(AVFilterContext *ctx,
-                        AVFilterBufferRef *dst, AVFilterBufferRef *src,
+                        AVFrame *dst, AVFrame *src,
                         int x, int y)
 {
     OverlayContext *over = ctx->priv;
-    int i, j, k;
-    int width   = src->video->w;
-    int height  = src->video->h;
+    int i, imax, j, jmax, k, kmax;
+    const int src_w = src->width;
+    const int src_h = src->height;
+    const int dst_w = dst->width;
+    const int dst_h = dst->height;
+
+    if (x >= dst_w || x+dst_w  < 0 ||
+        y >= dst_h || y+dst_h < 0)
+        return; /* no intersection */
 
     if (over->main_is_packed_rgb) {
-        uint8_t *dp = dst->data[0] + x * over->main_pix_step[0] +
-                      y * dst->linesize[0];
-        uint8_t *sp = src->data[0];
         uint8_t alpha;          ///< the amount of overlay to blend on to main
         const int dr = over->main_rgba_map[R];
         const int dg = over->main_rgba_map[G];
@@ -343,9 +384,18 @@
         const int sa = over->overlay_rgba_map[A];
         const int sstep = over->overlay_pix_step[0];
         const int main_has_alpha = over->main_has_alpha;
-        for (i = 0; i < height; i++) {
-            uint8_t *d = dp, *s = sp;
-            for (j = 0; j < width; j++) {
+        uint8_t *s, *sp, *d, *dp;
+
+        i = FFMAX(-y, 0);
+        sp = src->data[0] + i     * src->linesize[0];
+        dp = dst->data[0] + (y+i) * dst->linesize[0];
+
+        for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
+            j = FFMAX(-x, 0);
+            s = sp + j     * sstep;
+            d = dp + (x+j) * dstep;
+
+            for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
                 alpha = s[sa];
 
                 // if the main channel has an alpha channel, alpha has to be calculated
@@ -391,13 +441,19 @@
     } else {
         const int main_has_alpha = over->main_has_alpha;
         if (main_has_alpha) {
-            uint8_t *da = dst->data[3] + x * over->main_pix_step[3] +
-                          y * dst->linesize[3];
-            uint8_t *sa = src->data[3];
             uint8_t alpha;          ///< the amount of overlay to blend on to main
-            for (i = 0; i < height; i++) {
-                uint8_t *d = da, *s = sa;
-                for (j = 0; j < width; j++) {
+            uint8_t *s, *sa, *d, *da;
+
+            i = FFMAX(-y, 0);
+            sa = src->data[3] + i     * src->linesize[3];
+            da = dst->data[3] + (y+i) * dst->linesize[3];
+
+            for (imax = FFMIN(-y + dst_h, src_h); i < imax; i++) {
+                j = FFMAX(-x, 0);
+                s = sa + j;
+                d = da + x+j;
+
+                for (jmax = FFMIN(-x + dst_w, src_w); j < jmax; j++) {
                     alpha = *s;
                     if (alpha != 0 && alpha != 255) {
                         uint8_t alpha_d = *d;
@@ -423,24 +479,36 @@
         for (i = 0; i < 3; i++) {
             int hsub = i ? over->hsub : 0;
             int vsub = i ? over->vsub : 0;
-            uint8_t *dp = dst->data[i] + (x >> hsub) +
-                (y >> vsub) * dst->linesize[i];
-            uint8_t *sp = src->data[i];
-            uint8_t *ap = src->data[3];
-            int wp = FFALIGN(width, 1<<hsub) >> hsub;
-            int hp = FFALIGN(height, 1<<vsub) >> vsub;
-            for (j = 0; j < hp; j++) {
-                uint8_t *d = dp, *s = sp, *a = ap;
-                for (k = 0; k < wp; k++) {
-                    // average alpha for color components, improve quality
+            int src_wp = FFALIGN(src_w, 1<<hsub) >> hsub;
+            int src_hp = FFALIGN(src_h, 1<<vsub) >> vsub;
+            int dst_wp = FFALIGN(dst_w, 1<<hsub) >> hsub;
+            int dst_hp = FFALIGN(dst_h, 1<<vsub) >> vsub;
+            int yp = y>>vsub;
+            int xp = x>>hsub;
+            uint8_t *s, *sp, *d, *dp, *a, *ap;
+
+            j = FFMAX(-yp, 0);
+            sp = src->data[i] + j         * src->linesize[i];
+            dp = dst->data[i] + (yp+j)    * dst->linesize[i];
+            ap = src->data[3] + (j<<vsub) * src->linesize[3];
+
+            for (jmax = FFMIN(-yp + dst_hp, src_hp); j < jmax; j++) {
+                k = FFMAX(-xp, 0);
+                d = dp + xp+k;
+                s = sp + k;
+                a = ap + (k<<hsub);
+
+                for (kmax = FFMIN(-xp + dst_wp, src_wp); k < kmax; k++) {
                     int alpha_v, alpha_h, alpha;
-                    if (hsub && vsub && j+1 < hp && k+1 < wp) {
+
+                    // average alpha for color components, improve quality
+                    if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
                         alpha = (a[0] + a[src->linesize[3]] +
                                  a[1] + a[src->linesize[3]+1]) >> 2;
                     } else if (hsub || vsub) {
-                        alpha_h = hsub && k+1 < wp ?
+                        alpha_h = hsub && k+1 < src_wp ?
                             (a[0] + a[1]) >> 1 : a[0];
-                        alpha_v = vsub && j+1 < hp ?
+                        alpha_v = vsub && j+1 < src_hp ?
                             (a[0] + a[src->linesize[3]]) >> 1 : a[0];
                         alpha = (alpha_v + alpha_h) >> 1;
                     } else
@@ -450,13 +518,13 @@
                     if (main_has_alpha && alpha != 0 && alpha != 255) {
                         // average alpha for color components, improve quality
                         uint8_t alpha_d;
-                        if (hsub && vsub && j+1 < hp && k+1 < wp) {
+                        if (hsub && vsub && j+1 < src_hp && k+1 < src_wp) {
                             alpha_d = (d[0] + d[src->linesize[3]] +
                                        d[1] + d[src->linesize[3]+1]) >> 2;
                         } else if (hsub || vsub) {
-                            alpha_h = hsub && k+1 < wp ?
+                            alpha_h = hsub && k+1 < src_wp ?
                                 (d[0] + d[1]) >> 1 : d[0];
-                            alpha_v = vsub && j+1 < hp ?
+                            alpha_v = vsub && j+1 < src_hp ?
                                 (d[0] + d[src->linesize[3]]) >> 1 : d[0];
                             alpha_d = (alpha_v + alpha_h) >> 1;
                         } else
@@ -476,22 +544,26 @@
     }
 }
 
-static int try_filter_frame(AVFilterContext *ctx, AVFilterBufferRef *mainpic)
+static int try_filter_frame(AVFilterContext *ctx, AVFrame *mainpic)
 {
     OverlayContext *over = ctx->priv;
-    AVFilterLink *outlink = ctx->outputs[0];
-    AVFilterBufferRef *next_overpic;
+    AVFilterLink *inlink = ctx->inputs[0];
+    AVFrame *next_overpic;
     int ret;
 
     /* Discard obsolete overlay frames: if there is a next overlay frame with pts
      * before the main frame, we can drop the current overlay. */
     while (1) {
         next_overpic = ff_bufqueue_peek(&over->queue_over, 0);
+        if (!next_overpic && over->overlay_eof && !over->repeatlast) {
+            av_frame_free(&over->overpicref);
+            break;
+        }
         if (!next_overpic || av_compare_ts(next_overpic->pts, ctx->inputs[OVERLAY]->time_base,
                                            mainpic->pts     , ctx->inputs[MAIN]->time_base) > 0)
             break;
         ff_bufqueue_get(&over->queue_over);
-        avfilter_unref_buffer(over->overpicref);
+        av_frame_free(&over->overpicref);
         over->overpicref = next_overpic;
     }
 
@@ -505,14 +577,31 @@
     /* At this point, we know that the current overlay frame extends to the
      * time of the main frame. */
     av_dlog(ctx, "main_pts:%s main_pts_time:%s",
-            av_ts2str(mainpic->pts), av_ts2timestr(mainpic->pts, &outlink->time_base));
+            av_ts2str(mainpic->pts), av_ts2timestr(mainpic->pts, &ctx->inputs[MAIN]->time_base));
     if (over->overpicref)
         av_dlog(ctx, " over_pts:%s over_pts_time:%s",
-                av_ts2str(over->overpicref->pts), av_ts2timestr(over->overpicref->pts, &outlink->time_base));
+                av_ts2str(over->overpicref->pts), av_ts2timestr(over->overpicref->pts, &ctx->inputs[OVERLAY]->time_base));
     av_dlog(ctx, "\n");
 
-    if (over->overpicref)
-        blend_image(ctx, mainpic, over->overpicref, over->x, over->y);
+    if (over->overpicref) {
+        if (over->eval_mode == EVAL_MODE_FRAME) {
+            int64_t pos = av_frame_get_pkt_pos(mainpic);
+
+            over->var_values[VAR_N] = inlink->frame_count;
+            over->var_values[VAR_T] = mainpic->pts == AV_NOPTS_VALUE ?
+                NAN : mainpic->pts * av_q2d(inlink->time_base);
+            over->var_values[VAR_POS] = pos == -1 ? NAN : pos;
+
+            eval_expr(ctx);
+            av_log(ctx, AV_LOG_DEBUG, "n:%f t:%f pos:%f x:%f xi:%d y:%f yi:%d\n",
+                   over->var_values[VAR_N], over->var_values[VAR_T], over->var_values[VAR_POS],
+                   over->var_values[VAR_X], over->x,
+                   over->var_values[VAR_Y], over->y);
+        }
+        if (over->enable)
+            blend_image(ctx, mainpic, over->overpicref, over->x, over->y);
+
+    }
     ret = ff_filter_frame(ctx->outputs[0], mainpic);
     av_assert1(ret != AVERROR(EAGAIN));
     over->frame_requested = 0;
@@ -522,7 +611,7 @@
 static int try_filter_next_frame(AVFilterContext *ctx)
 {
     OverlayContext *over = ctx->priv;
-    AVFilterBufferRef *next_mainpic = ff_bufqueue_peek(&over->queue_main, 0);
+    AVFrame *next_mainpic = ff_bufqueue_peek(&over->queue_main, 0);
     int ret;
 
     if (!next_mainpic)
@@ -541,7 +630,7 @@
     return ret == AVERROR(EAGAIN) ? 0 : ret;
 }
 
-static int filter_frame_main(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
+static int filter_frame_main(AVFilterLink *inlink, AVFrame *inpicref)
 {
     AVFilterContext *ctx = inlink->dst;
     OverlayContext *over = ctx->priv;
@@ -562,7 +651,7 @@
     return 0;
 }
 
-static int filter_frame_over(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
+static int filter_frame_over(AVFilterLink *inlink, AVFrame *inpicref)
 {
     AVFilterContext *ctx = inlink->dst;
     OverlayContext *over = ctx->priv;
@@ -575,6 +664,20 @@
     return ret == AVERROR(EAGAIN) ? 0 : ret;
 }
 
+#define DEF_FILTER_FRAME(name, mode, enable_value)                              \
+static int filter_frame_##name##_##mode(AVFilterLink *inlink, AVFrame *frame)   \
+{                                                                               \
+    AVFilterContext *ctx = inlink->dst;                                         \
+    OverlayContext *over = ctx->priv;                                           \
+    over->enable = enable_value;                                                \
+    return filter_frame_##name(inlink, frame);                                  \
+}
+
+DEF_FILTER_FRAME(main, enabled,  1);
+DEF_FILTER_FRAME(main, disabled, 0);
+DEF_FILTER_FRAME(over, enabled,  1);
+DEF_FILTER_FRAME(over, disabled, 0);
+
 static int request_frame(AVFilterLink *outlink)
 {
     AVFilterContext *ctx = outlink->src;
@@ -605,21 +708,43 @@
     return 0;
 }
 
+#define OFFSET(x) offsetof(OverlayContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption overlay_options[] = {
+    { "x", "set the x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "y", "set the y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_FRAME}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
+         { "init",  "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT},  .flags = FLAGS, .unit = "eval" },
+         { "frame", "eval expressions per-frame",                  0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
+    { "rgb", "force packed RGB in input and output (deprecated)", OFFSET(allow_packed_rgb), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
+    { "shortest", "force termination when the shortest input terminates", OFFSET(shortest), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
+    { "format", "set output format", OFFSET(format), AV_OPT_TYPE_INT, {.i64=OVERLAY_FORMAT_YUV420}, 0, OVERLAY_FORMAT_NB-1, FLAGS, "format" },
+        { "yuv420", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV420}, .flags = FLAGS, .unit = "format" },
+        { "yuv444", "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_YUV444}, .flags = FLAGS, .unit = "format" },
+        { "rgb",    "", 0, AV_OPT_TYPE_CONST, {.i64=OVERLAY_FORMAT_RGB},    .flags = FLAGS, .unit = "format" },
+    { "repeatlast", "repeat overlay of the last overlay frame", OFFSET(repeatlast), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(overlay);
+
 static const AVFilterPad avfilter_vf_overlay_inputs[] = {
     {
         .name         = "main",
         .type         = AVMEDIA_TYPE_VIDEO,
         .get_video_buffer = ff_null_get_video_buffer,
         .config_props = config_input_main,
-        .filter_frame = filter_frame_main,
-        .min_perms    = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE,
+        .filter_frame             = filter_frame_main_enabled,
+        .passthrough_filter_frame = filter_frame_main_disabled,
+        .needs_writable = 1,
     },
     {
         .name         = "overlay",
         .type         = AVMEDIA_TYPE_VIDEO,
         .config_props = config_input_overlay,
-        .filter_frame = filter_frame_over,
-        .min_perms    = AV_PERM_READ | AV_PERM_PRESERVE,
+        .filter_frame             = filter_frame_over_enabled,
+        .passthrough_filter_frame = filter_frame_over_disabled,
     },
     { NULL }
 };
@@ -628,7 +753,6 @@
     {
         .name          = "default",
         .type          = AVMEDIA_TYPE_VIDEO,
-        .rej_perms     = AV_PERM_WRITE,
         .config_props  = config_output,
         .request_frame = request_frame,
     },
@@ -643,10 +767,12 @@
     .uninit    = uninit,
 
     .priv_size = sizeof(OverlayContext),
+    .priv_class = &overlay_class,
 
     .query_formats = query_formats,
+    .process_command = process_command,
 
     .inputs    = avfilter_vf_overlay_inputs,
     .outputs   = avfilter_vf_overlay_outputs,
-    .priv_class = &overlay_class,
+    .flags     = AVFILTER_FLAG_SUPPORT_TIMELINE,
 };
diff --git a/libavfilter/vf_pad.c b/libavfilter/vf_pad.c
index 5c146f2..3407679 100644
--- a/libavfilter/vf_pad.c
+++ b/libavfilter/vf_pad.c
@@ -38,6 +38,8 @@
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/mathematics.h"
+#include "libavutil/opt.h"
+
 #include "drawutils.h"
 
 static const char *const var_names[] = {
@@ -82,43 +84,19 @@
     int x, y;               ///< offsets of the input area with respect to the padded area
     int in_w, in_h;         ///< width and height for the padded input video, which has to be aligned to the chroma values in order to avoid chroma issues
 
-    char *w_expr;       ///< width  expression string
-    char *h_expr;       ///< height expression string
-    char *x_expr;       ///< width  expression string
-    char *y_expr;       ///< height expression string
+    char *w_expr;           ///< width  expression string
+    char *h_expr;           ///< height expression string
+    char *x_expr;           ///< width  expression string
+    char *y_expr;           ///< height expression string
     char *color_str;
     uint8_t rgba_color[4];  ///< color for the padding area
     FFDrawContext draw;
     FFDrawColor color;
 } PadContext;
 
-#define OFFSET(x) offsetof(PadContext, x)
-#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
-
-static const AVOption pad_options[] = {
-    { "width",  "set the pad area width expression",       OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
-    { "w",      "set the pad area width expression",       OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
-    { "height", "set the pad area height expression",      OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
-    { "h",      "set the pad area height expression",      OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
-    { "x",      "set the x offset expression for the input image position", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
-    { "y",      "set the y offset expression for the input image position", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
-    { "color",  "set the color of the padded area border", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = "black"}, .flags = FLAGS },
-    {NULL}
-};
-
-AVFILTER_DEFINE_CLASS(pad);
-
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     PadContext *pad = ctx->priv;
-    static const char *shorthand[] = { "width", "height", "x", "y", "color", NULL };
-    int ret;
-
-    pad->class = &pad_class;
-    av_opt_set_defaults(pad);
-
-    if ((ret = av_opt_set_from_string(pad, args, shorthand, "=", ":")) < 0)
-        return ret;
 
     if (av_parse_color(pad->rgba_color, pad->color_str, -1, ctx) < 0)
         return AVERROR(EINVAL);
@@ -126,12 +104,6 @@
     return 0;
 }
 
-static av_cold void uninit(AVFilterContext *ctx)
-{
-    PadContext *pad = ctx->priv;
-    av_opt_free(pad);
-}
-
 static int config_input(AVFilterLink *inlink)
 {
     AVFilterContext *ctx = inlink->dst;
@@ -238,98 +210,126 @@
     return 0;
 }
 
-static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
+static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
 {
     PadContext *pad = inlink->dst->priv;
-    int align = (perms&AV_PERM_ALIGN) ? AVFILTER_ALIGN : 1;
 
-    AVFilterBufferRef *picref = ff_get_video_buffer(inlink->dst->outputs[0], perms,
-                                                    w + (pad->w - pad->in_w) + 4*align,
-                                                    h + (pad->h - pad->in_h));
+    AVFrame *frame = ff_get_video_buffer(inlink->dst->outputs[0],
+                                         w + (pad->w - pad->in_w),
+                                         h + (pad->h - pad->in_h));
     int plane;
 
-    if (!picref)
+    if (!frame)
         return NULL;
 
-    picref->video->w = w;
-    picref->video->h = h;
+    frame->width  = w;
+    frame->height = h;
 
-    for (plane = 0; plane < 4 && picref->data[plane]; plane++)
-        picref->data[plane] += FFALIGN(pad->x >> pad->draw.hsub[plane], align) * pad->draw.pixelstep[plane] +
-                                      (pad->y >> pad->draw.vsub[plane])        * picref->linesize[plane];
+    for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
+        int hsub = pad->draw.hsub[plane];
+        int vsub = pad->draw.vsub[plane];
+        frame->data[plane] += (pad->x >> hsub) * pad->draw.pixelstep[plane] +
+                              (pad->y >> vsub) * frame->linesize[plane];
+    }
 
-    return picref;
+    return frame;
 }
 
-static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, int hsub, int vsub, int x, int y)
+/* check whether each plane in this buffer can be padded without copying */
+static int buffer_needs_copy(PadContext *s, AVFrame *frame, AVBufferRef *buf)
 {
-    int64_t x_in_buf, y_in_buf;
+    int planes[4] = { -1, -1, -1, -1}, *p = planes;
+    int i, j;
 
-    x_in_buf =  outpicref->data[plane] - outpicref->buf->data[plane]
-             +  (x >> hsub) * pad->draw.pixelstep[plane]
-             +  (y >> vsub) * outpicref->linesize[plane];
+    /* get all planes in this buffer */
+    for (i = 0; i < FF_ARRAY_ELEMS(planes) && frame->data[i]; i++) {
+        if (av_frame_get_plane_buffer(frame, i) == buf)
+            *p++ = i;
+    }
 
-    if(x_in_buf < 0 || x_in_buf % pad->draw.pixelstep[plane])
-        return 1;
-    x_in_buf /= pad->draw.pixelstep[plane];
+    /* for each plane in this buffer, check that it can be padded without
+     * going over buffer bounds or other planes */
+    for (i = 0; i < FF_ARRAY_ELEMS(planes) && planes[i] >= 0; i++) {
+        int hsub = s->draw.hsub[planes[i]];
+        int vsub = s->draw.vsub[planes[i]];
 
-    av_assert0(outpicref->buf->linesize[plane]>0); //while reference can use negative linesize the main buffer should not
+        uint8_t *start = frame->data[planes[i]];
+        uint8_t *end   = start + (frame->height >> vsub) *
+                                 frame->linesize[planes[i]];
 
-    y_in_buf = x_in_buf / outpicref->buf->linesize[plane];
-    x_in_buf %= outpicref->buf->linesize[plane];
+        /* amount of free space needed before the start and after the end
+         * of the plane */
+        ptrdiff_t req_start = (s->x >> hsub) * s->draw.pixelstep[planes[i]] +
+                              (s->y >> vsub) * frame->linesize[planes[i]];
+        ptrdiff_t req_end   = ((s->w - s->x - frame->width) >> hsub) *
+                              s->draw.pixelstep[planes[i]] +
+                              (s->y >> vsub) * frame->linesize[planes[i]];
 
-    if(   y_in_buf<<vsub >= outpicref->buf->h
-       || x_in_buf<<hsub >= outpicref->buf->w)
-        return 1;
+        if (frame->linesize[planes[i]] < (s->w >> hsub) * s->draw.pixelstep[planes[i]])
+            return 1;
+        if (start - buf->data < req_start ||
+            (buf->data + buf->size) - end < req_end)
+            return 1;
+
+#define SIGN(x) ((x) > 0 ? 1 : -1)
+        for (j = 0; j < FF_ARRAY_ELEMS(planes) && planes[j] >= 0; j++) {
+            int vsub1 = s->draw.vsub[planes[j]];
+            uint8_t *start1 = frame->data[planes[j]];
+            uint8_t *end1   = start1 + (frame->height >> vsub1) *
+                                       frame->linesize[planes[j]];
+            if (i == j)
+                continue;
+
+            if (SIGN(start - end1) != SIGN(start - end1 - req_start) ||
+                SIGN(end - start1) != SIGN(end - start1 + req_end))
+                return 1;
+        }
+    }
+
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
+static int frame_needs_copy(PadContext *s, AVFrame *frame)
+{
+    int i;
+
+    if (!av_frame_is_writable(frame))
+        return 1;
+
+    for (i = 0; i < 4 && frame->buf[i]; i++)
+        if (buffer_needs_copy(s, frame, frame->buf[i]))
+            return 1;
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     PadContext *pad = inlink->dst->priv;
-    AVFilterBufferRef *out = avfilter_ref_buffer(in, ~0);
-    int plane, needs_copy;
+    AVFrame *out;
+    int needs_copy = frame_needs_copy(pad, in);
 
-    if (!out) {
-        avfilter_unref_bufferp(&in);
-        return AVERROR(ENOMEM);
-    }
-
-    for (plane = 0; plane < 4 && out->data[plane] && pad->draw.pixelstep[plane]; plane++) {
-        int hsub = pad->draw.hsub[plane];
-        int vsub = pad->draw.vsub[plane];
-
-        av_assert0(out->buf->w > 0 && out->buf->h > 0);
-
-        if (out->format != out->buf->format) //unsupported currently
-            break;
-
-        out->data[plane] -= (pad->x  >> hsub) * pad->draw.pixelstep[plane] +
-                            (pad->y  >> vsub) * out->linesize[plane];
-
-        if (does_clip(pad, out, plane, hsub, vsub, 0,                   0) ||
-            does_clip(pad, out, plane, hsub, vsub, 0,          pad->h - 1) ||
-            does_clip(pad, out, plane, hsub, vsub, pad->w - 1,          0) ||
-            does_clip(pad, out, plane, hsub, vsub, pad->w - 1, pad->h - 1))
-            break;
-    }
-    needs_copy = plane < 4 && out->data[plane] || !(out->perms & AV_PERM_WRITE);
     if (needs_copy) {
         av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
-        avfilter_unref_buffer(out);
-        out = ff_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
+        out = ff_get_video_buffer(inlink->dst->outputs[0],
                                   FFMAX(inlink->w, pad->w),
                                   FFMAX(inlink->h, pad->h));
         if (!out) {
-            avfilter_unref_bufferp(&in);
+            av_frame_free(&in);
             return AVERROR(ENOMEM);
         }
 
-        avfilter_copy_buffer_ref_props(out, in);
-    }
+        av_frame_copy_props(out, in);
+    } else {
+        int i;
 
-    out->video->w = pad->w;
-    out->video->h = pad->h;
+        out = in;
+        for (i = 0; i < 4 && out->data[i]; i++) {
+            int hsub = pad->draw.hsub[i];
+            int vsub = pad->draw.vsub[i];
+            out->data[i] -= (pad->x >> hsub) * pad->draw.pixelstep[i] +
+                            (pad->y >> vsub) * out->linesize[i];
+        }
+    }
 
     /* top bar */
     if (pad->y) {
@@ -347,23 +347,43 @@
 
     /* left border */
     ff_fill_rectangle(&pad->draw, &pad->color, out->data, out->linesize,
-                      0, pad->y, pad->x, in->video->h);
+                      0, pad->y, pad->x, in->height);
 
     if (needs_copy) {
         ff_copy_rectangle2(&pad->draw,
                           out->data, out->linesize, in->data, in->linesize,
-                          pad->x, pad->y, 0, 0, in->video->w, in->video->h);
+                          pad->x, pad->y, 0, 0, in->width, in->height);
     }
 
     /* right border */
     ff_fill_rectangle(&pad->draw, &pad->color, out->data, out->linesize,
                       pad->x + pad->in_w, pad->y, pad->w - pad->x - pad->in_w,
-                      in->video->h);
+                      in->height);
 
-    avfilter_unref_bufferp(&in);
+    out->width  = pad->w;
+    out->height = pad->h;
+
+    if (in != out)
+        av_frame_free(&in);
     return ff_filter_frame(inlink->dst->outputs[0], out);
 }
 
+#define OFFSET(x) offsetof(PadContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption pad_options[] = {
+    { "width",  "set the pad area width expression",       OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "w",      "set the pad area width expression",       OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "height", "set the pad area height expression",      OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "h",      "set the pad area height expression",      OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "x",      "set the x offset expression for the input image position", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "y",      "set the y offset expression for the input image position", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str = "0"}, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "color",  "set the color of the padded area border", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = "black"}, .flags = FLAGS },
+    { NULL },
+};
+
+AVFILTER_DEFINE_CLASS(pad);
+
 static const AVFilterPad avfilter_vf_pad_inputs[] = {
     {
         .name             = "default",
@@ -389,12 +409,11 @@
     .description   = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
 
     .priv_size     = sizeof(PadContext),
+    .priv_class    = &pad_class,
     .init          = init,
-    .uninit        = uninit,
     .query_formats = query_formats,
 
     .inputs    = avfilter_vf_pad_inputs,
 
     .outputs   = avfilter_vf_pad_outputs,
-    .priv_class = &pad_class,
 };
diff --git a/libavfilter/vf_pixdesctest.c b/libavfilter/vf_pixdesctest.c
index 6ac03d9..42afc63 100644
--- a/libavfilter/vf_pixdesctest.c
+++ b/libavfilter/vf_pixdesctest.c
@@ -52,21 +52,20 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     PixdescTestContext *priv = inlink->dst->priv;
     AVFilterLink *outlink    = inlink->dst->outputs[0];
-    AVFilterBufferRef *out;
+    AVFrame *out;
     int i, c, w = inlink->w, h = inlink->h;
 
-    out = ff_get_video_buffer(outlink, AV_PERM_WRITE,
-                              outlink->w, outlink->h);
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!out) {
-        avfilter_unref_bufferp(&in);
+        av_frame_free(&in);
         return AVERROR(ENOMEM);
     }
 
-    avfilter_copy_buffer_ref_props(out, in);
+    av_frame_copy_props(out, in);
 
     for (i = 0; i < 4; i++) {
         int h = outlink->h;
@@ -102,7 +101,7 @@
         }
     }
 
-    avfilter_unref_bufferp(&in);
+    av_frame_free(&in);
     return ff_filter_frame(outlink, out);
 }
 
@@ -112,7 +111,6 @@
         .type         = AVMEDIA_TYPE_VIDEO,
         .filter_frame = filter_frame,
         .config_props = config_props,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL }
 };
diff --git a/libavfilter/vf_pp.c b/libavfilter/vf_pp.c
index bde5ebf..0571cfc 100644
--- a/libavfilter/vf_pp.c
+++ b/libavfilter/vf_pp.c
@@ -31,21 +31,29 @@
 #include "libpostproc/postprocess.h"
 
 typedef struct {
+    const AVClass *class;
+    char *subfilters;
     int mode_id;
     pp_mode *modes[PP_QUALITY_MAX + 1];
     void *pp_ctx;
 } PPFilterContext;
 
-static av_cold int pp_init(AVFilterContext *ctx, const char *args)
+#define OFFSET(x) offsetof(PPFilterContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption pp_options[] = {
+    { "subfilters", "set postprocess subfilters", OFFSET(subfilters), AV_OPT_TYPE_STRING, {.str="de"}, .flags = FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(pp);
+
+static av_cold int pp_init(AVFilterContext *ctx)
 {
     int i;
     PPFilterContext *pp = ctx->priv;
 
-    if (!args || !*args)
-        args = "de";
-
     for (i = 0; i <= PP_QUALITY_MAX; i++) {
-        pp->modes[i] = pp_get_mode_by_name_and_quality(args, i);
+        pp->modes[i] = pp_get_mode_by_name_and_quality(pp->subfilters, i);
         if (!pp->modes[i])
             return AVERROR_EXTERNAL;
     }
@@ -100,32 +108,35 @@
     return 0;
 }
 
-static int pp_filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inbuf)
+static int pp_filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
 {
     AVFilterContext *ctx = inlink->dst;
     PPFilterContext *pp = ctx->priv;
     AVFilterLink *outlink = ctx->outputs[0];
     const int aligned_w = FFALIGN(outlink->w, 8);
     const int aligned_h = FFALIGN(outlink->h, 8);
-    AVFilterBufferRef *outbuf;
+    AVFrame *outbuf;
+    int qstride, qp_type;
+    int8_t *qp_table ;
 
-    outbuf = ff_get_video_buffer(outlink, AV_PERM_WRITE, aligned_w, aligned_h);
+    outbuf = ff_get_video_buffer(outlink, aligned_w, aligned_h);
     if (!outbuf) {
-        avfilter_unref_buffer(inbuf);
+        av_frame_free(&inbuf);
         return AVERROR(ENOMEM);
     }
-    avfilter_copy_buffer_ref_props(outbuf, inbuf);
+    av_frame_copy_props(outbuf, inbuf);
+    qp_table = av_frame_get_qp_table(inbuf, &qstride, &qp_type);
 
     pp_postprocess((const uint8_t **)inbuf->data, inbuf->linesize,
                    outbuf->data,                 outbuf->linesize,
                    aligned_w, outlink->h,
-                   outbuf->video->qp_table,
-                   outbuf->video->qp_table_linesize,
+                   qp_table,
+                   qstride,
                    pp->modes[pp->mode_id],
                    pp->pp_ctx,
-                   outbuf->video->pict_type);
+                   outbuf->pict_type | (qp_type ? PP_PICT_TYPE_QP2 : 0));
 
-    avfilter_unref_buffer(inbuf);
+    av_frame_free(&inbuf);
     return ff_filter_frame(outlink, outbuf);
 }
 
@@ -146,7 +157,6 @@
         .type         = AVMEDIA_TYPE_VIDEO,
         .config_props = pp_config_props,
         .filter_frame = pp_filter_frame,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL }
 };
@@ -169,4 +179,6 @@
     .inputs          = pp_inputs,
     .outputs         = pp_outputs,
     .process_command = pp_process_command,
+    .priv_class      = &pp_class,
+    .flags           = AVFILTER_FLAG_SUPPORT_TIMELINE,
 };
diff --git a/libavfilter/vf_removelogo.c b/libavfilter/vf_removelogo.c
index ddaf9ef..dfc75d1 100644
--- a/libavfilter/vf_removelogo.c
+++ b/libavfilter/vf_removelogo.c
@@ -70,6 +70,7 @@
  */
 
 #include "libavutil/imgutils.h"
+#include "libavutil/opt.h"
 #include "avfilter.h"
 #include "formats.h"
 #include "internal.h"
@@ -79,6 +80,8 @@
 #include "lswsutils.h"
 
 typedef struct {
+    const AVClass *class;
+    char *filename;
     /* Stores our collection of masks. The first is for an array of
        the second for the y axis, and the third for the x axis. */
     int ***mask;
@@ -91,6 +94,16 @@
     FFBoundingBox half_mask_bbox;
 } RemovelogoContext;
 
+#define OFFSET(x) offsetof(RemovelogoContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption removelogo_options[] = {
+    { "filename", "set bitmap filename", OFFSET(filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
+    { "f",        "set bitmap filename", OFFSET(filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(removelogo);
+
 /**
  * Choose a slightly larger mask size to improve performance.
  *
@@ -264,7 +277,7 @@
                                   src_w/2, src_h/2, 0, max_mask_size);
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     RemovelogoContext *removelogo = ctx->priv;
     int ***mask;
@@ -272,13 +285,13 @@
     int a, b, c, w, h;
     int full_max_mask_size, half_max_mask_size;
 
-    if (!args) {
-        av_log(ctx, AV_LOG_ERROR, "An image file must be specified as argument\n");
+    if (!removelogo->filename) {
+        av_log(ctx, AV_LOG_ERROR, "The bitmap file name is mandatory\n");
         return AVERROR(EINVAL);
     }
 
     /* Load our mask image. */
-    if ((ret = load_mask(&removelogo->full_mask_data, &w, &h, args, ctx)) < 0)
+    if ((ret = load_mask(&removelogo->full_mask_data, &w, &h, removelogo->filename, ctx)) < 0)
         return ret;
     removelogo->mask_w = w;
     removelogo->mask_h = h;
@@ -473,23 +486,23 @@
     }
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
+static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
 {
     RemovelogoContext *removelogo = inlink->dst->priv;
     AVFilterLink *outlink = inlink->dst->outputs[0];
-    AVFilterBufferRef *outpicref;
+    AVFrame *outpicref;
     int direct = 0;
 
-    if (inpicref->perms & AV_PERM_WRITE) {
+    if (av_frame_is_writable(inpicref)) {
         direct = 1;
         outpicref = inpicref;
     } else {
-        outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+        outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
         if (!outpicref) {
-            avfilter_unref_bufferp(&inpicref);
+            av_frame_free(&inpicref);
             return AVERROR(ENOMEM);
         }
-        avfilter_copy_buffer_ref_props(outpicref, inpicref);
+        av_frame_copy_props(outpicref, inpicref);
     }
 
     blur_image(removelogo->mask,
@@ -509,7 +522,7 @@
                inlink->w/2, inlink->h/2, direct, &removelogo->half_mask_bbox);
 
     if (!direct)
-        avfilter_unref_bufferp(&inpicref);
+        av_frame_free(&inpicref);
 
     return ff_filter_frame(outlink, outpicref);
 }
@@ -543,7 +556,6 @@
         .get_video_buffer = ff_null_get_video_buffer,
         .config_props     = config_props_input,
         .filter_frame     = filter_frame,
-        .min_perms        = AV_PERM_READ,
     },
     { NULL }
 };
@@ -565,4 +577,6 @@
     .query_formats = query_formats,
     .inputs        = removelogo_inputs,
     .outputs       = removelogo_outputs,
+    .priv_class    = &removelogo_class,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE,
 };
diff --git a/libavfilter/vf_scale.c b/libavfilter/vf_scale.c
index f6e79ff..cce2cc2 100644
--- a/libavfilter/vf_scale.c
+++ b/libavfilter/vf_scale.c
@@ -78,7 +78,6 @@
      *  -1 = keep original aspect
      */
     int w, h;
-    char *flags_str;            ///sws flags string
     char *size_str;
     unsigned int flags;         ///sws flags
 
@@ -90,45 +89,13 @@
 
     char *w_expr;               ///< width  expression string
     char *h_expr;               ///< height expression string
+    char *flags_str;
 } ScaleContext;
 
-#define OFFSET(x) offsetof(ScaleContext, x)
-#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
-
-static const AVOption scale_options[] = {
-    { "w",      "set width expression",    OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
-    { "width",  "set width expression",    OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
-    { "h",      "set height expression",   OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
-    { "height", "set height expression",   OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
-    { "flags",  "set libswscale flags",    OFFSET(flags_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, INT_MAX, FLAGS },
-    { "interl", "set interlacing", OFFSET(interlaced), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, 1, FLAGS },
-    { "size",   "set video size",          OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
-    { "s",      "set video size",          OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
-    { NULL },
-};
-
-AVFILTER_DEFINE_CLASS(scale);
-
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     ScaleContext *scale = ctx->priv;
-    static const char *shorthand[] = { "w", "h", NULL };
     int ret;
-    const char *args0 = args;
-
-    scale->class = &scale_class;
-    av_opt_set_defaults(scale);
-
-    if (args && (scale->size_str = av_get_token(&args, ":"))) {
-        if (av_parse_video_size(&scale->w, &scale->h, scale->size_str) < 0) {
-            av_freep(&scale->size_str);
-            args = args0;
-        } else if (*args)
-            args++;
-    }
-
-    if ((ret = av_opt_set_from_string(scale, args, shorthand, "=", ":")) < 0)
-        return ret;
 
     if (scale->size_str && (scale->w_expr || scale->h_expr)) {
         av_log(ctx, AV_LOG_ERROR,
@@ -136,6 +103,9 @@
             return AVERROR(EINVAL);
     }
 
+    if (scale->w_expr && !scale->h_expr)
+        FFSWAP(char *, scale->w_expr, scale->size_str);
+
     if (scale->size_str) {
         char buf[32];
         if ((ret = av_parse_video_size(&scale->w, &scale->h, scale->size_str)) < 0) {
@@ -157,6 +127,7 @@
            scale->w_expr, scale->h_expr, (char *)av_x_if_null(scale->flags_str, ""), scale->interlaced);
 
     scale->flags = SWS_BILINEAR;
+
     if (scale->flags_str) {
         const AVClass *class = sws_get_class();
         const AVOption    *o = av_opt_find(&class, "sws_flags", NULL, 0,
@@ -329,7 +300,7 @@
     return ret;
 }
 
-static int scale_slice(AVFilterLink *link, AVFilterBufferRef *out_buf, AVFilterBufferRef *cur_pic, struct SwsContext *sws, int y, int h, int mul, int field)
+static int scale_slice(AVFilterLink *link, AVFrame *out_buf, AVFrame *cur_pic, struct SwsContext *sws, int y, int h, int mul, int field)
 {
     ScaleContext *scale = link->dst->priv;
     const uint8_t *in[4];
@@ -353,17 +324,17 @@
                          out,out_stride);
 }
 
-static int filter_frame(AVFilterLink *link, AVFilterBufferRef *in)
+static int filter_frame(AVFilterLink *link, AVFrame *in)
 {
     ScaleContext *scale = link->dst->priv;
     AVFilterLink *outlink = link->dst->outputs[0];
-    AVFilterBufferRef *out;
+    AVFrame *out;
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
     char buf[32];
 
-    if(   in->video->w != link->w
-       || in->video->h != link->h
-       || in->format   != link->format) {
+    if(   in->width  != link->w
+       || in->height != link->h
+       || in->format != link->format) {
         int ret;
         snprintf(buf, sizeof(buf)-1, "%d", outlink->w);
         av_opt_set(scale, "w", buf, 0);
@@ -371,8 +342,8 @@
         av_opt_set(scale, "h", buf, 0);
 
         link->dst->inputs[0]->format = in->format;
-        link->dst->inputs[0]->w      = in->video->w;
-        link->dst->inputs[0]->h      = in->video->h;
+        link->dst->inputs[0]->w      = in->width;
+        link->dst->inputs[0]->h      = in->height;
 
         if ((ret = config_props(outlink)) < 0)
             return ret;
@@ -384,41 +355,57 @@
     scale->hsub = desc->log2_chroma_w;
     scale->vsub = desc->log2_chroma_h;
 
-    out = ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN, outlink->w, outlink->h);
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!out) {
-        avfilter_unref_bufferp(&in);
+        av_frame_free(&in);
         return AVERROR(ENOMEM);
     }
 
-    avfilter_copy_buffer_ref_props(out, in);
-    out->video->w = outlink->w;
-    out->video->h = outlink->h;
+    av_frame_copy_props(out, in);
+    out->width  = outlink->w;
+    out->height = outlink->h;
 
     if(scale->output_is_pal)
         avpriv_set_systematic_pal2((uint32_t*)out->data[1], outlink->format == AV_PIX_FMT_PAL8 ? AV_PIX_FMT_BGR8 : outlink->format);
 
-    av_reduce(&out->video->sample_aspect_ratio.num, &out->video->sample_aspect_ratio.den,
-              (int64_t)in->video->sample_aspect_ratio.num * outlink->h * link->w,
-              (int64_t)in->video->sample_aspect_ratio.den * outlink->w * link->h,
+    av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
+              (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
+              (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
               INT_MAX);
 
-    if(scale->interlaced>0 || (scale->interlaced<0 && in->video->interlaced)){
+    if(scale->interlaced>0 || (scale->interlaced<0 && in->interlaced_frame)){
         scale_slice(link, out, in, scale->isws[0], 0, (link->h+1)/2, 2, 0);
         scale_slice(link, out, in, scale->isws[1], 0,  link->h   /2, 2, 1);
     }else{
         scale_slice(link, out, in, scale->sws, 0, link->h, 1, 0);
     }
 
-    avfilter_unref_bufferp(&in);
+    av_frame_free(&in);
     return ff_filter_frame(outlink, out);
 }
 
+#define OFFSET(x) offsetof(ScaleContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption scale_options[] = {
+    { "w",     "Output video width",          OFFSET(w_expr),    AV_OPT_TYPE_STRING,        .flags = FLAGS },
+    { "width", "Output video width",          OFFSET(w_expr),    AV_OPT_TYPE_STRING,        .flags = FLAGS },
+    { "h",     "Output video height",         OFFSET(h_expr),    AV_OPT_TYPE_STRING,        .flags = FLAGS },
+    { "height","Output video height",         OFFSET(h_expr),    AV_OPT_TYPE_STRING,        .flags = FLAGS },
+    { "flags", "Flags to pass to libswscale", OFFSET(flags_str), AV_OPT_TYPE_STRING, { .str = "bilinear" }, .flags = FLAGS },
+    { "interl", "set interlacing", OFFSET(interlaced), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, 1, FLAGS },
+    { "size",   "set video size",          OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
+    { "s",      "set video size",          OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
+    { NULL },
+};
+
+AVFILTER_DEFINE_CLASS(scale);
+
 static const AVFilterPad avfilter_vf_scale_inputs[] = {
     {
         .name        = "default",
         .type        = AVMEDIA_TYPE_VIDEO,
         .filter_frame = filter_frame,
-        .min_perms   = AV_PERM_READ,
     },
     { NULL }
 };
@@ -442,8 +429,8 @@
     .query_formats = query_formats,
 
     .priv_size = sizeof(ScaleContext),
+    .priv_class = &scale_class,
 
     .inputs    = avfilter_vf_scale_inputs,
     .outputs   = avfilter_vf_scale_outputs,
-    .priv_class = &scale_class,
 };
diff --git a/libavfilter/vf_separatefields.c b/libavfilter/vf_separatefields.c
new file mode 100644
index 0000000..c91c0c1
--- /dev/null
+++ b/libavfilter/vf_separatefields.c
@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 2013 Paul B Mahol
+ *
+ * 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 "libavutil/pixdesc.h"
+#include "avfilter.h"
+#include "internal.h"
+
+typedef struct {
+    int nb_planes;
+    double ts_unit;
+} SeparateFieldsContext;
+
+static int config_props_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    SeparateFieldsContext *sf = ctx->priv;
+    AVFilterLink *inlink = ctx->inputs[0];
+
+    sf->nb_planes = av_pix_fmt_count_planes(inlink->format);;
+
+    if (inlink->h & 1) {
+        av_log(ctx, AV_LOG_ERROR, "height must be even\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    outlink->time_base.num = inlink->time_base.num;
+    outlink->time_base.den = inlink->time_base.den * 2;
+    outlink->frame_rate.num = inlink->frame_rate.num * 2;
+    outlink->frame_rate.den = inlink->frame_rate.den;
+    outlink->w = inlink->w;
+    outlink->h = inlink->h / 2;
+    sf->ts_unit = av_q2d(av_inv_q(av_mul_q(outlink->frame_rate, outlink->time_base)));
+
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
+{
+    AVFilterContext *ctx = inlink->dst;
+    SeparateFieldsContext *sf = ctx->priv;
+    AVFilterLink *outlink = ctx->outputs[0];
+    AVFrame *second;
+    int i, ret;
+
+    inpicref->height = outlink->h;
+    inpicref->interlaced_frame = 0;
+
+    second = av_frame_clone(inpicref);
+    if (!second)
+        return AVERROR(ENOMEM);
+
+    for (i = 0; i < sf->nb_planes; i++) {
+        if (!inpicref->top_field_first)
+            inpicref->data[i] = inpicref->data[i] + inpicref->linesize[i];
+        else
+            second->data[i] = second->data[i] + second->linesize[i];
+        inpicref->linesize[i] *= 2;
+        second->linesize[i]   *= 2;
+    }
+
+    inpicref->pts = outlink->frame_count * sf->ts_unit;
+    ret = ff_filter_frame(outlink, inpicref);
+    if (ret < 0)
+        return ret;
+
+    second->pts = outlink->frame_count * sf->ts_unit;
+    return ff_filter_frame(outlink, second);
+}
+
+static const AVFilterPad separatefields_inputs[] = {
+    {
+        .name             = "default",
+        .type             = AVMEDIA_TYPE_VIDEO,
+        .filter_frame     = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad separatefields_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .config_props  = config_props_output,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_vf_separatefields = {
+    .name          = "separatefields",
+    .description   = NULL_IF_CONFIG_SMALL("Split input video frames into fields."),
+    .priv_size     = sizeof(SeparateFieldsContext),
+    .inputs        = separatefields_inputs,
+    .outputs       = separatefields_outputs,
+};
diff --git a/libavfilter/vf_setfield.c b/libavfilter/vf_setfield.c
index 43949fa..4d42cfa 100644
--- a/libavfilter/vf_setfield.c
+++ b/libavfilter/vf_setfield.c
@@ -54,32 +54,15 @@
 
 AVFILTER_DEFINE_CLASS(setfield);
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    SetFieldContext *setfield = ctx->priv;
-    static const char *shorthand[] = { "mode", NULL };
-
-    setfield->class = &setfield_class;
-    av_opt_set_defaults(setfield);
-
-    return av_opt_set_from_string(setfield, args, shorthand, "=", ":");
-}
-
-static av_cold void uninit(AVFilterContext *ctx)
-{
-    SetFieldContext *setfield = ctx->priv;
-    av_opt_free(setfield);
-}
-
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
+static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
 {
     SetFieldContext *setfield = inlink->dst->priv;
 
     if (setfield->mode == MODE_PROG) {
-        picref->video->interlaced = 0;
+        picref->interlaced_frame = 0;
     } else if (setfield->mode != MODE_AUTO) {
-        picref->video->interlaced = 1;
-        picref->video->top_field_first = setfield->mode;
+        picref->interlaced_frame = 1;
+        picref->top_field_first = setfield->mode;
     }
     return ff_filter_frame(inlink->dst->outputs[0], picref);
 }
@@ -105,9 +88,6 @@
 AVFilter avfilter_vf_setfield = {
     .name      = "setfield",
     .description = NULL_IF_CONFIG_SMALL("Force field for the output video frame."),
-    .init      = init,
-    .uninit    = uninit,
-
     .priv_size = sizeof(SetFieldContext),
     .inputs    = setfield_inputs,
     .outputs   = setfield_outputs,
diff --git a/libavfilter/vf_showinfo.c b/libavfilter/vf_showinfo.c
index f91721d..dd97843 100644
--- a/libavfilter/vf_showinfo.c
+++ b/libavfilter/vf_showinfo.c
@@ -35,14 +35,7 @@
     unsigned int frame;
 } ShowInfoContext;
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    ShowInfoContext *showinfo = ctx->priv;
-    showinfo->frame = 0;
-    return 0;
-}
-
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
 {
     AVFilterContext *ctx = inlink->dst;
     ShowInfoContext *showinfo = ctx->priv;
@@ -51,7 +44,7 @@
     int i, plane, vsub = desc->log2_chroma_h;
 
     for (plane = 0; plane < 4 && frame->data[plane]; plane++) {
-        int64_t linesize = av_image_get_linesize(frame->format, frame->video->w, plane);
+        int64_t linesize = av_image_get_linesize(frame->format, frame->width, plane);
         uint8_t *data = frame->data[plane];
         int h = plane == 1 || plane == 2 ? inlink->h >> vsub : inlink->h;
 
@@ -70,14 +63,14 @@
            "fmt:%s sar:%d/%d s:%dx%d i:%c iskey:%d type:%c "
            "checksum:%08X plane_checksum:[%08X",
            showinfo->frame,
-           av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base), frame->pos,
+           av_ts2str(frame->pts), av_ts2timestr(frame->pts, &inlink->time_base), av_frame_get_pkt_pos(frame),
            desc->name,
-           frame->video->sample_aspect_ratio.num, frame->video->sample_aspect_ratio.den,
-           frame->video->w, frame->video->h,
-           !frame->video->interlaced     ? 'P' :         /* Progressive  */
-           frame->video->top_field_first ? 'T' : 'B',    /* Top / Bottom */
-           frame->video->key_frame,
-           av_get_picture_type_char(frame->video->pict_type),
+           frame->sample_aspect_ratio.num, frame->sample_aspect_ratio.den,
+           frame->width, frame->height,
+           !frame->interlaced_frame ? 'P' :         /* Progressive  */
+           frame->top_field_first   ? 'T' : 'B',    /* Top / Bottom */
+           frame->key_frame,
+           av_get_picture_type_char(frame->pict_type),
            checksum, plane_checksum[0]);
 
     for (plane = 1; plane < 4 && frame->data[plane]; plane++)
@@ -94,7 +87,6 @@
         .type             = AVMEDIA_TYPE_VIDEO,
         .get_video_buffer = ff_null_get_video_buffer,
         .filter_frame     = filter_frame,
-        .min_perms        = AV_PERM_READ,
     },
     { NULL }
 };
@@ -112,7 +104,6 @@
     .description = NULL_IF_CONFIG_SMALL("Show textual information for each video frame."),
 
     .priv_size = sizeof(ShowInfoContext),
-    .init      = init,
 
     .inputs    = avfilter_vf_showinfo_inputs,
 
diff --git a/libavfilter/vf_smartblur.c b/libavfilter/vf_smartblur.c
index 54ab209..4dd1664 100644
--- a/libavfilter/vf_smartblur.c
+++ b/libavfilter/vf_smartblur.c
@@ -25,6 +25,7 @@
  * Ported from MPlayer libmpcodecs/vf_smartblur.c by Michael Niedermayer.
  */
 
+#include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libswscale/swscale.h"
 
@@ -50,6 +51,7 @@
 } FilterParam;
 
 typedef struct {
+    const AVClass *class;
     FilterParam  luma;
     FilterParam  chroma;
     int          hsub;
@@ -57,63 +59,51 @@
     unsigned int sws_flags;
 } SmartblurContext;
 
-#define CHECK_PARAM(param, name, min, max, format, ret)                       \
-    if (param < min || param > max) {                                         \
-        av_log(ctx, AV_LOG_ERROR,                                             \
-               "Invalid " #name " value " #format ": "                        \
-               "must be included between range " #format " and " #format "\n",\
-               param, min, max);                                              \
-        ret = AVERROR(EINVAL);                                                \
-    }
+#define OFFSET(x) offsetof(SmartblurContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static const AVOption smartblur_options[] = {
+    { "luma_radius",    "set luma radius",    OFFSET(luma.radius),    AV_OPT_TYPE_FLOAT, {.dbl=1.0}, RADIUS_MIN, RADIUS_MAX, .flags=FLAGS },
+    { "lr"         ,    "set luma radius",    OFFSET(luma.radius),    AV_OPT_TYPE_FLOAT, {.dbl=1.0}, RADIUS_MIN, RADIUS_MAX, .flags=FLAGS },
+    { "luma_strength",  "set luma strength",  OFFSET(luma.strength),  AV_OPT_TYPE_FLOAT, {.dbl=1.0}, STRENGTH_MIN, STRENGTH_MAX, .flags=FLAGS },
+    { "ls",             "set luma strength",  OFFSET(luma.strength),  AV_OPT_TYPE_FLOAT, {.dbl=1.0}, STRENGTH_MIN, STRENGTH_MAX, .flags=FLAGS },
+    { "luma_threshold", "set luma threshold", OFFSET(luma.threshold), AV_OPT_TYPE_INT,   {.i64=0}, THRESHOLD_MIN, THRESHOLD_MAX, .flags=FLAGS },
+    { "lt",             "set luma threshold", OFFSET(luma.threshold), AV_OPT_TYPE_INT,   {.i64=0}, THRESHOLD_MIN, THRESHOLD_MAX, .flags=FLAGS },
+
+    { "chroma_radius",    "set chroma radius",    OFFSET(chroma.radius),    AV_OPT_TYPE_FLOAT, {.dbl=RADIUS_MIN-1},   RADIUS_MIN-1, RADIUS_MAX, .flags=FLAGS },
+    { "cr",               "set chroma radius",    OFFSET(chroma.radius),    AV_OPT_TYPE_FLOAT, {.dbl=RADIUS_MIN-1},   RADIUS_MIN-1, RADIUS_MAX, .flags=FLAGS },
+    { "chroma_strength",  "set chroma strength",  OFFSET(chroma.strength),  AV_OPT_TYPE_FLOAT, {.dbl=STRENGTH_MIN-1}, STRENGTH_MIN-1, STRENGTH_MAX, .flags=FLAGS },
+    { "cs",               "set chroma strength",  OFFSET(chroma.strength),  AV_OPT_TYPE_FLOAT, {.dbl=STRENGTH_MIN-1}, STRENGTH_MIN-1, STRENGTH_MAX, .flags=FLAGS },
+    { "chroma_threshold", "set chroma threshold", OFFSET(chroma.threshold), AV_OPT_TYPE_INT,   {.i64=THRESHOLD_MIN-1}, THRESHOLD_MIN-1, THRESHOLD_MAX, .flags=FLAGS },
+    { "ct",               "set chroma threshold", OFFSET(chroma.threshold), AV_OPT_TYPE_INT,   {.i64=THRESHOLD_MIN-1}, THRESHOLD_MIN-1, THRESHOLD_MAX, .flags=FLAGS },
+
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(smartblur);
+
+static av_cold int init(AVFilterContext *ctx)
 {
     SmartblurContext *sblur = ctx->priv;
-    int n = 0, ret = 0;
-    float lradius, lstrength, cradius, cstrength;
-    int lthreshold, cthreshold;
 
-    if (args)
-        n = sscanf(args, "%f:%f:%d:%f:%f:%d",
-                   &lradius, &lstrength, &lthreshold,
-                   &cradius, &cstrength, &cthreshold);
-
-    if (n != 3 && n != 6) {
-        av_log(ctx, AV_LOG_ERROR,
-               "Incorrect number of parameters or invalid syntax: "
-               "must be luma_radius:luma_strength:luma_threshold"
-               "[:chroma_radius:chroma_strength:chroma_threshold]\n");
-        return AVERROR(EINVAL);
-    }
-
-    sblur->luma.radius    = lradius;
-    sblur->luma.strength  = lstrength;
-    sblur->luma.threshold = lthreshold;
-
-    if (n == 3) {
-        sblur->chroma.radius    = sblur->luma.radius;
+    /* make chroma default to luma values, if not explicitly set */
+    if (sblur->chroma.radius < RADIUS_MIN)
+        sblur->chroma.radius = sblur->luma.radius;
+    if (sblur->chroma.strength < STRENGTH_MIN)
         sblur->chroma.strength  = sblur->luma.strength;
+    if (sblur->chroma.threshold < THRESHOLD_MIN)
         sblur->chroma.threshold = sblur->luma.threshold;
-    } else {
-        sblur->chroma.radius    = cradius;
-        sblur->chroma.strength  = cstrength;
-        sblur->chroma.threshold = cthreshold;
-    }
 
     sblur->luma.quality = sblur->chroma.quality = 3.0;
     sblur->sws_flags = SWS_BICUBIC;
 
-    CHECK_PARAM(lradius,    luma radius,    RADIUS_MIN,    RADIUS_MAX,    %0.1f, ret)
-    CHECK_PARAM(lstrength,  luma strength,  STRENGTH_MIN,  STRENGTH_MAX,  %0.1f, ret)
-    CHECK_PARAM(lthreshold, luma threshold, THRESHOLD_MIN, THRESHOLD_MAX, %d,    ret)
+    av_log(ctx, AV_LOG_VERBOSE,
+           "luma_radius:%f luma_strength:%f luma_threshold:%d "
+           "chroma_radius:%f chroma_strength:%f chroma_threshold:%d\n",
+           sblur->luma.radius, sblur->luma.strength, sblur->luma.threshold,
+           sblur->chroma.radius, sblur->chroma.strength, sblur->chroma.threshold);
 
-    if (n != 3) {
-        CHECK_PARAM(sblur->chroma.radius,    chroma radius,    RADIUS_MIN,   RADIUS_MAX,    %0.1f, ret)
-        CHECK_PARAM(sblur->chroma.strength,  chroma strength,  STRENGTH_MIN, STRENGTH_MAX,  %0.1f, ret)
-        CHECK_PARAM(sblur->chroma.threshold, chroma threshold, THRESHOLD_MIN,THRESHOLD_MAX, %d,    ret)
-    }
-
-    return ret;
+    return 0;
 }
 
 static av_cold void uninit(AVFilterContext *ctx)
@@ -246,20 +236,20 @@
     }
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpic)
+static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
 {
     SmartblurContext  *sblur  = inlink->dst->priv;
     AVFilterLink *outlink     = inlink->dst->outputs[0];
-    AVFilterBufferRef *outpic;
+    AVFrame *outpic;
     int cw = inlink->w >> sblur->hsub;
     int ch = inlink->h >> sblur->vsub;
 
-    outpic = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+    outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!outpic) {
-        avfilter_unref_bufferp(&inpic);
+        av_frame_free(&inpic);
         return AVERROR(ENOMEM);
     }
-    avfilter_copy_buffer_ref_props(outpic, inpic);
+    av_frame_copy_props(outpic, inpic);
 
     blur(outpic->data[0], outpic->linesize[0],
          inpic->data[0],  inpic->linesize[0],
@@ -277,7 +267,7 @@
              sblur->chroma.filter_context);
     }
 
-    avfilter_unref_bufferp(&inpic);
+    av_frame_free(&inpic);
     return ff_filter_frame(outlink, outpic);
 }
 
@@ -287,7 +277,6 @@
         .type         = AVMEDIA_TYPE_VIDEO,
         .filter_frame = filter_frame,
         .config_props = config_props,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL }
 };
@@ -311,4 +300,6 @@
     .query_formats = query_formats,
     .inputs        = smartblur_inputs,
     .outputs       = smartblur_outputs,
+    .priv_class    = &smartblur_class,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE,
 };
diff --git a/libavfilter/vf_stereo3d.c b/libavfilter/vf_stereo3d.c
new file mode 100644
index 0000000..cc4bef6
--- /dev/null
+++ b/libavfilter/vf_stereo3d.c
@@ -0,0 +1,546 @@
+/*
+ * Copyright (c) 2010 Gordon Schmidt <gordon.schmidt <at> s2000.tu-chemnitz.de>
+ * Copyright (c) 2013 Paul B Mahol
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU 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 "libavutil/avassert.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/opt.h"
+#include "libavutil/parseutils.h"
+#include "libavutil/pixdesc.h"
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+enum StereoCode {
+    ANAGLYPH_RC_GRAY,   // anaglyph red/cyan gray
+    ANAGLYPH_RC_HALF,   // anaglyph red/cyan half colored
+    ANAGLYPH_RC_COLOR,  // anaglyph red/cyan colored
+    ANAGLYPH_RC_DUBOIS, // anaglyph red/cyan dubois
+    ANAGLYPH_GM_GRAY,   // anaglyph green/magenta gray
+    ANAGLYPH_GM_HALF,   // anaglyph green/magenta half colored
+    ANAGLYPH_GM_COLOR,  // anaglyph green/magenta colored
+    ANAGLYPH_GM_DUBOIS, // anaglyph green/magenta dubois
+    ANAGLYPH_YB_GRAY,   // anaglyph yellow/blue gray
+    ANAGLYPH_YB_HALF,   // anaglyph yellow/blue half colored
+    ANAGLYPH_YB_COLOR,  // anaglyph yellow/blue colored
+    ANAGLYPH_YB_DUBOIS, // anaglyph yellow/blue dubois
+    ANAGLYPH_RB_GRAY,   // anaglyph red/blue gray
+    ANAGLYPH_RG_GRAY,   // anaglyph red/green gray
+    MONO_L,             // mono output for debugging (left eye only)
+    MONO_R,             // mono output for debugging (right eye only)
+    INTERLEAVE_ROWS_LR, // row-interleave (left eye has top row)
+    INTERLEAVE_ROWS_RL, // row-interleave (right eye has top row)
+    SIDE_BY_SIDE_LR,    // side by side parallel (left eye left, right eye right)
+    SIDE_BY_SIDE_RL,    // side by side crosseye (right eye left, left eye right)
+    SIDE_BY_SIDE_2_LR,  // side by side parallel with half width resolution
+    SIDE_BY_SIDE_2_RL,  // side by side crosseye with half width resolution
+    ABOVE_BELOW_LR,     // above-below (left eye above, right eye below)
+    ABOVE_BELOW_RL,     // above-below (right eye above, left eye below)
+    ABOVE_BELOW_2_LR,   // above-below with half height resolution
+    ABOVE_BELOW_2_RL,   // above-below with half height resolution
+    STEREO_CODE_COUNT   // TODO: needs autodetection
+};
+
+typedef struct StereoComponent {
+    enum StereoCode format;
+    int width, height;
+    int off_left, off_right;
+    int off_lstep, off_rstep;
+    int row_left, row_right;
+} StereoComponent;
+
+static const int ana_coeff[][3][6] = {
+  [ANAGLYPH_RB_GRAY]   =
+    {{19595, 38470,  7471,     0,     0,     0},
+     {    0,     0,     0,     0,     0,     0},
+     {    0,     0,     0, 19595, 38470,  7471}},
+  [ANAGLYPH_RG_GRAY]   =
+    {{19595, 38470,  7471,     0,     0,     0},
+     {    0,     0,     0, 19595, 38470,  7471},
+     {    0,     0,     0,     0,     0,     0}},
+  [ANAGLYPH_RC_GRAY]   =
+    {{19595, 38470,  7471,     0,     0,     0},
+     {    0,     0,     0, 19595, 38470,  7471},
+     {    0,     0,     0, 19595, 38470,  7471}},
+  [ANAGLYPH_RC_HALF]   =
+    {{19595, 38470,  7471,     0,     0,     0},
+     {    0,     0,     0,     0, 65536,     0},
+     {    0,     0,     0,     0,     0, 65536}},
+  [ANAGLYPH_RC_COLOR]  =
+    {{65536,     0,     0,     0,     0,     0},
+     {    0,     0,     0,     0, 65536,     0},
+     {    0,     0,     0,     0,     0, 65536}},
+  [ANAGLYPH_RC_DUBOIS] =
+    {{29891, 32800, 11559, -2849, -5763,  -102},
+     {-2627, -2479, -1033, 24804, 48080, -1209},
+     { -997, -1350,  -358, -4729, -7403, 80373}},
+  [ANAGLYPH_GM_GRAY]   =
+    {{    0,     0,     0, 19595, 38470,  7471},
+     {19595, 38470,  7471,     0,     0,     0},
+     {    0,     0,     0, 19595, 38470,  7471}},
+  [ANAGLYPH_GM_HALF]   =
+    {{    0,     0,     0, 65536,     0,     0},
+     {19595, 38470,  7471,     0,     0,     0},
+     {    0,     0,     0,     0,     0, 65536}},
+  [ANAGLYPH_GM_COLOR]  =
+    {{    0,     0,     0, 65536,     0,     0},
+     {    0, 65536,     0,     0,     0,     0},
+     {    0,     0,     0,     0,     0, 65536}},
+  [ANAGLYPH_GM_DUBOIS]  =
+    {{-4063,-10354, -2556, 34669, 46203,  1573},
+     {18612, 43778,  9372, -1049,  -983, -4260},
+     { -983, -1769,  1376,   590,  4915, 61407}},
+  [ANAGLYPH_YB_GRAY]   =
+    {{    0,     0,     0, 19595, 38470,  7471},
+     {    0,     0,     0, 19595, 38470,  7471},
+     {19595, 38470,  7471,     0,     0,     0}},
+  [ANAGLYPH_YB_HALF]   =
+    {{    0,     0,     0, 65536,     0,     0},
+     {    0,     0,     0,     0, 65536,     0},
+     {19595, 38470,  7471,     0,     0,     0}},
+  [ANAGLYPH_YB_COLOR]  =
+    {{    0,     0,     0, 65536,     0,     0},
+     {    0,     0,     0,     0, 65536,     0},
+     {    0,     0, 65536,     0,     0,     0}},
+  [ANAGLYPH_YB_DUBOIS] =
+    {{65535,-12650,18451,   -987, -7590, -1049},
+     {-1604, 56032, 4196,    370,  3826, -1049},
+     {-2345,-10676, 1358,   5801, 11416, 56217}},
+};
+
+typedef struct Stereo3DContext {
+    const AVClass *class;
+    StereoComponent in, out;
+    int width, height;
+    int row_step;
+    int ana_matrix[3][6];
+    int nb_planes;
+    int linesize[4];
+    int pixstep[4];
+} Stereo3DContext;
+
+#define OFFSET(x) offsetof(Stereo3DContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption stereo3d_options[] = {
+    { "in",    "set input format",  OFFSET(in.format),  AV_OPT_TYPE_INT, {.i64=SIDE_BY_SIDE_LR}, SIDE_BY_SIDE_LR, ABOVE_BELOW_2_RL, FLAGS, "in"},
+    { "ab2l",  "above below half height left first",  0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR},  0, 0, FLAGS, "in" },
+    { "ab2r",  "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL},  0, 0, FLAGS, "in" },
+    { "abl",   "above below left first",              0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR},    0, 0, FLAGS, "in" },
+    { "abr",   "above below right first",             0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL},    0, 0, FLAGS, "in" },
+    { "sbs2l", "side by side half width left first",  0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR}, 0, 0, FLAGS, "in" },
+    { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL}, 0, 0, FLAGS, "in" },
+    { "sbsl",  "side by side left first",             0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR},   0, 0, FLAGS, "in" },
+    { "sbsr",  "side by side right first",            0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL},   0, 0, FLAGS, "in" },
+    { "out",   "set output format", OFFSET(out.format), AV_OPT_TYPE_INT, {.i64=ANAGLYPH_RC_DUBOIS}, 0, STEREO_CODE_COUNT-1, FLAGS, "out"},
+    { "ab2l",  "above below half height left first",  0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_LR},   0, 0, FLAGS, "out" },
+    { "ab2r",  "above below half height right first", 0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_2_RL},   0, 0, FLAGS, "out" },
+    { "abl",   "above below left first",              0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_LR},     0, 0, FLAGS, "out" },
+    { "abr",   "above below right first",             0, AV_OPT_TYPE_CONST, {.i64=ABOVE_BELOW_RL},     0, 0, FLAGS, "out" },
+    { "agmc",  "anaglyph green magenta color",        0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_COLOR},  0, 0, FLAGS, "out" },
+    { "agmd",  "anaglyph green magenta dubois",       0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_DUBOIS}, 0, 0, FLAGS, "out" },
+    { "agmg",  "anaglyph green magenta gray",         0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_GRAY},   0, 0, FLAGS, "out" },
+    { "agmh",  "anaglyph green magenta half color",   0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_GM_HALF},   0, 0, FLAGS, "out" },
+    { "arbg",  "anaglyph red blue gray",              0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RB_GRAY},   0, 0, FLAGS, "out" },
+    { "arcc",  "anaglyph red cyan color",             0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_COLOR},  0, 0, FLAGS, "out" },
+    { "arcd",  "anaglyph red cyan dubois",            0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_DUBOIS}, 0, 0, FLAGS, "out" },
+    { "arcg",  "anaglyph red cyan gray",              0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_GRAY},   0, 0, FLAGS, "out" },
+    { "arch",  "anaglyph red cyan half color",        0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RC_HALF},   0, 0, FLAGS, "out" },
+    { "argg",  "anaglyph red green gray",             0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_RG_GRAY},   0, 0, FLAGS, "out" },
+    { "aybc",  "anaglyph yellow blue color",          0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_COLOR},  0, 0, FLAGS, "out" },
+    { "aybd",  "anaglyph yellow blue dubois",         0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_DUBOIS}, 0, 0, FLAGS, "out" },
+    { "aybg",  "anaglyph yellow blue gray",           0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_GRAY},   0, 0, FLAGS, "out" },
+    { "aybh",  "anaglyph yellow blue half color",     0, AV_OPT_TYPE_CONST, {.i64=ANAGLYPH_YB_HALF},   0, 0, FLAGS, "out" },
+    { "irl",   "interleave rows left first",          0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_LR}, 0, 0, FLAGS, "out" },
+    { "irr",   "interleave rows right first",         0, AV_OPT_TYPE_CONST, {.i64=INTERLEAVE_ROWS_RL}, 0, 0, FLAGS, "out" },
+    { "ml",    "mono left",                           0, AV_OPT_TYPE_CONST, {.i64=MONO_L},             0, 0, FLAGS, "out" },
+    { "mr",    "mono right",                          0, AV_OPT_TYPE_CONST, {.i64=MONO_R},             0, 0, FLAGS, "out" },
+    { "sbs2l", "side by side half width left first",  0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_LR},  0, 0, FLAGS, "out" },
+    { "sbs2r", "side by side half width right first", 0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_2_RL},  0, 0, FLAGS, "out" },
+    { "sbsl",  "side by side left first",             0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_LR},    0, 0, FLAGS, "out" },
+    { "sbsr",  "side by side right first",            0, AV_OPT_TYPE_CONST, {.i64=SIDE_BY_SIDE_RL},    0, 0, FLAGS, "out" },
+    {NULL},
+};
+
+AVFILTER_DEFINE_CLASS(stereo3d);
+
+static const enum AVPixelFormat anaglyph_pix_fmts[] = { AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE };
+static const enum AVPixelFormat other_pix_fmts[] = {
+    AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
+    AV_PIX_FMT_RGB48BE, AV_PIX_FMT_BGR48BE,
+    AV_PIX_FMT_RGB48LE, AV_PIX_FMT_BGR48LE,
+    AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_BGRA64BE,
+    AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_BGRA64LE,
+    AV_PIX_FMT_RGBA,  AV_PIX_FMT_BGRA,
+    AV_PIX_FMT_ARGB,  AV_PIX_FMT_ABGR,
+    AV_PIX_FMT_RGB0,  AV_PIX_FMT_BGR0,
+    AV_PIX_FMT_0RGB,  AV_PIX_FMT_0BGR,
+    AV_PIX_FMT_GBRP,
+    AV_PIX_FMT_GBRP9BE,  AV_PIX_FMT_GBRP9LE,
+    AV_PIX_FMT_GBRP10BE, AV_PIX_FMT_GBRP10LE,
+    AV_PIX_FMT_GBRP12BE, AV_PIX_FMT_GBRP12LE,
+    AV_PIX_FMT_GBRP14BE, AV_PIX_FMT_GBRP14LE,
+    AV_PIX_FMT_GBRP16BE, AV_PIX_FMT_GBRP16LE,
+    AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
+    AV_PIX_FMT_YUVJ444P,
+    AV_PIX_FMT_YUV444P9LE,  AV_PIX_FMT_YUVA444P9LE,
+    AV_PIX_FMT_YUV444P9BE,  AV_PIX_FMT_YUVA444P9BE,
+    AV_PIX_FMT_YUV444P10LE, AV_PIX_FMT_YUVA444P10LE,
+    AV_PIX_FMT_YUV444P10BE, AV_PIX_FMT_YUVA444P10BE,
+    AV_PIX_FMT_YUV444P12BE,  AV_PIX_FMT_YUV444P12LE,
+    AV_PIX_FMT_YUV444P14BE,  AV_PIX_FMT_YUV444P14LE,
+    AV_PIX_FMT_YUV444P16LE, AV_PIX_FMT_YUVA444P16LE,
+    AV_PIX_FMT_YUV444P16BE, AV_PIX_FMT_YUVA444P16BE,
+    AV_PIX_FMT_NONE
+};
+
+static int query_formats(AVFilterContext *ctx)
+{
+    Stereo3DContext *s = ctx->priv;
+    const enum AVPixelFormat *pix_fmts;
+
+    switch (s->out.format) {
+    case ANAGLYPH_GM_COLOR:
+    case ANAGLYPH_GM_DUBOIS:
+    case ANAGLYPH_GM_GRAY:
+    case ANAGLYPH_GM_HALF:
+    case ANAGLYPH_RB_GRAY:
+    case ANAGLYPH_RC_COLOR:
+    case ANAGLYPH_RC_DUBOIS:
+    case ANAGLYPH_RC_GRAY:
+    case ANAGLYPH_RC_HALF:
+    case ANAGLYPH_RG_GRAY:
+    case ANAGLYPH_YB_COLOR:
+    case ANAGLYPH_YB_DUBOIS:
+    case ANAGLYPH_YB_GRAY:
+    case ANAGLYPH_YB_HALF:
+        pix_fmts = anaglyph_pix_fmts;
+        break;
+    default:
+        pix_fmts = other_pix_fmts;
+    }
+
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+
+    return 0;
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    AVFilterLink *inlink = ctx->inputs[0];
+    Stereo3DContext *s = ctx->priv;
+    AVRational aspect = inlink->sample_aspect_ratio;
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
+    int ret;
+
+    switch (s->in.format) {
+    case SIDE_BY_SIDE_2_LR:
+    case SIDE_BY_SIDE_LR:
+    case SIDE_BY_SIDE_2_RL:
+    case SIDE_BY_SIDE_RL:
+        if (inlink->w & 1) {
+            av_log(ctx, AV_LOG_ERROR, "width must be even\n");
+            return AVERROR_INVALIDDATA;
+        }
+        break;
+    case ABOVE_BELOW_2_LR:
+    case ABOVE_BELOW_LR:
+    case ABOVE_BELOW_2_RL:
+    case ABOVE_BELOW_RL:
+        if (s->out.format == INTERLEAVE_ROWS_LR ||
+            s->out.format == INTERLEAVE_ROWS_RL) {
+            if (inlink->h & 3) {
+                av_log(ctx, AV_LOG_ERROR, "height must be multiple of 4\n");
+                return AVERROR_INVALIDDATA;
+            }
+        }
+        if (inlink->h & 1) {
+            av_log(ctx, AV_LOG_ERROR, "height must be even\n");
+            return AVERROR_INVALIDDATA;
+        }
+        break;
+    }
+
+    s->in.width     =
+    s->width        = inlink->w;
+    s->in.height    =
+    s->height       = inlink->h;
+    s->row_step     = 1;
+    s->in.off_lstep =
+    s->in.off_rstep =
+    s->in.off_left  =
+    s->in.off_right =
+    s->in.row_left  =
+    s->in.row_right = 0;
+
+    switch (s->in.format) {
+    case SIDE_BY_SIDE_2_LR:
+        aspect.num     *= 2;
+    case SIDE_BY_SIDE_LR:
+        s->width        = inlink->w / 2;
+        s->in.off_right = s->width;
+        break;
+    case SIDE_BY_SIDE_2_RL:
+        aspect.num     *= 2;
+    case SIDE_BY_SIDE_RL:
+        s->width        = inlink->w / 2;
+        s->in.off_left  = s->width;
+        break;
+    case ABOVE_BELOW_2_LR:
+        aspect.den     *= 2;
+    case ABOVE_BELOW_LR:
+        s->in.row_right =
+        s->height       = inlink->h / 2;
+        break;
+    case ABOVE_BELOW_2_RL:
+        aspect.den     *= 2;
+    case ABOVE_BELOW_RL:
+        s->in.row_left  =
+        s->height       = inlink->h / 2;
+        break;
+    default:
+        av_log(ctx, AV_LOG_ERROR, "input format %d is not supported\n", s->in.format);
+        return AVERROR(EINVAL);
+    }
+
+    s->out.width     = s->width;
+    s->out.height    = s->height;
+    s->out.off_lstep =
+    s->out.off_rstep =
+    s->out.off_left  =
+    s->out.off_right =
+    s->out.row_left  =
+    s->out.row_right = 0;
+
+    switch (s->out.format) {
+    case ANAGLYPH_RB_GRAY:
+    case ANAGLYPH_RG_GRAY:
+    case ANAGLYPH_RC_GRAY:
+    case ANAGLYPH_RC_HALF:
+    case ANAGLYPH_RC_COLOR:
+    case ANAGLYPH_RC_DUBOIS:
+    case ANAGLYPH_GM_GRAY:
+    case ANAGLYPH_GM_HALF:
+    case ANAGLYPH_GM_COLOR:
+    case ANAGLYPH_GM_DUBOIS:
+    case ANAGLYPH_YB_GRAY:
+    case ANAGLYPH_YB_HALF:
+    case ANAGLYPH_YB_COLOR:
+    case ANAGLYPH_YB_DUBOIS:
+        memcpy(s->ana_matrix, ana_coeff[s->out.format], sizeof(s->ana_matrix));
+        break;
+    case SIDE_BY_SIDE_2_LR:
+        aspect.num      /= 2;
+    case SIDE_BY_SIDE_LR:
+        s->out.width     = s->width * 2;
+        s->out.off_right = s->width;
+        break;
+    case SIDE_BY_SIDE_2_RL:
+        aspect.num      /= 2;
+    case SIDE_BY_SIDE_RL:
+        s->out.width     = s->width * 2;
+        s->out.off_left  = s->width;
+        break;
+    case ABOVE_BELOW_2_LR:
+        aspect.den      /= 2;
+    case ABOVE_BELOW_LR:
+        s->out.height    = s->height * 2;
+        s->out.row_right = s->height;
+        break;
+    case ABOVE_BELOW_2_RL:
+        aspect.den      /= 2;
+    case ABOVE_BELOW_RL:
+        s->out.height    = s->height * 2;
+        s->out.row_left  = s->height;
+        break;
+    case INTERLEAVE_ROWS_LR:
+        s->row_step      = 2;
+        s->height        = s->height / 2;
+        s->out.off_rstep =
+        s->in.off_rstep  = 1;
+        break;
+    case INTERLEAVE_ROWS_RL:
+        s->row_step      = 2;
+        s->height        = s->height / 2;
+        s->out.off_lstep =
+        s->in.off_lstep  = 1;
+        break;
+    case MONO_R:
+        s->in.off_left   = s->in.off_right;
+        s->in.row_left   = s->in.row_right;
+    case MONO_L:
+        break;
+    default:
+        av_log(ctx, AV_LOG_ERROR, "output format is not supported\n");
+        return AVERROR(EINVAL);
+    }
+
+    outlink->w = s->out.width;
+    outlink->h = s->out.height;
+    outlink->sample_aspect_ratio = aspect;
+
+    if ((ret = av_image_fill_linesizes(s->linesize, outlink->format, s->width)) < 0)
+        return ret;
+    s->nb_planes = av_pix_fmt_count_planes(outlink->format);
+    av_image_fill_max_pixsteps(s->pixstep, NULL, desc);
+
+    return 0;
+}
+
+static inline uint8_t ana_convert(const int *coeff, uint8_t *left, uint8_t *right)
+{
+    int sum;
+
+    sum  = coeff[0] * left[0] + coeff[3] * right[0]; //red in
+    sum += coeff[1] * left[1] + coeff[4] * right[1]; //green in
+    sum += coeff[2] * left[2] + coeff[5] * right[2]; //blue in
+
+    return av_clip_uint8(sum >> 16);
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
+{
+    AVFilterContext *ctx  = inlink->dst;
+    Stereo3DContext *s = ctx->priv;
+    AVFilterLink *outlink = ctx->outputs[0];
+    AVFrame *out;
+    int out_off_left[4], out_off_right[4];
+    int in_off_left[4], in_off_right[4];
+    int i;
+
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+    if (!out) {
+        av_frame_free(&inpicref);
+        return AVERROR(ENOMEM);
+    }
+    av_frame_copy_props(out, inpicref);
+
+    for (i = 0; i < 4; i++) {
+        in_off_left[i]   = (s->in.row_left   + s->in.off_lstep)  * inpicref->linesize[i] + s->in.off_left   * s->pixstep[i];
+        in_off_right[i]  = (s->in.row_right  + s->in.off_rstep)  * inpicref->linesize[i] + s->in.off_right  * s->pixstep[i];
+        out_off_left[i]  = (s->out.row_left  + s->out.off_lstep) * out->linesize[i]      + s->out.off_left  * s->pixstep[i];
+        out_off_right[i] = (s->out.row_right + s->out.off_rstep) * out->linesize[i]      + s->out.off_right * s->pixstep[i];
+    }
+
+    switch (s->out.format) {
+    case SIDE_BY_SIDE_LR:
+    case SIDE_BY_SIDE_RL:
+    case SIDE_BY_SIDE_2_LR:
+    case SIDE_BY_SIDE_2_RL:
+    case ABOVE_BELOW_LR:
+    case ABOVE_BELOW_RL:
+    case ABOVE_BELOW_2_LR:
+    case ABOVE_BELOW_2_RL:
+    case INTERLEAVE_ROWS_LR:
+    case INTERLEAVE_ROWS_RL:
+        for (i = 0; i < s->nb_planes; i++) {
+            av_image_copy_plane(out->data[i] + out_off_left[i],
+                                out->linesize[i] * s->row_step,
+                                inpicref->data[i] + in_off_left[i],
+                                inpicref->linesize[i] * s->row_step,
+                                s->linesize[i], s->height);
+            av_image_copy_plane(out->data[i] + out_off_right[i],
+                                out->linesize[i] * s->row_step,
+                                inpicref->data[i] + in_off_right[i],
+                                inpicref->linesize[i] * s->row_step,
+                                s->linesize[i], s->height);
+        }
+        break;
+    case MONO_L:
+    case MONO_R:
+        for (i = 0; i < s->nb_planes; i++) {
+            av_image_copy_plane(out->data[i], out->linesize[i],
+                                inpicref->data[i] + in_off_left[i],
+                                inpicref->linesize[i],
+                                s->linesize[i], s->height);
+        }
+        break;
+    case ANAGLYPH_RB_GRAY:
+    case ANAGLYPH_RG_GRAY:
+    case ANAGLYPH_RC_GRAY:
+    case ANAGLYPH_RC_HALF:
+    case ANAGLYPH_RC_COLOR:
+    case ANAGLYPH_RC_DUBOIS:
+    case ANAGLYPH_GM_GRAY:
+    case ANAGLYPH_GM_HALF:
+    case ANAGLYPH_GM_COLOR:
+    case ANAGLYPH_GM_DUBOIS:
+    case ANAGLYPH_YB_GRAY:
+    case ANAGLYPH_YB_HALF:
+    case ANAGLYPH_YB_COLOR:
+    case ANAGLYPH_YB_DUBOIS: {
+        int i, x, y, il, ir, o;
+        uint8_t *src = inpicref->data[0];
+        uint8_t *dst = out->data[0];
+        int out_width = s->out.width;
+        int *ana_matrix[3];
+
+        for (i = 0; i < 3; i++)
+            ana_matrix[i] = s->ana_matrix[i];
+
+        for (y = 0; y < s->out.height; y++) {
+            o   = out->linesize[0] * y;
+            il  = in_off_left[0]  + y * inpicref->linesize[0];
+            ir  = in_off_right[0] + y * inpicref->linesize[0];
+            for (x = 0; x < out_width; x++, il += 3, ir += 3, o+= 3) {
+                dst[o    ] = ana_convert(ana_matrix[0], src + il, src + ir);
+                dst[o + 1] = ana_convert(ana_matrix[1], src + il, src + ir);
+                dst[o + 2] = ana_convert(ana_matrix[2], src + il, src + ir);
+            }
+        }
+        break;
+    }
+    default:
+        av_assert0(0);
+    }
+
+    av_frame_free(&inpicref);
+    return ff_filter_frame(outlink, out);
+}
+
+static const AVFilterPad stereo3d_inputs[] = {
+    {
+        .name             = "default",
+        .type             = AVMEDIA_TYPE_VIDEO,
+        .filter_frame     = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad stereo3d_outputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .config_props = config_output,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_vf_stereo3d = {
+    .name          = "stereo3d",
+    .description   = NULL_IF_CONFIG_SMALL("Convert video stereoscopic 3D view."),
+    .priv_size     = sizeof(Stereo3DContext),
+    .query_formats = query_formats,
+    .inputs        = stereo3d_inputs,
+    .outputs       = stereo3d_outputs,
+    .priv_class    = &stereo3d_class,
+};
diff --git a/libavfilter/vf_subtitles.c b/libavfilter/vf_subtitles.c
index 7513e24..10f07a4 100644
--- a/libavfilter/vf_subtitles.c
+++ b/libavfilter/vf_subtitles.c
@@ -85,17 +85,9 @@
     av_log(ctx, level, "\n");
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args, const AVClass *class)
+static av_cold int init(AVFilterContext *ctx)
 {
     AssContext *ass = ctx->priv;
-    static const char *shorthand[] = { "filename", NULL };
-    int ret;
-
-    ass->class = class;
-    av_opt_set_defaults(ass);
-
-    if ((ret = av_opt_set_from_string(ass, args, shorthand, "=", ":")) < 0)
-        return ret;
 
     if (!ass->filename) {
         av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
@@ -123,7 +115,6 @@
 {
     AssContext *ass = ctx->priv;
 
-    av_opt_free(ass);
     if (ass->track)
         ass_free_track(ass->track);
     if (ass->renderer)
@@ -158,7 +149,7 @@
 #define AB(c)  (((c)>>8) &0xFF)
 #define AA(c)  ((0xFF-c) &0xFF)
 
-static void overlay_ass_image(AssContext *ass, AVFilterBufferRef *picref,
+static void overlay_ass_image(AssContext *ass, AVFrame *picref,
                               const ASS_Image *image)
 {
     for (; image; image = image->next) {
@@ -167,13 +158,13 @@
         ff_draw_color(&ass->draw, &color, rgba_color);
         ff_blend_mask(&ass->draw, &color,
                       picref->data, picref->linesize,
-                      picref->video->w, picref->video->h,
+                      picref->width, picref->height,
                       image->bitmap, image->stride, image->w, image->h,
                       3, 0, image->dst_x, image->dst_y);
     }
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
+static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
 {
     AVFilterContext *ctx = inlink->dst;
     AVFilterLink *outlink = ctx->outputs[0];
@@ -197,7 +188,7 @@
         .type             = AVMEDIA_TYPE_VIDEO,
         .filter_frame     = filter_frame,
         .config_props     = config_input,
-        .min_perms        = AV_PERM_READ | AV_PERM_WRITE,
+        .needs_writable   = 1,
     },
     { NULL }
 };
@@ -219,10 +210,10 @@
 
 AVFILTER_DEFINE_CLASS(ass);
 
-static av_cold int init_ass(AVFilterContext *ctx, const char *args)
+static av_cold int init_ass(AVFilterContext *ctx)
 {
     AssContext *ass = ctx->priv;
-    int ret = init(ctx, args, &ass_class);
+    int ret = init(ctx);
 
     if (ret < 0)
         return ret;
@@ -260,7 +251,7 @@
 
 AVFILTER_DEFINE_CLASS(subtitles);
 
-static av_cold int init_subtitles(AVFilterContext *ctx, const char *args)
+static av_cold int init_subtitles(AVFilterContext *ctx)
 {
     int ret, sid;
     AVDictionary *codec_opts = NULL;
@@ -273,7 +264,7 @@
     AssContext *ass = ctx->priv;
 
     /* Init libass */
-    ret = init(ctx, args, &subtitles_class);
+    ret = init(ctx);
     if (ret < 0)
         return ret;
     ass->track = ass_new_track(ass->library);
@@ -311,7 +302,7 @@
         return AVERROR(EINVAL);
     }
     dec_desc = avcodec_descriptor_get(dec_ctx->codec_id);
-    if (dec_desc && (dec_desc->props & AV_CODEC_PROP_BITMAP_SUB)) {
+    if (dec_desc && !(dec_desc->props & AV_CODEC_PROP_TEXT_SUB)) {
         av_log(ctx, AV_LOG_ERROR,
                "Only text based subtitles are currently supported\n");
         return AVERROR_PATCHWELCOME;
diff --git a/libavfilter/vf_super2xsai.c b/libavfilter/vf_super2xsai.c
index e2db3b4..4f25968 100644
--- a/libavfilter/vf_super2xsai.c
+++ b/libavfilter/vf_super2xsai.c
@@ -303,23 +303,23 @@
     return 0;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
+static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
 {
     AVFilterLink *outlink = inlink->dst->outputs[0];
-    AVFilterBufferRef *outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+    AVFrame *outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!outpicref) {
-        avfilter_unref_bufferp(&inpicref);
+        av_frame_free(&inpicref);
         return AVERROR(ENOMEM);
     }
-    avfilter_copy_buffer_ref_props(outpicref, inpicref);
-    outpicref->video->w = outlink->w;
-    outpicref->video->h = outlink->h;
+    av_frame_copy_props(outpicref, inpicref);
+    outpicref->width  = outlink->w;
+    outpicref->height = outlink->h;
 
     super2xsai(inlink->dst, inpicref->data[0], inpicref->linesize[0],
                outpicref->data[0], outpicref->linesize[0],
                inlink->w, inlink->h);
 
-    avfilter_unref_bufferp(&inpicref);
+    av_frame_free(&inpicref);
     return ff_filter_frame(outlink, outpicref);
 }
 
@@ -329,7 +329,6 @@
         .type         = AVMEDIA_TYPE_VIDEO,
         .config_props = config_input,
         .filter_frame = filter_frame,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL }
 };
diff --git a/libavfilter/vf_swapuv.c b/libavfilter/vf_swapuv.c
index 82cc07c..2ca97f9 100644
--- a/libavfilter/vf_swapuv.c
+++ b/libavfilter/vf_swapuv.c
@@ -29,23 +29,24 @@
 #include "internal.h"
 #include "video.h"
 
-static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms,
-                                           int w, int h)
+static void do_swap(AVFrame *frame)
 {
-    AVFilterBufferRef *picref =
-        ff_default_get_video_buffer(link, perms, w, h);
+    FFSWAP(uint8_t*,     frame->data[1],     frame->data[2]);
+    FFSWAP(int,          frame->linesize[1], frame->linesize[2]);
+    FFSWAP(uint64_t,     frame->error[1],    frame->error[2]);
+    FFSWAP(AVBufferRef*, frame->buf[1],      frame->buf[2]);
+}
 
-    FFSWAP(uint8_t*, picref->data[1], picref->data[2]);
-    FFSWAP(int, picref->linesize[1], picref->linesize[2]);
-
+static AVFrame *get_video_buffer(AVFilterLink *link, int w, int h)
+{
+    AVFrame *picref = ff_default_get_video_buffer(link, w, h);
+    do_swap(picref);
     return picref;
 }
 
-static int filter_frame(AVFilterLink *link, AVFilterBufferRef *inpicref)
+static int filter_frame(AVFilterLink *link, AVFrame *inpicref)
 {
-    FFSWAP(uint8_t*, inpicref->data[1], inpicref->data[2]);
-    FFSWAP(int, inpicref->linesize[1], inpicref->linesize[2]);
-
+    do_swap(inpicref);
     return ff_filter_frame(link->dst->outputs[0], inpicref);
 }
 
diff --git a/libavfilter/vf_telecine.c b/libavfilter/vf_telecine.c
new file mode 100644
index 0000000..136df22
--- /dev/null
+++ b/libavfilter/vf_telecine.c
@@ -0,0 +1,282 @@
+/*
+ * Copyright (c) 2012 Rudolf Polzer
+ * Copyright (c) 2013 Paul B Mahol
+ *
+ * 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 telecine filter, heavily based from mpv-player:TOOLS/vf_dlopen/telecine.c by
+ * Rudolf Polzer.
+ */
+
+#include "libavutil/avstring.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+typedef struct {
+    const AVClass *class;
+    int first_field;
+    char *pattern;
+    unsigned int pattern_pos;
+
+    AVRational pts;
+    double ts_unit;
+    int out_cnt;
+    int occupied;
+
+    int nb_planes;
+    int planeheight[4];
+    int stride[4];
+
+    AVFrame *frame[5];
+    AVFrame *temp;
+} TelecineContext;
+
+#define OFFSET(x) offsetof(TelecineContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption telecine_options[] = {
+    {"first_field", "select first field", OFFSET(first_field), AV_OPT_TYPE_INT,   {.i64=0}, 0, 1, FLAGS, "field"},
+        {"top",    "select top field first",                0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
+        {"t",      "select top field first",                0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
+        {"bottom", "select bottom field first",             0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
+        {"b",      "select bottom field first",             0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
+    {"pattern", "pattern that describe for how many fields a frame is to be displayed", OFFSET(pattern), AV_OPT_TYPE_STRING, {.str="23"}, 0, 0, FLAGS},
+    {NULL}
+};
+
+AVFILTER_DEFINE_CLASS(telecine);
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    TelecineContext *tc = ctx->priv;
+    const char *p;
+    int max = 0;
+
+    if (!strlen(tc->pattern)) {
+        av_log(ctx, AV_LOG_ERROR, "No pattern provided.\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    for (p = tc->pattern; *p; p++) {
+        if (!av_isdigit(*p)) {
+            av_log(ctx, AV_LOG_ERROR, "Provided pattern includes non-numeric characters.\n");
+            return AVERROR_INVALIDDATA;
+        }
+
+        max = FFMAX(*p - '0', max);
+        tc->pts.num += 2;
+        tc->pts.den += *p - '0';
+    }
+
+    tc->out_cnt = (max + 1) / 2;
+    av_log(ctx, AV_LOG_INFO, "Telecine pattern %s yields up to %d frames per frame, pts advance factor: %d/%d\n",
+           tc->pattern, tc->out_cnt, tc->pts.num, tc->pts.den);
+
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    AVFilterFormats *pix_fmts = NULL;
+    int fmt;
+
+    for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
+        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
+        if (!(desc->flags & PIX_FMT_HWACCEL))
+            ff_add_format(&pix_fmts, fmt);
+    }
+
+    ff_set_common_formats(ctx, pix_fmts);
+    return 0;
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    TelecineContext *tc = inlink->dst->priv;
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+    int i, ret;
+
+    tc->temp = ff_get_video_buffer(inlink, inlink->w, inlink->h);
+    if (!tc->temp)
+        return AVERROR(ENOMEM);
+    for (i = 0; i < tc->out_cnt; i++) {
+        tc->frame[i] = ff_get_video_buffer(inlink, inlink->w, inlink->h);
+        if (!tc->frame[i])
+            return AVERROR(ENOMEM);
+    }
+
+    if ((ret = av_image_fill_linesizes(tc->stride, inlink->format, inlink->w)) < 0)
+        return ret;
+
+    tc->planeheight[1] = tc->planeheight[2] = inlink->h >> desc->log2_chroma_h;
+    tc->planeheight[0] = tc->planeheight[3] = inlink->h;
+
+    tc->nb_planes = av_pix_fmt_count_planes(inlink->format);
+
+    return 0;
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    TelecineContext *tc = ctx->priv;
+    const AVFilterLink *inlink = ctx->inputs[0];
+    AVRational fps = inlink->frame_rate;
+
+    if (!fps.num || !fps.den) {
+        av_log(ctx, AV_LOG_ERROR, "The input needs a constant frame rate; "
+               "current rate of %d/%d is invalid\n", fps.num, fps.den);
+        return AVERROR(EINVAL);
+    }
+    fps = av_mul_q(fps, av_inv_q(tc->pts));
+    av_log(ctx, AV_LOG_VERBOSE, "FPS: %d/%d -> %d/%d\n",
+           inlink->frame_rate.num, inlink->frame_rate.den, fps.num, fps.den);
+
+    outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
+    outlink->frame_rate = fps;
+    outlink->time_base = av_mul_q(inlink->time_base, tc->pts);
+
+    tc->ts_unit = av_q2d(av_inv_q(av_mul_q(fps, outlink->time_base)));
+
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
+{
+    AVFilterContext *ctx = inlink->dst;
+    AVFilterLink *outlink = ctx->outputs[0];
+    TelecineContext *tc = ctx->priv;
+    int i, len, ret = 0, nout = 0;
+
+    len = tc->pattern[tc->pattern_pos] - '0';
+
+    tc->pattern_pos++;
+    if (!tc->pattern[tc->pattern_pos])
+        tc->pattern_pos = 0;
+
+    if (!len) { // do not output any field from this frame
+        av_frame_free(&inpicref);
+        return 0;
+    }
+
+    if (tc->occupied) {
+        for (i = 0; i < tc->nb_planes; i++) {
+            // fill in the EARLIER field from the buffered pic
+            av_image_copy_plane(tc->frame[nout]->data[i] + tc->frame[nout]->linesize[i] * tc->first_field,
+                                tc->frame[nout]->linesize[i] * 2,
+                                tc->temp->data[i] + tc->temp->linesize[i] * tc->first_field,
+                                tc->temp->linesize[i] * 2,
+                                tc->stride[i],
+                                (tc->planeheight[i] - tc->first_field + 1) / 2);
+            // fill in the LATER field from the new pic
+            av_image_copy_plane(tc->frame[nout]->data[i] + tc->frame[nout]->linesize[i] * !tc->first_field,
+                                tc->frame[nout]->linesize[i] * 2,
+                                inpicref->data[i] + inpicref->linesize[i] * !tc->first_field,
+                                inpicref->linesize[i] * 2,
+                                tc->stride[i],
+                                (tc->planeheight[i] - !tc->first_field + 1) / 2);
+        }
+        nout++;
+        len--;
+        tc->occupied = 0;
+    }
+
+    while (len >= 2) {
+        // output THIS image as-is
+        for (i = 0; i < tc->nb_planes; i++)
+            av_image_copy_plane(tc->frame[nout]->data[i], tc->frame[nout]->linesize[i],
+                                inpicref->data[i], inpicref->linesize[i],
+                                tc->stride[i],
+                                tc->planeheight[i]);
+        nout++;
+        len -= 2;
+    }
+
+    if (len >= 1) {
+        // copy THIS image to the buffer, we need it later
+        for (i = 0; i < tc->nb_planes; i++)
+            av_image_copy_plane(tc->temp->data[i], tc->temp->linesize[i],
+                                inpicref->data[i], inpicref->linesize[i],
+                                tc->stride[i],
+                                tc->planeheight[i]);
+        tc->occupied = 1;
+    }
+
+    for (i = 0; i < nout; i++) {
+        AVFrame *frame = av_frame_clone(tc->frame[i]);
+
+        if (!frame) {
+            av_frame_free(&inpicref);
+            return AVERROR(ENOMEM);
+        }
+
+        av_frame_copy_props(frame, inpicref);
+        frame->pts = outlink->frame_count * tc->ts_unit;
+        ret = ff_filter_frame(outlink, frame);
+    }
+    av_frame_free(&inpicref);
+
+    return ret;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    TelecineContext *tc = ctx->priv;
+    int i;
+
+    av_frame_free(&tc->temp);
+    for (i = 0; i < tc->out_cnt; i++)
+        av_frame_free(&tc->frame[i]);
+}
+
+static const AVFilterPad telecine_inputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .filter_frame  = filter_frame,
+        .config_props  = config_input,
+    },
+    { NULL }
+};
+
+static const AVFilterPad telecine_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .config_props  = config_output,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_vf_telecine = {
+    .name          = "telecine",
+    .description   = NULL_IF_CONFIG_SMALL("Apply a telecine pattern."),
+    .priv_size     = sizeof(TelecineContext),
+    .priv_class    = &telecine_class,
+    .init          = init,
+    .uninit        = uninit,
+    .query_formats = query_formats,
+    .inputs        = telecine_inputs,
+    .outputs       = telecine_outputs,
+};
diff --git a/libavfilter/vf_thumbnail.c b/libavfilter/vf_thumbnail.c
index 0d245d9..f0bb3db 100644
--- a/libavfilter/vf_thumbnail.c
+++ b/libavfilter/vf_thumbnail.c
@@ -27,37 +27,39 @@
  * @see http://notbrainsurgery.livejournal.com/29773.html
  */
 
+#include "libavutil/opt.h"
 #include "avfilter.h"
 #include "internal.h"
 
 #define HIST_SIZE (3*256)
 
 struct thumb_frame {
-    AVFilterBufferRef *buf;     ///< cached frame
+    AVFrame *buf;               ///< cached frame
     int histogram[HIST_SIZE];   ///< RGB color distribution histogram of the frame
 };
 
 typedef struct {
+    const AVClass *class;
     int n;                      ///< current frame
     int n_frames;               ///< number of frames for analysis
     struct thumb_frame *frames; ///< the n_frames frames
+    AVRational tb;              ///< copy of the input timebase to ease access
 } ThumbContext;
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+#define OFFSET(x) offsetof(ThumbContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption thumbnail_options[] = {
+    { "n", "set the frames batch size", OFFSET(n_frames), AV_OPT_TYPE_INT, {.i64=100}, 2, INT_MAX, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(thumbnail);
+
+static av_cold int init(AVFilterContext *ctx)
 {
     ThumbContext *thumb = ctx->priv;
 
-    if (!args) {
-        thumb->n_frames = 100;
-    } else {
-        int n = sscanf(args, "%d", &thumb->n_frames);
-        if (n != 1 || thumb->n_frames < 2) {
-            thumb->n_frames = 0;
-            av_log(ctx, AV_LOG_ERROR,
-                   "Invalid number of frames specified (minimum is 2).\n");
-            return AVERROR(EINVAL);
-        }
-    }
     thumb->frames = av_calloc(thumb->n_frames, sizeof(*thumb->frames));
     if (!thumb->frames) {
         av_log(ctx, AV_LOG_ERROR,
@@ -86,14 +88,52 @@
     return sum_sq_err;
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
+static AVFrame *get_best_frame(AVFilterContext *ctx)
 {
+    AVFrame *picref;
+    ThumbContext *thumb = ctx->priv;
     int i, j, best_frame_idx = 0;
+    int nb_frames = thumb->n;
     double avg_hist[HIST_SIZE] = {0}, sq_err, min_sq_err = -1;
+
+    // average histogram of the N frames
+    for (j = 0; j < FF_ARRAY_ELEMS(avg_hist); j++) {
+        for (i = 0; i < nb_frames; i++)
+            avg_hist[j] += (double)thumb->frames[i].histogram[j];
+        avg_hist[j] /= nb_frames;
+    }
+
+    // find the frame closer to the average using the sum of squared errors
+    for (i = 0; i < nb_frames; i++) {
+        sq_err = frame_sum_square_err(thumb->frames[i].histogram, avg_hist);
+        if (i == 0 || sq_err < min_sq_err)
+            best_frame_idx = i, min_sq_err = sq_err;
+    }
+
+    // free and reset everything (except the best frame buffer)
+    for (i = 0; i < nb_frames; i++) {
+        memset(thumb->frames[i].histogram, 0, sizeof(thumb->frames[i].histogram));
+        if (i != best_frame_idx)
+            av_frame_free(&thumb->frames[i].buf);
+    }
+    thumb->n = 0;
+
+    // raise the chosen one
+    picref = thumb->frames[best_frame_idx].buf;
+    av_log(ctx, AV_LOG_INFO, "frame id #%d (pts_time=%f) selected "
+           "from a set of %d images\n", best_frame_idx,
+           picref->pts * av_q2d(thumb->tb), nb_frames);
+    thumb->frames[best_frame_idx].buf = NULL;
+
+    return picref;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
+{
+    int i, j;
     AVFilterContext *ctx  = inlink->dst;
     ThumbContext *thumb   = ctx->priv;
     AVFilterLink *outlink = ctx->outputs[0];
-    AVFilterBufferRef *picref;
     int *hist = thumb->frames[thumb->n].histogram;
     const uint8_t *p = frame->data[0];
 
@@ -111,40 +151,11 @@
     }
 
     // no selection until the buffer of N frames is filled up
-    if (thumb->n < thumb->n_frames - 1) {
-        thumb->n++;
+    thumb->n++;
+    if (thumb->n < thumb->n_frames)
         return 0;
-    }
 
-    // average histogram of the N frames
-    for (j = 0; j < FF_ARRAY_ELEMS(avg_hist); j++) {
-        for (i = 0; i < thumb->n_frames; i++)
-            avg_hist[j] += (double)thumb->frames[i].histogram[j];
-        avg_hist[j] /= thumb->n_frames;
-    }
-
-    // find the frame closer to the average using the sum of squared errors
-    for (i = 0; i < thumb->n_frames; i++) {
-        sq_err = frame_sum_square_err(thumb->frames[i].histogram, avg_hist);
-        if (i == 0 || sq_err < min_sq_err)
-            best_frame_idx = i, min_sq_err = sq_err;
-    }
-
-    // free and reset everything (except the best frame buffer)
-    for (i = 0; i < thumb->n_frames; i++) {
-        memset(thumb->frames[i].histogram, 0, sizeof(thumb->frames[i].histogram));
-        if (i == best_frame_idx)
-            continue;
-        avfilter_unref_bufferp(&thumb->frames[i].buf);
-    }
-    thumb->n = 0;
-
-    // raise the chosen one
-    picref = thumb->frames[best_frame_idx].buf;
-    av_log(ctx, AV_LOG_INFO, "frame id #%d (pts_time=%f) selected\n",
-           best_frame_idx, picref->pts * av_q2d(inlink->time_base));
-    thumb->frames[best_frame_idx].buf = NULL;
-    return ff_filter_frame(outlink, picref);
+    return ff_filter_frame(outlink, get_best_frame(ctx));
 }
 
 static av_cold void uninit(AVFilterContext *ctx)
@@ -152,44 +163,38 @@
     int i;
     ThumbContext *thumb = ctx->priv;
     for (i = 0; i < thumb->n_frames && thumb->frames[i].buf; i++)
-        avfilter_unref_bufferp(&thumb->frames[i].buf);
+        av_frame_free(&thumb->frames[i].buf);
     av_freep(&thumb->frames);
 }
 
 static int request_frame(AVFilterLink *link)
 {
-    ThumbContext *thumb = link->src->priv;
+    AVFilterContext *ctx = link->src;
+    ThumbContext *thumb = ctx->priv;
 
     /* loop until a frame thumbnail is available (when a frame is queued,
      * thumb->n is reset to zero) */
     do {
-        int ret = ff_request_frame(link->src->inputs[0]);
+        int ret = ff_request_frame(ctx->inputs[0]);
+        if (ret == AVERROR_EOF && thumb->n) {
+            ret = ff_filter_frame(link, get_best_frame(ctx));
+            if (ret < 0)
+                return ret;
+            ret = AVERROR_EOF;
+        }
         if (ret < 0)
             return ret;
     } while (thumb->n);
     return 0;
 }
 
-static int poll_frame(AVFilterLink *link)
+static int config_props(AVFilterLink *inlink)
 {
-    ThumbContext *thumb  = link->src->priv;
-    AVFilterLink *inlink = link->src->inputs[0];
-    int ret, available_frames = ff_poll_frame(inlink);
+    AVFilterContext *ctx = inlink->dst;
+    ThumbContext *thumb = ctx->priv;
 
-    /* If the input link is not able to provide any frame, we can't do anything
-     * at the moment and thus have zero thumbnail available. */
-    if (!available_frames)
-        return 0;
-
-    /* Since at least one frame is available and the next frame will allow us
-     * to compute a thumbnail, we can return 1 frame. */
-    if (thumb->n == thumb->n_frames - 1)
-        return 1;
-
-    /* we have some frame(s) available in the input link, but not yet enough to
-     * output a thumbnail, so we request more */
-    ret = ff_request_frame(inlink);
-    return ret < 0 ? ret : 0;
+    thumb->tb = inlink->time_base;
+    return 0;
 }
 
 static int query_formats(AVFilterContext *ctx)
@@ -206,8 +211,8 @@
     {
         .name             = "default",
         .type             = AVMEDIA_TYPE_VIDEO,
+        .config_props     = config_props,
         .get_video_buffer = ff_null_get_video_buffer,
-        .min_perms        = AV_PERM_PRESERVE,
         .filter_frame     = filter_frame,
     },
     { NULL }
@@ -218,7 +223,6 @@
         .name          = "default",
         .type          = AVMEDIA_TYPE_VIDEO,
         .request_frame = request_frame,
-        .poll_frame    = poll_frame,
     },
     { NULL }
 };
@@ -232,4 +236,5 @@
     .query_formats = query_formats,
     .inputs        = thumbnail_inputs,
     .outputs       = thumbnail_outputs,
+    .priv_class    = &thumbnail_class,
 };
diff --git a/libavfilter/vf_tile.c b/libavfilter/vf_tile.c
index e4ced88..f5eb763 100644
--- a/libavfilter/vf_tile.c
+++ b/libavfilter/vf_tile.c
@@ -40,7 +40,7 @@
     unsigned nb_frames;
     FFDrawContext draw;
     FFDrawColor blank;
-    AVFilterBufferRef *out_ref;
+    AVFrame *out_ref;
 } TileContext;
 
 #define REASONABLE_SIZE 1024
@@ -51,28 +51,20 @@
 static const AVOption tile_options[] = {
     { "layout", "set grid size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE,
         {.str = "6x5"}, 0, 0, FLAGS },
+    { "nb_frames", "set maximum number of frame to render", OFFSET(nb_frames),
+        AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
     { "margin",  "set outer border margin in pixels",    OFFSET(margin),
         AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
     { "padding", "set inner border thickness in pixels", OFFSET(padding),
         AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1024, FLAGS },
-    { "nb_frames", "set maximum number of frame to render", OFFSET(nb_frames),
-        AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, FLAGS },
     {NULL},
 };
 
 AVFILTER_DEFINE_CLASS(tile);
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     TileContext *tile = ctx->priv;
-    static const char *shorthand[] = { "layout", "nb_frames", "margin", "padding", NULL };
-    int ret;
-
-    tile->class = &tile_class;
-    av_opt_set_defaults(tile);
-
-    if ((ret = av_opt_set_from_string(tile, args, shorthand, "=", ":")) < 0)
-        return ret;
 
     if (tile->w > REASONABLE_SIZE || tile->h > REASONABLE_SIZE) {
         av_log(ctx, AV_LOG_ERROR, "Tile size %ux%u is insane.\n",
@@ -124,6 +116,8 @@
     /* TODO make the color an option, or find an unified way of choosing it */
     ff_draw_color(&tile->draw, &tile->blank, (uint8_t[]){ 0, 0, 0, -1 });
 
+    outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
+
     return 0;
 }
 
@@ -138,7 +132,7 @@
     *y = tile->margin + (inlink->h + tile->padding) * ty;
 }
 
-static void draw_blank_frame(AVFilterContext *ctx, AVFilterBufferRef *out_buf)
+static void draw_blank_frame(AVFilterContext *ctx, AVFrame *out_buf)
 {
     TileContext *tile    = ctx->priv;
     AVFilterLink *inlink = ctx->inputs[0];
@@ -154,7 +148,7 @@
 {
     TileContext *tile     = ctx->priv;
     AVFilterLink *outlink = ctx->outputs[0];
-    AVFilterBufferRef *out_buf = tile->out_ref;
+    AVFrame *out_buf = tile->out_ref;
     int ret;
 
     while (tile->current < tile->nb_frames)
@@ -168,7 +162,7 @@
  * buffers are fed to filter_frame in the order they were obtained from
  * get_buffer (think B-frames). */
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
+static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
 {
     AVFilterContext *ctx  = inlink->dst;
     TileContext *tile     = ctx->priv;
@@ -176,13 +170,12 @@
     unsigned x0, y0;
 
     if (!tile->current) {
-        tile->out_ref = ff_get_video_buffer(outlink, AV_PERM_WRITE,
-                                            outlink->w, outlink->h);
+        tile->out_ref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
         if (!tile->out_ref)
             return AVERROR(ENOMEM);
-        avfilter_copy_buffer_ref_props(tile->out_ref, picref);
-        tile->out_ref->video->w = outlink->w;
-        tile->out_ref->video->h = outlink->h;
+        av_frame_copy_props(tile->out_ref, picref);
+        tile->out_ref->width  = outlink->w;
+        tile->out_ref->height = outlink->h;
 
         /* fill surface once for margin/padding */
         if (tile->margin || tile->padding)
@@ -198,7 +191,7 @@
                        picref->data, picref->linesize,
                        x0, y0, 0, 0, inlink->w, inlink->h);
 
-    avfilter_unref_bufferp(&picref);
+    av_frame_free(&picref);
     if (++tile->current == tile->nb_frames)
         return end_last_frame(ctx);
 
@@ -212,16 +205,9 @@
     AVFilterLink *inlink = ctx->inputs[0];
     int r;
 
-    while (1) {
-        r = ff_request_frame(inlink);
-        if (r < 0) {
-            if (r == AVERROR_EOF && tile->current)
-                r = end_last_frame(ctx);
-            break;
-        }
-        if (!tile->current) /* done */
-            break;
-    }
+    r = ff_request_frame(inlink);
+    if (r == AVERROR_EOF && tile->current)
+        r = end_last_frame(ctx);
     return r;
 }
 
@@ -230,7 +216,6 @@
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
         .filter_frame = filter_frame,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL }
 };
diff --git a/libavfilter/vf_tinterlace.c b/libavfilter/vf_tinterlace.c
index 909784e..13b8ce2 100644
--- a/libavfilter/vf_tinterlace.c
+++ b/libavfilter/vf_tinterlace.c
@@ -48,8 +48,8 @@
     int flags;                  ///< flags affecting interlacing algorithm
     int frame;                  ///< number of the output frame
     int vsub;                   ///< chroma vertical subsampling
-    AVFilterBufferRef *cur;
-    AVFilterBufferRef *next;
+    AVFrame *cur;
+    AVFrame *next;
     uint8_t *black_data[4];     ///< buffer used to fill padded lines
     int black_linesize[4];
 } TInterlaceContext;
@@ -97,25 +97,12 @@
     return 0;
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    TInterlaceContext *tinterlace = ctx->priv;
-    static const char *shorthand[] = { "mode", NULL };
-
-    tinterlace->class = &tinterlace_class;
-    av_opt_set_defaults(tinterlace);
-
-    return av_opt_set_from_string(tinterlace, args, shorthand, "=", ":");
-}
-
 static av_cold void uninit(AVFilterContext *ctx)
 {
     TInterlaceContext *tinterlace = ctx->priv;
 
-    avfilter_unref_bufferp(&tinterlace->cur );
-    avfilter_unref_bufferp(&tinterlace->next);
-
-    av_opt_free(tinterlace);
+    av_frame_free(&tinterlace->cur );
+    av_frame_free(&tinterlace->next);
     av_freep(&tinterlace->black_data[0]);
 }
 
@@ -228,15 +215,15 @@
     }
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
+static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
 {
     AVFilterContext *ctx = inlink->dst;
     AVFilterLink *outlink = ctx->outputs[0];
     TInterlaceContext *tinterlace = ctx->priv;
-    AVFilterBufferRef *cur, *next, *out;
+    AVFrame *cur, *next, *out;
     int field, tff, ret;
 
-    avfilter_unref_buffer(tinterlace->cur);
+    av_frame_free(&tinterlace->cur);
     tinterlace->cur  = tinterlace->next;
     tinterlace->next = picref;
 
@@ -249,13 +236,13 @@
     switch (tinterlace->mode) {
     case MODE_MERGE: /* move the odd frame into the upper field of the new image, even into
              * the lower field, generating a double-height video at half framerate */
-        out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
         if (!out)
             return AVERROR(ENOMEM);
-        avfilter_copy_buffer_ref_props(out, cur);
-        out->video->h = outlink->h;
-        out->video->interlaced = 1;
-        out->video->top_field_first = 1;
+        av_frame_copy_props(out, cur);
+        out->height = outlink->h;
+        out->interlaced_frame = 1;
+        out->top_field_first = 1;
 
         /* write odd frame lines into the upper field of the new frame */
         copy_picture_field(out->data, out->linesize,
@@ -267,20 +254,22 @@
                            (const uint8_t **)next->data, next->linesize,
                            inlink->format, inlink->w, inlink->h,
                            FIELD_UPPER_AND_LOWER, 1, FIELD_LOWER, tinterlace->flags);
-        avfilter_unref_bufferp(&tinterlace->next);
+        av_frame_free(&tinterlace->next);
         break;
 
     case MODE_DROP_ODD:  /* only output even frames, odd  frames are dropped; height unchanged, half framerate */
     case MODE_DROP_EVEN: /* only output odd  frames, even frames are dropped; height unchanged, half framerate */
-        out = avfilter_ref_buffer(tinterlace->mode == MODE_DROP_EVEN ? cur : next, AV_PERM_READ);
-        avfilter_unref_bufferp(&tinterlace->next);
+        out = av_frame_clone(tinterlace->mode == MODE_DROP_EVEN ? cur : next);
+        av_frame_free(&tinterlace->next);
         break;
 
     case MODE_PAD: /* expand each frame to double height, but pad alternate
                     * lines with black; framerate unchanged */
-        out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
-        avfilter_copy_buffer_ref_props(out, cur);
-        out->video->h = outlink->h;
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+        if (!out)
+            return AVERROR(ENOMEM);
+        av_frame_copy_props(out, cur);
+        out->height = outlink->h;
 
         field = (1 + tinterlace->frame) & 1 ? FIELD_UPPER : FIELD_LOWER;
         /* copy upper and lower fields */
@@ -300,12 +289,12 @@
     case MODE_INTERLEAVE_TOP:    /* top    field first */
     case MODE_INTERLEAVE_BOTTOM: /* bottom field first */
         tff = tinterlace->mode == MODE_INTERLEAVE_TOP;
-        out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
         if (!out)
             return AVERROR(ENOMEM);
-        avfilter_copy_buffer_ref_props(out, cur);
-        out->video->interlaced = 1;
-        out->video->top_field_first = tff;
+        av_frame_copy_props(out, cur);
+        out->interlaced_frame = 1;
+        out->top_field_first = tff;
 
         /* copy upper/lower field from cur */
         copy_picture_field(out->data, out->linesize,
@@ -319,25 +308,25 @@
                            inlink->format, inlink->w, inlink->h,
                            tff ? FIELD_LOWER : FIELD_UPPER, 1, tff ? FIELD_LOWER : FIELD_UPPER,
                            tinterlace->flags);
-        avfilter_unref_bufferp(&tinterlace->next);
+        av_frame_free(&tinterlace->next);
         break;
     case MODE_INTERLACEX2: /* re-interlace preserving image height, double frame rate */
         /* output current frame first */
-        out = avfilter_ref_buffer(cur, ~AV_PERM_WRITE);
+        out = av_frame_clone(cur);
         if (!out)
             return AVERROR(ENOMEM);
-        out->video->interlaced = 1;
+        out->interlaced_frame = 1;
 
         if ((ret = ff_filter_frame(outlink, out)) < 0)
             return ret;
 
         /* output mix of current and next frame */
-        tff = next->video->top_field_first;
-        out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+        tff = next->top_field_first;
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
         if (!out)
             return AVERROR(ENOMEM);
-        avfilter_copy_buffer_ref_props(out, next);
-        out->video->interlaced = 1;
+        av_frame_copy_props(out, next);
+        out->interlaced_frame = 1;
 
         /* write current frame second field lines into the second field of the new frame */
         copy_picture_field(out->data, out->linesize,
@@ -400,7 +389,6 @@
     .name          = "tinterlace",
     .description   = NULL_IF_CONFIG_SMALL("Perform temporal field interlacing."),
     .priv_size     = sizeof(TInterlaceContext),
-    .init          = init,
     .uninit        = uninit,
     .query_formats = query_formats,
     .inputs        = tinterlace_inputs,
diff --git a/libavfilter/vf_transpose.c b/libavfilter/vf_transpose.c
index 270d346..2b1fa1f 100644
--- a/libavfilter/vf_transpose.c
+++ b/libavfilter/vf_transpose.c
@@ -32,6 +32,7 @@
 #include "libavutil/pixdesc.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
+#include "libavutil/opt.h"
 #include "avfilter.h"
 #include "formats.h"
 #include "internal.h"
@@ -43,47 +44,22 @@
     TRANSPOSE_PT_TYPE_PORTRAIT,
 } PassthroughType;
 
+enum TransposeDir {
+    TRANSPOSE_CCLOCK_FLIP,
+    TRANSPOSE_CLOCK,
+    TRANSPOSE_CCLOCK,
+    TRANSPOSE_CLOCK_FLIP,
+};
+
 typedef struct {
     const AVClass *class;
     int hsub, vsub;
     int pixsteps[4];
 
-    /* 0    Rotate by 90 degrees counterclockwise and vflip. */
-    /* 1    Rotate by 90 degrees clockwise.                  */
-    /* 2    Rotate by 90 degrees counterclockwise.           */
-    /* 3    Rotate by 90 degrees clockwise and vflip.        */
-    int dir;
     PassthroughType passthrough; ///< landscape passthrough mode enabled
+    enum TransposeDir dir;
 } TransContext;
 
-#define OFFSET(x) offsetof(TransContext, x)
-#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
-
-static const AVOption transpose_options[] = {
-    { "dir", "set transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, {.i64=0},  0, 7, FLAGS },
-
-    { "passthrough", "do not apply transposition if the input matches the specified geometry",
-      OFFSET(passthrough), AV_OPT_TYPE_INT, {.i64=TRANSPOSE_PT_TYPE_NONE},  0, INT_MAX, FLAGS, "passthrough" },
-    { "none",      "always apply transposition",   0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_NONE},      INT_MIN, INT_MAX, FLAGS, "passthrough" },
-    { "portrait",  "preserve portrait geometry",   0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_PORTRAIT},  INT_MIN, INT_MAX, FLAGS, "passthrough" },
-    { "landscape", "preserve landscape geometry",  0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_LANDSCAPE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
-
-    { NULL },
-};
-
-AVFILTER_DEFINE_CLASS(transpose);
-
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    TransContext *trans = ctx->priv;
-    const char *shorthand[] = { "dir", "passthrough", NULL };
-
-    trans->class = &transpose_class;
-    av_opt_set_defaults(trans);
-
-    return av_opt_set_from_string(trans, args, shorthand, "=", ":");
-}
-
 static int query_formats(AVFilterContext *ctx)
 {
     AVFilterFormats *pix_fmts = NULL;
@@ -148,47 +124,47 @@
     return 0;
 }
 
-static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
+static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h)
 {
     TransContext *trans = inlink->dst->priv;
 
     return trans->passthrough ?
-        ff_null_get_video_buffer   (inlink, perms, w, h) :
-        ff_default_get_video_buffer(inlink, perms, w, h);
+        ff_null_get_video_buffer   (inlink, w, h) :
+        ff_default_get_video_buffer(inlink, w, h);
 }
 
-static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     TransContext *trans = inlink->dst->priv;
     AVFilterLink *outlink = inlink->dst->outputs[0];
-    AVFilterBufferRef *out;
+    AVFrame *out;
     int plane;
 
     if (trans->passthrough)
         return ff_filter_frame(outlink, in);
 
-    out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!out) {
-        avfilter_unref_bufferp(&in);
+        av_frame_free(&in);
         return AVERROR(ENOMEM);
     }
 
     out->pts = in->pts;
 
-    if (in->video->sample_aspect_ratio.num == 0) {
-        out->video->sample_aspect_ratio = in->video->sample_aspect_ratio;
+    if (in->sample_aspect_ratio.num == 0) {
+        out->sample_aspect_ratio = in->sample_aspect_ratio;
     } else {
-        out->video->sample_aspect_ratio.num = in->video->sample_aspect_ratio.den;
-        out->video->sample_aspect_ratio.den = in->video->sample_aspect_ratio.num;
+        out->sample_aspect_ratio.num = in->sample_aspect_ratio.den;
+        out->sample_aspect_ratio.den = in->sample_aspect_ratio.num;
     }
 
     for (plane = 0; out->data[plane]; plane++) {
         int hsub = plane == 1 || plane == 2 ? trans->hsub : 0;
         int vsub = plane == 1 || plane == 2 ? trans->vsub : 0;
         int pixstep = trans->pixsteps[plane];
-        int inh  = in->video->h>>vsub;
-        int outw = out->video->w>>hsub;
-        int outh = out->video->h>>vsub;
+        int inh  = in->height  >> vsub;
+        int outw = out->width  >> hsub;
+        int outh = out->height >> vsub;
         uint8_t *dst, *src;
         int dstlinesize, srclinesize;
         int x, y;
@@ -243,17 +219,38 @@
         }
     }
 
-    avfilter_unref_bufferp(&in);
+    av_frame_free(&in);
     return ff_filter_frame(outlink, out);
 }
 
+#define OFFSET(x) offsetof(TransContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption transpose_options[] = {
+    { "dir", "Transpose direction", OFFSET(dir), AV_OPT_TYPE_INT, { .i64 = TRANSPOSE_CCLOCK_FLIP },
+        TRANSPOSE_CCLOCK_FLIP, TRANSPOSE_CLOCK_FLIP, FLAGS, "dir" },
+        { "cclock_flip", "counter-clockwise with vertical flip", 0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK_FLIP }, .unit = "dir" },
+        { "clock",       "clockwise",                            0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK       }, .unit = "dir" },
+        { "cclock",      "counter-clockwise",                    0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CCLOCK      }, .unit = "dir" },
+        { "clock_flip",  "clockwise with vertical flip",         0, AV_OPT_TYPE_CONST, { .i64 = TRANSPOSE_CLOCK_FLIP  }, .unit = "dir" },
+
+    { "passthrough", "do not apply transposition if the input matches the specified geometry",
+      OFFSET(passthrough), AV_OPT_TYPE_INT, {.i64=TRANSPOSE_PT_TYPE_NONE},  0, INT_MAX, FLAGS, "passthrough" },
+    { "none",      "always apply transposition",   0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_NONE},      INT_MIN, INT_MAX, FLAGS, "passthrough" },
+    { "portrait",  "preserve portrait geometry",   0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_PORTRAIT},  INT_MIN, INT_MAX, FLAGS, "passthrough" },
+    { "landscape", "preserve landscape geometry",  0, AV_OPT_TYPE_CONST, {.i64=TRANSPOSE_PT_TYPE_LANDSCAPE}, INT_MIN, INT_MAX, FLAGS, "passthrough" },
+
+    { NULL },
+};
+
+AVFILTER_DEFINE_CLASS(transpose);
+
 static const AVFilterPad avfilter_vf_transpose_inputs[] = {
     {
         .name        = "default",
         .type        = AVMEDIA_TYPE_VIDEO,
         .get_video_buffer= get_video_buffer,
         .filter_frame = filter_frame,
-        .min_perms   = AV_PERM_READ,
     },
     { NULL }
 };
@@ -271,12 +268,11 @@
     .name      = "transpose",
     .description = NULL_IF_CONFIG_SMALL("Transpose input video."),
 
-    .init = init,
     .priv_size = sizeof(TransContext),
+    .priv_class = &transpose_class,
 
     .query_formats = query_formats,
 
     .inputs    = avfilter_vf_transpose_inputs,
     .outputs   = avfilter_vf_transpose_outputs,
-    .priv_class = &transpose_class,
 };
diff --git a/libavfilter/vf_unsharp.c b/libavfilter/vf_unsharp.c
index 8c6e18a..2d7aab2 100644
--- a/libavfilter/vf_unsharp.c
+++ b/libavfilter/vf_unsharp.c
@@ -66,35 +66,13 @@
 
 typedef struct {
     const AVClass *class;
+    int lmsize_x, lmsize_y, cmsize_x, cmsize_y;
+    float lamount, camount;
     FilterParam luma;   ///< luma parameters (width, height, amount)
     FilterParam chroma; ///< chroma parameters (width, height, amount)
     int hsub, vsub;
-    int luma_msize_x, luma_msize_y, chroma_msize_x, chroma_msize_y;
-    double luma_amount, chroma_amount;
 } UnsharpContext;
 
-#define OFFSET(x) offsetof(UnsharpContext, x)
-
-static const AVOption unsharp_options[] = {
-    { "luma_msize_x",    "set luma matrix x size",     OFFSET(luma_msize_x),    AV_OPT_TYPE_INT,    {.i64=5}, 3, 63 },
-    { "lx",              "set luma matrix x size",     OFFSET(luma_msize_x),    AV_OPT_TYPE_INT,    {.i64=5}, 3, 63 },
-    { "luma_msize_y",    "set luma matrix y size",     OFFSET(luma_msize_y),    AV_OPT_TYPE_INT,    {.i64=5}, 3, 63 },
-    { "ly",              "set luma matrix y size",     OFFSET(luma_msize_y),    AV_OPT_TYPE_INT,    {.i64=5}, 3, 63 },
-    { "luma_amount",     "set luma effect amount",     OFFSET(luma_amount),     AV_OPT_TYPE_DOUBLE, {.dbl=1.0}, -DBL_MAX, DBL_MAX },
-    { "la",              "set luma effect amount",     OFFSET(luma_amount),     AV_OPT_TYPE_DOUBLE, {.dbl=1.0}, -DBL_MAX, DBL_MAX },
-
-    { "chroma_msize_x",  "set chroma matrix x size",   OFFSET(chroma_msize_x), AV_OPT_TYPE_INT,    {.i64=5}, 3, 63 },
-    { "cx",              "set chroma matrix x size",   OFFSET(chroma_msize_x), AV_OPT_TYPE_INT,    {.i64=5}, 3, 63 },
-    { "chroma_msize_y",  "set chroma matrix y size",   OFFSET(chroma_msize_y), AV_OPT_TYPE_INT,    {.i64=5}, 3, 63 },
-    { "cy"          ,    "set chroma matrix y size",   OFFSET(chroma_msize_y), AV_OPT_TYPE_INT,    {.i64=5}, 3, 63 },
-    { "chroma_amount",   "set chroma effect strenght", OFFSET(chroma_amount),  AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -DBL_MAX, DBL_MAX },
-    { "ca",              "set chroma effect strenght", OFFSET(chroma_amount),  AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -DBL_MAX, DBL_MAX },
-
-    { NULL }
-};
-
-AVFILTER_DEFINE_CLASS(unsharp);
-
 static void apply_unsharp(      uint8_t *dst, int dst_stride,
                           const uint8_t *src, int src_stride,
                           int width, int height, FilterParam *fp)
@@ -153,7 +131,7 @@
     }
 }
 
-static void set_filter_param(FilterParam *fp, int msize_x, int msize_y, double amount)
+static void set_filter_param(FilterParam *fp, int msize_x, int msize_y, float amount)
 {
     fp->msize_x = msize_x;
     fp->msize_y = msize_y;
@@ -165,24 +143,13 @@
     fp->halfscale = 1 << (fp->scalebits - 1);
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     UnsharpContext *unsharp = ctx->priv;
-    static const char *shorthand[] = {
-        "luma_msize_x", "luma_msize_y", "luma_amount",
-        "chroma_msize_x", "chroma_msize_y", "chroma_amount",
-        NULL
-    };
-    int ret;
 
-    unsharp->class = &unsharp_class;
-    av_opt_set_defaults(unsharp);
 
-    if ((ret = av_opt_set_from_string(unsharp, args, shorthand, "=", ":")) < 0)
-        return ret;
-
-    set_filter_param(&unsharp->luma,   unsharp->luma_msize_x,   unsharp->luma_msize_y,   unsharp->luma_amount);
-    set_filter_param(&unsharp->chroma, unsharp->chroma_msize_x, unsharp->chroma_msize_y, unsharp->chroma_amount);
+    set_filter_param(&unsharp->luma,   unsharp->lmsize_x, unsharp->lmsize_y, unsharp->lamount);
+    set_filter_param(&unsharp->chroma, unsharp->cmsize_x, unsharp->cmsize_y, unsharp->camount);
 
     return 0;
 }
@@ -255,39 +222,59 @@
 
     free_filter_param(&unsharp->luma);
     free_filter_param(&unsharp->chroma);
-    av_opt_free(unsharp);
 }
 
-static int filter_frame(AVFilterLink *link, AVFilterBufferRef *in)
+static int filter_frame(AVFilterLink *link, AVFrame *in)
 {
     UnsharpContext *unsharp = link->dst->priv;
     AVFilterLink *outlink   = link->dst->outputs[0];
-    AVFilterBufferRef *out;
+    AVFrame *out;
     int cw = SHIFTUP(link->w, unsharp->hsub);
     int ch = SHIFTUP(link->h, unsharp->vsub);
 
-    out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     if (!out) {
-        avfilter_unref_bufferp(&in);
+        av_frame_free(&in);
         return AVERROR(ENOMEM);
     }
-    avfilter_copy_buffer_ref_props(out, in);
+    av_frame_copy_props(out, in);
 
     apply_unsharp(out->data[0], out->linesize[0], in->data[0], in->linesize[0], link->w, link->h, &unsharp->luma);
     apply_unsharp(out->data[1], out->linesize[1], in->data[1], in->linesize[1], cw,      ch,      &unsharp->chroma);
     apply_unsharp(out->data[2], out->linesize[2], in->data[2], in->linesize[2], cw,      ch,      &unsharp->chroma);
 
-    avfilter_unref_bufferp(&in);
+    av_frame_free(&in);
     return ff_filter_frame(outlink, out);
 }
 
+#define OFFSET(x) offsetof(UnsharpContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+#define MIN_SIZE 3
+#define MAX_SIZE 63
+static const AVOption unsharp_options[] = {
+    { "luma_msize_x",   "luma matrix horizontal size",   OFFSET(lmsize_x), AV_OPT_TYPE_INT,   { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
+    { "lx",             "luma matrix horizontal size",   OFFSET(lmsize_x), AV_OPT_TYPE_INT,   { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
+    { "luma_msize_y",   "luma matrix vertical size",     OFFSET(lmsize_y), AV_OPT_TYPE_INT,   { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
+    { "ly",             "luma matrix vertical size",     OFFSET(lmsize_y), AV_OPT_TYPE_INT,   { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
+    { "luma_amount",    "luma effect strength",          OFFSET(lamount),  AV_OPT_TYPE_FLOAT, { .dbl = 1 },       -2,        5, FLAGS },
+    { "la",             "luma effect strength",          OFFSET(lamount),  AV_OPT_TYPE_FLOAT, { .dbl = 1 },       -2,        5, FLAGS },
+    { "chroma_msize_x", "chroma matrix horizontal size", OFFSET(cmsize_x), AV_OPT_TYPE_INT,   { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
+    { "cx",             "chroma matrix horizontal size", OFFSET(cmsize_x), AV_OPT_TYPE_INT,   { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
+    { "chroma_msize_y", "chroma matrix vertical size",   OFFSET(cmsize_y), AV_OPT_TYPE_INT,   { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
+    { "cy",             "chroma matrix vertical size",   OFFSET(cmsize_y), AV_OPT_TYPE_INT,   { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
+    { "chroma_amount",  "chroma effect strength",        OFFSET(camount),  AV_OPT_TYPE_FLOAT, { .dbl = 0 },       -2,        5, FLAGS },
+    { "ca",             "chroma effect strength",        OFFSET(camount),  AV_OPT_TYPE_FLOAT, { .dbl = 0 },       -2,        5, FLAGS },
+    { NULL },
+};
+
+AVFILTER_DEFINE_CLASS(unsharp);
+
 static const AVFilterPad avfilter_vf_unsharp_inputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_VIDEO,
         .filter_frame = filter_frame,
         .config_props = config_props,
-        .min_perms    = AV_PERM_READ,
     },
     { NULL }
 };
@@ -305,14 +292,13 @@
     .description = NULL_IF_CONFIG_SMALL("Sharpen or blur the input video."),
 
     .priv_size = sizeof(UnsharpContext),
+    .priv_class = &unsharp_class,
 
     .init = init,
     .uninit = uninit,
     .query_formats = query_formats,
 
     .inputs    = avfilter_vf_unsharp_inputs,
-
     .outputs   = avfilter_vf_unsharp_outputs,
-
-    .priv_class = &unsharp_class,
+    .flags     = AVFILTER_FLAG_SUPPORT_TIMELINE,
 };
diff --git a/libavfilter/vf_vflip.c b/libavfilter/vf_vflip.c
index 6077789..28fa800 100644
--- a/libavfilter/vf_vflip.c
+++ b/libavfilter/vf_vflip.c
@@ -43,33 +43,29 @@
     return 0;
 }
 
-static AVFilterBufferRef *get_video_buffer(AVFilterLink *link, int perms,
-                                        int w, int h)
+static AVFrame *get_video_buffer(AVFilterLink *link, int w, int h)
 {
     FlipContext *flip = link->dst->priv;
-    AVFilterBufferRef *picref;
+    AVFrame *frame;
     int i;
 
-    if (!(perms & AV_PERM_NEG_LINESIZES))
-        return ff_default_get_video_buffer(link, perms, w, h);
-
-    picref = ff_get_video_buffer(link->dst->outputs[0], perms, w, h);
-    if (!picref)
+    frame = ff_get_video_buffer(link->dst->outputs[0], w, h);
+    if (!frame)
         return NULL;
 
     for (i = 0; i < 4; i ++) {
         int vsub = i == 1 || i == 2 ? flip->vsub : 0;
 
-        if (picref->data[i]) {
-            picref->data[i] += (((h + (1<<vsub)-1) >> vsub)-1) * picref->linesize[i];
-            picref->linesize[i] = -picref->linesize[i];
+        if (frame->data[i]) {
+            frame->data[i] += (((h + (1<<vsub) - 1) >> vsub) - 1) * frame->linesize[i];
+            frame->linesize[i] = -frame->linesize[i];
         }
     }
 
-    return picref;
+    return frame;
 }
 
-static int filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
+static int filter_frame(AVFilterLink *link, AVFrame *frame)
 {
     FlipContext *flip = link->dst->priv;
     int i;
diff --git a/libavfilter/vf_vidstabdetect.c b/libavfilter/vf_vidstabdetect.c
new file mode 100644
index 0000000..d96ca8e
--- /dev/null
+++ b/libavfilter/vf_vidstabdetect.c
@@ -0,0 +1,218 @@
+/*
+ * Copyright (c) 2013 Georg Martius <georg dot martius at web dot de>
+ *
+ * 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
+ */
+
+#define DEFAULT_RESULT_NAME     "transforms.trf"
+
+#include <vid.stab/libvidstab.h>
+
+#include "libavutil/common.h"
+#include "libavutil/opt.h"
+#include "libavutil/imgutils.h"
+#include "avfilter.h"
+#include "internal.h"
+
+#include "vidstabutils.h"
+
+typedef struct {
+    const AVClass *class;
+
+    VSMotionDetect md;
+    VSMotionDetectConfig conf;
+
+    char *result;
+    FILE *f;
+} StabData;
+
+
+#define OFFSET(x) offsetof(StabData, x)
+#define OFFSETC(x) (offsetof(StabData, conf)+offsetof(VSMotionDetectConfig, x))
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption vidstabdetect_options[] = {
+    {"result",      "path to the file used to write the transforms",                 OFFSET(result),             AV_OPT_TYPE_STRING, {.str = DEFAULT_RESULT_NAME}, .flags = FLAGS},
+    {"shakiness",   "how shaky is the video and how quick is the camera?"
+                    " 1: little (fast) 10: very strong/quick (slow)",                OFFSETC(shakiness),         AV_OPT_TYPE_INT,    {.i64 = 5},      1,  10, FLAGS},
+    {"accuracy",    "(>=shakiness) 1: low 15: high (slow)",                          OFFSETC(accuracy),          AV_OPT_TYPE_INT,    {.i64 = 9},      1,  15, FLAGS},
+    {"stepsize",    "region around minimum is scanned with 1 pixel resolution",      OFFSETC(stepSize),          AV_OPT_TYPE_INT,    {.i64 = 6},      1,  32, FLAGS},
+    {"mincontrast", "below this contrast a field is discarded (0-1)",                OFFSETC(contrastThreshold), AV_OPT_TYPE_DOUBLE, {.dbl = 0.25}, 0.0, 1.0, FLAGS},
+    {"show",        "0: draw nothing; 1,2: show fields and transforms",              OFFSETC(show),              AV_OPT_TYPE_INT,    {.i64 = 0},      0,   2, FLAGS},
+    {"tripod",      "virtual tripod mode (if >0): motion is compared to a reference"
+                    " reference frame (frame # is the value)",                       OFFSETC(virtualTripod),     AV_OPT_TYPE_INT,    {.i64 = 0}, 0, INT_MAX, FLAGS},
+    {NULL}
+};
+
+AVFILTER_DEFINE_CLASS(vidstabdetect);
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    StabData *sd = ctx->priv;
+    vs_set_mem_and_log_functions();
+    sd->class = &vidstabdetect_class;
+    av_log(ctx, AV_LOG_VERBOSE, "vidstabdetect filter: init %s\n", LIBVIDSTAB_VERSION);
+    return 0;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    StabData *sd = ctx->priv;
+    VSMotionDetect *md = &(sd->md);
+
+    if (sd->f) {
+        fclose(sd->f);
+        sd->f = NULL;
+    }
+
+    vsMotionDetectionCleanup(md);
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    // If you add something here also add it in vidstabutils.c
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
+        AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
+        AV_PIX_FMT_YUV440P,  AV_PIX_FMT_GRAY8,
+        AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, AV_PIX_FMT_RGBA,
+        AV_PIX_FMT_NONE
+    };
+
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+    return 0;
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    StabData *sd = ctx->priv;
+
+    VSMotionDetect* md = &(sd->md);
+    VSFrameInfo fi;
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+
+    vsFrameInfoInit(&fi, inlink->w, inlink->h, av_2_vs_pixel_format(ctx, inlink->format));
+    if (fi.bytesPerPixel != av_get_bits_per_pixel(desc)/8) {
+        av_log(ctx, AV_LOG_ERROR, "pixel-format error: wrong bits/per/pixel, please report a BUG");
+        return AVERROR(EINVAL);
+    }
+    if (fi.log2ChromaW != desc->log2_chroma_w) {
+        av_log(ctx, AV_LOG_ERROR, "pixel-format error: log2_chroma_w, please report a BUG");
+        return AVERROR(EINVAL);
+    }
+
+    if (fi.log2ChromaH != desc->log2_chroma_h) {
+        av_log(ctx, AV_LOG_ERROR, "pixel-format error: log2_chroma_h, please report a BUG");
+        return AVERROR(EINVAL);
+    }
+
+    // set values that are not initialized by the options
+    sd->conf.algo     = 1;
+    sd->conf.modName  = "vidstabdetect";
+    if (vsMotionDetectInit(md, &sd->conf, &fi) != VS_OK) {
+        av_log(ctx, AV_LOG_ERROR, "initialization of Motion Detection failed, please report a BUG");
+        return AVERROR(EINVAL);
+    }
+
+    vsMotionDetectGetConfig(&sd->conf, md);
+    av_log(ctx, AV_LOG_INFO, "Video stabilization settings (pass 1/2):\n");
+    av_log(ctx, AV_LOG_INFO, "     shakiness = %d\n", sd->conf.shakiness);
+    av_log(ctx, AV_LOG_INFO, "      accuracy = %d\n", sd->conf.accuracy);
+    av_log(ctx, AV_LOG_INFO, "      stepsize = %d\n", sd->conf.stepSize);
+    av_log(ctx, AV_LOG_INFO, "   mincontrast = %f\n", sd->conf.contrastThreshold);
+    av_log(ctx, AV_LOG_INFO, "          show = %d\n", sd->conf.show);
+    av_log(ctx, AV_LOG_INFO, "        result = %s\n", sd->result);
+
+    sd->f = fopen(sd->result, "w");
+    if (sd->f == NULL) {
+        av_log(ctx, AV_LOG_ERROR, "cannot open transform file %s\n", sd->result);
+        return AVERROR(EINVAL);
+    } else {
+        if (vsPrepareFile(md, sd->f) != VS_OK) {
+            av_log(ctx, AV_LOG_ERROR, "cannot write to transform file %s\n", sd->result);
+            return AVERROR(EINVAL);
+        }
+    }
+    return 0;
+}
+
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx = inlink->dst;
+    StabData *sd = ctx->priv;
+    VSMotionDetect *md = &(sd->md);
+    LocalMotions localmotions;
+
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+    VSFrame frame;
+    int plane;
+
+    if (sd->conf.show > 0 && !av_frame_is_writable(in))
+        av_frame_make_writable(in);
+
+    for (plane = 0; plane < md->fi.planes; plane++) {
+        frame.data[plane] = in->data[plane];
+        frame.linesize[plane] = in->linesize[plane];
+    }
+    if (vsMotionDetection(md, &localmotions, &frame) != VS_OK) {
+        av_log(ctx, AV_LOG_ERROR, "motion detection failed");
+        return AVERROR(AVERROR_EXTERNAL);
+    } else {
+        if (vsWriteToFile(md, sd->f, &localmotions) != VS_OK) {
+            av_log(ctx, AV_LOG_ERROR, "cannot write to transform file");
+            return AVERROR(errno);
+        }
+        vs_vector_del(&localmotions);
+    }
+
+    return ff_filter_frame(outlink, in);
+}
+
+static const AVFilterPad avfilter_vf_vidstabdetect_inputs[] = {
+    {
+        .name             = "default",
+        .type             = AVMEDIA_TYPE_VIDEO,
+        .filter_frame     = filter_frame,
+        .config_props     = config_input,
+    },
+    { NULL }
+};
+
+static const AVFilterPad avfilter_vf_vidstabdetect_outputs[] = {
+    {
+        .name             = "default",
+        .type             = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_vf_vidstabdetect = {
+    .name          = "vidstabdetect",
+    .description   = NULL_IF_CONFIG_SMALL("Extract relative transformations, "
+                                          "pass 1 of 2 for stabilization "
+                                          "(see vidstabtransform for pass 2)."),
+    .priv_size     = sizeof(StabData),
+    .init          = init,
+    .uninit        = uninit,
+    .query_formats = query_formats,
+
+    .inputs        = avfilter_vf_vidstabdetect_inputs,
+    .outputs       = avfilter_vf_vidstabdetect_outputs,
+    .priv_class    = &vidstabdetect_class,
+};
diff --git a/libavfilter/vf_vidstabtransform.c b/libavfilter/vf_vidstabtransform.c
new file mode 100644
index 0000000..eee3da7
--- /dev/null
+++ b/libavfilter/vf_vidstabtransform.c
@@ -0,0 +1,292 @@
+/*
+ * Copyright (c) 2013 Georg Martius <georg dot martius at web dot de>
+ *
+ * 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
+ */
+
+#define DEFAULT_INPUT_NAME     "transforms.trf"
+
+#include <vid.stab/libvidstab.h>
+
+#include "libavutil/common.h"
+#include "libavutil/opt.h"
+#include "libavutil/imgutils.h"
+#include "avfilter.h"
+#include "internal.h"
+
+#include "vidstabutils.h"
+
+typedef struct {
+    const AVClass *class;
+
+    VSTransformData td;
+    VSTransformConfig conf;
+
+    VSTransformations trans;    // transformations
+    char *input;                // name of transform file
+    int tripod;
+} TransformContext;
+
+#define OFFSET(x) offsetof(TransformContext, x)
+#define OFFSETC(x) (offsetof(TransformContext, conf)+offsetof(VSTransformConfig, x))
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption vidstabtransform_options[] = {
+    {"input",     "path to the file storing the transforms",                        OFFSET(input),
+                   AV_OPT_TYPE_STRING, {.str = DEFAULT_INPUT_NAME}, .flags = FLAGS },
+    {"smoothing", "number of frames*2 + 1 used for lowpass filtering",              OFFSETC(smoothing),
+                   AV_OPT_TYPE_INT,    {.i64 = 10},       1, 1000, FLAGS},
+    {"maxshift",  "maximal number of pixels to translate image",                    OFFSETC(maxShift),
+                   AV_OPT_TYPE_INT,    {.i64 = -1},      -1, 500,  FLAGS},
+    {"maxangle",  "maximal angle in rad to rotate image",                           OFFSETC(maxAngle),
+                   AV_OPT_TYPE_DOUBLE, {.dbl = -1.0},  -1.0, 3.14, FLAGS},
+    {"crop",      "set cropping mode",                                              OFFSETC(crop),
+                   AV_OPT_TYPE_INT,    {.i64 = 0},        0, 1,    FLAGS, "crop"},
+    {  "keep",    "keep border",                                                    0,
+                   AV_OPT_TYPE_CONST,  {.i64 = VSKeepBorder }, 0, 0, FLAGS, "crop"},
+    {  "black",   "black border",                                                   0,
+                   AV_OPT_TYPE_CONST,  {.i64 = VSCropBorder }, 0, 0, FLAGS, "crop"},
+    {"invert",    "1: invert transforms",                                           OFFSETC(invert),
+                   AV_OPT_TYPE_INT,    {.i64 = 0},        0, 1,    FLAGS},
+    {"relative",  "consider transforms as 0: absolute, 1: relative",                OFFSETC(relative),
+                   AV_OPT_TYPE_INT,    {.i64 = 1},        0, 1,    FLAGS},
+    {"zoom",      "percentage to zoom >0: zoom in, <0 zoom out",                    OFFSETC(zoom),
+                   AV_OPT_TYPE_DOUBLE, {.dbl = 0},        0, 100,  FLAGS},
+    {"optzoom",   "0: nothing, 1: determine optimal zoom (added to 'zoom')",        OFFSETC(optZoom),
+                   AV_OPT_TYPE_INT,    {.i64 = 1},        0, 1,    FLAGS},
+    {"interpol",  "type of interpolation",                                          OFFSETC(interpolType),
+                   AV_OPT_TYPE_INT,    {.i64 = 2},        0, 3,    FLAGS, "interpol"},
+    {  "no",      "no interpolation",                                               0,
+                   AV_OPT_TYPE_CONST,  {.i64 = VS_Zero  },  0, 0,  FLAGS, "interpol"},
+    {  "linear",  "linear (horizontal)",                                            0,
+                   AV_OPT_TYPE_CONST,  {.i64 = VS_Linear }, 0, 0,  FLAGS, "interpol"},
+    {  "bilinear","bi-linear",                                                      0,
+                   AV_OPT_TYPE_CONST,  {.i64 = VS_BiLinear},0, 0,  FLAGS, "interpol"},
+    {  "bicubic", "bi-cubic",                                                       0,
+                   AV_OPT_TYPE_CONST,  {.i64 = VS_BiCubic },0, 0,  FLAGS, "interpol"},
+    {"tripod",    "if 1: virtual tripod mode (equiv. to relative=0:smoothing=0)",   OFFSET(tripod),
+                   AV_OPT_TYPE_INT,    {.i64 = 0},        0, 1,    FLAGS},
+    {NULL}
+};
+
+AVFILTER_DEFINE_CLASS(vidstabtransform);
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    TransformContext *tc = ctx->priv;
+    vs_set_mem_and_log_functions();
+    tc->class = &vidstabtransform_class;
+    av_log(ctx, AV_LOG_VERBOSE, "vidstabtransform filter: init %s\n", LIBVIDSTAB_VERSION);
+    return 0;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    TransformContext *tc = ctx->priv;
+
+    vsTransformDataCleanup(&tc->td);
+    vsTransformationsCleanup(&tc->trans);
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    // If you add something here also add it in vidstabutils.c
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
+        AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA420P,
+        AV_PIX_FMT_YUV440P,  AV_PIX_FMT_GRAY8,
+        AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24, AV_PIX_FMT_RGBA,
+        AV_PIX_FMT_NONE
+    };
+
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+    return 0;
+}
+
+
+static int config_input(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    TransformContext *tc = ctx->priv;
+    FILE *f;
+
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+
+    VSTransformData *td = &(tc->td);
+
+    VSFrameInfo fi_src;
+    VSFrameInfo fi_dest;
+
+    if (!vsFrameInfoInit(&fi_src, inlink->w, inlink->h,
+                         av_2_vs_pixel_format(ctx, inlink->format)) ||
+        !vsFrameInfoInit(&fi_dest, inlink->w, inlink->h,
+                         av_2_vs_pixel_format(ctx, inlink->format))) {
+        av_log(ctx, AV_LOG_ERROR, "unknown pixel format: %i (%s)",
+               inlink->format, desc->name);
+        return AVERROR(EINVAL);
+    }
+
+    if (fi_src.bytesPerPixel != av_get_bits_per_pixel(desc)/8 ||
+        fi_src.log2ChromaW != desc->log2_chroma_w ||
+        fi_src.log2ChromaH != desc->log2_chroma_h) {
+        av_log(ctx, AV_LOG_ERROR, "pixel-format error: bpp %i<>%i  ",
+               fi_src.bytesPerPixel, av_get_bits_per_pixel(desc)/8);
+        av_log(ctx, AV_LOG_ERROR, "chroma_subsampl: w: %i<>%i  h: %i<>%i\n",
+               fi_src.log2ChromaW, desc->log2_chroma_w,
+               fi_src.log2ChromaH, desc->log2_chroma_h);
+        return AVERROR(EINVAL);
+    }
+
+    // set values that are not initializes by the options
+    tc->conf.modName = "vidstabtransform";
+    tc->conf.verbose =1;
+    if (tc->tripod) {
+        av_log(ctx, AV_LOG_INFO, "Virtual tripod mode: relative=0, smoothing=0");
+        tc->conf.relative  = 0;
+        tc->conf.smoothing = 0;
+    }
+
+    if (vsTransformDataInit(td, &tc->conf, &fi_src, &fi_dest) != VS_OK) {
+        av_log(ctx, AV_LOG_ERROR, "initialization of vid.stab transform failed, please report a BUG\n");
+        return AVERROR(EINVAL);
+    }
+
+    vsTransformGetConfig(&tc->conf, td);
+    av_log(ctx, AV_LOG_INFO, "Video transformation/stabilization settings (pass 2/2):\n");
+    av_log(ctx, AV_LOG_INFO, "    input     = %s\n", tc->input);
+    av_log(ctx, AV_LOG_INFO, "    smoothing = %d\n", tc->conf.smoothing);
+    av_log(ctx, AV_LOG_INFO, "    maxshift  = %d\n", tc->conf.maxShift);
+    av_log(ctx, AV_LOG_INFO, "    maxangle  = %f\n", tc->conf.maxAngle);
+    av_log(ctx, AV_LOG_INFO, "    crop      = %s\n", tc->conf.crop ? "Black" : "Keep");
+    av_log(ctx, AV_LOG_INFO, "    relative  = %s\n", tc->conf.relative ? "True": "False");
+    av_log(ctx, AV_LOG_INFO, "    invert    = %s\n", tc->conf.invert ? "True" : "False");
+    av_log(ctx, AV_LOG_INFO, "    zoom      = %f\n", tc->conf.zoom);
+    av_log(ctx, AV_LOG_INFO, "    optzoom   = %s\n", tc->conf.optZoom ? "On" : "Off");
+    av_log(ctx, AV_LOG_INFO, "    interpol  = %s\n", getInterpolationTypeName(tc->conf.interpolType));
+
+    f = fopen(tc->input, "r");
+    if (f == NULL) {
+        av_log(ctx, AV_LOG_ERROR, "cannot open input file %s\n", tc->input);
+        return AVERROR(errno);
+    } else {
+        VSManyLocalMotions mlms;
+        if (vsReadLocalMotionsFile(f, &mlms) == VS_OK) {
+            // calculate the actual transforms from the local motions
+            if (vsLocalmotions2TransformsSimple(td, &mlms, &tc->trans) != VS_OK) {
+                av_log(ctx, AV_LOG_ERROR, "calculating transformations failed\n");
+                return AVERROR(EINVAL);
+            }
+        } else { // try to read old format
+            if (!vsReadOldTransforms(td, f, &tc->trans)) { /* read input file */
+                av_log(ctx, AV_LOG_ERROR, "error parsing input file %s\n", tc->input);
+                return AVERROR(EINVAL);
+            }
+        }
+    }
+    fclose(f);
+
+    if (vsPreprocessTransforms(td, &tc->trans) != VS_OK ) {
+        av_log(ctx, AV_LOG_ERROR, "error while preprocessing transforms\n");
+        return AVERROR(EINVAL);
+    }
+
+    // TODO: add sharpening, so far the user needs to call the unsharp filter manually
+    return 0;
+}
+
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx = inlink->dst;
+    TransformContext *tc = ctx->priv;
+    VSTransformData* td = &(tc->td);
+
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+    int direct = 0;
+    AVFrame *out;
+    VSFrame inframe;
+    int plane;
+
+    if (av_frame_is_writable(in)) {
+        direct = 1;
+        out = in;
+    } else {
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+        if (!out) {
+            av_frame_free(&in);
+            return AVERROR(ENOMEM);
+        }
+        av_frame_copy_props(out, in);
+    }
+
+    for (plane = 0; plane < vsTransformGetSrcFrameInfo(td)->planes; plane++) {
+        inframe.data[plane] = in->data[plane];
+        inframe.linesize[plane] = in->linesize[plane];
+    }
+    if (direct) {
+        vsTransformPrepare(td, &inframe, &inframe);
+    } else { // separate frames
+        VSFrame outframe;
+        for (plane = 0; plane < vsTransformGetDestFrameInfo(td)->planes; plane++) {
+            outframe.data[plane] = out->data[plane];
+            outframe.linesize[plane] = out->linesize[plane];
+        }
+        vsTransformPrepare(td, &inframe, &outframe);
+    }
+
+    vsDoTransform(td, vsGetNextTransform(td, &tc->trans));
+
+    vsTransformFinish(td);
+
+    if (!direct)
+        av_frame_free(&in);
+
+    return ff_filter_frame(outlink, out);
+}
+
+static const AVFilterPad avfilter_vf_vidstabtransform_inputs[] = {
+    {
+        .name             = "default",
+        .type             = AVMEDIA_TYPE_VIDEO,
+        .filter_frame     = filter_frame,
+        .config_props     = config_input,
+    },
+    { NULL }
+};
+
+static const AVFilterPad avfilter_vf_vidstabtransform_outputs[] = {
+    {
+        .name             = "default",
+        .type             = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_vf_vidstabtransform = {
+    .name          = "vidstabtransform",
+    .description   = NULL_IF_CONFIG_SMALL("Transform the frames, "
+                                          "pass 2 of 2 for stabilization "
+                                          "(see vidstabdetect for pass 1)."),
+    .priv_size     = sizeof(TransformContext),
+    .init          = init,
+    .uninit        = uninit,
+    .query_formats = query_formats,
+
+    .inputs        = avfilter_vf_vidstabtransform_inputs,
+    .outputs       = avfilter_vf_vidstabtransform_outputs,
+    .priv_class    = &vidstabtransform_class,
+};
diff --git a/libavfilter/vf_yadif.c b/libavfilter/vf_yadif.c
index b7c2d80..5d8934b 100644
--- a/libavfilter/vf_yadif.c
+++ b/libavfilter/vf_yadif.c
@@ -31,17 +31,18 @@
 #undef NDEBUG
 #include <assert.h>
 
-#define PERM_RWP AV_PERM_WRITE | AV_PERM_PRESERVE | AV_PERM_REUSE
-
 #define CHECK(j)\
-    {   int score = FFABS(cur[mrefs + off_left + (j)] - cur[prefs + off_left - (j)])\
+    {   int score = FFABS(cur[mrefs - 1 + (j)] - cur[prefs - 1 - (j)])\
                   + FFABS(cur[mrefs  +(j)] - cur[prefs  -(j)])\
-                  + FFABS(cur[mrefs + off_right + (j)] - cur[prefs + off_right - (j)]);\
+                  + FFABS(cur[mrefs + 1 + (j)] - cur[prefs + 1 - (j)]);\
         if (score < spatial_score) {\
             spatial_score= score;\
             spatial_pred= (cur[mrefs  +(j)] + cur[prefs  -(j)])>>1;\
 
-#define FILTER(start, end) \
+/* The is_not_edge argument here controls when the code will enter a branch
+ * which reads up to and including x-3 and x+3. */
+
+#define FILTER(start, end, is_not_edge) \
     for (x = start;  x < end; x++) { \
         int c = cur[mrefs]; \
         int d = (prev2[0] + next2[0])>>1; \
@@ -51,12 +52,10 @@
         int temporal_diff2 =(FFABS(next[mrefs] - c) + FFABS(next[prefs] - e) )>>1; \
         int diff = FFMAX3(temporal_diff0 >> 1, temporal_diff1, temporal_diff2); \
         int spatial_pred = (c+e) >> 1; \
-        int off_right = (x < w - 1) ? 1 : -1;\
-        int off_left  = x ? -1 : 1;\
-        int spatial_score = FFABS(cur[mrefs + off_left]  - cur[prefs + off_left]) + FFABS(c-e) \
-                          + FFABS(cur[mrefs + off_right] - cur[prefs + off_right]) - 1; \
  \
-        if (x > 2 && x < w - 3) {\
+        if (is_not_edge) {\
+            int spatial_score = FFABS(cur[mrefs - 1] - cur[prefs - 1]) + FFABS(c-e) \
+                              + FFABS(cur[mrefs + 1] - cur[prefs + 1]) - 1; \
             CHECK(-1) CHECK(-2) }} }} \
             CHECK( 1) CHECK( 2) }} }} \
         }\
@@ -97,12 +96,15 @@
     uint8_t *prev2 = parity ? prev : cur ;
     uint8_t *next2 = parity ? cur  : next;
 
-    FILTER(0, w)
+    /* The function is called with the pointers already pointing to data[3] and
+     * with 6 subtracted from the width.  This allows the FILTER macro to be
+     * called so that it processes all the pixels normally.  A constant value of
+     * true for is_not_edge lets the compiler ignore the if statement. */
+    FILTER(0, w, 1)
 }
 
 static void filter_edges(void *dst1, void *prev1, void *cur1, void *next1,
-                         int w, int prefs, int mrefs, int parity, int mode,
-                         int l_edge)
+                         int w, int prefs, int mrefs, int parity, int mode)
 {
     uint8_t *dst  = dst1;
     uint8_t *prev = prev1;
@@ -112,7 +114,9 @@
     uint8_t *prev2 = parity ? prev : cur ;
     uint8_t *next2 = parity ? cur  : next;
 
-    FILTER(0, l_edge)
+    /* Only edge pixels need to be processed here.  A constant value of false
+     * for is_not_edge should let the compiler ignore the whole branch. */
+    FILTER(0, 3, 0)
 
     dst  = (uint8_t*)dst1  + w - 3;
     prev = (uint8_t*)prev1 + w - 3;
@@ -121,7 +125,7 @@
     prev2 = (uint8_t*)(parity ? prev : cur);
     next2 = (uint8_t*)(parity ? cur  : next);
 
-    FILTER(w - 3, w)
+    FILTER(w - 3, w, 0)
 }
 
 
@@ -140,12 +144,11 @@
     mrefs /= 2;
     prefs /= 2;
 
-    FILTER(0, w)
+    FILTER(0, w, 1)
 }
 
 static void filter_edges_16bit(void *dst1, void *prev1, void *cur1, void *next1,
-                               int w, int prefs, int mrefs, int parity, int mode,
-                               int l_edge)
+                               int w, int prefs, int mrefs, int parity, int mode)
 {
     uint16_t *dst  = dst1;
     uint16_t *prev = prev1;
@@ -154,8 +157,10 @@
     int x;
     uint16_t *prev2 = parity ? prev : cur ;
     uint16_t *next2 = parity ? cur  : next;
+    mrefs /= 2;
+    prefs /= 2;
 
-    FILTER(0, l_edge)
+    FILTER(0, 3, 0)
 
     dst   = (uint16_t*)dst1  + w - 3;
     prev  = (uint16_t*)prev1 + w - 3;
@@ -164,21 +169,21 @@
     prev2 = (uint16_t*)(parity ? prev : cur);
     next2 = (uint16_t*)(parity ? cur  : next);
 
-    FILTER(w - 3, w)
+    FILTER(w - 3, w, 0)
 }
 
-static void filter(AVFilterContext *ctx, AVFilterBufferRef *dstpic,
+static void filter(AVFilterContext *ctx, AVFrame *dstpic,
                    int parity, int tff)
 {
     YADIFContext *yadif = ctx->priv;
     int y, i;
 
     for (i = 0; i < yadif->csp->nb_components; i++) {
-        int w = dstpic->video->w;
-        int h = dstpic->video->h;
+        int w = dstpic->width;
+        int h = dstpic->height;
         int refs = yadif->cur->linesize[i];
         int df = (yadif->csp->comp[i].depth_minus1 + 8) / 8;
-        int l_edge, l_edge_pix;
+        int pix_3 = 3 * df;
 
         if (i == 1 || i == 2) {
         /* Why is this not part of the per-plane description thing? */
@@ -189,8 +194,6 @@
         /* filtering reads 3 pixels to the left/right; to avoid invalid reads,
          * we need to call the c variant which avoids this for border pixels
          */
-        l_edge     = yadif->req_align;
-        l_edge_pix = l_edge / df;
 
         for (y = 0; y < h; y++) {
             if ((y ^ parity) & 1) {
@@ -199,22 +202,15 @@
                 uint8_t *next = &yadif->next->data[i][y * refs];
                 uint8_t *dst  = &dstpic->data[i][y * dstpic->linesize[i]];
                 int     mode  = y == 1 || y + 2 == h ? 2 : yadif->mode;
-                if (yadif->req_align) {
-                    yadif->filter_line(dst + l_edge, prev + l_edge, cur + l_edge,
-                                       next + l_edge, w - l_edge_pix - 3,
-                                       y + 1 < h ? refs : -refs,
-                                       y ? -refs : refs,
-                                       parity ^ tff, mode);
-                    yadif->filter_edges(dst, prev, cur, next, w,
-                                         y + 1 < h ? refs : -refs,
-                                         y ? -refs : refs,
-                                         parity ^ tff, mode, l_edge_pix);
-                } else {
-                    yadif->filter_line(dst, prev, cur, next + l_edge, w,
-                                       y + 1 < h ? refs : -refs,
-                                       y ? -refs : refs,
-                                       parity ^ tff, mode);
-                }
+                yadif->filter_line(dst + pix_3, prev + pix_3, cur + pix_3,
+                                   next + pix_3, w - 6,
+                                   y + 1 < h ? refs : -refs,
+                                   y ? -refs : refs,
+                                   parity ^ tff, mode);
+                yadif->filter_edges(dst, prev, cur, next, w,
+                                    y + 1 < h ? refs : -refs,
+                                    y ? -refs : refs,
+                                    parity ^ tff, mode);
             } else {
                 memcpy(&dstpic->data[i][y * dstpic->linesize[i]],
                        &yadif->cur->data[i][y * refs], w * df);
@@ -232,19 +228,19 @@
     int tff, ret;
 
     if (yadif->parity == -1) {
-        tff = yadif->cur->video->interlaced ?
-              yadif->cur->video->top_field_first : 1;
+        tff = yadif->cur->interlaced_frame ?
+              yadif->cur->top_field_first : 1;
     } else {
         tff = yadif->parity ^ 1;
     }
 
     if (is_second) {
-        yadif->out = ff_get_video_buffer(link, PERM_RWP, link->w, link->h);
+        yadif->out = ff_get_video_buffer(link, link->w, link->h);
         if (!yadif->out)
             return AVERROR(ENOMEM);
 
-        avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
-        yadif->out->video->interlaced = 0;
+        av_frame_copy_props(yadif->out, yadif->cur);
+        yadif->out->interlaced_frame = 0;
     }
 
     filter(ctx, yadif->out, tff ^ !is_second, tff);
@@ -265,47 +261,46 @@
     return ret;
 }
 
-static int filter_frame(AVFilterLink *link, AVFilterBufferRef *picref)
+static int filter_frame(AVFilterLink *link, AVFrame *frame)
 {
     AVFilterContext *ctx = link->dst;
     YADIFContext *yadif = ctx->priv;
 
-    av_assert0(picref);
+    av_assert0(frame);
 
     if (yadif->frame_pending)
         return_frame(ctx, 1);
 
     if (yadif->prev)
-        avfilter_unref_buffer(yadif->prev);
+        av_frame_free(&yadif->prev);
     yadif->prev = yadif->cur;
     yadif->cur  = yadif->next;
-    yadif->next = picref;
+    yadif->next = frame;
 
     if (!yadif->cur)
         return 0;
 
-    if (yadif->deint && !yadif->cur->video->interlaced) {
-        yadif->out  = avfilter_ref_buffer(yadif->cur, ~AV_PERM_WRITE);
+    if (yadif->deint && !yadif->cur->interlaced_frame) {
+        yadif->out  = av_frame_clone(yadif->cur);
         if (!yadif->out)
             return AVERROR(ENOMEM);
 
-        avfilter_unref_bufferp(&yadif->prev);
+        av_frame_free(&yadif->prev);
         if (yadif->out->pts != AV_NOPTS_VALUE)
             yadif->out->pts *= 2;
         return ff_filter_frame(ctx->outputs[0], yadif->out);
     }
 
     if (!yadif->prev &&
-        !(yadif->prev = avfilter_ref_buffer(yadif->cur, ~AV_PERM_WRITE)))
+        !(yadif->prev = av_frame_clone(yadif->cur)))
         return AVERROR(ENOMEM);
 
-    yadif->out = ff_get_video_buffer(ctx->outputs[0], PERM_RWP,
-                                     link->w, link->h);
+    yadif->out = ff_get_video_buffer(ctx->outputs[0], link->w, link->h);
     if (!yadif->out)
         return AVERROR(ENOMEM);
 
-    avfilter_copy_buffer_ref_props(yadif->out, yadif->cur);
-    yadif->out->video->interlaced = 0;
+    av_frame_copy_props(yadif->out, yadif->cur);
+    yadif->out->interlaced_frame = 0;
 
     if (yadif->out->pts != AV_NOPTS_VALUE)
         yadif->out->pts *= 2;
@@ -332,7 +327,7 @@
         ret  = ff_request_frame(link->src->inputs[0]);
 
         if (ret == AVERROR_EOF && yadif->cur) {
-            AVFilterBufferRef *next = avfilter_ref_buffer(yadif->next, ~AV_PERM_WRITE);
+            AVFrame *next = av_frame_clone(yadif->next);
 
             if (!next)
                 return AVERROR(ENOMEM);
@@ -349,40 +344,13 @@
     return 0;
 }
 
-#define OFFSET(x) offsetof(YADIFContext, x)
-#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
-
-#define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
-
-static const AVOption yadif_options[] = {
-    { "mode",   "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, "mode"},
-    CONST("send_frame",           "send one frame for each frame",                                     YADIF_MODE_SEND_FRAME,           "mode"),
-    CONST("send_field",           "send one frame for each field",                                     YADIF_MODE_SEND_FIELD,           "mode"),
-    CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"),
-    CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"),
-
-    { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, "parity" },
-    CONST("tff",  "assume top field first",    YADIF_PARITY_TFF,  "parity"),
-    CONST("bff",  "assume bottom field first", YADIF_PARITY_BFF,  "parity"),
-    CONST("auto", "auto detect parity",        YADIF_PARITY_AUTO, "parity"),
-
-    { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, "deint" },
-    CONST("all",        "deinterlace all frames",                       YADIF_DEINT_ALL,         "deint"),
-    CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED,  "deint"),
-
-    {NULL},
-};
-
-AVFILTER_DEFINE_CLASS(yadif);
-
 static av_cold void uninit(AVFilterContext *ctx)
 {
     YADIFContext *yadif = ctx->priv;
 
-    avfilter_unref_bufferp(&yadif->prev);
-    avfilter_unref_bufferp(&yadif->cur );
-    avfilter_unref_bufferp(&yadif->next);
-    av_opt_free(yadif);
+    av_frame_free(&yadif->prev);
+    av_frame_free(&yadif->cur );
+    av_frame_free(&yadif->next);
 }
 
 static int query_formats(AVFilterContext *ctx)
@@ -426,24 +394,6 @@
     return 0;
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
-{
-    YADIFContext *yadif = ctx->priv;
-    static const char *shorthand[] = { "mode", "parity", "deint", NULL };
-    int ret;
-
-    yadif->class = &yadif_class;
-    av_opt_set_defaults(yadif);
-
-    if ((ret = av_opt_set_from_string(yadif, args, shorthand, "=", ":")) < 0)
-        return ret;
-
-    av_log(ctx, AV_LOG_VERBOSE, "mode:%d parity:%d deint:%d\n",
-           yadif->mode, yadif->parity, yadif->deint);
-
-    return 0;
-}
-
 static int config_props(AVFilterLink *link)
 {
     AVFilterContext *ctx = link->src;
@@ -469,20 +419,46 @@
     } else {
         s->filter_line  = filter_line_c;
         s->filter_edges = filter_edges;
-
-        if (ARCH_X86)
-            ff_yadif_init_x86(s);
     }
 
+    if (ARCH_X86)
+        ff_yadif_init_x86(s);
+
     return 0;
 }
 
+
+#define OFFSET(x) offsetof(YADIFContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+#define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
+
+static const AVOption yadif_options[] = {
+    { "mode",   "specify the interlacing mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=YADIF_MODE_SEND_FRAME}, 0, 3, FLAGS, "mode"},
+    CONST("send_frame",           "send one frame for each frame",                                     YADIF_MODE_SEND_FRAME,           "mode"),
+    CONST("send_field",           "send one frame for each field",                                     YADIF_MODE_SEND_FIELD,           "mode"),
+    CONST("send_frame_nospatial", "send one frame for each frame, but skip spatial interlacing check", YADIF_MODE_SEND_FRAME_NOSPATIAL, "mode"),
+    CONST("send_field_nospatial", "send one frame for each field, but skip spatial interlacing check", YADIF_MODE_SEND_FIELD_NOSPATIAL, "mode"),
+
+    { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=YADIF_PARITY_AUTO}, -1, 1, FLAGS, "parity" },
+    CONST("tff",  "assume top field first",    YADIF_PARITY_TFF,  "parity"),
+    CONST("bff",  "assume bottom field first", YADIF_PARITY_BFF,  "parity"),
+    CONST("auto", "auto detect parity",        YADIF_PARITY_AUTO, "parity"),
+
+    { "deint", "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=YADIF_DEINT_ALL}, 0, 1, FLAGS, "deint" },
+    CONST("all",        "deinterlace all frames",                       YADIF_DEINT_ALL,         "deint"),
+    CONST("interlaced", "only deinterlace frames marked as interlaced", YADIF_DEINT_INTERLACED,  "deint"),
+
+    {NULL},
+};
+
+AVFILTER_DEFINE_CLASS(yadif);
+
 static const AVFilterPad avfilter_vf_yadif_inputs[] = {
     {
         .name             = "default",
         .type             = AVMEDIA_TYPE_VIDEO,
         .filter_frame     = filter_frame,
-        .min_perms        = AV_PERM_PRESERVE,
     },
     { NULL }
 };
@@ -502,12 +478,10 @@
     .description   = NULL_IF_CONFIG_SMALL("Deinterlace the input image."),
 
     .priv_size     = sizeof(YADIFContext),
-    .init          = init,
+    .priv_class    = &yadif_class,
     .uninit        = uninit,
     .query_formats = query_formats,
 
     .inputs    = avfilter_vf_yadif_inputs,
     .outputs   = avfilter_vf_yadif_outputs,
-
-    .priv_class = &yadif_class,
 };
diff --git a/libavfilter/video.c b/libavfilter/video.c
index a493204..b274070 100644
--- a/libavfilter/video.c
+++ b/libavfilter/video.c
@@ -24,6 +24,7 @@
 #include <stdio.h>
 
 #include "libavutil/avassert.h"
+#include "libavutil/buffer.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/mem.h"
 
@@ -31,23 +32,21 @@
 #include "internal.h"
 #include "video.h"
 
-AVFilterBufferRef *ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
+AVFrame *ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
 {
-    return ff_get_video_buffer(link->dst->outputs[0], perms, w, h);
+    return ff_get_video_buffer(link->dst->outputs[0], w, h);
 }
 
-AVFilterBufferRef *ff_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
+/* TODO: set the buffer's priv member to a context structure for the whole
+ * filter chain.  This will allow for a buffer pool instead of the constant
+ * alloc & free cycle currently implemented. */
+AVFrame *ff_default_get_video_buffer(AVFilterLink *link, int w, int h)
 {
-    int linesize[4];
-    uint8_t *data[4];
-    int i;
-    AVFilterBufferRef *picref = NULL;
+    AVFrame *frame = av_frame_alloc();
+    int ret;
+
+#if 0 //POOL
     AVFilterPool *pool = link->pool;
-    int full_perms = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE |
-                     AV_PERM_REUSE | AV_PERM_REUSE2 | AV_PERM_ALIGN;
-
-    av_assert1(!(perms & ~(full_perms | AV_PERM_NEG_LINESIZES)));
-
     if (pool) {
         for (i = 0; i < POOL_SIZE; i++) {
             picref = pool->pic[i];
@@ -71,27 +70,30 @@
         pool = link->pool = av_mallocz(sizeof(AVFilterPool));
         pool->refcount = 1;
     }
-
-    // align: +2 is needed for swscaler, +16 to be SIMD-friendly
-    if ((i = av_image_alloc(data, linesize, w, h, link->format, 32)) < 0)
+#endif
+    if (!frame)
         return NULL;
 
-    picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
-                                                       full_perms, w, h, link->format);
-    if (!picref) {
-        av_free(data[0]);
-        return NULL;
-    }
+    frame->width  = w;
+    frame->height = h;
+    frame->format = link->format;
 
+    ret = av_frame_get_buffer(frame, 32);
+    if (ret < 0)
+        av_frame_free(&frame);
+
+#if 0 //POOL
     memset(data[0], 128, i);
 
     picref->buf->priv = pool;
     picref->buf->free = NULL;
     pool->refcount++;
+#endif
 
-    return picref;
+    return frame;
 }
 
+#if FF_API_AVFILTERBUFFER
 AVFilterBufferRef *
 avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
                                           int w, int h, enum AVPixelFormat format)
@@ -136,25 +138,20 @@
     av_free(pic);
     return NULL;
 }
+#endif
 
-AVFilterBufferRef *ff_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
+AVFrame *ff_get_video_buffer(AVFilterLink *link, int w, int h)
 {
-    AVFilterBufferRef *ret = NULL;
+    AVFrame *ret = NULL;
 
     av_unused char buf[16];
     FF_TPRINTF_START(NULL, get_video_buffer); ff_tlog_link(NULL, link, 0);
-    ff_tlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
 
     if (link->dstpad->get_video_buffer)
-        ret = link->dstpad->get_video_buffer(link, perms, w, h);
+        ret = link->dstpad->get_video_buffer(link, w, h);
 
     if (!ret)
-        ret = ff_default_get_video_buffer(link, perms, w, h);
-
-    if (ret)
-        ret->type = AVMEDIA_TYPE_VIDEO;
-
-    FF_TPRINTF_START(NULL, get_video_buffer); ff_tlog_link(NULL, link, 0); ff_tlog(NULL, " returning "); ff_tlog_ref(NULL, ret, 1);
+        ret = ff_default_get_video_buffer(link, w, h);
 
     return ret;
 }
diff --git a/libavfilter/video.h b/libavfilter/video.h
index a6af163..56c58d6 100644
--- a/libavfilter/video.h
+++ b/libavfilter/video.h
@@ -23,22 +23,19 @@
 
 #include "avfilter.h"
 
-AVFilterBufferRef *ff_default_get_video_buffer(AVFilterLink *link,
-                                               int perms, int w, int h);
-AVFilterBufferRef *ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h);
+AVFrame *ff_default_get_video_buffer(AVFilterLink *link, int w, int h);
+AVFrame *ff_null_get_video_buffer(AVFilterLink *link, int w, int h);
 
 /**
  * Request a picture buffer with a specific set of permissions.
  *
  * @param link  the output link to the filter from which the buffer will
  *              be requested
- * @param perms the required access permissions
  * @param w     the minimum width of the buffer to allocate
  * @param h     the minimum height of the buffer to allocate
  * @return      A reference to the buffer. This must be unreferenced with
  *              avfilter_unref_buffer when you are finished with it.
  */
-AVFilterBufferRef *ff_get_video_buffer(AVFilterLink *link, int perms,
-                                       int w, int h);
+AVFrame *ff_get_video_buffer(AVFilterLink *link, int w, int h);
 
 #endif /* AVFILTER_VIDEO_H */
diff --git a/libavfilter/vidstabutils.c b/libavfilter/vidstabutils.c
new file mode 100644
index 0000000..dd6486d
--- /dev/null
+++ b/libavfilter/vidstabutils.c
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2013 Georg Martius <georg dot martius at web dot de>
+ *
+ * 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 "vidstabutils.h"
+
+/** convert AV's pixelformat to vid.stab pixelformat */
+VSPixelFormat av_2_vs_pixel_format(AVFilterContext *ctx, enum AVPixelFormat pf)
+{
+    switch (pf) {
+    case AV_PIX_FMT_YUV420P:  return PF_YUV420P;
+    case AV_PIX_FMT_YUV422P:  return PF_YUV422P;
+    case AV_PIX_FMT_YUV444P:  return PF_YUV444P;
+    case AV_PIX_FMT_YUV410P:  return PF_YUV410P;
+    case AV_PIX_FMT_YUV411P:  return PF_YUV411P;
+    case AV_PIX_FMT_YUV440P:  return PF_YUV440P;
+    case AV_PIX_FMT_YUVA420P: return PF_YUVA420P;
+    case AV_PIX_FMT_GRAY8:    return PF_GRAY8;
+    case AV_PIX_FMT_RGB24:    return PF_RGB24;
+    case AV_PIX_FMT_BGR24:    return PF_BGR24;
+    case AV_PIX_FMT_RGBA:     return PF_RGBA;
+    default:
+        av_log(ctx, AV_LOG_ERROR, "cannot deal with pixel format %i\n", pf);
+        return PF_NONE;
+    }
+}
+
+/** struct to hold a valid context for logging from within vid.stab lib */
+typedef struct {
+    const AVClass *class;
+} VS2AVLogCtx;
+
+/** wrapper to log vs_log into av_log */
+static int vs_2_av_log_wrapper(int type, const char *tag, const char *format, ...)
+{
+    va_list ap;
+    VS2AVLogCtx ctx;
+    AVClass class = {
+      .class_name = tag,
+      .item_name  = av_default_item_name,
+      .option     = 0,
+      .version    = LIBAVUTIL_VERSION_INT,
+      .category   = AV_CLASS_CATEGORY_FILTER,
+    };
+    ctx.class = &class;
+    va_start(ap, format);
+    av_vlog(&ctx, type, format, ap);
+    va_end(ap);
+    return VS_OK;
+}
+
+/** sets the memory allocation function and logging constants to av versions */
+void vs_set_mem_and_log_functions(void)
+{
+    vs_malloc  = av_malloc;
+    vs_zalloc  = av_mallocz;
+    vs_realloc = av_realloc;
+    vs_free    = av_free;
+
+    VS_ERROR_TYPE = AV_LOG_ERROR;
+    VS_WARN_TYPE  = AV_LOG_WARNING;
+    VS_INFO_TYPE  = AV_LOG_INFO;
+    VS_MSG_TYPE   = AV_LOG_VERBOSE;
+
+    vs_log   = vs_2_av_log_wrapper;
+
+    VS_ERROR = 0;
+    VS_OK    = 1;
+}
diff --git a/libavfilter/vidstabutils.h b/libavfilter/vidstabutils.h
new file mode 100644
index 0000000..f1c20e6
--- /dev/null
+++ b/libavfilter/vidstabutils.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2013 Georg Martius <georg dot martius at web dot de>
+ *
+ * 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 AVFILTER_VIDSTABUTILS_H
+#define AVFILTER_VIDSTABUTILS_H
+
+#include <vid.stab/libvidstab.h>
+
+#include "avfilter.h"
+
+/* ** some conversions from avlib to vid.stab constants and functions *** */
+
+/** converts the pixelformat of avlib into the one of the vid.stab library */
+VSPixelFormat av_2_vs_pixel_format(AVFilterContext *ctx, enum AVPixelFormat pf);
+
+/** sets the memory allocation function and logging constants to av versions */
+void vs_set_mem_and_log_functions(void);
+
+#endif
diff --git a/libavfilter/vsink_nullsink.c b/libavfilter/vsink_nullsink.c
index a37d346..d498aab 100644
--- a/libavfilter/vsink_nullsink.c
+++ b/libavfilter/vsink_nullsink.c
@@ -20,9 +20,9 @@
 #include "internal.h"
 #include "libavutil/internal.h"
 
-static int filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
+static int filter_frame(AVFilterLink *link, AVFrame *frame)
 {
-    avfilter_unref_bufferp(&frame);
+    av_frame_free(&frame);
     return 0;
 }
 
diff --git a/libavfilter/vsrc_cellauto.c b/libavfilter/vsrc_cellauto.c
index 06d9f9a..a51cda3 100644
--- a/libavfilter/vsrc_cellauto.c
+++ b/libavfilter/vsrc_cellauto.c
@@ -30,6 +30,7 @@
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/random_seed.h"
+#include "libavutil/avstring.h"
 #include "avfilter.h"
 #include "internal.h"
 #include "formats.h"
@@ -46,8 +47,7 @@
     int buf_prev_row_idx, buf_row_idx;
     uint8_t rule;
     uint64_t pts;
-    AVRational time_base;
-    char *rate;                 ///< video frame rate
+    AVRational frame_rate;
     double   random_fill_ratio;
     uint32_t random_seed;
     int stitch, scroll, start_full;
@@ -64,8 +64,8 @@
     { "f",        "read initial pattern from file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
     { "pattern",  "set initial pattern", OFFSET(pattern), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
     { "p",        "set initial pattern", OFFSET(pattern), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
-    { "rate",     "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
-    { "r",        "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
+    { "rate",     "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
+    { "r",        "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
     { "size",     "set video size", OFFSET(w),    AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS },
     { "s",        "set video size", OFFSET(w),    AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS },
     { "rule",     "set rule",       OFFSET(rule), AV_OPT_TYPE_INT,    {.i64 = 110},  0, 255, FLAGS },
@@ -133,7 +133,7 @@
         if (*p == '\n' || !*p)
             break;
         else
-            cellauto->buf[i] = !!isgraph(*(p++));
+            cellauto->buf[i] = !!av_isgraph(*(p++));
     }
 
     return 0;
@@ -159,29 +159,14 @@
     return init_pattern_from_string(ctx);
 }
 
-static int init(AVFilterContext *ctx, const char *args)
+static int init(AVFilterContext *ctx)
 {
     CellAutoContext *cellauto = ctx->priv;
-    AVRational frame_rate;
     int ret;
 
-    cellauto->class = &cellauto_class;
-    av_opt_set_defaults(cellauto);
-
-    if ((ret = av_set_options_string(cellauto, args, "=", ":")) < 0)
-        return ret;
-
-    if ((ret = av_parse_video_rate(&frame_rate, cellauto->rate)) < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", cellauto->rate);
-        return AVERROR(EINVAL);
-    }
-
     if (!cellauto->w && !cellauto->filename && !cellauto->pattern)
         av_opt_set(cellauto, "size", "320x518", 0);
 
-    cellauto->time_base.num = frame_rate.den;
-    cellauto->time_base.den = frame_rate.num;
-
     if (cellauto->filename && cellauto->pattern) {
         av_log(ctx, AV_LOG_ERROR, "Only one of the filename or pattern options can be used\n");
         return AVERROR(EINVAL);
@@ -214,7 +199,7 @@
 
     av_log(ctx, AV_LOG_VERBOSE,
            "s:%dx%d r:%d/%d rule:%d stitch:%d scroll:%d full:%d seed:%u\n",
-           cellauto->w, cellauto->h, frame_rate.num, frame_rate.den,
+           cellauto->w, cellauto->h, cellauto->frame_rate.num, cellauto->frame_rate.den,
            cellauto->rule, cellauto->stitch, cellauto->scroll, cellauto->start_full,
            cellauto->random_seed);
     return 0;
@@ -235,7 +220,7 @@
 
     outlink->w = cellauto->w;
     outlink->h = cellauto->h;
-    outlink->time_base = cellauto->time_base;
+    outlink->time_base = av_inv_q(cellauto->frame_rate);
 
     return 0;
 }
@@ -271,7 +256,7 @@
     cellauto->generation++;
 }
 
-static void fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
+static void fill_picture(AVFilterContext *ctx, AVFrame *picref)
 {
     CellAutoContext *cellauto = ctx->priv;
     int i, j, k, row_idx = 0;
@@ -302,9 +287,10 @@
 static int request_frame(AVFilterLink *outlink)
 {
     CellAutoContext *cellauto = outlink->src->priv;
-    AVFilterBufferRef *picref =
-        ff_get_video_buffer(outlink, AV_PERM_WRITE, cellauto->w, cellauto->h);
-    picref->video->sample_aspect_ratio = (AVRational) {1, 1};
+    AVFrame *picref = ff_get_video_buffer(outlink, cellauto->w, cellauto->h);
+    if (!picref)
+        return AVERROR(ENOMEM);
+    picref->sample_aspect_ratio = (AVRational) {1, 1};
     if (cellauto->generation == 0 && cellauto->start_full) {
         int i;
         for (i = 0; i < cellauto->h-1; i++)
@@ -314,14 +300,11 @@
     evolve(outlink->src);
 
     picref->pts = cellauto->pts++;
-    picref->pos = -1;
 
 #ifdef DEBUG
     show_cellauto_row(outlink->src);
 #endif
-    ff_filter_frame(outlink, picref);
-
-    return 0;
+    return ff_filter_frame(outlink, picref);
 }
 
 static int query_formats(AVFilterContext *ctx)
diff --git a/libavfilter/vsrc_life.c b/libavfilter/vsrc_life.c
index d8b8355..85f0989 100644
--- a/libavfilter/vsrc_life.c
+++ b/libavfilter/vsrc_life.c
@@ -31,6 +31,7 @@
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/random_seed.h"
+#include "libavutil/avstring.h"
 #include "avfilter.h"
 #include "internal.h"
 #include "formats.h"
@@ -59,8 +60,7 @@
     uint16_t stay_rule;         ///< encode the behavior for filled cells
     uint16_t born_rule;         ///< encode the behavior for empty cells
     uint64_t pts;
-    AVRational time_base;
-    char *rate;                 ///< video frame rate
+    AVRational frame_rate;
     double   random_fill_ratio;
     uint32_t random_seed;
     int stitch;
@@ -72,7 +72,7 @@
     uint8_t death_color[4];
     uint8_t  mold_color[4];
     AVLFG lfg;
-    void (*draw)(AVFilterContext*, AVFilterBufferRef*);
+    void (*draw)(AVFilterContext*, AVFrame*);
 } LifeContext;
 
 #define ALIVE_CELL 0xFF
@@ -84,8 +84,8 @@
     { "f",        "set source file",  OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
     { "size",     "set video size",   OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS },
     { "s",        "set video size",   OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS },
-    { "rate",     "set video rate",   OFFSET(rate),     AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
-    { "r",        "set video rate",   OFFSET(rate),     AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
+    { "rate",     "set video rate",   OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
+    { "r",        "set video rate",   OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
     { "rule",     "set rule",         OFFSET(rule_str), AV_OPT_TYPE_STRING, {.str = "B3/S23"}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "random_fill_ratio", "set fill ratio for filling initial grid randomly", OFFSET(random_fill_ratio), AV_OPT_TYPE_DOUBLE, {.dbl=1/M_PHI}, 0, 1, FLAGS },
     { "ratio",             "set fill ratio for filling initial grid randomly", OFFSET(random_fill_ratio), AV_OPT_TYPE_DOUBLE, {.dbl=1/M_PHI}, 0, 1, FLAGS },
@@ -212,7 +212,7 @@
             if (*p == '\n') {
                 p++; break;
             } else
-                life->buf[0][i*life->w + j] = isgraph(*(p++)) ? ALIVE_CELL : 0;
+                life->buf[0][i*life->w + j] = av_isgraph(*(p++)) ? ALIVE_CELL : 0;
         }
     }
     life->buf_idx = 0;
@@ -220,24 +220,11 @@
     return 0;
 }
 
-static int init(AVFilterContext *ctx, const char *args)
+static int init(AVFilterContext *ctx)
 {
     LifeContext *life = ctx->priv;
-    AVRational frame_rate;
     int ret;
 
-    life->class = &life_class;
-    av_opt_set_defaults(life);
-
-    if ((ret = av_set_options_string(life, args, "=", ":")) < 0)
-        return ret;
-
-    if ((ret = av_parse_video_rate(&frame_rate, life->rate)) < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", life->rate);
-        return AVERROR(EINVAL);
-    }
-    av_freep(&life->rate);
-
     if (!life->w && !life->filename)
         av_opt_set(life, "size", "320x240", 0);
 
@@ -261,9 +248,6 @@
         av_log(ctx, AV_LOG_WARNING,
                "Mold color is set while mold isn't, ignoring the color.\n");
 
-    life->time_base.num = frame_rate.den;
-    life->time_base.den = frame_rate.num;
-
     if (!life->filename) {
         /* fill the grid randomly */
         int i;
@@ -292,7 +276,7 @@
 
     av_log(ctx, AV_LOG_VERBOSE,
            "s:%dx%d r:%d/%d rule:%s stay_rule:%d born_rule:%d stitch:%d seed:%u\n",
-           life->w, life->h, frame_rate.num, frame_rate.den,
+           life->w, life->h, life->frame_rate.num, life->frame_rate.den,
            life->rule_str, life->stay_rule, life->born_rule, life->stitch,
            life->random_seed);
     return 0;
@@ -314,7 +298,7 @@
 
     outlink->w = life->w;
     outlink->h = life->h;
-    outlink->time_base = life->time_base;
+    outlink->time_base = av_inv_q(life->frame_rate);
 
     return 0;
 }
@@ -374,7 +358,7 @@
     life->buf_idx = !life->buf_idx;
 }
 
-static void fill_picture_monoblack(AVFilterContext *ctx, AVFilterBufferRef *picref)
+static void fill_picture_monoblack(AVFilterContext *ctx, AVFrame *picref)
 {
     LifeContext *life = ctx->priv;
     uint8_t *buf = life->buf[life->buf_idx];
@@ -399,7 +383,7 @@
 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
 
-static void fill_picture_rgb(AVFilterContext *ctx, AVFilterBufferRef *picref)
+static void fill_picture_rgb(AVFilterContext *ctx, AVFrame *picref)
 {
     LifeContext *life = ctx->priv;
     uint8_t *buf = life->buf[life->buf_idx];
@@ -429,19 +413,18 @@
 static int request_frame(AVFilterLink *outlink)
 {
     LifeContext *life = outlink->src->priv;
-    AVFilterBufferRef *picref = ff_get_video_buffer(outlink, AV_PERM_WRITE, life->w, life->h);
-    picref->video->sample_aspect_ratio = (AVRational) {1, 1};
+    AVFrame *picref = ff_get_video_buffer(outlink, life->w, life->h);
+    if (!picref)
+        return AVERROR(ENOMEM);
+    picref->sample_aspect_ratio = (AVRational) {1, 1};
     picref->pts = life->pts++;
-    picref->pos = -1;
 
     life->draw(outlink->src, picref);
     evolve(outlink->src);
 #ifdef DEBUG
     show_life_grid(outlink->src);
 #endif
-    ff_filter_frame(outlink, picref);
-
-    return 0;
+    return ff_filter_frame(outlink, picref);
 }
 
 static int query_formats(AVFilterContext *ctx)
diff --git a/libavfilter/vsrc_mandelbrot.c b/libavfilter/vsrc_mandelbrot.c
index 1244edf..2633c39 100644
--- a/libavfilter/vsrc_mandelbrot.c
+++ b/libavfilter/vsrc_mandelbrot.c
@@ -41,6 +41,8 @@
 enum Outer{
     ITERATION_COUNT,
     NORMALIZED_ITERATION_COUNT,
+    WHITE,
+    OUTZ,
 };
 
 enum Inner{
@@ -58,9 +60,8 @@
 typedef struct {
     const AVClass *class;
     int w, h;
-    AVRational time_base;
+    AVRational frame_rate;
     uint64_t pts;
-    char *rate;
     int maxiter;
     double start_x;
     double start_y;
@@ -76,6 +77,10 @@
     Point *next_cache;
     double (*zyklus)[2];
     uint32_t dither;
+
+    double morphxf;
+    double morphyf;
+    double morphamp;
 } MBContext;
 
 #define OFFSET(x) offsetof(MBContext, x)
@@ -84,8 +89,8 @@
 static const AVOption mandelbrot_options[] = {
     {"size",        "set frame size",                OFFSET(w),       AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"},  CHAR_MIN, CHAR_MAX, FLAGS },
     {"s",           "set frame size",                OFFSET(w),       AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"},  CHAR_MIN, CHAR_MAX, FLAGS },
-    {"rate",        "set frame rate",                OFFSET(rate),    AV_OPT_TYPE_STRING,     {.str="25"},  CHAR_MIN, CHAR_MAX, FLAGS },
-    {"r",           "set frame rate",                OFFSET(rate),    AV_OPT_TYPE_STRING,     {.str="25"},  CHAR_MIN, CHAR_MAX, FLAGS },
+    {"rate",        "set frame rate",                OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"},  CHAR_MIN, CHAR_MAX, FLAGS },
+    {"r",           "set frame rate",                OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"},  CHAR_MIN, CHAR_MAX, FLAGS },
     {"maxiter",     "set max iterations number",     OFFSET(maxiter), AV_OPT_TYPE_INT,        {.i64=7189},  1,        INT_MAX, FLAGS },
     {"start_x",     "set the initial x position",    OFFSET(start_x), AV_OPT_TYPE_DOUBLE,     {.dbl=-0.743643887037158704752191506114774}, -100, 100, FLAGS },
     {"start_y",     "set the initial y position",    OFFSET(start_y), AV_OPT_TYPE_DOUBLE,     {.dbl=-0.131825904205311970493132056385139}, -100, 100, FLAGS },
@@ -93,10 +98,15 @@
     {"end_scale",   "set the terminal scale value",  OFFSET(end_scale), AV_OPT_TYPE_DOUBLE,   {.dbl=0.3},  0, FLT_MAX, FLAGS },
     {"end_pts",     "set the terminal pts value",    OFFSET(end_pts), AV_OPT_TYPE_DOUBLE,     {.dbl=400},  0, INT64_MAX, FLAGS },
     {"bailout",     "set the bailout value",         OFFSET(bailout), AV_OPT_TYPE_DOUBLE,     {.dbl=10},   0, FLT_MAX, FLAGS },
+    {"morphxf",     "set morph x frequency",         OFFSET(morphxf), AV_OPT_TYPE_DOUBLE,     {.dbl=0.01},   -FLT_MAX, FLT_MAX, FLAGS },
+    {"morphyf",     "set morph y frequency",         OFFSET(morphyf), AV_OPT_TYPE_DOUBLE,     {.dbl=0.0123}, -FLT_MAX, FLT_MAX, FLAGS },
+    {"morphamp",    "set morph amplitude",           OFFSET(morphamp), AV_OPT_TYPE_DOUBLE,    {.dbl=0},      -FLT_MAX, FLT_MAX, FLAGS },
 
     {"outer",       "set outer coloring mode",       OFFSET(outer), AV_OPT_TYPE_INT, {.i64=NORMALIZED_ITERATION_COUNT}, 0, INT_MAX, FLAGS, "outer" },
     {"iteration_count", "set iteration count mode",  0, AV_OPT_TYPE_CONST, {.i64=ITERATION_COUNT}, INT_MIN, INT_MAX, FLAGS, "outer" },
     {"normalized_iteration_count", "set normalized iteration count mode",   0, AV_OPT_TYPE_CONST, {.i64=NORMALIZED_ITERATION_COUNT}, INT_MIN, INT_MAX, FLAGS, "outer" },
+    {"white", "set white mode",                      0, AV_OPT_TYPE_CONST, {.i64=WHITE}, INT_MIN, INT_MAX, FLAGS, "outer" },
+    {"outz",        "set outz mode",                 0, AV_OPT_TYPE_CONST, {.i64=OUTZ}, INT_MIN, INT_MAX, FLAGS, "outer" },
 
     {"inner",       "set inner coloring mode",       OFFSET(inner), AV_OPT_TYPE_INT, {.i64=MINCOL}, 0, INT_MAX, FLAGS, "inner" },
     {"black",       "set black mode",                0, AV_OPT_TYPE_CONST, {.i64=BLACK}, INT_MIN, INT_MAX, FLAGS, "inner"},
@@ -109,29 +119,15 @@
 
 AVFILTER_DEFINE_CLASS(mandelbrot);
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     MBContext *mb = ctx->priv;
-    AVRational rate_q;
-    int err;
 
-    mb->class = &mandelbrot_class;
-    av_opt_set_defaults(mb);
-
-    if ((err = (av_set_options_string(mb, args, "=", ":"))) < 0)
-        return err;
     mb->bailout *= mb->bailout;
 
     mb->start_scale /=mb->h;
     mb->end_scale /=mb->h;
 
-    if (av_parse_video_rate(&rate_q, mb->rate) < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", mb->rate);
-        return AVERROR(EINVAL);
-    }
-    mb->time_base.num = rate_q.den;
-    mb->time_base.den = rate_q.num;
-
     mb->cache_allocated = mb->w * mb->h * 3;
     mb->cache_used = 0;
     mb->point_cache= av_malloc(sizeof(*mb->point_cache)*mb->cache_allocated);
@@ -145,7 +141,6 @@
 {
     MBContext *mb = ctx->priv;
 
-    av_freep(&mb->rate);
     av_freep(&mb->point_cache);
     av_freep(&mb-> next_cache);
     av_freep(&mb->zyklus);
@@ -172,13 +167,15 @@
 
     inlink->w = mb->w;
     inlink->h = mb->h;
-    inlink->time_base = mb->time_base;
+    inlink->time_base = av_inv_q(mb->frame_rate);
 
     return 0;
 }
 
 static void fill_from_cache(AVFilterContext *ctx, uint32_t *color, int *in_cidx, int *out_cidx, double py, double scale){
     MBContext *mb = ctx->priv;
+    if(mb->morphamp)
+        return;
     for(; *in_cidx < mb->cache_used; (*in_cidx)++){
         Point *p= &mb->point_cache[*in_cidx];
         int x;
@@ -276,13 +273,18 @@
 
             if(color[x + y*linesize] & 0xFF000000)
                 continue;
-            if(interpol(mb, color, x, y, linesize)){
-                if(next_cidx < mb->cache_allocated){
-                    mb->next_cache[next_cidx  ].p[0]= cr;
-                    mb->next_cache[next_cidx  ].p[1]= ci;
-                    mb->next_cache[next_cidx++].val = color[x + y*linesize];
+            if(!mb->morphamp){
+                if(interpol(mb, color, x, y, linesize)){
+                    if(next_cidx < mb->cache_allocated){
+                        mb->next_cache[next_cidx  ].p[0]= cr;
+                        mb->next_cache[next_cidx  ].p[1]= ci;
+                        mb->next_cache[next_cidx++].val = color[x + y*linesize];
+                    }
+                    continue;
                 }
-                continue;
+            }else{
+                zr += cos(pts * mb->morphxf) * mb->morphamp;
+                zi += sin(pts * mb->morphyf) * mb->morphamp;
             }
 
             use_zyklus= (x==0 || mb->inner!=BLACK ||color[x-1 + y*linesize] == 0xFF000000);
@@ -328,10 +330,22 @@
                         zi= mb->zyklus[i][1];
                         if(zr*zr + zi*zi > mb->bailout){
                             switch(mb->outer){
-                            case            ITERATION_COUNT: zr = i; break;
-                            case NORMALIZED_ITERATION_COUNT: zr= i + log2(log(mb->bailout) / log(zr*zr + zi*zi)); break;
+                            case            ITERATION_COUNT:
+                                zr = i;
+                                c = lrintf((sin(zr)+1)*127) + lrintf((sin(zr/1.234)+1)*127)*256*256 + lrintf((sin(zr/100)+1)*127)*256;
+                                break;
+                            case NORMALIZED_ITERATION_COUNT:
+                                zr = i + log2(log(mb->bailout) / log(zr*zr + zi*zi));
+                                c = lrintf((sin(zr)+1)*127) + lrintf((sin(zr/1.234)+1)*127)*256*256 + lrintf((sin(zr/100)+1)*127)*256;
+                                break;
+                            case                      WHITE:
+                                c = 0xFFFFFF;
+                                break;
+                            case                      OUTZ:
+                                zr /= mb->bailout;
+                                zi /= mb->bailout;
+                                c = (((int)(zr*128+128))&0xFF)*256 + (((int)(zi*128+128))&0xFF);
                             }
-                            c= lrintf((sin(zr)+1)*127) + lrintf((sin(zr/1.234)+1)*127)*256*256 + lrintf((sin(zr/100)+1)*127)*256;
                             break;
                         }
                     }
@@ -382,15 +396,15 @@
 static int request_frame(AVFilterLink *link)
 {
     MBContext *mb = link->src->priv;
-    AVFilterBufferRef *picref = ff_get_video_buffer(link, AV_PERM_WRITE, mb->w, mb->h);
-    picref->video->sample_aspect_ratio = (AVRational) {1, 1};
+    AVFrame *picref = ff_get_video_buffer(link, mb->w, mb->h);
+    if (!picref)
+        return AVERROR(ENOMEM);
+
+    picref->sample_aspect_ratio = (AVRational) {1, 1};
     picref->pts = mb->pts++;
-    picref->pos = -1;
 
     draw_mandelbrot(link->src, (uint32_t*)picref->data[0], picref->linesize[0]/4, picref->pts);
-    ff_filter_frame(link, picref);
-
-    return 0;
+    return ff_filter_frame(link, picref);
 }
 
 static const AVFilterPad mandelbrot_outputs[] = {
diff --git a/libavfilter/vsrc_mptestsrc.c b/libavfilter/vsrc_mptestsrc.c
index d526ee2..e931c76 100644
--- a/libavfilter/vsrc_mptestsrc.c
+++ b/libavfilter/vsrc_mptestsrc.c
@@ -53,35 +53,33 @@
 typedef struct MPTestContext {
     const AVClass *class;
     unsigned int frame_nb;
-    AVRational time_base;
-    int64_t pts, max_pts;
+    AVRational frame_rate;
+    int64_t pts, max_pts, duration;
     int hsub, vsub;
-    char *size, *rate, *duration;
     enum test_type test;
 } MPTestContext;
 
 #define OFFSET(x) offsetof(MPTestContext, x)
 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 static const AVOption mptestsrc_options[]= {
-    { "rate",     "set video rate",     OFFSET(rate),     AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
-    { "r",        "set video rate",     OFFSET(rate),     AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
-    { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
-    { "d",        "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
+    { "rate",     "set video rate",     OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
+    { "r",        "set video rate",     OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
+    { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
+    { "d",        "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
 
     { "test", "set test to perform", OFFSET(test),  AV_OPT_TYPE_INT,   {.i64=TEST_ALL}, 0, INT_MAX, FLAGS, "test" },
     { "t",    "set test to perform", OFFSET(test),  AV_OPT_TYPE_INT,   {.i64=TEST_ALL}, 0, INT_MAX, FLAGS, "test" },
-    { "dc_luma",     "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_DC_LUMA},     INT_MIN, INT_MAX, FLAGS, "test" },
-    { "dc_chroma",   "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_DC_CHROMA},   INT_MIN, INT_MAX, FLAGS, "test" },
-    { "freq_luma",   "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_FREQ_LUMA},   INT_MIN, INT_MAX, FLAGS, "test" },
-    { "freq_chroma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_FREQ_CHROMA}, INT_MIN, INT_MAX, FLAGS, "test" },
-    { "amp_luma",    "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_AMP_LUMA},    INT_MIN, INT_MAX, FLAGS, "test" },
-    { "amp_chroma",  "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_AMP_CHROMA},  INT_MIN, INT_MAX, FLAGS, "test" },
-    { "cbp",         "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_CBP},         INT_MIN, INT_MAX, FLAGS, "test" },
-    { "mv",          "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_MV},          INT_MIN, INT_MAX, FLAGS, "test" },
-    { "ring1",       "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_RING1},       INT_MIN, INT_MAX, FLAGS, "test" },
-    { "ring2",       "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_RING2},       INT_MIN, INT_MAX, FLAGS, "test" },
-    { "all",         "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_ALL},         INT_MIN, INT_MAX, FLAGS, "test" },
-
+        { "dc_luma",     "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_DC_LUMA},     INT_MIN, INT_MAX, FLAGS, "test" },
+        { "dc_chroma",   "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_DC_CHROMA},   INT_MIN, INT_MAX, FLAGS, "test" },
+        { "freq_luma",   "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_FREQ_LUMA},   INT_MIN, INT_MAX, FLAGS, "test" },
+        { "freq_chroma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_FREQ_CHROMA}, INT_MIN, INT_MAX, FLAGS, "test" },
+        { "amp_luma",    "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_AMP_LUMA},    INT_MIN, INT_MAX, FLAGS, "test" },
+        { "amp_chroma",  "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_AMP_CHROMA},  INT_MIN, INT_MAX, FLAGS, "test" },
+        { "cbp",         "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_CBP},         INT_MIN, INT_MAX, FLAGS, "test" },
+        { "mv",          "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_MV},          INT_MIN, INT_MAX, FLAGS, "test" },
+        { "ring1",       "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_RING1},       INT_MIN, INT_MAX, FLAGS, "test" },
+        { "ring2",       "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_RING2},       INT_MIN, INT_MAX, FLAGS, "test" },
+        { "all",         "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_ALL},         INT_MIN, INT_MAX, FLAGS, "test" },
     { NULL },
 };
 
@@ -256,39 +254,18 @@
     }
 }
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static av_cold int init(AVFilterContext *ctx)
 {
     MPTestContext *test = ctx->priv;
-    AVRational frame_rate_q;
-    int64_t duration = -1;
-    int ret;
 
-    test->class = &mptestsrc_class;
-    av_opt_set_defaults(test);
-
-    if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0)
-        return ret;
-
-    if ((ret = av_parse_video_rate(&frame_rate_q, test->rate)) < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->rate);
-        return ret;
-    }
-
-    if ((test->duration) && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
-        return ret;
-    }
-
-    test->time_base.num = frame_rate_q.den;
-    test->time_base.den = frame_rate_q.num;
-    test->max_pts = duration >= 0 ?
-        av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
+    test->max_pts = test->duration >= 0 ?
+        av_rescale_q(test->duration, AV_TIME_BASE_Q, av_inv_q(test->frame_rate)) : -1;
     test->frame_nb = 0;
     test->pts = 0;
 
     av_log(ctx, AV_LOG_VERBOSE, "rate:%d/%d duration:%f\n",
-           frame_rate_q.num, frame_rate_q.den,
-           duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base));
+           test->frame_rate.num, test->frame_rate.den,
+           test->duration < 0 ? -1 : test->max_pts * av_q2d(av_inv_q(test->frame_rate)));
     init_idct();
 
     return 0;
@@ -305,7 +282,7 @@
 
     outlink->w = WIDTH;
     outlink->h = HEIGHT;
-    outlink->time_base = test->time_base;
+    outlink->time_base = av_inv_q(test->frame_rate);
 
     return 0;
 }
@@ -323,20 +300,26 @@
 static int request_frame(AVFilterLink *outlink)
 {
     MPTestContext *test = outlink->src->priv;
-    AVFilterBufferRef *picref;
-    int w = WIDTH, h = HEIGHT, ch = h>>test->vsub;
+    AVFrame *picref;
+    int w = WIDTH, h = HEIGHT, cw = w>>test->hsub, ch = h>>test->vsub;
     unsigned int frame = test->frame_nb;
     enum test_type tt = test->test;
+    int i;
 
     if (test->max_pts >= 0 && test->pts > test->max_pts)
         return AVERROR_EOF;
-    picref = ff_get_video_buffer(outlink, AV_PERM_WRITE, w, h);
+    picref = ff_get_video_buffer(outlink, w, h);
+    if (!picref)
+        return AVERROR(ENOMEM);
     picref->pts = test->pts++;
 
     // clean image
-    memset(picref->data[0], 0,   picref->linesize[0] * h);
-    memset(picref->data[1], 128, picref->linesize[1] * ch);
-    memset(picref->data[2], 128, picref->linesize[2] * ch);
+    for (i = 0; i < h; i++)
+        memset(picref->data[0] + i*picref->linesize[0], 0, w);
+    for (i = 0; i < ch; i++) {
+        memset(picref->data[1] + i*picref->linesize[1], 128, cw);
+        memset(picref->data[2] + i*picref->linesize[2], 128, cw);
+    }
 
     if (tt == TEST_ALL && frame%30) /* draw a black frame at the beginning of each test */
         tt = (frame/30)%(TEST_NB-1);
@@ -355,9 +338,7 @@
     }
 
     test->frame_nb++;
-    ff_filter_frame(outlink, picref);
-
-    return 0;
+    return ff_filter_frame(outlink, picref);
 }
 
 static const AVFilterPad mptestsrc_outputs[] = {
diff --git a/libavfilter/vsrc_testsrc.c b/libavfilter/vsrc_testsrc.c
index f5e37f8..1b474a2 100644
--- a/libavfilter/vsrc_testsrc.c
+++ b/libavfilter/vsrc_testsrc.c
@@ -30,11 +30,12 @@
  * rgbtestsrc is ported from MPlayer libmpcodecs/vf_rgbtest.c by
  * Michael Niedermayer.
  *
- * smptebars is by Paul B Mahol.
+ * smptebars and smptehdbars are by Paul B Mahol.
  */
 
 #include <float.h>
 
+#include "libavutil/avassert.h"
 #include "libavutil/common.h"
 #include "libavutil/opt.h"
 #include "libavutil/imgutils.h"
@@ -52,15 +53,13 @@
     unsigned int nb_frame;
     AVRational time_base, frame_rate;
     int64_t pts;
-    char *frame_rate_str;       ///< video frame rate
-    char *duration_str;         ///< total duration of the generated video
     int64_t duration;           ///< duration expressed in microseconds
     AVRational sar;             ///< sample aspect ratio
     int nb_decimals;
     int draw_once;              ///< draw only the first frame, always put out the same picture
-    AVFilterBufferRef *picref;  ///< cached reference containing the painted picture
+    AVFrame *picref;            ///< cached reference containing the painted picture
 
-    void (* fill_picture_fn)(AVFilterContext *ctx, AVFilterBufferRef *picref);
+    void (* fill_picture_fn)(AVFilterContext *ctx, AVFrame *frame);
 
     /* only used by color */
     char *color_str;
@@ -75,47 +74,39 @@
 #define OFFSET(x) offsetof(TestSourceContext, x)
 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
 
-static const AVOption options[] = {
-    { "size",     "set video size",     OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },
-    { "s",        "set video size",     OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },
-    { "rate",     "set video rate",     OFFSET(frame_rate_str), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
-    { "r",        "set video rate",     OFFSET(frame_rate_str), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
-    { "duration", "set video duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL},   0, 0, FLAGS },
-    { "d",        "set video duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL},   0, 0, FLAGS },
+#define COMMON_OPTIONS \
+    { "size",     "set video size",     OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },\
+    { "s",        "set video size",     OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },\
+    { "rate",     "set video rate",     OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },\
+    { "r",        "set video rate",     OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },\
+    { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },\
+    { "d",        "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },\
     { "sar",      "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl= 1},  0, INT_MAX, FLAGS },
 
-    /* only used by color */
-    { "color", "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
-    { "c",     "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
 
-    /* only used by testsrc */
-    { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0},  INT_MIN, INT_MAX, FLAGS },
-    { "n",        "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0},  INT_MIN, INT_MAX, FLAGS },
+static const AVOption color_options[] = {
+    /* only used by color */
+    { "color", "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "c",     "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
+
+    COMMON_OPTIONS
     { NULL },
 };
 
-static av_cold int init(AVFilterContext *ctx, const char *args)
+static const AVOption options[] = {
+    COMMON_OPTIONS
+    /* only used by testsrc */
+    { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0},  0, 17, FLAGS },
+    { "n",        "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0},  0, 17, FLAGS },
+
+    { NULL },
+};
+
+static av_cold int init(AVFilterContext *ctx)
 {
     TestSourceContext *test = ctx->priv;
     int ret = 0;
 
-    av_opt_set_defaults(test);
-
-    if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0)
-        return ret;
-
-    if ((ret = av_parse_video_rate(&test->frame_rate, test->frame_rate_str)) < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->frame_rate_str);
-        return ret;
-    }
-
-    test->duration = -1;
-    if (test->duration_str &&
-        (ret = av_parse_time(&test->duration, test->duration_str, 1)) < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration_str);
-        return ret;
-    }
-
     if (test->nb_decimals && strcmp(ctx->filter->name, "testsrc")) {
         av_log(ctx, AV_LOG_WARNING,
                "Option 'decimals' is ignored with source '%s'\n",
@@ -149,8 +140,7 @@
 {
     TestSourceContext *test = ctx->priv;
 
-    av_opt_free(test);
-    avfilter_unref_bufferp(&test->picref);
+    av_frame_free(&test->picref);
 }
 
 static int config_props(AVFilterLink *outlink)
@@ -169,7 +159,7 @@
 static int request_frame(AVFilterLink *outlink)
 {
     TestSourceContext *test = outlink->src->priv;
-    AVFilterBufferRef *outpicref;
+    AVFrame *frame;
 
     if (test->duration >= 0 &&
         av_rescale_q(test->pts, test->time_base, AV_TIME_BASE_Q) >= test->duration)
@@ -178,39 +168,36 @@
     if (test->draw_once) {
         if (!test->picref) {
             test->picref =
-                ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_PRESERVE|AV_PERM_REUSE,
-                                    test->w, test->h);
+                ff_get_video_buffer(outlink, test->w, test->h);
             if (!test->picref)
                 return AVERROR(ENOMEM);
             test->fill_picture_fn(outlink->src, test->picref);
         }
-        outpicref = avfilter_ref_buffer(test->picref, ~AV_PERM_WRITE);
+        frame = av_frame_clone(test->picref);
     } else
-        outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, test->w, test->h);
+        frame = ff_get_video_buffer(outlink, test->w, test->h);
 
-    if (!outpicref)
+    if (!frame)
         return AVERROR(ENOMEM);
-    outpicref->pts = test->pts;
-    outpicref->pos = -1;
-    outpicref->video->key_frame = 1;
-    outpicref->video->interlaced = 0;
-    outpicref->video->pict_type = AV_PICTURE_TYPE_I;
-    outpicref->video->sample_aspect_ratio = test->sar;
+    frame->pts                 = test->pts;
+    frame->key_frame           = 1;
+    frame->interlaced_frame    = 0;
+    frame->pict_type           = AV_PICTURE_TYPE_I;
+    frame->sample_aspect_ratio = test->sar;
     if (!test->draw_once)
-        test->fill_picture_fn(outlink->src, outpicref);
+        test->fill_picture_fn(outlink->src, frame);
 
     test->pts++;
     test->nb_frame++;
 
-    return ff_filter_frame(outlink, outpicref);
+    return ff_filter_frame(outlink, frame);
 }
 
 #if CONFIG_COLOR_FILTER
 
-#define color_options options
 AVFILTER_DEFINE_CLASS(color);
 
-static void color_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
+static void color_fill_picture(AVFilterContext *ctx, AVFrame *picref)
 {
     TestSourceContext *test = ctx->priv;
     ff_fill_rectangle(&test->draw, &test->color,
@@ -218,14 +205,12 @@
                       0, 0, test->w, test->h);
 }
 
-static av_cold int color_init(AVFilterContext *ctx, const char *args)
+static av_cold int color_init(AVFilterContext *ctx)
 {
     TestSourceContext *test = ctx->priv;
-    test->class = &color_class;
     test->fill_picture_fn = color_fill_picture;
     test->draw_once = 1;
-    av_opt_set(test, "color", "black", 0);
-    return init(ctx, args);
+    return init(ctx);
 }
 
 static int color_query_formats(AVFilterContext *ctx)
@@ -270,6 +255,7 @@
     .name        = "color",
     .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input."),
 
+    .priv_class = &color_class,
     .priv_size = sizeof(TestSourceContext),
     .init      = color_init,
     .uninit    = uninit,
@@ -277,7 +263,6 @@
     .query_formats = color_query_formats,
     .inputs        = NULL,
     .outputs       = color_outputs,
-    .priv_class    = &color_class,
 };
 
 #endif /* CONFIG_COLOR_FILTER */
@@ -287,15 +272,14 @@
 #define nullsrc_options options
 AVFILTER_DEFINE_CLASS(nullsrc);
 
-static void nullsrc_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref) { }
+static void nullsrc_fill_picture(AVFilterContext *ctx, AVFrame *picref) { }
 
-static av_cold int nullsrc_init(AVFilterContext *ctx, const char *args)
+static av_cold int nullsrc_init(AVFilterContext *ctx)
 {
     TestSourceContext *test = ctx->priv;
 
-    test->class = &nullsrc_class;
     test->fill_picture_fn = nullsrc_fill_picture;
-    return init(ctx, args);
+    return init(ctx);
 }
 
 static const AVFilterPad nullsrc_outputs[] = {
@@ -314,9 +298,9 @@
     .init       = nullsrc_init,
     .uninit     = uninit,
     .priv_size  = sizeof(TestSourceContext),
+    .priv_class = &nullsrc_class,
     .inputs     = NULL,
     .outputs    = nullsrc_outputs,
-    .priv_class = &nullsrc_class,
 };
 
 #endif /* CONFIG_NULLSRC_FILTER */
@@ -338,8 +322,8 @@
  * @param w width  of the rectangle to draw, expressed as a number of segment_width units
  * @param h height of the rectangle to draw, expressed as a number of segment_width units
  */
-static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
-                           unsigned x, unsigned y, unsigned w, unsigned h)
+static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, int segment_width,
+                           int x, int y, int w, int h)
 {
     int i;
     int step = 3;
@@ -353,8 +337,8 @@
     }
 }
 
-static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
-                       unsigned segment_width)
+static void draw_digit(int digit, uint8_t *dst, int dst_linesize,
+                       int segment_width)
 {
 #define TOP_HBAR        1
 #define MID_HBAR        2
@@ -398,7 +382,7 @@
 
 #define GRADIENT_SIZE (6 * 256)
 
-static void test_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
+static void test_fill_picture(AVFilterContext *ctx, AVFrame *frame)
 {
     TestSourceContext *test = ctx->priv;
     uint8_t *p, *p0;
@@ -412,9 +396,9 @@
     int seg_size;
     int second;
     int i;
-    uint8_t *data = picref->data[0];
-    int width  = picref->video->w;
-    int height = picref->video->h;
+    uint8_t *data = frame->data[0];
+    int width  = frame->width;
+    int height = frame->height;
 
     /* draw colored bars and circle */
     radius = (width + height) / 4;
@@ -444,11 +428,11 @@
         }
         quad0 += dquad_y;
         dquad_y += 2;
-        p0 += picref->linesize[0];
+        p0 += frame->linesize[0];
     }
 
     /* draw sliding color line */
-    p0 = p = data + picref->linesize[0] * height * 3/4;
+    p0 = p = data + frame->linesize[0] * height * 3/4;
     grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
         GRADIENT_SIZE;
     rgrad = 0;
@@ -478,24 +462,29 @@
     }
     p = p0;
     for (y = height / 8; y > 0; y--) {
-        memcpy(p+picref->linesize[0], p, 3 * width);
-        p += picref->linesize[0];
+        memcpy(p+frame->linesize[0], p, 3 * width);
+        p += frame->linesize[0];
     }
 
     /* draw digits */
     seg_size = width / 80;
     if (seg_size >= 1 && height >= 13 * seg_size) {
+        int64_t p10decimals = 1;
         double time = av_q2d(test->time_base) * test->nb_frame *
                       pow(10, test->nb_decimals);
-        if (time > INT_MAX)
+        if (time >= INT_MAX)
             return;
-        second = (int)time;
+
+        for (x = 0; x < test->nb_decimals; x++)
+            p10decimals *= 10;
+
+        second = av_rescale_rnd(test->nb_frame * test->time_base.num, p10decimals, test->time_base.den, AV_ROUND_ZERO);
         x = width - (width - seg_size * 64) / 2;
         y = (height - seg_size * 13) / 2;
-        p = data + (x*3 + y * picref->linesize[0]);
+        p = data + (x*3 + y * frame->linesize[0]);
         for (i = 0; i < 8; i++) {
             p -= 3 * 8 * seg_size;
-            draw_digit(second % 10, p, picref->linesize[0], seg_size);
+            draw_digit(second % 10, p, frame->linesize[0], seg_size);
             second /= 10;
             if (second == 0)
                 break;
@@ -503,13 +492,12 @@
     }
 }
 
-static av_cold int test_init(AVFilterContext *ctx, const char *args)
+static av_cold int test_init(AVFilterContext *ctx)
 {
     TestSourceContext *test = ctx->priv;
 
-    test->class = &testsrc_class;
     test->fill_picture_fn = test_fill_picture;
-    return init(ctx, args);
+    return init(ctx);
 }
 
 static int test_query_formats(AVFilterContext *ctx)
@@ -532,17 +520,17 @@
 };
 
 AVFilter avfilter_vsrc_testsrc = {
-    .name      = "testsrc",
-    .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
-    .priv_size = sizeof(TestSourceContext),
-    .init      = test_init,
-    .uninit    = uninit,
+    .name          = "testsrc",
+    .description   = NULL_IF_CONFIG_SMALL("Generate test pattern."),
+    .priv_size     = sizeof(TestSourceContext),
+    .priv_class    = &testsrc_class,
+    .init          = test_init,
+    .uninit        = uninit,
 
-    .query_formats   = test_query_formats,
+    .query_formats = test_query_formats,
 
     .inputs    = NULL,
     .outputs   = avfilter_vsrc_testsrc_outputs,
-    .priv_class = &testsrc_class,
 };
 
 #endif /* CONFIG_TESTSRC_FILTER */
@@ -588,13 +576,13 @@
     }
 }
 
-static void rgbtest_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
+static void rgbtest_fill_picture(AVFilterContext *ctx, AVFrame *frame)
 {
     TestSourceContext *test = ctx->priv;
-    int x, y, w = picref->video->w, h = picref->video->h;
+    int x, y, w = frame->width, h = frame->height;
 
     for (y = 0; y < h; y++) {
-         for (x = 0; x < picref->video->w; x++) {
+         for (x = 0; x < w; x++) {
              int c = 256*x/w;
              int r = 0, g = 0, b = 0;
 
@@ -602,20 +590,19 @@
              else if (3*y < 2*h) g = c;
              else                b = c;
 
-             rgbtest_put_pixel(picref->data[0], picref->linesize[0], x, y, r, g, b,
+             rgbtest_put_pixel(frame->data[0], frame->linesize[0], x, y, r, g, b,
                                ctx->outputs[0]->format, test->rgba_map);
          }
      }
 }
 
-static av_cold int rgbtest_init(AVFilterContext *ctx, const char *args)
+static av_cold int rgbtest_init(AVFilterContext *ctx)
 {
     TestSourceContext *test = ctx->priv;
 
     test->draw_once = 1;
-    test->class = &rgbtestsrc_class;
     test->fill_picture_fn = rgbtest_fill_picture;
-    return init(ctx, args);
+    return init(ctx);
 }
 
 static int rgbtest_query_formats(AVFilterContext *ctx)
@@ -651,26 +638,23 @@
 };
 
 AVFilter avfilter_vsrc_rgbtestsrc = {
-    .name      = "rgbtestsrc",
-    .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
-    .priv_size = sizeof(TestSourceContext),
-    .init      = rgbtest_init,
-    .uninit    = uninit,
+    .name          = "rgbtestsrc",
+    .description   = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
+    .priv_size     = sizeof(TestSourceContext),
+    .priv_class    = &rgbtestsrc_class,
+    .init          = rgbtest_init,
+    .uninit        = uninit,
 
-    .query_formats   = rgbtest_query_formats,
+    .query_formats = rgbtest_query_formats,
 
     .inputs    = NULL,
 
     .outputs   = avfilter_vsrc_rgbtestsrc_outputs,
-    .priv_class = &rgbtestsrc_class,
 };
 
 #endif /* CONFIG_RGBTESTSRC_FILTER */
 
-#if CONFIG_SMPTEBARS_FILTER
-
-#define smptebars_options options
-AVFILTER_DEFINE_CLASS(smptebars);
+#if CONFIG_SMPTEBARS_FILTER || CONFIG_SMPTEHDBARS_FILTER
 
 static const uint8_t rainbow[7][4] = {
     { 191, 191, 191, 255 },     /* gray */
@@ -703,54 +687,34 @@
 static const uint8_t i_pixel[4] = {   0,  68, 130, 255 };
 static const uint8_t q_pixel[4] = {  67,   0, 130, 255 };
 
-static void smptebars_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
+static const uint8_t gray40[4] = { 102, 102, 102, 255 };
+static const uint8_t gray15[4] = {  38,  38,  38, 255 };
+static const uint8_t   cyan[4] = {   0, 255, 255, 255 };
+static const uint8_t yellow[4] = { 255, 255,   0, 255 };
+static const uint8_t   blue[4] = {   0,   0, 255, 255 };
+static const uint8_t    red[4] = { 255,   0,   0, 255 };
+static const uint8_t black0[4] = {   5,   5,   5, 255 };
+static const uint8_t black2[4] = {  10,  10,  10, 255 };
+static const uint8_t black4[4] = {  15,  15,  15, 255 };
+static const uint8_t   neg2[4] = {   0,   0,   0, 255 };
+
+static void inline draw_bar(TestSourceContext *test, const uint8_t *color,
+                            unsigned x, unsigned y, unsigned w, unsigned h,
+                            AVFrame *frame)
 {
-    TestSourceContext *test = ctx->priv;
-    FFDrawColor color;
-    int r_w, r_h, w_h, p_w, p_h, i, x = 0;
+    FFDrawColor draw_color;
 
-    r_w = (test->w + 6) / 7;
-    r_h = test->h * 2 / 3;
-    w_h = test->h * 3 / 4 - r_h;
-    p_w = r_w * 5 / 4;
-    p_h = test->h - w_h - r_h;
+    x = FFMIN(x, test->w - 1);
+    y = FFMIN(y, test->h - 1);
+    w = FFMIN(w, test->w - x);
+    h = FFMIN(h, test->h - y);
 
-#define DRAW_COLOR(rgba, x, y, w, h)                                    \
-    ff_draw_color(&test->draw, &color, rgba);                           \
-    ff_fill_rectangle(&test->draw, &color,                              \
-                      picref->data, picref->linesize, x, y, w, h)       \
+    av_assert0(x + w <= test->w);
+    av_assert0(y + h <= test->h);
 
-    for (i = 0; i < 7; i++) {
-        DRAW_COLOR(rainbow[i], x, 0,   FFMIN(r_w, test->w - x), r_h);
-        DRAW_COLOR(wobnair[i], x, r_h, FFMIN(r_w, test->w - x), w_h);
-        x += r_w;
-    }
-    x = 0;
-    DRAW_COLOR(i_pixel, x, r_h + w_h, p_w, p_h);
-    x += p_w;
-    DRAW_COLOR(white, x, r_h + w_h, p_w, p_h);
-    x += p_w;
-    DRAW_COLOR(q_pixel, x, r_h + w_h, p_w, p_h);
-    x += p_w;
-    DRAW_COLOR(black, x, r_h + w_h, 5 * r_w - x, p_h);
-    x += 5 * r_w - x;
-    DRAW_COLOR(neg4ire, x, r_h + w_h, r_w / 3, p_h);
-    x += r_w / 3;
-    DRAW_COLOR(black, x, r_h + w_h, r_w / 3, p_h);
-    x += r_w / 3;
-    DRAW_COLOR(pos4ire, x, r_h + w_h, r_w / 3, p_h);
-    x += r_w / 3;
-    DRAW_COLOR(black, x, r_h + w_h, test->w - x, p_h);
-}
-
-static av_cold int smptebars_init(AVFilterContext *ctx, const char *args)
-{
-    TestSourceContext *test = ctx->priv;
-
-    test->class = &smptebars_class;
-    test->fill_picture_fn = smptebars_fill_picture;
-    test->draw_once = 1;
-    return init(ctx, args);
+    ff_draw_color(&test->draw, &draw_color, color);
+    ff_fill_rectangle(&test->draw, &draw_color,
+                      frame->data, frame->linesize, x, y, w, h);
 }
 
 static int smptebars_query_formats(AVFilterContext *ctx)
@@ -779,6 +743,57 @@
     { NULL }
 };
 
+#if CONFIG_SMPTEBARS_FILTER
+
+#define smptebars_options options
+AVFILTER_DEFINE_CLASS(smptebars);
+
+static void smptebars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
+{
+    TestSourceContext *test = ctx->priv;
+    int r_w, r_h, w_h, p_w, p_h, i, tmp, x = 0;
+    const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(picref->format);
+
+    r_w = FFALIGN((test->w + 6) / 7, 1 << pixdesc->log2_chroma_w);
+    r_h = FFALIGN(test->h * 2 / 3, 1 << pixdesc->log2_chroma_h);
+    w_h = FFALIGN(test->h * 3 / 4 - r_h,  1 << pixdesc->log2_chroma_h);
+    p_w = FFALIGN(r_w * 5 / 4, 1 << pixdesc->log2_chroma_w);
+    p_h = test->h - w_h - r_h;
+
+    for (i = 0; i < 7; i++) {
+        draw_bar(test, rainbow[i], x, 0,   r_w, r_h, picref);
+        draw_bar(test, wobnair[i], x, r_h, r_w, w_h, picref);
+        x += r_w;
+    }
+    x = 0;
+    draw_bar(test, i_pixel, x, r_h + w_h, p_w, p_h, picref);
+    x += p_w;
+    draw_bar(test, white, x, r_h + w_h, p_w, p_h, picref);
+    x += p_w;
+    draw_bar(test, q_pixel, x, r_h + w_h, p_w, p_h, picref);
+    x += p_w;
+    tmp = FFALIGN(5 * r_w - x,  1 << pixdesc->log2_chroma_w);
+    draw_bar(test, black, x, r_h + w_h, tmp, p_h, picref);
+    x += tmp;
+    tmp = FFALIGN(r_w / 3,  1 << pixdesc->log2_chroma_w);
+    draw_bar(test, neg4ire, x, r_h + w_h, tmp, p_h, picref);
+    x += tmp;
+    draw_bar(test, black, x, r_h + w_h, tmp, p_h, picref);
+    x += tmp;
+    draw_bar(test, pos4ire, x, r_h + w_h, tmp, p_h, picref);
+    x += tmp;
+    draw_bar(test, black, x, r_h + w_h, test->w - x, p_h, picref);
+}
+
+static av_cold int smptebars_init(AVFilterContext *ctx)
+{
+    TestSourceContext *test = ctx->priv;
+
+    test->fill_picture_fn = smptebars_fill_picture;
+    test->draw_once = 1;
+    return init(ctx);
+}
+
 AVFilter avfilter_vsrc_smptebars = {
     .name      = "smptebars",
     .description = NULL_IF_CONFIG_SMALL("Generate SMPTE color bars."),
@@ -793,3 +808,108 @@
 };
 
 #endif  /* CONFIG_SMPTEBARS_FILTER */
+
+#if CONFIG_SMPTEHDBARS_FILTER
+
+#define smptehdbars_options options
+AVFILTER_DEFINE_CLASS(smptehdbars);
+
+static void smptehdbars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
+{
+    TestSourceContext *test = ctx->priv;
+    int d_w, r_w, r_h, l_w, i, tmp, x = 0, y = 0;
+    const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(picref->format);
+
+    d_w = FFALIGN(test->w / 8, 1 << pixdesc->log2_chroma_w);
+    r_h = FFALIGN(test->h * 7 / 12, 1 << pixdesc->log2_chroma_h);
+    draw_bar(test, gray40, x, 0, d_w, r_h, picref);
+    x += d_w;
+
+    r_w = FFALIGN((((test->w + 3) / 4) * 3) / 7, 1 << pixdesc->log2_chroma_w);
+    for (i = 0; i < 7; i++) {
+        draw_bar(test, rainbow[i], x, 0, r_w, r_h, picref);
+        x += r_w;
+    }
+    draw_bar(test, gray40, x, 0, test->w - x, r_h, picref);
+    y = r_h;
+    r_h = FFALIGN(test->h / 12, 1 << pixdesc->log2_chroma_h);
+    draw_bar(test, cyan, 0, y, d_w, r_h, picref);
+    x = d_w;
+    draw_bar(test, i_pixel, x, y, r_w, r_h, picref);
+    x += r_w;
+    tmp = r_w * 6;
+    draw_bar(test, rainbow[0], x, y, tmp, r_h, picref);
+    x += tmp;
+    l_w = x;
+    draw_bar(test, blue, x, y, test->w - x, r_h, picref);
+    y += r_h;
+    draw_bar(test, yellow, 0, y, d_w, r_h, picref);
+    x = d_w;
+    draw_bar(test, q_pixel, x, y, r_w, r_h, picref);
+    x += r_w;
+
+    for (i = 0; i < tmp; i += 1 << pixdesc->log2_chroma_w) {
+        uint8_t yramp[4] = {0};
+
+        yramp[0] =
+        yramp[1] =
+        yramp[2] = i * 255 / tmp;
+        yramp[3] = 255;
+
+        draw_bar(test, yramp, x, y, 1 << pixdesc->log2_chroma_w, r_h, picref);
+        x += 1 << pixdesc->log2_chroma_w;
+    }
+    draw_bar(test, red, x, y, test->w - x, r_h, picref);
+    y += r_h;
+    draw_bar(test, gray15, 0, y, d_w, test->h - y, picref);
+    x = d_w;
+    tmp = FFALIGN(r_w * 3 / 2, 1 << pixdesc->log2_chroma_w);
+    draw_bar(test, black0, x, y, tmp, test->h - y, picref);
+    x += tmp;
+    tmp = FFALIGN(r_w * 2, 1 << pixdesc->log2_chroma_w);
+    draw_bar(test, white, x, y, tmp, test->h - y, picref);
+    x += tmp;
+    tmp = FFALIGN(r_w * 5 / 6, 1 << pixdesc->log2_chroma_w);
+    draw_bar(test, black0, x, y, tmp, test->h - y, picref);
+    x += tmp;
+    tmp = FFALIGN(r_w / 3, 1 << pixdesc->log2_chroma_w);
+    draw_bar(test,   neg2, x, y, tmp, test->h - y, picref);
+    x += tmp;
+    draw_bar(test, black0, x, y, tmp, test->h - y, picref);
+    x += tmp;
+    draw_bar(test, black2, x, y, tmp, test->h - y, picref);
+    x += tmp;
+    draw_bar(test, black0, x, y, tmp, test->h - y, picref);
+    x += tmp;
+    draw_bar(test, black4, x, y, tmp, test->h - y, picref);
+    x += tmp;
+    r_w = l_w - x;
+    draw_bar(test, black0, x, y, r_w, test->h - y, picref);
+    x += r_w;
+    draw_bar(test, gray15, x, y, test->w - x, test->h - y, picref);
+}
+
+static av_cold int smptehdbars_init(AVFilterContext *ctx)
+{
+    TestSourceContext *test = ctx->priv;
+
+    test->fill_picture_fn = smptehdbars_fill_picture;
+    test->draw_once = 1;
+    return init(ctx);
+}
+
+AVFilter avfilter_vsrc_smptehdbars = {
+    .name      = "smptehdbars",
+    .description = NULL_IF_CONFIG_SMALL("Generate SMPTE HD color bars."),
+    .priv_size = sizeof(TestSourceContext),
+    .init      = smptehdbars_init,
+    .uninit    = uninit,
+
+    .query_formats = smptebars_query_formats,
+    .inputs        = NULL,
+    .outputs       = smptebars_outputs,
+    .priv_class    = &smptehdbars_class,
+};
+
+#endif  /* CONFIG_SMPTEHDBARS_FILTER */
+#endif  /* CONFIG_SMPTEBARS_FILTER || CONFIG_SMPTEHDBARS_FILTER */
diff --git a/libavfilter/x86/Makefile b/libavfilter/x86/Makefile
index 59cefe8..cd97347 100644
--- a/libavfilter/x86/Makefile
+++ b/libavfilter/x86/Makefile
@@ -5,4 +5,4 @@
 
 YASM-OBJS-$(CONFIG_HQDN3D_FILTER)            += x86/vf_hqdn3d.o
 YASM-OBJS-$(CONFIG_VOLUME_FILTER)            += x86/af_volume.o
-YASM-OBJS-$(CONFIG_YADIF_FILTER)             += x86/vf_yadif.o
+YASM-OBJS-$(CONFIG_YADIF_FILTER)             += x86/vf_yadif.o x86/yadif-16.o x86/yadif-10.o
diff --git a/libavfilter/x86/vf_gradfun.c b/libavfilter/x86/vf_gradfun.c
index 214e764..10accca 100644
--- a/libavfilter/x86/vf_gradfun.c
+++ b/libavfilter/x86/vf_gradfun.c
@@ -200,9 +200,9 @@
 
 av_cold void ff_gradfun_init_x86(GradFunContext *gf)
 {
+#if HAVE_MMXEXT_INLINE
     int cpu_flags = av_get_cpu_flags();
 
-#if HAVE_MMXEXT_INLINE
     if (cpu_flags & AV_CPU_FLAG_MMXEXT)
         gf->filter_line = gradfun_filter_line_mmxext;
 #endif
diff --git a/libavfilter/x86/vf_yadif.asm b/libavfilter/x86/vf_yadif.asm
index a8f7987..ebc505c 100644
--- a/libavfilter/x86/vf_yadif.asm
+++ b/libavfilter/x86/vf_yadif.asm
@@ -90,17 +90,17 @@
 %endmacro
 
 %macro LOAD 2
-    movh      m%1, %2
-    punpcklbw m%1, m7
+    movh      %1, %2
+    punpcklbw %1, m7
 %endmacro
 
 %macro FILTER 3
 .loop%1:
     pxor         m7, m7
-    LOAD          0, [curq+t1]
-    LOAD          1, [curq+t0]
-    LOAD          2, [%2]
-    LOAD          3, [%3]
+    LOAD         m0, [curq+t1]
+    LOAD         m1, [curq+t0]
+    LOAD         m2, [%2]
+    LOAD         m3, [%3]
     mova         m4, m3
     paddw        m3, m2
     psraw        m3, 1
@@ -109,8 +109,8 @@
     mova   [rsp+32], m1
     psubw        m2, m4
     ABS1         m2, m4
-    LOAD          3, [prevq+t1]
-    LOAD          4, [prevq+t0]
+    LOAD         m3, [prevq+t1]
+    LOAD         m4, [prevq+t0]
     psubw        m3, m0
     psubw        m4, m1
     ABS1         m3, m5
@@ -119,8 +119,8 @@
     psrlw        m2, 1
     psrlw        m3, 1
     pmaxsw       m2, m3
-    LOAD          3, [nextq+t1]
-    LOAD          4, [nextq+t0]
+    LOAD         m3, [nextq+t1]
+    LOAD         m4, [nextq+t0]
     psubw        m3, m0
     psubw        m4, m1
     ABS1         m3, m5
@@ -166,10 +166,10 @@
     mova         m6, [rsp+48]
     cmp   DWORD r8m, 2
     jge .end%1
-    LOAD          2, [%2+t1*2]
-    LOAD          4, [%3+t1*2]
-    LOAD          3, [%2+t0*2]
-    LOAD          5, [%3+t0*2]
+    LOAD         m2, [%2+t1*2]
+    LOAD         m4, [%3+t1*2]
+    LOAD         m3, [%2+t0*2]
+    LOAD         m5, [%3+t0*2]
     paddw        m2, m4
     paddw        m3, m5
     psrlw        m2, 1
@@ -220,8 +220,6 @@
 cglobal yadif_filter_line, 4, 7, 8, 80, dst, prev, cur, next, w, prefs, \
                                         mrefs, parity, mode
 %endif
-    cmp      DWORD wm, 0
-    jle .ret
 %if ARCH_X86_32
     mov            r4, r5mp
     mov            r5, r6mp
diff --git a/libavfilter/x86/vf_yadif_init.c b/libavfilter/x86/vf_yadif_init.c
index 2873744..0043931 100644
--- a/libavfilter/x86/vf_yadif_init.c
+++ b/libavfilter/x86/vf_yadif_init.c
@@ -36,24 +36,65 @@
                                 void *next, int w, int prefs,
                                 int mrefs, int parity, int mode);
 
+void ff_yadif_filter_line_16bit_mmxext(void *dst, void *prev, void *cur,
+                                       void *next, int w, int prefs,
+                                       int mrefs, int parity, int mode);
+void ff_yadif_filter_line_16bit_sse2(void *dst, void *prev, void *cur,
+                                     void *next, int w, int prefs,
+                                     int mrefs, int parity, int mode);
+void ff_yadif_filter_line_16bit_ssse3(void *dst, void *prev, void *cur,
+                                      void *next, int w, int prefs,
+                                      int mrefs, int parity, int mode);
+void ff_yadif_filter_line_16bit_sse4(void *dst, void *prev, void *cur,
+                                     void *next, int w, int prefs,
+                                     int mrefs, int parity, int mode);
+
+void ff_yadif_filter_line_10bit_mmxext(void *dst, void *prev, void *cur,
+                                       void *next, int w, int prefs,
+                                       int mrefs, int parity, int mode);
+void ff_yadif_filter_line_10bit_sse2(void *dst, void *prev, void *cur,
+                                     void *next, int w, int prefs,
+                                     int mrefs, int parity, int mode);
+void ff_yadif_filter_line_10bit_ssse3(void *dst, void *prev, void *cur,
+                                      void *next, int w, int prefs,
+                                      int mrefs, int parity, int mode);
+
 av_cold void ff_yadif_init_x86(YADIFContext *yadif)
 {
-    int cpu_flags = av_get_cpu_flags();
-
 #if HAVE_YASM
+    int cpu_flags = av_get_cpu_flags();
+    int bit_depth = (!yadif->csp) ? 8
+                                  : yadif->csp->comp[0].depth_minus1 + 1;
+
+    if (bit_depth >= 15) {
 #if ARCH_X86_32
-    if (EXTERNAL_MMXEXT(cpu_flags)) {
-        yadif->filter_line = ff_yadif_filter_line_mmxext;
-        yadif->req_align   = 8;
-    }
+        if (EXTERNAL_MMXEXT(cpu_flags))
+            yadif->filter_line = ff_yadif_filter_line_16bit_mmxext;
 #endif /* ARCH_X86_32 */
-    if (EXTERNAL_SSE2(cpu_flags)) {
-        yadif->filter_line = ff_yadif_filter_line_sse2;
-        yadif->req_align   = 16;
-    }
-    if (EXTERNAL_SSSE3(cpu_flags)) {
-        yadif->filter_line = ff_yadif_filter_line_ssse3;
-        yadif->req_align   = 16;
+        if (EXTERNAL_SSE2(cpu_flags))
+            yadif->filter_line = ff_yadif_filter_line_16bit_sse2;
+        if (EXTERNAL_SSSE3(cpu_flags))
+            yadif->filter_line = ff_yadif_filter_line_16bit_ssse3;
+        if (EXTERNAL_SSE4(cpu_flags))
+            yadif->filter_line = ff_yadif_filter_line_16bit_sse4;
+    } else if ( bit_depth >= 9 && bit_depth <= 14) {
+#if ARCH_X86_32
+        if (EXTERNAL_MMXEXT(cpu_flags))
+            yadif->filter_line = ff_yadif_filter_line_10bit_mmxext;
+#endif /* ARCH_X86_32 */
+        if (EXTERNAL_SSE2(cpu_flags))
+            yadif->filter_line = ff_yadif_filter_line_10bit_sse2;
+        if (EXTERNAL_SSSE3(cpu_flags))
+            yadif->filter_line = ff_yadif_filter_line_10bit_ssse3;
+    } else {
+#if ARCH_X86_32
+        if (EXTERNAL_MMXEXT(cpu_flags))
+            yadif->filter_line = ff_yadif_filter_line_mmxext;
+#endif /* ARCH_X86_32 */
+        if (EXTERNAL_SSE2(cpu_flags))
+            yadif->filter_line = ff_yadif_filter_line_sse2;
+        if (EXTERNAL_SSSE3(cpu_flags))
+            yadif->filter_line = ff_yadif_filter_line_ssse3;
     }
 #endif /* HAVE_YASM */
 }
diff --git a/libavfilter/x86/yadif-10.asm b/libavfilter/x86/yadif-10.asm
new file mode 100644
index 0000000..d586deb
--- /dev/null
+++ b/libavfilter/x86/yadif-10.asm
@@ -0,0 +1,282 @@
+;*****************************************************************************
+;* x86-optimized functions for yadif filter
+;*
+;* Copyright (C) 2006 Michael Niedermayer <michaelni@gmx.at>
+;* Copyright (c) 2013 Daniel Kang <daniel.d.kang@gmail.com>
+;* Copyright (c) 2011-2013 James Darnley <james.darnley@gmail.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 General Public License as published by
+;* the Free Software Foundation; either version 2 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 General Public License for more details.
+;*
+;* You should have received a copy of the GNU 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 "libavutil/x86/x86util.asm"
+
+SECTION_RODATA
+
+pw_1: times 8 dw 1
+
+SECTION .text
+
+%macro PABS 2
+%if cpuflag(ssse3)
+    pabsw %1, %1
+%else
+    pxor    %2, %2
+    pcmpgtw %2, %1
+    pxor    %1, %2
+    psubw   %1, %2
+%endif
+%endmacro
+
+%macro PMAXUW 2
+%if cpuflag(sse4)
+    pmaxuw %1, %2
+%else
+    psubusw %1, %2
+    paddusw %1, %2
+%endif
+%endmacro
+
+%macro CHECK 2
+    movu      m2, [curq+t1+%1*2]
+    movu      m3, [curq+t0+%2*2]
+    mova      m4, m2
+    mova      m5, m2
+    pxor      m4, m3
+    pavgw     m5, m3
+    pand      m4, [pw_1]
+    psubusw   m5, m4
+%if mmsize == 16
+    psrldq    m5, 2
+%else
+    psrlq     m5, 16
+%endif
+    mova      m4, m2
+    psubusw   m2, m3
+    psubusw   m3, m4
+    PMAXUW    m2, m3
+    mova      m3, m2
+    mova      m4, m2
+%if mmsize == 16
+    psrldq    m3, 2
+    psrldq    m4, 4
+%else
+    psrlq     m3, 16
+    psrlq     m4, 32
+%endif
+    paddw     m2, m3
+    paddw     m2, m4
+%endmacro
+
+%macro CHECK1 0
+    mova    m3, m0
+    pcmpgtw m3, m2
+    pminsw  m0, m2
+    mova    m6, m3
+    pand    m5, m3
+    pandn   m3, m1
+    por     m3, m5
+    mova    m1, m3
+%endmacro
+
+; %macro CHECK2 0
+;     paddw   m6, [pw_1]
+;     psllw   m6, 14
+;     paddsw  m2, m6
+;     mova    m3, m0
+;     pcmpgtw m3, m2
+;     pminsw  m0, m2
+;     pand    m5, m3
+;     pandn   m3, m1
+;     por     m3, m5
+;     mova    m1, m3
+; %endmacro
+
+; This version of CHECK2 is required for 14-bit samples.  The left-shift trick
+; in the old code is not large enough to correctly select pixels or scores.
+
+%macro CHECK2 0
+    mova    m3, m0
+    pcmpgtw m0, m2
+    pand    m0, m6
+    mova    m6, m0
+    pand    m5, m6
+    pand    m2, m0
+    pandn   m6, m1
+    pandn   m0, m3
+    por     m6, m5
+    por     m0, m2
+    mova    m1, m6
+%endmacro
+
+%macro LOAD 2
+    movu      %1, %2
+%endmacro
+
+%macro FILTER 3
+.loop%1:
+    pxor         m7, m7
+    LOAD         m0, [curq+t1]
+    LOAD         m1, [curq+t0]
+    LOAD         m2, [%2]
+    LOAD         m3, [%3]
+    mova         m4, m3
+    paddw        m3, m2
+    psraw        m3, 1
+    mova   [rsp+ 0], m0
+    mova   [rsp+16], m3
+    mova   [rsp+32], m1
+    psubw        m2, m4
+    PABS         m2, m4
+    LOAD         m3, [prevq+t1]
+    LOAD         m4, [prevq+t0]
+    psubw        m3, m0
+    psubw        m4, m1
+    PABS         m3, m5
+    PABS         m4, m5
+    paddw        m3, m4
+    psrlw        m2, 1
+    psrlw        m3, 1
+    pmaxsw       m2, m3
+    LOAD         m3, [nextq+t1]
+    LOAD         m4, [nextq+t0]
+    psubw        m3, m0
+    psubw        m4, m1
+    PABS         m3, m5
+    PABS         m4, m5
+    paddw        m3, m4
+    psrlw        m3, 1
+    pmaxsw       m2, m3
+    mova   [rsp+48], m2
+
+    paddw        m1, m0
+    paddw        m0, m0
+    psubw        m0, m1
+    psrlw        m1, 1
+    PABS         m0, m2
+
+    movu         m2, [curq+t1-1*2]
+    movu         m3, [curq+t0-1*2]
+    mova         m4, m2
+    psubusw      m2, m3
+    psubusw      m3, m4
+    PMAXUW       m2, m3
+%if mmsize == 16
+    mova         m3, m2
+    psrldq       m3, 4
+%else
+    mova         m3, m2
+    psrlq        m3, 32
+%endif
+    paddw        m0, m2
+    paddw        m0, m3
+    psubw        m0, [pw_1]
+
+    CHECK -2, 0
+    CHECK1
+    CHECK -3, 1
+    CHECK2
+    CHECK 0, -2
+    CHECK1
+    CHECK 1, -3
+    CHECK2
+
+    mova         m6, [rsp+48]
+    cmp   DWORD r8m, 2
+    jge .end%1
+    LOAD         m2, [%2+t1*2]
+    LOAD         m4, [%3+t1*2]
+    LOAD         m3, [%2+t0*2]
+    LOAD         m5, [%3+t0*2]
+    paddw        m2, m4
+    paddw        m3, m5
+    psrlw        m2, 1
+    psrlw        m3, 1
+    mova         m4, [rsp+ 0]
+    mova         m5, [rsp+16]
+    mova         m7, [rsp+32]
+    psubw        m2, m4
+    psubw        m3, m7
+    mova         m0, m5
+    psubw        m5, m4
+    psubw        m0, m7
+    mova         m4, m2
+    pminsw       m2, m3
+    pmaxsw       m3, m4
+    pmaxsw       m2, m5
+    pminsw       m3, m5
+    pmaxsw       m2, m0
+    pminsw       m3, m0
+    pxor         m4, m4
+    pmaxsw       m6, m3
+    psubw        m4, m2
+    pmaxsw       m6, m4
+
+.end%1:
+    mova         m2, [rsp+16]
+    mova         m3, m2
+    psubw        m2, m6
+    paddw        m3, m6
+    pmaxsw       m1, m2
+    pminsw       m1, m3
+
+    movu     [dstq], m1
+    add        dstq, mmsize-4
+    add       prevq, mmsize-4
+    add        curq, mmsize-4
+    add       nextq, mmsize-4
+    sub   DWORD r4m, mmsize/2-2
+    jg .loop%1
+%endmacro
+
+%macro YADIF 0
+%if ARCH_X86_32
+cglobal yadif_filter_line_10bit, 4, 6, 8, 80, dst, prev, cur, next, w, \
+                                              prefs, mrefs, parity, mode
+%else
+cglobal yadif_filter_line_10bit, 4, 7, 8, 80, dst, prev, cur, next, w, \
+                                              prefs, mrefs, parity, mode
+%endif
+%if ARCH_X86_32
+    mov            r4, r5mp
+    mov            r5, r6mp
+    DECLARE_REG_TMP 4,5
+%else
+    movsxd         r5, DWORD r5m
+    movsxd         r6, DWORD r6m
+    DECLARE_REG_TMP 5,6
+%endif
+
+    cmp DWORD paritym, 0
+    je .parity0
+    FILTER 1, prevq, curq
+    jmp .ret
+
+.parity0:
+    FILTER 0, curq, nextq
+
+.ret:
+    RET
+%endmacro
+
+INIT_XMM ssse3
+YADIF
+INIT_XMM sse2
+YADIF
+%if ARCH_X86_32
+INIT_MMX mmxext
+YADIF
+%endif
diff --git a/libavfilter/x86/yadif-16.asm b/libavfilter/x86/yadif-16.asm
new file mode 100644
index 0000000..a2e6006
--- /dev/null
+++ b/libavfilter/x86/yadif-16.asm
@@ -0,0 +1,347 @@
+;*****************************************************************************
+;* x86-optimized functions for yadif filter
+;*
+;* Copyright (C) 2006 Michael Niedermayer <michaelni@gmx.at>
+;* Copyright (c) 2013 Daniel Kang <daniel.d.kang@gmail.com>
+;* Copyright (c) 2011-2013 James Darnley <james.darnley@gmail.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 General Public License as published by
+;* the Free Software Foundation; either version 2 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 General Public License for more details.
+;*
+;* You should have received a copy of the GNU 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 "libavutil/x86/x86util.asm"
+
+SECTION_RODATA
+
+pw_1:    times 8 dw 1
+pw_8000: times 8 dw 0x8000
+pd_1:    times 4 dd 1
+pd_8000: times 4 dd 0x8000
+
+SECTION .text
+
+%macro PIXSHIFT1 1
+%if cpuflag(sse2)
+    psrldq %1, 2
+%else
+    psrlq %1, 16
+%endif
+%endmacro
+
+%macro PIXSHIFT2 1
+%if cpuflag(sse2)
+    psrldq %1, 4
+%else
+    psrlq %1, 32
+%endif
+%endmacro
+
+%macro PABS 2
+%if cpuflag(ssse3)
+    pabsd %1, %1
+%else
+    pxor    %2, %2
+    pcmpgtd %2, %1
+    pxor    %1, %2
+    psubd   %1, %2
+%endif
+%endmacro
+
+%macro PACK 1
+%if cpuflag(sse4)
+    packusdw %1, %1
+%else
+    psubd    %1, [pd_8000]
+    packssdw %1, %1
+    paddw    %1, [pw_8000]
+%endif
+%endmacro
+
+%macro PMINSD 3
+%if cpuflag(sse4)
+    pminsd %1, %2
+%else
+    mova    %3, %2
+    pcmpgtd %3, %1
+    pand    %1, %3
+    pandn   %3, %2
+    por     %1, %3
+%endif
+%endmacro
+
+%macro PMAXSD 3
+%if cpuflag(sse4)
+    pmaxsd %1, %2
+%else
+    mova    %3, %1
+    pcmpgtd %3, %2
+    pand    %1, %3
+    pandn   %3, %2
+    por     %1, %3
+%endif
+%endmacro
+
+%macro PMAXUW 2
+%if cpuflag(sse4)
+    pmaxuw %1, %2
+%else
+    psubusw %1, %2
+    paddusw %1, %2
+%endif
+%endmacro
+
+%macro CHECK 2
+    movu      m2, [curq+t1+%1*2]
+    movu      m3, [curq+t0+%2*2]
+    mova      m4, m2
+    mova      m5, m2
+    pxor      m4, m3
+    pavgw     m5, m3
+    pand      m4, [pw_1]
+    psubusw   m5, m4
+%if mmsize == 16
+    psrldq    m5, 2
+%else
+    psrlq     m5, 16
+%endif
+    punpcklwd m5, m7
+    mova      m4, m2
+    psubusw   m2, m3
+    psubusw   m3, m4
+    PMAXUW    m2, m3
+    mova      m3, m2
+    mova      m4, m2
+%if mmsize == 16
+    psrldq    m3, 2
+    psrldq    m4, 4
+%else
+    psrlq     m3, 16
+    psrlq     m4, 32
+%endif
+    punpcklwd m2, m7
+    punpcklwd m3, m7
+    punpcklwd m4, m7
+    paddd     m2, m3
+    paddd     m2, m4
+%endmacro
+
+%macro CHECK1 0
+    mova    m3, m0
+    pcmpgtd m3, m2
+    PMINSD  m0, m2, m6
+    mova    m6, m3
+    pand    m5, m3
+    pandn   m3, m1
+    por     m3, m5
+    mova    m1, m3
+%endmacro
+
+%macro CHECK2 0
+    paddd   m6, [pd_1]
+    pslld   m6, 30
+    paddd   m2, m6
+    mova    m3, m0
+    pcmpgtd m3, m2
+    PMINSD  m0, m2, m4
+    pand    m5, m3
+    pandn   m3, m1
+    por     m3, m5
+    mova    m1, m3
+%endmacro
+
+; This version of CHECK2 has 3 fewer instructions on sets older than SSE4 but I
+; am not sure whether it is any faster.  A rewrite or refactor of the filter
+; code should make it possible to eliminate the move intruction at the end.  It
+; exists to satisfy the expectation that the "score" values are in m1.
+
+; %macro CHECK2 0
+;     mova    m3, m0
+;     pcmpgtd m0, m2
+;     pand    m0, m6
+;     mova    m6, m0
+;     pand    m5, m6
+;     pand    m2, m0
+;     pandn   m6, m1
+;     pandn   m0, m3
+;     por     m6, m5
+;     por     m0, m2
+;     mova    m1, m6
+; %endmacro
+
+%macro LOAD 2
+    movh      %1, %2
+    punpcklwd %1, m7
+%endmacro
+
+%macro FILTER 3
+.loop%1:
+    pxor         m7, m7
+    LOAD         m0, [curq+t1]
+    LOAD         m1, [curq+t0]
+    LOAD         m2, [%2]
+    LOAD         m3, [%3]
+    mova         m4, m3
+    paddd        m3, m2
+    psrad        m3, 1
+    mova   [rsp+ 0], m0
+    mova   [rsp+16], m3
+    mova   [rsp+32], m1
+    psubd        m2, m4
+    PABS         m2, m4
+    LOAD         m3, [prevq+t1]
+    LOAD         m4, [prevq+t0]
+    psubd        m3, m0
+    psubd        m4, m1
+    PABS         m3, m5
+    PABS         m4, m5
+    paddd        m3, m4
+    psrld        m2, 1
+    psrld        m3, 1
+    PMAXSD       m2, m3, m6
+    LOAD         m3, [nextq+t1]
+    LOAD         m4, [nextq+t0]
+    psubd        m3, m0
+    psubd        m4, m1
+    PABS         m3, m5
+    PABS         m4, m5
+    paddd        m3, m4
+    psrld        m3, 1
+    PMAXSD       m2, m3, m6
+    mova   [rsp+48], m2
+
+    paddd        m1, m0
+    paddd        m0, m0
+    psubd        m0, m1
+    psrld        m1, 1
+    PABS         m0, m2
+
+    movu         m2, [curq+t1-1*2]
+    movu         m3, [curq+t0-1*2]
+    mova         m4, m2
+    psubusw      m2, m3
+    psubusw      m3, m4
+    PMAXUW       m2, m3
+%if mmsize == 16
+    mova         m3, m2
+    psrldq       m3, 4
+%else
+    mova         m3, m2
+    psrlq        m3, 32
+%endif
+    punpcklwd    m2, m7
+    punpcklwd    m3, m7
+    paddd        m0, m2
+    paddd        m0, m3
+    psubd        m0, [pd_1]
+
+    CHECK -2, 0
+    CHECK1
+    CHECK -3, 1
+    CHECK2
+    CHECK 0, -2
+    CHECK1
+    CHECK 1, -3
+    CHECK2
+
+    mova         m6, [rsp+48]
+    cmp   DWORD r8m, 2
+    jge .end%1
+    LOAD         m2, [%2+t1*2]
+    LOAD         m4, [%3+t1*2]
+    LOAD         m3, [%2+t0*2]
+    LOAD         m5, [%3+t0*2]
+    paddd        m2, m4
+    paddd        m3, m5
+    psrld        m2, 1
+    psrld        m3, 1
+    mova         m4, [rsp+ 0]
+    mova         m5, [rsp+16]
+    mova         m7, [rsp+32]
+    psubd        m2, m4
+    psubd        m3, m7
+    mova         m0, m5
+    psubd        m5, m4
+    psubd        m0, m7
+    mova         m4, m2
+    PMINSD       m2, m3, m7
+    PMAXSD       m3, m4, m7
+    PMAXSD       m2, m5, m7
+    PMINSD       m3, m5, m7
+    PMAXSD       m2, m0, m7
+    PMINSD       m3, m0, m7
+    pxor         m4, m4
+    PMAXSD       m6, m3, m7
+    psubd        m4, m2
+    PMAXSD       m6, m4, m7
+
+.end%1:
+    mova         m2, [rsp+16]
+    mova         m3, m2
+    psubd        m2, m6
+    paddd        m3, m6
+    PMAXSD       m1, m2, m7
+    PMINSD       m1, m3, m7
+    PACK         m1
+
+    movh     [dstq], m1
+    add        dstq, mmsize/2
+    add       prevq, mmsize/2
+    add        curq, mmsize/2
+    add       nextq, mmsize/2
+    sub   DWORD r4m, mmsize/4
+    jg .loop%1
+%endmacro
+
+%macro YADIF 0
+%if ARCH_X86_32
+cglobal yadif_filter_line_16bit, 4, 6, 8, 80, dst, prev, cur, next, w, \
+                                              prefs, mrefs, parity, mode
+%else
+cglobal yadif_filter_line_16bit, 4, 7, 8, 80, dst, prev, cur, next, w, \
+                                              prefs, mrefs, parity, mode
+%endif
+%if ARCH_X86_32
+    mov            r4, r5mp
+    mov            r5, r6mp
+    DECLARE_REG_TMP 4,5
+%else
+    movsxd         r5, DWORD r5m
+    movsxd         r6, DWORD r6m
+    DECLARE_REG_TMP 5,6
+%endif
+
+    cmp DWORD paritym, 0
+    je .parity0
+    FILTER 1, prevq, curq
+    jmp .ret
+
+.parity0:
+    FILTER 0, curq, nextq
+
+.ret:
+    RET
+%endmacro
+
+INIT_XMM sse4
+YADIF
+INIT_XMM ssse3
+YADIF
+INIT_XMM sse2
+YADIF
+%if ARCH_X86_32
+INIT_MMX mmxext
+YADIF
+%endif
diff --git a/libavfilter/yadif.h b/libavfilter/yadif.h
index 50fc856..3ddf005 100644
--- a/libavfilter/yadif.h
+++ b/libavfilter/yadif.h
@@ -49,21 +49,19 @@
 
     int frame_pending;
 
-    AVFilterBufferRef *cur;
-    AVFilterBufferRef *next;
-    AVFilterBufferRef *prev;
-    AVFilterBufferRef *out;
+    AVFrame *cur;
+    AVFrame *next;
+    AVFrame *prev;
+    AVFrame *out;
 
     /**
      * Required alignment for filter_line
      */
-    int req_align;
     void (*filter_line)(void *dst,
                         void *prev, void *cur, void *next,
                         int w, int prefs, int mrefs, int parity, int mode);
     void (*filter_edges)(void *dst, void *prev, void *cur, void *next,
-                         int w, int prefs, int mrefs, int parity, int mode,
-                         int l_edge);
+                         int w, int prefs, int mrefs, int parity, int mode);
 
     const AVPixFmtDescriptor *csp;
     int eof;
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 03d7e23..470e7f3 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -13,7 +13,6 @@
        cutils.o             \
        id3v1.o              \
        id3v2.o              \
-       log2_tab.o           \
        metadata.o           \
        mux.o                \
        options.o            \
@@ -21,7 +20,6 @@
        riff.o               \
        sdp.o                \
        seek.o               \
-       subtitles.o          \
        utils.o              \
 
 OBJS-$(CONFIG_NETWORK)                   += network.o
@@ -48,6 +46,7 @@
                                             rtpdec_xiph.o               \
                                             srtp.o
 OBJS-$(CONFIG_RTPENC_CHAIN)              += rtpenc_chain.o rtp.o
+OBJS-$(CONFIG_SHARED)                    += log2_tab.o
 
 # muxers/demuxers
 OBJS-$(CONFIG_A64_MUXER)                 += a64.o rawenc.o
@@ -69,11 +68,11 @@
 OBJS-$(CONFIG_ANM_DEMUXER)               += anm.o
 OBJS-$(CONFIG_APC_DEMUXER)               += apc.o
 OBJS-$(CONFIG_APE_DEMUXER)               += ape.o apetag.o img2.o
-OBJS-$(CONFIG_AQTITLE_DEMUXER)           += aqtitledec.o
+OBJS-$(CONFIG_AQTITLE_DEMUXER)           += aqtitledec.o subtitles.o
 OBJS-$(CONFIG_ASF_DEMUXER)               += asfdec.o asf.o asfcrypt.o \
                                             avlanguage.o
 OBJS-$(CONFIG_ASF_MUXER)                 += asfenc.o asf.o
-OBJS-$(CONFIG_ASS_DEMUXER)               += assdec.o
+OBJS-$(CONFIG_ASS_DEMUXER)               += assdec.o subtitles.o
 OBJS-$(CONFIG_ASS_MUXER)                 += assenc.o
 OBJS-$(CONFIG_AST_DEMUXER)               += ast.o astdec.o
 OBJS-$(CONFIG_AST_MUXER)                 += ast.o astenc.o
@@ -177,7 +176,7 @@
 OBJS-$(CONFIG_IV8_DEMUXER)               += iv8.o
 OBJS-$(CONFIG_IVF_DEMUXER)               += ivfdec.o
 OBJS-$(CONFIG_IVF_MUXER)                 += ivfenc.o
-OBJS-$(CONFIG_JACOSUB_DEMUXER)           += jacosubdec.o
+OBJS-$(CONFIG_JACOSUB_DEMUXER)           += jacosubdec.o subtitles.o
 OBJS-$(CONFIG_JACOSUB_MUXER)             += jacosubenc.o rawenc.o
 OBJS-$(CONFIG_JV_DEMUXER)                += jvdec.o
 OBJS-$(CONFIG_LATM_DEMUXER)              += rawdec.o
@@ -195,7 +194,7 @@
                                             flacenc_header.o avlanguage.o
 OBJS-$(CONFIG_MD5_MUXER)                 += md5enc.o
 OBJS-$(CONFIG_MGSTS_DEMUXER)             += mgsts.o
-OBJS-$(CONFIG_MICRODVD_DEMUXER)          += microdvddec.o
+OBJS-$(CONFIG_MICRODVD_DEMUXER)          += microdvddec.o subtitles.o
 OBJS-$(CONFIG_MICRODVD_MUXER)            += microdvdenc.o
 OBJS-$(CONFIG_MJPEG_DEMUXER)             += rawdec.o
 OBJS-$(CONFIG_MJPEG_MUXER)               += rawenc.o
@@ -224,8 +223,8 @@
 OBJS-$(CONFIG_MPEGTS_MUXER)              += mpegtsenc.o
 OBJS-$(CONFIG_MPEGVIDEO_DEMUXER)         += mpegvideodec.o rawdec.o
 OBJS-$(CONFIG_MPJPEG_MUXER)              += mpjpeg.o
-OBJS-$(CONFIG_MPL2_DEMUXER)              += mpl2dec.o
-OBJS-$(CONFIG_MPSUB_DEMUXER)             += mpsubdec.o
+OBJS-$(CONFIG_MPL2_DEMUXER)              += mpl2dec.o subtitles.o
+OBJS-$(CONFIG_MPSUB_DEMUXER)             += mpsubdec.o subtitles.o
 OBJS-$(CONFIG_MSNWC_TCP_DEMUXER)         += msnwc_tcp.o
 OBJS-$(CONFIG_MTV_DEMUXER)               += mtv.o
 OBJS-$(CONFIG_MVI_DEMUXER)               += mvi.o
@@ -296,7 +295,7 @@
 OBJS-$(CONFIG_PCM_U32LE_MUXER)           += pcmenc.o rawenc.o
 OBJS-$(CONFIG_PCM_U8_DEMUXER)            += pcmdec.o pcm.o
 OBJS-$(CONFIG_PCM_U8_MUXER)              += pcmenc.o rawenc.o
-OBJS-$(CONFIG_PJS_DEMUXER)               += pjsdec.o
+OBJS-$(CONFIG_PJS_DEMUXER)               += pjsdec.o subtitles.o
 OBJS-$(CONFIG_PMP_DEMUXER)               += pmpdec.o
 OBJS-$(CONFIG_PVA_DEMUXER)               += pva.o
 OBJS-$(CONFIG_PVF_DEMUXER)               += pvfdec.o pcm.o
@@ -304,7 +303,7 @@
 OBJS-$(CONFIG_R3D_DEMUXER)               += r3d.o
 OBJS-$(CONFIG_RAWVIDEO_DEMUXER)          += rawvideodec.o
 OBJS-$(CONFIG_RAWVIDEO_MUXER)            += rawenc.o
-OBJS-$(CONFIG_REALTEXT_DEMUXER)          += realtextdec.o
+OBJS-$(CONFIG_REALTEXT_DEMUXER)          += realtextdec.o subtitles.o
 OBJS-$(CONFIG_RL2_DEMUXER)               += rl2.o
 OBJS-$(CONFIG_RM_DEMUXER)                += rmdec.o rm.o rmsipr.o
 OBJS-$(CONFIG_RM_MUXER)                  += rmenc.o rm.o
@@ -330,7 +329,7 @@
                                             urldecode.o
 OBJS-$(CONFIG_RTSP_MUXER)                += rtsp.o rtspenc.o httpauth.o \
                                             urldecode.o
-OBJS-$(CONFIG_SAMI_DEMUXER)              += samidec.o
+OBJS-$(CONFIG_SAMI_DEMUXER)              += samidec.o subtitles.o
 OBJS-$(CONFIG_SAP_DEMUXER)               += sapdec.o
 OBJS-$(CONFIG_SAP_MUXER)                 += sapenc.o
 OBJS-$(CONFIG_SBG_DEMUXER)               += sbgdec.o
@@ -349,15 +348,15 @@
 OBJS-$(CONFIG_SOX_MUXER)                 += soxenc.o rawenc.o
 OBJS-$(CONFIG_SPDIF_DEMUXER)             += spdif.o spdifdec.o
 OBJS-$(CONFIG_SPDIF_MUXER)               += spdif.o spdifenc.o
-OBJS-$(CONFIG_SRT_DEMUXER)               += srtdec.o
+OBJS-$(CONFIG_SRT_DEMUXER)               += srtdec.o subtitles.o
 OBJS-$(CONFIG_SRT_MUXER)                 += srtenc.o
 OBJS-$(CONFIG_STR_DEMUXER)               += psxstr.o
-OBJS-$(CONFIG_SUBVIEWER1_DEMUXER)        += subviewer1dec.o
-OBJS-$(CONFIG_SUBVIEWER_DEMUXER)         += subviewerdec.o
+OBJS-$(CONFIG_SUBVIEWER1_DEMUXER)        += subviewer1dec.o subtitles.o
+OBJS-$(CONFIG_SUBVIEWER_DEMUXER)         += subviewerdec.o subtitles.o
 OBJS-$(CONFIG_SWF_DEMUXER)               += swfdec.o swf.o
 OBJS-$(CONFIG_SWF_MUXER)                 += swfenc.o swf.o
 OBJS-$(CONFIG_TAK_DEMUXER)               += takdec.o apetag.o img2.o rawdec.o
-OBJS-$(CONFIG_TEDCAPTIONS_DEMUXER)       += tedcaptionsdec.o
+OBJS-$(CONFIG_TEDCAPTIONS_DEMUXER)       += tedcaptionsdec.o subtitles.o
 OBJS-$(CONFIG_TEE_MUXER)                 += tee.o
 OBJS-$(CONFIG_THP_DEMUXER)               += thp.o
 OBJS-$(CONFIG_TIERTEXSEQ_DEMUXER)        += tiertexseq.o
@@ -373,9 +372,10 @@
 OBJS-$(CONFIG_VC1T_MUXER)                += vc1testenc.o
 OBJS-$(CONFIG_VIVO_DEMUXER)              += vivo.o
 OBJS-$(CONFIG_VMD_DEMUXER)               += sierravmd.o
+OBJS-$(CONFIG_VOBSUB_DEMUXER)            += subtitles.o # mpeg demuxer is in the dependencies
 OBJS-$(CONFIG_VOC_DEMUXER)               += vocdec.o voc.o
 OBJS-$(CONFIG_VOC_MUXER)                 += vocenc.o voc.o
-OBJS-$(CONFIG_VPLAYER_DEMUXER)           += vplayerdec.o
+OBJS-$(CONFIG_VPLAYER_DEMUXER)           += vplayerdec.o subtitles.o
 OBJS-$(CONFIG_VQF_DEMUXER)               += vqf.o
 OBJS-$(CONFIG_W64_DEMUXER)               += wavdec.o w64.o pcm.o
 OBJS-$(CONFIG_W64_MUXER)                 += wavenc.o w64.o
@@ -385,7 +385,7 @@
 OBJS-$(CONFIG_WEBM_MUXER)                += matroskaenc.o matroska.o \
                                             isom.o avc.o \
                                             flacenc_header.o avlanguage.o
-OBJS-$(CONFIG_WEBVTT_DEMUXER)            += webvttdec.o
+OBJS-$(CONFIG_WEBVTT_DEMUXER)            += webvttdec.o subtitles.o
 OBJS-$(CONFIG_WSAUD_DEMUXER)             += westwood_aud.o
 OBJS-$(CONFIG_WSVQA_DEMUXER)             += westwood_vqa.o
 OBJS-$(CONFIG_WTV_DEMUXER)               += wtvdec.o wtv.o asfdec.o asf.o asfcrypt.o \
@@ -405,6 +405,7 @@
 OBJS-$(CONFIG_LIBMODPLUG_DEMUXER)        += libmodplug.o
 OBJS-$(CONFIG_LIBNUT_DEMUXER)            += libnut.o
 OBJS-$(CONFIG_LIBNUT_MUXER)              += libnut.o
+OBJS-$(CONFIG_LIBQUVI_DEMUXER)           += libquvi.o
 OBJS-$(CONFIG_LIBRTMP)                   += librtmp.o
 
 # protocols I/O
diff --git a/libavformat/a64.c b/libavformat/a64.c
index c672fb6..786c75c 100644
--- a/libavformat/a64.c
+++ b/libavformat/a64.c
@@ -20,7 +20,6 @@
  */
 
 #include "libavcodec/avcodec.h"
-#include "libavcodec/a64enc.h"
 #include "libavcodec/bytestream.h"
 #include "avformat.h"
 #include "rawenc.h"
diff --git a/libavformat/adtsenc.c b/libavformat/adtsenc.c
index 14c72a8..60d7b07 100644
--- a/libavformat/adtsenc.c
+++ b/libavformat/adtsenc.c
@@ -158,7 +158,6 @@
         }
     }
     avio_write(pb, pkt->data, pkt->size);
-    avio_flush(pb);
 
     return 0;
 }
diff --git a/libavformat/aiffdec.c b/libavformat/aiffdec.c
index 8d466fa..62af3dc 100644
--- a/libavformat/aiffdec.c
+++ b/libavformat/aiffdec.c
@@ -147,9 +147,6 @@
         case AV_CODEC_ID_GSM:
             codec->block_align = 33;
             break;
-        case AV_CODEC_ID_QCELP:
-            codec->block_align = 35;
-            break;
         default:
             aiff->block_duration = 1;
             break;
@@ -192,7 +189,7 @@
 static int aiff_read_header(AVFormatContext *s)
 {
     int ret, size, filesize;
-    int64_t offset = 0;
+    int64_t offset = 0, position;
     uint32_t tag;
     unsigned version = AIFF_C_VERSION1;
     AVIOContext *pb = s->pb;
@@ -236,6 +233,7 @@
                 goto got_sound;
             break;
         case MKTAG('I', 'D', '3', ' '):
+            position = avio_tell(pb);
             ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
             if (id3v2_extra_meta)
                 if ((ret = ff_id3v2_parse_apic(s, &id3v2_extra_meta)) < 0) {
@@ -243,6 +241,8 @@
                     return ret;
                 }
             ff_id3v2_free_extra_meta(&id3v2_extra_meta);
+            if (position + size > avio_tell(pb))
+                avio_skip(pb, position + size - avio_tell(pb));
             break;
         case MKTAG('F', 'V', 'E', 'R'):     /* Version chunk */
             version = avio_rb32(pb);
@@ -283,6 +283,21 @@
             if (st->codec->codec_id == AV_CODEC_ID_QDM2 && size>=12*4 && !st->codec->block_align) {
                 st->codec->block_align = AV_RB32(st->codec->extradata+11*4);
                 aiff->block_duration = AV_RB32(st->codec->extradata+9*4);
+            } else if (st->codec->codec_id == AV_CODEC_ID_QCELP) {
+                char rate = 0;
+                if (size >= 25)
+                    rate = st->codec->extradata[24];
+                switch (rate) {
+                case 'H': // RATE_HALF
+                    st->codec->block_align = 17;
+                    break;
+                case 'F': // RATE_FULL
+                default:
+                    st->codec->block_align = 35;
+                }
+                aiff->block_duration = 160;
+                st->codec->bit_rate = st->codec->sample_rate * (st->codec->block_align << 3) /
+                                      aiff->block_duration;
             }
             break;
         case MKTAG('C','H','A','N'):
@@ -329,7 +344,7 @@
         return AVERROR_EOF;
 
     /* Now for that packet */
-    if (st->codec->block_align >= 33) // GSM, QCLP, IMA4
+    if (st->codec->block_align >= 17) // GSM, QCLP, IMA4
         size = st->codec->block_align;
     else
         size = (MAX_SIZE / st->codec->block_align) * st->codec->block_align;
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index ec91b24..80c53f6 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -275,7 +275,7 @@
     REGISTER_DEMUXER (TTA,              tta);
     REGISTER_DEMUXER (TXD,              txd);
     REGISTER_DEMUXER (TTY,              tty);
-    REGISTER_DEMUXER (VC1,              vc1);
+    REGISTER_MUXDEMUX(VC1,              vc1);
     REGISTER_MUXDEMUX(VC1T,             vc1t);
     REGISTER_DEMUXER (VIVO,             vivo);
     REGISTER_DEMUXER (VMD,              vmd);
@@ -300,9 +300,6 @@
     REGISTER_MUXDEMUX(YUV4MPEGPIPE,     yuv4mpegpipe);
 
     /* protocols */
-#if FF_API_APPLEHTTP_PROTO
-    REGISTER_PROTOCOL(APPLEHTTP,        applehttp);
-#endif
     REGISTER_PROTOCOL(BLURAY,           bluray);
     REGISTER_PROTOCOL(CACHE,            cache);
     REGISTER_PROTOCOL(CONCAT,           concat);
@@ -336,6 +333,7 @@
     /* external libraries */
     REGISTER_DEMUXER (LIBMODPLUG,       libmodplug);
     REGISTER_MUXDEMUX(LIBNUT,           libnut);
+    REGISTER_DEMUXER (LIBQUVI,          libquvi);
     REGISTER_PROTOCOL(LIBRTMP,          librtmp);
     REGISTER_PROTOCOL(LIBRTMPE,         librtmpe);
     REGISTER_PROTOCOL(LIBRTMPS,         librtmps);
diff --git a/libavformat/amr.c b/libavformat/amr.c
index 07ab1ba..7201ac3 100644
--- a/libavformat/amr.c
+++ b/libavformat/amr.c
@@ -56,7 +56,6 @@
 static int amr_write_packet(AVFormatContext *s, AVPacket *pkt)
 {
     avio_write(s->pb, pkt->data, pkt->size);
-    avio_flush(s->pb);
     return 0;
 }
 #endif /* CONFIG_AMR_MUXER */
diff --git a/libavformat/anm.c b/libavformat/anm.c
index f93c57e..1c58575 100644
--- a/libavformat/anm.c
+++ b/libavformat/anm.c
@@ -85,7 +85,7 @@
 
     avio_skip(pb, 4); /* magic number */
     if (avio_rl16(pb) != MAX_PAGES) {
-        av_log_ask_for_sample(s, "max_pages != " AV_STRINGIFY(MAX_PAGES) "\n");
+        avpriv_request_sample(s, "max_pages != " AV_STRINGIFY(MAX_PAGES));
         return AVERROR_PATCHWELCOME;
     }
 
@@ -134,9 +134,9 @@
     /* color cycling and palette data */
     st->codec->extradata_size = 16*8 + 4*256;
     st->codec->extradata      = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
-    if (!st->codec->extradata)
+    if (!st->codec->extradata) {
         return AVERROR(ENOMEM);
-
+    }
     ret = avio_read(pb, st->codec->extradata, st->codec->extradata_size);
     if (ret < 0)
         return ret;
@@ -155,14 +155,15 @@
 
     /* find page of first frame */
     anm->page = find_record(anm, 0);
-    if (anm->page < 0)
+    if (anm->page < 0) {
         return anm->page;
+    }
 
     anm->record = -1;
     return 0;
 
 invalid:
-    av_log_ask_for_sample(s, NULL);
+    avpriv_request_sample(s, "Invalid header element");
     return AVERROR_PATCHWELCOME;
 }
 
diff --git a/libavformat/ape.c b/libavformat/ape.c
index 551513d..e2b8ada 100644
--- a/libavformat/ape.c
+++ b/libavformat/ape.c
@@ -28,7 +28,7 @@
 #include "apetag.h"
 
 /* The earliest and latest file formats supported by this library */
-#define APE_MIN_VERSION 3950
+#define APE_MIN_VERSION 3800
 #define APE_MAX_VERSION 3990
 
 #define MAC_FORMAT_FLAG_8_BIT                 1 // is 8-bit [OBSOLETE]
@@ -81,6 +81,7 @@
 
     /* Seektable */
     uint32_t *seektable;
+    uint8_t  *bittable;
 } APEContext;
 
 static int ape_probe(AVProbeData * p)
@@ -128,9 +129,13 @@
     } else {
         for (i = 0; i < ape_ctx->seektablelength / sizeof(uint32_t); i++) {
             if (i < ape_ctx->totalframes - 1) {
-                av_log(s, AV_LOG_DEBUG, "%8d   %"PRIu32" (%"PRIu32" bytes)\n",
+                av_log(s, AV_LOG_DEBUG, "%8d   %"PRIu32" (%"PRIu32" bytes)",
                        i, ape_ctx->seektable[i],
                        ape_ctx->seektable[i + 1] - ape_ctx->seektable[i]);
+                if (ape_ctx->bittable)
+                    av_log(s, AV_LOG_DEBUG, " + %2d bits\n",
+                           ape_ctx->bittable[i]);
+                av_log(s, AV_LOG_DEBUG, "\n");
             } else {
                 av_log(s, AV_LOG_DEBUG, "%8d   %"PRIu32"\n", i, ape_ctx->seektable[i]);
             }
@@ -263,6 +268,8 @@
     if(!ape->frames)
         return AVERROR(ENOMEM);
     ape->firstframe   = ape->junklength + ape->descriptorlength + ape->headerlength + ape->seektablelength + ape->wavheaderlength;
+    if (ape->fileversion < 3810)
+        ape->firstframe += ape->totalframes;
     ape->currentframe = 0;
 
 
@@ -276,6 +283,13 @@
             return AVERROR(ENOMEM);
         for (i = 0; i < ape->seektablelength / sizeof(uint32_t); i++)
             ape->seektable[i] = avio_rl32(pb);
+        if (ape->fileversion < 3810) {
+            ape->bittable = av_malloc(ape->totalframes);
+            if (!ape->bittable)
+                return AVERROR(ENOMEM);
+            for (i = 0; i < ape->totalframes; i++)
+                ape->bittable[i] = avio_r8(pb);
+        }
     }else{
         av_log(s, AV_LOG_ERROR, "Missing seektable\n");
         return AVERROR_INVALIDDATA;
@@ -309,7 +323,14 @@
         }
         ape->frames[i].size = (ape->frames[i].size + 3) & ~3;
     }
-
+    if (ape->fileversion < 3810) {
+        for (i = 0; i < ape->totalframes; i++) {
+            if (i < ape->totalframes - 1 && ape->bittable[i + 1])
+                ape->frames[i].size += 4;
+            ape->frames[i].skip <<= 3;
+            ape->frames[i].skip  += ape->bittable[i];
+        }
+    }
 
     ape_dumpinfo(s, ape);
 
@@ -412,6 +433,7 @@
 
     av_freep(&ape->frames);
     av_freep(&ape->seektable);
+    av_freep(&ape->bittable);
     return 0;
 }
 
diff --git a/libavformat/aqtitledec.c b/libavformat/aqtitledec.c
index 325946c..a78fd81 100644
--- a/libavformat/aqtitledec.c
+++ b/libavformat/aqtitledec.c
@@ -70,7 +70,7 @@
 
         line[strcspn(line, "\r\n")] = 0;
 
-        if (sscanf(line, "-->> %"PRId64, &frame) == 1) {
+        if (sscanf(line, "-->> %"SCNd64, &frame) == 1) {
             new_event = 1;
             pos = avio_tell(s->pb);
             if (sub) {
diff --git a/libavformat/asfdec.c b/libavformat/asfdec.c
index 233b6ca..1d7f26c 100644
--- a/libavformat/asfdec.c
+++ b/libavformat/asfdec.c
@@ -278,11 +278,12 @@
 {
     char *value;
     int64_t off = avio_tell(s->pb);
+#define LEN 22
 
-    if ((unsigned)len >= (UINT_MAX - 1) / 2)
+    if ((unsigned)len >= (UINT_MAX - LEN) / 2)
         return;
 
-    value = av_malloc(2 * len + 1);
+    value = av_malloc(2 * len + LEN);
     if (!value)
         goto finish;
 
@@ -302,7 +303,7 @@
         goto finish;
     } else if (type > 1 && type <= 5) {  // boolean or DWORD or QWORD or WORD
         uint64_t num = get_value(s->pb, type, type2_size);
-        snprintf(value, len, "%"PRIu64, num);
+        snprintf(value, LEN, "%"PRIu64, num);
     } else if (type == 6) { // (don't) handle GUID
         av_log(s, AV_LOG_DEBUG, "Unsupported GUID value in tag %s.\n", key);
         goto finish;
@@ -1011,6 +1012,7 @@
         asf->packet_obj_size = avio_rl32(pb);
         if (asf->packet_obj_size >= (1 << 24) || asf->packet_obj_size <= 0) {
             av_log(s, AV_LOG_ERROR, "packet_obj_size invalid\n");
+            asf->packet_obj_size = 0;
             return AVERROR_INVALIDDATA;
         }
         asf->packet_frag_timestamp = avio_rl32(pb); // timestamp
@@ -1280,9 +1282,10 @@
                            asf_st->ds_span);
                 } else {
                     /* packet descrambling */
-                    uint8_t *newdata = av_malloc(asf_st->pkt.size +
-                                                 FF_INPUT_BUFFER_PADDING_SIZE);
-                    if (newdata) {
+                    AVBufferRef *buf = av_buffer_alloc(asf_st->pkt.size +
+                                                       FF_INPUT_BUFFER_PADDING_SIZE);
+                    if (buf) {
+                        uint8_t *newdata = buf->data;
                         int offset = 0;
                         memset(newdata + asf_st->pkt.size, 0,
                                FF_INPUT_BUFFER_PADDING_SIZE);
@@ -1298,13 +1301,18 @@
                                    asf_st->ds_chunk_size);
                             offset += asf_st->ds_chunk_size;
                         }
-                        av_free(asf_st->pkt.data);
-                        asf_st->pkt.data = newdata;
+                        av_buffer_unref(&asf_st->pkt.buf);
+                        asf_st->pkt.buf  = buf;
+                        asf_st->pkt.data = buf->data;
                     }
                 }
             }
             asf_st->frag_offset         = 0;
             *pkt                        = asf_st->pkt;
+#if FF_API_DESTRUCT_PACKET
+            asf_st->pkt.destruct        = NULL;
+#endif
+            asf_st->pkt.buf             = 0;
             asf_st->pkt.size            = 0;
             asf_st->pkt.data            = 0;
             asf_st->pkt.side_data_elems = 0;
diff --git a/libavformat/asfenc.c b/libavformat/asfenc.c
index f3aec9c..6daf238 100644
--- a/libavformat/asfenc.c
+++ b/libavformat/asfenc.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/avassert.h"
 #include "libavutil/dict.h"
 #include "avformat.h"
 #include "avio_internal.h"
@@ -220,7 +221,7 @@
 
 void ff_put_guid(AVIOContext *s, const ff_asf_guid *g)
 {
-    assert(sizeof(*g) == 16);
+    av_assert0(sizeof(*g) == 16);
     avio_write(s, *g, sizeof(*g));
 }
 
@@ -600,7 +601,7 @@
     padsize -= PACKET_HEADER_MIN_SIZE;
     if (asf->multi_payloads_present)
         padsize--;
-    assert(padsize >= 0);
+    av_assert0(padsize >= 0);
 
     avio_w8(pb, ASF_PACKET_ERROR_CORRECTION_FLAGS);
     for (i = 0; i < ASF_PACKET_ERROR_CORRECTION_DATA_SIZE; i++)
@@ -639,7 +640,7 @@
     ASFContext *asf = s->priv_data;
     int packet_hdr_size, packet_filled_size;
 
-    assert(asf->packet_timestamp_end >= asf->packet_timestamp_start);
+    av_assert0(asf->packet_timestamp_end >= asf->packet_timestamp_start);
 
     if (asf->is_streamed)
         put_chunk(s, 0x4424, s->packet_size, 0);
@@ -651,7 +652,7 @@
                                                asf->packet_size_left);
 
     packet_filled_size = PACKET_SIZE - asf->packet_size_left;
-    assert(packet_hdr_size <= asf->packet_size_left);
+    av_assert0(packet_hdr_size <= asf->packet_size_left);
     memset(asf->packet_buf + packet_filled_size, 0, asf->packet_size_left);
 
     avio_write(s->pb, asf->packet_buf, s->packet_size - packet_hdr_size);
@@ -804,7 +805,7 @@
         flags &= ~AV_PKT_FLAG_KEY;
 
     pts = (pkt->pts != AV_NOPTS_VALUE) ? pkt->pts : pkt->dts;
-    assert(pts != AV_NOPTS_VALUE);
+    av_assert0(pts != AV_NOPTS_VALUE);
     pts *= 10000;
     asf->duration = FFMAX(asf->duration, pts + pkt->duration * 10000);
 
diff --git a/libavformat/assenc.c b/libavformat/assenc.c
index bda507d..f2e904a 100644
--- a/libavformat/assenc.c
+++ b/libavformat/assenc.c
@@ -20,9 +20,11 @@
  */
 
 #include "avformat.h"
+#include "internal.h"
 
 typedef struct ASSContext{
     unsigned int extra_index;
+    int write_ts; // 0: ssa (timing in payload), 1: ass (matroska like)
 }ASSContext;
 
 static int write_header(AVFormatContext *s)
@@ -31,10 +33,13 @@
     AVCodecContext *avctx= s->streams[0]->codec;
     uint8_t *last= NULL;
 
-    if(s->nb_streams != 1 || avctx->codec_id != AV_CODEC_ID_SSA){
+    if (s->nb_streams != 1 || (avctx->codec_id != AV_CODEC_ID_SSA &&
+                               avctx->codec_id != AV_CODEC_ID_ASS)) {
         av_log(s, AV_LOG_ERROR, "Exactly one ASS/SSA stream is needed.\n");
         return -1;
     }
+    ass->write_ts = avctx->codec_id == AV_CODEC_ID_ASS;
+    avpriv_set_pts_info(s->streams[0], 64, 1, 100);
 
     while(ass->extra_index < avctx->extradata_size){
         uint8_t *p  = avctx->extradata + ass->extra_index;
@@ -57,10 +62,31 @@
 
 static int write_packet(AVFormatContext *s, AVPacket *pkt)
 {
-    avio_write(s->pb, pkt->data, pkt->size);
+    ASSContext *ass = s->priv_data;
 
-    avio_flush(s->pb);
+    if (ass->write_ts) {
+        long int layer;
+        char *p;
+        int64_t start = pkt->pts;
+        int64_t end   = start + pkt->duration;
+        int hh1, mm1, ss1, ms1;
+        int hh2, mm2, ss2, ms2;
 
+        p = pkt->data + strcspn(pkt->data, ",") + 1; // skip ReadOrder
+        layer = strtol(p, &p, 10);
+        if (*p == ',')
+            p++;
+        hh1 = (int)(start / 360000);    mm1 = (int)(start / 6000) % 60;
+        hh2 = (int)(end   / 360000);    mm2 = (int)(end   / 6000) % 60;
+        ss1 = (int)(start / 100) % 60;  ms1 = (int)(start % 100);
+        ss2 = (int)(end   / 100) % 60;  ms2 = (int)(end   % 100);
+        if (hh1 > 9) hh1 = 9, mm1 = 59, ss1 = 59, ms1 = 99;
+        if (hh2 > 9) hh2 = 9, mm2 = 59, ss2 = 59, ms2 = 99;
+        avio_printf(s->pb, "Dialogue: %ld,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
+                    layer, hh1, mm1, ss1, ms1, hh2, mm2, ss2, ms2, p);
+    } else {
+        avio_write(s->pb, pkt->data, pkt->size);
+    }
     return 0;
 }
 
diff --git a/libavformat/astdec.c b/libavformat/astdec.c
index fb5a34f..8862744 100644
--- a/libavformat/astdec.c
+++ b/libavformat/astdec.c
@@ -50,7 +50,7 @@
 
     depth = avio_rb16(s->pb);
     if (depth != 16) {
-        av_log_ask_for_sample(s, "unsupported depth %d\n", depth);
+        avpriv_request_sample(s, "depth %d", depth);
         return AVERROR_INVALIDDATA;
     }
 
diff --git a/libavformat/au.c b/libavformat/au.c
index db2ab27..22004d0 100644
--- a/libavformat/au.c
+++ b/libavformat/au.c
@@ -2,6 +2,8 @@
  * AU muxer and demuxer
  * Copyright (c) 2001 Fabrice Bellard
  *
+ * first version by Francois Revol <revol@free.fr>
+ *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
@@ -20,8 +22,6 @@
  */
 
 /*
- * First version by Francois Revol revol@free.fr
- *
  * Reference documents:
  * http://www.opengroup.org/public/pubs/external/auformat.html
  * http://www.goice.co.jp/member/mo/formats/au.html
@@ -96,13 +96,13 @@
     codec = ff_codec_get_id(codec_au_tags, id);
 
     if (codec == AV_CODEC_ID_NONE) {
-        av_log_ask_for_sample(s, "unknown or unsupported codec tag: %u\n", id);
+        avpriv_request_sample(s, "unknown or unsupported codec tag: %u", id);
         return AVERROR_PATCHWELCOME;
     }
 
     bps = av_get_bits_per_sample(codec);
     if (!bps) {
-        av_log_ask_for_sample(s, "could not determine bits per sample\n");
+        avpriv_request_sample(s, "Unknown bits per sample");
         return AVERROR_PATCHWELCOME;
     }
 
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 9b72279..80d693a 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -158,9 +158,9 @@
  * information will be in AVStream.time_base units, i.e. it has to be
  * multiplied by the timebase to convert them to seconds.
  *
- * If AVPacket.destruct is set on the returned packet, then the packet is
+ * If AVPacket.buf is set on the returned packet, then the packet is
  * allocated dynamically and the user may keep it indefinitely.
- * Otherwise, if AVPacket.destruct is NULL, the packet data is backed by a
+ * Otherwise, if AVPacket.buf is NULL, the packet data is backed by a
  * static storage somewhere inside the demuxer and the packet is only valid
  * until the next av_read_frame() call or closing the file. If the caller
  * requires a longer lifetime, av_dup_packet() will make an av_malloc()ed copy
@@ -207,10 +207,6 @@
 #include "avio.h"
 #include "libavformat/version.h"
 
-#if FF_API_AV_GETTIME
-#include "libavutil/time.h"
-#endif
-
 struct AVFormatContext;
 
 
@@ -663,17 +659,6 @@
      *             not actually used for encoding.
      */
     AVCodecContext *codec;
-#if FF_API_R_FRAME_RATE
-    /**
-     * Real base framerate of the stream.
-     * This is the lowest framerate with which all timestamps can be
-     * represented accurately (it is the least common multiple of all
-     * framerates in the stream). Note, this value is just a guess!
-     * For example, if the time base is 1/90000 and all frames have either
-     * approximately 3600 or 1800 timer ticks, then r_frame_rate will be 50/1.
-     */
-    AVRational r_frame_rate;
-#endif
     void *priv_data;
 
     /**
@@ -738,6 +723,19 @@
      */
     AVPacket attached_pic;
 
+    /**
+     * Real base framerate of the stream.
+     * This is the lowest framerate with which all timestamps can be
+     * represented accurately (it is the least common multiple of all
+     * framerates in the stream). Note, this value is just a guess!
+     * For example, if the time base is 1/90000 and all frames have either
+     * approximately 3600 or 1800 timer ticks, then r_frame_rate will be 50/1.
+     *
+     * Code outside avformat should access this field using:
+     * av_stream_get/set_r_frame_rate(stream)
+     */
+    AVRational r_frame_rate;
+
     /*****************************************************************
      * All fields below this line are not part of the public API. They
      * may not be used outside of libavformat and can be changed and
@@ -759,6 +757,8 @@
         int64_t codec_info_duration_fields;
         int found_decoder;
 
+        int64_t last_duration;
+
         /**
          * Those are used for average framerate estimation.
          */
@@ -873,6 +873,9 @@
 
 } AVStream;
 
+AVRational av_stream_get_r_frame_rate(const AVStream *s);
+void       av_stream_set_r_frame_rate(AVStream *s, AVRational r);
+
 #define AV_PROGRAM_RUNNING 1
 
 /**
@@ -1164,11 +1167,11 @@
     int use_wallclock_as_timestamps;
 
     /**
-     * Avoids negative timestamps during muxing
+     * Avoid negative timestamps during muxing.
      *  0 -> allow negative timestamps
      *  1 -> avoid negative timestamps
      * -1 -> choose automatically (default)
-     * Note, this is only works when interleave_packet_per_dts is in use
+     * Note, this only works when interleave_packet_per_dts is in use.
      * - encoding: Set by user via AVOptions (NO direct access)
      * - decoding: unused
      */
@@ -1210,6 +1213,13 @@
      */
     int seek2any;
 
+    /**
+     * Flush the I/O context after each packet.
+     * - encoding: Set by user via AVOptions (NO direct access)
+     * - decoding: unused
+     */
+    int flush_packets;
+
     /*****************************************************************
      * All fields below this line are not part of the public API. They
      * may not be used outside of libavformat and can be changed and
@@ -1247,6 +1257,14 @@
      */
 #define RAW_PACKET_BUFFER_SIZE 2500000
     int raw_packet_buffer_remaining_size;
+
+    /**
+     * IO repositioned flag.
+     * This is set by avformat when the underlaying IO context read pointer
+     * is repositioned, for example when doing byte based seeking.
+     * Demuxers can use the flag to detect such changes.
+     */
+    int io_repositioned;
 } AVFormatContext;
 
 /**
@@ -1603,7 +1621,7 @@
  * omit invalid data between valid frames so as to give the decoder the maximum
  * information possible for decoding.
  *
- * If pkt->destruct is NULL, then the packet is valid until the next
+ * If pkt->buf is NULL, then the packet is valid until the next
  * av_read_frame() or until av_close_input_file(). Otherwise the packet is valid
  * indefinitely. In both cases the packet must be freed with
  * av_free_packet when it is no longer needed. For video, the packet contains
@@ -1775,10 +1793,10 @@
  * demuxer level.
  *
  * @param s media file handle
- * @param pkt The packet containing the data to be written. Libavformat takes
- * ownership of the data and will free it when it sees fit using the packet's
- * @ref AVPacket.destruct "destruct" field. The caller must not access the data
- * after this function returns, as it may already be freed.
+ * @param pkt The packet containing the data to be written. pkt->buf must be set
+ * to a valid AVBufferRef describing the packet data. Libavformat takes
+ * ownership of this reference and will unref it when it sees fit. The caller
+ * must not access the data through this reference after this function returns.
  * This can be NULL (at any time, not just at the end), to flush the
  * interleaving queues.
  * Packet's @ref AVPacket.stream_index "stream_index" field must be set to the
@@ -1792,16 +1810,6 @@
  */
 int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt);
 
-#if FF_API_INTERLEAVE_PACKET
-/**
- * @deprecated this function was never meant to be called by the user
- * programs.
- */
-attribute_deprecated
-int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
-                                 AVPacket *pkt, int flush);
-#endif
-
 /**
  * Write the stream trailer to an output media file and free the
  * file private data.
@@ -2108,6 +2116,16 @@
 AVRational av_guess_sample_aspect_ratio(AVFormatContext *format, AVStream *stream, AVFrame *frame);
 
 /**
+ * Guess the frame rate, based on both the container and codec information.
+ *
+ * @param ctx the format context which the stream is part of
+ * @param stream the stream which the frame is part of
+ * @param frame the frame for which the frame rate should be determined, may be NULL
+ * @return the guessed (valid) frame rate, 0/1 if no idea
+ */
+AVRational av_guess_frame_rate(AVFormatContext *ctx, AVStream *stream, AVFrame *frame);
+
+/**
  * Check if the stream st contained in s is matched by the stream specifier
  * spec.
  *
@@ -2123,7 +2141,7 @@
 int avformat_match_stream_specifier(AVFormatContext *s, AVStream *st,
                                     const char *spec);
 
-void avformat_queue_attached_pictures(AVFormatContext *s);
+int avformat_queue_attached_pictures(AVFormatContext *s);
 
 
 /**
diff --git a/libavformat/avidec.c b/libavformat/avidec.c
index 00ac3de..3105d33 100644
--- a/libavformat/avidec.c
+++ b/libavformat/avidec.c
@@ -62,6 +62,7 @@
     int64_t  riff_end;
     int64_t  movi_end;
     int64_t  fsize;
+    int64_t io_fsize;
     int64_t movi_list;
     int64_t last_pkt_pos;
     int index_loaded;
@@ -373,7 +374,7 @@
 
     av_log(avi, AV_LOG_DEBUG, "use odml:%d\n", avi->use_odml);
 
-    avi->fsize = avio_size(pb);
+    avi->io_fsize = avi->fsize = avio_size(pb);
     if(avi->fsize<=0 || avi->fsize < avi->riff_end)
         avi->fsize= avi->riff_end == 8 ? INT64_MAX : avi->riff_end;
 
@@ -569,8 +570,13 @@
             default:
                 av_log(s, AV_LOG_INFO, "unknown stream type %X\n", tag1);
             }
-            if(ast->sample_size == 0)
+            if(ast->sample_size == 0) {
                 st->duration = st->nb_frames;
+                if (st->duration > 0 && avi->io_fsize > 0 && avi->riff_end > avi->io_fsize) {
+                    av_log(s, AV_LOG_DEBUG, "File is truncated adjusting duration\n");
+                    st->duration = av_rescale(st->duration, avi->io_fsize, avi->riff_end);
+                }
+            }
             ast->frame_offset= ast->cum_len;
             avio_skip(pb, size - 12 * 4);
             break;
@@ -938,7 +944,7 @@
         n= get_stream_idx(d+2);
         av_dlog(s, "%X %X %X %X %X %X %X %X %"PRId64" %u %d\n",
                 d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], i, size, n);
-        if(i + (uint64_t)size > avi->fsize || d[0] > 127)
+        if(i*(avi->io_fsize>0) + (uint64_t)size > avi->fsize || d[0] > 127)
             continue;
 
         //parse ix##
@@ -1057,7 +1063,9 @@
     AVIContext *avi = s->priv_data;
     AVIOContext *pb = s->pb;
     int err;
+#if FF_API_DESTRUCT_PACKET
     void* dstr;
+#endif
 
     if (CONFIG_DV_DEMUXER && avi->dv_demux) {
         int size = avpriv_dv_get_packet(avi->dv_demux, pkt);
@@ -1162,10 +1170,16 @@
         }
 
         if (CONFIG_DV_DEMUXER && avi->dv_demux) {
+            AVBufferRef *avbuf = pkt->buf;
+#if FF_API_DESTRUCT_PACKET
             dstr = pkt->destruct;
+#endif
             size = avpriv_dv_produce_packet(avi->dv_demux, pkt,
                                     pkt->data, pkt->size, pkt->pos);
+#if FF_API_DESTRUCT_PACKET
             pkt->destruct = dstr;
+#endif
+            pkt->buf = avbuf;
             pkt->flags |= AV_PKT_FLAG_KEY;
             if (size < 0)
                 av_free_packet(pkt);
diff --git a/libavformat/avienc.c b/libavformat/avienc.c
index 15f0794..f2fa9dc 100644
--- a/libavformat/avienc.c
+++ b/libavformat/avienc.c
@@ -57,7 +57,7 @@
 
 typedef struct  {
     int64_t frames_hdr_strm;
-    int audio_strm_length;
+    int64_t audio_strm_length;
     int packet_count;
     int entry;
 
@@ -587,7 +587,6 @@
     if (size & 1)
         avio_w8(pb, 0);
 
-    avio_flush(pb);
     return 0;
 }
 
diff --git a/libavformat/avio.h b/libavformat/avio.h
index 17b341d..5bdbc62 100644
--- a/libavformat/avio.h
+++ b/libavformat/avio.h
@@ -120,26 +120,32 @@
      * max filesize, used to limit allocations
      * This field is internal to libavformat and access from outside is not allowed.
      */
-     int64_t maxsize;
+    int64_t maxsize;
 
-     /**
-      * avio_read and avio_write should if possible be satisfied directly
-      * instead of going through a buffer, and avio_seek will always
-      * call the underlying seek function directly.
-      */
-     int direct;
+    /**
+     * avio_read and avio_write should if possible be satisfied directly
+     * instead of going through a buffer, and avio_seek will always
+     * call the underlying seek function directly.
+     */
+    int direct;
 
     /**
      * Bytes read statistic
      * This field is internal to libavformat and access from outside is not allowed.
      */
-     int64_t bytes_read;
+    int64_t bytes_read;
 
     /**
      * seek statistic
      * This field is internal to libavformat and access from outside is not allowed.
      */
-     int seek_count;
+    int seek_count;
+
+    /**
+     * writeout statistic
+     * This field is internal to libavformat and access from outside is not allowed.
+     */
+    int writeout_count;
 } AVIOContext;
 
 /* unbuffered I/O */
@@ -217,8 +223,8 @@
 
 /**
  * Oring this flag as into the "whence" parameter to a seek function causes it to
- * seek by any means (like reopening and linear reading) or other normally unreasonble
- * means that can be extreemly slow.
+ * seek by any means (like reopening and linear reading) or other normally unreasonable
+ * means that can be extremely slow.
  * This may be ignored by the seek code.
  */
 #define AVSEEK_FORCE 0x20000
diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
index 7a73a17..966e0e6 100644
--- a/libavformat/aviobuf.c
+++ b/libavformat/aviobuf.c
@@ -131,6 +131,7 @@
             s->error = ret;
         }
     }
+    s->writeout_count ++;
     s->pos += len;
 }
 
@@ -827,7 +828,9 @@
     avio_flush(s);
     h = s->opaque;
     av_freep(&s->buffer);
-    if (!s->write_flag)
+    if (s->write_flag)
+        av_log(s, AV_LOG_DEBUG, "Statistics: %d seeks, %d writeouts\n", s->seek_count, s->writeout_count);
+    else
         av_log(s, AV_LOG_DEBUG, "Statistics: %"PRId64" bytes read, %d seeks\n", s->bytes_read, s->seek_count);
     av_free(s);
     return ffurl_close(h);
diff --git a/libavformat/avisynth.c b/libavformat/avisynth.c
index 1e4dee1..a5a4fcc 100644
--- a/libavformat/avisynth.c
+++ b/libavformat/avisynth.c
@@ -1,9 +1,8 @@
 /*
- * AVISynth support
- * Copyright (c) 2006 DivX, Inc.
+ * Avi/AvxSynth support
+ * Copyright (c) 2012 AvxSynth Team.
  *
- * This file is part of FFmpeg.
- *
+ * 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
@@ -21,214 +20,617 @@
 
 #include "avformat.h"
 #include "internal.h"
-#include "riff.h"
+#include "libavcodec/internal.h"
 
-#include <windows.h>
-#include <vfw.h>
+// Enable function pointer definitions for runtime loading.
+#define AVSC_NO_DECLSPEC
 
-typedef struct {
-  PAVISTREAM handle;
-  AVISTREAMINFO info;
-  DWORD read;
-  LONG chunck_size;
-  LONG chunck_samples;
-} AVISynthStream;
+// Shut up ffmpeg error messages.
+// avisynth_c.h contains inline functions that call these functions.
+#undef malloc
+#undef free
+#undef printf
 
-typedef struct {
-  PAVIFILE file;
-  AVISynthStream *streams;
-  int nb_streams;
-  int next_stream;
-} AVISynthContext;
+// Platform-specific directives for AviSynth vs AvxSynth.
+#ifdef _WIN32
+  #include <windows.h>
+  #undef EXTERN_C
+  #include "compat/avisynth/avisynth_c.h"
+  #define AVISYNTH_LIB "avisynth"
+#else
+  #include <dlfcn.h>
+  #include "compat/avisynth/avxsynth_c.h"
+   #if defined (__APPLE__)
+     #define AVISYNTH_LIB "libavxsynth.dylib"
+   #else
+     #define AVISYNTH_LIB "libavxsynth.so"
+   #endif
 
-static int avisynth_read_header(AVFormatContext *s)
-{
-  AVISynthContext *avs = s->priv_data;
-  HRESULT res;
-  AVIFILEINFO info;
-  DWORD id;
-  AVStream *st;
-  AVISynthStream *stream;
-  wchar_t filename_wchar[1024] = { 0 };
-  char filename_char[1024] = { 0 };
-
-  AVIFileInit();
-
-  /* avisynth can't accept UTF-8 filename */
-  MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wchar, 1024);
-  WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wchar, -1, filename_char, 1024, NULL, NULL);
-  res = AVIFileOpen(&avs->file, filename_char, OF_READ|OF_SHARE_DENY_WRITE, NULL);
-  if (res != S_OK)
-    {
-      av_log(s, AV_LOG_ERROR, "AVIFileOpen failed with error %ld\n", res);
-      AVIFileExit();
-      return -1;
-    }
-
-  res = AVIFileInfo(avs->file, &info, sizeof(info));
-  if (res != S_OK)
-    {
-      av_log(s, AV_LOG_ERROR, "AVIFileInfo failed with error %ld\n", res);
-      AVIFileExit();
-      return -1;
-    }
-
-  avs->streams = av_mallocz(info.dwStreams * sizeof(AVISynthStream));
-
-  for (id=0; id<info.dwStreams; id++)
-    {
-      stream = &avs->streams[id];
-      stream->read = 0;
-      if (AVIFileGetStream(avs->file, &stream->handle, 0, id) == S_OK)
-        {
-          if (AVIStreamInfo(stream->handle, &stream->info, sizeof(stream->info)) == S_OK)
-            {
-              if (stream->info.fccType == streamtypeAUDIO)
-                {
-                  WAVEFORMATEX wvfmt;
-                  LONG struct_size = sizeof(WAVEFORMATEX);
-                  if (AVIStreamReadFormat(stream->handle, 0, &wvfmt, &struct_size) != S_OK)
-                    continue;
-
-                  st = avformat_new_stream(s, NULL);
-                  st->id = id;
-                  st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
-
-                  st->codec->block_align = wvfmt.nBlockAlign;
-                  st->codec->channels = wvfmt.nChannels;
-                  st->codec->sample_rate = wvfmt.nSamplesPerSec;
-                  st->codec->bit_rate = wvfmt.nAvgBytesPerSec * 8;
-                  st->codec->bits_per_coded_sample = wvfmt.wBitsPerSample;
-
-                  stream->chunck_samples = wvfmt.nSamplesPerSec * (uint64_t)info.dwScale / (uint64_t)info.dwRate;
-                  stream->chunck_size = stream->chunck_samples * wvfmt.nChannels * wvfmt.wBitsPerSample / 8;
-
-                  st->codec->codec_tag = wvfmt.wFormatTag;
-                  st->codec->codec_id = ff_wav_codec_get_id(wvfmt.wFormatTag, st->codec->bits_per_coded_sample);
-                }
-              else if (stream->info.fccType == streamtypeVIDEO)
-                {
-                  BITMAPINFO imgfmt;
-                  LONG struct_size = sizeof(BITMAPINFO);
-
-                  stream->chunck_size = stream->info.dwSampleSize;
-                  stream->chunck_samples = 1;
-
-                  if (AVIStreamReadFormat(stream->handle, 0, &imgfmt, &struct_size) != S_OK)
-                    continue;
-
-                  st = avformat_new_stream(s, NULL);
-                  st->id = id;
-                  st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
-                  st->avg_frame_rate.num = stream->info.dwRate;
-                  st->avg_frame_rate.den = stream->info.dwScale;
-#if FF_API_R_FRAME_RATE
-                  st->r_frame_rate = st->avg_frame_rate;
+  #define LoadLibrary(x) dlopen(x, RTLD_NOW | RTLD_GLOBAL)
+  #define GetProcAddress dlsym
+  #define FreeLibrary dlclose
 #endif
 
-                  st->codec->width = imgfmt.bmiHeader.biWidth;
-                  st->codec->height = imgfmt.bmiHeader.biHeight;
+// AvxSynth doesn't have these colorspaces, so disable them
+#ifndef _WIN32
+#define avs_is_yv24(vi) 0
+#define avs_is_yv16(vi) 0
+#define avs_is_yv411(vi) 0
+#define avs_is_y8(vi) 0
+#endif
 
-                  st->codec->bits_per_coded_sample = imgfmt.bmiHeader.biBitCount;
-                  st->codec->bit_rate = (uint64_t)stream->info.dwSampleSize * (uint64_t)stream->info.dwRate * 8 / (uint64_t)stream->info.dwScale;
-                  st->codec->codec_tag = imgfmt.bmiHeader.biCompression;
-                  st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, imgfmt.bmiHeader.biCompression);
-                  if (st->codec->codec_id == AV_CODEC_ID_RAWVIDEO && imgfmt.bmiHeader.biCompression== BI_RGB) {
-                    st->codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
-                    if (st->codec->extradata) {
-                      st->codec->extradata_size = 9;
-                      memcpy(st->codec->extradata, "BottomUp", 9);
-                    }
-                  }
+typedef struct {
+    void *library;
+#define AVSC_DECLARE_FUNC(name) name##_func name
+    AVSC_DECLARE_FUNC(avs_create_script_environment);
+    AVSC_DECLARE_FUNC(avs_delete_script_environment);
+    AVSC_DECLARE_FUNC(avs_get_error);
+    AVSC_DECLARE_FUNC(avs_clip_get_error);
+    AVSC_DECLARE_FUNC(avs_invoke);
+    AVSC_DECLARE_FUNC(avs_release_value);
+    AVSC_DECLARE_FUNC(avs_get_video_info);
+    AVSC_DECLARE_FUNC(avs_take_clip);
+    AVSC_DECLARE_FUNC(avs_release_clip);
+    AVSC_DECLARE_FUNC(avs_bit_blt);
+    AVSC_DECLARE_FUNC(avs_get_audio);
+    AVSC_DECLARE_FUNC(avs_get_frame);
+    AVSC_DECLARE_FUNC(avs_release_video_frame);
+#undef AVSC_DECLARE_FUNC
+} AviSynthLibrary;
 
+struct AviSynthContext {
+    AVS_ScriptEnvironment *env;
+    AVS_Clip *clip;
+    const AVS_VideoInfo *vi;
 
-                  st->duration = stream->info.dwLength;
-                }
-              else
-                {
-                  AVIStreamRelease(stream->handle);
-                  continue;
-                }
+    // avisynth_read_packet_video() iterates over this.
+    int n_planes;
+    const int *planes;
 
-              avs->nb_streams++;
+    int curr_stream;
+    int curr_frame;
+    int64_t curr_sample;
 
-              st->codec->stream_codec_tag = stream->info.fccHandler;
+    int error;
 
-              avpriv_set_pts_info(st, 64, info.dwScale, info.dwRate);
-              st->start_time = stream->info.dwStart;
-            }
+    // Linked list pointers.
+    struct AviSynthContext *next;
+};
+typedef struct AviSynthContext AviSynthContext;
+
+static const int avs_planes_packed[1] = {0};
+static const int avs_planes_grey[1] = {AVS_PLANAR_Y};
+static const int avs_planes_yuv[3] = {AVS_PLANAR_Y, AVS_PLANAR_U, AVS_PLANAR_V};
+
+// A conflict between C++ global objects, atexit, and dynamic loading requires
+// us to register our own atexit handler to prevent double freeing.
+static AviSynthLibrary *avs_library = NULL;
+static int avs_atexit_called = 0;
+
+// Linked list of AviSynthContexts. An atexit handler destroys this list.
+static AviSynthContext *avs_ctx_list = NULL;
+
+static av_cold void avisynth_atexit_handler(void);
+
+static av_cold int avisynth_load_library(void) {
+    avs_library = av_mallocz(sizeof(AviSynthLibrary));
+    if (!avs_library)
+        return AVERROR_UNKNOWN;
+
+    avs_library->library = LoadLibrary(AVISYNTH_LIB);
+    if (!avs_library->library)
+        goto init_fail;
+
+#define LOAD_AVS_FUNC(name, continue_on_fail) \
+{ \
+    avs_library->name = (void*)GetProcAddress(avs_library->library, #name); \
+    if(!continue_on_fail && !avs_library->name) \
+        goto fail; \
+}
+    LOAD_AVS_FUNC(avs_create_script_environment, 0);
+    LOAD_AVS_FUNC(avs_delete_script_environment, 0);
+    LOAD_AVS_FUNC(avs_get_error, 1); // New to AviSynth 2.6
+    LOAD_AVS_FUNC(avs_clip_get_error, 0);
+    LOAD_AVS_FUNC(avs_invoke, 0);
+    LOAD_AVS_FUNC(avs_release_value, 0);
+    LOAD_AVS_FUNC(avs_get_video_info, 0);
+    LOAD_AVS_FUNC(avs_take_clip, 0);
+    LOAD_AVS_FUNC(avs_release_clip, 0);
+    LOAD_AVS_FUNC(avs_bit_blt, 0);
+    LOAD_AVS_FUNC(avs_get_audio, 0);
+    LOAD_AVS_FUNC(avs_get_frame, 0);
+    LOAD_AVS_FUNC(avs_release_video_frame, 0);
+#undef LOAD_AVS_FUNC
+
+    atexit(avisynth_atexit_handler);
+    return 0;
+
+fail:
+    FreeLibrary(avs_library->library);
+init_fail:
+    av_freep(&avs_library);
+    return AVERROR_UNKNOWN;
+}
+
+// Note that avisynth_context_create and avisynth_context_destroy
+// do not allocate or free the actual context! That is taken care of
+// by libavformat.
+static av_cold int avisynth_context_create(AVFormatContext *s) {
+    AviSynthContext *avs = (AviSynthContext *)s->priv_data;
+    int ret;
+
+    if (!avs_library) {
+        if (ret = avisynth_load_library())
+            return ret;
+    }
+
+    avs->env = avs_library->avs_create_script_environment(3);
+    if (avs_library->avs_get_error) {
+        const char *error = avs_library->avs_get_error(avs->env);
+        if (error) {
+            av_log(s, AV_LOG_ERROR, "%s\n", error);
+            return AVERROR_UNKNOWN;
         }
     }
 
-  return 0;
-}
-
-static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
-{
-  AVISynthContext *avs = s->priv_data;
-  HRESULT res;
-  AVISynthStream *stream;
-  int stream_id = avs->next_stream;
-  LONG read_size;
-
-  // handle interleaving manually...
-  stream = &avs->streams[stream_id];
-
-  if (stream->read >= stream->info.dwLength)
-    return AVERROR(EIO);
-
-  if (av_new_packet(pkt, stream->chunck_size))
-    return AVERROR(EIO);
-  pkt->stream_index = stream_id;
-  pkt->pts = avs->streams[stream_id].read / avs->streams[stream_id].chunck_samples;
-
-  res = AVIStreamRead(stream->handle, stream->read, stream->chunck_samples, pkt->data, stream->chunck_size, &read_size, NULL);
-
-  pkt->size = read_size;
-
-  stream->read += stream->chunck_samples;
-
-  // prepare for the next stream to read
-  do {
-    avs->next_stream = (avs->next_stream+1) % avs->nb_streams;
-  } while (avs->next_stream != stream_id && s->streams[avs->next_stream]->discard >= AVDISCARD_ALL);
-
-  return (res == S_OK) ? pkt->size : -1;
-}
-
-static int avisynth_read_close(AVFormatContext *s)
-{
-  AVISynthContext *avs = s->priv_data;
-  int i;
-
-  for (i=0;i<avs->nb_streams;i++)
-    {
-      AVIStreamRelease(avs->streams[i].handle);
+    if (!avs_ctx_list) {
+        avs_ctx_list = avs;
+    } else {
+        avs->next = avs_ctx_list;
+        avs_ctx_list = avs;
     }
 
-  av_free(avs->streams);
-  AVIFileRelease(avs->file);
-  AVIFileExit();
-  return 0;
+    return 0;
 }
 
-static int avisynth_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
-{
-  AVISynthContext *avs = s->priv_data;
-  int stream_id;
+static av_cold void avisynth_context_destroy(AviSynthContext *avs) {
+    if (avs_atexit_called)
+       return;
 
-  for (stream_id = 0; stream_id < avs->nb_streams; stream_id++)
-    {
-      avs->streams[stream_id].read = pts * avs->streams[stream_id].chunck_samples;
+    if (avs == avs_ctx_list) {
+        avs_ctx_list = avs->next;
+    } else {
+        AviSynthContext *prev = avs_ctx_list;
+        while (prev->next != avs)
+            prev = prev->next;
+        prev->next = avs->next;
     }
 
-  return 0;
+    if (avs->clip) {
+        avs_library->avs_release_clip(avs->clip);
+        avs->clip = NULL;
+    }
+    if (avs->env) {
+        avs_library->avs_delete_script_environment(avs->env);
+        avs->env = NULL;
+    }
+}
+
+static av_cold void avisynth_atexit_handler(void) {
+    AviSynthContext *avs = avs_ctx_list;
+
+    while (avs) {
+        AviSynthContext *next = avs->next;
+        avisynth_context_destroy(avs);
+        avs = next;
+    }
+    FreeLibrary(avs_library->library);
+    av_freep(&avs_library);
+
+    avs_atexit_called = 1;
+}
+
+// Create AVStream from audio and video data.
+static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st) {
+    AviSynthContext *avs = s->priv_data;
+    int planar = 0; // 0: packed, 1: YUV, 2: Y8
+
+    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
+    st->codec->codec_id = CODEC_ID_RAWVIDEO;
+    st->codec->width = avs->vi->width;
+    st->codec->height = avs->vi->height;
+
+    st->time_base = (AVRational) {avs->vi->fps_denominator, avs->vi->fps_numerator};
+    st->avg_frame_rate = (AVRational) {avs->vi->fps_numerator, avs->vi->fps_denominator};
+    st->start_time = 0;
+    st->duration = avs->vi->num_frames;
+    st->nb_frames = avs->vi->num_frames;
+
+    switch (avs->vi->pixel_type) {
+#ifdef _WIN32
+    case AVS_CS_YV24:
+        st->codec->pix_fmt = PIX_FMT_YUV444P;
+        planar = 1;
+        break;
+    case AVS_CS_YV16:
+        st->codec->pix_fmt = PIX_FMT_YUV422P;
+        planar = 1;
+        break;
+    case AVS_CS_YV411:
+        st->codec->pix_fmt = PIX_FMT_YUV411P;
+        planar = 1;
+        break;
+    case AVS_CS_Y8:
+        st->codec->pix_fmt = PIX_FMT_GRAY8;
+        planar = 2;
+        break;
+#endif
+    case AVS_CS_BGR24:
+        st->codec->pix_fmt = PIX_FMT_BGR24;
+        break;
+    case AVS_CS_BGR32:
+        st->codec->pix_fmt = PIX_FMT_RGB32;
+        break;
+    case AVS_CS_YUY2:
+        st->codec->pix_fmt = PIX_FMT_YUYV422;
+        break;
+    case AVS_CS_YV12:
+        st->codec->pix_fmt = PIX_FMT_YUV420P;
+        planar = 1;
+        break;
+    case AVS_CS_I420: // Is this even used anywhere?
+        st->codec->pix_fmt = PIX_FMT_YUV420P;
+        planar = 1;
+        break;
+    default:
+        av_log(s, AV_LOG_ERROR, "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
+        avs->error = 1;
+        return AVERROR_UNKNOWN;
+    }
+
+    switch (planar) {
+    case 2: // Y8
+        avs->n_planes = 1;
+        avs->planes = avs_planes_grey;
+        break;
+    case 1: // YUV
+        avs->n_planes = 3;
+        avs->planes = avs_planes_yuv;
+        break;
+    default:
+        avs->n_planes = 1;
+        avs->planes = avs_planes_packed;
+    }
+    return 0;
+}
+
+static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st) {
+    AviSynthContext *avs = s->priv_data;
+
+    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
+    st->codec->sample_rate = avs->vi->audio_samples_per_second;
+    st->codec->channels = avs->vi->nchannels;
+    st->time_base = (AVRational) {1, avs->vi->audio_samples_per_second};
+
+    switch (avs->vi->sample_type) {
+    case AVS_SAMPLE_INT8:
+        st->codec->codec_id = CODEC_ID_PCM_U8;
+        break;
+    case AVS_SAMPLE_INT16:
+        st->codec->codec_id = CODEC_ID_PCM_S16LE;
+        break;
+    case AVS_SAMPLE_INT24:
+        st->codec->codec_id = CODEC_ID_PCM_S24LE;
+        break;
+    case AVS_SAMPLE_INT32:
+        st->codec->codec_id = CODEC_ID_PCM_S32LE;
+        break;
+    case AVS_SAMPLE_FLOAT:
+        st->codec->codec_id = CODEC_ID_PCM_F32LE;
+        break;
+    default:
+        av_log(s, AV_LOG_ERROR, "unknown AviSynth sample type %d\n", avs->vi->sample_type);
+        avs->error = 1;
+        return AVERROR_UNKNOWN;
+    }
+    return 0;
+}
+
+static int avisynth_create_stream(AVFormatContext *s) {
+    AviSynthContext *avs = s->priv_data;
+    AVStream *st;
+    int ret;
+    int id = 0;
+
+    if (avs_has_video(avs->vi)) {
+        st = avformat_new_stream(s, NULL);
+        if (!st)
+            return AVERROR_UNKNOWN;
+        st->id = id++;
+        if (ret = avisynth_create_stream_video(s, st))
+            return ret;
+    }
+    if (avs_has_audio(avs->vi)) {
+        st = avformat_new_stream(s, NULL);
+        if (!st)
+            return AVERROR_UNKNOWN;
+        st->id = id++;
+        if (ret = avisynth_create_stream_audio(s, st))
+            return ret;
+    }
+    return 0;
+}
+
+static int avisynth_open_file(AVFormatContext *s) {
+    AviSynthContext *avs = (AviSynthContext *)s->priv_data;
+    AVS_Value arg, val;
+    int ret;
+
+    if (ret = avisynth_context_create(s))
+        return ret;
+
+    arg = avs_new_value_string(s->filename);
+    val = avs_library->avs_invoke(avs->env, "Import", arg, 0);
+    if (avs_is_error(val)) {
+        av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
+        ret = AVERROR_UNKNOWN;
+        goto fail;
+    }
+    if (!avs_is_clip(val)) {
+        av_log(s, AV_LOG_ERROR, "%s\n", "AviSynth script did not return a clip");
+        ret = AVERROR_UNKNOWN;
+        goto fail;
+    }
+
+    avs->clip = avs_library->avs_take_clip(val, avs->env);
+    avs->vi = avs_library->avs_get_video_info(avs->clip);
+
+    // Release the AVS_Value as it will go out of scope.
+    avs_library->avs_release_value(val);
+
+    if (ret = avisynth_create_stream(s))
+        goto fail;
+
+    return 0;
+
+fail:
+    avisynth_context_destroy(avs);
+    return ret;
+}
+
+static void avisynth_next_stream(AVFormatContext *s, AVStream **st, AVPacket *pkt, int *discard) {
+    AviSynthContext *avs = s->priv_data;
+
+    pkt->stream_index = avs->curr_stream++;
+    avs->curr_stream %= s->nb_streams;
+
+    *st = s->streams[pkt->stream_index];
+    if ((*st)->discard == AVDISCARD_ALL)
+        *discard = 1;
+    else
+        *discard = 0;
+
+    return;
+}
+
+// Copy AviSynth clip data into an AVPacket.
+static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt, int discard) {
+    AviSynthContext *avs = s->priv_data;
+    AVS_VideoFrame *frame;
+    unsigned char *dst_p;
+    const unsigned char *src_p;
+    int n, i, plane, rowsize, planeheight, pitch, bits;
+    const char *error;
+
+    if (avs->curr_frame >= avs->vi->num_frames)
+        return AVERROR_EOF;
+
+    // This must happen even if the stream is discarded to prevent desync.
+    n = avs->curr_frame++;
+    if (discard)
+        return 0;
+
+    pkt->pts = n;
+    pkt->dts = n;
+    pkt->duration = 1;
+
+    // Define the bpp values for the new AviSynth 2.6 colorspaces
+    if (avs_is_yv24(avs->vi)) {
+        bits = 24;
+    } else if (avs_is_yv16(avs->vi)) {
+        bits = 16;
+    } else if (avs_is_yv411(avs->vi)) {
+        bits = 12;
+    } else if (avs_is_y8(avs->vi)) {
+        bits = 8;
+    } else {
+        bits = avs_bits_per_pixel(avs->vi);
+    }
+
+    // Without cast to int64_t, calculation overflows at about 9k x 9k resolution.
+    pkt->size = (((int64_t)avs->vi->width * (int64_t)avs->vi->height) * bits) / 8;
+    if (!pkt->size)
+        return AVERROR_UNKNOWN;
+    pkt->data = av_malloc(pkt->size);
+    if (!pkt->data)
+        return AVERROR_UNKNOWN;
+
+    frame = avs_library->avs_get_frame(avs->clip, n);
+    error = avs_library->avs_clip_get_error(avs->clip);
+    if (error) {
+        av_log(s, AV_LOG_ERROR, "%s\n", error);
+        avs->error = 1;
+        av_freep(&pkt->data);
+        return AVERROR_UNKNOWN;
+    }
+
+    dst_p = pkt->data;
+    for (i = 0; i < avs->n_planes; i++) {
+        plane = avs->planes[i];
+        src_p = avs_get_read_ptr_p(frame, plane);
+        rowsize = avs_get_row_size_p(frame, plane);
+        planeheight = avs_get_height_p(frame, plane);
+        pitch = avs_get_pitch_p(frame, plane);
+
+        // Flip RGB video.
+        if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) {
+            src_p = src_p + (planeheight - 1) * pitch;
+            pitch = -pitch;
+        }
+
+        avs_library->avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch, rowsize, planeheight);
+        dst_p += rowsize * planeheight;
+    }
+
+    avs_library->avs_release_video_frame(frame);
+    return 0;
+}
+
+static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt, int discard) {
+    AviSynthContext *avs = s->priv_data;
+    AVRational fps, samplerate;
+    int samples;
+    int64_t n;
+    const char *error;
+
+    if (avs->curr_sample >= avs->vi->num_audio_samples)
+        return AVERROR_EOF;
+
+    fps.num = avs->vi->fps_numerator;
+    fps.den = avs->vi->fps_denominator;
+    samplerate.num = avs->vi->audio_samples_per_second;
+    samplerate.den = 1;
+
+    if (avs_has_video(avs->vi)) {
+        if (avs->curr_frame < avs->vi->num_frames)
+            samples = av_rescale_q(avs->curr_frame, samplerate, fps) - avs->curr_sample;
+        else
+            samples = av_rescale_q(1, samplerate, fps);
+    } else {
+        samples = 1000;
+    }
+
+    // After seeking, audio may catch up with video.
+    if (samples <= 0) {
+        pkt->size = 0;
+        pkt->data = NULL;
+        return 0;
+    }
+
+    if (avs->curr_sample + samples > avs->vi->num_audio_samples)
+        samples = avs->vi->num_audio_samples - avs->curr_sample;
+
+    // This must happen even if the stream is discarded to prevent desync.
+    n = avs->curr_sample;
+    avs->curr_sample += samples;
+    if (discard)
+        return 0;
+
+    pkt->pts = n;
+    pkt->dts = n;
+    pkt->duration = samples;
+
+    pkt->size = avs_bytes_per_channel_sample(avs->vi) * samples * avs->vi->nchannels;
+    if (!pkt->size)
+        return AVERROR_UNKNOWN;
+    pkt->data = av_malloc(pkt->size);
+    if (!pkt->data)
+        return AVERROR_UNKNOWN;
+
+    avs_library->avs_get_audio(avs->clip, pkt->data, n, samples);
+    error = avs_library->avs_clip_get_error(avs->clip);
+    if (error) {
+        av_log(s, AV_LOG_ERROR, "%s\n", error);
+        avs->error = 1;
+        av_freep(&pkt->data);
+        return AVERROR_UNKNOWN;
+    }
+    return 0;
+}
+
+static av_cold int avisynth_read_header(AVFormatContext *s) {
+    int ret;
+
+    // Calling library must implement a lock for thread-safe opens.
+    if (ret = avpriv_lock_avformat())
+        return ret;
+
+    if (ret = avisynth_open_file(s)) {
+        avpriv_unlock_avformat();
+        return ret;
+    }
+
+    avpriv_unlock_avformat();
+    return 0;
+}
+
+static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt) {
+    AviSynthContext *avs = s->priv_data;
+    AVStream *st;
+    int discard = 0;
+    int ret;
+
+    if (avs->error)
+        return AVERROR_UNKNOWN;
+
+    pkt->destruct = av_destruct_packet;
+
+    // If either stream reaches EOF, try to read the other one before giving up.
+    avisynth_next_stream(s, &st, pkt, &discard);
+    if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
+        ret = avisynth_read_packet_video(s, pkt, discard);
+        if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
+            avisynth_next_stream(s, &st, pkt, &discard);
+            return avisynth_read_packet_audio(s, pkt, discard);
+        }
+        return ret;
+    } else {
+        ret = avisynth_read_packet_audio(s, pkt, discard);
+        if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
+            avisynth_next_stream(s, &st, pkt, &discard);
+            return avisynth_read_packet_video(s, pkt, discard);
+        }
+        return ret;
+    }
+}
+
+static av_cold int avisynth_read_close(AVFormatContext *s) {
+    if (avpriv_lock_avformat())
+        return AVERROR_UNKNOWN;
+
+    avisynth_context_destroy(s->priv_data);
+    avpriv_unlock_avformat();
+    return 0;
+}
+
+static int avisynth_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) {
+    AviSynthContext *avs = s->priv_data;
+    AVStream *st;
+    AVRational fps, samplerate;
+
+    if (avs->error)
+        return AVERROR_UNKNOWN;
+
+    fps = (AVRational) {avs->vi->fps_numerator, avs->vi->fps_denominator};
+    samplerate = (AVRational) {avs->vi->audio_samples_per_second, 1};
+
+    st = s->streams[stream_index];
+    if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
+        // AviSynth frame counts are signed int.
+        if ((timestamp >= avs->vi->num_frames) || (timestamp > INT_MAX) || (timestamp < 0))
+            return AVERROR_EOF;
+        avs->curr_frame = timestamp;
+        if (avs_has_audio(avs->vi))
+            avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
+    } else {
+        if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
+            return AVERROR_EOF;
+        // Force frame granularity for seeking.
+        if (avs_has_video(avs->vi)) {
+            avs->curr_frame = av_rescale_q(timestamp, fps, samplerate);
+            avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
+        } else {
+            avs->curr_sample = timestamp;
+        }
+    }
+
+    return 0;
 }
 
 AVInputFormat ff_avisynth_demuxer = {
     .name           = "avisynth",
-    .long_name      = NULL_IF_CONFIG_SMALL("AVISynth"),
-    .priv_data_size = sizeof(AVISynthContext),
+    .long_name      = NULL_IF_CONFIG_SMALL("AviSynth script"),
+    .priv_data_size = sizeof(AviSynthContext),
     .read_header    = avisynth_read_header,
     .read_packet    = avisynth_read_packet,
     .read_close     = avisynth_read_close,
diff --git a/libavformat/avr.c b/libavformat/avr.c
index 71a107c..473136e 100644
--- a/libavformat/avr.c
+++ b/libavformat/avr.c
@@ -51,7 +51,7 @@
     } else if (chan == 0xFFFFu) {
         st->codec->channels = 2;
     } else {
-        av_log_ask_for_sample(s, "unknown number of channels\n");
+        avpriv_request_sample(s, "chan %d", chan);
         return AVERROR_PATCHWELCOME;
     }
 
@@ -78,7 +78,7 @@
     } else if (sign == 0xFFFFu && bps == 16) {
         st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
     } else {
-        av_log_ask_for_sample(s, "unknown bits per sample\n");
+        avpriv_request_sample(s, "bits per sample %d", bps);
         return AVERROR_PATCHWELCOME;
     }
 
diff --git a/libavformat/avs.c b/libavformat/avs.c
index 5a1eb2e..ec9198b 100644
--- a/libavformat/avs.c
+++ b/libavformat/avs.c
@@ -50,7 +50,7 @@
 
     d = p->buf;
     if (d[0] == 'w' && d[1] == 'W' && d[2] == 0x10 && d[3] == 0)
-        return 50;
+        return 55;
 
     return 0;
 }
diff --git a/libavformat/bethsoftvid.c b/libavformat/bethsoftvid.c
index caff7ab..b8f08c1 100644
--- a/libavformat/bethsoftvid.c
+++ b/libavformat/bethsoftvid.c
@@ -108,8 +108,9 @@
             return AVERROR(ENOMEM);
         vid->video_index = st->index;
         if (vid->audio_index < 0) {
-            av_log_ask_for_sample(s, "No audio packet before first video "
-                                  "packet. Using default video time base.\n");
+            avpriv_request_sample(s, "Using default video time base since "
+                                  "having no audio packet before the first "
+                                  "video packet");
         }
         avpriv_set_pts_info(st, 64, 185, vid->sample_rate);
         st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
diff --git a/libavformat/bintext.c b/libavformat/bintext.c
index 91f95f3..c1c6c00 100644
--- a/libavformat/bintext.c
+++ b/libavformat/bintext.c
@@ -43,8 +43,8 @@
     int chars_per_frame; /**< characters to send decoder per frame;
                               set by private options as characters per second, and then
                               converted to characters per frame at runtime */
-    char *video_size;    /**< video size (WxH pixels) (private option) */
-    char *framerate;     /**< frames per second (private option) */
+    int width, height;    /**< video size (WxH pixels) (private option) */
+    AVRational framerate; /**< frames per second (private option) */
     uint64_t fsize;  /**< file size less metadata buffer */
 } BinDemuxContext;
 
@@ -57,26 +57,12 @@
     st->codec->codec_tag   = 0;
     st->codec->codec_type  = AVMEDIA_TYPE_VIDEO;
 
-    if (bin->video_size) {
-        if (av_parse_video_size(&st->codec->width, &st->codec->height, bin->video_size) < 0) {
-            av_log(s, AV_LOG_ERROR, "Could not parse video size: '%s'\n", bin->video_size);
-            return NULL;
-        }
-    } else {
+    if (!bin->width) {
         st->codec->width  = (80<<3);
         st->codec->height = (25<<4);
     }
 
-    if (bin->framerate) {
-        AVRational framerate;
-        if (av_parse_video_rate(&framerate, bin->framerate) < 0) {
-            av_log(s, AV_LOG_ERROR, "Could not parse framerate: '%s'\n", bin->framerate);
-            return NULL;
-        }
-        avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
-    } else {
-        avpriv_set_pts_info(st, 60, 1, 25);
-    }
+    avpriv_set_pts_info(st, 60, bin->framerate.den, bin->framerate.num);
 
     /* simulate tty display speed */
     bin->chars_per_frame = FFMAX(av_q2d(st->time_base) * bin->chars_per_frame, 1);
@@ -162,7 +148,7 @@
         bin->fsize = avio_size(pb);
         if (ff_sauce_read(s, &bin->fsize, &got_width, 0) < 0)
             next_tag_read(s, &bin->fsize);
-        if (!bin->video_size) {
+        if (!bin->width) {
             predict_width(st->codec, bin->fsize, got_width);
             calculate_height(st->codec, bin->fsize);
         }
@@ -261,7 +247,7 @@
         bin->fsize = avio_size(pb) - 1 - 192 - 4096;
         st->codec->width = 80<<3;
         ff_sauce_read(s, &bin->fsize, &got_width, 0);
-        if (!bin->video_size)
+        if (!bin->width)
             calculate_height(st->codec, bin->fsize);
         avio_seek(pb, 1 + 192 + 4096, SEEK_SET);
     }
@@ -314,7 +300,7 @@
 
     bin->fsize = avio_size(pb) - 12 - 4096 - 48;
     ff_sauce_read(s, &bin->fsize, &got_width, 0);
-    if (!bin->video_size)
+    if (!bin->width)
         calculate_height(st->codec, bin->fsize);
     avio_seek(pb, 12, SEEK_SET);
     return 0;
@@ -346,8 +332,8 @@
 #define OFFSET(x) offsetof(BinDemuxContext, x)
 static const AVOption options[] = {
     { "linespeed", "set simulated line speed (bytes per second)", OFFSET(chars_per_frame), AV_OPT_TYPE_INT, {.i64 = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
-    { "video_size", "set video size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
-    { "framerate", "set framerate (frames per second)", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
+    { "video_size", "set video size, such as 640x480 or hd720.", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
+    { "framerate", "set framerate (frames per second)", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
     { NULL },
 };
 
diff --git a/libavformat/bit.c b/libavformat/bit.c
index 9f6ea4a..9b2246c 100644
--- a/libavformat/bit.c
+++ b/libavformat/bit.c
@@ -139,7 +139,6 @@
     init_get_bits(&gb, pkt->data, 8*10);
     for(i=0; i< 8 * 10; i++)
         avio_wl16(pb, get_bits1(&gb) ? BIT_1 : BIT_0);
-    avio_flush(pb);
 
     return 0;
 }
diff --git a/libavformat/brstm.c b/libavformat/brstm.c
index 82f3027..7781b3c 100644
--- a/libavformat/brstm.c
+++ b/libavformat/brstm.c
@@ -75,7 +75,7 @@
         return AVERROR_INVALIDDATA;
     }
     if (bom == 0xFFFE) {
-        av_log_ask_for_sample(s, "unsupported byte order\n");
+        avpriv_request_sample(s, "little endian byte order");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -110,7 +110,7 @@
     case 1: codec = AV_CODEC_ID_PCM_S16BE_PLANAR; break;
     case 2: codec = AV_CODEC_ID_ADPCM_THP;        break;
     default:
-        av_log_ask_for_sample(s, "unsupported codec: %d\n", codec);
+        avpriv_request_sample(s, "codec %d", codec);
         return AVERROR_PATCHWELCOME;
     }
 
@@ -220,8 +220,8 @@
             }
             avio_skip(s->pb, start - avio_tell(s->pb));
 
-            if (major!=1 || minor)
-                av_log_ask_for_sample(s, "Version %d.%d\n", major, minor);
+            if (major != 1 || minor)
+                avpriv_request_sample(s, "Version %d.%d", major, minor);
 
             return 0;
         default:
diff --git a/libavformat/concatdec.c b/libavformat/concatdec.c
index 052b4eb..5359ad1 100644
--- a/libavformat/concatdec.c
+++ b/libavformat/concatdec.c
@@ -37,6 +37,7 @@
     unsigned nb_files;
     AVFormatContext *avf;
     int safe;
+    int seekable;
 } ConcatContext;
 
 static int concat_probe(AVProbeData *probe)
@@ -82,25 +83,26 @@
 {
     ConcatContext *cat = avf->priv_data;
     ConcatFile *file;
-    char *url;
+    char *url = NULL;
     size_t url_len;
+    int ret;
 
     if (cat->safe > 0 && !safe_filename(filename)) {
         av_log(avf, AV_LOG_ERROR, "Unsafe file name '%s'\n", filename);
-        return AVERROR(EPERM);
+        FAIL(AVERROR(EPERM));
     }
     url_len = strlen(avf->filename) + strlen(filename) + 16;
     if (!(url = av_malloc(url_len)))
-        return AVERROR(ENOMEM);
+        FAIL(AVERROR(ENOMEM));
     ff_make_absolute_url(url, url_len, avf->filename, filename);
-    av_free(filename);
+    av_freep(&filename);
 
     if (cat->nb_files >= *nb_files_alloc) {
         size_t n = FFMAX(*nb_files_alloc * 2, 16);
         ConcatFile *new_files;
         if (n <= cat->nb_files || n > SIZE_MAX / sizeof(*cat->files) ||
             !(new_files = av_realloc(cat->files, n * sizeof(*cat->files))))
-            return AVERROR(ENOMEM);
+            FAIL(AVERROR(ENOMEM));
         cat->files = new_files;
         *nb_files_alloc = n;
     }
@@ -114,6 +116,11 @@
     file->duration   = AV_NOPTS_VALUE;
 
     return 0;
+
+fail:
+    av_free(url);
+    av_free(filename);
+    return ret;
 }
 
 static int open_file(AVFormatContext *avf, unsigned fileno)
@@ -122,6 +129,8 @@
     ConcatFile *file = &cat->files[fileno];
     int ret;
 
+    if (cat->avf)
+        avformat_close_input(&cat->avf);
     if ((ret = avformat_open_input(&cat->avf, file->url, NULL, NULL)) < 0 ||
         (ret = avformat_find_stream_info(cat->avf, NULL)) < 0) {
         av_log(avf, AV_LOG_ERROR, "Impossible to open '%s'\n", file->url);
@@ -157,6 +166,7 @@
     unsigned nb_files_alloc = 0;
     ConcatFile *file = NULL;
     AVStream *st, *source_st;
+    int64_t time = 0;
 
     while (1) {
         if ((ret = ff_get_line(avf->pb, buf, sizeof(buf))) <= 0)
@@ -207,6 +217,20 @@
     if (ret < 0)
         FAIL(ret);
 
+    for (i = 0; i < cat->nb_files; i++) {
+        if (cat->files[i].start_time == AV_NOPTS_VALUE)
+            cat->files[i].start_time = time;
+        else
+            time = cat->files[i].start_time;
+        if (cat->files[i].duration == AV_NOPTS_VALUE)
+            break;
+        time += cat->files[i].duration;
+    }
+    if (i == cat->nb_files) {
+        avf->duration = time;
+        cat->seekable = 1;
+    }
+
     if ((ret = open_file(avf, 0)) < 0)
         FAIL(ret);
     for (i = 0; i < cat->avf->nb_streams; i++) {
@@ -238,7 +262,6 @@
 
     if (++fileno >= cat->nb_files)
         return AVERROR_EOF;
-    avformat_close_input(&cat->avf);
     return open_file(avf, fileno);
 }
 
@@ -263,6 +286,95 @@
     return ret;
 }
 
+static void rescale_interval(AVRational tb_in, AVRational tb_out,
+                             int64_t *min_ts, int64_t *ts, int64_t *max_ts)
+{
+    *ts     = av_rescale_q    (*    ts, tb_in, tb_out);
+    *min_ts = av_rescale_q_rnd(*min_ts, tb_in, tb_out,
+                               AV_ROUND_UP   | AV_ROUND_PASS_MINMAX);
+    *max_ts = av_rescale_q_rnd(*max_ts, tb_in, tb_out,
+                               AV_ROUND_DOWN | AV_ROUND_PASS_MINMAX);
+}
+
+static int try_seek(AVFormatContext *avf, int stream,
+                    int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
+{
+    ConcatContext *cat = avf->priv_data;
+    int64_t t0 = cat->cur_file->start_time - cat->avf->start_time;
+
+    ts -= t0;
+    min_ts = min_ts == INT64_MIN ? INT64_MIN : min_ts - t0;
+    max_ts = max_ts == INT64_MAX ? INT64_MAX : max_ts - t0;
+    if (stream >= 0) {
+        if (stream >= cat->avf->nb_streams)
+            return AVERROR(EIO);
+        rescale_interval(AV_TIME_BASE_Q, cat->avf->streams[stream]->time_base,
+                         &min_ts, &ts, &max_ts);
+    }
+    return avformat_seek_file(cat->avf, stream, min_ts, ts, max_ts, flags);
+}
+
+static int real_seek(AVFormatContext *avf, int stream,
+                     int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
+{
+    ConcatContext *cat = avf->priv_data;
+    int ret, left, right;
+
+    if (stream >= 0) {
+        if (stream >= avf->nb_streams)
+            return AVERROR(EINVAL);
+        rescale_interval(avf->streams[stream]->time_base, AV_TIME_BASE_Q,
+                         &min_ts, &ts, &max_ts);
+    }
+
+    left  = 0;
+    right = cat->nb_files;
+    while (right - left > 1) {
+        int mid = (left + right) / 2;
+        if (ts < cat->files[mid].start_time)
+            right = mid;
+        else
+            left  = mid;
+    }
+
+    if ((ret = open_file(avf, left)) < 0)
+        return ret;
+
+    ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
+    if (ret < 0 && !(flags & AVSEEK_FLAG_BACKWARD) &&
+        left < cat->nb_files - 1 &&
+        cat->files[left + 1].start_time < max_ts) {
+        if ((ret = open_file(avf, left + 1)) < 0)
+            return ret;
+        ret = try_seek(avf, stream, min_ts, ts, max_ts, flags);
+    }
+    return ret;
+}
+
+static int concat_seek(AVFormatContext *avf, int stream,
+                       int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
+{
+    ConcatContext *cat = avf->priv_data;
+    ConcatFile *cur_file_saved = cat->cur_file;
+    AVFormatContext *cur_avf_saved = cat->avf;
+    int ret;
+
+    if (!cat->seekable)
+        return AVERROR(ESPIPE); /* XXX: can we use it? */
+    if (flags & (AVSEEK_FLAG_BYTE | AVSEEK_FLAG_FRAME))
+        return AVERROR(ENOSYS);
+    cat->avf = NULL;
+    if ((ret = real_seek(avf, stream, min_ts, ts, max_ts, flags)) < 0) {
+        if (cat->avf)
+            avformat_close_input(&cat->avf);
+        cat->avf      = cur_avf_saved;
+        cat->cur_file = cur_file_saved;
+    } else {
+        avformat_close_input(&cur_avf_saved);
+    }
+    return ret;
+}
+
 #define OFFSET(x) offsetof(ConcatContext, x)
 #define DEC AV_OPT_FLAG_DECODING_PARAM
 
@@ -288,6 +400,6 @@
     .read_header    = concat_read_header,
     .read_packet    = concat_read_packet,
     .read_close     = concat_read_close,
+    .read_seek2     = concat_seek,
     .priv_class     = &concat_class,
-    .extensions     = "ffconcat,ffcat",
 };
diff --git a/libavformat/daud.c b/libavformat/daud.c
index fb62ab1..6983785 100644
--- a/libavformat/daud.c
+++ b/libavformat/daud.c
@@ -68,7 +68,6 @@
     avio_wb16(s->pb, pkt->size);
     avio_wb16(s->pb, 0x8010); // unknown
     avio_write(s->pb, pkt->data, pkt->size);
-    avio_flush(s->pb);
     return 0;
 }
 
diff --git a/libavformat/dfa.c b/libavformat/dfa.c
index 8cf2f24..5799d98 100644
--- a/libavformat/dfa.c
+++ b/libavformat/dfa.c
@@ -36,13 +36,15 @@
     AVIOContext *pb = s->pb;
     AVStream *st;
     int frames;
+    int version;
     uint32_t mspf;
 
     if (avio_rl32(pb) != MKTAG('D', 'F', 'I', 'A')) {
         av_log(s, AV_LOG_ERROR, "Invalid magic for DFA\n");
         return AVERROR_INVALIDDATA;
     }
-    avio_skip(pb, 2); // unused
+
+    version = avio_rl16(pb);
     frames = avio_rl16(pb);
 
     st = avformat_new_stream(s, NULL);
@@ -62,6 +64,12 @@
     avio_skip(pb, 128 - 16); // padding
     st->duration = frames;
 
+    st->codec->extradata = av_malloc(2);
+    st->codec->extradata_size = 2;
+    AV_WL16(st->codec->extradata, version);
+    if (version == 0x100)
+        st->sample_aspect_ratio = (AVRational){2, 1};
+
     return 0;
 }
 
diff --git a/libavformat/dvenc.c b/libavformat/dvenc.c
index 0b3811e..e37bd23 100644
--- a/libavformat/dvenc.c
+++ b/libavformat/dvenc.c
@@ -390,7 +390,6 @@
                               pkt->data, pkt->size, &frame);
     if (fsize > 0) {
         avio_write(s->pb, frame, fsize);
-        avio_flush(s->pb);
     }
     return 0;
 }
diff --git a/libavformat/electronicarts.c b/libavformat/electronicarts.c
index 5b821a9..dae40b1 100644
--- a/libavformat/electronicarts.c
+++ b/libavformat/electronicarts.c
@@ -184,7 +184,7 @@
         case  3: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R3; break;
         case -1: break;
         default:
-            av_log_ask_for_sample(s, "unsupported stream type; revision=%i\n", revision);
+            avpriv_request_sample(s, "stream type; revision=%i", revision);
             return 0;
         }
         switch (revision2) {
@@ -195,7 +195,7 @@
             case  2: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R1; break;
             case  3: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2; break;
             default:
-                av_log_ask_for_sample(s, "unsupported stream type; revision=%i, revision2=%i\n", revision, revision2);
+                avpriv_request_sample(s, "stream type; revision=%i, revision2=%i", revision, revision2);
                 return 0;
             }
             break;
@@ -203,12 +203,12 @@
         case -1: break;
         default:
             ea->audio_codec = AV_CODEC_ID_NONE;
-            av_log_ask_for_sample(s, "unsupported stream type; revision2=%i\n", revision2);
+            avpriv_request_sample(s, "stream type; revision2=%i", revision2);
             return 0;
         }
         break;
     default:
-        av_log_ask_for_sample(s, "unsupported stream type; compression_type=%i\n", compression_type);
+        avpriv_request_sample(s, "stream type; compression_type=%i", compression_type);
         return 0;
     }
 
@@ -244,7 +244,7 @@
     case 1: ea->audio_codec = AV_CODEC_ID_PCM_MULAW; ea->bytes = 1; break;
     case 2: ea->audio_codec = AV_CODEC_ID_ADPCM_IMA_EA_EACS; break;
     default:
-        av_log_ask_for_sample(s, "unsupported stream type; audio compression_type=%i\n", compression_type);
+        avpriv_request_sample(s, "stream type; audio compression_type=%i", compression_type);
     }
 
     return 1;
@@ -289,6 +289,10 @@
     avio_skip(pb, 4);
     ea->time_base.den = avio_rl32(pb);
     ea->time_base.num = avio_rl32(pb);
+    if (ea->time_base.den <= 0 || ea->time_base.num <= 0) {
+        av_log(s, AV_LOG_ERROR, "Timebase is invalid\n");
+        return AVERROR_INVALIDDATA;
+    }
     ea->video_codec = AV_CODEC_ID_VP6;
 
     return 1;
@@ -332,7 +336,7 @@
         switch (blockid) {
             case ISNh_TAG:
                 if (avio_rl32(pb) != EACS_TAG) {
-                    av_log_ask_for_sample(s, "unknown 1SNh headerid\n");
+                    avpriv_request_sample(s, "unknown 1SNh headerid");
                     return 0;
                 }
                 err = process_audio_header_eacs(s);
@@ -344,7 +348,7 @@
                 if (blockid == GSTR_TAG) {
                     avio_skip(pb, 4);
                 } else if ((blockid & 0xFFFF)!=PT00_TAG) {
-                    av_log_ask_for_sample(s, "unknown SCHl headerid\n");
+                    avpriv_request_sample(s, "unknown SCHl headerid");
                     return 0;
                 }
                 err = process_audio_header_elements(s);
@@ -531,7 +535,7 @@
                 chunk_size -= 12;
             }
             if (partial_packet) {
-                av_log_ask_for_sample(s, "video header followed by audio packet not supported.\n");
+                avpriv_request_sample(s, "video header followed by audio packet");
                 av_free_packet(pkt);
                 partial_packet = 0;
             }
diff --git a/libavformat/epafdec.c b/libavformat/epafdec.c
index ffb8b95..c737892 100644
--- a/libavformat/epafdec.c
+++ b/libavformat/epafdec.c
@@ -77,7 +77,7 @@
         st->codec->codec_id = AV_CODEC_ID_PCM_S8;
         break;
     case 1:
-        av_log_missing_feature(s, "24-bit Paris PCM format", 1);
+        avpriv_request_sample(s, "24-bit Paris PCM format");
     default:
         return AVERROR_INVALIDDATA;
     }
diff --git a/libavformat/filmstripdec.c b/libavformat/filmstripdec.c
index aa41fa4..1cd1a48 100644
--- a/libavformat/filmstripdec.c
+++ b/libavformat/filmstripdec.c
@@ -55,7 +55,7 @@
 
     st->nb_frames = avio_rb32(pb);
     if (avio_rb16(pb) != 0) {
-        av_log_ask_for_sample(s, "unsupported packing method\n");
+        avpriv_request_sample(s, "Unsupported packing method");
         return AVERROR_PATCHWELCOME;
     }
 
diff --git a/libavformat/flacdec.c b/libavformat/flacdec.c
index ecc47e6..d5aacfd 100644
--- a/libavformat/flacdec.c
+++ b/libavformat/flacdec.c
@@ -34,7 +34,8 @@
 {
     const CodecMime *mime = ff_id3v2_mime_tags;
     enum  AVCodecID      id = AV_CODEC_ID_NONE;
-    uint8_t mimetype[64], *desc = NULL, *data = NULL;
+    AVBufferRef *data = NULL;
+    uint8_t mimetype[64], *desc = NULL;
     AVIOContext *pb = NULL;
     AVStream *st;
     int type, width, height;
@@ -110,10 +111,10 @@
             ret = AVERROR_INVALIDDATA;
         goto fail;
     }
-    if (!(data = av_malloc(len))) {
+    if (!(data = av_buffer_alloc(len))) {
         RETURN_ERROR(AVERROR(ENOMEM));
     }
-    if (avio_read(pb, data, len) != len) {
+    if (avio_read(pb, data->data, len) != len) {
         av_log(s, AV_LOG_ERROR, "Error reading attached picture data.\n");
         if (s->error_recognition & AV_EF_EXPLODE)
             ret = AVERROR(EIO);
@@ -126,9 +127,9 @@
     }
 
     av_init_packet(&st->attached_pic);
-    st->attached_pic.data         = data;
+    st->attached_pic.buf          = data;
+    st->attached_pic.data         = data->data;
     st->attached_pic.size         = len;
-    st->attached_pic.destruct     = av_destruct_packet;
     st->attached_pic.stream_index = st->index;
     st->attached_pic.flags       |= AV_PKT_FLAG_KEY;
 
@@ -146,8 +147,8 @@
     return 0;
 
 fail:
+    av_buffer_unref(&data);
     av_freep(&desc);
-    av_freep(&data);
     av_freep(&pb);
     return ret;
 
@@ -277,11 +278,9 @@
 
 static int flac_probe(AVProbeData *p)
 {
-    const uint8_t *bufptr = p->buf;
-    const uint8_t *end    = p->buf + p->buf_size;
-
-    if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
-    else                                            return AVPROBE_SCORE_MAX/2;
+    if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
+        return 0;
+    return AVPROBE_SCORE_MAX/2;
 }
 
 AVInputFormat ff_flac_demuxer = {
diff --git a/libavformat/flacenc.c b/libavformat/flacenc.c
index b625278..8c1a1bb 100644
--- a/libavformat/flacenc.c
+++ b/libavformat/flacenc.c
@@ -122,7 +122,6 @@
 static int flac_write_packet(struct AVFormatContext *s, AVPacket *pkt)
 {
     avio_write(s->pb, pkt->data, pkt->size);
-    avio_flush(s->pb);
     return 0;
 }
 
diff --git a/libavformat/flvdec.c b/libavformat/flvdec.c
index b57929e..8bb56e8 100644
--- a/libavformat/flvdec.c
+++ b/libavformat/flvdec.c
@@ -199,7 +199,7 @@
     }
 }
 
-static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid) {
+static int flv_set_video_codec(AVFormatContext *s, AVStream *vstream, int flv_codecid, int read) {
     AVCodecContext *vcodec = vstream->codec;
     switch(flv_codecid) {
         case FLV_CODECID_H263  : vcodec->codec_id = AV_CODEC_ID_FLV1   ; break;
@@ -210,11 +210,17 @@
         case FLV_CODECID_VP6A  :
             if(flv_codecid == FLV_CODECID_VP6A)
                 vcodec->codec_id = AV_CODEC_ID_VP6A;
-            if(vcodec->extradata_size != 1) {
-                vcodec->extradata_size = 1;
-                vcodec->extradata = av_malloc(1 + FF_INPUT_BUFFER_PADDING_SIZE);
+            if (read) {
+                if (vcodec->extradata_size != 1) {
+                    vcodec->extradata = av_malloc(1 + FF_INPUT_BUFFER_PADDING_SIZE);
+                    if (vcodec->extradata)
+                        vcodec->extradata_size = 1;
+                }
+                if (vcodec->extradata)
+                    vcodec->extradata[0] = avio_r8(s->pb);
+                else
+                    avio_skip(s->pb, 1);
             }
-            vcodec->extradata[0] = avio_r8(s->pb);
             return 1; // 1 byte body size adjustment for flv_read_packet()
         case FLV_CODECID_H264:
             vcodec->codec_id = AV_CODEC_ID_H264;
@@ -392,7 +398,7 @@
         acodec = astream ? astream->codec : NULL;
         vcodec = vstream ? vstream->codec : NULL;
 
-        if (amf_type == AMF_DATA_TYPE_NUMBER) {
+        if (amf_type == AMF_DATA_TYPE_NUMBER || amf_type == AMF_DATA_TYPE_BOOL) {
             if (!strcmp(key, "duration"))
                 s->duration = num_val * AV_TIME_BASE;
             else if (!strcmp(key, "videodatarate") && vcodec && 0 <= (int)(num_val * 1024.0))
@@ -406,13 +412,21 @@
                 st->codec->codec_id = AV_CODEC_ID_TEXT;
             } else if (flv->trust_metadata) {
                 if (!strcmp(key, "videocodecid") && vcodec) {
-                    flv_set_video_codec(s, vstream, num_val);
+                    flv_set_video_codec(s, vstream, num_val, 0);
                 } else
                 if (!strcmp(key, "audiocodecid") && acodec) {
-                    flv_set_audio_codec(s, astream, acodec, num_val);
+                    int id = ((int)num_val) << FLV_AUDIO_CODECID_OFFSET;
+                    flv_set_audio_codec(s, astream, acodec, id);
                 } else
                 if (!strcmp(key, "audiosamplerate") && acodec) {
                     acodec->sample_rate = num_val;
+                } else if (!strcmp(key, "audiosamplesize") && acodec) {
+                    acodec->bits_per_coded_sample = num_val;
+                } else if (!strcmp(key, "stereo") && acodec) {
+                    acodec->channels = num_val + 1;
+                    acodec->channel_layout = acodec->channels == 2 ?
+                                             AV_CH_LAYOUT_STEREO :
+                                             AV_CH_LAYOUT_MONO;
                 } else
                 if (!strcmp(key, "width") && vcodec) {
                     vcodec->width = num_val;
@@ -728,9 +742,10 @@
         }
     }
     if(i == s->nb_streams){
+        static const enum AVMediaType stream_types[] = {AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_DATA};
         av_log(s, AV_LOG_WARNING, "Stream discovered after head already parsed\n");
         st = create_stream(s,
-             (int[]){AVMEDIA_TYPE_VIDEO, AVMEDIA_TYPE_AUDIO, AVMEDIA_TYPE_DATA}[stream_type]);
+                           stream_types[stream_type]);
         if (!st)
             return AVERROR(ENOMEM);
 
@@ -743,7 +758,7 @@
         avio_seek(s->pb, next, SEEK_SET);
         continue;
     }
-    if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
+    if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || stream_type == FLV_STREAM_TYPE_AUDIO)
         av_add_index_entry(st, pos, dts, size, 0, AVINDEX_KEYFRAME);
     break;
  }
@@ -795,7 +810,7 @@
             sample_rate = ctx.sample_rate;
         }
     } else if(stream_type == FLV_STREAM_TYPE_VIDEO) {
-        size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);
+        size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK, 1);
     }
 
     if (st->codec->codec_id == AV_CODEC_ID_AAC ||
diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c
index 502da0f..b0f8a41 100644
--- a/libavformat/flvenc.c
+++ b/libavformat/flvenc.c
@@ -212,6 +212,11 @@
             } else {
                 framerate = 1 / av_q2d(s->streams[i]->codec->time_base);
             }
+            if (video_enc) {
+                av_log(s, AV_LOG_ERROR,
+                       "at most one video stream is supported in flv\n");
+                return AVERROR(EINVAL);
+            }
             video_enc = enc;
             if (enc->codec_tag == 0) {
                 av_log(s, AV_LOG_ERROR, "Video codec '%s' for stream %d is not compatible with FLV\n",
@@ -220,6 +225,11 @@
             }
             break;
         case AVMEDIA_TYPE_AUDIO:
+            if (audio_enc) {
+                av_log(s, AV_LOG_ERROR,
+                       "at most one audio stream is supported in flv\n");
+                return AVERROR(EINVAL);
+            }
             audio_enc = enc;
             if (get_audio_flags(s, enc) < 0)
                 return AVERROR_INVALIDDATA;
@@ -493,10 +503,13 @@
                 return ret;
     } else if (enc->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
                (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
+        if (!s->streams[pkt->stream_index]->nb_frames) {
         av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
                "use audio bitstream filter 'aac_adtstoasc' to fix it "
                "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
         return AVERROR_INVALIDDATA;
+        }
+        av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
     }
 
     if (flv->delay == AV_NOPTS_VALUE)
@@ -566,7 +579,6 @@
                               pkt->pts + flv->delay + pkt->duration);
     }
 
-    avio_flush(pb);
     av_free(data);
 
     return pb->error;
diff --git a/libavformat/framecrcenc.c b/libavformat/framecrcenc.c
index 92f2e91..df0ae79 100644
--- a/libavformat/framecrcenc.c
+++ b/libavformat/framecrcenc.c
@@ -46,7 +46,6 @@
     }
     av_strlcatf(buf, sizeof(buf), "\n");
     avio_write(s->pb, buf, strlen(buf));
-    avio_flush(s->pb);
     return 0;
 }
 
diff --git a/libavformat/gif.c b/libavformat/gif.c
index 31b0101..d459bd6 100644
--- a/libavformat/gif.c
+++ b/libavformat/gif.c
@@ -2,6 +2,8 @@
  * Animated GIF muxer
  * Copyright (c) 2000 Fabrice Bellard
  *
+ * first version by Francois Revol <revol@free.fr>
+ *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
@@ -19,333 +21,166 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-/*
- * First version by Francois Revol revol@free.fr
- *
- * Features and limitations:
- * - currently no compression is performed,
- *   in fact the size of the data is 9/8 the size of the image in 8bpp
- * - uses only a global standard palette
- * - tested with IE 5.0, Opera for BeOS, NetPositive (BeOS), and Mozilla (BeOS).
- *
- * Reference documents:
- * http://www.goice.co.jp/member/mo/formats/gif.html
- * http://astronomy.swin.edu.au/pbourke/dataformats/gif/
- * http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/GIF89a.txt
- *
- * this url claims to have an LZW algorithm not covered by Unisys patent:
- * http://www.msg.net/utility/whirlgif/gifencod.html
- * could help reduce the size of the files _a lot_...
- * some sites mentions an RLE type compression also.
- */
-
 #include "avformat.h"
+#include "internal.h"
+#include "libavutil/avassert.h"
+#include "libavutil/imgutils.h"
 #include "libavutil/log.h"
 #include "libavutil/opt.h"
 
-/* The GIF format uses reversed order for bitstreams... */
-/* at least they don't use PDP_ENDIAN :) */
-#define BITSTREAM_WRITER_LE
-
-#include "libavcodec/put_bits.h"
-
-/* bitstream minipacket size */
-#define GIF_CHUNKS 100
-
-/* slows down the decoding (and some browsers don't like it) */
-/* update on the 'some browsers don't like it issue from above:
- * this was probably due to missing 'Data Sub-block Terminator'
- * (byte 19) in the app_header */
-#define GIF_ADD_APP_HEADER // required to enable looping of animated gif
-
-typedef struct {
-    unsigned char r;
-    unsigned char g;
-    unsigned char b;
-} rgb_triplet;
-
-/* we use the standard 216 color palette */
-
-/* this script was used to create the palette:
- * for r in 00 33 66 99 cc ff; do
- *     for g in 00 33 66 99 cc ff; do
- *         echo -n "    "
- *         for b in 00 33 66 99 cc ff; do
- *             echo -n "{ 0x$r, 0x$g, 0x$b }, "
- *         done
- *         echo ""
- *     done
- * done
- */
-
-static const rgb_triplet gif_clut[216] = {
-    { 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x33 }, { 0x00, 0x00, 0x66 }, { 0x00, 0x00, 0x99 }, { 0x00, 0x00, 0xcc }, { 0x00, 0x00, 0xff },
-    { 0x00, 0x33, 0x00 }, { 0x00, 0x33, 0x33 }, { 0x00, 0x33, 0x66 }, { 0x00, 0x33, 0x99 }, { 0x00, 0x33, 0xcc }, { 0x00, 0x33, 0xff },
-    { 0x00, 0x66, 0x00 }, { 0x00, 0x66, 0x33 }, { 0x00, 0x66, 0x66 }, { 0x00, 0x66, 0x99 }, { 0x00, 0x66, 0xcc }, { 0x00, 0x66, 0xff },
-    { 0x00, 0x99, 0x00 }, { 0x00, 0x99, 0x33 }, { 0x00, 0x99, 0x66 }, { 0x00, 0x99, 0x99 }, { 0x00, 0x99, 0xcc }, { 0x00, 0x99, 0xff },
-    { 0x00, 0xcc, 0x00 }, { 0x00, 0xcc, 0x33 }, { 0x00, 0xcc, 0x66 }, { 0x00, 0xcc, 0x99 }, { 0x00, 0xcc, 0xcc }, { 0x00, 0xcc, 0xff },
-    { 0x00, 0xff, 0x00 }, { 0x00, 0xff, 0x33 }, { 0x00, 0xff, 0x66 }, { 0x00, 0xff, 0x99 }, { 0x00, 0xff, 0xcc }, { 0x00, 0xff, 0xff },
-    { 0x33, 0x00, 0x00 }, { 0x33, 0x00, 0x33 }, { 0x33, 0x00, 0x66 }, { 0x33, 0x00, 0x99 }, { 0x33, 0x00, 0xcc }, { 0x33, 0x00, 0xff },
-    { 0x33, 0x33, 0x00 }, { 0x33, 0x33, 0x33 }, { 0x33, 0x33, 0x66 }, { 0x33, 0x33, 0x99 }, { 0x33, 0x33, 0xcc }, { 0x33, 0x33, 0xff },
-    { 0x33, 0x66, 0x00 }, { 0x33, 0x66, 0x33 }, { 0x33, 0x66, 0x66 }, { 0x33, 0x66, 0x99 }, { 0x33, 0x66, 0xcc }, { 0x33, 0x66, 0xff },
-    { 0x33, 0x99, 0x00 }, { 0x33, 0x99, 0x33 }, { 0x33, 0x99, 0x66 }, { 0x33, 0x99, 0x99 }, { 0x33, 0x99, 0xcc }, { 0x33, 0x99, 0xff },
-    { 0x33, 0xcc, 0x00 }, { 0x33, 0xcc, 0x33 }, { 0x33, 0xcc, 0x66 }, { 0x33, 0xcc, 0x99 }, { 0x33, 0xcc, 0xcc }, { 0x33, 0xcc, 0xff },
-    { 0x33, 0xff, 0x00 }, { 0x33, 0xff, 0x33 }, { 0x33, 0xff, 0x66 }, { 0x33, 0xff, 0x99 }, { 0x33, 0xff, 0xcc }, { 0x33, 0xff, 0xff },
-    { 0x66, 0x00, 0x00 }, { 0x66, 0x00, 0x33 }, { 0x66, 0x00, 0x66 }, { 0x66, 0x00, 0x99 }, { 0x66, 0x00, 0xcc }, { 0x66, 0x00, 0xff },
-    { 0x66, 0x33, 0x00 }, { 0x66, 0x33, 0x33 }, { 0x66, 0x33, 0x66 }, { 0x66, 0x33, 0x99 }, { 0x66, 0x33, 0xcc }, { 0x66, 0x33, 0xff },
-    { 0x66, 0x66, 0x00 }, { 0x66, 0x66, 0x33 }, { 0x66, 0x66, 0x66 }, { 0x66, 0x66, 0x99 }, { 0x66, 0x66, 0xcc }, { 0x66, 0x66, 0xff },
-    { 0x66, 0x99, 0x00 }, { 0x66, 0x99, 0x33 }, { 0x66, 0x99, 0x66 }, { 0x66, 0x99, 0x99 }, { 0x66, 0x99, 0xcc }, { 0x66, 0x99, 0xff },
-    { 0x66, 0xcc, 0x00 }, { 0x66, 0xcc, 0x33 }, { 0x66, 0xcc, 0x66 }, { 0x66, 0xcc, 0x99 }, { 0x66, 0xcc, 0xcc }, { 0x66, 0xcc, 0xff },
-    { 0x66, 0xff, 0x00 }, { 0x66, 0xff, 0x33 }, { 0x66, 0xff, 0x66 }, { 0x66, 0xff, 0x99 }, { 0x66, 0xff, 0xcc }, { 0x66, 0xff, 0xff },
-    { 0x99, 0x00, 0x00 }, { 0x99, 0x00, 0x33 }, { 0x99, 0x00, 0x66 }, { 0x99, 0x00, 0x99 }, { 0x99, 0x00, 0xcc }, { 0x99, 0x00, 0xff },
-    { 0x99, 0x33, 0x00 }, { 0x99, 0x33, 0x33 }, { 0x99, 0x33, 0x66 }, { 0x99, 0x33, 0x99 }, { 0x99, 0x33, 0xcc }, { 0x99, 0x33, 0xff },
-    { 0x99, 0x66, 0x00 }, { 0x99, 0x66, 0x33 }, { 0x99, 0x66, 0x66 }, { 0x99, 0x66, 0x99 }, { 0x99, 0x66, 0xcc }, { 0x99, 0x66, 0xff },
-    { 0x99, 0x99, 0x00 }, { 0x99, 0x99, 0x33 }, { 0x99, 0x99, 0x66 }, { 0x99, 0x99, 0x99 }, { 0x99, 0x99, 0xcc }, { 0x99, 0x99, 0xff },
-    { 0x99, 0xcc, 0x00 }, { 0x99, 0xcc, 0x33 }, { 0x99, 0xcc, 0x66 }, { 0x99, 0xcc, 0x99 }, { 0x99, 0xcc, 0xcc }, { 0x99, 0xcc, 0xff },
-    { 0x99, 0xff, 0x00 }, { 0x99, 0xff, 0x33 }, { 0x99, 0xff, 0x66 }, { 0x99, 0xff, 0x99 }, { 0x99, 0xff, 0xcc }, { 0x99, 0xff, 0xff },
-    { 0xcc, 0x00, 0x00 }, { 0xcc, 0x00, 0x33 }, { 0xcc, 0x00, 0x66 }, { 0xcc, 0x00, 0x99 }, { 0xcc, 0x00, 0xcc }, { 0xcc, 0x00, 0xff },
-    { 0xcc, 0x33, 0x00 }, { 0xcc, 0x33, 0x33 }, { 0xcc, 0x33, 0x66 }, { 0xcc, 0x33, 0x99 }, { 0xcc, 0x33, 0xcc }, { 0xcc, 0x33, 0xff },
-    { 0xcc, 0x66, 0x00 }, { 0xcc, 0x66, 0x33 }, { 0xcc, 0x66, 0x66 }, { 0xcc, 0x66, 0x99 }, { 0xcc, 0x66, 0xcc }, { 0xcc, 0x66, 0xff },
-    { 0xcc, 0x99, 0x00 }, { 0xcc, 0x99, 0x33 }, { 0xcc, 0x99, 0x66 }, { 0xcc, 0x99, 0x99 }, { 0xcc, 0x99, 0xcc }, { 0xcc, 0x99, 0xff },
-    { 0xcc, 0xcc, 0x00 }, { 0xcc, 0xcc, 0x33 }, { 0xcc, 0xcc, 0x66 }, { 0xcc, 0xcc, 0x99 }, { 0xcc, 0xcc, 0xcc }, { 0xcc, 0xcc, 0xff },
-    { 0xcc, 0xff, 0x00 }, { 0xcc, 0xff, 0x33 }, { 0xcc, 0xff, 0x66 }, { 0xcc, 0xff, 0x99 }, { 0xcc, 0xff, 0xcc }, { 0xcc, 0xff, 0xff },
-    { 0xff, 0x00, 0x00 }, { 0xff, 0x00, 0x33 }, { 0xff, 0x00, 0x66 }, { 0xff, 0x00, 0x99 }, { 0xff, 0x00, 0xcc }, { 0xff, 0x00, 0xff },
-    { 0xff, 0x33, 0x00 }, { 0xff, 0x33, 0x33 }, { 0xff, 0x33, 0x66 }, { 0xff, 0x33, 0x99 }, { 0xff, 0x33, 0xcc }, { 0xff, 0x33, 0xff },
-    { 0xff, 0x66, 0x00 }, { 0xff, 0x66, 0x33 }, { 0xff, 0x66, 0x66 }, { 0xff, 0x66, 0x99 }, { 0xff, 0x66, 0xcc }, { 0xff, 0x66, 0xff },
-    { 0xff, 0x99, 0x00 }, { 0xff, 0x99, 0x33 }, { 0xff, 0x99, 0x66 }, { 0xff, 0x99, 0x99 }, { 0xff, 0x99, 0xcc }, { 0xff, 0x99, 0xff },
-    { 0xff, 0xcc, 0x00 }, { 0xff, 0xcc, 0x33 }, { 0xff, 0xcc, 0x66 }, { 0xff, 0xcc, 0x99 }, { 0xff, 0xcc, 0xcc }, { 0xff, 0xcc, 0xff },
-    { 0xff, 0xff, 0x00 }, { 0xff, 0xff, 0x33 }, { 0xff, 0xff, 0x66 }, { 0xff, 0xff, 0x99 }, { 0xff, 0xff, 0xcc }, { 0xff, 0xff, 0xff },
-};
-
-/* GIF header */
 static int gif_image_write_header(AVIOContext *pb, int width, int height,
                                   int loop_count, uint32_t *palette)
 {
     int i;
-    unsigned int v;
 
     avio_write(pb, "GIF", 3);
     avio_write(pb, "89a", 3);
     avio_wl16(pb, width);
     avio_wl16(pb, height);
 
-    avio_w8(pb, 0xf7); /* flags: global clut, 256 entries */
-    avio_w8(pb, 0x1f); /* background color index */
-    avio_w8(pb, 0);    /* aspect ratio */
-
-    /* the global palette */
-    if (!palette) {
-        avio_write(pb, (const unsigned char *)gif_clut, 216 * 3);
-        for (i = 0; i < ((256 - 216) * 3); i++)
-            avio_w8(pb, 0);
-    } else {
+    if (palette) {
+        avio_w8(pb, 0xf7); /* flags: global clut, 256 entries */
+        avio_w8(pb, 0x1f); /* background color index */
+        avio_w8(pb, 0);    /* aspect ratio */
         for (i = 0; i < 256; i++) {
-            v = palette[i];
-            avio_w8(pb, (v >> 16) & 0xff);
-            avio_w8(pb, (v >>  8) & 0xff);
-            avio_w8(pb, (v)       & 0xff);
+            const uint32_t v = palette[i] & 0xffffff;
+            avio_wb24(pb, v);
         }
+    } else {
+        avio_w8(pb, 0); /* flags */
+        avio_w8(pb, 0); /* background color index */
+        avio_w8(pb, 0); /* aspect ratio */
     }
 
-    /* update: this is the 'NETSCAPE EXTENSION' that allows for looped animated
-     * GIF, see http://members.aol.com/royalef/gifabout.htm#net-extension
-     *
-     * byte   1       : 33 (hex 0x21) GIF Extension code
-     * byte   2       : 255 (hex 0xFF) Application Extension Label
-     * byte   3       : 11 (hex (0x0B) Length of Application Block
-     *                          (eleven bytes of data to follow)
-     * bytes  4 to 11 : "NETSCAPE"
-     * bytes 12 to 14 : "2.0"
-     * byte  15       : 3 (hex 0x03) Length of Data Sub-Block
-     *                          (three bytes of data to follow)
-     * byte  16       : 1 (hex 0x01)
-     * bytes 17 to 18 : 0 to 65535, an unsigned integer in
-     *                          lo-hi byte format. This indicate the
-     *                          number of iterations the loop should
-     *                          be executed.
-     * bytes 19       : 0 (hex 0x00) a Data Sub-block Terminator
-     */
-
-    /* application extension header */
-#ifdef GIF_ADD_APP_HEADER
-    if (loop_count >= 0 && loop_count <= 65535) {
-        avio_w8(pb, 0x21);
-        avio_w8(pb, 0xff);
-        avio_w8(pb, 0x0b);
-        // bytes 4 to 14
-        avio_write(pb, "NETSCAPE2.0", sizeof("NETSCAPE2.0") - 1);
-        avio_w8(pb, 0x03); // byte 15
-        avio_w8(pb, 0x01); // byte 16
-        avio_wl16(pb, (uint16_t)loop_count);
-        avio_w8(pb, 0x00); // byte 19
-    }
-#endif
-    return 0;
-}
-
-/* this is maybe slow, but allows for extensions */
-static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
-{
-    return (((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6);
-}
-
-static int gif_image_write_image(AVIOContext *pb,
-                                 int x1, int y1, int width, int height,
-                                 const uint8_t *buf, int linesize, int pix_fmt)
-{
-    PutBitContext p;
-    uint8_t buffer[200]; /* 100 * 9 / 8 = 113 */
-    int i, left, w, v;
-    const uint8_t *ptr;
-    /* image block */
-
-    avio_w8(pb, 0x2c);
-    avio_wl16(pb, x1);
-    avio_wl16(pb, y1);
-    avio_wl16(pb, width);
-    avio_wl16(pb, height);
-    avio_w8(pb, 0x00); /* flags */
-    /* no local clut */
-
-    avio_w8(pb, 0x08);
-
-    left = width * height;
-
-    init_put_bits(&p, buffer, 130);
-
-/*
- * the thing here is the bitstream is written as little packets, with a size
- * byte before but it's still the same bitstream between packets (no flush !)
- */
-    ptr = buf;
-    w   = width;
-    while (left > 0) {
-        put_bits(&p, 9, 0x0100); /* clear code */
-
-        for (i = (left < GIF_CHUNKS) ? left : GIF_CHUNKS; i; i--) {
-            if (pix_fmt == AV_PIX_FMT_RGB24) {
-                v    = gif_clut_index(ptr[0], ptr[1], ptr[2]);
-                ptr += 3;
-            } else {
-                v = *ptr++;
-            }
-            put_bits(&p, 9, v);
-            if (--w == 0) {
-                w    = width;
-                buf += linesize;
-                ptr  = buf;
-            }
-        }
-
-        if (left <= GIF_CHUNKS) {
-            put_bits(&p, 9, 0x101); /* end of stream */
-            flush_put_bits(&p);
-        }
-        if (put_bits_ptr(&p) - p.buf > 0) {
-            avio_w8(pb, put_bits_ptr(&p) - p.buf); /* byte count of the packet */
-            avio_write(pb, p.buf, put_bits_ptr(&p) - p.buf); /* the actual buffer */
-            p.buf_ptr = p.buf; /* dequeue the bytes off the bitstream */
-        }
-        left -= GIF_CHUNKS;
-    }
-    avio_w8(pb, 0x00); /* end of image block */
+    /* "NETSCAPE EXTENSION" for looped animation GIF */
+    avio_w8(pb, 0x21); /* GIF Extension code */
+    avio_w8(pb, 0xff); /* Application Extension Label */
+    avio_w8(pb, 0x0b); /* Length of Application Block */
+    avio_write(pb, "NETSCAPE2.0", sizeof("NETSCAPE2.0") - 1);
+    avio_w8(pb, 0x03); /* Length of Data Sub-Block */
+    avio_w8(pb, 0x01);
+    avio_wl16(pb, (uint16_t)loop_count);
+    avio_w8(pb, 0x00); /* Data Sub-block Terminator */
 
     return 0;
 }
 
 typedef struct {
     AVClass *class;         /** Class for private options. */
-    int64_t time, file_time;
-    uint8_t buffer[100]; /* data chunks */
     int loop;
+    int last_delay;
+    AVPacket *prev_pkt;
+    int duration;
 } GIFContext;
 
 static int gif_write_header(AVFormatContext *s)
 {
     GIFContext *gif = s->priv_data;
     AVIOContext *pb = s->pb;
-    AVCodecContext *enc, *video_enc;
-    int i, width, height /*, rate*/;
+    AVCodecContext *video_enc;
+    int width, height;
+    uint32_t palette[AVPALETTE_COUNT];
 
-/* XXX: do we reject audio streams or just ignore them ?
- *  if (s->nb_streams > 1)
- *      return -1;
- */
-    gif->time      = 0;
-    gif->file_time = 0;
-
-    video_enc = NULL;
-    for (i = 0; i < s->nb_streams; i++) {
-        enc = s->streams[i]->codec;
-        if (enc->codec_type != AVMEDIA_TYPE_AUDIO)
-            video_enc = enc;
-    }
-
-    if (!video_enc) {
-        av_free(gif);
-        return -1;
-    } else {
-        width  = video_enc->width;
-        height = video_enc->height;
-//        rate = video_enc->time_base.den;
-    }
-
-    if (video_enc->pix_fmt != AV_PIX_FMT_RGB24) {
+    if (s->nb_streams != 1 ||
+        s->streams[0]->codec->codec_type != AVMEDIA_TYPE_VIDEO ||
+        s->streams[0]->codec->codec_id   != AV_CODEC_ID_GIF) {
         av_log(s, AV_LOG_ERROR,
-               "ERROR: gif only handles the rgb24 pixel format. Use -pix_fmt rgb24.\n");
-        return AVERROR(EIO);
+               "GIF muxer supports only a single video GIF stream.\n");
+        return AVERROR(EINVAL);
     }
 
-    gif_image_write_header(pb, width, height, gif->loop, NULL);
+    video_enc = s->streams[0]->codec;
+    width  = video_enc->width;
+    height = video_enc->height;
+
+    avpriv_set_pts_info(s->streams[0], 64, 1, 100);
+    if (avpriv_set_systematic_pal2(palette, video_enc->pix_fmt) < 0) {
+        av_assert0(video_enc->pix_fmt == AV_PIX_FMT_PAL8);
+        gif_image_write_header(pb, width, height, gif->loop, NULL);
+    } else {
+        gif_image_write_header(pb, width, height, gif->loop, palette);
+    }
 
     avio_flush(s->pb);
     return 0;
 }
 
-static int gif_write_video(AVFormatContext *s, AVCodecContext *enc,
-                           const uint8_t *buf, int size)
+static int flush_packet(AVFormatContext *s, AVPacket *new)
 {
+    GIFContext *gif = s->priv_data;
+    int size;
     AVIOContext *pb = s->pb;
-    int jiffies;
+    uint8_t flags = 0x4, transparent_color_index = 0x1f;
+    const uint32_t *palette;
+    AVPacket *pkt = gif->prev_pkt;
+
+    if (!pkt)
+        return 0;
+
+    /* Mark one colour as transparent if the input palette contains at least
+     * one colour that is more than 50% transparent. */
+    palette = (uint32_t*)av_packet_get_side_data(pkt, AV_PKT_DATA_PALETTE, &size);
+    if (palette && size != AVPALETTE_SIZE) {
+        av_log(s, AV_LOG_ERROR, "Invalid palette extradata\n");
+        return AVERROR_INVALIDDATA;
+    }
+    if (palette) {
+        unsigned i, smallest_alpha = 0xff;
+
+        for (i = 0; i < AVPALETTE_COUNT; i++) {
+            const uint32_t v = palette[i];
+            if (v >> 24 < smallest_alpha) {
+                smallest_alpha = v >> 24;
+                transparent_color_index = i;
+            }
+        }
+        if (smallest_alpha < 128)
+            flags |= 0x1; /* Transparent Color Flag */
+    }
+
+    if (new && new->pts != AV_NOPTS_VALUE)
+        gif->duration = av_clip_uint16(new->pts - gif->prev_pkt->pts);
+    else if (!new && gif->last_delay >= 0)
+        gif->duration = gif->last_delay;
 
     /* graphic control extension block */
     avio_w8(pb, 0x21);
     avio_w8(pb, 0xf9);
     avio_w8(pb, 0x04); /* block size */
-    avio_w8(pb, 0x04); /* flags */
-
-    /* 1 jiffy is 1/70 s */
-    /* the delay_time field indicates the number of jiffies - 1 */
-    /* XXX: should use delay, in order to be more accurate */
-    /* instead of using the same rounded value each time */
-    /* XXX: don't even remember if I really use it for now */
-    jiffies = (70 * enc->time_base.num / enc->time_base.den) - 1;
-
-    avio_wl16(pb, jiffies);
-
-    avio_w8(pb, 0x1f); /* transparent color index */
+    avio_w8(pb, flags);
+    avio_wl16(pb, gif->duration);
+    avio_w8(pb, transparent_color_index);
     avio_w8(pb, 0x00);
 
-    gif_image_write_image(pb, 0, 0, enc->width, enc->height,
-                          buf, enc->width * 3, AV_PIX_FMT_RGB24);
+    avio_write(pb, pkt->data, pkt->size);
 
-    avio_flush(s->pb);
+    av_free_packet(gif->prev_pkt);
+    if (new)
+        av_copy_packet(gif->prev_pkt, new);
+
     return 0;
 }
 
 static int gif_write_packet(AVFormatContext *s, AVPacket *pkt)
 {
-    AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
-    if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
-        return 0; /* just ignore audio */
-    else
-        return gif_write_video(s, codec, pkt->data, pkt->size);
+    GIFContext *gif = s->priv_data;
+
+    if (!gif->prev_pkt) {
+        gif->prev_pkt = av_malloc(sizeof(*gif->prev_pkt));
+        if (!gif->prev_pkt)
+            return AVERROR(ENOMEM);
+        return av_copy_packet(gif->prev_pkt, pkt);
+    }
+    return flush_packet(s, pkt);
 }
 
 static int gif_write_trailer(AVFormatContext *s)
 {
+    GIFContext *gif = s->priv_data;
     AVIOContext *pb = s->pb;
 
+    flush_packet(s, NULL);
+    av_freep(&gif->prev_pkt);
     avio_w8(pb, 0x3b);
 
     return 0;
@@ -356,6 +191,8 @@
 static const AVOption options[] = {
     { "loop", "Number of times to loop the output.", OFFSET(loop),
       AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 65535, ENC },
+    { "final_delay", "Force delay (in ms) after the last frame", OFFSET(last_delay),
+      AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 65535, ENC },
     { NULL },
 };
 
@@ -373,9 +210,10 @@
     .extensions     = "gif",
     .priv_data_size = sizeof(GIFContext),
     .audio_codec    = AV_CODEC_ID_NONE,
-    .video_codec    = AV_CODEC_ID_RAWVIDEO,
+    .video_codec    = AV_CODEC_ID_GIF,
     .write_header   = gif_write_header,
     .write_packet   = gif_write_packet,
     .write_trailer  = gif_write_trailer,
     .priv_class     = &gif_muxer_class,
+    .flags          = AVFMT_VARIABLE_FPS,
 };
diff --git a/libavformat/gifdec.c b/libavformat/gifdec.c
index 1122849..e05dc41 100644
--- a/libavformat/gifdec.c
+++ b/libavformat/gifdec.c
@@ -44,6 +44,13 @@
      */
     int min_delay;
     int default_delay;
+
+    /**
+     * loop options
+     */
+    int total_iter;
+    int iter_count;
+    int ignore_loop;
 } GIFDemuxContext;
 
 /**
@@ -156,6 +163,17 @@
         /* skip the rest of the Graphic Control Extension block */
         if ((ret = avio_skip(pb, sb_size - 3)) < 0 )
             return ret;
+    } else if (ext_label == GIF_APP_EXT_LABEL) {
+        uint8_t netscape_ext[sizeof(NETSCAPE_EXT_STR)-1 + 2];
+
+        if ((sb_size = avio_r8(pb)) != strlen(NETSCAPE_EXT_STR))
+            return 0;
+        ret = avio_read(pb, netscape_ext, sizeof(netscape_ext));
+        if (ret < sizeof(netscape_ext))
+            return ret;
+        gdc->total_iter = avio_rl16(pb);
+        if (gdc->total_iter == 0)
+            gdc->total_iter = -1;
     }
 
     if ((ret = gif_skip_subblocks(pb)) < 0)
@@ -268,9 +286,12 @@
         }
     }
 
-    if (ret >= 0 && !frame_parsed) {
+    if ((ret >= 0 && !frame_parsed) || ret == AVERROR_EOF) {
         /* This might happen when there is no image block
          * between extension blocks and GIF_TRAILER or EOF */
+        if (!gdc->ignore_loop && (block_label == GIF_TRAILER || url_feof(pb))
+            && (gdc->total_iter < 0 || ++gdc->iter_count < gdc->total_iter))
+            return avio_seek(pb, 0, SEEK_SET);
         return AVERROR_EOF;
     } else
         return ret;
@@ -279,6 +300,7 @@
 static const AVOption options[] = {
     { "min_delay"    , "minimum valid delay between frames (in hundredths of second)", offsetof(GIFDemuxContext, min_delay)    , AV_OPT_TYPE_INT, {.i64 = GIF_MIN_DELAY}    , 0, 100 * 60, AV_OPT_FLAG_DECODING_PARAM },
     { "default_delay", "default delay between frames (in hundredths of second)"      , offsetof(GIFDemuxContext, default_delay), AV_OPT_TYPE_INT, {.i64 = GIF_DEFAULT_DELAY}, 0, 100 * 60, AV_OPT_FLAG_DECODING_PARAM },
+    { "ignore_loop"  , "ignore loop setting (netscape extension)"                    , offsetof(GIFDemuxContext, ignore_loop)  , AV_OPT_TYPE_INT, {.i64 = 1}                , 0,        1, AV_OPT_FLAG_DECODING_PARAM },
     { NULL },
 };
 
diff --git a/libavformat/gxfenc.c b/libavformat/gxfenc.c
index 28acb74..9322f7b 100644
--- a/libavformat/gxfenc.c
+++ b/libavformat/gxfenc.c
@@ -950,8 +950,6 @@
         gxf->packet_count = 0;
     }
 
-    avio_flush(pb);
-
     return 0;
 }
 
diff --git a/libavformat/hls.c b/libavformat/hls.c
index 0b7d050..7de6059 100644
--- a/libavformat/hls.c
+++ b/libavformat/hls.c
@@ -110,7 +110,7 @@
 static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
 {
     int len = ff_get_line(s, buf, maxlen);
-    while (len > 0 && isspace(buf[len - 1]))
+    while (len > 0 && av_isspace(buf[len - 1]))
         buf[--len] = '\0';
     return len;
 }
@@ -590,12 +590,14 @@
         /* Create new AVStreams for each stream in this variant */
         for (j = 0; j < v->ctx->nb_streams; j++) {
             AVStream *st = avformat_new_stream(s, NULL);
+            AVStream *ist = v->ctx->streams[j];
             if (!st) {
                 ret = AVERROR(ENOMEM);
                 goto fail;
             }
             ff_program_add_stream_index(s, i, stream_offset + j);
             st->id = i;
+            avpriv_set_pts_info(st, ist->pts_wrap_bits, ist->time_base.num, ist->time_base.den);
             avcodec_copy_context(st->codec, v->ctx->streams[j]->codec);
             if (v->bandwidth)
                 av_dict_set(&st->metadata, "variant_bitrate", bitrate_str,
diff --git a/libavformat/hlsproto.c b/libavformat/hlsproto.c
index 79cf0e4..4e35043 100644
--- a/libavformat/hlsproto.c
+++ b/libavformat/hlsproto.c
@@ -71,7 +71,7 @@
 static int read_chomp_line(AVIOContext *s, char *buf, int maxlen)
 {
     int len = ff_get_line(s, buf, maxlen);
-    while (len > 0 && isspace(buf[len - 1]))
+    while (len > 0 && av_isspace(buf[len - 1]))
         buf[--len] = '\0';
     return len;
 }
@@ -204,19 +204,6 @@
                nested_url);
         ret = AVERROR(EINVAL);
         goto fail;
-#if FF_API_APPLEHTTP_PROTO
-    } else if (av_strstart(uri, "applehttp+", &nested_url)) {
-        av_strlcpy(s->playlisturl, nested_url, sizeof(s->playlisturl));
-        av_log(h, AV_LOG_WARNING,
-               "The applehttp protocol is deprecated, use hls+%s as url "
-               "instead.\n", nested_url);
-    } else if (av_strstart(uri, "applehttp://", &nested_url)) {
-        av_strlcpy(s->playlisturl, "http://", sizeof(s->playlisturl));
-        av_strlcat(s->playlisturl, nested_url, sizeof(s->playlisturl));
-        av_log(h, AV_LOG_WARNING,
-               "The applehttp protocol is deprecated, use hls+http://%s as url "
-               "instead.\n", nested_url);
-#endif
     } else {
         av_log(h, AV_LOG_ERROR, "Unsupported url %s\n", uri);
         ret = AVERROR(EINVAL);
@@ -326,17 +313,6 @@
     goto start;
 }
 
-#if FF_API_APPLEHTTP_PROTO
-URLProtocol ff_applehttp_protocol = {
-    .name           = "applehttp",
-    .url_open       = hls_open,
-    .url_read       = hls_read,
-    .url_close      = hls_close,
-    .flags          = URL_PROTOCOL_FLAG_NESTED_SCHEME,
-    .priv_data_size = sizeof(HLSContext),
-};
-#endif
-
 URLProtocol ff_hls_protocol = {
     .name           = "hls",
     .url_open       = hls_open,
diff --git a/libavformat/http.c b/libavformat/http.c
index 5946000..7f33eca 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -70,13 +70,13 @@
 #define OFFSET(x) offsetof(HTTPContext, x)
 #define D AV_OPT_FLAG_DECODING_PARAM
 #define E AV_OPT_FLAG_ENCODING_PARAM
-#define DEC AV_OPT_FLAG_DECODING_PARAM
+#define DEFAULT_USER_AGENT "Mozilla/5.0 Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
 static const AVOption options[] = {
 {"seekable", "control seekability of connection", OFFSET(seekable), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, D },
 {"chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
 {"headers", "set custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
 {"content_type", "force a content type", OFFSET(content_type), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
-{"user-agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC},
+{"user-agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, {.str = DEFAULT_USER_AGENT}, 0, 0, D },
 {"multiple_requests", "use persistent connections", OFFSET(multiple_requests), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, D|E },
 {"post_data", "set custom HTTP post data", OFFSET(post_data), AV_OPT_TYPE_BINARY, .flags = D|E },
 {"timeout", "set timeout of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D|E },
@@ -298,9 +298,9 @@
 
     p = line;
     if (line_count == 0) {
-        while (!isspace(*p) && *p != '\0')
+        while (!av_isspace(*p) && *p != '\0')
             p++;
-        while (isspace(*p))
+        while (av_isspace(*p))
             p++;
         s->http_code = strtol(p, &end, 10);
 
@@ -325,7 +325,7 @@
         *p = '\0';
         tag = line;
         p++;
-        while (isspace(*p))
+        while (av_isspace(*p))
             p++;
         if (!av_strcasecmp(tag, "Location")) {
             av_strlcpy(s->location, p, sizeof(s->location));
@@ -540,8 +540,7 @@
     /* set default headers if needed */
     if (!has_header(s->headers, "\r\nUser-Agent: "))
         len += av_strlcatf(headers + len, sizeof(headers) - len,
-                           "User-Agent: %s\r\n",
-                           s->user_agent ? s->user_agent : LIBAVFORMAT_IDENT);
+                           "User-Agent: %s\r\n", s->user_agent);
     if (!has_header(s->headers, "\r\nAccept: "))
         len += av_strlcpy(headers + len, "Accept: */*\r\n",
                           sizeof(headers) - len);
diff --git a/libavformat/httpauth.c b/libavformat/httpauth.c
index e97aee1..5ca48b9 100644
--- a/libavformat/httpauth.c
+++ b/libavformat/httpauth.c
@@ -27,7 +27,6 @@
 #include "libavutil/md5.h"
 #include "urldecode.h"
 #include "avformat.h"
-#include <ctype.h>
 
 static void handle_basic_params(HTTPAuthState *state, const char *key,
                                 int key_len, char **dest, int *dest_len)
@@ -80,8 +79,8 @@
     char *ptr = strstr(qop, "auth");
     char *end = ptr + strlen("auth");
 
-    if (ptr && (!*end || isspace(*end) || *end == ',') &&
-        (ptr == qop || isspace(ptr[-1]) || ptr[-1] == ',')) {
+    if (ptr && (!*end || av_isspace(*end) || *end == ',') &&
+        (ptr == qop || av_isspace(ptr[-1]) || ptr[-1] == ',')) {
         av_strlcpy(qop, "auth", size);
     } else {
         qop[0] = 0;
diff --git a/libavformat/icodec.c b/libavformat/icodec.c
index 37909aa..fa308da 100644
--- a/libavformat/icodec.c
+++ b/libavformat/icodec.c
@@ -53,7 +53,7 @@
 {
     IcoDemuxContext *ico = s->priv_data;
     AVIOContext *pb = s->pb;
-    int i;
+    int i, codec;
 
     avio_skip(pb, 4);
     ico->nb_images = avio_rl16(pb);
@@ -88,7 +88,8 @@
         if (avio_seek(pb, ico->images[i].offset, SEEK_SET) < 0)
             break;
 
-        switch(avio_rl32(pb)) {
+        codec = avio_rl32(pb);
+        switch (codec) {
         case MKTAG(0x89, 'P', 'N', 'G'):
             st->codec->codec_id = AV_CODEC_ID_PNG;
             st->codec->width    = 0;
@@ -106,7 +107,7 @@
                 st->codec->height = tmp / 2;
             break;
         default:
-            av_log_ask_for_sample(s, "unsupported codec\n");
+            avpriv_request_sample(s, "codec %d", codec);
             return AVERROR_INVALIDDATA;
         }
     }
diff --git a/libavformat/icoenc.c b/libavformat/icoenc.c
index 3e6a1ea..561c6ca 100644
--- a/libavformat/icoenc.c
+++ b/libavformat/icoenc.c
@@ -151,8 +151,6 @@
             avio_w8(pb, 0x00); // Write bitmask (opaque)
     }
 
-    avio_flush(pb);
-
     return 0;
 }
 
diff --git a/libavformat/id3v2.c b/libavformat/id3v2.c
index 2cab5ac..3567bbc 100644
--- a/libavformat/id3v2.c
+++ b/libavformat/id3v2.c
@@ -433,7 +433,7 @@
 static void free_apic(void *obj)
 {
     ID3v2ExtraMetaAPIC *apic = obj;
-    av_freep(&apic->data);
+    av_buffer_unref(&apic->buf);
     av_freep(&apic->description);
     av_freep(&apic);
 }
@@ -489,10 +489,10 @@
         goto fail;
     }
 
-    apic->len   = taglen;
-    apic->data  = av_malloc(taglen);
-    if (!apic->data || !apic->len || avio_read(pb, apic->data, taglen) != taglen)
+    apic->buf = av_buffer_alloc(taglen + FF_INPUT_BUFFER_PADDING_SIZE);
+    if (!apic->buf || !taglen || avio_read(pb, apic->buf->data, taglen) != taglen)
         goto fail;
+    memset(apic->buf->data + taglen, 0, FF_INPUT_BUFFER_PADDING_SIZE);
 
     new_extra->tag    = "APIC";
     new_extra->data   = apic;
@@ -846,14 +846,13 @@
         av_dict_set(&st->metadata, "comment", apic->type, 0);
 
         av_init_packet(&st->attached_pic);
-        st->attached_pic.data         = apic->data;
-        st->attached_pic.size         = apic->len;
-        st->attached_pic.destruct     = av_destruct_packet;
+        st->attached_pic.buf          = apic->buf;
+        st->attached_pic.data         = apic->buf->data;
+        st->attached_pic.size         = apic->buf->size - FF_INPUT_BUFFER_PADDING_SIZE;
         st->attached_pic.stream_index = st->index;
         st->attached_pic.flags       |= AV_PKT_FLAG_KEY;
 
-        apic->data = NULL;
-        apic->len  = 0;
+        apic->buf = NULL;
     }
 
     return 0;
diff --git a/libavformat/id3v2.h b/libavformat/id3v2.h
index f727010..e893922 100644
--- a/libavformat/id3v2.h
+++ b/libavformat/id3v2.h
@@ -67,8 +67,7 @@
 } ID3v2ExtraMetaGEOB;
 
 typedef struct ID3v2ExtraMetaAPIC {
-    uint8_t     *data;
-    int          len;
+    AVBufferRef *buf;
     const char  *type;
     uint8_t     *description;
     enum AVCodecID id;
diff --git a/libavformat/idcin.c b/libavformat/idcin.c
index c32607d..2a8af40 100644
--- a/libavformat/idcin.c
+++ b/libavformat/idcin.c
@@ -68,7 +68,7 @@
  *       transmitting them to the video decoder
  */
 
-#include "libavutil/audioconvert.h"
+#include "libavutil/channel_layout.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
 #include "avformat.h"
diff --git a/libavformat/iff.c b/libavformat/iff.c
index 348026a..1efc147 100644
--- a/libavformat/iff.c
+++ b/libavformat/iff.c
@@ -250,6 +250,11 @@
             break;
 
         case ID_CMAP:
+            if (data_size < 3 || data_size > 768 || data_size % 3) {
+                 av_log(s, AV_LOG_ERROR, "Invalid CMAP chunk size %d\n",
+                        data_size);
+                 return AVERROR_INVALIDDATA;
+            }
             st->codec->extradata_size = data_size + IFF_EXTRA_VIDEO_SIZE;
             st->codec->extradata      = av_malloc(data_size + IFF_EXTRA_VIDEO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
             if (!st->codec->extradata)
@@ -296,7 +301,7 @@
             else if (fmt_size == sizeof(deep_abgr) && !memcmp(fmt, deep_abgr, sizeof(deep_abgr)))
                 st->codec->pix_fmt = AV_PIX_FMT_ABGR;
             else {
-                av_log_ask_for_sample(s, "unsupported color format\n");
+                avpriv_request_sample(s, "color format %.16s", fmt);
                 return AVERROR_PATCHWELCOME;
             }
             break;
@@ -362,7 +367,7 @@
             } else if (iff->maud_bits ==  8 && iff->maud_compression == 3) {
                 st->codec->codec_id = AV_CODEC_ID_PCM_MULAW;
             } else {
-                av_log_ask_for_sample(s, "unsupported compression %d and bit depth %d\n", iff->maud_compression, iff->maud_bits);
+                avpriv_request_sample(s, "compression %d and bit depth %d", iff->maud_compression, iff->maud_bits);
                 return AVERROR_PATCHWELCOME;
             }
 
@@ -410,6 +415,7 @@
             if (!st->codec->extradata)
                 return AVERROR(ENOMEM);
         }
+        av_assert0(st->codec->extradata_size >= IFF_EXTRA_VIDEO_SIZE);
         buf = st->codec->extradata;
         bytestream_put_be16(&buf, IFF_EXTRA_VIDEO_SIZE);
         bytestream_put_byte(&buf, iff->bitmap_compression);
diff --git a/libavformat/ilbc.c b/libavformat/ilbc.c
index 7a23b2f..3f154ce 100644
--- a/libavformat/ilbc.c
+++ b/libavformat/ilbc.c
@@ -56,7 +56,6 @@
 static int ilbc_write_packet(AVFormatContext *s, AVPacket *pkt)
 {
     avio_write(s->pb, pkt->data, pkt->size);
-    avio_flush(s->pb);
     return 0;
 }
 
diff --git a/libavformat/img2.c b/libavformat/img2.c
index eee100a..7539359 100644
--- a/libavformat/img2.c
+++ b/libavformat/img2.c
@@ -50,7 +50,6 @@
     { AV_CODEC_ID_RAWVIDEO,   "y"        },
     { AV_CODEC_ID_RAWVIDEO,   "raw"      },
     { AV_CODEC_ID_BMP,        "bmp"      },
-    { AV_CODEC_ID_GIF,        "gif"      },
     { AV_CODEC_ID_TARGA,      "tga"      },
     { AV_CODEC_ID_TIFF,       "tiff"     },
     { AV_CODEC_ID_TIFF,       "tif"      },
@@ -67,13 +66,14 @@
     { AV_CODEC_ID_SUNRAST,    "im32"     },
     { AV_CODEC_ID_SUNRAST,    "sunras"   },
     { AV_CODEC_ID_JPEG2000,   "j2c"      },
-    { AV_CODEC_ID_JPEG2000,   "j2k"      },
     { AV_CODEC_ID_JPEG2000,   "jp2"      },
     { AV_CODEC_ID_JPEG2000,   "jpc"      },
+    { AV_CODEC_ID_JPEG2000,   "j2k"      },
     { AV_CODEC_ID_DPX,        "dpx"      },
     { AV_CODEC_ID_EXR,        "exr"      },
     { AV_CODEC_ID_PICTOR,     "pic"      },
     { AV_CODEC_ID_V210X,      "yuv10"    },
+    { AV_CODEC_ID_WEBP,       "webp"     },
     { AV_CODEC_ID_XBM,        "xbm"      },
     { AV_CODEC_ID_XFACE,      "xface"    },
     { AV_CODEC_ID_XWD,        "xwd"      },
diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
index 882abb9..12cf462 100644
--- a/libavformat/img2dec.c
+++ b/libavformat/img2dec.c
@@ -52,8 +52,8 @@
     int split_planes;       /**< use independent file for each Y, U, V plane */
     char path[1024];
     char *pixel_format;     /**< Set by a private option. */
-    char *video_size;       /**< Set by a private option. */
-    char *framerate;        /**< Set by a private option. */
+    int width, height;      /**< Set by a private option. */
+    AVRational framerate;   /**< Set by a private option. */
     int loop;
     enum { PT_GLOB_SEQUENCE, PT_GLOB, PT_SEQUENCE } pattern_type;
     int use_glob;
@@ -193,11 +193,9 @@
 static int img_read_header(AVFormatContext *s1)
 {
     VideoDemuxData *s = s1->priv_data;
-    int first_index, last_index, ret = 0;
-    int width = 0, height = 0;
+    int first_index, last_index;
     AVStream *st;
     enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
-    AVRational framerate;
 
     s1->ctx_flags |= AVFMTCTX_NOHEADER;
 
@@ -212,17 +210,6 @@
                s->pixel_format);
         return AVERROR(EINVAL);
     }
-    if (s->video_size &&
-        (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
-        av_log(s, AV_LOG_ERROR,
-               "Could not parse video size: %s.\n", s->video_size);
-        return ret;
-    }
-    if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
-        av_log(s, AV_LOG_ERROR,
-               "Could not parse framerate: %s.\n", s->framerate);
-        return ret;
-    }
 
     av_strlcpy(s->path, s1->filename, sizeof(s->path));
     s->img_number = 0;
@@ -236,11 +223,11 @@
         st->need_parsing = AVSTREAM_PARSE_FULL;
     }
 
-    avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
+    avpriv_set_pts_info(st, 60, s->framerate.den, s->framerate.num);
 
-    if (width && height) {
-        st->codec->width  = width;
-        st->codec->height = height;
+    if (s->width && s->height) {
+        st->codec->width  = s->width;
+        st->codec->height = s->height;
     }
 
     if (!s->is_pipe) {
@@ -444,7 +431,7 @@
 #define OFFSET(x) offsetof(VideoDemuxData, x)
 #define DEC AV_OPT_FLAG_DECODING_PARAM
 static const AVOption options[] = {
-    { "framerate",    "set the video framerate",             OFFSET(framerate),    AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0,       DEC },
+    { "framerate",    "set the video framerate",             OFFSET(framerate),    AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0,   DEC },
     { "loop",         "force loop over input file sequence", OFFSET(loop),         AV_OPT_TYPE_INT,    {.i64 = 0   }, 0, 1,       DEC },
 
     { "pattern_type", "set pattern type",                    OFFSET(pattern_type), AV_OPT_TYPE_INT,    {.i64=PT_GLOB_SEQUENCE}, 0,       INT_MAX, DEC, "pattern_type"},
@@ -455,7 +442,7 @@
     { "pixel_format", "set video pixel format",              OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0,       DEC },
     { "start_number", "set first number in the sequence",    OFFSET(start_number), AV_OPT_TYPE_INT,    {.i64 = 0   }, 0, INT_MAX, DEC },
     { "start_number_range", "set range for looking at the first sequence number", OFFSET(start_number_range), AV_OPT_TYPE_INT, {.i64 = 5}, 1, INT_MAX, DEC },
-    { "video_size",   "set video size",                      OFFSET(video_size),   AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0,       DEC },
+    { "video_size",   "set video size",                      OFFSET(width),        AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0,   DEC },
     { "frame_size",   "force frame size in bytes",           OFFSET(frame_size),   AV_OPT_TYPE_INT,    {.i64 = 0   }, 0, INT_MAX, DEC },
     { NULL },
 };
diff --git a/libavformat/img2enc.c b/libavformat/img2enc.c
index 67b5819..fb297da 100644
--- a/libavformat/img2enc.c
+++ b/libavformat/img2enc.c
@@ -36,7 +36,7 @@
     int is_pipe;
     int split_planes;       /**< use independent file for each Y, U, V plane */
     char path[1024];
-    int updatefirst;
+    int update;
 } VideoMuxData;
 
 static int write_header(AVFormatContext *s)
@@ -75,8 +75,10 @@
     int i;
 
     if (!img->is_pipe) {
-        if (av_get_frame_filename(filename, sizeof(filename),
-                                  img->path, img->img_number) < 0 && img->img_number > 1 && !img->updatefirst) {
+        if (img->update) {
+            av_strlcpy(filename, img->path, sizeof(filename));
+        } else if (av_get_frame_filename(filename, sizeof(filename), img->path, img->img_number) < 0 &&
+                   img->img_number > 1) {
             av_log(s, AV_LOG_ERROR,
                    "Could not get frame filename number %d from pattern '%s' (either set updatefirst or use a pattern like %%03d within the filename pattern)\n",
                    img->img_number, img->path);
@@ -128,7 +130,8 @@
 #define OFFSET(x) offsetof(VideoMuxData, x)
 #define ENC AV_OPT_FLAG_ENCODING_PARAM
 static const AVOption muxoptions[] = {
-    { "updatefirst",  "update the first image file",      OFFSET(updatefirst), AV_OPT_TYPE_INT, { .i64 = 0 }, 0,       1, ENC },
+    { "updatefirst",  "continuously overwrite one file", OFFSET(update),  AV_OPT_TYPE_INT, { .i64 = 0 }, 0,       1, ENC },
+    { "update",       "continuously overwrite one file", OFFSET(update),  AV_OPT_TYPE_INT, { .i64 = 0 }, 0,       1, ENC },
     { "start_number", "set first number in the sequence", OFFSET(img_number), AV_OPT_TYPE_INT,  { .i64 = 1 }, 1, INT_MAX, ENC },
     { NULL },
 };
diff --git a/libavformat/isom.c b/libavformat/isom.c
index 3f419a8..4078c0d 100644
--- a/libavformat/isom.c
+++ b/libavformat/isom.c
@@ -293,6 +293,7 @@
     { AV_CODEC_ID_QDM2,            MKTAG('Q', 'D', 'M', '2') },
     { AV_CODEC_ID_QDMC,            MKTAG('Q', 'D', 'M', 'C') },
     { AV_CODEC_ID_SPEEX,           MKTAG('s', 'p', 'e', 'x') }, /* Flash Media Server */
+    { AV_CODEC_ID_SPEEX,           MKTAG('S', 'P', 'X', 'N') },
     { AV_CODEC_ID_WMAV2,           MKTAG('W', 'M', 'A', '2') },
     { AV_CODEC_ID_EVRC,            MKTAG('s', 'e', 'v', 'c') }, /* 3GPP2 */
     { AV_CODEC_ID_SMV,             MKTAG('s', 's', 'm', 'v') }, /* 3GPP2 */
diff --git a/libavformat/ivfenc.c b/libavformat/ivfenc.c
index ddb205b..45bae22 100644
--- a/libavformat/ivfenc.c
+++ b/libavformat/ivfenc.c
@@ -53,7 +53,6 @@
     avio_wl32(pb, pkt->size);
     avio_wl64(pb, pkt->pts);
     avio_write(pb, pkt->data, pkt->size);
-    avio_flush(pb);
 
     return 0;
 }
diff --git a/libavformat/libquvi.c b/libavformat/libquvi.c
new file mode 100644
index 0000000..fbae74c
--- /dev/null
+++ b/libavformat/libquvi.c
@@ -0,0 +1,146 @@
+/*
+ * Copyright (c) 2013 Clément Bœsch
+ *
+ * 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 <quvi/quvi.h>
+
+#include "libavformat/avformat.h"
+#include "libavformat/internal.h"
+#include "libavutil/opt.h"
+
+typedef struct {
+    const AVClass *class;
+    char *format;
+    AVFormatContext *fmtctx;
+} LibQuviContext;
+
+#define OFFSET(x) offsetof(LibQuviContext, x)
+#define FLAGS AV_OPT_FLAG_DECODING_PARAM
+static const AVOption libquvi_options[] = {
+    { "format", "request specific format", OFFSET(format), AV_OPT_TYPE_STRING, {.str="best"}, .flags = FLAGS },
+    { NULL }
+};
+
+static const AVClass libquvi_context_class = {
+    .class_name     = "libquvi",
+    .item_name      = av_default_item_name,
+    .option         = libquvi_options,
+    .version        = LIBAVUTIL_VERSION_INT,
+};
+
+static int libquvi_close(AVFormatContext *s)
+{
+    LibQuviContext *qc = s->priv_data;
+    if (qc->fmtctx)
+        avformat_close_input(&qc->fmtctx);
+    return 0;
+}
+
+static int libquvi_read_header(AVFormatContext *s)
+{
+    int i, ret;
+    quvi_t q;
+    quvi_media_t m;
+    QUVIcode rc;
+    LibQuviContext *qc = s->priv_data;
+    char *media_url, *pagetitle;
+
+    rc = quvi_init(&q);
+    if (rc != QUVI_OK)
+        goto quvi_fail;
+
+    quvi_setopt(q, QUVIOPT_FORMAT, qc->format);
+
+    rc = quvi_parse(q, s->filename, &m);
+    if (rc != QUVI_OK)
+        goto quvi_fail;
+
+    rc = quvi_getprop(m, QUVIPROP_MEDIAURL, &media_url);
+    if (rc != QUVI_OK)
+        goto quvi_fail;
+
+    ret = avformat_open_input(&qc->fmtctx, media_url, NULL, NULL);
+    if (ret < 0)
+        goto end;
+
+    rc = quvi_getprop(m, QUVIPROP_PAGETITLE, &pagetitle);
+    if (rc == QUVI_OK)
+        av_dict_set(&s->metadata, "title", pagetitle, 0);
+
+    for (i = 0; i < qc->fmtctx->nb_streams; i++) {
+        AVStream *st = avformat_new_stream(s, NULL);
+        AVStream *ist = qc->fmtctx->streams[i];
+        if (!st) {
+            ret = AVERROR(ENOMEM);
+            goto end;
+        }
+        avpriv_set_pts_info(st, ist->pts_wrap_bits, ist->time_base.num, ist->time_base.den);
+        avcodec_copy_context(st->codec, qc->fmtctx->streams[i]->codec);
+    }
+
+    return 0;
+
+quvi_fail:
+    av_log(s, AV_LOG_ERROR, "%s\n", quvi_strerror(q, rc));
+    ret = AVERROR_EXTERNAL;
+
+end:
+    quvi_parse_close(&m);
+    quvi_close(&q);
+    return ret;
+}
+
+static int libquvi_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    LibQuviContext *qc = s->priv_data;
+    return av_read_frame(qc->fmtctx, pkt);
+}
+
+static int libquvi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
+{
+    LibQuviContext *qc = s->priv_data;
+    return av_seek_frame(qc->fmtctx, stream_index, timestamp, flags);
+}
+
+static int libquvi_probe(AVProbeData *p)
+{
+    int score;
+    quvi_t q;
+    QUVIcode rc;
+
+    rc = quvi_init(&q);
+    if (rc != QUVI_OK)
+        return AVERROR(ENOMEM);
+    score = quvi_supported(q, (char *)p->filename) == QUVI_OK ? AVPROBE_SCORE_MAX/2 : 0;
+    quvi_close(&q);
+    return score;
+}
+
+AVInputFormat ff_libquvi_demuxer = {
+    .name           = "libquvi",
+    .long_name      = NULL_IF_CONFIG_SMALL("libquvi demuxer"),
+    .priv_data_size = sizeof(LibQuviContext),
+    .read_probe     = libquvi_probe,
+    .read_header    = libquvi_read_header,
+    .read_packet    = libquvi_read_packet,
+    .read_close     = libquvi_close,
+    .read_seek      = libquvi_read_seek,
+    .priv_class     = &libquvi_context_class,
+    .flags          = AVFMT_NOFILE,
+};
diff --git a/libavformat/lmlm4.c b/libavformat/lmlm4.c
index 29ee2a3..c8ea421 100644
--- a/libavformat/lmlm4.c
+++ b/libavformat/lmlm4.c
@@ -93,8 +93,8 @@
         av_log(s, AV_LOG_ERROR, "invalid or unsupported frame_type\n");
         return AVERROR(EIO);
     }
-    if (packet_size > LMLM4_MAX_PACKET_SIZE) {
-        av_log(s, AV_LOG_ERROR, "packet size exceeds maximum\n");
+    if (packet_size > LMLM4_MAX_PACKET_SIZE || packet_size<=8) {
+        av_log(s, AV_LOG_ERROR, "packet size %d is invalid\n", packet_size);
         return AVERROR(EIO);
     }
 
diff --git a/libavformat/lvfdec.c b/libavformat/lvfdec.c
index 08a0891..f8dda58 100644
--- a/libavformat/lvfdec.c
+++ b/libavformat/lvfdec.c
@@ -41,7 +41,7 @@
     if (!nb_streams)
         return AVERROR_INVALIDDATA;
     if (nb_streams > 2) {
-        av_log_ask_for_sample(s, "too many streams\n");
+        avpriv_request_sample(s, "%d streams", nb_streams);
         return AVERROR_PATCHWELCOME;
     }
 
@@ -87,7 +87,7 @@
             avio_seek(s->pb, 2048 + 8, SEEK_SET);
             return 0;
         default:
-            av_log_ask_for_sample(s, "unknown id\n");
+            avpriv_request_sample(s, "id %d", id);
             return AVERROR_PATCHWELCOME;
         }
 
diff --git a/libavformat/lxfdec.c b/libavformat/lxfdec.c
index 90c4974..876f988 100644
--- a/libavformat/lxfdec.c
+++ b/libavformat/lxfdec.c
@@ -130,7 +130,7 @@
     version     = bytestream_get_le32(&p);
     header_size = bytestream_get_le32(&p);
     if (version > 1)
-        av_log_ask_for_sample(s, "Unknown format version %i\n", version);
+        avpriv_request_sample(s, "format version %i", version);
     if (header_size < (version ? 72 : 60) ||
         header_size > LXF_MAX_PACKET_HEADER_SIZE ||
         (header_size & 3)) {
diff --git a/libavformat/matroska.c b/libavformat/matroska.c
index 09eecf2..ee57c18 100644
--- a/libavformat/matroska.c
+++ b/libavformat/matroska.c
@@ -61,10 +61,16 @@
     {"S_TEXT/UTF8"      , AV_CODEC_ID_TEXT},
     {"S_TEXT/UTF8"      , AV_CODEC_ID_SRT},
     {"S_TEXT/ASCII"     , AV_CODEC_ID_TEXT},
+#if FF_API_ASS_SSA
     {"S_TEXT/ASS"       , AV_CODEC_ID_SSA},
     {"S_TEXT/SSA"       , AV_CODEC_ID_SSA},
     {"S_ASS"            , AV_CODEC_ID_SSA},
     {"S_SSA"            , AV_CODEC_ID_SSA},
+#endif
+    {"S_TEXT/ASS"       , AV_CODEC_ID_ASS},
+    {"S_TEXT/SSA"       , AV_CODEC_ID_ASS},
+    {"S_ASS"            , AV_CODEC_ID_ASS},
+    {"S_SSA"            , AV_CODEC_ID_ASS},
     {"S_VOBSUB"         , AV_CODEC_ID_DVD_SUBTITLE},
     {"S_DVBSUB"         , AV_CODEC_ID_DVB_SUBTITLE},
     {"S_HDMV/PGS"       , AV_CODEC_ID_HDMV_PGS_SUBTITLE},
diff --git a/libavformat/matroska.h b/libavformat/matroska.h
index 8e6f8cc..8a7e10b 100644
--- a/libavformat/matroska.h
+++ b/libavformat/matroska.h
@@ -137,6 +137,15 @@
 #define MATROSKA_ID_ENCODINGCOMPALGO 0x4254
 #define MATROSKA_ID_ENCODINGCOMPSETTINGS 0x4255
 
+#define MATROSKA_ID_ENCODINGENCRYPTION 0x5035
+#define MATROSKA_ID_ENCODINGENCAESSETTINGS 0x47E7
+#define MATROSKA_ID_ENCODINGENCALGO 0x47E1
+#define MATROSKA_ID_ENCODINGENCKEYID 0x47E2
+#define MATROSKA_ID_ENCODINGSIGALGO 0x47E5
+#define MATROSKA_ID_ENCODINGSIGHASHALGO 0x47E6
+#define MATROSKA_ID_ENCODINGSIGKEYID 0x47E4
+#define MATROSKA_ID_ENCODINGSIGNATURE 0x47E3
+
 /* ID in the cues master */
 #define MATROSKA_ID_POINTENTRY 0xBB
 
diff --git a/libavformat/matroskadec.c b/libavformat/matroskadec.c
index ab675ae..d17bf61 100644
--- a/libavformat/matroskadec.c
+++ b/libavformat/matroskadec.c
@@ -41,6 +41,7 @@
 #include "matroska.h"
 #include "libavcodec/bytestream.h"
 #include "libavcodec/mpeg4audio.h"
+#include "libavutil/base64.h"
 #include "libavutil/intfloat.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/avstring.h"
@@ -108,9 +109,15 @@
 } MatroskaTrackCompression;
 
 typedef struct {
+    uint64_t algo;
+    EbmlBin  key_id;
+} MatroskaTrackEncryption;
+
+typedef struct {
     uint64_t scope;
     uint64_t type;
     MatroskaTrackCompression compression;
+    MatroskaTrackEncryption encryption;
 } MatroskaTrackEncoding;
 
 typedef struct {
@@ -320,8 +327,8 @@
 
 static EbmlSyntax matroska_track_video[] = {
     { MATROSKA_ID_VIDEOFRAMERATE,     EBML_FLOAT,0, offsetof(MatroskaTrackVideo,frame_rate) },
-    { MATROSKA_ID_VIDEODISPLAYWIDTH,  EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_width) },
-    { MATROSKA_ID_VIDEODISPLAYHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_height) },
+    { MATROSKA_ID_VIDEODISPLAYWIDTH,  EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_width), {.u=-1} },
+    { MATROSKA_ID_VIDEODISPLAYHEIGHT, EBML_UINT, 0, offsetof(MatroskaTrackVideo,display_height), {.u=-1} },
     { MATROSKA_ID_VIDEOPIXELWIDTH,    EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_width) },
     { MATROSKA_ID_VIDEOPIXELHEIGHT,   EBML_UINT, 0, offsetof(MatroskaTrackVideo,pixel_height) },
     { MATROSKA_ID_VIDEOCOLORSPACE,    EBML_BIN,  0, offsetof(MatroskaTrackVideo,color_space) },
@@ -351,10 +358,21 @@
     { 0 }
 };
 
+static EbmlSyntax matroska_track_encoding_encryption[] = {
+    { MATROSKA_ID_ENCODINGENCALGO,        EBML_UINT, 0, offsetof(MatroskaTrackEncryption,algo), {.u=0} },
+    { MATROSKA_ID_ENCODINGENCKEYID,       EBML_BIN, 0, offsetof(MatroskaTrackEncryption,key_id) },
+    { MATROSKA_ID_ENCODINGENCAESSETTINGS, EBML_NONE },
+    { MATROSKA_ID_ENCODINGSIGALGO,        EBML_NONE },
+    { MATROSKA_ID_ENCODINGSIGHASHALGO,    EBML_NONE },
+    { MATROSKA_ID_ENCODINGSIGKEYID,       EBML_NONE },
+    { MATROSKA_ID_ENCODINGSIGNATURE,      EBML_NONE },
+    { 0 }
+};
 static EbmlSyntax matroska_track_encoding[] = {
     { MATROSKA_ID_ENCODINGSCOPE,      EBML_UINT, 0, offsetof(MatroskaTrackEncoding,scope), {.u=1} },
     { MATROSKA_ID_ENCODINGTYPE,       EBML_UINT, 0, offsetof(MatroskaTrackEncoding,type), {.u=0} },
     { MATROSKA_ID_ENCODINGCOMPRESSION,EBML_NEST, 0, offsetof(MatroskaTrackEncoding,compression), {.n=matroska_track_encoding_compression} },
+    { MATROSKA_ID_ENCODINGENCRYPTION, EBML_NEST, 0, offsetof(MatroskaTrackEncoding,encryption), {.n=matroska_track_encoding_encryption} },
     { MATROSKA_ID_ENCODINGORDER,      EBML_NONE },
     { 0 }
 };
@@ -1201,10 +1219,12 @@
     return result;
 }
 
+#if FF_API_ASS_SSA
 static void matroska_fix_ass_packet(MatroskaDemuxContext *matroska,
                                     AVPacket *pkt, uint64_t display_duration)
 {
-    char *line, *layer, *ptr = pkt->data, *end = ptr+pkt->size;
+    AVBufferRef *line;
+    char *layer, *ptr = pkt->data, *end = ptr+pkt->size;
     for (; *ptr!=',' && ptr<end-1; ptr++);
     if (*ptr == ',')
         ptr++;
@@ -1223,13 +1243,14 @@
         es = ec/   100;  ec -=    100*es;
         *ptr++ = '\0';
         len = 50 + end-ptr + FF_INPUT_BUFFER_PADDING_SIZE;
-        if (!(line = av_malloc(len)))
+        if (!(line = av_buffer_alloc(len)))
             return;
-        snprintf(line,len,"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
+        snprintf(line->data, len,"Dialogue: %s,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s\r\n",
                  layer, sh, sm, ss, sc, eh, em, es, ec, ptr);
-        av_free(pkt->data);
-        pkt->data = line;
-        pkt->size = strlen(line);
+        av_buffer_unref(&pkt->buf);
+        pkt->buf  = line;
+        pkt->data = line->data;
+        pkt->size = strlen(line->data);
     }
 }
 
@@ -1245,6 +1266,7 @@
     av_free(in);
     return 0;
 }
+#endif
 
 static void matroska_convert_tag(AVFormatContext *s, EbmlList *list,
                                  AVDictionary **metadata, char *prefix)
@@ -1428,7 +1450,7 @@
     for (i = 0; i < seekhead_list->nb_elem; i++)
         if (seekhead[i].id == MATROSKA_ID_CUES)
             break;
-    assert(i <= seekhead_list->nb_elem);
+    av_assert1(i <= seekhead_list->nb_elem);
 
     if (matroska_parse_seekhead_entry(matroska, i) < 0)
        matroska->cues_parsing_deferred = -1;
@@ -1545,6 +1567,7 @@
         int extradata_offset = 0;
         uint32_t fourcc = 0;
         AVIOContext b;
+        char* key_id_base64 = NULL;
 
         /* Apply some sanity checks. */
         if (track->type != MATROSKA_TRACK_TYPE_VIDEO &&
@@ -1561,9 +1584,9 @@
         if (track->type == MATROSKA_TRACK_TYPE_VIDEO) {
             if (!track->default_duration && track->video.frame_rate > 0)
                 track->default_duration = 1000000000/track->video.frame_rate;
-            if (!track->video.display_width)
+            if (track->video.display_width == -1)
                 track->video.display_width = track->video.pixel_width;
-            if (!track->video.display_height)
+            if (track->video.display_height == -1)
                 track->video.display_height = track->video.pixel_height;
             if (track->video.color_space.size == 4)
                 fourcc = AV_RL32(track->video.color_space.data);
@@ -1575,8 +1598,24 @@
             av_log(matroska->ctx, AV_LOG_ERROR,
                    "Multiple combined encodings not supported");
         } else if (encodings_list->nb_elem == 1) {
-            if (encodings[0].type ||
-                (
+            if (encodings[0].type) {
+                if (encodings[0].encryption.key_id.size > 0) {
+                    /* Save the encryption key id to be stored later as a
+                       metadata tag. */
+                    const int b64_size = AV_BASE64_SIZE(encodings[0].encryption.key_id.size);
+                    key_id_base64 = av_malloc(b64_size);
+                    if (key_id_base64 == NULL)
+                        return AVERROR(ENOMEM);
+
+                    av_base64_encode(key_id_base64, b64_size,
+                                     encodings[0].encryption.key_id.data,
+                                     encodings[0].encryption.key_id.size);
+                } else {
+                    encodings[0].scope = 0;
+                    av_log(matroska->ctx, AV_LOG_ERROR,
+                           "Unsupported encoding type");
+                }
+            } else if (
 #if CONFIG_ZLIB
                  encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_ZLIB &&
 #endif
@@ -1586,7 +1625,7 @@
 #if CONFIG_LZO
                  encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_LZO &&
 #endif
-                 encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP)) {
+                 encodings[0].compression.algo != MATROSKA_TRACK_ENCODING_COMP_HEADERSTRIP) {
                 encodings[0].scope = 0;
                 av_log(matroska->ctx, AV_LOG_ERROR,
                        "Unsupported encoding type");
@@ -1616,8 +1655,16 @@
         }
 
         st = track->stream = avformat_new_stream(s, NULL);
-        if (st == NULL)
+        if (st == NULL) {
+            av_free(key_id_base64);
             return AVERROR(ENOMEM);
+        }
+
+        if (key_id_base64) {
+            /* export encryption key id as base64 metadata tag */
+            av_dict_set(&st->metadata, "enc_key_id", key_id_base64, 0);
+            av_freep(&key_id_base64);
+        }
 
         if (!strcmp(track->codec_id, "V_MS/VFW/FOURCC")
             && track->codec_priv.size >= 40
@@ -1642,7 +1689,7 @@
                    && (track->codec_priv.data != NULL)) {
             fourcc = AV_RL32(track->codec_priv.data);
             codec_id = ff_codec_get_id(ff_codec_movvideo_tags, fourcc);
-        } else if (codec_id == AV_CODEC_ID_ALAC && track->codec_priv.size && track->codec_priv.size < INT_MAX-12) {
+        } else if (codec_id == AV_CODEC_ID_ALAC && track->codec_priv.size && track->codec_priv.size < INT_MAX - 12 - FF_INPUT_BUFFER_PADDING_SIZE) {
             /* Only ALAC's magic cookie is stored in Matroska's track headers.
                Create the "atom size", "tag", and "tag version" fields the
                decoder expects manually. */
@@ -1826,7 +1873,12 @@
             st->need_parsing = AVSTREAM_PARSE_HEADERS;
         } else if (track->type == MATROSKA_TRACK_TYPE_SUBTITLE) {
             st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
-            if (st->codec->codec_id == AV_CODEC_ID_SSA)
+#if FF_API_ASS_SSA
+            if (st->codec->codec_id == AV_CODEC_ID_SSA ||
+                st->codec->codec_id == AV_CODEC_ID_ASS)
+#else
+            if (st->codec->codec_id == AV_CODEC_ID_ASS)
+#endif
                 matroska->contains_ssa = 1;
         }
     }
@@ -1959,18 +2011,18 @@
         uint32_t total = 0;
         for (n = 0; res == 0 && n < *laces - 1; n++) {
             while (1) {
-                if (size == 0) {
-                    res = AVERROR_EOF;
+                if (size <= total) {
+                    res = AVERROR_INVALIDDATA;
                     break;
                 }
                 temp = *data;
+                total += temp;
                 lace_size[n] += temp;
                 data += 1;
                 size -= 1;
                 if (temp != 0xff)
                     break;
             }
-            total += lace_size[n];
         }
         if (size <= total) {
             res = AVERROR_INVALIDDATA;
@@ -1992,12 +2044,12 @@
 
     case 0x3: /* EBML lacing */ {
         uint64_t num;
-        uint32_t total;
+        uint64_t total;
         n = matroska_ebmlnum_uint(matroska, data, size, &num);
-        if (n < 0) {
+        if (n < 0 || num > INT_MAX) {
             av_log(matroska->ctx, AV_LOG_INFO,
                    "EBML block data error\n");
-            res = n;
+            res = n<0 ? n : AVERROR_INVALIDDATA;
             break;
         }
         data += n;
@@ -2007,10 +2059,10 @@
             int64_t snum;
             int r;
             r = matroska_ebmlnum_sint(matroska, data, size, &snum);
-            if (r < 0) {
+            if (r < 0 || lace_size[n - 1] + snum > (uint64_t)INT_MAX) {
                 av_log(matroska->ctx, AV_LOG_INFO,
                        "EBML block data error\n");
-                res = r;
+                res = r<0 ? r : AVERROR_INVALIDDATA;
                 break;
             }
             data += r;
@@ -2121,7 +2173,7 @@
     int offset = 0, res;
     AVPacket *pkt;
 
-    if (encodings && encodings->scope & 1) {
+    if (encodings && !encodings->type && encodings->scope & 1) {
         res = matroska_decode_buffer(&pkt_data, &pkt_size, track);
         if (res < 0)
             return res;
@@ -2156,6 +2208,8 @@
                                                      AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL,
                                                      additional_size + 8);
         if(side_data == NULL) {
+            av_free_packet(pkt);
+            av_free(pkt);
             return AVERROR(ENOMEM);
         }
         AV_WB64(side_data, additional_id);
@@ -2191,6 +2245,7 @@
         pkt->duration = lace_duration;
     }
 
+#if FF_API_ASS_SSA
     if (st->codec->codec_id == AV_CODEC_ID_SSA)
         matroska_fix_ass_packet(matroska, pkt, lace_duration);
 
@@ -2204,6 +2259,10 @@
         dynarray_add(&matroska->packets,&matroska->num_packets,pkt);
         matroska->prev_pkt = pkt;
     }
+#else
+    dynarray_add(&matroska->packets, &matroska->num_packets, pkt);
+    matroska->prev_pkt = pkt;
+#endif
 
     return 0;
 }
@@ -2298,7 +2357,8 @@
              st->codec->block_align && track->audio.sub_packet_size) {
 
 #if CONFIG_RA_288_DECODER || CONFIG_COOK_DECODER || CONFIG_ATRAC3_DECODER || CONFIG_SIPR_DECODER
-            res = matroska_parse_rm_audio(matroska, track, st, data, size,
+            res = matroska_parse_rm_audio(matroska, track, st, data,
+                                          lace_size[n],
                                           timecode, pos);
 #else
             res = AVERROR_INVALIDDATA;
diff --git a/libavformat/matroskaenc.c b/libavformat/matroskaenc.c
index 4544f8e..6726fd9 100644
--- a/libavformat/matroskaenc.c
+++ b/libavformat/matroskaenc.c
@@ -92,7 +92,6 @@
     mkv_cues        *cues;
     mkv_track       *tracks;
 
-    unsigned int    audio_buffer_size;
     AVPacket        cur_audio_pkt;
 
     int have_attachments;
@@ -1043,7 +1042,6 @@
 
     av_init_packet(&mkv->cur_audio_pkt);
     mkv->cur_audio_pkt.size = 0;
-    mkv->audio_buffer_size  = 0;
     mkv->cluster_pos = -1;
 
     avio_flush(pb);
@@ -1074,6 +1072,7 @@
     return end - start;
 }
 
+#if FF_API_ASS_SSA
 static int mkv_write_ass_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
 {
     MatroskaMuxContext *mkv = s->priv_data;
@@ -1118,6 +1117,7 @@
 
     return max_duration;
 }
+#endif
 
 static void mkv_write_block(AVFormatContext *s, AVIOContext *pb,
                             unsigned int blockid, AVPacket *pkt, int flags)
@@ -1238,8 +1238,10 @@
 
     if (codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
         mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe << 7);
+#if FF_API_ASS_SSA
     } else if (codec->codec_id == AV_CODEC_ID_SSA) {
         duration = mkv_write_ass_blocks(s, pb, pkt);
+#endif
     } else if (codec->codec_id == AV_CODEC_ID_SRT) {
         duration = mkv_write_srt_blocks(s, pb, pkt);
     } else {
@@ -1262,19 +1264,6 @@
     return 0;
 }
 
-static int mkv_copy_packet(MatroskaMuxContext *mkv, const AVPacket *pkt)
-{
-    uint8_t *data           = mkv->cur_audio_pkt.data;
-    mkv->cur_audio_pkt      = *pkt;
-    mkv->cur_audio_pkt.data = av_fast_realloc(data, &mkv->audio_buffer_size, pkt->size);
-    if (!mkv->cur_audio_pkt.data)
-        return AVERROR(ENOMEM);
-
-    memcpy(mkv->cur_audio_pkt.data, pkt->data, pkt->size);
-    mkv->cur_audio_pkt.size = pkt->size;
-    return 0;
-}
-
 static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
 {
     MatroskaMuxContext *mkv = s->priv_data;
@@ -1301,7 +1290,7 @@
     // check if we have an audio packet cached
     if (mkv->cur_audio_pkt.size > 0) {
         ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
-        mkv->cur_audio_pkt.size = 0;
+        av_free_packet(&mkv->cur_audio_pkt);
         if (ret < 0) {
             av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret);
             return ret;
@@ -1310,9 +1299,11 @@
 
     // buffer an audio packet to ensure the packet containing the video
     // keyframe's timecode is contained in the same cluster for WebM
-    if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
-        ret = mkv_copy_packet(mkv, pkt);
-    else
+    if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
+        mkv->cur_audio_pkt = *pkt;
+        mkv->cur_audio_pkt.buf = av_buffer_ref(pkt->buf);
+        ret = mkv->cur_audio_pkt.buf ? 0 : AVERROR(ENOMEM);
+    } else
         ret = mkv_write_packet_internal(s, pkt);
     return ret;
 }
@@ -1327,7 +1318,7 @@
     // check if we have an audio packet cached
     if (mkv->cur_audio_pkt.size > 0) {
         ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
-        mkv->cur_audio_pkt.size = 0;
+        av_free_packet(&mkv->cur_audio_pkt);
         if (ret < 0) {
             av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret);
             return ret;
@@ -1364,7 +1355,6 @@
     av_free(mkv->tracks);
     av_freep(&mkv->cues->entries);
     av_freep(&mkv->cues);
-    av_destruct_packet(&mkv->cur_audio_pkt);
 
     return 0;
 }
@@ -1432,7 +1422,11 @@
          ff_codec_bmp_tags, ff_codec_wav_tags,
          additional_audio_tags, additional_video_tags, 0
     },
+#if FF_API_ASS_SSA
     .subtitle_codec    = AV_CODEC_ID_SSA,
+#else
+    .subtitle_codec    = AV_CODEC_ID_ASS,
+#endif
     .query_codec       = mkv_query_codec,
 };
 #endif
diff --git a/libavformat/microdvdenc.c b/libavformat/microdvdenc.c
index 30fd0ea..4d84384 100644
--- a/libavformat/microdvdenc.c
+++ b/libavformat/microdvdenc.c
@@ -52,7 +52,6 @@
         avio_printf(avf->pb, "{%"PRId64"}", pkt->pts + pkt->duration);
     avio_write(avf->pb, pkt->data, pkt->size);
     avio_write(avf->pb, "\n", 1);
-    avio_flush(avf->pb);
     return 0;
 }
 
diff --git a/libavformat/mkvtimestamp_v2.c b/libavformat/mkvtimestamp_v2.c
index 3ed195a..7ba6691 100644
--- a/libavformat/mkvtimestamp_v2.c
+++ b/libavformat/mkvtimestamp_v2.c
@@ -37,7 +37,6 @@
         av_log(s, AV_LOG_WARNING, "More than one stream unsupported\n");
     snprintf(buf, sizeof(buf), "%" PRId64 "\n", pkt->dts);
     avio_write(s->pb, buf, strlen(buf));
-    avio_flush(s->pb);
     return 0;
 }
 
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 137f79b..3e48f15 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -3,6 +3,9 @@
  * Copyright (c) 2001 Fabrice Bellard
  * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
  *
+ * first version by Francois Revol <revol@free.fr>
+ * seek function by Gael Chardon <gael.dev@4now.net>
+ *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
@@ -48,11 +51,6 @@
 #include <zlib.h>
 #endif
 
-/*
- * First version by Francois Revol revol@free.fr
- * Seek function by Gael Chardon gael.dev@4now.net
- */
-
 #include "qtpalette.h"
 
 
@@ -835,7 +833,7 @@
 
     version = avio_r8(pb);
     if (version > 1) {
-        av_log_ask_for_sample(c->fc, "unsupported version %d\n", version);
+        avpriv_request_sample(c->fc, "Version %d", version);
         return AVERROR_PATCHWELCOME;
     }
     avio_rb24(pb); /* flags */
@@ -1033,7 +1031,9 @@
     if ((uint64_t)atom.size > (1<<30))
         return AVERROR_INVALIDDATA;
 
-    if (st->codec->codec_id == AV_CODEC_ID_QDM2 || st->codec->codec_id == AV_CODEC_ID_QDMC) {
+    if (st->codec->codec_id == AV_CODEC_ID_QDM2 ||
+        st->codec->codec_id == AV_CODEC_ID_QDMC ||
+        st->codec->codec_id == AV_CODEC_ID_SPEEX) {
         // pass all frma atom to codec, needed at least for QDMC and QDM2
         av_free(st->codec->extradata);
         st->codec->extradata_size = 0;
diff --git a/libavformat/mov_chan.c b/libavformat/mov_chan.c
index cf1f839..7078b4a 100644
--- a/libavformat/mov_chan.c
+++ b/libavformat/mov_chan.c
@@ -426,6 +426,7 @@
     MOV_CH_LAYOUT_MPEG_7_1_A,
     MOV_CH_LAYOUT_MPEG_7_1_C,
     MOV_CH_LAYOUT_SMPTE_DTV,
+    0,
 };
 
 static const struct {
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 510cef5..8f3b1bc 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -1602,7 +1602,7 @@
     (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
     ffio_wfourcc(pb, "tkhd");
     avio_w8(pb, version);
-    avio_wb24(pb, 0xf); /* flags (track enabled) */
+    avio_wb24(pb, track->secondary ? 0x2 : 0xf); /* flags (first track enabled) */
     if (version == 1) {
         avio_wb64(pb, track->time);
         avio_wb64(pb, track->time);
@@ -2315,6 +2315,7 @@
 {
     int i;
     int64_t pos = avio_tell(pb);
+    int not_first[AVMEDIA_TYPE_NB]={0};
     avio_wb32(pb, 0); /* size placeholder*/
     ffio_wfourcc(pb, "moov");
 
@@ -2355,6 +2356,13 @@
         mov_write_iods_tag(pb, mov);
     for (i=0; i<mov->nb_streams; i++) {
         if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
+            if(i < s->nb_streams){
+                int codec_type= s->streams[i]->codec->codec_type;
+                if(codec_type==AVMEDIA_TYPE_AUDIO || codec_type==AVMEDIA_TYPE_SUBTITLE){
+                    mov->tracks[i].secondary= not_first[codec_type];
+                    not_first[codec_type]= 1;
+                }
+            }
             mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
         }
     }
@@ -3165,6 +3173,14 @@
         memcpy(trk->vos_data, enc->extradata, trk->vos_len);
     }
 
+    if (enc->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
+        (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
+        if (!s->streams[pkt->stream_index]->nb_frames) {
+            av_log(s, AV_LOG_ERROR, "malformated aac bitstream, use -absf aac_adtstoasc\n");
+            return -1;
+        }
+        av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
+    }
     if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
         /* from x264 or from bytestream h264 */
         /* nal reformating needed */
@@ -3175,10 +3191,6 @@
         } else {
             size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
         }
-    } else if (enc->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
-               (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
-        av_log(s, AV_LOG_ERROR, "malformated aac bitstream, use -absf aac_adtstoasc\n");
-        return -1;
     } else {
         avio_write(pb, pkt->data, size);
     }
diff --git a/libavformat/movenc.h b/libavformat/movenc.h
index b5d066f..a5db895 100644
--- a/libavformat/movenc.h
+++ b/libavformat/movenc.h
@@ -94,6 +94,7 @@
 #define MOV_TIMECODE_FLAG_ALLOWNEGATIVE 0x0004
     uint32_t    timecode_flags;
     int         language;
+    int         secondary;
     int         track_id;
     int         tag; ///< stsd fourcc
     AVCodecContext *enc;
diff --git a/libavformat/mp3dec.c b/libavformat/mp3dec.c
index c6d6987..d599c9c 100644
--- a/libavformat/mp3dec.c
+++ b/libavformat/mp3dec.c
@@ -40,7 +40,7 @@
     int xing_toc;
     int start_pad;
     int end_pad;
-} MP3Context;
+} MP3DecContext;
 
 /* mp3 read */
 
@@ -89,7 +89,7 @@
 static void read_xing_toc(AVFormatContext *s, int64_t filesize, int64_t duration)
 {
     int i;
-    MP3Context *mp3 = s->priv_data;
+    MP3DecContext *mp3 = s->priv_data;
 
     if (!filesize &&
         !(filesize = avio_size(s->pb))) {
@@ -113,13 +113,14 @@
  */
 static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
 {
-    MP3Context *mp3 = s->priv_data;
+    MP3DecContext *mp3 = s->priv_data;
     uint32_t v, spf;
     unsigned frames = 0; /* Total number of frames in file */
     unsigned size = 0; /* Total number of bytes in the stream */
     const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
     MPADecodeHeader c;
     int vbrtag_size = 0;
+    int is_cbr;
 
     v = avio_rb32(s->pb);
     if(ff_mpa_check_header(v) < 0)
@@ -135,7 +136,8 @@
     /* Check for Xing / Info tag */
     avio_skip(s->pb, xing_offtbl[c.lsf == 1][c.nb_channels == 1]);
     v = avio_rb32(s->pb);
-    if(v == MKBETAG('X', 'i', 'n', 'g') || v == MKBETAG('I', 'n', 'f', 'o')) {
+    is_cbr = v == MKBETAG('I', 'n', 'f', 'o');
+    if (v == MKBETAG('X', 'i', 'n', 'g') || is_cbr) {
         v = avio_rb32(s->pb);
         if(v & XING_FLAG_FRAMES)
             frames = avio_rb32(s->pb);
@@ -180,7 +182,7 @@
     if(frames)
         st->duration = av_rescale_q(frames, (AVRational){spf, c.sample_rate},
                                     st->time_base);
-    if(size && frames)
+    if (size && frames && !is_cbr)
         st->codec->bit_rate = av_rescale(size, 8 * c.sample_rate, frames * (int64_t)spf);
 
     return 0;
@@ -188,7 +190,7 @@
 
 static int mp3_read_header(AVFormatContext *s)
 {
-    MP3Context *mp3 = s->priv_data;
+    MP3DecContext *mp3 = s->priv_data;
     AVStream *st;
     int64_t off;
 
@@ -224,7 +226,7 @@
 
 static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
-    MP3Context *mp3 = s->priv_data;
+    MP3DecContext *mp3 = s->priv_data;
     int ret, size;
     int64_t pos;
 
@@ -271,7 +273,7 @@
 static int mp3_seek(AVFormatContext *s, int stream_index, int64_t timestamp,
                     int flags)
 {
-    MP3Context *mp3 = s->priv_data;
+    MP3DecContext *mp3 = s->priv_data;
     AVIndexEntry *ie;
     AVStream *st = s->streams[0];
     int64_t ret  = av_index_search_timestamp(st, timestamp, flags);
@@ -317,11 +319,11 @@
 AVInputFormat ff_mp3_demuxer = {
     .name           = "mp3",
     .long_name      = NULL_IF_CONFIG_SMALL("MP2/3 (MPEG audio layer 2/3)"),
-    .priv_data_size = sizeof(MP3Context),
     .read_probe     = mp3_read_probe,
     .read_header    = mp3_read_header,
     .read_packet    = mp3_read_packet,
     .read_seek      = mp3_seek,
+    .priv_data_size = sizeof(MP3DecContext),
     .flags          = AVFMT_GENERIC_INDEX,
     .extensions     = "mp2,mp3,m2a", /* XXX: use probe */
 };
diff --git a/libavformat/mp3enc.c b/libavformat/mp3enc.c
index cc9f0d0..ee0956e 100644
--- a/libavformat/mp3enc.c
+++ b/libavformat/mp3enc.c
@@ -422,7 +422,11 @@
                 return AVERROR(ENOMEM);
 
             pktl->pkt     = *pkt;
-            pkt->destruct = NULL;
+            pktl->pkt.buf = av_buffer_ref(pkt->buf);
+            if (!pktl->pkt.buf) {
+                av_freep(&pktl);
+                return AVERROR(ENOMEM);
+            }
 
             if (mp3->queue_end)
                 mp3->queue_end->next = pktl;
diff --git a/libavformat/mpeg.c b/libavformat/mpeg.c
index 4eaffd8..8f835ad 100644
--- a/libavformat/mpeg.c
+++ b/libavformat/mpeg.c
@@ -30,6 +30,7 @@
 
 #undef NDEBUG
 #include <assert.h>
+#include "libavutil/avassert.h"
 
 /*********************************************/
 /* demux code */
@@ -108,6 +109,8 @@
     int32_t header_state;
     unsigned char psm_es_type[256];
     int sofdec;
+    int dvd;
+    int imkh_cctv;
 #if CONFIG_VOBSUB_DEMUXER
     AVFormatContext *sub_ctx;
     FFDemuxSubtitlesQueue q;
@@ -117,22 +120,18 @@
 static int mpegps_read_header(AVFormatContext *s)
 {
     MpegDemuxContext *m = s->priv_data;
-    const char *sofdec = "Sofdec";
-    int v, i = 0;
+    char buffer[7];
     int64_t last_pos = avio_tell(s->pb);
 
     m->header_state = 0xff;
     s->ctx_flags |= AVFMTCTX_NOHEADER;
 
-    m->sofdec = -1;
-    do {
-        v = avio_r8(s->pb);
-        m->sofdec++;
-    } while (v == sofdec[i] && i++ < 6);
-
-    m->sofdec = (m->sofdec == 6) ? 1 : 0;
-
-    if (!m->sofdec)
+    avio_get_str(s->pb, 6, buffer, sizeof(buffer));
+    if (!memcmp("IMKH", buffer, 4)) {
+        m->imkh_cctv = 1;
+    } else if (!memcmp("Sofdec", buffer, 6)) {
+        m->sofdec = 1;
+    } else
        avio_seek(s->pb, last_pos, SEEK_SET);
 
     /* no need to do more */
@@ -247,21 +246,82 @@
         goto redo;
     }
     if (startcode == PRIVATE_STREAM_2) {
-        len = avio_rb16(s->pb);
         if (!m->sofdec) {
-            while (len-- >= 6) {
-                if (avio_r8(s->pb) == 'S') {
-                    uint8_t buf[5];
-                    avio_read(s->pb, buf, sizeof(buf));
-                    m->sofdec = !memcmp(buf, "ofdec", 5);
-                    len -= sizeof(buf);
-                    break;
+            /* Need to detect whether this from a DVD or a 'Sofdec' stream */
+            int len = avio_rb16(s->pb);
+            int bytesread = 0;
+            uint8_t *ps2buf = av_malloc(len);
+
+            if (ps2buf) {
+                bytesread = avio_read(s->pb, ps2buf, len);
+
+                if (bytesread != len) {
+                    avio_skip(s->pb, len - bytesread);
+                } else {
+                    uint8_t *p = 0;
+                    if (len >= 6)
+                        p = memchr(ps2buf, 'S', len - 5);
+
+                    if (p)
+                        m->sofdec = !memcmp(p+1, "ofdec", 5);
+
+                    m->sofdec -= !m->sofdec;
+
+                    if (m->sofdec < 0) {
+                        if (len == 980  && ps2buf[0] == 0) {
+                            /* PCI structure? */
+                            uint32_t startpts = AV_RB32(ps2buf + 0x0d);
+                            uint32_t endpts = AV_RB32(ps2buf + 0x11);
+                            uint8_t hours = ((ps2buf[0x19] >> 4) * 10) + (ps2buf[0x19] & 0x0f);
+                            uint8_t mins  = ((ps2buf[0x1a] >> 4) * 10) + (ps2buf[0x1a] & 0x0f);
+                            uint8_t secs  = ((ps2buf[0x1b] >> 4) * 10) + (ps2buf[0x1b] & 0x0f);
+
+                            m->dvd = (hours <= 23 &&
+                                      mins  <= 59 &&
+                                      secs  <= 59 &&
+                                      (ps2buf[0x19] & 0x0f) < 10 &&
+                                      (ps2buf[0x1a] & 0x0f) < 10 &&
+                                      (ps2buf[0x1b] & 0x0f) < 10 &&
+                                      endpts >= startpts);
+                        } else if (len == 1018 && ps2buf[0] == 1) {
+                            /* DSI structure? */
+                            uint8_t hours = ((ps2buf[0x1d] >> 4) * 10) + (ps2buf[0x1d] & 0x0f);
+                            uint8_t mins  = ((ps2buf[0x1e] >> 4) * 10) + (ps2buf[0x1e] & 0x0f);
+                            uint8_t secs  = ((ps2buf[0x1f] >> 4) * 10) + (ps2buf[0x1f] & 0x0f);
+
+                            m->dvd = (hours <= 23 &&
+                                      mins  <= 59 &&
+                                      secs  <= 59 &&
+                                      (ps2buf[0x1d] & 0x0f) < 10 &&
+                                      (ps2buf[0x1e] & 0x0f) < 10 &&
+                                      (ps2buf[0x1f] & 0x0f) < 10);
+                        }
+                    }
                 }
+
+                av_free(ps2buf);
+
+                /* If this isn't a DVD packet or no memory
+                 * could be allocated, just ignore it.
+                 * If we did, move back to the start of the
+                 * packet (plus 'length' field) */
+                if (!m->dvd || avio_skip(s->pb, -(len + 2)) < 0) {
+                    /* Skip back failed.
+                     * This packet will be lost but that can't be helped
+                     * if we can't skip back
+                     */
+                    goto redo;
+                }
+            } else {
+                /* No memory */
+                avio_skip(s->pb, len);
+                goto redo;
             }
-            m->sofdec -= !m->sofdec;
+        } else if (!m->dvd) {
+            int len = avio_rb16(s->pb);
+            avio_skip(s->pb, len);
+            goto redo;
         }
-        avio_skip(s->pb, len);
-        goto redo;
     }
     if (startcode == PROGRAM_STREAM_MAP) {
         mpegps_psm_parse(m, s->pb);
@@ -271,7 +331,9 @@
     /* find matching stream */
     if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
           (startcode >= 0x1e0 && startcode <= 0x1ef) ||
-          (startcode == 0x1bd) || (startcode == 0x1fd)))
+          (startcode == 0x1bd) ||
+          (startcode == PRIVATE_STREAM_2) ||
+          (startcode == 0x1fd)))
         goto redo;
     if (ppos) {
         *ppos = avio_tell(s->pb) - 4;
@@ -279,6 +341,8 @@
     len = avio_rb16(s->pb);
     pts =
     dts = AV_NOPTS_VALUE;
+    if (startcode != PRIVATE_STREAM_2)
+    {
     /* stuffing */
     for(;;) {
         if (len < 1)
@@ -352,6 +416,7 @@
     }
     else if( c!= 0xf )
         goto redo;
+    }
 
     if (startcode == PRIVATE_STREAM_1) {
         startcode = avio_r8(s->pb);
@@ -438,6 +503,9 @@
         } else if(es_type == STREAM_TYPE_AUDIO_AC3){
             codec_id = AV_CODEC_ID_AC3;
             type = AVMEDIA_TYPE_AUDIO;
+        } else if(m->imkh_cctv && es_type == 0x91){
+            codec_id = AV_CODEC_ID_PCM_MULAW;
+            type = AVMEDIA_TYPE_AUDIO;
     } else if (startcode >= 0x1e0 && startcode <= 0x1ef) {
         static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
         unsigned char buf[8];
@@ -448,6 +516,9 @@
         else
             request_probe= 1;
         type = AVMEDIA_TYPE_VIDEO;
+    } else if (startcode == PRIVATE_STREAM_2) {
+        type = AVMEDIA_TYPE_DATA;
+        codec_id = AV_CODEC_ID_DVD_NAV;
     } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
         type = AVMEDIA_TYPE_AUDIO;
         codec_id = m->sofdec > 0 ? AV_CODEC_ID_ADPCM_ADX : AV_CODEC_ID_MP2;
@@ -493,6 +564,11 @@
     st->id = startcode;
     st->codec->codec_type = type;
     st->codec->codec_id = codec_id;
+    if (st->codec->codec_id == AV_CODEC_ID_PCM_MULAW) {
+        st->codec->channels = 1;
+        st->codec->channel_layout = AV_CH_LAYOUT_MONO;
+        st->codec->sample_rate = 8000;
+    }
     st->request_probe     = request_probe;
     if (codec_id != AV_CODEC_ID_PCM_S16BE)
         st->need_parsing = AVSTREAM_PARSE_FULL;
@@ -659,7 +735,7 @@
             int64_t pos, timestamp;
             const char *p = line + 10;
 
-            if (sscanf(p, "%02d:%02d:%02d:%03d, filepos: %"PRIx64,
+            if (sscanf(p, "%02d:%02d:%02d:%03d, filepos: %"SCNx64,
                        &hh, &mm, &ss, &ms, &pos) != 5) {
                 av_log(s, AV_LOG_ERROR, "Unable to parse timestamp line '%s', "
                        "abort parsing\n", line);
@@ -734,6 +810,8 @@
     return ret;
 }
 
+#define FAIL(r) do { ret = r; goto fail; } while (0)
+
 static int vobsub_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
     MpegDemuxContext *vobsub = s->priv_data;
@@ -767,7 +845,7 @@
 
         ret = mpegps_read_pes_header(vobsub->sub_ctx, NULL, &startcode, &pts, &dts);
         if (ret < 0)
-            return ret;
+            FAIL(ret);
         to_read = ret & 0xffff;
 
         /* this prevents reads above the current packet */
@@ -784,7 +862,7 @@
 
         ret = av_grow_packet(pkt, to_read);
         if (ret < 0)
-            return ret;
+            FAIL(ret);
 
         n = avio_read(pb, pkt->data + (pkt->size - to_read), to_read);
         if (n < to_read)
@@ -799,7 +877,12 @@
     pkt->pos = idx_pkt.pos;
     pkt->stream_index = idx_pkt.stream_index;
 
+    av_free_packet(&idx_pkt);
     return 0;
+
+fail:
+    av_free_packet(&idx_pkt);
+    return ret;
 }
 
 static int vobsub_read_seek(AVFormatContext *s, int stream_index,
diff --git a/libavformat/mpeg.h b/libavformat/mpeg.h
index ca019c9..a1e8980 100644
--- a/libavformat/mpeg.h
+++ b/libavformat/mpeg.h
@@ -54,6 +54,7 @@
 #define STREAM_TYPE_AUDIO_AAC       0x0f
 #define STREAM_TYPE_VIDEO_MPEG4     0x10
 #define STREAM_TYPE_VIDEO_H264      0x1b
+#define STREAM_TYPE_VIDEO_CAVS      0x42
 
 #define STREAM_TYPE_AUDIO_AC3       0x81
 #define STREAM_TYPE_AUDIO_DTS       0x8a
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 8b92bc4..85b5146 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/buffer.h"
 #include "libavutil/crc.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
@@ -176,7 +177,7 @@
     int64_t pts, dts;
     int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */
     uint8_t header[MAX_PES_HEADER_SIZE];
-    uint8_t *buffer;
+    AVBufferRef *buffer;
     SLConfigDescr sl;
 } PESContext;
 
@@ -208,9 +209,6 @@
 
 static void clear_programs(MpegTSContext *ts)
 {
-    int i;
-    for(i=0; i<ts->nb_prg; i++)
-        clear_avprogram(ts, ts->prg[i].id);
     av_freep(&ts->prg);
     ts->nb_prg=0;
 }
@@ -406,7 +404,7 @@
         av_freep(&filter->u.section_filter.section_buf);
     else if (filter->type == MPEGTS_PES) {
         PESContext *pes = filter->u.pes_filter.opaque;
-        av_freep(&pes->buffer);
+        av_buffer_unref(&pes->buffer);
         /* referenced private data will be freed later in
          * avformat_close_input */
         if (!((PESContext *)filter->u.pes_filter.opaque)->st) {
@@ -568,6 +566,7 @@
     { 0x11, AVMEDIA_TYPE_AUDIO,   AV_CODEC_ID_AAC_LATM }, /* LATM syntax */
 #endif
     { 0x1b, AVMEDIA_TYPE_VIDEO,       AV_CODEC_ID_H264 },
+    { 0x42, AVMEDIA_TYPE_VIDEO,       AV_CODEC_ID_CAVS },
     { 0xd1, AVMEDIA_TYPE_VIDEO,      AV_CODEC_ID_DIRAC },
     { 0xea, AVMEDIA_TYPE_VIDEO,        AV_CODEC_ID_VC1 },
     { 0 },
@@ -703,8 +702,8 @@
 {
     av_init_packet(pkt);
 
-    pkt->destruct = av_destruct_packet;
-    pkt->data = pes->buffer;
+    pkt->buf  = pes->buffer;
+    pkt->data = pes->buffer->data;
     pkt->size = pes->data_index;
 
     if(pes->total_size != MAX_PES_PAYLOAD &&
@@ -869,7 +868,8 @@
                         pes->total_size = MAX_PES_PAYLOAD;
 
                     /* allocate pes buffer */
-                    pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
+                    pes->buffer = av_buffer_alloc(pes->total_size +
+                                                  FF_INPUT_BUFFER_PADDING_SIZE);
                     if (!pes->buffer)
                         return AVERROR(ENOMEM);
 
@@ -971,7 +971,7 @@
                 if (pes->data_index > 0 && pes->data_index+buf_size > pes->total_size) {
                     new_pes_packet(pes, ts->pkt);
                     pes->total_size = MAX_PES_PAYLOAD;
-                    pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
+                    pes->buffer = av_buffer_alloc(pes->total_size + FF_INPUT_BUFFER_PADDING_SIZE);
                     if (!pes->buffer)
                         return AVERROR(ENOMEM);
                     ts->stop_parse = 1;
@@ -980,7 +980,7 @@
                     // not sure if this is legal in ts but see issue #2392
                     buf_size = pes->total_size;
                 }
-                memcpy(pes->buffer+pes->data_index, p, buf_size);
+                memcpy(pes->buffer->data + pes->data_index, p, buf_size);
                 pes->data_index += buf_size;
             }
             buf_size = 0;
@@ -1165,7 +1165,7 @@
         descr->sl.au_seq_num_len     = (lengths >> 7) & 0x1f;
         descr->sl.packet_seq_num_len = (lengths >> 2) & 0x1f;
     } else {
-        av_log_missing_feature(d->s, "Predefined SLConfigDescriptor", 0);
+        avpriv_report_missing_feature(d->s, "Predefined SLConfigDescriptor");
     }
     return 0;
 }
@@ -1389,7 +1389,7 @@
         }
         if (st->codec->extradata) {
             if (st->codec->extradata_size == 4 && memcmp(st->codec->extradata, *pp, 4))
-                av_log_ask_for_sample(fc, "DVB sub with multiple IDs\n");
+                avpriv_request_sample(fc, "DVB sub with multiple IDs");
         } else {
             st->codec->extradata = av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE);
             if (st->codec->extradata) {
@@ -1636,6 +1636,17 @@
             add_pid_to_pmt(ts, sid, pmt_pid);
         }
     }
+
+    if (sid < 0) {
+        int i,j;
+        for (j=0; j<ts->stream->nb_programs; j++) {
+            for (i=0; i<ts->nb_prg; i++)
+                if (ts->prg[i].id == ts->stream->programs[j]->id)
+                    break;
+            if (i==ts->nb_prg)
+                clear_avprogram(ts, ts->stream->programs[j]->id);
+        }
+    }
 }
 
 static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
@@ -1880,7 +1891,7 @@
             if (ts->pids[i]) {
                 if (ts->pids[i]->type == MPEGTS_PES) {
                    PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
-                   av_freep(&pes->buffer);
+                   av_buffer_unref(&pes->buffer);
                    pes->data_index = 0;
                    pes->state = MPEGTS_SKIP; /* skip until pes header */
                 }
diff --git a/libavformat/mpegts.h b/libavformat/mpegts.h
index 98c4b93..269c23b 100644
--- a/libavformat/mpegts.h
+++ b/libavformat/mpegts.h
@@ -52,6 +52,7 @@
 #define STREAM_TYPE_AUDIO_AAC_LATM  0x11
 #define STREAM_TYPE_VIDEO_MPEG4     0x10
 #define STREAM_TYPE_VIDEO_H264      0x1b
+#define STREAM_TYPE_VIDEO_CAVS      0x42
 #define STREAM_TYPE_VIDEO_VC1       0xea
 #define STREAM_TYPE_VIDEO_DIRAC     0xd1
 
diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index 56b9d85..0ddae65 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -22,10 +22,11 @@
 #include "libavutil/bswap.h"
 #include "libavutil/crc.h"
 #include "libavutil/dict.h"
+#include "libavutil/intreadwrite.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/opt.h"
 #include "libavutil/avassert.h"
-#include "libavcodec/mpegvideo.h"
+#include "libavcodec/internal.h"
 #include "avformat.h"
 #include "internal.h"
 #include "mpegts.h"
@@ -288,6 +289,9 @@
         case AV_CODEC_ID_H264:
             stream_type = STREAM_TYPE_VIDEO_H264;
             break;
+        case AV_CODEC_ID_CAVS:
+            stream_type = STREAM_TYPE_VIDEO_CAVS;
+            break;
         case AV_CODEC_ID_DIRAC:
             stream_type = STREAM_TYPE_VIDEO_DIRAC;
             break;
@@ -1091,13 +1095,16 @@
         uint32_t state = -1;
 
         if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001) {
+            if (!st->nb_frames) {
             av_log(s, AV_LOG_ERROR, "H.264 bitstream malformed, "
                    "no startcode found, use the h264_mp4toannexb bitstream filter (-bsf h264_mp4toannexb)\n");
             return AVERROR(EINVAL);
+            }
+            av_log(s, AV_LOG_WARNING, "H.264 bitstream error, startcode missing\n");
         }
 
         do {
-            p = avpriv_mpv_find_start_code(p, buf_end, &state);
+            p = avpriv_find_start_code(p, buf_end, &state);
             av_dlog(s, "nal %d\n", state & 0x1f);
         } while (p < buf_end && (state & 0x1f) != 9 &&
                  (state & 0x1f) != 5 && (state & 0x1f) != 1);
diff --git a/libavformat/mpjpeg.c b/libavformat/mpjpeg.c
index 916938f..2586ea8 100644
--- a/libavformat/mpjpeg.c
+++ b/libavformat/mpjpeg.c
@@ -47,7 +47,6 @@
 
     snprintf(buf1, sizeof(buf1), "\r\n--%s\r\n", BOUNDARY_TAG);
     avio_write(s->pb, buf1, strlen(buf1));
-    avio_flush(s->pb);
     return 0;
 }
 
diff --git a/libavformat/mpl2dec.c b/libavformat/mpl2dec.c
index ce2061b..b152cc8 100644
--- a/libavformat/mpl2dec.c
+++ b/libavformat/mpl2dec.c
@@ -40,8 +40,8 @@
     const unsigned char *ptr_end = ptr + p->buf_size;
 
     for (i = 0; i < 2; i++) {
-        if (sscanf(ptr, "[%"PRId64"][%"PRId64"]%c", &start, &end, &c) != 3 &&
-            sscanf(ptr, "[%"PRId64"][]%c",          &start,       &c) != 2)
+        if (sscanf(ptr, "[%"SCNd64"][%"SCNd64"]%c", &start, &end, &c) != 3 &&
+            sscanf(ptr, "[%"SCNd64"][]%c",          &start,       &c) != 2)
             return 0;
         ptr += strcspn(ptr, "\r\n") + 1;
         if (ptr >= ptr_end)
@@ -56,13 +56,13 @@
     int len;
     int64_t end;
 
-    if (sscanf(*line, "[%"PRId64"][]%c%n",
+    if (sscanf(*line, "[%"SCNd64"][]%c%n",
                pts_start, &c, &len) >= 2) {
         *duration = -1;
         *line += len - 1;
         return 0;
     }
-    if (sscanf(*line, "[%"PRId64"][%"PRId64"]%c%n",
+    if (sscanf(*line, "[%"SCNd64"][%"SCNd64"]%c%n",
                pts_start, &end, &c, &len) >= 3) {
         *duration = end - *pts_start;
         *line += len - 1;
diff --git a/libavformat/mtv.c b/libavformat/mtv.c
index 5f39ec5..6ffbb51 100644
--- a/libavformat/mtv.c
+++ b/libavformat/mtv.c
@@ -114,7 +114,7 @@
     audio_subsegments = avio_rl16(pb);
 
     if (audio_subsegments == 0) {
-        av_log_ask_for_sample(s, "MTV files without audio are not supported\n");
+        avpriv_request_sample(s, "MTV files without audio");
         return AVERROR_PATCHWELCOME;
     }
 
diff --git a/libavformat/mux.c b/libavformat/mux.c
index 98ec7a4..1153ab6 100644
--- a/libavformat/mux.c
+++ b/libavformat/mux.c
@@ -494,6 +494,8 @@
 
     did_split = av_packet_split_side_data(pkt);
     ret = s->oformat->write_packet(s, pkt);
+    if (s->flush_packets && s->pb && s->pb->error >= 0)
+        avio_flush(s->pb);
     if (did_split)
         av_packet_merge_side_data(pkt);
     return ret;
@@ -506,6 +508,8 @@
     if (!pkt) {
         if (s->oformat->flags & AVFMT_ALLOW_FLUSH) {
             ret = s->oformat->write_packet(s, NULL);
+            if (s->flush_packets && s->pb && s->pb->error >= 0)
+                avio_flush(s->pb);
             if (ret >= 0 && s->pb && s->pb->error < 0)
                 ret = s->pb->error;
             return ret;
@@ -540,7 +544,10 @@
     if (!this_pktl)
         return AVERROR(ENOMEM);
     this_pktl->pkt = *pkt;
+#if FF_API_DESTRUCT_PACKET
     pkt->destruct  = NULL;           // do not free original but only the copy
+#endif
+    pkt->buf       = NULL;
     av_dup_packet(&this_pktl->pkt);  // duplicate the packet if it uses non-allocated memory
 
     if (s->streams[pkt->stream_index]->last_in_packet_buffer) {
@@ -696,15 +703,6 @@
     }
 }
 
-#if FF_API_INTERLEAVE_PACKET
-int av_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
-                                 AVPacket *pkt, int flush)
-{
-    return ff_interleave_packet_per_dts(s, out, pkt, flush);
-}
-
-#endif
-
 /**
  * Interleave an AVPacket correctly so it can be muxed.
  * @param out the interleaved packet will be output here
diff --git a/libavformat/mvdec.c b/libavformat/mvdec.c
index 103a40a..5525233 100644
--- a/libavformat/mvdec.c
+++ b/libavformat/mvdec.c
@@ -172,7 +172,7 @@
         } else if (!strcmp(str, "MVC2")) {
             st->codec->codec_id = AV_CODEC_ID_MVC2;
         } else {
-            av_log_ask_for_sample(avctx, "unknown video compression %s\n", str);
+            avpriv_request_sample(avctx, "video compression %s", str);
         }
         av_free(str);
     } else if (!strcmp(name, "FPS")) {
@@ -214,7 +214,7 @@
         name[sizeof(name) - 1] = 0;
         size = avio_rb32(pb);
         if (parse(avctx, st, name, size) < 0) {
-            av_log_ask_for_sample(avctx, "unknown variable %s\n", name);
+            avpriv_request_sample(avctx, "variable %s", name);
             avio_skip(pb, size);
         }
     }
@@ -274,7 +274,7 @@
             vst->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
             break;
         default:
-            av_log_ask_for_sample(avctx, "unknown video compression %i\n", v);
+            avpriv_request_sample(avctx, "video compression %i", v);
             break;
         }
         vst->codec->codec_tag = 0;
@@ -293,7 +293,7 @@
         if (v == AUDIO_FORMAT_SIGNED) {
             ast->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
         } else {
-            av_log_ask_for_sample(avctx, "unknown audio compression (format %i)\n", v);
+            avpriv_request_sample(avctx, "audio compression (format %i)", v);
         }
 
         avio_skip(pb, 12);
@@ -317,7 +317,7 @@
         read_table(avctx, NULL, parse_global_var);
 
         if (mv->nb_audio_tracks > 1) {
-            av_log_ask_for_sample(avctx, "multiple audio streams\n");
+            avpriv_request_sample(avctx, "multiple audio streams support");
             return AVERROR_PATCHWELCOME;
         } else if (mv->nb_audio_tracks) {
             ast = avformat_new_stream(avctx, NULL);
@@ -329,7 +329,7 @@
             if (ast->codec->codec_tag == 100 && ast->codec->codec_id == AUDIO_FORMAT_SIGNED && ast->codec->bits_per_coded_sample == 16) {
                 ast->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
             } else {
-                av_log_ask_for_sample(avctx, "unknown audio compression %i (format %i, width %i)\n",
+                avpriv_request_sample(avctx, "audio compression %i (format %i, width %i)",
                     ast->codec->codec_tag, ast->codec->codec_id, ast->codec->bits_per_coded_sample);
                 ast->codec->codec_id = AV_CODEC_ID_NONE;
             }
@@ -341,7 +341,7 @@
         }
 
         if (mv->nb_video_tracks > 1) {
-            av_log_ask_for_sample(avctx, "multiple video streams\n");
+            avpriv_request_sample(avctx, "multiple video streams support");
             return AVERROR_PATCHWELCOME;
         } else if (mv->nb_video_tracks) {
             vst = avformat_new_stream(avctx, NULL);
@@ -357,7 +357,7 @@
         if (mv->nb_video_tracks)
             read_index(pb, vst);
     } else {
-        av_log_ask_for_sample(avctx, "unknown version %i\n", version);
+        avpriv_request_sample(avctx, "version %i", version);
         return AVERROR_PATCHWELCOME;
     }
 
diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c
index f7aaa5a..286a884 100644
--- a/libavformat/mxfdec.c
+++ b/libavformat/mxfdec.c
@@ -149,6 +149,7 @@
     int frame_layout; /* See MXFFrameLayout enum */
     int channels;
     int bits_per_sample;
+    int field_dominance;
     unsigned int component_depth;
     unsigned int horiz_subsampling;
     unsigned int vert_subsampling;
@@ -411,8 +412,7 @@
     int item_len = avio_rb32(pb);
 
     if (item_len != 18) {
-        av_log_ask_for_sample(pb, "unsupported primer pack item length %d\n",
-                              item_len);
+        avpriv_request_sample(pb, "Primer pack item length %d", item_len);
         return AVERROR_PATCHWELCOME;
     }
     if (item_num > 65536) {
@@ -839,6 +839,9 @@
         descriptor->aspect_ratio.num = avio_rb32(pb);
         descriptor->aspect_ratio.den = avio_rb32(pb);
         break;
+    case 0x3212:
+        descriptor->field_dominance = avio_r8(pb);
+        break;
     case 0x3301:
         descriptor->component_depth = avio_rb32(pb);
         break;
@@ -1602,7 +1605,7 @@
             st->codec->extradata = av_mallocz(descriptor->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
             if (st->codec->extradata)
                 memcpy(st->codec->extradata, descriptor->extradata, descriptor->extradata_size);
-        } else if(st->codec->codec_id == CODEC_ID_H264) {
+        } else if(st->codec->codec_id == AV_CODEC_ID_H264) {
             ff_generate_avci_extradata(st);
         }
         if (st->codec->codec_type != AVMEDIA_TYPE_DATA && (*essence_container_ul)[15] > 0x01) {
@@ -1616,6 +1619,124 @@
     return ret;
 }
 
+static int mxf_read_utf16_string(AVIOContext *pb, int size, char** str)
+{
+    int ret;
+    size_t buf_size;
+
+    if (size < 0)
+        return AVERROR(EINVAL);
+
+    buf_size = size + size/2 + 1;
+    *str = av_malloc(buf_size);
+    if (!*str)
+        return AVERROR(ENOMEM);
+
+    if ((ret = avio_get_str16be(pb, size, *str, buf_size)) < 0) {
+        av_freep(str);
+        return ret;
+    }
+
+    return ret;
+}
+
+static int mxf_uid_to_str(UID uid, char **str)
+{
+    int i;
+    char *p;
+    p = *str = av_mallocz(sizeof(UID) * 2 + 4 + 1);
+    if (!p)
+        return AVERROR(ENOMEM);
+    for (i = 0; i < sizeof(UID); i++) {
+        snprintf(p, 2 + 1, "%.2x", uid[i]);
+        p += 2;
+        if (i == 3 || i == 5 || i == 7 || i == 9) {
+            snprintf(p, 1 + 1, "-");
+            p++;
+        }
+    }
+    return 0;
+}
+
+static int mxf_timestamp_to_str(uint64_t timestamp, char **str)
+{
+    struct tm time = {0};
+    time.tm_year = (timestamp >> 48) - 1900;
+    time.tm_mon  = (timestamp >> 40 & 0xFF) - 1;
+    time.tm_mday = (timestamp >> 32 & 0xFF);
+    time.tm_hour = (timestamp >> 24 & 0xFF);
+    time.tm_min  = (timestamp >> 16 & 0xFF);
+    time.tm_sec  = (timestamp >> 8  & 0xFF);
+
+    /* ensure month/day are valid */
+    time.tm_mon  = FFMAX(time.tm_mon, 0);
+    time.tm_mday = FFMAX(time.tm_mday, 1);
+
+    *str = av_mallocz(32);
+    if (!*str)
+        return AVERROR(ENOMEM);
+    strftime(*str, 32, "%Y-%m-%d %H:%M:%S", &time);
+
+    return 0;
+}
+
+#define SET_STR_METADATA(pb, name, str) do { \
+    if ((ret = mxf_read_utf16_string(pb, size, &str)) < 0) \
+        return ret; \
+    av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \
+} while (0)
+
+#define SET_UID_METADATA(pb, name, var, str) do { \
+    avio_read(pb, var, 16); \
+    if ((ret = mxf_uid_to_str(var, &str)) < 0) \
+        return ret; \
+    av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \
+} while (0)
+
+#define SET_TS_METADATA(pb, name, var, str) do { \
+    var = avio_rb64(pb); \
+    if ((ret = mxf_timestamp_to_str(var, &str)) < 0) \
+        return ret; \
+    av_dict_set(&s->metadata, name, str, AV_DICT_DONT_STRDUP_VAL); \
+} while (0)
+
+static int mxf_read_identification_metadata(void *arg, AVIOContext *pb, int tag, int size, UID _uid, int64_t klv_offset)
+{
+    MXFContext *mxf = arg;
+    AVFormatContext *s = mxf->fc;
+    int ret;
+    UID uid = { 0 };
+    char *str = NULL;
+    uint64_t ts;
+    switch (tag) {
+    case 0x3C01:
+        SET_STR_METADATA(pb, "company_name", str);
+        break;
+    case 0x3C02:
+        SET_STR_METADATA(pb, "product_name", str);
+        break;
+    case 0x3C04:
+        SET_STR_METADATA(pb, "product_version", str);
+        break;
+    case 0x3C05:
+        SET_UID_METADATA(pb, "product_uid", uid, str);
+        break;
+    case 0x3C06:
+        SET_TS_METADATA(pb, "modification_date", ts, str);
+        break;
+    case 0x3C08:
+        SET_STR_METADATA(pb, "application_platform", str);
+        break;
+    case 0x3C09:
+        SET_UID_METADATA(pb, "generation_uid", uid, str);
+        break;
+    case 0x3C0A:
+        SET_UID_METADATA(pb, "uid", uid, str);
+        break;
+    }
+    return 0;
+}
+
 static const MXFMetadataReadTableEntry mxf_metadata_read_table[] = {
     { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x05,0x01,0x00 }, mxf_read_primer_pack },
     { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02,0x01,0x00 }, mxf_read_partition_pack },
@@ -1628,6 +1749,7 @@
     { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }, mxf_read_partition_pack },
     { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x02,0x00 }, mxf_read_partition_pack },
     { { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }, mxf_read_partition_pack },
+    { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x30,0x00 }, mxf_read_identification_metadata },
     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 }, mxf_read_content_storage, 0, AnyType },
     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 }, mxf_read_source_package, sizeof(MXFPackage), SourcePackage },
     { { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 }, mxf_read_material_package, sizeof(MXFPackage), MaterialPackage },
@@ -2127,9 +2249,11 @@
             if (next_ofs >= 0 && next_klv > next_ofs) {
                 /* if this check is hit then it's possible OPAtom was treated as OP1a
                  * truncate the packet since it's probably very large (>2 GiB is common) */
-                av_log_ask_for_sample(s,
-                    "KLV for edit unit %i extends into next edit unit - OPAtom misinterpreted as OP1a?\n",
-                    mxf->current_edit_unit);
+                avpriv_request_sample(s,
+                                      "OPAtom misinterpreted as OP1a?"
+                                      "KLV for edit unit %i extending into "
+                                      "next edit unit",
+                                      mxf->current_edit_unit);
                 klv.length = next_ofs - avio_tell(s->pb);
             }
 
@@ -2317,6 +2441,7 @@
     MXFContext* mxf = s->priv_data;
     int64_t seekpos;
     int i, ret;
+    int64_t ret64;
     MXFIndexTable *t;
     MXFTrack *source_track = st->priv_data;
 
@@ -2331,9 +2456,10 @@
         sample_time = 0;
     seconds = av_rescale(sample_time, st->time_base.num, st->time_base.den);
 
-    if ((ret = avio_seek(s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET)) < 0)
-        return ret;
+    if ((ret64 = avio_seek(s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET)) < 0)
+        return ret64;
     ff_update_cur_dts(s, st, sample_time);
+    mxf->current_edit_unit = sample_time;
     } else {
         t = &mxf->index_tables[0];
 
diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c
index cf9b77d..ea13ee0 100644
--- a/libavformat/mxfenc.c
+++ b/libavformat/mxfenc.c
@@ -32,6 +32,7 @@
 
 //#define DEBUG
 
+#include <inttypes.h>
 #include <math.h>
 #include <time.h>
 
diff --git a/libavformat/mxg.c b/libavformat/mxg.c
index e9a6f22..604be78 100644
--- a/libavformat/mxg.c
+++ b/libavformat/mxg.c
@@ -170,7 +170,10 @@
 
                 pkt->pts = pkt->dts = mxg->dts;
                 pkt->stream_index = 0;
+#if FF_API_DESTRUCT_PACKET
                 pkt->destruct = NULL;
+#endif
+                pkt->buf  = NULL;
                 pkt->size = mxg->buffer_ptr - mxg->soi_ptr;
                 pkt->data = mxg->soi_ptr;
 
@@ -208,7 +211,10 @@
                     /* time (GMT) of first sample in usec since 1970, little-endian */
                     pkt->pts = pkt->dts = AV_RL64(startmarker_ptr + 8);
                     pkt->stream_index = 1;
+#if FF_API_DESTRUCT_PACKET
                     pkt->destruct = NULL;
+#endif
+                    pkt->buf  = NULL;
                     pkt->size = size - 14;
                     pkt->data = startmarker_ptr + 16;
 
diff --git a/libavformat/nistspheredec.c b/libavformat/nistspheredec.c
index 59c2020..c09df9c 100644
--- a/libavformat/nistspheredec.c
+++ b/libavformat/nistspheredec.c
@@ -70,7 +70,7 @@
                        !av_strcasecmp(coding, "mu-law")) {
                 st->codec->codec_id = AV_CODEC_ID_PCM_MULAW;
             } else {
-                av_log_ask_for_sample(s, "unsupported coding: %s\n", coding);
+                avpriv_request_sample(s, "coding %s", coding);
             }
 
             avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
@@ -93,7 +93,7 @@
             } else if (!av_strcasecmp(format, "10")) {
                 be = 1;
             } else if (av_strcasecmp(format, "1")) {
-                av_log_ask_for_sample(s, "unsupported sample byte format: %s\n", format);
+                avpriv_request_sample(s, "sample byte format %s", format);
                 return AVERROR_PATCHWELCOME;
             }
         } else if (!memcmp(buffer, "sample_coding", 13)) {
diff --git a/libavformat/nsvdec.c b/libavformat/nsvdec.c
index bcc2180..e582f0f 100644
--- a/libavformat/nsvdec.c
+++ b/libavformat/nsvdec.c
@@ -2,6 +2,8 @@
  * NSV demuxer
  * Copyright (c) 2004 The FFmpeg Project
  *
+ * first version by Francois Revol <revol@free.fr>
+ *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
@@ -37,7 +39,6 @@
 #define NSV_MAX_RESYNC_TRIES 300
 
 /*
- * First version by Francois Revol - revol@free.fr
  * References:
  * (1) http://www.multimedia.cx/nsv-format.txt
  * seems someone came to the same conclusions as me, and updated it:
diff --git a/libavformat/nutdec.c b/libavformat/nutdec.c
index f260a3e..8896518 100644
--- a/libavformat/nutdec.c
+++ b/libavformat/nutdec.c
@@ -73,8 +73,10 @@
         return avio_rl16(bc);
     else if (len == 4)
         return avio_rl32(bc);
-    else
+    else {
+        av_log(NULL, AV_LOG_ERROR, "Unsupported fourcc length %d\n", len);
         return -1;
+    }
 }
 
 #ifdef TRACE
@@ -555,14 +557,14 @@
     tmp       = ffio_read_varlen(bc);
     *back_ptr = nut->last_syncpoint_pos - 16 * ffio_read_varlen(bc);
     if (*back_ptr < 0)
-        return -1;
+        return AVERROR_INVALIDDATA;
 
     ff_nut_reset_ts(nut, nut->time_base[tmp % nut->time_base_count],
                     tmp / nut->time_base_count);
 
     if (skip_reserved(bc, end) || ffio_get_checksum(bc)) {
         av_log(s, AV_LOG_ERROR, "sync point checksum mismatch\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
     *ts = tmp / nut->time_base_count *
diff --git a/libavformat/oggdec.c b/libavformat/oggdec.c
index 5cb03fe..fd954b8 100644
--- a/libavformat/oggdec.c
+++ b/libavformat/oggdec.c
@@ -195,7 +195,7 @@
         if (i >= ogg->nstreams)
             return ogg_new_stream(s, serial);
     } else if (ogg->nstreams != 1) {
-        av_log_missing_feature(s, "Changing stream parameters in multistream ogg", 0);
+        avpriv_report_missing_feature(s, "Changing stream parameters in multistream ogg");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -735,6 +735,11 @@
     int pstart, psize;
     int64_t fpos, pts, dts;
 
+    if (s->io_repositioned) {
+        ogg_reset(s);
+        s->io_repositioned = 0;
+    }
+
     //Get an ogg packet
 retry:
     do {
diff --git a/libavformat/oggenc.c b/libavformat/oggenc.c
index 3d4519c..9a815d1 100644
--- a/libavformat/oggenc.c
+++ b/libavformat/oggenc.c
@@ -81,7 +81,7 @@
     { "pagesize", "preferred page size in bytes (deprecated)",
         OFFSET(pref_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, MAX_PAGE_SIZE, PARAM },
     { "page_duration", "preferred page duration, in microseconds",
-        OFFSET(pref_duration), AV_OPT_TYPE_INT, { .i64 = 1000000 }, 0, INT64_MAX, PARAM },
+        OFFSET(pref_duration), AV_OPT_TYPE_INT64, { .i64 = 1000000 }, 0, INT64_MAX, PARAM },
     { NULL },
 };
 
diff --git a/libavformat/oggparseskeleton.c b/libavformat/oggparseskeleton.c
index b6959a6..307387d 100644
--- a/libavformat/oggparseskeleton.c
+++ b/libavformat/oggparseskeleton.c
@@ -75,8 +75,8 @@
         target_idx = ogg_find_stream(ogg, AV_RL32(buf+12));
         start_granule = AV_RL64(buf+36);
         if (os->start_granule != OGG_NOGRANULE_VALUE) {
-            av_log_missing_feature(s,
-                                   "Multiple fisbone for the same stream", 0);
+            avpriv_report_missing_feature(s,
+                                          "Multiple fisbone for the same stream");
             return 1;
         }
         if (target_idx >= 0 && start_granule != OGG_NOGRANULE_VALUE) {
diff --git a/libavformat/oggparsevorbis.c b/libavformat/oggparsevorbis.c
index 0b52bc7..da029a4 100644
--- a/libavformat/oggparsevorbis.c
+++ b/libavformat/oggparsevorbis.c
@@ -122,7 +122,7 @@
             }
 
             for (j = 0; j < tl; j++)
-                tt[j] = toupper(t[j]);
+                tt[j] = av_toupper(t[j]);
             tt[tl] = 0;
 
             memcpy(ct, v, vl);
diff --git a/libavformat/oma.c b/libavformat/oma.c
index 9e4bfbd..fc926bf 100644
--- a/libavformat/oma.c
+++ b/libavformat/oma.c
@@ -22,7 +22,7 @@
 #include "oma.h"
 #include "libavcodec/avcodec.h"
 
-const uint16_t ff_oma_srate_tab[8] = { 320, 441, 480, 882, 960, 0, 0, 0};
+const uint16_t ff_oma_srate_tab[8] = { 320, 441, 480, 882, 960, 0 };
 
 const AVCodecTag ff_oma_codec_tags[] = {
     { AV_CODEC_ID_ATRAC3,      OMA_CODECID_ATRAC3  },
diff --git a/libavformat/omadec.c b/libavformat/omadec.c
index 8d29675..ae35907 100644
--- a/libavformat/omadec.c
+++ b/libavformat/omadec.c
@@ -309,10 +309,13 @@
 
     switch (buf[32]) {
         case OMA_CODECID_ATRAC3:
-            samplerate = ff_oma_srate_tab[(codec_params >> 13) & 7]*100;
+            samplerate = ff_oma_srate_tab[(codec_params >> 13) & 7] * 100;
+            if (!samplerate) {
+                av_log(s, AV_LOG_ERROR, "Unsupported sample rate\n");
+                return AVERROR_INVALIDDATA;
+            }
             if (samplerate != 44100)
-                av_log_ask_for_sample(s, "Unsupported sample rate: %d\n",
-                                      samplerate);
+                avpriv_request_sample(s, "Sample rate %d", samplerate);
 
             framesize = (codec_params & 0x3FF) * 8;
             jsflag = (codec_params >> 17) & 1; /* get stereo coding mode, 1 for joint-stereo */
@@ -340,9 +343,14 @@
         case OMA_CODECID_ATRAC3P:
             st->codec->channels = (codec_params >> 10) & 7;
             framesize = ((codec_params & 0x3FF) * 8) + 8;
-            st->codec->sample_rate = ff_oma_srate_tab[(codec_params >> 13) & 7]*100;
-            st->codec->bit_rate    = st->codec->sample_rate * framesize * 8 / 1024;
-            avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
+            samplerate = ff_oma_srate_tab[(codec_params >> 13) & 7] * 100;
+            if (!samplerate) {
+                av_log(s, AV_LOG_ERROR, "Unsupported sample rate\n");
+                return AVERROR_INVALIDDATA;
+            }
+            st->codec->sample_rate = samplerate;
+            st->codec->bit_rate    = samplerate * framesize * 8 / 1024;
+            avpriv_set_pts_info(st, 64, 1, samplerate);
             av_log(s, AV_LOG_ERROR, "Unsupported codec ATRAC3+!\n");
             break;
         case OMA_CODECID_MP3:
diff --git a/libavformat/options_table.h b/libavformat/options_table.h
index 6750050..9f86137 100644
--- a/libavformat/options_table.h
+++ b/libavformat/options_table.h
@@ -75,6 +75,7 @@
 {"avoid_negative_ts", "shift timestamps to make them positive. 1 enables, 0 disables, default of -1 enables when required by target format.", OFFSET(avoid_negative_ts), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, E},
 {"skip_initial_bytes", "skip initial bytes", OFFSET(skip_initial_bytes), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX-1, D},
 {"correct_ts_overflow", "correct single timestamp overflows", OFFSET(correct_ts_overflow), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, D},
+{"flush_packets", "enable flushing of the I/O context after each packet", OFFSET(flush_packets), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E},
 {NULL},
 };
 
diff --git a/libavformat/os_support.c b/libavformat/os_support.c
index ad1a23d..dc88834 100644
--- a/libavformat/os_support.c
+++ b/libavformat/os_support.c
@@ -29,10 +29,14 @@
 
 #if defined(_WIN32) && !defined(__MINGW32CE__)
 #undef open
+#undef lseek
+#undef stat
+#undef fstat
 #include <fcntl.h>
 #include <io.h>
 #include <windows.h>
 #include <share.h>
+#include <errno.h>
 
 int ff_win32_open(const char *filename_utf8, int oflag, int pmode)
 {
@@ -41,20 +45,25 @@
     wchar_t *filename_w;
 
     /* convert UTF-8 to wide chars */
-    num_chars = MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, NULL, 0);
+    num_chars = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filename_utf8, -1, NULL, 0);
     if (num_chars <= 0)
-        return -1;
+        goto fallback;
     filename_w = av_mallocz(sizeof(wchar_t) * num_chars);
+    if (!filename_w) {
+        errno = ENOMEM;
+        return -1;
+    }
     MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, filename_w, num_chars);
 
     fd = _wsopen(filename_w, oflag, SH_DENYNO, pmode);
     av_freep(&filename_w);
 
-    /* filename maybe be in CP_ACP */
-    if (fd == -1 && !(oflag & O_CREAT))
-        return _sopen(filename_utf8, oflag, SH_DENYNO, pmode);
+    if (fd != -1 || (oflag & O_CREAT))
+        return fd;
 
-    return fd;
+fallback:
+    /* filename may be be in CP_ACP */
+    return _sopen(filename_utf8, oflag, SH_DENYNO, pmode);
 }
 #endif
 
diff --git a/libavformat/os_support.h b/libavformat/os_support.h
index b06cefd..e5f31e0 100644
--- a/libavformat/os_support.h
+++ b/libavformat/os_support.h
@@ -31,7 +31,7 @@
 
 #include <sys/stat.h>
 
-#if defined(__MINGW32__) && !defined(__MINGW32CE__)
+#if defined(_WIN32) && !defined(__MINGW32CE__)
 #  include <fcntl.h>
 #  ifdef lseek
 #   undef lseek
diff --git a/libavformat/pjsdec.c b/libavformat/pjsdec.c
index ef2b626..a69a316 100644
--- a/libavformat/pjsdec.c
+++ b/libavformat/pjsdec.c
@@ -39,7 +39,7 @@
     int64_t start, end;
     const unsigned char *ptr = p->buf;
 
-    if (sscanf(ptr, "%"PRId64",%"PRId64",%c", &start, &end, &c) == 3) {
+    if (sscanf(ptr, "%"SCNd64",%"SCNd64",%c", &start, &end, &c) == 3) {
         size_t q1pos = strcspn(ptr, "\"");
         size_t q2pos = q1pos + strcspn(ptr + q1pos + 1, "\"") + 1;
         if (strcspn(ptr, "\r\n") > q2pos)
@@ -52,7 +52,7 @@
 {
     int64_t start, end;
 
-    if (sscanf(*line, "%"PRId64",%"PRId64, &start, &end) == 2) {
+    if (sscanf(*line, "%"SCNd64",%"SCNd64, &start, &end) == 2) {
         *line += strcspn(*line, "\"") + 1;
         *duration = end - start;
         return start;
diff --git a/libavformat/pmpdec.c b/libavformat/pmpdec.c
index e0394a9..2fe6c46 100644
--- a/libavformat/pmpdec.c
+++ b/libavformat/pmpdec.c
@@ -139,7 +139,7 @@
         int num_packets;
         pmp->audio_packets = avio_r8(pb);
         if (!pmp->audio_packets) {
-            av_log_ask_for_sample(s, "0 audio packets\n");
+            avpriv_request_sample(s, "0 audio packets");
             return AVERROR_PATCHWELCOME;
         }
         num_packets = (pmp->num_streams - 1) * pmp->audio_packets + 1;
diff --git a/libavformat/psxstr.c b/libavformat/psxstr.c
index a2a629f..90c933e 100644
--- a/libavformat/psxstr.c
+++ b/libavformat/psxstr.c
@@ -235,6 +235,10 @@
                     *ret_pkt = *pkt;
                     pkt->data= NULL;
                     pkt->size= -1;
+                    pkt->buf = NULL;
+#if FF_API_DESTRUCT_PACKET
+                    pkt->destruct = NULL;
+#endif
                     return 0;
                 }
 
diff --git a/libavformat/rawdec.c b/libavformat/rawdec.c
index 07c2782..153cc7b 100644
--- a/libavformat/rawdec.c
+++ b/libavformat/rawdec.c
@@ -70,7 +70,6 @@
 {
     AVStream *st;
     FFRawVideoDemuxerContext *s1 = s->priv_data;
-    AVRational framerate;
     int ret = 0;
 
 
@@ -84,12 +83,7 @@
     st->codec->codec_id = s->iformat->raw_codec_id;
     st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
 
-    if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
-        av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
-        goto fail;
-    }
-
-    st->codec->time_base = av_inv_q(framerate);
+    st->codec->time_base = av_inv_q(s1->framerate);
     avpriv_set_pts_info(st, 64, 1, 1200000);
 
 fail:
@@ -101,7 +95,7 @@
 #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
 #define DEC AV_OPT_FLAG_DECODING_PARAM
 const AVOption ff_rawvideo_options[] = {
-    { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC},
+    { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC},
     { NULL },
 };
 
diff --git a/libavformat/rawdec.h b/libavformat/rawdec.h
index d978295..5910855 100644
--- a/libavformat/rawdec.h
+++ b/libavformat/rawdec.h
@@ -30,7 +30,7 @@
     const AVClass *class;     /**< Class for private options. */
     char *video_size;         /**< String describing video size, set by a private option. */
     char *pixel_format;       /**< Set by a private option. */
-    char *framerate;          /**< String describing framerate, set by a private option. */
+    AVRational framerate;     /**< AVRational describing framerate, set by a private option. */
 } FFRawVideoDemuxerContext;
 
 extern const AVOption ff_rawvideo_options[];
diff --git a/libavformat/rawenc.c b/libavformat/rawenc.c
index 0345f70..7894c8e 100644
--- a/libavformat/rawenc.c
+++ b/libavformat/rawenc.c
@@ -26,7 +26,6 @@
 int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
 {
     avio_write(s->pb, pkt->data, pkt->size);
-    avio_flush(s->pb);
     return 0;
 }
 
@@ -268,3 +267,15 @@
     .flags             = AVFMT_NOTIMESTAMPS,
 };
 #endif
+
+#if CONFIG_VC1_MUXER
+AVOutputFormat ff_vc1_muxer = {
+    .name              = "vc1",
+    .long_name         = NULL_IF_CONFIG_SMALL("raw VC-1 video"),
+    .extensions        = "vc1",
+    .audio_codec       = AV_CODEC_ID_NONE,
+    .video_codec       = AV_CODEC_ID_VC1,
+    .write_packet      = ff_raw_write_packet,
+    .flags             = AVFMT_NOTIMESTAMPS,
+};
+#endif
diff --git a/libavformat/rawvideodec.c b/libavformat/rawvideodec.c
index 8460781..cbcae43 100644
--- a/libavformat/rawvideodec.c
+++ b/libavformat/rawvideodec.c
@@ -27,18 +27,16 @@
 
 typedef struct RawVideoDemuxerContext {
     const AVClass *class;     /**< Class for private options. */
-    char *video_size;         /**< String describing video size, set by a private option. */
+    int width, height;        /**< Integers describing video size, set by a private option. */
     char *pixel_format;       /**< Set by a private option. */
-    char *framerate;          /**< String describing framerate, set by a private option. */
+    AVRational framerate;     /**< AVRational describing framerate, set by a private option. */
 } RawVideoDemuxerContext;
 
 
 static int rawvideo_read_header(AVFormatContext *ctx)
 {
     RawVideoDemuxerContext *s = ctx->priv_data;
-    int width = 0, height = 0, ret = 0;
     enum AVPixelFormat pix_fmt;
-    AVRational framerate;
     AVStream *st;
 
     st = avformat_new_stream(ctx, NULL);
@@ -49,30 +47,18 @@
 
     st->codec->codec_id = ctx->iformat->raw_codec_id;
 
-    if (s->video_size &&
-        (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Couldn't parse video size.\n");
-        return ret;
-    }
-
     if ((pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
         av_log(ctx, AV_LOG_ERROR, "No such pixel format: %s.\n",
                s->pixel_format);
         return AVERROR(EINVAL);
     }
 
-    if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
-        av_log(ctx, AV_LOG_ERROR, "Could not parse framerate: %s.\n",
-               s->framerate);
-        return ret;
-    }
+    avpriv_set_pts_info(st, 64, s->framerate.den, s->framerate.num);
 
-    avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
-
-    st->codec->width  = width;
-    st->codec->height = height;
+    st->codec->width  = s->width;
+    st->codec->height = s->height;
     st->codec->pix_fmt = pix_fmt;
-    st->codec->bit_rate = av_rescale_q(avpicture_get_size(st->codec->pix_fmt, width, height),
+    st->codec->bit_rate = av_rescale_q(avpicture_get_size(st->codec->pix_fmt, s->width, s->height),
                                        (AVRational){8,1}, st->time_base);
 
     return 0;
@@ -103,9 +89,9 @@
 #define OFFSET(x) offsetof(RawVideoDemuxerContext, x)
 #define DEC AV_OPT_FLAG_DECODING_PARAM
 static const AVOption rawvideo_options[] = {
-    { "video_size", "set frame size", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
+    { "video_size", "set frame size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
     { "pixel_format", "set pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = "yuv420p"}, 0, 0, DEC },
-    { "framerate", "set frame rate", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
+    { "framerate", "set frame rate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC },
     { NULL },
 };
 
diff --git a/libavformat/riff.c b/libavformat/riff.c
index ac1a4ff..7ad96a8 100644
--- a/libavformat/riff.c
+++ b/libavformat/riff.c
@@ -27,11 +27,11 @@
 #include "libavcodec/bytestream.h"
 #include "libavutil/avassert.h"
 
-/* Note: when encoding, the first matching tag is used, so order is
-   important if multiple tags possible for a given codec.
-   Note also that this list is used for more than just riff, other
-   files use it as well.
-*/
+/* Note: When encoding, the first matching tag is used, so order is
+ * important if multiple tags are possible for a given codec.
+ * Note also that this list is used for more than just riff, other
+ * files use it as well.
+ */
 const AVCodecTag ff_codec_bmp_tags[] = {
     { AV_CODEC_ID_H264,         MKTAG('H', '2', '6', '4') },
     { AV_CODEC_ID_H264,         MKTAG('h', '2', '6', '4') },
@@ -52,7 +52,7 @@
     { AV_CODEC_ID_H263,         MKTAG('M', '2', '6', '3') },
     { AV_CODEC_ID_H263,         MKTAG('l', 's', 'v', 'm') },
     { AV_CODEC_ID_H263P,        MKTAG('H', '2', '6', '3') },
-    { AV_CODEC_ID_H263I,        MKTAG('I', '2', '6', '3') }, /* intel h263 */
+    { AV_CODEC_ID_H263I,        MKTAG('I', '2', '6', '3') }, /* Intel H.263 */
     { AV_CODEC_ID_H261,         MKTAG('H', '2', '6', '1') },
     { AV_CODEC_ID_H263,         MKTAG('U', '2', '6', '3') },
     { AV_CODEC_ID_MPEG4,        MKTAG('F', 'M', 'P', '4') },
@@ -61,8 +61,10 @@
     { AV_CODEC_ID_MPEG4,        MKTAG('X', 'V', 'I', 'D') },
     { AV_CODEC_ID_MPEG4,        MKTAG('M', 'P', '4', 'S') },
     { AV_CODEC_ID_MPEG4,        MKTAG('M', '4', 'S', '2') },
-    { AV_CODEC_ID_MPEG4,        MKTAG( 4 ,  0 ,  0 ,  0 ) }, /* some broken avi use this */
-    { AV_CODEC_ID_MPEG4,        MKTAG('Z', 'M', 'P', '4') }, /* some broken avi use this */
+    /* some broken AVIs use this */
+    { AV_CODEC_ID_MPEG4,        MKTAG( 4 ,  0 ,  0 ,  0 ) },
+    /* some broken AVIs use this */
+    { AV_CODEC_ID_MPEG4,        MKTAG('Z', 'M', 'P', '4') },
     { AV_CODEC_ID_MPEG4,        MKTAG('D', 'I', 'V', '1') },
     { AV_CODEC_ID_MPEG4,        MKTAG('B', 'L', 'Z', '0') },
     { AV_CODEC_ID_MPEG4,        MKTAG('m', 'p', '4', 'v') },
@@ -71,7 +73,8 @@
     { AV_CODEC_ID_MPEG4,        MKTAG('S', 'E', 'D', 'G') },
     { AV_CODEC_ID_MPEG4,        MKTAG('R', 'M', 'P', '4') },
     { AV_CODEC_ID_MPEG4,        MKTAG('3', 'I', 'V', '2') },
-    { AV_CODEC_ID_MPEG4,        MKTAG('W', 'A', 'W', 'V') }, /* WaWv MPEG-4 Video Codec */
+    /* WaWv MPEG-4 Video Codec */
+    { AV_CODEC_ID_MPEG4,        MKTAG('W', 'A', 'W', 'V') },
     { AV_CODEC_ID_MPEG4,        MKTAG('F', 'F', 'D', 'S') },
     { AV_CODEC_ID_MPEG4,        MKTAG('F', 'V', 'F', 'W') },
     { AV_CODEC_ID_MPEG4,        MKTAG('D', 'C', 'O', 'D') },
@@ -82,18 +85,22 @@
     { AV_CODEC_ID_MPEG4,        MKTAG('V', 'I', 'D', 'M') },
     { AV_CODEC_ID_MPEG4,        MKTAG('M', '4', 'T', '3') },
     { AV_CODEC_ID_MPEG4,        MKTAG('G', 'E', 'O', 'X') },
-    { AV_CODEC_ID_MPEG4,        MKTAG('H', 'D', 'X', '4') }, /* flipped video */
+    /* flipped video */
+    { AV_CODEC_ID_MPEG4,        MKTAG('H', 'D', 'X', '4') },
     { AV_CODEC_ID_MPEG4,        MKTAG('D', 'M', 'K', '2') },
     { AV_CODEC_ID_MPEG4,        MKTAG('D', 'I', 'G', 'I') },
     { AV_CODEC_ID_MPEG4,        MKTAG('I', 'N', 'M', 'C') },
-    { AV_CODEC_ID_MPEG4,        MKTAG('E', 'P', 'H', 'V') }, /* Ephv MPEG-4 */
+    /* Ephv MPEG-4 */
+    { AV_CODEC_ID_MPEG4,        MKTAG('E', 'P', 'H', 'V') },
     { AV_CODEC_ID_MPEG4,        MKTAG('E', 'M', '4', 'A') },
-    { AV_CODEC_ID_MPEG4,        MKTAG('M', '4', 'C', 'C') }, /* Divio MPEG-4 */
+    /* Divio MPEG-4 */
+    { AV_CODEC_ID_MPEG4,        MKTAG('M', '4', 'C', 'C') },
     { AV_CODEC_ID_MPEG4,        MKTAG('S', 'N', '4', '0') },
     { AV_CODEC_ID_MPEG4,        MKTAG('V', 'S', 'P', 'X') },
     { AV_CODEC_ID_MPEG4,        MKTAG('U', 'L', 'D', 'X') },
     { AV_CODEC_ID_MPEG4,        MKTAG('G', 'E', 'O', 'V') },
-    { AV_CODEC_ID_MPEG4,        MKTAG('S', 'I', 'P', 'P') }, /* Samsung SHR-6040 */
+    /* Samsung SHR-6040 */
+    { AV_CODEC_ID_MPEG4,        MKTAG('S', 'I', 'P', 'P') },
     { AV_CODEC_ID_MPEG4,        MKTAG('S', 'M', '4', 'V') },
     { AV_CODEC_ID_MPEG4,        MKTAG('X', 'V', 'I', 'X') },
     { AV_CODEC_ID_MPEG4,        MKTAG('D', 'r', 'e', 'X') },
@@ -122,9 +129,12 @@
     { AV_CODEC_ID_DVVIDEO,      MKTAG('d', 'v', 's', 'l') },
     { AV_CODEC_ID_DVVIDEO,      MKTAG('d', 'v', '2', '5') },
     { AV_CODEC_ID_DVVIDEO,      MKTAG('d', 'v', '5', '0') },
-    { AV_CODEC_ID_DVVIDEO,      MKTAG('c', 'd', 'v', 'c') }, /* Canopus DV */
-    { AV_CODEC_ID_DVVIDEO,      MKTAG('C', 'D', 'V', 'H') }, /* Canopus DV */
-    { AV_CODEC_ID_DVVIDEO,      MKTAG('C', 'D', 'V', '5') }, /* Canopus DV */
+    /* Canopus DV */
+    { AV_CODEC_ID_DVVIDEO,      MKTAG('c', 'd', 'v', 'c') },
+    /* Canopus DV */
+    { AV_CODEC_ID_DVVIDEO,      MKTAG('C', 'D', 'V', 'H') },
+    /* Canopus DV */
+    { AV_CODEC_ID_DVVIDEO,      MKTAG('C', 'D', 'V', '5') },
     { AV_CODEC_ID_DVVIDEO,      MKTAG('d', 'v', 'c', ' ') },
     { AV_CODEC_ID_DVVIDEO,      MKTAG('d', 'v', 'c', 's') },
     { AV_CODEC_ID_DVVIDEO,      MKTAG('d', 'v', 'h', '1') },
@@ -144,10 +154,12 @@
     { AV_CODEC_ID_MPEG4,        MKTAG( 4 ,  0 ,  0 ,  16) },
     { AV_CODEC_ID_MPEG2VIDEO,   MKTAG('D', 'V', 'R', ' ') },
     { AV_CODEC_ID_MPEG2VIDEO,   MKTAG('M', 'M', 'E', 'S') },
-    { AV_CODEC_ID_MPEG2VIDEO,   MKTAG('L', 'M', 'P', '2') }, /* Lead MPEG2 in avi */
+    /* Lead MPEG-2 in AVI */
+    { AV_CODEC_ID_MPEG2VIDEO,   MKTAG('L', 'M', 'P', '2') },
     { AV_CODEC_ID_MPEG2VIDEO,   MKTAG('s', 'l', 'i', 'f') },
     { AV_CODEC_ID_MPEG2VIDEO,   MKTAG('E', 'M', '2', 'V') },
-    { AV_CODEC_ID_MPEG2VIDEO,   MKTAG('M', '7', '0', '1') }, /* Matrox MPEG2 intra-only */
+    /* Matrox MPEG-2 intra-only */
+    { AV_CODEC_ID_MPEG2VIDEO,   MKTAG('M', '7', '0', '1') },
     { AV_CODEC_ID_MPEG2VIDEO,   MKTAG('m', 'p', 'g', 'v') },
     { AV_CODEC_ID_MPEG1VIDEO,   MKTAG('B', 'W', '1', '0') },
     { AV_CODEC_ID_MPEG1VIDEO,   MKTAG('X', 'M', 'P', 'G') }, /* Xing MPEG intra only */
@@ -156,23 +168,31 @@
     { AV_CODEC_ID_MJPEG,        MKTAG('d', 'm', 'b', '1') },
     { AV_CODEC_ID_MJPEG,        MKTAG('m', 'j', 'p', 'a') },
     { AV_CODEC_ID_LJPEG,        MKTAG('L', 'J', 'P', 'G') },
-    { AV_CODEC_ID_MJPEG,        MKTAG('J', 'P', 'G', 'L') }, /* Pegasus lossless JPEG */
-    { AV_CODEC_ID_JPEGLS,       MKTAG('M', 'J', 'L', 'S') }, /* JPEG-LS custom FOURCC for avi - encoder */
+    /* Pegasus lossless JPEG */
+    { AV_CODEC_ID_MJPEG,        MKTAG('J', 'P', 'G', 'L') },
+    /* JPEG-LS custom FOURCC for AVI - encoder */
+    { AV_CODEC_ID_JPEGLS,       MKTAG('M', 'J', 'L', 'S') },
     { AV_CODEC_ID_JPEGLS,       MKTAG('M', 'J', 'P', 'G') },
-    { AV_CODEC_ID_MJPEG,        MKTAG('M', 'J', 'L', 'S') }, /* JPEG-LS custom FOURCC for avi - decoder */
+    /* JPEG-LS custom FOURCC for AVI - decoder */
+    { AV_CODEC_ID_MJPEG,        MKTAG('M', 'J', 'L', 'S') },
     { AV_CODEC_ID_MJPEG,        MKTAG('j', 'p', 'e', 'g') },
     { AV_CODEC_ID_MJPEG,        MKTAG('I', 'J', 'P', 'G') },
     { AV_CODEC_ID_AVRN,         MKTAG('A', 'V', 'R', 'n') },
     { AV_CODEC_ID_MJPEG,        MKTAG('A', 'C', 'D', 'V') },
     { AV_CODEC_ID_MJPEG,        MKTAG('Q', 'I', 'V', 'G') },
-    { AV_CODEC_ID_MJPEG,        MKTAG('S', 'L', 'M', 'J') }, /* SL M-JPEG */
-    { AV_CODEC_ID_MJPEG,        MKTAG('C', 'J', 'P', 'G') }, /* Creative Webcam JPEG */
-    { AV_CODEC_ID_MJPEG,        MKTAG('I', 'J', 'L', 'V') }, /* Intel JPEG Library Video Codec */
-    { AV_CODEC_ID_MJPEG,        MKTAG('M', 'V', 'J', 'P') }, /* Midvid JPEG Video Codec */
+    /* SL M-JPEG */
+    { AV_CODEC_ID_MJPEG,        MKTAG('S', 'L', 'M', 'J') },
+    /* Creative Webcam JPEG */
+    { AV_CODEC_ID_MJPEG,        MKTAG('C', 'J', 'P', 'G') },
+    /* Intel JPEG Library Video Codec */
+    { AV_CODEC_ID_MJPEG,        MKTAG('I', 'J', 'L', 'V') },
+    /* Midvid JPEG Video Codec */
+    { AV_CODEC_ID_MJPEG,        MKTAG('M', 'V', 'J', 'P') },
     { AV_CODEC_ID_MJPEG,        MKTAG('A', 'V', 'I', '1') },
     { AV_CODEC_ID_MJPEG,        MKTAG('A', 'V', 'I', '2') },
     { AV_CODEC_ID_MJPEG,        MKTAG('M', 'T', 'S', 'J') },
-    { AV_CODEC_ID_MJPEG,        MKTAG('Z', 'J', 'P', 'G') }, /* Paradigm Matrix M-JPEG Codec */
+    /* Paradigm Matrix M-JPEG Codec */
+    { AV_CODEC_ID_MJPEG,        MKTAG('Z', 'J', 'P', 'G') },
     { AV_CODEC_ID_MJPEG,        MKTAG('M', 'M', 'J', 'P') },
     { AV_CODEC_ID_HUFFYUV,      MKTAG('H', 'F', 'Y', 'U') },
     { AV_CODEC_ID_FFVHUFF,      MKTAG('F', 'F', 'V', 'H') },
@@ -202,7 +222,8 @@
     { AV_CODEC_ID_RAWVIDEO,     MKTAG('Y', '8', ' ', ' ') },
     { AV_CODEC_ID_RAWVIDEO,     MKTAG('H', 'D', 'Y', 'C') },
     { AV_CODEC_ID_RAWVIDEO,     MKTAG('Y', 'V', 'U', '9') },
-    { AV_CODEC_ID_RAWVIDEO,     MKTAG('V', 'D', 'T', 'Z') }, /* SoftLab-NSK VideoTizer */
+    /* SoftLab-NSK VideoTizer */
+    { AV_CODEC_ID_RAWVIDEO,     MKTAG('V', 'D', 'T', 'Z') },
     { AV_CODEC_ID_RAWVIDEO,     MKTAG('Y', '4', '1', '1') },
     { AV_CODEC_ID_RAWVIDEO,     MKTAG('N', 'V', '1', '2') },
     { AV_CODEC_ID_RAWVIDEO,     MKTAG('N', 'V', '2', '1') },
@@ -333,18 +354,21 @@
 
 const AVCodecTag ff_codec_wav_tags[] = {
     { AV_CODEC_ID_PCM_S16LE,       0x0001 },
-    { AV_CODEC_ID_PCM_U8,          0x0001 }, /* must come after s16le in this list */
+    /* must come after s16le in this list */
+    { AV_CODEC_ID_PCM_U8,          0x0001 },
     { AV_CODEC_ID_PCM_S24LE,       0x0001 },
     { AV_CODEC_ID_PCM_S32LE,       0x0001 },
     { AV_CODEC_ID_ADPCM_MS,        0x0002 },
     { AV_CODEC_ID_PCM_F32LE,       0x0003 },
-    { AV_CODEC_ID_PCM_F64LE,       0x0003 }, /* must come after f32le in this list */
+    /* must come after f32le in this list */
+    { AV_CODEC_ID_PCM_F64LE,       0x0003 },
     { AV_CODEC_ID_PCM_ALAW,        0x0006 },
     { AV_CODEC_ID_PCM_MULAW,       0x0007 },
     { AV_CODEC_ID_WMAVOICE,        0x000A },
     { AV_CODEC_ID_ADPCM_IMA_OKI,   0x0010 },
     { AV_CODEC_ID_ADPCM_IMA_WAV,   0x0011 },
-    { AV_CODEC_ID_PCM_ZORK,        0x0011 }, /* must come after adpcm_ima_wav in this list */
+    /* must come after adpcm_ima_wav in this list */
+    { AV_CODEC_ID_PCM_ZORK,        0x0011 },
     { AV_CODEC_ID_ADPCM_IMA_OKI,   0x0017 },
     { AV_CODEC_ID_ADPCM_YAMAHA,    0x0020 },
     { AV_CODEC_ID_TRUESPEECH,      0x0022 },
@@ -356,8 +380,10 @@
     { AV_CODEC_ID_MP3,             0x0055 },
     { AV_CODEC_ID_AMR_NB,          0x0057 },
     { AV_CODEC_ID_AMR_WB,          0x0058 },
-    { AV_CODEC_ID_ADPCM_IMA_DK4,   0x0061 },  /* rogue format number */
-    { AV_CODEC_ID_ADPCM_IMA_DK3,   0x0062 },  /* rogue format number */
+    /* rogue format number */
+    { AV_CODEC_ID_ADPCM_IMA_DK4,   0x0061 },
+    /* rogue format number */
+    { AV_CODEC_ID_ADPCM_IMA_DK3,   0x0062 },
     { AV_CODEC_ID_ADPCM_IMA_WAV,   0x0069 },
     { AV_CODEC_ID_VOXWARE,         0x0075 },
     { AV_CODEC_ID_AAC,             0x00ff },
@@ -373,7 +399,8 @@
     { AV_CODEC_ID_IAC,             0x0402 },
     { AV_CODEC_ID_GSM_MS,          0x1500 },
     { AV_CODEC_ID_TRUESPEECH,      0x1501 },
-    { AV_CODEC_ID_AAC,             0x1600 }, /* ADTS AAC */
+    /* ADTS AAC */
+    { AV_CODEC_ID_AAC,             0x1600 },
     { AV_CODEC_ID_AAC_LATM,        0x1602 },
     { AV_CODEC_ID_AC3,             0x2000 },
     { AV_CODEC_ID_DTS,             0x2001 },
@@ -386,35 +413,52 @@
     { AV_CODEC_ID_AAC,             0xA106 },
     { AV_CODEC_ID_SPEEX,           0xA109 },
     { AV_CODEC_ID_FLAC,            0xF1AC },
-    { AV_CODEC_ID_ADPCM_SWF,       ('S'<<8)+'F' },
-    { AV_CODEC_ID_VORBIS,          ('V'<<8)+'o' }, //HACK/FIXME, does vorbis in WAV/AVI have an (in)official id?
+    { AV_CODEC_ID_ADPCM_SWF,       ('S' << 8) + 'F' },
+    /* HACK/FIXME: Does Vorbis in WAV/AVI have an (in)official ID? */
+    { AV_CODEC_ID_VORBIS,          ('V' << 8) + 'o' },
     { AV_CODEC_ID_NONE,      0 },
 };
 
 const AVCodecGuid ff_codec_wav_guids[] = {
-    {AV_CODEC_ID_AC3,        {0x2C,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA}},
-    {AV_CODEC_ID_ATRAC3P,    {0xBF,0xAA,0x23,0xE9,0x58,0xCB,0x71,0x44,0xA1,0x19,0xFF,0xFA,0x01,0xE4,0xCE,0x62}},
-    {AV_CODEC_ID_EAC3,       {0xAF,0x87,0xFB,0xA7,0x02,0x2D,0xFB,0x42,0xA4,0xD4,0x05,0xCD,0x93,0x84,0x3B,0xDD}},
-    {AV_CODEC_ID_MP2,        {0x2B,0x80,0x6D,0xE0,0x46,0xDB,0xCF,0x11,0xB4,0xD1,0x00,0x80,0x5F,0x6C,0xBB,0xEA}},
-    {AV_CODEC_ID_NONE}
+    { AV_CODEC_ID_AC3,      { 0x2C, 0x80, 0x6D, 0xE0, 0x46, 0xDB, 0xCF, 0x11, 0xB4, 0xD1, 0x00, 0x80, 0x5F, 0x6C, 0xBB, 0xEA } },
+    { AV_CODEC_ID_ATRAC3P,  { 0xBF, 0xAA, 0x23, 0xE9, 0x58, 0xCB, 0x71, 0x44, 0xA1, 0x19, 0xFF, 0xFA, 0x01, 0xE4, 0xCE, 0x62 } },
+    { AV_CODEC_ID_EAC3,     { 0xAF, 0x87, 0xFB, 0xA7, 0x02, 0x2D, 0xFB, 0x42, 0xA4, 0xD4, 0x05, 0xCD, 0x93, 0x84, 0x3B, 0xDD } },
+    { AV_CODEC_ID_MP2,      { 0x2B, 0x80, 0x6D, 0xE0, 0x46, 0xDB, 0xCF, 0x11, 0xB4, 0xD1, 0x00, 0x80, 0x5F, 0x6C, 0xBB, 0xEA } },
+    { AV_CODEC_ID_NONE }
 };
 
 const AVMetadataConv ff_riff_info_conv[] = {
-    { "IART", "artist"    },
-    { "ICMT", "comment"   },
-    { "ICOP", "copyright" },
-    { "ICRD", "date"      },
-    { "IGNR", "genre"     },
-    { "ILNG", "language"  },
-    { "INAM", "title"     },
-    { "IPRD", "album"     },
-    { "IPRT", "track"     },
-    { "ISFT", "encoder"   },
-    { "ISMP", "timecode"  },
-    { "ITCH", "encoded_by"},
+    { "IART", "artist"     },
+    { "ICMT", "comment"    },
+    { "ICOP", "copyright"  },
+    { "ICRD", "date"       },
+    { "IGNR", "genre"      },
+    { "ILNG", "language"   },
+    { "INAM", "title"      },
+    { "IPRD", "album"      },
+    { "IPRT", "track"      },
+    { "ISFT", "encoder"    },
+    { "ISMP", "timecode"   },
+    { "ITCH", "encoded_by" },
     { 0 },
 };
 
+void ff_get_guid(AVIOContext *s, ff_asf_guid *g)
+{
+    av_assert0(sizeof(*g) == 16); //compiler will optimize this out
+    if (avio_read(s, *g, sizeof(*g)) < (int)sizeof(*g))
+        memset(*g, 0, sizeof(*g));
+}
+
+enum AVCodecID ff_codec_guid_get_id(const AVCodecGuid *guids, ff_asf_guid guid)
+{
+    int i;
+    for (i = 0; guids[i].id != AV_CODEC_ID_NONE; i++)
+        if (!ff_guidcmp(guids[i].guid, guid))
+            return guids[i].id;
+    return AV_CODEC_ID_NONE;
+}
+
 #if CONFIG_MUXERS
 int64_t ff_start_tag(AVIOContext *pb, const char *tag)
 {
@@ -445,59 +489,63 @@
     int hdrsize = 18;
     int waveformatextensible;
     uint8_t temp[256];
-    uint8_t *riff_extradata= temp;
-    uint8_t *riff_extradata_start= temp;
+    uint8_t *riff_extradata       = temp;
+    uint8_t *riff_extradata_start = temp;
 
-    if(!enc->codec_tag || enc->codec_tag > 0xffff)
+    if (!enc->codec_tag || enc->codec_tag > 0xffff)
         return -1;
 
     /* We use the known constant frame size for the codec if known, otherwise
-       fallback to using AVCodecContext.frame_size, which is not as reliable
-       for indicating packet duration */
+     * fall back on using AVCodecContext.frame_size, which is not as reliable
+     * for indicating packet duration. */
     frame_size = av_get_audio_frame_duration(enc, 0);
     if (!frame_size)
         frame_size = enc->frame_size;
 
-    waveformatextensible =   (enc->channels > 2 && enc->channel_layout)
-                          || enc->sample_rate > 48000
-                          || av_get_bits_per_sample(enc->codec_id) > 16;
+    waveformatextensible = (enc->channels > 2 && enc->channel_layout) ||
+                           enc->sample_rate > 48000 ||
+                           av_get_bits_per_sample(enc->codec_id) > 16;
 
-    if (waveformatextensible) {
+    if (waveformatextensible)
         avio_wl16(pb, 0xfffe);
-    } else {
+    else
         avio_wl16(pb, enc->codec_tag);
-    }
+
     avio_wl16(pb, enc->channels);
     avio_wl32(pb, enc->sample_rate);
     if (enc->codec_id == AV_CODEC_ID_ATRAC3 ||
         enc->codec_id == AV_CODEC_ID_G723_1 ||
-        enc->codec_id == AV_CODEC_ID_GSM_MS ||
         enc->codec_id == AV_CODEC_ID_MP2    ||
-        enc->codec_id == AV_CODEC_ID_MP3) {
+        enc->codec_id == AV_CODEC_ID_MP3    ||
+        enc->codec_id == AV_CODEC_ID_GSM_MS) {
         bps = 0;
     } else {
         if (!(bps = av_get_bits_per_sample(enc->codec_id))) {
             if (enc->bits_per_coded_sample)
                 bps = enc->bits_per_coded_sample;
             else
-                bps = 16; // default to 16
+                bps = 16;  // default to 16
         }
     }
-    if(bps != enc->bits_per_coded_sample && enc->bits_per_coded_sample){
-        av_log(enc, AV_LOG_WARNING, "requested bits_per_coded_sample (%d) and actually stored (%d) differ\n", enc->bits_per_coded_sample, bps);
+    if (bps != enc->bits_per_coded_sample && enc->bits_per_coded_sample) {
+        av_log(enc, AV_LOG_WARNING,
+               "requested bits_per_coded_sample (%d) "
+               "and actually stored (%d) differ\n",
+               enc->bits_per_coded_sample, bps);
     }
 
-    if (enc->codec_id == AV_CODEC_ID_MP2 || enc->codec_id == AV_CODEC_ID_MP3) {
-        /* this is wrong, but it seems many demuxers do not work if this is set
-           correctly */
+    if (enc->codec_id == AV_CODEC_ID_MP2 ||
+        enc->codec_id == AV_CODEC_ID_MP3) {
+        /* This is wrong, but it seems many demuxers do not work if this
+         * is set correctly. */
         blkalign = frame_size;
-        //blkalign = 144 * enc->bit_rate/enc->sample_rate;
+        // blkalign = 144 * enc->bit_rate/enc->sample_rate;
     } else if (enc->codec_id == AV_CODEC_ID_AC3) {
-            blkalign = 3840; //maximum bytes per frame
+        blkalign = 3840;                /* maximum bytes per frame */
     } else if (enc->codec_id == AV_CODEC_ID_AAC) {
-            blkalign = 768 * enc->channels; //maximum bytes per frame
+        blkalign = 768 * enc->channels; /* maximum bytes per frame */
     } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
-            blkalign = 24;
+        blkalign = 24;
     } else if (enc->block_align != 0) { /* specified by the codec */
         blkalign = enc->block_align;
     } else
@@ -515,8 +563,8 @@
         bytespersec = enc->bit_rate / 8;
     }
     avio_wl32(pb, bytespersec); /* bytes per second */
-    avio_wl16(pb, blkalign); /* block align */
-    avio_wl16(pb, bps); /* bits per sample */
+    avio_wl16(pb, blkalign);    /* block align */
+    avio_wl16(pb, bps);         /* bits per sample */
     if (enc->codec_id == AV_CODEC_ID_MP3) {
         hdrsize += 12;
         bytestream_put_le16(&riff_extradata, 1);    /* wID */
@@ -526,33 +574,48 @@
         bytestream_put_le16(&riff_extradata, 1393); /* nCodecDelay */
     } else if (enc->codec_id == AV_CODEC_ID_MP2) {
         hdrsize += 22;
-        bytestream_put_le16(&riff_extradata, 2);                          /* fwHeadLayer */
-        bytestream_put_le32(&riff_extradata, enc->bit_rate);              /* dwHeadBitrate */
-        bytestream_put_le16(&riff_extradata, enc->channels == 2 ? 1 : 8); /* fwHeadMode */
-        bytestream_put_le16(&riff_extradata, 0);                          /* fwHeadModeExt */
-        bytestream_put_le16(&riff_extradata, 1);                          /* wHeadEmphasis */
-        bytestream_put_le16(&riff_extradata, 16);                         /* fwHeadFlags */
-        bytestream_put_le32(&riff_extradata, 0);                          /* dwPTSLow */
-        bytestream_put_le32(&riff_extradata, 0);                          /* dwPTSHigh */
+        /* fwHeadLayer */
+        bytestream_put_le16(&riff_extradata, 2);
+        /* dwHeadBitrate */
+        bytestream_put_le32(&riff_extradata, enc->bit_rate);
+        /* fwHeadMode */
+        bytestream_put_le16(&riff_extradata, enc->channels == 2 ? 1 : 8);
+        /* fwHeadModeExt */
+        bytestream_put_le16(&riff_extradata, 0);
+        /* wHeadEmphasis */
+        bytestream_put_le16(&riff_extradata, 1);
+        /* fwHeadFlags */
+        bytestream_put_le16(&riff_extradata, 16);
+        /* dwPTSLow */
+        bytestream_put_le32(&riff_extradata, 0);
+        /* dwPTSHigh */
+        bytestream_put_le32(&riff_extradata, 0);
     } else if (enc->codec_id == AV_CODEC_ID_G723_1) {
         hdrsize += 20;
         bytestream_put_le32(&riff_extradata, 0x9ace0002); /* extradata needed for msacm g723.1 codec */
         bytestream_put_le32(&riff_extradata, 0xaea2f732);
         bytestream_put_le16(&riff_extradata, 0xacde);
-    } else if (enc->codec_id == AV_CODEC_ID_GSM_MS || enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
+    } else if (enc->codec_id == AV_CODEC_ID_GSM_MS ||
+               enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
         hdrsize += 2;
-        bytestream_put_le16(&riff_extradata, frame_size); /* wSamplesPerBlock */
-    } else if(enc->extradata_size){
-        riff_extradata_start= enc->extradata;
-        riff_extradata= enc->extradata + enc->extradata_size;
-        hdrsize += enc->extradata_size;
+        /* wSamplesPerBlock */
+        bytestream_put_le16(&riff_extradata, frame_size);
+    } else if (enc->extradata_size) {
+        riff_extradata_start = enc->extradata;
+        riff_extradata       = enc->extradata + enc->extradata_size;
+        hdrsize             += enc->extradata_size;
     }
-    if(waveformatextensible) {                                    /* write WAVEFORMATEXTENSIBLE extensions */
+    /* write WAVEFORMATEXTENSIBLE extensions */
+    if (waveformatextensible) {
         hdrsize += 22;
-        avio_wl16(pb, riff_extradata - riff_extradata_start + 22); /* 22 is WAVEFORMATEXTENSIBLE size */
-        avio_wl16(pb, bps);                                        /* ValidBitsPerSample || SamplesPerBlock || Reserved */
-        avio_wl32(pb, enc->channel_layout);                        /* dwChannelMask */
-        avio_wl32(pb, enc->codec_tag);                             /* GUID + next 3 */
+        /* 22 is WAVEFORMATEXTENSIBLE size */
+        avio_wl16(pb, riff_extradata - riff_extradata_start + 22);
+        /* ValidBitsPerSample || SamplesPerBlock || Reserved */
+        avio_wl16(pb, bps);
+        /* dwChannelMask */
+        avio_wl32(pb, enc->channel_layout);
+        /* GUID + next 3 */
+        avio_wl32(pb, enc->codec_tag);
         avio_wl32(pb, 0x00100000);
         avio_wl32(pb, 0xAA000080);
         avio_wl32(pb, 0x719B3800);
@@ -560,7 +623,7 @@
         avio_wl16(pb, riff_extradata - riff_extradata_start); /* cbSize */
     }
     avio_write(pb, riff_extradata_start, riff_extradata - riff_extradata_start);
-    if(hdrsize&1){
+    if (hdrsize & 1) {
         hdrsize++;
         avio_w8(pb, 0);
     }
@@ -569,15 +632,18 @@
 }
 
 /* BITMAPINFOHEADER header */
-void ff_put_bmp_header(AVIOContext *pb, AVCodecContext *enc, const AVCodecTag *tags, int for_asf)
+void ff_put_bmp_header(AVIOContext *pb, AVCodecContext *enc,
+                       const AVCodecTag *tags, int for_asf)
 {
-    avio_wl32(pb, 40 + enc->extradata_size); /* size */
+    /* size */
+    avio_wl32(pb, 40 + enc->extradata_size);
     avio_wl32(pb, enc->width);
     //We always store RGB TopDown
     avio_wl32(pb, enc->codec_tag ? enc->height : -enc->height);
-    avio_wl16(pb, 1); /* planes */
-
-    avio_wl16(pb, enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24); /* depth */
+    /* planes */
+    avio_wl16(pb, 1);
+    /* depth */
+    avio_wl16(pb, enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24);
     /* compression type */
     avio_wl32(pb, enc->codec_tag);
     avio_wl32(pb, (enc->width * enc->height * (enc->bits_per_coded_sample ? enc->bits_per_coded_sample : 24)+7) / 8);
@@ -592,34 +658,36 @@
         avio_w8(pb, 0);
 }
 
-void ff_parse_specific_params(AVCodecContext *stream, int *au_rate, int *au_ssize, int *au_scale)
+void ff_parse_specific_params(AVCodecContext *stream, int *au_rate,
+                              int *au_ssize, int *au_scale)
 {
     int gcd;
     int audio_frame_size;
 
     /* We use the known constant frame size for the codec if known, otherwise
-       fallback to using AVCodecContext.frame_size, which is not as reliable
-       for indicating packet duration */
+     * fall back on using AVCodecContext.frame_size, which is not as reliable
+     * for indicating packet duration. */
     audio_frame_size = av_get_audio_frame_duration(stream, 0);
     if (!audio_frame_size)
         audio_frame_size = stream->frame_size;
 
-    *au_ssize= stream->block_align;
+    *au_ssize = stream->block_align;
     if (audio_frame_size && stream->sample_rate) {
         *au_scale = audio_frame_size;
-        *au_rate= stream->sample_rate;
-    }else if(stream->codec_type == AVMEDIA_TYPE_VIDEO ||
-             stream->codec_type == AVMEDIA_TYPE_DATA ||
-             stream->codec_type == AVMEDIA_TYPE_SUBTITLE){
-        *au_scale= stream->time_base.num;
-        *au_rate = stream->time_base.den;
-    }else{
-        *au_scale= stream->block_align ? stream->block_align*8 : 8;
-        *au_rate = stream->bit_rate ? stream->bit_rate : 8*stream->sample_rate;
+        *au_rate  = stream->sample_rate;
+    } else if (stream->codec_type == AVMEDIA_TYPE_VIDEO ||
+               stream->codec_type == AVMEDIA_TYPE_DATA ||
+               stream->codec_type == AVMEDIA_TYPE_SUBTITLE) {
+        *au_scale = stream->time_base.num;
+        *au_rate  = stream->time_base.den;
+    } else {
+        *au_scale = stream->block_align ? stream->block_align * 8 : 8;
+        *au_rate  = stream->bit_rate ? stream->bit_rate :
+                    8 * stream->sample_rate;
     }
-    gcd= av_gcd(*au_scale, *au_rate);
+    gcd        = av_gcd(*au_scale, *au_rate);
     *au_scale /= gcd;
-    *au_rate /= gcd;
+    *au_rate  /= gcd;
 }
 
 void ff_riff_write_info_tag(AVIOContext *pb, const char *tag, const char *str)
@@ -639,17 +707,16 @@
     "IARL", "IART", "ICMS", "ICMT", "ICOP", "ICRD", "ICRP", "IDIM", "IDPI",
     "IENG", "IGNR", "IKEY", "ILGT", "ILNG", "IMED", "INAM", "IPLT", "IPRD",
     "IPRT", "ISBJ", "ISFT", "ISHP", "ISMP", "ISRC", "ISRF", "ITCH",
-    {0}
+    { 0 }
 };
 
 static int riff_has_valid_tags(AVFormatContext *s)
 {
     int i;
 
-    for (i = 0; *riff_tags[i]; i++) {
+    for (i = 0; *riff_tags[i]; i++)
         if (av_dict_get(s->metadata, riff_tags[i], NULL, AV_DICT_MATCH_CASE))
             return 1;
-    }
 
     return 0;
 }
@@ -669,13 +736,13 @@
 
     list_pos = ff_start_tag(pb, "LIST");
     ffio_wfourcc(pb, "INFO");
-    for (i = 0; *riff_tags[i]; i++) {
-        if ((t = av_dict_get(s->metadata, riff_tags[i], NULL, AV_DICT_MATCH_CASE)))
+    for (i = 0; *riff_tags[i]; i++)
+        if ((t = av_dict_get(s->metadata, riff_tags[i],
+                             NULL, AV_DICT_MATCH_CASE)))
             ff_riff_write_info_tag(s->pb, t->key, t->value);
-    }
     ff_end_tag(pb, list_pos);
 }
-#endif //CONFIG_MUXERS
+#endif /* CONFIG_MUXERS */
 
 #if CONFIG_DEMUXERS
 /* We could be given one of the three possible structures here:
@@ -685,52 +752,66 @@
  * WAVEFORMATEX adds 'WORD  cbSize' and basically makes itself
  * an openended structure.
  */
+
+static void parse_waveformatex(AVIOContext *pb, AVCodecContext *c)
+{
+    ff_asf_guid subformat;
+    int bps = avio_rl16(pb);
+    if (bps)
+        c->bits_per_coded_sample = bps;
+
+    c->channel_layout        = avio_rl32(pb); /* dwChannelMask */
+
+    ff_get_guid(pb, &subformat);
+    if (!memcmp(subformat + 4,
+                (const uint8_t[]){ FF_MEDIASUBTYPE_BASE_GUID }, 12)) {
+        c->codec_tag = AV_RL32(subformat);
+        c->codec_id  = ff_wav_codec_get_id(c->codec_tag,
+                                           c->bits_per_coded_sample);
+    } else {
+        c->codec_id = ff_codec_guid_get_id(ff_codec_wav_guids, subformat);
+        if (!c->codec_id)
+            av_log(c, AV_LOG_WARNING,
+                   "unknown subformat:"FF_PRI_GUID"\n",
+                   FF_ARG_GUID(subformat));
+    }
+}
+
 int ff_get_wav_header(AVIOContext *pb, AVCodecContext *codec, int size)
 {
     int id;
 
-    id = avio_rl16(pb);
-    codec->codec_type = AVMEDIA_TYPE_AUDIO;
-    codec->channels = avio_rl16(pb);
+    id                 = avio_rl16(pb);
+    codec->codec_type  = AVMEDIA_TYPE_AUDIO;
+    codec->channels    = avio_rl16(pb);
     codec->sample_rate = avio_rl32(pb);
-    codec->bit_rate = avio_rl32(pb) * 8;
+    codec->bit_rate    = avio_rl32(pb) * 8;
     codec->block_align = avio_rl16(pb);
     if (size == 14) {  /* We're dealing with plain vanilla WAVEFORMAT */
         codec->bits_per_coded_sample = 8;
-    }else
+    } else
         codec->bits_per_coded_sample = avio_rl16(pb);
     if (id == 0xFFFE) {
         codec->codec_tag = 0;
     } else {
         codec->codec_tag = id;
-        codec->codec_id = ff_wav_codec_get_id(id, codec->bits_per_coded_sample);
+        codec->codec_id  = ff_wav_codec_get_id(id,
+                                               codec->bits_per_coded_sample);
     }
     if (size >= 18) {  /* We're obviously dealing with WAVEFORMATEX */
         int cbSize = avio_rl16(pb); /* cbSize */
-        size -= 18;
+        size  -= 18;
         cbSize = FFMIN(size, cbSize);
         if (cbSize >= 22 && id == 0xfffe) { /* WAVEFORMATEXTENSIBLE */
-            ff_asf_guid subformat;
-            int bps = avio_rl16(pb);
-            if (bps)
-                codec->bits_per_coded_sample = bps;
-            codec->channel_layout = avio_rl32(pb); /* dwChannelMask */
-            ff_get_guid(pb, &subformat);
-            if (!memcmp(subformat + 4, (const uint8_t[]){FF_MEDIASUBTYPE_BASE_GUID}, 12)) {
-                codec->codec_tag = AV_RL32(subformat);
-                codec->codec_id = ff_wav_codec_get_id(codec->codec_tag, codec->bits_per_coded_sample);
-            } else {
-                codec->codec_id = ff_codec_guid_get_id(ff_codec_wav_guids, subformat);
-                if (!codec->codec_id)
-                    av_log(codec, AV_LOG_WARNING, "unknown subformat:"FF_PRI_GUID"\n", FF_ARG_GUID(subformat));
-            }
+            parse_waveformatex(pb, codec);
             cbSize -= 22;
-            size -= 22;
+            size   -= 22;
         }
         codec->extradata_size = cbSize;
         if (cbSize > 0) {
             av_free(codec->extradata);
-            codec->extradata = av_mallocz(codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
+            codec->extradata = av_mallocz(codec->extradata_size +
+                                          FF_INPUT_BUFFER_PADDING_SIZE);
             if (!codec->extradata)
                 return AVERROR(ENOMEM);
             avio_read(pb, codec->extradata, codec->extradata_size);
@@ -742,7 +823,8 @@
             avio_skip(pb, size);
     }
     if (codec->codec_id == AV_CODEC_ID_AAC_LATM) {
-        /* channels and sample_rate values are those prior to applying SBR and/or PS */
+        /* Channels and sample_rate values are those prior to applying SBR
+         * and/or PS. */
         codec->channels    = 0;
         codec->sample_rate = 0;
     }
@@ -753,7 +835,6 @@
     return 0;
 }
 
-
 enum AVCodecID ff_wav_codec_get_id(unsigned int tag, int bps)
 {
     enum AVCodecID id;
@@ -764,7 +845,7 @@
     if (id == AV_CODEC_ID_PCM_S16LE)
         id = ff_get_pcm_codec_id(bps, 0, 0, ~1);
     else if (id == AV_CODEC_ID_PCM_F32LE)
-        id = ff_get_pcm_codec_id(bps, 1, 0, 0);
+        id = ff_get_pcm_codec_id(bps, 1, 0,  0);
 
     if (id == AV_CODEC_ID_ADPCM_IMA_WAV && bps == 8)
         id = AV_CODEC_ID_PCM_ZORK;
@@ -774,13 +855,13 @@
 int ff_get_bmp_header(AVIOContext *pb, AVStream *st, unsigned *esize)
 {
     int tag1;
-    if(esize) *esize = avio_rl32(pb);
-    else               avio_rl32(pb);
-    st->codec->width = avio_rl32(pb);
+    if(esize) *esize  = avio_rl32(pb);
+    else                avio_rl32(pb);
+    st->codec->width  = avio_rl32(pb);
     st->codec->height = (int32_t)avio_rl32(pb);
     avio_rl16(pb); /* planes */
-    st->codec->bits_per_coded_sample= avio_rl16(pb); /* depth */
-    tag1 = avio_rl32(pb);
+    st->codec->bits_per_coded_sample = avio_rl16(pb); /* depth */
+    tag1                             = avio_rl32(pb);
     avio_rl32(pb); /* ImageSize */
     avio_rl32(pb); /* XPelsPerMeter */
     avio_rl32(pb); /* YPelsPerMeter */
@@ -789,35 +870,19 @@
     return tag1;
 }
 
-void ff_get_guid(AVIOContext *s, ff_asf_guid *g)
-{
-    av_assert0(sizeof(*g) == 16); //compiler will optimize this out
-    if (avio_read(s, *g, sizeof(*g)) < (int)sizeof(*g))
-        memset(*g, 0, sizeof(*g));
-}
-
-enum AVCodecID ff_codec_guid_get_id(const AVCodecGuid *guids, ff_asf_guid guid)
-{
-    int i;
-    for (i = 0; guids[i].id != AV_CODEC_ID_NONE; i++) {
-        if (!ff_guidcmp(guids[i].guid, guid))
-            return guids[i].id;
-    }
-    return AV_CODEC_ID_NONE;
-}
-
 int ff_read_riff_info(AVFormatContext *s, int64_t size)
 {
     int64_t start, end, cur;
     AVIOContext *pb = s->pb;
 
     start = avio_tell(pb);
-    end = start + size;
+    end   = start + size;
 
-    while ((cur = avio_tell(pb)) >= 0 && cur <= end - 8 /* = tag + size */) {
+    while ((cur = avio_tell(pb)) >= 0 &&
+           cur <= end - 8 /* = tag + size */) {
         uint32_t chunk_code;
         int64_t chunk_size;
-        char key[5] = {0};
+        char key[5] = { 0 };
         char *value;
 
         chunk_code = avio_rl32(pb);
@@ -827,9 +892,11 @@
                 av_log(s, AV_LOG_WARNING, "INFO subchunk truncated\n");
                 return AVERROR_INVALIDDATA;
             }
-            break;
+            return AVERROR_EOF;
         }
-        if (chunk_size > end || end - chunk_size < cur || chunk_size == UINT_MAX) {
+        if (chunk_size > end ||
+            end - chunk_size < cur ||
+            chunk_size == UINT_MAX) {
             avio_seek(pb, -9, SEEK_CUR);
             chunk_code = avio_rl32(pb);
             chunk_size = avio_rl32(pb);
@@ -844,19 +911,25 @@
         if (!chunk_code) {
             if (chunk_size)
                 avio_skip(pb, chunk_size);
+            else if (pb->eof_reached) {
+                av_log(s, AV_LOG_WARNING, "truncated file\n");
+                return AVERROR_EOF;
+            }
             continue;
         }
 
         value = av_mallocz(chunk_size + 1);
         if (!value) {
-            av_log(s, AV_LOG_ERROR, "out of memory, unable to read INFO tag\n");
+            av_log(s, AV_LOG_ERROR,
+                   "out of memory, unable to read INFO tag\n");
             return AVERROR(ENOMEM);
         }
 
         AV_WL32(key, chunk_code);
 
         if (avio_read(pb, value, chunk_size) != chunk_size) {
-            av_log(s, AV_LOG_WARNING, "premature end of file while reading INFO tag\n");
+            av_log(s, AV_LOG_WARNING,
+                   "premature end of file while reading INFO tag\n");
         }
 
         av_dict_set(&s->metadata, key, value, AV_DICT_DONT_STRDUP_VAL);
@@ -864,4 +937,4 @@
 
     return 0;
 }
-#endif // CONFIG_DEMUXERS
+#endif /* CONFIG_DEMUXERS */
diff --git a/libavformat/riff.h b/libavformat/riff.h
index 70b2f76..cb83789 100644
--- a/libavformat/riff.h
+++ b/libavformat/riff.h
@@ -56,34 +56,8 @@
 
 void ff_parse_specific_params(AVCodecContext *stream, int *au_rate, int *au_ssize, int *au_scale);
 
-typedef uint8_t ff_asf_guid[16];
-
 int ff_read_riff_info(AVFormatContext *s, int64_t size);
 
-#define FF_PRI_GUID \
-    "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
-#define FF_ARG_GUID(g) \
-    g[0],g[1],g[2],g[3],g[4],g[5],g[6],g[7],g[8],g[9],g[10],g[11],g[12],g[13],g[14],g[15]
-
-static av_always_inline int ff_guidcmp(const void *g1, const void *g2)
-{
-    return memcmp(g1, g2, sizeof(ff_asf_guid));
-}
-
-void ff_get_guid(AVIOContext *s, ff_asf_guid *g);
-
-typedef struct {
-    enum AVCodecID id;
-    ff_asf_guid guid;
-} AVCodecGuid;
-
-enum AVCodecID ff_codec_guid_get_id(const AVCodecGuid *guids, ff_asf_guid guid);
-
-extern const AVCodecGuid ff_codec_wav_guids[];
-
-#define FF_MEDIASUBTYPE_BASE_GUID \
-    0x00,0x00,0x10,0x00,0x80,0x00,0x00,0xAA,0x00,0x38,0x9B,0x71
-
 /**
  * Write all recognized RIFF tags from s->metadata
  */
@@ -94,4 +68,32 @@
  */
 void ff_riff_write_info_tag(AVIOContext *pb, const char *tag, const char *str);
 
+typedef uint8_t ff_asf_guid[16];
+
+typedef struct AVCodecGuid {
+    enum AVCodecID id;
+    ff_asf_guid guid;
+} AVCodecGuid;
+
+extern const AVCodecGuid ff_codec_wav_guids[];
+
+#define FF_PRI_GUID \
+    "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x"
+
+#define FF_ARG_GUID(g) \
+    g[0], g[1], g[2],  g[3],  g[4],  g[5],  g[6],  g[7], \
+    g[8], g[9], g[10], g[11], g[12], g[13], g[14], g[15]
+
+#define FF_MEDIASUBTYPE_BASE_GUID \
+    0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71
+
+static av_always_inline int ff_guidcmp(const void *g1, const void *g2)
+{
+    return memcmp(g1, g2, sizeof(ff_asf_guid));
+}
+
+void ff_get_guid(AVIOContext *s, ff_asf_guid *g);
+
+enum AVCodecID ff_codec_guid_get_id(const AVCodecGuid *guids, ff_asf_guid guid);
+
 #endif /* AVFORMAT_RIFF_H */
diff --git a/libavformat/rmdec.c b/libavformat/rmdec.c
index ee1e0ff..13d1d25 100644
--- a/libavformat/rmdec.c
+++ b/libavformat/rmdec.c
@@ -662,6 +662,7 @@
     int hdr;
     int seq = 0, pic_num = 0, len2 = 0, pos = 0; //init to silcense compiler warning
     int type;
+    int ret;
 
     hdr = avio_r8(pb); len--;
     type = hdr >> 6;
@@ -690,7 +691,10 @@
         pkt->data[0] = 0;
         AV_WL32(pkt->data + 1, 1);
         AV_WL32(pkt->data + 5, 0);
-        avio_read(pb, pkt->data + 9, len);
+        if ((ret = avio_read(pb, pkt->data + 9, len)) != len) {
+            av_free_packet(pkt);
+            return ret < 0 ? ret : AVERROR(EIO);
+        }
         return 0;
     }
     //now we have to deal with single slice
@@ -706,6 +710,7 @@
         av_free_packet(&vst->pkt); //FIXME this should be output.
         if(av_new_packet(&vst->pkt, vst->videobufsize) < 0)
             return AVERROR(ENOMEM);
+        memset(vst->pkt.data, 0, vst->pkt.size);
         vst->videobufpos = 8*vst->slices + 1;
         vst->cur_slice = 0;
         vst->curpic_num = pic_num;
@@ -730,6 +735,10 @@
         *pkt= vst->pkt;
         vst->pkt.data= NULL;
         vst->pkt.size= 0;
+        vst->pkt.buf = NULL;
+#if FF_API_DESTRUCT_PACKET
+        vst->pkt.destruct = NULL;
+#endif
         if(vst->slices != vst->cur_slice) //FIXME find out how to set slices correct from the begin
             memmove(pkt->data + 1 + 8*vst->cur_slice, pkt->data + 1 + 8*vst->slices,
                 vst->videobufpos - 1 - 8*vst->slices);
@@ -1000,6 +1009,18 @@
     return dts;
 }
 
+static int rm_read_seek(AVFormatContext *s, int stream_index,
+                        int64_t pts, int flags)
+{
+    RMDemuxContext *rm = s->priv_data;
+
+    if (ff_seek_frame_binary(s, stream_index, pts, flags) < 0)
+        return -1;
+    rm->audio_pkt_cnt = 0;
+    return 0;
+}
+
+
 AVInputFormat ff_rm_demuxer = {
     .name           = "rm",
     .long_name      = NULL_IF_CONFIG_SMALL("RealMedia"),
@@ -1009,6 +1030,7 @@
     .read_packet    = rm_read_packet,
     .read_close     = rm_read_close,
     .read_timestamp = rm_read_dts,
+    .read_seek      = rm_read_seek,
 };
 
 AVInputFormat ff_rdt_demuxer = {
diff --git a/libavformat/rmenc.c b/libavformat/rmenc.c
index a96c429..17192ff 100644
--- a/libavformat/rmenc.c
+++ b/libavformat/rmenc.c
@@ -374,7 +374,6 @@
     } else {
         avio_write(pb, buf, size);
     }
-    avio_flush(pb);
     stream->nb_frames++;
     av_free(buf1);
     return 0;
@@ -419,7 +418,6 @@
     avio_w8(pb, stream->nb_frames & 0xff);
 
     avio_write(pb, buf, size);
-    avio_flush(pb);
 
     stream->nb_frames++;
     return 0;
diff --git a/libavformat/rpl.c b/libavformat/rpl.c
index dcc7950..0bc4b97 100644
--- a/libavformat/rpl.c
+++ b/libavformat/rpl.c
@@ -255,7 +255,7 @@
     for (i = 0; !error && i < number_of_chunks; i++) {
         int64_t offset, video_size, audio_size;
         error |= read_line(pb, line, sizeof(line));
-        if (3 != sscanf(line, "%"PRId64" , %"PRId64" ; %"PRId64,
+        if (3 != sscanf(line, "%"SCNd64" , %"SCNd64" ; %"SCNd64,
                         &offset, &video_size, &audio_size))
             error = -1;
         av_add_index_entry(vst, offset, i * rpl->frames_per_chunk,
diff --git a/libavformat/rsodec.c b/libavformat/rsodec.c
index aae80a8..6e3ea0a 100644
--- a/libavformat/rsodec.c
+++ b/libavformat/rsodec.c
@@ -43,13 +43,13 @@
     codec = ff_codec_get_id(ff_codec_rso_tags, id);
 
     if (codec == AV_CODEC_ID_ADPCM_IMA_WAV) {
-        av_log(s, AV_LOG_ERROR, "ADPCM in RSO not implemented\n");
+        avpriv_report_missing_feature(s, "ADPCM in RSO");
         return AVERROR_PATCHWELCOME;
     }
 
     bps = av_get_bits_per_sample(codec);
     if (!bps) {
-        av_log_ask_for_sample(s, "could not determine bits per sample\n");
+        avpriv_request_sample(s, "Unknown bits per sample");
         return AVERROR_PATCHWELCOME;
     }
 
diff --git a/libavformat/rtmphttp.c b/libavformat/rtmphttp.c
index d21ac67..3a51f7c 100644
--- a/libavformat/rtmphttp.c
+++ b/libavformat/rtmphttp.c
@@ -236,7 +236,7 @@
             goto fail;
         }
     }
-    while (off > 0 && isspace(rt->client_id[off - 1]))
+    while (off > 0 && av_isspace(rt->client_id[off - 1]))
         off--;
     rt->client_id[off] = '\0';
 
diff --git a/libavformat/rtmpproto.c b/libavformat/rtmpproto.c
index 598122a..d73e015 100644
--- a/libavformat/rtmpproto.c
+++ b/libavformat/rtmpproto.c
@@ -1690,7 +1690,7 @@
     }
 
     if (!strcmp(authmod, "adobe")) {
-        if ((ret = do_adobe_auth(rt, user, salt, challenge, opaque)) < 0)
+        if ((ret = do_adobe_auth(rt, user, salt, opaque, challenge)) < 0)
             return ret;
     } else {
         if ((ret = do_llnw_auth(rt, user, nonce)) < 0)
diff --git a/libavformat/rtpdec.c b/libavformat/rtpdec.c
index a1f1701..b512b89 100644
--- a/libavformat/rtpdec.c
+++ b/libavformat/rtpdec.c
@@ -424,7 +424,7 @@
     AVIOContext *pb;
     uint8_t *buf;
     int64_t now;
-    uint16_t first_missing, missing_mask;
+    uint16_t first_missing = 0, missing_mask = 0;
 
     if (!fd && !avio)
         return -1;
@@ -864,11 +864,15 @@
 
 int ff_rtp_finalize_packet(AVPacket *pkt, AVIOContext **dyn_buf, int stream_idx)
 {
+    int ret;
     av_init_packet(pkt);
 
     pkt->size         = avio_close_dyn_buf(*dyn_buf, &pkt->data);
     pkt->stream_index = stream_idx;
-    pkt->destruct     = av_destruct_packet;
-    *dyn_buf          = NULL;
+    *dyn_buf = NULL;
+    if ((ret = av_packet_from_data(pkt, pkt->data, pkt->size)) < 0) {
+        av_freep(&pkt->data);
+        return ret;
+    }
     return pkt->size;
 }
diff --git a/libavformat/rtpdec.h b/libavformat/rtpdec.h
index e469c0f..623d911 100644
--- a/libavformat/rtpdec.h
+++ b/libavformat/rtpdec.h
@@ -33,7 +33,7 @@
 typedef struct RTPDynamicProtocolHandler RTPDynamicProtocolHandler;
 
 #define RTP_MIN_PACKET_LENGTH 12
-#define RTP_MAX_PACKET_LENGTH 1500
+#define RTP_MAX_PACKET_LENGTH 8192
 
 #define RTP_REORDER_QUEUE_DEFAULT_SIZE 10
 
diff --git a/libavformat/rtpdec_latm.c b/libavformat/rtpdec_latm.c
index bacc186..a2ad071 100644
--- a/libavformat/rtpdec_latm.c
+++ b/libavformat/rtpdec_latm.c
@@ -157,8 +157,8 @@
     } else if (!strcmp(attr, "cpresent")) {
         int cpresent = atoi(value);
         if (cpresent != 0)
-            av_log_missing_feature(NULL, "RTP MP4A-LATM with in-band "
-                                         "configuration", 1);
+            avpriv_request_sample(NULL,
+                                  "RTP MP4A-LATM with in-band configuration");
     }
 
     return 0;
diff --git a/libavformat/rtpdec_qt.c b/libavformat/rtpdec_qt.c
index 9631b0a..8e887a5 100644
--- a/libavformat/rtpdec_qt.c
+++ b/libavformat/rtpdec_qt.c
@@ -97,8 +97,8 @@
         is_start  = get_bits1(&gb);
         is_finish = get_bits1(&gb);
         if (!is_start || !is_finish) {
-            av_log_missing_feature(s, "RTP-X-QT with payload description "
-                                      "split over several packets", 1);
+            avpriv_request_sample(s, "RTP-X-QT with payload description "
+                                  "split over several packets");
             return AVERROR_PATCHWELCOME;
         }
         skip_bits(&gb, 12); // reserved
@@ -161,7 +161,7 @@
         avio_seek(&pb, 4, SEEK_SET);
 
     if (has_packet_info) {
-        av_log_missing_feature(s, "RTP-X-QT with packet specific info", 1);
+        avpriv_request_sample(s, "RTP-X-QT with packet-specific info");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -186,12 +186,14 @@
         memcpy(qt->pkt.data + qt->pkt.size, buf + avio_tell(&pb), alen);
         qt->pkt.size += alen;
         if (has_marker_bit) {
-            *pkt = qt->pkt;
+            int ret = av_packet_from_data(pkt, qt->pkt.data, qt->pkt.size);
+            if (ret < 0)
+                return ret;
+
             qt->pkt.size = 0;
             qt->pkt.data = NULL;
             pkt->flags        = flags & RTP_FLAG_KEY ? AV_PKT_FLAG_KEY : 0;
             pkt->stream_index = st->index;
-            pkt->destruct     = av_destruct_packet;
             memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
             return 0;
         }
@@ -224,7 +226,7 @@
         return 0;
 
     default:  /* unimplemented */
-        av_log_missing_feature(NULL, "RTP-X-QT with packing scheme 2", 1);
+        avpriv_request_sample(NULL, "RTP-X-QT with packing scheme 2");
         return AVERROR_PATCHWELCOME;
     }
 }
diff --git a/libavformat/rtpdec_vp8.c b/libavformat/rtpdec_vp8.c
index 623d359..b4b4f22 100644
--- a/libavformat/rtpdec_vp8.c
+++ b/libavformat/rtpdec_vp8.c
@@ -82,7 +82,7 @@
         keyidx_present = 0;
     int pictureid = -1, pictureid_mask = 0;
     int returned_old_frame = 0;
-    uint32_t old_timestamp;
+    uint32_t old_timestamp = 0;
 
     if (!buf) {
         if (vp8->data) {
diff --git a/libavformat/rtpenc_mpv.c b/libavformat/rtpenc_mpv.c
index 2708dd1..4b45f51 100644
--- a/libavformat/rtpenc_mpv.c
+++ b/libavformat/rtpenc_mpv.c
@@ -20,7 +20,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "libavcodec/mpegvideo.h"
+#include "libavcodec/internal.h"
 #include "avformat.h"
 #include "rtpenc.h"
 
@@ -56,7 +56,7 @@
             r1 = buf1;
             while (1) {
                 start_code = -1;
-                r = avpriv_mpv_find_start_code(r1, end, &start_code);
+                r = avpriv_find_start_code(r1, end, &start_code);
                 if((start_code & 0xFFFFFF00) == 0x100) {
                     /* New start code found */
                     if (start_code == 0x100) {
diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
index 317893c..dc94f0f 100644
--- a/libavformat/rtsp.c
+++ b/libavformat/rtsp.c
@@ -91,6 +91,7 @@
     { "min_port", "Minimum local UDP port", OFFSET(rtp_port_min), AV_OPT_TYPE_INT, {.i64 = RTSP_RTP_PORT_MIN}, 0, 65535, DEC|ENC },
     { "max_port", "Maximum local UDP port", OFFSET(rtp_port_max), AV_OPT_TYPE_INT, {.i64 = RTSP_RTP_PORT_MAX}, 0, 65535, DEC|ENC },
     { "timeout", "Maximum timeout (in seconds) to wait for incoming connections. -1 is infinite. Implies flag listen", OFFSET(initial_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, INT_MIN, INT_MAX, DEC },
+    { "stimeout", "timeout (in micro seconds) of socket i/o operations.", OFFSET(stimeout), AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX, DEC },
     RTSP_REORDERING_OPTS(),
     { NULL },
 };
@@ -1618,7 +1619,8 @@
         }
     } else {
         /* open the tcp connection */
-        ff_url_join(tcpname, sizeof(tcpname), "tcp", NULL, host, port, NULL);
+        ff_url_join(tcpname, sizeof(tcpname), "tcp", NULL, host, port,
+                    "?timeout=%d", rt->stimeout);
         if (ffurl_open(&rt->rtsp_hd, tcpname, AVIO_FLAG_READ_WRITE,
                        &s->interrupt_callback, NULL) < 0) {
             err = AVERROR(EIO);
@@ -2134,7 +2136,7 @@
 
 static int rtp_read_header(AVFormatContext *s)
 {
-    uint8_t recvbuf[1500];
+    uint8_t recvbuf[RTP_MAX_PACKET_LENGTH];
     char host[500], sdp[500];
     int ret, port;
     URLContext* in = NULL;
diff --git a/libavformat/rtsp.h b/libavformat/rtsp.h
index 321cd7a..4af3507 100644
--- a/libavformat/rtsp.h
+++ b/libavformat/rtsp.h
@@ -391,6 +391,11 @@
     int initial_timeout;
 
     /**
+     * timeout of socket i/o operations.
+     */
+    int stimeout;
+
+    /**
      * Size of RTP packet reordering queue.
      */
     int reordering_queue_size;
diff --git a/libavformat/sapdec.c b/libavformat/sapdec.c
index ed3e857..fe7bd82 100644
--- a/libavformat/sapdec.c
+++ b/libavformat/sapdec.c
@@ -27,6 +27,7 @@
 #include "internal.h"
 #include "avio_internal.h"
 #include "url.h"
+#include "rtpdec.h"
 #if HAVE_POLL_H
 #include <poll.h>
 #endif
@@ -63,7 +64,7 @@
 {
     struct SAPState *sap = s->priv_data;
     char host[1024], path[1024], url[1024];
-    uint8_t recvbuf[1500];
+    uint8_t recvbuf[RTP_MAX_PACKET_LENGTH];
     int port;
     int ret, i;
     AVInputFormat* infmt;
@@ -186,7 +187,7 @@
     int fd = ffurl_get_file_handle(sap->ann_fd);
     int n, ret;
     struct pollfd p = {fd, POLLIN, 0};
-    uint8_t recvbuf[1500];
+    uint8_t recvbuf[RTP_MAX_PACKET_LENGTH];
 
     if (sap->eof)
         return AVERROR_EOF;
diff --git a/libavformat/segment.c b/libavformat/segment.c
index 13d8c43..dbcfe89 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -53,6 +53,7 @@
     LIST_TYPE_CSV,
     LIST_TYPE_M3U8,
     LIST_TYPE_EXT, ///< deprecated
+    LIST_TYPE_FFCONCAT,
     LIST_TYPE_NB,
 } ListType;
 
@@ -128,6 +129,7 @@
 
     oc->oformat            = seg->oformat;
     oc->interrupt_callback = s->interrupt_callback;
+    av_dict_copy(&oc->metadata, s->metadata, 0);
 
     for (i = 0; i < s->nb_streams; i++) {
         AVStream *st;
@@ -225,6 +227,8 @@
         for (entry = seg->segment_list_entries; entry; entry = entry->next)
             max_duration = FFMAX(max_duration, entry->end_time - entry->start_time);
         avio_printf(seg->list_pb, "#EXT-X-TARGETDURATION:%"PRId64"\n", (int64_t)ceil(max_duration));
+    } else if (seg->list_type == LIST_TYPE_FFCONCAT) {
+        avio_printf(seg->list_pb, "ffconcat version 1.0\n");
     }
 
     return ret;
@@ -232,7 +236,8 @@
 
 static void segment_list_print_entry(AVIOContext      *list_ioctx,
                                      ListType          list_type,
-                                     const SegmentListEntry *list_entry)
+                                     const SegmentListEntry *list_entry,
+                                     void *log_ctx)
 {
     switch (list_type) {
     case LIST_TYPE_FLAT:
@@ -247,6 +252,18 @@
         avio_printf(list_ioctx, "#EXTINF:%f,\n%s\n",
                     list_entry->end_time - list_entry->start_time, list_entry->filename);
         break;
+    case LIST_TYPE_FFCONCAT:
+    {
+        char *buf;
+        if (av_escape(&buf, list_entry->filename, NULL, AV_ESCAPE_MODE_AUTO, AV_ESCAPE_FLAG_WHITESPACE) < 0) {
+            av_log(log_ctx, AV_LOG_WARNING,
+                   "Error writing list entry '%s' in list file\n", list_entry->filename);
+            return;
+        }
+        avio_printf(list_ioctx, "file %s\n", buf);
+        av_free(buf);
+        break;
+    }
     default:
         av_assert0(!"Invalid list type");
     }
@@ -293,11 +310,11 @@
             if ((ret = segment_list_open(s)) < 0)
                 goto end;
             for (entry = seg->segment_list_entries; entry; entry = entry->next)
-                segment_list_print_entry(seg->list_pb, seg->list_type, entry);
+                segment_list_print_entry(seg->list_pb, seg->list_type, entry, s);
             if (seg->list_type == LIST_TYPE_M3U8 && is_last)
                 avio_printf(seg->list_pb, "#EXT-X-ENDLIST\n");
         } else {
-            segment_list_print_entry(seg->list_pb, seg->list_type, &seg->cur_entry);
+            segment_list_print_entry(seg->list_pb, seg->list_type, &seg->cur_entry, s);
         }
         avio_flush(seg->list_pb);
     }
@@ -551,6 +568,7 @@
             if      (av_match_ext(seg->list, "csv" )) seg->list_type = LIST_TYPE_CSV;
             else if (av_match_ext(seg->list, "ext" )) seg->list_type = LIST_TYPE_EXT;
             else if (av_match_ext(seg->list, "m3u8")) seg->list_type = LIST_TYPE_M3U8;
+            else if (av_match_ext(seg->list, "ffcat,ffconcat")) seg->list_type = LIST_TYPE_FFCONCAT;
             else                                      seg->list_type = LIST_TYPE_FLAT;
         }
         if ((ret = segment_list_open(s)) < 0)
@@ -761,6 +779,7 @@
     { "flat", "flat format",     0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_FLAT }, INT_MIN, INT_MAX, E, "list_type" },
     { "csv",  "csv format",      0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_CSV  }, INT_MIN, INT_MAX, E, "list_type" },
     { "ext",  "extended format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_EXT  }, INT_MIN, INT_MAX, E, "list_type" },
+    { "ffconcat", "ffconcat format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_FFCONCAT }, INT_MIN, INT_MAX, E, "list_type" },
     { "m3u8", "M3U8 format",     0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_M3U8 }, INT_MIN, INT_MAX, E, "list_type" },
     { "hls", "Apple HTTP Live Streaming compatible", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_M3U8 }, INT_MIN, INT_MAX, E, "list_type" },
 
diff --git a/libavformat/smacker.c b/libavformat/smacker.c
index 883a2b7..84481e2 100644
--- a/libavformat/smacker.c
+++ b/libavformat/smacker.c
@@ -276,7 +276,7 @@
                 } else if(t & 0x40){ /* copy with offset */
                     off = avio_r8(s->pb);
                     j = (t & 0x3F) + 1;
-                    if (off + j > 0xff) {
+                    if (off + j - 1 > 0xff) {
                         av_log(s, AV_LOG_ERROR,
                                "Invalid palette update, offset=%d length=%d extends beyond palette size\n",
                                off, j);
diff --git a/libavformat/smjpegdec.c b/libavformat/smjpegdec.c
index f32f635..38ac2fb 100644
--- a/libavformat/smjpegdec.c
+++ b/libavformat/smjpegdec.c
@@ -52,7 +52,7 @@
     avio_skip(pb, 8); // magic
     version = avio_rb32(pb);
     if (version)
-        av_log_ask_for_sample(s, "unknown version %d\n", version);
+        avpriv_request_sample(s, "Unknown version %d", version);
 
     duration = avio_rb32(pb); // in msec
 
@@ -77,7 +77,7 @@
             break;
         case SMJPEG_SND:
             if (ast) {
-                av_log_ask_for_sample(s, "multiple audio streams not supported\n");
+                avpriv_request_sample(s, "Multiple audio streams");
                 return AVERROR_PATCHWELCOME;
             }
             hlength = avio_rb32(pb);
@@ -100,7 +100,7 @@
             break;
         case SMJPEG_VID:
             if (vst) {
-                av_log_ask_for_sample(s, "multiple video streams not supported\n");
+                avpriv_request_sample(s, "Multiple video streams");
                 return AVERROR_INVALIDDATA;
             }
             hlength = avio_rb32(pb);
diff --git a/libavformat/smjpegenc.c b/libavformat/smjpegenc.c
index 0a27687..430a497 100644
--- a/libavformat/smjpegenc.c
+++ b/libavformat/smjpegenc.c
@@ -109,7 +109,6 @@
     avio_wb32(pb, pkt->pts);
     avio_wb32(pb, pkt->size);
     avio_write(pb, pkt->data, pkt->size);
-    avio_flush(pb);
 
     smc->duration = FFMAX(smc->duration, pkt->pts + pkt->duration);
     return 0;
diff --git a/libavformat/spdifdec.c b/libavformat/spdifdec.c
index e1329cc..726a85c 100644
--- a/libavformat/spdifdec.c
+++ b/libavformat/spdifdec.c
@@ -91,8 +91,8 @@
         break;
     default:
         if (s) { /* be silent during a probe */
-            av_log(s, AV_LOG_WARNING, "Data type 0x%04x", data_type);
-            av_log_missing_feature(s, " in IEC 61937", 1);
+            avpriv_request_sample(s, "Data type 0x%04x in IEC 61937",
+                                  data_type);
         }
         return AVERROR_PATCHWELCOME;
     }
@@ -184,7 +184,7 @@
     pkt_size_bits = avio_rl16(pb);
 
     if (pkt_size_bits % 16)
-        av_log_ask_for_sample(s, "Packet does not end to a 16-bit boundary.");
+        avpriv_request_sample(s, "Packet not ending at a 16-bit boundary");
 
     ret = av_new_packet(pkt, FFALIGN(pkt_size_bits, 16) >> 3);
     if (ret)
@@ -218,7 +218,7 @@
         st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
         st->codec->codec_id = codec_id;
     } else if (codec_id != s->streams[0]->codec->codec_id) {
-        av_log_missing_feature(s, "Codec change in IEC 61937", 0);
+        avpriv_report_missing_feature(s, "Codec change in IEC 61937");
         return AVERROR_PATCHWELCOME;
     }
 
diff --git a/libavformat/spdifenc.c b/libavformat/spdifenc.c
index aaf0568..857c20d 100644
--- a/libavformat/spdifenc.c
+++ b/libavformat/spdifenc.c
@@ -307,7 +307,7 @@
          * discs and dts-in-wav. */
         ctx->use_preamble = 0;
     } else if (ctx->out_bytes > ctx->pkt_offset - BURST_HEADER_SIZE) {
-        av_log_ask_for_sample(s, "Unrecognized large DTS frame.");
+        avpriv_request_sample(s, "Unrecognized large DTS frame");
         /* This will fail with a "bitrate too high" in the caller */
     }
 
@@ -412,8 +412,8 @@
     if (pkt->size > TRUEHD_FRAME_OFFSET - mat_code_length) {
         /* if such frames exist, we'd need some more complex logic to
          * distribute the TrueHD frames in the MAT frame */
-        av_log(s, AV_LOG_ERROR, "TrueHD frame too big, %d bytes\n", pkt->size);
-        av_log_ask_for_sample(s, NULL);
+        avpriv_request_sample(s, "Too large TrueHD frame of %d bytes",
+                              pkt->size);
         return AVERROR_PATCHWELCOME;
     }
 
@@ -538,7 +538,6 @@
     av_log(s, AV_LOG_DEBUG, "type=%x len=%i pkt_offset=%i\n",
            ctx->data_type, ctx->out_bytes, ctx->pkt_offset);
 
-    avio_flush(s->pb);
     return 0;
 }
 
diff --git a/libavformat/srtenc.c b/libavformat/srtenc.c
index e02c4ef..b43504b 100644
--- a/libavformat/srtenc.c
+++ b/libavformat/srtenc.c
@@ -98,7 +98,6 @@
     avio_write(avf->pb, pkt->data, pkt->size);
     if (write_ts)
         avio_write(avf->pb, "\n\n", 2);
-    avio_flush(avf->pb);
     srt->index++;
     return 0;
 }
diff --git a/libavformat/srtp.c b/libavformat/srtp.c
index 65309d0..b6e8211 100644
--- a/libavformat/srtp.c
+++ b/libavformat/srtp.c
@@ -25,6 +25,7 @@
 #include "libavutil/intreadwrite.h"
 #include "libavutil/log.h"
 #include "rtp.h"
+#include "rtpdec.h"
 #include "srtp.h"
 
 void ff_srtp_free(struct SRTPContext *s)
@@ -419,7 +420,7 @@
 {
     struct SRTPContext enc = { 0 }, dec = { 0 };
     int len;
-    char buf[1500];
+    char buf[RTP_MAX_PACKET_LENGTH];
     ff_srtp_set_crypto(&enc, suite, key);
     ff_srtp_set_crypto(&dec, suite, key);
     len = ff_srtp_encrypt(&enc, data, in_len, buf, sizeof(buf));
@@ -441,7 +442,7 @@
     static const char *aes128_32_suite = "AES_CM_128_HMAC_SHA1_32";
     static const char *aes128_80_32_suite = "SRTP_AES128_CM_HMAC_SHA1_32";
     static const char *test_key = "abcdefghijklmnopqrstuvwxyz1234567890ABCD";
-    uint8_t buf[1500];
+    uint8_t buf[RTP_MAX_PACKET_LENGTH];
     struct SRTPContext srtp = { 0 };
     int len;
     ff_srtp_set_crypto(&srtp, aes128_80_suite, aes128_80_key);
diff --git a/libavformat/srtpproto.c b/libavformat/srtpproto.c
index f5e3d14..f9d6b17 100644
--- a/libavformat/srtpproto.c
+++ b/libavformat/srtpproto.c
@@ -25,6 +25,7 @@
 #include "url.h"
 
 #include "internal.h"
+#include "rtpdec.h"
 #include "srtp.h"
 
 typedef struct SRTPProtoContext {
@@ -33,7 +34,7 @@
     const char *out_suite, *out_params;
     const char *in_suite, *in_params;
     struct SRTPContext srtp_out, srtp_in;
-    uint8_t encryptbuf[1500];
+    uint8_t encryptbuf[RTP_MAX_PACKET_LENGTH];
 } SRTPProtoContext;
 
 #define D AV_OPT_FLAG_DECODING_PARAM
diff --git a/libavformat/subtitles.c b/libavformat/subtitles.c
index 5462616..2af0450 100644
--- a/libavformat/subtitles.c
+++ b/libavformat/subtitles.c
@@ -20,6 +20,7 @@
 
 #include "avformat.h"
 #include "subtitles.h"
+#include "libavutil/avassert.h"
 #include "libavutil/avstring.h"
 
 AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
@@ -49,7 +50,6 @@
         sub = &subs[q->nb_subs++];
         if (av_new_packet(sub, len) < 0)
             return NULL;
-        sub->destruct = NULL;
         sub->flags |= AV_PKT_FLAG_KEY;
         sub->pts = sub->dts = 0;
         memcpy(sub->data, event, len);
@@ -85,7 +85,8 @@
 
     if (q->current_sub_idx == q->nb_subs)
         return AVERROR_EOF;
-    *pkt = *sub;
+    av_copy_packet(pkt, sub);
+
     pkt->dts = pkt->pts;
     q->current_sub_idx++;
     return 0;
@@ -135,7 +136,7 @@
     int i;
 
     for (i = 0; i < q->nb_subs; i++)
-        av_destruct_packet(&q->subs[i]);
+        av_free_packet(&q->subs[i]);
     av_freep(&q->subs);
     q->nb_subs = q->allocated_size = q->current_sub_idx = 0;
 }
@@ -170,12 +171,12 @@
 
     while (*s) {
         while (*s) {
-            if (!in_quotes && isspace(*s))
+            if (!in_quotes && av_isspace(*s))
                 break;
             in_quotes ^= *s == '"'; // XXX: support escaping?
             s++;
         }
-        while (isspace(*s))
+        while (av_isspace(*s))
             s++;
         if (!av_strncasecmp(s, attr, len) && s[len] == '=')
             return s + len + 1 + (s[len + 1] == '"');
diff --git a/libavformat/swfenc.c b/libavformat/swfenc.c
index 5b7fd1f..7018362 100644
--- a/libavformat/swfenc.c
+++ b/libavformat/swfenc.c
@@ -441,8 +441,6 @@
     put_swf_tag(s, TAG_SHOWFRAME);
     put_swf_end_tag(s);
 
-    avio_flush(s->pb);
-
     return 0;
 }
 
diff --git a/libavformat/tcp.c b/libavformat/tcp.c
index 8b35fb1..0d792e7 100644
--- a/libavformat/tcp.c
+++ b/libavformat/tcp.c
@@ -21,6 +21,7 @@
 #include "avformat.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/opt.h"
+#include "libavutil/time.h"
 #include "internal.h"
 #include "network.h"
 #include "os_support.h"
diff --git a/libavformat/tedcaptionsdec.c b/libavformat/tedcaptionsdec.c
index 85bed0a..048ba96 100644
--- a/libavformat/tedcaptionsdec.c
+++ b/libavformat/tedcaptionsdec.c
@@ -295,7 +295,7 @@
     if (!st)
         return AVERROR(ENOMEM);
     st->codec->codec_type     = AVMEDIA_TYPE_SUBTITLE;
-    st->codec->codec_id       = CODEC_ID_TEXT;
+    st->codec->codec_id       = AV_CODEC_ID_TEXT;
     avpriv_set_pts_info(st, 64, 1, 1000);
     st->probe_packets = 0;
     st->start_time    = 0;
diff --git a/libavformat/tee.c b/libavformat/tee.c
index f41327b..10787ca 100644
--- a/libavformat/tee.c
+++ b/libavformat/tee.c
@@ -100,7 +100,7 @@
         av_dict_set(&options, "f", NULL, 0);
     }
 
-    avformat_alloc_output_context2(&avf2, NULL, format, filename);
+    ret = avformat_alloc_output_context2(&avf2, NULL, format, filename);
     if (ret < 0)
         goto fail;
     av_free(format);
diff --git a/libavformat/tty.c b/libavformat/tty.c
index 4c62b5b..3afb9b2 100644
--- a/libavformat/tty.c
+++ b/libavformat/tty.c
@@ -38,8 +38,8 @@
     AVClass *class;
     int chars_per_frame;
     uint64_t fsize;  /**< file size less metadata buffer */
-    char *video_size;/**< A string describing video size, set by a private option. */
-    char *framerate; /**< Set by a private option. */
+    int width, height; /**< Set by a private option. */
+    AVRational framerate; /**< Set by a private option. */
 } TtyDemuxContext;
 
 /**
@@ -75,9 +75,8 @@
 static int read_header(AVFormatContext *avctx)
 {
     TtyDemuxContext *s = avctx->priv_data;
-    int width = 0, height = 0, ret = 0;
+    int ret = 0;
     AVStream *st = avformat_new_stream(avctx, NULL);
-    AVRational framerate;
 
     if (!st) {
         ret = AVERROR(ENOMEM);
@@ -87,17 +86,10 @@
     st->codec->codec_type  = AVMEDIA_TYPE_VIDEO;
     st->codec->codec_id    = AV_CODEC_ID_ANSI;
 
-    if (s->video_size && (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
-        av_log (avctx, AV_LOG_ERROR, "Couldn't parse video size.\n");
-        goto fail;
-    }
-    if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
-        av_log(avctx, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s->framerate);
-        goto fail;
-    }
-    st->codec->width  = width;
-    st->codec->height = height;
-    avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
+    st->codec->width  = s->width;
+    st->codec->height = s->height;
+    avpriv_set_pts_info(st, 60, s->framerate.den, s->framerate.num);
+    st->avg_frame_rate = s->framerate;
 
     /* simulate tty display speed */
     s->chars_per_frame = FFMAX(av_q2d(st->time_base)*s->chars_per_frame, 1);
@@ -145,8 +137,8 @@
 #define DEC AV_OPT_FLAG_DECODING_PARAM
 static const AVOption options[] = {
     { "chars_per_frame", "", offsetof(TtyDemuxContext, chars_per_frame), AV_OPT_TYPE_INT, {.i64 = 6000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
-    { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
-    { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
+    { "video_size", "A string describing frame size, such as 640x480 or hd720.", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
+    { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC },
     { NULL },
 };
 
diff --git a/libavformat/udp.c b/libavformat/udp.c
index 184ab81..e8a01db 100644
--- a/libavformat/udp.c
+++ b/libavformat/udp.c
@@ -34,6 +34,7 @@
 #include "libavutil/avstring.h"
 #include "libavutil/opt.h"
 #include "libavutil/log.h"
+#include "libavutil/time.h"
 #include "internal.h"
 #include "network.h"
 #include "os_support.h"
diff --git a/libavformat/urldecode.c b/libavformat/urldecode.c
index b100903..283d912 100644
--- a/libavformat/urldecode.c
+++ b/libavformat/urldecode.c
@@ -26,7 +26,6 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include <ctype.h>
 #include <string.h>
 
 #include "libavutil/mem.h"
@@ -54,7 +53,7 @@
         if (c == '%' && s + 2 < url_len) {
             char c2 = url[s++];
             char c3 = url[s++];
-            if (isxdigit(c2) && isxdigit(c3)) {
+            if (av_isxdigit(c2) && av_isxdigit(c3)) {
                 c2 = av_tolower(c2);
                 c3 = av_tolower(c3);
 
diff --git a/libavformat/utils.c b/libavformat/utils.c
index d23f53e..c1f1362 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -99,6 +99,12 @@
     return timestamp;
 }
 
+#define MAKE_ACCESSORS(str, name, type, field) \
+    type av_##name##_get_##field(const str *s) { return s->field; } \
+    void av_##name##_set_##field(str *s, type v) { s->field = v; }
+
+MAKE_ACCESSORS(AVStream, stream, AVRational, r_frame_rate)
+
 /** head of registered input format linked list */
 static AVInputFormat *first_iformat = NULL;
 /** head of registered output format linked list */
@@ -216,6 +222,10 @@
 
 enum AVCodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
                             const char *filename, const char *mime_type, enum AVMediaType type){
+    if (!strcmp(fmt->name, "segment") || !strcmp(fmt->name, "ssegment")) {
+        fmt = av_guess_format(NULL, filename, NULL);
+    }
+
     if(type == AVMEDIA_TYPE_VIDEO){
         enum AVCodecID codec_id= AV_CODEC_ID_NONE;
 
@@ -245,6 +255,9 @@
     return NULL;
 }
 
+/* an arbitrarily chosen "sane" max packet size -- 50M */
+#define SANE_CHUNK_SIZE (50000000)
+
 int ffio_limit(AVIOContext *s, int size)
 {
     if(s->maxsize>=0){
@@ -265,43 +278,68 @@
     return size;
 }
 
-int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
+/*
+ * Read the data in sane-sized chunks and append to pkt.
+ * Return the number of bytes read or an error.
+ */
+static int append_packet_chunked(AVIOContext *s, AVPacket *pkt, int size)
 {
+    int64_t orig_pos   = pkt->pos; // av_grow_packet might reset pos
+    int orig_size      = pkt->size;
     int ret;
-    int orig_size = size;
-    size= ffio_limit(s, size);
 
-    ret= av_new_packet(pkt, size);
+    do {
+        int prev_size = pkt->size;
+        int read_size;
 
-    if(ret<0)
-        return ret;
+        /*
+         * When the caller requests a lot of data, limit it to the amount left
+         * in file or SANE_CHUNK_SIZE when it is not known
+         */
+        read_size = size;
+        if (read_size > SANE_CHUNK_SIZE/10) {
+            read_size = ffio_limit(s, read_size);
+            // If filesize/maxsize is unknown, limit to SANE_CHUNK_SIZE
+            if (s->maxsize < 0)
+                read_size = FFMIN(read_size, SANE_CHUNK_SIZE);
+        }
 
-    pkt->pos= avio_tell(s);
+        ret = av_grow_packet(pkt, read_size);
+        if (ret < 0)
+            break;
 
-    ret= avio_read(s, pkt->data, size);
-    if(ret<=0)
-        av_free_packet(pkt);
-    else
-        av_shrink_packet(pkt, ret);
-    if (pkt->size < orig_size)
+        ret = avio_read(s, pkt->data + prev_size, read_size);
+        if (ret != read_size) {
+            av_shrink_packet(pkt, prev_size + FFMAX(ret, 0));
+            break;
+        }
+
+        size -= read_size;
+    } while (size > 0);
+    if (size > 0)
         pkt->flags |= AV_PKT_FLAG_CORRUPT;
 
-    return ret;
+    pkt->pos = orig_pos;
+    if (!pkt->size)
+        av_free_packet(pkt);
+    return pkt->size > orig_size ? pkt->size - orig_size : ret;
+}
+
+int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
+{
+    av_init_packet(pkt);
+    pkt->data = NULL;
+    pkt->size = 0;
+    pkt->pos  = avio_tell(s);
+
+    return append_packet_chunked(s, pkt, size);
 }
 
 int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
 {
-    int ret;
-    int old_size;
     if (!pkt->size)
         return av_get_packet(s, pkt, size);
-    old_size = pkt->size;
-    ret = av_grow_packet(pkt, size);
-    if (ret < 0)
-        return ret;
-    ret = avio_read(s, pkt->data + old_size, size);
-    av_shrink_packet(pkt, old_size + FFMAX(ret, 0));
-    return ret;
+    return append_packet_chunked(s, pkt, size);
 }
 
 
@@ -350,6 +388,8 @@
         }else if (score == score_max)
             fmt = NULL;
     }
+    if(nodat)
+        score_max = FFMIN(AVPROBE_SCORE_MAX/4-1, score_max);
     *score_ret= score_max;
 
     return fmt;
@@ -555,16 +595,20 @@
     return &pktl->pkt;
 }
 
-void avformat_queue_attached_pictures(AVFormatContext *s)
+int avformat_queue_attached_pictures(AVFormatContext *s)
 {
     int i;
     for (i = 0; i < s->nb_streams; i++)
         if (s->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC &&
             s->streams[i]->discard < AVDISCARD_ALL) {
             AVPacket copy = s->streams[i]->attached_pic;
-            copy.destruct = NULL;
+            copy.buf      = av_buffer_ref(copy.buf);
+            if (!copy.buf)
+                return AVERROR(ENOMEM);
+
             add_to_pktbuf(&s->raw_packet_buffer, &copy, &s->raw_packet_buffer_end);
         }
+    return 0;
 }
 
 int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
@@ -627,7 +671,7 @@
             goto fail;
 
     if (id3v2_extra_meta) {
-        if (!strcmp(s->iformat->name, "mp3")) {
+        if (!strcmp(s->iformat->name, "mp3") || !strcmp(s->iformat->name, "aac")) {
             if((ret = ff_id3v2_parse_apic(s, &id3v2_extra_meta)) < 0)
                 goto fail;
         } else
@@ -635,7 +679,8 @@
     }
     ff_id3v2_free_extra_meta(&id3v2_extra_meta);
 
-    avformat_queue_attached_pictures(s);
+    if ((ret = avformat_queue_attached_pictures(s)) < 0)
+        goto fail;
 
     if (!(s->flags&AVFMT_FLAG_PRIV_OPT) && s->pb && !s->data_offset)
         s->data_offset = avio_tell(s->pb);
@@ -732,6 +777,8 @@
         if (pktl) {
             *pkt = pktl->pkt;
             st = s->streams[pkt->stream_index];
+            if (s->raw_packet_buffer_remaining_size <= 0)
+                probe_codec(s, st, NULL);
             if(st->request_probe <= 0){
                 s->raw_packet_buffer = pktl->next;
                 s->raw_packet_buffer_remaining_size += pkt->size;
@@ -1333,8 +1380,12 @@
         compute_pkt_fields(s, st, st->parser, &out_pkt);
 
         if (out_pkt.data == pkt->data && out_pkt.size == pkt->size) {
+            out_pkt.buf   = pkt->buf;
+            pkt->buf      = NULL;
+#if FF_API_DESTRUCT_PACKET
             out_pkt.destruct = pkt->destruct;
             pkt->destruct = NULL;
+#endif
         }
         if ((ret = av_dup_packet(&out_pkt)) < 0)
             goto fail;
@@ -1693,7 +1744,7 @@
     if(index<0){
         index= (*nb_index_entries)++;
         ie= &entries[index];
-        assert(index==0 || ie[-1].timestamp < timestamp);
+        av_assert0(index==0 || ie[-1].timestamp < timestamp);
     }else{
         ie= &entries[index];
         if(ie->timestamp != timestamp){
@@ -1805,14 +1856,14 @@
             av_dlog(s, "using cached pos_min=0x%"PRIx64" dts_min=%s\n",
                     pos_min, av_ts2str(ts_min));
         }else{
-            assert(index==0);
+            av_assert1(index==0);
         }
 
         index= av_index_search_timestamp(st, target_ts, flags & ~AVSEEK_FLAG_BACKWARD);
-        assert(index < st->nb_index_entries);
+        av_assert0(index < st->nb_index_entries);
         if(index >= 0){
             e= &st->index_entries[index];
-            assert(e->timestamp >= target_ts);
+            av_assert1(e->timestamp >= target_ts);
             pos_max= e->pos;
             ts_max= e->timestamp;
             pos_limit= pos_max - e->min_distance;
@@ -1969,6 +2020,8 @@
 
     avio_seek(s->pb, pos, SEEK_SET);
 
+    s->io_repositioned = 1;
+
     return 0;
 }
 
@@ -1992,7 +2045,7 @@
         int nonkey=0;
 
         if(st->nb_index_entries){
-            assert(st->index_entries);
+            av_assert0(st->index_entries);
             ie= &st->index_entries[st->nb_index_entries-1];
             if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
                 return ret;
@@ -2085,7 +2138,7 @@
     int ret = seek_frame_internal(s, stream_index, timestamp, flags);
 
     if (ret >= 0)
-        avformat_queue_attached_pictures(s);
+        ret = avformat_queue_attached_pictures(s);
 
     return ret;
 }
@@ -2094,6 +2147,8 @@
 {
     if(min_ts > ts || max_ts < ts)
         return -1;
+    if (stream_index < -1 || stream_index >= (int)s->nb_streams)
+        return AVERROR(EINVAL);
 
     if(s->seek2any>0)
         flags |= AVSEEK_FLAG_ANY;
@@ -2116,7 +2171,7 @@
         ret = s->iformat->read_seek2(s, stream_index, min_ts, ts, max_ts, flags);
 
         if (ret >= 0)
-            avformat_queue_attached_pictures(s);
+            ret = avformat_queue_attached_pictures(s);
         return ret;
     }
 
@@ -2230,8 +2285,10 @@
     }
         if (ic->pb && (filesize = avio_size(ic->pb)) > 0 && ic->duration != AV_NOPTS_VALUE) {
             /* compute the bitrate */
-            ic->bit_rate = (double)filesize * 8.0 * AV_TIME_BASE /
+            double bitrate = (double)filesize * 8.0 * AV_TIME_BASE /
                 (double)ic->duration;
+            if (bitrate >= 0 && bitrate <= INT_MAX)
+                ic->bit_rate = bitrate;
         }
 }
 
@@ -2255,7 +2312,7 @@
 static void estimate_timings_from_bit_rate(AVFormatContext *ic)
 {
     int64_t filesize, duration;
-    int bit_rate, i;
+    int bit_rate, i, show_warning = 0;
     AVStream *st;
 
     /* if bit_rate is already set, we believe it */
@@ -2276,12 +2333,17 @@
         if (filesize > 0) {
             for(i = 0; i < ic->nb_streams; i++) {
                 st = ic->streams[i];
-                duration= av_rescale(8*filesize, st->time_base.den, ic->bit_rate*(int64_t)st->time_base.num);
-                if (st->duration == AV_NOPTS_VALUE)
+                if (   st->time_base.num <= INT64_MAX / ic->bit_rate
+                    && st->duration == AV_NOPTS_VALUE) {
+                    duration= av_rescale(8*filesize, st->time_base.den, ic->bit_rate*(int64_t)st->time_base.num);
                     st->duration = duration;
+                    show_warning = 1;
+                }
             }
         }
     }
+    if (show_warning)
+        av_log(ic, AV_LOG_WARNING, "Estimating duration from bitrate, this may be inaccurate\n");
 }
 
 #define DURATION_MAX_READ_SIZE 250000LL
@@ -2342,8 +2404,10 @@
                 else
                     duration -= st->first_dts;
                 if (duration > 0) {
-                    if (st->duration == AV_NOPTS_VALUE || st->duration < duration)
+                    if (st->duration == AV_NOPTS_VALUE || st->info->last_duration<=0 ||
+                        (st->duration < duration && FFABS(duration - st->info->last_duration) < 60LL*st->time_base.den / st->time_base.num))
                         st->duration = duration;
+                    st->info->last_duration = duration;
                 }
             }
             av_free_packet(pkt);
@@ -2387,7 +2451,6 @@
         fill_all_stream_timings(ic);
         ic->duration_estimation_method = AVFMT_DURATION_FROM_STREAM;
     } else {
-        av_log(ic, AV_LOG_WARNING, "Estimating duration from bitrate, this may be inaccurate\n");
         /* less precise: use bitrate info */
         estimate_timings_from_bit_rate(ic);
         ic->duration_estimation_method = AVFMT_DURATION_FROM_BITRATE;
@@ -2438,6 +2501,9 @@
             FAIL("unspecified size");
         if (st->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
             FAIL("unspecified pixel format");
+        if (st->codec->codec_id == AV_CODEC_ID_RV30 || st->codec->codec_id == AV_CODEC_ID_RV40)
+            if (!st->sample_aspect_ratio.num && !st->codec->sample_aspect_ratio.num && !st->codec_info_nb_frames)
+                FAIL("no frame in rv30/40 and no sar");
         break;
     case AVMEDIA_TYPE_SUBTITLE:
         if (avctx->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !avctx->width)
@@ -2681,7 +2747,8 @@
 
 int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
 {
-    int i, count, ret, read_size, j;
+    int i, count, ret, j;
+    int64_t read_size;
     AVStream *st;
     AVPacket pkt1, *pkt;
     int64_t old_offset = avio_tell(ic->pb);
@@ -3209,6 +3276,7 @@
     if (st->attached_pic.data)
         av_free_packet(&st->attached_pic);
     av_dict_free(&st->metadata);
+    av_freep(&st->probe_data.buf);
     av_freep(&st->index_entries);
     av_freep(&st->codec->extradata);
     av_freep(&st->codec->subtitle_header);
@@ -3217,7 +3285,6 @@
     if (st->info)
         av_freep(&st->info->duration_error);
     av_freep(&st->info);
-    av_freep(&st->probe_data.buf);
     av_freep(&s->streams[ --s->nb_streams ]);
 }
 
@@ -3631,11 +3698,11 @@
         if (c == '%') {
             do {
                 nd = 0;
-                while (isdigit(*p)) {
+                while (av_isdigit(*p)) {
                     nd = nd * 10 + *p++ - '0';
                 }
                 c = *p++;
-            } while (isdigit(c));
+            } while (av_isdigit(c));
 
             switch(c) {
             case '%':
@@ -3856,7 +3923,7 @@
         p += strspn(p, SPACE_CHARS);
         if (*p == '\0')
             break;
-        c = toupper((unsigned char) *p++);
+        c = av_toupper((unsigned char) *p++);
         if (c >= '0' && c <= '9')
             c = c - '0';
         else if (c >= 'A' && c <= 'F')
@@ -3979,7 +4046,7 @@
         int key_len, dest_len = 0;
 
         /* Skip whitespace and potential commas. */
-        while (*ptr && (isspace(*ptr) || *ptr == ','))
+        while (*ptr && (av_isspace(*ptr) || *ptr == ','))
             ptr++;
         if (!*ptr)
             break;
@@ -4012,7 +4079,7 @@
             if (*ptr == '\"')
                 ptr++;
         } else {
-            for (; *ptr && !(isspace(*ptr) || *ptr == ','); ptr++)
+            for (; *ptr && !(av_isspace(*ptr) || *ptr == ','); ptr++)
                 if (dest && dest < dest_end)
                     *dest++ = *ptr;
         }
@@ -4221,6 +4288,22 @@
         return frame_sample_aspect_ratio;
 }
 
+AVRational av_guess_frame_rate(AVFormatContext *format, AVStream *st, AVFrame *frame)
+{
+    AVRational fr = st->r_frame_rate;
+
+    if (st->codec->ticks_per_frame > 1) {
+        AVRational codec_fr = av_inv_q(st->codec->time_base);
+        AVRational   avg_fr = st->avg_frame_rate;
+        codec_fr.den *= st->codec->ticks_per_frame;
+        if (   codec_fr.num > 0 && codec_fr.den > 0 && av_q2d(codec_fr) < av_q2d(fr)*0.7
+            && fabs(1.0 - av_q2d(av_div_q(avg_fr, fr))) > 0.1)
+            fr = codec_fr;
+    }
+
+    return fr;
+}
+
 int avformat_match_stream_specifier(AVFormatContext *s, AVStream *st,
                                     const char *spec)
 {
diff --git a/libavformat/vc1testenc.c b/libavformat/vc1testenc.c
index e084516..751333a 100644
--- a/libavformat/vc1testenc.c
+++ b/libavformat/vc1testenc.c
@@ -63,7 +63,6 @@
     avio_wl32(pb, pkt->size | ((pkt->flags & AV_PKT_FLAG_KEY) ? 0x80000000 : 0));
     avio_wl32(pb, pkt->pts);
     avio_write(pb, pkt->data, pkt->size);
-    avio_flush(pb);
     ctx->frames++;
 
     return 0;
@@ -83,7 +82,7 @@
 }
 
 AVOutputFormat ff_vc1t_muxer = {
-    .name              = "rcv",
+    .name              = "vc1test",
     .long_name         = NULL_IF_CONFIG_SMALL("VC-1 test bitstream"),
     .extensions        = "rcv",
     .priv_data_size    = sizeof(RCVContext),
diff --git a/libavformat/version.h b/libavformat/version.h
index 082ab99..8fd08cf 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -29,9 +29,9 @@
 
 #include "libavutil/avutil.h"
 
-#define LIBAVFORMAT_VERSION_MAJOR 54
-#define LIBAVFORMAT_VERSION_MINOR 63
-#define LIBAVFORMAT_VERSION_MICRO 102
+#define LIBAVFORMAT_VERSION_MAJOR 55
+#define LIBAVFORMAT_VERSION_MINOR  3
+#define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \
@@ -56,34 +56,27 @@
 #define FF_API_PKT_DUMP                (LIBAVFORMAT_VERSION_MAJOR < 54)
 #endif
 #ifndef FF_API_ALLOC_OUTPUT_CONTEXT
-#define FF_API_ALLOC_OUTPUT_CONTEXT    (LIBAVFORMAT_VERSION_MAJOR < 55)
+#define FF_API_ALLOC_OUTPUT_CONTEXT    (LIBAVFORMAT_VERSION_MAJOR < 56)
 #endif
 #ifndef FF_API_FORMAT_PARAMETERS
-#define FF_API_FORMAT_PARAMETERS       (LIBAVFORMAT_VERSION_MAJOR < 55)
+#define FF_API_FORMAT_PARAMETERS       (LIBAVFORMAT_VERSION_MAJOR < 56)
 #endif
 #ifndef FF_API_NEW_STREAM
-#define FF_API_NEW_STREAM              (LIBAVFORMAT_VERSION_MAJOR < 55)
+#define FF_API_NEW_STREAM              (LIBAVFORMAT_VERSION_MAJOR < 56)
 #endif
 #ifndef FF_API_SET_PTS_INFO
-#define FF_API_SET_PTS_INFO            (LIBAVFORMAT_VERSION_MAJOR < 55)
+#define FF_API_SET_PTS_INFO            (LIBAVFORMAT_VERSION_MAJOR < 56)
 #endif
 #ifndef FF_API_CLOSE_INPUT_FILE
-#define FF_API_CLOSE_INPUT_FILE        (LIBAVFORMAT_VERSION_MAJOR < 55)
-#endif
-#ifndef FF_API_APPLEHTTP_PROTO
-#define FF_API_APPLEHTTP_PROTO         (LIBAVFORMAT_VERSION_MAJOR < 55)
+#define FF_API_CLOSE_INPUT_FILE        (LIBAVFORMAT_VERSION_MAJOR < 56)
 #endif
 #ifndef FF_API_READ_PACKET
-#define FF_API_READ_PACKET             (LIBAVFORMAT_VERSION_MAJOR < 55)
+#define FF_API_READ_PACKET             (LIBAVFORMAT_VERSION_MAJOR < 56)
 #endif
-#ifndef FF_API_INTERLEAVE_PACKET
-#define FF_API_INTERLEAVE_PACKET       (LIBAVFORMAT_VERSION_MAJOR < 55)
-#endif
-#ifndef FF_API_AV_GETTIME
-#define FF_API_AV_GETTIME              (LIBAVFORMAT_VERSION_MAJOR < 55)
+#ifndef FF_API_ASS_SSA
+#define FF_API_ASS_SSA                 (LIBAVFORMAT_VERSION_MAJOR < 56)
 #endif
 #ifndef FF_API_R_FRAME_RATE
-#define FF_API_R_FRAME_RATE            (LIBAVFORMAT_VERSION_MAJOR < 55)
+#define FF_API_R_FRAME_RATE            1
 #endif
-
 #endif /* AVFORMAT_VERSION_H */
diff --git a/libavformat/wavdec.c b/libavformat/wavdec.c
index 782fa64..14c52f8 100644
--- a/libavformat/wavdec.c
+++ b/libavformat/wavdec.c
@@ -74,7 +74,7 @@
         size = next_tag(pb, &tag);
         if (tag == tag1)
             break;
-        avio_skip(pb, size);
+        avio_skip(pb, size + (size & 1));
     }
     return size;
 }
@@ -354,6 +354,9 @@
             break;
         }
 
+        /* skip padding byte */
+        next_tag_ofs += (next_tag_ofs < INT64_MAX && next_tag_ofs & 1);
+
         /* seek to next tag unless we know that we'll run into EOF */
         if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
             avio_seek(pb, next_tag_ofs, SEEK_SET) < 0) {
diff --git a/libavformat/westwood_aud.c b/libavformat/westwood_aud.c
index f4b8584..d666117 100644
--- a/libavformat/westwood_aud.c
+++ b/libavformat/westwood_aud.c
@@ -102,7 +102,7 @@
     switch (codec) {
     case  1:
         if (channels != 1) {
-            av_log_ask_for_sample(s, "Stereo WS-SND1 is not supported.\n");
+            avpriv_request_sample(s, "Stereo WS-SND1");
             return AVERROR_PATCHWELCOME;
         }
         st->codec->codec_id = AV_CODEC_ID_WESTWOOD_SND1;
@@ -113,7 +113,7 @@
         st->codec->bit_rate = channels * sample_rate * 4;
         break;
     default:
-        av_log_ask_for_sample(s, "Unknown codec: %d\n", codec);
+        avpriv_request_sample(s, "Unknown codec: %d", codec);
         return AVERROR_PATCHWELCOME;
     }
     avpriv_set_pts_info(st, 64, 1, sample_rate);
diff --git a/libavformat/wtvenc.c b/libavformat/wtvenc.c
index 22917a4..410a2dc 100644
--- a/libavformat/wtvenc.c
+++ b/libavformat/wtvenc.c
@@ -457,7 +457,6 @@
     write_pad(pb, WTV_PAD8(pkt->size) - pkt->size);
 
     wctx->serial++;
-    avio_flush(pb);
     return 0;
 }
 
diff --git a/libavformat/wvenc.c b/libavformat/wvenc.c
index 3cc0a50..c33d430 100644
--- a/libavformat/wvenc.c
+++ b/libavformat/wvenc.c
@@ -45,7 +45,7 @@
         return AVERROR(EINVAL);
     }
     if (codec->extradata_size > 0) {
-        av_log_missing_feature(s, "remuxing from matroska container", 0);
+        avpriv_report_missing_feature(s, "remuxing from matroska container");
         return AVERROR_PATCHWELCOME;
     }
     avpriv_set_pts_info(s->streams[0], 64, 1, codec->sample_rate);
@@ -110,7 +110,6 @@
         avio_write(pb, ptr, size - 24);
         ptr += size - 24; left -= size - 24;
     }
-    avio_flush(pb);
 
     return 0;
 }
diff --git a/libavformat/xmv.c b/libavformat/xmv.c
index b447d6c..672b6d9 100644
--- a/libavformat/xmv.c
+++ b/libavformat/xmv.c
@@ -124,6 +124,15 @@
     return 0;
 }
 
+static int xmv_read_close(AVFormatContext *s)
+{
+    XMVDemuxContext *xmv = s->priv_data;
+
+    av_freep(&xmv->audio);
+
+    return 0;
+}
+
 static int xmv_read_header(AVFormatContext *s)
 {
     XMVDemuxContext *xmv = s->priv_data;
@@ -133,6 +142,7 @@
     uint32_t file_version;
     uint32_t this_packet_size;
     uint16_t audio_track;
+    int ret;
 
     avio_skip(pb, 4); /* Next packet size */
 
@@ -143,7 +153,7 @@
 
     file_version = avio_rl32(pb);
     if ((file_version != 4) && (file_version != 2))
-        av_log_ask_for_sample(s, "Found uncommon version %d\n", file_version);
+        avpriv_request_sample(s, "Uncommon version %d", file_version);
 
 
     /* Video track */
@@ -171,8 +181,10 @@
     avio_skip(pb, 2); /* Unknown (padding?) */
 
     xmv->audio = av_malloc(xmv->audio_track_count * sizeof(XMVAudioPacket));
-    if (!xmv->audio)
-        return AVERROR(ENOMEM);
+    if (!xmv->audio) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
 
     for (audio_track = 0; audio_track < xmv->audio_track_count; audio_track++) {
         XMVAudioPacket *packet = &xmv->audio[audio_track];
@@ -184,11 +196,6 @@
         packet->bits_per_sample = avio_rl16(pb);
         packet->flags           = avio_rl16(pb);
 
-        if (!packet->channels) {
-            av_log(s, AV_LOG_ERROR, "0 channels\n");
-            return AVERROR(EINVAL);
-        }
-
         packet->bit_rate      = packet->bits_per_sample *
                                 packet->sample_rate *
                                 packet->channels;
@@ -208,9 +215,18 @@
             av_log(s, AV_LOG_WARNING, "Unsupported 5.1 ADPCM audio stream "
                                       "(0x%04X)\n", packet->flags);
 
+        if (!packet->channels || !packet->sample_rate) {
+            av_log(s, AV_LOG_ERROR, "Invalid parameters for audio track %d.\n",
+                   audio_track);
+            ret = AVERROR_INVALIDDATA;
+            goto fail;
+        }
+
         ast = avformat_new_stream(s, NULL);
-        if (!ast)
-            return AVERROR(ENOMEM);
+        if (!ast) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
 
         ast->codec->codec_type            = AVMEDIA_TYPE_AUDIO;
         ast->codec->codec_id              = packet->codec_id;
@@ -236,6 +252,10 @@
     xmv->stream_count       = xmv->audio_track_count + 1;
 
     return 0;
+
+fail:
+    xmv_read_close(s);
+    return ret;
 }
 
 static void xmv_read_extradata(uint8_t *extradata, AVIOContext *pb)
@@ -546,15 +566,6 @@
     return 0;
 }
 
-static int xmv_read_close(AVFormatContext *s)
-{
-    XMVDemuxContext *xmv = s->priv_data;
-
-    av_freep(&xmv->audio);
-
-    return 0;
-}
-
 AVInputFormat ff_xmv_demuxer = {
     .name           = "xmv",
     .long_name      = NULL_IF_CONFIG_SMALL("Microsoft XMV"),
diff --git a/libavformat/xwma.c b/libavformat/xwma.c
index db7d971..0d40bd7 100644
--- a/libavformat/xwma.c
+++ b/libavformat/xwma.c
@@ -85,9 +85,8 @@
      * anyway.
      */
     if (st->codec->codec_id != AV_CODEC_ID_WMAV2) {
-        av_log(s, AV_LOG_WARNING, "unexpected codec (tag 0x04%x; id %d)\n",
+        avpriv_request_sample(s, "Unexpected codec (tag 0x04%x; id %d)",
                               st->codec->codec_tag, st->codec->codec_id);
-        av_log_ask_for_sample(s, NULL);
     } else {
         /* In all xWMA files I have seen, there is no extradata. But the WMA
          * codecs require extradata, so we provide our own fake extradata.
@@ -101,9 +100,8 @@
              * if it will work, but just go on and try it, after asking
              * the user for a sample.
              */
-            av_log(s, AV_LOG_WARNING, "unexpected extradata (%d bytes)\n",
+            avpriv_request_sample(s, "Unexpected extradata (%d bytes)",
                                   st->codec->extradata_size);
-            av_log_ask_for_sample(s, NULL);
         } else {
             st->codec->extradata_size = 6;
             st->codec->extradata      = av_mallocz(6 + FF_INPUT_BUFFER_PADDING_SIZE);
diff --git a/libavformat/yop.c b/libavformat/yop.c
index d1c0129..c84deea 100644
--- a/libavformat/yop.c
+++ b/libavformat/yop.c
@@ -135,8 +135,7 @@
 
     if (yop->video_packet.data) {
         *pkt                   =  yop->video_packet;
-        yop->video_packet.data =  NULL;
-        yop->video_packet.size =  0;
+        memset(&yop->video_packet, 0, sizeof(yop->video_packet));
         pkt->data[0]           =  yop->odd_frame;
         pkt->flags             |= AV_PKT_FLAG_KEY;
         yop->odd_frame         ^= 1;
diff --git a/libavformat/yuv4mpeg.c b/libavformat/yuv4mpeg.c
index f34a4af..bf48230 100644
--- a/libavformat/yuv4mpeg.c
+++ b/libavformat/yuv4mpeg.c
@@ -232,7 +232,6 @@
         }
     }
 
-    avio_flush(pb);
     return 0;
 }
 
diff --git a/libavresample/x86/audio_convert_init.c b/libavresample/x86/audio_convert_init.c
index 879108d..2110445 100644
--- a/libavresample/x86/audio_convert_init.c
+++ b/libavresample/x86/audio_convert_init.c
@@ -25,123 +25,123 @@
 
 /* flat conversions */
 
-extern void ff_conv_s16_to_s32_sse2(int16_t *dst, const int32_t *src, int len);
+void ff_conv_s16_to_s32_sse2(int16_t *dst, const int32_t *src, int len);
 
-extern void ff_conv_s16_to_flt_sse2(float *dst, const int16_t *src, int len);
-extern void ff_conv_s16_to_flt_sse4(float *dst, const int16_t *src, int len);
+void ff_conv_s16_to_flt_sse2(float *dst, const int16_t *src, int len);
+void ff_conv_s16_to_flt_sse4(float *dst, const int16_t *src, int len);
 
-extern void ff_conv_s32_to_s16_mmx (int16_t *dst, const int32_t *src, int len);
-extern void ff_conv_s32_to_s16_sse2(int16_t *dst, const int32_t *src, int len);
+void ff_conv_s32_to_s16_mmx (int16_t *dst, const int32_t *src, int len);
+void ff_conv_s32_to_s16_sse2(int16_t *dst, const int32_t *src, int len);
 
-extern void ff_conv_s32_to_flt_sse2(float *dst, const int32_t *src, int len);
-extern void ff_conv_s32_to_flt_avx (float *dst, const int32_t *src, int len);
+void ff_conv_s32_to_flt_sse2(float *dst, const int32_t *src, int len);
+void ff_conv_s32_to_flt_avx (float *dst, const int32_t *src, int len);
 
-extern void ff_conv_flt_to_s16_sse2(int16_t *dst, const float *src, int len);
+void ff_conv_flt_to_s16_sse2(int16_t *dst, const float *src, int len);
 
-extern void ff_conv_flt_to_s32_sse2(int32_t *dst, const float *src, int len);
-extern void ff_conv_flt_to_s32_avx (int32_t *dst, const float *src, int len);
+void ff_conv_flt_to_s32_sse2(int32_t *dst, const float *src, int len);
+void ff_conv_flt_to_s32_avx (int32_t *dst, const float *src, int len);
 
 /* interleave conversions */
 
-extern void ff_conv_s16p_to_s16_2ch_sse2(int16_t *dst, int16_t *const *src,
-                                         int len, int channels);
-extern void ff_conv_s16p_to_s16_2ch_avx (int16_t *dst, int16_t *const *src,
-                                         int len, int channels);
+void ff_conv_s16p_to_s16_2ch_sse2(int16_t *dst, int16_t *const *src,
+                                  int len, int channels);
+void ff_conv_s16p_to_s16_2ch_avx (int16_t *dst, int16_t *const *src,
+                                  int len, int channels);
 
-extern void ff_conv_s16p_to_s16_6ch_sse2(int16_t *dst, int16_t *const *src,
-                                         int len, int channels);
-extern void ff_conv_s16p_to_s16_6ch_sse2slow(int16_t *dst, int16_t *const *src,
-                                             int len, int channels);
-extern void ff_conv_s16p_to_s16_6ch_avx (int16_t *dst, int16_t *const *src,
-                                         int len, int channels);
+void ff_conv_s16p_to_s16_6ch_sse2(int16_t *dst, int16_t *const *src,
+                                  int len, int channels);
+void ff_conv_s16p_to_s16_6ch_sse2slow(int16_t *dst, int16_t *const *src,
+                                      int len, int channels);
+void ff_conv_s16p_to_s16_6ch_avx (int16_t *dst, int16_t *const *src,
+                                  int len, int channels);
 
-extern void ff_conv_s16p_to_flt_2ch_sse2(float *dst, int16_t *const *src,
-                                         int len, int channels);
-extern void ff_conv_s16p_to_flt_2ch_avx (float *dst, int16_t *const *src,
-                                         int len, int channels);
+void ff_conv_s16p_to_flt_2ch_sse2(float *dst, int16_t *const *src,
+                                  int len, int channels);
+void ff_conv_s16p_to_flt_2ch_avx (float *dst, int16_t *const *src,
+                                  int len, int channels);
 
-extern void ff_conv_s16p_to_flt_6ch_sse2 (float *dst, int16_t *const *src,
-                                          int len, int channels);
-extern void ff_conv_s16p_to_flt_6ch_ssse3(float *dst, int16_t *const *src,
-                                         int len, int channels);
-extern void ff_conv_s16p_to_flt_6ch_avx  (float *dst, int16_t *const *src,
-                                          int len, int channels);
+void ff_conv_s16p_to_flt_6ch_sse2 (float *dst, int16_t *const *src,
+                                   int len, int channels);
+void ff_conv_s16p_to_flt_6ch_ssse3(float *dst, int16_t *const *src,
+                                  int len, int channels);
+void ff_conv_s16p_to_flt_6ch_avx  (float *dst, int16_t *const *src,
+                                   int len, int channels);
 
-extern void ff_conv_fltp_to_s16_2ch_sse2 (int16_t *dst, float *const *src,
-                                          int len, int channels);
-extern void ff_conv_fltp_to_s16_2ch_ssse3(int16_t *dst, float *const *src,
-                                          int len, int channels);
+void ff_conv_fltp_to_s16_2ch_sse2 (int16_t *dst, float *const *src,
+                                   int len, int channels);
+void ff_conv_fltp_to_s16_2ch_ssse3(int16_t *dst, float *const *src,
+                                   int len, int channels);
 
-extern void ff_conv_fltp_to_s16_6ch_sse (int16_t *dst, float *const *src,
-                                         int len, int channels);
-extern void ff_conv_fltp_to_s16_6ch_sse2(int16_t *dst, float *const *src,
-                                         int len, int channels);
-extern void ff_conv_fltp_to_s16_6ch_avx (int16_t *dst, float *const *src,
-                                         int len, int channels);
+void ff_conv_fltp_to_s16_6ch_sse (int16_t *dst, float *const *src,
+                                  int len, int channels);
+void ff_conv_fltp_to_s16_6ch_sse2(int16_t *dst, float *const *src,
+                                  int len, int channels);
+void ff_conv_fltp_to_s16_6ch_avx (int16_t *dst, float *const *src,
+                                  int len, int channels);
 
-extern void ff_conv_fltp_to_flt_2ch_sse(float *dst, float *const *src, int len,
-                                        int channels);
-extern void ff_conv_fltp_to_flt_2ch_avx(float *dst, float *const *src, int len,
-                                        int channels);
+void ff_conv_fltp_to_flt_2ch_sse(float *dst, float *const *src, int len,
+                                 int channels);
+void ff_conv_fltp_to_flt_2ch_avx(float *dst, float *const *src, int len,
+                                 int channels);
 
-extern void ff_conv_fltp_to_flt_6ch_mmx (float *dst, float *const *src, int len,
-                                         int channels);
-extern void ff_conv_fltp_to_flt_6ch_sse4(float *dst, float *const *src, int len,
-                                         int channels);
-extern void ff_conv_fltp_to_flt_6ch_avx (float *dst, float *const *src, int len,
-                                         int channels);
+void ff_conv_fltp_to_flt_6ch_mmx (float *dst, float *const *src, int len,
+                                  int channels);
+void ff_conv_fltp_to_flt_6ch_sse4(float *dst, float *const *src, int len,
+                                  int channels);
+void ff_conv_fltp_to_flt_6ch_avx (float *dst, float *const *src, int len,
+                                  int channels);
 
 /* deinterleave conversions */
 
-extern void ff_conv_s16_to_s16p_2ch_sse2(int16_t *const *dst, int16_t *src,
-                                         int len, int channels);
-extern void ff_conv_s16_to_s16p_2ch_ssse3(int16_t *const *dst, int16_t *src,
-                                          int len, int channels);
-extern void ff_conv_s16_to_s16p_2ch_avx (int16_t *const *dst, int16_t *src,
-                                         int len, int channels);
+void ff_conv_s16_to_s16p_2ch_sse2(int16_t *const *dst, int16_t *src,
+                                  int len, int channels);
+void ff_conv_s16_to_s16p_2ch_ssse3(int16_t *const *dst, int16_t *src,
+                                   int len, int channels);
+void ff_conv_s16_to_s16p_2ch_avx (int16_t *const *dst, int16_t *src,
+                                  int len, int channels);
 
-extern void ff_conv_s16_to_s16p_6ch_sse2 (int16_t *const *dst, int16_t *src,
-                                          int len, int channels);
-extern void ff_conv_s16_to_s16p_6ch_ssse3(int16_t *const *dst, int16_t *src,
-                                          int len, int channels);
-extern void ff_conv_s16_to_s16p_6ch_avx  (int16_t *const *dst, int16_t *src,
-                                          int len, int channels);
+void ff_conv_s16_to_s16p_6ch_sse2 (int16_t *const *dst, int16_t *src,
+                                   int len, int channels);
+void ff_conv_s16_to_s16p_6ch_ssse3(int16_t *const *dst, int16_t *src,
+                                   int len, int channels);
+void ff_conv_s16_to_s16p_6ch_avx  (int16_t *const *dst, int16_t *src,
+                                   int len, int channels);
 
-extern void ff_conv_s16_to_fltp_2ch_sse2(float *const *dst, int16_t *src,
-                                         int len, int channels);
-extern void ff_conv_s16_to_fltp_2ch_avx (float *const *dst, int16_t *src,
-                                         int len, int channels);
+void ff_conv_s16_to_fltp_2ch_sse2(float *const *dst, int16_t *src,
+                                  int len, int channels);
+void ff_conv_s16_to_fltp_2ch_avx (float *const *dst, int16_t *src,
+                                  int len, int channels);
 
-extern void ff_conv_s16_to_fltp_6ch_sse2 (float *const *dst, int16_t *src,
-                                          int len, int channels);
-extern void ff_conv_s16_to_fltp_6ch_ssse3(float *const *dst, int16_t *src,
-                                          int len, int channels);
-extern void ff_conv_s16_to_fltp_6ch_sse4 (float *const *dst, int16_t *src,
-                                          int len, int channels);
-extern void ff_conv_s16_to_fltp_6ch_avx  (float *const *dst, int16_t *src,
-                                          int len, int channels);
+void ff_conv_s16_to_fltp_6ch_sse2 (float *const *dst, int16_t *src,
+                                   int len, int channels);
+void ff_conv_s16_to_fltp_6ch_ssse3(float *const *dst, int16_t *src,
+                                   int len, int channels);
+void ff_conv_s16_to_fltp_6ch_sse4 (float *const *dst, int16_t *src,
+                                   int len, int channels);
+void ff_conv_s16_to_fltp_6ch_avx  (float *const *dst, int16_t *src,
+                                   int len, int channels);
 
-extern void ff_conv_flt_to_s16p_2ch_sse2(int16_t *const *dst, float *src,
-                                         int len, int channels);
-extern void ff_conv_flt_to_s16p_2ch_avx (int16_t *const *dst, float *src,
-                                         int len, int channels);
+void ff_conv_flt_to_s16p_2ch_sse2(int16_t *const *dst, float *src,
+                                  int len, int channels);
+void ff_conv_flt_to_s16p_2ch_avx (int16_t *const *dst, float *src,
+                                  int len, int channels);
 
-extern void ff_conv_flt_to_s16p_6ch_sse2 (int16_t *const *dst, float *src,
-                                          int len, int channels);
-extern void ff_conv_flt_to_s16p_6ch_ssse3(int16_t *const *dst, float *src,
-                                          int len, int channels);
-extern void ff_conv_flt_to_s16p_6ch_avx  (int16_t *const *dst, float *src,
-                                          int len, int channels);
+void ff_conv_flt_to_s16p_6ch_sse2 (int16_t *const *dst, float *src,
+                                   int len, int channels);
+void ff_conv_flt_to_s16p_6ch_ssse3(int16_t *const *dst, float *src,
+                                   int len, int channels);
+void ff_conv_flt_to_s16p_6ch_avx  (int16_t *const *dst, float *src,
+                                   int len, int channels);
 
-extern void ff_conv_flt_to_fltp_2ch_sse(float *const *dst, float *src, int len,
-                                        int channels);
-extern void ff_conv_flt_to_fltp_2ch_avx(float *const *dst, float *src, int len,
-                                        int channels);
+void ff_conv_flt_to_fltp_2ch_sse(float *const *dst, float *src, int len,
+                                 int channels);
+void ff_conv_flt_to_fltp_2ch_avx(float *const *dst, float *src, int len,
+                                 int channels);
 
-extern void ff_conv_flt_to_fltp_6ch_sse2(float *const *dst, float *src, int len,
-                                         int channels);
-extern void ff_conv_flt_to_fltp_6ch_avx (float *const *dst, float *src, int len,
-                                         int channels);
+void ff_conv_flt_to_fltp_6ch_sse2(float *const *dst, float *src, int len,
+                                  int channels);
+void ff_conv_flt_to_fltp_6ch_avx (float *const *dst, float *src, int len,
+                                  int channels);
 
 av_cold void ff_audio_convert_init_x86(AudioConvert *ac)
 {
diff --git a/libavresample/x86/audio_mix_init.c b/libavresample/x86/audio_mix_init.c
index 72b2397..9b977f8 100644
--- a/libavresample/x86/audio_mix_init.c
+++ b/libavresample/x86/audio_mix_init.c
@@ -23,80 +23,80 @@
 #include "libavutil/x86/cpu.h"
 #include "libavresample/audio_mix.h"
 
-extern void ff_mix_2_to_1_fltp_flt_sse(float **src, float **matrix, int len,
-                                       int out_ch, int in_ch);
-extern void ff_mix_2_to_1_fltp_flt_avx(float **src, float **matrix, int len,
-                                       int out_ch, int in_ch);
+void ff_mix_2_to_1_fltp_flt_sse(float **src, float **matrix, int len,
+                                int out_ch, int in_ch);
+void ff_mix_2_to_1_fltp_flt_avx(float **src, float **matrix, int len,
+                                int out_ch, int in_ch);
 
-extern void ff_mix_2_to_1_s16p_flt_sse2(int16_t **src, float **matrix, int len,
-                                        int out_ch, int in_ch);
-extern void ff_mix_2_to_1_s16p_flt_sse4(int16_t **src, float **matrix, int len,
-                                        int out_ch, int in_ch);
+void ff_mix_2_to_1_s16p_flt_sse2(int16_t **src, float **matrix, int len,
+                                 int out_ch, int in_ch);
+void ff_mix_2_to_1_s16p_flt_sse4(int16_t **src, float **matrix, int len,
+                                 int out_ch, int in_ch);
 
-extern void ff_mix_2_to_1_s16p_q8_sse2(int16_t **src, int16_t **matrix,
-                                       int len, int out_ch, int in_ch);
+void ff_mix_2_to_1_s16p_q8_sse2(int16_t **src, int16_t **matrix,
+                                int len, int out_ch, int in_ch);
 
-extern void ff_mix_1_to_2_fltp_flt_sse(float **src, float **matrix, int len,
-                                       int out_ch, int in_ch);
-extern void ff_mix_1_to_2_fltp_flt_avx(float **src, float **matrix, int len,
-                                       int out_ch, int in_ch);
+void ff_mix_1_to_2_fltp_flt_sse(float **src, float **matrix, int len,
+                                int out_ch, int in_ch);
+void ff_mix_1_to_2_fltp_flt_avx(float **src, float **matrix, int len,
+                                int out_ch, int in_ch);
 
-extern void ff_mix_1_to_2_s16p_flt_sse2(int16_t **src, float **matrix, int len,
-                                        int out_ch, int in_ch);
-extern void ff_mix_1_to_2_s16p_flt_sse4(int16_t **src, float **matrix, int len,
-                                        int out_ch, int in_ch);
-extern void ff_mix_1_to_2_s16p_flt_avx (int16_t **src, float **matrix, int len,
-                                        int out_ch, int in_ch);
+void ff_mix_1_to_2_s16p_flt_sse2(int16_t **src, float **matrix, int len,
+                                 int out_ch, int in_ch);
+void ff_mix_1_to_2_s16p_flt_sse4(int16_t **src, float **matrix, int len,
+                                 int out_ch, int in_ch);
+void ff_mix_1_to_2_s16p_flt_avx (int16_t **src, float **matrix, int len,
+                                 int out_ch, int in_ch);
 
-#define DEFINE_MIX_3_8_TO_1_2(chan)                                         \
-extern void ff_mix_ ## chan ## _to_1_fltp_flt_sse(float **src,              \
-                                                  float **matrix, int len,  \
-                                                  int out_ch, int in_ch);   \
-extern void ff_mix_ ## chan ## _to_2_fltp_flt_sse(float **src,              \
-                                                  float **matrix, int len,  \
-                                                  int out_ch, int in_ch);   \
-                                                                            \
-extern void ff_mix_ ## chan ## _to_1_s16p_flt_sse2(int16_t **src,           \
-                                                   float **matrix, int len, \
-                                                   int out_ch, int in_ch);  \
-extern void ff_mix_ ## chan ## _to_2_s16p_flt_sse2(int16_t **src,           \
-                                                   float **matrix, int len, \
-                                                   int out_ch, int in_ch);  \
-                                                                            \
-extern void ff_mix_ ## chan ## _to_1_s16p_flt_sse4(int16_t **src,           \
-                                                   float **matrix, int len, \
-                                                   int out_ch, int in_ch);  \
-extern void ff_mix_ ## chan ## _to_2_s16p_flt_sse4(int16_t **src,           \
-                                                   float **matrix, int len, \
-                                                   int out_ch, int in_ch);  \
-                                                                            \
-extern void ff_mix_ ## chan ## _to_1_fltp_flt_avx(float **src,              \
-                                                  float **matrix, int len,  \
-                                                  int out_ch, int in_ch);   \
-extern void ff_mix_ ## chan ## _to_2_fltp_flt_avx(float **src,              \
-                                                  float **matrix, int len,  \
-                                                  int out_ch, int in_ch);   \
-                                                                            \
-extern void ff_mix_ ## chan ## _to_1_s16p_flt_avx(int16_t **src,            \
-                                                  float **matrix, int len,  \
-                                                  int out_ch, int in_ch);   \
-extern void ff_mix_ ## chan ## _to_2_s16p_flt_avx(int16_t **src,            \
-                                                  float **matrix, int len,  \
-                                                  int out_ch, int in_ch);   \
-                                                                            \
-extern void ff_mix_ ## chan ## _to_1_fltp_flt_fma4(float **src,             \
-                                                   float **matrix, int len, \
-                                                   int out_ch, int in_ch);  \
-extern void ff_mix_ ## chan ## _to_2_fltp_flt_fma4(float **src,             \
-                                                   float **matrix, int len, \
-                                                   int out_ch, int in_ch);  \
-                                                                            \
-extern void ff_mix_ ## chan ## _to_1_s16p_flt_fma4(int16_t **src,           \
-                                                   float **matrix, int len, \
-                                                   int out_ch, int in_ch);  \
-extern void ff_mix_ ## chan ## _to_2_s16p_flt_fma4(int16_t **src,           \
-                                                   float **matrix, int len, \
-                                                   int out_ch, int in_ch);
+#define DEFINE_MIX_3_8_TO_1_2(chan)                                     \
+void ff_mix_ ## chan ## _to_1_fltp_flt_sse(float **src,                 \
+                                           float **matrix, int len,     \
+                                           int out_ch, int in_ch);      \
+void ff_mix_ ## chan ## _to_2_fltp_flt_sse(float **src,                 \
+                                           float **matrix, int len,     \
+                                           int out_ch, int in_ch);      \
+                                                                        \
+void ff_mix_ ## chan ## _to_1_s16p_flt_sse2(int16_t **src,              \
+                                            float **matrix, int len,    \
+                                            int out_ch, int in_ch);     \
+void ff_mix_ ## chan ## _to_2_s16p_flt_sse2(int16_t **src,              \
+                                            float **matrix, int len,    \
+                                            int out_ch, int in_ch);     \
+                                                                        \
+void ff_mix_ ## chan ## _to_1_s16p_flt_sse4(int16_t **src,              \
+                                            float **matrix, int len,    \
+                                            int out_ch, int in_ch);     \
+void ff_mix_ ## chan ## _to_2_s16p_flt_sse4(int16_t **src,              \
+                                            float **matrix, int len,    \
+                                            int out_ch, int in_ch);     \
+                                                                        \
+void ff_mix_ ## chan ## _to_1_fltp_flt_avx(float **src,                 \
+                                           float **matrix, int len,     \
+                                           int out_ch, int in_ch);      \
+void ff_mix_ ## chan ## _to_2_fltp_flt_avx(float **src,                 \
+                                           float **matrix, int len,     \
+                                           int out_ch, int in_ch);      \
+                                                                        \
+void ff_mix_ ## chan ## _to_1_s16p_flt_avx(int16_t **src,               \
+                                           float **matrix, int len,     \
+                                           int out_ch, int in_ch);      \
+void ff_mix_ ## chan ## _to_2_s16p_flt_avx(int16_t **src,               \
+                                           float **matrix, int len,     \
+                                           int out_ch, int in_ch);      \
+                                                                        \
+void ff_mix_ ## chan ## _to_1_fltp_flt_fma4(float **src,                \
+                                            float **matrix, int len,    \
+                                            int out_ch, int in_ch);     \
+void ff_mix_ ## chan ## _to_2_fltp_flt_fma4(float **src,                \
+                                            float **matrix, int len,    \
+                                            int out_ch, int in_ch);     \
+                                                                        \
+void ff_mix_ ## chan ## _to_1_s16p_flt_fma4(int16_t **src,              \
+                                            float **matrix, int len,    \
+                                            int out_ch, int in_ch);     \
+void ff_mix_ ## chan ## _to_2_s16p_flt_fma4(int16_t **src,              \
+                                            float **matrix, int len,    \
+                                            int out_ch, int in_ch);
 
 DEFINE_MIX_3_8_TO_1_2(3)
 DEFINE_MIX_3_8_TO_1_2(4)
diff --git a/libavresample/x86/dither_init.c b/libavresample/x86/dither_init.c
index 6532887..e9411c5 100644
--- a/libavresample/x86/dither_init.c
+++ b/libavresample/x86/dither_init.c
@@ -23,14 +23,13 @@
 #include "libavutil/x86/cpu.h"
 #include "libavresample/dither.h"
 
-extern void ff_quantize_sse2(int16_t *dst, const float *src, float *dither,
-                             int len);
+void ff_quantize_sse2(int16_t *dst, const float *src, float *dither, int len);
 
-extern void ff_dither_int_to_float_rectangular_sse2(float *dst, int *src, int len);
-extern void ff_dither_int_to_float_rectangular_avx(float *dst, int *src, int len);
+void ff_dither_int_to_float_rectangular_sse2(float *dst, int *src, int len);
+void ff_dither_int_to_float_rectangular_avx(float *dst, int *src, int len);
 
-extern void ff_dither_int_to_float_triangular_sse2(float *dst, int *src0, int len);
-extern void ff_dither_int_to_float_triangular_avx(float *dst, int *src0, int len);
+void ff_dither_int_to_float_triangular_sse2(float *dst, int *src0, int len);
+void ff_dither_int_to_float_triangular_avx(float *dst, int *src0, int len);
 
 av_cold void ff_dither_init_x86(DitherDSPContext *ddsp,
                                 enum AVResampleDitherMethod method)
diff --git a/libavutil/Makefile b/libavutil/Makefile
index 544c33f..33f82ed 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -14,6 +14,7 @@
           blowfish.h                                                    \
           bprint.h                                                      \
           bswap.h                                                       \
+          buffer.h                                                      \
           channel_layout.h                                              \
           common.h                                                      \
           cpu.h                                                         \
@@ -22,6 +23,7 @@
           eval.h                                                        \
           fifo.h                                                        \
           file.h                                                        \
+          frame.h                                                       \
           hmac.h                                                        \
           imgutils.h                                                    \
           intfloat.h                                                    \
@@ -50,6 +52,8 @@
 
 HEADERS-$(CONFIG_LZO)                   += lzo.h
 
+HEADERS-$(CONFIG_OPENCL)                += opencl.h
+
 ARCH_HEADERS = bswap.h                                                  \
                intmath.h                                                \
                intreadwrite.h                                           \
@@ -59,11 +63,13 @@
 
 OBJS = adler32.o                                                        \
        aes.o                                                            \
+       atomic.o                                                         \
        audio_fifo.o                                                     \
        avstring.o                                                       \
        base64.o                                                         \
        blowfish.o                                                       \
        bprint.o                                                         \
+       buffer.o                                                         \
        channel_layout.o                                                 \
        cpu.o                                                            \
        crc.o                                                            \
@@ -73,6 +79,7 @@
        fifo.o                                                           \
        file.o                                                           \
        float_dsp.o                                                      \
+       frame.o                                                          \
        hmac.o                                                           \
        imgutils.o                                                       \
        intfloat_readwrite.o                                             \
@@ -101,13 +108,20 @@
        xtea.o                                                           \
 
 OBJS-$(CONFIG_LZO)                      += lzo.o
+OBJS-$(CONFIG_OPENCL)                   += opencl.o
 
 OBJS += $(COMPAT_OBJS:%=../compat/%)
 
 SKIPHEADERS          = old_pix_fmts.h
 
+SKIPHEADERS-$(HAVE_ATOMICS_GCC)        += atomic_gcc.h
+SKIPHEADERS-$(HAVE_ATOMICS_SUNCC)      += atomic_suncc.h
+SKIPHEADERS-$(HAVE_ATOMICS_WIN32)      += atomic_win32.h
+SKIPHEADERS-$(CONFIG_OPENCL)           += opencl.h
+
 TESTPROGS = adler32                                                     \
             aes                                                         \
+            atomic                                                      \
             avstring                                                    \
             base64                                                      \
             blowfish                                                    \
diff --git a/libavutil/atomic.c b/libavutil/atomic.c
new file mode 100644
index 0000000..04182f2
--- /dev/null
+++ b/libavutil/atomic.c
@@ -0,0 +1,123 @@
+/*
+ * Copyright (c) 2012 Ronald S. Bultje <rsbultje@gmail.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 "atomic.h"
+
+#if !HAVE_ATOMICS_NATIVE
+
+#if HAVE_PTHREADS
+
+#include <pthread.h>
+
+static pthread_mutex_t atomic_lock = PTHREAD_MUTEX_INITIALIZER;
+
+int avpriv_atomic_int_get(volatile int *ptr)
+{
+    int res;
+
+    pthread_mutex_lock(&atomic_lock);
+    res = *ptr;
+    pthread_mutex_unlock(&atomic_lock);
+
+    return res;
+}
+
+void avpriv_atomic_int_set(volatile int *ptr, int val)
+{
+    pthread_mutex_lock(&atomic_lock);
+    *ptr = val;
+    pthread_mutex_unlock(&atomic_lock);
+}
+
+int avpriv_atomic_int_add_and_fetch(volatile int *ptr, int inc)
+{
+    int res;
+
+    pthread_mutex_lock(&atomic_lock);
+    *ptr += inc;
+    res = *ptr;
+    pthread_mutex_unlock(&atomic_lock);
+
+    return res;
+}
+
+void *avpriv_atomic_ptr_cas(void * volatile *ptr, void *oldval, void *newval)
+{
+    void *ret;
+    pthread_mutex_lock(&atomic_lock);
+    ret = *ptr;
+    if (*ptr == oldval)
+        *ptr = newval;
+    pthread_mutex_unlock(&atomic_lock);
+    return ret;
+}
+
+#elif !HAVE_THREADS
+
+int avpriv_atomic_int_get(volatile int *ptr)
+{
+    return *ptr;
+}
+
+void avpriv_atomic_int_set(volatile int *ptr, int val)
+{
+    *ptr = val;
+}
+
+int avpriv_atomic_int_add_and_fetch(volatile int *ptr, int inc)
+{
+    *ptr += inc;
+    return *ptr;
+}
+
+void *avpriv_atomic_ptr_cas(void * volatile *ptr, void *oldval, void *newval)
+{
+    if (*ptr == oldval) {
+        *ptr = newval;
+        return oldval;
+    }
+    return *ptr;
+}
+
+#else
+
+#error "Threading is enabled, but there is no implementation of atomic operations available"
+
+#endif /* HAVE_PTHREADS */
+
+#endif /* !HAVE_MEMORYBARRIER && !HAVE_SYNC_VAL_COMPARE_AND_SWAP && !HAVE_MACHINE_RW_BARRIER */
+
+#ifdef TEST
+#include "avassert.h"
+
+int main(void)
+{
+    volatile int val = 1;
+    int res;
+
+    res = avpriv_atomic_int_add_and_fetch(&val, 1);
+    av_assert0(res == 2);
+    avpriv_atomic_int_set(&val, 3);
+    res = avpriv_atomic_int_get(&val);
+    av_assert0(res == 3);
+
+    return 0;
+}
+#endif
diff --git a/libavutil/atomic.h b/libavutil/atomic.h
new file mode 100644
index 0000000..fd7d8fc
--- /dev/null
+++ b/libavutil/atomic.h
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2012 Ronald S. Bultje <rsbultje@gmail.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
+ */
+
+#ifndef AVUTIL_ATOMIC_H
+#define AVUTIL_ATOMIC_H
+
+#include "config.h"
+
+#if HAVE_ATOMICS_GCC
+#include "atomic_gcc.h"
+#elif HAVE_ATOMICS_WIN32
+#include "atomic_win32.h"
+#elif HAVE_ATOMICS_SUNCC
+#include "atomic_suncc.h"
+#else
+
+/**
+ * Load the current value stored in an atomic integer.
+ *
+ * @param ptr atomic integer
+ * @return the current value of the atomic integer
+ * @note This acts as a memory barrier.
+ */
+int avpriv_atomic_int_get(volatile int *ptr);
+
+/**
+ * Store a new value in an atomic integer.
+ *
+ * @param ptr atomic integer
+ * @param val the value to store in the atomic integer
+ * @note This acts as a memory barrier.
+ */
+void avpriv_atomic_int_set(volatile int *ptr, int val);
+
+/**
+ * Add a value to an atomic integer.
+ *
+ * @param ptr atomic integer
+ * @param inc the value to add to the atomic integer (may be negative)
+ * @return the new value of the atomic integer.
+ * @note This does NOT act as a memory barrier. This is primarily
+ *       intended for reference counting.
+ */
+int avpriv_atomic_int_add_and_fetch(volatile int *ptr, int inc);
+
+/**
+ * Atomic pointer compare and swap.
+ *
+ * @param ptr pointer to the pointer to operate on
+ * @param oldval do the swap if the current value of *ptr equals to oldval
+ * @param newval value to replace *ptr with
+ * @return the value of *ptr before comparison
+ */
+void *avpriv_atomic_ptr_cas(void * volatile *ptr, void *oldval, void *newval);
+
+#endif /* HAVE_MEMORYBARRIER */
+#endif /* AVUTIL_ATOMIC_H */
diff --git a/libavutil/atomic_gcc.h b/libavutil/atomic_gcc.h
new file mode 100644
index 0000000..2bb43c3
--- /dev/null
+++ b/libavutil/atomic_gcc.h
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2012 Ronald S. Bultje <rsbultje@gmail.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
+ */
+
+#ifndef AVUTIL_ATOMIC_GCC_H
+#define AVUTIL_ATOMIC_GCC_H
+
+#include <stdint.h>
+
+#include "atomic.h"
+
+#define avpriv_atomic_int_get atomic_int_get_gcc
+static inline int atomic_int_get_gcc(volatile int *ptr)
+{
+    __sync_synchronize();
+    return *ptr;
+}
+
+#define avpriv_atomic_int_set atomic_int_set_gcc
+static inline void atomic_int_set_gcc(volatile int *ptr, int val)
+{
+    *ptr = val;
+    __sync_synchronize();
+}
+
+#define avpriv_atomic_int_add_and_fetch atomic_int_add_and_fetch_gcc
+static inline int atomic_int_add_and_fetch_gcc(volatile int *ptr, int inc)
+{
+    return __sync_add_and_fetch(ptr, inc);
+}
+
+#define avpriv_atomic_ptr_cas atomic_ptr_cas_gcc
+static inline void *atomic_ptr_cas_gcc(void * volatile *ptr,
+                                       void *oldval, void *newval)
+{
+#ifdef __ARMCC_VERSION
+    // armcc will throw an error if ptr is not an integer type
+    volatile uintptr_t *tmp = (volatile uintptr_t*)ptr;
+    return (void*)__sync_val_compare_and_swap(tmp, oldval, newval);
+#else
+    return __sync_val_compare_and_swap(ptr, oldval, newval);
+#endif
+}
+
+#endif /* AVUTIL_ATOMIC_GCC_H */
diff --git a/libavutil/atomic_suncc.h b/libavutil/atomic_suncc.h
new file mode 100644
index 0000000..3cad24a
--- /dev/null
+++ b/libavutil/atomic_suncc.h
@@ -0,0 +1,55 @@
+/*
+ *
+ * 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 AVUTIL_ATOMIC_SUNCC_H
+#define AVUTIL_ATOMIC_SUNCC_H
+
+#include <atomic.h>
+#include <mbarrier.h>
+
+#include "atomic.h"
+
+#define avpriv_atomic_int_get atomic_int_get_suncc
+static inline int atomic_int_get_suncc(volatile int *ptr)
+{
+    __machine_rw_barrier();
+    return *ptr;
+}
+
+#define avpriv_atomic_int_set atomic_int_set_suncc
+static inline void atomic_int_set_suncc(volatile int *ptr, int val)
+{
+    *ptr = val;
+    __machine_rw_barrier();
+}
+
+#define avpriv_atomic_int_add_and_fetch atomic_int_add_and_fetch_suncc
+static inline int atomic_int_add_and_fetch_suncc(volatile int *ptr, int inc)
+{
+    return atomic_add_int_nv(ptr, inc);
+}
+
+#define avpriv_atomic_ptr_cas atomic_ptr_cas_suncc
+static inline void *atomic_ptr_cas_suncc(void * volatile *ptr,
+                                         void *oldval, void *newval)
+{
+    return atomic_cas_ptr(ptr, oldval, newval);
+}
+
+#endif /* AVUTIL_ATOMIC_SUNCC_H */
diff --git a/libavutil/atomic_win32.h b/libavutil/atomic_win32.h
new file mode 100644
index 0000000..f4ee078
--- /dev/null
+++ b/libavutil/atomic_win32.h
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2012 Ronald S. Bultje <rsbultje@gmail.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
+ */
+
+#ifndef AVUTIL_ATOMIC_WIN32_H
+#define AVUTIL_ATOMIC_WIN32_H
+
+#include <windows.h>
+
+#include "atomic.h"
+
+#define avpriv_atomic_int_get atomic_int_get_win32
+static inline int atomic_int_get_win32(volatile int *ptr)
+{
+    MemoryBarrier();
+    return *ptr;
+}
+
+#define avpriv_atomic_int_set atomic_int_set_win32
+static inline void atomic_int_set_win32(volatile int *ptr, int val)
+{
+    *ptr = val;
+    MemoryBarrier();
+}
+
+#define avpriv_atomic_int_add_and_fetch atomic_int_add_and_fetch_win32
+static inline int atomic_int_add_and_fetch_win32(volatile int *ptr, int inc)
+{
+    return inc + InterlockedExchangeAdd(ptr, inc);
+}
+
+#define avpriv_atomic_ptr_cas atomic_ptr_cas_win32
+static inline void *atomic_ptr_cas_win32(void * volatile *ptr,
+                                         void *oldval, void *newval)
+{
+    return InterlockedCompareExchangePointer(ptr, newval, oldval);
+}
+
+#endif /* AVUTIL_ATOMIC_WIN32_H */
diff --git a/libavutil/audio_fifo.h b/libavutil/audio_fifo.h
index 8c76388..55a538e 100644
--- a/libavutil/audio_fifo.h
+++ b/libavutil/audio_fifo.h
@@ -85,7 +85,8 @@
  * @param data        audio data plane pointers
  * @param nb_samples  number of samples to write
  * @return            number of samples actually written, or negative AVERROR
- *                    code on failure.
+ *                    code on failure. If successful, the number of samples
+ *                    actually written will always be nb_samples.
  */
 int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples);
 
@@ -99,7 +100,9 @@
  * @param data        audio data plane pointers
  * @param nb_samples  number of samples to read
  * @return            number of samples actually read, or negative AVERROR code
- *                    on failure.
+ *                    on failure. The number of samples actually read will not
+ *                    be greater than nb_samples, and will only be less than
+ *                    nb_samples if av_audio_fifo_size is less than nb_samples.
  */
 int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples);
 
diff --git a/libavutil/avstring.c b/libavutil/avstring.c
index e2422c0..cf9be2a 100644
--- a/libavutil/avstring.c
+++ b/libavutil/avstring.c
@@ -23,12 +23,12 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <string.h>
-#include <ctype.h>
 
 #include "config.h"
 #include "common.h"
 #include "mem.h"
 #include "avstring.h"
+#include "bprint.h"
 
 int av_strstart(const char *str, const char *pfx, const char **ptr)
 {
@@ -43,7 +43,7 @@
 
 int av_stristart(const char *str, const char *pfx, const char **ptr)
 {
-    while (*pfx && toupper((unsigned)*pfx) == toupper((unsigned)*str)) {
+    while (*pfx && av_toupper((unsigned)*pfx) == av_toupper((unsigned)*str)) {
         pfx++;
         str++;
     }
@@ -268,6 +268,45 @@
     return path;
 }
 
+int av_escape(char **dst, const char *src, const char *special_chars,
+              enum AVEscapeMode mode, int flags)
+{
+    AVBPrint dstbuf;
+
+    av_bprint_init(&dstbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
+    av_bprint_escape(&dstbuf, src, special_chars, mode, flags);
+
+    if (!av_bprint_is_complete(&dstbuf)) {
+        av_bprint_finalize(&dstbuf, NULL);
+        return AVERROR(ENOMEM);
+    } else {
+        av_bprint_finalize(&dstbuf, dst);
+        return dstbuf.len;
+    }
+}
+
+int av_isdigit(int c)
+{
+    return c >= '0' && c <= '9';
+}
+
+int av_isgraph(int c)
+{
+    return c > 32 && c < 127;
+}
+
+int av_isspace(int c)
+{
+    return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ||
+           c == '\v';
+}
+
+int av_isxdigit(int c)
+{
+    c = av_tolower(c);
+    return av_isdigit(c) || (c >= 'a' && c <= 'f');
+}
+
 #ifdef TEST
 
 int main(void)
diff --git a/libavutil/avstring.h b/libavutil/avstring.h
index b08d78e..438ef79 100644
--- a/libavutil/avstring.h
+++ b/libavutil/avstring.h
@@ -186,6 +186,21 @@
 char *av_strtok(char *s, const char *delim, char **saveptr);
 
 /**
+ * Locale-independent conversion of ASCII isdigit.
+ */
+int av_isdigit(int c);
+
+/**
+ * Locale-independent conversion of ASCII isgraph.
+ */
+int av_isgraph(int c);
+
+/**
+ * Locale-independent conversion of ASCII isspace.
+ */
+int av_isspace(int c);
+
+/**
  * Locale-independent conversion of ASCII characters to uppercase.
  */
 static inline int av_toupper(int c)
@@ -206,6 +221,11 @@
 }
 
 /**
+ * Locale-independent conversion of ASCII isxdigit.
+ */
+int av_isxdigit(int c);
+
+/**
  * Locale-independent case-insensitive compare.
  * @note This means only ASCII-range characters are case-insensitive
  */
@@ -233,6 +253,48 @@
  */
 const char *av_dirname(char *path);
 
+enum AVEscapeMode {
+    AV_ESCAPE_MODE_AUTO,      ///< Use auto-selected escaping mode.
+    AV_ESCAPE_MODE_BACKSLASH, ///< Use backslash escaping.
+    AV_ESCAPE_MODE_QUOTE,     ///< Use single-quote escaping.
+};
+
+/**
+ * Consider spaces special and escape them even in the middle of the
+ * string.
+ *
+ * This is equivalent to adding the whitespace characters to the special
+ * characters lists, except it is guaranteed to use the exact same list
+ * of whitespace characters as the rest of libavutil.
+ */
+#define AV_ESCAPE_FLAG_WHITESPACE 0x01
+
+/**
+ * Escape only specified special characters.
+ * Without this flag, escape also any characters that may be considered
+ * special by av_get_token(), such as the single quote.
+ */
+#define AV_ESCAPE_FLAG_STRICT 0x02
+
+/**
+ * Escape string in src, and put the escaped string in an allocated
+ * string in *dst, which must be freed with av_free().
+ *
+ * @param dst           pointer where an allocated string is put
+ * @param src           string to escape, must be non-NULL
+ * @param special_chars string containing the special characters which
+ *                      need to be escaped, can be NULL
+ * @param mode          escape mode to employ, see AV_ESCAPE_MODE_* macros.
+ *                      Any unknown value for mode will be considered equivalent to
+ *                      AV_ESCAPE_MODE_BACKSLASH, but this behaviour can change without
+ *                      notice.
+ * @param flags         flags which control how to escape, see AV_ESCAPE_FLAG_ macros
+ * @return the length of the allocated string, or a negative error code in case of error
+ * @see av_bprint_escape()
+ */
+int av_escape(char **dst, const char *src, const char *special_chars,
+              enum AVEscapeMode mode, int flags);
+
 /**
  * @}
  */
diff --git a/libavutil/avutil.h b/libavutil/avutil.h
index 78deff1..6f307d6 100644
--- a/libavutil/avutil.h
+++ b/libavutil/avutil.h
@@ -253,6 +253,27 @@
 }
 
 /**
+ * Compute the length of an integer list.
+ *
+ * @param elsize  size in bytes of each list element (only 1, 2, 4 or 8)
+ * @param term    list terminator (usually 0 or -1)
+ * @param list    pointer to the list
+ * @return  length of the list, in elements, not counting the terminator
+ */
+unsigned av_int_list_length_for_size(unsigned elsize,
+                                     const void *list, uint64_t term) av_pure;
+
+/**
+ * Compute the length of an integer list.
+ *
+ * @param term  list terminator (usually 0 or -1)
+ * @param list  pointer to the list
+ * @return  length of the list, in elements, not counting the terminator
+ */
+#define av_int_list_length(list, term) \
+    av_int_list_length_for_size(sizeof(*(list)), list, term)
+
+/**
  * @}
  * @}
  */
diff --git a/libavutil/bprint.c b/libavutil/bprint.c
index 4684ab4..fd7611a 100644
--- a/libavutil/bprint.c
+++ b/libavutil/bprint.c
@@ -23,6 +23,7 @@
 #include <string.h>
 #include <time.h>
 #include "avassert.h"
+#include "avstring.h"
 #include "bprint.h"
 #include "common.h"
 #include "error.h"
@@ -217,6 +218,50 @@
     return ret;
 }
 
+#define WHITESPACES " \n\t"
+
+void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_chars,
+                      enum AVEscapeMode mode, int flags)
+{
+    const char *src0 = src;
+
+    if (mode == AV_ESCAPE_MODE_AUTO)
+        mode = AV_ESCAPE_MODE_BACKSLASH; /* TODO: implement a heuristic */
+
+    switch (mode) {
+    case AV_ESCAPE_MODE_QUOTE:
+        /* enclose the string between '' */
+        av_bprint_chars(dstbuf, '\'', 1);
+        for (; *src; src++) {
+            if (*src == '\'')
+                av_bprintf(dstbuf, "'\\''");
+            else
+                av_bprint_chars(dstbuf, *src, 1);
+        }
+        av_bprint_chars(dstbuf, '\'', 1);
+        break;
+
+    /* case AV_ESCAPE_MODE_BACKSLASH or unknown mode */
+    default:
+        /* \-escape characters */
+        for (; *src; src++) {
+            int is_first_last       = src == src0 || !*(src+1);
+            int is_ws               = !!strchr(WHITESPACES, *src);
+            int is_strictly_special = special_chars && strchr(special_chars, *src);
+            int is_special          =
+                is_strictly_special || strchr("'\\", *src) ||
+                (is_ws && (flags & AV_ESCAPE_FLAG_WHITESPACE));
+
+            if (is_strictly_special ||
+                (!(flags & AV_ESCAPE_FLAG_STRICT) &&
+                 (is_special || (is_ws && is_first_last))))
+                av_bprint_chars(dstbuf, '\\', 1);
+            av_bprint_chars(dstbuf, *src, 1);
+        }
+        break;
+    }
+}
+
 #ifdef TEST
 
 #undef printf
diff --git a/libavutil/bprint.h b/libavutil/bprint.h
index f3915fe..df78916 100644
--- a/libavutil/bprint.h
+++ b/libavutil/bprint.h
@@ -22,6 +22,7 @@
 #define AVUTIL_BPRINT_H
 
 #include "attributes.h"
+#include "avstring.h"
 
 /**
  * Define a structure with extra padding to a fixed size
@@ -180,4 +181,20 @@
  */
 int av_bprint_finalize(AVBPrint *buf, char **ret_str);
 
+/**
+ * Escape the content in src and append it to dstbuf.
+ *
+ * @param dstbuf        already inited destination bprint buffer
+ * @param src           string containing the text to escape
+ * @param special_chars string containing the special characters which
+ *                      need to be escaped, can be NULL
+ * @param mode          escape mode to employ, see AV_ESCAPE_MODE_* macros.
+ *                      Any unknown value for mode will be considered equivalent to
+ *                      AV_ESCAPE_MODE_BACKSLASH, but this behaviour can change without
+ *                      notice.
+ * @param flags         flags which control how to escape, see AV_ESCAPE_FLAG_* macros
+ */
+void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_chars,
+                      enum AVEscapeMode mode, int flags);
+
 #endif /* AVUTIL_BPRINT_H */
diff --git a/libavutil/buffer.c b/libavutil/buffer.c
new file mode 100644
index 0000000..a5fc8d7
--- /dev/null
+++ b/libavutil/buffer.c
@@ -0,0 +1,358 @@
+/*
+ * 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 <stdint.h>
+#include <string.h>
+
+#include "atomic.h"
+#include "buffer_internal.h"
+#include "common.h"
+#include "mem.h"
+
+AVBufferRef *av_buffer_create(uint8_t *data, int size,
+                              void (*free)(void *opaque, uint8_t *data),
+                              void *opaque, int flags)
+{
+    AVBufferRef *ref = NULL;
+    AVBuffer    *buf = NULL;
+
+    buf = av_mallocz(sizeof(*buf));
+    if (!buf)
+        return NULL;
+
+    buf->data     = data;
+    buf->size     = size;
+    buf->free     = free ? free : av_buffer_default_free;
+    buf->opaque   = opaque;
+    buf->refcount = 1;
+
+    if (flags & AV_BUFFER_FLAG_READONLY)
+        buf->flags |= BUFFER_FLAG_READONLY;
+
+    ref = av_mallocz(sizeof(*ref));
+    if (!ref) {
+        av_freep(&buf);
+        return NULL;
+    }
+
+    ref->buffer = buf;
+    ref->data   = data;
+    ref->size   = size;
+
+    return ref;
+}
+
+void av_buffer_default_free(void *opaque, uint8_t *data)
+{
+    av_free(data);
+}
+
+AVBufferRef *av_buffer_alloc(int size)
+{
+    AVBufferRef *ret = NULL;
+    uint8_t    *data = NULL;
+
+    data = av_malloc(size);
+    if (!data)
+        return NULL;
+
+    ret = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
+    if (!ret)
+        av_freep(&data);
+
+    return ret;
+}
+
+AVBufferRef *av_buffer_allocz(int size)
+{
+    AVBufferRef *ret = av_buffer_alloc(size);
+    if (!ret)
+        return NULL;
+
+    memset(ret->data, 0, size);
+    return ret;
+}
+
+AVBufferRef *av_buffer_ref(AVBufferRef *buf)
+{
+    AVBufferRef *ret = av_mallocz(sizeof(*ret));
+
+    if (!ret)
+        return NULL;
+
+    *ret = *buf;
+
+    avpriv_atomic_int_add_and_fetch(&buf->buffer->refcount, 1);
+
+    return ret;
+}
+
+void av_buffer_unref(AVBufferRef **buf)
+{
+    AVBuffer *b;
+
+    if (!buf || !*buf)
+        return;
+    b = (*buf)->buffer;
+    av_freep(buf);
+
+    if (!avpriv_atomic_int_add_and_fetch(&b->refcount, -1)) {
+        b->free(b->opaque, b->data);
+        av_freep(&b);
+    }
+}
+
+int av_buffer_is_writable(const AVBufferRef *buf)
+{
+    if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)
+        return 0;
+
+    return avpriv_atomic_int_get(&buf->buffer->refcount) == 1;
+}
+
+void *av_buffer_get_opaque(const AVBufferRef *buf)
+{
+    return buf->buffer->opaque;
+}
+
+int av_buffer_get_ref_count(const AVBufferRef *buf)
+{
+    return buf->buffer->refcount;
+}
+
+int av_buffer_make_writable(AVBufferRef **pbuf)
+{
+    AVBufferRef *newbuf, *buf = *pbuf;
+
+    if (av_buffer_is_writable(buf))
+        return 0;
+
+    newbuf = av_buffer_alloc(buf->size);
+    if (!newbuf)
+        return AVERROR(ENOMEM);
+
+    memcpy(newbuf->data, buf->data, buf->size);
+    av_buffer_unref(pbuf);
+    *pbuf = newbuf;
+
+    return 0;
+}
+
+int av_buffer_realloc(AVBufferRef **pbuf, int size)
+{
+    AVBufferRef *buf = *pbuf;
+    uint8_t *tmp;
+
+    if (!buf) {
+        /* allocate a new buffer with av_realloc(), so it will be reallocatable
+         * later */
+        uint8_t *data = av_realloc(NULL, size);
+        if (!data)
+            return AVERROR(ENOMEM);
+
+        buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
+        if (!buf) {
+            av_freep(&data);
+            return AVERROR(ENOMEM);
+        }
+
+        buf->buffer->flags |= BUFFER_FLAG_REALLOCATABLE;
+        *pbuf = buf;
+
+        return 0;
+    } else if (buf->size == size)
+        return 0;
+
+    if (!(buf->buffer->flags & BUFFER_FLAG_REALLOCATABLE) ||
+        !av_buffer_is_writable(buf)) {
+        /* cannot realloc, allocate a new reallocable buffer and copy data */
+        AVBufferRef *new = NULL;
+
+        av_buffer_realloc(&new, size);
+        if (!new)
+            return AVERROR(ENOMEM);
+
+        memcpy(new->data, buf->data, FFMIN(size, buf->size));
+
+        av_buffer_unref(pbuf);
+        *pbuf = new;
+        return 0;
+    }
+
+    tmp = av_realloc(buf->buffer->data, size);
+    if (!tmp)
+        return AVERROR(ENOMEM);
+
+    buf->buffer->data = buf->data = tmp;
+    buf->buffer->size = buf->size = size;
+    return 0;
+}
+
+AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size))
+{
+    AVBufferPool *pool = av_mallocz(sizeof(*pool));
+    if (!pool)
+        return NULL;
+
+    pool->size     = size;
+    pool->alloc    = alloc ? alloc : av_buffer_alloc;
+
+    avpriv_atomic_int_set(&pool->refcount, 1);
+
+    return pool;
+}
+
+/*
+ * This function gets called when the pool has been uninited and
+ * all the buffers returned to it.
+ */
+static void buffer_pool_free(AVBufferPool *pool)
+{
+    while (pool->pool) {
+        BufferPoolEntry *buf = pool->pool;
+        pool->pool = buf->next;
+
+        buf->free(buf->opaque, buf->data);
+        av_freep(&buf);
+    }
+    av_freep(&pool);
+}
+
+void av_buffer_pool_uninit(AVBufferPool **ppool)
+{
+    AVBufferPool *pool;
+
+    if (!ppool || !*ppool)
+        return;
+    pool   = *ppool;
+    *ppool = NULL;
+
+    if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1))
+        buffer_pool_free(pool);
+}
+
+/* remove the whole buffer list from the pool and return it */
+static BufferPoolEntry *get_pool(AVBufferPool *pool)
+{
+    BufferPoolEntry *cur = *(void * volatile *)&pool->pool, *last = NULL;
+
+    while (cur != last) {
+        last = cur;
+        cur = avpriv_atomic_ptr_cas((void * volatile *)&pool->pool, last, NULL);
+        if (!cur)
+            return NULL;
+    }
+
+    return cur;
+}
+
+static void add_to_pool(BufferPoolEntry *buf)
+{
+    AVBufferPool *pool;
+    BufferPoolEntry *cur, *end = buf;
+
+    if (!buf)
+        return;
+    pool = buf->pool;
+
+    while (end->next)
+        end = end->next;
+
+    while (avpriv_atomic_ptr_cas((void * volatile *)&pool->pool, NULL, buf)) {
+        /* pool is not empty, retrieve it and append it to our list */
+        cur = get_pool(pool);
+        end->next = cur;
+        while (end->next)
+            end = end->next;
+    }
+}
+
+static void pool_release_buffer(void *opaque, uint8_t *data)
+{
+    BufferPoolEntry *buf = opaque;
+    AVBufferPool *pool = buf->pool;
+
+    if(CONFIG_MEMORY_POISONING)
+        memset(buf->data, 0x2a, pool->size);
+
+    add_to_pool(buf);
+    if (!avpriv_atomic_int_add_and_fetch(&pool->refcount, -1))
+        buffer_pool_free(pool);
+}
+
+/* allocate a new buffer and override its free() callback so that
+ * it is returned to the pool on free */
+static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
+{
+    BufferPoolEntry *buf;
+    AVBufferRef     *ret;
+
+    ret = pool->alloc(pool->size);
+    if (!ret)
+        return NULL;
+
+    buf = av_mallocz(sizeof(*buf));
+    if (!buf) {
+        av_buffer_unref(&ret);
+        return NULL;
+    }
+
+    buf->data   = ret->buffer->data;
+    buf->opaque = ret->buffer->opaque;
+    buf->free   = ret->buffer->free;
+    buf->pool   = pool;
+
+    ret->buffer->opaque = buf;
+    ret->buffer->free   = pool_release_buffer;
+
+    avpriv_atomic_int_add_and_fetch(&pool->refcount, 1);
+    avpriv_atomic_int_add_and_fetch(&pool->nb_allocated, 1);
+
+    return ret;
+}
+
+AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
+{
+    AVBufferRef *ret;
+    BufferPoolEntry *buf;
+
+    /* check whether the pool is empty */
+    buf = get_pool(pool);
+    if (!buf && pool->refcount <= pool->nb_allocated) {
+        av_log(NULL, AV_LOG_DEBUG, "Pool race dectected, spining to avoid overallocation and eventual OOM\n");
+        while (!buf && avpriv_atomic_int_get(&pool->refcount) <= avpriv_atomic_int_get(&pool->nb_allocated))
+            buf = get_pool(pool);
+    }
+
+    if (!buf)
+        return pool_alloc_buffer(pool);
+
+    /* keep the first entry, return the rest of the list to the pool */
+    add_to_pool(buf->next);
+    buf->next = NULL;
+
+    ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
+                           buf, 0);
+    if (!ret) {
+        add_to_pool(buf);
+        return NULL;
+    }
+    avpriv_atomic_int_add_and_fetch(&pool->refcount, 1);
+
+    return ret;
+}
diff --git a/libavutil/buffer.h b/libavutil/buffer.h
new file mode 100644
index 0000000..bcd1130
--- /dev/null
+++ b/libavutil/buffer.h
@@ -0,0 +1,274 @@
+/*
+ * 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
+ * @ingroup lavu_buffer
+ * refcounted data buffer API
+ */
+
+#ifndef AVUTIL_BUFFER_H
+#define AVUTIL_BUFFER_H
+
+#include <stdint.h>
+
+/**
+ * @defgroup lavu_buffer AVBuffer
+ * @ingroup lavu_data
+ *
+ * @{
+ * AVBuffer is an API for reference-counted data buffers.
+ *
+ * There are two core objects in this API -- AVBuffer and AVBufferRef. AVBuffer
+ * represents the data buffer itself; it is opaque and not meant to be accessed
+ * by the caller directly, but only through AVBufferRef. However, the caller may
+ * e.g. compare two AVBuffer pointers to check whether two different references
+ * are describing the same data buffer. AVBufferRef represents a single
+ * reference to an AVBuffer and it is the object that may be manipulated by the
+ * caller directly.
+ *
+ * There are two functions provided for creating a new AVBuffer with a single
+ * reference -- av_buffer_alloc() to just allocate a new buffer, and
+ * av_buffer_create() to wrap an existing array in an AVBuffer. From an existing
+ * reference, additional references may be created with av_buffer_ref().
+ * Use av_buffer_unref() to free a reference (this will automatically free the
+ * data once all the references are freed).
+ *
+ * The convention throughout this API and the rest of FFmpeg is such that the
+ * buffer is considered writable if there exists only one reference to it (and
+ * it has not been marked as read-only). The av_buffer_is_writable() function is
+ * provided to check whether this is true and av_buffer_make_writable() will
+ * automatically create a new writable buffer when necessary.
+ * Of course nothing prevents the calling code from violating this convention,
+ * however that is safe only when all the existing references are under its
+ * control.
+ *
+ * @note Referencing and unreferencing the buffers is thread-safe and thus
+ * may be done from multiple threads simultaneously without any need for
+ * additional locking.
+ *
+ * @note Two different references to the same buffer can point to different
+ * parts of the buffer (i.e. their AVBufferRef.data will not be equal).
+ */
+
+/**
+ * A reference counted buffer type. It is opaque and is meant to be used through
+ * references (AVBufferRef).
+ */
+typedef struct AVBuffer AVBuffer;
+
+/**
+ * A reference to a data buffer.
+ *
+ * The size of this struct is not a part of the public ABI and it is not meant
+ * to be allocated directly.
+ */
+typedef struct AVBufferRef {
+    AVBuffer *buffer;
+
+    /**
+     * The data buffer. It is considered writable if and only if
+     * this is the only reference to the buffer, in which case
+     * av_buffer_is_writable() returns 1.
+     */
+    uint8_t *data;
+    /**
+     * Size of data in bytes.
+     */
+    int      size;
+} AVBufferRef;
+
+/**
+ * Allocate an AVBuffer of the given size using av_malloc().
+ *
+ * @return an AVBufferRef of given size or NULL when out of memory
+ */
+AVBufferRef *av_buffer_alloc(int size);
+
+/**
+ * Same as av_buffer_alloc(), except the returned buffer will be initialized
+ * to zero.
+ */
+AVBufferRef *av_buffer_allocz(int size);
+
+/**
+ * Always treat the buffer as read-only, even when it has only one
+ * reference.
+ */
+#define AV_BUFFER_FLAG_READONLY (1 << 0)
+
+/**
+ * Create an AVBuffer from an existing array.
+ *
+ * If this function is successful, data is owned by the AVBuffer. The caller may
+ * only access data through the returned AVBufferRef and references derived from
+ * it.
+ * If this function fails, data is left untouched.
+ * @param data   data array
+ * @param size   size of data in bytes
+ * @param free   a callback for freeing data
+ * @param opaque parameter to be got for processing or passed to free
+ * @param flags  a combination of AV_BUFFER_FLAG_*
+ *
+ * @return an AVBufferRef referring to data on success, NULL on failure.
+ */
+AVBufferRef *av_buffer_create(uint8_t *data, int size,
+                              void (*free)(void *opaque, uint8_t *data),
+                              void *opaque, int flags);
+
+/**
+ * Default free callback, which calls av_free() on the buffer data.
+ * This function is meant to be passed to av_buffer_create(), not called
+ * directly.
+ */
+void av_buffer_default_free(void *opaque, uint8_t *data);
+
+/**
+ * Create a new reference to an AVBuffer.
+ *
+ * @return a new AVBufferRef referring to the same AVBuffer as buf or NULL on
+ * failure.
+ */
+AVBufferRef *av_buffer_ref(AVBufferRef *buf);
+
+/**
+ * Free a given reference and automatically free the buffer if there are no more
+ * references to it.
+ *
+ * @param buf the reference to be freed. The pointer is set to NULL on return.
+ */
+void av_buffer_unref(AVBufferRef **buf);
+
+/**
+ * @return 1 if the caller may write to the data referred to by buf (which is
+ * true if and only if buf is the only reference to the underlying AVBuffer).
+ * Return 0 otherwise.
+ * A positive answer is valid until av_buffer_ref() is called on buf.
+ */
+int av_buffer_is_writable(const AVBufferRef *buf);
+
+/**
+ * @return the opaque parameter set by av_buffer_create.
+ */
+void *av_buffer_get_opaque(const AVBufferRef *buf);
+
+int av_buffer_get_ref_count(const AVBufferRef *buf);
+
+/**
+ * Create a writable reference from a given buffer reference, avoiding data copy
+ * if possible.
+ *
+ * @param buf buffer reference to make writable. On success, buf is either left
+ *            untouched, or it is unreferenced and a new writable AVBufferRef is
+ *            written in its place. On failure, buf is left untouched.
+ * @return 0 on success, a negative AVERROR on failure.
+ */
+int av_buffer_make_writable(AVBufferRef **buf);
+
+/**
+ * Reallocate a given buffer.
+ *
+ * @param buf  a buffer reference to reallocate. On success, buf will be
+ *             unreferenced and a new reference with the required size will be
+ *             written in its place. On failure buf will be left untouched. *buf
+ *             may be NULL, then a new buffer is allocated.
+ * @param size required new buffer size.
+ * @return 0 on success, a negative AVERROR on failure.
+ *
+ * @note the buffer is actually reallocated with av_realloc() only if it was
+ * initially allocated through av_buffer_realloc(NULL) and there is only one
+ * reference to it (i.e. the one passed to this function). In all other cases
+ * a new buffer is allocated and the data is copied.
+ */
+int av_buffer_realloc(AVBufferRef **buf, int size);
+
+/**
+ * @}
+ */
+
+/**
+ * @defgroup lavu_bufferpool AVBufferPool
+ * @ingroup lavu_data
+ *
+ * @{
+ * AVBufferPool is an API for a lock-free thread-safe pool of AVBuffers.
+ *
+ * Frequently allocating and freeing large buffers may be slow. AVBufferPool is
+ * meant to solve this in cases when the caller needs a set of buffers of the
+ * same size (the most obvious use case being buffers for raw video or audio
+ * frames).
+ *
+ * At the beginning, the user must call av_buffer_pool_init() to create the
+ * buffer pool. Then whenever a buffer is needed, call av_buffer_pool_get() to
+ * get a reference to a new buffer, similar to av_buffer_alloc(). This new
+ * reference works in all aspects the same way as the one created by
+ * av_buffer_alloc(). However, when the last reference to this buffer is
+ * unreferenced, it is returned to the pool instead of being freed and will be
+ * reused for subsequent av_buffer_pool_get() calls.
+ *
+ * When the caller is done with the pool and no longer needs to allocate any new
+ * buffers, av_buffer_pool_uninit() must be called to mark the pool as freeable.
+ * Once all the buffers are released, it will automatically be freed.
+ *
+ * Allocating and releasing buffers with this API is thread-safe as long as
+ * either the default alloc callback is used, or the user-supplied one is
+ * thread-safe.
+ */
+
+/**
+ * The buffer pool. This structure is opaque and not meant to be accessed
+ * directly. It is allocated with av_buffer_pool_init() and freed with
+ * av_buffer_pool_uninit().
+ */
+typedef struct AVBufferPool AVBufferPool;
+
+/**
+ * Allocate and initialize a buffer pool.
+ *
+ * @param size size of each buffer in this pool
+ * @param alloc a function that will be used to allocate new buffers when the
+ * pool is empty. May be NULL, then the default allocator will be used
+ * (av_buffer_alloc()).
+ * @return newly created buffer pool on success, NULL on error.
+ */
+AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size));
+
+/**
+ * Mark the pool as being available for freeing. It will actually be freed only
+ * once all the allocated buffers associated with the pool are released. Thus it
+ * is safe to call this function while some of the allocated buffers are still
+ * in use.
+ *
+ * @param pool pointer to the pool to be freed. It will be set to NULL.
+ * @see av_buffer_pool_can_uninit()
+ */
+void av_buffer_pool_uninit(AVBufferPool **pool);
+
+/**
+ * Allocate a new AVBuffer, reusing an old buffer from the pool when available.
+ * This function may be called simultaneously from multiple threads.
+ *
+ * @return a reference to the new buffer on success, NULL on error.
+ */
+AVBufferRef *av_buffer_pool_get(AVBufferPool *pool);
+
+/**
+ * @}
+ */
+
+#endif /* AVUTIL_BUFFER_H */
diff --git a/libavutil/buffer_internal.h b/libavutil/buffer_internal.h
new file mode 100644
index 0000000..c291908
--- /dev/null
+++ b/libavutil/buffer_internal.h
@@ -0,0 +1,94 @@
+/*
+ * 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 AVUTIL_BUFFER_INTERNAL_H
+#define AVUTIL_BUFFER_INTERNAL_H
+
+#include <stdint.h>
+
+#include "buffer.h"
+
+/**
+ * The buffer is always treated as read-only.
+ */
+#define BUFFER_FLAG_READONLY      (1 << 0)
+/**
+ * The buffer was av_realloc()ed, so it is reallocatable.
+ */
+#define BUFFER_FLAG_REALLOCATABLE (1 << 1)
+
+struct AVBuffer {
+    uint8_t *data; /**< data described by this buffer */
+    int      size; /**< size of data in bytes */
+
+    /**
+     *  number of existing AVBufferRef instances referring to this buffer
+     */
+    volatile int refcount;
+
+    /**
+     * a callback for freeing the data
+     */
+    void (*free)(void *opaque, uint8_t *data);
+
+    /**
+     * an opaque pointer, to be used by the freeing callback
+     */
+    void *opaque;
+
+    /**
+     * A combination of BUFFER_FLAG_*
+     */
+    int flags;
+};
+
+typedef struct BufferPoolEntry {
+    uint8_t *data;
+
+    /*
+     * Backups of the original opaque/free of the AVBuffer corresponding to
+     * data. They will be used to free the buffer when the pool is freed.
+     */
+    void *opaque;
+    void (*free)(void *opaque, uint8_t *data);
+
+    AVBufferPool *pool;
+    struct BufferPoolEntry * volatile next;
+} BufferPoolEntry;
+
+struct AVBufferPool {
+    BufferPoolEntry * volatile pool;
+
+    /*
+     * This is used to track when the pool is to be freed.
+     * The pointer to the pool itself held by the caller is considered to
+     * be one reference. Each buffer requested by the caller increases refcount
+     * by one, returning the buffer to the pool decreases it by one.
+     * refcount reaches zero when the buffer has been uninited AND all the
+     * buffers have been released, then it's safe to free the pool and all
+     * the buffers in it.
+     */
+    volatile int refcount;
+
+    volatile int nb_allocated;
+
+    int size;
+    AVBufferRef* (*alloc)(int size);
+};
+
+#endif /* AVUTIL_BUFFER_INTERNAL_H */
diff --git a/libavutil/common.h b/libavutil/common.h
index 778c757..e303c25 100644
--- a/libavutil/common.h
+++ b/libavutil/common.h
@@ -26,7 +26,6 @@
 #ifndef AVUTIL_COMMON_H
 #define AVUTIL_COMMON_H
 
-#include <ctype.h>
 #include <errno.h>
 #include <inttypes.h>
 #include <limits.h>
@@ -230,6 +229,23 @@
     else               return a;
 }
 
+/**
+ * Clip a double value into the amin-amax range.
+ * @param a value to clip
+ * @param amin minimum value of the clip range
+ * @param amax maximum value of the clip range
+ * @return clipped value
+ */
+static av_always_inline av_const double av_clipd_c(double a, double amin, double amax)
+{
+#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
+    if (amin > amax) abort();
+#endif
+    if      (a < amin) return amin;
+    else if (a > amax) return amax;
+    else               return a;
+}
+
 /** Compute ceil(log2(x)).
  * @param x value used to compute ceil(log2(x))
  * @return computed ceiling of log2(x)
@@ -429,6 +445,9 @@
 #ifndef av_clipf
 #   define av_clipf         av_clipf_c
 #endif
+#ifndef av_clipd
+#   define av_clipd         av_clipd_c
+#endif
 #ifndef av_popcount
 #   define av_popcount      av_popcount_c
 #endif
diff --git a/libavutil/dict.c b/libavutil/dict.c
index 967c9e2..3a0e84c 100644
--- a/libavutil/dict.c
+++ b/libavutil/dict.c
@@ -18,7 +18,6 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include <ctype.h>
 #include <string.h>
 
 #include "avstring.h"
@@ -49,8 +48,8 @@
 
     for(; i<m->count; i++){
         const char *s= m->elems[i].key;
-        if(flags & AV_DICT_MATCH_CASE) for(j=0;         s[j]  ==         key[j]  && key[j]; j++);
-        else                               for(j=0; toupper(s[j]) == toupper(key[j]) && key[j]; j++);
+        if(flags & AV_DICT_MATCH_CASE) for(j=0;            s[j]  ==            key[j]  && key[j]; j++);
+        else                           for(j=0; av_toupper(s[j]) == av_toupper(key[j]) && key[j]; j++);
         if(key[j])
             continue;
         if(s[j] && !(flags & AV_DICT_IGNORE_SUFFIX))
@@ -87,9 +86,9 @@
     }
     if (value) {
         if (flags & AV_DICT_DONT_STRDUP_KEY) {
-            m->elems[m->count].key   = (char*)(intptr_t)key;
+            m->elems[m->count].key = (char*)(intptr_t)key;
         } else
-        m->elems[m->count].key  = av_strdup(key  );
+            m->elems[m->count].key = av_strdup(key);
         if (flags & AV_DICT_DONT_STRDUP_VAL) {
             m->elems[m->count].value = (char*)(intptr_t)value;
         } else if (oldval && flags & AV_DICT_APPEND) {
diff --git a/libavutil/eval.c b/libavutil/eval.c
index 5d20224..3abc3e5 100644
--- a/libavutil/eval.c
+++ b/libavutil/eval.c
@@ -33,6 +33,7 @@
 #include "log.h"
 #include "mathematics.h"
 #include "time.h"
+#include "avstring.h"
 
 typedef struct Parser {
     const AVClass *class;
@@ -140,11 +141,11 @@
     enum {
         e_value, e_const, e_func0, e_func1, e_func2,
         e_squish, e_gauss, e_ld, e_isnan, e_isinf,
-        e_mod, e_max, e_min, e_eq, e_gt, e_gte,
+        e_mod, e_max, e_min, e_eq, e_gt, e_gte, e_lte, e_lt,
         e_pow, e_mul, e_div, e_add,
         e_last, e_st, e_while, e_taylor, e_root, e_floor, e_ceil, e_trunc,
         e_sqrt, e_not, e_random, e_hypot, e_gcd,
-        e_if, e_ifnot, e_print,
+        e_if, e_ifnot, e_print, e_bitand, e_bitor, e_between,
     } type;
     double value; // is sign in other types
     union {
@@ -184,6 +185,11 @@
                                           e->param[2] ? eval_expr(p, e->param[2]) : 0);
         case e_ifnot:  return e->value * (!eval_expr(p, e->param[0]) ? eval_expr(p, e->param[1]) :
                                           e->param[2] ? eval_expr(p, e->param[2]) : 0);
+        case e_between: {
+            double d = eval_expr(p, e->param[0]);
+            return e->value * (d >= eval_expr(p, e->param[1]) &&
+                               d <= eval_expr(p, e->param[2]));
+        }
         case e_print: {
             double x = eval_expr(p, e->param[0]);
             int level = e->param[1] ? av_clip(eval_expr(p, e->param[1]), INT_MIN, INT_MAX) : AV_LOG_INFO;
@@ -274,6 +280,8 @@
                 case e_eq:  return e->value * (d == d2 ? 1.0 : 0.0);
                 case e_gt:  return e->value * (d >  d2 ? 1.0 : 0.0);
                 case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
+                case e_lt:  return e->value * (d <  d2 ? 1.0 : 0.0);
+                case e_lte: return e->value * (d <= d2 ? 1.0 : 0.0);
                 case e_pow: return e->value * pow(d, d2);
                 case e_mul: return e->value * (d * d2);
                 case e_div: return e->value * ((!CONFIG_FTRAPV || d2 ) ? (d / d2) : d * INFINITY);
@@ -281,6 +289,8 @@
                 case e_last:return e->value * d2;
                 case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
                 case e_hypot:return e->value * (sqrt(d*d + d2*d2));
+                case e_bitand: return isnan(d) || isnan(d2) ? NAN : e->value * ((long int)d & (long int)d2);
+                case e_bitor:  return isnan(d) || isnan(d2) ? NAN : e->value * ((long int)d | (long int)d2);
             }
         }
     }
@@ -400,8 +410,8 @@
     else if (strmatch(next, "eq"    )) d->type = e_eq;
     else if (strmatch(next, "gte"   )) d->type = e_gte;
     else if (strmatch(next, "gt"    )) d->type = e_gt;
-    else if (strmatch(next, "lte"   )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
-    else if (strmatch(next, "lt"    )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
+    else if (strmatch(next, "lte"   )) d->type = e_lte;
+    else if (strmatch(next, "lt"    )) d->type = e_lt;
     else if (strmatch(next, "ld"    )) d->type = e_ld;
     else if (strmatch(next, "isnan" )) d->type = e_isnan;
     else if (strmatch(next, "isinf" )) d->type = e_isinf;
@@ -421,6 +431,9 @@
     else if (strmatch(next, "gcd"   )) d->type = e_gcd;
     else if (strmatch(next, "if"    )) d->type = e_if;
     else if (strmatch(next, "ifnot" )) d->type = e_ifnot;
+    else if (strmatch(next, "bitand")) d->type = e_bitand;
+    else if (strmatch(next, "bitor" )) d->type = e_bitor;
+    else if (strmatch(next, "between"))d->type = e_between;
     else {
         for (i=0; p->func1_names && p->func1_names[i]; i++) {
             if (strmatch(next, p->func1_names[i])) {
@@ -449,7 +462,7 @@
     return 0;
 }
 
-static AVExpr *new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
+static AVExpr *make_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
 {
     AVExpr *e = av_mallocz(sizeof(AVExpr));
     if (!e)
@@ -496,7 +509,7 @@
             av_expr_free(e1);
             return ret;
         }
-        e0 = new_eval_expr(e_pow, 1, e1, e2);
+        e0 = make_eval_expr(e_pow, 1, e1, e2);
         if (!e0) {
             av_expr_free(e1);
             av_expr_free(e2);
@@ -523,7 +536,7 @@
             av_expr_free(e1);
             return ret;
         }
-        e0 = new_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2);
+        e0 = make_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2);
         if (!e0) {
             av_expr_free(e1);
             av_expr_free(e2);
@@ -546,7 +559,7 @@
             av_expr_free(e1);
             return ret;
         }
-        e0 = new_eval_expr(e_add, 1, e1, e2);
+        e0 = make_eval_expr(e_add, 1, e1, e2);
         if (!e0) {
             av_expr_free(e1);
             av_expr_free(e2);
@@ -575,7 +588,7 @@
             av_expr_free(e1);
             return ret;
         }
-        e0 = new_eval_expr(e_last, 1, e1, e2);
+        e0 = make_eval_expr(e_last, 1, e1, e2);
         if (!e0) {
             av_expr_free(e1);
             av_expr_free(e2);
@@ -616,6 +629,10 @@
         case e_taylor:
             return verify_expr(e->param[0]) && verify_expr(e->param[1])
                    && (!e->param[2] || verify_expr(e->param[2]));
+        case e_between:
+            return verify_expr(e->param[0]) &&
+                   verify_expr(e->param[1]) &&
+                   verify_expr(e->param[2]);
         default: return verify_expr(e->param[0]) && verify_expr(e->param[1]) && !e->param[2];
     }
 }
@@ -637,7 +654,7 @@
         return AVERROR(ENOMEM);
 
     while (*s)
-        if (!isspace(*s++)) *wp++ = s[-1];
+        if (!av_isspace(*s++)) *wp++ = s[-1];
     *wp++ = 0;
 
     p.class      = &class;
@@ -806,6 +823,12 @@
         "gauss(0.1)",
         "hypot(4,3)",
         "gcd(30,55)*print(min(9,1))",
+        "bitor(42, 12)",
+        "bitand(42, 12)",
+        "bitand(NAN, 1)",
+        "between(10, -3, 10)",
+        "between(-4, -2, -1)",
+        "between(1,2)",
         NULL
     };
 
diff --git a/libavutil/frame.c b/libavutil/frame.c
new file mode 100644
index 0000000..ca6814c
--- /dev/null
+++ b/libavutil/frame.c
@@ -0,0 +1,563 @@
+/*
+ *
+ * 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 "channel_layout.h"
+#include "avassert.h"
+#include "buffer.h"
+#include "common.h"
+#include "dict.h"
+#include "frame.h"
+#include "imgutils.h"
+#include "mem.h"
+#include "samplefmt.h"
+
+#define MAKE_ACCESSORS(str, name, type, field) \
+    type av_##name##_get_##field(const str *s) { return s->field; } \
+    void av_##name##_set_##field(str *s, type v) { s->field = v; }
+
+MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
+MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
+MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
+MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
+MAKE_ACCESSORS(AVFrame, frame, int,     channels)
+MAKE_ACCESSORS(AVFrame, frame, int,     sample_rate)
+MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
+MAKE_ACCESSORS(AVFrame, frame, int,     decode_error_flags)
+MAKE_ACCESSORS(AVFrame, frame, int,     pkt_size)
+
+#define CHECK_CHANNELS_CONSISTENCY(frame) \
+    av_assert2(!(frame)->channel_layout || \
+               (frame)->channels == \
+               av_get_channel_layout_nb_channels((frame)->channel_layout))
+
+AVDictionary **avpriv_frame_get_metadatap(AVFrame *frame) {return &frame->metadata;};
+
+int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int qp_type)
+{
+    av_buffer_unref(&f->qp_table_buf);
+
+    f->qp_table_buf = buf;
+
+    f->qscale_table = buf->data;
+    f->qstride      = stride;
+    f->qscale_type  = qp_type;
+
+    return 0;
+}
+
+int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type)
+{
+    *stride = f->qstride;
+    *type   = f->qscale_type;
+
+    if (!f->qp_table_buf)
+        return NULL;
+
+    return f->qp_table_buf->data;
+}
+
+static void get_frame_defaults(AVFrame *frame)
+{
+    if (frame->extended_data != frame->data)
+        av_freep(&frame->extended_data);
+
+    memset(frame, 0, sizeof(*frame));
+
+    frame->pts                   =
+    frame->pkt_dts               =
+    frame->pkt_pts               = AV_NOPTS_VALUE;
+    av_frame_set_best_effort_timestamp(frame, AV_NOPTS_VALUE);
+    av_frame_set_pkt_duration         (frame, 0);
+    av_frame_set_pkt_pos              (frame, -1);
+    av_frame_set_pkt_size             (frame, -1);
+    frame->key_frame           = 1;
+    frame->sample_aspect_ratio = (AVRational){ 0, 1 };
+    frame->format              = -1; /* unknown */
+    frame->extended_data       = frame->data;
+}
+
+AVFrame *av_frame_alloc(void)
+{
+    AVFrame *frame = av_mallocz(sizeof(*frame));
+
+    if (!frame)
+        return NULL;
+
+    frame->extended_data = NULL;
+    get_frame_defaults(frame);
+
+    return frame;
+}
+
+void av_frame_free(AVFrame **frame)
+{
+    if (!frame || !*frame)
+        return;
+
+    av_frame_unref(*frame);
+    av_freep(frame);
+}
+
+static int get_video_buffer(AVFrame *frame, int align)
+{
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
+    int ret, i;
+
+    if (!desc)
+        return AVERROR(EINVAL);
+
+    if ((ret = av_image_check_size(frame->width, frame->height, 0, NULL)) < 0)
+        return ret;
+
+    if (!frame->linesize[0]) {
+        ret = av_image_fill_linesizes(frame->linesize, frame->format,
+                                      frame->width);
+        if (ret < 0)
+            return ret;
+
+        for (i = 0; i < 4 && frame->linesize[i]; i++)
+            frame->linesize[i] = FFALIGN(frame->linesize[i], align);
+    }
+
+    for (i = 0; i < 4 && frame->linesize[i]; i++) {
+        int h = FFALIGN(frame->height, 32);
+        if (i == 1 || i == 2)
+            h = -((-h) >> desc->log2_chroma_h);
+
+        frame->buf[i] = av_buffer_alloc(frame->linesize[i] * h + 16);
+        if (!frame->buf[i])
+            goto fail;
+
+        frame->data[i] = frame->buf[i]->data;
+    }
+    if (desc->flags & PIX_FMT_PAL || desc->flags & PIX_FMT_PSEUDOPAL) {
+        av_buffer_unref(&frame->buf[1]);
+        frame->buf[1] = av_buffer_alloc(1024);
+        if (!frame->buf[1])
+            goto fail;
+        frame->data[1] = frame->buf[1]->data;
+    }
+
+    frame->extended_data = frame->data;
+
+    return 0;
+fail:
+    av_frame_unref(frame);
+    return AVERROR(ENOMEM);
+}
+
+static int get_audio_buffer(AVFrame *frame, int align)
+{
+    int channels = frame->channels;
+    int planar   = av_sample_fmt_is_planar(frame->format);
+    int planes   = planar ? channels : 1;
+    int ret, i;
+
+    CHECK_CHANNELS_CONSISTENCY(frame);
+    if (!frame->linesize[0]) {
+        ret = av_samples_get_buffer_size(&frame->linesize[0], channels,
+                                         frame->nb_samples, frame->format,
+                                         align);
+        if (ret < 0)
+            return ret;
+    }
+
+    if (planes > AV_NUM_DATA_POINTERS) {
+        frame->extended_data = av_mallocz(planes *
+                                          sizeof(*frame->extended_data));
+        frame->extended_buf  = av_mallocz((planes - AV_NUM_DATA_POINTERS) *
+                                          sizeof(*frame->extended_buf));
+        if (!frame->extended_data || !frame->extended_buf) {
+            av_freep(&frame->extended_data);
+            av_freep(&frame->extended_buf);
+            return AVERROR(ENOMEM);
+        }
+        frame->nb_extended_buf = planes - AV_NUM_DATA_POINTERS;
+    } else
+        frame->extended_data = frame->data;
+
+    for (i = 0; i < FFMIN(planes, AV_NUM_DATA_POINTERS); i++) {
+        frame->buf[i] = av_buffer_alloc(frame->linesize[0]);
+        if (!frame->buf[i]) {
+            av_frame_unref(frame);
+            return AVERROR(ENOMEM);
+        }
+        frame->extended_data[i] = frame->data[i] = frame->buf[i]->data;
+    }
+    for (i = 0; i < planes - AV_NUM_DATA_POINTERS; i++) {
+        frame->extended_buf[i] = av_buffer_alloc(frame->linesize[0]);
+        if (!frame->extended_buf[i]) {
+            av_frame_unref(frame);
+            return AVERROR(ENOMEM);
+        }
+        frame->extended_data[i + AV_NUM_DATA_POINTERS] = frame->extended_buf[i]->data;
+    }
+    return 0;
+
+}
+
+int av_frame_get_buffer(AVFrame *frame, int align)
+{
+    if (frame->format < 0)
+        return AVERROR(EINVAL);
+
+    if (frame->width > 0 && frame->height > 0)
+        return get_video_buffer(frame, align);
+    else if (frame->nb_samples > 0 && frame->channel_layout)
+        return get_audio_buffer(frame, align);
+
+    return AVERROR(EINVAL);
+}
+
+int av_frame_ref(AVFrame *dst, AVFrame *src)
+{
+    int i, ret = 0;
+
+    dst->format         = src->format;
+    dst->width          = src->width;
+    dst->height         = src->height;
+    dst->channels       = src->channels;
+    dst->channel_layout = src->channel_layout;
+    dst->nb_samples     = src->nb_samples;
+
+    ret = av_frame_copy_props(dst, src);
+    if (ret < 0)
+        return ret;
+
+    /* duplicate the frame data if it's not refcounted */
+    if (!src->buf[0]) {
+        ret = av_frame_get_buffer(dst, 32);
+        if (ret < 0)
+            return ret;
+
+        if (src->nb_samples) {
+            int ch = src->channels;
+            CHECK_CHANNELS_CONSISTENCY(src);
+            av_samples_copy(dst->extended_data, src->extended_data, 0, 0,
+                            dst->nb_samples, ch, dst->format);
+        } else {
+            av_image_copy(dst->data, dst->linesize, src->data, src->linesize,
+                          dst->format, dst->width, dst->height);
+        }
+        return 0;
+    }
+
+    /* ref the buffers */
+    for (i = 0; i < FF_ARRAY_ELEMS(src->buf) && src->buf[i]; i++) {
+        dst->buf[i] = av_buffer_ref(src->buf[i]);
+        if (!dst->buf[i]) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
+    }
+
+    if (src->extended_buf) {
+        dst->extended_buf = av_mallocz(sizeof(*dst->extended_buf) *
+                                       src->nb_extended_buf);
+        if (!dst->extended_buf) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
+        dst->nb_extended_buf = src->nb_extended_buf;
+
+        for (i = 0; i < src->nb_extended_buf; i++) {
+            dst->extended_buf[i] = av_buffer_ref(src->extended_buf[i]);
+            if (!dst->extended_buf[i]) {
+                ret = AVERROR(ENOMEM);
+                goto fail;
+            }
+        }
+    }
+
+    /* duplicate extended data */
+    if (src->extended_data != src->data) {
+        int ch = src->channels;
+
+        if (!ch) {
+            ret = AVERROR(EINVAL);
+            goto fail;
+        }
+        CHECK_CHANNELS_CONSISTENCY(src);
+
+        dst->extended_data = av_malloc(sizeof(*dst->extended_data) * ch);
+        if (!dst->extended_data) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
+        memcpy(dst->extended_data, src->extended_data, sizeof(*src->extended_data) * ch);
+    } else
+        dst->extended_data = dst->data;
+
+    memcpy(dst->data,     src->data,     sizeof(src->data));
+    memcpy(dst->linesize, src->linesize, sizeof(src->linesize));
+
+    return 0;
+
+fail:
+    av_frame_unref(dst);
+    return ret;
+}
+
+AVFrame *av_frame_clone(AVFrame *src)
+{
+    AVFrame *ret = av_frame_alloc();
+
+    if (!ret)
+        return NULL;
+
+    if (av_frame_ref(ret, src) < 0)
+        av_frame_free(&ret);
+
+    return ret;
+}
+
+void av_frame_unref(AVFrame *frame)
+{
+    int i;
+
+    for (i = 0; i < frame->nb_side_data; i++) {
+        av_freep(&frame->side_data[i]->data);
+        av_dict_free(&frame->side_data[i]->metadata);
+        av_freep(&frame->side_data[i]);
+    }
+    av_freep(&frame->side_data);
+
+    for (i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++)
+        av_buffer_unref(&frame->buf[i]);
+    for (i = 0; i < frame->nb_extended_buf; i++)
+        av_buffer_unref(&frame->extended_buf[i]);
+    av_freep(&frame->extended_buf);
+    av_dict_free(&frame->metadata);
+    av_buffer_unref(&frame->qp_table_buf);
+
+    get_frame_defaults(frame);
+}
+
+void av_frame_move_ref(AVFrame *dst, AVFrame *src)
+{
+    *dst = *src;
+    if (src->extended_data == src->data)
+        dst->extended_data = dst->data;
+    memset(src, 0, sizeof(*src));
+    get_frame_defaults(src);
+}
+
+int av_frame_is_writable(AVFrame *frame)
+{
+    int i, ret = 1;
+
+    /* assume non-refcounted frames are not writable */
+    if (!frame->buf[0])
+        return 0;
+
+    for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++)
+        ret &= !!av_buffer_is_writable(frame->buf[i]);
+    for (i = 0; i < frame->nb_extended_buf; i++)
+        ret &= !!av_buffer_is_writable(frame->extended_buf[i]);
+
+    return ret;
+}
+
+int av_frame_make_writable(AVFrame *frame)
+{
+    AVFrame tmp;
+    int ret;
+
+    if (!frame->buf[0])
+        return AVERROR(EINVAL);
+
+    if (av_frame_is_writable(frame))
+        return 0;
+
+    memset(&tmp, 0, sizeof(tmp));
+    tmp.format         = frame->format;
+    tmp.width          = frame->width;
+    tmp.height         = frame->height;
+    tmp.channels       = frame->channels;
+    tmp.channel_layout = frame->channel_layout;
+    tmp.nb_samples     = frame->nb_samples;
+    ret = av_frame_get_buffer(&tmp, 32);
+    if (ret < 0)
+        return ret;
+
+    if (tmp.nb_samples) {
+        int ch = tmp.channels;
+        CHECK_CHANNELS_CONSISTENCY(&tmp);
+        av_samples_copy(tmp.extended_data, frame->extended_data, 0, 0,
+                        frame->nb_samples, ch, frame->format);
+    } else {
+        av_image_copy(tmp.data, tmp.linesize, frame->data, frame->linesize,
+                      frame->format, frame->width, frame->height);
+    }
+
+    ret = av_frame_copy_props(&tmp, frame);
+    if (ret < 0) {
+        av_frame_unref(&tmp);
+        return ret;
+    }
+
+    av_frame_unref(frame);
+
+    *frame = tmp;
+    if (tmp.data == tmp.extended_data)
+        frame->extended_data = frame->data;
+
+    return 0;
+}
+
+int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
+{
+    int i;
+
+    dst->key_frame           = src->key_frame;
+    dst->pict_type           = src->pict_type;
+    dst->sample_aspect_ratio = src->sample_aspect_ratio;
+    dst->pts                 = src->pts;
+    dst->repeat_pict         = src->repeat_pict;
+    dst->interlaced_frame    = src->interlaced_frame;
+    dst->top_field_first     = src->top_field_first;
+    dst->palette_has_changed = src->palette_has_changed;
+    dst->sample_rate         = src->sample_rate;
+    dst->opaque              = src->opaque;
+#if FF_API_AVFRAME_LAVC
+    dst->type                = src->type;
+#endif
+    dst->pkt_pts             = src->pkt_pts;
+    dst->pkt_dts             = src->pkt_dts;
+    dst->pkt_pos             = src->pkt_pos;
+    dst->pkt_size            = src->pkt_size;
+    dst->pkt_duration        = src->pkt_duration;
+    dst->reordered_opaque    = src->reordered_opaque;
+    dst->quality             = src->quality;
+    dst->best_effort_timestamp = src->best_effort_timestamp;
+    dst->coded_picture_number = src->coded_picture_number;
+    dst->display_picture_number = src->display_picture_number;
+    dst->decode_error_flags  = src->decode_error_flags;
+
+    av_dict_copy(&dst->metadata, src->metadata, 0);
+
+    memcpy(dst->error, src->error, sizeof(dst->error));
+
+    for (i = 0; i < src->nb_side_data; i++) {
+        const AVFrameSideData *sd_src = src->side_data[i];
+        AVFrameSideData *sd_dst = av_frame_new_side_data(dst, sd_src->type,
+                                                         sd_src->size);
+        if (!sd_dst) {
+            for (i = 0; i < dst->nb_side_data; i++) {
+                av_freep(&dst->side_data[i]->data);
+                av_freep(&dst->side_data[i]);
+                av_dict_free(&dst->side_data[i]->metadata);
+            }
+            av_freep(&dst->side_data);
+            return AVERROR(ENOMEM);
+        }
+        memcpy(sd_dst->data, sd_src->data, sd_src->size);
+        av_dict_copy(&sd_dst->metadata, sd_src->metadata, 0);
+    }
+
+    dst->qscale_table = NULL;
+    dst->qstride      = 0;
+    dst->qscale_type  = 0;
+    if (src->qp_table_buf) {
+        dst->qp_table_buf = av_buffer_ref(src->qp_table_buf);
+        if (dst->qp_table_buf) {
+            dst->qscale_table = dst->qp_table_buf->data;
+            dst->qstride      = src->qstride;
+            dst->qscale_type  = src->qscale_type;
+        }
+    }
+
+    return 0;
+}
+
+AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane)
+{
+    uint8_t *data;
+    int planes, i;
+
+    if (frame->nb_samples) {
+        int channels = frame->channels;
+        if (!channels)
+            return NULL;
+        CHECK_CHANNELS_CONSISTENCY(frame);
+        planes = av_sample_fmt_is_planar(frame->format) ? channels : 1;
+    } else
+        planes = 4;
+
+    if (plane < 0 || plane >= planes || !frame->extended_data[plane])
+        return NULL;
+    data = frame->extended_data[plane];
+
+    for (i = 0; i < FF_ARRAY_ELEMS(frame->buf) && frame->buf[i]; i++) {
+        AVBufferRef *buf = frame->buf[i];
+        if (data >= buf->data && data < buf->data + buf->size)
+            return buf;
+    }
+    for (i = 0; i < frame->nb_extended_buf; i++) {
+        AVBufferRef *buf = frame->extended_buf[i];
+        if (data >= buf->data && data < buf->data + buf->size)
+            return buf;
+    }
+    return NULL;
+}
+
+AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
+                                        enum AVFrameSideDataType type,
+                                        int size)
+{
+    AVFrameSideData *ret, **tmp;
+
+    if (frame->nb_side_data > INT_MAX / sizeof(*frame->side_data) - 1)
+        return NULL;
+
+    tmp = av_realloc(frame->side_data,
+                     (frame->nb_side_data + 1) * sizeof(*frame->side_data));
+    if (!tmp)
+        return NULL;
+    frame->side_data = tmp;
+
+    ret = av_mallocz(sizeof(*ret));
+    if (!ret)
+        return NULL;
+
+    ret->data = av_malloc(size);
+    if (!ret->data) {
+        av_freep(&ret);
+        return NULL;
+    }
+
+    ret->size = size;
+    ret->type = type;
+
+    frame->side_data[frame->nb_side_data++] = ret;
+
+    return ret;
+}
+
+AVFrameSideData *av_frame_get_side_data(AVFrame *frame,
+                                        enum AVFrameSideDataType type)
+{
+    int i;
+
+    for (i = 0; i < frame->nb_side_data; i++) {
+        if (frame->side_data[i]->type == type)
+            return frame->side_data[i];
+    }
+    return NULL;
+}
diff --git a/libavutil/frame.h b/libavutil/frame.h
new file mode 100644
index 0000000..39a664f
--- /dev/null
+++ b/libavutil/frame.h
@@ -0,0 +1,607 @@
+/*
+ *
+ * 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 AVUTIL_FRAME_H
+#define AVUTIL_FRAME_H
+
+#include <stdint.h>
+
+#include "libavcodec/version.h"
+
+#include "avutil.h"
+#include "buffer.h"
+#include "dict.h"
+#include "rational.h"
+#include "samplefmt.h"
+
+enum AVFrameSideDataType {
+    /**
+     * The data is the AVPanScan struct defined in libavcodec.
+     */
+    AV_FRAME_DATA_PANSCAN,
+};
+
+typedef struct AVFrameSideData {
+    enum AVFrameSideDataType type;
+    uint8_t *data;
+    int      size;
+    AVDictionary *metadata;
+} AVFrameSideData;
+
+/**
+ * This structure describes decoded (raw) audio or video data.
+ *
+ * AVFrame must be allocated using av_frame_alloc(). Note that this only
+ * allocates the AVFrame itself, the buffers for the data must be managed
+ * through other means (see below).
+ * AVFrame must be freed with av_frame_free().
+ *
+ * AVFrame is typically allocated once and then reused multiple times to hold
+ * different data (e.g. a single AVFrame to hold frames received from a
+ * decoder). In such a case, av_frame_unref() will free any references held by
+ * the frame and reset it to its original clean state before it
+ * is reused again.
+ *
+ * The data described by an AVFrame is usually reference counted through the
+ * AVBuffer API. The underlying buffer references are stored in AVFrame.buf /
+ * AVFrame.extended_buf. An AVFrame is considered to be reference counted if at
+ * least one reference is set, i.e. if AVFrame.buf[0] != NULL. In such a case,
+ * every single data plane must be contained in one of the buffers in
+ * AVFrame.buf or AVFrame.extended_buf.
+ * There may be a single buffer for all the data, or one separate buffer for
+ * each plane, or anything in between.
+ *
+ * sizeof(AVFrame) is not a part of the public ABI, so new fields may be added
+ * to the end with a minor bump.
+ * Similarly fields that are marked as to be only accessed by
+ * av_opt_ptr() can be reordered. This allows 2 forks to add fields
+ * without breaking compatibility with each other.
+ */
+typedef struct AVFrame {
+#define AV_NUM_DATA_POINTERS 8
+    /**
+     * pointer to the picture/channel planes.
+     * This might be different from the first allocated byte
+     *
+     * Some decoders access areas outside 0,0 - width,height, please
+     * see avcodec_align_dimensions2(). Some filters and swscale can read
+     * up to 16 bytes beyond the planes, if these filters are to be used,
+     * then 16 extra bytes must be allocated.
+     */
+    uint8_t *data[AV_NUM_DATA_POINTERS];
+
+    /**
+     * For video, size in bytes of each picture line.
+     * For audio, size in bytes of each plane.
+     *
+     * For audio, only linesize[0] may be set. For planar audio, each channel
+     * plane must be the same size.
+     *
+     * For video the linesizes should be multiplies of the CPUs alignment
+     * preference, this is 16 or 32 for modern desktop CPUs.
+     * Some code requires such alignment other code can be slower without
+     * correct alignment, for yet other it makes no difference.
+     */
+    int linesize[AV_NUM_DATA_POINTERS];
+
+    /**
+     * pointers to the data planes/channels.
+     *
+     * For video, this should simply point to data[].
+     *
+     * For planar audio, each channel has a separate data pointer, and
+     * linesize[0] contains the size of each channel buffer.
+     * For packed audio, there is just one data pointer, and linesize[0]
+     * contains the total size of the buffer for all channels.
+     *
+     * Note: Both data and extended_data should always be set in a valid frame,
+     * but for planar audio with more channels that can fit in data,
+     * extended_data must be used in order to access all channels.
+     */
+    uint8_t **extended_data;
+
+    /**
+     * width and height of the video frame
+     */
+    int width, height;
+
+    /**
+     * number of audio samples (per channel) described by this frame
+     */
+    int nb_samples;
+
+    /**
+     * format of the frame, -1 if unknown or unset
+     * Values correspond to enum AVPixelFormat for video frames,
+     * enum AVSampleFormat for audio)
+     */
+    int format;
+
+    /**
+     * 1 -> keyframe, 0-> not
+     */
+    int key_frame;
+
+    /**
+     * Picture type of the frame.
+     */
+    enum AVPictureType pict_type;
+
+#if FF_API_AVFRAME_LAVC
+    attribute_deprecated
+    uint8_t *base[AV_NUM_DATA_POINTERS];
+#endif
+
+    /**
+     * Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
+     */
+    AVRational sample_aspect_ratio;
+
+    /**
+     * Presentation timestamp in time_base units (time when frame should be shown to user).
+     */
+    int64_t pts;
+
+    /**
+     * PTS copied from the AVPacket that was decoded to produce this frame.
+     */
+    int64_t pkt_pts;
+
+    /**
+     * DTS copied from the AVPacket that triggered returning this frame. (if frame threading isnt used)
+     * This is also the Presentation time of this AVFrame calculated from
+     * only AVPacket.dts values without pts values.
+     */
+    int64_t pkt_dts;
+
+    /**
+     * picture number in bitstream order
+     */
+    int coded_picture_number;
+    /**
+     * picture number in display order
+     */
+    int display_picture_number;
+
+    /**
+     * quality (between 1 (good) and FF_LAMBDA_MAX (bad))
+     */
+    int quality;
+
+#if FF_API_AVFRAME_LAVC
+    attribute_deprecated
+    int reference;
+
+    /**
+     * QP table
+     */
+    attribute_deprecated
+    int8_t *qscale_table;
+    /**
+     * QP store stride
+     */
+    attribute_deprecated
+    int qstride;
+
+    attribute_deprecated
+    int qscale_type;
+
+    /**
+     * mbskip_table[mb]>=1 if MB didn't change
+     * stride= mb_width = (width+15)>>4
+     */
+    attribute_deprecated
+    uint8_t *mbskip_table;
+
+    /**
+     * motion vector table
+     * @code
+     * example:
+     * int mv_sample_log2= 4 - motion_subsample_log2;
+     * int mb_width= (width+15)>>4;
+     * int mv_stride= (mb_width << mv_sample_log2) + 1;
+     * motion_val[direction][x + y*mv_stride][0->mv_x, 1->mv_y];
+     * @endcode
+     */
+    attribute_deprecated
+    int16_t (*motion_val[2])[2];
+
+    /**
+     * macroblock type table
+     * mb_type_base + mb_width + 2
+     */
+    attribute_deprecated
+    uint32_t *mb_type;
+
+    /**
+     * DCT coefficients
+     */
+    attribute_deprecated
+    short *dct_coeff;
+
+    /**
+     * motion reference frame index
+     * the order in which these are stored can depend on the codec.
+     */
+    attribute_deprecated
+    int8_t *ref_index[2];
+#endif
+
+    /**
+     * for some private data of the user
+     */
+    void *opaque;
+
+    /**
+     * error
+     */
+    uint64_t error[AV_NUM_DATA_POINTERS];
+
+#if FF_API_AVFRAME_LAVC
+    attribute_deprecated
+    int type;
+#endif
+
+    /**
+     * When decoding, this signals how much the picture must be delayed.
+     * extra_delay = repeat_pict / (2*fps)
+     */
+    int repeat_pict;
+
+    /**
+     * The content of the picture is interlaced.
+     */
+    int interlaced_frame;
+
+    /**
+     * If the content is interlaced, is top field displayed first.
+     */
+    int top_field_first;
+
+    /**
+     * Tell user application that palette has changed from previous frame.
+     */
+    int palette_has_changed;
+
+#if FF_API_AVFRAME_LAVC
+    attribute_deprecated
+    int buffer_hints;
+
+    /**
+     * Pan scan.
+     */
+    attribute_deprecated
+    struct AVPanScan *pan_scan;
+#endif
+
+    /**
+     * reordered opaque 64bit (generally an integer or a double precision float
+     * PTS but can be anything).
+     * The user sets AVCodecContext.reordered_opaque to represent the input at
+     * that time,
+     * the decoder reorders values as needed and sets AVFrame.reordered_opaque
+     * to exactly one of the values provided by the user through AVCodecContext.reordered_opaque
+     * @deprecated in favor of pkt_pts
+     */
+    int64_t reordered_opaque;
+
+#if FF_API_AVFRAME_LAVC
+    /**
+     * @deprecated this field is unused
+     */
+    attribute_deprecated void *hwaccel_picture_private;
+
+    attribute_deprecated
+    struct AVCodecContext *owner;
+    attribute_deprecated
+    void *thread_opaque;
+
+    /**
+     * log2 of the size of the block which a single vector in motion_val represents:
+     * (4->16x16, 3->8x8, 2-> 4x4, 1-> 2x2)
+     */
+    attribute_deprecated
+    uint8_t motion_subsample_log2;
+#endif
+
+    /**
+     * Sample rate of the audio data.
+     */
+    int sample_rate;
+
+    /**
+     * Channel layout of the audio data.
+     */
+    uint64_t channel_layout;
+
+    /**
+     * AVBuffer references backing the data for this frame. If all elements of
+     * this array are NULL, then this frame is not reference counted.
+     *
+     * There may be at most one AVBuffer per data plane, so for video this array
+     * always contains all the references. For planar audio with more than
+     * AV_NUM_DATA_POINTERS channels, there may be more buffers than can fit in
+     * this array. Then the extra AVBufferRef pointers are stored in the
+     * extended_buf array.
+     */
+    AVBufferRef *buf[AV_NUM_DATA_POINTERS];
+
+    /**
+     * For planar audio which requires more than AV_NUM_DATA_POINTERS
+     * AVBufferRef pointers, this array will hold all the references which
+     * cannot fit into AVFrame.buf.
+     *
+     * Note that this is different from AVFrame.extended_data, which always
+     * contains all the pointers. This array only contains the extra pointers,
+     * which cannot fit into AVFrame.buf.
+     *
+     * This array is always allocated using av_malloc() by whoever constructs
+     * the frame. It is freed in av_frame_unref().
+     */
+    AVBufferRef **extended_buf;
+    /**
+     * Number of elements in extended_buf.
+     */
+    int        nb_extended_buf;
+
+    AVFrameSideData **side_data;
+    int            nb_side_data;
+
+    /**
+     * frame timestamp estimated using various heuristics, in stream time base
+     * Code outside libavcodec should access this field using:
+     * av_frame_get_best_effort_timestamp(frame)
+     * - encoding: unused
+     * - decoding: set by libavcodec, read by user.
+     */
+    int64_t best_effort_timestamp;
+
+    /**
+     * reordered pos from the last AVPacket that has been input into the decoder
+     * Code outside libavcodec should access this field using:
+     * av_frame_get_pkt_pos(frame)
+     * - encoding: unused
+     * - decoding: Read by user.
+     */
+    int64_t pkt_pos;
+
+    /**
+     * duration of the corresponding packet, expressed in
+     * AVStream->time_base units, 0 if unknown.
+     * Code outside libavcodec should access this field using:
+     * av_frame_get_pkt_duration(frame)
+     * - encoding: unused
+     * - decoding: Read by user.
+     */
+    int64_t pkt_duration;
+
+    /**
+     * metadata.
+     * Code outside libavcodec should access this field using:
+     * av_frame_get_metadata(frame)
+     * - encoding: Set by user.
+     * - decoding: Set by libavcodec.
+     */
+    AVDictionary *metadata;
+
+    /**
+     * decode error flags of the frame, set to a combination of
+     * FF_DECODE_ERROR_xxx flags if the decoder produced a frame, but there
+     * were errors during the decoding.
+     * Code outside libavcodec should access this field using:
+     * av_frame_get_decode_error_flags(frame)
+     * - encoding: unused
+     * - decoding: set by libavcodec, read by user.
+     */
+    int decode_error_flags;
+#define FF_DECODE_ERROR_INVALID_BITSTREAM   1
+#define FF_DECODE_ERROR_MISSING_REFERENCE   2
+
+    /**
+     * number of audio channels, only used for audio.
+     * Code outside libavcodec should access this field using:
+     * av_frame_get_channels(frame)
+     * - encoding: unused
+     * - decoding: Read by user.
+     */
+    int channels;
+
+    /**
+     * size of the corresponding packet containing the compressed
+     * frame. It must be accessed using av_frame_get_pkt_size() and
+     * av_frame_set_pkt_size().
+     * It is set to a negative value if unknown.
+     * - encoding: unused
+     * - decoding: set by libavcodec, read by user.
+     */
+    int pkt_size;
+
+    /**
+     * Not to be accessed directly from outside libavutil
+     */
+    AVBufferRef *qp_table_buf;
+} AVFrame;
+
+/**
+ * Accessors for some AVFrame fields.
+ * The position of these field in the structure is not part of the ABI,
+ * they should not be accessed directly outside libavcodec.
+ */
+int64_t av_frame_get_best_effort_timestamp(const AVFrame *frame);
+void    av_frame_set_best_effort_timestamp(AVFrame *frame, int64_t val);
+int64_t av_frame_get_pkt_duration         (const AVFrame *frame);
+void    av_frame_set_pkt_duration         (AVFrame *frame, int64_t val);
+int64_t av_frame_get_pkt_pos              (const AVFrame *frame);
+void    av_frame_set_pkt_pos              (AVFrame *frame, int64_t val);
+int64_t av_frame_get_channel_layout       (const AVFrame *frame);
+void    av_frame_set_channel_layout       (AVFrame *frame, int64_t val);
+int     av_frame_get_channels             (const AVFrame *frame);
+void    av_frame_set_channels             (AVFrame *frame, int     val);
+int     av_frame_get_sample_rate          (const AVFrame *frame);
+void    av_frame_set_sample_rate          (AVFrame *frame, int     val);
+AVDictionary *av_frame_get_metadata       (const AVFrame *frame);
+void          av_frame_set_metadata       (AVFrame *frame, AVDictionary *val);
+int     av_frame_get_decode_error_flags   (const AVFrame *frame);
+void    av_frame_set_decode_error_flags   (AVFrame *frame, int     val);
+int     av_frame_get_pkt_size(const AVFrame *frame);
+void    av_frame_set_pkt_size(AVFrame *frame, int val);
+AVDictionary **avpriv_frame_get_metadatap(AVFrame *frame);
+int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type);
+int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int type);
+
+/**
+ * Allocate an AVFrame and set its fields to default values.  The resulting
+ * struct must be freed using av_frame_free().
+ *
+ * @return An AVFrame filled with default values or NULL on failure.
+ *
+ * @note this only allocates the AVFrame itself, not the data buffers. Those
+ * must be allocated through other means, e.g. with av_frame_get_buffer() or
+ * manually.
+ */
+AVFrame *av_frame_alloc(void);
+
+/**
+ * Free the frame and any dynamically allocated objects in it,
+ * e.g. extended_data. If the frame is reference counted, it will be
+ * unreferenced first.
+ *
+ * @param frame frame to be freed. The pointer will be set to NULL.
+ */
+void av_frame_free(AVFrame **frame);
+
+/**
+ * Setup a new reference to the data described by an given frame.
+ *
+ * Copy frame properties from src to dst and create a new reference for each
+ * AVBufferRef from src.
+ *
+ * If src is not reference counted, new buffers are allocated and the data is
+ * copied.
+ *
+ * @return 0 on success, a negative AVERROR on error
+ */
+int av_frame_ref(AVFrame *dst, AVFrame *src);
+
+/**
+ * Create a new frame that references the same data as src.
+ *
+ * This is a shortcut for av_frame_alloc()+av_frame_ref().
+ *
+ * @return newly created AVFrame on success, NULL on error.
+ */
+AVFrame *av_frame_clone(AVFrame *src);
+
+/**
+ * Unreference all the buffers referenced by frame and reset the frame fields.
+ */
+void av_frame_unref(AVFrame *frame);
+
+/**
+ * Move everythnig contained in src to dst and reset src.
+ */
+void av_frame_move_ref(AVFrame *dst, AVFrame *src);
+
+/**
+ * Allocate new buffer(s) for audio or video data.
+ *
+ * The following fields must be set on frame before calling this function:
+ * - format (pixel format for video, sample format for audio)
+ * - width and height for video
+ * - nb_samples and channel_layout for audio
+ *
+ * This function will fill AVFrame.data and AVFrame.buf arrays and, if
+ * necessary, allocate and fill AVFrame.extended_data and AVFrame.extended_buf.
+ * For planar formats, one buffer will be allocated for each plane.
+ *
+ * @param frame frame in which to store the new buffers.
+ * @param align required buffer size alignment
+ *
+ * @return 0 on success, a negative AVERROR on error.
+ */
+int av_frame_get_buffer(AVFrame *frame, int align);
+
+/**
+ * Check if the frame data is writable.
+ *
+ * @return A positive value if the frame data is writable (which is true if and
+ * only if each of the underlying buffers has only one reference, namely the one
+ * stored in this frame). Return 0 otherwise.
+ *
+ * If 1 is returned the answer is valid until av_buffer_ref() is called on any
+ * of the underlying AVBufferRefs (e.g. through av_frame_ref() or directly).
+ *
+ * @see av_frame_make_writable(), av_buffer_is_writable()
+ */
+int av_frame_is_writable(AVFrame *frame);
+
+/**
+ * Ensure that the frame data is writable, avoiding data copy if possible.
+ *
+ * Do nothing if the frame is writable, allocate new buffers and copy the data
+ * if it is not.
+ *
+ * @return 0 on success, a negative AVERROR on error.
+ *
+ * @see av_frame_is_writable(), av_buffer_is_writable(),
+ * av_buffer_make_writable()
+ */
+int av_frame_make_writable(AVFrame *frame);
+
+/**
+ * Copy only "metadata" fields from src to dst.
+ *
+ * Metadata for the purpose of this function are those fields that do not affect
+ * the data layout in the buffers.  E.g. pts, sample rate (for audio) or sample
+ * aspect ratio (for video), but not width/height or channel layout.
+ * Side data is also copied.
+ */
+int av_frame_copy_props(AVFrame *dst, const AVFrame *src);
+
+/**
+ * Get the buffer reference a given data plane is stored in.
+ *
+ * @param plane index of the data plane of interest in frame->extended_data.
+ *
+ * @return the buffer reference that contains the plane or NULL if the input
+ * frame is not valid.
+ */
+AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane);
+
+/**
+ * Add a new side data to a frame.
+ *
+ * @param frame a frame to which the side data should be added
+ * @param type type of the added side data
+ * @param size size of the side data
+ *
+ * @return newly added side data on success, NULL on error
+ */
+AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
+                                        enum AVFrameSideDataType type,
+                                        int size);
+
+/**
+ * @return a pointer to the side data of a given type on success, NULL if there
+ * is no side data with such type in this frame.
+ */
+AVFrameSideData *av_frame_get_side_data(AVFrame *frame,
+                                        enum AVFrameSideDataType type);
+
+#endif /* AVUTIL_FRAME_H */
diff --git a/libavutil/internal.h b/libavutil/internal.h
index a105fe6..6ff14fa 100644
--- a/libavutil/internal.h
+++ b/libavutil/internal.h
@@ -162,4 +162,25 @@
 #   define ONLY_IF_THREADS_ENABLED(x) NULL
 #endif
 
+/**
+ * Log a generic warning message about a missing feature.
+ *
+ * @param[in] avc a pointer to an arbitrary struct of which the first
+ *                field is a pointer to an AVClass struct
+ * @param[in] msg string containing the name of the missing feature
+ */
+void avpriv_report_missing_feature(void *avc,
+                                   const char *msg, ...) av_printf_format(2, 3);
+
+/**
+ * Log a generic warning message about a missing feature.
+ * Additionally request that a sample showcasing the feature be uploaded.
+ *
+ * @param[in] avc a pointer to an arbitrary struct of which the first field is
+ *                a pointer to an AVClass struct
+ * @param[in] msg string containing the name of the missing feature
+ */
+void avpriv_request_sample(void *avc,
+                           const char *msg, ...) av_printf_format(2, 3);
+
 #endif /* AVUTIL_INTERNAL_H */
diff --git a/libavutil/intfloat_readwrite.c b/libavutil/intfloat_readwrite.c
index 2998229..142331d 100644
--- a/libavutil/intfloat_readwrite.c
+++ b/libavutil/intfloat_readwrite.c
@@ -26,6 +26,7 @@
  */
 
 #include <stdint.h>
+#include "common.h"
 #include "mathematics.h"
 #include "intfloat_readwrite.h"
 
@@ -88,7 +89,7 @@
             ext.mantissa[i] = m>>(56-(i<<3));
     } else if (f != 0.0) {
         ext.exponent[0] = 0x7f; ext.exponent[1] = 0xff;
-        if (f != INFINITY)
+        if (!isinf(f))
             ext.mantissa[0] = ~0;
     }
     if (d < 0)
diff --git a/libavutil/log.c b/libavutil/log.c
index 700e89f..6051418 100644
--- a/libavutil/log.c
+++ b/libavutil/log.c
@@ -32,9 +32,11 @@
 #if HAVE_IO_H
 #include <io.h>
 #endif
+#include <stdarg.h>
 #include <stdlib.h>
 #include "avutil.h"
 #include "common.h"
+#include "internal.h"
 #include "log.h"
 
 #define LINE_SZ 1024
@@ -278,3 +280,34 @@
 {
     av_log_callback = callback;
 }
+
+static void missing_feature_sample(int sample, void *avc, const char *msg, va_list argument_list)
+{
+    av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
+    av_log(avc, AV_LOG_WARNING, " is not implemented. Update your FFmpeg "
+           "version to the newest one from Git. If the problem still "
+           "occurs, it means that your file has a feature which has not "
+           "been implemented.\n");
+    if (sample)
+        av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
+               "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
+               "and contact the ffmpeg-devel mailing list.\n");
+}
+
+void avpriv_request_sample(void *avc, const char *msg, ...)
+{
+    va_list argument_list;
+
+    va_start(argument_list, msg);
+    missing_feature_sample(1, avc, msg, argument_list);
+    va_end(argument_list);
+}
+
+void avpriv_report_missing_feature(void *avc, const char *msg, ...)
+{
+    va_list argument_list;
+
+    va_start(argument_list, msg);
+    missing_feature_sample(0, avc, msg, argument_list);
+    va_end(argument_list);
+}
diff --git a/libavutil/lzo.c b/libavutil/lzo.c
index c723257..221a66b 100644
--- a/libavutil/lzo.c
+++ b/libavutil/lzo.c
@@ -110,9 +110,8 @@
  */
 static inline void copy_backptr(LZOContext *c, int back, int cnt)
 {
-    register const uint8_t *src = &c->out[-back];
     register uint8_t *dst       = c->out;
-    if (src < c->out_start || src > dst) {
+    if (dst - c->out_start < back) {
         c->error |= AV_LZO_INVALID_BACKPTR;
         return;
     }
diff --git a/libavutil/mem.c b/libavutil/mem.c
index a7fe608..860c011 100644
--- a/libavutil/mem.c
+++ b/libavutil/mem.c
@@ -95,7 +95,11 @@
 #elif HAVE_ALIGNED_MALLOC
     ptr = _aligned_malloc(size, ALIGN);
 #elif HAVE_MEMALIGN
+#ifndef __DJGPP__
     ptr = memalign(ALIGN, size);
+#else
+    ptr = memalign(size, ALIGN);
+#endif
     /* Why 64?
      * Indeed, we should align it:
      *   on  4 for 386
diff --git a/libavutil/opencl.c b/libavutil/opencl.c
new file mode 100644
index 0000000..1fdb096
--- /dev/null
+++ b/libavutil/opencl.c
@@ -0,0 +1,811 @@
+/*
+ * Copyright (C) 2012 Peng Gao <peng@multicorewareinc.com>
+ * Copyright (C) 2012 Li   Cao <li@multicorewareinc.com>
+ * Copyright (C) 2012 Wei  Gao <weigao@multicorewareinc.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 "opencl.h"
+#include "avstring.h"
+#include "log.h"
+#include "avassert.h"
+#include "opt.h"
+
+#if HAVE_PTHREADS
+
+#include <pthread.h>
+static pthread_mutex_t atomic_opencl_lock = PTHREAD_MUTEX_INITIALIZER;
+
+#define LOCK_OPENCL pthread_mutex_lock(&atomic_opencl_lock);
+#define UNLOCK_OPENCL pthread_mutex_unlock(&atomic_opencl_lock);
+
+#elif !HAVE_THREADS
+#define LOCK_OPENCL
+#define UNLOCK_OPENCL
+#endif
+
+
+#define MAX_KERNEL_NUM 500
+#define MAX_KERNEL_CODE_NUM 200
+
+typedef struct {
+    int is_compiled;
+    const char *kernel_string;
+} KernelCode;
+
+typedef struct {
+    const AVClass *class;
+    int log_offset;
+    void *log_ctx;
+    int init_count;
+    int opt_init_flag;
+     /**
+     * if set to 1, the OpenCL environment was created by the user and
+     * passed as AVOpenCLExternalEnv when initing ,0:created by opencl wrapper.
+     */
+    int is_user_created;
+    int platform_idx;
+    int device_idx;
+    char *build_options;
+    cl_platform_id platform_id;
+    cl_device_type device_type;
+    cl_context context;
+    cl_device_id device_id;
+    cl_command_queue command_queue;
+    int program_count;
+    cl_program programs[MAX_KERNEL_CODE_NUM];
+    int kernel_code_count;
+    KernelCode kernel_code[MAX_KERNEL_CODE_NUM];
+    int kernel_count;
+    AVOpenCLDeviceList device_list;
+} OpenclContext;
+
+#define OFFSET(x) offsetof(OpenclContext, x)
+
+static const AVOption opencl_options[] = {
+     { "platform_idx",        "set platform index value",  OFFSET(platform_idx),  AV_OPT_TYPE_INT,    {.i64=-1}, -1, INT_MAX},
+     { "device_idx",          "set device index value",    OFFSET(device_idx),    AV_OPT_TYPE_INT,    {.i64=-1}, -1, INT_MAX},
+     { "build_options",       "build options of opencl",   OFFSET(build_options), AV_OPT_TYPE_STRING, {.str="-I."},  CHAR_MIN, CHAR_MAX},
+};
+
+static const AVClass openclutils_class = {
+    .class_name                = "OPENCLUTILS",
+    .option                    = opencl_options,
+    .item_name                 = av_default_item_name,
+    .version                   = LIBAVUTIL_VERSION_INT,
+    .log_level_offset_offset   = offsetof(OpenclContext, log_offset),
+    .parent_log_context_offset = offsetof(OpenclContext, log_ctx),
+};
+
+static OpenclContext opencl_ctx = {&openclutils_class};
+
+static const cl_device_type device_type[] = {CL_DEVICE_TYPE_GPU, CL_DEVICE_TYPE_CPU, CL_DEVICE_TYPE_DEFAULT};
+
+typedef struct {
+    int err_code;
+    const char *err_str;
+} OpenclErrorMsg;
+
+static const OpenclErrorMsg opencl_err_msg[] = {
+    {CL_DEVICE_NOT_FOUND,                               "DEVICE NOT FOUND"},
+    {CL_DEVICE_NOT_AVAILABLE,                           "DEVICE NOT AVAILABLE"},
+    {CL_COMPILER_NOT_AVAILABLE,                         "COMPILER NOT AVAILABLE"},
+    {CL_MEM_OBJECT_ALLOCATION_FAILURE,                  "MEM OBJECT ALLOCATION FAILURE"},
+    {CL_OUT_OF_RESOURCES,                               "OUT OF RESOURCES"},
+    {CL_OUT_OF_HOST_MEMORY,                             "OUT OF HOST MEMORY"},
+    {CL_PROFILING_INFO_NOT_AVAILABLE,                   "PROFILING INFO NOT AVAILABLE"},
+    {CL_MEM_COPY_OVERLAP,                               "MEM COPY OVERLAP"},
+    {CL_IMAGE_FORMAT_MISMATCH,                          "IMAGE FORMAT MISMATCH"},
+    {CL_IMAGE_FORMAT_NOT_SUPPORTED,                     "IMAGE FORMAT NOT_SUPPORTED"},
+    {CL_BUILD_PROGRAM_FAILURE,                          "BUILD PROGRAM FAILURE"},
+    {CL_MAP_FAILURE,                                    "MAP FAILURE"},
+    {CL_MISALIGNED_SUB_BUFFER_OFFSET,                   "MISALIGNED SUB BUFFER OFFSET"},
+    {CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST,      "EXEC STATUS ERROR FOR EVENTS IN WAIT LIST"},
+    {CL_COMPILE_PROGRAM_FAILURE,                        "COMPILE PROGRAM FAILURE"},
+    {CL_LINKER_NOT_AVAILABLE,                           "LINKER NOT AVAILABLE"},
+    {CL_LINK_PROGRAM_FAILURE,                           "LINK PROGRAM FAILURE"},
+    {CL_DEVICE_PARTITION_FAILED,                        "DEVICE PARTITION FAILED"},
+    {CL_KERNEL_ARG_INFO_NOT_AVAILABLE,                  "KERNEL ARG INFO NOT AVAILABLE"},
+    {CL_INVALID_VALUE,                                  "INVALID VALUE"},
+    {CL_INVALID_DEVICE_TYPE,                            "INVALID DEVICE TYPE"},
+    {CL_INVALID_PLATFORM,                               "INVALID PLATFORM"},
+    {CL_INVALID_DEVICE,                                 "INVALID DEVICE"},
+    {CL_INVALID_CONTEXT,                                "INVALID CONTEXT"},
+    {CL_INVALID_QUEUE_PROPERTIES,                       "INVALID QUEUE PROPERTIES"},
+    {CL_INVALID_COMMAND_QUEUE,                          "INVALID COMMAND QUEUE"},
+    {CL_INVALID_HOST_PTR,                               "INVALID HOST PTR"},
+    {CL_INVALID_MEM_OBJECT,                             "INVALID MEM OBJECT"},
+    {CL_INVALID_IMAGE_FORMAT_DESCRIPTOR,                "INVALID IMAGE FORMAT DESCRIPTOR"},
+    {CL_INVALID_IMAGE_SIZE,                             "INVALID IMAGE SIZE"},
+    {CL_INVALID_SAMPLER,                                "INVALID SAMPLER"},
+    {CL_INVALID_BINARY,                                 "INVALID BINARY"},
+    {CL_INVALID_BUILD_OPTIONS,                          "INVALID BUILD OPTIONS"},
+    {CL_INVALID_PROGRAM,                                "INVALID PROGRAM"},
+    {CL_INVALID_PROGRAM_EXECUTABLE,                     "INVALID PROGRAM EXECUTABLE"},
+    {CL_INVALID_KERNEL_NAME,                            "INVALID KERNEL NAME"},
+    {CL_INVALID_KERNEL_DEFINITION,                      "INVALID KERNEL DEFINITION"},
+    {CL_INVALID_KERNEL,                                 "INVALID KERNEL"},
+    {CL_INVALID_ARG_INDEX,                              "INVALID ARG INDEX"},
+    {CL_INVALID_ARG_VALUE,                              "INVALID ARG VALUE"},
+    {CL_INVALID_ARG_SIZE,                               "INVALID ARG_SIZE"},
+    {CL_INVALID_KERNEL_ARGS,                            "INVALID KERNEL ARGS"},
+    {CL_INVALID_WORK_DIMENSION,                         "INVALID WORK DIMENSION"},
+    {CL_INVALID_WORK_GROUP_SIZE,                        "INVALID WORK GROUP SIZE"},
+    {CL_INVALID_WORK_ITEM_SIZE,                         "INVALID WORK ITEM SIZE"},
+    {CL_INVALID_GLOBAL_OFFSET,                          "INVALID GLOBAL OFFSET"},
+    {CL_INVALID_EVENT_WAIT_LIST,                        "INVALID EVENT WAIT LIST"},
+    {CL_INVALID_EVENT,                                  "INVALID EVENT"},
+    {CL_INVALID_OPERATION,                              "INVALID OPERATION"},
+    {CL_INVALID_GL_OBJECT,                              "INVALID GL OBJECT"},
+    {CL_INVALID_BUFFER_SIZE,                            "INVALID BUFFER SIZE"},
+    {CL_INVALID_MIP_LEVEL,                              "INVALID MIP LEVEL"},
+    {CL_INVALID_GLOBAL_WORK_SIZE,                       "INVALID GLOBAL WORK SIZE"},
+    {CL_INVALID_PROPERTY,                               "INVALID PROPERTY"},
+    {CL_INVALID_IMAGE_DESCRIPTOR,                       "INVALID IMAGE DESCRIPTOR"},
+    {CL_INVALID_COMPILER_OPTIONS,                       "INVALID COMPILER OPTIONS"},
+    {CL_INVALID_LINKER_OPTIONS,                         "INVALID LINKER OPTIONS"},
+    {CL_INVALID_DEVICE_PARTITION_COUNT,                 "INVALID DEVICE PARTITION COUNT"},
+};
+
+static const char *opencl_errstr(cl_int status)
+{
+    int i;
+    for (i = 0; i < sizeof(opencl_err_msg); i++) {
+        if (opencl_err_msg[i].err_code == status)
+            return opencl_err_msg[i].err_str;
+    }
+    return "unknown error";
+}
+
+static void free_device_list(AVOpenCLDeviceList *device_list)
+{
+    int i, j;
+    if (!device_list)
+        return;
+    for (i = 0; i < device_list->platform_num; i++) {
+        if (!device_list->platform_node[i])
+            continue;
+        for (j = 0; j < device_list->platform_node[i]->device_num; j++) {
+            av_freep(&(device_list->platform_node[i]->device_node[j]));
+        }
+        av_freep(&device_list->platform_node[i]->device_node);
+        av_freep(&device_list->platform_node[i]);
+    }
+    av_freep(&device_list->platform_node);
+    device_list->platform_num = 0;
+}
+
+static int get_device_list(AVOpenCLDeviceList *device_list)
+{
+    cl_int status;
+    int i, j, k, device_num, total_devices_num,ret = 0;
+    int *devices_num;
+    cl_platform_id *platform_ids = NULL;
+    cl_device_id *device_ids = NULL;
+    AVOpenCLDeviceNode *device_node = NULL;
+    status = clGetPlatformIDs(0, NULL, &device_list->platform_num);
+    if (status != CL_SUCCESS) {
+        av_log(&opencl_ctx, AV_LOG_ERROR,
+               "Could not get OpenCL platform ids: %s\n", opencl_errstr(status));
+        return AVERROR_EXTERNAL;
+    }
+    platform_ids = av_mallocz(device_list->platform_num * sizeof(cl_platform_id));
+    if (!platform_ids)
+        return AVERROR(ENOMEM);
+    status = clGetPlatformIDs(device_list->platform_num, platform_ids, NULL);
+    if (status != CL_SUCCESS) {
+        av_log(&opencl_ctx, AV_LOG_ERROR,
+                "Could not get OpenCL platform ids: %s\n", opencl_errstr(status));
+        ret = AVERROR_EXTERNAL;
+        goto end;
+    }
+    device_list->platform_node = av_mallocz(device_list->platform_num * sizeof(AVOpenCLPlatformNode *));
+    if (!device_list->platform_node) {
+        ret = AVERROR(ENOMEM);
+        goto end;
+    }
+    devices_num = av_mallocz(sizeof(int) * FF_ARRAY_ELEMS(device_type));
+    if (!devices_num) {
+        ret = AVERROR(ENOMEM);
+        goto end;
+    }
+    for (i = 0; i < device_list->platform_num; i++) {
+        device_list->platform_node[i] = av_mallocz(sizeof(AVOpenCLPlatformNode));
+        if (!device_list->platform_node[i]) {
+            ret = AVERROR(ENOMEM);
+            goto end;
+        }
+        device_list->platform_node[i]->platform_id = platform_ids[i];
+        status = clGetPlatformInfo(platform_ids[i], CL_PLATFORM_VENDOR,
+                                   sizeof(device_list->platform_node[i]->platform_name),
+                                   device_list->platform_node[i]->platform_name, NULL);
+        total_devices_num = 0;
+        for (j = 0; j < FF_ARRAY_ELEMS(device_type); j++) {
+            status = clGetDeviceIDs(device_list->platform_node[i]->platform_id,
+                                    device_type[j], 0, NULL, &devices_num[j]);
+            total_devices_num += devices_num[j];
+        }
+        device_list->platform_node[i]->device_node = av_mallocz(total_devices_num * sizeof(AVOpenCLDeviceNode *));
+        if (!device_list->platform_node[i]->device_node) {
+            ret = AVERROR(ENOMEM);
+            goto end;
+        }
+        for (j = 0; j < FF_ARRAY_ELEMS(device_type); j++) {
+            if (devices_num[j]) {
+                device_ids = av_mallocz(devices_num[j] * sizeof(cl_device_id));
+                if (!device_ids) {
+                    ret = AVERROR(ENOMEM);
+                    goto end;
+                }
+                status = clGetDeviceIDs(device_list->platform_node[i]->platform_id, device_type[j],
+                                        devices_num[j], device_ids, NULL);
+                if (status != CL_SUCCESS) {
+                    av_log(&opencl_ctx, AV_LOG_WARNING,
+                            "Could not get device ID: %s:\n", opencl_errstr(status));
+                    av_freep(&device_ids);
+                    continue;
+                }
+                for (k = 0; k < devices_num[j]; k++) {
+                    device_num = device_list->platform_node[i]->device_num;
+                    device_list->platform_node[i]->device_node[device_num] = av_mallocz(sizeof(AVOpenCLDeviceNode));
+                    if (!device_list->platform_node[i]->device_node[device_num]) {
+                        ret = AVERROR(ENOMEM);
+                        goto end;
+                    }
+                    device_node = device_list->platform_node[i]->device_node[device_num];
+                    device_node->device_id = device_ids[k];
+                    device_node->device_type = device_type[j];
+                    status = clGetDeviceInfo(device_node->device_id, CL_DEVICE_NAME,
+                                             sizeof(device_node->device_name), device_node->device_name,
+                                             NULL);
+                    if (status != CL_SUCCESS) {
+                        av_log(&opencl_ctx, AV_LOG_WARNING,
+                                "Could not get device name: %s\n", opencl_errstr(status));
+                        continue;
+                    }
+                    device_list->platform_node[i]->device_num++;
+                }
+                av_freep(&device_ids);
+            }
+        }
+    }
+end:
+    av_freep(&platform_ids);
+    av_freep(&devices_num);
+    av_freep(&device_ids);
+    if (ret < 0)
+        free_device_list(device_list);
+    return ret;
+}
+
+int av_opencl_get_device_list(AVOpenCLDeviceList **device_list)
+{
+    int ret = 0;
+    *device_list = av_mallocz(sizeof(AVOpenCLDeviceList));
+    if (!(*device_list)) {
+        av_log(&opencl_ctx, AV_LOG_ERROR, "Could not allocate opencl device list\n");
+        return AVERROR(ENOMEM);
+    }
+    ret = get_device_list(*device_list);
+    if (ret < 0) {
+        av_log(&opencl_ctx, AV_LOG_ERROR, "Could not get device list from environment\n");
+        free_device_list(*device_list);
+        av_freep(device_list);
+        return ret;
+    }
+    return ret;
+}
+
+void av_opencl_free_device_list(AVOpenCLDeviceList **device_list)
+{
+    free_device_list(*device_list);
+    av_freep(device_list);
+}
+
+int av_opencl_set_option(const char *key, const char *val)
+{
+    int ret = 0;
+    LOCK_OPENCL
+    if (!opencl_ctx.opt_init_flag) {
+        av_opt_set_defaults(&opencl_ctx);
+        opencl_ctx.opt_init_flag = 1;
+    }
+    ret = av_opt_set(&opencl_ctx, key, val, 0);
+    UNLOCK_OPENCL
+    return ret;
+}
+
+int av_opencl_get_option(const char *key, uint8_t **out_val)
+{
+    int ret = 0;
+    LOCK_OPENCL
+    ret = av_opt_get(&opencl_ctx, key, 0, out_val);
+    UNLOCK_OPENCL
+    return ret;
+}
+
+void av_opencl_free_option(void)
+{
+    /*FIXME: free openclutils context*/
+    LOCK_OPENCL
+    av_opt_free(&opencl_ctx);
+    UNLOCK_OPENCL
+}
+
+AVOpenCLExternalEnv *av_opencl_alloc_external_env(void)
+{
+    AVOpenCLExternalEnv *ext = av_mallocz(sizeof(AVOpenCLExternalEnv));
+    if (!ext) {
+        av_log(&opencl_ctx, AV_LOG_ERROR,
+               "Could not malloc external opencl environment data space\n");
+    }
+    return ext;
+}
+
+void av_opencl_free_external_env(AVOpenCLExternalEnv **ext_opencl_env)
+{
+    av_freep(ext_opencl_env);
+}
+
+int av_opencl_register_kernel_code(const char *kernel_code)
+{
+    int i, ret = 0;
+    LOCK_OPENCL;
+    if (opencl_ctx.kernel_code_count >= MAX_KERNEL_CODE_NUM) {
+        av_log(&opencl_ctx, AV_LOG_ERROR,
+               "Could not register kernel code, maximum number of registered kernel code %d already reached\n",
+               MAX_KERNEL_CODE_NUM);
+        ret = AVERROR(EINVAL);
+        goto end;
+    }
+    for (i = 0; i < opencl_ctx.kernel_code_count; i++) {
+        if (opencl_ctx.kernel_code[i].kernel_string == kernel_code) {
+            av_log(&opencl_ctx, AV_LOG_WARNING, "Same kernel code has been registered\n");
+            goto end;
+        }
+    }
+    opencl_ctx.kernel_code[opencl_ctx.kernel_code_count].kernel_string = kernel_code;
+    opencl_ctx.kernel_code[opencl_ctx.kernel_code_count].is_compiled = 0;
+    opencl_ctx.kernel_code_count++;
+end:
+    UNLOCK_OPENCL;
+    return ret;
+}
+
+int av_opencl_create_kernel(AVOpenCLKernelEnv *env, const char *kernel_name)
+{
+    cl_int status;
+    int i, ret = 0;
+    LOCK_OPENCL;
+    if (strlen(kernel_name) + 1 > AV_OPENCL_MAX_KERNEL_NAME_SIZE) {
+        av_log(&opencl_ctx, AV_LOG_ERROR, "Created kernel name %s is too long\n", kernel_name);
+        ret = AVERROR(EINVAL);
+        goto end;
+    }
+    if (!env->kernel) {
+        if (opencl_ctx.kernel_count >= MAX_KERNEL_NUM) {
+            av_log(&opencl_ctx, AV_LOG_ERROR,
+                   "Could not create kernel with name '%s', maximum number of kernels %d already reached\n",
+                   kernel_name, MAX_KERNEL_NUM);
+            ret = AVERROR(EINVAL);
+            goto end;
+        }
+        if (opencl_ctx.program_count == 0) {
+            av_log(&opencl_ctx, AV_LOG_ERROR, "Program count of OpenCL is 0, can not create kernel\n");
+            ret = AVERROR(EINVAL);
+            goto end;
+        }
+        for (i = 0; i < opencl_ctx.program_count; i++) {
+            env->kernel = clCreateKernel(opencl_ctx.programs[i], kernel_name, &status);
+            if (status == CL_SUCCESS)
+                break;
+        }
+        if (status != CL_SUCCESS) {
+            av_log(&opencl_ctx, AV_LOG_ERROR, "Could not create OpenCL kernel: %s\n", opencl_errstr(status));
+            ret = AVERROR_EXTERNAL;
+            goto end;
+        }
+        opencl_ctx.kernel_count++;
+        env->command_queue = opencl_ctx.command_queue;
+        av_strlcpy(env->kernel_name, kernel_name, sizeof(env->kernel_name));
+    }
+end:
+    UNLOCK_OPENCL;
+    return ret;
+}
+
+void av_opencl_release_kernel(AVOpenCLKernelEnv *env)
+{
+    cl_int status;
+    LOCK_OPENCL
+    if (!env->kernel)
+        goto end;
+    status = clReleaseKernel(env->kernel);
+    if (status != CL_SUCCESS) {
+        av_log(&opencl_ctx, AV_LOG_ERROR, "Could not release kernel: %s\n",
+              opencl_errstr(status));
+    }
+    env->kernel = NULL;
+    env->command_queue = NULL;
+    env->kernel_name[0] = 0;
+    opencl_ctx.kernel_count--;
+end:
+    UNLOCK_OPENCL
+}
+
+static int init_opencl_env(OpenclContext *opencl_ctx, AVOpenCLExternalEnv *ext_opencl_env)
+{
+    cl_int status;
+    cl_context_properties cps[3];
+    int i, ret = 0;
+    AVOpenCLDeviceNode *device_node = NULL;
+
+    if (ext_opencl_env) {
+        if (opencl_ctx->is_user_created)
+            return 0;
+        opencl_ctx->platform_id     = ext_opencl_env->platform_id;
+        opencl_ctx->is_user_created = 1;
+        opencl_ctx->command_queue   = ext_opencl_env->command_queue;
+        opencl_ctx->context         = ext_opencl_env->context;
+        opencl_ctx->device_id       = ext_opencl_env->device_id;
+        opencl_ctx->device_type     = ext_opencl_env->device_type;
+    } else {
+        if (!opencl_ctx->is_user_created) {
+            if (!opencl_ctx->device_list.platform_num) {
+                ret = get_device_list(&opencl_ctx->device_list);
+                if (ret < 0) {
+                    return ret;
+                }
+            }
+            if (opencl_ctx->platform_idx >= 0) {
+                if (opencl_ctx->device_list.platform_num < opencl_ctx->platform_idx + 1) {
+                    av_log(opencl_ctx, AV_LOG_ERROR, "User set platform index not exist\n");
+                    return AVERROR(EINVAL);
+                }
+                if (!opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_num) {
+                    av_log(opencl_ctx, AV_LOG_ERROR, "No devices in user specific platform with index %d\n",
+                           opencl_ctx->platform_idx);
+                    return AVERROR(EINVAL);
+                }
+                opencl_ctx->platform_id = opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->platform_id;
+            } else {
+                /* get a usable platform by default*/
+                for (i = 0; i < opencl_ctx->device_list.platform_num; i++) {
+                    if (opencl_ctx->device_list.platform_node[i]->device_num) {
+                        opencl_ctx->platform_id = opencl_ctx->device_list.platform_node[i]->platform_id;
+                        opencl_ctx->platform_idx = i;
+                        break;
+                    }
+                }
+            }
+            if (!opencl_ctx->platform_id) {
+                av_log(opencl_ctx, AV_LOG_ERROR, "Could not get OpenCL platforms\n");
+                return AVERROR_EXTERNAL;
+            }
+            /* get a usable device*/
+            if (opencl_ctx->device_idx >= 0) {
+                if (opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_num < opencl_ctx->device_idx + 1) {
+                    av_log(opencl_ctx, AV_LOG_ERROR,
+                           "Could not get OpenCL device idx %d in the user set platform\n", opencl_ctx->platform_idx);
+                    return AVERROR(EINVAL);
+                }
+            } else {
+                opencl_ctx->device_idx = 0;
+            }
+
+            device_node = opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->device_node[opencl_ctx->device_idx];
+            opencl_ctx->device_id = device_node->device_id;
+            opencl_ctx->device_type = device_node->device_type;
+
+            /*
+             * Use available platform.
+             */
+            av_log(opencl_ctx, AV_LOG_VERBOSE, "Platform Name: %s, device id: 0x%x\n",
+                   opencl_ctx->device_list.platform_node[opencl_ctx->platform_idx]->platform_name,
+                   (unsigned int)opencl_ctx->device_id);
+            cps[0] = CL_CONTEXT_PLATFORM;
+            cps[1] = (cl_context_properties)opencl_ctx->platform_id;
+            cps[2] = 0;
+
+            opencl_ctx->context = clCreateContextFromType(cps, opencl_ctx->device_type,
+                                                       NULL, NULL, &status);
+            if (status != CL_SUCCESS) {
+                av_log(opencl_ctx, AV_LOG_ERROR,
+                       "Could not get OpenCL context from device type: %s\n", opencl_errstr(status));
+                return AVERROR_EXTERNAL;
+            }
+            opencl_ctx->command_queue = clCreateCommandQueue(opencl_ctx->context, opencl_ctx->device_id,
+                                                          0, &status);
+            if (status != CL_SUCCESS) {
+                av_log(opencl_ctx, AV_LOG_ERROR,
+                       "Could not create OpenCL command queue: %s\n", opencl_errstr(status));
+                return AVERROR_EXTERNAL;
+            }
+        }
+    }
+    return ret;
+}
+
+static int compile_kernel_file(OpenclContext *opencl_ctx)
+{
+    cl_int status;
+    int i, kernel_code_count = 0;
+    const char *kernel_code[MAX_KERNEL_CODE_NUM] = {NULL};
+    size_t kernel_code_len[MAX_KERNEL_CODE_NUM] = {0};
+
+    for (i = 0; i < opencl_ctx->kernel_code_count; i++) {
+        if (!opencl_ctx->kernel_code[i].is_compiled) {
+            kernel_code[kernel_code_count] = opencl_ctx->kernel_code[i].kernel_string;
+            kernel_code_len[kernel_code_count] = strlen(opencl_ctx->kernel_code[i].kernel_string);
+            opencl_ctx->kernel_code[i].is_compiled = 1;
+            kernel_code_count++;
+        }
+    }
+    if (!kernel_code_count)
+        return 0;
+    /* create a CL program using the kernel source */
+    opencl_ctx->programs[opencl_ctx->program_count] = clCreateProgramWithSource(opencl_ctx->context,
+                                                                                kernel_code_count,
+                                                                                kernel_code,
+                                                                                kernel_code_len,
+                                                                                &status);
+    if(status != CL_SUCCESS) {
+        av_log(opencl_ctx, AV_LOG_ERROR,
+               "Could not create OpenCL program with source code: %s\n", opencl_errstr(status));
+        return AVERROR_EXTERNAL;
+    }
+    if (!opencl_ctx->programs[opencl_ctx->program_count]) {
+        av_log(opencl_ctx, AV_LOG_ERROR, "Created program is NULL\n");
+        return AVERROR_EXTERNAL;
+    }
+    status = clBuildProgram(opencl_ctx->programs[opencl_ctx->program_count], 1, &(opencl_ctx->device_id),
+                            opencl_ctx->build_options, NULL, NULL);
+    if (status != CL_SUCCESS) {
+        av_log(opencl_ctx, AV_LOG_ERROR,
+               "Could not compile OpenCL kernel: %s\n", opencl_errstr(status));
+        return AVERROR_EXTERNAL;
+    }
+    opencl_ctx->program_count++;
+    return 0;
+}
+
+int av_opencl_init(AVOpenCLExternalEnv *ext_opencl_env)
+{
+    int ret = 0;
+    LOCK_OPENCL
+    if (!opencl_ctx.init_count) {
+        if (!opencl_ctx.opt_init_flag) {
+            av_opt_set_defaults(&opencl_ctx);
+            opencl_ctx.opt_init_flag = 1;
+        }
+        ret = init_opencl_env(&opencl_ctx, ext_opencl_env);
+        if (ret < 0)
+            goto end;
+    }
+    ret = compile_kernel_file(&opencl_ctx);
+    if (ret < 0)
+        goto end;
+    if (opencl_ctx.kernel_code_count <= 0) {
+        av_log(&opencl_ctx, AV_LOG_ERROR,
+               "No kernel code is registered, compile kernel file failed\n");
+        ret = AVERROR(EINVAL);
+        goto end;
+    }
+    opencl_ctx.init_count++;
+
+end:
+    UNLOCK_OPENCL
+    return ret;
+}
+
+void av_opencl_uninit(void)
+{
+    cl_int status;
+    int i;
+    LOCK_OPENCL
+    opencl_ctx.init_count--;
+    if (opencl_ctx.is_user_created)
+        goto end;
+    if (opencl_ctx.init_count > 0 || opencl_ctx.kernel_count > 0)
+        goto end;
+    for (i = 0; i < opencl_ctx.program_count; i++) {
+        if (opencl_ctx.programs[i]) {
+            status = clReleaseProgram(opencl_ctx.programs[i]);
+            if (status != CL_SUCCESS) {
+                av_log(&opencl_ctx, AV_LOG_ERROR,
+                       "Could not release OpenCL program: %s\n", opencl_errstr(status));
+            }
+            opencl_ctx.programs[i] = NULL;
+        }
+    }
+    if (opencl_ctx.command_queue) {
+        status = clReleaseCommandQueue(opencl_ctx.command_queue);
+        if (status != CL_SUCCESS) {
+            av_log(&opencl_ctx, AV_LOG_ERROR,
+                   "Could not release OpenCL command queue: %s\n", opencl_errstr(status));
+        }
+        opencl_ctx.command_queue = NULL;
+    }
+    if (opencl_ctx.context) {
+        status = clReleaseContext(opencl_ctx.context);
+        if (status != CL_SUCCESS) {
+            av_log(&opencl_ctx, AV_LOG_ERROR,
+                   "Could not release OpenCL context: %s\n", opencl_errstr(status));
+        }
+        opencl_ctx.context = NULL;
+    }
+    free_device_list(&opencl_ctx.device_list);
+end:
+    if ((opencl_ctx.init_count <= 0) && (opencl_ctx.kernel_count <= 0))
+        av_opt_free(&opencl_ctx); //FIXME: free openclutils context
+    UNLOCK_OPENCL
+}
+
+int av_opencl_buffer_create(cl_mem *cl_buf, size_t cl_buf_size, int flags, void *host_ptr)
+{
+    cl_int status;
+    *cl_buf = clCreateBuffer(opencl_ctx.context, flags, cl_buf_size, host_ptr, &status);
+    if (status != CL_SUCCESS) {
+        av_log(&opencl_ctx, AV_LOG_ERROR, "Could not create OpenCL buffer: %s\n", opencl_errstr(status));
+        return AVERROR_EXTERNAL;
+    }
+    return 0;
+}
+
+void av_opencl_buffer_release(cl_mem *cl_buf)
+{
+    cl_int status = 0;
+    if (!cl_buf)
+        return;
+    status = clReleaseMemObject(*cl_buf);
+    if (status != CL_SUCCESS) {
+        av_log(&opencl_ctx, AV_LOG_ERROR,
+               "Could not release OpenCL buffer: %s\n", opencl_errstr(status));
+    }
+    memset(cl_buf, 0, sizeof(*cl_buf));
+}
+
+int av_opencl_buffer_write(cl_mem dst_cl_buf, uint8_t *src_buf, size_t buf_size)
+{
+    cl_int status;
+    void *mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, dst_cl_buf,
+                                      CL_TRUE, CL_MAP_WRITE, 0, sizeof(uint8_t) * buf_size,
+                                      0, NULL, NULL, &status);
+
+    if (status != CL_SUCCESS) {
+        av_log(&opencl_ctx, AV_LOG_ERROR,
+               "Could not map OpenCL buffer: %s\n", opencl_errstr(status));
+        return AVERROR_EXTERNAL;
+    }
+    memcpy(mapped, src_buf, buf_size);
+
+    status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
+    if (status != CL_SUCCESS) {
+        av_log(&opencl_ctx, AV_LOG_ERROR,
+               "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
+        return AVERROR_EXTERNAL;
+    }
+    return 0;
+}
+
+int av_opencl_buffer_read(uint8_t *dst_buf, cl_mem src_cl_buf, size_t buf_size)
+{
+    cl_int status;
+    void *mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, src_cl_buf,
+                                      CL_TRUE, CL_MAP_READ, 0, buf_size,
+                                      0, NULL, NULL, &status);
+
+    if (status != CL_SUCCESS) {
+        av_log(&opencl_ctx, AV_LOG_ERROR,
+               "Could not map OpenCL buffer: %s\n", opencl_errstr(status));
+        return AVERROR_EXTERNAL;
+    }
+    memcpy(dst_buf, mapped, buf_size);
+
+    status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
+    if (status != CL_SUCCESS) {
+        av_log(&opencl_ctx, AV_LOG_ERROR,
+               "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
+        return AVERROR_EXTERNAL;
+    }
+    return 0;
+}
+
+int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int dst_cl_offset,
+                                 uint8_t **src_data, int *plane_size, int plane_num)
+{
+    int i, buffer_size = 0;
+    uint8_t *temp;
+    cl_int status;
+    void *mapped;
+    if ((unsigned int)plane_num > 8) {
+        return AVERROR(EINVAL);
+    }
+    for (i = 0;i < plane_num;i++) {
+        buffer_size += plane_size[i];
+    }
+    if (buffer_size > cl_buffer_size) {
+        av_log(&opencl_ctx, AV_LOG_ERROR,
+               "Cannot write image to OpenCL buffer: buffer too small\n");
+        return AVERROR(EINVAL);
+    }
+    mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, dst_cl_buf,
+                                CL_TRUE, CL_MAP_WRITE, 0, buffer_size + dst_cl_offset,
+                                0, NULL, NULL, &status);
+    if (status != CL_SUCCESS) {
+        av_log(&opencl_ctx, AV_LOG_ERROR,
+               "Could not map OpenCL buffer: %s\n", opencl_errstr(status));
+        return AVERROR_EXTERNAL;
+    }
+    temp = mapped;
+    temp += dst_cl_offset;
+    for (i = 0; i < plane_num; i++) {
+        memcpy(temp, src_data[i], plane_size[i]);
+        temp += plane_size[i];
+    }
+    status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, dst_cl_buf, mapped, 0, NULL, NULL);
+    if (status != CL_SUCCESS) {
+        av_log(&opencl_ctx, AV_LOG_ERROR,
+               "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
+        return AVERROR_EXTERNAL;
+    }
+    return 0;
+}
+
+int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_num,
+                                cl_mem src_cl_buf, size_t cl_buffer_size)
+{
+    int i,buffer_size = 0,ret = 0;
+    uint8_t *temp;
+    void *mapped;
+    cl_int status;
+    if ((unsigned int)plane_num > 8) {
+        return AVERROR(EINVAL);
+    }
+    for (i = 0; i < plane_num; i++) {
+        buffer_size += plane_size[i];
+    }
+    if (buffer_size > cl_buffer_size) {
+        av_log(&opencl_ctx, AV_LOG_ERROR,
+               "Cannot write image to CPU buffer: OpenCL buffer too small\n");
+        return AVERROR(EINVAL);
+    }
+    mapped = clEnqueueMapBuffer(opencl_ctx.command_queue, src_cl_buf,
+                                CL_TRUE, CL_MAP_READ, 0, buffer_size,
+                                0, NULL, NULL, &status);
+
+    if (status != CL_SUCCESS) {
+        av_log(&opencl_ctx, AV_LOG_ERROR,
+               "Could not map OpenCL buffer: %s\n", opencl_errstr(status));
+        return AVERROR_EXTERNAL;
+    }
+    temp = mapped;
+    if (ret >= 0) {
+        for (i = 0; i < plane_num; i++) {
+            memcpy(dst_data[i], temp, plane_size[i]);
+            temp += plane_size[i];
+        }
+    }
+    status = clEnqueueUnmapMemObject(opencl_ctx.command_queue, src_cl_buf, mapped, 0, NULL, NULL);
+    if (status != CL_SUCCESS) {
+        av_log(&opencl_ctx, AV_LOG_ERROR,
+               "Could not unmap OpenCL buffer: %s\n", opencl_errstr(status));
+        return AVERROR_EXTERNAL;
+    }
+    return 0;
+}
diff --git a/libavutil/opencl.h b/libavutil/opencl.h
new file mode 100644
index 0000000..acafe36
--- /dev/null
+++ b/libavutil/opencl.h
@@ -0,0 +1,275 @@
+/*
+ * Copyright (C) 2012 Peng Gao <peng@multicorewareinc.com>
+ * Copyright (C) 2012 Li   Cao <li@multicorewareinc.com>
+ * Copyright (C) 2012 Wei  Gao <weigao@multicorewareinc.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
+ */
+
+/**
+ * @file
+ * OpenCL wrapper
+ *
+ * This interface is considered still experimental and its API and ABI may
+ * change without prior notice.
+ */
+
+#ifndef LIBAVUTIL_OPENCL_H
+#define LIBAVUTIL_OPENCL_H
+
+#include <CL/cl.h>
+#include "config.h"
+#include "dict.h"
+
+#define AV_OPENCL_KERNEL( ... )# __VA_ARGS__
+
+#define AV_OPENCL_MAX_KERNEL_NAME_SIZE 150
+
+#define AV_OPENCL_MAX_DEVICE_NAME_SIZE 100
+
+#define AV_OPENCL_MAX_PLATFORM_NAME_SIZE 100
+
+typedef struct {
+    int device_type;
+    char device_name[AV_OPENCL_MAX_DEVICE_NAME_SIZE];
+    cl_device_id device_id;
+} AVOpenCLDeviceNode;
+
+typedef struct {
+    cl_platform_id platform_id;
+    char platform_name[AV_OPENCL_MAX_PLATFORM_NAME_SIZE];
+    int device_num;
+    AVOpenCLDeviceNode **device_node;
+} AVOpenCLPlatformNode;
+
+typedef struct {
+    int platform_num;
+    AVOpenCLPlatformNode **platform_node;
+} AVOpenCLDeviceList;
+
+typedef struct {
+    cl_command_queue command_queue;
+    cl_kernel kernel;
+    char kernel_name[AV_OPENCL_MAX_KERNEL_NAME_SIZE];
+} AVOpenCLKernelEnv;
+
+typedef struct {
+    cl_platform_id platform_id;
+    cl_device_type device_type;
+    cl_context context;
+    cl_device_id  device_id;
+    cl_command_queue command_queue;
+    char *platform_name;
+} AVOpenCLExternalEnv;
+
+/**
+ * Get OpenCL device list.
+ *
+ * It must be freed with av_opencl_free_device_list().
+ *
+ * @param device_list pointer to OpenCL environment device list,
+ *                    should be released by av_opencl_free_device_list()
+ *
+ * @return  >=0 on success, a negative error code in case of failure
+ */
+int av_opencl_get_device_list(AVOpenCLDeviceList **device_list);
+
+/**
+  * Free OpenCL device list.
+  *
+  * @param device_list pointer to OpenCL environment device list
+  *                       created by av_opencl_get_device_list()
+  */
+void av_opencl_free_device_list(AVOpenCLDeviceList **device_list);
+
+/**
+ * Set option in the global OpenCL context.
+ *
+ * This options affect the operation performed by the next
+ * av_opencl_init() operation.
+ *
+ * The currently accepted options are:
+ * - build_options: set options to compile registered kernels code
+ * - platform: set index of platform in device list
+ * - device: set index of device in device list
+ *
+ * See reference "OpenCL Specification Version: 1.2 chapter 5.6.4".
+ *
+ * @param key                 option key
+ * @param val                 option value
+ * @return >=0 on success, a negative error code in case of failure
+ * @see av_opencl_get_option()
+ */
+int av_opencl_set_option(const char *key, const char *val);
+
+/**
+ * Get option value from the global OpenCL context.
+ *
+ * @param key        option key
+ * @param out_val  pointer to location where option value will be
+ *                         written, must be freed with av_freep()
+ * @return  >=0 on success, a negative error code in case of failure
+ * @see av_opencl_set_option()
+ */
+int av_opencl_get_option(const char *key, uint8_t **out_val);
+
+/**
+ * Free option values of the global OpenCL context.
+ *
+ */
+void av_opencl_free_option(void);
+
+/**
+ * Allocate OpenCL external environment.
+ *
+ * It must be freed with av_opencl_free_external_env().
+ *
+ * @return pointer to allocated OpenCL external environment
+ */
+AVOpenCLExternalEnv *av_opencl_alloc_external_env(void);
+
+/**
+ * Free OpenCL external environment.
+ *
+ * @param ext_opencl_env pointer to OpenCL external environment
+ *                       created by av_opencl_alloc_external_env()
+ */
+void av_opencl_free_external_env(AVOpenCLExternalEnv **ext_opencl_env);
+
+/**
+ * Register kernel code.
+ *
+ *  The registered kernel code is stored in a global context, and compiled
+ *  in the runtime environment when av_opencl_init() is called.
+ *
+ * @param kernel_code    kernel code to be compiled in the OpenCL runtime environment
+ * @return  >=0 on success, a negative error code in case of failure
+ */
+int av_opencl_register_kernel_code(const char *kernel_code);
+
+/**
+ * Initialize the run time OpenCL environment and compile the kernel
+ * code registered with av_opencl_register_kernel_code().
+ *
+ * @param ext_opencl_env external OpenCL environment, created by an
+ *                       application program, ignored if set to NULL
+ * @return >=0 on success, a negative error code in case of failure
+ */
+ int av_opencl_init(AVOpenCLExternalEnv *ext_opencl_env);
+
+/**
+ * Create kernel object in the specified kernel environment.
+ *
+ * @param env              pointer to kernel environment which is filled with
+ *                         the environment used to run the kernel
+ * @param kernel_name      kernel function name
+ * @return >=0 on success, a negative error code in case of failure
+ */
+int av_opencl_create_kernel(AVOpenCLKernelEnv *env, const char *kernel_name);
+
+/**
+ * Create OpenCL buffer.
+ *
+ * The buffer is used to save the data used or created by an OpenCL
+ * kernel.
+ * The created buffer must be released with av_opencl_buffer_release().
+ *
+ * See clCreateBuffer() function reference for more information about
+ * the parameters.
+ *
+ * @param cl_buf       pointer to OpenCL buffer
+ * @param cl_buf_size  size in bytes of the OpenCL buffer to create
+ * @param flags        flags used to control buffer attributes
+ * @param host_ptr     host pointer of the OpenCL buffer
+ * @return >=0 on success, a negative error code in case of failure
+ */
+int av_opencl_buffer_create(cl_mem *cl_buf, size_t cl_buf_size, int flags, void *host_ptr);
+
+/**
+ * Write OpenCL buffer with data from src_buf.
+ *
+ * @param dst_cl_buf        pointer to OpenCL destination buffer
+ * @param src_buf           pointer to source buffer
+ * @param buf_size          size in bytes of the source and destination buffers
+ * @return >=0 on success, a negative error code in case of failure
+ */
+int av_opencl_buffer_write(cl_mem dst_cl_buf, uint8_t *src_buf, size_t buf_size);
+
+/**
+ * Read data from OpenCL buffer to memory buffer.
+ *
+ * @param dst_buf           pointer to destination buffer (CPU memory)
+ * @param src_cl_buf        pointer to source OpenCL buffer
+ * @param buf_size          size in bytes of the source and destination buffers
+ * @return >=0 on success, a negative error code in case of failure
+ */
+int av_opencl_buffer_read(uint8_t *dst_buf, cl_mem src_cl_buf, size_t buf_size);
+
+/**
+ * Write image data from memory to OpenCL buffer.
+ *
+ * The source must be an array of pointers to image plane buffers.
+ *
+ * @param dst_cl_buf         pointer to destination OpenCL buffer
+ * @param dst_cl_buf_size    size in bytes of OpenCL buffer
+ * @param dst_cl_buf_offset  the offset of the OpenCL buffer start position
+ * @param src_data           array of pointers to source plane buffers
+ * @param src_plane_sizes    array of sizes in bytes of the source plane buffers
+ * @param src_plane_num      number of source image planes
+ * @return >=0 on success, a negative error code in case of failure
+ */
+int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int dst_cl_offset,
+                                 uint8_t **src_data, int *plane_size, int plane_num);
+
+/**
+ * Read image data from OpenCL buffer.
+ *
+ * @param dst_data           array of pointers to destination plane buffers
+ * @param dst_plane_sizes    array of pointers to destination plane buffers
+ * @param dst_plane_num      number of destination image planes
+ * @param src_cl_buf         pointer to source OpenCL buffer
+ * @param src_cl_buf_size    size in bytes of OpenCL buffer
+ * @return >=0 on success, a negative error code in case of failure
+ */
+int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_num,
+                                cl_mem src_cl_buf, size_t cl_buffer_size);
+
+/**
+ * Release OpenCL buffer.
+ *
+ * @param cl_buf pointer to OpenCL buffer to release, which was
+ *               previously filled with av_opencl_buffer_create()
+ */
+void av_opencl_buffer_release(cl_mem *cl_buf);
+
+/**
+ * Release kernel object.
+ *
+ * @param env kernel environment where the kernel object was created
+ *            with av_opencl_create_kernel()
+ */
+void av_opencl_release_kernel(AVOpenCLKernelEnv *env);
+
+/**
+ * Release OpenCL environment.
+ *
+ * The OpenCL environment is effectively released only if all the created
+ * kernels had been released with av_opencl_release_kernel().
+ */
+void av_opencl_uninit(void);
+
+#endif /* LIBAVUTIL_OPENCL_H */
diff --git a/libavutil/opt.c b/libavutil/opt.c
index 61c76da..0e7a3a8 100644
--- a/libavutil/opt.c
+++ b/libavutil/opt.c
@@ -77,6 +77,7 @@
     case AV_OPT_TYPE_PIXEL_FMT:
     case AV_OPT_TYPE_SAMPLE_FMT:
     case AV_OPT_TYPE_INT:       *intnum = *(int         *)dst;return 0;
+    case AV_OPT_TYPE_DURATION:
     case AV_OPT_TYPE_INT64:     *intnum = *(int64_t     *)dst;return 0;
     case AV_OPT_TYPE_FLOAT:     *num    = *(float       *)dst;return 0;
     case AV_OPT_TYPE_DOUBLE:    *num    = *(double      *)dst;return 0;
@@ -101,6 +102,7 @@
     case AV_OPT_TYPE_PIXEL_FMT:
     case AV_OPT_TYPE_SAMPLE_FMT:
     case AV_OPT_TYPE_INT:   *(int       *)dst= llrint(num/den)*intnum; break;
+    case AV_OPT_TYPE_DURATION:
     case AV_OPT_TYPE_INT64: *(int64_t   *)dst= llrint(num/den)*intnum; break;
     case AV_OPT_TYPE_FLOAT: *(float     *)dst= num*intnum/den;         break;
     case AV_OPT_TYPE_DOUBLE:*(double    *)dst= num*intnum/den;         break;
@@ -177,7 +179,7 @@
                               opt->type == AV_OPT_TYPE_INT) ? \
                              opt->default_val.i64 : opt->default_val.dbl)
 
-static int set_string_number(void *obj, const AVOption *o, const char *val, void *dst)
+static int set_string_number(void *obj, void *target_obj, const AVOption *o, const char *val, void *dst)
 {
     int ret = 0, notfirst = 0;
     for (;;) {
@@ -200,7 +202,7 @@
         buf[i] = 0;
 
         {
-            const AVOption *o_named = av_opt_find(obj, buf, o->unit, 0, 0);
+            const AVOption *o_named = av_opt_find(target_obj, buf, o->unit, 0, 0);
             if (o_named && o_named->type == AV_OPT_TYPE_CONST)
                 d = DEFAULT_NUMVAL(o_named);
             else if (!strcmp(buf, "default")) d = DEFAULT_NUMVAL(o);
@@ -256,7 +258,8 @@
         return AVERROR_OPTION_NOT_FOUND;
     if (!val && (o->type != AV_OPT_TYPE_STRING &&
                  o->type != AV_OPT_TYPE_PIXEL_FMT && o->type != AV_OPT_TYPE_SAMPLE_FMT &&
-                 o->type != AV_OPT_TYPE_IMAGE_SIZE))
+                 o->type != AV_OPT_TYPE_IMAGE_SIZE && o->type != AV_OPT_TYPE_VIDEO_RATE &&
+                 o->type != AV_OPT_TYPE_DURATION))
         return AVERROR(EINVAL);
 
     dst = ((uint8_t*)target_obj) + o->offset;
@@ -268,7 +271,7 @@
     case AV_OPT_TYPE_INT64:
     case AV_OPT_TYPE_FLOAT:
     case AV_OPT_TYPE_DOUBLE:
-    case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, o, val, dst);
+    case AV_OPT_TYPE_RATIONAL: return set_string_number(obj, target_obj, o, val, dst);
     case AV_OPT_TYPE_IMAGE_SIZE:
         if (!val || !strcmp(val, "none")) {
             *(int *)dst = *((int *)dst + 1) = 0;
@@ -278,6 +281,15 @@
         if (ret < 0)
             av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as image size\n", val);
         return ret;
+    case AV_OPT_TYPE_VIDEO_RATE:
+        if (!val) {
+            ret = AVERROR(EINVAL);
+        } else {
+            ret = av_parse_video_rate(dst, val);
+        }
+        if (ret < 0)
+            av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as video rate\n", val);
+        return ret;
     case AV_OPT_TYPE_PIXEL_FMT:
         if (!val || !strcmp(val, "none")) {
             ret = AV_PIX_FMT_NONE;
@@ -310,6 +322,15 @@
         }
         *(enum AVSampleFormat *)dst = ret;
         return 0;
+    case AV_OPT_TYPE_DURATION:
+        if (!val) {
+            *(int64_t *)dst = 0;
+            return 0;
+        } else {
+            if ((ret = av_parse_time(dst, val, 1)) < 0)
+                av_log(obj, AV_LOG_ERROR, "Unable to parse option value \"%s\" as duration\n", val);
+            return ret;
+        }
     }
 
     av_log(obj, AV_LOG_ERROR, "Invalid option type.\n");
@@ -321,7 +342,7 @@
     {\
         if (!o || o->type != opttype)\
             return AVERROR(EINVAL);\
-        return set_string_number(obj, o, val, name ## _out);\
+        return set_string_number(obj, obj, o, val, name ## _out);\
     }
 
 OPT_EVAL_NUMBER(flags,  AV_OPT_TYPE_FLAGS,    int)
@@ -399,8 +420,8 @@
     if (o->type != AV_OPT_TYPE_BINARY)
         return AVERROR(EINVAL);
 
-    ptr = av_malloc(len);
-    if (!ptr)
+    ptr = len ? av_malloc(len) : NULL;
+    if (len && !ptr)
         return AVERROR(ENOMEM);
 
     dst = (uint8_t **)(((uint8_t *)target_obj) + o->offset);
@@ -409,7 +430,8 @@
     av_free(*dst);
     *dst = ptr;
     *lendst = len;
-    memcpy(ptr, val, len);
+    if (len)
+        memcpy(ptr, val, len);
 
     return 0;
 }
@@ -436,6 +458,23 @@
     return 0;
 }
 
+int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags)
+{
+    void *target_obj;
+    const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
+
+    if (!o || !target_obj)
+        return AVERROR_OPTION_NOT_FOUND;
+    if (o->type != AV_OPT_TYPE_VIDEO_RATE) {
+        av_log(obj, AV_LOG_ERROR,
+               "The value set by option '%s' is not a video rate.\n", o->name);
+        return AVERROR(EINVAL);
+    }
+    if (val.num <= 0 || val.den <= 0)
+        return AVERROR(EINVAL);
+    return set_number(obj, name, val.num, val.den, 1, search_flags);
+}
+
 static int set_format(void *obj, const char *name, int fmt, int search_flags,
                       enum AVOptionType type, const char *desc, int nb_fmts)
 {
@@ -530,6 +569,7 @@
     const AVOption *o = av_opt_find2(obj, name, NULL, 0, search_flags, &target_obj);
     uint8_t *bin, buf[128];
     int len, i, ret;
+    int64_t i64;
 
     if (!o || !target_obj || (o->offset<=0 && o->type != AV_OPT_TYPE_CONST))
         return AVERROR_OPTION_NOT_FOUND;
@@ -543,6 +583,7 @@
     case AV_OPT_TYPE_INT64:     ret = snprintf(buf, sizeof(buf), "%"PRId64, *(int64_t*)dst);break;
     case AV_OPT_TYPE_FLOAT:     ret = snprintf(buf, sizeof(buf), "%f" ,     *(float  *)dst);break;
     case AV_OPT_TYPE_DOUBLE:    ret = snprintf(buf, sizeof(buf), "%f" ,     *(double *)dst);break;
+    case AV_OPT_TYPE_VIDEO_RATE:
     case AV_OPT_TYPE_RATIONAL:  ret = snprintf(buf, sizeof(buf), "%d/%d",   ((AVRational*)dst)->num, ((AVRational*)dst)->den);break;
     case AV_OPT_TYPE_CONST:     ret = snprintf(buf, sizeof(buf), "%f" ,     o->default_val.dbl);break;
     case AV_OPT_TYPE_STRING:
@@ -570,6 +611,12 @@
     case AV_OPT_TYPE_SAMPLE_FMT:
         ret = snprintf(buf, sizeof(buf), "%s", (char *)av_x_if_null(av_get_sample_fmt_name(*(enum AVSampleFormat *)dst), "none"));
         break;
+    case AV_OPT_TYPE_DURATION:
+        i64 = *(int64_t *)dst;
+        ret = snprintf(buf, sizeof(buf), "%"PRIi64"d:%02d:%02d.%06d",
+                       i64 / 3600000000, (int)((i64 / 60000000) % 60),
+                       (int)((i64 / 1000000) % 60), (int)(i64 % 1000000));
+        break;
     default:
         return AVERROR(EINVAL);
     }
@@ -695,6 +742,22 @@
     return 0;
 }
 
+int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val)
+{
+    int64_t intnum = 1;
+    double     num = 1;
+    int   ret, den = 1;
+
+    if ((ret = get_number(obj, name, NULL, &num, &den, &intnum, search_flags)) < 0)
+        return ret;
+
+    if (num == 1.0 && (int)intnum == intnum)
+        *out_val = (AVRational){intnum, den};
+    else
+        *out_val = av_d2q(num*intnum/den, 1<<24);
+    return 0;
+}
+
 static int get_format(void *obj, const char *name, int search_flags, int *out_fmt,
                       enum AVOptionType type, const char *desc)
 {
@@ -777,9 +840,11 @@
         else if (unit && opt->type==AV_OPT_TYPE_CONST && strcmp(unit, opt->unit))
             continue;
         else if (unit && opt->type == AV_OPT_TYPE_CONST)
-            av_log(av_log_obj, AV_LOG_INFO, "   %-15s ", opt->name);
+            av_log(av_log_obj, AV_LOG_INFO, "     %-15s ", opt->name);
         else
-            av_log(av_log_obj, AV_LOG_INFO, "-%-17s ", opt->name);
+            av_log(av_log_obj, AV_LOG_INFO, "  %s%-17s ",
+                   (opt->flags & AV_OPT_FLAG_FILTERING_PARAM) ? "" : "-",
+                   opt->name);
 
         switch (opt->type) {
             case AV_OPT_TYPE_FLAGS:
@@ -809,12 +874,18 @@
             case AV_OPT_TYPE_IMAGE_SIZE:
                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<image_size>");
                 break;
+            case AV_OPT_TYPE_VIDEO_RATE:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<video_rate>");
+                break;
             case AV_OPT_TYPE_PIXEL_FMT:
                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<pix_fmt>");
                 break;
             case AV_OPT_TYPE_SAMPLE_FMT:
                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<sample_fmt>");
                 break;
+            case AV_OPT_TYPE_DURATION:
+                av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "<duration>");
+                break;
             case AV_OPT_TYPE_CONST:
             default:
                 av_log(av_log_obj, AV_LOG_INFO, "%-12s ", "");
@@ -891,6 +962,7 @@
             case AV_OPT_TYPE_FLAGS:
             case AV_OPT_TYPE_INT:
             case AV_OPT_TYPE_INT64:
+            case AV_OPT_TYPE_DURATION:
                 av_opt_set_int(s, opt->name, opt->default_val.i64, 0);
             break;
             case AV_OPT_TYPE_DOUBLE:
@@ -908,6 +980,7 @@
             break;
             case AV_OPT_TYPE_STRING:
             case AV_OPT_TYPE_IMAGE_SIZE:
+            case AV_OPT_TYPE_VIDEO_RATE:
                 av_opt_set(s, opt->name, opt->default_val.str, 0);
                 break;
             case AV_OPT_TYPE_PIXEL_FMT:
@@ -959,9 +1032,16 @@
     char *val;
     int ret;
 
+    if (!key)
+        return AVERROR(ENOMEM);
+
     if (*key && strspn(*buf, key_val_sep)) {
         (*buf)++;
         val = av_get_token(buf, pairs_sep);
+        if (!val) {
+            av_freep(&key);
+            return AVERROR(ENOMEM);
+        }
     } else {
         av_log(ctx, AV_LOG_ERROR, "Missing key or no key/value separator found after key '%s'\n", key);
         av_free(key);
@@ -970,7 +1050,7 @@
 
     av_log(ctx, AV_LOG_DEBUG, "Setting entry with key '%s' to value '%s'\n", key, val);
 
-    ret = av_opt_set(ctx, key, val, 0);
+    ret = av_opt_set(ctx, key, val, AV_OPT_SEARCH_CHILDREN);
     if (ret == AVERROR_OPTION_NOT_FOUND)
         av_log(ctx, AV_LOG_ERROR, "Key '%s' not found.\n", key);
 
@@ -1253,6 +1333,7 @@
     case AV_OPT_TYPE_SAMPLE_FMT:
     case AV_OPT_TYPE_FLOAT:
     case AV_OPT_TYPE_DOUBLE:
+    case AV_OPT_TYPE_DURATION:
         break;
     case AV_OPT_TYPE_STRING:
         range->component_min = 0;
@@ -1270,6 +1351,12 @@
         range->value_min = 0;
         range->value_max = INT_MAX/8;
         break;
+    case AV_OPT_TYPE_VIDEO_RATE:
+        range->component_min = 1;
+        range->component_max = INT_MAX;
+        range->value_min = 1;
+        range->value_max = INT_MAX;
+        break;
     default:
         ret = AVERROR(ENOSYS);
         goto fail;
@@ -1308,9 +1395,11 @@
     char *string;
     int flags;
     AVRational rational;
+    AVRational video_rate;
     int w, h;
     enum AVPixelFormat pix_fmt;
     enum AVSampleFormat sample_fmt;
+    int64_t duration;
 } TestContext;
 
 #define OFFSET(x) offsetof(TestContext, x)
@@ -1322,8 +1411,8 @@
 static const AVOption test_options[]= {
 {"num",      "set num",        OFFSET(num),      AV_OPT_TYPE_INT,      {.i64 = 0},       0,        100                 },
 {"toggle",   "set toggle",     OFFSET(toggle),   AV_OPT_TYPE_INT,      {.i64 = 0},       0,        1                   },
-{"rational", "set rational",   OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0},  0,        10                  },
-{"string",   "set string",     OFFSET(string),   AV_OPT_TYPE_STRING,   {0},              CHAR_MIN, CHAR_MAX            },
+{"rational", "set rational",   OFFSET(rational), AV_OPT_TYPE_RATIONAL, {.dbl = 0},       0,        10                  },
+{"string",   "set string",     OFFSET(string),   AV_OPT_TYPE_STRING,   {.str = "default"}, CHAR_MIN, CHAR_MAX          },
 {"flags",    "set flags",      OFFSET(flags),    AV_OPT_TYPE_FLAGS,    {.i64 = 0},       0,        INT_MAX, 0, "flags" },
 {"cool",     "set cool flag ", 0,                AV_OPT_TYPE_CONST,    {.i64 = TEST_FLAG_COOL}, INT_MIN,  INT_MAX, 0, "flags" },
 {"lame",     "set lame flag ", 0,                AV_OPT_TYPE_CONST,    {.i64 = TEST_FLAG_LAME}, INT_MIN,  INT_MAX, 0, "flags" },
@@ -1331,6 +1420,8 @@
 {"size",     "set size",       OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE,{0},             0,        0                   },
 {"pix_fmt",  "set pixfmt",     OFFSET(pix_fmt),  AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, -1, AV_PIX_FMT_NB-1},
 {"sample_fmt", "set samplefmt", OFFSET(sample_fmt), AV_OPT_TYPE_SAMPLE_FMT, {.i64 = AV_SAMPLE_FMT_NONE}, -1, AV_SAMPLE_FMT_NB-1},
+{"video_rate", "set videorate", OFFSET(video_rate), AV_OPT_TYPE_VIDEO_RATE,  {.str = "25"}, 0,     0                   },
+{"duration", "set duration",   OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX},
 {NULL},
 };
 
@@ -1381,11 +1472,18 @@
             "sample_fmt=s16",
             "sample_fmt=2",
             "sample_fmt=bogus",
+            "video_rate=pal",
+            "video_rate=25",
+            "video_rate=30000/1001",
+            "video_rate=30/1.001",
+            "video_rate=bogus",
+            "duration=bogus",
+            "duration=123.45",
+            "duration=1\\:23\\:45.67",
         };
 
         test_ctx.class = &test_class;
         av_opt_set_defaults(&test_ctx);
-        test_ctx.string = av_strdup("default");
 
         av_log_set_level(AV_LOG_DEBUG);
 
diff --git a/libavutil/opt.h b/libavutil/opt.h
index baf1b82..7f7b54e 100644
--- a/libavutil/opt.h
+++ b/libavutil/opt.h
@@ -230,6 +230,8 @@
     AV_OPT_TYPE_IMAGE_SIZE = MKBETAG('S','I','Z','E'), ///< offset must point to two consecutive integers
     AV_OPT_TYPE_PIXEL_FMT  = MKBETAG('P','F','M','T'),
     AV_OPT_TYPE_SAMPLE_FMT = MKBETAG('S','F','M','T'),
+    AV_OPT_TYPE_VIDEO_RATE = MKBETAG('V','R','A','T'), ///< offset must point to AVRational
+    AV_OPT_TYPE_DURATION   = MKBETAG('D','U','R',' '),
 #if FF_API_OLD_AVOPTIONS
     FF_OPT_TYPE_FLAGS = 0,
     FF_OPT_TYPE_INT,
@@ -653,6 +655,23 @@
 int av_opt_set_image_size(void *obj, const char *name, int w, int h, int search_flags);
 int av_opt_set_pixel_fmt (void *obj, const char *name, enum AVPixelFormat fmt, int search_flags);
 int av_opt_set_sample_fmt(void *obj, const char *name, enum AVSampleFormat fmt, int search_flags);
+int av_opt_set_video_rate(void *obj, const char *name, AVRational val, int search_flags);
+
+/**
+ * Set a binary option to an integer list.
+ *
+ * @param obj    AVClass object to set options on
+ * @param name   name of the binary option
+ * @param val    pointer to an integer list (must have the correct type with
+ *               regard to the contents of the list)
+ * @param term   list terminator (usually 0 or -1)
+ * @param flags  search flags
+ */
+#define av_opt_set_int_list(obj, name, val, term, flags) \
+    (av_int_list_length(val, term) > INT_MAX / sizeof(*(val)) ? \
+     AVERROR(EINVAL) : \
+     av_opt_set_bin(obj, name, (const uint8_t *)(val), \
+                    av_int_list_length(val, term) * sizeof(*(val)), flags))
 /**
  * @}
  */
@@ -679,6 +698,7 @@
 int av_opt_get_image_size(void *obj, const char *name, int search_flags, int *w_out, int *h_out);
 int av_opt_get_pixel_fmt (void *obj, const char *name, int search_flags, enum AVPixelFormat *out_fmt);
 int av_opt_get_sample_fmt(void *obj, const char *name, int search_flags, enum AVSampleFormat *out_fmt);
+int av_opt_get_video_rate(void *obj, const char *name, int search_flags, AVRational *out_val);
 /**
  * @}
  */
diff --git a/libavutil/parseutils.c b/libavutil/parseutils.c
index c67f971..f2f8f18 100644
--- a/libavutil/parseutils.c
+++ b/libavutil/parseutils.c
@@ -426,7 +426,7 @@
     val = 0;
     for(i = 0; i < len_max; i++) {
         c = *p;
-        if (!isdigit(c))
+        if (!av_isdigit(c))
             break;
         val = (val * 10) + c - '0';
         p++;
@@ -446,8 +446,8 @@
 
     for(;;) {
         /* consume time string until a non whitespace char is found */
-        while (isspace(*fmt)) {
-            while (isspace(*p))
+        while (av_isspace(*fmt)) {
+            while (av_isspace(*p))
                 p++;
             fmt++;
         }
@@ -519,7 +519,7 @@
         y--;
     }
 
-    t = 86400 *
+    t = 86400LL *
         (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469);
 
     t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
@@ -611,11 +611,11 @@
         int n;
         q++;
         for (n = 100000; n >= 1; n /= 10, q++) {
-            if (!isdigit(*q))
+            if (!av_isdigit(*q))
                 break;
             microseconds += n * (*q - '0');
         }
-        while (isdigit(*q))
+        while (av_isdigit(*q))
             q++;
     }
 
diff --git a/libavutil/pca.c b/libavutil/pca.c
index 54927a2..311b6bc 100644
--- a/libavutil/pca.c
+++ b/libavutil/pca.c
@@ -40,12 +40,12 @@
     if(n<=0)
         return NULL;
 
-    pca= av_mallocz(sizeof(PCA));
+    pca= av_mallocz(sizeof(*pca));
     pca->n= n;
     pca->z = av_malloc(sizeof(*pca->z) * n);
     pca->count=0;
-    pca->covariance= av_mallocz(sizeof(double)*n*n);
-    pca->mean= av_mallocz(sizeof(double)*n);
+    pca->covariance= av_calloc(n*n, sizeof(double));
+    pca->mean= av_calloc(n, sizeof(double));
 
     return pca;
 }
diff --git a/libavutil/pixdesc.c b/libavutil/pixdesc.c
index 1016dba..9767cc1 100644
--- a/libavutil/pixdesc.c
+++ b/libavutil/pixdesc.c
@@ -22,6 +22,7 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "avassert.h"
 #include "common.h"
 #include "pixfmt.h"
 #include "pixdesc.h"
@@ -498,7 +499,6 @@
             { 0, 3, 1, 0, 7 },        /* R */
             { 0, 3, 2, 0, 7 },        /* G */
             { 0, 3, 3, 0, 7 },        /* B */
-            { 0, 3, 4, 0, 7 },        /* A */
         },
         .flags = PIX_FMT_RGB,
     },
@@ -523,7 +523,6 @@
             { 0, 3, 3, 0, 7 },        /* R */
             { 0, 3, 2, 0, 7 },        /* G */
             { 0, 3, 1, 0, 7 },        /* B */
-            { 0, 3, 4, 0, 7 },        /* A */
         },
         .flags = PIX_FMT_RGB,
     },
@@ -1660,6 +1659,30 @@
         .log2_chroma_h = 1,
         .flags = PIX_FMT_HWACCEL,
     },
+    [AV_PIX_FMT_XYZ12LE] = {
+        .name = "xyz12le",
+        .nb_components = 3,
+        .log2_chroma_w = 0,
+        .log2_chroma_h = 0,
+        .comp = {
+            { 0, 5, 1, 4, 11 },       /* X */
+            { 0, 5, 3, 4, 11 },       /* Y */
+            { 0, 5, 5, 4, 11 },       /* Z */
+      },
+      /*.flags = -- not used*/
+    },
+    [AV_PIX_FMT_XYZ12BE] = {
+        .name = "xyz12be",
+        .nb_components = 3,
+        .log2_chroma_w = 0,
+        .log2_chroma_h = 0,
+        .comp = {
+            { 0, 5, 1, 4, 11 },       /* X */
+            { 0, 5, 3, 4, 11 },       /* Y */
+            { 0, 5, 5, 4, 11 },       /* Z */
+       },
+        .flags = PIX_FMT_BE,
+    },
 };
 
 static enum AVPixelFormat get_pix_fmt_internal(const char *name)
@@ -1791,3 +1814,57 @@
 
     return 0;
 }
+
+int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
+{
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
+    int i, planes[4] = { 0 }, ret = 0;
+
+    if (!desc)
+        return AVERROR(EINVAL);
+
+    for (i = 0; i < desc->nb_components; i++)
+        planes[desc->comp[i].plane] = 1;
+    for (i = 0; i < FF_ARRAY_ELEMS(planes); i++)
+        ret += planes[i];
+    return ret;
+}
+
+void ff_check_pixfmt_descriptors(void){
+    int i, j;
+
+    for (i=0; i<FF_ARRAY_ELEMS(av_pix_fmt_descriptors); i++) {
+        const AVPixFmtDescriptor *d = &av_pix_fmt_descriptors[i];
+        uint8_t fill[4][8+6+3] = {{0}};
+        uint8_t *data[4] = {fill[0], fill[1], fill[2], fill[3]};
+        int linesize[4] = {0,0,0,0};
+        uint16_t tmp[2];
+
+        if (!d->name && !d->nb_components && !d->log2_chroma_w && !d->log2_chroma_h && !d->flags)
+            continue;
+//         av_log(NULL, AV_LOG_DEBUG, "Checking: %s\n", d->name);
+        av_assert0(d->log2_chroma_w <= 3);
+        av_assert0(d->log2_chroma_h <= 3);
+        av_assert0(d->nb_components <= 4);
+        av_assert0(d->name && d->name[0]);
+        av_assert0((d->nb_components==4 || d->nb_components==2) == !!(d->flags & PIX_FMT_ALPHA));
+        av_assert2(av_get_pix_fmt(d->name) == i);
+
+        for (j=0; j<FF_ARRAY_ELEMS(d->comp); j++) {
+            const AVComponentDescriptor *c = &d->comp[j];
+            if(j>=d->nb_components) {
+                av_assert0(!c->plane && !c->step_minus1 && !c->offset_plus1 && !c->shift && !c->depth_minus1);
+                continue;
+            }
+            if (d->flags & PIX_FMT_BITSTREAM) {
+                av_assert0(c->step_minus1 >= c->depth_minus1);
+            } else {
+                av_assert0(8*(c->step_minus1+1) >= c->depth_minus1+1);
+            }
+            av_read_image_line(tmp, (void*)data, linesize, d, 0, 0, j, 2, 0);
+            av_assert0(tmp[0] == 0 && tmp[1] == 0);
+            tmp[0] = tmp[1] = (1<<(c->depth_minus1 + 1)) - 1;
+            av_write_image_line(tmp, data, linesize, d, 0, 0, j, 2);
+        }
+    }
+}
diff --git a/libavutil/pixdesc.h b/libavutil/pixdesc.h
index ca0722e..538e6a5 100644
--- a/libavutil/pixdesc.h
+++ b/libavutil/pixdesc.h
@@ -225,13 +225,20 @@
  * you do check the return code!
  *
  * @param[in]  pix_fmt the pixel format
- * @param[out] h_shift store log2_chroma_h
- * @param[out] v_shift store log2_chroma_w
+ * @param[out] h_shift store log2_chroma_w
+ * @param[out] v_shift store log2_chroma_h
  *
  * @return 0 on success, AVERROR(ENOSYS) on invalid or unknown pixel format
  */
 int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt,
                                      int *h_shift, int *v_shift);
 
+/**
+ * @return number of planes in pix_fmt, a negative AVERROR if pix_fmt is not a
+ * valid pixel format.
+ */
+int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt);
+
+void ff_check_pixfmt_descriptors(void);
 
 #endif /* AVUTIL_PIXDESC_H */
diff --git a/libavutil/pixfmt.h b/libavutil/pixfmt.h
index 1c00ac4..34be30a 100644
--- a/libavutil/pixfmt.h
+++ b/libavutil/pixfmt.h
@@ -202,6 +202,9 @@
 
     AV_PIX_FMT_VDPAU,     ///< HW acceleration through VDPAU, Picture.data[3] contains a VdpVideoSurface
 
+    AV_PIX_FMT_XYZ12LE,      ///< packed XYZ 4:4:4, 36 bpp, (msb) 12X, 12Y, 12Z (lsb), the 2-byte value for each X/Y/Z is stored as little-endian, the 4 lower bits are set to 0
+    AV_PIX_FMT_XYZ12BE,      ///< packed XYZ 4:4:4, 36 bpp, (msb) 12X, 12Y, 12Z (lsb), the 2-byte value for each X/Y/Z is stored as big-endian, the 4 lower bits are set to 0
+
 #ifndef AV_PIX_FMT_ABI_GIT_MASTER
     AV_PIX_FMT_RGBA64BE=0x123,  ///< packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is stored as big-endian
     AV_PIX_FMT_RGBA64LE,  ///< packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is stored as little-endian
@@ -304,6 +307,8 @@
 #define AV_PIX_FMT_YUVA422P16 AV_PIX_FMT_NE(YUVA422P16BE, YUVA422P16LE)
 #define AV_PIX_FMT_YUVA444P16 AV_PIX_FMT_NE(YUVA444P16BE, YUVA444P16LE)
 
+#define AV_PIX_FMT_XYZ12      AV_PIX_FMT_NE(XYZ12BE, XYZ12LE)
+
 #if FF_API_PIX_FMT
 #define PixelFormat AVPixelFormat
 
diff --git a/libavutil/ppc/float_dsp_altivec.h b/libavutil/ppc/float_dsp_altivec.h
index b262a83..e1d530a 100644
--- a/libavutil/ppc/float_dsp_altivec.h
+++ b/libavutil/ppc/float_dsp_altivec.h
@@ -21,18 +21,18 @@
 #ifndef AVUTIL_PPC_FLOAT_DSP_ALTIVEC_H
 #define AVUTIL_PPC_FLOAT_DSP_ALTIVEC_H
 
-extern void ff_vector_fmul_altivec(float *dst, const float *src0,
-                                   const float *src1, int len);
+void ff_vector_fmul_altivec(float *dst, const float *src0,
+                            const float *src1, int len);
 
-extern void ff_vector_fmul_window_altivec(float *dst, const float *src0,
-                                          const float *src1, const float *win,
-                                          int len);
+void ff_vector_fmul_window_altivec(float *dst, const float *src0,
+                                   const float *src1, const float *win,
+                                   int len);
 
-extern void ff_vector_fmul_add_altivec(float *dst, const float *src0,
-                                       const float *src1, const float *src2,
-                                       int len);
+void ff_vector_fmul_add_altivec(float *dst, const float *src0,
+                                const float *src1, const float *src2,
+                                int len);
 
-extern void ff_vector_fmul_reverse_altivec(float *dst, const float *src0,
-                                           const float *src1, int len);
+void ff_vector_fmul_reverse_altivec(float *dst, const float *src0,
+                                    const float *src1, int len);
 
 #endif /* AVUTIL_PPC_FLOAT_DSP_ALTIVEC_H */
diff --git a/libavutil/samplefmt.c b/libavutil/samplefmt.c
index 6f762df..08ecc83 100644
--- a/libavutil/samplefmt.c
+++ b/libavutil/samplefmt.c
@@ -207,6 +207,21 @@
 #endif
 }
 
+int av_samples_alloc_array_and_samples(uint8_t ***audio_data, int *linesize, int nb_channels,
+                                       int nb_samples, enum AVSampleFormat sample_fmt, int align)
+{
+    int ret, nb_planes = av_sample_fmt_is_planar(sample_fmt) ? nb_channels : 1;
+
+    *audio_data = av_calloc(nb_planes, sizeof(**audio_data));
+    if (!*audio_data)
+        return AVERROR(ENOMEM);
+    ret = av_samples_alloc(*audio_data, linesize, nb_channels,
+                           nb_samples, sample_fmt, align);
+    if (ret < 0)
+        av_freep(audio_data);
+    return ret;
+}
+
 int av_samples_copy(uint8_t **dst, uint8_t * const *src, int dst_offset,
                     int src_offset, int nb_samples, int nb_channels,
                     enum AVSampleFormat sample_fmt)
diff --git a/libavutil/samplefmt.h b/libavutil/samplefmt.h
index 529711f..db17d43 100644
--- a/libavutil/samplefmt.h
+++ b/libavutil/samplefmt.h
@@ -209,11 +209,24 @@
  * @return                 >=0 on success or a negative error code on failure
  * @todo return the size of the allocated buffer in case of success at the next bump
  * @see av_samples_fill_arrays()
+ * @see av_samples_alloc_array_and_samples()
  */
 int av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels,
                      int nb_samples, enum AVSampleFormat sample_fmt, int align);
 
 /**
+ * Allocate a data pointers array, samples buffer for nb_samples
+ * samples, and fill data pointers and linesize accordingly.
+ *
+ * This is the same as av_samples_alloc(), but also allocates the data
+ * pointers array.
+ *
+ * @see av_samples_alloc()
+ */
+int av_samples_alloc_array_and_samples(uint8_t ***audio_data, int *linesize, int nb_channels,
+                                       int nb_samples, enum AVSampleFormat sample_fmt, int align);
+
+/**
  * Copy samples from src to dst.
  *
  * @param dst destination array of pointers to data planes
diff --git a/libavutil/utils.c b/libavutil/utils.c
index fbfbc49..c157aa8 100644
--- a/libavutil/utils.c
+++ b/libavutil/utils.c
@@ -20,6 +20,7 @@
 #include "avutil.h"
 #include "avassert.h"
 #include "samplefmt.h"
+#include "pixdesc.h"
 
 /**
  * @file
@@ -28,6 +29,10 @@
 
 unsigned avutil_version(void)
 {
+    static int checks_done;
+    if (checks_done)
+        return LIBAVUTIL_VERSION_INT;
+
     av_assert0(AV_PIX_FMT_VDA_VLD == 81); //check if the pix fmt enum has not had anything inserted or removed by mistake
     av_assert0(AV_SAMPLE_FMT_DBLP == 9);
     av_assert0(AVMEDIA_TYPE_ATTACHMENT == 4);
@@ -40,6 +45,8 @@
         abort();
     }
 
+    ff_check_pixfmt_descriptors();
+    checks_done = 1;
     return LIBAVUTIL_VERSION_INT;
 }
 
@@ -79,3 +86,22 @@
     default:                 return '?';
     }
 }
+
+unsigned av_int_list_length_for_size(unsigned elsize,
+                                     const void *list, uint64_t term)
+{
+    unsigned i;
+
+    if (!list)
+        return 0;
+#define LIST_LENGTH(type) \
+    { type t = term, *l = list; for (i = 0; l[i] != t; i++); }
+    switch (elsize) {
+    case 1: LIST_LENGTH(uint8_t);  break;
+    case 2: LIST_LENGTH(uint16_t); break;
+    case 4: LIST_LENGTH(uint32_t); break;
+    case 8: LIST_LENGTH(uint64_t); break;
+    default: av_assert0(!"valid element size");
+    }
+    return i;
+}
diff --git a/libavutil/version.h b/libavutil/version.h
index cf65fff..e46e97c 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -75,8 +75,8 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR  52
-#define LIBAVUTIL_VERSION_MINOR  17
-#define LIBAVUTIL_VERSION_MICRO 103
+#define LIBAVUTIL_VERSION_MINOR  27
+#define LIBAVUTIL_VERSION_MICRO 100
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
                                                LIBAVUTIL_VERSION_MINOR, \
@@ -132,7 +132,9 @@
 #ifndef FF_API_LLS_PRIVATE
 #define FF_API_LLS_PRIVATE              (LIBAVUTIL_VERSION_MAJOR < 53)
 #endif
-
+#ifndef FF_API_AVFRAME_LAVC
+#define FF_API_AVFRAME_LAVC             (LIBAVUTIL_VERSION_MAJOR < 53)
+#endif
 
 /**
  * @}
diff --git a/libavutil/x86/asm.h b/libavutil/x86/asm.h
index d2e76db..70ccac7 100644
--- a/libavutil/x86/asm.h
+++ b/libavutil/x86/asm.h
@@ -24,6 +24,8 @@
 #include <stdint.h>
 #include "config.h"
 
+typedef struct xmm_reg { uint64_t a, b; } xmm_reg;
+
 #if ARCH_X86_64
 #    define OPSIZE "q"
 #    define REG_a "rax"
diff --git a/libavutil/x86/float_dsp.asm b/libavutil/x86/float_dsp.asm
index 004e6cf..1c28058 100644
--- a/libavutil/x86/float_dsp.asm
+++ b/libavutil/x86/float_dsp.asm
@@ -263,3 +263,26 @@
 %endif
     RET
 
+;-----------------------------------------------------------------------------
+; void ff_butterflies_float(float *src0, float *src1, int len);
+;-----------------------------------------------------------------------------
+INIT_XMM sse
+cglobal butterflies_float, 3,3,3, src0, src1, len
+    movsxdifnidn lenq, lend
+    test         lenq, lenq
+    jz           .end
+    shl          lenq, 2
+    add         src0q, lenq
+    add         src1q, lenq
+    neg          lenq
+.loop:
+    mova           m0, [src0q + lenq]
+    mova           m1, [src1q + lenq]
+    subps          m2, m0, m1
+    addps          m0, m0, m1
+    mova      [src1q + lenq], m2
+    mova      [src0q + lenq], m0
+    add          lenq, mmsize
+    jl          .loop
+.end:
+    REP_RET
diff --git a/libavutil/x86/float_dsp_init.c b/libavutil/x86/float_dsp_init.c
index 5c6383b..ee74837 100644
--- a/libavutil/x86/float_dsp_init.c
+++ b/libavutil/x86/float_dsp_init.c
@@ -23,23 +23,23 @@
 #include "cpu.h"
 #include "asm.h"
 
-extern void ff_vector_fmul_sse(float *dst, const float *src0, const float *src1,
+void ff_vector_fmul_sse(float *dst, const float *src0, const float *src1,
+                        int len);
+void ff_vector_fmul_avx(float *dst, const float *src0, const float *src1,
+                        int len);
+
+void ff_vector_fmac_scalar_sse(float *dst, const float *src, float mul,
                                int len);
-extern void ff_vector_fmul_avx(float *dst, const float *src0, const float *src1,
+void ff_vector_fmac_scalar_avx(float *dst, const float *src, float mul,
                                int len);
 
-extern void ff_vector_fmac_scalar_sse(float *dst, const float *src, float mul,
-                                      int len);
-extern void ff_vector_fmac_scalar_avx(float *dst, const float *src, float mul,
-                                      int len);
+void ff_vector_fmul_scalar_sse(float *dst, const float *src, float mul,
+                               int len);
 
-extern void ff_vector_fmul_scalar_sse(float *dst, const float *src, float mul,
-                                      int len);
-
-extern void ff_vector_dmul_scalar_sse2(double *dst, const double *src,
-                                       double mul, int len);
-extern void ff_vector_dmul_scalar_avx(double *dst, const double *src,
-                                      double mul, int len);
+void ff_vector_dmul_scalar_sse2(double *dst, const double *src,
+                                double mul, int len);
+void ff_vector_dmul_scalar_avx(double *dst, const double *src,
+                               double mul, int len);
 
 void ff_vector_fmul_add_sse(float *dst, const float *src0, const float *src1,
                             const float *src2, int len);
@@ -53,6 +53,8 @@
 
 float ff_scalarproduct_float_sse(const float *v1, const float *v2, int order);
 
+void ff_butterflies_float_sse(float *src0, float *src1, int len);
+
 #if HAVE_6REGS && HAVE_INLINE_ASM
 static void vector_fmul_window_3dnowext(float *dst, const float *src0,
                                         const float *src1, const float *win,
@@ -138,6 +140,7 @@
         fdsp->vector_fmul_add    = ff_vector_fmul_add_sse;
         fdsp->vector_fmul_reverse = ff_vector_fmul_reverse_sse;
         fdsp->scalarproduct_float = ff_scalarproduct_float_sse;
+        fdsp->butterflies_float   = ff_butterflies_float_sse;
     }
     if (EXTERNAL_SSE2(mm_flags)) {
         fdsp->vector_dmul_scalar = ff_vector_dmul_scalar_sse2;
diff --git a/libavutil/x86/x86inc.asm b/libavutil/x86/x86inc.asm
index 068e3cf..636213b 100644
--- a/libavutil/x86/x86inc.asm
+++ b/libavutil/x86/x86inc.asm
@@ -1114,10 +1114,10 @@
 AVX_INSTR blendps, 1, 0, 0
 AVX_INSTR blendvpd, 1, 0, 0
 AVX_INSTR blendvps, 1, 0, 0
-AVX_INSTR cmppd, 1, 0, 0
-AVX_INSTR cmpps, 1, 0, 0
-AVX_INSTR cmpsd, 1, 0, 0
-AVX_INSTR cmpss, 1, 0, 0
+AVX_INSTR cmppd, 1, 1, 0
+AVX_INSTR cmpps, 1, 1, 0
+AVX_INSTR cmpsd, 1, 1, 0
+AVX_INSTR cmpss, 1, 1, 0
 AVX_INSTR cvtdq2ps, 1, 0, 0
 AVX_INSTR cvtpd2dq, 1, 0, 0
 AVX_INSTR cvtps2dq, 1, 0, 0
diff --git a/libpostproc/postprocess.c b/libpostproc/postprocess.c
index facfd2c..b34943f 100644
--- a/libpostproc/postprocess.c
+++ b/libpostproc/postprocess.c
@@ -666,7 +666,7 @@
     char temp[GET_MODE_BUFFER_SIZE];
     char *p= temp;
     static const char filterDelimiters[] = ",/";
-    static const char optionDelimiters[] = ":";
+    static const char optionDelimiters[] = ":|";
     struct PPMode *ppMode;
     char *filterToken;
 
diff --git a/libpostproc/postprocess_template.c b/libpostproc/postprocess_template.c
index ad0404f..3db49b6 100644
--- a/libpostproc/postprocess_template.c
+++ b/libpostproc/postprocess_template.c
@@ -3216,17 +3216,19 @@
 #if TEMPLATE_PP_MMX
     __asm__ volatile(
         "movq (%0), %%mm0               \n\t"
+        "movq %%mm0, (%0, %1, 4)        \n\t"
         "add %1, %0                     \n\t"
         "movq %%mm0, (%0)               \n\t"
         "movq %%mm0, (%0, %1)           \n\t"
         "movq %%mm0, (%0, %1, 2)        \n\t"
+        "movq %%mm0, (%0, %1, 4)        \n\t"
         : "+r" (src)
         : "r" ((x86_reg)-stride)
     );
 #else
     int i;
     uint8_t *p=src;
-    for(i=0; i<3; i++){
+    for(i=0; i<5; i++){
         p-= stride;
         memcpy(p, src, 8);
     }
diff --git a/libpostproc/version.h b/libpostproc/version.h
index d0d3d43..111db44 100644
--- a/libpostproc/version.h
+++ b/libpostproc/version.h
@@ -29,7 +29,7 @@
 #include "libavutil/avutil.h"
 
 #define LIBPOSTPROC_VERSION_MAJOR 52
-#define LIBPOSTPROC_VERSION_MINOR  2
+#define LIBPOSTPROC_VERSION_MINOR  3
 #define LIBPOSTPROC_VERSION_MICRO 100
 
 #define LIBPOSTPROC_VERSION_INT AV_VERSION_INT(LIBPOSTPROC_VERSION_MAJOR, \
diff --git a/library.mak b/library.mak
index e849cc2..78c437e 100644
--- a/library.mak
+++ b/library.mak
@@ -1,5 +1,3 @@
-SRC_DIR := $(SRC_PATH)/lib$(NAME)
-
 include $(SRC_PATH)/common.mak
 
 LIBVERSION := $(lib$(NAME)_VERSION)
diff --git a/libswresample/Makefile b/libswresample/Makefile
index 9d9f10c..0b75bd0 100644
--- a/libswresample/Makefile
+++ b/libswresample/Makefile
@@ -8,11 +8,11 @@
 
 OBJS = audioconvert.o                        \
        dither.o                              \
-       log2_tab.o                            \
        rematrix.o                            \
        resample.o                            \
        swresample.o                          \
 
 OBJS-$(CONFIG_LIBSOXR) += soxr_resample.o
+OBJS-$(CONFIG_SHARED)  += log2_tab.o
 
 TESTPROGS = swresample
diff --git a/libswresample/rematrix.c b/libswresample/rematrix.c
index 51658ce..84c55ee 100644
--- a/libswresample/rematrix.c
+++ b/libswresample/rematrix.c
@@ -337,7 +337,7 @@
             return r;
     }
     if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
-        s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(int));
+        s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
         s->native_one    = av_mallocz(sizeof(int));
         for (i = 0; i < nb_out; i++)
             for (j = 0; j < nb_in; j++)
@@ -347,7 +347,7 @@
         s->mix_2_1_f = (mix_2_1_func_type*)sum2_s16;
         s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s16(s);
     }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
-        s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(float));
+        s->native_matrix = av_calloc(nb_in * nb_out, sizeof(float));
         s->native_one    = av_mallocz(sizeof(float));
         for (i = 0; i < nb_out; i++)
             for (j = 0; j < nb_in; j++)
@@ -357,7 +357,7 @@
         s->mix_2_1_f = (mix_2_1_func_type*)sum2_float;
         s->mix_any_f = (mix_any_func_type*)get_mix_any_func_float(s);
     }else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
-        s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(double));
+        s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
         s->native_one    = av_mallocz(sizeof(double));
         for (i = 0; i < nb_out; i++)
             for (j = 0; j < nb_in; j++)
diff --git a/libswscale/input.c b/libswscale/input.c
index 2def2de..5259603 100644
--- a/libswscale/input.c
+++ b/libswscale/input.c
@@ -36,17 +36,6 @@
 #include "swscale.h"
 #include "swscale_internal.h"
 
-#define RGB2YUV_SHIFT 15
-#define BY  ((int)(0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
-#define BV (-(int)(0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
-#define BU  ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
-#define GY  ((int)(0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
-#define GV (-(int)(0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
-#define GU (-(int)(0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
-#define RY  ((int)(0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
-#define RV  ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
-#define RU (-(int)(0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5))
-
 #define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
 
 #define r ((origin == AV_PIX_FMT_BGR48BE || origin == AV_PIX_FMT_BGR48LE) ? b_r : r_b)
@@ -54,79 +43,84 @@
 
 static av_always_inline void
 rgb64ToY_c_template(uint16_t *dst, const uint16_t *src, int width,
-                    enum AVPixelFormat origin)
+                    enum AVPixelFormat origin, int32_t *rgb2yuv)
 {
+    int32_t ry = rgb2yuv[RY_IDX], gy = rgb2yuv[GY_IDX], by = rgb2yuv[BY_IDX];
     int i;
     for (i = 0; i < width; i++) {
         unsigned int r_b = input_pixel(&src[i*4+0]);
         unsigned int   g = input_pixel(&src[i*4+1]);
         unsigned int b_r = input_pixel(&src[i*4+2]);
 
-        dst[i] = (RY*r + GY*g + BY*b + (0x2001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
+        dst[i] = (ry*r + gy*g + by*b + (0x2001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
     }
 }
 
 static av_always_inline void
 rgb64ToUV_c_template(uint16_t *dstU, uint16_t *dstV,
                     const uint16_t *src1, const uint16_t *src2,
-                    int width, enum AVPixelFormat origin)
+                    int width, enum AVPixelFormat origin, int32_t *rgb2yuv)
 {
     int i;
+    int32_t ru = rgb2yuv[RU_IDX], gu = rgb2yuv[GU_IDX], bu = rgb2yuv[BU_IDX];
+    int32_t rv = rgb2yuv[RV_IDX], gv = rgb2yuv[GV_IDX], bv = rgb2yuv[BV_IDX];
     av_assert1(src1==src2);
     for (i = 0; i < width; i++) {
         int r_b = input_pixel(&src1[i*4+0]);
         int   g = input_pixel(&src1[i*4+1]);
         int b_r = input_pixel(&src1[i*4+2]);
 
-        dstU[i] = (RU*r + GU*g + BU*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
-        dstV[i] = (RV*r + GV*g + BV*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
+        dstU[i] = (ru*r + gu*g + bu*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
+        dstV[i] = (rv*r + gv*g + bv*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
     }
 }
 
 static av_always_inline void
 rgb64ToUV_half_c_template(uint16_t *dstU, uint16_t *dstV,
                           const uint16_t *src1, const uint16_t *src2,
-                          int width, enum AVPixelFormat origin)
+                          int width, enum AVPixelFormat origin, int32_t *rgb2yuv)
 {
     int i;
+    int32_t ru = rgb2yuv[RU_IDX], gu = rgb2yuv[GU_IDX], bu = rgb2yuv[BU_IDX];
+    int32_t rv = rgb2yuv[RV_IDX], gv = rgb2yuv[GV_IDX], bv = rgb2yuv[BV_IDX];
     av_assert1(src1==src2);
     for (i = 0; i < width; i++) {
         int r_b = (input_pixel(&src1[8 * i + 0]) + input_pixel(&src1[8 * i + 4]) + 1) >> 1;
         int   g = (input_pixel(&src1[8 * i + 1]) + input_pixel(&src1[8 * i + 5]) + 1) >> 1;
         int b_r = (input_pixel(&src1[8 * i + 2]) + input_pixel(&src1[8 * i + 6]) + 1) >> 1;
 
-        dstU[i]= (RU*r + GU*g + BU*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
-        dstV[i]= (RV*r + GV*g + BV*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
+        dstU[i]= (ru*r + gu*g + bu*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
+        dstV[i]= (rv*r + gv*g + bv*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
     }
 }
 
 #define rgb64funcs(pattern, BE_LE, origin) \
 static void pattern ## 64 ## BE_LE ## ToY_c(uint8_t *_dst, const uint8_t *_src, const uint8_t *unused0, const uint8_t *unused1,\
-                                    int width, uint32_t *unused) \
+                                    int width, uint32_t *rgb2yuv) \
 { \
     const uint16_t *src = (const uint16_t *) _src; \
     uint16_t *dst = (uint16_t *) _dst; \
-    rgb64ToY_c_template(dst, src, width, origin); \
+    rgb64ToY_c_template(dst, src, width, origin, rgb2yuv); \
 } \
  \
 static void pattern ## 64 ## BE_LE ## ToUV_c(uint8_t *_dstU, uint8_t *_dstV, \
                                     const uint8_t *unused0, const uint8_t *_src1, const uint8_t *_src2, \
-                                    int width, uint32_t *unused) \
+                                    int width, uint32_t *rgb2yuv) \
 { \
     const uint16_t *src1 = (const uint16_t *) _src1, \
                    *src2 = (const uint16_t *) _src2; \
     uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV; \
-    rgb64ToUV_c_template(dstU, dstV, src1, src2, width, origin); \
+    rgb64ToUV_c_template(dstU, dstV, src1, src2, width, origin, rgb2yuv); \
 } \
  \
 static void pattern ## 64 ## BE_LE ## ToUV_half_c(uint8_t *_dstU, uint8_t *_dstV, \
                                     const uint8_t *unused0, const uint8_t *_src1, const uint8_t *_src2, \
-                                    int width, uint32_t *unused) \
+                                    int width, uint32_t *rgb2yuv) \
 { \
     const uint16_t *src1 = (const uint16_t *) _src1, \
                    *src2 = (const uint16_t *) _src2; \
     uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV; \
-    rgb64ToUV_half_c_template(dstU, dstV, src1, src2, width, origin); \
+    rgb64ToUV_half_c_template(dstU, dstV, src1, src2, width, origin, rgb2yuv); \
 }
 
 rgb64funcs(rgb, LE, AV_PIX_FMT_RGBA64LE)
@@ -134,15 +128,17 @@
 
 static av_always_inline void rgb48ToY_c_template(uint16_t *dst,
                                                  const uint16_t *src, int width,
-                                                 enum AVPixelFormat origin)
+                                                 enum AVPixelFormat origin,
+                                                 int32_t *rgb2yuv)
 {
+    int32_t ry = rgb2yuv[RY_IDX], gy = rgb2yuv[GY_IDX], by = rgb2yuv[BY_IDX];
     int i;
     for (i = 0; i < width; i++) {
         unsigned int r_b = input_pixel(&src[i * 3 + 0]);
         unsigned int g   = input_pixel(&src[i * 3 + 1]);
         unsigned int b_r = input_pixel(&src[i * 3 + 2]);
 
-        dst[i] = (RY * r + GY * g + BY * b + (0x2001 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
+        dst[i] = (ry*r + gy*g + by*b + (0x2001 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
     }
 }
 
@@ -151,17 +147,20 @@
                                                   const uint16_t *src1,
                                                   const uint16_t *src2,
                                                   int width,
-                                                  enum AVPixelFormat origin)
+                                                  enum AVPixelFormat origin,
+                                                  int32_t *rgb2yuv)
 {
     int i;
+    int32_t ru = rgb2yuv[RU_IDX], gu = rgb2yuv[GU_IDX], bu = rgb2yuv[BU_IDX];
+    int32_t rv = rgb2yuv[RV_IDX], gv = rgb2yuv[GV_IDX], bv = rgb2yuv[BV_IDX];
     av_assert1(src1 == src2);
     for (i = 0; i < width; i++) {
         int r_b = input_pixel(&src1[i * 3 + 0]);
         int g   = input_pixel(&src1[i * 3 + 1]);
         int b_r = input_pixel(&src1[i * 3 + 2]);
 
-        dstU[i] = (RU * r + GU * g + BU * b + (0x10001 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
-        dstV[i] = (RV * r + GV * g + BV * b + (0x10001 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
+        dstU[i] = (ru*r + gu*g + bu*b + (0x10001 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
+        dstV[i] = (rv*r + gv*g + bv*b + (0x10001 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
     }
 }
 
@@ -170,9 +169,12 @@
                                                        const uint16_t *src1,
                                                        const uint16_t *src2,
                                                        int width,
-                                                       enum AVPixelFormat origin)
+                                                       enum AVPixelFormat origin,
+                                                       int32_t *rgb2yuv)
 {
     int i;
+    int32_t ru = rgb2yuv[RU_IDX], gu = rgb2yuv[GU_IDX], bu = rgb2yuv[BU_IDX];
+    int32_t rv = rgb2yuv[RV_IDX], gv = rgb2yuv[GV_IDX], bv = rgb2yuv[BV_IDX];
     av_assert1(src1 == src2);
     for (i = 0; i < width; i++) {
         int r_b = (input_pixel(&src1[6 * i + 0]) +
@@ -182,8 +184,8 @@
         int b_r = (input_pixel(&src1[6 * i + 2]) +
                    input_pixel(&src1[6 * i + 5]) + 1) >> 1;
 
-        dstU[i] = (RU * r + GU * g + BU * b + (0x10001 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
-        dstV[i] = (RV * r + GV * g + BV * b + (0x10001 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
+        dstU[i] = (ru*r + gu*g + bu*b + (0x10001 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
+        dstV[i] = (rv*r + gv*g + bv*b + (0x10001 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT;
     }
 }
 
@@ -196,11 +198,11 @@
                                             const uint8_t *_src,        \
                                             const uint8_t *unused0, const uint8_t *unused1,\
                                             int width,                  \
-                                            uint32_t *unused)           \
+                                            uint32_t *rgb2yuv)          \
 {                                                                       \
     const uint16_t *src = (const uint16_t *)_src;                       \
     uint16_t *dst       = (uint16_t *)_dst;                             \
-    rgb48ToY_c_template(dst, src, width, origin);                       \
+    rgb48ToY_c_template(dst, src, width, origin, rgb2yuv);              \
 }                                                                       \
                                                                         \
 static void pattern ## 48 ## BE_LE ## ToUV_c(uint8_t *_dstU,            \
@@ -209,13 +211,13 @@
                                              const uint8_t *_src1,      \
                                              const uint8_t *_src2,      \
                                              int width,                 \
-                                             uint32_t *unused)          \
+                                             uint32_t *rgb2yuv)         \
 {                                                                       \
     const uint16_t *src1 = (const uint16_t *)_src1,                     \
                    *src2 = (const uint16_t *)_src2;                     \
     uint16_t *dstU = (uint16_t *)_dstU,                                 \
              *dstV = (uint16_t *)_dstV;                                 \
-    rgb48ToUV_c_template(dstU, dstV, src1, src2, width, origin);        \
+    rgb48ToUV_c_template(dstU, dstV, src1, src2, width, origin, rgb2yuv);        \
 }                                                                       \
                                                                         \
 static void pattern ## 48 ## BE_LE ## ToUV_half_c(uint8_t *_dstU,       \
@@ -224,13 +226,13 @@
                                                   const uint8_t *_src1, \
                                                   const uint8_t *_src2, \
                                                   int width,            \
-                                                  uint32_t *unused)     \
+                                                  uint32_t *rgb2yuv)    \
 {                                                                       \
     const uint16_t *src1 = (const uint16_t *)_src1,                     \
                    *src2 = (const uint16_t *)_src2;                     \
     uint16_t *dstU = (uint16_t *)_dstU,                                 \
              *dstV = (uint16_t *)_dstV;                                 \
-    rgb48ToUV_half_c_template(dstU, dstV, src1, src2, width, origin);   \
+    rgb48ToUV_half_c_template(dstU, dstV, src1, src2, width, origin, rgb2yuv);   \
 }
 
 rgb48funcs(rgb, LE, AV_PIX_FMT_RGB48LE)
@@ -254,9 +256,10 @@
                                                     int shb, int shp,
                                                     int maskr, int maskg,
                                                     int maskb, int rsh,
-                                                    int gsh, int bsh, int S)
+                                                    int gsh, int bsh, int S,
+                                                    int32_t *rgb2yuv)
 {
-    const int ry       = RY << rsh, gy = GY << gsh, by = BY << bsh;
+    const int ry       = rgb2yuv[RY_IDX]<<rsh, gy = rgb2yuv[GY_IDX]<<gsh, by = rgb2yuv[BY_IDX]<<bsh;
     const unsigned rnd = (32<<((S)-1)) + (1<<(S-7));
     int i;
 
@@ -279,10 +282,11 @@
                                                      int shb, int shp,
                                                      int maskr, int maskg,
                                                      int maskb, int rsh,
-                                                     int gsh, int bsh, int S)
+                                                     int gsh, int bsh, int S,
+                                                     int32_t *rgb2yuv)
 {
-    const int ru       = RU << rsh, gu = GU << gsh, bu = BU << bsh,
-              rv       = RV << rsh, gv = GV << gsh, bv = BV << bsh;
+    const int ru       = rgb2yuv[RU_IDX] << rsh, gu = rgb2yuv[GU_IDX] << gsh, bu = rgb2yuv[BU_IDX] << bsh,
+              rv       = rgb2yuv[RV_IDX] << rsh, gv = rgb2yuv[GV_IDX] << gsh, bv = rgb2yuv[BV_IDX] << bsh;
     const unsigned rnd = (256u<<((S)-1)) + (1<<(S-7));
     int i;
 
@@ -306,10 +310,11 @@
                                                           int shb, int shp,
                                                           int maskr, int maskg,
                                                           int maskb, int rsh,
-                                                          int gsh, int bsh, int S)
+                                                          int gsh, int bsh, int S,
+                                                          int32_t *rgb2yuv)
 {
-    const int ru       = RU << rsh, gu = GU << gsh, bu = BU << bsh,
-              rv       = RV << rsh, gv = GV << gsh, bv = BV << bsh,
+    const int ru       = rgb2yuv[RU_IDX] << rsh, gu = rgb2yuv[GU_IDX] << gsh, bu = rgb2yuv[BU_IDX] << bsh,
+              rv       = rgb2yuv[RV_IDX] << rsh, gv = rgb2yuv[GV_IDX] << gsh, bv = rgb2yuv[BV_IDX] << bsh,
               maskgx   = ~(maskr | maskb);
     const unsigned rnd = (256U<<(S)) + (1<<(S-6));
     int i;
@@ -343,30 +348,30 @@
 #define rgb16_32_wrapper(fmt, name, shr, shg, shb, shp, maskr,          \
                          maskg, maskb, rsh, gsh, bsh, S)                \
 static void name ## ToY_c(uint8_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2,            \
-                          int width, uint32_t *unused)                  \
+                          int width, uint32_t *tab)                     \
 {                                                                       \
     rgb16_32ToY_c_template((int16_t*)dst, src, width, fmt, shr, shg, shb, shp,    \
-                           maskr, maskg, maskb, rsh, gsh, bsh, S);      \
+                           maskr, maskg, maskb, rsh, gsh, bsh, S, tab); \
 }                                                                       \
                                                                         \
 static void name ## ToUV_c(uint8_t *dstU, uint8_t *dstV,                \
                            const uint8_t *unused0, const uint8_t *src, const uint8_t *dummy,    \
-                           int width, uint32_t *unused)                 \
+                           int width, uint32_t *tab)                    \
 {                                                                       \
     rgb16_32ToUV_c_template((int16_t*)dstU, (int16_t*)dstV, src, width, fmt,                \
                             shr, shg, shb, shp,                         \
-                            maskr, maskg, maskb, rsh, gsh, bsh, S);     \
+                            maskr, maskg, maskb, rsh, gsh, bsh, S, tab);\
 }                                                                       \
                                                                         \
 static void name ## ToUV_half_c(uint8_t *dstU, uint8_t *dstV,           \
                                 const uint8_t *unused0, const uint8_t *src,                     \
                                 const uint8_t *dummy,                   \
-                                int width, uint32_t *unused)            \
+                                int width, uint32_t *tab)               \
 {                                                                       \
     rgb16_32ToUV_half_c_template((int16_t*)dstU, (int16_t*)dstV, src, width, fmt,           \
                                  shr, shg, shb, shp,                    \
                                  maskr, maskg, maskb,                   \
-                                 rsh, gsh, bsh, S);                     \
+                                 rsh, gsh, bsh, S, tab);                \
 }
 
 rgb16_32_wrapper(AV_PIX_FMT_BGR32,    bgr32,  16, 0,  0, 0, 0xFF0000, 0xFF00,   0x00FF,  8, 0,  8, RGB2YUV_SHIFT + 8)
@@ -388,18 +393,21 @@
 
 static void gbr24pToUV_half_c(uint8_t *_dstU, uint8_t *_dstV,
                          const uint8_t *gsrc, const uint8_t *bsrc, const uint8_t *rsrc,
-                         int width, uint32_t *unused)
+                         int width, uint32_t *rgb2yuv)
 {
     uint16_t *dstU = (uint16_t *)_dstU;
     uint16_t *dstV = (uint16_t *)_dstV;
+    int32_t ru = rgb2yuv[RU_IDX], gu = rgb2yuv[GU_IDX], bu = rgb2yuv[BU_IDX];
+    int32_t rv = rgb2yuv[RV_IDX], gv = rgb2yuv[GV_IDX], bv = rgb2yuv[BV_IDX];
+
     int i;
     for (i = 0; i < width; i++) {
         unsigned int g   = gsrc[2*i] + gsrc[2*i+1];
         unsigned int b   = bsrc[2*i] + bsrc[2*i+1];
         unsigned int r   = rsrc[2*i] + rsrc[2*i+1];
 
-        dstU[i] = (RU*r + GU*g + BU*b + (0x4001<<(RGB2YUV_SHIFT-6))) >> (RGB2YUV_SHIFT-6+1);
-        dstV[i] = (RV*r + GV*g + BV*b + (0x4001<<(RGB2YUV_SHIFT-6))) >> (RGB2YUV_SHIFT-6+1);
+        dstU[i] = (ru*r + gu*g + bu*b + (0x4001<<(RGB2YUV_SHIFT-6))) >> (RGB2YUV_SHIFT-6+1);
+        dstV[i] = (rv*r + gv*g + bv*b + (0x4001<<(RGB2YUV_SHIFT-6))) >> (RGB2YUV_SHIFT-6+1);
     }
 }
 
@@ -593,273 +601,289 @@
 #define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
 
 static void bgr24ToY_c(uint8_t *_dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2,
-                       int width, uint32_t *unused)
+                       int width, uint32_t *rgb2yuv)
 {
     int16_t *dst = (int16_t *)_dst;
+    int32_t ry = rgb2yuv[RY_IDX], gy = rgb2yuv[GY_IDX], by = rgb2yuv[BY_IDX];
     int i;
     for (i = 0; i < width; i++) {
         int b = src[i * 3 + 0];
         int g = src[i * 3 + 1];
         int r = src[i * 3 + 2];
 
-        dst[i] = ((RY*r + GY*g + BY*b + (32<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6));
+        dst[i] = ((ry*r + gy*g + by*b + (32<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6));
     }
 }
 
 static void bgr24ToUV_c(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *unused0, const uint8_t *src1,
-                        const uint8_t *src2, int width, uint32_t *unused)
+                        const uint8_t *src2, int width, uint32_t *rgb2yuv)
 {
     int16_t *dstU = (int16_t *)_dstU;
     int16_t *dstV = (int16_t *)_dstV;
+    int32_t ru = rgb2yuv[RU_IDX], gu = rgb2yuv[GU_IDX], bu = rgb2yuv[BU_IDX];
+    int32_t rv = rgb2yuv[RV_IDX], gv = rgb2yuv[GV_IDX], bv = rgb2yuv[BV_IDX];
     int i;
     for (i = 0; i < width; i++) {
         int b = src1[3 * i + 0];
         int g = src1[3 * i + 1];
         int r = src1[3 * i + 2];
 
-        dstU[i] = (RU*r + GU*g + BU*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
-        dstV[i] = (RV*r + GV*g + BV*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
+        dstU[i] = (ru*r + gu*g + bu*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
+        dstV[i] = (rv*r + gv*g + bv*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
     }
     av_assert1(src1 == src2);
 }
 
 static void bgr24ToUV_half_c(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *unused0, const uint8_t *src1,
-                             const uint8_t *src2, int width, uint32_t *unused)
+                             const uint8_t *src2, int width, uint32_t *rgb2yuv)
 {
     int16_t *dstU = (int16_t *)_dstU;
     int16_t *dstV = (int16_t *)_dstV;
     int i;
+    int32_t ru = rgb2yuv[RU_IDX], gu = rgb2yuv[GU_IDX], bu = rgb2yuv[BU_IDX];
+    int32_t rv = rgb2yuv[RV_IDX], gv = rgb2yuv[GV_IDX], bv = rgb2yuv[BV_IDX];
     for (i = 0; i < width; i++) {
         int b = src1[6 * i + 0] + src1[6 * i + 3];
         int g = src1[6 * i + 1] + src1[6 * i + 4];
         int r = src1[6 * i + 2] + src1[6 * i + 5];
 
-        dstU[i] = (RU*r + GU*g + BU*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
-        dstV[i] = (RV*r + GV*g + BV*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
+        dstU[i] = (ru*r + gu*g + bu*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
+        dstV[i] = (rv*r + gv*g + bv*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
     }
     av_assert1(src1 == src2);
 }
 
 static void rgb24ToY_c(uint8_t *_dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width,
-                       uint32_t *unused)
+                       uint32_t *rgb2yuv)
 {
     int16_t *dst = (int16_t *)_dst;
+    int32_t ry = rgb2yuv[RY_IDX], gy = rgb2yuv[GY_IDX], by = rgb2yuv[BY_IDX];
     int i;
     for (i = 0; i < width; i++) {
         int r = src[i * 3 + 0];
         int g = src[i * 3 + 1];
         int b = src[i * 3 + 2];
 
-        dst[i] = ((RY*r + GY*g + BY*b + (32<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6));
+        dst[i] = ((ry*r + gy*g + by*b + (32<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6));
     }
 }
 
 static void rgb24ToUV_c(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *unused0, const uint8_t *src1,
-                        const uint8_t *src2, int width, uint32_t *unused)
+                        const uint8_t *src2, int width, uint32_t *rgb2yuv)
 {
     int16_t *dstU = (int16_t *)_dstU;
     int16_t *dstV = (int16_t *)_dstV;
     int i;
+    int32_t ru = rgb2yuv[RU_IDX], gu = rgb2yuv[GU_IDX], bu = rgb2yuv[BU_IDX];
+    int32_t rv = rgb2yuv[RV_IDX], gv = rgb2yuv[GV_IDX], bv = rgb2yuv[BV_IDX];
     av_assert1(src1 == src2);
     for (i = 0; i < width; i++) {
         int r = src1[3 * i + 0];
         int g = src1[3 * i + 1];
         int b = src1[3 * i + 2];
 
-        dstU[i] = (RU*r + GU*g + BU*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
-        dstV[i] = (RV*r + GV*g + BV*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
+        dstU[i] = (ru*r + gu*g + bu*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
+        dstV[i] = (rv*r + gv*g + bv*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
     }
 }
 
 static void rgb24ToUV_half_c(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *unused0, const uint8_t *src1,
-                             const uint8_t *src2, int width, uint32_t *unused)
+                             const uint8_t *src2, int width, uint32_t *rgb2yuv)
 {
     int16_t *dstU = (int16_t *)_dstU;
     int16_t *dstV = (int16_t *)_dstV;
     int i;
+    int32_t ru = rgb2yuv[RU_IDX], gu = rgb2yuv[GU_IDX], bu = rgb2yuv[BU_IDX];
+    int32_t rv = rgb2yuv[RV_IDX], gv = rgb2yuv[GV_IDX], bv = rgb2yuv[BV_IDX];
     av_assert1(src1 == src2);
     for (i = 0; i < width; i++) {
         int r = src1[6 * i + 0] + src1[6 * i + 3];
         int g = src1[6 * i + 1] + src1[6 * i + 4];
         int b = src1[6 * i + 2] + src1[6 * i + 5];
 
-        dstU[i] = (RU*r + GU*g + BU*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
-        dstV[i] = (RV*r + GV*g + BV*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
+        dstU[i] = (ru*r + gu*g + bu*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
+        dstV[i] = (rv*r + gv*g + bv*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
     }
 }
 
-static void planar_rgb_to_y(uint8_t *_dst, const uint8_t *src[4], int width)
+static void planar_rgb_to_y(uint8_t *_dst, const uint8_t *src[4], int width, int32_t *rgb2yuv)
 {
     uint16_t *dst = (uint16_t *)_dst;
+    int32_t ry = rgb2yuv[RY_IDX], gy = rgb2yuv[GY_IDX], by = rgb2yuv[BY_IDX];
     int i;
     for (i = 0; i < width; i++) {
         int g = src[0][i];
         int b = src[1][i];
         int r = src[2][i];
 
-        dst[i] = (RY*r + GY*g + BY*b + (0x801<<(RGB2YUV_SHIFT-7))) >> (RGB2YUV_SHIFT-6);
+        dst[i] = (ry*r + gy*g + by*b + (0x801<<(RGB2YUV_SHIFT-7))) >> (RGB2YUV_SHIFT-6);
     }
 }
 
-static void planar_rgb_to_uv(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *src[4], int width)
+static void planar_rgb_to_uv(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *src[4], int width, int32_t *rgb2yuv)
 {
     uint16_t *dstU = (uint16_t *)_dstU;
     uint16_t *dstV = (uint16_t *)_dstV;
+    int32_t ru = rgb2yuv[RU_IDX], gu = rgb2yuv[GU_IDX], bu = rgb2yuv[BU_IDX];
+    int32_t rv = rgb2yuv[RV_IDX], gv = rgb2yuv[GV_IDX], bv = rgb2yuv[BV_IDX];
     int i;
     for (i = 0; i < width; i++) {
         int g = src[0][i];
         int b = src[1][i];
         int r = src[2][i];
 
-        dstU[i] = (RU*r + GU*g + BU*b + (0x4001<<(RGB2YUV_SHIFT-7))) >> (RGB2YUV_SHIFT-6);
-        dstV[i] = (RV*r + GV*g + BV*b + (0x4001<<(RGB2YUV_SHIFT-7))) >> (RGB2YUV_SHIFT-6);
+        dstU[i] = (ru*r + gu*g + bu*b + (0x4001<<(RGB2YUV_SHIFT-7))) >> (RGB2YUV_SHIFT-6);
+        dstV[i] = (rv*r + gv*g + bv*b + (0x4001<<(RGB2YUV_SHIFT-7))) >> (RGB2YUV_SHIFT-6);
     }
 }
 
 #define rdpx(src) \
     is_be ? AV_RB16(src) : AV_RL16(src)
 static av_always_inline void planar_rgb16_to_y(uint8_t *_dst, const uint8_t *_src[4],
-                                               int width, int bpc, int is_be)
+                                               int width, int bpc, int is_be, int32_t *rgb2yuv)
 {
     int i;
     const uint16_t **src = (const uint16_t **)_src;
     uint16_t *dst        = (uint16_t *)_dst;
+    int32_t ry = rgb2yuv[RY_IDX], gy = rgb2yuv[GY_IDX], by = rgb2yuv[BY_IDX];
     for (i = 0; i < width; i++) {
         int g = rdpx(src[0] + i);
         int b = rdpx(src[1] + i);
         int r = rdpx(src[2] + i);
 
-        dst[i] = ((RY * r + GY * g + BY * b + (33 << (RGB2YUV_SHIFT + bpc - 9))) >> (RGB2YUV_SHIFT + bpc - 14));
+        dst[i] = ((ry*r + gy*g + by*b + (33 << (RGB2YUV_SHIFT + bpc - 9))) >> (RGB2YUV_SHIFT + bpc - 14));
     }
 }
 
-static void planar_rgb9le_to_y(uint8_t *dst, const uint8_t *src[4], int w)
+static void planar_rgb9le_to_y(uint8_t *dst, const uint8_t *src[4], int w, int32_t *rgb2yuv)
 {
-    planar_rgb16_to_y(dst, src, w, 9, 0);
+    planar_rgb16_to_y(dst, src, w, 9, 0, rgb2yuv);
 }
 
-static void planar_rgb9be_to_y(uint8_t *dst, const uint8_t *src[4], int w)
+static void planar_rgb9be_to_y(uint8_t *dst, const uint8_t *src[4], int w, int32_t *rgb2yuv)
 {
-    planar_rgb16_to_y(dst, src, w, 9, 1);
+    planar_rgb16_to_y(dst, src, w, 9, 1, rgb2yuv);
 }
 
-static void planar_rgb10le_to_y(uint8_t *dst, const uint8_t *src[4], int w)
+static void planar_rgb10le_to_y(uint8_t *dst, const uint8_t *src[4], int w, int32_t *rgb2yuv)
 {
-    planar_rgb16_to_y(dst, src, w, 10, 0);
+    planar_rgb16_to_y(dst, src, w, 10, 0, rgb2yuv);
 }
 
-static void planar_rgb10be_to_y(uint8_t *dst, const uint8_t *src[4], int w)
+static void planar_rgb10be_to_y(uint8_t *dst, const uint8_t *src[4], int w, int32_t *rgb2yuv)
 {
-    planar_rgb16_to_y(dst, src, w, 10, 1);
+    planar_rgb16_to_y(dst, src, w, 10, 1, rgb2yuv);
 }
 
-static void planar_rgb12le_to_y(uint8_t *dst, const uint8_t *src[4], int w)
+static void planar_rgb12le_to_y(uint8_t *dst, const uint8_t *src[4], int w, int32_t *rgb2yuv)
 {
-    planar_rgb16_to_y(dst, src, w, 12, 0);
+    planar_rgb16_to_y(dst, src, w, 12, 0, rgb2yuv);
 }
 
-static void planar_rgb12be_to_y(uint8_t *dst, const uint8_t *src[4], int w)
+static void planar_rgb12be_to_y(uint8_t *dst, const uint8_t *src[4], int w, int32_t *rgb2yuv)
 {
-    planar_rgb16_to_y(dst, src, w, 12, 1);
+    planar_rgb16_to_y(dst, src, w, 12, 1, rgb2yuv);
 }
 
-static void planar_rgb14le_to_y(uint8_t *dst, const uint8_t *src[4], int w)
+static void planar_rgb14le_to_y(uint8_t *dst, const uint8_t *src[4], int w, int32_t *rgb2yuv)
 {
-    planar_rgb16_to_y(dst, src, w, 14, 0);
+    planar_rgb16_to_y(dst, src, w, 14, 0, rgb2yuv);
 }
 
-static void planar_rgb14be_to_y(uint8_t *dst, const uint8_t *src[4], int w)
+static void planar_rgb14be_to_y(uint8_t *dst, const uint8_t *src[4], int w, int32_t *rgb2yuv)
 {
-    planar_rgb16_to_y(dst, src, w, 14, 1);
+    planar_rgb16_to_y(dst, src, w, 14, 1, rgb2yuv);
 }
 
-static void planar_rgb16le_to_y(uint8_t *dst, const uint8_t *src[4], int w)
+static void planar_rgb16le_to_y(uint8_t *dst, const uint8_t *src[4], int w, int32_t *rgb2yuv)
 {
-    planar_rgb16_to_y(dst, src, w, 16, 0);
+    planar_rgb16_to_y(dst, src, w, 16, 0, rgb2yuv);
 }
 
-static void planar_rgb16be_to_y(uint8_t *dst, const uint8_t *src[4], int w)
+static void planar_rgb16be_to_y(uint8_t *dst, const uint8_t *src[4], int w, int32_t *rgb2yuv)
 {
-    planar_rgb16_to_y(dst, src, w, 16, 1);
+    planar_rgb16_to_y(dst, src, w, 16, 1, rgb2yuv);
 }
 
 static av_always_inline void planar_rgb16_to_uv(uint8_t *_dstU, uint8_t *_dstV,
                                                 const uint8_t *_src[4], int width,
-                                                int bpc, int is_be)
+                                                int bpc, int is_be, int32_t *rgb2yuv)
 {
     int i;
     const uint16_t **src = (const uint16_t **)_src;
     uint16_t *dstU       = (uint16_t *)_dstU;
     uint16_t *dstV       = (uint16_t *)_dstV;
+    int32_t ru = rgb2yuv[RU_IDX], gu = rgb2yuv[GU_IDX], bu = rgb2yuv[BU_IDX];
+    int32_t rv = rgb2yuv[RV_IDX], gv = rgb2yuv[GV_IDX], bv = rgb2yuv[BV_IDX];
     for (i = 0; i < width; i++) {
         int g = rdpx(src[0] + i);
         int b = rdpx(src[1] + i);
         int r = rdpx(src[2] + i);
 
-        dstU[i] = (RU * r + GU * g + BU * b + (257 << (RGB2YUV_SHIFT + bpc - 9))) >> (RGB2YUV_SHIFT + bpc - 14);
-        dstV[i] = (RV * r + GV * g + BV * b + (257 << (RGB2YUV_SHIFT + bpc - 9))) >> (RGB2YUV_SHIFT + bpc - 14);
+        dstU[i] = (ru*r + gu*g + bu*b + (257 << (RGB2YUV_SHIFT + bpc - 9))) >> (RGB2YUV_SHIFT + bpc - 14);
+        dstV[i] = (rv*r + gv*g + bv*b + (257 << (RGB2YUV_SHIFT + bpc - 9))) >> (RGB2YUV_SHIFT + bpc - 14);
     }
 }
 #undef rdpx
 
 static void planar_rgb9le_to_uv(uint8_t *dstU, uint8_t *dstV,
-                                const uint8_t *src[4], int w)
+                                const uint8_t *src[4], int w, int32_t *rgb2yuv)
 {
-    planar_rgb16_to_uv(dstU, dstV, src, w, 9, 0);
+    planar_rgb16_to_uv(dstU, dstV, src, w, 9, 0, rgb2yuv);
 }
 
 static void planar_rgb9be_to_uv(uint8_t *dstU, uint8_t *dstV,
-                                const uint8_t *src[4], int w)
+                                const uint8_t *src[4], int w, int32_t *rgb2yuv)
 {
-    planar_rgb16_to_uv(dstU, dstV, src, w, 9, 1);
+    planar_rgb16_to_uv(dstU, dstV, src, w, 9, 1, rgb2yuv);
 }
 
 static void planar_rgb10le_to_uv(uint8_t *dstU, uint8_t *dstV,
-                                 const uint8_t *src[4], int w)
+                                 const uint8_t *src[4], int w, int32_t *rgb2yuv)
 {
-    planar_rgb16_to_uv(dstU, dstV, src, w, 10, 0);
+    planar_rgb16_to_uv(dstU, dstV, src, w, 10, 0, rgb2yuv);
 }
 
 static void planar_rgb10be_to_uv(uint8_t *dstU, uint8_t *dstV,
-                                 const uint8_t *src[4], int w)
+                                 const uint8_t *src[4], int w, int32_t *rgb2yuv)
 {
-    planar_rgb16_to_uv(dstU, dstV, src, w, 10, 1);
+    planar_rgb16_to_uv(dstU, dstV, src, w, 10, 1, rgb2yuv);
 }
 
 static void planar_rgb12le_to_uv(uint8_t *dstU, uint8_t *dstV,
-                                 const uint8_t *src[4], int w)
+                                 const uint8_t *src[4], int w, int32_t *rgb2yuv)
 {
-    planar_rgb16_to_uv(dstU, dstV, src, w, 12, 0);
+    planar_rgb16_to_uv(dstU, dstV, src, w, 12, 0, rgb2yuv);
 }
 
 static void planar_rgb12be_to_uv(uint8_t *dstU, uint8_t *dstV,
-                                 const uint8_t *src[4], int w)
+                                 const uint8_t *src[4], int w, int32_t *rgb2yuv)
 {
-    planar_rgb16_to_uv(dstU, dstV, src, w, 12, 1);
+    planar_rgb16_to_uv(dstU, dstV, src, w, 12, 1, rgb2yuv);
 }
 
 static void planar_rgb14le_to_uv(uint8_t *dstU, uint8_t *dstV,
-                                 const uint8_t *src[4], int w)
+                                 const uint8_t *src[4], int w, int32_t *rgb2yuv)
 {
-    planar_rgb16_to_uv(dstU, dstV, src, w, 14, 0);
+    planar_rgb16_to_uv(dstU, dstV, src, w, 14, 0, rgb2yuv);
 }
 
 static void planar_rgb14be_to_uv(uint8_t *dstU, uint8_t *dstV,
-                                 const uint8_t *src[4], int w)
+                                 const uint8_t *src[4], int w, int32_t *rgb2yuv)
 {
-    planar_rgb16_to_uv(dstU, dstV, src, w, 14, 1);
+    planar_rgb16_to_uv(dstU, dstV, src, w, 14, 1, rgb2yuv);
 }
 
 static void planar_rgb16le_to_uv(uint8_t *dstU, uint8_t *dstV,
-                                 const uint8_t *src[4], int w)
+                                 const uint8_t *src[4], int w, int32_t *rgb2yuv)
 {
-    planar_rgb16_to_uv(dstU, dstV, src, w, 16, 0);
+    planar_rgb16_to_uv(dstU, dstV, src, w, 16, 0, rgb2yuv);
 }
 
 static void planar_rgb16be_to_uv(uint8_t *dstU, uint8_t *dstV,
-                                 const uint8_t *src[4], int w)
+                                 const uint8_t *src[4], int w, int32_t *rgb2yuv)
 {
-    planar_rgb16_to_uv(dstU, dstV, src, w, 16, 1);
+    planar_rgb16_to_uv(dstU, dstV, src, w, 16, 1, rgb2yuv);
 }
 
 av_cold void ff_sws_init_input_funcs(SwsContext *c)
diff --git a/libswscale/output.c b/libswscale/output.c
index d9745fb..da760d2 100644
--- a/libswscale/output.c
+++ b/libswscale/output.c
@@ -1177,7 +1177,8 @@
                                   chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
                                   alpSrc, dest, dstW, y, fmt, hasAlpha); \
 }
-#define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
+
+#define YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
 YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
@@ -1186,8 +1187,10 @@
 { \
     name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
                                   dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
-} \
- \
+}
+
+#define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
+YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
                                 const int16_t *abuf0, uint8_t *dest, int dstW, \
@@ -1217,6 +1220,98 @@
 YUV2RGBWRAPPER(yuv2rgb,,   4,    AV_PIX_FMT_RGB4,      0)
 YUV2RGBWRAPPER(yuv2rgb,,   4b,   AV_PIX_FMT_RGB4_BYTE, 0)
 
+static av_always_inline void yuv2rgb_write_full(SwsContext *c,
+    uint8_t *dest, int i, int Y, int A, int U, int V,
+    int y, enum AVPixelFormat target, int hasAlpha, int err[4])
+{
+    int R, G, B;
+    int isrgb8 = target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8;
+
+    Y -= c->yuv2rgb_y_offset;
+    Y *= c->yuv2rgb_y_coeff;
+    Y += 1 << 21;
+    R = Y + V*c->yuv2rgb_v2r_coeff;
+    G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
+    B = Y +                          U*c->yuv2rgb_u2b_coeff;
+    if ((R | G | B) & 0xC0000000) {
+        R = av_clip_uintp2(R, 30);
+        G = av_clip_uintp2(G, 30);
+        B = av_clip_uintp2(B, 30);
+    }
+
+    switch(target) {
+    case AV_PIX_FMT_ARGB:
+        dest[0] = hasAlpha ? A : 255;
+        dest[1] = R >> 22;
+        dest[2] = G >> 22;
+        dest[3] = B >> 22;
+        break;
+    case AV_PIX_FMT_RGB24:
+        dest[0] = R >> 22;
+        dest[1] = G >> 22;
+        dest[2] = B >> 22;
+        break;
+    case AV_PIX_FMT_RGBA:
+        dest[0] = R >> 22;
+        dest[1] = G >> 22;
+        dest[2] = B >> 22;
+        dest[3] = hasAlpha ? A : 255;
+        break;
+    case AV_PIX_FMT_ABGR:
+        dest[0] = hasAlpha ? A : 255;
+        dest[1] = B >> 22;
+        dest[2] = G >> 22;
+        dest[3] = R >> 22;
+        break;
+    case AV_PIX_FMT_BGR24:
+        dest[0] = B >> 22;
+        dest[1] = G >> 22;
+        dest[2] = R >> 22;
+        break;
+    case AV_PIX_FMT_BGRA:
+        dest[0] = B >> 22;
+        dest[1] = G >> 22;
+        dest[2] = R >> 22;
+        dest[3] = hasAlpha ? A : 255;
+        break;
+    case AV_PIX_FMT_BGR4_BYTE:
+    case AV_PIX_FMT_RGB4_BYTE:
+    case AV_PIX_FMT_BGR8:
+    case AV_PIX_FMT_RGB8:
+    {
+        int r,g,b;
+        R >>= 22;
+        G >>= 22;
+        B >>= 22;
+        R += (7*err[0] + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2])>>4;
+        G += (7*err[1] + 1*c->dither_error[1][i] + 5*c->dither_error[1][i+1] + 3*c->dither_error[1][i+2])>>4;
+        B += (7*err[2] + 1*c->dither_error[2][i] + 5*c->dither_error[2][i+1] + 3*c->dither_error[2][i+2])>>4;
+        c->dither_error[0][i] = err[0];
+        c->dither_error[1][i] = err[1];
+        c->dither_error[2][i] = err[2];
+        r = R >> (isrgb8 ? 5 : 7);
+        g = G >> (isrgb8 ? 5 : 6);
+        b = B >> (isrgb8 ? 6 : 7);
+        r = av_clip(r, 0, isrgb8 ? 7 : 1);
+        g = av_clip(g, 0, isrgb8 ? 7 : 3);
+        b = av_clip(b, 0, isrgb8 ? 3 : 1);
+        err[0] = R - r*(isrgb8 ? 36 : 255);
+        err[1] = G - g*(isrgb8 ? 36 : 85);
+        err[2] = B - b*(isrgb8 ? 85 : 255);
+        if(target == AV_PIX_FMT_BGR4_BYTE) {
+            dest[0] = r + 2*g + 8*b;
+        } else if(target == AV_PIX_FMT_RGB4_BYTE) {
+            dest[0] = b + 2*g + 8*r;
+        } else if(target == AV_PIX_FMT_BGR8) {
+            dest[0] = r + 8*g + 64*b;
+        } else if(target == AV_PIX_FMT_RGB8) {
+            dest[0] = b + 4*g + 32*r;
+        } else
+            av_assert2(0);
+        break;}
+    }
+}
+
 static av_always_inline void
 yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
                           const int16_t **lumSrc, int lumFilterSize,
@@ -1228,14 +1323,17 @@
     int i;
     int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
     int err[4] = {0};
-    int isrgb8 = target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8;
+
+    if(   target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
+       || target == AV_PIX_FMT_BGR8      || target == AV_PIX_FMT_RGB8)
+        step = 1;
 
     for (i = 0; i < dstW; i++) {
         int j;
         int Y = 1<<9;
         int U = (1<<9)-(128 << 19);
         int V = (1<<9)-(128 << 19);
-        int R, G, B, A;
+        int A;
 
         for (j = 0; j < lumFilterSize; j++) {
             Y += lumSrc[j][i] * lumFilter[j];
@@ -1256,90 +1354,7 @@
             if (A & 0x100)
                 A = av_clip_uint8(A);
         }
-        Y -= c->yuv2rgb_y_offset;
-        Y *= c->yuv2rgb_y_coeff;
-        Y += 1 << 21;
-        R = Y + V*c->yuv2rgb_v2r_coeff;
-        G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
-        B = Y +                          U*c->yuv2rgb_u2b_coeff;
-        if ((R | G | B) & 0xC0000000) {
-            R = av_clip_uintp2(R, 30);
-            G = av_clip_uintp2(G, 30);
-            B = av_clip_uintp2(B, 30);
-        }
-
-        switch(target) {
-        case AV_PIX_FMT_ARGB:
-            dest[0] = hasAlpha ? A : 255;
-            dest[1] = R >> 22;
-            dest[2] = G >> 22;
-            dest[3] = B >> 22;
-            break;
-        case AV_PIX_FMT_RGB24:
-            dest[0] = R >> 22;
-            dest[1] = G >> 22;
-            dest[2] = B >> 22;
-            break;
-        case AV_PIX_FMT_RGBA:
-            dest[0] = R >> 22;
-            dest[1] = G >> 22;
-            dest[2] = B >> 22;
-            dest[3] = hasAlpha ? A : 255;
-            break;
-        case AV_PIX_FMT_ABGR:
-            dest[0] = hasAlpha ? A : 255;
-            dest[1] = B >> 22;
-            dest[2] = G >> 22;
-            dest[3] = R >> 22;
-            break;
-        case AV_PIX_FMT_BGR24:
-            dest[0] = B >> 22;
-            dest[1] = G >> 22;
-            dest[2] = R >> 22;
-            break;
-        case AV_PIX_FMT_BGRA:
-            dest[0] = B >> 22;
-            dest[1] = G >> 22;
-            dest[2] = R >> 22;
-            dest[3] = hasAlpha ? A : 255;
-            break;
-        case AV_PIX_FMT_BGR4_BYTE:
-        case AV_PIX_FMT_RGB4_BYTE:
-        case AV_PIX_FMT_BGR8:
-        case AV_PIX_FMT_RGB8:
-        {
-            int r,g,b;
-            R >>= 22;
-            G >>= 22;
-            B >>= 22;
-            R += (7*err[0] + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2])>>4;
-            G += (7*err[1] + 1*c->dither_error[1][i] + 5*c->dither_error[1][i+1] + 3*c->dither_error[1][i+2])>>4;
-            B += (7*err[2] + 1*c->dither_error[2][i] + 5*c->dither_error[2][i+1] + 3*c->dither_error[2][i+2])>>4;
-            c->dither_error[0][i] = err[0];
-            c->dither_error[1][i] = err[1];
-            c->dither_error[2][i] = err[2];
-            r = R >> (isrgb8 ? 5 : 7);
-            g = G >> (isrgb8 ? 5 : 6);
-            b = B >> (isrgb8 ? 6 : 7);
-            r = av_clip(r, 0, isrgb8 ? 7 : 1);
-            g = av_clip(g, 0, isrgb8 ? 7 : 3);
-            b = av_clip(b, 0, isrgb8 ? 3 : 1);
-            err[0] = R - r*(isrgb8 ? 36 : 255);
-            err[1] = G - g*(isrgb8 ? 36 : 85);
-            err[2] = B - b*(isrgb8 ? 85 : 255);
-            if(target == AV_PIX_FMT_BGR4_BYTE) {
-                dest[0] = r + 2*g + 8*b;
-            } else if(target == AV_PIX_FMT_RGB4_BYTE) {
-                dest[0] = b + 2*g + 8*r;
-            } else if(target == AV_PIX_FMT_BGR8) {
-                dest[0] = r + 8*g + 64*b;
-            } else if(target == AV_PIX_FMT_RGB8) {
-                dest[0] = b + 4*g + 32*r;
-            } else
-                av_assert2(0);
-            step = 1;
-            break;}
-        }
+        yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
         dest += step;
     }
     c->dither_error[0][i] = err[0];
@@ -1347,30 +1362,128 @@
     c->dither_error[2][i] = err[2];
 }
 
+static av_always_inline void
+yuv2rgb_full_2_c_template(SwsContext *c, const int16_t *buf[2],
+                     const int16_t *ubuf[2], const int16_t *vbuf[2],
+                     const int16_t *abuf[2], uint8_t *dest, int dstW,
+                     int yalpha, int uvalpha, int y,
+                     enum AVPixelFormat target, int hasAlpha)
+{
+    const int16_t *buf0  = buf[0],  *buf1  = buf[1],
+                  *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
+                  *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
+                  *abuf0 = hasAlpha ? abuf[0] : NULL,
+                  *abuf1 = hasAlpha ? abuf[1] : NULL;
+    int  yalpha1 = 4096 - yalpha;
+    int uvalpha1 = 4096 - uvalpha;
+    int i;
+    int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
+    int err[4] = {0};
+
+    if(   target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
+       || target == AV_PIX_FMT_BGR8      || target == AV_PIX_FMT_RGB8)
+        step = 1;
+
+    for (i = 0; i < dstW; i++) {
+        int Y = ( buf0[i] * yalpha1  +  buf1[i] * yalpha             ) >> 10; //FIXME rounding
+        int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha-(128 << 19)) >> 10;
+        int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha-(128 << 19)) >> 10;
+        int A;
+
+        if (hasAlpha) {
+            A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha + (1<<18)) >> 19;
+            if (A & 0x100)
+                A = av_clip_uint8(A);
+        }
+
+        yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
+        dest += step;
+    }
+    c->dither_error[0][i] = err[0];
+    c->dither_error[1][i] = err[1];
+    c->dither_error[2][i] = err[2];
+}
+
+static av_always_inline void
+yuv2rgb_full_1_c_template(SwsContext *c, const int16_t *buf0,
+                     const int16_t *ubuf[2], const int16_t *vbuf[2],
+                     const int16_t *abuf0, uint8_t *dest, int dstW,
+                     int uvalpha, int y, enum AVPixelFormat target,
+                     int hasAlpha)
+{
+    const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
+    int i;
+    int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
+    int err[4] = {0};
+
+    if(   target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
+       || target == AV_PIX_FMT_BGR8      || target == AV_PIX_FMT_RGB8)
+        step = 1;
+
+    if (uvalpha < 2048) {
+        for (i = 0; i < dstW; i++) {
+            int Y = buf0[i] << 2;
+            int U = (ubuf0[i] - (128<<7)) << 2;
+            int V = (vbuf0[i] - (128<<7)) << 2;
+            int A;
+
+            if (hasAlpha) {
+                A = (abuf0[i] + 64) >> 7;
+                if (A & 0x100)
+                    A = av_clip_uint8(A);
+            }
+
+            yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
+            dest += step;
+        }
+    } else {
+        const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
+        for (i = 0; i < dstW; i++) {
+            int Y = buf0[i] << 2;
+            int U = (ubuf0[i] + ubuf1[i] - (128<<8)) << 1;
+            int V = (vbuf0[i] + vbuf1[i] - (128<<8)) << 1;
+            int A;
+
+            if (hasAlpha) {
+                A = (abuf0[i] + 64) >> 7;
+                if (A & 0x100)
+                    A = av_clip_uint8(A);
+            }
+
+            yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
+            dest += step;
+        }
+    }
+
+    c->dither_error[0][i] = err[0];
+    c->dither_error[1][i] = err[1];
+    c->dither_error[2][i] = err[2];
+}
+
 #if CONFIG_SMALL
-YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
-YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
-YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
-YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
+YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
+YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
+YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
+YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
 #else
 #if CONFIG_SWSCALE_ALPHA
-YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA,  1)
-YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR,  1)
-YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA,  1)
-YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB,  1)
+YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA,  1)
+YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR,  1)
+YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA,  1)
+YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB,  1)
 #endif
-YUV2RGBWRAPPERX(yuv2, rgb_full, bgrx32_full, AV_PIX_FMT_BGRA,  0)
-YUV2RGBWRAPPERX(yuv2, rgb_full, xbgr32_full, AV_PIX_FMT_ABGR,  0)
-YUV2RGBWRAPPERX(yuv2, rgb_full, rgbx32_full, AV_PIX_FMT_RGBA,  0)
-YUV2RGBWRAPPERX(yuv2, rgb_full, xrgb32_full, AV_PIX_FMT_ARGB,  0)
+YUV2RGBWRAPPER(yuv2, rgb_full, bgrx32_full, AV_PIX_FMT_BGRA,  0)
+YUV2RGBWRAPPER(yuv2, rgb_full, xbgr32_full, AV_PIX_FMT_ABGR,  0)
+YUV2RGBWRAPPER(yuv2, rgb_full, rgbx32_full, AV_PIX_FMT_RGBA,  0)
+YUV2RGBWRAPPER(yuv2, rgb_full, xrgb32_full, AV_PIX_FMT_ARGB,  0)
 #endif
-YUV2RGBWRAPPERX(yuv2, rgb_full, bgr24_full,  AV_PIX_FMT_BGR24, 0)
-YUV2RGBWRAPPERX(yuv2, rgb_full, rgb24_full,  AV_PIX_FMT_RGB24, 0)
+YUV2RGBWRAPPER(yuv2, rgb_full, bgr24_full,  AV_PIX_FMT_BGR24, 0)
+YUV2RGBWRAPPER(yuv2, rgb_full, rgb24_full,  AV_PIX_FMT_RGB24, 0)
 
-YUV2RGBWRAPPERX(yuv2, rgb_full, bgr4_byte_full,  AV_PIX_FMT_BGR4_BYTE, 0)
-YUV2RGBWRAPPERX(yuv2, rgb_full, rgb4_byte_full,  AV_PIX_FMT_RGB4_BYTE, 0)
-YUV2RGBWRAPPERX(yuv2, rgb_full, bgr8_full,   AV_PIX_FMT_BGR8,  0)
-YUV2RGBWRAPPERX(yuv2, rgb_full, rgb8_full,   AV_PIX_FMT_RGB8,  0)
+YUV2RGBWRAPPER(yuv2, rgb_full, bgr4_byte_full,  AV_PIX_FMT_BGR4_BYTE, 0)
+YUV2RGBWRAPPER(yuv2, rgb_full, rgb4_byte_full,  AV_PIX_FMT_RGB4_BYTE, 0)
+YUV2RGBWRAPPER(yuv2, rgb_full, bgr8_full,   AV_PIX_FMT_BGR8,  0)
+YUV2RGBWRAPPER(yuv2, rgb_full, rgb8_full,   AV_PIX_FMT_RGB8,  0)
 
 static void
 yuv2gbrp_full_X_c(SwsContext *c, const int16_t *lumFilter,
@@ -1491,76 +1604,112 @@
             case AV_PIX_FMT_RGBA:
 #if CONFIG_SMALL
                 *yuv2packedX = yuv2rgba32_full_X_c;
+                *yuv2packed2 = yuv2rgba32_full_2_c;
+                *yuv2packed1 = yuv2rgba32_full_1_c;
 #else
 #if CONFIG_SWSCALE_ALPHA
                 if (c->alpPixBuf) {
                     *yuv2packedX = yuv2rgba32_full_X_c;
+                    *yuv2packed2 = yuv2rgba32_full_2_c;
+                    *yuv2packed1 = yuv2rgba32_full_1_c;
                 } else
 #endif /* CONFIG_SWSCALE_ALPHA */
                 {
                     *yuv2packedX = yuv2rgbx32_full_X_c;
+                    *yuv2packed2 = yuv2rgbx32_full_2_c;
+                    *yuv2packed1 = yuv2rgbx32_full_1_c;
                 }
 #endif /* !CONFIG_SMALL */
                 break;
             case AV_PIX_FMT_ARGB:
 #if CONFIG_SMALL
                 *yuv2packedX = yuv2argb32_full_X_c;
+                *yuv2packed2 = yuv2argb32_full_2_c;
+                *yuv2packed1 = yuv2argb32_full_1_c;
 #else
 #if CONFIG_SWSCALE_ALPHA
                 if (c->alpPixBuf) {
                     *yuv2packedX = yuv2argb32_full_X_c;
+                    *yuv2packed2 = yuv2argb32_full_2_c;
+                    *yuv2packed1 = yuv2argb32_full_1_c;
                 } else
 #endif /* CONFIG_SWSCALE_ALPHA */
                 {
                     *yuv2packedX = yuv2xrgb32_full_X_c;
+                    *yuv2packed2 = yuv2xrgb32_full_2_c;
+                    *yuv2packed1 = yuv2xrgb32_full_1_c;
                 }
 #endif /* !CONFIG_SMALL */
                 break;
             case AV_PIX_FMT_BGRA:
 #if CONFIG_SMALL
                 *yuv2packedX = yuv2bgra32_full_X_c;
+                *yuv2packed2 = yuv2bgra32_full_2_c;
+                *yuv2packed1 = yuv2bgra32_full_1_c;
 #else
 #if CONFIG_SWSCALE_ALPHA
                 if (c->alpPixBuf) {
                     *yuv2packedX = yuv2bgra32_full_X_c;
+                    *yuv2packed2 = yuv2bgra32_full_2_c;
+                    *yuv2packed1 = yuv2bgra32_full_1_c;
                 } else
 #endif /* CONFIG_SWSCALE_ALPHA */
                 {
                     *yuv2packedX = yuv2bgrx32_full_X_c;
+                    *yuv2packed2 = yuv2bgrx32_full_2_c;
+                    *yuv2packed1 = yuv2bgrx32_full_1_c;
                 }
 #endif /* !CONFIG_SMALL */
                 break;
             case AV_PIX_FMT_ABGR:
 #if CONFIG_SMALL
                 *yuv2packedX = yuv2abgr32_full_X_c;
+                *yuv2packed2 = yuv2abgr32_full_2_c;
+                *yuv2packed1 = yuv2abgr32_full_1_c;
 #else
 #if CONFIG_SWSCALE_ALPHA
                 if (c->alpPixBuf) {
                     *yuv2packedX = yuv2abgr32_full_X_c;
+                    *yuv2packed2 = yuv2abgr32_full_2_c;
+                    *yuv2packed1 = yuv2abgr32_full_1_c;
                 } else
 #endif /* CONFIG_SWSCALE_ALPHA */
                 {
                     *yuv2packedX = yuv2xbgr32_full_X_c;
+                    *yuv2packed2 = yuv2xbgr32_full_2_c;
+                    *yuv2packed1 = yuv2xbgr32_full_1_c;
                 }
 #endif /* !CONFIG_SMALL */
                 break;
             case AV_PIX_FMT_RGB24:
             *yuv2packedX = yuv2rgb24_full_X_c;
+            *yuv2packed2 = yuv2rgb24_full_2_c;
+            *yuv2packed1 = yuv2rgb24_full_1_c;
             break;
         case AV_PIX_FMT_BGR24:
             *yuv2packedX = yuv2bgr24_full_X_c;
+            *yuv2packed2 = yuv2bgr24_full_2_c;
+            *yuv2packed1 = yuv2bgr24_full_1_c;
             break;
         case AV_PIX_FMT_BGR4_BYTE:
             *yuv2packedX = yuv2bgr4_byte_full_X_c;
+            *yuv2packed2 = yuv2bgr4_byte_full_2_c;
+            *yuv2packed1 = yuv2bgr4_byte_full_1_c;
             break;
         case AV_PIX_FMT_RGB4_BYTE:
             *yuv2packedX = yuv2rgb4_byte_full_X_c;
+            *yuv2packed2 = yuv2rgb4_byte_full_2_c;
+            *yuv2packed1 = yuv2rgb4_byte_full_1_c;
             break;
         case AV_PIX_FMT_BGR8:
             *yuv2packedX = yuv2bgr8_full_X_c;
+            *yuv2packed2 = yuv2bgr8_full_2_c;
+            *yuv2packed1 = yuv2bgr8_full_1_c;
             break;
         case AV_PIX_FMT_RGB8:
             *yuv2packedX = yuv2rgb8_full_X_c;
+            *yuv2packed2 = yuv2rgb8_full_2_c;
+            *yuv2packed1 = yuv2rgb8_full_1_c;
             break;
         case AV_PIX_FMT_GBRP:
         case AV_PIX_FMT_GBRP9BE:
diff --git a/libswscale/rgb2rgb.c b/libswscale/rgb2rgb.c
index 1233a1d..c8c430d 100644
--- a/libswscale/rgb2rgb.c
+++ b/libswscale/rgb2rgb.c
@@ -73,10 +73,11 @@
                    uint8_t *udst, uint8_t *vdst,
                    int width, int height,
                    int lumStride, int chromStride, int srcStride);
-void (*rgb24toyv12)(const uint8_t *src, uint8_t *ydst,
-                    uint8_t *udst, uint8_t *vdst,
-                    int width, int height,
-                    int lumStride, int chromStride, int srcStride);
+void (*ff_rgb24toyv12)(const uint8_t *src, uint8_t *ydst,
+                       uint8_t *udst, uint8_t *vdst,
+                       int width, int height,
+                       int lumStride, int chromStride, int srcStride,
+                       int32_t *rgb2yuv);
 void (*planar2x)(const uint8_t *src, uint8_t *dst, int width, int height,
                  int srcStride, int dstStride);
 void (*interleaveBytes)(const uint8_t *src1, const uint8_t *src2, uint8_t *dst,
@@ -105,7 +106,6 @@
                      const uint8_t *src, int width, int height,
                      int lumStride, int chromStride, int srcStride);
 
-#define RGB2YUV_SHIFT 8
 #define BY ((int)( 0.098 * (1 << RGB2YUV_SHIFT) + 0.5))
 #define BV ((int)(-0.071 * (1 << RGB2YUV_SHIFT) + 0.5))
 #define BU ((int)( 0.439 * (1 << RGB2YUV_SHIFT) + 0.5))
diff --git a/libswscale/rgb2rgb.h b/libswscale/rgb2rgb.h
index e37f0fb..c6d514f 100644
--- a/libswscale/rgb2rgb.h
+++ b/libswscale/rgb2rgb.h
@@ -76,9 +76,9 @@
 void shuffle_bytes_3012(const uint8_t *src, uint8_t *dst, int src_size);
 void shuffle_bytes_3210(const uint8_t *src, uint8_t *dst, int src_size);
 
-void rgb24toyv12_c(const uint8_t *src, uint8_t *ydst, uint8_t *udst,
-                   uint8_t *vdst, int width, int height, int lumStride,
-                   int chromStride, int srcStride);
+void ff_rgb24toyv12_c(const uint8_t *src, uint8_t *ydst, uint8_t *udst,
+                      uint8_t *vdst, int width, int height, int lumStride,
+                      int chromStride, int srcStride, int32_t *rgb2yuv);
 
 /**
  * Height should be a multiple of 2 and width should be a multiple of 16.
@@ -124,9 +124,10 @@
  * Chrominance data is only taken from every second line, others are ignored.
  * FIXME: Write high quality version.
  */
-extern void (*rgb24toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
-                           int width, int height,
-                           int lumStride, int chromStride, int srcStride);
+extern void (*ff_rgb24toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
+                              int width, int height,
+                              int lumStride, int chromStride, int srcStride,
+                              int32_t *rgb2yuv);
 extern void (*planar2x)(const uint8_t *src, uint8_t *dst, int width, int height,
                         int srcStride, int dstStride);
 
diff --git a/libswscale/rgb2rgb_template.c b/libswscale/rgb2rgb_template.c
index 8753594..76229d8 100644
--- a/libswscale/rgb2rgb_template.c
+++ b/libswscale/rgb2rgb_template.c
@@ -612,10 +612,13 @@
  * others are ignored in the C version.
  * FIXME: Write HQ version.
  */
-void rgb24toyv12_c(const uint8_t *src, uint8_t *ydst, uint8_t *udst,
+void ff_rgb24toyv12_c(const uint8_t *src, uint8_t *ydst, uint8_t *udst,
                    uint8_t *vdst, int width, int height, int lumStride,
-                   int chromStride, int srcStride)
+                   int chromStride, int srcStride, int32_t *rgb2yuv)
 {
+    int32_t ry = rgb2yuv[RY_IDX], gy = rgb2yuv[GY_IDX], by = rgb2yuv[BY_IDX];
+    int32_t ru = rgb2yuv[RU_IDX], gu = rgb2yuv[GU_IDX], bu = rgb2yuv[BU_IDX];
+    int32_t rv = rgb2yuv[RV_IDX], gv = rgb2yuv[GV_IDX], bv = rgb2yuv[BV_IDX];
     int y;
     const int chromWidth = width >> 1;
 
@@ -626,9 +629,9 @@
             unsigned int g = src[6 * i + 1];
             unsigned int r = src[6 * i + 2];
 
-            unsigned int Y = ((RY * r + GY * g + BY * b) >> RGB2YUV_SHIFT) +  16;
-            unsigned int V = ((RV * r + GV * g + BV * b) >> RGB2YUV_SHIFT) + 128;
-            unsigned int U = ((RU * r + GU * g + BU * b) >> RGB2YUV_SHIFT) + 128;
+            unsigned int Y = ((ry * r + gy * g + by * b) >> RGB2YUV_SHIFT) +  16;
+            unsigned int V = ((rv * r + gv * g + bv * b) >> RGB2YUV_SHIFT) + 128;
+            unsigned int U = ((ru * r + gu * g + bu * b) >> RGB2YUV_SHIFT) + 128;
 
             udst[i]     = U;
             vdst[i]     = V;
@@ -638,7 +641,7 @@
             g = src[6 * i + 4];
             r = src[6 * i + 5];
 
-            Y = ((RY * r + GY * g + BY * b) >> RGB2YUV_SHIFT) + 16;
+            Y = ((ry * r + gy * g + by * b) >> RGB2YUV_SHIFT) + 16;
             ydst[2 * i + 1] = Y;
         }
         ydst += lumStride;
@@ -652,7 +655,7 @@
             unsigned int g = src[6 * i + 1];
             unsigned int r = src[6 * i + 2];
 
-            unsigned int Y = ((RY * r + GY * g + BY * b) >> RGB2YUV_SHIFT) + 16;
+            unsigned int Y = ((ry * r + gy * g + by * b) >> RGB2YUV_SHIFT) + 16;
 
             ydst[2 * i] = Y;
 
@@ -660,7 +663,7 @@
             g = src[6 * i + 4];
             r = src[6 * i + 5];
 
-            Y = ((RY * r + GY * g + BY * b) >> RGB2YUV_SHIFT) + 16;
+            Y = ((ry * r + gy * g + by * b) >> RGB2YUV_SHIFT) + 16;
             ydst[2 * i + 1] = Y;
         }
         udst += chromStride;
@@ -915,7 +918,7 @@
     yuv422ptouyvy      = yuv422ptouyvy_c;
     yuy2toyv12         = yuy2toyv12_c;
     planar2x           = planar2x_c;
-    rgb24toyv12        = rgb24toyv12_c;
+    ff_rgb24toyv12     = ff_rgb24toyv12_c;
     interleaveBytes    = interleaveBytes_c;
     vu9_to_vu12        = vu9_to_vu12_c;
     yvu9_to_yuy2       = yvu9_to_yuy2_c;
diff --git a/libswscale/swscale-test.c b/libswscale/swscale-test.c
index aece61e..e0b887e 100644
--- a/libswscale/swscale-test.c
+++ b/libswscale/swscale-test.c
@@ -305,7 +305,7 @@
 
         ret = sscanf(buf,
                      " %12s %dx%d -> %12s %dx%d flags=%d CRC=%x"
-                     " SSD=%"PRId64 ", %"PRId64 ", %"PRId64 ", %"PRId64 "\n",
+                     " SSD=%"SCNd64 ", %"SCNd64 ", %"SCNd64 ", %"SCNd64 "\n",
                      srcStr, &srcW, &srcH, dstStr, &dstW, &dstH,
                      &flags, &r.crc, &r.ssdY, &r.ssdU, &r.ssdV, &r.ssdA);
         if (ret != 12) {
diff --git a/libswscale/swscale.c b/libswscale/swscale.c
index bb90819..0c20a71 100644
--- a/libswscale/swscale.c
+++ b/libswscale/swscale.c
@@ -254,7 +254,7 @@
         toYV12(formatConvBuffer, src, src_in[1], src_in[2], srcW, pal);
         src = formatConvBuffer;
     } else if (c->readLumPlanar && !isAlpha) {
-        c->readLumPlanar(formatConvBuffer, src_in, srcW);
+        c->readLumPlanar(formatConvBuffer, src_in, srcW, c->input_rgb2yuv_table);
         src = formatConvBuffer;
     }
 
@@ -307,7 +307,7 @@
     } else if (c->readChrPlanar) {
         uint8_t *buf2 = formatConvBuffer +
                         FFALIGN(srcW*2+78, 16);
-        c->readChrPlanar(formatConvBuffer, buf2, src_in, srcW);
+        c->readChrPlanar(formatConvBuffer, buf2, src_in, srcW, c->input_rgb2yuv_table);
         src1 = formatConvBuffer;
         src2 = buf2;
     }
@@ -383,6 +383,10 @@
     int lastInLumBuf = c->lastInLumBuf;
     int lastInChrBuf = c->lastInChrBuf;
 
+    if (!usePal(c->srcFormat)) {
+        pal = c->input_rgb2yuv_table;
+    }
+
     if (isPacked(c->srcFormat)) {
         src[0] =
         src[1] =
diff --git a/libswscale/swscale_internal.h b/libswscale/swscale_internal.h
index 83d3a00..be5a80e 100644
--- a/libswscale/swscale_internal.h
+++ b/libswscale/swscale_internal.h
@@ -39,8 +39,6 @@
 
 #define YUVRGB_TABLE_HEADROOM 128
 
-#define FAST_BGR2YV12 // use 7-bit instead of 15-bit coefficients
-
 #define MAX_FILTER_SIZE 256
 
 #define DITHER1XBPP
@@ -360,6 +358,17 @@
     uint8_t *table_gU[256 + 2*YUVRGB_TABLE_HEADROOM];
     int table_gV[256 + 2*YUVRGB_TABLE_HEADROOM];
     uint8_t *table_bU[256 + 2*YUVRGB_TABLE_HEADROOM];
+    DECLARE_ALIGNED(16, int32_t, input_rgb2yuv_table)[16+40*4]; // This table can contain both C and SIMD formatted values, teh C vales are always at the XY_IDX points
+#define RY_IDX 0
+#define GY_IDX 1
+#define BY_IDX 2
+#define RU_IDX 3
+#define GU_IDX 4
+#define BU_IDX 5
+#define RV_IDX 6
+#define GV_IDX 7
+#define BV_IDX 8
+#define RGB2YUV_SHIFT 15
 
     int *dither_error[4];
 
@@ -489,9 +498,9 @@
      * internally to Y/UV.
      */
     /** @{ */
-    void (*readLumPlanar)(uint8_t *dst, const uint8_t *src[4], int width);
+    void (*readLumPlanar)(uint8_t *dst, const uint8_t *src[4], int width, int32_t *rgb2yuv);
     void (*readChrPlanar)(uint8_t *dstU, uint8_t *dstV, const uint8_t *src[4],
-                          int width);
+                          int width, int32_t *rgb2yuv);
     /** @} */
 
     /**
diff --git a/libswscale/swscale_unscaled.c b/libswscale/swscale_unscaled.c
index 8da73b2..863d9ee 100644
--- a/libswscale/swscale_unscaled.c
+++ b/libswscale/swscale_unscaled.c
@@ -532,23 +532,23 @@
                           dst[1] + srcSliceY * dstStride[1] };
 
     switch (c->srcFormat) {
-    case PIX_FMT_RGB24:
+    case AV_PIX_FMT_RGB24:
         packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst201,
                        stride201, srcSliceH, alpha_first, 3, c->srcW);
         break;
-    case PIX_FMT_BGR24:
+    case AV_PIX_FMT_BGR24:
         packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst102,
                        stride102, srcSliceH, alpha_first, 3, c->srcW);
         break;
-    case PIX_FMT_ARGB:
+    case AV_PIX_FMT_ARGB:
         alpha_first = 1;
-    case PIX_FMT_RGBA:
+    case AV_PIX_FMT_RGBA:
         packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst201,
                        stride201, srcSliceH, alpha_first, 4, c->srcW);
         break;
-    case PIX_FMT_ABGR:
+    case AV_PIX_FMT_ABGR:
         alpha_first = 1;
-    case PIX_FMT_BGRA:
+    case AV_PIX_FMT_BGRA:
         packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst102,
                        stride102, srcSliceH, alpha_first, 4, c->srcW);
         break;
@@ -746,13 +746,14 @@
                               int srcStride[], int srcSliceY, int srcSliceH,
                               uint8_t *dst[], int dstStride[])
 {
-    rgb24toyv12(
+    ff_rgb24toyv12(
         src[0],
         dst[0] +  srcSliceY       * dstStride[0],
         dst[1] + (srcSliceY >> 1) * dstStride[1],
         dst[2] + (srcSliceY >> 1) * dstStride[2],
         c->srcW, srcSliceH,
-        dstStride[0], dstStride[1], srcStride[0]);
+        dstStride[0], dstStride[1], srcStride[0],
+        c->input_rgb2yuv_table);
     if (dst[3])
         fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
     return srcSliceH;
diff --git a/libswscale/utils.c b/libswscale/utils.c
index 932cf94..ca7c2c4 100644
--- a/libswscale/utils.c
+++ b/libswscale/utils.c
@@ -799,6 +799,100 @@
     *v = desc->log2_chroma_h;
 }
 
+static void fill_rgb2yuv_table(SwsContext *c, const int table[4], int dstRange)
+{
+    int64_t W, V, Z, Cy, Cu, Cv;
+    int64_t vr =  table[0];
+    int64_t ub =  table[1];
+    int64_t ug = -table[2];
+    int64_t vg = -table[3];
+    int64_t ONE = 65536;
+    int64_t cy = ONE;
+    uint8_t *p = (uint8_t*)c->input_rgb2yuv_table;
+    int i;
+    static const int8_t map[] = {
+    BY_IDX, GY_IDX, -1    , BY_IDX, BY_IDX, GY_IDX, -1    , BY_IDX,
+    RY_IDX, -1    , GY_IDX, RY_IDX, RY_IDX, -1    , GY_IDX, RY_IDX,
+    RY_IDX, GY_IDX, -1    , RY_IDX, RY_IDX, GY_IDX, -1    , RY_IDX,
+    BY_IDX, -1    , GY_IDX, BY_IDX, BY_IDX, -1    , GY_IDX, BY_IDX,
+    BU_IDX, GU_IDX, -1    , BU_IDX, BU_IDX, GU_IDX, -1    , BU_IDX,
+    RU_IDX, -1    , GU_IDX, RU_IDX, RU_IDX, -1    , GU_IDX, RU_IDX,
+    RU_IDX, GU_IDX, -1    , RU_IDX, RU_IDX, GU_IDX, -1    , RU_IDX,
+    BU_IDX, -1    , GU_IDX, BU_IDX, BU_IDX, -1    , GU_IDX, BU_IDX,
+    BV_IDX, GV_IDX, -1    , BV_IDX, BV_IDX, GV_IDX, -1    , BV_IDX,
+    RV_IDX, -1    , GV_IDX, RV_IDX, RV_IDX, -1    , GV_IDX, RV_IDX,
+    RV_IDX, GV_IDX, -1    , RV_IDX, RV_IDX, GV_IDX, -1    , RV_IDX,
+    BV_IDX, -1    , GV_IDX, BV_IDX, BV_IDX, -1    , GV_IDX, BV_IDX,
+    RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX,
+    BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX, BY_IDX, RY_IDX,
+    GY_IDX, -1    , GY_IDX, -1    , GY_IDX, -1    , GY_IDX, -1    ,
+    -1    , GY_IDX, -1    , GY_IDX, -1    , GY_IDX, -1    , GY_IDX,
+    RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX,
+    BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX, BU_IDX, RU_IDX,
+    GU_IDX, -1    , GU_IDX, -1    , GU_IDX, -1    , GU_IDX, -1    ,
+    -1    , GU_IDX, -1    , GU_IDX, -1    , GU_IDX, -1    , GU_IDX,
+    RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX,
+    BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX, BV_IDX, RV_IDX,
+    GV_IDX, -1    , GV_IDX, -1    , GV_IDX, -1    , GV_IDX, -1    ,
+    -1    , GV_IDX, -1    , GV_IDX, -1    , GV_IDX, -1    , GV_IDX, //23
+    -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //24
+    -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //25
+    -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //26
+    -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //27
+    -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //28
+    -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //29
+    -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //30
+    -1    , -1    , -1    , -1    , -1    , -1    , -1    , -1    , //31
+    BY_IDX, GY_IDX, RY_IDX, -1    , -1    , -1    , -1    , -1    , //32
+    BU_IDX, GU_IDX, RU_IDX, -1    , -1    , -1    , -1    , -1    , //33
+    BV_IDX, GV_IDX, RV_IDX, -1    , -1    , -1    , -1    , -1    , //34
+    };
+
+    dstRange = 0; //FIXME range = 1 is handled elsewhere
+
+    if (!dstRange) {
+        cy = cy * 255 / 219;
+    } else {
+        vr = vr * 224 / 255;
+        ub = ub * 224 / 255;
+        ug = ug * 224 / 255;
+        vg = vg * 224 / 255;
+    }
+    W = ROUNDED_DIV(ONE*ONE*ug, ub);
+    V = ROUNDED_DIV(ONE*ONE*vg, vr);
+    Z = ONE*ONE-W-V;
+
+    Cy = ROUNDED_DIV(cy*Z, ONE);
+    Cu = ROUNDED_DIV(ub*Z, ONE);
+    Cv = ROUNDED_DIV(vr*Z, ONE);
+
+    c->input_rgb2yuv_table[RY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V        , Cy);
+    c->input_rgb2yuv_table[GY_IDX] =  ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE  , Cy);
+    c->input_rgb2yuv_table[BY_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W        , Cy);
+
+    c->input_rgb2yuv_table[RU_IDX] =  ROUNDED_DIV((1 << RGB2YUV_SHIFT)*V        , Cu);
+    c->input_rgb2yuv_table[GU_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE  , Cu);
+    c->input_rgb2yuv_table[BU_IDX] =  ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(Z+W)    , Cu);
+
+    c->input_rgb2yuv_table[RV_IDX] =  ROUNDED_DIV((1 << RGB2YUV_SHIFT)*(V+Z)    , Cv);
+    c->input_rgb2yuv_table[GV_IDX] = -ROUNDED_DIV((1 << RGB2YUV_SHIFT)*ONE*ONE  , Cv);
+    c->input_rgb2yuv_table[BV_IDX] =  ROUNDED_DIV((1 << RGB2YUV_SHIFT)*W        , Cv);
+
+    if(/*!dstRange && */table == ff_yuv2rgb_coeffs[SWS_CS_DEFAULT]) {
+        c->input_rgb2yuv_table[BY_IDX] =  ((int)(0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
+        c->input_rgb2yuv_table[BV_IDX] = (-(int)(0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
+        c->input_rgb2yuv_table[BU_IDX] =  ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
+        c->input_rgb2yuv_table[GY_IDX] =  ((int)(0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
+        c->input_rgb2yuv_table[GV_IDX] = (-(int)(0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
+        c->input_rgb2yuv_table[GU_IDX] = (-(int)(0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
+        c->input_rgb2yuv_table[RY_IDX] =  ((int)(0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
+        c->input_rgb2yuv_table[RV_IDX] =  ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
+        c->input_rgb2yuv_table[RU_IDX] = (-(int)(0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
+    }
+    for(i=0; i<FF_ARRAY_ELEMS(map); i++)
+        AV_WL16(p + 16*4 + 2*i, map[i] >= 0 ? c->input_rgb2yuv_table[map[i]] : 0);
+}
+
 int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
                              int srcRange, const int table[4], int dstRange,
                              int brightness, int contrast, int saturation)
@@ -819,12 +913,13 @@
     c->srcRange   = srcRange;
     c->dstRange   = dstRange;
 
-    if (isYUV(c->dstFormat) || isGray(c->dstFormat))
+    if ((isYUV(c->dstFormat) || isGray(c->dstFormat)) && (isYUV(c->srcFormat) || isGray(c->srcFormat)))
         return -1;
 
     c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
     c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
 
+    if (!isYUV(c->dstFormat) && !isGray(c->dstFormat)) {
     ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness,
                              contrast, saturation);
     // FIXME factorize
@@ -832,6 +927,10 @@
     if (HAVE_ALTIVEC && av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC)
         ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness,
                                        contrast, saturation);
+    }
+
+    fill_rgb2yuv_table(c, table, dstRange);
+
     return 0;
 }
 
diff --git a/libswscale/x86/input.asm b/libswscale/x86/input.asm
index 9d5a871..48d533e8 100644
--- a/libswscale/x86/input.asm
+++ b/libswscale/x86/input.asm
@@ -37,31 +37,57 @@
 
 rgb_Yrnd:        times 4 dd 0x80100        ;  16.5 << 15
 rgb_UVrnd:       times 4 dd 0x400100       ; 128.5 << 15
-bgr_Ycoeff_12x4: times 2 dw BY, GY, 0, BY
-bgr_Ycoeff_3x56: times 2 dw RY, 0, GY, RY
-rgb_Ycoeff_12x4: times 2 dw RY, GY, 0, RY
-rgb_Ycoeff_3x56: times 2 dw BY, 0, GY, BY
-bgr_Ucoeff_12x4: times 2 dw BU, GU, 0, BU
-bgr_Ucoeff_3x56: times 2 dw RU, 0, GU, RU
-rgb_Ucoeff_12x4: times 2 dw RU, GU, 0, RU
-rgb_Ucoeff_3x56: times 2 dw BU, 0, GU, BU
-bgr_Vcoeff_12x4: times 2 dw BV, GV, 0, BV
-bgr_Vcoeff_3x56: times 2 dw RV, 0, GV, RV
-rgb_Vcoeff_12x4: times 2 dw RV, GV, 0, RV
-rgb_Vcoeff_3x56: times 2 dw BV, 0, GV, BV
+%define bgr_Ycoeff_12x4 16*4 + 16* 0 + tableq
+%define bgr_Ycoeff_3x56 16*4 + 16* 1 + tableq
+%define rgb_Ycoeff_12x4 16*4 + 16* 2 + tableq
+%define rgb_Ycoeff_3x56 16*4 + 16* 3 + tableq
+%define bgr_Ucoeff_12x4 16*4 + 16* 4 + tableq
+%define bgr_Ucoeff_3x56 16*4 + 16* 5 + tableq
+%define rgb_Ucoeff_12x4 16*4 + 16* 6 + tableq
+%define rgb_Ucoeff_3x56 16*4 + 16* 7 + tableq
+%define bgr_Vcoeff_12x4 16*4 + 16* 8 + tableq
+%define bgr_Vcoeff_3x56 16*4 + 16* 9 + tableq
+%define rgb_Vcoeff_12x4 16*4 + 16*10 + tableq
+%define rgb_Vcoeff_3x56 16*4 + 16*11 + tableq
 
-rgba_Ycoeff_rb:  times 4 dw RY, BY
-rgba_Ycoeff_br:  times 4 dw BY, RY
-rgba_Ycoeff_ga:  times 4 dw GY, 0
-rgba_Ycoeff_ag:  times 4 dw 0,  GY
-rgba_Ucoeff_rb:  times 4 dw RU, BU
-rgba_Ucoeff_br:  times 4 dw BU, RU
-rgba_Ucoeff_ga:  times 4 dw GU, 0
-rgba_Ucoeff_ag:  times 4 dw 0,  GU
-rgba_Vcoeff_rb:  times 4 dw RV, BV
-rgba_Vcoeff_br:  times 4 dw BV, RV
-rgba_Vcoeff_ga:  times 4 dw GV, 0
-rgba_Vcoeff_ag:  times 4 dw 0,  GV
+%define rgba_Ycoeff_rb 16*4 + 16*12 + tableq
+%define rgba_Ycoeff_br 16*4 + 16*13 + tableq
+%define rgba_Ycoeff_ga 16*4 + 16*14 + tableq
+%define rgba_Ycoeff_ag 16*4 + 16*15 + tableq
+%define rgba_Ucoeff_rb 16*4 + 16*16 + tableq
+%define rgba_Ucoeff_br 16*4 + 16*17 + tableq
+%define rgba_Ucoeff_ga 16*4 + 16*18 + tableq
+%define rgba_Ucoeff_ag 16*4 + 16*19 + tableq
+%define rgba_Vcoeff_rb 16*4 + 16*20 + tableq
+%define rgba_Vcoeff_br 16*4 + 16*21 + tableq
+%define rgba_Vcoeff_ga 16*4 + 16*22 + tableq
+%define rgba_Vcoeff_ag 16*4 + 16*23 + tableq
+
+; bgr_Ycoeff_12x4: times 2 dw BY, GY, 0, BY
+; bgr_Ycoeff_3x56: times 2 dw RY, 0, GY, RY
+; rgb_Ycoeff_12x4: times 2 dw RY, GY, 0, RY
+; rgb_Ycoeff_3x56: times 2 dw BY, 0, GY, BY
+; bgr_Ucoeff_12x4: times 2 dw BU, GU, 0, BU
+; bgr_Ucoeff_3x56: times 2 dw RU, 0, GU, RU
+; rgb_Ucoeff_12x4: times 2 dw RU, GU, 0, RU
+; rgb_Ucoeff_3x56: times 2 dw BU, 0, GU, BU
+; bgr_Vcoeff_12x4: times 2 dw BV, GV, 0, BV
+; bgr_Vcoeff_3x56: times 2 dw RV, 0, GV, RV
+; rgb_Vcoeff_12x4: times 2 dw RV, GV, 0, RV
+; rgb_Vcoeff_3x56: times 2 dw BV, 0, GV, BV
+
+; rgba_Ycoeff_rb:  times 4 dw RY, BY
+; rgba_Ycoeff_br:  times 4 dw BY, RY
+; rgba_Ycoeff_ga:  times 4 dw GY, 0
+; rgba_Ycoeff_ag:  times 4 dw 0,  GY
+; rgba_Ucoeff_rb:  times 4 dw RU, BU
+; rgba_Ucoeff_br:  times 4 dw BU, RU
+; rgba_Ucoeff_ga:  times 4 dw GU, 0
+; rgba_Ucoeff_ag:  times 4 dw 0,  GU
+; rgba_Vcoeff_rb:  times 4 dw RV, BV
+; rgba_Vcoeff_br:  times 4 dw BV, RV
+; rgba_Vcoeff_ga:  times 4 dw GV, 0
+; rgba_Vcoeff_ag:  times 4 dw 0,  GV
 
 shuf_rgb_12x4:   db 0, 0x80, 1, 0x80,  2, 0x80,  3, 0x80, \
                     6, 0x80, 7, 0x80,  8, 0x80,  9, 0x80
@@ -82,7 +108,7 @@
 ; %1 = nr. of XMM registers
 ; %2 = rgb or bgr
 %macro RGB24_TO_Y_FN 2-3
-cglobal %2 %+ 24ToY, 6, 6, %1, dst, src, u1, u2, w, u3
+cglobal %2 %+ 24ToY, 6, 6, %1, dst, src, u1, u2, w, table
 %if mmsize == 8
     mova           m5, [%2_Ycoeff_12x4]
     mova           m6, [%2_Ycoeff_3x56]
@@ -171,7 +197,7 @@
 ; %1 = nr. of XMM registers
 ; %2 = rgb or bgr
 %macro RGB24_TO_UV_FN 2-3
-cglobal %2 %+ 24ToUV, 7, 7, %1, dstU, dstV, u1, src, u2, w, u3
+cglobal %2 %+ 24ToUV, 7, 7, %1, dstU, dstV, u1, src, u2, w, table
 %if ARCH_X86_64
     mova           m8, [%2_Ucoeff_12x4]
     mova           m9, [%2_Ucoeff_3x56]
@@ -311,7 +337,7 @@
 ; %1 = nr. of XMM registers
 ; %2-5 = rgba, bgra, argb or abgr (in individual characters)
 %macro RGB32_TO_Y_FN 5-6
-cglobal %2%3%4%5 %+ ToY, 6, 6, %1, dst, src, u1, u2, w, u3
+cglobal %2%3%4%5 %+ ToY, 6, 6, %1, dst, src, u1, u2, w, table
     mova           m5, [rgba_Ycoeff_%2%4]
     mova           m6, [rgba_Ycoeff_%3%5]
 %if %0 == 6
@@ -354,7 +380,7 @@
 ; %1 = nr. of XMM registers
 ; %2-5 = rgba, bgra, argb or abgr (in individual characters)
 %macro RGB32_TO_UV_FN 5-6
-cglobal %2%3%4%5 %+ ToUV, 7, 7, %1, dstU, dstV, u1, src, u2, w, u3
+cglobal %2%3%4%5 %+ ToUV, 7, 7, %1, dstU, dstV, u1, src, u2, w, table
 %if ARCH_X86_64
     mova           m8, [rgba_Ucoeff_%2%4]
     mova           m9, [rgba_Ucoeff_%3%5]
diff --git a/libswscale/x86/rgb2rgb.c b/libswscale/x86/rgb2rgb.c
index 1e20176..986afc4 100644
--- a/libswscale/x86/rgb2rgb.c
+++ b/libswscale/x86/rgb2rgb.c
@@ -76,7 +76,6 @@
 DECLARE_ASM_CONST(8, uint64_t, mul15_hi)     = 0x0210021002100210ULL;
 DECLARE_ASM_CONST(8, uint64_t, mul16_mid)    = 0x2080208020802080ULL;
 
-#define RGB2YUV_SHIFT 8
 #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
 #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
 #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
diff --git a/libswscale/x86/rgb2rgb_template.c b/libswscale/x86/rgb2rgb_template.c
index d802ab4..fd5b4b9 100644
--- a/libswscale/x86/rgb2rgb_template.c
+++ b/libswscale/x86/rgb2rgb_template.c
@@ -1610,10 +1610,15 @@
  * others are ignored in the C version.
  * FIXME: Write HQ version.
  */
+#if HAVE_7REGS
 static inline void RENAME(rgb24toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
                                        int width, int height,
-                                       int lumStride, int chromStride, int srcStride)
+                                       int lumStride, int chromStride, int srcStride,
+                                       int32_t *rgb2yuv)
 {
+#define BGR2Y_IDX "16*4+16*32"
+#define BGR2U_IDX "16*4+16*33"
+#define BGR2V_IDX "16*4+16*34"
     int y;
     const x86_reg chromWidth= width>>1;
     for (y=0; y<height-2; y+=2) {
@@ -1621,7 +1626,7 @@
         for (i=0; i<2; i++) {
             __asm__ volatile(
                 "mov                        %2, %%"REG_a"   \n\t"
-                "movq  "MANGLE(ff_bgr2YCoeff)", %%mm6       \n\t"
+                "movq          "BGR2Y_IDX"(%3), %%mm6       \n\t"
                 "movq       "MANGLE(ff_w1111)", %%mm5       \n\t"
                 "pxor                    %%mm7, %%mm7       \n\t"
                 "lea (%%"REG_a", %%"REG_a", 2), %%"REG_d"   \n\t"
@@ -1640,12 +1645,10 @@
                 "pmaddwd                 %%mm6, %%mm1       \n\t"
                 "pmaddwd                 %%mm6, %%mm2       \n\t"
                 "pmaddwd                 %%mm6, %%mm3       \n\t"
-#ifndef FAST_BGR2YV12
                 "psrad                      $8, %%mm0       \n\t"
                 "psrad                      $8, %%mm1       \n\t"
                 "psrad                      $8, %%mm2       \n\t"
                 "psrad                      $8, %%mm3       \n\t"
-#endif
                 "packssdw                %%mm1, %%mm0       \n\t"
                 "packssdw                %%mm3, %%mm2       \n\t"
                 "pmaddwd                 %%mm5, %%mm0       \n\t"
@@ -1665,12 +1668,10 @@
                 "pmaddwd                 %%mm6, %%mm1       \n\t"
                 "pmaddwd                 %%mm6, %%mm2       \n\t"
                 "pmaddwd                 %%mm6, %%mm3       \n\t"
-#ifndef FAST_BGR2YV12
                 "psrad                      $8, %%mm4       \n\t"
                 "psrad                      $8, %%mm1       \n\t"
                 "psrad                      $8, %%mm2       \n\t"
                 "psrad                      $8, %%mm3       \n\t"
-#endif
                 "packssdw                %%mm1, %%mm4       \n\t"
                 "packssdw                %%mm3, %%mm2       \n\t"
                 "pmaddwd                 %%mm5, %%mm4       \n\t"
@@ -1685,7 +1686,7 @@
                 MOVNTQ"                  %%mm0, (%1, %%"REG_a") \n\t"
                 "add                        $8,      %%"REG_a"  \n\t"
                 " js                        1b                  \n\t"
-                : : "r" (src+width*3), "r" (ydst+width), "g" ((x86_reg)-width)
+                : : "r" (src+width*3), "r" (ydst+width), "g" ((x86_reg)-width), "r"(rgb2yuv)
                 : "%"REG_a, "%"REG_d
             );
             ydst += lumStride;
@@ -1695,7 +1696,7 @@
         __asm__ volatile(
             "mov                        %4, %%"REG_a"   \n\t"
             "movq       "MANGLE(ff_w1111)", %%mm5       \n\t"
-            "movq  "MANGLE(ff_bgr2UCoeff)", %%mm6       \n\t"
+            "movq          "BGR2U_IDX"(%5), %%mm6       \n\t"
             "pxor                    %%mm7, %%mm7       \n\t"
             "lea (%%"REG_a", %%"REG_a", 2), %%"REG_d"   \n\t"
             "add                 %%"REG_d", %%"REG_d"   \n\t"
@@ -1744,19 +1745,17 @@
             "psrlw                      $2, %%mm0       \n\t"
             "psrlw                      $2, %%mm2       \n\t"
 #endif
-            "movq  "MANGLE(ff_bgr2VCoeff)", %%mm1       \n\t"
-            "movq  "MANGLE(ff_bgr2VCoeff)", %%mm3       \n\t"
+            "movq          "BGR2V_IDX"(%5), %%mm1       \n\t"
+            "movq          "BGR2V_IDX"(%5), %%mm3       \n\t"
 
             "pmaddwd                 %%mm0, %%mm1       \n\t"
             "pmaddwd                 %%mm2, %%mm3       \n\t"
             "pmaddwd                 %%mm6, %%mm0       \n\t"
             "pmaddwd                 %%mm6, %%mm2       \n\t"
-#ifndef FAST_BGR2YV12
             "psrad                      $8, %%mm0       \n\t"
             "psrad                      $8, %%mm1       \n\t"
             "psrad                      $8, %%mm2       \n\t"
             "psrad                      $8, %%mm3       \n\t"
-#endif
             "packssdw                %%mm2, %%mm0       \n\t"
             "packssdw                %%mm3, %%mm1       \n\t"
             "pmaddwd                 %%mm5, %%mm0       \n\t"
@@ -1806,19 +1805,17 @@
             "psrlw                      $2, %%mm4       \n\t"
             "psrlw                      $2, %%mm2       \n\t"
 #endif
-            "movq  "MANGLE(ff_bgr2VCoeff)", %%mm1       \n\t"
-            "movq  "MANGLE(ff_bgr2VCoeff)", %%mm3       \n\t"
+            "movq          "BGR2V_IDX"(%5), %%mm1       \n\t"
+            "movq          "BGR2V_IDX"(%5), %%mm3       \n\t"
 
             "pmaddwd                 %%mm4, %%mm1       \n\t"
             "pmaddwd                 %%mm2, %%mm3       \n\t"
             "pmaddwd                 %%mm6, %%mm4       \n\t"
             "pmaddwd                 %%mm6, %%mm2       \n\t"
-#ifndef FAST_BGR2YV12
             "psrad                      $8, %%mm4       \n\t"
             "psrad                      $8, %%mm1       \n\t"
             "psrad                      $8, %%mm2       \n\t"
             "psrad                      $8, %%mm3       \n\t"
-#endif
             "packssdw                %%mm2, %%mm4       \n\t"
             "packssdw                %%mm3, %%mm1       \n\t"
             "pmaddwd                 %%mm5, %%mm4       \n\t"
@@ -1837,7 +1834,7 @@
             "movd                    %%mm0, (%3, %%"REG_a") \n\t"
             "add                        $4, %%"REG_a"       \n\t"
             " js                        1b                  \n\t"
-            : : "r" (src+chromWidth*6), "r" (src+srcStride+chromWidth*6), "r" (udst+chromWidth), "r" (vdst+chromWidth), "g" (-chromWidth)
+            : : "r" (src+chromWidth*6), "r" (src+srcStride+chromWidth*6), "r" (udst+chromWidth), "r" (vdst+chromWidth), "g" (-chromWidth), "r"(rgb2yuv)
             : "%"REG_a, "%"REG_d
         );
 
@@ -1850,8 +1847,9 @@
                      SFENCE"     \n\t"
                      :::"memory");
 
-     rgb24toyv12_c(src, ydst, udst, vdst, width, height-y, lumStride, chromStride, srcStride);
+     ff_rgb24toyv12_c(src, ydst, udst, vdst, width, height-y, lumStride, chromStride, srcStride, rgb2yuv);
 }
+#endif /* HAVE_7REGS */
 #endif /* !COMPILE_TEMPLATE_SSE2 */
 
 #if !COMPILE_TEMPLATE_AMD3DNOW
@@ -2486,7 +2484,9 @@
 #if COMPILE_TEMPLATE_MMXEXT || COMPILE_TEMPLATE_AMD3DNOW
     planar2x           = RENAME(planar2x);
 #endif /* COMPILE_TEMPLATE_MMXEXT || COMPILE_TEMPLATE_AMD3DNOW */
-    rgb24toyv12        = RENAME(rgb24toyv12);
+#if HAVE_7REGS
+    ff_rgb24toyv12     = RENAME(rgb24toyv12);
+#endif /* HAVE_7REGS */
 
     yuyvtoyuv420       = RENAME(yuyvtoyuv420);
     uyvytoyuv420       = RENAME(uyvytoyuv420);
diff --git a/libswscale/x86/swscale.c b/libswscale/x86/swscale.c
index 2f67b1b..961b622 100644
--- a/libswscale/x86/swscale.c
+++ b/libswscale/x86/swscale.c
@@ -58,15 +58,10 @@
 DECLARE_ALIGNED(8, const uint64_t, ff_M24B)         = 0xFF0000FF0000FF00LL;
 DECLARE_ALIGNED(8, const uint64_t, ff_M24C)         = 0x0000FF0000FF0000LL;
 
-#ifdef FAST_BGR2YV12
-DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff)   = 0x000000210041000DULL;
-DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff)   = 0x0000FFEEFFDC0038ULL;
-DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff)   = 0x00000038FFD2FFF8ULL;
-#else
 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff)   = 0x000020E540830C8BULL;
 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff)   = 0x0000ED0FDAC23831ULL;
 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff)   = 0x00003831D0E6F6EAULL;
-#endif /* FAST_BGR2YV12 */
+
 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset)  = 0x1010101010101010ULL;
 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
 DECLARE_ALIGNED(8, const uint64_t, ff_w1111)        = 0x0001000100010001ULL;
@@ -279,7 +274,7 @@
 #endif /* HAVE_INLINE_ASM */
 
 #define SCALE_FUNC(filter_n, from_bpc, to_bpc, opt) \
-extern void ff_hscale ## from_bpc ## to ## to_bpc ## _ ## filter_n ## _ ## opt( \
+void ff_hscale ## from_bpc ## to ## to_bpc ## _ ## filter_n ## _ ## opt( \
                                                 SwsContext *c, int16_t *data, \
                                                 int dstW, const uint8_t *src, \
                                                 const int16_t *filter, \
@@ -318,9 +313,9 @@
 SCALE_FUNCS_SSE(sse4);
 
 #define VSCALEX_FUNC(size, opt) \
-extern void ff_yuv2planeX_ ## size ## _ ## opt(const int16_t *filter, int filterSize, \
-                                               const int16_t **src, uint8_t *dest, int dstW, \
-                                               const uint8_t *dither, int offset)
+void ff_yuv2planeX_ ## size ## _ ## opt(const int16_t *filter, int filterSize, \
+                                        const int16_t **src, uint8_t *dest, int dstW, \
+                                        const uint8_t *dither, int offset)
 #define VSCALEX_FUNCS(opt) \
     VSCALEX_FUNC(8,  opt); \
     VSCALEX_FUNC(9,  opt); \
@@ -335,8 +330,8 @@
 VSCALEX_FUNCS(avx);
 
 #define VSCALE_FUNC(size, opt) \
-extern void ff_yuv2plane1_ ## size ## _ ## opt(const int16_t *src, uint8_t *dst, int dstW, \
-                                               const uint8_t *dither, int offset)
+void ff_yuv2plane1_ ## size ## _ ## opt(const int16_t *src, uint8_t *dst, int dstW, \
+                                        const uint8_t *dither, int offset)
 #define VSCALE_FUNCS(opt1, opt2) \
     VSCALE_FUNC(8,  opt1); \
     VSCALE_FUNC(9,  opt2); \
@@ -351,15 +346,15 @@
 VSCALE_FUNCS(avx, avx);
 
 #define INPUT_Y_FUNC(fmt, opt) \
-extern void ff_ ## fmt ## ToY_  ## opt(uint8_t *dst, const uint8_t *src, \
-                                       const uint8_t *unused1, const uint8_t *unused2, \
-                                       int w, uint32_t *unused)
+void ff_ ## fmt ## ToY_  ## opt(uint8_t *dst, const uint8_t *src, \
+                                const uint8_t *unused1, const uint8_t *unused2, \
+                                int w, uint32_t *unused)
 #define INPUT_UV_FUNC(fmt, opt) \
-extern void ff_ ## fmt ## ToUV_ ## opt(uint8_t *dstU, uint8_t *dstV, \
-                                       const uint8_t *unused0, \
-                                       const uint8_t *src1, \
-                                       const uint8_t *src2, \
-                                       int w, uint32_t *unused)
+void ff_ ## fmt ## ToUV_ ## opt(uint8_t *dstU, uint8_t *dstV, \
+                                const uint8_t *unused0, \
+                                const uint8_t *src1, \
+                                const uint8_t *src2, \
+                                int w, uint32_t *unused)
 #define INPUT_FUNC(fmt, opt) \
     INPUT_Y_FUNC(fmt, opt); \
     INPUT_UV_FUNC(fmt, opt)
diff --git a/tests/Makefile b/tests/Makefile
index 73ee2d9..b267293 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -15,10 +15,10 @@
 
 OBJDIRS += tests/data tests/vsynth1
 
-tests/vsynth1/00.pgm: tests/videogen$(HOSTEXESUF) | tests/vsynth1
+$(VREF): tests/videogen$(HOSTEXESUF) | tests/vsynth1
 	$(M)./$< 'tests/vsynth1/'
 
-tests/data/asynth1.sw: tests/audiogen$(HOSTEXESUF) | tests/data
+$(AREF): tests/audiogen$(HOSTEXESUF) | tests/data
 	$(M)./$< $@
 
 tests/data/asynth-%.wav: tests/audiogen$(HOSTEXESUF) | tests/data
@@ -32,7 +32,7 @@
 
 tests/data/ffprobe-test.nut: ffmpeg$(EXESUF) | tests/data
 	$(M)$(TARGET_EXEC) ./$< \
-        -f lavfi -i "aevalsrc=sin(400*PI*2*t)::d=0.125[out0]; testsrc=d=0.125[out1]; testsrc=s=100x100:d=0.125[out2]" \
+        -f lavfi -i "aevalsrc=sin(400*PI*2*t):d=0.125[out0]; testsrc=d=0.125[out1]; testsrc=s=100x100:d=0.125[out2]" \
         -f ffmetadata -i $(SRC_PATH)/tests/test.ffmeta \
         -flags +bitexact -map 0:0 -map 0:1 -map 0:2 -map_metadata 1 \
         -map_metadata:s:0 1:s:0 -map_metadata:s:1 1:s:1 \
@@ -59,6 +59,10 @@
 DEMDEC  = $(call ALLYES, $(1)_DEMUXER $(2:%=%_DECODER))
 ENCMUX  = $(call ALLYES, $(1:%=%_ENCODER) $(2)_MUXER)
 
+FILTERDEMDEC       = $(call ALLYES, $(1)_FILTER $(2)_DEMUXER $(3)_DECODER)
+FILTERDEMDECMUX    = $(call ALLYES, $(1)_FILTER $(2)_DEMUXER $(3)_DECODER $(4)_MUXER)
+FILTERDEMDECENCMUX = $(call ALLYES, $(1)_FILTER $(2)_DEMUXER $(3)_DECODER $(4)_ENCODER $(5)_MUXER)
+
 include $(SRC_PATH)/tests/fate/acodec.mak
 include $(SRC_PATH)/tests/fate/vcodec.mak
 include $(SRC_PATH)/tests/fate/avformat.mak
@@ -83,7 +87,8 @@
 include $(SRC_PATH)/tests/fate/ea.mak
 include $(SRC_PATH)/tests/fate/ffmpeg.mak
 include $(SRC_PATH)/tests/fate/ffprobe.mak
-include $(SRC_PATH)/tests/fate/filter.mak
+include $(SRC_PATH)/tests/fate/filter-audio.mak
+include $(SRC_PATH)/tests/fate/filter-video.mak
 include $(SRC_PATH)/tests/fate/flac.mak
 include $(SRC_PATH)/tests/fate/fft.mak
 include $(SRC_PATH)/tests/fate/gif.mak
@@ -147,7 +152,7 @@
 	@echo "$@ requires external samples and SAMPLES not specified"; false
 endif
 
-FATE_UTILS = base64 tiny_psnr
+FATE_UTILS = base64 tiny_psnr tiny_ssim
 
 TOOL = ffmpeg
 
@@ -155,11 +160,25 @@
 
 $(FATE) $(FATE_TESTS-no): $(FATE_UTILS:%=tests/%$(HOSTEXESUF))
 	@echo "TEST    $(@:fate-%=%)"
-	$(Q)$(SRC_PATH)/tests/fate-run.sh $@ "$(SAMPLES)" "$(TARGET_EXEC)" "$(TARGET_PATH)" '$(CMD)' '$(CMP)' '$(REF)' '$(FUZZ)' '$(THREADS)' '$(THREAD_TYPE)' '$(CPUFLAGS)' '$(CMP_SHIFT)' '$(CMP_TARGET)' '$(SIZE_TOLERANCE)' '$(CMP_UNIT)'
+	$(Q)$(SRC_PATH)/tests/fate-run.sh $@ "$(SAMPLES)" "$(TARGET_EXEC)" "$(TARGET_PATH)" '$(CMD)' '$(CMP)' '$(REF)' '$(FUZZ)' '$(THREADS)' '$(THREAD_TYPE)' '$(CPUFLAGS)' '$(CMP_SHIFT)' '$(CMP_TARGET)' '$(SIZE_TOLERANCE)' '$(CMP_UNIT)' '$(GEN)'
 
 fate-list:
 	@printf '%s\n' $(sort $(FATE))
 
+coverage.info: TAG = LCOV
+coverage.info:
+	$(M)lcov -d $(CURDIR) -b $(SRC_PATH) --capture | sed -e 's#/./#/#g' > $@
+	$(M)lcov --remove $@ "/usr*" -o $@
+
+lcov:  TAG = GENHTML
+lcov: coverage.info
+	$(M)genhtml -o $(CURDIR)/lcov $<
+
+lcov-reset: TAG = LCOV
+lcov-reset:
+	$(M)lcov -d $(CURDIR) --zerocounters
+	$(Q)$(RM) -f coverage.info
+
 clean:: testclean
 
 testclean:
@@ -169,4 +188,5 @@
 
 -include $(wildcard tests/*.d)
 
-.PHONY: fate*
+.PHONY: fate* lcov lcov-reset
+.INTERMEDIATE: coverage.info
diff --git a/tests/fate-run.sh b/tests/fate-run.sh
index 0520d62..ed99387 100755
--- a/tests/fate-run.sh
+++ b/tests/fate-run.sh
@@ -22,6 +22,7 @@
 cmp_target=${13:-0}
 size_tolerance=${14:-0}
 cmp_unit=${15:-2}
+gen=${16:-no}
 
 outdir="tests/data/fate"
 outfile="${outdir}/${test}"
@@ -197,9 +198,17 @@
 
 echo "${test}:${sig:-$err}:$($base64 <$cmpfile):$($base64 <$errfile)" >$repfile
 
+if test $err != 0 && test $gen != "no" ; then
+    echo "GEN     $ref"
+    cp -f "$outfile" "$ref"
+    err=$?
+fi
+
 if test $err = 0; then
     rm -f $outfile $errfile $cmpfile $cleanfiles
-else
+elif test $gen = "no"; then
     echo "Test $test failed. Look at $errfile for details."
+else
+    echo "Updating reference failed, possibly no output file was generated."
 fi
 exit $err
diff --git a/tests/fate/aac.mak b/tests/fate/aac.mak
index c5c47e6..0b3999d 100644
--- a/tests/fate/aac.mak
+++ b/tests/fate/aac.mak
@@ -98,7 +98,9 @@
 $(FATE_AAC_ALL): CMP  = oneoff
 $(FATE_AAC_ALL): FUZZ = 2
 
-FATE_SAMPLES_FFMPEG += $(FATE_AAC_ALL) $(FATE_AAC_ENCODE)
+FATE_AAC_ENCODE-$(call ENCMUX, AAC, ADTS) += $(FATE_AAC_ENCODE)
+
+FATE_SAMPLES_FFMPEG += $(FATE_AAC_ALL) $(FATE_AAC_ENCODE-yes)
 
 fate-aac: $(FATE_AAC_ALL) $(FATE_AAC_ENCODE)
 fate-aac-latm: $(FATE_AAC_LATM-yes)
diff --git a/tests/fate/audio.mak b/tests/fate/audio.mak
index 3e5e313..65bb146 100644
--- a/tests/fate/audio.mak
+++ b/tests/fate/audio.mak
@@ -34,7 +34,7 @@
 fate-nellymoser: CMP = oneoff
 fate-nellymoser: REF = $(SAMPLES)/nellymoser/nellymoser.pcm
 
-FATE_SAMPLES_AUDIO += fate-nellymoser-aref-encode
+FATE_SAMPLES_AUDIO-$(call ENCMUX, NELLYMOSER, FLV) += fate-nellymoser-aref-encode
 fate-nellymoser-aref-encode: $(AREF) ./tests/data/asynth-16000-1.wav
 fate-nellymoser-aref-encode: CMD = enc_dec_pcm flv wav s16le $(REF) -c:a nellymoser
 fate-nellymoser-aref-encode: CMP = stddev
diff --git a/tests/fate/avfilter.mak b/tests/fate/avfilter.mak
index 15c8595..d68757d 100644
--- a/tests/fate/avfilter.mak
+++ b/tests/fate/avfilter.mak
@@ -12,9 +12,10 @@
              fate-lavfi-field                                           \
              fate-lavfi-idet                                            \
              fate-lavfi-il                                              \
-             fate-lavfi-life                                            \
              fate-lavfi-null                                            \
-             fate-lavfi-overlay                                         \
+             fate-lavfi-overlay_rgb                                     \
+             fate-lavfi-overlay_yuv420                                  \
+             fate-lavfi-overlay_yuv444                                  \
              fate-lavfi-pad                                             \
              fate-lavfi-pixfmts_copy                                    \
              fate-lavfi-pixfmts_crop                                    \
@@ -26,11 +27,9 @@
              fate-lavfi-pixfmts_vflip                                   \
              fate-lavfi-scale200                                        \
              fate-lavfi-scale500                                        \
-             fate-lavfi-scalenorm                                       \
              fate-lavfi-select                                          \
              fate-lavfi-setdar                                          \
              fate-lavfi-setsar                                          \
-             fate-lavfi-testsrc                                         \
              fate-lavfi-thumbnail                                       \
              fate-lavfi-tile                                            \
              fate-lavfi-transpose                                       \
@@ -39,6 +38,10 @@
              fate-lavfi-vflip_crop                                      \
              fate-lavfi-vflip_vflip                                     \
 
+FATE_LAVFI-$(CONFIG_AVDEVICE) += fate-lavfi-life                        \
+                                 fate-lavfi-scalenorm                   \
+                                 fate-lavfi-testsrc                     \
+
 FATE_LAVFI-$(CONFIG_GPL) += fate-lavfi-colormatrix1                     \
                             fate-lavfi-colormatrix2                     \
                             fate-lavfi-hue                              \
diff --git a/tests/fate/avformat.mak b/tests/fate/avformat.mak
index 86fb071..5f9c8c1 100644
--- a/tests/fate/avformat.mak
+++ b/tests/fate/avformat.mak
@@ -52,7 +52,8 @@
 FATE_LAVF-$(CONFIG_YUV4MPEGPIPE_MUXER)                               += yuv4mpeg
 
 FATE_LAVF += $(FATE_LAVF-yes:%=fate-lavf-%)
-FATE_LAVF += fate-lavf-pixfmt
+FATE_LAVF_PIXFMT-$(CONFIG_SCALE_FILTER) += fate-lavf-pixfmt
+FATE_LAVF += $(FATE_LAVF_PIXFMT-yes)
 
 $(FATE_LAVF): $(AREF) $(VREF)
 $(FATE_LAVF): CMD = lavftest
diff --git a/tests/fate/cover-art.mak b/tests/fate/cover-art.mak
index 56a384e..b175c3e 100644
--- a/tests/fate/cover-art.mak
+++ b/tests/fate/cover-art.mak
@@ -1,31 +1,33 @@
-FATE_COVER_ART += fate-cover-art-ape
+FATE_COVER_ART-$(CONFIG_APE_DEMUXER) += fate-cover-art-ape
 fate-cover-art-ape: CMD = md5 -i $(SAMPLES)/cover_art/luckynight_cover.ape -an -c:v copy -f rawvideo
 fate-cover-art-ape: REF = 45333c983c45af54449dff10af144317
 
-FATE_COVER_ART += fate-cover-art-flac
+FATE_COVER_ART-$(CONFIG_FLAC_DEMUXER) += fate-cover-art-flac
 fate-cover-art-flac: CMD = md5 -i $(SAMPLES)/cover_art/cover_art.flac -an -c:v copy -f rawvideo
 fate-cover-art-flac: REF = 0de1fc6200596fa32b8f7300a14c0261
 
-FATE_COVER_ART += fate-cover-art-m4a
+FATE_COVER_ART-$(CONFIG_MOV_DEMUXER) += fate-cover-art-m4a
 fate-cover-art-m4a: CMD = md5 -i $(SAMPLES)/cover_art/Owner-iTunes_9.0.3.15.m4a -an -c:v copy -f rawvideo
 fate-cover-art-m4a: REF = 08ba70a3b594ff6345a93965e96a9d3e
 
-FATE_COVER_ART += fate-cover-art-wma
+FATE_COVER_ART-$(CONFIG_ASF_DEMUXER) += fate-cover-art-wma
 fate-cover-art-wma: CMD = md5 -i $(SAMPLES)/cover_art/Californication_cover.wma -an -c:v copy -f rawvideo
 fate-cover-art-wma: REF = 0808bd0e1b61542a16e1906812dd924b
 
-FATE_COVER_ART += fate-cover-art-wma-id3
+FATE_COVER_ART-$(CONFIG_ASF_DEMUXER) += fate-cover-art-wma-id3
 fate-cover-art-wma-id3: CMD = md5 -i $(SAMPLES)/cover_art/wma_with_ID3_APIC_trimmed.wma -an -c:v copy -f rawvideo
 fate-cover-art-wma-id3: REF = e6a8dd03687d5178bc13fc7d3316696e
 
-FATE_COVER_ART += fate-cover-art-wma-metadatalib
+FATE_COVER_ART-$(CONFIG_ASF_DEMUXER) += fate-cover-art-wma-metadatalib
 fate-cover-art-wma-metadatalib: CMD = md5 -i $(SAMPLES)/cover_art/wma_with_metadata_library_object_tag_trimmed.wma -map 0:v -c:v copy -f rawvideo
 fate-cover-art-wma-metadatalib: REF = 32e8bd4fad546f63d881a0256f083aea
 
-FATE_COVER_ART += fate-cover-art-wv
+FATE_COVER_ART-$(CONFIG_WV_DEMUXER) += fate-cover-art-wv
 fate-cover-art-wv: CMD = md5 -i $(SAMPLES)/cover_art/luckynight_cover.wv -an -c:v copy -f rawvideo
 fate-cover-art-wv: REF = 45333c983c45af54449dff10af144317
 
+FATE_COVER_ART = $(FATE_COVER_ART-yes)
+
 $(FATE_COVER_ART): CMP = oneline
 FATE_SAMPLES_AVCONV += $(FATE_COVER_ART)
 fate-cover-art: $(FATE_COVER_ART)
diff --git a/tests/fate/demux.mak b/tests/fate/demux.mak
index 189ef27..3b5f954 100644
--- a/tests/fate/demux.mak
+++ b/tests/fate/demux.mak
@@ -1,4 +1,4 @@
-FATE_SAMPLES_DEMUX += fate-avio-direct
+FATE_SAMPLES_DEMUX-$(call DEMDEC, AVI, FRAPS) += fate-avio-direct
 fate-avio-direct: CMD = framecrc -avioflags direct -i $(SAMPLES)/fraps/fraps-v5-bouncing-balls-partial.avi -avioflags direct
 
 FATE_SAMPLES_DEMUX-$(CONFIG_AAC_DEMUXER) += fate-adts-demux
diff --git a/tests/fate/ffmpeg.mak b/tests/fate/ffmpeg.mak
index b98ccd8..21cdd5f 100644
--- a/tests/fate/ffmpeg.mak
+++ b/tests/fate/ffmpeg.mak
@@ -1,19 +1,27 @@
-FATE_MAPCHAN += fate-mapchan-6ch-extract-2
+FATE_MAPCHAN-$(CONFIG_CHANNELMAP_FILTER) += fate-mapchan-6ch-extract-2
 fate-mapchan-6ch-extract-2: tests/data/asynth-22050-6.wav
 fate-mapchan-6ch-extract-2: CMD = ffmpeg -i $(TARGET_PATH)/tests/data/asynth-22050-6.wav -map_channel 0.0.0 -flags +bitexact -f wav md5: -map_channel 0.0.1 -flags +bitexact -f wav md5:
 
-FATE_MAPCHAN += fate-mapchan-6ch-extract-2-downmix-mono
+FATE_MAPCHAN-$(CONFIG_CHANNELMAP_FILTER) += fate-mapchan-6ch-extract-2-downmix-mono
 fate-mapchan-6ch-extract-2-downmix-mono: tests/data/asynth-22050-6.wav
 fate-mapchan-6ch-extract-2-downmix-mono: CMD = md5 -i $(TARGET_PATH)/tests/data/asynth-22050-6.wav -map_channel 0.0.1 -map_channel 0.0.0 -ac 1 -flags +bitexact -f wav
 
-FATE_MAPCHAN += fate-mapchan-silent-mono
+FATE_MAPCHAN-$(CONFIG_CHANNELMAP_FILTER) += fate-mapchan-silent-mono
 fate-mapchan-silent-mono: tests/data/asynth-22050-1.wav
 fate-mapchan-silent-mono: CMD = md5 -i $(TARGET_PATH)/tests/data/asynth-22050-1.wav -map_channel -1 -map_channel 0.0.0 -flags +bitexact -f wav
 
+FATE_MAPCHAN = $(FATE_MAPCHAN-yes)
+
 FATE_FFMPEG += $(FATE_MAPCHAN)
 fate-mapchan: $(FATE_MAPCHAN)
 
-FATE_FFMPEG += fate-force_key_frames
+FATE_FFMPEG-$(CONFIG_COLOR_FILTER) += fate-ffmpeg-filter_complex
+fate-ffmpeg-filter_complex: CMD = framecrc -filter_complex color=d=1:r=5
+
+FATE_FFMPEG-$(CONFIG_COLOR_FILTER) += fate-ffmpeg-lavfi
+fate-ffmpeg-lavfi: CMD = framecrc -lavfi color=d=1:r=5
+
+FATE_FFMPEG-$(CONFIG_RAWVIDEO_DEMUXER) += fate-force_key_frames
 fate-force_key_frames: tests/data/vsynth2.yuv
 fate-force_key_frames: CMD = enc_dec \
   "rawvideo -s 352x288 -pix_fmt yuv420p" tests/data/vsynth2.yuv \
@@ -27,3 +35,14 @@
   -ss 132 -i $(SAMPLES)/sub/vobsub.idx \
   -filter_complex "sws_flags=+accurate_rnd+bitexact;[0:0]scale=720:480[v];[v][1:0]overlay[v2]" \
   -map "[v2]" -c:v rawvideo -map 1:s -c:s dvdsub
+
+FATE_FFMPEG-$(call ALLYES, PCM_S16LE_DEMUXER PCM_S16LE_MUXER PCM_S16LE_DECODER PCM_S16LE_ENCODER) += fate-unknown_layout-pcm
+fate-unknown_layout-pcm: $(AREF)
+fate-unknown_layout-pcm: CMD = md5 \
+  -guess_layout_max 0 -f s16le -ac 1 -ar 44100 -i $(AREF) -f s16le
+
+FATE_FFMPEG-$(call ALLYES, PCM_S16LE_DEMUXER AC3_MUXER PCM_S16LE_DECODER AC3_FIXED_ENCODER) += fate-unknown_layout-ac3
+fate-unknown_layout-ac3: $(AREF)
+fate-unknown_layout-ac3: CMD = md5 \
+  -guess_layout_max 0 -f s16le -ac 1 -ar 44100 -i $(AREF) \
+  -f ac3 -flags +bitexact -c ac3_fixed
diff --git a/tests/fate/filter-audio.mak b/tests/fate/filter-audio.mak
new file mode 100644
index 0000000..6eaa4bf
--- /dev/null
+++ b/tests/fate/filter-audio.mak
@@ -0,0 +1,64 @@
+FATE_AMIX += fate-filter-amix-simple
+fate-filter-amix-simple: CMD = ffmpeg -filter_complex amix -i $(SRC) -ss 3 -i $(SRC1) -f f32le -
+fate-filter-amix-simple: REF = $(SAMPLES)/filter/amix_simple.pcm
+
+FATE_AMIX += fate-filter-amix-first
+fate-filter-amix-first: CMD = ffmpeg -filter_complex amix=duration=first -ss 4 -i $(SRC) -i $(SRC1) -f f32le -
+fate-filter-amix-first: REF = $(SAMPLES)/filter/amix_first.pcm
+
+FATE_AMIX += fate-filter-amix-transition
+fate-filter-amix-transition: tests/data/asynth-44100-2-3.wav
+fate-filter-amix-transition: SRC2 = $(TARGET_PATH)/tests/data/asynth-44100-2-3.wav
+fate-filter-amix-transition: CMD = ffmpeg -filter_complex amix=inputs=3:dropout_transition=0.5 -i $(SRC) -ss 2 -i $(SRC1) -ss 4 -i $(SRC2) -f f32le -
+fate-filter-amix-transition: REF = $(SAMPLES)/filter/amix_transition.pcm
+
+FATE_AFILTER-$(call FILTERDEMDECENCMUX, AMIX, WAV, PCM_S16LE, PCM_F32LE, PCM_F32LE) += $(FATE_AMIX)
+$(FATE_AMIX): tests/data/asynth-44100-2.wav tests/data/asynth-44100-2-2.wav
+$(FATE_AMIX): SRC  = $(TARGET_PATH)/tests/data/asynth-44100-2.wav
+$(FATE_AMIX): SRC1 = $(TARGET_PATH)/tests/data/asynth-44100-2-2.wav
+$(FATE_AMIX): CMP  = oneoff
+$(FATE_AMIX): CMP_UNIT = f32
+
+FATE_AFILTER-$(call FILTERDEMDECMUX, ASYNCTS, FLV, NELLYMOSER, PCM_S16LE) += fate-filter-asyncts
+fate-filter-asyncts: SRC = $(SAMPLES)/nellymoser/nellymoser-discont.flv
+fate-filter-asyncts: CMD = pcm -analyzeduration 10000000 -i $(SRC) -af asyncts
+fate-filter-asyncts: CMP = oneoff
+fate-filter-asyncts: REF = $(SAMPLES)/nellymoser/nellymoser-discont-async-v2.pcm
+
+FATE_FILTER-$(CONFIG_ARESAMPLE_FILTER) += fate-filter-aresample
+fate-filter-aresample: SRC = $(SAMPLES)/nellymoser/nellymoser-discont.flv
+fate-filter-aresample: CMD = pcm -i $(SRC) -af aresample=min_comp=0.001:min_hard_comp=0.1:first_pts=0
+fate-filter-aresample: CMP = oneoff
+fate-filter-aresample: REF = $(SAMPLES)/nellymoser/nellymoser-discont.pcm
+
+FATE_AFILTER-$(call FILTERDEMDECENCMUX, CHANNELMAP, WAV, PCM_S16LE, PCM_S16LE, WAV) += fate-filter-channelmap
+fate-filter-channelmap: SRC = $(TARGET_PATH)/tests/data/asynth-44100-6.wav
+fate-filter-channelmap: tests/data/asynth-44100-6.wav
+fate-filter-channelmap: CMD = md5 -i $(SRC) -filter_complex_script $(SRC_PATH)/tests/filtergraphs/channelmap -f wav -flags +bitexact
+fate-filter-channelmap: CMP = oneline
+fate-filter-channelmap: REF = 06168d06085e2c0603e4e118ba4cade2
+
+FATE_AFILTER-$(call FILTERDEMDECENCMUX, CHANNELSPLIT, WAV, PCM_S16LE, PCM_S16LE, PCM_S16LE) += fate-filter-channelsplit
+fate-filter-channelsplit: SRC = $(TARGET_PATH)/tests/data/asynth-44100-2.wav
+fate-filter-channelsplit: tests/data/asynth-44100-2.wav
+fate-filter-channelsplit: CMD = md5 -i $(SRC) -filter_complex channelsplit -f s16le
+fate-filter-channelsplit: CMP = oneline
+fate-filter-channelsplit: REF = d92988d0fe2dd92236763f47b07ab597
+
+FATE_AFILTER-$(call FILTERDEMDECENCMUX, JOIN, WAV, PCM_S16LE, PCM_S16LE, PCM_S16LE) += fate-filter-join
+fate-filter-join: SRC1 = $(TARGET_PATH)/tests/data/asynth-44100-2.wav
+fate-filter-join: SRC2 = $(TARGET_PATH)/tests/data/asynth-44100-3.wav
+fate-filter-join: tests/data/asynth-44100-2.wav tests/data/asynth-44100-3.wav
+fate-filter-join: CMD = md5 -i $(SRC1) -i $(SRC2) -filter_complex join=channel_layout=5 -f s16le
+fate-filter-join: CMP = oneline
+fate-filter-join: REF = 88b0d24a64717ba8635b29e8dac6ecd8
+
+FATE_AFILTER-$(call ALLYES, WAV_DEMUXER PCM_S16LE_DECODER PCM_S16LE_ENCODER PCM_S16LE_MUXER APERMS_FILTER VOLUME_FILTER) += fate-filter-volume
+fate-filter-volume: SRC = $(TARGET_PATH)/tests/data/asynth-44100-2.wav
+fate-filter-volume: tests/data/asynth-44100-2.wav
+fate-filter-volume: CMD = md5 -i $(SRC) -af aperms=random,volume=precision=fixed:volume=0.5 -f s16le
+fate-filter-volume: CMP = oneline
+fate-filter-volume: REF = 4d6ba75ef3e32d305d066b9bc771d6f4
+
+FATE_SAMPLES_AVCONV += $(FATE_AFILTER-yes)
+fate-afilter: $(FATE_AFILTER-yes)
diff --git a/tests/fate/filter-video.mak b/tests/fate/filter-video.mak
new file mode 100644
index 0000000..cc83d8b
--- /dev/null
+++ b/tests/fate/filter-video.mak
@@ -0,0 +1,96 @@
+FATE_FILTER-$(call ALLYES, PERMS_FILTER DELOGO_FILTER RM_DEMUXER RV30_DECODER) += fate-filter-delogo
+fate-filter-delogo: CMD = framecrc -i $(SAMPLES)/real/rv30.rm -vf perms=random,delogo=show=0:x=290:y=25:w=26:h=16 -an
+
+FATE_YADIF += fate-filter-yadif-mode0
+fate-filter-yadif-mode0: CMD = framecrc -flags bitexact -idct simple -i $(SAMPLES)/mpeg2/mpeg2_field_encoding.ts -vframes 30 -vf yadif=0
+
+FATE_YADIF += fate-filter-yadif-mode1
+fate-filter-yadif-mode1: CMD = framecrc -flags bitexact -idct simple -i $(SAMPLES)/mpeg2/mpeg2_field_encoding.ts -vframes 59 -vf yadif=1
+
+FATE_FILTER-$(call FILTERDEMDEC, YADIF, MPEGTS, MPEG2VIDEO) += $(FATE_YADIF)
+
+FATE_SAMPLES_AVCONV += $(FATE_FILTER-yes)
+
+
+FATE_FILTER_VSYNTH-$(CONFIG_BOXBLUR_FILTER) += fate-filter-boxblur
+fate-filter-boxblur: CMD = framecrc -c:v pgmyuv -i $(SRC) -vf boxblur=2:1
+
+FATE_FILTER_VSYNTH-$(CONFIG_DRAWBOX_FILTER) += fate-filter-drawbox
+fate-filter-drawbox: CMD = framecrc -c:v pgmyuv -i $(SRC) -vf drawbox=10:20:200:60:red@0.5
+
+FATE_FILTER_VSYNTH-$(CONFIG_FADE_FILTER) += fate-filter-fade
+fate-filter-fade: CMD = framecrc -c:v pgmyuv -i $(SRC) -vf fade=in:0:25,fade=out:25:25
+
+FATE_FILTER_VSYNTH-$(CONFIG_GRADFUN_FILTER) += fate-filter-gradfun
+fate-filter-gradfun: CMD = framecrc -c:v pgmyuv -i $(SRC) -vf gradfun
+
+FATE_FILTER_VSYNTH-$(CONFIG_HQDN3D_FILTER) += fate-filter-hqdn3d
+fate-filter-hqdn3d: CMD = framecrc -c:v pgmyuv -i $(SRC) -vf hqdn3d
+
+FATE_FILTER_VSYNTH-$(CONFIG_INTERLACE_FILTER) += fate-filter-interlace
+fate-filter-interlace: CMD = framecrc -c:v pgmyuv -i $(SRC) -vf interlace
+
+FATE_FILTER_VSYNTH-$(call ALLYES, NEGATE_FILTER PERMS_FILTER) += fate-filter-negate
+fate-filter-negate: CMD = framecrc -c:v pgmyuv -i $(SRC) -vf perms=random,negate
+
+FATE_FILTER_VSYNTH-$(CONFIG_HISTOGRAM_FILTER) += fate-filter-histogram-levels
+fate-filter-histogram-levels: CMD = framecrc -c:v pgmyuv -i $(SRC) -vf histogram -flags +bitexact -sws_flags +accurate_rnd+bitexact
+
+FATE_FILTER_VSYNTH-$(CONFIG_HISTOGRAM_FILTER) += fate-filter-histogram-waveform
+fate-filter-histogram-waveform: CMD = framecrc -c:v pgmyuv -i $(SRC) -vf histogram=mode=waveform -flags +bitexact -sws_flags +accurate_rnd+bitexact
+
+FATE_FILTER_VSYNTH-$(CONFIG_OVERLAY_FILTER) += fate-filter-overlay
+fate-filter-overlay: CMD = framecrc -c:v pgmyuv -i $(SRC) -c:v pgmyuv -i $(SRC) -filter_complex_script $(SRC_PATH)/tests/filtergraphs/overlay
+
+FATE_FILTER_VSYNTH-$(call ALLYES, SETPTS_FILTER  SETTB_FILTER) += fate-filter-setpts
+fate-filter-setpts: CMD = framecrc -c:v pgmyuv -i $(SRC) -filter_script $(SRC_PATH)/tests/filtergraphs/setpts
+
+FATE_FILTER_VSYNTH-$(CONFIG_TRANSPOSE_FILTER) += fate-filter-transpose
+fate-filter-transpose: CMD = framecrc -c:v pgmyuv -i $(SRC) -vf transpose
+
+FATE_FILTER_VSYNTH-$(CONFIG_UNSHARP_FILTER) += fate-filter-unsharp
+fate-filter-unsharp: CMD = framecrc -c:v pgmyuv -i $(SRC) -vf unsharp
+
+FATE_FILTER-$(call ALLYES, SMJPEG_DEMUXER MJPEG_DECODER PERMS_FILTER HQDN3D_FILTER) += fate-filter-hqdn3d-sample
+fate-filter-hqdn3d-sample: CMD = framecrc -idct simple -i $(SAMPLES)/smjpeg/scenwin.mjpg -vf perms=random,hqdn3d -an
+
+FATE_FILTER-$(call ALLYES, UTVIDEO_DECODER AVI_DEMUXER PERMS_FILTER CURVES_FILTER) += fate-filter-curves
+fate-filter-curves: CMD = framecrc -i $(SAMPLES)/utvideo/utvideo_rgb_median.avi -vf perms=random,curves=vintage
+
+FATE_FILTER-$(call ALLYES, VMD_DEMUXER VMDVIDEO_DECODER FORMAT_FILTER PERMS_FILTER GRADFUN_FILTER) += fate-filter-gradfun-sample
+fate-filter-gradfun-sample: CMD = framecrc -i $(SAMPLES)/vmd/12.vmd -filter_script $(SRC_PATH)/tests/filtergraphs/gradfun -an -frames:v 20
+
+FATE_FILTER-$(call ALLYES, TESTSRC_FILTER SINE_FILTER CONCAT_FILTER) += fate-filter-concat
+fate-filter-concat: CMD = framecrc -filter_complex_script $(SRC_PATH)/tests/filtergraphs/concat
+
+$(FATE_FILTER_VSYNTH-yes): $(VREF)
+$(FATE_FILTER_VSYNTH-yes): SRC = $(TARGET_PATH)/tests/vsynth1/%02d.pgm
+
+FATE_AVCONV-$(call DEMDEC, IMAGE2, PGMYUV) += $(FATE_FILTER_VSYNTH-yes)
+
+#
+# Metadata tests
+#
+FILTER_METADATA_COMMAND = ffprobe$(EXESUF) -of compact=p=0 -show_entries frame=pkt_pts:frame_tags -bitexact -f lavfi
+
+SCENEDETECT_DEPS = FFPROBE LAVFI_INDEV MOVIE_FILTER SELECT_FILTER SCALE_FILTER \
+                   AVCODEC AVDEVICE MOV_DEMUXER SVQ3_DECODER ZLIB
+FATE_METADATA_FILTER-$(call ALLYES, $(SCENEDETECT_DEPS)) += fate-filter-metadata-scenedetect
+fate-filter-metadata-scenedetect: SRC = $(SAMPLES)/svq3/Vertical400kbit.sorenson3.mov
+fate-filter-metadata-scenedetect: CMD = run $(FILTER_METADATA_COMMAND) "sws_flags=+accurate_rnd+bitexact;movie='$(SRC)',select=gt(scene\,.4)"
+
+SILENCEDETECT_DEPS = FFPROBE AVDEVICE LAVFI_INDEV AMOVIE_FILTER AMR_DEMUXER AMRWB_DECODER SILENCEDETECT_FILTER
+FATE_METADATA_FILTER-$(call ALLYES, $(SILENCEDETECT_DEPS)) += fate-filter-metadata-silencedetect
+fate-filter-metadata-silencedetect: SRC = $(SAMPLES)/amrwb/seed-12k65.awb
+fate-filter-metadata-silencedetect: CMD = run $(FILTER_METADATA_COMMAND) "amovie='$(SRC)',silencedetect=d=-20dB"
+
+EBUR128_METADATA_DEPS = FFPROBE AVDEVICE LAVFI_INDEV AMOVIE_FILTER FLAC_DEMUXER FLAC_DECODER EBUR128_FILTER
+FATE_METADATA_FILTER-$(call ALLYES, $(EBUR128_METADATA_DEPS)) += fate-filter-metadata-ebur128
+fate-filter-metadata-ebur128: SRC = $(SAMPLES)/filter/seq-3341-7_seq-3342-5-24bit.flac
+fate-filter-metadata-ebur128: CMD = run $(FILTER_METADATA_COMMAND) "amovie='$(SRC)',ebur128=metadata=1"
+
+FATE_SAMPLES_FFPROBE += $(FATE_METADATA_FILTER-yes)
+
+fate-vfilter: $(FATE_FILTER-yes) $(FATE_FILTER_VSYNTH-yes)
+
+fate-filter: fate-afilter fate-vfilter $(FATE_METADATA_FILTER-yes)
diff --git a/tests/fate/filter.mak b/tests/fate/filter.mak
deleted file mode 100644
index e974226..0000000
--- a/tests/fate/filter.mak
+++ /dev/null
@@ -1,75 +0,0 @@
-FATE_AMIX += fate-filter-amix-simple
-fate-filter-amix-simple: CMD = ffmpeg -filter_complex amix -i $(SRC) -ss 3 -i $(SRC1) -f f32le -
-fate-filter-amix-simple: REF = $(SAMPLES)/filter/amix_simple.pcm
-
-FATE_AMIX += fate-filter-amix-first
-fate-filter-amix-first: CMD = ffmpeg -filter_complex amix=duration=first -ss 4 -i $(SRC) -i $(SRC1) -f f32le -
-fate-filter-amix-first: REF = $(SAMPLES)/filter/amix_first.pcm
-
-FATE_AMIX += fate-filter-amix-transition
-fate-filter-amix-transition: tests/data/asynth-44100-2-3.wav
-fate-filter-amix-transition: SRC2 = $(TARGET_PATH)/tests/data/asynth-44100-2-3.wav
-fate-filter-amix-transition: CMD = ffmpeg -filter_complex amix=inputs=3:dropout_transition=0.5 -i $(SRC) -ss 2 -i $(SRC1) -ss 4 -i $(SRC2) -f f32le -
-fate-filter-amix-transition: REF = $(SAMPLES)/filter/amix_transition.pcm
-
-$(FATE_AMIX): tests/data/asynth-44100-2.wav tests/data/asynth-44100-2-2.wav
-$(FATE_AMIX): SRC  = $(TARGET_PATH)/tests/data/asynth-44100-2.wav
-$(FATE_AMIX): SRC1 = $(TARGET_PATH)/tests/data/asynth-44100-2-2.wav
-$(FATE_AMIX): CMP  = oneoff
-$(FATE_AMIX): CMP_UNIT = f32
-
-FATE_FILTER-$(CONFIG_AMIX_FILTER) += $(FATE_AMIX)
-
-FATE_FILTER-$(CONFIG_ASYNCTS_FILTER) += fate-filter-asyncts
-fate-filter-asyncts: SRC = $(SAMPLES)/nellymoser/nellymoser-discont.flv
-fate-filter-asyncts: CMD = pcm -analyzeduration 10000000 -i $(SRC) -af asyncts
-fate-filter-asyncts: CMP = oneoff
-fate-filter-asyncts: REF = $(SAMPLES)/nellymoser/nellymoser-discont-async-v2.pcm
-
-FATE_FILTER-$(CONFIG_ARESAMPLE_FILTER) += fate-filter-aresample
-fate-filter-aresample: SRC = $(SAMPLES)/nellymoser/nellymoser-discont.flv
-fate-filter-aresample: CMD = pcm -i $(SRC) -af aresample=min_comp=0.001:min_hard_comp=0.1:first_pts=0
-fate-filter-aresample: CMP = oneoff
-fate-filter-aresample: REF = $(SAMPLES)/nellymoser/nellymoser-discont.pcm
-
-fate-filter-delogo: CMD = framecrc -i $(SAMPLES)/real/rv30.rm -vf delogo=show=0:x=290:y=25:w=26:h=16 -an
-
-FATE_FILTER-$(CONFIG_DELOGO_FILTER) += fate-filter-delogo
-
-FATE_YADIF += fate-filter-yadif-mode0
-fate-filter-yadif-mode0: CMD = framecrc -flags bitexact -idct simple -i $(SAMPLES)/mpeg2/mpeg2_field_encoding.ts -vf yadif=0
-
-FATE_YADIF += fate-filter-yadif-mode1
-fate-filter-yadif-mode1: CMD = framecrc -flags bitexact -idct simple -i $(SAMPLES)/mpeg2/mpeg2_field_encoding.ts -vf yadif=1
-
-FATE_FILTER-$(CONFIG_YADIF_FILTER) += $(FATE_YADIF)
-
-FATE_HQDN3D += fate-filter-hqdn3d
-fate-filter-hqdn3d: CMD = framecrc -idct simple -i $(SAMPLES)/smjpeg/scenwin.mjpg -vf hqdn3d -an
-FATE_FILTER-$(call ALLYES, SMJPEG_DEMUXER MJPEG_DECODER HQDN3D_FILTER) += $(FATE_HQDN3D)
-
-FATE_GRADFUN += fate-filter-gradfun
-fate-filter-gradfun: CMD = framecrc -i $(SAMPLES)/vmd/12.vmd -vf "sws_flags=+accurate_rnd+bitexact;gradfun=10:8" -an -frames:v 20
-FATE_FILTER-$(call ALLYES, VMD_DEMUXER VMDVIDEO_DECODER GRADFUN_FILTER) += $(FATE_GRADFUN)
-
-FATE_SAMPLES_AVCONV += $(FATE_FILTER-yes)
-
-#
-# Metadata tests
-#
-FILTER_METADATA_COMMAND = ffprobe$(EXESUF) -show_frames -of compact=nk=1:p=0 -bitexact -f lavfi
-
-SCENEDETECT_DEPS = FFPROBE LAVFI_INDEV MOVIE_FILTER SELECT_FILTER SCALE_FILTER \
-                   AVCODEC MOV_DEMUXER SVQ3_DECODER ZLIB
-FATE_METADATA_FILTER-$(call ALLYES, $(SCENEDETECT_DEPS)) += fate-filter-metadata-scenedetect
-fate-filter-metadata-scenedetect: SRC = $(SAMPLES)/svq3/Vertical400kbit.sorenson3.mov
-fate-filter-metadata-scenedetect: CMD = run $(FILTER_METADATA_COMMAND) "sws_flags=+accurate_rnd+bitexact;movie='$(SRC)',select=gt(scene\,.4)"
-
-SILENCEDETECT_DEPS = FFPROBE LAVFI_INDEV AMOVIE_FILTER AMR_DEMUXER AMRWB_DECODER
-FATE_METADATA_FILTER-$(call ALLYES, $(SILENCEDETECT_DEPS)) += fate-filter-metadata-silencedetect
-fate-filter-metadata-silencedetect: SRC = $(SAMPLES)/amrwb/seed-12k65.awb
-fate-filter-metadata-silencedetect: CMD = run $(FILTER_METADATA_COMMAND) "amovie='$(SRC)',silencedetect=d=-20dB"
-
-FATE_SAMPLES_FFPROBE += $(FATE_METADATA_FILTER-yes)
-
-fate-filter: $(FATE_FILTER-yes) $(FATE_METADATA_FILTER-yes)
diff --git a/tests/fate/flac.mak b/tests/fate/flac.mak
index 92eb743..172f661 100644
--- a/tests/fate/flac.mak
+++ b/tests/fate/flac.mak
@@ -22,5 +22,7 @@
 fate-flac-%: CMP = oneoff
 fate-flac-%: FUZZ = 0
 
-FATE_SAMPLES_AVCONV += $(FATE_FLAC)
+FATE_FLAC-$(call ENCMUX, FLAC, FLAC) += $(FATE_FLAC)
+
+FATE_SAMPLES_AVCONV += $(FATE_FLAC-yes)
 fate-flac: $(FATE_FLAC)
diff --git a/tests/fate/gif.mak b/tests/fate/gif.mak
index 97b0545..110a067 100644
--- a/tests/fate/gif.mak
+++ b/tests/fate/gif.mak
@@ -1,12 +1,26 @@
 FATE_GIF += fate-gif-color
 fate-gif-color: CMD = framecrc -i $(SAMPLES)/gif/tc217.gif -pix_fmt bgra
 
+FATE_GIF += fate-gif-disposal-background
+fate-gif-disposal-background: CMD = framecrc -trans_color 0 -i $(SAMPLES)/gif/m4nb.gif -pix_fmt bgra
+
 FATE_GIF += fate-gif-disposal-restore
 fate-gif-disposal-restore: CMD = framecrc -i $(SAMPLES)/gif/banner2.gif -pix_fmt bgra
 
 FATE_GIF += fate-gif-gray
 fate-gif-gray: CMD = framecrc -i $(SAMPLES)/gif/Newtons_cradle_animation_book_2.gif -pix_fmt bgra
 
+fate-gifenc%: fate-gif-color
+fate-gifenc%: PIXFMT = $(word 3, $(subst -, ,$(@)))
+fate-gifenc%: SRC = $(SAMPLES)/gif/tc217.gif
+fate-gifenc%: CMD = framecrc -i $(SRC) -c:v gif -pix_fmt $(PIXFMT)
+
+FATE_GIF_ENC_PIXFMT = rgb8 bgr8 rgb4_byte bgr4_byte gray pal8
+FATE_GIF_ENC-$(call ENCDEC, GIF, GIF) = $(FATE_GIF_ENC_PIXFMT:%=fate-gifenc-%)
+
+FATE_GIF += $(FATE_GIF_ENC-yes)
+fate-gifenc: $(FATE_GIF_ENC-yes)
+
 FATE_GIF-$(call DEMDEC, GIF, GIF) += $(FATE_GIF)
 
 FATE_SAMPLES_AVCONV += $(FATE_GIF-yes)
diff --git a/tests/fate/h264.mak b/tests/fate/h264.mak
index ca87b7d..2cbd5df 100644
--- a/tests/fate/h264.mak
+++ b/tests/fate/h264.mak
@@ -182,11 +182,18 @@
             sva_nl1_b                                                   \
             sva_nl2_e                                                   \
 
+FATE_H264_REINIT_TESTS := large_420_8-to-small_420_8                    \
+                          small_420_8-to-large_444_10                   \
+                          small_420_9-to-small_420_8                    \
+                          small_422_9-to-small_420_9                    \
+
 FATE_H264  := $(FATE_H264:%=fate-h264-conformance-%)                    \
+              $(FATE_H264_REINIT_TESTS:%=fate-h264-reinit-%)            \
               fate-h264-extreme-plane-pred                              \
               fate-h264-lossless                                        \
 
 FATE_H264-$(call DEMDEC, H264, H264) += $(FATE_H264)
+FATE_H264-$(call DEMDEC,  MOV, H264) += fate-h264-crop-to-container
 FATE_H264-$(call DEMDEC,  MOV, H264) += fate-h264-interlace-crop
 FATE_H264-$(call ALLYES, MOV_DEMUXER H264_MP4TOANNEXB_BSF) += fate-h264-bsf-mp4toannexb
 
@@ -378,6 +385,9 @@
 fate-h264-conformance-sva_nl2_e:                  CMD = framecrc -vsync drop -i $(SAMPLES)/h264-conformance/SVA_NL2_E.264
 
 fate-h264-bsf-mp4toannexb:                        CMD = md5 -i $(SAMPLES)/h264/interlaced_crop.mp4 -vcodec copy -bsf h264_mp4toannexb -f h264
+fate-h264-crop-to-container:                      CMD = framemd5 -i $(SAMPLES)/h264/crop-to-container-dims-canon.mov
 fate-h264-extreme-plane-pred:                     CMD = framemd5 -i $(SAMPLES)/h264/extreme-plane-pred.h264
 fate-h264-interlace-crop:                         CMD = framecrc -i $(SAMPLES)/h264/interlaced_crop.mp4 -vframes 3
 fate-h264-lossless:                               CMD = framecrc -i $(SAMPLES)/h264/lossless.h264
+
+fate-h264-reinit-%:                               CMD = framecrc -i $(SAMPLES)/h264/$(@:fate-h264-%=%).h264 -vf format=yuv444p10le,scale=352:288
diff --git a/tests/fate/image.mak b/tests/fate/image.mak
index abafd5f..2a6a4b2 100644
--- a/tests/fate/image.mak
+++ b/tests/fate/image.mak
@@ -76,7 +76,7 @@
 FATE_IMAGE += $(FATE_TIFF-yes)
 fate-tiff: $(FATE_TIFF-yes)
 
-FATE_IMAGE += fate-xface
+FATE_IMAGE-$(call DEMDEC, IMAGE2, XFACE) += fate-xface
 fate-xface: CMD = framecrc -i $(SAMPLES)/xface/lena.xface
 
 FATE_IMAGE += $(FATE_IMAGE-yes)
diff --git a/tests/fate/libavutil.mak b/tests/fate/libavutil.mak
index ac5e933..2508ba0 100644
--- a/tests/fate/libavutil.mak
+++ b/tests/fate/libavutil.mak
@@ -8,6 +8,11 @@
 fate-aes: CMD = run libavutil/aes-test
 fate-aes: REF = /dev/null
 
+FATE_LIBAVUTIL += fate-atomic
+fate-atomic: libavutil/atomic-test$(EXESUF)
+fate-atomic: CMD = run libavutil/atomic-test
+fate-atomic: REF = /dev/null
+
 FATE_LIBAVUTIL += fate-avstring
 fate-avstring: libavutil/avstring-test$(EXESUF)
 fate-avstring: CMD = run libavutil/avstring-test
diff --git a/tests/fate/microsoft.mak b/tests/fate/microsoft.mak
index 10aeaf6..6347f20 100644
--- a/tests/fate/microsoft.mak
+++ b/tests/fate/microsoft.mak
@@ -1,5 +1,5 @@
 FATE_MICROSOFT-$(call DEMDEC, AVI, MSMPEG4V1) += fate-msmpeg4v1
-fate-msmpeg4v1: CMD = framecrc -flags +bitexact -dct fastint -idct simple -i $(SAMPLES)/msmpeg4v1/mpg4.avi -an
+fate-msmpeg4v1: CMD = framecrc -flags +bitexact -idct simple -i $(SAMPLES)/msmpeg4v1/mpg4.avi -an
 
 FATE_MSVIDEO1 += fate-msvideo1-8bit
 fate-msvideo1-8bit: CMD = framecrc -i $(SAMPLES)/cram/skating.avi -t 1 -pix_fmt rgb24
diff --git a/tests/fate/pcm.mak b/tests/fate/pcm.mak
index e978719..4a3822f 100644
--- a/tests/fate/pcm.mak
+++ b/tests/fate/pcm.mak
@@ -1,32 +1,32 @@
-FATE_SAMPLES_PCM += fate-iff-pcm
+FATE_SAMPLES_PCM-$(call DEMDEC, WAV, PCM_U8) += fate-iff-pcm
 fate-iff-pcm: CMD = md5 -i $(SAMPLES)/iff/Bells -f s16le
 
-FATE_SAMPLES_PCM += fate-pcm_dvd
+FATE_SAMPLES_PCM-$(call DEMDEC, MPEGPS, PCM_DVD) += fate-pcm_dvd
 fate-pcm_dvd: CMD = framecrc -i $(SAMPLES)/pcm-dvd/coolitnow-partial.vob -vn
 
-FATE_SAMPLES_PCM += fate-pcm-planar
+FATE_SAMPLES_PCM-$(call DEMDEC, EA, PCM_S16LE_PLANAR) += fate-pcm-planar
 fate-pcm-planar: CMD = framecrc -i $(SAMPLES)/ea-mad/xeasport.mad -vn
 
-FATE_SAMPLES_PCM += fate-pcm_s16be-stereo
+FATE_SAMPLES_PCM-$(call DEMDEC, MOV, PCM_S16BE) += fate-pcm_s16be-stereo
 fate-pcm_s16be-stereo: CMD = md5 -i $(SAMPLES)/qt-surge-suite/surge-2-16-B-twos.mov -f s16le
 
-FATE_SAMPLES_PCM += fate-pcm_s16le-stereo
+FATE_SAMPLES_PCM-$(call DEMDEC, MOV, PCM_S16LE) += fate-pcm_s16le-stereo
 fate-pcm_s16le-stereo: CMD = md5 -i $(SAMPLES)/qt-surge-suite/surge-2-16-L-sowt.mov -f s16le
 
-FATE_SAMPLES_PCM += fate-pcm_u8-mono
+FATE_SAMPLES_PCM-$(call DEMDEC, MOV, PCM_U8) += fate-pcm_u8-mono
 fate-pcm_u8-mono: CMD = md5 -i $(SAMPLES)/qt-surge-suite/surge-1-8-raw.mov -f s16le
 
-FATE_SAMPLES_PCM += fate-pcm_u8-stereo
+FATE_SAMPLES_PCM-$(call DEMDEC, MOV, PCM_U8) += fate-pcm_u8-stereo
 fate-pcm_u8-stereo: CMD = md5 -i $(SAMPLES)/qt-surge-suite/surge-2-8-raw.mov -f s16le
 
-FATE_SAMPLES_PCM += fate-w64
+FATE_SAMPLES_PCM-$(call DEMDEC, W64, PCM_S16LE) += fate-w64
 fate-w64: CMD = crc -i $(SAMPLES)/w64/w64-pcm16.w64
 
-FATE_PCM += fate-dcinema-encode
+FATE_PCM-$(call ENCMUX, PCM_S24DAUD, DAUD) += fate-dcinema-encode
 fate-dcinema-encode: tests/data/asynth-96000-6.wav
 fate-dcinema-encode: SRC = tests/data/asynth-96000-6.wav
 fate-dcinema-encode: CMD = enc_dec_pcm daud md5 s16le $(SRC) -c:a pcm_s24daud
 
-FATE_FFMPEG += $(FATE_PCM)
-FATE_SAMPLES_AVCONV += $(FATE_SAMPLES_PCM)
-fate-pcm: $(FATE_PCM) $(FATE_SAMPLES_PCM)
+FATE_FFMPEG += $(FATE_PCM-yes)
+FATE_SAMPLES_AVCONV += $(FATE_SAMPLES_PCM-yes)
+fate-pcm: $(FATE_PCM-yes) $(FATE_SAMPLES_PCM-yes)
diff --git a/tests/fate/probe.mak b/tests/fate/probe.mak
index 033b484..1166de4 100644
--- a/tests/fate/probe.mak
+++ b/tests/fate/probe.mak
@@ -1,15 +1,17 @@
-FATE_PROBE_FORMAT += fate-probe-format-roundup997
+FATE_PROBE_FORMAT-$(CONFIG_MPEGPS_DEMUXER) += fate-probe-format-roundup997
 fate-probe-format-roundup997:  REF = mpeg
 
-FATE_PROBE_FORMAT += fate-probe-format-roundup1383
+FATE_PROBE_FORMAT-$(CONFIG_MP3_DEMUXER) += fate-probe-format-roundup1383
 fate-probe-format-roundup1383: REF = mp3
 
-FATE_PROBE_FORMAT += fate-probe-format-roundup1414
+FATE_PROBE_FORMAT-$(CONFIG_MPEGPS_DEMUXER) += fate-probe-format-roundup1414
 fate-probe-format-roundup1414: REF = mpeg
 
-FATE_PROBE_FORMAT += fate-probe-format-roundup2015
+FATE_PROBE_FORMAT-$(CONFIG_DV_DEMUXER) += fate-probe-format-roundup2015
 fate-probe-format-roundup2015: REF = dv
 
+FATE_PROBE_FORMAT = $(FATE_PROBE_FORMAT-yes)
+
 FATE_EXTERN-$(CONFIG_FFPROBE) += $(FATE_PROBE_FORMAT)
 fate-probe-format: $(FATE_PROBE_FORMAT)
 
diff --git a/tests/fate/real.mak b/tests/fate/real.mak
index 0b9f202..6c640ad 100644
--- a/tests/fate/real.mak
+++ b/tests/fate/real.mak
@@ -13,7 +13,7 @@
 fate-ra-cook: REF = $(SAMPLES)/real/ra_cook.pcm
 
 FATE_REAL-$(call DEMDEC, RM, RV30) += fate-rv30
-fate-rv30: CMD = framecrc -flags +bitexact -dct fastint -idct simple -i $(SAMPLES)/real/rv30.rm -an
+fate-rv30: CMD = framecrc -flags +bitexact -idct simple -i $(SAMPLES)/real/rv30.rm -an
 
 FATE_REAL-$(call DEMDEC, RM, RV40) += fate-rv40
 fate-rv40: CMD = framecrc -i $(SAMPLES)/real/spygames-2MB.rmvb -t 10 -an
diff --git a/tests/fate/screen.mak b/tests/fate/screen.mak
index b55e02b..e1b854e 100644
--- a/tests/fate/screen.mak
+++ b/tests/fate/screen.mak
@@ -35,6 +35,9 @@
 FATE_SCREEN-$(call DEMDEC, AVI, TSCC) += $(FATE_TSCC)
 fate-tscc: $(FATE_TSCC)
 
+FATE_SAMPLES_AVCONV-$(call DEMDEC, AVI, TSCC2) += fate-tscc2
+fate-tscc2: CMD = framecrc -i $(SAMPLES)/tscc/tsc2_16bpp.avi
+
 FATE_VMNC += fate-vmnc-16bit
 fate-vmnc-16bit: CMD = framecrc -i $(SAMPLES)/VMnc/test.avi -pix_fmt rgb24
 
diff --git a/tests/fate/utvideo.mak b/tests/fate/utvideo.mak
index c6ce217..64d3896 100644
--- a/tests/fate/utvideo.mak
+++ b/tests/fate/utvideo.mak
@@ -66,7 +66,7 @@
 FATE_UTVIDEOENC += fate-utvideoenc_yuv422_none
 fate-utvideoenc_yuv422_none: OPTS = -pix_fmt yuv422p -pred 3
 
-$(FATE_UTVIDEOENC): tests/vsynth1/00.pgm
+$(FATE_UTVIDEOENC): $(VREF)
 
 FATE_AVCONV-$(call ENCMUX, UTVIDEO, AVI) += $(FATE_UTVIDEOENC)
 fate-utvideoenc: $(FATE_UTVIDEOENC)
diff --git a/tests/fate/vcodec.mak b/tests/fate/vcodec.mak
index f10e80c..cc2ad28 100644
--- a/tests/fate/vcodec.mak
+++ b/tests/fate/vcodec.mak
@@ -55,7 +55,7 @@
 fate-vsynth%-dv-50:              FMT     = dv
 
 FATE_VCODEC-$(call ENCDEC, FFV1, AVI)   += ffv1
-fate-vsynth%-ffv1:               ENCOPTS = -strict -2
+fate-vsynth%-ffv1:               ENCOPTS = -slices 4 -strict -2
 
 FATE_VCODEC-$(call ENCDEC, FFVHUFF, AVI) += ffvhuff
 
@@ -89,7 +89,7 @@
 fate-vsynth%-jpegls:             ENCOPTS = -sws_flags neighbor+full_chroma_int
 fate-vsynth%-jpegls:             DECOPTS = -sws_flags area
 
-FATE_VCODEC-$(call ENCDEC, JPEG2000, AVI) += j2k
+FATE_VCODEC-$(call ENCDEC, J2K, AVI) += j2k
 fate-vsynth%-j2k:                ENCOPTS = -qscale 7 -strict experimental -pix_fmt rgb24
 fate-vsynth%-j2k:                DECINOPTS = -vcodec j2k -strict experimental
 
@@ -124,7 +124,7 @@
 $(FATE_MPEG2:%=fate-vsynth\%-%): CODEC  = mpeg2video
 
 fate-vsynth%-mpeg2:              ENCOPTS = -qscale 10
-fate-vsynth%-mpeg2-422:          ENCOPTS = -vb 1000k                    \
+fate-vsynth%-mpeg2-422:          ENCOPTS = -b:v 1000k                   \
                                            -bf 2                        \
                                            -trellis 1                   \
                                            -flags +mv0+ildct+ilme       \
@@ -134,7 +134,7 @@
                                            -pix_fmt yuv422p
 fate-vsynth%-mpeg2-idct-int:     ENCOPTS = -qscale 10 -idct int -dct int
 fate-vsynth%-mpeg2-ilace:        ENCOPTS = -qscale 10 -flags +ildct+ilme
-fate-vsynth%-mpeg2-ivlc-qprd:    ENCOPTS = -vb 500k                     \
+fate-vsynth%-mpeg2-ivlc-qprd:    ENCOPTS = -b:v 500k                    \
                                            -bf 2                        \
                                            -trellis 1                   \
                                            -flags +mv0                  \
@@ -202,11 +202,11 @@
 
 FATE_VCODEC-$(call ENCDEC, MSVIDEO1, AVI) += msvideo1
 
-FATE_VCODEC-$(call ENCDEC, PRORES, MOV) += prores prores_kostya
+FATE_VCODEC-$(call ENCDEC, PRORES, MOV) += prores prores_ks
 fate-vsynth%-prores:             FMT     = mov
 
-fate-vsynth%-prores_kostya:      ENCOPTS = -profile hq
-fate-vsynth%-prores_kostya:      FMT     = mov
+fate-vsynth%-prores_ks:          ENCOPTS = -profile hq
+fate-vsynth%-prores_ks:          FMT     = mov
 
 FATE_VCODEC-$(call ENCDEC, QTRLE, MOV)  += qtrle qtrlegray
 fate-vsynth%-qtrle:              FMT     = mov
diff --git a/tests/fate/video.mak b/tests/fate/video.mak
index 78049e5..b98f1f0 100644
--- a/tests/fate/video.mak
+++ b/tests/fate/video.mak
@@ -156,6 +156,9 @@
 FATE_VIDEO-$(call DEMDEC, IPMOVIE, INTERPLAY_VIDEO) += fate-interplay-mve-16bit
 fate-interplay-mve-16bit: CMD = framecrc -i $(SAMPLES)/interplay-mve/descent3-level5-16bit-partial.mve -pix_fmt rgb24 -an
 
+FATE_VIDEO-$(call DEMDEC, MXF, JPEG2000) += fate-jpeg2000-dcinema
+fate-jpeg2000-dcinema: CMD = framecrc -flags +bitexact -i $(SAMPLES)/jpeg2000/chiens_dcinema2K.mxf
+
 FATE_VIDEO-$(call DEMDEC, JV, JV) += fate-jv
 fate-jv: CMD = framecrc -i $(SAMPLES)/jv/intro.jv -pix_fmt rgb24 -an
 
@@ -181,7 +184,7 @@
 fate-motionpixels: CMD = framecrc -i $(SAMPLES)/motion-pixels/INTRO-partial.MVI -an -pix_fmt rgb24 -vframes 111
 
 FATE_VIDEO-$(call DEMDEC, MPEGTS, MPEG2VIDEO) += fate-mpeg2-field-enc
-fate-mpeg2-field-enc: CMD = framecrc -flags +bitexact -dct fastint -idct simple -i $(SAMPLES)/mpeg2/mpeg2_field_encoding.ts -an
+fate-mpeg2-field-enc: CMD = framecrc -flags +bitexact -idct simple -i $(SAMPLES)/mpeg2/mpeg2_field_encoding.ts -an -vframes 30
 
 FATE_VIDEO-$(call DEMDEC, MXG, MXPEG) += fate-mxpeg
 fate-mxpeg: CMD = framecrc -idct simple -flags +bitexact -i $(SAMPLES)/mxpeg/m1.mxg -an
@@ -254,7 +257,7 @@
 fate-v410dec: CMD = framecrc -i $(SAMPLES)/v410/lenav410.mov -pix_fmt yuv444p10le
 
 FATE_VIDEO-$(call ENCDEC, V410 PGMYUV, AVI IMAGE2) += fate-v410enc
-fate-v410enc: tests/vsynth1/00.pgm
+fate-v410enc: $(VREF)
 fate-v410enc: CMD = md5 -f image2 -vcodec pgmyuv -i $(TARGET_PATH)/tests/vsynth1/%02d.pgm -flags +bitexact -vcodec v410 -f avi
 
 FATE_VIDEO-$(call DEMDEC, SIFF, VB) += fate-vb
diff --git a/tests/fate/voice.mak b/tests/fate/voice.mak
index 3535d55..f27be18 100644
--- a/tests/fate/voice.mak
+++ b/tests/fate/voice.mak
@@ -1,12 +1,12 @@
-FATE_G722 += fate-g722dec-1
+FATE_G722-$(call DEMDEC, G722, ADPCM_G722) += fate-g722dec-1
 fate-g722dec-1: CMD = framecrc -i $(SAMPLES)/g722/conf-adminmenu-162.g722
 
-FATE_G722 += fate-g722-encode
+FATE_G722-$(call ENCMUX, ADPCM_G722, WAV) += fate-g722-encode
 fate-g722-encode: tests/data/asynth-16000-1.wav
 fate-g722-encode: SRC = tests/data/asynth-16000-1.wav
 fate-g722-encode: CMD = enc_dec_pcm wav md5 s16le $(SRC) -c:a g722
 
-FATE_VOICE += $(FATE_G722)
+FATE_VOICE-yes += $(FATE_G722-yes)
 fate-g722: $(FATE_G722)
 
 FATE_G723_1 += fate-g723_1-dec-1
@@ -33,7 +33,8 @@
 FATE_G723_1 += fate-g723_1-dec-8
 fate-g723_1-dec-8: CMD = framecrc -postfilter 1 -i $(SAMPLES)/g723_1/dtx63e.tco
 
-FATE_SAMPLES_AVCONV += $(FATE_G723_1)
+FATE_G723_1-$(call DEMDEC, G723_1, G723_1) += $(FATE_G723_1)
+FATE_SAMPLES_AVCONV += $(FATE_G723_1-yes)
 fate-g723_1: $(FATE_G723_1)
 
 FATE_G726 += fate-g726-encode-2bit
@@ -51,27 +52,27 @@
 $(FATE_G726): tests/data/asynth-8000-1.wav
 $(FATE_G726): SRC = tests/data/asynth-8000-1.wav
 
-FATE_VOICE += $(FATE_G726)
+FATE_VOICE-$(call ENCMUX, ADPCM_G726, WAV) += $(FATE_G726)
 fate-g726: $(FATE_G726)
 
-FATE_GSM += fate-gsm-ms
+FATE_GSM-$(call DEMDEC, WAV, GSM) += fate-gsm-ms
 fate-gsm-ms: CMD = framecrc -i $(SAMPLES)/gsm/ciao.wav
 
-FATE_GSM += fate-gsm-toast
+FATE_GSM-$(call DEMDEC, MOV, GSM) += fate-gsm-toast
 fate-gsm-toast: CMD = framecrc -i $(SAMPLES)/gsm/sample-gsm-8000.mov -t 10
 
-FATE_VOICE += $(FATE_GSM)
+FATE_VOICE-yes += $(FATE_GSM-yes)
 fate-gsm: $(FATE_GSM)
 
-FATE_VOICE += fate-qcelp
+FATE_VOICE-$(call DEMDEC, QCP, QCELP) += fate-qcelp
 fate-qcelp: CMD = pcm -i $(SAMPLES)/qcp/0036580847.QCP
 fate-qcelp: CMP = oneoff
 fate-qcelp: REF = $(SAMPLES)/qcp/0036580847.pcm
 
-FATE_VOICE += fate-truespeech
+FATE_VOICE-$(call DEMDEC, WAV, TRUESPEECH) += fate-truespeech
 fate-truespeech: CMD = pcm -i $(SAMPLES)/truespeech/a6.wav
 fate-truespeech: CMP = oneoff
 fate-truespeech: REF = $(SAMPLES)/truespeech/a6.pcm
 
-FATE_SAMPLES_FFMPEG += $(FATE_VOICE)
-fate-voice: $(FATE_VOICE)
+FATE_SAMPLES_FFMPEG += $(FATE_VOICE-yes)
+fate-voice: $(FATE_VOICE-yes)
diff --git a/tests/fate/vpx.mak b/tests/fate/vpx.mak
index 061a1e8..bb20dda 100644
--- a/tests/fate/vpx.mak
+++ b/tests/fate/vpx.mak
@@ -1,26 +1,26 @@
 FATE_VP3-$(call DEMDEC, MATROSKA, THEORA) += fate-theora-coeff-level64
-fate-theora-coeff-level64: CMD = framecrc -i $(SAMPLES)/vp3/coeff_level64.mkv
+fate-theora-coeff-level64: CMD = framecrc -flags +bitexact -i $(SAMPLES)/vp3/coeff_level64.mkv
 
 FATE_VP3-$(call DEMDEC, AVI, VP3) += fate-vp31
-fate-vp31: CMD = framecrc -i $(SAMPLES)/vp3/vp31.avi
+fate-vp31: CMD = framecrc -flags +bitexact -i $(SAMPLES)/vp3/vp31.avi
 
 FATE_SAMPLES_AVCONV += $(FATE_VP3-yes)
 fate-vp3: $(FATE_VP3-yes)
 
 FATE_SAMPLES_AVCONV-$(call DEMDEC, AVI, VP5) += fate-vp5
-fate-vp5: CMD = framecrc -i $(SAMPLES)/vp5/potter512-400-partial.avi -an
+fate-vp5: CMD = framecrc -flags +bitexact -i $(SAMPLES)/vp5/potter512-400-partial.avi -an
 
 FATE_VP6-$(call DEMDEC, EA, VP6) += fate-vp60
-fate-vp60: CMD = framecrc -i $(SAMPLES)/ea-vp6/g36.vp6
+fate-vp60: CMD = framecrc -flags +bitexact -i $(SAMPLES)/ea-vp6/g36.vp6
 
 FATE_VP6-$(call DEMDEC, EA, VP6) += fate-vp61
-fate-vp61: CMD = framecrc -i $(SAMPLES)/ea-vp6/MovieSkirmishGondor.vp6 -t 4
+fate-vp61: CMD = framecrc -flags +bitexact -i $(SAMPLES)/ea-vp6/MovieSkirmishGondor.vp6 -t 4
 
 FATE_VP6-$(call DEMDEC, FLV, VP6A) += fate-vp6a
-fate-vp6a: CMD = framecrc -i $(SAMPLES)/flash-vp6/300x180-Scr-f8-056alpha.flv
+fate-vp6a: CMD = framecrc -flags +bitexact -i $(SAMPLES)/flash-vp6/300x180-Scr-f8-056alpha.flv
 
 FATE_VP6-$(call DEMDEC, FLV, VP6F) += fate-vp6f
-fate-vp6f: CMD = framecrc -i $(SAMPLES)/flash-vp6/clip1024.flv
+fate-vp6f: CMD = framecrc -flags +bitexact -i $(SAMPLES)/flash-vp6/clip1024.flv
 
 FATE_VP8-$(call DEMDEC, FLV, VP8) += fate-vp8-alpha
 fate-vp8-alpha: CMD = framecrc -i $(SAMPLES)/vp8_alpha/vp8_video_with_alpha.webm -vcodec copy
diff --git a/tests/filtergraphs/channelmap b/tests/filtergraphs/channelmap
new file mode 100644
index 0000000..e2b6143
--- /dev/null
+++ b/tests/filtergraphs/channelmap
@@ -0,0 +1 @@
+channelmap=map=1|2|0|5|3|4:channel_layout=5.1
diff --git a/tests/filtergraphs/concat b/tests/filtergraphs/concat
new file mode 100644
index 0000000..26bca14
--- /dev/null
+++ b/tests/filtergraphs/concat
@@ -0,0 +1,8 @@
+testsrc=r=5:n=1:d=2 [v1];
+sine=440:b=2:d=1    [a1];
+testsrc=r=5:n=1:d=1 [v2];
+sine=622:b=2:d=2    [a2];
+testsrc=r=5:n=1:d=1 [v3];
+sine=880:b=2:d=1    [a3];
+
+[v1][a1][v2][a2][v3][a3] concat=v=1:a=1:n=3
diff --git a/tests/filtergraphs/gradfun b/tests/filtergraphs/gradfun
new file mode 100644
index 0000000..2923cb5
--- /dev/null
+++ b/tests/filtergraphs/gradfun
@@ -0,0 +1,2 @@
+sws_flags=+accurate_rnd+bitexact;
+format=gray, perms=random, gradfun=10:8
diff --git a/tests/filtergraphs/overlay b/tests/filtergraphs/overlay
new file mode 100644
index 0000000..f648274
--- /dev/null
+++ b/tests/filtergraphs/overlay
@@ -0,0 +1,3 @@
+sws_flags=+accurate_rnd+bitexact;
+[1:v] scale=50:50 [over];
+[0:v][over] overlay=20:20
diff --git a/tests/filtergraphs/setpts b/tests/filtergraphs/setpts
new file mode 100644
index 0000000..79037d1
--- /dev/null
+++ b/tests/filtergraphs/setpts
@@ -0,0 +1,2 @@
+settb=1/1000,
+setpts=1/(35*TB) * (N + 0.05 * sin(N*2*PI/25))
diff --git a/tests/lavf-regression.sh b/tests/lavf-regression.sh
index d0335c1..196e965 100755
--- a/tests/lavf-regression.sh
+++ b/tests/lavf-regression.sh
@@ -99,7 +99,7 @@
 fi
 
 if [ -n "$do_ffm" ] ; then
-do_lavf ffm "-ab 64k"
+do_lavf ffm
 fi
 
 if [ -n "$do_flm" ] ; then
@@ -180,18 +180,9 @@
 fi
 
 if [ -n "$do_gif" ] ; then
-# this tests the gif muxer
 file=${outfile}lavf.gif
 do_avconv $file $DEC_OPTS -f image2 -vcodec pgmyuv -i $raw_src $ENC_OPTS -t 1 -qscale 10 -pix_fmt rgb24
 do_avconv_crc $file $DEC_OPTS -i $target_path/$file -pix_fmt rgb24
-# and this the gif encoder
-do_image_formats gif "" "-pix_fmt rgb24"
-do_image_formats gif "-pix_fmt rgb4_byte" "-pix_fmt rgb24"
-do_image_formats gif "-pix_fmt bgr4_byte" "-pix_fmt rgb24"
-do_image_formats gif "-pix_fmt rgb8" "-pix_fmt rgb24"
-do_image_formats gif "-pix_fmt bgr8" "-pix_fmt rgb24"
-do_image_formats gif "-pix_fmt gray" "-pix_fmt rgb24"
-do_image_formats gif "-pix_fmt pal8" "-pix_fmt rgb24"
 fi
 
 if [ -n "$do_yuv4mpeg" ] ; then
diff --git a/tests/lavfi-regression.sh b/tests/lavfi-regression.sh
index d6aba4c..d3e95d7 100755
--- a/tests/lavfi-regression.sh
+++ b/tests/lavfi-regression.sh
@@ -42,24 +42,26 @@
 do_lavfi "crop_scale_vflip"   "null,null,crop=iw-200:ih-200:200:200,crop=iw-20:ih-20:20:20,scale=200:200,scale=250:250,vflip,vflip,null,scale=200:200,crop=iw-100:ih-100:100:100,vflip,scale=200:200,null,vflip,crop=iw-100:ih-100:100:100,null"
 do_lavfi "crop_vflip"         "crop=iw-100:ih-100:100:100,vflip"
 do_lavfi "drawbox"            "drawbox=224:24:88:72:#FF8010@0.5"
-do_lavfi "edgedetect"         "edgedetect"
+do_lavfi "edgedetect"         "format=gray,perms=random,edgedetect"
 do_lavfi "fade"               "fade=in:5:15,fade=out:30:15"
-do_lavfi "hue"                "hue=s=sin(2*PI*t)+1"
+do_lavfi "hue"                "perms=random,hue=s=sin(2*PI*t)+1"
 do_lavfi "idet"               "idet"
 do_lavfi "null"               "null"
-do_lavfi "overlay"            "split[m],scale=88:72,pad=96:80:4:4[o2];[m]fifo[o1],[o1][o2]overlay=240:16"
+do_lavfi "overlay_rgb"        "split[m],scale=88:72,pad=96:80:4:4[o2];[m]fifo[o1],[o1][o2]overlay=240:16:format=rgb"
+do_lavfi "overlay_yuv420"     "split[m],scale=88:72,pad=96:80:4:4[o2];[m]fifo[o1],[o1][o2]overlay=240:16:format=yuv420"
+do_lavfi "overlay_yuv444"     "split[m],scale=88:72,pad=96:80:4:4[o2];[m]fifo[o1],[o1][o2]overlay=240:16:format=yuv444"
 do_lavfi "pad"                "pad=iw*1.5:ih*1.5:iw*0.3:ih*0.2"
 do_lavfi "pp"                 "pp=be/hb/vb/tn/l5/al"
-do_lavfi "pp2"                "pp=be/fq:16/h1/v1/lb"
-do_lavfi "pp3"                "pp=be/fq:8/ha:128:7/va/li"
+do_lavfi "pp2"                "pp=be/fq|16/h1/v1/lb"
+do_lavfi "pp3"                "pp=be/fq|8/ha|128|7/va/li"
 do_lavfi "pp4"                "pp=be/ci"
 do_lavfi "pp5"                "pp=md"
 do_lavfi "pp6"                "pp=be/fd"
 do_lavfi "scale200"           "scale=200:200"
 do_lavfi "scale500"           "scale=500:500"
 do_lavfi "select"             "select=not(eq(mod(n\,2)\,0)+eq(mod(n\,3)\,0))"
-do_lavfi "setdar"             "setdar=16/9"
-do_lavfi "setsar"             "setsar=16/11"
+do_lavfi "setdar"             "setdar=dar=16/9"
+do_lavfi "setsar"             "setsar=sar=16/11"
 do_lavfi "thumbnail"          "thumbnail=10"
 do_lavfi "tile"               "tile=3x3:nb_frames=5:padding=7:margin=2"
 do_lavfi "transpose"          "transpose"
diff --git a/tests/ref/fate/eval b/tests/ref/fate/eval
index 59e3fe4..97e0b20 100644
--- a/tests/ref/fate/eval
+++ b/tests/ref/fate/eval
@@ -250,5 +250,23 @@
 Evaluating 'gcd(30,55)*print(min(9,1))'
 'gcd(30,55)*print(min(9,1))' -> 5.000000
 
+Evaluating 'bitor(42, 12)'
+'bitor(42, 12)' -> 46.000000
+
+Evaluating 'bitand(42, 12)'
+'bitand(42, 12)' -> 8.000000
+
+Evaluating 'bitand(NAN, 1)'
+'bitand(NAN, 1)' -> nan
+
+Evaluating 'between(10, -3, 10)'
+'between(10, -3, 10)' -> 1.000000
+
+Evaluating 'between(-4, -2, -1)'
+'between(-4, -2, -1)' -> 0.000000
+
+Evaluating 'between(1,2)'
+'between(1,2)' -> nan
+
 12.700000 == 12.7
 0.931323 == 0.931322575
diff --git a/tests/ref/fate/ffmpeg-filter_complex b/tests/ref/fate/ffmpeg-filter_complex
new file mode 100644
index 0000000..54d318d
--- /dev/null
+++ b/tests/ref/fate/ffmpeg-filter_complex
@@ -0,0 +1,6 @@
+#tb 0: 1/5
+0,          0,          0,        1,   115200, 0x375ec573
+0,          1,          1,        1,   115200, 0x375ec573
+0,          2,          2,        1,   115200, 0x375ec573
+0,          3,          3,        1,   115200, 0x375ec573
+0,          4,          4,        1,   115200, 0x375ec573
diff --git a/tests/ref/fate/ffmpeg-lavfi b/tests/ref/fate/ffmpeg-lavfi
new file mode 100644
index 0000000..54d318d
--- /dev/null
+++ b/tests/ref/fate/ffmpeg-lavfi
@@ -0,0 +1,6 @@
+#tb 0: 1/5
+0,          0,          0,        1,   115200, 0x375ec573
+0,          1,          1,        1,   115200, 0x375ec573
+0,          2,          2,        1,   115200, 0x375ec573
+0,          3,          3,        1,   115200, 0x375ec573
+0,          4,          4,        1,   115200, 0x375ec573
diff --git a/tests/ref/fate/ffprobe_compact b/tests/ref/fate/ffprobe_compact
index 2c903d4..470a3f7 100644
--- a/tests/ref/fate/ffprobe_compact
+++ b/tests/ref/fate/ffprobe_compact
@@ -1,31 +1,31 @@
 packet|codec_type=audio|stream_index=0|pts=0|pts_time=0.000000|dts=0|dts_time=0.000000|duration=1024|duration_time=0.023220|convergence_duration=N/A|convergence_duration_time=N/A|size=2048|pos=572|flags=K
 frame|media_type=audio|key_frame=1|pkt_pts=0|pkt_pts_time=0.000000|pkt_dts=0|pkt_dts_time=0.000000|pkt_duration=1024|pkt_duration_time=0.023220|pkt_pos=572|pkt_size=2048|sample_fmt=s16|nb_samples=1024|channels=1|channel_layout=unknown
 packet|codec_type=video|stream_index=1|pts=0|pts_time=0.000000|dts=0|dts_time=0.000000|duration=2048|duration_time=0.040000|convergence_duration=N/A|convergence_duration_time=N/A|size=230400|pos=2647|flags=K
-frame|media_type=video|key_frame=1|pkt_pts=0|pkt_pts_time=0.000000|pkt_dts=0|pkt_dts_time=0.000000|pkt_duration=2048|pkt_duration_time=0.040000|pkt_pos=2647|pkt_size=N/A|width=320|height=240|pix_fmt=rgb24|sample_aspect_ratio=1:1|pict_type=I|coded_picture_number=0|display_picture_number=0|interlaced_frame=0|top_field_first=0|repeat_pict=0|reference=0
+frame|media_type=video|key_frame=1|pkt_pts=0|pkt_pts_time=0.000000|pkt_dts=0|pkt_dts_time=0.000000|pkt_duration=2048|pkt_duration_time=0.040000|pkt_pos=2647|pkt_size=N/A|width=320|height=240|pix_fmt=rgb24|sample_aspect_ratio=1:1|pict_type=I|coded_picture_number=0|display_picture_number=0|interlaced_frame=0|top_field_first=0|repeat_pict=0
 packet|codec_type=video|stream_index=2|pts=0|pts_time=0.000000|dts=0|dts_time=0.000000|duration=2048|duration_time=0.040000|convergence_duration=N/A|convergence_duration_time=N/A|size=30000|pos=233068|flags=K
-frame|media_type=video|key_frame=1|pkt_pts=0|pkt_pts_time=0.000000|pkt_dts=0|pkt_dts_time=0.000000|pkt_duration=2048|pkt_duration_time=0.040000|pkt_pos=233068|pkt_size=N/A|width=100|height=100|pix_fmt=rgb24|sample_aspect_ratio=1:1|pict_type=I|coded_picture_number=0|display_picture_number=0|interlaced_frame=0|top_field_first=0|repeat_pict=0|reference=0
+frame|media_type=video|key_frame=1|pkt_pts=0|pkt_pts_time=0.000000|pkt_dts=0|pkt_dts_time=0.000000|pkt_duration=2048|pkt_duration_time=0.040000|pkt_pos=233068|pkt_size=N/A|width=100|height=100|pix_fmt=rgb24|sample_aspect_ratio=1:1|pict_type=I|coded_picture_number=0|display_picture_number=0|interlaced_frame=0|top_field_first=0|repeat_pict=0
 packet|codec_type=audio|stream_index=0|pts=1024|pts_time=0.023220|dts=1024|dts_time=0.023220|duration=1024|duration_time=0.023220|convergence_duration=N/A|convergence_duration_time=N/A|size=2048|pos=263073|flags=K
 frame|media_type=audio|key_frame=1|pkt_pts=1024|pkt_pts_time=0.023220|pkt_dts=1024|pkt_dts_time=0.023220|pkt_duration=1024|pkt_duration_time=0.023220|pkt_pos=263073|pkt_size=2048|sample_fmt=s16|nb_samples=1024|channels=1|channel_layout=unknown
 packet|codec_type=video|stream_index=1|pts=2048|pts_time=0.040000|dts=2048|dts_time=0.040000|duration=2048|duration_time=0.040000|convergence_duration=N/A|convergence_duration_time=N/A|size=230400|pos=265151|flags=K
-frame|media_type=video|key_frame=1|pkt_pts=2048|pkt_pts_time=0.040000|pkt_dts=2048|pkt_dts_time=0.040000|pkt_duration=2048|pkt_duration_time=0.040000|pkt_pos=265151|pkt_size=N/A|width=320|height=240|pix_fmt=rgb24|sample_aspect_ratio=1:1|pict_type=I|coded_picture_number=0|display_picture_number=0|interlaced_frame=0|top_field_first=0|repeat_pict=0|reference=0
+frame|media_type=video|key_frame=1|pkt_pts=2048|pkt_pts_time=0.040000|pkt_dts=2048|pkt_dts_time=0.040000|pkt_duration=2048|pkt_duration_time=0.040000|pkt_pos=265151|pkt_size=N/A|width=320|height=240|pix_fmt=rgb24|sample_aspect_ratio=1:1|pict_type=I|coded_picture_number=0|display_picture_number=0|interlaced_frame=0|top_field_first=0|repeat_pict=0
 packet|codec_type=video|stream_index=2|pts=2048|pts_time=0.040000|dts=2048|dts_time=0.040000|duration=2048|duration_time=0.040000|convergence_duration=N/A|convergence_duration_time=N/A|size=30000|pos=495575|flags=K
-frame|media_type=video|key_frame=1|pkt_pts=2048|pkt_pts_time=0.040000|pkt_dts=2048|pkt_dts_time=0.040000|pkt_duration=2048|pkt_duration_time=0.040000|pkt_pos=495575|pkt_size=N/A|width=100|height=100|pix_fmt=rgb24|sample_aspect_ratio=1:1|pict_type=I|coded_picture_number=0|display_picture_number=0|interlaced_frame=0|top_field_first=0|repeat_pict=0|reference=0
+frame|media_type=video|key_frame=1|pkt_pts=2048|pkt_pts_time=0.040000|pkt_dts=2048|pkt_dts_time=0.040000|pkt_duration=2048|pkt_duration_time=0.040000|pkt_pos=495575|pkt_size=N/A|width=100|height=100|pix_fmt=rgb24|sample_aspect_ratio=1:1|pict_type=I|coded_picture_number=0|display_picture_number=0|interlaced_frame=0|top_field_first=0|repeat_pict=0
 packet|codec_type=audio|stream_index=0|pts=2048|pts_time=0.046440|dts=2048|dts_time=0.046440|duration=1024|duration_time=0.023220|convergence_duration=N/A|convergence_duration_time=N/A|size=2048|pos=525580|flags=K
 frame|media_type=audio|key_frame=1|pkt_pts=2048|pkt_pts_time=0.046440|pkt_dts=2048|pkt_dts_time=0.046440|pkt_duration=1024|pkt_duration_time=0.023220|pkt_pos=525580|pkt_size=2048|sample_fmt=s16|nb_samples=1024|channels=1|channel_layout=unknown
 packet|codec_type=audio|stream_index=0|pts=3072|pts_time=0.069660|dts=3072|dts_time=0.069660|duration=1024|duration_time=0.023220|convergence_duration=N/A|convergence_duration_time=N/A|size=2048|pos=527651|flags=K
 frame|media_type=audio|key_frame=1|pkt_pts=3072|pkt_pts_time=0.069660|pkt_dts=3072|pkt_dts_time=0.069660|pkt_duration=1024|pkt_duration_time=0.023220|pkt_pos=527651|pkt_size=2048|sample_fmt=s16|nb_samples=1024|channels=1|channel_layout=unknown
 packet|codec_type=video|stream_index=1|pts=4096|pts_time=0.080000|dts=4096|dts_time=0.080000|duration=2048|duration_time=0.040000|convergence_duration=N/A|convergence_duration_time=N/A|size=230400|pos=529729|flags=K
-frame|media_type=video|key_frame=1|pkt_pts=4096|pkt_pts_time=0.080000|pkt_dts=4096|pkt_dts_time=0.080000|pkt_duration=2048|pkt_duration_time=0.040000|pkt_pos=529729|pkt_size=N/A|width=320|height=240|pix_fmt=rgb24|sample_aspect_ratio=1:1|pict_type=I|coded_picture_number=0|display_picture_number=0|interlaced_frame=0|top_field_first=0|repeat_pict=0|reference=0
+frame|media_type=video|key_frame=1|pkt_pts=4096|pkt_pts_time=0.080000|pkt_dts=4096|pkt_dts_time=0.080000|pkt_duration=2048|pkt_duration_time=0.040000|pkt_pos=529729|pkt_size=N/A|width=320|height=240|pix_fmt=rgb24|sample_aspect_ratio=1:1|pict_type=I|coded_picture_number=0|display_picture_number=0|interlaced_frame=0|top_field_first=0|repeat_pict=0
 packet|codec_type=video|stream_index=2|pts=4096|pts_time=0.080000|dts=4096|dts_time=0.080000|duration=2048|duration_time=0.040000|convergence_duration=N/A|convergence_duration_time=N/A|size=30000|pos=760153|flags=K
-frame|media_type=video|key_frame=1|pkt_pts=4096|pkt_pts_time=0.080000|pkt_dts=4096|pkt_dts_time=0.080000|pkt_duration=2048|pkt_duration_time=0.040000|pkt_pos=760153|pkt_size=N/A|width=100|height=100|pix_fmt=rgb24|sample_aspect_ratio=1:1|pict_type=I|coded_picture_number=0|display_picture_number=0|interlaced_frame=0|top_field_first=0|repeat_pict=0|reference=0
+frame|media_type=video|key_frame=1|pkt_pts=4096|pkt_pts_time=0.080000|pkt_dts=4096|pkt_dts_time=0.080000|pkt_duration=2048|pkt_duration_time=0.040000|pkt_pos=760153|pkt_size=N/A|width=100|height=100|pix_fmt=rgb24|sample_aspect_ratio=1:1|pict_type=I|coded_picture_number=0|display_picture_number=0|interlaced_frame=0|top_field_first=0|repeat_pict=0
 packet|codec_type=audio|stream_index=0|pts=4096|pts_time=0.092880|dts=4096|dts_time=0.092880|duration=1024|duration_time=0.023220|convergence_duration=N/A|convergence_duration_time=N/A|size=2048|pos=790158|flags=K
 frame|media_type=audio|key_frame=1|pkt_pts=4096|pkt_pts_time=0.092880|pkt_dts=4096|pkt_dts_time=0.092880|pkt_duration=1024|pkt_duration_time=0.023220|pkt_pos=790158|pkt_size=2048|sample_fmt=s16|nb_samples=1024|channels=1|channel_layout=unknown
 packet|codec_type=audio|stream_index=0|pts=5120|pts_time=0.116100|dts=5120|dts_time=0.116100|duration=1024|duration_time=0.023220|convergence_duration=N/A|convergence_duration_time=N/A|size=2048|pos=792229|flags=K
 frame|media_type=audio|key_frame=1|pkt_pts=5120|pkt_pts_time=0.116100|pkt_dts=5120|pkt_dts_time=0.116100|pkt_duration=1024|pkt_duration_time=0.023220|pkt_pos=792229|pkt_size=2048|sample_fmt=s16|nb_samples=1024|channels=1|channel_layout=unknown
 packet|codec_type=video|stream_index=1|pts=6144|pts_time=0.120000|dts=6144|dts_time=0.120000|duration=2048|duration_time=0.040000|convergence_duration=N/A|convergence_duration_time=N/A|size=230400|pos=794307|flags=K
-frame|media_type=video|key_frame=1|pkt_pts=6144|pkt_pts_time=0.120000|pkt_dts=6144|pkt_dts_time=0.120000|pkt_duration=2048|pkt_duration_time=0.040000|pkt_pos=794307|pkt_size=N/A|width=320|height=240|pix_fmt=rgb24|sample_aspect_ratio=1:1|pict_type=I|coded_picture_number=0|display_picture_number=0|interlaced_frame=0|top_field_first=0|repeat_pict=0|reference=0
+frame|media_type=video|key_frame=1|pkt_pts=6144|pkt_pts_time=0.120000|pkt_dts=6144|pkt_dts_time=0.120000|pkt_duration=2048|pkt_duration_time=0.040000|pkt_pos=794307|pkt_size=N/A|width=320|height=240|pix_fmt=rgb24|sample_aspect_ratio=1:1|pict_type=I|coded_picture_number=0|display_picture_number=0|interlaced_frame=0|top_field_first=0|repeat_pict=0
 packet|codec_type=video|stream_index=2|pts=6144|pts_time=0.120000|dts=6144|dts_time=0.120000|duration=2048|duration_time=0.040000|convergence_duration=N/A|convergence_duration_time=N/A|size=30000|pos=1024731|flags=K
-frame|media_type=video|key_frame=1|pkt_pts=6144|pkt_pts_time=0.120000|pkt_dts=6144|pkt_dts_time=0.120000|pkt_duration=2048|pkt_duration_time=0.040000|pkt_pos=1024731|pkt_size=N/A|width=100|height=100|pix_fmt=rgb24|sample_aspect_ratio=1:1|pict_type=I|coded_picture_number=0|display_picture_number=0|interlaced_frame=0|top_field_first=0|repeat_pict=0|reference=0
+frame|media_type=video|key_frame=1|pkt_pts=6144|pkt_pts_time=0.120000|pkt_dts=6144|pkt_dts_time=0.120000|pkt_duration=2048|pkt_duration_time=0.040000|pkt_pos=1024731|pkt_size=N/A|width=100|height=100|pix_fmt=rgb24|sample_aspect_ratio=1:1|pict_type=I|coded_picture_number=0|display_picture_number=0|interlaced_frame=0|top_field_first=0|repeat_pict=0
 stream|index=0|codec_name=pcm_s16le|profile=unknown|codec_type=audio|codec_time_base=1/44100|codec_tag_string=PSD[16]|codec_tag=0x10445350|sample_fmt=s16|sample_rate=44100|channels=1|bits_per_sample=16|id=N/A|r_frame_rate=0/0|avg_frame_rate=0/0|time_base=1/44100|start_pts=0|start_time=0.000000|duration_ts=527406|duration=11.959320|bit_rate=705600|nb_frames=N/A|nb_read_frames=6|nb_read_packets=6|disposition:default=0|disposition:dub=0|disposition:original=0|disposition:comment=0|disposition:lyrics=0|disposition:karaoke=0|disposition:forced=0|disposition:hearing_impaired=0|disposition:visual_impaired=0|disposition:clean_effects=0|disposition:attached_pic=0|tag:E=mc²
 stream|index=1|codec_name=rawvideo|profile=unknown|codec_type=video|codec_time_base=1/51200|codec_tag_string=RGB[24]|codec_tag=0x18424752|width=320|height=240|has_b_frames=0|sample_aspect_ratio=1:1|display_aspect_ratio=4:3|pix_fmt=rgb24|level=-99|timecode=N/A|id=N/A|r_frame_rate=25/1|avg_frame_rate=25/1|time_base=1/51200|start_pts=0|start_time=0.000000|duration_ts=612317|duration=11.959316|bit_rate=N/A|nb_frames=N/A|nb_read_frames=4|nb_read_packets=4|disposition:default=0|disposition:dub=0|disposition:original=0|disposition:comment=0|disposition:lyrics=0|disposition:karaoke=0|disposition:forced=0|disposition:hearing_impaired=0|disposition:visual_impaired=0|disposition:clean_effects=0|disposition:attached_pic=0|tag:title=foobar|tag:duration_ts=field-and-tags-conflict-attempt
 stream|index=2|codec_name=rawvideo|profile=unknown|codec_type=video|codec_time_base=1/51200|codec_tag_string=RGB[24]|codec_tag=0x18424752|width=100|height=100|has_b_frames=0|sample_aspect_ratio=1:1|display_aspect_ratio=1:1|pix_fmt=rgb24|level=-99|timecode=N/A|id=N/A|r_frame_rate=25/1|avg_frame_rate=25/1|time_base=1/51200|start_pts=0|start_time=0.000000|duration_ts=612317|duration=11.959316|bit_rate=N/A|nb_frames=N/A|nb_read_frames=4|nb_read_packets=4|disposition:default=0|disposition:dub=0|disposition:original=0|disposition:comment=0|disposition:lyrics=0|disposition:karaoke=0|disposition:forced=0|disposition:hearing_impaired=0|disposition:visual_impaired=0|disposition:clean_effects=0|disposition:attached_pic=0
diff --git a/tests/ref/fate/ffprobe_csv b/tests/ref/fate/ffprobe_csv
index 94314d7..95b93b5 100644
--- a/tests/ref/fate/ffprobe_csv
+++ b/tests/ref/fate/ffprobe_csv
@@ -1,31 +1,31 @@
 packet,audio,0,0,0.000000,0,0.000000,1024,0.023220,N/A,N/A,2048,572,K
 frame,audio,1,0,0.000000,0,0.000000,1024,0.023220,572,2048,s16,1024,1,unknown
 packet,video,1,0,0.000000,0,0.000000,2048,0.040000,N/A,N/A,230400,2647,K
-frame,video,1,0,0.000000,0,0.000000,2048,0.040000,2647,N/A,320,240,rgb24,1:1,I,0,0,0,0,0,0
+frame,video,1,0,0.000000,0,0.000000,2048,0.040000,2647,N/A,320,240,rgb24,1:1,I,0,0,0,0,0
 packet,video,2,0,0.000000,0,0.000000,2048,0.040000,N/A,N/A,30000,233068,K
-frame,video,1,0,0.000000,0,0.000000,2048,0.040000,233068,N/A,100,100,rgb24,1:1,I,0,0,0,0,0,0
+frame,video,1,0,0.000000,0,0.000000,2048,0.040000,233068,N/A,100,100,rgb24,1:1,I,0,0,0,0,0
 packet,audio,0,1024,0.023220,1024,0.023220,1024,0.023220,N/A,N/A,2048,263073,K
 frame,audio,1,1024,0.023220,1024,0.023220,1024,0.023220,263073,2048,s16,1024,1,unknown
 packet,video,1,2048,0.040000,2048,0.040000,2048,0.040000,N/A,N/A,230400,265151,K
-frame,video,1,2048,0.040000,2048,0.040000,2048,0.040000,265151,N/A,320,240,rgb24,1:1,I,0,0,0,0,0,0
+frame,video,1,2048,0.040000,2048,0.040000,2048,0.040000,265151,N/A,320,240,rgb24,1:1,I,0,0,0,0,0
 packet,video,2,2048,0.040000,2048,0.040000,2048,0.040000,N/A,N/A,30000,495575,K
-frame,video,1,2048,0.040000,2048,0.040000,2048,0.040000,495575,N/A,100,100,rgb24,1:1,I,0,0,0,0,0,0
+frame,video,1,2048,0.040000,2048,0.040000,2048,0.040000,495575,N/A,100,100,rgb24,1:1,I,0,0,0,0,0
 packet,audio,0,2048,0.046440,2048,0.046440,1024,0.023220,N/A,N/A,2048,525580,K
 frame,audio,1,2048,0.046440,2048,0.046440,1024,0.023220,525580,2048,s16,1024,1,unknown
 packet,audio,0,3072,0.069660,3072,0.069660,1024,0.023220,N/A,N/A,2048,527651,K
 frame,audio,1,3072,0.069660,3072,0.069660,1024,0.023220,527651,2048,s16,1024,1,unknown
 packet,video,1,4096,0.080000,4096,0.080000,2048,0.040000,N/A,N/A,230400,529729,K
-frame,video,1,4096,0.080000,4096,0.080000,2048,0.040000,529729,N/A,320,240,rgb24,1:1,I,0,0,0,0,0,0
+frame,video,1,4096,0.080000,4096,0.080000,2048,0.040000,529729,N/A,320,240,rgb24,1:1,I,0,0,0,0,0
 packet,video,2,4096,0.080000,4096,0.080000,2048,0.040000,N/A,N/A,30000,760153,K
-frame,video,1,4096,0.080000,4096,0.080000,2048,0.040000,760153,N/A,100,100,rgb24,1:1,I,0,0,0,0,0,0
+frame,video,1,4096,0.080000,4096,0.080000,2048,0.040000,760153,N/A,100,100,rgb24,1:1,I,0,0,0,0,0
 packet,audio,0,4096,0.092880,4096,0.092880,1024,0.023220,N/A,N/A,2048,790158,K
 frame,audio,1,4096,0.092880,4096,0.092880,1024,0.023220,790158,2048,s16,1024,1,unknown
 packet,audio,0,5120,0.116100,5120,0.116100,1024,0.023220,N/A,N/A,2048,792229,K
 frame,audio,1,5120,0.116100,5120,0.116100,1024,0.023220,792229,2048,s16,1024,1,unknown
 packet,video,1,6144,0.120000,6144,0.120000,2048,0.040000,N/A,N/A,230400,794307,K
-frame,video,1,6144,0.120000,6144,0.120000,2048,0.040000,794307,N/A,320,240,rgb24,1:1,I,0,0,0,0,0,0
+frame,video,1,6144,0.120000,6144,0.120000,2048,0.040000,794307,N/A,320,240,rgb24,1:1,I,0,0,0,0,0
 packet,video,2,6144,0.120000,6144,0.120000,2048,0.040000,N/A,N/A,30000,1024731,K
-frame,video,1,6144,0.120000,6144,0.120000,2048,0.040000,1024731,N/A,100,100,rgb24,1:1,I,0,0,0,0,0,0
+frame,video,1,6144,0.120000,6144,0.120000,2048,0.040000,1024731,N/A,100,100,rgb24,1:1,I,0,0,0,0,0
 stream,0,pcm_s16le,unknown,audio,1/44100,PSD[16],0x10445350,s16,44100,1,16,N/A,0/0,0/0,1/44100,0,0.000000,527406,11.959320,705600,N/A,6,6,0,0,0,0,0,0,0,0,0,0,0,mc²
 stream,1,rawvideo,unknown,video,1/51200,RGB[24],0x18424752,320,240,0,1:1,4:3,rgb24,-99,N/A,N/A,25/1,25/1,1/51200,0,0.000000,612317,11.959316,N/A,N/A,4,4,0,0,0,0,0,0,0,0,0,0,0,foobar,field-and-tags-conflict-attempt
 stream,2,rawvideo,unknown,video,1/51200,RGB[24],0x18424752,100,100,0,1:1,1:1,rgb24,-99,N/A,N/A,25/1,25/1,1/51200,0,0.000000,612317,11.959316,N/A,N/A,4,4,0,0,0,0,0,0,0,0,0,0,0
diff --git a/tests/ref/fate/ffprobe_default b/tests/ref/fate/ffprobe_default
index 4105293..6f6341d 100644
--- a/tests/ref/fate/ffprobe_default
+++ b/tests/ref/fate/ffprobe_default
@@ -65,7 +65,6 @@
 interlaced_frame=0
 top_field_first=0
 repeat_pict=0
-reference=0
 [/FRAME]
 [PACKET]
 codec_type=video
@@ -103,7 +102,6 @@
 interlaced_frame=0
 top_field_first=0
 repeat_pict=0
-reference=0
 [/FRAME]
 [PACKET]
 codec_type=audio
@@ -172,7 +170,6 @@
 interlaced_frame=0
 top_field_first=0
 repeat_pict=0
-reference=0
 [/FRAME]
 [PACKET]
 codec_type=video
@@ -210,7 +207,6 @@
 interlaced_frame=0
 top_field_first=0
 repeat_pict=0
-reference=0
 [/FRAME]
 [PACKET]
 codec_type=audio
@@ -310,7 +306,6 @@
 interlaced_frame=0
 top_field_first=0
 repeat_pict=0
-reference=0
 [/FRAME]
 [PACKET]
 codec_type=video
@@ -348,7 +343,6 @@
 interlaced_frame=0
 top_field_first=0
 repeat_pict=0
-reference=0
 [/FRAME]
 [PACKET]
 codec_type=audio
@@ -448,7 +442,6 @@
 interlaced_frame=0
 top_field_first=0
 repeat_pict=0
-reference=0
 [/FRAME]
 [PACKET]
 codec_type=video
@@ -486,7 +479,6 @@
 interlaced_frame=0
 top_field_first=0
 repeat_pict=0
-reference=0
 [/FRAME]
 [STREAM]
 index=0
diff --git a/tests/ref/fate/ffprobe_flat b/tests/ref/fate/ffprobe_flat
index 156f215..324b94f 100644
--- a/tests/ref/fate/ffprobe_flat
+++ b/tests/ref/fate/ffprobe_flat
@@ -58,7 +58,6 @@
 packets_and_frames.frame.1.interlaced_frame=0
 packets_and_frames.frame.1.top_field_first=0
 packets_and_frames.frame.1.repeat_pict=0
-packets_and_frames.frame.1.reference=0
 packets_and_frames.packet.2.codec_type="video"
 packets_and_frames.packet.2.stream_index=2
 packets_and_frames.packet.2.pts=0
@@ -92,7 +91,6 @@
 packets_and_frames.frame.2.interlaced_frame=0
 packets_and_frames.frame.2.top_field_first=0
 packets_and_frames.frame.2.repeat_pict=0
-packets_and_frames.frame.2.reference=0
 packets_and_frames.packet.3.codec_type="audio"
 packets_and_frames.packet.3.stream_index=0
 packets_and_frames.packet.3.pts=1024
@@ -153,7 +151,6 @@
 packets_and_frames.frame.4.interlaced_frame=0
 packets_and_frames.frame.4.top_field_first=0
 packets_and_frames.frame.4.repeat_pict=0
-packets_and_frames.frame.4.reference=0
 packets_and_frames.packet.5.codec_type="video"
 packets_and_frames.packet.5.stream_index=2
 packets_and_frames.packet.5.pts=2048
@@ -187,7 +184,6 @@
 packets_and_frames.frame.5.interlaced_frame=0
 packets_and_frames.frame.5.top_field_first=0
 packets_and_frames.frame.5.repeat_pict=0
-packets_and_frames.frame.5.reference=0
 packets_and_frames.packet.6.codec_type="audio"
 packets_and_frames.packet.6.stream_index=0
 packets_and_frames.packet.6.pts=2048
@@ -275,7 +271,6 @@
 packets_and_frames.frame.8.interlaced_frame=0
 packets_and_frames.frame.8.top_field_first=0
 packets_and_frames.frame.8.repeat_pict=0
-packets_and_frames.frame.8.reference=0
 packets_and_frames.packet.9.codec_type="video"
 packets_and_frames.packet.9.stream_index=2
 packets_and_frames.packet.9.pts=4096
@@ -309,7 +304,6 @@
 packets_and_frames.frame.9.interlaced_frame=0
 packets_and_frames.frame.9.top_field_first=0
 packets_and_frames.frame.9.repeat_pict=0
-packets_and_frames.frame.9.reference=0
 packets_and_frames.packet.10.codec_type="audio"
 packets_and_frames.packet.10.stream_index=0
 packets_and_frames.packet.10.pts=4096
@@ -397,7 +391,6 @@
 packets_and_frames.frame.12.interlaced_frame=0
 packets_and_frames.frame.12.top_field_first=0
 packets_and_frames.frame.12.repeat_pict=0
-packets_and_frames.frame.12.reference=0
 packets_and_frames.packet.13.codec_type="video"
 packets_and_frames.packet.13.stream_index=2
 packets_and_frames.packet.13.pts=6144
@@ -431,7 +424,6 @@
 packets_and_frames.frame.13.interlaced_frame=0
 packets_and_frames.frame.13.top_field_first=0
 packets_and_frames.frame.13.repeat_pict=0
-packets_and_frames.frame.13.reference=0
 streams.stream.0.index=0
 streams.stream.0.codec_name="pcm_s16le"
 streams.stream.0.profile="unknown"
diff --git a/tests/ref/fate/ffprobe_ini b/tests/ref/fate/ffprobe_ini
index 0e324b6..63a03f6 100644
--- a/tests/ref/fate/ffprobe_ini
+++ b/tests/ref/fate/ffprobe_ini
@@ -67,7 +67,6 @@
 interlaced_frame=0
 top_field_first=0
 repeat_pict=0
-reference=0
 
 [packets_and_frames.packet.2]
 codec_type=video
@@ -105,7 +104,6 @@
 interlaced_frame=0
 top_field_first=0
 repeat_pict=0
-reference=0
 
 [packets_and_frames.packet.3]
 codec_type=audio
@@ -174,7 +172,6 @@
 interlaced_frame=0
 top_field_first=0
 repeat_pict=0
-reference=0
 
 [packets_and_frames.packet.5]
 codec_type=video
@@ -212,7 +209,6 @@
 interlaced_frame=0
 top_field_first=0
 repeat_pict=0
-reference=0
 
 [packets_and_frames.packet.6]
 codec_type=audio
@@ -312,7 +308,6 @@
 interlaced_frame=0
 top_field_first=0
 repeat_pict=0
-reference=0
 
 [packets_and_frames.packet.9]
 codec_type=video
@@ -350,7 +345,6 @@
 interlaced_frame=0
 top_field_first=0
 repeat_pict=0
-reference=0
 
 [packets_and_frames.packet.10]
 codec_type=audio
@@ -450,7 +444,6 @@
 interlaced_frame=0
 top_field_first=0
 repeat_pict=0
-reference=0
 
 [packets_and_frames.packet.13]
 codec_type=video
@@ -488,7 +481,6 @@
 interlaced_frame=0
 top_field_first=0
 repeat_pict=0
-reference=0
 
 [streams.stream.0]
 index=0
diff --git a/tests/ref/fate/ffprobe_json b/tests/ref/fate/ffprobe_json
index 1864deb..51404cb 100644
--- a/tests/ref/fate/ffprobe_json
+++ b/tests/ref/fate/ffprobe_json
@@ -64,8 +64,7 @@
             "display_picture_number": 0,
             "interlaced_frame": 0,
             "top_field_first": 0,
-            "repeat_pict": 0,
-            "reference": 0
+            "repeat_pict": 0
         },
         {
             "type": "packet",
@@ -101,8 +100,7 @@
             "display_picture_number": 0,
             "interlaced_frame": 0,
             "top_field_first": 0,
-            "repeat_pict": 0,
-            "reference": 0
+            "repeat_pict": 0
         },
         {
             "type": "packet",
@@ -168,8 +166,7 @@
             "display_picture_number": 0,
             "interlaced_frame": 0,
             "top_field_first": 0,
-            "repeat_pict": 0,
-            "reference": 0
+            "repeat_pict": 0
         },
         {
             "type": "packet",
@@ -205,8 +202,7 @@
             "display_picture_number": 0,
             "interlaced_frame": 0,
             "top_field_first": 0,
-            "repeat_pict": 0,
-            "reference": 0
+            "repeat_pict": 0
         },
         {
             "type": "packet",
@@ -302,8 +298,7 @@
             "display_picture_number": 0,
             "interlaced_frame": 0,
             "top_field_first": 0,
-            "repeat_pict": 0,
-            "reference": 0
+            "repeat_pict": 0
         },
         {
             "type": "packet",
@@ -339,8 +334,7 @@
             "display_picture_number": 0,
             "interlaced_frame": 0,
             "top_field_first": 0,
-            "repeat_pict": 0,
-            "reference": 0
+            "repeat_pict": 0
         },
         {
             "type": "packet",
@@ -436,8 +430,7 @@
             "display_picture_number": 0,
             "interlaced_frame": 0,
             "top_field_first": 0,
-            "repeat_pict": 0,
-            "reference": 0
+            "repeat_pict": 0
         },
         {
             "type": "packet",
@@ -473,8 +466,7 @@
             "display_picture_number": 0,
             "interlaced_frame": 0,
             "top_field_first": 0,
-            "repeat_pict": 0,
-            "reference": 0
+            "repeat_pict": 0
         }
     ],
     "streams": [
diff --git a/tests/ref/fate/ffprobe_xml b/tests/ref/fate/ffprobe_xml
index 8d38202..0600ad7 100644
--- a/tests/ref/fate/ffprobe_xml
+++ b/tests/ref/fate/ffprobe_xml
@@ -4,31 +4,31 @@
         <packet codec_type="audio" stream_index="0" pts="0" pts_time="0.000000" dts="0" dts_time="0.000000" duration="1024" duration_time="0.023220" size="2048" pos="572" flags="K"/>
         <frame media_type="audio" key_frame="1" pkt_pts="0" pkt_pts_time="0.000000" pkt_dts="0" pkt_dts_time="0.000000" pkt_duration="1024" pkt_duration_time="0.023220" pkt_pos="572" pkt_size="2048" sample_fmt="s16" nb_samples="1024" channels="1"/>
         <packet codec_type="video" stream_index="1" pts="0" pts_time="0.000000" dts="0" dts_time="0.000000" duration="2048" duration_time="0.040000" size="230400" pos="2647" flags="K"/>
-        <frame media_type="video" key_frame="1" pkt_pts="0" pkt_pts_time="0.000000" pkt_dts="0" pkt_dts_time="0.000000" pkt_duration="2048" pkt_duration_time="0.040000" pkt_pos="2647" width="320" height="240" pix_fmt="rgb24" sample_aspect_ratio="1:1" pict_type="I" coded_picture_number="0" display_picture_number="0" interlaced_frame="0" top_field_first="0" repeat_pict="0" reference="0"/>
+        <frame media_type="video" key_frame="1" pkt_pts="0" pkt_pts_time="0.000000" pkt_dts="0" pkt_dts_time="0.000000" pkt_duration="2048" pkt_duration_time="0.040000" pkt_pos="2647" width="320" height="240" pix_fmt="rgb24" sample_aspect_ratio="1:1" pict_type="I" coded_picture_number="0" display_picture_number="0" interlaced_frame="0" top_field_first="0" repeat_pict="0"/>
         <packet codec_type="video" stream_index="2" pts="0" pts_time="0.000000" dts="0" dts_time="0.000000" duration="2048" duration_time="0.040000" size="30000" pos="233068" flags="K"/>
-        <frame media_type="video" key_frame="1" pkt_pts="0" pkt_pts_time="0.000000" pkt_dts="0" pkt_dts_time="0.000000" pkt_duration="2048" pkt_duration_time="0.040000" pkt_pos="233068" width="100" height="100" pix_fmt="rgb24" sample_aspect_ratio="1:1" pict_type="I" coded_picture_number="0" display_picture_number="0" interlaced_frame="0" top_field_first="0" repeat_pict="0" reference="0"/>
+        <frame media_type="video" key_frame="1" pkt_pts="0" pkt_pts_time="0.000000" pkt_dts="0" pkt_dts_time="0.000000" pkt_duration="2048" pkt_duration_time="0.040000" pkt_pos="233068" width="100" height="100" pix_fmt="rgb24" sample_aspect_ratio="1:1" pict_type="I" coded_picture_number="0" display_picture_number="0" interlaced_frame="0" top_field_first="0" repeat_pict="0"/>
         <packet codec_type="audio" stream_index="0" pts="1024" pts_time="0.023220" dts="1024" dts_time="0.023220" duration="1024" duration_time="0.023220" size="2048" pos="263073" flags="K"/>
         <frame media_type="audio" key_frame="1" pkt_pts="1024" pkt_pts_time="0.023220" pkt_dts="1024" pkt_dts_time="0.023220" pkt_duration="1024" pkt_duration_time="0.023220" pkt_pos="263073" pkt_size="2048" sample_fmt="s16" nb_samples="1024" channels="1"/>
         <packet codec_type="video" stream_index="1" pts="2048" pts_time="0.040000" dts="2048" dts_time="0.040000" duration="2048" duration_time="0.040000" size="230400" pos="265151" flags="K"/>
-        <frame media_type="video" key_frame="1" pkt_pts="2048" pkt_pts_time="0.040000" pkt_dts="2048" pkt_dts_time="0.040000" pkt_duration="2048" pkt_duration_time="0.040000" pkt_pos="265151" width="320" height="240" pix_fmt="rgb24" sample_aspect_ratio="1:1" pict_type="I" coded_picture_number="0" display_picture_number="0" interlaced_frame="0" top_field_first="0" repeat_pict="0" reference="0"/>
+        <frame media_type="video" key_frame="1" pkt_pts="2048" pkt_pts_time="0.040000" pkt_dts="2048" pkt_dts_time="0.040000" pkt_duration="2048" pkt_duration_time="0.040000" pkt_pos="265151" width="320" height="240" pix_fmt="rgb24" sample_aspect_ratio="1:1" pict_type="I" coded_picture_number="0" display_picture_number="0" interlaced_frame="0" top_field_first="0" repeat_pict="0"/>
         <packet codec_type="video" stream_index="2" pts="2048" pts_time="0.040000" dts="2048" dts_time="0.040000" duration="2048" duration_time="0.040000" size="30000" pos="495575" flags="K"/>
-        <frame media_type="video" key_frame="1" pkt_pts="2048" pkt_pts_time="0.040000" pkt_dts="2048" pkt_dts_time="0.040000" pkt_duration="2048" pkt_duration_time="0.040000" pkt_pos="495575" width="100" height="100" pix_fmt="rgb24" sample_aspect_ratio="1:1" pict_type="I" coded_picture_number="0" display_picture_number="0" interlaced_frame="0" top_field_first="0" repeat_pict="0" reference="0"/>
+        <frame media_type="video" key_frame="1" pkt_pts="2048" pkt_pts_time="0.040000" pkt_dts="2048" pkt_dts_time="0.040000" pkt_duration="2048" pkt_duration_time="0.040000" pkt_pos="495575" width="100" height="100" pix_fmt="rgb24" sample_aspect_ratio="1:1" pict_type="I" coded_picture_number="0" display_picture_number="0" interlaced_frame="0" top_field_first="0" repeat_pict="0"/>
         <packet codec_type="audio" stream_index="0" pts="2048" pts_time="0.046440" dts="2048" dts_time="0.046440" duration="1024" duration_time="0.023220" size="2048" pos="525580" flags="K"/>
         <frame media_type="audio" key_frame="1" pkt_pts="2048" pkt_pts_time="0.046440" pkt_dts="2048" pkt_dts_time="0.046440" pkt_duration="1024" pkt_duration_time="0.023220" pkt_pos="525580" pkt_size="2048" sample_fmt="s16" nb_samples="1024" channels="1"/>
         <packet codec_type="audio" stream_index="0" pts="3072" pts_time="0.069660" dts="3072" dts_time="0.069660" duration="1024" duration_time="0.023220" size="2048" pos="527651" flags="K"/>
         <frame media_type="audio" key_frame="1" pkt_pts="3072" pkt_pts_time="0.069660" pkt_dts="3072" pkt_dts_time="0.069660" pkt_duration="1024" pkt_duration_time="0.023220" pkt_pos="527651" pkt_size="2048" sample_fmt="s16" nb_samples="1024" channels="1"/>
         <packet codec_type="video" stream_index="1" pts="4096" pts_time="0.080000" dts="4096" dts_time="0.080000" duration="2048" duration_time="0.040000" size="230400" pos="529729" flags="K"/>
-        <frame media_type="video" key_frame="1" pkt_pts="4096" pkt_pts_time="0.080000" pkt_dts="4096" pkt_dts_time="0.080000" pkt_duration="2048" pkt_duration_time="0.040000" pkt_pos="529729" width="320" height="240" pix_fmt="rgb24" sample_aspect_ratio="1:1" pict_type="I" coded_picture_number="0" display_picture_number="0" interlaced_frame="0" top_field_first="0" repeat_pict="0" reference="0"/>
+        <frame media_type="video" key_frame="1" pkt_pts="4096" pkt_pts_time="0.080000" pkt_dts="4096" pkt_dts_time="0.080000" pkt_duration="2048" pkt_duration_time="0.040000" pkt_pos="529729" width="320" height="240" pix_fmt="rgb24" sample_aspect_ratio="1:1" pict_type="I" coded_picture_number="0" display_picture_number="0" interlaced_frame="0" top_field_first="0" repeat_pict="0"/>
         <packet codec_type="video" stream_index="2" pts="4096" pts_time="0.080000" dts="4096" dts_time="0.080000" duration="2048" duration_time="0.040000" size="30000" pos="760153" flags="K"/>
-        <frame media_type="video" key_frame="1" pkt_pts="4096" pkt_pts_time="0.080000" pkt_dts="4096" pkt_dts_time="0.080000" pkt_duration="2048" pkt_duration_time="0.040000" pkt_pos="760153" width="100" height="100" pix_fmt="rgb24" sample_aspect_ratio="1:1" pict_type="I" coded_picture_number="0" display_picture_number="0" interlaced_frame="0" top_field_first="0" repeat_pict="0" reference="0"/>
+        <frame media_type="video" key_frame="1" pkt_pts="4096" pkt_pts_time="0.080000" pkt_dts="4096" pkt_dts_time="0.080000" pkt_duration="2048" pkt_duration_time="0.040000" pkt_pos="760153" width="100" height="100" pix_fmt="rgb24" sample_aspect_ratio="1:1" pict_type="I" coded_picture_number="0" display_picture_number="0" interlaced_frame="0" top_field_first="0" repeat_pict="0"/>
         <packet codec_type="audio" stream_index="0" pts="4096" pts_time="0.092880" dts="4096" dts_time="0.092880" duration="1024" duration_time="0.023220" size="2048" pos="790158" flags="K"/>
         <frame media_type="audio" key_frame="1" pkt_pts="4096" pkt_pts_time="0.092880" pkt_dts="4096" pkt_dts_time="0.092880" pkt_duration="1024" pkt_duration_time="0.023220" pkt_pos="790158" pkt_size="2048" sample_fmt="s16" nb_samples="1024" channels="1"/>
         <packet codec_type="audio" stream_index="0" pts="5120" pts_time="0.116100" dts="5120" dts_time="0.116100" duration="1024" duration_time="0.023220" size="2048" pos="792229" flags="K"/>
         <frame media_type="audio" key_frame="1" pkt_pts="5120" pkt_pts_time="0.116100" pkt_dts="5120" pkt_dts_time="0.116100" pkt_duration="1024" pkt_duration_time="0.023220" pkt_pos="792229" pkt_size="2048" sample_fmt="s16" nb_samples="1024" channels="1"/>
         <packet codec_type="video" stream_index="1" pts="6144" pts_time="0.120000" dts="6144" dts_time="0.120000" duration="2048" duration_time="0.040000" size="230400" pos="794307" flags="K"/>
-        <frame media_type="video" key_frame="1" pkt_pts="6144" pkt_pts_time="0.120000" pkt_dts="6144" pkt_dts_time="0.120000" pkt_duration="2048" pkt_duration_time="0.040000" pkt_pos="794307" width="320" height="240" pix_fmt="rgb24" sample_aspect_ratio="1:1" pict_type="I" coded_picture_number="0" display_picture_number="0" interlaced_frame="0" top_field_first="0" repeat_pict="0" reference="0"/>
+        <frame media_type="video" key_frame="1" pkt_pts="6144" pkt_pts_time="0.120000" pkt_dts="6144" pkt_dts_time="0.120000" pkt_duration="2048" pkt_duration_time="0.040000" pkt_pos="794307" width="320" height="240" pix_fmt="rgb24" sample_aspect_ratio="1:1" pict_type="I" coded_picture_number="0" display_picture_number="0" interlaced_frame="0" top_field_first="0" repeat_pict="0"/>
         <packet codec_type="video" stream_index="2" pts="6144" pts_time="0.120000" dts="6144" dts_time="0.120000" duration="2048" duration_time="0.040000" size="30000" pos="1024731" flags="K"/>
-        <frame media_type="video" key_frame="1" pkt_pts="6144" pkt_pts_time="0.120000" pkt_dts="6144" pkt_dts_time="0.120000" pkt_duration="2048" pkt_duration_time="0.040000" pkt_pos="1024731" width="100" height="100" pix_fmt="rgb24" sample_aspect_ratio="1:1" pict_type="I" coded_picture_number="0" display_picture_number="0" interlaced_frame="0" top_field_first="0" repeat_pict="0" reference="0"/>
+        <frame media_type="video" key_frame="1" pkt_pts="6144" pkt_pts_time="0.120000" pkt_dts="6144" pkt_dts_time="0.120000" pkt_duration="2048" pkt_duration_time="0.040000" pkt_pos="1024731" width="100" height="100" pix_fmt="rgb24" sample_aspect_ratio="1:1" pict_type="I" coded_picture_number="0" display_picture_number="0" interlaced_frame="0" top_field_first="0" repeat_pict="0"/>
     </packets_and_frames>
 
     <streams>
diff --git a/tests/ref/fate/filter-boxblur b/tests/ref/fate/filter-boxblur
new file mode 100644
index 0000000..acb2beb
--- /dev/null
+++ b/tests/ref/fate/filter-boxblur
@@ -0,0 +1,51 @@
+#tb 0: 1/25
+0,          0,          0,        1,   152064, 0x113489f8
+0,          1,          1,        1,   152064, 0x650a64ca
+0,          2,          2,        1,   152064, 0x29ecf6b1
+0,          3,          3,        1,   152064, 0x5cb98169
+0,          4,          4,        1,   152064, 0x4f46b6d7
+0,          5,          5,        1,   152064, 0x793ba919
+0,          6,          6,        1,   152064, 0x6caf7c14
+0,          7,          7,        1,   152064, 0xca638b2e
+0,          8,          8,        1,   152064, 0xf6df803d
+0,          9,          9,        1,   152064, 0x907b386f
+0,         10,         10,        1,   152064, 0xbb8d47f9
+0,         11,         11,        1,   152064, 0x17befd28
+0,         12,         12,        1,   152064, 0xf29eae1e
+0,         13,         13,        1,   152064, 0xb08ba1e6
+0,         14,         14,        1,   152064, 0x39948df7
+0,         15,         15,        1,   152064, 0x841d0e7e
+0,         16,         16,        1,   152064, 0x21d64dd6
+0,         17,         17,        1,   152064, 0x18af38e9
+0,         18,         18,        1,   152064, 0x1f946a26
+0,         19,         19,        1,   152064, 0x0d9cdc4e
+0,         20,         20,        1,   152064, 0xaf2af54d
+0,         21,         21,        1,   152064, 0x09e724e1
+0,         22,         22,        1,   152064, 0xff7d1e4b
+0,         23,         23,        1,   152064, 0xd49d68ad
+0,         24,         24,        1,   152064, 0x7ea4f96b
+0,         25,         25,        1,   152064, 0xd2f698b9
+0,         26,         26,        1,   152064, 0xc2a696ac
+0,         27,         27,        1,   152064, 0x1e8ed7b4
+0,         28,         28,        1,   152064, 0x980ba498
+0,         29,         29,        1,   152064, 0x47c364f2
+0,         30,         30,        1,   152064, 0x1a196ada
+0,         31,         31,        1,   152064, 0x4847c564
+0,         32,         32,        1,   152064, 0xa959fc15
+0,         33,         33,        1,   152064, 0x7d1a79d3
+0,         34,         34,        1,   152064, 0xae7e438d
+0,         35,         35,        1,   152064, 0xc32794b4
+0,         36,         36,        1,   152064, 0x6ef43744
+0,         37,         37,        1,   152064, 0xa06a01d0
+0,         38,         38,        1,   152064, 0xc94b5847
+0,         39,         39,        1,   152064, 0xb7514f04
+0,         40,         40,        1,   152064, 0xf0c959e8
+0,         41,         41,        1,   152064, 0xe6439e5e
+0,         42,         42,        1,   152064, 0xd347bf8d
+0,         43,         43,        1,   152064, 0x40822107
+0,         44,         44,        1,   152064, 0xe0ad044a
+0,         45,         45,        1,   152064, 0x05f77e73
+0,         46,         46,        1,   152064, 0xbeaa536d
+0,         47,         47,        1,   152064, 0xb0edc576
+0,         48,         48,        1,   152064, 0x35fcb4fa
+0,         49,         49,        1,   152064, 0x8d9ad8fa
diff --git a/tests/ref/fate/filter-concat b/tests/ref/fate/filter-concat
new file mode 100644
index 0000000..73e4185
--- /dev/null
+++ b/tests/ref/fate/filter-concat
@@ -0,0 +1,202 @@
+#tb 0: 1/5
+#tb 1: 1/44100
+0,          0,          0,        1,   230400, 0x88c4d19a
+1,          0,          0,     1024,     2048, 0xb3f10192
+1,       1024,       1024,     1024,     2048, 0xb340fe4e
+1,       2048,       2048,     1024,     2048, 0x0a5f0111
+1,       3072,       3072,     1024,     2048, 0x51be06b8
+1,       4096,       4096,     1024,     2048, 0x71a1ffcb
+1,       5120,       5120,     1024,     2048, 0x7f64f50f
+1,       6144,       6144,     1024,     2048, 0x70a8fa17
+1,       7168,       7168,     1024,     2048, 0x0dad072a
+1,       8192,       8192,     1024,     2048, 0x5e810c51
+0,          1,          1,        1,   230400, 0x0d77c977
+1,       9216,       9216,     1024,     2048, 0xbe5bf462
+1,      10240,      10240,     1024,     2048, 0xbcd9faeb
+1,      11264,      11264,     1024,     2048, 0x0d5bfe9c
+1,      12288,      12288,     1024,     2048, 0x97d80297
+1,      13312,      13312,     1024,     2048, 0xba0f0894
+1,      14336,      14336,     1024,     2048, 0xcc22f291
+1,      15360,      15360,     1024,     2048, 0x11a9fa03
+1,      16384,      16384,     1024,     2048, 0x9a920378
+1,      17408,      17408,     1024,     2048, 0x901b0525
+0,          2,          2,        1,   230400, 0x242629d7
+1,      18432,      18432,     1024,     2048, 0x74b2003f
+1,      19456,      19456,     1024,     2048, 0xa20ef3ed
+1,      20480,      20480,     1024,     2048, 0x44cef9de
+1,      21504,      21504,     1024,     2048, 0x4b2e039b
+1,      22528,      22528,     1024,     2048, 0x198509a1
+1,      23552,      23552,     1024,     2048, 0xcab6f9e5
+1,      24576,      24576,     1024,     2048, 0x67f8f608
+1,      25600,      25600,     1024,     2048, 0x8d7f03fa
+0,          3,          3,        1,   230400, 0x62cdc018
+1,      26624,      26624,     1024,     2048, 0x3e1e0566
+1,      27648,      27648,     1024,     2048, 0x2cfe0308
+1,      28672,      28672,     1024,     2048, 0x1ceaf702
+1,      29696,      29696,     1024,     2048, 0x38a9f3d1
+1,      30720,      30720,     1024,     2048, 0x6c3306b7
+1,      31744,      31744,     1024,     2048, 0x600f0579
+1,      32768,      32768,     1024,     2048, 0x3e5afa28
+1,      33792,      33792,     1024,     2048, 0x053ff47a
+1,      34816,      34816,     1024,     2048, 0x0d28fed9
+0,          4,          4,        1,   230400, 0x248ad058
+1,      35840,      35840,     1024,     2048, 0x279805cc
+1,      36864,      36864,     1024,     2048, 0xb16a0a12
+1,      37888,      37888,     1024,     2048, 0xb45af340
+1,      38912,      38912,     1024,     2048, 0x1834f972
+1,      39936,      39936,     1024,     2048, 0xb5d206ae
+1,      40960,      40960,     1024,     2048, 0xc5760375
+1,      41984,      41984,     1024,     2048, 0x503800ce
+1,      43008,      43008,     1024,     2048, 0xa3bbf4af
+1,      44032,      44032,       68,      136, 0xc8d751c7
+0,          5,          5,        1,   230400, 0x223d134f
+1,      44100,      44100,     9600,    19200, 0x00000000
+0,          6,          6,        1,   230400, 0xbf1c3d34
+1,      53700,      53700,     9600,    19200, 0x00000000
+0,          7,          7,        1,   230400, 0xae0efe96
+1,      63300,      63300,     9600,    19200, 0x00000000
+0,          8,          8,        1,   230400, 0x0cd624d1
+1,      72900,      72900,     9600,    19200, 0x00000000
+0,          9,          9,        1,   230400, 0x6dedf2c0
+1,      82500,      82500,     5700,    11400, 0x00000000
+0,         10,         10,        1,   230400, 0x88c4d19a
+1,      88200,      88200,     1024,     2048, 0x283efb3a
+1,      89224,      89224,     1024,     2048, 0x7692fb8f
+1,      90248,      90248,     1024,     2048, 0xbaaafcc0
+1,      91272,      91272,     1024,     2048, 0xadc8017e
+1,      92296,      92296,     1024,     2048, 0x4f4dffdc
+1,      93320,      93320,     1024,     2048, 0x7ffbff48
+1,      94344,      94344,     1024,     2048, 0x2f990719
+1,      95368,      95368,     1024,     2048, 0xe2caf65c
+1,      96392,      96392,     1024,     2048, 0x825208e4
+0,         11,         11,        1,   230400, 0x0d77c977
+1,      97416,      97416,     1024,     2048, 0xf563f13b
+1,      98440,      98440,     1024,     2048, 0x855d03e9
+1,      99464,      99464,     1024,     2048, 0x0ba9fa4b
+1,     100488,     100488,     1024,     2048, 0x83e1fb92
+1,     101512,     101512,     1024,     2048, 0x1162f965
+1,     102536,     102536,     1024,     2048, 0x0cfef73d
+1,     103560,     103560,     1024,     2048, 0x5688ff75
+1,     104584,     104584,     1024,     2048, 0xf6c0ede9
+1,     105608,     105608,     1024,     2048, 0xfdb20602
+0,         12,         12,        1,   230400, 0x242629d7
+1,     106632,     106632,     1024,     2048, 0x40c5f17b
+1,     107656,     107656,     1024,     2048, 0x559600b1
+1,     108680,     108680,     1024,     2048, 0xccc3f930
+1,     109704,     109704,     1024,     2048, 0xdc800045
+1,     110728,     110728,     1024,     2048, 0xdce4fb3e
+1,     111752,     111752,     1024,     2048, 0x1e5efba9
+1,     112776,     112776,     1024,     2048, 0x8c2e0832
+1,     113800,     113800,     1024,     2048, 0x5c42f66d
+0,         13,         13,        1,   230400, 0x62cdc018
+1,     114824,     114824,     1024,     2048, 0x08e20b1e
+1,     115848,     115848,     1024,     2048, 0x4cf7f903
+1,     116872,     116872,     1024,     2048, 0xe6b90794
+1,     117896,     117896,     1024,     2048, 0x5956f8e6
+1,     118920,     118920,     1024,     2048, 0x6632ff16
+1,     119944,     119944,     1024,     2048, 0x46c8fe11
+1,     120968,     120968,     1024,     2048, 0x7431f732
+1,     121992,     121992,     1024,     2048, 0xa258049f
+1,     123016,     123016,     1024,     2048, 0xdb71f00e
+0,         14,         14,        1,   230400, 0x248ad058
+1,     124040,     124040,     1024,     2048, 0xa89b0359
+1,     125064,     125064,     1024,     2048, 0xe0aff0f2
+1,     126088,     126088,     1024,     2048, 0xc33e0085
+1,     127112,     127112,     1024,     2048, 0x9d09f379
+1,     128136,     128136,     1024,     2048, 0x8c78fd06
+1,     129160,     129160,     1024,     2048, 0x532bfbdd
+1,     130184,     130184,     1024,     2048, 0xfc36f5cd
+1,     131208,     131208,     1024,     2048, 0x2e8f0699
+1,     132232,     132232,     1024,     2048, 0x52382578
+1,     133256,     133256,     1024,     2048, 0x97ed1a28
+1,     134280,     134280,     1024,     2048, 0xabcdf73f
+1,     135304,     135304,     1024,     2048, 0x3a24082c
+1,     136328,     136328,     1024,     2048, 0xbe1cfc3d
+1,     137352,     137352,     1024,     2048, 0xad5800a5
+1,     138376,     138376,     1024,     2048, 0x90b80522
+1,     139400,     139400,     1024,     2048, 0x1fa1f912
+1,     140424,     140424,     1024,     2048, 0x733a0878
+1,     141448,     141448,     1024,     2048, 0x9a3eee47
+1,     142472,     142472,     1024,     2048, 0x5d900759
+1,     143496,     143496,     1024,     2048, 0x1287f540
+1,     144520,     144520,     1024,     2048, 0x941cfe5d
+1,     145544,     145544,     1024,     2048, 0x1587f8a9
+1,     146568,     146568,     1024,     2048, 0xb9e7f888
+1,     147592,     147592,     1024,     2048, 0xe9defbe2
+1,     148616,     148616,     1024,     2048, 0x3a5ef312
+1,     149640,     149640,     1024,     2048, 0xdcbe0544
+1,     150664,     150664,     1024,     2048, 0xbe51ecc5
+1,     151688,     151688,     1024,     2048, 0x21a60721
+1,     152712,     152712,     1024,     2048, 0xf29ff318
+1,     153736,     153736,     1024,     2048, 0xcd4c02ea
+1,     154760,     154760,     1024,     2048, 0xa424faac
+1,     155784,     155784,     1024,     2048, 0xbaedfdab
+1,     156808,     156808,     1024,     2048, 0xcbff047c
+1,     157832,     157832,     1024,     2048, 0x9ac8f96b
+1,     158856,     158856,     1024,     2048, 0x43220bee
+1,     159880,     159880,     1024,     2048, 0x547bf351
+1,     160904,     160904,     1024,     2048, 0x7dd10d6e
+1,     161928,     161928,     1024,     2048, 0x77cbf603
+1,     162952,     162952,     1024,     2048, 0xb6fcff50
+1,     163976,     163976,     1024,     2048, 0x927bfde5
+1,     165000,     165000,     1024,     2048, 0x5bd0fca5
+1,     166024,     166024,     1024,     2048, 0x672cff2a
+1,     167048,     167048,     1024,     2048, 0x3e3ef01c
+1,     168072,     168072,     1024,     2048, 0xe52607af
+1,     169096,     169096,     1024,     2048, 0x66bceaf5
+1,     170120,     170120,     1024,     2048, 0xe065046b
+1,     171144,     171144,     1024,     2048, 0x350bf21f
+1,     172168,     172168,     1024,     2048, 0x60b1fca4
+1,     173192,     173192,     1024,     2048, 0x8b1efa55
+1,     174216,     174216,     1024,     2048, 0xf86ff855
+1,     175240,     175240,     1024,     2048, 0x6934061b
+1,     176264,     176264,      136,      272, 0x4a458a45
+0,         20,         20,        1,   230400, 0x88c4d19a
+1,     176400,     176400,     1024,     2048, 0xdb0cfe95
+1,     177424,     177424,     1024,     2048, 0xcff3fdf1
+1,     178448,     178448,     1024,     2048, 0x070cf585
+1,     179472,     179472,     1024,     2048, 0xe9b8007f
+1,     180496,     180496,     1024,     2048, 0xc51ffd64
+1,     181520,     181520,     1024,     2048, 0xede2fbf9
+1,     182544,     182544,     1024,     2048, 0x51510410
+1,     183568,     183568,     1024,     2048, 0x198af498
+1,     184592,     184592,     1024,     2048, 0xae3603a2
+0,         21,         21,        1,   230400, 0x0d77c977
+1,     185616,     185616,     1024,     2048, 0x6200f7a1
+1,     186640,     186640,     1024,     2048, 0xe6e3fe32
+1,     187664,     187664,     1024,     2048, 0xb2e2fd77
+1,     188688,     188688,     1024,     2048, 0x063dff2f
+1,     189712,     189712,     1024,     2048, 0xa89ffe21
+1,     190736,     190736,     1024,     2048, 0x9e6ffa6d
+1,     191760,     191760,     1024,     2048, 0x028b004e
+1,     192784,     192784,     1024,     2048, 0x57edfa23
+1,     193808,     193808,     1024,     2048, 0x6d8efe1f
+0,         22,         22,        1,   230400, 0x242629d7
+1,     194832,     194832,     1024,     2048, 0x774bfe54
+1,     195856,     195856,     1024,     2048, 0xa931fcfb
+1,     196880,     196880,     1024,     2048, 0x3505004b
+1,     197904,     197904,     1024,     2048, 0x5001f576
+1,     198928,     198928,     1024,     2048, 0x78ea049b
+1,     199952,     199952,     1024,     2048, 0xd45bf733
+1,     200976,     200976,     1024,     2048, 0x6395fead
+1,     202000,     202000,     1024,     2048, 0xc126015e
+0,         23,         23,        1,   230400, 0x62cdc018
+1,     203024,     203024,     1024,     2048, 0xbecff8aa
+1,     204048,     204048,     1024,     2048, 0x0fea06c3
+1,     205072,     205072,     1024,     2048, 0xdea6f351
+1,     206096,     206096,     1024,     2048, 0x35b808f0
+1,     207120,     207120,     1024,     2048, 0x5487ee73
+1,     208144,     208144,     1024,     2048, 0xac69050e
+1,     209168,     209168,     1024,     2048, 0xcc5ffb00
+1,     210192,     210192,     1024,     2048, 0x328c00cb
+1,     211216,     211216,     1024,     2048, 0xa707fd82
+0,         24,         24,        1,   230400, 0x248ad058
+1,     212240,     212240,     1024,     2048, 0xe442f73d
+1,     213264,     213264,     1024,     2048, 0x545c0418
+1,     214288,     214288,     1024,     2048, 0x744ff3f7
+1,     215312,     215312,     1024,     2048, 0x01aa04fd
+1,     216336,     216336,     1024,     2048, 0xa885f7cd
+1,     217360,     217360,     1024,     2048, 0xcfca04f4
+1,     218384,     218384,     1024,     2048, 0x67fdf91b
+1,     219408,     219408,     1024,     2048, 0xce2b001d
+1,     220432,     220432,       68,      136, 0x33e64a0d
diff --git a/tests/ref/fate/filter-curves b/tests/ref/fate/filter-curves
new file mode 100644
index 0000000..a96f0ee
--- /dev/null
+++ b/tests/ref/fate/filter-curves
@@ -0,0 +1,6 @@
+#tb 0: 1001/30000
+0,          0,          0,        1,   921600, 0xcf426780
+0,          1,          1,        1,   921600, 0x7642892d
+0,          2,          2,        1,   921600, 0x13c1ab7e
+0,          3,          3,        1,   921600, 0x3eca04bf
+0,          4,          4,        1,   921600, 0x61539162
diff --git a/tests/ref/fate/filter-drawbox b/tests/ref/fate/filter-drawbox
new file mode 100644
index 0000000..869b9f3
--- /dev/null
+++ b/tests/ref/fate/filter-drawbox
@@ -0,0 +1,51 @@
+#tb 0: 1/25
+0,          0,          0,        1,   152064, 0x7eee5ca3
+0,          1,          1,        1,   152064, 0x61125759
+0,          2,          2,        1,   152064, 0x2a64f47f
+0,          3,          3,        1,   152064, 0xc1089594
+0,          4,          4,        1,   152064, 0xd9e18830
+0,          5,          5,        1,   152064, 0xeb135e03
+0,          6,          6,        1,   152064, 0x6a5b40d7
+0,          7,          7,        1,   152064, 0x0a356a16
+0,          8,          8,        1,   152064, 0xfc1d7858
+0,          9,          9,        1,   152064, 0xa04bfeb8
+0,         10,         10,        1,   152064, 0x2d952ef0
+0,         11,         11,        1,   152064, 0x7f360233
+0,         12,         12,        1,   152064, 0xdd2bd142
+0,         13,         13,        1,   152064, 0xd231ad4a
+0,         14,         14,        1,   152064, 0x0543400e
+0,         15,         15,        1,   152064, 0x8252be2b
+0,         16,         16,        1,   152064, 0xd9f702be
+0,         17,         17,        1,   152064, 0xed5cf787
+0,         18,         18,        1,   152064, 0xf9472f8e
+0,         19,         19,        1,   152064, 0x89e4a60b
+0,         20,         20,        1,   152064, 0x1f12c1f5
+0,         21,         21,        1,   152064, 0x76eaf390
+0,         22,         22,        1,   152064, 0x60b5eba3
+0,         23,         23,        1,   152064, 0xf09e348c
+0,         24,         24,        1,   152064, 0x1afabf8a
+0,         25,         25,        1,   152064, 0xd16c558e
+0,         26,         26,        1,   152064, 0x78634796
+0,         27,         27,        1,   152064, 0xcd13b1e3
+0,         28,         28,        1,   152064, 0x59c2c6e5
+0,         29,         29,        1,   152064, 0x265e6beb
+0,         30,         30,        1,   152064, 0x82c656af
+0,         31,         31,        1,   152064, 0x919e923c
+0,         32,         32,        1,   152064, 0xc428fc15
+0,         33,         33,        1,   152064, 0x488760cd
+0,         34,         34,        1,   152064, 0x0a080c93
+0,         35,         35,        1,   152064, 0xaab649e6
+0,         36,         36,        1,   152064, 0x9b34edaa
+0,         37,         37,        1,   152064, 0x44e12816
+0,         38,         38,        1,   152064, 0x03777927
+0,         39,         39,        1,   152064, 0x6644573e
+0,         40,         40,        1,   152064, 0x18574df7
+0,         41,         41,        1,   152064, 0x5dce82f6
+0,         42,         42,        1,   152064, 0xb8be9205
+0,         43,         43,        1,   152064, 0xb927eacb
+0,         44,         44,        1,   152064, 0x303ec874
+0,         45,         45,        1,   152064, 0x05eb3c6f
+0,         46,         46,        1,   152064, 0x74a614d6
+0,         47,         47,        1,   152064, 0x6d078969
+0,         48,         48,        1,   152064, 0xe57a7ae0
+0,         49,         49,        1,   152064, 0xd6fca9ec
diff --git a/tests/ref/fate/filter-fade b/tests/ref/fate/filter-fade
new file mode 100644
index 0000000..3acd889
--- /dev/null
+++ b/tests/ref/fate/filter-fade
@@ -0,0 +1,51 @@
+#tb 0: 1/25
+0,          0,          0,        1,   152064, 0xb4e6c735
+0,          1,          1,        1,   152064, 0xbfa36592
+0,          2,          2,        1,   152064, 0x8678003e
+0,          3,          3,        1,   152064, 0xb1e9b1a3
+0,          4,          4,        1,   152064, 0x6ccf66ac
+0,          5,          5,        1,   152064, 0x3390056b
+0,          6,          6,        1,   152064, 0x4fcae508
+0,          7,          7,        1,   152064, 0x2528a210
+0,          8,          8,        1,   152064, 0x8128070e
+0,          9,          9,        1,   152064, 0xc991f27d
+0,         10,         10,        1,   152064, 0xe97a9a8b
+0,         11,         11,        1,   152064, 0x71cb32fb
+0,         12,         12,        1,   152064, 0x0b395f46
+0,         13,         13,        1,   152064, 0x362c1742
+0,         14,         14,        1,   152064, 0x14d23d11
+0,         15,         15,        1,   152064, 0x4d11a4d6
+0,         16,         16,        1,   152064, 0xee808ebb
+0,         17,         17,        1,   152064, 0x1d69964c
+0,         18,         18,        1,   152064, 0xfd5c30e6
+0,         19,         19,        1,   152064, 0xcd69b313
+0,         20,         20,        1,   152064, 0x3d75953b
+0,         21,         21,        1,   152064, 0xd6c698ea
+0,         22,         22,        1,   152064, 0x25006f22
+0,         23,         23,        1,   152064, 0x44e6a8a7
+0,         24,         24,        1,   152064, 0x0c251d62
+0,         25,         25,        1,   152064, 0x95579936
+0,         26,         26,        1,   152064, 0x8156c4f5
+0,         27,         27,        1,   152064, 0xca743348
+0,         28,         28,        1,   152064, 0x56a1373d
+0,         29,         29,        1,   152064, 0x17360ed3
+0,         30,         30,        1,   152064, 0x245243a1
+0,         31,         31,        1,   152064, 0xdd5ff4df
+0,         32,         32,        1,   152064, 0x2b979f45
+0,         33,         33,        1,   152064, 0x8a74da27
+0,         34,         34,        1,   152064, 0x27a7cd9f
+0,         35,         35,        1,   152064, 0xaa543c0f
+0,         36,         36,        1,   152064, 0xd72b5d8a
+0,         37,         37,        1,   152064, 0x8538f93d
+0,         38,         38,        1,   152064, 0x3fdc67b7
+0,         39,         39,        1,   152064, 0x4fe2154f
+0,         40,         40,        1,   152064, 0x908e09fb
+0,         41,         41,        1,   152064, 0x75ca5951
+0,         42,         42,        1,   152064, 0xd4a2ef14
+0,         43,         43,        1,   152064, 0x32d343a7
+0,         44,         44,        1,   152064, 0x59733ac8
+0,         45,         45,        1,   152064, 0x268d64da
+0,         46,         46,        1,   152064, 0x813eaf95
+0,         47,         47,        1,   152064, 0xf098ff5b
+0,         48,         48,        1,   152064, 0xc5b85c26
+0,         49,         49,        1,   152064, 0xc0f79718
diff --git a/tests/ref/fate/filter-gradfun b/tests/ref/fate/filter-gradfun
index dca442f..794fae9 100644
--- a/tests/ref/fate/filter-gradfun
+++ b/tests/ref/fate/filter-gradfun
@@ -1,21 +1,51 @@
-#tb 0: 1/10
-0,          0,          0,        1,    76800, 0xc87b6728
-0,          1,          1,        1,    76800, 0xb0b24bc8
-0,          2,          2,        1,    76800, 0x45a22d68
-0,          3,          3,        1,    76800, 0x1821013b
-0,          4,          4,        1,    76800, 0xfb17dc4f
-0,          5,          5,        1,    76800, 0x8e7eb458
-0,          6,          6,        1,    76800, 0x0df37d02
-0,          7,          7,        1,    76800, 0x18e4500d
-0,          8,          8,        1,    76800, 0x639316b0
-0,          9,          9,        1,    76800, 0xce00e560
-0,         10,         10,        1,    76800, 0xd533b42c
-0,         11,         11,        1,    76800, 0x0772794c
-0,         12,         12,        1,    76800, 0x99544be0
-0,         13,         13,        1,    76800, 0x5f1a16c1
-0,         14,         14,        1,    76800, 0x90bfe8ac
-0,         15,         15,        1,    76800, 0xbd9bb834
-0,         16,         16,        1,    76800, 0x658d8053
-0,         17,         17,        1,    76800, 0x21cc5656
-0,         18,         18,        1,    76800, 0x6d39208b
-0,         19,         19,        1,    76800, 0x17b0f8cb
+#tb 0: 1/25
+0,          0,          0,        1,   152064, 0xaebf898f
+0,          1,          1,        1,   152064, 0x217b6560
+0,          2,          2,        1,   152064, 0x0269f62d
+0,          3,          3,        1,   152064, 0x12168087
+0,          4,          4,        1,   152064, 0x42f9b5fb
+0,          5,          5,        1,   152064, 0xa653a8b3
+0,          6,          6,        1,   152064, 0x2e317bc3
+0,          7,          7,        1,   152064, 0xf3c28b63
+0,          8,          8,        1,   152064, 0x12e57fce
+0,          9,          9,        1,   152064, 0xeac238dd
+0,         10,         10,        1,   152064, 0x8fe74772
+0,         11,         11,        1,   152064, 0x982afc9c
+0,         12,         12,        1,   152064, 0xd82ead51
+0,         13,         13,        1,   152064, 0xf83ca211
+0,         14,         14,        1,   152064, 0xc01f8de2
+0,         15,         15,        1,   152064, 0x7ec60ec6
+0,         16,         16,        1,   152064, 0x1beb4dd3
+0,         17,         17,        1,   152064, 0x5a513899
+0,         18,         18,        1,   152064, 0x8c766a7b
+0,         19,         19,        1,   152064, 0x8bcddbb0
+0,         20,         20,        1,   152064, 0x01f6f558
+0,         21,         21,        1,   152064, 0xac7e240f
+0,         22,         22,        1,   152064, 0xe8811d38
+0,         23,         23,        1,   152064, 0x4ea268c9
+0,         24,         24,        1,   152064, 0xadc5f950
+0,         25,         25,        1,   152064, 0xdbf498fb
+0,         26,         26,        1,   152064, 0x1b999678
+0,         27,         27,        1,   152064, 0x085dd810
+0,         28,         28,        1,   152064, 0x4665a44f
+0,         29,         29,        1,   152064, 0x674764cf
+0,         30,         30,        1,   152064, 0x65e96a72
+0,         31,         31,        1,   152064, 0x44e0c539
+0,         32,         32,        1,   152064, 0xb124fc9e
+0,         33,         33,        1,   152064, 0x5cff79e3
+0,         34,         34,        1,   152064, 0x7dd94359
+0,         35,         35,        1,   152064, 0x3ec294b6
+0,         36,         36,        1,   152064, 0x675d3767
+0,         37,         37,        1,   152064, 0x9efa01cb
+0,         38,         38,        1,   152064, 0x321058d2
+0,         39,         39,        1,   152064, 0xe0d04e9c
+0,         40,         40,        1,   152064, 0xa4dd58d4
+0,         41,         41,        1,   152064, 0x4cc19dc5
+0,         42,         42,        1,   152064, 0x30a1bf77
+0,         43,         43,        1,   152064, 0x2c9d20f2
+0,         44,         44,        1,   152064, 0x55cb0447
+0,         45,         45,        1,   152064, 0x4daa7e4f
+0,         46,         46,        1,   152064, 0x2a4f53c7
+0,         47,         47,        1,   152064, 0x9fb8c583
+0,         48,         48,        1,   152064, 0x072eb401
+0,         49,         49,        1,   152064, 0xa203d8b7
diff --git a/tests/ref/fate/filter-gradfun-sample b/tests/ref/fate/filter-gradfun-sample
new file mode 100644
index 0000000..dca442f
--- /dev/null
+++ b/tests/ref/fate/filter-gradfun-sample
@@ -0,0 +1,21 @@
+#tb 0: 1/10
+0,          0,          0,        1,    76800, 0xc87b6728
+0,          1,          1,        1,    76800, 0xb0b24bc8
+0,          2,          2,        1,    76800, 0x45a22d68
+0,          3,          3,        1,    76800, 0x1821013b
+0,          4,          4,        1,    76800, 0xfb17dc4f
+0,          5,          5,        1,    76800, 0x8e7eb458
+0,          6,          6,        1,    76800, 0x0df37d02
+0,          7,          7,        1,    76800, 0x18e4500d
+0,          8,          8,        1,    76800, 0x639316b0
+0,          9,          9,        1,    76800, 0xce00e560
+0,         10,         10,        1,    76800, 0xd533b42c
+0,         11,         11,        1,    76800, 0x0772794c
+0,         12,         12,        1,    76800, 0x99544be0
+0,         13,         13,        1,    76800, 0x5f1a16c1
+0,         14,         14,        1,    76800, 0x90bfe8ac
+0,         15,         15,        1,    76800, 0xbd9bb834
+0,         16,         16,        1,    76800, 0x658d8053
+0,         17,         17,        1,    76800, 0x21cc5656
+0,         18,         18,        1,    76800, 0x6d39208b
+0,         19,         19,        1,    76800, 0x17b0f8cb
diff --git a/tests/ref/fate/filter-histogram-levels b/tests/ref/fate/filter-histogram-levels
new file mode 100644
index 0000000..df9f6b2
--- /dev/null
+++ b/tests/ref/fate/filter-histogram-levels
@@ -0,0 +1,51 @@
+#tb 0: 1/25
+0,          0,          0,        1,   488448, 0x0d7343b9
+0,          1,          1,        1,   488448, 0x118e3ade
+0,          2,          2,        1,   488448, 0x778f1ba9
+0,          3,          3,        1,   488448, 0x153bf44e
+0,          4,          4,        1,   488448, 0x2d83c1ab
+0,          5,          5,        1,   488448, 0xa3e95f8f
+0,          6,          6,        1,   488448, 0x91aad31b
+0,          7,          7,        1,   488448, 0x90b92c09
+0,          8,          8,        1,   488448, 0x1e4c9f41
+0,          9,          9,        1,   488448, 0xa88c1882
+0,         10,         10,        1,   488448, 0x1aa04274
+0,         11,         11,        1,   488448, 0x49c45de8
+0,         12,         12,        1,   488448, 0xe799c29f
+0,         13,         13,        1,   488448, 0x789e233f
+0,         14,         14,        1,   488448, 0x9f753404
+0,         15,         15,        1,   488448, 0x83050c2c
+0,         16,         16,        1,   488448, 0xddf7ccbf
+0,         17,         17,        1,   488448, 0xe3128531
+0,         18,         18,        1,   488448, 0xcc6596af
+0,         19,         19,        1,   488448, 0x6e19754f
+0,         20,         20,        1,   488448, 0xc3b32c7c
+0,         21,         21,        1,   488448, 0x40b4853f
+0,         22,         22,        1,   488448, 0x6e492674
+0,         23,         23,        1,   488448, 0x7f867236
+0,         24,         24,        1,   488448, 0x22094365
+0,         25,         25,        1,   488448, 0x45f30fc3
+0,         26,         26,        1,   488448, 0xe6cbad09
+0,         27,         27,        1,   488448, 0x0c44836b
+0,         28,         28,        1,   488448, 0xa7f04271
+0,         29,         29,        1,   488448, 0xd222ba88
+0,         30,         30,        1,   488448, 0xc96a9749
+0,         31,         31,        1,   488448, 0x82e25bbd
+0,         32,         32,        1,   488448, 0xf79d1882
+0,         33,         33,        1,   488448, 0x6d7fdd68
+0,         34,         34,        1,   488448, 0xeb5c9b1b
+0,         35,         35,        1,   488448, 0x9014f9f4
+0,         36,         36,        1,   488448, 0x96c6ab5f
+0,         37,         37,        1,   488448, 0x03911af0
+0,         38,         38,        1,   488448, 0xbf9dd8eb
+0,         39,         39,        1,   488448, 0x73509963
+0,         40,         40,        1,   488448, 0xf2ecb068
+0,         41,         41,        1,   488448, 0xec2fb311
+0,         42,         42,        1,   488448, 0xf4c7ba26
+0,         43,         43,        1,   488448, 0x23f56543
+0,         44,         44,        1,   488448, 0x25f8c48c
+0,         45,         45,        1,   488448, 0xf1ccd38b
+0,         46,         46,        1,   488448, 0x10780667
+0,         47,         47,        1,   488448, 0xbeb70431
+0,         48,         48,        1,   488448, 0xbc950678
+0,         49,         49,        1,   488448, 0xfedf5d83
diff --git a/tests/ref/fate/filter-histogram-waveform b/tests/ref/fate/filter-histogram-waveform
new file mode 100644
index 0000000..da6bbc4
--- /dev/null
+++ b/tests/ref/fate/filter-histogram-waveform
@@ -0,0 +1,51 @@
+#tb 0: 1/25
+0,          0,          0,        1,   663552, 0x8a55d4d9
+0,          1,          1,        1,   663552, 0xf4c4d42d
+0,          2,          2,        1,   663552, 0x291cd441
+0,          3,          3,        1,   663552, 0xce5bd56b
+0,          4,          4,        1,   663552, 0x1dc0d637
+0,          5,          5,        1,   663552, 0x4676d387
+0,          6,          6,        1,   663552, 0x8064d607
+0,          7,          7,        1,   663552, 0x1981d54f
+0,          8,          8,        1,   663552, 0x8fffd4a3
+0,          9,          9,        1,   663552, 0x5041d2f7
+0,         10,         10,        1,   663552, 0x03bbd401
+0,         11,         11,        1,   663552, 0x965bd2f3
+0,         12,         12,        1,   663552, 0x0d39d27d
+0,         13,         13,        1,   663552, 0xad39d1fb
+0,         14,         14,        1,   663552, 0x1809d195
+0,         15,         15,        1,   663552, 0xe083cf2f
+0,         16,         16,        1,   663552, 0x0d6fd3b3
+0,         17,         17,        1,   663552, 0x3a8ed3f3
+0,         18,         18,        1,   663552, 0xbbb5d00d
+0,         19,         19,        1,   663552, 0xe6ead0df
+0,         20,         20,        1,   663552, 0xf5dcd35f
+0,         21,         21,        1,   663552, 0x9cd9d32b
+0,         22,         22,        1,   663552, 0xcb91d1b9
+0,         23,         23,        1,   663552, 0x5640cfd7
+0,         24,         24,        1,   663552, 0x5370d285
+0,         25,         25,        1,   663552, 0xd894d1dd
+0,         26,         26,        1,   663552, 0xace4ce65
+0,         27,         27,        1,   663552, 0x6e15ce17
+0,         28,         28,        1,   663552, 0xd21cce21
+0,         29,         29,        1,   663552, 0xec1ecd83
+0,         30,         30,        1,   663552, 0x9852ce0f
+0,         31,         31,        1,   663552, 0xe488cba3
+0,         32,         32,        1,   663552, 0x2e15cbed
+0,         33,         33,        1,   663552, 0x5e59ca97
+0,         34,         34,        1,   663552, 0x7cefcd7d
+0,         35,         35,        1,   663552, 0xcb99ccfb
+0,         36,         36,        1,   663552, 0xce32cf29
+0,         37,         37,        1,   663552, 0x7fb8ceef
+0,         38,         38,        1,   663552, 0x4014d18f
+0,         39,         39,        1,   663552, 0x224dd381
+0,         40,         40,        1,   663552, 0x5347d125
+0,         41,         41,        1,   663552, 0xec83ce79
+0,         42,         42,        1,   663552, 0x4d24ce8b
+0,         43,         43,        1,   663552, 0xfdc5ccbd
+0,         44,         44,        1,   663552, 0x592cd18b
+0,         45,         45,        1,   663552, 0xff06d43b
+0,         46,         46,        1,   663552, 0x7f69d4ef
+0,         47,         47,        1,   663552, 0x1607d3f1
+0,         48,         48,        1,   663552, 0x33e0d211
+0,         49,         49,        1,   663552, 0xe1b1d2cd
diff --git a/tests/ref/fate/filter-hqdn3d b/tests/ref/fate/filter-hqdn3d
index d69e6db..6f121c3 100644
--- a/tests/ref/fate/filter-hqdn3d
+++ b/tests/ref/fate/filter-hqdn3d
@@ -1,74 +1,51 @@
-#tb 0: 1/9
-0,          0,          0,        1,   115200, 0x2c810465
-0,          1,          1,        1,   115200, 0x957c0563
-0,          2,          2,        1,   115200, 0x786c6d5b
-0,          3,          3,        1,   115200, 0xd5ef87d3
-0,          4,          4,        1,   115200, 0x3a2158e2
-0,          5,          5,        1,   115200, 0x19d7d048
-0,          6,          6,        1,   115200, 0x16d5e09a
-0,          7,          7,        1,   115200, 0x73cca454
-0,          8,          8,        1,   115200, 0x4d6be3bc
-0,          9,          9,        1,   115200, 0x672aad0f
-0,         10,         10,        1,   115200, 0x1bd103b7
-0,         11,         11,        1,   115200, 0xbc3a9c02
-0,         12,         12,        1,   115200, 0xa19cb68c
-0,         13,         13,        1,   115200, 0x83477b6c
-0,         14,         14,        1,   115200, 0x68b6898e
-0,         15,         15,        1,   115200, 0xebbc5701
-0,         16,         16,        1,   115200, 0x37e873db
-0,         17,         17,        1,   115200, 0xcf9e7ad1
-0,         18,         18,        1,   115200, 0x3fdffd3a
-0,         19,         19,        1,   115200, 0x413da058
-0,         20,         20,        1,   115200, 0xa432b2f6
-0,         21,         21,        1,   115200, 0x9c532b61
-0,         22,         22,        1,   115200, 0x40c03856
-0,         23,         23,        1,   115200, 0xf8310ec7
-0,         24,         24,        1,   115200, 0x89246da7
-0,         25,         25,        1,   115200, 0x501d4dd0
-0,         26,         26,        1,   115200, 0xe5151ebf
-0,         27,         27,        1,   115200, 0xc166e201
-0,         28,         28,        1,   115200, 0xded25b69
-0,         29,         29,        1,   115200, 0xb51ec43f
-0,         30,         30,        1,   115200, 0x9bba20eb
-0,         31,         31,        1,   115200, 0x7a7b9278
-0,         32,         32,        1,   115200, 0xe98475d0
-0,         33,         33,        1,   115200, 0x960dc933
-0,         34,         34,        1,   115200, 0xe93f558b
-0,         35,         35,        1,   115200, 0x37ae3e42
-0,         36,         36,        1,   115200, 0x0ecaf64a
-0,         37,         37,        1,   115200, 0xd5938191
-0,         38,         38,        1,   115200, 0xeb04510a
-0,         39,         39,        1,   115200, 0xf5729201
-0,         40,         40,        1,   115200, 0xb2c04015
-0,         41,         41,        1,   115200, 0xd883143e
-0,         42,         42,        1,   115200, 0x5e04197b
-0,         43,         43,        1,   115200, 0x6b846e24
-0,         44,         44,        1,   115200, 0x026e7a0b
-0,         45,         45,        1,   115200, 0xf8c6708b
-0,         46,         46,        1,   115200, 0x3fe75c63
-0,         47,         47,        1,   115200, 0xf56467b1
-0,         48,         48,        1,   115200, 0xf1230483
-0,         49,         49,        1,   115200, 0x35a615ce
-0,         50,         50,        1,   115200, 0x30b42d7b
-0,         51,         51,        1,   115200, 0x8b7f22a1
-0,         52,         52,        1,   115200, 0x03a2e789
-0,         53,         53,        1,   115200, 0x15532e9e
-0,         54,         54,        1,   115200, 0xbe85f37a
-0,         55,         55,        1,   115200, 0x2c581d60
-0,         56,         56,        1,   115200, 0xd7a506ed
-0,         57,         57,        1,   115200, 0x35e09799
-0,         58,         58,        1,   115200, 0x2d4f5499
-0,         59,         59,        1,   115200, 0x92fdc85b
-0,         60,         60,        1,   115200, 0x33f4888a
-0,         61,         61,        1,   115200, 0x65e04e05
-0,         62,         62,        1,   115200, 0x14766728
-0,         63,         63,        1,   115200, 0x2a432c3f
-0,         64,         64,        1,   115200, 0x136a1362
-0,         65,         65,        1,   115200, 0xfbf4cb01
-0,         66,         66,        1,   115200, 0x7dcaff69
-0,         67,         67,        1,   115200, 0x5afd3b9e
-0,         68,         68,        1,   115200, 0x86fcb122
-0,         69,         69,        1,   115200, 0xc988b519
-0,         70,         70,        1,   115200, 0x48fd3e75
-0,         71,         71,        1,   115200, 0x2728a2d7
-0,         72,         72,        1,   115200, 0xa2ac6418
+#tb 0: 1/25
+0,          0,          0,        1,   152064, 0x0c09883a
+0,          1,          1,        1,   152064, 0xfc2748a0
+0,          2,          2,        1,   152064, 0xb746eea8
+0,          3,          3,        1,   152064, 0xf14192d7
+0,          4,          4,        1,   152064, 0xae26db7f
+0,          5,          5,        1,   152064, 0xe1a7da48
+0,          6,          6,        1,   152064, 0x5196b387
+0,          7,          7,        1,   152064, 0x20eac7ff
+0,          8,          8,        1,   152064, 0x4896c7f5
+0,          9,          9,        1,   152064, 0x97ba6468
+0,         10,         10,        1,   152064, 0x29b19884
+0,         11,         11,        1,   152064, 0x591e4e14
+0,         12,         12,        1,   152064, 0xb692fc98
+0,         13,         13,        1,   152064, 0x3457ed57
+0,         14,         14,        1,   152064, 0xbb10d71d
+0,         15,         15,        1,   152064, 0x47f45b57
+0,         16,         16,        1,   152064, 0xddbd8bc6
+0,         17,         17,        1,   152064, 0x1ef36e02
+0,         18,         18,        1,   152064, 0x55a363d2
+0,         19,         19,        1,   152064, 0x11c1c2f4
+0,         20,         20,        1,   152064, 0x553bdc2b
+0,         21,         21,        1,   152064, 0x2101d886
+0,         22,         22,        1,   152064, 0xe8f77998
+0,         23,         23,        1,   152064, 0x39bac7ff
+0,         24,         24,        1,   152064, 0x09a4172e
+0,         25,         25,        1,   152064, 0x6121f57f
+0,         26,         26,        1,   152064, 0x60e7525c
+0,         27,         27,        1,   152064, 0xd7895259
+0,         28,         28,        1,   152064, 0x12b2153b
+0,         29,         29,        1,   152064, 0x6119a22e
+0,         30,         30,        1,   152064, 0xf1969bd5
+0,         31,         31,        1,   152064, 0x7b03fc83
+0,         32,         32,        1,   152064, 0x17383667
+0,         33,         33,        1,   152064, 0x332bbfba
+0,         34,         34,        1,   152064, 0x253998a8
+0,         35,         35,        1,   152064, 0x2614c984
+0,         36,         36,        1,   152064, 0x50c86e8d
+0,         37,         37,        1,   152064, 0x9ff23b55
+0,         38,         38,        1,   152064, 0xc4589665
+0,         39,         39,        1,   152064, 0x5e7d86c7
+0,         40,         40,        1,   152064, 0xdda07f2f
+0,         41,         41,        1,   152064, 0xa1dacf4a
+0,         42,         42,        1,   152064, 0x3c83fb32
+0,         43,         43,        1,   152064, 0xa43da916
+0,         44,         44,        1,   152064, 0x0e767b80
+0,         45,         45,        1,   152064, 0xa18c5f82
+0,         46,         46,        1,   152064, 0xdb21c249
+0,         47,         47,        1,   152064, 0xb126341e
+0,         48,         48,        1,   152064, 0xc14742be
+0,         49,         49,        1,   152064, 0x582f631d
diff --git a/tests/ref/fate/filter-hqdn3d-sample b/tests/ref/fate/filter-hqdn3d-sample
new file mode 100644
index 0000000..d69e6db
--- /dev/null
+++ b/tests/ref/fate/filter-hqdn3d-sample
@@ -0,0 +1,74 @@
+#tb 0: 1/9
+0,          0,          0,        1,   115200, 0x2c810465
+0,          1,          1,        1,   115200, 0x957c0563
+0,          2,          2,        1,   115200, 0x786c6d5b
+0,          3,          3,        1,   115200, 0xd5ef87d3
+0,          4,          4,        1,   115200, 0x3a2158e2
+0,          5,          5,        1,   115200, 0x19d7d048
+0,          6,          6,        1,   115200, 0x16d5e09a
+0,          7,          7,        1,   115200, 0x73cca454
+0,          8,          8,        1,   115200, 0x4d6be3bc
+0,          9,          9,        1,   115200, 0x672aad0f
+0,         10,         10,        1,   115200, 0x1bd103b7
+0,         11,         11,        1,   115200, 0xbc3a9c02
+0,         12,         12,        1,   115200, 0xa19cb68c
+0,         13,         13,        1,   115200, 0x83477b6c
+0,         14,         14,        1,   115200, 0x68b6898e
+0,         15,         15,        1,   115200, 0xebbc5701
+0,         16,         16,        1,   115200, 0x37e873db
+0,         17,         17,        1,   115200, 0xcf9e7ad1
+0,         18,         18,        1,   115200, 0x3fdffd3a
+0,         19,         19,        1,   115200, 0x413da058
+0,         20,         20,        1,   115200, 0xa432b2f6
+0,         21,         21,        1,   115200, 0x9c532b61
+0,         22,         22,        1,   115200, 0x40c03856
+0,         23,         23,        1,   115200, 0xf8310ec7
+0,         24,         24,        1,   115200, 0x89246da7
+0,         25,         25,        1,   115200, 0x501d4dd0
+0,         26,         26,        1,   115200, 0xe5151ebf
+0,         27,         27,        1,   115200, 0xc166e201
+0,         28,         28,        1,   115200, 0xded25b69
+0,         29,         29,        1,   115200, 0xb51ec43f
+0,         30,         30,        1,   115200, 0x9bba20eb
+0,         31,         31,        1,   115200, 0x7a7b9278
+0,         32,         32,        1,   115200, 0xe98475d0
+0,         33,         33,        1,   115200, 0x960dc933
+0,         34,         34,        1,   115200, 0xe93f558b
+0,         35,         35,        1,   115200, 0x37ae3e42
+0,         36,         36,        1,   115200, 0x0ecaf64a
+0,         37,         37,        1,   115200, 0xd5938191
+0,         38,         38,        1,   115200, 0xeb04510a
+0,         39,         39,        1,   115200, 0xf5729201
+0,         40,         40,        1,   115200, 0xb2c04015
+0,         41,         41,        1,   115200, 0xd883143e
+0,         42,         42,        1,   115200, 0x5e04197b
+0,         43,         43,        1,   115200, 0x6b846e24
+0,         44,         44,        1,   115200, 0x026e7a0b
+0,         45,         45,        1,   115200, 0xf8c6708b
+0,         46,         46,        1,   115200, 0x3fe75c63
+0,         47,         47,        1,   115200, 0xf56467b1
+0,         48,         48,        1,   115200, 0xf1230483
+0,         49,         49,        1,   115200, 0x35a615ce
+0,         50,         50,        1,   115200, 0x30b42d7b
+0,         51,         51,        1,   115200, 0x8b7f22a1
+0,         52,         52,        1,   115200, 0x03a2e789
+0,         53,         53,        1,   115200, 0x15532e9e
+0,         54,         54,        1,   115200, 0xbe85f37a
+0,         55,         55,        1,   115200, 0x2c581d60
+0,         56,         56,        1,   115200, 0xd7a506ed
+0,         57,         57,        1,   115200, 0x35e09799
+0,         58,         58,        1,   115200, 0x2d4f5499
+0,         59,         59,        1,   115200, 0x92fdc85b
+0,         60,         60,        1,   115200, 0x33f4888a
+0,         61,         61,        1,   115200, 0x65e04e05
+0,         62,         62,        1,   115200, 0x14766728
+0,         63,         63,        1,   115200, 0x2a432c3f
+0,         64,         64,        1,   115200, 0x136a1362
+0,         65,         65,        1,   115200, 0xfbf4cb01
+0,         66,         66,        1,   115200, 0x7dcaff69
+0,         67,         67,        1,   115200, 0x5afd3b9e
+0,         68,         68,        1,   115200, 0x86fcb122
+0,         69,         69,        1,   115200, 0xc988b519
+0,         70,         70,        1,   115200, 0x48fd3e75
+0,         71,         71,        1,   115200, 0x2728a2d7
+0,         72,         72,        1,   115200, 0xa2ac6418
diff --git a/tests/ref/fate/filter-interlace b/tests/ref/fate/filter-interlace
new file mode 100644
index 0000000..4659942
--- /dev/null
+++ b/tests/ref/fate/filter-interlace
@@ -0,0 +1,26 @@
+#tb 0: 2/25
+0,          0,          0,        1,   152064, 0x6077db38
+0,          1,          1,        1,   152064, 0x3d4f1b15
+0,          2,          2,        1,   152064, 0x447594f6
+0,          3,          3,        1,   152064, 0xb6258a38
+0,          4,          4,        1,   152064, 0x296abb09
+0,          5,          5,        1,   152064, 0x0fad069d
+0,          6,          6,        1,   152064, 0x92c78c0d
+0,          7,          7,        1,   152064, 0x645531fd
+0,          8,          8,        1,   152064, 0xe7652880
+0,          9,          9,        1,   152064, 0x496e1151
+0,         10,         10,        1,   152064, 0x7f7cfb06
+0,         11,         11,        1,   152064, 0x45e9affe
+0,         12,         12,        1,   152064, 0xaedb2d3a
+0,         13,         13,        1,   152064, 0x03fd9ae6
+0,         14,         14,        1,   152064, 0x2084e84d
+0,         15,         15,        1,   152064, 0xcf05faf6
+0,         16,         16,        1,   152064, 0x84c746c2
+0,         17,         17,        1,   152064, 0x898a6321
+0,         18,         18,        1,   152064, 0xe12b7fe9
+0,         19,         19,        1,   152064, 0x12feb756
+0,         20,         20,        1,   152064, 0xd813601a
+0,         21,         21,        1,   152064, 0xc0f3d385
+0,         22,         22,        1,   152064, 0xfca3a63f
+0,         23,         23,        1,   152064, 0xa0796f44
+0,         24,         24,        1,   152064, 0x1d26af11
diff --git a/tests/ref/fate/filter-metadata-ebur128 b/tests/ref/fate/filter-metadata-ebur128
new file mode 100644
index 0000000..7a060bd
--- /dev/null
+++ b/tests/ref/fate/filter-metadata-ebur128
@@ -0,0 +1,280 @@
+pkt_pts=0|tag:lavfi.r128.M=-120.691|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-70.000|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=4800|tag:lavfi.r128.M=-120.691|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-70.000|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=9600|tag:lavfi.r128.M=-120.691|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-70.000|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=14400|tag:lavfi.r128.M=-21.333|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-21.340|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=19200|tag:lavfi.r128.M=-21.249|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-21.295|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=24000|tag:lavfi.r128.M=-21.235|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-21.276|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=28800|tag:lavfi.r128.M=-21.587|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-21.353|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=33600|tag:lavfi.r128.M=-21.999|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-21.475|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=38400|tag:lavfi.r128.M=-22.371|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-21.613|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=43200|tag:lavfi.r128.M=-23.195|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-21.807|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=48000|tag:lavfi.r128.M=-23.550|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-21.991|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=52800|tag:lavfi.r128.M=-23.901|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-22.167|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=57600|tag:lavfi.r128.M=-24.461|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-22.350|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=62400|tag:lavfi.r128.M=-24.984|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-22.533|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=67200|tag:lavfi.r128.M=-25.448|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-22.714|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=72000|tag:lavfi.r128.M=-25.847|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-22.889|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=76800|tag:lavfi.r128.M=-26.187|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-23.058|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=81600|tag:lavfi.r128.M=-26.229|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-23.210|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=86400|tag:lavfi.r128.M=-26.132|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-23.346|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=91200|tag:lavfi.r128.M=-26.164|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-23.470|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=96000|tag:lavfi.r128.M=-26.282|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-23.586|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=100800|tag:lavfi.r128.M=-26.371|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-23.696|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=105600|tag:lavfi.r128.M=-26.421|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-23.799|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=110400|tag:lavfi.r128.M=-26.362|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-23.892|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=115200|tag:lavfi.r128.M=-26.278|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-23.977|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=120000|tag:lavfi.r128.M=-26.524|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-24.061|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=124800|tag:lavfi.r128.M=-26.867|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-24.148|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=129600|tag:lavfi.r128.M=-27.286|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-24.239|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=134400|tag:lavfi.r128.M=-27.372|tag:lavfi.r128.S=-120.691|tag:lavfi.r128.I=-24.326|tag:lavfi.r128.LRA=0.000|tag:lavfi.r128.LRA.low=0.000|tag:lavfi.r128.LRA.high=0.000
+pkt_pts=139200|tag:lavfi.r128.M=-27.449|tag:lavfi.r128.S=-24.296|tag:lavfi.r128.I=-24.409|tag:lavfi.r128.LRA=20.000|tag:lavfi.r128.LRA.low=-44.300|tag:lavfi.r128.LRA.high=-24.300
+pkt_pts=144000|tag:lavfi.r128.M=-27.083|tag:lavfi.r128.S=-24.455|tag:lavfi.r128.I=-24.481|tag:lavfi.r128.LRA=20.080|tag:lavfi.r128.LRA.low=-44.380|tag:lavfi.r128.LRA.high=-24.300
+pkt_pts=148800|tag:lavfi.r128.M=-26.899|tag:lavfi.r128.S=-24.719|tag:lavfi.r128.I=-24.545|tag:lavfi.r128.LRA=20.190|tag:lavfi.r128.LRA.low=-44.490|tag:lavfi.r128.LRA.high=-24.300
+pkt_pts=153600|tag:lavfi.r128.M=-26.880|tag:lavfi.r128.S=-24.962|tag:lavfi.r128.I=-24.606|tag:lavfi.r128.LRA=20.310|tag:lavfi.r128.LRA.low=-44.610|tag:lavfi.r128.LRA.high=-24.300
+pkt_pts=158400|tag:lavfi.r128.M=-26.886|tag:lavfi.r128.S=-25.214|tag:lavfi.r128.I=-24.664|tag:lavfi.r128.LRA=0.920|tag:lavfi.r128.LRA.low=-25.220|tag:lavfi.r128.LRA.high=-24.300
+pkt_pts=163200|tag:lavfi.r128.M=-27.096|tag:lavfi.r128.S=-25.459|tag:lavfi.r128.I=-24.722|tag:lavfi.r128.LRA=1.160|tag:lavfi.r128.LRA.low=-25.460|tag:lavfi.r128.LRA.high=-24.300
+pkt_pts=168000|tag:lavfi.r128.M=-26.430|tag:lavfi.r128.S=-25.729|tag:lavfi.r128.I=-24.765|tag:lavfi.r128.LRA=1.430|tag:lavfi.r128.LRA.low=-25.730|tag:lavfi.r128.LRA.high=-24.300
+pkt_pts=172800|tag:lavfi.r128.M=-25.736|tag:lavfi.r128.S=-25.816|tag:lavfi.r128.I=-24.791|tag:lavfi.r128.LRA=1.520|tag:lavfi.r128.LRA.low=-25.820|tag:lavfi.r128.LRA.high=-24.300
+pkt_pts=177600|tag:lavfi.r128.M=-25.976|tag:lavfi.r128.S=-26.011|tag:lavfi.r128.I=-24.821|tag:lavfi.r128.LRA=1.720|tag:lavfi.r128.LRA.low=-26.020|tag:lavfi.r128.LRA.high=-24.300
+pkt_pts=182400|tag:lavfi.r128.M=-26.417|tag:lavfi.r128.S=-26.240|tag:lavfi.r128.I=-24.858|tag:lavfi.r128.LRA=1.940|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-24.300
+pkt_pts=187200|tag:lavfi.r128.M=-27.102|tag:lavfi.r128.S=-26.393|tag:lavfi.r128.I=-24.906|tag:lavfi.r128.LRA=1.940|tag:lavfi.r128.LRA.low=-26.400|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=192000|tag:lavfi.r128.M=-28.221|tag:lavfi.r128.S=-26.511|tag:lavfi.r128.I=-24.968|tag:lavfi.r128.LRA=2.060|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=196800|tag:lavfi.r128.M=-27.900|tag:lavfi.r128.S=-26.618|tag:lavfi.r128.I=-25.023|tag:lavfi.r128.LRA=2.160|tag:lavfi.r128.LRA.low=-26.620|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=201600|tag:lavfi.r128.M=-27.179|tag:lavfi.r128.S=-26.666|tag:lavfi.r128.I=-25.065|tag:lavfi.r128.LRA=2.210|tag:lavfi.r128.LRA.low=-26.670|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=206400|tag:lavfi.r128.M=-26.274|tag:lavfi.r128.S=-26.604|tag:lavfi.r128.I=-25.091|tag:lavfi.r128.LRA=2.160|tag:lavfi.r128.LRA.low=-26.620|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=211200|tag:lavfi.r128.M=-25.385|tag:lavfi.r128.S=-26.501|tag:lavfi.r128.I=-25.098|tag:lavfi.r128.LRA=2.160|tag:lavfi.r128.LRA.low=-26.620|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=216000|tag:lavfi.r128.M=-25.220|tag:lavfi.r128.S=-26.511|tag:lavfi.r128.I=-25.101|tag:lavfi.r128.LRA=2.160|tag:lavfi.r128.LRA.low=-26.620|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=220800|tag:lavfi.r128.M=-23.888|tag:lavfi.r128.S=-26.237|tag:lavfi.r128.I=-25.069|tag:lavfi.r128.LRA=2.160|tag:lavfi.r128.LRA.low=-26.620|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=225600|tag:lavfi.r128.M=-24.544|tag:lavfi.r128.S=-26.315|tag:lavfi.r128.I=-25.057|tag:lavfi.r128.LRA=2.160|tag:lavfi.r128.LRA.low=-26.620|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=230400|tag:lavfi.r128.M=-25.042|tag:lavfi.r128.S=-26.325|tag:lavfi.r128.I=-25.057|tag:lavfi.r128.LRA=2.160|tag:lavfi.r128.LRA.low=-26.620|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=235200|tag:lavfi.r128.M=-25.146|tag:lavfi.r128.S=-26.349|tag:lavfi.r128.I=-25.059|tag:lavfi.r128.LRA=2.160|tag:lavfi.r128.LRA.low=-26.620|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=240000|tag:lavfi.r128.M=-27.510|tag:lavfi.r128.S=-26.380|tag:lavfi.r128.I=-25.098|tag:lavfi.r128.LRA=2.160|tag:lavfi.r128.LRA.low=-26.620|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=244800|tag:lavfi.r128.M=-26.266|tag:lavfi.r128.S=-26.301|tag:lavfi.r128.I=-25.119|tag:lavfi.r128.LRA=2.160|tag:lavfi.r128.LRA.low=-26.620|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=249600|tag:lavfi.r128.M=-26.455|tag:lavfi.r128.S=-26.329|tag:lavfi.r128.I=-25.142|tag:lavfi.r128.LRA=2.160|tag:lavfi.r128.LRA.low=-26.620|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=254400|tag:lavfi.r128.M=-26.169|tag:lavfi.r128.S=-26.323|tag:lavfi.r128.I=-25.160|tag:lavfi.r128.LRA=2.150|tag:lavfi.r128.LRA.low=-26.610|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=259200|tag:lavfi.r128.M=-25.834|tag:lavfi.r128.S=-26.317|tag:lavfi.r128.I=-25.172|tag:lavfi.r128.LRA=2.150|tag:lavfi.r128.LRA.low=-26.610|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=264000|tag:lavfi.r128.M=-26.461|tag:lavfi.r128.S=-26.293|tag:lavfi.r128.I=-25.194|tag:lavfi.r128.LRA=2.150|tag:lavfi.r128.LRA.low=-26.610|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=268800|tag:lavfi.r128.M=-26.327|tag:lavfi.r128.S=-26.262|tag:lavfi.r128.I=-25.212|tag:lavfi.r128.LRA=2.150|tag:lavfi.r128.LRA.low=-26.610|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=273600|tag:lavfi.r128.M=-26.559|tag:lavfi.r128.S=-26.239|tag:lavfi.r128.I=-25.233|tag:lavfi.r128.LRA=2.150|tag:lavfi.r128.LRA.low=-26.610|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=278400|tag:lavfi.r128.M=-26.657|tag:lavfi.r128.S=-26.236|tag:lavfi.r128.I=-25.255|tag:lavfi.r128.LRA=2.150|tag:lavfi.r128.LRA.low=-26.610|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=283200|tag:lavfi.r128.M=-26.563|tag:lavfi.r128.S=-26.193|tag:lavfi.r128.I=-25.275|tag:lavfi.r128.LRA=1.890|tag:lavfi.r128.LRA.low=-26.610|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=288000|tag:lavfi.r128.M=-26.632|tag:lavfi.r128.S=-26.210|tag:lavfi.r128.I=-25.295|tag:lavfi.r128.LRA=1.890|tag:lavfi.r128.LRA.low=-26.610|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=292800|tag:lavfi.r128.M=-26.750|tag:lavfi.r128.S=-26.222|tag:lavfi.r128.I=-25.316|tag:lavfi.r128.LRA=1.890|tag:lavfi.r128.LRA.low=-26.610|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=297600|tag:lavfi.r128.M=-26.727|tag:lavfi.r128.S=-26.218|tag:lavfi.r128.I=-25.336|tag:lavfi.r128.LRA=1.890|tag:lavfi.r128.LRA.low=-26.610|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=302400|tag:lavfi.r128.M=-26.945|tag:lavfi.r128.S=-26.200|tag:lavfi.r128.I=-25.358|tag:lavfi.r128.LRA=1.800|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=307200|tag:lavfi.r128.M=-26.839|tag:lavfi.r128.S=-26.181|tag:lavfi.r128.I=-25.379|tag:lavfi.r128.LRA=1.800|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=312000|tag:lavfi.r128.M=-26.056|tag:lavfi.r128.S=-26.172|tag:lavfi.r128.I=-25.389|tag:lavfi.r128.LRA=1.800|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=316800|tag:lavfi.r128.M=-26.589|tag:lavfi.r128.S=-26.335|tag:lavfi.r128.I=-25.405|tag:lavfi.r128.LRA=1.800|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=321600|tag:lavfi.r128.M=-27.190|tag:lavfi.r128.S=-26.351|tag:lavfi.r128.I=-25.428|tag:lavfi.r128.LRA=1.800|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=326400|tag:lavfi.r128.M=-28.078|tag:lavfi.r128.S=-26.359|tag:lavfi.r128.I=-25.458|tag:lavfi.r128.LRA=1.800|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=331200|tag:lavfi.r128.M=-27.878|tag:lavfi.r128.S=-26.250|tag:lavfi.r128.I=-25.486|tag:lavfi.r128.LRA=1.800|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=336000|tag:lavfi.r128.M=-26.604|tag:lavfi.r128.S=-26.169|tag:lavfi.r128.I=-25.500|tag:lavfi.r128.LRA=1.800|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=340800|tag:lavfi.r128.M=-26.289|tag:lavfi.r128.S=-26.173|tag:lavfi.r128.I=-25.511|tag:lavfi.r128.LRA=1.800|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=345600|tag:lavfi.r128.M=-24.672|tag:lavfi.r128.S=-26.000|tag:lavfi.r128.I=-25.498|tag:lavfi.r128.LRA=1.800|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=350400|tag:lavfi.r128.M=-25.114|tag:lavfi.r128.S=-26.077|tag:lavfi.r128.I=-25.492|tag:lavfi.r128.LRA=1.800|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=355200|tag:lavfi.r128.M=-24.561|tag:lavfi.r128.S=-26.027|tag:lavfi.r128.I=-25.478|tag:lavfi.r128.LRA=1.800|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=360000|tag:lavfi.r128.M=-23.558|tag:lavfi.r128.S=-25.849|tag:lavfi.r128.I=-25.445|tag:lavfi.r128.LRA=1.800|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=364800|tag:lavfi.r128.M=-24.650|tag:lavfi.r128.S=-26.154|tag:lavfi.r128.I=-25.433|tag:lavfi.r128.LRA=1.800|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=369600|tag:lavfi.r128.M=-23.700|tag:lavfi.r128.S=-25.903|tag:lavfi.r128.I=-25.405|tag:lavfi.r128.LRA=1.800|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=374400|tag:lavfi.r128.M=-24.144|tag:lavfi.r128.S=-25.863|tag:lavfi.r128.I=-25.386|tag:lavfi.r128.LRA=1.800|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=379200|tag:lavfi.r128.M=-23.935|tag:lavfi.r128.S=-25.635|tag:lavfi.r128.I=-25.364|tag:lavfi.r128.LRA=1.550|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.970
+pkt_pts=384000|tag:lavfi.r128.M=-22.202|tag:lavfi.r128.S=-25.242|tag:lavfi.r128.I=-25.305|tag:lavfi.r128.LRA=1.550|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.970
+pkt_pts=388800|tag:lavfi.r128.M=-22.634|tag:lavfi.r128.S=-25.257|tag:lavfi.r128.I=-25.258|tag:lavfi.r128.LRA=1.550|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.970
+pkt_pts=393600|tag:lavfi.r128.M=-22.480|tag:lavfi.r128.S=-25.165|tag:lavfi.r128.I=-25.210|tag:lavfi.r128.LRA=1.550|tag:lavfi.r128.LRA.low=-26.520|tag:lavfi.r128.LRA.high=-24.970
+pkt_pts=398400|tag:lavfi.r128.M=-23.030|tag:lavfi.r128.S=-25.124|tag:lavfi.r128.I=-25.176|tag:lavfi.r128.LRA=1.540|tag:lavfi.r128.LRA.low=-26.510|tag:lavfi.r128.LRA.high=-24.970
+pkt_pts=403200|tag:lavfi.r128.M=-24.357|tag:lavfi.r128.S=-25.042|tag:lavfi.r128.I=-25.165|tag:lavfi.r128.LRA=1.540|tag:lavfi.r128.LRA.low=-26.510|tag:lavfi.r128.LRA.high=-24.970
+pkt_pts=408000|tag:lavfi.r128.M=-24.346|tag:lavfi.r128.S=-24.990|tag:lavfi.r128.I=-25.154|tag:lavfi.r128.LRA=1.540|tag:lavfi.r128.LRA.low=-26.510|tag:lavfi.r128.LRA.high=-24.970
+pkt_pts=412800|tag:lavfi.r128.M=-24.868|tag:lavfi.r128.S=-24.992|tag:lavfi.r128.I=-25.151|tag:lavfi.r128.LRA=1.540|tag:lavfi.r128.LRA.low=-26.510|tag:lavfi.r128.LRA.high=-24.970
+pkt_pts=417600|tag:lavfi.r128.M=-25.190|tag:lavfi.r128.S=-24.972|tag:lavfi.r128.I=-25.151|tag:lavfi.r128.LRA=1.540|tag:lavfi.r128.LRA.low=-26.510|tag:lavfi.r128.LRA.high=-24.970
+pkt_pts=422400|tag:lavfi.r128.M=-26.094|tag:lavfi.r128.S=-24.987|tag:lavfi.r128.I=-25.161|tag:lavfi.r128.LRA=1.540|tag:lavfi.r128.LRA.low=-26.510|tag:lavfi.r128.LRA.high=-24.970
+pkt_pts=427200|tag:lavfi.r128.M=-26.093|tag:lavfi.r128.S=-24.944|tag:lavfi.r128.I=-25.171|tag:lavfi.r128.LRA=1.560|tag:lavfi.r128.LRA.low=-26.510|tag:lavfi.r128.LRA.high=-24.950
+pkt_pts=432000|tag:lavfi.r128.M=-26.243|tag:lavfi.r128.S=-24.955|tag:lavfi.r128.I=-25.182|tag:lavfi.r128.LRA=1.560|tag:lavfi.r128.LRA.low=-26.510|tag:lavfi.r128.LRA.high=-24.950
+pkt_pts=436800|tag:lavfi.r128.M=-25.959|tag:lavfi.r128.S=-24.896|tag:lavfi.r128.I=-25.190|tag:lavfi.r128.LRA=1.610|tag:lavfi.r128.LRA.low=-26.510|tag:lavfi.r128.LRA.high=-24.900
+pkt_pts=441600|tag:lavfi.r128.M=-24.870|tag:lavfi.r128.S=-24.785|tag:lavfi.r128.I=-25.186|tag:lavfi.r128.LRA=1.720|tag:lavfi.r128.LRA.low=-26.510|tag:lavfi.r128.LRA.high=-24.790
+pkt_pts=446400|tag:lavfi.r128.M=-24.644|tag:lavfi.r128.S=-24.696|tag:lavfi.r128.I=-25.180|tag:lavfi.r128.LRA=1.680|tag:lavfi.r128.LRA.low=-26.400|tag:lavfi.r128.LRA.high=-24.720
+pkt_pts=451200|tag:lavfi.r128.M=-24.141|tag:lavfi.r128.S=-24.643|tag:lavfi.r128.I=-25.167|tag:lavfi.r128.LRA=1.700|tag:lavfi.r128.LRA.low=-26.400|tag:lavfi.r128.LRA.high=-24.700
+pkt_pts=456000|tag:lavfi.r128.M=-23.990|tag:lavfi.r128.S=-24.634|tag:lavfi.r128.I=-25.153|tag:lavfi.r128.LRA=1.750|tag:lavfi.r128.LRA.low=-26.400|tag:lavfi.r128.LRA.high=-24.650
+pkt_pts=460800|tag:lavfi.r128.M=-24.020|tag:lavfi.r128.S=-24.487|tag:lavfi.r128.I=-25.139|tag:lavfi.r128.LRA=1.760|tag:lavfi.r128.LRA.low=-26.400|tag:lavfi.r128.LRA.high=-24.640
+pkt_pts=465600|tag:lavfi.r128.M=-24.627|tag:lavfi.r128.S=-24.442|tag:lavfi.r128.I=-25.133|tag:lavfi.r128.LRA=1.910|tag:lavfi.r128.LRA.low=-26.400|tag:lavfi.r128.LRA.high=-24.490
+pkt_pts=470400|tag:lavfi.r128.M=-25.115|tag:lavfi.r128.S=-24.394|tag:lavfi.r128.I=-25.133|tag:lavfi.r128.LRA=1.940|tag:lavfi.r128.LRA.low=-26.400|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=475200|tag:lavfi.r128.M=-25.672|tag:lavfi.r128.S=-24.456|tag:lavfi.r128.I=-25.138|tag:lavfi.r128.LRA=1.940|tag:lavfi.r128.LRA.low=-26.400|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=480000|tag:lavfi.r128.M=-26.632|tag:lavfi.r128.S=-24.490|tag:lavfi.r128.I=-25.151|tag:lavfi.r128.LRA=1.940|tag:lavfi.r128.LRA.low=-26.400|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=484800|tag:lavfi.r128.M=-25.996|tag:lavfi.r128.S=-24.416|tag:lavfi.r128.I=-25.159|tag:lavfi.r128.LRA=1.940|tag:lavfi.r128.LRA.low=-26.400|tag:lavfi.r128.LRA.high=-24.460
+pkt_pts=489600|tag:lavfi.r128.M=-24.937|tag:lavfi.r128.S=-24.426|tag:lavfi.r128.I=-25.157|tag:lavfi.r128.LRA=1.950|tag:lavfi.r128.LRA.low=-26.400|tag:lavfi.r128.LRA.high=-24.450
+pkt_pts=494400|tag:lavfi.r128.M=-22.685|tag:lavfi.r128.S=-24.098|tag:lavfi.r128.I=-25.124|tag:lavfi.r128.LRA=1.960|tag:lavfi.r128.LRA.low=-26.390|tag:lavfi.r128.LRA.high=-24.430
+pkt_pts=499200|tag:lavfi.r128.M=-21.913|tag:lavfi.r128.S=-24.036|tag:lavfi.r128.I=-25.078|tag:lavfi.r128.LRA=1.970|tag:lavfi.r128.LRA.low=-26.390|tag:lavfi.r128.LRA.high=-24.420
+pkt_pts=504000|tag:lavfi.r128.M=-21.524|tag:lavfi.r128.S=-24.013|tag:lavfi.r128.I=-25.025|tag:lavfi.r128.LRA=1.990|tag:lavfi.r128.LRA.low=-26.390|tag:lavfi.r128.LRA.high=-24.400
+pkt_pts=508800|tag:lavfi.r128.M=-21.267|tag:lavfi.r128.S=-23.821|tag:lavfi.r128.I=-24.968|tag:lavfi.r128.LRA=2.090|tag:lavfi.r128.LRA.low=-26.390|tag:lavfi.r128.LRA.high=-24.300
+pkt_pts=513600|tag:lavfi.r128.M=-22.730|tag:lavfi.r128.S=-23.942|tag:lavfi.r128.I=-24.940|tag:lavfi.r128.LRA=2.290|tag:lavfi.r128.LRA.low=-26.390|tag:lavfi.r128.LRA.high=-24.100
+pkt_pts=518400|tag:lavfi.r128.M=-23.373|tag:lavfi.r128.S=-23.927|tag:lavfi.r128.I=-24.923|tag:lavfi.r128.LRA=2.350|tag:lavfi.r128.LRA.low=-26.390|tag:lavfi.r128.LRA.high=-24.040
+pkt_pts=523200|tag:lavfi.r128.M=-24.172|tag:lavfi.r128.S=-24.045|tag:lavfi.r128.I=-24.915|tag:lavfi.r128.LRA=2.350|tag:lavfi.r128.LRA.low=-26.390|tag:lavfi.r128.LRA.high=-24.040
+pkt_pts=528000|tag:lavfi.r128.M=-25.060|tag:lavfi.r128.S=-24.247|tag:lavfi.r128.I=-24.916|tag:lavfi.r128.LRA=2.350|tag:lavfi.r128.LRA.low=-26.390|tag:lavfi.r128.LRA.high=-24.040
+pkt_pts=532800|tag:lavfi.r128.M=-24.555|tag:lavfi.r128.S=-24.232|tag:lavfi.r128.I=-24.913|tag:lavfi.r128.LRA=2.350|tag:lavfi.r128.LRA.low=-26.390|tag:lavfi.r128.LRA.high=-24.040
+pkt_pts=537600|tag:lavfi.r128.M=-25.070|tag:lavfi.r128.S=-24.306|tag:lavfi.r128.I=-24.915|tag:lavfi.r128.LRA=2.350|tag:lavfi.r128.LRA.low=-26.390|tag:lavfi.r128.LRA.high=-24.040
+pkt_pts=542400|tag:lavfi.r128.M=-25.104|tag:lavfi.r128.S=-24.332|tag:lavfi.r128.I=-24.916|tag:lavfi.r128.LRA=2.320|tag:lavfi.r128.LRA.low=-26.360|tag:lavfi.r128.LRA.high=-24.040
+pkt_pts=547200|tag:lavfi.r128.M=-25.813|tag:lavfi.r128.S=-24.411|tag:lavfi.r128.I=-24.924|tag:lavfi.r128.LRA=2.320|tag:lavfi.r128.LRA.low=-26.360|tag:lavfi.r128.LRA.high=-24.040
+pkt_pts=552000|tag:lavfi.r128.M=-25.950|tag:lavfi.r128.S=-24.409|tag:lavfi.r128.I=-24.932|tag:lavfi.r128.LRA=2.320|tag:lavfi.r128.LRA.low=-26.360|tag:lavfi.r128.LRA.high=-24.040
+pkt_pts=556800|tag:lavfi.r128.M=-25.759|tag:lavfi.r128.S=-24.402|tag:lavfi.r128.I=-24.938|tag:lavfi.r128.LRA=2.320|tag:lavfi.r128.LRA.low=-26.360|tag:lavfi.r128.LRA.high=-24.040
+pkt_pts=561600|tag:lavfi.r128.M=-25.702|tag:lavfi.r128.S=-24.385|tag:lavfi.r128.I=-24.944|tag:lavfi.r128.LRA=2.320|tag:lavfi.r128.LRA.low=-26.360|tag:lavfi.r128.LRA.high=-24.040
+pkt_pts=566400|tag:lavfi.r128.M=-24.540|tag:lavfi.r128.S=-24.245|tag:lavfi.r128.I=-24.941|tag:lavfi.r128.LRA=2.320|tag:lavfi.r128.LRA.low=-26.360|tag:lavfi.r128.LRA.high=-24.040
+pkt_pts=571200|tag:lavfi.r128.M=-23.455|tag:lavfi.r128.S=-24.093|tag:lavfi.r128.I=-24.926|tag:lavfi.r128.LRA=2.310|tag:lavfi.r128.LRA.low=-26.360|tag:lavfi.r128.LRA.high=-24.050
+pkt_pts=576000|tag:lavfi.r128.M=-22.161|tag:lavfi.r128.S=-23.848|tag:lavfi.r128.I=-24.893|tag:lavfi.r128.LRA=2.320|tag:lavfi.r128.LRA.low=-26.360|tag:lavfi.r128.LRA.high=-24.040
+pkt_pts=580800|tag:lavfi.r128.M=-21.752|tag:lavfi.r128.S=-23.772|tag:lavfi.r128.I=-24.855|tag:lavfi.r128.LRA=2.340|tag:lavfi.r128.LRA.low=-26.360|tag:lavfi.r128.LRA.high=-24.020
+pkt_pts=585600|tag:lavfi.r128.M=-21.869|tag:lavfi.r128.S=-23.772|tag:lavfi.r128.I=-24.819|tag:lavfi.r128.LRA=2.410|tag:lavfi.r128.LRA.low=-26.360|tag:lavfi.r128.LRA.high=-23.950
+pkt_pts=590400|tag:lavfi.r128.M=-22.553|tag:lavfi.r128.S=-23.788|tag:lavfi.r128.I=-24.795|tag:lavfi.r128.LRA=2.430|tag:lavfi.r128.LRA.low=-26.360|tag:lavfi.r128.LRA.high=-23.930
+pkt_pts=595200|tag:lavfi.r128.M=-23.563|tag:lavfi.r128.S=-23.771|tag:lavfi.r128.I=-24.783|tag:lavfi.r128.LRA=2.510|tag:lavfi.r128.LRA.low=-26.360|tag:lavfi.r128.LRA.high=-23.850
+pkt_pts=600000|tag:lavfi.r128.M=-23.930|tag:lavfi.r128.S=-23.764|tag:lavfi.r128.I=-24.776|tag:lavfi.r128.LRA=2.530|tag:lavfi.r128.LRA.low=-26.360|tag:lavfi.r128.LRA.high=-23.830
+pkt_pts=604800|tag:lavfi.r128.M=-24.599|tag:lavfi.r128.S=-23.841|tag:lavfi.r128.I=-24.774|tag:lavfi.r128.LRA=2.530|tag:lavfi.r128.LRA.low=-26.360|tag:lavfi.r128.LRA.high=-23.830
+pkt_pts=609600|tag:lavfi.r128.M=-24.476|tag:lavfi.r128.S=-23.772|tag:lavfi.r128.I=-24.772|tag:lavfi.r128.LRA=2.570|tag:lavfi.r128.LRA.low=-26.360|tag:lavfi.r128.LRA.high=-23.790
+pkt_pts=614400|tag:lavfi.r128.M=-24.380|tag:lavfi.r128.S=-23.694|tag:lavfi.r128.I=-24.769|tag:lavfi.r128.LRA=2.580|tag:lavfi.r128.LRA.low=-26.360|tag:lavfi.r128.LRA.high=-23.780
+pkt_pts=619200|tag:lavfi.r128.M=-23.440|tag:lavfi.r128.S=-23.520|tag:lavfi.r128.I=-24.756|tag:lavfi.r128.LRA=2.580|tag:lavfi.r128.LRA.low=-26.360|tag:lavfi.r128.LRA.high=-23.780
+pkt_pts=624000|tag:lavfi.r128.M=-22.290|tag:lavfi.r128.S=-23.347|tag:lavfi.r128.I=-24.731|tag:lavfi.r128.LRA=2.580|tag:lavfi.r128.LRA.low=-26.360|tag:lavfi.r128.LRA.high=-23.780
+pkt_pts=628800|tag:lavfi.r128.M=-21.092|tag:lavfi.r128.S=-23.100|tag:lavfi.r128.I=-24.687|tag:lavfi.r128.LRA=2.580|tag:lavfi.r128.LRA.low=-26.360|tag:lavfi.r128.LRA.high=-23.780
+pkt_pts=633600|tag:lavfi.r128.M=-20.607|tag:lavfi.r128.S=-23.007|tag:lavfi.r128.I=-24.635|tag:lavfi.r128.LRA=2.590|tag:lavfi.r128.LRA.low=-26.360|tag:lavfi.r128.LRA.high=-23.770
+pkt_pts=638400|tag:lavfi.r128.M=-20.608|tag:lavfi.r128.S=-23.110|tag:lavfi.r128.I=-24.585|tag:lavfi.r128.LRA=2.650|tag:lavfi.r128.LRA.low=-26.350|tag:lavfi.r128.LRA.high=-23.700
+pkt_pts=643200|tag:lavfi.r128.M=-20.622|tag:lavfi.r128.S=-23.077|tag:lavfi.r128.I=-24.536|tag:lavfi.r128.LRA=2.820|tag:lavfi.r128.LRA.low=-26.350|tag:lavfi.r128.LRA.high=-23.530
+pkt_pts=648000|tag:lavfi.r128.M=-21.543|tag:lavfi.r128.S=-23.104|tag:lavfi.r128.I=-24.504|tag:lavfi.r128.LRA=3.000|tag:lavfi.r128.LRA.low=-26.350|tag:lavfi.r128.LRA.high=-23.350
+pkt_pts=652800|tag:lavfi.r128.M=-22.071|tag:lavfi.r128.S=-23.156|tag:lavfi.r128.I=-24.480|tag:lavfi.r128.LRA=3.190|tag:lavfi.r128.LRA.low=-26.350|tag:lavfi.r128.LRA.high=-23.160
+pkt_pts=657600|tag:lavfi.r128.M=-22.327|tag:lavfi.r128.S=-23.049|tag:lavfi.r128.I=-24.459|tag:lavfi.r128.LRA=3.240|tag:lavfi.r128.LRA.low=-26.350|tag:lavfi.r128.LRA.high=-23.110
+pkt_pts=662400|tag:lavfi.r128.M=-22.810|tag:lavfi.r128.S=-23.003|tag:lavfi.r128.I=-24.445|tag:lavfi.r128.LRA=3.240|tag:lavfi.r128.LRA.low=-26.350|tag:lavfi.r128.LRA.high=-23.110
+pkt_pts=667200|tag:lavfi.r128.M=-22.676|tag:lavfi.r128.S=-22.921|tag:lavfi.r128.I=-24.429|tag:lavfi.r128.LRA=3.240|tag:lavfi.r128.LRA.low=-26.350|tag:lavfi.r128.LRA.high=-23.110
+pkt_pts=672000|tag:lavfi.r128.M=-22.942|tag:lavfi.r128.S=-22.927|tag:lavfi.r128.I=-24.416|tag:lavfi.r128.LRA=3.250|tag:lavfi.r128.LRA.low=-26.350|tag:lavfi.r128.LRA.high=-23.100
+pkt_pts=676800|tag:lavfi.r128.M=-23.165|tag:lavfi.r128.S=-22.897|tag:lavfi.r128.I=-24.406|tag:lavfi.r128.LRA=3.270|tag:lavfi.r128.LRA.low=-26.350|tag:lavfi.r128.LRA.high=-23.080
+pkt_pts=681600|tag:lavfi.r128.M=-23.339|tag:lavfi.r128.S=-22.830|tag:lavfi.r128.I=-24.397|tag:lavfi.r128.LRA=3.300|tag:lavfi.r128.LRA.low=-26.350|tag:lavfi.r128.LRA.high=-23.050
+pkt_pts=686400|tag:lavfi.r128.M=-23.672|tag:lavfi.r128.S=-22.786|tag:lavfi.r128.I=-24.392|tag:lavfi.r128.LRA=3.330|tag:lavfi.r128.LRA.low=-26.340|tag:lavfi.r128.LRA.high=-23.010
+pkt_pts=691200|tag:lavfi.r128.M=-23.306|tag:lavfi.r128.S=-22.701|tag:lavfi.r128.I=-24.383|tag:lavfi.r128.LRA=3.330|tag:lavfi.r128.LRA.low=-26.340|tag:lavfi.r128.LRA.high=-23.010
+pkt_pts=696000|tag:lavfi.r128.M=-23.384|tag:lavfi.r128.S=-22.672|tag:lavfi.r128.I=-24.375|tag:lavfi.r128.LRA=3.410|tag:lavfi.r128.LRA.low=-26.340|tag:lavfi.r128.LRA.high=-22.930
+pkt_pts=700800|tag:lavfi.r128.M=-23.531|tag:lavfi.r128.S=-22.637|tag:lavfi.r128.I=-24.369|tag:lavfi.r128.LRA=3.410|tag:lavfi.r128.LRA.low=-26.340|tag:lavfi.r128.LRA.high=-22.930
+pkt_pts=705600|tag:lavfi.r128.M=-23.311|tag:lavfi.r128.S=-22.574|tag:lavfi.r128.I=-24.361|tag:lavfi.r128.LRA=3.440|tag:lavfi.r128.LRA.low=-26.340|tag:lavfi.r128.LRA.high=-22.900
+pkt_pts=710400|tag:lavfi.r128.M=-23.705|tag:lavfi.r128.S=-22.621|tag:lavfi.r128.I=-24.356|tag:lavfi.r128.LRA=3.500|tag:lavfi.r128.LRA.low=-26.340|tag:lavfi.r128.LRA.high=-22.840
+pkt_pts=715200|tag:lavfi.r128.M=-24.104|tag:lavfi.r128.S=-22.740|tag:lavfi.r128.I=-24.354|tag:lavfi.r128.LRA=3.550|tag:lavfi.r128.LRA.low=-26.340|tag:lavfi.r128.LRA.high=-22.790
+pkt_pts=720000|tag:lavfi.r128.M=-24.403|tag:lavfi.r128.S=-22.905|tag:lavfi.r128.I=-24.355|tag:lavfi.r128.LRA=3.550|tag:lavfi.r128.LRA.low=-26.340|tag:lavfi.r128.LRA.high=-22.790
+pkt_pts=724800|tag:lavfi.r128.M=-24.337|tag:lavfi.r128.S=-22.900|tag:lavfi.r128.I=-24.354|tag:lavfi.r128.LRA=3.550|tag:lavfi.r128.LRA.low=-26.340|tag:lavfi.r128.LRA.high=-22.790
+pkt_pts=729600|tag:lavfi.r128.M=-23.538|tag:lavfi.r128.S=-22.846|tag:lavfi.r128.I=-24.348|tag:lavfi.r128.LRA=3.550|tag:lavfi.r128.LRA.low=-26.340|tag:lavfi.r128.LRA.high=-22.790
+pkt_pts=734400|tag:lavfi.r128.M=-22.774|tag:lavfi.r128.S=-22.770|tag:lavfi.r128.I=-24.336|tag:lavfi.r128.LRA=3.560|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.770
+pkt_pts=739200|tag:lavfi.r128.M=-21.589|tag:lavfi.r128.S=-22.628|tag:lavfi.r128.I=-24.311|tag:lavfi.r128.LRA=3.590|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.740
+pkt_pts=744000|tag:lavfi.r128.M=-21.003|tag:lavfi.r128.S=-22.482|tag:lavfi.r128.I=-24.279|tag:lavfi.r128.LRA=3.620|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.710
+pkt_pts=748800|tag:lavfi.r128.M=-21.057|tag:lavfi.r128.S=-22.384|tag:lavfi.r128.I=-24.248|tag:lavfi.r128.LRA=3.650|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.680
+pkt_pts=753600|tag:lavfi.r128.M=-20.378|tag:lavfi.r128.S=-22.196|tag:lavfi.r128.I=-24.208|tag:lavfi.r128.LRA=3.690|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.640
+pkt_pts=758400|tag:lavfi.r128.M=-20.735|tag:lavfi.r128.S=-22.147|tag:lavfi.r128.I=-24.174|tag:lavfi.r128.LRA=3.700|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.630
+pkt_pts=763200|tag:lavfi.r128.M=-21.502|tag:lavfi.r128.S=-22.228|tag:lavfi.r128.I=-24.150|tag:lavfi.r128.LRA=3.700|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.630
+pkt_pts=768000|tag:lavfi.r128.M=-21.158|tag:lavfi.r128.S=-22.211|tag:lavfi.r128.I=-24.123|tag:lavfi.r128.LRA=3.700|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.630
+pkt_pts=772800|tag:lavfi.r128.M=-22.252|tag:lavfi.r128.S=-22.375|tag:lavfi.r128.I=-24.109|tag:lavfi.r128.LRA=3.750|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.580
+pkt_pts=777600|tag:lavfi.r128.M=-22.753|tag:lavfi.r128.S=-22.481|tag:lavfi.r128.I=-24.099|tag:lavfi.r128.LRA=3.840|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.490
+pkt_pts=782400|tag:lavfi.r128.M=-22.726|tag:lavfi.r128.S=-22.565|tag:lavfi.r128.I=-24.089|tag:lavfi.r128.LRA=3.840|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.490
+pkt_pts=787200|tag:lavfi.r128.M=-23.676|tag:lavfi.r128.S=-22.655|tag:lavfi.r128.I=-24.086|tag:lavfi.r128.LRA=3.840|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.490
+pkt_pts=792000|tag:lavfi.r128.M=-23.719|tag:lavfi.r128.S=-22.660|tag:lavfi.r128.I=-24.084|tag:lavfi.r128.LRA=3.840|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.490
+pkt_pts=796800|tag:lavfi.r128.M=-23.765|tag:lavfi.r128.S=-22.692|tag:lavfi.r128.I=-24.082|tag:lavfi.r128.LRA=3.840|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.490
+pkt_pts=801600|tag:lavfi.r128.M=-22.760|tag:lavfi.r128.S=-22.624|tag:lavfi.r128.I=-24.073|tag:lavfi.r128.LRA=3.840|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.490
+pkt_pts=806400|tag:lavfi.r128.M=-21.532|tag:lavfi.r128.S=-22.468|tag:lavfi.r128.I=-24.052|tag:lavfi.r128.LRA=3.840|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.490
+pkt_pts=811200|tag:lavfi.r128.M=-21.329|tag:lavfi.r128.S=-22.455|tag:lavfi.r128.I=-24.029|tag:lavfi.r128.LRA=3.860|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.470
+pkt_pts=816000|tag:lavfi.r128.M=-20.701|tag:lavfi.r128.S=-22.338|tag:lavfi.r128.I=-24.000|tag:lavfi.r128.LRA=3.870|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.460
+pkt_pts=820800|tag:lavfi.r128.M=-20.533|tag:lavfi.r128.S=-22.218|tag:lavfi.r128.I=-23.969|tag:lavfi.r128.LRA=3.940|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.390
+pkt_pts=825600|tag:lavfi.r128.M=-21.049|tag:lavfi.r128.S=-22.151|tag:lavfi.r128.I=-23.944|tag:lavfi.r128.LRA=3.950|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.380
+pkt_pts=830400|tag:lavfi.r128.M=-21.066|tag:lavfi.r128.S=-22.110|tag:lavfi.r128.I=-23.920|tag:lavfi.r128.LRA=3.990|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.340
+pkt_pts=835200|tag:lavfi.r128.M=-20.990|tag:lavfi.r128.S=-22.023|tag:lavfi.r128.I=-23.896|tag:lavfi.r128.LRA=4.100|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.230
+pkt_pts=840000|tag:lavfi.r128.M=-21.658|tag:lavfi.r128.S=-22.007|tag:lavfi.r128.I=-23.879|tag:lavfi.r128.LRA=4.110|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.220
+pkt_pts=844800|tag:lavfi.r128.M=-21.649|tag:lavfi.r128.S=-21.928|tag:lavfi.r128.I=-23.863|tag:lavfi.r128.LRA=4.110|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.220
+pkt_pts=849600|tag:lavfi.r128.M=-22.120|tag:lavfi.r128.S=-21.973|tag:lavfi.r128.I=-23.850|tag:lavfi.r128.LRA=4.130|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.200
+pkt_pts=854400|tag:lavfi.r128.M=-23.297|tag:lavfi.r128.S=-21.984|tag:lavfi.r128.I=-23.847|tag:lavfi.r128.LRA=4.170|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.160
+pkt_pts=859200|tag:lavfi.r128.M=-22.936|tag:lavfi.r128.S=-21.898|tag:lavfi.r128.I=-23.841|tag:lavfi.r128.LRA=4.170|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.160
+pkt_pts=864000|tag:lavfi.r128.M=-22.360|tag:lavfi.r128.S=-21.736|tag:lavfi.r128.I=-23.831|tag:lavfi.r128.LRA=4.180|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.150
+pkt_pts=868800|tag:lavfi.r128.M=-20.997|tag:lavfi.r128.S=-21.601|tag:lavfi.r128.I=-23.809|tag:lavfi.r128.LRA=4.220|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.110
+pkt_pts=873600|tag:lavfi.r128.M=-20.852|tag:lavfi.r128.S=-21.651|tag:lavfi.r128.I=-23.786|tag:lavfi.r128.LRA=4.300|tag:lavfi.r128.LRA.low=-26.330|tag:lavfi.r128.LRA.high=-22.030
+pkt_pts=878400|tag:lavfi.r128.M=-21.194|tag:lavfi.r128.S=-21.695|tag:lavfi.r128.I=-23.766|tag:lavfi.r128.LRA=4.310|tag:lavfi.r128.LRA.low=-26.320|tag:lavfi.r128.LRA.high=-22.010
+pkt_pts=883200|tag:lavfi.r128.M=-22.598|tag:lavfi.r128.S=-21.862|tag:lavfi.r128.I=-23.759|tag:lavfi.r128.LRA=4.330|tag:lavfi.r128.LRA.low=-26.320|tag:lavfi.r128.LRA.high=-21.990
+pkt_pts=888000|tag:lavfi.r128.M=-24.897|tag:lavfi.r128.S=-22.013|tag:lavfi.r128.I=-23.764|tag:lavfi.r128.LRA=4.330|tag:lavfi.r128.LRA.low=-26.320|tag:lavfi.r128.LRA.high=-21.990
+pkt_pts=892800|tag:lavfi.r128.M=-25.212|tag:lavfi.r128.S=-22.080|tag:lavfi.r128.I=-23.771|tag:lavfi.r128.LRA=4.330|tag:lavfi.r128.LRA.low=-26.320|tag:lavfi.r128.LRA.high=-21.990
+pkt_pts=897600|tag:lavfi.r128.M=-24.334|tag:lavfi.r128.S=-22.191|tag:lavfi.r128.I=-23.774|tag:lavfi.r128.LRA=4.330|tag:lavfi.r128.LRA.low=-26.320|tag:lavfi.r128.LRA.high=-21.990
+pkt_pts=902400|tag:lavfi.r128.M=-22.984|tag:lavfi.r128.S=-22.176|tag:lavfi.r128.I=-23.769|tag:lavfi.r128.LRA=4.330|tag:lavfi.r128.LRA.low=-26.320|tag:lavfi.r128.LRA.high=-21.990
+pkt_pts=907200|tag:lavfi.r128.M=-21.388|tag:lavfi.r128.S=-21.996|tag:lavfi.r128.I=-23.753|tag:lavfi.r128.LRA=4.330|tag:lavfi.r128.LRA.low=-26.320|tag:lavfi.r128.LRA.high=-21.990
+pkt_pts=912000|tag:lavfi.r128.M=-20.307|tag:lavfi.r128.S=-21.928|tag:lavfi.r128.I=-23.725|tag:lavfi.r128.LRA=4.340|tag:lavfi.r128.LRA.low=-26.320|tag:lavfi.r128.LRA.high=-21.980
+pkt_pts=916800|tag:lavfi.r128.M=-20.094|tag:lavfi.r128.S=-21.838|tag:lavfi.r128.I=-23.695|tag:lavfi.r128.LRA=4.390|tag:lavfi.r128.LRA.low=-26.320|tag:lavfi.r128.LRA.high=-21.930
+pkt_pts=921600|tag:lavfi.r128.M=-20.032|tag:lavfi.r128.S=-21.756|tag:lavfi.r128.I=-23.665|tag:lavfi.r128.LRA=4.390|tag:lavfi.r128.LRA.low=-26.320|tag:lavfi.r128.LRA.high=-21.930
+pkt_pts=926400|tag:lavfi.r128.M=-20.060|tag:lavfi.r128.S=-21.599|tag:lavfi.r128.I=-23.635|tag:lavfi.r128.LRA=4.420|tag:lavfi.r128.LRA.low=-26.320|tag:lavfi.r128.LRA.high=-21.900
+pkt_pts=931200|tag:lavfi.r128.M=-20.372|tag:lavfi.r128.S=-21.507|tag:lavfi.r128.I=-23.610|tag:lavfi.r128.LRA=4.450|tag:lavfi.r128.LRA.low=-26.320|tag:lavfi.r128.LRA.high=-21.870
+pkt_pts=936000|tag:lavfi.r128.M=-20.643|tag:lavfi.r128.S=-21.468|tag:lavfi.r128.I=-23.588|tag:lavfi.r128.LRA=4.480|tag:lavfi.r128.LRA.low=-26.320|tag:lavfi.r128.LRA.high=-21.840
+pkt_pts=940800|tag:lavfi.r128.M=-20.647|tag:lavfi.r128.S=-21.389|tag:lavfi.r128.I=-23.567|tag:lavfi.r128.LRA=4.560|tag:lavfi.r128.LRA.low=-26.320|tag:lavfi.r128.LRA.high=-21.760
+pkt_pts=945600|tag:lavfi.r128.M=-21.341|tag:lavfi.r128.S=-21.431|tag:lavfi.r128.I=-23.552|tag:lavfi.r128.LRA=4.580|tag:lavfi.r128.LRA.low=-26.320|tag:lavfi.r128.LRA.high=-21.740
+pkt_pts=950400|tag:lavfi.r128.M=-21.704|tag:lavfi.r128.S=-21.530|tag:lavfi.r128.I=-23.540|tag:lavfi.r128.LRA=4.620|tag:lavfi.r128.LRA.low=-26.320|tag:lavfi.r128.LRA.high=-21.700
+pkt_pts=955200|tag:lavfi.r128.M=-21.778|tag:lavfi.r128.S=-21.527|tag:lavfi.r128.I=-23.529|tag:lavfi.r128.LRA=4.620|tag:lavfi.r128.LRA.low=-26.320|tag:lavfi.r128.LRA.high=-21.700
+pkt_pts=960000|tag:lavfi.r128.M=-22.390|tag:lavfi.r128.S=-21.613|tag:lavfi.r128.I=-23.523|tag:lavfi.r128.LRA=4.660|tag:lavfi.r128.LRA.low=-26.320|tag:lavfi.r128.LRA.high=-21.660
+pkt_pts=964800|tag:lavfi.r128.M=-22.355|tag:lavfi.r128.S=-21.682|tag:lavfi.r128.I=-23.516|tag:lavfi.r128.LRA=4.660|tag:lavfi.r128.LRA.low=-26.320|tag:lavfi.r128.LRA.high=-21.660
+pkt_pts=969600|tag:lavfi.r128.M=-22.697|tag:lavfi.r128.S=-21.739|tag:lavfi.r128.I=-23.512|tag:lavfi.r128.LRA=4.660|tag:lavfi.r128.LRA.low=-26.320|tag:lavfi.r128.LRA.high=-21.660
+pkt_pts=974400|tag:lavfi.r128.M=-22.814|tag:lavfi.r128.S=-21.745|tag:lavfi.r128.I=-23.508|tag:lavfi.r128.LRA=4.650|tag:lavfi.r128.LRA.low=-26.310|tag:lavfi.r128.LRA.high=-21.660
+pkt_pts=979200|tag:lavfi.r128.M=-22.749|tag:lavfi.r128.S=-21.842|tag:lavfi.r128.I=-23.504|tag:lavfi.r128.LRA=4.650|tag:lavfi.r128.LRA.low=-26.310|tag:lavfi.r128.LRA.high=-21.660
+pkt_pts=984000|tag:lavfi.r128.M=-23.155|tag:lavfi.r128.S=-21.856|tag:lavfi.r128.I=-23.502|tag:lavfi.r128.LRA=4.650|tag:lavfi.r128.LRA.low=-26.310|tag:lavfi.r128.LRA.high=-21.660
+pkt_pts=988800|tag:lavfi.r128.M=-22.927|tag:lavfi.r128.S=-21.893|tag:lavfi.r128.I=-23.499|tag:lavfi.r128.LRA=4.650|tag:lavfi.r128.LRA.low=-26.310|tag:lavfi.r128.LRA.high=-21.660
+pkt_pts=993600|tag:lavfi.r128.M=-22.583|tag:lavfi.r128.S=-21.800|tag:lavfi.r128.I=-23.494|tag:lavfi.r128.LRA=4.650|tag:lavfi.r128.LRA.low=-26.310|tag:lavfi.r128.LRA.high=-21.660
+pkt_pts=998400|tag:lavfi.r128.M=-22.022|tag:lavfi.r128.S=-21.702|tag:lavfi.r128.I=-23.486|tag:lavfi.r128.LRA=4.650|tag:lavfi.r128.LRA.low=-26.310|tag:lavfi.r128.LRA.high=-21.660
+pkt_pts=1003200|tag:lavfi.r128.M=-21.852|tag:lavfi.r128.S=-21.729|tag:lavfi.r128.I=-23.476|tag:lavfi.r128.LRA=4.650|tag:lavfi.r128.LRA.low=-26.310|tag:lavfi.r128.LRA.high=-21.660
+pkt_pts=1008000|tag:lavfi.r128.M=-21.746|tag:lavfi.r128.S=-21.814|tag:lavfi.r128.I=-23.466|tag:lavfi.r128.LRA=4.650|tag:lavfi.r128.LRA.low=-26.310|tag:lavfi.r128.LRA.high=-21.660
+pkt_pts=1012800|tag:lavfi.r128.M=-21.751|tag:lavfi.r128.S=-21.912|tag:lavfi.r128.I=-23.456|tag:lavfi.r128.LRA=4.650|tag:lavfi.r128.LRA.low=-26.310|tag:lavfi.r128.LRA.high=-21.660
+pkt_pts=1017600|tag:lavfi.r128.M=-21.891|tag:lavfi.r128.S=-21.855|tag:lavfi.r128.I=-23.447|tag:lavfi.r128.LRA=4.650|tag:lavfi.r128.LRA.low=-26.310|tag:lavfi.r128.LRA.high=-21.660
+pkt_pts=1022400|tag:lavfi.r128.M=-21.392|tag:lavfi.r128.S=-21.759|tag:lavfi.r128.I=-23.435|tag:lavfi.r128.LRA=4.640|tag:lavfi.r128.LRA.low=-26.300|tag:lavfi.r128.LRA.high=-21.660
+pkt_pts=1027200|tag:lavfi.r128.M=-21.375|tag:lavfi.r128.S=-21.660|tag:lavfi.r128.I=-23.422|tag:lavfi.r128.LRA=4.640|tag:lavfi.r128.LRA.low=-26.300|tag:lavfi.r128.LRA.high=-21.660
+pkt_pts=1032000|tag:lavfi.r128.M=-21.806|tag:lavfi.r128.S=-21.620|tag:lavfi.r128.I=-23.413|tag:lavfi.r128.LRA=4.680|tag:lavfi.r128.LRA.low=-26.300|tag:lavfi.r128.LRA.high=-21.620
+pkt_pts=1036800|tag:lavfi.r128.M=-21.826|tag:lavfi.r128.S=-21.550|tag:lavfi.r128.I=-23.404|tag:lavfi.r128.LRA=4.680|tag:lavfi.r128.LRA.low=-26.300|tag:lavfi.r128.LRA.high=-21.620
+pkt_pts=1041600|tag:lavfi.r128.M=-22.447|tag:lavfi.r128.S=-21.588|tag:lavfi.r128.I=-23.399|tag:lavfi.r128.LRA=4.690|tag:lavfi.r128.LRA.low=-26.300|tag:lavfi.r128.LRA.high=-21.610
+pkt_pts=1046400|tag:lavfi.r128.M=-22.717|tag:lavfi.r128.S=-21.633|tag:lavfi.r128.I=-23.396|tag:lavfi.r128.LRA=4.690|tag:lavfi.r128.LRA.low=-26.300|tag:lavfi.r128.LRA.high=-21.610
+pkt_pts=1051200|tag:lavfi.r128.M=-23.325|tag:lavfi.r128.S=-21.845|tag:lavfi.r128.I=-23.396|tag:lavfi.r128.LRA=4.680|tag:lavfi.r128.LRA.low=-26.300|tag:lavfi.r128.LRA.high=-21.620
+pkt_pts=1056000|tag:lavfi.r128.M=-23.105|tag:lavfi.r128.S=-21.933|tag:lavfi.r128.I=-23.394|tag:lavfi.r128.LRA=4.680|tag:lavfi.r128.LRA.low=-26.300|tag:lavfi.r128.LRA.high=-21.620
+pkt_pts=1060800|tag:lavfi.r128.M=-23.086|tag:lavfi.r128.S=-22.015|tag:lavfi.r128.I=-23.393|tag:lavfi.r128.LRA=4.680|tag:lavfi.r128.LRA.low=-26.300|tag:lavfi.r128.LRA.high=-21.620
+pkt_pts=1065600|tag:lavfi.r128.M=-22.412|tag:lavfi.r128.S=-22.001|tag:lavfi.r128.I=-23.388|tag:lavfi.r128.LRA=4.680|tag:lavfi.r128.LRA.low=-26.300|tag:lavfi.r128.LRA.high=-21.620
+pkt_pts=1070400|tag:lavfi.r128.M=-20.845|tag:lavfi.r128.S=-21.992|tag:lavfi.r128.I=-23.372|tag:lavfi.r128.LRA=4.650|tag:lavfi.r128.LRA.low=-26.270|tag:lavfi.r128.LRA.high=-21.620
+pkt_pts=1075200|tag:lavfi.r128.M=-20.738|tag:lavfi.r128.S=-22.001|tag:lavfi.r128.I=-23.356|tag:lavfi.r128.LRA=4.650|tag:lavfi.r128.LRA.low=-26.270|tag:lavfi.r128.LRA.high=-21.620
+pkt_pts=1080000|tag:lavfi.r128.M=-20.165|tag:lavfi.r128.S=-21.924|tag:lavfi.r128.I=-23.335|tag:lavfi.r128.LRA=4.650|tag:lavfi.r128.LRA.low=-26.270|tag:lavfi.r128.LRA.high=-21.620
+pkt_pts=1084800|tag:lavfi.r128.M=-19.829|tag:lavfi.r128.S=-21.840|tag:lavfi.r128.I=-23.311|tag:lavfi.r128.LRA=4.650|tag:lavfi.r128.LRA.low=-26.270|tag:lavfi.r128.LRA.high=-21.620
+pkt_pts=1089600|tag:lavfi.r128.M=-20.699|tag:lavfi.r128.S=-21.887|tag:lavfi.r128.I=-23.295|tag:lavfi.r128.LRA=4.650|tag:lavfi.r128.LRA.low=-26.270|tag:lavfi.r128.LRA.high=-21.620
+pkt_pts=1094400|tag:lavfi.r128.M=-20.677|tag:lavfi.r128.S=-21.838|tag:lavfi.r128.I=-23.279|tag:lavfi.r128.LRA=4.650|tag:lavfi.r128.LRA.low=-26.270|tag:lavfi.r128.LRA.high=-21.620
+pkt_pts=1099200|tag:lavfi.r128.M=-20.121|tag:lavfi.r128.S=-21.654|tag:lavfi.r128.I=-23.259|tag:lavfi.r128.LRA=4.650|tag:lavfi.r128.LRA.low=-26.270|tag:lavfi.r128.LRA.high=-21.620
+pkt_pts=1104000|tag:lavfi.r128.M=-20.893|tag:lavfi.r128.S=-21.635|tag:lavfi.r128.I=-23.245|tag:lavfi.r128.LRA=4.650|tag:lavfi.r128.LRA.low=-26.270|tag:lavfi.r128.LRA.high=-21.620
+pkt_pts=1108800|tag:lavfi.r128.M=-19.603|tag:lavfi.r128.S=-21.450|tag:lavfi.r128.I=-23.220|tag:lavfi.r128.LRA=4.660|tag:lavfi.r128.LRA.low=-26.270|tag:lavfi.r128.LRA.high=-21.610
+pkt_pts=1113600|tag:lavfi.r128.M=-19.675|tag:lavfi.r128.S=-21.385|tag:lavfi.r128.I=-23.197|tag:lavfi.r128.LRA=4.670|tag:lavfi.r128.LRA.low=-26.270|tag:lavfi.r128.LRA.high=-21.600
+pkt_pts=1118400|tag:lavfi.r128.M=-20.415|tag:lavfi.r128.S=-21.339|tag:lavfi.r128.I=-23.180|tag:lavfi.r128.LRA=4.660|tag:lavfi.r128.LRA.low=-26.250|tag:lavfi.r128.LRA.high=-21.590
+pkt_pts=1123200|tag:lavfi.r128.M=-20.446|tag:lavfi.r128.S=-21.333|tag:lavfi.r128.I=-23.164|tag:lavfi.r128.LRA=4.690|tag:lavfi.r128.LRA.low=-26.250|tag:lavfi.r128.LRA.high=-21.560
+pkt_pts=1128000|tag:lavfi.r128.M=-21.361|tag:lavfi.r128.S=-21.254|tag:lavfi.r128.I=-23.154|tag:lavfi.r128.LRA=4.720|tag:lavfi.r128.LRA.low=-26.250|tag:lavfi.r128.LRA.high=-21.530
+pkt_pts=1132800|tag:lavfi.r128.M=-21.725|tag:lavfi.r128.S=-21.257|tag:lavfi.r128.I=-23.147|tag:lavfi.r128.LRA=4.720|tag:lavfi.r128.LRA.low=-26.250|tag:lavfi.r128.LRA.high=-21.530
+pkt_pts=1137600|tag:lavfi.r128.M=-22.311|tag:lavfi.r128.S=-21.311|tag:lavfi.r128.I=-23.143|tag:lavfi.r128.LRA=4.740|tag:lavfi.r128.LRA.low=-26.250|tag:lavfi.r128.LRA.high=-21.510
+pkt_pts=1142400|tag:lavfi.r128.M=-21.973|tag:lavfi.r128.S=-21.327|tag:lavfi.r128.I=-23.137|tag:lavfi.r128.LRA=4.780|tag:lavfi.r128.LRA.low=-26.250|tag:lavfi.r128.LRA.high=-21.470
+pkt_pts=1147200|tag:lavfi.r128.M=-22.032|tag:lavfi.r128.S=-21.275|tag:lavfi.r128.I=-23.132|tag:lavfi.r128.LRA=4.780|tag:lavfi.r128.LRA.low=-26.250|tag:lavfi.r128.LRA.high=-21.470
+pkt_pts=1152000|tag:lavfi.r128.M=-21.631|tag:lavfi.r128.S=-21.243|tag:lavfi.r128.I=-23.125|tag:lavfi.r128.LRA=4.800|tag:lavfi.r128.LRA.low=-26.250|tag:lavfi.r128.LRA.high=-21.450
+pkt_pts=1156800|tag:lavfi.r128.M=-20.494|tag:lavfi.r128.S=-21.139|tag:lavfi.r128.I=-23.110|tag:lavfi.r128.LRA=4.810|tag:lavfi.r128.LRA.low=-26.250|tag:lavfi.r128.LRA.high=-21.440
+pkt_pts=1161600|tag:lavfi.r128.M=-19.902|tag:lavfi.r128.S=-21.041|tag:lavfi.r128.I=-23.090|tag:lavfi.r128.LRA=4.860|tag:lavfi.r128.LRA.low=-26.250|tag:lavfi.r128.LRA.high=-21.390
+pkt_pts=1166400|tag:lavfi.r128.M=-20.221|tag:lavfi.r128.S=-21.104|tag:lavfi.r128.I=-23.073|tag:lavfi.r128.LRA=4.850|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.390
+pkt_pts=1171200|tag:lavfi.r128.M=-20.245|tag:lavfi.r128.S=-21.080|tag:lavfi.r128.I=-23.057|tag:lavfi.r128.LRA=4.900|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.340
+pkt_pts=1176000|tag:lavfi.r128.M=-21.470|tag:lavfi.r128.S=-21.099|tag:lavfi.r128.I=-23.049|tag:lavfi.r128.LRA=4.900|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.340
+pkt_pts=1180800|tag:lavfi.r128.M=-22.623|tag:lavfi.r128.S=-21.123|tag:lavfi.r128.I=-23.047|tag:lavfi.r128.LRA=4.910|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.330
+pkt_pts=1185600|tag:lavfi.r128.M=-22.312|tag:lavfi.r128.S=-21.090|tag:lavfi.r128.I=-23.044|tag:lavfi.r128.LRA=4.920|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.320
+pkt_pts=1190400|tag:lavfi.r128.M=-22.937|tag:lavfi.r128.S=-21.099|tag:lavfi.r128.I=-23.043|tag:lavfi.r128.LRA=4.960|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.280
+pkt_pts=1195200|tag:lavfi.r128.M=-22.401|tag:lavfi.r128.S=-21.017|tag:lavfi.r128.I=-23.041|tag:lavfi.r128.LRA=4.980|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.260
+pkt_pts=1200000|tag:lavfi.r128.M=-22.004|tag:lavfi.r128.S=-21.018|tag:lavfi.r128.I=-23.036|tag:lavfi.r128.LRA=4.980|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.260
+pkt_pts=1204800|tag:lavfi.r128.M=-22.416|tag:lavfi.r128.S=-21.030|tag:lavfi.r128.I=-23.033|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1209600|tag:lavfi.r128.M=-22.481|tag:lavfi.r128.S=-21.106|tag:lavfi.r128.I=-23.031|tag:lavfi.r128.LRA=5.100|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.140
+pkt_pts=1214400|tag:lavfi.r128.M=-22.841|tag:lavfi.r128.S=-21.245|tag:lavfi.r128.I=-23.030|tag:lavfi.r128.LRA=5.100|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.140
+pkt_pts=1219200|tag:lavfi.r128.M=-23.663|tag:lavfi.r128.S=-21.332|tag:lavfi.r128.I=-23.033|tag:lavfi.r128.LRA=5.100|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.140
+pkt_pts=1224000|tag:lavfi.r128.M=-22.728|tag:lavfi.r128.S=-21.357|tag:lavfi.r128.I=-23.031|tag:lavfi.r128.LRA=5.100|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.140
+pkt_pts=1228800|tag:lavfi.r128.M=-23.016|tag:lavfi.r128.S=-21.530|tag:lavfi.r128.I=-23.031|tag:lavfi.r128.LRA=5.100|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.140
+pkt_pts=1233600|tag:lavfi.r128.M=-22.934|tag:lavfi.r128.S=-21.518|tag:lavfi.r128.I=-23.031|tag:lavfi.r128.LRA=5.100|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.140
+pkt_pts=1238400|tag:lavfi.r128.M=-21.320|tag:lavfi.r128.S=-21.426|tag:lavfi.r128.I=-23.023|tag:lavfi.r128.LRA=5.100|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.140
+pkt_pts=1243200|tag:lavfi.r128.M=-21.282|tag:lavfi.r128.S=-21.541|tag:lavfi.r128.I=-23.015|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1248000|tag:lavfi.r128.M=-20.639|tag:lavfi.r128.S=-21.490|tag:lavfi.r128.I=-23.002|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1252800|tag:lavfi.r128.M=-20.626|tag:lavfi.r128.S=-21.711|tag:lavfi.r128.I=-22.990|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1257600|tag:lavfi.r128.M=-21.333|tag:lavfi.r128.S=-21.710|tag:lavfi.r128.I=-22.982|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1262400|tag:lavfi.r128.M=-21.384|tag:lavfi.r128.S=-21.694|tag:lavfi.r128.I=-22.975|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1267200|tag:lavfi.r128.M=-21.410|tag:lavfi.r128.S=-21.639|tag:lavfi.r128.I=-22.968|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1272000|tag:lavfi.r128.M=-21.461|tag:lavfi.r128.S=-21.725|tag:lavfi.r128.I=-22.961|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1276800|tag:lavfi.r128.M=-21.846|tag:lavfi.r128.S=-21.726|tag:lavfi.r128.I=-22.956|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1281600|tag:lavfi.r128.M=-22.482|tag:lavfi.r128.S=-21.713|tag:lavfi.r128.I=-22.954|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1286400|tag:lavfi.r128.M=-22.681|tag:lavfi.r128.S=-21.720|tag:lavfi.r128.I=-22.953|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1291200|tag:lavfi.r128.M=-22.718|tag:lavfi.r128.S=-21.804|tag:lavfi.r128.I=-22.952|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1296000|tag:lavfi.r128.M=-22.849|tag:lavfi.r128.S=-21.873|tag:lavfi.r128.I=-22.952|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1300800|tag:lavfi.r128.M=-23.041|tag:lavfi.r128.S=-22.068|tag:lavfi.r128.I=-22.952|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1305600|tag:lavfi.r128.M=-23.898|tag:lavfi.r128.S=-22.285|tag:lavfi.r128.I=-22.956|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1310400|tag:lavfi.r128.M=-24.280|tag:lavfi.r128.S=-22.343|tag:lavfi.r128.I=-22.960|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1315200|tag:lavfi.r128.M=-25.072|tag:lavfi.r128.S=-22.479|tag:lavfi.r128.I=-22.966|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1320000|tag:lavfi.r128.M=-25.530|tag:lavfi.r128.S=-22.491|tag:lavfi.r128.I=-22.973|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1324800|tag:lavfi.r128.M=-25.269|tag:lavfi.r128.S=-22.536|tag:lavfi.r128.I=-22.980|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1329600|tag:lavfi.r128.M=-25.127|tag:lavfi.r128.S=-22.630|tag:lavfi.r128.I=-22.986|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1334400|tag:lavfi.r128.M=-24.909|tag:lavfi.r128.S=-22.673|tag:lavfi.r128.I=-22.991|tag:lavfi.r128.LRA=4.990|tag:lavfi.r128.LRA.low=-26.240|tag:lavfi.r128.LRA.high=-21.250
+pkt_pts=1339200
diff --git a/tests/ref/fate/filter-metadata-scenedetect b/tests/ref/fate/filter-metadata-scenedetect
index dd5e3a4..67251df 100644
--- a/tests/ref/fate/filter-metadata-scenedetect
+++ b/tests/ref/fate/filter-metadata-scenedetect
@@ -1,10 +1,10 @@
-video|1|1620|2.700000|1620|2.700000|1|0.001667|155751|N/A|320|240|rgb24|1:1|I|0|0|0|0|0|0|1.000000
-video|1|4140|6.900000|4140|6.900000|1|0.001667|808205|N/A|320|240|rgb24|1:1|I|0|0|0|0|0|0|0.880000
-video|1|5800|9.666667|5800|9.666667|1|0.001667|928577|N/A|320|240|rgb24|1:1|I|0|0|0|0|0|0|1.000000
-video|1|6720|11.200000|6720|11.200000|1|0.001667|1027359|N/A|320|240|rgb24|1:1|I|0|0|0|0|0|0|0.460000
-video|1|8160|13.600000|8160|13.600000|1|0.001667|1247869|N/A|320|240|rgb24|1:1|I|0|0|0|0|0|0|1.000000
-video|1|9760|16.266667|9760|16.266667|1|0.001667|1337561|N/A|320|240|rgb24|1:1|I|0|0|0|0|0|0|1.000000
-video|1|14080|23.466667|14080|23.466667|1|0.001667|1994159|N/A|320|240|rgb24|1:1|I|0|0|0|0|0|0|0.840000
-video|1|15700|26.166667|15700|26.166667|1|0.001667|2145781|N/A|320|240|rgb24|1:1|I|0|0|0|0|0|0|1.000000
-video|1|18500|30.833333|18500|30.833333|1|0.001667|2507729|N/A|320|240|rgb24|1:1|I|0|0|0|0|0|0|0.470000
-video|1|21760|36.266667|21760|36.266667|1|0.001667|2932267|N/A|320|240|rgb24|1:1|I|0|0|0|0|0|0|1.000000
+pkt_pts=1620|tag:lavfi.scene_score=1.000000
+pkt_pts=4140|tag:lavfi.scene_score=0.880000
+pkt_pts=5800|tag:lavfi.scene_score=1.000000
+pkt_pts=6720|tag:lavfi.scene_score=0.460000
+pkt_pts=8160|tag:lavfi.scene_score=1.000000
+pkt_pts=9760|tag:lavfi.scene_score=1.000000
+pkt_pts=14080|tag:lavfi.scene_score=0.840000
+pkt_pts=15700|tag:lavfi.scene_score=1.000000
+pkt_pts=18500|tag:lavfi.scene_score=0.470000
+pkt_pts=21760|tag:lavfi.scene_score=1.000000
diff --git a/tests/ref/fate/filter-metadata-silencedetect b/tests/ref/fate/filter-metadata-silencedetect
index ee42ec0..4161287 100644
--- a/tests/ref/fate/filter-metadata-silencedetect
+++ b/tests/ref/fate/filter-metadata-silencedetect
@@ -1,512 +1,512 @@
-audio|1|0|0.000000|0|0.000000|320|0.020000|44|2560|dbl|320|1|mono
-audio|1|320|0.020000|320|0.020000|320|0.020000|77|2560|dbl|320|1|mono
-audio|1|640|0.040000|640|0.040000|320|0.020000|110|2560|dbl|320|1|mono
-audio|1|960|0.060000|960|0.060000|320|0.020000|143|2560|dbl|320|1|mono
-audio|1|1280|0.080000|1280|0.080000|320|0.020000|176|2560|dbl|320|1|mono
-audio|1|1600|0.100000|1600|0.100000|320|0.020000|209|2560|dbl|320|1|mono
-audio|1|1920|0.120000|1920|0.120000|324|0.020250|242|2560|dbl|320|1|mono|0.02
-audio|1|2240|0.140000|2240|0.140000|320|0.020000|275|2560|dbl|320|1|mono
-audio|1|2560|0.160000|2560|0.160000|328|0.020500|308|2560|dbl|320|1|mono|0.16|0.14
-audio|1|2880|0.180000|2880|0.180000|320|0.020000|341|2560|dbl|320|1|mono
-audio|1|3200|0.200000|3200|0.200000|320|0.020000|374|2560|dbl|320|1|mono
-audio|1|3520|0.220000|3520|0.220000|320|0.020000|407|2560|dbl|320|1|mono
-audio|1|3840|0.240000|3840|0.240000|320|0.020000|440|2560|dbl|320|1|mono
-audio|1|4160|0.260000|4160|0.260000|320|0.020000|473|2560|dbl|320|1|mono
-audio|1|4480|0.280000|4480|0.280000|320|0.020000|506|2560|dbl|320|1|mono
-audio|1|4800|0.300000|4800|0.300000|320|0.020000|539|2560|dbl|320|1|mono
-audio|1|5120|0.320000|5120|0.320000|320|0.020000|572|2560|dbl|320|1|mono
-audio|1|5440|0.340000|5440|0.340000|320|0.020000|605|2560|dbl|320|1|mono
-audio|1|5760|0.360000|5760|0.360000|331|0.020688|638|2560|dbl|320|1|mono|0.26|0.36|0.1
-audio|1|6080|0.380000|6080|0.380000|320|0.020000|671|2560|dbl|320|1|mono
-audio|1|6400|0.400000|6400|0.400000|320|0.020000|704|2560|dbl|320|1|mono
-audio|1|6720|0.420000|6720|0.420000|320|0.020000|737|2560|dbl|320|1|mono
-audio|1|7040|0.440000|7040|0.440000|320|0.020000|770|2560|dbl|320|1|mono
-audio|1|7360|0.460000|7360|0.460000|320|0.020000|803|2560|dbl|320|1|mono
-audio|1|7680|0.480000|7680|0.480000|320|0.020000|836|2560|dbl|320|1|mono
-audio|1|8000|0.500000|8000|0.500000|320|0.020000|869|2560|dbl|320|1|mono
-audio|1|8320|0.520000|8320|0.520000|320|0.020000|902|2560|dbl|320|1|mono
-audio|1|8640|0.540000|8640|0.540000|320|0.020000|935|2560|dbl|320|1|mono
-audio|1|8960|0.560000|8960|0.560000|320|0.020000|968|2560|dbl|320|1|mono
-audio|1|9280|0.580000|9280|0.580000|320|0.020000|1001|2560|dbl|320|1|mono
-audio|1|9600|0.600000|9600|0.600000|320|0.020000|1034|2560|dbl|320|1|mono
-audio|1|9920|0.620000|9920|0.620000|320|0.020000|1067|2560|dbl|320|1|mono
-audio|1|10240|0.640000|10240|0.640000|320|0.020000|1100|2560|dbl|320|1|mono
-audio|1|10560|0.660000|10560|0.660000|320|0.020000|1133|2560|dbl|320|1|mono
-audio|1|10880|0.680000|10880|0.680000|320|0.020000|1166|2560|dbl|320|1|mono
-audio|1|11200|0.700000|11200|0.700000|320|0.020000|1199|2560|dbl|320|1|mono
-audio|1|11520|0.720000|11520|0.720000|320|0.020000|1232|2560|dbl|320|1|mono
-audio|1|11840|0.740000|11840|0.740000|320|0.020000|1265|2560|dbl|320|1|mono
-audio|1|12160|0.760000|12160|0.760000|320|0.020000|1298|2560|dbl|320|1|mono
-audio|1|12480|0.780000|12480|0.780000|320|0.020000|1331|2560|dbl|320|1|mono
-audio|1|12800|0.800000|12800|0.800000|320|0.020000|1364|2560|dbl|320|1|mono
-audio|1|13120|0.820000|13120|0.820000|320|0.020000|1397|2560|dbl|320|1|mono
-audio|1|13440|0.840000|13440|0.840000|320|0.020000|1430|2560|dbl|320|1|mono
-audio|1|13760|0.860000|13760|0.860000|320|0.020000|1463|2560|dbl|320|1|mono
-audio|1|14080|0.880000|14080|0.880000|320|0.020000|1496|2560|dbl|320|1|mono
-audio|1|14400|0.900000|14400|0.900000|320|0.020000|1529|2560|dbl|320|1|mono
-audio|1|14720|0.920000|14720|0.920000|320|0.020000|1562|2560|dbl|320|1|mono
-audio|1|15040|0.940000|15040|0.940000|320|0.020000|1595|2560|dbl|320|1|mono
-audio|1|15360|0.960000|15360|0.960000|320|0.020000|1628|2560|dbl|320|1|mono
-audio|1|15680|0.980000|15680|0.980000|320|0.020000|1661|2560|dbl|320|1|mono
-audio|1|16000|1.000000|16000|1.000000|320|0.020000|1694|2560|dbl|320|1|mono
-audio|1|16320|1.020000|16320|1.020000|320|0.020000|1727|2560|dbl|320|1|mono
-audio|1|16640|1.040000|16640|1.040000|320|0.020000|1760|2560|dbl|320|1|mono
-audio|1|16960|1.060000|16960|1.060000|320|0.020000|1793|2560|dbl|320|1|mono
-audio|1|17280|1.080000|17280|1.080000|320|0.020000|1826|2560|dbl|320|1|mono
-audio|1|17600|1.100000|17600|1.100000|320|0.020000|1859|2560|dbl|320|1|mono
-audio|1|17920|1.120000|17920|1.120000|320|0.020000|1892|2560|dbl|320|1|mono
-audio|1|18240|1.140000|18240|1.140000|320|0.020000|1925|2560|dbl|320|1|mono
-audio|1|18560|1.160000|18560|1.160000|320|0.020000|1958|2560|dbl|320|1|mono
-audio|1|18880|1.180000|18880|1.180000|320|0.020000|1991|2560|dbl|320|1|mono
-audio|1|19200|1.200000|19200|1.200000|320|0.020000|2024|2560|dbl|320|1|mono
-audio|1|19520|1.220000|19520|1.220000|320|0.020000|2057|2560|dbl|320|1|mono
-audio|1|19840|1.240000|19840|1.240000|320|0.020000|2090|2560|dbl|320|1|mono
-audio|1|20160|1.260000|20160|1.260000|320|0.020000|2123|2560|dbl|320|1|mono
-audio|1|20480|1.280000|20480|1.280000|320|0.020000|2156|2560|dbl|320|1|mono
-audio|1|20800|1.300000|20800|1.300000|320|0.020000|2189|2560|dbl|320|1|mono
-audio|1|21120|1.320000|21120|1.320000|320|0.020000|2222|2560|dbl|320|1|mono
-audio|1|21440|1.340000|21440|1.340000|320|0.020000|2255|2560|dbl|320|1|mono
-audio|1|21760|1.360000|21760|1.360000|320|0.020000|2288|2560|dbl|320|1|mono
-audio|1|22080|1.380000|22080|1.380000|324|0.020250|2321|2560|dbl|320|1|mono|1.28
-audio|1|22400|1.400000|22400|1.400000|320|0.020000|2354|2560|dbl|320|1|mono
-audio|1|22720|1.420000|22720|1.420000|320|0.020000|2387|2560|dbl|320|1|mono
-audio|1|23040|1.440000|23040|1.440000|320|0.020000|2420|2560|dbl|320|1|mono
-audio|1|23360|1.460000|23360|1.460000|320|0.020000|2453|2560|dbl|320|1|mono
-audio|1|23680|1.480000|23680|1.480000|320|0.020000|2486|2560|dbl|320|1|mono
-audio|1|24000|1.500000|24000|1.500000|320|0.020000|2519|2560|dbl|320|1|mono
-audio|1|24320|1.520000|24320|1.520000|320|0.020000|2552|2560|dbl|320|1|mono
-audio|1|24640|1.540000|24640|1.540000|320|0.020000|2585|2560|dbl|320|1|mono
-audio|1|24960|1.560000|24960|1.560000|320|0.020000|2618|2560|dbl|320|1|mono
-audio|1|25280|1.580000|25280|1.580000|320|0.020000|2651|2560|dbl|320|1|mono
-audio|1|25600|1.600000|25600|1.600000|320|0.020000|2684|2560|dbl|320|1|mono
-audio|1|25920|1.620000|25920|1.620000|320|0.020000|2717|2560|dbl|320|1|mono
-audio|1|26240|1.640000|26240|1.640000|320|0.020000|2750|2560|dbl|320|1|mono
-audio|1|26560|1.660000|26560|1.660000|320|0.020000|2783|2560|dbl|320|1|mono
-audio|1|26880|1.680000|26880|1.680000|320|0.020000|2816|2560|dbl|320|1|mono
-audio|1|27200|1.700000|27200|1.700000|320|0.020000|2849|2560|dbl|320|1|mono
-audio|1|27520|1.720000|27520|1.720000|320|0.020000|2882|2560|dbl|320|1|mono
-audio|1|27840|1.740000|27840|1.740000|320|0.020000|2915|2560|dbl|320|1|mono
-audio|1|28160|1.760000|28160|1.760000|320|0.020000|2948|2560|dbl|320|1|mono
-audio|1|28480|1.780000|28480|1.780000|320|0.020000|2981|2560|dbl|320|1|mono
-audio|1|28800|1.800000|28800|1.800000|320|0.020000|3014|2560|dbl|320|1|mono
-audio|1|29120|1.820000|29120|1.820000|320|0.020000|3047|2560|dbl|320|1|mono
-audio|1|29440|1.840000|29440|1.840000|320|0.020000|3080|2560|dbl|320|1|mono
-audio|1|29760|1.860000|29760|1.860000|320|0.020000|3113|2560|dbl|320|1|mono
-audio|1|30080|1.880000|30080|1.880000|320|0.020000|3146|2560|dbl|320|1|mono
-audio|1|30400|1.900000|30400|1.900000|320|0.020000|3179|2560|dbl|320|1|mono
-audio|1|30720|1.920000|30720|1.920000|320|0.020000|3212|2560|dbl|320|1|mono
-audio|1|31040|1.940000|31040|1.940000|320|0.020000|3245|2560|dbl|320|1|mono
-audio|1|31360|1.960000|31360|1.960000|320|0.020000|3278|2560|dbl|320|1|mono
-audio|1|31680|1.980000|31680|1.980000|327|0.020438|3311|2560|dbl|320|1|mono|1.98|0.7
-audio|1|32000|2.000000|32000|2.000000|320|0.020000|3344|2560|dbl|320|1|mono
-audio|1|32320|2.020000|32320|2.020000|320|0.020000|3377|2560|dbl|320|1|mono
-audio|1|32640|2.040000|32640|2.040000|320|0.020000|3410|2560|dbl|320|1|mono
-audio|1|32960|2.060000|32960|2.060000|320|0.020000|3443|2560|dbl|320|1|mono
-audio|1|33280|2.080000|33280|2.080000|320|0.020000|3476|2560|dbl|320|1|mono
-audio|1|33600|2.100000|33600|2.100000|320|0.020000|3509|2560|dbl|320|1|mono
-audio|1|33920|2.120000|33920|2.120000|320|0.020000|3542|2560|dbl|320|1|mono
-audio|1|34240|2.140000|34240|2.140000|320|0.020000|3575|2560|dbl|320|1|mono
-audio|1|34560|2.160000|34560|2.160000|320|0.020000|3608|2560|dbl|320|1|mono
-audio|1|34880|2.180000|34880|2.180000|320|0.020000|3641|2560|dbl|320|1|mono
-audio|1|35200|2.200000|35200|2.200000|320|0.020000|3674|2560|dbl|320|1|mono
-audio|1|35520|2.220000|35520|2.220000|320|0.020000|3707|2560|dbl|320|1|mono
-audio|1|35840|2.240000|35840|2.240000|320|0.020000|3740|2560|dbl|320|1|mono
-audio|1|36160|2.260000|36160|2.260000|320|0.020000|3773|2560|dbl|320|1|mono
-audio|1|36480|2.280000|36480|2.280000|320|0.020000|3806|2560|dbl|320|1|mono
-audio|1|36800|2.300000|36800|2.300000|320|0.020000|3839|2560|dbl|320|1|mono
-audio|1|37120|2.320000|37120|2.320000|320|0.020000|3872|2560|dbl|320|1|mono
-audio|1|37440|2.340000|37440|2.340000|320|0.020000|3905|2560|dbl|320|1|mono
-audio|1|37760|2.360000|37760|2.360000|320|0.020000|3938|2560|dbl|320|1|mono
-audio|1|38080|2.380000|38080|2.380000|320|0.020000|3971|2560|dbl|320|1|mono
-audio|1|38400|2.400000|38400|2.400000|320|0.020000|4004|2560|dbl|320|1|mono
-audio|1|38720|2.420000|38720|2.420000|320|0.020000|4037|2560|dbl|320|1|mono
-audio|1|39040|2.440000|39040|2.440000|320|0.020000|4070|2560|dbl|320|1|mono
-audio|1|39360|2.460000|39360|2.460000|320|0.020000|4103|2560|dbl|320|1|mono
-audio|1|39680|2.480000|39680|2.480000|320|0.020000|4136|2560|dbl|320|1|mono
-audio|1|40000|2.500000|40000|2.500000|320|0.020000|4169|2560|dbl|320|1|mono
-audio|1|40320|2.520000|40320|2.520000|320|0.020000|4202|2560|dbl|320|1|mono
-audio|1|40640|2.540000|40640|2.540000|320|0.020000|4235|2560|dbl|320|1|mono
-audio|1|40960|2.560000|40960|2.560000|320|0.020000|4268|2560|dbl|320|1|mono
-audio|1|41280|2.580000|41280|2.580000|320|0.020000|4301|2560|dbl|320|1|mono
-audio|1|41600|2.600000|41600|2.600000|320|0.020000|4334|2560|dbl|320|1|mono
-audio|1|41920|2.620000|41920|2.620000|320|0.020000|4367|2560|dbl|320|1|mono
-audio|1|42240|2.640000|42240|2.640000|320|0.020000|4400|2560|dbl|320|1|mono
-audio|1|42560|2.660000|42560|2.660000|320|0.020000|4433|2560|dbl|320|1|mono
-audio|1|42880|2.680000|42880|2.680000|320|0.020000|4466|2560|dbl|320|1|mono
-audio|1|43200|2.700000|43200|2.700000|320|0.020000|4499|2560|dbl|320|1|mono
-audio|1|43520|2.720000|43520|2.720000|320|0.020000|4532|2560|dbl|320|1|mono
-audio|1|43840|2.740000|43840|2.740000|320|0.020000|4565|2560|dbl|320|1|mono
-audio|1|44160|2.760000|44160|2.760000|320|0.020000|4598|2560|dbl|320|1|mono
-audio|1|44480|2.780000|44480|2.780000|320|0.020000|4631|2560|dbl|320|1|mono
-audio|1|44800|2.800000|44800|2.800000|320|0.020000|4664|2560|dbl|320|1|mono
-audio|1|45120|2.820000|45120|2.820000|320|0.020000|4697|2560|dbl|320|1|mono
-audio|1|45440|2.840000|45440|2.840000|320|0.020000|4730|2560|dbl|320|1|mono
-audio|1|45760|2.860000|45760|2.860000|320|0.020000|4763|2560|dbl|320|1|mono
-audio|1|46080|2.880000|46080|2.880000|320|0.020000|4796|2560|dbl|320|1|mono
-audio|1|46400|2.900000|46400|2.900000|320|0.020000|4829|2560|dbl|320|1|mono
-audio|1|46720|2.920000|46720|2.920000|320|0.020000|4862|2560|dbl|320|1|mono
-audio|1|47040|2.940000|47040|2.940000|320|0.020000|4895|2560|dbl|320|1|mono
-audio|1|47360|2.960000|47360|2.960000|320|0.020000|4928|2560|dbl|320|1|mono
-audio|1|47680|2.980000|47680|2.980000|320|0.020000|4961|2560|dbl|320|1|mono
-audio|1|48000|3.000000|48000|3.000000|320|0.020000|4994|2560|dbl|320|1|mono
-audio|1|48320|3.020000|48320|3.020000|320|0.020000|5027|2560|dbl|320|1|mono
-audio|1|48640|3.040000|48640|3.040000|320|0.020000|5060|2560|dbl|320|1|mono
-audio|1|48960|3.060000|48960|3.060000|320|0.020000|5093|2560|dbl|320|1|mono
-audio|1|49280|3.080000|49280|3.080000|320|0.020000|5126|2560|dbl|320|1|mono
-audio|1|49600|3.100000|49600|3.100000|320|0.020000|5159|2560|dbl|320|1|mono
-audio|1|49920|3.120000|49920|3.120000|320|0.020000|5192|2560|dbl|320|1|mono
-audio|1|50240|3.140000|50240|3.140000|320|0.020000|5225|2560|dbl|320|1|mono
-audio|1|50560|3.160000|50560|3.160000|320|0.020000|5258|2560|dbl|320|1|mono
-audio|1|50880|3.180000|50880|3.180000|320|0.020000|5291|2560|dbl|320|1|mono
-audio|1|51200|3.200000|51200|3.200000|320|0.020000|5324|2560|dbl|320|1|mono
-audio|1|51520|3.220000|51520|3.220000|320|0.020000|5357|2560|dbl|320|1|mono
-audio|1|51840|3.240000|51840|3.240000|320|0.020000|5390|2560|dbl|320|1|mono
-audio|1|52160|3.260000|52160|3.260000|320|0.020000|5423|2560|dbl|320|1|mono
-audio|1|52480|3.280000|52480|3.280000|320|0.020000|5456|2560|dbl|320|1|mono
-audio|1|52800|3.300000|52800|3.300000|324|0.020250|5489|2560|dbl|320|1|mono|3.2
-audio|1|53120|3.320000|53120|3.320000|320|0.020000|5522|2560|dbl|320|1|mono
-audio|1|53440|3.340000|53440|3.340000|320|0.020000|5555|2560|dbl|320|1|mono
-audio|1|53760|3.360000|53760|3.360000|320|0.020000|5588|2560|dbl|320|1|mono
-audio|1|54080|3.380000|54080|3.380000|320|0.020000|5621|2560|dbl|320|1|mono
-audio|1|54400|3.400000|54400|3.400000|320|0.020000|5654|2560|dbl|320|1|mono
-audio|1|54720|3.420000|54720|3.420000|320|0.020000|5687|2560|dbl|320|1|mono
-audio|1|55040|3.440000|55040|3.440000|320|0.020000|5720|2560|dbl|320|1|mono
-audio|1|55360|3.460000|55360|3.460000|320|0.020000|5753|2560|dbl|320|1|mono
-audio|1|55680|3.480000|55680|3.480000|320|0.020000|5786|2560|dbl|320|1|mono
-audio|1|56000|3.500000|56000|3.500000|320|0.020000|5819|2560|dbl|320|1|mono
-audio|1|56320|3.520000|56320|3.520000|320|0.020000|5852|2560|dbl|320|1|mono
-audio|1|56640|3.540000|56640|3.540000|320|0.020000|5885|2560|dbl|320|1|mono
-audio|1|56960|3.560000|56960|3.560000|320|0.020000|5918|2560|dbl|320|1|mono
-audio|1|57280|3.580000|57280|3.580000|320|0.020000|5951|2560|dbl|320|1|mono
-audio|1|57600|3.600000|57600|3.600000|320|0.020000|5984|2560|dbl|320|1|mono
-audio|1|57920|3.620000|57920|3.620000|320|0.020000|6017|2560|dbl|320|1|mono
-audio|1|58240|3.640000|58240|3.640000|320|0.020000|6050|2560|dbl|320|1|mono
-audio|1|58560|3.660000|58560|3.660000|320|0.020000|6083|2560|dbl|320|1|mono
-audio|1|58880|3.680000|58880|3.680000|320|0.020000|6116|2560|dbl|320|1|mono
-audio|1|59200|3.700000|59200|3.700000|320|0.020000|6149|2560|dbl|320|1|mono
-audio|1|59520|3.720000|59520|3.720000|320|0.020000|6182|2560|dbl|320|1|mono
-audio|1|59840|3.740000|59840|3.740000|320|0.020000|6215|2560|dbl|320|1|mono
-audio|1|60160|3.760000|60160|3.760000|320|0.020000|6248|2560|dbl|320|1|mono
-audio|1|60480|3.780000|60480|3.780000|320|0.020000|6281|2560|dbl|320|1|mono
-audio|1|60800|3.800000|60800|3.800000|320|0.020000|6314|2560|dbl|320|1|mono
-audio|1|61120|3.820000|61120|3.820000|320|0.020000|6347|2560|dbl|320|1|mono
-audio|1|61440|3.840000|61440|3.840000|320|0.020000|6380|2560|dbl|320|1|mono
-audio|1|61760|3.860000|61760|3.860000|320|0.020000|6413|2560|dbl|320|1|mono
-audio|1|62080|3.880000|62080|3.880000|320|0.020000|6446|2560|dbl|320|1|mono
-audio|1|62400|3.900000|62400|3.900000|327|0.020438|6479|2560|dbl|320|1|mono|3.9|0.7
-audio|1|62720|3.920000|62720|3.920000|320|0.020000|6512|2560|dbl|320|1|mono
-audio|1|63040|3.940000|63040|3.940000|320|0.020000|6545|2560|dbl|320|1|mono
-audio|1|63360|3.960000|63360|3.960000|320|0.020000|6578|2560|dbl|320|1|mono
-audio|1|63680|3.980000|63680|3.980000|320|0.020000|6611|2560|dbl|320|1|mono
-audio|1|64000|4.000000|64000|4.000000|320|0.020000|6644|2560|dbl|320|1|mono
-audio|1|64320|4.020000|64320|4.020000|320|0.020000|6677|2560|dbl|320|1|mono
-audio|1|64640|4.040000|64640|4.040000|320|0.020000|6710|2560|dbl|320|1|mono
-audio|1|64960|4.060000|64960|4.060000|320|0.020000|6743|2560|dbl|320|1|mono
-audio|1|65280|4.080000|65280|4.080000|320|0.020000|6776|2560|dbl|320|1|mono
-audio|1|65600|4.100000|65600|4.100000|320|0.020000|6809|2560|dbl|320|1|mono
-audio|1|65920|4.120000|65920|4.120000|320|0.020000|6842|2560|dbl|320|1|mono
-audio|1|66240|4.140000|66240|4.140000|320|0.020000|6875|2560|dbl|320|1|mono
-audio|1|66560|4.160000|66560|4.160000|320|0.020000|6908|2560|dbl|320|1|mono
-audio|1|66880|4.180000|66880|4.180000|320|0.020000|6941|2560|dbl|320|1|mono
-audio|1|67200|4.200000|67200|4.200000|320|0.020000|6974|2560|dbl|320|1|mono
-audio|1|67520|4.220000|67520|4.220000|320|0.020000|7007|2560|dbl|320|1|mono
-audio|1|67840|4.240000|67840|4.240000|320|0.020000|7040|2560|dbl|320|1|mono
-audio|1|68160|4.260000|68160|4.260000|320|0.020000|7073|2560|dbl|320|1|mono
-audio|1|68480|4.280000|68480|4.280000|320|0.020000|7106|2560|dbl|320|1|mono
-audio|1|68800|4.300000|68800|4.300000|320|0.020000|7139|2560|dbl|320|1|mono
-audio|1|69120|4.320000|69120|4.320000|320|0.020000|7172|2560|dbl|320|1|mono
-audio|1|69440|4.340000|69440|4.340000|320|0.020000|7205|2560|dbl|320|1|mono
-audio|1|69760|4.360000|69760|4.360000|320|0.020000|7238|2560|dbl|320|1|mono
-audio|1|70080|4.380000|70080|4.380000|320|0.020000|7271|2560|dbl|320|1|mono
-audio|1|70400|4.400000|70400|4.400000|320|0.020000|7304|2560|dbl|320|1|mono
-audio|1|70720|4.420000|70720|4.420000|320|0.020000|7337|2560|dbl|320|1|mono
-audio|1|71040|4.440000|71040|4.440000|320|0.020000|7370|2560|dbl|320|1|mono
-audio|1|71360|4.460000|71360|4.460000|320|0.020000|7403|2560|dbl|320|1|mono
-audio|1|71680|4.480000|71680|4.480000|320|0.020000|7436|2560|dbl|320|1|mono
-audio|1|72000|4.500000|72000|4.500000|320|0.020000|7469|2560|dbl|320|1|mono
-audio|1|72320|4.520000|72320|4.520000|320|0.020000|7502|2560|dbl|320|1|mono
-audio|1|72640|4.540000|72640|4.540000|320|0.020000|7535|2560|dbl|320|1|mono
-audio|1|72960|4.560000|72960|4.560000|320|0.020000|7568|2560|dbl|320|1|mono
-audio|1|73280|4.580000|73280|4.580000|320|0.020000|7601|2560|dbl|320|1|mono
-audio|1|73600|4.600000|73600|4.600000|320|0.020000|7634|2560|dbl|320|1|mono
-audio|1|73920|4.620000|73920|4.620000|320|0.020000|7667|2560|dbl|320|1|mono
-audio|1|74240|4.640000|74240|4.640000|320|0.020000|7700|2560|dbl|320|1|mono
-audio|1|74560|4.660000|74560|4.660000|320|0.020000|7733|2560|dbl|320|1|mono
-audio|1|74880|4.680000|74880|4.680000|320|0.020000|7766|2560|dbl|320|1|mono
-audio|1|75200|4.700000|75200|4.700000|320|0.020000|7799|2560|dbl|320|1|mono
-audio|1|75520|4.720000|75520|4.720000|320|0.020000|7832|2560|dbl|320|1|mono
-audio|1|75840|4.740000|75840|4.740000|320|0.020000|7865|2560|dbl|320|1|mono
-audio|1|76160|4.760000|76160|4.760000|320|0.020000|7898|2560|dbl|320|1|mono
-audio|1|76480|4.780000|76480|4.780000|320|0.020000|7931|2560|dbl|320|1|mono
-audio|1|76800|4.800000|76800|4.800000|320|0.020000|7964|2560|dbl|320|1|mono
-audio|1|77120|4.820000|77120|4.820000|320|0.020000|7997|2560|dbl|320|1|mono
-audio|1|77440|4.840000|77440|4.840000|320|0.020000|8030|2560|dbl|320|1|mono
-audio|1|77760|4.860000|77760|4.860000|320|0.020000|8063|2560|dbl|320|1|mono
-audio|1|78080|4.880000|78080|4.880000|320|0.020000|8096|2560|dbl|320|1|mono
-audio|1|78400|4.900000|78400|4.900000|320|0.020000|8129|2560|dbl|320|1|mono
-audio|1|78720|4.920000|78720|4.920000|320|0.020000|8162|2560|dbl|320|1|mono
-audio|1|79040|4.940000|79040|4.940000|320|0.020000|8195|2560|dbl|320|1|mono
-audio|1|79360|4.960000|79360|4.960000|320|0.020000|8228|2560|dbl|320|1|mono
-audio|1|79680|4.980000|79680|4.980000|320|0.020000|8261|2560|dbl|320|1|mono
-audio|1|80000|5.000000|80000|5.000000|324|0.020250|8294|2560|dbl|320|1|mono|4.9
-audio|1|80320|5.020000|80320|5.020000|320|0.020000|8327|2560|dbl|320|1|mono
-audio|1|80640|5.040000|80640|5.040000|320|0.020000|8360|2560|dbl|320|1|mono
-audio|1|80960|5.060000|80960|5.060000|320|0.020000|8393|2560|dbl|320|1|mono
-audio|1|81280|5.080000|81280|5.080000|320|0.020000|8426|2560|dbl|320|1|mono
-audio|1|81600|5.100000|81600|5.100000|320|0.020000|8459|2560|dbl|320|1|mono
-audio|1|81920|5.120000|81920|5.120000|320|0.020000|8492|2560|dbl|320|1|mono
-audio|1|82240|5.140000|82240|5.140000|320|0.020000|8525|2560|dbl|320|1|mono
-audio|1|82560|5.160000|82560|5.160000|320|0.020000|8558|2560|dbl|320|1|mono
-audio|1|82880|5.180000|82880|5.180000|320|0.020000|8591|2560|dbl|320|1|mono
-audio|1|83200|5.200000|83200|5.200000|320|0.020000|8624|2560|dbl|320|1|mono
-audio|1|83520|5.220000|83520|5.220000|320|0.020000|8657|2560|dbl|320|1|mono
-audio|1|83840|5.240000|83840|5.240000|320|0.020000|8690|2560|dbl|320|1|mono
-audio|1|84160|5.260000|84160|5.260000|320|0.020000|8723|2560|dbl|320|1|mono
-audio|1|84480|5.280000|84480|5.280000|320|0.020000|8756|2560|dbl|320|1|mono
-audio|1|84800|5.300000|84800|5.300000|320|0.020000|8789|2560|dbl|320|1|mono
-audio|1|85120|5.320000|85120|5.320000|320|0.020000|8822|2560|dbl|320|1|mono
-audio|1|85440|5.340000|85440|5.340000|320|0.020000|8855|2560|dbl|320|1|mono
-audio|1|85760|5.360000|85760|5.360000|320|0.020000|8888|2560|dbl|320|1|mono
-audio|1|86080|5.380000|86080|5.380000|320|0.020000|8921|2560|dbl|320|1|mono
-audio|1|86400|5.400000|86400|5.400000|320|0.020000|8954|2560|dbl|320|1|mono
-audio|1|86720|5.420000|86720|5.420000|320|0.020000|8987|2560|dbl|320|1|mono
-audio|1|87040|5.440000|87040|5.440000|328|0.020500|9020|2560|dbl|320|1|mono|5.44|0.54
-audio|1|87360|5.460000|87360|5.460000|320|0.020000|9053|2560|dbl|320|1|mono
-audio|1|87680|5.480000|87680|5.480000|320|0.020000|9086|2560|dbl|320|1|mono
-audio|1|88000|5.500000|88000|5.500000|320|0.020000|9119|2560|dbl|320|1|mono
-audio|1|88320|5.520000|88320|5.520000|320|0.020000|9152|2560|dbl|320|1|mono
-audio|1|88640|5.540000|88640|5.540000|320|0.020000|9185|2560|dbl|320|1|mono
-audio|1|88960|5.560000|88960|5.560000|320|0.020000|9218|2560|dbl|320|1|mono
-audio|1|89280|5.580000|89280|5.580000|320|0.020000|9251|2560|dbl|320|1|mono
-audio|1|89600|5.600000|89600|5.600000|320|0.020000|9284|2560|dbl|320|1|mono
-audio|1|89920|5.620000|89920|5.620000|320|0.020000|9317|2560|dbl|320|1|mono
-audio|1|90240|5.640000|90240|5.640000|320|0.020000|9350|2560|dbl|320|1|mono
-audio|1|90560|5.660000|90560|5.660000|320|0.020000|9383|2560|dbl|320|1|mono
-audio|1|90880|5.680000|90880|5.680000|320|0.020000|9416|2560|dbl|320|1|mono
-audio|1|91200|5.700000|91200|5.700000|320|0.020000|9449|2560|dbl|320|1|mono
-audio|1|91520|5.720000|91520|5.720000|320|0.020000|9482|2560|dbl|320|1|mono
-audio|1|91840|5.740000|91840|5.740000|320|0.020000|9515|2560|dbl|320|1|mono
-audio|1|92160|5.760000|92160|5.760000|320|0.020000|9548|2560|dbl|320|1|mono
-audio|1|92480|5.780000|92480|5.780000|320|0.020000|9581|2560|dbl|320|1|mono
-audio|1|92800|5.800000|92800|5.800000|320|0.020000|9614|2560|dbl|320|1|mono
-audio|1|93120|5.820000|93120|5.820000|320|0.020000|9647|2560|dbl|320|1|mono
-audio|1|93440|5.840000|93440|5.840000|320|0.020000|9680|2560|dbl|320|1|mono
-audio|1|93760|5.860000|93760|5.860000|320|0.020000|9713|2560|dbl|320|1|mono
-audio|1|94080|5.880000|94080|5.880000|320|0.020000|9746|2560|dbl|320|1|mono
-audio|1|94400|5.900000|94400|5.900000|320|0.020000|9779|2560|dbl|320|1|mono
-audio|1|94720|5.920000|94720|5.920000|320|0.020000|9812|2560|dbl|320|1|mono
-audio|1|95040|5.940000|95040|5.940000|320|0.020000|9845|2560|dbl|320|1|mono
-audio|1|95360|5.960000|95360|5.960000|320|0.020000|9878|2560|dbl|320|1|mono
-audio|1|95680|5.980000|95680|5.980000|320|0.020000|9911|2560|dbl|320|1|mono
-audio|1|96000|6.000000|96000|6.000000|320|0.020000|9944|2560|dbl|320|1|mono
-audio|1|96320|6.020000|96320|6.020000|320|0.020000|9977|2560|dbl|320|1|mono
-audio|1|96640|6.040000|96640|6.040000|320|0.020000|10010|2560|dbl|320|1|mono
-audio|1|96960|6.060000|96960|6.060000|320|0.020000|10043|2560|dbl|320|1|mono
-audio|1|97280|6.080000|97280|6.080000|320|0.020000|10076|2560|dbl|320|1|mono
-audio|1|97600|6.100000|97600|6.100000|320|0.020000|10109|2560|dbl|320|1|mono
-audio|1|97920|6.120000|97920|6.120000|320|0.020000|10142|2560|dbl|320|1|mono
-audio|1|98240|6.140000|98240|6.140000|320|0.020000|10175|2560|dbl|320|1|mono
-audio|1|98560|6.160000|98560|6.160000|320|0.020000|10208|2560|dbl|320|1|mono
-audio|1|98880|6.180000|98880|6.180000|320|0.020000|10241|2560|dbl|320|1|mono
-audio|1|99200|6.200000|99200|6.200000|320|0.020000|10274|2560|dbl|320|1|mono
-audio|1|99520|6.220000|99520|6.220000|320|0.020000|10307|2560|dbl|320|1|mono
-audio|1|99840|6.240000|99840|6.240000|320|0.020000|10340|2560|dbl|320|1|mono
-audio|1|100160|6.260000|100160|6.260000|320|0.020000|10373|2560|dbl|320|1|mono
-audio|1|100480|6.280000|100480|6.280000|320|0.020000|10406|2560|dbl|320|1|mono
-audio|1|100800|6.300000|100800|6.300000|320|0.020000|10439|2560|dbl|320|1|mono
-audio|1|101120|6.320000|101120|6.320000|320|0.020000|10472|2560|dbl|320|1|mono
-audio|1|101440|6.340000|101440|6.340000|320|0.020000|10505|2560|dbl|320|1|mono
-audio|1|101760|6.360000|101760|6.360000|320|0.020000|10538|2560|dbl|320|1|mono
-audio|1|102080|6.380000|102080|6.380000|320|0.020000|10571|2560|dbl|320|1|mono
-audio|1|102400|6.400000|102400|6.400000|320|0.020000|10604|2560|dbl|320|1|mono
-audio|1|102720|6.420000|102720|6.420000|320|0.020000|10637|2560|dbl|320|1|mono
-audio|1|103040|6.440000|103040|6.440000|320|0.020000|10670|2560|dbl|320|1|mono
-audio|1|103360|6.460000|103360|6.460000|320|0.020000|10703|2560|dbl|320|1|mono
-audio|1|103680|6.480000|103680|6.480000|320|0.020000|10736|2560|dbl|320|1|mono
-audio|1|104000|6.500000|104000|6.500000|320|0.020000|10769|2560|dbl|320|1|mono
-audio|1|104320|6.520000|104320|6.520000|320|0.020000|10802|2560|dbl|320|1|mono
-audio|1|104640|6.540000|104640|6.540000|324|0.020250|10835|2560|dbl|320|1|mono|6.44
-audio|1|104960|6.560000|104960|6.560000|320|0.020000|10868|2560|dbl|320|1|mono
-audio|1|105280|6.580000|105280|6.580000|320|0.020000|10901|2560|dbl|320|1|mono
-audio|1|105600|6.600000|105600|6.600000|320|0.020000|10934|2560|dbl|320|1|mono
-audio|1|105920|6.620000|105920|6.620000|320|0.020000|10967|2560|dbl|320|1|mono
-audio|1|106240|6.640000|106240|6.640000|320|0.020000|11000|2560|dbl|320|1|mono
-audio|1|106560|6.660000|106560|6.660000|320|0.020000|11033|2560|dbl|320|1|mono
-audio|1|106880|6.680000|106880|6.680000|320|0.020000|11066|2560|dbl|320|1|mono
-audio|1|107200|6.700000|107200|6.700000|320|0.020000|11099|2560|dbl|320|1|mono
-audio|1|107520|6.720000|107520|6.720000|320|0.020000|11132|2560|dbl|320|1|mono
-audio|1|107840|6.740000|107840|6.740000|320|0.020000|11165|2560|dbl|320|1|mono
-audio|1|108160|6.760000|108160|6.760000|320|0.020000|11198|2560|dbl|320|1|mono
-audio|1|108480|6.780000|108480|6.780000|320|0.020000|11231|2560|dbl|320|1|mono
-audio|1|108800|6.800000|108800|6.800000|320|0.020000|11264|2560|dbl|320|1|mono
-audio|1|109120|6.820000|109120|6.820000|320|0.020000|11297|2560|dbl|320|1|mono
-audio|1|109440|6.840000|109440|6.840000|320|0.020000|11330|2560|dbl|320|1|mono
-audio|1|109760|6.860000|109760|6.860000|320|0.020000|11363|2560|dbl|320|1|mono
-audio|1|110080|6.880000|110080|6.880000|320|0.020000|11396|2560|dbl|320|1|mono
-audio|1|110400|6.900000|110400|6.900000|320|0.020000|11429|2560|dbl|320|1|mono
-audio|1|110720|6.920000|110720|6.920000|320|0.020000|11462|2560|dbl|320|1|mono
-audio|1|111040|6.940000|111040|6.940000|320|0.020000|11495|2560|dbl|320|1|mono
-audio|1|111360|6.960000|111360|6.960000|320|0.020000|11528|2560|dbl|320|1|mono
-audio|1|111680|6.980000|111680|6.980000|320|0.020000|11561|2560|dbl|320|1|mono
-audio|1|112000|7.000000|112000|7.000000|320|0.020000|11594|2560|dbl|320|1|mono
-audio|1|112320|7.020000|112320|7.020000|320|0.020000|11627|2560|dbl|320|1|mono
-audio|1|112640|7.040000|112640|7.040000|320|0.020000|11660|2560|dbl|320|1|mono
-audio|1|112960|7.060000|112960|7.060000|320|0.020000|11693|2560|dbl|320|1|mono
-audio|1|113280|7.080000|113280|7.080000|320|0.020000|11726|2560|dbl|320|1|mono
-audio|1|113600|7.100000|113600|7.100000|320|0.020000|11759|2560|dbl|320|1|mono
-audio|1|113920|7.120000|113920|7.120000|320|0.020000|11792|2560|dbl|320|1|mono
-audio|1|114240|7.140000|114240|7.140000|320|0.020000|11825|2560|dbl|320|1|mono
-audio|1|114560|7.160000|114560|7.160000|320|0.020000|11858|2560|dbl|320|1|mono
-audio|1|114880|7.180000|114880|7.180000|320|0.020000|11891|2560|dbl|320|1|mono
-audio|1|115200|7.200000|115200|7.200000|320|0.020000|11924|2560|dbl|320|1|mono
-audio|1|115520|7.220000|115520|7.220000|320|0.020000|11957|2560|dbl|320|1|mono
-audio|1|115840|7.240000|115840|7.240000|320|0.020000|11990|2560|dbl|320|1|mono
-audio|1|116160|7.260000|116160|7.260000|328|0.020500|12023|2560|dbl|320|1|mono|7.26|0.82
-audio|1|116480|7.280000|116480|7.280000|320|0.020000|12056|2560|dbl|320|1|mono
-audio|1|116800|7.300000|116800|7.300000|320|0.020000|12089|2560|dbl|320|1|mono
-audio|1|117120|7.320000|117120|7.320000|320|0.020000|12122|2560|dbl|320|1|mono
-audio|1|117440|7.340000|117440|7.340000|320|0.020000|12155|2560|dbl|320|1|mono
-audio|1|117760|7.360000|117760|7.360000|320|0.020000|12188|2560|dbl|320|1|mono
-audio|1|118080|7.380000|118080|7.380000|320|0.020000|12221|2560|dbl|320|1|mono
-audio|1|118400|7.400000|118400|7.400000|320|0.020000|12254|2560|dbl|320|1|mono
-audio|1|118720|7.420000|118720|7.420000|320|0.020000|12287|2560|dbl|320|1|mono
-audio|1|119040|7.440000|119040|7.440000|320|0.020000|12320|2560|dbl|320|1|mono
-audio|1|119360|7.460000|119360|7.460000|320|0.020000|12353|2560|dbl|320|1|mono
-audio|1|119680|7.480000|119680|7.480000|320|0.020000|12386|2560|dbl|320|1|mono
-audio|1|120000|7.500000|120000|7.500000|320|0.020000|12419|2560|dbl|320|1|mono
-audio|1|120320|7.520000|120320|7.520000|320|0.020000|12452|2560|dbl|320|1|mono
-audio|1|120640|7.540000|120640|7.540000|320|0.020000|12485|2560|dbl|320|1|mono
-audio|1|120960|7.560000|120960|7.560000|320|0.020000|12518|2560|dbl|320|1|mono
-audio|1|121280|7.580000|121280|7.580000|320|0.020000|12551|2560|dbl|320|1|mono
-audio|1|121600|7.600000|121600|7.600000|320|0.020000|12584|2560|dbl|320|1|mono
-audio|1|121920|7.620000|121920|7.620000|320|0.020000|12617|2560|dbl|320|1|mono
-audio|1|122240|7.640000|122240|7.640000|320|0.020000|12650|2560|dbl|320|1|mono
-audio|1|122560|7.660000|122560|7.660000|320|0.020000|12683|2560|dbl|320|1|mono
-audio|1|122880|7.680000|122880|7.680000|320|0.020000|12716|2560|dbl|320|1|mono
-audio|1|123200|7.700000|123200|7.700000|320|0.020000|12749|2560|dbl|320|1|mono
-audio|1|123520|7.720000|123520|7.720000|320|0.020000|12782|2560|dbl|320|1|mono
-audio|1|123840|7.740000|123840|7.740000|320|0.020000|12815|2560|dbl|320|1|mono
-audio|1|124160|7.760000|124160|7.760000|320|0.020000|12848|2560|dbl|320|1|mono
-audio|1|124480|7.780000|124480|7.780000|320|0.020000|12881|2560|dbl|320|1|mono
-audio|1|124800|7.800000|124800|7.800000|320|0.020000|12914|2560|dbl|320|1|mono
-audio|1|125120|7.820000|125120|7.820000|320|0.020000|12947|2560|dbl|320|1|mono
-audio|1|125440|7.840000|125440|7.840000|320|0.020000|12980|2560|dbl|320|1|mono
-audio|1|125760|7.860000|125760|7.860000|320|0.020000|13013|2560|dbl|320|1|mono
-audio|1|126080|7.880000|126080|7.880000|320|0.020000|13046|2560|dbl|320|1|mono
-audio|1|126400|7.900000|126400|7.900000|320|0.020000|13079|2560|dbl|320|1|mono
-audio|1|126720|7.920000|126720|7.920000|320|0.020000|13112|2560|dbl|320|1|mono
-audio|1|127040|7.940000|127040|7.940000|320|0.020000|13145|2560|dbl|320|1|mono
-audio|1|127360|7.960000|127360|7.960000|320|0.020000|13178|2560|dbl|320|1|mono
-audio|1|127680|7.980000|127680|7.980000|320|0.020000|13211|2560|dbl|320|1|mono
-audio|1|128000|8.000000|128000|8.000000|320|0.020000|13244|2560|dbl|320|1|mono
-audio|1|128320|8.020000|128320|8.020000|320|0.020000|13277|2560|dbl|320|1|mono
-audio|1|128640|8.040000|128640|8.040000|320|0.020000|13310|2560|dbl|320|1|mono
-audio|1|128960|8.060000|128960|8.060000|320|0.020000|13343|2560|dbl|320|1|mono
-audio|1|129280|8.080000|129280|8.080000|320|0.020000|13376|2560|dbl|320|1|mono
-audio|1|129600|8.100000|129600|8.100000|320|0.020000|13409|2560|dbl|320|1|mono
-audio|1|129920|8.120000|129920|8.120000|320|0.020000|13442|2560|dbl|320|1|mono
-audio|1|130240|8.140000|130240|8.140000|320|0.020000|13475|2560|dbl|320|1|mono
-audio|1|130560|8.160000|130560|8.160000|320|0.020000|13508|2560|dbl|320|1|mono
-audio|1|130880|8.180000|130880|8.180000|320|0.020000|13541|2560|dbl|320|1|mono
-audio|1|131200|8.200000|131200|8.200000|320|0.020000|13574|2560|dbl|320|1|mono
-audio|1|131520|8.220000|131520|8.220000|320|0.020000|13607|2560|dbl|320|1|mono
-audio|1|131840|8.240000|131840|8.240000|320|0.020000|13640|2560|dbl|320|1|mono
-audio|1|132160|8.260000|132160|8.260000|320|0.020000|13673|2560|dbl|320|1|mono
-audio|1|132480|8.280000|132480|8.280000|320|0.020000|13706|2560|dbl|320|1|mono
-audio|1|132800|8.300000|132800|8.300000|320|0.020000|13739|2560|dbl|320|1|mono
-audio|1|133120|8.320000|133120|8.320000|320|0.020000|13772|2560|dbl|320|1|mono
-audio|1|133440|8.340000|133440|8.340000|320|0.020000|13805|2560|dbl|320|1|mono
-audio|1|133760|8.360000|133760|8.360000|320|0.020000|13838|2560|dbl|320|1|mono
-audio|1|134080|8.380000|134080|8.380000|320|0.020000|13871|2560|dbl|320|1|mono
-audio|1|134400|8.400000|134400|8.400000|320|0.020000|13904|2560|dbl|320|1|mono
-audio|1|134720|8.420000|134720|8.420000|320|0.020000|13937|2560|dbl|320|1|mono
-audio|1|135040|8.440000|135040|8.440000|320|0.020000|13970|2560|dbl|320|1|mono
-audio|1|135360|8.460000|135360|8.460000|320|0.020000|14003|2560|dbl|320|1|mono
-audio|1|135680|8.480000|135680|8.480000|320|0.020000|14036|2560|dbl|320|1|mono
-audio|1|136000|8.500000|136000|8.500000|320|0.020000|14069|2560|dbl|320|1|mono
-audio|1|136320|8.520000|136320|8.520000|320|0.020000|14102|2560|dbl|320|1|mono
-audio|1|136640|8.540000|136640|8.540000|320|0.020000|14135|2560|dbl|320|1|mono
-audio|1|136960|8.560000|136960|8.560000|320|0.020000|14168|2560|dbl|320|1|mono
-audio|1|137280|8.580000|137280|8.580000|320|0.020000|14201|2560|dbl|320|1|mono
-audio|1|137600|8.600000|137600|8.600000|324|0.020250|14234|2560|dbl|320|1|mono|8.5
-audio|1|137920|8.620000|137920|8.620000|320|0.020000|14267|2560|dbl|320|1|mono
-audio|1|138240|8.640000|138240|8.640000|320|0.020000|14300|2560|dbl|320|1|mono
-audio|1|138560|8.660000|138560|8.660000|320|0.020000|14333|2560|dbl|320|1|mono
-audio|1|138880|8.680000|138880|8.680000|328|0.020500|14366|2560|dbl|320|1|mono|8.68|0.18
-audio|1|139200|8.700000|139200|8.700000|320|0.020000|14399|2560|dbl|320|1|mono
-audio|1|139520|8.720000|139520|8.720000|320|0.020000|14432|2560|dbl|320|1|mono
-audio|1|139840|8.740000|139840|8.740000|320|0.020000|14465|2560|dbl|320|1|mono
-audio|1|140160|8.760000|140160|8.760000|320|0.020000|14498|2560|dbl|320|1|mono
-audio|1|140480|8.780000|140480|8.780000|324|0.020250|14531|2560|dbl|320|1|mono|8.68
-audio|1|140800|8.800000|140800|8.800000|320|0.020000|14564|2560|dbl|320|1|mono
-audio|1|141120|8.820000|141120|8.820000|320|0.020000|14597|2560|dbl|320|1|mono
-audio|1|141440|8.840000|141440|8.840000|320|0.020000|14630|2560|dbl|320|1|mono
-audio|1|141760|8.860000|141760|8.860000|320|0.020000|14663|2560|dbl|320|1|mono
-audio|1|142080|8.880000|142080|8.880000|320|0.020000|14696|2560|dbl|320|1|mono
-audio|1|142400|8.900000|142400|8.900000|320|0.020000|14729|2560|dbl|320|1|mono
-audio|1|142720|8.920000|142720|8.920000|320|0.020000|14762|2560|dbl|320|1|mono
-audio|1|143040|8.940000|143040|8.940000|320|0.020000|14795|2560|dbl|320|1|mono
-audio|1|143360|8.960000|143360|8.960000|320|0.020000|14828|2560|dbl|320|1|mono
-audio|1|143680|8.980000|143680|8.980000|327|0.020438|14861|2560|dbl|320|1|mono|8.98|0.3
-audio|1|144000|9.000000|144000|9.000000|320|0.020000|14894|2560|dbl|320|1|mono
-audio|1|144320|9.020000|144320|9.020000|320|0.020000|14927|2560|dbl|320|1|mono
-audio|1|144640|9.040000|144640|9.040000|320|0.020000|14960|2560|dbl|320|1|mono
-audio|1|144960|9.060000|144960|9.060000|320|0.020000|14993|2560|dbl|320|1|mono
-audio|1|145280|9.080000|145280|9.080000|320|0.020000|15026|2560|dbl|320|1|mono
-audio|1|145600|9.100000|145600|9.100000|320|0.020000|15059|2560|dbl|320|1|mono
-audio|1|145920|9.120000|145920|9.120000|320|0.020000|15092|2560|dbl|320|1|mono
-audio|1|146240|9.140000|146240|9.140000|320|0.020000|15125|2560|dbl|320|1|mono
-audio|1|146560|9.160000|146560|9.160000|320|0.020000|15158|2560|dbl|320|1|mono
-audio|1|146880|9.180000|146880|9.180000|320|0.020000|15191|2560|dbl|320|1|mono
-audio|1|147200|9.200000|147200|9.200000|320|0.020000|15224|2560|dbl|320|1|mono
-audio|1|147520|9.220000|147520|9.220000|320|0.020000|15257|2560|dbl|320|1|mono
-audio|1|147840|9.240000|147840|9.240000|320|0.020000|15290|2560|dbl|320|1|mono
-audio|1|148160|9.260000|148160|9.260000|320|0.020000|15323|2560|dbl|320|1|mono
-audio|1|148480|9.280000|148480|9.280000|320|0.020000|15356|2560|dbl|320|1|mono
-audio|1|148800|9.300000|148800|9.300000|320|0.020000|15389|2560|dbl|320|1|mono
-audio|1|149120|9.320000|149120|9.320000|320|0.020000|15422|2560|dbl|320|1|mono
-audio|1|149440|9.340000|149440|9.340000|320|0.020000|15455|2560|dbl|320|1|mono
-audio|1|149760|9.360000|149760|9.360000|320|0.020000|15488|2560|dbl|320|1|mono
-audio|1|150080|9.380000|150080|9.380000|320|0.020000|15521|2560|dbl|320|1|mono
-audio|1|150400|9.400000|150400|9.400000|320|0.020000|15554|2560|dbl|320|1|mono
-audio|1|150720|9.420000|150720|9.420000|320|0.020000|15587|2560|dbl|320|1|mono
-audio|1|151040|9.440000|151040|9.440000|320|0.020000|15620|2560|dbl|320|1|mono
-audio|1|151360|9.460000|151360|9.460000|320|0.020000|15653|2560|dbl|320|1|mono
-audio|1|151680|9.480000|151680|9.480000|320|0.020000|15686|2560|dbl|320|1|mono
-audio|1|152000|9.500000|152000|9.500000|320|0.020000|15719|2560|dbl|320|1|mono
-audio|1|152320|9.520000|152320|9.520000|320|0.020000|15752|2560|dbl|320|1|mono
-audio|1|152640|9.540000|152640|9.540000|320|0.020000|15785|2560|dbl|320|1|mono
-audio|1|152960|9.560000|152960|9.560000|320|0.020000|15818|2560|dbl|320|1|mono
-audio|1|153280|9.580000|153280|9.580000|320|0.020000|15851|2560|dbl|320|1|mono
-audio|1|153600|9.600000|153600|9.600000|320|0.020000|15884|2560|dbl|320|1|mono
-audio|1|153920|9.620000|153920|9.620000|320|0.020000|15917|2560|dbl|320|1|mono
-audio|1|154240|9.640000|154240|9.640000|320|0.020000|15950|2560|dbl|320|1|mono
-audio|1|154560|9.660000|154560|9.660000|320|0.020000|15983|2560|dbl|320|1|mono
-audio|1|154880|9.680000|154880|9.680000|320|0.020000|16016|2560|dbl|320|1|mono
-audio|1|155200|9.700000|155200|9.700000|320|0.020000|16049|2560|dbl|320|1|mono
-audio|1|155520|9.720000|155520|9.720000|320|0.020000|16082|2560|dbl|320|1|mono
-audio|1|155840|9.740000|155840|9.740000|320|0.020000|16115|2560|dbl|320|1|mono
-audio|1|156160|9.760000|156160|9.760000|320|0.020000|16148|2560|dbl|320|1|mono
-audio|1|156480|9.780000|156480|9.780000|320|0.020000|16181|2560|dbl|320|1|mono
-audio|1|156800|9.800000|156800|9.800000|320|0.020000|16214|2560|dbl|320|1|mono
-audio|1|157120|9.820000|157120|9.820000|320|0.020000|16247|2560|dbl|320|1|mono
-audio|1|157440|9.840000|157440|9.840000|320|0.020000|16280|2560|dbl|320|1|mono
-audio|1|157760|9.860000|157760|9.860000|320|0.020000|16313|2560|dbl|320|1|mono
-audio|1|158080|9.880000|158080|9.880000|320|0.020000|16346|2560|dbl|320|1|mono
-audio|1|158400|9.900000|158400|9.900000|320|0.020000|16379|2560|dbl|320|1|mono
-audio|1|158720|9.920000|158720|9.920000|320|0.020000|16412|2560|dbl|320|1|mono
-audio|1|159040|9.940000|159040|9.940000|320|0.020000|16445|2560|dbl|320|1|mono
-audio|1|159360|9.960000|159360|9.960000|320|0.020000|16478|2560|dbl|320|1|mono
-audio|1|159680|9.980000|159680|9.980000|320|0.020000|16511|2560|dbl|320|1|mono
-audio|1|160000|10.000000|160000|10.000000|320|0.020000|16544|2560|dbl|320|1|mono
-audio|1|160320|10.020000|160320|10.020000|320|0.020000|16577|2560|dbl|320|1|mono
-audio|1|160640|10.040000|160640|10.040000|320|0.020000|16610|2560|dbl|320|1|mono
-audio|1|160960|10.060000|160960|10.060000|320|0.020000|16643|2560|dbl|320|1|mono
-audio|1|161280|10.080000|161280|10.080000|320|0.020000|16676|2560|dbl|320|1|mono
-audio|1|161600|10.100000|161600|10.100000|324|0.020250|16709|2560|dbl|320|1|mono|10
-audio|1|161920|10.120000|161920|10.120000|320|0.020000|16742|2560|dbl|320|1|mono
-audio|1|162240|10.140000|162240|10.140000|320|0.020000|16775|2560|dbl|320|1|mono
-audio|1|162560|10.160000|162560|10.160000|320|0.020000|16808|2560|dbl|320|1|mono
-audio|1|162880|10.180000|162880|10.180000|320|0.020000|16841|2560|dbl|320|1|mono
-audio|1|163200|10.200000|163200|10.200000|320|0.020000|16874|2560|dbl|320|1|mono
-audio|1|163520|10.220000|163520|10.220000|320|0.020000|16907|2560|dbl|320|1|mono
+pkt_pts=0
+pkt_pts=320
+pkt_pts=640
+pkt_pts=960
+pkt_pts=1280
+pkt_pts=1600
+pkt_pts=1920|tag:lavfi.silence_start=0.02
+pkt_pts=2240
+pkt_pts=2560|tag:lavfi.silence_end=0.16|tag:lavfi.silence_duration=0.14
+pkt_pts=2880
+pkt_pts=3200
+pkt_pts=3520
+pkt_pts=3840
+pkt_pts=4160
+pkt_pts=4480
+pkt_pts=4800
+pkt_pts=5120
+pkt_pts=5440
+pkt_pts=5760|tag:lavfi.silence_start=0.26|tag:lavfi.silence_end=0.36|tag:lavfi.silence_duration=0.1
+pkt_pts=6080
+pkt_pts=6400
+pkt_pts=6720
+pkt_pts=7040
+pkt_pts=7360
+pkt_pts=7680
+pkt_pts=8000
+pkt_pts=8320
+pkt_pts=8640
+pkt_pts=8960
+pkt_pts=9280
+pkt_pts=9600
+pkt_pts=9920
+pkt_pts=10240
+pkt_pts=10560
+pkt_pts=10880
+pkt_pts=11200
+pkt_pts=11520
+pkt_pts=11840
+pkt_pts=12160
+pkt_pts=12480
+pkt_pts=12800
+pkt_pts=13120
+pkt_pts=13440
+pkt_pts=13760
+pkt_pts=14080
+pkt_pts=14400
+pkt_pts=14720
+pkt_pts=15040
+pkt_pts=15360
+pkt_pts=15680
+pkt_pts=16000
+pkt_pts=16320
+pkt_pts=16640
+pkt_pts=16960
+pkt_pts=17280
+pkt_pts=17600
+pkt_pts=17920
+pkt_pts=18240
+pkt_pts=18560
+pkt_pts=18880
+pkt_pts=19200
+pkt_pts=19520
+pkt_pts=19840
+pkt_pts=20160
+pkt_pts=20480
+pkt_pts=20800
+pkt_pts=21120
+pkt_pts=21440
+pkt_pts=21760
+pkt_pts=22080|tag:lavfi.silence_start=1.28
+pkt_pts=22400
+pkt_pts=22720
+pkt_pts=23040
+pkt_pts=23360
+pkt_pts=23680
+pkt_pts=24000
+pkt_pts=24320
+pkt_pts=24640
+pkt_pts=24960
+pkt_pts=25280
+pkt_pts=25600
+pkt_pts=25920
+pkt_pts=26240
+pkt_pts=26560
+pkt_pts=26880
+pkt_pts=27200
+pkt_pts=27520
+pkt_pts=27840
+pkt_pts=28160
+pkt_pts=28480
+pkt_pts=28800
+pkt_pts=29120
+pkt_pts=29440
+pkt_pts=29760
+pkt_pts=30080
+pkt_pts=30400
+pkt_pts=30720
+pkt_pts=31040
+pkt_pts=31360
+pkt_pts=31680|tag:lavfi.silence_end=1.98|tag:lavfi.silence_duration=0.7
+pkt_pts=32000
+pkt_pts=32320
+pkt_pts=32640
+pkt_pts=32960
+pkt_pts=33280
+pkt_pts=33600
+pkt_pts=33920
+pkt_pts=34240
+pkt_pts=34560
+pkt_pts=34880
+pkt_pts=35200
+pkt_pts=35520
+pkt_pts=35840
+pkt_pts=36160
+pkt_pts=36480
+pkt_pts=36800
+pkt_pts=37120
+pkt_pts=37440
+pkt_pts=37760
+pkt_pts=38080
+pkt_pts=38400
+pkt_pts=38720
+pkt_pts=39040
+pkt_pts=39360
+pkt_pts=39680
+pkt_pts=40000
+pkt_pts=40320
+pkt_pts=40640
+pkt_pts=40960
+pkt_pts=41280
+pkt_pts=41600
+pkt_pts=41920
+pkt_pts=42240
+pkt_pts=42560
+pkt_pts=42880
+pkt_pts=43200
+pkt_pts=43520
+pkt_pts=43840
+pkt_pts=44160
+pkt_pts=44480
+pkt_pts=44800
+pkt_pts=45120
+pkt_pts=45440
+pkt_pts=45760
+pkt_pts=46080
+pkt_pts=46400
+pkt_pts=46720
+pkt_pts=47040
+pkt_pts=47360
+pkt_pts=47680
+pkt_pts=48000
+pkt_pts=48320
+pkt_pts=48640
+pkt_pts=48960
+pkt_pts=49280
+pkt_pts=49600
+pkt_pts=49920
+pkt_pts=50240
+pkt_pts=50560
+pkt_pts=50880
+pkt_pts=51200
+pkt_pts=51520
+pkt_pts=51840
+pkt_pts=52160
+pkt_pts=52480
+pkt_pts=52800|tag:lavfi.silence_start=3.2
+pkt_pts=53120
+pkt_pts=53440
+pkt_pts=53760
+pkt_pts=54080
+pkt_pts=54400
+pkt_pts=54720
+pkt_pts=55040
+pkt_pts=55360
+pkt_pts=55680
+pkt_pts=56000
+pkt_pts=56320
+pkt_pts=56640
+pkt_pts=56960
+pkt_pts=57280
+pkt_pts=57600
+pkt_pts=57920
+pkt_pts=58240
+pkt_pts=58560
+pkt_pts=58880
+pkt_pts=59200
+pkt_pts=59520
+pkt_pts=59840
+pkt_pts=60160
+pkt_pts=60480
+pkt_pts=60800
+pkt_pts=61120
+pkt_pts=61440
+pkt_pts=61760
+pkt_pts=62080
+pkt_pts=62400|tag:lavfi.silence_end=3.9|tag:lavfi.silence_duration=0.7
+pkt_pts=62720
+pkt_pts=63040
+pkt_pts=63360
+pkt_pts=63680
+pkt_pts=64000
+pkt_pts=64320
+pkt_pts=64640
+pkt_pts=64960
+pkt_pts=65280
+pkt_pts=65600
+pkt_pts=65920
+pkt_pts=66240
+pkt_pts=66560
+pkt_pts=66880
+pkt_pts=67200
+pkt_pts=67520
+pkt_pts=67840
+pkt_pts=68160
+pkt_pts=68480
+pkt_pts=68800
+pkt_pts=69120
+pkt_pts=69440
+pkt_pts=69760
+pkt_pts=70080
+pkt_pts=70400
+pkt_pts=70720
+pkt_pts=71040
+pkt_pts=71360
+pkt_pts=71680
+pkt_pts=72000
+pkt_pts=72320
+pkt_pts=72640
+pkt_pts=72960
+pkt_pts=73280
+pkt_pts=73600
+pkt_pts=73920
+pkt_pts=74240
+pkt_pts=74560
+pkt_pts=74880
+pkt_pts=75200
+pkt_pts=75520
+pkt_pts=75840
+pkt_pts=76160
+pkt_pts=76480
+pkt_pts=76800
+pkt_pts=77120
+pkt_pts=77440
+pkt_pts=77760
+pkt_pts=78080
+pkt_pts=78400
+pkt_pts=78720
+pkt_pts=79040
+pkt_pts=79360
+pkt_pts=79680
+pkt_pts=80000|tag:lavfi.silence_start=4.9
+pkt_pts=80320
+pkt_pts=80640
+pkt_pts=80960
+pkt_pts=81280
+pkt_pts=81600
+pkt_pts=81920
+pkt_pts=82240
+pkt_pts=82560
+pkt_pts=82880
+pkt_pts=83200
+pkt_pts=83520
+pkt_pts=83840
+pkt_pts=84160
+pkt_pts=84480
+pkt_pts=84800
+pkt_pts=85120
+pkt_pts=85440
+pkt_pts=85760
+pkt_pts=86080
+pkt_pts=86400
+pkt_pts=86720
+pkt_pts=87040|tag:lavfi.silence_end=5.44|tag:lavfi.silence_duration=0.54
+pkt_pts=87360
+pkt_pts=87680
+pkt_pts=88000
+pkt_pts=88320
+pkt_pts=88640
+pkt_pts=88960
+pkt_pts=89280
+pkt_pts=89600
+pkt_pts=89920
+pkt_pts=90240
+pkt_pts=90560
+pkt_pts=90880
+pkt_pts=91200
+pkt_pts=91520
+pkt_pts=91840
+pkt_pts=92160
+pkt_pts=92480
+pkt_pts=92800
+pkt_pts=93120
+pkt_pts=93440
+pkt_pts=93760
+pkt_pts=94080
+pkt_pts=94400
+pkt_pts=94720
+pkt_pts=95040
+pkt_pts=95360
+pkt_pts=95680
+pkt_pts=96000
+pkt_pts=96320
+pkt_pts=96640
+pkt_pts=96960
+pkt_pts=97280
+pkt_pts=97600
+pkt_pts=97920
+pkt_pts=98240
+pkt_pts=98560
+pkt_pts=98880
+pkt_pts=99200
+pkt_pts=99520
+pkt_pts=99840
+pkt_pts=100160
+pkt_pts=100480
+pkt_pts=100800
+pkt_pts=101120
+pkt_pts=101440
+pkt_pts=101760
+pkt_pts=102080
+pkt_pts=102400
+pkt_pts=102720
+pkt_pts=103040
+pkt_pts=103360
+pkt_pts=103680
+pkt_pts=104000
+pkt_pts=104320
+pkt_pts=104640|tag:lavfi.silence_start=6.44
+pkt_pts=104960
+pkt_pts=105280
+pkt_pts=105600
+pkt_pts=105920
+pkt_pts=106240
+pkt_pts=106560
+pkt_pts=106880
+pkt_pts=107200
+pkt_pts=107520
+pkt_pts=107840
+pkt_pts=108160
+pkt_pts=108480
+pkt_pts=108800
+pkt_pts=109120
+pkt_pts=109440
+pkt_pts=109760
+pkt_pts=110080
+pkt_pts=110400
+pkt_pts=110720
+pkt_pts=111040
+pkt_pts=111360
+pkt_pts=111680
+pkt_pts=112000
+pkt_pts=112320
+pkt_pts=112640
+pkt_pts=112960
+pkt_pts=113280
+pkt_pts=113600
+pkt_pts=113920
+pkt_pts=114240
+pkt_pts=114560
+pkt_pts=114880
+pkt_pts=115200
+pkt_pts=115520
+pkt_pts=115840
+pkt_pts=116160|tag:lavfi.silence_end=7.26|tag:lavfi.silence_duration=0.82
+pkt_pts=116480
+pkt_pts=116800
+pkt_pts=117120
+pkt_pts=117440
+pkt_pts=117760
+pkt_pts=118080
+pkt_pts=118400
+pkt_pts=118720
+pkt_pts=119040
+pkt_pts=119360
+pkt_pts=119680
+pkt_pts=120000
+pkt_pts=120320
+pkt_pts=120640
+pkt_pts=120960
+pkt_pts=121280
+pkt_pts=121600
+pkt_pts=121920
+pkt_pts=122240
+pkt_pts=122560
+pkt_pts=122880
+pkt_pts=123200
+pkt_pts=123520
+pkt_pts=123840
+pkt_pts=124160
+pkt_pts=124480
+pkt_pts=124800
+pkt_pts=125120
+pkt_pts=125440
+pkt_pts=125760
+pkt_pts=126080
+pkt_pts=126400
+pkt_pts=126720
+pkt_pts=127040
+pkt_pts=127360
+pkt_pts=127680
+pkt_pts=128000
+pkt_pts=128320
+pkt_pts=128640
+pkt_pts=128960
+pkt_pts=129280
+pkt_pts=129600
+pkt_pts=129920
+pkt_pts=130240
+pkt_pts=130560
+pkt_pts=130880
+pkt_pts=131200
+pkt_pts=131520
+pkt_pts=131840
+pkt_pts=132160
+pkt_pts=132480
+pkt_pts=132800
+pkt_pts=133120
+pkt_pts=133440
+pkt_pts=133760
+pkt_pts=134080
+pkt_pts=134400
+pkt_pts=134720
+pkt_pts=135040
+pkt_pts=135360
+pkt_pts=135680
+pkt_pts=136000
+pkt_pts=136320
+pkt_pts=136640
+pkt_pts=136960
+pkt_pts=137280
+pkt_pts=137600|tag:lavfi.silence_start=8.5
+pkt_pts=137920
+pkt_pts=138240
+pkt_pts=138560
+pkt_pts=138880|tag:lavfi.silence_end=8.68|tag:lavfi.silence_duration=0.18
+pkt_pts=139200
+pkt_pts=139520
+pkt_pts=139840
+pkt_pts=140160
+pkt_pts=140480|tag:lavfi.silence_start=8.68
+pkt_pts=140800
+pkt_pts=141120
+pkt_pts=141440
+pkt_pts=141760
+pkt_pts=142080
+pkt_pts=142400
+pkt_pts=142720
+pkt_pts=143040
+pkt_pts=143360
+pkt_pts=143680|tag:lavfi.silence_end=8.98|tag:lavfi.silence_duration=0.3
+pkt_pts=144000
+pkt_pts=144320
+pkt_pts=144640
+pkt_pts=144960
+pkt_pts=145280
+pkt_pts=145600
+pkt_pts=145920
+pkt_pts=146240
+pkt_pts=146560
+pkt_pts=146880
+pkt_pts=147200
+pkt_pts=147520
+pkt_pts=147840
+pkt_pts=148160
+pkt_pts=148480
+pkt_pts=148800
+pkt_pts=149120
+pkt_pts=149440
+pkt_pts=149760
+pkt_pts=150080
+pkt_pts=150400
+pkt_pts=150720
+pkt_pts=151040
+pkt_pts=151360
+pkt_pts=151680
+pkt_pts=152000
+pkt_pts=152320
+pkt_pts=152640
+pkt_pts=152960
+pkt_pts=153280
+pkt_pts=153600
+pkt_pts=153920
+pkt_pts=154240
+pkt_pts=154560
+pkt_pts=154880
+pkt_pts=155200
+pkt_pts=155520
+pkt_pts=155840
+pkt_pts=156160
+pkt_pts=156480
+pkt_pts=156800
+pkt_pts=157120
+pkt_pts=157440
+pkt_pts=157760
+pkt_pts=158080
+pkt_pts=158400
+pkt_pts=158720
+pkt_pts=159040
+pkt_pts=159360
+pkt_pts=159680
+pkt_pts=160000
+pkt_pts=160320
+pkt_pts=160640
+pkt_pts=160960
+pkt_pts=161280
+pkt_pts=161600|tag:lavfi.silence_start=10
+pkt_pts=161920
+pkt_pts=162240
+pkt_pts=162560
+pkt_pts=162880
+pkt_pts=163200
+pkt_pts=163520
diff --git a/tests/ref/fate/filter-negate b/tests/ref/fate/filter-negate
new file mode 100644
index 0000000..1d2ea73
--- /dev/null
+++ b/tests/ref/fate/filter-negate
@@ -0,0 +1,51 @@
+#tb 0: 1/25
+0,          0,          0,        1,   152064, 0xef20e1ec
+0,          1,          1,        1,   152064, 0x899606ec
+0,          2,          2,        1,   152064, 0xaea175d2
+0,          3,          3,        1,   152064, 0x5201eb55
+0,          4,          4,        1,   152064, 0xf02bb525
+0,          5,          5,        1,   152064, 0x99fdc305
+0,          6,          6,        1,   152064, 0x3a8fefe7
+0,          7,          7,        1,   152064, 0x005edfbf
+0,          8,          8,        1,   152064, 0xe37aea50
+0,          9,          9,        1,   152064, 0xa5ad32f9
+0,         10,         10,        1,   152064, 0xb1e52485
+0,         11,         11,        1,   152064, 0x55b06e56
+0,         12,         12,        1,   152064, 0xdfb7be97
+0,         13,         13,        1,   152064, 0x191bca13
+0,         14,         14,        1,   152064, 0xa554dd3c
+0,         15,         15,        1,   152064, 0x36075b77
+0,         16,         16,        1,   152064, 0xbf6b1cbd
+0,         17,         17,        1,   152064, 0xf33432b6
+0,         18,         18,        1,   152064, 0x5d7100c3
+0,         19,         19,        1,   152064, 0x376f8f0c
+0,         20,         20,        1,   152064, 0x07ca75fa
+0,         21,         21,        1,   152064, 0xe3984704
+0,         22,         22,        1,   152064, 0xa8fb4e4c
+0,         23,         23,        1,   152064, 0xe8e102d8
+0,         24,         24,        1,   152064, 0xcc6771c9
+0,         25,         25,        1,   152064, 0xf646d238
+0,         26,         26,        1,   152064, 0xa52cd41e
+0,         27,         27,        1,   152064, 0x536d92c2
+0,         28,         28,        1,   152064, 0x7058c6a1
+0,         29,         29,        1,   152064, 0xcc6c05d0
+0,         30,         30,        1,   152064, 0x1fc2ffb8
+0,         31,         31,        1,   152064, 0x041ea59c
+0,         32,         32,        1,   152064, 0xfc006e07
+0,         33,         33,        1,   152064, 0x0246efe1
+0,         34,         34,        1,   152064, 0x079428e5
+0,         35,         35,        1,   152064, 0x64d9d773
+0,         36,         36,        1,   152064, 0x914d3454
+0,         37,         37,        1,   152064, 0xef69686e
+0,         38,         38,        1,   152064, 0x3c91129f
+0,         39,         39,        1,   152064, 0x2a611ca7
+0,         40,         40,        1,   152064, 0xaf56124f
+0,         41,         41,        1,   152064, 0xce48cd45
+0,         42,         42,        1,   152064, 0x75feac29
+0,         43,         43,        1,   152064, 0xfd2b4b5b
+0,         44,         44,        1,   152064, 0x8d2f675c
+0,         45,         45,        1,   152064, 0x1573ed3b
+0,         46,         46,        1,   152064, 0xb0fc17ca
+0,         47,         47,        1,   152064, 0x53e5a654
+0,         48,         48,        1,   152064, 0xe0cbb786
+0,         49,         49,        1,   152064, 0xcaa092fe
diff --git a/tests/ref/fate/filter-overlay b/tests/ref/fate/filter-overlay
new file mode 100644
index 0000000..874a991
--- /dev/null
+++ b/tests/ref/fate/filter-overlay
@@ -0,0 +1,51 @@
+#tb 0: 1/25
+0,          0,          0,        1,   152064, 0x57a27a9b
+0,          1,          1,        1,   152064, 0x25c16856
+0,          2,          2,        1,   152064, 0x19adf9cc
+0,          3,          3,        1,   152064, 0x974e9a66
+0,          4,          4,        1,   152064, 0x3c68cbb8
+0,          5,          5,        1,   152064, 0x56c397f9
+0,          6,          6,        1,   152064, 0x9be92877
+0,          7,          7,        1,   152064, 0x53d12880
+0,          8,          8,        1,   152064, 0x31eb35a2
+0,          9,          9,        1,   152064, 0x86cd1566
+0,         10,         10,        1,   152064, 0xcf5951f5
+0,         11,         11,        1,   152064, 0x35c30986
+0,         12,         12,        1,   152064, 0x58e8eb9a
+0,         13,         13,        1,   152064, 0x63dfce6a
+0,         14,         14,        1,   152064, 0xbd2e4cc7
+0,         15,         15,        1,   152064, 0x3320ba14
+0,         16,         16,        1,   152064, 0xc757dd0a
+0,         17,         17,        1,   152064, 0x9f05ed64
+0,         18,         18,        1,   152064, 0x0f292094
+0,         19,         19,        1,   152064, 0x3ef899fc
+0,         20,         20,        1,   152064, 0x9a09b168
+0,         21,         21,        1,   152064, 0x6e2de51e
+0,         22,         22,        1,   152064, 0x6dcae8ee
+0,         23,         23,        1,   152064, 0x8a5e29d8
+0,         24,         24,        1,   152064, 0x91cce46b
+0,         25,         25,        1,   152064, 0xf576ab0d
+0,         26,         26,        1,   152064, 0x3301a89b
+0,         27,         27,        1,   152064, 0xc4b6130c
+0,         28,         28,        1,   152064, 0x37c00be0
+0,         29,         29,        1,   152064, 0xd210b7ca
+0,         30,         30,        1,   152064, 0x9eb783f2
+0,         31,         31,        1,   152064, 0xfe9e9f79
+0,         32,         32,        1,   152064, 0xcedbb511
+0,         33,         33,        1,   152064, 0xf14efe8d
+0,         34,         34,        1,   152064, 0x603fbef5
+0,         35,         35,        1,   152064, 0x82a36887
+0,         36,         36,        1,   152064, 0x8494465a
+0,         37,         37,        1,   152064, 0x5ae9429a
+0,         38,         38,        1,   152064, 0x7533853b
+0,         39,         39,        1,   152064, 0xcf7b8cb2
+0,         40,         40,        1,   152064, 0x9b297e6d
+0,         41,         41,        1,   152064, 0x0182b2db
+0,         42,         42,        1,   152064, 0xa2a9bcf0
+0,         43,         43,        1,   152064, 0xc77c11f8
+0,         44,         44,        1,   152064, 0xf0add83a
+0,         45,         45,        1,   152064, 0x81315436
+0,         46,         46,        1,   152064, 0x662e18cf
+0,         47,         47,        1,   152064, 0x6d5c96e6
+0,         48,         48,        1,   152064, 0x3ca384c3
+0,         49,         49,        1,   152064, 0x6688c0e0
diff --git a/tests/ref/fate/filter-setpts b/tests/ref/fate/filter-setpts
new file mode 100644
index 0000000..c1ce1d3
--- /dev/null
+++ b/tests/ref/fate/filter-setpts
@@ -0,0 +1,38 @@
+#tb 0: 1/25
+0,          0,          0,        1,   152064, 0x05b789ef
+0,          1,          1,        1,   152064, 0x4bb46551
+0,          2,          2,        1,   152064, 0x9dddf64a
+0,          3,          3,        1,   152064, 0x2a8380b0
+0,          4,          4,        1,   152064, 0x4de3b652
+0,          5,          5,        1,   152064, 0xedb5a8e6
+0,          6,          6,        1,   152064, 0x5ab58bac
+0,          7,          7,        1,   152064, 0x1f1b8026
+0,          8,          8,        1,   152064, 0x02344760
+0,          9,          9,        1,   152064, 0x30f5fcd5
+0,         10,         10,        1,   152064, 0xc711ad61
+0,         11,         11,        1,   152064, 0x52a48ddd
+0,         12,         12,        1,   152064, 0xa91c0f05
+0,         13,         13,        1,   152064, 0xb15d38c8
+0,         14,         14,        1,   152064, 0xf25f6acc
+0,         15,         15,        1,   152064, 0xf34ddbff
+0,         16,         16,        1,   152064, 0x9dc72412
+0,         17,         17,        1,   152064, 0x445d1d59
+0,         18,         18,        1,   152064, 0xce09f9d6
+0,         19,         19,        1,   152064, 0x95579936
+0,         20,         20,        1,   152064, 0x43d796b5
+0,         21,         21,        1,   152064, 0x76d2a455
+0,         22,         22,        1,   152064, 0x6dc3650e
+0,         23,         23,        1,   152064, 0xe295c51e
+0,         24,         24,        1,   152064, 0xd766fc8d
+0,         25,         25,        1,   152064, 0xe22f7a30
+0,         26,         26,        1,   152064, 0xfa8d94fb
+0,         27,         27,        1,   152064, 0x4c9737ab
+0,         28,         28,        1,   152064, 0x0b07594c
+0,         29,         29,        1,   152064, 0x88734edd
+0,         30,         30,        1,   152064, 0xd2735925
+0,         31,         31,        1,   152064, 0x20cebfa9
+0,         32,         32,        1,   152064, 0x575c20ec
+0,         33,         33,        1,   152064, 0x61b47e73
+0,         34,         34,        1,   152064, 0x09ef53ff
+0,         35,         35,        1,   152064, 0x6e88c5c2
+0,         36,         36,        1,   152064, 0x4bbad8ea
diff --git a/tests/ref/fate/filter-transpose b/tests/ref/fate/filter-transpose
new file mode 100644
index 0000000..0f23423
--- /dev/null
+++ b/tests/ref/fate/filter-transpose
@@ -0,0 +1,51 @@
+#tb 0: 1/25
+0,          0,          0,        1,   152064, 0xab5889ef
+0,          1,          1,        1,   152064, 0x898b6551
+0,          2,          2,        1,   152064, 0x231bf64a
+0,          3,          3,        1,   152064, 0x0b8c80b0
+0,          4,          4,        1,   152064, 0x8b3cb652
+0,          5,          5,        1,   152064, 0x0100a8e6
+0,          6,          6,        1,   152064, 0x55df7c23
+0,          7,          7,        1,   152064, 0x91bd8bac
+0,          8,          8,        1,   152064, 0xf5508026
+0,          9,          9,        1,   152064, 0xa7ec3915
+0,         10,         10,        1,   152064, 0x4e304760
+0,         11,         11,        1,   152064, 0x98ccfcd5
+0,         12,         12,        1,   152064, 0x00edad61
+0,         13,         13,        1,   152064, 0x01a0a223
+0,         14,         14,        1,   152064, 0x840e8ddd
+0,         15,         15,        1,   152064, 0x52280f05
+0,         16,         16,        1,   152064, 0x75064e18
+0,         17,         17,        1,   152064, 0xf41638c8
+0,         18,         18,        1,   152064, 0x258d6acc
+0,         19,         19,        1,   152064, 0x9d73dbff
+0,         20,         20,        1,   152064, 0x80e0f570
+0,         21,         21,        1,   152064, 0x3a912412
+0,         22,         22,        1,   152064, 0xaf241d59
+0,         23,         23,        1,   152064, 0xe73568ef
+0,         24,         24,        1,   152064, 0xc739f9d6
+0,         25,         25,        1,   152064, 0xc8c19936
+0,         26,         26,        1,   152064, 0x47dd96b5
+0,         27,         27,        1,   152064, 0x5329d887
+0,         28,         28,        1,   152064, 0x5b46a455
+0,         29,         29,        1,   152064, 0xfd8c650e
+0,         30,         30,        1,   152064, 0x85c86aca
+0,         31,         31,        1,   152064, 0x46a6c51e
+0,         32,         32,        1,   152064, 0x220dfc8d
+0,         33,         33,        1,   152064, 0xdbe27a30
+0,         34,         34,        1,   152064, 0xb8cc4378
+0,         35,         35,        1,   152064, 0xb6cf94fb
+0,         36,         36,        1,   152064, 0xeaa937ab
+0,         37,         37,        1,   152064, 0x335401f8
+0,         38,         38,        1,   152064, 0xdb3d594c
+0,         39,         39,        1,   152064, 0x211c4edd
+0,         40,         40,        1,   152064, 0xc3725925
+0,         41,         41,        1,   152064, 0x8f389e08
+0,         42,         42,        1,   152064, 0xc62cbfa9
+0,         43,         43,        1,   152064, 0x803820ec
+0,         44,         44,        1,   152064, 0x0d860471
+0,         45,         45,        1,   152064, 0xf6997e73
+0,         46,         46,        1,   152064, 0x6c2153ff
+0,         47,         47,        1,   152064, 0x1bb2c5c2
+0,         48,         48,        1,   152064, 0x39d1b483
+0,         49,         49,        1,   152064, 0x2b50d8ea
diff --git a/tests/ref/fate/filter-unsharp b/tests/ref/fate/filter-unsharp
new file mode 100644
index 0000000..5dcf40f
--- /dev/null
+++ b/tests/ref/fate/filter-unsharp
@@ -0,0 +1,51 @@
+#tb 0: 1/25
+0,          0,          0,        1,   152064, 0x19a94798
+0,          1,          1,        1,   152064, 0xc88b24f4
+0,          2,          2,        1,   152064, 0xd027b44b
+0,          3,          3,        1,   152064, 0xa9fb3e54
+0,          4,          4,        1,   152064, 0x2991747d
+0,          5,          5,        1,   152064, 0x1dc267fc
+0,          6,          6,        1,   152064, 0xe9063293
+0,          7,          7,        1,   152064, 0xc23e41a4
+0,          8,          8,        1,   152064, 0xaa433dc5
+0,          9,          9,        1,   152064, 0x22b0f0a3
+0,         10,         10,        1,   152064, 0x796d08d8
+0,         11,         11,        1,   152064, 0xa2babd6b
+0,         12,         12,        1,   152064, 0x531e6a62
+0,         13,         13,        1,   152064, 0xc8fa5b9d
+0,         14,         14,        1,   152064, 0x33e54ae8
+0,         15,         15,        1,   152064, 0x86dfd0b8
+0,         16,         16,        1,   152064, 0x101f1170
+0,         17,         17,        1,   152064, 0x230eef00
+0,         18,         18,        1,   152064, 0xa5ee1c5e
+0,         19,         19,        1,   152064, 0x241893c6
+0,         20,         20,        1,   152064, 0x86a0a883
+0,         21,         21,        1,   152064, 0x12b4d8f7
+0,         22,         22,        1,   152064, 0xb220d497
+0,         23,         23,        1,   152064, 0xbaea200e
+0,         24,         24,        1,   152064, 0x6d96b7f3
+0,         25,         25,        1,   152064, 0xc70d4ebb
+0,         26,         26,        1,   152064, 0x20df50af
+0,         27,         27,        1,   152064, 0xfce89174
+0,         28,         28,        1,   152064, 0x74be5c8e
+0,         29,         29,        1,   152064, 0x51f419a6
+0,         30,         30,        1,   152064, 0x790621e7
+0,         31,         31,        1,   152064, 0x37387da2
+0,         32,         32,        1,   152064, 0x8228baa4
+0,         33,         33,        1,   152064, 0xdd2a42b7
+0,         34,         34,        1,   152064, 0xa28bfc63
+0,         35,         35,        1,   152064, 0xe8284337
+0,         36,         36,        1,   152064, 0xb1dae9fe
+0,         37,         37,        1,   152064, 0x0378c0af
+0,         38,         38,        1,   152064, 0x79c514d4
+0,         39,         39,        1,   152064, 0x043e0347
+0,         40,         40,        1,   152064, 0x4d11131b
+0,         41,         41,        1,   152064, 0xb2a05924
+0,         42,         42,        1,   152064, 0xd0097464
+0,         43,         43,        1,   152064, 0x32dfd8c0
+0,         44,         44,        1,   152064, 0xd9ecbf03
+0,         45,         45,        1,   152064, 0x8dcc403f
+0,         46,         46,        1,   152064, 0x95e81af7
+0,         47,         47,        1,   152064, 0xb8018b25
+0,         48,         48,        1,   152064, 0xeecf7281
+0,         49,         49,        1,   152064, 0x23e49602
diff --git a/tests/ref/fate/filter-yadif-mode0 b/tests/ref/fate/filter-yadif-mode0
index 162ba96..4d42890 100644
--- a/tests/ref/fate/filter-yadif-mode0
+++ b/tests/ref/fate/filter-yadif-mode0
@@ -29,4 +29,3 @@
 0,         36,         36,        1,   622080, 0x58afbd5e
 0,         37,         37,        1,   622080, 0xb972f716
 0,         38,         38,        1,   622080, 0x6a6df129
-0,         39,         39,        1,   622080, 0x28b1373d
diff --git a/tests/ref/fate/filter-yadif-mode1 b/tests/ref/fate/filter-yadif-mode1
index f1f513e..08cd518 100644
--- a/tests/ref/fate/filter-yadif-mode1
+++ b/tests/ref/fate/filter-yadif-mode1
@@ -58,6 +58,3 @@
 0,         74,         74,        1,   622080, 0xb972f716
 0,         75,         75,        1,   622080, 0xbb5d01a2
 0,         76,         76,        1,   622080, 0x6a6df129
-0,         77,         77,        1,   622080, 0x9e45371e
-0,         78,         78,        1,   622080, 0x28b1373d
-0,         79,         79,        1,   622080, 0xa1cdb1f2
diff --git a/tests/ref/fate/gif-disposal-background b/tests/ref/fate/gif-disposal-background
new file mode 100644
index 0000000..dbfaa48
--- /dev/null
+++ b/tests/ref/fate/gif-disposal-background
@@ -0,0 +1,6 @@
+#tb 0: 1/100
+0,          0,          0,        1,    10368, 0x8b200cc8
+0,         50,         50,        1,    10368, 0x4e208e4f
+0,        100,        100,        1,    10368, 0x044dba8b
+0,        150,        150,        1,    10368, 0x1d617e09
+0,        200,        200,        1,    10368, 0xae38a6d2
diff --git a/tests/ref/fate/gifenc-bgr4_byte b/tests/ref/fate/gifenc-bgr4_byte
new file mode 100644
index 0000000..67b32af
--- /dev/null
+++ b/tests/ref/fate/gifenc-bgr4_byte
@@ -0,0 +1,174 @@
+#tb 0: 1/100
+0,          0,          0,        1,      507, 0x91080fbf
+0,         10,         10,        1,      213, 0x4f554bd7, S=1,     1024, 0xb6327c81
+0,         20,         20,        1,      130, 0xfe2d2987, S=1,     1024, 0xae3a7c81
+0,         30,         30,        1,      384, 0xc4fea72a, S=1,     1024, 0xb6327c81
+0,         40,         40,        1,      381, 0x050ba2b8, S=1,     1024, 0x9e4a7c81
+0,         50,         50,        1,      430, 0x00cfb2ae, S=1,     1024, 0x9e4a7c81
+0,         60,         60,        1,      517, 0xefb5d826, S=1,     1024, 0x9e4a7c81
+0,         70,         70,        1,      535, 0x326ce62a, S=1,     1024, 0x9e4a7c81
+0,         80,         80,        1,      437, 0x7c5db7bf, S=1,     1024, 0xb6327c81
+0,         90,         90,        1,      923, 0x9fb1a37c, S=1,     1024, 0xb6327c81
+0,        100,        100,        1,      693, 0xa7b549a4, S=1,     1024, 0xb6327c81
+0,        110,        110,        1,     1194, 0x67cd2ab5, S=1,     1024, 0xb6327c81
+0,        120,        120,        1,     1291, 0x1d23539d, S=1,     1024, 0xb6327c81
+0,        130,        130,        1,     1245, 0x065f32e6, S=1,     1024, 0xb6327c81
+0,        140,        140,        1,     1330, 0x83ec51a4, S=1,     1024, 0xb6327c81
+0,        150,        150,        1,     1275, 0xf0f438db, S=1,     1024, 0xb6327c81
+0,        160,        160,        1,     1474, 0xb41c97ee, S=1,     1024, 0xb6327c81
+0,        170,        170,        1,     1783, 0x86164ae5, S=1,     1024, 0xde0a7c81
+0,        180,        180,        1,     1675, 0x092dfa86, S=1,     1024, 0xde0a7c81
+0,        190,        190,        1,     1509, 0x639aaa00, S=1,     1024, 0xde0a7c81
+0,        200,        200,        1,     1705, 0xfd3719d5, S=1,     1024, 0xde0a7c81
+0,        210,        210,        1,     1745, 0x8a761db4, S=1,     1024, 0xde0a7c81
+0,        220,        220,        1,     1642, 0x18830245, S=1,     1024, 0xde0a7c81
+0,        230,        230,        1,     1717, 0x1d251ebd, S=1,     1024, 0xde0a7c81
+0,        240,        240,        1,     1900, 0x2ea879d1, S=1,     1024, 0xde0a7c81
+0,        250,        250,        1,     1806, 0xb071522f, S=1,     1024, 0xde0a7c81
+0,        260,        260,        1,     1915, 0x22d48344, S=1,     1024, 0xde0a7c81
+0,        270,        270,        1,     2100, 0x55fcd063, S=1,     1024, 0xde0a7c81
+0,        280,        280,        1,     2700, 0x7cc5f08b, S=1,     1024, 0xde0a7c81
+0,        290,        290,        1,     2673, 0xb997a80d, S=1,     1024, 0xde0a7c81
+0,        300,        300,        1,     2894, 0x62d9484c, S=1,     1024, 0xde0a7c81
+0,        310,        310,        1,     3257, 0xf753cf24, S=1,     1024, 0xde0a7c81
+0,        320,        320,        1,     3179, 0x34f2c13b, S=1,     1024, 0xde0a7c81
+0,        330,        330,        1,     3296, 0x7c06e72f, S=1,     1024, 0xde0a7c81
+0,        340,        340,        1,     3599, 0xd65f7633, S=1,     1024, 0xde0a7c81
+0,        350,        350,        1,     3698, 0x0b9e9fe2, S=1,     1024, 0xde0a7c81
+0,        360,        360,        1,     3814, 0x1869d3f4, S=1,     1024, 0xde0a7c81
+0,        370,        370,        1,     3626, 0x9be87da6, S=1,     1024, 0xde0a7c81
+0,        380,        380,        1,     2950, 0x048a6055, S=1,     1024, 0xde0a7c81
+0,        390,        390,        1,     3086, 0x64ec8fc2, S=1,     1024, 0xde0a7c81
+0,        400,        400,        1,     3093, 0x94cc8552, S=1,     1024, 0xde0a7c81
+0,        410,        410,        1,     3456, 0x01432c82, S=1,     1024, 0xde0a7c81
+0,        420,        420,        1,     4107, 0x9bea5c65, S=1,     1024, 0xde0a7c81
+0,        430,        430,        1,     4216, 0x23875ba3, S=1,     1024, 0xde0a7c81
+0,        440,        440,        1,     3613, 0xd0684d83, S=1,     1024, 0xde0a7c81
+0,        450,        450,        1,     3909, 0x1995e691, S=1,     1024, 0xde0a7c81
+0,        460,        460,        1,     4460, 0x791ce33c, S=1,     1024, 0xde0a7c81
+0,        470,        470,        1,     4592, 0x04ed2dd0, S=1,     1024, 0xde0a7c81
+0,        480,        480,        1,     4821, 0xbff89882, S=1,     1024, 0xde0a7c81
+0,        490,        490,        1,     5397, 0xf483c31d, S=1,     1024, 0xde0a7c81
+0,        500,        500,        1,     5266, 0x21c695aa, S=1,     1024, 0xde0a7c81
+0,        510,        510,        1,     5415, 0x0efce3ec, S=1,     1024, 0xde0a7c81
+0,        520,        520,        1,     5519, 0x857d071f, S=1,     1024, 0xde0a7c81
+0,        530,        530,        1,     5701, 0x8f885c9c, S=1,     1024, 0xde0a7c81
+0,        540,        540,        1,     6160, 0x48523e83, S=1,     1024, 0xde0a7c81
+0,        550,        550,        1,     6233, 0x8fd2511e, S=1,     1024, 0xde0a7c81
+0,        560,        560,        1,     5911, 0x92d4c516, S=1,     1024, 0xde0a7c81
+0,        570,        570,        1,     5997, 0xbd7cfa15, S=1,     1024, 0xde0a7c81
+0,        580,        580,        1,     5946, 0x8f5fedff, S=1,     1024, 0xde0a7c81
+0,        590,        590,        1,     6468, 0x45c0cb8c, S=1,     1024, 0xde0a7c81
+0,        600,        600,        1,     6737, 0x4e1e39ac, S=1,     1024, 0xde0a7c81
+0,        610,        610,        1,     6275, 0x1d5e8f4c, S=1,     1024, 0xde0a7c81
+0,        620,        620,        1,     6641, 0x844b3aad, S=1,     1024, 0xde0a7c81
+0,        630,        630,        1,     6378, 0x52568640, S=1,     1024, 0xde0a7c81
+0,        640,        640,        1,     6257, 0xfabc585f, S=1,     1024, 0xde0a7c81
+0,        650,        650,        1,     6908, 0xf261701c, S=1,     1024, 0xde0a7c81
+0,        660,        660,        1,     7230, 0xb4f524ce, S=1,     1024, 0xde0a7c81
+0,        670,        670,        1,     7556, 0x89c1a712, S=1,     1024, 0xde0a7c81
+0,        680,        680,        1,     7413, 0x553970a4, S=1,     1024, 0xde0a7c81
+0,        690,        690,        1,     7476, 0x24d2a761, S=1,     1024, 0xde0a7c81
+0,        700,        700,        1,     7595, 0x0ba1e430, S=1,     1024, 0xde0a7c81
+0,        710,        710,        1,     7756, 0x131205c0, S=1,     1024, 0xde0a7c81
+0,        720,        720,        1,     8015, 0xf4536a7f, S=1,     1024, 0xde0a7c81
+0,        730,        730,        1,     8128, 0xba80be2b, S=1,     1024, 0xde0a7c81
+0,        740,        740,        1,     8101, 0x44ceb3a2, S=1,     1024, 0xde0a7c81
+0,        750,        750,        1,     7863, 0x55043dfd, S=1,     1024, 0xde0a7c81
+0,        760,        760,        1,     7960, 0x38399182, S=1,     1024, 0xde0a7c81
+0,        770,        770,        1,     8238, 0x1d52ecf3, S=1,     1024, 0xde0a7c81
+0,        780,        780,        1,     8321, 0xd8d24a5c, S=1,     1024, 0xde0a7c81
+0,        790,        790,        1,     8562, 0x4a0cc02b, S=1,     1024, 0xde0a7c81
+0,        800,        800,        1,     8746, 0x2db40da7, S=1,     1024, 0xde0a7c81
+0,        810,        810,        1,     8578, 0x46f9a4c1, S=1,     1024, 0xde0a7c81
+0,        820,        820,        1,     8878, 0xf58d5a19, S=1,     1024, 0xde0a7c81
+0,        830,        830,        1,     9077, 0x78de57f6, S=1,     1024, 0xde0a7c81
+0,        840,        840,        1,     9310, 0x8c10f77a, S=1,     1024, 0xde0a7c81
+0,        850,        850,        1,     9394, 0x741f431e, S=1,     1024, 0xde0a7c81
+0,        860,        860,        1,     9161, 0x6f499587, S=1,     1024, 0xde0a7c81
+0,        870,        870,        1,     9462, 0x628936c3, S=1,     1024, 0xde0a7c81
+0,        880,        880,        1,     9650, 0x4cb4936e, S=1,     1024, 0xde0a7c81
+0,        890,        890,        1,     9701, 0x5e069c40, S=1,     1024, 0xde0a7c81
+0,        900,        900,        1,     9523, 0x66a13c83, S=1,     1024, 0xde0a7c81
+0,        910,        910,        1,     9891, 0x43ea0e93, S=1,     1024, 0xde0a7c81
+0,        920,        920,        1,    10005, 0x96a849e7, S=1,     1024, 0xde0a7c81
+0,        930,        930,        1,    10038, 0x68032d25, S=1,     1024, 0xde0a7c81
+0,        940,        940,        1,    10086, 0xef59458d, S=1,     1024, 0xde0a7c81
+0,        950,        950,        1,    10438, 0x3466fed0, S=1,     1024, 0xde0a7c81
+0,        960,        960,        1,    10583, 0x8bdd5477, S=1,     1024, 0xde0a7c81
+0,        970,        970,        1,    10581, 0x69d27fee, S=1,     1024, 0xde0a7c81
+0,        980,        980,        1,    10807, 0xde62d6e3, S=1,     1024, 0xde0a7c81
+0,        990,        990,        1,    11111, 0x34eb4c13, S=1,     1024, 0xde0a7c81
+0,       1000,       1000,        1,    11194, 0x584f6b73, S=1,     1024, 0xde0a7c81
+0,       1010,       1010,        1,    11240, 0xc90ba13f, S=1,     1024, 0xde0a7c81
+0,       1020,       1020,        1,    11482, 0x6521f3c4, S=1,     1024, 0xde0a7c81
+0,       1030,       1030,        1,    11680, 0xc62c5bc1, S=1,     1024, 0xde0a7c81
+0,       1040,       1040,        1,    11785, 0xc9bab793, S=1,     1024, 0xde0a7c81
+0,       1050,       1050,        1,    11436, 0xc9c40809, S=1,     1024, 0xde0a7c81
+0,       1060,       1060,        1,    11927, 0x8135c9a6, S=1,     1024, 0xde0a7c81
+0,       1070,       1070,        1,    11932, 0x722abcbe, S=1,     1024, 0xde0a7c81
+0,       1080,       1080,        1,    12280, 0x9cc46f52, S=1,     1024, 0xde0a7c81
+0,       1090,       1090,        1,    12334, 0x04a47f78, S=1,     1024, 0xde0a7c81
+0,       1100,       1100,        1,    12452, 0xa02db188, S=1,     1024, 0xde0a7c81
+0,       1110,       1110,        1,    12695, 0x1a813b2e, S=1,     1024, 0xde0a7c81
+0,       1120,       1120,        1,    12667, 0x31c94f78, S=1,     1024, 0xde0a7c81
+0,       1130,       1130,        1,    12957, 0x4da59f8c, S=1,     1024, 0xde0a7c81
+0,       1140,       1140,        1,    13053, 0x9a63df59, S=1,     1024, 0xde0a7c81
+0,       1150,       1150,        1,    13147, 0x138f2bbd, S=1,     1024, 0xde0a7c81
+0,       1160,       1160,        1,    13171, 0x43c1195f, S=1,     1024, 0xde0a7c81
+0,       1170,       1170,        1,    13197, 0xd32858d3, S=1,     1024, 0xde0a7c81
+0,       1180,       1180,        1,    13211, 0x12c36193, S=1,     1024, 0xde0a7c81
+0,       1190,       1190,        1,    13210, 0xfe496107, S=1,     1024, 0xde0a7c81
+0,       1200,       1200,        1,    13467, 0x4d8ea128, S=1,     1024, 0xde0a7c81
+0,       1210,       1210,        1,    13665, 0x94caddde, S=1,     1024, 0xde0a7c81
+0,       1220,       1220,        1,    13692, 0xe38febd9, S=1,     1024, 0xde0a7c81
+0,       1230,       1230,        1,    13821, 0xee592e62, S=1,     1024, 0xde0a7c81
+0,       1240,       1240,        1,    13946, 0xceb09235, S=1,     1024, 0xde0a7c81
+0,       1250,       1250,        1,    14063, 0x7361d2f5, S=1,     1024, 0xde0a7c81
+0,       1260,       1260,        1,    14124, 0x226bcac1, S=1,     1024, 0xde0a7c81
+0,       1270,       1270,        1,    14331, 0x0649512b, S=1,     1024, 0xde0a7c81
+0,       1280,       1280,        1,    14469, 0x0d7da45b, S=1,     1024, 0xde0a7c81
+0,       1290,       1290,        1,    14536, 0x73cca242, S=1,     1024, 0xde0a7c81
+0,       1300,       1300,        1,    14608, 0x1f3dd14e, S=1,     1024, 0xde0a7c81
+0,       1310,       1310,        1,    14898, 0xd13d258e, S=1,     1024, 0xde0a7c81
+0,       1320,       1320,        1,    14978, 0xfa049fea, S=1,     1024, 0xde0a7c81
+0,       1330,       1330,        1,    15142, 0x1dfad60c, S=1,     1024, 0xde0a7c81
+0,       1340,       1340,        1,    15129, 0x5962bae7, S=1,     1024, 0xde0a7c81
+0,       1350,       1350,        1,    15243, 0x2c2c113b, S=1,     1024, 0xde0a7c81
+0,       1360,       1360,        1,    15337, 0x3cab623b, S=1,     1024, 0xde0a7c81
+0,       1370,       1370,        1,    15638, 0xbff3a100, S=1,     1024, 0xde0a7c81
+0,       1380,       1380,        1,    15912, 0x13bf1fb2, S=1,     1024, 0xde0a7c81
+0,       1390,       1390,        1,    16041, 0x01134246, S=1,     1024, 0xde0a7c81
+0,       1400,       1400,        1,    16228, 0xe2f80035, S=1,     1024, 0xde0a7c81
+0,       1410,       1410,        1,    16262, 0xc8d3ea51, S=1,     1024, 0xde0a7c81
+0,       1420,       1420,        1,    16371, 0xe7da07f2, S=1,     1024, 0xde0a7c81
+0,       1430,       1430,        1,    16661, 0x10ada592, S=1,     1024, 0xde0a7c81
+0,       1440,       1440,        1,    16917, 0xbfb717e5, S=1,     1024, 0xde0a7c81
+0,       1450,       1450,        1,    17149, 0x4074ca41, S=1,     1024, 0xde0a7c81
+0,       1460,       1460,        1,    17172, 0xf749b49f, S=1,     1024, 0xde0a7c81
+0,       1470,       1470,        1,    17315, 0x2abea8a0, S=1,     1024, 0xde0a7c81
+0,       1480,       1480,        1,    17396, 0x02ec1121, S=1,     1024, 0xde0a7c81
+0,       1490,       1490,        1,    17430, 0xdb5cf2d2, S=1,     1024, 0xde0a7c81
+0,       1500,       1500,        1,    17576, 0x7c6552ad, S=1,     1024, 0xde0a7c81
+0,       1510,       1510,        1,    17764, 0x1d198d60, S=1,     1024, 0xde0a7c81
+0,       1520,       1520,        1,    17826, 0xe1727f57, S=1,     1024, 0xde0a7c81
+0,       1530,       1530,        1,    17918, 0xb78d9b9f, S=1,     1024, 0xde0a7c81
+0,       1540,       1540,        1,    17823, 0xc9fabf19, S=1,     1024, 0xde0a7c81
+0,       1550,       1550,        1,    18142, 0xeb5b21a9, S=1,     1024, 0xde0a7c81
+0,       1560,       1560,        1,    18257, 0x7b38822c, S=1,     1024, 0xde0a7c81
+0,       1570,       1570,        1,    18337, 0xd395c279, S=1,     1024, 0xde0a7c81
+0,       1580,       1580,        1,    18293, 0x6c3b3766, S=1,     1024, 0xde0a7c81
+0,       1590,       1590,        1,    18418, 0x2abcbcf8, S=1,     1024, 0xde0a7c81
+0,       1600,       1600,        1,    18607, 0x79424730, S=1,     1024, 0xde0a7c81
+0,       1610,       1610,        1,    18916, 0x8707bbc6, S=1,     1024, 0xde0a7c81
+0,       1620,       1620,        1,    19073, 0xd82c03f6, S=1,     1024, 0xde0a7c81
+0,       1630,       1630,        1,    19168, 0xb7d6fe27, S=1,     1024, 0xde0a7c81
+0,       1640,       1640,        1,    19210, 0x79f301eb, S=1,     1024, 0xde0a7c81
+0,       1650,       1650,        1,    19398, 0x0a5663c6, S=1,     1024, 0xde0a7c81
+0,       1660,       1660,        1,    19480, 0x4fe09e5b, S=1,     1024, 0xde0a7c81
+0,       1670,       1670,        1,    19659, 0xab971088, S=1,     1024, 0xde0a7c81
+0,       1680,       1680,        1,    19672, 0x2e331553, S=1,     1024, 0xde0a7c81
+0,       1690,       1690,        1,    19936, 0x2eea628a, S=1,     1024, 0xde0a7c81
+0,       1700,       1700,        1,    19975, 0xd6bb9ab2, S=1,     1024, 0xde0a7c81
+0,       1710,       1710,        1,    20021, 0xf7e98dc5, S=1,     1024, 0xde0a7c81
+0,       1720,       1720,        1,    20060, 0x20017807, S=1,     1024, 0xde0a7c81
diff --git a/tests/ref/fate/gifenc-bgr8 b/tests/ref/fate/gifenc-bgr8
new file mode 100644
index 0000000..040c233
--- /dev/null
+++ b/tests/ref/fate/gifenc-bgr8
@@ -0,0 +1,174 @@
+#tb 0: 1/100
+0,          0,          0,        1,      552, 0x271a2dd3
+0,         10,         10,        1,      297, 0x90168a95, S=1,     1024, 0xf351799f
+0,         20,         20,        1,      437, 0xc31bce1a, S=1,     1024, 0xf351799f
+0,         30,         30,        1,      450, 0x7c2dcfad, S=1,     1024, 0xf351799f
+0,         40,         40,        1,      547, 0xc131fd3b, S=1,     1024, 0xf351799f
+0,         50,         50,        1,      613, 0x47b82005, S=1,     1024, 0xf351799f
+0,         60,         60,        1,      642, 0x78bb1f5f, S=1,     1024, 0xf351799f
+0,         70,         70,        1,      660, 0x35c033a2, S=1,     1024, 0xf351799f
+0,         80,         80,        1,      821, 0xaf30790b, S=1,     1024, 0xf351799f
+0,         90,         90,        1,     1157, 0x741c2da1, S=1,     1024, 0xf351799f
+0,        100,        100,        1,      179, 0x3a27517c, S=1,     1024, 0xf351799f
+0,        110,        110,        1,     1333, 0x5ee76f3c, S=1,     1024, 0xf351799f
+0,        120,        120,        1,     1638, 0x5f640e86, S=1,     1024, 0xf351799f
+0,        130,        130,        1,     1531, 0xccb8e437, S=1,     1024, 0xf351799f
+0,        140,        140,        1,     1720, 0xc95d45ec, S=1,     1024, 0xf351799f
+0,        150,        150,        1,     1910, 0x56cc831e, S=1,     1024, 0xf351799f
+0,        160,        160,        1,     2124, 0x9cc8e130, S=1,     1024, 0xf351799f
+0,        170,        170,        1,     2247, 0xdf2725b0, S=1,     1024, 0xf351799f
+0,        180,        180,        1,     2311, 0xdc633703, S=1,     1024, 0xf351799f
+0,        190,        190,        1,     2408, 0x91c26f3e, S=1,     1024, 0xf351799f
+0,        200,        200,        1,     2601, 0x8cf3c157, S=1,     1024, 0xf351799f
+0,        210,        210,        1,     2686, 0x8e0b00e5, S=1,     1024, 0xf351799f
+0,        220,        220,        1,     2784, 0xaa880e55, S=1,     1024, 0xf351799f
+0,        230,        230,        1,     2884, 0x46f546f6, S=1,     1024, 0xf351799f
+0,        240,        240,        1,     2981, 0x050d7ad4, S=1,     1024, 0xf351799f
+0,        250,        250,        1,     3101, 0xbcc89bec, S=1,     1024, 0xf351799f
+0,        260,        260,        1,     3252, 0xdb80f3f9, S=1,     1024, 0xf351799f
+0,        270,        270,        1,     3329, 0xe4d42430, S=1,     1024, 0xf351799f
+0,        280,        280,        1,     3571, 0x6c7d8a9f, S=1,     1024, 0xf351799f
+0,        290,        290,        1,     3806, 0x4255f9f2, S=1,     1024, 0xf351799f
+0,        300,        300,        1,     2749, 0x64681c32, S=1,     1024, 0xf351799f
+0,        310,        310,        1,     4031, 0x3b077006, S=1,     1024, 0xf351799f
+0,        320,        320,        1,     3025, 0x86729c1c, S=1,     1024, 0xf351799f
+0,        330,        330,        1,     4294, 0xeb280b37, S=1,     1024, 0xf351799f
+0,        340,        340,        1,     2044, 0x5adcb93b, S=1,     1024, 0xf351799f
+0,        350,        350,        1,     3212, 0xcf79eeed, S=1,     1024, 0xf351799f
+0,        360,        360,        1,     2280, 0x1a394d2f, S=1,     1024, 0xf351799f
+0,        370,        370,        1,     3632, 0x66ad992e, S=1,     1024, 0xf351799f
+0,        380,        380,        1,     3552, 0x23697490, S=1,     1024, 0xf351799f
+0,        390,        390,        1,     3689, 0x868adbb7, S=1,     1024, 0xf351799f
+0,        400,        400,        1,     1558, 0x7a13e53b, S=1,     1024, 0xf351799f
+0,        410,        410,        1,      939, 0xe565cba1, S=1,     1024, 0xf351799f
+0,        420,        420,        1,      273, 0x3687799b, S=1,     1024, 0xf351799f
+0,        430,        430,        1,      929, 0x788ab0c3, S=1,     1024, 0xf351799f
+0,        440,        440,        1,      271, 0xe7af807c, S=1,     1024, 0xf351799f
+0,        450,        450,        1,      196, 0xf5ab51ee, S=1,     1024, 0xf351799f
+0,        460,        460,        1,     4299, 0x67ec0d55, S=1,     1024, 0xf351799f
+0,        470,        470,        1,     4894, 0x7315406b, S=1,     1024, 0xf351799f
+0,        480,        480,        1,     4927, 0x092e19d6, S=1,     1024, 0xf351799f
+0,        490,        490,        1,     4941, 0x58a357da, S=1,     1024, 0xf351799f
+0,        500,        500,        1,     4153, 0x7582ac32, S=1,     1024, 0xf351799f
+0,        510,        510,        1,     4677, 0xeaa3c04f, S=1,     1024, 0xf351799f
+0,        520,        520,        1,     4740, 0x636bb580, S=1,     1024, 0xf351799f
+0,        530,        530,        1,     4981, 0x31d556d4, S=1,     1024, 0xf351799f
+0,        540,        540,        1,     5179, 0x860fc6a1, S=1,     1024, 0xf351799f
+0,        550,        550,        1,     5046, 0xce9183d3, S=1,     1024, 0xf351799f
+0,        560,        560,        1,     5140, 0xa6d7b9af, S=1,     1024, 0xf351799f
+0,        570,        570,        1,     4288, 0xbc3af716, S=1,     1024, 0xf351799f
+0,        580,        580,        1,     5079, 0xa8d59e01, S=1,     1024, 0xf351799f
+0,        590,        590,        1,     5284, 0xea34e3b3, S=1,     1024, 0xf351799f
+0,        600,        600,        1,     5426, 0x556a15cd, S=1,     1024, 0xf351799f
+0,        610,        610,        1,     4644, 0x7cc08935, S=1,     1024, 0xf351799f
+0,        620,        620,        1,     5263, 0x7536cf7d, S=1,     1024, 0xf351799f
+0,        630,        630,        1,     5221, 0x9fbac3ca, S=1,     1024, 0xf351799f
+0,        640,        640,        1,     5216, 0x65f09bd1, S=1,     1024, 0xf351799f
+0,        650,        650,        1,     5394, 0x1293ff65, S=1,     1024, 0xf351799f
+0,        660,        660,        1,     5219, 0x9aa2dcc4, S=1,     1024, 0xf351799f
+0,        670,        670,        1,     5704, 0xba42dd96, S=1,     1024, 0xf351799f
+0,        680,        680,        1,     5636, 0xcb91a25b, S=1,     1024, 0xf351799f
+0,        690,        690,        1,     5818, 0x8dc0df92, S=1,     1024, 0xf351799f
+0,        700,        700,        1,     5763, 0x51d5d5f0, S=1,     1024, 0xf351799f
+0,        710,        710,        1,     6116, 0x09558b48, S=1,     1024, 0xf351799f
+0,        720,        720,        1,     6069, 0x41926817, S=1,     1024, 0xf351799f
+0,        730,        730,        1,     5796, 0x7fbeda44, S=1,     1024, 0xf351799f
+0,        740,        740,        1,     5999, 0xe07d3770, S=1,     1024, 0xf351799f
+0,        750,        750,        1,     6220, 0x6607b06f, S=1,     1024, 0xf351799f
+0,        760,        760,        1,     6374, 0x7628e533, S=1,     1024, 0xf351799f
+0,        770,        770,        1,     6465, 0xfe956b15, S=1,     1024, 0xf351799f
+0,        780,        780,        1,     7019, 0x6c9a1aef, S=1,     1024, 0xf351799f
+0,        790,        790,        1,     7255, 0x5fa5c1bf, S=1,     1024, 0xf351799f
+0,        800,        800,        1,     8197, 0xf11d6ef2, S=1,     1024, 0xf351799f
+0,        810,        810,        1,     8358, 0x027279e8, S=1,     1024, 0xf351799f
+0,        820,        820,        1,     7708, 0x607f8e8b, S=1,     1024, 0xf351799f
+0,        830,        830,        1,     7412, 0x6bb2105f, S=1,     1024, 0xf351799f
+0,        840,        840,        1,     7540, 0xdc032153, S=1,     1024, 0xf351799f
+0,        850,        850,        1,     7948, 0x916ecd8b, S=1,     1024, 0xf351799f
+0,        860,        860,        1,     8408, 0x1f97d414, S=1,     1024, 0xf351799f
+0,        870,        870,        1,     8056, 0x9cbf159c, S=1,     1024, 0xf351799f
+0,        880,        880,        1,     7401, 0x2625addb, S=1,     1024, 0xf351799f
+0,        890,        890,        1,     7494, 0x2877eacb, S=1,     1024, 0xf351799f
+0,        900,        900,        1,     7806, 0xe32574a3, S=1,     1024, 0xf351799f
+0,        910,        910,        1,     7768, 0x25ed7ee7, S=1,     1024, 0xf351799f
+0,        920,        920,        1,     7749, 0x6d8e978e, S=1,     1024, 0xf351799f
+0,        930,        930,        1,     8047, 0xec4b150c, S=1,     1024, 0xf351799f
+0,        940,        940,        1,     7617, 0x574430d4, S=1,     1024, 0xf351799f
+0,        950,        950,        1,     7979, 0x0eb1cf2a, S=1,     1024, 0xf351799f
+0,        960,        960,        1,    12062, 0xb49d9125, S=1,     1024, 0xf351799f
+0,        970,        970,        1,    12317, 0x2d8fd6e9, S=1,     1024, 0xf351799f
+0,        980,        980,        1,    12217, 0x9b3be549, S=1,     1024, 0xf351799f
+0,        990,        990,        1,    11226, 0x74889117, S=1,     1024, 0xf351799f
+0,       1000,       1000,        1,    11108, 0x5e5b0afd, S=1,     1024, 0xf351799f
+0,       1010,       1010,        1,    11366, 0xb38e8d15, S=1,     1024, 0xf351799f
+0,       1020,       1020,        1,    11896, 0xeb3e35ca, S=1,     1024, 0xf351799f
+0,       1030,       1030,        1,    11479, 0xbf7581e9, S=1,     1024, 0xf351799f
+0,       1040,       1040,        1,    13395, 0x415b38d8, S=1,     1024, 0xf351799f
+0,       1050,       1050,        1,    12913, 0x61544631, S=1,     1024, 0xf351799f
+0,       1060,       1060,        1,    13864, 0xd39fe768, S=1,     1024, 0xf351799f
+0,       1070,       1070,        1,    13551, 0x76c167d1, S=1,     1024, 0xf351799f
+0,       1080,       1080,        1,    14041, 0x2f206888, S=1,     1024, 0xf351799f
+0,       1090,       1090,        1,    14144, 0x9ec030d3, S=1,     1024, 0xf351799f
+0,       1100,       1100,        1,    14277, 0xa84b3a9b, S=1,     1024, 0xf351799f
+0,       1110,       1110,        1,    14424, 0xf5f1e06e, S=1,     1024, 0xf351799f
+0,       1120,       1120,        1,    14689, 0xbca0adb5, S=1,     1024, 0xf351799f
+0,       1130,       1130,        1,    14598, 0xc1d45745, S=1,     1024, 0xf351799f
+0,       1140,       1140,        1,    15213, 0x8f3080fc, S=1,     1024, 0xf351799f
+0,       1150,       1150,        1,    15425, 0xb0aa8f59, S=1,     1024, 0xf351799f
+0,       1160,       1160,        1,    15595, 0x1406e5d5, S=1,     1024, 0xf351799f
+0,       1170,       1170,        1,    15598, 0x48ec7d08, S=1,     1024, 0xf351799f
+0,       1180,       1180,        1,    15863, 0x5381db7b, S=1,     1024, 0xf351799f
+0,       1190,       1190,        1,    15717, 0xb87a1b87, S=1,     1024, 0xf351799f
+0,       1200,       1200,        1,    16078, 0x5bab2453, S=1,     1024, 0xf351799f
+0,       1210,       1210,        1,    16225, 0xa1f88113, S=1,     1024, 0xf351799f
+0,       1220,       1220,        1,    16135, 0x6af2f4e1, S=1,     1024, 0xf351799f
+0,       1230,       1230,        1,    16661, 0xf02a3343, S=1,     1024, 0xf351799f
+0,       1240,       1240,        1,    16619, 0xc71935a4, S=1,     1024, 0xf351799f
+0,       1250,       1250,        1,    16829, 0x29849844, S=1,     1024, 0xf351799f
+0,       1260,       1260,        1,    16944, 0x3423ae77, S=1,     1024, 0xf351799f
+0,       1270,       1270,        1,    17119, 0x609b4409, S=1,     1024, 0xf351799f
+0,       1280,       1280,        1,    17150, 0xf85dfd31, S=1,     1024, 0xf351799f
+0,       1290,       1290,        1,    17321, 0x38eccb10, S=1,     1024, 0xf351799f
+0,       1300,       1300,        1,    17395, 0x0ba08b85, S=1,     1024, 0xf351799f
+0,       1310,       1310,        1,    17666, 0x6fbc0264, S=1,     1024, 0xf351799f
+0,       1320,       1320,        1,    17730, 0x3dcc64a6, S=1,     1024, 0xf351799f
+0,       1330,       1330,        1,    17934, 0xb539974b, S=1,     1024, 0xf351799f
+0,       1340,       1340,        1,    17944, 0x2214ec94, S=1,     1024, 0xf351799f
+0,       1350,       1350,        1,    18238, 0x70f9ff1d, S=1,     1024, 0xf351799f
+0,       1360,       1360,        1,    18390, 0xb8319208, S=1,     1024, 0xf351799f
+0,       1370,       1370,        1,    18543, 0x45a1c02f, S=1,     1024, 0xf351799f
+0,       1380,       1380,        1,    18939, 0x2789a88c, S=1,     1024, 0xf351799f
+0,       1390,       1390,        1,    19145, 0x5daafd7a, S=1,     1024, 0xf351799f
+0,       1400,       1400,        1,    19120, 0x565f80e6, S=1,     1024, 0xf351799f
+0,       1410,       1410,        1,    19130, 0xff70cc21, S=1,     1024, 0xf351799f
+0,       1420,       1420,        1,    19494, 0xbfa284db, S=1,     1024, 0xf351799f
+0,       1430,       1430,        1,    19534, 0x3d40743b, S=1,     1024, 0xf351799f
+0,       1440,       1440,        1,    19747, 0x33c9b108, S=1,     1024, 0xf351799f
+0,       1450,       1450,        1,    20114, 0x9d223e36, S=1,     1024, 0xf351799f
+0,       1460,       1460,        1,    20257, 0xe7bdaf43, S=1,     1024, 0xf351799f
+0,       1470,       1470,        1,    20370, 0x0c5f1970, S=1,     1024, 0xf351799f
+0,       1480,       1480,        1,    20292, 0x6986d20e, S=1,     1024, 0xf351799f
+0,       1490,       1490,        1,    20491, 0xd88e4c08, S=1,     1024, 0xf351799f
+0,       1500,       1500,        1,    20647, 0x1aefaffc, S=1,     1024, 0xf351799f
+0,       1510,       1510,        1,    20666, 0x43e4aaaa, S=1,     1024, 0xf351799f
+0,       1520,       1520,        1,    21007, 0xa7ca3ef0, S=1,     1024, 0xf351799f
+0,       1530,       1530,        1,    21058, 0x06814351, S=1,     1024, 0xf351799f
+0,       1540,       1540,        1,    21153, 0x3c852b10, S=1,     1024, 0xf351799f
+0,       1550,       1550,        1,    21078, 0x8df15855, S=1,     1024, 0xf351799f
+0,       1560,       1560,        1,    21458, 0xd3a531d6, S=1,     1024, 0xf351799f
+0,       1570,       1570,        1,    21669, 0x88baca53, S=1,     1024, 0xf351799f
+0,       1580,       1580,        1,    21581, 0xd692fa1f, S=1,     1024, 0xf351799f
+0,       1590,       1590,        1,    21654, 0x30fb9061, S=1,     1024, 0xf351799f
+0,       1600,       1600,        1,    21987, 0xe7646d8b, S=1,     1024, 0xf351799f
+0,       1610,       1610,        1,    22205, 0x0fc55b6a, S=1,     1024, 0xf351799f
+0,       1620,       1620,        1,    22475, 0x4bc4c032, S=1,     1024, 0xf351799f
+0,       1630,       1630,        1,    22490, 0x58ca23f6, S=1,     1024, 0xf351799f
+0,       1640,       1640,        1,    22460, 0xf9ceb0ac, S=1,     1024, 0xf351799f
+0,       1650,       1650,        1,    22861, 0xb05f0f84, S=1,     1024, 0xf351799f
+0,       1660,       1660,        1,    22746, 0x0df23a5c, S=1,     1024, 0xf351799f
+0,       1670,       1670,        1,    23164, 0x755347ac, S=1,     1024, 0xf351799f
+0,       1680,       1680,        1,    23273, 0x9781a34f, S=1,     1024, 0xf351799f
+0,       1690,       1690,        1,    23211, 0x69c7606b, S=1,     1024, 0xf351799f
+0,       1700,       1700,        1,    23648, 0xdafde037, S=1,     1024, 0xf351799f
+0,       1710,       1710,        1,    23675, 0x2a2147ed, S=1,     1024, 0xf351799f
+0,       1720,       1720,        1,    23874, 0x12c184b6, S=1,     1024, 0xf351799f
diff --git a/tests/ref/fate/gifenc-gray b/tests/ref/fate/gifenc-gray
new file mode 100644
index 0000000..404237d
--- /dev/null
+++ b/tests/ref/fate/gifenc-gray
@@ -0,0 +1,174 @@
+#tb 0: 1/100
+0,          0,          0,        1,      568, 0xe1d43487
+0,         10,         10,        1,      142, 0x92d43281, S=1,     1024, 0xc2f67c9f
+0,         20,         20,        1,      130, 0x29383852, S=1,     1024, 0xc2f67c9f
+0,         30,         30,        1,      129, 0x036732fa, S=1,     1024, 0xc2f67c9f
+0,         40,         40,        1,      145, 0xcbc036bb, S=1,     1024, 0xc2f67c9f
+0,         50,         50,        1,      135, 0x6e4536d1, S=1,     1024, 0xc2f67c9f
+0,         60,         60,        1,      127, 0xe73f2f55, S=1,     1024, 0xc2f67c9f
+0,         70,         70,        1,      119, 0x7912307f, S=1,     1024, 0xc2f67c9f
+0,         80,         80,        1,      132, 0xce58343c, S=1,     1024, 0xc2f67c9f
+0,         90,         90,        1,      126, 0x1fcd2b9c, S=1,     1024, 0xc2f67c9f
+0,        100,        100,        1,      127, 0x6c293369, S=1,     1024, 0xc2f67c9f
+0,        110,        110,        1,      126, 0x19162e3f, S=1,     1024, 0xc2f67c9f
+0,        120,        120,        1,      119, 0x06602dec, S=1,     1024, 0xc2f67c9f
+0,        130,        130,        1,      118, 0x8b083037, S=1,     1024, 0xc2f67c9f
+0,        140,        140,        1,      115, 0xa8372c6d, S=1,     1024, 0xc2f67c9f
+0,        150,        150,        1,      114, 0x8d0f2dbd, S=1,     1024, 0xc2f67c9f
+0,        160,        160,        1,      129, 0xb5ab318d, S=1,     1024, 0xc2f67c9f
+0,        170,        170,        1,      129, 0x278e34bd, S=1,     1024, 0xc2f67c9f
+0,        180,        180,        1,      119, 0x408e3131, S=1,     1024, 0xc2f67c9f
+0,        190,        190,        1,      121, 0x069a2c25, S=1,     1024, 0xc2f67c9f
+0,        200,        200,        1,      127, 0x24793635, S=1,     1024, 0xc2f67c9f
+0,        210,        210,        1,      110, 0xe30a29cc, S=1,     1024, 0xc2f67c9f
+0,        220,        220,        1,      116, 0xf7b827f0, S=1,     1024, 0xc2f67c9f
+0,        230,        230,        1,      118, 0xf5f62ff6, S=1,     1024, 0xc2f67c9f
+0,        240,        240,        1,      115, 0xb7c92c27, S=1,     1024, 0xc2f67c9f
+0,        250,        250,        1,      114, 0x0a642b7e, S=1,     1024, 0xc2f67c9f
+0,        260,        260,        1,      114, 0x07b32c7c, S=1,     1024, 0xc2f67c9f
+0,        270,        270,        1,      118, 0x1f4432b8, S=1,     1024, 0xc2f67c9f
+0,        280,        280,        1,      144, 0xa81e3c75, S=1,     1024, 0xc2f67c9f
+0,        290,        290,        1,      121, 0xacb828d6, S=1,     1024, 0xc2f67c9f
+0,        300,        300,        1,      148, 0xb245426a, S=1,     1024, 0xc2f67c9f
+0,        310,        310,        1,      124, 0x3d173009, S=1,     1024, 0xc2f67c9f
+0,        320,        320,        1,      151, 0xa57c443d, S=1,     1024, 0xc2f67c9f
+0,        330,        330,        1,      121, 0x93da2d00, S=1,     1024, 0xc2f67c9f
+0,        340,        340,        1,      124, 0x1dd02f53, S=1,     1024, 0xc2f67c9f
+0,        350,        350,        1,      117, 0xb0942d05, S=1,     1024, 0xc2f67c9f
+0,        360,        360,        1,      117, 0xb5af292b, S=1,     1024, 0xc2f67c9f
+0,        370,        370,        1,      116, 0x731a30ff, S=1,     1024, 0xc2f67c9f
+0,        380,        380,        1,      123, 0x16342dde, S=1,     1024, 0xc2f67c9f
+0,        390,        390,        1,      117, 0xef9e2f92, S=1,     1024, 0xc2f67c9f
+0,        400,        400,        1,      110, 0x646e2a4a, S=1,     1024, 0xc2f67c9f
+0,        410,        410,        1,      110, 0xe44f2be1, S=1,     1024, 0xc2f67c9f
+0,        420,        420,        1,      108, 0x58f22c11, S=1,     1024, 0xc2f67c9f
+0,        430,        430,        1,      112, 0x2c702d3e, S=1,     1024, 0xc2f67c9f
+0,        440,        440,        1,      111, 0x7b412ab9, S=1,     1024, 0xc2f67c9f
+0,        450,        450,        1,      112, 0xe95d2dfb, S=1,     1024, 0xc2f67c9f
+0,        460,        460,        1,      109, 0x2baf2bde, S=1,     1024, 0xc2f67c9f
+0,        470,        470,        1,      114, 0x3a772c6a, S=1,     1024, 0xc2f67c9f
+0,        480,        480,        1,      110, 0xfe702831, S=1,     1024, 0xc2f67c9f
+0,        490,        490,        1,      125, 0xa31f344b, S=1,     1024, 0xc2f67c9f
+0,        500,        500,        1,      119, 0xbc31318d, S=1,     1024, 0xc2f67c9f
+0,        510,        510,        1,      115, 0xc7b62b8f, S=1,     1024, 0xc2f67c9f
+0,        520,        520,        1,      118, 0x7cf22dcb, S=1,     1024, 0xc2f67c9f
+0,        530,        530,        1,      119, 0xe16c2f96, S=1,     1024, 0xc2f67c9f
+0,        540,        540,        1,      119, 0x86d43604, S=1,     1024, 0xc2f67c9f
+0,        550,        550,        1,      109, 0x22b72af1, S=1,     1024, 0xc2f67c9f
+0,        560,        560,        1,      103, 0x12982731, S=1,     1024, 0xc2f67c9f
+0,        570,        570,        1,      114, 0x6ef22e10, S=1,     1024, 0xc2f67c9f
+0,        580,        580,        1,      109, 0x621b26dd, S=1,     1024, 0xc2f67c9f
+0,        590,        590,        1,      110, 0xfe062836, S=1,     1024, 0xc2f67c9f
+0,        600,        600,        1,      112, 0x4a8e2ce3, S=1,     1024, 0xc2f67c9f
+0,        610,        610,        1,      111, 0xa7ae2865, S=1,     1024, 0xc2f67c9f
+0,        620,        620,        1,      103, 0x80bc2b33, S=1,     1024, 0xc2f67c9f
+0,        630,        630,        1,      114, 0xc2f92f3f, S=1,     1024, 0xc2f67c9f
+0,        640,        640,        1,      108, 0x81332cdf, S=1,     1024, 0xc2f67c9f
+0,        650,        650,        1,      121, 0x0d1133ad, S=1,     1024, 0xc2f67c9f
+0,        660,        660,        1,      136, 0x2c0c3481, S=1,     1024, 0xc2f67c9f
+0,        670,        670,        1,      108, 0x55222d1f, S=1,     1024, 0xc2f67c9f
+0,        680,        680,        1,      125, 0x9cc536df, S=1,     1024, 0xc2f67c9f
+0,        690,        690,        1,      114, 0x04602ae4, S=1,     1024, 0xc2f67c9f
+0,        700,        700,        1,       99, 0xd58f291d, S=1,     1024, 0xc2f67c9f
+0,        710,        710,        1,       98, 0xcb8f2807, S=1,     1024, 0xc2f67c9f
+0,        720,        720,        1,      109, 0x7f292c9b, S=1,     1024, 0xc2f67c9f
+0,        730,        730,        1,      106, 0x3daa297f, S=1,     1024, 0xc2f67c9f
+0,        740,        740,        1,      100, 0xb7032b73, S=1,     1024, 0xc2f67c9f
+0,        750,        750,        1,       88, 0xd56b218e, S=1,     1024, 0xc2f67c9f
+0,        760,        760,        1,       93, 0xdcfd2353, S=1,     1024, 0xc2f67c9f
+0,        770,        770,        1,      112, 0x35322b03, S=1,     1024, 0xc2f67c9f
+0,        780,        780,        1,      112, 0x7c2b2d67, S=1,     1024, 0xc2f67c9f
+0,        790,        790,        1,      204, 0xd02c5bd9, S=1,     1024, 0xc2f67c9f
+0,        800,        800,        1,      204, 0x82055f0a, S=1,     1024, 0xc2f67c9f
+0,        810,        810,        1,      115, 0x76472e9b, S=1,     1024, 0xc2f67c9f
+0,        820,        820,        1,      110, 0x22b12abc, S=1,     1024, 0xc2f67c9f
+0,        830,        830,        1,      116, 0x50a0318e, S=1,     1024, 0xc2f67c9f
+0,        840,        840,        1,      115, 0x10632f57, S=1,     1024, 0xc2f67c9f
+0,        850,        850,        1,      120, 0x9c5631e3, S=1,     1024, 0xc2f67c9f
+0,        860,        860,        1,      114, 0x0e192ed8, S=1,     1024, 0xc2f67c9f
+0,        870,        870,        1,      109, 0xdedb2a99, S=1,     1024, 0xc2f67c9f
+0,        880,        880,        1,      105, 0x639c2cf5, S=1,     1024, 0xc2f67c9f
+0,        890,        890,        1,      117, 0x4aff2afa, S=1,     1024, 0xc2f67c9f
+0,        900,        900,        1,      103, 0x44842781, S=1,     1024, 0xc2f67c9f
+0,        910,        910,        1,      147, 0x22493e48, S=1,     1024, 0xc2f67c9f
+0,        920,        920,        1,      117, 0x3cb02d86, S=1,     1024, 0xc2f67c9f
+0,        930,        930,        1,      111, 0x813d2e1f, S=1,     1024, 0xc2f67c9f
+0,        940,        940,        1,       83, 0xb6961b02, S=1,     1024, 0xc2f67c9f
+0,        950,        950,        1,      159, 0x5f2e43d0, S=1,     1024, 0xc2f67c9f
+0,        960,        960,        1,      126, 0x56d42d1d, S=1,     1024, 0xc2f67c9f
+0,        970,        970,        1,      164, 0xea6c464d, S=1,     1024, 0xc2f67c9f
+0,        980,        980,        1,      133, 0x21113425, S=1,     1024, 0xc2f67c9f
+0,        990,        990,        1,      125, 0xfc6e322e, S=1,     1024, 0xc2f67c9f
+0,       1000,       1000,        1,      116, 0x226830da, S=1,     1024, 0xc2f67c9f
+0,       1010,       1010,        1,      130, 0xa71a317d, S=1,     1024, 0xc2f67c9f
+0,       1020,       1020,        1,      127, 0x5b5433af, S=1,     1024, 0xc2f67c9f
+0,       1030,       1030,        1,      172, 0x0ed64b21, S=1,     1024, 0xc2f67c9f
+0,       1040,       1040,        1,      142, 0x9733380f, S=1,     1024, 0xc2f67c9f
+0,       1050,       1050,        1,      184, 0x358b533c, S=1,     1024, 0xc2f67c9f
+0,       1060,       1060,        1,      155, 0x5304441c, S=1,     1024, 0xc2f67c9f
+0,       1070,       1070,        1,      139, 0x15743a98, S=1,     1024, 0xc2f67c9f
+0,       1080,       1080,        1,      129, 0xc5782f81, S=1,     1024, 0xc2f67c9f
+0,       1090,       1090,        1,      144, 0x1ee637c8, S=1,     1024, 0xc2f67c9f
+0,       1100,       1100,        1,      133, 0x69403297, S=1,     1024, 0xc2f67c9f
+0,       1110,       1110,        1,      192, 0x128553a4, S=1,     1024, 0xc2f67c9f
+0,       1120,       1120,        1,      159, 0xea783f5a, S=1,     1024, 0xc2f67c9f
+0,       1130,       1130,        1,      201, 0xc04252a1, S=1,     1024, 0xc2f67c9f
+0,       1140,       1140,        1,      161, 0xb56b44f6, S=1,     1024, 0xc2f67c9f
+0,       1150,       1150,        1,      136, 0x402130e8, S=1,     1024, 0xc2f67c9f
+0,       1160,       1160,        1,      117, 0x694c3490, S=1,     1024, 0xc2f67c9f
+0,       1170,       1170,        1,      151, 0x96803b5d, S=1,     1024, 0xc2f67c9f
+0,       1180,       1180,        1,      135, 0x9a9b399f, S=1,     1024, 0xc2f67c9f
+0,       1190,       1190,        1,      147, 0x7e83348d, S=1,     1024, 0xc2f67c9f
+0,       1200,       1200,        1,      135, 0x932e3720, S=1,     1024, 0xc2f67c9f
+0,       1210,       1210,        1,      183, 0x822946e2, S=1,     1024, 0xc2f67c9f
+0,       1220,       1220,        1,      116, 0x93652e25, S=1,     1024, 0xc2f67c9f
+0,       1230,       1230,        1,      117, 0x77063135, S=1,     1024, 0xc2f67c9f
+0,       1240,       1240,        1,      109, 0x12752965, S=1,     1024, 0xc2f67c9f
+0,       1250,       1250,        1,      111, 0x8fd62b54, S=1,     1024, 0xc2f67c9f
+0,       1260,       1260,        1,      188, 0x6c1850e5, S=1,     1024, 0xc2f67c9f
+0,       1270,       1270,        1,      121, 0x15003168, S=1,     1024, 0xc2f67c9f
+0,       1280,       1280,        1,      115, 0x93482f77, S=1,     1024, 0xc2f67c9f
+0,       1290,       1290,        1,      120, 0x36552de5, S=1,     1024, 0xc2f67c9f
+0,       1300,       1300,        1,      117, 0x74472f78, S=1,     1024, 0xc2f67c9f
+0,       1310,       1310,        1,      188, 0xadac5400, S=1,     1024, 0xc2f67c9f
+0,       1320,       1320,        1,      125, 0xccb83070, S=1,     1024, 0xc2f67c9f
+0,       1330,       1330,        1,      118, 0x606e2d29, S=1,     1024, 0xc2f67c9f
+0,       1340,       1340,        1,      130, 0x77eb3516, S=1,     1024, 0xc2f67c9f
+0,       1350,       1350,        1,      120, 0x500f3045, S=1,     1024, 0xc2f67c9f
+0,       1360,       1360,        1,      164, 0x729244f4, S=1,     1024, 0xc2f67c9f
+0,       1370,       1370,        1,      234, 0xff2969ee, S=1,     1024, 0xc2f67c9f
+0,       1380,       1380,        1,      207, 0xf9366155, S=1,     1024, 0xc2f67c9f
+0,       1390,       1390,        1,      210, 0x31555d58, S=1,     1024, 0xc2f67c9f
+0,       1400,       1400,        1,      118, 0x7f9a32bd, S=1,     1024, 0xc2f67c9f
+0,       1410,       1410,        1,      120, 0xfba63099, S=1,     1024, 0xc2f67c9f
+0,       1420,       1420,        1,      114, 0xcb382d23, S=1,     1024, 0xc2f67c9f
+0,       1430,       1430,        1,      115, 0x10f32bad, S=1,     1024, 0xc2f67c9f
+0,       1440,       1440,        1,      206, 0x405e630e, S=1,     1024, 0xc2f67c9f
+0,       1450,       1450,        1,      216, 0xf6e6635f, S=1,     1024, 0xc2f67c9f
+0,       1460,       1460,        1,      123, 0x43aa33b8, S=1,     1024, 0xc2f67c9f
+0,       1470,       1470,        1,      121, 0x780d3018, S=1,     1024, 0xc2f67c9f
+0,       1480,       1480,        1,      121, 0xdba92f73, S=1,     1024, 0xc2f67c9f
+0,       1490,       1490,        1,      118, 0x35b32e26, S=1,     1024, 0xc2f67c9f
+0,       1500,       1500,        1,      116, 0x79ff2c29, S=1,     1024, 0xc2f67c9f
+0,       1510,       1510,        1,      110, 0xb0552ba1, S=1,     1024, 0xc2f67c9f
+0,       1520,       1520,        1,      117, 0x08f62fd4, S=1,     1024, 0xc2f67c9f
+0,       1530,       1530,        1,      112, 0x0dac2e9d, S=1,     1024, 0xc2f67c9f
+0,       1540,       1540,        1,      142, 0x092138a5, S=1,     1024, 0xc2f67c9f
+0,       1550,       1550,        1,      119, 0x752a2fc6, S=1,     1024, 0xc2f67c9f
+0,       1560,       1560,        1,      105, 0xec5824cd, S=1,     1024, 0xc2f67c9f
+0,       1570,       1570,        1,      115, 0xea282f22, S=1,     1024, 0xc2f67c9f
+0,       1580,       1580,        1,      108, 0x60b72ece, S=1,     1024, 0xc2f67c9f
+0,       1590,       1590,        1,      139, 0x6ec63b92, S=1,     1024, 0xc2f67c9f
+0,       1600,       1600,        1,      216, 0x2c906371, S=1,     1024, 0xc2f67c9f
+0,       1610,       1610,        1,      226, 0x77b1687d, S=1,     1024, 0xc2f67c9f
+0,       1620,       1620,        1,      155, 0x8180455a, S=1,     1024, 0xc2f67c9f
+0,       1630,       1630,        1,      121, 0x66092c73, S=1,     1024, 0xc2f67c9f
+0,       1640,       1640,        1,      123, 0xf9f22cb9, S=1,     1024, 0xc2f67c9f
+0,       1650,       1650,        1,      220, 0x65525fcb, S=1,     1024, 0xc2f67c9f
+0,       1660,       1660,        1,      153, 0x4b9f414c, S=1,     1024, 0xc2f67c9f
+0,       1670,       1670,        1,      120, 0x072e3379, S=1,     1024, 0xc2f67c9f
+0,       1680,       1680,        1,      109, 0xe46a2b38, S=1,     1024, 0xc2f67c9f
+0,       1690,       1690,        1,      118, 0xf0f12d62, S=1,     1024, 0xc2f67c9f
+0,       1700,       1700,        1,      117, 0xcb9e2cbb, S=1,     1024, 0xc2f67c9f
+0,       1710,       1710,        1,      137, 0x1ae83718, S=1,     1024, 0xc2f67c9f
+0,       1720,       1720,        1,      161, 0x87364667, S=1,     1024, 0xc2f67c9f
diff --git a/tests/ref/fate/gifenc-pal8 b/tests/ref/fate/gifenc-pal8
new file mode 100644
index 0000000..4ae4d17
--- /dev/null
+++ b/tests/ref/fate/gifenc-pal8
@@ -0,0 +1,174 @@
+#tb 0: 1/100
+0,          0,          0,        1,     1320, 0x95d1a9e9, S=1,     1024, 0xec907a9e
+0,         10,         10,        1,     1463, 0xa697fe95, S=1,     1024, 0xec907a9e
+0,         20,         20,        1,     1637, 0x294d54f9, S=1,     1024, 0xec907a9e
+0,         30,         30,        1,     1755, 0x7e0b9c4d, S=1,     1024, 0xec907a9e
+0,         40,         40,        1,     1896, 0x4aacc768, S=1,     1024, 0xec907a9e
+0,         50,         50,        1,     2038, 0x209d1490, S=1,     1024, 0xec907a9e
+0,         60,         60,        1,     2168, 0x0c405606, S=1,     1024, 0xec907a9e
+0,         70,         70,        1,     2258, 0xc2fa9229, S=1,     1024, 0xec907a9e
+0,         80,         80,        1,     2441, 0x2076ec47, S=1,     1024, 0xec907a9e
+0,         90,         90,        1,     2565, 0x15742730, S=1,     1024, 0xec907a9e
+0,        100,        100,        1,     2733, 0x2eb57d95, S=1,     1024, 0xec907a9e
+0,        110,        110,        1,     2849, 0x9f16ad46, S=1,     1024, 0xec907a9e
+0,        120,        120,        1,     2981, 0xf139f908, S=1,     1024, 0xec907a9e
+0,        130,        130,        1,     3081, 0x0b2a4aeb, S=1,     1024, 0xec907a9e
+0,        140,        140,        1,     3200, 0x35227f42, S=1,     1024, 0xec907a9e
+0,        150,        150,        1,     3315, 0x35edc001, S=1,     1024, 0xec907a9e
+0,        160,        160,        1,     3462, 0x27a20d23, S=1,     1024, 0xec907a9e
+0,        170,        170,        1,     3599, 0x83c52b01, S=1,     1024, 0xec907a9e
+0,        180,        180,        1,     3712, 0x947d49d1, S=1,     1024, 0xec907a9e
+0,        190,        190,        1,     3838, 0xf87eb223, S=1,     1024, 0xec907a9e
+0,        200,        200,        1,     3948, 0x5203eede, S=1,     1024, 0xec907a9e
+0,        210,        210,        1,     4083, 0x32101e33, S=1,     1024, 0xec907a9e
+0,        220,        220,        1,     4186, 0x6b0e5a13, S=1,     1024, 0xec907a9e
+0,        230,        230,        1,     4323, 0x6326a0f5, S=1,     1024, 0xec907a9e
+0,        240,        240,        1,     4426, 0x5f6bd655, S=1,     1024, 0xec907a9e
+0,        250,        250,        1,     4564, 0xc62005fd, S=1,     1024, 0xec907a9e
+0,        260,        260,        1,     4664, 0x43e97006, S=1,     1024, 0xec907a9e
+0,        270,        270,        1,     4808, 0xf6e08d4f, S=1,     1024, 0xec907a9e
+0,        280,        280,        1,     4932, 0x2d51e0b5, S=1,     1024, 0xec907a9e
+0,        290,        290,        1,     5071, 0x25e830c7, S=1,     1024, 0xec907a9e
+0,        300,        300,        1,     5246, 0x452e945c, S=1,     1024, 0xec907a9e
+0,        310,        310,        1,     5353, 0x27eec140, S=1,     1024, 0xec907a9e
+0,        320,        320,        1,     5535, 0xda38038c, S=1,     1024, 0xec907a9e
+0,        330,        330,        1,     5691, 0x358970e4, S=1,     1024, 0xec907a9e
+0,        340,        340,        1,     5835, 0xd89201d8, S=1,     1024, 0xec907a9e
+0,        350,        350,        1,     5966, 0xfbee1fed, S=1,     1024, 0xec907a9e
+0,        360,        360,        1,     6082, 0xd4b88e90, S=1,     1024, 0xec907a9e
+0,        370,        370,        1,     6276, 0x1b02e684, S=1,     1024, 0xec907a9e
+0,        380,        380,        1,     6382, 0xc1db2297, S=1,     1024, 0xec907a9e
+0,        390,        390,        1,     6509, 0xc65473a6, S=1,     1024, 0xec907a9e
+0,        400,        400,        1,     6621, 0xca917a35, S=1,     1024, 0xec907a9e
+0,        410,        410,        1,     6714, 0x70f29b1d, S=1,     1024, 0xec907a9e
+0,        420,        420,        1,     6822, 0x6f02e7ec, S=1,     1024, 0xec907a9e
+0,        430,        430,        1,     6880, 0x95440d97, S=1,     1024, 0xec907a9e
+0,        440,        440,        1,     6983, 0xab7a43fb, S=1,     1024, 0xec907a9e
+0,        450,        450,        1,     7085, 0xc3427580, S=1,     1024, 0xec907a9e
+0,        460,        460,        1,     7242, 0x3d00c5f0, S=1,     1024, 0xec907a9e
+0,        470,        470,        1,     7349, 0x453fad59, S=1,     1024, 0xec907a9e
+0,        480,        480,        1,     7513, 0x96d12666, S=1,     1024, 0xec907a9e
+0,        490,        490,        1,     7575, 0xf8cb3c77, S=1,     1024, 0xec907a9e
+0,        500,        500,        1,     7759, 0x1fe6ab40, S=1,     1024, 0xec907a9e
+0,        510,        510,        1,     7846, 0x3853fd0f, S=1,     1024, 0xec907a9e
+0,        520,        520,        1,     7975, 0x88d02c47, S=1,     1024, 0xec907a9e
+0,        530,        530,        1,     8092, 0xe28a3368, S=1,     1024, 0xec907a9e
+0,        540,        540,        1,     8188, 0x159a5d3b, S=1,     1024, 0xec907a9e
+0,        550,        550,        1,     8307, 0x598cae50, S=1,     1024, 0xec907a9e
+0,        560,        560,        1,     8436, 0xd104ee50, S=1,     1024, 0xec907a9e
+0,        570,        570,        1,     8498, 0x0b1527ed, S=1,     1024, 0xec907a9e
+0,        580,        580,        1,     8559, 0x0f722288, S=1,     1024, 0xec907a9e
+0,        590,        590,        1,     8661, 0x3efb2500, S=1,     1024, 0xec907a9e
+0,        600,        600,        1,     8774, 0x1e188a8b, S=1,     1024, 0xec907a9e
+0,        610,        610,        1,     8848, 0x5b438dbb, S=1,     1024, 0xec907a9e
+0,        620,        620,        1,     8968, 0x4b47ecb3, S=1,     1024, 0xec907a9e
+0,        630,        630,        1,     9036, 0x7a8e0791, S=1,     1024, 0xec907a9e
+0,        640,        640,        1,     9114, 0x7379131a, S=1,     1024, 0xec907a9e
+0,        650,        650,        1,     9258, 0x06099246, S=1,     1024, 0xec907a9e
+0,        660,        660,        1,     9423, 0x721ac9ab, S=1,     1024, 0xec907a9e
+0,        670,        670,        1,     9529, 0xbb1c0d18, S=1,     1024, 0xec907a9e
+0,        680,        680,        1,     9608, 0xc29a05f0, S=1,     1024, 0xec907a9e
+0,        690,        690,        1,     9668, 0xf35221ff, S=1,     1024, 0xec907a9e
+0,        700,        700,        1,     9709, 0x8c6d33f0, S=1,     1024, 0xec907a9e
+0,        710,        710,        1,     9810, 0x2d5b59f4, S=1,     1024, 0xec907a9e
+0,        720,        720,        1,     9879, 0x110ea6e5, S=1,     1024, 0xec907a9e
+0,        730,        730,        1,    10038, 0x5945f5f4, S=1,     1024, 0xec907a9e
+0,        740,        740,        1,    10120, 0xab24f519, S=1,     1024, 0xec907a9e
+0,        750,        750,        1,    10218, 0x090d305a, S=1,     1024, 0xec907a9e
+0,        760,        760,        1,    10231, 0xa77e6a66, S=1,     1024, 0xec907a9e
+0,        770,        770,        1,    10313, 0x192254b7, S=1,     1024, 0xec907a9e
+0,        780,        780,        1,    10444, 0x36609934, S=1,     1024, 0xec907a9e
+0,        790,        790,        1,    10670, 0xfa971206, S=1,     1024, 0xec907a9e
+0,        800,        800,        1,    10902, 0x01b59fd5, S=1,     1024, 0xec907a9e
+0,        810,        810,        1,    10992, 0x1f2ab0bc, S=1,     1024, 0xec907a9e
+0,        820,        820,        1,    11044, 0x7f5b1dc2, S=1,     1024, 0xec907a9e
+0,        830,        830,        1,    11170, 0x0beaee49, S=1,     1024, 0xec907a9e
+0,        840,        840,        1,    11267, 0xb1ae3808, S=1,     1024, 0xec907a9e
+0,        850,        850,        1,    11427, 0x2cc9aa3f, S=1,     1024, 0xec907a9e
+0,        860,        860,        1,    11529, 0x654de4f3, S=1,     1024, 0xec907a9e
+0,        870,        870,        1,    11632, 0xf4972bdf, S=1,     1024, 0xec907a9e
+0,        880,        880,        1,    11825, 0x0cd2548c, S=1,     1024, 0xec907a9e
+0,        890,        890,        1,    11864, 0x80758637, S=1,     1024, 0xec907a9e
+0,        900,        900,        1,    11949, 0x87c86fb9, S=1,     1024, 0xec907a9e
+0,        910,        910,        1,    12088, 0x1b99c82b, S=1,     1024, 0xec907a9e
+0,        920,        920,        1,    12160, 0x72fbf4ac, S=1,     1024, 0xec907a9e
+0,        930,        930,        1,    12305, 0x9f62069a, S=1,     1024, 0xec907a9e
+0,        940,        940,        1,    12295, 0x0625d96b, S=1,     1024, 0xec907a9e
+0,        950,        950,        1,    12496, 0xe67cb2cf, S=1,     1024, 0xec907a9e
+0,        960,        960,        1,    12660, 0x4d1d05ea, S=1,     1024, 0xec907a9e
+0,        970,        970,        1,    12763, 0x12542531, S=1,     1024, 0xec907a9e
+0,        980,        980,        1,    12916, 0x37c444db, S=1,     1024, 0xec907a9e
+0,        990,        990,        1,    13021, 0xa1c89f22, S=1,     1024, 0xec907a9e
+0,       1000,       1000,        1,    13184, 0xe13ad0be, S=1,     1024, 0xec907a9e
+0,       1010,       1010,        1,    13315, 0x11b93a5d, S=1,     1024, 0xec907a9e
+0,       1020,       1020,        1,    13336, 0x128254ee, S=1,     1024, 0xec907a9e
+0,       1030,       1030,        1,    13536, 0x48b7b5de, S=1,     1024, 0xec907a9e
+0,       1040,       1040,        1,    13728, 0x03f4d302, S=1,     1024, 0xec907a9e
+0,       1050,       1050,        1,    13821, 0xfe4e97eb, S=1,     1024, 0xec907a9e
+0,       1060,       1060,        1,    14002, 0x0de77dfc, S=1,     1024, 0xec907a9e
+0,       1070,       1070,        1,    14156, 0xf1b1d4e1, S=1,     1024, 0xec907a9e
+0,       1080,       1080,        1,    14256, 0x355320cc, S=1,     1024, 0xec907a9e
+0,       1090,       1090,        1,    14451, 0x306cb131, S=1,     1024, 0xec907a9e
+0,       1100,       1100,        1,    14508, 0xca729c8e, S=1,     1024, 0xec907a9e
+0,       1110,       1110,        1,    14683, 0x7abec197, S=1,     1024, 0xec907a9e
+0,       1120,       1120,        1,    14852, 0xa5b90496, S=1,     1024, 0xec907a9e
+0,       1130,       1130,        1,    15090, 0xcd29839d, S=1,     1024, 0xec907a9e
+0,       1140,       1140,        1,    15225, 0xfd85f899, S=1,     1024, 0xec907a9e
+0,       1150,       1150,        1,    15340, 0x07411310, S=1,     1024, 0xec907a9e
+0,       1160,       1160,        1,    15473, 0xe90086c1, S=1,     1024, 0xec907a9e
+0,       1170,       1170,        1,    15583, 0x524da1f3, S=1,     1024, 0xec907a9e
+0,       1180,       1180,        1,    15683, 0x404cbd62, S=1,     1024, 0xec907a9e
+0,       1190,       1190,        1,    15779, 0x3ec01717, S=1,     1024, 0xec907a9e
+0,       1200,       1200,        1,    15934, 0xac8b4c03, S=1,     1024, 0xec907a9e
+0,       1210,       1210,        1,    16116, 0xb4fa83c9, S=1,     1024, 0xec907a9e
+0,       1220,       1220,        1,    16153, 0x18f669c1, S=1,     1024, 0xec907a9e
+0,       1230,       1230,        1,    16303, 0x765b10e4, S=1,     1024, 0xec907a9e
+0,       1240,       1240,        1,    16460, 0xc4a8149a, S=1,     1024, 0xec907a9e
+0,       1250,       1250,        1,    16475, 0x48664e2d, S=1,     1024, 0xec907a9e
+0,       1260,       1260,        1,    16710, 0x8a599742, S=1,     1024, 0xec907a9e
+0,       1270,       1270,        1,    16848, 0x8634a3d5, S=1,     1024, 0xec907a9e
+0,       1280,       1280,        1,    16955, 0x01f1ebda, S=1,     1024, 0xec907a9e
+0,       1290,       1290,        1,    17044, 0xd57b47df, S=1,     1024, 0xec907a9e
+0,       1300,       1300,        1,    17153, 0xc5cf7bc7, S=1,     1024, 0xec907a9e
+0,       1310,       1310,        1,    17375, 0xbbeea767, S=1,     1024, 0xec907a9e
+0,       1320,       1320,        1,    17476, 0xd7a25686, S=1,     1024, 0xec907a9e
+0,       1330,       1330,        1,    17602, 0x1c99b228, S=1,     1024, 0xec907a9e
+0,       1340,       1340,        1,    17676, 0xd860d1d2, S=1,     1024, 0xec907a9e
+0,       1350,       1350,        1,    17799, 0xe4ccb3ae, S=1,     1024, 0xec907a9e
+0,       1360,       1360,        1,    17930, 0xf322e416, S=1,     1024, 0xec907a9e
+0,       1370,       1370,        1,    18201, 0xc6f1cfa7, S=1,     1024, 0xec907a9e
+0,       1380,       1380,        1,    18409, 0x7cdfe609, S=1,     1024, 0xec907a9e
+0,       1390,       1390,        1,    18686, 0xf65e5519, S=1,     1024, 0xec907a9e
+0,       1400,       1400,        1,    18790, 0x6d5c8a90, S=1,     1024, 0xec907a9e
+0,       1410,       1410,        1,    18891, 0x4702a6cf, S=1,     1024, 0xec907a9e
+0,       1420,       1420,        1,    18995, 0x69e5e02d, S=1,     1024, 0xec907a9e
+0,       1430,       1430,        1,    19058, 0xc56f0272, S=1,     1024, 0xec907a9e
+0,       1440,       1440,        1,    19223, 0xe89733f6, S=1,     1024, 0xec907a9e
+0,       1450,       1450,        1,    19501, 0xdb80f9e9, S=1,     1024, 0xec907a9e
+0,       1460,       1460,        1,    19566, 0xa3952da8, S=1,     1024, 0xec907a9e
+0,       1470,       1470,        1,    19692, 0x65925150, S=1,     1024, 0xec907a9e
+0,       1480,       1480,        1,    19730, 0x1352599a, S=1,     1024, 0xec907a9e
+0,       1490,       1490,        1,    19916, 0xd246bf9b, S=1,     1024, 0xec907a9e
+0,       1500,       1500,        1,    20136, 0x1c462eea, S=1,     1024, 0xec907a9e
+0,       1510,       1510,        1,    20210, 0xdec93f63, S=1,     1024, 0xec907a9e
+0,       1520,       1520,        1,    20311, 0xdff7836e, S=1,     1024, 0xec907a9e
+0,       1530,       1530,        1,    20377, 0x753d813f, S=1,     1024, 0xec907a9e
+0,       1540,       1540,        1,    20478, 0x7592b1a8, S=1,     1024, 0xec907a9e
+0,       1550,       1550,        1,    20597, 0x58313994, S=1,     1024, 0xec907a9e
+0,       1560,       1560,        1,    20717, 0xcdd9c665, S=1,     1024, 0xec907a9e
+0,       1570,       1570,        1,    20816, 0x803b4b03, S=1,     1024, 0xec907a9e
+0,       1580,       1580,        1,    20912, 0xce36ce22, S=1,     1024, 0xec907a9e
+0,       1590,       1590,        1,    20975, 0x1d08e543, S=1,     1024, 0xec907a9e
+0,       1600,       1600,        1,    21130, 0xc966e24c, S=1,     1024, 0xec907a9e
+0,       1610,       1610,        1,    21343, 0x735e614e, S=1,     1024, 0xec907a9e
+0,       1620,       1620,        1,    21455, 0x78d7afd1, S=1,     1024, 0xec907a9e
+0,       1630,       1630,        1,    21533, 0x62a10f46, S=1,     1024, 0xec907a9e
+0,       1640,       1640,        1,    21645, 0x8e53a12f, S=1,     1024, 0xec907a9e
+0,       1650,       1650,        1,    21931, 0x3b2f7226, S=1,     1024, 0xec907a9e
+0,       1660,       1660,        1,    22009, 0xf06bc58a, S=1,     1024, 0xec907a9e
+0,       1670,       1670,        1,    22115, 0xeeb1c5d7, S=1,     1024, 0xec907a9e
+0,       1680,       1680,        1,    22211, 0xcbab1084, S=1,     1024, 0xec907a9e
+0,       1690,       1690,        1,    22380, 0xf07d5cb7, S=1,     1024, 0xec907a9e
+0,       1700,       1700,        1,    22443, 0xcb79441c, S=1,     1024, 0xec907a9e
+0,       1710,       1710,        1,    22588, 0xcad49814, S=1,     1024, 0xec907a9e
+0,       1720,       1720,        1,    22706, 0x34924e43, S=1,     1024, 0xec907a9e
diff --git a/tests/ref/fate/gifenc-rgb4_byte b/tests/ref/fate/gifenc-rgb4_byte
new file mode 100644
index 0000000..8a0595a
--- /dev/null
+++ b/tests/ref/fate/gifenc-rgb4_byte
@@ -0,0 +1,174 @@
+#tb 0: 1/100
+0,          0,          0,        1,      507, 0xde1f113a
+0,         10,         10,        1,      213, 0x23c24d3d, S=1,     1024, 0xf7700427
+0,         20,         20,        1,      130, 0x2c222a38, S=1,     1024, 0x03730427
+0,         30,         30,        1,      384, 0xb1d8a4bd, S=1,     1024, 0xf7700427
+0,         40,         40,        1,      381, 0x37a3a2c9, S=1,     1024, 0xf3740427
+0,         50,         50,        1,      430, 0x162bb3d3, S=1,     1024, 0xf3740427
+0,         60,         60,        1,      517, 0x411ad737, S=1,     1024, 0xf3740427
+0,         70,         70,        1,      535, 0x12cde6b7, S=1,     1024, 0xf3740427
+0,         80,         80,        1,      437, 0xec54b945, S=1,     1024, 0x0b6b0427
+0,         90,         90,        1,      923, 0xd2e2a35f, S=1,     1024, 0x0b6b0427
+0,        100,        100,        1,      693, 0x97064a1e, S=1,     1024, 0x0b6b0427
+0,        110,        110,        1,     1194, 0xa6152c8a, S=1,     1024, 0x0b6b0427
+0,        120,        120,        1,     1291, 0x94d25581, S=1,     1024, 0x0b6b0427
+0,        130,        130,        1,     1245, 0x5b483525, S=1,     1024, 0x0b6b0427
+0,        140,        140,        1,     1330, 0xfb5351c8, S=1,     1024, 0x0b6b0427
+0,        150,        150,        1,     1275, 0x353c3913, S=1,     1024, 0x0b6b0427
+0,        160,        160,        1,     1474, 0x27399754, S=1,     1024, 0x0b6b0427
+0,        170,        170,        1,     1783, 0x9e024aa6, S=1,     1024, 0xecb30526
+0,        180,        180,        1,     1675, 0x219dfaf8, S=1,     1024, 0xecb30526
+0,        190,        190,        1,     1509, 0xd7f5abbe, S=1,     1024, 0xecb30526
+0,        200,        200,        1,     1705, 0x44a01729, S=1,     1024, 0xecb30526
+0,        210,        210,        1,     1745, 0x31ff1f89, S=1,     1024, 0xecb30526
+0,        220,        220,        1,     1642, 0x55420147, S=1,     1024, 0xecb30526
+0,        230,        230,        1,     1717, 0x4b8d1cb7, S=1,     1024, 0xecb30526
+0,        240,        240,        1,     1900, 0xd7737a09, S=1,     1024, 0xecb30526
+0,        250,        250,        1,     1806, 0xfe1a513f, S=1,     1024, 0xecb30526
+0,        260,        260,        1,     1915, 0x976d80e6, S=1,     1024, 0xecb30526
+0,        270,        270,        1,     2100, 0x0ae6d1ce, S=1,     1024, 0xecb30526
+0,        280,        280,        1,     2700, 0x7a89f104, S=1,     1024, 0xecb30526
+0,        290,        290,        1,     2673, 0xf6b6a71d, S=1,     1024, 0xecb30526
+0,        300,        300,        1,     2894, 0x47eb484a, S=1,     1024, 0xecb30526
+0,        310,        310,        1,     3257, 0x0b0cd125, S=1,     1024, 0xecb30526
+0,        320,        320,        1,     3179, 0x3ee2c161, S=1,     1024, 0xecb30526
+0,        330,        330,        1,     3296, 0x6230e506, S=1,     1024, 0xecb30526
+0,        340,        340,        1,     3599, 0x8c2d75d6, S=1,     1024, 0xecb30526
+0,        350,        350,        1,     3698, 0x5a59a042, S=1,     1024, 0xecb30526
+0,        360,        360,        1,     3814, 0x96a8d57e, S=1,     1024, 0xecb30526
+0,        370,        370,        1,     3626, 0xb3e67f8e, S=1,     1024, 0xecb30526
+0,        380,        380,        1,     2950, 0x50806197, S=1,     1024, 0xecb30526
+0,        390,        390,        1,     3086, 0x72068d4c, S=1,     1024, 0xecb30526
+0,        400,        400,        1,     3093, 0xa248861e, S=1,     1024, 0xecb30526
+0,        410,        410,        1,     3456, 0x6d232a96, S=1,     1024, 0xecb30526
+0,        420,        420,        1,     4107, 0xe70d5eba, S=1,     1024, 0xecb30526
+0,        430,        430,        1,     4216, 0xab3258f3, S=1,     1024, 0xecb30526
+0,        440,        440,        1,     3613, 0x667f4ff8, S=1,     1024, 0xecb30526
+0,        450,        450,        1,     3909, 0xa7b0e73d, S=1,     1024, 0xecb30526
+0,        460,        460,        1,     4460, 0x7c8ae0be, S=1,     1024, 0xecb30526
+0,        470,        470,        1,     4592, 0x58112f48, S=1,     1024, 0xecb30526
+0,        480,        480,        1,     4821, 0x678d9b72, S=1,     1024, 0xecb30526
+0,        490,        490,        1,     5397, 0x79eabff3, S=1,     1024, 0xecb30526
+0,        500,        500,        1,     5266, 0xd5ab9630, S=1,     1024, 0xecb30526
+0,        510,        510,        1,     5415, 0x76dce16e, S=1,     1024, 0xecb30526
+0,        520,        520,        1,     5519, 0x30ed05d8, S=1,     1024, 0xecb30526
+0,        530,        530,        1,     5701, 0x5bae5af7, S=1,     1024, 0xecb30526
+0,        540,        540,        1,     6160, 0x98364177, S=1,     1024, 0xecb30526
+0,        550,        550,        1,     6233, 0x52a05075, S=1,     1024, 0xecb30526
+0,        560,        560,        1,     5911, 0x04bfc46a, S=1,     1024, 0xecb30526
+0,        570,        570,        1,     5997, 0xf1e6f586, S=1,     1024, 0xecb30526
+0,        580,        580,        1,     5946, 0xe6f3f055, S=1,     1024, 0xecb30526
+0,        590,        590,        1,     6468, 0xc8a3cf61, S=1,     1024, 0xecb30526
+0,        600,        600,        1,     6737, 0xc27b3b79, S=1,     1024, 0xecb30526
+0,        610,        610,        1,     6275, 0x84d88e2b, S=1,     1024, 0xecb30526
+0,        620,        620,        1,     6641, 0xb44b3534, S=1,     1024, 0xecb30526
+0,        630,        630,        1,     6378, 0x3965888b, S=1,     1024, 0xecb30526
+0,        640,        640,        1,     6257, 0x12115750, S=1,     1024, 0xecb30526
+0,        650,        650,        1,     6908, 0x57137217, S=1,     1024, 0xecb30526
+0,        660,        660,        1,     7230, 0xbacc24ee, S=1,     1024, 0xecb30526
+0,        670,        670,        1,     7556, 0x1aa2a694, S=1,     1024, 0xecb30526
+0,        680,        680,        1,     7413, 0xbc9e7718, S=1,     1024, 0xecb30526
+0,        690,        690,        1,     7476, 0xb2a1aba0, S=1,     1024, 0xecb30526
+0,        700,        700,        1,     7595, 0x4ce5e56c, S=1,     1024, 0xecb30526
+0,        710,        710,        1,     7756, 0x8f2504f8, S=1,     1024, 0xecb30526
+0,        720,        720,        1,     8015, 0xd4146c80, S=1,     1024, 0xecb30526
+0,        730,        730,        1,     8128, 0x11b2bf4c, S=1,     1024, 0xecb30526
+0,        740,        740,        1,     8101, 0xc627adbe, S=1,     1024, 0xecb30526
+0,        750,        750,        1,     7863, 0xe99f3f3b, S=1,     1024, 0xecb30526
+0,        760,        760,        1,     7960, 0x4bc091b8, S=1,     1024, 0xecb30526
+0,        770,        770,        1,     8238, 0x1086ea8a, S=1,     1024, 0xecb30526
+0,        780,        780,        1,     8321, 0x3a404791, S=1,     1024, 0xecb30526
+0,        790,        790,        1,     8562, 0xcbdcc01e, S=1,     1024, 0xecb30526
+0,        800,        800,        1,     8746, 0xec190b22, S=1,     1024, 0xecb30526
+0,        810,        810,        1,     8578, 0x12e7a4e8, S=1,     1024, 0xecb30526
+0,        820,        820,        1,     8878, 0x51c05771, S=1,     1024, 0xecb30526
+0,        830,        830,        1,     9077, 0xe12b589b, S=1,     1024, 0xecb30526
+0,        840,        840,        1,     9310, 0xde3bf881, S=1,     1024, 0xecb30526
+0,        850,        850,        1,     9394, 0x1eba46cc, S=1,     1024, 0xecb30526
+0,        860,        860,        1,     9161, 0x7c359911, S=1,     1024, 0xecb30526
+0,        870,        870,        1,     9462, 0xccda3664, S=1,     1024, 0xecb30526
+0,        880,        880,        1,     9650, 0x6e6292fc, S=1,     1024, 0xecb30526
+0,        890,        890,        1,     9701, 0x08909b95, S=1,     1024, 0xecb30526
+0,        900,        900,        1,     9523, 0xe61b38bb, S=1,     1024, 0xecb30526
+0,        910,        910,        1,     9891, 0x96b90b98, S=1,     1024, 0xecb30526
+0,        920,        920,        1,    10005, 0x2db84c80, S=1,     1024, 0xecb30526
+0,        930,        930,        1,    10038, 0x37e52a72, S=1,     1024, 0xecb30526
+0,        940,        940,        1,    10086, 0x135a43e4, S=1,     1024, 0xecb30526
+0,        950,        950,        1,    10438, 0x472c0372, S=1,     1024, 0xecb30526
+0,        960,        960,        1,    10583, 0xcf4c5862, S=1,     1024, 0xecb30526
+0,        970,        970,        1,    10581, 0xce658137, S=1,     1024, 0xecb30526
+0,        980,        980,        1,    10807, 0x3954dad9, S=1,     1024, 0xecb30526
+0,        990,        990,        1,    11111, 0x5f8d504f, S=1,     1024, 0xecb30526
+0,       1000,       1000,        1,    11194, 0x3c7e6a77, S=1,     1024, 0xecb30526
+0,       1010,       1010,        1,    11240, 0x5112a0a3, S=1,     1024, 0xecb30526
+0,       1020,       1020,        1,    11482, 0xb938f4f9, S=1,     1024, 0xecb30526
+0,       1030,       1030,        1,    11680, 0x44a25971, S=1,     1024, 0xecb30526
+0,       1040,       1040,        1,    11785, 0x7350b5db, S=1,     1024, 0xecb30526
+0,       1050,       1050,        1,    11436, 0xe3170ad5, S=1,     1024, 0xecb30526
+0,       1060,       1060,        1,    11927, 0x4ab8c884, S=1,     1024, 0xecb30526
+0,       1070,       1070,        1,    11932, 0xecb5bdf7, S=1,     1024, 0xecb30526
+0,       1080,       1080,        1,    12280, 0xa0ea76d4, S=1,     1024, 0xecb30526
+0,       1090,       1090,        1,    12334, 0x16147fc3, S=1,     1024, 0xecb30526
+0,       1100,       1100,        1,    12452, 0x61a8b3d7, S=1,     1024, 0xecb30526
+0,       1110,       1110,        1,    12695, 0x8b703e74, S=1,     1024, 0xecb30526
+0,       1120,       1120,        1,    12667, 0xc75b5175, S=1,     1024, 0xecb30526
+0,       1130,       1130,        1,    12957, 0x3b839f0d, S=1,     1024, 0xecb30526
+0,       1140,       1140,        1,    13053, 0xd3c9e3da, S=1,     1024, 0xecb30526
+0,       1150,       1150,        1,    13147, 0xdf5c2e68, S=1,     1024, 0xecb30526
+0,       1160,       1160,        1,    13171, 0x15961ca2, S=1,     1024, 0xecb30526
+0,       1170,       1170,        1,    13197, 0xa5eb5717, S=1,     1024, 0xecb30526
+0,       1180,       1180,        1,    13211, 0x1a625e31, S=1,     1024, 0xecb30526
+0,       1190,       1190,        1,    13210, 0x246661c9, S=1,     1024, 0xecb30526
+0,       1200,       1200,        1,    13467, 0xfcaaa461, S=1,     1024, 0xecb30526
+0,       1210,       1210,        1,    13665, 0x8100dbf2, S=1,     1024, 0xecb30526
+0,       1220,       1220,        1,    13692, 0xddd1eab9, S=1,     1024, 0xecb30526
+0,       1230,       1230,        1,    13821, 0xc70e2af0, S=1,     1024, 0xecb30526
+0,       1240,       1240,        1,    13946, 0xe15d9134, S=1,     1024, 0xecb30526
+0,       1250,       1250,        1,    14063, 0xf652d232, S=1,     1024, 0xecb30526
+0,       1260,       1260,        1,    14124, 0x756ccc81, S=1,     1024, 0xecb30526
+0,       1270,       1270,        1,    14331, 0x56d64fe8, S=1,     1024, 0xecb30526
+0,       1280,       1280,        1,    14469, 0x4c3faa7f, S=1,     1024, 0xecb30526
+0,       1290,       1290,        1,    14536, 0xad02a19b, S=1,     1024, 0xecb30526
+0,       1300,       1300,        1,    14608, 0x0971d168, S=1,     1024, 0xecb30526
+0,       1310,       1310,        1,    14898, 0x1a6827b3, S=1,     1024, 0xecb30526
+0,       1320,       1320,        1,    14978, 0xf9709fef, S=1,     1024, 0xecb30526
+0,       1330,       1330,        1,    15142, 0x3598da63, S=1,     1024, 0xecb30526
+0,       1340,       1340,        1,    15129, 0x062fb976, S=1,     1024, 0xecb30526
+0,       1350,       1350,        1,    15243, 0x0a6a12f9, S=1,     1024, 0xecb30526
+0,       1360,       1360,        1,    15337, 0x0f9a65d6, S=1,     1024, 0xecb30526
+0,       1370,       1370,        1,    15638, 0xf7bc9ef5, S=1,     1024, 0xecb30526
+0,       1380,       1380,        1,    15912, 0x2d5b26bb, S=1,     1024, 0xecb30526
+0,       1390,       1390,        1,    16041, 0xbfaf4857, S=1,     1024, 0xecb30526
+0,       1400,       1400,        1,    16228, 0xdac701f0, S=1,     1024, 0xecb30526
+0,       1410,       1410,        1,    16262, 0xcd0ae5e4, S=1,     1024, 0xecb30526
+0,       1420,       1420,        1,    16371, 0x9d4f0e73, S=1,     1024, 0xecb30526
+0,       1430,       1430,        1,    16661, 0xd37ba990, S=1,     1024, 0xecb30526
+0,       1440,       1440,        1,    16917, 0xd5b01774, S=1,     1024, 0xecb30526
+0,       1450,       1450,        1,    17149, 0x435ecdd4, S=1,     1024, 0xecb30526
+0,       1460,       1460,        1,    17172, 0x045fb234, S=1,     1024, 0xecb30526
+0,       1470,       1470,        1,    17315, 0xc5ddadab, S=1,     1024, 0xecb30526
+0,       1480,       1480,        1,    17396, 0xe8ef15b5, S=1,     1024, 0xecb30526
+0,       1490,       1490,        1,    17430, 0x6f58f8bf, S=1,     1024, 0xecb30526
+0,       1500,       1500,        1,    17576, 0x5c2a5445, S=1,     1024, 0xecb30526
+0,       1510,       1510,        1,    17764, 0x609f8c3b, S=1,     1024, 0xecb30526
+0,       1520,       1520,        1,    17826, 0x538c8532, S=1,     1024, 0xecb30526
+0,       1530,       1530,        1,    17918, 0x84fc9a95, S=1,     1024, 0xecb30526
+0,       1540,       1540,        1,    17823, 0x788fbada, S=1,     1024, 0xecb30526
+0,       1550,       1550,        1,    18142, 0x56881e47, S=1,     1024, 0xecb30526
+0,       1560,       1560,        1,    18257, 0xa35b86cf, S=1,     1024, 0xecb30526
+0,       1570,       1570,        1,    18337, 0x82ddbc21, S=1,     1024, 0xecb30526
+0,       1580,       1580,        1,    18293, 0xf0d838d6, S=1,     1024, 0xecb30526
+0,       1590,       1590,        1,    18418, 0x7ed8bba6, S=1,     1024, 0xecb30526
+0,       1600,       1600,        1,    18607, 0xccea47f6, S=1,     1024, 0xecb30526
+0,       1610,       1610,        1,    18916, 0x880ebd63, S=1,     1024, 0xecb30526
+0,       1620,       1620,        1,    19073, 0x055f02e3, S=1,     1024, 0xecb30526
+0,       1630,       1630,        1,    19168, 0xcc2c02d7, S=1,     1024, 0xecb30526
+0,       1640,       1640,        1,    19210, 0xa538ffc1, S=1,     1024, 0xecb30526
+0,       1650,       1650,        1,    19398, 0x4777644d, S=1,     1024, 0xecb30526
+0,       1660,       1660,        1,    19480, 0xcb2aa0fa, S=1,     1024, 0xecb30526
+0,       1670,       1670,        1,    19659, 0xe3c1122d, S=1,     1024, 0xecb30526
+0,       1680,       1680,        1,    19672, 0x1d1e193f, S=1,     1024, 0xecb30526
+0,       1690,       1690,        1,    19936, 0xcd036346, S=1,     1024, 0xecb30526
+0,       1700,       1700,        1,    19975, 0x96529b21, S=1,     1024, 0xecb30526
+0,       1710,       1710,        1,    20021, 0xcdaf8bb5, S=1,     1024, 0xecb30526
+0,       1720,       1720,        1,    20060, 0x1cea7784, S=1,     1024, 0xecb30526
diff --git a/tests/ref/fate/gifenc-rgb8 b/tests/ref/fate/gifenc-rgb8
new file mode 100644
index 0000000..63789b7
--- /dev/null
+++ b/tests/ref/fate/gifenc-rgb8
@@ -0,0 +1,174 @@
+#tb 0: 1/100
+0,          0,          0,        1,      552, 0x47602c6c
+0,         10,         10,        1,      297, 0x49dd8847, S=1,     1024, 0xcfc8799f
+0,         20,         20,        1,      437, 0x736bd351, S=1,     1024, 0xcfc8799f
+0,         30,         30,        1,      450, 0x2254d187, S=1,     1024, 0xcfc8799f
+0,         40,         40,        1,      547, 0xe16104bc, S=1,     1024, 0xcfc8799f
+0,         50,         50,        1,      613, 0xef4c2026, S=1,     1024, 0xcfc8799f
+0,         60,         60,        1,      642, 0xa0af1edf, S=1,     1024, 0xcfc8799f
+0,         70,         70,        1,      660, 0xd0763931, S=1,     1024, 0xcfc8799f
+0,         80,         80,        1,      821, 0xc38f7fac, S=1,     1024, 0xcfc8799f
+0,         90,         90,        1,     1157, 0x4c112ecd, S=1,     1024, 0xcfc8799f
+0,        100,        100,        1,      179, 0x0690541c, S=1,     1024, 0xcfc8799f
+0,        110,        110,        1,     1333, 0x216f70a7, S=1,     1024, 0xcfc8799f
+0,        120,        120,        1,     1638, 0x901c093d, S=1,     1024, 0xcfc8799f
+0,        130,        130,        1,     1531, 0xc9bae5ff, S=1,     1024, 0xcfc8799f
+0,        140,        140,        1,     1720, 0xce854743, S=1,     1024, 0xcfc8799f
+0,        150,        150,        1,     1910, 0x2690866d, S=1,     1024, 0xcfc8799f
+0,        160,        160,        1,     2124, 0xa586dad0, S=1,     1024, 0xcfc8799f
+0,        170,        170,        1,     2247, 0x72982a87, S=1,     1024, 0xcfc8799f
+0,        180,        180,        1,     2311, 0xd64235af, S=1,     1024, 0xcfc8799f
+0,        190,        190,        1,     2408, 0xe2a66cc9, S=1,     1024, 0xcfc8799f
+0,        200,        200,        1,     2601, 0xeab6c267, S=1,     1024, 0xcfc8799f
+0,        210,        210,        1,     2686, 0xfa990310, S=1,     1024, 0xcfc8799f
+0,        220,        220,        1,     2784, 0xca600dee, S=1,     1024, 0xcfc8799f
+0,        230,        230,        1,     2884, 0xc7134b99, S=1,     1024, 0xcfc8799f
+0,        240,        240,        1,     2981, 0x92507824, S=1,     1024, 0xcfc8799f
+0,        250,        250,        1,     3101, 0x3e029e0e, S=1,     1024, 0xcfc8799f
+0,        260,        260,        1,     3252, 0x8d3af677, S=1,     1024, 0xcfc8799f
+0,        270,        270,        1,     3329, 0x29a81b71, S=1,     1024, 0xcfc8799f
+0,        280,        280,        1,     3571, 0x18a68a51, S=1,     1024, 0xcfc8799f
+0,        290,        290,        1,     3806, 0x192dfed1, S=1,     1024, 0xcfc8799f
+0,        300,        300,        1,     2749, 0xdf1e1f9d, S=1,     1024, 0xcfc8799f
+0,        310,        310,        1,     4031, 0x6d4f7329, S=1,     1024, 0xcfc8799f
+0,        320,        320,        1,     3025, 0xb43c9e94, S=1,     1024, 0xcfc8799f
+0,        330,        330,        1,     4294, 0xb64a0a7f, S=1,     1024, 0xcfc8799f
+0,        340,        340,        1,     2044, 0x0440c072, S=1,     1024, 0xcfc8799f
+0,        350,        350,        1,     3212, 0xe91af08f, S=1,     1024, 0xcfc8799f
+0,        360,        360,        1,     2280, 0x1ec34aa0, S=1,     1024, 0xcfc8799f
+0,        370,        370,        1,     3632, 0x11af9aa2, S=1,     1024, 0xcfc8799f
+0,        380,        380,        1,     3552, 0xed2c75b2, S=1,     1024, 0xcfc8799f
+0,        390,        390,        1,     3689, 0x42a6dd0c, S=1,     1024, 0xcfc8799f
+0,        400,        400,        1,     1558, 0x2c14e4b2, S=1,     1024, 0xcfc8799f
+0,        410,        410,        1,      939, 0x7ae8cd8f, S=1,     1024, 0xcfc8799f
+0,        420,        420,        1,      273, 0x138c7831, S=1,     1024, 0xcfc8799f
+0,        430,        430,        1,      929, 0x42eeae3e, S=1,     1024, 0xcfc8799f
+0,        440,        440,        1,      271, 0x6d338044, S=1,     1024, 0xcfc8799f
+0,        450,        450,        1,      196, 0xa5de5322, S=1,     1024, 0xcfc8799f
+0,        460,        460,        1,     4299, 0x5bac0d86, S=1,     1024, 0xcfc8799f
+0,        470,        470,        1,     4894, 0x8a7d39a5, S=1,     1024, 0xcfc8799f
+0,        480,        480,        1,     4927, 0xdd6113e7, S=1,     1024, 0xcfc8799f
+0,        490,        490,        1,     4941, 0x71915520, S=1,     1024, 0xcfc8799f
+0,        500,        500,        1,     4153, 0x0f8cb8a5, S=1,     1024, 0xcfc8799f
+0,        510,        510,        1,     4677, 0x62cfc338, S=1,     1024, 0xcfc8799f
+0,        520,        520,        1,     4740, 0x4418bb44, S=1,     1024, 0xcfc8799f
+0,        530,        530,        1,     4981, 0xb93c5976, S=1,     1024, 0xcfc8799f
+0,        540,        540,        1,     5179, 0x97aac3a1, S=1,     1024, 0xcfc8799f
+0,        550,        550,        1,     5046, 0x836a80cd, S=1,     1024, 0xcfc8799f
+0,        560,        560,        1,     5140, 0xa725c1e7, S=1,     1024, 0xcfc8799f
+0,        570,        570,        1,     4288, 0x7eb6fbbf, S=1,     1024, 0xcfc8799f
+0,        580,        580,        1,     5079, 0xb2e7a2de, S=1,     1024, 0xcfc8799f
+0,        590,        590,        1,     5284, 0xb757dfe1, S=1,     1024, 0xcfc8799f
+0,        600,        600,        1,     5426, 0xf9f11e57, S=1,     1024, 0xcfc8799f
+0,        610,        610,        1,     4644, 0x66f889e0, S=1,     1024, 0xcfc8799f
+0,        620,        620,        1,     5263, 0x8617d7e9, S=1,     1024, 0xcfc8799f
+0,        630,        630,        1,     5221, 0x26e3ca43, S=1,     1024, 0xcfc8799f
+0,        640,        640,        1,     5216, 0xf3399cfa, S=1,     1024, 0xcfc8799f
+0,        650,        650,        1,     5394, 0xe0c801ca, S=1,     1024, 0xcfc8799f
+0,        660,        660,        1,     5219, 0xff22e354, S=1,     1024, 0xcfc8799f
+0,        670,        670,        1,     5704, 0xcfbcd55e, S=1,     1024, 0xcfc8799f
+0,        680,        680,        1,     5636, 0x7fc2a1e5, S=1,     1024, 0xcfc8799f
+0,        690,        690,        1,     5818, 0x6090ebbd, S=1,     1024, 0xcfc8799f
+0,        700,        700,        1,     5763, 0xc110c791, S=1,     1024, 0xcfc8799f
+0,        710,        710,        1,     6116, 0xb4ee8e30, S=1,     1024, 0xcfc8799f
+0,        720,        720,        1,     6069, 0x21b263db, S=1,     1024, 0xcfc8799f
+0,        730,        730,        1,     5796, 0x2514df52, S=1,     1024, 0xcfc8799f
+0,        740,        740,        1,     5999, 0x1c3c3701, S=1,     1024, 0xcfc8799f
+0,        750,        750,        1,     6220, 0x8340b150, S=1,     1024, 0xcfc8799f
+0,        760,        760,        1,     6374, 0x00d8eaa5, S=1,     1024, 0xcfc8799f
+0,        770,        770,        1,     6465, 0x74c4778a, S=1,     1024, 0xcfc8799f
+0,        780,        780,        1,     7019, 0xdb1a28a3, S=1,     1024, 0xcfc8799f
+0,        790,        790,        1,     7255, 0x1e19b76e, S=1,     1024, 0xcfc8799f
+0,        800,        800,        1,     8197, 0x26bc6a79, S=1,     1024, 0xcfc8799f
+0,        810,        810,        1,     8358, 0x118781e0, S=1,     1024, 0xcfc8799f
+0,        820,        820,        1,     7708, 0xfc0c963d, S=1,     1024, 0xcfc8799f
+0,        830,        830,        1,     7412, 0xdcc311ee, S=1,     1024, 0xcfc8799f
+0,        840,        840,        1,     7540, 0x32fe19c0, S=1,     1024, 0xcfc8799f
+0,        850,        850,        1,     7948, 0xf12eca3d, S=1,     1024, 0xcfc8799f
+0,        860,        860,        1,     8408, 0x43add468, S=1,     1024, 0xcfc8799f
+0,        870,        870,        1,     8056, 0x2d162377, S=1,     1024, 0xcfc8799f
+0,        880,        880,        1,     7401, 0x26ebb649, S=1,     1024, 0xcfc8799f
+0,        890,        890,        1,     7494, 0x35fcf9ae, S=1,     1024, 0xcfc8799f
+0,        900,        900,        1,     7806, 0x4238723d, S=1,     1024, 0xcfc8799f
+0,        910,        910,        1,     7768, 0xb01e795a, S=1,     1024, 0xcfc8799f
+0,        920,        920,        1,     7749, 0x6ab39c12, S=1,     1024, 0xcfc8799f
+0,        930,        930,        1,     8047, 0x0e5f24aa, S=1,     1024, 0xcfc8799f
+0,        940,        940,        1,     7617, 0xa2c2340e, S=1,     1024, 0xcfc8799f
+0,        950,        950,        1,     7979, 0x0824c4df, S=1,     1024, 0xcfc8799f
+0,        960,        960,        1,    12062, 0xc46d9d92, S=1,     1024, 0xcfc8799f
+0,        970,        970,        1,    12317, 0x1314dc0c, S=1,     1024, 0xcfc8799f
+0,        980,        980,        1,    12217, 0x78c2ed30, S=1,     1024, 0xcfc8799f
+0,        990,        990,        1,    11226, 0x9ac08eb8, S=1,     1024, 0xcfc8799f
+0,       1000,       1000,        1,    11108, 0x4eaa068c, S=1,     1024, 0xcfc8799f
+0,       1010,       1010,        1,    11366, 0x48f8993f, S=1,     1024, 0xcfc8799f
+0,       1020,       1020,        1,    11896, 0x32414841, S=1,     1024, 0xcfc8799f
+0,       1030,       1030,        1,    11479, 0xeaa38225, S=1,     1024, 0xcfc8799f
+0,       1040,       1040,        1,    13395, 0xaa9d4c72, S=1,     1024, 0xcfc8799f
+0,       1050,       1050,        1,    12913, 0x28854353, S=1,     1024, 0xcfc8799f
+0,       1060,       1060,        1,    13864, 0x663df630, S=1,     1024, 0xcfc8799f
+0,       1070,       1070,        1,    13551, 0xf7ba7be7, S=1,     1024, 0xcfc8799f
+0,       1080,       1080,        1,    14041, 0x2dc071b9, S=1,     1024, 0xcfc8799f
+0,       1090,       1090,        1,    14144, 0x33a03d1d, S=1,     1024, 0xcfc8799f
+0,       1100,       1100,        1,    14277, 0x6bda5935, S=1,     1024, 0xcfc8799f
+0,       1110,       1110,        1,    14424, 0xa696efd8, S=1,     1024, 0xcfc8799f
+0,       1120,       1120,        1,    14689, 0x8e3ad12c, S=1,     1024, 0xcfc8799f
+0,       1130,       1130,        1,    14598, 0x544668b4, S=1,     1024, 0xcfc8799f
+0,       1140,       1140,        1,    15213, 0x60009558, S=1,     1024, 0xcfc8799f
+0,       1150,       1150,        1,    15425, 0x86e5adf4, S=1,     1024, 0xcfc8799f
+0,       1160,       1160,        1,    15595, 0x878d09b9, S=1,     1024, 0xcfc8799f
+0,       1170,       1170,        1,    15598, 0x10daabc4, S=1,     1024, 0xcfc8799f
+0,       1180,       1180,        1,    15863, 0x2462016c, S=1,     1024, 0xcfc8799f
+0,       1190,       1190,        1,    15717, 0xe05041c4, S=1,     1024, 0xcfc8799f
+0,       1200,       1200,        1,    16078, 0x7c8f3a8c, S=1,     1024, 0xcfc8799f
+0,       1210,       1210,        1,    16225, 0x9771a52e, S=1,     1024, 0xcfc8799f
+0,       1220,       1220,        1,    16135, 0x2dfc1692, S=1,     1024, 0xcfc8799f
+0,       1230,       1230,        1,    16661, 0x09c96d7e, S=1,     1024, 0xcfc8799f
+0,       1240,       1240,        1,    16619, 0xc4735b56, S=1,     1024, 0xcfc8799f
+0,       1250,       1250,        1,    16829, 0x589dc13f, S=1,     1024, 0xcfc8799f
+0,       1260,       1260,        1,    16944, 0x997cd18f, S=1,     1024, 0xcfc8799f
+0,       1270,       1270,        1,    17119, 0x6c396b60, S=1,     1024, 0xcfc8799f
+0,       1280,       1280,        1,    17150, 0x8e603d31, S=1,     1024, 0xcfc8799f
+0,       1290,       1290,        1,    17321, 0x0bbcee5a, S=1,     1024, 0xcfc8799f
+0,       1300,       1300,        1,    17395, 0x99f0c974, S=1,     1024, 0xcfc8799f
+0,       1310,       1310,        1,    17666, 0x37184223, S=1,     1024, 0xcfc8799f
+0,       1320,       1320,        1,    17730, 0xa0d385b3, S=1,     1024, 0xcfc8799f
+0,       1330,       1330,        1,    17934, 0xb22cc97d, S=1,     1024, 0xcfc8799f
+0,       1340,       1340,        1,    17944, 0x0cd309c6, S=1,     1024, 0xcfc8799f
+0,       1350,       1350,        1,    18238, 0x6b7e3237, S=1,     1024, 0xcfc8799f
+0,       1360,       1360,        1,    18390, 0x888fc489, S=1,     1024, 0xcfc8799f
+0,       1370,       1370,        1,    18543, 0x90a2f238, S=1,     1024, 0xcfc8799f
+0,       1380,       1380,        1,    18939, 0xc57dda5b, S=1,     1024, 0xcfc8799f
+0,       1390,       1390,        1,    19145, 0x1267294a, S=1,     1024, 0xcfc8799f
+0,       1400,       1400,        1,    19120, 0xeac6a9c3, S=1,     1024, 0xcfc8799f
+0,       1410,       1410,        1,    19130, 0x31f3edbc, S=1,     1024, 0xcfc8799f
+0,       1420,       1420,        1,    19494, 0x3259a2f3, S=1,     1024, 0xcfc8799f
+0,       1430,       1430,        1,    19534, 0xda22a752, S=1,     1024, 0xcfc8799f
+0,       1440,       1440,        1,    19747, 0x8805c379, S=1,     1024, 0xcfc8799f
+0,       1450,       1450,        1,    20114, 0xaaf96864, S=1,     1024, 0xcfc8799f
+0,       1460,       1460,        1,    20257, 0x7223da26, S=1,     1024, 0xcfc8799f
+0,       1470,       1470,        1,    20370, 0x08ef382a, S=1,     1024, 0xcfc8799f
+0,       1480,       1480,        1,    20292, 0x4b47f207, S=1,     1024, 0xcfc8799f
+0,       1490,       1490,        1,    20491, 0xeedd6d1c, S=1,     1024, 0xcfc8799f
+0,       1500,       1500,        1,    20647, 0xb0d1dd45, S=1,     1024, 0xcfc8799f
+0,       1510,       1510,        1,    20666, 0x382cc8a4, S=1,     1024, 0xcfc8799f
+0,       1520,       1520,        1,    21007, 0x398f4f7d, S=1,     1024, 0xcfc8799f
+0,       1530,       1530,        1,    21058, 0xd6616a9d, S=1,     1024, 0xcfc8799f
+0,       1540,       1540,        1,    21153, 0x988749db, S=1,     1024, 0xcfc8799f
+0,       1550,       1550,        1,    21078, 0x1b328059, S=1,     1024, 0xcfc8799f
+0,       1560,       1560,        1,    21458, 0x6348529c, S=1,     1024, 0xcfc8799f
+0,       1570,       1570,        1,    21669, 0xcf63e2de, S=1,     1024, 0xcfc8799f
+0,       1580,       1580,        1,    21581, 0x1fc021af, S=1,     1024, 0xcfc8799f
+0,       1590,       1590,        1,    21654, 0x899dab18, S=1,     1024, 0xcfc8799f
+0,       1600,       1600,        1,    21987, 0x634086fe, S=1,     1024, 0xcfc8799f
+0,       1610,       1610,        1,    22205, 0x617a7335, S=1,     1024, 0xcfc8799f
+0,       1620,       1620,        1,    22475, 0x9fa2e01c, S=1,     1024, 0xcfc8799f
+0,       1630,       1630,        1,    22490, 0x7dc5376c, S=1,     1024, 0xcfc8799f
+0,       1640,       1640,        1,    22460, 0x33e6bbfe, S=1,     1024, 0xcfc8799f
+0,       1650,       1650,        1,    22861, 0x18993510, S=1,     1024, 0xcfc8799f
+0,       1660,       1660,        1,    22746, 0xdff85615, S=1,     1024, 0xcfc8799f
+0,       1670,       1670,        1,    23164, 0x899866a2, S=1,     1024, 0xcfc8799f
+0,       1680,       1680,        1,    23273, 0x13869ad9, S=1,     1024, 0xcfc8799f
+0,       1690,       1690,        1,    23211, 0xd30b6205, S=1,     1024, 0xcfc8799f
+0,       1700,       1700,        1,    23648, 0xa0cef01b, S=1,     1024, 0xcfc8799f
+0,       1710,       1710,        1,    23675, 0x760460b9, S=1,     1024, 0xcfc8799f
+0,       1720,       1720,        1,    23874, 0xacf998c5, S=1,     1024, 0xcfc8799f
diff --git a/tests/ref/fate/h264-crop-to-container b/tests/ref/fate/h264-crop-to-container
new file mode 100644
index 0000000..0f435ea
--- /dev/null
+++ b/tests/ref/fate/h264-crop-to-container
@@ -0,0 +1,2 @@
+#tb 0: 1001/30000
+0,          0,          0,        1,  3110400, 43a312e1eebc7dca1bd23456302a44e3
diff --git a/tests/ref/fate/h264-reinit-large_420_8-to-small_420_8 b/tests/ref/fate/h264-reinit-large_420_8-to-small_420_8
new file mode 100644
index 0000000..8518855
--- /dev/null
+++ b/tests/ref/fate/h264-reinit-large_420_8-to-small_420_8
@@ -0,0 +1,101 @@
+#tb 0: 1/25
+0,          0,          0,        1,   608256, 0x65574c43
+0,          1,          1,        1,   608256, 0x5e5a1aa4
+0,          2,          2,        1,   608256, 0xa9a5cbc5
+0,          3,          3,        1,   608256, 0x4e462c02
+0,          4,          4,        1,   608256, 0xf8215f14
+0,          5,          5,        1,   608256, 0x4f332ccd
+0,          6,          6,        1,   608256, 0x5dac18a3
+0,          7,          7,        1,   608256, 0x81196f38
+0,          8,          8,        1,   608256, 0xd4cb4ffc
+0,          9,          9,        1,   608256, 0x839af1a4
+0,         10,         10,        1,   608256, 0x15ab7a99
+0,         11,         11,        1,   608256, 0x7496aec8
+0,         12,         12,        1,   608256, 0xfe974593
+0,         13,         13,        1,   608256, 0x702a94b7
+0,         14,         14,        1,   608256, 0x0f74cd7a
+0,         15,         15,        1,   608256, 0x3b35b667
+0,         16,         16,        1,   608256, 0x22083d70
+0,         17,         17,        1,   608256, 0xab77f6e1
+0,         18,         18,        1,   608256, 0x51513651
+0,         19,         19,        1,   608256, 0x06ff4ef1
+0,         20,         20,        1,   608256, 0xdf2ed7d3
+0,         21,         21,        1,   608256, 0x3107421b
+0,         22,         22,        1,   608256, 0x975823fe
+0,         23,         23,        1,   608256, 0xe40d9894
+0,         24,         24,        1,   608256, 0x8fb1fead
+0,         25,         25,        1,   608256, 0x56e5056b
+0,         26,         26,        1,   608256, 0x3346b970
+0,         27,         27,        1,   608256, 0x7e3c5b82
+0,         28,         28,        1,   608256, 0x6a86c482
+0,         29,         29,        1,   608256, 0x3e800e13
+0,         30,         30,        1,   608256, 0xd6c4589e
+0,         31,         31,        1,   608256, 0xcdfa7d94
+0,         32,         32,        1,   608256, 0xdc91cfce
+0,         33,         33,        1,   608256, 0x8aa5fd0e
+0,         34,         34,        1,   608256, 0x85c190be
+0,         35,         35,        1,   608256, 0xe7563061
+0,         36,         36,        1,   608256, 0x7fd501ec
+0,         37,         37,        1,   608256, 0x088d0df3
+0,         38,         38,        1,   608256, 0x08129a2f
+0,         39,         39,        1,   608256, 0x7bed8d9c
+0,         40,         40,        1,   608256, 0x16ce64c6
+0,         41,         41,        1,   608256, 0x6a120a3a
+0,         42,         42,        1,   608256, 0xfe0b889e
+0,         43,         43,        1,   608256, 0x57f4efcd
+0,         44,         44,        1,   608256, 0xc363ca91
+0,         45,         45,        1,   608256, 0x92237dce
+0,         46,         46,        1,   608256, 0x77bab64f
+0,         47,         47,        1,   608256, 0x29118201
+0,         48,         48,        1,   608256, 0x6f8e8e53
+0,         49,         49,        1,   608256, 0x4a13c4a3
+0,         50,         50,        1,   608256, 0x27a069a3
+0,         51,         51,        1,   608256, 0x22a5ff96
+0,         52,         52,        1,   608256, 0x29ad753f
+0,         53,         53,        1,   608256, 0x955788a6
+0,         54,         54,        1,   608256, 0xb73599c5
+0,         55,         55,        1,   608256, 0x73765aca
+0,         56,         56,        1,   608256, 0x39118f45
+0,         57,         57,        1,   608256, 0xfb0f8b96
+0,         58,         58,        1,   608256, 0xfcf71085
+0,         59,         59,        1,   608256, 0xccaaca7d
+0,         60,         60,        1,   608256, 0xde873299
+0,         61,         61,        1,   608256, 0x5d904202
+0,         62,         62,        1,   608256, 0x1e92c9b8
+0,         63,         63,        1,   608256, 0xdb8ec231
+0,         64,         64,        1,   608256, 0x388e2b9f
+0,         65,         65,        1,   608256, 0x1d190c39
+0,         66,         66,        1,   608256, 0xb6609efd
+0,         67,         67,        1,   608256, 0x0c6bf1d0
+0,         68,         68,        1,   608256, 0x178e0a3c
+0,         69,         69,        1,   608256, 0x439509f7
+0,         70,         70,        1,   608256, 0x00eb29ec
+0,         71,         71,        1,   608256, 0xee45f2a0
+0,         72,         72,        1,   608256, 0xae62eb8c
+0,         73,         73,        1,   608256, 0x3bb7510d
+0,         74,         74,        1,   608256, 0x03f08d02
+0,         75,         75,        1,   608256, 0x4beffc2a
+0,         76,         76,        1,   608256, 0x6071eb56
+0,         77,         77,        1,   608256, 0xbcb4e4e5
+0,         78,         78,        1,   608256, 0x775864aa
+0,         79,         79,        1,   608256, 0x3cfa0a94
+0,         80,         80,        1,   608256, 0x4652d529
+0,         81,         81,        1,   608256, 0xc5be07e2
+0,         82,         82,        1,   608256, 0xe7b480a2
+0,         83,         83,        1,   608256, 0x83b11945
+0,         84,         84,        1,   608256, 0x25ff0458
+0,         85,         85,        1,   608256, 0x5a780cda
+0,         86,         86,        1,   608256, 0x3448077c
+0,         87,         87,        1,   608256, 0x0f9c6f09
+0,         88,         88,        1,   608256, 0x05ece146
+0,         89,         89,        1,   608256, 0x3b02b504
+0,         90,         90,        1,   608256, 0xf3a8e2a6
+0,         91,         91,        1,   608256, 0xd4544847
+0,         92,         92,        1,   608256, 0x49bd2a3f
+0,         93,         93,        1,   608256, 0x22a42082
+0,         94,         94,        1,   608256, 0x8d8d1923
+0,         95,         95,        1,   608256, 0x2ba6877f
+0,         96,         96,        1,   608256, 0x494d481b
+0,         97,         97,        1,   608256, 0x28c99c35
+0,         98,         98,        1,   608256, 0x80128077
+0,         99,         99,        1,   608256, 0x887c2fb0
diff --git a/tests/ref/fate/h264-reinit-small_420_8-to-large_444_10 b/tests/ref/fate/h264-reinit-small_420_8-to-large_444_10
new file mode 100644
index 0000000..7e7631a
--- /dev/null
+++ b/tests/ref/fate/h264-reinit-small_420_8-to-large_444_10
@@ -0,0 +1,101 @@
+#tb 0: 1/25
+0,          0,          0,        1,   608256, 0x27a069a3
+0,          1,          1,        1,   608256, 0x22a5ff96
+0,          2,          2,        1,   608256, 0x29ad753f
+0,          3,          3,        1,   608256, 0x955788a6
+0,          4,          4,        1,   608256, 0xb73599c5
+0,          5,          5,        1,   608256, 0x73765aca
+0,          6,          6,        1,   608256, 0x39118f45
+0,          7,          7,        1,   608256, 0xfb0f8b96
+0,          8,          8,        1,   608256, 0xfcf71085
+0,          9,          9,        1,   608256, 0xccaaca7d
+0,         10,         10,        1,   608256, 0xde873299
+0,         11,         11,        1,   608256, 0x5d904202
+0,         12,         12,        1,   608256, 0x1e92c9b8
+0,         13,         13,        1,   608256, 0xdb8ec231
+0,         14,         14,        1,   608256, 0x388e2b9f
+0,         15,         15,        1,   608256, 0x1d190c39
+0,         16,         16,        1,   608256, 0xb6609efd
+0,         17,         17,        1,   608256, 0x0c6bf1d0
+0,         18,         18,        1,   608256, 0x178e0a3c
+0,         19,         19,        1,   608256, 0x439509f7
+0,         20,         20,        1,   608256, 0x00eb29ec
+0,         21,         21,        1,   608256, 0xee45f2a0
+0,         22,         22,        1,   608256, 0xae62eb8c
+0,         23,         23,        1,   608256, 0x3bb7510d
+0,         24,         24,        1,   608256, 0x03f08d02
+0,         25,         25,        1,   608256, 0x4beffc2a
+0,         26,         26,        1,   608256, 0x6071eb56
+0,         27,         27,        1,   608256, 0xbcb4e4e5
+0,         28,         28,        1,   608256, 0x775864aa
+0,         29,         29,        1,   608256, 0x3cfa0a94
+0,         30,         30,        1,   608256, 0x4652d529
+0,         31,         31,        1,   608256, 0xc5be07e2
+0,         32,         32,        1,   608256, 0xe7b480a2
+0,         33,         33,        1,   608256, 0x83b11945
+0,         34,         34,        1,   608256, 0x25ff0458
+0,         35,         35,        1,   608256, 0x5a780cda
+0,         36,         36,        1,   608256, 0x3448077c
+0,         37,         37,        1,   608256, 0x0f9c6f09
+0,         38,         38,        1,   608256, 0x05ece146
+0,         39,         39,        1,   608256, 0x3b02b504
+0,         40,         40,        1,   608256, 0xf3a8e2a6
+0,         41,         41,        1,   608256, 0xd4544847
+0,         42,         42,        1,   608256, 0x49bd2a3f
+0,         43,         43,        1,   608256, 0x22a42082
+0,         44,         44,        1,   608256, 0x8d8d1923
+0,         45,         45,        1,   608256, 0x2ba6877f
+0,         46,         46,        1,   608256, 0x494d481b
+0,         47,         47,        1,   608256, 0x28c99c35
+0,         48,         48,        1,   608256, 0x80128077
+0,         49,         49,        1,   608256, 0x887c2fb0
+0,         50,         50,        1,   608256, 0xfaba3fe4
+0,         51,         51,        1,   608256, 0x0369a227
+0,         52,         52,        1,   608256, 0xe62cb2b1
+0,         53,         53,        1,   608256, 0x851d9e78
+0,         54,         54,        1,   608256, 0x6847b938
+0,         55,         55,        1,   608256, 0x48eb702e
+0,         56,         56,        1,   608256, 0x3eb52eec
+0,         57,         57,        1,   608256, 0xf880edf8
+0,         58,         58,        1,   608256, 0xf7ac1860
+0,         59,         59,        1,   608256, 0x3ead29f3
+0,         60,         60,        1,   608256, 0x6db1860a
+0,         61,         61,        1,   608256, 0x72ee9271
+0,         62,         62,        1,   608256, 0xa8b643e4
+0,         63,         63,        1,   608256, 0x22a95a3e
+0,         64,         64,        1,   608256, 0x4e8aa877
+0,         65,         65,        1,   608256, 0x9545cfcf
+0,         66,         66,        1,   608256, 0x42d820e0
+0,         67,         67,        1,   608256, 0xe701ea6f
+0,         68,         68,        1,   608256, 0x05f61a49
+0,         69,         69,        1,   608256, 0xc1de9f65
+0,         70,         70,        1,   608256, 0x2b121933
+0,         71,         71,        1,   608256, 0x4de4e185
+0,         72,         72,        1,   608256, 0x01c6fb05
+0,         73,         73,        1,   608256, 0x1fbf8603
+0,         74,         74,        1,   608256, 0x9d8f2fd4
+0,         75,         75,        1,   608256, 0xb7d6f14e
+0,         76,         76,        1,   608256, 0x503c33a9
+0,         77,         77,        1,   608256, 0x051e8b02
+0,         78,         78,        1,   608256, 0xeff7c286
+0,         79,         79,        1,   608256, 0xeb80088a
+0,         80,         80,        1,   608256, 0xffbc6cec
+0,         81,         81,        1,   608256, 0x42d55e9d
+0,         82,         82,        1,   608256, 0x4898c507
+0,         83,         83,        1,   608256, 0x2c2411b5
+0,         84,         84,        1,   608256, 0xd70a34a4
+0,         85,         85,        1,   608256, 0xce5e64f5
+0,         86,         86,        1,   608256, 0x7790d293
+0,         87,         87,        1,   608256, 0xac5b44fb
+0,         88,         88,        1,   608256, 0xae4c2921
+0,         89,         89,        1,   608256, 0x373a26bb
+0,         90,         90,        1,   608256, 0x3aa3fed4
+0,         91,         91,        1,   608256, 0x4f4b1496
+0,         92,         92,        1,   608256, 0x4760fe2b
+0,         93,         93,        1,   608256, 0x9e38af43
+0,         94,         94,        1,   608256, 0xd7c66f6e
+0,         95,         95,        1,   608256, 0xf6cb1649
+0,         96,         96,        1,   608256, 0xb0591bf1
+0,         97,         97,        1,   608256, 0xb507b282
+0,         98,         98,        1,   608256, 0xfb53c6e5
+0,         99,         99,        1,   608256, 0x5410f10f
diff --git a/tests/ref/fate/h264-reinit-small_420_9-to-small_420_8 b/tests/ref/fate/h264-reinit-small_420_9-to-small_420_8
new file mode 100644
index 0000000..662db29
--- /dev/null
+++ b/tests/ref/fate/h264-reinit-small_420_9-to-small_420_8
@@ -0,0 +1,101 @@
+#tb 0: 1/25
+0,          0,          0,        1,   608256, 0xee06377f
+0,          1,          1,        1,   608256, 0x9c22c5ee
+0,          2,          2,        1,   608256, 0x322abee2
+0,          3,          3,        1,   608256, 0x82745ed3
+0,          4,          4,        1,   608256, 0xe7b9248b
+0,          5,          5,        1,   608256, 0xc8c1ce0e
+0,          6,          6,        1,   608256, 0x8451eba9
+0,          7,          7,        1,   608256, 0xece9412d
+0,          8,          8,        1,   608256, 0xffbc2c24
+0,          9,          9,        1,   608256, 0xf58c117e
+0,         10,         10,        1,   608256, 0x7853f334
+0,         11,         11,        1,   608256, 0x1acd6465
+0,         12,         12,        1,   608256, 0xb668856c
+0,         13,         13,        1,   608256, 0x3f52c230
+0,         14,         14,        1,   608256, 0xd7e8b5b6
+0,         15,         15,        1,   608256, 0xf663681d
+0,         16,         16,        1,   608256, 0x07ef670c
+0,         17,         17,        1,   608256, 0x1d5084f4
+0,         18,         18,        1,   608256, 0x0ea3fe04
+0,         19,         19,        1,   608256, 0xf095d1e0
+0,         20,         20,        1,   608256, 0xbdac37fb
+0,         21,         21,        1,   608256, 0x2cbfdf91
+0,         22,         22,        1,   608256, 0x8da7190f
+0,         23,         23,        1,   608256, 0x08bf6fc9
+0,         24,         24,        1,   608256, 0xa2368e5c
+0,         25,         25,        1,   608256, 0x36d38f81
+0,         26,         26,        1,   608256, 0x94d344cc
+0,         27,         27,        1,   608256, 0x1cf33f86
+0,         28,         28,        1,   608256, 0x98d6341f
+0,         29,         29,        1,   608256, 0xb04e3a3a
+0,         30,         30,        1,   608256, 0x74e52483
+0,         31,         31,        1,   608256, 0x6acc8d96
+0,         32,         32,        1,   608256, 0x136d6c4e
+0,         33,         33,        1,   608256, 0x50efa37a
+0,         34,         34,        1,   608256, 0x53170421
+0,         35,         35,        1,   608256, 0x2ed5210e
+0,         36,         36,        1,   608256, 0x5f471854
+0,         37,         37,        1,   608256, 0xd6ff7856
+0,         38,         38,        1,   608256, 0x446bf456
+0,         39,         39,        1,   608256, 0x872b95b1
+0,         40,         40,        1,   608256, 0xc9e5e827
+0,         41,         41,        1,   608256, 0xdab3eb04
+0,         42,         42,        1,   608256, 0xccd6347e
+0,         43,         43,        1,   608256, 0xd7dfd0a9
+0,         44,         44,        1,   608256, 0x60b834c3
+0,         45,         45,        1,   608256, 0x0b822676
+0,         46,         46,        1,   608256, 0x0e5d3d5a
+0,         47,         47,        1,   608256, 0x1fa0233d
+0,         48,         48,        1,   608256, 0xb853b650
+0,         49,         49,        1,   608256, 0x30d8d033
+0,         50,         50,        1,   608256, 0x27a069a3
+0,         51,         51,        1,   608256, 0x22a5ff96
+0,         52,         52,        1,   608256, 0x29ad753f
+0,         53,         53,        1,   608256, 0x955788a6
+0,         54,         54,        1,   608256, 0xb73599c5
+0,         55,         55,        1,   608256, 0x73765aca
+0,         56,         56,        1,   608256, 0x39118f45
+0,         57,         57,        1,   608256, 0xfb0f8b96
+0,         58,         58,        1,   608256, 0xfcf71085
+0,         59,         59,        1,   608256, 0xccaaca7d
+0,         60,         60,        1,   608256, 0xde873299
+0,         61,         61,        1,   608256, 0x5d904202
+0,         62,         62,        1,   608256, 0x1e92c9b8
+0,         63,         63,        1,   608256, 0xdb8ec231
+0,         64,         64,        1,   608256, 0x388e2b9f
+0,         65,         65,        1,   608256, 0x1d190c39
+0,         66,         66,        1,   608256, 0xb6609efd
+0,         67,         67,        1,   608256, 0x0c6bf1d0
+0,         68,         68,        1,   608256, 0x178e0a3c
+0,         69,         69,        1,   608256, 0x439509f7
+0,         70,         70,        1,   608256, 0x00eb29ec
+0,         71,         71,        1,   608256, 0xee45f2a0
+0,         72,         72,        1,   608256, 0xae62eb8c
+0,         73,         73,        1,   608256, 0x3bb7510d
+0,         74,         74,        1,   608256, 0x03f08d02
+0,         75,         75,        1,   608256, 0x4beffc2a
+0,         76,         76,        1,   608256, 0x6071eb56
+0,         77,         77,        1,   608256, 0xbcb4e4e5
+0,         78,         78,        1,   608256, 0x775864aa
+0,         79,         79,        1,   608256, 0x3cfa0a94
+0,         80,         80,        1,   608256, 0x4652d529
+0,         81,         81,        1,   608256, 0xc5be07e2
+0,         82,         82,        1,   608256, 0xe7b480a2
+0,         83,         83,        1,   608256, 0x83b11945
+0,         84,         84,        1,   608256, 0x25ff0458
+0,         85,         85,        1,   608256, 0x5a780cda
+0,         86,         86,        1,   608256, 0x3448077c
+0,         87,         87,        1,   608256, 0x0f9c6f09
+0,         88,         88,        1,   608256, 0x05ece146
+0,         89,         89,        1,   608256, 0x3b02b504
+0,         90,         90,        1,   608256, 0xf3a8e2a6
+0,         91,         91,        1,   608256, 0xd4544847
+0,         92,         92,        1,   608256, 0x49bd2a3f
+0,         93,         93,        1,   608256, 0x22a42082
+0,         94,         94,        1,   608256, 0x8d8d1923
+0,         95,         95,        1,   608256, 0x2ba6877f
+0,         96,         96,        1,   608256, 0x494d481b
+0,         97,         97,        1,   608256, 0x28c99c35
+0,         98,         98,        1,   608256, 0x80128077
+0,         99,         99,        1,   608256, 0x887c2fb0
diff --git a/tests/ref/fate/h264-reinit-small_422_9-to-small_420_9 b/tests/ref/fate/h264-reinit-small_422_9-to-small_420_9
new file mode 100644
index 0000000..b5a9505
--- /dev/null
+++ b/tests/ref/fate/h264-reinit-small_422_9-to-small_420_9
@@ -0,0 +1,101 @@
+#tb 0: 1/25
+0,          0,          0,        1,   608256, 0x3aef31f3
+0,          1,          1,        1,   608256, 0x05afe4e8
+0,          2,          2,        1,   608256, 0x9d7dc9b1
+0,          3,          3,        1,   608256, 0x60b3efec
+0,          4,          4,        1,   608256, 0x7dc51741
+0,          5,          5,        1,   608256, 0xb3bfb413
+0,          6,          6,        1,   608256, 0x0a2738b3
+0,          7,          7,        1,   608256, 0xfd3f8f1b
+0,          8,          8,        1,   608256, 0x922d413f
+0,          9,          9,        1,   608256, 0x775b4cfb
+0,         10,         10,        1,   608256, 0xcbfaec82
+0,         11,         11,        1,   608256, 0x1f1903a2
+0,         12,         12,        1,   608256, 0xdd246528
+0,         13,         13,        1,   608256, 0xe3d8afdc
+0,         14,         14,        1,   608256, 0xb29c4235
+0,         15,         15,        1,   608256, 0xa230e654
+0,         16,         16,        1,   608256, 0xe0246276
+0,         17,         17,        1,   608256, 0xd47b6b3c
+0,         18,         18,        1,   608256, 0xa5593d23
+0,         19,         19,        1,   608256, 0x01cd5642
+0,         20,         20,        1,   608256, 0x0c451611
+0,         21,         21,        1,   608256, 0xc4833ff5
+0,         22,         22,        1,   608256, 0xbed881fc
+0,         23,         23,        1,   608256, 0x709e77e6
+0,         24,         24,        1,   608256, 0x9815dad3
+0,         25,         25,        1,   608256, 0x55c1fe5f
+0,         26,         26,        1,   608256, 0xd89ff770
+0,         27,         27,        1,   608256, 0xc636abf7
+0,         28,         28,        1,   608256, 0x17d3f5b1
+0,         29,         29,        1,   608256, 0x0389cd38
+0,         30,         30,        1,   608256, 0x502eea66
+0,         31,         31,        1,   608256, 0xcb697a48
+0,         32,         32,        1,   608256, 0xd4f6c694
+0,         33,         33,        1,   608256, 0x734a5163
+0,         34,         34,        1,   608256, 0xa53f5187
+0,         35,         35,        1,   608256, 0x64d2c1c7
+0,         36,         36,        1,   608256, 0x41a170d0
+0,         37,         37,        1,   608256, 0x6f671fe1
+0,         38,         38,        1,   608256, 0x56983008
+0,         39,         39,        1,   608256, 0x95911957
+0,         40,         40,        1,   608256, 0x027f2a7a
+0,         41,         41,        1,   608256, 0xcd22d70f
+0,         42,         42,        1,   608256, 0x268beec0
+0,         43,         43,        1,   608256, 0xcf14123e
+0,         44,         44,        1,   608256, 0xfc8e3bff
+0,         45,         45,        1,   608256, 0x6974ad54
+0,         46,         46,        1,   608256, 0x8fbd9cdd
+0,         47,         47,        1,   608256, 0x66110ef8
+0,         48,         48,        1,   608256, 0x84b4b1f1
+0,         49,         49,        1,   608256, 0xf0d4949b
+0,         50,         50,        1,   608256, 0xee06377f
+0,         51,         51,        1,   608256, 0x9c22c5ee
+0,         52,         52,        1,   608256, 0x322abee2
+0,         53,         53,        1,   608256, 0x82745ed3
+0,         54,         54,        1,   608256, 0xe7b9248b
+0,         55,         55,        1,   608256, 0xc8c1ce0e
+0,         56,         56,        1,   608256, 0x8451eba9
+0,         57,         57,        1,   608256, 0xece9412d
+0,         58,         58,        1,   608256, 0xffbc2c24
+0,         59,         59,        1,   608256, 0xf58c117e
+0,         60,         60,        1,   608256, 0x7853f334
+0,         61,         61,        1,   608256, 0x1acd6465
+0,         62,         62,        1,   608256, 0xb668856c
+0,         63,         63,        1,   608256, 0x3f52c230
+0,         64,         64,        1,   608256, 0xd7e8b5b6
+0,         65,         65,        1,   608256, 0xf663681d
+0,         66,         66,        1,   608256, 0x07ef670c
+0,         67,         67,        1,   608256, 0x1d5084f4
+0,         68,         68,        1,   608256, 0x0ea3fe04
+0,         69,         69,        1,   608256, 0xf095d1e0
+0,         70,         70,        1,   608256, 0xbdac37fb
+0,         71,         71,        1,   608256, 0x2cbfdf91
+0,         72,         72,        1,   608256, 0x8da7190f
+0,         73,         73,        1,   608256, 0x08bf6fc9
+0,         74,         74,        1,   608256, 0xa2368e5c
+0,         75,         75,        1,   608256, 0x36d38f81
+0,         76,         76,        1,   608256, 0x94d344cc
+0,         77,         77,        1,   608256, 0x1cf33f86
+0,         78,         78,        1,   608256, 0x98d6341f
+0,         79,         79,        1,   608256, 0xb04e3a3a
+0,         80,         80,        1,   608256, 0x74e52483
+0,         81,         81,        1,   608256, 0x6acc8d96
+0,         82,         82,        1,   608256, 0x136d6c4e
+0,         83,         83,        1,   608256, 0x50efa37a
+0,         84,         84,        1,   608256, 0x53170421
+0,         85,         85,        1,   608256, 0x2ed5210e
+0,         86,         86,        1,   608256, 0x5f471854
+0,         87,         87,        1,   608256, 0xd6ff7856
+0,         88,         88,        1,   608256, 0x446bf456
+0,         89,         89,        1,   608256, 0x872b95b1
+0,         90,         90,        1,   608256, 0xc9e5e827
+0,         91,         91,        1,   608256, 0xdab3eb04
+0,         92,         92,        1,   608256, 0xccd6347e
+0,         93,         93,        1,   608256, 0xd7dfd0a9
+0,         94,         94,        1,   608256, 0x60b834c3
+0,         95,         95,        1,   608256, 0x0b822676
+0,         96,         96,        1,   608256, 0x0e5d3d5a
+0,         97,         97,        1,   608256, 0x1fa0233d
+0,         98,         98,        1,   608256, 0xb853b650
+0,         99,         99,        1,   608256, 0x30d8d033
diff --git a/tests/ref/fate/jpeg2000-dcinema b/tests/ref/fate/jpeg2000-dcinema
new file mode 100644
index 0000000..940759a
--- /dev/null
+++ b/tests/ref/fate/jpeg2000-dcinema
@@ -0,0 +1,3 @@
+#tb 0: 1/24
+0,          0,          0,        1, 12441600, 0xf0de508b
+0,          1,          1,        1, 12441600, 0x8e50c249
diff --git a/tests/ref/fate/mpeg2-field-enc b/tests/ref/fate/mpeg2-field-enc
index 903adb5..bf8a01a 100644
--- a/tests/ref/fate/mpeg2-field-enc
+++ b/tests/ref/fate/mpeg2-field-enc
@@ -29,4 +29,3 @@
 0,         36,         36,        1,   622080, 0xa45e1d95
 0,         37,         37,        1,   622080, 0x6cc61d6c
 0,         38,         38,        1,   622080, 0x6983b417
-0,         39,         39,        1,   622080, 0x982363c0
diff --git a/tests/ref/fate/sub-charenc b/tests/ref/fate/sub-charenc
index 3c4825e..ef72754 100644
--- a/tests/ref/fate/sub-charenc
+++ b/tests/ref/fate/sub-charenc
@@ -1 +1 @@
-a39d7e299a8e25b4ffece0f8d64bf19e
+9615088d613931b50fd5016c7535b99a
diff --git a/tests/ref/fate/tscc2 b/tests/ref/fate/tscc2
new file mode 100644
index 0000000..ad71b82
--- /dev/null
+++ b/tests/ref/fate/tscc2
@@ -0,0 +1,33 @@
+#tb 0: 1/24
+0,          0,          0,        1,   230400, 0x7a2103c0
+0,          1,          1,        1,   230400, 0xd381c279
+0,          2,          2,        1,   230400, 0xd381c279
+0,          3,          3,        1,   230400, 0x110aec27
+0,          4,          4,        1,   230400, 0x4be67ee7
+0,          5,          5,        1,   230400, 0xd87fe4b4
+0,          6,          6,        1,   230400, 0xd87fe4b4
+0,          7,          7,        1,   230400, 0x9bc6a398
+0,          8,          8,        1,   230400, 0xd67d92db
+0,          9,          9,        1,   230400, 0x3df6559e
+0,         10,         10,        1,   230400, 0x3df6559e
+0,         11,         11,        1,   230400, 0x2136ff25
+0,         12,         12,        1,   230400, 0x94573fe6
+0,         13,         13,        1,   230400, 0xbf67d3f5
+0,         14,         14,        1,   230400, 0xbf67d3f5
+0,         15,         15,        1,   230400, 0x2592b5cf
+0,         16,         16,        1,   230400, 0x5b23cd93
+0,         17,         17,        1,   230400, 0x9b76d079
+0,         18,         18,        1,   230400, 0x9b76d079
+0,         19,         19,        1,   230400, 0x771a017e
+0,         20,         20,        1,   230400, 0xacfee1d0
+0,         21,         21,        1,   230400, 0x6b9ff4eb
+0,         22,         22,        1,   230400, 0x6b9ff4eb
+0,         23,         23,        1,   230400, 0xbaf643e1
+0,         24,         24,        1,   230400, 0x052efe59
+0,         25,         25,        1,   230400, 0xd751f901
+0,         26,         26,        1,   230400, 0xd751f901
+0,         27,         27,        1,   230400, 0x6f94e11f
+0,         28,         28,        1,   230400, 0x17eeabb9
+0,         29,         29,        1,   230400, 0x3733a035
+0,         30,         30,        1,   230400, 0x3733a035
+0,         31,         31,        1,   230400, 0xb0829f45
diff --git a/tests/ref/fate/unknown_layout-ac3 b/tests/ref/fate/unknown_layout-ac3
new file mode 100644
index 0000000..d332efc
--- /dev/null
+++ b/tests/ref/fate/unknown_layout-ac3
@@ -0,0 +1 @@
+bbb7550d6d93973c10f4ee13c87cf799
diff --git a/tests/ref/fate/unknown_layout-pcm b/tests/ref/fate/unknown_layout-pcm
new file mode 100644
index 0000000..73c6a2d
--- /dev/null
+++ b/tests/ref/fate/unknown_layout-pcm
@@ -0,0 +1 @@
+4dada0795adf50f7a0e60861658f86ea
diff --git a/tests/ref/fate/zmbv-15bit b/tests/ref/fate/zmbv-15bit
index f054a9d..384bb65 100644
--- a/tests/ref/fate/zmbv-15bit
+++ b/tests/ref/fate/zmbv-15bit
@@ -1,160 +1,160 @@
 #tb 0: 250000/1585341
-0,          0,          0,        1,   192000, 0xe1d317d6
-0,          1,          1,        1,   192000, 0xe100109d
-0,          2,          2,        1,   192000, 0xecc69c65
-0,          3,          3,        1,   192000, 0x68f06324
-0,          4,          4,        1,   192000, 0x68f06324
-0,          5,          5,        1,   192000, 0x68f06324
-0,          6,          6,        1,   192000, 0x68f06324
-0,          7,          7,        1,   192000, 0x68f06324
-0,          8,          8,        1,   192000, 0x68f06324
-0,          9,          9,        1,   192000, 0x68f06324
-0,         10,         10,        1,   192000, 0x68f06324
-0,         11,         11,        1,   192000, 0x68f06324
-0,         12,         12,        1,   192000, 0x68f06324
-0,         13,         13,        1,   192000, 0x68f06324
-0,         14,         14,        1,   192000, 0x68f06324
-0,         15,         15,        1,   192000, 0x68f06324
-0,         16,         16,        1,   192000, 0x68f06324
-0,         17,         17,        1,   192000, 0x4c03d2e3
-0,         18,         18,        1,   192000, 0x4c03d2e3
-0,         19,         19,        1,   192000, 0x4c03d2e3
-0,         20,         20,        1,   192000, 0x4c03d2e3
-0,         21,         21,        1,   192000, 0xb562cf68
-0,         22,         22,        1,   192000, 0xb562cf68
-0,         23,         23,        1,   192000, 0xb562cf68
-0,         24,         24,        1,   192000, 0xb562cf68
-0,         25,         25,        1,   192000, 0xb562cf68
-0,         26,         26,        1,   192000, 0x0e5e03c7
-0,         27,         27,        1,   192000, 0x0e5e03c7
-0,         28,         28,        1,   192000, 0x0e5e03c7
-0,         29,         29,        1,   192000, 0x0e5e03c7
-0,         30,         30,        1,   192000, 0xeb2e0f67
-0,         31,         31,        1,   192000, 0xeb2e0f67
-0,         32,         32,        1,   192000, 0xeb2e0f67
-0,         33,         33,        1,   192000, 0xeb2e0f67
-0,         34,         34,        1,   192000, 0xeb2e0f67
-0,         35,         35,        1,   192000, 0xdce603c7
-0,         36,         36,        1,   192000, 0xdce603c7
-0,         37,         37,        1,   192000, 0xdce603c7
-0,         38,         38,        1,   192000, 0xdce603c7
-0,         39,         39,        1,   192000, 0xa8e7db08
-0,         40,         40,        1,   192000, 0xa8e7db08
-0,         41,         41,        1,   192000, 0xa8e7db08
-0,         42,         42,        1,   192000, 0xa8e7db08
-0,         43,         43,        1,   192000, 0xa8e7db08
-0,         44,         44,        1,   192000, 0x322a1b07
-0,         45,         45,        1,   192000, 0x322a1b07
-0,         46,         46,        1,   192000, 0x322a1b07
-0,         47,         47,        1,   192000, 0x322a1b07
-0,         48,         48,        1,   192000, 0x743206af
-0,         49,         49,        1,   192000, 0x743206af
-0,         50,         50,        1,   192000, 0x743206af
-0,         51,         51,        1,   192000, 0x743206af
-0,         52,         52,        1,   192000, 0x743206af
-0,         53,         53,        1,   192000, 0x50195ddf
-0,         54,         54,        1,   192000, 0x50195ddf
-0,         55,         55,        1,   192000, 0x50195ddf
-0,         56,         56,        1,   192000, 0x50195ddf
-0,         57,         57,        1,   192000, 0xd31620d7
-0,         58,         58,        1,   192000, 0xd31620d7
-0,         59,         59,        1,   192000, 0xd31620d7
-0,         60,         60,        1,   192000, 0xd31620d7
-0,         61,         61,        1,   192000, 0xd31620d7
-0,         62,         62,        1,   192000, 0x3af023bf
-0,         63,         63,        1,   192000, 0x3af023bf
-0,         64,         64,        1,   192000, 0x3af023bf
-0,         65,         65,        1,   192000, 0x3af023bf
-0,         66,         66,        1,   192000, 0x561a124f
-0,         67,         67,        1,   192000, 0x561a124f
-0,         68,         68,        1,   192000, 0x561a124f
-0,         69,         69,        1,   192000, 0x561a124f
-0,         70,         70,        1,   192000, 0x561a124f
-0,         71,         71,        1,   192000, 0x99210c7f
-0,         72,         72,        1,   192000, 0x99210c7f
-0,         73,         73,        1,   192000, 0x99210c7f
-0,         74,         74,        1,   192000, 0x99210c7f
-0,         75,         75,        1,   192000, 0xc77b03c7
-0,         76,         76,        1,   192000, 0xc77b03c7
-0,         77,         77,        1,   192000, 0xc77b03c7
-0,         78,         78,        1,   192000, 0xc77b03c7
-0,         79,         79,        1,   192000, 0xc77b03c7
-0,         80,         80,        1,   192000, 0x83ea7550
-0,         81,         81,        1,   192000, 0xec285270
-0,         82,         82,        1,   192000, 0x0e075558
-0,         83,         83,        1,   192000, 0x880c2108
-0,         84,         84,        1,   192000, 0x40c523f0
-0,         85,         85,        1,   192000, 0x01378f78
-0,         86,         86,        1,   192000, 0x42045558
-0,         87,         87,        1,   192000, 0xde8f3278
-0,         88,         88,        1,   192000, 0xa58c0110
-0,         89,         89,        1,   192000, 0x335ea9d1
-0,         90,         90,        1,   192000, 0x7e94bb41
-0,         91,         91,        1,   192000, 0x9cc5d569
-0,         92,         92,        1,   192000, 0xe942e109
-0,         93,         93,        1,   192000, 0x4cb83848
-0,         94,         94,        1,   192000, 0x6986fe19
-0,         95,         95,        1,   192000, 0xbb8c23f0
-0,         96,         96,        1,   192000, 0x296766c8
-0,         97,         97,        1,   192000, 0xb4230cb0
-0,         98,         98,        1,   192000, 0x2c2f1850
-0,         99,         99,        1,   192000, 0x93c70110
-0,        100,        100,        1,   192000, 0xb830a9d1
-0,        101,        101,        1,   192000, 0xbed48fa9
-0,        102,        102,        1,   192000, 0xc087bb41
-0,        103,        103,        1,   192000, 0x792de6d9
-0,        104,        104,        1,   192000, 0x1edaf849
-0,        105,        105,        1,   192000, 0x6564bb41
-0,        106,        106,        1,   192000, 0x9153d569
-0,        107,        107,        1,   192000, 0xe73ff561
-0,        108,        108,        1,   192000, 0xfa3be3f1
-0,        109,        109,        1,   192000, 0x8008fe19
-0,        110,        110,        1,   192000, 0xd2561b38
-0,        111,        111,        1,   192000, 0xae3c26d8
-0,        112,        112,        1,   192000, 0xb0114f88
-0,        113,        113,        1,   192000, 0x117e1e20
-0,        114,        114,        1,   192000, 0x482d1280
-0,        115,        115,        1,   192000, 0x425106e0
-0,        116,        116,        1,   192000, 0x0e6b0cb0
-0,        117,        117,        1,   192000, 0x95dc2ca8
-0,        118,        118,        1,   192000, 0x52097b20
-0,        119,        119,        1,   192000, 0x41a84f88
-0,        120,        120,        1,   192000, 0xb78d7268
-0,        121,        121,        1,   192000, 0x2cd366c8
-0,        122,        122,        1,   192000, 0xbf39e109
-0,        123,        123,        1,   192000, 0xecbaeca9
-0,        124,        124,        1,   192000, 0x3254eca9
-0,        125,        125,        1,   192000, 0x3fc903f8
-0,        126,        126,        1,   192000, 0x0bbc5e10
-0,        127,        127,        1,   192000, 0xe9103560
-0,        128,        128,        1,   192000, 0xbd5d2f90
-0,        129,        129,        1,   192000, 0x7ace2ca8
-0,        130,        130,        1,   192000, 0x7354c6e1
-0,        131,        131,        1,   192000, 0x72e0d569
-0,        132,        132,        1,   192000, 0xa4ade3f1
-0,        133,        133,        1,   192000, 0xf2f8cf99
-0,        134,        134,        1,   192000, 0x2b7ee6d9
-0,        135,        135,        1,   192000, 0x548d1b38
-0,        136,        136,        1,   192000, 0xa1551b38
-0,        137,        137,        1,   192000, 0xfb3e3560
-0,        138,        138,        1,   192000, 0x3aaaccb1
-0,        139,        139,        1,   192000, 0xa85ee109
-0,        140,        140,        1,   192000, 0xc1bff849
-0,        141,        141,        1,   192000, 0xa62bef91
-0,        142,        142,        1,   192000, 0xddf40cb0
-0,        143,        143,        1,   192000, 0x15bb3e18
-0,        144,        144,        1,   192000, 0x92fd5558
-0,        145,        145,        1,   192000, 0x2d365270
-0,        146,        146,        1,   192000, 0xb5f343e8
-0,        147,        147,        1,   192000, 0x93042108
-0,        148,        148,        1,   192000, 0x96d59830
-0,        149,        149,        1,   192000, 0x1f69ddf0
-0,        150,        150,        1,   192000, 0x8eb0124f
-0,        151,        151,        1,   192000, 0xae727dd7
-0,        152,        152,        1,   192000, 0xb8a63aff
-0,        153,        153,        1,   192000, 0xfae83de7
-0,        154,        154,        1,   192000, 0x2f034987
-0,        155,        155,        1,   192000, 0xa99e1537
-0,        156,        156,        1,   192000, 0x0ad70c7f
-0,        157,        157,        1,   192000, 0x74b3e990
-0,        158,        158,        1,   192000, 0x1bf0d250
+0,          0,          0,        1,   192000, 0x8f9020f2
+0,          1,          1,        1,   192000, 0xbfba7f63
+0,          2,          2,        1,   192000, 0x96b48d11
+0,          3,          3,        1,   192000, 0xf91727d5
+0,          4,          4,        1,   192000, 0xf91727d5
+0,          5,          5,        1,   192000, 0xf91727d5
+0,          6,          6,        1,   192000, 0xf91727d5
+0,          7,          7,        1,   192000, 0xf91727d5
+0,          8,          8,        1,   192000, 0xf91727d5
+0,          9,          9,        1,   192000, 0xf91727d5
+0,         10,         10,        1,   192000, 0xf91727d5
+0,         11,         11,        1,   192000, 0xf91727d5
+0,         12,         12,        1,   192000, 0xf91727d5
+0,         13,         13,        1,   192000, 0xf91727d5
+0,         14,         14,        1,   192000, 0xf91727d5
+0,         15,         15,        1,   192000, 0xf91727d5
+0,         16,         16,        1,   192000, 0xf91727d5
+0,         17,         17,        1,   192000, 0x61c4e431
+0,         18,         18,        1,   192000, 0x61c4e431
+0,         19,         19,        1,   192000, 0x61c4e431
+0,         20,         20,        1,   192000, 0x61c4e431
+0,         21,         21,        1,   192000, 0x1fb3f649
+0,         22,         22,        1,   192000, 0x1fb3f649
+0,         23,         23,        1,   192000, 0x1fb3f649
+0,         24,         24,        1,   192000, 0x1fb3f649
+0,         25,         25,        1,   192000, 0x1fb3f649
+0,         26,         26,        1,   192000, 0x14f72c22
+0,         27,         27,        1,   192000, 0x14f72c22
+0,         28,         28,        1,   192000, 0x14f72c22
+0,         29,         29,        1,   192000, 0x14f72c22
+0,         30,         30,        1,   192000, 0x8cae3816
+0,         31,         31,        1,   192000, 0x8cae3816
+0,         32,         32,        1,   192000, 0x8cae3816
+0,         33,         33,        1,   192000, 0x8cae3816
+0,         34,         34,        1,   192000, 0x8cae3816
+0,         35,         35,        1,   192000, 0x2b6f2c22
+0,         36,         36,        1,   192000, 0x2b6f2c22
+0,         37,         37,        1,   192000, 0x2b6f2c22
+0,         38,         38,        1,   192000, 0x2b6f2c22
+0,         39,         39,        1,   192000, 0xc568024c
+0,         40,         40,        1,   192000, 0xc568024c
+0,         41,         41,        1,   192000, 0xc568024c
+0,         42,         42,        1,   192000, 0xc568024c
+0,         43,         43,        1,   192000, 0xc568024c
+0,         44,         44,        1,   192000, 0xa93b440a
+0,         45,         45,        1,   192000, 0xa93b440a
+0,         46,         46,        1,   192000, 0xa93b440a
+0,         47,         47,        1,   192000, 0xa93b440a
+0,         48,         48,        1,   192000, 0x798a2f1f
+0,         49,         49,        1,   192000, 0x798a2f1f
+0,         50,         50,        1,   192000, 0x798a2f1f
+0,         51,         51,        1,   192000, 0x798a2f1f
+0,         52,         52,        1,   192000, 0x798a2f1f
+0,         53,         53,        1,   192000, 0xf04288c5
+0,         54,         54,        1,   192000, 0xf04288c5
+0,         55,         55,        1,   192000, 0xf04288c5
+0,         56,         56,        1,   192000, 0xf04288c5
+0,         57,         57,        1,   192000, 0x4aa04a04
+0,         58,         58,        1,   192000, 0x4aa04a04
+0,         59,         59,        1,   192000, 0x4aa04a04
+0,         60,         60,        1,   192000, 0x4aa04a04
+0,         61,         61,        1,   192000, 0x4aa04a04
+0,         62,         62,        1,   192000, 0xb0304d01
+0,         63,         63,        1,   192000, 0xb0304d01
+0,         64,         64,        1,   192000, 0xb0304d01
+0,         65,         65,        1,   192000, 0xb0304d01
+0,         66,         66,        1,   192000, 0xef353b13
+0,         67,         67,        1,   192000, 0xef353b13
+0,         68,         68,        1,   192000, 0xef353b13
+0,         69,         69,        1,   192000, 0xef353b13
+0,         70,         70,        1,   192000, 0xef353b13
+0,         71,         71,        1,   192000, 0xbf823519
+0,         72,         72,        1,   192000, 0xbf823519
+0,         73,         73,        1,   192000, 0xbf823519
+0,         74,         74,        1,   192000, 0xbf823519
+0,         75,         75,        1,   192000, 0xd0352c22
+0,         76,         76,        1,   192000, 0xd0352c22
+0,         77,         77,        1,   192000, 0xd0352c22
+0,         78,         78,        1,   192000, 0xd0352c22
+0,         79,         79,        1,   192000, 0xd0352c22
+0,         80,         80,        1,   192000, 0x817299a6
+0,         81,         81,        1,   192000, 0xfb1475ca
+0,         82,         82,        1,   192000, 0xfcd278c7
+0,         83,         83,        1,   192000, 0xa08742fd
+0,         84,         84,        1,   192000, 0x47c245fa
+0,         85,         85,        1,   192000, 0xf7e7b48b
+0,         86,         86,        1,   192000, 0xf56378c7
+0,         87,         87,        1,   192000, 0x80bc54eb
+0,         88,         88,        1,   192000, 0xae59221e
+0,         89,         89,        1,   192000, 0x7d0fc869
+0,         90,         90,        1,   192000, 0x8e89da57
+0,         91,         91,        1,   192000, 0xf0a9f53c
+0,         92,         92,        1,   192000, 0x658d013f
+0,         93,         93,        1,   192000, 0x90ef5ae5
+0,         94,         94,        1,   192000, 0x93b81f21
+0,         95,         95,        1,   192000, 0x61e545fa
+0,         96,         96,        1,   192000, 0xc6688ab5
+0,         97,         97,        1,   192000, 0x72032e12
+0,         98,         98,        1,   192000, 0xf28c3a06
+0,         99,         99,        1,   192000, 0xf1bc221e
+0,        100,        100,        1,   192000, 0x941bc869
+0,        101,        101,        1,   192000, 0xe95ead84
+0,        102,        102,        1,   192000, 0x61dbda57
+0,        103,        103,        1,   192000, 0x79800739
+0,        104,        104,        1,   192000, 0x00c31927
+0,        105,        105,        1,   192000, 0x74a3da57
+0,        106,        106,        1,   192000, 0xb98cf53c
+0,        107,        107,        1,   192000, 0xd3f9162a
+0,        108,        108,        1,   192000, 0x888c043c
+0,        109,        109,        1,   192000, 0x100f1f21
+0,        110,        110,        1,   192000, 0x41993d03
+0,        111,        111,        1,   192000, 0x01a548f7
+0,        112,        112,        1,   192000, 0xbe5372cd
+0,        113,        113,        1,   192000, 0x0fec4000
+0,        114,        114,        1,   192000, 0x80e9340c
+0,        115,        115,        1,   192000, 0xea582818
+0,        116,        116,        1,   192000, 0x5c3a2e12
+0,        117,        117,        1,   192000, 0x5c2b4ef1
+0,        118,        118,        1,   192000, 0xf2409fa0
+0,        119,        119,        1,   192000, 0x4bc472cd
+0,        120,        120,        1,   192000, 0x3dcb96a9
+0,        121,        121,        1,   192000, 0xff978ab5
+0,        122,        122,        1,   192000, 0xff72013f
+0,        123,        123,        1,   192000, 0x2f530d33
+0,        124,        124,        1,   192000, 0xa3350d33
+0,        125,        125,        1,   192000, 0x6be6251b
+0,        126,        126,        1,   192000, 0x07f081be
+0,        127,        127,        1,   192000, 0xb5d957e8
+0,        128,        128,        1,   192000, 0x43c551ee
+0,        129,        129,        1,   192000, 0x19224ef1
+0,        130,        130,        1,   192000, 0x0720e64b
+0,        131,        131,        1,   192000, 0x3c53f53c
+0,        132,        132,        1,   192000, 0x8534043c
+0,        133,        133,        1,   192000, 0xb7c7ef42
+0,        134,        134,        1,   192000, 0x9e3e0739
+0,        135,        135,        1,   192000, 0x95e43d03
+0,        136,        136,        1,   192000, 0x92473d03
+0,        137,        137,        1,   192000, 0x636757e8
+0,        138,        138,        1,   192000, 0xba3aec45
+0,        139,        139,        1,   192000, 0xfd9e013f
+0,        140,        140,        1,   192000, 0x108c1927
+0,        141,        141,        1,   192000, 0x29db1030
+0,        142,        142,        1,   192000, 0xe0172e12
+0,        143,        143,        1,   192000, 0x744260df
+0,        144,        144,        1,   192000, 0xc59478c7
+0,        145,        145,        1,   192000, 0xa84475ca
+0,        146,        146,        1,   192000, 0x9d2866d9
+0,        147,        147,        1,   192000, 0xc49342fd
+0,        148,        148,        1,   192000, 0x13e5bd82
+0,        149,        149,        1,   192000, 0x10d10549
+0,        150,        150,        1,   192000, 0xf5ca3b13
+0,        151,        151,        1,   192000, 0x8c27a9a4
+0,        152,        152,        1,   192000, 0xcc4f64e9
+0,        153,        153,        1,   192000, 0x020c67e6
+0,        154,        154,        1,   192000, 0xaf4773da
+0,        155,        155,        1,   192000, 0x62093e10
+0,        156,        156,        1,   192000, 0x96783519
+0,        157,        157,        1,   192000, 0x8b9a113d
+0,        158,        158,        1,   192000, 0x9050f946
diff --git a/tests/ref/fate/zmbv-16bit b/tests/ref/fate/zmbv-16bit
index b0c3df5..cb39014 100644
--- a/tests/ref/fate/zmbv-16bit
+++ b/tests/ref/fate/zmbv-16bit
@@ -1,123 +1,123 @@
 #tb 0: 250000/1585341
-0,          0,          0,        1,   192000, 0x11e62dbe
-0,          1,          1,        1,   192000, 0x31698b8f
-0,          2,          2,        1,   192000, 0x31698b8f
-0,          3,          3,        1,   192000, 0x31698b8f
-0,          4,          4,        1,   192000, 0x31698b8f
-0,          5,          5,        1,   192000, 0x31698b8f
-0,          6,          6,        1,   192000, 0x31698b8f
-0,          7,          7,        1,   192000, 0x31698b8f
-0,          8,          8,        1,   192000, 0x31698b8f
-0,          9,          9,        1,   192000, 0x31698b8f
-0,         10,         10,        1,   192000, 0x31698b8f
-0,         11,         11,        1,   192000, 0x31698b8f
-0,         12,         12,        1,   192000, 0x31698b8f
-0,         13,         13,        1,   192000, 0x4ca609ea
-0,         14,         14,        1,   192000, 0x4ca609ea
-0,         15,         15,        1,   192000, 0x4ca609ea
-0,         16,         16,        1,   192000, 0x4ca609ea
-0,         17,         17,        1,   192000, 0x33dd0a8b
-0,         18,         18,        1,   192000, 0x33dd0a8b
-0,         19,         19,        1,   192000, 0x33dd0a8b
-0,         20,         20,        1,   192000, 0x33dd0a8b
-0,         21,         21,        1,   192000, 0x33dd0a8b
-0,         22,         22,        1,   192000, 0x08e2420f
-0,         23,         23,        1,   192000, 0x08e2420f
-0,         24,         24,        1,   192000, 0x08e2420f
-0,         25,         25,        1,   192000, 0x08e2420f
-0,         26,         26,        1,   192000, 0x7b7b50ab
-0,         27,         27,        1,   192000, 0x7b7b50ab
-0,         28,         28,        1,   192000, 0x7b7b50ab
-0,         29,         29,        1,   192000, 0x7b7b50ab
-0,         30,         30,        1,   192000, 0x7b7b50ab
-0,         31,         31,        1,   192000, 0x128744fb
-0,         32,         32,        1,   192000, 0x128744fb
-0,         33,         33,        1,   192000, 0x128744fb
-0,         34,         34,        1,   192000, 0x128744fb
-0,         35,         35,        1,   192000, 0x8643163b
-0,         36,         36,        1,   192000, 0x8643163b
-0,         37,         37,        1,   192000, 0x8643163b
-0,         38,         38,        1,   192000, 0x8643163b
-0,         39,         39,        1,   192000, 0x8643163b
-0,         40,         40,        1,   192000, 0x4f7c596f
-0,         41,         41,        1,   192000, 0x4f7c596f
-0,         42,         42,        1,   192000, 0x4f7c596f
-0,         43,         43,        1,   192000, 0x4f7c596f
-0,         44,         44,        1,   192000, 0xa275420f
-0,         45,         45,        1,   192000, 0xa275420f
-0,         46,         46,        1,   192000, 0xa275420f
-0,         47,         47,        1,   192000, 0xa275420f
-0,         48,         48,        1,   192000, 0xa275420f
-0,         49,         49,        1,   192000, 0x2e4796cb
-0,         50,         50,        1,   192000, 0x2e4796cb
-0,         51,         51,        1,   192000, 0x2e4796cb
-0,         52,         52,        1,   192000, 0x2e4796cb
-0,         53,         53,        1,   192000, 0xebd45683
-0,         54,         54,        1,   192000, 0xebd45683
-0,         55,         55,        1,   192000, 0xebd45683
-0,         56,         56,        1,   192000, 0xebd45683
-0,         57,         57,        1,   192000, 0xebd45683
-0,         58,         58,        1,   192000, 0x0bf6596f
-0,         59,         59,        1,   192000, 0x0bf6596f
-0,         60,         60,        1,   192000, 0x0bf6596f
-0,         61,         61,        1,   192000, 0x0bf6596f
-0,         62,         62,        1,   192000, 0xb7af47e7
-0,         63,         63,        1,   192000, 0xb7af47e7
-0,         64,         64,        1,   192000, 0xb7af47e7
-0,         65,         65,        1,   192000, 0xb7af47e7
-0,         66,         66,        1,   192000, 0xb7af47e7
-0,         67,         67,        1,   192000, 0x8bc344fb
-0,         68,         68,        1,   192000, 0x8bc344fb
-0,         69,         69,        1,   192000, 0x8bc344fb
-0,         70,         70,        1,   192000, 0x8bc344fb
-0,         71,         71,        1,   192000, 0x8bc344fb
-0,         72,         72,        1,   192000, 0x02a23f23
-0,         73,         73,        1,   192000, 0x02a23f23
-0,         74,         74,        1,   192000, 0x02a23f23
-0,         75,         75,        1,   192000, 0x02a23f23
-0,         76,         76,        1,   192000, 0x17be3087
-0,         77,         77,        1,   192000, 0x52c7b2d4
-0,         78,         78,        1,   192000, 0xa05e9888
-0,         79,         79,        1,   192000, 0x4d6a6cb4
-0,         80,         80,        1,   192000, 0x3e2189ec
-0,         81,         81,        1,   192000, 0xfbe3ca34
-0,         82,         82,        1,   192000, 0xa9cd8fc4
-0,         83,         83,        1,   192000, 0x986e6cb4
-0,         84,         84,        1,   192000, 0x42373b08
-0,         85,         85,        1,   192000, 0xab96e351
-0,         86,         86,        1,   192000, 0x28ccf4d9
-0,         87,         87,        1,   192000, 0xc19e0f34
-0,         88,         88,        1,   192000, 0xd8da1ae4
-0,         89,         89,        1,   192000, 0xd015728c
-0,         90,         90,        1,   192000, 0x550623a8
-0,         91,         91,        1,   192000, 0x3c5c5268
-0,         92,         92,        1,   192000, 0x3eae8128
-0,         93,         93,        1,   192000, 0x3c51381c
-0,         94,         94,        1,   192000, 0x745046b8
-0,         95,         95,        1,   192000, 0xa8bd43cc
-0,         96,         96,        1,   192000, 0xde8fe351
-0,         97,         97,        1,   192000, 0x3d5cc905
-0,         98,         98,        1,   192000, 0xcf8df4d9
-0,         99,         99,        1,   192000, 0x698b20bc
-0,        100,        100,        1,   192000, 0x159d3244
-0,        101,        101,        1,   192000, 0xba1af4d9
-0,        102,        102,        1,   192000, 0x033a0f34
-0,        103,        103,        1,   192000, 0x72612f58
-0,        104,        104,        1,   192000, 0x11e11dd0
-0,        105,        105,        1,   192000, 0x4fc04f7c
-0,        106,        106,        1,   192000, 0x37779888
-0,        107,        107,        1,   192000, 0xabfc5e18
-0,        108,        108,        1,   192000, 0x0ad97e3c
-0,        109,        109,        1,   192000, 0xddf492b0
-0,        110,        110,        1,   192000, 0x198b23a8
-0,        111,        111,        1,   192000, 0x6b491220
-0,        112,        112,        1,   192000, 0x632417f8
-0,        113,        113,        1,   192000, 0x0dc5381c
-0,        114,        114,        1,   192000, 0x6d548700
-0,        115,        115,        1,   192000, 0xae0c5b2c
-0,        116,        116,        1,   192000, 0x23427e3c
-0,        117,        117,        1,   192000, 0x5def728c
-0,        118,        118,        1,   192000, 0xec831ae4
+0,          0,          0,        1,   192000, 0xe869dc94
+0,          1,          1,        1,   192000, 0x742932f2
+0,          2,          2,        1,   192000, 0x742932f2
+0,          3,          3,        1,   192000, 0x742932f2
+0,          4,          4,        1,   192000, 0x742932f2
+0,          5,          5,        1,   192000, 0x742932f2
+0,          6,          6,        1,   192000, 0x742932f2
+0,          7,          7,        1,   192000, 0x742932f2
+0,          8,          8,        1,   192000, 0x742932f2
+0,          9,          9,        1,   192000, 0x742932f2
+0,         10,         10,        1,   192000, 0x742932f2
+0,         11,         11,        1,   192000, 0x742932f2
+0,         12,         12,        1,   192000, 0x742932f2
+0,         13,         13,        1,   192000, 0xb718e72e
+0,         14,         14,        1,   192000, 0xb718e72e
+0,         15,         15,        1,   192000, 0xb718e72e
+0,         16,         16,        1,   192000, 0xb718e72e
+0,         17,         17,        1,   192000, 0x7507f946
+0,         18,         18,        1,   192000, 0x7507f946
+0,         19,         19,        1,   192000, 0x7507f946
+0,         20,         20,        1,   192000, 0x7507f946
+0,         21,         21,        1,   192000, 0x7507f946
+0,         22,         22,        1,   192000, 0xe620321c
+0,         23,         23,        1,   192000, 0xe620321c
+0,         24,         24,        1,   192000, 0xe620321c
+0,         25,         25,        1,   192000, 0xe620321c
+0,         26,         26,        1,   192000, 0x9b5f410d
+0,         27,         27,        1,   192000, 0x9b5f410d
+0,         28,         28,        1,   192000, 0x9b5f410d
+0,         29,         29,        1,   192000, 0x9b5f410d
+0,         30,         30,        1,   192000, 0x9b5f410d
+0,         31,         31,        1,   192000, 0xea2c3519
+0,         32,         32,        1,   192000, 0xea2c3519
+0,         33,         33,        1,   192000, 0xea2c3519
+0,         34,         34,        1,   192000, 0xea2c3519
+0,         35,         35,        1,   192000, 0x1acb0549
+0,         36,         36,        1,   192000, 0x1acb0549
+0,         37,         37,        1,   192000, 0x1acb0549
+0,         38,         38,        1,   192000, 0x1acb0549
+0,         39,         39,        1,   192000, 0x1acb0549
+0,         40,         40,        1,   192000, 0x3a064a04
+0,         41,         41,        1,   192000, 0x3a064a04
+0,         42,         42,        1,   192000, 0x3a064a04
+0,         43,         43,        1,   192000, 0x3a064a04
+0,         44,         44,        1,   192000, 0xcede321c
+0,         45,         45,        1,   192000, 0xcede321c
+0,         46,         46,        1,   192000, 0xcede321c
+0,         47,         47,        1,   192000, 0xcede321c
+0,         48,         48,        1,   192000, 0xcede321c
+0,         49,         49,        1,   192000, 0xef4988c5
+0,         50,         50,        1,   192000, 0xef4988c5
+0,         51,         51,        1,   192000, 0xef4988c5
+0,         52,         52,        1,   192000, 0xef4988c5
+0,         53,         53,        1,   192000, 0x1fc84707
+0,         54,         54,        1,   192000, 0x1fc84707
+0,         55,         55,        1,   192000, 0x1fc84707
+0,         56,         56,        1,   192000, 0x1fc84707
+0,         57,         57,        1,   192000, 0x1fc84707
+0,         58,         58,        1,   192000, 0xbabd4a04
+0,         59,         59,        1,   192000, 0xbabd4a04
+0,         60,         60,        1,   192000, 0xbabd4a04
+0,         61,         61,        1,   192000, 0xbabd4a04
+0,         62,         62,        1,   192000, 0x649b3816
+0,         63,         63,        1,   192000, 0x649b3816
+0,         64,         64,        1,   192000, 0x649b3816
+0,         65,         65,        1,   192000, 0x649b3816
+0,         66,         66,        1,   192000, 0x649b3816
+0,         67,         67,        1,   192000, 0x206b3519
+0,         68,         68,        1,   192000, 0x206b3519
+0,         69,         69,        1,   192000, 0x206b3519
+0,         70,         70,        1,   192000, 0x206b3519
+0,         71,         71,        1,   192000, 0x206b3519
+0,         72,         72,        1,   192000, 0x25982f1f
+0,         73,         73,        1,   192000, 0x25982f1f
+0,         74,         74,        1,   192000, 0x25982f1f
+0,         75,         75,        1,   192000, 0x25982f1f
+0,         76,         76,        1,   192000, 0xde0e202e
+0,         77,         77,        1,   192000, 0xced09fa0
+0,         78,         78,        1,   192000, 0x3b4f84bb
+0,         79,         79,        1,   192000, 0xa09c57e8
+0,         80,         80,        1,   192000, 0xf3dd75ca
+0,         81,         81,        1,   192000, 0x4d4ab788
+0,         82,         82,        1,   192000, 0x4ac67bc4
+0,         83,         83,        1,   192000, 0xd61057e8
+0,         84,         84,        1,   192000, 0x03bc251b
+0,         85,         85,        1,   192000, 0xd263cb66
+0,         86,         86,        1,   192000, 0xe3dddd54
+0,         87,         87,        1,   192000, 0x460cf839
+0,         88,         88,        1,   192000, 0xbae1043c
+0,         89,         89,        1,   192000, 0xe6435de2
+0,         90,         90,        1,   192000, 0x340e0d33
+0,         91,         91,        1,   192000, 0x9acd3d03
+0,         92,         92,        1,   192000, 0x1d346cd3
+0,         93,         93,        1,   192000, 0xdabe221e
+0,         94,         94,        1,   192000, 0xeb4b310f
+0,         95,         95,        1,   192000, 0xec8f2e12
+0,         96,         96,        1,   192000, 0xe96fcb66
+0,         97,         97,        1,   192000, 0x3ec1b081
+0,         98,         98,        1,   192000, 0xb72fdd54
+0,         99,         99,        1,   192000, 0xced40a36
+0,        100,        100,        1,   192000, 0x56171c24
+0,        101,        101,        1,   192000, 0xc9f7dd54
+0,        102,        102,        1,   192000, 0x0eeff839
+0,        103,        103,        1,   192000, 0x295c1927
+0,        104,        104,        1,   192000, 0xdde00739
+0,        105,        105,        1,   192000, 0x51903a06
+0,        106,        106,        1,   192000, 0xe16a84bb
+0,        107,        107,        1,   192000, 0xf5b248f7
+0,        108,        108,        1,   192000, 0x054769d6
+0,        109,        109,        1,   192000, 0x9d327ec1
+0,        110,        110,        1,   192000, 0x94470d33
+0,        111,        111,        1,   192000, 0x9671fb36
+0,        112,        112,        1,   192000, 0x0853013f
+0,        113,        113,        1,   192000, 0x0844221e
+0,        114,        114,        1,   192000, 0x9e5972cd
+0,        115,        115,        1,   192000, 0xf7ce45fa
+0,        116,        116,        1,   192000, 0xe9d569d6
+0,        117,        117,        1,   192000, 0xabb05de2
+0,        118,        118,        1,   192000, 0x54d5043c
 0,        119,        119,        1,   192000, 0x00000000
 0,        120,        120,        1,   192000, 0x00000000
 0,        121,        121,        1,   192000, 0x00000000
diff --git a/tests/ref/lavf-fate/mp3 b/tests/ref/lavf-fate/mp3
index 91e2b48..361314b 100644
--- a/tests/ref/lavf-fate/mp3
+++ b/tests/ref/lavf-fate/mp3
@@ -1,3 +1,3 @@
-40a4e41ae74ec8dacdf02402831a6a58 *./tests/data/lavf-fate/lavf.mp3
-97230 ./tests/data/lavf-fate/lavf.mp3
+7fcf80c2059b5c058a6cdd2e2f798b6c *./tests/data/lavf-fate/lavf.mp3
+96366 ./tests/data/lavf-fate/lavf.mp3
 ./tests/data/lavf-fate/lavf.mp3 CRC=0x6c9850fe
diff --git a/tests/ref/lavf/dpx b/tests/ref/lavf/dpx
index 5961e00..0206bd7 100644
--- a/tests/ref/lavf/dpx
+++ b/tests/ref/lavf/dpx
@@ -1,9 +1,9 @@
-808ea110635774252439722a48329d61 *./tests/data/images/dpx/02.dpx
+d2f0b4e854fda2d3b3bee84cef80593c *./tests/data/images/dpx/02.dpx
 ./tests/data/images/dpx/%02d.dpx CRC=0x6da01946
 305792 ./tests/data/images/dpx/02.dpx
-5e1a777fa3f4094c9c4dd989cf9e8e8b *./tests/data/images/dpx/02.dpx
+075963c3c08978b6a20555ba09161434 *./tests/data/images/dpx/02.dpx
 ./tests/data/images/dpx/%02d.dpx CRC=0xe5b9c023
 609920 ./tests/data/images/dpx/02.dpx
-13dc41b1e1e36399a5e1f8b7e3344a81 *./tests/data/images/dpx/02.dpx
+b9f22728f8ff393bf30cf6cbd624fa95 *./tests/data/images/dpx/02.dpx
 ./tests/data/images/dpx/%02d.dpx CRC=0xb6310a70
 407168 ./tests/data/images/dpx/02.dpx
diff --git a/tests/ref/lavf/gif b/tests/ref/lavf/gif
index 509ea24..531cd1e 100644
--- a/tests/ref/lavf/gif
+++ b/tests/ref/lavf/gif
@@ -1,24 +1,3 @@
-e6089fd4ef3b9df44090ab3650bdd810 *./tests/data/lavf/lavf.gif
-2906401 ./tests/data/lavf/lavf.gif
-./tests/data/lavf/lavf.gif CRC=0x9825d7c0
-022dc66b5068404e88c618ce79d9eb5f *./tests/data/images/gif/02.gif
-./tests/data/images/gif/%02d.gif CRC=0x032e0034
-81538 ./tests/data/images/gif/02.gif
-759522b3025fcf8ed6aae582a18c5a14 *./tests/data/images/gif/02.gif
-./tests/data/images/gif/%02d.gif CRC=0x4c8f8a89
-38715 ./tests/data/images/gif/02.gif
-21e802ae7a2239bdbea6f915da1134b9 *./tests/data/images/gif/02.gif
-./tests/data/images/gif/%02d.gif CRC=0x4c8f8a89
-38715 ./tests/data/images/gif/02.gif
-fc4792ac40319344dc7027668a403fc3 *./tests/data/images/gif/02.gif
-./tests/data/images/gif/%02d.gif CRC=0x032e0034
-81538 ./tests/data/images/gif/02.gif
-022dc66b5068404e88c618ce79d9eb5f *./tests/data/images/gif/02.gif
-./tests/data/images/gif/%02d.gif CRC=0x032e0034
-81538 ./tests/data/images/gif/02.gif
-e3392f49c55aa794d3dc49189f52f257 *./tests/data/images/gif/02.gif
-./tests/data/images/gif/%02d.gif CRC=0x22d67c27
-63144 ./tests/data/images/gif/02.gif
-022dc66b5068404e88c618ce79d9eb5f *./tests/data/images/gif/02.gif
-./tests/data/images/gif/%02d.gif CRC=0x032e0034
-81538 ./tests/data/images/gif/02.gif
+e35f5ea283bbcb249818e0078ec72664 *./tests/data/lavf/lavf.gif
+2011766 ./tests/data/lavf/lavf.gif
+./tests/data/lavf/lavf.gif CRC=0x0dc5477c
diff --git a/tests/ref/lavfi/overlay b/tests/ref/lavfi/overlay
deleted file mode 100644
index ca70370..0000000
--- a/tests/ref/lavfi/overlay
+++ /dev/null
@@ -1 +0,0 @@
-overlay             4e85d551db9e56e8faddc1fc70fd6a00
diff --git a/tests/ref/lavfi/overlay_rgb b/tests/ref/lavfi/overlay_rgb
new file mode 100644
index 0000000..ce4e30b
--- /dev/null
+++ b/tests/ref/lavfi/overlay_rgb
@@ -0,0 +1 @@
+overlay_rgb         abea47d9e61d7eab40c4734294519524
diff --git a/tests/ref/lavfi/overlay_yuv420 b/tests/ref/lavfi/overlay_yuv420
new file mode 100644
index 0000000..3d12251
--- /dev/null
+++ b/tests/ref/lavfi/overlay_yuv420
@@ -0,0 +1 @@
+overlay_yuv420      4e85d551db9e56e8faddc1fc70fd6a00
diff --git a/tests/ref/lavfi/overlay_yuv444 b/tests/ref/lavfi/overlay_yuv444
new file mode 100644
index 0000000..b83e605
--- /dev/null
+++ b/tests/ref/lavfi/overlay_yuv444
@@ -0,0 +1 @@
+overlay_yuv444      16215d0b4054a44bbe7f6c46bd97d235
diff --git a/tests/ref/seek/lavf-gif b/tests/ref/seek/lavf-gif
index 0a2d1b3..9ce8308 100644
--- a/tests/ref/seek/lavf-gif
+++ b/tests/ref/seek/lavf-gif
@@ -1,40 +1,40 @@
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size:117024
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size: 81501
 ret: 0         st:-1 flags:0  ts:-1.000000
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size:117024
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size: 81501
 ret: 0         st:-1 flags:1  ts: 1.894167
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size:117024
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size: 81501
 ret:-1         st: 0 flags:0  ts: 0.790000
 ret:-1         st: 0 flags:1  ts:-0.320000
 ret:-1         st:-1 flags:0  ts: 2.576668
 ret: 0         st:-1 flags:1  ts: 1.470835
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size:117024
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size: 81501
 ret:-1         st: 0 flags:0  ts: 0.370000
 ret:-1         st: 0 flags:1  ts:-0.740000
 ret:-1         st:-1 flags:0  ts: 2.153336
 ret: 0         st:-1 flags:1  ts: 1.047503
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size:117024
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size: 81501
 ret: 0         st: 0 flags:0  ts:-0.060000
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size:117024
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size: 81501
 ret: 0         st: 0 flags:1  ts: 2.840000
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size:117024
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size: 81501
 ret:-1         st:-1 flags:0  ts: 1.730004
 ret: 0         st:-1 flags:1  ts: 0.624171
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size:117024
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size: 81501
 ret: 0         st: 0 flags:0  ts:-0.480000
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size:117024
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size: 81501
 ret: 0         st: 0 flags:1  ts: 2.410000
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size:117024
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size: 81501
 ret:-1         st:-1 flags:0  ts: 1.306672
 ret: 0         st:-1 flags:1  ts: 0.200839
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size:117024
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size: 81501
 ret: 0         st: 0 flags:0  ts:-0.900000
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size:117024
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size: 81501
 ret: 0         st: 0 flags:1  ts: 1.990000
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size:117024
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size: 81501
 ret:-1         st:-1 flags:0  ts: 0.883340
 ret:-1         st:-1 flags:1  ts:-0.222493
 ret:-1         st: 0 flags:0  ts: 2.670000
 ret: 0         st: 0 flags:1  ts: 1.570000
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size:117024
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:      0 size: 81501
 ret:-1         st:-1 flags:0  ts: 0.460008
 ret:-1         st:-1 flags:1  ts:-0.645825
diff --git a/tests/ref/seek/vsynth2-ffv1 b/tests/ref/seek/vsynth2-ffv1
index a1272f8..715a272 100644
--- a/tests/ref/seek/vsynth2-ffv1
+++ b/tests/ref/seek/vsynth2-ffv1
@@ -1,46 +1,46 @@
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:   5648 size: 70200
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:   5688 size: 71679
 ret: 0         st:-1 flags:0  ts:-1.000000
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:   5648 size: 70200
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:   5688 size: 71679
 ret: 0         st:-1 flags:1  ts: 1.894167
-ret: 0         st: 0 flags:1 dts: 1.440000 pts: 1.440000 pos:2485798 size: 74128
+ret: 0         st: 0 flags:1 dts: 1.440000 pts: 1.440000 pos:2498334 size: 75937
 ret: 0         st: 0 flags:0  ts: 0.800000
-ret: 0         st: 0 flags:1 dts: 0.960000 pts: 0.960000 pos:1622704 size: 71128
+ret: 0         st: 0 flags:1 dts: 0.960000 pts: 0.960000 pos:1630830 size: 72682
 ret:-1         st: 0 flags:1  ts:-0.320000
 ret:-1         st:-1 flags:0  ts: 2.576668
 ret: 0         st:-1 flags:1  ts: 1.470835
-ret: 0         st: 0 flags:1 dts: 1.440000 pts: 1.440000 pos:2485798 size: 74128
+ret: 0         st: 0 flags:1 dts: 1.440000 pts: 1.440000 pos:2498334 size: 75937
 ret: 0         st: 0 flags:0  ts: 0.360000
-ret: 0         st: 0 flags:1 dts: 0.480000 pts: 0.480000 pos: 796828 size: 67971
+ret: 0         st: 0 flags:1 dts: 0.480000 pts: 0.480000 pos: 801262 size: 69457
 ret:-1         st: 0 flags:1  ts:-0.760000
 ret:-1         st:-1 flags:0  ts: 2.153336
 ret: 0         st:-1 flags:1  ts: 1.047503
-ret: 0         st: 0 flags:1 dts: 0.960000 pts: 0.960000 pos:1622704 size: 71128
+ret: 0         st: 0 flags:1 dts: 0.960000 pts: 0.960000 pos:1630830 size: 72682
 ret: 0         st: 0 flags:0  ts:-0.040000
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:   5648 size: 70200
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:   5688 size: 71679
 ret: 0         st: 0 flags:1  ts: 2.840000
-ret: 0         st: 0 flags:1 dts: 1.920000 pts: 1.920000 pos:3374428 size: 75606
+ret: 0         st: 0 flags:1 dts: 1.920000 pts: 1.920000 pos:3392418 size: 77461
 ret: 0         st:-1 flags:0  ts: 1.730004
-ret: 0         st: 0 flags:1 dts: 1.920000 pts: 1.920000 pos:3374428 size: 75606
+ret: 0         st: 0 flags:1 dts: 1.920000 pts: 1.920000 pos:3392418 size: 77461
 ret: 0         st:-1 flags:1  ts: 0.624171
-ret: 0         st: 0 flags:1 dts: 0.480000 pts: 0.480000 pos: 796828 size: 67971
+ret: 0         st: 0 flags:1 dts: 0.480000 pts: 0.480000 pos: 801262 size: 69457
 ret: 0         st: 0 flags:0  ts:-0.480000
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:   5648 size: 70200
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:   5688 size: 71679
 ret: 0         st: 0 flags:1  ts: 2.400000
-ret: 0         st: 0 flags:1 dts: 1.920000 pts: 1.920000 pos:3374428 size: 75606
+ret: 0         st: 0 flags:1 dts: 1.920000 pts: 1.920000 pos:3392418 size: 77461
 ret: 0         st:-1 flags:0  ts: 1.306672
-ret: 0         st: 0 flags:1 dts: 1.440000 pts: 1.440000 pos:2485798 size: 74128
+ret: 0         st: 0 flags:1 dts: 1.440000 pts: 1.440000 pos:2498334 size: 75937
 ret: 0         st:-1 flags:1  ts: 0.200839
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:   5648 size: 70200
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:   5688 size: 71679
 ret: 0         st: 0 flags:0  ts:-0.920000
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:   5648 size: 70200
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:   5688 size: 71679
 ret: 0         st: 0 flags:1  ts: 2.000000
-ret: 0         st: 0 flags:1 dts: 1.920000 pts: 1.920000 pos:3374428 size: 75606
+ret: 0         st: 0 flags:1 dts: 1.920000 pts: 1.920000 pos:3392418 size: 77461
 ret: 0         st:-1 flags:0  ts: 0.883340
-ret: 0         st: 0 flags:1 dts: 0.960000 pts: 0.960000 pos:1622704 size: 71128
+ret: 0         st: 0 flags:1 dts: 0.960000 pts: 0.960000 pos:1630830 size: 72682
 ret:-1         st:-1 flags:1  ts:-0.222493
 ret:-1         st: 0 flags:0  ts: 2.680000
 ret: 0         st: 0 flags:1  ts: 1.560000
-ret: 0         st: 0 flags:1 dts: 1.440000 pts: 1.440000 pos:2485798 size: 74128
+ret: 0         st: 0 flags:1 dts: 1.440000 pts: 1.440000 pos:2498334 size: 75937
 ret: 0         st:-1 flags:0  ts: 0.460008
-ret: 0         st: 0 flags:1 dts: 0.480000 pts: 0.480000 pos: 796828 size: 67971
+ret: 0         st: 0 flags:1 dts: 0.480000 pts: 0.480000 pos: 801262 size: 69457
 ret:-1         st:-1 flags:1  ts:-0.645825
diff --git a/tests/ref/vsynth/vsynth1-ffv1 b/tests/ref/vsynth/vsynth1-ffv1
index 99787ed..a162a77 100644
--- a/tests/ref/vsynth/vsynth1-ffv1
+++ b/tests/ref/vsynth/vsynth1-ffv1
@@ -1,4 +1,4 @@
-91c237f18bc19975077c85175daed734 *tests/data/fate/vsynth1-ffv1.avi
-2655364 tests/data/fate/vsynth1-ffv1.avi
+d236e8441d7e04e24d882bb97faccd36 *tests/data/fate/vsynth1-ffv1.avi
+2689718 tests/data/fate/vsynth1-ffv1.avi
 c5ccac874dbf808e9088bc3107860042 *tests/data/fate/vsynth1-ffv1.out.rawvideo
 stddev:    0.00 PSNR:999.99 MAXDIFF:    0 bytes:  7603200/  7603200
diff --git a/tests/ref/vsynth/vsynth1-prores_kostya b/tests/ref/vsynth/vsynth1-prores_kostya
deleted file mode 100644
index ab6ecff..0000000
--- a/tests/ref/vsynth/vsynth1-prores_kostya
+++ /dev/null
@@ -1,4 +0,0 @@
-36411e06db3f86ddedd6171a56784d6b *tests/data/fate/vsynth1-prores_kostya.mov
-3858911 tests/data/fate/vsynth1-prores_kostya.mov
-0a4153637d0cc0a88a8bcbf04cfaf8c6 *tests/data/fate/vsynth1-prores_kostya.out.rawvideo
-stddev:    3.17 PSNR: 38.09 MAXDIFF:   39 bytes:  7603200/  7603200
diff --git a/tests/ref/vsynth/vsynth1-prores_ks b/tests/ref/vsynth/vsynth1-prores_ks
new file mode 100644
index 0000000..4e33ec7
--- /dev/null
+++ b/tests/ref/vsynth/vsynth1-prores_ks
@@ -0,0 +1,4 @@
+2f760d0e579bbbe24a922e9cbcf3294c *tests/data/fate/vsynth1-prores_ks.mov
+3858911 tests/data/fate/vsynth1-prores_ks.mov
+0a4153637d0cc0a88a8bcbf04cfaf8c6 *tests/data/fate/vsynth1-prores_ks.out.rawvideo
+stddev:    3.17 PSNR: 38.09 MAXDIFF:   39 bytes:  7603200/  7603200
diff --git a/tests/ref/vsynth/vsynth2-ffv1 b/tests/ref/vsynth/vsynth2-ffv1
index 888e843..8263b01 100644
--- a/tests/ref/vsynth/vsynth2-ffv1
+++ b/tests/ref/vsynth/vsynth2-ffv1
@@ -1,4 +1,4 @@
-3a757276e299bf88c30e06dfb53f1c99 *tests/data/fate/vsynth2-ffv1.avi
-3525792 tests/data/fate/vsynth2-ffv1.avi
+9d8486fc8a260204d8ee3212d95915b5 *tests/data/fate/vsynth2-ffv1.avi
+3546258 tests/data/fate/vsynth2-ffv1.avi
 dde5895817ad9d219f79a52d0bdfb001 *tests/data/fate/vsynth2-ffv1.out.rawvideo
 stddev:    0.00 PSNR:999.99 MAXDIFF:    0 bytes:  7603200/  7603200
diff --git a/tests/ref/vsynth/vsynth2-prores_kostya b/tests/ref/vsynth/vsynth2-prores_kostya
deleted file mode 100644
index 16b6078..0000000
--- a/tests/ref/vsynth/vsynth2-prores_kostya
+++ /dev/null
@@ -1,4 +0,0 @@
-7de64c634620de738c98aa22e9df927d *tests/data/fate/vsynth2-prores_kostya.mov
-3884596 tests/data/fate/vsynth2-prores_kostya.mov
-ca2f6c1162635dedfa468c90f1fdc0ef *tests/data/fate/vsynth2-prores_kostya.out.rawvideo
-stddev:    0.92 PSNR: 48.77 MAXDIFF:   10 bytes:  7603200/  7603200
diff --git a/tests/ref/vsynth/vsynth2-prores_ks b/tests/ref/vsynth/vsynth2-prores_ks
new file mode 100644
index 0000000..b9902db
--- /dev/null
+++ b/tests/ref/vsynth/vsynth2-prores_ks
@@ -0,0 +1,4 @@
+12c24e7305e131388d5beb2a980293f4 *tests/data/fate/vsynth2-prores_ks.mov
+3884596 tests/data/fate/vsynth2-prores_ks.mov
+ca2f6c1162635dedfa468c90f1fdc0ef *tests/data/fate/vsynth2-prores_ks.out.rawvideo
+stddev:    0.92 PSNR: 48.77 MAXDIFF:   10 bytes:  7603200/  7603200
diff --git a/tests/tiny_ssim.c b/tests/tiny_ssim.c
new file mode 100644
index 0000000..759a84b
--- /dev/null
+++ b/tests/tiny_ssim.c
@@ -0,0 +1,216 @@
+/*
+ * Copyright (c) 2003-2013 Loren Merritt
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA
+ */
+/*
+ * tiny_ssim.c
+ * Computes the Structural Similarity Metric between two rawYV12 video files.
+ * original algorithm:
+ * Z. Wang, A. C. Bovik, H. R. Sheikh and E. P. Simoncelli,
+ *   "Image quality assessment: From error visibility to structural similarity,"
+ *   IEEE Transactions on Image Processing, vol. 13, no. 4, pp. 600-612, Apr. 2004.
+ *
+ * To improve speed, this implementation uses the standard approximation of
+ * overlapped 8x8 block sums, rather than the original gaussian weights.
+ */
+
+#include <inttypes.h>
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
+#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
+
+#define BIT_DEPTH 8
+#define PIXEL_MAX ((1 << BIT_DEPTH)-1)
+typedef uint8_t  pixel;
+
+/****************************************************************************
+ * structural similarity metric
+ ****************************************************************************/
+static void ssim_4x4x2_core( const pixel *pix1, intptr_t stride1,
+                             const pixel *pix2, intptr_t stride2,
+                             int sums[2][4] )
+{
+    int x,y,z;
+
+    for( z = 0; z < 2; z++ )
+    {
+        uint32_t s1 = 0, s2 = 0, ss = 0, s12 = 0;
+        for( y = 0; y < 4; y++ )
+            for( x = 0; x < 4; x++ )
+            {
+                int a = pix1[x+y*stride1];
+                int b = pix2[x+y*stride2];
+                s1  += a;
+                s2  += b;
+                ss  += a*a;
+                ss  += b*b;
+                s12 += a*b;
+            }
+        sums[z][0] = s1;
+        sums[z][1] = s2;
+        sums[z][2] = ss;
+        sums[z][3] = s12;
+        pix1 += 4;
+        pix2 += 4;
+    }
+}
+
+static float ssim_end1( int s1, int s2, int ss, int s12 )
+{
+/* Maximum value for 10-bit is: ss*64 = (2^10-1)^2*16*4*64 = 4286582784, which will overflow in some cases.
+ * s1*s1, s2*s2, and s1*s2 also obtain this value for edge cases: ((2^10-1)*16*4)^2 = 4286582784.
+ * Maximum value for 9-bit is: ss*64 = (2^9-1)^2*16*4*64 = 1069551616, which will not overflow. */
+#if BIT_DEPTH > 9
+#define type float
+    static const float ssim_c1 = .01*.01*PIXEL_MAX*PIXEL_MAX*64;
+    static const float ssim_c2 = .03*.03*PIXEL_MAX*PIXEL_MAX*64*63;
+#else
+#define type int
+    static const int ssim_c1 = (int)(.01*.01*PIXEL_MAX*PIXEL_MAX*64 + .5);
+    static const int ssim_c2 = (int)(.03*.03*PIXEL_MAX*PIXEL_MAX*64*63 + .5);
+#endif
+    type fs1 = s1;
+    type fs2 = s2;
+    type fss = ss;
+    type fs12 = s12;
+    type vars = fss*64 - fs1*fs1 - fs2*fs2;
+    type covar = fs12*64 - fs1*fs2;
+    return (float)(2*fs1*fs2 + ssim_c1) * (float)(2*covar + ssim_c2)
+         / ((float)(fs1*fs1 + fs2*fs2 + ssim_c1) * (float)(vars + ssim_c2));
+#undef type
+}
+
+static float ssim_end4( int sum0[5][4], int sum1[5][4], int width )
+{
+    float ssim = 0.0;
+    int i;
+
+    for( i = 0; i < width; i++ )
+        ssim += ssim_end1( sum0[i][0] + sum0[i+1][0] + sum1[i][0] + sum1[i+1][0],
+                           sum0[i][1] + sum0[i+1][1] + sum1[i][1] + sum1[i+1][1],
+                           sum0[i][2] + sum0[i+1][2] + sum1[i][2] + sum1[i+1][2],
+                           sum0[i][3] + sum0[i+1][3] + sum1[i][3] + sum1[i+1][3] );
+    return ssim;
+}
+
+float ssim_plane(
+                           pixel *pix1, intptr_t stride1,
+                           pixel *pix2, intptr_t stride2,
+                           int width, int height, void *buf, int *cnt )
+{
+    int z = 0;
+    int x, y;
+    float ssim = 0.0;
+    int (*sum0)[4] = buf;
+    int (*sum1)[4] = sum0 + (width >> 2) + 3;
+    width >>= 2;
+    height >>= 2;
+    for( y = 1; y < height; y++ )
+    {
+        for( ; z <= y; z++ )
+        {
+            FFSWAP( void*, sum0, sum1 );
+            for( x = 0; x < width; x+=2 )
+                ssim_4x4x2_core( &pix1[4*(x+z*stride1)], stride1, &pix2[4*(x+z*stride2)], stride2, &sum0[x] );
+        }
+        for( x = 0; x < width-1; x += 4 )
+            ssim += ssim_end4( sum0+x, sum1+x, FFMIN(4,width-x-1) );
+    }
+//     *cnt = (height-1) * (width-1);
+    return ssim / ((height-1) * (width-1));
+}
+
+
+uint64_t ssd_plane( const uint8_t *pix1, const uint8_t *pix2, int size )
+{
+    uint64_t ssd = 0;
+    int i;
+    for( i=0; i<size; i++ )
+    {
+        int d = pix1[i] - pix2[i];
+        ssd += d*d;
+    }
+    return ssd;
+}
+
+double ssd_to_psnr( uint64_t ssd, uint64_t denom )
+{
+    return -10*log((double)ssd/(denom*255*255))/log(10);
+}
+
+int main(int argc, char* argv[])
+{
+    FILE *f[2];
+    uint8_t *buf[2], *plane[2][3];
+    int *temp;
+    uint64_t ssd[3] = {0,0,0};
+    double ssim[3] = {0,0,0};
+    int frame_size, w, h;
+    int frames, seek;
+    int i;
+
+    if( argc<4 || 2 != sscanf(argv[3], "%dx%d", &w, &h) )
+    {
+        printf("tiny_ssim <file1.yuv> <file2.yuv> <width>x<height> [<seek>]\n");
+        return -1;
+    }
+
+    f[0] = fopen(argv[1], "rb");
+    f[1] = fopen(argv[2], "rb");
+    sscanf(argv[3], "%dx%d", &w, &h);
+    frame_size = w*h*3/2;
+    for( i=0; i<2; i++ )
+    {
+        buf[i] = malloc(frame_size);
+        plane[i][0] = buf[i];
+        plane[i][1] = plane[i][0] + w*h;
+        plane[i][2] = plane[i][1] + w*h/4;
+    }
+    temp = malloc((2*w+12)*sizeof(*temp));
+    seek = argc<5 ? 0 : atoi(argv[4]);
+    fseek(f[seek<0], seek < 0 ? -seek : seek, SEEK_SET);
+
+    for( frames=0;; frames++ )
+    {
+        if( fread(buf[0], frame_size, 1, f[0]) != 1) break;
+        if( fread(buf[1], frame_size, 1, f[1]) != 1) break;
+        for( i=0; i<3; i++ )
+        {
+            ssd[i]  += ssd_plane ( plane[0][i], plane[1][i], w*h>>2*!!i );
+            ssim[i] += ssim_plane( plane[0][i], w>>!!i,
+                                   plane[1][i], w>>!!i,
+                                   w>>!!i, h>>!!i, temp, NULL );
+        }
+    }
+
+    if( !frames ) return 0;
+
+    printf( "PSNR Y:%.3f  U:%.3f  V:%.3f  All:%.3f\n",
+            ssd_to_psnr( ssd[0], (uint64_t)frames*w*h ),
+            ssd_to_psnr( ssd[1], (uint64_t)frames*w*h/4 ),
+            ssd_to_psnr( ssd[2], (uint64_t)frames*w*h/4 ),
+            ssd_to_psnr( ssd[0] + ssd[1] + ssd[2], (uint64_t)frames*w*h*3/2 ) );
+    printf( "SSIM Y:%.5f U:%.5f V:%.5f All:%.5f\n",
+            ssim[0] / frames,
+            ssim[1] / frames,
+            ssim[2] / frames,
+            (ssim[0]*4 + ssim[1] + ssim[2]) / (frames*6) );
+
+    return 0;
+}
diff --git a/tools/ffescape.c b/tools/ffescape.c
index d777fe4..0530d28 100644
--- a/tools/ffescape.c
+++ b/tools/ffescape.c
@@ -42,80 +42,16 @@
     printf("\n"
            "Options:\n"
            "-e                echo each input line on output\n"
+           "-f flag           select an escape flag, can assume the values 'whitespace' and 'strict'\n"
            "-h                print this help\n"
            "-i INFILE         set INFILE as input file, stdin if omitted\n"
            "-l LEVEL          set the number of escaping levels, 1 if omitted\n"
-           "-m ESCAPE_MODE    select escape mode between 'full', 'lazy', 'quote', default is 'lazy'\n"
+           "-m ESCAPE_MODE    select escape mode between 'auto', 'backslash', 'quote'\n"
            "-o OUTFILE        set OUTFILE as output file, stdout if omitted\n"
            "-p PROMPT         set output prompt, is '=> ' by default\n"
            "-s SPECIAL_CHARS  set the list of special characters\n");
 }
 
-#define WHITESPACES " \n\t"
-
-enum EscapeMode {
-    ESCAPE_MODE_FULL,
-    ESCAPE_MODE_LAZY,
-    ESCAPE_MODE_QUOTE,
-};
-
-static int escape(char **dst, const char *src, const char *special_chars,
-                  enum EscapeMode mode)
-{
-    AVBPrint dstbuf;
-
-    av_bprint_init(&dstbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
-
-    switch (mode) {
-    case ESCAPE_MODE_FULL:
-    case ESCAPE_MODE_LAZY:
-        /* \-escape characters */
-
-        if (mode == ESCAPE_MODE_LAZY && strchr(WHITESPACES, *src))
-            av_bprintf(&dstbuf, "\\%c", *src++);
-
-        for (; *src; src++) {
-            if ((special_chars && strchr(special_chars, *src)) ||
-                strchr("'\\", *src) ||
-                (mode == ESCAPE_MODE_FULL && strchr(WHITESPACES, *src)))
-                av_bprintf(&dstbuf, "\\%c", *src);
-            else
-                av_bprint_chars(&dstbuf, *src, 1);
-        }
-
-        if (mode == ESCAPE_MODE_LAZY && strchr(WHITESPACES, dstbuf.str[dstbuf.len-1])) {
-            char c = dstbuf.str[dstbuf.len-1];
-            dstbuf.str[dstbuf.len-1] = '\\';
-            av_bprint_chars(&dstbuf, c, 1);
-        }
-        break;
-
-    case ESCAPE_MODE_QUOTE:
-        /* enclose between '' the string */
-        av_bprint_chars(&dstbuf, '\'', 1);
-        for (; *src; src++) {
-            if (*src == '\'')
-                av_bprintf(&dstbuf, "'\\''");
-            else
-                av_bprint_chars(&dstbuf, *src, 1);
-        }
-        av_bprint_chars(&dstbuf, '\'', 1);
-        break;
-
-    default:
-        /* unknown escape mode */
-        return AVERROR(EINVAL);
-    }
-
-    if (!av_bprint_is_complete(&dstbuf)) {
-        av_bprint_finalize(&dstbuf, NULL);
-        return AVERROR(ENOMEM);
-    } else {
-        av_bprint_finalize(&dstbuf, dst);
-        return 0;
-    }
-}
-
 int main(int argc, char **argv)
 {
     AVBPrint src;
@@ -123,13 +59,14 @@
     const char *outfilename = NULL, *infilename = NULL;
     FILE *outfile = NULL, *infile = NULL;
     const char *prompt = "=> ";
-    enum EscapeMode escape_mode = ESCAPE_MODE_LAZY;
+    enum AVEscapeMode escape_mode = AV_ESCAPE_MODE_AUTO;
+    int escape_flags = 0;
     int level = 1;
     int echo = 0;
     char *special_chars = NULL;
     int c;
 
-    while ((c = getopt(argc, argv, "ehi:l:o:m:p:s:")) != -1) {
+    while ((c = getopt(argc, argv, "ef:hi:l:o:m:p:s:")) != -1) {
         switch (c) {
         case 'e':
             echo = 1;
@@ -140,6 +77,16 @@
         case 'i':
             infilename = optarg;
             break;
+        case 'f':
+            if      (!strcmp(optarg, "whitespace")) escape_flags |= AV_ESCAPE_FLAG_WHITESPACE;
+            else if (!strcmp(optarg, "strict"))     escape_flags |= AV_ESCAPE_FLAG_STRICT;
+            else {
+                av_log(NULL, AV_LOG_ERROR,
+                       "Invalid value '%s' for option -f, "
+                       "valid arguments are 'whitespace', and 'strict'\n", optarg);
+                return 1;
+            }
+            break;
         case 'l':
         {
             char *tail;
@@ -154,13 +101,13 @@
             break;
         }
         case 'm':
-            if      (!strcmp(optarg, "full"))  escape_mode = ESCAPE_MODE_FULL;
-            else if (!strcmp(optarg, "lazy"))  escape_mode = ESCAPE_MODE_LAZY;
-            else if (!strcmp(optarg, "quote")) escape_mode = ESCAPE_MODE_QUOTE;
+            if      (!strcmp(optarg, "auto"))      escape_mode = AV_ESCAPE_MODE_AUTO;
+            else if (!strcmp(optarg, "backslash")) escape_mode = AV_ESCAPE_MODE_BACKSLASH;
+            else if (!strcmp(optarg, "quote"))     escape_mode = AV_ESCAPE_MODE_QUOTE;
             else {
                 av_log(NULL, AV_LOG_ERROR,
                        "Invalid value '%s' for option -m, "
-                       "valid arguments are 'full', 'lazy', 'quote'\n", optarg);
+                       "valid arguments are 'backslash', and 'quote'\n", optarg);
                 return 1;
             }
             break;
@@ -219,7 +166,7 @@
     /* escape */
     dst_buf = src_buf;
     while (level--) {
-        if (escape(&dst_buf, src_buf, special_chars, escape_mode) < 0) {
+        if (av_escape(&dst_buf, src_buf, special_chars, escape_mode, escape_flags) < 0) {
             av_log(NULL, AV_LOG_ERROR, "Could not escape string\n");
             return 1;
         }
diff --git a/tools/graph2dot.c b/tools/graph2dot.c
index 74075c7..167c49b 100644
--- a/tools/graph2dot.c
+++ b/tools/graph2dot.c
@@ -28,7 +28,7 @@
 #include "libavutil/channel_layout.h"
 #include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
-#include "libavfilter/avfiltergraph.h"
+#include "libavfilter/avfilter.h"
 
 #if !HAVE_GETOPT
 #include "compat/getopt.c"
@@ -58,7 +58,7 @@
     fprintf(outfile, "node [shape=box]\n");
     fprintf(outfile, "rankdir=LR\n");
 
-    for (i = 0; i < graph->filter_count; i++) {
+    for (i = 0; i < graph->nb_filters; i++) {
         char filter_ctx_label[128];
         const AVFilterContext *filter_ctx = graph->filters[i];
 
diff --git a/tools/ismindex.c b/tools/ismindex.c
index 803c5b7..67a1927 100644
--- a/tools/ismindex.c
+++ b/tools/ismindex.c
@@ -58,7 +58,7 @@
     int duration;
 };
 
-struct VideoFile {
+struct Track {
     const char *name;
     int64_t duration;
     int bitrate;
@@ -76,12 +76,12 @@
     int tag;
 };
 
-struct VideoFiles {
-    int nb_files;
+struct Tracks {
+    int nb_tracks;
     int64_t duration;
-    struct VideoFile **files;
-    int video_file, audio_file;
-    int nb_video_files, nb_audio_files;
+    struct Track **tracks;
+    int video_track, audio_track;
+    int nb_video_tracks, nb_audio_tracks;
 };
 
 static int copy_tag(AVIOContext *in, AVIOContext *out, int32_t tag_name)
@@ -122,62 +122,63 @@
     return ret;
 }
 
-static int write_fragments(struct VideoFiles *files, int start_index,
+static int write_fragments(struct Tracks *tracks, int start_index,
                            AVIOContext *in)
 {
     char dirname[100], filename[500];
     int i, j;
 
-    for (i = start_index; i < files->nb_files; i++) {
-        struct VideoFile *vf = files->files[i];
-        const char *type     = vf->is_video ? "video" : "audio";
-        snprintf(dirname, sizeof(dirname), "QualityLevels(%d)", vf->bitrate);
-        mkdir(dirname, 0777);
-        for (j = 0; j < vf->chunks; j++) {
+    for (i = start_index; i < tracks->nb_tracks; i++) {
+        struct Track *track = tracks->tracks[i];
+        const char *type    = track->is_video ? "video" : "audio";
+        snprintf(dirname, sizeof(dirname), "QualityLevels(%d)", track->bitrate);
+        if (mkdir(dirname, 0777) == -1)
+            return AVERROR(errno);
+        for (j = 0; j < track->chunks; j++) {
             snprintf(filename, sizeof(filename), "%s/Fragments(%s=%"PRId64")",
-                     dirname, type, vf->offsets[j].time);
-            avio_seek(in, vf->offsets[j].offset, SEEK_SET);
+                     dirname, type, track->offsets[j].time);
+            avio_seek(in, track->offsets[j].offset, SEEK_SET);
             write_fragment(filename, in);
         }
     }
     return 0;
 }
 
-static int read_tfra(struct VideoFiles *files, int start_index, AVIOContext *f)
+static int read_tfra(struct Tracks *tracks, int start_index, AVIOContext *f)
 {
     int ret = AVERROR_EOF, track_id;
     int version, fieldlength, i, j;
     int64_t pos   = avio_tell(f);
     uint32_t size = avio_rb32(f);
-    struct VideoFile *vf = NULL;
+    struct Track *track = NULL;
 
     if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a'))
         goto fail;
     version = avio_r8(f);
     avio_rb24(f);
     track_id = avio_rb32(f); /* track id */
-    for (i = start_index; i < files->nb_files && !vf; i++)
-        if (files->files[i]->track_id == track_id)
-            vf = files->files[i];
-    if (!vf) {
+    for (i = start_index; i < tracks->nb_tracks && !track; i++)
+        if (tracks->tracks[i]->track_id == track_id)
+            track = tracks->tracks[i];
+    if (!track) {
         /* Ok, continue parsing the next atom */
         ret = 0;
         goto fail;
     }
     fieldlength = avio_rb32(f);
-    vf->chunks  = avio_rb32(f);
-    vf->offsets = av_mallocz(sizeof(*vf->offsets) * vf->chunks);
-    if (!vf->offsets) {
+    track->chunks  = avio_rb32(f);
+    track->offsets = av_mallocz(sizeof(*track->offsets) * track->chunks);
+    if (!track->offsets) {
         ret = AVERROR(ENOMEM);
         goto fail;
     }
-    for (i = 0; i < vf->chunks; i++) {
+    for (i = 0; i < track->chunks; i++) {
         if (version == 1) {
-            vf->offsets[i].time   = avio_rb64(f);
-            vf->offsets[i].offset = avio_rb64(f);
+            track->offsets[i].time   = avio_rb64(f);
+            track->offsets[i].offset = avio_rb64(f);
         } else {
-            vf->offsets[i].time   = avio_rb32(f);
-            vf->offsets[i].offset = avio_rb32(f);
+            track->offsets[i].time   = avio_rb32(f);
+            track->offsets[i].offset = avio_rb32(f);
         }
         for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
             avio_r8(f);
@@ -186,12 +187,12 @@
         for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
             avio_r8(f);
         if (i > 0)
-            vf->offsets[i - 1].duration = vf->offsets[i].time -
-                                          vf->offsets[i - 1].time;
+            track->offsets[i - 1].duration = track->offsets[i].time -
+                                             track->offsets[i - 1].time;
     }
-    if (vf->chunks > 0)
-        vf->offsets[vf->chunks - 1].duration = vf->duration -
-                                               vf->offsets[vf->chunks - 1].time;
+    if (track->chunks > 0)
+        track->offsets[track->chunks - 1].duration = track->duration -
+                                                     track->offsets[track->chunks - 1].time;
     ret = 0;
 
 fail:
@@ -199,7 +200,7 @@
     return ret;
 }
 
-static int read_mfra(struct VideoFiles *files, int start_index,
+static int read_mfra(struct Tracks *tracks, int start_index,
                      const char *file, int split)
 {
     int err = 0;
@@ -219,12 +220,12 @@
         err = AVERROR_INVALIDDATA;
         goto fail;
     }
-    while (!read_tfra(files, start_index, f)) {
+    while (!read_tfra(tracks, start_index, f)) {
         /* Empty */
     }
 
     if (split)
-        write_fragments(files, start_index, f);
+        err = write_fragments(tracks, start_index, f);
 
 fail:
     if (f)
@@ -234,24 +235,24 @@
     return err;
 }
 
-static int get_private_data(struct VideoFile *vf, AVCodecContext *codec)
+static int get_private_data(struct Track *track, AVCodecContext *codec)
 {
-    vf->codec_private_size = codec->extradata_size;
-    vf->codec_private      = av_mallocz(codec->extradata_size);
-    if (!vf->codec_private)
+    track->codec_private_size = codec->extradata_size;
+    track->codec_private      = av_mallocz(codec->extradata_size);
+    if (!track->codec_private)
         return AVERROR(ENOMEM);
-    memcpy(vf->codec_private, codec->extradata, codec->extradata_size);
+    memcpy(track->codec_private, codec->extradata, codec->extradata_size);
     return 0;
 }
 
-static int get_video_private_data(struct VideoFile *vf, AVCodecContext *codec)
+static int get_video_private_data(struct Track *track, AVCodecContext *codec)
 {
     AVIOContext *io = NULL;
     uint16_t sps_size, pps_size;
     int err = AVERROR(EINVAL);
 
     if (codec->codec_id == AV_CODEC_ID_VC1)
-        return get_private_data(vf, codec);
+        return get_private_data(track, codec);
 
     if (avio_open_dyn_buf(&io) < 0)  {
         err = AVERROR(ENOMEM);
@@ -272,16 +273,16 @@
     err = 0;
 
 fail:
-    vf->codec_private_size = avio_close_dyn_buf(io, &vf->codec_private);
+    track->codec_private_size = avio_close_dyn_buf(io, &track->codec_private);
     return err;
 }
 
-static int handle_file(struct VideoFiles *files, const char *file, int split)
+static int handle_file(struct Tracks *tracks, const char *file, int split)
 {
     AVFormatContext *ctx = NULL;
-    int err = 0, i, orig_files = files->nb_files;
+    int err = 0, i, orig_tracks = tracks->nb_tracks;
     char errbuf[50], *ptr;
-    struct VideoFile *vf;
+    struct Track *track;
 
     err = avformat_open_input(&ctx, file, NULL, NULL);
     if (err < 0) {
@@ -301,72 +302,83 @@
         fprintf(stderr, "No streams found in %s\n", file);
         goto fail;
     }
-    if (!files->duration)
-        files->duration = ctx->duration;
+    if (!tracks->duration)
+        tracks->duration = ctx->duration;
 
     for (i = 0; i < ctx->nb_streams; i++) {
+        struct Track **temp;
         AVStream *st = ctx->streams[i];
-        vf = av_mallocz(sizeof(*vf));
-        files->files = av_realloc(files->files,
-                                  sizeof(*files->files) * (files->nb_files + 1));
-        files->files[files->nb_files] = vf;
+        track = av_mallocz(sizeof(*track));
+        if (!track) {
+            err = AVERROR(ENOMEM);
+            goto fail;
+        }
+        temp = av_realloc(tracks->tracks,
+                          sizeof(*tracks->tracks) * (tracks->nb_tracks + 1));
+        if (!temp) {
+            av_free(track);
+            err = AVERROR(ENOMEM);
+            goto fail;
+        }
+        tracks->tracks = temp;
+        tracks->tracks[tracks->nb_tracks] = track;
 
-        vf->name = file;
+        track->name = file;
         if ((ptr = strrchr(file, '/')) != NULL)
-            vf->name = ptr + 1;
+            track->name = ptr + 1;
 
-        vf->bitrate   = st->codec->bit_rate;
-        vf->track_id  = st->id;
-        vf->timescale = st->time_base.den;
-        vf->duration  = av_rescale_rnd(ctx->duration, vf->timescale,
-                                       AV_TIME_BASE, AV_ROUND_UP);
-        vf->is_audio  = st->codec->codec_type == AVMEDIA_TYPE_AUDIO;
-        vf->is_video  = st->codec->codec_type == AVMEDIA_TYPE_VIDEO;
+        track->bitrate   = st->codec->bit_rate;
+        track->track_id  = st->id;
+        track->timescale = st->time_base.den;
+        track->duration  = av_rescale_rnd(ctx->duration, track->timescale,
+                                          AV_TIME_BASE, AV_ROUND_UP);
+        track->is_audio  = st->codec->codec_type == AVMEDIA_TYPE_AUDIO;
+        track->is_video  = st->codec->codec_type == AVMEDIA_TYPE_VIDEO;
 
-        if (!vf->is_audio && !vf->is_video) {
+        if (!track->is_audio && !track->is_video) {
             fprintf(stderr,
                     "Track %d in %s is neither video nor audio, skipping\n",
-                    vf->track_id, file);
-            av_freep(&files->files[files->nb_files]);
+                    track->track_id, file);
+            av_freep(&tracks->tracks[tracks->nb_tracks]);
             continue;
         }
 
-        if (vf->is_audio) {
-            if (files->audio_file < 0)
-                files->audio_file = files->nb_files;
-            files->nb_audio_files++;
-            vf->channels    = st->codec->channels;
-            vf->sample_rate = st->codec->sample_rate;
+        if (track->is_audio) {
+            if (tracks->audio_track < 0)
+                tracks->audio_track = tracks->nb_tracks;
+            tracks->nb_audio_tracks++;
+            track->channels    = st->codec->channels;
+            track->sample_rate = st->codec->sample_rate;
             if (st->codec->codec_id == AV_CODEC_ID_AAC) {
-                vf->fourcc    = "AACL";
-                vf->tag       = 255;
-                vf->blocksize = 4;
+                track->fourcc    = "AACL";
+                track->tag       = 255;
+                track->blocksize = 4;
             } else if (st->codec->codec_id == AV_CODEC_ID_WMAPRO) {
-                vf->fourcc    = "WMAP";
-                vf->tag       = st->codec->codec_tag;
-                vf->blocksize = st->codec->block_align;
+                track->fourcc    = "WMAP";
+                track->tag       = st->codec->codec_tag;
+                track->blocksize = st->codec->block_align;
             }
-            get_private_data(vf, st->codec);
+            get_private_data(track, st->codec);
         }
-        if (vf->is_video) {
-            if (files->video_file < 0)
-                files->video_file = files->nb_files;
-            files->nb_video_files++;
-            vf->width  = st->codec->width;
-            vf->height = st->codec->height;
+        if (track->is_video) {
+            if (tracks->video_track < 0)
+                tracks->video_track = tracks->nb_tracks;
+            tracks->nb_video_tracks++;
+            track->width  = st->codec->width;
+            track->height = st->codec->height;
             if (st->codec->codec_id == AV_CODEC_ID_H264)
-                vf->fourcc = "H264";
+                track->fourcc = "H264";
             else if (st->codec->codec_id == AV_CODEC_ID_VC1)
-                vf->fourcc = "WVC1";
-            get_video_private_data(vf, st->codec);
+                track->fourcc = "WVC1";
+            get_video_private_data(track, st->codec);
         }
 
-        files->nb_files++;
+        tracks->nb_tracks++;
     }
 
     avformat_close_input(&ctx);
 
-    err = read_mfra(files, orig_files, file, split);
+    err = read_mfra(tracks, orig_tracks, file, split);
 
 fail:
     if (ctx)
@@ -374,7 +386,7 @@
     return err;
 }
 
-static void output_server_manifest(struct VideoFiles *files,
+static void output_server_manifest(struct Tracks *tracks,
                                    const char *basename)
 {
     char filename[1000];
@@ -395,13 +407,13 @@
     fprintf(out, "\t</head>\n");
     fprintf(out, "\t<body>\n");
     fprintf(out, "\t\t<switch>\n");
-    for (i = 0; i < files->nb_files; i++) {
-        struct VideoFile *vf = files->files[i];
-        const char *type     = vf->is_video ? "video" : "audio";
+    for (i = 0; i < tracks->nb_tracks; i++) {
+        struct Track *track = tracks->tracks[i];
+        const char *type    = track->is_video ? "video" : "audio";
         fprintf(out, "\t\t\t<%s src=\"%s\" systemBitrate=\"%d\">\n",
-                type, vf->name, vf->bitrate);
+                type, track->name, track->bitrate);
         fprintf(out, "\t\t\t\t<param name=\"trackID\" value=\"%d\" "
-                     "valueType=\"data\" />\n", vf->track_id);
+                     "valueType=\"data\" />\n", track->track_id);
         fprintf(out, "\t\t\t</%s>\n", type);
     }
     fprintf(out, "\t\t</switch>\n");
@@ -410,7 +422,24 @@
     fclose(out);
 }
 
-static void output_client_manifest(struct VideoFiles *files,
+static void print_track_chunks(FILE *out, struct Tracks *tracks, int main,
+                               const char *type)
+{
+    int i, j;
+    struct Track *track = tracks->tracks[main];
+    for (i = 0; i < track->chunks; i++) {
+        for (j = main + 1; j < tracks->nb_tracks; j++) {
+            if (tracks->tracks[j]->is_audio == track->is_audio &&
+                track->offsets[i].duration != tracks->tracks[j]->offsets[i].duration)
+                fprintf(stderr, "Mismatched duration of %s chunk %d in %s and %s\n",
+                        type, i, track->name, tracks->tracks[j]->name);
+        }
+        fprintf(out, "\t\t<c n=\"%d\" d=\"%d\" />\n",
+                i, track->offsets[i].duration);
+    }
+}
+
+static void output_client_manifest(struct Tracks *tracks,
                                    const char *basename, int split)
 {
     char filename[1000];
@@ -428,108 +457,88 @@
     }
     fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
     fprintf(out, "<SmoothStreamingMedia MajorVersion=\"2\" MinorVersion=\"0\" "
-                 "Duration=\"%"PRId64 "\">\n", files->duration * 10);
-    if (files->video_file >= 0) {
-        struct VideoFile *vf = files->files[files->video_file];
-        struct VideoFile *first_vf = vf;
+                 "Duration=\"%"PRId64 "\">\n", tracks->duration * 10);
+    if (tracks->video_track >= 0) {
+        struct Track *track = tracks->tracks[tracks->video_track];
+        struct Track *first_track = track;
         int index = 0;
         fprintf(out,
                 "\t<StreamIndex Type=\"video\" QualityLevels=\"%d\" "
                 "Chunks=\"%d\" "
                 "Url=\"QualityLevels({bitrate})/Fragments(video={start time})\">\n",
-                files->nb_video_files, vf->chunks);
-        for (i = 0; i < files->nb_files; i++) {
-            vf = files->files[i];
-            if (!vf->is_video)
+                tracks->nb_video_tracks, track->chunks);
+        for (i = 0; i < tracks->nb_tracks; i++) {
+            track = tracks->tracks[i];
+            if (!track->is_video)
                 continue;
             fprintf(out,
                     "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
                     "FourCC=\"%s\" MaxWidth=\"%d\" MaxHeight=\"%d\" "
                     "CodecPrivateData=\"",
-                    index, vf->bitrate, vf->fourcc, vf->width, vf->height);
-            for (j = 0; j < vf->codec_private_size; j++)
-                fprintf(out, "%02X", vf->codec_private[j]);
+                    index, track->bitrate, track->fourcc, track->width, track->height);
+            for (j = 0; j < track->codec_private_size; j++)
+                fprintf(out, "%02X", track->codec_private[j]);
             fprintf(out, "\" />\n");
             index++;
-            if (vf->chunks != first_vf->chunks)
+            if (track->chunks != first_track->chunks)
                 fprintf(stderr, "Mismatched number of video chunks in %s and %s\n",
-                        vf->name, first_vf->name);
+                        track->name, first_track->name);
         }
-        vf = first_vf;
-        for (i = 0; i < vf->chunks; i++) {
-            for (j = files->video_file + 1; j < files->nb_files; j++) {
-                if (files->files[j]->is_video &&
-                    vf->offsets[i].duration != files->files[j]->offsets[i].duration)
-                    fprintf(stderr, "Mismatched duration of video chunk %d in %s and %s\n",
-                            i, vf->name, files->files[j]->name);
-            }
-            fprintf(out, "\t\t<c n=\"%d\" d=\"%d\" />\n", i,
-                    vf->offsets[i].duration);
-        }
+        print_track_chunks(out, tracks, tracks->video_track, "video");
         fprintf(out, "\t</StreamIndex>\n");
     }
-    if (files->audio_file >= 0) {
-        struct VideoFile *vf = files->files[files->audio_file];
-        struct VideoFile *first_vf = vf;
+    if (tracks->audio_track >= 0) {
+        struct Track *track = tracks->tracks[tracks->audio_track];
+        struct Track *first_track = track;
         int index = 0;
         fprintf(out,
                 "\t<StreamIndex Type=\"audio\" QualityLevels=\"%d\" "
                 "Chunks=\"%d\" "
                 "Url=\"QualityLevels({bitrate})/Fragments(audio={start time})\">\n",
-                files->nb_audio_files, vf->chunks);
-        for (i = 0; i < files->nb_files; i++) {
-            vf = files->files[i];
-            if (!vf->is_audio)
+                tracks->nb_audio_tracks, track->chunks);
+        for (i = 0; i < tracks->nb_tracks; i++) {
+            track = tracks->tracks[i];
+            if (!track->is_audio)
                 continue;
             fprintf(out,
                     "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
                     "FourCC=\"%s\" SamplingRate=\"%d\" Channels=\"%d\" "
                     "BitsPerSample=\"16\" PacketSize=\"%d\" "
                     "AudioTag=\"%d\" CodecPrivateData=\"",
-                    index, vf->bitrate, vf->fourcc, vf->sample_rate,
-                    vf->channels, vf->blocksize, vf->tag);
-            for (j = 0; j < vf->codec_private_size; j++)
-                fprintf(out, "%02X", vf->codec_private[j]);
+                    index, track->bitrate, track->fourcc, track->sample_rate,
+                    track->channels, track->blocksize, track->tag);
+            for (j = 0; j < track->codec_private_size; j++)
+                fprintf(out, "%02X", track->codec_private[j]);
             fprintf(out, "\" />\n");
             index++;
-            if (vf->chunks != first_vf->chunks)
+            if (track->chunks != first_track->chunks)
                 fprintf(stderr, "Mismatched number of audio chunks in %s and %s\n",
-                        vf->name, first_vf->name);
+                        track->name, first_track->name);
         }
-        vf = first_vf;
-        for (i = 0; i < vf->chunks; i++) {
-            for (j = files->audio_file + 1; j < files->nb_files; j++) {
-                if (files->files[j]->is_audio &&
-                    vf->offsets[i].duration != files->files[j]->offsets[i].duration)
-                    fprintf(stderr, "Mismatched duration of audio chunk %d in %s and %s\n",
-                            i, vf->name, files->files[j]->name);
-            }
-            fprintf(out, "\t\t<c n=\"%d\" d=\"%d\" />\n",
-                    i, vf->offsets[i].duration);
-        }
+        print_track_chunks(out, tracks, tracks->audio_track, "audio");
         fprintf(out, "\t</StreamIndex>\n");
     }
     fprintf(out, "</SmoothStreamingMedia>\n");
     fclose(out);
 }
 
-static void clean_files(struct VideoFiles *files)
+static void clean_tracks(struct Tracks *tracks)
 {
     int i;
-    for (i = 0; i < files->nb_files; i++) {
-        av_freep(&files->files[i]->codec_private);
-        av_freep(&files->files[i]->offsets);
-        av_freep(&files->files[i]);
+    for (i = 0; i < tracks->nb_tracks; i++) {
+        av_freep(&tracks->tracks[i]->codec_private);
+        av_freep(&tracks->tracks[i]->offsets);
+        av_freep(&tracks->tracks[i]);
     }
-    av_freep(&files->files);
-    files->nb_files = 0;
+    av_freep(&tracks->tracks);
+    tracks->nb_tracks = 0;
 }
 
 int main(int argc, char **argv)
 {
     const char *basename = NULL;
     int split = 0, i;
-    struct VideoFiles vf = { 0, .video_file = -1, .audio_file = -1 };
+    struct Tracks tracks = { 0, .video_track = -1, .audio_track = -1 };
 
     av_register_all();
 
@@ -542,18 +551,18 @@
         } else if (argv[i][0] == '-') {
             return usage(argv[0], 1);
         } else {
-            if (handle_file(&vf, argv[i], split))
+            if (handle_file(&tracks, argv[i], split))
                 return 1;
         }
     }
-    if (!vf.nb_files || (!basename && !split))
+    if (!tracks.nb_tracks || (!basename && !split))
         return usage(argv[0], 1);
 
     if (!split)
-        output_server_manifest(&vf, basename);
-    output_client_manifest(&vf, basename, split);
+        output_server_manifest(&tracks, basename);
+    output_client_manifest(&tracks, basename, split);
 
-    clean_files(&vf);
+    clean_tracks(&tracks);
 
     return 0;
 }
diff --git a/tools/normalize.py b/tools/normalize.py
new file mode 100755
index 0000000..e015913
--- /dev/null
+++ b/tools/normalize.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python2
+
+import sys, subprocess
+
+if len(sys.argv) > 1:
+    ifile  = sys.argv[1]
+    encopt = sys.argv[2:-1]
+    ofile  = sys.argv[-1]
+else:
+    print 'usage: %s <input> [encode_options] <output>' % sys.argv[0]
+    sys.exit(1)
+
+analysis_cmd  = 'ffprobe -v error -of compact=p=0:nk=1 '
+analysis_cmd += '-show_entries frame_tags=lavfi.r128.I -f lavfi '
+analysis_cmd += "amovie='%s',ebur128=metadata=1" % ifile
+try:
+    probe_out = subprocess.check_output(analysis_cmd, shell=True)
+except subprocess.CalledProcessError, e:
+    sys.exit(e.returncode)
+loudness = ref = -23
+for line in probe_out.splitlines():
+    sline = line.rstrip()
+    if sline:
+        loudness = sline
+adjust = ref - float(loudness)
+if abs(adjust) < 0.0001:
+    print 'No normalization needed for ' + ifile
+else:
+    print "Adjust %s by %.1fdB" % (ifile, adjust)
+    norm_cmd  = ['ffmpeg', '-i', ifile, '-af', 'volume=%fdB' % adjust]
+    norm_cmd += encopt + [ofile]
+    print ' => %s' % ' '.join(norm_cmd)
+    subprocess.call(norm_cmd)
diff --git a/tools/seek_print.c b/tools/seek_print.c
index a99a0ad..8eb7627 100644
--- a/tools/seek_print.c
+++ b/tools/seek_print.c
@@ -91,7 +91,7 @@
                        av_ts2str(packet.pts), av_ts2timestr(packet.pts, tb));
                 av_free_packet(&packet);
             }
-        } else if (sscanf(*argv, "seek:%i:%"PRIi64":%"PRIi64":%"PRIi64":%i",
+        } else if (sscanf(*argv, "seek:%i:%"SCNi64":%"SCNi64":%"SCNi64":%i",
                    &stream, &min_ts, &ts, &max_ts, &flags) == 5) {
             ret = avformat_seek_file(avf, stream, min_ts, ts, max_ts, flags);
             printf("seek: %d (%s)\n", ret, av_err2str(ret));