21 #include <VideoToolbox/VideoToolbox.h>
22 #include <CoreVideo/CoreVideo.h>
23 #include <CoreMedia/CoreMedia.h>
24 #include <TargetConditionals.h>
25 #include <Availability.h>
39 #if !HAVE_KCMVIDEOCODECTYPE_HEVC
43 #if !HAVE_KCMVIDEOCODECTYPE_HEVCWITHALPHA
47 #if !HAVE_KCVPIXELFORMATTYPE_420YPCBCR10BIPLANARVIDEORANGE
52 #ifndef TARGET_CPU_ARM64
53 # define TARGET_CPU_ARM64 0
57 size_t parameterSetIndex,
58 const uint8_t **parameterSetPointerOut,
59 size_t *parameterSetSizeOut,
60 size_t *parameterSetCountOut,
61 int *NALUnitHeaderLengthOut);
107 #define GET_SYM(symbol, defaultVal) \
109 CFStringRef* handle = (CFStringRef*)dlsym(RTLD_DEFAULT, #symbol); \
111 compat_keys.symbol = CFSTR(defaultVal); \
113 compat_keys.symbol = *handle; \
119 compat_keys.CMVideoFormatDescriptionGetHEVCParameterSetAtIndex =
122 "CMVideoFormatDescriptionGetHEVCParameterSetAtIndex"
160 "TargetQualityForAlpha");
163 "EnableHardwareAcceleratedVideoEncoder");
165 "RequireHardwareAcceleratedVideoEncoder");
248 CFStringRef profile_level,
249 CFNumberRef gamma_level,
250 CFDictionaryRef enc_info,
251 CFDictionaryRef pixel_buffer_info);
278 CFRelease(
info->cm_buffer);
328 *buf =
info->cm_buffer;
331 }
else if (
info->sei) {
369 CMSampleBufferRef sample_buffer,
376 size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
377 CMBlockBufferRef
block = CMSampleBufferGetDataBuffer(sample_buffer);
379 if (length_code_size > 4)
382 while (
offset < src_size) {
392 for (
i = 0;
i < length_code_size;
i++) {
394 box_len |= size_buf[
i];
397 curr_src_len = box_len + length_code_size;
409 double alpha_quality)
432 CMVideoFormatDescriptionRef vid_fmt,
436 size_t total_size = 0;
438 int is_count_bad = 0;
453 for (
i = 0;
i < ps_count || is_count_bad;
i++) {
467 if (
i > 0 && is_count_bad)
status = 0;
486 CMVideoFormatDescriptionRef vid_fmt,
492 int is_count_bad = 0;
510 for (
i = 0;
i < ps_count || is_count_bad;
i++) {
522 if (
i > 0 && is_count_bad)
status = 0;
528 if (dst_size < next_offset) {
536 memcpy(dst +
offset, ps, ps_size);
550 CMVideoFormatDescriptionRef vid_fmt;
554 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
584 void *sourceFrameCtx,
586 VTEncodeInfoFlags
flags,
587 CMSampleBufferRef sample_buffer)
603 if (!sample_buffer) {
620 CMSampleBufferRef sample_buffer,
624 CMVideoFormatDescriptionRef vid_fmt;
628 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
656 CFStringRef *profile_level_val)
666 *profile_level_val =
NULL;
673 switch (vtctx->
level) {
674 case 0: *profile_level_val =
675 compat_keys.kVTProfileLevel_H264_Baseline_AutoLevel;
break;
676 case 13: *profile_level_val = kVTProfileLevel_H264_Baseline_1_3;
break;
677 case 30: *profile_level_val = kVTProfileLevel_H264_Baseline_3_0;
break;
678 case 31: *profile_level_val = kVTProfileLevel_H264_Baseline_3_1;
break;
679 case 32: *profile_level_val = kVTProfileLevel_H264_Baseline_3_2;
break;
680 case 40: *profile_level_val =
681 compat_keys.kVTProfileLevel_H264_Baseline_4_0;
break;
682 case 41: *profile_level_val = kVTProfileLevel_H264_Baseline_4_1;
break;
683 case 42: *profile_level_val =
684 compat_keys.kVTProfileLevel_H264_Baseline_4_2;
break;
685 case 50: *profile_level_val =
686 compat_keys.kVTProfileLevel_H264_Baseline_5_0;
break;
687 case 51: *profile_level_val =
688 compat_keys.kVTProfileLevel_H264_Baseline_5_1;
break;
689 case 52: *profile_level_val =
690 compat_keys.kVTProfileLevel_H264_Baseline_5_2;
break;
695 switch (vtctx->
level) {
696 case 0: *profile_level_val =
697 compat_keys.kVTProfileLevel_H264_Main_AutoLevel;
break;
698 case 30: *profile_level_val = kVTProfileLevel_H264_Main_3_0;
break;
699 case 31: *profile_level_val = kVTProfileLevel_H264_Main_3_1;
break;
700 case 32: *profile_level_val = kVTProfileLevel_H264_Main_3_2;
break;
701 case 40: *profile_level_val = kVTProfileLevel_H264_Main_4_0;
break;
702 case 41: *profile_level_val = kVTProfileLevel_H264_Main_4_1;
break;
703 case 42: *profile_level_val =
705 case 50: *profile_level_val = kVTProfileLevel_H264_Main_5_0;
break;
706 case 51: *profile_level_val =
708 case 52: *profile_level_val =
714 switch (vtctx->
level) {
715 case 0: *profile_level_val =
716 compat_keys.kVTProfileLevel_H264_High_AutoLevel;
break;
717 case 30: *profile_level_val =
719 case 31: *profile_level_val =
721 case 32: *profile_level_val =
723 case 40: *profile_level_val =
725 case 41: *profile_level_val =
727 case 42: *profile_level_val =
729 case 50: *profile_level_val = kVTProfileLevel_H264_High_5_0;
break;
730 case 51: *profile_level_val =
732 case 52: *profile_level_val =
737 switch (vtctx->
level) {
738 case 0: *profile_level_val =
739 compat_keys.kVTProfileLevel_H264_Extended_AutoLevel;
break;
740 case 50: *profile_level_val =
741 compat_keys.kVTProfileLevel_H264_Extended_5_0;
break;
746 if (!*profile_level_val) {
761 CFStringRef *profile_level_val)
766 *profile_level_val =
NULL;
781 if (!*profile_level_val) {
792 int* av_pixel_format,
801 kCVPixelFormatType_420YpCbCr8BiPlanarFullRange :
802 kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
805 kCVPixelFormatType_420YpCbCr8PlanarFullRange :
806 kCVPixelFormatType_420YpCbCr8Planar;
808 *av_pixel_format = kCVPixelFormatType_32BGRA;
824 CFDictionarySetValue(dict,
825 kCVImageBufferColorPrimariesKey,
830 CFDictionarySetValue(dict,
831 kCVImageBufferTransferFunctionKey,
836 CFDictionarySetValue(dict,
837 kCVImageBufferYCbCrMatrixKey,
843 CFMutableDictionaryRef* dict)
845 CFNumberRef cv_color_format_num =
NULL;
846 CFNumberRef width_num =
NULL;
847 CFNumberRef height_num =
NULL;
848 CFMutableDictionaryRef pixel_buffer_info =
NULL;
857 pixel_buffer_info = CFDictionaryCreateMutable(
860 &kCFCopyStringDictionaryKeyCallBacks,
861 &kCFTypeDictionaryValueCallBacks);
863 if (!pixel_buffer_info)
goto pbinfo_nomem;
865 cv_color_format_num = CFNumberCreate(kCFAllocatorDefault,
868 if (!cv_color_format_num)
goto pbinfo_nomem;
870 CFDictionarySetValue(pixel_buffer_info,
871 kCVPixelBufferPixelFormatTypeKey,
872 cv_color_format_num);
875 width_num = CFNumberCreate(kCFAllocatorDefault,
878 if (!width_num)
return AVERROR(ENOMEM);
880 CFDictionarySetValue(pixel_buffer_info,
881 kCVPixelBufferWidthKey,
885 height_num = CFNumberCreate(kCFAllocatorDefault,
888 if (!height_num)
goto pbinfo_nomem;
890 CFDictionarySetValue(pixel_buffer_info,
891 kCVPixelBufferHeightKey,
897 *dict = pixel_buffer_info;
904 if (pixel_buffer_info) CFRelease(pixel_buffer_info);
910 CFStringRef *primaries)
919 *primaries = kCVImageBufferColorPrimaries_EBU_3213;
923 *primaries = kCVImageBufferColorPrimaries_SMPTE_C;
927 *primaries = kCVImageBufferColorPrimaries_ITU_R_709_2;
931 *primaries =
compat_keys.kCVImageBufferColorPrimaries_ITU_R_2020;
944 CFStringRef *transfer_fnc,
945 CFNumberRef *gamma_level)
953 *transfer_fnc =
NULL;
957 *transfer_fnc = kCVImageBufferTransferFunction_ITU_R_709_2;
961 *transfer_fnc = kCVImageBufferTransferFunction_SMPTE_240M_1995;
964 #if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_SMPTE_ST_2084_PQ
966 *transfer_fnc = kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ;
969 #if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_LINEAR
971 *transfer_fnc = kCVImageBufferTransferFunction_Linear;
974 #if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_ITU_R_2100_HLG
976 *transfer_fnc = kCVImageBufferTransferFunction_ITU_R_2100_HLG;
982 *transfer_fnc = kCVImageBufferTransferFunction_UseGamma;
983 *gamma_level = CFNumberCreate(
NULL, kCFNumberFloat32Type, &gamma);
988 *transfer_fnc = kCVImageBufferTransferFunction_UseGamma;
989 *gamma_level = CFNumberCreate(
NULL, kCFNumberFloat32Type, &gamma);
994 *transfer_fnc =
compat_keys.kCVImageBufferTransferFunction_ITU_R_2020;
998 *transfer_fnc =
NULL;
1009 *matrix = kCVImageBufferYCbCrMatrix_ITU_R_709_2;
1018 *matrix = kCVImageBufferYCbCrMatrix_ITU_R_601_4;
1022 *matrix = kCVImageBufferYCbCrMatrix_SMPTE_240M_1995;
1026 *matrix =
compat_keys.kCVImageBufferYCbCrMatrix_ITU_R_2020;
1045 CFStringRef profile_level,
1046 CFNumberRef gamma_level,
1047 CFDictionaryRef enc_info,
1048 CFDictionaryRef pixel_buffer_info,
1049 VTCompressionSessionRef *session)
1055 CFNumberRef bit_rate_num;
1056 CFNumberRef quality_num;
1057 CFNumberRef bytes_per_second;
1058 CFNumberRef one_second;
1059 CFArrayRef data_rate_limits;
1060 int64_t bytes_per_second_value = 0;
1061 int64_t one_second_value = 0;
1064 int status = VTCompressionSessionCreate(kCFAllocatorDefault,
1070 kCFAllocatorDefault,
1078 #if !TARGET_OS_IPHONE
1080 av_log(avctx,
AV_LOG_ERROR,
"Try -allow_sw 1. The hardware encoder may be busy, or not supported.\n");
1088 av_log(avctx,
AV_LOG_ERROR,
"Error: -q:v qscale not available for encoder. Use -b:v bitrate instead.\n");
1094 quality_num = CFNumberCreate(kCFAllocatorDefault,
1095 kCFNumberFloat32Type,
1097 if (!quality_num)
return AVERROR(ENOMEM);
1100 kVTCompressionPropertyKey_Quality,
1102 CFRelease(quality_num);
1104 bit_rate_num = CFNumberCreate(kCFAllocatorDefault,
1105 kCFNumberSInt32Type,
1107 if (!bit_rate_num)
return AVERROR(ENOMEM);
1110 kVTCompressionPropertyKey_AverageBitRate,
1112 CFRelease(bit_rate_num);
1122 bytes_per_second_value = max_rate >> 3;
1123 bytes_per_second = CFNumberCreate(kCFAllocatorDefault,
1124 kCFNumberSInt64Type,
1125 &bytes_per_second_value);
1126 if (!bytes_per_second) {
1129 one_second_value = 1;
1130 one_second = CFNumberCreate(kCFAllocatorDefault,
1131 kCFNumberSInt64Type,
1134 CFRelease(bytes_per_second);
1137 nums[0] = (
void *)bytes_per_second;
1138 nums[1] = (
void *)one_second;
1139 data_rate_limits = CFArrayCreate(kCFAllocatorDefault,
1140 (
const void **)nums,
1142 &kCFTypeArrayCallBacks);
1144 if (!data_rate_limits) {
1145 CFRelease(bytes_per_second);
1146 CFRelease(one_second);
1150 kVTCompressionPropertyKey_DataRateLimits,
1153 CFRelease(bytes_per_second);
1154 CFRelease(one_second);
1155 CFRelease(data_rate_limits);
1165 CFNumberRef alpha_quality_num = CFNumberCreate(kCFAllocatorDefault,
1166 kCFNumberDoubleType,
1168 if (!alpha_quality_num)
return AVERROR(ENOMEM);
1171 compat_keys.kVTCompressionPropertyKey_TargetQualityForAlpha,
1173 CFRelease(alpha_quality_num);
1177 if (profile_level) {
1179 kVTCompressionPropertyKey_ProfileLevel,
1182 av_log(avctx,
AV_LOG_ERROR,
"Error setting profile/level property: %d. Output will be encoded using a supported profile/level combination.\n",
status);
1187 CFNumberRef interval = CFNumberCreate(kCFAllocatorDefault,
1195 kVTCompressionPropertyKey_MaxKeyFrameInterval,
1197 CFRelease(interval);
1207 kVTCompressionPropertyKey_MoreFramesBeforeStart,
1210 if (
status == kVTPropertyNotSupportedErr) {
1211 av_log(avctx,
AV_LOG_WARNING,
"frames_before property is not supported on this device. Ignoring.\n");
1219 kVTCompressionPropertyKey_MoreFramesAfterEnd,
1222 if (
status == kVTPropertyNotSupportedErr) {
1223 av_log(avctx,
AV_LOG_WARNING,
"frames_after property is not supported on this device. Ignoring.\n");
1232 CFMutableDictionaryRef par;
1239 num = CFNumberCreate(kCFAllocatorDefault,
1243 den = CFNumberCreate(kCFAllocatorDefault,
1249 par = CFDictionaryCreateMutable(kCFAllocatorDefault,
1251 &kCFCopyStringDictionaryKeyCallBacks,
1252 &kCFTypeDictionaryValueCallBacks);
1254 if (!par || !num || !den) {
1255 if (par) CFRelease(par);
1256 if (num) CFRelease(num);
1257 if (den) CFRelease(den);
1262 CFDictionarySetValue(
1264 kCMFormatDescriptionKey_PixelAspectRatioHorizontalSpacing,
1267 CFDictionarySetValue(
1269 kCMFormatDescriptionKey_PixelAspectRatioVerticalSpacing,
1273 kVTCompressionPropertyKey_PixelAspectRatio,
1283 "Error setting pixel aspect ratio to %d:%d: %d.\n",
1295 kVTCompressionPropertyKey_TransferFunction,
1306 kVTCompressionPropertyKey_YCbCrMatrix,
1317 kVTCompressionPropertyKey_ColorPrimaries,
1327 kCVImageBufferGammaLevelKey,
1337 kVTCompressionPropertyKey_AllowFrameReordering,
1352 compat_keys.kVTCompressionPropertyKey_H264EntropyMode,
1370 status = VTCompressionSessionPrepareToEncodeFrames(vtctx->
session);
1381 CFMutableDictionaryRef enc_info;
1382 CFMutableDictionaryRef pixel_buffer_info;
1385 CFStringRef profile_level;
1386 CFNumberRef gamma_level =
NULL;
1403 av_log(avctx,
AV_LOG_WARNING,
"Cannot use B-frames with baseline profile. Output will not contain B-frames.\n");
1408 av_log(avctx,
AV_LOG_WARNING,
"CABAC entropy requires 'main' or 'high' profile, but baseline was requested. Encode will not use CABAC entropy.\n");
1421 enc_info = CFDictionaryCreateMutable(
1422 kCFAllocatorDefault,
1424 &kCFCopyStringDictionaryKeyCallBacks,
1425 &kCFTypeDictionaryValueCallBacks
1428 if (!enc_info)
return AVERROR(ENOMEM);
1430 #if !TARGET_OS_IPHONE
1432 CFDictionarySetValue(enc_info,
1433 compat_keys.kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
1436 CFDictionarySetValue(enc_info,
1437 compat_keys.kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder,
1440 CFDictionarySetValue(enc_info,
1441 compat_keys.kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
1451 pixel_buffer_info =
NULL;
1482 CFRelease(gamma_level);
1484 if (pixel_buffer_info)
1485 CFRelease(pixel_buffer_info);
1487 CFRelease(enc_info);
1495 CFBooleanRef has_b_frames_cfbool;
1508 kVTCompressionPropertyKey_AllowFrameReordering,
1509 kCFAllocatorDefault,
1510 &has_b_frames_cfbool);
1512 if (!
status && has_b_frames_cfbool) {
1516 CFRelease(has_b_frames_cfbool);
1525 CFArrayRef attachments;
1526 CFDictionaryRef attachment;
1527 CFBooleanRef not_sync;
1530 attachments = CMSampleBufferGetSampleAttachmentsArray(
buffer,
false);
1531 len = !attachments ? 0 : CFArrayGetCount(attachments);
1534 *is_key_frame =
true;
1538 attachment = CFArrayGetValueAtIndex(attachments, 0);
1540 if (CFDictionaryGetValueIfPresent(attachment,
1541 kCMSampleAttachmentKey_NotSync,
1542 (
const void **)¬_sync))
1544 *is_key_frame = !CFBooleanGetValue(not_sync);
1546 *is_key_frame =
true;
1567 size_t sei_payload_size = 0;
1568 int sei_payload_type = 0;
1570 uint8_t *nal_start = nal_data;
1575 nal_type = *nal_data & 0x1F;
1582 if (nal_data[nal_size - 1] == 0x80)
1585 while (nal_size > 0 && *nal_data > 0) {
1587 sei_payload_type += *nal_data;
1590 }
while (nal_size > 0 && *nal_data == 0xFF);
1598 sei_payload_size += *nal_data;
1601 }
while (nal_size > 0 && *nal_data == 0xFF);
1603 if (nal_size < sei_payload_size) {
1608 nal_data += sei_payload_size;
1609 nal_size -= sei_payload_size;
1612 *sei_end = nal_data;
1614 return nal_data - nal_start + 1;
1634 uint8_t* dst_end = dst + dst_size;
1636 int start_at = dst_offset > 2 ? dst_offset - 2 : 0;
1638 for (
i = start_at;
i < dst_offset &&
i < dst_size;
i++) {
1647 for (;
src < src_end;
src++, dst++) {
1649 int insert_ep3_byte = *
src <= 3;
1650 if (insert_ep3_byte) {
1668 wrote_bytes = dst - dst_start;
1671 return -wrote_bytes;
1682 size_t remaining_sei_size =
sei->size;
1683 size_t remaining_dst_size = dst_size;
1688 if (!remaining_dst_size)
1691 while (sei_type && remaining_dst_size != 0) {
1692 int sei_byte = sei_type > 255 ? 255 : sei_type;
1695 sei_type -= sei_byte;
1697 remaining_dst_size--;
1703 while (remaining_sei_size && remaining_dst_size != 0) {
1704 int size_byte = remaining_sei_size > 255 ? 255 : remaining_sei_size;
1707 remaining_sei_size -= size_byte;
1709 remaining_dst_size--;
1712 if (remaining_dst_size < sei->
size)
1715 header_bytes = dst - sei_start;
1723 if (bytes_written < 0)
1726 bytes_written += header_bytes;
1727 return bytes_written;
1751 size_t length_code_size,
1752 CMSampleBufferRef sample_buffer,
1757 size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
1758 size_t remaining_src_size = src_size;
1759 size_t remaining_dst_size = dst_size;
1760 size_t src_offset = 0;
1765 CMBlockBufferRef
block = CMSampleBufferGetDataBuffer(sample_buffer);
1767 if (length_code_size > 4) {
1771 while (remaining_src_size > 0) {
1772 size_t curr_src_len;
1773 size_t curr_dst_len;
1789 src_offset + length_code_size,
1800 for (
i = 0;
i < length_code_size;
i++) {
1802 box_len |= size_buf[
i];
1815 remaining_dst_size--;
1820 remaining_dst_size);
1822 if (wrote_bytes < 0)
1825 remaining_dst_size -= wrote_bytes;
1826 dst_data += wrote_bytes;
1828 if (remaining_dst_size <= 0)
1834 remaining_dst_size--;
1839 curr_src_len = box_len + length_code_size;
1842 if (remaining_src_size < curr_src_len) {
1846 if (remaining_dst_size < curr_dst_len) {
1854 src_offset + length_code_size,
1869 old_sei_length =
find_sei_end(avctx, dst_box, box_len, &new_sei);
1870 if (old_sei_length < 0)
1876 remaining_dst_size - old_sei_length);
1877 if (wrote_bytes < 0)
1880 if (new_sei + wrote_bytes >= dst_data + remaining_dst_size)
1883 new_sei[wrote_bytes++] = 0x80;
1884 extra_bytes = wrote_bytes - (dst_box + box_len - new_sei);
1886 dst_data += extra_bytes;
1887 remaining_dst_size -= extra_bytes;
1892 src_offset += curr_src_len;
1893 dst_data += curr_dst_len;
1895 remaining_src_size -= curr_src_len;
1896 remaining_dst_size -= curr_dst_len;
1917 if ((
sei->size % 255) == 0)
1920 return copied_size +
sei->size / 255 + 1 +
type / 255 + 1;
1925 CMSampleBufferRef sample_buffer,
1934 size_t length_code_size;
1935 size_t header_size = 0;
1937 size_t out_buf_size;
1938 size_t sei_nalu_size = 0;
1940 int64_t time_base_num;
1944 CMVideoFormatDescriptionRef vid_fmt;
1954 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
1972 sei_nalu_size =
sizeof(
start_code) + 1 + msg_size + 1;
1975 in_buf_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
1976 out_buf_size = header_size +
2008 pts = CMSampleBufferGetPresentationTimeStamp(sample_buffer);
2009 dts = CMSampleBufferGetDecodeTimeStamp (sample_buffer);
2011 if (CMTIME_IS_INVALID(dts)) {
2023 pkt->
dts = dts.value / time_base_num - dts_delta;
2041 size_t *contiguous_buf_size)
2044 int av_format =
frame->format;
2045 int av_color_range =
frame->color_range;
2054 "Could not get pixel format for color format '%s' range '%s'.\n",
2064 if (range_guessed) {
2069 "Color range not set for %s. Using MPEG range.\n",
2074 switch (av_format) {
2078 widths [0] = avctx->
width;
2079 heights[0] = avctx->
height;
2082 widths [1] = (avctx->
width + 1) / 2;
2083 heights[1] = (avctx->
height + 1) / 2;
2090 widths [0] = avctx->
width;
2091 heights[0] = avctx->
height;
2094 widths [1] = (avctx->
width + 1) / 2;
2095 heights[1] = (avctx->
height + 1) / 2;
2098 widths [2] = (avctx->
width + 1) / 2;
2099 heights[2] = (avctx->
height + 1) / 2;
2106 widths [0] = avctx->
width;
2107 heights[0] = avctx->
height;
2113 widths[0] = avctx->
width;
2114 heights[0] = avctx->
height;
2115 strides[0] =
frame ?
frame->linesize[0] : (avctx->
width * 2 + 63) & -64;
2117 widths[1] = (avctx->
width + 1) / 2;
2118 heights[1] = (avctx->
height + 1) / 2;
2119 strides[1] =
frame ?
frame->linesize[1] : ((avctx->
width + 1) / 2 + 63) & -64;
2126 "Could not get frame format info for color %d range %d.\n",
2133 *contiguous_buf_size = 0;
2134 for (
i = 0;
i < *plane_count;
i++) {
2135 if (
i < *plane_count - 1 &&
2136 frame->data[
i] + strides[
i] * heights[
i] !=
frame->data[
i + 1]) {
2137 *contiguous_buf_size = 0;
2141 *contiguous_buf_size += strides[
i] * heights[
i];
2150 CVPixelBufferRef cv_img,
2151 const size_t *plane_strides,
2152 const size_t *plane_rows)
2164 status = CVPixelBufferLockBaseAddress(cv_img, 0);
2169 "Error: Could not lock base address of CVPixelBuffer: %d.\n",
2174 if (CVPixelBufferIsPlanar(cv_img)) {
2175 plane_count = CVPixelBufferGetPlaneCount(cv_img);
2177 if (
i == plane_count) {
2178 CVPixelBufferUnlockBaseAddress(cv_img, 0);
2181 "Error: different number of planes in AVFrame and CVPixelBuffer.\n"
2187 dst_addr = (
uint8_t*)CVPixelBufferGetBaseAddressOfPlane(cv_img,
i);
2189 dst_stride = CVPixelBufferGetBytesPerRowOfPlane(cv_img,
i);
2190 src_stride = plane_strides[
i];
2191 rows = plane_rows[
i];
2193 if (dst_stride == src_stride) {
2194 memcpy(dst_addr, src_addr, src_stride * rows);
2196 copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
2198 for (j = 0; j < rows; j++) {
2199 memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
2204 if (
frame->data[1]) {
2205 CVPixelBufferUnlockBaseAddress(cv_img, 0);
2208 "Error: different number of planes in AVFrame and non-planar CVPixelBuffer.\n"
2214 dst_addr = (
uint8_t*)CVPixelBufferGetBaseAddress(cv_img);
2216 dst_stride = CVPixelBufferGetBytesPerRow(cv_img);
2217 src_stride = plane_strides[0];
2218 rows = plane_rows[0];
2220 if (dst_stride == src_stride) {
2221 memcpy(dst_addr, src_addr, src_stride * rows);
2223 copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
2225 for (j = 0; j < rows; j++) {
2226 memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
2231 status = CVPixelBufferUnlockBaseAddress(cv_img, 0);
2242 CVPixelBufferRef *cv_img)
2250 size_t contiguous_buf_size;
2251 CVPixelBufferPoolRef pix_buf_pool;
2257 *cv_img = (CVPixelBufferRef)
frame->data[3];
2264 memset(widths, 0,
sizeof(widths));
2265 memset(heights, 0,
sizeof(heights));
2266 memset(strides, 0,
sizeof(strides));
2276 &contiguous_buf_size
2283 "Error: Cannot convert format %d color_range %d: %d\n",
2292 pix_buf_pool = VTCompressionSessionGetPixelBufferPool(vtctx->
session);
2293 if (!pix_buf_pool) {
2300 vtstatus = VTCompressionSessionPrepareToEncodeFrames(vtctx->
session);
2301 if (vtstatus == kVTInvalidSessionErr) {
2306 pix_buf_pool = VTCompressionSessionGetPixelBufferPool(vtctx->
session);
2308 if (!pix_buf_pool) {
2314 "kVTInvalidSessionErr error.\n");
2317 status = CVPixelBufferPoolCreatePixelBuffer(
NULL,
2338 CFDictionaryRef* dict_out)
2340 CFDictionaryRef dict =
NULL;
2342 const void *keys[] = { kVTEncodeFrameOptionKey_ForceKeyFrame };
2343 const void *vals[] = { kCFBooleanTrue };
2345 dict = CFDictionaryCreate(
NULL, keys, vals, 1,
NULL,
NULL);
2346 if(!dict)
return AVERROR(ENOMEM);
2358 CFDictionaryRef frame_dict;
2359 CVPixelBufferRef cv_img =
NULL;
2373 if (vtctx->
a53_cc && side_data && side_data->
size) {
2388 status = VTCompressionSessionEncodeFrame(
2398 if (frame_dict) CFRelease(frame_dict);
2418 CMSampleBufferRef buf =
NULL;
2457 if (
status)
goto end_nopkt;
2458 if (!buf)
goto end_nopkt;
2466 if (
status)
goto end_nopkt;
2478 CFStringRef profile_level,
2479 CFNumberRef gamma_level,
2480 CFDictionaryRef enc_info,
2481 CFDictionaryRef pixel_buffer_info)
2485 CVPixelBufferPoolRef pool =
NULL;
2486 CVPixelBufferRef pix_buf =
NULL;
2488 CMSampleBufferRef buf =
NULL;
2500 pool = VTCompressionSessionGetPixelBufferPool(vtctx->
session);
2506 status = CVPixelBufferPoolCreatePixelBuffer(
NULL,
2510 if(
status != kCVReturnSuccess){
2527 "Error sending frame for extradata: %d\n",
2572 VTCompressionSessionCompleteFrames(vtctx->
session,
2614 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
2615 #define COMMON_OPTIONS \
2616 { "allow_sw", "Allow software encoding", OFFSET(allow_sw), AV_OPT_TYPE_BOOL, \
2617 { .i64 = 0 }, 0, 1, VE }, \
2618 { "require_sw", "Require software encoding", OFFSET(require_sw), AV_OPT_TYPE_BOOL, \
2619 { .i64 = 0 }, 0, 1, VE }, \
2620 { "realtime", "Hint that encoding should happen in real-time if not faster (e.g. capturing from camera).", \
2621 OFFSET(realtime), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
2622 { "frames_before", "Other frames will come before the frames in this session. This helps smooth concatenation issues.", \
2623 OFFSET(frames_before), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
2624 { "frames_after", "Other frames will come after the frames in this session. This helps smooth concatenation issues.", \
2625 OFFSET(frames_after), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
2627 #define OFFSET(x) offsetof(VTEncContext, x)
2636 {
"1.3",
"Level 1.3, only available with Baseline Profile", 0,
AV_OPT_TYPE_CONST, { .i64 = 13 }, INT_MIN, INT_MAX,
VE,
"level" },
2637 {
"3.0",
"Level 3.0", 0,
AV_OPT_TYPE_CONST, { .i64 = 30 }, INT_MIN, INT_MAX,
VE,
"level" },
2638 {
"3.1",
"Level 3.1", 0,
AV_OPT_TYPE_CONST, { .i64 = 31 }, INT_MIN, INT_MAX,
VE,
"level" },
2639 {
"3.2",
"Level 3.2", 0,
AV_OPT_TYPE_CONST, { .i64 = 32 }, INT_MIN, INT_MAX,
VE,
"level" },
2640 {
"4.0",
"Level 4.0", 0,
AV_OPT_TYPE_CONST, { .i64 = 40 }, INT_MIN, INT_MAX,
VE,
"level" },
2641 {
"4.1",
"Level 4.1", 0,
AV_OPT_TYPE_CONST, { .i64 = 41 }, INT_MIN, INT_MAX,
VE,
"level" },
2642 {
"4.2",
"Level 4.2", 0,
AV_OPT_TYPE_CONST, { .i64 = 42 }, INT_MIN, INT_MAX,
VE,
"level" },
2643 {
"5.0",
"Level 5.0", 0,
AV_OPT_TYPE_CONST, { .i64 = 50 }, INT_MIN, INT_MAX,
VE,
"level" },
2644 {
"5.1",
"Level 5.1", 0,
AV_OPT_TYPE_CONST, { .i64 = 51 }, INT_MIN, INT_MAX,
VE,
"level" },
2645 {
"5.2",
"Level 5.2", 0,
AV_OPT_TYPE_CONST, { .i64 = 52 }, INT_MIN, INT_MAX,
VE,
"level" },
2667 .
name =
"h264_videotoolbox",
2687 {
"alpha_quality",
"Compression quality for the alpha channel",
OFFSET(alpha_quality),
AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0.0, 1.0,
VE },
2701 .
name =
"hevc_videotoolbox",
2714 .wrapper_name =
"videotoolbox",