summaryrefslogtreecommitdiffstats
path: root/mpg123_artsplugin/dxhead.c
blob: fb0560556b1f0b0b166ed47d2b3f283965e8dec4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*---- DXhead.c --------------------------------------------


decoder MPEG Layer III

handle Xing header

mod 12/7/98 add vbr scale

Copyright 1998 Xing Technology Corp.
-----------------------------------------------------------*/  
#include <stdlib.h>
#include <unistd.h>
#include <float.h>
#include <math.h>
#include "mpg123/mpg123.h"
#include "dxhead.h"

/* 4   Xing
 * 4   flags
 * 4   frames
 * 4   bytes
 * 100 toc
 */ 

/*-------------------------------------------------------------*/ 
static int ExtractI4(unsigned char *buf) 
{
	
		int x;
	
/* big endian extract */ 
		
		x = buf[0];
	
		x <<= 8;
	
		x |= buf[1];
	
		x <<= 8;
	
		x |= buf[2];
	
		x <<= 8;
	
		x |= buf[3];
	
		
		return x;
	
}

/*-------------------------------------------------------------*/ 
int mpg123_get_xing_header(XHEADDATA * X, unsigned char *buf) 
{
	
		int i, head_flags;
	
		int h_id, h_mode, h_sr_index;
	
		static int sr_table[4] =
	{44100, 48000, 32000, 99999};
	
		
/* get Xing header data */ 
		
		
		X->flags = 0;	/* clear to null incase fail */
		X->toc = NULL;
		
		
/* get selected MPEG header data */ 
		h_id = (buf[1] >> 3) & 1;
	
		h_sr_index = (buf[2] >> 2) & 3;
	
		h_mode = (buf[3] >> 6) & 3;
	
		
		
/* determine offset of header */ 
		if (h_id)
	{			/* mpeg1 */
		
			if (h_mode != 3) {
			buf += (32 + 4);

}
		
			else
			buf += (17 + 4);
		
	}
	
		else
	{			/* mpeg2 */
		
			if (h_mode != 3)
			buf += (17 + 4);
		
			else
			buf += (9 + 4);
		
	}
	
		
		if (buf[0] != 'X')
		return 0;	/* fail */
	
		if (buf[1] != 'i')
		return 0;	/* header not found */
	
		if (buf[2] != 'n')
		return 0;
	
		if (buf[3] != 'g')
		return 0;
	
		buf += 4;
	
		
		X->h_id = h_id;
	
		X->samprate = sr_table[h_sr_index];
	
		if (h_id == 0)
		X->samprate >>= 1;
	
		
		head_flags = X->flags = ExtractI4(buf);
	buf += 4;		/* get flags */
	
		
		if (head_flags & FRAMES_FLAG)
	{
		X->frames = ExtractI4(buf);
		buf += 4;
	}
	
		if (head_flags & BYTES_FLAG)
	{
		X->bytes = ExtractI4(buf);
		buf += 4;
	}
	
		
		if (head_flags & TOC_FLAG)
	{
		
			X->toc = malloc(100);
			if (X->toc != NULL)
		{
			
				for (i = 0; i < 100; i++)
				X->toc[i] = buf[i];
			
		}
		
			buf += 100;
		
	}
	
		
		X->vbr_scale = -1;
	
		if (head_flags & VBR_SCALE_FLAG)
	{
		X->vbr_scale = ExtractI4(buf);
		buf += 4;
	}
	
		
/*if( X->toc != NULL ) {
 *for(i=0;i<100;i++) {
 *    if( (i%10) == 0 ) printf("\n");
 *    printf(" %3d", (int)(X->toc[i]));
 *}
 *}
 */ 
		
		return 1;	/* success */
	
}

/*-------------------------------------------------------------*/ 
int mpg123_seek_point(unsigned char TOC[100], int file_bytes, float percent) 
{
	
/* interpolate in TOC to get file seek point in bytes */ 
		int a, seekpoint;
	
		float fa, fb, fx;
	
		
		
		if (percent < 0.0f)
		percent = 0.0f;
	
		if (percent > 100.0f)
		percent = 100.0f;
	
		
		a = (int) percent;
	
		if (a > 99)
		a = 99;
	
		fa = TOC[a];
	
		if (a < 99)
	{
		
			fb = TOC[a + 1];
		
	}
	
		else
	{
		
			fb = 256.0f;
		
	}
	
		
		
		fx = fa + (fb - fa) * (percent - a);
	
		
		seekpoint = (int) ((1.0f / 256.0f) * fx * file_bytes);
	
		
		
		return seekpoint;
	
}

/*-------------------------------------------------------------*/ 
int mpg123_stream_check_for_xing_header(struct frame *fr, XHEADDATA * xhead)
{
	unsigned char *head_data;
	int ret;

	lseek(rd->filept, -(fr->framesize + 4), SEEK_CUR);
	head_data = malloc(fr->framesize + 4);
	read(rd->filept,head_data, fr->framesize +4); /* now read the rest */
	ret = mpg123_get_xing_header(xhead, head_data);
	free(head_data);
	return ret;
}