From 61a1612de9c18d497c3be01f10b0fabc2066d76f Mon Sep 17 00:00:00 2001 From: Aman Gupta Date: Thu, 6 Jul 2017 10:54:40 -0700 Subject: hwdec: add mediacodec hardware decoder for IMGFMT_MEDIACODEC frames --- DOCS/man/options.rst | 5 +++++ options/options.c | 1 + video/decode/hw_mediacodec.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ video/decode/vd_lavc.c | 2 ++ video/fmt-conversion.c | 3 +++ video/hwdec.h | 1 + video/img_format.h | 1 + 7 files changed, 60 insertions(+) diff --git a/DOCS/man/options.rst b/DOCS/man/options.rst index f5f668368e..c047e6d2d4 100644 --- a/DOCS/man/options.rst +++ b/DOCS/man/options.rst @@ -681,6 +681,7 @@ Video :d3d11va: requires ``--vo=gpu`` with ``--gpu-context=angle`` (Windows 8+ only) :d3d11va-copy: copies video back to system RAM (Windows 8+ only) + :mediacodec: requires ``--vo=mediacodec_embed`` (Android only) :mediacodec-copy: copies video back to system RAM (Android only) :rpi: requires ``--vo=gpu`` (Raspberry Pi only - default if available) :rpi-copy: copies video back to system RAM (Raspberry Pi only) @@ -2480,6 +2481,10 @@ Window support window embedding of foreign processes, this works only with libmpv, and will crash when used from the command line. + On Android, the ID is interpreted as ``android.view.Surface``. Pass it as a + value cast to ``intptr_t``. Use with ``--vo=mediacodec_embed`` and + ``--hwdec=mediacodec`` for direct rendering using MediaCodec. + ``--no-window-dragging`` Don't move the window when clicking on it and moving the mouse pointer. diff --git a/options/options.c b/options/options.c index 857bb85271..eae58a45a4 100644 --- a/options/options.c +++ b/options/options.c @@ -111,6 +111,7 @@ const struct m_opt_choice_alternatives mp_hwdec_names[] = { {"d3d11va-copy",HWDEC_D3D11VA_COPY}, {"rpi", HWDEC_RPI}, {"rpi-copy", HWDEC_RPI_COPY}, + {"mediacodec", HWDEC_MEDIACODEC}, {"mediacodec-copy",HWDEC_MEDIACODEC_COPY}, {"cuda", HWDEC_CUDA}, {"cuda-copy", HWDEC_CUDA_COPY}, diff --git a/video/decode/hw_mediacodec.c b/video/decode/hw_mediacodec.c index 9d3ef44f8b..b1a06c19ed 100644 --- a/video/decode/hw_mediacodec.c +++ b/video/decode/hw_mediacodec.c @@ -15,8 +15,55 @@ * License along with mpv. If not, see . */ +#include + +#include + +#include "options/options.h" #include "video/decode/lavc.h" +static int probe(struct lavc_ctx *ctx, struct vd_lavc_hwdec *hwdec, + const char *codec) +{ + if (ctx->opts->vo->WinID == 0) + return HWDEC_ERR_NO_CTX; + + return 0; +} + +static int init(struct lavc_ctx *ctx) +{ + return 0; +} + +static int init_decoder(struct lavc_ctx *ctx, int w, int h) +{ + av_mediacodec_default_free(ctx->avctx); + + AVMediaCodecContext *mcctx = av_mediacodec_alloc_context(); + if (!mcctx) + return -1; + + void *surface = (void *)(intptr_t)(ctx->opts->vo->WinID); + return av_mediacodec_default_init(ctx->avctx, mcctx, surface); +} + +static void uninit(struct lavc_ctx *ctx) +{ + if (ctx->avctx) + av_mediacodec_default_free(ctx->avctx); +} + +const struct vd_lavc_hwdec mp_vd_lavc_mediacodec = { + .type = HWDEC_MEDIACODEC, + .image_format = IMGFMT_MEDIACODEC, + .lavc_suffix = "_mediacodec", + .probe = probe, + .init = init, + .init_decoder = init_decoder, + .uninit = uninit, +}; + const struct vd_lavc_hwdec mp_vd_lavc_mediacodec_copy = { .type = HWDEC_MEDIACODEC_COPY, .lavc_suffix = "_mediacodec", diff --git a/video/decode/vd_lavc.c b/video/decode/vd_lavc.c index ec22b42571..fe0de0e43f 100644 --- a/video/decode/vd_lavc.c +++ b/video/decode/vd_lavc.c @@ -129,6 +129,7 @@ const struct m_sub_options vd_lavc_conf = { }, }; +extern const struct vd_lavc_hwdec mp_vd_lavc_mediacodec; extern const struct vd_lavc_hwdec mp_vd_lavc_mediacodec_copy; extern const struct vd_lavc_hwdec mp_vd_lavc_videotoolbox; extern const struct vd_lavc_hwdec mp_vd_lavc_videotoolbox_copy; @@ -261,6 +262,7 @@ static const struct vd_lavc_hwdec *const hwdec_list[] = { &mp_vd_lavc_d3d11va_copy, #endif #if HAVE_ANDROID + &mp_vd_lavc_mediacodec, &mp_vd_lavc_mediacodec_copy, #endif #if HAVE_CUDA_HWACCEL diff --git a/video/fmt-conversion.c b/video/fmt-conversion.c index 4288f82256..0bdbd109bb 100644 --- a/video/fmt-conversion.c +++ b/video/fmt-conversion.c @@ -60,6 +60,9 @@ static const struct { {IMGFMT_VDPAU, AV_PIX_FMT_VDPAU}, #if HAVE_VIDEOTOOLBOX_HWACCEL {IMGFMT_VIDEOTOOLBOX, AV_PIX_FMT_VIDEOTOOLBOX}, +#endif +#if HAVE_ANDROID + {IMGFMT_MEDIACODEC, AV_PIX_FMT_MEDIACODEC}, #endif {IMGFMT_VAAPI, AV_PIX_FMT_VAAPI}, {IMGFMT_DXVA2, AV_PIX_FMT_DXVA2_VLD}, diff --git a/video/hwdec.h b/video/hwdec.h index 379e754ffe..461ea6a3e6 100644 --- a/video/hwdec.h +++ b/video/hwdec.h @@ -22,6 +22,7 @@ enum hwdec_type { HWDEC_D3D11VA_COPY, HWDEC_RPI, HWDEC_RPI_COPY, + HWDEC_MEDIACODEC, HWDEC_MEDIACODEC_COPY, HWDEC_CUDA, HWDEC_CUDA_COPY, diff --git a/video/img_format.h b/video/img_format.h index 7f0330d55b..01d101aa8e 100644 --- a/video/img_format.h +++ b/video/img_format.h @@ -203,6 +203,7 @@ enum mp_imgfmt { IMGFMT_DXVA2, // IDirect3DSurface9 (NV12/P010/P016) IMGFMT_MMAL, // MMAL_BUFFER_HEADER_T IMGFMT_VIDEOTOOLBOX, // CVPixelBufferRef + IMGFMT_MEDIACODEC, // AVMediaCodecBuffer IMGFMT_CUDA, // CUDA Buffer // Generic pass-through of AV_PIX_FMT_*. Used for formats which don't have -- cgit v1.2.3