AVC / H.264 Encoder (Pull)

This article explains how you can use {transcoder-pull-net} to encode a raw YUV video file to an AVC / H.264 Annex B elementary stream.

The code snippets in this article are from the enc_avc_pull .NET sample.

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 AVC / H.264 Annex B format using the pull method. The encoded samples are pulled from the transcoder and written to the output file by the application.

Initialize AVBlocks

Initialize the AVBlocks library before creating the transcoder, and shut it down after encoding is complete.

static int Main(string[] args)
{
    var opt = new Options();

    if (!opt.Prepare(args))
        return opt.Error ? (int)ExitCodes.OptionsError : (int)ExitCodes.Success;

    Library.Initialize();

    // Set license information. To run AVBlocks in demo mode, comment the next line out
    // Library.SetLicense("<license-string>");

    bool encodeResult = Encode(opt);

    Library.Shutdown();

    return encodeResult ? (int)ExitCodes.Success : (int)ExitCodes.EncodingError;
}

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.

static MediaSocket CreateInputSocket(Options opt)
{
    MediaSocket socket = new MediaSocket();
    socket.StreamType = StreamType.UncompressedVideo;
    socket.File = opt.InputFile;

    MediaPin pin = new MediaPin();
    socket.Pins.Add(pin);
    VideoStreamInfo vsi = new VideoStreamInfo();
    pin.StreamInfo = vsi;

    vsi.StreamType = StreamType.UncompressedVideo;
    vsi.ScanType = ScanType.Progressive;

    vsi.FrameWidth = opt.Width;
    vsi.FrameHeight = opt.Height;
    vsi.ColorFormat = opt.Color.Id;
    vsi.FrameRate = opt.Fps;

    return socket;
}

Configure Output Socket

The output socket describes AVC / H.264 Annex B output. The pull sample does not set a file on the output socket because the application writes pulled samples to the output stream.

static MediaSocket CreateOutputSocket(Options opt)
{
    MediaSocket socket = new MediaSocket();
    socket.StreamType = StreamType.H264;
    socket.StreamSubType = StreamSubType.AvcAnnexB;

    MediaPin pin = new MediaPin();
    socket.Pins.Add(pin);
    VideoStreamInfo vsi = new VideoStreamInfo();
    pin.StreamInfo = vsi;

    vsi.StreamType = StreamType.H264;
    vsi.StreamSubType = StreamSubType.AvcAnnexB;

    return socket;
}

Configure Transcoder and Pull Encoded Samples

After creating the input and output sockets, the sample creates a transcoder, enables demo mode, opens it, pulls encoded samples, writes each sample buffer to the output file, and checks for end-of-stream.

static bool Encode(Options opt)
{
    DeleteFile(opt.OutputFile);

    // Create output directory if needed
    string outputDir = Path.GetDirectoryName(opt.OutputFile);
    if (!string.IsNullOrEmpty(outputDir))
        Directory.CreateDirectory(outputDir);

    using (FileStream outfile = File.Create(opt.OutputFile))
    {
        MediaSocket inSocket = CreateInputSocket(opt);
        MediaSocket outSocket = CreateOutputSocket(opt);

        // create Transcoder
        using (Transcoder transcoder = new Transcoder())
        {
            transcoder.AllowDemoMode = true;
            transcoder.Inputs.Add(inSocket);
            transcoder.Outputs.Add(outSocket);

            if (!transcoder.Open())
            {
                PrintError("Transcoder open", transcoder.Error);
                return false;
            }

            // encode by pulling encoded samples
            int outputIndex = 0;
            MediaSample sample = new MediaSample();

            while (transcoder.Pull(out outputIndex, sample))
            {
                outfile.Write(sample.Buffer.Start, sample.Buffer.DataOffset, sample.Buffer.DataSize);
            }

            ErrorInfo error = transcoder.Error;
            PrintError("Transcoder pull", error);

            bool success = false;
            if (error.Facility == ErrorFacility.Codec &&
                error.Code == (int)CodecError.EOS)
            {
                // ok
                success = true;
            }

            transcoder.Close();

            return success;
        }
    }
}

Complete Code

Here’s the complete working example that demonstrates AVC / H.264 encoding using the pull method with AVBlocks for .NET.

using System;
using System.IO;
using PrimoSoftware.AVBlocks;

namespace CliSample
{
    class Program
    {
        static int Main(string[] args)
        {
            var opt = new Options();

            if (!opt.Prepare(args))
                return opt.Error ? (int)ExitCodes.OptionsError : (int)ExitCodes.Success;

            Library.Initialize();

            // Set license information. To run AVBlocks in demo mode, comment the next line out
            // Library.SetLicense("<license-string>");

            bool encodeResult = Encode(opt);

            Library.Shutdown();

            return encodeResult ? (int)ExitCodes.Success : (int)ExitCodes.EncodingError;
        }

        static bool Encode(Options opt)
        {
            DeleteFile(opt.OutputFile);

            // Create output directory if needed
            string outputDir = Path.GetDirectoryName(opt.OutputFile);
            if (!string.IsNullOrEmpty(outputDir))
                Directory.CreateDirectory(outputDir);

            using (FileStream outfile = File.Create(opt.OutputFile))
            {
                MediaSocket inSocket = CreateInputSocket(opt);
                MediaSocket outSocket = CreateOutputSocket(opt);

                // create Transcoder
                using (Transcoder transcoder = new Transcoder())
                {
                    transcoder.AllowDemoMode = true;
                    transcoder.Inputs.Add(inSocket);
                    transcoder.Outputs.Add(outSocket);

                    if (!transcoder.Open())
                    {
                        PrintError("Transcoder open", transcoder.Error);
                        return false;
                    }

                    // encode by pulling encoded samples
                    int outputIndex = 0;
                    MediaSample sample = new MediaSample();

                    while (transcoder.Pull(out outputIndex, sample))
                    {
                        outfile.Write(sample.Buffer.Start, sample.Buffer.DataOffset, sample.Buffer.DataSize);
                    }

                    ErrorInfo error = transcoder.Error;
                    PrintError("Transcoder pull", error);

                    bool success = false;
                    if (error.Facility == ErrorFacility.Codec &&
                        error.Code == (int)CodecError.EOS)
                    {
                        // ok
                        success = true;
                    }

                    transcoder.Close();

                    return success;
                }
            }
        }

        static void DeleteFile(string filename)
        {
            try
            {
                if (File.Exists(filename))
                    File.Delete(filename);
            }
            catch { }
        }

        static MediaSocket CreateInputSocket(Options opt)
        {
            MediaSocket socket = new MediaSocket();
            socket.StreamType = StreamType.UncompressedVideo;
            socket.File = opt.InputFile;

            MediaPin pin = new MediaPin();
            socket.Pins.Add(pin);
            VideoStreamInfo vsi = new VideoStreamInfo();
            pin.StreamInfo = vsi;

            vsi.StreamType = StreamType.UncompressedVideo;
            vsi.ScanType = ScanType.Progressive;

            vsi.FrameWidth = opt.Width;
            vsi.FrameHeight = opt.Height;
            vsi.ColorFormat = opt.Color.Id;
            vsi.FrameRate = opt.Fps;

            return socket;
        }

        static MediaSocket CreateOutputSocket(Options opt)
        {
            MediaSocket socket = new MediaSocket();
            socket.StreamType = StreamType.H264;
            socket.StreamSubType = StreamSubType.AvcAnnexB;

            MediaPin pin = new MediaPin();
            socket.Pins.Add(pin);
            VideoStreamInfo vsi = new VideoStreamInfo();
            pin.StreamInfo = vsi;

            vsi.StreamType = StreamType.H264;
            vsi.StreamSubType = StreamSubType.AvcAnnexB;

            return socket;
        }

        static void PrintError(string action, ErrorInfo e)
        {
            if (action != null)
            {
                Console.Write("{0}: ", action);
            }

            if (ErrorFacility.Success == e.Facility)
            {
                Console.WriteLine("Success");
                return;
            }
            else
            {
                Console.WriteLine("{0}, facility:{1} code:{2} hint:{3}", e.Message ?? "", e.Facility, e.Code, e.Hint ?? "");
            }
        }

        enum ExitCodes : int
        {
            Success = 0,
            OptionsError = 1,
            EncodingError = 2,
        }
    }
}

How to Run

See the build instructions for macOS and the enc_avc_pull .NET sample for details.

Command Line

enc_avc_pull --input <file.yuv> --output <file.h264>

Examples

List options:

./bin/net10.0/enc_avc_pull --help
Usage: enc_avc_pull --input <file.yuv> --output <file.h264>
  -i,    --input    input YUV file
  -o,    --output   output AVC / H.264 file
  -r,    --rate     input frame rate
  -f,    --frame    input frame size
  -c,    --color    input color space
  --colors          list supported input color spaces
  --help            Display this help screen.