Home

Awesome

GPUJPEG

License C/C++ CI

GPUJPEG Logo

JPEG encoder and decoder library and console application for NVIDIA GPUs for high-performance image encoding and decoding. The software runs also on AMD GPUs using ZLUDA (see ZLUDA.md).

This documents provides an introduction to the library and how to use it. You can also look to FAQ.md for additional information. To see latest changes you can display file NEWS.md.

Table of contents

Authors

Features

Overview

Encoding/Decoding of JPEG codestream is divided into following phases:

 Encoding:                       Decoding
 1) Input data loading           1) Input data loading
 2) Preprocessing                2) Parsing codestream
 3) Forward DCT                  3) Huffman decoder
 4) Huffman encoder              4) Inverse DCT
 5) Formatting codestream        5) Postprocessing

and they are implemented on CPU or/and GPU as follows:

Performance

Source 16K (DCI) image (8, 9) was cropped to 15360x8640+0+0 (1920x1080 multiplied by 8 in both dimensions) and for lower resolutions downscaled. Encoding was done with default values with input in RGB (quality 75, non-interleaved, rst 24-36, average from 99 measurements excluding first iteration) with following command:

gpujpegtool -v -e mediadivision_frame_<res>.pnm mediadivision_frame_<res>.jpg -n 100 [-q <Q>]

Encoding

GPU \ resolutionHD (2 Mpix)4K (8 Mpix)8K (33 Mpix)16K (132 Mpix)
GTX 30800.54 ms1.71 ms6.20 ms24.48 ms
GTX 2080 Ti0.82 ms2.89 ms11.15 ms46.23 ms
GTX 1060M1.36 ms4.55 ms17.34 ms(low mem)
GTX 5802.38 ms8.68 ms(low mem)(low mem)
AMD Radeon RX 7600 [ZLUDA]0.88 ms3.16 ms13.09 ms50.52 ms

Note: First iteration took 233 ms for 8K on GTX 3080 and scales proportionally with respect to resolution.

Further measurements were performed on GTX 3080 only:

quality102030405060708090100
duration HD (ms)0.480.490.500.510.510.530.540.570.600.82
duration 4K (ms)1.611.651.661.671.691.681.701.721.792.44
duration 8K (ms)6.026.046.096.146.126.176.216.246.478.56
duration 8K (ms, w/o PCIe xfers)2.132.142.182.242.232.252.282.332.505.01
<!-- Additional notes (applies also for decode): 1. device needs to be set to maximum performance, otherwise powermanagement influences esp. PCIe transmits 2. stream formatter is starting to be a significant performance factor, eg. 0.82 ms for 8K Q=75 (contained in last line) 3. measurements were done without -DCMAKE_BUILD_TYPE=Release, should be measured with -->

Decoding

Decoded images were those encoded in previous section, averaging has been done similarly by taking 99 samples excluding the first one. Command used:

gpujpegtool -v mediavision_frame_<res>.jpg output.pnm -n 100
GPU \ resolutionHD (2 Mpix)4K (8 Mpix)8K (33 Mpix)16K (132 Mpix)
GTX 30800.75 ms1.94 ms6.76 ms31.50 ms
GTX 2080 Ti1.02 ms1.07 ms11.29 ms44.42 ms
GTX 1060M1.68 ms4.81 ms17.56 ms(low mem)
GTX 5802.61 ms7.96 ms(low mem)(low mem)
AMD Radeon RX 7600 [ZLUDA]1.00 ms3.02 ms11.25 ms45.06 ms

Note: (low mem) above means that the card didn't have sufficient memory to encode or decode the picture.

Following measurements were performed on GTX 3080 only:

quality102030405060708090100
duration HD (ms)0.580.600.630.650.670.690.730.780.891.58
duration 4K (ms)1.771.801.831.841.871.891.921.952.113.69
duration 8K (ms)6.856.886.906.926.986.706.746.847.1712.43
duration 8K (ms, w/o PCIe xfers)2.142.182.212.242.272.292.342.422.717.27

Quality

Following tables summarizes encoding quality and file size using NVIDIA GTX 580 for non-interleaved and non-subsampled stream with different quality settings (PSNR and encoded size values are averages of encoding several images, each of them multiple times):

qualityPSNR 4K¹size 4KPSNR HD²size HD
1029.33 dB539.30 kB27.41 dB145.90 kB
2032.70 dB697.20 kB30.32 dB198.30 kB
3034.63 dB850.60 kB31.92 dB243.60 kB
4035.97 dB958.90 kB32.99 dB282.20 kB
5036.94 dB1073.30 kB33.82 dB319.10 kB
6037.96 dB1217.10 kB34.65 dB360.00 kB
7039.22 dB1399.20 kB35.71 dB422.10 kB
8040.67 dB1710.00 kB37.15 dB526.70 kB
9042.83 dB2441.40 kB39.84 dB768.40 kB
10047.09 dB7798.70 kB47.21 dB2499.60 kB

<b><sup>1,2</sup></b> sizes 4096x2160 and 1920x1080

Compile

To build console application check Requirements and go to gpujpeg directory (where README.md and COPYING files are placed) and run cmake command:

cmake -DCMAKE_BUILD_TYPE=Release -Bbuild .
cmake --build build

You can also use autotools to create a build recipe for the library and the application or a plain old Makefile.bkp. However, cmake is recommended.

Usage

libgpujpeg library

To build libgpujpeg library check Compile.

To use library in your project you have to include library to your sources and linked shared library object to your executable:

#include <libgpujpeg/gpujpeg.h>

For simple library usage examples you look into subdirectory examples.

Encoding

For encoding by libgpujpeg library you have to declare two structures and set proper values to them. The first is definition of encoding/decoding parameters, and the second is structure with parameters of input image:

struct gpujpeg_parameters param = gpujpeg_default_parameters();
// you can adjust parameters:
param.quality = 80; // (default value is 75)

struct gpujpeg_image_parameters param_image = gpujpeg_default_image_parameter();
param_image.width = 1920;
param_image.height = 1080;
param_image.color_space = GPUJPEG_RGB; // input colorspace (GPUJPEG_RGB
                                       // default), can be also
                                       // eg. GPUJPEG_YCBCR_JPEG
param_image.pixel_format = GPUJPEG_444_U8_P012;
// or eg. GPUJPEG_U8 for grayscale
// (default value is GPUJPEG_444_U8_P012)

If you want to use subsampling in JPEG format call following function, that will set default sampling factors (2x2 for Y, 1x1 for Cb and Cr):

// Use 4:2:0 subsampling
gpujpeg_parameters_chroma_subsampling(&param, GPUJPEG_SUBSAMPLING_420);

Or define sampling factors by hand:

// User custom sampling factors
gpujpeg_parameters_chroma_subsampling(&param, MK_SUBSAMPLING(4, 4, 1, 2, 2, 1, 0, 0));

Next you can initialize CUDA device by calling (if not called, default CUDA device will be used):

if ( gpujpeg_init_device(device_id, 0) )
    return -1;

where first parameters is CUDA device (e.g. device_id = 0) id and second parameter is flag if verbose output should be used (0 or GPUJPEG_VERBOSE). Next step is to create encoder:

struct gpujpeg_encoder* encoder = gpujpeg_encoder_create(0);
if ( encoder == NULL )
    return -1;

When creating encoder, library allocates all device buffers which will be needed for image encoding and when you encode concrete image, they are already allocated and encoder will used them for every image. Now we need raw image data that we can encode by encoder, for example we can load it from file:

size_t image_size = 0;
uint8_t* input_image = NULL;
if ( gpujpeg_image_load_from_file("input_image.rgb", &input_image,
         &image_size) != 0 )
    return -1;

Next step is to encode uncompressed image data to JPEG compressed data by encoder:

struct gpujpeg_encoder_input encoder_input;
gpujpeg_encoder_input_set_image(&encoder_input, input_image);

uint8_t* image_compressed = NULL;
int image_compressed_size = 0;
if ( gpujpeg_encoder_encode(encoder, &encoder_input, &image_compressed,
         &image_compressed_size) != 0 )
    return -1;

Compressed data are placed in internal encoder buffer so we have to save them somewhere else before we start encoding next image, for example we can save them to file:

if ( gpujpeg_image_save_to_file("output_image.jpg", image_compressed,
         image_compressed_size, NULL) != 0 )
    return -1;

Now we can load, encode and save next image or finish and move to clean up encoder. Finally we have to clean up so destroy loaded image and destroy the encoder.

gpujpeg_image_destroy(input_image);
gpujpeg_encoder_destroy(encoder);

Decoding

For decoding we don't need to initialize two structures of parameters. We only have to initialize CUDA device if we haven't initialized it yet and create decoder:

if ( gpujpeg_init_device(device_id, 0) )
    return -1;

struct gpujpeg_decoder* decoder = gpujpeg_decoder_create(0);
if ( decoder == NULL )
    return -1;

Now we have two options. The first is to do nothing and decoder will postpone buffer allocations to decoding first image where it determines proper image size and all other parameters (recommended). The second option is to provide input image size and other parameters (reset interval, interleaving) and the decoder will allocate all buffers and it is fully ready when encoding even the first image:

// you can skip this code below and let the decoder initialize automatically
struct gpujpeg_parameters param;
gpujpeg_set_default_parameters(&param);
param.restart_interval = 16;
param.interleaved = 1;

struct gpujpeg_image_parameters param_image;
gpujpeg_image_set_default_parameters(&param_image);
param_image.width = 1920;
param_image.height = 1080;
param_image.color_space = GPUJPEG_RGB;
param_image.pixel_format = GPUJPEG_444_U8_P012;

// Pre initialize decoder before decoding
gpujpeg_decoder_init(decoder, &param, &param_image);

If you didn't initialize the decoder by gpujpeg_decoder_init but want to specify output image color space and subsampling factor, you can use following code:

gpujpeg_decoder_set_output_format(decoder, GPUJPEG_RGB,
                GPUJPEG_444_U8_P012);
// or eg. GPUJPEG_YCBCR_JPEG and GPUJPEG_422_U8_P1020

If not called, RGB or grayscale is output depending on JPEG channel count.

Next we have to load JPEG image data from file and decoded it to raw image data:

size_t image_size = 0;
uint8_t* image = NULL;
if ( gpujpeg_image_load_from_file("input_image.jpg", &image,
         &image_size) != 0 )
    return -1;

struct gpujpeg_decoder_output decoder_output;
gpujpeg_decoder_output_set_default(&decoder_output);
if ( gpujpeg_decoder_decode(decoder, image, image_size,
         &decoder_output) != 0 )
    return -1;

Now we can save decoded raw image data to file and perform cleanup:

if ( gpujpeg_image_save_to_file("output_image.pnm", decoder_output.data,
         decoder_output.data_size, &decoder_output.param_image) != 0 )
    return -1;

gpujpeg_image_destroy(image);
gpujpeg_decoder_destroy(decoder);

GPUJPEG console application

The console application gpujpeg uses libgpujpeg library to demonstrate it's functions. To build console application check Compile.

To encode image from raw RGB image file to JPEG image file use following command:

gpujpegtool --encode --size=WIDTHxHEIGHT --quality=QUALITY \
        INPUT_IMAGE.rgb OUTPUT_IMAGE.jpg

You must specify input image size by --size=WIDTHxHEIGHT parameter. Optionally you can specify desired output quality by parameter --quality=QUALITY which accepts values 0-100. Console application accepts a few more parameters and you can list them by folling command:

gpujpegtool --help

To decode image from JPEG image file to raw RGB image file use following command:

gpujpegtool --decode OUTPUT_IMAGE.jpg INPUT_IMAGE.rgb

You can also encode and decode image to test the console application:

gpujpegtool --encode --decode --size=WIDTHxHEIGHT --quality=QUALITY \
        INPUT_IMAGE.rgb OUTPUT_IMAGE.jpg

Decoder will create new decoded file OUTPUT_IMAGE.jpg.decoded.rgb and do not overwrite your INPUT_IMAGE.rgb file.

Console application is able to load raw RGB image file data from *.rgb files and raw YUV and YUV422 data from *.yuv files. For YUV422 you must specify *.yuv file and use --sampling-factor=4:2:2 parameter.

All supported parameters for console application are following:

--help
    Prints console application help
--size=1920x1080
    Input image size in pixels, e.g. 1920x1080
--pixel-format=444-u8-p012
    Input/output image pixel format ('u8', '444-u8-p012', '444-u8-p012z',
    '444-u8-p0p1p2', '422-u8-p1020', '422-u8-p0p1p2' or '420-u8-p0p1p2')
--colorspace=rgb
    Input image colorspace (supported are 'rgb', 'yuv' and 'ycbcr-jpeg',
    where 'yuv' means YCbCr ITU-R BT.601), when *.yuv file is specified,
    instead of default 'rgb', automatically the colorspace 'yuv' is used
--quality
    Set output quality level 0-100 (default 75)
--restart=8
    Set restart interval for encoder, number of MCUs between
    restart markers
--subsampled
    Produce chroma subsampled JPEG stream
--interleaved
    Produce interleaved stream
--encode
    Encode images
--decode
    Decode images
--device=0
    By using this parameter you can specify CUDA device id which will
    be used for encoding/decoding.

Restart interval is important for parallel huffman encoding and decoding. When --restart=N is used (default is 8), the coder can process each N MCUs independently, and so he can code each N MCUs in parallel. When --restart=0 is specified, restart interval is disabled and the coder must use CPU version of huffman coder (because on GPU would run only one thread, which is very slow).

The console application can encode/decode multiple images by following command:

gpujpegtool ARGUMENTS INPUT_IMAGE_1.rgb OUTPUT_IMAGE_1.jpg \
        INPUT_IMAGE_2.rgb OUTPUT_IMAGE_2.jpg ...

Requirements

To be able to build and run libgpujpeg library and gpujpeg console application you need:

  1. NVIDIA CUDA Toolkit
  2. C/C++ compiler + CMake
  3. CUDA enabled NVIDIA GPU (cc >= 2.0; older may or may not work) with NVIDIA drivers or AMD with ZLUDA (see ZLUDA.md)
  4. optional OpenGL support:
    • GLEW, OpenGL (usually present in Windows, may need headers installation in Linux)
    • GLFW or GLX (Linux only) for context creation
    • GLUT for OpenGL tests

License

References

  1. ITU-T Rec T.81
  2. ILG
  3. SiGenGPU (currently defunct)
  4. ECMA TR/098 (JFIF)
  5. ITU-T Rec T.84 (SPIFF)
  6. SPIFF File Format Summary (FileFormat.Info)
  7. Component ID registration