FFmpeg  4.3
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 "formats.h"
28 #include "internal.h"
29 #include "video.h"
30 
31 typedef struct HWUploadContext {
32  const AVClass *class;
33 
35 
38 
39  char *device_type;
41 
43 {
44  HWUploadContext *ctx = avctx->priv;
45  AVHWFramesConstraints *constraints = NULL;
46  const enum AVPixelFormat *input_pix_fmts, *output_pix_fmts;
47  AVFilterFormats *input_formats = NULL;
48  int err, i;
49 
50  if (ctx->hwdevice_ref) {
51  /* We already have a specified device. */
52  } else if (avctx->hw_device_ctx) {
53  if (ctx->device_type) {
55  &ctx->hwdevice_ref,
57  avctx->hw_device_ctx, 0);
58  if (err < 0)
59  return err;
60  } else {
62  if (!ctx->hwdevice_ref)
63  return AVERROR(ENOMEM);
64  }
65  } else {
66  av_log(ctx, AV_LOG_ERROR, "A hardware device reference is required "
67  "to upload frames to.\n");
68  return AVERROR(EINVAL);
69  }
70 
72  if (!constraints) {
73  err = AVERROR(EINVAL);
74  goto fail;
75  }
76 
77  input_pix_fmts = constraints->valid_sw_formats;
78  output_pix_fmts = constraints->valid_hw_formats;
79 
80  input_formats = ff_make_format_list(output_pix_fmts);
81  if (!input_formats) {
82  err = AVERROR(ENOMEM);
83  goto fail;
84  }
85  if (input_pix_fmts) {
86  for (i = 0; input_pix_fmts[i] != AV_PIX_FMT_NONE; i++) {
87  err = ff_add_format(&input_formats, input_pix_fmts[i]);
88  if (err < 0)
89  goto fail;
90  }
91  }
92 
93  if ((err = ff_formats_ref(input_formats, &avctx->inputs[0]->out_formats)) < 0 ||
94  (err = ff_formats_ref(ff_make_format_list(output_pix_fmts),
95  &avctx->outputs[0]->in_formats)) < 0)
96  goto fail;
97 
98  av_hwframe_constraints_free(&constraints);
99  return 0;
100 
101 fail:
103  av_hwframe_constraints_free(&constraints);
104  return err;
105 }
106 
108 {
109  AVFilterContext *avctx = outlink->src;
110  AVFilterLink *inlink = avctx->inputs[0];
111  HWUploadContext *ctx = avctx->priv;
112  int err;
113 
115 
116  if (inlink->format == outlink->format) {
117  // The input is already a hardware format, so we just want to
118  // pass through the input frames in their own hardware context.
119  if (!inlink->hw_frames_ctx) {
120  av_log(ctx, AV_LOG_ERROR, "No input hwframe context.\n");
121  return AVERROR(EINVAL);
122  }
123 
124  outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
125  if (!outlink->hw_frames_ctx)
126  return AVERROR(ENOMEM);
127 
128  return 0;
129  }
130 
132  if (!ctx->hwframes_ref)
133  return AVERROR(ENOMEM);
134 
136 
137  av_log(ctx, AV_LOG_DEBUG, "Surface format is %s.\n",
138  av_get_pix_fmt_name(inlink->format));
139 
140  ctx->hwframes->format = outlink->format;
141  if (inlink->hw_frames_ctx) {
142  AVHWFramesContext *in_hwframe_ctx =
144  ctx->hwframes->sw_format = in_hwframe_ctx->sw_format;
145  } else {
146  ctx->hwframes->sw_format = inlink->format;
147  }
148  ctx->hwframes->width = inlink->w;
149  ctx->hwframes->height = inlink->h;
150 
151  if (avctx->extra_hw_frames >= 0)
152  ctx->hwframes->initial_pool_size = 2 + avctx->extra_hw_frames;
153 
154  err = av_hwframe_ctx_init(ctx->hwframes_ref);
155  if (err < 0)
156  goto fail;
157 
158  outlink->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
159  if (!outlink->hw_frames_ctx) {
160  err = AVERROR(ENOMEM);
161  goto fail;
162  }
163 
164  return 0;
165 
166 fail:
168  return err;
169 }
170 
171 static int hwupload_filter_frame(AVFilterLink *link, AVFrame *input)
172 {
173  AVFilterContext *avctx = link->dst;
174  AVFilterLink *outlink = avctx->outputs[0];
175  HWUploadContext *ctx = avctx->priv;
176  AVFrame *output = NULL;
177  int err;
178 
179  if (input->format == outlink->format)
180  return ff_filter_frame(outlink, input);
181 
182  output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
183  if (!output) {
184  av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame to upload to.\n");
185  err = AVERROR(ENOMEM);
186  goto fail;
187  }
188 
189  output->width = input->width;
190  output->height = input->height;
191 
192  err = av_hwframe_transfer_data(output, input, 0);
193  if (err < 0) {
194  av_log(ctx, AV_LOG_ERROR, "Failed to upload frame: %d.\n", err);
195  goto fail;
196  }
197 
198  err = av_frame_copy_props(output, input);
199  if (err < 0)
200  goto fail;
201 
202  av_frame_free(&input);
203 
204  return ff_filter_frame(outlink, output);
205 
206 fail:
207  av_frame_free(&input);
208  av_frame_free(&output);
209  return err;
210 }
211 
213 {
214  HWUploadContext *ctx = avctx->priv;
215 
218 }
219 
220 #define OFFSET(x) offsetof(HWUploadContext, x)
221 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
222 static const AVOption hwupload_options[] = {
223  {
224  "derive_device", "Derive a new device of this type",
226  { .str = NULL }, 0, 0, FLAGS
227  },
228  {
229  NULL
230  }
231 };
232 
233 AVFILTER_DEFINE_CLASS(hwupload);
234 
235 static const AVFilterPad hwupload_inputs[] = {
236  {
237  .name = "default",
238  .type = AVMEDIA_TYPE_VIDEO,
239  .filter_frame = hwupload_filter_frame,
240  },
241  { NULL }
242 };
243 
244 static const AVFilterPad hwupload_outputs[] = {
245  {
246  .name = "default",
247  .type = AVMEDIA_TYPE_VIDEO,
248  .config_props = hwupload_config_output,
249  },
250  { NULL }
251 };
252 
254  .name = "hwupload",
255  .description = NULL_IF_CONFIG_SMALL("Upload a normal frame to a hardware frame"),
256  .uninit = hwupload_uninit,
257  .query_formats = hwupload_query_formats,
258  .priv_size = sizeof(HWUploadContext),
259  .priv_class = &hwupload_class,
260  .inputs = hwupload_inputs,
261  .outputs = hwupload_outputs,
262  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
263 };
#define NULL
Definition: coverity.c:32
static int hwupload_query_formats(AVFilterContext *avctx)
Definition: vf_hwupload.c:42
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition: internal.h:365
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:125
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
AVOption.
Definition: opt.h:246
Main libavfilter public API header.
enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name)
Look up an AVHWDeviceType by name.
Definition: hwcontext.c:82
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:229
AVBufferRef * hw_device_ctx
For filters which will create hardware frames, sets the device the filter should create them in...
Definition: avfilter.h:394
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:209
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:99
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
Free an AVHWFrameConstraints structure.
Definition: hwcontext.c:601
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1075
#define av_cold
Definition: attributes.h:88
AVOptions.
#define FLAGS
Definition: vf_hwupload.c:221
AVHWFramesContext * hwframes
Definition: vf_hwupload.c:37
#define av_log(a,...)
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:424
AVFILTER_DEFINE_CLASS(hwupload)
AVBufferRef * hwframes_ref
Definition: vf_hwupload.c:36
A filter pad used for either input or output.
Definition: internal.h:54
static int hwupload_config_output(AVFilterLink *outlink)
Definition: vf_hwupload.c:107
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
int width
Definition: frame.h:358
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
AVBufferRef * hwdevice_ref
Definition: vf_hwupload.c:34
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:336
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:333
#define fail()
Definition: checkasm.h:123
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
Definition: hwcontext.c:443
int initial_pool_size
Initial size of the frame pool.
Definition: hwcontext.h:199
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:470
AVFormatContext * ctx
Definition: movenc.c:48
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:714
static int hwupload_filter_frame(AVFilterLink *link, AVFrame *input)
Definition: vf_hwupload.c:171
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
AVFilter ff_vf_hwupload
Definition: vf_hwupload.c:253
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
char * device_type
Definition: vf_hwupload.c:39
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:373
This struct describes the constraints on hardware frames attached to a given device with a hardware-s...
Definition: hwcontext.h:453
#define OFFSET(x)
Definition: vf_hwupload.c:220
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:576
uint8_t * data
The data buffer.
Definition: buffer.h:89
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
static const AVOption hwupload_options[]
Definition: vf_hwupload.c:222
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:124
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.
Definition: hwcontext.h:458
const char * name
Filter name.
Definition: avfilter.h:148
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
A reference to a data buffer.
Definition: buffer.h:81
static av_cold void hwupload_uninit(AVFilterContext *avctx)
Definition: vf_hwupload.c:212
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition: hwcontext.c:247
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
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:465
static const AVFilterPad hwupload_outputs[]
Definition: vf_hwupload.c:244
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:338
int height
Definition: frame.h:358
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:2465
static const AVFilterPad hwupload_inputs[]
Definition: vf_hwupload.c:235
internal API functions
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:222
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:659