2013-10-29 21:38:29 +00:00
|
|
|
/*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
2013-10-29 21:38:29 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
2013-10-29 21:38:29 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2013-10-29 21:38:29 +00:00
|
|
|
* 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
|
2015-04-13 07:36:54 +00:00
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2013-10-29 21:38:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
2016-01-11 18:03:40 +00:00
|
|
|
#include "mpv_talloc.h"
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/options.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/common.h"
|
2014-10-03 20:32:16 +00:00
|
|
|
#include "common/global.h"
|
2013-10-29 21:38:29 +00:00
|
|
|
|
|
|
|
#include "stream/stream.h"
|
|
|
|
#include "sub/dec_sub.h"
|
|
|
|
#include "demux/demux.h"
|
|
|
|
#include "video/mp_image.h"
|
2013-11-23 20:36:20 +00:00
|
|
|
#include "video/decode/dec_video.h"
|
2014-05-01 21:15:50 +00:00
|
|
|
#include "video/filter/vf.h"
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2013-12-17 00:08:53 +00:00
|
|
|
#include "core.h"
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2016-01-17 15:56:32 +00:00
|
|
|
// 0: primary sub, 1: secondary sub, -1: not selected
|
|
|
|
static int get_order(struct MPContext *mpctx, struct track *track)
|
2014-12-21 22:47:00 +00:00
|
|
|
{
|
2016-01-17 15:56:32 +00:00
|
|
|
for (int n = 0; n < NUM_PTRACKS; n++) {
|
|
|
|
if (mpctx->current_track[n][STREAM_SUB] == track)
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reset_subtitles(struct MPContext *mpctx, struct track *track)
|
|
|
|
{
|
|
|
|
if (track->d_sub)
|
|
|
|
sub_reset(track->d_sub);
|
2015-11-17 00:54:02 +00:00
|
|
|
term_osd_set_subs(mpctx, NULL);
|
2014-12-21 22:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void reset_subtitle_state(struct MPContext *mpctx)
|
|
|
|
{
|
2016-01-17 15:56:32 +00:00
|
|
|
for (int n = 0; n < mpctx->num_tracks; n++) {
|
|
|
|
struct dec_sub *d_sub = mpctx->tracks[n]->d_sub;
|
|
|
|
if (d_sub)
|
|
|
|
sub_reset(d_sub);
|
|
|
|
}
|
|
|
|
term_osd_set_subs(mpctx, NULL);
|
2014-12-21 22:47:00 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 15:56:32 +00:00
|
|
|
void uninit_sub(struct MPContext *mpctx, struct track *track)
|
2014-10-03 17:57:49 +00:00
|
|
|
{
|
2016-01-17 15:56:32 +00:00
|
|
|
if (track && track->d_sub) {
|
|
|
|
reset_subtitles(mpctx, track);
|
|
|
|
sub_select(track->d_sub, false);
|
|
|
|
int order = get_order(mpctx, track);
|
2016-03-08 20:54:17 +00:00
|
|
|
osd_set_sub(mpctx->osd, order, NULL);
|
2014-10-03 17:57:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void uninit_sub_all(struct MPContext *mpctx)
|
|
|
|
{
|
2016-01-17 15:56:32 +00:00
|
|
|
for (int n = 0; n < mpctx->num_tracks; n++)
|
|
|
|
uninit_sub(mpctx, mpctx->tracks[n]);
|
2014-10-03 17:57:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 15:56:32 +00:00
|
|
|
static bool update_subtitle(struct MPContext *mpctx, double video_pts,
|
|
|
|
struct track *track)
|
2013-10-29 21:38:29 +00:00
|
|
|
{
|
|
|
|
struct MPOpts *opts = mpctx->opts;
|
2016-01-17 15:56:32 +00:00
|
|
|
struct dec_sub *dec_sub = track ? track->d_sub : NULL;
|
2014-10-03 17:57:49 +00:00
|
|
|
|
2016-01-17 15:56:32 +00:00
|
|
|
if (!dec_sub || video_pts == MP_NOPTS_VALUE)
|
2015-12-29 00:35:52 +00:00
|
|
|
return true;
|
2014-10-03 17:57:49 +00:00
|
|
|
|
2016-01-13 23:18:48 +00:00
|
|
|
if (mpctx->vo_chain) {
|
|
|
|
struct mp_image_params params = mpctx->vo_chain->vf->input_params;
|
2013-11-23 20:39:07 +00:00
|
|
|
if (params.imgfmt)
|
|
|
|
sub_control(dec_sub, SD_CTRL_SET_VIDEO_PARAMS, ¶ms);
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|
|
|
|
|
2015-12-29 00:35:52 +00:00
|
|
|
video_pts -= opts->sub_delay;
|
|
|
|
|
sub: make preloading more robust
Subtitles can be preloaded, which means they're fully read and copied
into ASS_Track. This in turn is mainly for the sake of being able to do
subtitle seeking (when it comes down to it, subtitle seeking is the
cause for most trouble here).
Commit a714f8e92 broke preloaded subtitles which have events with
unknown duration, such as some MicroDVD samples. The event list gets
cleared on every seek, so the property of being preloaded obviously gets
lost.
Fix this by moving most of the preloading logic to dec_sub.c. If the
subtitle list gets cleared, they are not considered preloaded anymore,
and the logic for demuxed subtitles is used.
As another minor thing, preloadeding subtitles did neither disable the
demux stream, nor did it discard packets. Thus you could get queue
overflows in theory (harmless, but annoying). Fix this by explicitly
discarding packets in preloaded mode.
In summary, now the only difference between preloaded and normal
demuxing are:
1. a seek is issued, and all packets are read on start
2. during playback, discard the packets instead of feeding them to the
subtitle decoder
This is still petty annoying. It would be nice if maintaining the
subtitle index (and maybe a subtitle packet cache for instant subtitle
presentation when seeking back) could be maintained in the demuxer
instead. Half of all file formats with interleaved subtitles have
this anyway (mp4, mkv muxed with newer mkvmerge).
2016-03-06 13:50:36 +00:00
|
|
|
if (track->demuxer->fully_read && sub_can_preload(dec_sub)) {
|
2016-01-21 22:49:25 +00:00
|
|
|
// Assume fully_read implies no interleaved audio/video streams.
|
|
|
|
// (Reading packets will change the demuxer position.)
|
2016-02-28 18:14:23 +00:00
|
|
|
demux_seek(track->demuxer, 0, 0);
|
sub: make preloading more robust
Subtitles can be preloaded, which means they're fully read and copied
into ASS_Track. This in turn is mainly for the sake of being able to do
subtitle seeking (when it comes down to it, subtitle seeking is the
cause for most trouble here).
Commit a714f8e92 broke preloaded subtitles which have events with
unknown duration, such as some MicroDVD samples. The event list gets
cleared on every seek, so the property of being preloaded obviously gets
lost.
Fix this by moving most of the preloading logic to dec_sub.c. If the
subtitle list gets cleared, they are not considered preloaded anymore,
and the logic for demuxed subtitles is used.
As another minor thing, preloadeding subtitles did neither disable the
demux stream, nor did it discard packets. Thus you could get queue
overflows in theory (harmless, but annoying). Fix this by explicitly
discarding packets in preloaded mode.
In summary, now the only difference between preloaded and normal
demuxing are:
1. a seek is issued, and all packets are read on start
2. during playback, discard the packets instead of feeding them to the
subtitle decoder
This is still petty annoying. It would be nice if maintaining the
subtitle index (and maybe a subtitle packet cache for instant subtitle
presentation when seeking back) could be maintained in the demuxer
instead. Half of all file formats with interleaved subtitles have
this anyway (mp4, mkv muxed with newer mkvmerge).
2016-03-06 13:50:36 +00:00
|
|
|
sub_preload(dec_sub);
|
2016-01-21 22:49:25 +00:00
|
|
|
}
|
|
|
|
|
sub: make preloading more robust
Subtitles can be preloaded, which means they're fully read and copied
into ASS_Track. This in turn is mainly for the sake of being able to do
subtitle seeking (when it comes down to it, subtitle seeking is the
cause for most trouble here).
Commit a714f8e92 broke preloaded subtitles which have events with
unknown duration, such as some MicroDVD samples. The event list gets
cleared on every seek, so the property of being preloaded obviously gets
lost.
Fix this by moving most of the preloading logic to dec_sub.c. If the
subtitle list gets cleared, they are not considered preloaded anymore,
and the logic for demuxed subtitles is used.
As another minor thing, preloadeding subtitles did neither disable the
demux stream, nor did it discard packets. Thus you could get queue
overflows in theory (harmless, but annoying). Fix this by explicitly
discarding packets in preloaded mode.
In summary, now the only difference between preloaded and normal
demuxing are:
1. a seek is issued, and all packets are read on start
2. during playback, discard the packets instead of feeding them to the
subtitle decoder
This is still petty annoying. It would be nice if maintaining the
subtitle index (and maybe a subtitle packet cache for instant subtitle
presentation when seeking back) could be maintained in the demuxer
instead. Half of all file formats with interleaved subtitles have
this anyway (mp4, mkv muxed with newer mkvmerge).
2016-03-06 13:50:36 +00:00
|
|
|
if (!sub_read_packets(dec_sub, video_pts))
|
|
|
|
return false;
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2013-12-24 16:46:14 +00:00
|
|
|
// Handle displaying subtitles on terminal; never done for secondary subs
|
2016-01-17 15:56:32 +00:00
|
|
|
if (mpctx->current_track[0][STREAM_SUB] == track && !mpctx->video_out)
|
2015-12-29 00:35:52 +00:00
|
|
|
term_osd_set_subs(mpctx, sub_get_text(dec_sub, video_pts));
|
|
|
|
|
|
|
|
return true;
|
2013-12-24 16:46:14 +00:00
|
|
|
}
|
|
|
|
|
2015-12-29 00:35:52 +00:00
|
|
|
// Return true if the subtitles for the given PTS are ready; false if the player
|
|
|
|
// should wait for new demuxer data, and then should retry.
|
|
|
|
bool update_subtitles(struct MPContext *mpctx, double video_pts)
|
2013-12-24 16:46:14 +00:00
|
|
|
{
|
2016-01-17 15:56:32 +00:00
|
|
|
bool ok = true;
|
|
|
|
for (int n = 0; n < NUM_PTRACKS; n++)
|
|
|
|
ok &= update_subtitle(mpctx, video_pts, mpctx->current_track[n][STREAM_SUB]);
|
|
|
|
return ok;
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|
|
|
|
|
2016-03-03 17:48:56 +00:00
|
|
|
static struct attachment_list *get_all_attachments(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
struct attachment_list *list = talloc_zero(NULL, struct attachment_list);
|
|
|
|
struct demuxer *prev_demuxer = NULL;
|
|
|
|
for (int n = 0; n < mpctx->num_tracks; n++) {
|
|
|
|
struct track *t = mpctx->tracks[n];
|
|
|
|
if (!t->demuxer || prev_demuxer == t->demuxer)
|
|
|
|
continue;
|
|
|
|
prev_demuxer = t->demuxer;
|
|
|
|
for (int i = 0; i < t->demuxer->num_attachments; i++) {
|
|
|
|
struct demux_attachment *att = &t->demuxer->attachments[i];
|
|
|
|
struct demux_attachment copy = {
|
|
|
|
.name = talloc_strdup(list, att->name),
|
|
|
|
.type = talloc_strdup(list, att->type),
|
|
|
|
.data = talloc_memdup(list, att->data, att->data_size),
|
|
|
|
.data_size = att->data_size,
|
|
|
|
};
|
|
|
|
MP_TARRAY_APPEND(list, list->entries, list->num_entries, copy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2015-12-27 01:07:01 +00:00
|
|
|
static bool init_subdec(struct MPContext *mpctx, struct track *track)
|
2013-12-24 16:46:14 +00:00
|
|
|
{
|
2016-01-17 15:56:32 +00:00
|
|
|
assert(!track->d_sub);
|
2015-12-26 17:32:27 +00:00
|
|
|
|
2015-12-27 01:07:01 +00:00
|
|
|
if (!track->demuxer || !track->stream)
|
|
|
|
return false;
|
2013-12-24 16:46:14 +00:00
|
|
|
|
2016-03-03 17:48:56 +00:00
|
|
|
track->d_sub = sub_create(mpctx->global, track->stream,
|
|
|
|
get_all_attachments(mpctx));
|
2016-01-17 15:56:32 +00:00
|
|
|
if (!track->d_sub)
|
2015-12-27 01:07:01 +00:00
|
|
|
return false;
|
2015-12-27 00:25:32 +00:00
|
|
|
|
2016-01-17 17:07:50 +00:00
|
|
|
struct track *vtrack = mpctx->current_track[0][STREAM_VIDEO];
|
2016-01-12 22:48:19 +00:00
|
|
|
struct mp_codec_params *v_c =
|
2016-01-17 17:07:50 +00:00
|
|
|
vtrack && vtrack->stream ? vtrack->stream->codec : NULL;
|
2016-01-12 22:48:19 +00:00
|
|
|
double fps = v_c ? v_c->fps : 25;
|
2016-01-17 15:56:32 +00:00
|
|
|
sub_control(track->d_sub, SD_CTRL_SET_VIDEO_DEF_FPS, &fps);
|
2013-12-24 16:46:14 +00:00
|
|
|
|
2015-12-27 01:07:01 +00:00
|
|
|
return true;
|
2013-12-24 16:46:14 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 15:56:32 +00:00
|
|
|
void reinit_sub(struct MPContext *mpctx, struct track *track)
|
2013-10-29 21:38:29 +00:00
|
|
|
{
|
2016-01-17 15:56:32 +00:00
|
|
|
if (!track || !track->stream || track->stream->type != STREAM_SUB)
|
2013-10-29 21:38:29 +00:00
|
|
|
return;
|
|
|
|
|
2016-01-17 15:56:32 +00:00
|
|
|
if (!track->d_sub && !init_subdec(mpctx, track)) {
|
2015-12-27 01:07:01 +00:00
|
|
|
error_on_track(mpctx, track);
|
|
|
|
return;
|
|
|
|
}
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2016-01-17 15:56:32 +00:00
|
|
|
sub_select(track->d_sub, true);
|
|
|
|
int order = get_order(mpctx, track);
|
2016-03-08 20:54:17 +00:00
|
|
|
osd_set_sub(mpctx->osd, order, track->d_sub);
|
2016-01-17 15:56:32 +00:00
|
|
|
sub_control(track->d_sub, SD_CTRL_SET_TOP, &(bool){!!order});
|
|
|
|
}
|
2015-12-27 01:07:01 +00:00
|
|
|
|
2016-01-17 15:56:32 +00:00
|
|
|
void reinit_sub_all(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
for (int n = 0; n < NUM_PTRACKS; n++)
|
|
|
|
reinit_sub(mpctx, mpctx->current_track[n][STREAM_SUB]);
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|