[FFmpeg-devel] [PATCH] movtextenc: Use default style information from ASS style

Niklesh Lalwani niklesh.lalwani at iitb.ac.in
Sun Aug 23 06:01:03 CEST 2015


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

Generate the default style for movtext from the ASS style. Earlier, we used a fixed default style.

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

diff --git a/libavcodec/movtextenc.c b/libavcodec/movtextenc.c
index 6d42d5f..87ec656 100644
--- a/libavcodec/movtextenc.c
+++ b/libavcodec/movtextenc.c
@@ -27,6 +27,7 @@
 #include "libavutil/mem.h"
 #include "libavutil/common.h"
 #include "ass_split.h"
+#include "ass_split.c"
 #include "ass.h"
 
 #define STYLE_FLAG_BOLD         (1<<0)
@@ -35,6 +36,16 @@
 #define STYLE_RECORD_SIZE       12
 #define SIZE_ADD                10
 
+#define BOTTOM_LEFT     1
+#define BOTTOM_CENTER   2
+#define BOTTOM_RIGHT    3
+#define MIDDLE_LEFT     4
+#define MIDDLE_CENTER   5
+#define MIDDLE_RIGHT    6
+#define TOP_LEFT        7
+#define TOP_CENTER      8
+#define TOP_RIGHT       9
+
 #define STYL_BOX   (1<<0)
 #define HLIT_BOX   (1<<1)
 #define HCLR_BOX   (1<<2)
@@ -151,54 +162,97 @@ const static size_t box_count = FF_ARRAY_ELEMS(box_types);
 
 static av_cold int mov_text_encode_init(AVCodecContext *avctx)
 {
-    /*
-     * For now, we'll use a fixed default style. When we add styling
-     * support, this will be generated from the ASS style.
-     */
-    static const uint8_t text_sample_entry[] = {
-        0x00, 0x00, 0x00, 0x00, // uint32_t displayFlags
-        0x01,                   // int8_t horizontal-justification
-        0xFF,                   // int8_t vertical-justification
-        0x00, 0x00, 0x00, 0x00, // uint8_t background-color-rgba[4]
-        // BoxRecord {
+    ASSStyle *style;
+    AVBPrint text_sample_entry;
+    char name[] = "Default";
+    uint8_t displayFlags[] = {0x00, 0x00, 0x00, 0x00};
+    int8_t h_align, v_align, style_flags, font_name_length;
+    int background_color, text_color;
+    uint8_t BoxRecord[] = {
         0x00, 0x00,             // int16_t top
         0x00, 0x00,             // int16_t left
         0x00, 0x00,             // int16_t bottom
         0x00, 0x00,             // int16_t right
-        // };
-        // StyleRecord {
+    };
+    uint8_t Style_defaults[] = {
         0x00, 0x00,             // uint16_t startChar
         0x00, 0x00,             // uint16_t endChar
         0x00, 0x01,             // uint16_t font-ID
-        0x00,                   // uint8_t face-style-flags
-        0x12,                   // uint8_t font-size
-        0xFF, 0xFF, 0xFF, 0xFF, // uint8_t text-color-rgba[4]
-        // };
-        // FontTableBox {
+    };
+    uint8_t Font_defaults [] = {
         0x00, 0x00, 0x00, 0x12, // uint32_t size
         'f', 't', 'a', 'b',     // uint8_t name[4]
         0x00, 0x01,             // uint16_t entry-count
-        // FontRecord {
         0x00, 0x01,             // uint16_t font-ID
-        0x05,                   // uint8_t font-name-length
-        'S', 'e', 'r', 'i', 'f',// uint8_t font[font-name-length]
-        // };
-        // };
     };
 
     MovTextContext *s = avctx->priv_data;
 
-    avctx->extradata_size = sizeof text_sample_entry;
+    s->ass_ctx = ff_ass_split(avctx->subtitle_header);
+    if (!s->ass_ctx)
+        return AVERROR_INVALIDDATA;
+
+    av_bprint_init(&text_sample_entry, 0, AV_BPRINT_SIZE_UNLIMITED);
+
+    style = ff_ass_style_get(s->ass_ctx, name); /* Get the default style */
+
+    background_color = style->back_color << 8 | 0x00;
+    text_color = style->primary_color << 8 | 0xFF;
+    style_flags = style->bold | style->italic << 1 | style->underline << 2;
+    font_name_length = strlen(style->name);
+
+    switch(style->alignment) {
+        case BOTTOM_LEFT :
+            h_align = 0;
+            v_align = -1;
+        case BOTTOM_CENTER :
+            h_align = 1;
+            v_align = -1;
+        case BOTTOM_RIGHT :
+            h_align = -1;
+            v_align = -1;
+        case MIDDLE_LEFT :
+            h_align = 0;
+            v_align = 1;
+        case MIDDLE_CENTER :
+            h_align = 1;
+            v_align = 1;
+        case MIDDLE_RIGHT :
+            h_align = -1;
+            v_align = 1;
+        case TOP_LEFT :
+            h_align = 0;
+            v_align = 0;
+        case TOP_CENTER :
+            h_align = 1;
+            v_align = 0;
+        case TOP_RIGHT :
+            h_align = -1;
+            v_align = 0;
+    }
+
+    av_bprint_append_any(&text_sample_entry, displayFlags, 4);
+    av_bprint_append_any(&text_sample_entry, &h_align, 1);
+    av_bprint_append_any(&text_sample_entry, &v_align, 1);
+    av_bprint_append_any(&text_sample_entry, &background_color, 4);
+    av_bprint_append_any(&text_sample_entry, BoxRecord, 8);
+    av_bprint_append_any(&text_sample_entry, Style_defaults, 6);
+    av_bprint_append_any(&text_sample_entry, &style_flags, 1);
+    av_bprint_append_any(&text_sample_entry, &style->font_size, 1);
+    av_bprint_append_any(&text_sample_entry, &text_color, 4);
+    av_bprint_append_any(&text_sample_entry, Font_defaults, 12);
+    av_bprint_append_any(&text_sample_entry, &font_name_length, 1);
+    av_bprint_append_any(&text_sample_entry, style->name, font_name_length);
+
+    avctx->extradata_size = sizeof text_sample_entry.size;
     avctx->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
     if (!avctx->extradata)
         return AVERROR(ENOMEM);
+    memcpy(avctx->extradata, text_sample_entry.str, avctx->extradata_size);
+    av_bprint_finalize(&text_sample_entry, NULL);
 
     av_bprint_init(&s->buffer, 0, AV_BPRINT_SIZE_UNLIMITED);
-
-    memcpy(avctx->extradata, text_sample_entry, avctx->extradata_size);
-
-    s->ass_ctx = ff_ass_split(avctx->subtitle_header);
-    return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
+    return 0;
 }
 
 static void mov_text_style_cb(void *priv, const char style, int close)
-- 
1.9.1



More information about the ffmpeg-devel mailing list