Coverage Report

Created: 2025-08-11 06:44

/src/mpv/demux/demux_disc.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * This file is part of mpv.
3
 *
4
 * mpv is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * mpv is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
#include <string.h>
19
#include <math.h>
20
#include <assert.h>
21
22
#include "common/common.h"
23
#include "common/msg.h"
24
25
#include "stream/stream.h"
26
#include "video/mp_image.h"
27
#include "demux.h"
28
#include "stheader.h"
29
30
#include "video/csputils.h"
31
32
struct priv {
33
    struct demuxer *slave;
34
    // streams[slave_stream_index] == our_stream
35
    struct sh_stream **streams;
36
    int num_streams;
37
    // This contains each DVD sub stream, or NULL. Needed because DVD packets
38
    // can come arbitrarily late in the MPEG stream, so the slave demuxer
39
    // might add the streams only later.
40
    struct sh_stream *dvd_subs[32];
41
    // Used to rewrite the raw MPEG timestamps to playback time.
42
    double base_time;   // playback display start time of current segment
43
    double base_dts;    // packet DTS that maps to base_time
44
    double last_dts;    // DTS of previously demuxed packet
45
    bool seek_reinit;   // needs reinit after seek
46
47
    bool is_dvd, is_cdda;
48
};
49
50
// If the timestamp difference between subsequent packets is this big, assume
51
// a reset. It should be big enough to account for 1. low video framerates and
52
// large audio frames, and 2. bad interleaving.
53
0
#define DTS_RESET_THRESHOLD 5.0
54
55
static void reselect_streams(demuxer_t *demuxer)
56
0
{
57
0
    struct priv *p = demuxer->priv;
58
0
    int num_slave = demux_get_num_stream(p->slave);
59
0
    for (int n = 0; n < MPMIN(num_slave, p->num_streams); n++) {
60
0
        if (p->streams[n]) {
61
0
            demuxer_select_track(p->slave, demux_get_stream(p->slave, n),
62
0
                MP_NOPTS_VALUE, demux_stream_is_selected(p->streams[n]));
63
0
        }
64
0
    }
65
0
}
66
67
static void get_disc_lang(struct stream *stream, struct sh_stream *sh, bool dvd)
68
0
{
69
0
    struct stream_lang_req req = {.type = sh->type, .id = sh->demuxer_id};
70
0
    if (dvd && sh->type == STREAM_SUB)
71
0
        req.id = req.id & 0x1F; // mpeg ID to index
72
0
    stream_control(stream, STREAM_CTRL_GET_LANG, &req);
73
0
    if (req.name[0])
74
0
        sh->lang = talloc_strdup(sh, req.name);
75
0
}
76
77
static void add_dvd_streams(demuxer_t *demuxer)
78
0
{
79
0
    struct priv *p = demuxer->priv;
80
0
    struct stream *stream = demuxer->stream;
81
0
    if (!p->is_dvd)
82
0
        return;
83
0
    struct stream_dvd_info_req info;
84
0
    if (stream_control(stream, STREAM_CTRL_GET_DVD_INFO, &info) > 0) {
85
0
        for (int n = 0; n < MPMIN(32, info.num_subs); n++) {
86
0
            struct sh_stream *sh = demux_alloc_sh_stream(STREAM_SUB);
87
0
            sh->demuxer_id = n + 0x20;
88
0
            sh->codec->codec = "dvd_subtitle";
89
0
            get_disc_lang(stream, sh, true);
90
            // p->streams _must_ match with p->slave->streams, so we can't add
91
            // it yet - it has to be done when the real stream appears, which
92
            // could be right on start, or any time later.
93
0
            p->dvd_subs[n] = sh;
94
95
            // emulate the extradata
96
0
            struct mp_csp_params csp = MP_CSP_PARAMS_DEFAULTS;
97
0
            struct pl_transform3x3 cmatrix;
98
0
            mp_get_csp_matrix(&csp, &cmatrix);
99
100
0
            char *s = talloc_strdup(sh, "");
101
0
            s = talloc_asprintf_append(s, "palette: ");
102
0
            for (int i = 0; i < 16; i++) {
103
0
                int color = info.palette[i];
104
0
                int y[3] = {(color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff};
105
0
                int c[3];
106
0
                mp_map_fixp_color(&cmatrix, 8, y, 8, c);
107
0
                color = (c[2] << 16) | (c[1] << 8) | c[0];
108
109
0
                if (i != 0)
110
0
                    s = talloc_asprintf_append(s, ", ");
111
0
                s = talloc_asprintf_append(s, "%06x", color);
112
0
            }
113
0
            s = talloc_asprintf_append(s, "\n");
114
115
0
            sh->codec->extradata = s;
116
0
            sh->codec->extradata_size = strlen(s);
117
118
0
            demux_add_sh_stream(demuxer, sh);
119
0
        }
120
0
    }
121
0
}
122
123
static void add_streams(demuxer_t *demuxer)
124
0
{
125
0
    struct priv *p = demuxer->priv;
126
127
0
    for (int n = p->num_streams; n < demux_get_num_stream(p->slave); n++) {
128
0
        struct sh_stream *src = demux_get_stream(p->slave, n);
129
0
        if (src->type == STREAM_SUB) {
130
0
            struct sh_stream *sub = NULL;
131
0
            if (src->demuxer_id >= 0x20 && src->demuxer_id <= 0x3F)
132
0
                sub = p->dvd_subs[src->demuxer_id - 0x20];
133
0
            if (sub) {
134
0
                mp_assert(p->num_streams == n); // directly mapped
135
0
                MP_TARRAY_APPEND(p, p->streams, p->num_streams, sub);
136
0
                continue;
137
0
            }
138
0
        }
139
0
        struct sh_stream *sh = demux_alloc_sh_stream(src->type);
140
0
        mp_assert(p->num_streams == n); // directly mapped
141
0
        MP_TARRAY_APPEND(p, p->streams, p->num_streams, sh);
142
        // Copy all stream fields that might be relevant
143
0
        *sh->codec = *src->codec;
144
0
        sh->demuxer_id = src->demuxer_id;
145
0
        if (src->type == STREAM_VIDEO) {
146
0
            double ar;
147
0
            if (stream_control(demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar)
148
0
                                == STREAM_OK)
149
0
            {
150
0
                struct mp_image_params f = {.w = src->codec->disp_w,
151
0
                                            .h = src->codec->disp_h};
152
0
                mp_image_params_set_dsize(&f, 1728 * ar, 1728);
153
0
                sh->codec->par_w = f.p_w;
154
0
                sh->codec->par_h = f.p_h;
155
0
            }
156
0
        }
157
0
        get_disc_lang(demuxer->stream, sh, p->is_dvd);
158
0
        demux_add_sh_stream(demuxer, sh);
159
0
    }
160
0
    reselect_streams(demuxer);
161
0
}
162
163
static void d_seek(demuxer_t *demuxer, double seek_pts, int flags)
164
0
{
165
0
    struct priv *p = demuxer->priv;
166
167
0
    if (p->is_cdda) {
168
0
        demux_seek(p->slave, seek_pts, flags);
169
0
        return;
170
0
    }
171
172
0
    if (flags & SEEK_FACTOR) {
173
0
        double tmp = 0;
174
0
        stream_control(demuxer->stream, STREAM_CTRL_GET_TIME_LENGTH, &tmp);
175
0
        seek_pts *= tmp;
176
0
    }
177
178
0
    MP_VERBOSE(demuxer, "seek to: %f\n", seek_pts);
179
180
0
    double seek_arg[] = {seek_pts, flags};
181
0
    stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_TIME, seek_arg);
182
183
0
    if (p->slave->desc->drop_buffers)
184
0
        p->slave->desc->drop_buffers(p->slave);
185
186
0
    p->seek_reinit = true;
187
0
}
188
189
static void reset_pts(demuxer_t *demuxer)
190
0
{
191
0
    struct priv *p = demuxer->priv;
192
193
0
    double base;
194
0
    if (stream_control(demuxer->stream, STREAM_CTRL_GET_CURRENT_TIME, &base) < 1)
195
0
        base = 0;
196
197
0
    MP_VERBOSE(demuxer, "reset to time: %f\n", base);
198
199
0
    p->base_dts = p->last_dts = MP_NOPTS_VALUE;
200
0
    p->base_time = base;
201
0
    p->seek_reinit = false;
202
0
}
203
204
static bool d_read_packet(struct demuxer *demuxer, struct demux_packet **out_pkt)
205
0
{
206
0
    struct priv *p = demuxer->priv;
207
208
0
    struct demux_packet *pkt = demux_read_any_packet(p->slave);
209
0
    if (!pkt)
210
0
        return false;
211
212
0
    demux_update(p->slave, MP_NOPTS_VALUE);
213
214
0
    if (p->seek_reinit)
215
0
        reset_pts(demuxer);
216
217
0
    add_streams(demuxer);
218
0
    if (pkt->stream >= p->num_streams) { // out of memory?
219
0
        talloc_free(pkt);
220
0
        return true;
221
0
    }
222
223
0
    struct sh_stream *sh = p->streams[pkt->stream];
224
0
    if (!demux_stream_is_selected(sh)) {
225
0
        talloc_free(pkt);
226
0
        return true;
227
0
    }
228
229
0
    pkt->stream = sh->index;
230
231
0
    if (p->is_cdda) {
232
0
        *out_pkt = pkt;
233
0
        return true;
234
0
    }
235
236
0
    MP_TRACE(demuxer, "ipts: %d %f %f\n", sh->type, pkt->pts, pkt->dts);
237
238
0
    if (sh->type == STREAM_SUB) {
239
0
        if (p->base_dts == MP_NOPTS_VALUE)
240
0
            MP_WARN(demuxer, "subtitle packet along PTS reset\n");
241
0
    } else if (pkt->dts != MP_NOPTS_VALUE) {
242
        // Use the very first DTS to rebase the start time of the MPEG stream
243
        // to the playback time.
244
0
        if (p->base_dts == MP_NOPTS_VALUE)
245
0
            p->base_dts = pkt->dts;
246
247
0
        if (p->last_dts == MP_NOPTS_VALUE)
248
0
            p->last_dts = pkt->dts;
249
250
0
        if (fabs(p->last_dts - pkt->dts) >= DTS_RESET_THRESHOLD) {
251
0
            MP_WARN(demuxer, "PTS discontinuity: %f->%f\n", p->last_dts, pkt->dts);
252
0
            p->base_time += p->last_dts - p->base_dts;
253
0
            p->base_dts = pkt->dts - pkt->duration;
254
0
        }
255
0
        p->last_dts = pkt->dts;
256
0
    }
257
258
0
    if (p->base_dts != MP_NOPTS_VALUE) {
259
0
        double delta = -p->base_dts + p->base_time;
260
0
        if (pkt->pts != MP_NOPTS_VALUE)
261
0
            pkt->pts += delta;
262
0
        if (pkt->dts != MP_NOPTS_VALUE)
263
0
            pkt->dts += delta;
264
0
    }
265
266
0
    MP_TRACE(demuxer, "opts: %d %f %f\n", sh->type, pkt->pts, pkt->dts);
267
268
0
    *out_pkt = pkt;
269
0
    return 1;
270
0
}
271
272
static void add_stream_editions(struct demuxer *demuxer)
273
0
{
274
0
    unsigned titles = 0;
275
0
    if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_TITLES, &titles) != STREAM_OK)
276
0
        return;
277
0
    for (unsigned title = 0; title < titles; ++title) {
278
0
        double duration = title;
279
0
        if (stream_control(demuxer->stream, STREAM_CTRL_GET_TITLE_LENGTH, &duration) != STREAM_OK)
280
0
            continue;
281
282
0
        struct demux_edition new = {
283
0
            .demuxer_id = title,
284
0
            .default_edition = false,
285
0
            .metadata = talloc_zero(demuxer, struct mp_tags),
286
0
        };
287
0
        MP_TARRAY_APPEND(demuxer, demuxer->editions, demuxer->num_editions, new);
288
289
0
        char *time = mp_format_time(duration, true);
290
0
        double playlist = title;
291
0
        if (stream_control(demuxer->stream, STREAM_CTRL_GET_TITLE_PLAYLIST, &playlist) == STREAM_OK)
292
0
            time = talloc_asprintf_append(time, ") (%05.0f.mpls", playlist);
293
0
        mp_tags_set_str(new.metadata, "TITLE",
294
0
                        mp_tprintf(42, "title: %u (%s)", title + 1, time));
295
0
        talloc_free(time);
296
0
    }
297
0
}
298
299
static void add_stream_chapters(struct demuxer *demuxer)
300
0
{
301
0
    int num = 0;
302
0
    if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &num) < 1)
303
0
        return;
304
0
    for (int n = 0; n < num; n++) {
305
0
        double p = n;
306
0
        if (stream_control(demuxer->stream, STREAM_CTRL_GET_CHAPTER_TIME, &p) < 1)
307
0
            continue;
308
0
        demuxer_add_chapter(demuxer, "", p, 0);
309
0
    }
310
0
}
311
312
static int d_open(demuxer_t *demuxer, enum demux_check check)
313
155k
{
314
155k
    struct priv *p = demuxer->priv = talloc_zero(demuxer, struct priv);
315
316
155k
    if (check != DEMUX_CHECK_FORCE)
317
155k
        return -1;
318
319
0
    struct demuxer_params params = {
320
0
        .force_format = "+lavf",
321
0
        .external_stream = demuxer->stream,
322
0
        .stream_flags = demuxer->stream_origin,
323
0
        .depth = demuxer->depth + 1,
324
0
    };
325
326
0
    struct stream *cur = demuxer->stream;
327
0
    const char *sname = "";
328
0
    if (cur->info)
329
0
        sname = cur->info->name;
330
331
0
    p->is_cdda = strcmp(sname, "cdda") == 0;
332
0
    p->is_dvd = strcmp(sname, "dvdnav") == 0 ||
333
0
                strcmp(sname, "ifo_dvdnav") == 0;
334
335
0
    if (p->is_cdda)
336
0
        params.force_format = "+rawaudio";
337
338
0
    char *t = NULL;
339
0
    stream_control(demuxer->stream, STREAM_CTRL_GET_DISC_NAME, &t);
340
0
    if (t) {
341
0
        mp_tags_set_str(demuxer->metadata, "TITLE", t);
342
0
        talloc_free(t);
343
0
    }
344
345
    // Initialize the playback time. We need to read _some_ data to get the
346
    // correct stream-layer time (at least with libdvdnav).
347
0
    stream_read_peek(demuxer->stream, &(char){0}, 1);
348
0
    reset_pts(demuxer);
349
350
0
    p->slave = demux_open_url("-", &params, demuxer->cancel, demuxer->global);
351
0
    if (!p->slave)
352
0
        return -1;
353
354
    // Can be seekable even if the stream isn't.
355
0
    demuxer->seekable = true;
356
357
0
    add_dvd_streams(demuxer);
358
0
    add_streams(demuxer);
359
0
    add_stream_chapters(demuxer);
360
0
    add_stream_editions(demuxer);
361
362
0
    double len;
363
0
    if (stream_control(demuxer->stream, STREAM_CTRL_GET_TIME_LENGTH, &len) >= 1)
364
0
        demuxer->duration = len;
365
366
0
    unsigned title;
367
0
    if (stream_control(demuxer->stream, STREAM_CTRL_GET_CURRENT_TITLE, &title) >= 1)
368
0
        demuxer->edition = title;
369
370
0
    return 0;
371
0
}
372
373
static void d_close(demuxer_t *demuxer)
374
155k
{
375
155k
    struct priv *p = demuxer->priv;
376
155k
    demux_free(p->slave);
377
155k
}
378
379
const demuxer_desc_t demuxer_desc_disc = {
380
    .name = "disc",
381
    .desc = "CD/DVD/BD wrapper",
382
    .read_packet = d_read_packet,
383
    .open = d_open,
384
    .close = d_close,
385
    .seek = d_seek,
386
    .switched_tracks = reselect_streams,
387
};