[FFmpeg-devel] [PATCH 5/5] lavc: add h264 mediacodec decoder

Matthieu Bouron matthieu.bouron at gmail.com
Mon Feb 15 18:52:30 CET 2016


From: Matthieu Bouron <matthieu.bouron at stupeflix.com>

---
 configure                       |    5 +
 libavcodec/Makefile             |    3 +
 libavcodec/allcodecs.c          |    1 +
 libavcodec/mediacodec_wrapper.c | 1522 +++++++++++++++++++++++++++++++++++++++
 libavcodec/mediacodec_wrapper.h |  123 ++++
 libavcodec/mediacodecdec.c      |  753 +++++++++++++++++++
 libavcodec/mediacodecdec.h      |   98 +++
 libavcodec/mediacodecdec_h264.c |  365 ++++++++++
 8 files changed, 2870 insertions(+)
 create mode 100644 libavcodec/mediacodec_wrapper.c
 create mode 100644 libavcodec/mediacodec_wrapper.h
 create mode 100644 libavcodec/mediacodecdec.c
 create mode 100644 libavcodec/mediacodecdec.h
 create mode 100644 libavcodec/mediacodecdec_h264.c

diff --git a/configure b/configure
index db4ebbc..1126f3a 100755
--- a/configure
+++ b/configure
@@ -275,6 +275,7 @@ External library support:
   --enable-libzvbi         enable teletext support via libzvbi [no]
   --disable-lzma           disable lzma [autodetect]
   --enable-decklink        enable Blackmagic DeckLink I/O support [no]
+  --enable-mediacodec      enable Android MediaCodec support [no]
   --enable-mmal            enable decoding via MMAL [no]
   --enable-netcdf          enable NetCDF, needed for sofalizer filter [no]
   --enable-nvenc           enable NVIDIA NVENC support [no]
@@ -1497,6 +1498,7 @@ EXTERNAL_LIBRARY_LIST="
     libzmq
     libzvbi
     lzma
+    mediacodec
     mmal
     netcdf
     nvenc
@@ -2491,6 +2493,8 @@ h264_d3d11va_hwaccel_deps="d3d11va"
 h264_d3d11va_hwaccel_select="h264_decoder"
 h264_dxva2_hwaccel_deps="dxva2"
 h264_dxva2_hwaccel_select="h264_decoder"
+h264_mediacodec_decoder_deps="mediacodec"
+h264_mediacodec_decoder_select="h264_mp4toannexb_bsf h264_parser"
 h264_mmal_decoder_deps="mmal"
 h264_mmal_decoder_select="mmal"
 h264_mmal_hwaccel_deps="mmal"
@@ -5571,6 +5575,7 @@ enabled libzmq            && require_pkg_config libzmq zmq.h zmq_ctx_new
 enabled libzvbi           && require libzvbi libzvbi.h vbi_decoder_new -lzvbi &&
                              { check_cpp_condition libzvbi.h "VBI_VERSION_MAJOR > 0 || VBI_VERSION_MINOR > 2 || VBI_VERSION_MINOR == 2 && VBI_VERSION_MICRO >= 28" ||
                                enabled gpl || die "ERROR: libzvbi requires version 0.2.28 or --enable-gpl."; }
+enabled mediacodec        && { enabled jni || die "ERROR: mediacodec requires --enable-jni"; }
 enabled mmal              && { check_lib interface/mmal/mmal.h mmal_port_connect -lmmal_core -lmmal_util -lmmal_vc_client -lbcm_host ||
                                 { ! enabled cross_compile && {
                                     add_cflags -isystem/opt/vc/include/ -isystem/opt/vc/include/interface/vmcs_host/linux -isystem/opt/vc/include/interface/vcos/pthreads -fgnu89-inline ;
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index f6a4fbb..a102a1d 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -87,6 +87,7 @@ OBJS-$(CONFIG_LSP)                     += lsp.o
 OBJS-$(CONFIG_LZF)                     += lzf.o
 OBJS-$(CONFIG_MDCT)                    += mdct_fixed.o mdct_float.o mdct_fixed_32.o
 OBJS-$(CONFIG_ME_CMP)                  += me_cmp.o
+OBJS-$(CONFIG_MEDIACODEC)              += mediacodecdec.o mediacodec_wrapper.o
 OBJS-$(CONFIG_MPEG_ER)                 += mpeg_er.o
 OBJS-$(CONFIG_MPEGAUDIO)               += mpegaudio.o mpegaudiodata.o   \
                                           mpegaudiodecheader.o
@@ -301,6 +302,7 @@ OBJS-$(CONFIG_H264_DECODER)            += h264.o h264_cabac.o h264_cavlc.o \
                                           h264_direct.o h264_loopfilter.o  \
                                           h264_mb.o h264_picture.o h264_ps.o \
                                           h264_refs.o h264_sei.o h264_slice.o
+OBJS-$(CONFIG_H264_MEDIACODEC_DECODER) += mediacodecdec_h264.o
 OBJS-$(CONFIG_H264_MMAL_DECODER)       += mmaldec.o
 OBJS-$(CONFIG_H264_VDA_DECODER)        += vda_h264_dec.o
 OBJS-$(CONFIG_H264_QSV_DECODER)        += qsvdec_h2645.o
@@ -943,6 +945,7 @@ SKIPHEADERS-$(CONFIG_LIBSCHROEDINGER)  += libschroedinger.h
 SKIPHEADERS-$(CONFIG_LIBUTVIDEO)       += libutvideo.h
 SKIPHEADERS-$(CONFIG_LIBVPX)           += libvpx.h
 SKIPHEADERS-$(CONFIG_LIBWEBP_ENCODER)  += libwebpenc_common.h
+SKIPHEADERS-$(CONFIG_MEDIACODEC)       += mediacodecdec.h mediacodec_wrapper.h
 SKIPHEADERS-$(CONFIG_QSV)              += qsv.h qsv_internal.h
 SKIPHEADERS-$(CONFIG_QSVDEC)           += qsvdec.h
 SKIPHEADERS-$(CONFIG_QSVENC)           += qsvenc.h
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 2097db0..58bcb24 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -196,6 +196,7 @@ void avcodec_register_all(void)
     REGISTER_ENCDEC (H263P,             h263p);
     REGISTER_DECODER(H264,              h264);
     REGISTER_DECODER(H264_CRYSTALHD,    h264_crystalhd);
+    REGISTER_DECODER(H264_MEDIACODEC,   h264_mediacodec);
     REGISTER_DECODER(H264_MMAL,         h264_mmal);
     REGISTER_DECODER(H264_QSV,          h264_qsv);
     REGISTER_DECODER(H264_VDA,          h264_vda);
diff --git a/libavcodec/mediacodec_wrapper.c b/libavcodec/mediacodec_wrapper.c
new file mode 100644
index 0000000..91f1015
--- /dev/null
+++ b/libavcodec/mediacodec_wrapper.c
@@ -0,0 +1,1522 @@
+/*
+ * Android MediaCodec Wrapper
+ *
+ * Copyright (c) Matthieu Bouron <matthieu.bouron stupeflix.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/avassert.h"
+#include "libavutil/jni.h"
+#include "libavutil/jni_internal.h"
+#include "libavutil/mem.h"
+#include "libavutil/avstring.h"
+
+#include "mediacodec_wrapper.h"
+
+struct JNIAMediaCodecListFields {
+
+    jclass mediaformat_class;
+    jmethodID create_video_format_id;
+
+    jclass mediacodec_list_class;
+    jmethodID init_id;
+    jmethodID find_decoder_for_format_id;
+
+    jmethodID get_codec_count_id;
+    jmethodID get_codec_info_at_id;
+
+    jclass mediacodec_info_class;
+    jmethodID get_name_id;
+    jmethodID get_supported_types_id;
+    jmethodID is_encoder_id;
+
+} JNIAMediaCodecListFields;
+
+static const struct FFJniField jfields_mapping[] = {
+    { "android/media/MediaFormat", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecListFields, mediaformat_class), 1 },
+        { "android/media/MediaFormat", "createVideoFormat", "(Ljava/lang/String;II)Landroid/media/MediaFormat;", FF_JNI_STATIC_METHOD, offsetof(struct JNIAMediaCodecListFields, create_video_format_id), 1 },
+
+    { "android/media/MediaCodecList", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecListFields, mediacodec_list_class), 1 },
+        { "android/media/MediaCodecList", "<init>", "(I)V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, init_id), 0 },
+        { "android/media/MediaCodecList", "findDecoderForFormat", "(Landroid/media/MediaFormat;)Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, find_decoder_for_format_id), 0 },
+
+        { "android/media/MediaCodecList", "getCodecCount", "()I", FF_JNI_STATIC_METHOD, offsetof(struct JNIAMediaCodecListFields, get_codec_count_id), 1 },
+        { "android/media/MediaCodecList", "getCodecInfoAt", "(I)Landroid/media/MediaCodecInfo;", FF_JNI_STATIC_METHOD, offsetof(struct JNIAMediaCodecListFields, get_codec_info_at_id), 1 },
+
+    { "android/media/MediaCodecInfo", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecListFields, mediacodec_info_class), 1 },
+        { "android/media/MediaCodecInfo", "getName", "()Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, get_name_id), 1 },
+        { "android/media/MediaCodecInfo", "getSupportedTypes", "()[Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, get_supported_types_id), 1 },
+        { "android/media/MediaCodecInfo", "isEncoder", "()Z", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecListFields, is_encoder_id), 1 },
+
+    { NULL }
+};
+
+#define JNI_ATTACH_ENV_OR_RETURN(env, attached, log_ctx, ret) do { \
+    (env) = avpriv_jni_attach_env(attached, log_ctx);              \
+    if (!(env)) {                                                  \
+        return ret;                                                \
+    }                                                              \
+} while (0)
+
+#define JNI_ATTACH_ENV_OR_RETURN_VOID(env, attached, log_ctx) do { \
+    (env) = avpriv_jni_attach_env(attached, log_ctx);              \
+    if (!(env)) {                                                  \
+        return;                                                    \
+    }                                                              \
+} while (0)
+
+#define JNI_DETACH_ENV(attached, log_ctx) do { \
+    if (attached)                              \
+        avpriv_jni_detach_env(log_ctx);        \
+} while (0)
+
+char *ff_AMediaCodecList_getCodecNameByType(const char *mime, int width, int height)
+{
+    int ret;
+    char *name = NULL;
+    char *supported_type = NULL;
+
+    int attached = 0;
+    JNIEnv *env = NULL;
+    struct JNIAMediaCodecListFields jfields = { 0 };
+
+    jobject format = NULL;
+    jobject codec = NULL;
+    jstring tmp = NULL;
+
+    jobject info = NULL;
+    jobject type = NULL;
+    jobjectArray types = NULL;
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, NULL);
+
+    if ((ret = avpriv_jni_init_jfields(env, &jfields, jfields_mapping, 0, NULL)) < 0) {
+        goto done;
+    }
+
+    if (jfields.init_id && jfields.find_decoder_for_format_id) {
+        tmp = avpriv_jni_utf_chars_to_jstring(env, mime, NULL);
+        if (!tmp) {
+            goto done;
+        }
+
+        format = (*env)->CallStaticObjectMethod(env, jfields.mediaformat_class, jfields.create_video_format_id, tmp, width, height);
+        if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+            goto done;
+        }
+        (*env)->DeleteLocalRef(env, tmp);
+
+        codec = (*env)->NewObject(env, jfields.mediacodec_list_class, jfields.init_id, 0);
+        if (!codec) {
+            av_log(NULL, AV_LOG_ERROR, "Could not create media codec list\n");
+            goto done;
+        }
+
+        tmp = (*env)->CallObjectMethod(env, codec, jfields.find_decoder_for_format_id, format);
+        if (!tmp) {
+            av_log(NULL, AV_LOG_ERROR, "Could not find decoder in media codec list\n");
+            goto done;
+        }
+
+        name = avpriv_jni_jstring_to_utf_chars(env, tmp, NULL);
+        if (!name) {
+            av_log(NULL, AV_LOG_ERROR, "Could not convert jstring to utf chars\n");
+            goto done;
+        }
+
+    } else {
+        int i;
+        int codec_count;
+
+        codec_count = (*env)->CallStaticIntMethod(env, jfields.mediacodec_list_class, jfields.get_codec_count_id);
+        if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+            goto done;
+        }
+
+        for(i = 0; i < codec_count; i++) {
+            int j;
+            int type_count;
+            int is_encoder;
+
+            info = (*env)->CallStaticObjectMethod(env, jfields.mediacodec_list_class, jfields.get_codec_info_at_id, i);
+            if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+                goto done;
+            }
+
+            types = (*env)->CallObjectMethod(env, info, jfields.get_supported_types_id);
+            if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+                goto done;
+            }
+
+            is_encoder = (*env)->CallBooleanMethod(env, info, jfields.is_encoder_id);
+            if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+                goto done;
+            }
+
+            if (is_encoder) {
+                continue;
+            }
+
+            type_count = (*env)->GetArrayLength(env, types);
+            for (j = 0; j < type_count; j++) {
+
+                type = (*env)->GetObjectArrayElement(env, types, j);
+                if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+                    goto done;
+                }
+
+                supported_type = avpriv_jni_jstring_to_utf_chars(env, type, NULL);
+                if (!supported_type) {
+                    av_log(NULL, AV_LOG_ERROR, "Could not convert jstring to utf chars\n");
+                    goto done;
+                }
+
+                if (!av_strcasecmp(supported_type, mime)) {
+                    jobject codec_name;
+
+                    codec_name = (*env)->CallObjectMethod(env, info, jfields.get_name_id);
+                    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+                        goto done;
+                    }
+
+                    name = avpriv_jni_jstring_to_utf_chars(env, codec_name, NULL);
+                    if (!name) {
+                        goto done;
+                    }
+
+                    if (strstr(name, "OMX.google")) {
+                        av_freep(&name);
+                        continue;
+                    }
+                }
+
+                av_freep(&supported_type);
+            }
+
+            (*env)->DeleteLocalRef(env, info);
+            info = NULL;
+
+            (*env)->DeleteLocalRef(env, types);
+            types = NULL;
+
+            if (name)
+                break;
+        }
+    }
+
+done:
+    if (format) {
+        (*env)->DeleteLocalRef(env, format);
+    }
+
+    if (codec) {
+        (*env)->DeleteLocalRef(env, codec);
+    }
+
+    if (tmp) {
+        (*env)->DeleteLocalRef(env, tmp);
+    }
+
+    if (info) {
+        (*env)->DeleteLocalRef(env, info);
+    }
+
+    if (type) {
+        (*env)->DeleteLocalRef(env, type);
+    }
+
+    if (types) {
+        (*env)->DeleteLocalRef(env, types);
+    }
+
+    av_freep(&supported_type);
+
+    avpriv_jni_reset_jfields(env, &jfields, jfields_mapping, 0, NULL);
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    return name;
+}
+
+struct JNIAMediaFormatFields {
+
+    jclass clazz;
+
+    jmethodID init_id;
+
+    jmethodID get_integer_id;
+    jmethodID get_long_id;
+    jmethodID get_float_id;
+    jmethodID get_bytebuffer_id;
+    jmethodID get_string_id;
+
+    jmethodID set_integer_id;
+    jmethodID set_long_id;
+    jmethodID set_float_id;
+    jmethodID set_bytebuffer_id;
+    jmethodID set_string_id;
+
+    jmethodID to_string_id;
+
+} JNIAMediaFormatFields;
+
+static const struct FFJniField jni_amediaformat_mapping[] = {
+    { "android/media/MediaFormat", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaFormatFields, clazz), 1 },
+
+        { "android/media/MediaFormat", "<init>", "()V", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, init_id), 1 },
+
+        { "android/media/MediaFormat", "getInteger", "(Ljava/lang/String;)I", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, get_integer_id), 1 },
+        { "android/media/MediaFormat", "getLong", "(Ljava/lang/String;)J", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, get_long_id), 1 },
+        { "android/media/MediaFormat", "getFloat", "(Ljava/lang/String;)F", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, get_float_id), 1 },
+        { "android/media/MediaFormat", "getByteBuffer", "(Ljava/lang/String;)Ljava/nio/ByteBuffer;", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, get_bytebuffer_id), 1 },
+        { "android/media/MediaFormat", "getString", "(Ljava/lang/String;)Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, get_string_id), 1 },
+
+        { "android/media/MediaFormat", "setInteger", "(Ljava/lang/String;I)V", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, set_integer_id), 1 },
+        { "android/media/MediaFormat", "setLong", "(Ljava/lang/String;J)V", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, set_long_id), 1 },
+        { "android/media/MediaFormat", "setFloat", "(Ljava/lang/String;F)V", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, set_float_id), 1 },
+        { "android/media/MediaFormat", "setByteBuffer", "(Ljava/lang/String;Ljava/nio/ByteBuffer;)V", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, set_bytebuffer_id), 1 },
+        { "android/media/MediaFormat", "setString", "(Ljava/lang/String;Ljava/lang/String;)V", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, set_string_id), 1 },
+
+        { "android/media/MediaFormat", "toString", "()Ljava/lang/String;", FF_JNI_METHOD, offsetof(struct JNIAMediaFormatFields, to_string_id), 1 },
+
+    { NULL }
+};
+
+struct FFAMediaFormat {
+
+    struct JNIAMediaFormatFields jfields;
+
+    jobject object;
+};
+
+FFAMediaFormat *ff_AMediaFormat_new()
+{
+    int attached = 0;
+    JNIEnv *env = NULL;
+    FFAMediaFormat *format = NULL;
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, NULL);
+
+    format = av_mallocz(sizeof(FFAMediaFormat));
+    if (!format) {
+    }
+
+    if (avpriv_jni_init_jfields(env, &format->jfields, jni_amediaformat_mapping, 1, NULL) < 0) {
+        goto fail;
+    }
+
+    format->object = (*env)->NewObject(env, format->jfields.clazz, format->jfields.init_id);
+    if (!format->object) {
+        goto fail;
+    }
+
+    format->object = (*env)->NewGlobalRef(env, format->object);
+    if (!format->object) {
+        goto fail;
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    return format;
+fail:
+    avpriv_jni_reset_jfields(env, &format->jfields, jni_amediaformat_mapping, 1, NULL);
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    av_freep(&format);
+
+    return NULL;
+}
+
+static FFAMediaFormat *ff_AMediaFormat_newFromObject(void *object)
+{
+    int attached = 0;
+    JNIEnv *env = NULL;
+    FFAMediaFormat *format = NULL;
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, NULL);
+
+    format = av_mallocz(sizeof(FFAMediaFormat));
+    if (!format) {
+        return NULL;
+    }
+
+    if (avpriv_jni_init_jfields(env, &format->jfields, jni_amediaformat_mapping, 1, NULL) < 0) {
+        goto fail;
+    }
+
+    format->object = (*env)->NewGlobalRef(env, object);
+    if (!format->object) {
+        goto fail;
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    return format;
+fail:
+    avpriv_jni_reset_jfields(env, &format->jfields, jni_amediaformat_mapping, 1, NULL);
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    av_freep(&format);
+
+    return NULL;
+}
+
+
+int ff_AMediaFormat_delete(FFAMediaFormat* format)
+{
+    int ret = 0;
+
+    int attached = 0;
+    JNIEnv *env = NULL;
+
+    if (!format) {
+        return 0;
+    }
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, AVERROR_EXTERNAL);
+
+    (*env)->DeleteGlobalRef(env, format->object);
+    format->object = NULL;
+
+    avpriv_jni_reset_jfields(env, &format->jfields, jni_amediaformat_mapping, 1, NULL);
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    av_freep(&format);
+
+    return ret;
+}
+
+char* ff_AMediaFormat_toString(FFAMediaFormat* format)
+{
+    char *ret = NULL;
+
+    int attached = 0;
+    JNIEnv *env = NULL;
+    jstring description = NULL;
+
+    av_assert0(format != NULL);
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, NULL);
+
+    description = (*env)->CallObjectMethod(env, format->object, format->jfields.to_string_id);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        goto fail;
+    }
+
+    ret = avpriv_jni_jstring_to_utf_chars(env, description, NULL);
+fail:
+
+    if (description) {
+        (*env)->DeleteLocalRef(env, description);
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    return ret;
+}
+
+int ff_AMediaFormat_getInt32(FFAMediaFormat* format, const char *name, int32_t *out)
+{
+    int ret = 1;
+
+    int attached = 0;
+    JNIEnv *env = NULL;
+    jstring key = NULL;
+
+    av_assert0(format != NULL);
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, 0);
+
+    key = avpriv_jni_utf_chars_to_jstring(env, name, NULL);
+    if (!key) {
+        ret = 0;
+        goto fail;
+    }
+
+    *out = (*env)->CallIntMethod(env, format->object, format->jfields.get_integer_id, key);
+    if ((ret = avpriv_jni_exception_check(env, 1, NULL)) < 0) {
+        ret = 0;
+        goto fail;
+    }
+
+    ret = 1;
+fail:
+    if (key) {
+        (*env)->DeleteLocalRef(env, key);
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    return ret;
+}
+
+int ff_AMediaFormat_getInt64(FFAMediaFormat* format, const char *name, int64_t *out)
+{
+    int ret = 1;
+
+    int attached = 0;
+    JNIEnv *env = NULL;
+    jstring key = NULL;
+
+    av_assert0(format != NULL);
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, 0);
+
+    key = avpriv_jni_utf_chars_to_jstring(env, name, NULL);
+    if (!key) {
+        ret = 0;
+        goto fail;
+    }
+
+    *out = (*env)->CallLongMethod(env, format->object, format->jfields.get_long_id, key);
+    if ((ret = avpriv_jni_exception_check(env, 1, NULL)) < 0) {
+        ret = 0;
+        goto fail;
+    }
+
+    ret = 1;
+fail:
+    if (key) {
+        (*env)->DeleteLocalRef(env, key);
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    return ret;
+}
+
+int ff_AMediaFormat_getFloat(FFAMediaFormat* format, const char *name, float *out)
+{
+    int ret = 1;
+
+    int attached = 0;
+    JNIEnv *env = NULL;
+    jstring key = NULL;
+
+    av_assert0(format != NULL);
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, 0);
+
+    key = avpriv_jni_utf_chars_to_jstring(env, name, NULL);
+    if (!key) {
+        ret = 0;
+        goto fail;
+    }
+
+    *out = (*env)->CallFloatMethod(env, format->object, format->jfields.get_float_id, key);
+    if ((ret = avpriv_jni_exception_check(env, 1, NULL)) < 0) {
+        ret = 0;
+        goto fail;
+    }
+
+    ret = 1;
+fail:
+    if (key) {
+        (*env)->DeleteLocalRef(env, key);
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    return ret;
+}
+
+int ff_AMediaFormat_getBuffer(FFAMediaFormat* format, const char *name, void** data, size_t *size)
+{
+    int ret = 1;
+
+    int attached = 0;
+    JNIEnv *env = NULL;
+    jstring key = NULL;
+    jobject result = NULL;
+
+    av_assert0(format != NULL);
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, 0);
+
+    key = avpriv_jni_utf_chars_to_jstring(env, name, NULL);
+    if (!key) {
+        ret = 0;
+        goto fail;
+    }
+
+    result = (*env)->CallObjectMethod(env, format->object, format->jfields.get_bytebuffer_id, key);
+    if ((ret = avpriv_jni_exception_check(env, 1, NULL)) < 0) {
+        ret = 0;
+        goto fail;
+    }
+
+    *data = (*env)->GetDirectBufferAddress(env, result);
+    *size = (*env)->GetDirectBufferCapacity(env, result);
+
+    if (*data && *size) {
+        void *src = *data;
+        *data = av_malloc(*size);
+        if (!*data) {
+            ret = 0;
+            goto fail;
+        }
+
+        memcpy(*data, src, *size);
+    }
+
+    ret = 1;
+fail:
+    if (key) {
+        (*env)->DeleteLocalRef(env, key);
+    }
+
+    if (result) {
+        (*env)->DeleteLocalRef(env, result);
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    return ret;
+}
+
+int ff_AMediaFormat_getString(FFAMediaFormat* format, const char *name, const char **out)
+{
+    int ret = 1;
+
+    int attached = 0;
+    JNIEnv *env = NULL;
+    jstring key = NULL;
+    jstring result = NULL;
+
+    av_assert0(format != NULL);
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, 0);
+
+    key = avpriv_jni_utf_chars_to_jstring(env, name, NULL);
+    if (!key) {
+        ret = 0;
+        goto fail;
+    }
+
+    result = (*env)->CallObjectMethod(env, format->object, format->jfields.get_string_id, key);
+    if ((ret = avpriv_jni_exception_check(env, 1, NULL)) < 0) {
+        ret = 0;
+        goto fail;
+    }
+
+    *out = avpriv_jni_jstring_to_utf_chars(env, result, NULL);
+    if (!*out) {
+        ret = 0;
+        goto fail;
+    }
+
+    ret = 1;
+fail:
+    if (key) {
+        (*env)->DeleteLocalRef(env, key);
+    }
+
+    if (result) {
+        (*env)->DeleteLocalRef(env, result);
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    return ret;
+}
+
+
+void ff_AMediaFormat_setInt32(FFAMediaFormat* format, const char* name, int32_t value)
+{
+    int attached = 0;
+    JNIEnv *env = NULL;
+    jstring key = NULL;
+
+    av_assert0(format != NULL);
+
+    JNI_ATTACH_ENV_OR_RETURN_VOID(env, &attached, NULL);
+
+    key = avpriv_jni_utf_chars_to_jstring(env, name, NULL);
+    if (!key) {
+        goto fail;
+    }
+
+    (*env)->CallVoidMethod(env, format->object, format->jfields.set_integer_id, key, value);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        goto fail;
+    }
+
+fail:
+    if (key) {
+        (*env)->DeleteLocalRef(env, key);
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+}
+
+void ff_AMediaFormat_setInt64(FFAMediaFormat* format, const char* name, int64_t value)
+{
+    int attached = 0;
+    JNIEnv *env = NULL;
+    jstring key = NULL;
+
+    av_assert0(format != NULL);
+
+    JNI_ATTACH_ENV_OR_RETURN_VOID(env, &attached, NULL);
+
+    key = avpriv_jni_utf_chars_to_jstring(env, name, NULL);
+    if (!key) {
+        goto fail;
+    }
+
+    (*env)->CallVoidMethod(env, format->object, format->jfields.set_long_id, key, value);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        goto fail;
+    }
+
+fail:
+    if (key) {
+        (*env)->DeleteLocalRef(env, key);
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+}
+
+void ff_AMediaFormat_setFloat(FFAMediaFormat* format, const char* name, float value)
+{
+    int attached = 0;
+    JNIEnv *env = NULL;
+    jstring key = NULL;
+
+    av_assert0(format != NULL);
+
+    JNI_ATTACH_ENV_OR_RETURN_VOID(env, &attached, NULL);
+
+    key = avpriv_jni_utf_chars_to_jstring(env, name, NULL);
+    if (!key) {
+        goto fail;
+    }
+
+    (*env)->CallVoidMethod(env, format->object, format->jfields.set_float_id, key, value);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        goto fail;
+    }
+
+fail:
+    if (key) {
+        (*env)->DeleteLocalRef(env, key);
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+}
+
+void ff_AMediaFormat_setString(FFAMediaFormat* format, const char* name, const char* value)
+{
+    int attached = 0;
+    JNIEnv *env = NULL;
+    jstring key = NULL;
+    jstring string = NULL;
+
+    av_assert0(format != NULL);
+
+    JNI_ATTACH_ENV_OR_RETURN_VOID(env, &attached, NULL);
+
+    key = avpriv_jni_utf_chars_to_jstring(env, name, NULL);
+    if (!key) {
+        goto fail;
+    }
+
+    string = avpriv_jni_utf_chars_to_jstring(env, value, NULL);
+    if (!string) {
+        goto fail;
+    }
+
+    (*env)->CallVoidMethod(env, format->object, format->jfields.set_string_id, key, string);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        goto fail;
+    }
+
+fail:
+    if (key) {
+        (*env)->DeleteLocalRef(env, key);
+    }
+
+    if (string) {
+        (*env)->DeleteLocalRef(env, string);
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+}
+
+void ff_AMediaFormat_setBuffer(FFAMediaFormat* format, const char* name, void* data, size_t size)
+{
+    int attached = 0;
+    JNIEnv *env = NULL;
+    jstring key = NULL;
+    jobject buffer = NULL;
+    void *buffer_data = NULL;
+
+    av_assert0(format != NULL);
+
+    JNI_ATTACH_ENV_OR_RETURN_VOID(env, &attached, NULL);
+
+    key = avpriv_jni_utf_chars_to_jstring(env, name, NULL);
+    if (!key) {
+        goto fail;
+    }
+
+    if (!data || !size) {
+        goto fail;
+    }
+
+    buffer_data = av_malloc(size);
+    if (!buffer_data) {
+        goto fail;
+    }
+
+    memcpy(buffer_data, data, size);
+
+    buffer = (*env)->NewDirectByteBuffer(env, buffer_data, size);
+    if (!buffer) {
+        goto fail;
+    }
+
+    (*env)->CallVoidMethod(env, format->object, format->jfields.set_bytebuffer_id, key, buffer);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        goto fail;
+    }
+
+fail:
+    if (key) {
+        (*env)->DeleteLocalRef(env, key);
+    }
+
+    if (buffer) {
+        (*env)->DeleteLocalRef(env, buffer);
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+}
+
+struct JNIAMediaCodecFields {
+
+    jclass mediacodec_class;
+
+    jfieldID info_try_again_later_id;
+    jfieldID info_output_buffers_changed_id;
+    jfieldID info_output_format_changed_id;
+
+    jmethodID create_by_codec_name_id;
+    jmethodID create_decoder_by_type_id;
+    jmethodID create_encoder_by_type_id;
+
+    jmethodID configure_id;
+    jmethodID start_id;
+    jmethodID flush_id;
+    jmethodID stop_id;
+    jmethodID release_id;
+
+    jmethodID get_output_format_id;
+
+    jmethodID dequeue_input_buffer_id;
+    jmethodID queue_input_buffer_id;
+    jmethodID get_input_buffer_id;
+    jmethodID get_input_buffers_id;
+
+    jmethodID dequeue_output_buffer_id;
+    jmethodID get_output_buffer_id;
+    jmethodID get_output_buffers_id;
+    jmethodID release_output_buffer_id;
+    jmethodID release_output_buffer_at_time_id;
+
+    jclass mediainfo_class;
+
+    jmethodID init_id;
+
+    jfieldID flags_id;
+    jfieldID offset_id;
+    jfieldID presentation_time_us_id;
+    jfieldID size_id;
+
+} JNIAMediaCodecFields;
+
+static const struct FFJniField jni_amediacodec_mapping[] = {
+    { "android/media/MediaCodec", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecFields, mediacodec_class), 1 },
+
+        { "android/media/MediaCodec", "INFO_TRY_AGAIN_LATER", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecFields, info_try_again_later_id), 1 },
+        { "android/media/MediaCodec", "INFO_OUTPUT_BUFFERS_CHANGED", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecFields, info_output_buffers_changed_id), 1 },
+        { "android/media/MediaCodec", "INFO_OUTPUT_FORMAT_CHANGED", "I", FF_JNI_STATIC_FIELD, offsetof(struct JNIAMediaCodecFields, info_output_format_changed_id), 1 },
+
+        { "android/media/MediaCodec", "createByCodecName", "(Ljava/lang/String;)Landroid/media/MediaCodec;", FF_JNI_STATIC_METHOD, offsetof(struct JNIAMediaCodecFields, create_by_codec_name_id), 1 },
+        { "android/media/MediaCodec", "createDecoderByType", "(Ljava/lang/String;)Landroid/media/MediaCodec;", FF_JNI_STATIC_METHOD, offsetof(struct JNIAMediaCodecFields, create_decoder_by_type_id), 1 },
+        { "android/media/MediaCodec", "createEncoderByType", "(Ljava/lang/String;)Landroid/media/MediaCodec;", FF_JNI_STATIC_METHOD, offsetof(struct JNIAMediaCodecFields, create_encoder_by_type_id), 1 },
+
+        { "android/media/MediaCodec", "configure", "(Landroid/media/MediaFormat;Landroid/view/Surface;Landroid/media/MediaCrypto;I)V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, configure_id), 1 },
+        { "android/media/MediaCodec", "start", "()V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, start_id), 1 },
+        { "android/media/MediaCodec", "flush", "()V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, flush_id), 1 },
+        { "android/media/MediaCodec", "stop", "()V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, stop_id), 1 },
+        { "android/media/MediaCodec", "release", "()V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, release_id), 1 },
+
+        { "android/media/MediaCodec", "getOutputFormat", "()Landroid/media/MediaFormat;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, get_output_format_id), 1 },
+
+        { "android/media/MediaCodec", "dequeueInputBuffer", "(J)I", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, dequeue_input_buffer_id), 1 },
+        { "android/media/MediaCodec", "queueInputBuffer", "(IIIJI)V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, queue_input_buffer_id), 1 },
+        { "android/media/MediaCodec", "getInputBuffer", "(I)Ljava/nio/ByteBuffer;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, get_input_buffer_id), 0 },
+        { "android/media/MediaCodec", "getInputBuffers", "()[Ljava/nio/ByteBuffer;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, get_input_buffers_id), 1 },
+
+
+        { "android/media/MediaCodec", "dequeueOutputBuffer", "(Landroid/media/MediaCodec$BufferInfo;J)I", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, dequeue_output_buffer_id), 1 },
+        { "android/media/MediaCodec", "getOutputBuffer", "(I)Ljava/nio/ByteBuffer;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, get_output_buffer_id), 0 },
+        { "android/media/MediaCodec", "getOutputBuffers", "()[Ljava/nio/ByteBuffer;", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, get_output_buffers_id), 1 },
+        { "android/media/MediaCodec", "releaseOutputBuffer", "(IZ)V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, release_output_buffer_id), 1 },
+        { "android/media/MediaCodec", "releaseOutputBuffer", "(IJ)V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, release_output_buffer_at_time_id), 0 },
+
+    { "android/media/MediaCodec$BufferInfo", NULL, NULL, FF_JNI_CLASS, offsetof(struct JNIAMediaCodecFields, mediainfo_class), 1 },
+
+        { "android/media/MediaCodec.BufferInfo", "<init>", "()V", FF_JNI_METHOD, offsetof(struct JNIAMediaCodecFields, init_id), 1 },
+        { "android/media/MediaCodec.BufferInfo", "flags", "I", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecFields, flags_id), 1 },
+        { "android/media/MediaCodec.BufferInfo", "offset", "I", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecFields, offset_id), 1 },
+        { "android/media/MediaCodec.BufferInfo", "presentationTimeUs", "J", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecFields, presentation_time_us_id), 1 },
+        { "android/media/MediaCodec.BufferInfo", "size", "I", FF_JNI_FIELD, offsetof(struct JNIAMediaCodecFields, size_id), 1 },
+
+    { NULL }
+};
+
+struct FFAMediaCodec {
+
+    struct JNIAMediaCodecFields jfields;
+
+    jobject object;
+
+    jobject input_buffers;
+    jobject output_buffers;
+
+    int INFO_TRY_AGAIN_LATER;
+    int INFO_OUTPUT_BUFFERS_CHANGED;
+    int INFO_OUTPUT_FORMAT_CHANGED;
+
+    int has_get_i_o_buffer;
+};
+
+FFAMediaCodec* ff_AMediaCodec_createCodecByName(const char *name)
+{
+    int attached = 0;
+    JNIEnv *env = NULL;
+    FFAMediaCodec *codec = NULL;
+    jstring codec_name = NULL;
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, NULL);
+
+    codec = av_mallocz(sizeof(FFAMediaCodec));
+    if (!codec) {
+        return NULL;
+    }
+
+    if (avpriv_jni_init_jfields(env, &codec->jfields, jni_amediacodec_mapping, 1, NULL) < 0) {
+        goto fail;
+    }
+
+    codec_name = avpriv_jni_utf_chars_to_jstring(env, name, NULL);
+    if (!codec_name) {
+        goto fail;
+    }
+
+    codec->object = (*env)->CallStaticObjectMethod(env, codec->jfields.mediacodec_class, codec->jfields.create_by_codec_name_id, codec_name);
+    if (!codec->object) {
+        goto fail;
+    }
+
+    codec->object = (*env)->NewGlobalRef(env, codec->object);
+    if (!codec->object) {
+        goto fail;
+    }
+
+    codec->INFO_TRY_AGAIN_LATER = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_try_again_later_id);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        goto fail;
+    }
+
+    codec->INFO_OUTPUT_BUFFERS_CHANGED = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_output_buffers_changed_id);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        goto fail;
+    }
+
+    codec->INFO_OUTPUT_FORMAT_CHANGED = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_output_format_changed_id);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        goto fail;
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    return codec;
+fail:
+    avpriv_jni_reset_jfields(env, &codec->jfields, jni_amediacodec_mapping, 1, NULL);
+
+    if (codec_name) {
+        (*env)->DeleteLocalRef(env, codec_name);
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    av_freep(&codec);
+
+    return NULL;
+}
+
+FFAMediaCodec* ff_AMediaCodec_createDecoderByType(const char *mime)
+{
+    int attached = 0;
+    JNIEnv *env = NULL;
+    FFAMediaCodec *codec = NULL;
+    jstring mime_type = NULL;
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, NULL);
+
+    codec = av_mallocz(sizeof(FFAMediaCodec));
+    if (!codec) {
+        return NULL;
+    }
+
+    if (avpriv_jni_init_jfields(env, &codec->jfields, jni_amediacodec_mapping, 1, NULL) < 0) {
+        goto fail;
+    }
+
+    mime_type = avpriv_jni_utf_chars_to_jstring(env, mime, NULL);
+    if (!mime_type) {
+        goto fail;
+    }
+
+    codec->object = (*env)->CallStaticObjectMethod(env, codec->jfields.mediacodec_class, codec->jfields.create_decoder_by_type_id, mime_type);
+    if (!codec->object) {
+        goto fail;
+    }
+
+    codec->object = (*env)->NewGlobalRef(env, codec->object);
+    if (!codec->object) {
+        goto fail;
+    }
+
+    codec->INFO_TRY_AGAIN_LATER = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_try_again_later_id);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        goto fail;
+    }
+
+    codec->INFO_OUTPUT_BUFFERS_CHANGED = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_output_buffers_changed_id);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        goto fail;
+    }
+
+    codec->INFO_OUTPUT_FORMAT_CHANGED = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_output_format_changed_id);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        goto fail;
+    }
+
+    if (codec->jfields.get_input_buffer_id && codec->jfields.get_output_buffer_id) {
+        codec->has_get_i_o_buffer = 1;
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    return codec;
+fail:
+    avpriv_jni_reset_jfields(env, &codec->jfields, jni_amediacodec_mapping, 1, NULL);
+
+    if (mime_type) {
+        (*env)->DeleteLocalRef(env, mime_type);
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    av_freep(&codec);
+
+    return NULL;
+}
+
+FFAMediaCodec* ff_AMediaCodec_createEncoderByType(const char *mime)
+{
+    int attached = 0;
+    JNIEnv *env = NULL;
+    FFAMediaCodec *codec = NULL;
+    jstring mime_type = NULL;
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, NULL);
+
+    codec = av_mallocz(sizeof(FFAMediaCodec));
+    if (!codec) {
+    }
+
+    if (avpriv_jni_init_jfields(env, &codec->jfields, jni_amediacodec_mapping, 1, NULL) < 0) {
+        goto fail;
+    }
+
+    mime_type = avpriv_jni_utf_chars_to_jstring(env, mime, NULL);
+    if (!mime_type) {
+        goto fail;
+    }
+
+    codec->object = (*env)->CallStaticObjectMethod(env, codec->jfields.mediacodec_class, codec->jfields.create_encoder_by_type_id, mime_type);
+    if (!codec->object) {
+        goto fail;
+    }
+
+    codec->object = (*env)->NewGlobalRef(env, codec->object);
+    if (!codec->object) {
+        goto fail;
+    }
+
+    codec->INFO_TRY_AGAIN_LATER = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_try_again_later_id);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        goto fail;
+    }
+
+    codec->INFO_OUTPUT_BUFFERS_CHANGED = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_output_buffers_changed_id);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        goto fail;
+    }
+
+    codec->INFO_OUTPUT_FORMAT_CHANGED = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_output_format_changed_id);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        goto fail;
+    }
+
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    return codec;
+fail:
+    avpriv_jni_reset_jfields(env, &codec->jfields, jni_amediacodec_mapping, 1, NULL);
+
+    if (mime_type) {
+        (*env)->DeleteLocalRef(env, mime_type);
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    av_freep(&codec);
+
+    return NULL;
+}
+
+
+int ff_AMediaCodec_delete(FFAMediaCodec* codec)
+{
+    int ret = 0;
+
+    int attached = 0;
+    JNIEnv *env = NULL;
+
+    if (!codec) {
+        return 0;
+    }
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, AVERROR_EXTERNAL);
+
+    (*env)->CallVoidMethod(env, codec->object, codec->jfields.release_id);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        ret = AVERROR_EXTERNAL;
+    }
+
+    (*env)->DeleteGlobalRef(env, codec->object);
+    codec->object = NULL;
+
+    avpriv_jni_reset_jfields(env, &codec->jfields, jni_amediacodec_mapping, 1, NULL);
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    av_freep(&codec);
+
+    return ret;
+}
+
+
+int ff_AMediaCodec_configure(FFAMediaCodec* codec, const FFAMediaFormat* format, void* surface, void *crypto, uint32_t flags)
+{
+    int ret = 0;
+    int attached = 0;
+    JNIEnv *env = NULL;
+
+    /* TODO: implement surface handling */
+    av_assert0(surface == NULL);
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, AVERROR_EXTERNAL);
+
+    (*env)->CallVoidMethod(env, codec->object, codec->jfields.configure_id, format->object, NULL, NULL, flags);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+
+fail:
+    JNI_DETACH_ENV(attached, NULL);
+
+    return ret;
+}
+
+int ff_AMediaCodec_start(FFAMediaCodec* codec)
+{
+    int ret = 0;
+    int attached = 0;
+    JNIEnv *env = NULL;
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, AVERROR_EXTERNAL);
+
+    (*env)->CallVoidMethod(env, codec->object, codec->jfields.start_id);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+
+fail:
+    JNI_DETACH_ENV(attached, NULL);
+
+    return ret;
+}
+
+int ff_AMediaCodec_stop(FFAMediaCodec* codec)
+{
+    int ret = 0;
+    int attached = 0;
+    JNIEnv *env = NULL;
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, AVERROR_EXTERNAL);
+
+    (*env)->CallVoidMethod(env, codec->object, codec->jfields.stop_id);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+
+fail:
+    JNI_DETACH_ENV(attached, NULL);
+
+    return ret;
+}
+
+int ff_AMediaCodec_flush(FFAMediaCodec* codec)
+{
+    int ret = 0;
+    int attached = 0;
+    JNIEnv *env = NULL;
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, AVERROR_EXTERNAL);
+
+    (*env)->CallVoidMethod(env, codec->object, codec->jfields.flush_id);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+
+fail:
+    JNI_DETACH_ENV(attached, NULL);
+
+    return ret;
+}
+
+
+int ff_AMediaCodec_releaseOutputBuffer(FFAMediaCodec* codec, size_t idx, int render)
+{
+    int ret = 0;
+    int attached = 0;
+    JNIEnv *env = NULL;
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, AVERROR_EXTERNAL);
+
+    (*env)->CallVoidMethod(env, codec->object, codec->jfields.release_output_buffer_id, idx, render);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+
+fail:
+    JNI_DETACH_ENV(attached, NULL);
+
+    return ret;
+}
+
+int ff_AMediaCodec_releaseOutputBufferAtTime(FFAMediaCodec *codec, size_t idx, int64_t timestampNs)
+{
+    int ret = 0;
+    int attached = 0;
+    JNIEnv *env = NULL;
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, AVERROR_EXTERNAL);
+
+    (*env)->CallVoidMethod(env, codec->object, codec->jfields.release_output_buffer_at_time_id, idx, timestampNs);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+
+fail:
+    JNI_DETACH_ENV(attached, NULL);
+
+    return ret;
+}
+
+
+ssize_t ff_AMediaCodec_dequeueInputBuffer(FFAMediaCodec* codec, int64_t timeoutUs)
+{
+    int ret = 0;
+    int attached = 0;
+    JNIEnv *env = NULL;
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, AVERROR_EXTERNAL);
+
+    ret = (*env)->CallIntMethod(env, codec->object, codec->jfields.dequeue_input_buffer_id, timeoutUs);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+
+fail:
+    JNI_DETACH_ENV(attached, NULL);
+
+    return ret;
+}
+
+int ff_AMediaCodec_queueInputBuffer(FFAMediaCodec* codec, size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags)
+{
+    int ret = 0;
+    int attached = 0;
+    JNIEnv *env = NULL;
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, AVERROR_EXTERNAL);
+
+    (*env)->CallVoidMethod(env, codec->object, codec->jfields.queue_input_buffer_id, idx, offset, size, time, flags);
+    if ((ret = avpriv_jni_exception_check(env, 1, NULL)) < 0) {
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+
+fail:
+    JNI_DETACH_ENV(attached, NULL);
+
+    return ret;
+}
+
+ssize_t ff_AMediaCodec_dequeueOutputBuffer(FFAMediaCodec* codec, FFAMediaCodecBufferInfo *info, int64_t timeoutUs)
+{
+    int ret = 0;
+    int attached = 0;
+    JNIEnv *env = NULL;
+
+    jobject mediainfo = NULL;
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, AVERROR_EXTERNAL);
+
+    mediainfo = (*env)->NewObject(env, codec->jfields.mediainfo_class, codec->jfields.init_id);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+
+    ret = (*env)->CallIntMethod(env, codec->object, codec->jfields.dequeue_output_buffer_id, mediainfo, timeoutUs);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+
+    info->flags = (*env)->GetIntField(env, mediainfo, codec->jfields.flags_id);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+
+    info->offset = (*env)->GetIntField(env, mediainfo, codec->jfields.offset_id);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+
+    info->presentationTimeUs = (*env)->GetLongField(env, mediainfo, codec->jfields.presentation_time_us_id);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+
+    info->size = (*env)->GetIntField(env, mediainfo, codec->jfields.size_id);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+fail:
+    if (mediainfo) {
+        (*env)->DeleteLocalRef(env, mediainfo);
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    return ret;
+}
+
+uint8_t* ff_AMediaCodec_getInputBuffer(FFAMediaCodec* codec, size_t idx, size_t *out_size)
+{
+    uint8_t *ret = NULL;
+    int attached = 0;
+    JNIEnv *env = NULL;
+
+    jobject buffer = NULL;
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, NULL);
+
+    if (codec->has_get_i_o_buffer) {
+        buffer = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_input_buffer_id, idx);
+        if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+            goto fail;
+        }
+    } else {
+        if (!codec->input_buffers) {
+            codec->input_buffers = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_input_buffers_id);
+            if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+                goto fail;
+            }
+
+            codec->input_buffers = (*env)->NewGlobalRef(env, codec->input_buffers);
+            if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+                goto fail;
+            }
+        }
+
+        buffer = (*env)->GetObjectArrayElement(env, codec->input_buffers, idx);
+        if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+            goto fail;
+        }
+    }
+
+    ret = (*env)->GetDirectBufferAddress(env, buffer);
+    *out_size = (*env)->GetDirectBufferCapacity(env, buffer);
+fail:
+    if (buffer) {
+        (*env)->DeleteLocalRef(env, buffer);
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    return ret;
+}
+
+uint8_t* ff_AMediaCodec_getOutputBuffer(FFAMediaCodec* codec, size_t idx, size_t *out_size)
+{
+    uint8_t *ret = NULL;
+    int attached = 0;
+    JNIEnv *env = NULL;
+
+    jobject buffer = NULL;
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, NULL);
+
+    if (codec->has_get_i_o_buffer) {
+        buffer = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_output_buffer_id, idx);
+        if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+            goto fail;
+        }
+    } else {
+        if (!codec->output_buffers) {
+            codec->output_buffers = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_output_buffers_id);
+            if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+                goto fail;
+            }
+
+            codec->output_buffers = (*env)->NewGlobalRef(env, codec->output_buffers);
+            if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+                goto fail;
+            }
+        }
+
+        buffer = (*env)->GetObjectArrayElement(env, codec->output_buffers, idx);
+        if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+            goto fail;
+        }
+    }
+
+    ret = (*env)->GetDirectBufferAddress(env, buffer);
+    *out_size = (*env)->GetDirectBufferCapacity(env, buffer);
+fail:
+    if (buffer) {
+        (*env)->DeleteLocalRef(env, buffer);
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    return ret;
+}
+
+FFAMediaFormat* ff_AMediaCodec_getOutputFormat(FFAMediaCodec* codec)
+{
+    FFAMediaFormat *ret = NULL;
+    int attached = 0;
+    JNIEnv *env = NULL;
+
+    jobject mediaformat = NULL;
+
+    JNI_ATTACH_ENV_OR_RETURN(env, &attached, NULL, NULL);
+
+    mediaformat = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_output_format_id);
+    if (avpriv_jni_exception_check(env, 1, NULL) < 0) {
+        goto fail;
+    }
+
+    ret = ff_AMediaFormat_newFromObject(mediaformat);
+fail:
+    if (mediaformat) {
+        (*env)->DeleteLocalRef(env, mediaformat);
+    }
+
+    JNI_DETACH_ENV(attached, NULL);
+
+    return ret;
+}
+
+int ff_AMediaCodec_infoTryAgainLater(FFAMediaCodec *codec, ssize_t idx)
+{
+    return idx == codec->INFO_TRY_AGAIN_LATER;
+}
+
+int ff_AMediaCodec_infoOutputBuffersChanged(FFAMediaCodec *codec, ssize_t idx)
+{
+    return idx == codec->INFO_OUTPUT_BUFFERS_CHANGED;
+}
+
+int ff_AMediaCodec_infoOutputFormatChanged(FFAMediaCodec *codec, ssize_t idx)
+{
+    return idx == codec->INFO_OUTPUT_FORMAT_CHANGED;
+}
+
+int ff_AMediaCodec_cleanOutputBuffers(FFAMediaCodec *codec)
+{
+    int ret = 0;
+
+    if (!codec->has_get_i_o_buffer) {
+        if (codec->output_buffers) {
+            int attached = 0;
+            JNIEnv *env = NULL;
+
+            env = avpriv_jni_attach_env(&attached, NULL);
+            if (!env) {
+                ret = AVERROR_EXTERNAL;
+                goto fail;
+            }
+
+            (*env)->DeleteGlobalRef(env, codec->output_buffers);
+            codec->output_buffers = NULL;
+
+            JNI_DETACH_ENV(attached, NULL);
+        }
+    }
+
+fail:
+    return ret;
+}
diff --git a/libavcodec/mediacodec_wrapper.h b/libavcodec/mediacodec_wrapper.h
new file mode 100644
index 0000000..f488d4e
--- /dev/null
+++ b/libavcodec/mediacodec_wrapper.h
@@ -0,0 +1,123 @@
+/*
+ * Android MediaCodec Wrapper
+ *
+ * Copyright (c) Matthieu Bouron <matthieu.bouron stupeflix.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 FF_MEDIACODEC_H
+#define FF_MEDIACODEC_H
+
+#include <sys/types.h>
+
+/**
+ * The following API around MediaCodec and MediaFormat is based on the
+ * NDK one provided by Google since Android 5.0.
+ *
+ * Differences from the NDK API:
+ *
+ * Buffers returned by ff_AMediaFormat_toString and ff_AMediaFormat_getString
+ * are newly allocated buffer and must be freed by the user after use.
+ *
+ * The MediaCrypto API is not implemented.
+ *
+ * ff_AMediaCodec_infoTryAgainLater, ff_AMediaCodec_infoOutputBuffersChanged,
+ * ff_AMediaCodec_infoOutputFormatChanged and ff_AMediaCodec_cleanOutputBuffers
+ * are not part of the original NDK API and are convenience functions to hide
+ * JNI implementation.
+ *
+ * The API around MediaCodecList is not part of the NDK (and is lacking as
+ * we still need to retreive the codec name to work around faulty decoders
+ * and encoders).
+ *
+ * For documentation, please refers to NdkMediaCodec.h NdkMediaFormat.h and
+ * http://developer.android.com/reference/android/media/MediaCodec.html.
+ *
+ */
+
+char *ff_AMediaCodecList_getCodecNameByType(const char *mime, int width, int height);
+
+struct FFAMediaFormat;
+typedef struct FFAMediaFormat FFAMediaFormat;
+
+FFAMediaFormat *ff_AMediaFormat_new(void);
+int ff_AMediaFormat_delete(FFAMediaFormat* format);
+
+char* ff_AMediaFormat_toString(FFAMediaFormat* format);
+
+int ff_AMediaFormat_getInt32(FFAMediaFormat* format, const char *name, int32_t *out);
+int ff_AMediaFormat_getInt64(FFAMediaFormat* format, const char *name, int64_t *out);
+int ff_AMediaFormat_getFloat(FFAMediaFormat* format, const char *name, float *out);
+int ff_AMediaFormat_getBuffer(FFAMediaFormat* format, const char *name, void** data, size_t *size);
+int ff_AMediaFormat_getString(FFAMediaFormat* format, const char *name, const char **out);
+
+void ff_AMediaFormat_setInt32(FFAMediaFormat* format, const char* name, int32_t value);
+void ff_AMediaFormat_setInt64(FFAMediaFormat* format, const char* name, int64_t value);
+void ff_AMediaFormat_setFloat(FFAMediaFormat* format, const char* name, float value);
+void ff_AMediaFormat_setString(FFAMediaFormat* format, const char* name, const char* value);
+void ff_AMediaFormat_setBuffer(FFAMediaFormat* format, const char* name, void* data, size_t size);
+
+struct FFAMediaCodec;
+typedef struct FFAMediaCodec FFAMediaCodec;
+typedef struct FFAMediaCodecCryptoInfo FFAMediaCodecCryptoInfo;
+
+struct FFAMediaCodecBufferInfo {
+    int32_t offset;
+    int32_t size;
+    int64_t presentationTimeUs;
+    uint32_t flags;
+};
+typedef struct FFAMediaCodecBufferInfo FFAMediaCodecBufferInfo;
+
+enum {
+    FF_AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM = 4,
+    FF_AMEDIACODEC_CONFIGURE_FLAG_ENCODE = 1,
+    FF_AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED = -3,
+    FF_AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED = -2,
+    FF_AMEDIACODEC_INFO_TRY_AGAIN_LATER = -1
+};
+
+FFAMediaCodec* ff_AMediaCodec_createCodecByName(const char *name);
+FFAMediaCodec* ff_AMediaCodec_createDecoderByType(const char *mime_type);
+FFAMediaCodec* ff_AMediaCodec_createEncoderByType(const char *mime_type);
+
+int ff_AMediaCodec_configure(FFAMediaCodec* codec, const FFAMediaFormat* format, void* surface, void *crypto, uint32_t flags);
+int ff_AMediaCodec_start(FFAMediaCodec* codec);
+int ff_AMediaCodec_stop(FFAMediaCodec* codec);
+int ff_AMediaCodec_flush(FFAMediaCodec* codec);
+int ff_AMediaCodec_delete(FFAMediaCodec* codec);
+
+uint8_t* ff_AMediaCodec_getInputBuffer(FFAMediaCodec* codec, size_t idx, size_t *out_size);
+uint8_t* ff_AMediaCodec_getOutputBuffer(FFAMediaCodec* codec, size_t idx, size_t *out_size);
+
+ssize_t ff_AMediaCodec_dequeueInputBuffer(FFAMediaCodec* codec, int64_t timeoutUs);
+int ff_AMediaCodec_queueInputBuffer(FFAMediaCodec* codec, size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags);
+
+ssize_t ff_AMediaCodec_dequeueOutputBuffer(FFAMediaCodec* codec, FFAMediaCodecBufferInfo *info, int64_t timeoutUs);
+FFAMediaFormat* ff_AMediaCodec_getOutputFormat(FFAMediaCodec* codec);
+
+int ff_AMediaCodec_releaseOutputBuffer(FFAMediaCodec* codec, size_t idx, int render);
+int ff_AMediaCodec_releaseOutputBufferAtTime(FFAMediaCodec *codec, size_t idx, int64_t timestampNs);
+
+int ff_AMediaCodec_infoTryAgainLater(FFAMediaCodec *codec, ssize_t idx);
+int ff_AMediaCodec_infoOutputBuffersChanged(FFAMediaCodec *codec, ssize_t idx);
+int ff_AMediaCodec_infoOutputFormatChanged(FFAMediaCodec *codec, ssize_t indx);
+
+int ff_AMediaCodec_cleanOutputBuffers(FFAMediaCodec *codec);
+
+#endif /* FF_MEDIACODEC_H */
diff --git a/libavcodec/mediacodecdec.c b/libavcodec/mediacodecdec.c
new file mode 100644
index 0000000..372fa59
--- /dev/null
+++ b/libavcodec/mediacodecdec.c
@@ -0,0 +1,753 @@
+/*
+ * Android MediaCodec decoder
+ *
+ * Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.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 <string.h>
+#include <sys/types.h>
+
+#include <android/native_window.h>
+
+#include "libavutil/atomic.h"
+#include "libavutil/common.h"
+#include "libavutil/mem.h"
+#include "libavutil/log.h"
+#include "libavutil/pixfmt.h"
+#include "libavutil/time.h"
+#include "libavutil/timestamp.h"
+#include "libavutil/jni.h"
+#include "libavutil/jni_internal.h"
+
+#include "avcodec.h"
+#include "internal.h"
+#include "mediacodecdec.h"
+
+#include "mediacodec_wrapper.h"
+
+enum {
+    COLOR_FormatYUV420Planar                              = 0x13,
+    COLOR_FormatYUV420SemiPlanar                          = 0x15,
+    COLOR_FormatYCbYCr                                    = 0x19,
+    COLOR_FormatAndroidOpaque                             = 0x7F000789,
+    COLOR_QCOM_FormatYUV420SemiPlanar                     = 0x7fa30c00,
+    COLOR_QCOM_FormatYUV420SemiPlanar32m                  = 0x7fa30c04,
+    COLOR_QCOM_FormatYUV420PackedSemiPlanar64x32Tile2m8ka = 0x7fa30c03,
+    COLOR_TI_FormatYUV420PackedSemiPlanar                 = 0x7f000100,
+    COLOR_TI_FormatYUV420PackedSemiPlanarInterlaced       = 0x7f000001,
+};
+
+static const struct {
+
+    int color_format;
+    enum AVPixelFormat pix_fmt;
+
+} color_formats[] = {
+
+    { COLOR_FormatYUV420Planar,                              AV_PIX_FMT_YUV420P },
+    { COLOR_FormatYUV420SemiPlanar,                          AV_PIX_FMT_NV12    },
+    { COLOR_QCOM_FormatYUV420SemiPlanar,                     AV_PIX_FMT_NV12    },
+    { COLOR_QCOM_FormatYUV420SemiPlanar32m,                  AV_PIX_FMT_NV12    },
+    { COLOR_QCOM_FormatYUV420PackedSemiPlanar64x32Tile2m8ka, AV_PIX_FMT_NV12    },
+    { COLOR_TI_FormatYUV420PackedSemiPlanar,                 AV_PIX_FMT_NV12    },
+    { COLOR_TI_FormatYUV420PackedSemiPlanarInterlaced,       AV_PIX_FMT_NV12    },
+    { 0 }
+};
+
+static enum AVPixelFormat mcdec_map_color_format(AVCodecContext *avctx, MediaCodecDecContext *s, int color_format)
+{
+    int i;
+    enum AVPixelFormat ret = AV_PIX_FMT_NONE;
+
+    if (!strcmp(s->codec_name, "OMX.k3.video.decoder.avc") && color_format == COLOR_FormatYCbYCr) {
+        s->color_format = color_format = COLOR_TI_FormatYUV420PackedSemiPlanar;
+    }
+
+    for (i = 0; i < FF_ARRAY_ELEMS(color_formats); i++) {
+        if (color_formats[i].color_format == color_format) {
+            return color_formats[i].pix_fmt;
+        }
+    }
+
+    av_log(avctx, AV_LOG_ERROR, "Output color format 0x%x (value=%d) is not supported\n",
+        color_format, color_format);
+
+    return ret;
+}
+
+static MediaCodecRef *mediacodec_ref_create(FFAMediaCodec *codec)
+{
+    MediaCodecRef *ref;
+
+    ref = av_mallocz(sizeof(*ref));
+    if (!ref)
+        goto fail;
+
+    ref->refcount = av_mallocz(sizeof(*ref->refcount));
+    if (!ref->refcount)
+        goto fail;
+
+    ref->codec = codec;
+    *ref->refcount = 1;
+
+    return ref;
+
+fail:
+    if (ref)
+        av_free(ref->refcount);
+
+    av_free(ref);
+
+    return NULL;
+}
+
+static MediaCodecRef *mediacodec_ref_ref(MediaCodecRef *ref)
+{
+    MediaCodecRef *ret;
+
+    ret = av_mallocz(sizeof(*ret));
+    if (!ret) {
+        return ret;
+    }
+
+    *ret = *ref;
+    avpriv_atomic_int_add_and_fetch(ret->refcount, 1);
+
+    return ret;
+}
+
+static void mediacodec_ref_unref(MediaCodecRef **ref)
+{
+    if (!ref || !*ref)
+        return;
+
+    if (!avpriv_atomic_int_add_and_fetch((*ref)->refcount, -1)) {
+        int status;
+
+        status = ff_AMediaCodec_flush((*ref)->codec);
+        if (status < 0) {
+            av_log(NULL, AV_LOG_ERROR,
+                "Failed to flush MediaCodec %p", (*ref)->codec);
+        }
+
+        status = ff_AMediaCodec_stop((*ref)->codec);
+        if (status < 0) {
+            av_log(NULL, AV_LOG_ERROR,
+                "Failed to stop MediaCodec %p", (*ref)->codec);
+        }
+
+        status = ff_AMediaCodec_delete((*ref)->codec);
+        if (status < 0) {
+            av_log(NULL, AV_LOG_ERROR,
+                "Failed to delete MediaCodec %p", (*ref)->codec);
+        }
+
+        av_free((*ref)->refcount);
+    }
+
+    av_freep(ref);
+}
+
+#define QCOM_TILE_WIDTH 64
+#define QCOM_TILE_HEIGHT 32
+#define QCOM_TILE_SIZE (QCOM_TILE_WIDTH * QCOM_TILE_HEIGHT)
+#define QCOM_TILE_GROUP_SIZE (4 * QCOM_TILE_SIZE)
+
+static size_t qcom_tile_pos(size_t x, size_t y, size_t w, size_t h)
+{
+  size_t flim = x + (y & ~1) * w;
+
+  if (y & 1) {
+    flim += (x & ~3) + 2;
+  } else if ((h & 1) == 0 || y != (h - 1)) {
+    flim += (x + 2) & ~3;
+  }
+
+  return flim;
+}
+
+static int mediacodec_wrap_buffer(AVCodecContext *avctx,
+                                  MediaCodecDecContext *s,
+                                  uint8_t *data,
+                                  size_t size,
+                                  ssize_t index,
+                                  FFAMediaCodecBufferInfo *info,
+                                  AVFrame *frame)
+{
+    int ret = 0;
+    int status = 0;
+    MediaCodecRef *codec_ref = s->codec_ref;
+
+    frame->width = avctx->width;
+    frame->height = avctx->height;
+    frame->format = avctx->pix_fmt;
+    frame->pkt_pts = info->presentationTimeUs;
+
+    /* MediaCodec buffers needs to be copied to our own refcounted buffers
+     * because the flush command invalidates all input and output buffers.
+     */
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
+        av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer\n");
+        goto done;
+    }
+
+    av_log(avctx, AV_LOG_DEBUG,
+            "Frame: width=%d stride=%d height=%d slice-height=%d "
+            "crop-top=%d crop-bottom=%d crop-left=%d crop-right=%d encoder=%s\n"
+            "destination linesizes=%d,%d,%d" ,
+            avctx->width, s->stride, avctx->height, s->slice_height,
+            s->crop_top, s->crop_bottom, s->crop_left, s->crop_right, s->codec_name,
+            frame->linesize[0], frame->linesize[1], frame->linesize[2]);
+
+    switch (s->color_format) {
+    case COLOR_FormatYUV420Planar: {
+        int i;
+        uint8_t *src = NULL;
+
+        for (i = 0; i < 3; i++) {
+            int stride = s->stride;
+            int height;
+
+            src = data + info->offset;
+            if (i == 0) {
+                height = avctx->height;
+
+                src += s->crop_top * s->stride;
+                src += s->crop_left;
+            } else {
+                height = avctx->height / 2;
+                stride = (s->stride + 1) / 2;
+
+                src += s->slice_height * s->stride;
+
+                if (i == 2) {
+                    src += ((s->slice_height + 1) / 2) * stride;
+                }
+
+                src += s->crop_top * stride;
+                src += (s->crop_left / 2);
+            }
+
+            if (frame->linesize[i] == stride) {
+                memcpy(frame->data[i], src, height * stride);
+            } else {
+                int j, width;
+                uint8_t *dst = frame->data[i];
+
+                if (i == 0) {
+                    width = avctx->width;
+                } else if (i == 1) {
+                    width = FFMIN(frame->linesize[i], FFALIGN(avctx->width, 2));
+                }
+
+                for (j = 0; j < height; j++) {
+                    memcpy(dst, src, width);
+                    src += stride;
+                    dst += frame->linesize[i];
+                }
+            }
+        }
+
+        break;
+    }
+    case COLOR_FormatYUV420SemiPlanar:
+    case COLOR_QCOM_FormatYUV420SemiPlanar:
+    case COLOR_QCOM_FormatYUV420SemiPlanar32m: {
+        int i;
+        uint8_t *src = NULL;
+
+        for (i = 0; i < 2; i++) {
+            int height;
+
+            src = data + info->offset;
+            if (i == 0) {
+                height = avctx->height;
+
+                src += s->crop_top * s->stride;
+                src += s->crop_left;
+            } else if (i == 1) {
+                height = avctx->height / 2;
+
+                src += s->slice_height * s->stride;
+                src += s->crop_top * s->stride;
+                src += s->crop_left;
+            }
+
+            if (frame->linesize[i] == s->stride) {
+                memcpy(frame->data[i], src, height * s->stride);
+            } else {
+                int j, width;
+                uint8_t *dst = frame->data[i];
+
+                if (i == 0) {
+                    width = avctx->width;
+                } else if (i == 1) {
+                    width = FFMIN(frame->linesize[i], FFALIGN(avctx->width, 2));
+                }
+
+                for (j = 0; j < height; j++) {
+                    memcpy(dst, src, width);
+                    src += s->stride;
+                    dst += frame->linesize[i];
+                }
+            }
+        }
+
+        break;
+    }
+    case COLOR_TI_FormatYUV420PackedSemiPlanar:
+    case COLOR_TI_FormatYUV420PackedSemiPlanarInterlaced: {
+        int i;
+        uint8_t *src = NULL;
+
+        for (i = 0; i < 2; i++) {
+            int height;
+
+            src = data + info->offset;
+            if (i == 0) {
+                height = avctx->height;
+            } else if (i == 1) {
+                height = avctx->height / 2;
+
+                src += (s->slice_height - s->crop_top / 2) * s->stride;
+
+                src += s->crop_top * s->stride;
+                src += s->crop_left;
+            }
+
+            if (frame->linesize[i] == s->stride) {
+                memcpy(frame->data[i], src, height * s->stride);
+            } else {
+                int j, width;
+                uint8_t *dst = frame->data[i];
+
+                if (i == 0) {
+                    width = avctx->width;
+                } else if (i == 1) {
+                    width = FFMIN(frame->linesize[i], FFALIGN(avctx->width, 2));
+                }
+
+                for (j = 0; j < height; j++) {
+                    memcpy(dst, src, width);
+                    src += s->stride;
+                    dst += frame->linesize[i];
+                }
+            }
+        }
+    }
+    case COLOR_QCOM_FormatYUV420PackedSemiPlanar64x32Tile2m8ka: {
+
+        size_t width = frame->width;
+        size_t linesize = frame->linesize[0];
+        size_t height = frame->height;
+
+        const size_t tile_w = (width - 1) / QCOM_TILE_WIDTH + 1;
+        const size_t tile_w_align = (tile_w + 1) & ~1;
+        const size_t tile_h_luma = (height - 1) / QCOM_TILE_HEIGHT + 1;
+        const size_t tile_h_chroma = (height / 2 - 1) / QCOM_TILE_HEIGHT + 1;
+
+        size_t luma_size = tile_w_align * tile_h_luma * QCOM_TILE_SIZE;
+        if((luma_size % QCOM_TILE_GROUP_SIZE) != 0)
+            luma_size = (((luma_size - 1) / QCOM_TILE_GROUP_SIZE) + 1) * QCOM_TILE_GROUP_SIZE;
+
+        /* The following code is borrowed from VLC and Gstreamer */
+        for(size_t y = 0; y < tile_h_luma; y++) {
+            size_t row_width = width;
+            for(size_t x = 0; x < tile_w; x++) {
+                size_t tile_width = row_width;
+                size_t tile_height = height;
+                /* dest luma memory index for this tile */
+                size_t luma_idx = y * QCOM_TILE_HEIGHT * linesize + x * QCOM_TILE_WIDTH;
+                /* dest chroma memory index for this tile */
+                /* XXX: remove divisions */
+                size_t chroma_idx = (luma_idx / linesize) * linesize / 2 + (luma_idx % linesize);
+
+                /* luma source pointer for this tile */
+                const uint8_t *src_luma  = data
+                    + qcom_tile_pos(x, y,tile_w_align, tile_h_luma) * QCOM_TILE_SIZE;
+
+                /* chroma source pointer for this tile */
+                const uint8_t *src_chroma = data + luma_size
+                    + qcom_tile_pos(x, y/2, tile_w_align, tile_h_chroma) * QCOM_TILE_SIZE;
+                if (y & 1)
+                    src_chroma += QCOM_TILE_SIZE/2;
+
+                /* account for right columns */
+                if (tile_width > QCOM_TILE_WIDTH)
+                    tile_width = QCOM_TILE_WIDTH;
+
+                /* account for bottom rows */
+                if (tile_height > QCOM_TILE_HEIGHT)
+                    tile_height = QCOM_TILE_HEIGHT;
+
+                tile_height /= 2;
+                while (tile_height--) {
+                    memcpy(frame->data[0] + luma_idx, src_luma, tile_width);
+                    src_luma += QCOM_TILE_WIDTH;
+                    luma_idx += linesize;
+
+                    memcpy(frame->data[0] + luma_idx, src_luma, tile_width);
+                    src_luma += QCOM_TILE_WIDTH;
+                    luma_idx += linesize;
+
+                    memcpy(frame->data[1] + chroma_idx, src_chroma, tile_width);
+                    src_chroma += QCOM_TILE_WIDTH;
+                    chroma_idx += linesize;
+                }
+                row_width -= QCOM_TILE_WIDTH;
+            }
+            height -= QCOM_TILE_HEIGHT;
+        }
+        break;
+    }
+    default:
+        av_log(avctx, AV_LOG_ERROR, "Unsupported color format 0x%x (value=%d)\n",
+            s->color_format, s->color_format);
+        ret = AVERROR(EINVAL);
+        goto done;
+    }
+
+    ret = 0;
+done:
+    status = ff_AMediaCodec_releaseOutputBuffer(codec_ref->codec, index, 0);
+    if (status < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Failed to release output buffer\n");
+        ret = AVERROR_EXTERNAL;
+    }
+
+    return ret;
+}
+
+static int mediacodec_dec_parse_format(AVCodecContext *avctx, MediaCodecDecContext *s)
+{
+    int32_t value = 0;
+    char *format = NULL;
+
+    if (!s->format) {
+        av_log(avctx, AV_LOG_ERROR, "Output MediaFormat is not set\n");
+        return AVERROR(EINVAL);
+    }
+
+    format = ff_AMediaFormat_toString(s->format);
+    if (!format) {
+        return AVERROR_EXTERNAL;
+    }
+    av_log(avctx, AV_LOG_DEBUG, "Parsing MediaFormat %s\n", format);
+    av_freep(&format);
+
+    /* Mandatory fields */
+    if (!ff_AMediaFormat_getInt32(s->format, "width", &value)) {
+        format = ff_AMediaFormat_toString(s->format);
+        av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "width", format);
+        av_freep(&format);
+        return AVERROR_EXTERNAL;
+    }
+    s->width = value;
+
+    if (!ff_AMediaFormat_getInt32(s->format, "height", &value)) {
+        format = ff_AMediaFormat_toString(s->format);
+        av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "height", format);
+        av_freep(&format);
+        return AVERROR_EXTERNAL;
+    }
+    s->height = value;
+
+    if (!ff_AMediaFormat_getInt32(s->format, "stride", &value)) {
+        format = ff_AMediaFormat_toString(s->format);
+        av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "stride", format);
+        av_freep(&format);
+        return AVERROR_EXTERNAL;
+    }
+    s->stride = value >= 0 ? value : s->width;
+
+    if (!ff_AMediaFormat_getInt32(s->format, "slice-height", &value)) {
+        format = ff_AMediaFormat_toString(s->format);
+        av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "slice-height", format);
+        av_freep(&format);
+        return AVERROR_EXTERNAL;
+    }
+    if (value > 0) {
+        s->slice_height = value;
+    } else {
+        s->slice_height = s->height;
+    }
+
+    if (strstr(s->codec_name, "OMX.Nvidia.")) {
+        s->slice_height = FFALIGN(s->height, 16);
+    } else if (strstr(s->codec_name, "OMX.SEC.avc.dec")) {
+        s->slice_height = avctx->height;
+        s->stride = avctx->width;
+    }
+
+    if (!ff_AMediaFormat_getInt32(s->format, "color-format", &value)) {
+        format = ff_AMediaFormat_toString(s->format);
+        av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "color-format", format);
+        av_freep(&format);
+        return AVERROR_EXTERNAL;
+    }
+    s->color_format = value;
+
+    s->pix_fmt = avctx->pix_fmt = mcdec_map_color_format(avctx, s, value);
+    if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
+        av_log(avctx, AV_LOG_ERROR, "Output color format is not supported\n");
+        return AVERROR(EINVAL);
+    }
+
+    /* Optional fields */
+    if (ff_AMediaFormat_getInt32(s->format, "crop-top", &value))
+        s->crop_top = value;
+
+    if (ff_AMediaFormat_getInt32(s->format, "crop-bottom", &value))
+        s->crop_bottom = value;
+
+    if (ff_AMediaFormat_getInt32(s->format, "crop-left", &value))
+        s->crop_left = value;
+
+    if (ff_AMediaFormat_getInt32(s->format, "crop-right", &value))
+        s->crop_right = value;
+
+    av_log(avctx, AV_LOG_INFO,
+        "Output crop parameters top=%d bottom=%d left=%d right=%d\n",
+        s->crop_top, s->crop_bottom, s->crop_left, s->crop_right);
+
+    return 0;
+}
+
+int ff_mediacodec_dec_init(AVCodecContext *avctx, MediaCodecDecContext *s,
+                           const char *mime, FFAMediaFormat *format)
+{
+    int ret = 0;
+    FFAMediaCodec *codec;
+    int status;
+
+    s->first_buffer_at = av_gettime();
+
+    s->codec_name = ff_AMediaCodecList_getCodecNameByType(mime, avctx->width, avctx->height);
+    if (!s->codec_name) {
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+
+    av_log(avctx, AV_LOG_DEBUG, "Found decoder %s\n", s->codec_name);
+    codec = ff_AMediaCodec_createCodecByName(s->codec_name);
+    if (!codec) {
+        av_log(avctx, AV_LOG_ERROR, "Failed to create media decoder for type %s and name %s\n", mime, s->codec_name);
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+
+    s->codec_ref = mediacodec_ref_create(codec);
+    if (!s->codec_ref) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+
+    status = ff_AMediaCodec_configure(codec, format, NULL, NULL, 0);
+    if (status < 0) {
+        char *desc = ff_AMediaFormat_toString(format);
+        av_log(avctx, AV_LOG_ERROR,
+            "Failed to configure codec (status = %d) with format %s\n",
+            status, desc);
+        av_freep(&desc);
+
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+
+    status = ff_AMediaCodec_start(codec);
+    if (status < 0) {
+        char *desc = ff_AMediaFormat_toString(format);
+        av_log(avctx, AV_LOG_ERROR,
+            "Failed to start codec (status = %d) with format %s\n",
+            status, desc);
+        av_freep(&desc);
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+
+    s->format = ff_AMediaCodec_getOutputFormat(codec);
+    if (s->format) {
+        if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) {
+            av_log(avctx, AV_LOG_ERROR,
+                "Failed to configure context\n");
+            goto fail;
+        }
+    }
+
+    av_log(avctx, AV_LOG_DEBUG, "MediaCodec %p started successfully\n", codec);
+
+    return 0;
+
+fail:
+    av_log(avctx, AV_LOG_ERROR, "MediaCodec %p failed to start\n", codec);
+    ff_mediacodec_dec_close(avctx, s);
+    return ret;
+}
+
+int ff_mediacodec_dec_decode(AVCodecContext *avctx, MediaCodecDecContext *s,
+                             AVFrame *frame, int *got_frame,
+                             AVPacket *pkt)
+{
+    int ret;
+    int offset = 0;
+    uint8_t *data;
+    ssize_t index;
+    size_t size;
+    uint32_t flags;
+    FFAMediaCodecBufferInfo info = { 0 };
+
+    int status;
+
+    int64_t input_dequeue_timeout_us = 8333;
+    int64_t output_dequeue_timeout_us = 8333;
+
+    FFAMediaCodec *codec = s->codec_ref->codec;
+
+    while (offset < pkt->size) {
+        int size;
+
+        index = ff_AMediaCodec_dequeueInputBuffer(codec, input_dequeue_timeout_us);
+        if (ff_AMediaCodec_infoTryAgainLater(codec, index)) {
+            break;
+        }
+
+        if (index < 0) {
+            av_log(avctx, AV_LOG_ERROR, "Failed to dequeue input buffer (status=%zd)\n", index);
+            return AVERROR_EXTERNAL;
+        }
+
+        data = ff_AMediaCodec_getInputBuffer(codec, index, &size);
+        if (!data) {
+            av_log(avctx, AV_LOG_ERROR, "Failed to get input buffer\n");
+            return AVERROR_EXTERNAL;
+        }
+
+        size = FFMIN(pkt->size - offset, size);
+
+        memcpy(data, pkt->data + offset, size);
+        offset += size;
+
+        status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, size, pkt->pts, flags);
+        if (status < 0) {
+            av_log(avctx, AV_LOG_ERROR, "Failed to queue input buffer (status = %d)\n", status);
+            return AVERROR_EXTERNAL;
+        }
+
+        s->queued_buffer_nb++;
+        if (s->queued_buffer_nb > s->queued_buffer_max)
+            s->queued_buffer_max = s->queued_buffer_nb;
+    }
+
+    index = ff_AMediaCodec_dequeueOutputBuffer(codec, &info, s->dequeued_buffer_nb == 0 ? 0 : output_dequeue_timeout_us);
+    if (index >= 0) {
+        int ret;
+
+        if (!s->first_buffer++) {
+            av_log(avctx, AV_LOG_DEBUG, "Got first buffer after %fms\n", (av_gettime() - s->first_buffer_at) / 1000);
+        }
+
+        av_log(avctx, AV_LOG_DEBUG, "Got output buffer %zd"
+                " offset=%" PRIi32 " size=%" PRIi32 " ts=%" PRIi64
+                " flags=%" PRIu32 "\n", index, info.offset, info.size,
+                info.presentationTimeUs, info.flags);
+
+        data = ff_AMediaCodec_getOutputBuffer(codec, index, &size);
+        if (!data) {
+            av_log(avctx, AV_LOG_ERROR, "Failed to get output buffer\n");
+            return AVERROR_EXTERNAL;
+        }
+
+        if ((ret = mediacodec_wrap_buffer(avctx, s, data, size, index, &info, frame)) < 0) {
+            return ret;
+        }
+
+        *got_frame = 1;
+        s->queued_buffer_nb--;
+        s->dequeued_buffer_nb++;
+
+    } else if (ff_AMediaCodec_infoOutputFormatChanged(codec, index)) {
+        char *format = NULL;
+
+        if (s->format) {
+            status = ff_AMediaFormat_delete(s->format);
+            if (status < 0) {
+                av_log(avctx, AV_LOG_ERROR, "Failed to delete MediaFormat %p\n", s->format);
+            }
+        }
+
+        s->format = ff_AMediaCodec_getOutputFormat(codec);
+        if (!s->format) {
+            av_log(avctx, AV_LOG_ERROR, "Failed to get output format\n");
+            return AVERROR_EXTERNAL;
+        }
+
+        format = ff_AMediaFormat_toString(s->format);
+        if (!format) {
+            return AVERROR_EXTERNAL;
+        }
+        av_log(avctx, AV_LOG_INFO, "Output MediaFormat changed to %s\n", format);
+        av_freep(&format);
+
+        if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) {
+            return ret;
+        }
+
+    } else if (ff_AMediaCodec_infoOutputBuffersChanged(codec, index)) {
+        ff_AMediaCodec_cleanOutputBuffers(codec);
+    } else if (ff_AMediaCodec_infoTryAgainLater(codec, index)) {
+        av_log(avctx, AV_LOG_DEBUG, "No output buffer available, try again later\n");
+    } else {
+        av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer (status=%zd)\n", index);
+        return AVERROR_EXTERNAL;
+    }
+
+    return offset;
+}
+
+int ff_mediacodec_dec_flush(AVCodecContext *avctx, MediaCodecDecContext *s)
+{
+    FFAMediaCodec *codec = s->codec_ref->codec;
+    int status;
+
+    s->queued_buffer_nb = 0;
+    s->dequeued_buffer_nb = 0;
+
+    status = ff_AMediaCodec_flush(codec);
+    if (status < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Failed to flush MediaCodec %p", codec);
+        return AVERROR_EXTERNAL;
+    }
+
+    s->first_buffer = 0;
+    s->first_buffer_at = av_gettime();
+
+    return 0;
+}
+
+int ff_mediacodec_dec_close(AVCodecContext *avctx, MediaCodecDecContext *s)
+{
+    mediacodec_ref_unref(&s->codec_ref);
+
+    if (s->format) {
+        ff_AMediaFormat_delete(s->format);
+        s->format = NULL;
+    }
+
+    return 0;
+}
diff --git a/libavcodec/mediacodecdec.h b/libavcodec/mediacodecdec.h
new file mode 100644
index 0000000..4dc7e51
--- /dev/null
+++ b/libavcodec/mediacodecdec.h
@@ -0,0 +1,98 @@
+/*
+ * Android MediaCodec decoder
+ *
+ * Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.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_MEDIACODECDEC_H
+#define AVCODEC_MEDIACODECDEC_H
+
+#include <stdint.h>
+#include <sys/types.h>
+
+#include "libavutil/frame.h"
+#include "libavutil/pixfmt.h"
+
+#include "avcodec.h"
+#include "mediacodec_wrapper.h"
+
+
+typedef struct MediaCodecRef {
+
+    FFAMediaCodec *codec;
+    int *refcount;
+
+} MediaCodecRef;
+
+typedef struct MediaCodecBuffer {
+
+    ssize_t index;
+    uint8_t *data;
+    size_t size;
+    FFAMediaCodecBufferInfo info;
+    MediaCodecRef *codec_ref;
+
+} MediaCodecBuffer;
+
+typedef struct MediaCodecDecContext {
+
+    const char *codec_name;
+    MediaCodecRef *codec_ref;
+    FFAMediaFormat *format;
+
+    int started;
+
+    int width;
+    int height;
+    int stride;
+    int slice_height;
+    int color_format;
+    enum AVPixelFormat pix_fmt;
+    int crop_top;
+    int crop_bottom;
+    int crop_left;
+    int crop_right;
+
+    int queued_buffer_nb;
+    int queued_buffer_max;
+    uint64_t dequeued_buffer_nb;
+
+    int first_buffer;
+    double first_buffer_at;
+
+} MediaCodecDecContext;
+
+int ff_mediacodec_dec_init(AVCodecContext *avctx,
+                           MediaCodecDecContext *s,
+                           const char *mime,
+                           FFAMediaFormat *format);
+
+int ff_mediacodec_dec_decode(AVCodecContext *avctx,
+                             MediaCodecDecContext *s,
+                             AVFrame *frame,
+                             int *got_frame,
+                             AVPacket *pkt);
+
+int ff_mediacodec_dec_flush(AVCodecContext *avctx,
+                            MediaCodecDecContext *s);
+
+int ff_mediacodec_dec_close(AVCodecContext *avctx,
+                            MediaCodecDecContext *s);
+
+#endif /* AVCODEC_MEDIACODECDEC_H */
diff --git a/libavcodec/mediacodecdec_h264.c b/libavcodec/mediacodecdec_h264.c
new file mode 100644
index 0000000..b9e6fc2
--- /dev/null
+++ b/libavcodec/mediacodecdec_h264.c
@@ -0,0 +1,365 @@
+/*
+ * Android MediaCodec H.264 decoder
+ *
+ * Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.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 <stdint.h>
+#include <string.h>
+
+#include "libavutil/common.h"
+#include "libavutil/fifo.h"
+#include "libavutil/opt.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/pixfmt.h"
+#include "libavutil/atomic.h"
+
+#include "avcodec.h"
+#include "internal.h"
+#include "mediacodecdec.h"
+#include "mediacodec_wrapper.h"
+
+#define CODEC_MIME "video/avc"
+
+typedef struct MediaCodecH264DecContext {
+
+    MediaCodecDecContext ctx;
+
+    AVCodecParserContext *parser;
+    AVCodecContext *avctx_internal;
+
+    AVBitStreamFilterContext *bsf;
+
+    AVFifoBuffer *fifo;
+
+    AVPacket input_ref;
+    AVPacket filtered_pkt;
+    uint8_t *filtered_data;
+
+} MediaCodecH264DecContext;
+
+static int h264_extradata_to_annexb_sps_pps(AVCodecContext *avctx,
+        uint8_t **extradata_annexb, int *extradata_annexb_size,
+        int *sps_offset, int *sps_size,
+        int *pps_offset, int *pps_size)
+{
+    uint16_t unit_size;
+    uint64_t total_size = 0;
+
+    uint8_t i, j, unit_nb;
+    uint8_t sps_seen = 0;
+    uint8_t pps_seen = 0;
+
+    const uint8_t *extradata;
+    static const uint8_t nalu_header[4] = { 0x00, 0x00, 0x00, 0x01 };
+
+    if (avctx->extradata_size < 8) {
+        av_log(avctx, AV_LOG_ERROR,
+            "Too small extradata size, corrupted stream or invalid MP4/AVCC bitstream\n");
+        return AVERROR(EINVAL);
+    }
+
+    *extradata_annexb = NULL;
+    *extradata_annexb_size = 0;
+
+    *sps_offset = *sps_size = 0;
+    *pps_offset = *pps_size = 0;
+
+    extradata = avctx->extradata + 4;
+
+    /* skip length size */
+    extradata++;
+
+    for (j = 0; j < 2; j ++) {
+
+        if (j == 0) {
+            /* number of sps unit(s) */
+            unit_nb = *extradata++ & 0x1f;
+        } else {
+            /* number of pps unit(s) */
+            unit_nb = *extradata++;
+        }
+
+        for (i = 0; i < unit_nb; i++) {
+            int err;
+
+            unit_size   = AV_RB16(extradata);
+            total_size += unit_size + 4;
+
+            if (total_size > INT_MAX) {
+                av_log(avctx, AV_LOG_ERROR,
+                    "Too big extradata size, corrupted stream or invalid MP4/AVCC bitstream\n");
+                av_freep(extradata_annexb);
+                return AVERROR(EINVAL);
+            }
+
+            if (extradata + 2 + unit_size > avctx->extradata + avctx->extradata_size) {
+                av_log(avctx, AV_LOG_ERROR, "Packet header is not contained in global extradata, "
+                    "corrupted stream or invalid MP4/AVCC bitstream\n");
+                av_freep(extradata_annexb);
+                return AVERROR(EINVAL);
+            }
+
+            if ((err = av_reallocp(extradata_annexb, total_size)) < 0) {
+                return err;
+            }
+
+            memcpy(*extradata_annexb + total_size - unit_size - 4, nalu_header, 4);
+            memcpy(*extradata_annexb + total_size - unit_size, extradata + 2, unit_size);
+            extradata += 2 + unit_size;
+        }
+
+        if (unit_nb) {
+            if (j == 0) {
+                sps_seen = 1;
+                *sps_size = total_size;
+            } else {
+                pps_seen = 1;
+                *pps_size = total_size - *sps_size;
+                *pps_offset = *sps_size;
+            }
+        }
+    }
+
+    *extradata_annexb_size = total_size;
+
+    if (!sps_seen)
+        av_log(avctx, AV_LOG_WARNING,
+               "Warning: SPS NALU missing or invalid. "
+               "The resulting stream may not play.\n");
+
+    if (!pps_seen)
+        av_log(avctx, AV_LOG_WARNING,
+               "Warning: PPS NALU missing or invalid. "
+               "The resulting stream may not play.\n");
+
+    return 0;
+}
+
+static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
+{
+    MediaCodecH264DecContext *s = avctx->priv_data;
+
+    ff_mediacodec_dec_close(avctx, &s->ctx);
+
+    av_fifo_free(s->fifo);
+
+    av_bitstream_filter_close(s->bsf);
+    av_parser_close(s->parser);
+    avcodec_free_context(&s->avctx_internal);
+
+    return 0;
+}
+
+static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
+{
+    int ret;
+    FFAMediaFormat *format = NULL;
+    MediaCodecH264DecContext *s = avctx->priv_data;
+
+    format = ff_AMediaFormat_new();
+    if (!format) {
+        av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
+        ret = AVERROR_EXTERNAL;
+        goto done;
+    }
+
+    ff_AMediaFormat_setString(format, "mime", CODEC_MIME);
+    ff_AMediaFormat_setInt32(format, "width", avctx->width);
+    ff_AMediaFormat_setInt32(format, "height", avctx->height);
+
+    if (avctx->extradata[0] == 1) {
+        uint8_t *extradata = NULL;
+        int extradata_size = 0;
+
+        int sps_offset, sps_size;
+        int pps_offset, pps_size;
+
+        if ((ret = h264_extradata_to_annexb_sps_pps(avctx, &extradata, &extradata_size,
+                &sps_offset, &sps_size, &pps_offset, &pps_size)) < 0) {
+            goto done;
+        }
+
+        ff_AMediaFormat_setBuffer(format, "csd-0", extradata + sps_offset, sps_size);
+        ff_AMediaFormat_setBuffer(format, "csd-1", extradata + pps_offset, pps_size);
+
+        av_freep(&extradata);
+    } else {
+        ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
+    }
+
+    if ((ret = ff_mediacodec_dec_init(avctx, &s->ctx, CODEC_MIME, format)) < 0) {
+        goto done;
+    }
+
+    av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
+
+    s->fifo = av_fifo_alloc(sizeof(AVPacket));
+    if (!s->fifo) {
+        ret = AVERROR(ENOMEM);
+        goto done;
+    }
+
+    s->bsf = av_bitstream_filter_init("h264_mp4toannexb");
+    if (!s->bsf) {
+        ret = AVERROR(ENOMEM);
+        goto done;
+    }
+
+    s->avctx_internal = avcodec_alloc_context3(NULL);
+    if (!s->avctx_internal) {
+        ret = AVERROR(ENOMEM);
+        goto done;
+    }
+
+    if (avctx->extradata) {
+        s->avctx_internal->extradata = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
+        if (!s->avctx_internal->extradata) {
+            ret = AVERROR(ENOMEM);
+            goto done;
+        }
+        memcpy(s->avctx_internal->extradata, avctx->extradata,
+               avctx->extradata_size);
+        s->avctx_internal->extradata_size = avctx->extradata_size;
+    }
+
+    s->parser = av_parser_init(AV_CODEC_ID_H264);
+    if (!s->parser) {
+        ret = AVERROR(ENOMEM);
+        goto done;
+    }
+    s->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
+
+done:
+    if (format) {
+        ff_AMediaFormat_delete(format);
+    }
+
+    if (ret < 0) {
+        mediacodec_decode_close(avctx);
+    }
+    return ret;
+}
+
+
+static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
+                                   int *got_frame, AVPacket *pkt)
+{
+    MediaCodecH264DecContext *s = avctx->priv_data;
+
+    return ff_mediacodec_dec_decode(avctx, &s->ctx, frame, got_frame, pkt);
+}
+
+static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
+                                   int *got_frame, AVPacket *avpkt)
+{
+    MediaCodecH264DecContext *s = avctx->priv_data;
+    AVFrame *frame    = data;
+    int ret;
+
+    /* buffer the input packet */
+    if (avpkt->size) {
+        AVPacket input_ref = { 0 };
+
+        if (av_fifo_space(s->fifo) < sizeof(input_ref)) {
+            ret = av_fifo_realloc2(s->fifo,
+                                   av_fifo_size(s->fifo) + sizeof(input_ref));
+            if (ret < 0)
+                return ret;
+        }
+
+        ret = av_packet_ref(&input_ref, avpkt);
+        if (ret < 0)
+            return ret;
+        av_fifo_generic_write(s->fifo, &input_ref, sizeof(input_ref), NULL);
+    }
+
+    /* process buffered data */
+    while (!*got_frame) {
+        /* prepare the input data -- convert to Annex B if needed */
+        if (s->filtered_pkt.size <= 0) {
+            int size;
+
+            /* no more data */
+            if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
+                return avpkt->size ? avpkt->size :
+                    ff_mediacodec_dec_decode(avctx, &s->ctx, frame, got_frame, avpkt);
+            }
+
+            if (s->filtered_data != s->input_ref.data)
+                av_freep(&s->filtered_data);
+            s->filtered_data = NULL;
+            av_packet_unref(&s->input_ref);
+
+            av_fifo_generic_read(s->fifo, &s->input_ref, sizeof(s->input_ref), NULL);
+            ret = av_bitstream_filter_filter(s->bsf, avctx, NULL,
+                                             &s->filtered_data, &size,
+                                             s->input_ref.data, s->input_ref.size, 0);
+            if (ret < 0) {
+                s->filtered_data = s->input_ref.data;
+                size             = s->input_ref.size;
+            }
+            s->filtered_pkt      = s->input_ref;
+            s->filtered_pkt.data = s->filtered_data;
+            s->filtered_pkt.size = size;
+        }
+
+        ret = mediacodec_process_data(avctx, frame, got_frame, &s->filtered_pkt);
+        if (ret < 0)
+            return ret;
+
+        s->filtered_pkt.size -= ret;
+        s->filtered_pkt.data += ret;
+    }
+
+    return avpkt->size;
+}
+
+static void mediacodec_decode_flush(AVCodecContext *avctx)
+{
+    MediaCodecH264DecContext *s = avctx->priv_data;
+
+    while (av_fifo_size(s->fifo)) {
+        AVPacket pkt;
+        av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
+        av_packet_unref(&pkt);
+    }
+    av_fifo_reset(s->fifo);
+
+    av_packet_unref(&s->input_ref);
+
+    av_init_packet(&s->filtered_pkt);
+    s->filtered_pkt.data = NULL;
+    s->filtered_pkt.size = 0;
+
+    ff_mediacodec_dec_flush(avctx, &s->ctx);
+}
+
+AVCodec ff_h264_mediacodec_decoder = {
+    .name           = "h264_mediacodec",
+    .long_name      = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_H264,
+    .priv_data_size = sizeof(MediaCodecH264DecContext),
+    .init           = mediacodec_decode_init,
+    .decode         = mediacodec_decode_frame,
+    .flush          = mediacodec_decode_flush,
+    .close          = mediacodec_decode_close,
+    .capabilities   = CODEC_CAP_DELAY,
+};
-- 
2.7.1



More information about the ffmpeg-devel mailing list