/* tests/fixtures/qoa/qoa_ref.c: the qoa.h reference encoder / decoder for tools/fixtures/qoa.py. qoa_ref enc encode raw interleaved s16le with qoa_encode qoa_ref dec decode with qoa_decode (prints channels rate samples) */ #include #include #include #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; }