28 #import <AVFoundation/AVFoundation.h>
79 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
137 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
138 AVCaptureDeviceTransportControlsPlaybackMode observed_mode;
162 - (void) captureOutput:(AVCaptureOutput *)captureOutput
163 didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
164 fromConnection:(AVCaptureConnection *)connection;
172 if (
self = [super
init]) {
176 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
178 NSString *keyPath = NSStringFromSelector(
@selector(transportControlsPlaybackMode));
179 NSKeyValueObservingOptions
options = NSKeyValueObservingOptionNew;
181 [
_context->observed_device addObserver: self
193 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
195 NSString *keyPath = NSStringFromSelector(
@selector(transportControlsPlaybackMode));
196 [_context->observed_device removeObserver: self forKeyPath: keyPath];
202 - (void)observeValueForKeyPath:(NSString *)keyPath
204 change:(NSDictionary *)change
205 context:(
void *)context {
207 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
208 AVCaptureDeviceTransportControlsPlaybackMode
mode =
209 [change[NSKeyValueChangeNewKey] integerValue];
212 if (
mode == AVCaptureDeviceTransportControlsNotPlayingMode) {
219 [
super observeValueForKeyPath: keyPath
226 - (void) captureOutput:(AVCaptureOutput *)captureOutput
227 didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
228 fromConnection:(AVCaptureConnection *)connection
254 - (void) captureOutput:(AVCaptureOutput *)captureOutput
255 didOutputSampleBuffer:(CMSampleBufferRef)audioFrame
256 fromConnection:(AVCaptureConnection *)connection;
264 if (
self = [super
init]) {
270 - (void) captureOutput:(AVCaptureOutput *)captureOutput
271 didOutputSampleBuffer:(CMSampleBufferRef)audioFrame
272 fromConnection:(AVCaptureConnection *)connection
291 [ctx->capture_session stopRunning];
293 [ctx->capture_session release];
294 [ctx->video_output release];
295 [ctx->audio_output release];
296 [ctx->avf_delegate release];
297 [ctx->avf_audio_delegate release];
303 ctx->avf_audio_delegate =
NULL;
310 if (
ctx->current_frame) {
311 CFRelease(
ctx->current_frame);
348 NSObject *
range = nil;
350 NSObject *selected_range = nil;
351 NSObject *selected_format = nil;
357 for (
format in [video_device valueForKey:
@"formats"]) {
358 CMFormatDescriptionRef formatDescription;
359 CMVideoDimensions dimensions;
361 formatDescription = (CMFormatDescriptionRef) [
format performSelector:
@selector(formatDescription)];
362 dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
364 if ((
ctx->width == 0 &&
ctx->height == 0) ||
365 (dimensions.width ==
ctx->width && dimensions.height ==
ctx->height)) {
369 for (
range in [
format valueForKey:
@"videoSupportedFrameRateRanges"]) {
370 double max_framerate;
372 [[range valueForKey:@"maxFrameRate"] getValue:&max_framerate];
374 selected_range =
range;
381 if (!selected_format) {
384 goto unsupported_format;
387 if (!selected_range) {
390 if (
ctx->video_is_muxed) {
393 goto unsupported_format;
397 if ([video_device lockForConfiguration:
NULL] == YES) {
398 if (selected_format) {
399 [video_device setValue:selected_format forKey:@"activeFormat"];
401 if (selected_range) {
402 NSValue *min_frame_duration = [selected_range valueForKey:@"minFrameDuration"];
403 [video_device setValue:min_frame_duration forKey:@"activeVideoMinFrameDuration"];
404 [video_device setValue:min_frame_duration forKey:@"activeVideoMaxFrameDuration"];
410 }
@catch(NSException *e) {
419 for (
format in [video_device valueForKey:
@"formats"]) {
420 CMFormatDescriptionRef formatDescription;
421 CMVideoDimensions dimensions;
423 formatDescription = (CMFormatDescriptionRef) [
format performSelector:
@selector(formatDescription)];
424 dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
426 for (
range in [
format valueForKey:
@"videoSupportedFrameRateRanges"]) {
427 double min_framerate;
428 double max_framerate;
430 [[range valueForKey:@"minFrameRate"] getValue:&min_framerate];
431 [[range valueForKey:@"maxFrameRate"] getValue:&max_framerate];
433 dimensions.width, dimensions.height,
434 min_framerate, max_framerate);
444 NSError *
error = nil;
445 AVCaptureInput* capture_input = nil;
447 NSNumber *pixel_format;
448 NSDictionary *capture_dict;
449 dispatch_queue_t queue;
451 if (
ctx->video_device_index <
ctx->num_video_devices) {
452 capture_input = (AVCaptureInput*) [[[AVCaptureDeviceInput alloc] initWithDevice:video_device
error:&
error] autorelease];
454 capture_input = (AVCaptureInput*) video_device;
457 if (!capture_input) {
459 [[
error localizedDescription] UTF8String]);
463 if ([
ctx->capture_session canAddInput:capture_input]) {
464 [ctx->capture_session addInput:capture_input];
471 ctx->video_output = [[AVCaptureVideoDataOutput alloc] init];
473 if (!
ctx->video_output) {
483 }
@catch (NSException *exception) {
484 if (![[exception
name] isEqualToString:NSUndefinedKeyException]) {
508 if ([[
ctx->video_output availableVideoCVPixelFormatTypes] indexOfObject:[NSNumber numberWithInt:pxl_fmt_spec.avf_id]] == NSNotFound) {
509 av_log(
s,
AV_LOG_ERROR,
"Selected pixel format (%s) is not supported by the input device.\n",
515 for (NSNumber *pxl_fmt in [
ctx->video_output availableVideoCVPixelFormatTypes]) {
530 pxl_fmt_spec = pxl_fmt_dummy;
545 if (
ctx->capture_raw_data) {
546 ctx->pixel_format = pxl_fmt_spec.ff_id;
547 ctx->video_output.videoSettings = @{ };
549 ctx->pixel_format = pxl_fmt_spec.ff_id;
550 pixel_format = [NSNumber numberWithUnsignedInt:pxl_fmt_spec.avf_id];
551 capture_dict = [NSDictionary dictionaryWithObject:pixel_format
552 forKey:(id)kCVPixelBufferPixelFormatTypeKey];
554 [ctx->video_output setVideoSettings:capture_dict];
556 [ctx->video_output setAlwaysDiscardsLateVideoFrames:ctx->drop_late_frames];
558 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
560 if (!
ctx->video_is_screen) {
561 int trans_ctrl = [video_device transportControlsSupported];
562 AVCaptureDeviceTransportControlsPlaybackMode trans_mode = [video_device transportControlsPlaybackMode];
565 ctx->observed_mode = trans_mode;
566 ctx->observed_device = video_device;
573 queue = dispatch_queue_create(
"avf_queue",
NULL);
574 [ctx->video_output setSampleBufferDelegate:ctx->avf_delegate queue:queue];
575 dispatch_release(queue);
577 if ([
ctx->capture_session canAddOutput:
ctx->video_output]) {
578 [ctx->capture_session addOutput:ctx->video_output];
590 NSError *
error = nil;
591 AVCaptureDeviceInput* audio_dev_input = [[[AVCaptureDeviceInput alloc] initWithDevice:audio_device
error:&
error] autorelease];
592 dispatch_queue_t queue;
594 if (!audio_dev_input) {
596 [[
error localizedDescription] UTF8String]);
600 if ([
ctx->capture_session canAddInput:audio_dev_input]) {
601 [ctx->capture_session addInput:audio_dev_input];
608 ctx->audio_output = [[AVCaptureAudioDataOutput alloc] init];
610 if (!
ctx->audio_output) {
617 queue = dispatch_queue_create(
"avf_audio_queue",
NULL);
618 [ctx->audio_output setSampleBufferDelegate:ctx->avf_audio_delegate queue:queue];
619 dispatch_release(queue);
621 if ([
ctx->capture_session canAddOutput:
ctx->audio_output]) {
622 [ctx->capture_session addOutput:ctx->audio_output];
634 CVImageBufferRef image_buffer;
635 CGSize image_buffer_size;
643 while (
ctx->frames_captured < 1) {
644 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
649 ctx->video_stream_index = stream->index;
653 image_buffer = CMSampleBufferGetImageBuffer(
ctx->current_frame);
656 image_buffer_size = CVImageBufferGetEncodedSize(image_buffer);
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;
666 stream->codecpar->format =
ctx->pixel_format;
669 CFRelease(
ctx->current_frame);
670 ctx->current_frame = nil;
680 CMFormatDescriptionRef format_desc;
688 while (
ctx->audio_frames_captured < 1) {
689 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, YES);
694 ctx->audio_stream_index = stream->index;
698 format_desc = CMSampleBufferGetFormatDescription(
ctx->current_audio_frame);
699 const AudioStreamBasicDescription *basic_desc = CMAudioFormatDescriptionGetStreamBasicDescription(format_desc);
708 stream->codecpar->sample_rate = basic_desc->mSampleRate;
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;
719 if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
721 ctx->audio_bits_per_sample == 32 &&
724 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
725 ctx->audio_signed_integer &&
726 ctx->audio_bits_per_sample == 16 &&
729 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
730 ctx->audio_signed_integer &&
731 ctx->audio_bits_per_sample == 24 &&
734 }
else if (basic_desc->mFormatID == kAudioFormatLinearPCM &&
735 ctx->audio_signed_integer &&
736 ctx->audio_bits_per_sample == 32 &&
745 if (
ctx->audio_non_interleaved) {
746 CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(
ctx->current_audio_frame);
747 ctx->audio_buffer_size = CMBlockBufferGetDataLength(block_buffer);
749 if (!
ctx->audio_buffer) {
756 CFRelease(
ctx->current_audio_frame);
757 ctx->current_audio_frame = nil;
765 #if ((TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 100000) || (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101500))
766 NSMutableArray *deviceTypes = nil;
767 if (mediaType == AVMediaTypeVideo) {
768 deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeBuiltInWideAngleCamera]];
769 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 100000)
770 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInDualCamera];
771 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInTelephotoCamera];
773 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 110100)
774 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInTrueDepthCamera];
776 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 130000)
777 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInTripleCamera];
778 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInDualWideCamera];
779 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInUltraWideCamera];
781 #if (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 130000)
782 [deviceTypes addObject: AVCaptureDeviceTypeDeskViewCamera];
784 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 150400)
785 [deviceTypes addObject: AVCaptureDeviceTypeBuiltInLiDARDepthCamera];
787 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 170000 || (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 140000))
788 [deviceTypes addObject: AVCaptureDeviceTypeContinuityCamera];
789 [deviceTypes addObject: AVCaptureDeviceTypeExternal];
790 #elif (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED < 140000)
791 [deviceTypes addObject: AVCaptureDeviceTypeExternalUnknown];
793 }
else if (mediaType == AVMediaTypeAudio) {
794 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 170000 || (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 140000))
795 deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeMicrophone]];
797 deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeBuiltInMicrophone]];
799 }
else if (mediaType == AVMediaTypeMuxed) {
800 #if (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 170000 || (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED >= 140000))
801 deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeExternal]];
802 #elif (TARGET_OS_OSX && __MAC_OS_X_VERSION_MIN_REQUIRED < 140000)
803 deviceTypes = [NSMutableArray arrayWithArray:@[AVCaptureDeviceTypeExternalUnknown]];
811 AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession =
812 [AVCaptureDeviceDiscoverySession
813 discoverySessionWithDeviceTypes:deviceTypes
815 position:AVCaptureDevicePositionUnspecified];
816 return [captureDeviceDiscoverySession devices];
818 return [AVCaptureDevice devicesWithMediaType:mediaType];
825 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
826 uint32_t num_screens = 0;
828 AVCaptureDevice *video_device = nil;
829 AVCaptureDevice *audio_device = nil;
834 ctx->num_video_devices = [devices count] + [devices_muxed count];
838 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
839 CGGetActiveDisplayList(0,
NULL, &num_screens);
843 if (
ctx->list_devices) {
846 for (AVCaptureDevice *device in devices) {
847 const char *
name = [[device localizedName] UTF8String];
848 index = [devices indexOfObject:device];
851 for (AVCaptureDevice *device in devices_muxed) {
852 const char *
name = [[device localizedName] UTF8String];
853 index = [devices count] + [devices_muxed indexOfObject:device];
856 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
857 if (num_screens > 0) {
858 CGDirectDisplayID screens[num_screens];
859 CGGetActiveDisplayList(num_screens, screens, &num_screens);
860 for (
int i = 0;
i < num_screens;
i++) {
868 for (AVCaptureDevice *device in devices) {
869 const char *
name = [[device localizedName] UTF8String];
870 int index = [devices indexOfObject:device];
882 if (
ctx->video_device_index == -1 &&
ctx->video_filename) {
883 sscanf(
ctx->video_filename,
"%d", &
ctx->video_device_index);
885 if (
ctx->audio_device_index == -1 &&
ctx->audio_filename) {
886 sscanf(
ctx->audio_filename,
"%d", &
ctx->audio_device_index);
889 if (
ctx->video_device_index >= 0) {
890 if (
ctx->video_device_index <
ctx->num_video_devices) {
891 if (
ctx->video_device_index < [devices count]) {
892 video_device = [devices objectAtIndex:ctx->video_device_index];
894 video_device = [devices_muxed objectAtIndex:(ctx->video_device_index - [devices count])];
895 ctx->video_is_muxed = 1;
897 }
else if (
ctx->video_device_index <
ctx->num_video_devices + num_screens) {
898 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
899 CGDirectDisplayID screens[num_screens];
900 CGGetActiveDisplayList(num_screens, screens, &num_screens);
901 AVCaptureScreenInput* capture_screen_input = [[[AVCaptureScreenInput alloc] initWithDisplayID:screens[ctx->video_device_index - ctx->
num_video_devices]] autorelease];
903 if (
ctx->framerate.num > 0) {
904 capture_screen_input.minFrameDuration = CMTimeMake(
ctx->framerate.den,
ctx->framerate.num);
907 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
908 if (
ctx->capture_cursor) {
909 capture_screen_input.capturesCursor = YES;
911 capture_screen_input.capturesCursor = NO;
915 if (
ctx->capture_mouse_clicks) {
916 capture_screen_input.capturesMouseClicks = YES;
918 capture_screen_input.capturesMouseClicks = NO;
921 video_device = (AVCaptureDevice*) capture_screen_input;
922 ctx->video_is_screen = 1;
928 }
else if (
ctx->video_filename &&
929 strncmp(
ctx->video_filename,
"none", 4)) {
930 if (!strncmp(
ctx->video_filename,
"default", 7)) {
931 video_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
934 for (AVCaptureDevice *device in devices) {
935 if (!strncmp(
ctx->video_filename, [[device localizedName] UTF8String], strlen(
ctx->video_filename))) {
936 video_device = device;
941 for (AVCaptureDevice *device in devices_muxed) {
942 if (!strncmp(
ctx->video_filename, [[device localizedName] UTF8String], strlen(
ctx->video_filename))) {
943 video_device = device;
944 ctx->video_is_muxed = 1;
949 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
953 if(sscanf(
ctx->video_filename,
"Capture screen %d", &idx) && idx < num_screens) {
954 CGDirectDisplayID screens[num_screens];
955 CGGetActiveDisplayList(num_screens, screens, &num_screens);
956 AVCaptureScreenInput* capture_screen_input = [[[AVCaptureScreenInput alloc] initWithDisplayID:screens[idx]] autorelease];
957 video_device = (AVCaptureDevice*) capture_screen_input;
958 ctx->video_device_index =
ctx->num_video_devices + idx;
959 ctx->video_is_screen = 1;
961 if (
ctx->framerate.num > 0) {
962 capture_screen_input.minFrameDuration = CMTimeMake(
ctx->framerate.den,
ctx->framerate.num);
965 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
966 if (
ctx->capture_cursor) {
967 capture_screen_input.capturesCursor = YES;
969 capture_screen_input.capturesCursor = NO;
973 if (
ctx->capture_mouse_clicks) {
974 capture_screen_input.capturesMouseClicks = YES;
976 capture_screen_input.capturesMouseClicks = NO;
990 if (
ctx->audio_device_index >= 0) {
993 if (
ctx->audio_device_index >= [devices count]) {
998 audio_device = [devices objectAtIndex:ctx->audio_device_index];
999 }
else if (
ctx->audio_filename &&
1000 strncmp(
ctx->audio_filename,
"none", 4)) {
1001 if (!strncmp(
ctx->audio_filename,
"default", 7)) {
1002 audio_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
1006 for (AVCaptureDevice *device in devices) {
1007 if (!strncmp(
ctx->audio_filename, [[device localizedName] UTF8String], strlen(
ctx->audio_filename))) {
1008 audio_device = device;
1014 if (!audio_device) {
1021 if (!video_device && !audio_device) {
1027 if (
ctx->video_device_index <
ctx->num_video_devices) {
1034 av_log(
s,
AV_LOG_DEBUG,
"audio device '%s' opened\n", [[audio_device localizedName] UTF8String]);
1038 ctx->capture_session = [[AVCaptureSession alloc] init];
1046 [ctx->capture_session startRunning];
1050 if (!
ctx->video_is_screen) {
1051 [video_device unlockForConfiguration];
1075 CVPixelBufferRef image_buffer,
1079 int src_linesize[4];
1080 const uint8_t *src_data[4];
1081 int width = CVPixelBufferGetWidth(image_buffer);
1082 int height = CVPixelBufferGetHeight(image_buffer);
1085 memset(src_linesize, 0,
sizeof(src_linesize));
1086 memset(src_data, 0,
sizeof(src_data));
1088 status = CVPixelBufferLockBaseAddress(image_buffer, 0);
1089 if (
status != kCVReturnSuccess) {
1094 if (CVPixelBufferIsPlanar(image_buffer)) {
1095 size_t plane_count = CVPixelBufferGetPlaneCount(image_buffer);
1097 for(
i = 0;
i < plane_count;
i++){
1098 src_linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(image_buffer,
i);
1099 src_data[i] = CVPixelBufferGetBaseAddressOfPlane(image_buffer,
i);
1102 src_linesize[0] = CVPixelBufferGetBytesPerRow(image_buffer);
1103 src_data[0] = CVPixelBufferGetBaseAddress(image_buffer);
1107 src_data, src_linesize,
1112 CVPixelBufferUnlockBaseAddress(image_buffer, 0);
1122 CVImageBufferRef image_buffer;
1123 CMBlockBufferRef block_buffer;
1126 if (
ctx->current_frame != nil) {
1130 image_buffer = CMSampleBufferGetImageBuffer(
ctx->current_frame);
1131 block_buffer = CMSampleBufferGetDataBuffer(
ctx->current_frame);
1133 if (image_buffer != nil) {
1134 length = (int)CVPixelBufferGetDataSize(image_buffer);
1135 }
else if (block_buffer != nil) {
1136 length = (int)CMBlockBufferGetDataLength(block_buffer);
1150 if (CMSampleBufferGetOutputSampleTimingInfoArray(
ctx->current_frame, 1, &
timing_info, &count) == noErr) {
1162 OSStatus
ret = CMBlockBufferCopyDataBytes(block_buffer, 0,
pkt->
size,
pkt->
data);
1163 if (
ret != kCMBlockBufferNoErr) {
1167 CFRelease(
ctx->current_frame);
1168 ctx->current_frame = nil;
1174 }
else if (
ctx->current_audio_frame != nil) {
1175 CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(
ctx->current_audio_frame);
1176 int block_buffer_size = CMBlockBufferGetDataLength(block_buffer);
1178 if (!block_buffer || !block_buffer_size) {
1183 if (
ctx->audio_non_interleaved && block_buffer_size >
ctx->audio_buffer_size) {
1196 if (CMSampleBufferGetOutputSampleTimingInfoArray(
ctx->current_audio_frame, 1, &
timing_info, &count) == noErr) {
1204 if (
ctx->audio_non_interleaved) {
1207 OSStatus
ret = CMBlockBufferCopyDataBytes(block_buffer, 0,
pkt->
size,
ctx->audio_buffer);
1208 if (
ret != kCMBlockBufferNoErr) {
1213 num_samples =
pkt->
size / (
ctx->audio_channels * (
ctx->audio_bits_per_sample >> 3));
1216 #define INTERLEAVE_OUTPUT(bps) \
1218 int##bps##_t **src; \
1219 int##bps##_t *dest; \
1220 src = av_malloc(ctx->audio_channels * sizeof(int##bps##_t*)); \
1222 unlock_frames(ctx); \
1223 return AVERROR(EIO); \
1226 for (c = 0; c < ctx->audio_channels; c++) { \
1227 src[c] = ((int##bps##_t*)ctx->audio_buffer) + c * num_samples; \
1229 dest = (int##bps##_t*)pkt->data; \
1230 shift = bps - ctx->audio_bits_per_sample; \
1231 for (sample = 0; sample < num_samples; sample++) \
1232 for (c = 0; c < ctx->audio_channels; c++) \
1233 *dest++ = src[c][sample] << shift; \
1237 if (
ctx->audio_bits_per_sample <= 16) {
1243 OSStatus
ret = CMBlockBufferCopyDataBytes(block_buffer, 0,
pkt->
size,
pkt->
data);
1244 if (
ret != kCMBlockBufferNoErr) {
1250 CFRelease(
ctx->current_audio_frame);
1251 ctx->current_audio_frame = nil;
1255 if (
ctx->observed_quit) {
1299 .
p.
name =
"avfoundation",