FFmpeg
vf_hwdownload.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"
21 #include "libavutil/log.h"
22 #include "libavutil/mem.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
25 
26 #include "avfilter.h"
27 #include "formats.h"
28 #include "internal.h"
29 #include "video.h"
30 
31 typedef struct HWDownloadContext {
32  const AVClass *class;
33 
37 
39 {
40  AVFilterFormats *infmts = NULL;
41  AVFilterFormats *outfmts = NULL;
42  const AVPixFmtDescriptor *desc;
43  int err;
44 
47  if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
48  err = ff_add_format(&infmts, av_pix_fmt_desc_get_id(desc));
49  else
50  err = ff_add_format(&outfmts, av_pix_fmt_desc_get_id(desc));
51  if (err) {
52  ff_formats_unref(&infmts);
53  ff_formats_unref(&outfmts);
54  return err;
55  }
56  }
57 
58  if ((err = ff_formats_ref(infmts, &avctx->inputs[0]->out_formats)) < 0 ||
59  (err = ff_formats_ref(outfmts, &avctx->outputs[0]->in_formats)) < 0)
60  return err;
61 
62  return 0;
63 }
64 
66 {
67  AVFilterContext *avctx = inlink->dst;
68  HWDownloadContext *ctx = avctx->priv;
69 
70  av_buffer_unref(&ctx->hwframes_ref);
71 
72  if (!inlink->hw_frames_ctx) {
73  av_log(ctx, AV_LOG_ERROR, "The input must have a hardware frame "
74  "reference.\n");
75  return AVERROR(EINVAL);
76  }
77 
78  ctx->hwframes_ref = av_buffer_ref(inlink->hw_frames_ctx);
79  if (!ctx->hwframes_ref)
80  return AVERROR(ENOMEM);
81 
82  ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
83 
84  return 0;
85 }
86 
88 {
89  AVFilterContext *avctx = outlink->src;
90  AVFilterLink *inlink = avctx->inputs[0];
91  HWDownloadContext *ctx = avctx->priv;
92  enum AVPixelFormat *formats;
93  int err, i, found;
94 
95  if (!ctx->hwframes_ref)
96  return AVERROR(EINVAL);
97 
98  err = av_hwframe_transfer_get_formats(ctx->hwframes_ref,
100  &formats, 0);
101  if (err < 0)
102  return err;
103 
104  found = 0;
105  for (i = 0; formats[i] != AV_PIX_FMT_NONE; i++) {
106  if (formats[i] == outlink->format) {
107  found = 1;
108  break;
109  }
110  }
111  av_freep(&formats);
112 
113  if (!found) {
114  av_log(ctx, AV_LOG_ERROR, "Invalid output format %s for hwframe "
115  "download.\n", av_get_pix_fmt_name(outlink->format));
116  return AVERROR(EINVAL);
117  }
118 
119  outlink->w = inlink->w;
120  outlink->h = inlink->h;
121 
122  return 0;
123 }
124 
126 {
127  AVFilterContext *avctx = link->dst;
128  AVFilterLink *outlink = avctx->outputs[0];
129  HWDownloadContext *ctx = avctx->priv;
130  AVFrame *output = NULL;
131  int err;
132 
133  if (!ctx->hwframes_ref || !input->hw_frames_ctx) {
134  av_log(ctx, AV_LOG_ERROR, "Input frames must have hardware context.\n");
135  err = AVERROR(EINVAL);
136  goto fail;
137  }
138  if ((void*)ctx->hwframes != input->hw_frames_ctx->data) {
139  av_log(ctx, AV_LOG_ERROR, "Input frame is not the in the configured "
140  "hwframe context.\n");
141  err = AVERROR(EINVAL);
142  goto fail;
143  }
144 
145  output = ff_get_video_buffer(outlink, ctx->hwframes->width,
146  ctx->hwframes->height);
147  if (!output) {
148  err = AVERROR(ENOMEM);
149  goto fail;
150  }
151 
153  if (err < 0) {
154  av_log(ctx, AV_LOG_ERROR, "Failed to download frame: %d.\n", err);
155  goto fail;
156  }
157 
158  output->width = outlink->w;
159  output->height = outlink->h;
160 
162  if (err < 0)
163  goto fail;
164 
166 
167  return ff_filter_frame(avctx->outputs[0], output);
168 
169 fail:
172  return err;
173 }
174 
176 {
177  HWDownloadContext *ctx = avctx->priv;
178 
179  av_buffer_unref(&ctx->hwframes_ref);
180 }
181 
182 static const AVClass hwdownload_class = {
183  .class_name = "hwdownload",
184  .item_name = av_default_item_name,
185  .option = NULL,
186  .version = LIBAVUTIL_VERSION_INT,
187 };
188 
189 static const AVFilterPad hwdownload_inputs[] = {
190  {
191  .name = "default",
192  .type = AVMEDIA_TYPE_VIDEO,
193  .config_props = hwdownload_config_input,
194  .filter_frame = hwdownload_filter_frame,
195  },
196  { NULL }
197 };
198 
199 static const AVFilterPad hwdownload_outputs[] = {
200  {
201  .name = "default",
202  .type = AVMEDIA_TYPE_VIDEO,
203  .config_props = hwdownload_config_output,
204  },
205  { NULL }
206 };
207 
209  .name = "hwdownload",
210  .description = NULL_IF_CONFIG_SMALL("Download a hardware frame to a normal frame"),
211  .uninit = hwdownload_uninit,
212  .query_formats = hwdownload_query_formats,
213  .priv_size = sizeof(HWDownloadContext),
214  .priv_class = &hwdownload_class,
217  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
218 };
formats
formats
Definition: signature.h:48
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:99
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
hwdownload_config_input
static int hwdownload_config_input(AVFilterLink *inlink)
Definition: vf_hwdownload.c:65
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
AV_HWFRAME_TRANSFER_DIRECTION_FROM
@ AV_HWFRAME_TRANSFER_DIRECTION_FROM
Transfer the data from the queried hw frame.
Definition: hwcontext.h:398
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: internal.h:385
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
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
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:202
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
pixdesc.h
av_pix_fmt_desc_next
const AVPixFmtDescriptor * av_pix_fmt_desc_next(const AVPixFmtDescriptor *prev)
Iterate over all pixel format descriptors known to libavutil.
Definition: pixdesc.c:2529
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:148
video.h
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
formats.h
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:353
fail
#define fail()
Definition: checkasm.h:120
AV_PIX_FMT_FLAG_HWACCEL
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:140
hwdownload_filter_frame
static int hwdownload_filter_frame(AVFilterLink *link, AVFrame *input)
Definition: vf_hwdownload.c:125
HWDownloadContext::hwframes
AVHWFramesContext * hwframes
Definition: vf_hwdownload.c:35
hwdownload_query_formats
static int hwdownload_query_formats(AVFilterContext *avctx)
Definition: vf_hwdownload.c:38
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_cold
#define av_cold
Definition: attributes.h:84
ff_formats_ref
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:440
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
hwdownload_inputs
static const AVFilterPad hwdownload_inputs[]
Definition: vf_hwdownload.c:189
ctx
AVFormatContext * ctx
Definition: movenc.c:48
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
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
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:654
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:125
HWDownloadContext
Definition: vf_hwdownload.c:31
AVFilterContext::inputs
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
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:337
hwdownload_uninit
static av_cold void hwdownload_uninit(AVFilterContext *avctx)
Definition: vf_hwdownload.c:175
inputs
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
Definition: filter_design.txt:243
hwdownload_outputs
static const AVFilterPad hwdownload_outputs[]
Definition: vf_hwdownload.c:199
HWDownloadContext::hwframes_ref
AVBufferRef * hwframes_ref
Definition: vf_hwdownload.c:34
desc
const char * desc
Definition: nvenc.c:68
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:188
hwdownload_config_output
static int hwdownload_config_output(AVFilterLink *outlink)
Definition: vf_hwdownload.c:87
buffer.h
av_pix_fmt_desc_get_id
enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc)
Definition: pixdesc.c:2541
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
internal.h
ff_formats_unref
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
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
hwdownload_class
static const AVClass hwdownload_class
Definition: vf_hwdownload.c:182
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
AVFilter
Filter definition.
Definition: avfilter.h:144
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:123
AVClass::class_name
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
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:439
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
avfilter.h
av_buffer_ref
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
AVFilterContext
An instance of a filter.
Definition: avfilter.h:338
av_hwframe_transfer_get_formats
int av_hwframe_transfer_get_formats(AVBufferRef *hwframe_ref, enum AVHWFrameTransferDirection dir, enum AVPixelFormat **formats, int flags)
Get a list of possible source or target formats usable in av_hwframe_transfer_data().
Definition: hwcontext.c:381
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:81
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
hwcontext.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
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:2438
ff_vf_hwdownload
AVFilter ff_vf_hwdownload
Definition: vf_hwdownload.c:208
AVFilterContext::outputs
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350