FFmpeg
Loading...
Searching...
No Matches
hwcontext_mediacodec.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "config.h"
20
21#include <android/native_window.h>
22#include <dlfcn.h>
23#include <media/NdkMediaCodec.h>
24
25#include "buffer.h"
26#include "common.h"
27#include "hwcontext.h"
28#include "hwcontext_internal.h"
30
33
34 void *libmedia;
35 media_status_t (*create_surface)(ANativeWindow **surface);
37
38
39static int mc_device_create(AVHWDeviceContext *ctx, const char *device,
41{
44 AVMediaCodecDeviceContext *dev = &s->ctx;
45
46 if (device && device[0]) {
47 av_log(ctx, AV_LOG_ERROR, "Device selection unsupported.\n");
48 return AVERROR_UNKNOWN;
49 }
50
51 while ((entry = av_dict_iterate(opts, entry))) {
52 if (!strcmp(entry->key, "create_window"))
53 dev->create_window = atoi(entry->value);
54 }
55
56 av_log(ctx, AV_LOG_DEBUG, "%s createPersistentInputSurface\n",
57 dev->create_window ? "Enable" : "Disable");
58
59 return 0;
60}
61
63{
66 ANativeWindow *native_window = NULL;
67
68 if (dev->surface)
69 return 0;
70
71 if (dev->native_window)
72 return 0;
73
74 // For backward compatibility, don't return error for a dummy
75 // AVHWDeviceContext without surface or native_window.
76 if (!dev->create_window)
77 return 0;
78
79 s->libmedia = dlopen("libmediandk.so", RTLD_NOW);
80 if (!s->libmedia)
81 return AVERROR_UNKNOWN;
82
83 s->create_surface = dlsym(s->libmedia, "AMediaCodec_createPersistentInputSurface");
84 if (!s->create_surface)
85 return AVERROR_UNKNOWN;
86
87 s->create_surface(&native_window);
88 dev->native_window = native_window;
89 return 0;
90}
91
93{
95 AVMediaCodecDeviceContext *dev = ctx->hwctx;
96 if (!s->libmedia)
97 return;
98
99 if (dev->native_window) {
100 ANativeWindow_release(dev->native_window);
101 dev->native_window = NULL;
102 }
103 dlclose(s->libmedia);
104 s->libmedia = NULL;
105}
106
109 .name = "mediacodec",
110
111 .device_hwctx_size = sizeof(MediaCodecDeviceContext),
112
113 .device_create = mc_device_create,
115 .device_uninit = mc_device_uninit,
116
117 .pix_fmts = (const enum AVPixelFormat[]){
120 },
121};
#define entry
refcounted data buffer API
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define s(width, name)
Definition cbs_vp9.c:198
common internal and external API header
#define NULL
Definition coverity.c:32
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition dict.c:42
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition error.h:73
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AV_HWDEVICE_TYPE_MEDIACODEC
Definition hwcontext.h:38
const HWContextType ff_hwcontext_type_mediacodec
static int mc_device_init(AVHWDeviceContext *ctx)
static void mc_device_uninit(AVHWDeviceContext *ctx)
static int mc_device_create(AVHWDeviceContext *ctx, const char *device, AVDictionary *opts, int flags)
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_MEDIACODEC
hardware decoding through MediaCodec
Definition pixfmt.h:316
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition hwcontext.h:63
void * native_window
Pointer to ANativeWindow.
void * surface
android/view/Surface handle, to be filled by the user.
int create_window
Enable createPersistentInputSurface automatically.
AVMediaCodecDeviceContext ctx
media_status_t(* create_surface)(ANativeWindow **surface)
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
static AVDictionary * opts
Definition movenc.c:51
static int device_init(AVFormatContext *ctx, int *width, int *height, uint32_t pixelformat)
Definition v4l2.c:218