2016-03-11 16:21:04 +00:00
|
|
|
/*
|
|
|
|
* Android MediaCodec public API functions
|
|
|
|
*
|
|
|
|
* Copyright (c) 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 "config.h"
|
|
|
|
|
2016-08-03 03:48:16 +00:00
|
|
|
#include "libavutil/error.h"
|
|
|
|
|
2016-10-16 14:52:10 +00:00
|
|
|
#include "mediacodec.h"
|
|
|
|
|
2017-10-25 23:18:46 +00:00
|
|
|
#if CONFIG_MEDIACODEC
|
2016-03-11 16:21:04 +00:00
|
|
|
|
|
|
|
#include <jni.h>
|
|
|
|
|
|
|
|
#include "libavcodec/avcodec.h"
|
|
|
|
#include "libavutil/mem.h"
|
|
|
|
|
|
|
|
#include "ffjni.h"
|
2016-10-12 12:44:08 +00:00
|
|
|
#include "mediacodecdec_common.h"
|
|
|
|
#include "version.h"
|
2016-03-11 16:21:04 +00:00
|
|
|
|
|
|
|
AVMediaCodecContext *av_mediacodec_alloc_context(void)
|
|
|
|
{
|
|
|
|
return av_mallocz(sizeof(AVMediaCodecContext));
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_mediacodec_default_init(AVCodecContext *avctx, AVMediaCodecContext *ctx, void *surface)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
JNIEnv *env = NULL;
|
|
|
|
|
2016-07-01 07:59:13 +00:00
|
|
|
env = ff_jni_get_env(avctx);
|
2016-03-11 16:21:04 +00:00
|
|
|
if (!env) {
|
|
|
|
return AVERROR_EXTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->surface = (*env)->NewGlobalRef(env, surface);
|
|
|
|
if (ctx->surface) {
|
|
|
|
avctx->hwaccel_context = ctx;
|
|
|
|
} else {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Could not create new global reference\n");
|
|
|
|
ret = AVERROR_EXTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void av_mediacodec_default_free(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
JNIEnv *env = NULL;
|
|
|
|
|
|
|
|
AVMediaCodecContext *ctx = avctx->hwaccel_context;
|
|
|
|
|
|
|
|
if (!ctx) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-01 07:59:13 +00:00
|
|
|
env = ff_jni_get_env(avctx);
|
2016-03-11 16:21:04 +00:00
|
|
|
if (!env) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->surface) {
|
|
|
|
(*env)->DeleteGlobalRef(env, ctx->surface);
|
|
|
|
ctx->surface = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_freep(&avctx->hwaccel_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
|
|
|
|
{
|
|
|
|
MediaCodecDecContext *ctx = buffer->ctx;
|
2017-03-22 18:27:30 +00:00
|
|
|
int released = atomic_fetch_add(&buffer->released, 1);
|
2016-03-11 16:21:04 +00:00
|
|
|
|
2017-03-22 18:27:30 +00:00
|
|
|
if (!released) {
|
2016-03-11 16:21:04 +00:00
|
|
|
return ff_AMediaCodec_releaseOutputBuffer(ctx->codec, buffer->index, render);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
AVMediaCodecContext *av_mediacodec_alloc_context(void)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_mediacodec_default_init(AVCodecContext *avctx, AVMediaCodecContext *ctx, void *surface)
|
|
|
|
{
|
2016-08-03 03:48:16 +00:00
|
|
|
return AVERROR(ENOSYS);
|
2016-03-11 16:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void av_mediacodec_default_free(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
|
|
|
|
{
|
2016-08-03 03:48:16 +00:00
|
|
|
return AVERROR(ENOSYS);
|
2016-03-11 16:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|