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
53 size_t parameterSetIndex,
54 const uint8_t **parameterSetPointerOut,
55 size_t *parameterSetSizeOut,
56 size_t *parameterSetCountOut,
57 int *NALUnitHeaderLengthOut);
103 #define GET_SYM(symbol, defaultVal) \
105 CFStringRef* handle = (CFStringRef*)dlsym(RTLD_DEFAULT, #symbol); \
107 compat_keys.symbol = CFSTR(defaultVal); \
109 compat_keys.symbol = *handle; \
115 compat_keys.CMVideoFormatDescriptionGetHEVCParameterSetAtIndex =
118 "CMVideoFormatDescriptionGetHEVCParameterSetAtIndex"
156 "TargetQualityForAlpha");
159 "EnableHardwareAcceleratedVideoEncoder");
161 "RequireHardwareAcceleratedVideoEncoder");
244 CFStringRef profile_level,
245 CFNumberRef gamma_level,
246 CFDictionaryRef enc_info,
247 CFDictionaryRef pixel_buffer_info);
274 CFRelease(
info->cm_buffer);
324 *buf =
info->cm_buffer;
327 }
else if (
info->sei) {
365 CMSampleBufferRef sample_buffer,
372 size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
373 CMBlockBufferRef
block = CMSampleBufferGetDataBuffer(sample_buffer);
375 if (length_code_size > 4)
378 while (
offset < src_size) {
388 for (
i = 0;
i < length_code_size;
i++) {
390 box_len |= size_buf[
i];
393 curr_src_len = box_len + length_code_size;
405 double alpha_quality)
428 CMVideoFormatDescriptionRef vid_fmt,
432 size_t total_size = 0;
434 int is_count_bad = 0;
449 for (
i = 0;
i < ps_count || is_count_bad;
i++) {
463 if (
i > 0 && is_count_bad)
status = 0;
482 CMVideoFormatDescriptionRef vid_fmt,
488 int is_count_bad = 0;
506 for (
i = 0;
i < ps_count || is_count_bad;
i++) {
518 if (
i > 0 && is_count_bad)
status = 0;
524 if (dst_size < next_offset) {
532 memcpy(dst +
offset, ps, ps_size);
546 CMVideoFormatDescriptionRef vid_fmt;
550 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
580 void *sourceFrameCtx,
582 VTEncodeInfoFlags
flags,
583 CMSampleBufferRef sample_buffer)
599 if (!sample_buffer) {
616 CMSampleBufferRef sample_buffer,
620 CMVideoFormatDescriptionRef vid_fmt;
624 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
652 CFStringRef *profile_level_val)
662 *profile_level_val =
NULL;
669 switch (vtctx->
level) {
670 case 0: *profile_level_val =
671 compat_keys.kVTProfileLevel_H264_Baseline_AutoLevel;
break;
672 case 13: *profile_level_val = kVTProfileLevel_H264_Baseline_1_3;
break;
673 case 30: *profile_level_val = kVTProfileLevel_H264_Baseline_3_0;
break;
674 case 31: *profile_level_val = kVTProfileLevel_H264_Baseline_3_1;
break;
675 case 32: *profile_level_val = kVTProfileLevel_H264_Baseline_3_2;
break;
676 case 40: *profile_level_val =
677 compat_keys.kVTProfileLevel_H264_Baseline_4_0;
break;
678 case 41: *profile_level_val = kVTProfileLevel_H264_Baseline_4_1;
break;
679 case 42: *profile_level_val =
680 compat_keys.kVTProfileLevel_H264_Baseline_4_2;
break;
681 case 50: *profile_level_val =
682 compat_keys.kVTProfileLevel_H264_Baseline_5_0;
break;
683 case 51: *profile_level_val =
684 compat_keys.kVTProfileLevel_H264_Baseline_5_1;
break;
685 case 52: *profile_level_val =
686 compat_keys.kVTProfileLevel_H264_Baseline_5_2;
break;
691 switch (vtctx->
level) {
692 case 0: *profile_level_val =
693 compat_keys.kVTProfileLevel_H264_Main_AutoLevel;
break;
694 case 30: *profile_level_val = kVTProfileLevel_H264_Main_3_0;
break;
695 case 31: *profile_level_val = kVTProfileLevel_H264_Main_3_1;
break;
696 case 32: *profile_level_val = kVTProfileLevel_H264_Main_3_2;
break;
697 case 40: *profile_level_val = kVTProfileLevel_H264_Main_4_0;
break;
698 case 41: *profile_level_val = kVTProfileLevel_H264_Main_4_1;
break;
699 case 42: *profile_level_val =
701 case 50: *profile_level_val = kVTProfileLevel_H264_Main_5_0;
break;
702 case 51: *profile_level_val =
704 case 52: *profile_level_val =
710 switch (vtctx->
level) {
711 case 0: *profile_level_val =
712 compat_keys.kVTProfileLevel_H264_High_AutoLevel;
break;
713 case 30: *profile_level_val =
715 case 31: *profile_level_val =
717 case 32: *profile_level_val =
719 case 40: *profile_level_val =
721 case 41: *profile_level_val =
723 case 42: *profile_level_val =
725 case 50: *profile_level_val = kVTProfileLevel_H264_High_5_0;
break;
726 case 51: *profile_level_val =
728 case 52: *profile_level_val =
733 switch (vtctx->
level) {
734 case 0: *profile_level_val =
735 compat_keys.kVTProfileLevel_H264_Extended_AutoLevel;
break;
736 case 50: *profile_level_val =
737 compat_keys.kVTProfileLevel_H264_Extended_5_0;
break;
742 if (!*profile_level_val) {
757 CFStringRef *profile_level_val)
762 *profile_level_val =
NULL;
777 if (!*profile_level_val) {
788 int* av_pixel_format,
797 kCVPixelFormatType_420YpCbCr8BiPlanarFullRange :
798 kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
801 kCVPixelFormatType_420YpCbCr8PlanarFullRange :
802 kCVPixelFormatType_420YpCbCr8Planar;
804 *av_pixel_format = kCVPixelFormatType_32BGRA;
820 CFDictionarySetValue(dict,
821 kCVImageBufferColorPrimariesKey,
826 CFDictionarySetValue(dict,
827 kCVImageBufferTransferFunctionKey,
832 CFDictionarySetValue(dict,
833 kCVImageBufferYCbCrMatrixKey,
839 CFMutableDictionaryRef* dict)
841 CFNumberRef cv_color_format_num =
NULL;
842 CFNumberRef width_num =
NULL;
843 CFNumberRef height_num =
NULL;
844 CFMutableDictionaryRef pixel_buffer_info =
NULL;
853 pixel_buffer_info = CFDictionaryCreateMutable(
856 &kCFCopyStringDictionaryKeyCallBacks,
857 &kCFTypeDictionaryValueCallBacks);
859 if (!pixel_buffer_info)
goto pbinfo_nomem;
861 cv_color_format_num = CFNumberCreate(kCFAllocatorDefault,
864 if (!cv_color_format_num)
goto pbinfo_nomem;
866 CFDictionarySetValue(pixel_buffer_info,
867 kCVPixelBufferPixelFormatTypeKey,
868 cv_color_format_num);
871 width_num = CFNumberCreate(kCFAllocatorDefault,
874 if (!width_num)
return AVERROR(ENOMEM);
876 CFDictionarySetValue(pixel_buffer_info,
877 kCVPixelBufferWidthKey,
881 height_num = CFNumberCreate(kCFAllocatorDefault,
884 if (!height_num)
goto pbinfo_nomem;
886 CFDictionarySetValue(pixel_buffer_info,
887 kCVPixelBufferHeightKey,
893 *dict = pixel_buffer_info;
900 if (pixel_buffer_info) CFRelease(pixel_buffer_info);
906 CFStringRef *primaries)
915 *primaries = kCVImageBufferColorPrimaries_EBU_3213;
919 *primaries = kCVImageBufferColorPrimaries_SMPTE_C;
923 *primaries = kCVImageBufferColorPrimaries_ITU_R_709_2;
927 *primaries =
compat_keys.kCVImageBufferColorPrimaries_ITU_R_2020;
940 CFStringRef *transfer_fnc,
941 CFNumberRef *gamma_level)
949 *transfer_fnc =
NULL;
953 *transfer_fnc = kCVImageBufferTransferFunction_ITU_R_709_2;
957 *transfer_fnc = kCVImageBufferTransferFunction_SMPTE_240M_1995;
960 #if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_SMPTE_ST_2084_PQ
962 *transfer_fnc = kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ;
965 #if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_LINEAR
967 *transfer_fnc = kCVImageBufferTransferFunction_Linear;
970 #if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_ITU_R_2100_HLG
972 *transfer_fnc = kCVImageBufferTransferFunction_ITU_R_2100_HLG;
978 *transfer_fnc = kCVImageBufferTransferFunction_UseGamma;
979 *gamma_level = CFNumberCreate(
NULL, kCFNumberFloat32Type, &gamma);
984 *transfer_fnc = kCVImageBufferTransferFunction_UseGamma;
985 *gamma_level = CFNumberCreate(
NULL, kCFNumberFloat32Type, &gamma);
990 *transfer_fnc =
compat_keys.kCVImageBufferTransferFunction_ITU_R_2020;
994 *transfer_fnc =
NULL;
1005 *matrix = kCVImageBufferYCbCrMatrix_ITU_R_709_2;
1014 *matrix = kCVImageBufferYCbCrMatrix_ITU_R_601_4;
1018 *matrix = kCVImageBufferYCbCrMatrix_SMPTE_240M_1995;
1022 *matrix =
compat_keys.kCVImageBufferYCbCrMatrix_ITU_R_2020;
1036 return TARGET_OS_OSX && TARGET_CPU_ARM64;
1041 CFStringRef profile_level,
1042 CFNumberRef gamma_level,
1043 CFDictionaryRef enc_info,
1044 CFDictionaryRef pixel_buffer_info,
1045 VTCompressionSessionRef *session)
1051 CFNumberRef bit_rate_num;
1052 CFNumberRef quality_num;
1053 CFNumberRef bytes_per_second;
1054 CFNumberRef one_second;
1055 CFArrayRef data_rate_limits;
1056 int64_t bytes_per_second_value = 0;
1057 int64_t one_second_value = 0;
1060 int status = VTCompressionSessionCreate(kCFAllocatorDefault,
1066 kCFAllocatorDefault,
1074 #if !TARGET_OS_IPHONE
1076 av_log(avctx,
AV_LOG_ERROR,
"Try -allow_sw 1. The hardware encoder may be busy, or not supported.\n");
1084 av_log(avctx,
AV_LOG_ERROR,
"Error: -q:v qscale not available for encoder. Use -b:v bitrate instead.\n");
1090 quality_num = CFNumberCreate(kCFAllocatorDefault,
1091 kCFNumberFloat32Type,
1093 if (!quality_num)
return AVERROR(ENOMEM);
1096 kVTCompressionPropertyKey_Quality,
1098 CFRelease(quality_num);
1100 bit_rate_num = CFNumberCreate(kCFAllocatorDefault,
1101 kCFNumberSInt32Type,
1103 if (!bit_rate_num)
return AVERROR(ENOMEM);
1106 kVTCompressionPropertyKey_AverageBitRate,
1108 CFRelease(bit_rate_num);
1118 bytes_per_second_value = max_rate >> 3;
1119 bytes_per_second = CFNumberCreate(kCFAllocatorDefault,
1120 kCFNumberSInt64Type,
1121 &bytes_per_second_value);
1122 if (!bytes_per_second) {
1125 one_second_value = 1;
1126 one_second = CFNumberCreate(kCFAllocatorDefault,
1127 kCFNumberSInt64Type,
1130 CFRelease(bytes_per_second);
1133 nums[0] = (
void *)bytes_per_second;
1134 nums[1] = (
void *)one_second;
1135 data_rate_limits = CFArrayCreate(kCFAllocatorDefault,
1136 (
const void **)nums,
1138 &kCFTypeArrayCallBacks);
1140 if (!data_rate_limits) {
1141 CFRelease(bytes_per_second);
1142 CFRelease(one_second);
1146 kVTCompressionPropertyKey_DataRateLimits,
1149 CFRelease(bytes_per_second);
1150 CFRelease(one_second);
1151 CFRelease(data_rate_limits);
1161 CFNumberRef alpha_quality_num = CFNumberCreate(kCFAllocatorDefault,
1162 kCFNumberDoubleType,
1164 if (!alpha_quality_num)
return AVERROR(ENOMEM);
1167 compat_keys.kVTCompressionPropertyKey_TargetQualityForAlpha,
1169 CFRelease(alpha_quality_num);
1173 if (profile_level) {
1175 kVTCompressionPropertyKey_ProfileLevel,
1178 av_log(avctx,
AV_LOG_ERROR,
"Error setting profile/level property: %d. Output will be encoded using a supported profile/level combination.\n",
status);
1183 CFNumberRef interval = CFNumberCreate(kCFAllocatorDefault,
1191 kVTCompressionPropertyKey_MaxKeyFrameInterval,
1193 CFRelease(interval);
1203 kVTCompressionPropertyKey_MoreFramesBeforeStart,
1206 if (
status == kVTPropertyNotSupportedErr) {
1207 av_log(avctx,
AV_LOG_WARNING,
"frames_before property is not supported on this device. Ignoring.\n");
1215 kVTCompressionPropertyKey_MoreFramesAfterEnd,
1218 if (
status == kVTPropertyNotSupportedErr) {
1219 av_log(avctx,
AV_LOG_WARNING,
"frames_after property is not supported on this device. Ignoring.\n");
1228 CFMutableDictionaryRef par;
1235 num = CFNumberCreate(kCFAllocatorDefault,
1239 den = CFNumberCreate(kCFAllocatorDefault,
1245 par = CFDictionaryCreateMutable(kCFAllocatorDefault,
1247 &kCFCopyStringDictionaryKeyCallBacks,
1248 &kCFTypeDictionaryValueCallBacks);
1250 if (!par || !num || !den) {
1251 if (par) CFRelease(par);
1252 if (num) CFRelease(num);
1253 if (den) CFRelease(den);
1258 CFDictionarySetValue(
1260 kCMFormatDescriptionKey_PixelAspectRatioHorizontalSpacing,
1263 CFDictionarySetValue(
1265 kCMFormatDescriptionKey_PixelAspectRatioVerticalSpacing,
1269 kVTCompressionPropertyKey_PixelAspectRatio,
1279 "Error setting pixel aspect ratio to %d:%d: %d.\n",
1291 kVTCompressionPropertyKey_TransferFunction,
1302 kVTCompressionPropertyKey_YCbCrMatrix,
1313 kVTCompressionPropertyKey_ColorPrimaries,
1323 kCVImageBufferGammaLevelKey,
1333 kVTCompressionPropertyKey_AllowFrameReordering,
1348 compat_keys.kVTCompressionPropertyKey_H264EntropyMode,
1366 status = VTCompressionSessionPrepareToEncodeFrames(vtctx->
session);
1377 CFMutableDictionaryRef enc_info;
1378 CFMutableDictionaryRef pixel_buffer_info;
1381 CFStringRef profile_level;
1382 CFNumberRef gamma_level =
NULL;
1399 av_log(avctx,
AV_LOG_WARNING,
"Cannot use B-frames with baseline profile. Output will not contain B-frames.\n");
1404 av_log(avctx,
AV_LOG_WARNING,
"CABAC entropy requires 'main' or 'high' profile, but baseline was requested. Encode will not use CABAC entropy.\n");
1417 enc_info = CFDictionaryCreateMutable(
1418 kCFAllocatorDefault,
1420 &kCFCopyStringDictionaryKeyCallBacks,
1421 &kCFTypeDictionaryValueCallBacks
1424 if (!enc_info)
return AVERROR(ENOMEM);
1426 #if !TARGET_OS_IPHONE
1428 CFDictionarySetValue(enc_info,
1429 compat_keys.kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
1432 CFDictionarySetValue(enc_info,
1433 compat_keys.kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder,
1436 CFDictionarySetValue(enc_info,
1437 compat_keys.kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
1447 pixel_buffer_info =
NULL;
1478 CFRelease(gamma_level);
1480 if (pixel_buffer_info)
1481 CFRelease(pixel_buffer_info);
1483 CFRelease(enc_info);
1491 CFBooleanRef has_b_frames_cfbool;
1504 kVTCompressionPropertyKey_AllowFrameReordering,
1505 kCFAllocatorDefault,
1506 &has_b_frames_cfbool);
1508 if (!
status && has_b_frames_cfbool) {
1512 CFRelease(has_b_frames_cfbool);
1521 CFArrayRef attachments;
1522 CFDictionaryRef attachment;
1523 CFBooleanRef not_sync;
1526 attachments = CMSampleBufferGetSampleAttachmentsArray(
buffer,
false);
1527 len = !attachments ? 0 : CFArrayGetCount(attachments);
1530 *is_key_frame =
true;
1534 attachment = CFArrayGetValueAtIndex(attachments, 0);
1536 if (CFDictionaryGetValueIfPresent(attachment,
1537 kCMSampleAttachmentKey_NotSync,
1538 (
const void **)¬_sync))
1540 *is_key_frame = !CFBooleanGetValue(not_sync);
1542 *is_key_frame =
true;
1563 size_t sei_payload_size = 0;
1564 int sei_payload_type = 0;
1566 uint8_t *nal_start = nal_data;
1571 nal_type = *nal_data & 0x1F;
1578 if (nal_data[nal_size - 1] == 0x80)
1581 while (nal_size > 0 && *nal_data > 0) {
1583 sei_payload_type += *nal_data;
1586 }
while (nal_size > 0 && *nal_data == 0xFF);
1594 sei_payload_size += *nal_data;
1597 }
while (nal_size > 0 && *nal_data == 0xFF);
1599 if (nal_size < sei_payload_size) {
1604 nal_data += sei_payload_size;
1605 nal_size -= sei_payload_size;
1608 *sei_end = nal_data;
1610 return nal_data - nal_start + 1;
1630 uint8_t* dst_end = dst + dst_size;
1632 int start_at = dst_offset > 2 ? dst_offset - 2 : 0;
1634 for (
i = start_at;
i < dst_offset &&
i < dst_size;
i++) {
1643 for (;
src < src_end;
src++, dst++) {
1645 int insert_ep3_byte = *
src <= 3;
1646 if (insert_ep3_byte) {
1664 wrote_bytes = dst - dst_start;
1667 return -wrote_bytes;
1678 size_t remaining_sei_size =
sei->size;
1679 size_t remaining_dst_size = dst_size;
1684 if (!remaining_dst_size)
1687 while (sei_type && remaining_dst_size != 0) {
1688 int sei_byte = sei_type > 255 ? 255 : sei_type;
1691 sei_type -= sei_byte;
1693 remaining_dst_size--;
1699 while (remaining_sei_size && remaining_dst_size != 0) {
1700 int size_byte = remaining_sei_size > 255 ? 255 : remaining_sei_size;
1703 remaining_sei_size -= size_byte;
1705 remaining_dst_size--;
1708 if (remaining_dst_size < sei->
size)
1711 header_bytes = dst - sei_start;
1719 if (bytes_written < 0)
1722 bytes_written += header_bytes;
1723 return bytes_written;
1747 size_t length_code_size,
1748 CMSampleBufferRef sample_buffer,
1753 size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
1754 size_t remaining_src_size = src_size;
1755 size_t remaining_dst_size = dst_size;
1756 size_t src_offset = 0;
1761 CMBlockBufferRef
block = CMSampleBufferGetDataBuffer(sample_buffer);
1763 if (length_code_size > 4) {
1767 while (remaining_src_size > 0) {
1768 size_t curr_src_len;
1769 size_t curr_dst_len;
1785 src_offset + length_code_size,
1796 for (
i = 0;
i < length_code_size;
i++) {
1798 box_len |= size_buf[
i];
1811 remaining_dst_size--;
1816 remaining_dst_size);
1818 if (wrote_bytes < 0)
1821 remaining_dst_size -= wrote_bytes;
1822 dst_data += wrote_bytes;
1824 if (remaining_dst_size <= 0)
1830 remaining_dst_size--;
1835 curr_src_len = box_len + length_code_size;
1838 if (remaining_src_size < curr_src_len) {
1842 if (remaining_dst_size < curr_dst_len) {
1850 src_offset + length_code_size,
1865 old_sei_length =
find_sei_end(avctx, dst_box, box_len, &new_sei);
1866 if (old_sei_length < 0)
1872 remaining_dst_size - old_sei_length);
1873 if (wrote_bytes < 0)
1876 if (new_sei + wrote_bytes >= dst_data + remaining_dst_size)
1879 new_sei[wrote_bytes++] = 0x80;
1880 extra_bytes = wrote_bytes - (dst_box + box_len - new_sei);
1882 dst_data += extra_bytes;
1883 remaining_dst_size -= extra_bytes;
1888 src_offset += curr_src_len;
1889 dst_data += curr_dst_len;
1891 remaining_src_size -= curr_src_len;
1892 remaining_dst_size -= curr_dst_len;
1913 if ((
sei->size % 255) == 0)
1916 return copied_size +
sei->size / 255 + 1 +
type / 255 + 1;
1921 CMSampleBufferRef sample_buffer,
1930 size_t length_code_size;
1931 size_t header_size = 0;
1933 size_t out_buf_size;
1934 size_t sei_nalu_size = 0;
1936 int64_t time_base_num;
1940 CMVideoFormatDescriptionRef vid_fmt;
1950 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
1968 sei_nalu_size =
sizeof(
start_code) + 1 + msg_size + 1;
1971 in_buf_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
1972 out_buf_size = header_size +
2004 pts = CMSampleBufferGetPresentationTimeStamp(sample_buffer);
2005 dts = CMSampleBufferGetDecodeTimeStamp (sample_buffer);
2007 if (CMTIME_IS_INVALID(dts)) {
2019 pkt->
dts = dts.value / time_base_num - dts_delta;
2037 size_t *contiguous_buf_size)
2040 int av_format =
frame->format;
2041 int av_color_range =
frame->color_range;
2050 "Could not get pixel format for color format '%s' range '%s'.\n",
2060 if (range_guessed) {
2065 "Color range not set for %s. Using MPEG range.\n",
2070 switch (av_format) {
2074 widths [0] = avctx->
width;
2075 heights[0] = avctx->
height;
2078 widths [1] = (avctx->
width + 1) / 2;
2079 heights[1] = (avctx->
height + 1) / 2;
2086 widths [0] = avctx->
width;
2087 heights[0] = avctx->
height;
2090 widths [1] = (avctx->
width + 1) / 2;
2091 heights[1] = (avctx->
height + 1) / 2;
2094 widths [2] = (avctx->
width + 1) / 2;
2095 heights[2] = (avctx->
height + 1) / 2;
2102 widths [0] = avctx->
width;
2103 heights[0] = avctx->
height;
2109 widths[0] = avctx->
width;
2110 heights[0] = avctx->
height;
2111 strides[0] =
frame ?
frame->linesize[0] : (avctx->
width * 2 + 63) & -64;
2113 widths[1] = (avctx->
width + 1) / 2;
2114 heights[1] = (avctx->
height + 1) / 2;
2115 strides[1] =
frame ?
frame->linesize[1] : ((avctx->
width + 1) / 2 + 63) & -64;
2122 "Could not get frame format info for color %d range %d.\n",
2129 *contiguous_buf_size = 0;
2130 for (
i = 0;
i < *plane_count;
i++) {
2131 if (
i < *plane_count - 1 &&
2132 frame->data[
i] + strides[
i] * heights[
i] !=
frame->data[
i + 1]) {
2133 *contiguous_buf_size = 0;
2137 *contiguous_buf_size += strides[
i] * heights[
i];
2146 CVPixelBufferRef cv_img,
2147 const size_t *plane_strides,
2148 const size_t *plane_rows)
2160 status = CVPixelBufferLockBaseAddress(cv_img, 0);
2165 "Error: Could not lock base address of CVPixelBuffer: %d.\n",
2170 if (CVPixelBufferIsPlanar(cv_img)) {
2171 plane_count = CVPixelBufferGetPlaneCount(cv_img);
2173 if (
i == plane_count) {
2174 CVPixelBufferUnlockBaseAddress(cv_img, 0);
2177 "Error: different number of planes in AVFrame and CVPixelBuffer.\n"
2183 dst_addr = (
uint8_t*)CVPixelBufferGetBaseAddressOfPlane(cv_img,
i);
2185 dst_stride = CVPixelBufferGetBytesPerRowOfPlane(cv_img,
i);
2186 src_stride = plane_strides[
i];
2187 rows = plane_rows[
i];
2189 if (dst_stride == src_stride) {
2190 memcpy(dst_addr, src_addr, src_stride * rows);
2192 copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
2194 for (j = 0; j < rows; j++) {
2195 memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
2200 if (
frame->data[1]) {
2201 CVPixelBufferUnlockBaseAddress(cv_img, 0);
2204 "Error: different number of planes in AVFrame and non-planar CVPixelBuffer.\n"
2210 dst_addr = (
uint8_t*)CVPixelBufferGetBaseAddress(cv_img);
2212 dst_stride = CVPixelBufferGetBytesPerRow(cv_img);
2213 src_stride = plane_strides[0];
2214 rows = plane_rows[0];
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 status = CVPixelBufferUnlockBaseAddress(cv_img, 0);
2238 CVPixelBufferRef *cv_img)
2246 size_t contiguous_buf_size;
2247 CVPixelBufferPoolRef pix_buf_pool;
2253 *cv_img = (CVPixelBufferRef)
frame->data[3];
2260 memset(widths, 0,
sizeof(widths));
2261 memset(heights, 0,
sizeof(heights));
2262 memset(strides, 0,
sizeof(strides));
2272 &contiguous_buf_size
2279 "Error: Cannot convert format %d color_range %d: %d\n",
2288 pix_buf_pool = VTCompressionSessionGetPixelBufferPool(vtctx->
session);
2289 if (!pix_buf_pool) {
2296 vtstatus = VTCompressionSessionPrepareToEncodeFrames(vtctx->
session);
2297 if (vtstatus == kVTInvalidSessionErr) {
2302 pix_buf_pool = VTCompressionSessionGetPixelBufferPool(vtctx->
session);
2304 if (!pix_buf_pool) {
2310 "kVTInvalidSessionErr error.\n");
2313 status = CVPixelBufferPoolCreatePixelBuffer(
NULL,
2334 CFDictionaryRef* dict_out)
2336 CFDictionaryRef dict =
NULL;
2338 const void *keys[] = { kVTEncodeFrameOptionKey_ForceKeyFrame };
2339 const void *vals[] = { kCFBooleanTrue };
2341 dict = CFDictionaryCreate(
NULL, keys, vals, 1,
NULL,
NULL);
2342 if(!dict)
return AVERROR(ENOMEM);
2354 CFDictionaryRef frame_dict;
2355 CVPixelBufferRef cv_img =
NULL;
2369 if (vtctx->
a53_cc && side_data && side_data->
size) {
2384 status = VTCompressionSessionEncodeFrame(
2394 if (frame_dict) CFRelease(frame_dict);
2414 CMSampleBufferRef buf =
NULL;
2453 if (
status)
goto end_nopkt;
2454 if (!buf)
goto end_nopkt;
2462 if (
status)
goto end_nopkt;
2474 CFStringRef profile_level,
2475 CFNumberRef gamma_level,
2476 CFDictionaryRef enc_info,
2477 CFDictionaryRef pixel_buffer_info)
2481 CVPixelBufferPoolRef pool =
NULL;
2482 CVPixelBufferRef pix_buf =
NULL;
2484 CMSampleBufferRef buf =
NULL;
2496 pool = VTCompressionSessionGetPixelBufferPool(vtctx->
session);
2502 status = CVPixelBufferPoolCreatePixelBuffer(
NULL,
2506 if(
status != kCVReturnSuccess){
2523 "Error sending frame for extradata: %d\n",
2568 VTCompressionSessionCompleteFrames(vtctx->
session,
2610 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
2611 #define COMMON_OPTIONS \
2612 { "allow_sw", "Allow software encoding", OFFSET(allow_sw), AV_OPT_TYPE_BOOL, \
2613 { .i64 = 0 }, 0, 1, VE }, \
2614 { "require_sw", "Require software encoding", OFFSET(require_sw), AV_OPT_TYPE_BOOL, \
2615 { .i64 = 0 }, 0, 1, VE }, \
2616 { "realtime", "Hint that encoding should happen in real-time if not faster (e.g. capturing from camera).", \
2617 OFFSET(realtime), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
2618 { "frames_before", "Other frames will come before the frames in this session. This helps smooth concatenation issues.", \
2619 OFFSET(frames_before), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
2620 { "frames_after", "Other frames will come after the frames in this session. This helps smooth concatenation issues.", \
2621 OFFSET(frames_after), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
2623 #define OFFSET(x) offsetof(VTEncContext, x)
2632 {
"1.3",
"Level 1.3, only available with Baseline Profile", 0,
AV_OPT_TYPE_CONST, { .i64 = 13 }, INT_MIN, INT_MAX,
VE,
"level" },
2633 {
"3.0",
"Level 3.0", 0,
AV_OPT_TYPE_CONST, { .i64 = 30 }, INT_MIN, INT_MAX,
VE,
"level" },
2634 {
"3.1",
"Level 3.1", 0,
AV_OPT_TYPE_CONST, { .i64 = 31 }, INT_MIN, INT_MAX,
VE,
"level" },
2635 {
"3.2",
"Level 3.2", 0,
AV_OPT_TYPE_CONST, { .i64 = 32 }, INT_MIN, INT_MAX,
VE,
"level" },
2636 {
"4.0",
"Level 4.0", 0,
AV_OPT_TYPE_CONST, { .i64 = 40 }, INT_MIN, INT_MAX,
VE,
"level" },
2637 {
"4.1",
"Level 4.1", 0,
AV_OPT_TYPE_CONST, { .i64 = 41 }, INT_MIN, INT_MAX,
VE,
"level" },
2638 {
"4.2",
"Level 4.2", 0,
AV_OPT_TYPE_CONST, { .i64 = 42 }, INT_MIN, INT_MAX,
VE,
"level" },
2639 {
"5.0",
"Level 5.0", 0,
AV_OPT_TYPE_CONST, { .i64 = 50 }, INT_MIN, INT_MAX,
VE,
"level" },
2640 {
"5.1",
"Level 5.1", 0,
AV_OPT_TYPE_CONST, { .i64 = 51 }, INT_MIN, INT_MAX,
VE,
"level" },
2641 {
"5.2",
"Level 5.2", 0,
AV_OPT_TYPE_CONST, { .i64 = 52 }, INT_MIN, INT_MAX,
VE,
"level" },
2663 .
name =
"h264_videotoolbox",
2683 {
"alpha_quality",
"Compression quality for the alpha channel",
OFFSET(alpha_quality),
AV_OPT_TYPE_DOUBLE, { .dbl = 0.0 }, 0.0, 1.0,
VE },
2697 .
name =
"hevc_videotoolbox",
2710 .wrapper_name =
"videotoolbox",