From d45a89c5e6bba393a0f112865f8d300b0f3ed360 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Fri, 9 Jun 2017 20:09:12 +0200 Subject: [PATCH] Added isKeyframe function to H264 library --- lib/h264.cpp | 30 ++++++++++++++++++++++++++++++ lib/h264.h | 1 + 2 files changed, 31 insertions(+) diff --git a/lib/h264.cpp b/lib/h264.cpp index 408a9900..05688266 100644 --- a/lib/h264.cpp +++ b/lib/h264.cpp @@ -9,6 +9,36 @@ #include "defines.h" namespace h264 { + + ///Helper function to determine if a H264 NAL unit is a keyframe or not + bool isKeyframe(const char * data, uint32_t len){ + uint8_t nalType = (data[0] & 0x1F); + if (nalType == 0x05){return true;} + if (nalType != 0x01){return false;} + Utils::bitstream bs; + for (size_t i = 1; i < 10 && i < len; ++i) { + if (i + 2 < len && (memcmp(data + i, "\000\000\003", 3) == 0)) { //Emulation prevention bytes + bs.append(data + i, 2); + i += 2; + } else { + bs.append(data + i, 1); + } + } + bs.getExpGolomb();//Discard first_mb_in_slice + uint64_t sliceType = bs.getUExpGolomb(); + //Slice types: + // 0: P - Predictive slice (at most 1 reference) + // 1: B - Bi-predictive slice (at most 2 references) + // 2: I - Intra slice (no external references) + // 3: SP - Switching predictive slice (at most 1 reference) + // 4: SI - Switching intra slice (no external references) + // 5-9: 0-4, but all in picture of same type + if (sliceType == 2 || sliceType == 4 || sliceType == 7 || sliceType == 9){ + return true; + } + return false; + } + std::deque analysePackets(const char * data, unsigned long len){ std::deque res; diff --git a/lib/h264.h b/lib/h264.h index 0afd0540..7578308f 100644 --- a/lib/h264.h +++ b/lib/h264.h @@ -67,4 +67,5 @@ namespace h264 { unsigned long dataLen; }; + bool isKeyframe(const char * data, uint32_t len); }