]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_hwdownload.c
Merge commit '0ba471d7d864c712f45d7ac6aca4829aba025adc'
[ffmpeg] / libavfilter / vf_hwdownload.c
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18
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"
26
27 #include "avfilter.h"
28 #include "formats.h"
29 #include "internal.h"
30 #include "video.h"
31
32 typedef struct HWDownloadContext {
33     const AVClass *class;
34
35     AVBufferRef       *hwframes_ref;
36     AVHWFramesContext *hwframes;
37 } HWDownloadContext;
38
39 static int hwdownload_query_formats(AVFilterContext *avctx)
40 {
41     AVFilterFormats  *infmts = NULL;
42     AVFilterFormats *outfmts = NULL;
43     const AVPixFmtDescriptor *desc;
44     int err;
45
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));
50         else
51             err = ff_add_format(&outfmts, av_pix_fmt_desc_get_id(desc));
52         if (err) {
53             ff_formats_unref(&infmts);
54             ff_formats_unref(&outfmts);
55             return err;
56         }
57     }
58
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)
61         return err;
62
63     return 0;
64 }
65
66 static int hwdownload_config_input(AVFilterLink *inlink)
67 {
68     AVFilterContext *avctx = inlink->dst;
69     HWDownloadContext *ctx = avctx->priv;
70
71     av_buffer_unref(&ctx->hwframes_ref);
72
73     if (!inlink->hw_frames_ctx) {
74         av_log(ctx, AV_LOG_ERROR, "The input must have a hardware frame "
75                "reference.\n");
76         return AVERROR(EINVAL);
77     }
78
79     ctx->hwframes_ref = av_buffer_ref(inlink->hw_frames_ctx);
80     if (!ctx->hwframes_ref)
81         return AVERROR(ENOMEM);
82
83     ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
84
85     return 0;
86 }
87
88 static int hwdownload_config_output(AVFilterLink *outlink)
89 {
90     AVFilterContext *avctx = outlink->src;
91     AVFilterLink *inlink   = avctx->inputs[0];
92     HWDownloadContext *ctx = avctx->priv;
93     enum AVPixelFormat *formats;
94     int err, i, found;
95
96     if (!ctx->hwframes_ref)
97         return AVERROR(EINVAL);
98
99     err = av_hwframe_transfer_get_formats(ctx->hwframes_ref,
100                                           AV_HWFRAME_TRANSFER_DIRECTION_FROM,
101                                           &formats, 0);
102     if (err < 0)
103         return err;
104
105     found = 0;
106     for (i = 0; formats[i] != AV_PIX_FMT_NONE; i++) {
107         if (formats[i] == outlink->format) {
108             found = 1;
109             break;
110         }
111     }
112     av_freep(&formats);
113
114     if (!found) {
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);
118     }
119
120     outlink->w = inlink->w;
121     outlink->h = inlink->h;
122
123     return 0;
124 }
125
126 static int hwdownload_filter_frame(AVFilterLink *link, AVFrame *input)
127 {
128     AVFilterContext *avctx = link->dst;
129     AVFilterLink  *outlink = avctx->outputs[0];
130     HWDownloadContext *ctx = avctx->priv;
131     AVFrame *output = NULL;
132     int err;
133
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);
137         goto fail;
138     }
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);
143         goto fail;
144     }
145
146     output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
147     if (!output) {
148         err = AVERROR(ENOMEM);
149         goto fail;
150     }
151
152     err = av_hwframe_transfer_data(output, input, 0);
153     if (err < 0) {
154         av_log(ctx, AV_LOG_ERROR, "Failed to download frame: %d.\n", err);
155         goto fail;
156     }
157
158     err = av_frame_copy_props(output, input);
159     if (err < 0)
160         goto fail;
161
162     av_frame_free(&input);
163
164     return ff_filter_frame(avctx->outputs[0], output);
165
166 fail:
167     av_frame_free(&input);
168     av_frame_free(&output);
169     return err;
170 }
171
172 static av_cold void hwdownload_uninit(AVFilterContext *avctx)
173 {
174     HWDownloadContext *ctx = avctx->priv;
175
176     av_buffer_unref(&ctx->hwframes_ref);
177 }
178
179 static const AVClass hwdownload_class = {
180     .class_name = "hwdownload",
181     .item_name  = av_default_item_name,
182     .option     = NULL,
183     .version    = LIBAVUTIL_VERSION_INT,
184 };
185
186 static const AVFilterPad hwdownload_inputs[] = {
187     {
188         .name         = "default",
189         .type         = AVMEDIA_TYPE_VIDEO,
190         .config_props = hwdownload_config_input,
191         .filter_frame = hwdownload_filter_frame,
192     },
193     { NULL }
194 };
195
196 static const AVFilterPad hwdownload_outputs[] = {
197     {
198         .name         = "default",
199         .type         = AVMEDIA_TYPE_VIDEO,
200         .config_props = hwdownload_config_output,
201     },
202     { NULL }
203 };
204
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,
214 };