diff options
Diffstat (limited to 'libass/ass_render.c')
-rw-r--r-- | libass/ass_render.c | 194 |
1 files changed, 159 insertions, 35 deletions
diff --git a/libass/ass_render.c b/libass/ass_render.c index a755f23223..5eb28ccff0 100644 --- a/libass/ass_render.c +++ b/libass/ass_render.c @@ -43,8 +43,10 @@ #define MAX_GLYPHS 3000 #define MAX_LINES 300 -#define BE_RADIUS 1.5 #define BLUR_MAX_RADIUS 50.0 +#define MAX_BE 100 +#define ROUND(x) ((int) ((x) + .5)) +#define SUBPIXEL_MASK 56 // d6 bitmask for subpixel accuracy adjustment static int last_render_id = 0; @@ -82,7 +84,6 @@ struct ass_renderer_s { ass_settings_t settings; int render_id; ass_synth_priv_t* synth_priv; - ass_synth_priv_t* synth_priv_blur; ass_image_t* images_root; // rendering result is stored here ass_image_t* prev_images_root; @@ -116,7 +117,7 @@ typedef struct glyph_info_s { // int height; int be; // blur edges double blur; // gaussian blur - int shadow; + double shadow; double frx, fry, frz; // rotation bitmap_hash_key_t hash_key; @@ -165,7 +166,7 @@ typedef struct render_context_s { uint32_t fade; // alpha from \fad char be; // blur edges double blur; // gaussian blur - int shadow; + double shadow; int drawing_mode; // not implemented; when != 0 text is discarded, except for style override tags effect_t effect_type; @@ -224,9 +225,15 @@ static void ass_lazy_track_init(void) } else { double orig_aspect = (global_settings->aspect * frame_context.height * frame_context.orig_width) / frame_context.orig_height / frame_context.width; - if (!track->PlayResY) { + if (!track->PlayResY && track->PlayResX == 1280) { + track->PlayResY = 1024; + mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_PlayResYUndefinedSettingY, track->PlayResY); + } else if (!track->PlayResY) { track->PlayResY = track->PlayResX / orig_aspect + .5; mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_PlayResYUndefinedSettingY, track->PlayResY); + } else if (!track->PlayResX && track->PlayResY == 1024) { + track->PlayResX = 1280; + mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_PlayResXUndefinedSettingX, track->PlayResX); } else if (!track->PlayResX) { track->PlayResX = track->PlayResY * orig_aspect + .5; mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_PlayResXUndefinedSettingX, track->PlayResX); @@ -263,8 +270,7 @@ ass_renderer_t* ass_renderer_init(ass_library_t* library) goto ass_init_exit; } - priv->synth_priv = ass_synth_init(BE_RADIUS); - priv->synth_priv_blur = ass_synth_init(BLUR_MAX_RADIUS); + priv->synth_priv = ass_synth_init(BLUR_MAX_RADIUS); priv->library = library; priv->ftlibrary = ft; @@ -272,6 +278,7 @@ ass_renderer_t* ass_renderer_init(ass_library_t* library) ass_font_cache_init(); ass_bitmap_cache_init(); + ass_composite_cache_init(); ass_glyph_cache_init(); text_info.glyphs = calloc(MAX_GLYPHS, sizeof(glyph_info_t)); @@ -287,6 +294,7 @@ void ass_renderer_done(ass_renderer_t* priv) { ass_font_cache_done(); ass_bitmap_cache_done(); + ass_composite_cache_done(); ass_glyph_cache_done(); if (render_context.stroker) { FT_Stroker_Done(render_context.stroker); @@ -399,6 +407,93 @@ static ass_image_t** render_glyph(bitmap_t* bm, int dst_x, int dst_y, uint32_t c } /** + * \brief Calculate overlapping area of two consecutive bitmaps and in case they + * overlap, composite them together + * Mainly useful for translucent glyphs and especially borders, to avoid the + * luminance adding up where they overlap (which looks ugly) + */ +static void render_overlap(ass_image_t** last_tail, ass_image_t** tail, bitmap_hash_key_t *last_hash, bitmap_hash_key_t* hash) { + int left, top, bottom, right; + int old_left, old_top, w, h, cur_left, cur_top; + int x, y, opos, cpos; + char m; + composite_hash_key_t hk; + composite_hash_val_t *hv; + composite_hash_key_t *nhk; + int ax = (*last_tail)->dst_x; + int ay = (*last_tail)->dst_y; + int aw = (*last_tail)->w; + int ah = (*last_tail)->h; + int bx = (*tail)->dst_x; + int by = (*tail)->dst_y; + int bw = (*tail)->w; + int bh = (*tail)->h; + unsigned char* a; + unsigned char* b; + + if ((*last_tail)->bitmap == (*tail)->bitmap) + return; + + // Calculate overlap coordinates + left = (ax > bx) ? ax : bx; + top = (ay > by) ? ay : by; + right = ((ax+aw) < (bx+bw)) ? (ax+aw) : (bx+bw); + bottom = ((ay+ah) < (by+bh)) ? (ay+ah) : (by+bh); + if ((right <= left) || (bottom <= top)) + return; + old_left = left-(ax); + old_top = top-(ay); + w = right-left; + h = bottom-top; + cur_left = left-(bx); + cur_top = top-(by); + + // Query cache + memcpy(&hk.a, last_hash, sizeof(*last_hash)); + memcpy(&hk.b, hash, sizeof(*hash)); + hk.aw = aw; + hk.ah = ah; + hk.bw = bw; + hk.bh = bh; + hk.ax = ax; + hk.ay = ay; + hk.bx = bx; + hk.by = by; + hv = cache_find_composite(&hk); + if (hv) { + (*last_tail)->bitmap = hv->a; + (*tail)->bitmap = hv->b; + return; + } + + // Allocate new bitmaps and copy over data + a = (*last_tail)->bitmap; + b = (*tail)->bitmap; + (*last_tail)->bitmap = malloc(aw*ah); + (*tail)->bitmap = malloc(bw*bh); + memcpy((*last_tail)->bitmap, a, aw*ah); + memcpy((*tail)->bitmap, b, bw*bh); + + // Composite overlapping area + for (y=0; y<h; y++) + for (x=0; x<w; x++) { + opos = (old_top+y)*(aw) + (old_left+x); + cpos = (cur_top+y)*(bw) + (cur_left+x); + m = (a[opos] > b[cpos]) ? a[opos] : b[cpos]; + (*last_tail)->bitmap[opos] = 0; + (*tail)->bitmap[cpos] = m; + } + + // Insert bitmaps into the cache + nhk = calloc(1, sizeof(*nhk)); + memcpy(nhk, &hk, sizeof(*nhk)); + hv = calloc(1, sizeof(*hv)); + hv->a = (*last_tail)->bitmap; + hv->b = (*tail)->bitmap; + cache_add_composite(nhk, hv); +} + +/** * \brief Convert text_info_t struct to ass_image_t list * Splits glyphs in halves when needed (for \kf karaoke). */ @@ -409,19 +504,28 @@ static ass_image_t* render_text(text_info_t* text_info, int dst_x, int dst_y) bitmap_t* bm; ass_image_t* head; ass_image_t** tail = &head; + ass_image_t** last_tail = 0; + ass_image_t** here_tail = 0; + bitmap_hash_key_t* last_hash = 0; for (i = 0; i < text_info->length; ++i) { glyph_info_t* info = text_info->glyphs + i; if ((info->symbol == 0) || (info->symbol == '\n') || !info->bm_s || (info->shadow == 0)) continue; - pen_x = dst_x + info->pos.x + info->shadow; - pen_y = dst_y + info->pos.y + info->shadow; + pen_x = dst_x + info->pos.x + ROUND(info->shadow * frame_context.border_scale); + pen_y = dst_y + info->pos.y + ROUND(info->shadow * frame_context.border_scale); bm = info->bm_s; + here_tail = tail; tail = render_glyph(bm, pen_x, pen_y, info->c[3], 0, 1000000, tail); + if (last_tail && tail != here_tail && ((info->c[3] & 0xff) > 0)) + render_overlap(last_tail, here_tail, last_hash, &info->hash_key); + last_tail = here_tail; + last_hash = &info->hash_key; } + last_tail = 0; for (i = 0; i < text_info->length; ++i) { glyph_info_t* info = text_info->glyphs + i; if ((info->symbol == 0) || (info->symbol == '\n') || !info->bm_o) @@ -433,8 +537,14 @@ static ass_image_t* render_text(text_info_t* text_info, int dst_x, int dst_y) if ((info->effect_type == EF_KARAOKE_KO) && (info->effect_timing <= info->bbox.xMax)) { // do nothing - } else + } else { + here_tail = tail; tail = render_glyph(bm, pen_x, pen_y, info->c[2], 0, 1000000, tail); + if (last_tail && tail != here_tail && ((info->c[2] & 0xff) > 0)) + render_overlap(last_tail, here_tail, last_hash, &info->hash_key); + last_tail = here_tail; + last_hash = &info->hash_key; + } } for (i = 0; i < text_info->length; ++i) { glyph_info_t* info = text_info->glyphs + i; @@ -467,13 +577,22 @@ static int x2scr(double x) { return x*frame_context.orig_width_nocrop / frame_context.track->PlayResX + FFMAX(global_settings->left_margin, 0); } +static double x2scr_pos(double x) { + return x*frame_context.orig_width / frame_context.track->PlayResX + + global_settings->left_margin; +} /** * \brief Mapping between script and screen coordinates */ -static int y2scr(double y) { +static double y2scr(double y) { return y * frame_context.orig_height_nocrop / frame_context.track->PlayResY + FFMAX(global_settings->top_margin, 0); } +static double y2scr_pos(double y) { + return y * frame_context.orig_height / frame_context.track->PlayResY + + global_settings->top_margin; +} + // the same for toptitles static int y2scr_top(double y) { if (global_settings->use_margins) @@ -776,29 +895,29 @@ static char* parse_tag(char* p, double pwr) { val = -1.; // reset to default change_border(val); } else if (mystrcmp(&p, "move")) { - int x1, x2, y1, y2; + double x1, x2, y1, y2; long long t1, t2, delta_t, t; double x, y; double k; skip('('); - mystrtoi(&p, &x1); + mystrtod(&p, &x1); skip(','); - mystrtoi(&p, &y1); + mystrtod(&p, &y1); skip(','); - mystrtoi(&p, &x2); + mystrtod(&p, &x2); skip(','); - mystrtoi(&p, &y2); + mystrtod(&p, &y2); if (*p == ',') { skip(','); mystrtoll(&p, &t1); skip(','); mystrtoll(&p, &t2); - mp_msg(MSGT_ASS, MSGL_DBG2, "movement6: (%d, %d) -> (%d, %d), (%" PRId64 " .. %" PRId64 ")\n", + mp_msg(MSGT_ASS, MSGL_DBG2, "movement6: (%f, %f) -> (%f, %f), (%" PRId64 " .. %" PRId64 ")\n", x1, y1, x2, y2, (int64_t)t1, (int64_t)t2); } else { t1 = 0; t2 = render_context.event->Duration; - mp_msg(MSGT_ASS, MSGL_DBG2, "movement: (%d, %d) -> (%d, %d)\n", x1, y1, x2, y2); + mp_msg(MSGT_ASS, MSGL_DBG2, "movement: (%f, %f) -> (%f, %f)\n", x1, y1, x2, y2); } skip(')'); delta_t = t2 - t1; @@ -884,13 +1003,13 @@ static char* parse_tag(char* p, double pwr) { else render_context.alignment = render_context.style->Alignment; } else if (mystrcmp(&p, "pos")) { - int v1, v2; + double v1, v2; skip('('); - mystrtoi(&p, &v1); + mystrtod(&p, &v1); skip(','); - mystrtoi(&p, &v2); + mystrtod(&p, &v2); skip(')'); - mp_msg(MSGT_ASS, MSGL_DBG2, "pos(%d, %d)\n", v1, v2); + mp_msg(MSGT_ASS, MSGL_DBG2, "pos(%f, %f)\n", v1, v2); if (render_context.evt_type == EVENT_POSITIONED) { mp_msg(MSGT_ASS, MSGL_V, "Subtitle has a new \\pos " "after \\move or \\pos, ignoring\n"); @@ -1043,9 +1162,9 @@ static char* parse_tag(char* p, double pwr) { } else if (mystrcmp(&p, "be")) { int val; if (mystrtoi(&p, &val)) { - // Clamp to 10, since high values need excessive CPU + // Clamp to a safe upper limit, since high values need excessive CPU val = (val < 0) ? 0 : val; - val = (val > 10) ? 10 : val; + val = (val > MAX_BE) ? MAX_BE : val; render_context.be = val; } else render_context.be = 0; @@ -1386,10 +1505,9 @@ static void get_bitmap_glyph(glyph_info_t* info) // render glyph error = glyph_to_bitmap(ass_renderer->synth_priv, - ass_renderer->synth_priv_blur, info->glyph, info->outline_glyph, &info->bm, &info->bm_o, - &info->bm_s, info->be, info->blur); + &info->bm_s, info->be, info->blur * frame_context.border_scale); if (error) info->symbol = 0; @@ -1851,8 +1969,13 @@ static int ass_render_event(ass_event_t* event, event_images_t* event_images) pen.y += delta.y * render_context.scale_y; } - shift.x = pen.x & 63; - shift.y = pen.y & 63; + shift.x = pen.x & SUBPIXEL_MASK; + shift.y = pen.y & SUBPIXEL_MASK; + + if (render_context.evt_type == EVENT_POSITIONED) { + shift.x += double_to_d6(x2scr_pos(render_context.pos_x)) & SUBPIXEL_MASK; + shift.y -= double_to_d6(y2scr_pos(render_context.pos_y)) & SUBPIXEL_MASK; + } ass_font_set_transform(render_context.font, render_context.scale_x * frame_context.font_scale_x, @@ -2017,8 +2140,8 @@ static int ass_render_event(ass_event_t* event, event_images_t* event_images) int base_y = 0; mp_msg(MSGT_ASS, MSGL_DBG2, "positioned event at %f, %f\n", render_context.pos_x, render_context.pos_y); get_base_point(bbox, alignment, &base_x, &base_y); - device_x = x2scr(render_context.pos_x) - base_x; - device_y = y2scr(render_context.pos_y) - base_y; + device_x = x2scr_pos(render_context.pos_x) - base_x; + device_y = y2scr_pos(render_context.pos_y) - base_y; } // fix clip coordinates (they depend on alignment) @@ -2103,6 +2226,7 @@ static void ass_reconfigure(ass_renderer_t* priv) priv->render_id = ++last_render_id; ass_glyph_cache_reset(); ass_bitmap_cache_reset(); + ass_composite_cache_reset(); ass_free_images(priv->prev_images_root); priv->prev_images_root = 0; } @@ -2225,12 +2349,12 @@ static int ass_start_frame(ass_renderer_t *priv, ass_track_t* track, long long n frame_context.font_scale = global_settings->font_size_coeff * frame_context.orig_height / frame_context.track->PlayResY; - frame_context.border_scale = ((double)frame_context.orig_height) / frame_context.track->PlayResY; - - if (frame_context.orig_width * track->PlayResY == frame_context.orig_height * track->PlayResX) - frame_context.font_scale_x = 1.; + if (frame_context.track->ScaledBorderAndShadow) + frame_context.border_scale = ((double)frame_context.orig_height) / frame_context.track->PlayResY; else - frame_context.font_scale_x = ((double)(frame_context.orig_width * track->PlayResY)) / (frame_context.orig_height * track->PlayResX); + frame_context.border_scale = 1.; + + frame_context.font_scale_x = 1.; priv->prev_images_root = priv->images_root; priv->images_root = 0; |