| 1 |
#include <stdio.h> |
| 2 |
#include <memory.h> |
| 3 |
#include <malloc.h> |
| 4 |
#include <time.h> |
| 5 |
#include "mp3.h" |
| 6 |
#include "mad_config.h" |
| 7 |
#include "mad_decoder.h" |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
static void dump_file( const char *filename ); |
| 15 |
static void test_file( const char *filename ); |
| 16 |
static void test_mad( FILE *f ); |
| 17 |
static void test_mp3f( FILE *f ); |
| 18 |
|
| 19 |
static void get_offsets( FILE *f ); |
| 20 |
|
| 21 |
static enum mad_flow cb_input( void *context, struct mad_stream *stream ); |
| 22 |
static enum mad_flow cb_header( void *context, struct mad_header const *header ); |
| 23 |
static enum mad_flow cb_accept_header( void *context, struct mad_header const *header ); |
| 24 |
static enum mad_flow cb_dump( void *context, struct mad_header const *header, struct mad_pcm *pcm ); |
| 25 |
|
| 26 |
static void cb_samples( const int *channels[MP3F_MAX_CHANNELS], |
| 27 |
unsigned num_samples, |
| 28 |
void *context ); |
| 29 |
|
| 30 |
#define BUFFER_SIZE 2048 |
| 31 |
static unsigned char the_buffer[BUFFER_SIZE]; |
| 32 |
|
| 33 |
#define MAX_OFFSETS 4096 |
| 34 |
static unsigned long offsets[MAX_OFFSETS]; |
| 35 |
static unsigned num_offsets; |
| 36 |
|
| 37 |
int main( int argc, char *argv[] ) |
| 38 |
{ |
| 39 |
int i; |
| 40 |
|
| 41 |
if ( argc <= 1 ) |
| 42 |
{ |
| 43 |
printf( "Usage: %s MP3_FILE [MP3_FILE...]\n", argv[0] ); |
| 44 |
return -1; |
| 45 |
} |
| 46 |
|
| 47 |
if ( argc > 2 && !strcmp( argv[1], "-dump" ) ) |
| 48 |
dump_file( argv[2] ); |
| 49 |
else for ( i = 1; i < argc; ++ i ) |
| 50 |
test_file( argv[i] ); |
| 51 |
|
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
typedef struct { |
| 56 |
FILE *f; |
| 57 |
unsigned done; |
| 58 |
unsigned left; |
| 59 |
FILE *f2; |
| 60 |
} DUMP_CONTEXT; |
| 61 |
|
| 62 |
static void dump_file( const char *filename ) |
| 63 |
{ |
| 64 |
enum { MAX_FRAMES = 16 }; |
| 65 |
FILE *f; |
| 66 |
unsigned i; |
| 67 |
|
| 68 |
printf( "Dumping file: \"%s\"...\n", filename ); |
| 69 |
if ( (f = fopen( filename, "rb" )) == NULL ) { |
| 70 |
printf( " Cannot open file.\n" ); |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
get_offsets( f ); |
| 75 |
|
| 76 |
printf( " Header offsets: %lu, %lu, %lu, %lu, ... %lu\n", |
| 77 |
offsets[0], offsets[1], offsets[2], offsets[3], |
| 78 |
offsets[num_offsets - 1] ); |
| 79 |
|
| 80 |
for ( i = 0; i < MAX_FRAMES; ++ i ) |
| 81 |
{ |
| 82 |
char dumpname[128]; |
| 83 |
struct mad_decoder d; |
| 84 |
DUMP_CONTEXT c = { f, i, MAX_FRAMES - i }; |
| 85 |
|
| 86 |
sprintf( dumpname, "%02u.dump", i ); |
| 87 |
printf( " Creating %s...\n", dumpname ); |
| 88 |
c.f2 = fopen( dumpname, "w" ); |
| 89 |
|
| 90 |
fseek( f, offsets[i], SEEK_SET ); |
| 91 |
mad_decoder_init( &d, &c, cb_input, cb_accept_header, NULL, cb_dump, NULL, NULL ); |
| 92 |
mad_decoder_run( &d, MAD_DECODER_MODE_SYNC ); |
| 93 |
mad_decoder_finish( &d ); |
| 94 |
|
| 95 |
fclose( c.f2 ); |
| 96 |
} |
| 97 |
|
| 98 |
fclose( f ); |
| 99 |
} |
| 100 |
|
| 101 |
static void test_file( const char *filename ) |
| 102 |
{ |
| 103 |
FILE *f; |
| 104 |
|
| 105 |
printf( "Testing file: \"%s\"...\n", filename ); |
| 106 |
|
| 107 |
if ( (f = fopen( filename, "rb" )) == NULL ) { |
| 108 |
printf( " Cannot open file.\n" ); |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
test_mad( f ); |
| 113 |
test_mp3f( f ); |
| 114 |
|
| 115 |
fclose( f ); |
| 116 |
} |
| 117 |
|
| 118 |
static void test_mad( FILE *f ) |
| 119 |
{ |
| 120 |
enum { MAX_BAD = 16 }; |
| 121 |
unsigned i, bad; |
| 122 |
|
| 123 |
get_offsets( f ); |
| 124 |
|
| 125 |
if ( num_offsets >= MAX_OFFSETS ) |
| 126 |
printf( " Reached maximum number of headers (%u)\n", MAX_OFFSETS ); |
| 127 |
else |
| 128 |
printf( " Number of headers found: %u\n", num_offsets ); |
| 129 |
printf( " Header offsets: %lu, %lu, %lu, %lu, ... %lu\n", |
| 130 |
offsets[0], offsets[1], offsets[2], offsets[3], |
| 131 |
offsets[num_offsets - 1] ); |
| 132 |
printf( " Checking header validity...\n" ); |
| 133 |
for ( i = bad = 0; i < num_offsets && bad < MAX_BAD; ++ i ) |
| 134 |
{ |
| 135 |
unsigned char header[2] = { 0, 0 }; |
| 136 |
if ( i > 0 && offsets[i] <= offsets[i - 1] ) |
| 137 |
{ |
| 138 |
printf( " Invalid offset table.\n" ); |
| 139 |
return; |
| 140 |
} |
| 141 |
fseek( f, offsets[i], SEEK_SET ); |
| 142 |
fread( header, 1, sizeof(header), f ); |
| 143 |
if ( (header[0] != 0xFF) || ((header[1] & 0xE0) != 0xE0) ) |
| 144 |
{ |
| 145 |
++ bad; |
| 146 |
printf( " ERROR: No header at file offset %lu\n", offsets[i] ); |
| 147 |
} |
| 148 |
else if ( bad > 0 ) |
| 149 |
printf( " Good header at file offset %lu\n", offsets[i] ); |
| 150 |
} |
| 151 |
if ( bad == 0 ) |
| 152 |
printf( " All offsets have valid MP3 headers\n" ); |
| 153 |
} |
| 154 |
|
| 155 |
static void get_offsets( FILE *f ) |
| 156 |
{ |
| 157 |
struct mad_decoder d; |
| 158 |
|
| 159 |
|
| 160 |
memset( &d, time(NULL), sizeof(d) ); |
| 161 |
|
| 162 |
num_offsets = 0; |
| 163 |
|
| 164 |
mad_decoder_init( &d, &f, cb_input, cb_header, NULL, NULL, NULL, NULL ); |
| 165 |
if ( mad_decoder_run( &d, MAD_DECODER_MODE_SYNC ) != 0 ) |
| 166 |
{ |
| 167 |
printf( " Error scanning file.\n" ); |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
mad_decoder_finish( &d ); |
| 172 |
} |
| 173 |
|
| 174 |
static enum mad_flow cb_input( void *context, struct mad_stream *stream ) |
| 175 |
{ |
| 176 |
FILE *f = *(FILE **)context; |
| 177 |
unsigned char *data = NULL; |
| 178 |
unsigned nthrown = 0, ncopied = 0, size = 0; |
| 179 |
unsigned offset; |
| 180 |
size_t nread = 0; |
| 181 |
|
| 182 |
if (feof (f)) |
| 183 |
return MAD_FLOW_STOP; |
| 184 |
|
| 185 |
if (stream -> next_frame) { |
| 186 |
nthrown = stream -> next_frame - the_buffer; |
| 187 |
ncopied = BUFFER_SIZE - nthrown; |
| 188 |
memmove (the_buffer, stream -> next_frame, ncopied); |
| 189 |
} |
| 190 |
|
| 191 |
data = the_buffer + ncopied; |
| 192 |
size = BUFFER_SIZE - ncopied; |
| 193 |
|
| 194 |
offset = ftell (f) - ncopied; |
| 195 |
if (size > 0) |
| 196 |
nread = fread (data, 1, size, f); |
| 197 |
|
| 198 |
mad_stream_buffer_offset (stream, the_buffer, nread + ncopied, offset); |
| 199 |
stream -> this_offset = offset; |
| 200 |
return MAD_FLOW_CONTINUE; |
| 201 |
} |
| 202 |
|
| 203 |
static enum mad_flow cb_header( void *context, struct mad_header const *header ) |
| 204 |
{ |
| 205 |
FILE *f = *(FILE **)context; |
| 206 |
unsigned long foff = ftell( f ); |
| 207 |
if ( foff > header->offset + BUFFER_SIZE ) |
| 208 |
printf( " ??? %lu <-> %lu\n", foff, header->offset ); |
| 209 |
offsets[num_offsets] = header->offset; |
| 210 |
if ( ++ num_offsets >= MAX_OFFSETS ) |
| 211 |
return MAD_FLOW_STOP; |
| 212 |
return MAD_FLOW_CONTINUE; |
| 213 |
} |
| 214 |
|
| 215 |
static enum mad_flow cb_accept_header( void *context, struct mad_header const *header ) |
| 216 |
{ |
| 217 |
(void)context; |
| 218 |
(void)header; |
| 219 |
return MAD_FLOW_CONTINUE; |
| 220 |
} |
| 221 |
|
| 222 |
static enum mad_flow cb_dump( void *context, struct mad_header const *header, struct mad_pcm *pcm ) |
| 223 |
{ |
| 224 |
DUMP_CONTEXT *c = (DUMP_CONTEXT *)context; |
| 225 |
FILE *f = c->f2; |
| 226 |
unsigned i, j, length = pcm->length; |
| 227 |
const mad_fixed_t *samples = pcm->samples[0]; |
| 228 |
(void)header; |
| 229 |
|
| 230 |
fprintf( f, "FRAME %u, OFFSET %lu (expected %lu)\n", |
| 231 |
c->done, header->offset, offsets[c->done] ); |
| 232 |
for ( i = 0; i < length; i += 8 ) { |
| 233 |
for ( j = i; i < length && j < i + 8; ++ j ) |
| 234 |
fprintf( f, "%9i ", samples[j] ); |
| 235 |
fprintf( f, "\n" ); |
| 236 |
} |
| 237 |
fprintf( f, "\n" ); |
| 238 |
|
| 239 |
++ c->done; |
| 240 |
-- c->left; |
| 241 |
return c->left == 0 ? MAD_FLOW_STOP : MAD_FLOW_CONTINUE; |
| 242 |
} |
| 243 |
|
| 244 |
static void test_mp3f( FILE *f ) |
| 245 |
{ |
| 246 |
enum { FIRST = 15000, SECOND = 2000, TOTAL = FIRST + SECOND }; |
| 247 |
enum { MID_OFFSET = 16000, MID_SIZE = 800 }; |
| 248 |
static int x[TOTAL], y[TOTAL], z[MID_SIZE]; |
| 249 |
int *p; |
| 250 |
MP3_FILE mp3f = mp3f_new(); |
| 251 |
|
| 252 |
mp3f_set_file( mp3f, f ); |
| 253 |
if ( mp3f_analyze( mp3f ) ) |
| 254 |
{ |
| 255 |
printf( " MP3F: Channels = %u\n", mp3f_channels( mp3f ) ); |
| 256 |
printf( " MP3F: Frequency = %u\n", mp3f_frequency( mp3f ) ); |
| 257 |
printf( " MP3F: Samples = %lu\n", (unsigned long)mp3f_samples( mp3f ) ); |
| 258 |
printf( " MP3F: Time => %.2lfs\n", |
| 259 |
(double)mp3f_samples(mp3f) / (double)mp3f_frequency(mp3f) ); |
| 260 |
} |
| 261 |
else |
| 262 |
printf( " MP3F: Cannot analyze\n" ); |
| 263 |
|
| 264 |
memset( x, 0x22, sizeof(x) ); |
| 265 |
memset( y, 0x22, sizeof(y) ); |
| 266 |
memset( z, 0x22, sizeof(z) ); |
| 267 |
|
| 268 |
mp3f_set_file( mp3f, f ); |
| 269 |
p = x; |
| 270 |
mp3f_set_callback( mp3f, cb_samples, &p ); |
| 271 |
mp3f_read( mp3f, TOTAL ); |
| 272 |
|
| 273 |
mp3f_set_file( mp3f, f ); |
| 274 |
p = y; |
| 275 |
mp3f_set_callback( mp3f, cb_samples, &p ); |
| 276 |
mp3f_read( mp3f, FIRST ); |
| 277 |
mp3f_read( mp3f, SECOND ); |
| 278 |
|
| 279 |
mp3f_set_file( mp3f, f ); |
| 280 |
p = z; |
| 281 |
mp3f_set_callback( mp3f, cb_samples, &p ); |
| 282 |
if ( mp3f_seek( mp3f, MID_OFFSET ) ) |
| 283 |
mp3f_read( mp3f, MID_SIZE ); |
| 284 |
else |
| 285 |
printf( " MP3F: Seek failed\n" ); |
| 286 |
|
| 287 |
if ( memcmp( x, y, FIRST * sizeof(int) ) != 0 ) |
| 288 |
printf( " MP3F: Difference in first samples!\n" ); |
| 289 |
else |
| 290 |
printf( " MP3F: First samples OK\n" ); |
| 291 |
|
| 292 |
if ( memcmp( x + FIRST, y + FIRST, SECOND * sizeof(int) ) != 0 ) |
| 293 |
printf( " MP3F: Difference in later samples!\n" ); |
| 294 |
else |
| 295 |
printf( " MP3F: Later samples OK\n" ); |
| 296 |
|
| 297 |
if ( memcmp( x + MID_OFFSET, z, MID_SIZE * sizeof(int) ) != 0 ) |
| 298 |
printf( " MP3F: Seek doesn't work\n" ); |
| 299 |
else |
| 300 |
printf( " MP3F: Seek works\n" ); |
| 301 |
|
| 302 |
mp3f_delete( mp3f ); |
| 303 |
} |
| 304 |
|
| 305 |
static void cb_samples( const int *channels[MP3F_MAX_CHANNELS], |
| 306 |
unsigned num_samples, |
| 307 |
void *context ) |
| 308 |
{ |
| 309 |
int **pp = (int **)context; |
| 310 |
int *p = *pp; |
| 311 |
unsigned i; |
| 312 |
|
| 313 |
for ( i = 0; i < num_samples; ++ i ) |
| 314 |
p[i] = channels[0][i]; |
| 315 |
*pp += num_samples; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
void *Melder_malloc( unsigned size ) { return malloc( size ); } |
| 320 |
void _Melder_free( void **p ) { free( *p ); } |
| 321 |
|