annotate Input/mpg123/dxhead.c @ 31:b1dcf238b2ba trunk

[svn] More AAC detection fixes c/o MSameer.
author nenolod
date Tue, 25 Oct 2005 22:09:36 -0700
parents cb178e5ad177
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
1 /*
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
2 * Handle Xing vbr header
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
3 */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
4 #include "config.h"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
5 #include "dxhead.h"
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
6 #include <stdlib.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
7 #include <float.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
8 #include <math.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
9 #include <string.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
10 #include <glib.h>
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
11
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
12 #define GET_INT32BE(b) \
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
13 (i = (b[0] << 24) | (b[1] << 16) | b[2] << 8 | b[3], b += 4, i)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
14
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
15 int
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
16 mpg123_get_xing_header(xing_header_t * xing, unsigned char *buf)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
17 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
18 int i, head_flags;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
19 int id, mode;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
20
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
21 memset(xing, 0, sizeof(xing_header_t));
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
22
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
23 /* get selected MPEG header data */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
24 id = (buf[1] >> 3) & 1;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
25 mode = (buf[3] >> 6) & 3;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
26 buf += 4;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
27
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
28 /* Skip the sub band data */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
29 if (id) {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
30 /* mpeg1 */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
31 if (mode != 3)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
32 buf += 32;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
33 else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
34 buf += 17;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
35 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
36 else {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
37 /* mpeg2 */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
38 if (mode != 3)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
39 buf += 17;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
40 else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
41 buf += 9;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
42 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
43
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
44 if (strncmp((char *) buf, "Xing", 4))
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
45 return 0;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
46 buf += 4;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
47
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
48 head_flags = GET_INT32BE(buf);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
49
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
50 if (head_flags & FRAMES_FLAG)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
51 xing->frames = GET_INT32BE(buf);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
52 if (xing->frames < 1)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
53 return 0;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
54 if (head_flags & BYTES_FLAG)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
55 xing->bytes = GET_INT32BE(buf);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
56
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
57 if (head_flags & TOC_FLAG) {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
58 for (i = 0; i < 100; i++) {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
59 xing->toc[i] = buf[i];
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
60 if (i > 0 && xing->toc[i] < xing->toc[i - 1])
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
61 return 0;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
62 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
63 if (xing->toc[99] == 0)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
64 return 0;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
65 buf += 100;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
66 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
67 else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
68 for (i = 0; i < 100; i++)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
69 xing->toc[i] = (i * 256) / 100;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
70
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
71 #ifdef XING_DEBUG
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
72 for (i = 0; i < 100; i++) {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
73 if ((i % 10) == 0)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
74 fprintf(stderr, "\n");
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
75 fprintf(stderr, " %3d", xing->toc[i]);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
76 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
77 #endif
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
78
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
79 return 1;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
80 }
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
81
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
82 int
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
83 mpg123_seek_point(xing_header_t * xing, float percent)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
84 {
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
85 /* interpolate in TOC to get file seek point in bytes */
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
86 int a, seekpoint;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
87 float fa, fb, fx;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
88
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
89 percent = CLAMP(percent, 0, 100);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
90 a = MIN(percent, 99);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
91
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
92 fa = xing->toc[a];
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
93
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
94 if (a < 99)
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
95 fb = xing->toc[a + 1];
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
96 else
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
97 fb = 256;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
98
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
99 fx = fa + (fb - fa) * (percent - a);
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
100 seekpoint = (1.0f / 256.0f) * fx * xing->bytes;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
101
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
102 return seekpoint;
cb178e5ad177 [svn] Import audacious source.
nenolod
parents:
diff changeset
103 }