~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to mpg123_artsplugin/dxhead.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*---- DXhead.c --------------------------------------------
 
2
 
 
3
 
 
4
decoder MPEG Layer III
 
5
 
 
6
handle Xing header
 
7
 
 
8
mod 12/7/98 add vbr scale
 
9
 
 
10
Copyright 1998 Xing Technology Corp.
 
11
-----------------------------------------------------------*/  
 
12
#include <stdlib.h>
 
13
#include <unistd.h>
 
14
#include <float.h>
 
15
#include <math.h>
 
16
#include "mpg123/mpg123.h"
 
17
#include "dxhead.h"
 
18
 
 
19
/* 4   Xing
 
20
 * 4   flags
 
21
 * 4   frames
 
22
 * 4   bytes
 
23
 * 100 toc
 
24
 */ 
 
25
 
 
26
/*-------------------------------------------------------------*/ 
 
27
static int ExtractI4(unsigned char *buf) 
 
28
{
 
29
        
 
30
                int x;
 
31
        
 
32
/* big endian extract */ 
 
33
                
 
34
                x = buf[0];
 
35
        
 
36
                x <<= 8;
 
37
        
 
38
                x |= buf[1];
 
39
        
 
40
                x <<= 8;
 
41
        
 
42
                x |= buf[2];
 
43
        
 
44
                x <<= 8;
 
45
        
 
46
                x |= buf[3];
 
47
        
 
48
                
 
49
                return x;
 
50
        
 
51
}
 
52
 
 
53
/*-------------------------------------------------------------*/ 
 
54
int mpg123_get_xing_header(XHEADDATA * X, unsigned char *buf) 
 
55
{
 
56
        
 
57
                int i, head_flags;
 
58
        
 
59
                int h_id, h_mode, h_sr_index;
 
60
        
 
61
                static int sr_table[4] =
 
62
        {44100, 48000, 32000, 99999};
 
63
        
 
64
                
 
65
/* get Xing header data */ 
 
66
                
 
67
                
 
68
                X->flags = 0;   /* clear to null incase fail */
 
69
                X->toc = NULL;
 
70
                
 
71
                
 
72
/* get selected MPEG header data */ 
 
73
                h_id = (buf[1] >> 3) & 1;
 
74
        
 
75
                h_sr_index = (buf[2] >> 2) & 3;
 
76
        
 
77
                h_mode = (buf[3] >> 6) & 3;
 
78
        
 
79
                
 
80
                
 
81
/* determine offset of header */ 
 
82
                if (h_id)
 
83
        {                       /* mpeg1 */
 
84
                
 
85
                        if (h_mode != 3) {
 
86
                        buf += (32 + 4);
 
87
 
 
88
}
 
89
                
 
90
                        else
 
91
                        buf += (17 + 4);
 
92
                
 
93
        }
 
94
        
 
95
                else
 
96
        {                       /* mpeg2 */
 
97
                
 
98
                        if (h_mode != 3)
 
99
                        buf += (17 + 4);
 
100
                
 
101
                        else
 
102
                        buf += (9 + 4);
 
103
                
 
104
        }
 
105
        
 
106
                
 
107
                if (buf[0] != 'X')
 
108
                return 0;       /* fail */
 
109
        
 
110
                if (buf[1] != 'i')
 
111
                return 0;       /* header not found */
 
112
        
 
113
                if (buf[2] != 'n')
 
114
                return 0;
 
115
        
 
116
                if (buf[3] != 'g')
 
117
                return 0;
 
118
        
 
119
                buf += 4;
 
120
        
 
121
                
 
122
                X->h_id = h_id;
 
123
        
 
124
                X->samprate = sr_table[h_sr_index];
 
125
        
 
126
                if (h_id == 0)
 
127
                X->samprate >>= 1;
 
128
        
 
129
                
 
130
                head_flags = X->flags = ExtractI4(buf);
 
131
        buf += 4;               /* get flags */
 
132
        
 
133
                
 
134
                if (head_flags & FRAMES_FLAG)
 
135
        {
 
136
                X->frames = ExtractI4(buf);
 
137
                buf += 4;
 
138
        }
 
139
        
 
140
                if (head_flags & BYTES_FLAG)
 
141
        {
 
142
                X->bytes = ExtractI4(buf);
 
143
                buf += 4;
 
144
        }
 
145
        
 
146
                
 
147
                if (head_flags & TOC_FLAG)
 
148
        {
 
149
                
 
150
                        X->toc = malloc(100);
 
151
                        if (X->toc != NULL)
 
152
                {
 
153
                        
 
154
                                for (i = 0; i < 100; i++)
 
155
                                X->toc[i] = buf[i];
 
156
                        
 
157
                }
 
158
                
 
159
                        buf += 100;
 
160
                
 
161
        }
 
162
        
 
163
                
 
164
                X->vbr_scale = -1;
 
165
        
 
166
                if (head_flags & VBR_SCALE_FLAG)
 
167
        {
 
168
                X->vbr_scale = ExtractI4(buf);
 
169
                buf += 4;
 
170
        }
 
171
        
 
172
                
 
173
/*if( X->toc != NULL ) {
 
174
 *for(i=0;i<100;i++) {
 
175
 *    if( (i%10) == 0 ) printf("\n");
 
176
 *    printf(" %3d", (int)(X->toc[i]));
 
177
 *}
 
178
 *}
 
179
 */ 
 
180
                
 
181
                return 1;       /* success */
 
182
        
 
183
}
 
184
 
 
185
/*-------------------------------------------------------------*/ 
 
186
int mpg123_seek_point(unsigned char TOC[100], int file_bytes, float percent) 
 
187
{
 
188
        
 
189
/* interpolate in TOC to get file seek point in bytes */ 
 
190
                int a, seekpoint;
 
191
        
 
192
                float fa, fb, fx;
 
193
        
 
194
                
 
195
                
 
196
                if (percent < 0.0f)
 
197
                percent = 0.0f;
 
198
        
 
199
                if (percent > 100.0f)
 
200
                percent = 100.0f;
 
201
        
 
202
                
 
203
                a = (int) percent;
 
204
        
 
205
                if (a > 99)
 
206
                a = 99;
 
207
        
 
208
                fa = TOC[a];
 
209
        
 
210
                if (a < 99)
 
211
        {
 
212
                
 
213
                        fb = TOC[a + 1];
 
214
                
 
215
        }
 
216
        
 
217
                else
 
218
        {
 
219
                
 
220
                        fb = 256.0f;
 
221
                
 
222
        }
 
223
        
 
224
                
 
225
                
 
226
                fx = fa + (fb - fa) * (percent - a);
 
227
        
 
228
                
 
229
                seekpoint = (int) ((1.0f / 256.0f) * fx * file_bytes);
 
230
        
 
231
                
 
232
                
 
233
                return seekpoint;
 
234
        
 
235
}
 
236
 
 
237
/*-------------------------------------------------------------*/ 
 
238
int mpg123_stream_check_for_xing_header(struct frame *fr, XHEADDATA * xhead)
 
239
{
 
240
        unsigned char *head_data;
 
241
        int ret;
 
242
 
 
243
        lseek(rd->filept, -(fr->framesize + 4), SEEK_CUR);
 
244
        head_data = malloc(fr->framesize + 4);
 
245
        read(rd->filept,head_data, fr->framesize +4); /* now read the rest */
 
246
        ret = mpg123_get_xing_header(xhead, head_data);
 
247
        free(head_data);
 
248
        return ret;
 
249
}
 
250