2 * This file is part of Libav.
4 * Libav 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 * Libav 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 Libav; 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 *infmts = NULL;
41 AVFilterFormats *outfmts = NULL;
42 const AVPixFmtDescriptor *desc;
45 for (desc = av_pix_fmt_desc_next(NULL); desc;
46 desc = av_pix_fmt_desc_next(desc)) {
47 if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
48 err = ff_add_format(&infmts, av_pix_fmt_desc_get_id(desc));
50 err = ff_add_format(&outfmts, av_pix_fmt_desc_get_id(desc));
52 ff_formats_unref(&infmts);
53 ff_formats_unref(&outfmts);
58 ff_formats_ref(infmts, &avctx->inputs[0]->out_formats);
59 ff_formats_ref(outfmts, &avctx->outputs[0]->in_formats);
63 static int hwdownload_config_input(AVFilterLink *inlink)
65 AVFilterContext *avctx = inlink->dst;
66 HWDownloadContext *ctx = avctx->priv;
68 av_buffer_unref(&ctx->hwframes_ref);
70 if (!inlink->hw_frames_ctx) {
71 av_log(ctx, AV_LOG_ERROR, "The input must have a hardware frame "
73 return AVERROR(EINVAL);
76 ctx->hwframes_ref = av_buffer_ref(inlink->hw_frames_ctx);
77 if (!ctx->hwframes_ref)
78 return AVERROR(ENOMEM);
80 ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
85 static int hwdownload_config_output(AVFilterLink *outlink)
87 AVFilterContext *avctx = outlink->src;
88 AVFilterLink *inlink = avctx->inputs[0];
89 HWDownloadContext *ctx = avctx->priv;
90 enum AVPixelFormat *formats;
93 if (!ctx->hwframes_ref)
94 return AVERROR(EINVAL);
96 err = av_hwframe_transfer_get_formats(ctx->hwframes_ref,
97 AV_HWFRAME_TRANSFER_DIRECTION_FROM,
103 for (i = 0; formats[i] != AV_PIX_FMT_NONE; i++) {
104 if (formats[i] == outlink->format) {
112 av_log(ctx, AV_LOG_ERROR, "Invalid output format %s for hwframe "
113 "download.\n", av_get_pix_fmt_name(outlink->format));
114 return AVERROR(EINVAL);
117 outlink->w = inlink->w;
118 outlink->h = inlink->h;
123 static int hwdownload_filter_frame(AVFilterLink *link, AVFrame *input)
125 AVFilterContext *avctx = link->dst;
126 AVFilterLink *outlink = avctx->outputs[0];
127 HWDownloadContext *ctx = avctx->priv;
128 AVFrame *output = NULL;
131 if (!ctx->hwframes_ref || !input->hw_frames_ctx) {
132 av_log(ctx, AV_LOG_ERROR, "Input frames must have hardware context.\n");
133 err = AVERROR(EINVAL);
136 if ((void*)ctx->hwframes != input->hw_frames_ctx->data) {
137 av_log(ctx, AV_LOG_ERROR, "Input frame is not the in the configured "
138 "hwframe context.\n");
139 err = AVERROR(EINVAL);
143 output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
145 err = AVERROR(ENOMEM);
149 err = av_hwframe_transfer_data(output, input, 0);
151 av_log(ctx, AV_LOG_ERROR, "Failed to download frame: %d.\n", err);
155 err = av_frame_copy_props(output, input);
159 av_frame_free(&input);
161 return ff_filter_frame(avctx->outputs[0], output);
164 av_frame_free(&input);
165 av_frame_free(&output);
169 static av_cold void hwdownload_uninit(AVFilterContext *avctx)
171 HWDownloadContext *ctx = avctx->priv;
173 av_buffer_unref(&ctx->hwframes_ref);
176 static const AVClass hwdownload_class = {
177 .class_name = "hwdownload",
178 .item_name = av_default_item_name,
180 .version = LIBAVUTIL_VERSION_INT,
183 static const AVFilterPad hwdownload_inputs[] = {
186 .type = AVMEDIA_TYPE_VIDEO,
187 .config_props = hwdownload_config_input,
188 .filter_frame = hwdownload_filter_frame,
193 static const AVFilterPad hwdownload_outputs[] = {
196 .type = AVMEDIA_TYPE_VIDEO,
197 .config_props = hwdownload_config_output,
202 AVFilter ff_vf_hwdownload = {
203 .name = "hwdownload",
204 .description = NULL_IF_CONFIG_SMALL("Download a hardware frame to a normal frame"),
205 .uninit = hwdownload_uninit,
206 .query_formats = hwdownload_query_formats,
207 .priv_size = sizeof(HWDownloadContext),
208 .priv_class = &hwdownload_class,
209 .inputs = hwdownload_inputs,
210 .outputs = hwdownload_outputs,