+ if (idx >= 0) {
+ os = ogg->streams + idx;
+
+ ret = buf_realloc(os, size);
+ if (ret < 0)
+ return ret;
+
+ readout_buf = os->buf + os->bufpos;
+ } else {
+ readout_buf = av_malloc(size);
+ }
+
+ ret = avio_read(bc, readout_buf, size);
+ if (ret < size) {
+ if (idx < 0)
+ av_free(readout_buf);
+ return ret < 0 ? ret : AVERROR_EOF;
+ }
+
+ if (crc ^ ffio_get_checksum(bc)) {
+ av_log(s, AV_LOG_ERROR, "CRC mismatch!\n");
+ if (idx < 0)
+ av_free(readout_buf);
+ avio_seek(bc, start_pos, SEEK_SET);
+ *sid = -1;
+ return 0;
+ }
+
+ /* Since we're almost sure its a valid packet, checking the version after
+ * the checksum lets the demuxer be more tolerant */
+ if (version) {
+ av_log(s, AV_LOG_ERROR, "Invalid Ogg vers!\n");
+ if (idx < 0)
+ av_free(readout_buf);
+ avio_seek(bc, start_pos, SEEK_SET);
+ *sid = -1;
+ return 0;
+ }
+
+ /* CRC is correct so we can be 99% sure there's an actual change here */