diff options
Diffstat (limited to 'demux/demux.c')
-rw-r--r-- | demux/demux.c | 251 |
1 files changed, 156 insertions, 95 deletions
diff --git a/demux/demux.c b/demux/demux.c index dcad692be5..19ad6970b0 100644 --- a/demux/demux.c +++ b/demux/demux.c @@ -29,7 +29,7 @@ #include "config.h" #include "options/options.h" -#include "talloc.h" +#include "mpv_talloc.h" #include "common/msg.h" #include "common/global.h" #include "osdep/threads.h" @@ -103,6 +103,11 @@ struct demux_internal { void (*wakeup_cb)(void *ctx); void *wakeup_cb_ctx; + struct sh_stream **streams; + int num_streams; + + int events; + bool warned_queue_overflow; bool last_eof; // last actual global EOF status bool eof; // whether we're in EOF state (reset for retry) @@ -198,42 +203,84 @@ void demux_set_ts_offset(struct demuxer *demuxer, double offset) pthread_mutex_unlock(&in->lock); } -struct sh_stream *new_sh_stream(demuxer_t *demuxer, enum stream_type type) +// Allocate a new sh_stream of the given type. It either has to be released +// with talloc_free(), or added to a demuxer with demux_add_sh_stream(). You +// cannot add or read packets from the stream before it has been added. +struct sh_stream *demux_alloc_sh_stream(enum stream_type type) { - assert(demuxer == demuxer->in->d_thread); - - if (demuxer->num_streams > MAX_SH_STREAMS) { - MP_WARN(demuxer, "Too many streams.\n"); - return NULL; - } - - int demuxer_id = 0; - for (int n = 0; n < demuxer->num_streams; n++) { - if (demuxer->streams[n]->type == type) - demuxer_id++; - } - - struct sh_stream *sh = talloc_ptrtype(demuxer, sh); + struct sh_stream *sh = talloc_ptrtype(NULL, sh); *sh = (struct sh_stream) { .type = type, - .index = demuxer->num_streams, - .ff_index = demuxer->num_streams, - .demuxer_id = demuxer_id, // may be overwritten by demuxer - .ds = talloc(sh, struct demux_stream), + .index = -1, + .ff_index = -1, // may be overwritten by demuxer + .demuxer_id = -1, // ... same + .codec = talloc_zero(sh, struct mp_codec_params), }; + sh->codec->type = type; + return sh; +} + +// Add a new sh_stream to the demuxer. Note that as soon as the stream has been +// added, it must be immutable, and must not be released (this will happen when +// the demuxer is destroyed). +void demux_add_sh_stream(struct demuxer *demuxer, struct sh_stream *sh) +{ + struct demux_internal *in = demuxer->in; + pthread_mutex_lock(&in->lock); + + assert(!sh->ds); // must not be added yet + + sh->ds = talloc(sh, struct demux_stream); *sh->ds = (struct demux_stream) { - .in = demuxer->in, + .in = in, .type = sh->type, - .selected = demuxer->in->autoselect, + .selected = in->autoselect, }; - MP_TARRAY_APPEND(demuxer, demuxer->streams, demuxer->num_streams, sh); - switch (sh->type) { - case STREAM_VIDEO: sh->video = talloc_zero(demuxer, struct sh_video); break; - case STREAM_AUDIO: sh->audio = talloc_zero(demuxer, struct sh_audio); break; - case STREAM_SUB: sh->sub = talloc_zero(demuxer, struct sh_sub); break; + + if (!sh->codec->codec) + sh->codec->codec = ""; + + sh->index = in->num_streams; + if (sh->ff_index < 0) + sh->ff_index = sh->index; + if (sh->demuxer_id < 0) { + sh->demuxer_id = 0; + for (int n = 0; n < in->num_streams; n++) { + if (in->streams[n]->type == sh->type) + sh->demuxer_id += 1; + } } - return sh; + MP_TARRAY_APPEND(in, in->streams, in->num_streams, sh); + + in->events |= DEMUX_EVENT_STREAMS; + if (in->wakeup_cb) + in->wakeup_cb(in->wakeup_cb_ctx); + pthread_mutex_unlock(&in->lock); +} + +// Return a stream with the given index. Since streams can only be added during +// the lifetime of the demuxer, it is guaranteed that an index within the valid +// range [0, demux_get_num_stream()) always returns a valid sh_stream pointer, +// which will be valid until the demuxer is destroyed. +struct sh_stream *demux_get_stream(struct demuxer *demuxer, int index) +{ + struct demux_internal *in = demuxer->in; + pthread_mutex_lock(&in->lock); + assert(index >= 0 && index < in->num_streams); + struct sh_stream *r = in->streams[index]; + pthread_mutex_unlock(&in->lock); + return r; +} + +// See demux_get_stream(). +int demux_get_num_stream(struct demuxer *demuxer) +{ + struct demux_internal *in = demuxer->in; + pthread_mutex_lock(&in->lock); + int r = in->num_streams; + pthread_mutex_unlock(&in->lock); + return r; } void free_demuxer(demuxer_t *demuxer) @@ -247,8 +294,10 @@ void free_demuxer(demuxer_t *demuxer) if (demuxer->desc->close) demuxer->desc->close(in->d_thread); - for (int n = 0; n < demuxer->num_streams; n++) - ds_flush(demuxer->streams[n]->ds); + for (int n = in->num_streams - 1; n >= 0; n--) { + ds_flush(in->streams[n]->ds); + talloc_free(in->streams[n]); + } pthread_mutex_destroy(&in->lock); pthread_cond_destroy(&in->wakeup); talloc_free(demuxer); @@ -388,8 +437,8 @@ static bool read_packet(struct demux_internal *in) // safe-guards against packet queue overflow. bool active = false, read_more = false; size_t packs = 0, bytes = 0; - for (int n = 0; n < in->d_buffer->num_streams; n++) { - struct demux_stream *ds = in->d_buffer->streams[n]->ds; + for (int n = 0; n < in->num_streams; n++) { + struct demux_stream *ds = in->streams[n]->ds; active |= ds->active; read_more |= ds->active && !ds->head; packs += ds->packs; @@ -404,16 +453,16 @@ static bool read_packet(struct demux_internal *in) if (!in->warned_queue_overflow) { in->warned_queue_overflow = true; MP_ERR(in, "Too many packets in the demuxer packet queues:\n"); - for (int n = 0; n < in->d_buffer->num_streams; n++) { - struct demux_stream *ds = in->d_buffer->streams[n]->ds; + for (int n = 0; n < in->num_streams; n++) { + struct demux_stream *ds = in->streams[n]->ds; if (ds->selected) { MP_ERR(in, " %s/%d: %zd packets, %zd bytes\n", stream_type_name(ds->type), n, ds->packs, ds->bytes); } } } - for (int n = 0; n < in->d_buffer->num_streams; n++) { - struct demux_stream *ds = in->d_buffer->streams[n]->ds; + for (int n = 0; n < in->num_streams; n++) { + struct demux_stream *ds = in->streams[n]->ds; ds->eof |= !ds->head; } pthread_cond_signal(&in->wakeup); @@ -433,8 +482,8 @@ static bool read_packet(struct demux_internal *in) pthread_mutex_lock(&in->lock); if (eof) { - for (int n = 0; n < in->d_buffer->num_streams; n++) - in->d_buffer->streams[n]->ds->eof = true; + for (int n = 0; n < in->num_streams; n++) + in->streams[n]->ds->eof = true; // If we had EOF previously, then don't wakeup (avoids wakeup loop) if (!in->last_eof) { if (in->wakeup_cb) @@ -478,8 +527,8 @@ static void start_refreshing(struct demux_internal *in) in->start_refresh_seek = false; double start_ts = MP_NOPTS_VALUE; - for (int n = 0; n < demux->num_streams; n++) { - struct demux_stream *ds = demux->streams[n]->ds; + for (int n = 0; n < in->num_streams; n++) { + struct demux_stream *ds = in->streams[n]->ds; if (ds->type == STREAM_VIDEO || ds->type == STREAM_AUDIO) start_ts = MP_PTS_MIN(start_ts, ds->base_ts); } @@ -488,8 +537,8 @@ static void start_refreshing(struct demux_internal *in) demux->partially_seekable || !demux->allow_refresh_seeks) return; - for (int n = 0; n < demux->num_streams; n++) { - struct demux_stream *ds = demux->streams[n]->ds; + for (int n = 0; n < in->num_streams; n++) { + struct demux_stream *ds = in->streams[n]->ds; // Streams which didn't read any packets yet can return all packets, // or they'd be stuck forever; affects newly selected streams too. if (ds->last_pos != -1) @@ -635,15 +684,33 @@ struct demux_packet *demux_read_packet(struct sh_stream *sh) return pkt; } +// Sparse packets (Subtitles) interleaved with other non-sparse packets (video, +// audio) should never be read actively, meaning the demuxer thread does not +// try to exceed default readahead in order to find a new packet. +static bool use_lazy_subtitle_reading(struct demux_stream *ds) +{ + if (ds->type != STREAM_SUB) + return false; + for (int n = 0; n < ds->in->num_streams; n++) { + struct demux_stream *s = ds->in->streams[n]->ds; + if (s->type != STREAM_SUB && s->selected && !s->eof) + return true; + } + return false; +} + // Poll the demuxer queue, and if there's a packet, return it. Otherwise, just // make the demuxer thread read packets for this stream, and if there's at // least one packet, call the wakeup callback. -// Unlike demux_read_packet(), this always enables readahead (which means you -// must not use it on interleaved subtitle streams). +// Unlike demux_read_packet(), this always enables readahead (except for +// interleaved subtitles). // Returns: // < 0: EOF was reached, *out_pkt=NULL // == 0: no new packet yet, but maybe later, *out_pkt=NULL // > 0: new packet read, *out_pkt is set +// Note: when reading interleaved subtitles, the demuxer won't try to forcibly +// read ahead to get the next subtitle packet (as the next packet could be +// minutes away). In this situation, this function will just return -1. int demux_read_packet_async(struct sh_stream *sh, struct demux_packet **out_pkt) { struct demux_stream *ds = sh ? sh->ds : NULL; @@ -653,10 +720,14 @@ int demux_read_packet_async(struct sh_stream *sh, struct demux_packet **out_pkt) if (ds->in->threading) { pthread_mutex_lock(&ds->in->lock); *out_pkt = dequeue_packet(ds); - r = *out_pkt ? 1 : ((ds->eof || !ds->selected) ? -1 : 0); - ds->active = ds->selected; // enable readahead - ds->in->eof = false; // force retry - pthread_cond_signal(&ds->in->wakeup); // possibly read more + if (use_lazy_subtitle_reading(ds)) { + r = *out_pkt ? 1 : -1; + } else { + r = *out_pkt ? 1 : ((ds->eof || !ds->selected) ? -1 : 0); + ds->active = ds->selected; // enable readahead + ds->in->eof = false; // force retry + pthread_cond_signal(&ds->in->wakeup); // possibly read more + } pthread_mutex_unlock(&ds->in->lock); } else { *out_pkt = demux_read_packet(sh); @@ -666,22 +737,6 @@ int demux_read_packet_async(struct sh_stream *sh, struct demux_packet **out_pkt) return r; } -// Return the pts of the next packet that demux_read_packet() would return. -// Might block. Sometimes used to force a packet read, without removing any -// packets from the queue. -double demux_get_next_pts(struct sh_stream *sh) -{ - double res = MP_NOPTS_VALUE; - if (sh) { - pthread_mutex_lock(&sh->ds->in->lock); - ds_get_packets(sh->ds); - if (sh->ds->head) - res = MP_ADD_PTS(sh->ds->head->pts, sh->ds->in->ts_offset); - pthread_mutex_unlock(&sh->ds->in->lock); - } - return res; -} - // Return whether a packet is queued. Never blocks, never forces any reads. bool demux_has_packet(struct sh_stream *sh) { @@ -697,21 +752,22 @@ bool demux_has_packet(struct sh_stream *sh) // Read and return any packet we find. struct demux_packet *demux_read_any_packet(struct demuxer *demuxer) { - assert(!demuxer->in->threading); // doesn't work with threading + struct demux_internal *in = demuxer->in; + assert(!in->threading); // doesn't work with threading bool read_more = true; while (read_more) { - for (int n = 0; n < demuxer->num_streams; n++) { - struct sh_stream *sh = demuxer->streams[n]; + for (int n = 0; n < in->num_streams; n++) { + struct sh_stream *sh = in->streams[n]; sh->ds->active = sh->ds->selected; // force read_packet() to read struct demux_packet *pkt = dequeue_packet(sh->ds); if (pkt) return pkt; } // retry after calling this - pthread_mutex_lock(&demuxer->in->lock); - read_more = read_packet(demuxer->in); - read_more &= !demuxer->in->eof; - pthread_mutex_unlock(&demuxer->in->lock); + pthread_mutex_lock(&in->lock); // lock only because read_packet unlocks + read_more = read_packet(in); + read_more &= !in->eof; + pthread_mutex_unlock(&in->lock); } return NULL; } @@ -795,13 +851,14 @@ static int decode_peak(demuxer_t *demuxer, const char *tag, float *out) static void apply_replaygain(demuxer_t *demuxer, struct replaygain_data *rg) { - for (int n = 0; n < demuxer->num_streams; n++) { - struct sh_stream *sh = demuxer->streams[n]; - if (sh->audio && !sh->audio->replaygain_data) { + struct demux_internal *in = demuxer->in; + for (int n = 0; n < in->num_streams; n++) { + struct sh_stream *sh = in->streams[n]; + if (sh->type == STREAM_AUDIO && !sh->codec->replaygain_data) { MP_VERBOSE(demuxer, "Replaygain: Track=%f/%f Album=%f/%f\n", rg->track_gain, rg->track_peak, rg->album_gain, rg->album_peak); - sh->audio->replaygain_data = talloc_memdup(demuxer, rg, sizeof(*rg)); + sh->codec->replaygain_data = talloc_memdup(in, rg, sizeof(*rg)); } } } @@ -853,11 +910,6 @@ static void demux_copy(struct demuxer *dst, struct demuxer *src) dst->start_time = src->start_time; dst->priv = src->priv; } - if (src->events & DEMUX_EVENT_STREAMS) { - // The stream structs themselves are immutable. - for (int n = dst->num_streams; n < src->num_streams; n++) - MP_TARRAY_APPEND(dst, dst->streams, dst->num_streams, src->streams[n]); - } if (src->events & DEMUX_EVENT_METADATA) { talloc_free(dst->metadata); dst->metadata = mp_tags_dup(dst, src->metadata); @@ -905,6 +957,8 @@ void demux_update(demuxer_t *demuxer) pthread_mutex_lock(&in->lock); demux_copy(demuxer, in->d_buffer); + demuxer->events |= in->events; + in->events = 0; if (in->stream_metadata && (demuxer->events & DEMUX_EVENT_METADATA)) mp_tags_merge(demuxer->metadata, in->stream_metadata); pthread_mutex_unlock(&in->lock); @@ -926,10 +980,15 @@ static void demux_init_cuesheet(struct demuxer *demuxer) if (cue && !demuxer->num_chapters) { struct cue_file *f = mp_parse_cue(bstr0(cue)); if (f) { - for (int n = 0; n < f->num_tracks; n++) { - struct cue_track *t = &f->tracks[n]; - int idx = demuxer_add_chapter(demuxer, "", t->start, -1); - mp_tags_merge(demuxer->chapters[idx].metadata, t->tags); + if (mp_check_embedded_cue(f) < 0) { + MP_WARN(demuxer, "Embedded cue sheet references more than one file. " + "Ignoring it.\n"); + } else { + for (int n = 0; n < f->num_tracks; n++) { + struct cue_track *t = &f->tracks[n]; + int idx = demuxer_add_chapter(demuxer, "", t->start, -1); + mp_tags_merge(demuxer->chapters[idx].metadata, t->tags); + } } } talloc_free(f); @@ -1108,8 +1167,8 @@ struct demuxer *demux_open_url(const char *url, static void flush_locked(demuxer_t *demuxer) { - for (int n = 0; n < demuxer->num_streams; n++) - ds_flush(demuxer->streams[n]->ds); + for (int n = 0; n < demuxer->in->num_streams; n++) + ds_flush(demuxer->in->streams[n]->ds); demuxer->in->warned_queue_overflow = false; demuxer->in->eof = false; demuxer->in->last_eof = false; @@ -1187,10 +1246,11 @@ void demux_set_enable_refresh_seeks(struct demuxer *demuxer, bool enabled) struct sh_stream *demuxer_stream_by_demuxer_id(struct demuxer *d, enum stream_type t, int id) { - for (int n = 0; n < d->num_streams; n++) { - struct sh_stream *s = d->streams[n]; + int num = demux_get_num_stream(d); + for (int n = 0; n < num; n++) { + struct sh_stream *s = demux_get_stream(d, n); if (s->type == t && s->demuxer_id == id) - return d->streams[n]; + return s; } return NULL; } @@ -1200,8 +1260,9 @@ void demuxer_switch_track(struct demuxer *demuxer, enum stream_type type, { assert(!stream || stream->type == type); - for (int n = 0; n < demuxer->num_streams; n++) { - struct sh_stream *cur = demuxer->streams[n]; + int num = demux_get_num_stream(demuxer); + for (int n = 0; n < num; n++) { + struct sh_stream *cur = demux_get_stream(demuxer, n); if (cur->type == type) demuxer_select_track(demuxer, cur, cur == stream); } @@ -1403,8 +1464,8 @@ static int cached_demux_control(struct demux_internal *in, int cmd, void *arg) double *rates = arg; for (int n = 0; n < STREAM_TYPE_COUNT; n++) rates[n] = -1; - for (int n = 0; n < in->d_user->num_streams; n++) { - struct demux_stream *ds = in->d_user->streams[n]->ds; + for (int n = 0; n < in->num_streams; n++) { + struct demux_stream *ds = in->streams[n]->ds; if (ds->selected && ds->bitrate >= 0) rates[ds->type] = MPMAX(0, rates[ds->type]) + ds->bitrate; } @@ -1418,8 +1479,8 @@ static int cached_demux_control(struct demux_internal *in, int cmd, void *arg) .ts_duration = -1, }; int num_packets = 0; - for (int n = 0; n < in->d_user->num_streams; n++) { - struct demux_stream *ds = in->d_user->streams[n]->ds; + for (int n = 0; n < in->num_streams; n++) { + struct demux_stream *ds = in->streams[n]->ds; if (ds->active) { r->underrun |= !ds->head && !ds->eof; r->ts_range[0] = MP_PTS_MAX(r->ts_range[0], ds->base_ts); |