FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_hwupload.c
Date: 2024-10-04 17:46:48
Exec Total Coverage
Lines: 0 115 0.0%
Functions: 0 4 0.0%
Branches: 0 48 0.0%

Line Branch Exec Source
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/hwcontext_internal.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
34 AVBufferRef *hwdevice_ref;
35
36 AVBufferRef *hwframes_ref;
37 AVHWFramesContext *hwframes;
38
39 char *device_type;
40 } HWUploadContext;
41
42 static int hwupload_query_formats(AVFilterContext *avctx)
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) {
54 err = av_hwdevice_ctx_create_derived(
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
107 static int hwupload_config_output(AVFilterLink *outlink)
108 {
109 FilterLink *outl = ff_filter_link(outlink);
110 AVFilterContext *avctx = outlink->src;
111 AVFilterLink *inlink = avctx->inputs[0];
112 FilterLink *inl = ff_filter_link(inlink);
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
126 outl->hw_frames_ctx = av_buffer_ref(inl->hw_frames_ctx);
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 =
145 (AVHWFramesContext*)inl->hw_frames_ctx->data;
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
173 static int hwupload_filter_frame(AVFilterLink *link, AVFrame *input)
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
194 err = av_hwframe_transfer_data(output, input, 0);
195 if (err < 0) {
196 av_log(ctx, AV_LOG_ERROR, "Failed to upload frame: %d.\n", err);
197 goto fail;
198 }
199
200 err = av_frame_copy_props(output, input);
201 if (err < 0)
202 goto fail;
203
204 av_frame_free(&input);
205
206 return ff_filter_frame(outlink, output);
207
208 fail:
209 av_frame_free(&input);
210 av_frame_free(&output);
211 return err;
212 }
213
214 static av_cold void hwupload_uninit(AVFilterContext *avctx)
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
253 const AVFilter ff_vf_hwupload = {
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,
259 FILTER_INPUTS(hwupload_inputs),
260 FILTER_OUTPUTS(hwupload_outputs),
261 FILTER_QUERY_FUNC(hwupload_query_formats),
262 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
263 .flags = AVFILTER_FLAG_HWDEVICE,
264 };
265