From 43ffe8b6b337da8e7fc5644aba94e23f88f89a47 Mon Sep 17 00:00:00 2001 From: Jaroslav Kysela Date: Thu, 4 Oct 2018 09:04:28 +0200 Subject: [PATCH] parser teletext: try to avoid duplicate subtitles, fixes #3996 --- src/parsers/parser_teletext.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/parsers/parser_teletext.c b/src/parsers/parser_teletext.c index 409cf1f59..5b5168f64 100644 --- a/src/parsers/parser_teletext.c +++ b/src/parsers/parser_teletext.c @@ -40,6 +40,8 @@ #include "parsers.h" #include "parser_teletext.h" +#define MAX_SUB_TEXT_SIZE 2000 + /** * */ @@ -49,6 +51,8 @@ typedef struct tt_mag { uint8_t ttm_charset[2]; uint8_t ttm_national; uint8_t ttm_page[23*40 + 1]; + int64_t ttm_last_sub_pts; + uint8_t ttm_last_sub_text[MAX_SUB_TEXT_SIZE]; } tt_mag_t; @@ -700,7 +704,7 @@ extract_subtitle(parser_t *t, parser_es_t *st, tt_mag_t *ttm, int64_t pts) { int i, j, start, off = 0; int k, current_color, is_font_tag_open, use_color_subs = 1; - uint8_t sub[2000]; + uint8_t sub[MAX_SUB_TEXT_SIZE]; uint8_t *cset, ch; int is_box = 0; @@ -722,7 +726,7 @@ extract_subtitle(parser_t *t, parser_es_t *st, tt_mag_t *ttm, int64_t pts) if (ch != current_color) { if (is_font_tag_open) { for (k = 0; k < 7; k++) { - if (off < sizeof(sub)) + if (off < sizeof(sub) - 1) sub[off++] = CLOSE_FONT[k]; } is_font_tag_open = 0; @@ -730,7 +734,7 @@ extract_subtitle(parser_t *t, parser_es_t *st, tt_mag_t *ttm, int64_t pts) /* no need for font-tag for default-color */ if (ch != 7) { for (k = 0; k < 22; k++) { - if (off < sizeof(sub)) + if (off < sizeof(sub) - 1) sub[off++] = COLOR_MAP[ch][k]; } is_font_tag_open = 1; @@ -757,16 +761,16 @@ extract_subtitle(parser_t *t, parser_es_t *st, tt_mag_t *ttm, int64_t pts) if (ucs2 == 0) { /* nothing */ } else if (ucs2 < 0x80) { - if (off < sizeof(sub)) + if (off < sizeof(sub) - 1) sub[off++] = ucs2; } else if (ucs2 < 0x800) { - if (off + 1 < sizeof(sub)) { + if (off + 1 < sizeof(sub) - 1) { sub[off++] = (ucs2 >> 6) | 0xc0; sub[off++] = (ucs2 & 0x3f) | 0x80; } } else { assert(ucs2 < 0xd800 || ucs2 >= 0xe000); - if (off + 2 < sizeof(sub)) { + if (off + 2 < sizeof(sub) - 1) { sub[off++] = (ucs2 >> 12) | 0xe0; sub[off++] = ((ucs2 >> 6) & 0x3f) | 0x80; sub[off++] = (ucs2 & 0x3f) | 0x80; @@ -777,12 +781,12 @@ extract_subtitle(parser_t *t, parser_es_t *st, tt_mag_t *ttm, int64_t pts) } if (use_color_subs && is_font_tag_open) { for (k = 0; k < 7; k++) { - if (off < sizeof(sub)) + if (off < sizeof(sub) - 1) sub[off++] = CLOSE_FONT[k]; } is_font_tag_open = 0; } - if(start != off && off < sizeof(sub)) + if(start != off && off < sizeof(sub) - 1) sub[off++] = '\n'; } @@ -793,6 +797,15 @@ extract_subtitle(parser_t *t, parser_es_t *st, tt_mag_t *ttm, int64_t pts) sub[off++] = 0; + /* Check for a duplicate */ + if ((ttm->ttm_last_sub_pts == pts || + ttm->ttm_last_sub_pts == pts - 1) && + strcmp((char *)ttm->ttm_last_sub_text, (char *)sub) == 0) + return 0; + + ttm->ttm_last_sub_pts = pts; + strcpy((char *)ttm->ttm_last_sub_text, (char *)sub); + th_pkt_t *pkt = pkt_alloc(st->es_type, sub, off, pts, pts, pts); pkt->pkt_componentindex = st->es_index; -- 2.47.2