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)
287 CFStringRef encoder_id =
NULL;
289 CFIndex length, max_size;
300 length = CFStringGetLength(encoder_id);
301 max_size = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
304 CFRelease(encoder_id);
308 CFStringGetCString(encoder_id,
311 kCFStringEncodingUTF8);
314 CFRelease(encoder_id);
321 CFStringRef profile_level,
322 CFNumberRef gamma_level,
323 CFDictionaryRef enc_info,
324 CFDictionaryRef pixel_buffer_info);
351 CFRelease(
info->cm_buffer);
429 *buf =
info->cm_buffer;
432 }
else if (
info->sei) {
470 CMSampleBufferRef sample_buffer,
477 size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
478 CMBlockBufferRef
block = CMSampleBufferGetDataBuffer(sample_buffer);
480 if (length_code_size > 4)
483 while (
offset < src_size) {
493 if (
status != kCMBlockBufferNoErr) {
497 for (
i = 0;
i < length_code_size;
i++) {
499 box_len |= size_buf[
i];
502 curr_src_len = box_len + length_code_size;
514 double alpha_quality)
529 return MKBETAG(
'a',
'p',
'c',
'o');
531 return MKBETAG(
'a',
'p',
'c',
's');
533 return MKBETAG(
'a',
'p',
'c',
'n');
535 return MKBETAG(
'a',
'p',
'c',
'h');
537 return MKBETAG(
'a',
'p',
'4',
'h');
539 return MKBETAG(
'a',
'p',
'4',
'x');
546 desc->log2_chroma_w == 0))
547 return MKBETAG(
'a',
'p',
'4',
'h');
549 return MKBETAG(
'a',
'p',
'c',
'n');
565 CMVideoFormatDescriptionRef vid_fmt,
569 size_t total_size = 0;
571 int is_count_bad = 0;
586 for (
i = 0;
i < ps_count || is_count_bad;
i++) {
600 if (
i > 0 && is_count_bad)
status = 0;
619 CMVideoFormatDescriptionRef vid_fmt,
625 int is_count_bad = 0;
643 for (
i = 0;
i < ps_count || is_count_bad;
i++) {
655 if (
i > 0 && is_count_bad)
status = 0;
661 if (dst_size < next_offset) {
669 memcpy(dst +
offset, ps, ps_size);
684 CMVideoFormatDescriptionRef vid_fmt;
688 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
714 CFDataRef
data = CMFormatDescriptionGetExtension(vid_fmt, kCMFormatDescriptionExtension_VerbatimSampleDescription);
715 if (
data && CFGetTypeID(
data) == CFDataGetTypeID()) {
716 CFIndex
size = CFDataGetLength(
data);
732 void *sourceFrameCtx,
734 VTEncodeInfoFlags
flags,
735 CMSampleBufferRef sample_buffer)
751 if (!sample_buffer) {
768 CMSampleBufferRef sample_buffer,
772 CMVideoFormatDescriptionRef vid_fmt;
776 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
804 CFStringRef *profile_level_val)
814 *profile_level_val =
NULL;
821 switch (vtctx->
level) {
822 case 0: *profile_level_val =
823 compat_keys.kVTProfileLevel_H264_Baseline_AutoLevel;
break;
824 case 13: *profile_level_val = kVTProfileLevel_H264_Baseline_1_3;
break;
825 case 30: *profile_level_val = kVTProfileLevel_H264_Baseline_3_0;
break;
826 case 31: *profile_level_val = kVTProfileLevel_H264_Baseline_3_1;
break;
827 case 32: *profile_level_val = kVTProfileLevel_H264_Baseline_3_2;
break;
828 case 40: *profile_level_val =
829 compat_keys.kVTProfileLevel_H264_Baseline_4_0;
break;
830 case 41: *profile_level_val = kVTProfileLevel_H264_Baseline_4_1;
break;
831 case 42: *profile_level_val =
832 compat_keys.kVTProfileLevel_H264_Baseline_4_2;
break;
833 case 50: *profile_level_val =
834 compat_keys.kVTProfileLevel_H264_Baseline_5_0;
break;
835 case 51: *profile_level_val =
836 compat_keys.kVTProfileLevel_H264_Baseline_5_1;
break;
837 case 52: *profile_level_val =
838 compat_keys.kVTProfileLevel_H264_Baseline_5_2;
break;
843 *profile_level_val =
compat_keys.kVTProfileLevel_H264_ConstrainedBaseline_AutoLevel;
845 if (vtctx->
level != 0) {
848 "Level is auto-selected when constrained-baseline "
849 "profile is used. The output may be encoded with a "
850 "different level.\n");
855 switch (vtctx->
level) {
856 case 0: *profile_level_val =
857 compat_keys.kVTProfileLevel_H264_Main_AutoLevel;
break;
858 case 30: *profile_level_val = kVTProfileLevel_H264_Main_3_0;
break;
859 case 31: *profile_level_val = kVTProfileLevel_H264_Main_3_1;
break;
860 case 32: *profile_level_val = kVTProfileLevel_H264_Main_3_2;
break;
861 case 40: *profile_level_val = kVTProfileLevel_H264_Main_4_0;
break;
862 case 41: *profile_level_val = kVTProfileLevel_H264_Main_4_1;
break;
863 case 42: *profile_level_val =
865 case 50: *profile_level_val = kVTProfileLevel_H264_Main_5_0;
break;
866 case 51: *profile_level_val =
868 case 52: *profile_level_val =
874 *profile_level_val =
compat_keys.kVTProfileLevel_H264_ConstrainedHigh_AutoLevel;
876 if (vtctx->
level != 0) {
879 "Level is auto-selected when constrained-high profile "
880 "is used. The output may be encoded with a different "
886 switch (vtctx->
level) {
887 case 0: *profile_level_val =
888 compat_keys.kVTProfileLevel_H264_High_AutoLevel;
break;
889 case 30: *profile_level_val =
891 case 31: *profile_level_val =
893 case 32: *profile_level_val =
895 case 40: *profile_level_val =
897 case 41: *profile_level_val =
899 case 42: *profile_level_val =
901 case 50: *profile_level_val = kVTProfileLevel_H264_High_5_0;
break;
902 case 51: *profile_level_val =
904 case 52: *profile_level_val =
909 switch (vtctx->
level) {
910 case 0: *profile_level_val =
911 compat_keys.kVTProfileLevel_H264_Extended_AutoLevel;
break;
912 case 50: *profile_level_val =
913 compat_keys.kVTProfileLevel_H264_Extended_5_0;
break;
918 if (!*profile_level_val) {
933 CFStringRef *profile_level_val)
942 *profile_level_val =
NULL;
956 "main profile with %d bit input\n",
bit_depth);
963 "Invalid main10 profile with %d bit input\n",
bit_depth);
971 if (!*profile_level_val) {
982 int* av_pixel_format,
985 const char *range_name;
991 if (*av_pixel_format)
996 "Could not get pixel format for color format '%s' range '%s'.\n",
998 range_name ? range_name :
"Unknown");
1007 CFDictionarySetValue(dict,
1008 kCVImageBufferColorPrimariesKey,
1013 CFDictionarySetValue(dict,
1014 kCVImageBufferTransferFunctionKey,
1019 CFDictionarySetValue(dict,
1020 kCVImageBufferYCbCrMatrixKey,
1026 CFMutableDictionaryRef* dict)
1028 CFNumberRef cv_color_format_num =
NULL;
1029 CFNumberRef width_num =
NULL;
1030 CFNumberRef height_num =
NULL;
1031 CFMutableDictionaryRef pixel_buffer_info =
NULL;
1032 int cv_color_format;
1040 pixel_buffer_info = CFDictionaryCreateMutable(
1041 kCFAllocatorDefault,
1043 &kCFCopyStringDictionaryKeyCallBacks,
1044 &kCFTypeDictionaryValueCallBacks);
1046 if (!pixel_buffer_info)
goto pbinfo_nomem;
1048 cv_color_format_num = CFNumberCreate(kCFAllocatorDefault,
1049 kCFNumberSInt32Type,
1051 if (!cv_color_format_num)
goto pbinfo_nomem;
1053 CFDictionarySetValue(pixel_buffer_info,
1054 kCVPixelBufferPixelFormatTypeKey,
1055 cv_color_format_num);
1058 width_num = CFNumberCreate(kCFAllocatorDefault,
1059 kCFNumberSInt32Type,
1061 if (!width_num)
goto pbinfo_nomem;
1063 CFDictionarySetValue(pixel_buffer_info,
1064 kCVPixelBufferWidthKey,
1068 height_num = CFNumberCreate(kCFAllocatorDefault,
1069 kCFNumberSInt32Type,
1071 if (!height_num)
goto pbinfo_nomem;
1073 CFDictionarySetValue(pixel_buffer_info,
1074 kCVPixelBufferHeightKey,
1080 *dict = pixel_buffer_info;
1087 if (pixel_buffer_info) CFRelease(pixel_buffer_info);
1093 CFNumberRef *gamma_level)
1097 *gamma_level =
NULL;
1105 *gamma_level = CFNumberCreate(
NULL, kCFNumberFloat32Type, &gamma);
1117 const char *print_option_name,
1123 if (
status == kVTPropertyNotSupportedErr) {
1126 "This device does not support the %s option. Value ignored.\n",
1128 }
else if (
status != 0) {
1131 "Error setting %s: Error %d\n",
1139 const char* print_option_name,
1141 CFNumberRef value_cfnum = CFNumberCreate(kCFAllocatorDefault,
1145 if (value_cfnum ==
NULL) {
1151 CFRelease(value_cfnum);
1158 CFStringRef profile_level,
1159 CFNumberRef gamma_level,
1160 CFDictionaryRef enc_info,
1161 CFDictionaryRef pixel_buffer_info,
1162 bool constant_bit_rate,
1163 VTCompressionSessionRef *session)
1169 CFNumberRef bit_rate_num;
1170 CFNumberRef quality_num;
1171 CFNumberRef bytes_per_second;
1172 CFNumberRef one_second;
1173 CFArrayRef data_rate_limits;
1174 int64_t bytes_per_second_value = 0;
1175 int64_t one_second_value = 0;
1178 int status = VTCompressionSessionCreate(kCFAllocatorDefault,
1184 kCFAllocatorDefault,
1192 #if !TARGET_OS_IPHONE
1194 av_log(avctx,
AV_LOG_ERROR,
"Try -allow_sw 1. The hardware encoder may be busy, or not supported.\n");
1201 #if defined (MAC_OS_X_VERSION_10_13) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_13)
1202 if (__builtin_available(macOS 10.13, *)) {
1203 status = VTCopySupportedPropertyDictionaryForEncoder(avctx->
width,
1222 av_log(avctx,
AV_LOG_ERROR,
"Error: -q:v qscale not available for encoder. Use -b:v bitrate instead.\n");
1228 quality_num = CFNumberCreate(kCFAllocatorDefault,
1229 kCFNumberFloat32Type,
1231 if (!quality_num)
return AVERROR(ENOMEM);
1234 kVTCompressionPropertyKey_Quality,
1236 CFRelease(quality_num);
1238 bit_rate_num = CFNumberCreate(kCFAllocatorDefault,
1239 kCFNumberSInt32Type,
1241 if (!bit_rate_num)
return AVERROR(ENOMEM);
1243 if (constant_bit_rate) {
1245 compat_keys.kVTCompressionPropertyKey_ConstantBitRate,
1247 if (
status == kVTPropertyNotSupportedErr) {
1248 av_log(avctx,
AV_LOG_ERROR,
"Error: -constant_bit_rate true is not supported by the encoder.\n");
1253 kVTCompressionPropertyKey_AverageBitRate,
1257 CFRelease(bit_rate_num);
1267 compat_keys.kVTCompressionPropertyKey_PrioritizeEncodingSpeedOverQuality,
1268 vtctx->
prio_speed ? kCFBooleanTrue : kCFBooleanFalse);
1270 av_log(avctx,
AV_LOG_WARNING,
"PrioritizeEncodingSpeedOverQuality property is not supported on this device. Ignoring.\n");
1276 bytes_per_second_value = max_rate >> 3;
1277 bytes_per_second = CFNumberCreate(kCFAllocatorDefault,
1278 kCFNumberSInt64Type,
1279 &bytes_per_second_value);
1280 if (!bytes_per_second) {
1283 one_second_value = 1;
1284 one_second = CFNumberCreate(kCFAllocatorDefault,
1285 kCFNumberSInt64Type,
1288 CFRelease(bytes_per_second);
1291 nums[0] = (
void *)bytes_per_second;
1292 nums[1] = (
void *)one_second;
1293 data_rate_limits = CFArrayCreate(kCFAllocatorDefault,
1294 (
const void **)nums,
1296 &kCFTypeArrayCallBacks);
1298 if (!data_rate_limits) {
1299 CFRelease(bytes_per_second);
1300 CFRelease(one_second);
1304 kVTCompressionPropertyKey_DataRateLimits,
1307 CFRelease(bytes_per_second);
1308 CFRelease(one_second);
1309 CFRelease(data_rate_limits);
1323 CFNumberRef alpha_quality_num = CFNumberCreate(kCFAllocatorDefault,
1324 kCFNumberDoubleType,
1326 if (!alpha_quality_num)
return AVERROR(ENOMEM);
1329 compat_keys.kVTCompressionPropertyKey_TargetQualityForAlpha,
1331 CFRelease(alpha_quality_num);
1336 "Error setting alpha quality: %d\n",
1342 if (profile_level) {
1344 kVTCompressionPropertyKey_ProfileLevel,
1347 av_log(avctx,
AV_LOG_ERROR,
"Error setting profile/level property: %d. Output will be encoded using a supported profile/level combination.\n",
status);
1352 CFNumberRef interval = CFNumberCreate(kCFAllocatorDefault,
1360 kVTCompressionPropertyKey_MaxKeyFrameInterval,
1362 CFRelease(interval);
1372 kVTCompressionPropertyKey_MoreFramesBeforeStart,
1375 if (
status == kVTPropertyNotSupportedErr) {
1376 av_log(avctx,
AV_LOG_WARNING,
"frames_before property is not supported on this device. Ignoring.\n");
1384 kVTCompressionPropertyKey_MoreFramesAfterEnd,
1387 if (
status == kVTPropertyNotSupportedErr) {
1388 av_log(avctx,
AV_LOG_WARNING,
"frames_after property is not supported on this device. Ignoring.\n");
1397 CFMutableDictionaryRef par;
1404 num = CFNumberCreate(kCFAllocatorDefault,
1408 den = CFNumberCreate(kCFAllocatorDefault,
1414 par = CFDictionaryCreateMutable(kCFAllocatorDefault,
1416 &kCFCopyStringDictionaryKeyCallBacks,
1417 &kCFTypeDictionaryValueCallBacks);
1419 if (!par || !num || !den) {
1420 if (par) CFRelease(par);
1421 if (num) CFRelease(num);
1422 if (den) CFRelease(den);
1427 CFDictionarySetValue(
1429 kCMFormatDescriptionKey_PixelAspectRatioHorizontalSpacing,
1432 CFDictionarySetValue(
1434 kCMFormatDescriptionKey_PixelAspectRatioVerticalSpacing,
1438 kVTCompressionPropertyKey_PixelAspectRatio,
1448 "Error setting pixel aspect ratio to %d:%d: %d.\n",
1460 kVTCompressionPropertyKey_TransferFunction,
1471 kVTCompressionPropertyKey_YCbCrMatrix,
1482 kVTCompressionPropertyKey_ColorPrimaries,
1492 kCVImageBufferGammaLevelKey,
1502 kVTCompressionPropertyKey_AllowFrameReordering,
1517 compat_keys.kVTCompressionPropertyKey_H264EntropyMode,
1528 vtctx->
realtime ? kCFBooleanTrue : kCFBooleanFalse);
1537 compat_keys.kVTCompressionPropertyKey_AllowOpenGOP,
1542 if (avctx->
qmin >= 0) {
1544 compat_keys.kVTCompressionPropertyKey_MinAllowedFrameQP,
1553 if (avctx->
qmax >= 0) {
1555 compat_keys.kVTCompressionPropertyKey_MaxAllowedFrameQP,
1566 kVTCompressionPropertyKey_MaxH264SliceBytes,
1577 compat_keys.kVTCompressionPropertyKey_MaximizePowerEfficiency,
1584 compat_keys.kVTCompressionPropertyKey_ReferenceBufferCount,
1593 status = VTCompressionSessionPrepareToEncodeFrames(vtctx->
session);
1604 CFMutableDictionaryRef enc_info;
1605 CFMutableDictionaryRef pixel_buffer_info =
NULL;
1608 CFStringRef profile_level =
NULL;
1609 CFNumberRef gamma_level =
NULL;
1618 #if defined(MAC_OS_X_VERSION_10_9) && !TARGET_OS_IPHONE && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_9)
1620 if (__builtin_available(macOS 10.10, *)) {
1621 VTRegisterProfessionalVideoWorkflowVideoEncoders();
1633 av_log(avctx,
AV_LOG_WARNING,
"Cannot use B-frames with baseline profile. Output will not contain B-frames.\n");
1638 av_log(avctx,
AV_LOG_WARNING,
"CABAC entropy requires 'main' or 'high' profile, but baseline was requested. Encode will not use CABAC entropy.\n");
1653 enc_info = CFDictionaryCreateMutable(
1654 kCFAllocatorDefault,
1656 &kCFCopyStringDictionaryKeyCallBacks,
1657 &kCFTypeDictionaryValueCallBacks
1660 if (!enc_info)
return AVERROR(ENOMEM);
1662 #if !TARGET_OS_IPHONE
1664 CFDictionarySetValue(enc_info,
1665 compat_keys.kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
1668 CFDictionarySetValue(enc_info,
1669 compat_keys.kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder,
1672 CFDictionarySetValue(enc_info,
1673 compat_keys.kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
1680 CFDictionarySetValue(enc_info,
1681 compat_keys.kVTVideoEncoderSpecification_EnableLowLatencyRateControl,
1721 CFRelease(gamma_level);
1723 if (pixel_buffer_info)
1724 CFRelease(pixel_buffer_info);
1726 CFRelease(enc_info);
1734 CFBooleanRef has_b_frames_cfbool;
1749 kVTCompressionPropertyKey_AllowFrameReordering,
1750 kCFAllocatorDefault,
1751 &has_b_frames_cfbool);
1753 if (!
status && has_b_frames_cfbool) {
1756 if (CFBooleanGetValue(has_b_frames_cfbool))
1760 CFRelease(has_b_frames_cfbool);
1769 CFArrayRef attachments;
1770 CFDictionaryRef attachment;
1771 CFBooleanRef not_sync;
1774 attachments = CMSampleBufferGetSampleAttachmentsArray(
buffer,
false);
1775 len = !attachments ? 0 : CFArrayGetCount(attachments);
1778 *is_key_frame =
true;
1782 attachment = CFArrayGetValueAtIndex(attachments, 0);
1784 if (CFDictionaryGetValueIfPresent(attachment,
1785 kCMSampleAttachmentKey_NotSync,
1786 (
const void **)¬_sync))
1788 *is_key_frame = !CFBooleanGetValue(not_sync);
1790 *is_key_frame =
true;
1811 size_t sei_payload_size = 0;
1812 uint8_t *nal_start = nal_data;
1818 nal_type = *nal_data & 0x1F;
1825 if (nal_data[nal_size - 1] == 0x80)
1828 while (nal_size > 0 && *nal_data > 0) {
1832 }
while (nal_size > 0 && *nal_data == 0xFF);
1840 sei_payload_size += *nal_data;
1843 }
while (nal_size > 0 && *nal_data == 0xFF);
1845 if (nal_size < sei_payload_size) {
1850 nal_data += sei_payload_size;
1851 nal_size -= sei_payload_size;
1854 *sei_end = nal_data;
1856 return nal_data - nal_start + 1;
1876 uint8_t* dst_end = dst + dst_size;
1877 const uint8_t* src_end =
src + src_size;
1878 int start_at = dst_offset > 2 ? dst_offset - 2 : 0;
1880 for (
i = start_at;
i < dst_offset &&
i < dst_size;
i++) {
1889 for (;
src < src_end;
src++, dst++) {
1891 int insert_ep3_byte = *
src <= 3;
1892 if (insert_ep3_byte) {
1910 wrote_bytes = dst - dst_start;
1913 return -wrote_bytes;
1923 uint8_t *sei_start = dst;
1924 size_t remaining_sei_size =
sei->size;
1925 size_t remaining_dst_size = dst_size;
1930 if (!remaining_dst_size)
1933 while (sei_type && remaining_dst_size != 0) {
1934 int sei_byte = sei_type > 255 ? 255 : sei_type;
1937 sei_type -= sei_byte;
1939 remaining_dst_size--;
1945 while (remaining_sei_size && remaining_dst_size != 0) {
1946 int size_byte = remaining_sei_size > 255 ? 255 : remaining_sei_size;
1949 remaining_sei_size -= size_byte;
1951 remaining_dst_size--;
1954 if (remaining_dst_size < sei->
size)
1957 header_bytes = dst - sei_start;
1965 if (bytes_written < 0)
1968 bytes_written += header_bytes;
1969 return bytes_written;
1993 size_t length_code_size,
1994 CMSampleBufferRef sample_buffer,
1999 size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
2000 size_t remaining_src_size = src_size;
2001 size_t remaining_dst_size = dst_size;
2002 size_t src_offset = 0;
2005 uint8_t size_buf[4];
2007 CMBlockBufferRef
block = CMSampleBufferGetDataBuffer(sample_buffer);
2009 if (length_code_size > 4) {
2013 while (remaining_src_size > 0) {
2014 size_t curr_src_len;
2015 size_t curr_dst_len;
2031 src_offset + length_code_size,
2042 for (
i = 0;
i < length_code_size;
i++) {
2044 box_len |= size_buf[
i];
2057 remaining_dst_size--;
2062 remaining_dst_size);
2064 if (wrote_bytes < 0)
2067 remaining_dst_size -= wrote_bytes;
2068 dst_data += wrote_bytes;
2070 if (remaining_dst_size <= 0)
2076 remaining_dst_size--;
2081 curr_src_len = box_len + length_code_size;
2084 if (remaining_src_size < curr_src_len) {
2088 if (remaining_dst_size < curr_dst_len) {
2096 src_offset + length_code_size,
2111 old_sei_length =
find_sei_end(avctx, dst_box, box_len, &new_sei);
2112 if (old_sei_length < 0)
2118 remaining_dst_size - old_sei_length);
2119 if (wrote_bytes < 0)
2122 if (new_sei + wrote_bytes >= dst_data + remaining_dst_size)
2125 new_sei[wrote_bytes++] = 0x80;
2126 extra_bytes = wrote_bytes - (dst_box + box_len - new_sei);
2128 dst_data += extra_bytes;
2129 remaining_dst_size -= extra_bytes;
2134 src_offset += curr_src_len;
2135 dst_data += curr_dst_len;
2137 remaining_src_size -= curr_src_len;
2138 remaining_dst_size -= curr_dst_len;
2159 if ((
sei->size % 255) == 0)
2162 return copied_size +
sei->size / 255 + 1 +
type / 255 + 1;
2167 CMSampleBufferRef sample_buffer,
2176 size_t length_code_size;
2177 size_t header_size = 0;
2179 size_t out_buf_size;
2180 size_t sei_nalu_size = 0;
2182 int64_t time_base_num;
2186 CMVideoFormatDescriptionRef vid_fmt;
2197 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
2215 sei_nalu_size =
sizeof(
start_code) + 1 + msg_size + 1;
2218 in_buf_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
2219 out_buf_size = header_size +
2248 CMBlockBufferRef buf = CMSampleBufferGetDataBuffer(sample_buffer);
2254 len = CMBlockBufferGetDataLength(buf);
2271 pts = CMSampleBufferGetPresentationTimeStamp(sample_buffer);
2272 dts = CMSampleBufferGetDecodeTimeStamp (sample_buffer);
2274 if (CMTIME_IS_INVALID(dts)) {
2286 pkt->
dts = dts.value / time_base_num - dts_delta;
2303 size_t *contiguous_buf_size)
2307 int av_format =
frame->format;
2320 if (range_guessed) {
2325 "Color range not set for %s. Using MPEG range.\n",
2332 for (
i = 0;
i <
desc->nb_components;
i++) {
2333 int p =
desc->comp[
i].plane;
2335 bool isAlpha = hasAlpha && (p + 1 == *plane_count);
2336 bool isChroma = (p != 0) && !isAlpha;
2337 int shiftw = isChroma ?
desc->log2_chroma_w : 0;
2338 int shifth = isChroma ?
desc->log2_chroma_h : 0;
2339 widths[p] = (avctx->
width + ((1 << shiftw) >> 1)) >> shiftw;
2340 heights[p] = (avctx->
height + ((1 << shifth) >> 1)) >> shifth;
2341 strides[p] =
frame->linesize[p];
2344 *contiguous_buf_size = 0;
2345 for (
i = 0;
i < *plane_count;
i++) {
2346 if (
i < *plane_count - 1 &&
2347 frame->data[
i] + strides[
i] * heights[
i] !=
frame->data[
i + 1]) {
2348 *contiguous_buf_size = 0;
2352 *contiguous_buf_size += strides[
i] * heights[
i];
2361 CVPixelBufferRef cv_img,
2362 const size_t *plane_strides,
2363 const size_t *plane_rows)
2375 status = CVPixelBufferLockBaseAddress(cv_img, 0);
2380 "Error: Could not lock base address of CVPixelBuffer: %d.\n",
2385 if (CVPixelBufferIsPlanar(cv_img)) {
2386 plane_count = CVPixelBufferGetPlaneCount(cv_img);
2388 if (
i == plane_count) {
2389 CVPixelBufferUnlockBaseAddress(cv_img, 0);
2392 "Error: different number of planes in AVFrame and CVPixelBuffer.\n"
2398 dst_addr = (uint8_t*)CVPixelBufferGetBaseAddressOfPlane(cv_img,
i);
2399 src_addr = (uint8_t*)
frame->data[
i];
2400 dst_stride = CVPixelBufferGetBytesPerRowOfPlane(cv_img,
i);
2401 src_stride = plane_strides[
i];
2402 rows = plane_rows[
i];
2404 if (dst_stride == src_stride) {
2405 memcpy(dst_addr, src_addr, src_stride * rows);
2407 copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
2409 for (j = 0; j < rows; j++) {
2410 memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
2415 if (
frame->data[1]) {
2416 CVPixelBufferUnlockBaseAddress(cv_img, 0);
2419 "Error: different number of planes in AVFrame and non-planar CVPixelBuffer.\n"
2425 dst_addr = (uint8_t*)CVPixelBufferGetBaseAddress(cv_img);
2426 src_addr = (uint8_t*)
frame->data[0];
2427 dst_stride = CVPixelBufferGetBytesPerRow(cv_img);
2428 src_stride = plane_strides[0];
2429 rows = plane_rows[0];
2431 if (dst_stride == src_stride) {
2432 memcpy(dst_addr, src_addr, src_stride * rows);
2434 copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
2436 for (j = 0; j < rows; j++) {
2437 memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
2442 status = CVPixelBufferUnlockBaseAddress(cv_img, 0);
2453 CVPixelBufferRef *cv_img)
2461 size_t contiguous_buf_size;
2462 CVPixelBufferPoolRef pix_buf_pool;
2468 *cv_img = (CVPixelBufferRef)
frame->data[3];
2475 memset(widths, 0,
sizeof(widths));
2476 memset(heights, 0,
sizeof(heights));
2477 memset(strides, 0,
sizeof(strides));
2487 &contiguous_buf_size
2494 "Error: Cannot convert format %d color_range %d: %d\n",
2503 pix_buf_pool = VTCompressionSessionGetPixelBufferPool(vtctx->
session);
2504 if (!pix_buf_pool) {
2511 vtstatus = VTCompressionSessionPrepareToEncodeFrames(vtctx->
session);
2512 if (vtstatus == kVTInvalidSessionErr) {
2517 pix_buf_pool = VTCompressionSessionGetPixelBufferPool(vtctx->
session);
2519 if (!pix_buf_pool) {
2525 "kVTInvalidSessionErr error.\n");
2528 status = CVPixelBufferPoolCreatePixelBuffer(
NULL,
2549 CFDictionaryRef* dict_out)
2551 CFDictionaryRef dict =
NULL;
2553 const void *keys[] = { kVTEncodeFrameOptionKey_ForceKeyFrame };
2554 const void *vals[] = { kCFBooleanTrue };
2556 dict = CFDictionaryCreate(
NULL, keys, vals, 1,
NULL,
NULL);
2557 if(!dict)
return AVERROR(ENOMEM);
2569 CFDictionaryRef frame_dict;
2570 CVPixelBufferRef cv_img =
NULL;
2585 if (vtctx->
a53_cc && side_data && side_data->
size) {
2601 status = VTCompressionSessionEncodeFrame(
2611 if (frame_dict) CFRelease(frame_dict);
2631 CMSampleBufferRef buf =
NULL;
2670 if (
status)
goto end_nopkt;
2671 if (!buf)
goto end_nopkt;
2679 if (
status)
goto end_nopkt;
2691 CFStringRef profile_level,
2692 CFNumberRef gamma_level,
2693 CFDictionaryRef enc_info,
2694 CFDictionaryRef pixel_buffer_info)
2698 CVPixelBufferPoolRef pool =
NULL;
2699 CVPixelBufferRef pix_buf =
NULL;
2701 CMSampleBufferRef buf =
NULL;
2714 pool = VTCompressionSessionGetPixelBufferPool(vtctx->
session);
2721 status = CVPixelBufferPoolCreatePixelBuffer(
NULL,
2725 if(
status != kCVReturnSuccess){
2743 "Error sending frame for extradata: %d\n",
2769 CVPixelBufferRelease(pix_buf);
2788 VTCompressionSessionCompleteFrames(vtctx->
session,
2818 #ifdef kCFCoreFoundationVersionNumber10_7
2823 #if HAVE_KCVPIXELFORMATTYPE_420YPCBCR10BIPLANARVIDEORANGE
2826 #if HAVE_KCVPIXELFORMATTYPE_422YPCBCR8BIPLANARVIDEORANGE
2829 #if HAVE_KCVPIXELFORMATTYPE_422YPCBCR10BIPLANARVIDEORANGE
2832 #if HAVE_KCVPIXELFORMATTYPE_422YPCBCR16BIPLANARVIDEORANGE
2835 #if HAVE_KCVPIXELFORMATTYPE_444YPCBCR8BIPLANARVIDEORANGE
2838 #if HAVE_KCVPIXELFORMATTYPE_444YPCBCR10BIPLANARVIDEORANGE
2841 #if HAVE_KCVPIXELFORMATTYPE_444YPCBCR16BIPLANARVIDEORANGE
2848 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
2849 #define COMMON_OPTIONS \
2850 { "allow_sw", "Allow software encoding", OFFSET(allow_sw), AV_OPT_TYPE_BOOL, \
2851 { .i64 = 0 }, 0, 1, VE }, \
2852 { "require_sw", "Require software encoding", OFFSET(require_sw), AV_OPT_TYPE_BOOL, \
2853 { .i64 = 0 }, 0, 1, VE }, \
2854 { "realtime", "Hint that encoding should happen in real-time if not faster (e.g. capturing from camera).", \
2855 OFFSET(realtime), AV_OPT_TYPE_BOOL, { .i64 = 0 }, -1, 1, VE }, \
2856 { "frames_before", "Other frames will come before the frames in this session. This helps smooth concatenation issues.", \
2857 OFFSET(frames_before), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
2858 { "frames_after", "Other frames will come after the frames in this session. This helps smooth concatenation issues.", \
2859 OFFSET(frames_after), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
2860 { "prio_speed", "prioritize encoding speed", OFFSET(prio_speed), AV_OPT_TYPE_BOOL, \
2861 { .i64 = -1 }, -1, 1, VE }, \
2862 { "power_efficient", "Set to 1 to enable more power-efficient encoding if supported.", \
2863 OFFSET(power_efficient), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE }, \
2864 { "max_ref_frames", \
2865 "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.", \
2866 OFFSET(max_ref_frames), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
2873 #define OFFSET(x) offsetof(VTEncContext, x)
2884 {
"1.3",
"Level 1.3, only available with Baseline Profile", 0,
AV_OPT_TYPE_CONST, { .i64 = 13 }, INT_MIN, INT_MAX,
VE, .unit =
"level" },
2885 {
"3.0",
"Level 3.0", 0,
AV_OPT_TYPE_CONST, { .i64 = 30 }, INT_MIN, INT_MAX,
VE, .unit =
"level" },
2886 {
"3.1",
"Level 3.1", 0,
AV_OPT_TYPE_CONST, { .i64 = 31 }, INT_MIN, INT_MAX,
VE, .unit =
"level" },
2887 {
"3.2",
"Level 3.2", 0,
AV_OPT_TYPE_CONST, { .i64 = 32 }, INT_MIN, INT_MAX,
VE, .unit =
"level" },
2888 {
"4.0",
"Level 4.0", 0,
AV_OPT_TYPE_CONST, { .i64 = 40 }, INT_MIN, INT_MAX,
VE, .unit =
"level" },
2889 {
"4.1",
"Level 4.1", 0,
AV_OPT_TYPE_CONST, { .i64 = 41 }, INT_MIN, INT_MAX,
VE, .unit =
"level" },
2890 {
"4.2",
"Level 4.2", 0,
AV_OPT_TYPE_CONST, { .i64 = 42 }, INT_MIN, INT_MAX,
VE, .unit =
"level" },
2891 {
"5.0",
"Level 5.0", 0,
AV_OPT_TYPE_CONST, { .i64 = 50 }, INT_MIN, INT_MAX,
VE, .unit =
"level" },
2892 {
"5.1",
"Level 5.1", 0,
AV_OPT_TYPE_CONST, { .i64 = 51 }, INT_MIN, INT_MAX,
VE, .unit =
"level" },
2893 {
"5.2",
"Level 5.2", 0,
AV_OPT_TYPE_CONST, { .i64 = 52 }, INT_MIN, INT_MAX,
VE, .unit =
"level" },
2903 {
"constant_bit_rate",
"Require constant bit rate (macOS 13 or newer)",
OFFSET(constant_bit_rate),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VE },
2904 {
"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 },
2917 .
p.
name =
"h264_videotoolbox",
2937 {
"alpha_quality",
"Compression quality for the alpha channel",
OFFSET(alpha_quality),
AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0.0, 1.0,
VE },
2939 {
"constant_bit_rate",
"Require constant bit rate (macOS 13 or newer)",
OFFSET(constant_bit_rate),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
VE },
2953 .
p.
name =
"hevc_videotoolbox",
2966 .p.wrapper_name =
"videotoolbox",
2992 .
p.
name =
"prores_videotoolbox",
3005 .p.wrapper_name =
"videotoolbox",