[6/6] libaom: Templatize code shared with libvpx to avoid code duplication

Message ID 1472138898-16470-6-git-send-email-diego@biurrun.de
State New
Headers show

Commit Message

Diego Biurrun Aug. 25, 2016, 3:28 p.m.
---

Now templatizes the decoder along with the shared code.

 libavcodec/libaom.c                              |  57 +-----------
 libavcodec/libaom.c_template.c                   | 111 +++++++++++++++++++++++
 libavcodec/libaom.h                              |  12 +--
 libavcodec/{libaom.h => libaom.h_template.c}     |  13 +--
 libavcodec/libaomdec.c                           |  93 +------------------
 libavcodec/{libaomdec.c => libaomdec_template.c} |  28 +-----
 libavcodec/libvpx.c                              |  57 +-----------
 libavcodec/libvpx.h                              |   8 +-
 libavcodec/libvpxdec.c                           | 106 ++++------------------
 9 files changed, 150 insertions(+), 335 deletions(-)
 create mode 100644 libavcodec/libaom.c_template.c
 copy libavcodec/{libaom.h => libaom.h_template.c} (72%)
 copy libavcodec/{libaomdec.c => libaomdec_template.c} (84%)

Patch

diff --git a/libavcodec/libaom.c b/libavcodec/libaom.c
index f8bcc37..fbf35a3 100644
--- a/libavcodec/libaom.c
+++ b/libavcodec/libaom.c
@@ -1,6 +1,4 @@ 
 /*
- * Copyright (c) 2013 Guillaume Martres <smarter@ubuntu.com>
- *
  * This file is part of Libav.
  *
  * Libav is free software; you can redistribute it and/or
@@ -18,58 +16,9 @@ 
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include <aom/aom_image.h>
-
 #include "libaom.h"
 
-enum AVPixelFormat ff_aom_imgfmt_to_pixfmt(aom_img_fmt_t img)
-{
-    switch (img) {
-    case AOM_IMG_FMT_RGB24:     return AV_PIX_FMT_RGB24;
-    case AOM_IMG_FMT_RGB565:    return AV_PIX_FMT_RGB565BE;
-    case AOM_IMG_FMT_RGB555:    return AV_PIX_FMT_RGB555BE;
-    case AOM_IMG_FMT_UYVY:      return AV_PIX_FMT_UYVY422;
-    case AOM_IMG_FMT_YUY2:      return AV_PIX_FMT_YUYV422;
-    case AOM_IMG_FMT_YVYU:      return AV_PIX_FMT_YVYU422;
-    case AOM_IMG_FMT_BGR24:     return AV_PIX_FMT_BGR24;
-    case AOM_IMG_FMT_ARGB:      return AV_PIX_FMT_ARGB;
-    case AOM_IMG_FMT_ARGB_LE:   return AV_PIX_FMT_BGRA;
-    case AOM_IMG_FMT_RGB565_LE: return AV_PIX_FMT_RGB565LE;
-    case AOM_IMG_FMT_RGB555_LE: return AV_PIX_FMT_RGB555LE;
-    case AOM_IMG_FMT_I420:      return AV_PIX_FMT_YUV420P;
-    case AOM_IMG_FMT_I422:      return AV_PIX_FMT_YUV422P;
-    case AOM_IMG_FMT_I444:      return AV_PIX_FMT_YUV444P;
-    case AOM_IMG_FMT_444A:      return AV_PIX_FMT_YUVA444P;
-    case AOM_IMG_FMT_I440:      return AV_PIX_FMT_YUV440P;
-    case AOM_IMG_FMT_I42016:    return AV_PIX_FMT_YUV420P16BE;
-    case AOM_IMG_FMT_I42216:    return AV_PIX_FMT_YUV422P16BE;
-    case AOM_IMG_FMT_I44416:    return AV_PIX_FMT_YUV444P16BE;
-    default:                    return AV_PIX_FMT_NONE;
-    }
-}
+#include "libaom.c_template.c"
 
-aom_img_fmt_t ff_aom_pixfmt_to_imgfmt(enum AVPixelFormat pix)
-{
-    switch (pix) {
-    case AV_PIX_FMT_RGB24:        return AOM_IMG_FMT_RGB24;
-    case AV_PIX_FMT_RGB565BE:     return AOM_IMG_FMT_RGB565;
-    case AV_PIX_FMT_RGB555BE:     return AOM_IMG_FMT_RGB555;
-    case AV_PIX_FMT_UYVY422:      return AOM_IMG_FMT_UYVY;
-    case AV_PIX_FMT_YUYV422:      return AOM_IMG_FMT_YUY2;
-    case AV_PIX_FMT_YVYU422:      return AOM_IMG_FMT_YVYU;
-    case AV_PIX_FMT_BGR24:        return AOM_IMG_FMT_BGR24;
-    case AV_PIX_FMT_ARGB:         return AOM_IMG_FMT_ARGB;
-    case AV_PIX_FMT_BGRA:         return AOM_IMG_FMT_ARGB_LE;
-    case AV_PIX_FMT_RGB565LE:     return AOM_IMG_FMT_RGB565_LE;
-    case AV_PIX_FMT_RGB555LE:     return AOM_IMG_FMT_RGB555_LE;
-    case AV_PIX_FMT_YUV420P:      return AOM_IMG_FMT_I420;
-    case AV_PIX_FMT_YUV422P:      return AOM_IMG_FMT_I422;
-    case AV_PIX_FMT_YUV444P:      return AOM_IMG_FMT_I444;
-    case AV_PIX_FMT_YUVA444P:     return AOM_IMG_FMT_444A;
-    case AV_PIX_FMT_YUV440P:      return AOM_IMG_FMT_I440;
-    case AV_PIX_FMT_YUV420P16BE:  return AOM_IMG_FMT_I42016;
-    case AV_PIX_FMT_YUV422P16BE:  return AOM_IMG_FMT_I42216;
-    case AV_PIX_FMT_YUV444P16BE:  return AOM_IMG_FMT_I44416;
-    default:                      return AOM_IMG_FMT_NONE;
-    }
-}
+FF_AOM_IMGFMT_TO_PIXFMT(TYPE, AOM)
+FF_AOM_PIXFMT_TO_IMGFMT(TYPE, AOM)
diff --git a/libavcodec/libaom.c_template.c b/libavcodec/libaom.c_template.c
new file mode 100644
index 0000000..1977b56
--- /dev/null
+++ b/libavcodec/libaom.c_template.c
@@ -0,0 +1,111 @@ 
+/*
+ * 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
+ */
+
+#define FF_AOM_IMGFMT_TO_PIXFMT(TYPE, PREFIX)                          \
+enum AVPixelFormat IMGFMT_TO_PIXFMT(TYPE img)                          \
+{                                                                      \
+    switch (img) {                                                     \
+    case PREFIX ## _IMG_FMT_RGB24:                                     \
+        return AV_PIX_FMT_RGB24;                                       \
+    case PREFIX ## _IMG_FMT_RGB565:                                    \
+        return AV_PIX_FMT_RGB565BE;                                    \
+    case PREFIX ## _IMG_FMT_RGB555:                                    \
+        return AV_PIX_FMT_RGB555BE;                                    \
+    case PREFIX ## _IMG_FMT_UYVY:                                      \
+        return AV_PIX_FMT_UYVY422;                                     \
+    case PREFIX ## _IMG_FMT_YUY2:                                      \
+        return AV_PIX_FMT_YUYV422;                                     \
+    case PREFIX ## _IMG_FMT_YVYU:                                      \
+        return AV_PIX_FMT_YVYU422;                                     \
+    case PREFIX ## _IMG_FMT_BGR24:                                     \
+        return AV_PIX_FMT_BGR24;                                       \
+    case PREFIX ## _IMG_FMT_ARGB:                                      \
+        return AV_PIX_FMT_ARGB;                                        \
+    case PREFIX ## _IMG_FMT_ARGB_LE:                                   \
+        return AV_PIX_FMT_BGRA;                                        \
+    case PREFIX ## _IMG_FMT_RGB565_LE:                                 \
+        return AV_PIX_FMT_RGB565LE;                                    \
+    case PREFIX ## _IMG_FMT_RGB555_LE:                                 \
+        return AV_PIX_FMT_RGB555LE;                                    \
+    case PREFIX ## _IMG_FMT_I420:                                      \
+        return AV_PIX_FMT_YUV420P;                                     \
+    case PREFIX ## _IMG_FMT_I422:                                      \
+        return AV_PIX_FMT_YUV422P;                                     \
+    case PREFIX ## _IMG_FMT_I444:                                      \
+        return AV_PIX_FMT_YUV444P;                                     \
+    case PREFIX ## _IMG_FMT_444A:                                      \
+        return AV_PIX_FMT_YUVA444P;                                    \
+    case PREFIX ## _IMG_FMT_I440:                                      \
+        return AV_PIX_FMT_YUV440P;                                     \
+    case PREFIX ## _IMG_FMT_I42016:                                    \
+        return AV_PIX_FMT_YUV420P16BE;                                 \
+    case PREFIX ## _IMG_FMT_I42216:                                    \
+        return AV_PIX_FMT_YUV422P16BE;                                 \
+    case PREFIX ## _IMG_FMT_I44416:                                    \
+        return AV_PIX_FMT_YUV444P16BE;                                 \
+    default:                                                           \
+        return AV_PIX_FMT_NONE;                                        \
+    }                                                                  \
+}
+
+#define FF_AOM_PIXFMT_TO_IMGFMT(TYPE, PREFIX)                          \
+TYPE PIXFMT_TO_IMGFMT(enum AVPixelFormat pix)                          \
+{                                                                      \
+    switch (pix) {                                                     \
+    case AV_PIX_FMT_RGB24:                                             \
+        return PREFIX ## _IMG_FMT_RGB24;                               \
+    case AV_PIX_FMT_RGB565BE:                                          \
+        return PREFIX ## _IMG_FMT_RGB565;                              \
+    case AV_PIX_FMT_RGB555BE:                                          \
+        return PREFIX ## _IMG_FMT_RGB555;                              \
+    case AV_PIX_FMT_UYVY422:                                           \
+        return PREFIX ## _IMG_FMT_UYVY;                                \
+    case AV_PIX_FMT_YUYV422:                                           \
+        return PREFIX ## _IMG_FMT_YUY2;                                \
+    case AV_PIX_FMT_YVYU422:                                           \
+        return PREFIX ## _IMG_FMT_YVYU;                                \
+    case AV_PIX_FMT_BGR24:                                             \
+        return PREFIX ## _IMG_FMT_BGR24;                               \
+    case AV_PIX_FMT_ARGB:                                              \
+        return PREFIX ## _IMG_FMT_ARGB;                                \
+    case AV_PIX_FMT_BGRA:                                              \
+        return PREFIX ## _IMG_FMT_ARGB_LE;                             \
+    case AV_PIX_FMT_RGB565LE:                                          \
+        return PREFIX ## _IMG_FMT_RGB565_LE;                           \
+    case AV_PIX_FMT_RGB555LE:                                          \
+        return PREFIX ## _IMG_FMT_RGB555_LE;                           \
+    case AV_PIX_FMT_YUV420P:                                           \
+        return PREFIX ## _IMG_FMT_I420;                                \
+    case AV_PIX_FMT_YUV422P:                                           \
+        return PREFIX ## _IMG_FMT_I422;                                \
+    case AV_PIX_FMT_YUV444P:                                           \
+        return PREFIX ## _IMG_FMT_I444;                                \
+    case AV_PIX_FMT_YUVA444P:                                          \
+        return PREFIX ## _IMG_FMT_444A;                                \
+    case AV_PIX_FMT_YUV440P:                                           \
+        return PREFIX ## _IMG_FMT_I440;                                \
+    case AV_PIX_FMT_YUV420P16BE:                                       \
+        return PREFIX ## _IMG_FMT_I42016;                              \
+    case AV_PIX_FMT_YUV422P16BE:                                       \
+        return PREFIX ## _IMG_FMT_I42216;                              \
+    case AV_PIX_FMT_YUV444P16BE:                                       \
+        return PREFIX ## _IMG_FMT_I44416;                              \
+    default:                                                           \
+        return PREFIX ## _IMG_FMT_NONE;                                \
+    }                                                                  \
+}
diff --git a/libavcodec/libaom.h b/libavcodec/libaom.h
index 7a7b8db..63a7f75 100644
--- a/libavcodec/libaom.h
+++ b/libavcodec/libaom.h
@@ -1,6 +1,4 @@ 
 /*
- * Copyright (c) 2013 Guillaume Martres <smarter@ubuntu.com>
- *
  * This file is part of Libav.
  *
  * Libav is free software; you can redistribute it and/or
@@ -21,11 +19,11 @@ 
 #ifndef AVCODEC_LIBAOM_H
 #define AVCODEC_LIBAOM_H
 
-#include <aom/aom_codec.h>
-
-#include "libavutil/pixfmt.h"
+#include <aom/aom_image.h>
 
-enum AVPixelFormat ff_aom_imgfmt_to_pixfmt(aom_img_fmt_t img);
-aom_img_fmt_t ff_aom_pixfmt_to_imgfmt(enum AVPixelFormat pix);
+#define TYPE aom_img_fmt_t
+#define IMGFMT_TO_PIXFMT ff_aom_imgfmt_to_pixfmt
+#define PIXFMT_TO_IMGFMT ff_aom_pixfmt_to_imgfmt
+#include "libaom.h_template.c"
 
 #endif /* AVCODEC_LIBAOM_H */
diff --git a/libavcodec/libaom.h b/libavcodec/libaom.h_template.c
similarity index 72%
copy from libavcodec/libaom.h
copy to libavcodec/libaom.h_template.c
index 7a7b8db..006a3d7 100644
--- a/libavcodec/libaom.h
+++ b/libavcodec/libaom.h_template.c
@@ -1,6 +1,4 @@ 
 /*
- * Copyright (c) 2013 Guillaume Martres <smarter@ubuntu.com>
- *
  * This file is part of Libav.
  *
  * Libav is free software; you can redistribute it and/or
@@ -18,14 +16,7 @@ 
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#ifndef AVCODEC_LIBAOM_H
-#define AVCODEC_LIBAOM_H
-
-#include <aom/aom_codec.h>
-
 #include "libavutil/pixfmt.h"
 
-enum AVPixelFormat ff_aom_imgfmt_to_pixfmt(aom_img_fmt_t img);
-aom_img_fmt_t ff_aom_pixfmt_to_imgfmt(enum AVPixelFormat pix);
-
-#endif /* AVCODEC_LIBAOM_H */
+enum AVPixelFormat IMGFMT_TO_PIXFMT(TYPE img);
+TYPE PIXFMT_TO_IMGFMT(enum AVPixelFormat pix);
diff --git a/libavcodec/libaomdec.c b/libavcodec/libaomdec.c
index e975b63..0b761fd 100644
--- a/libavcodec/libaomdec.c
+++ b/libavcodec/libaomdec.c
@@ -1,6 +1,4 @@ 
 /*
- * Copyright (c) 2010, Google, Inc.
- *
  * This file is part of Libav.
  *
  * Libav is free software; you can redistribute it and/or
@@ -26,98 +24,9 @@ 
 #include <aom/aom_decoder.h>
 #include <aom/aomdx.h>
 
-#include "libavutil/common.h"
-#include "libavutil/imgutils.h"
-
-#include "avcodec.h"
-#include "internal.h"
 #include "libaom.h"
 
-typedef struct AV1DecodeContext {
-    struct aom_codec_ctx decoder;
-} AV1DecodeContext;
-
-static av_cold int aom_init(AVCodecContext *avctx,
-                            const struct aom_codec_iface *iface)
-{
-    AV1DecodeContext *ctx = avctx->priv_data;
-    struct aom_codec_dec_cfg deccfg = {
-        /* token partitions+1 would be a decent choice */
-        .threads = FFMIN(avctx->thread_count, 16)
-    };
-
-    av_log(avctx, AV_LOG_INFO, "%s\n", aom_codec_version_str());
-    av_log(avctx, AV_LOG_VERBOSE, "%s\n", aom_codec_build_config());
-
-    if (aom_codec_dec_init(&ctx->decoder, iface, &deccfg, 0) != AOM_CODEC_OK) {
-        const char *error = aom_codec_error(&ctx->decoder);
-        av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n",
-               error);
-        return AVERROR(EINVAL);
-    }
-
-    return 0;
-}
-
-static int aom_decode(AVCodecContext *avctx, void *data, int *got_frame,
-                      AVPacket *avpkt)
-{
-    AV1DecodeContext *ctx = avctx->priv_data;
-    AVFrame *picture = data;
-    const void *iter = NULL;
-    struct aom_image *img;
-    int ret;
-
-    if (aom_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) !=
-        AOM_CODEC_OK) {
-        const char *error  = aom_codec_error(&ctx->decoder);
-        const char *detail = aom_codec_error_detail(&ctx->decoder);
-
-        av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
-        if (detail)
-            av_log(avctx, AV_LOG_ERROR, "  Additional information: %s\n",
-                   detail);
-        return AVERROR_INVALIDDATA;
-    }
-
-    if ((img = aom_codec_get_frame(&ctx->decoder, &iter))) {
-        avctx->pix_fmt = ff_aom_imgfmt_to_pixfmt(img->fmt);
-        if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
-            av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d)\n",
-                   img->fmt);
-            return AVERROR_INVALIDDATA;
-        }
-
-        if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
-            av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
-                   avctx->width, avctx->height, img->d_w, img->d_h);
-            ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
-            if (ret < 0)
-                return ret;
-        }
-        if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
-            return ret;
-        av_image_copy(picture->data, picture->linesize, (const uint8_t **) img->planes,
-                      img->stride, avctx->pix_fmt, img->d_w, img->d_h);
-        switch (img->range) {
-        case AOM_CR_STUDIO_RANGE:
-            picture->color_range = AVCOL_RANGE_MPEG;
-            break;
-        case AOM_CR_FULL_RANGE:
-            picture->color_range = AVCOL_RANGE_JPEG;
-            break;
-        }
-        *got_frame           = 1;
-    }
-    return avpkt->size;
-}
-
-static av_cold int aom_free(AVCodecContext *avctx)
-{
-    AV1DecodeContext *ctx = avctx->priv_data;
-    aom_codec_destroy(&ctx->decoder);
-    return 0;
-}
+#include "libaomdec_template.c"
 
 static av_cold int av1_init(AVCodecContext *avctx)
 {
diff --git a/libavcodec/libaomdec.c b/libavcodec/libaomdec_template.c
similarity index 84%
copy from libavcodec/libaomdec.c
copy to libavcodec/libaomdec_template.c
index e975b63..8c068a3 100644
--- a/libavcodec/libaomdec.c
+++ b/libavcodec/libaomdec_template.c
@@ -18,20 +18,11 @@ 
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-/**
- * @file
- * AV1 decoder support via libaom
- */
-
-#include <aom/aom_decoder.h>
-#include <aom/aomdx.h>
-
 #include "libavutil/common.h"
 #include "libavutil/imgutils.h"
 
 #include "avcodec.h"
 #include "internal.h"
-#include "libaom.h"
 
 typedef struct AV1DecodeContext {
     struct aom_codec_ctx decoder;
@@ -81,7 +72,7 @@  static int aom_decode(AVCodecContext *avctx, void *data, int *got_frame,
     }
 
     if ((img = aom_codec_get_frame(&ctx->decoder, &iter))) {
-        avctx->pix_fmt = ff_aom_imgfmt_to_pixfmt(img->fmt);
+        avctx->pix_fmt = IMGFMT_TO_PIXFMT(img->fmt);
         if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
             av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d)\n",
                    img->fmt);
@@ -118,20 +109,3 @@  static av_cold int aom_free(AVCodecContext *avctx)
     aom_codec_destroy(&ctx->decoder);
     return 0;
 }
-
-static av_cold int av1_init(AVCodecContext *avctx)
-{
-    return aom_init(avctx, &aom_codec_av1_dx_algo);
-}
-
-AVCodec ff_libaom_av1_decoder = {
-    .name           = "libaom",
-    .long_name      = NULL_IF_CONFIG_SMALL("libaom AV1"),
-    .type           = AVMEDIA_TYPE_VIDEO,
-    .id             = AV_CODEC_ID_AV1,
-    .priv_data_size = sizeof(AV1DecodeContext),
-    .init           = av1_init,
-    .close          = aom_free,
-    .decode         = aom_decode,
-    .capabilities   = AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_DR1,
-};
diff --git a/libavcodec/libvpx.c b/libavcodec/libvpx.c
index cb07177..014935b 100644
--- a/libavcodec/libvpx.c
+++ b/libavcodec/libvpx.c
@@ -1,6 +1,4 @@ 
 /*
- * Copyright (c) 2013 Guillaume Martres <smarter@ubuntu.com>
- *
  * This file is part of Libav.
  *
  * Libav is free software; you can redistribute it and/or
@@ -18,58 +16,9 @@ 
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include <vpx/vpx_codec.h>
-
 #include "libvpx.h"
 
-enum AVPixelFormat ff_vpx_imgfmt_to_pixfmt(vpx_img_fmt_t img)
-{
-    switch (img) {
-    case VPX_IMG_FMT_RGB24:     return AV_PIX_FMT_RGB24;
-    case VPX_IMG_FMT_RGB565:    return AV_PIX_FMT_RGB565BE;
-    case VPX_IMG_FMT_RGB555:    return AV_PIX_FMT_RGB555BE;
-    case VPX_IMG_FMT_UYVY:      return AV_PIX_FMT_UYVY422;
-    case VPX_IMG_FMT_YUY2:      return AV_PIX_FMT_YUYV422;
-    case VPX_IMG_FMT_YVYU:      return AV_PIX_FMT_YVYU422;
-    case VPX_IMG_FMT_BGR24:     return AV_PIX_FMT_BGR24;
-    case VPX_IMG_FMT_ARGB:      return AV_PIX_FMT_ARGB;
-    case VPX_IMG_FMT_ARGB_LE:   return AV_PIX_FMT_BGRA;
-    case VPX_IMG_FMT_RGB565_LE: return AV_PIX_FMT_RGB565LE;
-    case VPX_IMG_FMT_RGB555_LE: return AV_PIX_FMT_RGB555LE;
-    case VPX_IMG_FMT_I420:      return AV_PIX_FMT_YUV420P;
-    case VPX_IMG_FMT_I422:      return AV_PIX_FMT_YUV422P;
-    case VPX_IMG_FMT_I444:      return AV_PIX_FMT_YUV444P;
-    case VPX_IMG_FMT_444A:      return AV_PIX_FMT_YUVA444P;
-    case VPX_IMG_FMT_I440:      return AV_PIX_FMT_YUV440P;
-    case VPX_IMG_FMT_I42016:    return AV_PIX_FMT_YUV420P16BE;
-    case VPX_IMG_FMT_I42216:    return AV_PIX_FMT_YUV422P16BE;
-    case VPX_IMG_FMT_I44416:    return AV_PIX_FMT_YUV444P16BE;
-    default:                    return AV_PIX_FMT_NONE;
-    }
-}
+#include "libaom.c_template.c"
 
-vpx_img_fmt_t ff_vpx_pixfmt_to_imgfmt(enum AVPixelFormat pix)
-{
-    switch (pix) {
-    case AV_PIX_FMT_RGB24:        return VPX_IMG_FMT_RGB24;
-    case AV_PIX_FMT_RGB565BE:     return VPX_IMG_FMT_RGB565;
-    case AV_PIX_FMT_RGB555BE:     return VPX_IMG_FMT_RGB555;
-    case AV_PIX_FMT_UYVY422:      return VPX_IMG_FMT_UYVY;
-    case AV_PIX_FMT_YUYV422:      return VPX_IMG_FMT_YUY2;
-    case AV_PIX_FMT_YVYU422:      return VPX_IMG_FMT_YVYU;
-    case AV_PIX_FMT_BGR24:        return VPX_IMG_FMT_BGR24;
-    case AV_PIX_FMT_ARGB:         return VPX_IMG_FMT_ARGB;
-    case AV_PIX_FMT_BGRA:         return VPX_IMG_FMT_ARGB_LE;
-    case AV_PIX_FMT_RGB565LE:     return VPX_IMG_FMT_RGB565_LE;
-    case AV_PIX_FMT_RGB555LE:     return VPX_IMG_FMT_RGB555_LE;
-    case AV_PIX_FMT_YUV420P:      return VPX_IMG_FMT_I420;
-    case AV_PIX_FMT_YUV422P:      return VPX_IMG_FMT_I422;
-    case AV_PIX_FMT_YUV444P:      return VPX_IMG_FMT_I444;
-    case AV_PIX_FMT_YUVA444P:     return VPX_IMG_FMT_444A;
-    case AV_PIX_FMT_YUV440P:      return VPX_IMG_FMT_I440;
-    case AV_PIX_FMT_YUV420P16BE:  return VPX_IMG_FMT_I42016;
-    case AV_PIX_FMT_YUV422P16BE:  return VPX_IMG_FMT_I42216;
-    case AV_PIX_FMT_YUV444P16BE:  return VPX_IMG_FMT_I44416;
-    default:                      return VPX_IMG_FMT_NONE;
-    }
-}
+FF_AOM_IMGFMT_TO_PIXFMT(TYPE, VPX)
+FF_AOM_PIXFMT_TO_IMGFMT(TYPE, VPX)
diff --git a/libavcodec/libvpx.h b/libavcodec/libvpx.h
index b437f37..b08064e 100644
--- a/libavcodec/libvpx.h
+++ b/libavcodec/libvpx.h
@@ -23,9 +23,9 @@ 
 
 #include <vpx/vpx_codec.h>
 
-#include "avcodec.h"
-
-enum AVPixelFormat ff_vpx_imgfmt_to_pixfmt(vpx_img_fmt_t img);
-vpx_img_fmt_t ff_vpx_pixfmt_to_imgfmt(enum AVPixelFormat pix);
+#define TYPE vpx_img_fmt_t
+#define IMGFMT_TO_PIXFMT ff_vpx_imgfmt_to_pixfmt
+#define PIXFMT_TO_IMGFMT ff_vpx_pixfmt_to_imgfmt
+#include "libaom.h_template.c"
 
 #endif /* AVCODEC_LIBVPX_H */
diff --git a/libavcodec/libvpxdec.c b/libavcodec/libvpxdec.c
index 50f9660..b8c433b 100644
--- a/libavcodec/libvpxdec.c
+++ b/libavcodec/libvpxdec.c
@@ -1,6 +1,4 @@ 
 /*
- * Copyright (c) 2010, Google, Inc.
- *
  * This file is part of Libav.
  *
  * Libav is free software; you can redistribute it and/or
@@ -27,97 +25,33 @@ 
 #include <vpx/vpx_decoder.h>
 #include <vpx/vp8dx.h>
 
-#include "libavutil/common.h"
-#include "libavutil/imgutils.h"
-#include "avcodec.h"
-#include "internal.h"
 #include "libvpx.h"
 
-typedef struct VPXDecodeContext {
-    struct vpx_codec_ctx decoder;
-} VPXDecodeContext;
-
-static av_cold int vpx_init(AVCodecContext *avctx,
-                            const struct vpx_codec_iface *iface)
-{
-    VPXDecodeContext *ctx = avctx->priv_data;
-    struct vpx_codec_dec_cfg deccfg = {
-        /* token partitions+1 would be a decent choice */
-        .threads = FFMIN(avctx->thread_count, 16)
-    };
-
-    av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
-    av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
-
-    if (vpx_codec_dec_init(&ctx->decoder, iface, &deccfg, 0) != VPX_CODEC_OK) {
-        const char *error = vpx_codec_error(&ctx->decoder);
-        av_log(avctx, AV_LOG_ERROR, "Failed to initialize decoder: %s\n",
-               error);
-        return AVERROR(EINVAL);
-    }
-
-    return 0;
-}
-
-static int vpx_decode(AVCodecContext *avctx,
-                      void *data, int *got_frame, AVPacket *avpkt)
-{
-    VPXDecodeContext *ctx = avctx->priv_data;
-    AVFrame *picture = data;
-    const void *iter = NULL;
-    struct vpx_image *img;
-    int ret;
+#define AV1DecodeContext        VPXDecodeContext
 
-    if (vpx_codec_decode(&ctx->decoder, avpkt->data, avpkt->size, NULL, 0) !=
-        VPX_CODEC_OK) {
-        const char *error  = vpx_codec_error(&ctx->decoder);
-        const char *detail = vpx_codec_error_detail(&ctx->decoder);
+#define aom_codec_ctx           vpx_codec_ctx
+#define aom_codec_iface         vpx_codec_iface
+#define aom_codec_dec_cfg       vpx_codec_dec_cfg
+#define aom_codec_version_str   vpx_codec_version_str
+#define aom_codec_build_config  vpx_codec_build_config
+#define aom_codec_dec_init      vpx_codec_dec_init
+#define aom_codec_error         vpx_codec_error
+#define aom_codec_error_detail  vpx_codec_error_detail
+#define aom_codec_decode        vpx_codec_decode
+#define aom_codec_get_frame     vpx_codec_get_frame
+#define aom_codec_destroy       vpx_codec_destroy
+#define aom_image               vpx_image
 
-        av_log(avctx, AV_LOG_ERROR, "Failed to decode frame: %s\n", error);
-        if (detail)
-            av_log(avctx, AV_LOG_ERROR, "  Additional information: %s\n",
-                   detail);
-        return AVERROR_INVALIDDATA;
-    }
+#define AOM_CODEC_OK            VPX_CODEC_OK
 
-    if ((img = vpx_codec_get_frame(&ctx->decoder, &iter))) {
-        avctx->pix_fmt = ff_vpx_imgfmt_to_pixfmt(img->fmt);
-        if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
-            av_log(avctx, AV_LOG_ERROR, "Unsupported output colorspace (%d)\n",
-                   img->fmt);
-            return AVERROR_INVALIDDATA;
-        }
+#define AOM_CR_STUDIO_RANGE     VPX_CR_STUDIO_RANGE
+#define AOM_CR_FULL_RANGE       VPX_CR_FULL_RANGE
 
-        if ((int) img->d_w != avctx->width || (int) img->d_h != avctx->height) {
-            av_log(avctx, AV_LOG_INFO, "dimension change! %dx%d -> %dx%d\n",
-                   avctx->width, avctx->height, img->d_w, img->d_h);
-            ret = ff_set_dimensions(avctx, img->d_w, img->d_h);
-            if (ret < 0)
-                return ret;
-        }
-        if ((ret = ff_get_buffer(avctx, picture, 0)) < 0)
-            return ret;
-        av_image_copy(picture->data, picture->linesize, (const uint8_t **) img->planes,
-                      img->stride, avctx->pix_fmt, img->d_w, img->d_h);
-        switch (img->range) {
-        case VPX_CR_STUDIO_RANGE:
-            picture->color_range = AVCOL_RANGE_MPEG;
-            break;
-        case VPX_CR_FULL_RANGE:
-            picture->color_range = AVCOL_RANGE_JPEG;
-            break;
-        }
-        *got_frame           = 1;
-    }
-    return avpkt->size;
-}
+#define aom_init                vpx_init
+#define aom_decode              vpx_decode
+#define aom_free                vpx_free
 
-static av_cold int vpx_free(AVCodecContext *avctx)
-{
-    VPXDecodeContext *ctx = avctx->priv_data;
-    vpx_codec_destroy(&ctx->decoder);
-    return 0;
-}
+#include "libaomdec_template.c"
 
 #if CONFIG_LIBVPX_VP8_DECODER
 static av_cold int vp8_init(AVCodecContext *avctx)