Browse code

lavu: Add DRM hwcontext

Mark Thompson authored on 2017/06/19 02:16:27
Showing 11 changed files
... ...
@@ -307,6 +307,7 @@ External library support:
307 307
   --disable-cuvid          disable Nvidia CUVID support [autodetect]
308 308
   --disable-d3d11va        disable Microsoft Direct3D 11 video acceleration code [autodetect]
309 309
   --disable-dxva2          disable Microsoft DirectX 9 video acceleration code [autodetect]
310
+  --enable-libdrm          enable DRM code (Linux) [no]
310 311
   --enable-libmfx          enable Intel MediaSDK (AKA Quick Sync Video) code via libmfx [no]
311 312
   --enable-libnpp          enable Nvidia Performance Primitives-based code [no]
312 313
   --enable-mmal            enable Broadcom Multi-Media Abstraction Layer (Raspberry Pi) via MMAL [no]
... ...
@@ -1567,6 +1568,7 @@ EXTERNAL_LIBRARY_LIST="
1567 1567
     libcaca
1568 1568
     libcelt
1569 1569
     libdc1394
1570
+    libdrm
1570 1571
     libflite
1571 1572
     libfontconfig
1572 1573
     libfreetype
... ...
@@ -5877,6 +5879,7 @@ enabled libcelt           && require libcelt celt/celt.h celt_decode -lcelt0 &&
5877 5877
                                die "ERROR: libcelt must be installed and version must be >= 0.11.0."; }
5878 5878
 enabled libcaca           && require_pkg_config caca caca.h caca_create_canvas
5879 5879
 enabled libdc1394         && require_pkg_config libdc1394-2 dc1394/dc1394.h dc1394_new
5880
+enabled libdrm            && require_pkg_config libdrm xf86drm.h drmGetVersion
5880 5881
 enabled libfdk_aac        && { use_pkg_config fdk-aac "fdk-aac/aacenc_lib.h" aacEncOpen ||
5881 5882
                                { require libfdk_aac fdk-aac/aacenc_lib.h aacEncOpen -lfdk-aac &&
5882 5883
                                  warn "using libfdk without pkg-config"; } }
... ...
@@ -15,6 +15,9 @@ libavutil:     2015-08-28
15 15
 
16 16
 API changes, most recent first:
17 17
 
18
+2017-09-13 - xxxxxxx - lavu 55.75.100 - hwcontext.h hwcontext_drm.h
19
+  Add AV_HWDEVICE_TYPE_DRM and implementation.
20
+
18 21
 2017-09-08 - xxxxxxx - lavfi 6.103.100 - buffersrc.h
19 22
   Add av_buffersrc_close().
20 23
 
... ...
@@ -34,6 +34,7 @@ HEADERS = adler32.h                                                     \
34 34
           hwcontext.h                                                   \
35 35
           hwcontext_cuda.h                                              \
36 36
           hwcontext_d3d11va.h                                           \
37
+          hwcontext_drm.h                                               \
37 38
           hwcontext_dxva2.h                                             \
38 39
           hwcontext_qsv.h                                               \
39 40
           hwcontext_vaapi.h                                             \
... ...
@@ -161,6 +162,7 @@ OBJS-$(CONFIG_CUDA)                     += hwcontext_cuda.o
161 161
 OBJS-$(CONFIG_D3D11VA)                  += hwcontext_d3d11va.o
162 162
 OBJS-$(CONFIG_DXVA2)                    += hwcontext_dxva2.o
163 163
 OBJS-$(CONFIG_QSV)                   += hwcontext_qsv.o
164
+OBJS-$(CONFIG_LIBDRM)                   += hwcontext_drm.o
164 165
 OBJS-$(CONFIG_LZO)                      += lzo.o
165 166
 OBJS-$(CONFIG_OPENCL)                   += opencl.o opencl_internal.o
166 167
 OBJS-$(CONFIG_VAAPI)                    += hwcontext_vaapi.o
... ...
@@ -35,6 +35,9 @@ static const HWContextType *const hw_table[] = {
35 35
 #if CONFIG_D3D11VA
36 36
     &ff_hwcontext_type_d3d11va,
37 37
 #endif
38
+#if CONFIG_LIBDRM
39
+    &ff_hwcontext_type_drm,
40
+#endif
38 41
 #if CONFIG_DXVA2
39 42
     &ff_hwcontext_type_dxva2,
40 43
 #endif
... ...
@@ -55,6 +58,7 @@ static const HWContextType *const hw_table[] = {
55 55
 
56 56
 static const char *const hw_type_names[] = {
57 57
     [AV_HWDEVICE_TYPE_CUDA]   = "cuda",
58
+    [AV_HWDEVICE_TYPE_DRM]    = "drm",
58 59
     [AV_HWDEVICE_TYPE_DXVA2]  = "dxva2",
59 60
     [AV_HWDEVICE_TYPE_D3D11VA] = "d3d11va",
60 61
     [AV_HWDEVICE_TYPE_QSV]    = "qsv",
... ...
@@ -33,6 +33,7 @@ enum AVHWDeviceType {
33 33
     AV_HWDEVICE_TYPE_VIDEOTOOLBOX,
34 34
     AV_HWDEVICE_TYPE_NONE,
35 35
     AV_HWDEVICE_TYPE_D3D11VA,
36
+    AV_HWDEVICE_TYPE_DRM,
36 37
 };
37 38
 
38 39
 typedef struct AVHWDeviceInternal AVHWDeviceInternal;
39 40
new file mode 100644
... ...
@@ -0,0 +1,289 @@
0
+/*
1
+ * This file is part of FFmpeg.
2
+ *
3
+ * FFmpeg is free software; you can redistribute it and/or
4
+ * modify it under the terms of the GNU Lesser General Public
5
+ * License as published by the Free Software Foundation; either
6
+ * version 2.1 of the License, or (at your option) any later version.
7
+ *
8
+ * FFmpeg is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
+ * Lesser General Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Lesser General Public
14
+ * License along with FFmpeg; if not, write to the Free Software
15
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+ */
17
+
18
+#include <fcntl.h>
19
+#include <sys/mman.h>
20
+#include <unistd.h>
21
+
22
+#include <drm.h>
23
+#include <xf86drm.h>
24
+
25
+#include "avassert.h"
26
+#include "hwcontext.h"
27
+#include "hwcontext_drm.h"
28
+#include "hwcontext_internal.h"
29
+#include "imgutils.h"
30
+
31
+
32
+static void drm_device_free(AVHWDeviceContext *hwdev)
33
+{
34
+    AVDRMDeviceContext *hwctx = hwdev->hwctx;
35
+
36
+    close(hwctx->fd);
37
+}
38
+
39
+static int drm_device_create(AVHWDeviceContext *hwdev, const char *device,
40
+                             AVDictionary *opts, int flags)
41
+{
42
+    AVDRMDeviceContext *hwctx = hwdev->hwctx;
43
+    drmVersionPtr version;
44
+
45
+    hwctx->fd = open(device, O_RDWR);
46
+    if (hwctx->fd < 0)
47
+        return AVERROR(errno);
48
+
49
+    version = drmGetVersion(hwctx->fd);
50
+    if (!version) {
51
+        av_log(hwdev, AV_LOG_ERROR, "Failed to get version information "
52
+               "from %s: probably not a DRM device?\n", device);
53
+        close(hwctx->fd);
54
+        return AVERROR(EINVAL);
55
+    }
56
+
57
+    av_log(hwdev, AV_LOG_VERBOSE, "Opened DRM device %s: driver %s "
58
+           "version %d.%d.%d.\n", device, version->name,
59
+           version->version_major, version->version_minor,
60
+           version->version_patchlevel);
61
+
62
+    drmFreeVersion(version);
63
+
64
+    hwdev->free = &drm_device_free;
65
+
66
+    return 0;
67
+}
68
+
69
+static int drm_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame)
70
+{
71
+    frame->buf[0] = av_buffer_pool_get(hwfc->pool);
72
+    if (!frame->buf[0])
73
+        return AVERROR(ENOMEM);
74
+
75
+    frame->data[0] = (uint8_t*)frame->buf[0]->data;
76
+
77
+    frame->format = AV_PIX_FMT_DRM_PRIME;
78
+    frame->width  = hwfc->width;
79
+    frame->height = hwfc->height;
80
+
81
+    return 0;
82
+}
83
+
84
+typedef struct DRMMapping {
85
+    // Address and length of each mmap()ed region.
86
+    int nb_regions;
87
+    void *address[AV_DRM_MAX_PLANES];
88
+    size_t length[AV_DRM_MAX_PLANES];
89
+} DRMMapping;
90
+
91
+static void drm_unmap_frame(AVHWFramesContext *hwfc,
92
+                            HWMapDescriptor *hwmap)
93
+{
94
+    DRMMapping *map = hwmap->priv;
95
+    int i;
96
+
97
+    for (i = 0; i < map->nb_regions; i++)
98
+        munmap(map->address[i], map->length[i]);
99
+
100
+    av_free(map);
101
+}
102
+
103
+static int drm_map_frame(AVHWFramesContext *hwfc,
104
+                         AVFrame *dst, const AVFrame *src, int flags)
105
+{
106
+    const AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor*)src->data[0];
107
+    DRMMapping *map;
108
+    int err, i, p, plane;
109
+    int mmap_prot;
110
+    void *addr;
111
+
112
+    map = av_mallocz(sizeof(*map));
113
+    if (!map)
114
+        return AVERROR(ENOMEM);
115
+
116
+    mmap_prot = 0;
117
+    if (flags & AV_HWFRAME_MAP_READ)
118
+        mmap_prot |= PROT_READ;
119
+    if (flags & AV_HWFRAME_MAP_WRITE)
120
+        mmap_prot |= PROT_WRITE;
121
+
122
+    av_assert0(desc->nb_objects <= AV_DRM_MAX_PLANES);
123
+    for (i = 0; i < desc->nb_objects; i++) {
124
+        addr = mmap(NULL, desc->objects[i].size, mmap_prot, MAP_SHARED,
125
+                    desc->objects[i].fd, 0);
126
+        if (addr == MAP_FAILED) {
127
+            err = AVERROR(errno);
128
+            av_log(hwfc, AV_LOG_ERROR, "Failed to map DRM object %d to "
129
+                   "memory: %d.\n", desc->objects[i].fd, errno);
130
+            goto fail;
131
+        }
132
+
133
+        map->address[i] = addr;
134
+        map->length[i]  = desc->objects[i].size;
135
+    }
136
+    map->nb_regions = i;
137
+
138
+    plane = 0;
139
+    for (i = 0; i < desc->nb_layers; i++) {
140
+        const AVDRMLayerDescriptor *layer = &desc->layers[i];
141
+        for (p = 0; p < layer->nb_planes; p++) {
142
+            dst->data[plane] =
143
+                (uint8_t*)map->address[layer->planes[p].object_index] +
144
+                                       layer->planes[p].offset;
145
+            dst->linesize[plane] =     layer->planes[p].pitch;
146
+            ++plane;
147
+        }
148
+    }
149
+    av_assert0(plane <= AV_DRM_MAX_PLANES);
150
+
151
+    dst->width  = src->width;
152
+    dst->height = src->height;
153
+
154
+    err = ff_hwframe_map_create(src->hw_frames_ctx, dst, src,
155
+                                &drm_unmap_frame, map);
156
+    if (err < 0)
157
+        goto fail;
158
+
159
+    return 0;
160
+
161
+fail:
162
+    for (i = 0; i < desc->nb_objects; i++) {
163
+        if (map->address[i])
164
+            munmap(map->address[i], map->length[i]);
165
+    }
166
+    av_free(map);
167
+    return err;
168
+}
169
+
170
+static int drm_transfer_get_formats(AVHWFramesContext *ctx,
171
+                                    enum AVHWFrameTransferDirection dir,
172
+                                    enum AVPixelFormat **formats)
173
+{
174
+    enum AVPixelFormat *pix_fmts;
175
+
176
+    pix_fmts = av_malloc_array(2, sizeof(*pix_fmts));
177
+    if (!pix_fmts)
178
+        return AVERROR(ENOMEM);
179
+
180
+    pix_fmts[0] = ctx->sw_format;
181
+    pix_fmts[1] = AV_PIX_FMT_NONE;
182
+
183
+    *formats = pix_fmts;
184
+    return 0;
185
+}
186
+
187
+static int drm_transfer_data_from(AVHWFramesContext *hwfc,
188
+                                  AVFrame *dst, const AVFrame *src)
189
+{
190
+    AVFrame *map;
191
+    int err;
192
+
193
+    if (dst->width > hwfc->width || dst->height > hwfc->height)
194
+        return AVERROR(EINVAL);
195
+
196
+    map = av_frame_alloc();
197
+    if (!map)
198
+        return AVERROR(ENOMEM);
199
+    map->format = dst->format;
200
+
201
+    err = drm_map_frame(hwfc, map, src, AV_HWFRAME_MAP_READ);
202
+    if (err)
203
+        goto fail;
204
+
205
+    map->width  = dst->width;
206
+    map->height = dst->height;
207
+
208
+    err = av_frame_copy(dst, map);
209
+    if (err)
210
+        goto fail;
211
+
212
+    err = 0;
213
+fail:
214
+    av_frame_free(&map);
215
+    return err;
216
+}
217
+
218
+static int drm_transfer_data_to(AVHWFramesContext *hwfc,
219
+                                AVFrame *dst, const AVFrame *src)
220
+{
221
+    AVFrame *map;
222
+    int err;
223
+
224
+    if (src->width > hwfc->width || src->height > hwfc->height)
225
+        return AVERROR(EINVAL);
226
+
227
+    map = av_frame_alloc();
228
+    if (!map)
229
+        return AVERROR(ENOMEM);
230
+    map->format = src->format;
231
+
232
+    err = drm_map_frame(hwfc, map, dst, AV_HWFRAME_MAP_WRITE |
233
+                                        AV_HWFRAME_MAP_OVERWRITE);
234
+    if (err)
235
+        goto fail;
236
+
237
+    map->width  = src->width;
238
+    map->height = src->height;
239
+
240
+    err = av_frame_copy(map, src);
241
+    if (err)
242
+        goto fail;
243
+
244
+    err = 0;
245
+fail:
246
+    av_frame_free(&map);
247
+    return err;
248
+}
249
+
250
+static int drm_map_from(AVHWFramesContext *hwfc, AVFrame *dst,
251
+                        const AVFrame *src, int flags)
252
+{
253
+    int err;
254
+
255
+    if (hwfc->sw_format != dst->format)
256
+        return AVERROR(ENOSYS);
257
+
258
+    err = drm_map_frame(hwfc, dst, src, flags);
259
+    if (err)
260
+        return err;
261
+
262
+    err = av_frame_copy_props(dst, src);
263
+    if (err)
264
+        return err;
265
+
266
+    return 0;
267
+}
268
+
269
+const HWContextType ff_hwcontext_type_drm = {
270
+    .type                   = AV_HWDEVICE_TYPE_DRM,
271
+    .name                   = "DRM",
272
+
273
+    .device_hwctx_size      = sizeof(AVDRMDeviceContext),
274
+
275
+    .device_create          = &drm_device_create,
276
+
277
+    .frames_get_buffer      = &drm_get_buffer,
278
+
279
+    .transfer_get_formats   = &drm_transfer_get_formats,
280
+    .transfer_data_to       = &drm_transfer_data_to,
281
+    .transfer_data_from     = &drm_transfer_data_from,
282
+    .map_from               = &drm_map_from,
283
+
284
+    .pix_fmts = (const enum AVPixelFormat[]) {
285
+        AV_PIX_FMT_DRM_PRIME,
286
+        AV_PIX_FMT_NONE
287
+    },
288
+};
0 289
new file mode 100644
... ...
@@ -0,0 +1,166 @@
0
+/*
1
+ * This file is part of FFmpeg.
2
+ *
3
+ * FFmpeg is free software; you can redistribute it and/or
4
+ * modify it under the terms of the GNU Lesser General Public
5
+ * License as published by the Free Software Foundation; either
6
+ * version 2.1 of the License, or (at your option) any later version.
7
+ *
8
+ * FFmpeg is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
+ * Lesser General Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Lesser General Public
14
+ * License along with FFmpeg; if not, write to the Free Software
15
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+ */
17
+
18
+#ifndef AVUTIL_HWCONTEXT_DRM_H
19
+#define AVUTIL_HWCONTEXT_DRM_H
20
+
21
+#include <stddef.h>
22
+#include <stdint.h>
23
+
24
+/**
25
+ * @file
26
+ * API-specific header for AV_HWDEVICE_TYPE_DRM.
27
+ *
28
+ * Internal frame allocation is not currently supported - all frames
29
+ * must be allocated by the user.  Thus AVHWFramesContext is always
30
+ * NULL, though this may change if support for frame allocation is
31
+ * added in future.
32
+ */
33
+
34
+enum {
35
+    /**
36
+     * The maximum number of layers/planes in a DRM frame.
37
+     */
38
+    AV_DRM_MAX_PLANES = 4
39
+};
40
+
41
+/**
42
+ * DRM object descriptor.
43
+ *
44
+ * Describes a single DRM object, addressing it as a PRIME file
45
+ * descriptor.
46
+ */
47
+typedef struct AVDRMObjectDescriptor {
48
+    /**
49
+     * DRM PRIME fd for the object.
50
+     */
51
+    int fd;
52
+    /**
53
+     * Total size of the object.
54
+     *
55
+     * (This includes any parts not which do not contain image data.)
56
+     */
57
+    size_t size;
58
+    /**
59
+     * Format modifier applied to the object (DRM_FORMAT_MOD_*).
60
+     */
61
+    uint64_t format_modifier;
62
+} AVDRMObjectDescriptor;
63
+
64
+/**
65
+ * DRM plane descriptor.
66
+ *
67
+ * Describes a single plane of a layer, which is contained within
68
+ * a single object.
69
+ */
70
+typedef struct AVDRMPlaneDescriptor {
71
+    /**
72
+     * Index of the object containing this plane in the objects
73
+     * array of the enclosing frame descriptor.
74
+     */
75
+    int object_index;
76
+    /**
77
+     * Offset within that object of this plane.
78
+     */
79
+    ptrdiff_t offset;
80
+    /**
81
+     * Pitch (linesize) of this plane.
82
+     */
83
+    ptrdiff_t pitch;
84
+} AVDRMPlaneDescriptor;
85
+
86
+/**
87
+ * DRM layer descriptor.
88
+ *
89
+ * Describes a single layer within a frame.  This has the structure
90
+ * defined by its format, and will contain one or more planes.
91
+ */
92
+typedef struct AVDRMLayerDescriptor {
93
+    /**
94
+     * Format of the layer (DRM_FORMAT_*).
95
+     */
96
+    uint32_t format;
97
+    /**
98
+     * Number of planes in the layer.
99
+     *
100
+     * This must match the number of planes required by format.
101
+     */
102
+    int nb_planes;
103
+    /**
104
+     * Array of planes in this layer.
105
+     */
106
+    AVDRMPlaneDescriptor planes[AV_DRM_MAX_PLANES];
107
+} AVDRMLayerDescriptor;
108
+
109
+/**
110
+ * DRM frame descriptor.
111
+ *
112
+ * This is used as the data pointer for AV_PIX_FMT_DRM_PRIME frames.
113
+ * It is also used by user-allocated frame pools - allocating in
114
+ * AVHWFramesContext.pool must return AVBufferRefs which contain
115
+ * an object of this type.
116
+ *
117
+ * The fields of this structure should be set such it can be
118
+ * imported directly by EGL using the EGL_EXT_image_dma_buf_import
119
+ * and EGL_EXT_image_dma_buf_import_modifiers extensions.
120
+ * (Note that the exact layout of a particular format may vary between
121
+ * platforms - we only specify that the same platform should be able
122
+ * to import it.)
123
+ *
124
+ * The total number of planes must not exceed AV_DRM_MAX_PLANES, and
125
+ * the order of the planes by increasing layer index followed by
126
+ * increasing plane index must be the same as the order which would
127
+ * be used for the data pointers in the equivalent software format.
128
+ */
129
+typedef struct AVDRMFrameDescriptor {
130
+    /**
131
+     * Number of DRM objects making up this frame.
132
+     */
133
+    int nb_objects;
134
+    /**
135
+     * Array of objects making up the frame.
136
+     */
137
+    AVDRMObjectDescriptor objects[AV_DRM_MAX_PLANES];
138
+    /**
139
+     * Number of layers in the frame.
140
+     */
141
+    int nb_layers;
142
+    /**
143
+     * Array of layers in the frame.
144
+     */
145
+    AVDRMLayerDescriptor layers[AV_DRM_MAX_PLANES];
146
+} AVDRMFrameDescriptor;
147
+
148
+/**
149
+ * DRM device.
150
+ *
151
+ * Allocated as AVHWDeviceContext.hwctx.
152
+ */
153
+typedef struct AVDRMDeviceContext {
154
+    /**
155
+     * File descriptor of DRM device.
156
+     *
157
+     * This is used as the device to create frames on, and may also be
158
+     * used in some derivation and mapping operations.
159
+     *
160
+     * If no device is required, set to -1.
161
+     */
162
+    int fd;
163
+} AVDRMDeviceContext;
164
+
165
+#endif /* AVUTIL_HWCONTEXT_DRM_H */
... ...
@@ -159,6 +159,7 @@ int ff_hwframe_map_create(AVBufferRef *hwframe_ref,
159 159
 
160 160
 extern const HWContextType ff_hwcontext_type_cuda;
161 161
 extern const HWContextType ff_hwcontext_type_d3d11va;
162
+extern const HWContextType ff_hwcontext_type_drm;
162 163
 extern const HWContextType ff_hwcontext_type_dxva2;
163 164
 extern const HWContextType ff_hwcontext_type_qsv;
164 165
 extern const HWContextType ff_hwcontext_type_vaapi;
... ...
@@ -2237,6 +2237,10 @@ static const AVPixFmtDescriptor av_pix_fmt_descriptors[AV_PIX_FMT_NB] = {
2237 2237
         .flags = AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_ALPHA |
2238 2238
                  AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_FLOAT,
2239 2239
     },
2240
+    [AV_PIX_FMT_DRM_PRIME] = {
2241
+        .name = "drm_prime",
2242
+        .flags = AV_PIX_FMT_FLAG_HWACCEL,
2243
+    },
2240 2244
 };
2241 2245
 #if FF_API_PLUS1_MINUS1
2242 2246
 FF_ENABLE_DEPRECATION_WARNINGS
... ...
@@ -334,6 +334,13 @@ enum AVPixelFormat {
334 334
     AV_PIX_FMT_GBRAPF32BE, ///< IEEE-754 single precision planar GBRA 4:4:4:4, 128bpp, big-endian
335 335
     AV_PIX_FMT_GBRAPF32LE, ///< IEEE-754 single precision planar GBRA 4:4:4:4, 128bpp, little-endian
336 336
 
337
+    /**
338
+     * DRM-managed buffers exposed through PRIME buffer sharing.
339
+     *
340
+     * data[0] points to an AVDRMFrameDescriptor.
341
+     */
342
+    AV_PIX_FMT_DRM_PRIME,
343
+
337 344
     AV_PIX_FMT_NB         ///< number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of formats might differ between versions
338 345
 };
339 346
 
... ...
@@ -80,7 +80,7 @@
80 80
 
81 81
 
82 82
 #define LIBAVUTIL_VERSION_MAJOR  55
83
-#define LIBAVUTIL_VERSION_MINOR  74
83
+#define LIBAVUTIL_VERSION_MINOR  75
84 84
 #define LIBAVUTIL_VERSION_MICRO 100
85 85
 
86 86
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \