annotate ater8/read_file.c @ 51:db39e8855f3d

ater: implement play-d144
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 25 Sep 2024 05:53:38 +0000
parents ff94d7fc5891
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
42
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
2 * Here we implement the function that reads ETSI *.dec binary files
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
3 * which we've adopted as our TRAU-UL test input format for GSM-HR.
20
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/file.h>
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <sys/stat.h>
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdint.h>
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdbool.h>
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdio.h>
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <stdlib.h>
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <string.h>
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <unistd.h>
42
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
15 #include "../libhr/tw_gsmhr.h"
20
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 #include "read_file.h"
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18
42
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
19 static int validate_frames(const int16_t *buf, unsigned nframes)
20
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 {
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 unsigned n;
42
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
22 int rc;
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
23 int16_t expect_taf;
20
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 for (n = 0; n < nframes; n++) {
42
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
26 rc = gsmhr_check_decoder_params(buf);
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
27 if (rc < 0)
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
28 return rc;
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
29 /* disallow BFI=2 non-ETSI extension */
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
30 if (buf[18] > 1)
20
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 return -1;
42
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
32 /* enforce TAF matching position */
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
33 if (n % 12 == 11)
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
34 expect_taf = 1;
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
35 else
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
36 expect_taf = 0;
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
37 if (buf[21] != expect_taf)
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
38 return -1;
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
39 buf += GSMHR_NUM_PARAMS_DEC;
20
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 }
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 return 0;
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 }
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43
42
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
44 int read_binary_file(const char *filename, int16_t **bufret, unsigned *size_ret)
20
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 {
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 int fd, rc;
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 struct stat st;
42
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
48 int16_t *buf;
20
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 unsigned nframes;
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 fd = open(filename, O_RDONLY);
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 if (fd < 0) {
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 perror(filename);
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 return -1;
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 }
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 fstat(fd, &st);
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 if (!S_ISREG(st.st_mode)) {
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 close(fd);
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 fprintf(stderr, "error: %s is not a regular file\n", filename);
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 return -1;
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 }
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 if (!st.st_size) {
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 close(fd);
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 fprintf(stderr, "error: %s is an empty file\n", filename);
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 return -1;
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
42
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
67 if (st.st_size % 44) {
20
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 close(fd);
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 fprintf(stderr,
42
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
70 "error: size of %s is not a multiple of 44 bytes\n",
20
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 filename);
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 return -1;
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 }
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 buf = malloc(st.st_size);
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 if (!buf) {
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 close(fd);
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 fprintf(stderr, "unable to malloc buffer for %s\n", filename);
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 return -1;
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 }
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 read(fd, buf, st.st_size);
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 close(fd);
42
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
82 nframes = st.st_size / 44;
20
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83
42
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
84 rc = validate_frames(buf, nframes);
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
85 if (rc < 0) {
20
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 free(buf);
42
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
87 fprintf(stderr,
ff94d7fc5891 new program itt-ater-8
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
88 "error: %s is not a valid GSM-HR dec file, or has wrong endian\n",
20
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 filename);
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 return -1;
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 }
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 *bufret = buf;
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 *size_ret = nframes;
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 return 0;
5405c1573027 ater: implement file reading
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 }