FFmpeg
avfoundation.m
Go to the documentation of this file.
1 /*
2  * AVFoundation input device
3  * Copyright (c) 2014 Thilo Borgmann <thilo.borgmann@mail.de>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * AVFoundation input device
25  * @author Thilo Borgmann <thilo.borgmann@mail.de>
26  */
27 
28 #import <AVFoundation/AVFoundation.h>
29 #include <pthread.h>
30 
32 #include "libavutil/mem.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/avstring.h"
36 #include "libavformat/demux.h"
37 #include "libavformat/internal.h"
38 #include "libavutil/internal.h"
39 #include "libavutil/parseutils.h"
40 #include "libavutil/time.h"
41 #include "libavutil/imgutils.h"
42 #include "avdevice.h"
43 
44 static const int avf_time_base = 1000000;
45 
46 static const AVRational avf_time_base_q = {
47  .num = 1,
48  .den = avf_time_base
49 };
50 
53  OSType avf_id;
54 };
55 
56 static const struct AVFPixelFormatSpec avf_pixel_formats[] = {
57  { AV_PIX_FMT_MONOBLACK, kCVPixelFormatType_1Monochrome },
58  { AV_PIX_FMT_RGB555BE, kCVPixelFormatType_16BE555 },
59  { AV_PIX_FMT_RGB555LE, kCVPixelFormatType_16LE555 },
60  { AV_PIX_FMT_RGB565BE, kCVPixelFormatType_16BE565 },
61  { AV_PIX_FMT_RGB565LE, kCVPixelFormatType_16LE565 },
62  { AV_PIX_FMT_RGB24, kCVPixelFormatType_24RGB },
63  { AV_PIX_FMT_BGR24, kCVPixelFormatType_24BGR },
64  { AV_PIX_FMT_0RGB, kCVPixelFormatType_32ARGB },
65  { AV_PIX_FMT_BGR0, kCVPixelFormatType_32BGRA },
66  { AV_PIX_FMT_0BGR, kCVPixelFormatType_32ABGR },
67  { AV_PIX_FMT_RGB0, kCVPixelFormatType_32RGBA },
68  { AV_PIX_FMT_BGR48BE, kCVPixelFormatType_48RGB },
69  { AV_PIX_FMT_UYVY422, kCVPixelFormatType_422YpCbCr8 },
70  { AV_PIX_FMT_YUVA444P, kCVPixelFormatType_4444YpCbCrA8R },
71  { AV_PIX_FMT_YUVA444P16LE, kCVPixelFormatType_4444AYpCbCr16 },
72  { AV_PIX_FMT_YUV444P, kCVPixelFormatType_444YpCbCr8 },
73  { AV_PIX_FMT_YUV422P16, kCVPixelFormatType_422YpCbCr16 },
74  { AV_PIX_FMT_YUV422P10, kCVPixelFormatType_422YpCbCr10 },
75  { AV_PIX_FMT_YUV444P10, kCVPixelFormatType_444YpCbCr10 },
76  { AV_PIX_FMT_YUV420P, kCVPixelFormatType_420YpCbCr8Planar },
77  { AV_PIX_FMT_NV12, kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange },
78  { AV_PIX_FMT_YUYV422, kCVPixelFormatType_422YpCbCr8_yuvs },
79 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
80  { AV_PIX_FMT_GRAY8, kCVPixelFormatType_OneComponent8 },
81 #endif
82  { AV_PIX_FMT_NONE, 0 }
83 };
84 
85 typedef struct
86 {
87  AVClass* class;
88 
94 
96  int width, height;
97 
104 
110 
111  char *url;
114 
116 
120  int audio_be;
124 
127 
128  enum AVPixelFormat pixel_format;
129 
130  AVCaptureSession *capture_session;
131  AVCaptureVideoDataOutput *video_output;
132  AVCaptureAudioDataOutput *audio_output;
133  CMSampleBufferRef current_frame;
134  CMSampleBufferRef current_audio_frame;
135 
136  AVCaptureDevice *observed_device;
137 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
138  AVCaptureDeviceTransportControlsPlaybackMode observed_mode;
139 #endif
141 } AVFContext;
142 
144 {
145  pthread_mutex_lock(&ctx->frame_lock);
146 }
147 
149 {
150  pthread_mutex_unlock(&ctx->frame_lock);
151 }
152 
153 /** FrameReciever class - delegate for AVCaptureSession
154  */
155 @interface AVFFrameReceiver : NSObject
156 {
158 }
159 
160 - (id)initWithContext:(AVFContext*)context;
161 
162 - (void) captureOutput:(AVCaptureOutput *)captureOutput
163  didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
164  fromConnection:(AVCaptureConnection *)connection;
165 
166 @end
167 
168 @implementation AVFFrameReceiver
169 
170 - (id)initWithContext:(AVFContext*)context
171 {
172  if (self = [super init]) {
173  _context = context;
174 
175  // start observing if a device is set for it
176 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
177  if (_context->observed_device) {
178  NSString *keyPath = NSStringFromSelector(@selector(transportControlsPlaybackMode));
179  NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew;
180 
181  [_context->observed_device addObserver: self
182  forKeyPath: keyPath
183  options: options
184  context: _context];
185  }
186 #endif
187  }
188  return self;
189 }
190 
191 - (void)dealloc {
192  // stop observing if a device is set for it
193 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
194  if (_context->observed_device) {
195  NSString *keyPath = NSStringFromSelector(@selector(transportControlsPlaybackMode));
196  [_context->observed_device removeObserver: self forKeyPath: keyPath];
197  }
198 #endif
199  [super dealloc];
200 }
201 
202 - (void)observeValueForKeyPath:(NSString *)keyPath
203  ofObject:(id)object
204  change:(NSDictionary *)change
205  context:(void *)context {
206  if (context == _context) {
207 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
208  AVCaptureDeviceTransportControlsPlaybackMode mode =
209  [change[NSKeyValueChangeNewKey] integerValue];
210 
211  if (mode != _context->observed_mode) {
212  if (mode == AVCaptureDeviceTransportControlsNotPlayingMode) {
213  _context->observed_quit = 1;
214  }
215  _context->observed_mode = mode;
216  }
217 #endif
218  } else {
219  [super observeValueForKeyPath: keyPath
220  ofObject: object
221  change: change
222  context: context];
223  }
224 }
225 
226 - (void) captureOutput:(AVCaptureOutput *)captureOutput
227  didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
228  fromConnection:(AVCaptureConnection *)connection
229 {
231 
232  if (_context->current_frame != nil) {
233  CFRelease(_context->current_frame);
234  }
235 
236  _context->current_frame = (CMSampleBufferRef)CFRetain(videoFrame);
237 
239 
241 }
242 
243 @end
244 
245 /** AudioReciever class - delegate for AVCaptureSession
246  */
247 @interface AVFAudioReceiver : NSObject
248 {
250 }
251 
252 - (id)initWithContext:(AVFContext*)context;
253 
254 - (void) captureOutput:(AVCaptureOutput *)captureOutput
255  didOutputSampleBuffer:(CMSampleBufferRef)audioFrame
256  fromConnection:(AVCaptureConnection *)connection;
257 
258 @end
259 
260 @implementation AVFAudioReceiver
261 
262 - (id)initWithContext:(AVFContext*)context
263 {
264  if (self = [super init]) {
265  _context = context;
266  }
267  return self;
268 }
269 
270 - (void) captureOutput:(AVCaptureOutput *)captureOutput
271  didOutputSampleBuffer:(CMSampleBufferRef)audioFrame
272  fromConnection:(AVCaptureConnection *)connection
273 {
275 
276  if (_context->current_audio_frame != nil) {
277  CFRelease(_context->current_audio_frame);
278  }
279 
280  _context->current_audio_frame = (CMSampleBufferRef)CFRetain(audioFrame);
281 
283 
285 }
286 
287 @end
288 
290 {
291  [ctx->capture_session stopRunning];
292 
293  [ctx->capture_session release];
294  [ctx->video_output release];
295  [ctx->audio_output release];
296  [ctx->avf_delegate release];
297  [ctx->avf_audio_delegate release];
298 
299  ctx->capture_session = NULL;
300  ctx->video_output = NULL;
301  ctx->audio_output = NULL;
302  ctx->avf_delegate = NULL;
303  ctx->avf_audio_delegate = NULL;
304 
305  av_freep(&ctx->url);
306  av_freep(&ctx->audio_buffer);
307 
308  pthread_mutex_destroy(&ctx->frame_lock);
309 
310  if (ctx->current_frame) {
311  CFRelease(ctx->current_frame);
312  }
313 }
314 
316 {
317  AVFContext *ctx = (AVFContext*)s->priv_data;
318  char *save;
319 
320  ctx->url = av_strdup(s->url);
321 
322  if (!ctx->url)
323  return AVERROR(ENOMEM);
324  if (ctx->url[0] != ':') {
325  ctx->video_filename = av_strtok(ctx->url, ":", &save);
326  ctx->audio_filename = av_strtok(NULL, ":", &save);
327  } else {
328  ctx->audio_filename = av_strtok(ctx->url, ":", &save);
329  }
330  return 0;
331 }
332 
333 /**
334  * Configure the video device.
335  *
336  * Configure the video device using a run-time approach to access properties
337  * since formats, activeFormat are available since iOS >= 7.0 or OSX >= 10.7
338  * and activeVideoMaxFrameDuration is available since i0S >= 7.0 and OSX >= 10.9.
339  *
340  * The NSUndefinedKeyException must be handled by the caller of this function.
341  *
342  */
343 static int configure_video_device(AVFormatContext *s, AVCaptureDevice *video_device)
344 {
345  AVFContext *ctx = (AVFContext*)s->priv_data;
346 
347  double framerate = av_q2d(ctx->framerate);
348  NSObject *range = nil;
349  NSObject *format = nil;
350  NSObject *selected_range = nil;
351  NSObject *selected_format = nil;
352 
353  // try to configure format by formats list
354  // might raise an exception if no format list is given
355  // (then fallback to default, no configuration)
356  @try {
357  for (format in [video_device valueForKey:@"formats"]) {
358  CMFormatDescriptionRef formatDescription;
359  CMVideoDimensions dimensions;
360 
361  formatDescription = (CMFormatDescriptionRef) [format performSelector:@selector(formatDescription)];
362  dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
363 
364  if ((ctx->width == 0 && ctx->height == 0) ||
365  (dimensions.width == ctx->width && dimensions.height == ctx->height)) {
366 
367  selected_format = format;
368 
369  for (range in [format valueForKey:@"videoSupportedFrameRateRanges"]) {
370  double max_framerate;
371 
372  [[range valueForKey:@"maxFrameRate"] getValue:&max_framerate];
373  if (fabs (framerate - max_framerate) < 0.01) {
374  selected_range = range;
375  break;
376  }
377  }
378  }
379  }
380 
381  if (!selected_format) {
382  av_log(s, AV_LOG_ERROR, "Selected video size (%dx%d) is not supported by the device.\n",
383  ctx->width, ctx->height);
384  goto unsupported_format;
385  }
386 
387  if (!selected_range) {
388  av_log(s, AV_LOG_ERROR, "Selected framerate (%f) is not supported by the device.\n",
389  framerate);
390  if (ctx->video_is_muxed) {
391  av_log(s, AV_LOG_ERROR, "Falling back to default.\n");
392  } else {
393  goto unsupported_format;
394  }
395  }
396 
397  if ([video_device lockForConfiguration:NULL] == YES) {
398  if (selected_format) {
399  [video_device setValue:selected_format forKey:@"activeFormat"];
400  }
401  if (selected_range) {
402  NSValue *min_frame_duration = [selected_range valueForKey:@"minFrameDuration"];
403  [video_device setValue:min_frame_duration forKey:@"activeVideoMinFrameDuration"];
404  [video_device setValue:min_frame_duration forKey:@"activeVideoMaxFrameDuration"];
405  }
406  } else {
407  av_log(s, AV_LOG_ERROR, "Could not lock device for configuration.\n");
408  return AVERROR(EINVAL);
409  }
410  } @catch(NSException *e) {
411  av_log(ctx, AV_LOG_WARNING, "Configuration of video device failed, falling back to default.\n");
412  }
413 
414  return 0;
415 
416 unsupported_format:
417 
418  av_log(s, AV_LOG_ERROR, "Supported modes:\n");
419  for (format in [video_device valueForKey:@"formats"]) {
420  CMFormatDescriptionRef formatDescription;
421  CMVideoDimensions dimensions;
422 
423  formatDescription = (CMFormatDescriptionRef) [format performSelector:@selector(formatDescription)];
424  dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
425 
426  for (range in [format valueForKey:@"videoSupportedFrameRateRanges"]) {
427  double min_framerate;
428  double max_framerate;
429 
430  [[range valueForKey:@"minFrameRate"] getValue:&min_framerate];
431  [[range valueForKey:@"maxFrameRate"] getValue:&max_framerate];
432  av_log(s, AV_LOG_ERROR, " %dx%d@[%f %f]fps\n",
433  dimensions.width, dimensions.height,
434  min_framerate, max_framerate);
435  }
436  }
437  return AVERROR(EINVAL);
438 }
439 
440 static int add_video_device(AVFormatContext *s, AVCaptureDevice *video_device)
441 {
442  AVFContext *ctx = (AVFContext*)s->priv_data;
443  int ret;
444  NSError *error = nil;
445  AVCaptureInput* capture_input = nil;
446  struct AVFPixelFormatSpec pxl_fmt_spec;
447  NSNumber *pixel_format;
448  NSDictionary *capture_dict;
449  dispatch_queue_t queue;
450 
451  if (ctx->video_device_index < ctx->num_video_devices) {
452  capture_input = (AVCaptureInput*) [[[AVCaptureDeviceInput alloc] initWithDevice:video_device error:&error] autorelease];
453  } else {
454  capture_input = (AVCaptureInput*) video_device;
455  }
456 
457  if (!capture_input) {
458  av_log(s, AV_LOG_ERROR, "Failed to create AV capture input device: %s\n",
459  [[error localizedDescription] UTF8String]);
460  return 1;
461  }
462 
463  if ([ctx->capture_session canAddInput:capture_input]) {
464  [ctx->capture_session addInput:capture_input];
465  } else {
466  av_log(s, AV_LOG_ERROR, "can't add video input to capture session\n");
467  return 1;
468  }
469 
470  // Attaching output
471  ctx->video_output = [[AVCaptureVideoDataOutput alloc] init];
472 
473  if (!ctx->video_output) {
474  av_log(s, AV_LOG_ERROR, "Failed to init AV video output\n");
475  return 1;
476  }
477 
478  // Configure device framerate and video size
479  @try {
480  if ((ret = configure_video_device(s, video_device)) < 0) {
481  return ret;
482  }
483  } @catch (NSException *exception) {
484  if (![[exception name] isEqualToString:NSUndefinedKeyException]) {
485  av_log (s, AV_LOG_ERROR, "An error occurred: %s", [exception.reason UTF8String]);
486  return AVERROR_EXTERNAL;
487  }
488  }
489 
490  // select pixel format
491  pxl_fmt_spec.ff_id = AV_PIX_FMT_NONE;
492 
493  for (int i = 0; avf_pixel_formats[i].ff_id != AV_PIX_FMT_NONE; i++) {
494  if (ctx->pixel_format == avf_pixel_formats[i].ff_id) {
495  pxl_fmt_spec = avf_pixel_formats[i];
496  break;
497  }
498  }
499 
500  // check if selected pixel format is supported by AVFoundation
501  if (pxl_fmt_spec.ff_id == AV_PIX_FMT_NONE) {
502  av_log(s, AV_LOG_ERROR, "Selected pixel format (%s) is not supported by AVFoundation.\n",
503  av_get_pix_fmt_name(pxl_fmt_spec.ff_id));
504  return 1;
505  }
506 
507  // check if the pixel format is available for this device
508  if ([[ctx->video_output availableVideoCVPixelFormatTypes] indexOfObject:[NSNumber numberWithInt:pxl_fmt_spec.avf_id]] == NSNotFound) {
509  av_log(s, AV_LOG_ERROR, "Selected pixel format (%s) is not supported by the input device.\n",
510  av_get_pix_fmt_name(pxl_fmt_spec.ff_id));
511 
512  pxl_fmt_spec.ff_id = AV_PIX_FMT_NONE;
513 
514  av_log(s, AV_LOG_ERROR, "Supported pixel formats:\n");
515  for (NSNumber *pxl_fmt in [ctx->video_output availableVideoCVPixelFormatTypes]) {
516  struct AVFPixelFormatSpec pxl_fmt_dummy;
517  pxl_fmt_dummy.ff_id = AV_PIX_FMT_NONE;
518  for (int i = 0; avf_pixel_formats[i].ff_id != AV_PIX_FMT_NONE; i++) {
519  if ([pxl_fmt intValue] == avf_pixel_formats[i].avf_id) {
520  pxl_fmt_dummy = avf_pixel_formats[i];
521  break;
522  }
523  }
524 
525  if (pxl_fmt_dummy.ff_id != AV_PIX_FMT_NONE) {
526  av_log(s, AV_LOG_ERROR, " %s\n", av_get_pix_fmt_name(pxl_fmt_dummy.ff_id));
527 
528  // select first supported pixel format instead of user selected (or default) pixel format
529  if (pxl_fmt_spec.ff_id == AV_PIX_FMT_NONE) {
530  pxl_fmt_spec = pxl_fmt_dummy;
531  }
532  }
533  }
534 
535  // fail if there is no appropriate pixel format or print a warning about overriding the pixel format
536  if (pxl_fmt_spec.ff_id == AV_PIX_FMT_NONE) {
537  return 1;
538  } else {
539  av_log(s, AV_LOG_WARNING, "Overriding selected pixel format to use %s instead.\n",
540  av_get_pix_fmt_name(pxl_fmt_spec.ff_id));
541  }
542  }
543 
544  // set videoSettings to an empty dict for receiving raw data of muxed devices
545  if (ctx->capture_raw_data) {
546  ctx->pixel_format = pxl_fmt_spec.ff_id;
547  ctx->video_output.videoSettings = @{ };
548  } else {
549  ctx->pixel_format = pxl_fmt_spec.ff_id;
550  pixel_format = [NSNumber numberWithUnsignedInt:pxl_fmt_spec.avf_id];
551  capture_dict = [NSDictionary dictionaryWithObject:pixel_format
552  forKey:(id)kCVPixelBufferPixelFormatTypeKey];
553 
554  [ctx->video_output setVideoSettings:capture_dict];
555  }
556  [ctx->video_output setAlwaysDiscardsLateVideoFrames:ctx->drop_late_frames];
557 
558 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
559  // check for transport control support and set observer device if supported
560  if (!ctx->video_is_screen) {
561  int trans_ctrl = [video_device transportControlsSupported];
562  AVCaptureDeviceTransportControlsPlaybackMode trans_mode = [video_device transportControlsPlaybackMode];
563 
564  if (trans_ctrl) {
565  ctx->observed_mode = trans_mode;
566  ctx->observed_device = video_device;
567  }
568  }
569 #endif
570 
571  ctx->avf_delegate = [[AVFFrameReceiver alloc] initWithContext:ctx];
572 
573  queue = dispatch_queue_create("avf_queue", NULL);
574  [ctx->video_output setSampleBufferDelegate:ctx->avf_delegate queue:queue];
575  dispatch_release(queue);
576 
577  if ([ctx->capture_session canAddOutput:ctx->video_output]) {
578  [ctx->capture_session addOutput:ctx->video_output];
579  } else {
580  av_log(s, AV_LOG_ERROR, "can't add video output to capture session\n");
581  return 1;
582  }
583 
584  return 0;
585 }
586 
587 static int add_audio_device(AVFormatContext *s, AVCaptureDevice *audio_device)
588 {
589  AVFContext *ctx = (AVFContext*)s->priv_data;
590  NSError *error = nil;
591  AVCaptureDeviceInput* audio_dev_input = [[[AVCaptureDeviceInput alloc] initWithDevice:audio_device error:&error] autorelease];
592  dispatch_queue_t queue;
593 
594  if (!audio_dev_input) {
595  av_log(s, AV_LOG_ERROR, "Failed to create AV capture input device: %s\n",
596  [[error localizedDescription] UTF8String]);
597  return 1;
598  }
599 
600  if ([ctx->capture_session canAddInput:audio_dev_input]) {
601  [ctx->capture_session addInput:audio_dev_input];
602  } else {
603  av_log(s, AV_LOG_ERROR, "can't add audio input to capture session\n");
604  return 1;
605  }
606 
607  // Attaching output
608  ctx->audio_output = [[AVCaptureAudioDataOutput alloc] init];
609 
610  if (!ctx->audio_output) {
611  av_log(s, AV_LOG_ERROR, "Failed to init AV audio output\n");
612  return 1;
613  }
614 
615  ctx->avf_audio_delegate = [[AVFAudioReceiver alloc] initWithContext:ctx];
616 
617  queue = dispatch_queue_create("avf_audio_queue", NULL);
618  [ctx->audio_output setSampleBufferDelegate:ctx->avf_audio_delegate queue:queue];
619  dispatch_release(queue);
620 
621  if ([ctx->capture_session canAddOutput:ctx->audio_output]) {
622  [ctx->capture_session addOutput:ctx->audio_output];
623  } else {
624  av_log(s, AV_LOG_ERROR, "adding audio output to capture session failed\n");
625  return 1;
626  }
627 
628  return 0;
629 }
630 
632 {
633  AVFContext *ctx = (AVFContext*)s->priv_data;
634  CVImageBufferRef image_buffer;
635  CGSize image_buffer_size;
636  AVStream* stream = avformat_new_stream(s, NULL);
637 
638  if (!stream) {
639  return 1;
640  }
641 
642  // Take stream info from the first frame.
643  while (ctx->frames_captured < 1) {
644  CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
645  }
646 
647  lock_frames(ctx);
648 
649  ctx->video_stream_index = stream->index;
650 
651  avpriv_set_pts_info(stream, 64, 1, avf_time_base);
652 
653  image_buffer = CMSampleBufferGetImageBuffer(ctx->current_frame);
654 
655  if (image_buffer) {
656  image_buffer_size = CVImageBufferGetEncodedSize(image_buffer);
657 
658  stream->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
659  stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
660  stream->codecpar->width = (int)image_buffer_size.width;
661  stream->codecpar->height = (int)image_buffer_size.height;
662  stream->codecpar->format = ctx->pixel_format;
663  } else {
664  stream->codecpar->codec_id = AV_CODEC_ID_DVVIDEO;
665  stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
666  stream->codecpar->format = ctx->pixel_format;
667  }
668 
669  CFRelease(ctx->current_frame);
670  ctx->current_frame = nil;
671 
673 
674  return 0;
675 }
676 
678 {
679  AVFContext *ctx = (AVFContext*)s->priv_data;
680  CMFormatDescriptionRef format_desc;
681  AVStream* stream = avformat_new_stream(s, NULL);
682 
683  if (!stream) {
684  return 1;
685  }
686 
687  // Take stream info from the first frame.
688  while (ctx->audio_frames_captured < 1) {
689  CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
690  }
691 
692  lock_frames(ctx);
693 
694  ctx->audio_stream_index = stream->index;
695 
696  avpriv_set_pts_info(stream, 64, 1, avf_time_base);
697 
698  format_desc = CMSampleBufferGetFormatDescription(ctx->current_audio_frame);
699  const AudioStreamBasicDescription *basic_desc = CMAudioFormatDescriptionGetStreamBasicDescription(format_desc);
700 
701  if (!basic_desc) {
703  av_log(s, AV_LOG_ERROR, "audio format not available\n");
704  return 1;
705  }
706 
707  stream->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
708  stream->codecpar->sample_rate = basic_desc->mSampleRate;
709  av_channel_layout_default(&stream->codecpar->ch_layout, basic_desc->mChannelsPerFrame);
710 
711  ctx->audio_channels = basic_desc->mChannelsPerFrame;
712  ctx->audio_bits_per_sample = basic_desc->mBitsPerChannel;
713  ctx->audio_float = basic_desc->mFormatFlags & kAudioFormatFlagIsFloat;
714  ctx->audio_be = basic_desc->mFormatFlags & kAudioFormatFlagIsBigEndian;
715  ctx->audio_signed_integer = basic_desc->mFormatFlags & kAudioFormatFlagIsSignedInteger;
716  ctx->audio_packed = basic_desc->mFormatFlags & kAudioFormatFlagIsPacked;
717  ctx->audio_non_interleaved = basic_desc->mFormatFlags & kAudioFormatFlagIsNonInterleaved;
718 
719  if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
720  ctx->audio_float &&
721  ctx->audio_bits_per_sample == 32 &&
722  ctx->audio_packed) {
723  stream->codecpar->codec_id = ctx->audio_be ? AV_CODEC_ID_PCM_F32BE : AV_CODEC_ID_PCM_F32LE;
724  } else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
725  ctx->audio_signed_integer &&
726  ctx->audio_bits_per_sample == 16 &&
727  ctx->audio_packed) {
728  stream->codecpar->codec_id = ctx->audio_be ? AV_CODEC_ID_PCM_S16BE : AV_CODEC_ID_PCM_S16LE;
729  } else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
730  ctx->audio_signed_integer &&
731  ctx->audio_bits_per_sample == 24 &&
732  ctx->audio_packed) {
733  stream->codecpar->codec_id = ctx->audio_be ? AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
734  } else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
735  ctx->audio_signed_integer &&
736  ctx->audio_bits_per_sample == 32 &&
737  ctx->audio_packed) {
738  stream->codecpar->codec_id = ctx->audio_be ? AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE;
739  } else {
741  av_log(s, AV_LOG_ERROR, "audio format is not supported\n");
742  return 1;
743  }
744 
745  if (ctx->audio_non_interleaved) {
746  CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(ctx->current_audio_frame);
747  ctx->audio_buffer_size = CMBlockBufferGetDataLength(block_buffer);
748  ctx->audio_buffer = av_malloc(ctx->audio_buffer_size);
749  if (!ctx->audio_buffer) {
751  av_log(s, AV_LOG_ERROR, "error allocating audio buffer\n");
752  return 1;
753  }
754  }
755 
756  CFRelease(ctx->current_audio_frame);
757  ctx->current_audio_frame = nil;
758 
760 
761  return 0;
762 }
763 
764 static NSArray* getDevicesWithMediaType(AVMediaType mediaType) {
765 #if ((TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 100000) || (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101500))
766  NSMutableArray *deviceTypes = nil;
767  if (mediaType == AVMediaTypeVideo) {
768  deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeBuiltInWideAngleCamera]];
769  #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 100000)
770  [deviceTypes addObject: AVCaptureDeviceTypeBuiltInDualCamera];
771  [deviceTypes addObject: AVCaptureDeviceTypeBuiltInTelephotoCamera];
772  #endif
773  #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 110100)
774  [deviceTypes addObject: AVCaptureDeviceTypeBuiltInTrueDepthCamera];
775  #endif
776  #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 130000)
777  [deviceTypes addObject: AVCaptureDeviceTypeBuiltInTripleCamera];
778  [deviceTypes addObject: AVCaptureDeviceTypeBuiltInDualWideCamera];
779  [deviceTypes addObject: AVCaptureDeviceTypeBuiltInUltraWideCamera];
780  #endif
781  #if (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 130000)
782  [deviceTypes addObject: AVCaptureDeviceTypeDeskViewCamera];
783  #endif
784  #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 150400)
785  [deviceTypes addObject: AVCaptureDeviceTypeBuiltInLiDARDepthCamera];
786  #endif
787  #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 170000 || (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 140000))
788  [deviceTypes addObject: AVCaptureDeviceTypeContinuityCamera];
789  [deviceTypes addObject: AVCaptureDeviceTypeExternal];
790  #elif (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED < 140000)
791  [deviceTypes addObject: AVCaptureDeviceTypeExternalUnknown];
792  #endif
793  } else if (mediaType == AVMediaTypeAudio) {
794  #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 170000 || (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 140000))
795  deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeMicrophone]];
796  #else
797  deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeBuiltInMicrophone]];
798  #endif
799  } else if (mediaType == AVMediaTypeMuxed) {
800  #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 170000 || (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 140000))
801  deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeExternal]];
802  #elif (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED < 140000)
803  deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeExternalUnknown]];
804  #else
805  return nil;
806  #endif
807  } else {
808  return nil;
809  }
810 
811  AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession =
812  [AVCaptureDeviceDiscoverySession
813  discoverySessionWithDeviceTypes:deviceTypes
814  mediaType:mediaType
815  position:AVCaptureDevicePositionUnspecified];
816  return [captureDeviceDiscoverySession devices];
817 #else
818  return [AVCaptureDevice devicesWithMediaType:mediaType];
819 #endif
820 }
821 
823 {
824  int ret = 0;
825  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
826  uint32_t num_screens = 0;
827  AVFContext *ctx = (AVFContext*)s->priv_data;
828  AVCaptureDevice *video_device = nil;
829  AVCaptureDevice *audio_device = nil;
830  // Find capture device
831  NSArray *devices = getDevicesWithMediaType(AVMediaTypeVideo);
832  NSArray *devices_muxed = getDevicesWithMediaType(AVMediaTypeMuxed);
833 
834  ctx->num_video_devices = [devices count] + [devices_muxed count];
835 
836  pthread_mutex_init(&ctx->frame_lock, NULL);
837 
838 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
839  CGGetActiveDisplayList(0, NULL, &num_screens);
840 #endif
841 
842  // List devices if requested
843  if (ctx->list_devices) {
844  int index = 0;
845  av_log(ctx, AV_LOG_INFO, "AVFoundation video devices:\n");
846  for (AVCaptureDevice *device in devices) {
847  const char *name = [[device localizedName] UTF8String];
848  index = [devices indexOfObject:device];
849  av_log(ctx, AV_LOG_INFO, "[%d] %s\n", index, name);
850  }
851  for (AVCaptureDevice *device in devices_muxed) {
852  const char *name = [[device localizedName] UTF8String];
853  index = [devices count] + [devices_muxed indexOfObject:device];
854  av_log(ctx, AV_LOG_INFO, "[%d] %s\n", index, name);
855  }
856 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
857  if (num_screens > 0) {
858  CGDirectDisplayID screens[num_screens];
859  CGGetActiveDisplayList(num_screens, screens, &num_screens);
860  for (int i = 0; i < num_screens; i++) {
861  av_log(ctx, AV_LOG_INFO, "[%d] Capture screen %d\n", ctx->num_video_devices + i, i);
862  }
863  }
864 #endif
865 
866  av_log(ctx, AV_LOG_INFO, "AVFoundation audio devices:\n");
867  devices = getDevicesWithMediaType(AVMediaTypeAudio);
868  for (AVCaptureDevice *device in devices) {
869  const char *name = [[device localizedName] UTF8String];
870  int index = [devices indexOfObject:device];
871  av_log(ctx, AV_LOG_INFO, "[%d] %s\n", index, name);
872  }
873  goto fail;
874  }
875 
876  // parse input filename for video and audio device
878  if (ret)
879  goto fail;
880 
881  // check for device index given in filename
882  if (ctx->video_device_index == -1 && ctx->video_filename) {
883  sscanf(ctx->video_filename, "%d", &ctx->video_device_index);
884  }
885  if (ctx->audio_device_index == -1 && ctx->audio_filename) {
886  sscanf(ctx->audio_filename, "%d", &ctx->audio_device_index);
887  }
888 
889  if (ctx->video_device_index >= 0) {
890  if (ctx->video_device_index < ctx->num_video_devices) {
891  if (ctx->video_device_index < [devices count]) {
892  video_device = [devices objectAtIndex:ctx->video_device_index];
893  } else {
894  video_device = [devices_muxed objectAtIndex:(ctx->video_device_index - [devices count])];
895  ctx->video_is_muxed = 1;
896  }
897  } else if (ctx->video_device_index < ctx->num_video_devices + num_screens) {
898 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
899  CGDirectDisplayID screens[num_screens];
900  CGGetActiveDisplayList(num_screens, screens, &num_screens);
901  AVCaptureScreenInput* capture_screen_input = [[[AVCaptureScreenInput alloc] initWithDisplayID:screens[ctx->video_device_index - ctx->num_video_devices]] autorelease];
902 
903  if (ctx->framerate.num > 0) {
904  capture_screen_input.minFrameDuration = CMTimeMake(ctx->framerate.den, ctx->framerate.num);
905  }
906 
907 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
908  if (ctx->capture_cursor) {
909  capture_screen_input.capturesCursor = YES;
910  } else {
911  capture_screen_input.capturesCursor = NO;
912  }
913 #endif
914 
915  if (ctx->capture_mouse_clicks) {
916  capture_screen_input.capturesMouseClicks = YES;
917  } else {
918  capture_screen_input.capturesMouseClicks = NO;
919  }
920 
921  video_device = (AVCaptureDevice*) capture_screen_input;
922  ctx->video_is_screen = 1;
923 #endif
924  } else {
925  av_log(ctx, AV_LOG_ERROR, "Invalid device index\n");
926  goto fail;
927  }
928  } else if (ctx->video_filename &&
929  strncmp(ctx->video_filename, "none", 4)) {
930  if (!strncmp(ctx->video_filename, "default", 7)) {
931  video_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
932  } else {
933  // looking for video inputs
934  for (AVCaptureDevice *device in devices) {
935  if (!strncmp(ctx->video_filename, [[device localizedName] UTF8String], strlen(ctx->video_filename))) {
936  video_device = device;
937  break;
938  }
939  }
940  // looking for muxed inputs
941  for (AVCaptureDevice *device in devices_muxed) {
942  if (!strncmp(ctx->video_filename, [[device localizedName] UTF8String], strlen(ctx->video_filename))) {
943  video_device = device;
944  ctx->video_is_muxed = 1;
945  break;
946  }
947  }
948 
949 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
950  // looking for screen inputs
951  if (!video_device) {
952  int idx;
953  if(sscanf(ctx->video_filename, "Capture screen %d", &idx) && idx < num_screens) {
954  CGDirectDisplayID screens[num_screens];
955  CGGetActiveDisplayList(num_screens, screens, &num_screens);
956  AVCaptureScreenInput* capture_screen_input = [[[AVCaptureScreenInput alloc] initWithDisplayID:screens[idx]] autorelease];
957  video_device = (AVCaptureDevice*) capture_screen_input;
958  ctx->video_device_index = ctx->num_video_devices + idx;
959  ctx->video_is_screen = 1;
960 
961  if (ctx->framerate.num > 0) {
962  capture_screen_input.minFrameDuration = CMTimeMake(ctx->framerate.den, ctx->framerate.num);
963  }
964 
965 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
966  if (ctx->capture_cursor) {
967  capture_screen_input.capturesCursor = YES;
968  } else {
969  capture_screen_input.capturesCursor = NO;
970  }
971 #endif
972 
973  if (ctx->capture_mouse_clicks) {
974  capture_screen_input.capturesMouseClicks = YES;
975  } else {
976  capture_screen_input.capturesMouseClicks = NO;
977  }
978  }
979  }
980 #endif
981  }
982 
983  if (!video_device) {
984  av_log(ctx, AV_LOG_ERROR, "Video device not found\n");
985  goto fail;
986  }
987  }
988 
989  // get audio device
990  if (ctx->audio_device_index >= 0) {
991  NSArray *devices = getDevicesWithMediaType(AVMediaTypeAudio);
992 
993  if (ctx->audio_device_index >= [devices count]) {
994  av_log(ctx, AV_LOG_ERROR, "Invalid audio device index\n");
995  goto fail;
996  }
997 
998  audio_device = [devices objectAtIndex:ctx->audio_device_index];
999  } else if (ctx->audio_filename &&
1000  strncmp(ctx->audio_filename, "none", 4)) {
1001  if (!strncmp(ctx->audio_filename, "default", 7)) {
1002  audio_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
1003  } else {
1004  NSArray *devices = getDevicesWithMediaType(AVMediaTypeAudio);
1005 
1006  for (AVCaptureDevice *device in devices) {
1007  if (!strncmp(ctx->audio_filename, [[device localizedName] UTF8String], strlen(ctx->audio_filename))) {
1008  audio_device = device;
1009  break;
1010  }
1011  }
1012  }
1013 
1014  if (!audio_device) {
1015  av_log(ctx, AV_LOG_ERROR, "Audio device not found\n");
1016  goto fail;
1017  }
1018  }
1019 
1020  // Video nor Audio capture device not found, looking for AVMediaTypeVideo/Audio
1021  if (!video_device && !audio_device) {
1022  av_log(s, AV_LOG_ERROR, "No AV capture device found\n");
1023  goto fail;
1024  }
1025 
1026  if (video_device) {
1027  if (ctx->video_device_index < ctx->num_video_devices) {
1028  av_log(s, AV_LOG_DEBUG, "'%s' opened\n", [[video_device localizedName] UTF8String]);
1029  } else {
1030  av_log(s, AV_LOG_DEBUG, "'%s' opened\n", [[video_device description] UTF8String]);
1031  }
1032  }
1033  if (audio_device) {
1034  av_log(s, AV_LOG_DEBUG, "audio device '%s' opened\n", [[audio_device localizedName] UTF8String]);
1035  }
1036 
1037  // Initialize capture session
1038  ctx->capture_session = [[AVCaptureSession alloc] init];
1039 
1040  if (video_device && add_video_device(s, video_device)) {
1041  goto fail;
1042  }
1043  if (audio_device && add_audio_device(s, audio_device)) {
1044  }
1045 
1046  [ctx->capture_session startRunning];
1047 
1048  /* Unlock device configuration only after the session is started so it
1049  * does not reset the capture formats */
1050  if (!ctx->video_is_screen) {
1051  [video_device unlockForConfiguration];
1052  }
1053 
1054  if (video_device && get_video_config(s)) {
1055  goto fail;
1056  }
1057 
1058  // set audio stream
1059  if (audio_device && get_audio_config(s)) {
1060  goto fail;
1061  }
1062 
1063  [pool release];
1064  return 0;
1065 
1066 fail:
1067  [pool release];
1069  if (ret)
1070  return ret;
1071  return AVERROR(EIO);
1072 }
1073 
1075  CVPixelBufferRef image_buffer,
1076  AVPacket *pkt)
1077 {
1078  AVFContext *ctx = s->priv_data;
1079  int src_linesize[4];
1080  const uint8_t *src_data[4];
1081  int width = CVPixelBufferGetWidth(image_buffer);
1082  int height = CVPixelBufferGetHeight(image_buffer);
1083  int status;
1084 
1085  memset(src_linesize, 0, sizeof(src_linesize));
1086  memset(src_data, 0, sizeof(src_data));
1087 
1088  status = CVPixelBufferLockBaseAddress(image_buffer, 0);
1089  if (status != kCVReturnSuccess) {
1090  av_log(s, AV_LOG_ERROR, "Could not lock base address: %d (%dx%d)\n", status, width, height);
1091  return AVERROR_EXTERNAL;
1092  }
1093 
1094  if (CVPixelBufferIsPlanar(image_buffer)) {
1095  size_t plane_count = CVPixelBufferGetPlaneCount(image_buffer);
1096  int i;
1097  for(i = 0; i < plane_count; i++){
1098  src_linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(image_buffer, i);
1099  src_data[i] = CVPixelBufferGetBaseAddressOfPlane(image_buffer, i);
1100  }
1101  } else {
1102  src_linesize[0] = CVPixelBufferGetBytesPerRow(image_buffer);
1103  src_data[0] = CVPixelBufferGetBaseAddress(image_buffer);
1104  }
1105 
1107  src_data, src_linesize,
1108  ctx->pixel_format, width, height, 1);
1109 
1110 
1111 
1112  CVPixelBufferUnlockBaseAddress(image_buffer, 0);
1113 
1114  return status;
1115 }
1116 
1118 {
1119  AVFContext* ctx = (AVFContext*)s->priv_data;
1120 
1121  do {
1122  CVImageBufferRef image_buffer;
1123  CMBlockBufferRef block_buffer;
1124  lock_frames(ctx);
1125 
1126  if (ctx->current_frame != nil) {
1127  int status;
1128  int length = 0;
1129 
1130  image_buffer = CMSampleBufferGetImageBuffer(ctx->current_frame);
1131  block_buffer = CMSampleBufferGetDataBuffer(ctx->current_frame);
1132 
1133  if (image_buffer != nil) {
1134  length = (int)CVPixelBufferGetDataSize(image_buffer);
1135  } else if (block_buffer != nil) {
1136  length = (int)CMBlockBufferGetDataLength(block_buffer);
1137  } else {
1138  unlock_frames(ctx);
1139  return AVERROR(EINVAL);
1140  }
1141 
1142  if (av_new_packet(pkt, length) < 0) {
1143  unlock_frames(ctx);
1144  return AVERROR(EIO);
1145  }
1146 
1147  CMItemCount count;
1148  CMSampleTimingInfo timing_info;
1149 
1150  if (CMSampleBufferGetOutputSampleTimingInfoArray(ctx->current_frame, 1, &timing_info, &count) == noErr) {
1151  AVRational timebase_q = av_make_q(1, timing_info.presentationTimeStamp.timescale);
1152  pkt->pts = pkt->dts = av_rescale_q(timing_info.presentationTimeStamp.value, timebase_q, avf_time_base_q);
1153  }
1154 
1155  pkt->stream_index = ctx->video_stream_index;
1157 
1158  if (image_buffer) {
1159  status = copy_cvpixelbuffer(s, image_buffer, pkt);
1160  } else {
1161  status = 0;
1162  OSStatus ret = CMBlockBufferCopyDataBytes(block_buffer, 0, pkt->size, pkt->data);
1163  if (ret != kCMBlockBufferNoErr) {
1164  status = AVERROR(EIO);
1165  }
1166  }
1167  CFRelease(ctx->current_frame);
1168  ctx->current_frame = nil;
1169 
1170  if (status < 0) {
1171  unlock_frames(ctx);
1172  return status;
1173  }
1174  } else if (ctx->current_audio_frame != nil) {
1175  CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(ctx->current_audio_frame);
1176  int block_buffer_size = CMBlockBufferGetDataLength(block_buffer);
1177 
1178  if (!block_buffer || !block_buffer_size) {
1179  unlock_frames(ctx);
1180  return AVERROR(EIO);
1181  }
1182 
1183  if (ctx->audio_non_interleaved && block_buffer_size > ctx->audio_buffer_size) {
1184  unlock_frames(ctx);
1185  return AVERROR_BUFFER_TOO_SMALL;
1186  }
1187 
1188  if (av_new_packet(pkt, block_buffer_size) < 0) {
1189  unlock_frames(ctx);
1190  return AVERROR(EIO);
1191  }
1192 
1193  CMItemCount count;
1194  CMSampleTimingInfo timing_info;
1195 
1196  if (CMSampleBufferGetOutputSampleTimingInfoArray(ctx->current_audio_frame, 1, &timing_info, &count) == noErr) {
1197  AVRational timebase_q = av_make_q(1, timing_info.presentationTimeStamp.timescale);
1198  pkt->pts = pkt->dts = av_rescale_q(timing_info.presentationTimeStamp.value, timebase_q, avf_time_base_q);
1199  }
1200 
1201  pkt->stream_index = ctx->audio_stream_index;
1203 
1204  if (ctx->audio_non_interleaved) {
1205  int sample, c, shift, num_samples;
1206 
1207  OSStatus ret = CMBlockBufferCopyDataBytes(block_buffer, 0, pkt->size, ctx->audio_buffer);
1208  if (ret != kCMBlockBufferNoErr) {
1209  unlock_frames(ctx);
1210  return AVERROR(EIO);
1211  }
1212 
1213  num_samples = pkt->size / (ctx->audio_channels * (ctx->audio_bits_per_sample >> 3));
1214 
1215  // transform decoded frame into output format
1216  #define INTERLEAVE_OUTPUT(bps) \
1217  { \
1218  int##bps##_t **src; \
1219  int##bps##_t *dest; \
1220  src = av_malloc(ctx->audio_channels * sizeof(int##bps##_t*)); \
1221  if (!src) { \
1222  unlock_frames(ctx); \
1223  return AVERROR(EIO); \
1224  } \
1225  \
1226  for (c = 0; c < ctx->audio_channels; c++) { \
1227  src[c] = ((int##bps##_t*)ctx->audio_buffer) + c * num_samples; \
1228  } \
1229  dest = (int##bps##_t*)pkt->data; \
1230  shift = bps - ctx->audio_bits_per_sample; \
1231  for (sample = 0; sample < num_samples; sample++) \
1232  for (c = 0; c < ctx->audio_channels; c++) \
1233  *dest++ = src[c][sample] << shift; \
1234  av_freep(&src); \
1235  }
1236 
1237  if (ctx->audio_bits_per_sample <= 16) {
1238  INTERLEAVE_OUTPUT(16)
1239  } else {
1240  INTERLEAVE_OUTPUT(32)
1241  }
1242  } else {
1243  OSStatus ret = CMBlockBufferCopyDataBytes(block_buffer, 0, pkt->size, pkt->data);
1244  if (ret != kCMBlockBufferNoErr) {
1245  unlock_frames(ctx);
1246  return AVERROR(EIO);
1247  }
1248  }
1249 
1250  CFRelease(ctx->current_audio_frame);
1251  ctx->current_audio_frame = nil;
1252  } else {
1253  pkt->data = NULL;
1254  unlock_frames(ctx);
1255  if (ctx->observed_quit) {
1256  return AVERROR_EOF;
1257  } else {
1258  return AVERROR(EAGAIN);
1259  }
1260  }
1261 
1262  unlock_frames(ctx);
1263  } while (!pkt->data);
1264 
1265  return 0;
1266 }
1267 
1269 {
1270  AVFContext* ctx = (AVFContext*)s->priv_data;
1272  return 0;
1273 }
1274 
1275 static const AVOption options[] = {
1276  { "list_devices", "list available devices", offsetof(AVFContext, list_devices), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
1277  { "video_device_index", "select video device by index for devices with same name (starts at 0)", offsetof(AVFContext, video_device_index), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
1278  { "audio_device_index", "select audio device by index for devices with same name (starts at 0)", offsetof(AVFContext, audio_device_index), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
1279  { "pixel_format", "set pixel format", offsetof(AVFContext, pixel_format), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_YUV420P}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM},
1280  { "framerate", "set frame rate", offsetof(AVFContext, framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "ntsc"}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
1281  { "video_size", "set video size", offsetof(AVFContext, width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
1282  { "capture_cursor", "capture the screen cursor", offsetof(AVFContext, capture_cursor), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
1283  { "capture_mouse_clicks", "capture the screen mouse clicks", offsetof(AVFContext, capture_mouse_clicks), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
1284  { "capture_raw_data", "capture the raw data from device connection", offsetof(AVFContext, capture_raw_data), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
1285  { "drop_late_frames", "drop frames that are available later than expected", offsetof(AVFContext, drop_late_frames), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
1286 
1287  { NULL },
1288 };
1289 
1290 static const AVClass avf_class = {
1291  .class_name = "AVFoundation indev",
1292  .item_name = av_default_item_name,
1293  .option = options,
1294  .version = LIBAVUTIL_VERSION_INT,
1296 };
1297 
1299  .p.name = "avfoundation",
1300  .p.long_name = NULL_IF_CONFIG_SMALL("AVFoundation input device"),
1301  .p.flags = AVFMT_NOFILE,
1302  .p.priv_class = &avf_class,
1303  .priv_data_size = sizeof(AVFContext),
1306  .read_close = avf_close,
1307 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:334
pthread_mutex_t
_fmutex pthread_mutex_t
Definition: os2threads.h:53
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
AV_CODEC_ID_PCM_F32BE
@ AV_CODEC_ID_PCM_F32BE
Definition: codec_id.h:354
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
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
AVFContext::audio_buffer_size
int audio_buffer_size
Definition: avfoundation.m:126
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
AVFContext::audio_float
int audio_float
Definition: avfoundation.m:119
AVFContext::observed_quit
int observed_quit
Definition: avfoundation.m:140
unlock_frames
static void unlock_frames(AVFContext *ctx)
Definition: avfoundation.m:148
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AV_OPT_TYPE_VIDEO_RATE
@ AV_OPT_TYPE_VIDEO_RATE
Underlying C type is AVRational.
Definition: opt.h:315
pthread_mutex_init
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: os2threads.h:104
AV_CODEC_ID_RAWVIDEO
@ AV_CODEC_ID_RAWVIDEO
Definition: codec_id.h:65
AVFContext::current_audio_frame
CMSampleBufferRef current_audio_frame
Definition: avfoundation.m:134
pixdesc.h
AVFContext::audio_frames_captured
int audio_frames_captured
Definition: avfoundation.m:90
AVPacket::data
uint8_t * data
Definition: packet.h:539
AVOption
AVOption.
Definition: opt.h:429
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:76
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:594
parse_device_name
static int parse_device_name(AVFormatContext *s)
Definition: avfoundation.m:315
AV_PIX_FMT_RGB555BE
@ AV_PIX_FMT_RGB555BE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), big-endian , X=unused/undefined
Definition: pixfmt.h:114
AVFContext::audio_channels
int audio_channels
Definition: avfoundation.m:117
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
AVFContext::video_filename
char * video_filename
Definition: avfoundation.m:112
AVFPixelFormatSpec::avf_id
OSType avf_id
Definition: avfoundation.m:53
AVFContext::audio_be
int audio_be
Definition: avfoundation.m:120
AVFContext::capture_cursor
int capture_cursor
Definition: avfoundation.m:98
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:867
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:335
fail
#define fail()
Definition: checkasm.h:188
avf_close
static int avf_close(AVFormatContext *s)
Definition: avfoundation.m:1268
avf_time_base
static const int avf_time_base
Definition: avfoundation.m:44
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
AVFContext::current_frame
CMSampleBufferRef current_frame
Definition: avfoundation.m:133
AVFPixelFormatSpec::ff_id
enum AVPixelFormat ff_id
Definition: avfoundation.m:52
AVFContext::observed_device
AVCaptureDevice * observed_device
Definition: avfoundation.m:136
AVERROR_BUFFER_TOO_SMALL
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
Definition: error.h:53
AVRational::num
int num
Numerator.
Definition: rational.h:59
AVFContext::framerate
AVRational framerate
Definition: avfoundation.m:95
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:505
description
Tag description
Definition: snow.txt:206
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
avf_time_base_q
static const AVRational avf_time_base_q
Definition: avfoundation.m:46
AV_PIX_FMT_YUV422P16
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:514
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
AVFContext::num_video_devices
int num_video_devices
Definition: avfoundation.m:115
INTERLEAVE_OUTPUT
#define INTERLEAVE_OUTPUT(bps)
getDevicesWithMediaType
static NSArray * getDevicesWithMediaType(AVMediaType mediaType)
Definition: avfoundation.m:764
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: packet.c:98
format
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 format(the sample packing is implied by the sample format) and sample rate. The lists are not just lists
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVFAudioReceiver::_context
AVFContext * _context
Definition: avfoundation.m:249
options
static const AVOption options[]
Definition: avfoundation.m:1275
add_audio_device
static int add_audio_device(AVFormatContext *s, AVCaptureDevice *audio_device)
Definition: avfoundation.m:587
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
av_strtok
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition: avstring.c:178
AVFContext::capture_mouse_clicks
int capture_mouse_clicks
Definition: avfoundation.m:99
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
ctx
AVFormatContext * ctx
Definition: movenc.c:49
AVFContext::frame_lock
pthread_mutex_t frame_lock
Definition: avfoundation.m:91
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
AVFContext::capture_raw_data
int capture_raw_data
Definition: avfoundation.m:100
AVFContext::list_devices
int list_devices
Definition: avfoundation.m:105
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
AVFPixelFormatSpec
Definition: avfoundation.m:51
get_video_config
static int get_video_config(AVFormatContext *s)
Definition: avfoundation.m:631
if
if(ret)
Definition: filter_design.txt:179
AVFContext::audio_packed
int audio_packed
Definition: avfoundation.m:122
AVFFrameReceiver::_context
AVFContext * _context
Definition: avfoundation.m:157
context
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 keep it simple and lowercase description are in without and describe what they for example set the foo of the bar offset is the offset of the field in your context
Definition: writing_filters.txt:91
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVFContext::video_output
AVCaptureVideoDataOutput * video_output
Definition: avfoundation.m:131
framerate
float framerate
Definition: av1_levels.c:29
AVFContext::audio_signed_integer
int audio_signed_integer
Definition: avfoundation.m:121
AV_PIX_FMT_RGB565LE
@ AV_PIX_FMT_RGB565LE
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), little-endian
Definition: pixfmt.h:113
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:540
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
NULL
#define NULL
Definition: coverity.c:32
AVFContext::drop_late_frames
int drop_late_frames
Definition: avfoundation.m:101
AV_PIX_FMT_YUYV422
@ AV_PIX_FMT_YUYV422
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:74
add_video_device
static int add_video_device(AVFormatContext *s, AVCaptureDevice *video_device)
Definition: avfoundation.m:440
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVFFrameReceiver
FrameReciever class - delegate for AVCaptureSession.
Definition: avfoundation.m:155
ff_avfoundation_demuxer
const FFInputFormat ff_avfoundation_demuxer
Definition: avfoundation.m:1298
AV_PIX_FMT_MONOBLACK
@ AV_PIX_FMT_MONOBLACK
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb.
Definition: pixfmt.h:83
AV_OPT_TYPE_IMAGE_SIZE
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
Definition: opt.h:303
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
parseutils.h
AV_PIX_FMT_BGR0
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:265
time.h
AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT
@ AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT
Definition: log.h:41
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:503
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
avf_read_packet
static int avf_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: avfoundation.m:1117
AVFContext::width
int width
Definition: avfoundation.m:96
configure_video_device
static int configure_video_device(AVFormatContext *s, AVCaptureDevice *video_device)
Configure the video device.
Definition: avfoundation.m:343
index
int index
Definition: gxfenc.c:90
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
pthread_mutex_unlock
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:82
AVFContext::audio_buffer
int32_t * audio_buffer
Definition: avfoundation.m:125
AVFContext::video_stream_index
int video_stream_index
Definition: avfoundation.m:107
AV_CODEC_ID_PCM_S24LE
@ AV_CODEC_ID_PCM_S24LE
Definition: codec_id.h:346
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:75
AVMediaType
AVMediaType
Definition: avutil.h:199
AVPacket::size
int size
Definition: packet.h:540
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
height
#define height
Definition: dsp.h:85
destroy_context
static void destroy_context(AVFContext *ctx)
Definition: avfoundation.m:289
shift
static int shift(int a, int b)
Definition: bonk.c:261
AVFContext::url
char * url
Definition: avfoundation.m:111
AVFormatContext::url
char * url
input or output URL.
Definition: avformat.h:1416
sample
#define sample
Definition: flacdsp_template.c:44
av_make_q
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:468
AVFContext::audio_non_interleaved
int audio_non_interleaved
Definition: avfoundation.m:123
range
enum AVColorRange range
Definition: mediacodec_wrapper.c:2464
avdevice.h
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:538
AV_PIX_FMT_YUVA444P
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:174
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:545
AV_PIX_FMT_RGB0
@ AV_PIX_FMT_RGB0
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:263
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:220
pthread_mutex_destroy
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:112
av_channel_layout_default
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
Definition: channel_layout.c:837
lock_frames
static void lock_frames(AVFContext *ctx)
Definition: avfoundation.m:143
AVFContext::audio_stream_index
int audio_stream_index
Definition: avfoundation.m:109
copy_cvpixelbuffer
static int copy_cvpixelbuffer(AVFormatContext *s, CVPixelBufferRef image_buffer, AVPacket *pkt)
Definition: avfoundation.m:1074
AV_PIX_FMT_RGB555LE
@ AV_PIX_FMT_RGB555LE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined
Definition: pixfmt.h:115
AVFContext::audio_bits_per_sample
int audio_bits_per_sample
Definition: avfoundation.m:118
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:532
avf_read_header
static int avf_read_header(AVFormatContext *s)
Definition: avfoundation.m:822
internal.h
AV_CODEC_ID_DVVIDEO
@ AV_CODEC_ID_DVVIDEO
Definition: codec_id.h:76
AV_CODEC_ID_PCM_S32BE
@ AV_CODEC_ID_PCM_S32BE
Definition: codec_id.h:343
demux.h
AVFContext::frames_captured
int frames_captured
Definition: avfoundation.m:89
AVFContext::video_is_muxed
int video_is_muxed
Definition: avfoundation.m:102
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
AV_PIX_FMT_0BGR
@ AV_PIX_FMT_0BGR
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
Definition: pixfmt.h:264
AV_PIX_FMT_NV12
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:96
AVFContext::audio_device_index
int audio_device_index
Definition: avfoundation.m:108
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:80
avf_pixel_formats
static const struct AVFPixelFormatSpec avf_pixel_formats[]
Definition: avfoundation.m:56
AVFContext::audio_output
AVCaptureAudioDataOutput * audio_output
Definition: avfoundation.m:132
id
enum AVCodecID id
Definition: dts2pts.c:367
AV_PIX_FMT_UYVY422
@ AV_PIX_FMT_UYVY422
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:88
status
ov_status_e status
Definition: dnn_backend_openvino.c:100
AVFContext::avf_audio_delegate
id avf_audio_delegate
Definition: avfoundation.m:93
channel_layout.h
AVFContext::video_is_screen
int video_is_screen
Definition: avfoundation.m:103
mode
mode
Definition: ebur128.h:83
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
AV_OPT_TYPE_PIXEL_FMT
@ AV_OPT_TYPE_PIXEL_FMT
Underlying C type is enum AVPixelFormat.
Definition: opt.h:307
AVPacket::stream_index
int stream_index
Definition: packet.h:541
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
AVFContext::audio_filename
char * audio_filename
Definition: avfoundation.m:113
AV_PIX_FMT_RGB565BE
@ AV_PIX_FMT_RGB565BE
packed RGB 5:6:5, 16bpp, (msb) 5R 6G 5B(lsb), big-endian
Definition: pixfmt.h:112
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:272
AV_OPT_FLAG_DECODING_PARAM
#define AV_OPT_FLAG_DECODING_PARAM
A generic parameter which can be set by the user for demuxing or decoding.
Definition: opt.h:356
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_CODEC_ID_PCM_S32LE
@ AV_CODEC_ID_PCM_S32LE
Definition: codec_id.h:342
mem.h
get_audio_config
static int get_audio_config(AVFormatContext *s)
Definition: avfoundation.m:677
AVFContext
Definition: avfoundation.m:85
timing_info
static int FUNC() timing_info(CodedBitstreamContext *ctx, RWContext *rw, AV1RawTimingInfo *current)
Definition: cbs_av1_syntax_template.c:158
av_image_copy_to_buffer
int av_image_copy_to_buffer(uint8_t *dst, int dst_size, const uint8_t *const src_data[4], const int src_linesize[4], enum AVPixelFormat pix_fmt, int width, int height, int align)
Copy image data from an image into a buffer.
Definition: imgutils.c:501
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:327
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
FFInputFormat
Definition: demux.h:42
int32_t
int32_t
Definition: audioconvert.c:56
imgutils.h
AVFContext::video_device_index
int video_device_index
Definition: avfoundation.m:106
AV_PIX_FMT_0RGB
@ AV_PIX_FMT_0RGB
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
Definition: pixfmt.h:262
AV_CODEC_ID_PCM_F32LE
@ AV_CODEC_ID_PCM_F32LE
Definition: codec_id.h:355
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVFAudioReceiver
AudioReciever class - delegate for AVCaptureSession.
Definition: avfoundation.m:247
avstring.h
AVFContext::avf_delegate
id avf_delegate
Definition: avfoundation.m:92
AV_PIX_FMT_YUVA444P16LE
@ AV_PIX_FMT_YUVA444P16LE
planar YUV 4:4:4 64bpp, (1 Cr & Cb sample per 1x1 Y & A samples, little-endian)
Definition: pixfmt.h:192
width
#define width
Definition: dsp.h:85
avf_class
static const AVClass avf_class
Definition: avfoundation.m:1290
AVFContext::capture_session
AVCaptureSession * capture_session
Definition: avfoundation.m:130
AV_CODEC_ID_PCM_S24BE
@ AV_CODEC_ID_PCM_S24BE
Definition: codec_id.h:347
av_get_pix_fmt_name
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:3090
AV_PIX_FMT_BGR48BE
@ AV_PIX_FMT_BGR48BE
packed RGB 16:16:16, 48bpp, 16B, 16G, 16R, the 2-byte value for each R/G/B component is stored as big...
Definition: pixfmt.h:145
pthread_mutex_lock
#define pthread_mutex_lock(a)
Definition: ffprobe.c:78