FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_hwdownload.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 0 77 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 "formats.h"
28 #include "internal.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(AVFilterContext *avctx)
39 {
40 int err;
41
42 if ((err = ff_formats_ref(ff_formats_pixdesc_filter(AV_PIX_FMT_FLAG_HWACCEL, 0),
43 &avctx->inputs[0]->outcfg.formats)) ||
44 (err = ff_formats_ref(ff_formats_pixdesc_filter(0, AV_PIX_FMT_FLAG_HWACCEL),
45 &avctx->outputs[0]->incfg.formats)))
46 return err;
47
48 return 0;
49 }
50
51 static int hwdownload_config_input(AVFilterLink *inlink)
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
73 static int hwdownload_config_output(AVFilterLink *outlink)
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,
85 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
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
111 static int hwdownload_filter_frame(AVFilterLink *link, AVFrame *input)
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
138 err = av_hwframe_transfer_data(output, input, 0);
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
147 err = av_frame_copy_props(output, input);
148 if (err < 0)
149 goto fail;
150
151 av_frame_free(&input);
152
153 return ff_filter_frame(avctx->outputs[0], output);
154
155 fail:
156 av_frame_free(&input);
157 av_frame_free(&output);
158 return err;
159 }
160
161 static av_cold void hwdownload_uninit(AVFilterContext *avctx)
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
192 const AVFilter ff_vf_hwdownload = {
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,
198 FILTER_INPUTS(hwdownload_inputs),
199 FILTER_OUTPUTS(hwdownload_outputs),
200 FILTER_QUERY_FUNC(hwdownload_query_formats),
201 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
202 };
203