From 068c5b1cb9eaa05e88e05f69a16d69b65b0447ef Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Sat, 16 Sep 2017 14:38:34 +0200 Subject: [PATCH] Add a way to force the 'hvc1' fourCC until a better solution is developed. --- MP42/MP42FormatUtilites.h | 1 + MP42/MP42FormatUtilites.mm | 29 ++++++++++++++++++++++++++--- MP42/MP42Muxer.m | 3 +++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/MP42/MP42FormatUtilites.h b/MP42/MP42FormatUtilites.h index a1c9031..507ba31 100644 --- a/MP42/MP42FormatUtilites.h +++ b/MP42/MP42FormatUtilites.h @@ -41,6 +41,7 @@ extern "C" { int analyze_AVC(const uint8_t *cookie, uint32_t cookieLen); int analyze_HEVC(const uint8_t *frame, uint32_t cookieLen, bool *completeness); + void force_HEVC_completeness(const uint8_t *cookie, uint32_t cookieLen); #ifdef __cplusplus } diff --git a/MP42/MP42FormatUtilites.mm b/MP42/MP42FormatUtilites.mm index bf842a8..f3a9540 100644 --- a/MP42/MP42FormatUtilites.mm +++ b/MP42/MP42FormatUtilites.mm @@ -1199,7 +1199,7 @@ typedef struct HEVCConfig { struct NAL_units *NAL_units; } HEVCConfig; -int analyze_HEVC(const uint8_t *cookie, uint32_t cookieLen, bool *completeness) +int parse_HEVC(const uint8_t *cookie, uint32_t cookieLen, bool *completeness, bool forceCompleteness) { int result = 0; bool complete = true; @@ -1211,7 +1211,11 @@ int analyze_HEVC(const uint8_t *cookie, uint32_t cookieLen, bool *completeness) b.SetBytes((uint8_t *)cookie, cookieLen); try { - info->configurationVersion = b.GetBits(8); + if (forceCompleteness) { + b.PutBits(1, 8); + } else { + 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); @@ -1245,7 +1249,10 @@ int analyze_HEVC(const uint8_t *cookie, uint32_t cookieLen, bool *completeness) for (UInt8 j = 0; j < info->numOfArrays; j++) { bool unitIsComplete = true; + u_int32_t completenessPos = b.GetBitPosition(); + info->NAL_units[j].array_completeness = b.GetBits(1); + if (info->NAL_units[j].array_completeness == 0) { unitIsComplete = false; } @@ -1258,7 +1265,14 @@ int analyze_HEVC(const uint8_t *cookie, uint32_t cookieLen, bool *completeness) if (info->NAL_units[j].NAL_unit_type == NAL_UNIT_VPS || info->NAL_units[j].NAL_unit_type == NAL_UNIT_SPS || info->NAL_units[j].NAL_unit_type == NAL_UNIT_PPS) { - complete = unitIsComplete; + if (forceCompleteness && info->NAL_units[j].numNalus > 0) { + u_int32_t currentPos = b.GetBitPosition(); + b.SetBitPosition(completenessPos); + b.PutBits(1, 1); + b.SetBitPosition(currentPos); + } else { + complete = unitIsComplete; + } } for (UInt8 i = 0; i < info->NAL_units[j].numNalus; i++) { @@ -1278,3 +1292,12 @@ int analyze_HEVC(const uint8_t *cookie, uint32_t cookieLen, bool *completeness) return 0; } + +int analyze_HEVC(const uint8_t *cookie, uint32_t cookieLen, bool *completeness) { + return parse_HEVC(cookie, cookieLen, completeness, false); +} + +void force_HEVC_completeness(const uint8_t *cookie, uint32_t cookieLen) { + bool completeness = 0; + parse_HEVC(cookie, cookieLen, &completeness, true); +} diff --git a/MP42/MP42Muxer.m b/MP42/MP42Muxer.m index d45041c..89087bc 100644 --- a/MP42/MP42Muxer.m +++ b/MP42/MP42Muxer.m @@ -200,6 +200,9 @@ else if ([track isMemberOfClass:[MP42VideoTrack class]] && (format == kMP42VideoCodecType_HEVC || format == kMP42VideoCodecType_HEVC_PSinBitstream)) { + uint8_t *hvcCAtom = (uint8_t *)magicCookie.bytes; + force_HEVC_completeness(hvcCAtom, magicCookie.length); + // Check whether we can use hvc1 or hev1 fourcc. bool completeness = 0; if (magicCookie.length && !analyze_HEVC(magicCookie.bytes, magicCookie.length, &completeness)) {