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 #define DTS_RESET_THRESHOLD 5.0
54 
reselect_streams(demuxer_t * demuxer)55 static void reselect_streams(demuxer_t *demuxer)
56 {
57     struct priv *p = demuxer->priv;
58     int num_slave = demux_get_num_stream(p->slave);
59     for (int n = 0; n < MPMIN(num_slave, p->num_streams); n++) {
60         if (p->streams[n]) {
61             demuxer_select_track(p->slave, demux_get_stream(p->slave, n),
62                 MP_NOPTS_VALUE, demux_stream_is_selected(p->streams[n]));
63         }
64     }
65 }
66 
get_disc_lang(struct stream * stream,struct sh_stream * sh,bool dvd)67 static void get_disc_lang(struct stream *stream, struct sh_stream *sh, bool dvd)
68 {
69     struct stream_lang_req req = {.type = sh->type, .id = sh->demuxer_id};
70     if (dvd && sh->type == STREAM_SUB)
71         req.id = req.id & 0x1F; // mpeg ID to index
72     stream_control(stream, STREAM_CTRL_GET_LANG, &req);
73     if (req.name[0])
74         sh->lang = talloc_strdup(sh, req.name);
75 }
76 
add_dvd_streams(demuxer_t * demuxer)77 static void add_dvd_streams(demuxer_t *demuxer)
78 {
79     struct priv *p = demuxer->priv;
80     struct stream *stream = demuxer->stream;
81     if (!p->is_dvd)
82         return;
83     struct stream_dvd_info_req info;
84     if (stream_control(stream, STREAM_CTRL_GET_DVD_INFO, &info) > 0) {
85         for (int n = 0; n < MPMIN(32, info.num_subs); n++) {
86             struct sh_stream *sh = demux_alloc_sh_stream(STREAM_SUB);
87             sh->demuxer_id = n + 0x20;
88             sh->codec->codec = "dvd_subtitle";
89             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             p->dvd_subs[n] = sh;
94 
95             // emulate the extradata
96             struct mp_csp_params csp = MP_CSP_PARAMS_DEFAULTS;
97             struct mp_cmat cmatrix;
98             mp_get_csp_matrix(&csp, &cmatrix);
99 
100             char *s = talloc_strdup(sh, "");
101             s = talloc_asprintf_append(s, "palette: ");
102             for (int i = 0; i < 16; i++) {
103                 int color = info.palette[i];
104                 int y[3] = {(color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff};
105                 int c[3];
106                 mp_map_fixp_color(&cmatrix, 8, y, 8, c);
107                 color = (c[2] << 16) | (c[1] << 8) | c[0];
108 
109                 if (i != 0)
110                     s = talloc_asprintf_append(s, ", ");
111                 s = talloc_asprintf_append(s, "%06x", color);
112             }
113             s = talloc_asprintf_append(s, "\n");
114 
115             sh->codec->extradata = s;
116             sh->codec->extradata_size = strlen(s);
117 
118             demux_add_sh_stream(demuxer, sh);
119         }
120     }
121 }
122 
add_streams(demuxer_t * demuxer)123 static void add_streams(demuxer_t *demuxer)
124 {
125     struct priv *p = demuxer->priv;
126 
127     for (int n = p->num_streams; n < demux_get_num_stream(p->slave); n++) {
128         struct sh_stream *src = demux_get_stream(p->slave, n);
129         if (src->type == STREAM_SUB) {
130             struct sh_stream *sub = NULL;
131             if (src->demuxer_id >= 0x20 && src->demuxer_id <= 0x3F)
132                 sub = p->dvd_subs[src->demuxer_id - 0x20];
133             if (sub) {
134                 assert(p->num_streams == n); // directly mapped
135                 MP_TARRAY_APPEND(p, p->streams, p->num_streams, sub);
136                 continue;
137             }
138         }
139         struct sh_stream *sh = demux_alloc_sh_stream(src->type);
140         assert(p->num_streams == n); // directly mapped
141         MP_TARRAY_APPEND(p, p->streams, p->num_streams, sh);
142         // Copy all stream fields that might be relevant
143         *sh->codec = *src->codec;
144         sh->demuxer_id = src->demuxer_id;
145         if (src->type == STREAM_VIDEO) {
146             double ar;
147             if (stream_control(demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar)
148                                 == STREAM_OK)
149             {
150                 struct mp_image_params f = {.w = src->codec->disp_w,
151                                             .h = src->codec->disp_h};
152                 mp_image_params_set_dsize(&f, 1728 * ar, 1728);
153                 sh->codec->par_w = f.p_w;
154                 sh->codec->par_h = f.p_h;
155             }
156         }
157         get_disc_lang(demuxer->stream, sh, p->is_dvd);
158         demux_add_sh_stream(demuxer, sh);
159     }
160     reselect_streams(demuxer);
161 }
162 
d_seek(demuxer_t * demuxer,double seek_pts,int flags)163 static void d_seek(demuxer_t *demuxer, double seek_pts, int flags)
164 {
165     struct priv *p = demuxer->priv;
166 
167     if (p->is_cdda) {
168         demux_seek(p->slave, seek_pts, flags);
169         return;
170     }
171 
172     if (flags & SEEK_FACTOR) {
173         double tmp = 0;
174         stream_control(demuxer->stream, STREAM_CTRL_GET_TIME_LENGTH, &tmp);
175         seek_pts *= tmp;
176     }
177 
178     MP_VERBOSE(demuxer, "seek to: %f\n", seek_pts);
179 
180     // Supposed to induce a seek reset. Does it even work? I don't know.
181     // It will log some bogus error messages, since the demuxer will try a
182     // low level seek, which will obviously not work. But it will probably
183     // clear its internal buffers.
184     demux_seek(p->slave, 0, SEEK_FACTOR | SEEK_FORCE);
185     stream_drop_buffers(demuxer->stream);
186 
187     double seek_arg[] = {seek_pts, flags};
188     stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_TIME, seek_arg);
189 
190     p->seek_reinit = true;
191 }
192 
reset_pts(demuxer_t * demuxer)193 static void reset_pts(demuxer_t *demuxer)
194 {
195     struct priv *p = demuxer->priv;
196 
197     double base;
198     if (stream_control(demuxer->stream, STREAM_CTRL_GET_CURRENT_TIME, &base) < 1)
199         base = 0;
200 
201     MP_VERBOSE(demuxer, "reset to time: %f\n", base);
202 
203     p->base_dts = p->last_dts = MP_NOPTS_VALUE;
204     p->base_time = base;
205     p->seek_reinit = false;
206 }
207 
d_read_packet(struct demuxer * demuxer,struct demux_packet ** out_pkt)208 static bool d_read_packet(struct demuxer *demuxer, struct demux_packet **out_pkt)
209 {
210     struct priv *p = demuxer->priv;
211 
212     struct demux_packet *pkt = demux_read_any_packet(p->slave);
213     if (!pkt)
214         return false;
215 
216     demux_update(p->slave, MP_NOPTS_VALUE);
217 
218     if (p->seek_reinit)
219         reset_pts(demuxer);
220 
221     add_streams(demuxer);
222     if (pkt->stream >= p->num_streams) { // out of memory?
223         talloc_free(pkt);
224         return true;
225     }
226 
227     struct sh_stream *sh = p->streams[pkt->stream];
228     if (!demux_stream_is_selected(sh)) {
229         talloc_free(pkt);
230         return true;
231     }
232 
233     pkt->stream = sh->index;
234 
235     if (p->is_cdda) {
236         *out_pkt = pkt;
237         return true;
238     }
239 
240     MP_TRACE(demuxer, "ipts: %d %f %f\n", sh->type, pkt->pts, pkt->dts);
241 
242     if (sh->type == STREAM_SUB) {
243         if (p->base_dts == MP_NOPTS_VALUE)
244             MP_WARN(demuxer, "subtitle packet along PTS reset\n");
245     } else if (pkt->dts != MP_NOPTS_VALUE) {
246         // Use the very first DTS to rebase the start time of the MPEG stream
247         // to the playback time.
248         if (p->base_dts == MP_NOPTS_VALUE)
249             p->base_dts = pkt->dts;
250 
251         if (p->last_dts == MP_NOPTS_VALUE)
252             p->last_dts = pkt->dts;
253 
254         if (fabs(p->last_dts - pkt->dts) >= DTS_RESET_THRESHOLD) {
255             MP_WARN(demuxer, "PTS discontinuity: %f->%f\n", p->last_dts, pkt->dts);
256             p->base_time += p->last_dts - p->base_dts;
257             p->base_dts = pkt->dts - pkt->duration;
258         }
259         p->last_dts = pkt->dts;
260     }
261 
262     if (p->base_dts != MP_NOPTS_VALUE) {
263         double delta = -p->base_dts + p->base_time;
264         if (pkt->pts != MP_NOPTS_VALUE)
265             pkt->pts += delta;
266         if (pkt->dts != MP_NOPTS_VALUE)
267             pkt->dts += delta;
268     }
269 
270     MP_TRACE(demuxer, "opts: %d %f %f\n", sh->type, pkt->pts, pkt->dts);
271 
272     *out_pkt = pkt;
273     return 1;
274 }
275 
add_stream_chapters(struct demuxer * demuxer)276 static void add_stream_chapters(struct demuxer *demuxer)
277 {
278     int num = 0;
279     if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &num) < 1)
280         return;
281     for (int n = 0; n < num; n++) {
282         double p = n;
283         if (stream_control(demuxer->stream, STREAM_CTRL_GET_CHAPTER_TIME, &p) < 1)
284             continue;
285         demuxer_add_chapter(demuxer, "", p, 0);
286     }
287 }
288 
d_open(demuxer_t * demuxer,enum demux_check check)289 static int d_open(demuxer_t *demuxer, enum demux_check check)
290 {
291     struct priv *p = demuxer->priv = talloc_zero(demuxer, struct priv);
292 
293     if (check != DEMUX_CHECK_FORCE)
294         return -1;
295 
296     struct demuxer_params params = {
297         .force_format = "+lavf",
298         .external_stream = demuxer->stream,
299         .stream_flags = demuxer->stream_origin,
300     };
301 
302     struct stream *cur = demuxer->stream;
303     const char *sname = "";
304     if (cur->info)
305         sname = cur->info->name;
306 
307     p->is_cdda = strcmp(sname, "cdda") == 0;
308     p->is_dvd = strcmp(sname, "dvd") == 0 ||
309                 strcmp(sname, "ifo") == 0 ||
310                 strcmp(sname, "dvdnav") == 0 ||
311                 strcmp(sname, "ifo_dvdnav") == 0;
312 
313     if (p->is_cdda)
314         params.force_format = "+rawaudio";
315 
316     char *t = NULL;
317     stream_control(demuxer->stream, STREAM_CTRL_GET_DISC_NAME, &t);
318     if (t) {
319         mp_tags_set_str(demuxer->metadata, "TITLE", t);
320         talloc_free(t);
321     }
322 
323     // Initialize the playback time. We need to read _some_ data to get the
324     // correct stream-layer time (at least with libdvdnav).
325     stream_read_peek(demuxer->stream, &(char){0}, 1);
326     reset_pts(demuxer);
327 
328     p->slave = demux_open_url("-", &params, demuxer->cancel, demuxer->global);
329     if (!p->slave)
330         return -1;
331 
332     // Can be seekable even if the stream isn't.
333     demuxer->seekable = true;
334 
335     add_dvd_streams(demuxer);
336     add_streams(demuxer);
337     add_stream_chapters(demuxer);
338 
339     double len;
340     if (stream_control(demuxer->stream, STREAM_CTRL_GET_TIME_LENGTH, &len) >= 1)
341         demuxer->duration = len;
342 
343     return 0;
344 }
345 
d_close(demuxer_t * demuxer)346 static void d_close(demuxer_t *demuxer)
347 {
348     struct priv *p = demuxer->priv;
349     demux_free(p->slave);
350 }
351 
352 const demuxer_desc_t demuxer_desc_disc = {
353     .name = "disc",
354     .desc = "CD/DVD/BD wrapper",
355     .read_packet = d_read_packet,
356     .open = d_open,
357     .close = d_close,
358     .seek = d_seek,
359     .switched_tracks = reselect_streams,
360 };
361