[FFmpeg-devel] [PATCH] lavc: add mpeg4 mediacodec decoder

Thomas Volkert silvo at gmx.net
Sun Jul 24 12:37:08 EEST 2016


From: Thomas Volkert <thomas at netzeal.de>

---
 configure                        |   1 +
 libavcodec/Makefile              |   1 +
 libavcodec/allcodecs.c           |   1 +
 libavcodec/mediacodecdec_mpeg4.c | 239 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 242 insertions(+)
 create mode 100644 libavcodec/mediacodecdec_mpeg4.c

diff --git a/configure b/configure
index 1b41303..9004b06 100755
--- a/configure
+++ b/configure
@@ -2621,6 +2621,7 @@ mpeg2_videotoolbox_hwaccel_select="mpeg2video_decoder"
 mpeg2_xvmc_hwaccel_deps="xvmc"
 mpeg2_xvmc_hwaccel_select="mpeg2video_decoder"
 mpeg4_crystalhd_decoder_select="crystalhd"
+mpeg4_mediacodec_decoder_deps="mediacodec"
 mpeg4_mmal_decoder_deps="mmal"
 mpeg4_mmal_decoder_select="mmal"
 mpeg4_mmal_hwaccel_deps="mmal"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index abef19e..642cf2a 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -317,6 +317,7 @@ OBJS-$(CONFIG_H264_DECODER)            += h264.o h264_cabac.o h264_cavlc.o \
                                           h2645_parse.o
 OBJS-$(CONFIG_H264_CUVID_DECODER)      += cuvid.o
 OBJS-$(CONFIG_H264_MEDIACODEC_DECODER) += mediacodecdec_h264.o
+OBJS-$(CONFIG_MPEG4_MEDIACODEC_DECODER) += mediacodecdec_mpeg4.o
 OBJS-$(CONFIG_H264_MMAL_DECODER)       += mmaldec.o
 OBJS-$(CONFIG_H264_NVENC_ENCODER)      += nvenc_h264.o
 OBJS-$(CONFIG_NVENC_ENCODER)           += nvenc_h264.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 951e199..2d98694 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -239,6 +239,7 @@ void avcodec_register_all(void)
     REGISTER_ENCDEC (MPEG2VIDEO,        mpeg2video);
     REGISTER_ENCDEC (MPEG4,             mpeg4);
     REGISTER_DECODER(MPEG4_CRYSTALHD,   mpeg4_crystalhd);
+    REGISTER_DECODER(MPEG4_MEDIACODEC,  mpeg4_mediacodec);
     REGISTER_DECODER(MPEG4_MMAL,        mpeg4_mmal);
 #if FF_API_VDPAU
     REGISTER_DECODER(MPEG4_VDPAU,       mpeg4_vdpau);
diff --git a/libavcodec/mediacodecdec_mpeg4.c b/libavcodec/mediacodecdec_mpeg4.c
new file mode 100644
index 0000000..7c4559b
--- /dev/null
+++ b/libavcodec/mediacodecdec_mpeg4.c
@@ -0,0 +1,239 @@
+/*
+ * Android MediaCodec MPEG 4 decoder
+ *
+ * Copyright (c) 2016 Thomas Volkert <thomas at netzeal.de>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/avassert.h"
+#include "libavutil/common.h"
+#include "libavutil/fifo.h"
+
+#include "avcodec.h"
+#include "internal.h"
+#include "mediacodecdec.h"
+#include "mediacodec_wrapper.h"
+
+#define CODEC_MIME "video/mp4v-es"
+
+typedef struct MediaCodecMPEG4DecContext {
+
+    MediaCodecDecContext *ctx;
+
+    AVBSFContext *bsf;
+
+    AVFifoBuffer *fifo;
+
+    AVPacket filtered_pkt;
+
+} MediaCodecMPEG4DecContext;
+
+static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
+{
+    MediaCodecMPEG4DecContext *s = avctx->priv_data;
+
+    ff_mediacodec_dec_close(avctx, s->ctx);
+    s->ctx = NULL;
+
+    av_fifo_free(s->fifo);
+
+    av_bsf_free(&s->bsf);
+    av_packet_unref(&s->filtered_pkt);
+
+    return 0;
+}
+
+static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
+{
+    int ret;
+
+    FFAMediaFormat *format = NULL;
+    MediaCodecMPEG4DecContext *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);
+
+    s->ctx = av_mallocz(sizeof(*s->ctx));
+    if (!s->ctx) {
+        av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
+        ret = AVERROR(ENOMEM);
+        goto done;
+    }
+
+    if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, CODEC_MIME, format)) < 0) {
+        s->ctx = NULL;
+        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;
+    }
+
+    const AVBitStreamFilter *bsf = av_bsf_get_by_name("mpeg4_unpack_bframes");
+    if(!bsf) {
+        ret = AVERROR_BSF_NOT_FOUND;
+        goto done;
+    }
+
+    if ((ret = av_bsf_alloc(bsf, &s->bsf))) {
+        goto done;
+    }
+
+    if (((ret = avcodec_parameters_from_context(s->bsf->par_in, avctx)) < 0) ||
+        ((ret = av_bsf_init(s->bsf)) < 0)) {
+          goto done;
+    }
+
+    av_init_packet(&s->filtered_pkt);
+
+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)
+{
+    MediaCodecMPEG4DecContext *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)
+{
+    MediaCodecMPEG4DecContext *s = avctx->priv_data;
+    AVFrame *frame    = data;
+    int ret;
+
+    /* buffer the input packet */
+    if (avpkt->size) {
+        AVPacket input_pkt = { 0 };
+
+        if (av_fifo_space(s->fifo) < sizeof(input_pkt)) {
+            ret = av_fifo_realloc2(s->fifo,
+                                   av_fifo_size(s->fifo) + sizeof(input_pkt));
+            if (ret < 0)
+                return ret;
+        }
+
+        ret = av_packet_ref(&input_pkt, avpkt);
+        if (ret < 0)
+            return ret;
+        av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
+    }
+
+    if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
+        if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
+            return avpkt->size;
+        }
+    }
+
+    /* process buffered data */
+    while (!*got_frame) {
+        /* prepare the input data -- unpack DivX-style packed b frame if needed */
+        if (s->filtered_pkt.size <= 0) {
+            AVPacket input_pkt = { 0 };
+
+            av_packet_unref(&s->filtered_pkt);
+
+            /* 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);
+            }
+
+            av_fifo_generic_read(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
+
+            ret = av_bsf_send_packet(s->bsf, &input_pkt);
+            if (ret < 0) {
+                return ret;
+            }
+
+            ret = av_bsf_receive_packet(s->bsf, &s->filtered_pkt);
+            if (ret == AVERROR(EAGAIN)) {
+                goto done;
+            }
+
+            /* no explicit flushing needed */
+            av_assert0(ret != AVERROR_EOF);
+
+            if (ret < 0) {
+                return ret;
+            }
+        }
+
+        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;
+    }
+done:
+    return avpkt->size;
+}
+
+static void mediacodec_decode_flush(AVCodecContext *avctx)
+{
+    MediaCodecMPEG4DecContext *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->filtered_pkt);
+
+    ff_mediacodec_dec_flush(avctx, s->ctx);
+}
+
+AVCodec ff_mpeg4_mediacodec_decoder = {
+    .name           = "mpeg4_mediacodec",
+    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 Android MediaCodec decoder"),
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_MPEG4,
+    .priv_data_size = sizeof(MediaCodecMPEG4DecContext),
+    .init           = mediacodec_decode_init,
+    .decode         = mediacodec_decode_frame,
+    .flush          = mediacodec_decode_flush,
+    .close          = mediacodec_decode_close,
+    .capabilities   = CODEC_CAP_DELAY,
+    .caps_internal  = FF_CODEC_CAP_SETS_PKT_DTS,
+};
-- 
2.7.4



More information about the ffmpeg-devel mailing list