1 /*
2 * Copyright (c) 2023 Zhao Zhili <zhilizhao@tencent.com>
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <VideoToolbox/VideoToolbox.h>
22
30
33
39
47
49 {
53
54 ret = VTPixelTransferSessionCreate(kCFAllocatorDefault, &
s->transfer);
58 }
59
60 #define STRING_OPTION(var_name, func_name, default_value) \
61 do { \
62 if (s->var_name##_string) { \
63 int var = av_##func_name##_from_name(s->var_name##_string); \
64 if (var < 0) { \
65 av_log(avctx, AV_LOG_ERROR, "Invalid %s.\n", #var_name); \
66 return AVERROR(EINVAL); \
67 } \
68 s->var_name = var; \
69 } else { \
70 s->var_name = default_value; \
71 } \
72 } while (0)
73
77
82 "Doesn't support converting to colour primaries %s\n",
83 s->colour_primaries_string);
85 }
86 VTSessionSetProperty(
s->transfer, kVTPixelTransferPropertyKey_DestinationColorPrimaries,
value);
87 }
88
93 "Doesn't support converting to trc %s\n",
94 s->colour_transfer_string);
96 }
97 VTSessionSetProperty(
s->transfer, kVTPixelTransferPropertyKey_DestinationTransferFunction,
value);
98 }
99
104 "Doesn't support converting to colorspace %s\n",
105 s->colour_matrix_string);
107 }
108 VTSessionSetProperty(
s->transfer, kVTPixelTransferPropertyKey_DestinationYCbCrMatrix,
value);
109 }
110
111 return 0;
112 }
113
115 {
117
119 VTPixelTransferSessionInvalidate(
s->transfer);
120 CFRelease(
s->transfer);
122 }
123 }
124
126 {
131 CVPixelBufferRef
src;
132 CVPixelBufferRef dst;
133
138 }
139
143
144 av_reduce(&
out->sample_aspect_ratio.num, &
out->sample_aspect_ratio.den,
147 INT_MAX);
149 out->color_primaries =
s->colour_primaries;
151 out->color_trc =
s->colour_transfer;
153 out->colorspace =
s->colour_matrix;
154
155 src = (CVPixelBufferRef)in->
data[3];
156 dst = (CVPixelBufferRef)
out->data[3];
157 ret = VTPixelTransferSessionTransferImage(
s->transfer,
src, dst);
162 }
163
165
167
172 }
173
175 {
176 int err;
182
186 if (err < 0)
187 return err;
188
189 outlink->
w =
s->output_width;
190 outlink->
h =
s->output_height;
191
192 if (
inlink->sample_aspect_ratio.num) {
195 } else {
197 }
198
200
206 hw_frame_ctx_out->
width = outlink->
w;
207 hw_frame_ctx_out->
height = outlink->
h;
208
210 if (err < 0)
211 return err;
212
214 if (err < 0) {
216 "Failed to init videotoolbox frame context, %s\n",
218 return err;
219 }
220
221 return 0;
222 }
223
224 #define OFFSET(x) offsetof(ScaleVtContext, x)
225 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
227 { "w", "Output video width",
229 { "h", "Output video height",
231 { "color_matrix", "Output colour matrix coefficient set",
233 { "color_primaries", "Output colour primaries",
235 { "color_transfer", "Output colour transfer characteristics",
238 };
239
241
243 {
247 },
248 };
249
251 {
255 },
256 };
257
267 .priv_class = &scale_vt_class,
270 };