diff mbox series

[FFmpeg-devel,5/6] avcodec/movtextdec: Switch to pointer comparisons and bytestream API

Message ID AM7PR03MB6660D8AC2016B46688E6DC748F6F9@AM7PR03MB6660.eurprd03.prod.outlook.com
State Accepted
Commit bbc866c9c9e3674cb49eceba7ed41f92d8ff7f61
Headers show
Series [FFmpeg-devel,1/6] avcodec/movtextdec: Switch to smaller type | expand

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. 8, 2021, 8:23 p.m. UTC
Improves readability and avoids a redundant index variable
that was mistakenly called "tracksize".

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

Patch

diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c
index 967c0adf7f..63709eb54b 100644
--- a/libavcodec/movtextdec.c
+++ b/libavcodec/movtextdec.c
@@ -102,7 +102,6 @@  typedef struct {
     MovTextDefault d;
     uint8_t box_flags;
     uint16_t style_entries, ftab_entries;
-    uint64_t tracksize;
     int readorder;
     int frame_width;
     int frame_height;
@@ -481,9 +480,7 @@  static int mov_text_decode_frame(AVCodecContext *avctx,
     int ret;
     AVBPrint buf;
     const char *ptr = avpkt->data, *end;
-    int text_length, tsmb_type, ret_tsmb;
-    uint64_t tsmb_size;
-    const uint8_t *tsmb;
+    int text_length;
     size_t i;
 
     if (!ptr || avpkt->size < 2)
@@ -510,27 +507,23 @@  static int mov_text_decode_frame(AVCodecContext *avctx,
 
     mov_text_cleanup(m);
 
-    tsmb_size = 0;
-    m->tracksize = 2 + text_length;
     m->style_entries = 0;
     m->box_flags = 0;
     // Note that the spec recommends lines be no longer than 2048 characters.
     av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
-    if (text_length + 2 != avpkt->size) {
-        while (m->tracksize + 8 <= avpkt->size) {
-            int size_var;
-            // A box is a minimum of 8 bytes.
-            tsmb = ptr + m->tracksize - 2;
-            tsmb_size = AV_RB32(tsmb);
-            tsmb += 4;
-            tsmb_type = AV_RB32(tsmb);
-            tsmb += 4;
+    if (text_length + 2 < avpkt->size) {
+        const uint8_t *tsmb = end;
+        const uint8_t *const tsmb_end = avpkt->data + avpkt->size;
+        // A box is a minimum of 8 bytes.
+        while (tsmb_end - tsmb >= 8) {
+            uint64_t tsmb_size = bytestream_get_be32(&tsmb);
+            uint32_t tsmb_type = bytestream_get_be32(&tsmb);
+            int size_var, ret_tsmb;
 
             if (tsmb_size == 1) {
-                if (m->tracksize + 16 > avpkt->size)
+                if (tsmb_end - tsmb < 8)
                     break;
-                tsmb_size = AV_RB64(tsmb);
-                tsmb += 8;
+                tsmb_size = bytestream_get_be64(&tsmb);
                 size_var = 16;
             } else
                 size_var = 8;
@@ -540,13 +533,11 @@  static int mov_text_decode_frame(AVCodecContext *avctx,
                 av_log(avctx, AV_LOG_ERROR, "tsmb_size invalid\n");
                 return AVERROR_INVALIDDATA;
             }
+            tsmb_size -= size_var;
 
-            if (tsmb_size > avpkt->size - m->tracksize)
+            if (tsmb_end - tsmb < tsmb_size)
                 break;
 
-            m->tracksize += tsmb_size;
-            tsmb_size -= size_var;
-
             for (i = 0; i < box_count; i++) {
                 if (tsmb_type == box_types[i].type) {
                     if (tsmb_size < box_types[i].base_size)
@@ -556,6 +547,7 @@  static int mov_text_decode_frame(AVCodecContext *avctx,
                         break;
                 }
             }
+            tsmb += tsmb_size;
         }
         text_to_ass(&buf, ptr, end, avctx);
         mov_text_cleanup(m);