FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
36 
40 
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;
48 
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  }
54 
56  if (!ctx->hwdevice_ref)
57  return AVERROR(ENOMEM);
59 
61  if (!constraints) {
62  err = AVERROR(EINVAL);
63  goto fail;
64  }
65 
66  input_pix_fmts = constraints->valid_sw_formats;
67  output_pix_fmts = constraints->valid_hw_formats;
68 
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  ff_formats_unref(&input_formats);
79  goto fail;
80  }
81  }
82  }
83 
84  ff_formats_ref(input_formats, &avctx->inputs[0]->out_formats);
85 
86  ff_formats_ref(ff_make_format_list(output_pix_fmts),
87  &avctx->outputs[0]->in_formats);
88 
89  av_hwframe_constraints_free(&constraints);
90  return 0;
91 
92 fail:
94  av_hwframe_constraints_free(&constraints);
95  return err;
96 }
97 
99 {
100  AVFilterContext *avctx = outlink->src;
101  AVFilterLink *inlink = avctx->inputs[0];
102  HWUploadContext *ctx = avctx->priv;
103  int err;
104 
106 
107  if (inlink->format == outlink->format) {
108  // The input is already a hardware format, so we just want to
109  // pass through the input frames in their own hardware context.
110  if (!inlink->hw_frames_ctx) {
111  av_log(ctx, AV_LOG_ERROR, "No input hwframe context.\n");
112  return AVERROR(EINVAL);
113  }
114 
115  outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
116  if (!outlink->hw_frames_ctx)
117  return AVERROR(ENOMEM);
118 
119  return 0;
120  }
121 
123  if (!ctx->hwframes_ref)
124  return AVERROR(ENOMEM);
125 
127 
128  av_log(ctx, AV_LOG_DEBUG, "Surface format is %s.\n",
129  av_get_pix_fmt_name(inlink->format));
130 
131  ctx->hwframes->format = outlink->format;
132  ctx->hwframes->sw_format = inlink->format;
133  ctx->hwframes->width = inlink->w;
134  ctx->hwframes->height = inlink->h;
135 
136  err = av_hwframe_ctx_init(ctx->hwframes_ref);
137  if (err < 0)
138  goto fail;
139 
140  outlink->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
141  if (!outlink->hw_frames_ctx) {
142  err = AVERROR(ENOMEM);
143  goto fail;
144  }
145 
146  return 0;
147 
148 fail:
150  return err;
151 }
152 
153 static int hwupload_filter_frame(AVFilterLink *link, AVFrame *input)
154 {
155  AVFilterContext *avctx = link->dst;
156  AVFilterLink *outlink = avctx->outputs[0];
157  HWUploadContext *ctx = avctx->priv;
158  AVFrame *output = NULL;
159  int err;
160 
161  if (input->format == outlink->format)
162  return ff_filter_frame(outlink, input);
163 
164  output = av_frame_alloc();
165  if (!output) {
166  err = AVERROR(ENOMEM);
167  goto fail;
168  }
169 
170  err = av_hwframe_get_buffer(ctx->hwframes_ref, output, 0);
171  if (err < 0) {
172  av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame to upload to.\n");
173  goto fail;
174  }
175 
176  output->width = input->width;
177  output->height = input->height;
178 
179  err = av_hwframe_transfer_data(output, input, 0);
180  if (err < 0) {
181  av_log(ctx, AV_LOG_ERROR, "Failed to upload frame: %d.\n", err);
182  goto fail;
183  }
184 
185  err = av_frame_copy_props(output, input);
186  if (err < 0)
187  goto fail;
188 
189  av_frame_free(&input);
190 
191  return ff_filter_frame(outlink, output);
192 
193 fail:
194  av_frame_free(&input);
195  av_frame_free(&output);
196  return err;
197 }
198 
200 {
201  HWUploadContext *ctx = avctx->priv;
202 
205 }
206 
207 static const AVClass hwupload_class = {
208  .class_name = "hwupload",
209  .item_name = av_default_item_name,
210  .option = NULL,
211  .version = LIBAVUTIL_VERSION_INT,
212 };
213 
214 static const AVFilterPad hwupload_inputs[] = {
215  {
216  .name = "default",
217  .type = AVMEDIA_TYPE_VIDEO,
218  .filter_frame = hwupload_filter_frame,
219  },
220  { NULL }
221 };
222 
223 static const AVFilterPad hwupload_outputs[] = {
224  {
225  .name = "default",
226  .type = AVMEDIA_TYPE_VIDEO,
227  .config_props = hwupload_config_output,
228  },
229  { NULL }
230 };
231 
233  .name = "hwupload",
234  .description = NULL_IF_CONFIG_SMALL("Upload a normal frame to a hardware frame"),
235  .uninit = hwupload_uninit,
236  .query_formats = hwupload_query_formats,
237  .priv_size = sizeof(HWUploadContext),
238  .priv_class = &hwupload_class,
239  .inputs = hwupload_inputs,
240  .outputs = hwupload_outputs,
241 };
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:53
#define NULL
Definition: coverity.c:32
static int hwupload_query_formats(AVFilterContext *avctx)
Definition: vf_hwupload.c:41
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:124
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
Main libavfilter public API header.
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:221
AVBufferRef * hw_device_ctx
For filters which will create hardware frames, sets the device the filter should create them in...
Definition: avfilter.h:354
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:201
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:447
const char * name
Pad name.
Definition: internal.h:59
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:313
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1180
#define av_cold
Definition: attributes.h:82
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:140
AVOptions.
AVHWFramesContext * hwframes
Definition: vf_hwupload.c:38
#define av_log(a,...)
AVBufferRef * hwframes_ref
Definition: vf_hwupload.c:37
A filter pad used for either input or output.
Definition: internal.h:53
static int hwupload_config_output(AVFilterLink *outlink)
Definition: vf_hwupload.c:98
int width
width and height of the video frame
Definition: frame.h:236
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_default_item_name
#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:153
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
void * priv
private data for use by the filter
Definition: avfilter.h:320
#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:337
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:260
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
Definition: hwcontext.c:387
#define fail()
Definition: checkasm.h:81
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
Definition: hwcontext.c:361
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:440
AVFormatContext * ctx
Definition: movenc.c:48
static int hwupload_filter_frame(AVFilterLink *link, AVFrame *input)
Definition: vf_hwupload.c:153
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:386
AVFilter ff_vf_hwupload
Definition: vf_hwupload.c:232
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:248
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:376
This struct describes the constraints on hardware frames attached to a given device with a hardware-s...
Definition: hwcontext.h:365
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:422
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:142
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:116
refcounted data buffer API
void ff_formats_unref(AVFilterFormats **ref)
If *ref is non-NULL, remove *ref as a reference to the format list it currently points to...
Definition: formats.c:476
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:370
const char * name
Filter name.
Definition: avfilter.h:146
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:317
AVHWDeviceContext * hwdevice
Definition: vf_hwupload.c:35
A reference to a data buffer.
Definition: buffer.h:81
static av_cold void hwupload_uninit(AVFilterContext *avctx)
Definition: vf_hwupload.c:199
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition: hwcontext.c:174
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
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:377
static const AVFilterPad hwupload_outputs[]
Definition: vf_hwupload.c:223
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:305
int height
Definition: frame.h:236
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:2138
static const AVFilterPad hwupload_inputs[]
Definition: vf_hwupload.c:214
internal API functions
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:214
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:580
static const AVClass hwupload_class
Definition: vf_hwupload.c:207