[FFmpeg-devel] [PATCH] movtextenc.c: Add support for text highlighting

Niklesh Lalwani niklesh.lalwani at iitb.ac.in
Mon Jul 20 10:12:24 CEST 2015


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

This patch adds support for secondary color changes through highlight and hilightcolor box.
The code is also reorganised to make it easier to read and maintain.

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

diff --git a/libavcodec/movtextenc.c b/libavcodec/movtextenc.c
index 91b73ed..3219858 100644
--- a/libavcodec/movtextenc.c
+++ b/libavcodec/movtextenc.c
@@ -35,6 +35,10 @@
 #define STYLE_RECORD_SIZE       12
 #define SIZE_ADD                10
 
+#define STYL_BOX   (1<<0)
+#define HLIT_BOX   (1<<1)
+#define HCLR_BOX   (1<<2)
+
 #define av_bprint_append_any(buf, data, size)   av_bprint_append_data(buf, ((const char*)data), size)
 
 typedef struct {
@@ -44,14 +48,23 @@ typedef struct {
 } StyleBox;
 
 typedef struct {
+    uint16_t start;
+    uint16_t end;
+} HighlightBox;
+
+typedef struct {
+   uint32_t color;
+} HilightcolorBox;
+
+typedef struct {
     ASSSplitContext *ass_ctx;
     AVBPrint buffer;
     StyleBox **style_attributes;
     StyleBox *style_attributes_temp;
+    HighlightBox hlit;
+    HilightcolorBox hclr;
     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 +72,82 @@ 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 void encode_hlit(MovTextContext *s, uint32_t tsmb_type)
+{
+    uint32_t tsmb_size;
+    if (s->box_flags & HLIT_BOX) {
+        tsmb_size = 12;
+        tsmb_size = AV_RB32(&tsmb_size);
+        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->hlit.start, 2);
+        av_bprint_append_any(&s->buffer, &s->hlit.end, 2);
+    }
+}
+
+static void encode_hclr(MovTextContext *s, uint32_t tsmb_type)
+{
+    uint32_t tsmb_size;
+    if (s->box_flags & HCLR_BOX) {
+        tsmb_size = 12;
+        tsmb_size = AV_RB32(&tsmb_size);
+        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->hclr.color, 4);
+    }
+}
+
+static const Box box_types[] = {
+    { MKTAG('s','t','y','l'), encode_styl },
+    { MKTAG('h','l','i','t'), encode_hlit },
+    { MKTAG('h','c','l','r'), encode_hclr },
+};
+
+const static size_t box_count = FF_ARRAY_ELEMS(box_types);
 
 static av_cold int mov_text_encode_init(AVCodecContext *avctx)
 {
@@ -116,13 +205,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 +221,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 +254,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 +276,26 @@ 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_color_cb(void *priv, unsigned int color, unsigned int color_id)
+{
+    MovTextContext *s = priv;
+    if (color_id == 2) {    //secondary color changes
+        if (s->box_flags & HLIT_BOX) {  //close tag
+            s->hlit.end = AV_RB16(&s->text_pos);
+        } else {
+            s->box_flags |= HCLR_BOX;
+            s->box_flags |= HLIT_BOX;
+            s->hlit.start = AV_RB16(&s->text_pos);
+            s->hclr.color = color | (0xFF << 24);  //set alpha value to FF
+        }
+    }
+    /* If there are more than one secondary color changes in ASS, take start of
+       first section and end of last section. Movtext allows only one
+       highlight box per sample.
+     */
 }
 
 static void mov_text_text_cb(void *priv, const char *text, int len)
@@ -208,6 +316,7 @@ static const ASSCodesCallbacks mov_text_callbacks = {
     .text     = mov_text_text_cb,
     .new_line = mov_text_new_line_cb,
     .style    = mov_text_style_cb,
+    .color    = mov_text_color_cb,
 };
 
 static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf,
@@ -215,13 +324,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 +342,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