diff mbox series

[FFmpeg-devel] avcodec/ccaption_dec: add support for colors

Message ID 20200614104257.1373-1-onemda@gmail.com
State Accepted
Commit cb98483ea081c0ea7949a75f9776703f395f8e8d
Headers show
Series [FFmpeg-devel] avcodec/ccaption_dec: add support for colors | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Paul B Mahol June 14, 2020, 10:42 a.m. UTC
Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 libavcodec/ccaption_dec.c | 43 +++++++++++++++++++++++++++++++--------
 1 file changed, 35 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/ccaption_dec.c b/libavcodec/ccaption_dec.c
index 936e2db27f..a953181e53 100644
--- a/libavcodec/ccaption_dec.c
+++ b/libavcodec/ccaption_dec.c
@@ -32,10 +32,6 @@ 
 
 static const AVRational ms_tb = {1, 1000};
 
-/*
- * TODO list
- * 1) handle font and color completely
- */
 enum cc_mode {
     CCMODE_POPON,
     CCMODE_PAINTON,
@@ -319,11 +315,13 @@  static void write_char(CCaptionSubContext *ctx, struct Screen *screen, char ch)
     uint8_t col = ctx->cursor_column;
     char *row = screen->characters[ctx->cursor_row];
     char *font = screen->fonts[ctx->cursor_row];
+    char *color = screen->colors[ctx->cursor_row];
     char *charset = screen->charsets[ctx->cursor_row];
 
     if (col < SCREEN_COLUMNS) {
         row[col] = ch;
         font[col] = ctx->cursor_font;
+        color[col] = ctx->cursor_color;
         charset[col] = ctx->cursor_charset;
         ctx->cursor_charset = CCSET_BASIC_AMERICAN;
         if (ch) ctx->cursor_column++;
@@ -437,6 +435,7 @@  static int capture_screen(CCaptionSubContext *ctx)
     int i, j, tab = 0;
     struct Screen *screen = ctx->screen + ctx->active_screen;
     enum cc_font prev_font = CCFONT_REGULAR;
+    enum cc_color_code prev_color = CCCOL_WHITE;
     av_bprint_clear(&ctx->buffer);
 
     for (i = 0; screen->row_used && i < SCREEN_ROWS; i++)
@@ -457,6 +456,7 @@  static int capture_screen(CCaptionSubContext *ctx)
         if (CHECK_FLAG(screen->row_used, i)) {
             const char *row = screen->characters[i];
             const char *font = screen->fonts[i];
+            const char *color = screen->colors[i];
             const char *charset = screen->charsets[i];
             const char *override;
             int x, y, seen_char = 0;
@@ -471,7 +471,7 @@  static int capture_screen(CCaptionSubContext *ctx)
             av_bprintf(&ctx->buffer, "{\\an7}{\\pos(%d,%d)}", x, y);
 
             for (; j < SCREEN_COLUMNS; j++) {
-                const char *e_tag = "", *s_tag = "";
+                const char *e_tag = "", *s_tag = "", *c_tag = "";
 
                 if (row[j] == 0)
                     break;
@@ -500,15 +500,42 @@  static int capture_screen(CCaptionSubContext *ctx)
                         break;
                     }
                 }
+                if (prev_color != color[j]) {
+                    switch (color[j]) {
+                    case CCCOL_WHITE:
+                        c_tag = "{\\c&HFFFFFF&}";
+                        break;
+                    case CCCOL_GREEN:
+                        c_tag = "{\\c&H00FF00&}";
+                        break;
+                    case CCCOL_BLUE:
+                        c_tag = "{\\c&HFF0000&}";
+                        break;
+                    case CCCOL_CYAN:
+                        c_tag = "{\\c&HFFFF00&}";
+                        break;
+                    case CCCOL_RED:
+                        c_tag = "{\\c&H0000FF&}";
+                        break;
+                    case CCCOL_YELLOW:
+                        c_tag = "{\\c&H00FFFF&}";
+                        break;
+                    case CCCOL_MAGENTA:
+                        c_tag = "{\\c&HFF00FF&}";
+                        break;
+                    }
+                }
+
                 prev_font = font[j];
+                prev_color = color[j];
                 override = charset_overrides[(int)charset[j]][(int)row[j]];
                 if (override) {
-                    av_bprintf(&ctx->buffer, "%s%s%s", e_tag, s_tag, override);
+                    av_bprintf(&ctx->buffer, "%s%s%s%s", e_tag, s_tag, c_tag, override);
                     seen_char = 1;
                 } else if (row[j] == ' ' && !seen_char) {
-                    av_bprintf(&ctx->buffer, "%s%s\\h", e_tag, s_tag);
+                    av_bprintf(&ctx->buffer, "%s%s%s\\h", e_tag, s_tag, c_tag);
                 } else {
-                    av_bprintf(&ctx->buffer, "%s%s%c", e_tag, s_tag, row[j]);
+                    av_bprintf(&ctx->buffer, "%s%s%s%c", e_tag, s_tag, c_tag, row[j]);
                     seen_char = 1;
                 }