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
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  */
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"
26 #include "avfilter.h"
27 #include "formats.h"
28 #include "internal.h"
29 #include "video.h"
31 typedef struct HWUploadContext {
32  const AVClass *class;
42 {
43  HWUploadContext *ctx = avctx->priv;
44  AVHWFramesConstraints *constraints = NULL;
45  const enum AVPixelFormat *input_pix_fmts, *output_pix_fmts;
46  AVFilterFormats *input_formats = NULL;
47  int err, i;
49  if (!avctx->hw_device_ctx) {
50  av_log(ctx, AV_LOG_ERROR, "A hardware device reference is required "
51  "to upload frames to.\n");
52  return AVERROR(EINVAL);
53  }
56  if (!ctx->hwdevice_ref)
57  return AVERROR(ENOMEM);
61  if (!constraints) {
62  err = AVERROR(EINVAL);
63  goto fail;
64  }
66  input_pix_fmts = constraints->valid_sw_formats;
67  output_pix_fmts = constraints->valid_hw_formats;
69  input_formats = ff_make_format_list(output_pix_fmts);
70  if (!input_formats) {
71  err = AVERROR(ENOMEM);
72  goto fail;
73  }
74  if (input_pix_fmts) {
75  for (i = 0; input_pix_fmts[i] != AV_PIX_FMT_NONE; i++) {
76  err = ff_add_format(&input_formats, input_pix_fmts[i]);
77  if (err < 0)
78  goto fail;
79  }
80  }
82  if ((err = ff_formats_ref(input_formats, &avctx->inputs[0]->out_formats)) < 0 ||
83  (err = ff_formats_ref(ff_make_format_list(output_pix_fmts),
84  &avctx->outputs[0]->in_formats)) < 0)
85  goto fail;
87  av_hwframe_constraints_free(&constraints);
88  return 0;
90 fail:
92  av_hwframe_constraints_free(&constraints);
93  return err;
94 }
97 {
98  AVFilterContext *avctx = outlink->src;
99  AVFilterLink *inlink = avctx->inputs[0];
100  HWUploadContext *ctx = avctx->priv;
101  int err;
105  if (inlink->format == outlink->format) {
106  // The input is already a hardware format, so we just want to
107  // pass through the input frames in their own hardware context.
108  if (!inlink->hw_frames_ctx) {
109  av_log(ctx, AV_LOG_ERROR, "No input hwframe context.\n");
110  return AVERROR(EINVAL);
111  }
113  outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
114  if (!outlink->hw_frames_ctx)
115  return AVERROR(ENOMEM);
117  return 0;
118  }
121  if (!ctx->hwframes_ref)
122  return AVERROR(ENOMEM);
126  av_log(ctx, AV_LOG_DEBUG, "Surface format is %s.\n",
127  av_get_pix_fmt_name(inlink->format));
129  ctx->hwframes->format = outlink->format;
130  ctx->hwframes->sw_format = inlink->format;
131  ctx->hwframes->width = inlink->w;
132  ctx->hwframes->height = inlink->h;
134  if (avctx->extra_hw_frames >= 0)
135  ctx->hwframes->initial_pool_size = 2 + avctx->extra_hw_frames;
137  err = av_hwframe_ctx_init(ctx->hwframes_ref);
138  if (err < 0)
139  goto fail;
141  outlink->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
142  if (!outlink->hw_frames_ctx) {
143  err = AVERROR(ENOMEM);
144  goto fail;
145  }
147  return 0;
149 fail:
151  return err;
152 }
155 {
156  AVFilterContext *avctx = link->dst;
157  AVFilterLink *outlink = avctx->outputs[0];
158  HWUploadContext *ctx = avctx->priv;
159  AVFrame *output = NULL;
160  int err;
162  if (input->format == outlink->format)
163  return ff_filter_frame(outlink, input);
165  output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
166  if (!output) {
167  av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame to upload to.\n");
168  err = AVERROR(ENOMEM);
169  goto fail;
170  }
172  output->width = input->width;
173  output->height = input->height;
175  err = av_hwframe_transfer_data(output, input, 0);
176  if (err < 0) {
177  av_log(ctx, AV_LOG_ERROR, "Failed to upload frame: %d.\n", err);
178  goto fail;
179  }
181  err = av_frame_copy_props(output, input);
182  if (err < 0)
183  goto fail;
185  av_frame_free(&input);
187  return ff_filter_frame(outlink, output);
189 fail:
190  av_frame_free(&input);
191  av_frame_free(&output);
192  return err;
193 }
196 {
197  HWUploadContext *ctx = avctx->priv;
201 }
203 static const AVClass hwupload_class = {
204  .class_name = "hwupload",
205  .item_name = av_default_item_name,
206  .option = NULL,
207  .version = LIBAVUTIL_VERSION_INT,
208 };
210 static const AVFilterPad hwupload_inputs[] = {
211  {
212  .name = "default",
213  .type = AVMEDIA_TYPE_VIDEO,
214  .filter_frame = hwupload_filter_frame,
215  },
216  { NULL }
217 };
219 static const AVFilterPad hwupload_outputs[] = {
220  {
221  .name = "default",
222  .type = AVMEDIA_TYPE_VIDEO,
223  .config_props = hwupload_config_output,
224  },
225  { NULL }
226 };
229  .name = "hwupload",
230  .description = NULL_IF_CONFIG_SMALL("Upload a normal frame to a hardware frame"),
231  .uninit = hwupload_uninit,
232  .query_formats = hwupload_query_formats,
233  .priv_size = sizeof(HWUploadContext),
234  .priv_class = &hwupload_class,
235  .inputs = hwupload_inputs,
236  .outputs = hwupload_outputs,
237  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
238 };
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:60
#define NULL
Definition: coverity.c:32
static int hwupload_query_formats(AVFilterContext *avctx)
Definition: vf_hwupload.c:41
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:126
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
Definition: version.h:85
Main libavfilter public API header.
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:228
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:208
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:562
const char * name
Pad name.
Definition: internal.h:60
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
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:82
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
AVHWFramesContext * hwframes
Definition: vf_hwupload.c:38
#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
AVBufferRef * hwframes_ref
Definition: vf_hwupload.c:37
A filter pad used for either input or output.
Definition: internal.h:54
static int hwupload_config_output(AVFilterLink *outlink)
Definition: vf_hwupload.c:96
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
int width
Definition: frame.h:353
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
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:329
#define fail()
Definition: checkasm.h:122
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
Definition: hwcontext.c:439
int initial_pool_size
Initial size of the frame pool.
Definition: hwcontext.h:198
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:439
AVFormatContext * ctx
Definition: movenc.c:48
static int hwupload_filter_frame(AVFilterLink *link, AVFrame *input)
Definition: vf_hwupload.c:154
AVFilter ff_vf_hwupload
Definition: vf_hwupload.c:228
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:368
This struct describes the constraints on hardware frames attached to a given device with a hardware-s...
Definition: hwcontext.h:432
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:537
uint8_t * data
The data buffer.
Definition: buffer.h:89
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:123
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:437
const char * name
Filter name.
Definition: avfilter.h:148
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
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
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
AVHWDeviceContext * hwdevice
Definition: vf_hwupload.c:35
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
A reference to a data buffer.
Definition: buffer.h:81
static av_cold void hwupload_uninit(AVFilterContext *avctx)
Definition: vf_hwupload.c:195
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition: hwcontext.c:243
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:94
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:444
static const AVFilterPad hwupload_outputs[]
Definition: vf_hwupload.c:219
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:353
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:2438
static const AVFilterPad hwupload_inputs[]
Definition: vf_hwupload.c:210
internal API functions
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
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:221
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:654
static const AVClass hwupload_class
Definition: vf_hwupload.c:203