2013-10-29 22:38:29 +01:00
|
|
|
/*
|
2015-04-13 09:36:54 +02:00
|
|
|
* This file is part of mpv.
|
2013-10-29 22:38:29 +01:00
|
|
|
*
|
2015-04-13 09:36:54 +02:00
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
2013-10-29 22:38:29 +01: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 09:36:54 +02:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2013-10-29 22:38:29 +01: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 09:36:54 +02:00
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2013-10-29 22:38:29 +01: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 02:39:45 +01:00
|
|
|
#include "common/msg.h"
|
2013-12-17 02:02:25 +01:00
|
|
|
#include "options/options.h"
|
2013-12-17 02:39:45 +01:00
|
|
|
#include "common/common.h"
|
2014-10-03 22:32:16 +02:00
|
|
|
#include "common/global.h"
|
2013-10-29 22:38:29 +01:00
|
|
|
|
|
|
|
#include "stream/stream.h"
|
|
|
|
#include "sub/dec_sub.h"
|
|
|
|
#include "demux/demux.h"
|
|
|
|
#include "video/mp_image.h"
|
2013-11-23 21:36:20 +01:00
|
|
|
#include "video/decode/dec_video.h"
|
2014-05-01 23:15:50 +02:00
|
|
|
#include "video/filter/vf.h"
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2013-12-17 01:08:53 +01:00
|
|
|
#include "core.h"
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2014-12-21 23:47:00 +01:00
|
|
|
static void reset_subtitles(struct MPContext *mpctx, int order)
|
|
|
|
{
|
|
|
|
if (mpctx->d_sub[order])
|
|
|
|
sub_reset(mpctx->d_sub[order]);
|
2015-11-17 01:54:02 +01:00
|
|
|
term_osd_set_subs(mpctx, NULL);
|
2014-12-21 23:47:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void reset_subtitle_state(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
reset_subtitles(mpctx, 0);
|
|
|
|
reset_subtitles(mpctx, 1);
|
|
|
|
}
|
|
|
|
|
2014-10-03 19:57:49 +02:00
|
|
|
void uninit_sub(struct MPContext *mpctx, int order)
|
|
|
|
{
|
|
|
|
if (mpctx->d_sub[order]) {
|
|
|
|
reset_subtitles(mpctx, order);
|
2015-12-26 18:35:36 +01:00
|
|
|
sub_select(mpctx->d_sub[order], false);
|
2015-12-26 18:32:27 +01:00
|
|
|
mpctx->d_sub[order] = NULL; // not destroyed
|
2015-11-17 01:54:02 +01:00
|
|
|
osd_set_sub(mpctx->osd, OSDTYPE_SUB + order, NULL);
|
2014-10-03 19:57:49 +02:00
|
|
|
reselect_demux_streams(mpctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void uninit_sub_all(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
uninit_sub(mpctx, 0);
|
|
|
|
uninit_sub(mpctx, 1);
|
|
|
|
}
|
|
|
|
|
2013-10-29 22:38:29 +01:00
|
|
|
// When reading subtitles from a demuxer, and we read video or audio from the
|
|
|
|
// demuxer, we should not explicitly read subtitle packets. (With external
|
|
|
|
// subs, we have to.)
|
|
|
|
static bool is_interleaved(struct MPContext *mpctx, struct track *track)
|
|
|
|
{
|
|
|
|
if (track->is_external || !track->demuxer)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
struct demuxer *demuxer = track->demuxer;
|
2013-12-23 20:14:54 +01:00
|
|
|
for (int t = 0; t < mpctx->num_tracks; t++) {
|
|
|
|
struct track *other = mpctx->tracks[t];
|
|
|
|
if (other != track && other->selected && other->demuxer == demuxer &&
|
|
|
|
(other->type == STREAM_VIDEO || other->type == STREAM_AUDIO))
|
2013-10-29 22:38:29 +01:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-04 18:11:12 +02:00
|
|
|
return track->demuxer == mpctx->demuxer;
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|
|
|
|
|
2013-12-24 17:46:14 +01:00
|
|
|
static void update_subtitle(struct MPContext *mpctx, int order)
|
2013-10-29 22:38:29 +01:00
|
|
|
{
|
|
|
|
struct MPOpts *opts = mpctx->opts;
|
2013-12-24 17:46:14 +01:00
|
|
|
struct track *track = mpctx->current_track[order][STREAM_SUB];
|
|
|
|
struct dec_sub *dec_sub = mpctx->d_sub[order];
|
2014-10-03 19:57:49 +02:00
|
|
|
|
2014-12-07 14:21:12 +01:00
|
|
|
if (!track || !dec_sub)
|
2014-10-03 19:57:49 +02:00
|
|
|
return;
|
|
|
|
|
2013-11-23 21:39:07 +01:00
|
|
|
if (mpctx->d_video) {
|
2014-05-01 23:15:50 +02:00
|
|
|
struct mp_image_params params = mpctx->d_video->vfilter->override_params;
|
2013-11-23 21:39:07 +01:00
|
|
|
if (params.imgfmt)
|
|
|
|
sub_control(dec_sub, SD_CTRL_SET_VIDEO_PARAMS, ¶ms);
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|
|
|
|
|
2015-11-16 23:15:59 +01:00
|
|
|
double refpts_s = mpctx->playback_pts;
|
2014-01-06 17:09:31 +01:00
|
|
|
double curpts_s = refpts_s - opts->sub_delay;
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2013-11-23 21:37:15 +01:00
|
|
|
if (!track->preloaded && track->stream) {
|
|
|
|
struct sh_stream *sh_stream = track->stream;
|
2013-10-29 22:38:29 +01:00
|
|
|
bool interleaved = is_interleaved(mpctx, track);
|
|
|
|
|
|
|
|
while (1) {
|
2013-11-23 21:37:15 +01:00
|
|
|
if (interleaved && !demux_has_packet(sh_stream))
|
2013-10-29 22:38:29 +01:00
|
|
|
break;
|
2013-11-23 21:37:15 +01:00
|
|
|
double subpts_s = demux_get_next_pts(sh_stream);
|
|
|
|
if (!demux_has_packet(sh_stream))
|
2013-10-29 22:38:29 +01:00
|
|
|
break;
|
|
|
|
if (subpts_s > curpts_s) {
|
2013-12-19 21:28:55 +01:00
|
|
|
MP_DBG(mpctx, "Sub early: c_pts=%5.3f s_pts=%5.3f\n",
|
2013-10-29 22:38:29 +01:00
|
|
|
curpts_s, subpts_s);
|
2015-12-05 23:54:00 +01:00
|
|
|
// Often subs can be handled in advance
|
|
|
|
if (!sub_accepts_packet_in_advance(dec_sub))
|
2013-10-29 22:38:29 +01:00
|
|
|
break;
|
|
|
|
// Try to avoid demuxing whole file at once
|
|
|
|
if (subpts_s > curpts_s + 1 && !interleaved)
|
|
|
|
break;
|
|
|
|
}
|
2013-11-23 21:37:15 +01:00
|
|
|
struct demux_packet *pkt = demux_read_packet(sh_stream);
|
2013-12-19 21:28:55 +01:00
|
|
|
MP_DBG(mpctx, "Sub: c_pts=%5.3f s_pts=%5.3f duration=%5.3f len=%d\n",
|
|
|
|
curpts_s, pkt->pts, pkt->duration, pkt->len);
|
2013-10-29 22:38:29 +01:00
|
|
|
sub_decode(dec_sub, pkt);
|
|
|
|
talloc_free(pkt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-24 17:46:14 +01:00
|
|
|
// Handle displaying subtitles on terminal; never done for secondary subs
|
2015-11-17 01:54:02 +01:00
|
|
|
if (order == 0 && !mpctx->video_out)
|
|
|
|
term_osd_set_subs(mpctx, sub_get_text(dec_sub, curpts_s));
|
2013-12-24 17:46:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void update_subtitles(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
update_subtitle(mpctx, 0);
|
|
|
|
update_subtitle(mpctx, 1);
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|
|
|
|
|
2015-12-26 18:32:27 +01:00
|
|
|
static void reinit_subdec(struct MPContext *mpctx, struct track *track)
|
2013-12-24 17:46:14 +01:00
|
|
|
{
|
2014-08-14 23:59:35 +02:00
|
|
|
struct MPOpts *opts = mpctx->opts;
|
|
|
|
|
2015-12-26 18:32:27 +01:00
|
|
|
struct dec_sub *dec_sub = track->dec_sub;
|
|
|
|
|
2013-12-24 17:46:14 +01:00
|
|
|
if (sub_is_initialized(dec_sub))
|
|
|
|
return;
|
|
|
|
|
2015-12-27 01:25:32 +01:00
|
|
|
sub_init(dec_sub, track->demuxer, track->stream);
|
|
|
|
|
2013-12-24 17:46:14 +01:00
|
|
|
struct sh_video *sh_video =
|
|
|
|
mpctx->d_video ? mpctx->d_video->header->video : NULL;
|
2015-12-27 01:25:32 +01:00
|
|
|
double fps = sh_video ? sh_video->fps : 25;
|
|
|
|
sub_control(dec_sub, SD_CTRL_SET_VIDEO_DEF_FPS, &fps);
|
2013-12-24 17:46:14 +01: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.
|
2014-08-14 23:59:35 +02:00
|
|
|
if (!track->preloaded && track->is_external && !opts->sub_clear_on_seek) {
|
2013-12-24 17:46:14 +01:00
|
|
|
demux_seek(track->demuxer, 0, SEEK_ABSOLUTE);
|
|
|
|
track->preloaded = sub_read_all_packets(dec_sub, track->stream);
|
2015-02-18 21:10:43 +01:00
|
|
|
if (track->preloaded)
|
|
|
|
demux_stop_thread(track->demuxer);
|
2013-12-24 17:46:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void reinit_subs(struct MPContext *mpctx, int order)
|
2013-10-29 22:38:29 +01:00
|
|
|
{
|
2013-12-24 17:46:14 +01:00
|
|
|
struct track *track = mpctx->current_track[order][STREAM_SUB];
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2014-10-03 19:57:49 +02:00
|
|
|
assert(!mpctx->d_sub[order]);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2014-07-29 17:55:28 +02:00
|
|
|
struct sh_stream *sh = track ? track->stream : NULL;
|
2013-11-23 21:37:15 +01:00
|
|
|
if (!sh)
|
2013-10-29 22:38:29 +01:00
|
|
|
return;
|
|
|
|
|
2015-12-26 18:32:27 +01:00
|
|
|
if (!track->dec_sub)
|
|
|
|
track->dec_sub = sub_create(mpctx->global);
|
|
|
|
mpctx->d_sub[order] = track->dec_sub;
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2015-12-26 18:35:36 +01:00
|
|
|
sub_select(track->dec_sub, true);
|
2015-12-26 18:32:27 +01:00
|
|
|
reinit_subdec(mpctx, track);
|
|
|
|
osd_set_sub(mpctx->osd, OSDTYPE_SUB + order, track->dec_sub);
|
|
|
|
sub_control(track->dec_sub, SD_CTRL_SET_TOP, &(bool){!!order});
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|