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/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"
31 typedef struct HWDownloadContext {
34 AVBufferRef *hwframes_ref;
35 AVHWFramesContext *hwframes;
38 static int hwdownload_query_formats(AVFilterContext *avctx)
40 AVFilterFormats *fmts;
43 if ((err = ff_formats_pixdesc_filter(&fmts, AV_PIX_FMT_FLAG_HWACCEL, 0)) ||
44 (err = ff_formats_ref(fmts, &avctx->inputs[0]->outcfg.formats)) ||
45 (err = ff_formats_pixdesc_filter(&fmts, 0, AV_PIX_FMT_FLAG_HWACCEL)) ||
46 (err = ff_formats_ref(fmts, &avctx->outputs[0]->incfg.formats)))
52 static int hwdownload_config_input(AVFilterLink *inlink)
54 AVFilterContext *avctx = inlink->dst;
55 HWDownloadContext *ctx = avctx->priv;
57 av_buffer_unref(&ctx->hwframes_ref);
59 if (!inlink->hw_frames_ctx) {
60 av_log(ctx, AV_LOG_ERROR, "The input must have a hardware frame "
62 return AVERROR(EINVAL);
65 ctx->hwframes_ref = av_buffer_ref(inlink->hw_frames_ctx);
66 if (!ctx->hwframes_ref)
67 return AVERROR(ENOMEM);
69 ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
74 static int hwdownload_config_output(AVFilterLink *outlink)
76 AVFilterContext *avctx = outlink->src;
77 AVFilterLink *inlink = avctx->inputs[0];
78 HWDownloadContext *ctx = avctx->priv;
79 enum AVPixelFormat *formats;
82 if (!ctx->hwframes_ref)
83 return AVERROR(EINVAL);
85 err = av_hwframe_transfer_get_formats(ctx->hwframes_ref,
86 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
92 for (i = 0; formats[i] != AV_PIX_FMT_NONE; i++) {
93 if (formats[i] == outlink->format) {
101 av_log(ctx, AV_LOG_ERROR, "Invalid output format %s for hwframe "
102 "download.\n", av_get_pix_fmt_name(outlink->format));
103 return AVERROR(EINVAL);
106 outlink->w = inlink->w;
107 outlink->h = inlink->h;
112 static int hwdownload_filter_frame(AVFilterLink *link, AVFrame *input)
114 AVFilterContext *avctx = link->dst;
115 AVFilterLink *outlink = avctx->outputs[0];
116 HWDownloadContext *ctx = avctx->priv;
117 AVFrame *output = NULL;
120 if (!ctx->hwframes_ref || !input->hw_frames_ctx) {
121 av_log(ctx, AV_LOG_ERROR, "Input frames must have hardware context.\n");
122 err = AVERROR(EINVAL);
125 if ((void*)ctx->hwframes != input->hw_frames_ctx->data) {
126 av_log(ctx, AV_LOG_ERROR, "Input frame is not the in the configured "
127 "hwframe context.\n");
128 err = AVERROR(EINVAL);
132 output = ff_get_video_buffer(outlink, ctx->hwframes->width,
133 ctx->hwframes->height);
135 err = AVERROR(ENOMEM);
139 err = av_hwframe_transfer_data(output, input, 0);
141 av_log(ctx, AV_LOG_ERROR, "Failed to download frame: %d.\n", err);
145 output->width = outlink->w;
146 output->height = outlink->h;
148 err = av_frame_copy_props(output, input);
152 av_frame_free(&input);
154 return ff_filter_frame(avctx->outputs[0], output);
157 av_frame_free(&input);
158 av_frame_free(&output);
162 static av_cold void hwdownload_uninit(AVFilterContext *avctx)
164 HWDownloadContext *ctx = avctx->priv;
166 av_buffer_unref(&ctx->hwframes_ref);
169 static const AVClass hwdownload_class = {
170 .class_name = "hwdownload",
171 .item_name = av_default_item_name,
173 .version = LIBAVUTIL_VERSION_INT,
176 static const AVFilterPad hwdownload_inputs[] = {
179 .type = AVMEDIA_TYPE_VIDEO,
180 .config_props = hwdownload_config_input,
181 .filter_frame = hwdownload_filter_frame,
186 static const AVFilterPad hwdownload_outputs[] = {
189 .type = AVMEDIA_TYPE_VIDEO,
190 .config_props = hwdownload_config_output,
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 .query_formats = hwdownload_query_formats,
200 .priv_size = sizeof(HWDownloadContext),
201 .priv_class = &hwdownload_class,
202 .inputs = hwdownload_inputs,
203 .outputs = hwdownload_outputs,
204 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,