VP9 Decoder (Run)

This article explains how to use TTranscoder::run to decode VP9 video in an IVF container to a raw YUV video file.

The code snippets in this article are from the dec_vp9_file macOS sample.

Linux and Windows samples are also available:

Source Video

For source we use the foreman_qcif_vp9.ivf file from the AVBlocks Assets repository. After downloading and unzipping you will find foreman_qcif_vp9.ivf in the vid subdirectory.

Code

This code takes a compressed VP9 video file in an IVF container and decodes it to raw YUV 4:2:0 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 object shuts the library down and cleans up resources when it goes out of scope at the end of the program.

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 is created from the VP9/IVF input file. AVBlocks detects the encoded video stream parameters automatically.

TMediaSocket()
    .file(opt.inputFile)

Configure Output Socket

The output socket writes uncompressed video to the requested YUV file. The sample configures the output video stream as StreamType::UncompressedVideo with ColorFormat::YUV420.

TMediaSocket()
    .file(opt.outputFile)
    .streamType(StreamType::UncompressedVideo)
    .addPin(
        TMediaPin()
            .streamInfo(TVideoStreamInfo()
                .streamType(StreamType::UncompressedVideo)
                .colorFormat(ColorFormat::YUV420)
            )
    )

Configure Transcoder and Decode

After building the input socket and the YUV output socket, the sample creates a transcoder, enables demo mode, adds the sockets, opens the transcoder, runs the decode, and closes it.

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 from dec_vp9_file.cpp:

#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_vp9_file example for details.

Command Line

./dec_vp9_file --input <file> --output <yuv_file>

Examples

List options:

./bin/x64/dec_vp9_file --help
Usage: dec_vp9_file -i <ivf file> -o <yuv file>
  -h,    --help
         --input    VP9/IVF input file.
         --output   YUV output file.

The following command decodes a VP9 video from assets/vid/foreman_qcif_vp9.ivf to output/dec_vp9_file/foreman_qcif.yuv:

mkdir -p ./output/dec_vp9_file

./bin/x64/dec_vp9_file \
    --input ./assets/vid/foreman_qcif_vp9.ivf \
    --output ./output/dec_vp9_file/foreman_qcif.yuv