1 /* GStreamer DVD title source
2 * Copyright (C) 1999 Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) 2001 Billy Biggs <vektor@dumbterm.net>.
4 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
34 #include "dvdreadsrc.h"
38 #include <gst/gst-i18n-plugin.h>
40 GST_DEBUG_CATEGORY_STATIC (gstgst_dvd_read_src_debug);
41 #define GST_CAT_DEFAULT (gstgst_dvd_read_src_debug)
52 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
55 GST_STATIC_CAPS ("video/mpeg, mpegversion=2, systemstream=(boolean)true"));
57 static GstFormat title_format;
58 static GstFormat angle_format;
59 static GstFormat sector_format;
60 static GstFormat chapter_format;
62 static gboolean gst_dvd_read_src_start (GstBaseSrc * basesrc);
63 static gboolean gst_dvd_read_src_stop (GstBaseSrc * basesrc);
64 static GstFlowReturn gst_dvd_read_src_create (GstPushSrc * pushsrc,
66 static gboolean gst_dvd_read_src_src_query (GstBaseSrc * basesrc,
68 static gboolean gst_dvd_read_src_src_event (GstBaseSrc * basesrc,
70 static gboolean gst_dvd_read_src_goto_title (GstDvdReadSrc * src, gint title,
72 static gboolean gst_dvd_read_src_goto_chapter (GstDvdReadSrc * src,
74 static gboolean gst_dvd_read_src_goto_sector (GstDvdReadSrc * src, gint angle);
75 static void gst_dvd_read_src_set_property (GObject * object, guint prop_id,
76 const GValue * value, GParamSpec * pspec);
77 static void gst_dvd_read_src_get_property (GObject * object, guint prop_id,
78 GValue * value, GParamSpec * pspec);
79 static GstEvent *gst_dvd_read_src_make_clut_change_event (GstDvdReadSrc * src,
81 static gboolean gst_dvd_read_src_get_size (GstDvdReadSrc * src, gint64 * size);
82 static gboolean gst_dvd_read_src_do_seek (GstBaseSrc * src, GstSegment * s);
83 static gint64 gst_dvd_read_src_convert_timecode (dvd_time_t * time);
84 static gint gst_dvd_read_src_get_next_cell (GstDvdReadSrc * src,
85 pgc_t * pgc, gint cell);
86 static GstClockTime gst_dvd_read_src_get_time_for_sector (GstDvdReadSrc * src,
88 static gint gst_dvd_read_src_get_sector_from_time (GstDvdReadSrc * src,
91 static void gst_dvd_read_src_uri_handler_init (gpointer g_iface,
94 #define gst_dvd_read_src_parent_class parent_class
95 G_DEFINE_TYPE_WITH_CODE (GstDvdReadSrc, gst_dvd_read_src, GST_TYPE_PUSH_SRC,
96 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
97 gst_dvd_read_src_uri_handler_init));
100 gst_dvd_read_src_finalize (GObject * object)
102 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
104 g_free (src->location);
106 G_OBJECT_CLASS (parent_class)->finalize (object);
110 gst_dvd_read_src_init (GstDvdReadSrc * src)
112 GstCaps *src_caps = gst_static_pad_template_get_caps (&srctemplate);
114 src->vts_file = NULL;
115 src->vmg_file = NULL;
116 src->dvd_title = NULL;
118 src->location = g_strdup ("/dev/dvd");
119 src->first_seek = TRUE;
120 src->new_seek = TRUE;
121 src->new_cell = TRUE;
122 src->change_cell = FALSE;
124 src->uri_chapter = 1;
127 src->title_lang_event_pending = NULL;
128 src->pending_clut_event = NULL;
130 gst_pad_use_fixed_caps (GST_BASE_SRC_PAD (src));
131 gst_pad_set_caps (GST_BASE_SRC_PAD (src), src_caps);
132 gst_caps_unref (src_caps);
136 gst_dvd_read_src_is_seekable (GstBaseSrc * src)
142 gst_dvd_read_src_class_init (GstDvdReadSrcClass * klass)
144 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
145 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
146 GstPushSrcClass *gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
147 GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
149 gobject_class->finalize = gst_dvd_read_src_finalize;
150 gobject_class->set_property = gst_dvd_read_src_set_property;
151 gobject_class->get_property = gst_dvd_read_src_get_property;
153 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEVICE,
154 g_param_spec_string ("device", "Device",
155 "DVD device location", NULL,
156 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
157 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TITLE,
158 g_param_spec_int ("title", "title", "title",
159 1, 999, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
160 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CHAPTER,
161 g_param_spec_int ("chapter", "chapter", "chapter",
162 1, 999, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
163 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ANGLE,
164 g_param_spec_int ("angle", "angle", "angle",
165 1, 999, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
167 gst_element_class_add_pad_template (gstelement_class,
168 gst_static_pad_template_get (&srctemplate));
170 gst_element_class_set_static_metadata (gstelement_class, "DVD Source",
172 "Access a DVD title/chapter/angle using libdvdread",
173 "Erik Walthinsen <omega@cse.ogi.edu>");
175 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_dvd_read_src_start);
176 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_dvd_read_src_stop);
177 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_dvd_read_src_src_query);
178 gstbasesrc_class->event = GST_DEBUG_FUNCPTR (gst_dvd_read_src_src_event);
179 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_dvd_read_src_do_seek);
180 gstbasesrc_class->is_seekable =
181 GST_DEBUG_FUNCPTR (gst_dvd_read_src_is_seekable);
183 gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_dvd_read_src_create);
185 title_format = gst_format_register ("title", "DVD title");
186 angle_format = gst_format_register ("angle", "DVD angle");
187 sector_format = gst_format_register ("sector", "DVD sector");
188 chapter_format = gst_format_register ("chapter", "DVD chapter");
192 gst_dvd_read_src_start (GstBaseSrc * basesrc)
194 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
196 g_return_val_if_fail (src->location != NULL, FALSE);
198 GST_DEBUG_OBJECT (src, "Opening DVD '%s'", src->location);
200 if ((src->dvd = DVDOpen (src->location)) == NULL)
203 /* Load the video manager to find out the information about the titles */
204 GST_DEBUG_OBJECT (src, "Loading VMG info");
206 if (!(src->vmg_file = ifoOpen (src->dvd, 0)))
207 goto ifo_open_failed;
209 src->tt_srpt = src->vmg_file->tt_srpt;
211 src->title = src->uri_title - 1;
212 src->chapter = src->uri_chapter - 1;
213 src->angle = src->uri_angle - 1;
215 if (!gst_dvd_read_src_goto_title (src, src->title, src->angle))
216 goto title_open_failed;
218 if (!gst_dvd_read_src_goto_chapter (src, src->chapter))
219 goto chapter_open_failed;
221 src->new_seek = FALSE;
222 src->change_cell = TRUE;
224 src->first_seek = TRUE;
231 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
232 (_("Could not open DVD")),
233 ("DVDOpen(%s) failed: %s", src->location, g_strerror (errno)));
238 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
239 (_("Could not open DVD")),
240 ("ifoOpen() failed: %s", g_strerror (errno)));
245 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
246 (_("Could not open DVD title %d"), src->uri_title), (NULL));
251 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
252 (_("Failed to go to chapter %d of DVD title %d"),
253 src->uri_chapter, src->uri_title), (NULL));
259 gst_dvd_read_src_stop (GstBaseSrc * basesrc)
261 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
264 ifoClose (src->vts_file);
265 src->vts_file = NULL;
268 ifoClose (src->vmg_file);
269 src->vmg_file = NULL;
271 if (src->dvd_title) {
272 DVDCloseFile (src->dvd_title);
273 src->dvd_title = NULL;
279 src->new_cell = TRUE;
280 src->new_seek = TRUE;
281 src->change_cell = FALSE;
284 src->need_newsegment = TRUE;
285 src->vts_tmapt = NULL;
286 if (src->title_lang_event_pending) {
287 gst_event_unref (src->title_lang_event_pending);
288 src->title_lang_event_pending = NULL;
290 if (src->pending_clut_event) {
291 gst_event_unref (src->pending_clut_event);
292 src->pending_clut_event = NULL;
294 if (src->chapter_starts) {
295 g_free (src->chapter_starts);
296 src->chapter_starts = NULL;
299 GST_LOG_OBJECT (src, "closed DVD");
305 cur_title_get_chapter_pgc (GstDvdReadSrc * src, gint chapter, gint * p_pgn,
306 gint * p_pgc_id, pgc_t ** p_pgc)
311 g_assert (chapter >= 0 && chapter < src->num_chapters);
313 pgc_id = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter].pgcn;
314 pgn = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter].pgn;
315 pgc = src->vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
323 cur_title_get_chapter_bounds (GstDvdReadSrc * src, gint chapter,
324 gint * p_first_cell, gint * p_last_cell)
327 gint pgn, pgc_id, pgn_next_ch;
329 g_assert (chapter >= 0 && chapter < src->num_chapters);
331 cur_title_get_chapter_pgc (src, chapter, &pgn, &pgc_id, &pgc);
333 *p_first_cell = pgc->program_map[pgn - 1] - 1;
335 /* last cell is used as a 'up to boundary', not 'up to and including',
336 * i.e. it is the first cell not included in the chapter range */
337 if (chapter == (src->num_chapters - 1)) {
338 *p_last_cell = pgc->nr_of_cells;
340 pgn_next_ch = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter + 1].pgn;
341 *p_last_cell = pgc->program_map[pgn_next_ch - 1] - 1;
344 GST_DEBUG_OBJECT (src, "Chapter %d bounds: %d %d (within %d cells)",
345 chapter, *p_first_cell, *p_last_cell, pgc->nr_of_cells);
349 gst_dvd_read_src_goto_chapter (GstDvdReadSrc * src, gint chapter)
353 /* make sure the chapter number is valid for this title */
354 if (chapter < 0 || chapter >= src->num_chapters) {
355 GST_WARNING_OBJECT (src, "invalid chapter %d (only %d available)",
356 chapter, src->num_chapters);
357 chapter = CLAMP (chapter, 0, src->num_chapters - 1);
360 /* determine which program chain we want to watch. This is
361 * based on the chapter number */
362 cur_title_get_chapter_pgc (src, chapter, &src->pgn, &src->pgc_id,
364 cur_title_get_chapter_bounds (src, chapter, &src->start_cell,
367 GST_LOG_OBJECT (src, "Opened chapter %d - cell %d-%d", chapter + 1,
368 src->start_cell, src->last_cell);
370 /* retrieve position */
372 for (i = 0; i < chapter; i++) {
375 cur_title_get_chapter_bounds (src, i, &c1, &c2);
379 src->cur_pgc->cell_playback[c1].last_sector -
380 src->cur_pgc->cell_playback[c1].first_sector;
385 /* prepare reading for new cell */
386 src->new_cell = TRUE;
387 src->next_cell = src->start_cell;
389 src->chapter = chapter;
391 if (src->pending_clut_event)
392 gst_event_unref (src->pending_clut_event);
394 src->pending_clut_event =
395 gst_dvd_read_src_make_clut_change_event (src, src->cur_pgc->palette);
401 gst_dvd_read_src_get_chapter_starts (GstDvdReadSrc * src)
403 GstClockTime uptohere;
406 g_free (src->chapter_starts);
407 src->chapter_starts = g_new (GstClockTime, src->num_chapters);
409 uptohere = (GstClockTime) 0;
410 for (c = 0; c < src->num_chapters; ++c) {
411 GstClockTime chapter_duration = 0;
412 gint cell_start, cell_end, cell;
416 cur_title_get_chapter_pgc (src, c, &pgn, &pgc_id, &pgc);
417 cur_title_get_chapter_bounds (src, c, &cell_start, &cell_end);
420 while (cell < cell_end) {
421 dvd_time_t *cell_duration;
423 cell_duration = &pgc->cell_playback[cell].playback_time;
424 chapter_duration += gst_dvd_read_src_convert_timecode (cell_duration);
425 cell = gst_dvd_read_src_get_next_cell (src, pgc, cell);
428 src->chapter_starts[c] = uptohere;
430 GST_INFO_OBJECT (src, "[%02u] Chapter %02u starts at %" GST_TIME_FORMAT
431 ", dur = %" GST_TIME_FORMAT ", cells %d-%d", src->title + 1, c + 1,
432 GST_TIME_ARGS (uptohere), GST_TIME_ARGS (chapter_duration),
433 cell_start, cell_end);
435 uptohere += chapter_duration;
440 gst_dvd_read_src_goto_title (GstDvdReadSrc * src, gint title, gint angle)
443 gchar lang_code[3] = { '\0', '\0', '\0' }, *t;
450 /* make sure our title number is valid */
451 num_titles = src->tt_srpt->nr_of_srpts;
452 GST_INFO_OBJECT (src, "There are %d titles on this DVD", num_titles);
453 if (title < 0 || title >= num_titles)
456 src->num_chapters = src->tt_srpt->title[title].nr_of_ptts;
457 GST_INFO_OBJECT (src, "Title %d has %d chapters", title + 1,
460 /* make sure the angle number is valid for this title */
461 src->num_angles = src->tt_srpt->title[title].nr_of_angles;
462 GST_LOG_OBJECT (src, "Title %d has %d angles", title + 1, src->num_angles);
463 if (angle < 0 || angle >= src->num_angles) {
464 GST_WARNING_OBJECT (src, "Invalid angle %d (only %d available)",
465 angle, src->num_angles);
466 angle = CLAMP (angle, 0, src->num_angles - 1);
469 /* load the VTS information for the title set our title is in */
470 title_set_nr = src->tt_srpt->title[title].title_set_nr;
471 src->vts_file = ifoOpen (src->dvd, title_set_nr);
472 if (src->vts_file == NULL)
473 goto ifo_open_failed;
475 src->ttn = src->tt_srpt->title[title].vts_ttn;
476 src->vts_ptt_srpt = src->vts_file->vts_ptt_srpt;
478 /* interactive title? */
479 if (src->num_chapters > 0 &&
480 src->vts_ptt_srpt->title[src->ttn - 1].ptt[0].pgn == 0) {
481 goto commands_only_pgc;
484 /* we've got enough info, time to open the title set data */
485 src->dvd_title = DVDOpenFile (src->dvd, title_set_nr, DVD_READ_TITLE_VOBS);
486 if (src->dvd_title == NULL)
487 goto title_open_failed;
489 GST_INFO_OBJECT (src, "Opened title %d, angle %d", title + 1, angle);
495 if (src->title_lang_event_pending) {
496 gst_event_unref (src->title_lang_event_pending);
497 src->title_lang_event_pending = NULL;
500 s = gst_structure_new ("application/x-gst-dvd",
501 "event", G_TYPE_STRING, "dvd-lang-codes", NULL);
503 /* so we can filter out invalid/unused streams (same for all chapters) */
504 cur_title_get_chapter_pgc (src, 0, &pgn0, &pgc0_id, &pgc0);
507 for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_audio_streams; i++) {
508 const audio_attr_t *a;
510 /* audio stream present? */
511 if (pgc0 != NULL && (pgc0->audio_control[i] & 0x8000) == 0)
514 a = &src->vts_file->vtsi_mat->vts_audio_attr[i];
516 t = g_strdup_printf ("audio-%d-format", i);
517 gst_structure_set (s, t, G_TYPE_INT, (int) a->audio_format, NULL);
519 t = g_strdup_printf ("audio-%d-stream", i);
520 gst_structure_set (s, t, G_TYPE_INT, (int) i, NULL);
524 t = g_strdup_printf ("audio-%d-language", i);
525 lang_code[0] = (a->lang_code >> 8) & 0xff;
526 lang_code[1] = a->lang_code & 0xff;
527 gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
533 GST_INFO_OBJECT (src, "[%02d] Audio %02d: lang='%s', format=%d",
534 src->title + 1, i, lang_code, (gint) a->audio_format);
538 for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_subp_streams; i++) {
539 const subp_attr_t *u;
540 const video_attr_t *v;
543 /* subpicture stream present? */
544 if (pgc0 != NULL && (pgc0->subp_control[i] & 0x80000000) == 0)
547 u = &src->vts_file->vtsi_mat->vts_subp_attr[i];
548 v = &src->vts_file->vtsi_mat->vts_video_attr;
552 if (v->display_aspect_ratio == 0) /* 4:3 */
553 sid = (pgc0->subp_control[i] >> 24) & 0x1f;
554 else if (v->display_aspect_ratio == 3) /* 16:9 */
555 sid = (pgc0->subp_control[i] >> 8) & 0x1f;
559 t = g_strdup_printf ("subpicture-%d-language", i);
560 lang_code[0] = (u->lang_code >> 8) & 0xff;
561 lang_code[1] = u->lang_code & 0xff;
562 gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
563 t = g_strdup_printf ("subpicture-%d-stream", i);
564 gst_structure_set (s, t, G_TYPE_INT, (int) sid, NULL);
566 t = g_strdup_printf ("subpicture-%d-format", i);
567 gst_structure_set (s, t, G_TYPE_INT, (int) 0, NULL);
573 GST_INFO_OBJECT (src, "[%02d] Subtitle %02d: lang='%s', type=%d",
574 src->title + 1, sid, lang_code, u->type);
577 src->title_lang_event_pending =
578 gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
580 /* dump seek tables */
581 src->vts_tmapt = src->vts_file->vts_tmapt;
582 if (src->vts_tmapt) {
585 GST_LOG_OBJECT (src, "nr_of_tmaps = %d", src->vts_tmapt->nr_of_tmaps);
586 for (i = 0; i < src->vts_tmapt->nr_of_tmaps; ++i) {
587 GST_LOG_OBJECT (src, "======= Table %d ===================", i);
588 GST_LOG_OBJECT (src, "Offset relative to VTS_TMAPTI: %d",
589 src->vts_tmapt->tmap_offset[i]);
590 GST_LOG_OBJECT (src, "Time unit (seconds) : %d",
591 src->vts_tmapt->tmap[i].tmu);
592 GST_LOG_OBJECT (src, "Number of entries : %d",
593 src->vts_tmapt->tmap[i].nr_of_entries);
594 for (j = 0; j < src->vts_tmapt->tmap[i].nr_of_entries; j++) {
597 time = (guint64) src->vts_tmapt->tmap[i].tmu * (j + 1) * GST_SECOND;
598 GST_LOG_OBJECT (src, "Time: %" GST_TIME_FORMAT " VOBU "
599 "Sector: 0x%08x %s", GST_TIME_ARGS (time),
600 src->vts_tmapt->tmap[i].map_ent[j] & 0x7fffffff,
601 (src->vts_tmapt->tmap[i].map_ent[j] >> 31) ? "discontinuity" : "");
605 GST_WARNING_OBJECT (src, "no vts_tmapt - seeking will suck");
608 gst_dvd_read_src_get_chapter_starts (src);
615 GST_WARNING_OBJECT (src, "Invalid title %d (only %d available)",
621 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
622 (_("Could not open DVD title %d"), title_set_nr),
623 ("ifoOpen(%d) failed: %s", title_set_nr, g_strerror (errno)));
628 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
629 (_("Could not open DVD title %d"), title_set_nr),
630 ("Can't open title VOBS (VTS_%02d_1.VOB)", title_set_nr));
635 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
636 (_("Could not open DVD title %d. Interactive titles are not supported "
637 "by this element"), title_set_nr),
638 ("Commands-only PGC, not supported, use rsndvdbin"));
643 /* FIXME: double-check this function, compare against original */
645 gst_dvd_read_src_get_next_cell (GstDvdReadSrc * src, pgc_t * pgc, gint cell)
647 /* Check if we're entering an angle block. */
648 if (pgc->cell_playback[cell].block_type != BLOCK_TYPE_ANGLE_BLOCK)
651 while (pgc->cell_playback[cell].block_mode != BLOCK_MODE_LAST_CELL)
657 /* Returns true if the pack is a NAV pack */
659 gst_dvd_read_src_is_nav_pack (const guint8 * data, gint lbn, dsi_t * dsi_pack)
661 if (GST_READ_UINT32_BE (data + 0x26) != 0x000001BF)
664 /* Check that this is substream 0 (PCI) */
668 if (GST_READ_UINT32_BE (data + 0x400) != 0x000001BF)
671 /* Check that this is substream 1 (DSI) */
672 if (data[0x406] != 1)
675 /* Check sizes of PCI and DSI packets */
676 if (GST_READ_UINT16_BE (data + 0x2a) != 0x03d4)
679 if (GST_READ_UINT16_BE (data + 0x404) != 0x03fa)
682 /* Read the DSI packet into the provided struct and check it */
683 navRead_DSI (dsi_pack, (unsigned char *) data + DSI_START_BYTE);
684 if (lbn != dsi_pack->dsi_gi.nv_pck_lbn)
690 /* find time for sector from index, returns NONE if there is no exact match */
692 gst_dvd_read_src_get_time_for_sector (GstDvdReadSrc * src, guint sector)
696 if (src->vts_tmapt == NULL || src->vts_tmapt->nr_of_tmaps == 0)
697 return GST_CLOCK_TIME_NONE;
699 for (i = 0; i < src->vts_tmapt->nr_of_tmaps; ++i) {
700 for (j = 0; j < src->vts_tmapt->tmap[i].nr_of_entries; ++j) {
701 if ((src->vts_tmapt->tmap[i].map_ent[j] & 0x7fffffff) == sector)
702 return (guint64) src->vts_tmapt->tmap[i].tmu * (j + 1) * GST_SECOND;
707 return (GstClockTime) 0;
709 return GST_CLOCK_TIME_NONE;
712 /* returns the sector in the index at (or before) the given time, or -1 */
714 gst_dvd_read_src_get_sector_from_time (GstDvdReadSrc * src, GstClockTime ts)
718 if (src->vts_tmapt == NULL || src->vts_tmapt->nr_of_tmaps < src->ttn)
722 for (j = 0; j < src->vts_tmapt->tmap[src->ttn - 1].nr_of_entries; ++j) {
723 GstClockTime entry_time;
726 (guint64) src->vts_tmapt->tmap[src->ttn - 1].tmu * (j + 1) * GST_SECOND;
727 if (entry_time <= ts) {
728 sector = src->vts_tmapt->tmap[src->ttn - 1].map_ent[j] & 0x7fffffff;
730 if (entry_time >= ts) {
744 GST_DVD_READ_ERROR = -1,
745 GST_DVD_READ_EOS = -2,
746 GST_DVD_READ_AGAIN = -3
749 static GstDvdReadReturn
750 gst_dvd_read_src_read (GstDvdReadSrc * src, gint angle, gint new_seek,
755 guint8 oneblock[DVD_VIDEO_LB_LEN];
757 guint next_vobu, cur_output_size;
763 seg = &(GST_BASE_SRC (src)->segment);
765 /* playback by cell in this pgc, starting at the cell for our chapter */
767 src->cur_cell = src->start_cell;
771 if (src->cur_cell >= src->last_cell) {
772 /* advance to next chapter */
773 if (src->chapter == (src->num_chapters - 1) ||
774 (seg->format == chapter_format && seg->stop != -1 &&
775 src->chapter == (seg->stop - 1))) {
776 GST_DEBUG_OBJECT (src, "end of chapter segment");
780 GST_INFO_OBJECT (src, "end of chapter %d, switch to next",
784 gst_dvd_read_src_goto_chapter (src, src->chapter);
786 return GST_DVD_READ_AGAIN;
789 if (src->new_cell || new_seek) {
791 src->cur_cell = src->next_cell;
792 if (src->cur_cell >= src->last_cell) {
793 GST_LOG_OBJECT (src, "last cell in chapter");
798 /* take angle into account */
799 if (src->cur_pgc->cell_playback[src->cur_cell].block_type
800 == BLOCK_TYPE_ANGLE_BLOCK)
801 src->cur_cell += angle;
803 /* calculate next cell */
805 gst_dvd_read_src_get_next_cell (src, src->cur_pgc, src->cur_cell);
807 /* we loop until we're out of this cell */
808 src->cur_pack = src->cur_pgc->cell_playback[src->cur_cell].first_sector;
809 src->new_cell = FALSE;
810 GST_DEBUG_OBJECT (src, "Starting new cell %d @ pack %d", src->cur_cell,
814 if (src->cur_pack >= src->cur_pgc->cell_playback[src->cur_cell].last_sector) {
815 src->new_cell = TRUE;
816 GST_LOG_OBJECT (src, "Beyond last sector for cell %d, going to next cell",
818 return GST_DVD_READ_AGAIN;
821 /* read NAV packet */
826 len = DVDReadBlocks (src->dvd_title, src->cur_pack, 1, oneblock);
830 if (!gst_dvd_read_src_is_nav_pack (oneblock, src->cur_pack, &dsi_pack)) {
831 GST_LOG_OBJECT (src, "Skipping nav packet @ pack %d", src->cur_pack);
834 if (retries < 2000) {
837 GST_LOG_OBJECT (src, "No nav packet @ pack %d after 2000 blocks",
843 /* determine where we go next. These values are the ones we
844 * mostly care about */
845 cur_output_size = dsi_pack.dsi_gi.vobu_ea + 1;
847 /* If we're not at the end of this cell, we can determine the next
848 * VOBU to display using the VOBU_SRI information section of the
849 * DSI. Using this value correctly follows the current angle,
850 * avoiding the doubled scenes in The Matrix, and makes our life
853 * Otherwise, we set our next address past the end of this cell to
854 * force the code above to go to the next cell in the program. */
855 if (dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL) {
856 next_vobu = src->cur_pack + (dsi_pack.vobu_sri.next_vobu & 0x7fffffff);
858 next_vobu = src->cur_pgc->cell_playback[src->cur_cell].last_sector + 1;
861 g_assert (cur_output_size < 1024);
863 /* create the buffer (TODO: use buffer pool?) */
865 gst_buffer_new_allocate (NULL, cur_output_size * DVD_VIDEO_LB_LEN, NULL);
867 GST_LOG_OBJECT (src, "Going to read %u sectors @ pack %d", cur_output_size,
870 gst_buffer_map (buf, &map, GST_MAP_WRITE);
871 /* read in and output cursize packs */
873 DVDReadBlocks (src->dvd_title, src->cur_pack, cur_output_size, map.data);
875 if (len != cur_output_size)
876 goto block_read_error;
878 gst_buffer_unmap (buf, &map);
879 gst_buffer_resize (buf, 0, cur_output_size * DVD_VIDEO_LB_LEN);
880 /* GST_BUFFER_OFFSET (buf) = priv->cur_pack * DVD_VIDEO_LB_LEN; */
881 GST_BUFFER_TIMESTAMP (buf) =
882 gst_dvd_read_src_get_time_for_sector (src, src->cur_pack);
886 GST_LOG_OBJECT (src, "Read %u sectors", cur_output_size);
888 src->cur_pack = next_vobu;
890 next_time = GST_BUFFER_TIMESTAMP (buf);
891 if (GST_CLOCK_TIME_IS_VALID (next_time) && seg->format == GST_FORMAT_TIME &&
892 GST_CLOCK_TIME_IS_VALID (seg->stop) &&
893 next_time > seg->stop + 5 * GST_SECOND) {
894 GST_DEBUG_OBJECT (src, "end of TIME segment");
898 return GST_DVD_READ_OK;
903 GST_INFO_OBJECT (src, "Reached end-of-segment/stream - EOS");
904 return GST_DVD_READ_EOS;
908 GST_ERROR_OBJECT (src, "Read failed for block %d", src->cur_pack);
909 return GST_DVD_READ_ERROR;
913 GST_ERROR_OBJECT (src, "Read failed for %d blocks at %d",
914 cur_output_size, src->cur_pack);
915 gst_buffer_unmap (buf, &map);
916 gst_buffer_unref (buf);
917 return GST_DVD_READ_ERROR;
921 /* we don't cache the result on purpose */
923 gst_dvd_read_descrambler_available (void)
929 module = g_module_open ("libdvdcss", 0);
930 if (module != NULL) {
931 res = g_module_symbol (module, "dvdcss_open", &sym);
932 g_module_close (module);
941 gst_dvd_read_src_create (GstPushSrc * pushsrc, GstBuffer ** p_buf)
943 GstDvdReadSrc *src = GST_DVD_READ_SRC (pushsrc);
947 g_return_val_if_fail (src->dvd != NULL, GST_FLOW_ERROR);
949 srcpad = GST_BASE_SRC (src)->srcpad;
951 if (src->need_newsegment) {
954 gst_segment_init (&seg, GST_FORMAT_BYTES);
955 seg.start = src->cur_pack * DVD_VIDEO_LB_LEN;
958 gst_pad_push_event (srcpad, gst_event_new_segment (&seg));
959 src->need_newsegment = FALSE;
963 gst_dvd_read_src_goto_title (src, src->title, src->angle);
964 gst_dvd_read_src_goto_chapter (src, src->chapter);
966 src->new_seek = FALSE;
967 src->change_cell = TRUE;
970 if (src->title_lang_event_pending) {
971 gst_pad_push_event (srcpad, src->title_lang_event_pending);
972 src->title_lang_event_pending = NULL;
975 if (src->pending_clut_event) {
976 gst_pad_push_event (srcpad, src->pending_clut_event);
977 src->pending_clut_event = NULL;
982 res = gst_dvd_read_src_read (src, src->angle, src->change_cell, p_buf);
983 } while (res == GST_DVD_READ_AGAIN);
986 case GST_DVD_READ_ERROR:{
987 /* FIXME: figure out a way to detect if scrambling is the problem */
988 if (!gst_dvd_read_descrambler_available ()) {
989 GST_ELEMENT_ERROR (src, RESOURCE, READ,
990 (_("Could not read DVD. This may be because the DVD is encrypted "
991 "and a DVD decryption library is not installed.")), (NULL));
993 GST_ELEMENT_ERROR (src, RESOURCE, READ, (_("Could not read DVD.")),
996 return GST_FLOW_ERROR;
998 case GST_DVD_READ_EOS:{
1001 case GST_DVD_READ_OK:{
1002 src->change_cell = FALSE;
1009 g_return_val_if_reached (GST_FLOW_EOS);
1013 gst_dvd_read_src_set_property (GObject * object, guint prop_id,
1014 const GValue * value, GParamSpec * pspec)
1016 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
1019 GST_OBJECT_LOCK (src);
1020 started = GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_FLAG_STARTED);
1025 g_warning ("%s: property '%s' needs to be set before the device is "
1026 "opened", GST_ELEMENT_NAME (src), pspec->name);
1030 g_free (src->location);
1031 /* clear the filename if we get a NULL (is that possible?) */
1032 if (g_value_get_string (value) == NULL) {
1033 src->location = g_strdup ("/dev/dvd");
1035 src->location = g_strdup (g_value_get_string (value));
1040 src->uri_title = g_value_get_int (value);
1042 src->title = src->uri_title - 1;
1043 src->new_seek = TRUE;
1047 src->uri_chapter = g_value_get_int (value);
1049 src->chapter = src->uri_chapter - 1;
1050 src->new_seek = TRUE;
1054 src->uri_angle = g_value_get_int (value);
1056 src->angle = src->uri_angle - 1;
1060 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1064 GST_OBJECT_UNLOCK (src);
1068 gst_dvd_read_src_get_property (GObject * object, guint prop_id, GValue * value,
1071 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
1073 GST_OBJECT_LOCK (src);
1077 g_value_set_string (value, src->location);
1080 g_value_set_int (value, src->uri_title);
1083 g_value_set_int (value, src->uri_chapter);
1086 g_value_set_int (value, src->uri_angle);
1089 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1093 GST_OBJECT_UNLOCK (src);
1097 gst_dvd_read_src_get_size (GstDvdReadSrc * src, gint64 * size)
1099 gboolean ret = FALSE;
1101 if (src->dvd_title) {
1104 blocks = DVDFileSize (src->dvd_title);
1106 *size = (gint64) blocks *DVD_VIDEO_LB_LEN;
1110 GST_WARNING_OBJECT (src, "DVDFileSize(%p) failed!", src->dvd_title);
1117 /*** Querying and seeking ***/
1120 gst_dvd_read_src_handle_seek_event (GstDvdReadSrc * src, GstEvent * event)
1123 GstSeekType cur_type, end_type;
1124 gint64 new_off, total;
1130 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &new_off,
1134 GST_DEBUG_OBJECT (src, "cannot do backwards playback yet");
1138 if (end_type != GST_SEEK_TYPE_NONE) {
1139 if ((format != chapter_format && format != GST_FORMAT_TIME) ||
1140 end_type != GST_SEEK_TYPE_SET) {
1141 GST_DEBUG_OBJECT (src, "end seek type not supported");
1146 if (cur_type != GST_SEEK_TYPE_SET) {
1147 GST_DEBUG_OBJECT (src, "only SEEK_TYPE_SET is supported");
1151 if (format == angle_format) {
1152 GST_OBJECT_LOCK (src);
1153 if (new_off < 0 || new_off >= src->num_angles) {
1154 GST_OBJECT_UNLOCK (src);
1155 GST_DEBUG_OBJECT (src, "invalid angle %d, only %d available",
1156 src->num_angles, src->num_angles);
1159 src->angle = (gint) new_off;
1160 GST_OBJECT_UNLOCK (src);
1161 GST_DEBUG_OBJECT (src, "switched to angle %d", (gint) new_off + 1);
1165 if (format != chapter_format && format != title_format &&
1166 format != GST_FORMAT_BYTES && format != GST_FORMAT_TIME) {
1167 GST_DEBUG_OBJECT (src, "unsupported seek format %d (%s)", format,
1168 gst_format_get_name (format));
1172 if (format == GST_FORMAT_BYTES) {
1173 GST_DEBUG_OBJECT (src, "Requested seek to byte %" G_GUINT64_FORMAT,
1175 } else if (format == GST_FORMAT_TIME) {
1176 GST_DEBUG_OBJECT (src, "Requested seek to time %" GST_TIME_FORMAT,
1177 GST_TIME_ARGS (new_off));
1178 if (gst_dvd_read_src_get_sector_from_time (src, new_off) < 0) {
1179 GST_DEBUG_OBJECT (src, "Can't find sector for requested time");
1184 srcpad = GST_BASE_SRC_PAD (src);
1186 /* check whether the seek looks reasonable (ie within possible range) */
1187 if (format == GST_FORMAT_BYTES) {
1188 GST_OBJECT_LOCK (src);
1189 query_ok = gst_dvd_read_src_get_size (src, &total);
1190 GST_OBJECT_UNLOCK (src);
1192 query_ok = gst_pad_query_duration (srcpad, format, &total);
1196 GST_DEBUG_OBJECT (src, "Failed to query duration in format %s",
1197 gst_format_get_name (format));
1201 GST_DEBUG_OBJECT (src, "Total %s: %12" G_GINT64_FORMAT,
1202 gst_format_get_name (format), total);
1203 GST_DEBUG_OBJECT (src, "Seek to %s: %12" G_GINT64_FORMAT,
1204 gst_format_get_name (format), new_off);
1206 if (new_off >= total) {
1207 GST_DEBUG_OBJECT (src, "Seek position out of range");
1211 /* set segment to seek format; this allows us to use the do_seek
1212 * virtual function and let the base source handle all the tricky
1213 * stuff for us. We don't use the segment internally anyway */
1214 /* FIXME: can't take the stream lock here - what to do? */
1215 GST_OBJECT_LOCK (src);
1216 GST_BASE_SRC (src)->segment.format = format;
1217 GST_BASE_SRC (src)->segment.start = 0;
1218 GST_BASE_SRC (src)->segment.stop = total;
1219 GST_BASE_SRC (src)->segment.duration = total;
1220 GST_OBJECT_UNLOCK (src);
1222 return GST_BASE_SRC_CLASS (parent_class)->event (GST_BASE_SRC (src), event);
1226 gst_dvd_read_src_get_sector_bounds (GstDvdReadSrc * src, gint * first,
1230 cur_title_get_chapter_bounds (src, 0, &c1, &tmp);
1231 cur_title_get_chapter_bounds (src, src->num_chapters - 1, &tmp, &c2);
1232 *first = src->cur_pgc->cell_playback[c1].first_sector;
1233 *last = src->cur_pgc->cell_playback[c2].last_sector;
1237 gst_dvd_read_src_do_seek (GstBaseSrc * basesrc, GstSegment * s)
1241 src = GST_DVD_READ_SRC (basesrc);
1243 GST_DEBUG_OBJECT (src, "Seeking to %s: %12" G_GINT64_FORMAT,
1244 gst_format_get_name (s->format), s->position);
1246 /* Ignore the first seek to 0, as it breaks starting playback
1247 * from another chapter by seeking back to sector 0 */
1248 if (src->first_seek && s->format == GST_FORMAT_BYTES && s->start == 0) {
1249 src->first_seek = FALSE;
1253 if (s->format == sector_format || s->format == GST_FORMAT_BYTES
1254 || s->format == GST_FORMAT_TIME) {
1257 old = src->cur_pack;
1259 if (s->format == sector_format) {
1261 gst_dvd_read_src_get_sector_bounds (src, &first, &last);
1262 GST_DEBUG_OBJECT (src, "Format is sector, seeking to %" G_GINT64_FORMAT,
1264 src->cur_pack = s->position;
1265 if (src->cur_pack < first)
1266 src->cur_pack = first;
1267 if (src->cur_pack > last)
1268 src->cur_pack = last;
1269 } else if (s->format == GST_FORMAT_TIME) {
1271 GST_DEBUG_OBJECT (src, "Format is time");
1273 sector = gst_dvd_read_src_get_sector_from_time (src, s->position);
1275 GST_DEBUG_OBJECT (src, "Time %" GST_TIME_FORMAT " => sector %d",
1276 GST_TIME_ARGS (s->position), sector);
1278 /* really shouldn't happen, we've checked this earlier ... */
1279 g_return_val_if_fail (sector >= 0, FALSE);
1281 src->cur_pack = sector;
1285 gst_dvd_read_src_get_sector_bounds (src, &first, &last);
1286 GST_DEBUG_OBJECT (src, "Format is byte");
1287 src->cur_pack = s->position / DVD_VIDEO_LB_LEN;
1288 if (((gint64) src->cur_pack * DVD_VIDEO_LB_LEN) != s->position) {
1289 GST_LOG_OBJECT (src, "rounded down offset %" G_GINT64_FORMAT " => %"
1290 G_GINT64_FORMAT, s->position,
1291 (gint64) src->cur_pack * DVD_VIDEO_LB_LEN);
1293 src->cur_pack += first;
1296 if (!gst_dvd_read_src_goto_sector (src, src->angle)) {
1297 GST_DEBUG_OBJECT (src, "seek to sector 0x%08x failed", src->cur_pack);
1298 src->cur_pack = old;
1302 GST_LOG_OBJECT (src, "seek to sector 0x%08x ok", src->cur_pack);
1303 } else if (s->format == chapter_format) {
1304 if (!gst_dvd_read_src_goto_chapter (src, (gint) s->position)) {
1305 GST_DEBUG_OBJECT (src, "seek to chapter %d failed",
1306 (gint) s->position + 1);
1309 GST_INFO_OBJECT (src, "seek to chapter %d ok", (gint) s->position + 1);
1310 src->chapter = s->position;
1311 } else if (s->format == title_format) {
1312 if (!gst_dvd_read_src_goto_title (src, (gint) s->position, src->angle) ||
1313 !gst_dvd_read_src_goto_chapter (src, 0)) {
1314 GST_DEBUG_OBJECT (src, "seek to title %d failed", (gint) s->position);
1317 src->title = (gint) s->position;
1319 GST_INFO_OBJECT (src, "seek to title %d ok", src->title + 1);
1321 g_return_val_if_reached (FALSE);
1324 src->need_newsegment = TRUE;
1329 gst_dvd_read_src_src_event (GstBaseSrc * basesrc, GstEvent * event)
1331 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
1334 GST_LOG_OBJECT (src, "handling %s event", GST_EVENT_TYPE_NAME (event));
1336 switch (GST_EVENT_TYPE (event)) {
1337 case GST_EVENT_SEEK:
1338 res = gst_dvd_read_src_handle_seek_event (src, event);
1341 res = GST_BASE_SRC_CLASS (parent_class)->event (basesrc, event);
1349 gst_dvd_read_src_make_clut_change_event (GstDvdReadSrc * src,
1352 GstStructure *structure;
1356 structure = gst_structure_new ("application/x-gst-dvd",
1357 "event", G_TYPE_STRING, "dvd-spu-clut-change", NULL);
1359 /* Create a separate field for each value in the table. */
1360 for (i = 0; i < 16; i++) {
1361 g_snprintf (name, sizeof (name), "clut%02d", i);
1362 gst_structure_set (structure, name, G_TYPE_INT, (int) clut[i], NULL);
1365 /* Create the DVD event and put the structure into it. */
1366 return gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, structure);
1370 gst_dvd_read_src_convert_timecode (dvd_time_t * time)
1373 const gint64 one_hour = 3600 * GST_SECOND;
1374 const gint64 one_min = 60 * GST_SECOND;
1376 g_return_val_if_fail ((time->hour >> 4) < 0xa
1377 && (time->hour & 0xf) < 0xa, -1);
1378 g_return_val_if_fail ((time->minute >> 4) < 0x7
1379 && (time->minute & 0xf) < 0xa, -1);
1380 g_return_val_if_fail ((time->second >> 4) < 0x7
1381 && (time->second & 0xf) < 0xa, -1);
1383 ret_time = ((time->hour >> 4) * 10 + (time->hour & 0xf)) * one_hour;
1384 ret_time += ((time->minute >> 4) * 10 + (time->minute & 0xf)) * one_min;
1385 ret_time += ((time->second >> 4) * 10 + (time->second & 0xf)) * GST_SECOND;
1391 gst_dvd_read_src_do_duration_query (GstDvdReadSrc * src, GstQuery * query)
1396 gst_query_parse_duration (query, &format, NULL);
1399 case GST_FORMAT_TIME:{
1400 if (src->cur_pgc == NULL)
1402 val = gst_dvd_read_src_convert_timecode (&src->cur_pgc->playback_time);
1407 case GST_FORMAT_BYTES:{
1408 if (!gst_dvd_read_src_get_size (src, &val))
1413 if (format == sector_format) {
1414 val = DVDFileSize (src->dvd_title);
1415 } else if (format == title_format) {
1416 val = src->tt_srpt->nr_of_srpts;
1417 } else if (format == chapter_format) {
1418 val = src->num_chapters;
1419 } else if (format == angle_format) {
1420 val = src->tt_srpt->title[src->title].nr_of_angles;
1422 GST_DEBUG_OBJECT (src, "Don't know how to handle format %d (%s)",
1423 format, gst_format_get_name (format));
1430 GST_LOG_OBJECT (src, "duration = %" G_GINT64_FORMAT " %s", val,
1431 gst_format_get_name (format));
1433 gst_query_set_duration (query, format, val);
1438 gst_dvd_read_src_do_position_query (GstDvdReadSrc * src, GstQuery * query)
1443 gst_query_parse_position (query, &format, NULL);
1446 case GST_FORMAT_BYTES:{
1447 val = (gint64) src->cur_pack * DVD_VIDEO_LB_LEN;
1451 if (format == sector_format) {
1452 val = src->cur_pack;
1453 } else if (format == title_format) {
1455 } else if (format == chapter_format) {
1457 } else if (format == angle_format) {
1460 GST_DEBUG_OBJECT (src, "Don't know how to handle format %d (%s)",
1461 format, gst_format_get_name (format));
1468 GST_LOG_OBJECT (src, "position = %" G_GINT64_FORMAT " %s", val,
1469 gst_format_get_name (format));
1471 gst_query_set_position (query, format, val);
1476 gst_dvd_read_src_do_convert_query (GstDvdReadSrc * src, GstQuery * query)
1478 GstFormat src_format, dest_format;
1479 gboolean ret = FALSE;
1480 gint64 src_val, dest_val = -1;
1482 gst_query_parse_convert (query, &src_format, &src_val, &dest_format, NULL);
1484 if (src_format == dest_format) {
1490 /* Formats to consider: TIME, DEFAULT, BYTES, title, chapter, sector.
1491 * Note: title and chapter are counted as starting from 0 here, just like
1492 * in the context of seek events. Another note: DEFAULT format is undefined */
1494 if (src_format == GST_FORMAT_BYTES) {
1495 src_format = sector_format;
1496 src_val /= DVD_VIDEO_LB_LEN;
1499 if (src_format == sector_format) {
1501 if (dest_format == GST_FORMAT_TIME && src_val < G_MAXUINT) {
1502 dest_val = gst_dvd_read_src_get_time_for_sector (src, (guint) src_val);
1503 ret = (dest_val >= 0);
1504 } else if (dest_format == GST_FORMAT_BYTES) {
1505 dest_val = src_val * DVD_VIDEO_LB_LEN;
1510 } else if (src_format == title_format) {
1512 if (dest_format == GST_FORMAT_TIME) {
1513 /* not really true, but we use this to trick the base source into
1514 * handling seeks in title-format for us (the source won't know that
1515 * we changed the title in this case) (changing titles should really
1516 * be done with an interface rather than a seek, but for now we're
1517 * stuck with this mechanism. Fix in 0.11) */
1518 dest_val = (GstClockTime) 0;
1523 } else if (src_format == chapter_format) {
1524 /* CHAPTER => xyz */
1525 if (dest_format == GST_FORMAT_TIME) {
1526 if (src->num_chapters >= 0 && src_val < src->num_chapters) {
1527 dest_val = src->chapter_starts[src_val];
1530 } else if (dest_format == sector_format) {
1534 } else if (src_format == GST_FORMAT_TIME) {
1536 if (dest_format == sector_format || dest_format == GST_FORMAT_BYTES) {
1537 dest_val = gst_dvd_read_src_get_sector_from_time (src, src_val);
1538 ret = (dest_val >= 0);
1539 if (dest_format == GST_FORMAT_BYTES)
1540 dest_val *= DVD_VIDEO_LB_LEN;
1541 } else if (dest_format == chapter_format) {
1542 if (src->chapter_starts != NULL) {
1545 for (i = src->num_chapters - 1; i >= 0; --i) {
1546 if (src->chapter_starts && src->chapter_starts[i] >= src_val) {
1565 gst_query_set_convert (query, src_format, src_val, dest_format, dest_val);
1572 gst_dvd_read_src_src_query (GstBaseSrc * basesrc, GstQuery * query)
1574 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
1575 gboolean res = TRUE;
1577 GST_LOG_OBJECT (src, "handling %s query", GST_QUERY_TYPE_NAME (query));
1579 switch (GST_QUERY_TYPE (query)) {
1580 case GST_QUERY_DURATION:
1581 GST_OBJECT_LOCK (src);
1582 if (GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_FLAG_STARTED)) {
1583 res = gst_dvd_read_src_do_duration_query (src, query);
1585 GST_DEBUG_OBJECT (src, "query failed: not started");
1588 GST_OBJECT_UNLOCK (src);
1590 case GST_QUERY_POSITION:
1591 GST_OBJECT_LOCK (src);
1592 if (GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_FLAG_STARTED)) {
1593 res = gst_dvd_read_src_do_position_query (src, query);
1595 GST_DEBUG_OBJECT (src, "query failed: not started");
1598 GST_OBJECT_UNLOCK (src);
1600 case GST_QUERY_CONVERT:
1601 GST_OBJECT_LOCK (src);
1602 if (GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_FLAG_STARTED)) {
1603 res = gst_dvd_read_src_do_convert_query (src, query);
1605 GST_DEBUG_OBJECT (src, "query failed: not started");
1608 GST_OBJECT_UNLOCK (src);
1611 res = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
1619 gst_dvd_read_src_goto_sector (GstDvdReadSrc * src, int angle)
1621 gint seek_to = src->cur_pack;
1622 gint chapter, next, cur, i;
1624 /* retrieve position */
1626 GST_DEBUG_OBJECT (src, "Goto sector %d, angle %d, within %d chapters",
1627 seek_to, angle, src->num_chapters);
1629 for (i = 0; i < src->num_chapters; i++) {
1632 cur_title_get_chapter_bounds (src, i, &c1, &c2);
1633 GST_DEBUG_OBJECT (src, " Looking in chapter %d, bounds: %d %d", i, c1, c2);
1635 for (next = cur = c1; cur < c2;) {
1636 gint first = src->cur_pgc->cell_playback[cur].first_sector;
1637 gint last = src->cur_pgc->cell_playback[cur].last_sector;
1638 GST_DEBUG_OBJECT (src, "Cell %d sector bounds: %d %d", cur, first, last);
1640 if (src->cur_pgc->cell_playback[cur].block_type == BLOCK_TYPE_ANGLE_BLOCK)
1642 next = gst_dvd_read_src_get_next_cell (src, src->cur_pgc, cur);
1643 /* seeking to 0 should end up at first chapter in any case */
1644 if ((seek_to >= first && seek_to <= last) || (seek_to == 0 && i == 0)) {
1645 GST_DEBUG_OBJECT (src, "Seek target found in chapter %d", i);
1652 GST_DEBUG_OBJECT (src, "Seek to sector %u failed", seek_to);
1658 /* so chapter $chapter and cell $cur contain our sector
1659 * of interest. Let's go there! */
1660 GST_INFO_OBJECT (src, "Seek succeeded, going to chapter %u, cell %u",
1663 gst_dvd_read_src_goto_chapter (src, chapter);
1664 src->cur_cell = cur;
1665 src->next_cell = next;
1666 src->new_cell = FALSE;
1667 src->cur_pack = seek_to;
1674 /*** URI interface ***/
1677 gst_dvd_read_src_uri_get_type (GType type)
1682 static const gchar *const *
1683 gst_dvd_read_src_uri_get_protocols (GType type)
1685 static const gchar *protocols[] = { "dvd", NULL };
1691 gst_dvd_read_src_uri_get_uri (GstURIHandler * handler)
1693 GstDvdReadSrc *src = GST_DVD_READ_SRC (handler);
1696 GST_OBJECT_LOCK (src);
1697 uri = g_strdup_printf ("dvd://%d,%d,%d", src->uri_title, src->uri_chapter,
1699 GST_OBJECT_UNLOCK (src);
1705 gst_dvd_read_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
1708 GstDvdReadSrc *src = GST_DVD_READ_SRC (handler);
1710 /* parse out the new t/c/a and seek to them */
1712 gchar *location = NULL;
1717 location = gst_uri_get_location (uri);
1719 GST_OBJECT_LOCK (src);
1722 src->uri_chapter = 1;
1726 goto empty_location;
1728 strcur = strs = g_strsplit (location, ",", 0);
1729 while (strcur && *strcur) {
1732 if (!sscanf (*strcur, "%d", &val))
1736 g_warning ("Invalid value %d in URI '%s'. Must be 1 or greater",
1743 src->uri_title = val;
1746 src->uri_chapter = val;
1749 src->uri_angle = val;
1757 if (pos > 0 && GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_FLAG_STARTED)) {
1758 src->title = src->uri_title - 1;
1759 src->chapter = src->uri_chapter - 1;
1760 src->angle = src->uri_angle - 1;
1761 src->new_seek = TRUE;
1769 GST_OBJECT_UNLOCK (src);
1776 gst_dvd_read_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1778 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1780 iface->get_type = gst_dvd_read_src_uri_get_type;
1781 iface->get_protocols = gst_dvd_read_src_uri_get_protocols;
1782 iface->get_uri = gst_dvd_read_src_uri_get_uri;
1783 iface->set_uri = gst_dvd_read_src_uri_set_uri;
1787 plugin_init (GstPlugin * plugin)
1789 GST_DEBUG_CATEGORY_INIT (gstgst_dvd_read_src_debug, "dvdreadsrc", 0,
1790 "DVD reader element based on dvdreadsrc");
1793 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
1795 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1796 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1797 #endif /* ENABLE_NLS */
1799 if (!gst_element_register (plugin, "dvdreadsrc", GST_RANK_NONE,
1800 GST_TYPE_DVD_READ_SRC)) {
1807 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1810 "Access a DVD with dvdread",
1811 plugin_init, VERSION, "GPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);