From 6d36fad83c779936a012e85a1eb92ec94651c7c0 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sun, 28 Jan 2018 10:08:45 +0100 Subject: video: make decoder wrapper a filter Move dec_video.c to filters/f_decoder_wrapper.c. It essentially becomes a source filter. vd.h mostly disappears, because mp_filter takes care of the dataflow, but its remains are in struct mp_decoder_fns. One goal is to simplify dataflow by letting the filter framework handle it (or more accurately, using its conventions). One result is that the decode calls disappear from video.c, because we simply connect the decoder wrapper and the filter chain with mp_pin_connect(). Another goal is to eventually remove the code duplication between the audio and video paths for this. This commit prepares for this by trying to make f_decoder_wrapper.c extensible, so it can be used for audio as well later. Decoder framedropping changes a bit. It doesn't seem to be worse than before, and it's an obscure feature, so I'm content with its new state. Some special code that was apparently meant to avoid dropping too many frames in a row is removed, though. I'm not sure how the source code tree should be organized. For one, video/decode/vd_lavc.c is the only file in its directory, which is a bit annoying. --- demux/packet.c | 2 + filters/f_decoder_wrapper.c | 638 ++++++++++++++++++++++++++++++++++++++++++++ filters/f_decoder_wrapper.h | 104 ++++++++ filters/f_demux_in.c | 83 ++++++ filters/f_demux_in.h | 11 + filters/f_output_chain.c | 14 +- filters/f_output_chain.h | 2 + filters/f_utils.c | 2 + filters/filter.h | 1 + filters/frame.c | 13 +- filters/frame.h | 1 + player/audio.c | 1 - player/command.c | 41 +-- player/core.h | 14 +- player/loadfile.c | 27 +- player/main.c | 2 +- player/osd.c | 7 +- player/playloop.c | 37 +-- player/screenshot.c | 1 - player/sub.c | 1 - player/video.c | 228 ++++------------ video/decode/dec_video.c | 526 ------------------------------------ video/decode/dec_video.h | 101 ------- video/decode/vd.h | 56 ---- video/decode/vd_lavc.c | 218 ++++++++------- wscript_build.py | 3 +- 26 files changed, 1094 insertions(+), 1040 deletions(-) create mode 100644 filters/f_decoder_wrapper.c create mode 100644 filters/f_decoder_wrapper.h create mode 100644 filters/f_demux_in.c create mode 100644 filters/f_demux_in.h delete mode 100644 video/decode/dec_video.c delete mode 100644 video/decode/dec_video.h delete mode 100644 video/decode/vd.h diff --git a/demux/packet.c b/demux/packet.c index 84fda8c736..83aee6801a 100644 --- a/demux/packet.c +++ b/demux/packet.c @@ -78,6 +78,8 @@ struct demux_packet *new_demux_packet_from_avpacket(struct AVPacket *avpkt) // (buf must include proper padding) struct demux_packet *new_demux_packet_from_buf(struct AVBufferRef *buf) { + if (!buf) + return NULL; AVPacket pkt = { .size = buf->size, .data = buf->data, diff --git a/filters/f_decoder_wrapper.c b/filters/f_decoder_wrapper.c new file mode 100644 index 0000000000..e85621957f --- /dev/null +++ b/filters/f_decoder_wrapper.c @@ -0,0 +1,638 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see . + */ + +#include +#include +#include +#include + +#include +#include + +#include "config.h" +#include "options/options.h" +#include "common/msg.h" + +#include "osdep/timer.h" + +#include "demux/demux.h" +#include "demux/packet.h" + +#include "common/codecs.h" +#include "common/global.h" +#include "common/recorder.h" + +#include "video/out/vo.h" +#include "video/csputils.h" + +#include "demux/stheader.h" + +#include "f_decoder_wrapper.h" +#include "f_demux_in.h" +#include "filter_internal.h" + +struct priv { + struct mp_filter *f; + struct mp_log *log; + struct MPOpts *opts; + + struct sh_stream *header; + struct mp_codec_params *codec; + + struct mp_decoder *decoder; + + // Demuxer output. + struct mp_pin *demux; + + // Last PTS from decoder (set with each vd_driver->decode() call) + double codec_pts; + int num_codec_pts_problems; + + // Last packet DTS from decoder (passed through from source packets) + double codec_dts; + int num_codec_dts_problems; + + // PTS or DTS of packet first read + double first_packet_pdts; + + // There was at least one packet with nonsense timestamps. + int has_broken_packet_pts; // <0: uninitialized, 0: no problems, 1: broken + + int has_broken_decoded_pts; + + int packets_without_output; // number packets sent without frame received + + // Final PTS of previously decoded frame + double pts; + + struct mp_image_params dec_format, last_format, fixed_format; + + double start_pts; + double start, end; + struct demux_packet *new_segment; + struct mp_frame packet; + + struct mp_frame decoded_coverart; + int coverart_returned; // 0: no, 1: coverart frame itself, 2: EOF returned + + struct mp_decoder_wrapper public; +}; + +static void reset_decoder(struct priv *p) +{ + p->first_packet_pdts = MP_NOPTS_VALUE; + p->start_pts = MP_NOPTS_VALUE; + p->pts = MP_NOPTS_VALUE; + p->codec_pts = MP_NOPTS_VALUE; + p->codec_dts = MP_NOPTS_VALUE; + p->has_broken_decoded_pts = 0; + p->last_format = p->fixed_format = (struct mp_image_params){0}; + p->public.dropped_frames = 0; + p->public.attempt_framedrops = 0; + p->packets_without_output = 0; + mp_frame_unref(&p->packet); + talloc_free(p->new_segment); + p->new_segment = NULL; + p->start = p->end = MP_NOPTS_VALUE; + p->coverart_returned = 0; + + if (p->decoder) + mp_filter_reset(p->decoder->f); +} + +static void reset(struct mp_filter *f) +{ + struct priv *p = f->priv; + + reset_decoder(p); +} + +int mp_decoder_wrapper_control(struct mp_decoder_wrapper *d, + enum dec_ctrl cmd, void *arg) +{ + struct priv *p = d->f->priv; + if (p->decoder && p->decoder->control) + return p->decoder->control(p->decoder->f, cmd, arg); + return CONTROL_UNKNOWN; +} + +static void destroy(struct mp_filter *f) +{ + struct priv *p = f->priv; + if (p->decoder) { + MP_VERBOSE(f, "Uninit video.\n"); + talloc_free(p->decoder->f); + p->decoder = NULL; + } + reset_decoder(p); + mp_frame_unref(&p->decoded_coverart); +} + +struct mp_decoder_list *video_decoder_list(void) +{ + struct mp_decoder_list *list = talloc_zero(NULL, struct mp_decoder_list); + vd_lavc.add_decoders(list); + return list; +} + +bool mp_decoder_wrapper_reinit(struct mp_decoder_wrapper *d) +{ + struct priv *p = d->f->priv; + struct MPOpts *opts = p->opts; + + if (p->decoder) + talloc_free(p->decoder->f); + p->decoder = NULL; + + reset_decoder(p); + p->has_broken_packet_pts = -10; // needs 10 packets to reach decision + + const struct mp_decoder_fns *driver = &vd_lavc; + + struct mp_decoder_list *full = talloc_zero(NULL, struct mp_decoder_list); + driver->add_decoders(full); + struct mp_decoder_list *list = + mp_select_decoders(p->log, full, p->codec->codec, opts->video_decoders); + talloc_free(full); + + mp_print_decoders(p->log, MSGL_V, "Codec list:", list); + + for (int n = 0; n < list->num_entries; n++) { + struct mp_decoder_entry *sel = &list->entries[n]; + MP_VERBOSE(p, "Opening decoder %s\n", sel->decoder); + + p->decoder = driver->create(p->f, p->codec, sel->decoder); + if (p->decoder) { + p->public.decoder_desc = + talloc_asprintf(p, "%s (%s)", sel->decoder, sel->desc); + MP_VERBOSE(p, "Selected codec: %s\n", p->public.decoder_desc); + break; + } + + MP_WARN(p, "Decoder init failed for %s\n", sel->decoder); + } + + if (!p->decoder) { + MP_ERR(p, "Failed to initialize a decoder for codec '%s'.\n", + p->codec->codec ? p->codec->codec : ""); + } + + talloc_free(list); + return !!p->decoder; +} + +static bool is_valid_peak(float sig_peak) +{ + return !sig_peak || (sig_peak >= 1 && sig_peak <= 100); +} + +static void fix_image_params(struct priv *p, + struct mp_image_params *params) +{ + struct MPOpts *opts = p->opts; + struct mp_image_params m = *params; + struct mp_codec_params *c = p->codec; + + MP_VERBOSE(p, "Decoder format: %s\n", mp_image_params_to_str(params)); + p->dec_format = *params; + + // While mp_image_params normally always have to have d_w/d_h set, the + // decoder signals unknown bitstream aspect ratio with both set to 0. + bool use_container = true; + if (opts->aspect_method == 1 && m.p_w > 0 && m.p_h > 0) { + MP_VERBOSE(p, "Using bitstream aspect ratio.\n"); + use_container = false; + } + + if (use_container && c->par_w > 0 && c->par_h) { + MP_VERBOSE(p, "Using container aspect ratio.\n"); + m.p_w = c->par_w; + m.p_h = c->par_h; + } + + if (opts->movie_aspect >= 0) { + MP_VERBOSE(p, "Forcing user-set aspect ratio.\n"); + if (opts->movie_aspect == 0) { + m.p_w = m.p_h = 1; + } else { + AVRational a = av_d2q(opts->movie_aspect, INT_MAX); + mp_image_params_set_dsize(&m, a.num, a.den); + } + } + + // Assume square pixels if no aspect ratio is set at all. + if (m.p_w <= 0 || m.p_h <= 0) + m.p_w = m.p_h = 1; + + m.rotate = p->codec->rotate; + m.stereo_in = p->codec->stereo_mode; + + if (opts->video_rotate < 0) { + m.rotate = 0; + } else { + m.rotate = (m.rotate + opts->video_rotate) % 360; + } + m.stereo_out = opts->video_stereo_mode; + + mp_colorspace_merge(&m.color, &c->color); + + // Sanitize the HDR peak. Sadly necessary + if (!is_valid_peak(m.color.sig_peak)) { + MP_WARN(p, "Invalid HDR peak in stream: %f\n", m.color.sig_peak); + m.color.sig_peak = 0.0; + } + + m.spherical = c->spherical; + if (m.spherical.type == MP_SPHERICAL_AUTO) + m.spherical.type = MP_SPHERICAL_NONE; + + // Guess missing colorspace fields from metadata. This guarantees all + // fields are at least set to legal values afterwards. + mp_image_params_guess_csp(&m); + + p->last_format = *params; + p->fixed_format = m; +} + +static void process_video_frame(struct priv *p, struct mp_image *mpi) +{ + struct MPOpts *opts = p->opts; + + // Note: the PTS is reordered, but the DTS is not. Both should be monotonic. + double pts = mpi->pts; + double dts = mpi->dts; + + if (pts != MP_NOPTS_VALUE) { + if (pts < p->codec_pts) + p->num_codec_pts_problems++; + p->codec_pts = mpi->pts; + } + + if (dts != MP_NOPTS_VALUE) { + if (dts <= p->codec_dts) + p->num_codec_dts_problems++; + p->codec_dts = mpi->dts; + } + + if (p->has_broken_packet_pts < 0) + p->has_broken_packet_pts++; + if (p->num_codec_pts_problems) + p->has_broken_packet_pts = 1; + + // If PTS is unset, or non-monotonic, fall back to DTS. + if ((p->num_codec_pts_problems > p->num_codec_dts_problems || + pts == MP_NOPTS_VALUE) && dts != MP_NOPTS_VALUE) + pts = dts; + + if (!opts->correct_pts || pts == MP_NOPTS_VALUE) { + double fps = p->public.fps > 0 ? p->public.fps : 25; + + if (opts->correct_pts) { + if (p->has_broken_decoded_pts <= 1) { + MP_WARN(p, "No video PTS! Making something up. using " + "%f FPS.\n", fps); + if (p->has_broken_decoded_pts == 1) + MP_WARN(p, "Ignoring further missing PTS warnings.\n"); + p->has_broken_decoded_pts++; + } + } + + double frame_time = 1.0f / fps; + double base = p->first_packet_pdts; + pts = p->pts; + if (pts == MP_NOPTS_VALUE) { + pts = base == MP_NOPTS_VALUE ? 0 : base; + } else { + pts += frame_time; + } + } + + if (!mp_image_params_equal(&p->last_format, &mpi->params)) + fix_image_params(p, &mpi->params); + + mpi->params = p->fixed_format; + + mpi->pts = pts; + p->pts = pts; + + // Compensate for incorrectly using mpeg-style DTS for avi timestamps. + if (p->decoder && p->decoder->control && p->codec->avi_dts && + opts->correct_pts && mpi->pts != MP_NOPTS_VALUE && p->public.fps > 0) + { + int delay = -1; + p->decoder->control(p->decoder->f, VDCTRL_GET_BFRAMES, &delay); + mpi->pts -= MPMAX(delay, 0) / p->public.fps; + } + + struct demux_packet *ccpkt = new_demux_packet_from_buf(mpi->a53_cc); + if (ccpkt) { + av_buffer_unref(&mpi->a53_cc); + ccpkt->pts = mpi->pts; + ccpkt->dts = mpi->dts; + demuxer_feed_caption(p->header, ccpkt); + } + + if (mpi->pts == MP_NOPTS_VALUE || mpi->pts >= p->start_pts) + p->start_pts = MP_NOPTS_VALUE; +} + +void mp_decoder_wrapper_reset_params(struct mp_decoder_wrapper *d) +{ + struct priv *p = d->f->priv; + p->last_format = (struct mp_image_params){0}; +} + +void mp_decoder_wrapper_get_video_dec_params(struct mp_decoder_wrapper *d, + struct mp_image_params *m) +{ + struct priv *p = d->f->priv; + *m = p->dec_format; +} + +// Frames before the start timestamp can be dropped. (Used for hr-seek.) +void mp_decoder_wrapper_set_start_pts(struct mp_decoder_wrapper *d, double pts) +{ + struct priv *p = d->f->priv; + p->start_pts = pts; +} + +static bool is_new_segment(struct priv *p, struct mp_frame frame) +{ + if (frame.type != MP_FRAME_PACKET) + return false; + struct demux_packet *pkt = frame.data; + return pkt->segmented && (pkt->start != p->start || pkt->end != p->end || + pkt->codec != p->codec); +} + +static void feed_packet(struct priv *p) +{ + if (!p->decoder || !mp_pin_in_needs_data(p->decoder->f->pins[0])) + return; + + if (!p->packet.type && !p->new_segment) { + p->packet = mp_pin_out_read(p->demux); + if (!p->packet.type) + return; + if (p->packet.type != MP_FRAME_EOF && p->packet.type != MP_FRAME_PACKET) { + MP_ERR(p, "invalid frame type from demuxer\n"); + mp_frame_unref(&p->packet); + mp_filter_internal_mark_failed(p->f); + return; + } + } + + // Flush current data if the packet is a new segment. + if (is_new_segment(p, p->packet)) { + assert(!p->new_segment); + p->new_segment = p->packet.data; + p->packet = MP_EOF_FRAME; + } + + assert(p->packet.type == MP_FRAME_PACKET || p->packet.type == MP_FRAME_EOF); + struct demux_packet *packet = p->packet.data; + + // For video framedropping, including parts of the hr-seek logic. + if (p->decoder->control) { + double start_pts = p->start_pts; + if (p->start != MP_NOPTS_VALUE && (start_pts == MP_NOPTS_VALUE || + p->start > start_pts)) + start_pts = p->start; + + int framedrop_type = 0; + + if (p->public.attempt_framedrops) + framedrop_type = 1; + + if (start_pts != MP_NOPTS_VALUE && packet && + packet->pts < start_pts - .005 && !p->has_broken_packet_pts) + framedrop_type = 2; + + p->decoder->control(p->decoder->f, VDCTRL_SET_FRAMEDROP, &framedrop_type); + } + + if (p->public.recorder_sink) + mp_recorder_feed_packet(p->public.recorder_sink, packet); + + double pkt_pts = packet ? packet->pts : MP_NOPTS_VALUE; + double pkt_dts = packet ? packet->dts : MP_NOPTS_VALUE; + + if (pkt_pts == MP_NOPTS_VALUE) + p->has_broken_packet_pts = 1; + + if (packet && packet->dts == MP_NOPTS_VALUE && !p->codec->avi_dts) + packet->dts = packet->pts; + + double pkt_pdts = pkt_pts == MP_NOPTS_VALUE ? pkt_dts : pkt_pts; + if (p->first_packet_pdts == MP_NOPTS_VALUE) + p->first_packet_pdts = pkt_pdts; + + mp_pin_in_write(p->decoder->f->pins[0], p->packet); + p->packet = MP_NO_FRAME; + + p->packets_without_output += 1; +} + +// Return true if the current frame is outside segment range. +static bool process_decoded_frame(struct priv *p, struct mp_frame *frame) +{ + if (frame->type == MP_FRAME_EOF) { + // if we were just draining current segment, don't propagate EOF + if (p->new_segment) + mp_frame_unref(frame); + return true; + } + + bool segment_ended = false; + + if (frame->type == MP_FRAME_VIDEO) { + struct mp_image *mpi = frame->data; + + process_video_frame(p, mpi); + + if (mpi->pts != MP_NOPTS_VALUE) { + double vpts = mpi->pts; + segment_ended = p->end != MP_NOPTS_VALUE && vpts >= p->end; + if ((p->start != MP_NOPTS_VALUE && vpts < p->start) || segment_ended) + mp_frame_unref(frame); + } + } else { + MP_ERR(p, "unknown frame type from decoder\n"); + } + + return segment_ended; +} + +static void read_frame(struct priv *p) +{ + struct mp_pin *pin = p->f->ppins[0]; + + if (!p->decoder || !mp_pin_in_needs_data(pin)) + return; + + if (p->decoded_coverart.type) { + if (p->coverart_returned == 0) { + mp_pin_in_write(pin, mp_frame_ref(p->decoded_coverart)); + p->coverart_returned = 1; + } else if (p->coverart_returned == 1) { + mp_pin_in_write(pin, MP_EOF_FRAME); + p->coverart_returned = 2; + } + return; + } + + struct mp_frame frame = mp_pin_out_read(p->decoder->f->pins[1]); + if (!frame.type) + return; + + if (p->public.attempt_framedrops) { + int dropped = MPMAX(0, p->packets_without_output - 1); + p->public.attempt_framedrops = + MPMAX(0, p->public.attempt_framedrops - dropped); + p->public.dropped_frames += dropped; + } + p->packets_without_output = 0; + + bool segment_ended = process_decoded_frame(p, &frame); + + // If there's a new segment, start it as soon as we're drained/finished. + if (segment_ended && p->new_segment) { + struct demux_packet *new_segment = p->new_segment; + p->new_segment = NULL; + + reset_decoder(p); + + if (p->codec != new_segment->codec) { + p->codec = new_segment->codec; + if (!mp_decoder_wrapper_reinit(&p->public)) + mp_filter_internal_mark_failed(p->f); + } + + p->start = new_segment->start; + p->end = new_segment->end; + + p->packet = MAKE_FRAME(MP_FRAME_PACKET, new_segment); + mp_filter_internal_mark_progress(p->f); + } + + if (!frame.type) { + mp_filter_internal_mark_progress(p->f); // make it retry + return; + } + + if (p->header->attached_picture && frame.type == MP_FRAME_VIDEO) { + p->decoded_coverart = mp_frame_ref(frame); + p->coverart_returned = 1; + } + + mp_pin_in_write(pin, frame); +} + +static void process(struct mp_filter *f) +{ + struct priv *p = f->priv; + + feed_packet(p); + read_frame(p); +} + +static const struct mp_filter_info decode_wrapper_filter = { + .name = "decode", + .priv_size = sizeof(struct priv), + .process = process, + .reset = reset, + .destroy = destroy, +}; + +struct mp_decoder_wrapper *mp_decoder_wrapper_create(struct mp_filter *parent, + struct sh_stream *src) +{ + struct mp_filter *f = mp_filter_create(parent, &decode_wrapper_filter); + if (!f) + return NULL; + + struct priv *p = f->priv; + struct mp_decoder_wrapper *w = &p->public; + p->opts = f->global->opts; + p->log = f->log; + p->f = f; + p->header = src; + p->codec = p->header->codec; + w->f = f; + + mp_filter_add_pin(f, MP_PIN_OUT, "out"); + + if (p->header->type == STREAM_VIDEO) { + p->log = f->log = mp_log_new(f, parent->log, "!vd"); + + p->public.fps = src->codec->fps; + + MP_VERBOSE(p, "Container reported FPS: %f\n", p->public.fps); + + if (p->opts->force_fps) { + p->public.fps = p->opts->force_fps; + MP_INFO(p, "FPS forced to %5.3f.\n", p->public.fps); + MP_INFO(p, "Use --no-correct-pts to force FPS based timing.\n"); + } + } + + struct mp_filter *demux = mp_demux_in_create(f, p->header); + if (!demux) + goto error; + p->demux = demux->pins[0]; + + return w; +error: + talloc_free(f); + return NULL; +} + +void lavc_process(struct mp_filter *f, bool *eof_flag, + bool (*send)(struct mp_filter *f, struct demux_packet *pkt), + bool (*receive)(struct mp_filter *f, struct mp_frame *res)) +{ + if (!mp_pin_in_needs_data(f->ppins[1])) + return; + + struct mp_frame frame = {0}; + if (!receive(f, &frame)) { + if (!*eof_flag) + mp_pin_in_write(f->ppins[1], MP_EOF_FRAME); + *eof_flag = true; + } else if (frame.type) { + *eof_flag = false; + mp_pin_in_write(f->ppins[1], frame); + } else { + // Need to feed a packet. + frame = mp_pin_out_read(f->ppins[0]); + struct demux_packet *pkt = NULL; + if (frame.type == MP_FRAME_PACKET) { + pkt = frame.data; + } else if (frame.type != MP_FRAME_EOF) { + if (frame.type) { + MP_ERR(f, "unexpected frame type\n"); + mp_frame_unref(&frame); + mp_filter_internal_mark_failed(f); + } + return; + } + if (!send(f, pkt)) + MP_WARN(f, "could not consume packet\n"); // should never happen + talloc_free(pkt); + mp_filter_internal_mark_progress(f); + } +} diff --git a/filters/f_decoder_wrapper.h b/filters/f_decoder_wrapper.h new file mode 100644 index 0000000000..4d970bd79a --- /dev/null +++ b/filters/f_decoder_wrapper.h @@ -0,0 +1,104 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see . + */ + +#pragma once + +#include + +#include "filter.h" + +struct sh_stream; +struct mp_codec_params; +struct mp_image_params; +struct mp_decoder_list; +struct demux_packet; + +// (free with talloc_free(mp_decoder_wrapper.f) +struct mp_decoder_wrapper { + // Filter with no input and 1 output, which returns the decoded data. + struct mp_filter *f; + + // For informational purposes. + char *decoder_desc; + + // Can be set by user. + struct mp_recorder_sink *recorder_sink; + + // --- for STREAM_VIDEO + + // FPS from demuxer or from user override + float fps; + + // Framedrop control for playback (not used for hr seek etc.) + int attempt_framedrops; // try dropping this many frames + int dropped_frames; // total frames _probably_ dropped +}; + +// Create the decoder wrapper for the given stream, plus underlying decoder. +// The src stream must be selected, and remain valid and selected until the +// wrapper is destroyed. +struct mp_decoder_wrapper *mp_decoder_wrapper_create(struct mp_filter *parent, + struct sh_stream *src); + +struct mp_decoder_list *video_decoder_list(void); + +// For precise seeking: if possible, try to drop frames up until the given PTS. +// This is automatically unset if the target is reached, or on reset. +void mp_decoder_wrapper_set_start_pts(struct mp_decoder_wrapper *d, double pts); + +enum dec_ctrl { + VDCTRL_FORCE_HWDEC_FALLBACK, // force software decoding fallback + VDCTRL_GET_HWDEC, + VDCTRL_REINIT, + VDCTRL_GET_BFRAMES, + // framedrop mode: 0=none, 1=standard, 2=hrseek + VDCTRL_SET_FRAMEDROP, +}; + +int mp_decoder_wrapper_control(struct mp_decoder_wrapper *d, + enum dec_ctrl cmd, void *arg); + +// Force it to reevaluate output parameters (for overrides like aspect). +void mp_decoder_wrapper_reset_params(struct mp_decoder_wrapper *d); + +void mp_decoder_wrapper_get_video_dec_params(struct mp_decoder_wrapper *d, + struct mp_image_params *p); + +bool mp_decoder_wrapper_reinit(struct mp_decoder_wrapper *d); + +struct mp_decoder { + // Bidirectional filter; takes MP_FRAME_PACKET for input. + struct mp_filter *f; + + // Can be set by decoder impl. on init for "special" functionality. + int (*control)(struct mp_filter *f, enum dec_ctrl cmd, void *arg); +}; + +struct mp_decoder_fns { + struct mp_decoder *(*create)(struct mp_filter *parent, + struct mp_codec_params *codec, + const char *decoder); + void (*add_decoders)(struct mp_decoder_list *list); +}; + +extern const struct mp_decoder_fns vd_lavc; + +// Convenience wrapper for lavc based decoders. eof_flag must be set to false +// on init and resets. +void lavc_process(struct mp_filter *f, bool *eof_flag, + bool (*send)(struct mp_filter *f, struct demux_packet *pkt), + bool (*receive)(struct mp_filter *f, struct mp_frame *res)); diff --git a/filters/f_demux_in.c b/filters/f_demux_in.c new file mode 100644 index 0000000000..defe68426f --- /dev/null +++ b/filters/f_demux_in.c @@ -0,0 +1,83 @@ +#include "common/common.h" +#include "demux/demux.h" +#include "demux/packet.h" + +#include "f_demux_in.h" +#include "filter_internal.h" + +struct priv { + struct sh_stream *src; + bool eof_returned; +}; + +static void wakeup(void *ctx) +{ + struct mp_filter *f = ctx; + + mp_filter_wakeup(f); +} + +static void process(struct mp_filter *f) +{ + struct priv *p = f->priv; + + if (!mp_pin_in_needs_data(f->ppins[0])) + return; + + struct demux_packet *pkt = NULL; + if (demux_read_packet_async(p->src, &pkt) == 0) + return; // wait + + struct mp_frame frame = {MP_FRAME_PACKET, pkt}; + if (pkt) { + p->eof_returned = false; + } else { + frame.type = MP_FRAME_EOF; + + // While the demuxer will repeat EOFs, filters never do that. + if (p->eof_returned) + return; + p->eof_returned = true; + } + + mp_pin_in_write(f->ppins[0], frame); +} + +static void reset(struct mp_filter *f) +{ + struct priv *p = f->priv; + + p->eof_returned = false; +} + +static void destroy(struct mp_filter *f) +{ + struct priv *p = f->priv; + + demux_set_stream_wakeup_cb(p->src, NULL, NULL); +} + +static const struct mp_filter_info demux_filter = { + .name = "demux", + .priv_size = sizeof(struct priv), + .process = process, + .reset = reset, + .destroy = destroy, +}; + +struct mp_filter *mp_demux_in_create(struct mp_filter *parent, + struct sh_stream *src) +{ + struct mp_filter *f = mp_filter_create(parent, &demux_filter); + if (!f) + return NULL; + + struct priv *p = f->priv; + p->src = src; + + mp_filter_add_pin(f, MP_PIN_OUT, "out"); + + demux_set_stream_wakeup_cb(p->src, wakeup, f); + + return f; +} diff --git a/filters/f_demux_in.h b/filters/f_demux_in.h new file mode 100644 index 0000000000..eebd428fee --- /dev/null +++ b/filters/f_demux_in.h @@ -0,0 +1,11 @@ +#pragma once + +#include "filter.h" + +struct sh_stream; + +// Create a filter with a single output for the given stream. The stream must +// be selected, and remain so until the filter is destroyed. The filter will +// set/unset the stream's wakeup callback. +struct mp_filter *mp_demux_in_create(struct mp_filter *parent, + struct sh_stream *src); diff --git a/filters/f_output_chain.c b/filters/f_output_chain.c index 3cbbaa2ccb..e53f9eafaa 100644 --- a/filters/f_output_chain.c +++ b/filters/f_output_chain.c @@ -406,9 +406,13 @@ static void process(struct mp_filter *f) if (mp_pin_can_transfer_data(p->filters_in, f->ppins[0])) { struct mp_frame frame = mp_pin_out_read(f->ppins[0]); - if (frame.type == MP_FRAME_EOF) { + p->public.got_input_eof = frame.type == MP_FRAME_EOF; + if (p->public.got_input_eof) MP_VERBOSE(p, "filter input EOF\n"); - p->public.got_input_eof = true; + + if (frame.type == MP_FRAME_VIDEO && p->public.update_subtitles) { + p->public.update_subtitles(p->public.update_subtitles_ctx, + mp_frame_get_pts(frame)); } mp_pin_in_write(p->filters_in, frame); @@ -417,10 +421,9 @@ static void process(struct mp_filter *f) if (mp_pin_can_transfer_data(f->ppins[1], p->filters_out)) { struct mp_frame frame = mp_pin_out_read(p->filters_out); - if (frame.type == MP_FRAME_EOF) { + p->public.got_output_eof = frame.type == MP_FRAME_EOF; + if (p->public.got_output_eof) MP_VERBOSE(p, "filter output EOF\n"); - p->public.got_output_eof = true; - } mp_pin_in_write(f->ppins[1], frame); } @@ -488,6 +491,7 @@ void mp_output_chain_set_vo(struct mp_output_chain *c, struct vo *vo) p->stream_info.hwdec_devs = vo ? vo->hwdec_devs : NULL; p->stream_info.osd = vo ? vo->osd : NULL; p->stream_info.rotate90 = vo ? vo->driver->caps & VO_CAP_ROTATE90 : false; + p->stream_info.dr_vo = vo; p->vo = vo; update_output_caps(p); } diff --git a/filters/f_output_chain.h b/filters/f_output_chain.h index 246f84181f..e5f9eff125 100644 --- a/filters/f_output_chain.h +++ b/filters/f_output_chain.h @@ -32,6 +32,8 @@ struct mp_output_chain { struct mp_image_params input_params; struct mp_image_params output_params; double container_fps; + void (*update_subtitles)(void *ctx, double pts); + void *update_subtitles_ctx; // --- for type==MP_OUTPUT_CHAIN_AUDIO struct mp_aframe *input_aformat; diff --git a/filters/f_utils.c b/filters/f_utils.c index d15d063879..8dc185f90e 100644 --- a/filters/f_utils.c +++ b/filters/f_utils.c @@ -28,7 +28,9 @@ static void frame_duration_process(struct mp_filter *f) if (p->buffered->pts != MP_NOPTS_VALUE && next->pts != MP_NOPTS_VALUE && next->pts >= p->buffered->pts) + { p->buffered->pkt_duration = next->pts - p->buffered->pts; + } mp_pin_in_write(f->ppins[1], MAKE_FRAME(MP_FRAME_VIDEO, p->buffered)); } else { mp_pin_out_request_data(f->ppins[0]); diff --git a/filters/filter.h b/filters/filter.h index eb554c29c8..9ba387691a 100644 --- a/filters/filter.h +++ b/filters/filter.h @@ -345,6 +345,7 @@ struct mp_stream_info { struct mp_hwdec_devices *hwdec_devs; struct osd_state *osd; bool rotate90; + struct vo *dr_vo; // for calling vo_get_image() }; // Search for a parent filter (including f) that has this set, and return it. diff --git a/filters/frame.c b/filters/frame.c index 6c5b28e77a..f1d4c98eab 100644 --- a/filters/frame.c +++ b/filters/frame.c @@ -2,6 +2,7 @@ #include "audio/aframe.h" #include "common/av_common.h" +#include "demux/packet.h" #include "video/mp_image.h" #include "frame.h" @@ -11,7 +12,6 @@ struct frame_handler { bool is_data; bool is_signaling; void *(*new_ref)(void *data); - // The following must be non-NULL if new_ref is non-NULL. double (*get_pts)(void *data); void (*set_pts)(void *data, double pts); AVFrame *(*new_av_ref)(void *data); @@ -69,6 +69,11 @@ static void *audio_from_av_ref(AVFrame *data) return mp_aframe_from_avframe(data); } +static void *packet_ref(void *data) +{ + return demux_copy_packet(data); +} + static const struct frame_handler frame_handlers[] = { [MP_FRAME_NONE] = { .name = "none", @@ -97,6 +102,12 @@ static const struct frame_handler frame_handlers[] = { .from_av_ref = audio_from_av_ref, .free = talloc_free, }, + [MP_FRAME_PACKET] = { + .name = "packet", + .is_data = true, + .new_ref = packet_ref, + .free = talloc_free, + }, }; const char *mp_frame_type_str(enum mp_frame_type t) diff --git a/filters/frame.h b/filters/frame.h index 135920dea7..606ca38846 100644 --- a/filters/frame.h +++ b/filters/frame.h @@ -6,6 +6,7 @@ enum mp_frame_type { MP_FRAME_NONE = 0, // NULL, placeholder, no frame available (_not_ EOF) MP_FRAME_VIDEO, // struct mp_image* MP_FRAME_AUDIO, // struct mp_aframe* + MP_FRAME_PACKET, // struct demux_packet* MP_FRAME_EOF, // NULL, signals end of stream (but frames after it can // resume filtering!) }; diff --git a/player/audio.c b/player/audio.c index 014bde9ff1..ab53ab3b86 100644 --- a/player/audio.c +++ b/player/audio.c @@ -36,7 +36,6 @@ #include "audio/decode/dec_audio.h" #include "audio/out/ao.h" #include "demux/demux.h" -#include "video/decode/dec_video.h" #include "core.h" #include "command.h" diff --git a/player/command.c b/player/command.c index 809f859280..d1de5a86ff 100644 --- a/player/command.c +++ b/player/command.c @@ -36,6 +36,7 @@ #include "common/codecs.h" #include "common/msg.h" #include "common/msg_control.h" +#include "filters/f_decoder_wrapper.h" #include "command.h" #include "osdep/timer.h" #include "common/common.h" @@ -50,13 +51,12 @@ #include "options/m_option.h" #include "options/m_property.h" #include "options/m_config.h" -#include "video/decode/vd.h" #include "video/out/vo.h" #include "video/csputils.h" +#include "video/hwdec.h" #include "audio/aframe.h" #include "audio/format.h" #include "audio/out/ao.h" -#include "video/decode/dec_video.h" #include "audio/decode/dec_audio.h" #include "video/out/bitmap_packer.h" #include "options/path.h" @@ -675,10 +675,12 @@ static int mp_property_frame_drop_dec(void *ctx, struct m_property *prop, int action, void *arg) { MPContext *mpctx = ctx; - if (!mpctx->vo_chain) + struct mp_decoder_wrapper *dec = mpctx->vo_chain && mpctx->vo_chain->track + ? mpctx->vo_chain->track->dec : NULL; + if (!dec) return M_PROPERTY_UNAVAILABLE; - return m_property_int_ro(action, arg, mpctx->vo_chain->video_src->dropped_frames); + return m_property_int_ro(action, arg, dec->dropped_frames); } static int mp_property_mistimed_frame_count(void *ctx, struct m_property *prop, @@ -2182,8 +2184,8 @@ static int get_track_entry(int item, int action, void *arg, void *ctx) track->stream ? *track->stream->codec : (struct mp_codec_params){0}; const char *decoder_desc = NULL; - if (track->d_video) - decoder_desc = track->d_video->decoder_desc; + if (track->dec) + decoder_desc = track->dec->decoder_desc; if (track->d_audio) decoder_desc = track->d_audio->decoder_desc; @@ -2367,7 +2369,7 @@ static int mp_property_hwdec(void *ctx, struct m_property *prop, { MPContext *mpctx = ctx; struct track *track = mpctx->current_track[0][STREAM_VIDEO]; - struct dec_video *vd = track ? track->d_video : NULL; + struct mp_decoder_wrapper *dec = track ? track->dec : NULL; struct MPOpts *opts = mpctx->opts; if (action == M_PROPERTY_SET) { @@ -2379,10 +2381,10 @@ static int mp_property_hwdec(void *ctx, struct m_property *prop, talloc_free(opts->hwdec_api); opts->hwdec_api = talloc_strdup(NULL, new); - if (!vd) + if (!dec) return M_PROPERTY_OK; - video_vd_control(vd, VDCTRL_REINIT, NULL); + mp_decoder_wrapper_control(dec, VDCTRL_REINIT, NULL); double last_pts = mpctx->last_vo_pts; if (last_pts != MP_NOPTS_VALUE) queue_seek(mpctx, MPSEEK_ABSOLUTE, last_pts, MPSEEK_EXACT, 0); @@ -2397,13 +2399,13 @@ static int mp_property_hwdec_current(void *ctx, struct m_property *prop, { MPContext *mpctx = ctx; struct track *track = mpctx->current_track[0][STREAM_VIDEO]; - struct dec_video *vd = track ? track->d_video : NULL; + struct mp_decoder_wrapper *dec = track ? track->dec : NULL; - if (!vd) + if (!dec) return M_PROPERTY_UNAVAILABLE; char *current = NULL; - video_vd_control(vd, VDCTRL_GET_HWDEC, ¤t); + mp_decoder_wrapper_control(dec, VDCTRL_GET_HWDEC, ¤t); if (!current) current = "no"; return m_property_strdup_ro(action, arg, current); @@ -2547,7 +2549,7 @@ static int mp_property_video_codec(void *ctx, struct m_property *prop, { MPContext *mpctx = ctx; struct track *track = mpctx->current_track[0][STREAM_VIDEO]; - const char *c = track && track->d_video ? track->d_video->decoder_desc : NULL; + const char *c = track && track->dec ? track->dec->decoder_desc : NULL; return m_property_strdup_ro(action, arg, c); } @@ -2620,8 +2622,8 @@ static int mp_property_dec_imgparams(void *ctx, struct m_property *prop, MPContext *mpctx = ctx; struct mp_image_params p = {0}; struct vo_chain *vo_c = mpctx->vo_chain; - if (vo_c && vo_c->video_src) - video_get_dec_params(vo_c->video_src, &p); + if (vo_c && vo_c->track) + mp_decoder_wrapper_get_video_dec_params(vo_c->track->dec, &p); if (!p.imgfmt) return M_PROPERTY_UNAVAILABLE; return property_imgparams(p, action, arg); @@ -2984,9 +2986,8 @@ static int mp_property_aspect(void *ctx, struct m_property *prop, } } struct track *track = mpctx->current_track[0][STREAM_VIDEO]; - if (track && track->d_video && aspect <= 0) { - struct dec_video *d_video = track->d_video; - struct mp_codec_params *c = d_video->header->codec; + if (track && track->stream && aspect <= 0) { + struct mp_codec_params *c = track->stream->codec; if (c->disp_w && c->disp_h) aspect = (float)c->disp_w / c->disp_h; } @@ -5807,8 +5808,8 @@ void mp_option_change_callback(void *ctx, struct m_config_option *co, int flags) if (flags & UPDATE_IMGPAR) { struct track *track = mpctx->current_track[0][STREAM_VIDEO]; - if (track && track->d_video) { - video_reset_params(track->d_video); + if (track && track->dec) { + mp_decoder_wrapper_reset_params(track->dec); mp_force_video_refresh(mpctx); } } diff --git a/player/core.h b/player/core.h index 0560393bd3..f27c30b145 100644 --- a/player/core.h +++ b/player/core.h @@ -153,7 +153,7 @@ struct track { struct dec_sub *d_sub; // Current decoding state (NULL if selected==false) - struct dec_video *d_video; + struct mp_decoder_wrapper *dec; struct dec_audio *d_audio; // Where the decoded result goes to (one of them is not NULL if active) @@ -170,7 +170,6 @@ struct track { struct vo_chain { struct mp_log *log; - struct mp_hwdec_devices *hwdec_devs; double container_fps; struct mp_output_chain *filter; @@ -178,19 +177,13 @@ struct vo_chain { //struct vf_chain *vf; struct vo *vo; - // 1-element input frame queue. - struct mp_image *input_mpi; - struct track *track; struct mp_pin *filter_src; - bool filter_src_got_eof; // whether this returned EOF last time - struct dec_video *video_src; + struct mp_pin *dec_src; // - video consists of a single picture, which should be shown only once // - do not sync audio to video in any way bool is_coverart; - // Just to avoid decoding the coverart picture again after a seek. - struct mp_image *cached_coverart; }; // Like vo_chain, for audio. @@ -351,7 +344,6 @@ typedef struct MPContext { // Number of mistimed frames. int mistimed_frames_total; bool hrseek_active; // skip all data until hrseek_pts - bool hrseek_framedrop; // allow decoder to drop frames before hrseek_pts bool hrseek_lastframe; // drop everything until last frame reached bool hrseek_backstep; // go to frame before seek target double hrseek_pts; @@ -366,8 +358,6 @@ typedef struct MPContext { // How much video timing has been changed to make it match the audio // timeline. Used for status line information only. double total_avsync_change; - // Used to compute the number of frames dropped in a row. - int dropped_frames_start; // A-V sync difference when last frame was displayed. Kept to display // the same value if the status line is updated at a time where no new // video frame is shown. diff --git a/player/loadfile.c b/player/loadfile.c index ad1b20ac8e..d35ae6ad6b 100644 --- a/player/loadfile.c +++ b/player/loadfile.c @@ -45,13 +45,13 @@ #include "audio/decode/dec_audio.h" #include "audio/out/ao.h" +#include "filters/f_decoder_wrapper.h" #include "filters/f_lavfi.h" #include "filters/filter_internal.h" #include "demux/demux.h" #include "stream/stream.h" #include "sub/dec_sub.h" #include "external_files.h" -#include "video/decode/dec_video.h" #include "video/out/vo.h" #include "core.h" @@ -984,9 +984,9 @@ static void cleanup_deassociated_complex_filters(struct MPContext *mpctx) for (int n = 0; n < mpctx->num_tracks; n++) { struct track *track = mpctx->tracks[n]; if (!(track->sink || track->vo_c || track->ao_c)) { - if (track->d_video && !track->vo_c) { - video_uninit(track->d_video); - track->d_video = NULL; + if (track->dec && !track->vo_c) { + talloc_free(track->dec->f); + track->dec->f = NULL; } if (track->d_audio && !track->ao_c) { audio_uninit(track->d_audio); @@ -996,7 +996,7 @@ static void cleanup_deassociated_complex_filters(struct MPContext *mpctx) } } - if (mpctx->vo_chain && !mpctx->vo_chain->video_src && + if (mpctx->vo_chain && !mpctx->vo_chain->dec_src && !mpctx->vo_chain->filter_src) { uninit_video_chain(mpctx); @@ -1079,18 +1079,16 @@ static int reinit_complex_filters(struct MPContext *mpctx, bool force_uninit) struct mp_pin *pad = mp_filter_get_named_pin(mpctx->lavfi, "vo"); if (pad && mp_pin_get_dir(pad) == MP_PIN_OUT) { if (mpctx->vo_chain) { - if (mpctx->vo_chain->video_src) { - MP_ERR(mpctx, "Pad vo tries to connect to already used VO.\n"); - goto done; - } + MP_ERR(mpctx, "Pad vo tries to connect to already used VO.\n"); + goto done; } else { reinit_video_chain_src(mpctx, NULL); if (!mpctx->vo_chain) goto done; } - mp_pin_set_manual_connection(pad, true); struct vo_chain *vo_c = mpctx->vo_chain; vo_c->filter_src = pad; + mp_pin_connect(vo_c->filter->f->pins[0], vo_c->filter_src); } pad = mp_filter_get_named_pin(mpctx->lavfi, "ao"); @@ -1112,8 +1110,9 @@ static int reinit_complex_filters(struct MPContext *mpctx, bool force_uninit) for (int n = 0; n < mpctx->num_tracks; n++) { struct track *track = mpctx->tracks[n]; if (track->sink && track->type == STREAM_VIDEO) { - if (!track->d_video && !init_video_decoder(mpctx, track)) + if (!track->dec && !init_video_decoder(mpctx, track)) goto done; + mp_pin_connect(track->sink, track->dec->f->pins[0]); } if (track->sink && track->type == STREAM_AUDIO) { if (!track->d_audio && !init_audio_decoder(mpctx, track)) @@ -1587,8 +1586,8 @@ static void set_track_recorder_sink(struct track *track, { if (track->d_sub) sub_set_recorder_sink(track->d_sub, sink); - if (track->d_video) - track->d_video->recorder_sink = sink; + if (track->dec) + track->dec->recorder_sink = sink; if (track->d_audio) track->d_audio->recorder_sink = sink; track->remux_sink = sink; @@ -1633,7 +1632,7 @@ void open_recorder(struct MPContext *mpctx, bool on_init) for (int n = 0; n < mpctx->num_tracks; n++) { struct track *track = mpctx->tracks[n]; if (track->stream && track->selected && - (track->d_sub || track->d_video || track->d_audio)) + (track->d_sub || track->dec || track->d_audio)) { MP_TARRAY_APPEND(NULL, streams, num_streams, track->stream); } diff --git a/player/main.c b/player/main.c index 5f79e784ed..98abbc8e4f 100644 --- a/player/main.c +++ b/player/main.c @@ -42,6 +42,7 @@ #include "common/msg.h" #include "common/msg_control.h" #include "common/global.h" +#include "filters/f_decoder_wrapper.h" #include "options/parse_configfile.h" #include "options/parse_commandline.h" #include "common/playlist.h" @@ -54,7 +55,6 @@ #include "demux/demux.h" #include "stream/stream.h" #include "sub/osd.h" -#include "video/decode/dec_video.h" #include "video/out/vo.h" #include "core.h" diff --git a/player/osd.c b/player/osd.c index 875bc446db..fd6b421849 100644 --- a/player/osd.c +++ b/player/osd.c @@ -30,6 +30,7 @@ #include "options/options.h" #include "common/common.h" #include "options/m_property.h" +#include "filters/f_decoder_wrapper.h" #include "common/encode.h" #include "osdep/terminal.h" @@ -39,7 +40,6 @@ #include "stream/stream.h" #include "sub/osd.h" -#include "video/decode/dec_video.h" #include "video/out/vo.h" #include "core.h" @@ -221,8 +221,9 @@ static char *get_term_status_msg(struct MPContext *mpctx) talloc_free(r); } int64_t c = vo_get_drop_count(mpctx->video_out); - struct dec_video *d_video = mpctx->vo_chain->video_src; - int dropped_frames = d_video ? d_video->dropped_frames : 0; + struct mp_decoder_wrapper *dec = mpctx->vo_chain->track + ? mpctx->vo_chain->track->dec : NULL; + int dropped_frames = dec ? dec->dropped_frames : 0; if (c > 0 || dropped_frames > 0) { saddf(&line, " Dropped: %"PRId64, c); if (dropped_frames) diff --git a/player/playloop.c b/player/playloop.c index a75263cc5f..748469354d 100644 --- a/player/playloop.c +++ b/player/playloop.c @@ -29,6 +29,7 @@ #include "common/common.h" #include "common/encode.h" #include "common/recorder.h" +#include "filters/f_decoder_wrapper.h" #include "options/m_config.h" #include "options/m_property.h" #include "common/playlist.h" @@ -43,7 +44,6 @@ #include "demux/demux.h" #include "stream/stream.h" #include "sub/osd.h" -#include "video/decode/dec_video.h" #include "video/out/vo.h" #include "core.h" @@ -213,8 +213,6 @@ void add_step_frame(struct MPContext *mpctx, int dir) void reset_playback_state(struct MPContext *mpctx) { for (int n = 0; n < mpctx->num_tracks; n++) { - if (mpctx->tracks[n]->d_video) - video_reset(mpctx->tracks[n]->d_video); if (mpctx->tracks[n]->d_audio) audio_reset_decoding(mpctx->tracks[n]->d_audio); mpctx->tracks[n]->sink_eof = false; @@ -227,7 +225,6 @@ void reset_playback_state(struct MPContext *mpctx) reset_subtitle_state(mpctx); mpctx->hrseek_active = false; - mpctx->hrseek_framedrop = false; mpctx->hrseek_lastframe = false; mpctx->hrseek_backstep = false; mpctx->current_seek = (struct seek_params){0}; @@ -376,13 +373,22 @@ static void mp_seek(MPContext *mpctx, struct seek_params seek) if (hr_seek) { mpctx->hrseek_active = true; - mpctx->hrseek_framedrop = !hr_seek_very_exact && opts->hr_seek_framedrop; mpctx->hrseek_backstep = seek.type == MPSEEK_BACKSTEP; mpctx->hrseek_pts = seek_pts; + // allow decoder to drop frames before hrseek_pts + bool hrseek_framedrop = !hr_seek_very_exact && opts->hr_seek_framedrop; + MP_VERBOSE(mpctx, "hr-seek, skipping to %f%s%s\n", mpctx->hrseek_pts, - mpctx->hrseek_framedrop ? "" : " (no framedrop)", + hrseek_framedrop ? "" : " (no framedrop)", mpctx->hrseek_backstep ? " (backstep)" : ""); + + for (int n = 0; n < mpctx->num_tracks; n++) { + struct track *track = mpctx->tracks[n]; + struct mp_decoder_wrapper *dec = track->dec; + if (dec && hrseek_framedrop) + mp_decoder_wrapper_set_start_pts(dec, mpctx->hrseek_pts); + } } if (mpctx->stop_play == AT_END_OF_FILE) @@ -1079,9 +1085,9 @@ static void handle_complex_filter_decoders(struct MPContext *mpctx) struct track *track = mpctx->tracks[n]; if (!track->selected) continue; - if (!track->sink || !mp_pin_in_needs_data(track->sink)) - continue; if (track->d_audio) { + if (!track->sink || !mp_pin_in_needs_data(track->sink)) + continue; audio_work(track->d_audio); struct mp_aframe *fr; int res = audio_get_frame(track->d_audio, &fr); @@ -1096,21 +1102,6 @@ static void handle_complex_filter_decoders(struct MPContext *mpctx) mp_wakeup_core(mpctx); } } - if (track->d_video) { - video_work(track->d_video); - struct mp_image *fr; - int res = video_get_frame(track->d_video, &fr); - if (res == DATA_OK) { - mp_pin_in_write(track->sink, MAKE_FRAME(MP_FRAME_VIDEO, fr)); - track->sink_eof = false; - } else if (res == DATA_EOF) { - if (!track->sink_eof) - mp_pin_in_write(track->sink, MP_EOF_FRAME); - track->sink_eof = true; - } else if (res == DATA_AGAIN) { - mp_wakeup_core(mpctx); - } - } } } diff --git a/player/screenshot.c b/player/screenshot.c index 5234b39d8e..16ff357dc8 100644 --- a/player/screenshot.c +++ b/player/screenshot.c @@ -34,7 +34,6 @@ #include "options/path.h" #include "video/mp_image.h" #include "video/mp_image_pool.h" -#include "video/decode/dec_video.h" #include "video/out/vo.h" #include "video/image_writer.h" #include "sub/osd.h" diff --git a/player/sub.c b/player/sub.c index 2d644e3e00..65b8ecc78f 100644 --- a/player/sub.c +++ b/player/sub.c @@ -33,7 +33,6 @@ #include "sub/dec_sub.h" #include "demux/demux.h" #include "video/mp_image.h" -#include "video/decode/dec_video.h" #include "core.h" diff --git a/player/video.c b/player/video.c index 483043f358..48b02ecec7 100644 --- a/player/video.c +++ b/player/video.c @@ -39,8 +39,7 @@ #include "stream/stream.h" #include "sub/osd.h" #include "video/hwdec.h" -#include "video/decode/dec_video.h" -#include "video/decode/vd.h" +#include "filters/f_decoder_wrapper.h" #include "video/out/vo.h" #include "audio/decode/dec_audio.h" @@ -55,7 +54,6 @@ enum { VD_PROGRESS = 1, // progress, but no output; repeat call with no waiting VD_NEW_FRAME = 2, // the call produced a new frame VD_WAIT = 3, // no EOF, but no output; wait until wakeup - VD_RECONFIG = 4, }; static const char av_desync_help_text[] = @@ -93,17 +91,7 @@ int reinit_video_filters(struct MPContext *mpctx) static void vo_chain_reset_state(struct vo_chain *vo_c) { - mp_image_unrefp(&vo_c->input_mpi); vo_seek_reset(vo_c->vo); - - if (vo_c->video_src) - video_reset(vo_c->video_src); - - // Prepare for continued playback after a seek. - if (!vo_c->input_mpi && vo_c->cached_coverart) - vo_c->input_mpi = mp_image_new_ref(vo_c->cached_coverart); - - vo_c->filter_src_got_eof = false; } void reset_video_state(struct MPContext *mpctx) @@ -123,7 +111,6 @@ void reset_video_state(struct MPContext *mpctx) mpctx->num_past_frames = 0; mpctx->total_avsync_change = 0; mpctx->last_av_difference = 0; - mpctx->dropped_frames_start = 0; mpctx->mistimed_frames_total = 0; mpctx->drop_message_shown = 0; mpctx->display_sync_drift_dir = 0; @@ -148,16 +135,15 @@ static void vo_chain_uninit(struct vo_chain *vo_c) if (track) { assert(track->vo_c == vo_c); track->vo_c = NULL; - assert(track->d_video == vo_c->video_src); - track->d_video = NULL; - video_uninit(vo_c->video_src); + if (vo_c->dec_src) + assert(track->dec->f->pins[0] == vo_c->dec_src); + talloc_free(track->dec->f); + track->dec = NULL; } if (vo_c->filter_src) mp_pin_disconnect(vo_c->filter_src); - mp_image_unrefp(&vo_c->input_mpi); - mp_image_unrefp(&vo_c->cached_coverart); talloc_free(vo_c->filter->f); talloc_free(vo_c); // this does not free the VO @@ -178,36 +164,25 @@ void uninit_video_chain(struct MPContext *mpctx) int init_video_decoder(struct MPContext *mpctx, struct track *track) { - assert(!track->d_video); + assert(!track->dec); if (!track->stream) goto err_out; - track->d_video = talloc_zero(NULL, struct dec_video); - struct dec_video *d_video = track->d_video; - d_video->global = mpctx->global; - d_video->log = mp_log_new(d_video, mpctx->log, "!vd"); - d_video->opts = mpctx->opts; - d_video->header = track->stream; - d_video->codec = track->stream->codec; - d_video->fps = d_video->header->codec->fps; + struct mp_filter *parent = mpctx->filter_root; + // If possible, set this as parent so the decoder gets the hwdec and DR + // interfaces. // Note: at least mpv_opengl_cb_uninit_gl() relies on being able to get // rid of all references to the VO by destroying the VO chain. Thus, // decoders not linked to vo_chain must not use the hwdec context. - if (mpctx->vo_chain) { - d_video->hwdec_devs = mpctx->vo_chain->hwdec_devs; - d_video->vo = mpctx->vo_chain->vo; - } - - MP_VERBOSE(d_video, "Container reported FPS: %f\n", d_video->fps); + if (track->vo_c) + parent = track->vo_c->filter->f; - if (d_video->opts->force_fps) { - d_video->fps = d_video->opts->force_fps; - MP_INFO(mpctx, "FPS forced to %5.3f.\n", d_video->fps); - MP_INFO(mpctx, "Use --no-correct-pts to force FPS based timing.\n"); - } + track->dec = mp_decoder_wrapper_create(parent, track->stream); + if (!track->dec) + goto err_out; - if (!video_init_best_codec(d_video)) + if (!mp_decoder_wrapper_reinit(track->dec)) goto err_out; return 1; @@ -216,8 +191,6 @@ err_out: if (track->sink) mp_pin_disconnect(track->sink); track->sink = NULL; - video_uninit(track->d_video); - track->d_video = NULL; error_on_track(mpctx, track); return 0; } @@ -232,6 +205,14 @@ void reinit_video_chain(struct MPContext *mpctx) reinit_video_chain_src(mpctx, track); } +static void filter_update_subtitles(void *ctx, double pts) +{ + struct MPContext *mpctx = ctx; + + if (osd_get_render_subs_in_filter(mpctx->osd)) + update_subtitles(mpctx, pts); +} + // (track=NULL creates a blank chain, used for lavfi-complex) void reinit_video_chain_src(struct MPContext *mpctx, struct track *track) { @@ -266,8 +247,8 @@ void reinit_video_chain_src(struct MPContext *mpctx, struct track *track) mp_output_chain_create(mpctx->filter_root, MP_OUTPUT_CHAIN_VIDEO); vo_c->filter->container_fps = vo_c->container_fps; mp_output_chain_set_vo(vo_c->filter, vo_c->vo); - - vo_c->hwdec_devs = vo_c->vo->hwdec_devs; + vo_c->filter->update_subtitles = filter_update_subtitles; + vo_c->filter->update_subtitles_ctx = mpctx; if (track) { vo_c->track = track; @@ -275,12 +256,14 @@ void reinit_video_chain_src(struct MPContext *mpctx, struct track *track) if (!init_video_decoder(mpctx, track)) goto err_out; - vo_c->video_src = track->d_video; - vo_c->container_fps = vo_c->video_src->fps; + vo_c->dec_src = track->dec->f->pins[0]; + vo_c->container_fps = track->dec->fps; vo_c->is_coverart = !!track->stream->attached_picture; track->vo_c = vo_c; vo_c->track = track; + + mp_pin_connect(vo_c->filter->f->pins[0], vo_c->dec_src); } #if HAVE_ENCODING @@ -328,129 +311,23 @@ void mp_force_video_refresh(struct MPContext *mpctx) } } -static bool check_framedrop(struct MPContext *mpctx, struct vo_chain *vo_c) +static void check_framedrop(struct MPContext *mpctx, struct vo_chain *vo_c) { struct MPOpts *opts = mpctx->opts; // check for frame-drop: if (mpctx->video_status == STATUS_PLAYING && !mpctx->paused && mpctx->audio_status == STATUS_PLAYING && !ao_untimed(mpctx->ao) && - vo_c->video_src) + vo_c->track && vo_c->track->dec && (opts->frame_dropping & 2)) { float fps = vo_c->container_fps; - double frame_time = fps > 0 ? 1.0 / fps : 0; - // we should avoid dropping too many frames in sequence unless we - // are too late. and we allow 100ms A-V delay here: - int dropped_frames = - vo_c->video_src->dropped_frames - mpctx->dropped_frames_start; - if (mpctx->last_av_difference - 0.100 > dropped_frames * frame_time) - return !!(opts->frame_dropping & 2); - } - return false; -} - -// Read a packet, store decoded image into d_video->waiting_decoded_mpi -// returns VD_* code -static int decode_image(struct MPContext *mpctx) -{ - struct vo_chain *vo_c = mpctx->vo_chain; - if (vo_c->input_mpi) - return VD_PROGRESS; - - int res = DATA_EOF; - if (vo_c->filter_src) { - struct mp_frame frame = mp_pin_out_read(vo_c->filter_src); - if (frame.type == MP_FRAME_EOF) { - res = DATA_EOF; - vo_c->filter_src_got_eof = true; - } else if (frame.type == MP_FRAME_VIDEO) { - res = DATA_OK; - vo_c->input_mpi = frame.data; - vo_c->filter_src_got_eof = false; - } else if (frame.type) { - MP_ERR(vo_c, "unexpected frame type\n"); - mp_frame_unref(&frame); - res = DATA_EOF; - } else { - res = vo_c->filter_src_got_eof ? DATA_EOF : DATA_WAIT; - } - } else if (vo_c->video_src) { - struct dec_video *d_video = vo_c->video_src; - bool hrseek = mpctx->hrseek_active && mpctx->hrseek_framedrop && - mpctx->video_status == STATUS_SYNCING; - video_set_start(d_video, hrseek ? mpctx->hrseek_pts : MP_NOPTS_VALUE); - - video_set_framedrop(d_video, check_framedrop(mpctx, vo_c)); - - video_work(d_video); - res = video_get_frame(d_video, &vo_c->input_mpi); - } - - switch (res) { - case DATA_WAIT: return VD_WAIT; - case DATA_OK: - case DATA_STARVE: - case DATA_AGAIN: return VD_PROGRESS; - case DATA_EOF: return VD_EOF; - default: abort(); - } -} - -static int video_filter(struct MPContext *mpctx, bool eof) -{ - struct vo_chain *vo_c = mpctx->vo_chain; - - if (vo_c->input_mpi || eof) { - struct mp_frame frame = {MP_FRAME_VIDEO, vo_c->input_mpi}; - if (!vo_c->input_mpi) { - frame = MP_EOF_FRAME; - if (vo_c->filter->got_input_eof) - return vo_c->filter->got_output_eof ? VD_EOF : VD_WAIT; - } - if (mp_pin_in_needs_data(vo_c->filter->f->pins[0])) { - if (osd_get_render_subs_in_filter(mpctx->osd)) - update_subtitles(mpctx, vo_c->input_mpi->pts); - mp_pin_in_write(vo_c->filter->f->pins[0], frame); - vo_c->input_mpi = NULL; - return VD_PROGRESS; - } - } - - return VD_WAIT; -} - -// Make sure at least 1 filtered image is available, decode new video if needed. -// returns VD_* code -// A return value of VD_PROGRESS doesn't necessarily output a frame, but makes -// the promise that calling this function again will eventually do something. -static int video_decode_and_filter(struct MPContext *mpctx) -{ - struct vo_chain *vo_c = mpctx->vo_chain; - - int r = video_filter(mpctx, false); - if (r < 0) - return r; - - if (!vo_c->input_mpi) { - if (vo_c->cached_coverart) { - // Don't ever decode it twice, not even after seek resets. - // (On seek resets, input_mpi is set to the cached image.) - r = VD_EOF; - } else { - // Decode a new image, or at least feed the decoder a packet. - r = decode_image(mpctx); - if (r == VD_WAIT) - return r; - } - } - - if (vo_c->input_mpi) { - if (vo_c->is_coverart && !vo_c->cached_coverart) - vo_c->cached_coverart = mp_image_new_ref(vo_c->input_mpi); - } else if (r == VD_EOF) { - r = video_filter(mpctx, true); + // it's a crappy heuristic; avoid getting upset by incorrect fps + if (fps <= 20 || fps >= 500) + return; + double frame_time = 1.0 / fps; + // try to drop as many frames as we appear to be behind + vo_c->track->dec->attempt_framedrops = + MPCLAMP((mpctx->last_av_difference - 0.010) / frame_time, 0, 100); } - - return r; } /* Modify video timing to match the audio timeline. There are two main @@ -511,9 +388,6 @@ static void handle_new_frame(struct MPContext *mpctx) mpctx->time_frame += frame_time / mpctx->video_speed; adjust_sync(mpctx, pts, frame_time); } - struct dec_video *d_video = mpctx->vo_chain->video_src; - if (d_video) - mpctx->dropped_frames_start = d_video->dropped_frames; MP_TRACE(mpctx, "frametime=%5.3f\n", frame_time); } @@ -586,18 +460,11 @@ static int video_output_image(struct MPContext *mpctx) int r = VD_PROGRESS; if (needs_new_frame(mpctx)) { // Filter a new frame. - if (!mp_pin_out_request_data(vo_c->filter->f->pins[1])) { - r = video_decode_and_filter(mpctx); - if (r < 0) - return r; // error - } struct mp_image *img = NULL; struct mp_frame frame = mp_pin_out_read(vo_c->filter->f->pins[1]); - if (frame.type == MP_FRAME_NONE && vo_c->filter->got_output_eof) - frame = MP_EOF_FRAME; - if (frame.type == MP_FRAME_NONE) - return video_decode_and_filter(mpctx); - if (frame.type == MP_FRAME_EOF) { + if (frame.type == MP_FRAME_NONE) { + r = vo_c->filter->got_output_eof ? VD_EOF : VD_WAIT; + } else if (frame.type == MP_FRAME_EOF) { r = VD_EOF; } else if (frame.type == MP_FRAME_VIDEO) { img = frame.data; @@ -657,11 +524,11 @@ static bool check_for_hwdec_fallback(struct MPContext *mpctx) { struct vo_chain *vo_c = mpctx->vo_chain; - if (!vo_c->filter->failed_output_conversion || !vo_c->video_src) + if (!vo_c->filter->failed_output_conversion || !vo_c->track) return false; - if (video_vd_control(vo_c->video_src, VDCTRL_FORCE_HWDEC_FALLBACK, NULL) - != CONTROL_OK) + if (mp_decoder_wrapper_control(vo_c->track->dec, + VDCTRL_FORCE_HWDEC_FALLBACK, NULL) != CONTROL_OK) return false; mp_output_chain_reset_harder(vo_c->filter); @@ -1213,11 +1080,8 @@ void write_video(struct MPContext *mpctx) // wait until VO wakes us up to get more frames // (NB: in theory, the 1st frame after display sync mode change uses the // wrong waiting mode) - if (!vo_is_ready_for_frame(vo, mpctx->display_sync_active ? -1 : p