FFmpeg
vf_hwupload_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 "libavutil/buffer.h"
20 #include "libavutil/hwcontext.h"
21 #include "libavutil/log.h"
22 #include "libavutil/opt.h"
23 
24 #include "avfilter.h"
25 #include "filters.h"
26 #include "formats.h"
27 #include "video.h"
28 
29 typedef struct CudaUploadContext {
30  const AVClass *class;
32 
36 
38 {
39  CudaUploadContext *s = ctx->priv;
40  char buf[64] = { 0 };
41 
42  snprintf(buf, sizeof(buf), "%d", s->device_idx);
43 
44  return av_hwdevice_ctx_create(&s->hwdevice, AV_HWDEVICE_TYPE_CUDA, buf, NULL, 0);
45 }
46 
48 {
49  CudaUploadContext *s = ctx->priv;
50 
51  av_buffer_unref(&s->hwframe);
52  av_buffer_unref(&s->hwdevice);
53 }
54 
56  AVFilterFormatsConfig **cfg_in,
57  AVFilterFormatsConfig **cfg_out)
58 {
59  int ret;
60 
61  static const enum AVPixelFormat input_pix_fmts[] = {
65 #if CONFIG_VULKAN
67 #endif
69  };
70  static const enum AVPixelFormat output_pix_fmts[] = {
72  };
73  AVFilterFormats *in_fmts = ff_make_format_list(input_pix_fmts);
74  AVFilterFormats *out_fmts;
75 
76  ret = ff_formats_ref(in_fmts, &cfg_in[0]->formats);
77  if (ret < 0)
78  return ret;
79 
80  out_fmts = ff_make_format_list(output_pix_fmts);
81 
82  ret = ff_formats_ref(out_fmts, &cfg_out[0]->formats);
83  if (ret < 0)
84  return ret;
85 
86  return 0;
87 }
88 
90 {
91  FilterLink *outl = ff_filter_link(outlink);
92  AVFilterContext *ctx = outlink->src;
93  AVFilterLink *inlink = ctx->inputs[0];
95  CudaUploadContext *s = ctx->priv;
96 
97  AVHWFramesContext *hwframe_ctx;
98  int ret;
99 
100  av_buffer_unref(&s->hwframe);
101  s->hwframe = av_hwframe_ctx_alloc(s->hwdevice);
102  if (!s->hwframe)
103  return AVERROR(ENOMEM);
104 
105  hwframe_ctx = (AVHWFramesContext*)s->hwframe->data;
106  hwframe_ctx->format = AV_PIX_FMT_CUDA;
107  if (inl->hw_frames_ctx) {
108  AVHWFramesContext *in_hwframe_ctx = (AVHWFramesContext*)inl->hw_frames_ctx->data;
109  hwframe_ctx->sw_format = in_hwframe_ctx->sw_format;
110  } else {
111  hwframe_ctx->sw_format = inlink->format;
112  }
113  hwframe_ctx->width = inlink->w;
114  hwframe_ctx->height = inlink->h;
115 
116  ret = av_hwframe_ctx_init(s->hwframe);
117  if (ret < 0)
118  return ret;
119 
120  outl->hw_frames_ctx = av_buffer_ref(s->hwframe);
121  if (!outl->hw_frames_ctx)
122  return AVERROR(ENOMEM);
123 
124  return 0;
125 }
126 
128 {
129  AVFilterContext *ctx = link->dst;
130  AVFilterLink *outlink = ctx->outputs[0];
131 
132  AVFrame *out = NULL;
133  int ret;
134 
135  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
136  if (!out) {
137  ret = AVERROR(ENOMEM);
138  goto fail;
139  }
140 
141  out->width = in->width;
142  out->height = in->height;
143 
144  ret = av_hwframe_transfer_data(out, in, 0);
145  if (ret < 0) {
146  av_log(ctx, AV_LOG_ERROR, "Error transferring data to the GPU\n");
147  goto fail;
148  }
149 
150  ret = av_frame_copy_props(out, in);
151  if (ret < 0)
152  goto fail;
153 
154  av_frame_free(&in);
155 
156  return ff_filter_frame(ctx->outputs[0], out);
157 fail:
158  av_frame_free(&in);
159  av_frame_free(&out);
160  return ret;
161 }
162 
163 #define OFFSET(x) offsetof(CudaUploadContext, x)
164 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
165 static const AVOption cudaupload_options[] = {
166  { "device", "Number of the device to use", OFFSET(device_idx), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
167  { NULL },
168 };
169 
170 AVFILTER_DEFINE_CLASS(cudaupload);
171 
172 static const AVFilterPad cudaupload_inputs[] = {
173  {
174  .name = "default",
175  .type = AVMEDIA_TYPE_VIDEO,
176  .filter_frame = cudaupload_filter_frame,
177  },
178 };
179 
180 static const AVFilterPad cudaupload_outputs[] = {
181  {
182  .name = "default",
183  .type = AVMEDIA_TYPE_VIDEO,
184  .config_props = cudaupload_config_output,
185  },
186 };
187 
189  .name = "hwupload_cuda",
190  .description = NULL_IF_CONFIG_SMALL("Upload a system memory frame to a CUDA device."),
191 
192  .init = cudaupload_init,
193  .uninit = cudaupload_uninit,
194 
195  .priv_size = sizeof(CudaUploadContext),
196  .priv_class = &cudaupload_class,
197 
200 
202 
203  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
204 };
cudaupload_outputs
static const AVFilterPad cudaupload_outputs[]
Definition: vf_hwupload_cuda.c:180
formats
formats
Definition: signature.h:47
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:116
ff_vf_hwupload_cuda
const AVFilter ff_vf_hwupload_cuda
Definition: vf_hwupload_cuda.c:188
AV_PIX_FMT_CUDA
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition: pixfmt.h:260
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:435
out
FILE * out
Definition: movenc.c:55
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1061
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
AVHWFramesContext::format
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:197
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
cudaupload_init
static av_cold int cudaupload_init(AVFilterContext *ctx)
Definition: vf_hwupload_cuda.c:37
av_hwframe_ctx_init
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:322
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVFrame::width
int width
Definition: frame.h:461
av_hwframe_ctx_alloc
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition: hwcontext.c:248
AVOption
AVOption.
Definition: opt.h:429
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
AVHWFramesContext::width
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:217
video.h
AV_PIX_FMT_VULKAN
@ AV_PIX_FMT_VULKAN
Vulkan hardware images.
Definition: pixfmt.h:379
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
cudaupload_options
static const AVOption cudaupload_options[]
Definition: vf_hwupload_cuda.c:165
formats.h
AV_HWDEVICE_TYPE_CUDA
@ AV_HWDEVICE_TYPE_CUDA
Definition: hwcontext.h:30
cudaupload_filter_frame
static int cudaupload_filter_frame(AVFilterLink *link, AVFrame *in)
Definition: vf_hwupload_cuda.c:127
fail
#define fail()
Definition: checkasm.h:193
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
AVHWFramesContext::height
int height
Definition: hwcontext.h:217
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_PIX_FMT_YUVA420P
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:108
AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:515
AV_PIX_FMT_0BGR32
#define AV_PIX_FMT_0BGR32
Definition: pixfmt.h:480
ff_formats_ref
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:678
OFFSET
#define OFFSET(x)
Definition: vf_hwupload_cuda.c:163
filters.h
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(cudaupload)
ctx
AVFormatContext * ctx
Definition: movenc.c:49
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
link
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a link
Definition: filter_design.txt:23
if
if(ret)
Definition: filter_design.txt:179
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
AVHWFramesContext::sw_format
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:210
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:725
av_buffer_unref
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:139
cudaupload_query_formats
static int cudaupload_query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition: vf_hwupload_cuda.c:55
AVFilterFormatsConfig
Lists of formats / etc.
Definition: avfilter.h:111
ff_filter_link
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition: filters.h:197
FF_FILTER_FLAG_HWFRAME_AWARE
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition: filters.h:206
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
cudaupload_config_output
static int cudaupload_config_output(AVFilterLink *outlink)
Definition: vf_hwupload_cuda.c:89
buffer.h
log.h
cudaupload_inputs
static const AVFilterPad cudaupload_inputs[]
Definition: vf_hwupload_cuda.c:172
FILTER_QUERY_FUNC2
#define FILTER_QUERY_FUNC2(func)
Definition: filters.h:239
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
AV_PIX_FMT_P016
#define AV_PIX_FMT_P016
Definition: pixfmt.h:554
AVFilter
Filter definition.
Definition: avfilter.h:201
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:115
ret
ret
Definition: filter_design.txt:187
AV_PIX_FMT_NV12
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:96
AV_PIX_FMT_0RGB32
#define AV_PIX_FMT_0RGB32
Definition: pixfmt.h:479
CudaUploadContext
Definition: vf_hwupload_cuda.c:29
av_hwdevice_ctx_create
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:600
av_hwframe_transfer_data
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
Definition: hwcontext.c:433
AVFrame::height
int height
Definition: frame.h:461
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
avfilter.h
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
CudaUploadContext::device_idx
int device_idx
Definition: vf_hwupload_cuda.c:31
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
AV_PIX_FMT_P010
#define AV_PIX_FMT_P010
Definition: pixfmt.h:552
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
CudaUploadContext::hwdevice
AVBufferRef * hwdevice
Definition: vf_hwupload_cuda.c:33
hwcontext.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
snprintf
#define snprintf
Definition: snprintf.h:34
CudaUploadContext::hwframe
AVBufferRef * hwframe
Definition: vf_hwupload_cuda.c:34
FLAGS
#define FLAGS
Definition: vf_hwupload_cuda.c:164
cudaupload_uninit
static av_cold void cudaupload_uninit(AVFilterContext *ctx)
Definition: vf_hwupload_cuda.c:47