comparison Plugins/Input/mpg123/common.c @ 61:fa848bd484d8 trunk

[svn] Move plugins to Plugins/
author nenolod
date Fri, 28 Oct 2005 22:58:11 -0700
parents
children 40a60fe13ed6
comparison
equal deleted inserted replaced
60:1771f253e1b2 61:fa848bd484d8
1 #include <stdlib.h>
2 #include <string.h>
3 #include <ctype.h>
4
5 #include <signal.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9
10 #include "mpg123.h"
11 #include "xmms-id3.h"
12 #include "id3_header.h"
13
14 /* max = 1728 */
15 #define MAXFRAMESIZE 1792
16
17 const int tabsel_123[2][3][16] = {
18 {{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416,
19 448,},
20 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
21 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}},
22 {{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
23 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
24 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}}
25 };
26
27 const int mpg123_freqs[9] =
28 { 44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000 };
29
30 struct bitstream_info bsi;
31
32 extern gint mpg123_bitrate, mpg123_frequency, mpg123_length;
33 extern gchar *mpg123_title, *mpg123_filename;
34 extern gboolean mpg123_stereo;
35
36 static int fsizeold = 0, ssize;
37 static unsigned char bsspace[2][MAXFRAMESIZE + 512]; /* MAXFRAMESIZE */
38 static unsigned char *bsbuf = bsspace[1], *bsbufold;
39 static int bsnum = 0;
40
41 unsigned char *mpg123_pcm_sample;
42 int mpg123_pcm_point = 0;
43
44 static VFSFile *filept;
45 static int filept_opened;
46
47 static int get_fileinfo(void);
48
49 static int
50 fullread(VFSFile * fd, unsigned char *buf, int count)
51 {
52 int ret, cnt = 0;
53
54 while (cnt < count) {
55 if (fd)
56 ret = vfs_fread(buf + cnt, 1, count - cnt, fd);
57 else
58 ret = mpg123_http_read(buf + cnt, count - cnt);
59 if (ret < 0)
60 return ret;
61 if (ret == 0)
62 break;
63 cnt += ret;
64 }
65 return cnt;
66 }
67
68 static int
69 stream_init(void)
70 {
71 if (get_fileinfo() < 0)
72 return -1;
73 return 0;
74 }
75
76 void
77 mpg123_stream_close(void)
78 {
79 if (filept)
80 vfs_fclose(filept);
81 else if (mpg123_info->network_stream)
82 mpg123_http_close();
83 }
84
85 /****************************************
86 * HACK,HACK,HACK: step back <num> frames
87 * can only work if the 'stream' isn't a real stream but a file
88 static int stream_back_bytes(int bytes)
89 {
90 if (vfs_fseek(filept, -bytes, SEEK_CUR) < 0)
91 return -1;
92 return 0;
93 }
94 */
95
96 static int
97 stream_head_read(unsigned long *newhead)
98 {
99 unsigned char hbuf[4];
100
101 if (fullread(filept, hbuf, 4) != 4)
102 return FALSE;
103
104 *newhead = ((unsigned long) hbuf[0] << 24) |
105 ((unsigned long) hbuf[1] << 16) |
106 ((unsigned long) hbuf[2] << 8) | (unsigned long) hbuf[3];
107
108 return TRUE;
109 }
110
111 static int
112 stream_head_shift(unsigned long *head)
113 {
114 unsigned char hbuf;
115
116 if (fullread(filept, &hbuf, 1) != 1)
117 return 0;
118 *head <<= 8;
119 *head |= hbuf;
120 *head &= 0xffffffff;
121 return 1;
122 }
123
124 static int
125 stream_mpg123_read_frame_body(unsigned char *buf, int size)
126 {
127 long l;
128
129 if ((l = fullread(filept, buf, size)) != size) {
130 if (l <= 0)
131 return 0;
132 memset(buf + l, 0, size - l);
133 }
134 return 1;
135 }
136
137 static long
138 stream_tell(void)
139 {
140 return vfs_ftell(filept);
141 }
142
143 /*
144 static void stream_rewind(void)
145 {
146 vfs_fseek(filept, 0, SEEK_SET);
147 }
148 */
149
150 int
151 mpg123_stream_jump_to_frame(struct frame *fr, int frame)
152 {
153 if (!filept)
154 return -1;
155 mpg123_read_frame_init();
156 vfs_fseek(filept, frame * (fr->framesize + 4), SEEK_SET);
157 mpg123_read_frame(fr);
158 return 0;
159 }
160
161 int
162 mpg123_stream_jump_to_byte(struct frame *fr, int byte)
163 {
164 if (!filept)
165 return -1;
166 vfs_fseek(filept, byte, SEEK_SET);
167 mpg123_read_frame(fr);
168 return 0;
169 }
170
171 int
172 mpg123_stream_check_for_xing_header(struct frame *fr, xing_header_t * xhead)
173 {
174 unsigned char *head_data;
175 int ret;
176
177 vfs_fseek(filept, -(fr->framesize + 4), SEEK_CUR);
178 head_data = g_malloc(fr->framesize + 4);
179 vfs_fread(head_data, 1, fr->framesize + 4, filept);
180 ret = mpg123_get_xing_header(xhead, head_data);
181 g_free(head_data);
182 return ret;
183 }
184
185 static int
186 get_fileinfo(void)
187 {
188 guchar buf[3];
189
190 if (filept == NULL)
191 return -1;
192 if (vfs_fseek(filept, 0, SEEK_END) < 0)
193 return -1;
194
195 mpg123_info->filesize = vfs_ftell(filept);
196 if (vfs_fseek(filept, -128, SEEK_END) < 0)
197 return -1;
198 if (fullread(filept, buf, 3) != 3)
199 return -1;
200 if (!strncmp((char *) buf, "TAG", 3))
201 mpg123_info->filesize -= 128;
202 if (vfs_fseek(filept, 0, SEEK_SET) < 0)
203 return -1;
204
205 if (mpg123_info->filesize <= 0)
206 return -1;
207
208 return mpg123_info->filesize;
209 }
210
211 void
212 mpg123_read_frame_init(void)
213 {
214 memset(bsspace[0], 0, MAXFRAMESIZE + 512);
215 memset(bsspace[1], 0, MAXFRAMESIZE + 512);
216 mpg123_info->output_audio = FALSE;
217 }
218
219 /*
220 * Function read_id3v2_tag (head)
221 *
222 * Read ID3v2 tag from stream. Return TRUE upon success, or FALSE if
223 * an error occurred.
224 *
225 */
226 static gboolean
227 read_id3v2_tag(unsigned long head)
228 {
229 guchar *id3buf;
230 int hdrsize;
231 struct id3_tag *id3d;
232 struct id3tag_t tag;
233 guchar buf[7];
234
235 buf[0] = head & 0xff;
236 /*
237 * Read ID3tag header.
238 */
239 if (fullread(filept, buf + 1, 6) != 6)
240 return FALSE;
241
242 hdrsize = ID3_GET_SIZE28(buf[3], buf[4], buf[5], buf[6]);
243
244 /*
245 * A invalid header could fool us into requesting insane
246 * amounts of memory. Make sure the header size is
247 * reasonable.
248 */
249 if ((mpg123_info->filesize && hdrsize > mpg123_info->filesize) ||
250 (!mpg123_info->filesize && hdrsize > 1000000))
251 return FALSE;
252
253 if (mpg123_cfg.disable_id3v2) {
254 guint8 *tmp = g_malloc(hdrsize);
255 gboolean ret;
256 ret = (fullread(filept, tmp, hdrsize) == hdrsize);
257 g_free(tmp);
258 return ret;
259 }
260
261 id3buf = g_malloc(hdrsize + ID3_TAGHDR_SIZE + 3);
262 memcpy(id3buf, "ID3", 3);
263 memcpy(id3buf + 3, buf, ID3_TAGHDR_SIZE);
264
265 /*
266 * Read ID3tag body.
267 */
268 if (fullread(filept, id3buf + ID3_TAGHDR_SIZE + 3, hdrsize) != hdrsize) {
269 g_free(id3buf);
270 return FALSE;
271 }
272
273 /*
274 * Get info from tag.
275 */
276 if ((id3d = id3_open_mem(id3buf, 0)) != NULL) {
277 mpg123_get_id3v2(id3d, &tag);
278 if (!mpg123_info->first_frame) {
279 char *songname = mpg123_title;
280 mpg123_title = mpg123_format_song_title(&tag, mpg123_filename);
281 mpg123_ip.set_info(mpg123_title, mpg123_length,
282 mpg123_bitrate * 1000,
283 mpg123_frequency, mpg123_stereo);
284 if (songname)
285 g_free(songname);
286 }
287 else {
288 mpg123_title = mpg123_format_song_title(&tag, mpg123_filename);
289 }
290 id3_close(id3d);
291 }
292 g_free(id3buf);
293
294 return TRUE;
295 }
296
297 int
298 mpg123_head_check(unsigned long head)
299 {
300 if ((head & 0xffe00000) != 0xffe00000)
301 return FALSE;
302 if (!((head >> 17) & 3))
303 return FALSE;
304 if (((head >> 12) & 0xf) == 0xf)
305 return FALSE;
306 if (!((head >> 12) & 0xf))
307 return FALSE;
308 if (((head >> 10) & 0x3) == 0x3)
309 return FALSE;
310 if (((head >> 19) & 1) == 1 &&
311 ((head >> 17) & 3) == 3 && ((head >> 16) & 1) == 1)
312 return FALSE;
313 if ((head & 0xffff0000) == 0xfffe0000)
314 return FALSE;
315
316 return TRUE;
317 }
318
319 /*****************************************************************
320 * read next frame
321 */
322 int
323 mpg123_read_frame(struct frame *fr)
324 {
325 unsigned long newhead;
326
327 fsizeold = fr->framesize; /* for Layer3 */
328
329 if (!stream_head_read(&newhead))
330 return FALSE;
331
332 if (!mpg123_head_check(newhead) || !mpg123_decode_header(fr, newhead)) {
333 int try = 0;
334
335 do {
336 try++;
337 if ((newhead & 0xffffff00) ==
338 ('I' << 24) + ('D' << 16) + ('3' << 8)) {
339 read_id3v2_tag(newhead);
340 if (!stream_head_read(&newhead))
341 return FALSE;
342 }
343 else if (!stream_head_shift(&newhead))
344 return 0;
345
346 }
347 while ((!mpg123_head_check(newhead) ||
348 !mpg123_decode_header(fr, newhead)) && try < (256 * 1024));
349 if (try >= (256 * 1024))
350 return FALSE;
351
352 mpg123_info->filesize -= try;
353 }
354 /* flip/init buffer for Layer 3 */
355 bsbufold = bsbuf;
356 bsbuf = bsspace[bsnum] + 512;
357 bsnum = (bsnum + 1) & 1;
358
359 if (!stream_mpg123_read_frame_body(bsbuf, fr->framesize))
360 return 0;
361
362 bsi.bitindex = 0;
363 bsi.wordpointer = (unsigned char *) bsbuf;
364
365
366 return 1;
367
368 }
369
370 /*
371 * the code a header and write the information
372 * into the frame structure
373 */
374 int
375 mpg123_decode_header(struct frame *fr, unsigned long newhead)
376 {
377 if (newhead & (1 << 20)) {
378 fr->lsf = (newhead & (1 << 19)) ? 0x0 : 0x1;
379 fr->mpeg25 = 0;
380 }
381 else {
382 fr->lsf = 1;
383 fr->mpeg25 = 1;
384 }
385 fr->lay = 4 - ((newhead >> 17) & 3);
386 if (fr->mpeg25) {
387 fr->sampling_frequency = 6 + ((newhead >> 10) & 0x3);
388 }
389 else
390 fr->sampling_frequency = ((newhead >> 10) & 0x3) + (fr->lsf * 3);
391 fr->error_protection = ((newhead >> 16) & 0x1) ^ 0x1;
392
393 fr->bitrate_index = ((newhead >> 12) & 0xf);
394 fr->padding = ((newhead >> 9) & 0x1);
395 fr->extension = ((newhead >> 8) & 0x1);
396 fr->mode = ((newhead >> 6) & 0x3);
397 fr->mode_ext = ((newhead >> 4) & 0x3);
398 fr->copyright = ((newhead >> 3) & 0x1);
399 fr->original = ((newhead >> 2) & 0x1);
400 fr->emphasis = newhead & 0x3;
401
402 fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2;
403
404 ssize = 0;
405
406 if (!fr->bitrate_index)
407 return (0);
408
409 switch (fr->lay) {
410 case 1:
411 fr->do_layer = mpg123_do_layer1;
412 /* inits also shared tables with layer1 */
413 mpg123_init_layer2(fr->synth_type == SYNTH_MMX);
414 fr->framesize =
415 (long) tabsel_123[fr->lsf][0][fr->bitrate_index] * 12000;
416 fr->framesize /= mpg123_freqs[fr->sampling_frequency];
417 fr->framesize = ((fr->framesize + fr->padding) << 2) - 4;
418 break;
419 case 2:
420 fr->do_layer = mpg123_do_layer2;
421 /* inits also shared tables with layer1 */
422 mpg123_init_layer2(fr->synth_type == SYNTH_MMX);
423 fr->framesize =
424 (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000;
425 fr->framesize /= mpg123_freqs[fr->sampling_frequency];
426 fr->framesize += fr->padding - 4;
427 break;
428 case 3:
429 fr->do_layer = mpg123_do_layer3;
430 if (fr->lsf)
431 ssize = (fr->stereo == 1) ? 9 : 17;
432 else
433 ssize = (fr->stereo == 1) ? 17 : 32;
434 if (fr->error_protection)
435 ssize += 2;
436 fr->framesize =
437 (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000;
438 fr->framesize /= mpg123_freqs[fr->sampling_frequency] << (fr->lsf);
439 fr->framesize = fr->framesize + fr->padding - 4;
440 break;
441 default:
442 return (0);
443 }
444 if (fr->framesize > MAXFRAMESIZE)
445 return 0;
446 return 1;
447 }
448
449 void
450 mpg123_open_stream(char *bs_filenam, int fd)
451 {
452 filept_opened = 1;
453 if (!strncasecmp(bs_filenam, "http://", 7)) {
454 filept = NULL;
455 mpg123_http_open(bs_filenam);
456 mpg123_info->filesize = 0;
457 mpg123_info->network_stream = TRUE;
458 }
459 else {
460 if ((filept = vfs_fopen(bs_filenam, "rb")) == NULL ||
461 stream_init() == -1)
462 mpg123_info->eof = TRUE;
463 }
464
465 }
466
467 void
468 mpg123_set_pointer(long backstep)
469 {
470 bsi.wordpointer = bsbuf + ssize - backstep;
471 if (backstep)
472 memcpy(bsi.wordpointer, bsbufold + fsizeold - backstep, backstep);
473 bsi.bitindex = 0;
474 }
475
476 double
477 mpg123_compute_bpf(struct frame *fr)
478 {
479 double bpf;
480
481 switch (fr->lay) {
482 case 1:
483 bpf = tabsel_123[fr->lsf][0][fr->bitrate_index];
484 bpf *= 12000.0 * 4.0;
485 bpf /= mpg123_freqs[fr->sampling_frequency] << (fr->lsf);
486 break;
487 case 2:
488 case 3:
489 bpf = tabsel_123[fr->lsf][fr->lay - 1][fr->bitrate_index];
490 bpf *= 144000;
491 bpf /= mpg123_freqs[fr->sampling_frequency] << (fr->lsf);
492 break;
493 default:
494 bpf = 1.0;
495 }
496
497 return bpf;
498 }
499
500 int
501 mpg123_calc_numframes(struct frame *fr)
502 {
503 return (int) (mpg123_info->filesize / mpg123_compute_bpf(fr));
504 }
505
506 double
507 mpg123_relative_pos(void)
508 {
509 if (!filept || !mpg123_info->filesize)
510 return 0;
511 return ((double) stream_tell()) / mpg123_info->filesize;
512 }