2 * Copyright (c) 2023 Zhao Zhili <zhilizhao@tencent.com>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <VideoToolbox/VideoToolbox.h>
23 #include "libavutil/hwcontext.h"
24 #include "libavutil/hwcontext_videotoolbox.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixdesc.h"
28 #include "scale_eval.h"
31 typedef struct ScaleVtContext {
34 VTPixelTransferSessionRef transfer;
40 enum AVColorPrimaries colour_primaries;
41 enum AVColorTransferCharacteristic colour_transfer;
42 enum AVColorSpace colour_matrix;
43 char *colour_primaries_string;
44 char *colour_transfer_string;
45 char *colour_matrix_string;
48 static av_cold int scale_vt_init(AVFilterContext *avctx)
50 ScaleVtContext *s = avctx->priv;
54 ret = VTPixelTransferSessionCreate(kCFAllocatorDefault, &s->transfer);
56 av_log(avctx, AV_LOG_ERROR, "transfer session create failed, %d\n", ret);
57 return AVERROR_EXTERNAL;
60 #define STRING_OPTION(var_name, func_name, default_value) \
62 if (s->var_name##_string) { \
63 int var = av_##func_name##_from_name(s->var_name##_string); \
65 av_log(avctx, AV_LOG_ERROR, "Invalid %s.\n", #var_name); \
66 return AVERROR(EINVAL); \
70 s->var_name = default_value; \
74 STRING_OPTION(colour_primaries, color_primaries, AVCOL_PRI_UNSPECIFIED);
75 STRING_OPTION(colour_transfer, color_transfer, AVCOL_TRC_UNSPECIFIED);
76 STRING_OPTION(colour_matrix, color_space, AVCOL_SPC_UNSPECIFIED);
78 if (s->colour_primaries != AVCOL_PRI_UNSPECIFIED) {
79 value = av_map_videotoolbox_color_primaries_from_av(s->colour_primaries);
81 av_log(avctx, AV_LOG_ERROR,
82 "Doesn't support converting to colour primaries %s\n",
83 s->colour_primaries_string);
84 return AVERROR(ENOTSUP);
86 VTSessionSetProperty(s->transfer, kVTPixelTransferPropertyKey_DestinationColorPrimaries, value);
89 if (s->colour_transfer != AVCOL_TRC_UNSPECIFIED) {
90 value = av_map_videotoolbox_color_trc_from_av(s->colour_transfer);
92 av_log(avctx, AV_LOG_ERROR,
93 "Doesn't support converting to trc %s\n",
94 s->colour_transfer_string);
95 return AVERROR(ENOTSUP);
97 VTSessionSetProperty(s->transfer, kVTPixelTransferPropertyKey_DestinationTransferFunction, value);
100 if (s->colour_matrix != AVCOL_SPC_UNSPECIFIED) {
101 value = av_map_videotoolbox_color_matrix_from_av(s->colour_matrix);
103 av_log(avctx, AV_LOG_ERROR,
104 "Doesn't support converting to colorspace %s\n",
105 s->colour_matrix_string);
106 return AVERROR(ENOTSUP);
108 VTSessionSetProperty(s->transfer, kVTPixelTransferPropertyKey_DestinationYCbCrMatrix, value);
114 static av_cold void scale_vt_uninit(AVFilterContext *avctx)
116 ScaleVtContext *s = avctx->priv;
119 VTPixelTransferSessionInvalidate(s->transfer);
120 CFRelease(s->transfer);
125 static int scale_vt_filter_frame(AVFilterLink *link, AVFrame *in)
128 AVFilterContext *ctx = link->dst;
129 ScaleVtContext *s = ctx->priv;
130 AVFilterLink *outlink = ctx->outputs[0];
131 CVPixelBufferRef src;
132 CVPixelBufferRef dst;
134 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
136 ret = AVERROR(ENOMEM);
140 ret = av_frame_copy_props(out, in);
144 av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
145 (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
146 (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
148 if (s->colour_primaries != AVCOL_PRI_UNSPECIFIED)
149 out->color_primaries = s->colour_primaries;
150 if (s->colour_transfer != AVCOL_TRC_UNSPECIFIED)
151 out->color_trc = s->colour_transfer;
152 if (s->colour_matrix != AVCOL_SPC_UNSPECIFIED)
153 out->colorspace = s->colour_matrix;
155 src = (CVPixelBufferRef)in->data[3];
156 dst = (CVPixelBufferRef)out->data[3];
157 ret = VTPixelTransferSessionTransferImage(s->transfer, src, dst);
159 av_log(ctx, AV_LOG_ERROR, "transfer image failed, %d\n", ret);
160 ret = AVERROR_EXTERNAL;
166 return ff_filter_frame(outlink, out);
174 static int scale_vt_config_output(AVFilterLink *outlink)
177 AVFilterContext *avctx = outlink->src;
178 ScaleVtContext *s = avctx->priv;
179 AVFilterLink *inlink = outlink->src->inputs[0];
180 AVHWFramesContext *hw_frame_ctx_in;
181 AVHWFramesContext *hw_frame_ctx_out;
183 err = ff_scale_eval_dimensions(s, s->w_expr, s->h_expr, inlink, outlink,
189 outlink->w = s->output_width;
190 outlink->h = s->output_height;
192 if (inlink->sample_aspect_ratio.num) {
193 AVRational r = {outlink->h * inlink->w, outlink->w * inlink->h};
194 outlink->sample_aspect_ratio = av_mul_q(r, inlink->sample_aspect_ratio);
196 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
199 hw_frame_ctx_in = (AVHWFramesContext *)inlink->hw_frames_ctx->data;
201 av_buffer_unref(&outlink->hw_frames_ctx);
202 outlink->hw_frames_ctx = av_hwframe_ctx_alloc(hw_frame_ctx_in->device_ref);
203 hw_frame_ctx_out = (AVHWFramesContext *)outlink->hw_frames_ctx->data;
204 hw_frame_ctx_out->format = AV_PIX_FMT_VIDEOTOOLBOX;
205 hw_frame_ctx_out->sw_format = hw_frame_ctx_in->sw_format;
206 hw_frame_ctx_out->width = outlink->w;
207 hw_frame_ctx_out->height = outlink->h;
209 err = ff_filter_init_hw_frames(avctx, outlink, 1);
213 err = av_hwframe_ctx_init(outlink->hw_frames_ctx);
215 av_log(avctx, AV_LOG_ERROR,
216 "Failed to init videotoolbox frame context, %s\n",
224 #define OFFSET(x) offsetof(ScaleVtContext, x)
225 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
226 static const AVOption scale_vt_options[] = {
227 { "w", "Output video width",
228 OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, .flags = FLAGS },
229 { "h", "Output video height",
230 OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, .flags = FLAGS },
231 { "color_matrix", "Output colour matrix coefficient set",
232 OFFSET(colour_matrix_string), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS },
233 { "color_primaries", "Output colour primaries",
234 OFFSET(colour_primaries_string), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS },
235 { "color_transfer", "Output colour transfer characteristics",
236 OFFSET(colour_transfer_string), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS },
240 AVFILTER_DEFINE_CLASS(scale_vt);
242 static const AVFilterPad scale_vt_inputs[] = {
245 .type = AVMEDIA_TYPE_VIDEO,
246 .filter_frame = &scale_vt_filter_frame,
250 static const AVFilterPad scale_vt_outputs[] = {
253 .type = AVMEDIA_TYPE_VIDEO,
254 .config_props = &scale_vt_config_output,
258 const AVFilter ff_vf_scale_vt = {
260 .description = NULL_IF_CONFIG_SMALL("Scale Videotoolbox frames"),
261 .priv_size = sizeof(ScaleVtContext),
262 .init = scale_vt_init,
263 .uninit = scale_vt_uninit,
264 FILTER_INPUTS(scale_vt_inputs),
265 FILTER_OUTPUTS(scale_vt_outputs),
266 FILTER_SINGLE_PIXFMT(AV_PIX_FMT_VIDEOTOOLBOX),
267 .priv_class = &scale_vt_class,
268 .flags = AVFILTER_FLAG_HWDEVICE,
269 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,