21 #include <VideoToolbox/VideoToolbox.h>
22 #include <CoreVideo/CoreVideo.h>
23 #include <CoreMedia/CoreMedia.h>
24 #include <TargetConditionals.h>
25 #include <Availability.h>
38 #if !HAVE_KCMVIDEOCODECTYPE_HEVC
42 #if !HAVE_KCVPIXELFORMATTYPE_420YPCBCR10BIPLANARVIDEORANGE
48 size_t parameterSetIndex,
49 const uint8_t **parameterSetPointerOut,
50 size_t *parameterSetSizeOut,
51 size_t *parameterSetCountOut,
52 int *NALUnitHeaderLengthOut);
97 #define GET_SYM(symbol, defaultVal) \
99 CFStringRef* handle = (CFStringRef*)dlsym(RTLD_DEFAULT, #symbol); \
101 compat_keys.symbol = CFSTR(defaultVal); \
103 compat_keys.symbol = *handle; \
109 compat_keys.CMVideoFormatDescriptionGetHEVCParameterSetAtIndex =
112 "CMVideoFormatDescriptionGetHEVCParameterSetAtIndex"
151 "EnableHardwareAcceleratedVideoEncoder");
153 "RequireHardwareAcceleratedVideoEncoder");
233 CFStringRef profile_level,
234 CFNumberRef gamma_level,
235 CFDictionaryRef enc_info,
236 CFDictionaryRef pixel_buffer_info);
263 CFRelease(
info->cm_buffer);
312 *buf =
info->cm_buffer;
315 }
else if (
info->sei) {
354 CMSampleBufferRef sample_buffer,
361 size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
362 CMBlockBufferRef
block = CMSampleBufferGetDataBuffer(sample_buffer);
364 if (length_code_size > 4)
367 while (
offset < src_size) {
377 for (
i = 0;
i < length_code_size;
i++) {
379 box_len |= size_buf[
i];
382 curr_src_len = box_len + length_code_size;
411 CMVideoFormatDescriptionRef vid_fmt,
415 size_t total_size = 0;
417 int is_count_bad = 0;
432 for (
i = 0;
i < ps_count || is_count_bad;
i++) {
446 if (
i > 0 && is_count_bad)
status = 0;
465 CMVideoFormatDescriptionRef vid_fmt,
471 int is_count_bad = 0;
489 for (
i = 0;
i < ps_count || is_count_bad;
i++) {
501 if (
i > 0 && is_count_bad)
status = 0;
507 if (dst_size < next_offset) {
515 memcpy(dst +
offset, ps, ps_size);
529 CMVideoFormatDescriptionRef vid_fmt;
533 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
563 void *sourceFrameCtx,
565 VTEncodeInfoFlags
flags,
566 CMSampleBufferRef sample_buffer)
573 if(sample_buffer) CFRelease(sample_buffer);
583 if (!sample_buffer) {
600 CMSampleBufferRef sample_buffer,
604 CMVideoFormatDescriptionRef vid_fmt;
608 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
636 CFStringRef *profile_level_val)
646 *profile_level_val =
NULL;
653 switch (vtctx->
level) {
654 case 0: *profile_level_val =
655 compat_keys.kVTProfileLevel_H264_Baseline_AutoLevel;
break;
656 case 13: *profile_level_val = kVTProfileLevel_H264_Baseline_1_3;
break;
657 case 30: *profile_level_val = kVTProfileLevel_H264_Baseline_3_0;
break;
658 case 31: *profile_level_val = kVTProfileLevel_H264_Baseline_3_1;
break;
659 case 32: *profile_level_val = kVTProfileLevel_H264_Baseline_3_2;
break;
660 case 40: *profile_level_val =
661 compat_keys.kVTProfileLevel_H264_Baseline_4_0;
break;
662 case 41: *profile_level_val = kVTProfileLevel_H264_Baseline_4_1;
break;
663 case 42: *profile_level_val =
664 compat_keys.kVTProfileLevel_H264_Baseline_4_2;
break;
665 case 50: *profile_level_val =
666 compat_keys.kVTProfileLevel_H264_Baseline_5_0;
break;
667 case 51: *profile_level_val =
668 compat_keys.kVTProfileLevel_H264_Baseline_5_1;
break;
669 case 52: *profile_level_val =
670 compat_keys.kVTProfileLevel_H264_Baseline_5_2;
break;
675 switch (vtctx->
level) {
676 case 0: *profile_level_val =
677 compat_keys.kVTProfileLevel_H264_Main_AutoLevel;
break;
678 case 30: *profile_level_val = kVTProfileLevel_H264_Main_3_0;
break;
679 case 31: *profile_level_val = kVTProfileLevel_H264_Main_3_1;
break;
680 case 32: *profile_level_val = kVTProfileLevel_H264_Main_3_2;
break;
681 case 40: *profile_level_val = kVTProfileLevel_H264_Main_4_0;
break;
682 case 41: *profile_level_val = kVTProfileLevel_H264_Main_4_1;
break;
683 case 42: *profile_level_val =
685 case 50: *profile_level_val = kVTProfileLevel_H264_Main_5_0;
break;
686 case 51: *profile_level_val =
688 case 52: *profile_level_val =
694 switch (vtctx->
level) {
695 case 0: *profile_level_val =
696 compat_keys.kVTProfileLevel_H264_High_AutoLevel;
break;
697 case 30: *profile_level_val =
699 case 31: *profile_level_val =
701 case 32: *profile_level_val =
703 case 40: *profile_level_val =
705 case 41: *profile_level_val =
707 case 42: *profile_level_val =
709 case 50: *profile_level_val = kVTProfileLevel_H264_High_5_0;
break;
710 case 51: *profile_level_val =
712 case 52: *profile_level_val =
717 switch (vtctx->
level) {
718 case 0: *profile_level_val =
719 compat_keys.kVTProfileLevel_H264_Extended_AutoLevel;
break;
720 case 50: *profile_level_val =
721 compat_keys.kVTProfileLevel_H264_Extended_5_0;
break;
726 if (!*profile_level_val) {
741 CFStringRef *profile_level_val)
746 *profile_level_val =
NULL;
761 if (!*profile_level_val) {
772 int* av_pixel_format,
781 kCVPixelFormatType_420YpCbCr8BiPlanarFullRange :
782 kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
785 kCVPixelFormatType_420YpCbCr8PlanarFullRange :
786 kCVPixelFormatType_420YpCbCr8Planar;
803 CFDictionarySetValue(dict,
804 kCVImageBufferColorPrimariesKey,
809 CFDictionarySetValue(dict,
810 kCVImageBufferTransferFunctionKey,
815 CFDictionarySetValue(dict,
816 kCVImageBufferYCbCrMatrixKey,
822 CFMutableDictionaryRef* dict)
824 CFNumberRef cv_color_format_num =
NULL;
825 CFNumberRef width_num =
NULL;
826 CFNumberRef height_num =
NULL;
827 CFMutableDictionaryRef pixel_buffer_info =
NULL;
836 pixel_buffer_info = CFDictionaryCreateMutable(
839 &kCFCopyStringDictionaryKeyCallBacks,
840 &kCFTypeDictionaryValueCallBacks);
842 if (!pixel_buffer_info)
goto pbinfo_nomem;
844 cv_color_format_num = CFNumberCreate(kCFAllocatorDefault,
847 if (!cv_color_format_num)
goto pbinfo_nomem;
849 CFDictionarySetValue(pixel_buffer_info,
850 kCVPixelBufferPixelFormatTypeKey,
851 cv_color_format_num);
854 width_num = CFNumberCreate(kCFAllocatorDefault,
857 if (!width_num)
return AVERROR(ENOMEM);
859 CFDictionarySetValue(pixel_buffer_info,
860 kCVPixelBufferWidthKey,
864 height_num = CFNumberCreate(kCFAllocatorDefault,
867 if (!height_num)
goto pbinfo_nomem;
869 CFDictionarySetValue(pixel_buffer_info,
870 kCVPixelBufferHeightKey,
876 *dict = pixel_buffer_info;
883 if (pixel_buffer_info) CFRelease(pixel_buffer_info);
889 CFStringRef *primaries)
898 *primaries = kCVImageBufferColorPrimaries_EBU_3213;
902 *primaries = kCVImageBufferColorPrimaries_SMPTE_C;
906 *primaries = kCVImageBufferColorPrimaries_ITU_R_709_2;
910 *primaries =
compat_keys.kCVImageBufferColorPrimaries_ITU_R_2020;
923 CFStringRef *transfer_fnc,
924 CFNumberRef *gamma_level)
932 *transfer_fnc =
NULL;
936 *transfer_fnc = kCVImageBufferTransferFunction_ITU_R_709_2;
940 *transfer_fnc = kCVImageBufferTransferFunction_SMPTE_240M_1995;
943 #if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_SMPTE_ST_2084_PQ
945 *transfer_fnc = kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ;
948 #if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_LINEAR
950 *transfer_fnc = kCVImageBufferTransferFunction_Linear;
953 #if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_ITU_R_2100_HLG
955 *transfer_fnc = kCVImageBufferTransferFunction_ITU_R_2100_HLG;
961 *transfer_fnc = kCVImageBufferTransferFunction_UseGamma;
962 *gamma_level = CFNumberCreate(
NULL, kCFNumberFloat32Type, &gamma);
967 *transfer_fnc = kCVImageBufferTransferFunction_UseGamma;
968 *gamma_level = CFNumberCreate(
NULL, kCFNumberFloat32Type, &gamma);
973 *transfer_fnc =
compat_keys.kCVImageBufferTransferFunction_ITU_R_2020;
977 *transfer_fnc =
NULL;
988 *matrix = kCVImageBufferYCbCrMatrix_ITU_R_709_2;
997 *matrix = kCVImageBufferYCbCrMatrix_ITU_R_601_4;
1001 *matrix = kCVImageBufferYCbCrMatrix_SMPTE_240M_1995;
1005 *matrix =
compat_keys.kCVImageBufferYCbCrMatrix_ITU_R_2020;
1018 CFStringRef profile_level,
1019 CFNumberRef gamma_level,
1020 CFDictionaryRef enc_info,
1021 CFDictionaryRef pixel_buffer_info,
1022 VTCompressionSessionRef *session)
1027 CFNumberRef bit_rate_num;
1028 CFNumberRef bytes_per_second;
1029 CFNumberRef one_second;
1030 CFArrayRef data_rate_limits;
1031 int64_t bytes_per_second_value = 0;
1032 int64_t one_second_value = 0;
1035 int status = VTCompressionSessionCreate(kCFAllocatorDefault,
1041 kCFAllocatorDefault,
1049 #if !TARGET_OS_IPHONE
1051 av_log(avctx,
AV_LOG_ERROR,
"Try -allow_sw 1. The hardware encoder may be busy, or not supported.\n");
1058 bit_rate_num = CFNumberCreate(kCFAllocatorDefault,
1059 kCFNumberSInt32Type,
1061 if (!bit_rate_num)
return AVERROR(ENOMEM);
1064 kVTCompressionPropertyKey_AverageBitRate,
1066 CFRelease(bit_rate_num);
1075 bytes_per_second_value = max_rate >> 3;
1076 bytes_per_second = CFNumberCreate(kCFAllocatorDefault,
1077 kCFNumberSInt64Type,
1078 &bytes_per_second_value);
1079 if (!bytes_per_second) {
1082 one_second_value = 1;
1083 one_second = CFNumberCreate(kCFAllocatorDefault,
1084 kCFNumberSInt64Type,
1087 CFRelease(bytes_per_second);
1090 nums[0] = (
void *)bytes_per_second;
1091 nums[1] = (
void *)one_second;
1092 data_rate_limits = CFArrayCreate(kCFAllocatorDefault,
1093 (
const void **)nums,
1095 &kCFTypeArrayCallBacks);
1097 if (!data_rate_limits) {
1098 CFRelease(bytes_per_second);
1099 CFRelease(one_second);
1103 kVTCompressionPropertyKey_DataRateLimits,
1106 CFRelease(bytes_per_second);
1107 CFRelease(one_second);
1108 CFRelease(data_rate_limits);
1118 if (profile_level) {
1120 kVTCompressionPropertyKey_ProfileLevel,
1123 av_log(avctx,
AV_LOG_ERROR,
"Error setting profile/level property: %d. Output will be encoded using a supported profile/level combination.\n",
status);
1129 CFNumberRef interval = CFNumberCreate(kCFAllocatorDefault,
1137 kVTCompressionPropertyKey_MaxKeyFrameInterval,
1139 CFRelease(interval);
1149 kVTCompressionPropertyKey_MoreFramesBeforeStart,
1152 if (
status == kVTPropertyNotSupportedErr) {
1153 av_log(avctx,
AV_LOG_WARNING,
"frames_before property is not supported on this device. Ignoring.\n");
1161 kVTCompressionPropertyKey_MoreFramesAfterEnd,
1164 if (
status == kVTPropertyNotSupportedErr) {
1165 av_log(avctx,
AV_LOG_WARNING,
"frames_after property is not supported on this device. Ignoring.\n");
1174 CFMutableDictionaryRef par;
1181 num = CFNumberCreate(kCFAllocatorDefault,
1185 den = CFNumberCreate(kCFAllocatorDefault,
1191 par = CFDictionaryCreateMutable(kCFAllocatorDefault,
1193 &kCFCopyStringDictionaryKeyCallBacks,
1194 &kCFTypeDictionaryValueCallBacks);
1196 if (!par || !num || !den) {
1197 if (par) CFRelease(par);
1198 if (num) CFRelease(num);
1199 if (den) CFRelease(den);
1204 CFDictionarySetValue(
1206 kCMFormatDescriptionKey_PixelAspectRatioHorizontalSpacing,
1209 CFDictionarySetValue(
1211 kCMFormatDescriptionKey_PixelAspectRatioVerticalSpacing,
1215 kVTCompressionPropertyKey_PixelAspectRatio,
1225 "Error setting pixel aspect ratio to %d:%d: %d.\n",
1237 kVTCompressionPropertyKey_TransferFunction,
1248 kVTCompressionPropertyKey_YCbCrMatrix,
1259 kVTCompressionPropertyKey_ColorPrimaries,
1269 kCVImageBufferGammaLevelKey,
1279 kVTCompressionPropertyKey_AllowFrameReordering,
1294 compat_keys.kVTCompressionPropertyKey_H264EntropyMode,
1312 status = VTCompressionSessionPrepareToEncodeFrames(vtctx->
session);
1323 CFMutableDictionaryRef enc_info;
1324 CFMutableDictionaryRef pixel_buffer_info;
1327 CFStringRef profile_level;
1328 CFNumberRef gamma_level =
NULL;
1344 av_log(avctx,
AV_LOG_WARNING,
"Cannot use B-frames with baseline profile. Output will not contain B-frames.\n");
1349 av_log(avctx,
AV_LOG_WARNING,
"CABAC entropy requires 'main' or 'high' profile, but baseline was requested. Encode will not use CABAC entropy.\n");
1360 enc_info = CFDictionaryCreateMutable(
1361 kCFAllocatorDefault,
1363 &kCFCopyStringDictionaryKeyCallBacks,
1364 &kCFTypeDictionaryValueCallBacks
1367 if (!enc_info)
return AVERROR(ENOMEM);
1369 #if !TARGET_OS_IPHONE
1371 CFDictionarySetValue(enc_info,
1372 compat_keys.kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
1375 CFDictionarySetValue(enc_info,
1376 compat_keys.kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder,
1379 CFDictionarySetValue(enc_info,
1380 compat_keys.kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
1390 pixel_buffer_info =
NULL;
1421 CFRelease(gamma_level);
1423 if (pixel_buffer_info)
1424 CFRelease(pixel_buffer_info);
1426 CFRelease(enc_info);
1434 CFBooleanRef has_b_frames_cfbool;
1447 kVTCompressionPropertyKey_AllowFrameReordering,
1448 kCFAllocatorDefault,
1449 &has_b_frames_cfbool);
1451 if (!
status && has_b_frames_cfbool) {
1453 vtctx->
has_b_frames = CFBooleanGetValue(has_b_frames_cfbool);
1454 CFRelease(has_b_frames_cfbool);
1463 CFArrayRef attachments;
1464 CFDictionaryRef attachment;
1465 CFBooleanRef not_sync;
1468 attachments = CMSampleBufferGetSampleAttachmentsArray(
buffer,
false);
1469 len = !attachments ? 0 : CFArrayGetCount(attachments);
1472 *is_key_frame =
true;
1476 attachment = CFArrayGetValueAtIndex(attachments, 0);
1478 if (CFDictionaryGetValueIfPresent(attachment,
1479 kCMSampleAttachmentKey_NotSync,
1480 (
const void **)¬_sync))
1482 *is_key_frame = !CFBooleanGetValue(not_sync);
1484 *is_key_frame =
true;
1505 size_t sei_payload_size = 0;
1506 int sei_payload_type = 0;
1508 uint8_t *nal_start = nal_data;
1513 nal_type = *nal_data & 0x1F;
1520 if (nal_data[nal_size - 1] == 0x80)
1523 while (nal_size > 0 && *nal_data > 0) {
1525 sei_payload_type += *nal_data;
1528 }
while (nal_size > 0 && *nal_data == 0xFF);
1536 sei_payload_size += *nal_data;
1539 }
while (nal_size > 0 && *nal_data == 0xFF);
1541 if (nal_size < sei_payload_size) {
1546 nal_data += sei_payload_size;
1547 nal_size -= sei_payload_size;
1550 *sei_end = nal_data;
1552 return nal_data - nal_start + 1;
1572 uint8_t* dst_end = dst + dst_size;
1574 int start_at = dst_offset > 2 ? dst_offset - 2 : 0;
1576 for (
i = start_at;
i < dst_offset &&
i < dst_size;
i++) {
1585 for (;
src < src_end;
src++, dst++) {
1587 int insert_ep3_byte = *
src <= 3;
1588 if (insert_ep3_byte) {
1606 wrote_bytes = dst - dst_start;
1609 return -wrote_bytes;
1620 size_t remaining_sei_size =
sei->size;
1621 size_t remaining_dst_size = dst_size;
1626 if (!remaining_dst_size)
1629 while (sei_type && remaining_dst_size != 0) {
1630 int sei_byte = sei_type > 255 ? 255 : sei_type;
1633 sei_type -= sei_byte;
1635 remaining_dst_size--;
1641 while (remaining_sei_size && remaining_dst_size != 0) {
1642 int size_byte = remaining_sei_size > 255 ? 255 : remaining_sei_size;
1645 remaining_sei_size -= size_byte;
1647 remaining_dst_size--;
1650 if (remaining_dst_size < sei->
size)
1653 header_bytes = dst - sei_start;
1661 if (bytes_written < 0)
1664 bytes_written += header_bytes;
1665 return bytes_written;
1689 size_t length_code_size,
1690 CMSampleBufferRef sample_buffer,
1695 size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
1696 size_t remaining_src_size = src_size;
1697 size_t remaining_dst_size = dst_size;
1698 size_t src_offset = 0;
1703 CMBlockBufferRef
block = CMSampleBufferGetDataBuffer(sample_buffer);
1705 if (length_code_size > 4) {
1709 while (remaining_src_size > 0) {
1710 size_t curr_src_len;
1711 size_t curr_dst_len;
1727 src_offset + length_code_size,
1738 for (
i = 0;
i < length_code_size;
i++) {
1740 box_len |= size_buf[
i];
1753 remaining_dst_size--;
1758 remaining_dst_size);
1760 if (wrote_bytes < 0)
1763 remaining_dst_size -= wrote_bytes;
1764 dst_data += wrote_bytes;
1766 if (remaining_dst_size <= 0)
1772 remaining_dst_size--;
1777 curr_src_len = box_len + length_code_size;
1780 if (remaining_src_size < curr_src_len) {
1784 if (remaining_dst_size < curr_dst_len) {
1792 src_offset + length_code_size,
1807 old_sei_length =
find_sei_end(avctx, dst_box, box_len, &new_sei);
1808 if (old_sei_length < 0)
1814 remaining_dst_size - old_sei_length);
1815 if (wrote_bytes < 0)
1818 if (new_sei + wrote_bytes >= dst_data + remaining_dst_size)
1821 new_sei[wrote_bytes++] = 0x80;
1822 extra_bytes = wrote_bytes - (dst_box + box_len - new_sei);
1824 dst_data += extra_bytes;
1825 remaining_dst_size -= extra_bytes;
1830 src_offset += curr_src_len;
1831 dst_data += curr_dst_len;
1833 remaining_src_size -= curr_src_len;
1834 remaining_dst_size -= curr_dst_len;
1855 if ((
sei->size % 255) == 0)
1858 return copied_size +
sei->size / 255 + 1 +
type / 255 + 1;
1863 CMSampleBufferRef sample_buffer,
1872 size_t length_code_size;
1873 size_t header_size = 0;
1875 size_t out_buf_size;
1876 size_t sei_nalu_size = 0;
1878 int64_t time_base_num;
1882 CMVideoFormatDescriptionRef vid_fmt;
1892 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
1910 sei_nalu_size =
sizeof(
start_code) + 1 + msg_size + 1;
1913 in_buf_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
1914 out_buf_size = header_size +
1946 pts = CMSampleBufferGetPresentationTimeStamp(sample_buffer);
1947 dts = CMSampleBufferGetDecodeTimeStamp (sample_buffer);
1949 if (CMTIME_IS_INVALID(dts)) {
1961 pkt->
dts = dts.value / time_base_num - dts_delta;
1979 size_t *contiguous_buf_size)
1982 int av_format =
frame->format;
1983 int av_color_range =
frame->color_range;
1992 "Could not get pixel format for color format '%s' range '%s'.\n",
2002 if (range_guessed) {
2007 "Color range not set for %s. Using MPEG range.\n",
2012 switch (av_format) {
2016 widths [0] = avctx->
width;
2017 heights[0] = avctx->
height;
2020 widths [1] = (avctx->
width + 1) / 2;
2021 heights[1] = (avctx->
height + 1) / 2;
2028 widths [0] = avctx->
width;
2029 heights[0] = avctx->
height;
2032 widths [1] = (avctx->
width + 1) / 2;
2033 heights[1] = (avctx->
height + 1) / 2;
2036 widths [2] = (avctx->
width + 1) / 2;
2037 heights[2] = (avctx->
height + 1) / 2;
2043 widths[0] = avctx->
width;
2044 heights[0] = avctx->
height;
2045 strides[0] =
frame ?
frame->linesize[0] : (avctx->
width * 2 + 63) & -64;
2047 widths[1] = (avctx->
width + 1) / 2;
2048 heights[1] = (avctx->
height + 1) / 2;
2049 strides[1] =
frame ?
frame->linesize[1] : ((avctx->
width + 1) / 2 + 63) & -64;
2056 "Could not get frame format info for color %d range %d.\n",
2063 *contiguous_buf_size = 0;
2064 for (
i = 0;
i < *plane_count;
i++) {
2065 if (
i < *plane_count - 1 &&
2066 frame->data[
i] + strides[
i] * heights[
i] !=
frame->data[
i + 1]) {
2067 *contiguous_buf_size = 0;
2071 *contiguous_buf_size += strides[
i] * heights[
i];
2080 CVPixelBufferRef cv_img,
2081 const size_t *plane_strides,
2082 const size_t *plane_rows)
2094 status = CVPixelBufferLockBaseAddress(cv_img, 0);
2099 "Error: Could not lock base address of CVPixelBuffer: %d.\n",
2104 if (CVPixelBufferIsPlanar(cv_img)) {
2105 plane_count = CVPixelBufferGetPlaneCount(cv_img);
2107 if (
i == plane_count) {
2108 CVPixelBufferUnlockBaseAddress(cv_img, 0);
2111 "Error: different number of planes in AVFrame and CVPixelBuffer.\n"
2117 dst_addr = (
uint8_t*)CVPixelBufferGetBaseAddressOfPlane(cv_img,
i);
2119 dst_stride = CVPixelBufferGetBytesPerRowOfPlane(cv_img,
i);
2120 src_stride = plane_strides[
i];
2121 rows = plane_rows[
i];
2123 if (dst_stride == src_stride) {
2124 memcpy(dst_addr, src_addr, src_stride * rows);
2126 copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
2128 for (j = 0; j < rows; j++) {
2129 memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
2134 if (
frame->data[1]) {
2135 CVPixelBufferUnlockBaseAddress(cv_img, 0);
2138 "Error: different number of planes in AVFrame and non-planar CVPixelBuffer.\n"
2144 dst_addr = (
uint8_t*)CVPixelBufferGetBaseAddress(cv_img);
2146 dst_stride = CVPixelBufferGetBytesPerRow(cv_img);
2147 src_stride = plane_strides[0];
2148 rows = plane_rows[0];
2150 if (dst_stride == src_stride) {
2151 memcpy(dst_addr, src_addr, src_stride * rows);
2153 copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
2155 for (j = 0; j < rows; j++) {
2156 memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
2161 status = CVPixelBufferUnlockBaseAddress(cv_img, 0);
2172 CVPixelBufferRef *cv_img)
2180 size_t contiguous_buf_size;
2181 CVPixelBufferPoolRef pix_buf_pool;
2187 *cv_img = (CVPixelBufferRef)
frame->data[3];
2194 memset(widths, 0,
sizeof(widths));
2195 memset(heights, 0,
sizeof(heights));
2196 memset(strides, 0,
sizeof(strides));
2206 &contiguous_buf_size
2213 "Error: Cannot convert format %d color_range %d: %d\n",
2222 pix_buf_pool = VTCompressionSessionGetPixelBufferPool(vtctx->
session);
2223 if (!pix_buf_pool) {
2230 vtstatus = VTCompressionSessionPrepareToEncodeFrames(vtctx->
session);
2231 if (vtstatus == kVTInvalidSessionErr) {
2236 pix_buf_pool = VTCompressionSessionGetPixelBufferPool(vtctx->
session);
2238 if (!pix_buf_pool) {
2244 "kVTInvalidSessionErr error.\n");
2247 status = CVPixelBufferPoolCreatePixelBuffer(
NULL,
2268 CFDictionaryRef* dict_out)
2270 CFDictionaryRef dict =
NULL;
2272 const void *keys[] = { kVTEncodeFrameOptionKey_ForceKeyFrame };
2273 const void *vals[] = { kCFBooleanTrue };
2275 dict = CFDictionaryCreate(
NULL, keys, vals, 1,
NULL,
NULL);
2276 if(!dict)
return AVERROR(ENOMEM);
2288 CFDictionaryRef frame_dict;
2289 CVPixelBufferRef cv_img =
NULL;
2303 if (vtctx->
a53_cc && side_data && side_data->
size) {
2318 status = VTCompressionSessionEncodeFrame(
2328 if (frame_dict) CFRelease(frame_dict);
2348 CMSampleBufferRef buf =
NULL;
2387 if (
status)
goto end_nopkt;
2388 if (!buf)
goto end_nopkt;
2396 if (
status)
goto end_nopkt;
2408 CFStringRef profile_level,
2409 CFNumberRef gamma_level,
2410 CFDictionaryRef enc_info,
2411 CFDictionaryRef pixel_buffer_info)
2415 CVPixelBufferPoolRef pool =
NULL;
2416 CVPixelBufferRef pix_buf =
NULL;
2418 CMSampleBufferRef buf =
NULL;
2430 pool = VTCompressionSessionGetPixelBufferPool(vtctx->
session);
2436 status = CVPixelBufferPoolCreatePixelBuffer(
NULL,
2440 if(
status != kCVReturnSuccess){
2457 "Error sending frame for extradata: %d\n",
2501 VTCompressionSessionCompleteFrames(vtctx->
session,
2540 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
2541 #define COMMON_OPTIONS \
2542 { "allow_sw", "Allow software encoding", OFFSET(allow_sw), AV_OPT_TYPE_BOOL, \
2543 { .i64 = 0 }, 0, 1, VE }, \
2544 { "require_sw", "Require software encoding", OFFSET(require_sw), AV_OPT_TYPE_BOOL, \
2545 { .i64 = 0 }, 0, 1, VE }, \
2546 { "realtime", "Hint that encoding should happen in real-time if not faster (e.g. capturing from camera).", \
2547 OFFSET(realtime), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
2548 { "frames_before", "Other frames will come before the frames in this session. This helps smooth concatenation issues.", \
2549 OFFSET(frames_before), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
2550 { "frames_after", "Other frames will come after the frames in this session. This helps smooth concatenation issues.", \
2551 OFFSET(frames_after), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
2553 #define OFFSET(x) offsetof(VTEncContext, x)
2562 {
"1.3",
"Level 1.3, only available with Baseline Profile", 0,
AV_OPT_TYPE_CONST, { .i64 = 13 }, INT_MIN, INT_MAX,
VE,
"level" },
2563 {
"3.0",
"Level 3.0", 0,
AV_OPT_TYPE_CONST, { .i64 = 30 }, INT_MIN, INT_MAX,
VE,
"level" },
2564 {
"3.1",
"Level 3.1", 0,
AV_OPT_TYPE_CONST, { .i64 = 31 }, INT_MIN, INT_MAX,
VE,
"level" },
2565 {
"3.2",
"Level 3.2", 0,
AV_OPT_TYPE_CONST, { .i64 = 32 }, INT_MIN, INT_MAX,
VE,
"level" },
2566 {
"4.0",
"Level 4.0", 0,
AV_OPT_TYPE_CONST, { .i64 = 40 }, INT_MIN, INT_MAX,
VE,
"level" },
2567 {
"4.1",
"Level 4.1", 0,
AV_OPT_TYPE_CONST, { .i64 = 41 }, INT_MIN, INT_MAX,
VE,
"level" },
2568 {
"4.2",
"Level 4.2", 0,
AV_OPT_TYPE_CONST, { .i64 = 42 }, INT_MIN, INT_MAX,
VE,
"level" },
2569 {
"5.0",
"Level 5.0", 0,
AV_OPT_TYPE_CONST, { .i64 = 50 }, INT_MIN, INT_MAX,
VE,
"level" },
2570 {
"5.1",
"Level 5.1", 0,
AV_OPT_TYPE_CONST, { .i64 = 51 }, INT_MIN, INT_MAX,
VE,
"level" },
2571 {
"5.2",
"Level 5.2", 0,
AV_OPT_TYPE_CONST, { .i64 = 52 }, INT_MIN, INT_MAX,
VE,
"level" },
2593 .
name =
"h264_videotoolbox",
2625 .
name =
"hevc_videotoolbox",
2638 .wrapper_name =
"videotoolbox",