|
15 | 15 | */ |
16 | 16 |
|
17 | 17 | #include "include/XINGSeeker.h" |
| 18 | +#include "include/avc_utils.h" |
18 | 19 |
|
19 | 20 | #include <media/stagefright/DataSource.h> |
20 | 21 | #include <media/stagefright/Utils.h> |
21 | 22 |
|
22 | 23 | namespace android { |
23 | 24 |
|
24 | | -static bool parse_xing_header( |
25 | | - const sp<DataSource> &source, off64_t first_frame_pos, |
26 | | - int32_t *frame_number = NULL, int32_t *byte_number = NULL, |
27 | | - unsigned char *table_of_contents = NULL, bool *toc_is_valid = NULL, |
28 | | - int32_t *quality_indicator = NULL, int64_t *duration = NULL); |
29 | | - |
30 | | -// static |
31 | | -sp<XINGSeeker> XINGSeeker::CreateFromSource( |
32 | | - const sp<DataSource> &source, off64_t first_frame_pos) { |
33 | | - sp<XINGSeeker> seeker = new XINGSeeker; |
34 | | - |
35 | | - seeker->mFirstFramePos = first_frame_pos; |
36 | | - |
37 | | - if (!parse_xing_header( |
38 | | - source, first_frame_pos, |
39 | | - NULL, &seeker->mSizeBytes, seeker->mTOC, &seeker->mTOCValid, |
40 | | - NULL, &seeker->mDurationUs)) { |
41 | | - return NULL; |
42 | | - } |
43 | | - |
44 | | - return seeker; |
45 | | -} |
46 | | - |
47 | 25 | XINGSeeker::XINGSeeker() |
48 | 26 | : mDurationUs(-1), |
49 | 27 | mSizeBytes(0) { |
@@ -91,134 +69,101 @@ bool XINGSeeker::getOffsetForTime(int64_t *timeUs, off64_t *pos) { |
91 | 69 | return true; |
92 | 70 | } |
93 | 71 |
|
94 | | -static bool parse_xing_header( |
95 | | - const sp<DataSource> &source, off64_t first_frame_pos, |
96 | | - int32_t *frame_number, int32_t *byte_number, |
97 | | - unsigned char *table_of_contents, bool *toc_valid, |
98 | | - int32_t *quality_indicator, |
99 | | - int64_t *duration) { |
100 | | - if (frame_number) { |
101 | | - *frame_number = 0; |
102 | | - } |
103 | | - if (byte_number) { |
104 | | - *byte_number = 0; |
105 | | - } |
106 | | - if (toc_valid) { |
107 | | - *toc_valid = false; |
108 | | - } |
109 | | - if (quality_indicator) { |
110 | | - *quality_indicator = 0; |
111 | | - } |
112 | | - if (duration) { |
113 | | - *duration = 0; |
114 | | - } |
| 72 | +// static |
| 73 | +sp<XINGSeeker> XINGSeeker::CreateFromSource( |
| 74 | + const sp<DataSource> &source, off64_t first_frame_pos) { |
| 75 | + sp<XINGSeeker> seeker = new XINGSeeker; |
| 76 | + |
| 77 | + seeker->mFirstFramePos = first_frame_pos; |
| 78 | + |
| 79 | + ALOGI("xingseeker first frame pos: %lld", first_frame_pos); |
| 80 | + |
| 81 | + seeker->mSizeBytes = 0; |
| 82 | + seeker->mTOCValid = false; |
| 83 | + seeker->mDurationUs = 0; |
115 | 84 |
|
116 | 85 | uint8_t buffer[4]; |
117 | 86 | int offset = first_frame_pos; |
118 | 87 | if (source->readAt(offset, &buffer, 4) < 4) { // get header |
119 | | - return false; |
| 88 | + return NULL; |
120 | 89 | } |
121 | 90 | offset += 4; |
122 | 91 |
|
123 | | - uint8_t id, layer, sr_index, mode; |
124 | | - layer = (buffer[1] >> 1) & 3; |
125 | | - id = (buffer[1] >> 3) & 3; |
126 | | - sr_index = (buffer[2] >> 2) & 3; |
127 | | - mode = (buffer[3] >> 6) & 3; |
128 | | - if (layer == 0) { |
129 | | - return false; |
130 | | - } |
131 | | - if (id == 1) { |
132 | | - return false; |
133 | | - } |
134 | | - if (sr_index == 3) { |
135 | | - return false; |
| 92 | + int header = U32_AT(buffer);; |
| 93 | + size_t xingframesize = 0; |
| 94 | + int sampling_rate = 0; |
| 95 | + int num_channels; |
| 96 | + int samples_per_frame = 0; |
| 97 | + if (!GetMPEGAudioFrameSize(header, &xingframesize, &sampling_rate, &num_channels, |
| 98 | + NULL, &samples_per_frame)) { |
| 99 | + return NULL; |
136 | 100 | } |
| 101 | + seeker->mFirstFramePos += xingframesize; |
| 102 | + |
| 103 | + uint8_t version = (buffer[1] >> 3) & 3; |
| 104 | + |
137 | 105 | // determine offset of XING header |
138 | | - if(id&1) { // mpeg1 |
139 | | - if (mode != 3) offset += 32; |
| 106 | + if(version & 1) { // mpeg1 |
| 107 | + if (num_channels != 1) offset += 32; |
140 | 108 | else offset += 17; |
141 | | - } else { // mpeg2 |
142 | | - if (mode != 3) offset += 17; |
| 109 | + } else { // mpeg 2 or 2.5 |
| 110 | + if (num_channels != 1) offset += 17; |
143 | 111 | else offset += 9; |
144 | 112 | } |
145 | 113 |
|
146 | 114 | if (source->readAt(offset, &buffer, 4) < 4) { // XING header ID |
147 | | - return false; |
| 115 | + return NULL; |
148 | 116 | } |
149 | 117 | offset += 4; |
150 | 118 | // Check XING ID |
151 | 119 | if ((buffer[0] != 'X') || (buffer[1] != 'i') |
152 | 120 | || (buffer[2] != 'n') || (buffer[3] != 'g')) { |
153 | 121 | if ((buffer[0] != 'I') || (buffer[1] != 'n') |
154 | 122 | || (buffer[2] != 'f') || (buffer[3] != 'o')) { |
155 | | - return false; |
| 123 | + return NULL; |
156 | 124 | } |
157 | 125 | } |
158 | 126 |
|
159 | 127 | if (source->readAt(offset, &buffer, 4) < 4) { // flags |
160 | | - return false; |
| 128 | + return NULL; |
161 | 129 | } |
162 | 130 | offset += 4; |
163 | 131 | uint32_t flags = U32_AT(buffer); |
164 | 132 |
|
165 | 133 | if (flags & 0x0001) { // Frames field is present |
166 | 134 | if (source->readAt(offset, buffer, 4) < 4) { |
167 | | - return false; |
168 | | - } |
169 | | - if (frame_number) { |
170 | | - *frame_number = U32_AT(buffer); |
171 | | - } |
172 | | - int32_t frame = U32_AT(buffer); |
173 | | - // Samples per Frame: 1. index = MPEG Version ID, 2. index = Layer |
174 | | - const int samplesPerFrames[2][3] = |
175 | | - { |
176 | | - { 384, 1152, 576 }, // MPEG 2, 2.5: layer1, layer2, layer3 |
177 | | - { 384, 1152, 1152 }, // MPEG 1: layer1, layer2, layer3 |
178 | | - }; |
179 | | - // sampling rates in hertz: 1. index = MPEG Version ID, 2. index = sampling rate index |
180 | | - const int samplingRates[4][3] = |
181 | | - { |
182 | | - { 11025, 12000, 8000, }, // MPEG 2.5 |
183 | | - { 0, 0, 0, }, // reserved |
184 | | - { 22050, 24000, 16000, }, // MPEG 2 |
185 | | - { 44100, 48000, 32000, } // MPEG 1 |
186 | | - }; |
187 | | - if (duration) { |
188 | | - *duration = (int64_t)frame * samplesPerFrames[id&1][3-layer] * 1000000LL |
189 | | - / samplingRates[id][sr_index]; |
| 135 | + return NULL; |
190 | 136 | } |
| 137 | + int32_t frames = U32_AT(buffer); |
| 138 | + seeker->mDurationUs = (int64_t)frames * samples_per_frame * 1000000LL / sampling_rate; |
191 | 139 | offset += 4; |
192 | 140 | } |
193 | 141 | if (flags & 0x0002) { // Bytes field is present |
194 | | - if (byte_number) { |
195 | | - if (source->readAt(offset, buffer, 4) < 4) { |
196 | | - return false; |
197 | | - } |
198 | | - *byte_number = U32_AT(buffer); |
| 142 | + if (source->readAt(offset, buffer, 4) < 4) { |
| 143 | + return NULL; |
199 | 144 | } |
| 145 | + seeker->mSizeBytes = U32_AT(buffer); |
200 | 146 | offset += 4; |
201 | 147 | } |
202 | 148 | if (flags & 0x0004) { // TOC field is present |
203 | | - if (table_of_contents) { |
204 | | - if (source->readAt(offset + 1, table_of_contents, 99) < 99) { |
205 | | - return false; |
206 | | - } |
207 | | - if (toc_valid) { |
208 | | - *toc_valid = true; |
209 | | - } |
| 149 | + if (source->readAt(offset + 1, seeker->mTOC, 99) < 99) { |
| 150 | + return NULL; |
210 | 151 | } |
| 152 | + seeker->mTOCValid = true; |
211 | 153 | offset += 100; |
212 | 154 | } |
| 155 | + |
| 156 | +#if 0 |
213 | 157 | if (flags & 0x0008) { // Quality indicator field is present |
214 | | - if (quality_indicator) { |
215 | | - if (source->readAt(offset, buffer, 4) < 4) { |
216 | | - return false; |
217 | | - } |
218 | | - *quality_indicator = U32_AT(buffer); |
| 158 | + if (source->readAt(offset, buffer, 4) < 4) { |
| 159 | + return NULL; |
219 | 160 | } |
| 161 | + // do something with the quality indicator |
| 162 | + offset += 4; |
220 | 163 | } |
221 | | - return true; |
| 164 | +#endif |
| 165 | + |
| 166 | + return seeker; |
222 | 167 | } |
223 | 168 |
|
224 | 169 | } // namespace android |
|
0 commit comments