FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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_H264_MEDIACODEC_HWACCEL
30 
31 #include <jni.h>
32 
33 #include "libavcodec/avcodec.h"
34 #include "libavutil/atomic.h"
35 #include "libavutil/mem.h"
36 
37 #include "ffjni.h"
38 #include "mediacodecdec_common.h"
39 #include "version.h"
40 
42 {
43  return av_mallocz(sizeof(AVMediaCodecContext));
44 }
45 
47 {
48  int ret = 0;
49  JNIEnv *env = NULL;
50 
51  env = ff_jni_get_env(avctx);
52  if (!env) {
53  return AVERROR_EXTERNAL;
54  }
55 
56  ctx->surface = (*env)->NewGlobalRef(env, surface);
57  if (ctx->surface) {
58  avctx->hwaccel_context = ctx;
59  } else {
60  av_log(avctx, AV_LOG_ERROR, "Could not create new global reference\n");
61  ret = AVERROR_EXTERNAL;
62  }
63 
64  return ret;
65 }
66 
68 {
69  JNIEnv *env = NULL;
70 
72 
73  if (!ctx) {
74  return;
75  }
76 
77  env = ff_jni_get_env(avctx);
78  if (!env) {
79  return;
80  }
81 
82  if (ctx->surface) {
83  (*env)->DeleteGlobalRef(env, ctx->surface);
84  ctx->surface = NULL;
85  }
86 
87  av_freep(&avctx->hwaccel_context);
88 }
89 
90 int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
91 {
92  MediaCodecDecContext *ctx = buffer->ctx;
93  int released = avpriv_atomic_int_add_and_fetch(&buffer->released, 1);
94 
95  if (released == 1) {
96  return ff_AMediaCodec_releaseOutputBuffer(ctx->codec, buffer->index, render);
97  }
98 
99  return 0;
100 }
101 
102 #else
103 
104 #include <stdlib.h>
105 
107 {
108  return NULL;
109 }
110 
112 {
113  return AVERROR(ENOSYS);
114 }
115 
117 {
118 }
119 
120 int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
121 {
122  return AVERROR(ENOSYS);
123 }
124 
125 #endif
This structure holds a reference to a android/view/Surface object that will be used as output by the ...
Definition: mediacodec.h:33
#define NULL
Definition: coverity.c:32
#define avpriv_atomic_int_add_and_fetch
Definition: atomic_gcc.h:50
Memory handling functions.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:252
int av_mediacodec_default_init(AVCodecContext *avctx, AVMediaCodecContext *ctx, void *surface)
Convenience function that sets up the MediaCodec context.
Definition: mediacodec.c:111
int ff_AMediaCodec_releaseOutputBuffer(FFAMediaCodec *codec, size_t idx, int render)
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:3008
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
error code definitions
#define AVERROR(e)
Definition: error.h:43
AVMediaCodecContext * av_mediacodec_alloc_context(void)
Allocate and initialize a MediaCodec context.
Definition: mediacodec.c:106
AVFormatContext * ctx
Definition: movenc.c:48
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:120
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1676
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:116
JNIEnv * ff_jni_get_env(void *log_ctx)
Definition: ffjni.c:51
void * surface
android/view/Surface object reference.
Definition: mediacodec.h:38
#define av_freep(p)
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
GLuint buffer
Definition: opengl_enc.c:102