[FFmpeg-devel] [PATCH 1/2] movtextenc.c: Reorganize the code for easier maintenance

Niklesh Lalwani niklesh.lalwani at iitb.ac.in
Mon Jul 20 20:27:28 CEST 2015


From: Niklesh <niklesh.lalwani at iitb.ac.in>

This patch reorganizes the code to make it easier to add support for different text modifier boxes and other styles in the future.

Signed-off-by: Niklesh <niklesh.lalwani at iitb.ac.in>
---
 libavcodec/movtextenc.c | 104 ++++++++++++++++++++++++++++++------------------
 1 file changed, 65 insertions(+), 39 deletions(-)

diff --git a/libavcodec/movtextenc.c b/libavcodec/movtextenc.c
index 91b73ed..f3df6a3 100644
--- a/libavcodec/movtextenc.c
+++ b/libavcodec/movtextenc.c
@@ -35,6 +35,8 @@
 #define STYLE_RECORD_SIZE       12
 #define SIZE_ADD                10
 
+#define STYL_BOX   (1<<0)
+
 #define av_bprint_append_any(buf, data, size)   av_bprint_append_data(buf, ((const char*)data), size)
 
 typedef struct {
@@ -49,9 +51,7 @@ typedef struct {
     StyleBox **style_attributes;
     StyleBox *style_attributes_temp;
     int count;
-    uint8_t style_box_flag;
-    uint32_t tsmb_size;
-    uint32_t tsmb_type;
+    uint8_t box_flags;
     uint16_t style_entries;
     uint16_t style_fontID;
     uint8_t style_fontsize;
@@ -59,6 +59,55 @@ typedef struct {
     uint16_t text_pos;
 } MovTextContext;
 
+typedef struct {
+    uint32_t type;
+    void (*encode)(MovTextContext *s, uint32_t tsmb_type);
+} Box;
+
+static void mov_text_cleanup(MovTextContext *s)
+{
+    int j;
+    if (s->box_flags & STYL_BOX) {
+        for (j = 0; j < s->count; j++) {
+            av_freep(&s->style_attributes[j]);
+        }
+        av_freep(&s->style_attributes);
+    }
+}
+
+static void encode_styl(MovTextContext *s, uint32_t tsmb_type)
+{
+    int j;
+    uint32_t tsmb_size;
+    if (s->box_flags & STYL_BOX) {
+        tsmb_size = s->count * STYLE_RECORD_SIZE + SIZE_ADD;
+        tsmb_size = AV_RB32(&tsmb_size);
+        s->style_entries = AV_RB16(&s->count);
+        s->style_fontID = 0x00 | 0x01<<8;
+        s->style_fontsize = 0x12;
+        s->style_color = MKTAG(0xFF, 0xFF, 0xFF, 0xFF);
+        /*The above three attributes are hard coded for now
+        but will come from ASS style in the future*/
+        av_bprint_append_any(&s->buffer, &tsmb_size, 4);
+        av_bprint_append_any(&s->buffer, &tsmb_type, 4);
+        av_bprint_append_any(&s->buffer, &s->style_entries, 2);
+        for (j = 0; j < s->count; j++) {
+            av_bprint_append_any(&s->buffer, &s->style_attributes[j]->style_start, 2);
+            av_bprint_append_any(&s->buffer, &s->style_attributes[j]->style_end, 2);
+            av_bprint_append_any(&s->buffer, &s->style_fontID, 2);
+            av_bprint_append_any(&s->buffer, &s->style_attributes[j]->style_flag, 1);
+            av_bprint_append_any(&s->buffer, &s->style_fontsize, 1);
+            av_bprint_append_any(&s->buffer, &s->style_color, 4);
+        }
+        mov_text_cleanup(s);
+    }
+}
+
+static const Box box_types[] = {
+    { MKTAG('s','t','y','l'), encode_styl },
+};
+
+const static size_t box_count = FF_ARRAY_ELEMS(box_types);
 
 static av_cold int mov_text_encode_init(AVCodecContext *avctx)
 {
@@ -116,13 +165,13 @@ static void mov_text_style_cb(void *priv, const char style, int close)
 {
     MovTextContext *s = priv;
     if (!close) {
-        if (s->style_box_flag == 0) {   //first style entry
+        if (!(s->box_flags & STYL_BOX)) {   //first style entry
 
             s->style_attributes_temp = av_malloc(sizeof(*s->style_attributes_temp));
 
             if (!s->style_attributes_temp) {
                 av_bprint_clear(&s->buffer);
-                s->style_box_flag = 0;
+                s->box_flags &= ~STYL_BOX;
                 return;
             }
 
@@ -132,12 +181,11 @@ static void mov_text_style_cb(void *priv, const char style, int close)
             if (s->style_attributes_temp->style_flag) { //break the style record here and start a new one
                 s->style_attributes_temp->style_end = AV_RB16(&s->text_pos);
                 av_dynarray_add(&s->style_attributes, &s->count, s->style_attributes_temp);
-
                 s->style_attributes_temp = av_malloc(sizeof(*s->style_attributes_temp));
-
                 if (!s->style_attributes_temp) {
+                    mov_text_cleanup(s);
                     av_bprint_clear(&s->buffer);
-                    s->style_box_flag = 0;
+                    s->box_flags &= ~STYL_BOX;
                     return;
                 }
 
@@ -166,8 +214,9 @@ static void mov_text_style_cb(void *priv, const char style, int close)
         s->style_attributes_temp = av_malloc(sizeof(*s->style_attributes_temp));
 
         if (!s->style_attributes_temp) {
+            mov_text_cleanup(s);
             av_bprint_clear(&s->buffer);
-            s->style_box_flag = 0;
+            s->box_flags &= ~STYL_BOX;
             return;
         }
 
@@ -187,7 +236,7 @@ static void mov_text_style_cb(void *priv, const char style, int close)
             s->style_attributes_temp->style_start = AV_RB16(&s->text_pos);
         }
     }
-    s->style_box_flag = 1;
+    s->box_flags |= STYL_BOX;
 }
 
 static void mov_text_text_cb(void *priv, const char *text, int len)
@@ -215,13 +264,13 @@ static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf,
 {
     MovTextContext *s = avctx->priv_data;
     ASSDialog *dialog;
-    int i, j, num, length;
+    int i, num, length;
+    size_t j;
 
     s->text_pos = 0;
     s->count = 0;
-    s->style_box_flag = 0;
+    s->box_flags = 0;
     s->style_entries = 0;
-
     for (i = 0; i < sub->num_rects; i++) {
 
         if (sub->rects[i]->type != SUBTITLE_ASS) {
@@ -233,32 +282,9 @@ static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf,
         for (; dialog && num--; dialog++) {
             ff_ass_split_override_codes(&mov_text_callbacks, s, dialog->text);
         }
-        if (s->style_box_flag) {
-            s->tsmb_size = s->count * STYLE_RECORD_SIZE + SIZE_ADD;  //size of one style record is 12 bytes
-            s->tsmb_size = AV_RB32(&s->tsmb_size);
-            s->tsmb_type = MKTAG('s','t','y','l');
-            s->style_entries = AV_RB16(&s->count);
-            s->style_fontID = 0x00 | 0x01<<8;
-            s->style_fontsize = 0x12;
-            s->style_color = MKTAG(0xFF, 0xFF, 0xFF, 0xFF);
-            /*The above three attributes are hard coded for now
-            but will come from ASS style in the future*/
-            av_bprint_append_any(&s->buffer, &s->tsmb_size, 4);
-            av_bprint_append_any(&s->buffer, &s->tsmb_type, 4);
-            av_bprint_append_any(&s->buffer, &s->style_entries, 2);
-            for (j = 0; j < s->count; j++) {
-                av_bprint_append_any(&s->buffer, &s->style_attributes[j]->style_start, 2);
-                av_bprint_append_any(&s->buffer, &s->style_attributes[j]->style_end, 2);
-                av_bprint_append_any(&s->buffer, &s->style_fontID, 2);
-                av_bprint_append_any(&s->buffer, &s->style_attributes[j]->style_flag, 1);
-                av_bprint_append_any(&s->buffer, &s->style_fontsize, 1);
-                av_bprint_append_any(&s->buffer, &s->style_color, 4);
-            }
-            for (j = 0; j < s->count; j++) {
-                av_freep(&s->style_attributes[j]);
-            }
-            av_freep(&s->style_attributes);
-            av_freep(&s->style_attributes_temp);
+
+        for (j = 0; j < box_count; j++) {
+            box_types[j].encode(s, box_types[j].type);
         }
     }
 
-- 
1.9.1



More information about the ffmpeg-devel mailing list