]> git.sesse.net Git - ffmpeg/blob - libavcodec/mediacodec.c
Merge commit '470cd0c5fe6337b6cb5276b3f84400999450fc1b'
[ffmpeg] / libavcodec / mediacodec.c
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 #if CONFIG_H264_MEDIACODEC_HWACCEL
26
27 #include <jni.h>
28
29 #include "libavcodec/avcodec.h"
30 #include "libavutil/atomic.h"
31 #include "libavutil/mem.h"
32
33 #include "ffjni.h"
34 #include "mediacodec.h"
35 #include "mediacodecdec.h"
36
37 AVMediaCodecContext *av_mediacodec_alloc_context(void)
38 {
39     return av_mallocz(sizeof(AVMediaCodecContext));
40 }
41
42 int av_mediacodec_default_init(AVCodecContext *avctx, AVMediaCodecContext *ctx, void *surface)
43 {
44     int ret = 0;
45     JNIEnv *env = NULL;
46     int attached = 0;
47
48     env = ff_jni_attach_env(&attached, avctx);
49     if (!env) {
50         return AVERROR_EXTERNAL;
51     }
52
53     ctx->surface = (*env)->NewGlobalRef(env, surface);
54     if (ctx->surface) {
55         avctx->hwaccel_context = ctx;
56     } else {
57         av_log(avctx, AV_LOG_ERROR, "Could not create new global reference\n");
58         ret = AVERROR_EXTERNAL;
59     }
60
61     if (attached) {
62         ff_jni_detach_env(avctx);
63     }
64
65     return ret;
66 }
67
68 void av_mediacodec_default_free(AVCodecContext *avctx)
69 {
70     JNIEnv *env = NULL;
71     int attached = 0;
72
73     AVMediaCodecContext *ctx = avctx->hwaccel_context;
74
75     if (!ctx) {
76         return;
77     }
78
79     env = ff_jni_attach_env(&attached, avctx);
80     if (!env) {
81         return;
82     }
83
84     if (ctx->surface) {
85         (*env)->DeleteGlobalRef(env, ctx->surface);
86         ctx->surface = NULL;
87     }
88
89     if (attached) {
90         ff_jni_detach_env(avctx);
91     }
92
93     av_freep(&avctx->hwaccel_context);
94 }
95
96 int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
97 {
98     MediaCodecDecContext *ctx = buffer->ctx;
99     int released = avpriv_atomic_int_add_and_fetch(&buffer->released, 1);
100
101     if (released == 1) {
102         return ff_AMediaCodec_releaseOutputBuffer(ctx->codec, buffer->index, render);
103     }
104
105     return 0;
106 }
107
108 #else
109
110 #include <stdlib.h>
111
112 #include "mediacodec.h"
113
114 AVMediaCodecContext *av_mediacodec_alloc_context(void)
115 {
116     return NULL;
117 }
118
119 int av_mediacodec_default_init(AVCodecContext *avctx, AVMediaCodecContext *ctx, void *surface)
120 {
121     return 0;
122 }
123
124 void av_mediacodec_default_free(AVCodecContext *avctx)
125 {
126 }
127
128 int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
129 {
130     return 0;
131 }
132
133 #endif