FFmpeg
Loading...
Searching...
No Matches
vf_hwupload.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"
22#include "libavutil/log.h"
23#include "libavutil/pixdesc.h"
24#include "libavutil/opt.h"
25
26#include "avfilter.h"
27#include "filters.h"
28#include "formats.h"
29#include "video.h"
30
41
43{
44 HWUploadContext *ctx = avctx->priv;
45 int err;
46
47 if (!avctx->hw_device_ctx) {
48 av_log(ctx, AV_LOG_ERROR, "A hardware device reference is required "
49 "to upload frames to.\n");
50 return AVERROR(EINVAL);
51 }
52
53 if (ctx->device_type) {
55 &ctx->hwdevice_ref,
57 avctx->hw_device_ctx, 0);
58 if (err < 0)
59 return err;
60 } else {
61 ctx->hwdevice_ref = av_buffer_ref(avctx->hw_device_ctx);
62 if (!ctx->hwdevice_ref)
63 return AVERROR(ENOMEM);
64 }
65
66 return 0;
67}
68
70 AVFilterFormatsConfig **cfg_in,
71 AVFilterFormatsConfig **cfg_out)
72{
73 const HWUploadContext *ctx = avctx->priv;
74 AVHWFramesConstraints *constraints = NULL;
75 const enum AVPixelFormat *input_pix_fmts, *output_pix_fmts;
76 AVFilterFormats *input_formats = NULL;
77 int err, i;
78
79 constraints = av_hwdevice_get_hwframe_constraints(ctx->hwdevice_ref, NULL);
80 if (!constraints) {
81 err = AVERROR(EINVAL);
82 goto fail;
83 }
84
85 input_pix_fmts = constraints->valid_sw_formats;
86 output_pix_fmts = constraints->valid_hw_formats;
87
88 input_formats = ff_make_pixel_format_list(output_pix_fmts);
89 if (!input_formats) {
90 err = AVERROR(ENOMEM);
91 goto fail;
92 }
93 if (input_pix_fmts) {
94 for (i = 0; input_pix_fmts[i] != AV_PIX_FMT_NONE; i++) {
95 err = ff_add_format(&input_formats, input_pix_fmts[i]);
96 if (err < 0)
97 goto fail;
98 }
99 }
100
101 if ((err = ff_formats_ref(input_formats, &cfg_in[0]->formats)) < 0 ||
102 (err = ff_formats_ref(ff_make_pixel_format_list(output_pix_fmts),
103 &cfg_out[0]->formats)) < 0)
104 goto fail;
105
106 av_hwframe_constraints_free(&constraints);
107 return 0;
108
109fail:
110 av_hwframe_constraints_free(&constraints);
111 return err;
112}
113
115{
116 FilterLink *outl = ff_filter_link(outlink);
117 AVFilterContext *avctx = outlink->src;
118 AVFilterLink *inlink = avctx->inputs[0];
119 FilterLink *inl = ff_filter_link(inlink);
120 HWUploadContext *ctx = avctx->priv;
121 int err;
122
123 av_buffer_unref(&ctx->hwframes_ref);
124
125 if (inlink->format == outlink->format) {
126 // The input is already a hardware format, so we just want to
127 // pass through the input frames in their own hardware context.
128 if (!inl->hw_frames_ctx) {
129 av_log(ctx, AV_LOG_ERROR, "No input hwframe context.\n");
130 return AVERROR(EINVAL);
131 }
132
134 if (!outl->hw_frames_ctx)
135 return AVERROR(ENOMEM);
136
137 return 0;
138 }
139
140 ctx->hwframes_ref = av_hwframe_ctx_alloc(ctx->hwdevice_ref);
141 if (!ctx->hwframes_ref)
142 return AVERROR(ENOMEM);
143
144 ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
145
146 av_log(ctx, AV_LOG_DEBUG, "Surface format is %s.\n",
147 av_get_pix_fmt_name(inlink->format));
148
149 ctx->hwframes->format = outlink->format;
150 if (inl->hw_frames_ctx) {
151 AVHWFramesContext *in_hwframe_ctx =
153 ctx->hwframes->sw_format = in_hwframe_ctx->sw_format;
154 } else {
155 ctx->hwframes->sw_format = inlink->format;
156 }
157 ctx->hwframes->width = inlink->w;
158 ctx->hwframes->height = inlink->h;
159
160 if (avctx->extra_hw_frames >= 0)
161 ctx->hwframes->initial_pool_size = 2 + avctx->extra_hw_frames;
162
163 err = av_hwframe_ctx_init(ctx->hwframes_ref);
164 if (err < 0)
165 goto fail;
166
167 outl->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
168 if (!outl->hw_frames_ctx) {
169 err = AVERROR(ENOMEM);
170 goto fail;
171 }
172
173 return 0;
174
175fail:
176 av_buffer_unref(&ctx->hwframes_ref);
177 return err;
178}
179
181{
182 AVFilterContext *avctx = link->dst;
183 AVFilterLink *outlink = avctx->outputs[0];
184 HWUploadContext *ctx = avctx->priv;
185 AVFrame *output = NULL;
186 int err;
187
188 if (input->format == outlink->format)
189 return ff_filter_frame(outlink, input);
190
191 output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
192 if (!output) {
193 av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame to upload to.\n");
194 err = AVERROR(ENOMEM);
195 goto fail;
196 }
197
198 output->width = input->width;
199 output->height = input->height;
200
201 err = av_hwframe_transfer_data(output, input, 0);
202 if (err < 0) {
203 av_log(ctx, AV_LOG_ERROR, "Failed to upload frame: %d.\n", err);
204 goto fail;
205 }
206
207 err = av_frame_copy_props(output, input);
208 if (err < 0)
209 goto fail;
210
211 av_frame_free(&input);
212
213 return ff_filter_frame(outlink, output);
214
215fail:
216 av_frame_free(&input);
217 av_frame_free(&output);
218 return err;
219}
220
222{
223 HWUploadContext *ctx = avctx->priv;
224
225 av_buffer_unref(&ctx->hwframes_ref);
226 av_buffer_unref(&ctx->hwdevice_ref);
227}
228
229#define OFFSET(x) offsetof(HWUploadContext, x)
230#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
231static const AVOption hwupload_options[] = {
232 {
233 "derive_device", "Derive a new device of this type",
234 OFFSET(device_type), AV_OPT_TYPE_STRING,
235 { .str = NULL }, 0, 0, FLAGS
236 },
237 {
238 NULL
239 }
240};
241
243
244static const AVFilterPad hwupload_inputs[] = {
245 {
246 .name = "default",
247 .type = AVMEDIA_TYPE_VIDEO,
248 .filter_frame = hwupload_filter_frame,
249 },
250};
251
253 {
254 .name = "default",
255 .type = AVMEDIA_TYPE_VIDEO,
256 .config_props = hwupload_config_output,
257 },
258};
259
261 .p.name = "hwupload",
262 .p.description = NULL_IF_CONFIG_SMALL("Upload a normal frame to a hardware frame"),
263 .p.priv_class = &hwupload_class,
264 .p.flags = AVFILTER_FLAG_HWDEVICE,
265 .init = hwupload_init,
266 .uninit = hwupload_uninit,
267 .priv_size = sizeof(HWUploadContext),
271 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
272};
const FFFilter ff_vf_hwupload
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
refcounted data buffer API
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add ref as a new reference to formats.
Definition formats.c:756
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition formats.c:571
av_warn_unused_result AVFilterFormats * ff_make_pixel_format_list(const enum AVPixelFormat *fmts)
Create a list of supported pixel formats.
#define fail
Definition test.h:479
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
Definition avfilter.h:187
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
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition buffer.c:103
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
Free an AVHWFrameConstraints structure.
Definition hwcontext.c:606
int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ref_ptr, enum AVHWDeviceType type, AVBufferRef *src_ref, int flags)
Create a new device of the specified type from an existing device.
Definition hwcontext.c:718
enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name)
Look up an AVHWDeviceType by name.
Definition hwcontext.c:110
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition hwcontext.c:337
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition hwcontext.c:263
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
Definition hwcontext.c:448
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...
Definition hwcontext.c:581
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#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:208
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
AVOptions.
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:3380
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
formats
Definition signature.h:47
A reference to a data buffer.
Definition buffer.h:82
uint8_t * data
The data buffer.
Definition buffer.h:90
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
int extra_hw_frames
Sets the number of extra hardware frames which the filter will allocate on its output links for use i...
Definition avfilter.h:352
AVFilterLink ** inputs
array of pointers to input links
Definition avfilter.h:281
void * priv
private data for use by the filter
Definition avfilter.h:288
AVBufferRef * hw_device_ctx
For filters which will create hardware frames, sets the device the filter should create them in.
Definition avfilter.h:336
AVFilterLink ** outputs
array of pointers to output links
Definition avfilter.h:285
Lists of formats / etc.
Definition avfilter.h:120
A list of supported formats for one end of a filter link.
Definition formats.h:64
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int width
Definition frame.h:544
int height
Definition frame.h:544
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:559
This struct describes the constraints on hardware frames attached to a given device with a hardware-s...
Definition hwcontext.h:444
enum AVPixelFormat * valid_hw_formats
A list of possible values for format in the hw_frames_ctx, terminated by AV_PIX_FMT_NONE.
Definition hwcontext.h:449
enum AVPixelFormat * valid_sw_formats
A list of possible values for sw_format in the hw_frames_ctx, terminated by AV_PIX_FMT_NONE.
Definition hwcontext.h:456
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition hwcontext.h:213
AVOption.
Definition opt.h:428
AVBufferRef * hwframes_ref
Definition vf_hwupload.c:36
AVBufferRef * hwdevice_ref
Definition vf_hwupload.c:34
AVHWFramesContext * hwframes
Definition vf_hwupload.c:37
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
static int hwupload_query_formats(const AVFilterContext *avctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition vf_hwupload.c:69
static av_cold void hwupload_uninit(AVFilterContext *avctx)
static const AVOption hwupload_options[]
static const AVFilterPad hwupload_outputs[]
static int hwupload_filter_frame(AVFilterLink *link, AVFrame *input)
static const AVFilterPad hwupload_inputs[]
static int hwupload_init(AVFilterContext *avctx)
Definition vf_hwupload.c:42
#define OFFSET(x)
static int hwupload_config_output(AVFilterLink *outlink)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89