2013-10-29 22:38:29 +01:00
|
|
|
/*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
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
|
|
|
|
|
|
|
void uninit_subs(struct demuxer *demuxer)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < demuxer->num_streams; i++) {
|
|
|
|
struct sh_stream *sh = demuxer->streams[i];
|
|
|
|
if (sh->sub) {
|
|
|
|
sub_destroy(sh->sub->dec_sub);
|
|
|
|
sh->sub->dec_sub = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
void reset_subtitles(struct MPContext *mpctx, int order)
|
2013-10-29 22:38:29 +01:00
|
|
|
{
|
2014-01-18 01:19:20 +01:00
|
|
|
int obj = order ? OSDTYPE_SUB2 : OSDTYPE_SUB;
|
2013-12-24 17:46:14 +01:00
|
|
|
if (mpctx->d_sub[order])
|
|
|
|
sub_reset(mpctx->d_sub[order]);
|
2013-10-29 22:38:29 +01:00
|
|
|
set_osd_subtitle(mpctx, NULL);
|
2014-01-18 01:19:20 +01:00
|
|
|
osd_set_text(mpctx->osd, obj, NULL);
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|
|
|
|
|
2014-07-30 23:01:55 +02:00
|
|
|
void reset_subtitle_state(struct MPContext *mpctx)
|
|
|
|
{
|
|
|
|
reset_subtitles(mpctx, 0);
|
|
|
|
reset_subtitles(mpctx, 1);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
if (order == 0) {
|
|
|
|
if (!(mpctx->initialized_flags & INITIALIZED_SUB))
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (!(mpctx->initialized_flags & INITIALIZED_SUB2))
|
|
|
|
return;
|
|
|
|
}
|
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];
|
|
|
|
struct dec_sub *dec_sub = mpctx->d_sub[order];
|
2013-11-23 21:37:15 +01:00
|
|
|
assert(track && dec_sub);
|
2014-01-18 01:19:20 +01:00
|
|
|
int obj = order ? OSDTYPE_SUB2 : OSDTYPE_SUB;
|
2013-10-29 22:38:29 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-01-18 01:19:20 +01:00
|
|
|
struct osd_sub_state state;
|
|
|
|
osd_get_sub(mpctx->osd, obj, &state);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2014-08-04 18:13:09 +02:00
|
|
|
state.video_offset = 0;
|
|
|
|
if (track->under_timeline) {
|
|
|
|
state.video_offset += mpctx->video_offset;
|
|
|
|
} else if (track->is_external) {
|
|
|
|
state.video_offset += get_start_time(mpctx);
|
|
|
|
};
|
2014-01-18 01:19:20 +01:00
|
|
|
|
|
|
|
osd_set_sub(mpctx->osd, obj, &state);
|
|
|
|
|
|
|
|
double refpts_s = mpctx->playback_pts - state.video_offset;
|
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);
|
|
|
|
|
2013-11-23 21:37:15 +01:00
|
|
|
assert(sh_stream->sub->dec_sub == dec_sub);
|
|
|
|
|
2013-10-29 22:38:29 +01:00
|
|
|
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);
|
|
|
|
// Libass handled subs can be fed to it in advance
|
|
|
|
if (!sub_accept_packets_in_advance(dec_sub))
|
|
|
|
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
|
|
|
|
if (order == 0) {
|
2014-01-18 01:19:20 +01:00
|
|
|
if (!state.render_bitmap_subs || !mpctx->video_out) {
|
sub: uglify sub decoder with locking
The plan is to make the whole OSD thread-safe, and we start with this.
We just put locks on all entry points (fortunately, dec_sub.c and all
sd_*.c decoders are very closed off, and only the entry points in
dec_sub.h let you access it). I think this is pretty ugly, but at least
it's very simple.
There's a special case with sub_get_bitmaps(): this function returns
pointers to decoder data (specifically, libass images). There's no way
to synchronize this internally, so expose sub_lock/sub_unlock functions.
To make things simpler, and especially because the lock is sort-of
exposed to the outside world, make the locks recursive. Although the
only case where this is actually needed (although trivial) is
sub_set_extradata().
One corner case are ASS subtitles: for some reason, we keep a single
ASS_Renderer instance for subtitles around (probably to avoid rescanning
fonts with ordered chapters), and this ASS_Renderer instance is not
synchronized. Also, demux_libass.c loads ASS_Track objects, which are
directly passed to sd_ass.c. These things are not synchronized (and
would be hard to synchronize), and basically we're out of luck. But I
think for now, accesses happen reasonably serialized, so there is no
actual problem yet, even if we start to access OSD from other threads.
2014-01-17 23:13:09 +01:00
|
|
|
sub_lock(dec_sub);
|
2013-12-24 17:46:14 +01:00
|
|
|
set_osd_subtitle(mpctx, sub_get_text(dec_sub, curpts_s));
|
sub: uglify sub decoder with locking
The plan is to make the whole OSD thread-safe, and we start with this.
We just put locks on all entry points (fortunately, dec_sub.c and all
sd_*.c decoders are very closed off, and only the entry points in
dec_sub.h let you access it). I think this is pretty ugly, but at least
it's very simple.
There's a special case with sub_get_bitmaps(): this function returns
pointers to decoder data (specifically, libass images). There's no way
to synchronize this internally, so expose sub_lock/sub_unlock functions.
To make things simpler, and especially because the lock is sort-of
exposed to the outside world, make the locks recursive. Although the
only case where this is actually needed (although trivial) is
sub_set_extradata().
One corner case are ASS subtitles: for some reason, we keep a single
ASS_Renderer instance for subtitles around (probably to avoid rescanning
fonts with ordered chapters), and this ASS_Renderer instance is not
synchronized. Also, demux_libass.c loads ASS_Track objects, which are
directly passed to sd_ass.c. These things are not synchronized (and
would be hard to synchronize), and basically we're out of luck. But I
think for now, accesses happen reasonably serialized, so there is no
actual problem yet, even if we start to access OSD from other threads.
2014-01-17 23:13:09 +01:00
|
|
|
sub_unlock(dec_sub);
|
|
|
|
}
|
2013-12-24 17:46:14 +01:00
|
|
|
} else if (order == 1) {
|
sub: uglify sub decoder with locking
The plan is to make the whole OSD thread-safe, and we start with this.
We just put locks on all entry points (fortunately, dec_sub.c and all
sd_*.c decoders are very closed off, and only the entry points in
dec_sub.h let you access it). I think this is pretty ugly, but at least
it's very simple.
There's a special case with sub_get_bitmaps(): this function returns
pointers to decoder data (specifically, libass images). There's no way
to synchronize this internally, so expose sub_lock/sub_unlock functions.
To make things simpler, and especially because the lock is sort-of
exposed to the outside world, make the locks recursive. Although the
only case where this is actually needed (although trivial) is
sub_set_extradata().
One corner case are ASS subtitles: for some reason, we keep a single
ASS_Renderer instance for subtitles around (probably to avoid rescanning
fonts with ordered chapters), and this ASS_Renderer instance is not
synchronized. Also, demux_libass.c loads ASS_Track objects, which are
directly passed to sd_ass.c. These things are not synchronized (and
would be hard to synchronize), and basically we're out of luck. But I
think for now, accesses happen reasonably serialized, so there is no
actual problem yet, even if we start to access OSD from other threads.
2014-01-17 23:13:09 +01:00
|
|
|
sub_lock(dec_sub);
|
2014-01-18 01:19:20 +01:00
|
|
|
osd_set_text(mpctx->osd, obj, sub_get_text(dec_sub, curpts_s));
|
sub: uglify sub decoder with locking
The plan is to make the whole OSD thread-safe, and we start with this.
We just put locks on all entry points (fortunately, dec_sub.c and all
sd_*.c decoders are very closed off, and only the entry points in
dec_sub.h let you access it). I think this is pretty ugly, but at least
it's very simple.
There's a special case with sub_get_bitmaps(): this function returns
pointers to decoder data (specifically, libass images). There's no way
to synchronize this internally, so expose sub_lock/sub_unlock functions.
To make things simpler, and especially because the lock is sort-of
exposed to the outside world, make the locks recursive. Although the
only case where this is actually needed (although trivial) is
sub_set_extradata().
One corner case are ASS subtitles: for some reason, we keep a single
ASS_Renderer instance for subtitles around (probably to avoid rescanning
fonts with ordered chapters), and this ASS_Renderer instance is not
synchronized. Also, demux_libass.c loads ASS_Track objects, which are
directly passed to sd_ass.c. These things are not synchronized (and
would be hard to synchronize), and basically we're out of luck. But I
think for now, accesses happen reasonably serialized, so there is no
actual problem yet, even if we start to access OSD from other threads.
2014-01-17 23:13:09 +01:00
|
|
|
sub_unlock(dec_sub);
|
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
|
|
|
}
|
|
|
|
|
2013-12-24 17:46:14 +01:00
|
|
|
static void reinit_subdec(struct MPContext *mpctx, struct track *track,
|
|
|
|
struct dec_sub *dec_sub)
|
|
|
|
{
|
|
|
|
if (sub_is_initialized(dec_sub))
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct sh_video *sh_video =
|
|
|
|
mpctx->d_video ? mpctx->d_video->header->video : NULL;
|
|
|
|
int w = sh_video ? sh_video->disp_w : 0;
|
|
|
|
int h = sh_video ? sh_video->disp_h : 0;
|
|
|
|
float fps = sh_video ? sh_video->fps : 25;
|
|
|
|
|
|
|
|
sub_set_video_res(dec_sub, w, h);
|
|
|
|
sub_set_video_fps(dec_sub, fps);
|
|
|
|
sub_set_ass_renderer(dec_sub, mpctx->ass_library, mpctx->ass_renderer);
|
|
|
|
sub_init_from_sh(dec_sub, track->stream);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (!track->preloaded && track->is_external) {
|
|
|
|
demux_seek(track->demuxer, 0, SEEK_ABSOLUTE);
|
|
|
|
track->preloaded = sub_read_all_packets(dec_sub, track->stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void reinit_subs(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];
|
2014-01-18 01:19:20 +01:00
|
|
|
int obj = order ? OSDTYPE_SUB2 : OSDTYPE_SUB;
|
2013-12-24 17:46:14 +01:00
|
|
|
int init_flag = order ? INITIALIZED_SUB2 : INITIALIZED_SUB;
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2013-12-24 17:46:14 +01:00
|
|
|
assert(!(mpctx->initialized_flags & init_flag));
|
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;
|
|
|
|
|
2013-11-23 21:37:15 +01:00
|
|
|
if (!sh->sub->dec_sub) {
|
2013-12-24 17:46:14 +01:00
|
|
|
assert(!mpctx->d_sub[order]);
|
2013-12-21 19:06:37 +01:00
|
|
|
sh->sub->dec_sub = sub_create(mpctx->global);
|
2013-11-23 21:37:15 +01:00
|
|
|
}
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2013-12-24 17:46:14 +01:00
|
|
|
assert(!mpctx->d_sub[order] || sh->sub->dec_sub == mpctx->d_sub[order]);
|
2013-11-23 21:37:15 +01:00
|
|
|
|
|
|
|
// The decoder is kept in the stream header in order to make ordered
|
|
|
|
// chapters work well.
|
2013-12-24 17:46:14 +01:00
|
|
|
mpctx->d_sub[order] = sh->sub->dec_sub;
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2013-12-24 17:46:14 +01:00
|
|
|
mpctx->initialized_flags |= init_flag;
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2013-12-24 17:46:14 +01:00
|
|
|
struct dec_sub *dec_sub = mpctx->d_sub[order];
|
2013-10-29 22:38:29 +01:00
|
|
|
assert(dec_sub);
|
|
|
|
|
2013-12-24 17:46:14 +01:00
|
|
|
reinit_subdec(mpctx, track, dec_sub);
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2014-01-18 01:19:20 +01:00
|
|
|
struct osd_sub_state state = {
|
|
|
|
.dec_sub = dec_sub,
|
|
|
|
// Decides whether to use OSD path or normal subtitle rendering path.
|
|
|
|
.render_bitmap_subs = opts->ass_enabled || !sub_has_get_text(dec_sub),
|
|
|
|
};
|
2013-10-29 22:38:29 +01:00
|
|
|
|
2013-12-24 17:46:14 +01:00
|
|
|
// Secondary subs are rendered with the "text" renderer to transform them
|
|
|
|
// to toptitles.
|
|
|
|
if (order == 1 && sub_has_get_text(dec_sub))
|
2014-01-18 01:19:20 +01:00
|
|
|
state.render_bitmap_subs = false;
|
2013-12-24 17:46:14 +01:00
|
|
|
|
2014-01-18 01:19:20 +01:00
|
|
|
osd_set_sub(mpctx->osd, obj, &state);
|
2013-10-29 22:38:29 +01:00
|
|
|
}
|