]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_hwdownload.c
vf_deinterlace_vaapi: Create filter buffer after context
[ffmpeg] / libavfilter / vf_hwdownload.c
1 /*
2  * This file is part of Libav.
3  *
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.
8  *
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.
13  *
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
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     AVFilterFormats  *infmts = NULL;
41     AVFilterFormats *outfmts = NULL;
42     const AVPixFmtDescriptor *desc;
43     int err;
44
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));
49         else
50             err = ff_add_format(&outfmts, av_pix_fmt_desc_get_id(desc));
51         if (err) {
52             ff_formats_unref(&infmts);
53             ff_formats_unref(&outfmts);
54             return err;
55         }
56     }
57
58     ff_formats_ref(infmts,  &avctx->inputs[0]->out_formats);
59     ff_formats_ref(outfmts, &avctx->outputs[0]->in_formats);
60     return 0;
61 }
62
63 static int hwdownload_config_input(AVFilterLink *inlink)
64 {
65     AVFilterContext *avctx = inlink->dst;
66     HWDownloadContext *ctx = avctx->priv;
67
68     av_buffer_unref(&ctx->hwframes_ref);
69
70     if (!inlink->hw_frames_ctx) {
71         av_log(ctx, AV_LOG_ERROR, "The input must have a hardware frame "
72                "reference.\n");
73         return AVERROR(EINVAL);
74     }
75
76     ctx->hwframes_ref = av_buffer_ref(inlink->hw_frames_ctx);
77     if (!ctx->hwframes_ref)
78         return AVERROR(ENOMEM);
79
80     ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
81
82     return 0;
83 }
84
85 static int hwdownload_config_output(AVFilterLink *outlink)
86 {
87     AVFilterContext *avctx = outlink->src;
88     AVFilterLink *inlink   = avctx->inputs[0];
89     HWDownloadContext *ctx = avctx->priv;
90     enum AVPixelFormat *formats;
91     int err, i, found;
92
93     if (!ctx->hwframes_ref)
94         return AVERROR(EINVAL);
95
96     err = av_hwframe_transfer_get_formats(ctx->hwframes_ref,
97                                           AV_HWFRAME_TRANSFER_DIRECTION_FROM,
98                                           &formats, 0);
99     if (err < 0)
100         return err;
101
102     found = 0;
103     for (i = 0; formats[i] != AV_PIX_FMT_NONE; i++) {
104         if (formats[i] == outlink->format) {
105             found = 1;
106             break;
107         }
108     }
109     av_freep(&formats);
110
111     if (!found) {
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);
115     }
116
117     outlink->w = inlink->w;
118     outlink->h = inlink->h;
119
120     return 0;
121 }
122
123 static int hwdownload_filter_frame(AVFilterLink *link, AVFrame *input)
124 {
125     AVFilterContext *avctx = link->dst;
126     AVFilterLink  *outlink = avctx->outputs[0];
127     HWDownloadContext *ctx = avctx->priv;
128     AVFrame *output = NULL;
129     int err;
130
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);
134         goto fail;
135     }
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);
140         goto fail;
141     }
142
143     output = ff_get_video_buffer(outlink, ctx->hwframes->width,
144                                  ctx->hwframes->height);
145     if (!output) {
146         err = AVERROR(ENOMEM);
147         goto fail;
148     }
149
150     err = av_hwframe_transfer_data(output, input, 0);
151     if (err < 0) {
152         av_log(ctx, AV_LOG_ERROR, "Failed to download frame: %d.\n", err);
153         goto fail;
154     }
155
156     output->width  = outlink->w;
157     output->height = outlink->h;
158
159     err = av_frame_copy_props(output, input);
160     if (err < 0)
161         goto fail;
162
163     av_frame_free(&input);
164
165     return ff_filter_frame(avctx->outputs[0], output);
166
167 fail:
168     av_frame_free(&input);
169     av_frame_free(&output);
170     return err;
171 }
172
173 static av_cold void hwdownload_uninit(AVFilterContext *avctx)
174 {
175     HWDownloadContext *ctx = avctx->priv;
176
177     av_buffer_unref(&ctx->hwframes_ref);
178 }
179
180 static const AVClass hwdownload_class = {
181     .class_name = "hwdownload",
182     .item_name  = av_default_item_name,
183     .option     = NULL,
184     .version    = LIBAVUTIL_VERSION_INT,
185 };
186
187 static const AVFilterPad hwdownload_inputs[] = {
188     {
189         .name         = "default",
190         .type         = AVMEDIA_TYPE_VIDEO,
191         .config_props = hwdownload_config_input,
192         .filter_frame = hwdownload_filter_frame,
193     },
194     { NULL }
195 };
196
197 static const AVFilterPad hwdownload_outputs[] = {
198     {
199         .name         = "default",
200         .type         = AVMEDIA_TYPE_VIDEO,
201         .config_props = hwdownload_config_output,
202     },
203     { NULL }
204 };
205
206 AVFilter ff_vf_hwdownload = {
207     .name          = "hwdownload",
208     .description   = NULL_IF_CONFIG_SMALL("Download a hardware frame to a normal frame"),
209     .uninit        = hwdownload_uninit,
210     .query_formats = hwdownload_query_formats,
211     .priv_size     = sizeof(HWDownloadContext),
212     .priv_class    = &hwdownload_class,
213     .inputs        = hwdownload_inputs,
214     .outputs       = hwdownload_outputs,
215     .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
216 };