FFmpeg
vf_coreimage.m
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Thilo Borgmann
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 /**
22  * @file
23  * Video processing based on Apple's CoreImage API
24  */
25 
26 #import <CoreImage/CoreImage.h>
27 #import <AppKit/AppKit.h>
28 
29 #include "avfilter.h"
30 #include "formats.h"
31 #include "internal.h"
32 #include "video.h"
33 #include "libavutil/internal.h"
34 #include "libavutil/mem.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/pixdesc.h"
37 
38 typedef struct CoreImageContext {
39  const AVClass *class;
40 
41  int is_video_source; ///< filter is used as video source
42 
43  int w, h; ///< video size
44  AVRational sar; ///< sample aspect ratio
45  AVRational frame_rate; ///< video frame rate
46  AVRational time_base; ///< stream time base
47  int64_t duration; ///< duration expressed in microseconds
48  int64_t pts; ///< increasing presentation time stamp
49  AVFrame *picref; ///< cached reference containing the painted picture
50 
51  CFTypeRef glctx; ///< OpenGL context
52  CGContextRef cgctx; ///< Bitmap context for image copy
53  CFTypeRef input_image; ///< Input image container for passing into Core Image API
54  CGColorSpaceRef color_space; ///< Common color space for input image and cgcontext
55  int bits_per_component; ///< Shared bpc for input-output operation
56 
57  char *filter_string; ///< The complete user provided filter definition
58  CFTypeRef *filters; ///< CIFilter object for all requested filters
59  int num_filters; ///< Amount of filters in *filters
60 
61  char *output_rect; ///< Rectangle to be filled with filter intput
62  int list_filters; ///< Option used to list all available filters including generators
63  int list_generators; ///< Option used to list all available generators
65 
67 {
68  CoreImageContext *ctx = link->src->priv;
69 
70  link->w = ctx->w;
71  link->h = ctx->h;
73  link->frame_rate = ctx->frame_rate;
74  link->time_base = ctx->time_base;
75 
77  ctx->bits_per_component = av_get_bits_per_pixel(desc) / desc->nb_components;
78 
79  return 0;
80 }
81 
82 /** Determine image properties from input link of filter chain.
83  */
85 {
86  CoreImageContext *ctx = link->dst->priv;
88  ctx->bits_per_component = av_get_bits_per_pixel(desc) / desc->nb_components;
89 
90  return 0;
91 }
92 
93 /** Print a list of all available filters including options and respective value ranges and defaults.
94  */
96 {
97  // querying filters and attributes
98  NSArray *filter_categories = nil;
99 
100  if (ctx->list_generators && !ctx->list_filters) {
101  filter_categories = [NSArray arrayWithObjects:kCICategoryGenerator, nil];
102  }
103 
104  NSArray *filter_names = [CIFilter filterNamesInCategories:filter_categories];
105  NSEnumerator *filters = [filter_names objectEnumerator];
106 
107  NSString *filter_name;
108  while (filter_name = [filters nextObject]) {
109  av_log(ctx, AV_LOG_INFO, "Filter: %s\n", [filter_name UTF8String]);
110  NSString *input;
111 
112  CIFilter *filter = [CIFilter filterWithName:filter_name];
113  NSDictionary *filter_attribs = [filter attributes]; // <nsstring, id>
114  NSArray *filter_inputs = [filter inputKeys]; // <nsstring>
115 
116  for (input in filter_inputs) {
117  NSDictionary *input_attribs = [filter_attribs valueForKey:input];
118  NSString *input_class = [input_attribs valueForKey:kCIAttributeClass];
119  if ([input_class isEqualToString:@"NSNumber"]) {
120  NSNumber *value_default = [input_attribs valueForKey:kCIAttributeDefault];
121  NSNumber *value_min = [input_attribs valueForKey:kCIAttributeSliderMin];
122  NSNumber *value_max = [input_attribs valueForKey:kCIAttributeSliderMax];
123 
124  av_log(ctx, AV_LOG_INFO, "\tOption: %s\t[%s]\t[%s %s][%s]\n",
125  [input UTF8String],
126  [input_class UTF8String],
127  [[value_min stringValue] UTF8String],
128  [[value_max stringValue] UTF8String],
129  [[value_default stringValue] UTF8String]);
130  } else {
131  av_log(ctx, AV_LOG_INFO, "\tOption: %s\t[%s]\n",
132  [input UTF8String],
133  [input_class UTF8String]);
134  }
135  }
136  }
137 }
138 
140 {
141  int i;
142 
143  // (re-)initialize input image
144  const CGSize frame_size = {
145  frame->width,
146  frame->height
147  };
148 
149  NSData *data = [NSData dataWithBytesNoCopy:frame->data[0]
150  length:frame->height*frame->linesize[0]
151  freeWhenDone:NO];
152 
153  CIImage *ret = [(__bridge CIImage*)ctx->input_image initWithBitmapData:data
154  bytesPerRow:frame->linesize[0]
155  size:frame_size
156  format:kCIFormatARGB8
157  colorSpace:ctx->color_space]; //kCGColorSpaceGenericRGB
158  if (!ret) {
159  av_log(ctx, AV_LOG_ERROR, "Input image could not be initialized.\n");
160  return AVERROR_EXTERNAL;
161  }
162 
163  CIFilter *filter = NULL;
164  CIImage *filter_input = (__bridge CIImage*)ctx->input_image;
165  CIImage *filter_output = NULL;
166 
167  // successively apply all filters
168  for (i = 0; i < ctx->num_filters; i++) {
169  if (i) {
170  // set filter input to previous filter output
171  filter_input = [(__bridge CIImage*)ctx->filters[i-1] valueForKey:kCIOutputImageKey];
172  CGRect out_rect = [filter_input extent];
173  if (out_rect.size.width > frame->width || out_rect.size.height > frame->height) {
174  // do not keep padded image regions after filtering
175  out_rect.origin.x = 0.0f;
176  out_rect.origin.y = 0.0f;
177  out_rect.size.width = frame->width;
178  out_rect.size.height = frame->height;
179  }
180  filter_input = [filter_input imageByCroppingToRect:out_rect];
181  }
182 
183  filter = (__bridge CIFilter*)ctx->filters[i];
184 
185  // do not set input image for the first filter if used as video source
186  if (!ctx->is_video_source || i) {
187  @try {
188  [filter setValue:filter_input forKey:kCIInputImageKey];
189  } @catch (NSException *exception) {
190  if (![[exception name] isEqualToString:NSUndefinedKeyException]) {
191  av_log(ctx, AV_LOG_ERROR, "An error occurred: %s.", [exception.reason UTF8String]);
192  return AVERROR_EXTERNAL;
193  } else {
194  av_log(ctx, AV_LOG_WARNING, "Selected filter does not accept an input image.\n");
195  }
196  }
197  }
198  }
199 
200  // get output of last filter
201  filter_output = [filter valueForKey:kCIOutputImageKey];
202 
203  if (!filter_output) {
204  av_log(ctx, AV_LOG_ERROR, "Filter output not available.\n");
205  return AVERROR_EXTERNAL;
206  }
207 
208  // do not keep padded image regions after filtering
209  CGRect out_rect = [filter_output extent];
210  if (out_rect.size.width > frame->width || out_rect.size.height > frame->height) {
211  av_log(ctx, AV_LOG_DEBUG, "Cropping output image.\n");
212  out_rect.origin.x = 0.0f;
213  out_rect.origin.y = 0.0f;
214  out_rect.size.width = frame->width;
215  out_rect.size.height = frame->height;
216  }
217 
218  CGImageRef out = [(__bridge CIContext*)ctx->glctx createCGImage:filter_output
219  fromRect:out_rect];
220 
221  if (!out) {
222  av_log(ctx, AV_LOG_ERROR, "Cannot create valid output image.\n");
223  }
224 
225  // create bitmap context on the fly for rendering into current frame->data[]
226  if (ctx->cgctx) {
227  CGContextRelease(ctx->cgctx);
228  ctx->cgctx = NULL;
229  }
230  size_t out_width = CGImageGetWidth(out);
231  size_t out_height = CGImageGetHeight(out);
232 
233  if (out_width > frame->width || out_height > frame->height) { // this might result in segfault
234  av_log(ctx, AV_LOG_WARNING, "Output image has unexpected size: %lux%lu (expected: %ix%i). This may crash...\n",
235  out_width, out_height, frame->width, frame->height);
236  }
237  ctx->cgctx = CGBitmapContextCreate(frame->data[0],
238  frame->width,
239  frame->height,
240  ctx->bits_per_component,
241  frame->linesize[0],
242  ctx->color_space,
243  (uint32_t)kCGImageAlphaPremultipliedFirst); // ARGB
244  if (!ctx->cgctx) {
245  av_log(ctx, AV_LOG_ERROR, "CGBitmap context cannot be created.\n");
246  return AVERROR_EXTERNAL;
247  }
248 
249  // copy ("draw") the output image into the frame data
250  CGRect rect = {{0,0},{frame->width, frame->height}};
251  if (ctx->output_rect) {
252  @try {
253  NSString *tmp_string = [NSString stringWithUTF8String:ctx->output_rect];
254  NSRect tmp = NSRectFromString(tmp_string);
255  rect = NSRectToCGRect(tmp);
256  } @catch (NSException *exception) {
257  av_log(ctx, AV_LOG_ERROR, "An error occurred: %s.", [exception.reason UTF8String]);
258  return AVERROR_EXTERNAL;
259  }
260  if (rect.size.width == 0.0f) {
261  av_log(ctx, AV_LOG_WARNING, "Width of output rect is zero.\n");
262  }
263  if (rect.size.height == 0.0f) {
264  av_log(ctx, AV_LOG_WARNING, "Height of output rect is zero.\n");
265  }
266  }
267 
268  CGContextDrawImage(ctx->cgctx, rect, out);
269 
270  return ff_filter_frame(link, frame);
271 }
272 
273 /** Apply all valid filters successively to the input image.
274  * The final output image is copied from the GPU by "drawing" using a bitmap context.
275  */
277 {
278  return apply_filter(link->dst->priv, link->dst->outputs[0], frame);
279 }
280 
282 {
283  CoreImageContext *ctx = link->src->priv;
284  AVFrame *frame;
285 
286  if (ctx->duration >= 0 &&
287  av_rescale_q(ctx->pts, ctx->time_base, AV_TIME_BASE_Q) >= ctx->duration) {
288  return AVERROR_EOF;
289  }
290 
291  if (!ctx->picref) {
292  ctx->picref = ff_get_video_buffer(link, ctx->w, ctx->h);
293  if (!ctx->picref) {
294  return AVERROR(ENOMEM);
295  }
296  }
297 
298  frame = av_frame_clone(ctx->picref);
299  if (!frame) {
300  return AVERROR(ENOMEM);
301  }
302 
303  frame->pts = ctx->pts;
304  frame->duration = 1;
305 #if FF_API_FRAME_KEY
306  frame->key_frame = 1;
307 #endif
308  frame->flags |= AV_FRAME_FLAG_KEY;
309 #if FF_API_INTERLACED_FRAME
310  frame->interlaced_frame = 0;
311 #endif
312  frame->flags &= ~AV_FRAME_FLAG_INTERLACED;
313  frame->pict_type = AV_PICTURE_TYPE_I;
314  frame->sample_aspect_ratio = ctx->sar;
315 
316  ctx->pts++;
317 
318  return apply_filter(ctx, link, frame);
319 }
320 
321 /** Set an option of the given filter to the provided key-value pair.
322  */
323 static void set_option(CoreImageContext *ctx, CIFilter *filter, const char *key, const char *value)
324 {
325  NSString *input_key = [NSString stringWithUTF8String:key];
326  NSString *input_val = [NSString stringWithUTF8String:value];
327 
328  NSDictionary *filter_attribs = [filter attributes]; // <nsstring, id>
329  NSDictionary *input_attribs = [filter_attribs valueForKey:input_key];
330 
331  NSString *input_class = [input_attribs valueForKey:kCIAttributeClass];
332  NSString *input_type = [input_attribs valueForKey:kCIAttributeType];
333 
334  if (!input_attribs) {
335  av_log(ctx, AV_LOG_WARNING, "Skipping unknown option: \"%s\".\n",
336  [input_key UTF8String]); // [[filter name] UTF8String]) not currently defined...
337  return;
338  }
339 
340  av_log(ctx, AV_LOG_DEBUG, "key: %s, val: %s, #attribs: %lu, class: %s, type: %s\n",
341  [input_key UTF8String],
342  [input_val UTF8String],
343  input_attribs ? (unsigned long)[input_attribs count] : -1,
344  [input_class UTF8String],
345  [input_type UTF8String]);
346 
347  if ([input_class isEqualToString:@"NSNumber"]) {
348  float input = input_val.floatValue;
349  NSNumber *max_value = [input_attribs valueForKey:kCIAttributeSliderMax];
350  NSNumber *min_value = [input_attribs valueForKey:kCIAttributeSliderMin];
351  NSNumber *used_value = nil;
352 
353 #define CLAMP_WARNING do { \
354 av_log(ctx, AV_LOG_WARNING, "Value of \"%f\" for option \"%s\" is out of range [%f %f], clamping to \"%f\".\n", \
355  input, \
356  [input_key UTF8String], \
357  min_value.floatValue, \
358  max_value.floatValue, \
359  used_value.floatValue); \
360 } while(0)
361  if (input > max_value.floatValue) {
362  used_value = max_value;
364  } else if (input < min_value.floatValue) {
365  used_value = min_value;
367  } else {
368  used_value = [NSNumber numberWithFloat:input];
369  }
370 
371  [filter setValue:used_value forKey:input_key];
372  } else if ([input_class isEqualToString:@"CIVector"]) {
373  CIVector *input = [CIVector vectorWithString:input_val];
374 
375  if (!input) {
376  av_log(ctx, AV_LOG_WARNING, "Skipping invalid CIVctor description: \"%s\".\n",
377  [input_val UTF8String]);
378  return;
379  }
380 
381  [filter setValue:input forKey:input_key];
382  } else if ([input_class isEqualToString:@"CIColor"]) {
383  CIColor *input = [CIColor colorWithString:input_val];
384 
385  if (!input) {
386  av_log(ctx, AV_LOG_WARNING, "Skipping invalid CIColor description: \"%s\".\n",
387  [input_val UTF8String]);
388  return;
389  }
390 
391  [filter setValue:input forKey:input_key];
392  } else if ([input_class isEqualToString:@"NSString"]) { // set display name as string with latin1 encoding
393  [filter setValue:input_val forKey:input_key];
394  } else if ([input_class isEqualToString:@"NSData"]) { // set display name as string with latin1 encoding
395  NSData *input = [NSData dataWithBytes:(const void*)[input_val cStringUsingEncoding:NSISOLatin1StringEncoding]
396  length:[input_val lengthOfBytesUsingEncoding:NSISOLatin1StringEncoding]];
397 
398  if (!input) {
399  av_log(ctx, AV_LOG_WARNING, "Skipping invalid NSData description: \"%s\".\n",
400  [input_val UTF8String]);
401  return;
402  }
403 
404  [filter setValue:input forKey:input_key];
405  } else {
406  av_log(ctx, AV_LOG_WARNING, "Skipping unsupported option class: \"%s\".\n",
407  [input_class UTF8String]);
408  avpriv_report_missing_feature(ctx, "Handling of some option classes");
409  return;
410  }
411 }
412 
413 /** Create a filter object by a given name and set all options to defaults.
414  * Overwrite any option given by the user to the provided value in filter_options.
415  */
416 static CIFilter* create_filter(CoreImageContext *ctx, const char *filter_name, AVDictionary *filter_options)
417 {
418  // create filter object
419  CIFilter *filter = [CIFilter filterWithName:[NSString stringWithUTF8String:filter_name]];
420 
421  // set default options
422  [filter setDefaults];
423 
424  // set user options
425  if (filter_options) {
426  const AVDictionaryEntry *o = NULL;
427  while ((o = av_dict_iterate(filter_options, o))) {
428  set_option(ctx, filter, o->key, o->value);
429  }
430  }
431 
432  return filter;
433 }
434 
435 static av_cold int init(AVFilterContext *fctx)
436 {
437  CoreImageContext *ctx = fctx->priv;
438  AVDictionary *filter_dict = NULL;
439  const AVDictionaryEntry *f = NULL;
440  const AVDictionaryEntry *o = NULL;
441  int ret;
442  int i;
443 
444  if (ctx->list_filters || ctx->list_generators) {
445  list_filters(ctx);
446  return AVERROR_EXIT;
447  }
448 
449  if (ctx->filter_string) {
450  // parse filter string (filter=name@opt=val@opt2=val2#name2@opt3=val3) for filters separated by #
451  av_log(ctx, AV_LOG_DEBUG, "Filter_string: %s\n", ctx->filter_string);
452  ret = av_dict_parse_string(&filter_dict, ctx->filter_string, "@", "#", AV_DICT_MULTIKEY); // parse filter_name:all_filter_options
453  if (ret) {
454  av_dict_free(&filter_dict);
455  av_log(ctx, AV_LOG_ERROR, "Parsing of filters failed.\n");
456  return AVERROR(EIO);
457  }
458  ctx->num_filters = av_dict_count(filter_dict);
459  av_log(ctx, AV_LOG_DEBUG, "Filter count: %i\n", ctx->num_filters);
460 
461  // allocate CIFilter array
462  ctx->filters = av_calloc(ctx->num_filters, sizeof(CIFilter*));
463  if (!ctx->filters) {
464  av_log(ctx, AV_LOG_ERROR, "Could not allocate filter array.\n");
465  return AVERROR(ENOMEM);
466  }
467 
468  // parse filters for option key-value pairs (opt=val@opt2=val2) separated by @
469  i = 0;
470  while ((f = av_dict_iterate(filter_dict, f))) {
471  AVDictionary *filter_options = NULL;
472 
473  if (strncmp(f->value, "default", 7)) { // not default
474  ret = av_dict_parse_string(&filter_options, f->value, "=", "@", 0); // parse option_name:option_value
475  if (ret) {
476  av_dict_free(&filter_options);
477  av_log(ctx, AV_LOG_ERROR, "Parsing of filter options for \"%s\" failed.\n", f->key);
478  return AVERROR(EIO);
479  }
480  }
481 
482  if (av_log_get_level() >= AV_LOG_DEBUG) {
483  av_log(ctx, AV_LOG_DEBUG, "Creating filter %i: \"%s\":\n", i, f->key);
484  if (!filter_options) {
485  av_log(ctx, AV_LOG_DEBUG, "\tusing default options\n");
486  } else {
487  while ((o = av_dict_iterate(filter_options, o))) {
488  av_log(ctx, AV_LOG_DEBUG, "\t%s: %s\n", o->key, o->value);
489  }
490  }
491  }
492 
493  ctx->filters[i] = CFBridgingRetain(create_filter(ctx, f->key, filter_options));
494  if (!ctx->filters[i]) {
495  av_log(ctx, AV_LOG_ERROR, "Could not create filter \"%s\".\n", f->key);
496  return AVERROR(EINVAL);
497  }
498 
499  i++;
500  }
501  } else {
502  av_log(ctx, AV_LOG_ERROR, "No filters specified.\n");
503  return AVERROR(EINVAL);
504  }
505 
506  // create GPU context on OSX
507  const NSOpenGLPixelFormatAttribute attr[] = {
508  NSOpenGLPFAAccelerated,
509  NSOpenGLPFANoRecovery,
510  NSOpenGLPFAColorSize, 32,
511  0
512  };
513 
514  NSOpenGLPixelFormat *pixel_format = [[NSOpenGLPixelFormat alloc] initWithAttributes:(void *)&attr];
515  ctx->color_space = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
516  ctx->glctx = CFBridgingRetain([CIContext contextWithCGLContext:CGLGetCurrentContext()
517  pixelFormat:[pixel_format CGLPixelFormatObj]
518  colorSpace:ctx->color_space
519  options:nil]);
520 
521  if (!ctx->glctx) {
522  av_log(ctx, AV_LOG_ERROR, "CIContext not created.\n");
523  return AVERROR_EXTERNAL;
524  }
525 
526  // Creating an empty input image as input container for the context
527  ctx->input_image = CFBridgingRetain([CIImage emptyImage]);
528 
529  return 0;
530 }
531 
533 {
534  CoreImageContext *ctx = fctx->priv;
535 
536  ctx->is_video_source = 1;
537  ctx->time_base = av_inv_q(ctx->frame_rate);
538  ctx->pts = 0;
539 
540  return init(fctx);
541 }
542 
543 static av_cold void uninit(AVFilterContext *fctx)
544 {
545 #define SafeCFRelease(ptr) do { \
546  if (ptr) { \
547  CFRelease(ptr); \
548  ptr = NULL; \
549  } \
550 } while (0)
551 
552  CoreImageContext *ctx = fctx->priv;
553 
554  SafeCFRelease(ctx->glctx);
555  SafeCFRelease(ctx->cgctx);
556  SafeCFRelease(ctx->color_space);
557  SafeCFRelease(ctx->input_image);
558 
559  if (ctx->filters) {
560  for (int i = 0; i < ctx->num_filters; i++) {
561  SafeCFRelease(ctx->filters[i]);
562  }
563  av_freep(&ctx->filters);
564  }
565 
566  av_frame_free(&ctx->picref);
567 }
568 
570  {
571  .name = "default",
572  .type = AVMEDIA_TYPE_VIDEO,
573  .filter_frame = filter_frame,
574  .config_props = config_input,
575  },
576 };
577 
579  {
580  .name = "default",
581  .type = AVMEDIA_TYPE_VIDEO,
582  },
583 };
584 
586  {
587  .name = "default",
588  .type = AVMEDIA_TYPE_VIDEO,
589  .request_frame = request_frame,
590  .config_props = config_output,
591  },
592 };
593 
594 #define OFFSET(x) offsetof(CoreImageContext, x)
595 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
596 
597 #define GENERATOR_OPTIONS \
598  {"size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS}, \
599  {"s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS}, \
600  {"rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS}, \
601  {"r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS}, \
602  {"duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS}, \
603  {"d", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS}, \
604  {"sar", "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl = 1}, 0, INT_MAX, FLAGS},
605 
606 #define FILTER_OPTIONS \
607  {"list_filters", "list available filters", OFFSET(list_filters), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, .flags = FLAGS}, \
608  {"list_generators", "list available generators", OFFSET(list_generators), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, .flags = FLAGS}, \
609  {"filter", "names and options of filters to apply", OFFSET(filter_string), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS}, \
610  {"output_rect", "output rectangle within output image", OFFSET(output_rect), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS},
611 
612 
613 // definitions for coreimage video filter
614 static const AVOption coreimage_options[] = {
616  { NULL }
617 };
618 
619 AVFILTER_DEFINE_CLASS(coreimage);
620 
622  .name = "coreimage",
623  .description = NULL_IF_CONFIG_SMALL("Video filtering using CoreImage API."),
624  .init = init,
625  .uninit = uninit,
626  .priv_size = sizeof(CoreImageContext),
627  .priv_class = &coreimage_class,
631 };
632 
633 // definitions for coreimagesrc video source
634 static const AVOption coreimagesrc_options[] = {
637  { NULL }
638 };
639 
640 AVFILTER_DEFINE_CLASS(coreimagesrc);
641 
643  .name = "coreimagesrc",
644  .description = NULL_IF_CONFIG_SMALL("Video source using image generators of CoreImage API."),
645  .init = init_src,
646  .uninit = uninit,
647  .priv_size = sizeof(CoreImageContext),
648  .priv_class = &coreimagesrc_class,
649  .inputs = NULL,
652 };
ff_vsrc_coreimagesrc
const AVFilter ff_vsrc_coreimagesrc
Definition: vf_coreimage.m:642
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:112
CoreImageContext::glctx
CFTypeRef glctx
OpenGL context.
Definition: vf_coreimage.m:51
CoreImageContext::w
int w
Definition: vf_coreimage.m:43
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
name
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 minimum maximum flags name is the option name
Definition: writing_filters.txt:88
CoreImageContext::duration
int64_t duration
duration expressed in microseconds
Definition: vf_coreimage.m:47
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
out
FILE * out
Definition: movenc.c:55
CoreImageContext::cgctx
CGContextRef cgctx
Bitmap context for image copy.
Definition: vf_coreimage.m:52
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1015
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2965
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
av_dict_count
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition: dict.c:39
rect
Definition: f_ebur128.c:77
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(coreimage)
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:264
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:160
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:375
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
pixdesc.h
AVOption
AVOption.
Definition: opt.h:346
create_filter
static CIFilter * create_filter(CoreImageContext *ctx, const char *filter_name, AVDictionary *filter_options)
Create a filter object by a given name and set all options to defaults.
Definition: vf_coreimage.m:416
data
const char data[16]
Definition: mxf.c:148
CoreImageContext::color_space
CGColorSpaceRef color_space
Common color space for input image and cgcontext.
Definition: vf_coreimage.m:54
vf_coreimage_outputs
static const AVFilterPad vf_coreimage_outputs[]
Definition: vf_coreimage.m:578
av_get_bits_per_pixel
int av_get_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
Return the number of bits per pixel used by the pixel format described by pixdesc.
Definition: pixdesc.c:2917
filter
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
Definition: filter_design.txt:228
AVDictionary
Definition: dict.c:34
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
coreimage_options
static const AVOption coreimage_options[]
Definition: vf_coreimage.m:614
video.h
CoreImageContext
Definition: vf_coreimage.m:38
formats.h
ff_vf_coreimage
const AVFilter ff_vf_coreimage
Definition: vf_coreimage.m:621
filter_frame
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Apply all valid filters successively to the input image.
Definition: vf_coreimage.m:276
CLAMP_WARNING
#define CLAMP_WARNING
AVFilterContext::priv
void * priv
private data for use by the filter
Definition: avfilter.h:422
vsrc_coreimagesrc_outputs
static const AVFilterPad vsrc_coreimagesrc_outputs[]
Definition: vf_coreimage.m:585
request_frame
static int request_frame(AVFilterLink *link)
Definition: vf_coreimage.m:281
init
static av_cold int init(AVFilterContext *fctx)
Definition: vf_coreimage.m:435
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
config_output
static int config_output(AVFilterLink *link)
Definition: vf_coreimage.m:66
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:626
CoreImageContext::picref
AVFrame * picref
cached reference containing the painted picture
Definition: vf_coreimage.m:49
AVDictionaryEntry::key
char * key
Definition: dict.h:90
frame_size
int frame_size
Definition: mxfenc.c:2423
FILTER_OPTIONS
#define FILTER_OPTIONS
Definition: vf_coreimage.m:606
filters
#define filters(fmt, type, inverse, clp, inverset, clip, one, clip_fn, packed)
Definition: af_crystalizer.c:54
CoreImageContext::h
int h
video size
Definition: vf_coreimage.m:43
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
ctx
AVFormatContext * ctx
Definition: movenc.c:49
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:593
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
CoreImageContext::time_base
AVRational time_base
stream time base
Definition: vf_coreimage.m:46
key
const char * key
Definition: hwcontext_opencl.c:189
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
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
if
if(ret)
Definition: filter_design.txt:179
config_input
static int config_input(AVFilterLink *link)
Determine image properties from input link of filter chain.
Definition: vf_coreimage.m:84
av_log_get_level
int av_log_get_level(void)
Get the current log level.
Definition: log.c:442
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
AV_DICT_MULTIKEY
#define AV_DICT_MULTIKEY
Allow to store several equal keys in the dictionary.
Definition: dict.h:84
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
CoreImageContext::filters
CFTypeRef * filters
CIFilter object for all requested filters.
Definition: vf_coreimage.m:58
CoreImageContext::bits_per_component
int bits_per_component
Shared bpc for input-output operation.
Definition: vf_coreimage.m:55
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
inputs
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Definition: filter_design.txt:243
CoreImageContext::pts
int64_t pts
increasing presentation time stamp
Definition: vf_coreimage.m:48
options
const OptionDef options[]
f
f
Definition: af_crystalizer.c:121
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
CoreImageContext::sar
AVRational sar
sample aspect ratio
Definition: vf_coreimage.m:44
CoreImageContext::input_image
CFTypeRef input_image
Input image container for passing into Core Image API.
Definition: vf_coreimage.m:53
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
AVFrame::time_base
AVRational time_base
Time base for the timestamps in this frame.
Definition: frame.h:502
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:462
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
av_dict_free
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:223
input
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
Definition: filter_design.txt:172
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
internal.h
AV_PIX_FMT_ARGB
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:99
FILTER_SINGLE_PIXFMT
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition: internal.h:172
list_filters
static void list_filters(CoreImageContext *ctx)
Print a list of all available filters including options and respective value ranges and defaults.
Definition: vf_coreimage.m:95
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
coreimagesrc_options
static const AVOption coreimagesrc_options[]
Definition: vf_coreimage.m:634
internal.h
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
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
CoreImageContext::filter_string
char * filter_string
The complete user provided filter definition.
Definition: vf_coreimage.m:57
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
set_option
static void set_option(CoreImageContext *ctx, CIFilter *filter, const char *key, const char *value)
Set an option of the given filter to the provided key-value pair.
Definition: vf_coreimage.m:323
AV_FRAME_FLAG_INTERLACED
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition: frame.h:634
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
CoreImageContext::frame_rate
AVRational frame_rate
video frame rate
Definition: vf_coreimage.m:45
AVFilter
Filter definition.
Definition: avfilter.h:166
ret
ret
Definition: filter_design.txt:187
CoreImageContext::output_rect
char * output_rect
Rectangle to be filled with filter intput.
Definition: vf_coreimage.m:61
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVFrame::sample_aspect_ratio
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition: frame.h:482
CoreImageContext::num_filters
int num_filters
Amount of filters in *filters.
Definition: vf_coreimage.m:59
CoreImageContext::list_generators
int list_generators
Option used to list all available generators.
Definition: vf_coreimage.m:63
init_src
static av_cold int init_src(AVFilterContext *fctx)
Definition: vf_coreimage.m:532
avfilter.h
av_dict_parse_string
int av_dict_parse_string(AVDictionary **pm, const char *str, const char *key_val_sep, const char *pairs_sep, int flags)
Parse the key/value pairs list and add the parsed entries to a dictionary.
Definition: dict.c:200
CoreImageContext::is_video_source
int is_video_source
filter is used as video source
Definition: vf_coreimage.m:41
apply_filter
static int apply_filter(CoreImageContext *ctx, AVFilterLink *link, AVFrame *frame)
Definition: vf_coreimage.m:139
AVFormatContext::duration
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1390
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
desc
const char * desc
Definition: libsvtav1.c:75
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
uninit
static av_cold void uninit(AVFilterContext *fctx)
Definition: vf_coreimage.m:543
mem.h
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
AVDictionaryEntry
Definition: dict.h:89
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
vf_coreimage_inputs
static const AVFilterPad vf_coreimage_inputs[]
Definition: vf_coreimage.m:569
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
GENERATOR_OPTIONS
#define GENERATOR_OPTIONS
Definition: vf_coreimage.m:597
AVERROR_EXIT
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:58
SafeCFRelease
#define SafeCFRelease(ptr)
AVDictionaryEntry::value
char * value
Definition: dict.h:91
av_dict_iterate
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition: dict.c:44
CoreImageContext::list_filters
int list_filters
Option used to list all available filters including generators.
Definition: vf_coreimage.m:62