2009-07-25 04:24:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
|
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
2009-07-25 04:24:39 +00:00
|
|
|
*
|
2017-04-20 07:18:28 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2009-07-25 04:24:39 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2009-07-25 04:24:39 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-04-20 07:18:28 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2009-07-25 04:24:39 +00:00
|
|
|
*
|
2017-04-20 07:18:28 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2009-07-25 04:24:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
2011-01-19 16:00:22 +00:00
|
|
|
#include <stdbool.h>
|
2016-03-08 19:49:33 +00:00
|
|
|
#include <assert.h>
|
2009-07-25 04:24:39 +00:00
|
|
|
|
|
|
|
#include <ass/ass.h>
|
|
|
|
#include <ass/ass_types.h>
|
|
|
|
|
2014-06-14 17:17:31 +00:00
|
|
|
#include "common/common.h"
|
2013-12-21 18:06:37 +00:00
|
|
|
#include "common/global.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/path.h"
|
2009-07-25 04:24:39 +00:00
|
|
|
#include "ass_mp.h"
|
2016-07-03 17:11:33 +00:00
|
|
|
#include "img_convert.h"
|
2013-11-24 11:58:06 +00:00
|
|
|
#include "osd.h"
|
2010-03-09 20:35:53 +00:00
|
|
|
#include "stream/stream.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/options.h"
|
2016-06-30 19:38:50 +00:00
|
|
|
#include "video/out/bitmap_packer.h"
|
|
|
|
#include "video/mp_image.h"
|
2009-07-25 04:24:39 +00:00
|
|
|
|
2013-06-03 20:14:56 +00:00
|
|
|
// res_y should be track->PlayResY
|
|
|
|
// It determines scaling of font sizes and more.
|
2013-12-10 18:58:57 +00:00
|
|
|
void mp_ass_set_style(ASS_Style *style, double res_y,
|
2013-09-26 15:49:54 +00:00
|
|
|
const struct osd_style_opts *opts)
|
2012-11-17 19:56:45 +00:00
|
|
|
{
|
2015-11-29 12:50:51 +00:00
|
|
|
if (!style)
|
|
|
|
return;
|
|
|
|
|
2012-11-17 19:56:45 +00:00
|
|
|
if (opts->font) {
|
2013-12-15 16:38:48 +00:00
|
|
|
if (!style->FontName || strcmp(style->FontName, opts->font) != 0) {
|
|
|
|
free(style->FontName);
|
|
|
|
style->FontName = strdup(opts->font);
|
|
|
|
}
|
2012-11-17 19:56:45 +00:00
|
|
|
}
|
|
|
|
|
2013-06-03 20:14:56 +00:00
|
|
|
// libass_font_size = FontSize * (window_height / res_y)
|
|
|
|
// scale translates parameters from PlayResY=720 to res_y
|
|
|
|
double scale = res_y / 720.0;
|
2012-11-17 19:56:45 +00:00
|
|
|
|
|
|
|
style->FontSize = opts->font_size * scale;
|
|
|
|
style->PrimaryColour = MP_ASS_COLOR(opts->color);
|
|
|
|
style->SecondaryColour = style->PrimaryColour;
|
2014-06-01 21:53:24 +00:00
|
|
|
style->OutlineColour = MP_ASS_COLOR(opts->border_color);
|
|
|
|
if (opts->back_color.a) {
|
|
|
|
style->BackColour = MP_ASS_COLOR(opts->back_color);
|
|
|
|
style->BorderStyle = 4; // opaque box
|
|
|
|
} else {
|
|
|
|
style->BackColour = MP_ASS_COLOR(opts->shadow_color);
|
|
|
|
style->BorderStyle = 1; // outline
|
|
|
|
}
|
2012-11-17 19:56:45 +00:00
|
|
|
style->Outline = opts->border_size * scale;
|
|
|
|
style->Shadow = opts->shadow_offset * scale;
|
|
|
|
style->Spacing = opts->spacing * scale;
|
|
|
|
style->MarginL = opts->margin_x * scale;
|
|
|
|
style->MarginR = style->MarginL;
|
|
|
|
style->MarginV = opts->margin_y * scale;
|
|
|
|
style->ScaleX = 1.;
|
|
|
|
style->ScaleY = 1.;
|
2015-02-16 19:04:02 +00:00
|
|
|
style->Alignment = 1 + (opts->align_x + 1) + (opts->align_y + 2) % 3 * 4;
|
2017-02-01 12:19:09 +00:00
|
|
|
#ifdef ASS_JUSTIFY_LEFT
|
|
|
|
style->Justify = opts->justify;
|
|
|
|
#endif
|
2013-04-13 16:53:03 +00:00
|
|
|
style->Blur = opts->blur;
|
2015-04-01 20:58:12 +00:00
|
|
|
style->Bold = opts->bold;
|
2016-04-08 08:43:34 +00:00
|
|
|
style->Italic = opts->italic;
|
2012-11-17 19:56:45 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 18:06:37 +00:00
|
|
|
void mp_ass_configure_fonts(ASS_Renderer *priv, struct osd_style_opts *opts,
|
|
|
|
struct mpv_global *global, struct mp_log *log)
|
2009-07-25 04:24:39 +00:00
|
|
|
{
|
2013-12-21 19:45:19 +00:00
|
|
|
void *tmp = talloc_new(NULL);
|
2014-06-18 23:55:40 +00:00
|
|
|
char *default_font = mp_find_config_file(tmp, global, "subfont.ttf");
|
2013-12-21 19:45:19 +00:00
|
|
|
char *config = mp_find_config_file(tmp, global, "fonts.conf");
|
2012-12-09 11:45:05 +00:00
|
|
|
|
2013-12-21 19:45:19 +00:00
|
|
|
if (default_font && !mp_path_exists(default_font))
|
2012-12-09 11:45:05 +00:00
|
|
|
default_font = NULL;
|
|
|
|
|
2013-12-21 19:45:19 +00:00
|
|
|
mp_verbose(log, "Setting up fonts...\n");
|
2012-12-09 11:45:05 +00:00
|
|
|
ass_set_fonts(priv, default_font, opts->font, 1, config, 1);
|
2013-12-21 19:45:19 +00:00
|
|
|
mp_verbose(log, "Done.\n");
|
2009-07-25 04:24:39 +00:00
|
|
|
|
2013-12-21 19:45:19 +00:00
|
|
|
talloc_free(tmp);
|
2009-07-25 04:24:39 +00:00
|
|
|
}
|
|
|
|
|
2014-06-10 21:56:05 +00:00
|
|
|
static const int map_ass_level[] = {
|
2012-07-29 21:57:46 +00:00
|
|
|
MSGL_ERR, // 0 "FATAL errors"
|
|
|
|
MSGL_WARN,
|
|
|
|
MSGL_INFO,
|
|
|
|
MSGL_V,
|
|
|
|
MSGL_V,
|
2017-09-28 09:53:57 +00:00
|
|
|
MSGL_DEBUG, // 5 application recommended level
|
|
|
|
MSGL_TRACE,
|
2013-12-21 20:41:18 +00:00
|
|
|
MSGL_TRACE, // 7 "verbose DEBUG"
|
2012-07-29 21:57:46 +00:00
|
|
|
};
|
|
|
|
|
2009-07-25 04:24:39 +00:00
|
|
|
static void message_callback(int level, const char *format, va_list va, void *ctx)
|
|
|
|
{
|
2013-12-21 18:06:37 +00:00
|
|
|
struct mp_log *log = ctx;
|
|
|
|
if (!log)
|
|
|
|
return;
|
2012-07-29 21:57:46 +00:00
|
|
|
level = map_ass_level[level];
|
2013-12-21 20:49:13 +00:00
|
|
|
mp_msg_va(log, level, format, va);
|
2009-07-25 04:24:39 +00:00
|
|
|
// libass messages lack trailing \n
|
2013-12-21 20:49:13 +00:00
|
|
|
mp_msg(log, level, "\n");
|
2009-07-25 04:24:39 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 18:06:37 +00:00
|
|
|
ASS_Library *mp_ass_init(struct mpv_global *global, struct mp_log *log)
|
2009-07-25 04:24:39 +00:00
|
|
|
{
|
2014-06-18 23:55:40 +00:00
|
|
|
char *path = mp_find_config_file(NULL, global, "fonts");
|
2013-12-15 14:04:49 +00:00
|
|
|
ASS_Library *priv = ass_library_init();
|
|
|
|
if (!priv)
|
|
|
|
abort();
|
2013-12-21 18:06:37 +00:00
|
|
|
ass_set_message_cb(priv, message_callback, log);
|
2013-02-08 22:50:21 +00:00
|
|
|
if (path)
|
|
|
|
ass_set_fonts_dir(priv, path);
|
2012-12-09 14:05:21 +00:00
|
|
|
talloc_free(path);
|
2009-07-25 04:24:39 +00:00
|
|
|
return priv;
|
|
|
|
}
|
2016-01-07 21:31:09 +00:00
|
|
|
|
|
|
|
void mp_ass_flush_old_events(ASS_Track *track, long long ts)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
for (; n < track->n_events; n++) {
|
|
|
|
if ((track->events[n].Start + track->events[n].Duration) >= ts)
|
|
|
|
break;
|
|
|
|
ass_free_event(track, n);
|
|
|
|
track->n_events--;
|
|
|
|
}
|
|
|
|
for (int i = 0; n > 0 && i < track->n_events; i++) {
|
|
|
|
track->events[i] = track->events[i+n];
|
|
|
|
}
|
|
|
|
}
|
2016-06-30 19:38:50 +00:00
|
|
|
|
2016-07-03 17:11:33 +00:00
|
|
|
static void draw_ass_rgba(unsigned char *src, int src_w, int src_h,
|
|
|
|
int src_stride, unsigned char *dst, size_t dst_stride,
|
|
|
|
int dst_x, int dst_y, uint32_t color)
|
|
|
|
{
|
|
|
|
const unsigned int r = (color >> 24) & 0xff;
|
|
|
|
const unsigned int g = (color >> 16) & 0xff;
|
|
|
|
const unsigned int b = (color >> 8) & 0xff;
|
|
|
|
const unsigned int a = 0xff - (color & 0xff);
|
|
|
|
|
|
|
|
dst += dst_y * dst_stride + dst_x * 4;
|
|
|
|
|
|
|
|
for (int y = 0; y < src_h; y++, dst += dst_stride, src += src_stride) {
|
|
|
|
uint32_t *dstrow = (uint32_t *) dst;
|
|
|
|
for (int x = 0; x < src_w; x++) {
|
|
|
|
const unsigned int v = src[x];
|
|
|
|
int rr = (r * a * v);
|
|
|
|
int gg = (g * a * v);
|
|
|
|
int bb = (b * a * v);
|
|
|
|
int aa = a * v;
|
|
|
|
uint32_t dstpix = dstrow[x];
|
|
|
|
unsigned int dstb = dstpix & 0xFF;
|
|
|
|
unsigned int dstg = (dstpix >> 8) & 0xFF;
|
|
|
|
unsigned int dstr = (dstpix >> 16) & 0xFF;
|
|
|
|
unsigned int dsta = (dstpix >> 24) & 0xFF;
|
|
|
|
dstb = (bb + dstb * (255 * 255 - aa)) / (255 * 255);
|
|
|
|
dstg = (gg + dstg * (255 * 255 - aa)) / (255 * 255);
|
|
|
|
dstr = (rr + dstr * (255 * 255 - aa)) / (255 * 255);
|
|
|
|
dsta = (aa * 255 + dsta * (255 * 255 - aa)) / (255 * 255);
|
|
|
|
dstrow[x] = dstb | (dstg << 8) | (dstr << 16) | (dsta << 24);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-30 19:38:50 +00:00
|
|
|
struct mp_ass_packer {
|
|
|
|
struct sub_bitmap *cached_parts; // only for the array memory
|
|
|
|
struct mp_image *cached_img;
|
|
|
|
struct sub_bitmaps cached_subs;
|
|
|
|
bool cached_subs_valid;
|
2016-07-03 17:11:33 +00:00
|
|
|
struct sub_bitmap rgba_imgs[MP_SUB_BB_LIST_MAX];
|
2016-06-30 19:38:50 +00:00
|
|
|
struct bitmap_packer *packer;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Free with talloc_free().
|
|
|
|
struct mp_ass_packer *mp_ass_packer_alloc(void *ta_parent)
|
|
|
|
{
|
|
|
|
struct mp_ass_packer *p = talloc_zero(ta_parent, struct mp_ass_packer);
|
|
|
|
p->packer = talloc_zero(p, struct bitmap_packer);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-07-03 17:05:19 +00:00
|
|
|
static bool pack(struct mp_ass_packer *p, struct sub_bitmaps *res, int imgfmt)
|
|
|
|
{
|
|
|
|
packer_set_size(p->packer, res->num_parts);
|
|
|
|
|
|
|
|
for (int n = 0; n < res->num_parts; n++)
|
|
|
|
p->packer->in[n] = (struct pos){res->parts[n].w, res->parts[n].h};
|
|
|
|
|
|
|
|
if (p->packer->count == 0 || packer_pack(p->packer) < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
struct pos bb[2];
|
|
|
|
packer_get_bb(p->packer, bb);
|
|
|
|
|
|
|
|
res->packed_w = bb[1].x;
|
|
|
|
res->packed_h = bb[1].y;
|
|
|
|
|
|
|
|
if (!p->cached_img || p->cached_img->w < res->packed_w ||
|
2017-04-17 17:34:27 +00:00
|
|
|
p->cached_img->h < res->packed_h ||
|
|
|
|
p->cached_img->imgfmt != imgfmt)
|
2016-07-03 17:05:19 +00:00
|
|
|
{
|
|
|
|
talloc_free(p->cached_img);
|
|
|
|
p->cached_img = mp_image_alloc(imgfmt, p->packer->w, p->packer->h);
|
|
|
|
if (!p->cached_img)
|
|
|
|
return false;
|
|
|
|
talloc_steal(p, p->cached_img);
|
|
|
|
}
|
|
|
|
|
|
|
|
res->packed = p->cached_img;
|
|
|
|
|
|
|
|
for (int n = 0; n < res->num_parts; n++) {
|
|
|
|
struct sub_bitmap *b = &res->parts[n];
|
|
|
|
struct pos pos = p->packer->result[n];
|
|
|
|
|
|
|
|
b->src_x = pos.x;
|
|
|
|
b->src_y = pos.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool pack_libass(struct mp_ass_packer *p, struct sub_bitmaps *res)
|
|
|
|
{
|
|
|
|
if (!pack(p, res, IMGFMT_Y8))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (int n = 0; n < res->num_parts; n++) {
|
|
|
|
struct sub_bitmap *b = &res->parts[n];
|
|
|
|
|
|
|
|
int stride = res->packed->stride[0];
|
|
|
|
void *pdata =
|
|
|
|
(uint8_t *)res->packed->planes[0] + b->src_y * stride + b->src_x;
|
|
|
|
memcpy_pic(pdata, b->bitmap, b->w, b->h, stride, b->stride);
|
|
|
|
|
|
|
|
b->bitmap = pdata;
|
|
|
|
b->stride = stride;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-03 17:11:33 +00:00
|
|
|
static bool pack_rgba(struct mp_ass_packer *p, struct sub_bitmaps *res)
|
|
|
|
{
|
|
|
|
struct mp_rect bb_list[MP_SUB_BB_LIST_MAX];
|
|
|
|
int num_bb = mp_get_sub_bb_list(res, bb_list, MP_SUB_BB_LIST_MAX);
|
|
|
|
|
|
|
|
struct sub_bitmaps imgs = {
|
|
|
|
.change_id = res->change_id,
|
|
|
|
.format = SUBBITMAP_RGBA,
|
|
|
|
.parts = p->rgba_imgs,
|
|
|
|
.num_parts = num_bb,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int n = 0; n < imgs.num_parts; n++) {
|
|
|
|
imgs.parts[n].w = bb_list[n].x1 - bb_list[n].x0;
|
|
|
|
imgs.parts[n].h = bb_list[n].y1 - bb_list[n].y0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pack(p, &imgs, IMGFMT_BGRA))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (int n = 0; n < num_bb; n++) {
|
|
|
|
struct mp_rect bb = bb_list[n];
|
|
|
|
struct sub_bitmap *b = &imgs.parts[n];
|
|
|
|
|
|
|
|
b->x = bb.x0;
|
|
|
|
b->y = bb.y0;
|
|
|
|
b->w = b->dw = bb.x1 - bb.x0;
|
|
|
|
b->h = b->dh = bb.y1 - bb.y0;
|
|
|
|
b->stride = imgs.packed->stride[0];
|
|
|
|
b->bitmap = (uint8_t *)imgs.packed->planes[0] +
|
|
|
|
b->stride * b->src_y + b->src_x * 4;
|
|
|
|
|
|
|
|
memset_pic(b->bitmap, 0, b->w * 4, b->h, b->stride);
|
|
|
|
|
|
|
|
for (int i = 0; i < res->num_parts; i++) {
|
|
|
|
struct sub_bitmap *s = &res->parts[i];
|
|
|
|
|
|
|
|
// Assume mp_get_sub_bb_list() never splits sub bitmaps
|
|
|
|
// So we don't clip/adjust the size of the sub bitmap
|
|
|
|
if (s->x > bb.x1 || s->x + s->w < bb.x0 ||
|
|
|
|
s->y > bb.y1 || s->y + s->h < bb.y0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
draw_ass_rgba(s->bitmap, s->w, s->h, s->stride,
|
|
|
|
b->bitmap, b->stride,
|
|
|
|
s->x - bb.x0, s->y - bb.y0,
|
|
|
|
s->libass.color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*res = imgs;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-30 19:38:50 +00:00
|
|
|
// Pack the contents of image_lists[0] to image_lists[num_image_lists-1] into
|
|
|
|
// a single image, and make *out point to it. *out is completely overwritten.
|
|
|
|
// If libass reported any change, image_lists_changed must be set (it then
|
|
|
|
// repacks all images). preferred_osd_format can be set to a desired
|
|
|
|
// sub_bitmap_format. Currently, only SUBBITMAP_LIBASS is supported.
|
|
|
|
void mp_ass_packer_pack(struct mp_ass_packer *p, ASS_Image **image_lists,
|
|
|
|
int num_image_lists, bool image_lists_changed,
|
|
|
|
int preferred_osd_format, struct sub_bitmaps *out)
|
|
|
|
{
|
2016-07-03 17:11:33 +00:00
|
|
|
int format = preferred_osd_format == SUBBITMAP_RGBA ? SUBBITMAP_RGBA
|
|
|
|
: SUBBITMAP_LIBASS;
|
|
|
|
|
|
|
|
if (p->cached_subs_valid && !image_lists_changed &&
|
|
|
|
p->cached_subs.format == format)
|
|
|
|
{
|
2016-06-30 19:38:50 +00:00
|
|
|
*out = p->cached_subs;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out = (struct sub_bitmaps){.change_id = 1};
|
|
|
|
p->cached_subs_valid = false;
|
|
|
|
|
|
|
|
struct sub_bitmaps res = {
|
|
|
|
.change_id = image_lists_changed,
|
|
|
|
.format = SUBBITMAP_LIBASS,
|
|
|
|
.parts = p->cached_parts,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int n = 0; n < num_image_lists; n++) {
|
|
|
|
for (struct ass_image *img = image_lists[n]; img; img = img->next) {
|
|
|
|
if (img->w == 0 || img->h == 0)
|
|
|
|
continue;
|
|
|
|
MP_TARRAY_GROW(p, p->cached_parts, res.num_parts);
|
|
|
|
res.parts = p->cached_parts;
|
|
|
|
struct sub_bitmap *b = &res.parts[res.num_parts];
|
|
|
|
b->bitmap = img->bitmap;
|
|
|
|
b->stride = img->stride;
|
|
|
|
b->libass.color = img->color;
|
|
|
|
b->dw = b->w = img->w;
|
|
|
|
b->dh = b->h = img->h;
|
|
|
|
b->x = img->dst_x;
|
|
|
|
b->y = img->dst_y;
|
|
|
|
res.num_parts++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 17:11:33 +00:00
|
|
|
bool r = false;
|
|
|
|
if (format == SUBBITMAP_RGBA) {
|
|
|
|
r = pack_rgba(p, &res);
|
|
|
|
} else {
|
|
|
|
r = pack_libass(p, &res);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!r)
|
2016-06-30 19:38:50 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
*out = res;
|
|
|
|
p->cached_subs = res;
|
|
|
|
p->cached_subs.change_id = 0;
|
|
|
|
p->cached_subs_valid = true;
|
|
|
|
}
|