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"
|
|
|
|
#include "talloc.h"
|
|
|
|
|
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
|
|
|
|
2014-12-21 22:47:00 +00:00
|
|
|
static void reset_subtitles(struct MPContext *mpctx, int order)
|
|
|
|
{
|
|
|
|
if (mpctx->d_sub[order])
|
|
|
|
sub_reset(mpctx->d_sub[order]);
|
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)
|
|
|
|
{
|
|
|
|
reset_subtitles(mpctx, 0);
|
|
|
|
reset_subtitles(mpctx, 1);
|
|
|
|
}
|
|
|
|
|
2014-10-03 17:57:49 +00:00
|
|
|
void uninit_sub(struct MPContext *mpctx, int order)
|
|
|
|
{
|
|
|
|
if (mpctx->d_sub[order]) {
|
|
|
|
reset_subtitles(mpctx, order);
|
2015-12-26 17:35:36 +00:00
|
|
|
sub_select(mpctx->d_sub[order], false);
|
2015-12-26 17:32:27 +00:00
|
|
|
mpctx->d_sub[order] = NULL; // not destroyed
|
2015-11-17 00:54:02 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
uninit_sub(mpctx, 0);
|
|
|
|
uninit_sub(mpctx, 1);
|
|
|
|
}
|
|
|
|
|
2015-12-29 00:35:52 +00:00
|
|
|
static bool update_subtitle(struct MPContext *mpctx, double video_pts, int order)
|
2013-10-29 21:38:29 +00:00
|
|
|
{
|
|
|
|
struct MPOpts *opts = mpctx->opts;
|
2013-12-24 16:46:14 +00:00
|
|
|
struct track *track = mpctx->current_track[order][STREAM_SUB];
|
|
|
|
struct dec_sub *dec_sub = mpctx->d_sub[order];
|
2014-10-03 17:57:49 +00:00
|
|
|
|
2015-12-29 00:35:52 +00:00
|
|
|
if (!track || !dec_sub || video_pts == MP_NOPTS_VALUE)
|
|
|
|
return true;
|
2014-10-03 17:57:49 +00:00
|
|
|
|
2013-11-23 20:39:07 +00:00
|
|
|
if (mpctx->d_video) {
|
2014-05-01 21:15:50 +00:00
|
|
|
struct mp_image_params params = mpctx->d_video->vfilter->override_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;
|
|
|
|
|
|
|
|
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
|
2015-11-17 00:54:02 +00:00
|
|
|
if (order == 0 && !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
|
|
|
{
|
2015-12-29 00:35:52 +00:00
|
|
|
return update_subtitle(mpctx, video_pts, 0) &
|
|
|
|
update_subtitle(mpctx, video_pts, 1);
|
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
|
|
|
{
|
2014-08-14 21:59:35 +00:00
|
|
|
struct MPOpts *opts = mpctx->opts;
|
|
|
|
|
2015-12-27 01:07:01 +00:00
|
|
|
assert(!track->dec_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
|
|
|
|
2015-12-27 01:07:01 +00:00
|
|
|
track->dec_sub = sub_create(mpctx->global, track->demuxer, track->stream);
|
|
|
|
if (!track->dec_sub)
|
|
|
|
return false;
|
2015-12-27 00:25:32 +00:00
|
|
|
|
2013-12-24 16:46:14 +00:00
|
|
|
struct sh_video *sh_video =
|
|
|
|
mpctx->d_video ? mpctx->d_video->header->video : NULL;
|
2015-12-27 00:25:32 +00:00
|
|
|
double fps = sh_video ? sh_video->fps : 25;
|
2015-12-27 01:07:01 +00:00
|
|
|
sub_control(track->dec_sub, SD_CTRL_SET_VIDEO_DEF_FPS, &fps);
|
2013-12-24 16:46:14 +00:00
|
|
|
|
|
|
|
// Don't do this if the file has video/audio streams. Don't do it even
|
|
|
|
// if it has only sub streams, because reading packets will change the
|
|
|
|
// demuxer position.
|
2015-12-27 01:07:01 +00:00
|
|
|
if (track->is_external && !opts->sub_clear_on_seek) {
|
2013-12-24 16:46:14 +00:00
|
|
|
demux_seek(track->demuxer, 0, SEEK_ABSOLUTE);
|
2015-12-27 01:07:01 +00:00
|
|
|
track->preloaded = sub_read_all_packets(track->dec_sub);
|
2015-02-18 20:10:43 +00:00
|
|
|
if (track->preloaded)
|
|
|
|
demux_stop_thread(track->demuxer);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void reinit_subs(struct MPContext *mpctx, int order)
|
2013-10-29 21:38:29 +00:00
|
|
|
{
|
2014-10-03 17:57:49 +00:00
|
|
|
assert(!mpctx->d_sub[order]);
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2015-12-27 01:07:01 +00:00
|
|
|
struct track *track = mpctx->current_track[order][STREAM_SUB];
|
|
|
|
if (!track)
|
2013-10-29 21:38:29 +00:00
|
|
|
return;
|
|
|
|
|
2015-12-27 01:07:01 +00:00
|
|
|
if (!track->dec_sub && !init_subdec(mpctx, track)) {
|
|
|
|
error_on_track(mpctx, track);
|
|
|
|
return;
|
|
|
|
}
|
2013-10-29 21:38:29 +00:00
|
|
|
|
2015-12-26 17:35:36 +00:00
|
|
|
sub_select(track->dec_sub, true);
|
2015-12-26 17:32:27 +00:00
|
|
|
osd_set_sub(mpctx->osd, OSDTYPE_SUB + order, track->dec_sub);
|
|
|
|
sub_control(track->dec_sub, SD_CTRL_SET_TOP, &(bool){!!order});
|
2015-12-27 01:07:01 +00:00
|
|
|
|
|
|
|
mpctx->d_sub[order] = track->dec_sub;
|
2013-10-29 21:38:29 +00:00
|
|
|
}
|