+ p[i] = vbi_rev8(buf[4 + i]);
+ /* Unfortunately libzvbi does not expose page flags, and
+ * vbi_classify_page only checks MIP, so we have to manually
+ * decode the page flags and store the results. */
+ pmag = vbi_unham16p(p);
+ if (pmag >= 0 && pmag >> 3 == 0) { // We found a row 0 header
+ int page = vbi_unham16p(p + 2);
+ int flags1 = vbi_unham16p(p + 6);
+ int flags2 = vbi_unham16p(p + 8);
+ if (page >= 0 && flags1 >= 0 && flags2 >= 0) {
+ int pgno = ((pmag & 7) << 8) + page;
+ // Check for disabled NEWSFLASH flag and enabled SUBTITLE and SUPRESS_HEADER flags
+ ctx->subtitle_map[pgno] = (!(flags1 & 0x40) && flags1 & 0x80 && flags2 & 0x01);
+ // Propagate ERASE_PAGE flag for repeated page headers to work around a libzvbi bug
+ if (ctx->subtitle_map[pgno] && pgno == ctx->last_pgno) {
+ int last_byte9 = vbi_unham8(ctx->last_p5);
+ if (last_byte9 >= 0 && last_byte9 & 0x8) {
+ int byte9 = vbi_unham8(p[5]);
+ if (byte9 >= 0)
+ p[5] = vbi_ham8(byte9 | 0x8);
+ }
+ }
+ ctx->last_pgno = pgno;
+ ctx->last_p5 = p[5];
+ }
+ }