FFmpeg
Loading...
Searching...
No Matches
mediacodec.c
Go to the documentation of this file.
1/*
2 * Android MediaCodec public API functions
3 *
4 * Copyright (c) 2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
23#include "config.h"
24
25#include "libavutil/error.h"
26
27#include "mediacodec.h"
28
29#if CONFIG_MEDIACODEC
30
31#include <jni.h>
32
33#include "libavcodec/avcodec.h"
34#include "libavutil/mem.h"
35
36#include "ffjni.h"
38
40{
41 return av_mallocz(sizeof(AVMediaCodecContext));
42}
43
45{
46 int ret = 0;
47 JNIEnv *env = NULL;
48
49 env = ff_jni_get_env(avctx);
50 if (!env) {
51 return AVERROR_EXTERNAL;
52 }
53
54 ctx->surface = (*env)->NewGlobalRef(env, surface);
55 if (ctx->surface) {
56 avctx->hwaccel_context = ctx;
57 } else {
58 av_log(avctx, AV_LOG_ERROR, "Could not create new global reference\n");
59 ret = AVERROR_EXTERNAL;
60 }
61
62 return ret;
63}
64
66{
67 JNIEnv *env = NULL;
68
70
71 if (!ctx) {
72 return;
73 }
74
75 env = ff_jni_get_env(avctx);
76 if (!env) {
77 return;
78 }
79
80 if (ctx->surface) {
81 (*env)->DeleteGlobalRef(env, ctx->surface);
82 ctx->surface = NULL;
83 }
84
86}
87
88int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
89{
91 int released = atomic_fetch_add(&buffer->released, 1);
92
93 if (!released && (ctx->delay_flush || buffer->serial == atomic_load(&ctx->serial))) {
94 atomic_fetch_sub(&ctx->hw_buffer_count, 1);
95 av_log(ctx->avctx, AV_LOG_DEBUG,
96 "Releasing output buffer %zd (%p) ts=%"PRId64" with render=%d [%d pending]\n",
97 buffer->index, buffer, buffer->pts, render, atomic_load(&ctx->hw_buffer_count));
98 return ff_AMediaCodec_releaseOutputBuffer(ctx->codec, buffer->index, render);
99 }
100
101 return 0;
102}
103
104int av_mediacodec_render_buffer_at_time(AVMediaCodecBuffer *buffer, int64_t time)
105{
107 int released = atomic_fetch_add(&buffer->released, 1);
108
109 if (!released && (ctx->delay_flush || buffer->serial == atomic_load(&ctx->serial))) {
110 atomic_fetch_sub(&ctx->hw_buffer_count, 1);
111 av_log(ctx->avctx, AV_LOG_DEBUG,
112 "Rendering output buffer %zd (%p) ts=%"PRId64" with time=%"PRId64" [%d pending]\n",
113 buffer->index, buffer, buffer->pts, time, atomic_load(&ctx->hw_buffer_count));
114 return ff_AMediaCodec_releaseOutputBufferAtTime(ctx->codec, buffer->index, time);
115 }
116
117 return 0;
118}
119
120#else
121
122#include <stdlib.h>
123
128
130{
131 return AVERROR(ENOSYS);
132}
133
137
138int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
139{
140 return AVERROR(ENOSYS);
141}
142
144{
145 return AVERROR(ENOSYS);
146}
147
148#endif
static AVFormatContext * ctx
Libavcodec external API header.
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define atomic_load(object)
Definition stdatomic.h:93
error code definitions
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
JNIEnv * ff_jni_get_env(void *log_ctx)
Definition ffjni.c:53
int av_mediacodec_default_init(AVCodecContext *avctx, AVMediaCodecContext *ctx, void *surface)
Convenience function that sets up the MediaCodec context.
Definition mediacodec.c:129
int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
Release a MediaCodec buffer and render it to the surface that is associated with the decoder.
Definition mediacodec.c:138
int av_mediacodec_render_buffer_at_time(AVMediaCodecBuffer *buffer, int64_t time)
Release a MediaCodec buffer and render it at the given time to the surface that is associated with th...
Definition mediacodec.c:143
AVMediaCodecContext * av_mediacodec_alloc_context(void)
Allocate and initialize a MediaCodec context.
Definition mediacodec.c:124
void av_mediacodec_default_free(AVCodecContext *avctx)
This function must be called to free the MediaCodec context initialized with av_mediacodec_default_in...
Definition mediacodec.c:134
static int ff_AMediaCodec_releaseOutputBuffer(FFAMediaCodec *codec, size_t idx, int render)
static int ff_AMediaCodec_releaseOutputBufferAtTime(FFAMediaCodec *codec, size_t idx, int64_t timestampNs)
Memory handling functions.
main external API structure.
Definition avcodec.h:443
void * hwaccel_context
Legacy hardware accelerator context.
Definition avcodec.h:1447
This structure holds a reference to a android/view/Surface object that will be used as output by the ...
Definition mediacodec.h:33
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static char buffer[20]
Definition seek.c:32
#define atomic_fetch_sub(object, operand)
Definition stdatomic.h:140
#define atomic_fetch_add(object, operand)
Definition stdatomic.h:137