Skip to content

Commit

Permalink
[Utils] Add HvccToAnnexb method
Browse files Browse the repository at this point in the history
  • Loading branch information
CastagnaIT committed Nov 12, 2024
1 parent a1a28e0 commit 56b683f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
58 changes: 57 additions & 1 deletion src/utils/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "Base64Utils.h"
#include "StringUtils.h"
#include "log.h"
#include "kodi/tools/StringUtils.h"

#include <algorithm> // any_of
#include <chrono>
Expand All @@ -21,6 +20,63 @@
using namespace UTILS;
using namespace kodi::tools;

std::vector<uint8_t> UTILS::HvccToAnnexb(const std::vector<uint8_t>& hvcc)
{
if (hvcc.size() < 23)
{
LOG::LogF(LOGERROR, "Cannot convert HVCC data, wrong data size");
return {};
}

std::vector<uint8_t> result;
const uint8_t* data = hvcc.data() + 22; // Start after length field
const uint8_t* end = hvcc.data() + hvcc.size();
uint8_t numSequences = *data++;

for (uint8_t i = 0; i < numSequences; ++i)
{
if (data + 2 > end)
{
LOG::LogF(LOGERROR, "Cannot convert HVCC data, wrong data size");
return {};
}

uint8_t nalType = (*data++ << 1);
uint16_t numNals = (data[0] << 8) | data[1];
data += 2;

for (uint16_t j = 0; j < numNals; ++j)
{
if (data + 2 > end)
{
LOG::LogF(LOGERROR, "Cannot convert HVCC data, wrong data size");
return {};
}

uint16_t nalSize = (data[0] << 8) | data[1];
data += 2;

if (data + nalSize > end)
{
LOG::LogF(LOGERROR, "Cannot convert HVCC data, wrong data size");
return {};
}

// Add Annex B start code
result.push_back(0x00);
result.push_back(0x00);
result.push_back(0x00);
result.push_back(0x01);

// Copy NAL data
result.insert(result.end(), data, data + nalSize);
data += nalSize;
}
}

return result;
}

std::vector<uint8_t> UTILS::AnnexbToHvcc(const std::vector<uint8_t>& annexb)
{
std::vector<uint8_t> result;
Expand Down
1 change: 1 addition & 0 deletions src/utils/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ constexpr uint8_t DEFAULT_KEYID[16]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// Placeholder for unknown AP4_Track id
constexpr uint32_t AP4_TRACK_ID_UNKNOWN = -1;

std::vector<uint8_t> HvccToAnnexb(const std::vector<uint8_t>& hvcc);
std::vector<uint8_t> AnnexbToHvcc(const std::vector<uint8_t>& annexb);
std::vector<uint8_t> AnnexbToAvc(const std::vector<uint8_t>& annexb);
bool IsAnnexB(const std::vector<uint8_t>& data);
Expand Down

0 comments on commit 56b683f

Please sign in to comment.