blob: 610bb49a733ba01f0234a580c37cf92070b0206e
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/mem.h" |
35 | |
36 | #include "ffjni.h" |
37 | #include "mediacodecdec_common.h" |
38 | #include "version.h" |
39 | |
40 | AVMediaCodecContext *av_mediacodec_alloc_context(void) |
41 | { |
42 | return av_mallocz(sizeof(AVMediaCodecContext)); |
43 | } |
44 | |
45 | int av_mediacodec_default_init(AVCodecContext *avctx, AVMediaCodecContext *ctx, void *surface) |
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"); |
60 | ret = AVERROR_EXTERNAL; |
61 | } |
62 | |
63 | return ret; |
64 | } |
65 | |
66 | void av_mediacodec_default_free(AVCodecContext *avctx) |
67 | { |
68 | JNIEnv *env = NULL; |
69 | |
70 | AVMediaCodecContext *ctx = avctx->hwaccel_context; |
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 | { |
91 | MediaCodecDecContext *ctx = buffer->ctx; |
92 | int released = atomic_fetch_add(&buffer->released, 1); |
93 | |
94 | if (!released) { |
95 | return ff_AMediaCodec_releaseOutputBuffer(ctx->codec, buffer->index, render); |
96 | } |
97 | |
98 | return 0; |
99 | } |
100 | |
101 | #else |
102 | |
103 | #include <stdlib.h> |
104 | |
105 | AVMediaCodecContext *av_mediacodec_alloc_context(void) |
106 | { |
107 | return NULL; |
108 | } |
109 | |
110 | int av_mediacodec_default_init(AVCodecContext *avctx, AVMediaCodecContext *ctx, void *surface) |
111 | { |
112 | return AVERROR(ENOSYS); |
113 | } |
114 | |
115 | void av_mediacodec_default_free(AVCodecContext *avctx) |
116 | { |
117 | } |
118 | |
119 | int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render) |
120 | { |
121 | return AVERROR(ENOSYS); |
122 | } |
123 | |
124 | #endif |
125 |