Overview
The KlvEncoder C++ library provides methods to encode KLV format metadata. KLV (Key-Length-Value) is a video metadata encoding format which uses a type-length-value structure (the standard SMPTE 336M-2007 defines the KLV encoding format). Metadata is encoded into Key-Length-Value blocks that contain a data ID (a standard key or a tag number), the data length, and the data itself. Bytes are encoded in big-endian format. The KlvEncoder library prepares a buffer with KLV metadata. The library is cross-platform and compatible with Windows and Linux OS. The main file KlvEncoder.h includes the declaration of the KlvEncoder class which provides methods for encoding video metadata into KLV format. The library requires C++17 standard. The library doesn’t have any third-party dependencies. The library implements the ST0601.19 standard (Unmanned Air System (UAS) Datalink Local Set (LS) for UAS platforms) (see MISBTag enumeration for supported tags). Note: The library encodes linear KLV buffer structure with Universal Key and BER length.
Downloads
Documentation: GO TO DOCUMENTATION
Simple interface
class KlvEncoder
{
public:
/// Get library version.
static std::string getVersion();
/// Add parameter to buffer.
bool addParam(MISBTag key, const void* value, size_t length = 0);
/// Reset buffer.
void resetBuffer();
/// Get buffer with KLV metadata.
std::vector<uint8_t> getBuffer();
};
Simple example
#include <iostream>
#include <filesystem>
#include <fstream>
#include "KlvEncoder.h"
// Link namespaces.
using namespace std;
using namespace filesystem;
using namespace cr::metadata;
// Entry point.
int main(void)
{
// Create encoder instance.
KlvEncoder encoder;
// Reset buffer before encoding new KLV package.
encoder.resetBuffer();
// Add camera horizontal Field of View, Tag 16.
float horizontalFov = 90.0f;
encoder.addParam(MISBTag::ST0601_16_SensorHorizontalFieldOfView,
(void*)&horizontalFov);
// Add camera vertical Field of View, Tag 17.
float verticalFov = 45.0f;
encoder.addParam(MISBTag::ST0601_17_SensorVerticalFieldOfView,
(void*)&verticalFov);
// Add relative Azimuth Angle, Tag 18.
double azimuth = 45.0;
encoder.addParam(MISBTag::ST0601_18_SensorRelativeAzimuthAngle,
(void*)&azimuth);
// Add relative Elevation Angle, Tag 19.
double elevation = -30.0;
encoder.addParam(MISBTag::ST0601_19_SensorRelativeElevationAngle,
(void*)&elevation);
// Get KLV buffer.
vector<uint8_t> klvBuffer = encoder.getBuffer();
// Show bytes in hex.
cout << "KLV Buffer (" << klvBuffer.size() << " bytes): ";
for (size_t i = 0; i < klvBuffer.size(); i++)
cout << hex << (int)klvBuffer[i] << " ";
cout << endl;
// Save KLV buffer to file.
path outputPath = "output.klv";
ofstream outputFile(outputPath, ios::binary);
for (size_t i = 0; i < klvBuffer.size(); i++)
outputFile << klvBuffer[i];
outputFile.close();
return 1;
}

