Bug fix of wrong mp4, m4a duration
[platform/upstream/ffmpeg.git] / libavdevice / avfoundation.m
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/channel_layout.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/avstring.h"
35 #include "libavformat/internal.h"
36 #include "libavutil/internal.h"
37 #include "libavutil/parseutils.h"
38 #include "libavutil/time.h"
39 #include "libavutil/imgutils.h"
40 #include "avdevice.h"
41
42 static const int avf_time_base = 1000000;
43
44 static const AVRational avf_time_base_q = {
45     .num = 1,
46     .den = avf_time_base
47 };
48
49 struct AVFPixelFormatSpec {
50     enum AVPixelFormat ff_id;
51     OSType avf_id;
52 };
53
54 static const struct AVFPixelFormatSpec avf_pixel_formats[] = {
55     { AV_PIX_FMT_MONOBLACK,    kCVPixelFormatType_1Monochrome },
56     { AV_PIX_FMT_RGB555BE,     kCVPixelFormatType_16BE555 },
57     { AV_PIX_FMT_RGB555LE,     kCVPixelFormatType_16LE555 },
58     { AV_PIX_FMT_RGB565BE,     kCVPixelFormatType_16BE565 },
59     { AV_PIX_FMT_RGB565LE,     kCVPixelFormatType_16LE565 },
60     { AV_PIX_FMT_RGB24,        kCVPixelFormatType_24RGB },
61     { AV_PIX_FMT_BGR24,        kCVPixelFormatType_24BGR },
62     { AV_PIX_FMT_0RGB,         kCVPixelFormatType_32ARGB },
63     { AV_PIX_FMT_BGR0,         kCVPixelFormatType_32BGRA },
64     { AV_PIX_FMT_0BGR,         kCVPixelFormatType_32ABGR },
65     { AV_PIX_FMT_RGB0,         kCVPixelFormatType_32RGBA },
66     { AV_PIX_FMT_BGR48BE,      kCVPixelFormatType_48RGB },
67     { AV_PIX_FMT_UYVY422,      kCVPixelFormatType_422YpCbCr8 },
68     { AV_PIX_FMT_YUVA444P,     kCVPixelFormatType_4444YpCbCrA8R },
69     { AV_PIX_FMT_YUVA444P16LE, kCVPixelFormatType_4444AYpCbCr16 },
70     { AV_PIX_FMT_YUV444P,      kCVPixelFormatType_444YpCbCr8 },
71     { AV_PIX_FMT_YUV422P16,    kCVPixelFormatType_422YpCbCr16 },
72     { AV_PIX_FMT_YUV422P10,    kCVPixelFormatType_422YpCbCr10 },
73     { AV_PIX_FMT_YUV444P10,    kCVPixelFormatType_444YpCbCr10 },
74     { AV_PIX_FMT_YUV420P,      kCVPixelFormatType_420YpCbCr8Planar },
75     { AV_PIX_FMT_NV12,         kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange },
76     { AV_PIX_FMT_YUYV422,      kCVPixelFormatType_422YpCbCr8_yuvs },
77 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
78     { AV_PIX_FMT_GRAY8,        kCVPixelFormatType_OneComponent8 },
79 #endif
80     { AV_PIX_FMT_NONE, 0 }
81 };
82
83 typedef struct
84 {
85     AVClass*        class;
86
87     int             frames_captured;
88     int             audio_frames_captured;
89     pthread_mutex_t frame_lock;
90     id              avf_delegate;
91     id              avf_audio_delegate;
92
93     AVRational      framerate;
94     int             width, height;
95
96     int             capture_cursor;
97     int             capture_mouse_clicks;
98     int             capture_raw_data;
99     int             drop_late_frames;
100     int             video_is_muxed;
101     int             video_is_screen;
102
103     int             list_devices;
104     int             video_device_index;
105     int             video_stream_index;
106     int             audio_device_index;
107     int             audio_stream_index;
108
109     char            *url;
110     char            *video_filename;
111     char            *audio_filename;
112
113     int             num_video_devices;
114
115     int             audio_channels;
116     int             audio_bits_per_sample;
117     int             audio_float;
118     int             audio_be;
119     int             audio_signed_integer;
120     int             audio_packed;
121     int             audio_non_interleaved;
122
123     int32_t         *audio_buffer;
124     int             audio_buffer_size;
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
138     int                      observed_quit;
139 } AVFContext;
140
141 static void lock_frames(AVFContext* ctx)
142 {
143     pthread_mutex_lock(&ctx->frame_lock);
144 }
145
146 static void unlock_frames(AVFContext* ctx)
147 {
148     pthread_mutex_unlock(&ctx->frame_lock);
149 }
150
151 /** FrameReciever class - delegate for AVCaptureSession
152  */
153 @interface AVFFrameReceiver : NSObject
154 {
155     AVFContext* _context;
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 {
228     lock_frames(_context);
229
230     if (_context->current_frame != nil) {
231         CFRelease(_context->current_frame);
232     }
233
234     _context->current_frame = (CMSampleBufferRef)CFRetain(videoFrame);
235
236     unlock_frames(_context);
237
238     ++_context->frames_captured;
239 }
240
241 @end
242
243 /** AudioReciever class - delegate for AVCaptureSession
244  */
245 @interface AVFAudioReceiver : NSObject
246 {
247     AVFContext* _context;
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 {
272     lock_frames(_context);
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
280     unlock_frames(_context);
281
282     ++_context->audio_frames_captured;
283 }
284
285 @end
286
287 static void destroy_context(AVFContext* ctx)
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->url);
304     av_freep(&ctx->audio_buffer);
305
306     pthread_mutex_destroy(&ctx->frame_lock);
307
308     if (ctx->current_frame) {
309         CFRelease(ctx->current_frame);
310     }
311 }
312
313 static int parse_device_name(AVFormatContext *s)
314 {
315     AVFContext *ctx = (AVFContext*)s->priv_data;
316     char *save;
317
318     ctx->url = av_strdup(s->url);
319
320     if (!ctx->url)
321         return AVERROR(ENOMEM);
322     if (ctx->url[0] != ':') {
323         ctx->video_filename = av_strtok(ctx->url,  ":", &save);
324         ctx->audio_filename = av_strtok(NULL, ":", &save);
325     } else {
326         ctx->audio_filename = av_strtok(ctx->url,  ":", &save);
327     }
328     return 0;
329 }
330
331 /**
332  * Configure the video device.
333  *
334  * Configure the video device using a run-time approach to access properties
335  * since formats, activeFormat are available since  iOS >= 7.0 or OSX >= 10.7
336  * and activeVideoMaxFrameDuration is available since i0S >= 7.0 and OSX >= 10.9.
337  *
338  * The NSUndefinedKeyException must be handled by the caller of this function.
339  *
340  */
341 static int configure_video_device(AVFormatContext *s, AVCaptureDevice *video_device)
342 {
343     AVFContext *ctx = (AVFContext*)s->priv_data;
344
345     double framerate = av_q2d(ctx->framerate);
346     NSObject *range = nil;
347     NSObject *format = nil;
348     NSObject *selected_range = nil;
349     NSObject *selected_format = nil;
350
351     // try to configure format by formats list
352     // might raise an exception if no format list is given
353     // (then fallback to default, no configuration)
354     @try {
355         for (format in [video_device valueForKey:@"formats"]) {
356             CMFormatDescriptionRef formatDescription;
357             CMVideoDimensions dimensions;
358
359             formatDescription = (CMFormatDescriptionRef) [format performSelector:@selector(formatDescription)];
360             dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
361
362             if ((ctx->width == 0 && ctx->height == 0) ||
363                 (dimensions.width == ctx->width && dimensions.height == ctx->height)) {
364
365                 selected_format = format;
366
367                 for (range in [format valueForKey:@"videoSupportedFrameRateRanges"]) {
368                     double max_framerate;
369
370                     [[range valueForKey:@"maxFrameRate"] getValue:&max_framerate];
371                     if (fabs (framerate - max_framerate) < 0.01) {
372                         selected_range = range;
373                         break;
374                     }
375                 }
376             }
377         }
378
379         if (!selected_format) {
380             av_log(s, AV_LOG_ERROR, "Selected video size (%dx%d) is not supported by the device.\n",
381                 ctx->width, ctx->height);
382             goto unsupported_format;
383         }
384
385         if (!selected_range) {
386             av_log(s, AV_LOG_ERROR, "Selected framerate (%f) is not supported by the device.\n",
387                 framerate);
388             if (ctx->video_is_muxed) {
389                 av_log(s, AV_LOG_ERROR, "Falling back to default.\n");
390             } else {
391                 goto unsupported_format;
392             }
393         }
394
395         if ([video_device lockForConfiguration:NULL] == YES) {
396             if (selected_format) {
397                 [video_device setValue:selected_format forKey:@"activeFormat"];
398             }
399             if (selected_range) {
400                 NSValue *min_frame_duration = [selected_range valueForKey:@"minFrameDuration"];
401                 [video_device setValue:min_frame_duration forKey:@"activeVideoMinFrameDuration"];
402                 [video_device setValue:min_frame_duration forKey:@"activeVideoMaxFrameDuration"];
403             }
404         } else {
405             av_log(s, AV_LOG_ERROR, "Could not lock device for configuration.\n");
406             return AVERROR(EINVAL);
407         }
408     } @catch(NSException *e) {
409         av_log(ctx, AV_LOG_WARNING, "Configuration of video device failed, falling back to default.\n");
410     }
411
412     return 0;
413
414 unsupported_format:
415
416     av_log(s, AV_LOG_ERROR, "Supported modes:\n");
417     for (format in [video_device valueForKey:@"formats"]) {
418         CMFormatDescriptionRef formatDescription;
419         CMVideoDimensions dimensions;
420
421         formatDescription = (CMFormatDescriptionRef) [format performSelector:@selector(formatDescription)];
422         dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
423
424         for (range in [format valueForKey:@"videoSupportedFrameRateRanges"]) {
425             double min_framerate;
426             double max_framerate;
427
428             [[range valueForKey:@"minFrameRate"] getValue:&min_framerate];
429             [[range valueForKey:@"maxFrameRate"] getValue:&max_framerate];
430             av_log(s, AV_LOG_ERROR, "  %dx%d@[%f %f]fps\n",
431                 dimensions.width, dimensions.height,
432                 min_framerate, max_framerate);
433         }
434     }
435     return AVERROR(EINVAL);
436 }
437
438 static int add_video_device(AVFormatContext *s, AVCaptureDevice *video_device)
439 {
440     AVFContext *ctx = (AVFContext*)s->priv_data;
441     int ret;
442     NSError *error  = nil;
443     AVCaptureInput* capture_input = nil;
444     struct AVFPixelFormatSpec pxl_fmt_spec;
445     NSNumber *pixel_format;
446     NSDictionary *capture_dict;
447     dispatch_queue_t queue;
448
449     if (ctx->video_device_index < ctx->num_video_devices) {
450         capture_input = (AVCaptureInput*) [[[AVCaptureDeviceInput alloc] initWithDevice:video_device error:&error] autorelease];
451     } else {
452         capture_input = (AVCaptureInput*) video_device;
453     }
454
455     if (!capture_input) {
456         av_log(s, AV_LOG_ERROR, "Failed to create AV capture input device: %s\n",
457                [[error localizedDescription] UTF8String]);
458         return 1;
459     }
460
461     if ([ctx->capture_session canAddInput:capture_input]) {
462         [ctx->capture_session addInput:capture_input];
463     } else {
464         av_log(s, AV_LOG_ERROR, "can't add video input to capture session\n");
465         return 1;
466     }
467
468     // Attaching output
469     ctx->video_output = [[AVCaptureVideoDataOutput alloc] init];
470
471     if (!ctx->video_output) {
472         av_log(s, AV_LOG_ERROR, "Failed to init AV video output\n");
473         return 1;
474     }
475
476     // Configure device framerate and video size
477     @try {
478         if ((ret = configure_video_device(s, video_device)) < 0) {
479             return ret;
480         }
481     } @catch (NSException *exception) {
482         if (![[exception name] isEqualToString:NSUndefinedKeyException]) {
483           av_log (s, AV_LOG_ERROR, "An error occurred: %s", [exception.reason UTF8String]);
484           return AVERROR_EXTERNAL;
485         }
486     }
487
488     // select pixel format
489     pxl_fmt_spec.ff_id = AV_PIX_FMT_NONE;
490
491     for (int i = 0; avf_pixel_formats[i].ff_id != AV_PIX_FMT_NONE; i++) {
492         if (ctx->pixel_format == avf_pixel_formats[i].ff_id) {
493             pxl_fmt_spec = avf_pixel_formats[i];
494             break;
495         }
496     }
497
498     // check if selected pixel format is supported by AVFoundation
499     if (pxl_fmt_spec.ff_id == AV_PIX_FMT_NONE) {
500         av_log(s, AV_LOG_ERROR, "Selected pixel format (%s) is not supported by AVFoundation.\n",
501                av_get_pix_fmt_name(pxl_fmt_spec.ff_id));
502         return 1;
503     }
504
505     // check if the pixel format is available for this device
506     if ([[ctx->video_output availableVideoCVPixelFormatTypes] indexOfObject:[NSNumber numberWithInt:pxl_fmt_spec.avf_id]] == NSNotFound) {
507         av_log(s, AV_LOG_ERROR, "Selected pixel format (%s) is not supported by the input device.\n",
508                av_get_pix_fmt_name(pxl_fmt_spec.ff_id));
509
510         pxl_fmt_spec.ff_id = AV_PIX_FMT_NONE;
511
512         av_log(s, AV_LOG_ERROR, "Supported pixel formats:\n");
513         for (NSNumber *pxl_fmt in [ctx->video_output availableVideoCVPixelFormatTypes]) {
514             struct AVFPixelFormatSpec pxl_fmt_dummy;
515             pxl_fmt_dummy.ff_id = AV_PIX_FMT_NONE;
516             for (int i = 0; avf_pixel_formats[i].ff_id != AV_PIX_FMT_NONE; i++) {
517                 if ([pxl_fmt intValue] == avf_pixel_formats[i].avf_id) {
518                     pxl_fmt_dummy = avf_pixel_formats[i];
519                     break;
520                 }
521             }
522
523             if (pxl_fmt_dummy.ff_id != AV_PIX_FMT_NONE) {
524                 av_log(s, AV_LOG_ERROR, "  %s\n", av_get_pix_fmt_name(pxl_fmt_dummy.ff_id));
525
526                 // select first supported pixel format instead of user selected (or default) pixel format
527                 if (pxl_fmt_spec.ff_id == AV_PIX_FMT_NONE) {
528                     pxl_fmt_spec = pxl_fmt_dummy;
529                 }
530             }
531         }
532
533         // fail if there is no appropriate pixel format or print a warning about overriding the pixel format
534         if (pxl_fmt_spec.ff_id == AV_PIX_FMT_NONE) {
535             return 1;
536         } else {
537             av_log(s, AV_LOG_WARNING, "Overriding selected pixel format to use %s instead.\n",
538                    av_get_pix_fmt_name(pxl_fmt_spec.ff_id));
539         }
540     }
541
542     // set videoSettings to an empty dict for receiving raw data of muxed devices
543     if (ctx->capture_raw_data) {
544         ctx->pixel_format = pxl_fmt_spec.ff_id;
545         ctx->video_output.videoSettings = @{ };
546     } else {
547         ctx->pixel_format = pxl_fmt_spec.ff_id;
548         pixel_format = [NSNumber numberWithUnsignedInt:pxl_fmt_spec.avf_id];
549         capture_dict = [NSDictionary dictionaryWithObject:pixel_format
550                                                    forKey:(id)kCVPixelBufferPixelFormatTypeKey];
551
552         [ctx->video_output setVideoSettings:capture_dict];
553     }
554     [ctx->video_output setAlwaysDiscardsLateVideoFrames:ctx->drop_late_frames];
555
556 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
557     // check for transport control support and set observer device if supported
558     if (!ctx->video_is_screen) {
559         int trans_ctrl = [video_device transportControlsSupported];
560         AVCaptureDeviceTransportControlsPlaybackMode trans_mode = [video_device transportControlsPlaybackMode];
561
562         if (trans_ctrl) {
563             ctx->observed_mode   = trans_mode;
564             ctx->observed_device = video_device;
565         }
566     }
567 #endif
568
569     ctx->avf_delegate = [[AVFFrameReceiver alloc] initWithContext:ctx];
570
571     queue = dispatch_queue_create("avf_queue", NULL);
572     [ctx->video_output setSampleBufferDelegate:ctx->avf_delegate queue:queue];
573     dispatch_release(queue);
574
575     if ([ctx->capture_session canAddOutput:ctx->video_output]) {
576         [ctx->capture_session addOutput:ctx->video_output];
577     } else {
578         av_log(s, AV_LOG_ERROR, "can't add video output to capture session\n");
579         return 1;
580     }
581
582     return 0;
583 }
584
585 static int add_audio_device(AVFormatContext *s, AVCaptureDevice *audio_device)
586 {
587     AVFContext *ctx = (AVFContext*)s->priv_data;
588     NSError *error  = nil;
589     AVCaptureDeviceInput* audio_dev_input = [[[AVCaptureDeviceInput alloc] initWithDevice:audio_device error:&error] autorelease];
590     dispatch_queue_t queue;
591
592     if (!audio_dev_input) {
593         av_log(s, AV_LOG_ERROR, "Failed to create AV capture input device: %s\n",
594                [[error localizedDescription] UTF8String]);
595         return 1;
596     }
597
598     if ([ctx->capture_session canAddInput:audio_dev_input]) {
599         [ctx->capture_session addInput:audio_dev_input];
600     } else {
601         av_log(s, AV_LOG_ERROR, "can't add audio input to capture session\n");
602         return 1;
603     }
604
605     // Attaching output
606     ctx->audio_output = [[AVCaptureAudioDataOutput alloc] init];
607
608     if (!ctx->audio_output) {
609         av_log(s, AV_LOG_ERROR, "Failed to init AV audio output\n");
610         return 1;
611     }
612
613     ctx->avf_audio_delegate = [[AVFAudioReceiver alloc] initWithContext:ctx];
614
615     queue = dispatch_queue_create("avf_audio_queue", NULL);
616     [ctx->audio_output setSampleBufferDelegate:ctx->avf_audio_delegate queue:queue];
617     dispatch_release(queue);
618
619     if ([ctx->capture_session canAddOutput:ctx->audio_output]) {
620         [ctx->capture_session addOutput:ctx->audio_output];
621     } else {
622         av_log(s, AV_LOG_ERROR, "adding audio output to capture session failed\n");
623         return 1;
624     }
625
626     return 0;
627 }
628
629 static int get_video_config(AVFormatContext *s)
630 {
631     AVFContext *ctx = (AVFContext*)s->priv_data;
632     CVImageBufferRef image_buffer;
633     CMBlockBufferRef block_buffer;
634     CGSize image_buffer_size;
635     AVStream* stream = avformat_new_stream(s, NULL);
636
637     if (!stream) {
638         return 1;
639     }
640
641     // Take stream info from the first frame.
642     while (ctx->frames_captured < 1) {
643         CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
644     }
645
646     lock_frames(ctx);
647
648     ctx->video_stream_index = stream->index;
649
650     avpriv_set_pts_info(stream, 64, 1, avf_time_base);
651
652     image_buffer = CMSampleBufferGetImageBuffer(ctx->current_frame);
653     block_buffer = CMSampleBufferGetDataBuffer(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
672     unlock_frames(ctx);
673
674     return 0;
675 }
676
677 static int get_audio_config(AVFormatContext *s)
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) {
702         unlock_frames(ctx);
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 {
740         unlock_frames(ctx);
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) {
750             unlock_frames(ctx);
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
759     unlock_frames(ctx);
760
761     return 0;
762 }
763
764 static int avf_read_header(AVFormatContext *s)
765 {
766     int ret = 0;
767     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
768     uint32_t num_screens    = 0;
769     AVFContext *ctx         = (AVFContext*)s->priv_data;
770     AVCaptureDevice *video_device = nil;
771     AVCaptureDevice *audio_device = nil;
772     // Find capture device
773     NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
774     NSArray *devices_muxed = [AVCaptureDevice devicesWithMediaType:AVMediaTypeMuxed];
775
776     ctx->num_video_devices = [devices count] + [devices_muxed count];
777
778     pthread_mutex_init(&ctx->frame_lock, NULL);
779
780 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
781     CGGetActiveDisplayList(0, NULL, &num_screens);
782 #endif
783
784     // List devices if requested
785     if (ctx->list_devices) {
786         int index = 0;
787         av_log(ctx, AV_LOG_INFO, "AVFoundation video devices:\n");
788         for (AVCaptureDevice *device in devices) {
789             const char *name = [[device localizedName] UTF8String];
790             index            = [devices indexOfObject:device];
791             av_log(ctx, AV_LOG_INFO, "[%d] %s\n", index, name);
792         }
793         for (AVCaptureDevice *device in devices_muxed) {
794             const char *name = [[device localizedName] UTF8String];
795             index            = [devices count] + [devices_muxed indexOfObject:device];
796             av_log(ctx, AV_LOG_INFO, "[%d] %s\n", index, name);
797         }
798 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
799         if (num_screens > 0) {
800             CGDirectDisplayID screens[num_screens];
801             CGGetActiveDisplayList(num_screens, screens, &num_screens);
802             for (int i = 0; i < num_screens; i++) {
803                 av_log(ctx, AV_LOG_INFO, "[%d] Capture screen %d\n", ctx->num_video_devices + i, i);
804             }
805         }
806 #endif
807
808         av_log(ctx, AV_LOG_INFO, "AVFoundation audio devices:\n");
809         devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
810         for (AVCaptureDevice *device in devices) {
811             const char *name = [[device localizedName] UTF8String];
812             int index  = [devices indexOfObject:device];
813             av_log(ctx, AV_LOG_INFO, "[%d] %s\n", index, name);
814         }
815          goto fail;
816     }
817
818     // parse input filename for video and audio device
819     ret = parse_device_name(s);
820     if (ret)
821         goto fail;
822
823     // check for device index given in filename
824     if (ctx->video_device_index == -1 && ctx->video_filename) {
825         sscanf(ctx->video_filename, "%d", &ctx->video_device_index);
826     }
827     if (ctx->audio_device_index == -1 && ctx->audio_filename) {
828         sscanf(ctx->audio_filename, "%d", &ctx->audio_device_index);
829     }
830
831     if (ctx->video_device_index >= 0) {
832         if (ctx->video_device_index < ctx->num_video_devices) {
833             if (ctx->video_device_index < [devices count]) {
834                 video_device = [devices objectAtIndex:ctx->video_device_index];
835             } else {
836                 video_device = [devices_muxed objectAtIndex:(ctx->video_device_index - [devices count])];
837                 ctx->video_is_muxed = 1;
838             }
839         } else if (ctx->video_device_index < ctx->num_video_devices + num_screens) {
840 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
841             CGDirectDisplayID screens[num_screens];
842             CGGetActiveDisplayList(num_screens, screens, &num_screens);
843             AVCaptureScreenInput* capture_screen_input = [[[AVCaptureScreenInput alloc] initWithDisplayID:screens[ctx->video_device_index - ctx->num_video_devices]] autorelease];
844
845             if (ctx->framerate.num > 0) {
846                 capture_screen_input.minFrameDuration = CMTimeMake(ctx->framerate.den, ctx->framerate.num);
847             }
848
849 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
850             if (ctx->capture_cursor) {
851                 capture_screen_input.capturesCursor = YES;
852             } else {
853                 capture_screen_input.capturesCursor = NO;
854             }
855 #endif
856
857             if (ctx->capture_mouse_clicks) {
858                 capture_screen_input.capturesMouseClicks = YES;
859             } else {
860                 capture_screen_input.capturesMouseClicks = NO;
861             }
862
863             video_device = (AVCaptureDevice*) capture_screen_input;
864             ctx->video_is_screen = 1;
865 #endif
866          } else {
867             av_log(ctx, AV_LOG_ERROR, "Invalid device index\n");
868             goto fail;
869         }
870     } else if (ctx->video_filename &&
871                strncmp(ctx->video_filename, "none", 4)) {
872         if (!strncmp(ctx->video_filename, "default", 7)) {
873             video_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
874         } else {
875         // looking for video inputs
876         for (AVCaptureDevice *device in devices) {
877             if (!strncmp(ctx->video_filename, [[device localizedName] UTF8String], strlen(ctx->video_filename))) {
878                 video_device = device;
879                 break;
880             }
881         }
882         // looking for muxed inputs
883         for (AVCaptureDevice *device in devices_muxed) {
884             if (!strncmp(ctx->video_filename, [[device localizedName] UTF8String], strlen(ctx->video_filename))) {
885                 video_device = device;
886                 ctx->video_is_muxed = 1;
887                 break;
888             }
889         }
890
891 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
892         // looking for screen inputs
893         if (!video_device) {
894             int idx;
895             if(sscanf(ctx->video_filename, "Capture screen %d", &idx) && idx < num_screens) {
896                 CGDirectDisplayID screens[num_screens];
897                 CGGetActiveDisplayList(num_screens, screens, &num_screens);
898                 AVCaptureScreenInput* capture_screen_input = [[[AVCaptureScreenInput alloc] initWithDisplayID:screens[idx]] autorelease];
899                 video_device = (AVCaptureDevice*) capture_screen_input;
900                 ctx->video_device_index = ctx->num_video_devices + idx;
901                 ctx->video_is_screen = 1;
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         }
922 #endif
923         }
924
925         if (!video_device) {
926             av_log(ctx, AV_LOG_ERROR, "Video device not found\n");
927             goto fail;
928         }
929     }
930
931     // get audio device
932     if (ctx->audio_device_index >= 0) {
933         NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
934
935         if (ctx->audio_device_index >= [devices count]) {
936             av_log(ctx, AV_LOG_ERROR, "Invalid audio device index\n");
937             goto fail;
938         }
939
940         audio_device = [devices objectAtIndex:ctx->audio_device_index];
941     } else if (ctx->audio_filename &&
942                strncmp(ctx->audio_filename, "none", 4)) {
943         if (!strncmp(ctx->audio_filename, "default", 7)) {
944             audio_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
945         } else {
946         NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio];
947
948         for (AVCaptureDevice *device in devices) {
949             if (!strncmp(ctx->audio_filename, [[device localizedName] UTF8String], strlen(ctx->audio_filename))) {
950                 audio_device = device;
951                 break;
952             }
953         }
954         }
955
956         if (!audio_device) {
957             av_log(ctx, AV_LOG_ERROR, "Audio device not found\n");
958              goto fail;
959         }
960     }
961
962     // Video nor Audio capture device not found, looking for AVMediaTypeVideo/Audio
963     if (!video_device && !audio_device) {
964         av_log(s, AV_LOG_ERROR, "No AV capture device found\n");
965         goto fail;
966     }
967
968     if (video_device) {
969         if (ctx->video_device_index < ctx->num_video_devices) {
970             av_log(s, AV_LOG_DEBUG, "'%s' opened\n", [[video_device localizedName] UTF8String]);
971         } else {
972             av_log(s, AV_LOG_DEBUG, "'%s' opened\n", [[video_device description] UTF8String]);
973         }
974     }
975     if (audio_device) {
976         av_log(s, AV_LOG_DEBUG, "audio device '%s' opened\n", [[audio_device localizedName] UTF8String]);
977     }
978
979     // Initialize capture session
980     ctx->capture_session = [[AVCaptureSession alloc] init];
981
982     if (video_device && add_video_device(s, video_device)) {
983         goto fail;
984     }
985     if (audio_device && add_audio_device(s, audio_device)) {
986     }
987
988     [ctx->capture_session startRunning];
989
990     /* Unlock device configuration only after the session is started so it
991      * does not reset the capture formats */
992     if (!ctx->video_is_screen) {
993         [video_device unlockForConfiguration];
994     }
995
996     if (video_device && get_video_config(s)) {
997         goto fail;
998     }
999
1000     // set audio stream
1001     if (audio_device && get_audio_config(s)) {
1002         goto fail;
1003     }
1004
1005     [pool release];
1006     return 0;
1007
1008 fail:
1009     [pool release];
1010     destroy_context(ctx);
1011     if (ret)
1012         return ret;
1013     return AVERROR(EIO);
1014 }
1015
1016 static int copy_cvpixelbuffer(AVFormatContext *s,
1017                                CVPixelBufferRef image_buffer,
1018                                AVPacket *pkt)
1019 {
1020     AVFContext *ctx = s->priv_data;
1021     int src_linesize[4];
1022     const uint8_t *src_data[4];
1023     int width  = CVPixelBufferGetWidth(image_buffer);
1024     int height = CVPixelBufferGetHeight(image_buffer);
1025     int status;
1026
1027     memset(src_linesize, 0, sizeof(src_linesize));
1028     memset(src_data, 0, sizeof(src_data));
1029
1030     status = CVPixelBufferLockBaseAddress(image_buffer, 0);
1031     if (status != kCVReturnSuccess) {
1032         av_log(s, AV_LOG_ERROR, "Could not lock base address: %d (%dx%d)\n", status, width, height);
1033         return AVERROR_EXTERNAL;
1034     }
1035
1036     if (CVPixelBufferIsPlanar(image_buffer)) {
1037         size_t plane_count = CVPixelBufferGetPlaneCount(image_buffer);
1038         int i;
1039         for(i = 0; i < plane_count; i++){
1040             src_linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(image_buffer, i);
1041             src_data[i] = CVPixelBufferGetBaseAddressOfPlane(image_buffer, i);
1042         }
1043     } else {
1044         src_linesize[0] = CVPixelBufferGetBytesPerRow(image_buffer);
1045         src_data[0] = CVPixelBufferGetBaseAddress(image_buffer);
1046     }
1047
1048     status = av_image_copy_to_buffer(pkt->data, pkt->size,
1049                                      src_data, src_linesize,
1050                                      ctx->pixel_format, width, height, 1);
1051
1052
1053
1054     CVPixelBufferUnlockBaseAddress(image_buffer, 0);
1055
1056     return status;
1057 }
1058
1059 static int avf_read_packet(AVFormatContext *s, AVPacket *pkt)
1060 {
1061     AVFContext* ctx = (AVFContext*)s->priv_data;
1062
1063     do {
1064         CVImageBufferRef image_buffer;
1065         CMBlockBufferRef block_buffer;
1066         lock_frames(ctx);
1067
1068         if (ctx->current_frame != nil) {
1069             int status;
1070             int length = 0;
1071
1072             image_buffer = CMSampleBufferGetImageBuffer(ctx->current_frame);
1073             block_buffer = CMSampleBufferGetDataBuffer(ctx->current_frame);
1074
1075             if (image_buffer != nil) {
1076                 length = (int)CVPixelBufferGetDataSize(image_buffer);
1077             } else if (block_buffer != nil) {
1078                 length = (int)CMBlockBufferGetDataLength(block_buffer);
1079             } else  {
1080                 unlock_frames(ctx);
1081                 return AVERROR(EINVAL);
1082             }
1083
1084             if (av_new_packet(pkt, length) < 0) {
1085                 unlock_frames(ctx);
1086                 return AVERROR(EIO);
1087             }
1088
1089             CMItemCount count;
1090             CMSampleTimingInfo timing_info;
1091
1092             if (CMSampleBufferGetOutputSampleTimingInfoArray(ctx->current_frame, 1, &timing_info, &count) == noErr) {
1093                 AVRational timebase_q = av_make_q(1, timing_info.presentationTimeStamp.timescale);
1094                 pkt->pts = pkt->dts = av_rescale_q(timing_info.presentationTimeStamp.value, timebase_q, avf_time_base_q);
1095             }
1096
1097             pkt->stream_index  = ctx->video_stream_index;
1098             pkt->flags        |= AV_PKT_FLAG_KEY;
1099
1100             if (image_buffer) {
1101                 status = copy_cvpixelbuffer(s, image_buffer, pkt);
1102             } else {
1103                 status = 0;
1104                 OSStatus ret = CMBlockBufferCopyDataBytes(block_buffer, 0, pkt->size, pkt->data);
1105                 if (ret != kCMBlockBufferNoErr) {
1106                     status = AVERROR(EIO);
1107                 }
1108              }
1109             CFRelease(ctx->current_frame);
1110             ctx->current_frame = nil;
1111
1112             if (status < 0) {
1113                 unlock_frames(ctx);
1114                 return status;
1115             }
1116         } else if (ctx->current_audio_frame != nil) {
1117             CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(ctx->current_audio_frame);
1118             int block_buffer_size         = CMBlockBufferGetDataLength(block_buffer);
1119
1120             if (!block_buffer || !block_buffer_size) {
1121                 unlock_frames(ctx);
1122                 return AVERROR(EIO);
1123             }
1124
1125             if (ctx->audio_non_interleaved && block_buffer_size > ctx->audio_buffer_size) {
1126                 unlock_frames(ctx);
1127                 return AVERROR_BUFFER_TOO_SMALL;
1128             }
1129
1130             if (av_new_packet(pkt, block_buffer_size) < 0) {
1131                 unlock_frames(ctx);
1132                 return AVERROR(EIO);
1133             }
1134
1135             CMItemCount count;
1136             CMSampleTimingInfo timing_info;
1137
1138             if (CMSampleBufferGetOutputSampleTimingInfoArray(ctx->current_audio_frame, 1, &timing_info, &count) == noErr) {
1139                 AVRational timebase_q = av_make_q(1, timing_info.presentationTimeStamp.timescale);
1140                 pkt->pts = pkt->dts = av_rescale_q(timing_info.presentationTimeStamp.value, timebase_q, avf_time_base_q);
1141             }
1142
1143             pkt->stream_index  = ctx->audio_stream_index;
1144             pkt->flags        |= AV_PKT_FLAG_KEY;
1145
1146             if (ctx->audio_non_interleaved) {
1147                 int sample, c, shift, num_samples;
1148
1149                 OSStatus ret = CMBlockBufferCopyDataBytes(block_buffer, 0, pkt->size, ctx->audio_buffer);
1150                 if (ret != kCMBlockBufferNoErr) {
1151                     unlock_frames(ctx);
1152                     return AVERROR(EIO);
1153                 }
1154
1155                 num_samples = pkt->size / (ctx->audio_channels * (ctx->audio_bits_per_sample >> 3));
1156
1157                 // transform decoded frame into output format
1158                 #define INTERLEAVE_OUTPUT(bps)                                         \
1159                 {                                                                      \
1160                     int##bps##_t **src;                                                \
1161                     int##bps##_t *dest;                                                \
1162                     src = av_malloc(ctx->audio_channels * sizeof(int##bps##_t*));      \
1163                     if (!src) {                                                        \
1164                         unlock_frames(ctx);                                            \
1165                         return AVERROR(EIO);                                           \
1166                     }                                                                  \
1167                                                                                        \
1168                     for (c = 0; c < ctx->audio_channels; c++) {                        \
1169                         src[c] = ((int##bps##_t*)ctx->audio_buffer) + c * num_samples; \
1170                     }                                                                  \
1171                     dest  = (int##bps##_t*)pkt->data;                                  \
1172                     shift = bps - ctx->audio_bits_per_sample;                          \
1173                     for (sample = 0; sample < num_samples; sample++)                   \
1174                         for (c = 0; c < ctx->audio_channels; c++)                      \
1175                             *dest++ = src[c][sample] << shift;                         \
1176                     av_freep(&src);                                                    \
1177                 }
1178
1179                 if (ctx->audio_bits_per_sample <= 16) {
1180                     INTERLEAVE_OUTPUT(16)
1181                 } else {
1182                     INTERLEAVE_OUTPUT(32)
1183                 }
1184             } else {
1185                 OSStatus ret = CMBlockBufferCopyDataBytes(block_buffer, 0, pkt->size, pkt->data);
1186                 if (ret != kCMBlockBufferNoErr) {
1187                     unlock_frames(ctx);
1188                     return AVERROR(EIO);
1189                 }
1190             }
1191
1192             CFRelease(ctx->current_audio_frame);
1193             ctx->current_audio_frame = nil;
1194         } else {
1195             pkt->data = NULL;
1196             unlock_frames(ctx);
1197             if (ctx->observed_quit) {
1198                 return AVERROR_EOF;
1199             } else {
1200                 return AVERROR(EAGAIN);
1201             }
1202         }
1203
1204         unlock_frames(ctx);
1205     } while (!pkt->data);
1206
1207     return 0;
1208 }
1209
1210 static int avf_close(AVFormatContext *s)
1211 {
1212     AVFContext* ctx = (AVFContext*)s->priv_data;
1213     destroy_context(ctx);
1214     return 0;
1215 }
1216
1217 static const AVOption options[] = {
1218     { "list_devices", "list available devices", offsetof(AVFContext, list_devices), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
1219     { "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 },
1220     { "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 },
1221     { "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},
1222     { "framerate", "set frame rate", offsetof(AVFContext, framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "ntsc"}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
1223     { "video_size", "set video size", offsetof(AVFContext, width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
1224     { "capture_cursor", "capture the screen cursor", offsetof(AVFContext, capture_cursor), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
1225     { "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 },
1226     { "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 },
1227     { "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 },
1228
1229     { NULL },
1230 };
1231
1232 static const AVClass avf_class = {
1233     .class_name = "AVFoundation indev",
1234     .item_name  = av_default_item_name,
1235     .option     = options,
1236     .version    = LIBAVUTIL_VERSION_INT,
1237     .category   = AV_CLASS_CATEGORY_DEVICE_VIDEO_INPUT,
1238 };
1239
1240 const AVInputFormat ff_avfoundation_demuxer = {
1241     .name           = "avfoundation",
1242     .long_name      = NULL_IF_CONFIG_SMALL("AVFoundation input device"),
1243     .priv_data_size = sizeof(AVFContext),
1244     .read_header    = avf_read_header,
1245     .read_packet    = avf_read_packet,
1246     .read_close     = avf_close,
1247     .flags          = AVFMT_NOFILE,
1248     .priv_class     = &avf_class,
1249 };