Subversion Repositories Kolibri OS

Rev

Rev 1905 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1905 Rev 3960
Line 1... Line 1...
1
/*
1
/*
2
	parse: spawned from common; clustering around stream/frame parsing
2
	parse: spawned from common; clustering around stream/frame parsing
Line 3... Line 3...
3
 
3
 
4
	copyright ?-2009 by the mpg123 project - free software under the terms of the LGPL 2.1
4
	copyright ?-2012 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
5
	see COPYING and AUTHORS files in distribution or http://mpg123.org
6
	initially written by Michael Hipp & Thomas Orgis
6
	initially written by Michael Hipp & Thomas Orgis
Line 7... Line 7...
7
*/
7
*/
Line 11... Line 11...
11
#include 
11
#include 
12
#include 
12
#include 
Line 13... Line 13...
13
 
13
 
Line -... Line 14...
-
 
14
#include "getbits.h"
-
 
15
 
-
 
16
#if defined (WANT_WIN32_SOCKETS)
-
 
17
#include 
Line 14... Line 18...
14
#include "getbits.h"
18
#include 
15
 
19
#endif
16
 
20
 
17
/* a limit for number of frames in a track; beyond that unsigned long may not be enough to hold byte addresses */
21
/* a limit for number of frames in a track; beyond that unsigned long may not be enough to hold byte addresses */
Line 22... Line 26...
22
/* hm, is this portable across preprocessors? */
26
/* hm, is this portable across preprocessors? */
23
#define ULONG_MAX ((unsigned long)-1)
27
#define ULONG_MAX ((unsigned long)-1)
24
#endif
28
#endif
25
#define TRACK_MAX_FRAMES ULONG_MAX/4/1152
29
#define TRACK_MAX_FRAMES ULONG_MAX/4/1152
Line -... Line 30...
-
 
30
 
-
 
31
#include "mpeghead.h"
26
 
32
 
Line 27... Line 33...
27
#include "debug.h"
33
#include "debug.h"
Line 28... Line -...
28
 
-
 
29
#define bsbufid(fr) (fr)->bsbuf==(fr)->bsspace[0] ? 0 : ((fr)->bsbuf==fr->bsspace[1] ? 1 : ( (fr)->bsbuf==(fr)->bsspace[0]+512 ? 2 : ((fr)->bsbuf==fr->bsspace[1]+512 ? 3 : -1) ) )
34
 
30
 
-
 
31
/*
-
 
32
	AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
-
 
33
	A: sync
-
 
34
	B: mpeg version
-
 
35
	C: layer
-
 
36
	D: CRC
-
 
37
	E: bitrate
-
 
38
	F:sampling rate
35
#define bsbufid(fr) (fr)->bsbuf==(fr)->bsspace[0] ? 0 : ((fr)->bsbuf==fr->bsspace[1] ? 1 : ( (fr)->bsbuf==(fr)->bsspace[0]+512 ? 2 : ((fr)->bsbuf==fr->bsspace[1]+512 ? 3 : -1) ) )
39
	G: padding
-
 
40
	H: private
-
 
41
	I: channel mode
-
 
42
	J: mode ext
-
 
43
	K: copyright
36
 
44
	L: original
37
/* PARSE_GOOD and PARSE_BAD have to be 1 and 0 (TRUE and FALSE), others can vary. */
45
	M: emphasis
38
enum parse_codes
46
 
-
 
47
	old compare mask 0xfffffd00:
-
 
48
	11111111 11111111 11111101 00000000
39
{
49
 
-
 
50
	means: everything must match excluding padding and channel mode, ext mode, ...
-
 
51
	But a vbr stream's headers will differ in bitrate!
-
 
52
	We are already strict in allowing only frames of same type in stream, we should at least watch out for VBR while being strict.
40
	 PARSE_MORE = MPG123_NEED_MORE
53
 
-
 
54
	So a better mask is:
41
	,PARSE_ERR  = MPG123_ERR
55
	11111111 11111111 00001101 00000000
42
	,PARSE_END  = 10 /* No more audio data to find. */
56
 
-
 
57
	Even more, I'll allow varying crc bit.
43
	,PARSE_GOOD = 1 /* Everything's fine. */
58
	11111111 11111110 00001101 00000000
44
	,PARSE_BAD  = 0 /* Not fine (invalid data). */
59
 
-
 
60
	(still unsure about this private bit)
-
 
Line 61... Line 45...
61
*/
45
	,PARSE_RESYNC = 2 /* Header not good, go into resync. */
62
#define HDRCMPMASK 0xfffe0d00
46
	,PARSE_AGAIN  = 3 /* Really start over, throw away and read a new header, again. */
63
#define HDRSAMPMASK 0xc00 /* 1100 00000000, FF bits (sample rate) */
47
};
64
 
48
 
Line 75... Line 59...
75
		{0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
59
		{0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,},
76
		{0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,}
60
		{0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,}
77
	}
61
	}
78
};
62
};
Line 79... Line 63...
79
 
63
 
Line 80... Line 64...
80
const long freqs[9] = { 44100, 48000, 32000, 22050, 24000, 16000 , 11025 , 12000 , 8000 };
64
static const long freqs[9] = { 44100, 48000, 32000, 22050, 24000, 16000 , 11025 , 12000 , 8000 };
81
 
-
 
-
 
65
 
82
static int decode_header(mpg123_handle *fr,unsigned long newhead);
66
static int decode_header(mpg123_handle *fr,unsigned long newhead, int *freeformat_count);
83
 
-
 
84
int read_frame_init(mpg123_handle* fr)
67
static int skip_junk(mpg123_handle *fr, unsigned long *newheadp, long *headcount);
85
{
-
 
86
	if(frame_reset(fr) != 0) return -1;
-
 
Line 87... Line 68...
87
	return 0;
68
static int do_readahead(mpg123_handle *fr, unsigned long newhead);
-
 
69
static int wetwork(mpg123_handle *fr, unsigned long *newheadp);
Line 88... Line 70...
88
}
70
 
89
 
71
/* These two are to be replaced by one function that gives all the frame parameters (for outsiders).*/
90
/* These two are to be replaced by one function that gives all the frame parameters (for outsiders).*/
72
/* Those functions are unsafe regarding bad arguments (inside the mpg123_handle), but just returning anything would also be unsafe, the caller code has to be trusted. */
91
 
73
 
Line 97... Line 79...
97
long frame_freq(mpg123_handle *fr)
79
long frame_freq(mpg123_handle *fr)
98
{
80
{
99
	return freqs[fr->sampling_frequency];
81
	return freqs[fr->sampling_frequency];
100
}
82
}
Line 101... Line -...
101
 
-
 
102
#define free_format_header(head) ( ((head & 0xffe00000) == 0xffe00000) && ((head>>17)&3) && (((head>>12)&0xf) == 0x0) && (((head>>10)&0x3) != 0x3 ))
-
 
103
 
83
 
104
/* compiler is smart enought to inline this one or should I really do it as macro...? */
84
/* compiler is smart enought to inline this one or should I really do it as macro...? */
105
int head_check(unsigned long head)
85
static int head_check(unsigned long head)
106
{
86
{
107
	if
87
	if
108
	(
-
 
109
		/* first 11 bits are set to 1 for frame sync */
88
	(
110
		((head & 0xffe00000) != 0xffe00000)
89
		((head & HDR_SYNC) != HDR_SYNC)
111
		||
90
		||
112
		/* layer: 01,10,11 is 1,2,3; 00 is reserved */
91
		/* layer: 01,10,11 is 1,2,3; 00 is reserved */
113
		(!((head>>17)&3))
92
		(!(HDR_LAYER_VAL(head)))
114
		||
93
		||
115
		/* 1111 means bad bitrate */
94
		/* 1111 means bad bitrate */
116
		(((head>>12)&0xf) == 0xf)
95
		(HDR_BITRATE_VAL(head) == 0xf)
117
		||
96
		||
118
		/* sampling freq: 11 is reserved */
97
		/* sampling freq: 11 is reserved */
119
		(((head>>10)&0x3) == 0x3 )
98
		(HDR_SAMPLERATE_VAL(head) == 0x3)
120
		/* here used to be a mpeg 2.5 check... re-enabled 2.5 decoding due to lack of evidence that it is really not good */
99
		/* here used to be a mpeg 2.5 check... re-enabled 2.5 decoding due to lack of evidence that it is really not good */
121
	)
100
	)
122
	{
101
	{
123
		return FALSE;
102
		return FALSE;
Line 139... Line 118...
139
 
118
 
140
		Also, how to avoid false positives? I guess I should interpret more of the header to rule that out(?).
119
		Also, how to avoid false positives? I guess I should interpret more of the header to rule that out(?).
141
		I hope that ensuring all zeros until tag start is enough.
120
		I hope that ensuring all zeros until tag start is enough.
142
	*/
121
	*/
143
	int lame_offset = (fr->stereo == 2) ? (fr->lsf ? 17 : 32 ) : (fr->lsf ? 9 : 17);
-
 
144
	/* At least skip the decoder delay. */
-
 
145
#ifdef GAPLESS
-
 
146
	if(fr->begin_s == 0) frame_gapless_init(fr, GAPLESS_DELAY, 0);
-
 
Line -... Line 122...
-
 
122
	int lame_offset = (fr->stereo == 2) ? (fr->lsf ? 17 : 32 ) : (fr->lsf ? 9 : 17);
-
 
123
 
-
 
124
	if(fr->p.flags & MPG123_IGNORE_INFOFRAME) return 0;
147
#endif
125
 
148
 
126
	/* Note: CRC or not, that does not matter here. */
149
	if(fr->framesize >= 120+lame_offset) /* traditional Xing header is 120 bytes */
127
	if(fr->framesize >= 120+lame_offset) /* traditional Xing header is 120 bytes */
150
	{
128
	{
151
		int i;
129
		int i;
Line 193... Line 171...
193
				xing_flags = make_long(fr->bsbuf, lame_offset);
171
				xing_flags = make_long(fr->bsbuf, lame_offset);
194
				lame_offset += 4;
172
				lame_offset += 4;
195
				debug1("Xing: flags 0x%08lx", xing_flags);
173
				debug1("Xing: flags 0x%08lx", xing_flags);
196
				if(xing_flags & 1) /* frames */
174
				if(xing_flags & 1) /* frames */
197
				{
175
				{
-
 
176
					if(fr->p.flags & MPG123_IGNORE_STREAMLENGTH)
198
					/*
177
					{
199
						In theory, one should use that value for skipping...
178
						if(VERBOSE3)
200
						When I know the exact number of samples I could simply count in flush_output,
179
						fprintf(stderr, "Note: Ignoring Xing frames because of MPG123_IGNORE_STREAMLENGTH\n");
201
						but that's problematic with seeking and such.
180
					}
202
						I still miss the real solution for detecting the end.
181
					else
203
					*/
182
					{
204
					fr->track_frames = (off_t) make_long(fr->bsbuf, lame_offset);
183
						fr->track_frames = (off_t) make_long(fr->bsbuf, lame_offset);
205
					if(fr->track_frames > TRACK_MAX_FRAMES) fr->track_frames = 0; /* endless stream? */
184
						if(fr->track_frames > TRACK_MAX_FRAMES) fr->track_frames = 0; /* endless stream? */
206
					#ifdef GAPLESS
185
#ifdef GAPLESS
207
					/* if no further info there, remove/add at least the decoder delay */
186
						/* All or nothing: Only if encoder delay/padding is known we'll cut samples for gapless. */
208
					if(fr->p.flags & MPG123_GAPLESS)
187
						if(fr->p.flags & MPG123_GAPLESS)
209
					{
-
 
210
						off_t length = fr->track_frames * spf(fr);
188
						frame_gapless_init(fr, fr->track_frames, 0, 0);
211
						if(length > 1)
-
 
212
						frame_gapless_init(fr, GAPLESS_DELAY, length+GAPLESS_DELAY);
-
 
213
					}
-
 
214
					#endif
189
#endif
215
					if(VERBOSE3) fprintf(stderr, "Note: Xing: %lu frames\n", (long unsigned)fr->track_frames);
190
						if(VERBOSE3) fprintf(stderr, "Note: Xing: %lu frames\n", (long unsigned)fr->track_frames);
-
 
191
					}
-
 
192
 
216
					lame_offset += 4;
193
					lame_offset += 4;
217
				}
194
				}
218
				if(xing_flags & 0x2) /* bytes */
195
				if(xing_flags & 0x2) /* bytes */
219
				{
196
				{
-
 
197
					if(fr->p.flags & MPG123_IGNORE_STREAMLENGTH)
-
 
198
					{
-
 
199
						if(VERBOSE3)
-
 
200
						fprintf(stderr, "Note: Ignoring Xing bytes because of MPG123_IGNORE_STREAMLENGTH\n");
-
 
201
					}
-
 
202
					else
-
 
203
					{
220
					unsigned long xing_bytes = make_long(fr->bsbuf, lame_offset);					/* We assume that this is the _total_ size of the file, including Xing frame ... and ID3 frames...
204
						unsigned long xing_bytes = make_long(fr->bsbuf, lame_offset);					/* We assume that this is the _total_ size of the file, including Xing frame ... and ID3 frames...
221
					   It's not that clearly documented... */
205
						   It's not that clearly documented... */
222
					if(fr->rdat.filelen < 1)
206
						if(fr->rdat.filelen < 1)
223
					fr->rdat.filelen = (off_t) xing_bytes; /* One could start caring for overflow here. */
207
						fr->rdat.filelen = (off_t) xing_bytes; /* One could start caring for overflow here. */
224
					else
208
						else
Line 236... Line 220...
236
						}
220
							}
237
					}
221
						}
Line 238... Line 222...
238
 
222
 
239
					if(VERBOSE3)
223
						if(VERBOSE3)
-
 
224
						fprintf(stderr, "Note: Xing: %lu bytes\n", (long unsigned)xing_bytes);
Line 240... Line 225...
240
					fprintf(stderr, "Note: Xing: %lu bytes\n", (long unsigned)xing_bytes);
225
					}
241
 
226
 
242
					lame_offset += 4;
227
					lame_offset += 4;
243
				}
228
				}
Line 254... Line 239...
254
						fprintf(stderr, "Note: Xing: quality = %lu\n", (long unsigned)xing_quality);
239
						fprintf(stderr, "Note: Xing: quality = %lu\n", (long unsigned)xing_quality);
255
					}
240
					}
256
					lame_offset += 4;
241
					lame_offset += 4;
257
				}
242
				}
258
				/* I guess that either 0 or LAME extra data follows */
243
				/* I guess that either 0 or LAME extra data follows */
259
				/* there may this crc16 be floating around... (?) */
-
 
260
				if(fr->bsbuf[lame_offset] != 0)
244
				if(fr->bsbuf[lame_offset] != 0)
261
				{
245
				{
262
					unsigned char lame_vbr;
246
					unsigned char lame_vbr;
263
					float replay_gain[2] = {0,0};
247
					float replay_gain[2] = {0,0};
264
					float peak = 0;
248
					float peak = 0;
Line 267... Line 251...
267
					memcpy(nb, fr->bsbuf+lame_offset, 9);
251
					memcpy(nb, fr->bsbuf+lame_offset, 9);
268
					nb[9] = 0;
252
					nb[9] = 0;
269
					if(VERBOSE3) fprintf(stderr, "Note: Info: Encoder: %s\n", nb);
253
					if(VERBOSE3) fprintf(stderr, "Note: Info: Encoder: %s\n", nb);
270
					if(!strncmp("LAME", nb, 4))
254
					if(!strncmp("LAME", nb, 4))
271
					{
255
					{
-
 
256
						/* Lame versions before 3.95.1 used 83 dB reference level, later versions 89 dB.
-
 
257
						   We stick with 89 dB as being "normal", adding 6 dB. */
-
 
258
						unsigned int major, minor;
-
 
259
						char rest[6];
-
 
260
						rest[0] = 0;
-
 
261
						if(sscanf(nb+4, "%u.%u%s", &major, &minor, rest) >= 2)
-
 
262
						{
-
 
263
							debug3("LAME: %u/%u/%s", major, minor, rest);
-
 
264
							/* We cannot detect LAME 3.95 reliably (same version string as 3.95.1), so this is a blind spot.
-
 
265
							   Everything < 3.95 is safe, though. */
-
 
266
							if(major < 3 || (major == 3 && minor < 95))  /* || (major == 3 && minor == 95 && rest[0] == 0)) */
-
 
267
							{
272
						gain_offset = 6;
268
								gain_offset = 6;
-
 
269
								if(VERBOSE3) fprintf(stderr, "Note: Info: Old LAME detected, using ReplayGain preamp of %f dB.\n", gain_offset);
-
 
270
							}
-
 
271
						}
273
						debug("TODO: finish lame detetcion...");
272
						else if(VERBOSE3) fprintf(stderr, "Note: Info: Cannot determine LAME version.\n");
274
					}
273
					}
275
					lame_offset += 9;
274
					lame_offset += 9;
276
					/* the 4 big bits are tag revision, the small bits vbr method */
275
					/* the 4 big bits are tag revision, the small bits vbr method */
277
					lame_vbr = fr->bsbuf[lame_offset] & 15;
276
					lame_vbr = fr->bsbuf[lame_offset] & 15;
278
					if(VERBOSE3)
277
					if(VERBOSE3)
Line 328... Line 327...
328
							unsigned char gt = fr->bsbuf[lame_offset] >> 5; /* only first 3 bits */
327
							unsigned char gt = fr->bsbuf[lame_offset] >> 5; /* only first 3 bits */
329
							if(gt == 1) gt = 0; /* radio */
328
							if(gt == 1) gt = 0; /* radio */
330
							else if(gt == 2) gt = 1; /* audiophile */
329
							else if(gt == 2) gt = 1; /* audiophile */
331
							else continue;
330
							else continue;
332
							/* get the 9 bits into a number, divide by 10, multiply sign... happy bit banging */
331
							/* get the 9 bits into a number, divide by 10, multiply sign... happy bit banging */
333
							replay_gain[0] = (float) ((fr->bsbuf[lame_offset] & 0x2) ? -0.1 : 0.1) * (make_short(fr->bsbuf, lame_offset) & 0x1f);
332
							replay_gain[gt] = (float) ((fr->bsbuf[lame_offset] & 0x2) ? -0.1 : 0.1) * (make_short(fr->bsbuf, lame_offset) & 0x1ff);
-
 
333
							/* If this is an automatic value from LAME (or whatever), the automatic gain offset applies.
-
 
334
							   If a user or whoever set the value, do not touch it! 011 is automatic origin. */
-
 
335
							if(origin == 3) replay_gain[gt] += gain_offset;
334
						}
336
						}
335
						lame_offset += 2;
337
						lame_offset += 2;
336
					}
338
					}
337
					if(VERBOSE3) 
339
					if(VERBOSE3) 
338
					{
340
					{
Line 361... Line 363...
361
					        ((((int) fr->bsbuf[lame_offset]) << 4) | (((int) fr->bsbuf[lame_offset+1]) >> 4)),
363
					        ((((int) fr->bsbuf[lame_offset]) << 4) | (((int) fr->bsbuf[lame_offset+1]) >> 4)),
362
					        (((((int) fr->bsbuf[lame_offset+1]) << 8) | (((int) fr->bsbuf[lame_offset+2]))) & 0xfff) );
364
					        (((((int) fr->bsbuf[lame_offset+1]) << 8) | (((int) fr->bsbuf[lame_offset+2]))) & 0xfff) );
363
					#ifdef GAPLESS
365
					#ifdef GAPLESS
364
					if(fr->p.flags & MPG123_GAPLESS)
366
					if(fr->p.flags & MPG123_GAPLESS)
365
					{
367
					{
366
						off_t length = fr->track_frames * spf(fr);
-
 
367
						off_t skipbegin = GAPLESS_DELAY + ((((int) fr->bsbuf[lame_offset]) << 4) | (((int) fr->bsbuf[lame_offset+1]) >> 4));
368
						off_t skipbegin = ((((int) fr->bsbuf[lame_offset]) << 4) | (((int) fr->bsbuf[lame_offset+1]) >> 4));
368
						off_t skipend = -GAPLESS_DELAY + (((((int) fr->bsbuf[lame_offset+1]) << 8) | (((int) fr->bsbuf[lame_offset+2]))) & 0xfff);
369
						off_t skipend = (((((int) fr->bsbuf[lame_offset+1]) << 8) | (((int) fr->bsbuf[lame_offset+2]))) & 0xfff);
369
						debug3("preparing gapless mode for layer3: length %lu, skipbegin %lu, skipend %lu", 
-
 
370
								(long unsigned)length, (long unsigned)skipbegin, (long unsigned)skipend);
-
 
371
						if(length > 1)
-
 
372
						frame_gapless_init(fr, skipbegin, (skipend < length) ? length-skipend : length);
370
						frame_gapless_init(fr, fr->track_frames, skipbegin, skipend);
373
					}
371
					}
374
					#endif
372
					#endif
375
				}
373
				}
376
				/* switch buffer back ... */
374
				/* switch buffer back ... */
377
				fr->bsbuf = fr->bsspace[fr->bsnum]+512;
375
				fr->bsbuf = fr->bsspace[fr->bsnum]+512;
Line 384... Line 382...
384
}
382
}
Line 385... Line 383...
385
 
383
 
386
/* Just tell if the header is some mono. */
384
/* Just tell if the header is some mono. */
387
static int header_mono(unsigned long newhead)
385
static int header_mono(unsigned long newhead)
388
{
386
{
389
	return ((newhead>>6)&0x3) == MPG_MD_MONO ? TRUE : FALSE;
387
	return HDR_CHANNEL_VAL(newhead) == MPG_MD_MONO ? TRUE : FALSE;
Line 390... Line -...
390
}
-
 
391
 
388
}
392
/*
389
 
393
	That's a big one: read the next frame. 1 is success, <= 0 is some error
-
 
394
	Special error READER_MORE means: Please feed more data and try again.
-
 
395
*/
390
/* true if the two headers will work with the same decoding routines */
396
int read_frame(mpg123_handle *fr)
-
 
397
{
-
 
398
	/* TODO: rework this thing */
-
 
399
	unsigned long newhead;
-
 
400
	off_t framepos;
391
static int head_compatible(unsigned long fred, unsigned long bret)
401
	int ret;
-
 
402
	/* stuff that needs resetting if complete frame reading fails */
-
 
403
	int oldsize  = fr->framesize;
392
{
-
 
393
	return ( (fred & HDR_CMPMASK) == (bret & HDR_CMPMASK)
Line -... Line 394...
-
 
394
		&&       header_mono(fred) == header_mono(bret)    );
-
 
395
}
-
 
396
 
-
 
397
static void halfspeed_prepare(mpg123_handle *fr)
-
 
398
{
-
 
399
	/* save for repetition */
-
 
400
	if(fr->p.halfspeed && fr->lay == 3)
-
 
401
	{
-
 
402
		debug("halfspeed - reusing old bsbuf ");
-
 
403
		memcpy (fr->ssave, fr->bsbuf, fr->ssize);
-
 
404
	}
-
 
405
}
-
 
406
 
404
	int oldphase = fr->halfphase;
407
/* If this returns 1, the next frame is the repetition. */
405
	fr->fsizeold=fr->framesize;       /* for Layer3 */
408
static int halfspeed_do(mpg123_handle *fr)
406
 
409
{
407
	/* Speed-down hack: Play it again, Sam (the frame, I mean). */
410
	/* Speed-down hack: Play it again, Sam (the frame, I mean). */
408
	if (fr->p.halfspeed) 
411
	if (fr->p.halfspeed) 
Line 421... Line 424...
421
		else
424
		else
422
		{
425
		{
423
			fr->halfphase = fr->p.halfspeed - 1;
426
			fr->halfphase = fr->p.halfspeed - 1;
424
		}
427
		}
425
	}
428
	}
426
 
-
 
427
read_again:
-
 
428
	debug2("trying to get frame %li at 0x%lx", (long)fr->num+1, (unsigned long)fr->rd->tell(fr));
-
 
429
	if((ret = fr->rd->head_read(fr,&newhead)) <= 0){ debug("need more?"); goto read_frame_bad;}
-
 
430
 
-
 
431
init_resync:
-
 
432
 
-
 
433
	fr->header_change = 2; /* output format change is possible... */
-
 
434
	if(fr->oldhead)        /* check a following header for change */
-
 
435
	{
-
 
436
		if(fr->oldhead == newhead) fr->header_change = 0;
-
 
437
		else
-
 
438
		/* If they have the same sample rate. Note that only is _not_ the case for the first header, as we enforce sample rate match for following frames.
-
 
439
			 So, during one stream, only change of stereoness is possible and indicated by header_change == 2. */
-
 
440
		if((fr->oldhead & HDRSAMPMASK) == (newhead & HDRSAMPMASK))
-
 
441
		{
-
 
442
			/* Now if both channel modes are mono or both stereo, it's no big deal. */
-
 
443
			if( header_mono(fr->oldhead) == header_mono(newhead))
-
 
444
			fr->header_change = 1;
-
 
445
		}
-
 
446
	}
-
 
447
 
-
 
448
#ifdef SKIP_JUNK
-
 
449
	/* watch out for junk/tags on beginning of stream by invalid header */
-
 
450
	if(!fr->firsthead && !head_check(newhead)) {
-
 
451
		int i;
-
 
452
 
-
 
453
		/* check for id3v2; first three bytes (of 4) are "ID3" */
-
 
454
		if((newhead & (unsigned long) 0xffffff00) == (unsigned long) 0x49443300)
-
 
455
		{
-
 
456
			int id3ret = 0;
-
 
457
			id3ret = parse_new_id3(fr, newhead);
-
 
458
			if     (id3ret < 0){ debug("need more?"); ret = id3ret; goto read_frame_bad; }
-
 
459
#ifndef NO_ID3V2
-
 
460
			else if(id3ret > 0){ debug("got ID3v2"); fr->metaflags  |= MPG123_NEW_ID3|MPG123_ID3; }
-
 
461
			else debug("no useful ID3v2");
-
 
462
#endif
-
 
463
 
-
 
464
			fr->oldhead = 0;
-
 
465
			goto read_again; /* Also in case of invalid ID3 tag (ret==0), try to get on track again. */
-
 
466
		}
-
 
467
		else if(VERBOSE2 && fr->silent_resync == 0) fprintf(stderr,"Note: Junk at the beginning (0x%08lx)\n",newhead);
-
 
468
 
-
 
469
		/* I even saw RIFF headers at the beginning of MPEG streams ;( */
-
 
470
		if(newhead == ('R'<<24)+('I'<<16)+('F'<<8)+'F') {
-
 
471
			if(VERBOSE2 && fr->silent_resync == 0) fprintf(stderr, "Note: Looks like a RIFF header.\n");
-
 
472
 
-
 
473
			if((ret=fr->rd->head_read(fr,&newhead))<=0){ debug("need more?"); goto read_frame_bad; }
-
 
474
 
-
 
475
			while(newhead != ('d'<<24)+('a'<<16)+('t'<<8)+'a')
-
 
476
			{
-
 
477
				if((ret=fr->rd->head_shift(fr,&newhead))<=0){ debug("need more?"); goto read_frame_bad; }
-
 
478
			}
-
 
479
			if((ret=fr->rd->head_read(fr,&newhead))<=0){ debug("need more?"); goto read_frame_bad; }
-
 
480
 
-
 
481
			if(VERBOSE2 && fr->silent_resync == 0) fprintf(stderr,"Note: Skipped RIFF header!\n");
-
 
482
 
-
 
483
			fr->oldhead = 0;
-
 
484
			goto read_again;
-
 
485
		}
-
 
486
		/* unhandled junk... just continue search for a header */
-
 
487
		/* step in byte steps through next 64K */
-
 
488
		debug("searching for header...");
-
 
489
		for(i=0;i<65536;i++) {
-
 
490
			if((ret=fr->rd->head_shift(fr,&newhead))<=0){ debug("need more?"); goto read_frame_bad; }
-
 
491
			/* if(head_check(newhead)) */
-
 
492
			if(head_check(newhead) && decode_header(fr, newhead))
-
 
493
				break;
-
 
494
		}
-
 
495
		if(i == 65536)
-
 
496
		{
-
 
497
			if(NOQUIET) error("Giving up searching valid MPEG header after 64K of junk.");
-
 
498
			return 0;
429
	return 0;
499
		}
430
}
500
		else debug("hopefully found one...");
-
 
-
 
431
 
501
		/* 
432
/* 
502
		 * should we additionaly check, whether a new frame starts at
433
	Temporary macro until we got this worked out.
503
		 * the next expected position? (some kind of read ahead)
434
	Idea is to filter out special return values that shall trigger direct jumps to end / resync / read again. 
504
		 * We could implement this easily, at least for files.
435
	Particularily, the generic ret==PARSE_BAD==0 and ret==PARSE_GOOD==1 are not affected.
505
		 */
436
*/
-
 
437
#define JUMP_CONCLUSION(ret) \
-
 
438
{ \
-
 
439
if(ret < 0){ debug1("%s", ret == MPG123_NEED_MORE ? "need more" : "read error"); goto read_frame_bad; } \
-
 
440
else if(ret == PARSE_AGAIN) goto read_again; \
-
 
441
else if(ret == PARSE_RESYNC) goto init_resync; \
-
 
442
else if(ret == PARSE_END) goto read_frame_bad; \
506
	}
443
}
507
#endif
-
 
Line 508... Line -...
508
 
-
 
509
	/* first attempt of read ahead check to find the real first header; cannot believe what junk is out there! */
-
 
510
	/* for now, a spurious first free format header screws up here; need free format support for detecting false free format headers... */
-
 
511
	if(!fr->firsthead && fr->rdat.flags & (READER_SEEKABLE|READER_BUFFERED) && head_check(newhead) && decode_header(fr, newhead))
444
 
512
	{
-
 
513
		unsigned long nexthead = 0;
-
 
514
		int hd = 0;
-
 
515
		off_t start = fr->rd->tell(fr);
-
 
516
		debug2("doing ahead check with BPF %d at %li", fr->framesize+4, (long)start);
445
/*
517
		/* step framesize bytes forward and read next possible header*/
-
 
518
		if((ret=fr->rd->skip_bytes(fr, fr->framesize))<0)
-
 
519
		{
-
 
520
			if(ret==READER_ERROR && NOQUIET) error("cannot seek!");
-
 
521
			goto read_frame_bad;
-
 
522
		}
-
 
523
		hd = fr->rd->head_read(fr,&nexthead);
-
 
524
		if(hd==MPG123_NEED_MORE){ debug("need more?"); ret = hd; goto read_frame_bad; }
-
 
525
		if((ret=fr->rd->back_bytes(fr, fr->rd->tell(fr)-start))<0)
-
 
526
		{
446
	That's a big one: read the next frame. 1 is success, <= 0 is some error
527
			if(ret==READER_ERROR && NOQUIET) error("cannot seek!");
-
 
528
			else debug("need more?"); 
-
 
529
			goto read_frame_bad;
-
 
530
		}
-
 
531
		debug1("After fetching next header, at %li", (long)fr->rd->tell(fr));
-
 
532
		if(!hd)
-
 
533
		{
-
 
534
			if(NOQUIET) warning("cannot read next header, a one-frame stream? Duh...");
-
 
535
		}
-
 
536
		else
-
 
537
		{
-
 
538
			debug2("does next header 0x%08lx match first 0x%08lx?", nexthead, newhead);
-
 
539
			/* not allowing free format yet */
-
 
540
			if(!head_check(nexthead) || (nexthead & HDRCMPMASK) != (newhead & HDRCMPMASK))
-
 
541
			{
-
 
542
				debug("No, the header was not valid, start from beginning...");
-
 
543
				fr->oldhead = 0; /* start over */
-
 
544
				/* try next byte for valid header */
-
 
545
				if((ret=fr->rd->back_bytes(fr, 3))<0)
-
 
546
				{
-
 
547
					if(NOQUIET) error("cannot seek!");
-
 
548
					else debug("need more?"); 
-
 
549
					goto read_frame_bad;
-
 
550
				}
-
 
551
				goto read_again;
-
 
552
			}
-
 
553
		}
447
	Special error READER_MORE means: Please feed more data and try again.
554
	}
-
 
555
 
-
 
556
	/* why has this head check been avoided here before? */
448
*/
557
	if(!head_check(newhead))
-
 
558
	{
-
 
559
		/* and those ugly ID3 tags */
-
 
560
		if((newhead & 0xffffff00) == ('T'<<24)+('A'<<16)+('G'<<8))
-
 
561
		{
-
 
562
			fr->id3buf[0] = (unsigned char) ((newhead >> 24) & 0xff);
-
 
563
			fr->id3buf[1] = (unsigned char) ((newhead >> 16) & 0xff);
-
 
564
			fr->id3buf[2] = (unsigned char) ((newhead >> 8)  & 0xff);
-
 
565
			fr->id3buf[3] = (unsigned char) ( newhead        & 0xff);
-
 
566
			if((ret=fr->rd->fullread(fr,fr->id3buf+4,124)) < 0){ debug("need more?"); goto read_frame_bad; }
-
 
567
			fr->metaflags  |= MPG123_NEW_ID3|MPG123_ID3;
-
 
568
			fr->rdat.flags |= READER_ID3TAG; /* that marks id3v1 */
-
 
569
			if (VERBOSE2) fprintf(stderr,"Note: Skipped ID3 Tag!\n");
-
 
570
			goto read_again;
-
 
571
		}
-
 
572
		/* duplicated code from above! */
-
 
573
		/* check for id3v2; first three bytes (of 4) are "ID3" */
-
 
574
		if((newhead & (unsigned long) 0xffffff00) == (unsigned long) 0x49443300)
449
int read_frame(mpg123_handle *fr)
-
 
450
{
575
		{
451
	/* TODO: rework this thing */
576
			int id3length = 0;
452
	int freeformat_count = 0;
-
 
453
	unsigned long newhead;
-
 
454
	off_t framepos;
577
			id3length = parse_new_id3(fr, newhead);
455
	int ret;
-
 
456
	/* stuff that needs resetting if complete frame reading fails */
-
 
457
	int oldsize  = fr->framesize;
Line 578... Line 458...
578
			if(id3length < 0){ debug("need more?"); ret = id3length; goto read_frame_bad; }
458
	int oldphase = fr->halfphase;
579
 
-
 
580
			fr->metaflags  |= MPG123_NEW_ID3|MPG123_ID3;
-
 
581
			goto read_again;
459
 
582
		}
-
 
583
		else if(NOQUIET && fr->silent_resync == 0)
460
	/* The counter for the search-first-header loop.
584
		{
461
	   It is persistent outside the loop to prevent seemingly endless loops
585
			fprintf(stderr,"Note: Illegal Audio-MPEG-Header 0x%08lx at offset 0x%lx.\n",
-
 
Line 586... Line -...
586
				newhead, (long unsigned int)fr->rd->tell(fr)-4);
-
 
587
		}
-
 
588
 
-
 
589
		if(NOQUIET && (newhead & 0xffffff00) == ('b'<<24)+('m'<<16)+('p'<<8)) fprintf(stderr,"Note: Could be a BMP album art.\n");
462
	   when repeatedly headers are found that do not have valid followup headers. */
590
		/* Do resync if not forbidden by flag.
-
 
591
		I used to have a check for not-icy-meta here, but concluded that the desync issues came from a reader bug, not the stream. */
-
 
592
		if( !(fr->p.flags & MPG123_NO_RESYNC) )
-
 
Line 593... Line -...
593
		{
-
 
594
			long try = 0;
463
	long headcount = 0;
Line 595... Line -...
595
			long limit = fr->p.resync_limit;
-
 
596
			
-
 
597
			/* If a resync is needed the bitreservoir of previous frames is no longer valid */
-
 
598
			fr->bitreservoir = 0;
464
 
599
 
-
 
600
			/* TODO: make this more robust, I'd like to cat two mp3 fragments together (in a dirty way) and still have mpg123 beign able to decode all it somehow. */
465
	fr->fsizeold=fr->framesize;       /* for Layer3 */
601
			if(NOQUIET && fr->silent_resync == 0) fprintf(stderr, "Note: Trying to resync...\n");
466
 
602
			/* Read more bytes until we find something that looks
-
 
603
			 reasonably like a valid header.  This is not a
-
 
604
			 perfect strategy, but it should get us back on the
-
 
605
			 track within a short time (and hopefully without
467
	if(halfspeed_do(fr) == 1) return 1;
Line 606... Line 468...
606
			 too much distortion in the audio output).  */
468
 
607
			do
-
 
608
			{
-
 
609
				++try;
469
read_again:
Line 610... Line 470...
610
				if(limit >= 0 && try >= limit) break;				
470
	/* In case we are looping to find a valid frame, discard any buffered data before the current position.
611
 
-
 
612
				if((ret=fr->rd->head_shift(fr,&newhead)) <= 0)
-
 
Line -... Line 471...
-
 
471
	   This is essential to prevent endless looping, always going back to the beginning when feeder buffer is exhausted. */
613
				{
472
	if(fr->rd->forget != NULL) fr->rd->forget(fr);
614
					debug("need more?");
473
 
615
					if(NOQUIET) fprintf (stderr, "Note: Hit end of (available) data during resync.\n");
474
	debug2("trying to get frame %"OFF_P" at %"OFF_P, (off_p)fr->num+1, (off_p)fr->rd->tell(fr));
-
 
475
	if((ret = fr->rd->head_read(fr,&newhead)) <= 0){ debug1("need more? (%i)", ret); goto read_frame_bad;}
-
 
476
 
616
 
477
init_resync:
-
 
478
 
-
 
479
	fr->header_change = 2; /* output format change is possible... */
617
					goto read_frame_bad;
480
	if(fr->oldhead)        /* check a following header for change */
618
				}
-
 
619
				if(VERBOSE3) debug3("resync try %li at 0x%lx, got newhead 0x%08lx", try, (unsigned long)fr->rd->tell(fr),  newhead);
-
 
620
 
-
 
621
				if(!fr->oldhead)
-
 
622
				{
-
 
623
					debug("going to init_resync...");
-
 
624
					goto init_resync;       /* "considered harmful", eh? */
-
 
625
				}
-
 
626
				/* we should perhaps collect a list of valid headers that occured in file... there can be more */
-
 
627
				/* Michael's new resync routine seems to work better with the one frame readahead (and some input buffering?) */
-
 
628
			} while
-
 
Line 629... Line -...
629
			(
-
 
630
				!head_check(newhead) /* Simply check for any valid header... we have the readahead to get it straight now(?) */
-
 
631
				/*   (newhead & HDRCMPMASK) != (fr->oldhead & HDRCMPMASK)
481
	{
632
				&& (newhead & HDRCMPMASK) != (fr->firsthead & HDRCMPMASK)*/
-
 
633
			);
-
 
634
			/* too many false positives 
482
		if(fr->oldhead == newhead) fr->header_change = 0;
635
			}while (!(head_check(newhead) && decode_header(fr, newhead))); */
-
 
636
			if(NOQUIET && fr->silent_resync == 0) fprintf (stderr, "Note: Skipped %li bytes in input.\n", try);
-
 
637
 
-
 
638
			if(limit >= 0 && try >= limit)
483
		else
639
			{
-
 
640
				if(NOQUIET)
484
		/* Headers that match in this test behave the same for the outside world.
641
				error1("Giving up resync after %li bytes - your stream is not nice... (maybe increasing resync limit could help).", try);
485
		   namely: same decoding routines, same amount of decoded data. */
642
 
486
		if(head_compatible(fr->oldhead, newhead))
643
				fr->err = MPG123_RESYNC_FAIL;
-
 
644
				return READER_ERROR;
487
		fr->header_change = 1;
645
			}
-
 
646
			else
-
 
Line 647... Line 488...
647
			{
488
	}
648
				debug1("Found valid header 0x%lx... unsetting firsthead to reinit stream.", newhead);
489
 
649
				fr->firsthead = 0;
490
#ifdef SKIP_JUNK
-
 
491
	if(!fr->firsthead && !head_check(newhead))
-
 
492
	{
-
 
493
		ret = skip_junk(fr, &newhead, &headcount);
-
 
494
		JUMP_CONCLUSION(ret);
-
 
495
	}
-
 
496
#endif
-
 
497
 
650
				goto init_resync;
498
	ret = head_check(newhead);
Line 651... Line 499...
651
			}
499
	if(ret) ret = decode_header(fr, newhead, &freeformat_count);
652
		}
500
 
653
		else
501
	JUMP_CONCLUSION(ret); /* That only continues for ret == PARSE_BAD or PARSE_GOOD. */
654
		{
-
 
655
			if(NOQUIET) error("not attempting to resync...");
502
	if(ret == PARSE_BAD)
656
 
-
 
657
			fr->err = MPG123_OUT_OF_SYNC;
-
 
658
			return READER_ERROR;
-
 
659
		}
-
 
660
	}
-
 
661
 
-
 
662
	if (!fr->firsthead)
-
 
663
	{
503
	{ /* Header was not good. */
664
		if(!decode_header(fr,newhead))
-
 
665
		{
504
		ret = wetwork(fr, &newhead); /* Messy stuff, handle junk, resync ... */
666
			if(NOQUIET) error("decode header failed before first valid one, going to read again");
505
		JUMP_CONCLUSION(ret);
Line -... Line 506...
-
 
506
		/* Normally, we jumped already. If for some reason everything's fine to continue, do continue. */
-
 
507
		if(ret != PARSE_GOOD) goto read_frame_bad;
667
 
508
	}
668
			goto read_again;
509
 
669
		}
510
	if(!fr->firsthead)
670
	}
511
	{
671
	else
512
		ret = do_readahead(fr, newhead);
Line 725... Line 566...
725
	if(++fr->mean_frames != 0)
566
	if(++fr->mean_frames != 0)
726
	{
567
	{
727
		fr->mean_framesize = ((fr->mean_frames-1)*fr->mean_framesize+compute_bpf(fr)) / fr->mean_frames ;
568
		fr->mean_framesize = ((fr->mean_frames-1)*fr->mean_framesize+compute_bpf(fr)) / fr->mean_frames ;
728
	}
569
	}
729
	++fr->num; /* 0 for first frame! */
570
	++fr->num; /* 0 for first frame! */
730
	debug4("Frame %li %08lx %i, next filepos=0x%lx", 
571
	debug4("Frame %"OFF_P" %08lx %i, next filepos=%"OFF_P, 
731
	(long)fr->num, newhead, fr->framesize, (long unsigned)fr->rd->tell(fr));
572
	(off_p)fr->num, newhead, fr->framesize, (off_p)fr->rd->tell(fr));
732
	/* save for repetition */
573
	if(!(fr->state_flags & FRAME_FRANKENSTEIN) && (
733
	if(fr->p.halfspeed && fr->lay == 3)
574
		(fr->track_frames > 0 && fr->num >= fr->track_frames)
-
 
575
#ifdef GAPLESS
-
 
576
		|| (fr->gapless_frames > 0 && fr->num >= fr->gapless_frames)
-
 
577
#endif
-
 
578
	))
734
	{
579
	{
735
		debug("halfspeed - reusing old bsbuf ");
580
		fr->state_flags |= FRAME_FRANKENSTEIN;
-
 
581
		if(NOQUIET) fprintf(stderr, "\nWarning: Encountered more data after announced end of track (frame %"OFF_P"/%"OFF_P"). Frankenstein!\n", (off_p)fr->num, 
-
 
582
#ifdef GAPLESS
736
		memcpy (fr->ssave, fr->bsbuf, fr->ssize);
583
		fr->gapless_frames > 0 ? (off_p)fr->gapless_frames : 
-
 
584
#endif
-
 
585
		(off_p)fr->track_frames);
737
	}
586
	}
Line -... Line 587...
-
 
587
 
-
 
588
	halfspeed_prepare(fr);
738
 
589
 
-
 
590
	/* index the position */
739
	/* index the position */
591
	fr->input_offset = framepos;
740
#ifdef FRAME_INDEX
592
#ifdef FRAME_INDEX
741
	/* Keep track of true frame positions in our frame index.
593
	/* Keep track of true frame positions in our frame index.
742
	   but only do so when we are sure that the frame number is accurate... */
594
	   but only do so when we are sure that the frame number is accurate... */
743
	if(fr->accurate && FI_NEXT(fr->index, fr->num))
595
	if((fr->state_flags & FRAME_ACCURATE) && FI_NEXT(fr->index, fr->num))
744
	fi_add(&fr->index, framepos);
596
	fi_add(&fr->index, framepos);
Line 745... Line 597...
745
#endif
597
#endif
Line 746... Line 598...
746
 
598
 
Line 747... Line 599...
747
	if(fr->silent_resync > 0) --fr->silent_resync;
599
	if(fr->silent_resync > 0) --fr->silent_resync;
748
 
600
 
Line -... Line 601...
-
 
601
	if(fr->rd->forget != NULL) fr->rd->forget(fr);
-
 
602
 
749
	if(fr->rd->forget != NULL) fr->rd->forget(fr);
603
	fr->to_decode = fr->to_ignore = TRUE;
750
 
604
	if(fr->error_protection) fr->crc = getbits(fr, 16); /* skip crc */
-
 
605
 
-
 
606
	fr->oldhead = newhead;
-
 
607
 
-
 
608
	return 1;
751
	fr->to_decode = fr->to_ignore = TRUE;
609
read_frame_bad:
752
	if(fr->error_protection) fr->crc = getbits(fr, 16); /* skip crc */
610
	/* Also if we searched for valid data in vein, we can forget skipped data.
753
 
611
	   Otherwise, the feeder would hold every dead old byte in memory until the first valid frame! */
754
	return 1;
612
	if(fr->rd->forget != NULL) fr->rd->forget(fr);
-
 
613
 
755
read_frame_bad:
614
	fr->silent_resync = 0;
756
	fr->silent_resync = 0;
615
	if(fr->err == MPG123_OK) fr->err = MPG123_ERR_READER;
Line 757... Line 616...
757
	if(fr->err == MPG123_OK) fr->err = MPG123_ERR_READER;
616
	fr->framesize = oldsize;
758
	fr->framesize = oldsize;
617
	fr->halfphase = oldphase;
759
	fr->halfphase = oldphase;
618
	/* That return code might be inherited from some feeder action, or reader error. */
-
 
619
	return ret;
-
 
620
}
-
 
621
 
760
	return ret;
622
 
761
}
623
/*
762
 
624
 * read ahead and find the next MPEG header, to guess framesize
763
 
625
 * return value: success code
764
/*
626
 *  PARSE_GOOD: found a valid frame size (stored in the handle).
765
 * read ahead and find the next MPEG header, to guess framesize
627
 * <0: error codes, possibly from feeder buffer (NEED_MORE)
766
 * return value: guessed framesize
628
 *  PARSE_BAD: cannot get the framesize for some reason and shall silentry try the next possible header (if this is no free format stream after all...)
767
 */
629
 */
768
static long guess_freeformat_framesize(mpg123_handle *fr)
630
static int guess_freeformat_framesize(mpg123_handle *fr)
-
 
631
{
769
{
632
	long i;
770
	long i;
633
	int ret;
771
	int ret;
-
 
772
	unsigned long head;
634
	unsigned long head;
773
	if(!(fr->rdat.flags & (READER_SEEKABLE|READER_BUFFERED)))
635
	if(!(fr->rdat.flags & (READER_SEEKABLE|READER_BUFFERED)))
Line 774... Line 636...
774
	{
636
	{
775
		if(NOQUIET) error("Cannot look for freeformat frame size with non-seekable and non-buffered stream!");
637
		if(NOQUIET) error("Cannot look for freeformat frame size with non-seekable and non-buffered stream!");
776
		return -1;
-
 
777
	}
-
 
778
	/* FIXME: We need proper handling/passing of MPG123_NEED_MORE! */
-
 
779
	if((ret=fr->rd->head_read(fr,&head))<=0)
-
 
780
	return -1;
-
 
781
 
-
 
782
	/* We are already 4 bytes into it */
-
 
783
	for(i=4;i<65536;i++) {
-
 
784
		if((ret=fr->rd->head_shift(fr,&head))<=0)
-
 
785
		{
-
 
786
			return -1;
-
 
787
		}
-
 
788
		if(head_check(head))
-
 
789
		{
-
 
790
			int sampling_frequency,mpeg25,lsf;
638
 
791
			
-
 
792
			if(head & (1<<20))
-
 
793
			{
-
 
794
				lsf = (head & (1<<19)) ? 0x0 : 0x1;
-
 
795
				mpeg25 = 0;
-
 
796
			}
-
 
797
			else
-
 
798
			{
639
		return PARSE_BAD;
Line -... Line 640...
-
 
640
	}
799
				lsf = 1;
641
	if((ret=fr->rd->head_read(fr,&head))<=0)
800
				mpeg25 = 1;
642
	return ret;
801
			}
643
 
802
			
644
	/* We are already 4 bytes into it */
803
			if(mpeg25)
645
	for(i=4;i
804
				sampling_frequency = 6 + ((head>>10)&0x3);
646
	{
805
			else
647
		if((ret=fr->rd->head_shift(fr,&head))<=0) return ret;
806
				sampling_frequency = ((head>>10)&0x3) + (lsf*3);
648
 
807
			
649
		/* No head_check needed, the mask contains all relevant bits. */
808
			if((lsf==fr->lsf) && (mpeg25==fr->mpeg25) && (sampling_frequency == fr->sampling_frequency))
650
		if((head & HDR_SAMEMASK) == (fr->oldhead & HDR_SAMEMASK))
Line 809... Line 651...
809
			{
651
		{
810
				fr->rd->back_bytes(fr,i+1);
652
			fr->rd->back_bytes(fr,i+1);
811
				return i-3;
653
			fr->framesize = i-3;
-
 
654
			return PARSE_GOOD; /* Success! */
-
 
655
		}
-
 
656
	}
-
 
657
	fr->rd->back_bytes(fr,i);
-
 
658
	return PARSE_BAD;
812
			}
659
}
813
		}
660
 
814
	}
661
 
-
 
662
/*
815
	fr->rd->back_bytes(fr,i);
663
 * decode a header and write the information
816
	return -1;
664
 * into the frame structure
817
}
665
 * Return values are compatible with those of read_frame, namely:
818
 
-
 
819
 
-
 
820
/*
666
 *  1: success
-
 
667
 *  0: no valid header
821
 * decode a header and write the information
668
 * <0: some error
822
 * into the frame structure
669
 * You are required to do a head_check() before calling!
823
 */
670
 */
824
static int decode_header(mpg123_handle *fr,unsigned long newhead)
671
static int decode_header(mpg123_handle *fr,unsigned long newhead, int *freeformat_count)
825
{
672
{
826
	if(!head_check(newhead))
673
#ifdef DEBUG /* Do not waste cycles checking the header twice all the time. */
827
	{
674
	if(!head_check(newhead))
828
		if(NOQUIET) error("tried to decode obviously invalid header");
675
	{
829
 
676
		error1("trying to decode obviously invalid header 0x%08lx", newhead);
830
		return 0;
677
	}
Line 831... Line 678...
831
	}
678
#endif
832
	if( newhead & (1<<20) )
679
	if(HDR_VERSION_VAL(newhead) & 0x2)
833
	{
680
	{
834
		fr->lsf = (newhead & (1<<19)) ? 0x0 : 0x1;
681
		fr->lsf = (HDR_VERSION_VAL(newhead) & 0x1) ? 0 : 1;
835
		fr->mpeg25 = 0;
682
		fr->mpeg25 = 0;
836
	}
683
	}
837
	else
684
	else
838
	{
685
	{
839
		fr->lsf = 1;
-
 
840
		fr->mpeg25 = 1;
-
 
841
	}
-
 
842
 
-
 
843
	if(   (fr->p.flags & MPG123_NO_RESYNC) || !fr->oldhead
-
 
844
	   || (((fr->oldhead>>19)&0x3) ^ ((newhead>>19)&0x3))  )
-
 
845
	{
686
		fr->lsf = 1;
846
		/* If "tryresync" is false, assume that certain
687
		fr->mpeg25 = 1;
847
		parameters do not change within the stream!
688
	}
848
		Force an update if lsf or mpeg25 settings
689
 
849
		have changed. */
690
	if(   (fr->p.flags & MPG123_NO_RESYNC) || !fr->oldhead
Line 850... Line 691...
850
		fr->lay = 4-((newhead>>17)&3);
691
	   || (HDR_VERSION_VAL(fr->oldhead) != HDR_VERSION_VAL(newhead)) )
-
 
692
	{
851
		if( ((newhead>>10)&0x3) == 0x3)
693
		/* If "tryresync" is false, assume that certain
852
		{
694
		parameters do not change within the stream!
853
			if(NOQUIET) error("Stream error");
695
		Force an update if lsf or mpeg25 settings
854
 
696
		have changed. */
855
			return 0; /* exit() here really is too much, isn't it? */
697
		fr->lay = 4 - HDR_LAYER_VAL(newhead);
856
		}
698
		if(fr->mpeg25)
857
		if(fr->mpeg25)
699
		fr->sampling_frequency = 6 + HDR_SAMPLERATE_VAL(newhead);
858
		fr->sampling_frequency = 6 + ((newhead>>10)&0x3);
700
		else
859
		else
701
		fr->sampling_frequency = HDR_SAMPLERATE_VAL(newhead) + (fr->lsf*3);
860
		fr->sampling_frequency = ((newhead>>10)&0x3) + (fr->lsf*3);
702
	}
861
	}
703
 
862
 
704
	#ifdef DEBUG
Line 863... Line 705...
863
	#ifdef DEBUG
705
	/* seen a file where this varies (old lame tag without crc, track with crc) */
Line 864... Line -...
864
	if((((newhead>>16)&0x1)^0x1) != fr->error_protection) debug("changed crc bit!");
-
 
865
	#endif
-
 
866
	fr->error_protection = ((newhead>>16)&0x1)^0x1; /* seen a file where this varies (old lame tag without crc, track with crc) */
706
	if((HDR_CRC_VAL(newhead)^0x1) != fr->error_protection) debug("changed crc bit!");
867
	fr->bitrate_index = ((newhead>>12)&0xf);
-
 
868
	fr->padding   = ((newhead>>9)&0x1);
707
	#endif
869
	fr->extension = ((newhead>>8)&0x1);
708
	fr->error_protection = HDR_CRC_VAL(newhead)^0x1;
870
	fr->mode      = ((newhead>>6)&0x3);
709
	fr->bitrate_index    = HDR_BITRATE_VAL(newhead);
871
	fr->mode_ext  = ((newhead>>4)&0x3);
710
	fr->padding          = HDR_PADDING_VAL(newhead);
872
	fr->copyright = ((newhead>>3)&0x1);
711
	fr->extension        = HDR_PRIVATE_VAL(newhead);
-
 
712
	fr->mode             = HDR_CHANNEL_VAL(newhead);
-
 
713
	fr->mode_ext         = HDR_CHANEX_VAL(newhead);
-
 
714
	fr->copyright        = HDR_COPYRIGHT_VAL(newhead);
-
 
715
	fr->original         = HDR_ORIGINAL_VAL(newhead);
-
 
716
	fr->emphasis         = HDR_EMPHASIS_VAL(newhead);
-
 
717
	fr->freeformat       = !(newhead & HDR_BITRATE);
-
 
718
 
873
	fr->original  = ((newhead>>2)&0x1);
719
	fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2;
874
	fr->emphasis  = newhead & 0x3;
720
 
875
	fr->freeformat = free_format_header(newhead);
721
	/* we can't use tabsel_123 for freeformat, so trying to guess framesize... */
876
 
722
	if(fr->freeformat)
-
 
723
	{
-
 
724
		/* when we first encounter the frame with freeformat, guess framesize */
877
	fr->stereo    = (fr->mode == MPG_MD_MONO) ? 1 : 2;
725
		if(fr->freeformat_framesize < 0)
878
 
726
		{
879
	fr->oldhead = newhead;
727
			int ret;
-
 
728
			*freeformat_count += 1;
-
 
729
			if(*freeformat_count > 5)
-
 
730
			{
880
	
731
				if(VERBOSE3) error("You fooled me too often. Refusing to guess free format frame size _again_.");
-
 
732
				return PARSE_BAD;
881
	/* we can't use tabsel_123 for freeformat, so trying to guess framesize... */
733
			}
882
	/* FIXME: We need proper handling/passing of MPG123_NEED_MORE! */
734
			ret = guess_freeformat_framesize(fr);
883
	if(fr->freeformat)
735
			if(ret == PARSE_GOOD)
884
	{
736
			{
885
		/* when we first encounter the frame with freeformat, guess framesize */
737
				fr->freeformat_framesize = fr->framesize - fr->padding;
886
		if(fr->freeformat_framesize < 0)
738
				if(VERBOSE2)
Line 948... Line 800...
948
		break;
800
		break;
949
#endif 
801
#endif 
950
		default:
802
		default:
951
			if(NOQUIET) error1("Layer type %i not supported in this build!", fr->lay); 
803
			if(NOQUIET) error1("Layer type %i not supported in this build!", fr->lay); 
Line 952... Line 804...
952
 
804
 
953
			return 0;
805
			return PARSE_BAD;
954
	}
806
	}
955
	if (fr->framesize > MAXFRAMESIZE)
807
	if (fr->framesize > MAXFRAMESIZE)
956
	{
808
	{
Line 957... Line 809...
957
		if(NOQUIET) error1("Frame size too big: %d", fr->framesize+4-fr->padding);
809
		if(NOQUIET) error1("Frame size too big: %d", fr->framesize+4-fr->padding);
958
 
810
 
959
		return (0);
811
		return PARSE_BAD;
960
	}
812
	}
Line 961... Line 813...
961
	return 1;
813
	return PARSE_GOOD;
962
}
814
}
963
 
815
 
Line 994... Line 846...
994
	}
846
	}
Line 995... Line 847...
995
 
847
 
996
	return bpf;
848
	return bpf;
Line -... Line 849...
-
 
849
}
-
 
850
 
-
 
851
int attribute_align_arg mpg123_spf(mpg123_handle *mh)
-
 
852
{
-
 
853
	if(mh == NULL) return MPG123_ERR;
-
 
854
 
-
 
855
	return spf(mh);
997
}
856
}
998
 
857
 
999
double attribute_align_arg mpg123_tpf(mpg123_handle *fr)
858
double attribute_align_arg mpg123_tpf(mpg123_handle *fr)
1000
{
859
{
1001
	static int bs[4] = { 0,384,1152,1152 };
860
	static int bs[4] = { 0,384,1152,1152 };
Line 1087... Line 946...
1087
	}
946
	}
Line 1088... Line 947...
1088
 
947
 
1089
	tpf = mpg123_tpf(fr);
948
	tpf = mpg123_tpf(fr);
1090
	return (int) (no*tpf);
949
	return (int) (no*tpf);
-
 
950
}
-
 
951
 
-
 
952
/* first attempt of read ahead check to find the real first header; cannot believe what junk is out there! */
-
 
953
static int do_readahead(mpg123_handle *fr, unsigned long newhead)
-
 
954
{
-
 
955
	unsigned long nexthead = 0;
-
 
956
	int hd = 0;
-
 
957
	off_t start, oret;
-
 
958
	int ret;
-
 
959
 
-
 
960
	if( ! (!fr->firsthead && fr->rdat.flags & (READER_SEEKABLE|READER_BUFFERED)) )
-
 
961
	return PARSE_GOOD;
-
 
962
 
-
 
963
	start = fr->rd->tell(fr);
-
 
964
 
-
 
965
	debug2("doing ahead check with BPF %d at %"OFF_P, fr->framesize+4, (off_p)start);
-
 
966
	/* step framesize bytes forward and read next possible header*/
-
 
967
	if((oret=fr->rd->skip_bytes(fr, fr->framesize))<0)
-
 
968
	{
-
 
969
		if(oret==READER_ERROR && NOQUIET) error("cannot seek!");
-
 
970
 
-
 
971
		return oret == MPG123_NEED_MORE ? PARSE_MORE : PARSE_ERR;
-
 
972
	}
-
 
973
 
-
 
974
	/* Read header, seek back. */
-
 
975
	hd = fr->rd->head_read(fr,&nexthead);
-
 
976
	if( fr->rd->back_bytes(fr, fr->rd->tell(fr)-start) < 0 )
-
 
977
	{
-
 
978
		if(NOQUIET) error("Cannot seek back!");
-
 
979
 
-
 
980
		return PARSE_ERR;
-
 
981
	}
-
 
982
	if(hd == MPG123_NEED_MORE) return PARSE_MORE;
-
 
983
 
-
 
984
	debug1("After fetching next header, at %"OFF_P, (off_p)fr->rd->tell(fr));
-
 
985
	if(!hd)
-
 
986
	{
-
 
987
		if(NOQUIET) warning("Cannot read next header, a one-frame stream? Duh...");
-
 
988
		return PARSE_END;
-
 
989
	}
-
 
990
 
-
 
991
	debug2("does next header 0x%08lx match first 0x%08lx?", nexthead, newhead);
-
 
992
	if(!head_check(nexthead) || !head_compatible(newhead, nexthead))
-
 
993
	{
-
 
994
		debug("No, the header was not valid, start from beginning...");
-
 
995
		fr->oldhead = 0; /* start over */
-
 
996
		/* try next byte for valid header */
-
 
997
		if((ret=fr->rd->back_bytes(fr, 3))<0)
-
 
998
		{
-
 
999
			if(NOQUIET) error("Cannot seek 3 bytes back!");
-
 
1000
 
-
 
1001
			return PARSE_ERR;
-
 
1002
		}
-
 
1003
		return PARSE_AGAIN;
-
 
1004
	}
-
 
1005
	else return PARSE_GOOD;
-
 
1006
}
-
 
1007
 
-
 
1008
static int handle_id3v2(mpg123_handle *fr, unsigned long newhead)
-
 
1009
{
-
 
1010
	int ret;
-
 
1011
	fr->oldhead = 0; /* Think about that. Used to be present only for skipping of junk, not resync-style wetwork. */
-
 
1012
	ret = parse_new_id3(fr, newhead);
-
 
1013
	if     (ret < 0) return ret;
-
 
1014
#ifndef NO_ID3V2
-
 
1015
	else if(ret > 0){ debug("got ID3v2"); fr->metaflags  |= MPG123_NEW_ID3|MPG123_ID3; }
-
 
1016
	else debug("no useful ID3v2");
-
 
1017
#endif
-
 
1018
	return PARSE_AGAIN;
-
 
1019
}
-
 
1020
 
-
 
1021
/* watch out for junk/tags on beginning of stream by invalid header */
-
 
1022
static int skip_junk(mpg123_handle *fr, unsigned long *newheadp, long *headcount)
-
 
1023
{
-
 
1024
	int ret;
-
 
1025
	int freeformat_count = 0;
-
 
1026
	long limit = 65536;
-
 
1027
	unsigned long newhead = *newheadp;
-
 
1028
	/* check for id3v2; first three bytes (of 4) are "ID3" */
-
 
1029
	if((newhead & (unsigned long) 0xffffff00) == (unsigned long) 0x49443300)
-
 
1030
	{
-
 
1031
		return handle_id3v2(fr, newhead);
-
 
1032
	}
-
 
1033
	else if(VERBOSE2 && fr->silent_resync == 0) fprintf(stderr,"Note: Junk at the beginning (0x%08lx)\n",newhead);
-
 
1034
 
-
 
1035
	/* I even saw RIFF headers at the beginning of MPEG streams ;( */
-
 
1036
	if(newhead == ('R'<<24)+('I'<<16)+('F'<<8)+'F')
-
 
1037
	{
-
 
1038
		if(VERBOSE2 && fr->silent_resync == 0) fprintf(stderr, "Note: Looks like a RIFF header.\n");
-
 
1039
 
-
 
1040
		if((ret=fr->rd->head_read(fr,&newhead))<=0) return ret;
-
 
1041
 
-
 
1042
		while(newhead != ('d'<<24)+('a'<<16)+('t'<<8)+'a')
-
 
1043
		{
-
 
1044
			if((ret=fr->rd->head_shift(fr,&newhead))<=0) return ret;
-
 
1045
		}
-
 
1046
		if((ret=fr->rd->head_read(fr,&newhead))<=0) return ret;
-
 
1047
 
-
 
1048
		if(VERBOSE2 && fr->silent_resync == 0) fprintf(stderr,"Note: Skipped RIFF header!\n");
-
 
1049
 
-
 
1050
		fr->oldhead = 0;
-
 
1051
		*newheadp = newhead;
-
 
1052
		return PARSE_AGAIN;
-
 
1053
	}
-
 
1054
 
-
 
1055
	/*
-
 
1056
		Unhandled junk... just continue search for a header, stepping in single bytes through next 64K.
-
 
1057
		This is rather identical to the resync loop.
-
 
1058
	*/
-
 
1059
	debug("searching for header...");
-
 
1060
	*newheadp = 0; /* Invalidate the external value. */
-
 
1061
	ret = 0; /* We will check the value after the loop. */
-
 
1062
 
-
 
1063
	/* We prepare for at least the 64K bytes as usual, unless
-
 
1064
	   user explicitly wanted more (even infinity). Never less. */
-
 
1065
	if(fr->p.resync_limit < 0 || fr->p.resync_limit > limit)
-
 
1066
	limit = fr->p.resync_limit;
-
 
1067
 
-
 
1068
	do
-
 
1069
	{
-
 
1070
		++(*headcount);
-
 
1071
		if(limit >= 0 && *headcount >= limit) break;				
-
 
1072
 
-
 
1073
		if((ret=fr->rd->head_shift(fr,&newhead))<=0) return ret;
-
 
1074
 
-
 
1075
		if(head_check(newhead) && (ret=decode_header(fr, newhead, &freeformat_count))) break;
-
 
1076
	} while(1);
-
 
1077
	if(ret<0) return ret;
-
 
1078
 
-
 
1079
	if(limit >= 0 && *headcount >= limit)
-
 
1080
	{
-
 
1081
		if(NOQUIET) error1("Giving up searching valid MPEG header after %li bytes of junk.", *headcount);
-
 
1082
		return PARSE_END;
-
 
1083
	}
-
 
1084
	else debug1("hopefully found one at %"OFF_P, (off_p)fr->rd->tell(fr));
-
 
1085
 
-
 
1086
	/* If the new header ist good, it is already decoded. */
-
 
1087
	*newheadp = newhead;
-
 
1088
	return PARSE_GOOD;
-
 
1089
}
-
 
1090
 
-
 
1091
/* The newhead is bad, so let's check if it is something special, otherwise just resync. */
-
 
1092
static int wetwork(mpg123_handle *fr, unsigned long *newheadp)
-
 
1093
{
-
 
1094
	int ret = PARSE_ERR;
-
 
1095
	unsigned long newhead = *newheadp;
-
 
1096
	*newheadp = 0;
-
 
1097
 
-
 
1098
	/* Classic ID3 tags. Read, then start parsing again. */
-
 
1099
	if((newhead & 0xffffff00) == ('T'<<24)+('A'<<16)+('G'<<8))
-
 
1100
	{
-
 
1101
		fr->id3buf[0] = (unsigned char) ((newhead >> 24) & 0xff);
-
 
1102
		fr->id3buf[1] = (unsigned char) ((newhead >> 16) & 0xff);
-
 
1103
		fr->id3buf[2] = (unsigned char) ((newhead >> 8)  & 0xff);
-
 
1104
		fr->id3buf[3] = (unsigned char) ( newhead        & 0xff);
-
 
1105
 
-
 
1106
		if((ret=fr->rd->fullread(fr,fr->id3buf+4,124)) < 0) return ret;
-
 
1107
 
-
 
1108
		fr->metaflags  |= MPG123_NEW_ID3|MPG123_ID3;
-
 
1109
		fr->rdat.flags |= READER_ID3TAG; /* that marks id3v1 */
-
 
1110
		if(VERBOSE3) fprintf(stderr,"Note: Skipped ID3v1 tag.\n");
-
 
1111
 
-
 
1112
		return PARSE_AGAIN;
-
 
1113
	}
-
 
1114
	/* This is similar to initial junk skipping code... */
-
 
1115
	/* Check for id3v2; first three bytes (of 4) are "ID3" */
-
 
1116
	if((newhead & (unsigned long) 0xffffff00) == (unsigned long) 0x49443300)
-
 
1117
	{
-
 
1118
		return handle_id3v2(fr, newhead);
-
 
1119
	}
-
 
1120
	else if(NOQUIET && fr->silent_resync == 0)
-
 
1121
	{
-
 
1122
		fprintf(stderr,"Note: Illegal Audio-MPEG-Header 0x%08lx at offset %"OFF_P".\n",
-
 
1123
			newhead, (off_p)fr->rd->tell(fr)-4);
-
 
1124
	}
-
 
1125
 
-
 
1126
	/* Now we got something bad at hand, try to recover. */
-
 
1127
 
-
 
1128
	if(NOQUIET && (newhead & 0xffffff00) == ('b'<<24)+('m'<<16)+('p'<<8)) fprintf(stderr,"Note: Could be a BMP album art.\n");
-
 
1129
 
-
 
1130
	if( !(fr->p.flags & MPG123_NO_RESYNC) )
-
 
1131
	{
-
 
1132
		long try = 0;
-
 
1133
		long limit = fr->p.resync_limit;
-
 
1134
 
-
 
1135
		/* If a resync is needed the bitreservoir of previous frames is no longer valid */
-
 
1136
		fr->bitreservoir = 0;
-
 
1137
 
-
 
1138
		if(NOQUIET && fr->silent_resync == 0) fprintf(stderr, "Note: Trying to resync...\n");
-
 
1139
 
-
 
1140
		do /* ... shift the header with additional single bytes until be found something that could be a header. */
-
 
1141
		{
-
 
1142
			++try;
-
 
1143
			if(limit >= 0 && try >= limit) break;				
-
 
1144
 
-
 
1145
			if((ret=fr->rd->head_shift(fr,&newhead)) <= 0)
-
 
1146
			{
-
 
1147
				*newheadp = newhead;
-
 
1148
				if(NOQUIET) fprintf (stderr, "Note: Hit end of (available) data during resync.\n");
-
 
1149
 
-
 
1150
				return ret ? ret : PARSE_END;
-
 
1151
			}
-
 
1152
			if(VERBOSE3) debug3("resync try %li at %"OFF_P", got newhead 0x%08lx", try, (off_p)fr->rd->tell(fr),  newhead);
-
 
1153
		} while(!head_check(newhead));
-
 
1154
 
-
 
1155
		*newheadp = newhead;
-
 
1156
		if(NOQUIET && fr->silent_resync == 0) fprintf (stderr, "Note: Skipped %li bytes in input.\n", try);
-
 
1157
 
-
 
1158
		/* Now we either got something that could be a header, or we gave up. */
-
 
1159
		if(limit >= 0 && try >= limit)
-
 
1160
		{
-
 
1161
			if(NOQUIET)
-
 
1162
			error1("Giving up resync after %li bytes - your stream is not nice... (maybe increasing resync limit could help).", try);
-
 
1163
 
-
 
1164
			fr->err = MPG123_RESYNC_FAIL;
-
 
1165
			return PARSE_ERR;
-
 
1166
		}
-
 
1167
		else
-
 
1168
		{
-
 
1169
			debug1("Found possibly valid header 0x%lx... unsetting firsthead to reinit stream.", newhead);
-
 
1170
			fr->firsthead = 0;
-
 
1171
			fr->oldhead = 0;
-
 
1172
			return PARSE_RESYNC;
-
 
1173
		}
-
 
1174
	}
-
 
1175
	else
-
 
1176
	{
-
 
1177
		if(NOQUIET) error("not attempting to resync...");
-
 
1178
 
-
 
1179
		fr->err = MPG123_OUT_OF_SYNC;
-
 
1180
		return PARSE_ERR;
-
 
1181
	}
-
 
1182
	/* Control never goes here... we return before that. */