mp3_sync.c 581 B

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