2 * This file is part of FFmpeg.
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.
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.
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
19 #include "libavutil/avassert.h"
20 #include "libavutil/buffer.h"
21 #include "libavutil/hwcontext.h"
22 #include "libavutil/log.h"
23 #include "libavutil/mem.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/pixdesc.h"
32 typedef struct HWDownloadContext {
35 AVBufferRef *hwframes_ref;
36 AVHWFramesContext *hwframes;
39 static int hwdownload_query_formats(AVFilterContext *avctx)
41 AVFilterFormats *infmts = NULL;
42 AVFilterFormats *outfmts = NULL;
43 const AVPixFmtDescriptor *desc;
46 for (desc = av_pix_fmt_desc_next(NULL); desc;
47 desc = av_pix_fmt_desc_next(desc)) {
48 if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
49 err = ff_add_format(&infmts, av_pix_fmt_desc_get_id(desc));
51 err = ff_add_format(&outfmts, av_pix_fmt_desc_get_id(desc));
53 ff_formats_unref(&infmts);
54 ff_formats_unref(&outfmts);
59 ff_formats_ref(infmts, &avctx->inputs[0]->out_formats);
60 ff_formats_ref(outfmts, &avctx->outputs[0]->in_formats);
64 static int hwdownload_config_input(AVFilterLink *inlink)
66 AVFilterContext *avctx = inlink->dst;
67 HWDownloadContext *ctx = avctx->priv;
69 av_buffer_unref(&ctx->hwframes_ref);
71 if (!inlink->hw_frames_ctx) {
72 av_log(ctx, AV_LOG_ERROR, "The input must have a hardware frame "
74 return AVERROR(EINVAL);
77 ctx->hwframes_ref = av_buffer_ref(inlink->hw_frames_ctx);
78 if (!ctx->hwframes_ref)
79 return AVERROR(ENOMEM);
81 ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
86 static int hwdownload_config_output(AVFilterLink *outlink)
88 AVFilterContext *avctx = outlink->src;
89 AVFilterLink *inlink = avctx->inputs[0];
90 HWDownloadContext *ctx = avctx->priv;
91 enum AVPixelFormat *formats;
94 if (!ctx->hwframes_ref)
95 return AVERROR(EINVAL);
97 err = av_hwframe_transfer_get_formats(ctx->hwframes_ref,
98 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
104 for (i = 0; formats[i] != AV_PIX_FMT_NONE; i++) {
105 if (formats[i] == outlink->format) {
113 av_log(ctx, AV_LOG_ERROR, "Invalid output format %s for hwframe "
114 "download.\n", av_get_pix_fmt_name(outlink->format));
115 return AVERROR(EINVAL);
118 outlink->w = inlink->w;
119 outlink->h = inlink->h;
124 static int hwdownload_filter_frame(AVFilterLink *link, AVFrame *input)
126 AVFilterContext *avctx = link->dst;
127 AVFilterLink *outlink = avctx->outputs[0];
128 HWDownloadContext *ctx = avctx->priv;
129 AVFrame *output = NULL;
132 if (!ctx->hwframes_ref || !input->hw_frames_ctx) {
133 av_log(ctx, AV_LOG_ERROR, "Input frames must have hardware context.\n");
134 err = AVERROR(EINVAL);
137 if ((void*)ctx->hwframes != input->hw_frames_ctx->data) {
138 av_log(ctx, AV_LOG_ERROR, "Input frame is not the in the configured "
139 "hwframe context.\n");
140 err = AVERROR(EINVAL);
144 output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
146 err = AVERROR(ENOMEM);
150 err = av_hwframe_transfer_data(output, input, 0);
152 av_log(ctx, AV_LOG_ERROR, "Failed to download frame: %d.\n", err);
156 err = av_frame_copy_props(output, input);
160 av_frame_free(&input);
162 return ff_filter_frame(avctx->outputs[0], output);
165 av_frame_free(&input);
166 av_frame_free(&output);
170 static av_cold void hwdownload_uninit(AVFilterContext *avctx)
172 HWDownloadContext *ctx = avctx->priv;
174 av_buffer_unref(&ctx->hwframes_ref);
177 static const AVClass hwdownload_class = {
178 .class_name = "hwdownload",
179 .item_name = av_default_item_name,
181 .version = LIBAVUTIL_VERSION_INT,
184 static const AVFilterPad hwdownload_inputs[] = {
187 .type = AVMEDIA_TYPE_VIDEO,
188 .config_props = hwdownload_config_input,
189 .filter_frame = hwdownload_filter_frame,
194 static const AVFilterPad hwdownload_outputs[] = {
197 .type = AVMEDIA_TYPE_VIDEO,
198 .config_props = hwdownload_config_output,
203 AVFilter ff_vf_hwdownload = {
204 .name = "hwdownload",
205 .description = NULL_IF_CONFIG_SMALL("Download a hardware frame to a normal frame"),
206 .uninit = hwdownload_uninit,
207 .query_formats = hwdownload_query_formats,
208 .priv_size = sizeof(HWDownloadContext),
209 .priv_class = &hwdownload_class,
210 .inputs = hwdownload_inputs,
211 .outputs = hwdownload_outputs,