opus fix comment skipping code

opus requires the comment header to be a valid file our codec attemps to skip the comment data
in order to reduce the ram allocated originally it caused files with large album art to skip
the beginning of tracks my first attempt at fixing this then caused files with low bitrates
to do the same while fixing files with large album art

This patch should fix both although the initial start might be a bit slower but
this shouldn't cause too much of an issue

Change-Id: Ia1c3561347894cc45f24bb2659436914f8f03b43
diff --git a/lib/rbcodec/codecs/opus.c b/lib/rbcodec/codecs/opus.c
index 89eb4fe..acae1aa 100644
--- a/lib/rbcodec/codecs/opus.c
+++ b/lib/rbcodec/codecs/opus.c
@@ -78,6 +78,29 @@
     return 0;
 }
 
+/* seek to comment header */
+static int seek_opus_tags(void)
+{
+    int pos = 0;
+    const int64_t maxpos = sizeof(OpusHeader) + 1024;
+    const char synccode[] = "OpusTags";
+    char buf[sizeof(synccode)];
+    ci->seek_buffer(0);
+    while (ci->read_filebuf(buf, 1) == 1 && ci->curpos < maxpos) {
+        if (buf[0] == synccode[0]) {
+            if (ci->read_filebuf(&buf[1], sizeof(buf) - 2) != sizeof(buf) - 2)
+                break;
+            if (memcmp(buf, synccode, sizeof(buf)) == 0) {
+                ci->seek_buffer(ci->curpos - sizeof(buf));
+                LOGF("OpusTags %ld", ci->curpos);
+                return 1;
+            }
+        }
+    }
+    /* comment header not found probably invalid file */
+    return 0;
+}
+
 /* The read/seek functions track absolute position within the stream */
 static int64_t get_next_page(ogg_sync_state *oy, ogg_page *og,
                                  int64_t boundary)
@@ -455,7 +478,12 @@
                     /* seek buffer directly to the first audio packet to avoid 
                        allocating space for huge comment packets
                        (embedded Album Art) */
-                        seek_ogg_page(ci->curpos - oy.returned);
+                        if (seek_opus_tags())
+                            seek_ogg_page(ci->curpos);
+                        else {
+                            LOGF("Could not find comment header");
+                            goto done;
+                        }
                     }
                     LOGF("sync reset");
                     ogg_sync_reset(&oy); /* next page */