1
0
mirror of https://github.com/mpv-player/mpv synced 2024-12-26 17:12:36 +00:00
mpv/sub/sub.c

356 lines
10 KiB
C
Raw Normal View History

/*
* 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 MPlayer; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <libavutil/common.h>
#include "core/mp_common.h"
#include "stream/stream.h"
#include "osdep/timer.h"
#include "talloc.h"
#include "core/options.h"
#include "core/mplayer.h"
#include "core/mp_msg.h"
#include "sub.h"
#include "dec_sub.h"
#include "img_convert.h"
#include "draw_bmp.h"
#include "spudec.h"
#include "subreader.h"
#include "video/mp_image.h"
#include "video/mp_image_pool.h"
2008-05-01 06:12:38 +00:00
char * const sub_osd_names[]={
_("Seekbar"),
_("Play"),
_("Pause"),
_("Stop"),
_("Rewind"),
_("Forward"),
_("Clock"),
_("Contrast"),
_("Saturation"),
_("Volume"),
_("Brightness"),
_("Hue"),
_("Balance")
};
2008-05-01 06:12:38 +00:00
char * const sub_osd_names_short[] ={ "", "|>", "||", "[]", "<<" , ">>", "", "", "", "", "", "", "" };
int sub_pos=100;
int sub_visibility=1;
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
subtitle* vo_sub=NULL;
float sub_delay = 0;
float sub_fps = 0;
void *vo_spudec=NULL;
void *vo_vobsub=NULL;
osd: make the OSD and sub font more customizable Make more aspects of the OSD font customizable. This also affects the font used for unstyled subtitles (such as SRT), or when using the --no-ass option. This adds back some customizability that was lost with commit 74e7a1 (osd: use libass for OSD rendering). Removed options: --ass-border-color --ass-color --font --subfont --subfont-text-scale Added options: --osd-color --osd-border --osd-back-color --osd-shadow-color --osd-font --osd-font-size --osd-border-size --osd-margin-x --osd-margin-y --osd-shadow-offset --osd-spacing --sub-scale The font size is now specified in pixels as it would be rendered on a window with a height of 720 pixels. OSD and subtitles are always scaled with the window height, so specifying or expecting an absolute font size doesn't make sense. Such scaled pixel units are used to specify font border etc. as well. (Note: the font size is directly passed to libass. How the fonts are actually rasterized is outside of our control, but in theory ASS font sizes map to "script" pixels and then are scaled to screen size.) The default settings should be about the same, with slight difference due to rounding to the new scales. The OSD and subtitle fonts are not separately configurable. It has limited use and would double the number of newly added options, which would be more confusing than helpful. It could be easily added later, should the need arise. Other small details that change: - ASS_Style.Encoding is not set to -1 for subs anymore (assuming subs use VSFilter direction in -no-ass mode too) - use a different WrapStyle for OSD - ASS forced styles are not applied to OSD
2012-11-17 19:56:45 +00:00
static const const struct osd_style_opts osd_style_opts_def = {
.font = "Sans",
.font_size = 45,
.color = {255, 255, 255, 255},
.border_color = {0, 0, 0, 255},
.shadow_color = {240, 240, 240, 128},
.border_size = 2.5,
.shadow_offset = 0,
.margin_x = 25,
.margin_y = 10,
};
osd: make the OSD and sub font more customizable Make more aspects of the OSD font customizable. This also affects the font used for unstyled subtitles (such as SRT), or when using the --no-ass option. This adds back some customizability that was lost with commit 74e7a1 (osd: use libass for OSD rendering). Removed options: --ass-border-color --ass-color --font --subfont --subfont-text-scale Added options: --osd-color --osd-border --osd-back-color --osd-shadow-color --osd-font --osd-font-size --osd-border-size --osd-margin-x --osd-margin-y --osd-shadow-offset --osd-spacing --sub-scale The font size is now specified in pixels as it would be rendered on a window with a height of 720 pixels. OSD and subtitles are always scaled with the window height, so specifying or expecting an absolute font size doesn't make sense. Such scaled pixel units are used to specify font border etc. as well. (Note: the font size is directly passed to libass. How the fonts are actually rasterized is outside of our control, but in theory ASS font sizes map to "script" pixels and then are scaled to screen size.) The default settings should be about the same, with slight difference due to rounding to the new scales. The OSD and subtitle fonts are not separately configurable. It has limited use and would double the number of newly added options, which would be more confusing than helpful. It could be easily added later, should the need arise. Other small details that change: - ASS_Style.Encoding is not set to -1 for subs anymore (assuming subs use VSFilter direction in -no-ass mode too) - use a different WrapStyle for OSD - ASS forced styles are not applied to OSD
2012-11-17 19:56:45 +00:00
#undef OPT_BASE_STRUCT
#define OPT_BASE_STRUCT struct osd_style_opts
const struct m_sub_options osd_style_conf = {
.opts = (m_option_t[]) {
OPT_STRING("font", font, 0),
OPT_FLOATRANGE("font-size", font_size, 0, 1, 9000),
OPT_COLOR("color", color, 0),
OPT_COLOR("border-color", border_color, 0),
OPT_COLOR("shadow-color", shadow_color, 0),
OPT_COLOR("back-color", back_color, 0),
OPT_FLOATRANGE("border-size", border_size, 0, 0, 10),
OPT_FLOATRANGE("shadow-offset", shadow_offset, 0, 0, 10),
OPT_FLOATRANGE("spacing", spacing, 0, -10, 10),
OPT_INTRANGE("margin-x", margin_x, 0, 0, 300),
OPT_INTRANGE("margin-y", margin_y, 0, 0, 600),
{0}
},
.size = sizeof(struct osd_style_opts),
.defaults = &osd_style_opts_def,
};
static struct osd_state *global_osd;
static bool osd_res_equals(struct mp_osd_res a, struct mp_osd_res b)
{
return a.w == b.w && a.h == b.h && a.ml == b.ml && a.mt == b.mt
&& a.mr == b.mr && a.mb == b.mb
&& a.display_par == b.display_par
&& a.video_par == b.video_par;
}
struct osd_state *osd_create(struct MPOpts *opts, struct ass_library *asslib)
{
2008-06-24 05:29:36 +00:00
struct osd_state *osd = talloc_zero(NULL, struct osd_state);
*osd = (struct osd_state) {
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
.opts = opts,
.ass_library = asslib,
.osd_text = talloc_strdup(osd, ""),
.progbar_type = -1,
};
for (int n = 0; n < MAX_OSD_PARTS; n++) {
struct osd_object *obj = talloc_struct(osd, struct osd_object, {
.type = n,
});
for (int i = 0; i < OSD_CONV_CACHE_MAX; i++)
obj->cache[i] = talloc_steal(obj, osd_conv_cache_new());
osd->objs[n] = obj;
}
osd->objs[OSDTYPE_SPU]->is_sub = true; // spudec.c
osd->objs[OSDTYPE_SUB]->is_sub = true; // dec_sub.c
osd->objs[OSDTYPE_SUBTITLE]->is_sub = true; // osd_libass.c
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
osd_init_backend(osd);
global_osd = osd;
return osd;
}
void osd_free(struct osd_state *osd)
{
if (!osd)
return;
osd_destroy_backend(osd);
talloc_free(osd);
global_osd = NULL;
}
void osd_set_text(struct osd_state *osd, const char *text)
{
if (!text)
text = "";
if (strcmp(osd->osd_text, text) == 0)
return;
talloc_free(osd->osd_text);
osd->osd_text = talloc_strdup(osd, text);
vo_osd_changed(OSDTYPE_OSD);
}
static bool spu_visible(struct osd_state *osd, struct osd_object *obj)
{
struct MPOpts *opts = osd->opts;
return opts->sub_visibility && vo_spudec && spudec_visible(vo_spudec);
}
static void render_object(struct osd_state *osd, struct osd_object *obj,
struct mp_osd_res res, double video_pts,
const bool formats[SUBBITMAP_COUNT],
struct sub_bitmaps *out_imgs)
{
struct MPOpts *opts = osd->opts;
*out_imgs = (struct sub_bitmaps) {0};
if (!osd_res_equals(res, obj->vo_res))
obj->force_redraw = true;
obj->vo_res = res;
if (obj->type == OSDTYPE_SPU) {
if (spu_visible(osd, obj))
spudec_get_indexed(vo_spudec, &obj->vo_res, out_imgs);
} else if (obj->type == OSDTYPE_SUB) {
double sub_pts = video_pts;
if (sub_pts != MP_NOPTS_VALUE)
sub_pts += sub_delay - osd->sub_offset;
sub_get_bitmaps(osd, obj->vo_res, sub_pts, out_imgs);
} else {
osd_object_get_bitmaps(osd, obj, out_imgs);
}
if (obj->force_redraw) {
out_imgs->bitmap_id++;
out_imgs->bitmap_pos_id++;
}
obj->force_redraw = false;
obj->vo_bitmap_id += out_imgs->bitmap_id;
obj->vo_bitmap_pos_id += out_imgs->bitmap_pos_id;
if (out_imgs->num_parts == 0)
return;
if (obj->cached.bitmap_id == obj->vo_bitmap_id
&& obj->cached.bitmap_pos_id == obj->vo_bitmap_pos_id
&& formats[obj->cached.format])
{
*out_imgs = obj->cached;
return;
}
out_imgs->render_index = obj->type;
out_imgs->bitmap_id = obj->vo_bitmap_id;
out_imgs->bitmap_pos_id = obj->vo_bitmap_pos_id;
if (formats[out_imgs->format])
return;
bool cached = false; // do we have a copy of all the image data?
if (out_imgs->format == SUBBITMAP_INDEXED && opts->sub_gray)
cached |= osd_conv_idx_to_gray(obj->cache[0], out_imgs);
if (formats[SUBBITMAP_RGBA] && out_imgs->format == SUBBITMAP_INDEXED)
cached |= osd_conv_idx_to_rgba(obj->cache[1], out_imgs);
if (out_imgs->format == SUBBITMAP_RGBA && opts->sub_gauss != 0.0f)
cached |= osd_conv_blur_rgba(obj->cache[2], out_imgs, opts->sub_gauss);
// Do this conversion last to not trigger gauss blurring for ASS
if (formats[SUBBITMAP_RGBA] && out_imgs->format == SUBBITMAP_LIBASS)
cached |= osd_conv_ass_to_rgba(obj->cache[3], out_imgs);
if (cached)
obj->cached = *out_imgs;
}
// draw_flags is a bit field of OSD_DRAW_* constants
void osd_draw(struct osd_state *osd, struct mp_osd_res res,
double video_pts, int draw_flags,
const bool formats[SUBBITMAP_COUNT],
void (*cb)(void *ctx, struct sub_bitmaps *imgs), void *cb_ctx)
{
if (draw_flags & OSD_DRAW_SUB_FILTER)
draw_flags |= OSD_DRAW_SUB_ONLY;
for (int n = 0; n < MAX_OSD_PARTS; n++) {
struct osd_object *obj = osd->objs[n];
// Object is drawn into the video frame itself; don't draw twice
if (osd->render_subs_in_filter && obj->is_sub &&
!(draw_flags & OSD_DRAW_SUB_FILTER))
continue;
if ((draw_flags & OSD_DRAW_SUB_ONLY) && !obj->is_sub)
continue;
struct sub_bitmaps imgs;
render_object(osd, obj, res, video_pts, formats, &imgs);
if (imgs.num_parts > 0) {
if (formats[imgs.format]) {
cb(cb_ctx, &imgs);
} else {
mp_msg(MSGT_OSD, MSGL_ERR,
"Can't render OSD part %d (format %d).\n",
obj->type, imgs.format);
}
}
}
}
struct draw_on_image_closure {
struct osd_state *osd;
struct mp_image *dest;
struct mp_image_pool *pool;
bool changed;
};
static void draw_on_image(void *ctx, struct sub_bitmaps *imgs)
{
struct draw_on_image_closure *closure = ctx;
struct osd_state *osd = closure->osd;
if (closure->pool) {
mp_image_pool_make_writeable(closure->pool, closure->dest);
} else {
mp_image_make_writeable(closure->dest);
}
mp_draw_sub_bitmaps(&osd->draw_cache, closure->dest, imgs);
talloc_steal(osd, osd->draw_cache);
closure->changed = true;
}
// Calls mp_image_make_writeable() on the dest image if something is drawn.
// Returns whether anything was drawn.
bool osd_draw_on_image(struct osd_state *osd, struct mp_osd_res res,
double video_pts, int draw_flags, struct mp_image *dest)
{
struct draw_on_image_closure closure = {osd, dest};
osd_draw(osd, res, video_pts, draw_flags, mp_draw_sub_formats,
&draw_on_image, &closure);
return closure.changed;
}
// Like osd_draw_on_image(), but if dest needs to be copied to make it
// writeable, allocate images from the given pool. (This is a minor
// optimization to reduce "real" image sized memory allocations.)
void osd_draw_on_image_p(struct osd_state *osd, struct mp_osd_res res,
double video_pts, int draw_flags,
struct mp_image_pool *pool, struct mp_image *dest)
{
struct draw_on_image_closure closure = {osd, dest, pool};
osd_draw(osd, res, video_pts, draw_flags, mp_draw_sub_formats,
&draw_on_image, &closure);
}
void vo_osd_changed(int new_value)
{
struct osd_state *osd = global_osd;
for (int n = 0; n < MAX_OSD_PARTS; n++) {
if (osd->objs[n]->type == new_value)
osd->objs[n]->force_redraw = true;
}
osd->want_redraw = true;
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
}
osd: make the OSD and sub font more customizable Make more aspects of the OSD font customizable. This also affects the font used for unstyled subtitles (such as SRT), or when using the --no-ass option. This adds back some customizability that was lost with commit 74e7a1 (osd: use libass for OSD rendering). Removed options: --ass-border-color --ass-color --font --subfont --subfont-text-scale Added options: --osd-color --osd-border --osd-back-color --osd-shadow-color --osd-font --osd-font-size --osd-border-size --osd-margin-x --osd-margin-y --osd-shadow-offset --osd-spacing --sub-scale The font size is now specified in pixels as it would be rendered on a window with a height of 720 pixels. OSD and subtitles are always scaled with the window height, so specifying or expecting an absolute font size doesn't make sense. Such scaled pixel units are used to specify font border etc. as well. (Note: the font size is directly passed to libass. How the fonts are actually rasterized is outside of our control, but in theory ASS font sizes map to "script" pixels and then are scaled to screen size.) The default settings should be about the same, with slight difference due to rounding to the new scales. The OSD and subtitle fonts are not separately configurable. It has limited use and would double the number of newly added options, which would be more confusing than helpful. It could be easily added later, should the need arise. Other small details that change: - ASS_Style.Encoding is not set to -1 for subs anymore (assuming subs use VSFilter direction in -no-ass mode too) - use a different WrapStyle for OSD - ASS forced styles are not applied to OSD
2012-11-17 19:56:45 +00:00
void osd_subs_changed(struct osd_state *osd)
{
for (int n = 0; n < MAX_OSD_PARTS; n++) {
if (osd->objs[n]->is_sub)
vo_osd_changed(n);
}
}
bool sub_bitmaps_bb(struct sub_bitmaps *imgs, struct mp_rect *out_bb)
{
struct mp_rect bb = {INT_MAX, INT_MAX, INT_MIN, INT_MIN};
for (int n = 0; n < imgs->num_parts; n++) {
struct sub_bitmap *p = &imgs->parts[n];
bb.x0 = FFMIN(bb.x0, p->x);
bb.y0 = FFMIN(bb.y0, p->y);
bb.x1 = FFMAX(bb.x1, p->x + p->dw);
bb.y1 = FFMAX(bb.y1, p->y + p->dh);
}
// avoid degenerate bounding box if empty
bb.x0 = FFMIN(bb.x0, bb.x1);
bb.y0 = FFMIN(bb.y0, bb.y1);
*out_bb = bb;
return bb.x0 < bb.x1 && bb.y0 < bb.y1;
}