FFmpeg
Loading...
Searching...
No Matches
hwcontext_cuda.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 <inttypes.h>
20#include <stdatomic.h>
21#include <stdio.h>
22#include <string.h>
23
24#include "libavutil/adler32.h"
25#include "libavutil/hwcontext.h"
27#include "libavutil/imgutils.h"
28#include "libavutil/log.h"
29#include "libavutil/mem.h"
30#include "libavutil/pixdesc.h"
31#include "libavutil/pixfmt.h"
32#include "libavutil/time.h"
33
35{
36 for (int i = 0; i < FF_ARRAY_ELEMS(frame->data) && frame->data[i]; i++) {
37 int shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
38
39 frame->data[i] += (AV_CEIL_RSHIFT(frame->height, shift) - 1) * frame->linesize[i];
40 frame->linesize[i] = -frame->linesize[i];
41 }
42}
43
44static int test_format(AVBufferRef *device_ref, enum AVPixelFormat fmt,
45 int flip_upload, int flip_download)
46{
47 AVBufferRef *frames_ref = NULL;
49 AVFrame *sw_frame = NULL, *hw_frame = NULL, *download = NULL;
51 int ret;
52
53 frames_ref = av_hwframe_ctx_alloc(device_ref);
54 if (!frames_ref) {
55 ret = AVERROR(ENOMEM);
56 goto fail;
57 }
58
59 hwfc = (AVHWFramesContext *)frames_ref->data;
60 hwfc->format = AV_PIX_FMT_CUDA;
61 hwfc->sw_format = fmt;
62 hwfc->width = 64;
63 hwfc->height = 64;
64
65 ret = av_hwframe_ctx_init(frames_ref);
66 if (ret < 0)
67 goto fail;
68
69 sw_frame = av_frame_alloc();
70 hw_frame = av_frame_alloc();
71 download = av_frame_alloc();
72 if (!sw_frame || !hw_frame || !download) {
73 ret = AVERROR(ENOMEM);
74 goto fail;
75 }
76
77 sw_frame->format = fmt;
78 sw_frame->width = 64;
79 sw_frame->height = 64;
80 ret = av_frame_get_buffer(sw_frame, 0);
81 if (ret < 0)
82 goto fail;
83 if (flip_upload)
84 vflip(sw_frame, desc);
85
86 {
87 int linesizes[4];
88 av_image_fill_linesizes(linesizes, fmt, 64);
89
90 for (int i = 0; i < FF_ARRAY_ELEMS(sw_frame->data) && sw_frame->data[i]; i++) {
91 int shift = (i == 1 || i == 2) && desc ? desc->log2_chroma_h : 0;
92 int h = AV_CEIL_RSHIFT(64, shift);
93
94 for (int y = 0; y < h; y++)
95 for (int x = 0; x < linesizes[i]; x++)
96 sw_frame->data[i][y * sw_frame->linesize[i] + x] =
97 (uint8_t)(x + y * 3 + i * 17);
98 }
99 }
100
101 ret = av_hwframe_get_buffer(frames_ref, hw_frame, 0);
102 if (ret < 0)
103 goto fail;
104
105 ret = av_hwframe_transfer_data(hw_frame, sw_frame, 0);
106 if (ret < 0)
107 goto fail;
108
109 download->format = fmt;
110 download->width = 64;
111 download->height = 64;
112 ret = av_frame_get_buffer(download, 0);
113 if (ret < 0)
114 goto fail;
115 if (flip_download)
116 vflip(download, desc);
117
118 ret = av_hwframe_transfer_data(download, hw_frame, 0);
119 if (ret < 0)
120 goto fail;
121
122 {
123 int linesizes[4];
124 av_image_fill_linesizes(linesizes, fmt, 64);
125
126 for (int i = 0; i < FF_ARRAY_ELEMS(download->data) && download->data[i]; i++) {
127 int shift = (i == 1 || i == 2) && desc ? desc->log2_chroma_h : 0;
128 int h = AV_CEIL_RSHIFT(64, shift);
129
130 for (int y = 0; y < h; y++) {
131 if (memcmp(sw_frame->data[i] + y * sw_frame->linesize[i],
132 download->data[i] + y * download->linesize[i],
133 linesizes[i])) {
134 printf("fail: %-16s plane %d row %d mismatch\n",
135 av_get_pix_fmt_name(fmt), i, y);
136 ret = AVERROR(EINVAL);
137 goto fail;
138 }
139 }
140 }
141 }
142
143fail:
144 av_frame_free(&sw_frame);
145 av_frame_free(&hw_frame);
146 av_frame_free(&download);
147 av_buffer_unref(&frames_ref);
148 return ret;
149}
150
152
153/* Hold the stream until the source was overwritten, at most 100 ms. */
154static void CUDAAPI hold_stream(CUstream stream, CUresult status, void *opaque)
155{
157 while (!atomic_load(&source_overwritten) && av_gettime_relative() - start < 100000);
158}
159
160/* Reuse the memory of an uploaded transfer buffer while the stream is still
161 * busy, the upload must not see the new contents. */
162static int test_source_reuse(AVBufferRef *device_ref)
163{
164 AVCUDADeviceContext *hwctx = ((AVHWDeviceContext *)device_ref->data)->hwctx;
165 CudaFunctions *cu = hwctx->internal->cuda_dl;
166 AVBufferRef *frames_ref = av_hwframe_ctx_alloc(device_ref);
167 AVFrame *sw_frame = av_frame_alloc(), *hw_frame = av_frame_alloc();
168 AVFrame *download = av_frame_alloc();
169 AVHWFramesContext *hwfc;
170 CUcontext dummy;
171 AVAdler checksum = 1;
172 int ret = AVERROR(ENOMEM);
173
174 if (!frames_ref || !sw_frame || !hw_frame || !download)
175 goto fail;
176
177 hwfc = (AVHWFramesContext *)frames_ref->data;
178 hwfc->format = AV_PIX_FMT_CUDA;
180 hwfc->width = 64;
181 hwfc->height = 64;
182 if ((ret = av_hwframe_ctx_init(frames_ref)) < 0 ||
183 (ret = av_hwframe_get_buffer(frames_ref, hw_frame, 0)) < 0)
184 goto fail;
185
186 for (int value = 0x11; value <= 0x22; value += 0x11) {
187 av_frame_unref(sw_frame);
188 sw_frame->format = AV_PIX_FMT_GRAY8;
189 sw_frame->width = 64;
190 sw_frame->height = 64;
191 if ((ret = av_hwframe_get_transfer_buffer(frames_ref, sw_frame)) < 0)
192 goto fail;
193 for (int y = 0; y < 64; y++)
194 memset(sw_frame->data[0] + y * sw_frame->linesize[0], value, 64);
195 if (value == 0x22) {
197 break;
198 }
199
201 if (cu->cuCtxPushCurrent(hwctx->cuda_ctx) != CUDA_SUCCESS)
202 goto fail;
203 if (cu->cuStreamAddCallback(hwctx->stream, hold_stream, NULL, 0) == CUDA_SUCCESS)
204 ret = 0;
205 cu->cuCtxPopCurrent(&dummy);
206 if (ret < 0)
207 goto fail;
208
209 if ((ret = av_hwframe_transfer_data(hw_frame, sw_frame, 0)) < 0)
210 goto fail;
211 }
212
213 if ((ret = av_hwframe_transfer_data(download, hw_frame, 0)) < 0)
214 goto fail;
215 for (int y = 0; y < 64; y++)
216 checksum = av_adler32_update(checksum,
217 download->data[0] + y * download->linesize[0], 64);
218 fprintf(stderr, "source reuse checksum: %08"PRIx32"\n", checksum);
219
220fail:
221 av_frame_free(&sw_frame);
222 av_frame_free(&hw_frame);
223 av_frame_free(&download);
224 av_buffer_unref(&frames_ref);
225 return ret;
226}
227
228/* Verify that an unsupported sw_format is refused at init time. Returns 0 when
229 * the format is correctly rejected, and an error when it is wrongly accepted. */
230static int test_rejected(AVBufferRef *device_ref, enum AVPixelFormat fmt)
231{
232 AVBufferRef *frames_ref = av_hwframe_ctx_alloc(device_ref);
233 AVHWFramesContext *hwfc;
234 int ret;
235
236 if (!frames_ref)
237 return AVERROR(ENOMEM);
238
239 hwfc = (AVHWFramesContext *)frames_ref->data;
240 hwfc->format = AV_PIX_FMT_CUDA;
241 hwfc->sw_format = fmt;
242 hwfc->width = 64;
243 hwfc->height = 64;
244
245 /* Init is expected to fail; silence the resulting error log. */
247 ret = av_hwframe_ctx_init(frames_ref);
249
250 av_buffer_unref(&frames_ref);
251
252 return ret >= 0 ? AVERROR(EINVAL) : 0;
253}
254
255int main(void)
256{
257 AVBufferRef *device_ref = NULL;
258 enum AVPixelFormat fmt;
259 int ret, failures = 0, total = 0;
260
262 if (ret < 0) {
263 /* CONFIG_CUDA only requires the ffnvcodec headers and a dynamic
264 * loader; a FATE host that compiled CUDA support need not have an
265 * NVIDIA GPU or usable driver. Skip cleanly in that case so that
266 * make fate does not fail for an unavailable test environment. A real
267 * transfer mismatch below still returns a nonzero exit status. */
268 fprintf(stderr, "No CUDA device available, skipping.\n");
269 return 0;
270 }
271
272 for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
274 if (!desc || (desc->flags & AV_PIX_FMT_FLAG_HWACCEL))
275 continue;
276
277 total++;
278 /* Palette formats must be refused rather than silently dropping the
279 * palette across a transfer; every other format must round-trip. */
280 if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
281 if (test_rejected(device_ref, fmt) < 0) {
282 printf("fail: %-16s wrongly accepted\n",
284 failures++;
285 }
286 } else {
287 ret = 0;
288 for (int flip = 0; flip < 4 && ret >= 0; flip++)
289 ret = test_format(device_ref, fmt, flip & 1, flip & 2);
290 if (ret < 0) {
291 printf("fail: %-16s %s\n",
293 av_err2str(ret));
294 failures++;
295 }
296 }
297 }
298
299 total++;
300 ret = test_source_reuse(device_ref);
301 if (ret < 0) {
302 printf("fail: source reuse %s\n", av_err2str(ret));
303 failures++;
304 }
305
306 av_buffer_unref(&device_ref);
307
308 if (failures)
309 printf("%d / %d tests failed.\n", failures, total);
310 else
311 printf("%d tests passed.\n", total);
312
313 return !!failures;
314}
Public header for Adler-32 hash function implementation.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
__device__ int printf(const char *,...)
static AVFrame * frame
double value
Definition eval.c:102
int ret
Definition ffplay.c:391
static int dummy
Definition ffplay.c:4429
#define fail
Definition test.h:479
uint32_t AVAdler
Definition adler32.h:42
AVAdler av_adler32_update(AVAdler adler, const uint8_t *buf, size_t len)
Calculate the Adler32 checksum of a buffer.
Definition adler32.c:44
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition buffer.c:140
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:499
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition frame.c:209
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:53
#define AV_LOG_QUIET
Print no output.
Definition log.h:192
#define AV_LOG_INFO
Standard information.
Definition log.h:221
void av_log_set_level(int level)
Set the log level.
Definition log.c:482
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition imgutils.c:89
int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type, const char *device, AVDictionary *opts, int flags)
Open a device of the specified type and create an AVHWDeviceContext for it.
Definition hwcontext.c:673
int av_hwframe_get_transfer_buffer(AVBufferRef *hwframe_ctx, AVFrame *frame)
Allocate the buffers of a software frame that data is transferred to or from frames of the given AVHW...
Definition hwcontext.c:442
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition hwcontext.c:347
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition hwcontext.c:267
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
Definition hwcontext.c:498
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
Definition hwcontext.c:564
@ AV_HWDEVICE_TYPE_CUDA
Definition hwcontext.h:30
FFmpeg internal API for CUDA.
misc image utilities
static int shift(int a, int b)
Definition bonk.c:261
static void flip(AVCodecContext *avctx, AVFrame *frame)
Definition rawdec.c:132
const char * desc
Definition libsvtav1.c:83
Memory handling functions.
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition pixdesc.c:3404
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition pixdesc.h:128
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition pixdesc.h:120
pixel format definitions
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NB
number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of...
Definition pixfmt.h:511
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition pixfmt.h:260
static int failures
Definition probetest.c:33
#define FF_ARRAY_ELEMS(a)
#define atomic_store(object, desired)
Definition stdatomic.h:256
int atomic_int
Definition stdatomic.h:63
#define atomic_load(object)
Definition stdatomic.h:250
A reference to a data buffer.
Definition buffer.h:82
uint8_t * data
The data buffer.
Definition buffer.h:90
This struct is allocated as AVHWDeviceContext.hwctx.
AVCUDADeviceContextInternal * internal
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:500
int width
Definition frame.h:551
int height
Definition frame.h:551
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:524
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:566
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition hwcontext.h:63
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition hwcontext.h:200
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition hwcontext.h:213
int width
The allocated dimensions of the frames in this pool.
Definition hwcontext.h:220
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
static int test_source_reuse(AVBufferRef *device_ref)
int main(void)
static void CUDAAPI hold_stream(CUstream stream, CUresult status, void *opaque)
static void vflip(AVFrame *frame, const AVPixFmtDescriptor *desc)
static int test_format(AVBufferRef *device_ref, enum AVPixelFormat fmt, int flip_upload, int flip_download)
static int test_rejected(AVBufferRef *device_ref, enum AVPixelFormat fmt)
static atomic_int source_overwritten
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition time.c:57