2009-07-25 04:24:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
|
|
|
|
*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with libass; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
2011-01-19 16:00:22 +00:00
|
|
|
#include <stdbool.h>
|
2009-07-25 04:24:39 +00:00
|
|
|
|
|
|
|
#include <ass/ass.h>
|
|
|
|
#include <ass/ass_types.h>
|
|
|
|
|
2010-03-09 20:35:53 +00:00
|
|
|
#include <libavutil/common.h>
|
|
|
|
|
2010-10-30 16:39:40 +00:00
|
|
|
#include "config.h"
|
2009-07-25 04:24:39 +00:00
|
|
|
#include "mp_msg.h"
|
2010-04-26 14:42:20 +00:00
|
|
|
#include "path.h"
|
2009-07-25 04:24:39 +00:00
|
|
|
#include "ass_mp.h"
|
|
|
|
#include "subreader.h"
|
osd: use libass for OSD rendering
The OSD will now be rendered with libass. The old rendering code, which
used freetype/fontconfig and did text layout manually, is disabled. To
re-enable the old code, use the --disable-libass-osd configure switch.
Some switches do nothing with the new code enabled, such as -subalign,
-sub-bg-alpha, -sub-bg-color, and many more. (The reason is mostly that
the code for rendering unstyled subtitles with libass doesn't make any
attempts to support them. Some of them could be supported in theory.)
Teletext rendering is not implemented in the new OSD rendering code. I
don't have any teletext sources for testing, and since teletext is
being phased out world-wide, the need for this is questionable.
Note that rendering is extremely inefficient, mostly because the libass
output is blended with the extremely strange mplayer OSD format. This
could be improved at a later point.
Remove most OSD rendering from vo_aa.c, because that was extremely
hacky, can't be made work with osd_libass, and didn't work anyway in
my tests.
Internally, some cleanup is done. Subtitle and OSD related variable
declarations were literally all over the place. Move them to sub.h and
sub.c, which were hoarding most of these declarations already. Make the
player core in mplayer.c free of concerns like bitmap font loading.
The old OSD rendering code has been moved to osd_ft.c. The font_load.c
and font_load_ft.c are only needed and compiled if the old OSD
rendering code is configured.
2012-03-22 05:26:37 +00:00
|
|
|
#include "sub/sub.h"
|
2010-03-09 20:35:53 +00:00
|
|
|
#include "stream/stream.h"
|
2011-09-03 10:47:56 +00:00
|
|
|
#include "options.h"
|
2009-07-25 04:24:39 +00:00
|
|
|
|
2011-09-03 10:47:56 +00:00
|
|
|
ASS_Track *mp_ass_default_track(ASS_Library *library, struct MPOpts *opts)
|
2009-07-25 04:24:39 +00:00
|
|
|
{
|
2009-08-06 20:58:31 +00:00
|
|
|
ASS_Track *track = ass_new_track(library);
|
2009-07-25 04:24:39 +00:00
|
|
|
|
|
|
|
track->track_type = TRACK_TYPE_ASS;
|
|
|
|
track->Timer = 100.;
|
|
|
|
track->PlayResY = 288;
|
|
|
|
track->WrapStyle = 0;
|
|
|
|
|
2011-09-03 10:47:56 +00:00
|
|
|
if (opts->ass_styles_file)
|
|
|
|
ass_read_styles(track, opts->ass_styles_file, sub_cp);
|
2009-07-25 04:24:39 +00:00
|
|
|
|
|
|
|
if (track->n_styles == 0) {
|
2011-01-26 02:12:25 +00:00
|
|
|
track->Kerning = true;
|
|
|
|
int sid = ass_alloc_style(track);
|
2011-05-30 18:57:58 +00:00
|
|
|
track->default_style = sid;
|
2011-01-26 02:12:25 +00:00
|
|
|
ASS_Style *style = track->styles + sid;
|
2009-07-25 04:24:39 +00:00
|
|
|
style->Name = strdup("Default");
|
2012-07-28 20:00:31 +00:00
|
|
|
style->FontName = sub_font_name ? strdup(sub_font_name)
|
|
|
|
: font_name ? strdup(font_name) : strdup("Sans");
|
2009-07-25 04:24:39 +00:00
|
|
|
style->treat_fontname_as_pattern = 1;
|
|
|
|
|
2011-01-26 02:12:25 +00:00
|
|
|
double fs = track->PlayResY * text_font_scale_factor / 100.;
|
|
|
|
/* The font size is always proportional to video height only;
|
|
|
|
* real -subfont-autoscale behavior is not implemented.
|
|
|
|
* Apply a correction that corresponds to about 4:3 aspect ratio
|
|
|
|
* video to get a size somewhat closer to what non-libass rendering
|
|
|
|
* would produce with the same text_font_scale_factor
|
|
|
|
* and subtitle_autoscale.
|
|
|
|
*/
|
2009-07-25 04:24:39 +00:00
|
|
|
if (subtitle_autoscale == 2)
|
|
|
|
fs *= 1.3;
|
|
|
|
else if (subtitle_autoscale == 3)
|
2011-01-26 02:12:25 +00:00
|
|
|
fs *= 1.7;
|
2009-07-25 04:24:39 +00:00
|
|
|
|
2011-01-26 02:12:25 +00:00
|
|
|
uint32_t c1 = 0xFFFFFF00;
|
|
|
|
uint32_t c2 = 0x00000000;
|
2011-09-03 10:47:56 +00:00
|
|
|
if (opts->ass_color)
|
|
|
|
c1 = strtoll(opts->ass_color, NULL, 16);
|
|
|
|
if (opts->ass_border_color)
|
|
|
|
c2 = strtoll(opts->ass_border_color, NULL, 16);
|
2009-07-25 04:24:39 +00:00
|
|
|
|
2011-01-26 02:12:25 +00:00
|
|
|
style->FontSize = fs;
|
2009-07-25 04:24:39 +00:00
|
|
|
style->PrimaryColour = c1;
|
|
|
|
style->SecondaryColour = c1;
|
|
|
|
style->OutlineColour = c2;
|
|
|
|
style->BackColour = 0x00000000;
|
|
|
|
style->BorderStyle = 1;
|
|
|
|
style->Alignment = 2;
|
2011-01-26 02:12:25 +00:00
|
|
|
style->Outline = fs / 16;
|
2009-07-25 04:24:39 +00:00
|
|
|
style->MarginL = 10;
|
|
|
|
style->MarginR = 10;
|
|
|
|
style->MarginV = 5;
|
|
|
|
style->ScaleX = 1.;
|
|
|
|
style->ScaleY = 1.;
|
|
|
|
}
|
|
|
|
|
|
|
|
ass_process_force_style(track);
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
2009-08-06 20:58:31 +00:00
|
|
|
static int check_duplicate_plaintext_event(ASS_Track *track)
|
2009-07-25 04:24:39 +00:00
|
|
|
{
|
|
|
|
int i;
|
2009-08-06 20:58:31 +00:00
|
|
|
ASS_Event *evt = track->events + track->n_events - 1;
|
2009-07-25 04:24:39 +00:00
|
|
|
|
|
|
|
for (i = 0; i < track->n_events - 1; ++i) // ignoring last event, it is the one we are comparing with
|
|
|
|
if (track->events[i].Start == evt->Start &&
|
|
|
|
track->events[i].Duration == evt->Duration &&
|
|
|
|
strcmp(track->events[i].Text, evt->Text) == 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-08-06 20:58:31 +00:00
|
|
|
* \brief Convert subtitle to ASS_Events for the given track
|
|
|
|
* \param track track
|
2009-07-25 04:24:39 +00:00
|
|
|
* \param sub subtitle to convert
|
|
|
|
* \return event id
|
|
|
|
* note: assumes that subtitle is _not_ fps-based; caller must manually correct
|
|
|
|
* Start and Duration in other case.
|
|
|
|
**/
|
2011-01-18 13:33:36 +00:00
|
|
|
static int ass_process_subtitle(ASS_Track *track, subtitle *sub)
|
2009-07-25 04:24:39 +00:00
|
|
|
{
|
|
|
|
int eid;
|
2009-08-06 20:58:31 +00:00
|
|
|
ASS_Event *event;
|
2009-07-25 04:24:39 +00:00
|
|
|
int len = 0, j;
|
|
|
|
char *p;
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
eid = ass_alloc_event(track);
|
|
|
|
event = track->events + eid;
|
|
|
|
|
|
|
|
event->Start = sub->start * 10;
|
|
|
|
event->Duration = (sub->end - sub->start) * 10;
|
2011-05-30 18:57:58 +00:00
|
|
|
event->Style = track->default_style;
|
2009-07-25 04:24:39 +00:00
|
|
|
|
|
|
|
for (j = 0; j < sub->lines; ++j)
|
|
|
|
len += sub->text[j] ? strlen(sub->text[j]) : 0;
|
|
|
|
|
|
|
|
len += 2 * sub->lines; // '\N', including the one after the last line
|
|
|
|
len += 6; // {\anX}
|
|
|
|
len += 1; // '\0'
|
|
|
|
|
|
|
|
event->Text = malloc(len);
|
|
|
|
end = event->Text + len;
|
|
|
|
p = event->Text;
|
|
|
|
|
|
|
|
if (sub->alignment)
|
|
|
|
p += snprintf(p, end - p, "{\\an%d}", sub->alignment);
|
|
|
|
|
|
|
|
for (j = 0; j < sub->lines; ++j)
|
|
|
|
p += snprintf(p, end - p, "%s\\N", sub->text[j]);
|
|
|
|
|
|
|
|
if (sub->lines > 0)
|
|
|
|
p -= 2; // remove last "\N"
|
|
|
|
*p = 0;
|
|
|
|
|
|
|
|
if (check_duplicate_plaintext_event(track)) {
|
|
|
|
ass_free_event(track, eid);
|
|
|
|
track->n_events--;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_msg(MSGT_ASS, MSGL_V,
|
|
|
|
"plaintext event at %" PRId64 ", +%" PRId64 ": %s \n",
|
|
|
|
(int64_t) event->Start, (int64_t) event->Duration, event->Text);
|
|
|
|
|
|
|
|
return eid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-08-06 20:58:31 +00:00
|
|
|
* \brief Convert subdata to ASS_Track
|
2009-07-25 04:24:39 +00:00
|
|
|
* \param subdata subtitles struct from subreader
|
|
|
|
* \param fps video framerate
|
2009-08-06 20:58:31 +00:00
|
|
|
* \return newly allocated ASS_Track, filled with subtitles from subdata
|
2009-07-25 04:24:39 +00:00
|
|
|
*/
|
2011-09-03 10:47:56 +00:00
|
|
|
ASS_Track *mp_ass_read_subdata(ASS_Library *library, struct MPOpts *opts,
|
|
|
|
sub_data *subdata, double fps)
|
2009-07-25 04:24:39 +00:00
|
|
|
{
|
2009-08-06 20:58:31 +00:00
|
|
|
ASS_Track *track;
|
2009-07-25 04:24:39 +00:00
|
|
|
int i;
|
|
|
|
|
2011-09-03 10:47:56 +00:00
|
|
|
track = mp_ass_default_track(library, opts);
|
2009-07-25 04:24:39 +00:00
|
|
|
track->name = subdata->filename ? strdup(subdata->filename) : 0;
|
|
|
|
|
|
|
|
for (i = 0; i < subdata->sub_num; ++i) {
|
|
|
|
int eid = ass_process_subtitle(track, subdata->subtitles + i);
|
|
|
|
if (eid < 0)
|
|
|
|
continue;
|
|
|
|
if (!subdata->sub_uses_time) {
|
|
|
|
track->events[eid].Start *= 100. / fps;
|
|
|
|
track->events[eid].Duration *= 100. / fps;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
2011-01-20 16:47:18 +00:00
|
|
|
ASS_Track *mp_ass_read_stream(ASS_Library *library, const char *fname,
|
|
|
|
char *charset)
|
2010-03-09 20:35:53 +00:00
|
|
|
{
|
|
|
|
ASS_Track *track;
|
|
|
|
|
2011-02-25 16:10:00 +00:00
|
|
|
struct stream *s = open_stream(fname, NULL, NULL);
|
|
|
|
if (!s)
|
2010-03-09 20:35:53 +00:00
|
|
|
// Stream code should have printed an error already
|
|
|
|
return NULL;
|
2011-02-25 16:10:00 +00:00
|
|
|
struct bstr content = stream_read_complete(s, NULL, 100000000, 1);
|
|
|
|
if (content.start == NULL)
|
|
|
|
mp_tmsg(MSGT_ASS, MSGL_ERR, "Refusing to load subtitle file "
|
|
|
|
"larger than 100 MB: %s\n", fname);
|
|
|
|
free_stream(s);
|
|
|
|
if (content.len == 0) {
|
|
|
|
talloc_free(content.start);
|
2010-03-09 20:35:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-02-25 16:10:00 +00:00
|
|
|
content.start[content.len] = 0;
|
|
|
|
track = ass_read_memory(library, content.start, content.len, charset);
|
2010-03-09 20:35:53 +00:00
|
|
|
if (track) {
|
|
|
|
free(track->name);
|
|
|
|
track->name = strdup(fname);
|
|
|
|
}
|
2011-02-25 16:10:00 +00:00
|
|
|
talloc_free(content.start);
|
2010-03-09 20:35:53 +00:00
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
2011-09-03 10:47:56 +00:00
|
|
|
void mp_ass_configure(ASS_Renderer *priv, struct MPOpts *opts, int w, int h,
|
|
|
|
bool unscaled)
|
2009-07-25 04:24:39 +00:00
|
|
|
{
|
|
|
|
int hinting;
|
|
|
|
ass_set_frame_size(priv, w, h);
|
2011-09-03 10:47:56 +00:00
|
|
|
ass_set_margins(priv, opts->ass_top_margin, opts->ass_bottom_margin, 0, 0);
|
2012-03-06 19:01:40 +00:00
|
|
|
mp_ass_reload_options(priv, opts);
|
2011-09-03 10:47:56 +00:00
|
|
|
if (!unscaled && (opts->ass_hinting & 4))
|
2009-07-25 04:24:39 +00:00
|
|
|
hinting = 0;
|
|
|
|
else
|
2011-09-03 10:47:56 +00:00
|
|
|
hinting = opts->ass_hinting & 3;
|
2009-07-25 04:24:39 +00:00
|
|
|
ass_set_hinting(priv, hinting);
|
2011-09-03 10:47:56 +00:00
|
|
|
ass_set_line_spacing(priv, opts->ass_line_spacing);
|
2009-07-25 04:24:39 +00:00
|
|
|
}
|
|
|
|
|
2011-01-20 16:47:18 +00:00
|
|
|
void mp_ass_configure_fonts(ASS_Renderer *priv)
|
2009-07-25 04:24:39 +00:00
|
|
|
{
|
|
|
|
char *dir, *path, *family;
|
|
|
|
dir = get_path("fonts");
|
2012-07-28 20:00:31 +00:00
|
|
|
path = get_path("subfont.ttf");
|
|
|
|
if (!mp_path_exists(path)) {
|
|
|
|
free(path);
|
|
|
|
path = NULL;
|
2012-04-14 00:51:19 +00:00
|
|
|
}
|
2012-07-28 20:00:31 +00:00
|
|
|
if (sub_font_name)
|
2009-07-25 04:24:39 +00:00
|
|
|
family = strdup(sub_font_name);
|
2012-07-28 20:00:31 +00:00
|
|
|
else if (font_name)
|
2009-07-25 04:24:39 +00:00
|
|
|
family = strdup(font_name);
|
|
|
|
else
|
|
|
|
family = 0;
|
|
|
|
|
2012-07-28 20:00:31 +00:00
|
|
|
ass_set_fonts(priv, path, family, 1, NULL, 1);
|
2009-07-25 04:24:39 +00:00
|
|
|
|
|
|
|
free(dir);
|
|
|
|
free(path);
|
|
|
|
free(family);
|
|
|
|
}
|
|
|
|
|
2012-07-29 21:57:46 +00:00
|
|
|
static int map_ass_level[] = {
|
|
|
|
MSGL_ERR, // 0 "FATAL errors"
|
|
|
|
MSGL_WARN,
|
|
|
|
MSGL_INFO,
|
|
|
|
MSGL_V,
|
|
|
|
MSGL_V,
|
|
|
|
MSGL_V, // 5 application recommended level
|
|
|
|
MSGL_DBG2,
|
|
|
|
MSGL_DBG3, // 7 "verbose DEBUG"
|
|
|
|
};
|
|
|
|
|
2009-07-25 04:24:39 +00:00
|
|
|
static void message_callback(int level, const char *format, va_list va, void *ctx)
|
|
|
|
{
|
2012-07-29 21:57:46 +00:00
|
|
|
level = map_ass_level[level];
|
2009-07-25 04:24:39 +00:00
|
|
|
mp_msg(MSGT_ASS, level, "[ass] ");
|
|
|
|
mp_msg_va(MSGT_ASS, level, format, va);
|
|
|
|
// libass messages lack trailing \n
|
|
|
|
mp_msg(MSGT_ASS, level, "\n");
|
|
|
|
}
|
|
|
|
|
2011-09-03 10:47:56 +00:00
|
|
|
ASS_Library *mp_ass_init(struct MPOpts *opts)
|
2009-07-25 04:24:39 +00:00
|
|
|
{
|
2009-08-06 20:58:31 +00:00
|
|
|
ASS_Library *priv;
|
2009-07-25 04:24:39 +00:00
|
|
|
char *path = get_path("fonts");
|
|
|
|
priv = ass_library_init();
|
|
|
|
ass_set_message_cb(priv, message_callback, NULL);
|
|
|
|
ass_set_fonts_dir(priv, path);
|
2011-09-03 10:47:56 +00:00
|
|
|
ass_set_extract_fonts(priv, opts->use_embedded_fonts);
|
2009-07-25 04:24:39 +00:00
|
|
|
free(path);
|
|
|
|
return priv;
|
|
|
|
}
|
|
|
|
|
2011-08-08 03:52:39 +00:00
|
|
|
void mp_ass_reload_options(ASS_Renderer *priv, struct MPOpts *opts)
|
2009-07-25 04:24:39 +00:00
|
|
|
{
|
2011-08-08 03:01:05 +00:00
|
|
|
/* This could be needed for vf_ass case if the margins were actually
|
|
|
|
* runtime configurable, but would be wrong with EOSD:
|
2011-09-03 10:47:56 +00:00
|
|
|
* ass_set_margins(priv, opts->ass_top_margin, opts->ass_bottom_margin,
|
|
|
|
* 0, 0);
|
2011-08-08 03:01:05 +00:00
|
|
|
*/
|
2011-09-03 10:47:56 +00:00
|
|
|
ass_set_use_margins(priv, opts->ass_use_margins);
|
|
|
|
ass_set_font_scale(priv, opts->ass_font_scale);
|
2009-07-25 04:24:39 +00:00
|
|
|
}
|