summaryrefslogtreecommitdiff
path: root/gr-vocoder/lib/g7xx/decode.c
blob: bd6d98edebcade31efa23acb052e2cee9beebfc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
/*
 * decode.c
 *
 * CCITT ADPCM decoder
 *
 * Usage : decode [-3|4|5] [-a|u|l] < infile > outfile
 */
#include "g72x.h"
#include <stdio.h>


/*
 * Unpack input codes and pass them back as bytes.
 * Returns 1 if there is residual input, returns -1 if eof, else returns 0.
 */
int unpack_input(unsigned char* code, int bits)
{
    static unsigned int in_buffer = 0;
    static int in_bits = 0;
    unsigned char in_byte;

    if (in_bits < bits) {
        if (fread(&in_byte, sizeof(char), 1, stdin) != 1) {
            *code = 0;
            return (-1);
        }
        in_buffer |= (in_byte << in_bits);
        in_bits += 8;
    }
    *code = in_buffer & ((1 << bits) - 1);
    in_buffer >>= bits;
    in_bits -= bits;
    return (in_bits > 0);
}


main(int argc, char** argv)
{
    short sample;
    unsigned char code;
    int n;
    struct g72x_state state;
    int out_coding;
    int out_size;
    int (*dec_routine)();
    int dec_bits;

    g72x_init_state(&state);
    out_coding = AUDIO_ENCODING_ULAW;
    out_size = sizeof(char);
    dec_routine = g721_decoder;
    dec_bits = 4;

    /* Process encoding argument, if any */
    while ((argc > 1) && (argv[1][0] == '-')) {
        switch (argv[1][1]) {
        case '3':
            dec_routine = g723_24_decoder;
            dec_bits = 3;
            break;
        case '4':
            dec_routine = g721_decoder;
            dec_bits = 4;
            break;
        case '5':
            dec_routine = g723_40_decoder;
            dec_bits = 5;
            break;
        case 'u':
            out_coding = AUDIO_ENCODING_ULAW;
            out_size = sizeof(char);
            break;
        case 'a':
            out_coding = AUDIO_ENCODING_ALAW;
            out_size = sizeof(char);
            break;
        case 'l':
            out_coding = AUDIO_ENCODING_LINEAR;
            out_size = sizeof(short);
            break;
        default:
            fprintf(stderr, "CCITT ADPCM Decoder -- usage:\n");
            fprintf(stderr, "\tdecode [-3|4|5] [-a|u|l] < infile > outfile\n");
            fprintf(stderr, "where:\n");
            fprintf(stderr, "\t-3\tProcess G.723 24kbps (3-bit) input data\n");
            fprintf(stderr, "\t-4\tProcess G.721 32kbps (4-bit) input data [default]\n");
            fprintf(stderr, "\t-5\tProcess G.723 40kbps (5-bit) input data\n");
            fprintf(stderr, "\t-a\tGenerate 8-bit A-law data\n");
            fprintf(stderr, "\t-u\tGenerate 8-bit u-law data [default]\n");
            fprintf(stderr, "\t-l\tGenerate 16-bit linear PCM data\n");
            exit(1);
        }
        argc--;
        argv++;
    }

    /* Read and unpack input codes and process them */
    while (unpack_input(&code, dec_bits) >= 0) {
        sample = (*dec_routine)(code, out_coding, &state);
        if (out_size == 2) {
            fwrite(&sample, out_size, 1, stdout);
        } else {
            code = (unsigned char)sample;
            fwrite(&code, out_size, 1, stdout);
        }
    }
    fclose(stdout);
}