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
27
28 #define CUDA_FRAME_ALIGNMENT 256
29
33
41 };
42
44 const void *hwconfig,
46 {
47 int i;
48
53
57
61
64
65 return 0;
66 }
67
69 {
73
75
76 cu->cuCtxPushCurrent(hwctx->
cuda_ctx);
77
78 cu->cuMemFree((CUdeviceptr)data);
79
80 cu->cuCtxPopCurrent(&dummy);
81 }
82
84 {
88
92 CUresult err;
93
94 err = cu->cuCtxPushCurrent(hwctx->
cuda_ctx);
95 if (err != CUDA_SUCCESS) {
98 }
99
100 err = cu->cuMemAlloc(&data, size);
101 if (err != CUDA_SUCCESS)
103
105 if (!ret) {
106 cu->cuMemFree(data);
108 }
109
111 cu->cuCtxPopCurrent(&dummy);
112 return ret;
113 }
114
116 {
119 int i;
120
123 break;
124 }
129 }
130
132
135
139 size = aligned_width * ctx->
height * 3 / 2;
140 break;
144 size = aligned_width * ctx->
height * 3;
145 break;
147 size = aligned_width * ctx->
height * 6;
148 break;
149 default:
152 }
153
157 }
158
159 return 0;
160 }
161
163 {
164 int aligned_width;
165 int width_in_bytes = ctx->
width;
166
170 width_in_bytes *= 2;
171 }
173
177
186 break;
190 frame->
data[1] = frame->
data[2] + aligned_width * ctx->
height / 4;
192 frame->
linesize[1] = aligned_width / 2;
193 frame->
linesize[2] = aligned_width / 2;
194 break;
203 break;
204 default:
207 }
208
212
213 return 0;
214 }
215
219 {
221
223 if (!fmts)
225
228
229 *formats = fmts;
230
231 return 0;
232 }
233
236 {
240
242 CUresult err;
243 int i;
244
245 err = cu->cuCtxPushCurrent(device_hwctx->
cuda_ctx);
246 if (err != CUDA_SUCCESS)
248
250 CUDA_MEMCPY2D cpy = {
251 .srcMemoryType = CU_MEMORYTYPE_DEVICE,
252 .dstMemoryType = CU_MEMORYTYPE_HOST,
253 .srcDevice = (CUdeviceptr)src->
data[i],
254 .dstHost = dst->
data[i],
259 };
260
261 err = cu->cuMemcpy2D(&cpy);
262 if (err != CUDA_SUCCESS) {
265 }
266 }
267
268 cu->cuCtxPopCurrent(&dummy);
269
270 return 0;
271 }
272
275 {
279
281 CUresult err;
282 int i;
283
284 err = cu->cuCtxPushCurrent(device_hwctx->
cuda_ctx);
285 if (err != CUDA_SUCCESS)
287
289 CUDA_MEMCPY2D cpy = {
290 .srcMemoryType = CU_MEMORYTYPE_HOST,
291 .dstMemoryType = CU_MEMORYTYPE_DEVICE,
292 .srcHost = src->
data[i],
293 .dstDevice = (CUdeviceptr)dst->
data[i],
298 };
299
300 err = cu->cuMemcpy2D(&cpy);
301 if (err != CUDA_SUCCESS) {
304 }
305 }
306
307 cu->cuCtxPopCurrent(&dummy);
308
309 return 0;
310 }
311
313 {
315
320 }
322 }
323
325 }
326
328 {
330 int ret;
331
336 }
337
340 if (ret < 0) {
343 }
344 }
345
346 return 0;
347
350 return ret;
351 }
352
355 {
357 CudaFunctions *cu;
358 CUdevice cu_device;
360 CUresult err;
361 int device_idx = 0;
362
363 if (device)
364 device_idx = strtol(device,
NULL, 0);
365
368
370
371 err = cu->cuInit(0);
372 if (err != CUDA_SUCCESS) {
375 }
376
377 err = cu->cuDeviceGet(&cu_device, device_idx);
378 if (err != CUDA_SUCCESS) {
381 }
382
383 err = cu->cuCtxCreate(&hwctx->
cuda_ctx, CU_CTX_SCHED_BLOCKING_SYNC, cu_device);
384 if (err != CUDA_SUCCESS) {
387 }
388
389 cu->cuCtxPopCurrent(&dummy);
390
392
393 return 0;
394
398 }
399
402 .name = "CUDA",
403
406
416
418 };
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Memory handling functions.
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
AVCUDADeviceContextInternal * internal
int width
The allocated dimensions of the frames in this pool.
static int cuda_frames_get_constraints(AVHWDeviceContext *ctx, const void *hwconfig, AVHWFramesConstraints *constraints)
static int cuda_frames_init(AVHWFramesContext *ctx)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
AVBufferPool * pool_internal
static enum AVPixelFormat supported_formats[]
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
static int cuda_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src)
#define AV_PIX_FMT_YUV444P16
static void cuda_buffer_free(void *opaque, uint8_t *data)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
static int cuda_device_init(AVHWDeviceContext *ctx)
static int cuda_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
static int cuda_transfer_get_formats(AVHWFramesContext *ctx, enum AVHWFrameTransferDirection dir, enum AVPixelFormat **formats)
static AVBufferRef * cuda_pool_alloc(void *opaque, int size)
FFmpeg internal API for CUDA.
HW acceleration through CUDA.
AVBufferPool * av_buffer_pool_init2(int size, void *opaque, AVBufferRef *(*alloc)(void *opaque, int size), void(*pool_free)(void *opaque))
Allocate and initialize a buffer pool with a more complex allocator.
static void error(const char *err)
#define FF_ARRAY_ELEMS(a)
#define CUDA_FRAME_ALIGNMENT
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
This struct describes the constraints on hardware frames attached to a given device with a hardware-s...
static int cuda_transfer_data_to(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
const HWContextType ff_hwcontext_type_cuda
uint8_t * data
The data buffer.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
This struct is allocated as AVHWDeviceContext.hwctx.
This struct describes a set or pool of "hardware" frames (i.e.
refcounted data buffer API
enum AVPixelFormat * valid_hw_formats
A list of possible values for format in the hw_frames_ctx, terminated by AV_PIX_FMT_NONE.
AVHWFramesInternal * internal
Private data used internally by libavutil.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
A reference to a data buffer.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
common internal and external API header
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
AVHWFrameTransferDirection
AVBufferPool * pool
A pool from which the frames are allocated by av_hwframe_get_buffer().
static int cuda_device_create(AVHWDeviceContext *ctx, const char *device, AVDictionary *opts, int flags)
enum AVPixelFormat * valid_sw_formats
A list of possible values for sw_format in the hw_frames_ctx, terminated by AV_PIX_FMT_NONE.
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
#define av_malloc_array(a, b)
static void cuda_device_uninit(AVHWDeviceContext *ctx)
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.
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
AVPixelFormat
Pixel format.