forked from sippy/libg722
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
148 lines (134 loc) · 4.32 KB
/
test.c
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright (c) 2014-2016 Sippy Software, Inc., http://www.sippysoft.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <sys/endian.h>
#include <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "g722_encoder.h"
#include "g722_decoder.h"
#define BUFFER_SIZE 10
static void
usage(const char *argv0)
{
fprintf(stderr, "usage: %s [--sln16k] [--bend] file.g722 file.raw\n"
" %s --encode [--sln16k] [--bend] file.raw file.g722\n", argv0,
argv0);
exit (1);
}
int
main(int argc, char **argv)
{
FILE *fi, *fo;
uint8_t ibuf[BUFFER_SIZE];
int16_t obuf[BUFFER_SIZE * 2];
G722_DEC_CTX *g722_dctx;
G722_ENC_CTX *g722_ectx;
int i, srate, ch, enc, bend;
size_t oblen;
/* options descriptor */
static struct option longopts[] = {
{ "sln16k", no_argument, NULL, 256 },
{ "encode", no_argument, NULL, 257 },
{ "bend", no_argument, NULL, 258 },
{ NULL, 0, NULL, 0 }
};
srate = G722_SAMPLE_RATE_8000;
oblen = sizeof(obuf) / 2;
enc = bend = 0;
while ((ch = getopt_long(argc, argv, "", longopts, NULL)) != -1) {
switch (ch) {
case 256:
srate &= ~G722_SAMPLE_RATE_8000;
oblen *= 2;
break;
case 257:
enc = 1;
break;
case 258:
bend = 1;
break;
default:
usage(argv[0]);
}
}
argc -= optind;
argv += optind;
if (argc != 2) {
usage(argv[-optind]);
}
fi = fopen(argv[0], "r");
if (fi == NULL) {
fprintf(stderr, "cannot open %s\n", argv[0]);
exit (1);
}
fo = fopen(argv[1], "w");
if (fo == NULL) {
fprintf(stderr, "cannot open %s\n", argv[1]);
exit (1);
}
if (enc == 0) {
g722_dctx = g722_decoder_new(64000, srate);
if (g722_dctx == NULL) {
fprintf(stderr, "g722_decoder_new() failed\n");
exit (1);
}
while (fread(ibuf, sizeof(ibuf), 1, fi) == 1) {
g722_decode(g722_dctx, ibuf, sizeof(ibuf), obuf);
for (i = 0; i < (oblen / sizeof(obuf[0])); i++) {
if (bend == 0) {
obuf[i] = htole16(obuf[i]);
} else {
obuf[i] = htobe16(obuf[i]);
}
}
fwrite(obuf, oblen, 1, fo);
fflush(fo);
}
} else {
g722_ectx = g722_encoder_new(64000, srate);
if (g722_ectx == NULL) {
fprintf(stderr, "g722_encoder_new() failed\n");
exit (1);
}
while (fread(obuf, oblen, 1, fi) == 1) {
for (i = 0; i < (oblen / sizeof(obuf[0])); i++) {
if (bend == 0) {
obuf[i] = le16toh(obuf[i]);
} else {
obuf[i] = be16toh(obuf[i]);
}
}
g722_encode(g722_ectx, obuf, (oblen / sizeof(obuf[0])), ibuf);
fwrite(ibuf, sizeof(ibuf), 1, fo);
fflush(fo);
}
}
fclose(fi);
fclose(fo);
exit(0);
}