+static int find_sync_word(unsigned char *buf, int nBytes)\r
+{\r
+ unsigned char *p, *pe;\r
+\r
+ /* find byte-aligned syncword - need 12 (MPEG 1,2) or 11 (MPEG 2.5) matching bits */\r
+ for (p = buf, pe = buf + nBytes - 4; p < pe; p++)\r
+ {\r
+ int pn;\r
+ if (p[0] != 0xff) continue;\r
+ pn = p[1];\r
+ if ((pn & 0xf8) != 0xf8 || // currently must be MPEG1\r
+ (pn & 6) == 0) { // invalid layer\r
+ p++; continue;\r
+ }\r
+ pn = p[2];\r
+ if ((pn & 0xf0) < 0x20 || (pn & 0xf0) == 0xf0 || // bitrates\r
+ (pn & 0x0c) != 0) { // not 44kHz\r
+ continue;\r
+ }\r
+\r
+ return p - buf;\r
+ }\r
+\r
+ return -1;\r
+}\r
+\r