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  int err;
41 
43  &avctx->inputs[0]->outcfg.formats)) ||
45  &avctx->outputs[0]->incfg.formats)))
46  return err;
47 
48  return 0;
49 }
50 
52 {
53  AVFilterContext *avctx = inlink->dst;
54  HWDownloadContext *ctx = avctx->priv;
55 
56  av_buffer_unref(&ctx->hwframes_ref);
57 
58  if (!inlink->hw_frames_ctx) {
59  av_log(ctx, AV_LOG_ERROR, "The input must have a hardware frame "
60  "reference.\n");
61  return AVERROR(EINVAL);
62  }
63 
64  ctx->hwframes_ref = av_buffer_ref(inlink->hw_frames_ctx);
65  if (!ctx->hwframes_ref)
66  return AVERROR(ENOMEM);
67 
68  ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
69 
70  return 0;
71 }
72 
74 {
75  AVFilterContext *avctx = outlink->src;
76  AVFilterLink *inlink = avctx->inputs[0];
77  HWDownloadContext *ctx = avctx->priv;
78  enum AVPixelFormat *formats;
79  int err, i, found;
80 
81  if (!ctx->hwframes_ref)
82  return AVERROR(EINVAL);
83 
84  err = av_hwframe_transfer_get_formats(ctx->hwframes_ref,
86  &formats, 0);
87  if (err < 0)
88  return err;
89 
90  found = 0;
91  for (i = 0; formats[i] != AV_PIX_FMT_NONE; i++) {
92  if (formats[i] == outlink->format) {
93  found = 1;
94  break;
95  }
96  }
97  av_freep(&formats);
98 
99  if (!found) {
100  av_log(ctx, AV_LOG_ERROR, "Invalid output format %s for hwframe "
101  "download.\n", av_get_pix_fmt_name(outlink->format));
102  return AVERROR(EINVAL);
103  }
104 
105  outlink->w = inlink->w;
106  outlink->h = inlink->h;
107 
108  return 0;
109 }
110 
112 {
113  AVFilterContext *avctx = link->dst;
114  AVFilterLink *outlink = avctx->outputs[0];
115  HWDownloadContext *ctx = avctx->priv;
116  AVFrame *output = NULL;
117  int err;
118 
119  if (!ctx->hwframes_ref || !input->hw_frames_ctx) {
120  av_log(ctx, AV_LOG_ERROR, "Input frames must have hardware context.\n");
121  err = AVERROR(EINVAL);
122  goto fail;
123  }
124  if ((void*)ctx->hwframes != input->hw_frames_ctx->data) {
125  av_log(ctx, AV_LOG_ERROR, "Input frame is not the in the configured "
126  "hwframe context.\n");
127  err = AVERROR(EINVAL);
128  goto fail;
129  }
130 
131  output = ff_get_video_buffer(outlink, ctx->hwframes->width,
132  ctx->hwframes->height);
133  if (!output) {
134  err = AVERROR(ENOMEM);
135  goto fail;
136  }
137 
139  if (err < 0) {
140  av_log(ctx, AV_LOG_ERROR, "Failed to download frame: %d.\n", err);
141  goto fail;
142  }
143 
144  output->width = outlink->w;
145  output->height = outlink->h;
146 
148  if (err < 0)
149  goto fail;
150 
152 
153  return ff_filter_frame(avctx->outputs[0], output);
154 
155 fail:
158  return err;
159 }
160 
162 {
163  HWDownloadContext *ctx = avctx->priv;
164 
165  av_buffer_unref(&ctx->hwframes_ref);
166 }
167 
168 static const AVClass hwdownload_class = {
169  .class_name = "hwdownload",
170  .item_name = av_default_item_name,
171  .option = NULL,
172  .version = LIBAVUTIL_VERSION_INT,
173 };
174 
175 static const AVFilterPad hwdownload_inputs[] = {
176  {
177  .name = "default",
178  .type = AVMEDIA_TYPE_VIDEO,
179  .config_props = hwdownload_config_input,
180  .filter_frame = hwdownload_filter_frame,
181  },
182 };
183 
184 static const AVFilterPad hwdownload_outputs[] = {
185  {
186  .name = "default",
187  .type = AVMEDIA_TYPE_VIDEO,
188  .config_props = hwdownload_config_output,
189  },
190 };
191 
193  .name = "hwdownload",
194  .description = NULL_IF_CONFIG_SMALL("Download a hardware frame to a normal frame"),
195  .uninit = hwdownload_uninit,
196  .priv_size = sizeof(HWDownloadContext),
197  .priv_class = &hwdownload_class,
201  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
202 };
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:98
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
hwdownload_config_input
static int hwdownload_config_input(AVFilterLink *inlink)
Definition: vf_hwdownload.c:51
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:419
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:371
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
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:109
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
pixdesc.h
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:168
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:169
video.h
formats.h
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:417
fail
#define fail()
Definition: checkasm.h:127
AV_PIX_FMT_FLAG_HWACCEL
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:128
hwdownload_filter_frame
static int hwdownload_filter_frame(AVFilterLink *link, AVFrame *input)
Definition: vf_hwdownload.c:111
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:50
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
ff_formats_ref
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:555
hwdownload_inputs
static const AVFilterPad hwdownload_inputs[]
Definition: vf_hwdownload.c:175
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
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:66
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:537
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
HWDownloadContext
Definition: vf_hwdownload.c:31
AVFilterContext::inputs
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:410
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
hwdownload_uninit
static av_cold void hwdownload_uninit(AVFilterContext *avctx)
Definition: vf_hwdownload.c:161
hwdownload_outputs
static const AVFilterPad hwdownload_outputs[]
Definition: vf_hwdownload.c:184
HWDownloadContext::hwframes_ref
AVBufferRef * hwframes_ref
Definition: vf_hwdownload.c:34
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:117
hwdownload_config_output
static int hwdownload_config_output(AVFilterLink *outlink)
Definition: vf_hwdownload.c:73
buffer.h
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_pixdesc_filter
AVFilterFormats * ff_formats_pixdesc_filter(unsigned want, unsigned rej)
Construct a formats list containing all pixel formats with certain properties.
Definition: formats.c:457
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
hwdownload_class
static const AVClass hwdownload_class
Definition: vf_hwdownload.c:168
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
AVFilter
Filter definition.
Definition: avfilter.h:165
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:124
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:71
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:443
ff_vf_hwdownload
const AVFilter ff_vf_hwdownload
Definition: vf_hwdownload.c:192
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:402
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:385
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
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:506
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
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:2580
AVFilterContext::outputs
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:414