2 * This file is part of FFmpeg.
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.
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.
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
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"
31 typedef struct HWUploadContext {
34 AVBufferRef *hwdevice_ref;
35 AVHWDeviceContext *hwdevice;
37 AVBufferRef *hwframes_ref;
38 AVHWFramesContext *hwframes;
41 static int hwupload_query_formats(AVFilterContext *avctx)
43 HWUploadContext *ctx = avctx->priv;
44 AVHWFramesConstraints *constraints = NULL;
45 const enum AVPixelFormat *input_pix_fmts, *output_pix_fmts;
46 AVFilterFormats *input_formats = NULL;
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);
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;
60 constraints = av_hwdevice_get_hwframe_constraints(ctx->hwdevice_ref, NULL);
62 err = AVERROR(EINVAL);
66 input_pix_fmts = constraints->valid_sw_formats;
67 output_pix_fmts = constraints->valid_hw_formats;
69 input_formats = ff_make_format_list(output_pix_fmts);
71 err = AVERROR(ENOMEM);
75 for (i = 0; input_pix_fmts[i] != AV_PIX_FMT_NONE; i++) {
76 err = ff_add_format(&input_formats, input_pix_fmts[i]);
82 if ((err = ff_formats_ref(input_formats, &avctx->inputs[0]->out_formats)) < 0 ||
83 (err = ff_formats_ref(ff_make_format_list(output_pix_fmts),
84 &avctx->outputs[0]->in_formats)) < 0)
87 av_hwframe_constraints_free(&constraints);
91 av_buffer_unref(&ctx->hwdevice_ref);
92 av_hwframe_constraints_free(&constraints);
96 static int hwupload_config_output(AVFilterLink *outlink)
98 AVFilterContext *avctx = outlink->src;
99 AVFilterLink *inlink = avctx->inputs[0];
100 HWUploadContext *ctx = avctx->priv;
103 av_buffer_unref(&ctx->hwframes_ref);
105 if (inlink->format == outlink->format) {
106 // The input is already a hardware format, so we just want to
107 // pass through the input frames in their own hardware context.
108 if (!inlink->hw_frames_ctx) {
109 av_log(ctx, AV_LOG_ERROR, "No input hwframe context.\n");
110 return AVERROR(EINVAL);
113 outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
114 if (!outlink->hw_frames_ctx)
115 return AVERROR(ENOMEM);
120 ctx->hwframes_ref = av_hwframe_ctx_alloc(ctx->hwdevice_ref);
121 if (!ctx->hwframes_ref)
122 return AVERROR(ENOMEM);
124 ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
126 av_log(ctx, AV_LOG_DEBUG, "Surface format is %s.\n",
127 av_get_pix_fmt_name(inlink->format));
129 ctx->hwframes->format = outlink->format;
130 ctx->hwframes->sw_format = inlink->format;
131 ctx->hwframes->width = inlink->w;
132 ctx->hwframes->height = inlink->h;
134 if (avctx->extra_hw_frames >= 0)
135 ctx->hwframes->initial_pool_size = 2 + avctx->extra_hw_frames;
137 err = av_hwframe_ctx_init(ctx->hwframes_ref);
141 outlink->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
142 if (!outlink->hw_frames_ctx) {
143 err = AVERROR(ENOMEM);
150 av_buffer_unref(&ctx->hwframes_ref);
154 static int hwupload_filter_frame(AVFilterLink *link, AVFrame *input)
156 AVFilterContext *avctx = link->dst;
157 AVFilterLink *outlink = avctx->outputs[0];
158 HWUploadContext *ctx = avctx->priv;
159 AVFrame *output = NULL;
162 if (input->format == outlink->format)
163 return ff_filter_frame(outlink, input);
165 output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
167 av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame to upload to.\n");
168 err = AVERROR(ENOMEM);
172 output->width = input->width;
173 output->height = input->height;
175 err = av_hwframe_transfer_data(output, input, 0);
177 av_log(ctx, AV_LOG_ERROR, "Failed to upload frame: %d.\n", err);
181 err = av_frame_copy_props(output, input);
185 av_frame_free(&input);
187 return ff_filter_frame(outlink, output);
190 av_frame_free(&input);
191 av_frame_free(&output);
195 static av_cold void hwupload_uninit(AVFilterContext *avctx)
197 HWUploadContext *ctx = avctx->priv;
199 av_buffer_unref(&ctx->hwframes_ref);
200 av_buffer_unref(&ctx->hwdevice_ref);
203 static const AVClass hwupload_class = {
204 .class_name = "hwupload",
205 .item_name = av_default_item_name,
207 .version = LIBAVUTIL_VERSION_INT,
210 static const AVFilterPad hwupload_inputs[] = {
213 .type = AVMEDIA_TYPE_VIDEO,
214 .filter_frame = hwupload_filter_frame,
219 static const AVFilterPad hwupload_outputs[] = {
222 .type = AVMEDIA_TYPE_VIDEO,
223 .config_props = hwupload_config_output,
228 AVFilter ff_vf_hwupload = {
230 .description = NULL_IF_CONFIG_SMALL("Upload a normal frame to a hardware frame"),
231 .uninit = hwupload_uninit,
232 .query_formats = hwupload_query_formats,
233 .priv_size = sizeof(HWUploadContext),
234 .priv_class = &hwupload_class,
235 .inputs = hwupload_inputs,
236 .outputs = hwupload_outputs,
237 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,