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 if ((err = ff_formats_ref(infmts, &avctx->inputs[0]->out_formats)) < 0 ||
60 (err = ff_formats_ref(outfmts, &avctx->outputs[0]->in_formats)) < 0)
66 static int hwdownload_config_input(AVFilterLink *inlink)
68 AVFilterContext *avctx = inlink->dst;
69 HWDownloadContext *ctx = avctx->priv;
71 av_buffer_unref(&ctx->hwframes_ref);
73 if (!inlink->hw_frames_ctx) {
74 av_log(ctx, AV_LOG_ERROR, "The input must have a hardware frame "
76 return AVERROR(EINVAL);
79 ctx->hwframes_ref = av_buffer_ref(inlink->hw_frames_ctx);
80 if (!ctx->hwframes_ref)
81 return AVERROR(ENOMEM);
83 ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
88 static int hwdownload_config_output(AVFilterLink *outlink)
90 AVFilterContext *avctx = outlink->src;
91 AVFilterLink *inlink = avctx->inputs[0];
92 HWDownloadContext *ctx = avctx->priv;
93 enum AVPixelFormat *formats;
96 if (!ctx->hwframes_ref)
97 return AVERROR(EINVAL);
99 err = av_hwframe_transfer_get_formats(ctx->hwframes_ref,
100 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
106 for (i = 0; formats[i] != AV_PIX_FMT_NONE; i++) {
107 if (formats[i] == outlink->format) {
115 av_log(ctx, AV_LOG_ERROR, "Invalid output format %s for hwframe "
116 "download.\n", av_get_pix_fmt_name(outlink->format));
117 return AVERROR(EINVAL);
120 outlink->w = inlink->w;
121 outlink->h = inlink->h;
126 static int hwdownload_filter_frame(AVFilterLink *link, AVFrame *input)
128 AVFilterContext *avctx = link->dst;
129 AVFilterLink *outlink = avctx->outputs[0];
130 HWDownloadContext *ctx = avctx->priv;
131 AVFrame *output = NULL;
134 if (!ctx->hwframes_ref || !input->hw_frames_ctx) {
135 av_log(ctx, AV_LOG_ERROR, "Input frames must have hardware context.\n");
136 err = AVERROR(EINVAL);
139 if ((void*)ctx->hwframes != input->hw_frames_ctx->data) {
140 av_log(ctx, AV_LOG_ERROR, "Input frame is not the in the configured "
141 "hwframe context.\n");
142 err = AVERROR(EINVAL);
146 output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
148 err = AVERROR(ENOMEM);
152 err = av_hwframe_transfer_data(output, input, 0);
154 av_log(ctx, AV_LOG_ERROR, "Failed to download frame: %d.\n", err);
158 err = av_frame_copy_props(output, input);
162 av_frame_free(&input);
164 return ff_filter_frame(avctx->outputs[0], output);
167 av_frame_free(&input);
168 av_frame_free(&output);
172 static av_cold void hwdownload_uninit(AVFilterContext *avctx)
174 HWDownloadContext *ctx = avctx->priv;
176 av_buffer_unref(&ctx->hwframes_ref);
179 static const AVClass hwdownload_class = {
180 .class_name = "hwdownload",
181 .item_name = av_default_item_name,
183 .version = LIBAVUTIL_VERSION_INT,
186 static const AVFilterPad hwdownload_inputs[] = {
189 .type = AVMEDIA_TYPE_VIDEO,
190 .config_props = hwdownload_config_input,
191 .filter_frame = hwdownload_filter_frame,
196 static const AVFilterPad hwdownload_outputs[] = {
199 .type = AVMEDIA_TYPE_VIDEO,
200 .config_props = hwdownload_config_output,
205 AVFilter ff_vf_hwdownload = {
206 .name = "hwdownload",
207 .description = NULL_IF_CONFIG_SMALL("Download a hardware frame to a normal frame"),
208 .uninit = hwdownload_uninit,
209 .query_formats = hwdownload_query_formats,
210 .priv_size = sizeof(HWDownloadContext),
211 .priv_class = &hwdownload_class,
212 .inputs = hwdownload_inputs,
213 .outputs = hwdownload_outputs,