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* --- 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 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 2608 insertions(+) create mode 100644 stream/stream_bluray.c create mode 100644 stream/stream_dvd.c create mode 100644 stream/stream_dvdnav.c (limited to 'stream') 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; + } + } + + // check BD+ + if (info->bdplus_detected) { + if (!info->libbdplus_detected) { + MP_ERR(s, "BD+ encryption detected but cannot find libbdplus.\n"); + return false; + } + if (!info->bdplus_handled) { + MP_ERR(s, "Cannot decrypt BD+ encryption.\n"); + return false; + } + } + + return true; +} + +static void select_initial_title(stream_t *s, int title_guess) { + struct bluray_priv_s *b = s->priv; + + int title = -1; + if (b->use_nav) { + if (b->cfg_title == BLURAY_MENU_TITLE) + title = 0; // BLURAY_TITLE_TOP_MENU + else if (b->cfg_title == BLURAY_DEFAULT_TITLE) + title = b->num_titles - 1; + else + title = b->cfg_title; + } else { + if (b->cfg_title != BLURAY_DEFAULT_TITLE ) + title = b->cfg_title; + else + title = title_guess; + } + if (title < 0) + return; + + if (play_title(b, title)) + b->current_title = title; + else { + MP_WARN(s, "Couldn't start title '%d'.\n", title); + if (!b->use_nav) // cannot query title info in nav + b->current_title = bd_get_current_title(b->bd); + } +} + +static void select_initial_angle(stream_t *s) { + struct bluray_priv_s *b = s->priv; + if (!b->use_nav) // no way to figure out current title info + return; + BLURAY_TITLE_INFO *info = bd_get_title_info(b->bd, b->current_title, 0); + if (!info) + return; + /* Select angle */ + unsigned int angle = s->opts->bluray_angle; + if (!angle) + angle = BLURAY_DEFAULT_ANGLE; + angle = FFMIN(angle, info->angle_count); + if (angle) + bd_select_angle(b->bd, angle); + b->current_angle = bd_get_current_angle(b->bd); + bd_free_title_info(info); +} + +static int bluray_stream_open(stream_t *s) +{ + struct bluray_priv_s *b = s->priv; + + const char *device = NULL; + /* find the requested device */ + if (b->cfg_device && b->cfg_device[0]) + device = b->cfg_device; + else if (s->opts->bluray_device && s->opts->bluray_device[0]) + device = s->opts->bluray_device; + + if (!device) { + MP_ERR(s, "No Blu-ray device/location was specified ...\n"); + return STREAM_UNSUPPORTED; + } + + /* open device */ + BLURAY *bd = bd_open(device, NULL); + if (!bd) { + MP_ERR(s, "Couldn't open Blu-ray device: %s\n", device); + return STREAM_UNSUPPORTED; + } + b->bd = bd; + + if (!check_disc_info(s)) { + destruct(b); + return STREAM_UNSUPPORTED; + } + + int title_guess = BLURAY_DEFAULT_TITLE; + if (b->use_nav) { + const BLURAY_DISC_INFO *disc_info = bd_get_disc_info(b->bd); + b->num_titles = disc_info->num_hdmv_titles + disc_info->num_bdj_titles; + ++b->num_titles; // for BLURAY_TITLE_TOP_MENU + ++b->num_titles; // for BLURAY_TITLE_FIRST_PLAY + } else { + /* check for available titles on disc */ + b->num_titles = bd_get_titles(bd, TITLES_RELEVANT, 0); + if (!b->num_titles) { + MP_ERR(s, "Can't find any Blu-ray-compatible title here.\n"); + destruct(b); + return STREAM_UNSUPPORTED; + } + + /* parse titles information */ + uint64_t max_duration = 0; + for (int i = 0; i < b->num_titles; i++) { + BLURAY_TITLE_INFO *ti = bd_get_title_info(bd, i, 0); + if (!ti) + continue; + + /* try to guess which title may contain the main movie */ + if (ti->duration > max_duration) { + max_duration = ti->duration; + title_guess = i; + } + + bd_free_title_info(ti); + } + } + + // these should be set before any callback + b->pool = mp_image_pool_new(6); + b->current_angle = -1; + b->current_title = -1; + + // initialize libbluray event queue + bd_get_event(bd, NULL); + + if (b->use_nav) { + if (!bd_play(bd)) { + destruct(b); + return STREAM_ERROR; + } + bd_register_overlay_proc(bd, s, overlay_process); + } + + select_initial_title(s, title_guess); + select_initial_angle(s); + + if (b->use_nav) + s->fill_buffer = bdnav_stream_fill_buffer; + else + s->fill_buffer = bluray_stream_fill_buffer; + s->close = bluray_stream_close; + s->control = bluray_stream_control; + s->type = STREAMTYPE_BLURAY; + s->end_pos = bd_get_title_size(bd); + s->sector_size = BLURAY_SECTOR_SIZE; + s->priv = b; + s->demuxer = "+disc"; + + MP_VERBOSE(s, "Blu-ray successfully opened.\n"); + + return STREAM_OK; +} + +const stream_info_t stream_info_bluray = { + .name = "bd", + .open = bluray_stream_open, + .protocols = (const char*const[]){ "bd", "br", "bluray", NULL }, + .priv_defaults = &bluray_stream_priv_dflts, + .priv_size = sizeof(struct bluray_priv_s), + .options = bluray_stream_opts_fields, + .url_options = (const char*const[]){ + "hostname=title", + "filename=device", + NULL + }, +}; + +const stream_info_t stream_info_bdnav = { + .name = "bdnav", + .open = bluray_stream_open, + .protocols = (const char*const[]){ "bdnav", "brnav", "bluraynav", NULL }, + .priv_defaults = &bdnav_stream_priv_dflts, + .priv_size = sizeof(struct bluray_priv_s), + .options = bdnav_stream_opts_fields, + .url_options = (const char*const[]){ + "hostname=title", + "filename=device", + NULL + }, +}; diff --git a/stream/stream_dvd.c b/stream/stream_dvd.c new file mode 100644 index 0000000000..857d144a65 --- /dev/null +++ b/stream/stream_dvd.c @@ -0,0 +1,968 @@ +/* + * This file is part of MPlayer. + * + * Original author: Benjamin Zores + * + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "config.h" +#include "talloc.h" +#include "common/common.h" +#include "common/msg.h" + +#define FIRST_AC3_AID 128 +#define FIRST_DTS_AID 136 +#define FIRST_MPG_AID 0 +#define FIRST_PCM_AID 160 + +#include "stream.h" +#include "options/m_option.h" +#include "options/options.h" + +#include "stream_dvd_common.h" + +#define LIBDVDREAD_VERSION(maj,min,micro) ((maj)*10000 + (min)*100 + (micro)) +/* + * Try to autodetect the libdvd-0.9.0 library + * (0.9.0 removed the header, and moved the two defines + * DVD_VIDEO_LB_LEN and MAX_UDF_FILE_NAME_LEN from it to + * ) + */ +#ifndef DVDREAD_VERSION +#if defined(DVD_VIDEO_LB_LEN) && defined(MAX_UDF_FILE_NAME_LEN) +#define DVDREAD_VERSION LIBDVDREAD_VERSION(0,9,0) +#else +#define DVDREAD_VERSION LIBDVDREAD_VERSION(0,8,0) +#endif +#endif + +typedef struct { + int id; // 0 - 31 mpeg; 128 - 159 ac3; 160 - 191 pcm + int language; + int type; + int channels; +} stream_language_t; + +typedef struct { + dvd_reader_t *dvd; + dvd_file_t *title; + ifo_handle_t *vmg_file; + tt_srpt_t *tt_srpt; + ifo_handle_t *vts_file; + vts_ptt_srpt_t *vts_ptt_srpt; + pgc_t *cur_pgc; +// + int cur_title; + int cur_cell; + int last_cell; + int cur_pack; + int cell_last_pack; + int cur_pgc_idx; +// Navi: + int packs_left; + dsi_t dsi_pack; + int angle_seek; + unsigned int *cell_times_table; +// audio datas + int nr_of_channels; + stream_language_t audio_streams[32]; +// subtitles + int nr_of_subtitles; + stream_language_t subtitles[32]; + + int dvd_angle; + char *dvd_device_current; + int dvd_speed; + int dvd_title; + + int cfg_title; + char *cfg_device; +} dvd_priv_t; + +static const dvd_priv_t stream_priv_dflts = { + .cfg_title = 0, +}; + +#define OPT_BASE_STRUCT dvd_priv_t +/// URL definition +static const m_option_t stream_opts_fields[] = { + OPT_INTRANGE("title", cfg_title, 0, 0, 99), + OPT_STRING("device", cfg_device, 0), + {0} +}; + +static int dvd_lang_from_aid(stream_t *stream, int id) { + dvd_priv_t *d; + int i; + if (!stream) return 0; + d = stream->priv; + if (!d) return 0; + for(i=0;inr_of_channels;i++) { + if(d->audio_streams[i].id==id) + return d->audio_streams[i].language; + } + return 0; +} + +static int dvd_number_of_subs(stream_t *stream) { + int i; + int maxid = -1; + dvd_priv_t *d; + if (!stream) return -1; + d = stream->priv; + if (!d) return -1; + for (i = 0; i < d->nr_of_subtitles; i++) + if (d->subtitles[i].id > maxid) maxid = d->subtitles[i].id; + return maxid + 1; +} + +static int dvd_lang_from_sid(stream_t *stream, int id) { + int i; + dvd_priv_t *d; + if (!stream) return 0; + d = stream->priv; + if (!d) return 0; + for (i = 0; i < d->nr_of_subtitles; i++) + if (d->subtitles[i].id == id && d->subtitles[i].language) return d->subtitles[i].language; + return 0; +} + +static int dvd_next_cell(stream_t *stream, dvd_priv_t *d) { + int next_cell=d->cur_cell; + + MP_DBG(stream, "dvd_next_cell: next1=0x%X \n",next_cell); + if( d->cur_pgc->cell_playback[ next_cell ].block_type == BLOCK_TYPE_ANGLE_BLOCK ) { + while(next_celllast_cell) { + if( d->cur_pgc->cell_playback[next_cell].block_mode == BLOCK_MODE_LAST_CELL ) + break; + ++next_cell; + } + } + MP_DBG(stream, "dvd_next_cell: next2=0x%X \n",next_cell); + + ++next_cell; + if(next_cell>=d->last_cell) + return -1; // EOF + if(d->cur_pgc->cell_playback[next_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK ) { + next_cell+=d->dvd_angle-1; + if(next_cell>=d->last_cell) + return -1; // EOF + } + MP_DBG(stream, "dvd_next_cell: next3=0x%X \n",next_cell); + return next_cell; +} + +static int dvd_read_sector(stream_t *stream, dvd_priv_t *d, unsigned char *data) +{ + int len; + + if(d->packs_left==0) { + /** + * If we're not at the end of this cell, we can determine the next + * VOBU to display using the VOBU_SRI information section of the + * DSI. Using this value correctly follows the current angle, + * avoiding the doubled scenes in The Matrix, and makes our life + * really happy. + * + * Otherwise, we set our next address past the end of this cell to + * force the code above to go to the next cell in the program. + */ + if(d->dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL) { + d->cur_pack= d->dsi_pack.dsi_gi.nv_pck_lbn + ( d->dsi_pack.vobu_sri.next_vobu & 0x7fffffff ); + MP_DBG(stream, "Navi new pos=0x%X \n",d->cur_pack); + } else { + // end of cell! find next cell! + MP_VERBOSE(stream, "--- END OF CELL !!! ---\n"); + d->cur_pack=d->cell_last_pack+1; + } + } + +read_next: + if(d->cur_pack>d->cell_last_pack) { + // end of cell! + int next=dvd_next_cell(stream, d); + if(next>=0) { + d->cur_cell=next; + // if( d->cur_pgc->cell_playback[d->cur_cell].block_type + // == BLOCK_TYPE_ANGLE_BLOCK ) d->cur_cell+=dvd_angle-1; + d->cur_pack = d->cur_pgc->cell_playback[ d->cur_cell ].first_sector; + d->cell_last_pack=d->cur_pgc->cell_playback[ d->cur_cell ].last_sector; + MP_VERBOSE(stream, "DVD next cell: %d pack: 0x%X-0x%X \n",d->cur_cell,d->cur_pack,d->cell_last_pack); + } else + return -1; // EOF + } + + len = DVDReadBlocks(d->title, d->cur_pack, 1, data); + // only == 0 should indicate an error, but some dvdread version are buggy when used with dvdcss + if(len <= 0) return -1; //error + + if(data[38]==0 && data[39]==0 && data[40]==1 && data[41]==0xBF && + data[1024]==0 && data[1025]==0 && data[1026]==1 && data[1027]==0xBF) { + // found a Navi packet!!! +#if DVDREAD_VERSION >= LIBDVDREAD_VERSION(0,9,0) + navRead_DSI(&d->dsi_pack, &(data[ DSI_START_BYTE ])); +#else + navRead_DSI(&d->dsi_pack, &(data[ DSI_START_BYTE ]), sizeof(dsi_t)); +#endif + if(d->cur_pack != d->dsi_pack.dsi_gi.nv_pck_lbn ) { + MP_VERBOSE(stream, "Invalid NAVI packet! lba=0x%X navi=0x%X \n", + d->cur_pack,d->dsi_pack.dsi_gi.nv_pck_lbn); + } else { + // process! + d->packs_left = d->dsi_pack.dsi_gi.vobu_ea; + MP_DBG(stream, "Found NAVI packet! lba=0x%X len=%d \n",d->cur_pack,d->packs_left); + //navPrint_DSI(&d->dsi_pack); + MP_TRACE(stream, "\r### CELL %d: Navi: %d/%d IFO: %d/%d \n",d->cur_cell, + d->dsi_pack.dsi_gi.vobu_c_idn,d->dsi_pack.dsi_gi.vobu_vob_idn, + d->cur_pgc->cell_position[d->cur_cell].cell_nr, + d->cur_pgc->cell_position[d->cur_cell].vob_id_nr); + + if(d->angle_seek) { + int i,skip=0; + for(i=0;i<9;i++) // check if all values zero: + if((skip=d->dsi_pack.sml_agli.data[i].address)!=0) break; + if(skip && skip!=0x7fffffff) { + // sml_agli table has valid data (at least one non-zero): + d->cur_pack=d->dsi_pack.dsi_gi.nv_pck_lbn+ + d->dsi_pack.sml_agli.data[d->dvd_angle-1].address; + d->angle_seek=0; + d->cur_pack--; + MP_VERBOSE(stream, "Angle-seek synced using sml_agli map! new_lba=0x%X \n",d->cur_pack); + } else { + // check if we're in the right cell, jump otherwise: + if( (d->dsi_pack.dsi_gi.vobu_c_idn==d->cur_pgc->cell_position[d->cur_cell].cell_nr) && + (d->dsi_pack.dsi_gi.vobu_vob_idn==d->cur_pgc->cell_position[d->cur_cell].vob_id_nr) ){ + d->angle_seek=0; + MP_VERBOSE(stream, "Angle-seek synced by cell/vob IDN search! \n"); + } else { + // wrong angle, skip this vobu: + d->cur_pack=d->dsi_pack.dsi_gi.nv_pck_lbn+ + d->dsi_pack.dsi_gi.vobu_ea; + d->angle_seek=2; // DEBUG + } + } + } + } + ++d->cur_pack; + goto read_next; + } + + ++d->cur_pack; + if(d->packs_left>=0) --d->packs_left; + + if(d->angle_seek) { + if(d->angle_seek==2) MP_VERBOSE(stream, "!!! warning! reading packet while angle_seek !!!\n"); + goto read_next; // searching for Navi packet + } + + return d->cur_pack-1; +} + +static int fill_buffer(stream_t *s, char *buf, int len) +{ + int64_t pos; + if (len < 2048) + return -1; + pos = dvd_read_sector(s, s->priv, buf); + if (pos < 0) + return -1; + return 2048; // full sector +} + +static void stream_dvd_close(stream_t *s) { + dvd_priv_t *d = s->priv; + ifoClose(d->vts_file); + ifoClose(d->vmg_file); + DVDCloseFile(d->title); + DVDClose(d->dvd); + if (d->dvd_speed) + dvd_set_speed(s,d->dvd_device_current, -1); /* -1 => restore default */ +} + +static int mp_get_titleset_length(ifo_handle_t *vts_file, tt_srpt_t *tt_srpt, int title_no) +{ + int vts_ttn; ///< title number within video title set + int pgc_no; ///< program chain number + int msec; ///< time length in milliseconds + + msec=0; + if(!vts_file || !tt_srpt) + return 0; + + if(vts_file->vtsi_mat && vts_file->vts_pgcit) + { + vts_ttn = tt_srpt->title[title_no].vts_ttn - 1; + pgc_no = vts_file->vts_ptt_srpt->title[vts_ttn].ptt[0].pgcn - 1; + msec = mp_dvdtimetomsec(&vts_file->vts_pgcit->pgci_srp[pgc_no].pgc->playback_time); + } + return msec; +} + + +static int get_num_chapter(ifo_handle_t *vts_file, tt_srpt_t *tt_srpt, int title_no) +{ + if(!vts_file || !tt_srpt) + return 0; + + if(title_no < 0 || title_no >= tt_srpt->nr_of_srpts) + return 0; + + // map global title to vts title + title_no = tt_srpt->title[title_no].vts_ttn - 1; + + if(title_no < 0 || title_no >= vts_file->vts_ptt_srpt->nr_of_srpts) + return 0; + + return vts_file->vts_ptt_srpt->title[title_no].nr_of_ptts; +} + +// p: in=chapter number, out=PTS +static int get_chapter_time(ifo_handle_t *vts_file, tt_srpt_t *tt_srpt, int title_no, double *p) +{ + unsigned int i, cell, last_cell; + unsigned int t=0; + ptt_info_t *ptt; + pgc_t *pgc; + + title_no = tt_srpt->title[title_no].vts_ttn - 1; + if(vts_file->vts_ptt_srpt->title[title_no].nr_of_ptts < 2) + return 0; + ptt = vts_file->vts_ptt_srpt->title[title_no].ptt; + + int cur = 0; + for(i=0; ivts_ptt_srpt->title[title_no].nr_of_ptts; i++) + { + pgc = vts_file->vts_pgcit->pgci_srp[ptt[i].pgcn-1].pgc; + cell = pgc->program_map[ptt[i].pgn-1]; //here the cell is 1-based + if(ptt[i].pgnnr_of_programs) + last_cell = pgc->program_map[ptt[i].pgn]; + else + last_cell = 0; + while (cell < last_cell) { + if(!(pgc->cell_playback[cell-1].block_type == BLOCK_TYPE_ANGLE_BLOCK && + pgc->cell_playback[cell-1].block_mode != BLOCK_MODE_FIRST_CELL) + ) { + if (cur == *p) { + *p = t / 1000.0; + return 1; + } + t += mp_dvdtimetomsec(&pgc->cell_playback[cell-1].playback_time); + cur++; + } + cell++; + } + } + return 0; +} + +static void list_chapters(stream_t *stream, ifo_handle_t *vts_file, tt_srpt_t *tt_srpt, int title_no) +{ + MP_INFO(stream, "CHAPTERS: "); + for (int n = 0; ; n++) { + double p = n; + int r; + r = get_chapter_time(vts_file, tt_srpt, title_no, &p); + if (!r) + break; + int t = p * 1000; + MP_INFO(stream, "%02d:%02d:%02d.%03d,", t/3600000, (t/60000)%60, (t/1000)%60, t%1000); + } + MP_INFO(stream, "\n"); +} + +static double dvd_get_current_time(stream_t *stream, int cell) +{ + int i, tm; + dvd_priv_t *d = stream->priv; + + tm=0; + if(cell < 0) cell=d->cur_cell; + for(i=0; icur_pgc->cell_playback[i].block_type == BLOCK_TYPE_ANGLE_BLOCK && + d->cur_pgc->cell_playback[i].block_mode != BLOCK_MODE_FIRST_CELL + ) + continue; + tm += d->cell_times_table[i]; + } + tm += mp_dvdtimetomsec(&d->dsi_pack.dsi_gi.c_eltm); + + return (double)tm/1000.0; +} + +static void dvd_seek(stream_t *stream, dvd_priv_t *d, int pos) +{ + d->packs_left=-1; + d->cur_pack=pos; + + // check if we stay in current cell (speedup things, and avoid angle skip) + if(d->cur_pack>d->cell_last_pack || + d->cur_packcur_pgc->cell_playback[ d->cur_cell ].first_sector) { + + // ok, cell change, find the right cell! + cell_playback_t *cell; + for(d->cur_cell=0; d->cur_cell < d->cur_pgc->nr_of_cells; d->cur_cell++) { + cell = &(d->cur_pgc->cell_playback[d->cur_cell]); + if(cell->block_type == BLOCK_TYPE_ANGLE_BLOCK && cell->block_mode != BLOCK_MODE_FIRST_CELL) + continue; + d->cell_last_pack=cell->last_sector; + if(d->cur_packfirst_sector) { + d->cur_pack=cell->first_sector; + break; + } + if(d->cur_pack<=d->cell_last_pack) break; // ok, we find it! :) + } + } + + MP_VERBOSE(stream, "DVD Seek! lba=0x%X cell=%d packs: 0x%X-0x%X \n", + d->cur_pack,d->cur_cell,d->cur_pgc->cell_playback[ d->cur_cell ].first_sector,d->cell_last_pack); + + // if we're in interleaved multi-angle cell, find the right angle chain! + // (read Navi block, and use the seamless angle jump table) + d->angle_seek=1; +} + +static int do_seek(stream_t *s, int64_t newpos) { + stream_drop_buffers(s); + dvd_seek(s, s->priv,newpos/2048); + return 1; +} + +static int dvd_seek_to_time(stream_t *stream, ifo_handle_t *vts_file, double sec) +{ + unsigned int i, j, k, timeunit, ac_time, tmap_sector=0, cell_sector=0, vobu_sector=0; + int t=0; + double tm, duration; + int64_t pos = -1; + dvd_priv_t *d = stream->priv; + vts_tmapt_t *vts_tmapt = vts_file->vts_tmapt; + + if(!vts_file->vts_tmapt || sec < 0) + return 0; + + duration = (double) mp_get_titleset_length(d->vts_file, d->tt_srpt, d->cur_title) / 1000.0f; + if(sec > duration) + return 0; + + i=d->cur_pgc_idx; + timeunit = vts_tmapt->tmap[i].tmu; + for(j = 0; j < vts_tmapt->tmap[i].nr_of_entries; j++) { + ac_time = timeunit * (j + 1); + if(ac_time >= sec) + break; + tmap_sector = vts_tmapt->tmap[i].map_ent[j] & 0x7fffffff; + } + //search enclosing cell + for(i=0; icur_pgc->nr_of_cells; i++) { + if(tmap_sector >= d->cur_pgc->cell_playback[i].first_sector && tmap_sector <= d->cur_pgc->cell_playback[i].last_sector) { + cell_sector = d->cur_pgc->cell_playback[i].first_sector; + break; + } + } + + pos = ((int64_t)cell_sector)<<11; + do_seek(stream, pos); + do { + char buf[2048]; + if (dvd_read_sector(stream, stream->priv, buf) < 0) // skip + break; + t = mp_dvdtimetomsec(&d->dsi_pack.dsi_gi.c_eltm); + } while(!t); + tm = dvd_get_current_time(stream, -1); + + pos = ((int64_t)tmap_sector)<<11; + do_seek(stream, pos); + //now get current time in terms of the cell+cell time offset + memset(&d->dsi_pack.dsi_gi.c_eltm, 0, sizeof(dvd_time_t)); + while(tm <= sec) { + char buf[2048]; + if (dvd_read_sector(stream, stream->priv, buf) < 0) // skip + break; + pos += 2048; + tm = dvd_get_current_time(stream, -1); + }; + tmap_sector = pos >> 11; + + //search closest VOBU sector + k=(vts_file->vts_vobu_admap->last_byte + 1 - VOBU_ADMAP_SIZE)/4; //entries in the vobu admap + for(i=1; ivts_vobu_admap->vobu_start_sectors[i] > tmap_sector) + break; + } + vobu_sector = vts_file->vts_vobu_admap->vobu_start_sectors[i-1]; + pos = ((int64_t)vobu_sector) << 11; + do_seek(stream, pos); + + return 1; +} + +static int control(stream_t *stream,int cmd,void* arg) +{ + dvd_priv_t *d = stream->priv; + switch(cmd) + { + case STREAM_CTRL_GET_TIME_LENGTH: + { + *((double *)arg) = (double) mp_get_titleset_length(d->vts_file, d->tt_srpt, d->cur_title)/1000.0; + return 1; + } + case STREAM_CTRL_GET_NUM_TITLES: + { + *((unsigned int *)arg) = d->vmg_file->tt_srpt->nr_of_srpts; + return 1; + } + case STREAM_CTRL_GET_NUM_CHAPTERS: + { + int r; + r = get_num_chapter(d->vts_file, d->tt_srpt, d->cur_title); + if(! r) return STREAM_UNSUPPORTED; + *((unsigned int *)arg) = r; + return 1; + } + case STREAM_CTRL_GET_CHAPTER_TIME: + { + int r; + r = get_chapter_time(d->vts_file, d->tt_srpt, d->cur_title, (double *)arg); + if(! r) return STREAM_UNSUPPORTED; + return 1; + } + case STREAM_CTRL_GET_CURRENT_TITLE: + { + *((unsigned int *)arg) = d->cur_title; + return 1; + } + case STREAM_CTRL_GET_CURRENT_TIME: + { + double tm; + tm = dvd_get_current_time(stream, -1); + if(tm != -1) { + *((double *)arg) = tm; + return 1; + } + break; + } + case STREAM_CTRL_SEEK_TO_TIME: + { + if(dvd_seek_to_time(stream, d->vts_file, *((double*)arg))) + return 1; + break; + } + case STREAM_CTRL_GET_ASPECT_RATIO: + { + *((double *)arg) = !d->vts_file->vtsi_mat->vts_video_attr.display_aspect_ratio ? 4.0/3.0 : 16.0/9.0; + return 1; + } + case STREAM_CTRL_GET_NUM_ANGLES: + { + *((int *)arg) = d->vmg_file->tt_srpt->title[d->dvd_title].nr_of_angles; + return 1; + } + case STREAM_CTRL_GET_ANGLE: + { + *((int *)arg) = d->dvd_angle; + return 1; + } + case STREAM_CTRL_SET_ANGLE: + { + int ang = *((int *)arg); + if(ang>d->vmg_file->tt_srpt->title[d->dvd_title].nr_of_angles || ang<=0) + break; + d->dvd_angle = ang; + d->angle_seek = 1; + return 1; + } + case STREAM_CTRL_GET_LANG: + { + struct stream_lang_req *req = arg; + int lang = 0; + switch(req->type) { + case STREAM_AUDIO: + lang = dvd_lang_from_aid(stream, req->id); + break; + case STREAM_SUB: + lang = dvd_lang_from_sid(stream, req->id); + break; + } + if (!lang) + break; + snprintf(req->name, sizeof(req->name), "%c%c", lang >> 8, lang); + return STREAM_OK; + } + case STREAM_CTRL_GET_DVD_INFO: + { + struct stream_dvd_info_req *req = arg; + memset(req, 0, sizeof(*req)); + req->num_subs = dvd_number_of_subs(stream); + memcpy(req->palette, d->cur_pgc->palette, sizeof(req->palette)); + return STREAM_OK; + } + case STREAM_CTRL_GET_DISC_NAME: + { + char buffer[128]; + if (DVDUDFVolumeInfo(d->dvd, buffer, sizeof(buffer), NULL, 0) < 0 && + DVDISOVolumeInfo(d->dvd, buffer, sizeof(buffer), NULL, 0) < 0) + break; + if (!buffer[0]) + break; + *(char**)arg = talloc_strdup(NULL, buffer); + return STREAM_OK; + } + } + return STREAM_UNSUPPORTED; +} + + +static int open_s(stream_t *stream) +{ + int k; + dvd_priv_t *d = stream->priv; + + d->dvd_angle = stream->opts->dvd_angle; + + MP_VERBOSE(stream, "URL: %s\n", stream->url); + d->dvd_title = d->cfg_title + 1; + if(1){ + //int ret,ret2; + int ttn,pgc_id,pgn; + dvd_reader_t *dvd; + dvd_file_t *title; + ifo_handle_t *vmg_file; + tt_srpt_t *tt_srpt; + ifo_handle_t *vts_file; + pgc_t *pgc; + /** + * Open the disc. + */ + if(d->cfg_device && d->cfg_device[0]) + d->dvd_device_current = d->cfg_device; + else if(stream->opts->dvd_device && stream->opts->dvd_device[0]) + d->dvd_device_current = talloc_strdup(stream, stream->opts->dvd_device); + else + d->dvd_device_current = DEFAULT_DVD_DEVICE; + d->dvd_speed = stream->opts->dvd_speed; + dvd_set_speed(stream,d->dvd_device_current, d->dvd_speed); +#if defined(__APPLE__) || defined(__DARWIN__) + /* Dynamic DVD drive selection on Darwin */ + if(!strcmp(d->dvd_device_current, "/dev/rdiskN")) { + int i; + size_t len = strlen(d->dvd_device_current)+1; + char *temp_device = malloc(len); + + for (i = 1; i < 10; i++) { + snprintf(temp_device, len, "/dev/rdisk%d", i); + dvd = DVDOpen(temp_device); + if(!dvd) { + MP_ERR(stream, "Couldn't open DVD device: %s (%s)\n",temp_device, strerror(errno)); + } else { +#if DVDREAD_VERSION <= LIBDVDREAD_VERSION(0,9,4) + dvd_file_t *dvdfile = DVDOpenFile(dvd,d->dvd_title,DVD_READ_INFO_FILE); + if(!dvdfile) { + MP_ERR(stream, "Couldn't open DVD device: %s (%s)\n",temp_device, strerror(errno)); + DVDClose(dvd); + continue; + } + DVDCloseFile(dvdfile); +#endif + break; + } + } + free(temp_device); + + if(!dvd) { + return STREAM_UNSUPPORTED; + } + } else +#endif /* defined(__APPLE__) || defined(__DARWIN__) */ + { + dvd = DVDOpen(d->dvd_device_current); + if(!dvd) { + MP_ERR(stream, "Couldn't open DVD device: %s (%s)\n",d->dvd_device_current, strerror(errno)); + return STREAM_UNSUPPORTED; + } + } + + MP_VERBOSE(stream, "Reading disc structure, please wait...\n"); + + /** + * Load the video manager to find out the information about the titles on + * this disc. + */ + vmg_file = ifoOpen(dvd, 0); + if(!vmg_file) { + MP_ERR(stream, "Can't open VMG info!\n"); + DVDClose( dvd ); + return STREAM_UNSUPPORTED; + } + tt_srpt = vmg_file->tt_srpt; + /** + * Make sure our title number is valid. + */ + MP_INFO(stream, "There are %d titles on this DVD.\n", tt_srpt->nr_of_srpts ); + if(d->dvd_title < 1 || d->dvd_title > tt_srpt->nr_of_srpts) { + MP_ERR(stream, "Invalid DVD title number: %d\n", d->dvd_title); + ifoClose( vmg_file ); + DVDClose( dvd ); + return STREAM_UNSUPPORTED; + } + --(d->dvd_title); // remap 1.. -> 0.. + /** + * Make sure the angle number is valid for this title. + */ + MP_INFO(stream, "There are %d angles in this DVD title.\n", tt_srpt->title[d->dvd_title].nr_of_angles); + if(d->dvd_angle<1 || d->dvd_angle>tt_srpt->title[d->dvd_title].nr_of_angles) { + MP_ERR(stream, "Invalid DVD angle number: %d\n", d->dvd_angle); + goto fail; + } + + ttn = tt_srpt->title[d->dvd_title].vts_ttn - 1; + /** + * Load the VTS information for the title set our title is in. + */ + vts_file = ifoOpen( dvd, tt_srpt->title[d->dvd_title].title_set_nr ); + if(!vts_file) { + MP_ERR(stream, "Cannot open the IFO file for DVD title %d.\n", tt_srpt->title[d->dvd_title].title_set_nr ); + goto fail; + } + /** + * We've got enough info, time to open the title set data. + */ + title = DVDOpenFile(dvd, tt_srpt->title[d->dvd_title].title_set_nr, DVD_READ_TITLE_VOBS); + if(!title) { + MP_ERR(stream, "Cannot open title VOBS (VTS_%02d_1.VOB).\n", tt_srpt->title[d->dvd_title].title_set_nr); + ifoClose( vts_file ); + goto fail; + } + + MP_VERBOSE(stream, "DVD successfully opened.\n"); + // store data + d->dvd=dvd; + d->title=title; + d->vmg_file=vmg_file; + d->tt_srpt=tt_srpt; + d->vts_file=vts_file; + d->cur_title = d->dvd_title; + + pgc = vts_file->vts_pgcit ? vts_file->vts_pgcit->pgci_srp[ttn].pgc : NULL; + /** + * Check number of audio channels and types + */ + { + d->nr_of_channels=0; + if(vts_file->vts_pgcit) { + int i; + for(i=0;i<8;i++) + if(pgc->audio_control[i] & 0x8000) { + audio_attr_t * audio = &vts_file->vtsi_mat->vts_audio_attr[i]; + int language = 0; + char tmp[] = "unknown"; + stream_language_t *audio_stream = &d->audio_streams[d->nr_of_channels]; + + if(audio->lang_type == 1) { + language=audio->lang_code; + tmp[0]=language>>8; + tmp[1]=language&0xff; + tmp[2]=0; + } + + audio_stream->language=language; + audio_stream->id=pgc->audio_control[i] >> 8 & 7; + switch(audio->audio_format) { + case 0: // ac3 + audio_stream->id+=FIRST_AC3_AID; + break; + case 6: // dts + audio_stream->id+=FIRST_DTS_AID; + break; + case 2: // mpeg layer 1/2/3 + case 3: // mpeg2 ext + audio_stream->id+=FIRST_MPG_AID; + break; + case 4: // lpcm + audio_stream->id+=FIRST_PCM_AID; + break; + } + + audio_stream->type=audio->audio_format; + // Pontscho: to my mind, tha channels: + // 1 - stereo + // 5 - 5.1 + audio_stream->channels=audio->channels; + MP_INFO(stream, "audio stream: %d format: %s (%s) language: %s aid: %d.\n", + d->nr_of_channels, + dvd_audio_stream_types[ audio->audio_format ], + dvd_audio_stream_channels[ audio->channels ], + tmp, + audio_stream->id + ); + + d->nr_of_channels++; + } + } + MP_INFO(stream, "number of audio channels on disk: %d.\n",d->nr_of_channels ); + } + + /** + * Check number of subtitles and language + */ + { + int i; + + d->nr_of_subtitles=0; + for(i=0;i<32;i++) + if(pgc->subp_control[i] & 0x80000000) { + subp_attr_t * subtitle = &vts_file->vtsi_mat->vts_subp_attr[i]; + video_attr_t *video = &vts_file->vtsi_mat->vts_video_attr; + int language = 0; + char tmp[] = "unknown"; + stream_language_t *sub_stream = &d->subtitles[d->nr_of_subtitles]; + + if(subtitle->type == 1) { + language=subtitle->lang_code; + tmp[0]=language>>8; + tmp[1]=language&0xff; + tmp[2]=0; + } + + sub_stream->language=language; + sub_stream->id=d->nr_of_subtitles; + if(video->display_aspect_ratio == 0) /* 4:3 */ + sub_stream->id = pgc->subp_control[i] >> 24 & 31; + else if(video->display_aspect_ratio == 3) /* 16:9 */ + sub_stream->id = pgc->subp_control[i] >> 8 & 31; + + MP_INFO(stream, "subtitle ( sid ): %d language: %s\n", sub_stream->id, tmp); + d->nr_of_subtitles++; + } + MP_INFO(stream, "number of subtitles on disk: %d\n",d->nr_of_subtitles); + } + + /** + * Determine which program chain we want to watch. This is based on the + * chapter number. + */ + pgc_id = vts_file->vts_ptt_srpt->title[ttn].ptt[0].pgcn; // local + pgn = vts_file->vts_ptt_srpt->title[ttn].ptt[0].pgn; // local + d->cur_pgc_idx = pgc_id-1; + d->cur_pgc = vts_file->vts_pgcit->pgci_srp[pgc_id-1].pgc; + d->cur_cell = d->cur_pgc->program_map[pgn-1] - 1; // start playback here + d->packs_left=-1; // for Navi stuff + d->angle_seek=0; + d->last_cell=d->cur_pgc->nr_of_cells; + + if(d->cur_pgc->cell_playback[d->cur_cell].block_type == BLOCK_TYPE_ANGLE_BLOCK ) + d->cur_cell+=d->dvd_angle-1; + d->cur_pack = d->cur_pgc->cell_playback[ d->cur_cell ].first_sector; + d->cell_last_pack=d->cur_pgc->cell_playback[ d->cur_cell ].last_sector; + MP_VERBOSE(stream, "DVD start cell: %d pack: 0x%X-0x%X \n",d->cur_cell,d->cur_pack,d->cell_last_pack); + + //assign cell_times_table + d->cell_times_table = malloc(sizeof(unsigned int) * d->cur_pgc->nr_of_cells); + if(d->cell_times_table == NULL) + return STREAM_UNSUPPORTED; + for(k=0; kcur_pgc->nr_of_cells; k++) + d->cell_times_table[k] = mp_dvdtimetomsec(&d->cur_pgc->cell_playback[k].playback_time); + list_chapters(stream, vts_file,tt_srpt,d->dvd_title); + + // ... (unimplemented) + // return NULL; + stream->type = STREAMTYPE_DVD; + stream->demuxer = "+disc"; + stream->lavf_type = "mpeg"; + stream->sector_size = 2048; + stream->fill_buffer = fill_buffer; + stream->control = control; + stream->close = stream_dvd_close; + stream->end_pos = (int64_t)(d->cur_pgc->cell_playback[d->last_cell-1].last_sector)*2048; + MP_VERBOSE(stream, "DVD start=%d end=%d \n",d->cur_pack,d->cur_pgc->cell_playback[d->last_cell-1].last_sector); + stream->priv = (void*)d; + return STREAM_OK; + +fail: + ifoClose(vmg_file); + DVDClose(dvd); + return STREAM_UNSUPPORTED; + } + MP_ERR(stream, "mpv was compiled without DVD support, exiting.\n"); + return STREAM_UNSUPPORTED; +} + +static int ifo_stream_open (stream_t *stream) +{ + char* filename; + dvd_priv_t *priv = talloc_ptrtype(stream, priv); + stream->priv = priv; + *priv = stream_priv_dflts; + + // "file://" prefix -> decode URL-style escapes + if (strlen(stream->url) > strlen(stream->path)) + mp_url_unescape_inplace(stream->path); + + int len = strlen(stream->path); + if (len < 4 || strcasecmp (stream->path + len - 4, ".ifo")) + return STREAM_UNSUPPORTED; + + MP_INFO(stream, ".IFO detected. Redirecting to dvd://\n"); + + filename = strdup(basename(stream->path)); + + talloc_free(priv->cfg_device); + priv->cfg_device = talloc_strdup(NULL, dirname(stream->path)); + if(!strncasecmp(filename,"vts_",4)) + { + if(sscanf(filename+3, "_%02d_", &priv->cfg_title)!=0) + priv->cfg_title = 0; + }else + priv->cfg_title = 0; + + free(filename); + stream->url=talloc_strdup(stream, "dvdread://"); + + return open_s(stream); +} + +const stream_info_t stream_info_dvd = { + .name = "dvd", + .open = open_s, + .protocols = (const char*const[]){ "dvdread", NULL }, + .priv_size = sizeof(dvd_priv_t), + .priv_defaults = &stream_priv_dflts, + .options = stream_opts_fields, + .url_options = (const char*const[]){ + "hostname=title", + "filename=device", + NULL + }, +}; + +const stream_info_t stream_info_ifo = { + .name = "ifo", + .open = ifo_stream_open, + .protocols = (const char*const[]){ "file", "", NULL }, +}; diff --git a/stream/stream_dvdnav.c b/stream/stream_dvdnav.c new file mode 100644 index 0000000000..2d9658bc78 --- /dev/null +++ b/stream/stream_dvdnav.c @@ -0,0 +1,755 @@ +/* + * 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 L