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