FFmpeg
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 
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,
56  av_hwdevice_find_type_by_name(ctx->device_type),
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  } 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 
71  constraints = av_hwdevice_get_hwframe_constraints(ctx->hwdevice_ref, NULL);
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]->outcfg.formats)) < 0 ||
94  (err = ff_formats_ref(ff_make_format_list(output_pix_fmts),
95  &avctx->outputs[0]->incfg.formats)) < 0)
96  goto fail;
97 
98  av_hwframe_constraints_free(&constraints);
99  return 0;
100 
101 fail:
102  av_buffer_unref(&ctx->hwdevice_ref);
103  av_hwframe_constraints_free(&constraints);
104  return err;
105 }
106 
108 {
109  FilterLink *outl = ff_filter_link(outlink);
110  AVFilterContext *avctx = outlink->src;
111  AVFilterLink *inlink = avctx->inputs[0];
113  HWUploadContext *ctx = avctx->priv;
114  int err;
115 
116  av_buffer_unref(&ctx->hwframes_ref);
117 
118  if (inlink->format == outlink->format) {
119  // The input is already a hardware format, so we just want to
120  // pass through the input frames in their own hardware context.
121  if (!inl->hw_frames_ctx) {
122  av_log(ctx, AV_LOG_ERROR, "No input hwframe context.\n");
123  return AVERROR(EINVAL);
124  }
125 
127  if (!outl->hw_frames_ctx)
128  return AVERROR(ENOMEM);
129 
130  return 0;
131  }
132 
133  ctx->hwframes_ref = av_hwframe_ctx_alloc(ctx->hwdevice_ref);
134  if (!ctx->hwframes_ref)
135  return AVERROR(ENOMEM);
136 
137  ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
138 
139  av_log(ctx, AV_LOG_DEBUG, "Surface format is %s.\n",
140  av_get_pix_fmt_name(inlink->format));
141 
142  ctx->hwframes->format = outlink->format;
143  if (inl->hw_frames_ctx) {
144  AVHWFramesContext *in_hwframe_ctx =
146  ctx->hwframes->sw_format = in_hwframe_ctx->sw_format;
147  } else {
148  ctx->hwframes->sw_format = inlink->format;
149  }
150  ctx->hwframes->width = inlink->w;
151  ctx->hwframes->height = inlink->h;
152 
153  if (avctx->extra_hw_frames >= 0)
154  ctx->hwframes->initial_pool_size = 2 + avctx->extra_hw_frames;
155 
156  err = av_hwframe_ctx_init(ctx->hwframes_ref);
157  if (err < 0)
158  goto fail;
159 
160  outl->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
161  if (!outl->hw_frames_ctx) {
162  err = AVERROR(ENOMEM);
163  goto fail;
164  }
165 
166  return 0;
167 
168 fail:
169  av_buffer_unref(&ctx->hwframes_ref);
170  return err;
171 }
172 
174 {
175  AVFilterContext *avctx = link->dst;
176  AVFilterLink *outlink = avctx->outputs[0];
177  HWUploadContext *ctx = avctx->priv;
178  AVFrame *output = NULL;
179  int err;
180 
181  if (input->format == outlink->format)
182  return ff_filter_frame(outlink, input);
183 
184  output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
185  if (!output) {
186  av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame to upload to.\n");
187  err = AVERROR(ENOMEM);
188  goto fail;
189  }
190 
191  output->width = input->width;
192  output->height = input->height;
193 
195  if (err < 0) {
196  av_log(ctx, AV_LOG_ERROR, "Failed to upload frame: %d.\n", err);
197  goto fail;
198  }
199 
201  if (err < 0)
202  goto fail;
203 
205 
206  return ff_filter_frame(outlink, output);
207 
208 fail:
211  return err;
212 }
213 
215 {
216  HWUploadContext *ctx = avctx->priv;
217 
218  av_buffer_unref(&ctx->hwframes_ref);
219  av_buffer_unref(&ctx->hwdevice_ref);
220 }
221 
222 #define OFFSET(x) offsetof(HWUploadContext, x)
223 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
224 static const AVOption hwupload_options[] = {
225  {
226  "derive_device", "Derive a new device of this type",
227  OFFSET(device_type), AV_OPT_TYPE_STRING,
228  { .str = NULL }, 0, 0, FLAGS
229  },
230  {
231  NULL
232  }
233 };
234 
235 AVFILTER_DEFINE_CLASS(hwupload);
236 
237 static const AVFilterPad hwupload_inputs[] = {
238  {
239  .name = "default",
240  .type = AVMEDIA_TYPE_VIDEO,
241  .filter_frame = hwupload_filter_frame,
242  },
243 };
244 
245 static const AVFilterPad hwupload_outputs[] = {
246  {
247  .name = "default",
248  .type = AVMEDIA_TYPE_VIDEO,
249  .config_props = hwupload_config_output,
250  },
251 };
252 
254  .name = "hwupload",
255  .description = NULL_IF_CONFIG_SMALL("Upload a normal frame to a hardware frame"),
256  .uninit = hwupload_uninit,
257  .priv_size = sizeof(HWUploadContext),
258  .priv_class = &hwupload_class,
262  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
263  .flags = AVFILTER_FLAG_HWDEVICE,
264 };
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
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
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1062
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(hwupload)
HWUploadContext
Definition: vf_hwupload.c:31
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
hwupload_config_output
static int hwupload_config_output(AVFilterLink *outlink)
Definition: vf_hwupload.c:107
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
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
test::height
int height
Definition: vc1dsp.c:40
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
pixdesc.h
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_hwdevice_find_type_by_name
enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name)
Look up an AVHWDeviceType by name.
Definition: hwcontext.c:102
AVFilterContext::hw_device_ctx
AVBufferRef * hw_device_ctx
For filters which will create hardware frames, sets the device the filter should create them in.
Definition: avfilter.h:539
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
hwupload_uninit
static av_cold void hwupload_uninit(AVFilterContext *avctx)
Definition: vf_hwupload.c:214
AVHWFramesConstraints::valid_hw_formats
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:446
av_hwdevice_get_hwframe_constraints
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:566
hwupload_filter_frame
static int hwupload_filter_frame(AVFilterLink *link, AVFrame *input)
Definition: vf_hwupload.c:173
video.h
OFFSET
#define OFFSET(x)
Definition: vf_hwupload.c:222
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
AVHWFramesConstraints
This struct describes the constraints on hardware frames attached to a given device with a hardware-s...
Definition: hwcontext.h:441
formats.h
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:472
hwupload_options
static const AVOption hwupload_options[]
Definition: vf_hwupload.c:224
HWUploadContext::hwframes
AVHWFramesContext * hwframes
Definition: vf_hwupload.c:37
fail
#define fail()
Definition: checkasm.h:188
AVFilterContext::extra_hw_frames
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:563
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
HWUploadContext::hwframes_ref
AVBufferRef * hwframes_ref
Definition: vf_hwupload.c:36
av_cold
#define av_cold
Definition: attributes.h:90
HWUploadContext::device_type
char * device_type
Definition: vf_hwupload.c:39
AVHWFramesConstraints::valid_sw_formats
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:453
av_hwframe_constraints_free
void av_hwframe_constraints_free(AVHWFramesConstraints **constraints)
Free an AVHWFrameConstraints structure.
Definition: hwcontext.c:591
ff_formats_ref
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:678
filters.h
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
ctx
AVFormatContext * ctx
Definition: movenc.c:49
HWUploadContext::hwdevice_ref
AVBufferRef * hwdevice_ref
Definition: vf_hwupload.c:34
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
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:713
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
AVFilterContext::inputs
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:465
ff_add_format
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:504
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
test::width
int width
Definition: vc1dsp.c:39
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
AVFILTER_FLAG_HWDEVICE
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
Definition: avfilter.h:173
buffer.h
hwupload_outputs
static const AVFilterPad hwupload_outputs[]
Definition: vf_hwupload.c:245
input
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
Definition: filter_design.txt:172
hwupload_inputs
static const AVFilterPad hwupload_inputs[]
Definition: vf_hwupload.c:237
av_hwdevice_ctx_create_derived
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:703
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
AVFilter
Filter definition.
Definition: avfilter.h:201
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:115
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
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
ff_vf_hwupload
const AVFilter ff_vf_hwupload
Definition: vf_hwupload.c:253
hwupload_query_formats
static int hwupload_query_formats(AVFilterContext *avctx)
Definition: vf_hwupload.c:42
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
AVFilterFormatsConfig::formats
AVFilterFormats * formats
List of supported formats (pixel or sample).
Definition: avfilter.h:116
hwcontext_internal.h
hwcontext.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
FLAGS
#define FLAGS
Definition: vf_hwupload.c:223
AV_OPT_TYPE_STRING
@ 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:276
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: filters.h:236
av_get_pix_fmt_name
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:2945
AVFilterContext::outputs
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:469