summaryrefslogtreecommitdiffstats
path: root/libhb/muxcommon.c
diff options
context:
space:
mode:
authorJohn Stebbins <[email protected]>2019-01-07 10:52:28 -0700
committerJohn Stebbins <[email protected]>2019-01-14 13:36:08 -0800
commit1f52f52337028b977a83a0e1061b4cc36918c34f (patch)
tree7066a5ec5e4bc760ddfac69206eda40611bd7166 /libhb/muxcommon.c
parent31e676d288bb1be2f1d6096b26159ac92c7e143f (diff)
ssa: improve SSA to TX3G conversion
We were only applying SSA inline override tags. With this patch we now parse SSA style descritions in the SSA header and apply them per event.
Diffstat (limited to 'libhb/muxcommon.c')
-rw-r--r--libhb/muxcommon.c234
1 files changed, 0 insertions, 234 deletions
diff --git a/libhb/muxcommon.c b/libhb/muxcommon.c
index a61fc0803..5034b32e3 100644
--- a/libhb/muxcommon.c
+++ b/libhb/muxcommon.c
@@ -707,237 +707,3 @@ hb_work_object_t hb_muxer =
muxClose
};
-#define TX3G_STYLES (HB_STYLE_FLAG_BOLD | \
- HB_STYLE_FLAG_ITALIC | \
- HB_STYLE_FLAG_UNDERLINE)
-
-struct output_buf_s
-{
- int alloc;
- int size;
- uint8_t * buf;
-};
-
-typedef struct style_context_s
-{
- struct output_buf_s style_atoms;
- int style_atom_count;
- hb_subtitle_style_t current_style;
- int style_start;
- int height;
-} style_context_t;
-
-static int check_realloc_output(struct output_buf_s * output, int size)
-{
- if (output->alloc < size)
- {
- uint8_t * tmp;
-
- output->alloc = size + 1024;
- output->size = size;
- tmp = realloc(output->buf, output->alloc);
- if (tmp == NULL)
- {
- hb_error("realloc failed!");
- free(output->buf);
- output->size = 0;
- output->alloc = 0;
- output->buf = NULL;
- return 0;
- }
- output->buf = tmp;
- }
- return 1;
-}
-
-static int update_style_atoms(style_context_t *ctx, int stop)
-{
- uint8_t * style_entry;
- uint8_t face = 0;
- int font_size;
- int pos = 10 + (12 * ctx->style_atom_count);
- int size = 10 + (12 * (ctx->style_atom_count + 1));
-
- if (!check_realloc_output(&ctx->style_atoms, size))
- {
- return 0;
- }
- style_entry = ctx->style_atoms.buf + pos;
-
- if (ctx->current_style.flags & HB_STYLE_FLAG_BOLD)
- face |= 1;
- if (ctx->current_style.flags & HB_STYLE_FLAG_ITALIC)
- face |= 2;
- if (ctx->current_style.flags & HB_STYLE_FLAG_UNDERLINE)
- face |= 4;
-
- style_entry[0] = (ctx->style_start >> 8) & 0xff; // startChar
- style_entry[1] = ctx->style_start & 0xff;
- style_entry[2] = (stop >> 8) & 0xff; // endChar
- style_entry[3] = stop & 0xff;
- style_entry[4] = 0; // font-ID msb
- style_entry[5] = 1; // font-ID lsb
- style_entry[6] = face; // face-style-flags
- font_size = 0.05 * ctx->height;
- if (font_size < 12)
- {
- font_size = 12;
- }
- else if (font_size > 255)
- {
- font_size = 255;
- }
- style_entry[7] = font_size; // font-size
- style_entry[8] = (ctx->current_style.fg_rgb >> 16) & 0xff; // r
- style_entry[9] = (ctx->current_style.fg_rgb >> 8) & 0xff; // g
- style_entry[10] = (ctx->current_style.fg_rgb) & 0xff; // b
- style_entry[11] = ctx->current_style.fg_alpha; // a
-
- ctx->style_atom_count++;
-
- return 1;
-}
-
-static int update_style(style_context_t *ctx,
- hb_subtitle_style_t *style, int pos)
-{
- if (ctx->style_start < pos)
- {
- // do we need to add a style atom?
- if (((ctx->current_style.flags ^ style->flags) & TX3G_STYLES) ||
- ctx->current_style.fg_rgb != style->fg_rgb ||
- ctx->current_style.fg_alpha != style->fg_alpha)
- {
- if (!update_style_atoms(ctx, pos - 1))
- {
- return 0;
- }
- ctx->current_style = *style;
- ctx->style_start = pos;
- }
- }
- else
- {
- ctx->current_style = *style;
- ctx->style_start = pos;
- }
- return 1;
-}
-
-static void style_context_init(style_context_t *ctx)
-{
- memset(ctx, 0, sizeof(*ctx));
- ctx->style_atoms.buf = NULL;
- ctx->style_atoms.size = 0;
- ctx->style_atoms.alloc = 0;
- ctx->style_start = INT_MAX;
-}
-
-/*
- * Copy the input to output removing markup and adding markup to the style
- * atom where appropriate.
- */
-void hb_muxmp4_process_subtitle_style(int height,
- uint8_t * input,
- uint8_t ** out_buf,
- uint8_t ** out_style_atoms,
- uint16_t * stylesize)
-{
- uint16_t utf8_count = 0; // utf8 count from start of subtitle
- int consumed, in_pos = 0, out_pos = 0, len, ii;
- style_context_t ctx;
- hb_subtitle_style_t style;
- struct output_buf_s output;
- char * text, * tmp;
-
- output.buf = NULL;
- output.alloc = 0;
- output.size = 0;
- *out_buf = NULL;
- *out_style_atoms = NULL;
- *stylesize = 0;
-
- style_context_init(&ctx);
- ctx.height = height;
- hb_ssa_style_init(&style);
-
- // Skip past the SSA preamble
- text = (char*)input;
- for (ii = 0; ii < 8; ii++)
- {
- tmp = strchr(text, ',');
- if (tmp == NULL)
- break;
- text = tmp + 1;
- }
- in_pos = text - (char*)input;
-
- // Always allocate enough for empty string
- if (!check_realloc_output(&output, 1))
- {
- goto fail;
- }
- while (input[in_pos] != '\0')
- {
- text = hb_ssa_to_text((char*)input + in_pos, &consumed, &style);
- if (text == NULL)
- break;
-
- // count UTF8 characters, and get length of text
- len = 0;
- for (ii = 0; text[ii] != '\0'; ii++)
- {
- if ((text[ii] & 0xc0) == 0x80)
- {
- utf8_count++;
- hb_deep_log( 3, "mux: Counted %d UTF-8 chrs within subtitle",
- utf8_count);
- }
- len++;
- }
- if (!check_realloc_output(&output, out_pos + len + 1))
- {
- goto fail;
- }
- strcpy((char*)output.buf + out_pos, text);
- free(text);
- out_pos += len;
- in_pos += consumed;
- if (!update_style(&ctx, &style, out_pos - utf8_count))
- {
- goto fail;
- }
- }
- // Return to default style at end of line, flushes any pending
- // style changes
- hb_ssa_style_init(&style);
- if (!update_style(&ctx, &style, out_pos - utf8_count))
- {
- goto fail;
- }
-
- // null terminate output string
- output.buf[out_pos] = 0;
-
- if (ctx.style_atom_count > 0)
- {
- *stylesize = 10 + (ctx.style_atom_count * 12);
-
- memcpy(ctx.style_atoms.buf + 4, "styl", 4);
-
- ctx.style_atoms.buf[0] = 0;
- ctx.style_atoms.buf[1] = 0;
- ctx.style_atoms.buf[2] = (*stylesize >> 8) & 0xff;
- ctx.style_atoms.buf[3] = *stylesize & 0xff;
- ctx.style_atoms.buf[8] = (ctx.style_atom_count >> 8) & 0xff;
- ctx.style_atoms.buf[9] = ctx.style_atom_count & 0xff;
- *out_style_atoms = ctx.style_atoms.buf;
- }
- *out_buf = output.buf;
- return;
-
-fail:
- free(output.buf);
- free(ctx.style_atoms.buf);
-}
-