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);
|
|
|
|
if (order >= 0 && order <= 1)
|
|
|
|
osd_set_sub(mpctx->osd, OSDTYPE_SUB + order, NULL);
|
2014-10-03 17:57:49 +00:00
|
|
|
reselect_demux_streams(mpctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-01-21 22:49:25 +00:00
|
|
|
if (!track->preloaded && track->demuxer->fully_read && !opts->sub_clear_on_seek)
|
|
|
|
{
|
|
|
|
// Assume fully_read implies no interleaved audio/video streams.
|
|
|
|
// (Reading packets will change the demuxer position.)
|
|
|
|
demux_seek(track->demuxer, 0, SEEK_ABSOLUTE);
|
|
|
|
track->preloaded = sub_read_all_packets(track->d_sub);
|
|
|
|
}
|
|
|
|
|
2015-12-29 00:35:52 +00:00
|
|
|
if (!track->preloaded) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-01-17 15:56:32 +00:00
|
|
|
track->d_sub = sub_create(mpctx->global, track->demuxer, track->stream);
|
|
|
|
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);
|
|
|
|
if (order >= 0 && order <= 1)
|
|
|
|
osd_set_sub(mpctx->osd, OSDTYPE_SUB + order, track->d_sub);
|
|
|
|
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
|
|
|
}
|