summaryrefslogtreecommitdiffstats
path: root/sub/dec_sub.c
blob: 67828921b47a9cc17783e517e5e73554f3e8953b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * 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 <stdlib.h>
#include <stdbool.h>
#include <assert.h>

#include "config.h"
#include "demux/stheader.h"
#include "sub/sd.h"
#include "sub/sub.h"
#include "sub/dec_sub.h"
#include "core/options.h"

extern const struct sd_functions sd_ass;
extern const struct sd_functions sd_lavc;
extern const struct sd_functions sd_spu;

static const struct sd_functions *sd_list[] = {
#ifdef CONFIG_ASS
    &sd_ass,
#endif
    &sd_lavc,
    &sd_spu,
    NULL
};

struct dec_sub {
    struct MPOpts *opts;
    struct sd init_sd;

    struct sd *sd;
};

struct dec_sub *sub_create(struct MPOpts *opts)
{
    struct dec_sub *sub = talloc_zero(NULL, struct dec_sub);
    sub->opts = opts;
    return sub;
}

void sub_destroy(struct dec_sub *sub)
{
    if (!sub)
        return;
    if (sub->sd && sub->sd->driver->uninit)
        sub->sd->driver->uninit(sub->sd);
    talloc_free(sub->sd);
    talloc_free(sub);
}

bool sub_is_initialized(struct dec_sub *sub)
{
    return !!sub->sd;
}

struct sd *sub_get_sd(struct dec_sub *sub)
{
    return sub->sd;
}

void sub_set_video_res(struct dec_sub *sub, int w, int h)
{
    sub->init_sd.sub_video_w = w;
    sub->init_sd.sub_video_h = h;
}

void sub_set_extradata(struct dec_sub *sub, void *data, int data_len)
{
    sub->init_sd.extradata = data_len ? talloc_memdup(sub, data, data_len) : NULL;
    sub->init_sd.extradata_len = data_len;
}

void sub_set_ass_renderer(struct dec_sub *sub, struct ass_library *ass_library,
                          struct ass_renderer *ass_renderer)
{
    sub->init_sd.ass_library = ass_library;
    sub->init_sd.ass_renderer = ass_renderer;
}

static int sub_init_decoder(struct dec_sub *sub, struct sd *sd)
{
    sd->driver = NULL;
    for (int n = 0; sd_list[n]; n++) {
        if (sd_list[n]->supports_format(sd->codec)) {
            sd->driver = sd_list[n];
            break;
        }
    }

    if (!sd->driver)
        return -1;

    if (sd->driver->init(sd) < 0)
        return -1;

    return 0;
}

void sub_init_from_sh(struct dec_sub *sub, struct sh_sub *sh)
{
    assert(!sub->sd);
    if (sh->extradata && !sub->init_sd.extradata)
        sub_set_extradata(sub, sh->extradata, sh->extradata_len);
    struct sd *sd = talloc(NULL, struct sd);
    *sd = sub->init_sd;
    sd->opts = sub->opts;
    sd->codec = sh->gsh->codec;
    sd->ass_track = sh->track;
    if (sub_init_decoder(sub, sd) < 0) {
        talloc_free(sd);
        sd = NULL;
    }
    sub->sd = sd;
}

bool sub_accept_packets_in_advance(struct dec_sub *sub)
{
    return sub->sd && sub->sd->driver->accept_packets_in_advance;
}

void sub_decode(struct dec_sub *sub, struct demux_packet *packet)
{
    if (sub->sd)
        sub->sd->driver->decode(sub->sd, packet);
}

void sub_get_bitmaps(struct dec_sub *sub, struct mp_osd_res dim, double pts,
                     struct sub_bitmaps *res)
{
    struct MPOpts *opts = sub->opts;

    *res = (struct sub_bitmaps) {0};
    if (sub->sd && opts->sub_visibility) {
        if (sub->sd->driver->get_bitmaps)
            sub->sd->driver->get_bitmaps(sub->sd, dim, pts, res);
    }
}

bool sub_has_get_text(struct dec_sub *sub)
{
    return sub->sd && sub->sd->driver->get_text;
}

char *sub_get_text(struct dec_sub *sub, double pts)
{
    struct MPOpts *opts = sub->opts;
    char *text = NULL;
    if (sub->sd && opts->sub_visibility) {
        if (sub->sd->driver->get_text)
            text = sub->sd->driver->get_text(sub->sd, pts);
    }
    return text;
}

void sub_reset(struct dec_sub *sub)
{
    if (sub->sd && sub->sd->driver->reset)
        sub->sd->driver->reset(sub->sd);
}