b00a5fb2c6e0ff2eff104eeba063c41460d1ace1
[libpicofe.git] / common / mp3.c
1 #include "mp3.h"
2
3 int mp3_find_sync_word(const unsigned char *buf, int size)
4 {
5         const unsigned char *p, *pe;
6
7         /* find byte-aligned syncword - need 12 (MPEG 1,2) or 11 (MPEG 2.5) matching bits */
8         for (p = buf, pe = buf + size - 3; p <= pe; p++)
9         {
10                 int pn;
11                 if (p[0] != 0xff)
12                         continue;
13                 pn = p[1];
14                 if ((pn & 0xf8) != 0xf8 || // currently must be MPEG1
15                     (pn & 6) == 0) {       // invalid layer
16                         p++; continue;
17                 }
18                 pn = p[2];
19                 if ((pn & 0xf0) < 0x20 || (pn & 0xf0) == 0xf0 || // bitrates
20                     (pn & 0x0c) != 0) { // not 44kHz
21                         continue;
22                 }
23
24                 return p - buf;
25         }
26
27         return -1;
28 }
29