Commit Graph

810 Commits

Author SHA1 Message Date
nanahi dec73f503f sub/osd: fix rounding when rescaling bitmap subtitle rects
With multiple rects touching each other without any gaps, the current
scale method can cause gaps or overlaps between rects. To make sure that
this does not happen, scale on the edges of the rects instead, and then
calculate the width and height from the results.

NB: while this is better than the status quo, it does not fix all
scaling artifacts because of the following:

- When two rects share a vertical edge but have different heights,
  misalignment will occur: after rounding rect heights to pixels,
  the height scale factor for the two rects will be slightly different.
  As a result, there will be misalignment between the scaled images.
- With a GPU renderer, different bitmap subtitle parts are rendered as
  different textures. This means that the pixel contents of the parts have
  different boundary conditions from the whole combined image. As a result,
  there will still be small gaps when the subtitle is scaled up.

The only way to properly address these points is to make sure that the
parts are combined to a single image at the native resolution before
being scaled. This can be partly achieved with --blend-subtitles=video.
2024-05-03 16:16:53 +02:00
nanahi 91bb2f2543 sd_lavc: reduce unnecessary error log on probing nonexistent codecs 2024-05-03 16:13:07 +02:00
nanahi fc741dab1a sd_lavc: fix null dereference on error
If sub decoder does not exist, priv remains NULL.

Fixes: 1394e5a111
2024-05-03 16:13:07 +02:00
rcombs 99f1b2b7b4 player/command: add sub-text/ass-full sub-property
This is like sub-text/ass, but it returns a full ASS line, making it suitable for some more advanced scripting use-cases.
2024-04-27 01:19:56 +02:00
rcombs aa0a9ce2ec sd_ass: allow get_text to return more than 500 bytes 2024-04-27 01:19:56 +02:00
Kacper Michajłow f55d19e846 sub/lavc_conv: don't override style of converted teletext pages
This fixes teletext pages rendering, while keeping the same default
style for subtitles and other converted formats.
2024-04-27 01:14:23 +02:00
Kacper Michajłow d8378dc226 sub/lavc_conv: don't strip ASS style header
This fixes converted subtitles that are styled.

This reverts commit 5e2c211a4e.

Most of the subtitle decoders in libavcodec sets meaningful style
values. For the rest we can conditionally strip style.
2024-04-27 01:14:23 +02:00
Kacper Michajłow afae94cfbd dec_sub: fix locking for sub_ass_get_extradata
Fixes: 715feea8d8
2024-04-24 15:52:17 +02:00
llyyr 805577c792 sd_ass: add `sub-vsfilter-bidi-compat` to enable vsfilter bidi compat
Enable ASS_FEATURE_{WHOLE_TEXT_LAYOUT, BIDI_BRACKETS} and auto base
detection by default, and add an option to disable this if needed.

This is strictly an improvement for webvtt files as they always use
auto base detection. This _fixes_ right-to-left text rendering for
webvtt files which correctly mark rtl/ltr. Webvtt files obtained from
sources which sideload the RTL information through css also see an
improvement due to the auto detection.

Generally SRT files also want this, but some are also written to
workaround VSFilter quirks.

See also: https://github.com/mpv-player/mpv/pull/12985#issuecomment-1839565138
2024-04-18 00:14:44 +02:00
llyyr f862d3b6cd sd_ass: fix margins for all styles when overriding PlayResX
Also save old playresx and use it instead of assuming values of things
we know.
2024-04-18 00:14:44 +02:00
llyyr d2efa1c1be sd_ass: replace ifdef with explicit version check 2024-04-18 00:14:44 +02:00
nanahi 715feea8d8 dec_sub: fix locking for sub_ass_get_extradata
sub->sd can be destroyed and recreated when update_segment is called
inside a lock.

Fixes: f9918b5390
2024-04-17 23:42:35 +02:00
nanahi b7ad0968ad dec_sub: don't use recursive mutex
92a9f11a0b added locking for dec_sub.
At that time, because the lock was exposed to the outside world,
a recursive mutex was used. However, this is no longer true after
e9e883e3b2, when the public locking
functions were removed. This means that the lock is now private.

Unlike input.c, dec_sub already enforces said call hierarchy, so
combined with the aforementioned change, the lock is only ever
called once and never recursively. Thus the lock can be converted to
a normal mutex.
2024-04-17 23:42:35 +02:00
nanahi e731972163 dec_sub: fix locking for sub_is_{primary,secondary}_visible
These public functions should use locks to keep its usage
consistent with input.c.

Fixes: 024e0cd4c1
2024-04-17 23:42:35 +02:00
Kacper Michajłow 1394e5a111 sub/sd_lavc: check decoder output type for dvb and arib
Depending on the options:
For AV_CODEC_ID_ARIB_CAPTION this allows using bitmap output.
For AV_CODEC_ID_DVB_TELETEXT this allows using text output.

Fixes: #13471
2024-04-17 23:41:30 +02:00
Kacper Michajłow 35c6b62ddc sub/lavc_conv: set dvb teletext and arib caption output type to ASS
Also set teletext page while at it.
2024-04-17 23:41:30 +02:00
Kacper Michajłow 73779a8c70 sub/lavc_conv: take sd context as a parameter for lavc_conv_create
Will be useful for future commits.
2024-04-17 23:41:30 +02:00
Guido Cella d6610a5b2f command: add escape-ass
This adds a command to escape ASS tags to remove code duplication
between sub/osd_libass.c, console.lua, osc.lua, stats.lua and any user
script that calls mp.create_osd_overlay().

A command is used instead of scripting functions so that all clients can
use this and not just use Lua and JS ones.

osd_mangle_ass() also interprets osd-sym-cc and osd-ass-cc/{0,1}, but
since they use invalid UTF-8 characters there is no risk of escape-ass
users using them by accident, like with any OSD message.

Always replacing \n with \\N in mangle_ass() even when it is not called
by escape-ass doesn't seem to cause any issue, but I made it conditional
anyway to avoid changing how all OSD messages are treated unnecessarily.
2024-03-21 03:20:14 +01:00
sfan5 ead9f892b3 various: use static assertions where appropriate 2024-03-17 20:04:04 +01:00
Kacper Michajłow d6981a4cac sub: add flag if sub_bitmap should be rendered in video color space 2024-03-02 15:57:02 +00:00
Dudemanguy 8ba6d8f7a9 sd_ass: fix use-after-free in ft->event_format
0b35b4c917 originally introduced sd_filter
to make a more general subtitle filter infrastructure. But when doing
so, it directly sets ft->event_format to ass_track->event_format in the
struct. The lifetime of ass_track and the sd_filter are not equivalent
which makes it easy to trigger undefined behavior. Notably, commit
cda8f1613f introduced assobjects_destroy
which can destroy ass_track anytime during runtime which means that the
string in ft->event_format is actually freed and should never be used.
Remedy this by simply doing a proper strdup when the filter inits with
ft as the parent so we avoid this scenario altogether. Fixex #13525.
2024-02-29 15:57:58 -06:00
Christoph Heinrich 505a08a37f sd_ass: don't wrongly recognize \pos as \p
An ass event like `{\p1\pos{1,1}}m 0 0 l -3 -7 l 11 -7 l 11 -2` ends
the drawing mode started with `\p1` due to `\pos` gets confused with
`\p`, and thus that line is wrongly considered to be visible text.
2024-02-27 19:50:03 +00:00
Guido Cella 09f6f28cc2 osd_libass: update the OSD bar's dent and border size
Make the OSD bar markers bigger so we can default to a smaller,
better-looking border size, without sacrificing markers' visibility.
2024-02-26 16:01:21 +00:00
Guido Cella 748504de52 sub: fix LRC lines with multiple timestamps
LRC subtitles can have lines with multiple timestamps, e.g.

[00:00.00][00:02.00]foo
[00:01.00]bar

Currently mpv shows only the "foo" that was decoded first, because it
compares the packet file position to check if a packet was already seen,
and it is the same for both occurrences of "foo". Fix this by also
comparing the pts.

This keeps comparing the packet position on top of the pts to not break
subtitle lines with the same timestamp, like:

1
00:00:00,000 --> 00:00:01,000
foo

2
00:00:00,000 --> 00:00:01,000
bar

where mpv shows both lines on top of each other. They are common in ASS
subtitles.

Fixes https://github.com/mpv-player/mpv/issues/13497.
2024-02-25 14:30:07 +01:00
Guido Cella b35e34ae2f command: fix sub-seek while paused without a video
When using sub-seek without a video track while paused, adding the 0.01
SUB_SEEK_OFFSET to the new timestamp is not enough to show the new
subtitle line. Add 0.1 instead to fix it. 0.01 is already enough for
sub-step.
2024-02-23 21:37:22 +01:00
Dudemanguy 4e5d996c3a player/sub: attempt to detect animated subtitles
The previous commits optimized sub redrawing on still images/terminal so
mpv wouldn't redraw so much. There is a gap though. It only assumes
static subtitles. Since ASS can be animated, those types of subtitles
will always need redraws so we need to build in specific detection for
this. We need to build a whitelist of events in ASS that are considered
animations and then flag the packet. Additionally, there's a bunch of
annoying bookkeeping that has to be done since packets can be dropped on
seeks and so on.
2024-02-15 16:43:11 +00:00
Dudemanguy 8f043de961 player/sub: avoid wasteful subtitle redraws
This only affects two special cases: printing subtitles to the terminal
and printing subtitles on a still picture. Previously, mpv was very dumb
here and spammed this logic on every single loop. For terminal
subtitles, this isn't as big of a deal, but for the image case this is
pretty bad. The entire VO constantly redrew even when there was no need
to which can be very expensive depending on user settings.

Instead, let's rework sub_read_packets so that it also tells us whether
or not the subtitle packets update in some way in addition to telling us
whether or not to read more. Since we cache all packets thanks to the
previous commit, we can leverage this information to make a guess
whether or not the current subtitle packet is supposed to be visible on
the screen. Because the redraw now only happens when it is needed, the
mp_set_timeout_hack can be removed.
2024-02-15 16:43:11 +00:00
Dudemanguy 0a4b098c61 dec_sub: expand sub packet caching
Previously, mpv only saved a strict limit of two packets while decoding
subtitles, but it is a bit incomplete. Firstly, it's certainly possible
for there to be more than two subtitles visible at once. And also, it
did not take into account preloading. Rework this mechanism so that it
is a growable array that can store as many packets as we want. Note that
in this commit, the packets are only ever discarded on reset or destroy,
so in theory it could grow forever. Some discarding logic will be added
in the next commit since it is inherently tied to other things.
2024-02-15 16:43:11 +00:00
Mohammad AlSaleh a1bda5b34d sub: allow setting lavc txt_page special values via teletext_page
* Range of accepted values for teletext_page now include 0 and -1.
 * 0 means "subtitle" and -1 means "*".
 * Make 0 the default.

Signed-off-by: Mohammad AlSaleh <CE.Mohammad.AlSaleh@gmail.com>
2024-02-13 10:44:41 +01:00
Dudemanguy df720b5218 sd_lavc: support secondary subs properly
Before 3250f6e447, secondary subtitles
didn't work properly with sd_lavc at all. They would render but be in
the same position as the primary subtitles. sd_ass used SD_CTRL_SET_TOP
to specifically place secondary subtitles in a different spot, but it
was unused by sd_lavc. With that above mentioned commit, it became
possible to distinguish between the position of primary and secondary
subtitles via mpv's option system. sd_lavc was missed however because at
the time we simply didn't realize this could work and was actually
simple. With some subsequent refactoring that happened later, the fix is
to just use the correct sd->order when accessing the shared subtitle
options instead of hard coding 0. Fixes #13440.
2024-02-08 11:43:24 -06:00
Kacper Michajłow e9076896cd all: add missing repr assignments
Fixes: 66e451f4
2024-01-26 18:17:02 +02:00
Kacper Michajłow 475f76dc6d csputils: replace more primitives with pl_
We can go deeper, but need to stop somewhere to not reimplement vo_gpu
using libplacebo...
2024-01-22 14:54:55 +00:00
Kacper Michajłow 47be5ad4aa csputils: replace mp_chroma_location with pl_chroma_location 2024-01-22 14:54:55 +00:00
Kacper Michajłow 0ac7a40dac csputils: replace mp_alpha_type with pl_alpha_mode 2024-01-22 14:54:55 +00:00
Kacper Michajłow 66e451f4e6 csputils: replace mp_colorspace with pl_color_space 2024-01-22 14:54:55 +00:00
Mohammad AlSaleh 02a20b4ebe sub: add `--sub-lavc-o` option
We have `--vd-lavc-o` and `--ad-lavc-o`, but no equivalent option for
 subtitles.

Signed-off-by: Mohammad AlSaleh <CE.Mohammad.AlSaleh@gmail.com>
2024-01-21 17:55:11 +00:00
Guido Cella 0f2370476b sub: fix sub-seek and sub-step -1 with unknown duration subs
f9cefbfec4 made it so mp_ass_flush_old_events() is continously called on
subtitles with unknown duration, without explaining why, breaking
sub-seek/step -1 with a VO (the issue does not occur when showing
subtitles in the terminal because get_bitmaps() is not called). I don't
experience any issue after removing the call, so delete it to fix these
commands.

After removing that, you can sub-seek -1 once after regular playback,
but not after seeking and thus not multiple times in a row. This is
caused by a714f8e928 which fixed subtitles with unknown duration being
duplicated when seeking with a VO (it does not happen in the terminal)
by clearing old lines on seeks, which broke sub-seek -1 and sub-step -1
in a second way after any seek. The proper fix is to remove the line
ctx->num_seen_packets = 0 for subtitles with unknown duration instead,
which lets decode() return early when a line has already been shown.

Having removed these 2 lines, I also removed sd->preload_ok = false, and
thus the whole conditional, since according to sub/sd.h preload_ok only
needs to be set to false when old subtitles are discarded, and they are
no longer discarded,

The bug can be reproduced with
mpv --sub-file=<(curl 'https://music.xianqiao.wang/neteaseapiv2/lyric?id=1399616170' | jq -r .lrc.lyric) 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
2024-01-20 16:08:07 +00:00
Dudemanguy 9bf4b9d5d4 filter_sdh: optimize get_char_bytes
strlen is only relevant if the length is less than [1, 4], so this can
be replaced with strnlen instead which will only traverse characters
upto the maxlen insted of the entire string length. It also makes MPMIN
unneeded. Also fix a comment.
2024-01-15 16:05:17 +00:00
Dudemanguy 2dd3951a9c filter_sdh: fix incorrect placement of null terminator
The +1 here is not correct. For a 4-byte unicode character, this would
throw a runtime error because the +1 would try to assign the null
terminator past the actual bound of our array. Just remove it since it
should be exactly equal to whatever we have for bytes.
2024-01-12 20:46:00 -06:00
Dudemanguy e15b2b19a3 filter_sdh: sanitize get_char_bytes heuristic to avoid overflow
There's a simple check in filter_sdh that gets the bytes of the first
character in a string in order to do pointer arthimetic to filter the
string. The problem is that it is possible for the amount of bytes to be
greater than the actual length of the string for certain unicode
characters. This can't be worked with so enforce the strlen as the
absolute minimum here to avoid overflow situations.

Fixes #13237.
2024-01-12 20:45:40 -06:00
dyphire b563b2aed0 options: add --secondary-sub-ass-override
Default: strip. preserve the old behavior
2023-12-18 14:58:34 +00:00
Dudemanguy 1112de220b sd_ass: remove unneeded ontop variable
Missed in 3250f6e447. Note that the
hardcoded ass alignment value is not used anymore as of that commit, but
we should ideally be moving towards secondary subs actually being
customizable via ASS anyways.
2023-12-16 15:25:32 +00:00
Dudemanguy b0f31a7637 player: refactor secondary subtitle options and properties
Over the years, we've accumulated several secondary subtitle related
options and properties, but the implementation was not really consistent
and it wasn't clear what the right process for adding more should be. So
to make things nicer, let's refactor all of the subtitle options with
secondary variants (sub-delay, sub-pos, and sub-visibility) and split
them off to a new, separate struct. All of the underlying values are
stored in an array instead for simplicity. Additionally, the
implementation of some secondary-sub-* properties were slightly changed
so there would be less redundancy.
2023-12-16 15:25:32 +00:00
karelrooted 3250f6e447 options: add --secondary-sub-pos
The default value is 0 (on the top of the screen)
2023-12-13 21:18:57 +00:00
Dudemanguy 443c2487d7 filter_sdh: add full width parentheses to the enclosures string
Since these are technically parentheses, we'll treat them the same way
as normal parenthesis. Fixes #11155.
2023-12-08 18:14:06 +00:00
Dudemanguy ce958b7742 filter_sdh: add --sub-filter-sdh-enclosures option
This filter is a bit complicated, but one of the essential parts of it
is removing text enclosed by particular set of characters (e.g. text
inbetween []). This was previously hardcoded to only take into account
parenthesis and brackets, but people may want to filter more things so
make this customizable. The option only takes "left hand characters" so
the right pair is mapped internally if applicable. If not, then we just
use the same character. Fixes #8268 since the unicode character in
question can just be passed to this option.
2023-12-08 18:14:06 +00:00
Dudemanguy b7d85f0d4a filter_sdh: combine skip_bracketed and skip_parenthesized
These two functions are almost exactly the same. The parenthesis variant
is essentially just a special case with more conditions to not remove
text. These can easily be combined together into one generic
skip_enclosed function to handle both cases. We also use char * instead
of char for the character comparison here since not everything is
neccesarily 1 byte and can fit into a char. This will be useful for the
following commits where we extend this logic further.
2023-12-08 18:14:06 +00:00
Guido Cella 0c4812aa72 options: add --osd-bar-border-size
Closes #1484. The default size is smaller than the previous
--osd-border-size default value of 3 with the default --osd-bar-h.
2023-11-27 15:02:28 +00:00
Ripose dea512ea38 options: add secondary-sub-delay
Add --secondary-sub-delay option and decouple --sub-delay from secondary
subtitles. This produces desirable behavior in most cases as secondary
and primary subtitles tracks tend to be timed independently of one
another.

This feature is implemented by turning the sub_delay field in
mp_subtitle_opts into an array of 2 floats. From here the track index is
either passed around or derived when sub_delay is needed. There are some
cases in dec_sub.c where it is possible for dec_sub.order (equivalent to
track index) to be -1. In these cases, sub_delay is inferred as 0.
2023-11-26 23:22:05 +01:00
sfan5 aa362fdcf4 various: replace some OOM handling
We prefer to fail fast rather than degrade in unpredictable ways.
The example in sub/ is particularly egregious because the code just
skips the work it's meant to do when an allocation fails.
2023-11-24 10:04:55 +01:00