FFmpeg
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"
37 #include "mediacodecdec_common.h"
38 #include "version.h"
39 
41 {
42  return av_mallocz(sizeof(AVMediaCodecContext));
43 }
44 
46 {
47  int ret = 0;
48  JNIEnv *env = NULL;
49 
50  env = ff_jni_get_env(avctx);
51  if (!env) {
52  return AVERROR_EXTERNAL;
53  }
54 
55  ctx->surface = (*env)->NewGlobalRef(env, surface);
56  if (ctx->surface) {
57  avctx->hwaccel_context = ctx;
58  } else {
59  av_log(avctx, AV_LOG_ERROR, "Could not create new global reference\n");
61  }
62 
63  return ret;
64 }
65 
67 {
68  JNIEnv *env = NULL;
69 
71 
72  if (!ctx) {
73  return;
74  }
75 
76  env = ff_jni_get_env(avctx);
77  if (!env) {
78  return;
79  }
80 
81  if (ctx->surface) {
82  (*env)->DeleteGlobalRef(env, ctx->surface);
83  ctx->surface = NULL;
84  }
85 
86  av_freep(&avctx->hwaccel_context);
87 }
88 
89 int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
90 {
92  int released = atomic_fetch_add(&buffer->released, 1);
93 
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);
100  }
101 
102  return 0;
103 }
104 
105 int av_mediacodec_render_buffer_at_time(AVMediaCodecBuffer *buffer, int64_t time)
106 {
108  int released = atomic_fetch_add(&buffer->released, 1);
109 
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);
116  }
117 
118  return 0;
119 }
120 
121 #else
122 
123 #include <stdlib.h>
124 
126 {
127  return NULL;
128 }
129 
131 {
132  return AVERROR(ENOSYS);
133 }
134 
136 {
137 }
138 
139 int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
140 {
141  return AVERROR(ENOSYS);
142 }
143 
144 int av_mediacodec_render_buffer_at_time(AVMediaCodecBuffer *buffer, int64_t time)
145 {
146  return AVERROR(ENOSYS);
147 }
148 
149 #endif
AVCodecContext::hwaccel_context
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2741
MediaCodecDecContext
Definition: mediacodecdec_common.h:37
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:388
ff_AMediaCodec_releaseOutputBuffer
int ff_AMediaCodec_releaseOutputBuffer(FFAMediaCodec *codec, size_t idx, int render)
Definition: mediacodec_wrapper.c:1416
atomic_fetch_sub
#define atomic_fetch_sub(object, operand)
Definition: stdatomic.h:137
mediacodecdec_common.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_mediacodec_alloc_context
AVMediaCodecContext * av_mediacodec_alloc_context(void)
Allocate and initialize a MediaCodec context.
Definition: mediacodec.c:125
av_mediacodec_default_free
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:135
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
ctx
AVFormatContext * ctx
Definition: movenc.c:48
atomic_load
#define atomic_load(object)
Definition: stdatomic.h:93
NULL
#define NULL
Definition: coverity.c:32
AVMediaCodecContext
This structure holds a reference to a android/view/Surface object that will be used as output by the ...
Definition: mediacodec.h:33
ff_jni_get_env
JNIEnv * ff_jni_get_env(void *log_ctx)
Definition: ffjni.c:51
error.h
av_mediacodec_default_init
int av_mediacodec_default_init(AVCodecContext *avctx, AVMediaCodecContext *ctx, void *surface)
Convenience function that sets up the MediaCodec context.
Definition: mediacodec.c:130
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
ffjni.h
av_mediacodec_render_buffer_at_time
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:144
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
avcodec.h
ret
ret
Definition: filter_design.txt:187
av_mediacodec_release_buffer
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:139
AVCodecContext
main external API structure.
Definition: avcodec.h:1565
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
config.h
atomic_fetch_add
#define atomic_fetch_add(object, operand)
Definition: stdatomic.h:131
mem.h
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
ff_AMediaCodec_releaseOutputBufferAtTime
int ff_AMediaCodec_releaseOutputBufferAtTime(FFAMediaCodec *codec, size_t idx, int64_t timestampNs)
Definition: mediacodec_wrapper.c:1433
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
jni.h
mediacodec.h