view amrtest/encode.c @ 581:e2d5cad04cbf

libgsmhr1 RxFE: store CN R0+LPC separately from speech In the original GSM 06.06 code the ECU for speech mode is entirely separate from the CN generator, maintaining separate state. (The main intertie between them is the speech vs CN state variable, distinguishing between speech and CN BFIs, in addition to the CN-specific function of distinguishing between initial and update SIDs.) In the present RxFE implementation I initially thought that we could use the same saved_frame buffer for both ECU and CN, overwriting just the first 4 params (R0 and LPC) when a valid SID comes in. However, I now realize it was a bad idea: the original code has a corner case (long sequence of speech-mode BFIs to put the ECU in state 6, then SID and CN-mode BFIs, then a good speech frame) that would be broken by that buffer reuse approach. We could eliminate this corner case by resetting the ECU state when passing through a CN insertion period, but doing so would needlessly increase the behavioral diffs between GSM 06.06 and our version. Solution: use a separate CN-specific buffer for CN R0+LPC parameters, and match the behavior of GSM 06.06 code in this regard.
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 13 Feb 2025 10:02:45 +0000
parents 526a7f0e027d
children
line wrap: on
line source

/*
 * twamr-encode is a speech encoder utility going from WAV input
 * to RFC 4867 storage format (.amr) output.  It logically replaces
 * amrnb-enc from opencore-amr package, but its command line structure
 * for specifying AMR modes follows that of twamr-tseq-enc.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include "../libtwamr/tw_amr.h"
#include "../libtest/wavreader.h"
#include "../libtest/wavrdhelp.h"

main(argc, argv)
	char **argv;
{
	char *infname, *outfname, *mode_arg;
	void *wav;
	FILE *outf;
	struct amr_encoder_state *state;
	enum Mode mode_val;
	int16_t pcm[160];
	struct amr_param_frame frame;
	uint8_t out_bytes[AMR_IETF_MAX_PL];
	int opt, dtx = 0, vad2 = 0, use_mode_file = 0, rc;
	unsigned nbytes;
	extern int optind;

	while ((opt = getopt(argc, argv, "d2")) != EOF) {
		switch (opt) {
		case 'd':
			dtx = 1;
			continue;
		case '2':
			vad2 = 1;
			continue;
		default:
		usage:
			fprintf(stderr,
			"usage: %s [-d] [-2] input.wav mode output.amr\n",
				argv[0]);
			exit(1);
		}
	}
	if (argc != optind + 3)
		goto usage;
	infname = argv[optind];
	mode_arg = argv[optind+1];
	outfname = argv[optind+2];

	wav = wav_read_open(infname);
	if (!wav) {
		perror(infname);
		exit(1);
	}
	rc = wavrd_check_header(wav, infname);
	if (rc < 0)
		exit(1);	/* error msg already printed */
	if (!strncmp(mode_arg, "file:", 5)) {
		open_mode_file(mode_arg + 5);
		use_mode_file = 1;
	} else {
		rc = grok_mode_name(mode_arg, &mode_val);
		if (rc < 0) {
			fprintf(stderr, "error: invalid mode argument \"%s\"\n",
				mode_arg);
			exit(1);
		}
	}
	outf = fopen(outfname, "w");
	if (!outf) {
		perror(outfname);
		exit(1);
	}
	state = amr_encoder_create(dtx, vad2);
	if (!state) {
		perror("amr_encoder_create()");
		exit(1);
	}
	fwrite(amr_file_header_magic, 1, AMR_IETF_HDR_LEN, outf);
	for (;;) {
		rc = wavrd_get_pcm_block(wav, pcm);
		if (!rc)
			break;
		if (use_mode_file)
			read_mode_file_line(&mode_val);
		amr_encode_frame(state, mode_val, pcm, &frame);
		nbytes = amr_frame_to_ietf(&frame, out_bytes);
		fwrite(out_bytes, 1, nbytes, outf);
	}
	fclose(outf);
	exit(0);
}