view hrutil/etsi-enc.c @ 630:f85ef5c4d044 default tip

libgsmhr1: provide sizes of state structures
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 12 Mar 2026 06:19:12 +0000
parents 45e727b53da1
children
line wrap: on
line source

/*
 * gsmhr-etsi-enc is a test program for our GSM-HR encoder: it reads raw
 * 16-bit PCM (matching ETSI's *.inp) as input and writes ETSI's *.cod
 * format as output, allowing our encoder to be tested with ETSI's
 * official test sequences.
 *
 * ETSI input and output files are read and written in the local machine's
 * native byte order by default; -b and -l options can be used to force
 * BE or LE, respectively.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include "../libgsmhr1/tw_gsmhr.h"
#include "../libtest/local_endian.h"

static int
read_input(inf, pcm, filename_for_errs, big_endian)
	FILE *inf;
	int16_t *pcm;
	char *filename_for_errs;
{
	uint8_t file_bytes[320], *sp;
	int cc;
	unsigned n;

	cc = fread(file_bytes, 2, 160, inf);
	if (cc == 0)
		return 0;
	if (cc != 160) {
		fprintf(stderr, "error: short read from %s\n",
			filename_for_errs);
		exit(1);
	}
	sp = file_bytes;
	for (n = 0; n < 160; n++) {
		if (big_endian)
			pcm[n] = (sp[0] << 8) | sp[1];
		else
			pcm[n] = sp[0] | (sp[1] << 8);
		sp += 2;
	}
	return 1;
}

main(argc, argv)
	char **argv;
{
	char *infname, *outfname;
	FILE *inf, *outf;
	int opt, rc, big_endian, dtx = 0;
	struct gsmhr_encoder_state *state;
	int16_t pcm[160];
	int16_t prm_out[GSMHR_NUM_PARAMS_ENC];
	extern int optind;

	big_endian = is_native_big_endian();
	while ((opt = getopt(argc, argv, "bdl")) != EOF) {
		switch (opt) {
		case 'b':
			big_endian = 1;
			continue;
		case 'd':
			dtx = 1;
			continue;
		case 'l':
			big_endian = 0;
			continue;
		default:
		usage:
			fprintf(stderr,
				"usage: %s [-b|-l] [-d] input.inp output.cod\n",
				argv[0]);
			exit(1);
		}
	}
	if (argc != optind + 2)
		goto usage;
	infname = argv[optind];
	outfname = argv[optind+1];

	inf = fopen(infname, "r");
	if (!inf) {
		perror(infname);
		exit(1);
	}
	outf = fopen(outfname, "w");
	if (!outf) {
		perror(outfname);
		exit(1);
	}
	state = gsmhr_encoder_create(dtx);
	if (!state) {
		perror("gsmhr_encoder_create()");
		exit(1);
	}
	for (;;) {
		rc = read_input(inf, pcm, infname, big_endian);
		if (!rc)
			break;
		gsmhr_encode_frame(state, pcm, prm_out);
		emit_cod_to_endian(outf, prm_out, big_endian);
	}
	fclose(outf);
	exit(0);
}