lavfi: Use ff_get_video_buffer in all filters using hwframes
[libav.git] / libavfilter / vf_hwupload.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/hwcontext_internal.h"
22 #include "libavutil/log.h"
23 #include "libavutil/pixdesc.h"
24 #include "libavutil/opt.h"
25
26 #include "avfilter.h"
27 #include "formats.h"
28 #include "internal.h"
29 #include "video.h"
30
31 typedef struct HWUploadContext {
32 const AVClass *class;
33
34 AVBufferRef *hwdevice_ref;
35 AVHWDeviceContext *hwdevice;
36
37 AVBufferRef *hwframes_ref;
38 AVHWFramesContext *hwframes;
39 } HWUploadContext;
40
41 static int hwupload_query_formats(AVFilterContext *avctx)
42 {
43 HWUploadContext *ctx = avctx->priv;
44 AVHWFramesConstraints *constraints = NULL;
45 const enum AVPixelFormat *input_pix_fmts, *output_pix_fmts;
46 AVFilterFormats *input_formats = NULL;
47 int err, i;
48
49 if (!avctx->hw_device_ctx) {
50 av_log(ctx, AV_LOG_ERROR, "A hardware device reference is required "
51 "to upload frames to.\n");
52 return AVERROR(EINVAL);
53 }
54
55 ctx->hwdevice_ref = av_buffer_ref(avctx->hw_device_ctx);
56 if (!ctx->hwdevice_ref)
57 return AVERROR(ENOMEM);
58 ctx->hwdevice = (AVHWDeviceContext*)ctx->hwdevice_ref->data;
59
60 constraints = av_hwdevice_get_hwframe_constraints(ctx->hwdevice_ref, NULL);
61 if (!constraints) {
62 err = AVERROR(EINVAL);
63 goto fail;
64 }
65
66 input_pix_fmts = constraints->valid_sw_formats;
67 output_pix_fmts = constraints->valid_hw_formats;
68
69 input_formats = ff_make_format_list(output_pix_fmts);
70 if (!input_formats) {
71 err = AVERROR(ENOMEM);
72 goto fail;
73 }
74 if (input_pix_fmts) {
75 for (i = 0; input_pix_fmts[i] != AV_PIX_FMT_NONE; i++) {
76 err = ff_add_format(&input_formats, input_pix_fmts[i]);
77 if (err < 0) {
78 ff_formats_unref(&input_formats);
79 goto fail;
80 }
81 }
82 }
83
84 ff_formats_ref(input_formats, &avctx->inputs[0]->out_formats);
85
86 ff_formats_ref(ff_make_format_list(output_pix_fmts),
87 &avctx->outputs[0]->in_formats);
88
89 av_hwframe_constraints_free(&constraints);
90 return 0;
91
92 fail:
93 av_buffer_unref(&ctx->hwdevice_ref);
94 av_hwframe_constraints_free(&constraints);
95 return err;
96 }
97
98 static int hwupload_config_output(AVFilterLink *outlink)
99 {
100 AVFilterContext *avctx = outlink->src;
101 AVFilterLink *inlink = avctx->inputs[0];
102 HWUploadContext *ctx = avctx->priv;
103 int err;
104
105 av_buffer_unref(&ctx->hwframes_ref);
106
107 if (inlink->format == outlink->format) {
108 // The input is already a hardware format, so we just want to
109 // pass through the input frames in their own hardware context.
110 if (!inlink->hw_frames_ctx) {
111 av_log(ctx, AV_LOG_ERROR, "No input hwframe context.\n");
112 return AVERROR(EINVAL);
113 }
114
115 outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
116 if (!outlink->hw_frames_ctx)
117 return AVERROR(ENOMEM);
118
119 return 0;
120 }
121
122 ctx->hwframes_ref = av_hwframe_ctx_alloc(ctx->hwdevice_ref);
123 if (!ctx->hwframes_ref)
124 return AVERROR(ENOMEM);
125
126 ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
127
128 av_log(ctx, AV_LOG_DEBUG, "Surface format is %s.\n",
129 av_get_pix_fmt_name(inlink->format));
130
131 ctx->hwframes->format = outlink->format;
132 ctx->hwframes->sw_format = inlink->format;
133 ctx->hwframes->width = inlink->w;
134 ctx->hwframes->height = inlink->h;
135
136 err = av_hwframe_ctx_init(ctx->hwframes_ref);
137 if (err < 0)
138 goto fail;
139
140 outlink->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
141 if (!outlink->hw_frames_ctx) {
142 err = AVERROR(ENOMEM);
143 goto fail;
144 }
145
146 return 0;
147
148 fail:
149 av_buffer_unref(&ctx->hwframes_ref);
150 return err;
151 }
152
153 static int hwupload_filter_frame(AVFilterLink *link, AVFrame *input)
154 {
155 AVFilterContext *avctx = link->dst;
156 AVFilterLink *outlink = avctx->outputs[0];
157 HWUploadContext *ctx = avctx->priv;
158 AVFrame *output = NULL;
159 int err;
160
161 if (input->format == outlink->format)
162 return ff_filter_frame(outlink, input);
163
164 output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
165 if (!output) {
166 av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame to upload to.\n");
167 err = AVERROR(ENOMEM);
168 goto fail;
169 }
170
171 output->width = input->width;
172 output->height = input->height;
173
174 err = av_hwframe_transfer_data(output, input, 0);
175 if (err < 0) {
176 av_log(ctx, AV_LOG_ERROR, "Failed to upload frame: %d.\n", err);
177 goto fail;
178 }
179
180 err = av_frame_copy_props(output, input);
181 if (err < 0)
182 goto fail;
183
184 av_frame_free(&input);
185
186 return ff_filter_frame(outlink, output);
187
188 fail:
189 av_frame_free(&input);
190 av_frame_free(&output);
191 return err;
192 }
193
194 static av_cold void hwupload_uninit(AVFilterContext *avctx)
195 {
196 HWUploadContext *ctx = avctx->priv;
197
198 av_buffer_unref(&ctx->hwframes_ref);
199 av_buffer_unref(&ctx->hwdevice_ref);
200 }
201
202 static const AVClass hwupload_class = {
203 .class_name = "hwupload",
204 .item_name = av_default_item_name,
205 .option = NULL,
206 .version = LIBAVUTIL_VERSION_INT,
207 };
208
209 static const AVFilterPad hwupload_inputs[] = {
210 {
211 .name = "default",
212 .type = AVMEDIA_TYPE_VIDEO,
213 .filter_frame = hwupload_filter_frame,
214 },
215 { NULL }
216 };
217
218 static const AVFilterPad hwupload_outputs[] = {
219 {
220 .name = "default",
221 .type = AVMEDIA_TYPE_VIDEO,
222 .config_props = hwupload_config_output,
223 },
224 { NULL }
225 };
226
227 AVFilter ff_vf_hwupload = {
228 .name = "hwupload",
229 .description = NULL_IF_CONFIG_SMALL("Upload a normal frame to a hardware frame"),
230 .uninit = hwupload_uninit,
231 .query_formats = hwupload_query_formats,
232 .priv_size = sizeof(HWUploadContext),
233 .priv_class = &hwupload_class,
234 .inputs = hwupload_inputs,
235 .outputs = hwupload_outputs,
236 };