VP8 Encoder (Run)

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

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

Linux and Windows samples are also available:

Source Video

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

Code

This code takes a raw YUV video file and encodes it to VP8 video in an IVF container.

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 encode(opt) ? 0 : 1;
}

Configure Input Socket

The input socket describes the raw YUV source file. The frame size, frame rate, and color format must match the source video.

TMediaSocket()
    .file(opt.yuv_file)
    .streamType(StreamType::UncompressedVideo)
    .addPin(
        TMediaPin()
            .streamInfo(TVideoStreamInfo()
                .streamType(StreamType::UncompressedVideo)
                .frameWidth(opt.frame_size.width_)
                .frameHeight(opt.frame_size.height_)
                .colorFormat(opt.yuv_color.Id)
                .frameRate(opt.fps)
                .scanType(ScanType::Progressive)
            )
    )

Configure Output Socket

The output socket writes VP8 video in an IVF container. The socket stream type is StreamType::IVF, and the output pin stream type is StreamType::VP8.

TMediaSocket()
    .file(opt.ivf_file)
    .streamType(StreamType::IVF)
    .addPin(
        TMediaPin()
            .streamInfo(TVideoStreamInfo()
                .streamType(StreamType::VP8)
            )
    )

Configure Transcoder and Encode

After creating the raw YUV input socket and IVF output socket, the sample creates a transcoder, enables demo mode, adds the sockets, removes any existing output file, opens the transcoder, runs the encode, and closes it.

bool encode(const Options& opt)
{
    try {
        deleteFile(opt.ivf_file.c_str());

        TTranscoder()
            .allowDemoMode(true)
            .addInput(
                TMediaSocket()
                    .file(opt.yuv_file)
                    .streamType(StreamType::UncompressedVideo)
                    .addPin(
                        TMediaPin()
                            .streamInfo(TVideoStreamInfo()
                                .streamType(StreamType::UncompressedVideo)
                                .frameWidth(opt.frame_size.width_)
                                .frameHeight(opt.frame_size.height_)
                                .colorFormat(opt.yuv_color.Id)
                                .frameRate(opt.fps)
                                .scanType(ScanType::Progressive)
                            )
                    )
            )
            .addOutput(
                TMediaSocket()
                    .file(opt.ivf_file)
                    .streamType(StreamType::IVF)
                    .addPin(
                        TMediaPin()
                            .streamInfo(TVideoStreamInfo()
                                .streamType(StreamType::VP8)
                            )
                    )
            )
            .open()
            .run()
            .close();

        std::println("Output: {}", opt.ivf_file);
        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 enc_vp8_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 encode(const Options& opt)
{
    try {
        deleteFile(opt.ivf_file.c_str());

        TTranscoder()
            .allowDemoMode(true)
            .addInput(
                TMediaSocket()
                    .file(opt.yuv_file)
                    .streamType(StreamType::UncompressedVideo)
                    .addPin(
                        TMediaPin()
                            .streamInfo(TVideoStreamInfo()
                                .streamType(StreamType::UncompressedVideo)
                                .frameWidth(opt.frame_size.width_)
                                .frameHeight(opt.frame_size.height_)
                                .colorFormat(opt.yuv_color.Id)
                                .frameRate(opt.fps)
                                .scanType(ScanType::Progressive)
                            )
                    )
            )
            .addOutput(
                TMediaSocket()
                    .file(opt.ivf_file)
                    .streamType(StreamType::IVF)
                    .addPin(
                        TMediaPin()
                            .streamInfo(TVideoStreamInfo()
                                .streamType(StreamType::VP8)
                            )
                    )
            )
            .open()
            .run()
            .close();

        std::println("Output: {}", opt.ivf_file);
        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 encode(opt) ? 0 : 1;
}

How to Run

See the build instructions for macOS and the enc_vp8_file example for details.

Command Line

./enc_vp8_file --frame <width>x<height> --rate <fps> --color <COLOR> --input <file.yuv> --output <file.ivf> [--colors] [--help]

Examples

List options:

./bin/x64/enc_vp8_file --help
enc_vp8_file --frame <width>x<height> --rate <fps> --color <COLOR> --input <file.yuv> --output <file.ivf> [--colors]
  -h,    --help
  -i,    --input    input YUV file
  -o,    --output   output IVF file
  -r,    --rate     input frame rate
  -f,    --frame    input frame sizes <width>x<height>
  -c,    --color    input color format. Use --colors to list all supported color
                    formats
         --colors   list COLOR constants

Encode a raw YUV video from ./assets/vid/foreman_qcif.yuv to a VP8 video in ./output/enc_vp8_file/foreman_qcif.ivf:

mkdir -p ./output/enc_vp8_file

./bin/x64/enc_vp8_file \
  --input ./assets/vid/foreman_qcif.yuv \
  --output ./output/enc_vp8_file/foreman_qcif.ivf \
  --frame 176x144 \
  --rate 30 \
  --color yuv420