2 * Android MediaCodec public API functions
4 * Copyright (c) 2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libavutil/error.h"
27 #include "mediacodec.h"
33 #include "libavcodec/avcodec.h"
34 #include "libavutil/mem.h"
37 #include "mediacodecdec_common.h"
40 AVMediaCodecContext *av_mediacodec_alloc_context(void)
42 return av_mallocz(sizeof(AVMediaCodecContext));
45 int av_mediacodec_default_init(AVCodecContext *avctx, AVMediaCodecContext *ctx, void *surface)
50 env = ff_jni_get_env(avctx);
52 return AVERROR_EXTERNAL;
55 ctx->surface = (*env)->NewGlobalRef(env, surface);
57 avctx->hwaccel_context = ctx;
59 av_log(avctx, AV_LOG_ERROR, "Could not create new global reference\n");
60 ret = AVERROR_EXTERNAL;
66 void av_mediacodec_default_free(AVCodecContext *avctx)
70 AVMediaCodecContext *ctx = avctx->hwaccel_context;
76 env = ff_jni_get_env(avctx);
82 (*env)->DeleteGlobalRef(env, ctx->surface);
86 av_freep(&avctx->hwaccel_context);
89 int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
91 MediaCodecDecContext *ctx = buffer->ctx;
92 int released = atomic_fetch_add(&buffer->released, 1);
94 if (!released && (ctx->delay_flush || buffer->serial == atomic_load(&ctx->serial))) {
95 atomic_fetch_sub(&ctx->hw_buffer_count, 1);
96 av_log(ctx->avctx, AV_LOG_DEBUG,
97 "Releasing output buffer %zd (%p) ts=%"PRId64" with render=%d [%d pending]\n",
98 buffer->index, buffer, buffer->pts, render, atomic_load(&ctx->hw_buffer_count));
99 return ff_AMediaCodec_releaseOutputBuffer(ctx->codec, buffer->index, render);
105 int av_mediacodec_render_buffer_at_time(AVMediaCodecBuffer *buffer, int64_t time)
107 MediaCodecDecContext *ctx = buffer->ctx;
108 int released = atomic_fetch_add(&buffer->released, 1);
110 if (!released && (ctx->delay_flush || buffer->serial == atomic_load(&ctx->serial))) {
111 atomic_fetch_sub(&ctx->hw_buffer_count, 1);
112 av_log(ctx->avctx, AV_LOG_DEBUG,
113 "Rendering output buffer %zd (%p) ts=%"PRId64" with time=%"PRId64" [%d pending]\n",
114 buffer->index, buffer, buffer->pts, time, atomic_load(&ctx->hw_buffer_count));
115 return ff_AMediaCodec_releaseOutputBufferAtTime(ctx->codec, buffer->index, time);
125 AVMediaCodecContext *av_mediacodec_alloc_context(void)
130 int av_mediacodec_default_init(AVCodecContext *avctx, AVMediaCodecContext *ctx, void *surface)
132 return AVERROR(ENOSYS);
135 void av_mediacodec_default_free(AVCodecContext *avctx)
139 int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
141 return AVERROR(ENOSYS);
144 int av_mediacodec_render_buffer_at_time(AVMediaCodecBuffer *buffer, int64_t time)
146 return AVERROR(ENOSYS);