Example 2 - 5.2 English - 68552

AOCL API Guide (68552)

Document ID
68552
Release Date
2025-12-29
Version
5.2 English

The following test program shows the sample usage and calling sequence of aocl - compression APIs to extract format compliant compressed stream from a stream produced by AOCL multi-threaded compressor :

Note

Build AOCL-Compression library with AOCL_ENABLE_THREADS.

 1#include <stdio.h> 
 2#include <stdlib.h>
 3#include "aocl_compression.h"
 4
 5int main(int argc, char** argv)
 6{
 7    aocl_compression_desc aocl_compression_ds;
 8    aocl_compression_desc* aocl_compression_handle = &aocl_compression_ds;
 9    FILE* inFp = NULL;
10    int file_size = 0;
11    char* inPtr = NULL, * compPtr = NULL, * decompPtr = NULL;
12    int64_t resultCompBound = 0, resultComp = 0, resultDecomp = 0;
13
14    printf("Running example_aocl_llc_skip_rap_frame\n");
15    printf("Demonstrates obtaining format-compliant compressed stream from a stream produced by AOCL multi-threaded compressor\n");
16    if (argc < 2)
17    {
18        printf("Provide input test file path\n");
19        return -1;
20    }
21    inFp = fopen(argv[1], "rb");
22    fseek(inFp, 0L, SEEK_END);
23    file_size = ftell(inFp);
24    rewind(inFp);
25
26    aocl_compression_type method = LZ4; // One of the compression methods as per aocl_compression_type
27    aocl_compression_handle->level = 0;
28    aocl_compression_handle->optVar = 0;
29    aocl_compression_handle->optOff = 0;
30    aocl_compression_handle->measureStats = 0;
31
32    // 1. setup and create a handle
33    if (aocl_llc_setup(aocl_compression_handle, method) != 0)
34    {
35        printf("Setup: failed\n");
36        goto error_exit;
37    }
38
39    // 2. allocate buffers
40    aocl_compression_handle->inSize = file_size;
41    resultCompBound = aocl_llc_compressBound(method, aocl_compression_handle->inSize);
42    if (resultCompBound < 0)
43    {
44        printf("CompressBound: failed\n");
45        goto error_exit;
46    }
47    aocl_compression_handle->outSize = resultCompBound;
48    inPtr = (char*)calloc(1, aocl_compression_handle->inSize);
49    compPtr = (char*)calloc(1, aocl_compression_handle->outSize);
50    decompPtr = (char*)calloc(1, aocl_compression_handle->inSize);
51    aocl_compression_handle->inBuf = inPtr;
52    aocl_compression_handle->outBuf = compPtr;
53    file_size = fread(inPtr, 1, file_size, inFp);
54
55
56    // 3. MT compress
57    resultComp = aocl_llc_compress(aocl_compression_handle, method);
58
59    if (resultComp <= 0)
60    {
61        printf("Compression: failed\n");
62        goto error_exit;
63    }
64    printf("Compression: done\n");
65
66    //4. ST decompress
67    // Get number of bytes for the RAP frame
68    int rap_frame_len = aocl_llc_skip_rap_frame((char*)compPtr, resultComp);
69
70    // Skip RAP frame in input stream and pass this to ST decompressor
71    aocl_compression_handle->inSize = resultComp - rap_frame_len;
72    aocl_compression_handle->outSize = file_size;
73    aocl_compression_handle->inBuf = compPtr + rap_frame_len;
74    aocl_compression_handle->outBuf = decompPtr;
75
76    // Pass format compliant stream to aocl decompressor (or any legacy ST decompressor)
77    resultDecomp = aocl_llc_decompress(aocl_compression_handle, method);
78
79    if (resultDecomp <= 0)
80    {
81        printf("Decompression Failure\n");
82        goto error_exit;
83    }
84    printf("Decompression: done\n");
85
86    // 5. destroy handle
87    aocl_llc_destroy(aocl_compression_handle, method);
88error_exit:
89    if (inPtr)
90        free(inPtr);
91    if (compPtr)
92        free(compPtr);
93    if (decompPtr)
94        free(decompPtr);
95    if (inFp)
96        fclose(inFp);
97    return 0;
98}

To build this example test program on a Linux system using GCC or AOCC, you must specify path to aocl_compression.h header file and link with libaocl_compression.so file as follows:

gcc test.c -I <aocl_compression.h file path> -L <libaocl_compression.so directory path> -laocl_compression -Wl,-rpath=<libaocl_compression.so directory path>

Before running the example program, ensure it points to the right library dependencies for openMP, etc.