diff mbox series

[FFmpeg-devel,4/4] avcodec/movtextdec: Sanitize style entries

Message ID AM7PR03MB66601AEEEF9A1993ADCC9EFC8F6E9@AM7PR03MB6660.eurprd03.prod.outlook.com
State Accepted
Commit cc1251ab814374ffb27cdebf2099b11a422d8187
Headers show
Series [FFmpeg-devel,1/4] avcodec/movtextdec: Rename several structure elements
Related show

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Andreas Rheinhardt Dec. 7, 2021, 11:24 p.m. UTC
There are three types of style entries which are redundant:
a) Entries with length zero. They are already discarded.
b) Entries that are equivalent to the default style:
They can be safely discarded.
c) Entries that are equivalent to the immediately preceding style
if the start of the current style coincides with the end of the
preceding style. In this case the styles can be merged.

This commit implements discarding/merging in cases b) and c).
This fixes ticket #9548. In said ticket each packet contained
exactly one style entry that covered the complete packet with
the exception of the last character (probably created by a tool
that didn't know that the style's end is exclusive). Said style
coincided with the default style, leading to a superfluous reset,
which is now gone.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/movtextdec.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
diff mbox series

Patch

diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c
index c50626c0b5..b0c54bf1d0 100644
--- a/libavcodec/movtextdec.c
+++ b/libavcodec/movtextdec.c
@@ -263,6 +263,14 @@  static int decode_hclr(const uint8_t *tsmb, MovTextContext *m, const AVPacket *a
     return 0;
 }
 
+static int styles_equivalent(const StyleBox *a, const StyleBox *b)
+{
+#define CMP(field) a->field == b->field
+    return CMP(bold)  && CMP(italic)   && CMP(underline) && CMP(color) &&
+           CMP(alpha) && CMP(fontsize) && CMP(font_id);
+#undef CMP
+}
+
 static int decode_styl(const uint8_t *tsmb, MovTextContext *m, const AVPacket *avpkt)
 {
     int i;
@@ -299,6 +307,19 @@  static int decode_styl(const uint8_t *tsmb, MovTextContext *m, const AVPacket *a
         }
 
         mov_text_parse_style_record(style, &tsmb);
+        if (styles_equivalent(style, &m->d.style)) {
+            /* Skip this style as it is equivalent to the default style */
+            m->style_entries--;
+            i--;
+            continue;
+        } else if (i && style->start == style[-1].end &&
+                   styles_equivalent(style, &style[-1])) {
+            /* Merge the two adjacent styles */
+            style[-1].end = style->end;
+            m->style_entries--;
+            i--;
+            continue;
+        }
     }
     return 0;
 }