1 /* Copyright (c) 2022 Amazon
2 Written by Jan Buethe */
3 /*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <stdio.h>
29 #include <inttypes.h>
30
31 #include "fec_packets.h"
32
get_fec_frame(const char * const filename,float * features,int packet_index,int subframe_index)33 int get_fec_frame(const char * const filename, float *features, int packet_index, int subframe_index)
34 {
35
36 int16_t version;
37 int16_t header_size;
38 int16_t num_packets;
39 int16_t packet_size;
40 int16_t subframe_size;
41 int16_t subframes_per_packet;
42 int16_t num_features;
43 long offset;
44
45 FILE *fid = fopen(filename, "rb");
46
47 /* read header */
48 if (fread(&version, sizeof(version), 1, fid) != 1) goto error;
49 if (fread(&header_size, sizeof(header_size), 1, fid) != 1) goto error;
50 if (fread(&num_packets, sizeof(num_packets), 1, fid) != 1) goto error;
51 if (fread(&packet_size, sizeof(packet_size), 1, fid) != 1) goto error;
52 if (fread(&subframe_size, sizeof(subframe_size), 1, fid) != 1) goto error;
53 if (fread(&subframes_per_packet, sizeof(subframes_per_packet), 1, fid) != 1) goto error;
54 if (fread(&num_features, sizeof(num_features), 1, fid) != 1) goto error;
55
56 /* check if indices are valid */
57 if (packet_index >= num_packets || subframe_index >= subframes_per_packet)
58 {
59 fprintf(stderr, "get_fec_frame: index out of bounds\n");
60 goto error;
61 }
62
63 /* calculate offset in file (+ 2 is for rate) */
64 offset = header_size + packet_index * packet_size + 2 + subframe_index * subframe_size;
65 fseek(fid, offset, SEEK_SET);
66
67 /* read features */
68 if (fread(features, sizeof(*features), num_features, fid) != num_features) goto error;
69
70 fclose(fid);
71 return 0;
72
73 error:
74 fclose(fid);
75 return 1;
76 }
77
get_fec_rate(const char * const filename,int packet_index)78 int get_fec_rate(const char * const filename, int packet_index)
79 {
80 int16_t version;
81 int16_t header_size;
82 int16_t num_packets;
83 int16_t packet_size;
84 int16_t subframe_size;
85 int16_t subframes_per_packet;
86 int16_t num_features;
87 long offset;
88 int16_t rate;
89
90 FILE *fid = fopen(filename, "rb");
91
92 /* read header */
93 if (fread(&version, sizeof(version), 1, fid) != 1) goto error;
94 if (fread(&header_size, sizeof(header_size), 1, fid) != 1) goto error;
95 if (fread(&num_packets, sizeof(num_packets), 1, fid) != 1) goto error;
96 if (fread(&packet_size, sizeof(packet_size), 1, fid) != 1) goto error;
97 if (fread(&subframe_size, sizeof(subframe_size), 1, fid) != 1) goto error;
98 if (fread(&subframes_per_packet, sizeof(subframes_per_packet), 1, fid) != 1) goto error;
99 if (fread(&num_features, sizeof(num_features), 1, fid) != 1) goto error;
100
101 /* check if indices are valid */
102 if (packet_index >= num_packets)
103 {
104 fprintf(stderr, "get_fec_rate: index out of bounds\n");
105 goto error;
106 }
107
108 /* calculate offset in file (+ 2 is for rate) */
109 offset = header_size + packet_index * packet_size;
110 fseek(fid, offset, SEEK_SET);
111
112 /* read rate */
113 if (fread(&rate, sizeof(rate), 1, fid) != 1) goto error;
114
115 fclose(fid);
116 return (int) rate;
117
118 error:
119 fclose(fid);
120 return -1;
121 }
122
123 #if 0
124 int main()
125 {
126 float features[20];
127 int i;
128
129 if (get_fec_frame("../test.fec", &features[0], 0, 127))
130 {
131 return 1;
132 }
133
134 for (i = 0; i < 20; i ++)
135 {
136 printf("%d %f\n", i, features[i]);
137 }
138
139 printf("rate: %d\n", get_fec_rate("../test.fec", 0));
140
141 }
142 #endif