]> git.sesse.net Git - ffmpeg/blob - libavcodec/mediacodec.c
Merge commit 'bcd91f1644b46dd142c5355c8b742b27d9028903'
[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
47     env = ff_jni_get_env(avctx);
48     if (!env) {
49         return AVERROR_EXTERNAL;
50     }
51
52     ctx->surface = (*env)->NewGlobalRef(env, surface);
53     if (ctx->surface) {
54         avctx->hwaccel_context = ctx;
55     } else {
56         av_log(avctx, AV_LOG_ERROR, "Could not create new global reference\n");
57         ret = AVERROR_EXTERNAL;
58     }
59
60     return ret;
61 }
62
63 void av_mediacodec_default_free(AVCodecContext *avctx)
64 {
65     JNIEnv *env = NULL;
66
67     AVMediaCodecContext *ctx = avctx->hwaccel_context;
68
69     if (!ctx) {
70         return;
71     }
72
73     env = ff_jni_get_env(avctx);
74     if (!env) {
75         return;
76     }
77
78     if (ctx->surface) {
79         (*env)->DeleteGlobalRef(env, ctx->surface);
80         ctx->surface = NULL;
81     }
82
83     av_freep(&avctx->hwaccel_context);
84 }
85
86 int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
87 {
88     MediaCodecDecContext *ctx = buffer->ctx;
89     int released = avpriv_atomic_int_add_and_fetch(&buffer->released, 1);
90
91     if (released == 1) {
92         return ff_AMediaCodec_releaseOutputBuffer(ctx->codec, buffer->index, render);
93     }
94
95     return 0;
96 }
97
98 #else
99
100 #include <stdlib.h>
101
102 #include "mediacodec.h"
103
104 AVMediaCodecContext *av_mediacodec_alloc_context(void)
105 {
106     return NULL;
107 }
108
109 int av_mediacodec_default_init(AVCodecContext *avctx, AVMediaCodecContext *ctx, void *surface)
110 {
111     return 0;
112 }
113
114 void av_mediacodec_default_free(AVCodecContext *avctx)
115 {
116 }
117
118 int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
119 {
120     return 0;
121 }
122
123 #endif