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 #ifndef MPLAYER_DEMUXER_H
19 #define MPLAYER_DEMUXER_H
20 
21 #include <sys/types.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdbool.h>
26 
27 #include "misc/bstr.h"
28 #include "common/common.h"
29 #include "common/tags.h"
30 #include "packet.h"
31 #include "stheader.h"
32 
33 #define MAX_SEEK_RANGES 10
34 
35 struct demux_seek_range {
36     double start, end;
37 };
38 
39 struct demux_reader_state {
40     bool eof, underrun, idle;
41     bool bof_cached, eof_cached;
42     double ts_duration;
43     double ts_reader; // approx. timerstamp of decoder position
44     double ts_end; // approx. timestamp of end of buffered range
45     int64_t total_bytes;
46     int64_t fw_bytes;
47     int64_t file_cache_bytes;
48     double seeking; // current low level seek target, or NOPTS
49     int low_level_seeks; // number of started low level seeks
50     uint64_t byte_level_seeks; // number of byte stream level seeks
51     double ts_last; // approx. timestamp of demuxer position
52     uint64_t bytes_per_second; // low level statistics
53     // Positions that can be seeked to without incurring the latency of a low
54     // level seek.
55     int num_seek_ranges;
56     struct demux_seek_range seek_ranges[MAX_SEEK_RANGES];
57 };
58 
59 #define SEEK_FACTOR   (1 << 1)      // argument is in range [0,1]
60 #define SEEK_FORWARD  (1 << 2)      // prefer later time if not exact
61                                     // (if unset, prefer earlier time)
62 #define SEEK_CACHED   (1 << 3)      // allow packet cache seeks only
63 #define SEEK_SATAN    (1 << 4)      // enable backward demuxing
64 #define SEEK_HR       (1 << 5)      // hr-seek (this is a weak hint only)
65 #define SEEK_FORCE    (1 << 6)      // ignore unseekable flag
66 #define SEEK_BLOCK    (1 << 7)      // upon successfully queued seek, block readers
67                                     // (simplifies syncing multiple reader threads)
68 
69 // Strictness of the demuxer open format check.
70 // demux.c will try by default: NORMAL, UNSAFE (in this order)
71 // Using "-demuxer format" will try REQUEST
72 // Using "-demuxer +format" will try FORCE
73 // REQUEST can be used as special value for raw demuxers which have no file
74 // header check; then they should fail if check!=FORCE && check!=REQUEST.
75 //
76 // In general, the list is sorted from weakest check to normal check.
77 // You can use relation operators to compare the check level.
78 enum demux_check {
79     DEMUX_CHECK_FORCE,  // force format if possible
80     DEMUX_CHECK_UNSAFE, // risky/fuzzy detection
81     DEMUX_CHECK_REQUEST,// requested by user or stream implementation
82     DEMUX_CHECK_NORMAL, // normal, safe detection
83 };
84 
85 enum demux_event {
86     DEMUX_EVENT_INIT = 1 << 0,      // complete (re-)initialization
87     DEMUX_EVENT_STREAMS = 1 << 1,   // a stream was added
88     DEMUX_EVENT_METADATA = 1 << 2,  // metadata or stream_metadata changed
89     DEMUX_EVENT_DURATION = 1 << 3,  // duration updated
90     DEMUX_EVENT_ALL = 0xFFFF,
91 };
92 
93 struct demuxer;
94 struct timeline;
95 
96 /**
97  * Demuxer description structure
98  */
99 typedef struct demuxer_desc {
100     const char *name;      // Demuxer name, used with -demuxer switch
101     const char *desc;      // Displayed to user
102 
103     // If non-NULL, these are added to the global option list.
104     const struct m_sub_options *options;
105 
106     // Return 0 on success, otherwise -1
107     int (*open)(struct demuxer *demuxer, enum demux_check check);
108     // The following functions are all optional
109     // Try to read a packet. Return false on EOF. If true is returned, the
110     // demuxer may set *pkt to a new packet (the reference goes to the caller).
111     // If *pkt is NULL (the value when this function is called), the call
112     // will be repeated.
113     bool (*read_packet)(struct demuxer *demuxer, struct demux_packet **pkt);
114     void (*close)(struct demuxer *demuxer);
115     void (*seek)(struct demuxer *demuxer, double rel_seek_secs, int flags);
116     void (*switched_tracks)(struct demuxer *demuxer);
117     // See timeline.c
118     void (*load_timeline)(struct timeline *tl);
119 } demuxer_desc_t;
120 
121 typedef struct demux_chapter
122 {
123     int original_index;
124     double pts;
125     struct mp_tags *metadata;
126     uint64_t demuxer_id; // for mapping to internal demuxer data structures
127 } demux_chapter_t;
128 
129 struct demux_edition {
130     uint64_t demuxer_id;
131     bool default_edition;
132     struct mp_tags *metadata;
133 };
134 
135 struct matroska_segment_uid {
136     unsigned char segment[16];
137     uint64_t edition;
138 };
139 
140 struct matroska_data {
141     struct matroska_segment_uid uid;
142     // Ordered chapter information if any
143     struct matroska_chapter {
144         uint64_t start;
145         uint64_t end;
146         bool has_segment_uid;
147         struct matroska_segment_uid uid;
148         char *name;
149     } *ordered_chapters;
150     int num_ordered_chapters;
151 };
152 
153 struct replaygain_data {
154     float track_gain;
155     float track_peak;
156     float album_gain;
157     float album_peak;
158 };
159 
160 typedef struct demux_attachment
161 {
162     char *name;
163     char *type;
164     void *data;
165     unsigned int data_size;
166 } demux_attachment_t;
167 
168 struct demuxer_params {
169     bool is_top_level; // if true, it's not a sub-demuxer (enables cache etc.)
170     char *force_format;
171     int matroska_num_wanted_uids;
172     struct matroska_segment_uid *matroska_wanted_uids;
173     int matroska_wanted_segment;
174     bool *matroska_was_valid;
175     struct timeline *timeline;
176     bool disable_timeline;
177     bstr init_fragment;
178     bool skip_lavf_probing;
179     bool stream_record; // if true, enable stream recording if option is set
180     int stream_flags;
181     struct stream *external_stream; // if set, use this, don't open or close streams
182     // result
183     bool demuxer_failed;
184 };
185 
186 typedef struct demuxer {
187     const demuxer_desc_t *desc; ///< Demuxer description structure
188     const char *filetype; // format name when not identified by demuxer (libavformat)
189     int64_t filepos;  // input stream current pos.
190     int64_t filesize;
191     char *filename;  // same as stream->url
192     bool seekable;
193     bool partially_seekable; // true if _maybe_ seekable; implies seekable=true
194     double start_time;
195     double duration;  // -1 if unknown
196     // File format allows PTS resets (even if the current file is without)
197     bool ts_resets_possible;
198     // The file data was fully read, and there is no need to keep the stream
199     // open, keep the cache active, or to run the demuxer thread. Generating
200     // packets is not slow either (unlike e.g. libavdevice pseudo-demuxers).
201     // Typical examples: text subtitles, playlists
202     bool fully_read;
203     bool is_network; // opened directly from a network stream
204     bool is_streaming; // implies a "slow" input, such as network or FUSE
205     int stream_origin; // any STREAM_ORIGIN_* (set from source stream)
206     bool access_references; // allow opening other files/URLs
207 
208     // Bitmask of DEMUX_EVENT_*
209     int events;
210 
211     struct demux_edition *editions;
212     int num_editions;
213     int edition;
214 
215     struct demux_chapter *chapters;
216     int num_chapters;
217 
218     struct demux_attachment *attachments;
219     int num_attachments;
220 
221     struct matroska_data matroska_data;
222 
223     // If the file is a playlist file
224     struct playlist *playlist;
225 
226     struct mp_tags *metadata;
227 
228     void *priv;   // demuxer-specific internal data
229     struct mpv_global *global;
230     struct mp_log *log, *glog;
231     struct demuxer_params *params;
232 
233     // internal to demux.c
234     struct demux_internal *in;
235 
236     // Triggered when ending demuxing forcefully. Usually bound to the stream too.
237     struct mp_cancel *cancel;
238 
239     // Since the demuxer can run in its own thread, and the stream is not
240     // thread-safe, only the demuxer is allowed to access the stream directly.
241     // Also note that the stream can get replaced if fully_read is set.
242     struct stream *stream;
243 } demuxer_t;
244 
245 void demux_free(struct demuxer *demuxer);
246 void demux_cancel_and_free(struct demuxer *demuxer);
247 
248 struct demux_free_async_state;
249 struct demux_free_async_state *demux_free_async(struct demuxer *demuxer);
250 void demux_free_async_force(struct demux_free_async_state *state);
251 bool demux_free_async_finish(struct demux_free_async_state *state);
252 
253 void demuxer_feed_caption(struct sh_stream *stream, demux_packet_t *dp);
254 
255 int demux_read_packet_async(struct sh_stream *sh, struct demux_packet **out_pkt);
256 int demux_read_packet_async_until(struct sh_stream *sh, double min_pts,
257                                   struct demux_packet **out_pkt);
258 bool demux_stream_is_selected(struct sh_stream *stream);
259 void demux_set_stream_wakeup_cb(struct sh_stream *sh,
260                                 void (*cb)(void *ctx), void *ctx);
261 struct demux_packet *demux_read_any_packet(struct demuxer *demuxer);
262 
263 struct sh_stream *demux_get_stream(struct demuxer *demuxer, int index);
264 int demux_get_num_stream(struct demuxer *demuxer);
265 
266 struct sh_stream *demux_alloc_sh_stream(enum stream_type type);
267 void demux_add_sh_stream(struct demuxer *demuxer, struct sh_stream *sh);
268 
269 struct mp_cancel;
270 struct demuxer *demux_open_url(const char *url,
271                                struct demuxer_params *params,
272                                struct mp_cancel *cancel,
273                                struct mpv_global *global);
274 
275 void demux_start_thread(struct demuxer *demuxer);
276 void demux_stop_thread(struct demuxer *demuxer);
277 void demux_set_wakeup_cb(struct demuxer *demuxer, void (*cb)(void *ctx), void *ctx);
278 void demux_start_prefetch(struct demuxer *demuxer);
279 
280 bool demux_cancel_test(struct demuxer *demuxer);
281 
282 void demux_flush(struct demuxer *demuxer);
283 int demux_seek(struct demuxer *demuxer, double rel_seek_secs, int flags);
284 void demux_set_ts_offset(struct demuxer *demuxer, double offset);
285 
286 void demux_get_bitrate_stats(struct demuxer *demuxer, double *rates);
287 void demux_get_reader_state(struct demuxer *demuxer, struct demux_reader_state *r);
288 
289 void demux_block_reading(struct demuxer *demuxer, bool block);
290 
291 void demuxer_select_track(struct demuxer *demuxer, struct sh_stream *stream,
292                           double ref_pts, bool selected);
293 void demuxer_refresh_track(struct demuxer *demuxer, struct sh_stream *stream,
294                            double ref_pts);
295 
296 int demuxer_help(struct mp_log *log, const m_option_t *opt, struct bstr name);
297 
298 int demuxer_add_attachment(struct demuxer *demuxer, char *name,
299                            char *type, void *data, size_t data_size);
300 int demuxer_add_chapter(demuxer_t *demuxer, char *name,
301                         double pts, uint64_t demuxer_id);
302 void demux_stream_tags_changed(struct demuxer *demuxer, struct sh_stream *sh,
303                                struct mp_tags *tags, double pts);
304 void demux_close_stream(struct demuxer *demuxer);
305 
306 void demux_metadata_changed(demuxer_t *demuxer);
307 void demux_update(demuxer_t *demuxer, double playback_pts);
308 
309 bool demux_cache_dump_set(struct demuxer *demuxer, double start, double end,
310                           char *file);
311 int demux_cache_dump_get_status(struct demuxer *demuxer);
312 
313 double demux_probe_cache_dump_target(struct demuxer *demuxer, double pts,
314                                      bool for_end);
315 
316 bool demux_is_network_cached(demuxer_t *demuxer);
317 
318 void demux_report_unbuffered_read_bytes(struct demuxer *demuxer, int64_t new);
319 int64_t demux_get_bytes_read_hack(struct demuxer *demuxer);
320 
321 struct sh_stream *demuxer_stream_by_demuxer_id(struct demuxer *d,
322                                                enum stream_type t, int id);
323 
324 struct demux_chapter *demux_copy_chapter_data(struct demux_chapter *c, int num);
325 
326 bool demux_matroska_uid_cmp(struct matroska_segment_uid *a,
327                             struct matroska_segment_uid *b);
328 
329 const char *stream_type_name(enum stream_type type);
330 
331 #endif /* MPLAYER_DEMUXER_H */
332