author | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
---|---|---|
committer | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
commit | 15318cad33835e4e2dc620d033e43cd930676cdd (patch) (unidiff) | |
tree | c2fa0399a2c47fda8e2cd0092c73a809d17f68eb /core/multimedia/opieplayer/libmpeg3/audio/header.c | |
download | opie-15318cad33835e4e2dc620d033e43cd930676cdd.zip opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2 |
Initial revision
Diffstat (limited to 'core/multimedia/opieplayer/libmpeg3/audio/header.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | core/multimedia/opieplayer/libmpeg3/audio/header.c | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/core/multimedia/opieplayer/libmpeg3/audio/header.c b/core/multimedia/opieplayer/libmpeg3/audio/header.c new file mode 100644 index 0000000..02b5e7c --- a/dev/null +++ b/core/multimedia/opieplayer/libmpeg3/audio/header.c | |||
@@ -0,0 +1,163 @@ | |||
1 | #include "mpeg3audio.h" | ||
2 | #include "tables.h" | ||
3 | #include "../libmpeg3.h" | ||
4 | #include "../mpeg3protos.h" | ||
5 | |||
6 | #include <stdio.h> | ||
7 | |||
8 | /* Return 1 if the head check doesn't find a header. */ | ||
9 | int mpeg3audio_head_check(unsigned long head) | ||
10 | { | ||
11 | if((head & 0xffe00000) != 0xffe00000) return 1; | ||
12 | if(!((head >> 17) & 3)) return 1; | ||
13 | if(((head >> 12) & 0xf) == 0xf) return 1; | ||
14 | if(!((head >> 12) & 0xf)) return 1; | ||
15 | if(((head >> 10) & 0x3) == 0x3 ) return 1; | ||
16 | if(((head >> 19) & 1) == 1 && ((head >> 17) & 3) == 3 && ((head >> 16) & 1) == 1) | ||
17 | return 1; | ||
18 | if((head & 0xffff0000) == 0xfffe0000) return 1; | ||
19 | |||
20 | return 0; | ||
21 | } | ||
22 | |||
23 | int mpeg3audio_decode_header(mpeg3audio_t *audio) | ||
24 | { | ||
25 | if(audio->newhead & (1 << 20)) | ||
26 | { | ||
27 | audio->lsf = (audio->newhead & (1 << 19)) ? 0x0 : 0x1; | ||
28 | audio->mpeg35 = 0; | ||
29 | } | ||
30 | else | ||
31 | { | ||
32 | audio->lsf = 1; | ||
33 | audio->mpeg35 = 1; | ||
34 | } | ||
35 | |||
36 | audio->layer = 4 - ((audio->newhead >> 17) & 3); | ||
37 | if(audio->mpeg35) | ||
38 | audio->sampling_frequency_code = 6 + ((audio->newhead >> 10) & 0x3); | ||
39 | else | ||
40 | audio->sampling_frequency_code = ((audio->newhead >> 10) & 0x3) + (audio->lsf * 3); | ||
41 | |||
42 | audio->error_protection = ((audio->newhead >> 16) & 0x1) ^ 0x1; | ||
43 | |||
44 | audio->bitrate_index = ((audio->newhead >> 12) & 0xf); | ||
45 | audio->padding = ((audio->newhead >> 9) & 0x1); | ||
46 | audio->extension = ((audio->newhead >> 8) & 0x1); | ||
47 | audio->mode = ((audio->newhead >> 6) & 0x3); | ||
48 | audio->mode_ext = ((audio->newhead >> 4) & 0x3); | ||
49 | audio->copyright = ((audio->newhead >> 3) & 0x1); | ||
50 | audio->original = ((audio->newhead >> 2) & 0x1); | ||
51 | audio->emphasis = audio->newhead & 0x3; | ||
52 | audio->channels = (audio->mode == MPG_MD_MONO) ? 1 : 2; | ||
53 | if(audio->channels > 1) | ||
54 | audio->single = -1; | ||
55 | else | ||
56 | audio->single = 3; | ||
57 | |||
58 | audio->prev_framesize = audio->framesize; | ||
59 | |||
60 | if(!audio->bitrate_index) return 1; | ||
61 | audio->bitrate = 1000 * mpeg3_tabsel_123[audio->lsf][audio->layer - 1][audio->bitrate_index]; | ||
62 | |||
63 | switch(audio->layer) | ||
64 | { | ||
65 | case 1: | ||
66 | audio->framesize = (long)mpeg3_tabsel_123[audio->lsf][0][audio->bitrate_index] * 12000; | ||
67 | audio->framesize /= mpeg3_freqs[audio->sampling_frequency_code]; | ||
68 | audio->framesize = ((audio->framesize + audio->padding) << 2) - 4; | ||
69 | break; | ||
70 | case 2: | ||
71 | audio->framesize = (long)mpeg3_tabsel_123[audio->lsf][1][audio->bitrate_index] * 144000; | ||
72 | audio->framesize /= mpeg3_freqs[audio->sampling_frequency_code]; | ||
73 | audio->framesize += audio->padding - 4; | ||
74 | break; | ||
75 | case 3: | ||
76 | if(audio->lsf) | ||
77 | audio->ssize = (audio->channels == 1) ? 9 : 17; | ||
78 | else | ||
79 | audio->ssize = (audio->channels == 1) ? 17 : 32; | ||
80 | if(audio->error_protection) | ||
81 | audio->ssize += 2; | ||
82 | audio->framesize = (long)mpeg3_tabsel_123[audio->lsf][2][audio->bitrate_index] * 144000; | ||
83 | audio->framesize /= mpeg3_freqs[audio->sampling_frequency_code] << (audio->lsf); | ||
84 | audio->framesize = audio->framesize + audio->padding - 4; | ||
85 | break; | ||
86 | default: | ||
87 | return 1; | ||
88 | } | ||
89 | |||
90 | if(audio->framesize > MAXFRAMESIZE) return 1; | ||
91 | |||
92 | return 0; | ||
93 | } | ||
94 | |||
95 | int mpeg3audio_read_frame_body(mpeg3audio_t *audio) | ||
96 | { | ||
97 | int i; | ||
98 | for(i = 0; i < audio->framesize; i++) | ||
99 | { | ||
100 | audio->bsbuf[i] = mpeg3bits_getbits(audio->astream, 8); | ||
101 | } | ||
102 | return 0; | ||
103 | } | ||
104 | |||
105 | /* Seek to the start of the previous header */ | ||
106 | int mpeg3audio_prev_header(mpeg3audio_t *audio) | ||
107 | { | ||
108 | int result = 0, i, len = (int)audio->avg_framesize; | ||
109 | |||
110 | for(i = 0; i < len && !result; i++) | ||
111 | { | ||
112 | mpeg3bits_getbits_reverse(audio->astream, 8); | ||
113 | } | ||
114 | /* Get reading in the forward direction again. */ | ||
115 | result |= mpeg3bits_refill(audio->astream); | ||
116 | return result; | ||
117 | } | ||
118 | |||
119 | /* Read the next header */ | ||
120 | int mpeg3audio_read_header(mpeg3audio_t *audio) | ||
121 | { | ||
122 | unsigned int code; | ||
123 | int i; | ||
124 | int attempt = 0; | ||
125 | int result = 0; | ||
126 | |||
127 | switch(audio->format) | ||
128 | { | ||
129 | case AUDIO_AC3: | ||
130 | result = mpeg3audio_read_ac3_header(audio); | ||
131 | break; | ||
132 | |||
133 | case AUDIO_MPEG: | ||
134 | /* Layer 1 not supported */ | ||
135 | if(audio->layer == 1) | ||
136 | { | ||
137 | fprintf(stderr, "mpeg3audio_new: layer 1 not supported\n"); | ||
138 | result = 1; | ||
139 | } | ||
140 | audio->newhead = mpeg3bits_showbits(audio->astream, 32); | ||
141 | if(!mpeg3bits_eof(audio->astream) && | ||
142 | (mpeg3audio_head_check(audio->newhead) || mpeg3audio_decode_header(audio))) | ||
143 | { | ||
144 | do | ||
145 | { | ||
146 | attempt++; | ||
147 | mpeg3bits_getbyte_noptr(audio->astream); | ||
148 | audio->newhead = mpeg3bits_showbits(audio->astream, 32); | ||
149 | }while(!mpeg3bits_eof(audio->astream) && | ||
150 | attempt < 65536 && | ||
151 | (mpeg3audio_head_check(audio->newhead) || mpeg3audio_decode_header(audio))); | ||
152 | } | ||
153 | |||
154 | /* Skip the 4 bytes containing the header */ | ||
155 | mpeg3bits_getbits(audio->astream, 32); | ||
156 | break; | ||
157 | |||
158 | case AUDIO_PCM: | ||
159 | mpeg3audio_read_pcm_header(audio); | ||
160 | break; | ||
161 | } | ||
162 | return mpeg3bits_eof(audio->astream); | ||
163 | } | ||