]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_hwdownload.c
Merge commit '8d0cc8ca97678f4ca87948ebabcbaab5a4f4c1f6'
[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     ff_formats_ref(infmts,  &avctx->inputs[0]->out_formats);
60     ff_formats_ref(outfmts, &avctx->outputs[0]->in_formats);
61     return 0;
62 }
63
64 static int hwdownload_config_input(AVFilterLink *inlink)
65 {
66     AVFilterContext *avctx = inlink->dst;
67     HWDownloadContext *ctx = avctx->priv;
68
69     av_buffer_unref(&ctx->hwframes_ref);
70
71     if (!inlink->hw_frames_ctx) {
72         av_log(ctx, AV_LOG_ERROR, "The input must have a hardware frame "
73                "reference.\n");
74         return AVERROR(EINVAL);
75     }
76
77     ctx->hwframes_ref = av_buffer_ref(inlink->hw_frames_ctx);
78     if (!ctx->hwframes_ref)
79         return AVERROR(ENOMEM);
80
81     ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
82
83     return 0;
84 }
85
86 static int hwdownload_config_output(AVFilterLink *outlink)
87 {
88     AVFilterContext *avctx = outlink->src;
89     AVFilterLink *inlink   = avctx->inputs[0];
90     HWDownloadContext *ctx = avctx->priv;
91     enum AVPixelFormat *formats;
92     int err, i, found;
93
94     if (!ctx->hwframes_ref)
95         return AVERROR(EINVAL);
96
97     err = av_hwframe_transfer_get_formats(ctx->hwframes_ref,
98                                           AV_HWFRAME_TRANSFER_DIRECTION_FROM,
99                                           &formats, 0);
100     if (err < 0)
101         return err;
102
103     found = 0;
104     for (i = 0; formats[i] != AV_PIX_FMT_NONE; i++) {
105         if (formats[i] == outlink->format) {
106             found = 1;
107             break;
108         }
109     }
110     av_freep(&formats);
111
112     if (!found) {
113         av_log(ctx, AV_LOG_ERROR, "Invalid output format %s for hwframe "
114                "download.\n", av_get_pix_fmt_name(outlink->format));
115         return AVERROR(EINVAL);
116     }
117
118     outlink->w = inlink->w;
119     outlink->h = inlink->h;
120
121     return 0;
122 }
123
124 static int hwdownload_filter_frame(AVFilterLink *link, AVFrame *input)
125 {
126     AVFilterContext *avctx = link->dst;
127     AVFilterLink  *outlink = avctx->outputs[0];
128     HWDownloadContext *ctx = avctx->priv;
129     AVFrame *output = NULL;
130     int err;
131
132     if (!ctx->hwframes_ref || !input->hw_frames_ctx) {
133         av_log(ctx, AV_LOG_ERROR, "Input frames must have hardware context.\n");
134         err = AVERROR(EINVAL);
135         goto fail;
136     }
137     if ((void*)ctx->hwframes != input->hw_frames_ctx->data) {
138         av_log(ctx, AV_LOG_ERROR, "Input frame is not the in the configured "
139                "hwframe context.\n");
140         err = AVERROR(EINVAL);
141         goto fail;
142     }
143
144     output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
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     err = av_frame_copy_props(output, input);
157     if (err < 0)
158         goto fail;
159
160     av_frame_free(&input);
161
162     return ff_filter_frame(avctx->outputs[0], output);
163
164 fail:
165     av_frame_free(&input);
166     av_frame_free(&output);
167     return err;
168 }
169
170 static av_cold void hwdownload_uninit(AVFilterContext *avctx)
171 {
172     HWDownloadContext *ctx = avctx->priv;
173
174     av_buffer_unref(&ctx->hwframes_ref);
175 }
176
177 static const AVClass hwdownload_class = {
178     .class_name = "hwdownload",
179     .item_name  = av_default_item_name,
180     .option     = NULL,
181     .version    = LIBAVUTIL_VERSION_INT,
182 };
183
184 static const AVFilterPad hwdownload_inputs[] = {
185     {
186         .name         = "default",
187         .type         = AVMEDIA_TYPE_VIDEO,
188         .config_props = hwdownload_config_input,
189         .filter_frame = hwdownload_filter_frame,
190     },
191     { NULL }
192 };
193
194 static const AVFilterPad hwdownload_outputs[] = {
195     {
196         .name         = "default",
197         .type         = AVMEDIA_TYPE_VIDEO,
198         .config_props = hwdownload_config_output,
199     },
200     { NULL }
201 };
202
203 AVFilter ff_vf_hwdownload = {
204     .name          = "hwdownload",
205     .description   = NULL_IF_CONFIG_SMALL("Download a hardware frame to a normal frame"),
206     .uninit        = hwdownload_uninit,
207     .query_formats = hwdownload_query_formats,
208     .priv_size     = sizeof(HWDownloadContext),
209     .priv_class    = &hwdownload_class,
210     .inputs        = hwdownload_inputs,
211     .outputs       = hwdownload_outputs,
212 };