Files
4DBug f765763537 ws-audio2: codecs/audio/Qoa and audio/qoa (QOA read / write, port of qoa.h, MIT)
Decode bit-exact with ffmpeg and qoa.h; encode byte-for-byte qoa.h's qoa_encode (fixtures compile the committed
qoa.h reference with nix gcc).
2026-10-31 14:26:33 -05:00

53 lines
1.5 KiB
C

/* tests/fixtures/qoa/qoa_ref.c: the qoa.h reference encoder / decoder for tools/fixtures/qoa.py.
qoa_ref enc <channels> <rate> <in.s16le> <out.qoa> encode raw interleaved s16le with qoa_encode
qoa_ref dec <in.qoa> <out.s16le> decode with qoa_decode (prints channels rate samples) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define QOA_IMPLEMENTATION
#include "qoa.h"
static unsigned char *slurp(const char *path, long *size) {
FILE *f = fopen(path, "rb");
if (!f) return NULL;
fseek(f, 0, SEEK_END);
*size = ftell(f);
fseek(f, 0, SEEK_SET);
unsigned char *d = malloc(*size ? *size : 1);
if (fread(d, 1, *size, f) != (size_t)*size) { fclose(f); return NULL; }
fclose(f);
return d;
}
int main(int argc, char **argv) {
if (argc == 6 && !strcmp(argv[1], "enc")) {
long size;
short *pcm = (short *)slurp(argv[4], &size);
qoa_desc q;
memset(&q, 0, sizeof q);
q.channels = atoi(argv[2]);
q.samplerate = atoi(argv[3]);
q.samples = size / 2 / q.channels;
unsigned int len;
void *out = qoa_encode(pcm, &q, &len);
if (!out) return 1;
FILE *f = fopen(argv[5], "wb");
fwrite(out, 1, len, f);
fclose(f);
return 0;
}
if (argc == 4 && !strcmp(argv[1], "dec")) {
long size;
unsigned char *d = slurp(argv[2], &size);
qoa_desc q;
short *pcm = qoa_decode(d, (int)size, &q);
if (!pcm) return 1;
FILE *f = fopen(argv[3], "wb");
fwrite(pcm, 2, (size_t)q.samples * q.channels, f);
fclose(f);
printf("%u %u %u\n", q.channels, q.samplerate, q.samples);
return 0;
}
return 2;
}