FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_hwdownload.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 0 79 0.0%
Functions: 0 5 0.0%
Branches: 0 30 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/log.h"
22 #include "libavutil/mem.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
25
26 #include "avfilter.h"
27 #include "filters.h"
28 #include "formats.h"
29 #include "video.h"
30
31 typedef struct HWDownloadContext {
32 const AVClass *class;
33
34 AVBufferRef *hwframes_ref;
35 AVHWFramesContext *hwframes;
36 } HWDownloadContext;
37
38 static int hwdownload_query_formats(const AVFilterContext *avctx,
39 AVFilterFormatsConfig **cfg_in,
40 AVFilterFormatsConfig **cfg_out)
41 {
42 int err;
43
44 if ((err = ff_formats_ref(ff_formats_pixdesc_filter(AV_PIX_FMT_FLAG_HWACCEL, 0),
45 &cfg_in[0]->formats)) ||
46 (err = ff_formats_ref(ff_formats_pixdesc_filter(0, AV_PIX_FMT_FLAG_HWACCEL),
47 &cfg_out[0]->formats)))
48 return err;
49
50 return 0;
51 }
52
53 static int hwdownload_config_input(AVFilterLink *inlink)
54 {
55 FilterLink *l = ff_filter_link(inlink);
56 AVFilterContext *avctx = inlink->dst;
57 HWDownloadContext *ctx = avctx->priv;
58
59 av_buffer_unref(&ctx->hwframes_ref);
60
61 if (!l->hw_frames_ctx) {
62 av_log(ctx, AV_LOG_ERROR, "The input must have a hardware frame "
63 "reference.\n");
64 return AVERROR(EINVAL);
65 }
66
67 ctx->hwframes_ref = av_buffer_ref(l->hw_frames_ctx);
68 if (!ctx->hwframes_ref)
69 return AVERROR(ENOMEM);
70
71 ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
72
73 return 0;
74 }
75
76 static int hwdownload_config_output(AVFilterLink *outlink)
77 {
78 AVFilterContext *avctx = outlink->src;
79 AVFilterLink *inlink = avctx->inputs[0];
80 HWDownloadContext *ctx = avctx->priv;
81 enum AVPixelFormat *formats;
82 int err, i, found;
83
84 if (!ctx->hwframes_ref)
85 return AVERROR(EINVAL);
86
87 err = av_hwframe_transfer_get_formats(ctx->hwframes_ref,
88 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
89 &formats, 0);
90 if (err < 0)
91 return err;
92
93 found = 0;
94 for (i = 0; formats[i] != AV_PIX_FMT_NONE; i++) {
95 if (formats[i] == outlink->format) {
96 found = 1;
97 break;
98 }
99 }
100 av_freep(&formats);
101
102 if (!found) {
103 av_log(ctx, AV_LOG_ERROR, "Invalid output format %s for hwframe "
104 "download.\n", av_get_pix_fmt_name(outlink->format));
105 return AVERROR(EINVAL);
106 }
107
108 outlink->w = inlink->w;
109 outlink->h = inlink->h;
110
111 return 0;
112 }
113
114 static int hwdownload_filter_frame(AVFilterLink *link, AVFrame *input)
115 {
116 AVFilterContext *avctx = link->dst;
117 AVFilterLink *outlink = avctx->outputs[0];
118 HWDownloadContext *ctx = avctx->priv;
119 AVFrame *output = NULL;
120 int err;
121
122 if (!ctx->hwframes_ref || !input->hw_frames_ctx) {
123 av_log(ctx, AV_LOG_ERROR, "Input frames must have hardware context.\n");
124 err = AVERROR(EINVAL);
125 goto fail;
126 }
127 if ((void*)ctx->hwframes != input->hw_frames_ctx->data) {
128 av_log(ctx, AV_LOG_ERROR, "Input frame is not the in the configured "
129 "hwframe context.\n");
130 err = AVERROR(EINVAL);
131 goto fail;
132 }
133
134 output = ff_get_video_buffer(outlink, ctx->hwframes->width,
135 ctx->hwframes->height);
136 if (!output) {
137 err = AVERROR(ENOMEM);
138 goto fail;
139 }
140
141 err = av_hwframe_transfer_data(output, input, 0);
142 if (err < 0) {
143 av_log(ctx, AV_LOG_ERROR, "Failed to download frame: %d.\n", err);
144 goto fail;
145 }
146
147 output->width = outlink->w;
148 output->height = outlink->h;
149
150 err = av_frame_copy_props(output, input);
151 if (err < 0)
152 goto fail;
153
154 av_frame_free(&input);
155
156 return ff_filter_frame(avctx->outputs[0], output);
157
158 fail:
159 av_frame_free(&input);
160 av_frame_free(&output);
161 return err;
162 }
163
164 static av_cold void hwdownload_uninit(AVFilterContext *avctx)
165 {
166 HWDownloadContext *ctx = avctx->priv;
167
168 av_buffer_unref(&ctx->hwframes_ref);
169 }
170
171 static const AVClass hwdownload_class = {
172 .class_name = "hwdownload",
173 .item_name = av_default_item_name,
174 .option = NULL,
175 .version = LIBAVUTIL_VERSION_INT,
176 };
177
178 static const AVFilterPad hwdownload_inputs[] = {
179 {
180 .name = "default",
181 .type = AVMEDIA_TYPE_VIDEO,
182 .config_props = hwdownload_config_input,
183 .filter_frame = hwdownload_filter_frame,
184 },
185 };
186
187 static const AVFilterPad hwdownload_outputs[] = {
188 {
189 .name = "default",
190 .type = AVMEDIA_TYPE_VIDEO,
191 .config_props = hwdownload_config_output,
192 },
193 };
194
195 const AVFilter ff_vf_hwdownload = {
196 .name = "hwdownload",
197 .description = NULL_IF_CONFIG_SMALL("Download a hardware frame to a normal frame"),
198 .uninit = hwdownload_uninit,
199 .priv_size = sizeof(HWDownloadContext),
200 .priv_class = &hwdownload_class,
201 FILTER_INPUTS(hwdownload_inputs),
202 FILTER_OUTPUTS(hwdownload_outputs),
203 FILTER_QUERY_FUNC2(hwdownload_query_formats),
204 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
205 };
206