Upscale Video¶
This article explains how to scale a 480p (854x480) video up to Full HD 1080p (1920x1080) video.
Source Video¶
For a source video we use the MP4 file from the TED talk video What’s the next window into our universe? by Andrew Connolly. The original video format is Wide 480p or 16:9, 854 x 480.
Sample Code¶
This code takes an MP4 file with H.264 video and AAC audio, and scales the video stream up to Full HD 1920x1080 (1080p) using bicubic method for interpolation. The audio stream is copied from the source as is. The input and output file paths are hardcoded in the source code, no command line parsing is used.
The snippets in this section are from the simple_video_upscale macOS sample.
Linux and Windows samples are also available:
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()
{
TLibrary library;
const char* inputFile = "AndrewConnolly_2014.mp4";
const char* outputFile = "AndrewConnolly_2014_1080p.mp4";
// transcoder will fail if output exists (by design)
std::remove(outputFile);
Configure Transcoder and Upscale¶
We probe the input file with a TMediaInfo object, and create an input socket from it. The output socket is created by cloning the input socket, so it starts out with an identical configuration, and then we set the output file path on it. The transcoder is the core component that performs the actual upscaling - it takes the 480p video from the input and resizes it to 1920x1080 using bicubic interpolation for the best upscaling quality.
The allowDemoMode(true) call allows the transcoder to work even without a valid license (useful for testing, but not recommended for production). We then add our input and output sockets to the transcoder, open it to prepare for processing, run the actual upscaling operation, and finally close it to clean up.
auto input = TMediaSocket(TMediaInfo().file(inputFile).open());
// Demo mode is allowed when using the demo version of the library.
TTranscoder()
.allowDemoMode(true)
.addInput(input)
// The output starts as a copy of the input
.addOutput(
input.clone()
.file(outputFile)
.videoInfoRef(0, [](TVideoStreamInfo& video) {
// Set the new frame width and height to Full HD 1920x1080
video.frameWidth(1920).frameHeight(1080);
})
.pinRef(0, [](TMediaPin& pin) {
// Set the resize interpolation method:
// InterpolationMethod::Cubic is best for upscaling (however, it is slow)
pin.addParam(Param::Video::Resize::InterpolationMethod, InterpolationMethod::Cubic);
})
)
.transcode();
Complete C++ Code¶
Here’s the complete working example that demonstrates video upscaling using AVBlocks. The input and output file paths are hardcoded, so no command line parsing is required.
#include <primo/avblocks/avb++.h>
#include <cstdio>
#include <print>
#include "../shim/shim23.h"
using namespace primo::codecs;
using namespace primo::avblocks;
using namespace primo::avblocks::modern;
int main()
{
TLibrary library;
const char* inputFile = "AndrewConnolly_2014.mp4";
const char* outputFile = "AndrewConnolly_2014_1080p.mp4";
// transcoder will fail if output exists (by design)
std::remove(outputFile);
try {
// Probe the input file and create the input socket from the probe
auto input = TMediaSocket(TMediaInfo().file(inputFile).open());
// Demo mode is allowed when using the demo version of the library.
TTranscoder()
.allowDemoMode(true)
.addInput(input)
// The output starts as a copy of the input
.addOutput(
input.clone()
.file(outputFile)
.videoInfoRef(0, [](TVideoStreamInfo& video) {
// Set the new frame width and height to Full HD 1920x1080
video.frameWidth(1920).frameHeight(1080);
})
.pinRef(0, [](TMediaPin& pin) {
// Set the resize interpolation method:
// InterpolationMethod::Cubic is best for upscaling (however, it is slow)
pin.addParam(Param::Video::Resize::InterpolationMethod, InterpolationMethod::Cubic);
})
)
.transcode();
std::println("Output: {}", outputFile);
return 0;
} catch (const TAVBlocksException& ex) {
std::println(stderr, "AVBlocks error: {}", ex.what());
return 1;
}
}
How to run¶
See the build instructions for macOS and the simple_video_upscale example for details.
Download the sample video, the AndrewConnolly_2014.mp4 file from the Internet Archive:
cd samples/darwin/simple_video_upscale
curl -L -o AndrewConnolly_2014.mp4 \
https://archive.org/download/AndrewConnolly_2014/AndrewConnolly_2014.mp4
Run the sample from the sample directory - the input and output file paths are relative to the working directory:
../../../bin/arm64/simple_video_upscale # or ../../../bin/x64/simple_video_upscale
The upscaled output file AndrewConnolly_2014_1080p.mp4 will be created in the samples/darwin/simple_video_upscale directory.