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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, 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)
113 src->vts_file = NULL;
114 src->vmg_file = NULL;
115 src->dvd_title = NULL;
117 src->location = g_strdup ("/dev/dvd");
118 src->new_seek = TRUE;
119 src->new_cell = TRUE;
120 src->change_cell = FALSE;
122 src->uri_chapter = 1;
125 src->title_lang_event_pending = NULL;
126 src->pending_clut_event = NULL;
128 gst_pad_use_fixed_caps (GST_BASE_SRC_PAD (src));
129 gst_pad_set_caps (GST_BASE_SRC_PAD (src),
130 gst_static_pad_template_get_caps (&srctemplate));
134 gst_dvd_read_src_is_seekable (GstBaseSrc * src)
140 gst_dvd_read_src_class_init (GstDvdReadSrcClass * klass)
142 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
143 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
144 GstPushSrcClass *gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
145 GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
147 gobject_class->finalize = gst_dvd_read_src_finalize;
148 gobject_class->set_property = gst_dvd_read_src_set_property;
149 gobject_class->get_property = gst_dvd_read_src_get_property;
151 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DEVICE,
152 g_param_spec_string ("device", "Device",
153 "DVD device location", NULL,
154 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
155 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TITLE,
156 g_param_spec_int ("title", "title", "title",
157 1, 999, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
158 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CHAPTER,
159 g_param_spec_int ("chapter", "chapter", "chapter",
160 1, 999, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
161 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_ANGLE,
162 g_param_spec_int ("angle", "angle", "angle",
163 1, 999, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
165 gst_element_class_add_pad_template (gstelement_class,
166 gst_static_pad_template_get (&srctemplate));
168 gst_element_class_set_static_metadata (gstelement_class, "DVD Source",
170 "Access a DVD title/chapter/angle using libdvdread",
171 "Erik Walthinsen <omega@cse.ogi.edu>");
173 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_dvd_read_src_start);
174 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_dvd_read_src_stop);
175 gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_dvd_read_src_src_query);
176 gstbasesrc_class->event = GST_DEBUG_FUNCPTR (gst_dvd_read_src_src_event);
177 gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_dvd_read_src_do_seek);
178 gstbasesrc_class->is_seekable =
179 GST_DEBUG_FUNCPTR (gst_dvd_read_src_is_seekable);
181 gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_dvd_read_src_create);
183 title_format = gst_format_register ("title", "DVD title");
184 angle_format = gst_format_register ("angle", "DVD angle");
185 sector_format = gst_format_register ("sector", "DVD sector");
186 chapter_format = gst_format_register ("chapter", "DVD chapter");
190 gst_dvd_read_src_start (GstBaseSrc * basesrc)
192 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
194 g_return_val_if_fail (src->location != NULL, FALSE);
196 GST_DEBUG_OBJECT (src, "Opening DVD '%s'", src->location);
198 if ((src->dvd = DVDOpen (src->location)) == NULL)
201 /* Load the video manager to find out the information about the titles */
202 GST_DEBUG_OBJECT (src, "Loading VMG info");
204 if (!(src->vmg_file = ifoOpen (src->dvd, 0)))
205 goto ifo_open_failed;
207 src->tt_srpt = src->vmg_file->tt_srpt;
209 src->title = src->uri_title - 1;
210 src->chapter = src->uri_chapter - 1;
211 src->angle = src->uri_angle - 1;
213 if (!gst_dvd_read_src_goto_title (src, src->title, src->angle))
214 goto title_open_failed;
216 if (!gst_dvd_read_src_goto_chapter (src, src->chapter))
217 goto chapter_open_failed;
219 src->new_seek = FALSE;
220 src->change_cell = TRUE;
227 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
228 (_("Could not open DVD")),
229 ("DVDOpen(%s) failed: %s", src->location, g_strerror (errno)));
234 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
235 (_("Could not open DVD")),
236 ("ifoOpen() failed: %s", g_strerror (errno)));
241 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
242 (_("Could not open DVD title %d"), src->uri_title), (NULL));
247 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
248 (_("Failed to go to chapter %d of DVD title %d"),
249 src->uri_chapter, src->uri_title), (NULL));
255 gst_dvd_read_src_stop (GstBaseSrc * basesrc)
257 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
260 ifoClose (src->vts_file);
261 src->vts_file = NULL;
264 ifoClose (src->vmg_file);
265 src->vmg_file = NULL;
267 if (src->dvd_title) {
268 DVDCloseFile (src->dvd_title);
269 src->dvd_title = NULL;
275 src->new_cell = TRUE;
276 src->new_seek = TRUE;
277 src->change_cell = FALSE;
280 src->need_newsegment = TRUE;
281 src->vts_tmapt = NULL;
282 if (src->title_lang_event_pending) {
283 gst_event_unref (src->title_lang_event_pending);
284 src->title_lang_event_pending = NULL;
286 if (src->pending_clut_event) {
287 gst_event_unref (src->pending_clut_event);
288 src->pending_clut_event = NULL;
290 if (src->chapter_starts) {
291 g_free (src->chapter_starts);
292 src->chapter_starts = NULL;
295 GST_LOG_OBJECT (src, "closed DVD");
301 cur_title_get_chapter_pgc (GstDvdReadSrc * src, gint chapter, gint * p_pgn,
302 gint * p_pgc_id, pgc_t ** p_pgc)
307 g_assert (chapter >= 0 && chapter < src->num_chapters);
309 pgc_id = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter].pgcn;
310 pgn = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter].pgn;
311 pgc = src->vts_file->vts_pgcit->pgci_srp[pgc_id - 1].pgc;
319 cur_title_get_chapter_bounds (GstDvdReadSrc * src, gint chapter,
320 gint * p_first_cell, gint * p_last_cell)
323 gint pgn, pgc_id, pgn_next_ch;
325 g_assert (chapter >= 0 && chapter < src->num_chapters);
327 cur_title_get_chapter_pgc (src, chapter, &pgn, &pgc_id, &pgc);
329 *p_first_cell = pgc->program_map[pgn - 1] - 1;
331 if (chapter == (src->num_chapters - 1)) {
332 *p_last_cell = pgc->nr_of_cells - 1;
334 pgn_next_ch = src->vts_ptt_srpt->title[src->ttn - 1].ptt[chapter + 1].pgn;
335 *p_last_cell = pgc->program_map[pgn_next_ch - 1] - 1;
338 GST_DEBUG_OBJECT (src, "Chapter %d bounds: %d %d (within %d cells)",
339 chapter, *p_first_cell, *p_last_cell, pgc->nr_of_cells);
343 gst_dvd_read_src_goto_chapter (GstDvdReadSrc * src, gint chapter)
347 /* make sure the chapter number is valid for this title */
348 if (chapter < 0 || chapter >= src->num_chapters) {
349 GST_WARNING_OBJECT (src, "invalid chapter %d (only %d available)",
350 chapter, src->num_chapters);
351 chapter = CLAMP (chapter, 0, src->num_chapters - 1);
354 /* determine which program chain we want to watch. This is
355 * based on the chapter number */
356 cur_title_get_chapter_pgc (src, chapter, &src->pgn, &src->pgc_id,
358 cur_title_get_chapter_bounds (src, chapter, &src->start_cell,
361 GST_LOG_OBJECT (src, "Opened chapter %d - cell %d-%d", chapter + 1,
362 src->start_cell, src->last_cell);
364 /* retrieve position */
366 for (i = 0; i < chapter; i++) {
369 cur_title_get_chapter_bounds (src, i, &c1, &c2);
373 src->cur_pgc->cell_playback[c1].last_sector -
374 src->cur_pgc->cell_playback[c1].first_sector;
379 /* prepare reading for new cell */
380 src->new_cell = TRUE;
381 src->next_cell = src->start_cell;
383 src->chapter = chapter;
385 if (src->pending_clut_event)
386 gst_event_unref (src->pending_clut_event);
388 src->pending_clut_event =
389 gst_dvd_read_src_make_clut_change_event (src, src->cur_pgc->palette);
395 gst_dvd_read_src_get_chapter_starts (GstDvdReadSrc * src)
397 GstClockTime uptohere;
400 g_free (src->chapter_starts);
401 src->chapter_starts = g_new (GstClockTime, src->num_chapters);
403 uptohere = (GstClockTime) 0;
404 for (c = 0; c < src->num_chapters; ++c) {
405 GstClockTime chapter_duration = 0;
406 gint cell_start, cell_end, cell;
410 cur_title_get_chapter_pgc (src, c, &pgn, &pgc_id, &pgc);
411 cur_title_get_chapter_bounds (src, c, &cell_start, &cell_end);
414 while (cell < cell_end) {
415 dvd_time_t *cell_duration;
417 cell_duration = &pgc->cell_playback[cell].playback_time;
418 chapter_duration += gst_dvd_read_src_convert_timecode (cell_duration);
419 cell = gst_dvd_read_src_get_next_cell (src, pgc, cell);
422 src->chapter_starts[c] = uptohere;
424 GST_INFO_OBJECT (src, "[%02u] Chapter %02u starts at %" GST_TIME_FORMAT
425 ", dur = %" GST_TIME_FORMAT ", cells %d-%d", src->title + 1, c + 1,
426 GST_TIME_ARGS (uptohere), GST_TIME_ARGS (chapter_duration),
427 cell_start, cell_end);
429 uptohere += chapter_duration;
434 gst_dvd_read_src_goto_title (GstDvdReadSrc * src, gint title, gint angle)
437 gchar lang_code[3] = { '\0', '\0', '\0' }, *t;
444 /* make sure our title number is valid */
445 num_titles = src->tt_srpt->nr_of_srpts;
446 GST_INFO_OBJECT (src, "There are %d titles on this DVD", num_titles);
447 if (title < 0 || title >= num_titles)
450 src->num_chapters = src->tt_srpt->title[title].nr_of_ptts;
451 GST_INFO_OBJECT (src, "Title %d has %d chapters", title + 1,
454 /* make sure the angle number is valid for this title */
455 src->num_angles = src->tt_srpt->title[title].nr_of_angles;
456 GST_LOG_OBJECT (src, "Title %d has %d angles", title + 1, src->num_angles);
457 if (angle < 0 || angle >= src->num_angles) {
458 GST_WARNING_OBJECT (src, "Invalid angle %d (only %d available)",
459 angle, src->num_angles);
460 angle = CLAMP (angle, 0, src->num_angles - 1);
463 /* load the VTS information for the title set our title is in */
464 title_set_nr = src->tt_srpt->title[title].title_set_nr;
465 src->vts_file = ifoOpen (src->dvd, title_set_nr);
466 if (src->vts_file == NULL)
467 goto ifo_open_failed;
469 src->ttn = src->tt_srpt->title[title].vts_ttn;
470 src->vts_ptt_srpt = src->vts_file->vts_ptt_srpt;
472 /* interactive title? */
473 if (src->num_chapters > 0 &&
474 src->vts_ptt_srpt->title[src->ttn - 1].ptt[0].pgn == 0) {
475 goto commands_only_pgc;
478 /* we've got enough info, time to open the title set data */
479 src->dvd_title = DVDOpenFile (src->dvd, title_set_nr, DVD_READ_TITLE_VOBS);
480 if (src->dvd_title == NULL)
481 goto title_open_failed;
483 GST_INFO_OBJECT (src, "Opened title %d, angle %d", title + 1, angle);
489 if (src->title_lang_event_pending) {
490 gst_event_unref (src->title_lang_event_pending);
491 src->title_lang_event_pending = NULL;
494 s = gst_structure_new ("application/x-gst-dvd",
495 "event", G_TYPE_STRING, "dvd-lang-codes", NULL);
497 /* so we can filter out invalid/unused streams (same for all chapters) */
498 cur_title_get_chapter_pgc (src, 0, &pgn0, &pgc0_id, &pgc0);
501 for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_audio_streams; i++) {
502 const audio_attr_t *a;
504 /* audio stream present? */
505 if (pgc0 != NULL && (pgc0->audio_control[i] & 0x8000) == 0)
508 a = &src->vts_file->vtsi_mat->vts_audio_attr[i];
510 t = g_strdup_printf ("audio-%d-format", i);
511 gst_structure_set (s, t, G_TYPE_INT, (int) a->audio_format, NULL);
515 t = g_strdup_printf ("audio-%d-language", i);
516 lang_code[0] = (a->lang_code >> 8) & 0xff;
517 lang_code[1] = a->lang_code & 0xff;
518 gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
524 GST_INFO_OBJECT (src, "[%02d] Audio %02d: lang='%s', format=%d",
525 src->title + 1, i, lang_code, (gint) a->audio_format);
529 for (i = 0; i < src->vts_file->vtsi_mat->nr_of_vts_subp_streams; i++) {
530 const subp_attr_t *u;
531 const video_attr_t *v;
534 /* subpicture stream present? */
535 if (pgc0 != NULL && (pgc0->subp_control[i] & 0x80000000) == 0)
538 u = &src->vts_file->vtsi_mat->vts_subp_attr[i];
539 v = &src->vts_file->vtsi_mat->vts_video_attr;
543 if (v->display_aspect_ratio == 0) /* 4:3 */
544 sid = (pgc0->subp_control[i] >> 24) & 0x1f;
545 else if (v->display_aspect_ratio == 3) /* 16:9 */
546 sid = (pgc0->subp_control[i] >> 8) & 0x1f;
550 t = g_strdup_printf ("subtitle-%d-language", sid);
551 lang_code[0] = (u->lang_code >> 8) & 0xff;
552 lang_code[1] = u->lang_code & 0xff;
553 gst_structure_set (s, t, G_TYPE_STRING, lang_code, NULL);
559 GST_INFO_OBJECT (src, "[%02d] Subtitle %02d: lang='%s', type=%d",
560 src->title + 1, sid, lang_code, u->type);
563 src->title_lang_event_pending =
564 gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
566 /* dump seek tables */
567 src->vts_tmapt = src->vts_file->vts_tmapt;
568 if (src->vts_tmapt) {
571 GST_LOG_OBJECT (src, "nr_of_tmaps = %d", src->vts_tmapt->nr_of_tmaps);
572 for (i = 0; i < src->vts_tmapt->nr_of_tmaps; ++i) {
573 GST_LOG_OBJECT (src, "======= Table %d ===================", i);
574 GST_LOG_OBJECT (src, "Offset relative to VTS_TMAPTI: %d",
575 src->vts_tmapt->tmap_offset[i]);
576 GST_LOG_OBJECT (src, "Time unit (seconds) : %d",
577 src->vts_tmapt->tmap[i].tmu);
578 GST_LOG_OBJECT (src, "Number of entries : %d",
579 src->vts_tmapt->tmap[i].nr_of_entries);
580 for (j = 0; j < src->vts_tmapt->tmap[i].nr_of_entries; j++) {
583 time = src->vts_tmapt->tmap[i].tmu * (j + 1) * GST_SECOND;
584 GST_LOG_OBJECT (src, "Time: %" GST_TIME_FORMAT " VOBU "
585 "Sector: 0x%08x %s", GST_TIME_ARGS (time),
586 src->vts_tmapt->tmap[i].map_ent[j] & 0x7fffffff,
587 (src->vts_tmapt->tmap[i].map_ent[j] >> 31) ? "discontinuity" : "");
591 GST_WARNING_OBJECT (src, "no vts_tmapt - seeking will suck");
594 gst_dvd_read_src_get_chapter_starts (src);
601 GST_WARNING_OBJECT (src, "Invalid title %d (only %d available)",
607 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
608 (_("Could not open DVD title %d"), title_set_nr),
609 ("ifoOpen(%d) failed: %s", title_set_nr, g_strerror (errno)));
614 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
615 (_("Could not open DVD title %d"), title_set_nr),
616 ("Can't open title VOBS (VTS_%02d_1.VOB)", title_set_nr));
621 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
622 (_("Could not open DVD title %d. Interactive titles are not supported "
623 "by this element"), title_set_nr),
624 ("Commands-only PGC, not supported, use rsndvdbin"));
629 /* FIXME: double-check this function, compare against original */
631 gst_dvd_read_src_get_next_cell (GstDvdReadSrc * src, pgc_t * pgc, gint cell)
633 /* Check if we're entering an angle block. */
634 if (pgc->cell_playback[cell].block_type != BLOCK_TYPE_ANGLE_BLOCK)
637 while (pgc->cell_playback[cell].block_mode != BLOCK_MODE_LAST_CELL)
643 /* Returns true if the pack is a NAV pack */
645 gst_dvd_read_src_is_nav_pack (const guint8 * data, gint lbn, dsi_t * dsi_pack)
647 if (GST_READ_UINT32_BE (data + 0x26) != 0x000001BF)
650 /* Check that this is substream 0 (PCI) */
654 if (GST_READ_UINT32_BE (data + 0x400) != 0x000001BF)
657 /* Check that this is substream 1 (DSI) */
658 if (data[0x406] != 1)
661 /* Check sizes of PCI and DSI packets */
662 if (GST_READ_UINT16_BE (data + 0x2a) != 0x03d4)
665 if (GST_READ_UINT16_BE (data + 0x404) != 0x03fa)
668 /* Read the DSI packet into the provided struct and check it */
669 navRead_DSI (dsi_pack, (unsigned char *) data + DSI_START_BYTE);
670 if (lbn != dsi_pack->dsi_gi.nv_pck_lbn)
676 /* find time for sector from index, returns NONE if there is no exact match */
678 gst_dvd_read_src_get_time_for_sector (GstDvdReadSrc * src, guint sector)
682 if (src->vts_tmapt == NULL || src->vts_tmapt->nr_of_tmaps == 0)
683 return GST_CLOCK_TIME_NONE;
685 for (i = 0; i < src->vts_tmapt->nr_of_tmaps; ++i) {
686 for (j = 0; j < src->vts_tmapt->tmap[i].nr_of_entries; ++j) {
687 if ((src->vts_tmapt->tmap[i].map_ent[j] & 0x7fffffff) == sector)
688 return src->vts_tmapt->tmap[i].tmu * (j + 1) * GST_SECOND;
693 return (GstClockTime) 0;
695 return GST_CLOCK_TIME_NONE;
698 /* returns the sector in the index at (or before) the given time, or -1 */
700 gst_dvd_read_src_get_sector_from_time (GstDvdReadSrc * src, GstClockTime ts)
704 if (src->vts_tmapt == NULL || src->vts_tmapt->nr_of_tmaps < src->ttn)
708 for (j = 0; j < src->vts_tmapt->tmap[src->ttn - 1].nr_of_entries; ++j) {
709 GstClockTime entry_time;
711 entry_time = src->vts_tmapt->tmap[src->ttn - 1].tmu * (j + 1) * GST_SECOND;
712 if (entry_time <= ts) {
713 sector = src->vts_tmapt->tmap[src->ttn - 1].map_ent[j] & 0x7fffffff;
715 if (entry_time >= ts) {
729 GST_DVD_READ_ERROR = -1,
730 GST_DVD_READ_EOS = -2,
731 GST_DVD_READ_AGAIN = -3
734 static GstDvdReadReturn
735 gst_dvd_read_src_read (GstDvdReadSrc * src, gint angle, gint new_seek,
740 guint8 oneblock[DVD_VIDEO_LB_LEN];
742 guint next_vobu, cur_output_size;
748 seg = &(GST_BASE_SRC (src)->segment);
750 /* playback by cell in this pgc, starting at the cell for our chapter */
752 src->cur_cell = src->start_cell;
756 if (src->cur_cell >= src->last_cell) {
757 /* advance to next chapter */
758 if (src->chapter == (src->num_chapters - 1) ||
759 (seg->format == chapter_format && seg->stop != -1 &&
760 src->chapter == (seg->stop - 1))) {
761 GST_DEBUG_OBJECT (src, "end of chapter segment");
765 GST_INFO_OBJECT (src, "end of chapter %d, switch to next",
769 gst_dvd_read_src_goto_chapter (src, src->chapter);
771 return GST_DVD_READ_AGAIN;
774 if (src->new_cell || new_seek) {
776 src->cur_cell = src->next_cell;
777 if (src->cur_cell >= src->last_cell) {
778 GST_LOG_OBJECT (src, "last cell in chapter");
783 /* take angle into account */
784 if (src->cur_pgc->cell_playback[src->cur_cell].block_type
785 == BLOCK_TYPE_ANGLE_BLOCK)
786 src->cur_cell += angle;
788 /* calculate next cell */
790 gst_dvd_read_src_get_next_cell (src, src->cur_pgc, src->cur_cell);
792 /* we loop until we're out of this cell */
793 src->cur_pack = src->cur_pgc->cell_playback[src->cur_cell].first_sector;
794 src->new_cell = FALSE;
795 GST_DEBUG_OBJECT (src, "Starting new cell %d @ pack %d", src->cur_cell,
799 if (src->cur_pack >= src->cur_pgc->cell_playback[src->cur_cell].last_sector) {
800 src->new_cell = TRUE;
801 GST_LOG_OBJECT (src, "Beyond last sector for cell %d, going to next cell",
803 return GST_DVD_READ_AGAIN;
806 /* read NAV packet */
811 len = DVDReadBlocks (src->dvd_title, src->cur_pack, 1, oneblock);
815 if (!gst_dvd_read_src_is_nav_pack (oneblock, src->cur_pack, &dsi_pack)) {
816 GST_LOG_OBJECT (src, "Skipping nav packet @ pack %d", src->cur_pack);
819 if (retries < 2000) {
822 GST_LOG_OBJECT (src, "No nav packet @ pack %d after 2000 blocks",
828 /* determine where we go next. These values are the ones we
829 * mostly care about */
830 cur_output_size = dsi_pack.dsi_gi.vobu_ea + 1;
832 /* If we're not at the end of this cell, we can determine the next
833 * VOBU to display using the VOBU_SRI information section of the
834 * DSI. Using this value correctly follows the current angle,
835 * avoiding the doubled scenes in The Matrix, and makes our life
838 * Otherwise, we set our next address past the end of this cell to
839 * force the code above to go to the next cell in the program. */
840 if (dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL) {
841 next_vobu = src->cur_pack + (dsi_pack.vobu_sri.next_vobu & 0x7fffffff);
843 next_vobu = src->cur_pgc->cell_playback[src->cur_cell].last_sector + 1;
846 g_assert (cur_output_size < 1024);
848 /* create the buffer (TODO: use buffer pool?) */
850 gst_buffer_new_allocate (NULL, cur_output_size * DVD_VIDEO_LB_LEN, NULL);
852 GST_LOG_OBJECT (src, "Going to read %u sectors @ pack %d", cur_output_size,
855 gst_buffer_map (buf, &map, GST_MAP_WRITE);
856 /* read in and output cursize packs */
858 DVDReadBlocks (src->dvd_title, src->cur_pack, cur_output_size, map.data);
860 if (len != cur_output_size)
861 goto block_read_error;
863 gst_buffer_unmap (buf, &map);
864 gst_buffer_resize (buf, 0, cur_output_size * DVD_VIDEO_LB_LEN);
865 /* GST_BUFFER_OFFSET (buf) = priv->cur_pack * DVD_VIDEO_LB_LEN; */
866 GST_BUFFER_TIMESTAMP (buf) =
867 gst_dvd_read_src_get_time_for_sector (src, src->cur_pack);
871 GST_LOG_OBJECT (src, "Read %u sectors", cur_output_size);
873 src->cur_pack = next_vobu;
875 next_time = GST_BUFFER_TIMESTAMP (buf);
876 if (GST_CLOCK_TIME_IS_VALID (next_time) && seg->format == GST_FORMAT_TIME &&
877 GST_CLOCK_TIME_IS_VALID (seg->stop) &&
878 next_time > seg->stop + 5 * GST_SECOND) {
879 GST_DEBUG_OBJECT (src, "end of TIME segment");
883 return GST_DVD_READ_OK;
888 GST_INFO_OBJECT (src, "Reached end-of-segment/stream - EOS");
889 return GST_DVD_READ_EOS;
893 GST_ERROR_OBJECT (src, "Read failed for block %d", src->cur_pack);
894 return GST_DVD_READ_ERROR;
898 GST_ERROR_OBJECT (src, "Read failed for %d blocks at %d",
899 cur_output_size, src->cur_pack);
900 gst_buffer_unmap (buf, &map);
901 gst_buffer_unref (buf);
902 return GST_DVD_READ_ERROR;
906 /* we don't cache the result on purpose */
908 gst_dvd_read_descrambler_available (void)
914 module = g_module_open ("libdvdcss", 0);
915 if (module != NULL) {
916 res = g_module_symbol (module, "dvdcss_open", &sym);
917 g_module_close (module);
926 gst_dvd_read_src_create (GstPushSrc * pushsrc, GstBuffer ** p_buf)
928 GstDvdReadSrc *src = GST_DVD_READ_SRC (pushsrc);
932 g_return_val_if_fail (src->dvd != NULL, GST_FLOW_ERROR);
934 srcpad = GST_BASE_SRC (src)->srcpad;
936 if (src->need_newsegment) {
939 gst_segment_init (&seg, GST_FORMAT_BYTES);
940 seg.start = src->cur_pack * DVD_VIDEO_LB_LEN;
943 gst_pad_push_event (srcpad, gst_event_new_segment (&seg));
944 src->need_newsegment = FALSE;
948 gst_dvd_read_src_goto_title (src, src->title, src->angle);
949 gst_dvd_read_src_goto_chapter (src, src->chapter);
951 src->new_seek = FALSE;
952 src->change_cell = TRUE;
955 if (src->title_lang_event_pending) {
956 gst_pad_push_event (srcpad, src->title_lang_event_pending);
957 src->title_lang_event_pending = NULL;
960 if (src->pending_clut_event) {
961 gst_pad_push_event (srcpad, src->pending_clut_event);
962 src->pending_clut_event = NULL;
967 res = gst_dvd_read_src_read (src, src->angle, src->change_cell, p_buf);
968 } while (res == GST_DVD_READ_AGAIN);
971 case GST_DVD_READ_ERROR:{
972 /* FIXME: figure out a way to detect if scrambling is the problem */
973 if (!gst_dvd_read_descrambler_available ()) {
974 GST_ELEMENT_ERROR (src, RESOURCE, READ,
975 (_("Could not read DVD. This may be because the DVD is encrypted "
976 "and a DVD decryption library is not installed.")), (NULL));
978 GST_ELEMENT_ERROR (src, RESOURCE, READ, (_("Could not read DVD.")),
981 return GST_FLOW_ERROR;
983 case GST_DVD_READ_EOS:{
986 case GST_DVD_READ_OK:{
987 src->change_cell = FALSE;
994 g_return_val_if_reached (GST_FLOW_EOS);
998 gst_dvd_read_src_set_property (GObject * object, guint prop_id,
999 const GValue * value, GParamSpec * pspec)
1001 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
1004 GST_OBJECT_LOCK (src);
1005 started = GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_FLAG_STARTED);
1010 g_warning ("%s: property '%s' needs to be set before the device is "
1011 "opened", GST_ELEMENT_NAME (src), pspec->name);
1015 g_free (src->location);
1016 /* clear the filename if we get a NULL (is that possible?) */
1017 if (g_value_get_string (value) == NULL) {
1018 src->location = g_strdup ("/dev/dvd");
1020 src->location = g_strdup (g_value_get_string (value));
1025 src->uri_title = g_value_get_int (value);
1027 src->title = src->uri_title - 1;
1028 src->new_seek = TRUE;
1032 src->uri_chapter = g_value_get_int (value);
1034 src->chapter = src->uri_chapter - 1;
1035 src->new_seek = TRUE;
1039 src->uri_angle = g_value_get_int (value);
1041 src->angle = src->uri_angle - 1;
1045 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1049 GST_OBJECT_UNLOCK (src);
1053 gst_dvd_read_src_get_property (GObject * object, guint prop_id, GValue * value,
1056 GstDvdReadSrc *src = GST_DVD_READ_SRC (object);
1058 GST_OBJECT_LOCK (src);
1062 g_value_set_string (value, src->location);
1065 g_value_set_int (value, src->uri_title);
1068 g_value_set_int (value, src->uri_chapter);
1071 g_value_set_int (value, src->uri_angle);
1074 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1078 GST_OBJECT_UNLOCK (src);
1082 gst_dvd_read_src_get_size (GstDvdReadSrc * src, gint64 * size)
1084 gboolean ret = FALSE;
1086 if (src->dvd_title) {
1089 blocks = DVDFileSize (src->dvd_title);
1091 *size = (gint64) blocks *DVD_VIDEO_LB_LEN;
1095 GST_WARNING_OBJECT (src, "DVDFileSize(%p) failed!", src->dvd_title);
1102 /*** Querying and seeking ***/
1105 gst_dvd_read_src_handle_seek_event (GstDvdReadSrc * src, GstEvent * event)
1108 GstSeekType cur_type, end_type;
1109 gint64 new_off, total;
1115 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &new_off,
1119 GST_DEBUG_OBJECT (src, "cannot do backwards playback yet");
1123 if (end_type != GST_SEEK_TYPE_NONE) {
1124 if ((format != chapter_format && format != GST_FORMAT_TIME) ||
1125 end_type != GST_SEEK_TYPE_SET) {
1126 GST_DEBUG_OBJECT (src, "end seek type not supported");
1131 if (cur_type != GST_SEEK_TYPE_SET) {
1132 GST_DEBUG_OBJECT (src, "only SEEK_TYPE_SET is supported");
1136 if (format == angle_format) {
1137 GST_OBJECT_LOCK (src);
1138 if (new_off < 0 || new_off >= src->num_angles) {
1139 GST_OBJECT_UNLOCK (src);
1140 GST_DEBUG_OBJECT (src, "invalid angle %d, only %d available",
1141 src->num_angles, src->num_angles);
1144 src->angle = (gint) new_off;
1145 GST_OBJECT_UNLOCK (src);
1146 GST_DEBUG_OBJECT (src, "switched to angle %d", (gint) new_off + 1);
1150 if (format != chapter_format && format != title_format &&
1151 format != GST_FORMAT_BYTES && format != GST_FORMAT_TIME) {
1152 GST_DEBUG_OBJECT (src, "unsupported seek format %d (%s)", format,
1153 gst_format_get_name (format));
1157 if (format == GST_FORMAT_BYTES) {
1158 GST_DEBUG_OBJECT (src, "Requested seek to byte %" G_GUINT64_FORMAT,
1160 } else if (format == GST_FORMAT_TIME) {
1161 GST_DEBUG_OBJECT (src, "Requested seek to time %" GST_TIME_FORMAT,
1162 GST_TIME_ARGS (new_off));
1163 if (gst_dvd_read_src_get_sector_from_time (src, new_off) < 0) {
1164 GST_DEBUG_OBJECT (src, "Can't find sector for requested time");
1169 srcpad = GST_BASE_SRC_PAD (src);
1171 /* check whether the seek looks reasonable (ie within possible range) */
1172 if (format == GST_FORMAT_BYTES) {
1173 GST_OBJECT_LOCK (src);
1174 query_ok = gst_dvd_read_src_get_size (src, &total);
1175 GST_OBJECT_UNLOCK (src);
1177 query_ok = gst_pad_query_duration (srcpad, format, &total);
1181 GST_DEBUG_OBJECT (src, "Failed to query duration in format %s",
1182 gst_format_get_name (format));
1186 GST_DEBUG_OBJECT (src, "Total %s: %12" G_GINT64_FORMAT,
1187 gst_format_get_name (format), total);
1188 GST_DEBUG_OBJECT (src, "Seek to %s: %12" G_GINT64_FORMAT,
1189 gst_format_get_name (format), new_off);
1191 if (new_off >= total) {
1192 GST_DEBUG_OBJECT (src, "Seek position out of range");
1196 /* set segment to seek format; this allows us to use the do_seek
1197 * virtual function and let the base source handle all the tricky
1198 * stuff for us. We don't use the segment internally anyway */
1199 /* FIXME: can't take the stream lock here - what to do? */
1200 GST_OBJECT_LOCK (src);
1201 GST_BASE_SRC (src)->segment.format = format;
1202 GST_BASE_SRC (src)->segment.start = 0;
1203 GST_BASE_SRC (src)->segment.stop = total;
1204 GST_BASE_SRC (src)->segment.duration = total;
1205 GST_OBJECT_UNLOCK (src);
1207 return GST_BASE_SRC_CLASS (parent_class)->event (GST_BASE_SRC (src), event);
1211 gst_dvd_read_src_get_sector_bounds (GstDvdReadSrc * src, gint * first,
1215 cur_title_get_chapter_bounds (src, 0, &c1, &tmp);
1216 cur_title_get_chapter_bounds (src, src->num_chapters - 1, &tmp, &c2);
1217 *first = src->cur_pgc->cell_playback[c1].first_sector;
1218 *last = src->cur_pgc->cell_playback[c2].last_sector;
1222 gst_dvd_read_src_do_seek (GstBaseSrc * basesrc, GstSegment * s)
1226 src = GST_DVD_READ_SRC (basesrc);
1228 GST_DEBUG_OBJECT (src, "Seeking to %s: %12" G_GINT64_FORMAT,
1229 gst_format_get_name (s->format), s->position);
1231 if (s->format == sector_format || s->format == GST_FORMAT_BYTES
1232 || s->format == GST_FORMAT_TIME) {
1235 old = src->cur_pack;
1237 if (s->format == sector_format) {
1239 gst_dvd_read_src_get_sector_bounds (src, &first, &last);
1240 GST_DEBUG_OBJECT (src, "Format is sector, seeking to %" G_GINT64_FORMAT,
1242 src->cur_pack = s->position;
1243 if (src->cur_pack < first)
1244 src->cur_pack = first;
1245 if (src->cur_pack > last)
1246 src->cur_pack = last;
1247 } else if (s->format == GST_FORMAT_TIME) {
1249 GST_DEBUG_OBJECT (src, "Format is time");
1251 sector = gst_dvd_read_src_get_sector_from_time (src, s->position);
1253 GST_DEBUG_OBJECT (src, "Time %" GST_TIME_FORMAT " => sector %d",
1254 GST_TIME_ARGS (s->position), sector);
1256 /* really shouldn't happen, we've checked this earlier ... */
1257 g_return_val_if_fail (sector >= 0, FALSE);
1259 src->cur_pack = sector;
1263 gst_dvd_read_src_get_sector_bounds (src, &first, &last);
1264 GST_DEBUG_OBJECT (src, "Format is byte");
1265 src->cur_pack = s->position / DVD_VIDEO_LB_LEN;
1266 if (((gint64) src->cur_pack * DVD_VIDEO_LB_LEN) != s->position) {
1267 GST_LOG_OBJECT (src, "rounded down offset %" G_GINT64_FORMAT " => %"
1268 G_GINT64_FORMAT, s->position,
1269 (gint64) src->cur_pack * DVD_VIDEO_LB_LEN);
1271 src->cur_pack += first;
1274 if (!gst_dvd_read_src_goto_sector (src, src->angle)) {
1275 GST_DEBUG_OBJECT (src, "seek to sector 0x%08x failed", src->cur_pack);
1276 src->cur_pack = old;
1280 GST_LOG_OBJECT (src, "seek to sector 0x%08x ok", src->cur_pack);
1281 } else if (s->format == chapter_format) {
1282 if (!gst_dvd_read_src_goto_chapter (src, (gint) s->position)) {
1283 GST_DEBUG_OBJECT (src, "seek to chapter %d failed",
1284 (gint) s->position + 1);
1287 GST_INFO_OBJECT (src, "seek to chapter %d ok", (gint) s->position + 1);
1288 src->chapter = s->position;
1289 } else if (s->format == title_format) {
1290 if (!gst_dvd_read_src_goto_title (src, (gint) s->position, src->angle) ||
1291 !gst_dvd_read_src_goto_chapter (src, 0)) {
1292 GST_DEBUG_OBJECT (src, "seek to title %d failed", (gint) s->position);
1295 src->title = (gint) s->position;
1297 GST_INFO_OBJECT (src, "seek to title %d ok", src->title + 1);
1299 g_return_val_if_reached (FALSE);
1302 src->need_newsegment = TRUE;
1307 gst_dvd_read_src_src_event (GstBaseSrc * basesrc, GstEvent * event)
1309 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
1312 GST_LOG_OBJECT (src, "handling %s event", GST_EVENT_TYPE_NAME (event));
1314 switch (GST_EVENT_TYPE (event)) {
1315 case GST_EVENT_SEEK:
1316 res = gst_dvd_read_src_handle_seek_event (src, event);
1319 res = GST_BASE_SRC_CLASS (parent_class)->event (basesrc, event);
1327 gst_dvd_read_src_make_clut_change_event (GstDvdReadSrc * src,
1330 GstStructure *structure;
1334 structure = gst_structure_new ("application/x-gst-dvd",
1335 "event", G_TYPE_STRING, "dvd-spu-clut-change", NULL);
1337 /* Create a separate field for each value in the table. */
1338 for (i = 0; i < 16; i++) {
1339 g_snprintf (name, sizeof (name), "clut%02d", i);
1340 gst_structure_set (structure, name, G_TYPE_INT, (int) clut[i], NULL);
1343 /* Create the DVD event and put the structure into it. */
1344 return gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, structure);
1348 gst_dvd_read_src_convert_timecode (dvd_time_t * time)
1351 const gint64 one_hour = 3600 * GST_SECOND;
1352 const gint64 one_min = 60 * GST_SECOND;
1354 g_return_val_if_fail ((time->hour >> 4) < 0xa
1355 && (time->hour & 0xf) < 0xa, -1);
1356 g_return_val_if_fail ((time->minute >> 4) < 0x7
1357 && (time->minute & 0xf) < 0xa, -1);
1358 g_return_val_if_fail ((time->second >> 4) < 0x7
1359 && (time->second & 0xf) < 0xa, -1);
1361 ret_time = ((time->hour >> 4) * 10 + (time->hour & 0xf)) * one_hour;
1362 ret_time += ((time->minute >> 4) * 10 + (time->minute & 0xf)) * one_min;
1363 ret_time += ((time->second >> 4) * 10 + (time->second & 0xf)) * GST_SECOND;
1369 gst_dvd_read_src_do_duration_query (GstDvdReadSrc * src, GstQuery * query)
1374 gst_query_parse_duration (query, &format, NULL);
1377 case GST_FORMAT_TIME:{
1378 if (src->cur_pgc == NULL)
1380 val = gst_dvd_read_src_convert_timecode (&src->cur_pgc->playback_time);
1385 case GST_FORMAT_BYTES:{
1386 if (!gst_dvd_read_src_get_size (src, &val))
1391 if (format == sector_format) {
1392 val = DVDFileSize (src->dvd_title);
1393 } else if (format == title_format) {
1394 val = src->tt_srpt->nr_of_srpts;
1395 } else if (format == chapter_format) {
1396 val = src->num_chapters;
1397 } else if (format == angle_format) {
1398 val = src->tt_srpt->title[src->title].nr_of_angles;
1400 GST_DEBUG_OBJECT (src, "Don't know how to handle format %d (%s)",
1401 format, gst_format_get_name (format));
1408 GST_LOG_OBJECT (src, "duration = %" G_GINT64_FORMAT " %s", val,
1409 gst_format_get_name (format));
1411 gst_query_set_duration (query, format, val);
1416 gst_dvd_read_src_do_position_query (GstDvdReadSrc * src, GstQuery * query)
1421 gst_query_parse_position (query, &format, NULL);
1424 case GST_FORMAT_BYTES:{
1425 val = (gint64) src->cur_pack * DVD_VIDEO_LB_LEN;
1429 if (format == sector_format) {
1430 val = src->cur_pack;
1431 } else if (format == title_format) {
1433 } else if (format == chapter_format) {
1435 } else if (format == angle_format) {
1438 GST_DEBUG_OBJECT (src, "Don't know how to handle format %d (%s)",
1439 format, gst_format_get_name (format));
1446 GST_LOG_OBJECT (src, "position = %" G_GINT64_FORMAT " %s", val,
1447 gst_format_get_name (format));
1449 gst_query_set_position (query, format, val);
1454 gst_dvd_read_src_do_convert_query (GstDvdReadSrc * src, GstQuery * query)
1456 GstFormat src_format, dest_format;
1457 gboolean ret = FALSE;
1458 gint64 src_val, dest_val = -1;
1460 gst_query_parse_convert (query, &src_format, &src_val, &dest_format, NULL);
1462 if (src_format == dest_format) {
1468 /* Formats to consider: TIME, DEFAULT, BYTES, title, chapter, sector.
1469 * Note: title and chapter are counted as starting from 0 here, just like
1470 * in the context of seek events. Another note: DEFAULT format is undefined */
1472 if (src_format == GST_FORMAT_BYTES) {
1473 src_format = sector_format;
1474 src_val /= DVD_VIDEO_LB_LEN;
1477 if (src_format == sector_format) {
1479 if (dest_format == GST_FORMAT_TIME && src_val < G_MAXUINT) {
1480 dest_val = gst_dvd_read_src_get_time_for_sector (src, (guint) src_val);
1481 ret = (dest_val >= 0);
1482 } else if (dest_format == GST_FORMAT_BYTES) {
1483 dest_val = src_val * DVD_VIDEO_LB_LEN;
1488 } else if (src_format == title_format) {
1490 if (dest_format == GST_FORMAT_TIME) {
1491 /* not really true, but we use this to trick the base source into
1492 * handling seeks in title-format for us (the source won't know that
1493 * we changed the title in this case) (changing titles should really
1494 * be done with an interface rather than a seek, but for now we're
1495 * stuck with this mechanism. Fix in 0.11) */
1496 dest_val = (GstClockTime) 0;
1501 } else if (src_format == chapter_format) {
1502 /* CHAPTER => xyz */
1503 if (dest_format == GST_FORMAT_TIME) {
1504 if (src->num_chapters >= 0 && src_val < src->num_chapters) {
1505 dest_val = src->chapter_starts[src_val];
1508 } else if (dest_format == sector_format) {
1512 } else if (src_format == GST_FORMAT_TIME) {
1514 if (dest_format == sector_format || dest_format == GST_FORMAT_BYTES) {
1515 dest_val = gst_dvd_read_src_get_sector_from_time (src, src_val);
1516 ret = (dest_val >= 0);
1517 if (dest_format == GST_FORMAT_BYTES)
1518 dest_val *= DVD_VIDEO_LB_LEN;
1519 } else if (dest_format == chapter_format) {
1520 if (src->chapter_starts != NULL) {
1523 for (i = src->num_chapters - 1; i >= 0; --i) {
1524 if (src->chapter_starts && src->chapter_starts[i] >= src_val) {
1543 gst_query_set_convert (query, src_format, src_val, dest_format, dest_val);
1550 gst_dvd_read_src_src_query (GstBaseSrc * basesrc, GstQuery * query)
1552 GstDvdReadSrc *src = GST_DVD_READ_SRC (basesrc);
1554 gboolean res = TRUE;
1556 GST_LOG_OBJECT (src, "handling %s query",
1557 gst_query_type_get_name (GST_QUERY_TYPE (query)));
1559 GST_OBJECT_LOCK (src);
1560 started = (GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_FLAG_STARTED));
1561 GST_OBJECT_UNLOCK (src);
1564 GST_DEBUG_OBJECT (src, "query failed: not started");
1568 switch (GST_QUERY_TYPE (query)) {
1569 case GST_QUERY_DURATION:
1570 GST_OBJECT_LOCK (src);
1571 res = gst_dvd_read_src_do_duration_query (src, query);
1572 GST_OBJECT_UNLOCK (src);
1574 case GST_QUERY_POSITION:
1575 GST_OBJECT_LOCK (src);
1576 res = gst_dvd_read_src_do_position_query (src, query);
1577 GST_OBJECT_UNLOCK (src);
1579 case GST_QUERY_CONVERT:
1580 GST_OBJECT_LOCK (src);
1581 res = gst_dvd_read_src_do_convert_query (src, query);
1582 GST_OBJECT_UNLOCK (src);
1585 res = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
1593 gst_dvd_read_src_goto_sector (GstDvdReadSrc * src, int angle)
1595 gint seek_to = src->cur_pack;
1596 gint chapter, next, cur, i;
1598 /* retrieve position */
1600 GST_DEBUG_OBJECT (src, "Goto sector %d, angle %d, within %d chapters",
1601 seek_to, angle, src->num_chapters);
1603 for (i = 0; i < src->num_chapters; i++) {
1606 cur_title_get_chapter_bounds (src, i, &c1, &c2);
1607 GST_DEBUG_OBJECT (src, " Looking in chapter %d, bounds: %d %d", i, c1, c2);
1609 for (next = cur = c1; cur < c2;) {
1610 gint first = src->cur_pgc->cell_playback[cur].first_sector;
1611 gint last = src->cur_pgc->cell_playback[cur].last_sector;
1612 GST_DEBUG_OBJECT (src, "Cell %d sector bounds: %d %d", cur, first, last);
1613 /* seeking to 0 should end up at first chapter in any case */
1614 if ((seek_to >= first && seek_to <= last) || (seek_to == 0 && i == 0)) {
1615 GST_DEBUG_OBJECT (src, "Seek target found in chapter %d", i);
1620 if (src->cur_pgc->cell_playback[cur].block_type == BLOCK_TYPE_ANGLE_BLOCK)
1622 next = gst_dvd_read_src_get_next_cell (src, src->cur_pgc, cur);
1626 GST_DEBUG_OBJECT (src, "Seek to sector %u failed", seek_to);
1632 /* so chapter $chapter and cell $cur contain our sector
1633 * of interest. Let's go there! */
1634 GST_INFO_OBJECT (src, "Seek succeeded, going to chapter %u, cell %u",
1637 gst_dvd_read_src_goto_chapter (src, chapter);
1638 src->cur_cell = cur;
1639 src->next_cell = next;
1640 src->new_cell = FALSE;
1641 src->cur_pack = seek_to;
1648 /*** URI interface ***/
1651 gst_dvd_read_src_uri_get_type (GType type)
1656 static const gchar *const *
1657 gst_dvd_read_src_uri_get_protocols (GType type)
1659 static const gchar *protocols[] = { "dvd", NULL };
1665 gst_dvd_read_src_uri_get_uri (GstURIHandler * handler)
1667 GstDvdReadSrc *src = GST_DVD_READ_SRC (handler);
1670 GST_OBJECT_LOCK (src);
1671 uri = g_strdup_printf ("dvd://%d,%d,%d", src->uri_title, src->uri_chapter,
1673 GST_OBJECT_UNLOCK (src);
1679 gst_dvd_read_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
1682 GstDvdReadSrc *src = GST_DVD_READ_SRC (handler);
1684 /* parse out the new t/c/a and seek to them */
1686 gchar *location = NULL;
1691 location = gst_uri_get_location (uri);
1693 GST_OBJECT_LOCK (src);
1696 src->uri_chapter = 1;
1700 goto empty_location;
1702 strcur = strs = g_strsplit (location, ",", 0);
1703 while (strcur && *strcur) {
1706 if (!sscanf (*strcur, "%d", &val))
1710 g_warning ("Invalid value %d in URI '%s'. Must be 1 or greater",
1717 src->uri_title = val;
1720 src->uri_chapter = val;
1723 src->uri_angle = val;
1731 if (pos > 0 && GST_OBJECT_FLAG_IS_SET (src, GST_BASE_SRC_FLAG_STARTED)) {
1732 src->title = src->uri_title - 1;
1733 src->chapter = src->uri_chapter - 1;
1734 src->angle = src->uri_angle - 1;
1735 src->new_seek = TRUE;
1743 GST_OBJECT_UNLOCK (src);
1750 gst_dvd_read_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1752 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1754 iface->get_type = gst_dvd_read_src_uri_get_type;
1755 iface->get_protocols = gst_dvd_read_src_uri_get_protocols;
1756 iface->get_uri = gst_dvd_read_src_uri_get_uri;
1757 iface->set_uri = gst_dvd_read_src_uri_set_uri;
1761 plugin_init (GstPlugin * plugin)
1763 GST_DEBUG_CATEGORY_INIT (gstgst_dvd_read_src_debug, "dvdreadsrc", 0,
1764 "DVD reader element based on dvdreadsrc");
1767 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
1769 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
1770 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1771 #endif /* ENABLE_NLS */
1773 if (!gst_element_register (plugin, "dvdreadsrc", GST_RANK_SECONDARY,
1774 GST_TYPE_DVD_READ_SRC)) {
1781 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1784 "Access a DVD with dvdread",
1785 plugin_init, VERSION, "GPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);