FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_hwupload.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 0 113 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 "formats.h"
28 #include "internal.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 AVFilterContext *avctx = outlink->src;
110 AVFilterLink *inlink = avctx->inputs[0];
111 HWUploadContext *ctx = avctx->priv;
112 int err;
113
114 av_buffer_unref(&ctx->hwframes_ref);
115
116 if (inlink->format == outlink->format) {
117 // The input is already a hardware format, so we just want to
118 // pass through the input frames in their own hardware context.
119 if (!inlink->hw_frames_ctx) {
120 av_log(ctx, AV_LOG_ERROR, "No input hwframe context.\n");
121 return AVERROR(EINVAL);
122 }
123
124 outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
125 if (!outlink->hw_frames_ctx)
126 return AVERROR(ENOMEM);
127
128 return 0;
129 }
130
131 ctx->hwframes_ref = av_hwframe_ctx_alloc(ctx->hwdevice_ref);
132 if (!ctx->hwframes_ref)
133 return AVERROR(ENOMEM);
134
135 ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
136
137 av_log(ctx, AV_LOG_DEBUG, "Surface format is %s.\n",
138 av_get_pix_fmt_name(inlink->format));
139
140 ctx->hwframes->format = outlink->format;
141 if (inlink->hw_frames_ctx) {
142 AVHWFramesContext *in_hwframe_ctx =
143 (AVHWFramesContext*)inlink->hw_frames_ctx->data;
144 ctx->hwframes->sw_format = in_hwframe_ctx->sw_format;
145 } else {
146 ctx->hwframes->sw_format = inlink->format;
147 }
148 ctx->hwframes->width = inlink->w;
149 ctx->hwframes->height = inlink->h;
150
151 if (avctx->extra_hw_frames >= 0)
152 ctx->hwframes->initial_pool_size = 2 + avctx->extra_hw_frames;
153
154 err = av_hwframe_ctx_init(ctx->hwframes_ref);
155 if (err < 0)
156 goto fail;
157
158 outlink->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
159 if (!outlink->hw_frames_ctx) {
160 err = AVERROR(ENOMEM);
161 goto fail;
162 }
163
164 return 0;
165
166 fail:
167 av_buffer_unref(&ctx->hwframes_ref);
168 return err;
169 }
170
171 static int hwupload_filter_frame(AVFilterLink *link, AVFrame *input)
172 {
173 AVFilterContext *avctx = link->dst;
174 AVFilterLink *outlink = avctx->outputs[0];
175 HWUploadContext *ctx = avctx->priv;
176 AVFrame *output = NULL;
177 int err;
178
179 if (input->format == outlink->format)
180 return ff_filter_frame(outlink, input);
181
182 output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
183 if (!output) {
184 av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame to upload to.\n");
185 err = AVERROR(ENOMEM);
186 goto fail;
187 }
188
189 output->width = input->width;
190 output->height = input->height;
191
192 err = av_hwframe_transfer_data(output, input, 0);
193 if (err < 0) {
194 av_log(ctx, AV_LOG_ERROR, "Failed to upload frame: %d.\n", err);
195 goto fail;
196 }
197
198 err = av_frame_copy_props(output, input);
199 if (err < 0)
200 goto fail;
201
202 av_frame_free(&input);
203
204 return ff_filter_frame(outlink, output);
205
206 fail:
207 av_frame_free(&input);
208 av_frame_free(&output);
209 return err;
210 }
211
212 static av_cold void hwupload_uninit(AVFilterContext *avctx)
213 {
214 HWUploadContext *ctx = avctx->priv;
215
216 av_buffer_unref(&ctx->hwframes_ref);
217 av_buffer_unref(&ctx->hwdevice_ref);
218 }
219
220 #define OFFSET(x) offsetof(HWUploadContext, x)
221 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
222 static const AVOption hwupload_options[] = {
223 {
224 "derive_device", "Derive a new device of this type",
225 OFFSET(device_type), AV_OPT_TYPE_STRING,
226 { .str = NULL }, 0, 0, FLAGS
227 },
228 {
229 NULL
230 }
231 };
232
233 AVFILTER_DEFINE_CLASS(hwupload);
234
235 static const AVFilterPad hwupload_inputs[] = {
236 {
237 .name = "default",
238 .type = AVMEDIA_TYPE_VIDEO,
239 .filter_frame = hwupload_filter_frame,
240 },
241 };
242
243 static const AVFilterPad hwupload_outputs[] = {
244 {
245 .name = "default",
246 .type = AVMEDIA_TYPE_VIDEO,
247 .config_props = hwupload_config_output,
248 },
249 };
250
251 const AVFilter ff_vf_hwupload = {
252 .name = "hwupload",
253 .description = NULL_IF_CONFIG_SMALL("Upload a normal frame to a hardware frame"),
254 .uninit = hwupload_uninit,
255 .priv_size = sizeof(HWUploadContext),
256 .priv_class = &hwupload_class,
257 FILTER_INPUTS(hwupload_inputs),
258 FILTER_OUTPUTS(hwupload_outputs),
259 FILTER_QUERY_FUNC(hwupload_query_formats),
260 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
261 .flags = AVFILTER_FLAG_HWDEVICE,
262 };
263