Skip to content

Commit

Permalink
Correctly handle files that are not multiple of the buffer size.
Browse files Browse the repository at this point in the history
  • Loading branch information
sobomax committed Apr 22, 2024
1 parent 7104d89 commit a5e0db6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
26 changes: 14 additions & 12 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ main(int argc, char **argv)
G722_DEC_CTX *g722_dctx;
G722_ENC_CTX *g722_ectx;
int i, srate, ch, enc, bend;
size_t oblen;
int oblen;

/* options descriptor */
static struct option longopts[] = {
Expand All @@ -70,13 +70,13 @@ main(int argc, char **argv)
};

srate = G722_SAMPLE_RATE_8000;
oblen = sizeof(obuf) / 2;
oblen = 1;
enc = bend = 0;
while ((ch = getopt_long(argc, argv, "", longopts, NULL)) != -1) {
switch (ch) {
case 256:
srate &= ~G722_SAMPLE_RATE_8000;
oblen *= 2;
oblen = 2;
break;
case 257:
enc = 1;
Expand Down Expand Up @@ -106,23 +106,23 @@ main(int argc, char **argv)
exit (1);
}

int ib;
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++) {
while ((ib=fread(ibuf, 1, sizeof(ibuf), fi)) >= 1) {
g722_decode(g722_dctx, ibuf, ib, obuf);
for (i = 0; i < (ib * oblen); i++) {
if (bend == 0) {
obuf[i] = htole16(obuf[i]);
} else {
obuf[i] = htobe16(obuf[i]);
}
}
fwrite(obuf, oblen, 1, fo);
fwrite(obuf, ib * oblen * sizeof(obuf[0]), 1, fo);
fflush(fo);
}
} else {
Expand All @@ -131,16 +131,18 @@ main(int argc, char **argv)
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++) {
int insize = sizeof(obuf) / ((oblen == 1) ? 2 : 1);
while ((ib=fread(obuf, 1, insize, fi)) >= 1) {
int ibnelem = ib / sizeof(obuf[0]);
for (i = 0; i < ibnelem; 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);
g722_encode(g722_ectx, obuf, ibnelem, ibuf);
fwrite(ibuf, ibnelem / oblen, 1, fo);
fflush(fo);
}
}
Expand Down
4 changes: 2 additions & 2 deletions test_data/test.checksum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
8eb29e8e9feb9709d96c7bbda37cbb2f77510fcfab2d1f48a495178cd2231253 test.raw.out
a886f22d37ecd356e1963072bc481ed2c5dd12539428188d9779d33a520f3ff8 test.raw.16k.out
15570a43acb0ff0f30aced265973eeb7733023be3697cdff0df36c41e88385c6 pcminb.g722.out
1d3f23110538d25b96631db4aff3eff84f99b0859140302a99b323b259b24b89 pcminb.raw.16k.out
fc0abeb220fc66fc7d5429758dff26b56bdc9e2d64ce701a5b2489ba3a2fb877 pcminb.g722.out
6f80f1635a58a9352b1a31357f6f3feea4459953df34d6d275167f4c3e8cc967 pcminb.raw.16k.out
cb8edfaa7b5a85384dde8e0c9976f5247294732d6caf32c07b94510fc8844403 test.g722.out
e70c92cd2068d112839db12bfaa787cc0ea9a809b64fbf62fd78b6041cd9fbcc fullscale.raw.out

0 comments on commit a5e0db6

Please sign in to comment.