HEVC / H.265 Annex B Decoder (Run)¶
This article explains how you can use TTranscoder::run to decode a HEVC / H.265 Annex B elementary stream to a raw YUV video file.
The code snippets in this article are from the dec_hevc_file macOS sample.
Linux and Windows samples are also available:
Source Video¶
For source we use a HEVC-encoded video file. After downloading and unzipping the AVBlocks Assets archive, you can find HEVC test files in the vid subdirectory.
Code¶
This code takes a compressed HEVC elementary stream file and decodes it to raw YUV video format.
Initialize AVBlocks¶
The first step in any AVBlocks application is to initialize the library. This must be done before using any other AVBlocks functionality. The TLibrary constructor initializes the library and loads the necessary codecs. The TLibrary destructor shuts the library down and releases any allocated memory, so the library is terminated when the TLibrary object goes out of scope at the end of main.
int main(int argc, char* argv[])
{
Options opt;
switch(prepareOptions(opt, argc, argv))
{
case Command: return 0;
case Error: return 1;
case Parsed: break;
}
TLibrary library;
return decode(opt) ? 0 : 1;
}
Configure Input Socket¶
The input socket defines the source HEVC elementary stream. The socket is created from the input file only. AVBlocks reads the stream headers and automatically detects the video parameters, including resolution, frame rate, and codec settings, when the transcoder is opened.
TMediaSocket()
.file(opt.inputFile)
Configure Output Socket¶
The output socket defines where and how the decoded raw YUV video data will be written. We configure it to output an uncompressed YUV 4:2:0 video file. The socket represents the output destination, while the pin represents the specific video stream within that destination. By setting ColorFormat::YUV420, we ensure the output uses the standard YUV 4:2:0 planar format, which is the most common format for raw video and provides a good balance between quality and file size.
TMediaSocket()
.file(opt.outputFile)
.streamType(StreamType::UncompressedVideo)
.addPin(
TMediaPin()
.streamInfo(TVideoStreamInfo()
.streamType(StreamType::UncompressedVideo)
.colorFormat(ColorFormat::YUV420)
)
)
Configure and Run Transcoder¶
This section shows how to set up the transcoder with the input and output sockets, then run the decoding operation. The allowDemoMode(true) call allows the transcoder to work even without a valid license (useful for testing, but not recommended for production). We add our input and output sockets to the transcoder, open it to prepare for processing, run the actual transcoding operation, and finally close it to clean up.
bool decode(const Options& opt)
{
try {
deleteFile(opt.outputFile.c_str());
TTranscoder()
.allowDemoMode(true)
.addInput(
TMediaSocket()
.file(opt.inputFile)
)
.addOutput(
TMediaSocket()
.file(opt.outputFile)
.streamType(StreamType::UncompressedVideo)
.addPin(
TMediaPin()
.streamInfo(TVideoStreamInfo()
.streamType(StreamType::UncompressedVideo)
.colorFormat(ColorFormat::YUV420)
)
)
)
.open()
.run()
.close();
std::println("Output: {}", opt.outputFile);
return true;
} catch (const TAVBlocksException& ex) {
std::println(stderr, "AVBlocks error: {}", ex.what());
return false;
} catch (const std::exception& ex) {
std::println(stderr, "Error: {}", ex.what());
return false;
}
}
Complete C++ Code¶
Here’s the complete working example that demonstrates HEVC/H.265 decoding using AVBlocks. This code combines all the previous snippets into a functional program that can be compiled and run. The main function handles command-line argument parsing, initializes AVBlocks, performs the decoding operation, and properly shuts down the library before exiting.
#include <primo/avblocks/avb++.h>
#include <print>
#include "options.h"
#include "util.h"
using namespace primo::codecs;
using namespace primo::avblocks::modern;
bool decode(const Options& opt)
{
try {
deleteFile(opt.outputFile.c_str());
TTranscoder()
.allowDemoMode(true)
.addInput(
TMediaSocket()
.file(opt.inputFile)
)
.addOutput(
TMediaSocket()
.file(opt.outputFile)
.streamType(StreamType::UncompressedVideo)
.addPin(
TMediaPin()
.streamInfo(TVideoStreamInfo()
.streamType(StreamType::UncompressedVideo)
.colorFormat(ColorFormat::YUV420)
)
)
)
.open()
.run()
.close();
std::println("Output: {}", opt.outputFile);
return true;
} catch (const TAVBlocksException& ex) {
std::println(stderr, "AVBlocks error: {}", ex.what());
return false;
} catch (const std::exception& ex) {
std::println(stderr, "Error: {}", ex.what());
return false;
}
}
int main(int argc, char* argv[])
{
Options opt;
switch(prepareOptions(opt, argc, argv))
{
case Command: return 0;
case Error: return 1;
case Parsed: break;
}
TLibrary library;
return decode(opt) ? 0 : 1;
}
How to Run¶
See the build instructions for macOS and the dec_hevc_file example for details.
Command Line¶
./dec_hevc_file --input <hevc file> --output <yuv file>
Examples¶
List options:
./bin/x64/dec_hevc_file --help
Usage: dec_hevc_file -i <h265 file> -o <yuv file>
-h, --help
--input H265 input file.
--output YUV output file.
The following example decodes an HEVC input file into raw YUV output:
mkdir -p ./output/dec_hevc_file
./bin/x64/dec_hevc_file \
--input ./assets/vid/foreman_qcif.h265 \
--output ./output/dec_hevc_file/foreman_qcif.yuv