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;
36 AVBufferRef *hwframes_ref;
37 AVHWFramesContext *hwframes;
42 static int hwupload_query_formats(AVFilterContext *avctx)
44 HWUploadContext *ctx = avctx->priv;
45 AVHWFramesConstraints *constraints = NULL;
46 const enum AVPixelFormat *input_pix_fmts, *output_pix_fmts;
47 AVFilterFormats *input_formats = NULL;
50 if (ctx->hwdevice_ref) {
51 /* We already have a specified device. */
52 } else if (avctx->hw_device_ctx) {
53 if (ctx->device_type) {
54 err = av_hwdevice_ctx_create_derived(
56 av_hwdevice_find_type_by_name(ctx->device_type),
57 avctx->hw_device_ctx, 0);
61 ctx->hwdevice_ref = av_buffer_ref(avctx->hw_device_ctx);
62 if (!ctx->hwdevice_ref)
63 return AVERROR(ENOMEM);
66 av_log(ctx, AV_LOG_ERROR, "A hardware device reference is required "
67 "to upload frames to.\n");
68 return AVERROR(EINVAL);
71 constraints = av_hwdevice_get_hwframe_constraints(ctx->hwdevice_ref, NULL);
73 err = AVERROR(EINVAL);
77 input_pix_fmts = constraints->valid_sw_formats;
78 output_pix_fmts = constraints->valid_hw_formats;
80 input_formats = ff_make_format_list(output_pix_fmts);
82 err = AVERROR(ENOMEM);
86 for (i = 0; input_pix_fmts[i] != AV_PIX_FMT_NONE; i++) {
87 err = ff_add_format(&input_formats, input_pix_fmts[i]);
93 if ((err = ff_formats_ref(input_formats, &avctx->inputs[0]->outcfg.formats)) < 0 ||
94 (err = ff_formats_ref(ff_make_format_list(output_pix_fmts),
95 &avctx->outputs[0]->incfg.formats)) < 0)
98 av_hwframe_constraints_free(&constraints);
102 av_buffer_unref(&ctx->hwdevice_ref);
103 av_hwframe_constraints_free(&constraints);
107 static int hwupload_config_output(AVFilterLink *outlink)
109 AVFilterContext *avctx = outlink->src;
110 AVFilterLink *inlink = avctx->inputs[0];
111 HWUploadContext *ctx = avctx->priv;
114 av_buffer_unref(&ctx->hwframes_ref);
116 if (inlink->format == outlink->format) {
117 // The input is already a hardware format, so we just want to
118 // pass through the input frames in their own hardware context.
119 if (!inlink->hw_frames_ctx) {
120 av_log(ctx, AV_LOG_ERROR, "No input hwframe context.\n");
121 return AVERROR(EINVAL);
124 outlink->hw_frames_ctx = av_buffer_ref(inlink->hw_frames_ctx);
125 if (!outlink->hw_frames_ctx)
126 return AVERROR(ENOMEM);
131 ctx->hwframes_ref = av_hwframe_ctx_alloc(ctx->hwdevice_ref);
132 if (!ctx->hwframes_ref)
133 return AVERROR(ENOMEM);
135 ctx->hwframes = (AVHWFramesContext*)ctx->hwframes_ref->data;
137 av_log(ctx, AV_LOG_DEBUG, "Surface format is %s.\n",
138 av_get_pix_fmt_name(inlink->format));
140 ctx->hwframes->format = outlink->format;
141 if (inlink->hw_frames_ctx) {
142 AVHWFramesContext *in_hwframe_ctx =
143 (AVHWFramesContext*)inlink->hw_frames_ctx->data;
144 ctx->hwframes->sw_format = in_hwframe_ctx->sw_format;
146 ctx->hwframes->sw_format = inlink->format;
148 ctx->hwframes->width = inlink->w;
149 ctx->hwframes->height = inlink->h;
151 if (avctx->extra_hw_frames >= 0)
152 ctx->hwframes->initial_pool_size = 2 + avctx->extra_hw_frames;
154 err = av_hwframe_ctx_init(ctx->hwframes_ref);
158 outlink->hw_frames_ctx = av_buffer_ref(ctx->hwframes_ref);
159 if (!outlink->hw_frames_ctx) {
160 err = AVERROR(ENOMEM);
167 av_buffer_unref(&ctx->hwframes_ref);
171 static int hwupload_filter_frame(AVFilterLink *link, AVFrame *input)
173 AVFilterContext *avctx = link->dst;
174 AVFilterLink *outlink = avctx->outputs[0];
175 HWUploadContext *ctx = avctx->priv;
176 AVFrame *output = NULL;
179 if (input->format == outlink->format)
180 return ff_filter_frame(outlink, input);
182 output = ff_get_video_buffer(outlink, outlink->w, outlink->h);
184 av_log(ctx, AV_LOG_ERROR, "Failed to allocate frame to upload to.\n");
185 err = AVERROR(ENOMEM);
189 output->width = input->width;
190 output->height = input->height;
192 err = av_hwframe_transfer_data(output, input, 0);
194 av_log(ctx, AV_LOG_ERROR, "Failed to upload frame: %d.\n", err);
198 err = av_frame_copy_props(output, input);
202 av_frame_free(&input);
204 return ff_filter_frame(outlink, output);
207 av_frame_free(&input);
208 av_frame_free(&output);
212 static av_cold void hwupload_uninit(AVFilterContext *avctx)
214 HWUploadContext *ctx = avctx->priv;
216 av_buffer_unref(&ctx->hwframes_ref);
217 av_buffer_unref(&ctx->hwdevice_ref);
220 #define OFFSET(x) offsetof(HWUploadContext, x)
221 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
222 static const AVOption hwupload_options[] = {
224 "derive_device", "Derive a new device of this type",
225 OFFSET(device_type), AV_OPT_TYPE_STRING,
226 { .str = NULL }, 0, 0, FLAGS
233 AVFILTER_DEFINE_CLASS(hwupload);
235 static const AVFilterPad hwupload_inputs[] = {
238 .type = AVMEDIA_TYPE_VIDEO,
239 .filter_frame = hwupload_filter_frame,
244 static const AVFilterPad hwupload_outputs[] = {
247 .type = AVMEDIA_TYPE_VIDEO,
248 .config_props = hwupload_config_output,
253 const AVFilter ff_vf_hwupload = {
255 .description = NULL_IF_CONFIG_SMALL("Upload a normal frame to a hardware frame"),
256 .uninit = hwupload_uninit,
257 .query_formats = hwupload_query_formats,
258 .priv_size = sizeof(HWUploadContext),
259 .priv_class = &hwupload_class,
260 .inputs = hwupload_inputs,
261 .outputs = hwupload_outputs,
262 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,