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
20
30
32 #if CONFIG_CUDA
34 #endif
35 #if CONFIG_DXVA2
37 #endif
38 #if CONFIG_QSV
40 #endif
41 #if CONFIG_VAAPI
43 #endif
44 #if CONFIG_VDPAU
46 #endif
48 };
49
54 };
55
57 {
59
60 /* uninit might still want access the hw context and the user
61 * free() callback might destroy it, so uninit has to be called first */
64
67
72 }
73
75 {
79 int i;
80
81 for (i = 0; hw_table[i]; i++) {
82 if (hw_table[i]->type == type) {
83 hw_type = hw_table[i];
84 break;
85 }
86 }
87 if (!hw_type)
89
91 if (!ctx)
93
97
102 }
103
108 }
109
113 if (!buf)
115
118
120
122
130 }
131
133 {
135 int ret;
136
139 if (ret < 0)
141 }
142
143 return 0;
147 return ret;
148 }
149
154 };
155
157 {
159
162
165
168
170
175 }
176
178 {
183
187
191
192 if (hw_type->frames_priv_size) {
196 }
197
198 if (hw_type->frames_hwctx_size) {
202 }
203
205 if (!device_ref)
207
211 if (!buf)
213
215 ctx->device_ref = device_ref;
216 ctx->device_ctx = device_ctx;
219
221
223
225 if (device_ref)
233 }
234
236 {
239 int i, ret = 0;
240
244
249
251 if (ret < 0)
253 }
254
259
260 return ret;
261 }
262
264 {
267 int ret;
268
269 /* validate the pixel format */
272 break;
273 }
276 "The hardware pixel format '%s' is not supported by the device type '%s'\n",
279 }
280
281 /* validate the dimensions */
283 if (ret < 0)
284 return ret;
285
286 /* format-specific init */
289 if (ret < 0)
291 }
292
295
296 /* preallocate the frames in the pool, if requested */
299 if (ret < 0)
301 }
302
303 return 0;
307 return ret;
308 }
309
313 {
315
318
320 }
321
323 {
325 int ret = 0;
326
328 if (!frame_tmp)
330
331 /* if the format is set, use that
332 * otherwise pick the first supported one */
335 } else {
337
340 &formats, 0);
341 if (ret < 0)
343 frame_tmp->
format = formats[0];
345 }
348
350 if (ret < 0)
352
354 if (ret < 0)
356
358
361 return ret;
362 }
363
365 {
367 int ret;
368
371
374
376 if (ret < 0)
377 return ret;
380
382 if (ret < 0)
383 return ret;
384 } else
386
387 return 0;
388 }
389
391 {
393 int ret;
394
397
400
404
406 if (ret < 0) {
408 return ret;
409 }
410
411 return 0;
412 }
413
415 {
418
419 if (hw_type->device_hwconfig_size == 0)
421
422 return av_mallocz(hw_type->device_hwconfig_size);
423 }
424
426 const void *hwconfig)
427 {
431
432 if (!hw_type->frames_get_constraints)
434
435 constraints =
av_mallocz(
sizeof(*constraints));
436 if (!constraints)
438
439 constraints->min_width = constraints->min_height = 0;
440 constraints->max_width = constraints->max_height = INT_MAX;
441
442 if (hw_type->frames_get_constraints(ctx, hwconfig, constraints) >= 0) {
443 return constraints;
444 } else {
447 }
448 }
449
451 {
452 if (*constraints) {
453 av_freep(&(*constraints)->valid_hw_formats);
454 av_freep(&(*constraints)->valid_sw_formats);
455 }
457 }
458
461 {
464 int ret = 0;
465
467 if (!device_ref) {
470 }
472
476 }
477
479 opts, flags);
480 if (ret < 0)
482
484 if (ret < 0)
486
487 *pdevice_ref = device_ref;
488 return 0;
492 return ret;
493 }
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
static enum AVPixelFormat pix_fmt
static void hwdevice_ctx_free(void *opaque, uint8_t *data)
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
void(* frames_uninit)(AVHWFramesContext *ctx)
#define LIBAVUTIL_VERSION_INT
Memory handling functions.
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
const HWContextType ff_hwcontext_type_vdpau
AVFormatInternal * internal
An opaque field for libavformat internal usage.
static enum AVSampleFormat formats[]
int width
The allocated dimensions of the frames in this pool.
void * av_hwdevice_hwconfig_alloc(AVBufferRef *ref)
Allocate a HW-specific configuration structure for a given HW device.
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
size_t device_priv_size
size of the private data, i.e.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
const HWContextType * hw_type
void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
Free an AVHWFrameConstraints structure.
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame...
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
AVBufferPool * pool_internal
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
static int hwframe_pool_prealloc(AVBufferRef *ref)
size_t device_hwctx_size
size of the public hardware-specific context, i.e.
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
const HWContextType ff_hwcontext_type_qsv
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.
static void hwframe_ctx_free(void *opaque, uint8_t *data)
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
static const AVClass hwframe_ctx_class
void(* free)(struct AVHWFramesContext *ctx)
This field may be set by the caller before calling av_hwframe_ctx_init().
int width
width and height of the video frame
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void(* free)(struct AVHWDeviceContext *ctx)
This field may be set by the caller before calling av_hwdevice_ctx_init().
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
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.
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
enum AVHWDeviceType type
This field identifies the underlying API used for hardware access.
static void * av_mallocz_array(size_t nmemb, size_t size)
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
void(* device_uninit)(AVHWDeviceContext *ctx)
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
int initial_pool_size
Initial size of the frame pool.
Transfer the data from the queried hw frame.
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
enum AVPixelFormat * pix_fmts
An array of pixel formats supported by the AVHWFramesContext instances Terminated by AV_PIX_FMT_NONE...
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
AVBufferRef * av_hwdevice_ctx_alloc(enum AVHWDeviceType type)
Allocate an AVHWDeviceContext for a given pixel format.
This struct describes the constraints on hardware frames attached to a given device with a hardware-s...
int(* device_create)(AVHWDeviceContext *ctx, const char *device, AVDictionary *opts, int flags)
const HWContextType ff_hwcontext_type_cuda
AVHWFramesConstraints * av_hwdevice_get_hwframe_constraints(AVBufferRef *ref, const void *hwconfig)
Get the constraints on HW frames given a device and the HW-specific configuration to be used with tha...
uint8_t * data
The data buffer.
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
const AVClass * av_class
A class for logging.
int av_hwdevice_ctx_init(AVBufferRef *ref)
Finalize the device context before use.
Describe the class of an AVClass context structure.
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
int(* frames_get_buffer)(AVHWFramesContext *ctx, AVFrame *frame)
This struct describes a set or pool of "hardware" frames (i.e.
const AVClass * av_class
A class for logging and AVOptions.
refcounted data buffer API
static const HWContextType * hw_table[]
static int transfer_data_alloc(AVFrame *dst, const AVFrame *src, int flags)
AVHWFramesInternal * internal
Private data used internally by libavutil.
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
int(* transfer_data_from)(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src)
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
int(* transfer_data_to)(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src)
A reference to a data buffer.
const HWContextType * hw_type
common internal and external API header
static int ref[MAX_W *MAX_W]
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
AVHWFrameTransferDirection
AVBufferPool * pool
A pool from which the frames are allocated by av_hwframe_get_buffer().
int(* transfer_get_formats)(AVHWFramesContext *ctx, enum AVHWFrameTransferDirection dir, enum AVPixelFormat **formats)
int(* device_init)(AVHWDeviceContext *ctx)
AVHWDeviceInternal * internal
Private data used internally by libavutil.
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.
static const AVClass hwdevice_ctx_class
int(* frames_init)(AVHWFramesContext *ctx)
const HWContextType ff_hwcontext_type_dxva2
AVPixelFormat
Pixel format.
int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref, enum AVHWFrameTransferDirection dir, enum AVPixelFormat **formats, int flags)
Get a list of possible source or target formats usable in av_hwframe_transfer_data().
const HWContextType ff_hwcontext_type_vaapi