FFmpeg
Loading...
Searching...
No Matches
vf_scale_vt.c
Go to the documentation of this file.
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
23#include "libavutil/hwcontext.h"
25#include "libavutil/opt.h"
26#include "libavutil/pixdesc.h"
27
28#include "filters.h"
29#include "scale_eval.h"
30#include "video.h"
31
48
50{
51 ScaleVtContext *s = avctx->priv;
52 int ret;
53 CFStringRef value;
54
55 ret = VTPixelTransferSessionCreate(kCFAllocatorDefault, &s->transfer);
56 if (ret != noErr) {
57 av_log(avctx, AV_LOG_ERROR, "transfer session create failed, %d\n", ret);
58 return AVERROR_EXTERNAL;
59 }
60
61#define STRING_OPTION(var_name, func_name, default_value) \
62 do { \
63 if (s->var_name##_string) { \
64 int var = av_##func_name##_from_name(s->var_name##_string); \
65 if (var < 0) { \
66 av_log(avctx, AV_LOG_ERROR, "Invalid %s.\n", #var_name); \
67 return AVERROR(EINVAL); \
68 } \
69 s->var_name = var; \
70 } else { \
71 s->var_name = default_value; \
72 } \
73 } while (0)
74
76 STRING_OPTION(colour_transfer, color_transfer, AVCOL_TRC_UNSPECIFIED);
77 STRING_OPTION(colour_matrix, color_space, AVCOL_SPC_UNSPECIFIED);
78
79 if (s->colour_primaries != AVCOL_PRI_UNSPECIFIED) {
81 if (!value) {
82 av_log(avctx, AV_LOG_ERROR,
83 "Doesn't support converting to colour primaries %s\n",
84 s->colour_primaries_string);
85 return AVERROR(ENOTSUP);
86 }
87 VTSessionSetProperty(s->transfer, kVTPixelTransferPropertyKey_DestinationColorPrimaries, value);
88 }
89
90 if (s->colour_transfer != AVCOL_TRC_UNSPECIFIED) {
92 if (!value) {
93 av_log(avctx, AV_LOG_ERROR,
94 "Doesn't support converting to trc %s\n",
95 s->colour_transfer_string);
96 return AVERROR(ENOTSUP);
97 }
98 VTSessionSetProperty(s->transfer, kVTPixelTransferPropertyKey_DestinationTransferFunction, value);
99 }
100
101 if (s->colour_matrix != AVCOL_SPC_UNSPECIFIED) {
103 if (!value) {
104 av_log(avctx, AV_LOG_ERROR,
105 "Doesn't support converting to colorspace %s\n",
106 s->colour_matrix_string);
107 return AVERROR(ENOTSUP);
108 }
109 VTSessionSetProperty(s->transfer, kVTPixelTransferPropertyKey_DestinationYCbCrMatrix, value);
110 }
111
112 VTSessionSetProperty(s->transfer, kVTPixelTransferPropertyKey_ScalingMode, kVTScalingMode_CropSourceToCleanAperture);
113
114 return 0;
115}
116
118{
119 ScaleVtContext *s = avctx->priv;
120
121 if (s->transfer) {
122 VTPixelTransferSessionInvalidate(s->transfer);
123 CFRelease(s->transfer);
124 s->transfer = NULL;
125 }
126}
127
129{
130 int ret;
131 AVFilterContext *ctx = link->dst;
132 ScaleVtContext *s = ctx->priv;
133 AVFilterLink *outlink = ctx->outputs[0];
134 CVPixelBufferRef src;
135 CVPixelBufferRef dst;
136
137 int left;
138 int top;
139 int width;
140 int height;
141 CFNumberRef crop_width_num;
142 CFNumberRef crop_height_num;
143 CFNumberRef crop_offset_left_num;
144 CFNumberRef crop_offset_top_num;
145 const void *clean_aperture_keys[4];
146 const void *source_clean_aperture_values[4];
147 CFDictionaryRef source_clean_aperture;
148
149 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
150 if (!out) {
151 ret = AVERROR(ENOMEM);
152 goto fail;
153 }
154
155 ret = av_frame_copy_props(out, in);
156 if (ret < 0)
157 goto fail;
158
159 out->crop_left = 0;
160 out->crop_top = 0;
161 out->crop_right = 0;
162 out->crop_bottom = 0;
163 if (out->width != in->width || out->height != in->height) {
164 av_frame_side_data_remove_by_props(&out->side_data, &out->nb_side_data,
166 }
167
168 av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
169 (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
170 (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
171 INT_MAX);
172 if (s->colour_primaries != AVCOL_PRI_UNSPECIFIED)
173 out->color_primaries = s->colour_primaries;
174 if (s->colour_transfer != AVCOL_TRC_UNSPECIFIED)
175 out->color_trc = s->colour_transfer;
176 if (s->colour_matrix != AVCOL_SPC_UNSPECIFIED)
177 out->colorspace = s->colour_matrix;
178
179 width = (in->width - in->crop_right) - in->crop_left;
180 height = (in->height - in->crop_bottom) - in->crop_top;
181 // The crop offsets are relative to the center of the frame.
182 // the crop width and crop height are relative to the center of the crop rect, not top left as normal.
183 left = in->crop_left - in->width / 2 + width / 2;
184 top = in->crop_top - in->height / 2 + height / 2;
185 crop_width_num = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &width);
186 crop_height_num = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &height);
187 crop_offset_left_num = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &left);
188 crop_offset_top_num = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &top);
189
190 clean_aperture_keys[0] = kCVImageBufferCleanApertureWidthKey;
191 clean_aperture_keys[1] = kCVImageBufferCleanApertureHeightKey;
192 clean_aperture_keys[2] = kCVImageBufferCleanApertureHorizontalOffsetKey;
193 clean_aperture_keys[3] = kCVImageBufferCleanApertureVerticalOffsetKey;
194
195 source_clean_aperture_values[0] = crop_width_num;
196 source_clean_aperture_values[1] = crop_height_num;
197 source_clean_aperture_values[2] = crop_offset_left_num;
198 source_clean_aperture_values[3] = crop_offset_top_num;
199
200 source_clean_aperture = CFDictionaryCreate(kCFAllocatorDefault,
201 clean_aperture_keys,
202 source_clean_aperture_values,
203 4,
204 &kCFTypeDictionaryKeyCallBacks,
205 &kCFTypeDictionaryValueCallBacks);
206
207 CFRelease(crop_width_num);
208 CFRelease(crop_height_num);
209 CFRelease(crop_offset_left_num);
210 CFRelease(crop_offset_top_num);
211
212 src = (CVPixelBufferRef)in->data[3];
213 dst = (CVPixelBufferRef)out->data[3];
214 CVBufferSetAttachment(src, kCVImageBufferCleanApertureKey,
215 source_clean_aperture, kCVAttachmentMode_ShouldPropagate);
216 ret = VTPixelTransferSessionTransferImage(s->transfer, src, dst);
217 CFRelease(source_clean_aperture);
218 if (ret != noErr) {
219 av_log(ctx, AV_LOG_ERROR, "transfer image failed, %d\n", ret);
220 ret = AVERROR_EXTERNAL;
221 goto fail;
222 }
223
224 av_frame_free(&in);
225
226 return ff_filter_frame(outlink, out);
227
228fail:
229 av_frame_free(&in);
231 return ret;
232}
233
235{
236 int err;
237 FilterLink *outl = ff_filter_link(outlink);
238 AVFilterContext *avctx = outlink->src;
239 ScaleVtContext *s = avctx->priv;
240 AVFilterLink *inlink = outlink->src->inputs[0];
241 FilterLink *inl = ff_filter_link(inlink);
242 AVHWFramesContext *hw_frame_ctx_in;
243 AVHWFramesContext *hw_frame_ctx_out;
244
245 err = ff_scale_eval_dimensions(s, s->w_expr, s->h_expr, inlink, outlink,
246 &s->output_width,
247 &s->output_height);
248 if (err < 0)
249 return err;
250
251 err = ff_scale_adjust_dimensions(inlink, &s->output_width, &s->output_height,
253 if (err < 0)
254 return err;
255
256 outlink->w = s->output_width;
257 outlink->h = s->output_height;
258
259 if (inlink->sample_aspect_ratio.num) {
260 AVRational r = {outlink->h * inlink->w, outlink->w * inlink->h};
262 } else {
263 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
264 }
265
266 hw_frame_ctx_in = (AVHWFramesContext *)inl->hw_frames_ctx->data;
267
269 outl->hw_frames_ctx = av_hwframe_ctx_alloc(hw_frame_ctx_in->device_ref);
270 hw_frame_ctx_out = (AVHWFramesContext *)outl->hw_frames_ctx->data;
271 hw_frame_ctx_out->format = AV_PIX_FMT_VIDEOTOOLBOX;
272 hw_frame_ctx_out->sw_format = hw_frame_ctx_in->sw_format;
273 hw_frame_ctx_out->width = outlink->w;
274 hw_frame_ctx_out->height = outlink->h;
275 ((AVVTFramesContext *)hw_frame_ctx_out->hwctx)->color_range = ((AVVTFramesContext *)hw_frame_ctx_in->hwctx)->color_range;
276
277 err = ff_filter_init_hw_frames(avctx, outlink, 1);
278 if (err < 0)
279 return err;
280
282 if (err < 0) {
283 av_log(avctx, AV_LOG_ERROR,
284 "Failed to init videotoolbox frame context, %s\n",
285 av_err2str(err));
286 return err;
287 }
288
289 return 0;
290}
291
292#define OFFSET(x) offsetof(ScaleVtContext, x)
293#define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
294static const AVOption scale_vt_options[] = {
295 { "w", "Output video width",
296 OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, .flags = FLAGS },
297 { "h", "Output video height",
298 OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, .flags = FLAGS },
299 { "color_matrix", "Output colour matrix coefficient set",
300 OFFSET(colour_matrix_string), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS },
301 { "color_primaries", "Output colour primaries",
302 OFFSET(colour_primaries_string), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS },
303 { "color_transfer", "Output colour transfer characteristics",
304 OFFSET(colour_transfer_string), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS },
305 { NULL },
306};
307
309
310static const AVFilterPad scale_vt_inputs[] = {
311 {
312 .name = "default",
313 .type = AVMEDIA_TYPE_VIDEO,
314 .filter_frame = &scale_vt_filter_frame,
315 },
316};
317
319 {
320 .name = "default",
321 .type = AVMEDIA_TYPE_VIDEO,
322 .config_props = &scale_vt_config_output,
323 },
324};
325
327 .p.name = "scale_vt",
328 .p.description = NULL_IF_CONFIG_SMALL("Scale Videotoolbox frames"),
329 .p.priv_class = &scale_vt_class,
330 .p.flags = AVFILTER_FLAG_HWDEVICE,
331 .priv_size = sizeof(ScaleVtContext),
337 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
338};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFFilter ff_vf_scale_vt
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_filter_init_hw_frames(AVFilterContext *avctx, AVFilterLink *link, int default_pool_size)
Perform any additional setup required for hardware frames.
Definition avfilter.c:1668
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static const AVColorPrimariesDesc color_primaries[AVCOL_PRI_NB]
Definition csp.c:76
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
double value
Definition eval.c:102
#define fail
Definition test.h:479
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
Definition avfilter.h:187
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition buffer.c:139
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
void av_frame_side_data_remove_by_props(AVFrameSideData ***sd, int *nb_sd, int props)
Remove and free all side data instances that match any of the given side data properties.
Definition side_data.c:123
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
@ AV_SIDE_DATA_PROP_SIZE_DEPENDENT
Side data depends on the video dimensions.
Definition frame.h:354
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition rational.c:80
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition rational.c:35
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition hwcontext.c:337
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition hwcontext.c:263
CFStringRef av_map_videotoolbox_color_matrix_from_av(enum AVColorSpace space)
Convert an AVColorSpace to a VideoToolbox/CoreVideo color matrix string.
CFStringRef av_map_videotoolbox_color_primaries_from_av(enum AVColorPrimaries pri)
Convert an AVColorPrimaries to a VideoToolbox/CoreVideo color primaries string.
CFStringRef av_map_videotoolbox_color_trc_from_av(enum AVColorTransferCharacteristic trc)
Convert an AVColorTransferCharacteristic to a VideoToolbox/CoreVideo color transfer function string.
An API-specific header for AV_HWDEVICE_TYPE_VIDEOTOOLBOX.
#define r
Definition input.c:42
static av_cold void uninit(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FF_FILTER_FLAG_HWFRAME_AWARE
The filter is aware of hardware frames, and any hardware frame context should not be automatically pr...
Definition filters.h:208
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition filters.h:254
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
AVOptions.
@ AV_PIX_FMT_VIDEOTOOLBOX
hardware decoding through Videotoolbox
Definition pixfmt.h:305
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition pixfmt.h:642
@ AVCOL_PRI_UNSPECIFIED
Definition pixfmt.h:645
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition pixfmt.h:672
@ AVCOL_TRC_UNSPECIFIED
Definition pixfmt.h:675
AVColorSpace
YUV colorspace type.
Definition pixfmt.h:706
@ AVCOL_SPC_UNSPECIFIED
Definition pixfmt.h:709
int ff_scale_adjust_dimensions(AVFilterLink *inlink, int *ret_w, int *ret_h, int force_original_aspect_ratio, int force_divisible_by, double w_adj)
Transform evaluated width and height obtained from ff_scale_eval_dimensions into actual target width ...
Definition scale_eval.c:123
int ff_scale_eval_dimensions(void *log_ctx, const char *w_expr, const char *h_expr, AVFilterLink *inlink, AVFilterLink *outlink, int *ret_w, int *ret_h)
Parse and evaluate string expressions for width and height.
Definition scale_eval.c:58
@ SCALE_FORCE_OAR_DISABLE
Definition scale_eval.h:25
uint8_t * data
The data buffer.
Definition buffer.h:90
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
AVFilterLink ** inputs
array of pointers to input links
Definition avfilter.h:281
void * priv
private data for use by the filter
Definition avfilter.h:288
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
size_t crop_right
Definition frame.h:798
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int width
Definition frame.h:544
int height
Definition frame.h:544
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition frame.h:569
size_t crop_top
Definition frame.h:795
size_t crop_left
Definition frame.h:797
size_t crop_bottom
Definition frame.h:796
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition hwcontext.h:200
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
Definition hwcontext.h:129
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
Definition hwcontext.h:153
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition hwcontext.h:213
int width
The allocated dimensions of the frames in this pool.
Definition hwcontext.h:220
AVOption.
Definition opt.h:428
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
VTPixelTransferSessionRef transfer
Definition vf_scale_vt.c:35
enum AVColorPrimaries colour_primaries
Definition vf_scale_vt.c:41
enum AVColorTransferCharacteristic colour_transfer
Definition vf_scale_vt.c:42
char * colour_primaries_string
Definition vf_scale_vt.c:44
char * colour_transfer_string
Definition vf_scale_vt.c:45
char * colour_matrix_string
Definition vf_scale_vt.c:46
enum AVColorSpace colour_matrix
Definition vf_scale_vt.c:43
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
static av_cold void scale_vt_uninit(AVFilterContext *avctx)
static const AVFilterPad scale_vt_outputs[]
static const AVOption scale_vt_options[]
static const AVFilterPad scale_vt_inputs[]
static int scale_vt_filter_frame(AVFilterLink *link, AVFrame *in)
static av_cold int scale_vt_init(AVFilterContext *avctx)
Definition vf_scale_vt.c:49
static int scale_vt_config_output(AVFilterLink *outlink)
#define OFFSET(x)
#define STRING_OPTION(var_name, func_name, default_value)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89