···212212 return header1 ? (header1 == header2) : true;
213213}
214214215215+/* Helper function to read 4-byte in big endian format. */
216216+static void read_uint32be_mp3data(int fd, unsigned long *data)
217217+{
218218+#ifdef ROCKBOX_BIG_ENDIAN
219219+ (void)read(fd, (char*)data, 4);
220220+#else
221221+ (void)read(fd, (char*)data, 4);
222222+ *data = betoh32(*data);
223223+#endif
224224+}
225225+215226static unsigned long __find_next_frame(int fd, long *offset, long max_offset,
216227 unsigned long reference_header,
217228 int(*getfunc)(int fd, unsigned char *c),
218229 bool single_header)
219230{
220220- uint32_t header=0;
221221- uint32_t ref_header=0;
222222- long ref_header_pos=0;
231231+ unsigned long header=0;
223232 unsigned char tmp;
224233 long pos = 0;
225234226226- /* We will search until we find two consecutive MPEG frame headers with
235235+ /* We will search until we find two consecutive MPEG frame headers with
227236 * the same MPEG version, layer and sampling frequency. The first header
228237 * of this pair is assumed to be the first valid MPEG frame header of the
229238 * whole stream. */
···234243 return 0;
235244 header |= tmp;
236245 pos++;
237237-246246+238247 /* Abort if max_offset is reached. Stop parsing. */
239248 if (max_offset > 0 && pos > max_offset)
240249 return 0;
241241-250250+242251 if (is_mp3frameheader(header)) {
243252 if (single_header) {
244253 /* We search for one _single_ valid header that has the same
245245- * type as the reference_header (if reference_header != 0).
254254+ * type as the reference_header (if reference_header != 0).
246255 * In this case we are finished. */
247256 if (headers_have_same_type(reference_header, header))
248257 break;
249258 } else {
250250- /* The current header is valid. Compare it against the last
251251- one we found. NOTE: ref_header MUST come second! */
252252- if (headers_have_same_type(header, ref_header)) {
253253- /* Found a match, return the header and offset of the FIRST */
254254- header = ref_header;
255255- lseek(fd, ref_header_pos, SEEK_SET);
259259+ /* The current header is valid. Now gather the frame size,
260260+ * seek to this byte position and check if there is another
261261+ * valid MPEG frame header of the same type. */
262262+ struct mp3info info;
263263+264264+ /* Gather frame size from given header and seek to next
265265+ * frame header. */
266266+ mp3headerinfo(&info, header);
267267+ lseek(fd, info.frame_size-4, SEEK_CUR);
268268+269269+ /* Read possible next frame header and seek back to last frame
270270+ * headers byte position. */
271271+ reference_header = 0;
272272+ read_uint32be_mp3data(fd, &reference_header);
273273+ //
274274+ lseek(fd, -info.frame_size, SEEK_CUR);
275275+276276+ /* If the current header is of the same type as the previous
277277+ * header we are finished. */
278278+ if (headers_have_same_type(header, reference_header))
256279 break;
257257- }
258258- /* Otherwise look for another.. */
259259- ref_header = header;
260260- ref_header_pos = lseek(fd, 0, SEEK_CUR);
261280 }
262281 }
263263-282282+264283 } while (true);
265284266285 *offset = pos - 4;