21 #include <VideoToolbox/VideoToolbox.h>
22 #include <CoreVideo/CoreVideo.h>
23 #include <CoreMedia/CoreMedia.h>
24 #include <TargetConditionals.h>
25 #include <Availability.h>
44 #if !HAVE_KCMVIDEOCODECTYPE_HEVC
48 #if !HAVE_KCMVIDEOCODECTYPE_HEVCWITHALPHA
52 #if !HAVE_KCVPIXELFORMATTYPE_420YPCBCR10BIPLANARVIDEORANGE
57 #ifndef TARGET_CPU_ARM64
58 # define TARGET_CPU_ARM64 0
62 size_t parameterSetIndex,
63 const uint8_t **parameterSetPointerOut,
64 size_t *parameterSetSizeOut,
65 size_t *parameterSetCountOut,
66 int *NALUnitHeaderLengthOut);
137 #define GET_SYM(symbol, defaultVal) \
139 CFStringRef* handle = (CFStringRef*)dlsym(RTLD_DEFAULT, #symbol); \
141 compat_keys.symbol = CFSTR(defaultVal); \
143 compat_keys.symbol = *handle; \
149 compat_keys.CMVideoFormatDescriptionGetHEVCParameterSetAtIndex =
152 "CMVideoFormatDescriptionGetHEVCParameterSetAtIndex"
192 "TargetQualityForAlpha");
194 "PrioritizeEncodingSpeedOverQuality");
199 "EnableHardwareAcceleratedVideoEncoder");
201 "RequireHardwareAcceleratedVideoEncoder");
203 "EnableLowLatencyRateControl");
206 "MaximizePowerEfficiency");
208 "ReferenceBufferCount");
213 #define H264_PROFILE_CONSTRAINED_HIGH (AV_PROFILE_H264_HIGH | AV_PROFILE_H264_CONSTRAINED)
291 CFRelease(
info->cm_buffer);
299 CFStringRef encoder_id =
NULL;
301 CFIndex length, max_size;
312 length = CFStringGetLength(encoder_id);
313 max_size = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
316 CFRelease(encoder_id);
320 CFStringGetCString(encoder_id,
323 kCFStringEncodingUTF8);
326 CFRelease(encoder_id);
333 CFStringRef profile_level,
334 CFNumberRef gamma_level,
335 CFDictionaryRef enc_info,
336 CFDictionaryRef pixel_buffer_info);
440 *buf =
info->cm_buffer;
468 CMSampleBufferRef sample_buffer,
475 size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
476 CMBlockBufferRef
block = CMSampleBufferGetDataBuffer(sample_buffer);
478 if (length_code_size > 4)
481 while (
offset < src_size) {
491 if (
status != kCMBlockBufferNoErr) {
495 for (
i = 0;
i < length_code_size;
i++) {
497 box_len |= size_buf[
i];
500 curr_src_len = box_len + length_code_size;
512 double alpha_quality)
527 return MKBETAG(
'a',
'p',
'c',
'o');
529 return MKBETAG(
'a',
'p',
'c',
's');
531 return MKBETAG(
'a',
'p',
'c',
'n');
533 return MKBETAG(
'a',
'p',
'c',
'h');
535 return MKBETAG(
'a',
'p',
'4',
'h');
537 return MKBETAG(
'a',
'p',
'4',
'x');
544 desc->log2_chroma_w == 0))
545 return MKBETAG(
'a',
'p',
'4',
'h');
547 return MKBETAG(
'a',
'p',
'c',
'n');
563 CMVideoFormatDescriptionRef vid_fmt,
567 size_t total_size = 0;
569 int is_count_bad = 0;
584 for (
i = 0;
i < ps_count || is_count_bad;
i++) {
598 if (
i > 0 && is_count_bad)
status = 0;
617 CMVideoFormatDescriptionRef vid_fmt,
623 int is_count_bad = 0;
641 for (
i = 0;
i < ps_count || is_count_bad;
i++) {
653 if (
i > 0 && is_count_bad)
status = 0;
659 if (dst_size < next_offset) {
682 CMVideoFormatDescriptionRef vid_fmt;
686 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
712 CFDataRef
data = CMFormatDescriptionGetExtension(vid_fmt, kCMFormatDescriptionExtension_VerbatimSampleDescription);
713 if (
data && CFGetTypeID(
data) == CFDataGetTypeID()) {
714 CFIndex
size = CFDataGetLength(
data);
730 void *sourceFrameCtx,
732 VTEncodeInfoFlags
flags,
733 CMSampleBufferRef sample_buffer)
752 if (!sample_buffer) {
756 CFRetain(sample_buffer);
757 info->cm_buffer = sample_buffer;
773 CMSampleBufferRef sample_buffer,
777 CMVideoFormatDescriptionRef vid_fmt;
781 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
809 CFStringRef *profile_level_val)
819 *profile_level_val =
NULL;
826 switch (vtctx->
level) {
827 case 0: *profile_level_val =
828 compat_keys.kVTProfileLevel_H264_Baseline_AutoLevel;
break;
829 case 13: *profile_level_val = kVTProfileLevel_H264_Baseline_1_3;
break;
830 case 30: *profile_level_val = kVTProfileLevel_H264_Baseline_3_0;
break;
831 case 31: *profile_level_val = kVTProfileLevel_H264_Baseline_3_1;
break;
832 case 32: *profile_level_val = kVTProfileLevel_H264_Baseline_3_2;
break;
833 case 40: *profile_level_val =
834 compat_keys.kVTProfileLevel_H264_Baseline_4_0;
break;
835 case 41: *profile_level_val = kVTProfileLevel_H264_Baseline_4_1;
break;
836 case 42: *profile_level_val =
837 compat_keys.kVTProfileLevel_H264_Baseline_4_2;
break;
838 case 50: *profile_level_val =
839 compat_keys.kVTProfileLevel_H264_Baseline_5_0;
break;
840 case 51: *profile_level_val =
841 compat_keys.kVTProfileLevel_H264_Baseline_5_1;
break;
842 case 52: *profile_level_val =
843 compat_keys.kVTProfileLevel_H264_Baseline_5_2;
break;
848 *profile_level_val =
compat_keys.kVTProfileLevel_H264_ConstrainedBaseline_AutoLevel;
850 if (vtctx->
level != 0) {
853 "Level is auto-selected when constrained-baseline "
854 "profile is used. The output may be encoded with a "
855 "different level.\n");
860 switch (vtctx->
level) {
861 case 0: *profile_level_val =
862 compat_keys.kVTProfileLevel_H264_Main_AutoLevel;
break;
863 case 30: *profile_level_val = kVTProfileLevel_H264_Main_3_0;
break;
864 case 31: *profile_level_val = kVTProfileLevel_H264_Main_3_1;
break;
865 case 32: *profile_level_val = kVTProfileLevel_H264_Main_3_2;
break;
866 case 40: *profile_level_val = kVTProfileLevel_H264_Main_4_0;
break;
867 case 41: *profile_level_val = kVTProfileLevel_H264_Main_4_1;
break;
868 case 42: *profile_level_val =
870 case 50: *profile_level_val = kVTProfileLevel_H264_Main_5_0;
break;
871 case 51: *profile_level_val =
873 case 52: *profile_level_val =
879 *profile_level_val =
compat_keys.kVTProfileLevel_H264_ConstrainedHigh_AutoLevel;
881 if (vtctx->
level != 0) {
884 "Level is auto-selected when constrained-high profile "
885 "is used. The output may be encoded with a different "
891 switch (vtctx->
level) {
892 case 0: *profile_level_val =
893 compat_keys.kVTProfileLevel_H264_High_AutoLevel;
break;
894 case 30: *profile_level_val =
896 case 31: *profile_level_val =
898 case 32: *profile_level_val =
900 case 40: *profile_level_val =
902 case 41: *profile_level_val =
904 case 42: *profile_level_val =
906 case 50: *profile_level_val = kVTProfileLevel_H264_High_5_0;
break;
907 case 51: *profile_level_val =
909 case 52: *profile_level_val =
914 switch (vtctx->
level) {
915 case 0: *profile_level_val =
916 compat_keys.kVTProfileLevel_H264_Extended_AutoLevel;
break;
917 case 50: *profile_level_val =
918 compat_keys.kVTProfileLevel_H264_Extended_5_0;
break;
923 if (!*profile_level_val) {
938 CFStringRef *profile_level_val)
947 *profile_level_val =
NULL;
961 "main profile with %d bit input\n",
bit_depth);
968 "Invalid main10 profile with %d bit input\n",
bit_depth);
976 if (!*profile_level_val) {
987 int* av_pixel_format,
990 const char *range_name;
996 if (*av_pixel_format)
1001 "Could not get pixel format for color format '%s' range '%s'.\n",
1003 range_name ? range_name :
"Unknown");
1012 CFDictionarySetValue(dict,
1013 kCVImageBufferColorPrimariesKey,
1018 CFDictionarySetValue(dict,
1019 kCVImageBufferTransferFunctionKey,
1024 CFDictionarySetValue(dict,
1025 kCVImageBufferYCbCrMatrixKey,
1031 CFMutableDictionaryRef* dict)
1033 CFNumberRef cv_color_format_num =
NULL;
1034 CFNumberRef width_num =
NULL;
1035 CFNumberRef height_num =
NULL;
1036 CFMutableDictionaryRef pixel_buffer_info =
NULL;
1037 int cv_color_format;
1045 pixel_buffer_info = CFDictionaryCreateMutable(
1046 kCFAllocatorDefault,
1048 &kCFCopyStringDictionaryKeyCallBacks,
1049 &kCFTypeDictionaryValueCallBacks);
1051 if (!pixel_buffer_info)
goto pbinfo_nomem;
1053 cv_color_format_num = CFNumberCreate(kCFAllocatorDefault,
1054 kCFNumberSInt32Type,
1056 if (!cv_color_format_num)
goto pbinfo_nomem;
1058 CFDictionarySetValue(pixel_buffer_info,
1059 kCVPixelBufferPixelFormatTypeKey,
1060 cv_color_format_num);
1063 width_num = CFNumberCreate(kCFAllocatorDefault,
1064 kCFNumberSInt32Type,
1066 if (!width_num)
goto pbinfo_nomem;
1068 CFDictionarySetValue(pixel_buffer_info,
1069 kCVPixelBufferWidthKey,
1073 height_num = CFNumberCreate(kCFAllocatorDefault,
1074 kCFNumberSInt32Type,
1076 if (!height_num)
goto pbinfo_nomem;
1078 CFDictionarySetValue(pixel_buffer_info,
1079 kCVPixelBufferHeightKey,
1085 *dict = pixel_buffer_info;
1092 if (pixel_buffer_info) CFRelease(pixel_buffer_info);
1098 CFNumberRef *gamma_level)
1102 *gamma_level =
NULL;
1110 *gamma_level = CFNumberCreate(
NULL, kCFNumberFloat32Type, &gamma);
1122 const char *print_option_name,
1128 if (
status == kVTPropertyNotSupportedErr) {
1131 "This device does not support the %s option. Value ignored.\n",
1133 }
else if (
status != 0) {
1136 "Error setting %s: Error %d\n",
1144 const char* print_option_name,
1146 CFNumberRef value_cfnum = CFNumberCreate(kCFAllocatorDefault,
1150 if (value_cfnum ==
NULL) {
1156 CFRelease(value_cfnum);
1163 CFStringRef profile_level,
1164 CFNumberRef gamma_level,
1165 CFDictionaryRef enc_info,
1166 CFDictionaryRef pixel_buffer_info,
1167 bool constant_bit_rate,
1168 VTCompressionSessionRef *session)
1174 CFNumberRef bit_rate_num;
1175 CFNumberRef quality_num;
1176 CFNumberRef bytes_per_second;
1177 CFNumberRef one_second;
1178 CFArrayRef data_rate_limits;
1179 int64_t bytes_per_second_value = 0;
1183 int status = VTCompressionSessionCreate(kCFAllocatorDefault,
1189 kCFAllocatorDefault,
1197 #if !TARGET_OS_IPHONE
1199 av_log(avctx,
AV_LOG_ERROR,
"Try -allow_sw 1. The hardware encoder may be busy, or not supported.\n");
1206 #if defined (MAC_OS_X_VERSION_10_13) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_13)
1207 if (__builtin_available(macOS 10.13, *)) {
1212 status = VTCopySupportedPropertyDictionaryForEncoder(avctx->
width,
1231 av_log(avctx,
AV_LOG_ERROR,
"Error: -q:v qscale not available for encoder. Use -b:v bitrate instead.\n");
1237 quality_num = CFNumberCreate(kCFAllocatorDefault,
1238 kCFNumberFloat32Type,
1240 if (!quality_num)
return AVERROR(ENOMEM);
1243 kVTCompressionPropertyKey_Quality,
1245 CFRelease(quality_num);
1247 bit_rate_num = CFNumberCreate(kCFAllocatorDefault,
1248 kCFNumberSInt32Type,
1250 if (!bit_rate_num)
return AVERROR(ENOMEM);
1252 if (constant_bit_rate) {
1254 compat_keys.kVTCompressionPropertyKey_ConstantBitRate,
1256 if (
status == kVTPropertyNotSupportedErr) {
1257 av_log(avctx,
AV_LOG_ERROR,
"Error: -constant_bit_rate true is not supported by the encoder.\n");
1262 kVTCompressionPropertyKey_AverageBitRate,
1266 CFRelease(bit_rate_num);
1276 compat_keys.kVTCompressionPropertyKey_PrioritizeEncodingSpeedOverQuality,
1277 vtctx->
prio_speed ? kCFBooleanTrue : kCFBooleanFalse);
1279 av_log(avctx,
AV_LOG_WARNING,
"PrioritizeEncodingSpeedOverQuality property is not supported on this device. Ignoring.\n");
1285 bytes_per_second_value = max_rate >> 3;
1286 bytes_per_second = CFNumberCreate(kCFAllocatorDefault,
1287 kCFNumberSInt64Type,
1288 &bytes_per_second_value);
1289 if (!bytes_per_second) {
1292 one_second_value = 1;
1293 one_second = CFNumberCreate(kCFAllocatorDefault,
1294 kCFNumberSInt64Type,
1297 CFRelease(bytes_per_second);
1300 nums[0] = (
void *)bytes_per_second;
1301 nums[1] = (
void *)one_second;
1302 data_rate_limits = CFArrayCreate(kCFAllocatorDefault,
1303 (
const void **)nums,
1305 &kCFTypeArrayCallBacks);
1307 if (!data_rate_limits) {
1308 CFRelease(bytes_per_second);
1309 CFRelease(one_second);
1313 kVTCompressionPropertyKey_DataRateLimits,
1316 CFRelease(bytes_per_second);
1317 CFRelease(one_second);
1318 CFRelease(data_rate_limits);
1332 CFNumberRef alpha_quality_num = CFNumberCreate(kCFAllocatorDefault,
1333 kCFNumberDoubleType,
1335 if (!alpha_quality_num)
return AVERROR(ENOMEM);
1338 compat_keys.kVTCompressionPropertyKey_TargetQualityForAlpha,
1340 CFRelease(alpha_quality_num);
1345 "Error setting alpha quality: %d\n",
1351 if (profile_level) {
1353 kVTCompressionPropertyKey_ProfileLevel,
1356 av_log(avctx,
AV_LOG_ERROR,
"Error setting profile/level property: %d. Output will be encoded using a supported profile/level combination.\n",
status);
1361 CFNumberRef interval = CFNumberCreate(kCFAllocatorDefault,
1369 kVTCompressionPropertyKey_MaxKeyFrameInterval,
1371 CFRelease(interval);
1381 kVTCompressionPropertyKey_MoreFramesBeforeStart,
1384 if (
status == kVTPropertyNotSupportedErr) {
1385 av_log(avctx,
AV_LOG_WARNING,
"frames_before property is not supported on this device. Ignoring.\n");
1393 kVTCompressionPropertyKey_MoreFramesAfterEnd,
1396 if (
status == kVTPropertyNotSupportedErr) {
1397 av_log(avctx,
AV_LOG_WARNING,
"frames_after property is not supported on this device. Ignoring.\n");
1406 CFMutableDictionaryRef par;
1413 num = CFNumberCreate(kCFAllocatorDefault,
1417 den = CFNumberCreate(kCFAllocatorDefault,
1423 par = CFDictionaryCreateMutable(kCFAllocatorDefault,
1425 &kCFCopyStringDictionaryKeyCallBacks,
1426 &kCFTypeDictionaryValueCallBacks);
1428 if (!par || !num || !den) {
1429 if (par) CFRelease(par);
1430 if (num) CFRelease(num);
1431 if (den) CFRelease(den);
1436 CFDictionarySetValue(
1438 kCMFormatDescriptionKey_PixelAspectRatioHorizontalSpacing,
1441 CFDictionarySetValue(
1443 kCMFormatDescriptionKey_PixelAspectRatioVerticalSpacing,
1447 kVTCompressionPropertyKey_PixelAspectRatio,
1457 "Error setting pixel aspect ratio to %d:%d: %d.\n",
1469 kVTCompressionPropertyKey_TransferFunction,
1480 kVTCompressionPropertyKey_YCbCrMatrix,
1491 kVTCompressionPropertyKey_ColorPrimaries,
1501 kCVImageBufferGammaLevelKey,
1511 kVTCompressionPropertyKey_AllowFrameReordering,
1526 compat_keys.kVTCompressionPropertyKey_H264EntropyMode,
1537 vtctx->
realtime ? kCFBooleanTrue : kCFBooleanFalse);
1546 compat_keys.kVTCompressionPropertyKey_AllowOpenGOP,
1551 if (avctx->
qmin >= 0) {
1553 compat_keys.kVTCompressionPropertyKey_MinAllowedFrameQP,
1562 if (avctx->
qmax >= 0) {
1564 compat_keys.kVTCompressionPropertyKey_MaxAllowedFrameQP,
1575 kVTCompressionPropertyKey_MaxH264SliceBytes,
1586 compat_keys.kVTCompressionPropertyKey_MaximizePowerEfficiency,
1593 compat_keys.kVTCompressionPropertyKey_ReferenceBufferCount,
1602 status = VTCompressionSessionPrepareToEncodeFrames(vtctx->
session);
1613 CFMutableDictionaryRef enc_info;
1614 CFMutableDictionaryRef pixel_buffer_info =
NULL;
1617 CFStringRef profile_level =
NULL;
1618 CFNumberRef gamma_level =
NULL;
1627 #if defined(MAC_OS_X_VERSION_10_9) && !TARGET_OS_IPHONE && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_9)
1629 if (__builtin_available(macOS 10.10, *)) {
1630 VTRegisterProfessionalVideoWorkflowVideoEncoders();
1642 av_log(avctx,
AV_LOG_WARNING,
"Cannot use B-frames with baseline profile. Output will not contain B-frames.\n");
1647 av_log(avctx,
AV_LOG_WARNING,
"CABAC entropy requires 'main' or 'high' profile, but baseline was requested. Encode will not use CABAC entropy.\n");
1662 enc_info = CFDictionaryCreateMutable(
1663 kCFAllocatorDefault,
1665 &kCFCopyStringDictionaryKeyCallBacks,
1666 &kCFTypeDictionaryValueCallBacks
1669 if (!enc_info)
return AVERROR(ENOMEM);
1671 #if !TARGET_OS_IPHONE
1673 CFDictionarySetValue(enc_info,
1674 compat_keys.kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
1677 CFDictionarySetValue(enc_info,
1678 compat_keys.kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder,
1681 CFDictionarySetValue(enc_info,
1682 compat_keys.kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
1689 CFDictionarySetValue(enc_info,
1690 compat_keys.kVTVideoEncoderSpecification_EnableLowLatencyRateControl,
1730 CFRelease(gamma_level);
1732 if (pixel_buffer_info)
1733 CFRelease(pixel_buffer_info);
1735 CFRelease(enc_info);
1743 CFBooleanRef has_b_frames_cfbool;
1758 kVTCompressionPropertyKey_AllowFrameReordering,
1759 kCFAllocatorDefault,
1760 &has_b_frames_cfbool);
1762 if (!
status && has_b_frames_cfbool) {
1765 if (CFBooleanGetValue(has_b_frames_cfbool))
1769 CFRelease(has_b_frames_cfbool);
1778 CFArrayRef attachments;
1779 CFDictionaryRef attachment;
1780 CFBooleanRef not_sync;
1783 attachments = CMSampleBufferGetSampleAttachmentsArray(
buffer,
false);
1784 len = !attachments ? 0 : CFArrayGetCount(attachments);
1787 *is_key_frame =
true;
1791 attachment = CFArrayGetValueAtIndex(attachments, 0);
1793 if (CFDictionaryGetValueIfPresent(attachment,
1794 kCMSampleAttachmentKey_NotSync,
1795 (
const void **)¬_sync))
1797 *is_key_frame = !CFBooleanGetValue(not_sync);
1799 *is_key_frame =
true;
1820 size_t sei_payload_size = 0;
1821 uint8_t *nal_start = nal_data;
1827 nal_type = *nal_data & 0x1F;
1834 if (nal_data[nal_size - 1] == 0x80)
1837 while (nal_size > 0 && *nal_data > 0) {
1841 }
while (nal_size > 0 && *nal_data == 0xFF);
1849 sei_payload_size += *nal_data;
1852 }
while (nal_size > 0 && *nal_data == 0xFF);
1854 if (nal_size < sei_payload_size) {
1859 nal_data += sei_payload_size;
1860 nal_size -= sei_payload_size;
1863 *sei_end = nal_data;
1865 return nal_data - nal_start + 1;
1885 uint8_t* dst_end =
dst + dst_size;
1886 const uint8_t* src_end =
src + src_size;
1887 int start_at = dst_offset > 2 ? dst_offset - 2 : 0;
1889 for (
i = start_at;
i < dst_offset &&
i < dst_size;
i++) {
1900 int insert_ep3_byte = *
src <= 3;
1901 if (insert_ep3_byte) {
1919 wrote_bytes =
dst - dst_start;
1922 return -wrote_bytes;
1932 uint8_t *sei_start =
dst;
1933 size_t remaining_sei_size =
sei->size;
1934 size_t remaining_dst_size = dst_size;
1939 if (!remaining_dst_size)
1942 while (sei_type && remaining_dst_size != 0) {
1943 int sei_byte = sei_type > 255 ? 255 : sei_type;
1946 sei_type -= sei_byte;
1948 remaining_dst_size--;
1954 while (remaining_sei_size && remaining_dst_size != 0) {
1955 int size_byte = remaining_sei_size > 255 ? 255 : remaining_sei_size;
1958 remaining_sei_size -= size_byte;
1960 remaining_dst_size--;
1963 if (remaining_dst_size < sei->
size)
1966 header_bytes =
dst - sei_start;
1974 if (bytes_written < 0)
1977 bytes_written += header_bytes;
1978 return bytes_written;
2002 size_t length_code_size,
2003 CMSampleBufferRef sample_buffer,
2008 size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
2009 size_t remaining_src_size = src_size;
2010 size_t remaining_dst_size = dst_size;
2011 size_t src_offset = 0;
2014 uint8_t size_buf[4];
2016 CMBlockBufferRef
block = CMSampleBufferGetDataBuffer(sample_buffer);
2018 if (length_code_size > 4) {
2022 while (remaining_src_size > 0) {
2023 size_t curr_src_len;
2024 size_t curr_dst_len;
2040 src_offset + length_code_size,
2051 for (
i = 0;
i < length_code_size;
i++) {
2053 box_len |= size_buf[
i];
2066 remaining_dst_size--;
2071 remaining_dst_size);
2073 if (wrote_bytes < 0)
2076 remaining_dst_size -= wrote_bytes;
2077 dst_data += wrote_bytes;
2079 if (remaining_dst_size <= 0)
2085 remaining_dst_size--;
2090 curr_src_len = box_len + length_code_size;
2093 if (remaining_src_size < curr_src_len) {
2097 if (remaining_dst_size < curr_dst_len) {
2105 src_offset + length_code_size,
2120 old_sei_length =
find_sei_end(avctx, dst_box, box_len, &new_sei);
2121 if (old_sei_length < 0)
2127 remaining_dst_size - old_sei_length);
2128 if (wrote_bytes < 0)
2131 if (new_sei + wrote_bytes >= dst_data + remaining_dst_size)
2134 new_sei[wrote_bytes++] = 0x80;
2135 extra_bytes = wrote_bytes - (dst_box + box_len - new_sei);
2137 dst_data += extra_bytes;
2138 remaining_dst_size -= extra_bytes;
2143 src_offset += curr_src_len;
2144 dst_data += curr_dst_len;
2146 remaining_src_size -= curr_src_len;
2147 remaining_dst_size -= curr_dst_len;
2168 if ((
sei->size % 255) == 0)
2171 return copied_size +
sei->size / 255 + 1 +
type / 255 + 1;
2176 CMSampleBufferRef sample_buffer,
2185 size_t length_code_size;
2186 size_t header_size = 0;
2188 size_t out_buf_size;
2189 size_t sei_nalu_size = 0;
2195 CMVideoFormatDescriptionRef vid_fmt;
2206 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
2224 sei_nalu_size =
sizeof(
start_code) + 1 + msg_size + 1;
2227 in_buf_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
2228 out_buf_size = header_size +
2231 nalu_count * ((int)
sizeof(
start_code) - (int)length_code_size);
2257 CMBlockBufferRef buf = CMSampleBufferGetDataBuffer(sample_buffer);
2263 len = CMBlockBufferGetDataLength(buf);
2280 pts = CMSampleBufferGetPresentationTimeStamp(sample_buffer);
2281 dts = CMSampleBufferGetDecodeTimeStamp (sample_buffer);
2283 if (CMTIME_IS_INVALID(dts)) {
2295 pkt->
dts = dts.value / time_base_num - dts_delta;
2312 size_t *contiguous_buf_size)
2316 int av_format =
frame->format;
2329 if (range_guessed) {
2334 "Color range not set for %s. Using MPEG range.\n",
2341 for (
i = 0;
i <
desc->nb_components;
i++) {
2342 int p =
desc->comp[
i].plane;
2344 bool isAlpha = hasAlpha && (p + 1 == *plane_count);
2345 bool isChroma = (p != 0) && !isAlpha;
2346 int shiftw = isChroma ?
desc->log2_chroma_w : 0;
2347 int shifth = isChroma ?
desc->log2_chroma_h : 0;
2348 widths[p] = (avctx->
width + ((1 << shiftw) >> 1)) >> shiftw;
2349 heights[p] = (avctx->
height + ((1 << shifth) >> 1)) >> shifth;
2350 strides[p] =
frame->linesize[p];
2353 *contiguous_buf_size = 0;
2354 for (
i = 0;
i < *plane_count;
i++) {
2355 if (
i < *plane_count - 1 &&
2356 frame->data[
i] + strides[
i] * heights[
i] !=
frame->data[
i + 1]) {
2357 *contiguous_buf_size = 0;
2361 *contiguous_buf_size += strides[
i] * heights[
i];
2370 CVPixelBufferRef cv_img,
2371 const size_t *plane_strides,
2372 const size_t *plane_rows)
2384 status = CVPixelBufferLockBaseAddress(cv_img, 0);
2389 "Error: Could not lock base address of CVPixelBuffer: %d.\n",
2394 if (CVPixelBufferIsPlanar(cv_img)) {
2395 plane_count = CVPixelBufferGetPlaneCount(cv_img);
2397 if (
i == plane_count) {
2398 CVPixelBufferUnlockBaseAddress(cv_img, 0);
2401 "Error: different number of planes in AVFrame and CVPixelBuffer.\n"
2407 dst_addr = (uint8_t*)CVPixelBufferGetBaseAddressOfPlane(cv_img,
i);
2408 src_addr = (uint8_t*)
frame->data[
i];
2409 dst_stride = CVPixelBufferGetBytesPerRowOfPlane(cv_img,
i);
2410 src_stride = plane_strides[
i];
2411 rows = plane_rows[
i];
2413 if (dst_stride == src_stride) {
2414 memcpy(dst_addr, src_addr, src_stride * rows);
2416 copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
2418 for (j = 0; j < rows; j++) {
2419 memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
2424 if (
frame->data[1]) {
2425 CVPixelBufferUnlockBaseAddress(cv_img, 0);
2428 "Error: different number of planes in AVFrame and non-planar CVPixelBuffer.\n"
2434 dst_addr = (uint8_t*)CVPixelBufferGetBaseAddress(cv_img);
2435 src_addr = (uint8_t*)
frame->data[0];
2436 dst_stride = CVPixelBufferGetBytesPerRow(cv_img);
2437 src_stride = plane_strides[0];
2438 rows = plane_rows[0];
2440 if (dst_stride == src_stride) {
2441 memcpy(dst_addr, src_addr, src_stride * rows);
2443 copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
2445 for (j = 0; j < rows; j++) {
2446 memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
2451 status = CVPixelBufferUnlockBaseAddress(cv_img, 0);
2462 CVPixelBufferRef *cv_img,
2471 size_t contiguous_buf_size;
2472 CVPixelBufferPoolRef pix_buf_pool;
2478 *cv_img = (CVPixelBufferRef)
frame->data[3];
2482 if (
frame->buf[0]) {
2491 memset(widths, 0,
sizeof(widths));
2492 memset(heights, 0,
sizeof(heights));
2493 memset(strides, 0,
sizeof(strides));
2503 &contiguous_buf_size
2510 "Error: Cannot convert format %d color_range %d: %d\n",
2519 pix_buf_pool = VTCompressionSessionGetPixelBufferPool(vtctx->
session);
2520 if (!pix_buf_pool) {
2527 vtstatus = VTCompressionSessionPrepareToEncodeFrames(vtctx->
session);
2528 if (vtstatus == kVTInvalidSessionErr) {
2533 pix_buf_pool = VTCompressionSessionGetPixelBufferPool(vtctx->
session);
2535 if (!pix_buf_pool) {
2541 "kVTInvalidSessionErr error.\n");
2544 status = CVPixelBufferPoolCreatePixelBuffer(
NULL,
2565 CFDictionaryRef* dict_out)
2567 CFDictionaryRef dict =
NULL;
2569 const void *keys[] = { kVTEncodeFrameOptionKey_ForceKeyFrame };
2570 const void *vals[] = { kCFBooleanTrue };
2572 dict = CFDictionaryCreate(
NULL, keys, vals, 1,
NULL,
NULL);
2573 if(!dict)
return AVERROR(ENOMEM);
2585 CFDictionaryRef frame_dict =
NULL;
2586 CVPixelBufferRef cv_img =
NULL;
2604 if (vtctx->
a53_cc && side_data && side_data->
size) {
2613 status = VTCompressionSessionEncodeFrame(
2632 CFRelease(frame_dict);
2650 CMSampleBufferRef buf =
NULL;
2689 if (
status)
goto end_nopkt;
2690 if (!buf)
goto end_nopkt;
2695 if (
status)
goto end_nopkt;
2707 CFStringRef profile_level,
2708 CFNumberRef gamma_level,
2709 CFDictionaryRef enc_info,
2710 CFDictionaryRef pixel_buffer_info)
2714 CVPixelBufferPoolRef pool =
NULL;
2715 CVPixelBufferRef pix_buf =
NULL;
2717 CMSampleBufferRef buf =
NULL;
2734 pool = VTCompressionSessionGetPixelBufferPool(vtctx->
session);
2741 status = CVPixelBufferPoolCreatePixelBuffer(
NULL,
2745 if(
status != kCVReturnSuccess){
2763 "Error sending frame for extradata: %d\n",
2790 CVPixelBufferRelease(pix_buf);
2818 VTCompressionSessionCompleteFrames(vtctx->
session,
2848 #ifdef kCFCoreFoundationVersionNumber10_7
2853 #if HAVE_KCVPIXELFORMATTYPE_420YPCBCR10BIPLANARVIDEORANGE
2856 #if HAVE_KCVPIXELFORMATTYPE_422YPCBCR8BIPLANARVIDEORANGE
2859 #if HAVE_KCVPIXELFORMATTYPE_422YPCBCR10BIPLANARVIDEORANGE
2862 #if HAVE_KCVPIXELFORMATTYPE_422YPCBCR16BIPLANARVIDEORANGE
2865 #if HAVE_KCVPIXELFORMATTYPE_444YPCBCR8BIPLANARVIDEORANGE
2868 #if HAVE_KCVPIXELFORMATTYPE_444YPCBCR10BIPLANARVIDEORANGE
2871 #if HAVE_KCVPIXELFORMATTYPE_444YPCBCR16BIPLANARVIDEORANGE
2878 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
2879 #define COMMON_OPTIONS \
2880 { "allow_sw", "Allow software encoding", OFFSET(allow_sw), AV_OPT_TYPE_BOOL, \
2881 { .i64 = 0 }, 0, 1, VE }, \
2882 { "require_sw", "Require software encoding", OFFSET(require_sw), AV_OPT_TYPE_BOOL, \
2883 { .i64 = 0 }, 0, 1, VE }, \
2884 { "realtime", "Hint that encoding should happen in real-time if not faster (e.g. capturing from camera).", \
2885 OFFSET(realtime), AV_OPT_TYPE_BOOL, { .i64 = 0 }, -1, 1, VE }, \
2886 { "frames_before", "Other frames will come before the frames in this session. This helps smooth concatenation issues.", \
2887 OFFSET(frames_before), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
2888 { "frames_after", "Other frames will come after the frames in this session. This helps smooth concatenation issues.", \
2889 OFFSET(frames_after), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
2890 { "prio_speed", "prioritize encoding speed", OFFSET(prio_speed), AV_OPT_TYPE_BOOL, \
2891 { .i64 = -1 }, -1, 1, VE }, \
2892 { "power_efficient", "Set to 1 to enable more power-efficient encoding if supported.", \
2893 OFFSET(power_efficient), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE }, \
2894 { "max_ref_frames", \
2895 "Sets the maximum number of reference frames. This only has an effect when the value is less than the maximum allowed by the profile/level.", \
2896 OFFSET(max_ref_frames), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
2903 #define OFFSET(x) offsetof(VTEncContext, x)
2914 {
"1.3",
"Level 1.3, only available with Baseline Profile", 0,
AV_OPT_TYPE_CONST, { .i64 = 13 }, INT_MIN, INT_MAX,
VE, .unit =
"level" },
2915 {
"3.0",
"Level 3.0", 0,
AV_OPT_TYPE_CONST, { .i64 = 30 }, INT_MIN, INT_MAX,
VE, .unit =
"level" },
2916 {
"3.1",
"Level 3.1", 0,
AV_OPT_TYPE_CONST, { .i64 = 31 }, INT_MIN, INT_MAX,
VE, .unit =
"level" },
2917 {
"3.2",
"Level 3.2", 0,
AV_OPT_TYPE_CONST, { .i64 = 32 }, INT_MIN, INT_MAX,
VE, .unit =
"level" },
2918 {
"4.0",
"Level 4.0", 0,
AV_OPT_TYPE_CONST, { .i64 = 40 }, INT_MIN, INT_MAX,
VE, .unit =
"level" },
2919 {
"4.1",
"Level 4.1", 0,
AV_OPT_TYPE_CONST, { .i64 = 41 }, INT_MIN, INT_MAX,
VE, .unit =
"level" },
2920 {
"4.2",
"Level 4.2", 0,
AV_OPT_TYPE_CONST, { .i64 = 42 }, INT_MIN, INT_MAX,
VE, .unit =
"level" },
2921 {
"5.0",
"Level 5.0", 0,
AV_OPT_TYPE_CONST, { .i64 = 50 }, INT_MIN, INT_MAX,
VE, .unit =
"level" },
2922 {
"5.1",
"Level 5.1", 0,
AV_OPT_TYPE_CONST, { .i64 = 51 }, INT_MIN, INT_MAX,
VE, .unit =
"level" },
2923 {
"5.2",
"Level 5.2", 0,
AV_OPT_TYPE_CONST, { .i64 = 52 }, INT_MIN, INT_MAX,
VE, .unit =
"level" },
2933 {
"constant_bit_rate",
"Require constant bit rate (macOS 13 or newer)",
OFFSET(constant_bit_rate),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VE },
2934 {
"max_slice_bytes",
"Set the maximum number of bytes in an H.264 slice.",
OFFSET(max_slice_bytes),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX,
VE },
2954 .
p.
name =
"h264_videotoolbox",
2975 {
"alpha_quality",
"Compression quality for the alpha channel",
OFFSET(alpha_quality),
AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0.0, 1.0,
VE },
2977 {
"constant_bit_rate",
"Require constant bit rate (macOS 13 or newer)",
OFFSET(constant_bit_rate),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VE },
2991 .
p.
name =
"hevc_videotoolbox",
3006 .p.wrapper_name =
"videotoolbox",
3032 .
p.
name =
"prores_videotoolbox",
3047 .p.wrapper_name =
"videotoolbox",