From 23a7257cca5982fa44300825ea489ba95a7e4c17 Mon Sep 17 00:00:00 2001 From: wm4 Date: Tue, 15 Jul 2014 01:49:02 +0200 Subject: Revert "Remove DVD and Bluray support" This reverts commit 4b93210e0c244a65ef10a566abed2ad25ecaf9a1. *shrug* --- demux/demux.c | 2 + demux/demux_disc.c | 347 ++++++++++++++++++ old-configure | 10 + options/options.c | 11 + options/options.h | 6 + player/command.c | 133 +++++++ player/configfiles.c | 6 + player/core.h | 9 + player/discnav.c | 334 +++++++++++++++++ player/loadfile.c | 7 + player/misc.c | 4 + player/playloop.c | 2 + stream/cache.c | 30 ++ stream/stream.c | 15 + stream/stream.h | 9 + stream/stream_bluray.c | 831 ++++++++++++++++++++++++++++++++++++++++++ stream/stream_dvd.c | 968 +++++++++++++++++++++++++++++++++++++++++++++++++ stream/stream_dvdnav.c | 755 ++++++++++++++++++++++++++++++++++++++ sub/osd.c | 11 + sub/osd.h | 8 + wscript | 13 + wscript_build.py | 6 + 22 files changed, 3517 insertions(+) create mode 100644 demux/demux_disc.c create mode 100644 player/discnav.c create mode 100644 stream/stream_bluray.c create mode 100644 stream/stream_dvd.c create mode 100644 stream/stream_dvdnav.c diff --git a/demux/demux.c b/demux/demux.c index b7f184c161..e716194614 100644 --- a/demux/demux.c +++ b/demux/demux.c @@ -52,12 +52,14 @@ extern const demuxer_desc_t demuxer_desc_lavf; extern const demuxer_desc_t demuxer_desc_libass; extern const demuxer_desc_t demuxer_desc_subreader; extern const demuxer_desc_t demuxer_desc_playlist; +extern const demuxer_desc_t demuxer_desc_disc; /* Please do not add any new demuxers here. If you want to implement a new * demuxer, add it to libavformat, except for wrappers around external * libraries and demuxers requiring binary support. */ const demuxer_desc_t *const demuxer_list[] = { + &demuxer_desc_disc, &demuxer_desc_edl, &demuxer_desc_cue, &demuxer_desc_rawaudio, diff --git a/demux/demux_disc.c b/demux/demux_disc.c new file mode 100644 index 0000000000..06cea65d1a --- /dev/null +++ b/demux/demux_disc.c @@ -0,0 +1,347 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with mpv. If not, see . + */ + +#include +#include + +#include "common/common.h" +#include "common/msg.h" + +#include "stream/stream.h" +#include "demux.h" +#include "stheader.h" + +#include "video/csputils.h" + +struct priv { + struct demuxer *slave; + // streams[slave_stream_index] == our_stream + struct sh_stream **streams; + int num_streams; + // This contains each DVD sub stream, or NULL. Needed because DVD packets + // can come arbitrarily late in the MPEG stream, so the slave demuxer + // might add the streams only later. + struct sh_stream *dvd_subs[32]; + // Used to rewrite the raw MPEG timestamps to playback time. + struct { + double base_time; // playback display start time of current segment + double base_dts; // packet DTS that maps to base_time + double last_dts; // DTS of previously demuxed packet + } pts[STREAM_TYPE_COUNT]; + double seek_pts; + bool seek_reinit; // needs reinit after seek +}; + +static void reselect_streams(demuxer_t *demuxer) +{ + struct priv *p = demuxer->priv; + for (int n = 0; n < MPMIN(p->slave->num_streams, p->num_streams); n++) { + if (p->streams[n]) { + demuxer_select_track(p->slave, p->slave->streams[n], + demux_stream_is_selected(p->streams[n])); + } + } +} + +static void get_disc_lang(struct stream *stream, struct sh_stream *sh) +{ + struct stream_lang_req req = {.type = sh->type, .id = sh->demuxer_id}; + if (stream->uncached_type == STREAMTYPE_DVD && sh->type == STREAM_SUB) + req.id = req.id & 0x1F; // mpeg ID to index + stream_control(stream, STREAM_CTRL_GET_LANG, &req); + if (req.name[0]) + sh->lang = talloc_strdup(sh, req.name); +} + +static void add_dvd_streams(demuxer_t *demuxer) +{ + struct priv *p = demuxer->priv; + struct stream *stream = demuxer->stream; + if (stream->uncached_type != STREAMTYPE_DVD) + return; + struct stream_dvd_info_req info; + if (stream_control(stream, STREAM_CTRL_GET_DVD_INFO, &info) > 0) { + for (int n = 0; n < MPMIN(32, info.num_subs); n++) { + struct sh_stream *sh = new_sh_stream(demuxer, STREAM_SUB); + if (!sh) + break; + sh->demuxer_id = n + 0x20; + sh->codec = "dvd_subtitle"; + get_disc_lang(stream, sh); + // p->streams _must_ match with p->slave->streams, so we can't add + // it yet - it has to be done when the real stream appears, which + // could be right on start, or any time later. + p->dvd_subs[n] = sh; + + // emulate the extradata + struct mp_csp_params csp = MP_CSP_PARAMS_DEFAULTS; + csp.int_bits_in = 8; + csp.int_bits_out = 8; + float cmatrix[3][4]; + mp_get_yuv2rgb_coeffs(&csp, cmatrix); + + char *s = talloc_strdup(sh, ""); + s = talloc_asprintf_append(s, "palette: "); + for (int i = 0; i < 16; i++) { + int color = info.palette[i]; + int c[3] = {(color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff}; + mp_map_int_color(cmatrix, 8, c); + color = (c[2] << 16) | (c[1] << 8) | c[0]; + + if (i != 0) + s = talloc_asprintf_append(s, ", "); + s = talloc_asprintf_append(s, "%06x", color); + } + s = talloc_asprintf_append(s, "\n"); + + sh->sub->extradata = s; + sh->sub->extradata_len = strlen(s); + } + } +} + +static void add_streams(demuxer_t *demuxer) +{ + struct priv *p = demuxer->priv; + + for (int n = p->num_streams; n < p->slave->num_streams; n++) { + struct sh_stream *src = p->slave->streams[n]; + if (src->sub) { + struct sh_stream *sub = NULL; + if (src->demuxer_id >= 0x20 && src->demuxer_id <= 0x3F) + sub = p->dvd_subs[src->demuxer_id - 0x20]; + if (sub) { + assert(p->num_streams == n); // directly mapped + MP_TARRAY_APPEND(p, p->streams, p->num_streams, sub); + continue; + } + } + struct sh_stream *sh = new_sh_stream(demuxer, src->type); + assert(p->num_streams == n); // directly mapped + MP_TARRAY_APPEND(p, p->streams, p->num_streams, sh); + // Copy all stream fields that might be relevant + sh->codec = talloc_strdup(sh, src->codec); + sh->format = src->format; + sh->lav_headers = src->lav_headers; + if (sh && src->video) { + double ar; + if (stream_control(demuxer->stream, STREAM_CTRL_GET_ASPECT_RATIO, &ar) + == STREAM_OK) + sh->video->aspect = ar; + } + if (sh && src->audio) + sh->audio = src->audio; + } + reselect_streams(demuxer); +} + +static void d_seek(demuxer_t *demuxer, float rel_seek_secs, int flags) +{ + struct priv *p = demuxer->priv; + + if (demuxer->stream->uncached_type == STREAMTYPE_CDDA) { + demux_seek(p->slave, rel_seek_secs, flags); + return; + } + + double pts = p->seek_pts; + if (flags & SEEK_ABSOLUTE) + pts = 0.0f; + + if (flags & SEEK_FACTOR) { + double tmp = 0; + stream_control(demuxer->stream, STREAM_CTRL_GET_TIME_LENGTH, &tmp); + pts += tmp * rel_seek_secs; + } else { + pts += rel_seek_secs; + } + + MP_VERBOSE(demuxer, "seek to: %f\n", pts); + + stream_control(demuxer->stream, STREAM_CTRL_SEEK_TO_TIME, &pts); + demux_control(p->slave, DEMUXER_CTRL_RESYNC, NULL); + + p->seek_pts = pts; + p->seek_reinit = true; +} + +static void reset_pts(demuxer_t *demuxer) +{ + struct priv *p = demuxer->priv; + + double base; + if (stream_control(demuxer->stream, STREAM_CTRL_GET_CURRENT_TIME, &base) < 1) + base = 0; + + MP_VERBOSE(demuxer, "reset to time: %f\n", base); + + for (int n = 0; n < STREAM_TYPE_COUNT; n++) { + p->pts[n].base_dts = p->pts[n].last_dts = MP_NOPTS_VALUE; + p->pts[n].base_time = base; + } + + p->seek_reinit = false; +} + +static int d_fill_buffer(demuxer_t *demuxer) +{ + struct priv *p = demuxer->priv; + + struct demux_packet *pkt = demux_read_any_packet(p->slave); + if (!pkt) + return 0; + + if (p->seek_reinit) + reset_pts(demuxer); + + add_streams(demuxer); + if (pkt->stream >= p->num_streams) { // out of memory? + talloc_free(pkt); + return 0; + } + + struct sh_stream *sh = p->streams[pkt->stream]; + if (!demux_stream_is_selected(sh)) { + talloc_free(pkt); + return 1; + } + + int t = sh->type; + + // Subtitle timestamps are not continuous, so the heuristic below can't be + // applied. Instead, use the video stream as reference. + if (t == STREAM_SUB) + t = STREAM_VIDEO; + + MP_TRACE(demuxer, "ipts: %d %f %f\n", sh->type, pkt->pts, pkt->dts); + + if (sh->type == STREAM_SUB) { + if (p->pts[t].base_dts == MP_NOPTS_VALUE) + MP_WARN(demuxer, "subtitle packet along PTS reset, report a bug\n"); + } else if (pkt->dts != MP_NOPTS_VALUE) { + if (p->pts[t].base_dts == MP_NOPTS_VALUE) + p->pts[t].base_dts = p->pts[t].last_dts = pkt->dts; + + if (pkt->dts < p->pts[t].last_dts || pkt->dts > p->pts[t].last_dts + 0.5) + { + MP_VERBOSE(demuxer, "PTS discontinuity on stream %d\n", sh->type); + p->pts[t].base_time += p->pts[t].last_dts - p->pts[t].base_dts; + p->pts[t].base_dts = p->pts[t].last_dts = pkt->dts - pkt->duration; + } + p->pts[t].last_dts = pkt->dts; + } + if (p->pts[t].base_dts != MP_NOPTS_VALUE) { + double delta = -p->pts[t].base_dts + p->pts[t].base_time; + if (pkt->pts != MP_NOPTS_VALUE) + pkt->pts += delta; + if (pkt->dts != MP_NOPTS_VALUE) + pkt->dts += delta; + } + + MP_TRACE(demuxer, "opts: %d %f %f\n", sh->type, pkt->pts, pkt->dts); + + if (pkt->pts != MP_NOPTS_VALUE) + p->seek_pts = pkt->pts; + + demux_add_packet(sh, pkt); + return 1; +} + +static void add_stream_chapters(struct demuxer *demuxer) +{ + int num = 0; + if (stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_CHAPTERS, &num) < 1) + return; + for (int n = 0; n < num; n++) { + double p = n; + if (stream_control(demuxer->stream, STREAM_CTRL_GET_CHAPTER_TIME, &p) < 1) + continue; + demuxer_add_chapter(demuxer, bstr0(""), p * 1e9, 0, 0); + } +} + +static int d_open(demuxer_t *demuxer, enum demux_check check) +{ + struct priv *p = demuxer->priv = talloc_zero(demuxer, struct priv); + + if (check != DEMUX_CHECK_FORCE) + return -1; + + char *demux = "+lavf"; + if (demuxer->stream->uncached_type == STREAMTYPE_CDDA) + demux = "+rawaudio"; + + // Initialize the playback time. We need to read _some_ data to get the + // correct stream-layer time (at least with libdvdnav). + stream_peek(demuxer->stream, 1); + reset_pts(demuxer); + + p->slave = demux_open(demuxer->stream, demux, NULL, demuxer->global); + if (!p->slave) + return -1; + + // So that we don't miss initial packets of delayed subtitle streams. + p->slave->stream_select_default = true; + + // Can be seekable even if the stream isn't. + demuxer->seekable = true; + + add_dvd_streams(demuxer); + add_streams(demuxer); + add_stream_chapters(demuxer); + + return 0; +} + +static void d_close(demuxer_t *demuxer) +{ + struct priv *p = demuxer->priv; + free_demuxer(p->slave); +} + +static int d_control(demuxer_t *demuxer, int cmd, void *arg) +{ + struct priv *p = demuxer->priv; + + switch (cmd) { + case DEMUXER_CTRL_GET_TIME_LENGTH: { + double len; + if (stream_control(demuxer->stream, STREAM_CTRL_GET_TIME_LENGTH, &len) < 1) + break; + *(double *)arg = len; + return DEMUXER_CTRL_OK; + } + case DEMUXER_CTRL_RESYNC: + demux_flush(p->slave); + break; // relay to slave demuxer + case DEMUXER_CTRL_SWITCHED_TRACKS: + reselect_streams(demuxer); + return DEMUXER_CTRL_OK; + } + return demux_control(p->slave, cmd, arg); +} + +const demuxer_desc_t demuxer_desc_disc = { + .name = "disc", + .desc = "CD/DVD/BD wrapper", + .fill_buffer = d_fill_buffer, + .open = d_open, + .close = d_close, + .seek = d_seek, + .control = d_control, + .type = DEMUXER_TYPE_DISC, +}; diff --git a/old-configure b/old-configure index 8de6ac7eda..a57be03a56 100755 --- a/old-configure +++ b/old-configure @@ -179,6 +179,9 @@ options_state_machine() { opt_yes_no _libquvi4 "libquvi 0.4.x" opt_yes_no _libquvi9 "libquvi 0.9.x" opt_yes_no _lcms2 "LCMS2 support" + opt_yes_no _bluray "Blu-ray support" + opt_yes_no _dvdread "libdvdread" + opt_yes_no _dvdnav "libdvdnav" opt_yes_no _enca "ENCA charset oracle library" opt_yes_no _libass "subtitle rendering with libass" opt_yes_no _libpostproc "postprocess filter (vf_pp)" @@ -727,6 +730,12 @@ check_pkg_config "OpenAL" $_openal OPENAL 'openal >= 1.13' check_pkg_config "ALSA audio" $_alsa ALSA 'alsa >= 1.0.9' +check_pkg_config "Blu-ray support" $_bluray LIBBLURAY 'libbluray >= 0.2.1' + +check_pkg_config "dvdread" $_dvdread DVDREAD 'dvdread >= 4.1.0' + +check_pkg_config "dvdnav" $_dvdnav DVDNAV 'dvdnav >= 4.2.0' + check_pkg_config "libcdio" $_libcdio CDDA 'libcdio_paranoia' _oldass=$_libass @@ -952,6 +961,7 @@ cat > $TMPC << EOF #define HAVE_SDL1 0 #define DEFAULT_CDROM_DEVICE "/dev/cdrom" +#define DEFAULT_DVD_DEVICE "/dev/dvd" #define PATH_DEV_DSP "/dev/dsp" #define PATH_DEV_MIXER "/dev/mixer" diff --git a/options/options.c b/options/options.c index 21adaf76c4..84eda8db64 100644 --- a/options/options.c +++ b/options/options.c @@ -147,9 +147,18 @@ const m_option_t mp_opts[] = { ({"no", 0})), OPT_INTRANGE("cache-pause-restart", stream_cache_unpause, 0, 0, 0x7fffffff), +#if HAVE_DVDREAD || HAVE_DVDNAV + OPT_STRING("dvd-device", dvd_device, 0), + OPT_INT("dvd-speed", dvd_speed, 0), + OPT_INTRANGE("dvd-angle", dvd_angle, 0, 1, 99), +#endif /* HAVE_DVDREAD */ OPT_INTPAIR("chapter", chapterrange, 0), OPT_CHOICE_OR_INT("edition", edition_id, 0, 0, 8190, ({"auto", -1})), +#if HAVE_LIBBLURAY + OPT_STRING("bluray-device", bluray_device, 0), + OPT_INTRANGE("bluray-angle", bluray_angle, 0, 0, 999), +#endif /* HAVE_LIBBLURAY */ OPT_STRINGLIST("http-header-fields", network_http_header_fields, 0), OPT_STRING("user-agent", network_useragent, 0), @@ -631,6 +640,8 @@ const struct MPOpts mp_default_opts = { .index_mode = 1, + .dvd_angle = 1, + .mf_fps = 1.0, }; diff --git a/options/options.h b/options/options.h index 9cd5f800d9..100fded4a3 100644 --- a/options/options.h +++ b/options/options.h @@ -251,6 +251,12 @@ typedef struct MPOpts { struct dvb_params *stream_dvb_opts; char *cdrom_device; + int dvd_title; + int dvd_angle; + int dvd_speed; + char *dvd_device; + int bluray_angle; + char *bluray_device; double mf_fps; char *mf_type; diff --git a/player/command.c b/player/command.c index facc890e8a..fffa63bb70 100644 --- a/player/command.c +++ b/player/command.c @@ -232,6 +232,13 @@ static int mp_property_media_title(void *ctx, struct m_property *prop, name = demux_info_get(mpctx->master_demuxer, "title"); if (name && name[0]) return m_property_strdup_ro(action, arg, name); + struct stream *stream = mpctx->master_demuxer->stream; + if (stream_control(stream, STREAM_CTRL_GET_DISC_NAME, &name) > 0 + && name) { + int r = m_property_strdup_ro(action, arg, name); + talloc_free(name); + return r; + } } return mp_property_filename(ctx, prop, action, arg); } @@ -478,6 +485,48 @@ static int mp_property_playback_time(void *ctx, struct m_property *prop, return property_time(action, arg, get_playback_time(mpctx)); } +/// Current BD/DVD title (RW) +static int mp_property_disc_title(void *ctx, struct m_property *prop, + int action, void *arg) +{ + MPContext *mpctx = ctx; + struct demuxer *demuxer = mpctx->master_demuxer; + if (!demuxer || !demuxer->stream) + return M_PROPERTY_UNAVAILABLE; + struct stream *stream = demuxer->stream; + unsigned int title = -1; + switch (action) { + case M_PROPERTY_GET: + if (stream_control(stream, STREAM_CTRL_GET_CURRENT_TITLE, &title) <= 0) + return M_PROPERTY_UNAVAILABLE; + *(int*)arg = title; + return M_PROPERTY_OK; + case M_PROPERTY_GET_TYPE: + *(struct m_option *)arg = (struct m_option){ + .type = CONF_TYPE_INT, + .flags = M_OPT_MIN, + .min = -1, + }; + return M_PROPERTY_OK; + case M_PROPERTY_SET: + title = *(int*)arg; + if (stream_control(stream, STREAM_CTRL_SET_CURRENT_TITLE, &title) <= 0) + return M_PROPERTY_NOT_IMPLEMENTED; + return M_PROPERTY_OK; + } + return M_PROPERTY_NOT_IMPLEMENTED; +} + +static int mp_property_disc_menu(void *ctx, struct m_property *prop, + int action, void *arg) +{ + MPContext *mpctx = ctx; + int state = mp_nav_in_menu(mpctx); + if (state < 0) + return M_PROPERTY_UNAVAILABLE; + return m_property_flag_ro(action, arg, !!state); +} + /// Current chapter (RW) static int mp_property_chapter(void *ctx, struct m_property *prop, int action, void *arg) @@ -757,6 +806,19 @@ static int mp_property_quvi_format(void *ctx, struct m_property *prop, return mp_property_generic_option(mpctx, prop, action, arg); } +/// Number of titles in BD/DVD +static int mp_property_disc_titles(void *ctx, struct m_property *prop, + int action, void *arg) +{ + MPContext *mpctx = ctx; + struct demuxer *demuxer = mpctx->master_demuxer; + unsigned int num_titles; + if (!demuxer || stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_TITLES, + &num_titles) < 1) + return M_PROPERTY_UNAVAILABLE; + return m_property_int_ro(action, arg, num_titles); +} + /// Number of chapters in file static int mp_property_chapters(void *ctx, struct m_property *prop, int action, void *arg) @@ -780,6 +842,69 @@ static int mp_property_editions(void *ctx, struct m_property *prop, return m_property_int_ro(action, arg, demuxer->num_editions); } +/// Current dvd angle (RW) +static int mp_property_angle(void *ctx, struct m_property *prop, + int action, void *arg) +{ + MPContext *mpctx = ctx; + struct demuxer *demuxer = mpctx->master_demuxer; + if (!demuxer) + return M_PROPERTY_UNAVAILABLE; + + int ris, angles = -1, angle = 1; + + ris = stream_control(demuxer->stream, STREAM_CTRL_GET_NUM_ANGLES, &angles); + if (ris == STREAM_UNSUPPORTED) + return M_PROPERTY_UNAVAILABLE; + + ris = stream_control(demuxer->stream, STREAM_CTRL_GET_ANGLE, &angle); + if (ris == STREAM_UNSUPPORTED) + return -1; + + if (angle < 0 || angles <= 1) + return M_PROPERTY_UNAVAILABLE; + + switch (action) { + case M_PROPERTY_GET: + *(int *) arg = angle; + return M_PROPERTY_OK; + case M_PROPERTY_PRINT: { + *(char **) arg = talloc_asprintf(NULL, "%d/%d", angle, angles); + return M_PROPERTY_OK; + } + case M_PROPERTY_SET: + angle = *(int *)arg; + if (angle < 0 || angle > angles) + return M_PROPERTY_ERROR; + demux_flush(demuxer); + + ris = stream_control(demuxer->stream, STREAM_CTRL_SET_ANGLE, &angle); + if (ris != STREAM_OK) + return M_PROPERTY_ERROR; + + demux_control(demuxer, DEMUXER_CTRL_RESYNC, NULL); + + if (mpctx->d_video) + video_reset_decoding(mpctx->d_video); + + if (mpctx->d_audio) + audio_reset_decoding(mpctx->d_audio); + + return M_PROPERTY_OK; + case M_PROPERTY_GET_TYPE: { + struct m_option opt = { + .type = CONF_TYPE_INT, + .flags = CONF_RANGE, + .min = 1, + .max = angles, + }; + *(struct m_option *)arg = opt; + return M_PROPERTY_OK; + } + } + return M_PROPERTY_NOT_IMPLEMENTED; +} + static int get_tag_entry(int item, int action, void *arg, void *ctx) { struct mp_tags *tags = ctx; @@ -2525,11 +2650,15 @@ static const struct m_property mp_properties[] = { {"time-remaining", mp_property_remaining}, {"playtime-remaining", mp_property_playtime_remaining}, {"playback-time", mp_property_playback_time}, + {"disc-title", mp_property_disc_title}, + {"disc-menu-active", mp_property_disc_menu}, {"chapter", mp_property_chapter}, {"edition", mp_property_edition}, {"quvi-format", mp_property_quvi_format}, + {"disc-titles", mp_property_disc_titles}, {"chapters", mp_property_chapters}, {"editions", mp_property_editions}, + {"angle", mp_property_angle}, {"metadata", mp_property_metadata}, {"chapter-metadata", mp_property_chapter_metadata}, {"vf-metadata", mp_property_vf_metadata}, @@ -3651,6 +3780,10 @@ int run_command(MPContext *mpctx, mp_cmd_t *cmd) mp_input_disable_section(mpctx->input, cmd->args[0].v.s); break; + case MP_CMD_DISCNAV: + mp_nav_user_input(mpctx, cmd->args[0].v.s); + break; + case MP_CMD_VO_CMDLINE: if (mpctx->video_out) { char *s = cmd->args[0].v.s; diff --git a/player/configfiles.c b/player/configfiles.c index b05184c5fb..d1c79c9c9d 100644 --- a/player/configfiles.c +++ b/player/configfiles.c @@ -173,6 +173,7 @@ void mp_load_auto_profiles(struct MPContext *mpctx) static char *mp_get_playback_resume_config_filename(struct mpv_global *global, const char *fname) { + struct MPOpts *opts = global->opts; char *res = NULL; void *tmp = talloc_new(NULL); const char *realpath = fname; @@ -183,6 +184,11 @@ static char *mp_get_playback_resume_config_filename(struct mpv_global *global, goto exit; realpath = mp_path_join(tmp, bstr0(cwd), bstr0(fname)); } + if (bstr_startswith0(bfname, "dvd://")) + realpath = talloc_asprintf(tmp, "%s - %s", realpath, opts->dvd_device); + if (bstr_startswith0(bfname, "br://") || bstr_startswith0(bfname, "bd://") || + bstr_startswith0(bfname, "bluray://")) + realpath = talloc_asprintf(tmp, "%s - %s", realpath, opts->bluray_device); uint8_t md5[16]; av_md5_sum(md5, realpath, strlen(realpath)); char *conf = talloc_strdup(tmp, ""); diff --git a/player/core.h b/player/core.h index 856e970945..84b6123c1c 100644 --- a/player/core.h +++ b/player/core.h @@ -350,6 +350,7 @@ typedef struct MPContext { struct screenshot_ctx *screenshot_ctx; struct command_ctx *command_ctx; struct encode_lavc_context *encode_lavc_ctx; + struct mp_nav_state *nav_state; } MPContext; // audio.c @@ -370,6 +371,14 @@ void mp_write_watch_later_conf(struct MPContext *mpctx); struct playlist_entry *mp_check_playlist_resume(struct MPContext *mpctx, struct playlist *playlist); +// discnav.c +void mp_nav_init(struct MPContext *mpctx); +void mp_nav_reset(struct MPContext *mpctx); +void mp_nav_destroy(struct MPContext *mpctx); +void mp_nav_user_input(struct MPContext *mpctx, char *command); +void mp_handle_nav(struct MPContext *mpctx); +int mp_nav_in_menu(struct MPContext *mpctx); + // loadfile.c void uninit_player(struct MPContext *mpctx, unsigned int mask); struct track *mp_add_subtitles(struct MPContext *mpctx, char *filename); diff --git a/player/discnav.c b/player/discnav.c new file mode 100644 index 0000000000..0b52fed479 --- /dev/null +++ b/player/discnav.c @@ -0,0 +1,334 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with mpv. If not, see . + */ + +#include +#include +#include + +#include "core.h" +#include "command.h" + +#include "common/msg.h" +#include "common/common.h" +#include "input/input.h" + +#include "stream/discnav.h" + +#include "sub/dec_sub.h" +#include "sub/osd.h" + +#include "video/mp_image.h" +#include "video/decode/dec_video.h" + +struct mp_nav_state { + struct mp_log *log; + + int nav_still_frame; + bool nav_eof; + bool nav_menu; + bool nav_draining; + + // Accessed by OSD (possibly separate thread) + // Protected by the given lock + pthread_mutex_t osd_lock; + int hi_visible; + int highlight[4]; // x0 y0 x1 y1 + int vidsize[2]; + int subsize[2]; + struct sub_bitmap *hi_elem; + struct sub_bitmap *overlays[2]; + struct sub_bitmap outputs[3]; +}; + +static inline bool is_valid_size(int size[2]) +{ + return size[0] >= 1 && size[1] >= 1; +} + +static void update_resolution(struct MPContext *mpctx) +{ + struct mp_nav_state *nav = mpctx->nav_state; + int size[2] = {0}; + if (mpctx->d_sub[0]) + sub_control(mpctx->d_sub[0], SD_CTRL_GET_RESOLUTION, size); + if (!is_valid_size(size)) { + struct mp_image_params vid = {0}; + if (mpctx->d_video) + vid = mpctx->d_video->decoder_output; + size[0] = vid.w; + size[1] = vid.h; + } + pthread_mutex_lock(&nav->osd_lock); + nav->vidsize[0] = size[0]; + nav->vidsize[1] = size[1]; + pthread_mutex_unlock(&nav->osd_lock); +} + +// Send update events and such. +static void update_state(struct MPContext *mpctx) +{ + mp_notify_property(mpctx, "disc-menu-active"); +} + +// Return 1 if in menu, 0 if in video, or <0 if no navigation possible. +int mp_nav_in_menu(struct MPContext *mpctx) +{ + return mpctx->nav_state ? mpctx->nav_state->nav_menu : -1; +} + +// Allocate state and enable navigation features. Must happen before +// initializing cache, because the cache would read data. Since stream_dvdnav is +// in a mode which skips all transitions on reading data (before enabling +// navigation), this would skip some menu screens. +void mp_nav_init(struct MPContext *mpctx) +{ + assert(!mpctx->nav_state); + + // dvdnav is interactive + if (mpctx->encode_lavc_ctx) + return; + + struct mp_nav_cmd inp = {MP_NAV_CMD_ENABLE}; + if (stream_control(mpctx->stream, STREAM_CTRL_NAV_CMD, &inp) < 1) + return; + + mpctx->nav_state = talloc_zero(NULL, struct mp_nav_state); + mpctx->nav_state->log = mp_log_new(mpctx->nav_state, mpctx->log, "discnav"); + pthread_mutex_init(&mpctx->nav_state->osd_lock, NULL); + + MP_VERBOSE(mpctx->nav_state, "enabling\n"); + + mp_input_enable_section(mpctx->input, "discnav", 0); + mp_input_set_section_mouse_area(mpctx->input, "discnav-menu", + INT_MIN, INT_MIN, INT_MAX, INT_MAX); + + update_state(mpctx); +} + +void mp_nav_reset(struct MPContext *mpctx) +{ + struct mp_nav_state *nav = mpctx->nav_state; + if (!nav) + return; + struct mp_nav_cmd inp = {MP_NAV_CMD_RESUME}; + stream_control(mpctx->stream, STREAM_CTRL_NAV_CMD, &inp); + osd_set_nav_highlight(mpctx->osd, NULL); + nav->hi_visible = 0; + nav->nav_menu = false; + nav->nav_draining = false; + nav->nav_still_frame = 0; + mp_input_disable_section(mpctx->input, "discnav-menu"); + stream_control(mpctx->stream, STREAM_CTRL_RESUME_CACHE, NULL); + update_state(mpctx); +} + +void mp_nav_destroy(struct MPContext *mpctx) +{ + osd_set_nav_highlight(mpctx->osd, NULL); + if (!mpctx->nav_state) + return; + mp_input_disable_section(mpctx->input, "discnav"); + mp_input_disable_section(mpctx->input, "discnav-menu"); + pthread_mutex_destroy(&mpctx->nav_state->osd_lock); + talloc_free(mpctx->nav_state); + mpctx->nav_state = NULL; + update_state(mpctx); +} + +void mp_nav_user_input(struct MPContext *mpctx, char *command) +{ + struct mp_nav_state *nav = mpctx->nav_state; + if (!nav) + return; + if (strcmp(command, "mouse_move") == 0) { + struct mp_image_params vid = {0}; + if (mpctx->d_video) + vid = mpctx->d_video->decoder_output; + struct mp_nav_cmd inp = {MP_NAV_CMD_MOUSE_POS}; + int x, y; + mp_input_get_mouse_pos(mpctx->input, &x, &y); + osd_coords_to_video(mpctx->osd, vid.w, vid.h, &x, &y); + inp.u.mouse_pos.x = x; + inp.u.mouse_pos.y = y; + stream_control(mpctx->stream, STREAM_CTRL_NAV_CMD, &inp); + } else { + struct mp_nav_cmd inp = {MP_NAV_CMD_MENU}; + inp.u.menu.action = command; + stream_control(mpctx->stream, STREAM_CTRL_NAV_CMD, &inp); + } +} + +void mp_handle_nav(struct MPContext *mpctx) +{ + struct mp_nav_state *nav = mpctx->nav_state; + if (!nav) + return; + while (1) { + struct mp_nav_event *ev = NULL; + stream_control(mpctx->stream, STREAM_CTRL_GET_NAV_EVENT, &ev); + if (!ev) + break; + switch (ev->event) { + case MP_NAV_EVENT_DRAIN: { + nav->nav_draining = true; + MP_VERBOSE(nav, "drain requested\n"); + break; + } + case MP_NAV_EVENT_RESET_ALL: { + mpctx->stop_play = PT_RELOAD_DEMUXER; + MP_VERBOSE(nav, "reload\n"); + // return immediately. + // other events should be handled after reloaded. + talloc_free(ev); + return; + } + case MP_NAV_EVENT_RESET: { + nav->nav_still_frame = 0; + break; + } + case MP_NAV_EVENT_EOF: + nav->nav_eof = true; + break; + case MP_NAV_EVENT_STILL_FRAME: { + int len = ev->u.still_frame.seconds; + MP_VERBOSE(nav, "wait for %d seconds\n", len); + if (len > 0 && nav->nav_still_frame == 0) + nav->nav_still_frame = len; + break; + } + case MP_NAV_EVENT_MENU_MODE: + nav->nav_menu = ev->u.menu_mode.enable; + if (nav->nav_menu) { + mp_input_enable_section(mpctx->input, "discnav-menu", + MP_INPUT_ON_TOP); + } else { + mp_input_disable_section(mpctx->input, "discnav-menu"); + } + update_state(mpctx); + break; + case MP_NAV_EVENT_HIGHLIGHT: { + pthread_mutex_lock(&nav->osd_lock); + MP_VERBOSE(nav, "highlight: %d %d %d - %d %d\n", + ev->u.highlight.display, + ev->u.highlight.sx, ev->u.highlight.sy, + ev->u.highlight.ex, ev->u.highlight.ey); + nav->highlight[0] = ev->u.highlight.sx; + nav->highlight[1] = ev->u.highlight.sy; + nav->highlight[2] = ev->u.highlight.ex; + nav->highlight[3] = ev->u.highlight.ey; + nav->hi_visible = ev->u.highlight.display; + pthread_mutex_unlock(&nav->osd_lock); + update_resolution(mpctx); + osd_set_nav_highlight(mpctx->osd, mpctx); + break; + } + case MP_NAV_EVENT_OVERLAY: { + pthread_mutex_lock(&nav->osd_lock); + for (int i = 0; i < 2; i++) { + if (nav->overlays[i]) + talloc_free(nav->overlays[i]); + nav->overlays[i] = talloc_steal(nav, ev->u.overlay.images[i]); + } + pthread_mutex_unlock(&nav->osd_lock); + update_resolution(mpctx); + osd_set_nav_highlight(mpctx->osd, mpctx); + break; + } + default: ; // ignore + } + talloc_free(ev); + } + update_resolution(mpctx); + if (mpctx->stop_play == AT_END_OF_FILE) { + if (nav->nav_still_frame > 0) { + // gross hack + mpctx->time_frame += nav->nav_still_frame; + mpctx->playing_last_frame = true; + nav->nav_still_frame = -2; + } else if (nav->nav_still_frame == -2) { + struct mp_nav_cmd inp = {MP_NAV_CMD_SKIP_STILL}; + stream_control(mpctx->stream, STREAM_CTRL_NAV_CMD, &inp); + } + } + if (nav->nav_draining && mpctx->stop_play == AT_END_OF_FILE) { + MP_VERBOSE(nav, "execute drain\n"); + struct mp_nav_cmd inp = {MP_NAV_CMD_DRAIN_OK}; + stream_control(mpctx->stream, STREAM_CTRL_NAV_CMD, &inp); + nav->nav_draining = false; + stream_control(mpctx->stream, STREAM_CTRL_RESUME_CACHE, NULL); + } + // E.g. keep displaying still frames + if (mpctx->stop_play == AT_END_OF_FILE && !nav->nav_eof) + mpctx->stop_play = KEEP_PLAYING; +} + +// Render "fake" highlights, because using actual dvd sub highlight elements +// is too hard, and would require changes to libavcodec's dvdsub decoder. +// Note: a proper solution would introduce something like +// SD_CTRL_APPLY_DVDNAV, which would crop the vobsub frame, +// and apply the current CLUT. +void mp_nav_get_highlight(void *priv, struct mp_osd_res res, + struct sub_bitmaps *out_imgs) +{ + struct MPContext *mpctx = priv; + struct mp_nav_state *nav = mpctx->nav_state; + + pthread_mutex_lock(&nav->osd_lock); + + struct sub_bitmap *sub = nav->hi_elem; + if (!sub) + sub = talloc_zero(nav, struct sub_bitmap); + + nav->hi_elem = sub; + if (!is_valid_size(nav->vidsize)) { + pthread_mutex_unlock(&nav->osd_lock); + return; + } + int sizes[2] = {nav->vidsize[0], nav->vidsize[1]}; + if (sizes[0] != nav->subsize[0] || sizes[1] != nav->subsize[1]) { + talloc_free(sub->bitmap); + sub->bitmap = talloc_array(sub, uint32_t, sizes[0] * sizes[1]); + memset(sub->bitmap, 0x80, talloc_get_size(sub->bitmap)); + nav->subsize[0] = sizes[0]; + nav->subsize[1] = sizes[1]; + } + + out_imgs->num_parts = 0; + + if (nav->hi_visible) { + sub->x = nav->highlight[0]; + sub->y = nav->highlight[1]; + sub->w = MPCLAMP(nav->highlight[2] - sub->x, 0, sizes[0]); + sub->h = MPCLAMP(nav->highlight[3] - sub->y, 0, sizes[1]); + sub->stride = sub->w * 4; + if (sub->w > 0 && sub->h > 0) + nav->outputs[out_imgs->num_parts++] = *sub; + } + + if (nav->overlays[0]) + nav->outputs[out_imgs->num_parts++] = *nav->overlays[0]; + if (nav->overlays[1]) + nav->outputs[out_imgs->num_parts++] = *nav->overlays[1]; + + if (out_imgs->num_parts) { + out_imgs->parts = nav->outputs; + out_imgs->format = SUBBITMAP_RGBA; + osd_rescale_bitmaps(out_imgs, sizes[0], sizes[1], res, -1); + } + + pthread_mutex_unlock(&nav->osd_lock); +} diff --git a/player/loadfile.c b/player/loadfile.c index 3f131d1df6..e2a1e6e145 100644 --- a/player/loadfile.c +++ b/player/loadfile.c @@ -1050,6 +1050,9 @@ static void play_current_file(struct MPContext *mpctx) goto terminate_playback; } + // Must be called before enabling cache. + mp_nav_init(mpctx); + int res = stream_enable_cache(&mpctx->stream, &opts->stream_cache); if (res == 0) if (demux_was_interrupted(mpctx)) @@ -1059,6 +1062,8 @@ static void play_current_file(struct MPContext *mpctx) goto_reopen_demuxer: ; + mp_nav_reset(mpctx); + //============ Open DEMUXERS --- DETECT file type ======================= mpctx->audio_delay = opts->audio_delay; @@ -1266,6 +1271,8 @@ goto_reopen_demuxer: ; terminate_playback: + mp_nav_destroy(mpctx); + if (mpctx->stop_play == KEEP_PLAYING) mpctx->stop_play = AT_END_OF_FILE; diff --git a/player/misc.c b/player/misc.c index 3da404f7ea..abaf5b208a 100644 --- a/player/misc.c +++ b/player/misc.c @@ -112,6 +112,10 @@ double get_start_time(struct MPContext *mpctx) struct demuxer *demuxer = mpctx->demuxer; if (!demuxer) return 0; + // We reload the demuxer on menu transitions; don't make it use the first + // timestamp it finds as start PTS. + if (mpctx->nav_state) + return 0; return demuxer->start_time; } diff --git a/player/playloop.c b/player/playloop.c index f992b0b064..7e9b63995b 100644 --- a/player/playloop.c +++ b/player/playloop.c @@ -1224,6 +1224,8 @@ void run_playloop(struct MPContext *mpctx) mpctx->stop_play = AT_END_OF_FILE; } + mp_handle_nav(mpctx); + if (!mpctx->stop_play && !mpctx->restart_playback) { // If no more video is available, one frame means one playloop iteration. diff --git a/stream/cache.c b/stream/cache.c index 8c6a6a63dd..197f7d3391 100644 --- a/stream/cache.c +++ b/stream/cache.c @@ -114,6 +114,8 @@ struct priv { int stream_cache_idle; int stream_cache_fill; struct mp_tags *stream_metadata; + char *disc_name; + double start_pts; }; enum { @@ -169,6 +171,7 @@ static void cache_drop_contents(struct priv *s) { s->offset = s->min_filepos = s->max_filepos = s->read_filepos; s->eof = false; + s->start_pts = MP_NOPTS_VALUE; } // Copy at most dst_size from the cache at the given absolute file position pos. @@ -268,6 +271,14 @@ static bool cache_fill(struct priv *s) len = stream_read_partial(s->stream, &s->buffer[pos], space); pthread_mutex_lock(&s->mutex); + // Do this after reading a block, because at least libdvdnav updates the + // stream position only after actually reading something after a seek. + if (s->start_pts == MP_NOPTS_VALUE) { + double pts; + if (stream_control(s->stream, STREAM_CTRL_GET_CURRENT_TIME, &pts) > 0) + s->start_pts = pts; + } + s->max_filepos += len; if (pos + len == s->buffer_size) s->offset += s->buffer_size; // wrap... @@ -345,6 +356,7 @@ static void update_cached_controls(struct priv *s) int64_t i64; double d; struct mp_tags *tags; + char *t; s->stream_time_length = 0; if (stream_control(s->stream, STREAM_CTRL_GET_TIME_LENGTH, &d) == STREAM_OK) s->stream_time_length = d; @@ -355,6 +367,11 @@ static void update_cached_controls(struct priv *s) talloc_free(s->stream_metadata); s->stream_metadata = talloc_steal(s, tags); } + if (stream_control(s->stream, STREAM_CTRL_GET_DISC_NAME, &t) == STREAM_OK) + { + talloc_free(s->disc_name); + s->disc_name = talloc_steal(s, t); + } s->stream_size = -1; if (stream_control(s->stream, STREAM_CTRL_GET_SIZE, &i64) == STREAM_OK) s->stream_size = i64; @@ -385,6 +402,12 @@ static int cache_get_cached_control(stream_t *cache, int cmd, void *arg) case STREAM_CTRL_GET_NUM_CHAPTERS: *(unsigned int *)arg = s->stream_num_chapters; return STREAM_OK; + case STREAM_CTRL_GET_CURRENT_TIME: { + if (s->start_pts == MP_NOPTS_VALUE) + return STREAM_UNSUPPORTED; + *(double *)arg = s->start_pts; + return STREAM_OK; + } case STREAM_CTRL_GET_METADATA: { if (s->stream_metadata) { ta_set_parent(s->stream_metadata, NULL); @@ -394,6 +417,12 @@ static int cache_get_cached_control(stream_t *cache, int cmd, void *arg) } return STREAM_UNSUPPORTED; } + case STREAM_CTRL_GET_DISC_NAME: { + if (!s->disc_name) + return STREAM_UNSUPPORTED; + *(char **)arg = talloc_strdup(NULL, s->disc_name); + return STREAM_OK; + } case STREAM_CTRL_RESUME_CACHE: s->idle = s->eof = false; pthread_cond_signal(&s->wakeup); @@ -406,6 +435,7 @@ static bool control_needs_flush(int stream_ctrl) { switch (stream_ctrl) { case STREAM_CTRL_SEEK_TO_TIME: + case STREAM_CTRL_SET_ANGLE: case STREAM_CTRL_SET_CURRENT_TITLE: return true; } diff --git a/stream/stream.c b/stream/stream.c index ba35e0f9ea..d600088267 100644 --- a/stream/stream.c +++ b/stream/stream.c @@ -64,6 +64,10 @@ extern const stream_info_t stream_info_ffmpeg; extern const stream_info_t stream_info_avdevice; extern const stream_info_t stream_info_file; extern const stream_info_t stream_info_ifo; +extern const stream_info_t stream_info_dvd; +extern const stream_info_t stream_info_dvdnav; +extern const stream_info_t stream_info_bluray; +extern const stream_info_t stream_info_bdnav; extern const stream_info_t stream_info_rar_filter; extern const stream_info_t stream_info_rar_entry; extern const stream_info_t stream_info_edl; @@ -86,6 +90,17 @@ static const stream_info_t *const stream_list[] = { #if HAVE_LIBSMBCLIENT &stream_info_smb, #endif +#if HAVE_DVDREAD + &stream_info_ifo, + &stream_info_dvd, +#endif +#if HAVE_DVDNAV + &stream_info_dvdnav, +#endif +#if HAVE_LIBBLURAY + &stream_info_bluray, + &stream_info_bdnav, +#endif &stream_info_memory, &stream_info_null, diff --git a/stream/stream.h b/stream/stream.h index c49af132d8..344e8b55ef 100644 --- a/stream/stream.h +++ b/stream/stream.h @@ -33,6 +33,8 @@ enum streamtype { STREAMTYPE_GENERIC = 0, STREAMTYPE_FILE, STREAMTYPE_DVB, + STREAMTYPE_DVD, + STREAMTYPE_BLURAY, STREAMTYPE_TV, STREAMTYPE_MF, STREAMTYPE_EDL, @@ -66,6 +68,9 @@ enum stream_ctrl { STREAM_CTRL_SEEK_TO_TIME, STREAM_CTRL_GET_SIZE, STREAM_CTRL_GET_ASPECT_RATIO, + STREAM_CTRL_GET_NUM_ANGLES, + STREAM_CTRL_GET_ANGLE, + STREAM_CTRL_SET_ANGLE, STREAM_CTRL_GET_NUM_TITLES, STREAM_CTRL_GET_LANG, STREAM_CTRL_GET_CURRENT_TITLE, @@ -77,9 +82,13 @@ enum stream_ctrl { STREAM_CTRL_RESUME_CACHE, STREAM_CTRL_RECONNECT, STREAM_CTRL_GET_CHAPTER_TIME, + STREAM_CTRL_GET_DVD_INFO, STREAM_CTRL_SET_CONTENTS, STREAM_CTRL_GET_METADATA, STREAM_CTRL_GET_BASE_FILENAME, + STREAM_CTRL_GET_NAV_EVENT, // struct mp_nav_event** + STREAM_CTRL_NAV_CMD, // struct mp_nav_cmd* + STREAM_CTRL_GET_DISC_NAME, STREAM_CTRL_TV_SET_SCAN, STREAM_CTRL_SET_TV_FREQ, STREAM_CTRL_GET_TV_FREQ, diff --git a/stream/stream_bluray.c b/stream/stream_bluray.c new file mode 100644 index 0000000000..686541b551 --- /dev/null +++ b/stream/stream_bluray.c @@ -0,0 +1,831 @@ +/* + * Copyright (C) 2010 Benjamin Zores + * + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/* + * Blu-ray parser/reader using libbluray + * Use 'git clone git://git.videolan.org/libbluray' to get it. + * + * TODO: + * - Add descrambled keys database support (KEYDB.cfg) + * + */ + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "config.h" +#include "talloc.h" +#include "common/common.h" +#include "common/msg.h" +#include "options/m_option.h" +#include "options/options.h" +#include "stream.h" +#include "osdep/timer.h" +#include "discnav.h" +#include "sub/osd.h" +#include "sub/img_convert.h" +#include "video/mp_image.h" +#include "video/mp_image_pool.h" + +#define BLURAY_SECTOR_SIZE 6144 + +#define BLURAY_DEFAULT_ANGLE 0 +#define BLURAY_DEFAULT_CHAPTER 0 +#define BLURAY_DEFAULT_TITLE -2 +#define BLURAY_MENU_TITLE -1 + +// 90khz ticks +#define BD_TIMEBASE (90000) +#define BD_TIME_TO_MP(x) ((x) / (double)(BD_TIMEBASE)) +#define BD_TIME_FROM_MP(x) ((uint64_t)(x * BD_TIMEBASE)) + +// copied from aacs.h in libaacs +#define AACS_ERROR_CORRUPTED_DISC -1 /* opening or reading of AACS files failed */ +#define AACS_ERROR_NO_CONFIG -2 /* missing config file */ +#define AACS_ERROR_NO_PK -3 /* no matching processing key */ +#define AACS_ERROR_NO_CERT -4 /* no valid certificate */ +#define AACS_ERROR_CERT_REVOKED -5 /* certificate has been revoked */ +#define AACS_ERROR_MMC_OPEN -6 /* MMC open failed (no MMC drive ?) */ +#define AACS_ERROR_MMC_FAILURE -7 /* MMC failed */ +#define AACS_ERROR_NO_DK -8 /* no matching device key */ + +struct bluray_overlay { + struct sub_bitmap *image; + bool clean, hidden; + int x, y, w, h; +}; + +struct bluray_priv_s { + BLURAY *bd; + BLURAY_TITLE_INFO *title_info; + int num_titles; + int current_angle; + int current_title; + int current_playlist; + + int cfg_title; + char *cfg_device; + + // overlay stuffs + struct bluray_overlay overlays[2], ol_flushed[2]; + struct mp_image_pool *pool; + + // navigation stuffs + uint64_t next_event; + uint32_t still_length; + int mousex, mousey; + bool in_menu, use_nav, nav_enabled, popup_enabled; +}; + +static const struct bluray_priv_s bluray_stream_priv_dflts = { + .cfg_title = BLURAY_DEFAULT_TITLE, +}; + +static const struct bluray_priv_s bdnav_stream_priv_dflts = { + .cfg_title = BLURAY_DEFAULT_TITLE, + .use_nav = true, +}; + +#define OPT_BASE_STRUCT struct bluray_priv_s +static const m_option_t bluray_stream_opts_fields[] = { + OPT_CHOICE_OR_INT("title", cfg_title, 0, 0, 99999, + ({"longest", BLURAY_DEFAULT_TITLE})), + OPT_STRING("device", cfg_device, 0), + {0} +}; + +static const m_option_t bdnav_stream_opts_fields[] = { + OPT_CHOICE_OR_INT("title", cfg_title, 0, 0, 99999, + ({"menu", BLURAY_MENU_TITLE}, + {"first", BLURAY_DEFAULT_TITLE})), + OPT_STRING("device", cfg_device, 0), + {0} +}; + +static void destruct(struct bluray_priv_s *priv) +{ + if (priv->title_info) + bd_free_title_info(priv->title_info); + bd_close(priv->bd); + talloc_free(priv->pool); +} + +inline static int play_title(struct bluray_priv_s *priv, int title) +{ + if (priv->use_nav) { + if (title == priv->num_titles - 1) + title = BLURAY_TITLE_FIRST_PLAY; + return bd_play_title(priv->bd, title); + } else + return bd_select_title(priv->bd, title); +} + +static void overlay_release(struct bluray_overlay *overlay) +{ + if (overlay->image) + talloc_free(overlay->image); + *overlay = (struct bluray_overlay) { .clean = true }; +} + +static void overlay_alloc(struct bluray_priv_s *priv, + struct bluray_overlay *overlay, + int x, int y, int w, int h) +{ + assert(overlay->image == NULL); + struct sub_bitmap *image = talloc_zero(NULL, struct sub_bitmap); + overlay->w = image->w = image->dw = w; + overlay->h = image->h = image->dh = h; + overlay->x = image->x = x; + overlay->y = image->y = y; + struct mp_image *mpi = mp_image_pool_get(priv->pool, IMGFMT_RGBA, w, h); + mpi = talloc_steal(image, mpi); + assert(image->w > 0 && image->h > 0 && mpi != NULL); + image->stride = mpi->stride[0]; + image->bitmap = mpi->planes[0]; + overlay->image = image; + overlay->clean = true; + overlay->hidden = false; +} + +static void overlay_close_all(struct bluray_priv_s *priv) +{ + for (int i = 0; i < 2; i++) + overlay_release(&priv->overlays[i]); +} + +static void overlay_close(struct bluray_priv_s *priv, + const BD_OVERLAY *const bo) +{ + overlay_release(&priv->overlays[bo->plane]); +} + +static inline uint32_t conv_rgba(const BD_PG_PALETTE_ENTRY *p) +{ + uint32_t rgba; + uint8_t *out = (uint8_t*)&rgba; + const int y = p->Y, cb = (int)p->Cb - 128, cr = (int)p->Cr - 128; + // CAUTION: inaccurate but fast, broken in big endian +#define CONV(a) (MPCLAMP((a), 0, 255)*p->T >> 8) + out[0] = CONV(y + cb + (cb >> 1) + (cb >> 2) + (cb >> 6)); + out[1] = CONV(y - ((cb >> 2) + (cb >> 4) + (cb >> 5)) + - ((cr >> 3) + (cr >> 4) + (cr >> 5))); + out[2] = CONV(y + cr + (cr >> 2) + (cr >> 3) + (cr >> 5)); + out[3] = p->T; +#undef CONV + return rgba; +} + +static void overlay_process(void *data, const BD_OVERLAY *const bo) +{ + stream_t *s = data; + struct bluray_priv_s *priv = s->priv; + if (!bo) { + overlay_close_all(priv); + return; + } + struct bluray_overlay *overlay = &priv->overlays[bo->plane]; + switch (bo->cmd) { + case BD_OVERLAY_INIT: + overlay_alloc(priv, overlay, bo->x, bo->y, bo->w, bo->h); + break; + case BD_OVERLAY_CLOSE: + overlay_close(priv, bo); + break; + case BD_OVERLAY_CLEAR: + if (!overlay->clean) { + memset(overlay->image->bitmap, 0, + overlay->image->stride*overlay->h); + overlay->clean = true; + } + break; + case BD_OVERLAY_DRAW: { + if (!bo->img) + break; + overlay->hidden = false; + overlay->clean = false; + struct sub_bitmap *img = overlay->image; + uint32_t *const origin = img->bitmap; + const BD_PG_RLE_ELEM *in = bo->img; + for (int y = 0; y < bo->h; y++) { + uint32_t *out = origin + (img->stride/4) * (y + bo->y) + bo->x; + for (int x = 0; x < bo->w; ) { + uint32_t c = 0; + if (bo->palette[in->color].T) { + c = conv_rgba(&bo->palette[in->color]); + for (int i = 0; i < in->len; i++) + *out++ = c; + } else { + memset(out, 0, in->len*4); + out += in->len; + } + x += in->len; + ++in; + } + } + break; + } + case BD_OVERLAY_WIPE: { + uint32_t *const origin = overlay->image->bitmap; + for (int y = 0; y < bo->h; y++) + memset(origin + overlay->w * (y + bo->y) + bo->x, 0, 4 * bo->w); + break; + } + case BD_OVERLAY_HIDE: + priv->overlays[bo->plane].hidden = true; + break; + case BD_OVERLAY_FLUSH: { + struct bluray_overlay *in = overlay; + struct bluray_overlay *out = &priv->ol_flushed[bo->plane]; + if (out->image && (out->image->stride != in->image->stride || + out->image->h != in->image->h)) + overlay_release(out); + if (!out->image) + overlay_alloc(priv, out, in->x, in->y, in->w, in->h); + const int len = in->image->stride*in->image->h; + memcpy(out->image->bitmap, in->image->bitmap, len); + out->clean = in->clean; + out->hidden = in->hidden; + priv->next_event |= 1 << MP_NAV_EVENT_OVERLAY; + break; + } default: + break; + } +} + +static inline bool set_event_type(struct bluray_priv_s *priv, int type, + struct mp_nav_event *event) +{ + if (!(priv->next_event & (1 << type))) + return false; + priv->next_event &= ~(1 << type); + event->event = type; + return true; +} + +static void fill_next_event(stream_t *s, struct mp_nav_event **ret) +{ + struct bluray_priv_s *priv = s->priv; + struct mp_nav_event e = {0}; + // this should be checked before any other events + if (!set_event_type(priv, MP_NAV_EVENT_RESET_ALL, &e)) + for (int n = 0; n < 30 && !set_event_type(priv, n, &e); n++) ; + switch (e.event) { + case MP_NAV_EVENT_NONE: + return; + case MP_NAV_EVENT_OVERLAY: { + for (int i = 0; i < 2; i++) { + struct bluray_overlay *o = &priv->ol_flushed[i]; + e.u.overlay.images[i] = NULL; + if (!o->clean && !o->hidden) { + e.u.overlay.images[i] = o->image; + o->image = NULL; + } + } + break; + } + case MP_NAV_EVENT_MENU_MODE: + e.u.menu_mode.enable = priv->in_menu; + break; + case MP_NAV_EVENT_STILL_FRAME: + e.u.still_frame.seconds = priv->still_length; + break; + } + *ret = talloc(NULL, struct mp_nav_event); + **ret = e; +} + +static void bluray_stream_close(stream_t *s) +{ + destruct(s->priv); +} + +static void handle_event(stream_t *s, const BD_EVENT *ev) +{ + static const int reset_flags = (1 << MP_NAV_EVENT_RESET_ALL) + | (1 << MP_NAV_EVENT_RESET); + struct bluray_priv_s *b = s->priv; + switch (ev->event) { + case BD_EVENT_MENU: + b->in_menu = ev->param; + b->next_event |= 1 << MP_NAV_EVENT_MENU_MODE; + break; + case BD_EVENT_STILL: + b->still_length = ev->param ? -1 : 0; + if (b->nav_enabled) + b->next_event |= 1 << MP_NAV_EVENT_STILL_FRAME; + break; + case BD_EVENT_STILL_TIME: + b->still_length = ev->param ? -1 : ev->param*1000; + if (b->nav_enabled) + b->next_event |= 1 << MP_NAV_EVENT_STILL_FRAME; + else + bd_read_skip_still(b->bd); + break; + case BD_EVENT_END_OF_TITLE: + overlay_close_all(b); + break; + case BD_EVENT_PLAYLIST: + b->next_event = reset_flags; + b->current_playlist = ev->param; + if (!b->use_nav) + b->current_title = bd_get_current_title(b->bd); + if (b->title_info) + bd_free_title_info(b->title_info); + b->title_info = bd_get_playlist_info(b->bd, b->current_playlist, + b->current_angle); + break; + case BD_EVENT_TITLE: + b->next_event = reset_flags; + if (ev->param == BLURAY_TITLE_FIRST_PLAY) { + if (b->use_nav) + b->current_title = b->num_titles - 1; + else + b->current_title = bd_get_current_title(b->bd); + } else + b->current_title = ev->param; + if (b->title_info) { + bd_free_title_info(b->title_info); + b->title_info = NULL; + } + break; + case BD_EVENT_ANGLE: + b->current_angle = ev->param; + if (b->title_info) { + bd_free_title_info(b->title_info); + b->title_info = bd_get_playlist_info(b->bd, b->current_playlist, + b->current_angle); + } + break; + case BD_EVENT_POPUP: + b->popup_enabled = ev->param; + break; +#if BLURAY_VERSION >= BLURAY_VERSION_CODE(0, 5, 0) + case BD_EVENT_DISCONTINUITY: + b->next_event = reset_flags; + break; +#endif + default: + MP_TRACE(s, "Unhandled event: %d %d\n", ev->event, ev->param); + break; + } +} + +static int bluray_stream_fill_buffer(stream_t *s, char *buf, int len) +{ + struct bluray_priv_s *b = s->priv; + assert(!b->use_nav); + BD_EVENT event; + while (bd_get_event(b->bd, &event)) + handle_event(s, &event); + return bd_read(b->bd, buf, len); +} + +static int bdnav_stream_fill_buffer(stream_t *s, char *buf, int len) +{ + struct bluray_priv_s *b = s->priv; + assert(b->use_nav); + BD_EVENT event; + int read = -1; + for (;;) { + read = bd_read_ext(b->bd, buf, len, &event); + if (read < 0) + return read; + if (read == 0) { + if (event.event == BD_EVENT_NONE) + return 0; // end of stream + handle_event(s, &event); + } else + break; + } + return read; +} + +static bd_vk_key_e translate_nav_menu_action(const char *cmd) +{ + if (strcmp(cmd, "mouse") == 0) + return BD_VK_MOUSE_ACTIVATE; + if (strcmp(cmd, "up") == 0) + return BD_VK_UP; + if (strcmp(cmd, "down") == 0) + return BD_VK_DOWN; + if (strcmp(cmd, "left") == 0) + return BD_VK_LEFT; + if (strcmp(cmd, "right") == 0) + return BD_VK_RIGHT; + if (strcmp(cmd, "select") == 0) + return BD_VK_ENTER; + return BD_VK_NONE; +} + +static void handle_nav_command(stream_t *s, struct mp_nav_cmd *ev) +{ + struct bluray_priv_s *priv = s->priv; + switch (ev->event) { + case MP_NAV_CMD_ENABLE: + priv->nav_enabled = true; + break; + case MP_NAV_CMD_MENU: { + const int64_t pts = mp_time_us(); + const char *action = ev->u.menu.action; + bd_vk_key_e key = translate_nav_menu_action(action); + if (key != BD_VK_NONE) { + if (key == BD_VK_MOUSE_ACTIVATE) + bd_mouse_select(priv->bd, pts, priv->mousex, priv->mousey); + bd_user_input(priv->bd, pts, key); + } else if (strcmp(action, "menu") == 0) { + if (priv->popup_enabled) + bd_user_input(priv->bd, pts, BD_VK_POPUP); + else + bd_menu_call(priv->bd, pts); + } + break; + } case MP_NAV_CMD_MOUSE_POS: + priv->mousex = ev->u.mouse_pos.x; + priv->mousey = ev->u.mouse_pos.y; + bd_mouse_select(priv->bd, mp_time_us(), priv->mousex, priv->mousey); + break; + case MP_NAV_CMD_SKIP_STILL: + bd_read_skip_still(priv->bd); + break; + } +} + +static int bluray_stream_control(stream_t *s, int cmd, void *arg) +{ + struct bluray_priv_s *b = s->priv; + + switch (cmd) { + case STREAM_CTRL_GET_NUM_CHAPTERS: { + const BLURAY_TITLE_INFO *ti = b->title_info; + if (!ti) + return STREAM_UNSUPPORTED; + *((unsigned int *) arg) = ti->chapter_count; + return STREAM_OK; + } + case STREAM_CTRL_GET_CHAPTER_TIME: { + const BLURAY_TITLE_INFO *ti = b->title_info; + if (!ti) + return STREAM_UNSUPPORTED; + int chapter = *(double *)arg; + double time = MP_NOPTS_VALUE; + if (chapter >= 0 || chapter < ti->chapter_count) + time = BD_TIME_TO_MP(ti->chapters[chapter].start); + if (time == MP_NOPTS_VALUE) + return STREAM_ERROR; + *(double *)arg = time; + return STREAM_OK; + } + case STREAM_CTRL_SET_CURRENT_TITLE: { + const uint32_t title = *((unsigned int*)arg); + if (title >= b->num_titles || !play_title(b, title)) + return STREAM_UNSUPPORTED; + b->current_title = title; + return STREAM_OK; + } + case STREAM_CTRL_GET_CURRENT_TITLE: { + *((unsigned int *) arg) = b->current_title; + return STREAM_OK; + } + case STREAM_CTRL_GET_NUM_TITLES: { + *((unsigned int *)arg) = b->num_titles; + return STREAM_OK; + } + case STREAM_CTRL_GET_TIME_LENGTH: { + const BLURAY_TITLE_INFO *ti = b->title_info; + if (!ti) + return STREAM_UNSUPPORTED; + *((double *) arg) = BD_TIME_TO_MP(ti->duration); + return STREAM_OK; + } + case STREAM_CTRL_GET_CURRENT_TIME: { + *((double *) arg) = BD_TIME_TO_MP(bd_tell_time(b->bd)); + return STREAM_OK; + } + case STREAM_CTRL_SEEK_TO_TIME: { + double pts = *((double *) arg); + bd_seek_time(b->bd, BD_TIME_FROM_MP(pts)); + stream_drop_buffers(s); + // API makes it hard to determine seeking success + return STREAM_OK; + } + case STREAM_CTRL_GET_NUM_ANGLES: { + const BLURAY_TITLE_INFO *ti = b->title_info; + if (!ti) + return STREAM_UNSUPPORTED; + *((int *) arg) = ti->angle_count; + return STREAM_OK; + } + case STREAM_CTRL_GET_ANGLE: { + *((int *) arg) = b->current_angle; + return STREAM_OK; + } + case STREAM_CTRL_SET_ANGLE: { + const BLURAY_TITLE_INFO *ti = b->title_info; + if (!ti) + return STREAM_UNSUPPORTED; + int angle = *((int *) arg); + if (angle < 0 || angle > ti->angle_count) + return STREAM_UNSUPPORTED; + b->current_angle = angle; + bd_seamless_angle_change(b->bd, angle); + return STREAM_OK; + } + case STREAM_CTRL_GET_LANG: { + const BLURAY_TITLE_INFO *ti = b->title_info; + if (ti && ti->clip_count) { + struct stream_lang_req *req = arg; + BLURAY_STREAM_INFO *si = NULL; + int count = 0; + switch (req->type) { + case STREAM_AUDIO: + count = ti->clips[0].audio_stream_count; + si = ti->clips[0].audio_streams; + break; + case STREAM_SUB: + count = ti->clips[0].pg_stream_count; + si = ti->clips[0].pg_streams; + break; + } + for (int n = 0; n < count; n++) { + BLURAY_STREAM_INFO *i = &si[n]; + if (i->pid == req->id) { + snprintf(req->name, sizeof(req->name), "%.4s", i->lang); + return STREAM_OK; + } + } + } + return STREAM_ERROR; + } + case STREAM_CTRL_GET_DISC_NAME: { + const struct meta_dl *meta = bd_get_meta(b->bd); + if (!meta || !meta->di_name || !meta->di_name[0]) + break; + *(char**)arg = talloc_strdup(NULL, meta->di_name); + return STREAM_OK; + } + case STREAM_CTRL_NAV_CMD: + if (!b->use_nav) + return STREAM_UNSUPPORTED; + handle_nav_command(s, arg); + return STREAM_OK; + case STREAM_CTRL_GET_NAV_EVENT: { + struct mp_nav_event **ev = arg; + if (ev) + fill_next_event(s, ev); + return STREAM_OK; + } + default: + break; + } + + return STREAM_UNSUPPORTED; +} + +static const char *aacs_strerr(int err) +{ + switch (err) { + case AACS_ERROR_CORRUPTED_DISC: return "opening or reading of AACS files failed"; + case AACS_ERROR_NO_CONFIG: return "missing config file"; + case AACS_ERROR_NO_PK: return "no matching processing key"; + case AACS_ERROR_NO_CERT: return "no valid certificate"; + case AACS_ERROR_CERT_REVOKED: return "certificate has been revoked"; + case AACS_ERROR_MMC_OPEN: return "MMC open failed (maybe no MMC drive?)"; + case AACS_ERROR_MMC_FAILURE: return "MMC failed"; + case AACS_ERROR_NO_DK: return "no matching device key"; + default: return "unknown error"; + } +} + +static bool check_disc_info(stream_t *s) +{ + struct bluray_priv_s *b = s->priv; + const BLURAY_DISC_INFO *info = bd_get_disc_info(b->bd); + + // check Blu-ray + if (!info->bluray_detected) { + MP_ERR(s, "Given stream is not a Blu-ray.\n"); + return false; + } + + // check AACS + if (info->aacs_detected) { + if (!info->libaacs_detected) { + MP_ERR(s, "AACS encryption detected but cannot find libaacs.\n"); + return false; + } + if (!info->aacs_handled) { + MP_ERR(s, "AACS error: %s\n", aacs_strerr(info->aacs_error_code)); + return false; +