test_encode.c 2.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <inttypes.h>
#include "libae.h"

#define CHUNK_OUT 0x4000
#define CHUNK_IN 1024
#define ALL_IN 9478

int main(int argc, char *argv[])
{
    ae_stream strm;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
14
    uint8_t *in;
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    uint8_t *out;
    int chunk_in, chunk_out, i, c, total_out, status;
    int input_avail, output_avail;

    if (argc == 3)
    {
        chunk_in = atoi(argv[1]);
        chunk_out = atoi(argv[2]);
    }
    else
    {
        chunk_in = CHUNK_IN;
        chunk_out = CHUNK_OUT;
    }

    out = (uint8_t *)malloc(chunk_out);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
31
    in = (uint8_t *)malloc(chunk_in * sizeof(uint8_t));
32 33 34
    if (in == NULL || out == NULL)
        return 1;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
35
    strm.bit_per_sample = 8;
36 37
    strm.block_size = 8;
    strm.segment_size = 2;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
38
    strm.flags = AE_DATA_UNSIGNED | AE_DATA_PREPROCESS;
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

    if (ae_encode_init(&strm) != AE_OK)
        return 1;

    total_out = 0;
    strm.avail_in = 0;
    strm.avail_out = chunk_out;
    strm.next_out = out;

    input_avail = 1;
    output_avail = 1;

    while(input_avail || output_avail)
    {
        if (strm.avail_in == 0)
        {
            i = 0;
            while(i < chunk_in && (c = getc(stdin)) != EOF)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
57 58 59 60 61 62 63
            {
                in[i] = c;
                /* in[i] |= getc(stdin) << 8; */
                /* in[i] |= getc(stdin) << 16; */
                /* in[i] |= getc(stdin) << 24; */
                i++;
            }
64 65
            strm.avail_in = i;

66
            strm.next_in = (uint8_t *)in;
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
            if (c == EOF)
                input_avail = 0;
        }

        if ((status = ae_encode(&strm, AE_NO_FLUSH)) != AE_OK)
        {
            fprintf(stderr, "error is %i\n", status);
            return 1;
        }

        if (strm.total_out - total_out > 0)
        {
            for (i=0; i < strm.total_out - total_out; i++)
            {
                putc(out[i], stdout);
            }
            total_out = strm.total_out;
            output_avail = 1;
            strm.next_out = out;
            strm.avail_out = chunk_out;
        }
        else
        {
            output_avail = 0;
        }

    }

    if ((status = ae_encode(&strm, AE_FLUSH)) != AE_OK)
    {
        fprintf(stderr, "error is %i\n", status);
        return 1;
    }

    if (strm.total_out - total_out > 0)
    {
        for (i=0; i < strm.total_out - total_out; i++)
        {
            putc(out[i], stdout);
        }
    }
    return 0;
}