view efrtest/etsi-bit-rd.c @ 467:ad032051166a

doc: AMR-EFR-hybrid-emu new article
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 12 May 2024 23:54:43 +0000
parents 8ed838709897
children
line wrap: on
line source

/*
 * In this module we implement utility functions for reading ETSI *.cod
 * and *.dec files in either LE or BE format.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "../libgsmefr/gsm_efr.h"
#include "etsi.h"

read_etsi_bits(inf, big_endian, bitvec, nwords, filename_for_errs)
	FILE *inf;
	uint8_t *bitvec;
	unsigned nwords;
	char *filename_for_errs;
{
	uint8_t file_bytes[ETSI_DEC_NWORDS * 2], *sp;
	int cc;
	unsigned n, upper;

	cc = fread(file_bytes, 2, nwords, inf);
	if (cc == 0)
		return 0;
	if (cc != nwords) {
		fprintf(stderr, "error: short read from %s\n",
			filename_for_errs);
		exit(1);
	}
	sp = file_bytes;
	for (n = 0; n < nwords; n++) {
		if (big_endian) {
			upper = sp[0];
			bitvec[n] = sp[1];
		} else {
			bitvec[n] = sp[0];
			upper = sp[1];
		}
		if (upper) {
			fprintf(stderr,
		"error in %s: non-zero in what should be %s upper byte\n",
				filename_for_errs, big_endian ? "BE" : "LE");
			exit(1);
		}
		sp += 2;
	}
	return 1;
}

void
bits2frame(input_bits, frame, filename_for_errs, frame_no)
	uint8_t *input_bits, *frame;
	char *filename_for_errs;
	unsigned frame_no;
{
	uint8_t bits[248], *sp, *dp;
	unsigned nb, byte, mask;

	bits[0] = 1;
	bits[1] = 1;
	bits[2] = 0;
	bits[3] = 0;
	bcopy(input_bits, bits + 4, 244);
	sp = bits;
	dp = frame;
	for (nb = 0; nb < EFR_RTP_FRAME_LEN; nb++) {
		byte = 0;
		for (mask = 0x80; mask; mask >>= 1) {
			if (*sp > 1) {
				fprintf(stderr,
					"error in %s frame #%u: data bit > 1\n",
					filename_for_errs, frame_no);
				exit(1);
			}
			if (*sp)
				byte |= mask;
			sp++;
		}
		*dp++ = byte;
	}
}