diff --git a/MP42/MP42FormatUtilites.h b/MP42/MP42FormatUtilites.h index 7c87c2e..6e6de9c 100644 --- a/MP42/MP42FormatUtilites.h +++ b/MP42/MP42FormatUtilites.h @@ -19,6 +19,8 @@ extern "C" { int analyze_EAC3(void **context ,uint8_t *frame, uint32_t size); CFDataRef createCookie_EAC3(void *context); + int analyze_HEVC(const uint8_t *frame, uint32_t cookieLen, bool *completeness); + #ifdef __cplusplus } #endif diff --git a/MP42/MP42FormatUtilites.mm b/MP42/MP42FormatUtilites.mm index 6f61005..a576595 100644 --- a/MP42/MP42FormatUtilites.mm +++ b/MP42/MP42FormatUtilites.mm @@ -802,3 +802,114 @@ CFDataRef createCookie_EAC3(void *context) return cookieData; } + +#pragma mark - HEVC + +struct NAL_units{ + UInt8 array_completeness; + UInt8 NAL_unit_type; + UInt16 numNalus; +}; + + +struct hvcC_info { + UInt8 configurationVersion; + + UInt8 general_profile_space; + UInt8 general_tier_flag; + UInt8 general_profile_idc; + UInt32 general_profile_compatibility_flags; + UInt64 general_constraint_indicator_flags; + UInt8 general_level_idc; + + UInt16 min_spatial_segmentation_idc; + UInt8 parallelismType; + + UInt8 chromaFormat; + UInt8 bitDepthLumaMinus8; + UInt8 bitDepthChromaMinus8; + + UInt16 avgFrameRate; + UInt8 constantFrameRate; + + UInt8 numTemporalLayers; + UInt8 temporalIdNested; + + UInt8 lengthSizeMinusOne; + UInt8 numOfArrays; + + struct NAL_units *NAL_units; +}; + +int analyze_HEVC(const uint8_t *cookie, uint32_t cookieLen, bool *completeness) +{ + int result = 0; + bool complete = true; + + struct hvcC_info *info = (struct hvcC_info *)malloc (sizeof(struct hvcC_info)); + bzero(info, sizeof(struct hvcC_info)); + + CMemoryBitstream b; + b.SetBytes((uint8_t *)cookie, cookieLen); + + try { + info->configurationVersion = b.GetBits(8); + info->general_profile_space = b.GetBits(2); + info->general_tier_flag = b.GetBits(1); + info->general_profile_idc = b.GetBits(5); + info->general_profile_compatibility_flags = b.GetBits(32); + + info->general_constraint_indicator_flags = b.GetBits(32) << 16; + info->general_constraint_indicator_flags += b.GetBits(16); + info->general_level_idc = b.GetBits(8); + + b.SkipBits(4); // reserved 1111b + info->min_spatial_segmentation_idc = b.GetBits(12); + b.SkipBits(6); // reserved 111111b + info->parallelismType = b.GetBits(2); + b.SkipBits(6); // reserved 111111b + info->chromaFormat = b.GetBits(2); + b.SkipBits(5); // reserved 11111b + info->bitDepthLumaMinus8 = b.GetBits(3); + b.SkipBits(5); // reserved 11111b + info->bitDepthChromaMinus8 = b.GetBits(3); + + info->avgFrameRate = b.GetBits(16); + info->constantFrameRate = b.GetBits(2); + + info->numTemporalLayers = b.GetBits(3); + info->temporalIdNested = b.GetBits(1); + + info->lengthSizeMinusOne = b.GetBits(2); + info->numOfArrays = b.GetBits(8); + + info->NAL_units = (struct NAL_units *)malloc(sizeof(struct NAL_units) * info->numOfArrays); + + for (UInt8 j = 0; j < info->numOfArrays; j++) { + info->NAL_units[j].array_completeness = b.GetBits(1); + if (info->NAL_units[j].array_completeness == 0) { + complete = false; + } + + b.SkipBits(1); // reserved 0 + + info->NAL_units[j].NAL_unit_type = b.GetBits(6); + info->NAL_units[j].numNalus = b.GetBits(16); + + for (UInt8 i = 0; i < info->NAL_units[j].numNalus; i++) { + UInt16 nalUnitLength = b.GetBits(16); + b.SkipBits(8 * nalUnitLength); + } + } + } + catch (int e) { + result = 1; + } + + free(info->NAL_units); + free(info); + + *completeness = complete; + + return 0; +} diff --git a/MP42/MP42Muxer.m b/MP42/MP42Muxer.m index acdd5eb..1eba54f 100644 --- a/MP42/MP42Muxer.m +++ b/MP42/MP42Muxer.m @@ -17,6 +17,7 @@ #import "MP42BitmapSubConverter.h" #import "mp4v2.h" +#import "MP42FormatUtilites.h" #import "MP42PrivateUtilities.h" #import "MP42Track+Muxer.h" @@ -189,19 +190,29 @@ else if ([track isMemberOfClass:[MP42VideoTrack class]] && (track.format == kMP42VideoCodecType_HEVC || track.format == kMP42VideoCodecType_HEVC_2)) { - if (magicCookie.length < sizeof(uint8_t) * 6) { + // Check whether we can use hvc1 or hev1 fourcc. + bool completeness = 0; + if (magicCookie.length && !analyze_HEVC(magicCookie.bytes, magicCookie.length, &completeness)) { + NSSize size = [helper->importer sizeForTrack:track]; + + dstTrackId = MP4AddH265VideoTrack(_fileHandle, timeScale, MP4_INVALID_DURATION, size.width, size.height, completeness); + + if (dstTrackId) { + if (completeness) { + MP4SetTrackBytesProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.hvc1.hvcC.content", magicCookie.bytes, magicCookie.length); + } + else { + MP4SetTrackBytesProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.hev1.hvcC.content", magicCookie.bytes, magicCookie.length); + } + [helper->importer setActiveTrack:track]; + } + else { + continue; + } + } + else { continue; } - - NSSize size = [helper->importer sizeForTrack:track]; - - dstTrackId = MP4AddH265VideoTrack(_fileHandle, timeScale, MP4_INVALID_DURATION, size.width, size.height); - - if (dstTrackId) { - MP4SetTrackBytesProperty(_fileHandle, dstTrackId, "mdia.minf.stbl.stsd.hev1.hvcC.content", magicCookie.bytes, magicCookie.length); - } - - [helper->importer setActiveTrack:track]; } // MPEG-4 Visual video track diff --git a/MP42/MP42PrivateUtilities.m b/MP42/MP42PrivateUtilities.m index 1a2d135..922bc51 100644 --- a/MP42/MP42PrivateUtilities.m +++ b/MP42/MP42PrivateUtilities.m @@ -196,6 +196,9 @@ FourCharCode getTrackMediaFormat(MP4FileHandle fileHandle, MP4TrackId Id) if (!strcmp(dataName, "avc1")) { return kMP42VideoCodecType_H264; } + else if (!strcmp(dataName, "hvc1")) { + return kMP42VideoCodecType_HEVC; + } else if (!strcmp(dataName, "hev1")) { return kMP42VideoCodecType_HEVC_2; } diff --git a/contrib b/contrib index fbe5205..3d27e21 160000 --- a/contrib +++ b/contrib @@ -1 +1 @@ -Subproject commit fbe5205a67e49224e502f634bea532c145b48703 +Subproject commit 3d27e21ff6953cd80d2b064f29bdc9743df55090