FFmpeg
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 
32 typedef struct ScaleVtContext {
33  AVClass *class;
34 
35  VTPixelTransferSessionRef transfer;
38  char *w_expr;
39  char *h_expr;
40 
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) {
91  value = av_map_videotoolbox_color_trc_from_av(s->colour_transfer);
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  return 0;
113 }
114 
116 {
117  ScaleVtContext *s = avctx->priv;
118 
119  if (s->transfer) {
120  VTPixelTransferSessionInvalidate(s->transfer);
121  CFRelease(s->transfer);
122  s->transfer = NULL;
123  }
124 }
125 
127 {
128  int ret;
129  AVFilterContext *ctx = link->dst;
130  ScaleVtContext *s = ctx->priv;
131  AVFilterLink *outlink = ctx->outputs[0];
132  CVPixelBufferRef src;
133  CVPixelBufferRef dst;
134 
135  AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
136  if (!out) {
137  ret = AVERROR(ENOMEM);
138  goto fail;
139  }
140 
141  ret = av_frame_copy_props(out, in);
142  if (ret < 0)
143  goto fail;
144 
145  av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
146  (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
147  (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
148  INT_MAX);
149  if (s->colour_primaries != AVCOL_PRI_UNSPECIFIED)
150  out->color_primaries = s->colour_primaries;
151  if (s->colour_transfer != AVCOL_TRC_UNSPECIFIED)
152  out->color_trc = s->colour_transfer;
153  if (s->colour_matrix != AVCOL_SPC_UNSPECIFIED)
154  out->colorspace = s->colour_matrix;
155 
156  src = (CVPixelBufferRef)in->data[3];
157  dst = (CVPixelBufferRef)out->data[3];
158  ret = VTPixelTransferSessionTransferImage(s->transfer, src, dst);
159  if (ret != noErr) {
160  av_log(ctx, AV_LOG_ERROR, "transfer image failed, %d\n", ret);
162  goto fail;
163  }
164 
165  av_frame_free(&in);
166 
167  return ff_filter_frame(outlink, out);
168 
169 fail:
170  av_frame_free(&in);
171  av_frame_free(&out);
172  return ret;
173 }
174 
176 {
177  int err;
178  FilterLink *outl = ff_filter_link(outlink);
179  AVFilterContext *avctx = outlink->src;
180  ScaleVtContext *s = avctx->priv;
181  AVFilterLink *inlink = outlink->src->inputs[0];
183  AVHWFramesContext *hw_frame_ctx_in;
184  AVHWFramesContext *hw_frame_ctx_out;
185 
186  err = ff_scale_eval_dimensions(s, s->w_expr, s->h_expr, inlink, outlink,
187  &s->output_width,
188  &s->output_height);
189  if (err < 0)
190  return err;
191 
192  outlink->w = s->output_width;
193  outlink->h = s->output_height;
194 
195  if (inlink->sample_aspect_ratio.num) {
196  AVRational r = {outlink->h * inlink->w, outlink->w * inlink->h};
197  outlink->sample_aspect_ratio = av_mul_q(r, inlink->sample_aspect_ratio);
198  } else {
199  outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
200  }
201 
202  hw_frame_ctx_in = (AVHWFramesContext *)inl->hw_frames_ctx->data;
203 
205  outl->hw_frames_ctx = av_hwframe_ctx_alloc(hw_frame_ctx_in->device_ref);
206  hw_frame_ctx_out = (AVHWFramesContext *)outl->hw_frames_ctx->data;
207  hw_frame_ctx_out->format = AV_PIX_FMT_VIDEOTOOLBOX;
208  hw_frame_ctx_out->sw_format = hw_frame_ctx_in->sw_format;
209  hw_frame_ctx_out->width = outlink->w;
210  hw_frame_ctx_out->height = outlink->h;
211  ((AVVTFramesContext *)hw_frame_ctx_out->hwctx)->color_range = ((AVVTFramesContext *)hw_frame_ctx_in->hwctx)->color_range;
212 
213  err = ff_filter_init_hw_frames(avctx, outlink, 1);
214  if (err < 0)
215  return err;
216 
217  err = av_hwframe_ctx_init(outl->hw_frames_ctx);
218  if (err < 0) {
219  av_log(avctx, AV_LOG_ERROR,
220  "Failed to init videotoolbox frame context, %s\n",
221  av_err2str(err));
222  return err;
223  }
224 
225  return 0;
226 }
227 
228 #define OFFSET(x) offsetof(ScaleVtContext, x)
229 #define FLAGS (AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM)
230 static const AVOption scale_vt_options[] = {
231  { "w", "Output video width",
232  OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = "iw"}, .flags = FLAGS },
233  { "h", "Output video height",
234  OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = "ih"}, .flags = FLAGS },
235  { "color_matrix", "Output colour matrix coefficient set",
236  OFFSET(colour_matrix_string), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS },
237  { "color_primaries", "Output colour primaries",
238  OFFSET(colour_primaries_string), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS },
239  { "color_transfer", "Output colour transfer characteristics",
240  OFFSET(colour_transfer_string), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = FLAGS },
241  { NULL },
242 };
243 
244 AVFILTER_DEFINE_CLASS(scale_vt);
245 
246 static const AVFilterPad scale_vt_inputs[] = {
247  {
248  .name = "default",
249  .type = AVMEDIA_TYPE_VIDEO,
250  .filter_frame = &scale_vt_filter_frame,
251  },
252 };
253 
254 static const AVFilterPad scale_vt_outputs[] = {
255  {
256  .name = "default",
257  .type = AVMEDIA_TYPE_VIDEO,
258  .config_props = &scale_vt_config_output,
259  },
260 };
261 
263  .name = "scale_vt",
264  .description = NULL_IF_CONFIG_SMALL("Scale Videotoolbox frames"),
265  .priv_size = sizeof(ScaleVtContext),
266  .init = scale_vt_init,
271  .priv_class = &scale_vt_class,
272  .flags = AVFILTER_FLAG_HWDEVICE,
273  .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
274 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:116
av_map_videotoolbox_color_trc_from_av
CFStringRef av_map_videotoolbox_color_trc_from_av(enum AVColorTransferCharacteristic trc)
Convert an AVColorTransferCharacteristic to a VideoToolbox/CoreVideo color transfer function string.
Definition: hwcontext_videotoolbox.c:490
r
const char * r
Definition: vf_curves.c:127
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AVColorTransferCharacteristic
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition: pixfmt.h:611
out
FILE * out
Definition: movenc.c:55
av_map_videotoolbox_color_matrix_from_av
CFStringRef av_map_videotoolbox_color_matrix_from_av(enum AVColorSpace space)
Convert an AVColorSpace to a VideoToolbox/CoreVideo color matrix string.
Definition: hwcontext_videotoolbox.c:438
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1062
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
AVHWFramesContext::format
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:197
ff_vf_scale_vt
const AVFilter ff_vf_scale_vt
Definition: vf_scale_vt.c:262
int64_t
long long int64_t
Definition: coverity.c:34
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
av_hwframe_ctx_init
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:322
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
pixdesc.h
av_hwframe_ctx_alloc
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition: hwcontext.c:248
AVOption
AVOption.
Definition: opt.h:429
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:614
ff_scale_eval_dimensions
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:57
AVColorPrimaries
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition: pixfmt.h:586
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
AVHWFramesContext::width
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:217
video.h
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
scale_vt_filter_frame
static int scale_vt_filter_frame(AVFilterLink *link, AVFrame *in)
Definition: vf_scale_vt.c:126
FLAGS
#define FLAGS
Definition: vf_scale_vt.c:229
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:472
fail
#define fail()
Definition: checkasm.h:189
scale_vt_init
static av_cold int scale_vt_init(AVFilterContext *avctx)
Definition: vf_scale_vt.c:49
av_reduce
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
AVRational::num
int num
Numerator.
Definition: rational.h:59
OFFSET
#define OFFSET(x)
Definition: vf_scale_vt.c:228
scale_vt_config_output
static int scale_vt_config_output(AVFilterLink *outlink)
Definition: vf_scale_vt.c:175
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
av_map_videotoolbox_color_primaries_from_av
CFStringRef av_map_videotoolbox_color_primaries_from_av(enum AVColorPrimaries pri)
Convert an AVColorPrimaries to a VideoToolbox/CoreVideo color primaries string.
Definition: hwcontext_videotoolbox.c:465
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
AVHWFramesContext::height
int height
Definition: hwcontext.h:217
s
#define s(width, name)
Definition: cbs_vp9.c:198
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
color_range
color_range
Definition: vf_selectivecolor.c:43
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:589
AVVTFramesContext
Definition: hwcontext_videotoolbox.h:45
link
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a link
Definition: filter_design.txt:23
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
AVHWFramesContext::sw_format
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:210
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:713
av_buffer_unref
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
ScaleVtContext::output_width
int output_width
Definition: vf_scale_vt.c:36
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVHWFramesContext::device_ref
AVBufferRef * device_ref
A reference to the parent AVHWDeviceContext.
Definition: hwcontext.h:126
AVFilterContext::inputs
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:465
ScaleVtContext::colour_primaries_string
char * colour_primaries_string
Definition: vf_scale_vt.c:44
ScaleVtContext::w_expr
char * w_expr
Definition: vf_scale_vt.c:38
ff_filter_link
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition: filters.h:197
FF_FILTER_FLAG_HWFRAME_AWARE
#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:206
color_primaries
static const AVColorPrimariesDesc color_primaries[AVCOL_PRI_NB]
Definition: csp.c:76
hwcontext_videotoolbox.h
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
ScaleVtContext::colour_primaries
enum AVColorPrimaries colour_primaries
Definition: vf_scale_vt.c:41
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
ScaleVtContext::h_expr
char * h_expr
Definition: vf_scale_vt.c:39
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:122
AVFILTER_FLAG_HWDEVICE
#define AVFILTER_FLAG_HWDEVICE
The filter can create hardware frames using AVFilterContext.hw_device_ctx.
Definition: avfilter.h:173
ScaleVtContext::transfer
VTPixelTransferSessionRef transfer
Definition: vf_scale_vt.c:35
scale_eval.h
ScaleVtContext::output_height
int output_height
Definition: vf_scale_vt.c:37
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
scale_vt_uninit
static av_cold void scale_vt_uninit(AVFilterContext *avctx)
Definition: vf_scale_vt.c:115
ScaleVtContext::colour_transfer_string
char * colour_transfer_string
Definition: vf_scale_vt.c:45
AV_PIX_FMT_VIDEOTOOLBOX
@ AV_PIX_FMT_VIDEOTOOLBOX
hardware decoding through Videotoolbox
Definition: pixfmt.h:305
uninit
static void uninit(AVBSFContext *ctx)
Definition: pcm_rechunk.c:68
AVColorSpace
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:640
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
scale_vt_options
static const AVOption scale_vt_options[]
Definition: vf_scale_vt.c:230
AVCOL_SPC_UNSPECIFIED
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:643
scale_vt_outputs
static const AVFilterPad scale_vt_outputs[]
Definition: vf_scale_vt.c:254
ScaleVtContext::colour_transfer
enum AVColorTransferCharacteristic colour_transfer
Definition: vf_scale_vt.c:42
AVFilter
Filter definition.
Definition: avfilter.h:201
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:115
ret
ret
Definition: filter_design.txt:187
AVHWFramesContext::hwctx
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
Definition: hwcontext.h:150
AVFrame::sample_aspect_ratio
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:496
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(scale_vt)
AVRational::den
int den
Denominator.
Definition: rational.h:60
scale_vt_inputs
static const AVFilterPad scale_vt_inputs[]
Definition: vf_scale_vt.c:246
av_mul_q
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
STRING_OPTION
#define STRING_OPTION(var_name, func_name, default_value)
hwcontext.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_OPT_TYPE_STRING
@ 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:276
FILTER_SINGLE_PIXFMT
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition: filters.h:252
ScaleVtContext::colour_matrix
enum AVColorSpace colour_matrix
Definition: vf_scale_vt.c:43
ScaleVtContext::colour_matrix_string
char * colour_matrix_string
Definition: vf_scale_vt.c:46
src
#define src
Definition: vp8dsp.c:248
ff_filter_init_hw_frames
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:1639
ScaleVtContext
Definition: vf_scale_vt.c:32