Debian Codegraph @

src/tests/seek_accuracy.c

raw
1 /*
2 seek-accuracy: Take some given mpeg file and validate that seeks are indeed accurate.
3
4 copyright 2009-2023 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6 initially written by Thomas Orgis
7
8 arguments: decoder preframes testfile.mpeg
9 */
10
11 #include "config.h"
12 #include <mpg123.h>
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include "mpg123.h"
16 #include "../compat/compat.h"
17
18 #define SAMPLES 187
19 /* Cannot use the const value as fixed array size:-( */
20 const size_t samples = SAMPLES;
21
22 /* Use getlopt.c in future? But heck, don't bloat. */
23 enum theargs
24 {
25 arg_binname = 0
26 ,arg_decoder
27 ,arg_preframes
28 ,arg_file
29 ,arg_total
30 };
31
32 const char* filename;
33 int channels;
34 mpg123_handle *m;
35 size_t first_sample_errs = 0;
36
37
38 int check_seeking(size_t *errs);
39
40 int main(int argc, char **argv)
41 {
42 int ret = 0;
43 size_t errs[2] = {0, 0};
44 size_t errs_ntom[2] = {0, 0};
45 const char* decoder = "auto";
46 const char* preframes = "10";
47
48 if(argc < arg_total)
49 {
50 if(argc < 2)
51 {
52 /* Would at least need a file to use ... */
53 fprintf(stderr, "\nUsage: %s <decoder> <preframes> <mpeg audio file>\n\n", argv[0]);
54 return -1;
55 }
56 /* silently use defaults plus given file */
57 filename = argv[argc-1];
58 }
59 else
60 {
61 decoder = argv[arg_decoder];
62 preframes = argv[arg_preframes];
63 filename = argv[arg_file];
64 }
65 m = mpg123_new(decoder, NULL);
66 // Try to set decoder to generic to avoid memory sanitizer freaking out about
67 // asssembly synth routines.
68 mpg123_decoder(m, "generic");
69 mpg123_param(m, MPG123_RESYNC_LIMIT, -1, 0);
70 mpg123_param(m, MPG123_REMOVE_FLAGS, MPG123_GAPLESS, 0);
71
72 if(mpg123_param(m, MPG123_PREFRAMES, atol(preframes), 0) == MPG123_OK)
73 fprintf(stderr,"Testing library with preframes set to %li\n", atol(preframes));
74
75 ret = check_seeking(errs);
76
77 if(ret == 0)
78 {
79 fprintf(stderr,"Now with NtoM resampling to 33333 Hz!");
80 mpg123_param(m, MPG123_FORCE_RATE, 33333, 0);
81 ret = check_seeking(errs_ntom);
82 }
83 else fprintf(stderr, "Some failure occured! Unable to test properly!");
84
85 fprintf(stderr,"\n");
86 fprintf(stderr,"1to1 indexed seek errors: %zu / %zu\n", errs[0], samples);
87 fprintf(stderr,"1to1 non-indexed seek errors: %zu / %zu\n", errs[1], samples);
88 fprintf(stderr,"NtoM indexed seek errors: %zu / %zu\n", errs_ntom[0], samples);
89 fprintf(stderr,"NtoM non-indexed seek errors: %zu / %zu\n", errs_ntom[1], samples);
90 fprintf(stderr,"Errors in getting first sample again: %zu\n", first_sample_errs);
91 fprintf(stderr,"\n");
92
93 if(ret == 0)
94 {
95
96 if(first_sample_errs == 0 && errs[0] == 0 && errs[1] == 0 && errs_ntom[0] == 0 && errs_ntom[1] == 0)
97 {
98 printf("Congratulations, all seeks were accurate!\n");
99 printf("But be warned: Not _all_ sample offsets have been tested. This result just means that the basic mechanism of sample-accurate seeking seems to work.\n");
100 }
101 else
102 {
103 printf("There have been some errors. For layer 3 files, this may be due to too low MPG123_PREFRAMES.\n");
104 ret = -1;
105 }
106
107 }
108 else fprintf(stderr, "Some bad failure during checking!\n");
109
110 mpg123_delete(m);
111 mpg123_exit();
112
113 printf("%s\n", ret ? "FAIL" : "PASS");
114 return ret;
115 }
116
117 int open_file()
118 {
119 if( mpg123_open(m, filename) == MPG123_OK
120 &&
121 mpg123_getformat(m, NULL, &channels, NULL) == MPG123_OK )
122 {
123 fprintf(stderr,"Channels: %i\n", channels);
124 return 0;
125 }
126 else
127 {
128 fprintf(stderr,"Opening file failed: %s\n", mpg123_strerror(m));
129 return -1;
130 }
131 }
132
133 /* Operation:
134 Read through the whole file and remember selected samples and their position.
135 Then, seek to the positions selectively and compare.
136 Second mode: Check if this is identical to fresh decoding and seek (without frame index table)... less important now, focusing on the first one.
137 */
138
139 struct seeko
140 {
141 off_t position[SAMPLES];
142 short left[SAMPLES];
143 short right[SAMPLES];
144 };
145
146 /* select some positions to check seek accuracy on */
147 void fix_positions(struct seeko *so, off_t length)
148 {
149 off_t step = (length-1)/(samples-1);
150 size_t i;
151 /* simple fixed stepping, in order (one could shuffle this) */
152 for(i=0; i<samples-1; ++i)
153 so->position[i] = i*step;
154
155 /* plus the last one! */
156 so->position[samples-1] = length-1;
157 }
158
159 int collect(struct seeko *so)
160 {
161 off_t pos_count = 0;
162 off_t length;
163 int err = MPG123_OK;
164 size_t posi = 0;
165 mpg123_scan(m);
166 length = mpg123_length(m);
167 fprintf(stderr,"Estimated length: %" PRIiMAX "\n", (intmax_t)length);
168 /* Compute the interesting positions */
169 fix_positions(so, length);
170 /*
171 Default format is always 16bit int, rate does not matter.
172 Let's just get the channel count and not bother.
173 */
174 while(err == MPG123_OK)
175 {
176 short buff[1024]; /* choosing a non-divider of mpeg frame size on purpose */
177 size_t got = 0;
178 off_t buffsamples;
179 err = mpg123_read(m, (unsigned char*)buff, 1024*sizeof(short), &got);
180 buffsamples = got/(channels*sizeof(short));
181 while(so->position[posi] < pos_count+buffsamples)
182 {
183 size_t i = (so->position[posi]-pos_count)*channels;
184 fprintf(stderr,"got sample %zu (%" PRIiMAX ")\n", posi, (intmax_t)so->position[posi]);
185 so->left[posi] = buff[i];
186 if(channels == 2)
187 so->right[posi] = buff[i+1];
188
189 if(++posi >= samples) break;
190 }
191 if(posi >= samples) break;
192
193 pos_count += buffsamples;
194 }
195 if(posi < samples && (err != MPG123_DONE && err != MPG123_OK))
196 {
197 fprintf(stderr,"An error occured (not done)?: %s\n", mpg123_strerror(m));
198 return -1;
199 }
200 return 0;
201 }
202
203 /* We allow some minimal difference ... for subtle floating point effects. */
204 int same(short a, short b)
205 {
206 return (abs(a-b) <= 2);
207 }
208
209 int check_sample(struct seeko *so, size_t i)
210 {
211 short buf[2]; /* at max one left and right sample */
212 size_t got = 0;
213
214 if(mpg123_seek(m, so->position[i], SEEK_SET) != so->position[i])
215 {
216 fprintf(stderr,"Error seeking to %" PRIiMAX ": %s\n", (intmax_t)so->position[i], mpg123_strerror(m));
217 return -1;
218 }
219
220 if( mpg123_read(m, (unsigned char*)buf, channels*sizeof(short), &got) != MPG123_OK
221 ||
222 got/sizeof(short) != channels )
223 {
224 fprintf(stderr,"Error occured on reading sample %zu! (%s)\n", i, mpg123_strerror(m));
225 return -1;
226 }
227 if(same(buf[0], so->left[i]) && (channels == 1 || same(buf[1], so->right[i])))
228 {
229 fprintf(stderr,"sample %zu PASS\n", i);
230 }
231 else
232 {
233 if(channels == 1) fprintf(stderr,"sample %zu FAIL (%i != %i)\n", i, buf[0], so->left[i]);
234 else fprintf(stderr,"sample %zu FAIL (%i != %i || %i != %i)\n", i, buf[0], so->left[i], buf[1], so->right[i]);
235 return -1;
236 }
237 return 0;
238 }
239
240 size_t check_positions(struct seeko *so)
241 {
242 size_t i;
243 size_t errs = 0;
244 fprintf(stderr,"Seeking and comparing...\n");
245 for(i=0; i<samples; ++i)
246 {
247 if(i == samples/2)
248 {
249 fprintf(stderr,"a little scan in between\n");
250 mpg123_scan(m);
251 }
252 if(check_sample(so, i) != 0)
253 ++errs;
254 }
255 fprintf(stderr,"Check the first one again, seeking back from the end.\n");
256 if(check_sample(so, 0) != 0)
257 ++first_sample_errs;
258
259 return errs;
260 }
261
262 int check_seeking(size_t *errs)
263 {
264 struct seeko so;
265 int ret = 0;
266
267 /* First opening. */
268 if(open_file() != 0) return -1;
269
270 fprintf(stderr,"Collecting samples...\n");
271 ret = collect(&so);
272 if(ret != 0) return ret;
273
274 errs[0] = check_positions(&so);
275
276 fprintf(stderr,"With fresh opening (empty seek index):\n");
277 if(open_file() != 0) return -1;
278
279 errs[1] = check_positions(&so);
280
281 fprintf(stderr,"Another fresh Opening to see if first sample works:\n");
282 if(open_file() != 0) return -1;
283 if(check_sample(&so, 0) != 0)
284 ++first_sample_errs;
285
286 return ret;
287 }