21 #include <VideoToolbox/VideoToolbox.h>
22 #include <CoreVideo/CoreVideo.h>
23 #include <CoreMedia/CoreMedia.h>
24 #include <TargetConditionals.h>
25 #include <Availability.h>
41 #if !HAVE_KCMVIDEOCODECTYPE_HEVC
45 #if !HAVE_KCMVIDEOCODECTYPE_HEVCWITHALPHA
49 #if !HAVE_KCVPIXELFORMATTYPE_420YPCBCR10BIPLANARVIDEORANGE
54 #ifndef TARGET_CPU_ARM64
55 # define TARGET_CPU_ARM64 0
59 size_t parameterSetIndex,
60 const uint8_t **parameterSetPointerOut,
61 size_t *parameterSetSizeOut,
62 size_t *parameterSetCountOut,
63 int *NALUnitHeaderLengthOut);
109 #define GET_SYM(symbol, defaultVal) \
111 CFStringRef* handle = (CFStringRef*)dlsym(RTLD_DEFAULT, #symbol); \
113 compat_keys.symbol = CFSTR(defaultVal); \
115 compat_keys.symbol = *handle; \
121 compat_keys.CMVideoFormatDescriptionGetHEVCParameterSetAtIndex =
124 "CMVideoFormatDescriptionGetHEVCParameterSetAtIndex"
162 "TargetQualityForAlpha");
165 "EnableHardwareAcceleratedVideoEncoder");
167 "RequireHardwareAcceleratedVideoEncoder");
250 CFStringRef profile_level,
251 CFNumberRef gamma_level,
252 CFDictionaryRef enc_info,
253 CFDictionaryRef pixel_buffer_info);
280 CFRelease(
info->cm_buffer);
330 *buf =
info->cm_buffer;
333 }
else if (
info->sei) {
371 CMSampleBufferRef sample_buffer,
378 size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
379 CMBlockBufferRef
block = CMSampleBufferGetDataBuffer(sample_buffer);
381 if (length_code_size > 4)
384 while (
offset < src_size) {
394 for (
i = 0;
i < length_code_size;
i++) {
396 box_len |= size_buf[
i];
399 curr_src_len = box_len + length_code_size;
411 double alpha_quality)
424 return MKBETAG(
'a',
'p',
'c',
'o');
426 return MKBETAG(
'a',
'p',
'c',
's');
428 return MKBETAG(
'a',
'p',
'c',
'n');
430 return MKBETAG(
'a',
'p',
'c',
'h');
432 return MKBETAG(
'a',
'p',
'4',
'h');
434 return MKBETAG(
'a',
'p',
'4',
'x');
441 desc->log2_chroma_w == 0))
442 return MKBETAG(
'a',
'p',
'4',
'h');
444 return MKBETAG(
'a',
'p',
'c',
'n');
460 CMVideoFormatDescriptionRef vid_fmt,
464 size_t total_size = 0;
466 int is_count_bad = 0;
481 for (
i = 0;
i < ps_count || is_count_bad;
i++) {
495 if (
i > 0 && is_count_bad)
status = 0;
514 CMVideoFormatDescriptionRef vid_fmt,
520 int is_count_bad = 0;
538 for (
i = 0;
i < ps_count || is_count_bad;
i++) {
550 if (
i > 0 && is_count_bad)
status = 0;
556 if (dst_size < next_offset) {
564 memcpy(dst +
offset, ps, ps_size);
579 CMVideoFormatDescriptionRef vid_fmt;
583 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
609 CFDataRef
data = CMFormatDescriptionGetExtension(vid_fmt, kCMFormatDescriptionExtension_VerbatimSampleDescription);
610 if (
data && CFGetTypeID(
data) == CFDataGetTypeID()) {
611 CFIndex
size = CFDataGetLength(
data);
627 void *sourceFrameCtx,
629 VTEncodeInfoFlags
flags,
630 CMSampleBufferRef sample_buffer)
646 if (!sample_buffer) {
663 CMSampleBufferRef sample_buffer,
667 CMVideoFormatDescriptionRef vid_fmt;
671 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
699 CFStringRef *profile_level_val)
709 *profile_level_val =
NULL;
716 switch (vtctx->
level) {
717 case 0: *profile_level_val =
718 compat_keys.kVTProfileLevel_H264_Baseline_AutoLevel;
break;
719 case 13: *profile_level_val = kVTProfileLevel_H264_Baseline_1_3;
break;
720 case 30: *profile_level_val = kVTProfileLevel_H264_Baseline_3_0;
break;
721 case 31: *profile_level_val = kVTProfileLevel_H264_Baseline_3_1;
break;
722 case 32: *profile_level_val = kVTProfileLevel_H264_Baseline_3_2;
break;
723 case 40: *profile_level_val =
724 compat_keys.kVTProfileLevel_H264_Baseline_4_0;
break;
725 case 41: *profile_level_val = kVTProfileLevel_H264_Baseline_4_1;
break;
726 case 42: *profile_level_val =
727 compat_keys.kVTProfileLevel_H264_Baseline_4_2;
break;
728 case 50: *profile_level_val =
729 compat_keys.kVTProfileLevel_H264_Baseline_5_0;
break;
730 case 51: *profile_level_val =
731 compat_keys.kVTProfileLevel_H264_Baseline_5_1;
break;
732 case 52: *profile_level_val =
733 compat_keys.kVTProfileLevel_H264_Baseline_5_2;
break;
738 switch (vtctx->
level) {
739 case 0: *profile_level_val =
740 compat_keys.kVTProfileLevel_H264_Main_AutoLevel;
break;
741 case 30: *profile_level_val = kVTProfileLevel_H264_Main_3_0;
break;
742 case 31: *profile_level_val = kVTProfileLevel_H264_Main_3_1;
break;
743 case 32: *profile_level_val = kVTProfileLevel_H264_Main_3_2;
break;
744 case 40: *profile_level_val = kVTProfileLevel_H264_Main_4_0;
break;
745 case 41: *profile_level_val = kVTProfileLevel_H264_Main_4_1;
break;
746 case 42: *profile_level_val =
748 case 50: *profile_level_val = kVTProfileLevel_H264_Main_5_0;
break;
749 case 51: *profile_level_val =
751 case 52: *profile_level_val =
757 switch (vtctx->
level) {
758 case 0: *profile_level_val =
759 compat_keys.kVTProfileLevel_H264_High_AutoLevel;
break;
760 case 30: *profile_level_val =
762 case 31: *profile_level_val =
764 case 32: *profile_level_val =
766 case 40: *profile_level_val =
768 case 41: *profile_level_val =
770 case 42: *profile_level_val =
772 case 50: *profile_level_val = kVTProfileLevel_H264_High_5_0;
break;
773 case 51: *profile_level_val =
775 case 52: *profile_level_val =
780 switch (vtctx->
level) {
781 case 0: *profile_level_val =
782 compat_keys.kVTProfileLevel_H264_Extended_AutoLevel;
break;
783 case 50: *profile_level_val =
784 compat_keys.kVTProfileLevel_H264_Extended_5_0;
break;
789 if (!*profile_level_val) {
804 CFStringRef *profile_level_val)
809 *profile_level_val =
NULL;
824 if (!*profile_level_val) {
835 int* av_pixel_format,
844 return *av_pixel_format ? 0 :
AVERROR(EINVAL);
851 CFDictionarySetValue(dict,
852 kCVImageBufferColorPrimariesKey,
857 CFDictionarySetValue(dict,
858 kCVImageBufferTransferFunctionKey,
863 CFDictionarySetValue(dict,
864 kCVImageBufferYCbCrMatrixKey,
870 CFMutableDictionaryRef* dict)
872 CFNumberRef cv_color_format_num =
NULL;
873 CFNumberRef width_num =
NULL;
874 CFNumberRef height_num =
NULL;
875 CFMutableDictionaryRef pixel_buffer_info =
NULL;
884 pixel_buffer_info = CFDictionaryCreateMutable(
887 &kCFCopyStringDictionaryKeyCallBacks,
888 &kCFTypeDictionaryValueCallBacks);
890 if (!pixel_buffer_info)
goto pbinfo_nomem;
892 cv_color_format_num = CFNumberCreate(kCFAllocatorDefault,
895 if (!cv_color_format_num)
goto pbinfo_nomem;
897 CFDictionarySetValue(pixel_buffer_info,
898 kCVPixelBufferPixelFormatTypeKey,
899 cv_color_format_num);
902 width_num = CFNumberCreate(kCFAllocatorDefault,
905 if (!width_num)
goto pbinfo_nomem;
907 CFDictionarySetValue(pixel_buffer_info,
908 kCVPixelBufferWidthKey,
912 height_num = CFNumberCreate(kCFAllocatorDefault,
915 if (!height_num)
goto pbinfo_nomem;
917 CFDictionarySetValue(pixel_buffer_info,
918 kCVPixelBufferHeightKey,
924 *dict = pixel_buffer_info;
931 if (pixel_buffer_info) CFRelease(pixel_buffer_info);
937 CFStringRef *primaries)
946 *primaries = kCVImageBufferColorPrimaries_EBU_3213;
950 *primaries = kCVImageBufferColorPrimaries_SMPTE_C;
954 *primaries = kCVImageBufferColorPrimaries_ITU_R_709_2;
958 *primaries =
compat_keys.kCVImageBufferColorPrimaries_ITU_R_2020;
971 CFStringRef *transfer_fnc,
972 CFNumberRef *gamma_level)
980 *transfer_fnc =
NULL;
984 *transfer_fnc = kCVImageBufferTransferFunction_ITU_R_709_2;
988 *transfer_fnc = kCVImageBufferTransferFunction_SMPTE_240M_1995;
991 #if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_SMPTE_ST_2084_PQ
993 *transfer_fnc = kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ;
996 #if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_LINEAR
998 *transfer_fnc = kCVImageBufferTransferFunction_Linear;
1001 #if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_ITU_R_2100_HLG
1003 *transfer_fnc = kCVImageBufferTransferFunction_ITU_R_2100_HLG;
1009 *transfer_fnc = kCVImageBufferTransferFunction_UseGamma;
1010 *gamma_level = CFNumberCreate(
NULL, kCFNumberFloat32Type, &gamma);
1015 *transfer_fnc = kCVImageBufferTransferFunction_UseGamma;
1016 *gamma_level = CFNumberCreate(
NULL, kCFNumberFloat32Type, &gamma);
1021 *transfer_fnc =
compat_keys.kCVImageBufferTransferFunction_ITU_R_2020;
1025 *transfer_fnc =
NULL;
1036 *matrix = kCVImageBufferYCbCrMatrix_ITU_R_709_2;
1046 *matrix = kCVImageBufferYCbCrMatrix_ITU_R_601_4;
1050 *matrix = kCVImageBufferYCbCrMatrix_SMPTE_240M_1995;
1054 *matrix =
compat_keys.kCVImageBufferYCbCrMatrix_ITU_R_2020;
1073 CFStringRef profile_level,
1074 CFNumberRef gamma_level,
1075 CFDictionaryRef enc_info,
1076 CFDictionaryRef pixel_buffer_info,
1077 VTCompressionSessionRef *session)
1083 CFNumberRef bit_rate_num;
1084 CFNumberRef quality_num;
1085 CFNumberRef bytes_per_second;
1086 CFNumberRef one_second;
1087 CFArrayRef data_rate_limits;
1088 int64_t bytes_per_second_value = 0;
1089 int64_t one_second_value = 0;
1092 int status = VTCompressionSessionCreate(kCFAllocatorDefault,
1098 kCFAllocatorDefault,
1106 #if !TARGET_OS_IPHONE
1108 av_log(avctx,
AV_LOG_ERROR,
"Try -allow_sw 1. The hardware encoder may be busy, or not supported.\n");
1116 av_log(avctx,
AV_LOG_ERROR,
"Error: -q:v qscale not available for encoder. Use -b:v bitrate instead.\n");
1122 quality_num = CFNumberCreate(kCFAllocatorDefault,
1123 kCFNumberFloat32Type,
1125 if (!quality_num)
return AVERROR(ENOMEM);
1128 kVTCompressionPropertyKey_Quality,
1130 CFRelease(quality_num);
1132 bit_rate_num = CFNumberCreate(kCFAllocatorDefault,
1133 kCFNumberSInt32Type,
1135 if (!bit_rate_num)
return AVERROR(ENOMEM);
1138 kVTCompressionPropertyKey_AverageBitRate,
1140 CFRelease(bit_rate_num);
1150 bytes_per_second_value = max_rate >> 3;
1151 bytes_per_second = CFNumberCreate(kCFAllocatorDefault,
1152 kCFNumberSInt64Type,
1153 &bytes_per_second_value);
1154 if (!bytes_per_second) {
1157 one_second_value = 1;
1158 one_second = CFNumberCreate(kCFAllocatorDefault,
1159 kCFNumberSInt64Type,
1162 CFRelease(bytes_per_second);
1165 nums[0] = (
void *)bytes_per_second;
1166 nums[1] = (
void *)one_second;
1167 data_rate_limits = CFArrayCreate(kCFAllocatorDefault,
1168 (
const void **)nums,
1170 &kCFTypeArrayCallBacks);
1172 if (!data_rate_limits) {
1173 CFRelease(bytes_per_second);
1174 CFRelease(one_second);
1178 kVTCompressionPropertyKey_DataRateLimits,
1181 CFRelease(bytes_per_second);
1182 CFRelease(one_second);
1183 CFRelease(data_rate_limits);
1197 CFNumberRef alpha_quality_num = CFNumberCreate(kCFAllocatorDefault,
1198 kCFNumberDoubleType,
1200 if (!alpha_quality_num)
return AVERROR(ENOMEM);
1203 compat_keys.kVTCompressionPropertyKey_TargetQualityForAlpha,
1205 CFRelease(alpha_quality_num);
1209 if (profile_level) {
1211 kVTCompressionPropertyKey_ProfileLevel,
1214 av_log(avctx,
AV_LOG_ERROR,
"Error setting profile/level property: %d. Output will be encoded using a supported profile/level combination.\n",
status);
1219 CFNumberRef interval = CFNumberCreate(kCFAllocatorDefault,
1227 kVTCompressionPropertyKey_MaxKeyFrameInterval,
1229 CFRelease(interval);
1239 kVTCompressionPropertyKey_MoreFramesBeforeStart,
1242 if (
status == kVTPropertyNotSupportedErr) {
1243 av_log(avctx,
AV_LOG_WARNING,
"frames_before property is not supported on this device. Ignoring.\n");
1251 kVTCompressionPropertyKey_MoreFramesAfterEnd,
1254 if (
status == kVTPropertyNotSupportedErr) {
1255 av_log(avctx,
AV_LOG_WARNING,
"frames_after property is not supported on this device. Ignoring.\n");
1264 CFMutableDictionaryRef par;
1271 num = CFNumberCreate(kCFAllocatorDefault,
1275 den = CFNumberCreate(kCFAllocatorDefault,
1281 par = CFDictionaryCreateMutable(kCFAllocatorDefault,
1283 &kCFCopyStringDictionaryKeyCallBacks,
1284 &kCFTypeDictionaryValueCallBacks);
1286 if (!par || !num || !den) {
1287 if (par) CFRelease(par);
1288 if (num) CFRelease(num);
1289 if (den) CFRelease(den);
1294 CFDictionarySetValue(
1296 kCMFormatDescriptionKey_PixelAspectRatioHorizontalSpacing,
1299 CFDictionarySetValue(
1301 kCMFormatDescriptionKey_PixelAspectRatioVerticalSpacing,
1305 kVTCompressionPropertyKey_PixelAspectRatio,
1315 "Error setting pixel aspect ratio to %d:%d: %d.\n",
1327 kVTCompressionPropertyKey_TransferFunction,
1338 kVTCompressionPropertyKey_YCbCrMatrix,
1349 kVTCompressionPropertyKey_ColorPrimaries,
1359 kCVImageBufferGammaLevelKey,
1369 kVTCompressionPropertyKey_AllowFrameReordering,
1384 compat_keys.kVTCompressionPropertyKey_H264EntropyMode,
1395 vtctx->
realtime ? kCFBooleanTrue : kCFBooleanFalse);
1402 status = VTCompressionSessionPrepareToEncodeFrames(vtctx->
session);
1413 CFMutableDictionaryRef enc_info;
1414 CFMutableDictionaryRef pixel_buffer_info;
1417 CFStringRef profile_level =
NULL;
1418 CFNumberRef gamma_level =
NULL;
1427 #if defined(MAC_OS_X_VERSION_10_9) && !TARGET_OS_IPHONE && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_9)
1429 if (__builtin_available(macOS 10.10, *)) {
1430 VTRegisterProfessionalVideoWorkflowVideoEncoders();
1442 av_log(avctx,
AV_LOG_WARNING,
"Cannot use B-frames with baseline profile. Output will not contain B-frames.\n");
1447 av_log(avctx,
AV_LOG_WARNING,
"CABAC entropy requires 'main' or 'high' profile, but baseline was requested. Encode will not use CABAC entropy.\n");
1462 enc_info = CFDictionaryCreateMutable(
1463 kCFAllocatorDefault,
1465 &kCFCopyStringDictionaryKeyCallBacks,
1466 &kCFTypeDictionaryValueCallBacks
1469 if (!enc_info)
return AVERROR(ENOMEM);
1471 #if !TARGET_OS_IPHONE
1473 CFDictionarySetValue(enc_info,
1474 compat_keys.kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
1477 CFDictionarySetValue(enc_info,
1478 compat_keys.kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder,
1481 CFDictionarySetValue(enc_info,
1482 compat_keys.kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
1492 pixel_buffer_info =
NULL;
1523 CFRelease(gamma_level);
1525 if (pixel_buffer_info)
1526 CFRelease(pixel_buffer_info);
1528 CFRelease(enc_info);
1536 CFBooleanRef has_b_frames_cfbool;
1549 kVTCompressionPropertyKey_AllowFrameReordering,
1550 kCFAllocatorDefault,
1551 &has_b_frames_cfbool);
1553 if (!
status && has_b_frames_cfbool) {
1556 if (CFBooleanGetValue(has_b_frames_cfbool))
1560 CFRelease(has_b_frames_cfbool);
1569 CFArrayRef attachments;
1570 CFDictionaryRef attachment;
1571 CFBooleanRef not_sync;
1574 attachments = CMSampleBufferGetSampleAttachmentsArray(
buffer,
false);
1575 len = !attachments ? 0 : CFArrayGetCount(attachments);
1578 *is_key_frame =
true;
1582 attachment = CFArrayGetValueAtIndex(attachments, 0);
1584 if (CFDictionaryGetValueIfPresent(attachment,
1585 kCMSampleAttachmentKey_NotSync,
1586 (
const void **)¬_sync))
1588 *is_key_frame = !CFBooleanGetValue(not_sync);
1590 *is_key_frame =
true;
1611 size_t sei_payload_size = 0;
1612 int sei_payload_type = 0;
1614 uint8_t *nal_start = nal_data;
1619 nal_type = *nal_data & 0x1F;
1626 if (nal_data[nal_size - 1] == 0x80)
1629 while (nal_size > 0 && *nal_data > 0) {
1631 sei_payload_type += *nal_data;
1634 }
while (nal_size > 0 && *nal_data == 0xFF);
1642 sei_payload_size += *nal_data;
1645 }
while (nal_size > 0 && *nal_data == 0xFF);
1647 if (nal_size < sei_payload_size) {
1652 nal_data += sei_payload_size;
1653 nal_size -= sei_payload_size;
1656 *sei_end = nal_data;
1658 return nal_data - nal_start + 1;
1678 uint8_t* dst_end = dst + dst_size;
1679 const uint8_t* src_end =
src + src_size;
1680 int start_at = dst_offset > 2 ? dst_offset - 2 : 0;
1682 for (
i = start_at;
i < dst_offset &&
i < dst_size;
i++) {
1691 for (;
src < src_end;
src++, dst++) {
1693 int insert_ep3_byte = *
src <= 3;
1694 if (insert_ep3_byte) {
1712 wrote_bytes = dst - dst_start;
1715 return -wrote_bytes;
1725 uint8_t *sei_start = dst;
1726 size_t remaining_sei_size =
sei->size;
1727 size_t remaining_dst_size = dst_size;
1732 if (!remaining_dst_size)
1735 while (sei_type && remaining_dst_size != 0) {
1736 int sei_byte = sei_type > 255 ? 255 : sei_type;
1739 sei_type -= sei_byte;
1741 remaining_dst_size--;
1747 while (remaining_sei_size && remaining_dst_size != 0) {
1748 int size_byte = remaining_sei_size > 255 ? 255 : remaining_sei_size;
1751 remaining_sei_size -= size_byte;
1753 remaining_dst_size--;
1756 if (remaining_dst_size < sei->
size)
1759 header_bytes = dst - sei_start;
1767 if (bytes_written < 0)
1770 bytes_written += header_bytes;
1771 return bytes_written;
1795 size_t length_code_size,
1796 CMSampleBufferRef sample_buffer,
1801 size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
1802 size_t remaining_src_size = src_size;
1803 size_t remaining_dst_size = dst_size;
1804 size_t src_offset = 0;
1807 uint8_t size_buf[4];
1809 CMBlockBufferRef
block = CMSampleBufferGetDataBuffer(sample_buffer);
1811 if (length_code_size > 4) {
1815 while (remaining_src_size > 0) {
1816 size_t curr_src_len;
1817 size_t curr_dst_len;
1833 src_offset + length_code_size,
1844 for (
i = 0;
i < length_code_size;
i++) {
1846 box_len |= size_buf[
i];
1859 remaining_dst_size--;
1864 remaining_dst_size);
1866 if (wrote_bytes < 0)
1869 remaining_dst_size -= wrote_bytes;
1870 dst_data += wrote_bytes;
1872 if (remaining_dst_size <= 0)
1878 remaining_dst_size--;
1883 curr_src_len = box_len + length_code_size;
1886 if (remaining_src_size < curr_src_len) {
1890 if (remaining_dst_size < curr_dst_len) {
1898 src_offset + length_code_size,
1913 old_sei_length =
find_sei_end(avctx, dst_box, box_len, &new_sei);
1914 if (old_sei_length < 0)
1920 remaining_dst_size - old_sei_length);
1921 if (wrote_bytes < 0)
1924 if (new_sei + wrote_bytes >= dst_data + remaining_dst_size)
1927 new_sei[wrote_bytes++] = 0x80;
1928 extra_bytes = wrote_bytes - (dst_box + box_len - new_sei);
1930 dst_data += extra_bytes;
1931 remaining_dst_size -= extra_bytes;
1936 src_offset += curr_src_len;
1937 dst_data += curr_dst_len;
1939 remaining_src_size -= curr_src_len;
1940 remaining_dst_size -= curr_dst_len;
1961 if ((
sei->size % 255) == 0)
1964 return copied_size +
sei->size / 255 + 1 +
type / 255 + 1;
1969 CMSampleBufferRef sample_buffer,
1978 size_t length_code_size;
1979 size_t header_size = 0;
1981 size_t out_buf_size;
1982 size_t sei_nalu_size = 0;
1984 int64_t time_base_num;
1988 CMVideoFormatDescriptionRef vid_fmt;
1999 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
2017 sei_nalu_size =
sizeof(
start_code) + 1 + msg_size + 1;
2020 in_buf_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
2021 out_buf_size = header_size +
2050 CMBlockBufferRef buf = CMSampleBufferGetDataBuffer(sample_buffer);
2056 len = CMBlockBufferGetDataLength(buf);
2073 pts = CMSampleBufferGetPresentationTimeStamp(sample_buffer);
2074 dts = CMSampleBufferGetDecodeTimeStamp (sample_buffer);
2076 if (CMTIME_IS_INVALID(dts)) {
2088 pkt->
dts = dts.value / time_base_num - dts_delta;
2105 size_t *contiguous_buf_size)
2109 int av_format =
frame->format;
2110 int av_color_range =
frame->color_range;
2122 "Could not get pixel format for color format '%s' range '%s'.\n",
2132 if (range_guessed) {
2137 "Color range not set for %s. Using MPEG range.\n",
2144 for (
i = 0;
i <
desc->nb_components;
i++) {
2145 int p =
desc->comp[
i].plane;
2147 bool isAlpha = hasAlpha && (p + 1 == *plane_count);
2148 bool isChroma = (p != 0) && !isAlpha;
2149 int shiftw = isChroma ?
desc->log2_chroma_w : 0;
2150 int shifth = isChroma ?
desc->log2_chroma_h : 0;
2151 widths[p] = (avctx->
width + ((1 << shiftw) >> 1)) >> shiftw;
2152 heights[p] = (avctx->
height + ((1 << shifth) >> 1)) >> shifth;
2153 strides[p] =
frame->linesize[p];
2156 *contiguous_buf_size = 0;
2157 for (
i = 0;
i < *plane_count;
i++) {
2158 if (
i < *plane_count - 1 &&
2159 frame->data[
i] + strides[
i] * heights[
i] !=
frame->data[
i + 1]) {
2160 *contiguous_buf_size = 0;
2164 *contiguous_buf_size += strides[
i] * heights[
i];
2173 CVPixelBufferRef cv_img,
2174 const size_t *plane_strides,
2175 const size_t *plane_rows)
2187 status = CVPixelBufferLockBaseAddress(cv_img, 0);
2192 "Error: Could not lock base address of CVPixelBuffer: %d.\n",
2197 if (CVPixelBufferIsPlanar(cv_img)) {
2198 plane_count = CVPixelBufferGetPlaneCount(cv_img);
2200 if (
i == plane_count) {
2201 CVPixelBufferUnlockBaseAddress(cv_img, 0);
2204 "Error: different number of planes in AVFrame and CVPixelBuffer.\n"
2210 dst_addr = (uint8_t*)CVPixelBufferGetBaseAddressOfPlane(cv_img,
i);
2211 src_addr = (uint8_t*)
frame->data[
i];
2212 dst_stride = CVPixelBufferGetBytesPerRowOfPlane(cv_img,
i);
2213 src_stride = plane_strides[
i];
2214 rows = plane_rows[
i];
2216 if (dst_stride == src_stride) {
2217 memcpy(dst_addr, src_addr, src_stride * rows);
2219 copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
2221 for (j = 0; j < rows; j++) {
2222 memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
2227 if (
frame->data[1]) {
2228 CVPixelBufferUnlockBaseAddress(cv_img, 0);
2231 "Error: different number of planes in AVFrame and non-planar CVPixelBuffer.\n"
2237 dst_addr = (uint8_t*)CVPixelBufferGetBaseAddress(cv_img);
2238 src_addr = (uint8_t*)
frame->data[0];
2239 dst_stride = CVPixelBufferGetBytesPerRow(cv_img);
2240 src_stride = plane_strides[0];
2241 rows = plane_rows[0];
2243 if (dst_stride == src_stride) {
2244 memcpy(dst_addr, src_addr, src_stride * rows);
2246 copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
2248 for (j = 0; j < rows; j++) {
2249 memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
2254 status = CVPixelBufferUnlockBaseAddress(cv_img, 0);
2265 CVPixelBufferRef *cv_img)
2273 size_t contiguous_buf_size;
2274 CVPixelBufferPoolRef pix_buf_pool;
2280 *cv_img = (CVPixelBufferRef)
frame->data[3];
2287 memset(widths, 0,
sizeof(widths));
2288 memset(heights, 0,
sizeof(heights));
2289 memset(strides, 0,
sizeof(strides));
2299 &contiguous_buf_size
2306 "Error: Cannot convert format %d color_range %d: %d\n",
2315 pix_buf_pool = VTCompressionSessionGetPixelBufferPool(vtctx->
session);
2316 if (!pix_buf_pool) {
2323 vtstatus = VTCompressionSessionPrepareToEncodeFrames(vtctx->
session);
2324 if (vtstatus == kVTInvalidSessionErr) {
2329 pix_buf_pool = VTCompressionSessionGetPixelBufferPool(vtctx->
session);
2331 if (!pix_buf_pool) {
2337 "kVTInvalidSessionErr error.\n");
2340 status = CVPixelBufferPoolCreatePixelBuffer(
NULL,
2361 CFDictionaryRef* dict_out)
2363 CFDictionaryRef dict =
NULL;
2365 const void *keys[] = { kVTEncodeFrameOptionKey_ForceKeyFrame };
2366 const void *vals[] = { kCFBooleanTrue };
2368 dict = CFDictionaryCreate(
NULL, keys, vals, 1,
NULL,
NULL);
2369 if(!dict)
return AVERROR(ENOMEM);
2381 CFDictionaryRef frame_dict;
2382 CVPixelBufferRef cv_img =
NULL;
2397 if (vtctx->
a53_cc && side_data && side_data->
size) {
2413 status = VTCompressionSessionEncodeFrame(
2423 if (frame_dict) CFRelease(frame_dict);
2443 CMSampleBufferRef buf =
NULL;
2482 if (
status)
goto end_nopkt;
2483 if (!buf)
goto end_nopkt;
2491 if (
status)
goto end_nopkt;
2503 CFStringRef profile_level,
2504 CFNumberRef gamma_level,
2505 CFDictionaryRef enc_info,
2506 CFDictionaryRef pixel_buffer_info)
2510 CVPixelBufferPoolRef pool =
NULL;
2511 CVPixelBufferRef pix_buf =
NULL;
2513 CMSampleBufferRef buf =
NULL;
2525 pool = VTCompressionSessionGetPixelBufferPool(vtctx->
session);
2531 status = CVPixelBufferPoolCreatePixelBuffer(
NULL,
2535 if(
status != kCVReturnSuccess){
2552 "Error sending frame for extradata: %d\n",
2576 CVPixelBufferRelease(pix_buf);
2598 VTCompressionSessionCompleteFrames(vtctx->
session,
2643 #ifdef kCFCoreFoundationVersionNumber10_7
2648 #if HAVE_KCVPIXELFORMATTYPE_420YPCBCR10BIPLANARVIDEORANGE
2651 #if HAVE_KCVPIXELFORMATTYPE_422YPCBCR8BIPLANARVIDEORANGE
2654 #if HAVE_KCVPIXELFORMATTYPE_422YPCBCR10BIPLANARVIDEORANGE
2657 #if HAVE_KCVPIXELFORMATTYPE_422YPCBCR16BIPLANARVIDEORANGE
2660 #if HAVE_KCVPIXELFORMATTYPE_444YPCBCR8BIPLANARVIDEORANGE
2663 #if HAVE_KCVPIXELFORMATTYPE_444YPCBCR10BIPLANARVIDEORANGE
2666 #if HAVE_KCVPIXELFORMATTYPE_444YPCBCR16BIPLANARVIDEORANGE
2673 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
2674 #define COMMON_OPTIONS \
2675 { "allow_sw", "Allow software encoding", OFFSET(allow_sw), AV_OPT_TYPE_BOOL, \
2676 { .i64 = 0 }, 0, 1, VE }, \
2677 { "require_sw", "Require software encoding", OFFSET(require_sw), AV_OPT_TYPE_BOOL, \
2678 { .i64 = 0 }, 0, 1, VE }, \
2679 { "realtime", "Hint that encoding should happen in real-time if not faster (e.g. capturing from camera).", \
2680 OFFSET(realtime), AV_OPT_TYPE_BOOL, { .i64 = 0 }, -1, 1, VE }, \
2681 { "frames_before", "Other frames will come before the frames in this session. This helps smooth concatenation issues.", \
2682 OFFSET(frames_before), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
2683 { "frames_after", "Other frames will come after the frames in this session. This helps smooth concatenation issues.", \
2684 OFFSET(frames_after), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
2686 #define OFFSET(x) offsetof(VTEncContext, x)
2695 {
"1.3",
"Level 1.3, only available with Baseline Profile", 0,
AV_OPT_TYPE_CONST, { .i64 = 13 }, INT_MIN, INT_MAX,
VE,
"level" },
2696 {
"3.0",
"Level 3.0", 0,
AV_OPT_TYPE_CONST, { .i64 = 30 }, INT_MIN, INT_MAX,
VE,
"level" },
2697 {
"3.1",
"Level 3.1", 0,
AV_OPT_TYPE_CONST, { .i64 = 31 }, INT_MIN, INT_MAX,
VE,
"level" },
2698 {
"3.2",
"Level 3.2", 0,
AV_OPT_TYPE_CONST, { .i64 = 32 }, INT_MIN, INT_MAX,
VE,
"level" },
2699 {
"4.0",
"Level 4.0", 0,
AV_OPT_TYPE_CONST, { .i64 = 40 }, INT_MIN, INT_MAX,
VE,
"level" },
2700 {
"4.1",
"Level 4.1", 0,
AV_OPT_TYPE_CONST, { .i64 = 41 }, INT_MIN, INT_MAX,
VE,
"level" },
2701 {
"4.2",
"Level 4.2", 0,
AV_OPT_TYPE_CONST, { .i64 = 42 }, INT_MIN, INT_MAX,
VE,
"level" },
2702 {
"5.0",
"Level 5.0", 0,
AV_OPT_TYPE_CONST, { .i64 = 50 }, INT_MIN, INT_MAX,
VE,
"level" },
2703 {
"5.1",
"Level 5.1", 0,
AV_OPT_TYPE_CONST, { .i64 = 51 }, INT_MIN, INT_MAX,
VE,
"level" },
2704 {
"5.2",
"Level 5.2", 0,
AV_OPT_TYPE_CONST, { .i64 = 52 }, INT_MIN, INT_MAX,
VE,
"level" },
2726 .
name =
"h264_videotoolbox",
2746 {
"alpha_quality",
"Compression quality for the alpha channel",
OFFSET(alpha_quality),
AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0.0, 1.0,
VE },
2760 .
name =
"hevc_videotoolbox",
2774 .wrapper_name =
"videotoolbox",
2799 .
name =
"prores_videotoolbox",
2813 .wrapper_name =
"videotoolbox",