Example #1
0
File: sub.c Project: jon-y/mpv
void reinit_subs(struct MPContext *mpctx, int order)
{
    struct MPOpts *opts = mpctx->opts;
    struct track *track = mpctx->current_track[order][STREAM_SUB];
    int obj = order ? OSDTYPE_SUB2 : OSDTYPE_SUB;

    assert(!mpctx->d_sub[order]);

    struct sh_stream *sh = track ? track->stream : NULL;
    if (!sh)
        return;

    // The decoder is cached in the stream header in order to make ordered
    // chapters work better.
    if (!sh->sub->dec_sub)
        sh->sub->dec_sub = sub_create(mpctx->global);
    mpctx->d_sub[order] = sh->sub->dec_sub;

    struct dec_sub *dec_sub = mpctx->d_sub[order];
    reinit_subdec(mpctx, track, dec_sub);

    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),
    };

    // Secondary subs are rendered with the "text" renderer to transform them
    // to toptitles.
    if (order == 1 && sub_has_get_text(dec_sub))
        state.render_bitmap_subs = false;

    if (!mpctx->current_track[0][STREAM_VIDEO])
        state.render_bitmap_subs = false;

    osd_set_sub(mpctx->osd, obj, &state);
}
Example #2
0
void update_osd_sub_state(struct MPContext *mpctx, int order,
                          struct osd_sub_state *out_state)
{
    struct MPOpts *opts = mpctx->opts;
    struct track *track = mpctx->current_track[order][STREAM_SUB];
    struct dec_sub *dec_sub = mpctx->d_sub[order];
    int obj = order ? OSDTYPE_SUB2 : OSDTYPE_SUB;
    bool textsub = dec_sub && sub_has_get_text(dec_sub);

    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 || !textsub,
        .video_offset = get_track_video_offset(mpctx, track),
    };

    // Secondary subs are rendered with the "text" renderer to transform them
    // to toptitles.
    if (order == 1 && textsub)
        state.render_bitmap_subs = false;

    if (!mpctx->current_track[0][STREAM_VIDEO])
        state.render_bitmap_subs = false;

    osd_set_sub(mpctx->osd, obj, &state);
    if (out_state)
        *out_state = state;
}

static void update_subtitle(struct MPContext *mpctx, int order)
{
    struct MPOpts *opts = mpctx->opts;
    struct track *track = mpctx->current_track[order][STREAM_SUB];
    struct dec_sub *dec_sub = mpctx->d_sub[order];

    if (!track || !dec_sub)
        return;

    int obj = order ? OSDTYPE_SUB2 : OSDTYPE_SUB;

    if (mpctx->d_video) {
        struct mp_image_params params = mpctx->d_video->vfilter->override_params;
        if (params.imgfmt)
            sub_control(dec_sub, SD_CTRL_SET_VIDEO_PARAMS, &params);
    }

    struct osd_sub_state state;
    update_osd_sub_state(mpctx, order, &state);

    double refpts_s = mpctx->playback_pts - state.video_offset;
    double curpts_s = refpts_s - opts->sub_delay;

    if (!track->preloaded && track->stream) {
        struct sh_stream *sh_stream = track->stream;
        bool interleaved = is_interleaved(mpctx, track);

        assert(sh_stream->sub->dec_sub == dec_sub);

        while (1) {
            if (interleaved && !demux_has_packet(sh_stream))
                break;
            double subpts_s = demux_get_next_pts(sh_stream);
            if (!demux_has_packet(sh_stream))
                break;
            if (subpts_s > curpts_s) {
                MP_DBG(mpctx, "Sub early: c_pts=%5.3f s_pts=%5.3f\n",
                       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;
            }
            struct demux_packet *pkt = demux_read_packet(sh_stream);
            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);
            sub_decode(dec_sub, pkt);
            talloc_free(pkt);
        }
    }

    // Handle displaying subtitles on terminal; never done for secondary subs
    if (order == 0) {
        if (!state.render_bitmap_subs || !mpctx->video_out)
            set_osd_subtitle(mpctx, sub_get_text(dec_sub, curpts_s));
    } else if (order == 1) {
        osd_set_text(mpctx->osd, obj, sub_get_text(dec_sub, curpts_s));
    }
}

void update_subtitles(struct MPContext *mpctx)
{
    update_subtitle(mpctx, 0);
    update_subtitle(mpctx, 1);
}
Example #3
0
File: sub.c Project: hroncok/mpv
void reinit_subs(struct MPContext *mpctx)
{
    struct MPOpts *opts = mpctx->opts;
    struct track *track = mpctx->current_track[STREAM_SUB];

    assert(!(mpctx->initialized_flags & INITIALIZED_SUB));

    init_demux_stream(mpctx, STREAM_SUB);
    struct sh_stream *sh = mpctx->sh[STREAM_SUB];

    // No track selected, or lazily added DVD track (will actually be created
    // on first sub packet)
    if (!sh)
        return;

    if (!sh->sub->dec_sub) {
        assert(!mpctx->d_sub);
        sh->sub->dec_sub = sub_create(mpctx->global);
    }

    assert(!mpctx->d_sub || sh->sub->dec_sub == mpctx->d_sub);

    // The decoder is kept in the stream header in order to make ordered
    // chapters work well.
    mpctx->d_sub = sh->sub->dec_sub;

    mpctx->initialized_flags |= INITIALIZED_SUB;

    struct dec_sub *dec_sub = mpctx->d_sub;
    assert(dec_sub);

    if (!sub_is_initialized(dec_sub)) {
        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;

        set_dvdsub_fake_extradata(dec_sub, track->demuxer->stream, w, h);
        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, sh);

        // 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, sh);
        }
    }

    mpctx->osd->dec_sub = dec_sub;

    // Decides whether to use OSD path or normal subtitle rendering path.
    mpctx->osd->render_bitmap_subs =
        opts->ass_enabled || !sub_has_get_text(dec_sub);

    reset_subtitles(mpctx);
}